blob: 9988f0615651801be965284d79d6b7716f454270 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Eli Friedman932197d2010-06-13 19:06:42 +00009#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010
Zachary Turner827d5d72016-12-16 04:27:00 +000011#include "llvm/Support/FormatAdapters.h"
12#include "llvm/Support/FormatVariadic.h"
13
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000014#include <mutex>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include <string>
Sean Callananfe38c852015-10-08 23:07:53 +000016#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017
Greg Clayton6beaaa62011-01-17 03:46:26 +000018
Kate Stoneb9c1b512016-09-06 20:57:50 +000019// Clang headers like to use NDEBUG inside of them to enable/disable debug
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000020// related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
Greg Clayton6beaaa62011-01-17 03:46:26 +000021// or another. This is bad because it means that if clang was built in release
22// mode, it assumes that you are building in release mode which is not always
23// the case. You can end up with functions that are defined as empty in header
24// files when NDEBUG is not defined, and this can cause link errors with the
25// clang .a files that you have since you might be missing functions in the .a
26// file. So we have to define NDEBUG when including clang headers to avoid any
27// mismatches. This is covered by rdar://problem/8691220
28
Sean Callanan3b1d4f62011-10-26 17:46:51 +000029#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000030#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000031#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000032// Need to include assert.h so it is as clang would expect it to be (disabled)
33#include <assert.h>
34#endif
35
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "clang/AST/ASTContext.h"
37#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000038#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000040#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000041#include "clang/AST/DeclTemplate.h"
Greg Claytonfe689042015-11-10 17:47:04 +000042#include "clang/AST/Mangle.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "clang/AST/RecordLayout.h"
44#include "clang/AST/Type.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000045#include "clang/AST/VTableBuilder.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000047#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000049#include "clang/Basic/FileSystemOptions.h"
Rainer Orth6ca17072019-08-05 14:00:43 +000050#include "clang/Basic/LangStandard.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051#include "clang/Basic/SourceManager.h"
52#include "clang/Basic/TargetInfo.h"
53#include "clang/Basic/TargetOptions.h"
54#include "clang/Frontend/FrontendOptions.h"
Raphael Isemannf74a4c12019-04-30 08:41:35 +000055#include "clang/Sema/Sema.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000056
57#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000058#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000059#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
60// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
61#include <assert.h>
62#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063
Greg Claytond8d4a572015-08-11 21:38:15 +000064#include "llvm/Support/Signals.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000065#include "llvm/Support/Threading.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000066
Zachary Turnerd133f6a2016-03-28 22:53:41 +000067#include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
Alex Langford9e865612019-09-09 23:11:43 +000068#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000069#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
70#include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000071#include "lldb/Utility/ArchSpec.h"
Zachary Turner01c32432017-02-14 19:06:07 +000072#include "lldb/Utility/Flags.h"
73
Zachary Turner29cb8682017-03-03 20:57:05 +000074#include "lldb/Core/DumpDataExtractor.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000075#include "lldb/Core/Module.h"
76#include "lldb/Core/PluginManager.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000077#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000078#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000079#include "lldb/Core/UniqueCStringMap.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000080#include "lldb/Symbol/ClangASTImporter.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000081#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000082#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000083#include "lldb/Symbol/ClangUtil.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000084#include "lldb/Symbol/ObjectFile.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000085#include "lldb/Symbol/SymbolFile.h"
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 Ingham151c0322015-09-15 21:13:50 +000088#include "lldb/Target/Process.h"
89#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000090#include "lldb/Utility/DataExtractor.h"
Sean Callananc530ba92016-05-02 21:15:31 +000091#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000092#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000093#include "lldb/Utility/RegularExpression.h"
Pavel Labathd821c992018-08-07 11:07:21 +000094#include "lldb/Utility/Scalar.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000095
Alex Langfordb57017102019-07-15 22:56:12 +000096#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
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;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106using namespace clang;
Pavel Labath6f23a682019-09-30 13:44:17 +0000107using llvm::StringSwitch;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109namespace {
Pavel Labath65a376f2019-08-21 13:11:30 +0000110#ifdef LLDB_CONFIGURATION_DEBUG
Alex Langfordb2232a12019-08-20 22:06:13 +0000111static void VerifyDecl(clang::Decl *decl) {
112 assert(decl && "VerifyDecl called with nullptr?");
113 decl->getAccess();
114}
Pavel Labath65a376f2019-08-21 13:11:30 +0000115#endif
Alex Langfordb2232a12019-08-20 22:06:13 +0000116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117static inline bool
118ClangASTContextSupportsLanguage(lldb::LanguageType language) {
119 return language == eLanguageTypeUnknown || // Clang is the default type system
Rainer Orth6ca17072019-08-05 14:00:43 +0000120 lldb_private::Language::LanguageIsC(language) ||
121 lldb_private::Language::LanguageIsCPlusPlus(language) ||
122 lldb_private::Language::LanguageIsObjC(language) ||
123 lldb_private::Language::LanguageIsPascal(language) ||
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 // Use Clang for Rust until there is a proper language plugin for it
125 language == eLanguageTypeRust ||
Johan Engelen04799572016-11-25 11:01:12 +0000126 language == eLanguageTypeExtRenderScript ||
127 // Use Clang for D until there is a proper language plugin for it
Bruce Mitchenerb8233f82018-11-27 05:37:27 +0000128 language == eLanguageTypeD ||
129 // Open Dylan compiler debug info is designed to be Clang-compatible
130 language == eLanguageTypeDylan;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131}
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000132
133// Checks whether m1 is an overload of m2 (as opposed to an override). This is
134// called by addOverridesForMethod to distinguish overrides (which share a
135// vtable entry) from overloads (which require distinct entries).
136bool isOverload(clang::CXXMethodDecl *m1, clang::CXXMethodDecl *m2) {
137 // FIXME: This should detect covariant return types, but currently doesn't.
138 lldbassert(&m1->getASTContext() == &m2->getASTContext() &&
139 "Methods should have the same AST context");
140 clang::ASTContext &context = m1->getASTContext();
141
142 const auto *m1Type = llvm::cast<clang::FunctionProtoType>(
143 context.getCanonicalType(m1->getType()));
144
145 const auto *m2Type = llvm::cast<clang::FunctionProtoType>(
146 context.getCanonicalType(m2->getType()));
147
148 auto compareArgTypes = [&context](const clang::QualType &m1p,
149 const clang::QualType &m2p) {
150 return context.hasSameType(m1p.getUnqualifiedType(),
151 m2p.getUnqualifiedType());
152 };
153
154 // FIXME: In C++14 and later, we can just pass m2Type->param_type_end()
155 // as a fourth parameter to std::equal().
156 return (m1->getNumParams() != m2->getNumParams()) ||
157 !std::equal(m1Type->param_type_begin(), m1Type->param_type_end(),
158 m2Type->param_type_begin(), compareArgTypes);
159}
160
161// If decl is a virtual method, walk the base classes looking for methods that
162// decl overrides. This table of overridden methods is used by IRGen to
163// determine the vtable layout for decl's parent class.
164void addOverridesForMethod(clang::CXXMethodDecl *decl) {
165 if (!decl->isVirtual())
166 return;
167
168 clang::CXXBasePaths paths;
169
170 auto find_overridden_methods =
171 [decl](const clang::CXXBaseSpecifier *specifier,
172 clang::CXXBasePath &path) {
173 if (auto *base_record = llvm::dyn_cast<clang::CXXRecordDecl>(
174 specifier->getType()->getAs<clang::RecordType>()->getDecl())) {
175
176 clang::DeclarationName name = decl->getDeclName();
177
178 // If this is a destructor, check whether the base class destructor is
179 // virtual.
180 if (name.getNameKind() == clang::DeclarationName::CXXDestructorName)
181 if (auto *baseDtorDecl = base_record->getDestructor()) {
182 if (baseDtorDecl->isVirtual()) {
183 path.Decls = baseDtorDecl;
184 return true;
185 } else
186 return false;
187 }
188
189 // Otherwise, search for name in the base class.
190 for (path.Decls = base_record->lookup(name); !path.Decls.empty();
191 path.Decls = path.Decls.slice(1)) {
192 if (auto *method_decl =
193 llvm::dyn_cast<clang::CXXMethodDecl>(path.Decls.front()))
194 if (method_decl->isVirtual() && !isOverload(decl, method_decl)) {
195 path.Decls = method_decl;
196 return true;
197 }
198 }
199 }
200
201 return false;
202 };
203
204 if (decl->getParent()->lookupInBases(find_overridden_methods, paths)) {
205 for (auto *overridden_decl : paths.found_decls())
206 decl->addOverriddenMethod(
207 llvm::cast<clang::CXXMethodDecl>(overridden_decl));
208 }
209}
Greg Clayton56939cb2015-09-17 22:23:34 +0000210}
211
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +0000212static lldb::addr_t GetVTableAddress(Process &process,
213 VTableContextBase &vtable_ctx,
214 ValueObject &valobj,
215 const ASTRecordLayout &record_layout) {
216 // Retrieve type info
217 CompilerType pointee_type;
218 CompilerType this_type(valobj.GetCompilerType());
219 uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
220 if (!type_info)
221 return LLDB_INVALID_ADDRESS;
222
223 // Check if it's a pointer or reference
224 bool ptr_or_ref = false;
225 if (type_info & (eTypeIsPointer | eTypeIsReference)) {
226 ptr_or_ref = true;
227 type_info = pointee_type.GetTypeInfo();
228 }
229
230 // We process only C++ classes
231 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
232 if ((type_info & cpp_class) != cpp_class)
233 return LLDB_INVALID_ADDRESS;
234
235 // Calculate offset to VTable pointer
236 lldb::offset_t vbtable_ptr_offset =
237 vtable_ctx.isMicrosoft() ? record_layout.getVBPtrOffset().getQuantity()
238 : 0;
239
240 if (ptr_or_ref) {
241 // We have a pointer / ref to object, so read
242 // VTable pointer from process memory
243
244 if (valobj.GetAddressTypeOfChildren() != eAddressTypeLoad)
245 return LLDB_INVALID_ADDRESS;
246
247 auto vbtable_ptr_addr = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
248 if (vbtable_ptr_addr == LLDB_INVALID_ADDRESS)
249 return LLDB_INVALID_ADDRESS;
250
251 vbtable_ptr_addr += vbtable_ptr_offset;
252
253 Status err;
254 return process.ReadPointerFromMemory(vbtable_ptr_addr, err);
255 }
256
257 // We have an object already read from process memory,
258 // so just extract VTable pointer from it
259
260 DataExtractor data;
261 Status err;
262 auto size = valobj.GetData(data, err);
263 if (err.Fail() || vbtable_ptr_offset + data.GetAddressByteSize() > size)
264 return LLDB_INVALID_ADDRESS;
265
266 return data.GetPointer(&vbtable_ptr_offset);
267}
268
269static int64_t ReadVBaseOffsetFromVTable(Process &process,
270 VTableContextBase &vtable_ctx,
271 lldb::addr_t vtable_ptr,
272 const CXXRecordDecl *cxx_record_decl,
273 const CXXRecordDecl *base_class_decl) {
274 if (vtable_ctx.isMicrosoft()) {
275 clang::MicrosoftVTableContext &msoft_vtable_ctx =
276 static_cast<clang::MicrosoftVTableContext &>(vtable_ctx);
277
278 // Get the index into the virtual base table. The
279 // index is the index in uint32_t from vbtable_ptr
280 const unsigned vbtable_index =
281 msoft_vtable_ctx.getVBTableIndex(cxx_record_decl, base_class_decl);
282 const lldb::addr_t base_offset_addr = vtable_ptr + vbtable_index * 4;
283 Status err;
284 return process.ReadSignedIntegerFromMemory(base_offset_addr, 4, INT64_MAX,
285 err);
286 }
287
288 clang::ItaniumVTableContext &itanium_vtable_ctx =
289 static_cast<clang::ItaniumVTableContext &>(vtable_ctx);
290
291 clang::CharUnits base_offset_offset =
292 itanium_vtable_ctx.getVirtualBaseOffsetOffset(cxx_record_decl,
293 base_class_decl);
294 const lldb::addr_t base_offset_addr =
295 vtable_ptr + base_offset_offset.getQuantity();
296 const uint32_t base_offset_size = process.GetAddressByteSize();
297 Status err;
298 return process.ReadSignedIntegerFromMemory(base_offset_addr, base_offset_size,
299 INT64_MAX, err);
300}
301
302static bool GetVBaseBitOffset(VTableContextBase &vtable_ctx,
303 ValueObject &valobj,
304 const ASTRecordLayout &record_layout,
305 const CXXRecordDecl *cxx_record_decl,
306 const CXXRecordDecl *base_class_decl,
307 int32_t &bit_offset) {
308 ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
309 Process *process = exe_ctx.GetProcessPtr();
310 if (!process)
311 return false;
312
313 lldb::addr_t vtable_ptr =
314 GetVTableAddress(*process, vtable_ctx, valobj, record_layout);
315 if (vtable_ptr == LLDB_INVALID_ADDRESS)
316 return false;
317
318 auto base_offset = ReadVBaseOffsetFromVTable(
319 *process, vtable_ctx, vtable_ptr, cxx_record_decl, base_class_decl);
320 if (base_offset == INT64_MAX)
321 return false;
322
323 bit_offset = base_offset * 8;
324
325 return true;
326}
327
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *>
329 ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000330
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331static ClangASTMap &GetASTMap() {
332 static ClangASTMap *g_map_ptr = nullptr;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000333 static llvm::once_flag g_once_flag;
334 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
336 });
337 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000338}
339
Raphael Isemann92d5ea5d2019-11-27 10:27:25 +0100340char ClangASTContext::ID;
341
Raphael Isemann2f323fc2019-08-28 13:46:01 +0000342bool ClangASTContext::IsOperator(llvm::StringRef name,
Davide Italiano7e3ef4d2018-03-20 19:46:32 +0000343 clang::OverloadedOperatorKind &op_kind) {
Raphael Isemann2f323fc2019-08-28 13:46:01 +0000344 // All operators have to start with "operator".
345 if (!name.consume_front("operator"))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000347
Raphael Isemann2f323fc2019-08-28 13:46:01 +0000348 // Remember if there was a space after "operator". This is necessary to
349 // check for collisions with strangely named functions like "operatorint()".
350 bool space_after_operator = name.consume_front(" ");
Pavel Labath1ac2b202016-08-15 14:32:32 +0000351
Raphael Isemann2f323fc2019-08-28 13:46:01 +0000352 op_kind = StringSwitch<clang::OverloadedOperatorKind>(name)
353 .Case("+", clang::OO_Plus)
354 .Case("+=", clang::OO_PlusEqual)
355 .Case("++", clang::OO_PlusPlus)
356 .Case("-", clang::OO_Minus)
357 .Case("-=", clang::OO_MinusEqual)
358 .Case("--", clang::OO_MinusMinus)
359 .Case("->", clang::OO_Arrow)
360 .Case("->*", clang::OO_ArrowStar)
361 .Case("*", clang::OO_Star)
362 .Case("*=", clang::OO_StarEqual)
363 .Case("/", clang::OO_Slash)
364 .Case("/=", clang::OO_SlashEqual)
365 .Case("%", clang::OO_Percent)
366 .Case("%=", clang::OO_PercentEqual)
367 .Case("^", clang::OO_Caret)
368 .Case("^=", clang::OO_CaretEqual)
369 .Case("&", clang::OO_Amp)
370 .Case("&=", clang::OO_AmpEqual)
371 .Case("&&", clang::OO_AmpAmp)
372 .Case("|", clang::OO_Pipe)
373 .Case("|=", clang::OO_PipeEqual)
374 .Case("||", clang::OO_PipePipe)
375 .Case("~", clang::OO_Tilde)
376 .Case("!", clang::OO_Exclaim)
377 .Case("!=", clang::OO_ExclaimEqual)
378 .Case("=", clang::OO_Equal)
379 .Case("==", clang::OO_EqualEqual)
380 .Case("<", clang::OO_Less)
381 .Case("<<", clang::OO_LessLess)
382 .Case("<<=", clang::OO_LessLessEqual)
383 .Case("<=", clang::OO_LessEqual)
384 .Case(">", clang::OO_Greater)
385 .Case(">>", clang::OO_GreaterGreater)
386 .Case(">>=", clang::OO_GreaterGreaterEqual)
387 .Case(">=", clang::OO_GreaterEqual)
388 .Case("()", clang::OO_Call)
389 .Case("[]", clang::OO_Subscript)
390 .Case(",", clang::OO_Comma)
391 .Default(clang::NUM_OVERLOADED_OPERATORS);
Pavel Labath1ac2b202016-08-15 14:32:32 +0000392
Raphael Isemann2f323fc2019-08-28 13:46:01 +0000393 // We found a fitting operator, so we can exit now.
394 if (op_kind != clang::NUM_OVERLOADED_OPERATORS)
395 return true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000396
Raphael Isemann2f323fc2019-08-28 13:46:01 +0000397 // After the "operator " or "operator" part is something unknown. This means
398 // it's either one of the named operators (new/delete), a conversion operator
399 // (e.g. operator bool) or a function which name starts with "operator"
400 // (e.g. void operatorbool).
Pavel Labath1ac2b202016-08-15 14:32:32 +0000401
Raphael Isemann2f323fc2019-08-28 13:46:01 +0000402 // If it's a function that starts with operator it can't have a space after
403 // "operator" because identifiers can't contain spaces.
404 // E.g. "operator int" (conversion operator)
405 // vs. "operatorint" (function with colliding name).
406 if (!space_after_operator)
407 return false; // not an operator.
Pavel Labath1ac2b202016-08-15 14:32:32 +0000408
Raphael Isemann2f323fc2019-08-28 13:46:01 +0000409 // Now the operator is either one of the named operators or a conversion
410 // operator.
411 op_kind = StringSwitch<clang::OverloadedOperatorKind>(name)
412 .Case("new", clang::OO_New)
413 .Case("new[]", clang::OO_Array_New)
414 .Case("delete", clang::OO_Delete)
415 .Case("delete[]", clang::OO_Array_Delete)
416 // conversion operators hit this case.
417 .Default(clang::NUM_OVERLOADED_OPERATORS);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418
419 return true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000420}
Enrico Granata5d84a692014-08-19 21:46:37 +0000421
Greg Clayton57ee3062013-07-11 22:46:58 +0000422clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
424 switch (access) {
425 default:
426 break;
427 case eAccessNone:
Greg Clayton8cf05932010-07-22 18:30:50 +0000428 return AS_none;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429 case eAccessPublic:
430 return AS_public;
431 case eAccessPrivate:
432 return AS_private;
433 case eAccessProtected:
434 return AS_protected;
435 }
436 return AS_none;
Greg Clayton8cf05932010-07-22 18:30:50 +0000437}
438
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
440 // FIXME: Cleanup per-file based stuff.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441
Adrian Prantl05097242018-04-30 16:49:04 +0000442 // Set some properties which depend solely on the input kind; it would be
443 // nice to move these to the language standard, and have the driver resolve
444 // the input kind + language standard.
Rainer Orth6ca17072019-08-05 14:00:43 +0000445 if (IK.getLanguage() == clang::Language::Asm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 Opts.AsmPreprocessor = 1;
Richard Smith8186cd42017-04-26 22:10:53 +0000447 } else if (IK.isObjectiveC()) {
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000448 Opts.ObjC = 1;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 }
450
451 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
452
453 if (LangStd == LangStandard::lang_unspecified) {
454 // Based on the base language, pick one.
Richard Smith8186cd42017-04-26 22:10:53 +0000455 switch (IK.getLanguage()) {
Rainer Orth6ca17072019-08-05 14:00:43 +0000456 case clang::Language::Unknown:
457 case clang::Language::LLVM_IR:
458 case clang::Language::RenderScript:
David Blaikiea322f362017-01-06 00:38:06 +0000459 llvm_unreachable("Invalid input kind!");
Rainer Orth6ca17072019-08-05 14:00:43 +0000460 case clang::Language::OpenCL:
Pavel Labath47168542017-04-27 08:49:19 +0000461 LangStd = LangStandard::lang_opencl10;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 break;
Rainer Orth6ca17072019-08-05 14:00:43 +0000463 case clang::Language::CUDA:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 LangStd = LangStandard::lang_cuda;
465 break;
Rainer Orth6ca17072019-08-05 14:00:43 +0000466 case clang::Language::Asm:
467 case clang::Language::C:
468 case clang::Language::ObjC:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469 LangStd = LangStandard::lang_gnu99;
470 break;
Rainer Orth6ca17072019-08-05 14:00:43 +0000471 case clang::Language::CXX:
472 case clang::Language::ObjCXX:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473 LangStd = LangStandard::lang_gnucxx98;
474 break;
Rainer Orth6ca17072019-08-05 14:00:43 +0000475 case clang::Language::HIP:
Benjamin Kramer0d97c222018-04-25 13:22:47 +0000476 LangStd = LangStandard::lang_hip;
477 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000479 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480
Kate Stoneb9c1b512016-09-06 20:57:50 +0000481 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
482 Opts.LineComment = Std.hasLineComments();
483 Opts.C99 = Std.isC99();
484 Opts.CPlusPlus = Std.isCPlusPlus();
485 Opts.CPlusPlus11 = Std.isCPlusPlus11();
486 Opts.Digraphs = Std.hasDigraphs();
487 Opts.GNUMode = Std.isGNUMode();
488 Opts.GNUInline = !Std.isC99();
489 Opts.HexFloats = Std.hasHexFloats();
490 Opts.ImplicitInt = Std.hasImplicitInt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494 // OpenCL has some additional defaults.
Pavel Labath47168542017-04-27 08:49:19 +0000495 if (LangStd == LangStandard::lang_opencl10) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496 Opts.OpenCL = 1;
497 Opts.AltiVec = 1;
498 Opts.CXXOperatorNames = 1;
Richard Smithc6245102019-09-13 06:02:15 +0000499 Opts.setLaxVectorConversions(LangOptions::LaxVectorConversionKind::All);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000501
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502 // OpenCL and C++ both have bool, true, false keywords.
503 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000504
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506
Adrian Prantl05097242018-04-30 16:49:04 +0000507 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is
508 // specified, or -std is set to a conforming mode.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000509 Opts.Trigraphs = !Opts.GNUMode;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511 Opts.OptimizeSize = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 // FIXME: Eliminate this dependency.
514 // unsigned Opt =
515 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
516 // Opts.Optimize = Opt != 0;
517 unsigned Opt = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000518
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519 // This is the __NO_INLINE__ define, which just depends on things like the
520 // optimization level and -fno-inline, not actually whether the backend has
521 // inlining enabled.
522 //
523 // FIXME: This is affected by other options (-fno-inline).
524 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525}
526
Raphael Isemann92d5ea5d2019-11-27 10:27:25 +0100527ClangASTContext::ClangASTContext(llvm::StringRef target_triple) {
Raphael Isemannd01b4a72019-10-01 12:28:14 +0000528 if (!target_triple.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529 SetTargetTriple(target_triple);
Raphael Isemann2eb963a2019-10-02 12:26:08 +0000530 // The caller didn't pass an ASTContext so create a new one for this
531 // ClangASTContext.
532 CreateASTContext();
533}
534
Raphael Isemann92d5ea5d2019-11-27 10:27:25 +0100535ClangASTContext::ClangASTContext(ArchSpec arch) {
Raphael Isemann2eb963a2019-10-02 12:26:08 +0000536 SetTargetTriple(arch.GetTriple().str());
537 // The caller didn't pass an ASTContext so create a new one for this
538 // ClangASTContext.
539 CreateASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540}
541
Raphael Isemann92d5ea5d2019-11-27 10:27:25 +0100542ClangASTContext::ClangASTContext(ASTContext &existing_ctxt) {
Raphael Isemannc73bfc92019-10-01 12:55:37 +0000543 SetTargetTriple(existing_ctxt.getTargetInfo().getTriple().str());
544
545 m_ast_up.reset(&existing_ctxt);
546 GetASTMap().Insert(&existing_ctxt, this);
547}
548
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549// Destructor
Kate Stoneb9c1b512016-09-06 20:57:50 +0000550ClangASTContext::~ClangASTContext() { Finalize(); }
551
552ConstString ClangASTContext::GetPluginNameStatic() {
553 return ConstString("clang");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000554}
555
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556ConstString ClangASTContext::GetPluginName() {
557 return ClangASTContext::GetPluginNameStatic();
Greg Clayton56939cb2015-09-17 22:23:34 +0000558}
559
Kate Stoneb9c1b512016-09-06 20:57:50 +0000560uint32_t ClangASTContext::GetPluginVersion() { return 1; }
Greg Clayton56939cb2015-09-17 22:23:34 +0000561
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
563 lldb_private::Module *module,
564 Target *target) {
Raphael Isemann76016f92019-11-29 12:40:19 +0100565 if (!ClangASTContextSupportsLanguage(language))
566 return lldb::TypeSystemSP();
567 ArchSpec arch;
568 if (module)
569 arch = module->GetArchitecture();
570 else if (target)
571 arch = target->GetArchitecture();
Greg Clayton56939cb2015-09-17 22:23:34 +0000572
Raphael Isemann76016f92019-11-29 12:40:19 +0100573 if (!arch.IsValid())
574 return lldb::TypeSystemSP();
Greg Clayton56939cb2015-09-17 22:23:34 +0000575
Raphael Isemann76016f92019-11-29 12:40:19 +0100576 ArchSpec fixed_arch = arch;
577 // LLVM wants this to be set to iOS or MacOSX; if we're working on
578 // a bare-boards type image, change the triple for llvm's benefit.
579 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
580 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
581 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
582 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
583 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64_32 ||
584 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
585 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
586 } else {
587 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589 }
Raphael Isemann76016f92019-11-29 12:40:19 +0100590
591 if (module) {
592 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext(fixed_arch));
593 return ast_sp;
594 } else if (target && target->IsValid()) {
595 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
596 new ClangASTContextForExpressions(*target, fixed_arch));
597 ast_sp->m_scratch_ast_source_up.reset(
598 new ClangASTSource(target->shared_from_this()));
599 lldbassert(ast_sp->getFileManager());
600 ast_sp->m_scratch_ast_source_up->InstallASTContext(
Raphael Isemannbc7f1df2019-11-29 13:02:41 +0100601 *ast_sp, *ast_sp->getFileManager(), true);
Raphael Isemann76016f92019-11-29 12:40:19 +0100602 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
603 ast_sp->m_scratch_ast_source_up->CreateProxy());
604 ast_sp->SetExternalSource(proxy_ast_source);
605 return ast_sp;
606 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607 return lldb::TypeSystemSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608}
609
Adrian Prantlaa97a892019-08-22 21:45:58 +0000610LanguageSet ClangASTContext::GetSupportedLanguagesForTypes() {
611 LanguageSet languages;
612 languages.Insert(lldb::eLanguageTypeC89);
613 languages.Insert(lldb::eLanguageTypeC);
614 languages.Insert(lldb::eLanguageTypeC11);
615 languages.Insert(lldb::eLanguageTypeC_plus_plus);
616 languages.Insert(lldb::eLanguageTypeC99);
617 languages.Insert(lldb::eLanguageTypeObjC);
618 languages.Insert(lldb::eLanguageTypeObjC_plus_plus);
619 languages.Insert(lldb::eLanguageTypeC_plus_plus_03);
620 languages.Insert(lldb::eLanguageTypeC_plus_plus_11);
621 languages.Insert(lldb::eLanguageTypeC11);
622 languages.Insert(lldb::eLanguageTypeC_plus_plus_14);
623 return languages;
624}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625
Adrian Prantlaa97a892019-08-22 21:45:58 +0000626LanguageSet ClangASTContext::GetSupportedLanguagesForExpressions() {
627 LanguageSet languages;
628 languages.Insert(lldb::eLanguageTypeC_plus_plus);
629 languages.Insert(lldb::eLanguageTypeObjC_plus_plus);
630 languages.Insert(lldb::eLanguageTypeC_plus_plus_03);
631 languages.Insert(lldb::eLanguageTypeC_plus_plus_11);
632 languages.Insert(lldb::eLanguageTypeC_plus_plus_14);
633 return languages;
Enrico Granata5d84a692014-08-19 21:46:37 +0000634}
635
Kate Stoneb9c1b512016-09-06 20:57:50 +0000636void ClangASTContext::Initialize() {
Adrian Prantlaa97a892019-08-22 21:45:58 +0000637 PluginManager::RegisterPlugin(
638 GetPluginNameStatic(), "clang base AST context plug-in", CreateInstance,
639 GetSupportedLanguagesForTypes(), GetSupportedLanguagesForExpressions());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640}
641
Kate Stoneb9c1b512016-09-06 20:57:50 +0000642void ClangASTContext::Terminate() {
643 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000644}
645
Kate Stoneb9c1b512016-09-06 20:57:50 +0000646void ClangASTContext::Finalize() {
Raphael Isemann2eb963a2019-10-02 12:26:08 +0000647 assert(m_ast_up);
648 GetASTMap().Erase(m_ast_up.get());
649 if (!m_ast_owned)
650 m_ast_up.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000652 m_builtins_up.reset();
653 m_selector_table_up.reset();
654 m_identifier_table_up.reset();
655 m_target_info_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000656 m_target_options_rp.reset();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000657 m_diagnostics_engine_up.reset();
658 m_source_manager_up.reset();
659 m_language_options_up.reset();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000660 m_scratch_ast_source_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661}
662
Raphael Isemannf74a4c12019-04-30 08:41:35 +0000663void ClangASTContext::setSema(Sema *s) {
664 // Ensure that the new sema actually belongs to our ASTContext.
665 assert(s == nullptr || &s->getASTContext() == m_ast_up.get());
666 m_sema = s;
667}
668
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669const char *ClangASTContext::GetTargetTriple() {
670 return m_target_triple.c_str();
671}
672
Raphael Isemannd01b4a72019-10-01 12:28:14 +0000673void ClangASTContext::SetTargetTriple(llvm::StringRef target_triple) {
Raphael Isemannd01b4a72019-10-01 12:28:14 +0000674 m_target_triple = target_triple.str();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000675}
676
Kate Stoneb9c1b512016-09-06 20:57:50 +0000677void ClangASTContext::SetExternalSource(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000678 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000679 ASTContext *ast = getASTContext();
680 if (ast) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000681 ast->setExternalSource(ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000682 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000683 }
684}
685
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686ASTContext *ClangASTContext::getASTContext() {
Raphael Isemann2eb963a2019-10-02 12:26:08 +0000687 assert(m_ast_up);
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000688 return m_ast_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000689}
690
Raphael Isemann2eb963a2019-10-02 12:26:08 +0000691void ClangASTContext::CreateASTContext() {
692 assert(!m_ast_up);
693 m_ast_owned = true;
694 m_ast_up.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
695 *getIdentifierTable(), *getSelectorTable(),
696 *getBuiltinContext()));
697
698 m_ast_up->getDiagnostics().setClient(getDiagnosticConsumer(), false);
699
700 // This can be NULL if we don't know anything about the architecture or if
701 // the target for an architecture isn't enabled in the llvm/clang that we
702 // built
703 TargetInfo *target_info = getTargetInfo();
704 if (target_info)
705 m_ast_up->InitBuiltinTypes(*target_info);
706
707 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
708 m_ast_up->getTranslationUnitDecl()->setHasExternalLexicalStorage();
709 // m_ast_up->getTranslationUnitDecl()->setHasExternalVisibleStorage();
710 }
711
712 GetASTMap().Insert(m_ast_up.get(), this);
713
714 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_up(
715 new ClangExternalASTSourceCallbacks(
716 ClangASTContext::CompleteTagDecl,
717 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
718 ClangASTContext::LayoutRecordType, this));
719 SetExternalSource(ast_source_up);
720}
721
Kate Stoneb9c1b512016-09-06 20:57:50 +0000722ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
723 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
724 return clang_ast;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725}
726
Kate Stoneb9c1b512016-09-06 20:57:50 +0000727Builtin::Context *ClangASTContext::getBuiltinContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000728 if (m_builtins_up == nullptr)
729 m_builtins_up.reset(new Builtin::Context());
730 return m_builtins_up.get();
Sean Callanan79439e82010-11-18 02:56:27 +0000731}
732
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733IdentifierTable *ClangASTContext::getIdentifierTable() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000734 if (m_identifier_table_up == nullptr)
735 m_identifier_table_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000737 return m_identifier_table_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738}
739
Kate Stoneb9c1b512016-09-06 20:57:50 +0000740LangOptions *ClangASTContext::getLanguageOptions() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000741 if (m_language_options_up == nullptr) {
742 m_language_options_up.reset(new LangOptions());
Rainer Orth6ca17072019-08-05 14:00:43 +0000743 ParseLangArgs(*m_language_options_up, clang::Language::ObjCXX,
744 GetTargetTriple());
745 // InitializeLangOptions(*m_language_options_up, Language::ObjCXX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000746 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000747 return m_language_options_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748}
749
Kate Stoneb9c1b512016-09-06 20:57:50 +0000750SelectorTable *ClangASTContext::getSelectorTable() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000751 if (m_selector_table_up == nullptr)
752 m_selector_table_up.reset(new SelectorTable());
753 return m_selector_table_up.get();
Greg Claytonfe689042015-11-10 17:47:04 +0000754}
755
Kate Stoneb9c1b512016-09-06 20:57:50 +0000756clang::FileManager *ClangASTContext::getFileManager() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000757 if (m_file_manager_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000758 clang::FileSystemOptions file_system_options;
Jonas Devlieghere9764b652019-02-18 20:31:18 +0000759 m_file_manager_up.reset(new clang::FileManager(
760 file_system_options, FileSystem::Instance().GetVirtualFileSystem()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000762 return m_file_manager_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000763}
764
765clang::SourceManager *ClangASTContext::getSourceManager() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000766 if (m_source_manager_up == nullptr)
767 m_source_manager_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000768 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000769 return m_source_manager_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000770}
771
772clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000773 if (m_diagnostics_engine_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000775 m_diagnostics_engine_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
777 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000778 return m_diagnostics_engine_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000779}
780
781clang::MangleContext *ClangASTContext::getMangleContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000782 if (m_mangle_ctx_up == nullptr)
783 m_mangle_ctx_up.reset(getASTContext()->createMangleContext());
784 return m_mangle_ctx_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000785}
786
787class NullDiagnosticConsumer : public DiagnosticConsumer {
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000788public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000789 NullDiagnosticConsumer() {
790 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
791 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000792
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
Raphael Isemann17566302019-05-03 10:03:28 +0000794 const clang::Diagnostic &info) override {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000795 if (m_log) {
796 llvm::SmallVector<char, 32> diag_str(10);
797 info.FormatDiagnostic(diag_str);
798 diag_str.push_back('\0');
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000799 LLDB_LOGF(m_log, "Compiler diagnostic: %s\n", diag_str.data());
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000800 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000801 }
802
803 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
804 return new NullDiagnosticConsumer();
805 }
806
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000807private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000808 Log *m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000809};
810
Kate Stoneb9c1b512016-09-06 20:57:50 +0000811DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000812 if (m_diagnostic_consumer_up == nullptr)
813 m_diagnostic_consumer_up.reset(new NullDiagnosticConsumer);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000814
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000815 return m_diagnostic_consumer_up.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000816}
817
Kate Stoneb9c1b512016-09-06 20:57:50 +0000818std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000819 if (m_target_options_rp == nullptr && !m_target_triple.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000820 m_target_options_rp = std::make_shared<clang::TargetOptions>();
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000821 if (m_target_options_rp != nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000822 m_target_options_rp->Triple = m_target_triple;
823 }
824 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000825}
826
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827TargetInfo *ClangASTContext::getTargetInfo() {
828 // target_triple should be something like "x86_64-apple-macosx"
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000829 if (m_target_info_up == nullptr && !m_target_triple.empty())
830 m_target_info_up.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000831 getTargetOptions()));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000832 return m_target_info_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000833}
834
835#pragma mark Basic Types
836
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
838 ASTContext *ast, QualType qual_type) {
839 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000840 return qual_type_bit_size == bit_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000841}
Greg Clayton56939cb2015-09-17 22:23:34 +0000842
Greg Claytona1e5dc82015-08-11 22:53:00 +0000843CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
845 size_t bit_size) {
846 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
847 getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000848}
849
Kate Stoneb9c1b512016-09-06 20:57:50 +0000850CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
851 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
Alex Langfordbddab072019-08-13 19:40:36 +0000852 auto *clang_ast_context = ClangASTContext::GetASTContext(ast);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000853 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000854 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000855 switch (encoding) {
856 case eEncodingInvalid:
857 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000858 return CompilerType(clang_ast_context, ast->VoidPtrTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000859 break;
860
861 case eEncodingUint:
862 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000863 return CompilerType(clang_ast_context,
864 ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000865 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000866 return CompilerType(clang_ast_context,
867 ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000868 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000869 return CompilerType(clang_ast_context,
870 ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000871 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000872 return CompilerType(clang_ast_context,
873 ast->UnsignedLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000874 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000875 return CompilerType(clang_ast_context,
876 ast->UnsignedLongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000877 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +0000878 return CompilerType(clang_ast_context,
879 ast->UnsignedInt128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000880 break;
881
882 case eEncodingSint:
883 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000884 return CompilerType(clang_ast_context,
885 ast->SignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000886 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000887 return CompilerType(clang_ast_context, ast->ShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000888 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000889 return CompilerType(clang_ast_context, ast->IntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000890 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000891 return CompilerType(clang_ast_context, ast->LongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000892 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000893 return CompilerType(clang_ast_context, ast->LongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000894 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +0000895 return CompilerType(clang_ast_context, ast->Int128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000896 break;
897
898 case eEncodingIEEE754:
899 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000900 return CompilerType(clang_ast_context, ast->FloatTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000902 return CompilerType(clang_ast_context, ast->DoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000903 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000904 return CompilerType(clang_ast_context,
905 ast->LongDoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000906 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000907 return CompilerType(clang_ast_context, ast->HalfTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000908 break;
909
910 case eEncodingVector:
911 // Sanity check that bit_size is a multiple of 8's.
912 if (bit_size && !(bit_size & 0x7u))
913 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +0000914 clang_ast_context,
915 ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8)
916 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000917 break;
918 }
919
920 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000921}
922
Greg Clayton57ee3062013-07-11 22:46:58 +0000923lldb::BasicType
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000924ClangASTContext::GetBasicTypeEnumeration(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000925 if (name) {
926 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
927 static TypeNameToBasicTypeMap g_type_map;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000928 static llvm::once_flag g_once_flag;
929 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000930 // "void"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000931 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000932
933 // "char"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000934 g_type_map.Append(ConstString("char"), eBasicTypeChar);
935 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
936 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
937 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
938 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
939 g_type_map.Append(ConstString("unsigned wchar_t"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000940 eBasicTypeUnsignedWChar);
941 // "short"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000942 g_type_map.Append(ConstString("short"), eBasicTypeShort);
943 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
944 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
945 g_type_map.Append(ConstString("unsigned short int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000946 eBasicTypeUnsignedShort);
947
948 // "int"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000949 g_type_map.Append(ConstString("int"), eBasicTypeInt);
950 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
951 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
952 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000953
954 // "long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000955 g_type_map.Append(ConstString("long"), eBasicTypeLong);
956 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
957 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
958 g_type_map.Append(ConstString("unsigned long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000959 eBasicTypeUnsignedLong);
960
961 // "long long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000962 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
963 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
964 g_type_map.Append(ConstString("unsigned long long"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000965 eBasicTypeUnsignedLongLong);
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000966 g_type_map.Append(ConstString("unsigned long long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000967 eBasicTypeUnsignedLongLong);
968
969 // "int128"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000970 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
971 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000972
973 // Miscellaneous
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000974 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
975 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
976 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
977 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
978 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
979 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
980 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981 g_type_map.Sort();
982 });
983
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000984 return g_type_map.Find(name, eBasicTypeInvalid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000985 }
986 return eBasicTypeInvalid;
Greg Clayton57ee3062013-07-11 22:46:58 +0000987}
988
Raphael Isemannc502bae2019-11-20 12:40:08 +0100989CompilerType ClangASTContext::GetBasicType(ConstString name) {
990 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
991 return GetBasicType(basic_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992}
993
994uint32_t ClangASTContext::GetPointerByteSize() {
995 if (m_pointer_byte_size == 0)
Adrian Prantld963a7c2019-01-15 18:07:52 +0000996 if (auto size = GetBasicType(lldb::eBasicTypeVoid)
997 .GetPointerType()
998 .GetByteSize(nullptr))
999 m_pointer_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001000 return m_pointer_byte_size;
1001}
1002
1003CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
Raphael Isemannc502bae2019-11-20 12:40:08 +01001004 clang::ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005
Kate Stoneb9c1b512016-09-06 20:57:50 +00001006 lldb::opaque_compiler_type_t clang_type =
1007 GetOpaqueCompilerType(ast, basic_type);
1008
1009 if (clang_type)
1010 return CompilerType(GetASTContext(ast), clang_type);
1011 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001012}
1013
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1015 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1016 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001017
Kate Stoneb9c1b512016-09-06 20:57:50 +00001018#define streq(a, b) strcmp(a, b) == 0
1019 assert(ast != nullptr);
1020 if (ast) {
1021 switch (dw_ate) {
1022 default:
1023 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001024
Kate Stoneb9c1b512016-09-06 20:57:50 +00001025 case DW_ATE_address:
1026 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001027 return CompilerType(this, ast->VoidPtrTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001028 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001029
Kate Stoneb9c1b512016-09-06 20:57:50 +00001030 case DW_ATE_boolean:
1031 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001032 return CompilerType(this, ast->BoolTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001033 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001034 return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001035 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001036 return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001037 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001038 return CompilerType(this, ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001039 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001040
Kate Stoneb9c1b512016-09-06 20:57:50 +00001041 case DW_ATE_lo_user:
1042 // This has been seen to mean DW_AT_complex_integer
1043 if (type_name) {
1044 if (::strstr(type_name, "complex")) {
1045 CompilerType complex_int_clang_type =
1046 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1047 bit_size / 2);
Alex Langfordbddab072019-08-13 19:40:36 +00001048 return CompilerType(
1049 this, ast->getComplexType(
1050 ClangUtil::GetQualType(complex_int_clang_type))
1051 .getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001052 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001053 }
1054 break;
1055
1056 case DW_ATE_complex_float:
1057 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001058 return CompilerType(this, ast->FloatComplexTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001059 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001060 return CompilerType(this, ast->DoubleComplexTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001061 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001062 return CompilerType(this, ast->LongDoubleComplexTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001063 else {
1064 CompilerType complex_float_clang_type =
1065 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1066 bit_size / 2);
Alex Langfordbddab072019-08-13 19:40:36 +00001067 return CompilerType(
1068 this, ast->getComplexType(
1069 ClangUtil::GetQualType(complex_float_clang_type))
1070 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001071 }
1072 break;
1073
1074 case DW_ATE_float:
1075 if (streq(type_name, "float") &&
1076 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001077 return CompilerType(this, ast->FloatTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001078 if (streq(type_name, "double") &&
1079 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001080 return CompilerType(this, ast->DoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001081 if (streq(type_name, "long double") &&
1082 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001083 return CompilerType(this, ast->LongDoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001084 // Fall back to not requiring a name match
1085 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001086 return CompilerType(this, ast->FloatTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001087 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001088 return CompilerType(this, ast->DoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001089 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001090 return CompilerType(this, ast->LongDoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001091 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001092 return CompilerType(this, ast->HalfTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001093 break;
1094
1095 case DW_ATE_signed:
1096 if (type_name) {
1097 if (streq(type_name, "wchar_t") &&
1098 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1099 (getTargetInfo() &&
1100 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
Alex Langfordbddab072019-08-13 19:40:36 +00001101 return CompilerType(this, ast->WCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102 if (streq(type_name, "void") &&
1103 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001104 return CompilerType(this, ast->VoidTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001105 if (strstr(type_name, "long long") &&
1106 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001107 return CompilerType(this, ast->LongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108 if (strstr(type_name, "long") &&
1109 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001110 return CompilerType(this, ast->LongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001111 if (strstr(type_name, "short") &&
1112 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001113 return CompilerType(this, ast->ShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001114 if (strstr(type_name, "char")) {
1115 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001116 return CompilerType(this, ast->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001117 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001118 return CompilerType(this, ast->SignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001119 }
1120 if (strstr(type_name, "int")) {
1121 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001122 return CompilerType(this, ast->IntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001123 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00001124 return CompilerType(this, ast->Int128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001125 }
1126 }
1127 // We weren't able to match up a type name, just search by size
1128 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001129 return CompilerType(this, ast->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001131 return CompilerType(this, ast->ShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001132 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001133 return CompilerType(this, ast->IntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001134 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001135 return CompilerType(this, ast->LongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001136 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001137 return CompilerType(this, ast->LongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00001139 return CompilerType(this, ast->Int128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001140 break;
1141
1142 case DW_ATE_signed_char:
1143 if (ast->getLangOpts().CharIsSigned && type_name &&
1144 streq(type_name, "char")) {
1145 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001146 return CompilerType(this, ast->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001147 }
1148 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001149 return CompilerType(this, ast->SignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001150 break;
1151
1152 case DW_ATE_unsigned:
1153 if (type_name) {
1154 if (streq(type_name, "wchar_t")) {
1155 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1156 if (!(getTargetInfo() &&
1157 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
Alex Langfordbddab072019-08-13 19:40:36 +00001158 return CompilerType(this, ast->WCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001159 }
1160 }
1161 if (strstr(type_name, "long long")) {
1162 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001163 return CompilerType(this, ast->UnsignedLongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001164 } else if (strstr(type_name, "long")) {
1165 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001166 return CompilerType(this, ast->UnsignedLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001167 } else if (strstr(type_name, "short")) {
1168 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001169 return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001170 } else if (strstr(type_name, "char")) {
1171 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001172 return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001173 } else if (strstr(type_name, "int")) {
1174 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001175 return CompilerType(this, ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001176 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00001177 return CompilerType(this, ast->UnsignedInt128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001178 }
1179 }
1180 // We weren't able to match up a type name, just search by size
1181 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001182 return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001183 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001184 return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001185 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001186 return CompilerType(this, ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001187 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001188 return CompilerType(this, ast->UnsignedLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001189 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001190 return CompilerType(this, ast->UnsignedLongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001191 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00001192 return CompilerType(this, ast->UnsignedInt128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001193 break;
1194
1195 case DW_ATE_unsigned_char:
1196 if (!ast->getLangOpts().CharIsSigned && type_name &&
1197 streq(type_name, "char")) {
1198 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001199 return CompilerType(this, ast->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001200 }
1201 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001202 return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001203 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001204 return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001205 break;
1206
1207 case DW_ATE_imaginary_float:
1208 break;
1209
1210 case DW_ATE_UTF:
1211 if (type_name) {
Jonas Devliegherec46d39b2019-08-21 21:30:55 +00001212 if (streq(type_name, "char16_t"))
Alex Langfordbddab072019-08-13 19:40:36 +00001213 return CompilerType(this, ast->Char16Ty.getAsOpaquePtr());
Jonas Devlieghere6c9dc122019-08-23 04:11:38 +00001214 if (streq(type_name, "char32_t"))
Alex Langfordbddab072019-08-13 19:40:36 +00001215 return CompilerType(this, ast->Char32Ty.getAsOpaquePtr());
Jonas Devlieghere6c9dc122019-08-23 04:11:38 +00001216 if (streq(type_name, "char8_t"))
Jonas Devliegherec46d39b2019-08-21 21:30:55 +00001217 return CompilerType(this, ast->Char8Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001218 }
1219 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001220 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001221 }
1222 // This assert should fire for anything that we don't catch above so we know
1223 // to fix any issues we run into.
1224 if (type_name) {
1225 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1226 "DW_TAG_base_type '%s' encoded with "
1227 "DW_ATE = 0x%x, bit_size = %u\n",
1228 type_name, dw_ate, bit_size);
1229 } else {
1230 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1231 "DW_TAG_base_type encoded with "
1232 "DW_ATE = 0x%x, bit_size = %u\n",
1233 dw_ate, bit_size);
1234 }
1235 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001236}
1237
Kate Stoneb9c1b512016-09-06 20:57:50 +00001238CompilerType ClangASTContext::GetCStringType(bool is_const) {
1239 ASTContext *ast = getASTContext();
1240 QualType char_type(ast->CharTy);
1241
1242 if (is_const)
1243 char_type.addConst();
1244
Alex Langfordbddab072019-08-13 19:40:36 +00001245 return CompilerType(this, ast->getPointerType(char_type).getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001246}
1247
Zachary Turner115209e2018-11-05 19:25:39 +00001248clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001249ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1250 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001251}
1252
Kate Stoneb9c1b512016-09-06 20:57:50 +00001253clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1254 clang::Decl *source_decl) {
1255 FileSystemOptions file_system_options;
1256 FileManager file_manager(file_system_options);
1257 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1258
Gabor Marton5ac6d492019-05-15 10:29:48 +00001259 if (llvm::Expected<clang::Decl *> ret_or_error =
1260 importer.Import(source_decl)) {
1261 return *ret_or_error;
1262 } else {
1263 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
1264 LLDB_LOG_ERROR(log, ret_or_error.takeError(), "Couldn't import decl: {0}");
1265 return nullptr;
1266 }
Greg Clayton526e5af2010-11-13 03:52:47 +00001267}
1268
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1270 bool ignore_qualifiers) {
1271 ClangASTContext *ast =
1272 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1273 if (!ast || ast != type2.GetTypeSystem())
1274 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001275
Kate Stoneb9c1b512016-09-06 20:57:50 +00001276 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1277 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001278
Kate Stoneb9c1b512016-09-06 20:57:50 +00001279 QualType type1_qual = ClangUtil::GetQualType(type1);
1280 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001281
Kate Stoneb9c1b512016-09-06 20:57:50 +00001282 if (ignore_qualifiers) {
1283 type1_qual = type1_qual.getUnqualifiedType();
1284 type2_qual = type2_qual.getUnqualifiedType();
1285 }
1286
1287 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001288}
1289
Alex Langfordcb68bd72019-08-23 06:11:32 +00001290CompilerType ClangASTContext::GetTypeForDecl(void *opaque_decl) {
1291 if (!opaque_decl)
1292 return CompilerType();
1293
1294 clang::Decl *decl = static_cast<clang::Decl *>(opaque_decl);
1295 if (auto *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl))
1296 return GetTypeForDecl(named_decl);
1297 return CompilerType();
1298}
1299
Kate Stoneb9c1b512016-09-06 20:57:50 +00001300CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1301 if (clang::ObjCInterfaceDecl *interface_decl =
1302 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1303 return GetTypeForDecl(interface_decl);
1304 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1305 return GetTypeForDecl(tag_decl);
1306 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001307}
1308
Kate Stoneb9c1b512016-09-06 20:57:50 +00001309CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001310 // No need to call the getASTContext() accessor (which can create the AST if
1311 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001312 // AST if our AST didn't already exist...
1313 ASTContext *ast = &decl->getASTContext();
1314 if (ast)
Alex Langfordbddab072019-08-13 19:40:36 +00001315 return CompilerType(ClangASTContext::GetASTContext(ast),
1316 ast->getTagDeclType(decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001317 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001318}
1319
Kate Stoneb9c1b512016-09-06 20:57:50 +00001320CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001321 // No need to call the getASTContext() accessor (which can create the AST if
1322 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001323 // AST if our AST didn't already exist...
1324 ASTContext *ast = &decl->getASTContext();
1325 if (ast)
Alex Langfordbddab072019-08-13 19:40:36 +00001326 return CompilerType(ClangASTContext::GetASTContext(ast),
1327 ast->getObjCInterfaceType(decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001328 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001329}
1330
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001331#pragma mark Structure, Unions, Classes
1332
shafikde2c7ca2019-10-28 14:26:54 -07001333CompilerType ClangASTContext::CreateRecordType(
1334 DeclContext *decl_ctx, AccessType access_type, const char *name, int kind,
1335 LanguageType language, ClangASTMetadata *metadata, bool exports_symbols) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001336 ASTContext *ast = getASTContext();
1337 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001338
Kate Stoneb9c1b512016-09-06 20:57:50 +00001339 if (decl_ctx == nullptr)
1340 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001341
Kate Stoneb9c1b512016-09-06 20:57:50 +00001342 if (language == eLanguageTypeObjC ||
1343 language == eLanguageTypeObjC_plus_plus) {
1344 bool isForwardDecl = true;
1345 bool isInternal = false;
1346 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1347 }
Greg Clayton9e409562010-07-28 02:04:09 +00001348
Kate Stoneb9c1b512016-09-06 20:57:50 +00001349 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
Adrian Prantl05097242018-04-30 16:49:04 +00001350 // we will need to update this code. I was told to currently always use the
1351 // CXXRecordDecl class since we often don't know from debug information if
1352 // something is struct or a class, so we default to always use the more
Kate Stoneb9c1b512016-09-06 20:57:50 +00001353 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001354
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001355 bool has_name = name && name[0];
Greg Claytonc4ffd662013-03-08 01:37:30 +00001356
Kate Stoneb9c1b512016-09-06 20:57:50 +00001357 CXXRecordDecl *decl = CXXRecordDecl::Create(
1358 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001359 SourceLocation(), has_name ? &ast->Idents.get(name) : nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001360
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001361 if (!has_name) {
1362 // In C++ a lambda is also represented as an unnamed class. This is
1363 // different from an *anonymous class* that the user wrote:
1364 //
1365 // struct A {
1366 // // anonymous class (GNU/MSVC extension)
1367 // struct {
1368 // int x;
1369 // };
1370 // // unnamed class within a class
1371 // struct {
1372 // int y;
1373 // } B;
1374 // };
1375 //
1376 // void f() {
1377 // // unammed class outside of a class
1378 // struct {
1379 // int z;
1380 // } C;
1381 // }
1382 //
1383 // Anonymous classes is a GNU/MSVC extension that clang supports. It
1384 // requires the anonymous class be embedded within a class. So the new
1385 // heuristic verifies this condition.
shafikde2c7ca2019-10-28 14:26:54 -07001386 if (isa<CXXRecordDecl>(decl_ctx) && exports_symbols)
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001387 decl->setAnonymousStructOrUnion(true);
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001388 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001389
1390 if (decl) {
1391 if (metadata)
1392 SetMetadata(ast, decl, *metadata);
1393
1394 if (access_type != eAccessNone)
1395 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1396
1397 if (decl_ctx)
1398 decl_ctx->addDecl(decl);
1399
Alex Langfordbddab072019-08-13 19:40:36 +00001400 return CompilerType(this, ast->getTagDeclType(decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001401 }
1402 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001403}
1404
Sean Callanan09e91ac2017-05-11 22:08:05 +00001405namespace {
1406 bool IsValueParam(const clang::TemplateArgument &argument) {
1407 return argument.getKind() == TemplateArgument::Integral;
1408 }
1409}
1410
Kate Stoneb9c1b512016-09-06 20:57:50 +00001411static TemplateParameterList *CreateTemplateParameterList(
1412 ASTContext *ast,
1413 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1414 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1415 const bool parameter_pack = false;
1416 const bool is_typename = false;
1417 const unsigned depth = 0;
Sean Callanan09e91ac2017-05-11 22:08:05 +00001418 const size_t num_template_params = template_param_infos.args.size();
1419 DeclContext *const decl_context =
1420 ast->getTranslationUnitDecl(); // Is this the right decl context?,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001421 for (size_t i = 0; i < num_template_params; ++i) {
1422 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001423
Kate Stoneb9c1b512016-09-06 20:57:50 +00001424 IdentifierInfo *identifier_info = nullptr;
1425 if (name && name[0])
1426 identifier_info = &ast->Idents.get(name);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001427 if (IsValueParam(template_param_infos.args[i])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001428 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001429 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001430 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1431 template_param_infos.args[i].getIntegralType(), parameter_pack,
1432 nullptr));
1433
1434 } else {
1435 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001436 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001437 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1438 is_typename, parameter_pack));
1439 }
1440 }
Eugene Zemtsova9d928c2017-09-29 03:15:08 +00001441
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001442 if (template_param_infos.packed_args) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001443 IdentifierInfo *identifier_info = nullptr;
1444 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1445 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1446 const bool parameter_pack_true = true;
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001447
1448 if (!template_param_infos.packed_args->args.empty() &&
1449 IsValueParam(template_param_infos.packed_args->args[0])) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001450 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001451 *ast, decl_context, SourceLocation(), SourceLocation(), depth,
1452 num_template_params, identifier_info,
Sean Callanan09e91ac2017-05-11 22:08:05 +00001453 template_param_infos.packed_args->args[0].getIntegralType(),
1454 parameter_pack_true, nullptr));
1455 } else {
1456 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001457 *ast, decl_context, SourceLocation(), SourceLocation(), depth,
1458 num_template_params, identifier_info, is_typename,
1459 parameter_pack_true));
Sean Callanan09e91ac2017-05-11 22:08:05 +00001460 }
1461 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001462 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1463 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1464 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1465 SourceLocation(), requires_clause);
1466 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001467}
1468
Kate Stoneb9c1b512016-09-06 20:57:50 +00001469clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1470 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1471 const char *name, const TemplateParameterInfos &template_param_infos) {
Adrian Prantld8f460e2018-05-02 16:55:16 +00001472 // /// Create a function template node.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001473 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001474
Kate Stoneb9c1b512016-09-06 20:57:50 +00001475 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001476
Kate Stoneb9c1b512016-09-06 20:57:50 +00001477 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1478 ast, template_param_infos, template_param_decls);
1479 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1480 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1481 template_param_list, func_decl);
1482
1483 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1484 i < template_param_decl_count; ++i) {
1485 // TODO: verify which decl context we should put template_param_decls into..
1486 template_param_decls[i]->setDeclContext(func_decl);
1487 }
1488
1489 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001490}
1491
Kate Stoneb9c1b512016-09-06 20:57:50 +00001492void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1493 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1494 const TemplateParameterInfos &infos) {
Shafik Yaghmoura0858e22019-07-17 20:16:13 +00001495 TemplateArgumentList *template_args_ptr =
1496 TemplateArgumentList::CreateCopy(func_decl->getASTContext(), infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001497
Shafik Yaghmoura0858e22019-07-17 20:16:13 +00001498 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl,
1499 template_args_ptr, nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001500}
1501
Kate Stoneb9c1b512016-09-06 20:57:50 +00001502ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1503 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1504 int kind, const TemplateParameterInfos &template_param_infos) {
1505 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001506
Kate Stoneb9c1b512016-09-06 20:57:50 +00001507 ClassTemplateDecl *class_template_decl = nullptr;
1508 if (decl_ctx == nullptr)
1509 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001510
Kate Stoneb9c1b512016-09-06 20:57:50 +00001511 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1512 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001513
Kate Stoneb9c1b512016-09-06 20:57:50 +00001514 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001515
Kate Stoneb9c1b512016-09-06 20:57:50 +00001516 for (NamedDecl *decl : result) {
1517 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001518 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001519 return class_template_decl;
1520 }
1521
1522 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1523
1524 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1525 ast, template_param_infos, template_param_decls);
1526
1527 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1528 *ast, (TagDecl::TagKind)kind,
1529 decl_ctx, // What decl context do we use here? TU? The actual decl
1530 // context?
1531 SourceLocation(), SourceLocation(), &identifier_info);
1532
1533 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1534 i < template_param_decl_count; ++i) {
1535 template_param_decls[i]->setDeclContext(template_cxx_decl);
1536 }
1537
1538 // With templated classes, we say that a class is templated with
1539 // specializations, but that the bare class has no functions.
1540 // template_cxx_decl->startDefinition();
1541 // template_cxx_decl->completeDefinition();
1542
1543 class_template_decl = ClassTemplateDecl::Create(
1544 *ast,
1545 decl_ctx, // What decl context do we use here? TU? The actual decl
1546 // context?
Pavel Labath4294de32017-01-12 10:44:16 +00001547 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
Richard Smith35b007e2019-02-15 21:48:09 +00001548 template_cxx_decl->setDescribedClassTemplate(class_template_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001549
1550 if (class_template_decl) {
1551 if (access_type != eAccessNone)
1552 class_template_decl->setAccess(
1553 ConvertAccessTypeToAccessSpecifier(access_type));
1554
1555 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1556 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1557
1558 decl_ctx->addDecl(class_template_decl);
1559
Sean Callanan5e9e1992011-10-26 01:06:27 +00001560#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001561 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001562#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001563 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001564
Kate Stoneb9c1b512016-09-06 20:57:50 +00001565 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001566}
1567
Frederic Rissf4e7e522018-04-02 16:18:32 +00001568TemplateTemplateParmDecl *
1569ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) {
1570 ASTContext *ast = getASTContext();
1571
1572 auto *decl_ctx = ast->getTranslationUnitDecl();
1573
1574 IdentifierInfo &identifier_info = ast->Idents.get(template_name);
1575 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1576
1577 ClangASTContext::TemplateParameterInfos template_param_infos;
1578 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1579 ast, template_param_infos, template_param_decls);
1580
1581 // LLDB needs to create those decls only to be able to display a
Adrian Prantl05097242018-04-30 16:49:04 +00001582 // type that includes a template template argument. Only the name matters for
1583 // this purpose, so we use dummy values for the other characterisitcs of the
1584 // type.
Frederic Rissf4e7e522018-04-02 16:18:32 +00001585 return TemplateTemplateParmDecl::Create(
1586 *ast, decl_ctx, SourceLocation(),
1587 /*Depth*/ 0, /*Position*/ 0,
1588 /*IsParameterPack*/ false, &identifier_info, template_param_list);
1589}
1590
Greg Claytonf0705c82011-10-22 03:33:13 +00001591ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001592ClangASTContext::CreateClassTemplateSpecializationDecl(
1593 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1594 const TemplateParameterInfos &template_param_infos) {
1595 ASTContext *ast = getASTContext();
Sean Callanan09e91ac2017-05-11 22:08:05 +00001596 llvm::SmallVector<clang::TemplateArgument, 2> args(
1597 template_param_infos.args.size() +
1598 (template_param_infos.packed_args ? 1 : 0));
1599 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1600 args.begin());
1601 if (template_param_infos.packed_args) {
1602 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1603 *ast, template_param_infos.packed_args->args);
1604 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001605 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1606 ClassTemplateSpecializationDecl::Create(
1607 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Sean Callanan09e91ac2017-05-11 22:08:05 +00001608 SourceLocation(), class_template_decl, args,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001609 nullptr);
1610
1611 class_template_specialization_decl->setSpecializationKind(
1612 TSK_ExplicitSpecialization);
1613
1614 return class_template_specialization_decl;
1615}
1616
1617CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1618 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1619 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001620 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001621 if (ast)
1622 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00001623 this, ast->getTagDeclType(class_template_specialization_decl)
1624 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001625 }
1626 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001627}
1628
Kate Stoneb9c1b512016-09-06 20:57:50 +00001629static inline bool check_op_param(bool is_method,
1630 clang::OverloadedOperatorKind op_kind,
1631 bool unary, bool binary,
1632 uint32_t num_params) {
1633 // Special-case call since it can take any number of operands
1634 if (op_kind == OO_Call)
1635 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001636
Kate Stoneb9c1b512016-09-06 20:57:50 +00001637 // The parameter count doesn't include "this"
1638 if (is_method)
1639 ++num_params;
1640 if (num_params == 1)
1641 return unary;
1642 if (num_params == 2)
1643 return binary;
1644 else
Greg Clayton090d0982011-06-19 03:43:27 +00001645 return false;
1646}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001647
Kate Stoneb9c1b512016-09-06 20:57:50 +00001648bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1649 bool is_method, clang::OverloadedOperatorKind op_kind,
1650 uint32_t num_params) {
1651 switch (op_kind) {
1652 default:
1653 break;
1654 // C++ standard allows any number of arguments to new/delete
1655 case OO_New:
1656 case OO_Array_New:
1657 case OO_Delete:
1658 case OO_Array_Delete:
1659 return true;
1660 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001661
Kate Stoneb9c1b512016-09-06 20:57:50 +00001662#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1663 case OO_##Name: \
1664 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1665 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001666#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001667 default:
1668 break;
1669 }
1670 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001671}
1672
Greg Clayton57ee3062013-07-11 22:46:58 +00001673clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001674ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1675 clang::AccessSpecifier rhs) {
1676 // Make the access equal to the stricter of the field and the nested field's
1677 // access
1678 if (lhs == AS_none || rhs == AS_none)
1679 return AS_none;
1680 if (lhs == AS_private || rhs == AS_private)
1681 return AS_private;
1682 if (lhs == AS_protected || rhs == AS_protected)
1683 return AS_protected;
1684 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001685}
1686
Kate Stoneb9c1b512016-09-06 20:57:50 +00001687bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1688 uint32_t &bitfield_bit_size) {
Raphael Isemann02e91132019-11-20 12:09:19 +01001689 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001690 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001691 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001692
Kate Stoneb9c1b512016-09-06 20:57:50 +00001693 if (field->isBitField()) {
1694 Expr *bit_width_expr = field->getBitWidth();
1695 if (bit_width_expr) {
1696 llvm::APSInt bit_width_apsint;
1697 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1698 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001699 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001700 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001701 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001702 }
1703 return false;
1704}
1705
1706bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1707 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001708 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001709
1710 if (!record_decl->field_empty())
1711 return true;
1712
1713 // No fields, lets check this is a CXX record and check the base classes
1714 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1715 if (cxx_record_decl) {
1716 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1717 for (base_class = cxx_record_decl->bases_begin(),
1718 base_class_end = cxx_record_decl->bases_end();
1719 base_class != base_class_end; ++base_class) {
1720 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1721 base_class->getType()->getAs<RecordType>()->getDecl());
1722 if (RecordHasFields(base_class_decl))
1723 return true;
1724 }
1725 }
1726 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001727}
1728
Adrian Prantl4e8be2c2018-06-13 16:21:24 +00001729#pragma mark Objective-C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001730
Kate Stoneb9c1b512016-09-06 20:57:50 +00001731CompilerType ClangASTContext::CreateObjCClass(const char *name,
1732 DeclContext *decl_ctx,
1733 bool isForwardDecl,
1734 bool isInternal,
1735 ClangASTMetadata *metadata) {
1736 ASTContext *ast = getASTContext();
1737 assert(ast != nullptr);
1738 assert(name && name[0]);
1739 if (decl_ctx == nullptr)
1740 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001741
Kate Stoneb9c1b512016-09-06 20:57:50 +00001742 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1743 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1744 nullptr, SourceLocation(),
1745 /*isForwardDecl,*/
1746 isInternal);
1747
1748 if (decl && metadata)
1749 SetMetadata(ast, decl, *metadata);
1750
Alex Langfordbddab072019-08-13 19:40:36 +00001751 return CompilerType(this, ast->getObjCInterfaceType(decl).getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001752}
1753
Kate Stoneb9c1b512016-09-06 20:57:50 +00001754static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001755 return !ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001756}
1757
Greg Clayton57ee3062013-07-11 22:46:58 +00001758uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001759ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1760 bool omit_empty_base_classes) {
1761 uint32_t num_bases = 0;
1762 if (cxx_record_decl) {
1763 if (omit_empty_base_classes) {
1764 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1765 for (base_class = cxx_record_decl->bases_begin(),
1766 base_class_end = cxx_record_decl->bases_end();
1767 base_class != base_class_end; ++base_class) {
1768 // Skip empty base classes
1769 if (omit_empty_base_classes) {
1770 if (BaseSpecifierIsEmpty(base_class))
1771 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001772 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001773 ++num_bases;
1774 }
1775 } else
1776 num_bases = cxx_record_decl->getNumBases();
1777 }
1778 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001779}
1780
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001781#pragma mark Namespace Declarations
1782
Raphael Isemanna9469972019-03-12 07:45:04 +00001783NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1784 const char *name, DeclContext *decl_ctx, bool is_inline) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001785 NamespaceDecl *namespace_decl = nullptr;
1786 ASTContext *ast = getASTContext();
1787 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1788 if (decl_ctx == nullptr)
1789 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001790
Kate Stoneb9c1b512016-09-06 20:57:50 +00001791 if (name) {
1792 IdentifierInfo &identifier_info = ast->Idents.get(name);
1793 DeclarationName decl_name(&identifier_info);
1794 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1795 for (NamedDecl *decl : result) {
1796 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1797 if (namespace_decl)
1798 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001799 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001800
1801 namespace_decl =
Raphael Isemanna9469972019-03-12 07:45:04 +00001802 NamespaceDecl::Create(*ast, decl_ctx, is_inline, SourceLocation(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001803 SourceLocation(), &identifier_info, nullptr);
1804
1805 decl_ctx->addDecl(namespace_decl);
1806 } else {
1807 if (decl_ctx == translation_unit_decl) {
1808 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1809 if (namespace_decl)
1810 return namespace_decl;
1811
1812 namespace_decl =
1813 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1814 SourceLocation(), nullptr, nullptr);
1815 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1816 translation_unit_decl->addDecl(namespace_decl);
1817 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1818 } else {
1819 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1820 if (parent_namespace_decl) {
1821 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1822 if (namespace_decl)
1823 return namespace_decl;
1824 namespace_decl =
1825 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1826 SourceLocation(), nullptr, nullptr);
1827 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1828 parent_namespace_decl->addDecl(namespace_decl);
1829 assert(namespace_decl ==
1830 parent_namespace_decl->getAnonymousNamespace());
1831 } else {
Raphael Isemannc4944812019-07-04 19:49:31 +00001832 assert(false && "GetUniqueNamespaceDeclaration called with no name and "
1833 "no namespace as decl_ctx");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001834 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001835 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001836 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001837#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001838 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001839#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001840 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001841}
1842
Paul Hermand628cbb2015-09-15 23:44:17 +00001843clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001844ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1845 if (ctx != nullptr) {
1846 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1847 clang::SourceLocation());
1848 ctx->addDecl(decl);
1849 return decl;
1850 }
1851 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001852}
1853
Kate Stoneb9c1b512016-09-06 20:57:50 +00001854clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1855 clang::DeclContext *right,
1856 clang::DeclContext *root) {
1857 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001858 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001859
1860 std::set<clang::DeclContext *> path_left;
1861 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1862 path_left.insert(d);
1863
1864 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1865 if (path_left.find(d) != path_left.end())
1866 return d;
1867
1868 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00001869}
1870
Kate Stoneb9c1b512016-09-06 20:57:50 +00001871clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1872 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1873 if (decl_ctx != nullptr && ns_decl != nullptr) {
1874 clang::TranslationUnitDecl *translation_unit =
1875 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1876 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1877 *getASTContext(), decl_ctx, clang::SourceLocation(),
1878 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1879 clang::SourceLocation(), ns_decl,
1880 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
1881 decl_ctx->addDecl(using_decl);
1882 return using_decl;
1883 }
1884 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001885}
1886
1887clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001888ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1889 clang::NamedDecl *target) {
1890 if (current_decl_ctx != nullptr && target != nullptr) {
1891 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1892 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
1893 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1894 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1895 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
1896 target);
1897 using_decl->addShadowDecl(shadow_decl);
1898 current_decl_ctx->addDecl(using_decl);
1899 return using_decl;
1900 }
1901 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001902}
1903
Kate Stoneb9c1b512016-09-06 20:57:50 +00001904clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
1905 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
1906 if (decl_context != nullptr) {
1907 clang::VarDecl *var_decl = clang::VarDecl::Create(
1908 *getASTContext(), decl_context, clang::SourceLocation(),
1909 clang::SourceLocation(),
1910 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
1911 nullptr, clang::SC_None);
1912 var_decl->setAccess(clang::AS_public);
1913 decl_context->addDecl(var_decl);
1914 return var_decl;
1915 }
1916 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001917}
1918
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001919lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001920ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
1921 lldb::BasicType basic_type) {
1922 switch (basic_type) {
1923 case eBasicTypeVoid:
1924 return ast->VoidTy.getAsOpaquePtr();
1925 case eBasicTypeChar:
1926 return ast->CharTy.getAsOpaquePtr();
1927 case eBasicTypeSignedChar:
1928 return ast->SignedCharTy.getAsOpaquePtr();
1929 case eBasicTypeUnsignedChar:
1930 return ast->UnsignedCharTy.getAsOpaquePtr();
1931 case eBasicTypeWChar:
1932 return ast->getWCharType().getAsOpaquePtr();
1933 case eBasicTypeSignedWChar:
1934 return ast->getSignedWCharType().getAsOpaquePtr();
1935 case eBasicTypeUnsignedWChar:
1936 return ast->getUnsignedWCharType().getAsOpaquePtr();
1937 case eBasicTypeChar16:
1938 return ast->Char16Ty.getAsOpaquePtr();
1939 case eBasicTypeChar32:
1940 return ast->Char32Ty.getAsOpaquePtr();
1941 case eBasicTypeShort:
1942 return ast->ShortTy.getAsOpaquePtr();
1943 case eBasicTypeUnsignedShort:
1944 return ast->UnsignedShortTy.getAsOpaquePtr();
1945 case eBasicTypeInt:
1946 return ast->IntTy.getAsOpaquePtr();
1947 case eBasicTypeUnsignedInt:
1948 return ast->UnsignedIntTy.getAsOpaquePtr();
1949 case eBasicTypeLong:
1950 return ast->LongTy.getAsOpaquePtr();
1951 case eBasicTypeUnsignedLong:
1952 return ast->UnsignedLongTy.getAsOpaquePtr();
1953 case eBasicTypeLongLong:
1954 return ast->LongLongTy.getAsOpaquePtr();
1955 case eBasicTypeUnsignedLongLong:
1956 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1957 case eBasicTypeInt128:
1958 return ast->Int128Ty.getAsOpaquePtr();
1959 case eBasicTypeUnsignedInt128:
1960 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1961 case eBasicTypeBool:
1962 return ast->BoolTy.getAsOpaquePtr();
1963 case eBasicTypeHalf:
1964 return ast->HalfTy.getAsOpaquePtr();
1965 case eBasicTypeFloat:
1966 return ast->FloatTy.getAsOpaquePtr();
1967 case eBasicTypeDouble:
1968 return ast->DoubleTy.getAsOpaquePtr();
1969 case eBasicTypeLongDouble:
1970 return ast->LongDoubleTy.getAsOpaquePtr();
1971 case eBasicTypeFloatComplex:
1972 return ast->FloatComplexTy.getAsOpaquePtr();
1973 case eBasicTypeDoubleComplex:
1974 return ast->DoubleComplexTy.getAsOpaquePtr();
1975 case eBasicTypeLongDoubleComplex:
1976 return ast->LongDoubleComplexTy.getAsOpaquePtr();
1977 case eBasicTypeObjCID:
1978 return ast->getObjCIdType().getAsOpaquePtr();
1979 case eBasicTypeObjCClass:
1980 return ast->getObjCClassType().getAsOpaquePtr();
1981 case eBasicTypeObjCSel:
1982 return ast->getObjCSelType().getAsOpaquePtr();
1983 case eBasicTypeNullPtr:
1984 return ast->NullPtrTy.getAsOpaquePtr();
1985 default:
1986 return nullptr;
1987 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001988}
1989
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001990#pragma mark Function Types
1991
Pavel Labath1ac2b202016-08-15 14:32:32 +00001992clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00001993ClangASTContext::GetDeclarationName(const char *name,
1994 const CompilerType &function_clang_type) {
1995 if (!name || !name[0])
1996 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00001997
Kate Stoneb9c1b512016-09-06 20:57:50 +00001998 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
1999 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
2000 return DeclarationName(&getASTContext()->Idents.get(
2001 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00002002
Adrian Prantl05097242018-04-30 16:49:04 +00002003 // Check the number of operator parameters. Sometimes we have seen bad DWARF
2004 // that doesn't correctly describe operators and if we try to create a method
2005 // and add it to the class, clang will assert and crash, so we need to make
2006 // sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002007 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
2008 const clang::FunctionProtoType *function_type =
2009 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
2010 if (function_type == nullptr)
2011 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002012
Kate Stoneb9c1b512016-09-06 20:57:50 +00002013 const bool is_method = false;
2014 const unsigned int num_params = function_type->getNumParams();
2015 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
2016 is_method, op_kind, num_params))
2017 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002018
Kate Stoneb9c1b512016-09-06 20:57:50 +00002019 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00002020}
2021
Kate Stoneb9c1b512016-09-06 20:57:50 +00002022FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
2023 DeclContext *decl_ctx, const char *name,
2024 const CompilerType &function_clang_type, int storage, bool is_inline) {
2025 FunctionDecl *func_decl = nullptr;
2026 ASTContext *ast = getASTContext();
2027 if (decl_ctx == nullptr)
2028 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002029
Kate Stoneb9c1b512016-09-06 20:57:50 +00002030 const bool hasWrittenPrototype = true;
2031 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00002032
Kate Stoneb9c1b512016-09-06 20:57:50 +00002033 clang::DeclarationName declarationName =
2034 GetDeclarationName(name, function_clang_type);
2035 func_decl = FunctionDecl::Create(
2036 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2037 ClangUtil::GetQualType(function_clang_type), nullptr,
2038 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002039 isConstexprSpecified ? CSK_constexpr : CSK_unspecified);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002040 if (func_decl)
2041 decl_ctx->addDecl(func_decl);
2042
Sean Callanan5e9e1992011-10-26 01:06:27 +00002043#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00002044 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002045#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002046
2047 return func_decl;
2048}
2049
2050CompilerType ClangASTContext::CreateFunctionType(
2051 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002052 unsigned num_args, bool is_variadic, unsigned type_quals,
2053 clang::CallingConv cc) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002054 if (ast == nullptr)
2055 return CompilerType(); // invalid AST
2056
2057 if (!result_type || !ClangUtil::IsClangType(result_type))
2058 return CompilerType(); // invalid return type
2059
2060 std::vector<QualType> qual_type_args;
2061 if (num_args > 0 && args == nullptr)
2062 return CompilerType(); // invalid argument array passed in
2063
2064 // Verify that all arguments are valid and the right type
2065 for (unsigned i = 0; i < num_args; ++i) {
2066 if (args[i]) {
2067 // Make sure we have a clang type in args[i] and not a type from another
2068 // language whose name might match
2069 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2070 lldbassert(is_clang_type);
2071 if (is_clang_type)
2072 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2073 else
2074 return CompilerType(); // invalid argument type (must be a clang type)
2075 } else
2076 return CompilerType(); // invalid argument type (empty)
2077 }
2078
2079 // TODO: Detect calling convention in DWARF?
2080 FunctionProtoType::ExtProtoInfo proto_info;
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002081 proto_info.ExtInfo = cc;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002082 proto_info.Variadic = is_variadic;
2083 proto_info.ExceptionSpec = EST_None;
Mikael Nilsson8b3bf6c2018-12-13 10:17:26 +00002084 proto_info.TypeQuals = clang::Qualifiers::fromFastMask(type_quals);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002085 proto_info.RefQualifier = RQ_None;
2086
Alex Langfordbddab072019-08-13 19:40:36 +00002087 return CompilerType(ClangASTContext::GetASTContext(ast),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002088 ast->getFunctionType(ClangUtil::GetQualType(result_type),
Alex Langfordbddab072019-08-13 19:40:36 +00002089 qual_type_args, proto_info).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002090}
2091
2092ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
Zachary Turner6753d2d2018-12-12 17:17:53 +00002093 clang::DeclContext *decl_ctx, const char *name,
Shafik Yaghmourfa5c3402019-08-02 21:41:50 +00002094 const CompilerType &param_type, int storage, bool add_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002095 ASTContext *ast = getASTContext();
2096 assert(ast != nullptr);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00002097 auto *decl =
2098 ParmVarDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(),
2099 name && name[0] ? &ast->Idents.get(name) : nullptr,
2100 ClangUtil::GetQualType(param_type), nullptr,
2101 (clang::StorageClass)storage, nullptr);
Shafik Yaghmourfa5c3402019-08-02 21:41:50 +00002102 if (add_decl)
2103 decl_ctx->addDecl(decl);
2104
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00002105 return decl;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002106}
2107
2108void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2109 ParmVarDecl **params,
2110 unsigned num_params) {
2111 if (function_decl)
2112 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002113}
2114
Greg Claytona1e5dc82015-08-11 22:53:00 +00002115CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002116ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00002117 QualType block_type = m_ast_up->getBlockPointerType(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002118 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002119
Kate Stoneb9c1b512016-09-06 20:57:50 +00002120 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002121}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002122
2123#pragma mark Array Types
2124
Kate Stoneb9c1b512016-09-06 20:57:50 +00002125CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2126 size_t element_count,
2127 bool is_vector) {
2128 if (element_type.IsValid()) {
2129 ASTContext *ast = getASTContext();
2130 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002131
Kate Stoneb9c1b512016-09-06 20:57:50 +00002132 if (is_vector) {
2133 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00002134 this, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2135 element_count)
2136 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002137 } else {
2138
2139 llvm::APInt ap_element_count(64, element_count);
2140 if (element_count == 0) {
Alex Langfordbddab072019-08-13 19:40:36 +00002141 return CompilerType(this, ast->getIncompleteArrayType(
2142 ClangUtil::GetQualType(element_type),
2143 clang::ArrayType::Normal, 0)
2144 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002145 } else {
Alex Langfordbddab072019-08-13 19:40:36 +00002146 return CompilerType(this, ast->getConstantArrayType(
2147 ClangUtil::GetQualType(element_type),
Richard Smith772e2662019-10-04 01:25:59 +00002148 ap_element_count, nullptr,
Alex Langfordbddab072019-08-13 19:40:36 +00002149 clang::ArrayType::Normal, 0)
2150 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002151 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002152 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002153 }
2154 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002155}
2156
Kate Stoneb9c1b512016-09-06 20:57:50 +00002157CompilerType ClangASTContext::CreateStructForIdentifier(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002158 ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002159 const std::initializer_list<std::pair<const char *, CompilerType>>
2160 &type_fields,
2161 bool packed) {
2162 CompilerType type;
2163 if (!type_name.IsEmpty() &&
2164 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2165 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002166 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002167 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002168 }
2169
2170 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2171 clang::TTK_Struct, lldb::eLanguageTypeC);
2172 StartTagDeclarationDefinition(type);
2173 for (const auto &field : type_fields)
2174 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2175 0);
2176 if (packed)
2177 SetIsPacked(type);
2178 CompleteTagDeclarationDefinition(type);
2179 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002180}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002181
Kate Stoneb9c1b512016-09-06 20:57:50 +00002182CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002183 ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002184 const std::initializer_list<std::pair<const char *, CompilerType>>
2185 &type_fields,
2186 bool packed) {
2187 CompilerType type;
2188 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2189 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002190
Kate Stoneb9c1b512016-09-06 20:57:50 +00002191 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002192}
2193
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002194#pragma mark Enumeration Types
2195
Greg Claytona1e5dc82015-08-11 22:53:00 +00002196CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002197ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2198 const Declaration &decl,
Tamas Berghammer59765832017-11-07 10:39:22 +00002199 const CompilerType &integer_clang_type,
2200 bool is_scoped) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002201 // TODO: Do something intelligent with the Declaration object passed in
2202 // like maybe filling in the SourceLocation with it...
2203 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002204
Kate Stoneb9c1b512016-09-06 20:57:50 +00002205 // TODO: ask about these...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002206 // const bool IsFixed = false;
2207
2208 EnumDecl *enum_decl = EnumDecl::Create(
2209 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2210 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
Tamas Berghammercf6bf4c2017-11-07 13:43:55 +00002211 is_scoped, // IsScoped
2212 is_scoped, // IsScopedUsingClassTag
2213 false); // IsFixed
Kate Stoneb9c1b512016-09-06 20:57:50 +00002214
2215 if (enum_decl) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00002216 if (decl_ctx)
2217 decl_ctx->addDecl(enum_decl);
2218
Kate Stoneb9c1b512016-09-06 20:57:50 +00002219 // TODO: check if we should be setting the promotion type too?
2220 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2221
2222 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2223
Alex Langfordbddab072019-08-13 19:40:36 +00002224 return CompilerType(this, ast->getTagDeclType(enum_decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002225 }
2226 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002227}
2228
Kate Stoneb9c1b512016-09-06 20:57:50 +00002229CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2230 size_t bit_size,
2231 bool is_signed) {
2232 if (ast) {
Alex Langfordbddab072019-08-13 19:40:36 +00002233 auto *clang_ast_context = ClangASTContext::GetASTContext(ast);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002234 if (is_signed) {
2235 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002236 return CompilerType(clang_ast_context,
2237 ast->SignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002238
2239 if (bit_size == ast->getTypeSize(ast->ShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002240 return CompilerType(clang_ast_context, ast->ShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002241
2242 if (bit_size == ast->getTypeSize(ast->IntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002243 return CompilerType(clang_ast_context, ast->IntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002244
2245 if (bit_size == ast->getTypeSize(ast->LongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002246 return CompilerType(clang_ast_context, ast->LongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002247
2248 if (bit_size == ast->getTypeSize(ast->LongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002249 return CompilerType(clang_ast_context,
2250 ast->LongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002251
2252 if (bit_size == ast->getTypeSize(ast->Int128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00002253 return CompilerType(clang_ast_context, ast->Int128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002254 } else {
2255 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002256 return CompilerType(clang_ast_context,
2257 ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002258
2259 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002260 return CompilerType(clang_ast_context,
2261 ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002262
2263 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002264 return CompilerType(clang_ast_context,
2265 ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002266
2267 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002268 return CompilerType(clang_ast_context,
2269 ast->UnsignedLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002270
2271 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002272 return CompilerType(clang_ast_context,
2273 ast->UnsignedLongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002274
2275 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00002276 return CompilerType(clang_ast_context,
2277 ast->UnsignedInt128Ty.getAsOpaquePtr());
Enrico Granatae8bf7492014-08-15 23:00:02 +00002278 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002279 }
2280 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002281}
2282
Kate Stoneb9c1b512016-09-06 20:57:50 +00002283CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2284 bool is_signed) {
2285 if (ast)
2286 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2287 is_signed);
2288 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002289}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002290
Kate Stoneb9c1b512016-09-06 20:57:50 +00002291void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2292 if (decl_ctx) {
2293 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002294
Kate Stoneb9c1b512016-09-06 20:57:50 +00002295 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2296 if (named_decl) {
2297 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2298 named_decl->getDeclName().getAsString().c_str());
2299 } else {
2300 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002301 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002302 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002303}
2304
Kate Stoneb9c1b512016-09-06 20:57:50 +00002305void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2306 if (decl == nullptr)
2307 return;
2308 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002309
Kate Stoneb9c1b512016-09-06 20:57:50 +00002310 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2311 if (record_decl) {
2312 printf("%20s: %s%s\n", decl->getDeclKindName(),
2313 record_decl->getDeclName().getAsString().c_str(),
2314 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002315
Kate Stoneb9c1b512016-09-06 20:57:50 +00002316 } else {
2317 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2318 if (named_decl) {
2319 printf("%20s: %s\n", decl->getDeclKindName(),
2320 named_decl->getDeclName().getAsString().c_str());
2321 } else {
2322 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002323 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002324 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002325}
2326
Kate Stoneb9c1b512016-09-06 20:57:50 +00002327bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2328 clang::Decl *rhs_decl) {
2329 if (lhs_decl && rhs_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002330 // Make sure the decl kinds match first
Kate Stoneb9c1b512016-09-06 20:57:50 +00002331 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2332 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002333
Kate Stoneb9c1b512016-09-06 20:57:50 +00002334 if (lhs_decl_kind == rhs_decl_kind) {
Adrian Prantl05097242018-04-30 16:49:04 +00002335 // Now check that the decl contexts kinds are all equivalent before we
2336 // have to check any names of the decl contexts...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002337 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2338 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2339 if (lhs_decl_ctx && rhs_decl_ctx) {
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002340 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002341 if (lhs_decl_ctx && rhs_decl_ctx) {
2342 const clang::Decl::Kind lhs_decl_ctx_kind =
2343 lhs_decl_ctx->getDeclKind();
2344 const clang::Decl::Kind rhs_decl_ctx_kind =
2345 rhs_decl_ctx->getDeclKind();
2346 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2347 lhs_decl_ctx = lhs_decl_ctx->getParent();
2348 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002349
Kate Stoneb9c1b512016-09-06 20:57:50 +00002350 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2351 break;
2352 } else
2353 return false;
2354 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002355 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002356 }
2357
Kate Stoneb9c1b512016-09-06 20:57:50 +00002358 // Now make sure the name of the decls match
Kate Stoneb9c1b512016-09-06 20:57:50 +00002359 clang::NamedDecl *lhs_named_decl =
2360 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2361 clang::NamedDecl *rhs_named_decl =
2362 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2363 if (lhs_named_decl && rhs_named_decl) {
2364 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2365 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2366 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2367 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2368 return false;
2369 } else
Greg Claytona2721472011-06-25 00:44:06 +00002370 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002371 } else
2372 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002373
Adrian Prantl05097242018-04-30 16:49:04 +00002374 // We know that the decl context kinds all match, so now we need to
2375 // make sure the names match as well
Kate Stoneb9c1b512016-09-06 20:57:50 +00002376 lhs_decl_ctx = lhs_decl->getDeclContext();
2377 rhs_decl_ctx = rhs_decl->getDeclContext();
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002378 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002379 switch (lhs_decl_ctx->getDeclKind()) {
2380 case clang::Decl::TranslationUnit:
2381 // We don't care about the translation unit names
2382 return true;
2383 default: {
2384 clang::NamedDecl *lhs_named_decl =
2385 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2386 clang::NamedDecl *rhs_named_decl =
2387 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2388 if (lhs_named_decl && rhs_named_decl) {
2389 clang::DeclarationName lhs_decl_name =
2390 lhs_named_decl->getDeclName();
2391 clang::DeclarationName rhs_decl_name =
2392 rhs_named_decl->getDeclName();
2393 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2394 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2395 return false;
2396 } else
2397 return false;
2398 } else
2399 return false;
2400 } break;
2401 }
2402 lhs_decl_ctx = lhs_decl_ctx->getParent();
2403 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002404 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002405 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002406 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002407 }
2408 return false;
2409}
2410bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2411 clang::Decl *decl) {
2412 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002413 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002414
Kate Stoneb9c1b512016-09-06 20:57:50 +00002415 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002416
Kate Stoneb9c1b512016-09-06 20:57:50 +00002417 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002418 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002419
2420 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2421 if (tag_decl->isCompleteDefinition())
2422 return true;
2423
2424 if (!tag_decl->hasExternalLexicalStorage())
2425 return false;
2426
2427 ast_source->CompleteType(tag_decl);
2428
2429 return !tag_decl->getTypeForDecl()->isIncompleteType();
2430 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2431 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2432 if (objc_interface_decl->getDefinition())
2433 return true;
2434
2435 if (!objc_interface_decl->hasExternalLexicalStorage())
2436 return false;
2437
2438 ast_source->CompleteType(objc_interface_decl);
2439
2440 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2441 } else {
2442 return false;
2443 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002444}
2445
Kate Stoneb9c1b512016-09-06 20:57:50 +00002446void ClangASTContext::SetMetadataAsUserID(const void *object,
2447 user_id_t user_id) {
2448 ClangASTMetadata meta_data;
2449 meta_data.SetUserID(user_id);
2450 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002451}
2452
Kate Stoneb9c1b512016-09-06 20:57:50 +00002453void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2454 ClangASTMetadata &metadata) {
2455 ClangExternalASTSourceCommon *external_source =
2456 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2457
2458 if (external_source)
2459 external_source->SetMetadata(object, metadata);
2460}
2461
2462ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2463 const void *object) {
2464 ClangExternalASTSourceCommon *external_source =
2465 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2466
2467 if (external_source && external_source->HasMetadata(object))
2468 return external_source->GetMetadata(object);
2469 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002470 return nullptr;
2471}
2472
Kate Stoneb9c1b512016-09-06 20:57:50 +00002473bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2474 int kind) const {
2475 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2476 if (clang_type) {
2477 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2478 if (tag_type) {
2479 clang::TagDecl *tag_decl =
2480 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2481 if (tag_decl) {
2482 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2483 return true;
2484 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002485 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002486 }
2487 return false;
2488}
2489
2490bool ClangASTContext::SetDefaultAccessForRecordFields(
2491 clang::RecordDecl *record_decl, int default_accessibility,
2492 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2493 if (record_decl) {
2494 uint32_t field_idx;
2495 clang::RecordDecl::field_iterator field, field_end;
2496 for (field = record_decl->field_begin(),
2497 field_end = record_decl->field_end(), field_idx = 0;
2498 field != field_end; ++field, ++field_idx) {
2499 // If no accessibility was assigned, assign the correct one
2500 if (field_idx < num_assigned_accessibilities &&
2501 assigned_accessibilities[field_idx] == clang::AS_none)
2502 field->setAccess((clang::AccessSpecifier)default_accessibility);
2503 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002504 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002505 }
2506 return false;
2507}
2508
2509clang::DeclContext *
2510ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2511 return GetDeclContextForType(ClangUtil::GetQualType(type));
2512}
2513
2514clang::DeclContext *
2515ClangASTContext::GetDeclContextForType(clang::QualType type) {
2516 if (type.isNull())
2517 return nullptr;
2518
2519 clang::QualType qual_type = type.getCanonicalType();
2520 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2521 switch (type_class) {
2522 case clang::Type::ObjCInterface:
2523 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2524 ->getInterface();
2525 case clang::Type::ObjCObjectPointer:
2526 return GetDeclContextForType(
2527 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2528 ->getPointeeType());
2529 case clang::Type::Record:
2530 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2531 case clang::Type::Enum:
2532 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2533 case clang::Type::Typedef:
2534 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2535 ->getDecl()
2536 ->getUnderlyingType());
2537 case clang::Type::Auto:
2538 return GetDeclContextForType(
2539 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2540 case clang::Type::Elaborated:
2541 return GetDeclContextForType(
2542 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2543 case clang::Type::Paren:
2544 return GetDeclContextForType(
2545 llvm::cast<clang::ParenType>(qual_type)->desugar());
2546 default:
2547 break;
2548 }
2549 // No DeclContext in this type...
2550 return nullptr;
2551}
2552
2553static bool GetCompleteQualType(clang::ASTContext *ast,
2554 clang::QualType qual_type,
2555 bool allow_completion = true) {
2556 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2557 switch (type_class) {
2558 case clang::Type::ConstantArray:
2559 case clang::Type::IncompleteArray:
2560 case clang::Type::VariableArray: {
2561 const clang::ArrayType *array_type =
2562 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2563
2564 if (array_type)
2565 return GetCompleteQualType(ast, array_type->getElementType(),
2566 allow_completion);
2567 } break;
2568 case clang::Type::Record: {
2569 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2570 if (cxx_record_decl) {
2571 if (cxx_record_decl->hasExternalLexicalStorage()) {
2572 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2573 const bool fields_loaded =
2574 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2575 if (is_complete && fields_loaded)
2576 return true;
2577
2578 if (!allow_completion)
2579 return false;
2580
2581 // Call the field_begin() accessor to for it to use the external source
2582 // to load the fields...
2583 clang::ExternalASTSource *external_ast_source =
2584 ast->getExternalSource();
2585 if (external_ast_source) {
2586 external_ast_source->CompleteType(cxx_record_decl);
2587 if (cxx_record_decl->isCompleteDefinition()) {
2588 cxx_record_decl->field_begin();
2589 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2590 }
2591 }
2592 }
2593 }
2594 const clang::TagType *tag_type =
2595 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2596 return !tag_type->isIncompleteType();
2597 } break;
2598
2599 case clang::Type::Enum: {
2600 const clang::TagType *tag_type =
2601 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2602 if (tag_type) {
2603 clang::TagDecl *tag_decl = tag_type->getDecl();
2604 if (tag_decl) {
2605 if (tag_decl->getDefinition())
2606 return true;
2607
2608 if (!allow_completion)
2609 return false;
2610
2611 if (tag_decl->hasExternalLexicalStorage()) {
2612 if (ast) {
2613 clang::ExternalASTSource *external_ast_source =
2614 ast->getExternalSource();
2615 if (external_ast_source) {
2616 external_ast_source->CompleteType(tag_decl);
2617 return !tag_type->isIncompleteType();
2618 }
2619 }
2620 }
2621 return false;
2622 }
2623 }
2624
2625 } break;
2626 case clang::Type::ObjCObject:
2627 case clang::Type::ObjCInterface: {
2628 const clang::ObjCObjectType *objc_class_type =
2629 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2630 if (objc_class_type) {
2631 clang::ObjCInterfaceDecl *class_interface_decl =
2632 objc_class_type->getInterface();
2633 // We currently can't complete objective C types through the newly added
Adrian Prantl05097242018-04-30 16:49:04 +00002634 // ASTContext because it only supports TagDecl objects right now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002635 if (class_interface_decl) {
2636 if (class_interface_decl->getDefinition())
2637 return true;
2638
2639 if (!allow_completion)
2640 return false;
2641
2642 if (class_interface_decl->hasExternalLexicalStorage()) {
2643 if (ast) {
2644 clang::ExternalASTSource *external_ast_source =
2645 ast->getExternalSource();
2646 if (external_ast_source) {
2647 external_ast_source->CompleteType(class_interface_decl);
2648 return !objc_class_type->isIncompleteType();
2649 }
2650 }
2651 }
2652 return false;
2653 }
2654 }
2655 } break;
2656
2657 case clang::Type::Typedef:
2658 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2659 ->getDecl()
2660 ->getUnderlyingType(),
2661 allow_completion);
2662
2663 case clang::Type::Auto:
2664 return GetCompleteQualType(
2665 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2666 allow_completion);
2667
2668 case clang::Type::Elaborated:
2669 return GetCompleteQualType(
2670 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2671 allow_completion);
2672
2673 case clang::Type::Paren:
2674 return GetCompleteQualType(
2675 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2676 allow_completion);
2677
2678 case clang::Type::Attributed:
2679 return GetCompleteQualType(
2680 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2681 allow_completion);
2682
2683 default:
2684 break;
2685 }
2686
2687 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002688}
2689
2690static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002691ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2692 switch (access) {
2693 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002694 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002695 case eAccessPublic:
2696 return clang::ObjCIvarDecl::Public;
2697 case eAccessPrivate:
2698 return clang::ObjCIvarDecl::Private;
2699 case eAccessProtected:
2700 return clang::ObjCIvarDecl::Protected;
2701 case eAccessPackage:
2702 return clang::ObjCIvarDecl::Package;
2703 }
2704 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002705}
2706
Greg Claytond8d4a572015-08-11 21:38:15 +00002707// Tests
Greg Claytond8d4a572015-08-11 21:38:15 +00002708
Kate Stoneb9c1b512016-09-06 20:57:50 +00002709bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2710 clang::QualType qual_type(GetCanonicalQualType(type));
2711
2712 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2713 switch (type_class) {
2714 case clang::Type::IncompleteArray:
2715 case clang::Type::VariableArray:
2716 case clang::Type::ConstantArray:
2717 case clang::Type::ExtVector:
2718 case clang::Type::Vector:
2719 case clang::Type::Record:
2720 case clang::Type::ObjCObject:
2721 case clang::Type::ObjCInterface:
2722 return true;
2723 case clang::Type::Auto:
2724 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2725 ->getDeducedType()
2726 .getAsOpaquePtr());
2727 case clang::Type::Elaborated:
2728 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2729 ->getNamedType()
2730 .getAsOpaquePtr());
2731 case clang::Type::Typedef:
2732 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2733 ->getDecl()
2734 ->getUnderlyingType()
2735 .getAsOpaquePtr());
2736 case clang::Type::Paren:
2737 return IsAggregateType(
2738 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2739 default:
2740 break;
2741 }
2742 // The clang type does have a value
2743 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002744}
2745
Kate Stoneb9c1b512016-09-06 20:57:50 +00002746bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2747 clang::QualType qual_type(GetCanonicalQualType(type));
2748
2749 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2750 switch (type_class) {
2751 case clang::Type::Record: {
2752 if (const clang::RecordType *record_type =
2753 llvm::dyn_cast_or_null<clang::RecordType>(
2754 qual_type.getTypePtrOrNull())) {
2755 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2756 return record_decl->isAnonymousStructOrUnion();
2757 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002758 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002759 break;
2760 }
2761 case clang::Type::Auto:
2762 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2763 ->getDeducedType()
2764 .getAsOpaquePtr());
2765 case clang::Type::Elaborated:
2766 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2767 ->getNamedType()
2768 .getAsOpaquePtr());
2769 case clang::Type::Typedef:
2770 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2771 ->getDecl()
2772 ->getUnderlyingType()
2773 .getAsOpaquePtr());
2774 case clang::Type::Paren:
2775 return IsAnonymousType(
2776 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2777 default:
2778 break;
2779 }
2780 // The clang type does have a value
2781 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002782}
2783
Kate Stoneb9c1b512016-09-06 20:57:50 +00002784bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2785 CompilerType *element_type_ptr,
2786 uint64_t *size, bool *is_incomplete) {
2787 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002788
Kate Stoneb9c1b512016-09-06 20:57:50 +00002789 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2790 switch (type_class) {
2791 default:
2792 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002793
Kate Stoneb9c1b512016-09-06 20:57:50 +00002794 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002795 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002796 element_type_ptr->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00002797 this, llvm::cast<clang::ConstantArrayType>(qual_type)
2798 ->getElementType()
2799 .getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00002800 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002801 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2802 ->getSize()
2803 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002804 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002805 *is_incomplete = false;
2806 return true;
2807
2808 case clang::Type::IncompleteArray:
2809 if (element_type_ptr)
2810 element_type_ptr->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00002811 this, llvm::cast<clang::IncompleteArrayType>(qual_type)
2812 ->getElementType()
2813 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002814 if (size)
2815 *size = 0;
2816 if (is_incomplete)
2817 *is_incomplete = true;
2818 return true;
2819
2820 case clang::Type::VariableArray:
2821 if (element_type_ptr)
2822 element_type_ptr->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00002823 this, llvm::cast<clang::VariableArrayType>(qual_type)
2824 ->getElementType()
2825 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002826 if (size)
2827 *size = 0;
2828 if (is_incomplete)
2829 *is_incomplete = false;
2830 return true;
2831
2832 case clang::Type::DependentSizedArray:
2833 if (element_type_ptr)
2834 element_type_ptr->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00002835 this, llvm::cast<clang::DependentSizedArrayType>(qual_type)
2836 ->getElementType()
2837 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002838 if (size)
2839 *size = 0;
2840 if (is_incomplete)
2841 *is_incomplete = false;
2842 return true;
2843
2844 case clang::Type::Typedef:
2845 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2846 ->getDecl()
2847 ->getUnderlyingType()
2848 .getAsOpaquePtr(),
2849 element_type_ptr, size, is_incomplete);
2850 case clang::Type::Auto:
2851 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2852 ->getDeducedType()
2853 .getAsOpaquePtr(),
2854 element_type_ptr, size, is_incomplete);
2855 case clang::Type::Elaborated:
2856 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2857 ->getNamedType()
2858 .getAsOpaquePtr(),
2859 element_type_ptr, size, is_incomplete);
2860 case clang::Type::Paren:
2861 return IsArrayType(
2862 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2863 element_type_ptr, size, is_incomplete);
2864 }
2865 if (element_type_ptr)
2866 element_type_ptr->Clear();
2867 if (size)
2868 *size = 0;
2869 if (is_incomplete)
2870 *is_incomplete = false;
2871 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002872}
2873
Kate Stoneb9c1b512016-09-06 20:57:50 +00002874bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2875 CompilerType *element_type, uint64_t *size) {
2876 clang::QualType qual_type(GetCanonicalQualType(type));
2877
2878 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2879 switch (type_class) {
2880 case clang::Type::Vector: {
2881 const clang::VectorType *vector_type =
2882 qual_type->getAs<clang::VectorType>();
2883 if (vector_type) {
2884 if (size)
2885 *size = vector_type->getNumElements();
2886 if (element_type)
2887 *element_type =
Alex Langfordbddab072019-08-13 19:40:36 +00002888 CompilerType(this, vector_type->getElementType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002889 }
2890 return true;
2891 } break;
2892 case clang::Type::ExtVector: {
2893 const clang::ExtVectorType *ext_vector_type =
2894 qual_type->getAs<clang::ExtVectorType>();
2895 if (ext_vector_type) {
2896 if (size)
2897 *size = ext_vector_type->getNumElements();
2898 if (element_type)
2899 *element_type =
Alex Langfordbddab072019-08-13 19:40:36 +00002900 CompilerType(this, ext_vector_type->getElementType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002901 }
2902 return true;
2903 }
2904 default:
2905 break;
2906 }
2907 return false;
2908}
2909
2910bool ClangASTContext::IsRuntimeGeneratedType(
2911 lldb::opaque_compiler_type_t type) {
2912 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
2913 ->GetDeclContextForType(GetQualType(type));
2914 if (!decl_ctx)
2915 return false;
2916
2917 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2918 return false;
2919
2920 clang::ObjCInterfaceDecl *result_iface_decl =
2921 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2922
2923 ClangASTMetadata *ast_metadata =
2924 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2925 if (!ast_metadata)
2926 return false;
2927 return (ast_metadata->GetISAPtr() != 0);
2928}
2929
2930bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
2931 return GetQualType(type).getUnqualifiedType()->isCharType();
2932}
2933
2934bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
2935 const bool allow_completion = false;
2936 return GetCompleteQualType(getASTContext(), GetQualType(type),
2937 allow_completion);
2938}
2939
2940bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
2941 return GetQualType(type).isConstQualified();
2942}
2943
2944bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
2945 uint32_t &length) {
2946 CompilerType pointee_or_element_clang_type;
2947 length = 0;
2948 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2949
2950 if (!pointee_or_element_clang_type.IsValid())
2951 return false;
2952
2953 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2954 if (pointee_or_element_clang_type.IsCharType()) {
2955 if (type_flags.Test(eTypeIsArray)) {
Adrian Prantl05097242018-04-30 16:49:04 +00002956 // We know the size of the array and it could be a C string since it is
2957 // an array of characters
Kate Stoneb9c1b512016-09-06 20:57:50 +00002958 length = llvm::cast<clang::ConstantArrayType>(
2959 GetCanonicalQualType(type).getTypePtr())
2960 ->getSize()
2961 .getLimitedValue();
2962 }
2963 return true;
2964 }
2965 }
2966 return false;
2967}
2968
2969bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
2970 bool *is_variadic_ptr) {
2971 if (type) {
2972 clang::QualType qual_type(GetCanonicalQualType(type));
2973
2974 if (qual_type->isFunctionType()) {
2975 if (is_variadic_ptr) {
2976 const clang::FunctionProtoType *function_proto_type =
2977 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2978 if (function_proto_type)
2979 *is_variadic_ptr = function_proto_type->isVariadic();
2980 else
2981 *is_variadic_ptr = false;
2982 }
2983 return true;
2984 }
2985
Greg Claytond8d4a572015-08-11 21:38:15 +00002986 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002987 switch (type_class) {
2988 default:
2989 break;
2990 case clang::Type::Typedef:
2991 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
2992 ->getDecl()
2993 ->getUnderlyingType()
2994 .getAsOpaquePtr(),
2995 nullptr);
2996 case clang::Type::Auto:
2997 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
2998 ->getDeducedType()
2999 .getAsOpaquePtr(),
3000 nullptr);
3001 case clang::Type::Elaborated:
3002 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3003 ->getNamedType()
3004 .getAsOpaquePtr(),
3005 nullptr);
3006 case clang::Type::Paren:
3007 return IsFunctionType(
3008 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3009 nullptr);
3010 case clang::Type::LValueReference:
3011 case clang::Type::RValueReference: {
3012 const clang::ReferenceType *reference_type =
3013 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3014 if (reference_type)
3015 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3016 nullptr);
3017 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00003018 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003019 }
3020 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003021}
3022
3023// Used to detect "Homogeneous Floating-point Aggregates"
3024uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003025ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3026 CompilerType *base_type_ptr) {
3027 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003028 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003029
3030 clang::QualType qual_type(GetCanonicalQualType(type));
3031 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3032 switch (type_class) {
3033 case clang::Type::Record:
3034 if (GetCompleteType(type)) {
3035 const clang::CXXRecordDecl *cxx_record_decl =
3036 qual_type->getAsCXXRecordDecl();
3037 if (cxx_record_decl) {
3038 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3039 return 0;
3040 }
3041 const clang::RecordType *record_type =
3042 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3043 if (record_type) {
3044 const clang::RecordDecl *record_decl = record_type->getDecl();
3045 if (record_decl) {
3046 // We are looking for a structure that contains only floating point
3047 // types
3048 clang::RecordDecl::field_iterator field_pos,
3049 field_end = record_decl->field_end();
3050 uint32_t num_fields = 0;
3051 bool is_hva = false;
3052 bool is_hfa = false;
3053 clang::QualType base_qual_type;
3054 uint64_t base_bitwidth = 0;
3055 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3056 ++field_pos) {
3057 clang::QualType field_qual_type = field_pos->getType();
3058 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3059 if (field_qual_type->isFloatingType()) {
3060 if (field_qual_type->isComplexType())
3061 return 0;
3062 else {
3063 if (num_fields == 0)
3064 base_qual_type = field_qual_type;
3065 else {
3066 if (is_hva)
3067 return 0;
3068 is_hfa = true;
3069 if (field_qual_type.getTypePtr() !=
3070 base_qual_type.getTypePtr())
3071 return 0;
3072 }
3073 }
3074 } else if (field_qual_type->isVectorType() ||
3075 field_qual_type->isExtVectorType()) {
3076 if (num_fields == 0) {
3077 base_qual_type = field_qual_type;
3078 base_bitwidth = field_bitwidth;
3079 } else {
3080 if (is_hfa)
3081 return 0;
3082 is_hva = true;
3083 if (base_bitwidth != field_bitwidth)
3084 return 0;
3085 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3086 return 0;
3087 }
3088 } else
3089 return 0;
3090 ++num_fields;
3091 }
3092 if (base_type_ptr)
Alex Langfordbddab072019-08-13 19:40:36 +00003093 *base_type_ptr = CompilerType(this, base_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003094 return num_fields;
3095 }
3096 }
3097 }
3098 break;
3099
3100 case clang::Type::Typedef:
3101 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3102 ->getDecl()
3103 ->getUnderlyingType()
3104 .getAsOpaquePtr(),
3105 base_type_ptr);
3106
3107 case clang::Type::Auto:
3108 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3109 ->getDeducedType()
3110 .getAsOpaquePtr(),
3111 base_type_ptr);
3112
3113 case clang::Type::Elaborated:
3114 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3115 ->getNamedType()
3116 .getAsOpaquePtr(),
3117 base_type_ptr);
3118 default:
3119 break;
3120 }
3121 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003122}
3123
Kate Stoneb9c1b512016-09-06 20:57:50 +00003124size_t ClangASTContext::GetNumberOfFunctionArguments(
3125 lldb::opaque_compiler_type_t type) {
3126 if (type) {
3127 clang::QualType qual_type(GetCanonicalQualType(type));
3128 const clang::FunctionProtoType *func =
3129 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3130 if (func)
3131 return func->getNumParams();
3132 }
3133 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003134}
3135
Greg Claytona1e5dc82015-08-11 22:53:00 +00003136CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003137ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3138 const size_t index) {
3139 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003140 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003141 const clang::FunctionProtoType *func =
3142 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3143 if (func) {
3144 if (index < func->getNumParams())
Alex Langfordbddab072019-08-13 19:40:36 +00003145 return CompilerType(this, func->getParamType(index).getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003146 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003147 }
3148 return CompilerType();
3149}
3150
3151bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3152 if (type) {
3153 clang::QualType qual_type(GetCanonicalQualType(type));
3154
3155 if (qual_type->isFunctionPointerType())
3156 return true;
3157
3158 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3159 switch (type_class) {
3160 default:
3161 break;
3162 case clang::Type::Typedef:
3163 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3164 ->getDecl()
3165 ->getUnderlyingType()
3166 .getAsOpaquePtr());
3167 case clang::Type::Auto:
3168 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3169 ->getDeducedType()
3170 .getAsOpaquePtr());
3171 case clang::Type::Elaborated:
3172 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3173 ->getNamedType()
3174 .getAsOpaquePtr());
3175 case clang::Type::Paren:
3176 return IsFunctionPointerType(
3177 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3178
3179 case clang::Type::LValueReference:
3180 case clang::Type::RValueReference: {
3181 const clang::ReferenceType *reference_type =
3182 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3183 if (reference_type)
3184 return IsFunctionPointerType(
3185 reference_type->getPointeeType().getAsOpaquePtr());
3186 } break;
3187 }
3188 }
3189 return false;
3190}
3191
3192bool ClangASTContext::IsBlockPointerType(
3193 lldb::opaque_compiler_type_t type,
3194 CompilerType *function_pointer_type_ptr) {
3195 if (type) {
3196 clang::QualType qual_type(GetCanonicalQualType(type));
3197
3198 if (qual_type->isBlockPointerType()) {
3199 if (function_pointer_type_ptr) {
3200 const clang::BlockPointerType *block_pointer_type =
3201 qual_type->getAs<clang::BlockPointerType>();
3202 QualType pointee_type = block_pointer_type->getPointeeType();
Jonas Devlieghered5b44032019-02-13 06:25:41 +00003203 QualType function_pointer_type = m_ast_up->getPointerType(pointee_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003204 *function_pointer_type_ptr =
Alex Langfordbddab072019-08-13 19:40:36 +00003205 CompilerType(this, function_pointer_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003206 }
3207 return true;
3208 }
3209
3210 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3211 switch (type_class) {
3212 default:
3213 break;
3214 case clang::Type::Typedef:
3215 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3216 ->getDecl()
3217 ->getUnderlyingType()
3218 .getAsOpaquePtr(),
3219 function_pointer_type_ptr);
3220 case clang::Type::Auto:
3221 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3222 ->getDeducedType()
3223 .getAsOpaquePtr(),
3224 function_pointer_type_ptr);
3225 case clang::Type::Elaborated:
3226 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3227 ->getNamedType()
3228 .getAsOpaquePtr(),
3229 function_pointer_type_ptr);
3230 case clang::Type::Paren:
3231 return IsBlockPointerType(
3232 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3233 function_pointer_type_ptr);
3234
3235 case clang::Type::LValueReference:
3236 case clang::Type::RValueReference: {
3237 const clang::ReferenceType *reference_type =
3238 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3239 if (reference_type)
3240 return IsBlockPointerType(
3241 reference_type->getPointeeType().getAsOpaquePtr(),
3242 function_pointer_type_ptr);
3243 } break;
3244 }
3245 }
3246 return false;
3247}
3248
3249bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3250 bool &is_signed) {
3251 if (!type)
3252 return false;
3253
3254 clang::QualType qual_type(GetCanonicalQualType(type));
3255 const clang::BuiltinType *builtin_type =
3256 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3257
3258 if (builtin_type) {
3259 if (builtin_type->isInteger()) {
3260 is_signed = builtin_type->isSignedInteger();
3261 return true;
3262 }
3263 }
3264
3265 return false;
3266}
3267
3268bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3269 bool &is_signed) {
3270 if (type) {
3271 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3272 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3273
3274 if (enum_type) {
3275 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3276 is_signed);
3277 return true;
3278 }
3279 }
3280
3281 return false;
3282}
3283
3284bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3285 CompilerType *pointee_type) {
3286 if (type) {
3287 clang::QualType qual_type(GetCanonicalQualType(type));
3288 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3289 switch (type_class) {
3290 case clang::Type::Builtin:
3291 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3292 default:
3293 break;
3294 case clang::BuiltinType::ObjCId:
3295 case clang::BuiltinType::ObjCClass:
3296 return true;
3297 }
3298 return false;
3299 case clang::Type::ObjCObjectPointer:
3300 if (pointee_type)
3301 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003302 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3303 ->getPointeeType()
3304 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003305 return true;
3306 case clang::Type::BlockPointer:
3307 if (pointee_type)
3308 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003309 this, llvm::cast<clang::BlockPointerType>(qual_type)
3310 ->getPointeeType()
3311 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003312 return true;
3313 case clang::Type::Pointer:
3314 if (pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003315 pointee_type->SetCompilerType(this,
3316 llvm::cast<clang::PointerType>(qual_type)
3317 ->getPointeeType()
3318 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003319 return true;
3320 case clang::Type::MemberPointer:
3321 if (pointee_type)
3322 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003323 this, llvm::cast<clang::MemberPointerType>(qual_type)
3324 ->getPointeeType()
3325 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003326 return true;
3327 case clang::Type::Typedef:
3328 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3329 ->getDecl()
3330 ->getUnderlyingType()
3331 .getAsOpaquePtr(),
3332 pointee_type);
3333 case clang::Type::Auto:
3334 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3335 ->getDeducedType()
3336 .getAsOpaquePtr(),
3337 pointee_type);
3338 case clang::Type::Elaborated:
3339 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3340 ->getNamedType()
3341 .getAsOpaquePtr(),
3342 pointee_type);
3343 case clang::Type::Paren:
3344 return IsPointerType(
3345 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3346 pointee_type);
3347 default:
3348 break;
3349 }
3350 }
3351 if (pointee_type)
3352 pointee_type->Clear();
3353 return false;
3354}
3355
3356bool ClangASTContext::IsPointerOrReferenceType(
3357 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3358 if (type) {
3359 clang::QualType qual_type(GetCanonicalQualType(type));
3360 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3361 switch (type_class) {
3362 case clang::Type::Builtin:
3363 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3364 default:
3365 break;
3366 case clang::BuiltinType::ObjCId:
3367 case clang::BuiltinType::ObjCClass:
3368 return true;
3369 }
3370 return false;
3371 case clang::Type::ObjCObjectPointer:
3372 if (pointee_type)
3373 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003374 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3375 ->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003376 return true;
3377 case clang::Type::BlockPointer:
3378 if (pointee_type)
3379 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003380 this, llvm::cast<clang::BlockPointerType>(qual_type)
3381 ->getPointeeType()
3382 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003383 return true;
3384 case clang::Type::Pointer:
3385 if (pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003386 pointee_type->SetCompilerType(this,
3387 llvm::cast<clang::PointerType>(qual_type)
3388 ->getPointeeType()
3389 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003390 return true;
3391 case clang::Type::MemberPointer:
3392 if (pointee_type)
3393 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003394 this, llvm::cast<clang::MemberPointerType>(qual_type)
3395 ->getPointeeType()
3396 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003397 return true;
3398 case clang::Type::LValueReference:
3399 if (pointee_type)
3400 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003401 this, llvm::cast<clang::LValueReferenceType>(qual_type)
3402 ->desugar()
3403 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003404 return true;
3405 case clang::Type::RValueReference:
3406 if (pointee_type)
3407 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003408 this, llvm::cast<clang::RValueReferenceType>(qual_type)
3409 ->desugar()
3410 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003411 return true;
3412 case clang::Type::Typedef:
3413 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3414 ->getDecl()
3415 ->getUnderlyingType()
3416 .getAsOpaquePtr(),
3417 pointee_type);
3418 case clang::Type::Auto:
3419 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3420 ->getDeducedType()
3421 .getAsOpaquePtr(),
3422 pointee_type);
3423 case clang::Type::Elaborated:
3424 return IsPointerOrReferenceType(
3425 llvm::cast<clang::ElaboratedType>(qual_type)
3426 ->getNamedType()
3427 .getAsOpaquePtr(),
3428 pointee_type);
3429 case clang::Type::Paren:
3430 return IsPointerOrReferenceType(
3431 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3432 pointee_type);
3433 default:
3434 break;
3435 }
3436 }
3437 if (pointee_type)
3438 pointee_type->Clear();
3439 return false;
3440}
3441
3442bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3443 CompilerType *pointee_type,
3444 bool *is_rvalue) {
3445 if (type) {
3446 clang::QualType qual_type(GetCanonicalQualType(type));
3447 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3448
3449 switch (type_class) {
3450 case clang::Type::LValueReference:
3451 if (pointee_type)
3452 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003453 this, llvm::cast<clang::LValueReferenceType>(qual_type)
3454 ->desugar()
3455 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003456 if (is_rvalue)
3457 *is_rvalue = false;
3458 return true;
3459 case clang::Type::RValueReference:
3460 if (pointee_type)
3461 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003462 this, llvm::cast<clang::RValueReferenceType>(qual_type)
3463 ->desugar()
3464 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003465 if (is_rvalue)
3466 *is_rvalue = true;
3467 return true;
3468 case clang::Type::Typedef:
3469 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3470 ->getDecl()
3471 ->getUnderlyingType()
3472 .getAsOpaquePtr(),
3473 pointee_type, is_rvalue);
3474 case clang::Type::Auto:
3475 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3476 ->getDeducedType()
3477 .getAsOpaquePtr(),
3478 pointee_type, is_rvalue);
3479 case clang::Type::Elaborated:
3480 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3481 ->getNamedType()
3482 .getAsOpaquePtr(),
3483 pointee_type, is_rvalue);
3484 case clang::Type::Paren:
3485 return IsReferenceType(
3486 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3487 pointee_type, is_rvalue);
3488
3489 default:
3490 break;
3491 }
3492 }
3493 if (pointee_type)
3494 pointee_type->Clear();
3495 return false;
3496}
3497
3498bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3499 uint32_t &count, bool &is_complex) {
3500 if (type) {
3501 clang::QualType qual_type(GetCanonicalQualType(type));
3502
3503 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3504 qual_type->getCanonicalTypeInternal())) {
3505 clang::BuiltinType::Kind kind = BT->getKind();
3506 if (kind >= clang::BuiltinType::Float &&
3507 kind <= clang::BuiltinType::LongDouble) {
3508 count = 1;
3509 is_complex = false;
3510 return true;
3511 }
3512 } else if (const clang::ComplexType *CT =
3513 llvm::dyn_cast<clang::ComplexType>(
3514 qual_type->getCanonicalTypeInternal())) {
3515 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3516 is_complex)) {
3517 count = 2;
3518 is_complex = true;
3519 return true;
3520 }
3521 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3522 qual_type->getCanonicalTypeInternal())) {
3523 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3524 is_complex)) {
3525 count = VT->getNumElements();
3526 is_complex = false;
3527 return true;
3528 }
3529 }
3530 }
3531 count = 0;
3532 is_complex = false;
3533 return false;
3534}
3535
3536bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3537 if (!type)
3538 return false;
3539
3540 clang::QualType qual_type(GetQualType(type));
3541 const clang::TagType *tag_type =
3542 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3543 if (tag_type) {
3544 clang::TagDecl *tag_decl = tag_type->getDecl();
3545 if (tag_decl)
3546 return tag_decl->isCompleteDefinition();
3547 return false;
3548 } else {
3549 const clang::ObjCObjectType *objc_class_type =
3550 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3551 if (objc_class_type) {
3552 clang::ObjCInterfaceDecl *class_interface_decl =
3553 objc_class_type->getInterface();
3554 if (class_interface_decl)
3555 return class_interface_decl->getDefinition() != nullptr;
3556 return false;
3557 }
3558 }
3559 return true;
3560}
3561
3562bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
Raphael Isemann79b3cce2019-11-08 12:03:28 +01003563 if (ClangUtil::IsClangType(type)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003564 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3565
3566 const clang::ObjCObjectPointerType *obj_pointer_type =
3567 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3568
3569 if (obj_pointer_type)
3570 return obj_pointer_type->isObjCClassType();
3571 }
3572 return false;
3573}
3574
3575bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3576 if (ClangUtil::IsClangType(type))
3577 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3578 return false;
3579}
3580
3581bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3582 if (!type)
3583 return false;
3584 clang::QualType qual_type(GetCanonicalQualType(type));
3585 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3586 return (type_class == clang::Type::Record);
3587}
3588
3589bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3590 if (!type)
3591 return false;
3592 clang::QualType qual_type(GetCanonicalQualType(type));
3593 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3594 return (type_class == clang::Type::Enum);
3595}
3596
3597bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3598 if (type) {
3599 clang::QualType qual_type(GetCanonicalQualType(type));
3600 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3601 switch (type_class) {
3602 case clang::Type::Record:
3603 if (GetCompleteType(type)) {
3604 const clang::RecordType *record_type =
3605 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3606 const clang::RecordDecl *record_decl = record_type->getDecl();
3607 if (record_decl) {
3608 const clang::CXXRecordDecl *cxx_record_decl =
3609 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3610 if (cxx_record_decl)
3611 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003612 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003613 }
3614 break;
3615
3616 default:
3617 break;
3618 }
3619 }
3620 return false;
3621}
3622
3623bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3624 CompilerType *dynamic_pointee_type,
3625 bool check_cplusplus,
3626 bool check_objc) {
3627 clang::QualType pointee_qual_type;
3628 if (type) {
3629 clang::QualType qual_type(GetCanonicalQualType(type));
3630 bool success = false;
3631 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3632 switch (type_class) {
3633 case clang::Type::Builtin:
3634 if (check_objc &&
3635 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3636 clang::BuiltinType::ObjCId) {
3637 if (dynamic_pointee_type)
3638 dynamic_pointee_type->SetCompilerType(this, type);
3639 return true;
3640 }
3641 break;
3642
3643 case clang::Type::ObjCObjectPointer:
3644 if (check_objc) {
3645 if (auto objc_pointee_type =
3646 qual_type->getPointeeType().getTypePtrOrNull()) {
3647 if (auto objc_object_type =
3648 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3649 objc_pointee_type)) {
3650 if (objc_object_type->isObjCClass())
3651 return false;
3652 }
3653 }
3654 if (dynamic_pointee_type)
3655 dynamic_pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003656 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3657 ->getPointeeType()
3658 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003659 return true;
3660 }
3661 break;
3662
3663 case clang::Type::Pointer:
3664 pointee_qual_type =
3665 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3666 success = true;
3667 break;
3668
3669 case clang::Type::LValueReference:
3670 case clang::Type::RValueReference:
3671 pointee_qual_type =
3672 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3673 success = true;
3674 break;
3675
3676 case clang::Type::Typedef:
3677 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3678 ->getDecl()
3679 ->getUnderlyingType()
3680 .getAsOpaquePtr(),
3681 dynamic_pointee_type, check_cplusplus,
3682 check_objc);
3683
3684 case clang::Type::Auto:
3685 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3686 ->getDeducedType()
3687 .getAsOpaquePtr(),
3688 dynamic_pointee_type, check_cplusplus,
3689 check_objc);
3690
3691 case clang::Type::Elaborated:
3692 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3693 ->getNamedType()
3694 .getAsOpaquePtr(),
3695 dynamic_pointee_type, check_cplusplus,
3696 check_objc);
3697
3698 case clang::Type::Paren:
3699 return IsPossibleDynamicType(
3700 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3701 dynamic_pointee_type, check_cplusplus, check_objc);
3702 default:
3703 break;
3704 }
3705
3706 if (success) {
3707 // Check to make sure what we are pointing too is a possible dynamic C++
Adrian Prantl05097242018-04-30 16:49:04 +00003708 // type We currently accept any "void *" (in case we have a class that
3709 // has been watered down to an opaque pointer) and virtual C++ classes.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003710 const clang::Type::TypeClass pointee_type_class =
3711 pointee_qual_type.getCanonicalType()->getTypeClass();
3712 switch (pointee_type_class) {
3713 case clang::Type::Builtin:
3714 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3715 case clang::BuiltinType::UnknownAny:
3716 case clang::BuiltinType::Void:
3717 if (dynamic_pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003718 dynamic_pointee_type->SetCompilerType(
3719 this, pointee_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003720 return true;
3721 default:
3722 break;
3723 }
3724 break;
3725
3726 case clang::Type::Record:
3727 if (check_cplusplus) {
3728 clang::CXXRecordDecl *cxx_record_decl =
3729 pointee_qual_type->getAsCXXRecordDecl();
3730 if (cxx_record_decl) {
3731 bool is_complete = cxx_record_decl->isCompleteDefinition();
3732
3733 if (is_complete)
3734 success = cxx_record_decl->isDynamicClass();
3735 else {
3736 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3737 getASTContext(), cxx_record_decl);
3738 if (metadata)
3739 success = metadata->GetIsDynamicCXXType();
3740 else {
Alex Langfordbddab072019-08-13 19:40:36 +00003741 is_complete =
3742 CompilerType(this, pointee_qual_type.getAsOpaquePtr())
3743 .GetCompleteType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003744 if (is_complete)
3745 success = cxx_record_decl->isDynamicClass();
3746 else
3747 success = false;
3748 }
3749 }
3750
3751 if (success) {
3752 if (dynamic_pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003753 dynamic_pointee_type->SetCompilerType(
3754 this, pointee_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003755 return true;
3756 }
3757 }
3758 }
3759 break;
3760
3761 case clang::Type::ObjCObject:
3762 case clang::Type::ObjCInterface:
3763 if (check_objc) {
3764 if (dynamic_pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003765 dynamic_pointee_type->SetCompilerType(
3766 this, pointee_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003767 return true;
3768 }
3769 break;
3770
3771 default:
3772 break;
3773 }
3774 }
3775 }
3776 if (dynamic_pointee_type)
3777 dynamic_pointee_type->Clear();
3778 return false;
3779}
3780
3781bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3782 if (!type)
3783 return false;
3784
3785 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3786}
3787
3788bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3789 if (!type)
3790 return false;
3791 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3792}
3793
3794bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3795 if (!type)
3796 return false;
3797 return GetCanonicalQualType(type)->isVoidType();
3798}
3799
Alex Langforda03e2b22019-06-04 19:29:59 +00003800bool ClangASTContext::CanPassInRegisters(const CompilerType &type) {
3801 if (auto *record_decl =
3802 ClangASTContext::GetAsRecordDecl(type)) {
3803 return record_decl->canPassInRegisters();
3804 }
3805 return false;
3806}
3807
Kate Stoneb9c1b512016-09-06 20:57:50 +00003808bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3809 return ClangASTContextSupportsLanguage(language);
3810}
3811
Alex Langforddb542452019-10-30 12:50:05 -07003812Optional<std::string>
3813ClangASTContext::GetCXXClassName(const CompilerType &type) {
3814 if (!type)
3815 return llvm::None;
3816
3817 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3818 if (qual_type.isNull())
3819 return llvm::None;
3820
3821 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3822 if (!cxx_record_decl)
3823 return llvm::None;
3824
3825 return std::string(cxx_record_decl->getIdentifier()->getNameStart());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003826}
3827
3828bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3829 if (!type)
3830 return false;
3831
3832 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Jonas Devliegherea6682a42018-12-15 00:15:33 +00003833 return !qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003834}
3835
3836bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3837 if (!type)
3838 return false;
3839 clang::QualType qual_type(GetCanonicalQualType(type));
3840 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3841 if (tag_type)
3842 return tag_type->isBeingDefined();
3843 return false;
3844}
3845
3846bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3847 CompilerType *class_type_ptr) {
Raphael Isemann79b3cce2019-11-08 12:03:28 +01003848 if (!ClangUtil::IsClangType(type))
Kate Stoneb9c1b512016-09-06 20:57:50 +00003849 return false;
3850
3851 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3852
3853 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3854 if (class_type_ptr) {
3855 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3856 const clang::ObjCObjectPointerType *obj_pointer_type =
3857 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3858 if (obj_pointer_type == nullptr)
3859 class_type_ptr->Clear();
3860 else
3861 class_type_ptr->SetCompilerType(
3862 type.GetTypeSystem(),
3863 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3864 .getAsOpaquePtr());
3865 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003866 }
3867 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003868 }
3869 if (class_type_ptr)
3870 class_type_ptr->Clear();
3871 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003872}
3873
Greg Claytond8d4a572015-08-11 21:38:15 +00003874// Type Completion
Greg Claytond8d4a572015-08-11 21:38:15 +00003875
Kate Stoneb9c1b512016-09-06 20:57:50 +00003876bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3877 if (!type)
3878 return false;
3879 const bool allow_completion = true;
3880 return GetCompleteQualType(getASTContext(), GetQualType(type),
3881 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00003882}
3883
Kate Stoneb9c1b512016-09-06 20:57:50 +00003884ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
3885 std::string type_name;
3886 if (type) {
3887 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
3888 clang::QualType qual_type(GetQualType(type));
3889 printing_policy.SuppressTagKeyword = true;
3890 const clang::TypedefType *typedef_type =
3891 qual_type->getAs<clang::TypedefType>();
3892 if (typedef_type) {
3893 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3894 type_name = typedef_decl->getQualifiedNameAsString();
3895 } else {
3896 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003897 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003898 }
3899 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00003900}
3901
3902uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003903ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
3904 CompilerType *pointee_or_element_clang_type) {
3905 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003906 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003907
3908 if (pointee_or_element_clang_type)
3909 pointee_or_element_clang_type->Clear();
3910
3911 clang::QualType qual_type(GetQualType(type));
3912
3913 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3914 switch (type_class) {
Sean Callananddf802a2017-06-02 01:24:18 +00003915 case clang::Type::Attributed:
3916 return GetTypeInfo(
3917 qual_type->getAs<clang::AttributedType>()
3918 ->getModifiedType().getAsOpaquePtr(),
3919 pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003920 case clang::Type::Builtin: {
3921 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3922 qual_type->getCanonicalTypeInternal());
3923
3924 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3925 switch (builtin_type->getKind()) {
3926 case clang::BuiltinType::ObjCId:
3927 case clang::BuiltinType::ObjCClass:
3928 if (pointee_or_element_clang_type)
3929 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003930 this, getASTContext()->ObjCBuiltinClassTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003931 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3932 break;
3933
3934 case clang::BuiltinType::ObjCSel:
3935 if (pointee_or_element_clang_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003936 pointee_or_element_clang_type->SetCompilerType(
3937 this, getASTContext()->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003938 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3939 break;
3940
3941 case clang::BuiltinType::Bool:
3942 case clang::BuiltinType::Char_U:
3943 case clang::BuiltinType::UChar:
3944 case clang::BuiltinType::WChar_U:
3945 case clang::BuiltinType::Char16:
3946 case clang::BuiltinType::Char32:
3947 case clang::BuiltinType::UShort:
3948 case clang::BuiltinType::UInt:
3949 case clang::BuiltinType::ULong:
3950 case clang::BuiltinType::ULongLong:
3951 case clang::BuiltinType::UInt128:
3952 case clang::BuiltinType::Char_S:
3953 case clang::BuiltinType::SChar:
3954 case clang::BuiltinType::WChar_S:
3955 case clang::BuiltinType::Short:
3956 case clang::BuiltinType::Int:
3957 case clang::BuiltinType::Long:
3958 case clang::BuiltinType::LongLong:
3959 case clang::BuiltinType::Int128:
3960 case clang::BuiltinType::Float:
3961 case clang::BuiltinType::Double:
3962 case clang::BuiltinType::LongDouble:
3963 builtin_type_flags |= eTypeIsScalar;
3964 if (builtin_type->isInteger()) {
3965 builtin_type_flags |= eTypeIsInteger;
3966 if (builtin_type->isSignedInteger())
3967 builtin_type_flags |= eTypeIsSigned;
3968 } else if (builtin_type->isFloatingPoint())
3969 builtin_type_flags |= eTypeIsFloat;
3970 break;
3971 default:
3972 break;
3973 }
3974 return builtin_type_flags;
3975 }
3976
3977 case clang::Type::BlockPointer:
3978 if (pointee_or_element_clang_type)
3979 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003980 this, qual_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003981 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3982
3983 case clang::Type::Complex: {
3984 uint32_t complex_type_flags =
3985 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3986 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
3987 qual_type->getCanonicalTypeInternal());
3988 if (complex_type) {
3989 clang::QualType complex_element_type(complex_type->getElementType());
3990 if (complex_element_type->isIntegerType())
3991 complex_type_flags |= eTypeIsFloat;
3992 else if (complex_element_type->isFloatingType())
3993 complex_type_flags |= eTypeIsInteger;
3994 }
3995 return complex_type_flags;
3996 } break;
3997
3998 case clang::Type::ConstantArray:
3999 case clang::Type::DependentSizedArray:
4000 case clang::Type::IncompleteArray:
4001 case clang::Type::VariableArray:
4002 if (pointee_or_element_clang_type)
4003 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004004 this, llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4005 ->getElementType()
4006 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004007 return eTypeHasChildren | eTypeIsArray;
4008
4009 case clang::Type::DependentName:
4010 return 0;
4011 case clang::Type::DependentSizedExtVector:
4012 return eTypeHasChildren | eTypeIsVector;
4013 case clang::Type::DependentTemplateSpecialization:
4014 return eTypeIsTemplate;
4015 case clang::Type::Decltype:
Alex Langfordbddab072019-08-13 19:40:36 +00004016 return CompilerType(this, llvm::cast<clang::DecltypeType>(qual_type)
4017 ->getUnderlyingType()
4018 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004019 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004020
4021 case clang::Type::Enum:
4022 if (pointee_or_element_clang_type)
4023 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004024 this, llvm::cast<clang::EnumType>(qual_type)
4025 ->getDecl()
4026 ->getIntegerType()
4027 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004028 return eTypeIsEnumeration | eTypeHasValue;
4029
4030 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00004031 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
4032 ->getDeducedType()
4033 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004034 .GetTypeInfo(pointee_or_element_clang_type);
4035 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00004036 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
4037 ->getNamedType()
4038 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004039 .GetTypeInfo(pointee_or_element_clang_type);
4040 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00004041 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
4042 ->desugar()
4043 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004044 .GetTypeInfo(pointee_or_element_clang_type);
4045
4046 case clang::Type::FunctionProto:
4047 return eTypeIsFuncPrototype | eTypeHasValue;
4048 case clang::Type::FunctionNoProto:
4049 return eTypeIsFuncPrototype | eTypeHasValue;
4050 case clang::Type::InjectedClassName:
4051 return 0;
4052
4053 case clang::Type::LValueReference:
4054 case clang::Type::RValueReference:
4055 if (pointee_or_element_clang_type)
4056 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004057 this, llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4058 ->getPointeeType()
4059 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004060 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4061
4062 case clang::Type::MemberPointer:
4063 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4064
4065 case clang::Type::ObjCObjectPointer:
4066 if (pointee_or_element_clang_type)
4067 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004068 this, qual_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004069 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4070 eTypeHasValue;
4071
4072 case clang::Type::ObjCObject:
4073 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4074 case clang::Type::ObjCInterface:
4075 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4076
4077 case clang::Type::Pointer:
4078 if (pointee_or_element_clang_type)
4079 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004080 this, qual_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004081 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4082
4083 case clang::Type::Record:
4084 if (qual_type->getAsCXXRecordDecl())
4085 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4086 else
4087 return eTypeHasChildren | eTypeIsStructUnion;
4088 break;
4089 case clang::Type::SubstTemplateTypeParm:
4090 return eTypeIsTemplate;
4091 case clang::Type::TemplateTypeParm:
4092 return eTypeIsTemplate;
4093 case clang::Type::TemplateSpecialization:
4094 return eTypeIsTemplate;
4095
4096 case clang::Type::Typedef:
4097 return eTypeIsTypedef |
Alex Langfordbddab072019-08-13 19:40:36 +00004098 CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
4099 ->getDecl()
4100 ->getUnderlyingType()
4101 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004102 .GetTypeInfo(pointee_or_element_clang_type);
4103 case clang::Type::TypeOfExpr:
Alex Langfordbddab072019-08-13 19:40:36 +00004104 return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type)
4105 ->getUnderlyingExpr()
4106 ->getType()
4107 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004108 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004109 case clang::Type::TypeOf:
Alex Langfordbddab072019-08-13 19:40:36 +00004110 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
4111 ->getUnderlyingType()
4112 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004113 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004114 case clang::Type::UnresolvedUsing:
4115 return 0;
4116
4117 case clang::Type::ExtVector:
4118 case clang::Type::Vector: {
4119 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4120 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4121 qual_type->getCanonicalTypeInternal());
4122 if (vector_type) {
4123 if (vector_type->isIntegerType())
4124 vector_type_flags |= eTypeIsFloat;
4125 else if (vector_type->isFloatingType())
4126 vector_type_flags |= eTypeIsInteger;
4127 }
4128 return vector_type_flags;
4129 }
4130 default:
4131 return 0;
4132 }
4133 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004134}
4135
Greg Claytond8d4a572015-08-11 21:38:15 +00004136lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004137ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4138 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004139 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004140
4141 // If the type is a reference, then resolve it to what it refers to first:
4142 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4143 if (qual_type->isAnyPointerType()) {
4144 if (qual_type->isObjCObjectPointerType())
4145 return lldb::eLanguageTypeObjC;
Adrian Prantl1db0f0c2019-05-02 23:07:23 +00004146 if (qual_type->getPointeeCXXRecordDecl())
4147 return lldb::eLanguageTypeC_plus_plus;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004148
4149 clang::QualType pointee_type(qual_type->getPointeeType());
Adrian Prantl1db0f0c2019-05-02 23:07:23 +00004150 if (pointee_type->getPointeeCXXRecordDecl())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004151 return lldb::eLanguageTypeC_plus_plus;
4152 if (pointee_type->isObjCObjectOrInterfaceType())
4153 return lldb::eLanguageTypeObjC;
4154 if (pointee_type->isObjCClassType())
4155 return lldb::eLanguageTypeObjC;
4156 if (pointee_type.getTypePtr() ==
4157 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4158 return lldb::eLanguageTypeObjC;
4159 } else {
4160 if (qual_type->isObjCObjectOrInterfaceType())
4161 return lldb::eLanguageTypeObjC;
4162 if (qual_type->getAsCXXRecordDecl())
4163 return lldb::eLanguageTypeC_plus_plus;
4164 switch (qual_type->getTypeClass()) {
4165 default:
4166 break;
4167 case clang::Type::Builtin:
4168 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4169 default:
4170 case clang::BuiltinType::Void:
4171 case clang::BuiltinType::Bool:
4172 case clang::BuiltinType::Char_U:
4173 case clang::BuiltinType::UChar:
4174 case clang::BuiltinType::WChar_U:
4175 case clang::BuiltinType::Char16:
4176 case clang::BuiltinType::Char32:
4177 case clang::BuiltinType::UShort:
4178 case clang::BuiltinType::UInt:
4179 case clang::BuiltinType::ULong:
4180 case clang::BuiltinType::ULongLong:
4181 case clang::BuiltinType::UInt128:
4182 case clang::BuiltinType::Char_S:
4183 case clang::BuiltinType::SChar:
4184 case clang::BuiltinType::WChar_S:
4185 case clang::BuiltinType::Short:
4186 case clang::BuiltinType::Int:
4187 case clang::BuiltinType::Long:
4188 case clang::BuiltinType::LongLong:
4189 case clang::BuiltinType::Int128:
4190 case clang::BuiltinType::Float:
4191 case clang::BuiltinType::Double:
4192 case clang::BuiltinType::LongDouble:
4193 break;
4194
4195 case clang::BuiltinType::NullPtr:
4196 return eLanguageTypeC_plus_plus;
4197
4198 case clang::BuiltinType::ObjCId:
4199 case clang::BuiltinType::ObjCClass:
4200 case clang::BuiltinType::ObjCSel:
4201 return eLanguageTypeObjC;
4202
4203 case clang::BuiltinType::Dependent:
4204 case clang::BuiltinType::Overload:
4205 case clang::BuiltinType::BoundMember:
4206 case clang::BuiltinType::UnknownAny:
4207 break;
4208 }
4209 break;
4210 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00004211 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
4212 ->getDecl()
4213 ->getUnderlyingType()
4214 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004215 .GetMinimumLanguage();
4216 }
4217 }
4218 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004219}
4220
4221lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004222ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4223 if (!type)
4224 return lldb::eTypeClassInvalid;
4225
4226 clang::QualType qual_type(GetQualType(type));
4227
4228 switch (qual_type->getTypeClass()) {
4229 case clang::Type::UnaryTransform:
4230 break;
4231 case clang::Type::FunctionNoProto:
4232 return lldb::eTypeClassFunction;
4233 case clang::Type::FunctionProto:
4234 return lldb::eTypeClassFunction;
4235 case clang::Type::IncompleteArray:
4236 return lldb::eTypeClassArray;
4237 case clang::Type::VariableArray:
4238 return lldb::eTypeClassArray;
4239 case clang::Type::ConstantArray:
4240 return lldb::eTypeClassArray;
4241 case clang::Type::DependentSizedArray:
4242 return lldb::eTypeClassArray;
4243 case clang::Type::DependentSizedExtVector:
4244 return lldb::eTypeClassVector;
Fangrui Song8f284882018-07-13 22:40:40 +00004245 case clang::Type::DependentVector:
4246 return lldb::eTypeClassVector;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004247 case clang::Type::ExtVector:
4248 return lldb::eTypeClassVector;
4249 case clang::Type::Vector:
4250 return lldb::eTypeClassVector;
4251 case clang::Type::Builtin:
4252 return lldb::eTypeClassBuiltin;
4253 case clang::Type::ObjCObjectPointer:
4254 return lldb::eTypeClassObjCObjectPointer;
4255 case clang::Type::BlockPointer:
4256 return lldb::eTypeClassBlockPointer;
4257 case clang::Type::Pointer:
4258 return lldb::eTypeClassPointer;
4259 case clang::Type::LValueReference:
4260 return lldb::eTypeClassReference;
4261 case clang::Type::RValueReference:
4262 return lldb::eTypeClassReference;
4263 case clang::Type::MemberPointer:
4264 return lldb::eTypeClassMemberPointer;
4265 case clang::Type::Complex:
4266 if (qual_type->isComplexType())
4267 return lldb::eTypeClassComplexFloat;
4268 else
4269 return lldb::eTypeClassComplexInteger;
4270 case clang::Type::ObjCObject:
4271 return lldb::eTypeClassObjCObject;
4272 case clang::Type::ObjCInterface:
4273 return lldb::eTypeClassObjCInterface;
4274 case clang::Type::Record: {
4275 const clang::RecordType *record_type =
4276 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4277 const clang::RecordDecl *record_decl = record_type->getDecl();
4278 if (record_decl->isUnion())
4279 return lldb::eTypeClassUnion;
4280 else if (record_decl->isStruct())
4281 return lldb::eTypeClassStruct;
4282 else
4283 return lldb::eTypeClassClass;
4284 } break;
4285 case clang::Type::Enum:
4286 return lldb::eTypeClassEnumeration;
4287 case clang::Type::Typedef:
4288 return lldb::eTypeClassTypedef;
4289 case clang::Type::UnresolvedUsing:
4290 break;
4291 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00004292 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
4293 ->desugar()
4294 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004295 .GetTypeClass();
4296 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00004297 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
4298 ->getDeducedType()
4299 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004300 .GetTypeClass();
4301 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00004302 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
4303 ->getNamedType()
4304 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004305 .GetTypeClass();
4306
4307 case clang::Type::Attributed:
4308 break;
4309 case clang::Type::TemplateTypeParm:
4310 break;
4311 case clang::Type::SubstTemplateTypeParm:
4312 break;
4313 case clang::Type::SubstTemplateTypeParmPack:
4314 break;
4315 case clang::Type::InjectedClassName:
4316 break;
4317 case clang::Type::DependentName:
4318 break;
4319 case clang::Type::DependentTemplateSpecialization:
4320 break;
4321 case clang::Type::PackExpansion:
4322 break;
4323
4324 case clang::Type::TypeOfExpr:
Alex Langfordbddab072019-08-13 19:40:36 +00004325 return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type)
4326 ->getUnderlyingExpr()
4327 ->getType()
4328 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004329 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004330 case clang::Type::TypeOf:
Alex Langfordbddab072019-08-13 19:40:36 +00004331 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
4332 ->getUnderlyingType()
4333 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004334 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004335 case clang::Type::Decltype:
Alex Langfordbddab072019-08-13 19:40:36 +00004336 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
4337 ->getUnderlyingType()
4338 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004339 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004340 case clang::Type::TemplateSpecialization:
4341 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004342 case clang::Type::DeducedTemplateSpecialization:
4343 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004344 case clang::Type::Atomic:
4345 break;
4346 case clang::Type::Pipe:
4347 break;
4348
4349 // pointer type decayed from an array or function type.
4350 case clang::Type::Decayed:
4351 break;
4352 case clang::Type::Adjusted:
4353 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004354 case clang::Type::ObjCTypeParam:
4355 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00004356
4357 case clang::Type::DependentAddressSpace:
4358 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00004359 case clang::Type::MacroQualified:
4360 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004361 }
4362 // We don't know hot to display this type...
4363 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004364}
4365
Kate Stoneb9c1b512016-09-06 20:57:50 +00004366unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4367 if (type)
4368 return GetQualType(type).getQualifiers().getCVRQualifiers();
4369 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004370}
4371
Greg Claytond8d4a572015-08-11 21:38:15 +00004372// Creating related types
Greg Claytond8d4a572015-08-11 21:38:15 +00004373
Greg Claytona1e5dc82015-08-11 22:53:00 +00004374CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004375ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4376 uint64_t *stride) {
4377 if (type) {
4378 clang::QualType qual_type(GetCanonicalQualType(type));
4379
4380 const clang::Type *array_eletype =
4381 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4382
4383 if (!array_eletype)
4384 return CompilerType();
4385
Alex Langfordbddab072019-08-13 19:40:36 +00004386 CompilerType element_type(
4387 this, array_eletype->getCanonicalTypeUnqualified().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004388
4389 // TODO: the real stride will be >= this value.. find the real one!
4390 if (stride)
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00004391 if (Optional<uint64_t> size = element_type.GetByteSize(nullptr))
Adrian Prantld963a7c2019-01-15 18:07:52 +00004392 *stride = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004393
4394 return element_type;
4395 }
4396 return CompilerType();
4397}
4398
4399CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4400 uint64_t size) {
4401 if (type) {
4402 clang::QualType qual_type(GetCanonicalQualType(type));
4403 if (clang::ASTContext *ast_ctx = getASTContext()) {
4404 if (size != 0)
4405 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004406 this, ast_ctx
4407 ->getConstantArrayType(
Richard Smith772e2662019-10-04 01:25:59 +00004408 qual_type, llvm::APInt(64, size), nullptr,
Alex Langfordbddab072019-08-13 19:40:36 +00004409 clang::ArrayType::ArraySizeModifier::Normal, 0)
4410 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004411 else
4412 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004413 this,
4414 ast_ctx
4415 ->getIncompleteArrayType(
4416 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0)
4417 .getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00004418 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004419 }
4420
4421 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004422}
4423
Greg Claytona1e5dc82015-08-11 22:53:00 +00004424CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004425ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4426 if (type)
Alex Langfordbddab072019-08-13 19:40:36 +00004427 return CompilerType(this, GetCanonicalQualType(type).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004428 return CompilerType();
4429}
4430
4431static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4432 clang::QualType qual_type) {
4433 if (qual_type->isPointerType())
4434 qual_type = ast->getPointerType(
4435 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4436 else
4437 qual_type = qual_type.getUnqualifiedType();
4438 qual_type.removeLocalConst();
4439 qual_type.removeLocalRestrict();
4440 qual_type.removeLocalVolatile();
4441 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004442}
4443
4444CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004445ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4446 if (type)
4447 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004448 this,
4449 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004450 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004451}
4452
Kate Stoneb9c1b512016-09-06 20:57:50 +00004453int ClangASTContext::GetFunctionArgumentCount(
4454 lldb::opaque_compiler_type_t type) {
4455 if (type) {
4456 const clang::FunctionProtoType *func =
4457 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4458 if (func)
4459 return func->getNumParams();
4460 }
4461 return -1;
4462}
4463
4464CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4465 lldb::opaque_compiler_type_t type, size_t idx) {
4466 if (type) {
4467 const clang::FunctionProtoType *func =
4468 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4469 if (func) {
4470 const uint32_t num_args = func->getNumParams();
4471 if (idx < num_args)
Alex Langfordbddab072019-08-13 19:40:36 +00004472 return CompilerType(this, func->getParamType(idx).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004473 }
4474 }
4475 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004476}
4477
Greg Claytona1e5dc82015-08-11 22:53:00 +00004478CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004479ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4480 if (type) {
4481 clang::QualType qual_type(GetQualType(type));
4482 const clang::FunctionProtoType *func =
4483 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4484 if (func)
Alex Langfordbddab072019-08-13 19:40:36 +00004485 return CompilerType(this, func->getReturnType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004486 }
4487 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004488}
4489
4490size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004491ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4492 size_t num_functions = 0;
4493 if (type) {
4494 clang::QualType qual_type(GetCanonicalQualType(type));
4495 switch (qual_type->getTypeClass()) {
4496 case clang::Type::Record:
4497 if (GetCompleteQualType(getASTContext(), qual_type)) {
4498 const clang::RecordType *record_type =
4499 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4500 const clang::RecordDecl *record_decl = record_type->getDecl();
4501 assert(record_decl);
4502 const clang::CXXRecordDecl *cxx_record_decl =
4503 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4504 if (cxx_record_decl)
4505 num_functions = std::distance(cxx_record_decl->method_begin(),
4506 cxx_record_decl->method_end());
4507 }
4508 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004509
Sean Callananf9c622a2016-09-30 18:44:43 +00004510 case clang::Type::ObjCObjectPointer: {
4511 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004512 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004513 const clang::ObjCInterfaceType *objc_interface_type =
4514 objc_class_type->getInterfaceType();
4515 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004516 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4517 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004518 clang::ObjCInterfaceDecl *class_interface_decl =
4519 objc_interface_type->getDecl();
4520 if (class_interface_decl) {
4521 num_functions = std::distance(class_interface_decl->meth_begin(),
4522 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004523 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004524 }
4525 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004526 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004527
4528 case clang::Type::ObjCObject:
4529 case clang::Type::ObjCInterface:
4530 if (GetCompleteType(type)) {
4531 const clang::ObjCObjectType *objc_class_type =
4532 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4533 if (objc_class_type) {
4534 clang::ObjCInterfaceDecl *class_interface_decl =
4535 objc_class_type->getInterface();
4536 if (class_interface_decl)
4537 num_functions = std::distance(class_interface_decl->meth_begin(),
4538 class_interface_decl->meth_end());
4539 }
4540 }
4541 break;
4542
4543 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00004544 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
4545 ->getDecl()
4546 ->getUnderlyingType()
4547 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004548 .GetNumMemberFunctions();
4549
4550 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00004551 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
4552 ->getDeducedType()
4553 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004554 .GetNumMemberFunctions();
4555
4556 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00004557 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
4558 ->getNamedType()
4559 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004560 .GetNumMemberFunctions();
4561
4562 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00004563 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
4564 ->desugar()
4565 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004566 .GetNumMemberFunctions();
4567
4568 default:
4569 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004570 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004571 }
4572 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004573}
4574
4575TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004576ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4577 size_t idx) {
4578 std::string name;
4579 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4580 CompilerType clang_type;
4581 CompilerDecl clang_decl;
4582 if (type) {
4583 clang::QualType qual_type(GetCanonicalQualType(type));
4584 switch (qual_type->getTypeClass()) {
4585 case clang::Type::Record:
4586 if (GetCompleteQualType(getASTContext(), qual_type)) {
4587 const clang::RecordType *record_type =
4588 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4589 const clang::RecordDecl *record_decl = record_type->getDecl();
4590 assert(record_decl);
4591 const clang::CXXRecordDecl *cxx_record_decl =
4592 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4593 if (cxx_record_decl) {
4594 auto method_iter = cxx_record_decl->method_begin();
4595 auto method_end = cxx_record_decl->method_end();
4596 if (idx <
4597 static_cast<size_t>(std::distance(method_iter, method_end))) {
4598 std::advance(method_iter, idx);
4599 clang::CXXMethodDecl *cxx_method_decl =
4600 method_iter->getCanonicalDecl();
4601 if (cxx_method_decl) {
4602 name = cxx_method_decl->getDeclName().getAsString();
4603 if (cxx_method_decl->isStatic())
4604 kind = lldb::eMemberFunctionKindStaticMethod;
4605 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4606 kind = lldb::eMemberFunctionKindConstructor;
4607 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4608 kind = lldb::eMemberFunctionKindDestructor;
4609 else
4610 kind = lldb::eMemberFunctionKindInstanceMethod;
4611 clang_type = CompilerType(
4612 this, cxx_method_decl->getType().getAsOpaquePtr());
4613 clang_decl = CompilerDecl(this, cxx_method_decl);
4614 }
4615 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004616 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004617 }
4618 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004619
Sean Callananf9c622a2016-09-30 18:44:43 +00004620 case clang::Type::ObjCObjectPointer: {
4621 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004622 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004623 const clang::ObjCInterfaceType *objc_interface_type =
4624 objc_class_type->getInterfaceType();
4625 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004626 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4627 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004628 clang::ObjCInterfaceDecl *class_interface_decl =
4629 objc_interface_type->getDecl();
4630 if (class_interface_decl) {
4631 auto method_iter = class_interface_decl->meth_begin();
4632 auto method_end = class_interface_decl->meth_end();
4633 if (idx <
4634 static_cast<size_t>(std::distance(method_iter, method_end))) {
4635 std::advance(method_iter, idx);
4636 clang::ObjCMethodDecl *objc_method_decl =
4637 method_iter->getCanonicalDecl();
4638 if (objc_method_decl) {
4639 clang_decl = CompilerDecl(this, objc_method_decl);
4640 name = objc_method_decl->getSelector().getAsString();
4641 if (objc_method_decl->isClassMethod())
4642 kind = lldb::eMemberFunctionKindStaticMethod;
4643 else
4644 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004645 }
4646 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004647 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004648 }
4649 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004650 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004651
Kate Stoneb9c1b512016-09-06 20:57:50 +00004652 case clang::Type::ObjCObject:
4653 case clang::Type::ObjCInterface:
4654 if (GetCompleteType(type)) {
4655 const clang::ObjCObjectType *objc_class_type =
4656 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4657 if (objc_class_type) {
4658 clang::ObjCInterfaceDecl *class_interface_decl =
4659 objc_class_type->getInterface();
4660 if (class_interface_decl) {
4661 auto method_iter = class_interface_decl->meth_begin();
4662 auto method_end = class_interface_decl->meth_end();
4663 if (idx <
4664 static_cast<size_t>(std::distance(method_iter, method_end))) {
4665 std::advance(method_iter, idx);
4666 clang::ObjCMethodDecl *objc_method_decl =
4667 method_iter->getCanonicalDecl();
4668 if (objc_method_decl) {
4669 clang_decl = CompilerDecl(this, objc_method_decl);
4670 name = objc_method_decl->getSelector().getAsString();
4671 if (objc_method_decl->isClassMethod())
4672 kind = lldb::eMemberFunctionKindStaticMethod;
4673 else
4674 kind = lldb::eMemberFunctionKindInstanceMethod;
4675 }
4676 }
4677 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004678 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004679 }
4680 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004681
Kate Stoneb9c1b512016-09-06 20:57:50 +00004682 case clang::Type::Typedef:
4683 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4684 ->getDecl()
4685 ->getUnderlyingType()
4686 .getAsOpaquePtr(),
4687 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004688
Kate Stoneb9c1b512016-09-06 20:57:50 +00004689 case clang::Type::Auto:
4690 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4691 ->getDeducedType()
4692 .getAsOpaquePtr(),
4693 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004694
Kate Stoneb9c1b512016-09-06 20:57:50 +00004695 case clang::Type::Elaborated:
4696 return GetMemberFunctionAtIndex(
4697 llvm::cast<clang::ElaboratedType>(qual_type)
4698 ->getNamedType()
4699 .getAsOpaquePtr(),
4700 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004701
Kate Stoneb9c1b512016-09-06 20:57:50 +00004702 case clang::Type::Paren:
4703 return GetMemberFunctionAtIndex(
4704 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4705 idx);
4706
4707 default:
4708 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004709 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004710 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004711
Kate Stoneb9c1b512016-09-06 20:57:50 +00004712 if (kind == eMemberFunctionKindUnknown)
4713 return TypeMemberFunctionImpl();
4714 else
4715 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004716}
4717
Greg Claytona1e5dc82015-08-11 22:53:00 +00004718CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004719ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4720 if (type)
Alex Langfordbddab072019-08-13 19:40:36 +00004721 return CompilerType(
4722 this, GetQualType(type).getNonReferenceType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004723 return CompilerType();
4724}
4725
4726CompilerType ClangASTContext::CreateTypedefType(
4727 const CompilerType &type, const char *typedef_name,
4728 const CompilerDeclContext &compiler_decl_ctx) {
4729 if (type && typedef_name && typedef_name[0]) {
4730 ClangASTContext *ast =
4731 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4732 if (!ast)
4733 return CompilerType();
4734 clang::ASTContext *clang_ast = ast->getASTContext();
4735 clang::QualType qual_type(ClangUtil::GetQualType(type));
4736
4737 clang::DeclContext *decl_ctx =
4738 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4739 if (decl_ctx == nullptr)
4740 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4741
4742 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4743 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4744 &clang_ast->Idents.get(typedef_name),
4745 clang_ast->getTrivialTypeSourceInfo(qual_type));
4746
4747 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4748
Aleksandr Urakov709426b2018-09-10 08:08:43 +00004749 decl_ctx->addDecl(decl);
4750
Kate Stoneb9c1b512016-09-06 20:57:50 +00004751 // Get a uniqued clang::QualType for the typedef decl type
Alex Langfordbddab072019-08-13 19:40:36 +00004752 return CompilerType(ast, clang_ast->getTypedefType(decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004753 }
4754 return CompilerType();
4755}
4756
4757CompilerType
4758ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4759 if (type) {
4760 clang::QualType qual_type(GetQualType(type));
Alex Langfordbddab072019-08-13 19:40:36 +00004761 return CompilerType(
4762 this, qual_type.getTypePtr()->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004763 }
4764 return CompilerType();
4765}
4766
4767CompilerType
4768ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4769 if (type) {
4770 clang::QualType qual_type(GetQualType(type));
4771
4772 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4773 switch (type_class) {
4774 case clang::Type::ObjCObject:
4775 case clang::Type::ObjCInterface:
Alex Langfordbddab072019-08-13 19:40:36 +00004776 return CompilerType(this, getASTContext()
4777 ->getObjCObjectPointerType(qual_type)
4778 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004779
4780 default:
Alex Langfordbddab072019-08-13 19:40:36 +00004781 return CompilerType(
4782 this, getASTContext()->getPointerType(qual_type).getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00004783 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004784 }
4785 return CompilerType();
4786}
4787
4788CompilerType
4789ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4790 if (type)
4791 return CompilerType(this, getASTContext()
4792 ->getLValueReferenceType(GetQualType(type))
4793 .getAsOpaquePtr());
4794 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004795 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004796}
4797
Kate Stoneb9c1b512016-09-06 20:57:50 +00004798CompilerType
4799ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4800 if (type)
4801 return CompilerType(this, getASTContext()
4802 ->getRValueReferenceType(GetQualType(type))
4803 .getAsOpaquePtr());
4804 else
4805 return CompilerType();
4806}
4807
4808CompilerType
4809ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4810 if (type) {
4811 clang::QualType result(GetQualType(type));
4812 result.addConst();
4813 return CompilerType(this, result.getAsOpaquePtr());
4814 }
4815 return CompilerType();
4816}
4817
4818CompilerType
4819ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4820 if (type) {
4821 clang::QualType result(GetQualType(type));
4822 result.addVolatile();
4823 return CompilerType(this, result.getAsOpaquePtr());
4824 }
4825 return CompilerType();
4826}
4827
4828CompilerType
4829ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4830 if (type) {
4831 clang::QualType result(GetQualType(type));
4832 result.addRestrict();
4833 return CompilerType(this, result.getAsOpaquePtr());
4834 }
4835 return CompilerType();
4836}
4837
4838CompilerType
4839ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4840 const char *typedef_name,
4841 const CompilerDeclContext &compiler_decl_ctx) {
4842 if (type) {
4843 clang::ASTContext *clang_ast = getASTContext();
4844 clang::QualType qual_type(GetQualType(type));
4845
4846 clang::DeclContext *decl_ctx =
4847 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4848 if (decl_ctx == nullptr)
4849 decl_ctx = getASTContext()->getTranslationUnitDecl();
4850
4851 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4852 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4853 &clang_ast->Idents.get(typedef_name),
4854 clang_ast->getTrivialTypeSourceInfo(qual_type));
4855
4856 clang::TagDecl *tdecl = nullptr;
4857 if (!qual_type.isNull()) {
4858 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4859 tdecl = rt->getDecl();
4860 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4861 tdecl = et->getDecl();
4862 }
4863
4864 // Check whether this declaration is an anonymous struct, union, or enum,
Adrian Prantl05097242018-04-30 16:49:04 +00004865 // hidden behind a typedef. If so, we try to check whether we have a
4866 // typedef tag to attach to the original record declaration
Kate Stoneb9c1b512016-09-06 20:57:50 +00004867 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4868 tdecl->setTypedefNameForAnonDecl(decl);
4869
4870 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4871
4872 // Get a uniqued clang::QualType for the typedef decl type
4873 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4874 }
4875 return CompilerType();
4876}
4877
4878CompilerType
4879ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4880 if (type) {
4881 const clang::TypedefType *typedef_type =
4882 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4883 if (typedef_type)
Alex Langfordbddab072019-08-13 19:40:36 +00004884 return CompilerType(
4885 this, typedef_type->getDecl()->getUnderlyingType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004886 }
4887 return CompilerType();
4888}
Greg Claytond8d4a572015-08-11 21:38:15 +00004889
Greg Claytond8d4a572015-08-11 21:38:15 +00004890// Create related types using the current type's AST
Greg Claytond8d4a572015-08-11 21:38:15 +00004891
Kate Stoneb9c1b512016-09-06 20:57:50 +00004892CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
Raphael Isemannc502bae2019-11-20 12:40:08 +01004893 return ClangASTContext::GetBasicType(basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004894}
Greg Claytond8d4a572015-08-11 21:38:15 +00004895// Exploring the type
Greg Claytond8d4a572015-08-11 21:38:15 +00004896
Alex Langfordb482db62019-09-06 21:05:21 +00004897const llvm::fltSemantics &
4898ClangASTContext::GetFloatTypeSemantics(size_t byte_size) {
4899 if (auto *ast = getASTContext()) {
4900 const size_t bit_size = byte_size * 8;
4901 if (bit_size == ast->getTypeSize(ast->FloatTy))
4902 return ast->getFloatTypeSemantics(ast->FloatTy);
4903 else if (bit_size == ast->getTypeSize(ast->DoubleTy))
4904 return ast->getFloatTypeSemantics(ast->DoubleTy);
4905 else if (bit_size == ast->getTypeSize(ast->LongDoubleTy))
4906 return ast->getFloatTypeSemantics(ast->LongDoubleTy);
4907 else if (bit_size == ast->getTypeSize(ast->HalfTy))
4908 return ast->getFloatTypeSemantics(ast->HalfTy);
4909 }
4910 return llvm::APFloatBase::Bogus();
4911}
4912
Adrian Prantl2ee7b882019-01-16 21:19:20 +00004913Optional<uint64_t>
4914ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4915 ExecutionContextScope *exe_scope) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00004916 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00004917 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004918 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004919 switch (type_class) {
4920 case clang::Type::Record:
4921 if (GetCompleteType(type))
4922 return getASTContext()->getTypeSize(qual_type);
4923 else
Adrian Prantl2ee7b882019-01-16 21:19:20 +00004924 return None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004925 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004926
Kate Stoneb9c1b512016-09-06 20:57:50 +00004927 case clang::Type::ObjCInterface:
4928 case clang::Type::ObjCObject: {
4929 ExecutionContext exe_ctx(exe_scope);
4930 Process *process = exe_ctx.GetProcessPtr();
4931 if (process) {
Alex Langforde823bbe2019-06-10 20:53:23 +00004932 ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004933 if (objc_runtime) {
4934 uint64_t bit_size = 0;
4935 if (objc_runtime->GetTypeBitSize(
Alex Langfordbddab072019-08-13 19:40:36 +00004936 CompilerType(this, qual_type.getAsOpaquePtr()), bit_size))
Kate Stoneb9c1b512016-09-06 20:57:50 +00004937 return bit_size;
4938 }
4939 } else {
4940 static bool g_printed = false;
4941 if (!g_printed) {
4942 StreamString s;
4943 DumpTypeDescription(type, &s);
4944
4945 llvm::outs() << "warning: trying to determine the size of type ";
4946 llvm::outs() << s.GetString() << "\n";
4947 llvm::outs() << "without a valid ExecutionContext. this is not "
4948 "reliable. please file a bug against LLDB.\n";
4949 llvm::outs() << "backtrace:\n";
4950 llvm::sys::PrintStackTrace(llvm::outs());
4951 llvm::outs() << "\n";
4952 g_printed = true;
4953 }
4954 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004955 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004956 LLVM_FALLTHROUGH;
4957 default:
4958 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
4959 if (bit_size == 0) {
4960 if (qual_type->isIncompleteArrayType())
4961 return getASTContext()->getTypeSize(
4962 qual_type->getArrayElementTypeNoTypeQual()
4963 ->getCanonicalTypeUnqualified());
4964 }
4965 if (qual_type->isObjCObjectOrInterfaceType())
4966 return bit_size +
4967 getASTContext()->getTypeSize(
4968 getASTContext()->ObjCBuiltinClassTy);
Adrian Prantl2ee7b882019-01-16 21:19:20 +00004969 // Function types actually have a size of 0, that's not an error.
4970 if (qual_type->isFunctionProtoType())
4971 return bit_size;
4972 if (bit_size)
4973 return bit_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004974 }
4975 }
Adrian Prantl2ee7b882019-01-16 21:19:20 +00004976 return None;
Greg Claytond8d4a572015-08-11 21:38:15 +00004977}
4978
Davide Italiano36f13e42019-08-12 20:03:19 +00004979llvm::Optional<size_t>
Davide Italiano7f9bbe02019-08-12 21:49:54 +00004980ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type,
4981 ExecutionContextScope *exe_scope) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00004982 if (GetCompleteType(type))
4983 return getASTContext()->getTypeAlign(GetQualType(type));
Davide Italiano36f13e42019-08-12 20:03:19 +00004984 return {};
Kate Stoneb9c1b512016-09-06 20:57:50 +00004985}
4986
4987lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
4988 uint64_t &count) {
4989 if (!type)
4990 return lldb::eEncodingInvalid;
4991
4992 count = 1;
4993 clang::QualType qual_type(GetCanonicalQualType(type));
4994
4995 switch (qual_type->getTypeClass()) {
4996 case clang::Type::UnaryTransform:
4997 break;
4998
4999 case clang::Type::FunctionNoProto:
5000 case clang::Type::FunctionProto:
5001 break;
5002
5003 case clang::Type::IncompleteArray:
5004 case clang::Type::VariableArray:
5005 break;
5006
5007 case clang::Type::ConstantArray:
5008 break;
5009
Fangrui Song8f284882018-07-13 22:40:40 +00005010 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005011 case clang::Type::ExtVector:
5012 case clang::Type::Vector:
5013 // TODO: Set this to more than one???
5014 break;
5015
5016 case clang::Type::Builtin:
5017 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5018 case clang::BuiltinType::Void:
5019 break;
5020
5021 case clang::BuiltinType::Bool:
5022 case clang::BuiltinType::Char_S:
5023 case clang::BuiltinType::SChar:
5024 case clang::BuiltinType::WChar_S:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005025 case clang::BuiltinType::Short:
5026 case clang::BuiltinType::Int:
5027 case clang::BuiltinType::Long:
5028 case clang::BuiltinType::LongLong:
5029 case clang::BuiltinType::Int128:
5030 return lldb::eEncodingSint;
5031
5032 case clang::BuiltinType::Char_U:
5033 case clang::BuiltinType::UChar:
5034 case clang::BuiltinType::WChar_U:
Richard Smith51d12d82018-05-02 02:43:22 +00005035 case clang::BuiltinType::Char8:
5036 case clang::BuiltinType::Char16:
5037 case clang::BuiltinType::Char32:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005038 case clang::BuiltinType::UShort:
5039 case clang::BuiltinType::UInt:
5040 case clang::BuiltinType::ULong:
5041 case clang::BuiltinType::ULongLong:
5042 case clang::BuiltinType::UInt128:
5043 return lldb::eEncodingUint;
5044
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005045 // Fixed point types. Note that they are currently ignored.
5046 case clang::BuiltinType::ShortAccum:
5047 case clang::BuiltinType::Accum:
5048 case clang::BuiltinType::LongAccum:
5049 case clang::BuiltinType::UShortAccum:
5050 case clang::BuiltinType::UAccum:
5051 case clang::BuiltinType::ULongAccum:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005052 case clang::BuiltinType::ShortFract:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005053 case clang::BuiltinType::Fract:
5054 case clang::BuiltinType::LongFract:
5055 case clang::BuiltinType::UShortFract:
5056 case clang::BuiltinType::UFract:
5057 case clang::BuiltinType::ULongFract:
5058 case clang::BuiltinType::SatShortAccum:
5059 case clang::BuiltinType::SatAccum:
5060 case clang::BuiltinType::SatLongAccum:
5061 case clang::BuiltinType::SatUShortAccum:
5062 case clang::BuiltinType::SatUAccum:
5063 case clang::BuiltinType::SatULongAccum:
5064 case clang::BuiltinType::SatShortFract:
5065 case clang::BuiltinType::SatFract:
5066 case clang::BuiltinType::SatLongFract:
5067 case clang::BuiltinType::SatUShortFract:
5068 case clang::BuiltinType::SatUFract:
5069 case clang::BuiltinType::SatULongFract:
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005070 break;
5071
Kate Stoneb9c1b512016-09-06 20:57:50 +00005072 case clang::BuiltinType::Half:
5073 case clang::BuiltinType::Float:
Ted Woodward4355c7c2017-09-20 19:16:53 +00005074 case clang::BuiltinType::Float16:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005075 case clang::BuiltinType::Float128:
5076 case clang::BuiltinType::Double:
5077 case clang::BuiltinType::LongDouble:
5078 return lldb::eEncodingIEEE754;
5079
5080 case clang::BuiltinType::ObjCClass:
5081 case clang::BuiltinType::ObjCId:
5082 case clang::BuiltinType::ObjCSel:
5083 return lldb::eEncodingUint;
5084
5085 case clang::BuiltinType::NullPtr:
5086 return lldb::eEncodingUint;
5087
5088 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5089 case clang::BuiltinType::Kind::BoundMember:
5090 case clang::BuiltinType::Kind::BuiltinFn:
5091 case clang::BuiltinType::Kind::Dependent:
5092 case clang::BuiltinType::Kind::OCLClkEvent:
5093 case clang::BuiltinType::Kind::OCLEvent:
5094 case clang::BuiltinType::Kind::OCLImage1dRO:
5095 case clang::BuiltinType::Kind::OCLImage1dWO:
5096 case clang::BuiltinType::Kind::OCLImage1dRW:
5097 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5098 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5099 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5100 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5101 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5102 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5103 case clang::BuiltinType::Kind::OCLImage2dRO:
5104 case clang::BuiltinType::Kind::OCLImage2dWO:
5105 case clang::BuiltinType::Kind::OCLImage2dRW:
5106 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5107 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5108 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5109 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5110 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5111 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5112 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5113 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5114 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5115 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5116 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5117 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5118 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5119 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5120 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5121 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5122 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5123 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5124 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5125 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5126 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5127 case clang::BuiltinType::Kind::OCLImage3dRO:
5128 case clang::BuiltinType::Kind::OCLImage3dWO:
5129 case clang::BuiltinType::Kind::OCLImage3dRW:
5130 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005131 case clang::BuiltinType::Kind::OCLReserveID:
5132 case clang::BuiltinType::Kind::OCLSampler:
5133 case clang::BuiltinType::Kind::OMPArraySection:
5134 case clang::BuiltinType::Kind::Overload:
5135 case clang::BuiltinType::Kind::PseudoObject:
5136 case clang::BuiltinType::Kind::UnknownAny:
5137 break;
Jorge Gorbe Moyaa6e6c182018-11-08 22:04:58 +00005138
5139 case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload:
5140 case clang::BuiltinType::OCLIntelSubgroupAVCImePayload:
5141 case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload:
5142 case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload:
5143 case clang::BuiltinType::OCLIntelSubgroupAVCMceResult:
5144 case clang::BuiltinType::OCLIntelSubgroupAVCImeResult:
5145 case clang::BuiltinType::OCLIntelSubgroupAVCRefResult:
5146 case clang::BuiltinType::OCLIntelSubgroupAVCSicResult:
5147 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleRefStreamout:
5148 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualRefStreamout:
5149 case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleRefStreamin:
5150 case clang::BuiltinType::OCLIntelSubgroupAVCImeDualRefStreamin:
5151 break;
Raphael Isemann339b5d12019-08-09 09:58:47 +00005152
5153 case clang::BuiltinType::SveBool:
5154 case clang::BuiltinType::SveInt8:
5155 case clang::BuiltinType::SveInt16:
5156 case clang::BuiltinType::SveInt32:
5157 case clang::BuiltinType::SveInt64:
5158 case clang::BuiltinType::SveUint8:
5159 case clang::BuiltinType::SveUint16:
5160 case clang::BuiltinType::SveUint32:
5161 case clang::BuiltinType::SveUint64:
5162 case clang::BuiltinType::SveFloat16:
5163 case clang::BuiltinType::SveFloat32:
5164 case clang::BuiltinType::SveFloat64:
5165 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005166 }
5167 break;
Adrian Prantl05097242018-04-30 16:49:04 +00005168 // All pointer types are represented as unsigned integer encodings. We may
5169 // nee to add a eEncodingPointer if we ever need to know the difference
Kate Stoneb9c1b512016-09-06 20:57:50 +00005170 case clang::Type::ObjCObjectPointer:
5171 case clang::Type::BlockPointer:
5172 case clang::Type::Pointer:
5173 case clang::Type::LValueReference:
5174 case clang::Type::RValueReference:
5175 case clang::Type::MemberPointer:
5176 return lldb::eEncodingUint;
5177 case clang::Type::Complex: {
5178 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5179 if (qual_type->isComplexType())
5180 encoding = lldb::eEncodingIEEE754;
5181 else {
5182 const clang::ComplexType *complex_type =
5183 qual_type->getAsComplexIntegerType();
5184 if (complex_type)
Alex Langfordbddab072019-08-13 19:40:36 +00005185 encoding =
5186 CompilerType(this, complex_type->getElementType().getAsOpaquePtr())
5187 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005188 else
5189 encoding = lldb::eEncodingSint;
5190 }
5191 count = 2;
5192 return encoding;
5193 }
5194
5195 case clang::Type::ObjCInterface:
5196 break;
5197 case clang::Type::Record:
5198 break;
5199 case clang::Type::Enum:
5200 return lldb::eEncodingSint;
5201 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00005202 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
5203 ->getDecl()
5204 ->getUnderlyingType()
5205 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005206 .GetEncoding(count);
5207
5208 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00005209 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
5210 ->getDeducedType()
5211 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005212 .GetEncoding(count);
5213
5214 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00005215 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
5216 ->getNamedType()
5217 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005218 .GetEncoding(count);
5219
5220 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00005221 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
5222 ->desugar()
5223 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005224 .GetEncoding(count);
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005225 case clang::Type::TypeOfExpr:
Alex Langfordbddab072019-08-13 19:40:36 +00005226 return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type)
5227 ->getUnderlyingExpr()
5228 ->getType()
5229 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005230 .GetEncoding(count);
5231 case clang::Type::TypeOf:
Alex Langfordbddab072019-08-13 19:40:36 +00005232 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
5233 ->getUnderlyingType()
5234 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005235 .GetEncoding(count);
5236 case clang::Type::Decltype:
Alex Langfordbddab072019-08-13 19:40:36 +00005237 return CompilerType(this, llvm::cast<clang::DecltypeType>(qual_type)
5238 ->getUnderlyingType()
5239 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005240 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005241 case clang::Type::DependentSizedArray:
5242 case clang::Type::DependentSizedExtVector:
5243 case clang::Type::UnresolvedUsing:
5244 case clang::Type::Attributed:
5245 case clang::Type::TemplateTypeParm:
5246 case clang::Type::SubstTemplateTypeParm:
5247 case clang::Type::SubstTemplateTypeParmPack:
5248 case clang::Type::InjectedClassName:
5249 case clang::Type::DependentName:
5250 case clang::Type::DependentTemplateSpecialization:
5251 case clang::Type::PackExpansion:
5252 case clang::Type::ObjCObject:
5253
Kate Stoneb9c1b512016-09-06 20:57:50 +00005254 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005255 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005256 case clang::Type::Atomic:
5257 case clang::Type::Adjusted:
5258 case clang::Type::Pipe:
5259 break;
5260
5261 // pointer type decayed from an array or function type.
5262 case clang::Type::Decayed:
5263 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005264 case clang::Type::ObjCTypeParam:
5265 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005266
5267 case clang::Type::DependentAddressSpace:
5268 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00005269 case clang::Type::MacroQualified:
5270 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005271 }
5272 count = 0;
5273 return lldb::eEncodingInvalid;
5274}
5275
5276lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5277 if (!type)
5278 return lldb::eFormatDefault;
5279
5280 clang::QualType qual_type(GetCanonicalQualType(type));
5281
5282 switch (qual_type->getTypeClass()) {
5283 case clang::Type::UnaryTransform:
5284 break;
5285
5286 case clang::Type::FunctionNoProto:
5287 case clang::Type::FunctionProto:
5288 break;
5289
5290 case clang::Type::IncompleteArray:
5291 case clang::Type::VariableArray:
5292 break;
5293
5294 case clang::Type::ConstantArray:
5295 return lldb::eFormatVoid; // no value
5296
Fangrui Song8f284882018-07-13 22:40:40 +00005297 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005298 case clang::Type::ExtVector:
5299 case clang::Type::Vector:
5300 break;
5301
5302 case clang::Type::Builtin:
5303 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5304 // default: assert(0 && "Unknown builtin type!");
5305 case clang::BuiltinType::UnknownAny:
5306 case clang::BuiltinType::Void:
5307 case clang::BuiltinType::BoundMember:
5308 break;
5309
5310 case clang::BuiltinType::Bool:
5311 return lldb::eFormatBoolean;
5312 case clang::BuiltinType::Char_S:
5313 case clang::BuiltinType::SChar:
5314 case clang::BuiltinType::WChar_S:
5315 case clang::BuiltinType::Char_U:
5316 case clang::BuiltinType::UChar:
5317 case clang::BuiltinType::WChar_U:
5318 return lldb::eFormatChar;
5319 case clang::BuiltinType::Char16:
5320 return lldb::eFormatUnicode16;
5321 case clang::BuiltinType::Char32:
5322 return lldb::eFormatUnicode32;
5323 case clang::BuiltinType::UShort:
5324 return lldb::eFormatUnsigned;
5325 case clang::BuiltinType::Short:
5326 return lldb::eFormatDecimal;
5327 case clang::BuiltinType::UInt:
5328 return lldb::eFormatUnsigned;
5329 case clang::BuiltinType::Int:
5330 return lldb::eFormatDecimal;
5331 case clang::BuiltinType::ULong:
5332 return lldb::eFormatUnsigned;
5333 case clang::BuiltinType::Long:
5334 return lldb::eFormatDecimal;
5335 case clang::BuiltinType::ULongLong:
5336 return lldb::eFormatUnsigned;
5337 case clang::BuiltinType::LongLong:
5338 return lldb::eFormatDecimal;
5339 case clang::BuiltinType::UInt128:
5340 return lldb::eFormatUnsigned;
5341 case clang::BuiltinType::Int128:
5342 return lldb::eFormatDecimal;
5343 case clang::BuiltinType::Half:
5344 case clang::BuiltinType::Float:
5345 case clang::BuiltinType::Double:
5346 case clang::BuiltinType::LongDouble:
5347 return lldb::eFormatFloat;
5348 default:
5349 return lldb::eFormatHex;
5350 }
5351 break;
5352 case clang::Type::ObjCObjectPointer:
5353 return lldb::eFormatHex;
5354 case clang::Type::BlockPointer:
5355 return lldb::eFormatHex;
5356 case clang::Type::Pointer:
5357 return lldb::eFormatHex;
5358 case clang::Type::LValueReference:
5359 case clang::Type::RValueReference:
5360 return lldb::eFormatHex;
5361 case clang::Type::MemberPointer:
5362 break;
5363 case clang::Type::Complex: {
5364 if (qual_type->isComplexType())
5365 return lldb::eFormatComplex;
5366 else
5367 return lldb::eFormatComplexInteger;
5368 }
5369 case clang::Type::ObjCInterface:
5370 break;
5371 case clang::Type::Record:
5372 break;
5373 case clang::Type::Enum:
5374 return lldb::eFormatEnum;
5375 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00005376 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
5377 ->getDecl()
5378 ->getUnderlyingType()
5379 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005380 .GetFormat();
5381 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00005382 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
5383 ->desugar()
5384 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005385 .GetFormat();
5386 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00005387 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
5388 ->desugar()
5389 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005390 .GetFormat();
5391 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00005392 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
5393 ->getNamedType()
5394 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005395 .GetFormat();
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005396 case clang::Type::TypeOfExpr:
Alex Langfordbddab072019-08-13 19:40:36 +00005397 return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type)
5398 ->getUnderlyingExpr()
5399 ->getType()
5400 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005401 .GetFormat();
5402 case clang::Type::TypeOf:
Alex Langfordbddab072019-08-13 19:40:36 +00005403 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
5404 ->getUnderlyingType()
5405 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005406 .GetFormat();
5407 case clang::Type::Decltype:
Alex Langfordbddab072019-08-13 19:40:36 +00005408 return CompilerType(this, llvm::cast<clang::DecltypeType>(qual_type)
5409 ->getUnderlyingType()
5410 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005411 .GetFormat();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005412 case clang::Type::DependentSizedArray:
5413 case clang::Type::DependentSizedExtVector:
5414 case clang::Type::UnresolvedUsing:
5415 case clang::Type::Attributed:
5416 case clang::Type::TemplateTypeParm:
5417 case clang::Type::SubstTemplateTypeParm:
5418 case clang::Type::SubstTemplateTypeParmPack:
5419 case clang::Type::InjectedClassName:
5420 case clang::Type::DependentName:
5421 case clang::Type::DependentTemplateSpecialization:
5422 case clang::Type::PackExpansion:
5423 case clang::Type::ObjCObject:
5424
Kate Stoneb9c1b512016-09-06 20:57:50 +00005425 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005426 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005427 case clang::Type::Atomic:
5428 case clang::Type::Adjusted:
5429 case clang::Type::Pipe:
5430 break;
5431
5432 // pointer type decayed from an array or function type.
5433 case clang::Type::Decayed:
5434 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005435 case clang::Type::ObjCTypeParam:
5436 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005437
5438 case clang::Type::DependentAddressSpace:
5439 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00005440 case clang::Type::MacroQualified:
5441 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005442 }
5443 // We don't know hot to display this type...
5444 return lldb::eFormatBytes;
5445}
5446
5447static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5448 bool check_superclass) {
5449 while (class_interface_decl) {
5450 if (class_interface_decl->ivar_size() > 0)
5451 return true;
5452
5453 if (check_superclass)
5454 class_interface_decl = class_interface_decl->getSuperClass();
5455 else
5456 break;
5457 }
5458 return false;
5459}
5460
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00005461static Optional<SymbolFile::ArrayInfo>
Adrian Prantleca07c52018-11-05 20:49:07 +00005462GetDynamicArrayInfo(ClangASTContext &ast, SymbolFile *sym_file,
5463 clang::QualType qual_type,
5464 const ExecutionContext *exe_ctx) {
5465 if (qual_type->isIncompleteArrayType())
5466 if (auto *metadata = ast.GetMetadata(qual_type.getAsOpaquePtr()))
Pavel Labathffec31e2018-12-28 13:34:44 +00005467 return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(),
5468 exe_ctx);
Adrian Prantleca07c52018-11-05 20:49:07 +00005469 return llvm::None;
5470}
5471
Kate Stoneb9c1b512016-09-06 20:57:50 +00005472uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
Adrian Prantleca07c52018-11-05 20:49:07 +00005473 bool omit_empty_base_classes,
5474 const ExecutionContext *exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005475 if (!type)
5476 return 0;
5477
5478 uint32_t num_children = 0;
5479 clang::QualType qual_type(GetQualType(type));
5480 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5481 switch (type_class) {
5482 case clang::Type::Builtin:
5483 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5484 case clang::BuiltinType::ObjCId: // child is Class
5485 case clang::BuiltinType::ObjCClass: // child is Class
5486 num_children = 1;
5487 break;
5488
5489 default:
5490 break;
5491 }
5492 break;
5493
5494 case clang::Type::Complex:
5495 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005496 case clang::Type::Record:
5497 if (GetCompleteQualType(getASTContext(), qual_type)) {
5498 const clang::RecordType *record_type =
5499 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5500 const clang::RecordDecl *record_decl = record_type->getDecl();
5501 assert(record_decl);
5502 const clang::CXXRecordDecl *cxx_record_decl =
5503 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5504 if (cxx_record_decl) {
5505 if (omit_empty_base_classes) {
Adrian Prantl05097242018-04-30 16:49:04 +00005506 // Check each base classes to see if it or any of its base classes
5507 // contain any fields. This can help limit the noise in variable
5508 // views by not having to show base classes that contain no members.
Kate Stoneb9c1b512016-09-06 20:57:50 +00005509 clang::CXXRecordDecl::base_class_const_iterator base_class,
5510 base_class_end;
5511 for (base_class = cxx_record_decl->bases_begin(),
5512 base_class_end = cxx_record_decl->bases_end();
5513 base_class != base_class_end; ++base_class) {
5514 const clang::CXXRecordDecl *base_class_decl =
5515 llvm::cast<clang::CXXRecordDecl>(
5516 base_class->getType()
5517 ->getAs<clang::RecordType>()
5518 ->getDecl());
5519
5520 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00005521 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00005522 continue;
5523
5524 num_children++;
5525 }
5526 } else {
5527 // Include all base classes
5528 num_children += cxx_record_decl->getNumBases();
5529 }
5530 }
5531 clang::RecordDecl::field_iterator field, field_end;
5532 for (field = record_decl->field_begin(),
5533 field_end = record_decl->field_end();
5534 field != field_end; ++field)
5535 ++num_children;
5536 }
5537 break;
5538
5539 case clang::Type::ObjCObject:
5540 case clang::Type::ObjCInterface:
5541 if (GetCompleteQualType(getASTContext(), qual_type)) {
5542 const clang::ObjCObjectType *objc_class_type =
5543 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5544 assert(objc_class_type);
5545 if (objc_class_type) {
5546 clang::ObjCInterfaceDecl *class_interface_decl =
5547 objc_class_type->getInterface();
5548
5549 if (class_interface_decl) {
5550
5551 clang::ObjCInterfaceDecl *superclass_interface_decl =
5552 class_interface_decl->getSuperClass();
5553 if (superclass_interface_decl) {
5554 if (omit_empty_base_classes) {
5555 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5556 ++num_children;
5557 } else
5558 ++num_children;
5559 }
5560
5561 num_children += class_interface_decl->ivar_size();
5562 }
5563 }
5564 }
5565 break;
5566
5567 case clang::Type::ObjCObjectPointer: {
5568 const clang::ObjCObjectPointerType *pointer_type =
5569 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5570 clang::QualType pointee_type = pointer_type->getPointeeType();
5571 uint32_t num_pointee_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005572 CompilerType(this, pointee_type.getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005573 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005574 // If this type points to a simple type, then it has 1 child
5575 if (num_pointee_children == 0)
5576 num_children = 1;
5577 else
5578 num_children = num_pointee_children;
5579 } break;
5580
5581 case clang::Type::Vector:
5582 case clang::Type::ExtVector:
5583 num_children =
5584 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5585 break;
5586
5587 case clang::Type::ConstantArray:
5588 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5589 ->getSize()
5590 .getLimitedValue();
5591 break;
Adrian Prantleca07c52018-11-05 20:49:07 +00005592 case clang::Type::IncompleteArray:
5593 if (auto array_info =
5594 GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx))
5595 // Only 1-dimensional arrays are supported.
5596 num_children = array_info->element_orders.size()
5597 ? array_info->element_orders.back()
5598 : 0;
5599 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005600
5601 case clang::Type::Pointer: {
5602 const clang::PointerType *pointer_type =
5603 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5604 clang::QualType pointee_type(pointer_type->getPointeeType());
5605 uint32_t num_pointee_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005606 CompilerType(this, pointee_type.getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005607 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005608 if (num_pointee_children == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00005609 // We have a pointer to a pointee type that claims it has no children. We
5610 // will want to look at
Kate Stoneb9c1b512016-09-06 20:57:50 +00005611 num_children = GetNumPointeeChildren(pointee_type);
5612 } else
5613 num_children = num_pointee_children;
5614 } break;
5615
5616 case clang::Type::LValueReference:
5617 case clang::Type::RValueReference: {
5618 const clang::ReferenceType *reference_type =
5619 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5620 clang::QualType pointee_type = reference_type->getPointeeType();
5621 uint32_t num_pointee_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005622 CompilerType(this, pointee_type.getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005623 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005624 // If this type points to a simple type, then it has 1 child
5625 if (num_pointee_children == 0)
5626 num_children = 1;
5627 else
5628 num_children = num_pointee_children;
5629 } break;
5630
5631 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00005632 num_children = CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
Kate Stoneb9c1b512016-09-06 20:57:50 +00005633 ->getDecl()
Alex Langfordbddab072019-08-13 19:40:36 +00005634 ->getUnderlyingType()
5635 .getAsOpaquePtr())
5636 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005637 break;
5638
5639 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00005640 num_children = CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
5641 ->getDeducedType()
5642 .getAsOpaquePtr())
5643 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005644 break;
5645
5646 case clang::Type::Elaborated:
5647 num_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005648 CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
5649 ->getNamedType()
5650 .getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005651 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005652 break;
5653
5654 case clang::Type::Paren:
5655 num_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005656 CompilerType(
5657 this,
5658 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005659 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005660 break;
5661 default:
5662 break;
5663 }
5664 return num_children;
5665}
5666
Adrian Prantl0e4c4822019-03-06 21:22:25 +00005667CompilerType ClangASTContext::GetBuiltinTypeByName(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005668 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005669}
5670
Greg Claytond8d4a572015-08-11 21:38:15 +00005671lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005672ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5673 if (type) {
5674 clang::QualType qual_type(GetQualType(type));
5675 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5676 if (type_class == clang::Type::Builtin) {
5677 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5678 case clang::BuiltinType::Void:
5679 return eBasicTypeVoid;
5680 case clang::BuiltinType::Bool:
5681 return eBasicTypeBool;
5682 case clang::BuiltinType::Char_S:
5683 return eBasicTypeSignedChar;
5684 case clang::BuiltinType::Char_U:
5685 return eBasicTypeUnsignedChar;
5686 case clang::BuiltinType::Char16:
5687 return eBasicTypeChar16;
5688 case clang::BuiltinType::Char32:
5689 return eBasicTypeChar32;
5690 case clang::BuiltinType::UChar:
5691 return eBasicTypeUnsignedChar;
5692 case clang::BuiltinType::SChar:
5693 return eBasicTypeSignedChar;
5694 case clang::BuiltinType::WChar_S:
5695 return eBasicTypeSignedWChar;
5696 case clang::BuiltinType::WChar_U:
5697 return eBasicTypeUnsignedWChar;
5698 case clang::BuiltinType::Short:
5699 return eBasicTypeShort;
5700 case clang::BuiltinType::UShort:
5701 return eBasicTypeUnsignedShort;
5702 case clang::BuiltinType::Int:
5703 return eBasicTypeInt;
5704 case clang::BuiltinType::UInt:
5705 return eBasicTypeUnsignedInt;
5706 case clang::BuiltinType::Long:
5707 return eBasicTypeLong;
5708 case clang::BuiltinType::ULong:
5709 return eBasicTypeUnsignedLong;
5710 case clang::BuiltinType::LongLong:
5711 return eBasicTypeLongLong;
5712 case clang::BuiltinType::ULongLong:
5713 return eBasicTypeUnsignedLongLong;
5714 case clang::BuiltinType::Int128:
5715 return eBasicTypeInt128;
5716 case clang::BuiltinType::UInt128:
5717 return eBasicTypeUnsignedInt128;
5718
5719 case clang::BuiltinType::Half:
5720 return eBasicTypeHalf;
5721 case clang::BuiltinType::Float:
5722 return eBasicTypeFloat;
5723 case clang::BuiltinType::Double:
5724 return eBasicTypeDouble;
5725 case clang::BuiltinType::LongDouble:
5726 return eBasicTypeLongDouble;
5727
5728 case clang::BuiltinType::NullPtr:
5729 return eBasicTypeNullPtr;
5730 case clang::BuiltinType::ObjCId:
5731 return eBasicTypeObjCID;
5732 case clang::BuiltinType::ObjCClass:
5733 return eBasicTypeObjCClass;
5734 case clang::BuiltinType::ObjCSel:
5735 return eBasicTypeObjCSel;
5736 default:
5737 return eBasicTypeOther;
5738 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005739 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005740 }
5741 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005742}
5743
Kate Stoneb9c1b512016-09-06 20:57:50 +00005744void ClangASTContext::ForEachEnumerator(
5745 lldb::opaque_compiler_type_t type,
5746 std::function<bool(const CompilerType &integer_type,
Adrian Prantl0e4c4822019-03-06 21:22:25 +00005747 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00005748 const llvm::APSInt &value)> const &callback) {
5749 const clang::EnumType *enum_type =
5750 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5751 if (enum_type) {
5752 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5753 if (enum_decl) {
5754 CompilerType integer_type(this,
5755 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005756
Kate Stoneb9c1b512016-09-06 20:57:50 +00005757 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5758 for (enum_pos = enum_decl->enumerator_begin(),
5759 enum_end_pos = enum_decl->enumerator_end();
5760 enum_pos != enum_end_pos; ++enum_pos) {
5761 ConstString name(enum_pos->getNameAsString().c_str());
5762 if (!callback(integer_type, name, enum_pos->getInitVal()))
5763 break;
5764 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005765 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005766 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005767}
5768
Greg Claytond8d4a572015-08-11 21:38:15 +00005769#pragma mark Aggregate Types
5770
Kate Stoneb9c1b512016-09-06 20:57:50 +00005771uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5772 if (!type)
5773 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005774
Kate Stoneb9c1b512016-09-06 20:57:50 +00005775 uint32_t count = 0;
5776 clang::QualType qual_type(GetCanonicalQualType(type));
5777 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5778 switch (type_class) {
5779 case clang::Type::Record:
5780 if (GetCompleteType(type)) {
5781 const clang::RecordType *record_type =
5782 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5783 if (record_type) {
5784 clang::RecordDecl *record_decl = record_type->getDecl();
5785 if (record_decl) {
5786 uint32_t field_idx = 0;
5787 clang::RecordDecl::field_iterator field, field_end;
5788 for (field = record_decl->field_begin(),
5789 field_end = record_decl->field_end();
5790 field != field_end; ++field)
5791 ++field_idx;
5792 count = field_idx;
5793 }
5794 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005795 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005796 break;
5797
5798 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00005799 count = CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
5800 ->getDecl()
5801 ->getUnderlyingType()
5802 .getAsOpaquePtr())
5803 .GetNumFields();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005804 break;
5805
5806 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00005807 count = CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
5808 ->getDeducedType()
5809 .getAsOpaquePtr())
5810 .GetNumFields();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005811 break;
5812
5813 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00005814 count = CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
5815 ->getNamedType()
5816 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005817 .GetNumFields();
5818 break;
5819
5820 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00005821 count =
5822 CompilerType(
5823 this,
5824 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())
5825 .GetNumFields();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005826 break;
5827
Sean Callananf9c622a2016-09-30 18:44:43 +00005828 case clang::Type::ObjCObjectPointer: {
5829 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005830 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005831 const clang::ObjCInterfaceType *objc_interface_type =
5832 objc_class_type->getInterfaceType();
5833 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005834 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5835 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005836 clang::ObjCInterfaceDecl *class_interface_decl =
5837 objc_interface_type->getDecl();
5838 if (class_interface_decl) {
5839 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005840 }
5841 }
5842 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005843 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005844
5845 case clang::Type::ObjCObject:
5846 case clang::Type::ObjCInterface:
5847 if (GetCompleteType(type)) {
5848 const clang::ObjCObjectType *objc_class_type =
5849 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5850 if (objc_class_type) {
5851 clang::ObjCInterfaceDecl *class_interface_decl =
5852 objc_class_type->getInterface();
5853
5854 if (class_interface_decl)
5855 count = class_interface_decl->ivar_size();
5856 }
5857 }
5858 break;
5859
5860 default:
5861 break;
5862 }
5863 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005864}
5865
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005866static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005867GetObjCFieldAtIndex(clang::ASTContext *ast,
5868 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5869 std::string &name, uint64_t *bit_offset_ptr,
5870 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5871 if (class_interface_decl) {
5872 if (idx < (class_interface_decl->ivar_size())) {
5873 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5874 ivar_end = class_interface_decl->ivar_end();
5875 uint32_t ivar_idx = 0;
5876
5877 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5878 ++ivar_pos, ++ivar_idx) {
5879 if (ivar_idx == idx) {
5880 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5881
5882 clang::QualType ivar_qual_type(ivar_decl->getType());
5883
5884 name.assign(ivar_decl->getNameAsString());
5885
5886 if (bit_offset_ptr) {
5887 const clang::ASTRecordLayout &interface_layout =
5888 ast->getASTObjCInterfaceLayout(class_interface_decl);
5889 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5890 }
5891
5892 const bool is_bitfield = ivar_pos->isBitField();
5893
5894 if (bitfield_bit_size_ptr) {
5895 *bitfield_bit_size_ptr = 0;
5896
5897 if (is_bitfield && ast) {
5898 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00005899 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005900 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00005901 bitfield_bit_size_expr->EvaluateAsInt(result, *ast)) {
5902 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005903 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5904 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005905 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005906 }
5907 if (is_bitfield_ptr)
5908 *is_bitfield_ptr = is_bitfield;
5909
5910 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005911 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005912 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005913 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005914 }
5915 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005916}
5917
Kate Stoneb9c1b512016-09-06 20:57:50 +00005918CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5919 size_t idx, std::string &name,
5920 uint64_t *bit_offset_ptr,
5921 uint32_t *bitfield_bit_size_ptr,
5922 bool *is_bitfield_ptr) {
5923 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005924 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005925
5926 clang::QualType qual_type(GetCanonicalQualType(type));
5927 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5928 switch (type_class) {
5929 case clang::Type::Record:
5930 if (GetCompleteType(type)) {
5931 const clang::RecordType *record_type =
5932 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5933 const clang::RecordDecl *record_decl = record_type->getDecl();
5934 uint32_t field_idx = 0;
5935 clang::RecordDecl::field_iterator field, field_end;
5936 for (field = record_decl->field_begin(),
5937 field_end = record_decl->field_end();
5938 field != field_end; ++field, ++field_idx) {
5939 if (idx == field_idx) {
5940 // Print the member type if requested
5941 // Print the member name and equal sign
5942 name.assign(field->getNameAsString());
5943
5944 // Figure out the type byte size (field_type_info.first) and
5945 // alignment (field_type_info.second) from the AST context.
5946 if (bit_offset_ptr) {
5947 const clang::ASTRecordLayout &record_layout =
5948 getASTContext()->getASTRecordLayout(record_decl);
5949 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5950 }
5951
5952 const bool is_bitfield = field->isBitField();
5953
5954 if (bitfield_bit_size_ptr) {
5955 *bitfield_bit_size_ptr = 0;
5956
5957 if (is_bitfield) {
5958 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00005959 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005960 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00005961 bitfield_bit_size_expr->EvaluateAsInt(result,
Kate Stoneb9c1b512016-09-06 20:57:50 +00005962 *getASTContext())) {
Hans Wennborg30ce9622018-11-28 14:30:18 +00005963 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005964 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5965 }
5966 }
5967 }
5968 if (is_bitfield_ptr)
5969 *is_bitfield_ptr = is_bitfield;
5970
Alex Langfordbddab072019-08-13 19:40:36 +00005971 return CompilerType(this, field->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00005972 }
5973 }
5974 }
5975 break;
5976
Sean Callananf9c622a2016-09-30 18:44:43 +00005977 case clang::Type::ObjCObjectPointer: {
5978 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005979 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005980 const clang::ObjCInterfaceType *objc_interface_type =
5981 objc_class_type->getInterfaceType();
5982 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005983 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5984 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005985 clang::ObjCInterfaceDecl *class_interface_decl =
5986 objc_interface_type->getDecl();
5987 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005988 return CompilerType(
5989 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5990 idx, name, bit_offset_ptr,
5991 bitfield_bit_size_ptr, is_bitfield_ptr));
5992 }
5993 }
5994 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005995 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005996
5997 case clang::Type::ObjCObject:
5998 case clang::Type::ObjCInterface:
5999 if (GetCompleteType(type)) {
6000 const clang::ObjCObjectType *objc_class_type =
6001 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6002 assert(objc_class_type);
6003 if (objc_class_type) {
6004 clang::ObjCInterfaceDecl *class_interface_decl =
6005 objc_class_type->getInterface();
6006 return CompilerType(
6007 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6008 idx, name, bit_offset_ptr,
6009 bitfield_bit_size_ptr, is_bitfield_ptr));
6010 }
6011 }
6012 break;
6013
6014 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00006015 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
6016 ->getDecl()
6017 ->getUnderlyingType()
6018 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00006019 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6020 is_bitfield_ptr);
6021
6022 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00006023 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
6024 ->getDeducedType()
6025 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00006026 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6027 is_bitfield_ptr);
6028
6029 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00006030 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
6031 ->getNamedType()
6032 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00006033 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6034 is_bitfield_ptr);
6035
6036 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00006037 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
6038 ->desugar()
6039 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00006040 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6041 is_bitfield_ptr);
6042
6043 default:
6044 break;
6045 }
6046 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006047}
6048
Greg Clayton99558cc42015-08-24 23:46:31 +00006049uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006050ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
6051 uint32_t count = 0;
6052 clang::QualType qual_type(GetCanonicalQualType(type));
6053 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6054 switch (type_class) {
6055 case clang::Type::Record:
6056 if (GetCompleteType(type)) {
6057 const clang::CXXRecordDecl *cxx_record_decl =
6058 qual_type->getAsCXXRecordDecl();
6059 if (cxx_record_decl)
6060 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006061 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006062 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006063
Kate Stoneb9c1b512016-09-06 20:57:50 +00006064 case clang::Type::ObjCObjectPointer:
6065 count = GetPointeeType(type).GetNumDirectBaseClasses();
6066 break;
6067
6068 case clang::Type::ObjCObject:
6069 if (GetCompleteType(type)) {
6070 const clang::ObjCObjectType *objc_class_type =
6071 qual_type->getAsObjCQualifiedInterfaceType();
6072 if (objc_class_type) {
6073 clang::ObjCInterfaceDecl *class_interface_decl =
6074 objc_class_type->getInterface();
6075
6076 if (class_interface_decl && class_interface_decl->getSuperClass())
6077 count = 1;
6078 }
6079 }
6080 break;
6081 case clang::Type::ObjCInterface:
6082 if (GetCompleteType(type)) {
6083 const clang::ObjCInterfaceType *objc_interface_type =
6084 qual_type->getAs<clang::ObjCInterfaceType>();
6085 if (objc_interface_type) {
6086 clang::ObjCInterfaceDecl *class_interface_decl =
6087 objc_interface_type->getInterface();
6088
6089 if (class_interface_decl && class_interface_decl->getSuperClass())
6090 count = 1;
6091 }
6092 }
6093 break;
6094
6095 case clang::Type::Typedef:
6096 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6097 ->getDecl()
6098 ->getUnderlyingType()
6099 .getAsOpaquePtr());
6100 break;
6101
6102 case clang::Type::Auto:
6103 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6104 ->getDeducedType()
6105 .getAsOpaquePtr());
6106 break;
6107
6108 case clang::Type::Elaborated:
6109 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6110 ->getNamedType()
6111 .getAsOpaquePtr());
6112 break;
6113
6114 case clang::Type::Paren:
6115 return GetNumDirectBaseClasses(
6116 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6117
6118 default:
6119 break;
6120 }
6121 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006122}
6123
6124uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006125ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
6126 uint32_t count = 0;
6127 clang::QualType qual_type(GetCanonicalQualType(type));
6128 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6129 switch (type_class) {
6130 case clang::Type::Record:
6131 if (GetCompleteType(type)) {
6132 const clang::CXXRecordDecl *cxx_record_decl =
6133 qual_type->getAsCXXRecordDecl();
6134 if (cxx_record_decl)
6135 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006136 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006137 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006138
Kate Stoneb9c1b512016-09-06 20:57:50 +00006139 case clang::Type::Typedef:
6140 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6141 ->getDecl()
6142 ->getUnderlyingType()
6143 .getAsOpaquePtr());
6144 break;
6145
6146 case clang::Type::Auto:
6147 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6148 ->getDeducedType()
6149 .getAsOpaquePtr());
6150 break;
6151
6152 case clang::Type::Elaborated:
6153 count =
6154 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6155 ->getNamedType()
6156 .getAsOpaquePtr());
6157 break;
6158
6159 case clang::Type::Paren:
6160 count = GetNumVirtualBaseClasses(
6161 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6162 break;
6163
6164 default:
6165 break;
6166 }
6167 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006168}
6169
Kate Stoneb9c1b512016-09-06 20:57:50 +00006170CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6171 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6172 clang::QualType qual_type(GetCanonicalQualType(type));
6173 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6174 switch (type_class) {
6175 case clang::Type::Record:
6176 if (GetCompleteType(type)) {
6177 const clang::CXXRecordDecl *cxx_record_decl =
6178 qual_type->getAsCXXRecordDecl();
6179 if (cxx_record_decl) {
6180 uint32_t curr_idx = 0;
6181 clang::CXXRecordDecl::base_class_const_iterator base_class,
6182 base_class_end;
6183 for (base_class = cxx_record_decl->bases_begin(),
6184 base_class_end = cxx_record_decl->bases_end();
6185 base_class != base_class_end; ++base_class, ++curr_idx) {
6186 if (curr_idx == idx) {
6187 if (bit_offset_ptr) {
6188 const clang::ASTRecordLayout &record_layout =
6189 getASTContext()->getASTRecordLayout(cxx_record_decl);
6190 const clang::CXXRecordDecl *base_class_decl =
6191 llvm::cast<clang::CXXRecordDecl>(
6192 base_class->getType()
6193 ->getAs<clang::RecordType>()
6194 ->getDecl());
6195 if (base_class->isVirtual())
6196 *bit_offset_ptr =
6197 record_layout.getVBaseClassOffset(base_class_decl)
6198 .getQuantity() *
6199 8;
6200 else
6201 *bit_offset_ptr =
6202 record_layout.getBaseClassOffset(base_class_decl)
6203 .getQuantity() *
6204 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006205 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006206 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6207 }
6208 }
6209 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006210 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006211 break;
6212
6213 case clang::Type::ObjCObjectPointer:
6214 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6215
6216 case clang::Type::ObjCObject:
6217 if (idx == 0 && GetCompleteType(type)) {
6218 const clang::ObjCObjectType *objc_class_type =
6219 qual_type->getAsObjCQualifiedInterfaceType();
6220 if (objc_class_type) {
6221 clang::ObjCInterfaceDecl *class_interface_decl =
6222 objc_class_type->getInterface();
6223
6224 if (class_interface_decl) {
6225 clang::ObjCInterfaceDecl *superclass_interface_decl =
6226 class_interface_decl->getSuperClass();
6227 if (superclass_interface_decl) {
6228 if (bit_offset_ptr)
6229 *bit_offset_ptr = 0;
Alex Langfordbddab072019-08-13 19:40:36 +00006230 return CompilerType(this,
Kate Stoneb9c1b512016-09-06 20:57:50 +00006231 getASTContext()->getObjCInterfaceType(
Alex Langfordbddab072019-08-13 19:40:36 +00006232 superclass_interface_decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006233 }
6234 }
6235 }
6236 }
6237 break;
6238 case clang::Type::ObjCInterface:
6239 if (idx == 0 && GetCompleteType(type)) {
6240 const clang::ObjCObjectType *objc_interface_type =
6241 qual_type->getAs<clang::ObjCInterfaceType>();
6242 if (objc_interface_type) {
6243 clang::ObjCInterfaceDecl *class_interface_decl =
6244 objc_interface_type->getInterface();
6245
6246 if (class_interface_decl) {
6247 clang::ObjCInterfaceDecl *superclass_interface_decl =
6248 class_interface_decl->getSuperClass();
6249 if (superclass_interface_decl) {
6250 if (bit_offset_ptr)
6251 *bit_offset_ptr = 0;
Alex Langfordbddab072019-08-13 19:40:36 +00006252 return CompilerType(
6253 this, getASTContext()
6254 ->getObjCInterfaceType(superclass_interface_decl)
6255 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006256 }
6257 }
6258 }
6259 }
6260 break;
6261
6262 case clang::Type::Typedef:
6263 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6264 ->getDecl()
6265 ->getUnderlyingType()
6266 .getAsOpaquePtr(),
6267 idx, bit_offset_ptr);
6268
6269 case clang::Type::Auto:
6270 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6271 ->getDeducedType()
6272 .getAsOpaquePtr(),
6273 idx, bit_offset_ptr);
6274
6275 case clang::Type::Elaborated:
6276 return GetDirectBaseClassAtIndex(
6277 llvm::cast<clang::ElaboratedType>(qual_type)
6278 ->getNamedType()
6279 .getAsOpaquePtr(),
6280 idx, bit_offset_ptr);
6281
6282 case clang::Type::Paren:
6283 return GetDirectBaseClassAtIndex(
6284 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6285 idx, bit_offset_ptr);
6286
6287 default:
6288 break;
6289 }
6290 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006291}
6292
Kate Stoneb9c1b512016-09-06 20:57:50 +00006293CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6294 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6295 clang::QualType qual_type(GetCanonicalQualType(type));
6296 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6297 switch (type_class) {
6298 case clang::Type::Record:
6299 if (GetCompleteType(type)) {
6300 const clang::CXXRecordDecl *cxx_record_decl =
6301 qual_type->getAsCXXRecordDecl();
6302 if (cxx_record_decl) {
6303 uint32_t curr_idx = 0;
6304 clang::CXXRecordDecl::base_class_const_iterator base_class,
6305 base_class_end;
6306 for (base_class = cxx_record_decl->vbases_begin(),
6307 base_class_end = cxx_record_decl->vbases_end();
6308 base_class != base_class_end; ++base_class, ++curr_idx) {
6309 if (curr_idx == idx) {
6310 if (bit_offset_ptr) {
6311 const clang::ASTRecordLayout &record_layout =
6312 getASTContext()->getASTRecordLayout(cxx_record_decl);
6313 const clang::CXXRecordDecl *base_class_decl =
6314 llvm::cast<clang::CXXRecordDecl>(
6315 base_class->getType()
6316 ->getAs<clang::RecordType>()
6317 ->getDecl());
6318 *bit_offset_ptr =
6319 record_layout.getVBaseClassOffset(base_class_decl)
6320 .getQuantity() *
6321 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006322 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006323 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6324 }
6325 }
6326 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006327 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006328 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006329
Kate Stoneb9c1b512016-09-06 20:57:50 +00006330 case clang::Type::Typedef:
6331 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6332 ->getDecl()
6333 ->getUnderlyingType()
6334 .getAsOpaquePtr(),
6335 idx, bit_offset_ptr);
6336
6337 case clang::Type::Auto:
6338 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6339 ->getDeducedType()
6340 .getAsOpaquePtr(),
6341 idx, bit_offset_ptr);
6342
6343 case clang::Type::Elaborated:
6344 return GetVirtualBaseClassAtIndex(
6345 llvm::cast<clang::ElaboratedType>(qual_type)
6346 ->getNamedType()
6347 .getAsOpaquePtr(),
6348 idx, bit_offset_ptr);
6349
6350 case clang::Type::Paren:
6351 return GetVirtualBaseClassAtIndex(
6352 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6353 idx, bit_offset_ptr);
6354
6355 default:
6356 break;
6357 }
6358 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006359}
6360
Greg Claytond8d4a572015-08-11 21:38:15 +00006361// If a pointer to a pointee type (the clang_type arg) says that it has no
6362// children, then we either need to trust it, or override it and return a
6363// different result. For example, an "int *" has one child that is an integer,
6364// but a function pointer doesn't have any children. Likewise if a Record type
6365// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006366uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6367 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006368 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006369
6370 clang::QualType qual_type(type.getCanonicalType());
6371 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6372 switch (type_class) {
6373 case clang::Type::Builtin:
6374 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6375 case clang::BuiltinType::UnknownAny:
6376 case clang::BuiltinType::Void:
6377 case clang::BuiltinType::NullPtr:
6378 case clang::BuiltinType::OCLEvent:
6379 case clang::BuiltinType::OCLImage1dRO:
6380 case clang::BuiltinType::OCLImage1dWO:
6381 case clang::BuiltinType::OCLImage1dRW:
6382 case clang::BuiltinType::OCLImage1dArrayRO:
6383 case clang::BuiltinType::OCLImage1dArrayWO:
6384 case clang::BuiltinType::OCLImage1dArrayRW:
6385 case clang::BuiltinType::OCLImage1dBufferRO:
6386 case clang::BuiltinType::OCLImage1dBufferWO:
6387 case clang::BuiltinType::OCLImage1dBufferRW:
6388 case clang::BuiltinType::OCLImage2dRO:
6389 case clang::BuiltinType::OCLImage2dWO:
6390 case clang::BuiltinType::OCLImage2dRW:
6391 case clang::BuiltinType::OCLImage2dArrayRO:
6392 case clang::BuiltinType::OCLImage2dArrayWO:
6393 case clang::BuiltinType::OCLImage2dArrayRW:
6394 case clang::BuiltinType::OCLImage3dRO:
6395 case clang::BuiltinType::OCLImage3dWO:
6396 case clang::BuiltinType::OCLImage3dRW:
6397 case clang::BuiltinType::OCLSampler:
6398 return 0;
6399 case clang::BuiltinType::Bool:
6400 case clang::BuiltinType::Char_U:
6401 case clang::BuiltinType::UChar:
6402 case clang::BuiltinType::WChar_U:
6403 case clang::BuiltinType::Char16:
6404 case clang::BuiltinType::Char32:
6405 case clang::BuiltinType::UShort:
6406 case clang::BuiltinType::UInt:
6407 case clang::BuiltinType::ULong:
6408 case clang::BuiltinType::ULongLong:
6409 case clang::BuiltinType::UInt128:
6410 case clang::BuiltinType::Char_S:
6411 case clang::BuiltinType::SChar:
6412 case clang::BuiltinType::WChar_S:
6413 case clang::BuiltinType::Short:
6414 case clang::BuiltinType::Int:
6415 case clang::BuiltinType::Long:
6416 case clang::BuiltinType::LongLong:
6417 case clang::BuiltinType::Int128:
6418 case clang::BuiltinType::Float:
6419 case clang::BuiltinType::Double:
6420 case clang::BuiltinType::LongDouble:
6421 case clang::BuiltinType::Dependent:
6422 case clang::BuiltinType::Overload:
6423 case clang::BuiltinType::ObjCId:
6424 case clang::BuiltinType::ObjCClass:
6425 case clang::BuiltinType::ObjCSel:
6426 case clang::BuiltinType::BoundMember:
6427 case clang::BuiltinType::Half:
6428 case clang::BuiltinType::ARCUnbridgedCast:
6429 case clang::BuiltinType::PseudoObject:
6430 case clang::BuiltinType::BuiltinFn:
6431 case clang::BuiltinType::OMPArraySection:
6432 return 1;
6433 default:
6434 return 0;
6435 }
6436 break;
6437
6438 case clang::Type::Complex:
6439 return 1;
6440 case clang::Type::Pointer:
6441 return 1;
6442 case clang::Type::BlockPointer:
6443 return 0; // If block pointers don't have debug info, then no children for
6444 // them
6445 case clang::Type::LValueReference:
6446 return 1;
6447 case clang::Type::RValueReference:
6448 return 1;
6449 case clang::Type::MemberPointer:
6450 return 0;
6451 case clang::Type::ConstantArray:
6452 return 0;
6453 case clang::Type::IncompleteArray:
6454 return 0;
6455 case clang::Type::VariableArray:
6456 return 0;
6457 case clang::Type::DependentSizedArray:
6458 return 0;
6459 case clang::Type::DependentSizedExtVector:
6460 return 0;
6461 case clang::Type::Vector:
6462 return 0;
6463 case clang::Type::ExtVector:
6464 return 0;
6465 case clang::Type::FunctionProto:
6466 return 0; // When we function pointers, they have no children...
6467 case clang::Type::FunctionNoProto:
6468 return 0; // When we function pointers, they have no children...
6469 case clang::Type::UnresolvedUsing:
6470 return 0;
6471 case clang::Type::Paren:
6472 return GetNumPointeeChildren(
6473 llvm::cast<clang::ParenType>(qual_type)->desugar());
6474 case clang::Type::Typedef:
6475 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6476 ->getDecl()
6477 ->getUnderlyingType());
6478 case clang::Type::Auto:
6479 return GetNumPointeeChildren(
6480 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6481 case clang::Type::Elaborated:
6482 return GetNumPointeeChildren(
6483 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6484 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006485 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6486 ->getUnderlyingExpr()
6487 ->getType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006488 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006489 return GetNumPointeeChildren(
6490 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006491 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006492 return GetNumPointeeChildren(
6493 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006494 case clang::Type::Record:
6495 return 0;
6496 case clang::Type::Enum:
6497 return 1;
6498 case clang::Type::TemplateTypeParm:
6499 return 1;
6500 case clang::Type::SubstTemplateTypeParm:
6501 return 1;
6502 case clang::Type::TemplateSpecialization:
6503 return 1;
6504 case clang::Type::InjectedClassName:
6505 return 0;
6506 case clang::Type::DependentName:
6507 return 1;
6508 case clang::Type::DependentTemplateSpecialization:
6509 return 1;
6510 case clang::Type::ObjCObject:
6511 return 0;
6512 case clang::Type::ObjCInterface:
6513 return 0;
6514 case clang::Type::ObjCObjectPointer:
6515 return 1;
6516 default:
6517 break;
6518 }
6519 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006520}
6521
Kate Stoneb9c1b512016-09-06 20:57:50 +00006522CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6523 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6524 bool transparent_pointers, bool omit_empty_base_classes,
6525 bool ignore_array_bounds, std::string &child_name,
6526 uint32_t &child_byte_size, int32_t &child_byte_offset,
6527 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6528 bool &child_is_base_class, bool &child_is_deref_of_parent,
6529 ValueObject *valobj, uint64_t &language_flags) {
6530 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006531 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006532
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006533 auto get_exe_scope = [&exe_ctx]() {
6534 return exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr;
6535 };
6536
Kate Stoneb9c1b512016-09-06 20:57:50 +00006537 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6538 const clang::Type::TypeClass parent_type_class =
6539 parent_qual_type->getTypeClass();
6540 child_bitfield_bit_size = 0;
6541 child_bitfield_bit_offset = 0;
6542 child_is_base_class = false;
6543 language_flags = 0;
6544
Adrian Prantleca07c52018-11-05 20:49:07 +00006545 const bool idx_is_valid =
6546 idx < GetNumChildren(type, omit_empty_base_classes, exe_ctx);
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006547 int32_t bit_offset;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006548 switch (parent_type_class) {
6549 case clang::Type::Builtin:
6550 if (idx_is_valid) {
6551 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6552 case clang::BuiltinType::ObjCId:
6553 case clang::BuiltinType::ObjCClass:
6554 child_name = "isa";
6555 child_byte_size =
6556 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6557 CHAR_BIT;
Alex Langfordbddab072019-08-13 19:40:36 +00006558 return CompilerType(
6559 this, getASTContext()->ObjCBuiltinClassTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006560
6561 default:
6562 break;
6563 }
6564 }
6565 break;
6566
6567 case clang::Type::Record:
6568 if (idx_is_valid && GetCompleteType(type)) {
6569 const clang::RecordType *record_type =
6570 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6571 const clang::RecordDecl *record_decl = record_type->getDecl();
6572 assert(record_decl);
6573 const clang::ASTRecordLayout &record_layout =
6574 getASTContext()->getASTRecordLayout(record_decl);
6575 uint32_t child_idx = 0;
6576
6577 const clang::CXXRecordDecl *cxx_record_decl =
6578 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6579 if (cxx_record_decl) {
6580 // We might have base classes to print out first
6581 clang::CXXRecordDecl::base_class_const_iterator base_class,
6582 base_class_end;
6583 for (base_class = cxx_record_decl->bases_begin(),
6584 base_class_end = cxx_record_decl->bases_end();
6585 base_class != base_class_end; ++base_class) {
6586 const clang::CXXRecordDecl *base_class_decl = nullptr;
6587
6588 // Skip empty base classes
6589 if (omit_empty_base_classes) {
6590 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6591 base_class->getType()->getAs<clang::RecordType>()->getDecl());
Jonas Devliegherea6682a42018-12-15 00:15:33 +00006592 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00006593 continue;
6594 }
6595
6596 if (idx == child_idx) {
6597 if (base_class_decl == nullptr)
6598 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6599 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6600
6601 if (base_class->isVirtual()) {
6602 bool handled = false;
6603 if (valobj) {
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +00006604 clang::VTableContextBase *vtable_ctx =
6605 getASTContext()->getVTableContext();
6606 if (vtable_ctx)
6607 handled = GetVBaseBitOffset(*vtable_ctx, *valobj,
6608 record_layout, cxx_record_decl,
6609 base_class_decl, bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006610 }
6611 if (!handled)
6612 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6613 .getQuantity() *
6614 8;
6615 } else
6616 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6617 .getQuantity() *
6618 8;
6619
6620 // Base classes should be a multiple of 8 bits in size
6621 child_byte_offset = bit_offset / 8;
Alex Langfordbddab072019-08-13 19:40:36 +00006622 CompilerType base_class_clang_type(
6623 this, base_class->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006624 child_name = base_class_clang_type.GetTypeName().AsCString("");
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006625 Optional<uint64_t> size =
6626 base_class_clang_type.GetBitSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006627 if (!size)
6628 return {};
6629 uint64_t base_class_clang_type_bit_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006630
6631 // Base classes bit sizes should be a multiple of 8 bits in size
6632 assert(base_class_clang_type_bit_size % 8 == 0);
6633 child_byte_size = base_class_clang_type_bit_size / 8;
6634 child_is_base_class = true;
6635 return base_class_clang_type;
6636 }
6637 // We don't increment the child index in the for loop since we might
6638 // be skipping empty base classes
6639 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006640 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006641 }
6642 // Make sure index is in range...
6643 uint32_t field_idx = 0;
6644 clang::RecordDecl::field_iterator field, field_end;
6645 for (field = record_decl->field_begin(),
6646 field_end = record_decl->field_end();
6647 field != field_end; ++field, ++field_idx, ++child_idx) {
6648 if (idx == child_idx) {
6649 // Print the member type if requested
6650 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006651 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006652
6653 // Figure out the type byte size (field_type_info.first) and
6654 // alignment (field_type_info.second) from the AST context.
Alex Langfordbddab072019-08-13 19:40:36 +00006655 CompilerType field_clang_type(this,
6656 field->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006657 assert(field_idx < record_layout.getFieldCount());
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006658 Optional<uint64_t> size =
6659 field_clang_type.GetByteSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006660 if (!size)
6661 return {};
6662 child_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006663 const uint32_t child_bit_size = child_byte_size * 8;
6664
6665 // Figure out the field offset within the current struct/union/class
6666 // type
6667 bit_offset = record_layout.getFieldOffset(field_idx);
Raphael Isemann02e91132019-11-20 12:09:19 +01006668 if (FieldIsBitfield(*field, child_bitfield_bit_size)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006669 child_bitfield_bit_offset = bit_offset % child_bit_size;
6670 const uint32_t child_bit_offset =
6671 bit_offset - child_bitfield_bit_offset;
6672 child_byte_offset = child_bit_offset / 8;
6673 } else {
6674 child_byte_offset = bit_offset / 8;
6675 }
6676
6677 return field_clang_type;
6678 }
6679 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006680 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006681 break;
6682
6683 case clang::Type::ObjCObject:
6684 case clang::Type::ObjCInterface:
6685 if (idx_is_valid && GetCompleteType(type)) {
6686 const clang::ObjCObjectType *objc_class_type =
6687 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6688 assert(objc_class_type);
6689 if (objc_class_type) {
6690 uint32_t child_idx = 0;
6691 clang::ObjCInterfaceDecl *class_interface_decl =
6692 objc_class_type->getInterface();
6693
6694 if (class_interface_decl) {
6695
6696 const clang::ASTRecordLayout &interface_layout =
6697 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6698 clang::ObjCInterfaceDecl *superclass_interface_decl =
6699 class_interface_decl->getSuperClass();
6700 if (superclass_interface_decl) {
6701 if (omit_empty_base_classes) {
6702 CompilerType base_class_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00006703 this, getASTContext()
6704 ->getObjCInterfaceType(superclass_interface_decl)
6705 .getAsOpaquePtr());
Adrian Prantleca07c52018-11-05 20:49:07 +00006706 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes,
6707 exe_ctx) > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006708 if (idx == 0) {
6709 clang::QualType ivar_qual_type(
6710 getASTContext()->getObjCInterfaceType(
6711 superclass_interface_decl));
6712
6713 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006714 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006715
6716 clang::TypeInfo ivar_type_info =
6717 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6718
6719 child_byte_size = ivar_type_info.Width / 8;
6720 child_byte_offset = 0;
6721 child_is_base_class = true;
6722
Alex Langfordbddab072019-08-13 19:40:36 +00006723 return CompilerType(this, ivar_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006724 }
6725
6726 ++child_idx;
6727 }
6728 } else
6729 ++child_idx;
6730 }
6731
6732 const uint32_t superclass_idx = child_idx;
6733
6734 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6735 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6736 ivar_end = class_interface_decl->ivar_end();
6737
6738 for (ivar_pos = class_interface_decl->ivar_begin();
6739 ivar_pos != ivar_end; ++ivar_pos) {
6740 if (child_idx == idx) {
6741 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6742
6743 clang::QualType ivar_qual_type(ivar_decl->getType());
6744
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006745 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006746
6747 clang::TypeInfo ivar_type_info =
6748 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6749
6750 child_byte_size = ivar_type_info.Width / 8;
6751
6752 // Figure out the field offset within the current
Adrian Prantl05097242018-04-30 16:49:04 +00006753 // struct/union/class type For ObjC objects, we can't trust the
6754 // bit offset we get from the Clang AST, since that doesn't
6755 // account for the space taken up by unbacked properties, or
6756 // from the changing size of base classes that are newer than
6757 // this class. So if we have a process around that we can ask
6758 // about this object, do so.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006759 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6760 Process *process = nullptr;
6761 if (exe_ctx)
6762 process = exe_ctx->GetProcessPtr();
6763 if (process) {
6764 ObjCLanguageRuntime *objc_runtime =
Alex Langforde823bbe2019-06-10 20:53:23 +00006765 ObjCLanguageRuntime::Get(*process);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006766 if (objc_runtime != nullptr) {
Alex Langfordbddab072019-08-13 19:40:36 +00006767 CompilerType parent_ast_type(
6768 this, parent_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006769 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6770 parent_ast_type, ivar_decl->getNameAsString().c_str());
6771 }
6772 }
6773
Aleksandr Urakovff701722018-08-20 05:59:27 +00006774 // Setting this to INT32_MAX to make sure we don't compute it
Kate Stoneb9c1b512016-09-06 20:57:50 +00006775 // twice...
Aleksandr Urakov53459482018-08-17 07:28:24 +00006776 bit_offset = INT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006777
6778 if (child_byte_offset ==
6779 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6780 bit_offset = interface_layout.getFieldOffset(child_idx -
6781 superclass_idx);
6782 child_byte_offset = bit_offset / 8;
6783 }
6784
6785 // Note, the ObjC Ivar Byte offset is just that, it doesn't
Adrian Prantl05097242018-04-30 16:49:04 +00006786 // account for the bit offset of a bitfield within its
6787 // containing object. So regardless of where we get the byte
Kate Stoneb9c1b512016-09-06 20:57:50 +00006788 // offset from, we still need to get the bit offset for
6789 // bitfields from the layout.
6790
Raphael Isemann02e91132019-11-20 12:09:19 +01006791 if (FieldIsBitfield(ivar_decl, child_bitfield_bit_size)) {
Aleksandr Urakov53459482018-08-17 07:28:24 +00006792 if (bit_offset == INT32_MAX)
Kate Stoneb9c1b512016-09-06 20:57:50 +00006793 bit_offset = interface_layout.getFieldOffset(
6794 child_idx - superclass_idx);
6795
6796 child_bitfield_bit_offset = bit_offset % 8;
6797 }
Alex Langfordbddab072019-08-13 19:40:36 +00006798 return CompilerType(this, ivar_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006799 }
6800 ++child_idx;
6801 }
6802 }
6803 }
6804 }
6805 }
6806 break;
6807
6808 case clang::Type::ObjCObjectPointer:
6809 if (idx_is_valid) {
6810 CompilerType pointee_clang_type(GetPointeeType(type));
6811
6812 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6813 child_is_deref_of_parent = false;
6814 bool tmp_child_is_deref_of_parent = false;
6815 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6816 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6817 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6818 child_bitfield_bit_size, child_bitfield_bit_offset,
6819 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6820 language_flags);
6821 } else {
6822 child_is_deref_of_parent = true;
6823 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00006824 valobj ? valobj->GetName().GetCString() : nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006825 if (parent_name) {
6826 child_name.assign(1, '*');
6827 child_name += parent_name;
6828 }
6829
6830 // We have a pointer to an simple type
6831 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006832 if (Optional<uint64_t> size =
6833 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006834 child_byte_size = *size;
6835 child_byte_offset = 0;
6836 return pointee_clang_type;
6837 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006838 }
6839 }
6840 }
6841 break;
6842
6843 case clang::Type::Vector:
6844 case clang::Type::ExtVector:
6845 if (idx_is_valid) {
6846 const clang::VectorType *array =
6847 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6848 if (array) {
Alex Langfordbddab072019-08-13 19:40:36 +00006849 CompilerType element_type(this,
6850 array->getElementType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006851 if (element_type.GetCompleteType()) {
6852 char element_name[64];
6853 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6854 static_cast<uint64_t>(idx));
6855 child_name.assign(element_name);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006856 if (Optional<uint64_t> size =
6857 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006858 child_byte_size = *size;
6859 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6860 return element_type;
6861 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006862 }
6863 }
6864 }
6865 break;
6866
6867 case clang::Type::ConstantArray:
6868 case clang::Type::IncompleteArray:
6869 if (ignore_array_bounds || idx_is_valid) {
6870 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6871 if (array) {
Alex Langfordbddab072019-08-13 19:40:36 +00006872 CompilerType element_type(this,
6873 array->getElementType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006874 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006875 child_name = llvm::formatv("[{0}]", idx);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006876 if (Optional<uint64_t> size =
6877 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006878 child_byte_size = *size;
6879 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6880 return element_type;
6881 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006882 }
6883 }
6884 }
6885 break;
6886
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006887 case clang::Type::Pointer: {
6888 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006889
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006890 // Don't dereference "void *" pointers
6891 if (pointee_clang_type.IsVoidType())
6892 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006893
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006894 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6895 child_is_deref_of_parent = false;
6896 bool tmp_child_is_deref_of_parent = false;
6897 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6898 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6899 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6900 child_bitfield_bit_size, child_bitfield_bit_offset,
6901 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6902 language_flags);
6903 } else {
6904 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006905
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006906 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00006907 valobj ? valobj->GetName().GetCString() : nullptr;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006908 if (parent_name) {
6909 child_name.assign(1, '*');
6910 child_name += parent_name;
6911 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006912
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006913 // We have a pointer to an simple type
6914 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006915 if (Optional<uint64_t> size =
6916 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006917 child_byte_size = *size;
6918 child_byte_offset = 0;
6919 return pointee_clang_type;
6920 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006921 }
6922 }
6923 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006924 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006925
6926 case clang::Type::LValueReference:
6927 case clang::Type::RValueReference:
6928 if (idx_is_valid) {
6929 const clang::ReferenceType *reference_type =
6930 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
Alex Langfordbddab072019-08-13 19:40:36 +00006931 CompilerType pointee_clang_type(
6932 this, reference_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006933 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6934 child_is_deref_of_parent = false;
6935 bool tmp_child_is_deref_of_parent = false;
6936 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6937 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6938 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6939 child_bitfield_bit_size, child_bitfield_bit_offset,
6940 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6941 language_flags);
6942 } else {
6943 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00006944 valobj ? valobj->GetName().GetCString() : nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006945 if (parent_name) {
6946 child_name.assign(1, '&');
6947 child_name += parent_name;
6948 }
6949
6950 // We have a pointer to an simple type
6951 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006952 if (Optional<uint64_t> size =
6953 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006954 child_byte_size = *size;
6955 child_byte_offset = 0;
6956 return pointee_clang_type;
6957 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006958 }
6959 }
6960 }
6961 break;
6962
6963 case clang::Type::Typedef: {
6964 CompilerType typedefed_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00006965 this, llvm::cast<clang::TypedefType>(parent_qual_type)
6966 ->getDecl()
6967 ->getUnderlyingType()
6968 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006969 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
6970 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6971 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6972 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6973 child_is_deref_of_parent, valobj, language_flags);
6974 } break;
6975
6976 case clang::Type::Auto: {
6977 CompilerType elaborated_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00006978 this, llvm::cast<clang::AutoType>(parent_qual_type)
6979 ->getDeducedType()
6980 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006981 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6982 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6983 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6984 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6985 child_is_deref_of_parent, valobj, language_flags);
6986 }
6987
6988 case clang::Type::Elaborated: {
6989 CompilerType elaborated_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00006990 this, llvm::cast<clang::ElaboratedType>(parent_qual_type)
6991 ->getNamedType()
6992 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006993 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6994 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6995 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6996 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6997 child_is_deref_of_parent, valobj, language_flags);
6998 }
6999
7000 case clang::Type::Paren: {
Alex Langfordbddab072019-08-13 19:40:36 +00007001 CompilerType paren_clang_type(this,
7002 llvm::cast<clang::ParenType>(parent_qual_type)
7003 ->desugar()
7004 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007005 return paren_clang_type.GetChildCompilerTypeAtIndex(
7006 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7007 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7008 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7009 child_is_deref_of_parent, valobj, language_flags);
7010 }
7011
7012 default:
7013 break;
7014 }
7015 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007016}
7017
Kate Stoneb9c1b512016-09-06 20:57:50 +00007018static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
7019 const clang::CXXBaseSpecifier *base_spec,
7020 bool omit_empty_base_classes) {
7021 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007022
Kate Stoneb9c1b512016-09-06 20:57:50 +00007023 const clang::CXXRecordDecl *cxx_record_decl =
7024 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7025
7026 // const char *super_name = record_decl->getNameAsCString();
7027 // const char *base_name =
7028 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
7029 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
7030 //
7031 if (cxx_record_decl) {
7032 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
7033 for (base_class = cxx_record_decl->bases_begin(),
7034 base_class_end = cxx_record_decl->bases_end();
7035 base_class != base_class_end; ++base_class) {
7036 if (omit_empty_base_classes) {
7037 if (BaseSpecifierIsEmpty(base_class))
7038 continue;
7039 }
7040
7041 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
7042 // super_name, base_name,
7043 // child_idx,
7044 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
7045 //
7046 //
7047 if (base_class == base_spec)
7048 return child_idx;
7049 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00007050 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007051 }
7052
7053 return UINT32_MAX;
7054}
7055
7056static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
7057 clang::NamedDecl *canonical_decl,
7058 bool omit_empty_base_classes) {
7059 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
7060 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
7061 omit_empty_base_classes);
7062
7063 clang::RecordDecl::field_iterator field, field_end;
7064 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
7065 field != field_end; ++field, ++child_idx) {
7066 if (field->getCanonicalDecl() == canonical_decl)
7067 return child_idx;
7068 }
7069
7070 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00007071}
7072
7073// Look for a child member (doesn't include base classes, but it does include
Adrian Prantl05097242018-04-30 16:49:04 +00007074// their members) in the type hierarchy. Returns an index path into
7075// "clang_type" on how to reach the appropriate member.
Greg Claytond8d4a572015-08-11 21:38:15 +00007076//
7077// class A
7078// {
7079// public:
7080// int m_a;
7081// int m_b;
7082// };
7083//
7084// class B
7085// {
7086// };
7087//
7088// class C :
7089// public B,
7090// public A
7091// {
7092// };
7093//
7094// If we have a clang type that describes "class C", and we wanted to looked
7095// "m_b" in it:
7096//
Kate Stoneb9c1b512016-09-06 20:57:50 +00007097// With omit_empty_base_classes == false we would get an integer array back
Adrian Prantl05097242018-04-30 16:49:04 +00007098// with: { 1, 1 } The first index 1 is the child index for "class A" within
7099// class C The second index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007100//
Adrian Prantl05097242018-04-30 16:49:04 +00007101// With omit_empty_base_classes == true we would get an integer array back
7102// with: { 0, 1 } The first index 0 is the child index for "class A" within
7103// class C (since class B doesn't have any members it doesn't count) The second
7104// index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007105
Kate Stoneb9c1b512016-09-06 20:57:50 +00007106size_t ClangASTContext::GetIndexOfChildMemberWithName(
7107 lldb::opaque_compiler_type_t type, const char *name,
7108 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
7109 if (type && name && name[0]) {
7110 clang::QualType qual_type(GetCanonicalQualType(type));
7111 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7112 switch (type_class) {
7113 case clang::Type::Record:
7114 if (GetCompleteType(type)) {
7115 const clang::RecordType *record_type =
7116 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7117 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00007118
Kate Stoneb9c1b512016-09-06 20:57:50 +00007119 assert(record_decl);
7120 uint32_t child_idx = 0;
7121
7122 const clang::CXXRecordDecl *cxx_record_decl =
7123 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7124
7125 // Try and find a field that matches NAME
7126 clang::RecordDecl::field_iterator field, field_end;
7127 llvm::StringRef name_sref(name);
7128 for (field = record_decl->field_begin(),
7129 field_end = record_decl->field_end();
7130 field != field_end; ++field, ++child_idx) {
7131 llvm::StringRef field_name = field->getName();
7132 if (field_name.empty()) {
Alex Langfordbddab072019-08-13 19:40:36 +00007133 CompilerType field_type(this, field->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007134 child_indexes.push_back(child_idx);
7135 if (field_type.GetIndexOfChildMemberWithName(
7136 name, omit_empty_base_classes, child_indexes))
7137 return child_indexes.size();
7138 child_indexes.pop_back();
7139
7140 } else if (field_name.equals(name_sref)) {
7141 // We have to add on the number of base classes to this index!
7142 child_indexes.push_back(
7143 child_idx + ClangASTContext::GetNumBaseClasses(
7144 cxx_record_decl, omit_empty_base_classes));
7145 return child_indexes.size();
7146 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007147 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007148
Kate Stoneb9c1b512016-09-06 20:57:50 +00007149 if (cxx_record_decl) {
7150 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7151
7152 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7153
7154 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7155 // Didn't find things easily, lets let clang do its thang...
7156 clang::IdentifierInfo &ident_ref =
7157 getASTContext()->Idents.get(name_sref);
7158 clang::DeclarationName decl_name(&ident_ref);
7159
7160 clang::CXXBasePaths paths;
7161 if (cxx_record_decl->lookupInBases(
7162 [decl_name](const clang::CXXBaseSpecifier *specifier,
7163 clang::CXXBasePath &path) {
7164 return clang::CXXRecordDecl::FindOrdinaryMember(
7165 specifier, path, decl_name);
7166 },
7167 paths)) {
7168 clang::CXXBasePaths::const_paths_iterator path,
7169 path_end = paths.end();
7170 for (path = paths.begin(); path != path_end; ++path) {
7171 const size_t num_path_elements = path->size();
7172 for (size_t e = 0; e < num_path_elements; ++e) {
7173 clang::CXXBasePathElement elem = (*path)[e];
7174
7175 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7176 omit_empty_base_classes);
7177 if (child_idx == UINT32_MAX) {
7178 child_indexes.clear();
7179 return 0;
7180 } else {
7181 child_indexes.push_back(child_idx);
7182 parent_record_decl = llvm::cast<clang::RecordDecl>(
7183 elem.Base->getType()
7184 ->getAs<clang::RecordType>()
7185 ->getDecl());
7186 }
7187 }
7188 for (clang::NamedDecl *path_decl : path->Decls) {
7189 child_idx = GetIndexForRecordChild(
7190 parent_record_decl, path_decl, omit_empty_base_classes);
7191 if (child_idx == UINT32_MAX) {
7192 child_indexes.clear();
7193 return 0;
7194 } else {
7195 child_indexes.push_back(child_idx);
7196 }
7197 }
7198 }
7199 return child_indexes.size();
7200 }
7201 }
7202 }
7203 break;
7204
7205 case clang::Type::ObjCObject:
7206 case clang::Type::ObjCInterface:
7207 if (GetCompleteType(type)) {
7208 llvm::StringRef name_sref(name);
7209 const clang::ObjCObjectType *objc_class_type =
7210 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7211 assert(objc_class_type);
7212 if (objc_class_type) {
7213 uint32_t child_idx = 0;
7214 clang::ObjCInterfaceDecl *class_interface_decl =
7215 objc_class_type->getInterface();
7216
7217 if (class_interface_decl) {
7218 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7219 ivar_end = class_interface_decl->ivar_end();
7220 clang::ObjCInterfaceDecl *superclass_interface_decl =
7221 class_interface_decl->getSuperClass();
7222
7223 for (ivar_pos = class_interface_decl->ivar_begin();
7224 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7225 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7226
7227 if (ivar_decl->getName().equals(name_sref)) {
7228 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7229 (omit_empty_base_classes &&
7230 ObjCDeclHasIVars(superclass_interface_decl, true)))
7231 ++child_idx;
7232
7233 child_indexes.push_back(child_idx);
7234 return child_indexes.size();
7235 }
7236 }
7237
7238 if (superclass_interface_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00007239 // The super class index is always zero for ObjC classes, so we
7240 // push it onto the child indexes in case we find an ivar in our
7241 // superclass...
Kate Stoneb9c1b512016-09-06 20:57:50 +00007242 child_indexes.push_back(0);
7243
7244 CompilerType superclass_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00007245 this, getASTContext()
7246 ->getObjCInterfaceType(superclass_interface_decl)
7247 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007248 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7249 name, omit_empty_base_classes, child_indexes)) {
Adrian Prantl05097242018-04-30 16:49:04 +00007250 // We did find an ivar in a superclass so just return the
7251 // results!
Kate Stoneb9c1b512016-09-06 20:57:50 +00007252 return child_indexes.size();
7253 }
7254
Adrian Prantl05097242018-04-30 16:49:04 +00007255 // We didn't find an ivar matching "name" in our superclass, pop
7256 // the superclass zero index that we pushed on above.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007257 child_indexes.pop_back();
7258 }
7259 }
7260 }
7261 }
7262 break;
7263
7264 case clang::Type::ObjCObjectPointer: {
7265 CompilerType objc_object_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00007266 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7267 ->getPointeeType()
7268 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007269 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7270 name, omit_empty_base_classes, child_indexes);
7271 } break;
7272
7273 case clang::Type::ConstantArray: {
7274 // const clang::ConstantArrayType *array =
7275 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7276 // const uint64_t element_count =
7277 // array->getSize().getLimitedValue();
7278 //
7279 // if (idx < element_count)
7280 // {
7281 // std::pair<uint64_t, unsigned> field_type_info =
7282 // ast->getTypeInfo(array->getElementType());
7283 //
7284 // char element_name[32];
7285 // ::snprintf (element_name, sizeof (element_name),
7286 // "%s[%u]", parent_name ? parent_name : "", idx);
7287 //
7288 // child_name.assign(element_name);
7289 // assert(field_type_info.first % 8 == 0);
7290 // child_byte_size = field_type_info.first / 8;
7291 // child_byte_offset = idx * child_byte_size;
7292 // return array->getElementType().getAsOpaquePtr();
7293 // }
7294 } break;
7295
7296 // case clang::Type::MemberPointerType:
7297 // {
7298 // MemberPointerType *mem_ptr_type =
7299 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7300 // clang::QualType pointee_type =
7301 // mem_ptr_type->getPointeeType();
7302 //
7303 // if (ClangASTContext::IsAggregateType
7304 // (pointee_type.getAsOpaquePtr()))
7305 // {
7306 // return GetIndexOfChildWithName (ast,
7307 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7308 // name);
7309 // }
7310 // }
7311 // break;
7312 //
7313 case clang::Type::LValueReference:
7314 case clang::Type::RValueReference: {
7315 const clang::ReferenceType *reference_type =
7316 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7317 clang::QualType pointee_type(reference_type->getPointeeType());
Alex Langfordbddab072019-08-13 19:40:36 +00007318 CompilerType pointee_clang_type(this, pointee_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007319
7320 if (pointee_clang_type.IsAggregateType()) {
7321 return pointee_clang_type.GetIndexOfChildMemberWithName(
7322 name, omit_empty_base_classes, child_indexes);
7323 }
7324 } break;
7325
7326 case clang::Type::Pointer: {
7327 CompilerType pointee_clang_type(GetPointeeType(type));
7328
7329 if (pointee_clang_type.IsAggregateType()) {
7330 return pointee_clang_type.GetIndexOfChildMemberWithName(
7331 name, omit_empty_base_classes, child_indexes);
7332 }
7333 } break;
7334
7335 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00007336 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
7337 ->getDecl()
7338 ->getUnderlyingType()
7339 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007340 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7341 child_indexes);
7342
7343 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00007344 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
7345 ->getDeducedType()
7346 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007347 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7348 child_indexes);
7349
7350 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00007351 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
7352 ->getNamedType()
7353 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007354 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7355 child_indexes);
7356
7357 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00007358 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
7359 ->desugar()
7360 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007361 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7362 child_indexes);
7363
7364 default:
7365 break;
7366 }
7367 }
7368 return 0;
7369}
Greg Claytond8d4a572015-08-11 21:38:15 +00007370
7371// Get the index of the child of "clang_type" whose name matches. This function
7372// doesn't descend into the children, but only looks one level deep and name
7373// matches can include base class names.
7374
7375uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007376ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7377 const char *name,
7378 bool omit_empty_base_classes) {
7379 if (type && name && name[0]) {
7380 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007381
Kate Stoneb9c1b512016-09-06 20:57:50 +00007382 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7383
7384 switch (type_class) {
7385 case clang::Type::Record:
7386 if (GetCompleteType(type)) {
7387 const clang::RecordType *record_type =
7388 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7389 const clang::RecordDecl *record_decl = record_type->getDecl();
7390
7391 assert(record_decl);
7392 uint32_t child_idx = 0;
7393
7394 const clang::CXXRecordDecl *cxx_record_decl =
7395 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7396
7397 if (cxx_record_decl) {
7398 clang::CXXRecordDecl::base_class_const_iterator base_class,
7399 base_class_end;
7400 for (base_class = cxx_record_decl->bases_begin(),
7401 base_class_end = cxx_record_decl->bases_end();
7402 base_class != base_class_end; ++base_class) {
7403 // Skip empty base classes
7404 clang::CXXRecordDecl *base_class_decl =
7405 llvm::cast<clang::CXXRecordDecl>(
7406 base_class->getType()
7407 ->getAs<clang::RecordType>()
7408 ->getDecl());
7409 if (omit_empty_base_classes &&
Jonas Devliegherea6682a42018-12-15 00:15:33 +00007410 !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00007411 continue;
7412
Alex Langfordbddab072019-08-13 19:40:36 +00007413 CompilerType base_class_clang_type(
7414 this, base_class->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007415 std::string base_class_type_name(
7416 base_class_clang_type.GetTypeName().AsCString(""));
Jonas Devlieghere8d20cfd2018-12-21 22:46:10 +00007417 if (base_class_type_name == name)
Kate Stoneb9c1b512016-09-06 20:57:50 +00007418 return child_idx;
7419 ++child_idx;
7420 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007421 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007422
Kate Stoneb9c1b512016-09-06 20:57:50 +00007423 // Try and find a field that matches NAME
7424 clang::RecordDecl::field_iterator field, field_end;
7425 llvm::StringRef name_sref(name);
7426 for (field = record_decl->field_begin(),
7427 field_end = record_decl->field_end();
7428 field != field_end; ++field, ++child_idx) {
7429 if (field->getName().equals(name_sref))
7430 return child_idx;
7431 }
7432 }
7433 break;
7434
7435 case clang::Type::ObjCObject:
7436 case clang::Type::ObjCInterface:
7437 if (GetCompleteType(type)) {
7438 llvm::StringRef name_sref(name);
7439 const clang::ObjCObjectType *objc_class_type =
7440 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7441 assert(objc_class_type);
7442 if (objc_class_type) {
7443 uint32_t child_idx = 0;
7444 clang::ObjCInterfaceDecl *class_interface_decl =
7445 objc_class_type->getInterface();
7446
7447 if (class_interface_decl) {
7448 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7449 ivar_end = class_interface_decl->ivar_end();
7450 clang::ObjCInterfaceDecl *superclass_interface_decl =
7451 class_interface_decl->getSuperClass();
7452
7453 for (ivar_pos = class_interface_decl->ivar_begin();
7454 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7455 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7456
7457 if (ivar_decl->getName().equals(name_sref)) {
7458 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7459 (omit_empty_base_classes &&
7460 ObjCDeclHasIVars(superclass_interface_decl, true)))
7461 ++child_idx;
7462
7463 return child_idx;
7464 }
7465 }
7466
7467 if (superclass_interface_decl) {
7468 if (superclass_interface_decl->getName().equals(name_sref))
7469 return 0;
7470 }
7471 }
7472 }
7473 }
7474 break;
7475
7476 case clang::Type::ObjCObjectPointer: {
7477 CompilerType pointee_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00007478 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7479 ->getPointeeType()
7480 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007481 return pointee_clang_type.GetIndexOfChildWithName(
7482 name, omit_empty_base_classes);
7483 } break;
7484
7485 case clang::Type::ConstantArray: {
7486 // const clang::ConstantArrayType *array =
7487 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7488 // const uint64_t element_count =
7489 // array->getSize().getLimitedValue();
7490 //
7491 // if (idx < element_count)
7492 // {
7493 // std::pair<uint64_t, unsigned> field_type_info =
7494 // ast->getTypeInfo(array->getElementType());
7495 //
7496 // char element_name[32];
7497 // ::snprintf (element_name, sizeof (element_name),
7498 // "%s[%u]", parent_name ? parent_name : "", idx);
7499 //
7500 // child_name.assign(element_name);
7501 // assert(field_type_info.first % 8 == 0);
7502 // child_byte_size = field_type_info.first / 8;
7503 // child_byte_offset = idx * child_byte_size;
7504 // return array->getElementType().getAsOpaquePtr();
7505 // }
7506 } break;
7507
7508 // case clang::Type::MemberPointerType:
7509 // {
7510 // MemberPointerType *mem_ptr_type =
7511 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7512 // clang::QualType pointee_type =
7513 // mem_ptr_type->getPointeeType();
7514 //
7515 // if (ClangASTContext::IsAggregateType
7516 // (pointee_type.getAsOpaquePtr()))
7517 // {
7518 // return GetIndexOfChildWithName (ast,
7519 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7520 // name);
7521 // }
7522 // }
7523 // break;
7524 //
7525 case clang::Type::LValueReference:
7526 case clang::Type::RValueReference: {
7527 const clang::ReferenceType *reference_type =
7528 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
Alex Langfordbddab072019-08-13 19:40:36 +00007529 CompilerType pointee_type(
7530 this, reference_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007531
7532 if (pointee_type.IsAggregateType()) {
7533 return pointee_type.GetIndexOfChildWithName(name,
7534 omit_empty_base_classes);
7535 }
7536 } break;
7537
7538 case clang::Type::Pointer: {
7539 const clang::PointerType *pointer_type =
7540 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
Alex Langfordbddab072019-08-13 19:40:36 +00007541 CompilerType pointee_type(
7542 this, pointer_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007543
7544 if (pointee_type.IsAggregateType()) {
7545 return pointee_type.GetIndexOfChildWithName(name,
7546 omit_empty_base_classes);
7547 } else {
7548 // if (parent_name)
7549 // {
7550 // child_name.assign(1, '*');
7551 // child_name += parent_name;
7552 // }
7553 //
7554 // // We have a pointer to an simple type
7555 // if (idx == 0)
7556 // {
7557 // std::pair<uint64_t, unsigned> clang_type_info
7558 // = ast->getTypeInfo(pointee_type);
7559 // assert(clang_type_info.first % 8 == 0);
7560 // child_byte_size = clang_type_info.first / 8;
7561 // child_byte_offset = 0;
7562 // return pointee_type.getAsOpaquePtr();
7563 // }
7564 }
7565 } break;
7566
7567 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00007568 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
7569 ->getDeducedType()
7570 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007571 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7572
7573 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00007574 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
7575 ->getNamedType()
7576 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007577 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7578
7579 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00007580 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
7581 ->desugar()
7582 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007583 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7584
7585 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00007586 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
7587 ->getDecl()
7588 ->getUnderlyingType()
7589 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007590 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7591
7592 default:
7593 break;
7594 }
7595 }
7596 return UINT32_MAX;
7597}
Greg Claytond8d4a572015-08-11 21:38:15 +00007598
7599size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007600ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7601 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007602 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007603
Kate Stoneb9c1b512016-09-06 20:57:50 +00007604 clang::QualType qual_type(GetCanonicalQualType(type));
7605 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7606 switch (type_class) {
7607 case clang::Type::Record:
7608 if (GetCompleteType(type)) {
7609 const clang::CXXRecordDecl *cxx_record_decl =
7610 qual_type->getAsCXXRecordDecl();
7611 if (cxx_record_decl) {
7612 const clang::ClassTemplateSpecializationDecl *template_decl =
7613 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7614 cxx_record_decl);
7615 if (template_decl)
7616 return template_decl->getTemplateArgs().size();
7617 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007618 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007619 break;
7620
7621 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00007622 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
7623 ->getDecl()
7624 ->getUnderlyingType()
7625 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007626 .GetNumTemplateArguments();
7627
7628 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00007629 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
7630 ->getDeducedType()
7631 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007632 .GetNumTemplateArguments();
7633
7634 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00007635 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
7636 ->getNamedType()
7637 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007638 .GetNumTemplateArguments();
7639
7640 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00007641 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
7642 ->desugar()
7643 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007644 .GetNumTemplateArguments();
7645
7646 default:
7647 break;
7648 }
7649
7650 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007651}
7652
Pavel Labath769b21e2017-11-13 14:26:21 +00007653const clang::ClassTemplateSpecializationDecl *
7654ClangASTContext::GetAsTemplateSpecialization(
7655 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007656 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007657 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007658
7659 clang::QualType qual_type(GetCanonicalQualType(type));
7660 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7661 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007662 case clang::Type::Record: {
7663 if (! GetCompleteType(type))
7664 return nullptr;
7665 const clang::CXXRecordDecl *cxx_record_decl =
7666 qual_type->getAsCXXRecordDecl();
7667 if (!cxx_record_decl)
7668 return nullptr;
7669 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7670 cxx_record_decl);
7671 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007672
7673 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007674 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7675 ->getDecl()
7676 ->getUnderlyingType()
7677 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007678
7679 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007680 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7681 ->getDeducedType()
7682 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007683
7684 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007685 return GetAsTemplateSpecialization(
7686 llvm::cast<clang::ElaboratedType>(qual_type)
7687 ->getNamedType()
7688 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007689
7690 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007691 return GetAsTemplateSpecialization(
7692 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007693
7694 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007695 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007696 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007697}
7698
7699lldb::TemplateArgumentKind
7700ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7701 size_t arg_idx) {
7702 const clang::ClassTemplateSpecializationDecl *template_decl =
7703 GetAsTemplateSpecialization(type);
7704 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7705 return eTemplateArgumentKindNull;
7706
7707 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7708 case clang::TemplateArgument::Null:
7709 return eTemplateArgumentKindNull;
7710
7711 case clang::TemplateArgument::NullPtr:
7712 return eTemplateArgumentKindNullPtr;
7713
7714 case clang::TemplateArgument::Type:
7715 return eTemplateArgumentKindType;
7716
7717 case clang::TemplateArgument::Declaration:
7718 return eTemplateArgumentKindDeclaration;
7719
7720 case clang::TemplateArgument::Integral:
7721 return eTemplateArgumentKindIntegral;
7722
7723 case clang::TemplateArgument::Template:
7724 return eTemplateArgumentKindTemplate;
7725
7726 case clang::TemplateArgument::TemplateExpansion:
7727 return eTemplateArgumentKindTemplateExpansion;
7728
7729 case clang::TemplateArgument::Expression:
7730 return eTemplateArgumentKindExpression;
7731
7732 case clang::TemplateArgument::Pack:
7733 return eTemplateArgumentKindPack;
7734 }
7735 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7736}
7737
7738CompilerType
7739ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7740 size_t idx) {
7741 const clang::ClassTemplateSpecializationDecl *template_decl =
7742 GetAsTemplateSpecialization(type);
7743 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7744 return CompilerType();
7745
7746 const clang::TemplateArgument &template_arg =
7747 template_decl->getTemplateArgs()[idx];
7748 if (template_arg.getKind() != clang::TemplateArgument::Type)
7749 return CompilerType();
7750
Alex Langfordbddab072019-08-13 19:40:36 +00007751 return CompilerType(this, template_arg.getAsType().getAsOpaquePtr());
Pavel Labath769b21e2017-11-13 14:26:21 +00007752}
7753
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007754Optional<CompilerType::IntegralTemplateArgument>
Pavel Labath769b21e2017-11-13 14:26:21 +00007755ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7756 size_t idx) {
7757 const clang::ClassTemplateSpecializationDecl *template_decl =
7758 GetAsTemplateSpecialization(type);
7759 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
Pavel Labathf59056f2017-11-30 10:16:54 +00007760 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007761
7762 const clang::TemplateArgument &template_arg =
7763 template_decl->getTemplateArgs()[idx];
7764 if (template_arg.getKind() != clang::TemplateArgument::Integral)
Pavel Labathf59056f2017-11-30 10:16:54 +00007765 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007766
Alex Langfordbddab072019-08-13 19:40:36 +00007767 return {
7768 {template_arg.getAsIntegral(),
7769 CompilerType(this, template_arg.getIntegralType().getAsOpaquePtr())}};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007770}
7771
Kate Stoneb9c1b512016-09-06 20:57:50 +00007772CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7773 if (type)
7774 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7775 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007776}
7777
Kate Stoneb9c1b512016-09-06 20:57:50 +00007778clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7779 const clang::EnumType *enutype =
7780 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7781 if (enutype)
7782 return enutype->getDecl();
Konrad Kleine248a1302019-05-23 11:14:47 +00007783 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007784}
7785
7786clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7787 const clang::RecordType *record_type =
7788 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7789 if (record_type)
7790 return record_type->getDecl();
7791 return nullptr;
7792}
7793
7794clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
Zachary Turner1639c6b2018-12-17 16:15:13 +00007795 return ClangUtil::GetAsTagDecl(type);
Greg Claytone6b36cd2015-12-08 01:02:08 +00007796}
7797
Aleksandr Urakov709426b2018-09-10 08:08:43 +00007798clang::TypedefNameDecl *
7799ClangASTContext::GetAsTypedefDecl(const CompilerType &type) {
7800 const clang::TypedefType *typedef_type =
7801 llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type));
7802 if (typedef_type)
7803 return typedef_type->getDecl();
7804 return nullptr;
7805}
7806
Greg Claytond8d4a572015-08-11 21:38:15 +00007807clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007808ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7809 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007810}
7811
7812clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007813ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7814 const clang::ObjCObjectType *objc_class_type =
7815 llvm::dyn_cast<clang::ObjCObjectType>(
7816 ClangUtil::GetCanonicalQualType(type));
7817 if (objc_class_type)
7818 return objc_class_type->getInterface();
7819 return nullptr;
7820}
7821
7822clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007823 const CompilerType &type, llvm::StringRef name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00007824 const CompilerType &field_clang_type, AccessType access,
7825 uint32_t bitfield_bit_size) {
7826 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007827 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007828 ClangASTContext *ast =
7829 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7830 if (!ast)
7831 return nullptr;
7832 clang::ASTContext *clang_ast = ast->getASTContext();
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007833 clang::IdentifierInfo *ident = nullptr;
7834 if (!name.empty())
7835 ident = &clang_ast->Idents.get(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00007836
7837 clang::FieldDecl *field = nullptr;
7838
7839 clang::Expr *bit_width = nullptr;
7840 if (bitfield_bit_size != 0) {
7841 llvm::APInt bitfield_bit_size_apint(
7842 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7843 bit_width = new (*clang_ast)
7844 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7845 clang_ast->IntTy, clang::SourceLocation());
7846 }
7847
7848 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7849 if (record_decl) {
7850 field = clang::FieldDecl::Create(
7851 *clang_ast, record_decl, clang::SourceLocation(),
7852 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007853 ident, // Identifier
7854 ClangUtil::GetQualType(field_clang_type), // Field type
7855 nullptr, // TInfo *
7856 bit_width, // BitWidth
7857 false, // Mutable
7858 clang::ICIS_NoInit); // HasInit
Kate Stoneb9c1b512016-09-06 20:57:50 +00007859
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007860 if (name.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +00007861 // Determine whether this field corresponds to an anonymous struct or
7862 // union.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007863 if (const clang::TagType *TagT =
7864 field->getType()->getAs<clang::TagType>()) {
7865 if (clang::RecordDecl *Rec =
7866 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7867 if (!Rec->getDeclName()) {
7868 Rec->setAnonymousStructOrUnion(true);
7869 field->setImplicit();
7870 }
7871 }
7872 }
7873
7874 if (field) {
7875 field->setAccess(
7876 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7877
7878 record_decl->addDecl(field);
7879
7880#ifdef LLDB_CONFIGURATION_DEBUG
7881 VerifyDecl(field);
7882#endif
7883 }
7884 } else {
7885 clang::ObjCInterfaceDecl *class_interface_decl =
7886 ast->GetAsObjCInterfaceDecl(type);
7887
7888 if (class_interface_decl) {
7889 const bool is_synthesized = false;
7890
7891 field_clang_type.GetCompleteType();
7892
7893 field = clang::ObjCIvarDecl::Create(
7894 *clang_ast, class_interface_decl, clang::SourceLocation(),
7895 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007896 ident, // Identifier
7897 ClangUtil::GetQualType(field_clang_type), // Field type
7898 nullptr, // TypeSourceInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007899 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7900 is_synthesized);
7901
7902 if (field) {
7903 class_interface_decl->addDecl(field);
7904
7905#ifdef LLDB_CONFIGURATION_DEBUG
7906 VerifyDecl(field);
7907#endif
7908 }
7909 }
7910 }
7911 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007912}
7913
Kate Stoneb9c1b512016-09-06 20:57:50 +00007914void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7915 if (!type)
7916 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007917
Kate Stoneb9c1b512016-09-06 20:57:50 +00007918 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7919 if (!ast)
7920 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007921
Kate Stoneb9c1b512016-09-06 20:57:50 +00007922 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007923
Kate Stoneb9c1b512016-09-06 20:57:50 +00007924 if (!record_decl)
7925 return;
7926
7927 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7928
7929 IndirectFieldVector indirect_fields;
7930 clang::RecordDecl::field_iterator field_pos;
7931 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7932 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7933 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7934 last_field_pos = field_pos++) {
7935 if (field_pos->isAnonymousStructOrUnion()) {
7936 clang::QualType field_qual_type = field_pos->getType();
7937
7938 const clang::RecordType *field_record_type =
7939 field_qual_type->getAs<clang::RecordType>();
7940
7941 if (!field_record_type)
7942 continue;
7943
7944 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7945
7946 if (!field_record_decl)
7947 continue;
7948
7949 for (clang::RecordDecl::decl_iterator
7950 di = field_record_decl->decls_begin(),
7951 de = field_record_decl->decls_end();
7952 di != de; ++di) {
7953 if (clang::FieldDecl *nested_field_decl =
7954 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7955 clang::NamedDecl **chain =
7956 new (*ast->getASTContext()) clang::NamedDecl *[2];
7957 chain[0] = *field_pos;
7958 chain[1] = nested_field_decl;
7959 clang::IndirectFieldDecl *indirect_field =
7960 clang::IndirectFieldDecl::Create(
7961 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7962 nested_field_decl->getIdentifier(),
7963 nested_field_decl->getType(), {chain, 2});
7964
7965 indirect_field->setImplicit();
7966
7967 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7968 field_pos->getAccess(), nested_field_decl->getAccess()));
7969
7970 indirect_fields.push_back(indirect_field);
7971 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7972 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7973 size_t nested_chain_size =
7974 nested_indirect_field_decl->getChainingSize();
7975 clang::NamedDecl **chain = new (*ast->getASTContext())
7976 clang::NamedDecl *[nested_chain_size + 1];
7977 chain[0] = *field_pos;
7978
7979 int chain_index = 1;
7980 for (clang::IndirectFieldDecl::chain_iterator
7981 nci = nested_indirect_field_decl->chain_begin(),
7982 nce = nested_indirect_field_decl->chain_end();
7983 nci < nce; ++nci) {
7984 chain[chain_index] = *nci;
7985 chain_index++;
7986 }
7987
7988 clang::IndirectFieldDecl *indirect_field =
7989 clang::IndirectFieldDecl::Create(
7990 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7991 nested_indirect_field_decl->getIdentifier(),
7992 nested_indirect_field_decl->getType(),
7993 {chain, nested_chain_size + 1});
7994
7995 indirect_field->setImplicit();
7996
7997 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7998 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
7999
8000 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00008001 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008002 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008003 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008004 }
8005
Adrian Prantl05097242018-04-30 16:49:04 +00008006 // Check the last field to see if it has an incomplete array type as its last
8007 // member and if it does, the tell the record decl about it
Kate Stoneb9c1b512016-09-06 20:57:50 +00008008 if (last_field_pos != field_end_pos) {
8009 if (last_field_pos->getType()->isIncompleteArrayType())
8010 record_decl->hasFlexibleArrayMember();
8011 }
8012
8013 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
8014 ife = indirect_fields.end();
8015 ifi < ife; ++ifi) {
8016 record_decl->addDecl(*ifi);
8017 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008018}
8019
Kate Stoneb9c1b512016-09-06 20:57:50 +00008020void ClangASTContext::SetIsPacked(const CompilerType &type) {
8021 if (type) {
8022 ClangASTContext *ast =
8023 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8024 if (ast) {
8025 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
8026
8027 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00008028 return;
8029
Kate Stoneb9c1b512016-09-06 20:57:50 +00008030 record_decl->addAttr(
8031 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00008032 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008033 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008034}
8035
Kate Stoneb9c1b512016-09-06 20:57:50 +00008036clang::VarDecl *ClangASTContext::AddVariableToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008037 const CompilerType &type, llvm::StringRef name,
8038 const CompilerType &var_type, AccessType access) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00008039 if (!type.IsValid() || !var_type.IsValid())
8040 return nullptr;
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008041
Kate Stoneb9c1b512016-09-06 20:57:50 +00008042 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8043 if (!ast)
8044 return nullptr;
8045
8046 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008047 if (!record_decl)
8048 return nullptr;
8049
8050 clang::VarDecl *var_decl = nullptr;
8051 clang::IdentifierInfo *ident = nullptr;
8052 if (!name.empty())
8053 ident = &ast->getASTContext()->Idents.get(name);
8054
8055 var_decl = clang::VarDecl::Create(
8056 *ast->getASTContext(), // ASTContext &
8057 record_decl, // DeclContext *
8058 clang::SourceLocation(), // clang::SourceLocation StartLoc
8059 clang::SourceLocation(), // clang::SourceLocation IdLoc
8060 ident, // clang::IdentifierInfo *
8061 ClangUtil::GetQualType(var_type), // Variable clang::QualType
8062 nullptr, // TypeSourceInfo *
8063 clang::SC_Static); // StorageClass
8064 if (!var_decl)
8065 return nullptr;
8066
8067 var_decl->setAccess(
8068 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
8069 record_decl->addDecl(var_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008070
Greg Claytond8d4a572015-08-11 21:38:15 +00008071#ifdef LLDB_CONFIGURATION_DEBUG
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008072 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008073#endif
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008074
Kate Stoneb9c1b512016-09-06 20:57:50 +00008075 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008076}
8077
Kate Stoneb9c1b512016-09-06 20:57:50 +00008078clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
Davide Italiano675767a2018-03-27 19:40:50 +00008079 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008080 const CompilerType &method_clang_type, lldb::AccessType access,
8081 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
8082 bool is_attr_used, bool is_artificial) {
8083 if (!type || !method_clang_type.IsValid() || name == nullptr ||
8084 name[0] == '\0')
8085 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008086
Kate Stoneb9c1b512016-09-06 20:57:50 +00008087 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008088
Kate Stoneb9c1b512016-09-06 20:57:50 +00008089 clang::CXXRecordDecl *cxx_record_decl =
8090 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008091
Kate Stoneb9c1b512016-09-06 20:57:50 +00008092 if (cxx_record_decl == nullptr)
8093 return nullptr;
8094
8095 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8096
8097 clang::CXXMethodDecl *cxx_method_decl = nullptr;
8098
8099 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
8100
8101 const clang::FunctionType *function_type =
8102 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
8103
8104 if (function_type == nullptr)
8105 return nullptr;
8106
8107 const clang::FunctionProtoType *method_function_prototype(
8108 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
8109
8110 if (!method_function_prototype)
8111 return nullptr;
8112
8113 unsigned int num_params = method_function_prototype->getNumParams();
8114
8115 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
8116 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
8117
8118 if (is_artificial)
8119 return nullptr; // skip everything artificial
8120
Richard Smith36851a62019-05-09 04:40:57 +00008121 const clang::ExplicitSpecifier explicit_spec(
8122 nullptr /*expr*/, is_explicit
8123 ? clang::ExplicitSpecKind::ResolvedTrue
8124 : clang::ExplicitSpecKind::ResolvedFalse);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008125 if (name[0] == '~') {
8126 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
8127 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8128 clang::DeclarationNameInfo(
8129 getASTContext()->DeclarationNames.getCXXDestructorName(
8130 getASTContext()->getCanonicalType(record_qual_type)),
8131 clang::SourceLocation()),
Raphael Isemann15695cd2019-09-23 06:59:35 +00008132 method_qual_type, nullptr, is_inline, is_artificial,
8133 ConstexprSpecKind::CSK_unspecified);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008134 cxx_method_decl = cxx_dtor_decl;
8135 } else if (decl_name == cxx_record_decl->getDeclName()) {
8136 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
8137 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8138 clang::DeclarationNameInfo(
8139 getASTContext()->DeclarationNames.getCXXConstructorName(
8140 getASTContext()->getCanonicalType(record_qual_type)),
8141 clang::SourceLocation()),
8142 method_qual_type,
8143 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008144 explicit_spec, is_inline, is_artificial, CSK_unspecified);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008145 cxx_method_decl = cxx_ctor_decl;
8146 } else {
8147 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8148 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8149
8150 if (IsOperator(name, op_kind)) {
8151 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
Adrian Prantl05097242018-04-30 16:49:04 +00008152 // Check the number of operator parameters. Sometimes we have seen bad
8153 // DWARF that doesn't correctly describe operators and if we try to
8154 // create a method and add it to the class, clang will assert and
8155 // crash, so we need to make sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008156 const bool is_method = true;
8157 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8158 is_method, op_kind, num_params))
8159 return nullptr;
8160 cxx_method_decl = clang::CXXMethodDecl::Create(
8161 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8162 clang::DeclarationNameInfo(
8163 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8164 clang::SourceLocation()),
8165 method_qual_type,
8166 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008167 SC, is_inline, CSK_unspecified, clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008168 } else if (num_params == 0) {
8169 // Conversion operators don't take params...
8170 cxx_method_decl = clang::CXXConversionDecl::Create(
8171 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8172 clang::DeclarationNameInfo(
8173 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8174 getASTContext()->getCanonicalType(
8175 function_type->getReturnType())),
8176 clang::SourceLocation()),
8177 method_qual_type,
8178 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008179 is_inline, explicit_spec, CSK_unspecified,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008180 clang::SourceLocation());
8181 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008182 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008183
8184 if (cxx_method_decl == nullptr) {
8185 cxx_method_decl = clang::CXXMethodDecl::Create(
8186 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8187 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8188 method_qual_type,
8189 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008190 SC, is_inline, CSK_unspecified, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008191 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008192 }
8193
8194 clang::AccessSpecifier access_specifier =
8195 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8196
8197 cxx_method_decl->setAccess(access_specifier);
8198 cxx_method_decl->setVirtualAsWritten(is_virtual);
8199
8200 if (is_attr_used)
8201 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8202
Konrad Kleine248a1302019-05-23 11:14:47 +00008203 if (mangled_name != nullptr) {
Vedant Kumarf6bc2512019-09-25 18:00:31 +00008204 cxx_method_decl->addAttr(clang::AsmLabelAttr::CreateImplicit(
8205 *getASTContext(), mangled_name, /*literal=*/false));
Davide Italiano675767a2018-03-27 19:40:50 +00008206 }
8207
Kate Stoneb9c1b512016-09-06 20:57:50 +00008208 // Populate the method decl with parameter decls
8209
8210 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8211
8212 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8213 params.push_back(clang::ParmVarDecl::Create(
8214 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8215 clang::SourceLocation(),
8216 nullptr, // anonymous
8217 method_function_prototype->getParamType(param_index), nullptr,
8218 clang::SC_None, nullptr));
8219 }
8220
8221 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8222
8223 cxx_record_decl->addDecl(cxx_method_decl);
8224
8225 // Sometimes the debug info will mention a constructor (default/copy/move),
8226 // destructor, or assignment operator (copy/move) but there won't be any
8227 // version of this in the code. So we check if the function was artificially
8228 // generated and if it is trivial and this lets the compiler/backend know
8229 // that it can inline the IR for these when it needs to and we can avoid a
8230 // "missing function" error when running expressions.
8231
8232 if (is_artificial) {
8233 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8234 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8235 (cxx_ctor_decl->isCopyConstructor() &&
8236 cxx_record_decl->hasTrivialCopyConstructor()) ||
8237 (cxx_ctor_decl->isMoveConstructor() &&
8238 cxx_record_decl->hasTrivialMoveConstructor()))) {
8239 cxx_ctor_decl->setDefaulted();
8240 cxx_ctor_decl->setTrivial(true);
8241 } else if (cxx_dtor_decl) {
8242 if (cxx_record_decl->hasTrivialDestructor()) {
8243 cxx_dtor_decl->setDefaulted();
8244 cxx_dtor_decl->setTrivial(true);
8245 }
8246 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8247 cxx_record_decl->hasTrivialCopyAssignment()) ||
8248 (cxx_method_decl->isMoveAssignmentOperator() &&
8249 cxx_record_decl->hasTrivialMoveAssignment())) {
8250 cxx_method_decl->setDefaulted();
8251 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008252 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008253 }
8254
Greg Claytond8d4a572015-08-11 21:38:15 +00008255#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008256 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008257#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008258
Kate Stoneb9c1b512016-09-06 20:57:50 +00008259 return cxx_method_decl;
8260}
Greg Claytond8d4a572015-08-11 21:38:15 +00008261
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00008262void ClangASTContext::AddMethodOverridesForCXXRecordType(
8263 lldb::opaque_compiler_type_t type) {
8264 if (auto *record = GetAsCXXRecordDecl(type))
8265 for (auto *method : record->methods())
8266 addOverridesForMethod(method);
8267}
8268
Greg Claytond8d4a572015-08-11 21:38:15 +00008269#pragma mark C++ Base Classes
8270
Zachary Turner970f38e2018-10-25 20:44:56 +00008271std::unique_ptr<clang::CXXBaseSpecifier>
Kate Stoneb9c1b512016-09-06 20:57:50 +00008272ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8273 AccessType access, bool is_virtual,
8274 bool base_of_class) {
Zachary Turner970f38e2018-10-25 20:44:56 +00008275 if (!type)
8276 return nullptr;
8277
Jonas Devliegherea8f3ae72019-08-14 22:19:23 +00008278 return std::make_unique<clang::CXXBaseSpecifier>(
Zachary Turner970f38e2018-10-25 20:44:56 +00008279 clang::SourceRange(), is_virtual, base_of_class,
8280 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8281 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8282 clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008283}
8284
Zachary Turner970f38e2018-10-25 20:44:56 +00008285bool ClangASTContext::TransferBaseClasses(
Kate Stoneb9c1b512016-09-06 20:57:50 +00008286 lldb::opaque_compiler_type_t type,
Zachary Turner970f38e2018-10-25 20:44:56 +00008287 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) {
8288 if (!type)
8289 return false;
8290 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8291 if (!cxx_record_decl)
8292 return false;
8293 std::vector<clang::CXXBaseSpecifier *> raw_bases;
8294 raw_bases.reserve(bases.size());
8295
8296 // Clang will make a copy of them, so it's ok that we pass pointers that we're
8297 // about to destroy.
8298 for (auto &b : bases)
8299 raw_bases.push_back(b.get());
8300 cxx_record_decl->setBases(raw_bases.data(), raw_bases.size());
8301 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008302}
8303
8304bool ClangASTContext::SetObjCSuperClass(
8305 const CompilerType &type, const CompilerType &superclass_clang_type) {
8306 ClangASTContext *ast =
8307 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8308 if (!ast)
8309 return false;
8310 clang::ASTContext *clang_ast = ast->getASTContext();
8311
8312 if (type && superclass_clang_type.IsValid() &&
8313 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8314 clang::ObjCInterfaceDecl *class_interface_decl =
8315 GetAsObjCInterfaceDecl(type);
8316 clang::ObjCInterfaceDecl *super_interface_decl =
8317 GetAsObjCInterfaceDecl(superclass_clang_type);
8318 if (class_interface_decl && super_interface_decl) {
8319 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8320 clang_ast->getObjCInterfaceType(super_interface_decl)));
8321 return true;
8322 }
8323 }
8324 return false;
8325}
8326
8327bool ClangASTContext::AddObjCClassProperty(
8328 const CompilerType &type, const char *property_name,
8329 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8330 const char *property_setter_name, const char *property_getter_name,
8331 uint32_t property_attributes, ClangASTMetadata *metadata) {
8332 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8333 property_name[0] == '\0')
8334 return false;
8335 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8336 if (!ast)
8337 return false;
8338 clang::ASTContext *clang_ast = ast->getASTContext();
8339
8340 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008341 if (!class_interface_decl)
8342 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008343
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008344 CompilerType property_clang_type_to_access;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008345
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008346 if (property_clang_type.IsValid())
8347 property_clang_type_to_access = property_clang_type;
8348 else if (ivar_decl)
8349 property_clang_type_to_access =
8350 CompilerType(ast, ivar_decl->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008351
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008352 if (!class_interface_decl || !property_clang_type_to_access.IsValid())
8353 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008354
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008355 clang::TypeSourceInfo *prop_type_source;
8356 if (ivar_decl)
8357 prop_type_source =
8358 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8359 else
8360 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8361 ClangUtil::GetQualType(property_clang_type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00008362
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008363 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8364 *clang_ast, class_interface_decl,
8365 clang::SourceLocation(), // Source Location
8366 &clang_ast->Idents.get(property_name),
8367 clang::SourceLocation(), // Source Location for AT
8368 clang::SourceLocation(), // Source location for (
8369 ivar_decl ? ivar_decl->getType()
8370 : ClangUtil::GetQualType(property_clang_type),
8371 prop_type_source);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008372
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008373 if (!property_decl)
8374 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008375
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008376 if (metadata)
8377 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008378
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008379 class_interface_decl->addDecl(property_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008380
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008381 clang::Selector setter_sel, getter_sel;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008382
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008383 if (property_setter_name) {
8384 std::string property_setter_no_colon(property_setter_name,
8385 strlen(property_setter_name) - 1);
8386 clang::IdentifierInfo *setter_ident =
8387 &clang_ast->Idents.get(property_setter_no_colon);
8388 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8389 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8390 std::string setter_sel_string("set");
8391 setter_sel_string.push_back(::toupper(property_name[0]));
8392 setter_sel_string.append(&property_name[1]);
8393 clang::IdentifierInfo *setter_ident =
8394 &clang_ast->Idents.get(setter_sel_string);
8395 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8396 }
8397 property_decl->setSetterName(setter_sel);
8398 property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008399
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008400 if (property_getter_name != nullptr) {
8401 clang::IdentifierInfo *getter_ident =
8402 &clang_ast->Idents.get(property_getter_name);
8403 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8404 } else {
8405 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_name);
8406 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8407 }
8408 property_decl->setGetterName(getter_sel);
8409 property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008410
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008411 if (ivar_decl)
8412 property_decl->setPropertyIvarDecl(ivar_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008413
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008414 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8415 property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
8416 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8417 property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
8418 if (property_attributes & DW_APPLE_PROPERTY_assign)
8419 property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
8420 if (property_attributes & DW_APPLE_PROPERTY_retain)
8421 property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
8422 if (property_attributes & DW_APPLE_PROPERTY_copy)
8423 property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
8424 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8425 property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
8426 if (property_attributes & ObjCPropertyDecl::OBJC_PR_nullability)
8427 property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
8428 if (property_attributes & ObjCPropertyDecl::OBJC_PR_null_resettable)
8429 property_decl->setPropertyAttributes(
8430 ObjCPropertyDecl::OBJC_PR_null_resettable);
8431 if (property_attributes & ObjCPropertyDecl::OBJC_PR_class)
8432 property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_class);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008433
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008434 const bool isInstance =
8435 (property_attributes & ObjCPropertyDecl::OBJC_PR_class) == 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008436
Adrian Prantlc0eeea52019-11-21 17:21:49 -08008437 clang::ObjCMethodDecl *getter = nullptr;
8438 if (!getter_sel.isNull())
8439 getter = isInstance ? class_interface_decl->lookupInstanceMethod(getter_sel)
8440 : class_interface_decl->lookupClassMethod(getter_sel);
8441 if (!getter_sel.isNull() && !getter) {
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008442 const bool isVariadic = false;
8443 const bool isPropertyAccessor = false;
8444 const bool isSynthesizedAccessorStub = false;
8445 const bool isImplicitlyDeclared = true;
8446 const bool isDefined = false;
8447 const clang::ObjCMethodDecl::ImplementationControl impControl =
8448 clang::ObjCMethodDecl::None;
8449 const bool HasRelatedResultType = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008450
Adrian Prantlc0eeea52019-11-21 17:21:49 -08008451 getter = clang::ObjCMethodDecl::Create(
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008452 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8453 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8454 nullptr, class_interface_decl, isInstance, isVariadic,
8455 isPropertyAccessor, isSynthesizedAccessorStub, isImplicitlyDeclared,
8456 isDefined, impControl, HasRelatedResultType);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008457
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008458 if (getter) {
Adrian Prantlc0eeea52019-11-21 17:21:49 -08008459 if (metadata)
8460 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
Adrian Prantl8b40bdb2019-11-22 10:03:25 -08008461
8462 getter->setMethodParams(*clang_ast,
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008463 llvm::ArrayRef<clang::ParmVarDecl *>(),
8464 llvm::ArrayRef<clang::SourceLocation>());
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008465 class_interface_decl->addDecl(getter);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008466 }
8467 }
Adrian Prantlc0eeea52019-11-21 17:21:49 -08008468 if (getter) {
8469 getter->setPropertyAccessor(true);
8470 property_decl->setGetterMethodDecl(getter);
8471 }
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008472
Adrian Prantlc0eeea52019-11-21 17:21:49 -08008473 clang::ObjCMethodDecl *setter = nullptr;
8474 setter = isInstance ? class_interface_decl->lookupInstanceMethod(setter_sel)
8475 : class_interface_decl->lookupClassMethod(setter_sel);
8476 if (!setter_sel.isNull() && !setter) {
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008477 clang::QualType result_type = clang_ast->VoidTy;
8478 const bool isVariadic = false;
8479 const bool isPropertyAccessor = true;
8480 const bool isSynthesizedAccessorStub = false;
8481 const bool isImplicitlyDeclared = true;
8482 const bool isDefined = false;
8483 const clang::ObjCMethodDecl::ImplementationControl impControl =
8484 clang::ObjCMethodDecl::None;
8485 const bool HasRelatedResultType = false;
8486
Adrian Prantlc0eeea52019-11-21 17:21:49 -08008487 setter = clang::ObjCMethodDecl::Create(
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008488 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8489 setter_sel, result_type, nullptr, class_interface_decl, isInstance,
8490 isVariadic, isPropertyAccessor, isSynthesizedAccessorStub,
8491 isImplicitlyDeclared, isDefined, impControl, HasRelatedResultType);
8492
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008493 if (setter) {
Adrian Prantlc0eeea52019-11-21 17:21:49 -08008494 if (metadata)
8495 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8496
8497 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
Adrian Prantlc0eeea52019-11-21 17:21:49 -08008498 params.push_back(clang::ParmVarDecl::Create(
8499 *clang_ast, setter, clang::SourceLocation(), clang::SourceLocation(),
8500 nullptr, // anonymous
8501 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8502 clang::SC_Auto, nullptr));
8503
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008504 setter->setMethodParams(*clang_ast,
8505 llvm::ArrayRef<clang::ParmVarDecl *>(params),
8506 llvm::ArrayRef<clang::SourceLocation>());
8507
8508 class_interface_decl->addDecl(setter);
8509 }
8510 }
Adrian Prantlc0eeea52019-11-21 17:21:49 -08008511 if (setter) {
8512 setter->setPropertyAccessor(true);
8513 property_decl->setSetterMethodDecl(setter);
8514 }
Adrian Prantlbc8e88e2019-11-21 15:40:50 -08008515
8516 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008517}
8518
8519bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8520 bool check_superclass) {
8521 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8522 if (class_interface_decl)
8523 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8524 return false;
8525}
8526
8527clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8528 const CompilerType &type,
8529 const char *name, // the full symbol name as seen in the symbol table
8530 // (lldb::opaque_compiler_type_t type, "-[NString
8531 // stringWithCString:]")
8532 const CompilerType &method_clang_type, lldb::AccessType access,
8533 bool is_artificial, bool is_variadic) {
8534 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008535 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008536
Kate Stoneb9c1b512016-09-06 20:57:50 +00008537 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8538
8539 if (class_interface_decl == nullptr)
8540 return nullptr;
8541 ClangASTContext *lldb_ast =
8542 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8543 if (lldb_ast == nullptr)
8544 return nullptr;
8545 clang::ASTContext *ast = lldb_ast->getASTContext();
8546
8547 const char *selector_start = ::strchr(name, ' ');
8548 if (selector_start == nullptr)
8549 return nullptr;
8550
8551 selector_start++;
8552 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8553
8554 size_t len = 0;
8555 const char *start;
8556 // printf ("name = '%s'\n", name);
8557
8558 unsigned num_selectors_with_args = 0;
8559 for (start = selector_start; start && *start != '\0' && *start != ']';
8560 start += len) {
8561 len = ::strcspn(start, ":]");
8562 bool has_arg = (start[len] == ':');
8563 if (has_arg)
8564 ++num_selectors_with_args;
8565 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8566 if (has_arg)
8567 len += 1;
8568 }
8569
8570 if (selector_idents.size() == 0)
8571 return nullptr;
8572
8573 clang::Selector method_selector = ast->Selectors.getSelector(
8574 num_selectors_with_args ? selector_idents.size() : 0,
8575 selector_idents.data());
8576
8577 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8578
8579 // Populate the method decl with parameter decls
8580 const clang::Type *method_type(method_qual_type.getTypePtr());
8581
8582 if (method_type == nullptr)
8583 return nullptr;
8584
8585 const clang::FunctionProtoType *method_function_prototype(
8586 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8587
8588 if (!method_function_prototype)
8589 return nullptr;
8590
Adrian Prantl454acae2019-11-08 08:58:50 -08008591 const bool isInstance = (name[0] == '-');
Adrian Prantl8204d9f2019-11-08 09:52:58 -08008592 const bool isVariadic = is_variadic;
Adrian Prantl454acae2019-11-08 08:58:50 -08008593 const bool isPropertyAccessor = false;
8594 const bool isSynthesizedAccessorStub = false;
8595 /// Force this to true because we don't have source locations.
8596 const bool isImplicitlyDeclared = true;
8597 const bool isDefined = false;
8598 const clang::ObjCMethodDecl::ImplementationControl impControl =
Kate Stoneb9c1b512016-09-06 20:57:50 +00008599 clang::ObjCMethodDecl::None;
Adrian Prantl454acae2019-11-08 08:58:50 -08008600 const bool HasRelatedResultType = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008601
8602 const unsigned num_args = method_function_prototype->getNumParams();
8603
8604 if (num_args != num_selectors_with_args)
8605 return nullptr; // some debug information is corrupt. We are not going to
8606 // deal with it.
8607
8608 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8609 *ast,
8610 clang::SourceLocation(), // beginLoc,
8611 clang::SourceLocation(), // endLoc,
8612 method_selector, method_function_prototype->getReturnType(),
8613 nullptr, // TypeSourceInfo *ResultTInfo,
8614 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8615 ClangUtil::GetQualType(type)),
Adrian Prantl454acae2019-11-08 08:58:50 -08008616 isInstance, isVariadic, isPropertyAccessor, isSynthesizedAccessorStub,
8617 isImplicitlyDeclared, isDefined, impControl, HasRelatedResultType);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008618
8619 if (objc_method_decl == nullptr)
8620 return nullptr;
8621
8622 if (num_args > 0) {
8623 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8624
8625 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8626 params.push_back(clang::ParmVarDecl::Create(
8627 *ast, objc_method_decl, clang::SourceLocation(),
8628 clang::SourceLocation(),
8629 nullptr, // anonymous
8630 method_function_prototype->getParamType(param_index), nullptr,
8631 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008632 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008633
Kate Stoneb9c1b512016-09-06 20:57:50 +00008634 objc_method_decl->setMethodParams(
8635 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8636 llvm::ArrayRef<clang::SourceLocation>());
8637 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008638
Kate Stoneb9c1b512016-09-06 20:57:50 +00008639 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008640
Greg Claytond8d4a572015-08-11 21:38:15 +00008641#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008642 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008643#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008644
8645 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008646}
8647
Kate Stoneb9c1b512016-09-06 20:57:50 +00008648bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8649 bool has_extern) {
8650 if (!type)
8651 return false;
8652
8653 clang::QualType qual_type(GetCanonicalQualType(type));
8654
8655 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8656 switch (type_class) {
8657 case clang::Type::Record: {
8658 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8659 if (cxx_record_decl) {
8660 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8661 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8662 return true;
8663 }
8664 } break;
8665
8666 case clang::Type::Enum: {
8667 clang::EnumDecl *enum_decl =
8668 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8669 if (enum_decl) {
8670 enum_decl->setHasExternalLexicalStorage(has_extern);
8671 enum_decl->setHasExternalVisibleStorage(has_extern);
8672 return true;
8673 }
8674 } break;
8675
8676 case clang::Type::ObjCObject:
8677 case clang::Type::ObjCInterface: {
8678 const clang::ObjCObjectType *objc_class_type =
8679 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8680 assert(objc_class_type);
8681 if (objc_class_type) {
8682 clang::ObjCInterfaceDecl *class_interface_decl =
8683 objc_class_type->getInterface();
8684
8685 if (class_interface_decl) {
8686 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8687 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8688 return true;
8689 }
8690 }
8691 } break;
8692
8693 case clang::Type::Typedef:
8694 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8695 ->getDecl()
8696 ->getUnderlyingType()
8697 .getAsOpaquePtr(),
8698 has_extern);
8699
8700 case clang::Type::Auto:
8701 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8702 ->getDeducedType()
8703 .getAsOpaquePtr(),
8704 has_extern);
8705
8706 case clang::Type::Elaborated:
8707 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8708 ->getNamedType()
8709 .getAsOpaquePtr(),
8710 has_extern);
8711
8712 case clang::Type::Paren:
8713 return SetHasExternalStorage(
8714 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8715 has_extern);
8716
8717 default:
8718 break;
8719 }
8720 return false;
8721}
Greg Claytond8d4a572015-08-11 21:38:15 +00008722
8723#pragma mark TagDecl
8724
Kate Stoneb9c1b512016-09-06 20:57:50 +00008725bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8726 clang::QualType qual_type(ClangUtil::GetQualType(type));
8727 if (!qual_type.isNull()) {
8728 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8729 if (tag_type) {
8730 clang::TagDecl *tag_decl = tag_type->getDecl();
8731 if (tag_decl) {
8732 tag_decl->startDefinition();
8733 return true;
8734 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008735 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008736
8737 const clang::ObjCObjectType *object_type =
8738 qual_type->getAs<clang::ObjCObjectType>();
8739 if (object_type) {
8740 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8741 if (interface_decl) {
8742 interface_decl->startDefinition();
8743 return true;
8744 }
8745 }
8746 }
8747 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008748}
8749
Kate Stoneb9c1b512016-09-06 20:57:50 +00008750bool ClangASTContext::CompleteTagDeclarationDefinition(
8751 const CompilerType &type) {
8752 clang::QualType qual_type(ClangUtil::GetQualType(type));
8753 if (!qual_type.isNull()) {
8754 // Make sure we use the same methodology as
Adrian Prantl05097242018-04-30 16:49:04 +00008755 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end
8756 // the definition. Previously we were calling
Kate Stoneb9c1b512016-09-06 20:57:50 +00008757 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8758 if (tag_type) {
8759 clang::TagDecl *tag_decl = tag_type->getDecl();
8760 if (tag_decl) {
8761 clang::CXXRecordDecl *cxx_record_decl =
8762 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8763
8764 if (cxx_record_decl) {
8765 if (!cxx_record_decl->isCompleteDefinition())
8766 cxx_record_decl->completeDefinition();
8767 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8768 cxx_record_decl->setHasExternalLexicalStorage(false);
8769 cxx_record_decl->setHasExternalVisibleStorage(false);
8770 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008771 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008772 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008773 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008774
8775 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8776
8777 if (enutype) {
8778 clang::EnumDecl *enum_decl = enutype->getDecl();
8779
8780 if (enum_decl) {
8781 if (!enum_decl->isCompleteDefinition()) {
8782 ClangASTContext *lldb_ast =
8783 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8784 if (lldb_ast == nullptr)
8785 return false;
8786 clang::ASTContext *ast = lldb_ast->getASTContext();
8787
8788 /// TODO This really needs to be fixed.
8789
8790 QualType integer_type(enum_decl->getIntegerType());
8791 if (!integer_type.isNull()) {
8792 unsigned NumPositiveBits = 1;
8793 unsigned NumNegativeBits = 0;
8794
8795 clang::QualType promotion_qual_type;
8796 // If the enum integer type is less than an integer in bit width,
8797 // then we must promote it to an integer size.
8798 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8799 ast->getTypeSize(ast->IntTy)) {
8800 if (enum_decl->getIntegerType()->isSignedIntegerType())
8801 promotion_qual_type = ast->IntTy;
8802 else
8803 promotion_qual_type = ast->UnsignedIntTy;
8804 } else
8805 promotion_qual_type = enum_decl->getIntegerType();
8806
8807 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8808 promotion_qual_type, NumPositiveBits,
8809 NumNegativeBits);
8810 }
8811 }
8812 return true;
8813 }
8814 }
8815 }
8816 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008817}
8818
Aleksandr Urakov709426b2018-09-10 08:08:43 +00008819clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008820 const CompilerType &enum_type, const Declaration &decl, const char *name,
Zachary Turner1639c6b2018-12-17 16:15:13 +00008821 const llvm::APSInt &value) {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008822
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008823 if (!enum_type || ConstString(name).IsEmpty())
8824 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008825
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008826 lldbassert(enum_type.GetTypeSystem() == static_cast<TypeSystem *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +00008827
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008828 lldb::opaque_compiler_type_t enum_opaque_compiler_type =
8829 enum_type.GetOpaqueQualType();
8830
8831 if (!enum_opaque_compiler_type)
8832 return nullptr;
8833
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008834 clang::QualType enum_qual_type(
8835 GetCanonicalQualType(enum_opaque_compiler_type));
8836
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008837 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8838
8839 if (!clang_type)
8840 return nullptr;
8841
8842 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8843
8844 if (!enutype)
8845 return nullptr;
8846
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008847 clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create(
8848 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8849 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
Zachary Turner1639c6b2018-12-17 16:15:13 +00008850 clang::QualType(enutype, 0), nullptr, value);
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008851
8852 if (!enumerator_decl)
8853 return nullptr;
8854
8855 enutype->getDecl()->addDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008856
8857#ifdef LLDB_CONFIGURATION_DEBUG
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008858 VerifyDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008859#endif
8860
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008861 return enumerator_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008862}
8863
Zachary Turner1639c6b2018-12-17 16:15:13 +00008864clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
8865 const CompilerType &enum_type, const Declaration &decl, const char *name,
8866 int64_t enum_value, uint32_t enum_value_bit_size) {
8867 CompilerType underlying_type =
8868 GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
8869 bool is_signed = false;
8870 underlying_type.IsIntegerType(is_signed);
8871
8872 llvm::APSInt value(enum_value_bit_size, is_signed);
8873 value = enum_value;
8874
8875 return AddEnumerationValueToEnumerationType(enum_type, decl, name, value);
8876}
8877
Greg Claytona1e5dc82015-08-11 22:53:00 +00008878CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00008879ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8880 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8881 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8882 if (clang_type) {
8883 const clang::EnumType *enutype =
8884 llvm::dyn_cast<clang::EnumType>(clang_type);
8885 if (enutype) {
8886 clang::EnumDecl *enum_decl = enutype->getDecl();
8887 if (enum_decl)
Alex Langfordbddab072019-08-13 19:40:36 +00008888 return CompilerType(this, enum_decl->getIntegerType().getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00008889 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008890 }
8891 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008892}
8893
Kate Stoneb9c1b512016-09-06 20:57:50 +00008894CompilerType
8895ClangASTContext::CreateMemberPointerType(const CompilerType &type,
8896 const CompilerType &pointee_type) {
8897 if (type && pointee_type.IsValid() &&
8898 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8899 ClangASTContext *ast =
8900 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8901 if (!ast)
8902 return CompilerType();
Alex Langfordbddab072019-08-13 19:40:36 +00008903 return CompilerType(ast, ast->getASTContext()
8904 ->getMemberPointerType(
8905 ClangUtil::GetQualType(pointee_type),
8906 ClangUtil::GetQualType(type).getTypePtr())
8907 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008908 }
8909 return CompilerType();
8910}
Greg Claytond8d4a572015-08-11 21:38:15 +00008911
Greg Claytond8d4a572015-08-11 21:38:15 +00008912// Dumping types
Greg Claytond8d4a572015-08-11 21:38:15 +00008913#define DEPTH_INCREMENT 2
8914
Adrian Prantl0c72a422019-03-07 20:20:02 +00008915#ifndef NDEBUG
8916LLVM_DUMP_METHOD void
8917ClangASTContext::dump(lldb::opaque_compiler_type_t type) const {
8918 if (!type)
8919 return;
8920 clang::QualType qual_type(GetQualType(type));
8921 qual_type.dump();
8922}
8923#endif
8924
Zachary Turner49110232018-11-05 17:40:28 +00008925void ClangASTContext::Dump(Stream &s) {
Zachary Turner115209e2018-11-05 19:25:39 +00008926 Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl());
Zachary Turner49110232018-11-05 17:40:28 +00008927 tu->dump(s.AsRawOstream());
8928}
8929
Shafik Yaghmour5f469822019-10-11 16:36:20 +00008930void ClangASTContext::DumpFromSymbolFile(Stream &s,
8931 llvm::StringRef symbol_name) {
8932 SymbolFile *symfile = GetSymbolFile();
8933
8934 if (!symfile)
8935 return;
8936
8937 lldb_private::TypeList type_list;
8938 symfile->GetTypes(nullptr, eTypeClassAny, type_list);
8939 size_t ntypes = type_list.GetSize();
8940
8941 for (size_t i = 0; i < ntypes; ++i) {
8942 TypeSP type = type_list.GetTypeAtIndex(i);
8943
8944 if (!symbol_name.empty())
shafikde2c7ca2019-10-28 14:26:54 -07008945 if (symbol_name != type->GetName().GetStringRef())
Shafik Yaghmour5f469822019-10-11 16:36:20 +00008946 continue;
8947
8948 s << type->GetName().AsCString() << "\n";
8949
Adrian Prantlc0eeea52019-11-21 17:21:49 -08008950 CompilerType full_type = type->GetFullCompilerType();
8951 if (clang::TagDecl *tag_decl = GetAsTagDecl(full_type)) {
Shafik Yaghmour5f469822019-10-11 16:36:20 +00008952 tag_decl->dump(s.AsRawOstream());
Adrian Prantlc0eeea52019-11-21 17:21:49 -08008953 continue;
Shafik Yaghmour5f469822019-10-11 16:36:20 +00008954 }
Adrian Prantlc0eeea52019-11-21 17:21:49 -08008955 if (clang::TypedefNameDecl *typedef_decl = GetAsTypedefDecl(full_type)) {
8956 typedef_decl->dump(s.AsRawOstream());
8957 continue;
8958 }
8959 if (auto *objc_obj = llvm::dyn_cast<clang::ObjCObjectType>(
8960 ClangUtil::GetQualType(full_type).getTypePtr())) {
8961 if (clang::ObjCInterfaceDecl *interface_decl = objc_obj->getInterface()) {
8962 interface_decl->dump(s.AsRawOstream());
8963 continue;
8964 }
8965 }
8966 GetCanonicalQualType(full_type.GetOpaqueQualType()).dump(s.AsRawOstream());
Shafik Yaghmour5f469822019-10-11 16:36:20 +00008967 }
8968}
8969
Kate Stoneb9c1b512016-09-06 20:57:50 +00008970void ClangASTContext::DumpValue(
8971 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00008972 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008973 lldb::offset_t data_byte_offset, size_t data_byte_size,
8974 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
8975 bool show_summary, bool verbose, uint32_t depth) {
8976 if (!type)
8977 return;
8978
8979 clang::QualType qual_type(GetQualType(type));
8980 switch (qual_type->getTypeClass()) {
8981 case clang::Type::Record:
8982 if (GetCompleteType(type)) {
8983 const clang::RecordType *record_type =
8984 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8985 const clang::RecordDecl *record_decl = record_type->getDecl();
8986 assert(record_decl);
8987 uint32_t field_bit_offset = 0;
8988 uint32_t field_byte_offset = 0;
8989 const clang::ASTRecordLayout &record_layout =
8990 getASTContext()->getASTRecordLayout(record_decl);
8991 uint32_t child_idx = 0;
8992
8993 const clang::CXXRecordDecl *cxx_record_decl =
8994 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8995 if (cxx_record_decl) {
8996 // We might have base classes to print out first
8997 clang::CXXRecordDecl::base_class_const_iterator base_class,
8998 base_class_end;
8999 for (base_class = cxx_record_decl->bases_begin(),
9000 base_class_end = cxx_record_decl->bases_end();
9001 base_class != base_class_end; ++base_class) {
9002 const clang::CXXRecordDecl *base_class_decl =
9003 llvm::cast<clang::CXXRecordDecl>(
9004 base_class->getType()->getAs<clang::RecordType>()->getDecl());
9005
9006 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00009007 if (!verbose && !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00009008 continue;
9009
9010 if (base_class->isVirtual())
9011 field_bit_offset =
9012 record_layout.getVBaseClassOffset(base_class_decl)
9013 .getQuantity() *
9014 8;
9015 else
9016 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
9017 .getQuantity() *
9018 8;
9019 field_byte_offset = field_bit_offset / 8;
9020 assert(field_bit_offset % 8 == 0);
9021 if (child_idx == 0)
9022 s->PutChar('{');
9023 else
9024 s->PutChar(',');
9025
9026 clang::QualType base_class_qual_type = base_class->getType();
9027 std::string base_class_type_name(base_class_qual_type.getAsString());
9028
9029 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00009030 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
9031 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009032
9033 clang::TypeInfo base_class_type_info =
9034 getASTContext()->getTypeInfo(base_class_qual_type);
9035
9036 // Dump the value of the member
Alex Langfordbddab072019-08-13 19:40:36 +00009037 CompilerType base_clang_type(this,
9038 base_class_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009039 base_clang_type.DumpValue(
9040 exe_ctx,
9041 s, // Stream to dump to
9042 base_clang_type
9043 .GetFormat(), // The format with which to display the member
9044 data, // Data buffer containing all bytes for this type
9045 data_byte_offset + field_byte_offset, // Offset into "data" where
9046 // to grab value from
9047 base_class_type_info.Width / 8, // Size of this type in bytes
9048 0, // Bitfield bit size
9049 0, // Bitfield bit offset
9050 show_types, // Boolean indicating if we should show the variable
9051 // types
9052 show_summary, // Boolean indicating if we should show a summary
9053 // for the current type
9054 verbose, // Verbose output?
9055 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9056 // children
9057
9058 ++child_idx;
9059 }
9060 }
9061 uint32_t field_idx = 0;
9062 clang::RecordDecl::field_iterator field, field_end;
9063 for (field = record_decl->field_begin(),
9064 field_end = record_decl->field_end();
9065 field != field_end; ++field, ++field_idx, ++child_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009066 // Print the starting squiggly bracket (if this is the first member) or
9067 // comma (for member 2 and beyond) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009068 if (child_idx == 0)
9069 s->PutChar('{');
9070 else
9071 s->PutChar(',');
9072
9073 // Indent
9074 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
9075
9076 clang::QualType field_type = field->getType();
9077 // Print the member type if requested
Adrian Prantl05097242018-04-30 16:49:04 +00009078 // Figure out the type byte size (field_type_info.first) and alignment
9079 // (field_type_info.second) from the AST context.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009080 clang::TypeInfo field_type_info =
9081 getASTContext()->getTypeInfo(field_type);
9082 assert(field_idx < record_layout.getFieldCount());
9083 // Figure out the field offset within the current struct/union/class
9084 // type
9085 field_bit_offset = record_layout.getFieldOffset(field_idx);
9086 field_byte_offset = field_bit_offset / 8;
9087 uint32_t field_bitfield_bit_size = 0;
9088 uint32_t field_bitfield_bit_offset = 0;
Raphael Isemann02e91132019-11-20 12:09:19 +01009089 if (FieldIsBitfield(*field, field_bitfield_bit_size))
Kate Stoneb9c1b512016-09-06 20:57:50 +00009090 field_bitfield_bit_offset = field_bit_offset % 8;
9091
9092 if (show_types) {
9093 std::string field_type_name(field_type.getAsString());
9094 if (field_bitfield_bit_size > 0)
9095 s->Printf("(%s:%u) ", field_type_name.c_str(),
9096 field_bitfield_bit_size);
9097 else
9098 s->Printf("(%s) ", field_type_name.c_str());
9099 }
9100 // Print the member name and equal sign
9101 s->Printf("%s = ", field->getNameAsString().c_str());
9102
9103 // Dump the value of the member
Alex Langfordbddab072019-08-13 19:40:36 +00009104 CompilerType field_clang_type(this, field_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009105 field_clang_type.DumpValue(
9106 exe_ctx,
9107 s, // Stream to dump to
9108 field_clang_type
9109 .GetFormat(), // The format with which to display the member
9110 data, // Data buffer containing all bytes for this type
9111 data_byte_offset + field_byte_offset, // Offset into "data" where to
9112 // grab value from
9113 field_type_info.Width / 8, // Size of this type in bytes
9114 field_bitfield_bit_size, // Bitfield bit size
9115 field_bitfield_bit_offset, // Bitfield bit offset
9116 show_types, // Boolean indicating if we should show the variable
9117 // types
9118 show_summary, // Boolean indicating if we should show a summary for
9119 // the current type
9120 verbose, // Verbose output?
9121 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9122 // children
9123 }
9124
9125 // Indent the trailing squiggly bracket
9126 if (child_idx > 0)
9127 s->Printf("\n%*s}", depth, "");
9128 }
9129 return;
9130
9131 case clang::Type::Enum:
9132 if (GetCompleteType(type)) {
9133 const clang::EnumType *enutype =
9134 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9135 const clang::EnumDecl *enum_decl = enutype->getDecl();
9136 assert(enum_decl);
9137 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9138 lldb::offset_t offset = data_byte_offset;
9139 const int64_t enum_value = data.GetMaxU64Bitfield(
9140 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9141 for (enum_pos = enum_decl->enumerator_begin(),
9142 enum_end_pos = enum_decl->enumerator_end();
9143 enum_pos != enum_end_pos; ++enum_pos) {
9144 if (enum_pos->getInitVal() == enum_value) {
9145 s->Printf("%s", enum_pos->getNameAsString().c_str());
9146 return;
9147 }
9148 }
Adrian Prantl05097242018-04-30 16:49:04 +00009149 // If we have gotten here we didn't get find the enumerator in the enum
9150 // decl, so just print the integer.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009151 s->Printf("%" PRIi64, enum_value);
9152 }
9153 return;
9154
9155 case clang::Type::ConstantArray: {
9156 const clang::ConstantArrayType *array =
9157 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9158 bool is_array_of_characters = false;
9159 clang::QualType element_qual_type = array->getElementType();
9160
9161 const clang::Type *canonical_type =
9162 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9163 if (canonical_type)
9164 is_array_of_characters = canonical_type->isCharType();
9165
9166 const uint64_t element_count = array->getSize().getLimitedValue();
9167
9168 clang::TypeInfo field_type_info =
9169 getASTContext()->getTypeInfo(element_qual_type);
9170
9171 uint32_t element_idx = 0;
9172 uint32_t element_offset = 0;
9173 uint64_t element_byte_size = field_type_info.Width / 8;
9174 uint32_t element_stride = element_byte_size;
9175
9176 if (is_array_of_characters) {
9177 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009178 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9179 element_byte_size, element_count, UINT32_MAX,
9180 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009181 s->PutChar('"');
9182 return;
9183 } else {
Alex Langfordbddab072019-08-13 19:40:36 +00009184 CompilerType element_clang_type(this, element_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009185 lldb::Format element_format = element_clang_type.GetFormat();
9186
9187 for (element_idx = 0; element_idx < element_count; ++element_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009188 // Print the starting squiggly bracket (if this is the first member) or
9189 // comman (for member 2 and beyong) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009190 if (element_idx == 0)
9191 s->PutChar('{');
9192 else
9193 s->PutChar(',');
9194
9195 // Indent and print the index
9196 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9197
9198 // Figure out the field offset within the current struct/union/class
9199 // type
9200 element_offset = element_idx * element_stride;
9201
9202 // Dump the value of the member
9203 element_clang_type.DumpValue(
9204 exe_ctx,
9205 s, // Stream to dump to
9206 element_format, // The format with which to display the element
9207 data, // Data buffer containing all bytes for this type
9208 data_byte_offset +
9209 element_offset, // Offset into "data" where to grab value from
9210 element_byte_size, // Size of this type in bytes
9211 0, // Bitfield bit size
9212 0, // Bitfield bit offset
9213 show_types, // Boolean indicating if we should show the variable
9214 // types
9215 show_summary, // Boolean indicating if we should show a summary for
9216 // the current type
9217 verbose, // Verbose output?
9218 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9219 // children
9220 }
9221
9222 // Indent the trailing squiggly bracket
9223 if (element_idx > 0)
9224 s->Printf("\n%*s}", depth, "");
9225 }
9226 }
9227 return;
9228
9229 case clang::Type::Typedef: {
9230 clang::QualType typedef_qual_type =
9231 llvm::cast<clang::TypedefType>(qual_type)
9232 ->getDecl()
9233 ->getUnderlyingType();
9234
Alex Langfordbddab072019-08-13 19:40:36 +00009235 CompilerType typedef_clang_type(this, typedef_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009236 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9237 clang::TypeInfo typedef_type_info =
9238 getASTContext()->getTypeInfo(typedef_qual_type);
9239 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9240
9241 return typedef_clang_type.DumpValue(
9242 exe_ctx,
9243 s, // Stream to dump to
9244 typedef_format, // The format with which to display the element
9245 data, // Data buffer containing all bytes for this type
9246 data_byte_offset, // Offset into "data" where to grab value from
9247 typedef_byte_size, // Size of this type in bytes
9248 bitfield_bit_size, // Bitfield bit size
9249 bitfield_bit_offset, // Bitfield bit offset
9250 show_types, // Boolean indicating if we should show the variable types
9251 show_summary, // Boolean indicating if we should show a summary for the
9252 // current type
9253 verbose, // Verbose output?
9254 depth); // Scope depth for any types that have children
9255 } break;
9256
9257 case clang::Type::Auto: {
9258 clang::QualType elaborated_qual_type =
9259 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
Alex Langfordbddab072019-08-13 19:40:36 +00009260 CompilerType elaborated_clang_type(this,
9261 elaborated_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009262 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9263 clang::TypeInfo elaborated_type_info =
9264 getASTContext()->getTypeInfo(elaborated_qual_type);
9265 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9266
9267 return elaborated_clang_type.DumpValue(
9268 exe_ctx,
9269 s, // Stream to dump to
9270 elaborated_format, // The format with which to display the element
9271 data, // Data buffer containing all bytes for this type
9272 data_byte_offset, // Offset into "data" where to grab value from
9273 elaborated_byte_size, // Size of this type in bytes
9274 bitfield_bit_size, // Bitfield bit size
9275 bitfield_bit_offset, // Bitfield bit offset
9276 show_types, // Boolean indicating if we should show the variable types
9277 show_summary, // Boolean indicating if we should show a summary for the
9278 // current type
9279 verbose, // Verbose output?
9280 depth); // Scope depth for any types that have children
9281 } break;
9282
9283 case clang::Type::Elaborated: {
9284 clang::QualType elaborated_qual_type =
9285 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
Alex Langfordbddab072019-08-13 19:40:36 +00009286 CompilerType elaborated_clang_type(this,
9287 elaborated_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009288 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9289 clang::TypeInfo elaborated_type_info =
9290 getASTContext()->getTypeInfo(elaborated_qual_type);
9291 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9292
9293 return elaborated_clang_type.DumpValue(
9294 exe_ctx,
9295 s, // Stream to dump to
9296 elaborated_format, // The format with which to display the element
9297 data, // Data buffer containing all bytes for this type
9298 data_byte_offset, // Offset into "data" where to grab value from
9299 elaborated_byte_size, // Size of this type in bytes
9300 bitfield_bit_size, // Bitfield bit size
9301 bitfield_bit_offset, // Bitfield bit offset
9302 show_types, // Boolean indicating if we should show the variable types
9303 show_summary, // Boolean indicating if we should show a summary for the
9304 // current type
9305 verbose, // Verbose output?
9306 depth); // Scope depth for any types that have children
9307 } break;
9308
9309 case clang::Type::Paren: {
9310 clang::QualType desugar_qual_type =
9311 llvm::cast<clang::ParenType>(qual_type)->desugar();
Alex Langfordbddab072019-08-13 19:40:36 +00009312 CompilerType desugar_clang_type(this, desugar_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009313
9314 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9315 clang::TypeInfo desugar_type_info =
9316 getASTContext()->getTypeInfo(desugar_qual_type);
9317 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9318
9319 return desugar_clang_type.DumpValue(
9320 exe_ctx,
9321 s, // Stream to dump to
9322 desugar_format, // The format with which to display the element
9323 data, // Data buffer containing all bytes for this type
9324 data_byte_offset, // Offset into "data" where to grab value from
9325 desugar_byte_size, // Size of this type in bytes
9326 bitfield_bit_size, // Bitfield bit size
9327 bitfield_bit_offset, // Bitfield bit offset
9328 show_types, // Boolean indicating if we should show the variable types
9329 show_summary, // Boolean indicating if we should show a summary for the
9330 // current type
9331 verbose, // Verbose output?
9332 depth); // Scope depth for any types that have children
9333 } break;
9334
9335 default:
9336 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009337 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9338 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9339 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009340
9341 if (show_summary)
9342 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9343 break;
9344 }
9345}
9346
Frederic Rissd6470fb2019-10-08 15:35:58 +00009347static bool DumpEnumValue(const clang::QualType &qual_type, Stream *s,
9348 const DataExtractor &data, lldb::offset_t byte_offset,
9349 size_t byte_size, uint32_t bitfield_bit_offset,
9350 uint32_t bitfield_bit_size) {
9351 const clang::EnumType *enutype =
9352 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9353 const clang::EnumDecl *enum_decl = enutype->getDecl();
9354 assert(enum_decl);
Frederic Rissd6470fb2019-10-08 15:35:58 +00009355 lldb::offset_t offset = byte_offset;
Frederic Riss41ff3962019-10-08 15:35:59 +00009356 const uint64_t enum_svalue = data.GetMaxS64Bitfield(
Frederic Rissd6470fb2019-10-08 15:35:58 +00009357 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
Frederic Riss41ff3962019-10-08 15:35:59 +00009358 bool can_be_bitfield = true;
9359 uint64_t covered_bits = 0;
9360 int num_enumerators = 0;
Frederic Rissd6470fb2019-10-08 15:35:58 +00009361
Frederic Riss41ff3962019-10-08 15:35:59 +00009362 // Try to find an exact match for the value.
9363 // At the same time, we're applying a heuristic to determine whether we want
9364 // to print this enum as a bitfield. We're likely dealing with a bitfield if
9365 // every enumrator is either a one bit value or a superset of the previous
9366 // enumerators. Also 0 doesn't make sense when the enumerators are used as
9367 // flags.
Frederic Rissd6470fb2019-10-08 15:35:58 +00009368 for (auto enumerator : enum_decl->enumerators()) {
Frederic Riss41ff3962019-10-08 15:35:59 +00009369 uint64_t val = enumerator->getInitVal().getSExtValue();
Frederic Riss5d415b72019-10-08 17:59:02 +00009370 val = llvm::SignExtend64(val, 8*byte_size);
Frederic Riss41ff3962019-10-08 15:35:59 +00009371 if (llvm::countPopulation(val) != 1 && (val & ~covered_bits) != 0)
9372 can_be_bitfield = false;
9373 covered_bits |= val;
9374 ++num_enumerators;
9375 if (val == enum_svalue) {
9376 // Found an exact match, that's all we need to do.
Frederic Rissd6470fb2019-10-08 15:35:58 +00009377 s->PutCString(enumerator->getNameAsString());
9378 return true;
9379 }
9380 }
Frederic Riss41ff3962019-10-08 15:35:59 +00009381
Frederic Riss41ff3962019-10-08 15:35:59 +00009382 // Unsigned values make more sense for flags.
9383 offset = byte_offset;
9384 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9385 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9386
Frederic Rissb56e3a12019-10-08 19:52:01 +00009387 // No exact match, but we don't think this is a bitfield. Print the value as
9388 // decimal.
9389 if (!can_be_bitfield) {
9390 if (qual_type->isSignedIntegerOrEnumerationType())
9391 s->Printf("%" PRIi64, enum_svalue);
9392 else
9393 s->Printf("%" PRIu64, enum_uvalue);
9394 return true;
9395 }
9396
Frederic Riss41ff3962019-10-08 15:35:59 +00009397 uint64_t remaining_value = enum_uvalue;
9398 std::vector<std::pair<uint64_t, llvm::StringRef>> values;
9399 values.reserve(num_enumerators);
9400 for (auto enumerator : enum_decl->enumerators())
9401 if (auto val = enumerator->getInitVal().getZExtValue())
9402 values.emplace_back(val, enumerator->getName());
9403
9404 // Sort in reverse order of the number of the population count, so that in
9405 // `enum {A, B, ALL = A|B }` we visit ALL first. Use a stable sort so that
9406 // A | C where A is declared before C is displayed in this order.
9407 std::stable_sort(values.begin(), values.end(), [](const auto &a, const auto &b) {
9408 return llvm::countPopulation(a.first) > llvm::countPopulation(b.first);
9409 });
9410
9411 for (const auto &val : values) {
9412 if ((remaining_value & val.first) != val.first)
9413 continue;
9414 remaining_value &= ~val.first;
9415 s->PutCString(val.second);
9416 if (remaining_value)
9417 s->PutCString(" | ");
9418 }
9419
9420 // If there is a remainder that is not covered by the value, print it as hex.
9421 if (remaining_value)
9422 s->Printf("0x%" PRIx64, remaining_value);
9423
Frederic Rissd6470fb2019-10-08 15:35:58 +00009424 return true;
9425}
9426
Kate Stoneb9c1b512016-09-06 20:57:50 +00009427bool ClangASTContext::DumpTypeValue(
9428 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009429 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9430 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009431 ExecutionContextScope *exe_scope) {
9432 if (!type)
9433 return false;
9434 if (IsAggregateType(type)) {
9435 return false;
9436 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009437 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009438
9439 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Frederic Riss41ff3962019-10-08 15:35:59 +00009440
9441 if (type_class == clang::Type::Elaborated) {
9442 qual_type = llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9443 return DumpTypeValue(qual_type.getAsOpaquePtr(), s, format, data, byte_offset, byte_size,
9444 bitfield_bit_size, bitfield_bit_offset, exe_scope);
9445 }
9446
Kate Stoneb9c1b512016-09-06 20:57:50 +00009447 switch (type_class) {
9448 case clang::Type::Typedef: {
9449 clang::QualType typedef_qual_type =
9450 llvm::cast<clang::TypedefType>(qual_type)
9451 ->getDecl()
9452 ->getUnderlyingType();
Alex Langfordbddab072019-08-13 19:40:36 +00009453 CompilerType typedef_clang_type(this, typedef_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009454 if (format == eFormatDefault)
9455 format = typedef_clang_type.GetFormat();
9456 clang::TypeInfo typedef_type_info =
9457 getASTContext()->getTypeInfo(typedef_qual_type);
9458 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9459
9460 return typedef_clang_type.DumpTypeValue(
9461 s,
9462 format, // The format with which to display the element
9463 data, // Data buffer containing all bytes for this type
9464 byte_offset, // Offset into "data" where to grab value from
9465 typedef_byte_size, // Size of this type in bytes
9466 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9467 // treat as a bitfield
9468 bitfield_bit_offset, // Offset in bits of a bitfield value if
9469 // bitfield_bit_size != 0
9470 exe_scope);
9471 } break;
9472
9473 case clang::Type::Enum:
Adrian Prantl05097242018-04-30 16:49:04 +00009474 // If our format is enum or default, show the enumeration value as its
9475 // enumeration string value, else just display it as requested.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009476 if ((format == eFormatEnum || format == eFormatDefault) &&
Frederic Rissd6470fb2019-10-08 15:35:58 +00009477 GetCompleteType(type))
9478 return DumpEnumValue(qual_type, s, data, byte_offset, byte_size,
9479 bitfield_bit_offset, bitfield_bit_size);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009480 // format was not enum, just fall through and dump the value as
9481 // requested....
9482 LLVM_FALLTHROUGH;
9483
9484 default:
9485 // We are down to a scalar type that we just need to display.
9486 {
9487 uint32_t item_count = 1;
9488 // A few formats, we might need to modify our size and count for
9489 // depending
9490 // on how we are trying to display the value...
9491 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009492 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009493 case eFormatBoolean:
9494 case eFormatBinary:
9495 case eFormatComplex:
9496 case eFormatCString: // NULL terminated C strings
9497 case eFormatDecimal:
9498 case eFormatEnum:
9499 case eFormatHex:
9500 case eFormatHexUppercase:
9501 case eFormatFloat:
9502 case eFormatOctal:
9503 case eFormatOSType:
9504 case eFormatUnsigned:
9505 case eFormatPointer:
9506 case eFormatVectorOfChar:
9507 case eFormatVectorOfSInt8:
9508 case eFormatVectorOfUInt8:
9509 case eFormatVectorOfSInt16:
9510 case eFormatVectorOfUInt16:
9511 case eFormatVectorOfSInt32:
9512 case eFormatVectorOfUInt32:
9513 case eFormatVectorOfSInt64:
9514 case eFormatVectorOfUInt64:
9515 case eFormatVectorOfFloat32:
9516 case eFormatVectorOfFloat64:
9517 case eFormatVectorOfUInt128:
9518 break;
9519
9520 case eFormatChar:
9521 case eFormatCharPrintable:
9522 case eFormatCharArray:
9523 case eFormatBytes:
9524 case eFormatBytesWithASCII:
9525 item_count = byte_size;
9526 byte_size = 1;
9527 break;
9528
9529 case eFormatUnicode16:
9530 item_count = byte_size / 2;
9531 byte_size = 2;
9532 break;
9533
9534 case eFormatUnicode32:
9535 item_count = byte_size / 4;
9536 byte_size = 4;
9537 break;
9538 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009539 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9540 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9541 bitfield_bit_size, bitfield_bit_offset,
9542 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009543 }
9544 break;
9545 }
9546 }
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00009547 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009548}
9549
9550void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9551 ExecutionContext *exe_ctx, Stream *s,
9552 const lldb_private::DataExtractor &data,
9553 lldb::offset_t data_byte_offset,
9554 size_t data_byte_size) {
9555 uint32_t length = 0;
9556 if (IsCStringType(type, length)) {
9557 if (exe_ctx) {
9558 Process *process = exe_ctx->GetProcessPtr();
9559 if (process) {
9560 lldb::offset_t offset = data_byte_offset;
9561 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9562 std::vector<uint8_t> buf;
9563 if (length > 0)
9564 buf.resize(length);
9565 else
9566 buf.resize(256);
9567
Zachary Turner29cb8682017-03-03 20:57:05 +00009568 DataExtractor cstr_data(&buf.front(), buf.size(),
9569 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009570 buf.back() = '\0';
9571 size_t bytes_read;
9572 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009573 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009574 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9575 buf.size(), error)) > 0) {
9576 const size_t len = strlen((const char *)&buf.front());
9577 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009578 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009579 if (total_cstr_len == 0)
9580 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009581 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9582 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009583 total_cstr_len += len;
9584 if (len < buf.size())
9585 break;
9586 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009587 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009588 if (total_cstr_len > 0)
9589 s->PutChar('"');
9590 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009591 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009592 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009593}
9594
Kate Stoneb9c1b512016-09-06 20:57:50 +00009595void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9596 StreamFile s(stdout, false);
9597 DumpTypeDescription(type, &s);
9598 ClangASTMetadata *metadata =
9599 ClangASTContext::GetMetadata(getASTContext(), type);
9600 if (metadata) {
9601 metadata->Dump(&s);
9602 }
9603}
Greg Claytond8d4a572015-08-11 21:38:15 +00009604
Kate Stoneb9c1b512016-09-06 20:57:50 +00009605void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9606 Stream *s) {
9607 if (type) {
9608 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009609
Kate Stoneb9c1b512016-09-06 20:57:50 +00009610 llvm::SmallVector<char, 1024> buf;
9611 llvm::raw_svector_ostream llvm_ostrm(buf);
9612
9613 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9614 switch (type_class) {
9615 case clang::Type::ObjCObject:
9616 case clang::Type::ObjCInterface: {
9617 GetCompleteType(type);
9618
9619 const clang::ObjCObjectType *objc_class_type =
9620 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9621 assert(objc_class_type);
9622 if (objc_class_type) {
9623 clang::ObjCInterfaceDecl *class_interface_decl =
9624 objc_class_type->getInterface();
9625 if (class_interface_decl) {
9626 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9627 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009628 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009629 }
9630 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009631
Kate Stoneb9c1b512016-09-06 20:57:50 +00009632 case clang::Type::Typedef: {
9633 const clang::TypedefType *typedef_type =
9634 qual_type->getAs<clang::TypedefType>();
9635 if (typedef_type) {
9636 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9637 std::string clang_typedef_name(
9638 typedef_decl->getQualifiedNameAsString());
9639 if (!clang_typedef_name.empty()) {
9640 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009641 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009642 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009643 }
9644 } break;
9645
9646 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00009647 CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
9648 ->getDeducedType()
9649 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00009650 .DumpTypeDescription(s);
9651 return;
9652
9653 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00009654 CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
9655 ->getNamedType()
9656 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00009657 .DumpTypeDescription(s);
9658 return;
9659
9660 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00009661 CompilerType(
9662 this,
9663 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00009664 .DumpTypeDescription(s);
9665 return;
9666
9667 case clang::Type::Record: {
9668 GetCompleteType(type);
9669
9670 const clang::RecordType *record_type =
9671 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9672 const clang::RecordDecl *record_decl = record_type->getDecl();
9673 const clang::CXXRecordDecl *cxx_record_decl =
9674 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9675
9676 if (cxx_record_decl)
9677 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9678 s->GetIndentLevel());
9679 else
9680 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9681 s->GetIndentLevel());
9682 } break;
9683
9684 default: {
9685 const clang::TagType *tag_type =
9686 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9687 if (tag_type) {
9688 clang::TagDecl *tag_decl = tag_type->getDecl();
9689 if (tag_decl)
9690 tag_decl->print(llvm_ostrm, 0);
9691 } else {
9692 std::string clang_type_name(qual_type.getAsString());
9693 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009694 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009695 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009696 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009697 }
9698
Kate Stoneb9c1b512016-09-06 20:57:50 +00009699 if (buf.size() > 0) {
9700 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009701 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009702 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009703}
9704
Kate Stoneb9c1b512016-09-06 20:57:50 +00009705void ClangASTContext::DumpTypeName(const CompilerType &type) {
9706 if (ClangUtil::IsClangType(type)) {
9707 clang::QualType qual_type(
9708 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9709
9710 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9711 switch (type_class) {
9712 case clang::Type::Record: {
9713 const clang::CXXRecordDecl *cxx_record_decl =
9714 qual_type->getAsCXXRecordDecl();
9715 if (cxx_record_decl)
9716 printf("class %s", cxx_record_decl->getName().str().c_str());
9717 } break;
9718
9719 case clang::Type::Enum: {
9720 clang::EnumDecl *enum_decl =
9721 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9722 if (enum_decl) {
9723 printf("enum %s", enum_decl->getName().str().c_str());
9724 }
9725 } break;
9726
9727 case clang::Type::ObjCObject:
9728 case clang::Type::ObjCInterface: {
9729 const clang::ObjCObjectType *objc_class_type =
9730 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9731 if (objc_class_type) {
9732 clang::ObjCInterfaceDecl *class_interface_decl =
9733 objc_class_type->getInterface();
Adrian Prantl05097242018-04-30 16:49:04 +00009734 // We currently can't complete objective C types through the newly
9735 // added ASTContext because it only supports TagDecl objects right
9736 // now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00009737 if (class_interface_decl)
9738 printf("@class %s", class_interface_decl->getName().str().c_str());
9739 }
9740 } break;
9741
9742 case clang::Type::Typedef:
9743 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9744 ->getDecl()
9745 ->getName()
9746 .str()
9747 .c_str());
9748 break;
9749
9750 case clang::Type::Auto:
9751 printf("auto ");
9752 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9753 llvm::cast<clang::AutoType>(qual_type)
9754 ->getDeducedType()
9755 .getAsOpaquePtr()));
9756
9757 case clang::Type::Elaborated:
9758 printf("elaborated ");
9759 return DumpTypeName(CompilerType(
9760 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9761 ->getNamedType()
9762 .getAsOpaquePtr()));
9763
9764 case clang::Type::Paren:
9765 printf("paren ");
9766 return DumpTypeName(CompilerType(
9767 type.GetTypeSystem(),
9768 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9769
9770 default:
9771 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9772 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009773 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009774 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009775}
9776
Kate Stoneb9c1b512016-09-06 20:57:50 +00009777clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9778 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9779 const char *parent_name, int tag_decl_kind,
9780 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9781 if (template_param_infos.IsValid()) {
9782 std::string template_basename(parent_name);
9783 template_basename.erase(template_basename.find('<'));
9784
9785 return CreateClassTemplateDecl(decl_ctx, access_type,
9786 template_basename.c_str(), tag_decl_kind,
9787 template_param_infos);
9788 }
Konrad Kleine248a1302019-05-23 11:14:47 +00009789 return nullptr;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009790}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009791
Kate Stoneb9c1b512016-09-06 20:57:50 +00009792void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9793 ClangASTContext *ast = (ClangASTContext *)baton;
9794 SymbolFile *sym_file = ast->GetSymbolFile();
9795 if (sym_file) {
9796 CompilerType clang_type = GetTypeForDecl(decl);
9797 if (clang_type)
9798 sym_file->CompleteType(clang_type);
9799 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009800}
9801
Kate Stoneb9c1b512016-09-06 20:57:50 +00009802void ClangASTContext::CompleteObjCInterfaceDecl(
9803 void *baton, clang::ObjCInterfaceDecl *decl) {
9804 ClangASTContext *ast = (ClangASTContext *)baton;
9805 SymbolFile *sym_file = ast->GetSymbolFile();
9806 if (sym_file) {
9807 CompilerType clang_type = GetTypeForDecl(decl);
9808 if (clang_type)
9809 sym_file->CompleteType(clang_type);
9810 }
Zachary Turner42dff792016-04-15 00:21:26 +00009811}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009812
Kate Stoneb9c1b512016-09-06 20:57:50 +00009813DWARFASTParser *ClangASTContext::GetDWARFParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009814 if (!m_dwarf_ast_parser_up)
9815 m_dwarf_ast_parser_up.reset(new DWARFASTParserClang(*this));
9816 return m_dwarf_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00009817}
9818
9819PDBASTParser *ClangASTContext::GetPDBParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009820 if (!m_pdb_ast_parser_up)
9821 m_pdb_ast_parser_up.reset(new PDBASTParser(*this));
9822 return m_pdb_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00009823}
9824
9825bool ClangASTContext::LayoutRecordType(
9826 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9827 uint64_t &alignment,
9828 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9829 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9830 &base_offsets,
9831 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9832 &vbase_offsets) {
9833 ClangASTContext *ast = (ClangASTContext *)baton;
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009834 lldb_private::ClangASTImporter *importer = nullptr;
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009835 if (ast->m_dwarf_ast_parser_up)
9836 importer = &ast->m_dwarf_ast_parser_up->GetClangASTImporter();
9837 if (!importer && ast->m_pdb_ast_parser_up)
9838 importer = &ast->m_pdb_ast_parser_up->GetClangASTImporter();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009839 if (!importer)
9840 return false;
9841
9842 return importer->LayoutRecordType(record_decl, bit_size, alignment,
9843 field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009844}
9845
Paul Hermand628cbb2015-09-15 23:44:17 +00009846// CompilerDecl override functions
Paul Hermand628cbb2015-09-15 23:44:17 +00009847
Kate Stoneb9c1b512016-09-06 20:57:50 +00009848ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9849 if (opaque_decl) {
9850 clang::NamedDecl *nd =
9851 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9852 if (nd != nullptr)
9853 return ConstString(nd->getDeclName().getAsString());
9854 }
9855 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009856}
9857
Kate Stoneb9c1b512016-09-06 20:57:50 +00009858ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9859 if (opaque_decl) {
9860 clang::NamedDecl *nd =
9861 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9862 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9863 clang::MangleContext *mc = getMangleContext();
9864 if (mc && mc->shouldMangleCXXName(nd)) {
9865 llvm::SmallVector<char, 1024> buf;
9866 llvm::raw_svector_ostream llvm_ostrm(buf);
9867 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9868 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9869 Ctor_Complete, llvm_ostrm);
9870 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9871 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9872 Dtor_Complete, llvm_ostrm);
9873 } else {
9874 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009875 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009876 if (buf.size() > 0)
9877 return ConstString(buf.data(), buf.size());
9878 }
Greg Claytonfe689042015-11-10 17:47:04 +00009879 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009880 }
9881 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009882}
9883
Kate Stoneb9c1b512016-09-06 20:57:50 +00009884CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9885 if (opaque_decl)
9886 return CompilerDeclContext(this,
9887 ((clang::Decl *)opaque_decl)->getDeclContext());
9888 else
9889 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009890}
9891
Kate Stoneb9c1b512016-09-06 20:57:50 +00009892CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9893 if (clang::FunctionDecl *func_decl =
9894 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9895 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9896 if (clang::ObjCMethodDecl *objc_method =
9897 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9898 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9899 else
Greg Claytonfe689042015-11-10 17:47:04 +00009900 return CompilerType();
9901}
9902
Kate Stoneb9c1b512016-09-06 20:57:50 +00009903size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9904 if (clang::FunctionDecl *func_decl =
9905 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9906 return func_decl->param_size();
9907 if (clang::ObjCMethodDecl *objc_method =
9908 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9909 return objc_method->param_size();
9910 else
9911 return 0;
9912}
9913
9914CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9915 size_t idx) {
9916 if (clang::FunctionDecl *func_decl =
9917 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9918 if (idx < func_decl->param_size()) {
9919 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9920 if (var_decl)
9921 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9922 }
9923 } else if (clang::ObjCMethodDecl *objc_method =
9924 llvm::dyn_cast<clang::ObjCMethodDecl>(
9925 (clang::Decl *)opaque_decl)) {
9926 if (idx < objc_method->param_size())
9927 return CompilerType(
9928 this,
9929 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9930 }
9931 return CompilerType();
9932}
9933
Greg Clayton99558cc42015-08-24 23:46:31 +00009934// CompilerDeclContext functions
Greg Clayton99558cc42015-08-24 23:46:31 +00009935
Kate Stoneb9c1b512016-09-06 20:57:50 +00009936std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9937 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9938 std::vector<CompilerDecl> found_decls;
9939 if (opaque_decl_ctx) {
9940 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9941 std::set<DeclContext *> searched;
9942 std::multimap<DeclContext *, DeclContext *> search_queue;
9943 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009944
Kate Stoneb9c1b512016-09-06 20:57:50 +00009945 for (clang::DeclContext *decl_context = root_decl_ctx;
9946 decl_context != nullptr && found_decls.empty();
9947 decl_context = decl_context->getParent()) {
9948 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +00009949
Kate Stoneb9c1b512016-09-06 20:57:50 +00009950 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9951 it++) {
9952 if (!searched.insert(it->second).second)
9953 continue;
9954 symbol_file->ParseDeclsForContext(
9955 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +00009956
Kate Stoneb9c1b512016-09-06 20:57:50 +00009957 for (clang::Decl *child : it->second->decls()) {
9958 if (clang::UsingDirectiveDecl *ud =
9959 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9960 if (ignore_using_decls)
9961 continue;
9962 clang::DeclContext *from = ud->getCommonAncestor();
9963 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9964 search_queue.insert(
9965 std::make_pair(from, ud->getNominatedNamespace()));
9966 } else if (clang::UsingDecl *ud =
9967 llvm::dyn_cast<clang::UsingDecl>(child)) {
9968 if (ignore_using_decls)
9969 continue;
9970 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9971 clang::Decl *target = usd->getTargetDecl();
9972 if (clang::NamedDecl *nd =
9973 llvm::dyn_cast<clang::NamedDecl>(target)) {
9974 IdentifierInfo *ii = nd->getIdentifier();
9975 if (ii != nullptr &&
9976 ii->getName().equals(name.AsCString(nullptr)))
9977 found_decls.push_back(CompilerDecl(this, nd));
9978 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009979 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009980 } else if (clang::NamedDecl *nd =
9981 llvm::dyn_cast<clang::NamedDecl>(child)) {
9982 IdentifierInfo *ii = nd->getIdentifier();
9983 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9984 found_decls.push_back(CompilerDecl(this, nd));
9985 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009986 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009987 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009988 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009989 }
9990 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +00009991}
9992
Dawn Perchikb5925782015-12-12 19:31:41 +00009993// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009994// and return the number of levels it took to find it, or
Adrian Prantl05097242018-04-30 16:49:04 +00009995// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
9996// declaration, its name and/or type, if set, will be used to check that the
9997// decl found in the scope is a match.
Dawn Perchikb5925782015-12-12 19:31:41 +00009998//
Kate Stoneb9c1b512016-09-06 20:57:50 +00009999// The optional name is required by languages (like C++) to handle using
Adrian Prantl05097242018-04-30 16:49:04 +000010000// declarations like:
Dawn Perchikb5925782015-12-12 19:31:41 +000010001//
10002// void poo();
10003// namespace ns {
10004// void foo();
10005// void goo();
10006// }
10007// void bar() {
10008// using ns::foo;
10009// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
10010// // LLDB_INVALID_DECL_LEVEL for 'goo'.
10011// }
10012//
10013// The optional type is useful in the case that there's a specific overload
10014// that we're looking for that might otherwise be shadowed, like:
10015//
10016// void foo(int);
10017// namespace ns {
10018// void foo();
10019// }
10020// void bar() {
10021// using ns::foo;
10022// // CountDeclLevels returns 0 for { 'foo', void() },
10023// // 1 for { 'foo', void(int) }, and
10024// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
10025// }
10026//
10027// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +000010028// function at file scope will return the same level as a function at global
Adrian Prantl05097242018-04-30 16:49:04 +000010029// scope. Ideally we'd like to treat the file scope as an additional scope just
10030// below the global scope. More work needs to be done to recognise that, if
10031// the decl we're trying to look up is static, we should compare its source
10032// file with that of the current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010033uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
10034 clang::DeclContext *child_decl_ctx,
10035 ConstString *child_name,
10036 CompilerType *child_type) {
10037 if (frame_decl_ctx) {
10038 std::set<DeclContext *> searched;
10039 std::multimap<DeclContext *, DeclContext *> search_queue;
10040 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +000010041
Kate Stoneb9c1b512016-09-06 20:57:50 +000010042 // Get the lookup scope for the decl we're trying to find.
10043 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +000010044
Kate Stoneb9c1b512016-09-06 20:57:50 +000010045 // Look for it in our scope's decl context and its parents.
10046 uint32_t level = 0;
10047 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
10048 decl_ctx = decl_ctx->getParent()) {
10049 if (!decl_ctx->isLookupContext())
10050 continue;
10051 if (decl_ctx == parent_decl_ctx)
10052 // Found it!
10053 return level;
10054 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
10055 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
10056 it++) {
10057 if (searched.find(it->second) != searched.end())
10058 continue;
10059
10060 // Currently DWARF has one shared translation unit for all Decls at top
Adrian Prantl05097242018-04-30 16:49:04 +000010061 // level, so this would erroneously find using statements anywhere. So
10062 // don't look at the top-level translation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010063 // TODO fix this and add a testcase that depends on it.
10064
10065 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
10066 continue;
10067
10068 searched.insert(it->second);
10069 symbol_file->ParseDeclsForContext(
10070 CompilerDeclContext(this, it->second));
10071
10072 for (clang::Decl *child : it->second->decls()) {
10073 if (clang::UsingDirectiveDecl *ud =
10074 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10075 clang::DeclContext *ns = ud->getNominatedNamespace();
10076 if (ns == parent_decl_ctx)
10077 // Found it!
10078 return level;
10079 clang::DeclContext *from = ud->getCommonAncestor();
10080 if (searched.find(ns) == searched.end())
10081 search_queue.insert(std::make_pair(from, ns));
10082 } else if (child_name) {
10083 if (clang::UsingDecl *ud =
10084 llvm::dyn_cast<clang::UsingDecl>(child)) {
10085 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10086 clang::Decl *target = usd->getTargetDecl();
10087 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
10088 if (!nd)
10089 continue;
10090 // Check names.
10091 IdentifierInfo *ii = nd->getIdentifier();
10092 if (ii == nullptr ||
10093 !ii->getName().equals(child_name->AsCString(nullptr)))
10094 continue;
10095 // Check types, if one was provided.
10096 if (child_type) {
10097 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
10098 if (!AreTypesSame(clang_type, *child_type,
10099 /*ignore_qualifiers=*/true))
10100 continue;
10101 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010102 // Found it!
10103 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010104 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010105 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010106 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010107 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010108 }
10109 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +000010110 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010111 }
10112 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +000010113}
10114
Kate Stoneb9c1b512016-09-06 20:57:50 +000010115bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
10116 if (opaque_decl_ctx)
10117 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
10118 else
Greg Clayton99558cc42015-08-24 23:46:31 +000010119 return false;
10120}
10121
Kate Stoneb9c1b512016-09-06 20:57:50 +000010122ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
10123 if (opaque_decl_ctx) {
10124 clang::NamedDecl *named_decl =
10125 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10126 if (named_decl)
10127 return ConstString(named_decl->getName());
10128 }
10129 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +000010130}
10131
Kate Stoneb9c1b512016-09-06 20:57:50 +000010132ConstString
10133ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
10134 if (opaque_decl_ctx) {
10135 clang::NamedDecl *named_decl =
10136 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10137 if (named_decl)
10138 return ConstString(
10139 llvm::StringRef(named_decl->getQualifiedNameAsString()));
10140 }
10141 return ConstString();
10142}
10143
10144bool ClangASTContext::DeclContextIsClassMethod(
10145 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
10146 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
10147 if (opaque_decl_ctx) {
10148 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10149 if (ObjCMethodDecl *objc_method =
10150 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
10151 if (is_instance_method_ptr)
10152 *is_instance_method_ptr = objc_method->isInstanceMethod();
10153 if (language_ptr)
10154 *language_ptr = eLanguageTypeObjC;
10155 if (language_object_name_ptr)
10156 language_object_name_ptr->SetCString("self");
10157 return true;
10158 } else if (CXXMethodDecl *cxx_method =
10159 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
10160 if (is_instance_method_ptr)
10161 *is_instance_method_ptr = cxx_method->isInstance();
10162 if (language_ptr)
10163 *language_ptr = eLanguageTypeC_plus_plus;
10164 if (language_object_name_ptr)
10165 language_object_name_ptr->SetCString("this");
10166 return true;
10167 } else if (clang::FunctionDecl *function_decl =
10168 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
10169 ClangASTMetadata *metadata =
10170 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
10171 if (metadata && metadata->HasObjectPtr()) {
10172 if (is_instance_method_ptr)
10173 *is_instance_method_ptr = true;
10174 if (language_ptr)
10175 *language_ptr = eLanguageTypeObjC;
10176 if (language_object_name_ptr)
10177 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10178 return true;
10179 }
10180 }
10181 }
10182 return false;
10183}
10184
Raphael Isemanna9469972019-03-12 07:45:04 +000010185bool ClangASTContext::DeclContextIsContainedInLookup(
10186 void *opaque_decl_ctx, void *other_opaque_decl_ctx) {
10187 auto *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10188 auto *other = (clang::DeclContext *)other_opaque_decl_ctx;
10189
10190 do {
10191 // A decl context always includes its own contents in its lookup.
10192 if (decl_ctx == other)
10193 return true;
10194
10195 // If we have an inline namespace, then the lookup of the parent context
10196 // also includes the inline namespace contents.
10197 } while (other->isInlineNamespace() && (other = other->getParent()));
10198
10199 return false;
10200}
10201
Raphael Isemannc2dd84e2019-11-28 15:43:26 +010010202static bool IsClangDeclContext(const CompilerDeclContext &dc) {
10203 return dc.IsValid() && isa<ClangASTContext>(dc.GetTypeSystem());
10204}
10205
Kate Stoneb9c1b512016-09-06 20:57:50 +000010206clang::DeclContext *
10207ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
Raphael Isemannc2dd84e2019-11-28 15:43:26 +010010208 if (IsClangDeclContext(dc))
Kate Stoneb9c1b512016-09-06 20:57:50 +000010209 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10210 return nullptr;
10211}
Greg Clayton99558cc42015-08-24 23:46:31 +000010212
10213ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010214ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
Raphael Isemannc2dd84e2019-11-28 15:43:26 +010010215 if (IsClangDeclContext(dc))
Kate Stoneb9c1b512016-09-06 20:57:50 +000010216 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10217 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10218 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010219}
10220
10221CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010222ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
Raphael Isemannc2dd84e2019-11-28 15:43:26 +010010223 if (IsClangDeclContext(dc))
Kate Stoneb9c1b512016-09-06 20:57:50 +000010224 return llvm::dyn_cast<clang::CXXMethodDecl>(
10225 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10226 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010227}
10228
10229clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010230ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
Raphael Isemannc2dd84e2019-11-28 15:43:26 +010010231 if (IsClangDeclContext(dc))
Kate Stoneb9c1b512016-09-06 20:57:50 +000010232 return llvm::dyn_cast<clang::FunctionDecl>(
10233 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10234 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010235}
10236
10237clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010238ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
Raphael Isemannc2dd84e2019-11-28 15:43:26 +010010239 if (IsClangDeclContext(dc))
Kate Stoneb9c1b512016-09-06 20:57:50 +000010240 return llvm::dyn_cast<clang::NamespaceDecl>(
10241 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10242 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010243}
10244
10245ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010246ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10247 const void *object) {
10248 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10249 if (ast)
10250 return ClangASTContext::GetMetadata(ast, object);
10251 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010252}
10253
10254clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010255ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10256 ClangASTContext *ast =
10257 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10258 if (ast)
10259 return ast->getASTContext();
10260 return nullptr;
10261}
10262
Raphael Isemann2eb963a2019-10-02 12:26:08 +000010263ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target,
10264 ArchSpec arch)
10265 : ClangASTContext(arch), m_target_wp(target.shared_from_this()),
Kate Stoneb9c1b512016-09-06 20:57:50 +000010266 m_persistent_variables(new ClangPersistentVariables) {}
10267
10268UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010269 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010270 Expression::ResultType desired_type,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010271 const EvaluateExpressionOptions &options,
10272 ValueObject *ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000010273 TargetSP target_sp = m_target_wp.lock();
10274 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010275 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010276
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010277 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010278 desired_type, options, ctx_obj);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010279}
10280
Kate Stoneb9c1b512016-09-06 20:57:50 +000010281FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10282 const CompilerType &return_type, const Address &function_address,
10283 const ValueList &arg_value_list, const char *name) {
10284 TargetSP target_sp = m_target_wp.lock();
10285 if (!target_sp)
10286 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010287
Kate Stoneb9c1b512016-09-06 20:57:50 +000010288 Process *process = target_sp->GetProcessSP().get();
10289 if (!process)
10290 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010291
Kate Stoneb9c1b512016-09-06 20:57:50 +000010292 return new ClangFunctionCaller(*process, return_type, function_address,
10293 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010294}
10295
10296UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010297ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10298 const char *name) {
10299 TargetSP target_sp = m_target_wp.lock();
10300 if (!target_sp)
10301 return nullptr;
10302
10303 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010304}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010305
10306PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010307ClangASTContextForExpressions::GetPersistentExpressionState() {
10308 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010309}
Sean Callanan68e44232017-09-28 20:20:25 +000010310
10311clang::ExternalASTMerger &
10312ClangASTContextForExpressions::GetMergerUnchecked() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000010313 lldbassert(m_scratch_ast_source_up != nullptr);
10314 return m_scratch_ast_source_up->GetMergerUnchecked();
Sean Callanan68e44232017-09-28 20:20:25 +000010315}