blob: bee72b219b5e049ec4e746d392a47fcdd0b8f6fc [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 Isemann2f323fc2019-08-28 13:46:01 +0000340bool ClangASTContext::IsOperator(llvm::StringRef name,
Davide Italiano7e3ef4d2018-03-20 19:46:32 +0000341 clang::OverloadedOperatorKind &op_kind) {
Raphael Isemann2f323fc2019-08-28 13:46:01 +0000342 // All operators have to start with "operator".
343 if (!name.consume_front("operator"))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000345
Raphael Isemann2f323fc2019-08-28 13:46:01 +0000346 // Remember if there was a space after "operator". This is necessary to
347 // check for collisions with strangely named functions like "operatorint()".
348 bool space_after_operator = name.consume_front(" ");
Pavel Labath1ac2b202016-08-15 14:32:32 +0000349
Raphael Isemann2f323fc2019-08-28 13:46:01 +0000350 op_kind = StringSwitch<clang::OverloadedOperatorKind>(name)
351 .Case("+", clang::OO_Plus)
352 .Case("+=", clang::OO_PlusEqual)
353 .Case("++", clang::OO_PlusPlus)
354 .Case("-", clang::OO_Minus)
355 .Case("-=", clang::OO_MinusEqual)
356 .Case("--", clang::OO_MinusMinus)
357 .Case("->", clang::OO_Arrow)
358 .Case("->*", clang::OO_ArrowStar)
359 .Case("*", clang::OO_Star)
360 .Case("*=", clang::OO_StarEqual)
361 .Case("/", clang::OO_Slash)
362 .Case("/=", clang::OO_SlashEqual)
363 .Case("%", clang::OO_Percent)
364 .Case("%=", clang::OO_PercentEqual)
365 .Case("^", clang::OO_Caret)
366 .Case("^=", clang::OO_CaretEqual)
367 .Case("&", clang::OO_Amp)
368 .Case("&=", clang::OO_AmpEqual)
369 .Case("&&", clang::OO_AmpAmp)
370 .Case("|", clang::OO_Pipe)
371 .Case("|=", clang::OO_PipeEqual)
372 .Case("||", clang::OO_PipePipe)
373 .Case("~", clang::OO_Tilde)
374 .Case("!", clang::OO_Exclaim)
375 .Case("!=", clang::OO_ExclaimEqual)
376 .Case("=", clang::OO_Equal)
377 .Case("==", clang::OO_EqualEqual)
378 .Case("<", clang::OO_Less)
379 .Case("<<", clang::OO_LessLess)
380 .Case("<<=", clang::OO_LessLessEqual)
381 .Case("<=", clang::OO_LessEqual)
382 .Case(">", clang::OO_Greater)
383 .Case(">>", clang::OO_GreaterGreater)
384 .Case(">>=", clang::OO_GreaterGreaterEqual)
385 .Case(">=", clang::OO_GreaterEqual)
386 .Case("()", clang::OO_Call)
387 .Case("[]", clang::OO_Subscript)
388 .Case(",", clang::OO_Comma)
389 .Default(clang::NUM_OVERLOADED_OPERATORS);
Pavel Labath1ac2b202016-08-15 14:32:32 +0000390
Raphael Isemann2f323fc2019-08-28 13:46:01 +0000391 // We found a fitting operator, so we can exit now.
392 if (op_kind != clang::NUM_OVERLOADED_OPERATORS)
393 return true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000394
Raphael Isemann2f323fc2019-08-28 13:46:01 +0000395 // After the "operator " or "operator" part is something unknown. This means
396 // it's either one of the named operators (new/delete), a conversion operator
397 // (e.g. operator bool) or a function which name starts with "operator"
398 // (e.g. void operatorbool).
Pavel Labath1ac2b202016-08-15 14:32:32 +0000399
Raphael Isemann2f323fc2019-08-28 13:46:01 +0000400 // If it's a function that starts with operator it can't have a space after
401 // "operator" because identifiers can't contain spaces.
402 // E.g. "operator int" (conversion operator)
403 // vs. "operatorint" (function with colliding name).
404 if (!space_after_operator)
405 return false; // not an operator.
Pavel Labath1ac2b202016-08-15 14:32:32 +0000406
Raphael Isemann2f323fc2019-08-28 13:46:01 +0000407 // Now the operator is either one of the named operators or a conversion
408 // operator.
409 op_kind = StringSwitch<clang::OverloadedOperatorKind>(name)
410 .Case("new", clang::OO_New)
411 .Case("new[]", clang::OO_Array_New)
412 .Case("delete", clang::OO_Delete)
413 .Case("delete[]", clang::OO_Array_Delete)
414 // conversion operators hit this case.
415 .Default(clang::NUM_OVERLOADED_OPERATORS);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416
417 return true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000418}
Enrico Granata5d84a692014-08-19 21:46:37 +0000419
Greg Clayton57ee3062013-07-11 22:46:58 +0000420clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
422 switch (access) {
423 default:
424 break;
425 case eAccessNone:
Greg Clayton8cf05932010-07-22 18:30:50 +0000426 return AS_none;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427 case eAccessPublic:
428 return AS_public;
429 case eAccessPrivate:
430 return AS_private;
431 case eAccessProtected:
432 return AS_protected;
433 }
434 return AS_none;
Greg Clayton8cf05932010-07-22 18:30:50 +0000435}
436
Kate Stoneb9c1b512016-09-06 20:57:50 +0000437static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
438 // FIXME: Cleanup per-file based stuff.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439
Adrian Prantl05097242018-04-30 16:49:04 +0000440 // Set some properties which depend solely on the input kind; it would be
441 // nice to move these to the language standard, and have the driver resolve
442 // the input kind + language standard.
Rainer Orth6ca17072019-08-05 14:00:43 +0000443 if (IK.getLanguage() == clang::Language::Asm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444 Opts.AsmPreprocessor = 1;
Richard Smith8186cd42017-04-26 22:10:53 +0000445 } else if (IK.isObjectiveC()) {
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000446 Opts.ObjC = 1;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447 }
448
449 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
450
451 if (LangStd == LangStandard::lang_unspecified) {
452 // Based on the base language, pick one.
Richard Smith8186cd42017-04-26 22:10:53 +0000453 switch (IK.getLanguage()) {
Rainer Orth6ca17072019-08-05 14:00:43 +0000454 case clang::Language::Unknown:
455 case clang::Language::LLVM_IR:
456 case clang::Language::RenderScript:
David Blaikiea322f362017-01-06 00:38:06 +0000457 llvm_unreachable("Invalid input kind!");
Rainer Orth6ca17072019-08-05 14:00:43 +0000458 case clang::Language::OpenCL:
Pavel Labath47168542017-04-27 08:49:19 +0000459 LangStd = LangStandard::lang_opencl10;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 break;
Rainer Orth6ca17072019-08-05 14:00:43 +0000461 case clang::Language::CUDA:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 LangStd = LangStandard::lang_cuda;
463 break;
Rainer Orth6ca17072019-08-05 14:00:43 +0000464 case clang::Language::Asm:
465 case clang::Language::C:
466 case clang::Language::ObjC:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 LangStd = LangStandard::lang_gnu99;
468 break;
Rainer Orth6ca17072019-08-05 14:00:43 +0000469 case clang::Language::CXX:
470 case clang::Language::ObjCXX:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471 LangStd = LangStandard::lang_gnucxx98;
472 break;
Rainer Orth6ca17072019-08-05 14:00:43 +0000473 case clang::Language::HIP:
Benjamin Kramer0d97c222018-04-25 13:22:47 +0000474 LangStd = LangStandard::lang_hip;
475 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478
Kate Stoneb9c1b512016-09-06 20:57:50 +0000479 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
480 Opts.LineComment = Std.hasLineComments();
481 Opts.C99 = Std.isC99();
482 Opts.CPlusPlus = Std.isCPlusPlus();
483 Opts.CPlusPlus11 = Std.isCPlusPlus11();
484 Opts.Digraphs = Std.hasDigraphs();
485 Opts.GNUMode = Std.isGNUMode();
486 Opts.GNUInline = !Std.isC99();
487 Opts.HexFloats = Std.hasHexFloats();
488 Opts.ImplicitInt = Std.hasImplicitInt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492 // OpenCL has some additional defaults.
Pavel Labath47168542017-04-27 08:49:19 +0000493 if (LangStd == LangStandard::lang_opencl10) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494 Opts.OpenCL = 1;
495 Opts.AltiVec = 1;
496 Opts.CXXOperatorNames = 1;
Richard Smithc6245102019-09-13 06:02:15 +0000497 Opts.setLaxVectorConversions(LangOptions::LaxVectorConversionKind::All);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000498 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500 // OpenCL and C++ both have bool, true, false keywords.
501 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000504
Adrian Prantl05097242018-04-30 16:49:04 +0000505 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is
506 // specified, or -std is set to a conforming mode.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000507 Opts.Trigraphs = !Opts.GNUMode;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000509 Opts.OptimizeSize = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000510
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511 // FIXME: Eliminate this dependency.
512 // unsigned Opt =
513 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
514 // Opts.Optimize = Opt != 0;
515 unsigned Opt = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517 // This is the __NO_INLINE__ define, which just depends on things like the
518 // optimization level and -fno-inline, not actually whether the backend has
519 // inlining enabled.
520 //
521 // FIXME: This is affected by other options (-fno-inline).
522 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523}
524
Raphael Isemannd01b4a72019-10-01 12:28:14 +0000525ClangASTContext::ClangASTContext(llvm::StringRef target_triple)
526 : TypeSystem(TypeSystem::eKindClang) {
527 if (!target_triple.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528 SetTargetTriple(target_triple);
Raphael Isemann2eb963a2019-10-02 12:26:08 +0000529 // The caller didn't pass an ASTContext so create a new one for this
530 // ClangASTContext.
531 CreateASTContext();
532}
533
534ClangASTContext::ClangASTContext(ArchSpec arch)
535 : TypeSystem(TypeSystem::eKindClang) {
536 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 Isemannc73bfc92019-10-01 12:55:37 +0000542ClangASTContext::ClangASTContext(ASTContext &existing_ctxt)
543 : TypeSystem(TypeSystem::eKindClang) {
544 SetTargetTriple(existing_ctxt.getTargetInfo().getTriple().str());
545
546 m_ast_up.reset(&existing_ctxt);
547 GetASTMap().Insert(&existing_ctxt, this);
548}
549
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550// Destructor
Kate Stoneb9c1b512016-09-06 20:57:50 +0000551ClangASTContext::~ClangASTContext() { Finalize(); }
552
553ConstString ClangASTContext::GetPluginNameStatic() {
554 return ConstString("clang");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555}
556
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557ConstString ClangASTContext::GetPluginName() {
558 return ClangASTContext::GetPluginNameStatic();
Greg Clayton56939cb2015-09-17 22:23:34 +0000559}
560
Kate Stoneb9c1b512016-09-06 20:57:50 +0000561uint32_t ClangASTContext::GetPluginVersion() { return 1; }
Greg Clayton56939cb2015-09-17 22:23:34 +0000562
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
564 lldb_private::Module *module,
565 Target *target) {
566 if (ClangASTContextSupportsLanguage(language)) {
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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573 if (arch.IsValid()) {
574 ArchSpec fixed_arch = arch;
575 // LLVM wants this to be set to iOS or MacOSX; if we're working on
576 // a bare-boards type image, change the triple for llvm's benefit.
577 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
578 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
579 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
580 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
Jason Molenda7dd7a362019-10-16 19:14:49 +0000581 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64_32 ||
Kate Stoneb9c1b512016-09-06 20:57:50 +0000582 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
583 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
584 } else {
585 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
Greg Clayton56939cb2015-09-17 22:23:34 +0000586 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000587 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000588
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589 if (module) {
Raphael Isemann2eb963a2019-10-02 12:26:08 +0000590 std::shared_ptr<ClangASTContext> ast_sp(
591 new ClangASTContext(fixed_arch));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000592 return ast_sp;
593 } else if (target && target->IsValid()) {
594 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
Raphael Isemann2eb963a2019-10-02 12:26:08 +0000595 new ClangASTContextForExpressions(*target, fixed_arch));
596 ast_sp->m_scratch_ast_source_up.reset(
597 new ClangASTSource(target->shared_from_this()));
598 lldbassert(ast_sp->getFileManager());
599 ast_sp->m_scratch_ast_source_up->InstallASTContext(
600 *ast_sp->getASTContext(), *ast_sp->getFileManager(), true);
601 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
602 ast_sp->m_scratch_ast_source_up->CreateProxy());
603 ast_sp->SetExternalSource(proxy_ast_source);
604 return ast_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000605 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000606 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607 }
608 return lldb::TypeSystemSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609}
610
Adrian Prantlaa97a892019-08-22 21:45:58 +0000611LanguageSet ClangASTContext::GetSupportedLanguagesForTypes() {
612 LanguageSet languages;
613 languages.Insert(lldb::eLanguageTypeC89);
614 languages.Insert(lldb::eLanguageTypeC);
615 languages.Insert(lldb::eLanguageTypeC11);
616 languages.Insert(lldb::eLanguageTypeC_plus_plus);
617 languages.Insert(lldb::eLanguageTypeC99);
618 languages.Insert(lldb::eLanguageTypeObjC);
619 languages.Insert(lldb::eLanguageTypeObjC_plus_plus);
620 languages.Insert(lldb::eLanguageTypeC_plus_plus_03);
621 languages.Insert(lldb::eLanguageTypeC_plus_plus_11);
622 languages.Insert(lldb::eLanguageTypeC11);
623 languages.Insert(lldb::eLanguageTypeC_plus_plus_14);
624 return languages;
625}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000626
Adrian Prantlaa97a892019-08-22 21:45:58 +0000627LanguageSet ClangASTContext::GetSupportedLanguagesForExpressions() {
628 LanguageSet languages;
629 languages.Insert(lldb::eLanguageTypeC_plus_plus);
630 languages.Insert(lldb::eLanguageTypeObjC_plus_plus);
631 languages.Insert(lldb::eLanguageTypeC_plus_plus_03);
632 languages.Insert(lldb::eLanguageTypeC_plus_plus_11);
633 languages.Insert(lldb::eLanguageTypeC_plus_plus_14);
634 return languages;
Enrico Granata5d84a692014-08-19 21:46:37 +0000635}
636
Kate Stoneb9c1b512016-09-06 20:57:50 +0000637void ClangASTContext::Initialize() {
Adrian Prantlaa97a892019-08-22 21:45:58 +0000638 PluginManager::RegisterPlugin(
639 GetPluginNameStatic(), "clang base AST context plug-in", CreateInstance,
640 GetSupportedLanguagesForTypes(), GetSupportedLanguagesForExpressions());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641}
642
Kate Stoneb9c1b512016-09-06 20:57:50 +0000643void ClangASTContext::Terminate() {
644 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000645}
646
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647void ClangASTContext::Finalize() {
Raphael Isemann2eb963a2019-10-02 12:26:08 +0000648 assert(m_ast_up);
649 GetASTMap().Erase(m_ast_up.get());
650 if (!m_ast_owned)
651 m_ast_up.release();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000652
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000653 m_builtins_up.reset();
654 m_selector_table_up.reset();
655 m_identifier_table_up.reset();
656 m_target_info_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000657 m_target_options_rp.reset();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000658 m_diagnostics_engine_up.reset();
659 m_source_manager_up.reset();
660 m_language_options_up.reset();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000661 m_scratch_ast_source_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000662}
663
Raphael Isemannf74a4c12019-04-30 08:41:35 +0000664void ClangASTContext::setSema(Sema *s) {
665 // Ensure that the new sema actually belongs to our ASTContext.
666 assert(s == nullptr || &s->getASTContext() == m_ast_up.get());
667 m_sema = s;
668}
669
Kate Stoneb9c1b512016-09-06 20:57:50 +0000670const char *ClangASTContext::GetTargetTriple() {
671 return m_target_triple.c_str();
672}
673
Raphael Isemannd01b4a72019-10-01 12:28:14 +0000674void ClangASTContext::SetTargetTriple(llvm::StringRef target_triple) {
Raphael Isemannd01b4a72019-10-01 12:28:14 +0000675 m_target_triple = target_triple.str();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000676}
677
Kate Stoneb9c1b512016-09-06 20:57:50 +0000678void ClangASTContext::SetExternalSource(
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000679 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_up) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680 ASTContext *ast = getASTContext();
681 if (ast) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000682 ast->setExternalSource(ast_source_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000683 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000684 }
685}
686
Kate Stoneb9c1b512016-09-06 20:57:50 +0000687ASTContext *ClangASTContext::getASTContext() {
Raphael Isemann2eb963a2019-10-02 12:26:08 +0000688 assert(m_ast_up);
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000689 return m_ast_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000690}
691
Raphael Isemann2eb963a2019-10-02 12:26:08 +0000692void ClangASTContext::CreateASTContext() {
693 assert(!m_ast_up);
694 m_ast_owned = true;
695 m_ast_up.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
696 *getIdentifierTable(), *getSelectorTable(),
697 *getBuiltinContext()));
698
699 m_ast_up->getDiagnostics().setClient(getDiagnosticConsumer(), false);
700
701 // This can be NULL if we don't know anything about the architecture or if
702 // the target for an architecture isn't enabled in the llvm/clang that we
703 // built
704 TargetInfo *target_info = getTargetInfo();
705 if (target_info)
706 m_ast_up->InitBuiltinTypes(*target_info);
707
708 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
709 m_ast_up->getTranslationUnitDecl()->setHasExternalLexicalStorage();
710 // m_ast_up->getTranslationUnitDecl()->setHasExternalVisibleStorage();
711 }
712
713 GetASTMap().Insert(m_ast_up.get(), this);
714
715 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_up(
716 new ClangExternalASTSourceCallbacks(
717 ClangASTContext::CompleteTagDecl,
718 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
719 ClangASTContext::LayoutRecordType, this));
720 SetExternalSource(ast_source_up);
721}
722
Kate Stoneb9c1b512016-09-06 20:57:50 +0000723ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
724 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
725 return clang_ast;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000726}
727
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728Builtin::Context *ClangASTContext::getBuiltinContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000729 if (m_builtins_up == nullptr)
730 m_builtins_up.reset(new Builtin::Context());
731 return m_builtins_up.get();
Sean Callanan79439e82010-11-18 02:56:27 +0000732}
733
Kate Stoneb9c1b512016-09-06 20:57:50 +0000734IdentifierTable *ClangASTContext::getIdentifierTable() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000735 if (m_identifier_table_up == nullptr)
736 m_identifier_table_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000737 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000738 return m_identifier_table_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739}
740
Kate Stoneb9c1b512016-09-06 20:57:50 +0000741LangOptions *ClangASTContext::getLanguageOptions() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000742 if (m_language_options_up == nullptr) {
743 m_language_options_up.reset(new LangOptions());
Rainer Orth6ca17072019-08-05 14:00:43 +0000744 ParseLangArgs(*m_language_options_up, clang::Language::ObjCXX,
745 GetTargetTriple());
746 // InitializeLangOptions(*m_language_options_up, Language::ObjCXX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000747 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000748 return m_language_options_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000749}
750
Kate Stoneb9c1b512016-09-06 20:57:50 +0000751SelectorTable *ClangASTContext::getSelectorTable() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000752 if (m_selector_table_up == nullptr)
753 m_selector_table_up.reset(new SelectorTable());
754 return m_selector_table_up.get();
Greg Claytonfe689042015-11-10 17:47:04 +0000755}
756
Kate Stoneb9c1b512016-09-06 20:57:50 +0000757clang::FileManager *ClangASTContext::getFileManager() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000758 if (m_file_manager_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000759 clang::FileSystemOptions file_system_options;
Jonas Devlieghere9764b652019-02-18 20:31:18 +0000760 m_file_manager_up.reset(new clang::FileManager(
761 file_system_options, FileSystem::Instance().GetVirtualFileSystem()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000763 return m_file_manager_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764}
765
766clang::SourceManager *ClangASTContext::getSourceManager() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000767 if (m_source_manager_up == nullptr)
768 m_source_manager_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000770 return m_source_manager_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000771}
772
773clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000774 if (m_diagnostics_engine_up == nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000776 m_diagnostics_engine_up.reset(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000777 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
778 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000779 return m_diagnostics_engine_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000780}
781
782clang::MangleContext *ClangASTContext::getMangleContext() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000783 if (m_mangle_ctx_up == nullptr)
784 m_mangle_ctx_up.reset(getASTContext()->createMangleContext());
785 return m_mangle_ctx_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000786}
787
788class NullDiagnosticConsumer : public DiagnosticConsumer {
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000789public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000790 NullDiagnosticConsumer() {
791 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
792 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000793
Kate Stoneb9c1b512016-09-06 20:57:50 +0000794 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
Raphael Isemann17566302019-05-03 10:03:28 +0000795 const clang::Diagnostic &info) override {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000796 if (m_log) {
797 llvm::SmallVector<char, 32> diag_str(10);
798 info.FormatDiagnostic(diag_str);
799 diag_str.push_back('\0');
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000800 LLDB_LOGF(m_log, "Compiler diagnostic: %s\n", diag_str.data());
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000801 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000802 }
803
804 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
805 return new NullDiagnosticConsumer();
806 }
807
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000808private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000809 Log *m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000810};
811
Kate Stoneb9c1b512016-09-06 20:57:50 +0000812DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000813 if (m_diagnostic_consumer_up == nullptr)
814 m_diagnostic_consumer_up.reset(new NullDiagnosticConsumer);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000815
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000816 return m_diagnostic_consumer_up.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000817}
818
Kate Stoneb9c1b512016-09-06 20:57:50 +0000819std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000820 if (m_target_options_rp == nullptr && !m_target_triple.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000821 m_target_options_rp = std::make_shared<clang::TargetOptions>();
Jonas Devlieghere70355ac2019-02-12 03:47:39 +0000822 if (m_target_options_rp != nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000823 m_target_options_rp->Triple = m_target_triple;
824 }
825 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000826}
827
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828TargetInfo *ClangASTContext::getTargetInfo() {
829 // target_triple should be something like "x86_64-apple-macosx"
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000830 if (m_target_info_up == nullptr && !m_target_triple.empty())
831 m_target_info_up.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000832 getTargetOptions()));
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000833 return m_target_info_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000834}
835
836#pragma mark Basic Types
837
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
839 ASTContext *ast, QualType qual_type) {
840 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000841 return qual_type_bit_size == bit_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000842}
Greg Clayton56939cb2015-09-17 22:23:34 +0000843
Greg Claytona1e5dc82015-08-11 22:53:00 +0000844CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000845ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
846 size_t bit_size) {
847 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
848 getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000849}
850
Kate Stoneb9c1b512016-09-06 20:57:50 +0000851CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
852 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
Alex Langfordbddab072019-08-13 19:40:36 +0000853 auto *clang_ast_context = ClangASTContext::GetASTContext(ast);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000854 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000855 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000856 switch (encoding) {
857 case eEncodingInvalid:
858 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000859 return CompilerType(clang_ast_context, ast->VoidPtrTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000860 break;
861
862 case eEncodingUint:
863 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000864 return CompilerType(clang_ast_context,
865 ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000866 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000867 return CompilerType(clang_ast_context,
868 ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000869 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000870 return CompilerType(clang_ast_context,
871 ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000872 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000873 return CompilerType(clang_ast_context,
874 ast->UnsignedLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000875 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000876 return CompilerType(clang_ast_context,
877 ast->UnsignedLongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000878 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +0000879 return CompilerType(clang_ast_context,
880 ast->UnsignedInt128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000881 break;
882
883 case eEncodingSint:
884 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000885 return CompilerType(clang_ast_context,
886 ast->SignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000887 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000888 return CompilerType(clang_ast_context, ast->ShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000889 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000890 return CompilerType(clang_ast_context, ast->IntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000891 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000892 return CompilerType(clang_ast_context, ast->LongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000893 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000894 return CompilerType(clang_ast_context, ast->LongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000895 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +0000896 return CompilerType(clang_ast_context, ast->Int128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897 break;
898
899 case eEncodingIEEE754:
900 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000901 return CompilerType(clang_ast_context, ast->FloatTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000902 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000903 return CompilerType(clang_ast_context, ast->DoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000904 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000905 return CompilerType(clang_ast_context,
906 ast->LongDoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000907 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
Alex Langfordbddab072019-08-13 19:40:36 +0000908 return CompilerType(clang_ast_context, ast->HalfTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000909 break;
910
911 case eEncodingVector:
912 // Sanity check that bit_size is a multiple of 8's.
913 if (bit_size && !(bit_size & 0x7u))
914 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +0000915 clang_ast_context,
916 ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8)
917 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000918 break;
919 }
920
921 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000922}
923
Greg Clayton57ee3062013-07-11 22:46:58 +0000924lldb::BasicType
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000925ClangASTContext::GetBasicTypeEnumeration(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000926 if (name) {
927 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
928 static TypeNameToBasicTypeMap g_type_map;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000929 static llvm::once_flag g_once_flag;
930 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000931 // "void"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000932 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000933
934 // "char"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000935 g_type_map.Append(ConstString("char"), eBasicTypeChar);
936 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
937 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
938 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
939 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
940 g_type_map.Append(ConstString("unsigned wchar_t"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000941 eBasicTypeUnsignedWChar);
942 // "short"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000943 g_type_map.Append(ConstString("short"), eBasicTypeShort);
944 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
945 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
946 g_type_map.Append(ConstString("unsigned short int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000947 eBasicTypeUnsignedShort);
948
949 // "int"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000950 g_type_map.Append(ConstString("int"), eBasicTypeInt);
951 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
952 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
953 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000954
955 // "long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000956 g_type_map.Append(ConstString("long"), eBasicTypeLong);
957 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
958 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
959 g_type_map.Append(ConstString("unsigned long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000960 eBasicTypeUnsignedLong);
961
962 // "long long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000963 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
964 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
965 g_type_map.Append(ConstString("unsigned long long"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000966 eBasicTypeUnsignedLongLong);
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000967 g_type_map.Append(ConstString("unsigned long long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968 eBasicTypeUnsignedLongLong);
969
970 // "int128"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000971 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
972 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000973
974 // Miscellaneous
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000975 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
976 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
977 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
978 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
979 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
980 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
981 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000982 g_type_map.Sort();
983 });
984
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000985 return g_type_map.Find(name, eBasicTypeInvalid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000986 }
987 return eBasicTypeInvalid;
Greg Clayton57ee3062013-07-11 22:46:58 +0000988}
989
Raphael Isemannc502bae2019-11-20 12:40:08 +0100990CompilerType ClangASTContext::GetBasicType(ConstString name) {
991 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
992 return GetBasicType(basic_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000993}
994
995uint32_t ClangASTContext::GetPointerByteSize() {
996 if (m_pointer_byte_size == 0)
Adrian Prantld963a7c2019-01-15 18:07:52 +0000997 if (auto size = GetBasicType(lldb::eBasicTypeVoid)
998 .GetPointerType()
999 .GetByteSize(nullptr))
1000 m_pointer_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001 return m_pointer_byte_size;
1002}
1003
1004CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
Raphael Isemannc502bae2019-11-20 12:40:08 +01001005 clang::ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001006
Kate Stoneb9c1b512016-09-06 20:57:50 +00001007 lldb::opaque_compiler_type_t clang_type =
1008 GetOpaqueCompilerType(ast, basic_type);
1009
1010 if (clang_type)
1011 return CompilerType(GetASTContext(ast), clang_type);
1012 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001013}
1014
Kate Stoneb9c1b512016-09-06 20:57:50 +00001015CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1016 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1017 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001018
Kate Stoneb9c1b512016-09-06 20:57:50 +00001019#define streq(a, b) strcmp(a, b) == 0
1020 assert(ast != nullptr);
1021 if (ast) {
1022 switch (dw_ate) {
1023 default:
1024 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001025
Kate Stoneb9c1b512016-09-06 20:57:50 +00001026 case DW_ATE_address:
1027 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001028 return CompilerType(this, ast->VoidPtrTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001030
Kate Stoneb9c1b512016-09-06 20:57:50 +00001031 case DW_ATE_boolean:
1032 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001033 return CompilerType(this, ast->BoolTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001034 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001035 return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001037 return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001038 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001039 return CompilerType(this, ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001040 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001041
Kate Stoneb9c1b512016-09-06 20:57:50 +00001042 case DW_ATE_lo_user:
1043 // This has been seen to mean DW_AT_complex_integer
1044 if (type_name) {
1045 if (::strstr(type_name, "complex")) {
1046 CompilerType complex_int_clang_type =
1047 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1048 bit_size / 2);
Alex Langfordbddab072019-08-13 19:40:36 +00001049 return CompilerType(
1050 this, ast->getComplexType(
1051 ClangUtil::GetQualType(complex_int_clang_type))
1052 .getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001053 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001054 }
1055 break;
1056
1057 case DW_ATE_complex_float:
1058 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001059 return CompilerType(this, ast->FloatComplexTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001060 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001061 return CompilerType(this, ast->DoubleComplexTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001062 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001063 return CompilerType(this, ast->LongDoubleComplexTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001064 else {
1065 CompilerType complex_float_clang_type =
1066 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1067 bit_size / 2);
Alex Langfordbddab072019-08-13 19:40:36 +00001068 return CompilerType(
1069 this, ast->getComplexType(
1070 ClangUtil::GetQualType(complex_float_clang_type))
1071 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001072 }
1073 break;
1074
1075 case DW_ATE_float:
1076 if (streq(type_name, "float") &&
1077 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001078 return CompilerType(this, ast->FloatTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001079 if (streq(type_name, "double") &&
1080 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001081 return CompilerType(this, ast->DoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001082 if (streq(type_name, "long double") &&
1083 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001084 return CompilerType(this, ast->LongDoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001085 // Fall back to not requiring a name match
1086 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001087 return CompilerType(this, ast->FloatTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001088 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001089 return CompilerType(this, ast->DoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001090 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001091 return CompilerType(this, ast->LongDoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001093 return CompilerType(this, ast->HalfTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094 break;
1095
1096 case DW_ATE_signed:
1097 if (type_name) {
1098 if (streq(type_name, "wchar_t") &&
1099 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1100 (getTargetInfo() &&
1101 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
Alex Langfordbddab072019-08-13 19:40:36 +00001102 return CompilerType(this, ast->WCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001103 if (streq(type_name, "void") &&
1104 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001105 return CompilerType(this, ast->VoidTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001106 if (strstr(type_name, "long long") &&
1107 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001108 return CompilerType(this, ast->LongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001109 if (strstr(type_name, "long") &&
1110 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001111 return CompilerType(this, ast->LongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001112 if (strstr(type_name, "short") &&
1113 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001114 return CompilerType(this, ast->ShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001115 if (strstr(type_name, "char")) {
1116 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001117 return CompilerType(this, ast->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001118 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001119 return CompilerType(this, ast->SignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001120 }
1121 if (strstr(type_name, "int")) {
1122 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001123 return CompilerType(this, ast->IntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001124 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00001125 return CompilerType(this, ast->Int128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126 }
1127 }
1128 // We weren't able to match up a type name, just search by size
1129 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001130 return CompilerType(this, ast->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001131 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001132 return CompilerType(this, ast->ShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001133 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001134 return CompilerType(this, ast->IntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001135 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001136 return CompilerType(this, ast->LongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001137 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001138 return CompilerType(this, ast->LongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001139 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00001140 return CompilerType(this, ast->Int128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001141 break;
1142
1143 case DW_ATE_signed_char:
1144 if (ast->getLangOpts().CharIsSigned && type_name &&
1145 streq(type_name, "char")) {
1146 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001147 return CompilerType(this, ast->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001148 }
1149 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001150 return CompilerType(this, ast->SignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001151 break;
1152
1153 case DW_ATE_unsigned:
1154 if (type_name) {
1155 if (streq(type_name, "wchar_t")) {
1156 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1157 if (!(getTargetInfo() &&
1158 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
Alex Langfordbddab072019-08-13 19:40:36 +00001159 return CompilerType(this, ast->WCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001160 }
1161 }
1162 if (strstr(type_name, "long long")) {
1163 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001164 return CompilerType(this, ast->UnsignedLongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001165 } else if (strstr(type_name, "long")) {
1166 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001167 return CompilerType(this, ast->UnsignedLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001168 } else if (strstr(type_name, "short")) {
1169 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001170 return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001171 } else if (strstr(type_name, "char")) {
1172 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001173 return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001174 } else if (strstr(type_name, "int")) {
1175 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001176 return CompilerType(this, ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001177 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00001178 return CompilerType(this, ast->UnsignedInt128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001179 }
1180 }
1181 // We weren't able to match up a type name, just search by size
1182 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001183 return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001184 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001185 return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001186 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001187 return CompilerType(this, ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001188 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001189 return CompilerType(this, ast->UnsignedLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001190 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001191 return CompilerType(this, ast->UnsignedLongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001192 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00001193 return CompilerType(this, ast->UnsignedInt128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001194 break;
1195
1196 case DW_ATE_unsigned_char:
1197 if (!ast->getLangOpts().CharIsSigned && type_name &&
1198 streq(type_name, "char")) {
1199 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001200 return CompilerType(this, ast->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001201 }
1202 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001203 return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001204 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001205 return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001206 break;
1207
1208 case DW_ATE_imaginary_float:
1209 break;
1210
1211 case DW_ATE_UTF:
1212 if (type_name) {
Jonas Devliegherec46d39b2019-08-21 21:30:55 +00001213 if (streq(type_name, "char16_t"))
Alex Langfordbddab072019-08-13 19:40:36 +00001214 return CompilerType(this, ast->Char16Ty.getAsOpaquePtr());
Jonas Devlieghere6c9dc122019-08-23 04:11:38 +00001215 if (streq(type_name, "char32_t"))
Alex Langfordbddab072019-08-13 19:40:36 +00001216 return CompilerType(this, ast->Char32Ty.getAsOpaquePtr());
Jonas Devlieghere6c9dc122019-08-23 04:11:38 +00001217 if (streq(type_name, "char8_t"))
Jonas Devliegherec46d39b2019-08-21 21:30:55 +00001218 return CompilerType(this, ast->Char8Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001219 }
1220 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001221 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001222 }
1223 // This assert should fire for anything that we don't catch above so we know
1224 // to fix any issues we run into.
1225 if (type_name) {
1226 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1227 "DW_TAG_base_type '%s' encoded with "
1228 "DW_ATE = 0x%x, bit_size = %u\n",
1229 type_name, dw_ate, bit_size);
1230 } else {
1231 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1232 "DW_TAG_base_type encoded with "
1233 "DW_ATE = 0x%x, bit_size = %u\n",
1234 dw_ate, bit_size);
1235 }
1236 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237}
1238
Kate Stoneb9c1b512016-09-06 20:57:50 +00001239CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1240 if (ast)
Alex Langfordbddab072019-08-13 19:40:36 +00001241 return CompilerType(ClangASTContext::GetASTContext(ast),
1242 ast->UnknownAnyTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001243 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001244}
1245
Kate Stoneb9c1b512016-09-06 20:57:50 +00001246CompilerType ClangASTContext::GetCStringType(bool is_const) {
1247 ASTContext *ast = getASTContext();
1248 QualType char_type(ast->CharTy);
1249
1250 if (is_const)
1251 char_type.addConst();
1252
Alex Langfordbddab072019-08-13 19:40:36 +00001253 return CompilerType(this, ast->getPointerType(char_type).getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001254}
1255
Zachary Turner115209e2018-11-05 19:25:39 +00001256clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001257ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1258 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001259}
1260
Kate Stoneb9c1b512016-09-06 20:57:50 +00001261clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1262 clang::Decl *source_decl) {
1263 FileSystemOptions file_system_options;
1264 FileManager file_manager(file_system_options);
1265 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1266
Gabor Marton5ac6d492019-05-15 10:29:48 +00001267 if (llvm::Expected<clang::Decl *> ret_or_error =
1268 importer.Import(source_decl)) {
1269 return *ret_or_error;
1270 } else {
1271 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
1272 LLDB_LOG_ERROR(log, ret_or_error.takeError(), "Couldn't import decl: {0}");
1273 return nullptr;
1274 }
Greg Clayton526e5af2010-11-13 03:52:47 +00001275}
1276
Kate Stoneb9c1b512016-09-06 20:57:50 +00001277bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1278 bool ignore_qualifiers) {
1279 ClangASTContext *ast =
1280 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1281 if (!ast || ast != type2.GetTypeSystem())
1282 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001283
Kate Stoneb9c1b512016-09-06 20:57:50 +00001284 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1285 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001286
Kate Stoneb9c1b512016-09-06 20:57:50 +00001287 QualType type1_qual = ClangUtil::GetQualType(type1);
1288 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001289
Kate Stoneb9c1b512016-09-06 20:57:50 +00001290 if (ignore_qualifiers) {
1291 type1_qual = type1_qual.getUnqualifiedType();
1292 type2_qual = type2_qual.getUnqualifiedType();
1293 }
1294
1295 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001296}
1297
Alex Langfordcb68bd72019-08-23 06:11:32 +00001298CompilerType ClangASTContext::GetTypeForDecl(void *opaque_decl) {
1299 if (!opaque_decl)
1300 return CompilerType();
1301
1302 clang::Decl *decl = static_cast<clang::Decl *>(opaque_decl);
1303 if (auto *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl))
1304 return GetTypeForDecl(named_decl);
1305 return CompilerType();
1306}
1307
Kate Stoneb9c1b512016-09-06 20:57:50 +00001308CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1309 if (clang::ObjCInterfaceDecl *interface_decl =
1310 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1311 return GetTypeForDecl(interface_decl);
1312 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1313 return GetTypeForDecl(tag_decl);
1314 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001315}
1316
Kate Stoneb9c1b512016-09-06 20:57:50 +00001317CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001318 // No need to call the getASTContext() accessor (which can create the AST if
1319 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001320 // AST if our AST didn't already exist...
1321 ASTContext *ast = &decl->getASTContext();
1322 if (ast)
Alex Langfordbddab072019-08-13 19:40:36 +00001323 return CompilerType(ClangASTContext::GetASTContext(ast),
1324 ast->getTagDeclType(decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001325 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001326}
1327
Kate Stoneb9c1b512016-09-06 20:57:50 +00001328CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001329 // No need to call the getASTContext() accessor (which can create the AST if
1330 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001331 // AST if our AST didn't already exist...
1332 ASTContext *ast = &decl->getASTContext();
1333 if (ast)
Alex Langfordbddab072019-08-13 19:40:36 +00001334 return CompilerType(ClangASTContext::GetASTContext(ast),
1335 ast->getObjCInterfaceType(decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001336 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001337}
1338
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001339#pragma mark Structure, Unions, Classes
1340
shafikde2c7ca2019-10-28 14:26:54 -07001341CompilerType ClangASTContext::CreateRecordType(
1342 DeclContext *decl_ctx, AccessType access_type, const char *name, int kind,
1343 LanguageType language, ClangASTMetadata *metadata, bool exports_symbols) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001344 ASTContext *ast = getASTContext();
1345 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001346
Kate Stoneb9c1b512016-09-06 20:57:50 +00001347 if (decl_ctx == nullptr)
1348 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001349
Kate Stoneb9c1b512016-09-06 20:57:50 +00001350 if (language == eLanguageTypeObjC ||
1351 language == eLanguageTypeObjC_plus_plus) {
1352 bool isForwardDecl = true;
1353 bool isInternal = false;
1354 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1355 }
Greg Clayton9e409562010-07-28 02:04:09 +00001356
Kate Stoneb9c1b512016-09-06 20:57:50 +00001357 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
Adrian Prantl05097242018-04-30 16:49:04 +00001358 // we will need to update this code. I was told to currently always use the
1359 // CXXRecordDecl class since we often don't know from debug information if
1360 // something is struct or a class, so we default to always use the more
Kate Stoneb9c1b512016-09-06 20:57:50 +00001361 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001362
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001363 bool has_name = name && name[0];
Greg Claytonc4ffd662013-03-08 01:37:30 +00001364
Kate Stoneb9c1b512016-09-06 20:57:50 +00001365 CXXRecordDecl *decl = CXXRecordDecl::Create(
1366 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001367 SourceLocation(), has_name ? &ast->Idents.get(name) : nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001368
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001369 if (!has_name) {
1370 // In C++ a lambda is also represented as an unnamed class. This is
1371 // different from an *anonymous class* that the user wrote:
1372 //
1373 // struct A {
1374 // // anonymous class (GNU/MSVC extension)
1375 // struct {
1376 // int x;
1377 // };
1378 // // unnamed class within a class
1379 // struct {
1380 // int y;
1381 // } B;
1382 // };
1383 //
1384 // void f() {
1385 // // unammed class outside of a class
1386 // struct {
1387 // int z;
1388 // } C;
1389 // }
1390 //
1391 // Anonymous classes is a GNU/MSVC extension that clang supports. It
1392 // requires the anonymous class be embedded within a class. So the new
1393 // heuristic verifies this condition.
shafikde2c7ca2019-10-28 14:26:54 -07001394 if (isa<CXXRecordDecl>(decl_ctx) && exports_symbols)
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001395 decl->setAnonymousStructOrUnion(true);
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001396 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001397
1398 if (decl) {
1399 if (metadata)
1400 SetMetadata(ast, decl, *metadata);
1401
1402 if (access_type != eAccessNone)
1403 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1404
1405 if (decl_ctx)
1406 decl_ctx->addDecl(decl);
1407
Alex Langfordbddab072019-08-13 19:40:36 +00001408 return CompilerType(this, ast->getTagDeclType(decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001409 }
1410 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001411}
1412
Sean Callanan09e91ac2017-05-11 22:08:05 +00001413namespace {
1414 bool IsValueParam(const clang::TemplateArgument &argument) {
1415 return argument.getKind() == TemplateArgument::Integral;
1416 }
1417}
1418
Kate Stoneb9c1b512016-09-06 20:57:50 +00001419static TemplateParameterList *CreateTemplateParameterList(
1420 ASTContext *ast,
1421 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1422 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1423 const bool parameter_pack = false;
1424 const bool is_typename = false;
1425 const unsigned depth = 0;
Sean Callanan09e91ac2017-05-11 22:08:05 +00001426 const size_t num_template_params = template_param_infos.args.size();
1427 DeclContext *const decl_context =
1428 ast->getTranslationUnitDecl(); // Is this the right decl context?,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001429 for (size_t i = 0; i < num_template_params; ++i) {
1430 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001431
Kate Stoneb9c1b512016-09-06 20:57:50 +00001432 IdentifierInfo *identifier_info = nullptr;
1433 if (name && name[0])
1434 identifier_info = &ast->Idents.get(name);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001435 if (IsValueParam(template_param_infos.args[i])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001436 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001437 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001438 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1439 template_param_infos.args[i].getIntegralType(), parameter_pack,
1440 nullptr));
1441
1442 } else {
1443 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001444 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001445 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1446 is_typename, parameter_pack));
1447 }
1448 }
Eugene Zemtsova9d928c2017-09-29 03:15:08 +00001449
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001450 if (template_param_infos.packed_args) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001451 IdentifierInfo *identifier_info = nullptr;
1452 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1453 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1454 const bool parameter_pack_true = true;
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001455
1456 if (!template_param_infos.packed_args->args.empty() &&
1457 IsValueParam(template_param_infos.packed_args->args[0])) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001458 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001459 *ast, decl_context, SourceLocation(), SourceLocation(), depth,
1460 num_template_params, identifier_info,
Sean Callanan09e91ac2017-05-11 22:08:05 +00001461 template_param_infos.packed_args->args[0].getIntegralType(),
1462 parameter_pack_true, nullptr));
1463 } else {
1464 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001465 *ast, decl_context, SourceLocation(), SourceLocation(), depth,
1466 num_template_params, identifier_info, is_typename,
1467 parameter_pack_true));
Sean Callanan09e91ac2017-05-11 22:08:05 +00001468 }
1469 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001470 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1471 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1472 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1473 SourceLocation(), requires_clause);
1474 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001475}
1476
Kate Stoneb9c1b512016-09-06 20:57:50 +00001477clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1478 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1479 const char *name, const TemplateParameterInfos &template_param_infos) {
Adrian Prantld8f460e2018-05-02 16:55:16 +00001480 // /// Create a function template node.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001481 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001482
Kate Stoneb9c1b512016-09-06 20:57:50 +00001483 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001484
Kate Stoneb9c1b512016-09-06 20:57:50 +00001485 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1486 ast, template_param_infos, template_param_decls);
1487 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1488 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1489 template_param_list, func_decl);
1490
1491 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1492 i < template_param_decl_count; ++i) {
1493 // TODO: verify which decl context we should put template_param_decls into..
1494 template_param_decls[i]->setDeclContext(func_decl);
1495 }
1496
1497 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001498}
1499
Kate Stoneb9c1b512016-09-06 20:57:50 +00001500void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1501 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1502 const TemplateParameterInfos &infos) {
Shafik Yaghmoura0858e22019-07-17 20:16:13 +00001503 TemplateArgumentList *template_args_ptr =
1504 TemplateArgumentList::CreateCopy(func_decl->getASTContext(), infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001505
Shafik Yaghmoura0858e22019-07-17 20:16:13 +00001506 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl,
1507 template_args_ptr, nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001508}
1509
Kate Stoneb9c1b512016-09-06 20:57:50 +00001510ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1511 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1512 int kind, const TemplateParameterInfos &template_param_infos) {
1513 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001514
Kate Stoneb9c1b512016-09-06 20:57:50 +00001515 ClassTemplateDecl *class_template_decl = nullptr;
1516 if (decl_ctx == nullptr)
1517 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001518
Kate Stoneb9c1b512016-09-06 20:57:50 +00001519 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1520 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001521
Kate Stoneb9c1b512016-09-06 20:57:50 +00001522 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001523
Kate Stoneb9c1b512016-09-06 20:57:50 +00001524 for (NamedDecl *decl : result) {
1525 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001526 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001527 return class_template_decl;
1528 }
1529
1530 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1531
1532 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1533 ast, template_param_infos, template_param_decls);
1534
1535 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1536 *ast, (TagDecl::TagKind)kind,
1537 decl_ctx, // What decl context do we use here? TU? The actual decl
1538 // context?
1539 SourceLocation(), SourceLocation(), &identifier_info);
1540
1541 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1542 i < template_param_decl_count; ++i) {
1543 template_param_decls[i]->setDeclContext(template_cxx_decl);
1544 }
1545
1546 // With templated classes, we say that a class is templated with
1547 // specializations, but that the bare class has no functions.
1548 // template_cxx_decl->startDefinition();
1549 // template_cxx_decl->completeDefinition();
1550
1551 class_template_decl = ClassTemplateDecl::Create(
1552 *ast,
1553 decl_ctx, // What decl context do we use here? TU? The actual decl
1554 // context?
Pavel Labath4294de32017-01-12 10:44:16 +00001555 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
Richard Smith35b007e2019-02-15 21:48:09 +00001556 template_cxx_decl->setDescribedClassTemplate(class_template_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001557
1558 if (class_template_decl) {
1559 if (access_type != eAccessNone)
1560 class_template_decl->setAccess(
1561 ConvertAccessTypeToAccessSpecifier(access_type));
1562
1563 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1564 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1565
1566 decl_ctx->addDecl(class_template_decl);
1567
Sean Callanan5e9e1992011-10-26 01:06:27 +00001568#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001569 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001570#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001571 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001572
Kate Stoneb9c1b512016-09-06 20:57:50 +00001573 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001574}
1575
Frederic Rissf4e7e522018-04-02 16:18:32 +00001576TemplateTemplateParmDecl *
1577ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) {
1578 ASTContext *ast = getASTContext();
1579
1580 auto *decl_ctx = ast->getTranslationUnitDecl();
1581
1582 IdentifierInfo &identifier_info = ast->Idents.get(template_name);
1583 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1584
1585 ClangASTContext::TemplateParameterInfos template_param_infos;
1586 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1587 ast, template_param_infos, template_param_decls);
1588
1589 // LLDB needs to create those decls only to be able to display a
Adrian Prantl05097242018-04-30 16:49:04 +00001590 // type that includes a template template argument. Only the name matters for
1591 // this purpose, so we use dummy values for the other characterisitcs of the
1592 // type.
Frederic Rissf4e7e522018-04-02 16:18:32 +00001593 return TemplateTemplateParmDecl::Create(
1594 *ast, decl_ctx, SourceLocation(),
1595 /*Depth*/ 0, /*Position*/ 0,
1596 /*IsParameterPack*/ false, &identifier_info, template_param_list);
1597}
1598
Greg Claytonf0705c82011-10-22 03:33:13 +00001599ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001600ClangASTContext::CreateClassTemplateSpecializationDecl(
1601 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1602 const TemplateParameterInfos &template_param_infos) {
1603 ASTContext *ast = getASTContext();
Sean Callanan09e91ac2017-05-11 22:08:05 +00001604 llvm::SmallVector<clang::TemplateArgument, 2> args(
1605 template_param_infos.args.size() +
1606 (template_param_infos.packed_args ? 1 : 0));
1607 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1608 args.begin());
1609 if (template_param_infos.packed_args) {
1610 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1611 *ast, template_param_infos.packed_args->args);
1612 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001613 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1614 ClassTemplateSpecializationDecl::Create(
1615 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Sean Callanan09e91ac2017-05-11 22:08:05 +00001616 SourceLocation(), class_template_decl, args,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001617 nullptr);
1618
1619 class_template_specialization_decl->setSpecializationKind(
1620 TSK_ExplicitSpecialization);
1621
1622 return class_template_specialization_decl;
1623}
1624
1625CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1626 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1627 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001628 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001629 if (ast)
1630 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00001631 this, ast->getTagDeclType(class_template_specialization_decl)
1632 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001633 }
1634 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001635}
1636
Kate Stoneb9c1b512016-09-06 20:57:50 +00001637static inline bool check_op_param(bool is_method,
1638 clang::OverloadedOperatorKind op_kind,
1639 bool unary, bool binary,
1640 uint32_t num_params) {
1641 // Special-case call since it can take any number of operands
1642 if (op_kind == OO_Call)
1643 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001644
Kate Stoneb9c1b512016-09-06 20:57:50 +00001645 // The parameter count doesn't include "this"
1646 if (is_method)
1647 ++num_params;
1648 if (num_params == 1)
1649 return unary;
1650 if (num_params == 2)
1651 return binary;
1652 else
Greg Clayton090d0982011-06-19 03:43:27 +00001653 return false;
1654}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001655
Kate Stoneb9c1b512016-09-06 20:57:50 +00001656bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1657 bool is_method, clang::OverloadedOperatorKind op_kind,
1658 uint32_t num_params) {
1659 switch (op_kind) {
1660 default:
1661 break;
1662 // C++ standard allows any number of arguments to new/delete
1663 case OO_New:
1664 case OO_Array_New:
1665 case OO_Delete:
1666 case OO_Array_Delete:
1667 return true;
1668 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001669
Kate Stoneb9c1b512016-09-06 20:57:50 +00001670#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1671 case OO_##Name: \
1672 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1673 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001674#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001675 default:
1676 break;
1677 }
1678 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001679}
1680
Greg Clayton57ee3062013-07-11 22:46:58 +00001681clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001682ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1683 clang::AccessSpecifier rhs) {
1684 // Make the access equal to the stricter of the field and the nested field's
1685 // access
1686 if (lhs == AS_none || rhs == AS_none)
1687 return AS_none;
1688 if (lhs == AS_private || rhs == AS_private)
1689 return AS_private;
1690 if (lhs == AS_protected || rhs == AS_protected)
1691 return AS_protected;
1692 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001693}
1694
Kate Stoneb9c1b512016-09-06 20:57:50 +00001695bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1696 uint32_t &bitfield_bit_size) {
Raphael Isemann02e91132019-11-20 12:09:19 +01001697 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001698 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001699 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001700
Kate Stoneb9c1b512016-09-06 20:57:50 +00001701 if (field->isBitField()) {
1702 Expr *bit_width_expr = field->getBitWidth();
1703 if (bit_width_expr) {
1704 llvm::APSInt bit_width_apsint;
1705 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1706 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001707 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001708 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001709 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001710 }
1711 return false;
1712}
1713
1714bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1715 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001716 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001717
1718 if (!record_decl->field_empty())
1719 return true;
1720
1721 // No fields, lets check this is a CXX record and check the base classes
1722 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1723 if (cxx_record_decl) {
1724 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1725 for (base_class = cxx_record_decl->bases_begin(),
1726 base_class_end = cxx_record_decl->bases_end();
1727 base_class != base_class_end; ++base_class) {
1728 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1729 base_class->getType()->getAs<RecordType>()->getDecl());
1730 if (RecordHasFields(base_class_decl))
1731 return true;
1732 }
1733 }
1734 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001735}
1736
Adrian Prantl4e8be2c2018-06-13 16:21:24 +00001737#pragma mark Objective-C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001738
Kate Stoneb9c1b512016-09-06 20:57:50 +00001739CompilerType ClangASTContext::CreateObjCClass(const char *name,
1740 DeclContext *decl_ctx,
1741 bool isForwardDecl,
1742 bool isInternal,
1743 ClangASTMetadata *metadata) {
1744 ASTContext *ast = getASTContext();
1745 assert(ast != nullptr);
1746 assert(name && name[0]);
1747 if (decl_ctx == nullptr)
1748 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001749
Kate Stoneb9c1b512016-09-06 20:57:50 +00001750 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1751 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1752 nullptr, SourceLocation(),
1753 /*isForwardDecl,*/
1754 isInternal);
1755
1756 if (decl && metadata)
1757 SetMetadata(ast, decl, *metadata);
1758
Alex Langfordbddab072019-08-13 19:40:36 +00001759 return CompilerType(this, ast->getObjCInterfaceType(decl).getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001760}
1761
Kate Stoneb9c1b512016-09-06 20:57:50 +00001762static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001763 return !ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001764}
1765
Greg Clayton57ee3062013-07-11 22:46:58 +00001766uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001767ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1768 bool omit_empty_base_classes) {
1769 uint32_t num_bases = 0;
1770 if (cxx_record_decl) {
1771 if (omit_empty_base_classes) {
1772 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1773 for (base_class = cxx_record_decl->bases_begin(),
1774 base_class_end = cxx_record_decl->bases_end();
1775 base_class != base_class_end; ++base_class) {
1776 // Skip empty base classes
1777 if (omit_empty_base_classes) {
1778 if (BaseSpecifierIsEmpty(base_class))
1779 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001780 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001781 ++num_bases;
1782 }
1783 } else
1784 num_bases = cxx_record_decl->getNumBases();
1785 }
1786 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001787}
1788
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001789#pragma mark Namespace Declarations
1790
Raphael Isemanna9469972019-03-12 07:45:04 +00001791NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1792 const char *name, DeclContext *decl_ctx, bool is_inline) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001793 NamespaceDecl *namespace_decl = nullptr;
1794 ASTContext *ast = getASTContext();
1795 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1796 if (decl_ctx == nullptr)
1797 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001798
Kate Stoneb9c1b512016-09-06 20:57:50 +00001799 if (name) {
1800 IdentifierInfo &identifier_info = ast->Idents.get(name);
1801 DeclarationName decl_name(&identifier_info);
1802 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1803 for (NamedDecl *decl : result) {
1804 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1805 if (namespace_decl)
1806 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001807 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001808
1809 namespace_decl =
Raphael Isemanna9469972019-03-12 07:45:04 +00001810 NamespaceDecl::Create(*ast, decl_ctx, is_inline, SourceLocation(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001811 SourceLocation(), &identifier_info, nullptr);
1812
1813 decl_ctx->addDecl(namespace_decl);
1814 } else {
1815 if (decl_ctx == translation_unit_decl) {
1816 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1817 if (namespace_decl)
1818 return namespace_decl;
1819
1820 namespace_decl =
1821 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1822 SourceLocation(), nullptr, nullptr);
1823 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1824 translation_unit_decl->addDecl(namespace_decl);
1825 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1826 } else {
1827 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1828 if (parent_namespace_decl) {
1829 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1830 if (namespace_decl)
1831 return namespace_decl;
1832 namespace_decl =
1833 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1834 SourceLocation(), nullptr, nullptr);
1835 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1836 parent_namespace_decl->addDecl(namespace_decl);
1837 assert(namespace_decl ==
1838 parent_namespace_decl->getAnonymousNamespace());
1839 } else {
Raphael Isemannc4944812019-07-04 19:49:31 +00001840 assert(false && "GetUniqueNamespaceDeclaration called with no name and "
1841 "no namespace as decl_ctx");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001842 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001843 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001844 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001845#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001846 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001847#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001848 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001849}
1850
Paul Hermand628cbb2015-09-15 23:44:17 +00001851clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001852ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1853 if (ctx != nullptr) {
1854 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1855 clang::SourceLocation());
1856 ctx->addDecl(decl);
1857 return decl;
1858 }
1859 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001860}
1861
Kate Stoneb9c1b512016-09-06 20:57:50 +00001862clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1863 clang::DeclContext *right,
1864 clang::DeclContext *root) {
1865 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001866 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001867
1868 std::set<clang::DeclContext *> path_left;
1869 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1870 path_left.insert(d);
1871
1872 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1873 if (path_left.find(d) != path_left.end())
1874 return d;
1875
1876 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00001877}
1878
Kate Stoneb9c1b512016-09-06 20:57:50 +00001879clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1880 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1881 if (decl_ctx != nullptr && ns_decl != nullptr) {
1882 clang::TranslationUnitDecl *translation_unit =
1883 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1884 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1885 *getASTContext(), decl_ctx, clang::SourceLocation(),
1886 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1887 clang::SourceLocation(), ns_decl,
1888 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
1889 decl_ctx->addDecl(using_decl);
1890 return using_decl;
1891 }
1892 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001893}
1894
1895clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001896ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1897 clang::NamedDecl *target) {
1898 if (current_decl_ctx != nullptr && target != nullptr) {
1899 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1900 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
1901 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1902 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1903 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
1904 target);
1905 using_decl->addShadowDecl(shadow_decl);
1906 current_decl_ctx->addDecl(using_decl);
1907 return using_decl;
1908 }
1909 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001910}
1911
Kate Stoneb9c1b512016-09-06 20:57:50 +00001912clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
1913 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
1914 if (decl_context != nullptr) {
1915 clang::VarDecl *var_decl = clang::VarDecl::Create(
1916 *getASTContext(), decl_context, clang::SourceLocation(),
1917 clang::SourceLocation(),
1918 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
1919 nullptr, clang::SC_None);
1920 var_decl->setAccess(clang::AS_public);
1921 decl_context->addDecl(var_decl);
1922 return var_decl;
1923 }
1924 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001925}
1926
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001927lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001928ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
1929 lldb::BasicType basic_type) {
1930 switch (basic_type) {
1931 case eBasicTypeVoid:
1932 return ast->VoidTy.getAsOpaquePtr();
1933 case eBasicTypeChar:
1934 return ast->CharTy.getAsOpaquePtr();
1935 case eBasicTypeSignedChar:
1936 return ast->SignedCharTy.getAsOpaquePtr();
1937 case eBasicTypeUnsignedChar:
1938 return ast->UnsignedCharTy.getAsOpaquePtr();
1939 case eBasicTypeWChar:
1940 return ast->getWCharType().getAsOpaquePtr();
1941 case eBasicTypeSignedWChar:
1942 return ast->getSignedWCharType().getAsOpaquePtr();
1943 case eBasicTypeUnsignedWChar:
1944 return ast->getUnsignedWCharType().getAsOpaquePtr();
1945 case eBasicTypeChar16:
1946 return ast->Char16Ty.getAsOpaquePtr();
1947 case eBasicTypeChar32:
1948 return ast->Char32Ty.getAsOpaquePtr();
1949 case eBasicTypeShort:
1950 return ast->ShortTy.getAsOpaquePtr();
1951 case eBasicTypeUnsignedShort:
1952 return ast->UnsignedShortTy.getAsOpaquePtr();
1953 case eBasicTypeInt:
1954 return ast->IntTy.getAsOpaquePtr();
1955 case eBasicTypeUnsignedInt:
1956 return ast->UnsignedIntTy.getAsOpaquePtr();
1957 case eBasicTypeLong:
1958 return ast->LongTy.getAsOpaquePtr();
1959 case eBasicTypeUnsignedLong:
1960 return ast->UnsignedLongTy.getAsOpaquePtr();
1961 case eBasicTypeLongLong:
1962 return ast->LongLongTy.getAsOpaquePtr();
1963 case eBasicTypeUnsignedLongLong:
1964 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1965 case eBasicTypeInt128:
1966 return ast->Int128Ty.getAsOpaquePtr();
1967 case eBasicTypeUnsignedInt128:
1968 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1969 case eBasicTypeBool:
1970 return ast->BoolTy.getAsOpaquePtr();
1971 case eBasicTypeHalf:
1972 return ast->HalfTy.getAsOpaquePtr();
1973 case eBasicTypeFloat:
1974 return ast->FloatTy.getAsOpaquePtr();
1975 case eBasicTypeDouble:
1976 return ast->DoubleTy.getAsOpaquePtr();
1977 case eBasicTypeLongDouble:
1978 return ast->LongDoubleTy.getAsOpaquePtr();
1979 case eBasicTypeFloatComplex:
1980 return ast->FloatComplexTy.getAsOpaquePtr();
1981 case eBasicTypeDoubleComplex:
1982 return ast->DoubleComplexTy.getAsOpaquePtr();
1983 case eBasicTypeLongDoubleComplex:
1984 return ast->LongDoubleComplexTy.getAsOpaquePtr();
1985 case eBasicTypeObjCID:
1986 return ast->getObjCIdType().getAsOpaquePtr();
1987 case eBasicTypeObjCClass:
1988 return ast->getObjCClassType().getAsOpaquePtr();
1989 case eBasicTypeObjCSel:
1990 return ast->getObjCSelType().getAsOpaquePtr();
1991 case eBasicTypeNullPtr:
1992 return ast->NullPtrTy.getAsOpaquePtr();
1993 default:
1994 return nullptr;
1995 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001996}
1997
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001998#pragma mark Function Types
1999
Pavel Labath1ac2b202016-08-15 14:32:32 +00002000clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00002001ClangASTContext::GetDeclarationName(const char *name,
2002 const CompilerType &function_clang_type) {
2003 if (!name || !name[0])
2004 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002005
Kate Stoneb9c1b512016-09-06 20:57:50 +00002006 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
2007 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
2008 return DeclarationName(&getASTContext()->Idents.get(
2009 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00002010
Adrian Prantl05097242018-04-30 16:49:04 +00002011 // Check the number of operator parameters. Sometimes we have seen bad DWARF
2012 // that doesn't correctly describe operators and if we try to create a method
2013 // and add it to the class, clang will assert and crash, so we need to make
2014 // sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002015 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
2016 const clang::FunctionProtoType *function_type =
2017 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
2018 if (function_type == nullptr)
2019 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002020
Kate Stoneb9c1b512016-09-06 20:57:50 +00002021 const bool is_method = false;
2022 const unsigned int num_params = function_type->getNumParams();
2023 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
2024 is_method, op_kind, num_params))
2025 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002026
Kate Stoneb9c1b512016-09-06 20:57:50 +00002027 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00002028}
2029
Kate Stoneb9c1b512016-09-06 20:57:50 +00002030FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
2031 DeclContext *decl_ctx, const char *name,
2032 const CompilerType &function_clang_type, int storage, bool is_inline) {
2033 FunctionDecl *func_decl = nullptr;
2034 ASTContext *ast = getASTContext();
2035 if (decl_ctx == nullptr)
2036 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002037
Kate Stoneb9c1b512016-09-06 20:57:50 +00002038 const bool hasWrittenPrototype = true;
2039 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00002040
Kate Stoneb9c1b512016-09-06 20:57:50 +00002041 clang::DeclarationName declarationName =
2042 GetDeclarationName(name, function_clang_type);
2043 func_decl = FunctionDecl::Create(
2044 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2045 ClangUtil::GetQualType(function_clang_type), nullptr,
2046 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002047 isConstexprSpecified ? CSK_constexpr : CSK_unspecified);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002048 if (func_decl)
2049 decl_ctx->addDecl(func_decl);
2050
Sean Callanan5e9e1992011-10-26 01:06:27 +00002051#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00002052 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002053#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002054
2055 return func_decl;
2056}
2057
2058CompilerType ClangASTContext::CreateFunctionType(
2059 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002060 unsigned num_args, bool is_variadic, unsigned type_quals,
2061 clang::CallingConv cc) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002062 if (ast == nullptr)
2063 return CompilerType(); // invalid AST
2064
2065 if (!result_type || !ClangUtil::IsClangType(result_type))
2066 return CompilerType(); // invalid return type
2067
2068 std::vector<QualType> qual_type_args;
2069 if (num_args > 0 && args == nullptr)
2070 return CompilerType(); // invalid argument array passed in
2071
2072 // Verify that all arguments are valid and the right type
2073 for (unsigned i = 0; i < num_args; ++i) {
2074 if (args[i]) {
2075 // Make sure we have a clang type in args[i] and not a type from another
2076 // language whose name might match
2077 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2078 lldbassert(is_clang_type);
2079 if (is_clang_type)
2080 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2081 else
2082 return CompilerType(); // invalid argument type (must be a clang type)
2083 } else
2084 return CompilerType(); // invalid argument type (empty)
2085 }
2086
2087 // TODO: Detect calling convention in DWARF?
2088 FunctionProtoType::ExtProtoInfo proto_info;
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002089 proto_info.ExtInfo = cc;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002090 proto_info.Variadic = is_variadic;
2091 proto_info.ExceptionSpec = EST_None;
Mikael Nilsson8b3bf6c2018-12-13 10:17:26 +00002092 proto_info.TypeQuals = clang::Qualifiers::fromFastMask(type_quals);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002093 proto_info.RefQualifier = RQ_None;
2094
Alex Langfordbddab072019-08-13 19:40:36 +00002095 return CompilerType(ClangASTContext::GetASTContext(ast),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002096 ast->getFunctionType(ClangUtil::GetQualType(result_type),
Alex Langfordbddab072019-08-13 19:40:36 +00002097 qual_type_args, proto_info).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002098}
2099
2100ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
Zachary Turner6753d2d2018-12-12 17:17:53 +00002101 clang::DeclContext *decl_ctx, const char *name,
Shafik Yaghmourfa5c3402019-08-02 21:41:50 +00002102 const CompilerType &param_type, int storage, bool add_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002103 ASTContext *ast = getASTContext();
2104 assert(ast != nullptr);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00002105 auto *decl =
2106 ParmVarDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(),
2107 name && name[0] ? &ast->Idents.get(name) : nullptr,
2108 ClangUtil::GetQualType(param_type), nullptr,
2109 (clang::StorageClass)storage, nullptr);
Shafik Yaghmourfa5c3402019-08-02 21:41:50 +00002110 if (add_decl)
2111 decl_ctx->addDecl(decl);
2112
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00002113 return decl;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002114}
2115
2116void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2117 ParmVarDecl **params,
2118 unsigned num_params) {
2119 if (function_decl)
2120 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002121}
2122
Greg Claytona1e5dc82015-08-11 22:53:00 +00002123CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002124ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00002125 QualType block_type = m_ast_up->getBlockPointerType(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002126 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002127
Kate Stoneb9c1b512016-09-06 20:57:50 +00002128 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002129}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002130
2131#pragma mark Array Types
2132
Kate Stoneb9c1b512016-09-06 20:57:50 +00002133CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2134 size_t element_count,
2135 bool is_vector) {
2136 if (element_type.IsValid()) {
2137 ASTContext *ast = getASTContext();
2138 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002139
Kate Stoneb9c1b512016-09-06 20:57:50 +00002140 if (is_vector) {
2141 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00002142 this, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2143 element_count)
2144 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002145 } else {
2146
2147 llvm::APInt ap_element_count(64, element_count);
2148 if (element_count == 0) {
Alex Langfordbddab072019-08-13 19:40:36 +00002149 return CompilerType(this, ast->getIncompleteArrayType(
2150 ClangUtil::GetQualType(element_type),
2151 clang::ArrayType::Normal, 0)
2152 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002153 } else {
Alex Langfordbddab072019-08-13 19:40:36 +00002154 return CompilerType(this, ast->getConstantArrayType(
2155 ClangUtil::GetQualType(element_type),
Richard Smith772e2662019-10-04 01:25:59 +00002156 ap_element_count, nullptr,
Alex Langfordbddab072019-08-13 19:40:36 +00002157 clang::ArrayType::Normal, 0)
2158 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002159 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002160 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002161 }
2162 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002163}
2164
Kate Stoneb9c1b512016-09-06 20:57:50 +00002165CompilerType ClangASTContext::CreateStructForIdentifier(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002166 ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002167 const std::initializer_list<std::pair<const char *, CompilerType>>
2168 &type_fields,
2169 bool packed) {
2170 CompilerType type;
2171 if (!type_name.IsEmpty() &&
2172 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2173 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002174 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002175 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002176 }
2177
2178 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2179 clang::TTK_Struct, lldb::eLanguageTypeC);
2180 StartTagDeclarationDefinition(type);
2181 for (const auto &field : type_fields)
2182 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2183 0);
2184 if (packed)
2185 SetIsPacked(type);
2186 CompleteTagDeclarationDefinition(type);
2187 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002188}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002189
Kate Stoneb9c1b512016-09-06 20:57:50 +00002190CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002191 ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002192 const std::initializer_list<std::pair<const char *, CompilerType>>
2193 &type_fields,
2194 bool packed) {
2195 CompilerType type;
2196 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2197 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002198
Kate Stoneb9c1b512016-09-06 20:57:50 +00002199 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002200}
2201
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002202#pragma mark Enumeration Types
2203
Greg Claytona1e5dc82015-08-11 22:53:00 +00002204CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002205ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2206 const Declaration &decl,
Tamas Berghammer59765832017-11-07 10:39:22 +00002207 const CompilerType &integer_clang_type,
2208 bool is_scoped) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002209 // TODO: Do something intelligent with the Declaration object passed in
2210 // like maybe filling in the SourceLocation with it...
2211 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002212
Kate Stoneb9c1b512016-09-06 20:57:50 +00002213 // TODO: ask about these...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002214 // const bool IsFixed = false;
2215
2216 EnumDecl *enum_decl = EnumDecl::Create(
2217 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2218 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
Tamas Berghammercf6bf4c2017-11-07 13:43:55 +00002219 is_scoped, // IsScoped
2220 is_scoped, // IsScopedUsingClassTag
2221 false); // IsFixed
Kate Stoneb9c1b512016-09-06 20:57:50 +00002222
2223 if (enum_decl) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00002224 if (decl_ctx)
2225 decl_ctx->addDecl(enum_decl);
2226
Kate Stoneb9c1b512016-09-06 20:57:50 +00002227 // TODO: check if we should be setting the promotion type too?
2228 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2229
2230 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2231
Alex Langfordbddab072019-08-13 19:40:36 +00002232 return CompilerType(this, ast->getTagDeclType(enum_decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002233 }
2234 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002235}
2236
Kate Stoneb9c1b512016-09-06 20:57:50 +00002237CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2238 size_t bit_size,
2239 bool is_signed) {
2240 if (ast) {
Alex Langfordbddab072019-08-13 19:40:36 +00002241 auto *clang_ast_context = ClangASTContext::GetASTContext(ast);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002242 if (is_signed) {
2243 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002244 return CompilerType(clang_ast_context,
2245 ast->SignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002246
2247 if (bit_size == ast->getTypeSize(ast->ShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002248 return CompilerType(clang_ast_context, ast->ShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002249
2250 if (bit_size == ast->getTypeSize(ast->IntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002251 return CompilerType(clang_ast_context, ast->IntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002252
2253 if (bit_size == ast->getTypeSize(ast->LongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002254 return CompilerType(clang_ast_context, ast->LongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002255
2256 if (bit_size == ast->getTypeSize(ast->LongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002257 return CompilerType(clang_ast_context,
2258 ast->LongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002259
2260 if (bit_size == ast->getTypeSize(ast->Int128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00002261 return CompilerType(clang_ast_context, ast->Int128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002262 } else {
2263 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002264 return CompilerType(clang_ast_context,
2265 ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002266
2267 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002268 return CompilerType(clang_ast_context,
2269 ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002270
2271 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002272 return CompilerType(clang_ast_context,
2273 ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002274
2275 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002276 return CompilerType(clang_ast_context,
2277 ast->UnsignedLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002278
2279 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002280 return CompilerType(clang_ast_context,
2281 ast->UnsignedLongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002282
2283 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00002284 return CompilerType(clang_ast_context,
2285 ast->UnsignedInt128Ty.getAsOpaquePtr());
Enrico Granatae8bf7492014-08-15 23:00:02 +00002286 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002287 }
2288 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002289}
2290
Kate Stoneb9c1b512016-09-06 20:57:50 +00002291CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2292 bool is_signed) {
2293 if (ast)
2294 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2295 is_signed);
2296 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002297}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002298
Kate Stoneb9c1b512016-09-06 20:57:50 +00002299void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2300 if (decl_ctx) {
2301 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002302
Kate Stoneb9c1b512016-09-06 20:57:50 +00002303 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2304 if (named_decl) {
2305 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2306 named_decl->getDeclName().getAsString().c_str());
2307 } else {
2308 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002309 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002310 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002311}
2312
Kate Stoneb9c1b512016-09-06 20:57:50 +00002313void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2314 if (decl == nullptr)
2315 return;
2316 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002317
Kate Stoneb9c1b512016-09-06 20:57:50 +00002318 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2319 if (record_decl) {
2320 printf("%20s: %s%s\n", decl->getDeclKindName(),
2321 record_decl->getDeclName().getAsString().c_str(),
2322 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002323
Kate Stoneb9c1b512016-09-06 20:57:50 +00002324 } else {
2325 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2326 if (named_decl) {
2327 printf("%20s: %s\n", decl->getDeclKindName(),
2328 named_decl->getDeclName().getAsString().c_str());
2329 } else {
2330 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002331 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002332 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002333}
2334
Kate Stoneb9c1b512016-09-06 20:57:50 +00002335bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2336 clang::Decl *rhs_decl) {
2337 if (lhs_decl && rhs_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002338 // Make sure the decl kinds match first
Kate Stoneb9c1b512016-09-06 20:57:50 +00002339 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2340 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002341
Kate Stoneb9c1b512016-09-06 20:57:50 +00002342 if (lhs_decl_kind == rhs_decl_kind) {
Adrian Prantl05097242018-04-30 16:49:04 +00002343 // Now check that the decl contexts kinds are all equivalent before we
2344 // have to check any names of the decl contexts...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002345 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2346 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2347 if (lhs_decl_ctx && rhs_decl_ctx) {
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002348 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002349 if (lhs_decl_ctx && rhs_decl_ctx) {
2350 const clang::Decl::Kind lhs_decl_ctx_kind =
2351 lhs_decl_ctx->getDeclKind();
2352 const clang::Decl::Kind rhs_decl_ctx_kind =
2353 rhs_decl_ctx->getDeclKind();
2354 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2355 lhs_decl_ctx = lhs_decl_ctx->getParent();
2356 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002357
Kate Stoneb9c1b512016-09-06 20:57:50 +00002358 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2359 break;
2360 } else
2361 return false;
2362 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002363 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002364 }
2365
Kate Stoneb9c1b512016-09-06 20:57:50 +00002366 // Now make sure the name of the decls match
Kate Stoneb9c1b512016-09-06 20:57:50 +00002367 clang::NamedDecl *lhs_named_decl =
2368 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2369 clang::NamedDecl *rhs_named_decl =
2370 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2371 if (lhs_named_decl && rhs_named_decl) {
2372 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2373 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2374 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2375 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2376 return false;
2377 } else
Greg Claytona2721472011-06-25 00:44:06 +00002378 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002379 } else
2380 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002381
Adrian Prantl05097242018-04-30 16:49:04 +00002382 // We know that the decl context kinds all match, so now we need to
2383 // make sure the names match as well
Kate Stoneb9c1b512016-09-06 20:57:50 +00002384 lhs_decl_ctx = lhs_decl->getDeclContext();
2385 rhs_decl_ctx = rhs_decl->getDeclContext();
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002386 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002387 switch (lhs_decl_ctx->getDeclKind()) {
2388 case clang::Decl::TranslationUnit:
2389 // We don't care about the translation unit names
2390 return true;
2391 default: {
2392 clang::NamedDecl *lhs_named_decl =
2393 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2394 clang::NamedDecl *rhs_named_decl =
2395 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2396 if (lhs_named_decl && rhs_named_decl) {
2397 clang::DeclarationName lhs_decl_name =
2398 lhs_named_decl->getDeclName();
2399 clang::DeclarationName rhs_decl_name =
2400 rhs_named_decl->getDeclName();
2401 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2402 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2403 return false;
2404 } else
2405 return false;
2406 } else
2407 return false;
2408 } break;
2409 }
2410 lhs_decl_ctx = lhs_decl_ctx->getParent();
2411 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002412 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002413 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002414 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002415 }
2416 return false;
2417}
2418bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2419 clang::Decl *decl) {
2420 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002421 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002422
Kate Stoneb9c1b512016-09-06 20:57:50 +00002423 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002424
Kate Stoneb9c1b512016-09-06 20:57:50 +00002425 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002426 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002427
2428 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2429 if (tag_decl->isCompleteDefinition())
2430 return true;
2431
2432 if (!tag_decl->hasExternalLexicalStorage())
2433 return false;
2434
2435 ast_source->CompleteType(tag_decl);
2436
2437 return !tag_decl->getTypeForDecl()->isIncompleteType();
2438 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2439 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2440 if (objc_interface_decl->getDefinition())
2441 return true;
2442
2443 if (!objc_interface_decl->hasExternalLexicalStorage())
2444 return false;
2445
2446 ast_source->CompleteType(objc_interface_decl);
2447
2448 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2449 } else {
2450 return false;
2451 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002452}
2453
Kate Stoneb9c1b512016-09-06 20:57:50 +00002454void ClangASTContext::SetMetadataAsUserID(const void *object,
2455 user_id_t user_id) {
2456 ClangASTMetadata meta_data;
2457 meta_data.SetUserID(user_id);
2458 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002459}
2460
Kate Stoneb9c1b512016-09-06 20:57:50 +00002461void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2462 ClangASTMetadata &metadata) {
2463 ClangExternalASTSourceCommon *external_source =
2464 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2465
2466 if (external_source)
2467 external_source->SetMetadata(object, metadata);
2468}
2469
2470ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2471 const void *object) {
2472 ClangExternalASTSourceCommon *external_source =
2473 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2474
2475 if (external_source && external_source->HasMetadata(object))
2476 return external_source->GetMetadata(object);
2477 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002478 return nullptr;
2479}
2480
Kate Stoneb9c1b512016-09-06 20:57:50 +00002481bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2482 int kind) const {
2483 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2484 if (clang_type) {
2485 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2486 if (tag_type) {
2487 clang::TagDecl *tag_decl =
2488 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2489 if (tag_decl) {
2490 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2491 return true;
2492 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002493 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002494 }
2495 return false;
2496}
2497
2498bool ClangASTContext::SetDefaultAccessForRecordFields(
2499 clang::RecordDecl *record_decl, int default_accessibility,
2500 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2501 if (record_decl) {
2502 uint32_t field_idx;
2503 clang::RecordDecl::field_iterator field, field_end;
2504 for (field = record_decl->field_begin(),
2505 field_end = record_decl->field_end(), field_idx = 0;
2506 field != field_end; ++field, ++field_idx) {
2507 // If no accessibility was assigned, assign the correct one
2508 if (field_idx < num_assigned_accessibilities &&
2509 assigned_accessibilities[field_idx] == clang::AS_none)
2510 field->setAccess((clang::AccessSpecifier)default_accessibility);
2511 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002512 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002513 }
2514 return false;
2515}
2516
2517clang::DeclContext *
2518ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2519 return GetDeclContextForType(ClangUtil::GetQualType(type));
2520}
2521
2522clang::DeclContext *
2523ClangASTContext::GetDeclContextForType(clang::QualType type) {
2524 if (type.isNull())
2525 return nullptr;
2526
2527 clang::QualType qual_type = type.getCanonicalType();
2528 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2529 switch (type_class) {
2530 case clang::Type::ObjCInterface:
2531 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2532 ->getInterface();
2533 case clang::Type::ObjCObjectPointer:
2534 return GetDeclContextForType(
2535 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2536 ->getPointeeType());
2537 case clang::Type::Record:
2538 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2539 case clang::Type::Enum:
2540 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2541 case clang::Type::Typedef:
2542 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2543 ->getDecl()
2544 ->getUnderlyingType());
2545 case clang::Type::Auto:
2546 return GetDeclContextForType(
2547 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2548 case clang::Type::Elaborated:
2549 return GetDeclContextForType(
2550 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2551 case clang::Type::Paren:
2552 return GetDeclContextForType(
2553 llvm::cast<clang::ParenType>(qual_type)->desugar());
2554 default:
2555 break;
2556 }
2557 // No DeclContext in this type...
2558 return nullptr;
2559}
2560
2561static bool GetCompleteQualType(clang::ASTContext *ast,
2562 clang::QualType qual_type,
2563 bool allow_completion = true) {
2564 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2565 switch (type_class) {
2566 case clang::Type::ConstantArray:
2567 case clang::Type::IncompleteArray:
2568 case clang::Type::VariableArray: {
2569 const clang::ArrayType *array_type =
2570 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2571
2572 if (array_type)
2573 return GetCompleteQualType(ast, array_type->getElementType(),
2574 allow_completion);
2575 } break;
2576 case clang::Type::Record: {
2577 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2578 if (cxx_record_decl) {
2579 if (cxx_record_decl->hasExternalLexicalStorage()) {
2580 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2581 const bool fields_loaded =
2582 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2583 if (is_complete && fields_loaded)
2584 return true;
2585
2586 if (!allow_completion)
2587 return false;
2588
2589 // Call the field_begin() accessor to for it to use the external source
2590 // to load the fields...
2591 clang::ExternalASTSource *external_ast_source =
2592 ast->getExternalSource();
2593 if (external_ast_source) {
2594 external_ast_source->CompleteType(cxx_record_decl);
2595 if (cxx_record_decl->isCompleteDefinition()) {
2596 cxx_record_decl->field_begin();
2597 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2598 }
2599 }
2600 }
2601 }
2602 const clang::TagType *tag_type =
2603 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2604 return !tag_type->isIncompleteType();
2605 } break;
2606
2607 case clang::Type::Enum: {
2608 const clang::TagType *tag_type =
2609 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2610 if (tag_type) {
2611 clang::TagDecl *tag_decl = tag_type->getDecl();
2612 if (tag_decl) {
2613 if (tag_decl->getDefinition())
2614 return true;
2615
2616 if (!allow_completion)
2617 return false;
2618
2619 if (tag_decl->hasExternalLexicalStorage()) {
2620 if (ast) {
2621 clang::ExternalASTSource *external_ast_source =
2622 ast->getExternalSource();
2623 if (external_ast_source) {
2624 external_ast_source->CompleteType(tag_decl);
2625 return !tag_type->isIncompleteType();
2626 }
2627 }
2628 }
2629 return false;
2630 }
2631 }
2632
2633 } break;
2634 case clang::Type::ObjCObject:
2635 case clang::Type::ObjCInterface: {
2636 const clang::ObjCObjectType *objc_class_type =
2637 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2638 if (objc_class_type) {
2639 clang::ObjCInterfaceDecl *class_interface_decl =
2640 objc_class_type->getInterface();
2641 // We currently can't complete objective C types through the newly added
Adrian Prantl05097242018-04-30 16:49:04 +00002642 // ASTContext because it only supports TagDecl objects right now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002643 if (class_interface_decl) {
2644 if (class_interface_decl->getDefinition())
2645 return true;
2646
2647 if (!allow_completion)
2648 return false;
2649
2650 if (class_interface_decl->hasExternalLexicalStorage()) {
2651 if (ast) {
2652 clang::ExternalASTSource *external_ast_source =
2653 ast->getExternalSource();
2654 if (external_ast_source) {
2655 external_ast_source->CompleteType(class_interface_decl);
2656 return !objc_class_type->isIncompleteType();
2657 }
2658 }
2659 }
2660 return false;
2661 }
2662 }
2663 } break;
2664
2665 case clang::Type::Typedef:
2666 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2667 ->getDecl()
2668 ->getUnderlyingType(),
2669 allow_completion);
2670
2671 case clang::Type::Auto:
2672 return GetCompleteQualType(
2673 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2674 allow_completion);
2675
2676 case clang::Type::Elaborated:
2677 return GetCompleteQualType(
2678 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2679 allow_completion);
2680
2681 case clang::Type::Paren:
2682 return GetCompleteQualType(
2683 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2684 allow_completion);
2685
2686 case clang::Type::Attributed:
2687 return GetCompleteQualType(
2688 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2689 allow_completion);
2690
2691 default:
2692 break;
2693 }
2694
2695 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002696}
2697
2698static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002699ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2700 switch (access) {
2701 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002702 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002703 case eAccessPublic:
2704 return clang::ObjCIvarDecl::Public;
2705 case eAccessPrivate:
2706 return clang::ObjCIvarDecl::Private;
2707 case eAccessProtected:
2708 return clang::ObjCIvarDecl::Protected;
2709 case eAccessPackage:
2710 return clang::ObjCIvarDecl::Package;
2711 }
2712 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002713}
2714
Greg Claytond8d4a572015-08-11 21:38:15 +00002715// Tests
Greg Claytond8d4a572015-08-11 21:38:15 +00002716
Kate Stoneb9c1b512016-09-06 20:57:50 +00002717bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2718 clang::QualType qual_type(GetCanonicalQualType(type));
2719
2720 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2721 switch (type_class) {
2722 case clang::Type::IncompleteArray:
2723 case clang::Type::VariableArray:
2724 case clang::Type::ConstantArray:
2725 case clang::Type::ExtVector:
2726 case clang::Type::Vector:
2727 case clang::Type::Record:
2728 case clang::Type::ObjCObject:
2729 case clang::Type::ObjCInterface:
2730 return true;
2731 case clang::Type::Auto:
2732 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2733 ->getDeducedType()
2734 .getAsOpaquePtr());
2735 case clang::Type::Elaborated:
2736 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2737 ->getNamedType()
2738 .getAsOpaquePtr());
2739 case clang::Type::Typedef:
2740 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2741 ->getDecl()
2742 ->getUnderlyingType()
2743 .getAsOpaquePtr());
2744 case clang::Type::Paren:
2745 return IsAggregateType(
2746 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2747 default:
2748 break;
2749 }
2750 // The clang type does have a value
2751 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002752}
2753
Kate Stoneb9c1b512016-09-06 20:57:50 +00002754bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2755 clang::QualType qual_type(GetCanonicalQualType(type));
2756
2757 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2758 switch (type_class) {
2759 case clang::Type::Record: {
2760 if (const clang::RecordType *record_type =
2761 llvm::dyn_cast_or_null<clang::RecordType>(
2762 qual_type.getTypePtrOrNull())) {
2763 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2764 return record_decl->isAnonymousStructOrUnion();
2765 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002766 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002767 break;
2768 }
2769 case clang::Type::Auto:
2770 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2771 ->getDeducedType()
2772 .getAsOpaquePtr());
2773 case clang::Type::Elaborated:
2774 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2775 ->getNamedType()
2776 .getAsOpaquePtr());
2777 case clang::Type::Typedef:
2778 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2779 ->getDecl()
2780 ->getUnderlyingType()
2781 .getAsOpaquePtr());
2782 case clang::Type::Paren:
2783 return IsAnonymousType(
2784 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2785 default:
2786 break;
2787 }
2788 // The clang type does have a value
2789 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002790}
2791
Kate Stoneb9c1b512016-09-06 20:57:50 +00002792bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2793 CompilerType *element_type_ptr,
2794 uint64_t *size, bool *is_incomplete) {
2795 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002796
Kate Stoneb9c1b512016-09-06 20:57:50 +00002797 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2798 switch (type_class) {
2799 default:
2800 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002801
Kate Stoneb9c1b512016-09-06 20:57:50 +00002802 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002803 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002804 element_type_ptr->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00002805 this, llvm::cast<clang::ConstantArrayType>(qual_type)
2806 ->getElementType()
2807 .getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00002808 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002809 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2810 ->getSize()
2811 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002812 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002813 *is_incomplete = false;
2814 return true;
2815
2816 case clang::Type::IncompleteArray:
2817 if (element_type_ptr)
2818 element_type_ptr->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00002819 this, llvm::cast<clang::IncompleteArrayType>(qual_type)
2820 ->getElementType()
2821 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002822 if (size)
2823 *size = 0;
2824 if (is_incomplete)
2825 *is_incomplete = true;
2826 return true;
2827
2828 case clang::Type::VariableArray:
2829 if (element_type_ptr)
2830 element_type_ptr->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00002831 this, llvm::cast<clang::VariableArrayType>(qual_type)
2832 ->getElementType()
2833 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002834 if (size)
2835 *size = 0;
2836 if (is_incomplete)
2837 *is_incomplete = false;
2838 return true;
2839
2840 case clang::Type::DependentSizedArray:
2841 if (element_type_ptr)
2842 element_type_ptr->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00002843 this, llvm::cast<clang::DependentSizedArrayType>(qual_type)
2844 ->getElementType()
2845 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002846 if (size)
2847 *size = 0;
2848 if (is_incomplete)
2849 *is_incomplete = false;
2850 return true;
2851
2852 case clang::Type::Typedef:
2853 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2854 ->getDecl()
2855 ->getUnderlyingType()
2856 .getAsOpaquePtr(),
2857 element_type_ptr, size, is_incomplete);
2858 case clang::Type::Auto:
2859 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2860 ->getDeducedType()
2861 .getAsOpaquePtr(),
2862 element_type_ptr, size, is_incomplete);
2863 case clang::Type::Elaborated:
2864 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2865 ->getNamedType()
2866 .getAsOpaquePtr(),
2867 element_type_ptr, size, is_incomplete);
2868 case clang::Type::Paren:
2869 return IsArrayType(
2870 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2871 element_type_ptr, size, is_incomplete);
2872 }
2873 if (element_type_ptr)
2874 element_type_ptr->Clear();
2875 if (size)
2876 *size = 0;
2877 if (is_incomplete)
2878 *is_incomplete = false;
2879 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002880}
2881
Kate Stoneb9c1b512016-09-06 20:57:50 +00002882bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2883 CompilerType *element_type, uint64_t *size) {
2884 clang::QualType qual_type(GetCanonicalQualType(type));
2885
2886 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2887 switch (type_class) {
2888 case clang::Type::Vector: {
2889 const clang::VectorType *vector_type =
2890 qual_type->getAs<clang::VectorType>();
2891 if (vector_type) {
2892 if (size)
2893 *size = vector_type->getNumElements();
2894 if (element_type)
2895 *element_type =
Alex Langfordbddab072019-08-13 19:40:36 +00002896 CompilerType(this, vector_type->getElementType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002897 }
2898 return true;
2899 } break;
2900 case clang::Type::ExtVector: {
2901 const clang::ExtVectorType *ext_vector_type =
2902 qual_type->getAs<clang::ExtVectorType>();
2903 if (ext_vector_type) {
2904 if (size)
2905 *size = ext_vector_type->getNumElements();
2906 if (element_type)
2907 *element_type =
Alex Langfordbddab072019-08-13 19:40:36 +00002908 CompilerType(this, ext_vector_type->getElementType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002909 }
2910 return true;
2911 }
2912 default:
2913 break;
2914 }
2915 return false;
2916}
2917
2918bool ClangASTContext::IsRuntimeGeneratedType(
2919 lldb::opaque_compiler_type_t type) {
2920 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
2921 ->GetDeclContextForType(GetQualType(type));
2922 if (!decl_ctx)
2923 return false;
2924
2925 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2926 return false;
2927
2928 clang::ObjCInterfaceDecl *result_iface_decl =
2929 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2930
2931 ClangASTMetadata *ast_metadata =
2932 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2933 if (!ast_metadata)
2934 return false;
2935 return (ast_metadata->GetISAPtr() != 0);
2936}
2937
2938bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
2939 return GetQualType(type).getUnqualifiedType()->isCharType();
2940}
2941
2942bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
2943 const bool allow_completion = false;
2944 return GetCompleteQualType(getASTContext(), GetQualType(type),
2945 allow_completion);
2946}
2947
2948bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
2949 return GetQualType(type).isConstQualified();
2950}
2951
2952bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
2953 uint32_t &length) {
2954 CompilerType pointee_or_element_clang_type;
2955 length = 0;
2956 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2957
2958 if (!pointee_or_element_clang_type.IsValid())
2959 return false;
2960
2961 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2962 if (pointee_or_element_clang_type.IsCharType()) {
2963 if (type_flags.Test(eTypeIsArray)) {
Adrian Prantl05097242018-04-30 16:49:04 +00002964 // We know the size of the array and it could be a C string since it is
2965 // an array of characters
Kate Stoneb9c1b512016-09-06 20:57:50 +00002966 length = llvm::cast<clang::ConstantArrayType>(
2967 GetCanonicalQualType(type).getTypePtr())
2968 ->getSize()
2969 .getLimitedValue();
2970 }
2971 return true;
2972 }
2973 }
2974 return false;
2975}
2976
2977bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
2978 bool *is_variadic_ptr) {
2979 if (type) {
2980 clang::QualType qual_type(GetCanonicalQualType(type));
2981
2982 if (qual_type->isFunctionType()) {
2983 if (is_variadic_ptr) {
2984 const clang::FunctionProtoType *function_proto_type =
2985 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2986 if (function_proto_type)
2987 *is_variadic_ptr = function_proto_type->isVariadic();
2988 else
2989 *is_variadic_ptr = false;
2990 }
2991 return true;
2992 }
2993
Greg Claytond8d4a572015-08-11 21:38:15 +00002994 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002995 switch (type_class) {
2996 default:
2997 break;
2998 case clang::Type::Typedef:
2999 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
3000 ->getDecl()
3001 ->getUnderlyingType()
3002 .getAsOpaquePtr(),
3003 nullptr);
3004 case clang::Type::Auto:
3005 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3006 ->getDeducedType()
3007 .getAsOpaquePtr(),
3008 nullptr);
3009 case clang::Type::Elaborated:
3010 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3011 ->getNamedType()
3012 .getAsOpaquePtr(),
3013 nullptr);
3014 case clang::Type::Paren:
3015 return IsFunctionType(
3016 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3017 nullptr);
3018 case clang::Type::LValueReference:
3019 case clang::Type::RValueReference: {
3020 const clang::ReferenceType *reference_type =
3021 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3022 if (reference_type)
3023 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3024 nullptr);
3025 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00003026 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003027 }
3028 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003029}
3030
3031// Used to detect "Homogeneous Floating-point Aggregates"
3032uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003033ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3034 CompilerType *base_type_ptr) {
3035 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003036 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003037
3038 clang::QualType qual_type(GetCanonicalQualType(type));
3039 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3040 switch (type_class) {
3041 case clang::Type::Record:
3042 if (GetCompleteType(type)) {
3043 const clang::CXXRecordDecl *cxx_record_decl =
3044 qual_type->getAsCXXRecordDecl();
3045 if (cxx_record_decl) {
3046 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3047 return 0;
3048 }
3049 const clang::RecordType *record_type =
3050 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3051 if (record_type) {
3052 const clang::RecordDecl *record_decl = record_type->getDecl();
3053 if (record_decl) {
3054 // We are looking for a structure that contains only floating point
3055 // types
3056 clang::RecordDecl::field_iterator field_pos,
3057 field_end = record_decl->field_end();
3058 uint32_t num_fields = 0;
3059 bool is_hva = false;
3060 bool is_hfa = false;
3061 clang::QualType base_qual_type;
3062 uint64_t base_bitwidth = 0;
3063 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3064 ++field_pos) {
3065 clang::QualType field_qual_type = field_pos->getType();
3066 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3067 if (field_qual_type->isFloatingType()) {
3068 if (field_qual_type->isComplexType())
3069 return 0;
3070 else {
3071 if (num_fields == 0)
3072 base_qual_type = field_qual_type;
3073 else {
3074 if (is_hva)
3075 return 0;
3076 is_hfa = true;
3077 if (field_qual_type.getTypePtr() !=
3078 base_qual_type.getTypePtr())
3079 return 0;
3080 }
3081 }
3082 } else if (field_qual_type->isVectorType() ||
3083 field_qual_type->isExtVectorType()) {
3084 if (num_fields == 0) {
3085 base_qual_type = field_qual_type;
3086 base_bitwidth = field_bitwidth;
3087 } else {
3088 if (is_hfa)
3089 return 0;
3090 is_hva = true;
3091 if (base_bitwidth != field_bitwidth)
3092 return 0;
3093 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3094 return 0;
3095 }
3096 } else
3097 return 0;
3098 ++num_fields;
3099 }
3100 if (base_type_ptr)
Alex Langfordbddab072019-08-13 19:40:36 +00003101 *base_type_ptr = CompilerType(this, base_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003102 return num_fields;
3103 }
3104 }
3105 }
3106 break;
3107
3108 case clang::Type::Typedef:
3109 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3110 ->getDecl()
3111 ->getUnderlyingType()
3112 .getAsOpaquePtr(),
3113 base_type_ptr);
3114
3115 case clang::Type::Auto:
3116 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3117 ->getDeducedType()
3118 .getAsOpaquePtr(),
3119 base_type_ptr);
3120
3121 case clang::Type::Elaborated:
3122 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3123 ->getNamedType()
3124 .getAsOpaquePtr(),
3125 base_type_ptr);
3126 default:
3127 break;
3128 }
3129 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003130}
3131
Kate Stoneb9c1b512016-09-06 20:57:50 +00003132size_t ClangASTContext::GetNumberOfFunctionArguments(
3133 lldb::opaque_compiler_type_t type) {
3134 if (type) {
3135 clang::QualType qual_type(GetCanonicalQualType(type));
3136 const clang::FunctionProtoType *func =
3137 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3138 if (func)
3139 return func->getNumParams();
3140 }
3141 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003142}
3143
Greg Claytona1e5dc82015-08-11 22:53:00 +00003144CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003145ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3146 const size_t index) {
3147 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003148 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003149 const clang::FunctionProtoType *func =
3150 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3151 if (func) {
3152 if (index < func->getNumParams())
Alex Langfordbddab072019-08-13 19:40:36 +00003153 return CompilerType(this, func->getParamType(index).getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003154 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003155 }
3156 return CompilerType();
3157}
3158
3159bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3160 if (type) {
3161 clang::QualType qual_type(GetCanonicalQualType(type));
3162
3163 if (qual_type->isFunctionPointerType())
3164 return true;
3165
3166 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3167 switch (type_class) {
3168 default:
3169 break;
3170 case clang::Type::Typedef:
3171 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3172 ->getDecl()
3173 ->getUnderlyingType()
3174 .getAsOpaquePtr());
3175 case clang::Type::Auto:
3176 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3177 ->getDeducedType()
3178 .getAsOpaquePtr());
3179 case clang::Type::Elaborated:
3180 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3181 ->getNamedType()
3182 .getAsOpaquePtr());
3183 case clang::Type::Paren:
3184 return IsFunctionPointerType(
3185 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3186
3187 case clang::Type::LValueReference:
3188 case clang::Type::RValueReference: {
3189 const clang::ReferenceType *reference_type =
3190 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3191 if (reference_type)
3192 return IsFunctionPointerType(
3193 reference_type->getPointeeType().getAsOpaquePtr());
3194 } break;
3195 }
3196 }
3197 return false;
3198}
3199
3200bool ClangASTContext::IsBlockPointerType(
3201 lldb::opaque_compiler_type_t type,
3202 CompilerType *function_pointer_type_ptr) {
3203 if (type) {
3204 clang::QualType qual_type(GetCanonicalQualType(type));
3205
3206 if (qual_type->isBlockPointerType()) {
3207 if (function_pointer_type_ptr) {
3208 const clang::BlockPointerType *block_pointer_type =
3209 qual_type->getAs<clang::BlockPointerType>();
3210 QualType pointee_type = block_pointer_type->getPointeeType();
Jonas Devlieghered5b44032019-02-13 06:25:41 +00003211 QualType function_pointer_type = m_ast_up->getPointerType(pointee_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003212 *function_pointer_type_ptr =
Alex Langfordbddab072019-08-13 19:40:36 +00003213 CompilerType(this, function_pointer_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003214 }
3215 return true;
3216 }
3217
3218 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3219 switch (type_class) {
3220 default:
3221 break;
3222 case clang::Type::Typedef:
3223 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3224 ->getDecl()
3225 ->getUnderlyingType()
3226 .getAsOpaquePtr(),
3227 function_pointer_type_ptr);
3228 case clang::Type::Auto:
3229 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3230 ->getDeducedType()
3231 .getAsOpaquePtr(),
3232 function_pointer_type_ptr);
3233 case clang::Type::Elaborated:
3234 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3235 ->getNamedType()
3236 .getAsOpaquePtr(),
3237 function_pointer_type_ptr);
3238 case clang::Type::Paren:
3239 return IsBlockPointerType(
3240 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3241 function_pointer_type_ptr);
3242
3243 case clang::Type::LValueReference:
3244 case clang::Type::RValueReference: {
3245 const clang::ReferenceType *reference_type =
3246 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3247 if (reference_type)
3248 return IsBlockPointerType(
3249 reference_type->getPointeeType().getAsOpaquePtr(),
3250 function_pointer_type_ptr);
3251 } break;
3252 }
3253 }
3254 return false;
3255}
3256
3257bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3258 bool &is_signed) {
3259 if (!type)
3260 return false;
3261
3262 clang::QualType qual_type(GetCanonicalQualType(type));
3263 const clang::BuiltinType *builtin_type =
3264 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3265
3266 if (builtin_type) {
3267 if (builtin_type->isInteger()) {
3268 is_signed = builtin_type->isSignedInteger();
3269 return true;
3270 }
3271 }
3272
3273 return false;
3274}
3275
3276bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3277 bool &is_signed) {
3278 if (type) {
3279 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3280 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3281
3282 if (enum_type) {
3283 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3284 is_signed);
3285 return true;
3286 }
3287 }
3288
3289 return false;
3290}
3291
3292bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3293 CompilerType *pointee_type) {
3294 if (type) {
3295 clang::QualType qual_type(GetCanonicalQualType(type));
3296 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3297 switch (type_class) {
3298 case clang::Type::Builtin:
3299 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3300 default:
3301 break;
3302 case clang::BuiltinType::ObjCId:
3303 case clang::BuiltinType::ObjCClass:
3304 return true;
3305 }
3306 return false;
3307 case clang::Type::ObjCObjectPointer:
3308 if (pointee_type)
3309 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003310 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3311 ->getPointeeType()
3312 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003313 return true;
3314 case clang::Type::BlockPointer:
3315 if (pointee_type)
3316 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003317 this, llvm::cast<clang::BlockPointerType>(qual_type)
3318 ->getPointeeType()
3319 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003320 return true;
3321 case clang::Type::Pointer:
3322 if (pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003323 pointee_type->SetCompilerType(this,
3324 llvm::cast<clang::PointerType>(qual_type)
3325 ->getPointeeType()
3326 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003327 return true;
3328 case clang::Type::MemberPointer:
3329 if (pointee_type)
3330 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003331 this, llvm::cast<clang::MemberPointerType>(qual_type)
3332 ->getPointeeType()
3333 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003334 return true;
3335 case clang::Type::Typedef:
3336 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3337 ->getDecl()
3338 ->getUnderlyingType()
3339 .getAsOpaquePtr(),
3340 pointee_type);
3341 case clang::Type::Auto:
3342 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3343 ->getDeducedType()
3344 .getAsOpaquePtr(),
3345 pointee_type);
3346 case clang::Type::Elaborated:
3347 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3348 ->getNamedType()
3349 .getAsOpaquePtr(),
3350 pointee_type);
3351 case clang::Type::Paren:
3352 return IsPointerType(
3353 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3354 pointee_type);
3355 default:
3356 break;
3357 }
3358 }
3359 if (pointee_type)
3360 pointee_type->Clear();
3361 return false;
3362}
3363
3364bool ClangASTContext::IsPointerOrReferenceType(
3365 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3366 if (type) {
3367 clang::QualType qual_type(GetCanonicalQualType(type));
3368 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3369 switch (type_class) {
3370 case clang::Type::Builtin:
3371 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3372 default:
3373 break;
3374 case clang::BuiltinType::ObjCId:
3375 case clang::BuiltinType::ObjCClass:
3376 return true;
3377 }
3378 return false;
3379 case clang::Type::ObjCObjectPointer:
3380 if (pointee_type)
3381 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003382 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3383 ->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003384 return true;
3385 case clang::Type::BlockPointer:
3386 if (pointee_type)
3387 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003388 this, llvm::cast<clang::BlockPointerType>(qual_type)
3389 ->getPointeeType()
3390 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003391 return true;
3392 case clang::Type::Pointer:
3393 if (pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003394 pointee_type->SetCompilerType(this,
3395 llvm::cast<clang::PointerType>(qual_type)
3396 ->getPointeeType()
3397 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003398 return true;
3399 case clang::Type::MemberPointer:
3400 if (pointee_type)
3401 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003402 this, llvm::cast<clang::MemberPointerType>(qual_type)
3403 ->getPointeeType()
3404 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003405 return true;
3406 case clang::Type::LValueReference:
3407 if (pointee_type)
3408 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003409 this, llvm::cast<clang::LValueReferenceType>(qual_type)
3410 ->desugar()
3411 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003412 return true;
3413 case clang::Type::RValueReference:
3414 if (pointee_type)
3415 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003416 this, llvm::cast<clang::RValueReferenceType>(qual_type)
3417 ->desugar()
3418 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003419 return true;
3420 case clang::Type::Typedef:
3421 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3422 ->getDecl()
3423 ->getUnderlyingType()
3424 .getAsOpaquePtr(),
3425 pointee_type);
3426 case clang::Type::Auto:
3427 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3428 ->getDeducedType()
3429 .getAsOpaquePtr(),
3430 pointee_type);
3431 case clang::Type::Elaborated:
3432 return IsPointerOrReferenceType(
3433 llvm::cast<clang::ElaboratedType>(qual_type)
3434 ->getNamedType()
3435 .getAsOpaquePtr(),
3436 pointee_type);
3437 case clang::Type::Paren:
3438 return IsPointerOrReferenceType(
3439 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3440 pointee_type);
3441 default:
3442 break;
3443 }
3444 }
3445 if (pointee_type)
3446 pointee_type->Clear();
3447 return false;
3448}
3449
3450bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3451 CompilerType *pointee_type,
3452 bool *is_rvalue) {
3453 if (type) {
3454 clang::QualType qual_type(GetCanonicalQualType(type));
3455 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3456
3457 switch (type_class) {
3458 case clang::Type::LValueReference:
3459 if (pointee_type)
3460 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003461 this, llvm::cast<clang::LValueReferenceType>(qual_type)
3462 ->desugar()
3463 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003464 if (is_rvalue)
3465 *is_rvalue = false;
3466 return true;
3467 case clang::Type::RValueReference:
3468 if (pointee_type)
3469 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003470 this, llvm::cast<clang::RValueReferenceType>(qual_type)
3471 ->desugar()
3472 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003473 if (is_rvalue)
3474 *is_rvalue = true;
3475 return true;
3476 case clang::Type::Typedef:
3477 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3478 ->getDecl()
3479 ->getUnderlyingType()
3480 .getAsOpaquePtr(),
3481 pointee_type, is_rvalue);
3482 case clang::Type::Auto:
3483 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3484 ->getDeducedType()
3485 .getAsOpaquePtr(),
3486 pointee_type, is_rvalue);
3487 case clang::Type::Elaborated:
3488 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3489 ->getNamedType()
3490 .getAsOpaquePtr(),
3491 pointee_type, is_rvalue);
3492 case clang::Type::Paren:
3493 return IsReferenceType(
3494 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3495 pointee_type, is_rvalue);
3496
3497 default:
3498 break;
3499 }
3500 }
3501 if (pointee_type)
3502 pointee_type->Clear();
3503 return false;
3504}
3505
3506bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3507 uint32_t &count, bool &is_complex) {
3508 if (type) {
3509 clang::QualType qual_type(GetCanonicalQualType(type));
3510
3511 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3512 qual_type->getCanonicalTypeInternal())) {
3513 clang::BuiltinType::Kind kind = BT->getKind();
3514 if (kind >= clang::BuiltinType::Float &&
3515 kind <= clang::BuiltinType::LongDouble) {
3516 count = 1;
3517 is_complex = false;
3518 return true;
3519 }
3520 } else if (const clang::ComplexType *CT =
3521 llvm::dyn_cast<clang::ComplexType>(
3522 qual_type->getCanonicalTypeInternal())) {
3523 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3524 is_complex)) {
3525 count = 2;
3526 is_complex = true;
3527 return true;
3528 }
3529 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3530 qual_type->getCanonicalTypeInternal())) {
3531 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3532 is_complex)) {
3533 count = VT->getNumElements();
3534 is_complex = false;
3535 return true;
3536 }
3537 }
3538 }
3539 count = 0;
3540 is_complex = false;
3541 return false;
3542}
3543
3544bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3545 if (!type)
3546 return false;
3547
3548 clang::QualType qual_type(GetQualType(type));
3549 const clang::TagType *tag_type =
3550 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3551 if (tag_type) {
3552 clang::TagDecl *tag_decl = tag_type->getDecl();
3553 if (tag_decl)
3554 return tag_decl->isCompleteDefinition();
3555 return false;
3556 } else {
3557 const clang::ObjCObjectType *objc_class_type =
3558 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3559 if (objc_class_type) {
3560 clang::ObjCInterfaceDecl *class_interface_decl =
3561 objc_class_type->getInterface();
3562 if (class_interface_decl)
3563 return class_interface_decl->getDefinition() != nullptr;
3564 return false;
3565 }
3566 }
3567 return true;
3568}
3569
3570bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
Raphael Isemann79b3cce2019-11-08 12:03:28 +01003571 if (ClangUtil::IsClangType(type)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003572 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3573
3574 const clang::ObjCObjectPointerType *obj_pointer_type =
3575 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3576
3577 if (obj_pointer_type)
3578 return obj_pointer_type->isObjCClassType();
3579 }
3580 return false;
3581}
3582
3583bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3584 if (ClangUtil::IsClangType(type))
3585 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3586 return false;
3587}
3588
3589bool ClangASTContext::IsClassType(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::Record);
3595}
3596
3597bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3598 if (!type)
3599 return false;
3600 clang::QualType qual_type(GetCanonicalQualType(type));
3601 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3602 return (type_class == clang::Type::Enum);
3603}
3604
3605bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3606 if (type) {
3607 clang::QualType qual_type(GetCanonicalQualType(type));
3608 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3609 switch (type_class) {
3610 case clang::Type::Record:
3611 if (GetCompleteType(type)) {
3612 const clang::RecordType *record_type =
3613 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3614 const clang::RecordDecl *record_decl = record_type->getDecl();
3615 if (record_decl) {
3616 const clang::CXXRecordDecl *cxx_record_decl =
3617 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3618 if (cxx_record_decl)
3619 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003620 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003621 }
3622 break;
3623
3624 default:
3625 break;
3626 }
3627 }
3628 return false;
3629}
3630
3631bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3632 CompilerType *dynamic_pointee_type,
3633 bool check_cplusplus,
3634 bool check_objc) {
3635 clang::QualType pointee_qual_type;
3636 if (type) {
3637 clang::QualType qual_type(GetCanonicalQualType(type));
3638 bool success = false;
3639 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3640 switch (type_class) {
3641 case clang::Type::Builtin:
3642 if (check_objc &&
3643 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3644 clang::BuiltinType::ObjCId) {
3645 if (dynamic_pointee_type)
3646 dynamic_pointee_type->SetCompilerType(this, type);
3647 return true;
3648 }
3649 break;
3650
3651 case clang::Type::ObjCObjectPointer:
3652 if (check_objc) {
3653 if (auto objc_pointee_type =
3654 qual_type->getPointeeType().getTypePtrOrNull()) {
3655 if (auto objc_object_type =
3656 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3657 objc_pointee_type)) {
3658 if (objc_object_type->isObjCClass())
3659 return false;
3660 }
3661 }
3662 if (dynamic_pointee_type)
3663 dynamic_pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003664 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3665 ->getPointeeType()
3666 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003667 return true;
3668 }
3669 break;
3670
3671 case clang::Type::Pointer:
3672 pointee_qual_type =
3673 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3674 success = true;
3675 break;
3676
3677 case clang::Type::LValueReference:
3678 case clang::Type::RValueReference:
3679 pointee_qual_type =
3680 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3681 success = true;
3682 break;
3683
3684 case clang::Type::Typedef:
3685 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3686 ->getDecl()
3687 ->getUnderlyingType()
3688 .getAsOpaquePtr(),
3689 dynamic_pointee_type, check_cplusplus,
3690 check_objc);
3691
3692 case clang::Type::Auto:
3693 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3694 ->getDeducedType()
3695 .getAsOpaquePtr(),
3696 dynamic_pointee_type, check_cplusplus,
3697 check_objc);
3698
3699 case clang::Type::Elaborated:
3700 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3701 ->getNamedType()
3702 .getAsOpaquePtr(),
3703 dynamic_pointee_type, check_cplusplus,
3704 check_objc);
3705
3706 case clang::Type::Paren:
3707 return IsPossibleDynamicType(
3708 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3709 dynamic_pointee_type, check_cplusplus, check_objc);
3710 default:
3711 break;
3712 }
3713
3714 if (success) {
3715 // Check to make sure what we are pointing too is a possible dynamic C++
Adrian Prantl05097242018-04-30 16:49:04 +00003716 // type We currently accept any "void *" (in case we have a class that
3717 // has been watered down to an opaque pointer) and virtual C++ classes.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003718 const clang::Type::TypeClass pointee_type_class =
3719 pointee_qual_type.getCanonicalType()->getTypeClass();
3720 switch (pointee_type_class) {
3721 case clang::Type::Builtin:
3722 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3723 case clang::BuiltinType::UnknownAny:
3724 case clang::BuiltinType::Void:
3725 if (dynamic_pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003726 dynamic_pointee_type->SetCompilerType(
3727 this, pointee_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003728 return true;
3729 default:
3730 break;
3731 }
3732 break;
3733
3734 case clang::Type::Record:
3735 if (check_cplusplus) {
3736 clang::CXXRecordDecl *cxx_record_decl =
3737 pointee_qual_type->getAsCXXRecordDecl();
3738 if (cxx_record_decl) {
3739 bool is_complete = cxx_record_decl->isCompleteDefinition();
3740
3741 if (is_complete)
3742 success = cxx_record_decl->isDynamicClass();
3743 else {
3744 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3745 getASTContext(), cxx_record_decl);
3746 if (metadata)
3747 success = metadata->GetIsDynamicCXXType();
3748 else {
Alex Langfordbddab072019-08-13 19:40:36 +00003749 is_complete =
3750 CompilerType(this, pointee_qual_type.getAsOpaquePtr())
3751 .GetCompleteType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003752 if (is_complete)
3753 success = cxx_record_decl->isDynamicClass();
3754 else
3755 success = false;
3756 }
3757 }
3758
3759 if (success) {
3760 if (dynamic_pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003761 dynamic_pointee_type->SetCompilerType(
3762 this, pointee_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003763 return true;
3764 }
3765 }
3766 }
3767 break;
3768
3769 case clang::Type::ObjCObject:
3770 case clang::Type::ObjCInterface:
3771 if (check_objc) {
3772 if (dynamic_pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003773 dynamic_pointee_type->SetCompilerType(
3774 this, pointee_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003775 return true;
3776 }
3777 break;
3778
3779 default:
3780 break;
3781 }
3782 }
3783 }
3784 if (dynamic_pointee_type)
3785 dynamic_pointee_type->Clear();
3786 return false;
3787}
3788
3789bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3790 if (!type)
3791 return false;
3792
3793 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3794}
3795
3796bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3797 if (!type)
3798 return false;
3799 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3800}
3801
3802bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3803 if (!type)
3804 return false;
3805 return GetCanonicalQualType(type)->isVoidType();
3806}
3807
Alex Langforda03e2b22019-06-04 19:29:59 +00003808bool ClangASTContext::CanPassInRegisters(const CompilerType &type) {
3809 if (auto *record_decl =
3810 ClangASTContext::GetAsRecordDecl(type)) {
3811 return record_decl->canPassInRegisters();
3812 }
3813 return false;
3814}
3815
Kate Stoneb9c1b512016-09-06 20:57:50 +00003816bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3817 return ClangASTContextSupportsLanguage(language);
3818}
3819
Alex Langforddb542452019-10-30 12:50:05 -07003820Optional<std::string>
3821ClangASTContext::GetCXXClassName(const CompilerType &type) {
3822 if (!type)
3823 return llvm::None;
3824
3825 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3826 if (qual_type.isNull())
3827 return llvm::None;
3828
3829 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3830 if (!cxx_record_decl)
3831 return llvm::None;
3832
3833 return std::string(cxx_record_decl->getIdentifier()->getNameStart());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003834}
3835
3836bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3837 if (!type)
3838 return false;
3839
3840 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Jonas Devliegherea6682a42018-12-15 00:15:33 +00003841 return !qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003842}
3843
3844bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3845 if (!type)
3846 return false;
3847 clang::QualType qual_type(GetCanonicalQualType(type));
3848 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3849 if (tag_type)
3850 return tag_type->isBeingDefined();
3851 return false;
3852}
3853
3854bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3855 CompilerType *class_type_ptr) {
Raphael Isemann79b3cce2019-11-08 12:03:28 +01003856 if (!ClangUtil::IsClangType(type))
Kate Stoneb9c1b512016-09-06 20:57:50 +00003857 return false;
3858
3859 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3860
3861 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3862 if (class_type_ptr) {
3863 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3864 const clang::ObjCObjectPointerType *obj_pointer_type =
3865 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3866 if (obj_pointer_type == nullptr)
3867 class_type_ptr->Clear();
3868 else
3869 class_type_ptr->SetCompilerType(
3870 type.GetTypeSystem(),
3871 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3872 .getAsOpaquePtr());
3873 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003874 }
3875 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003876 }
3877 if (class_type_ptr)
3878 class_type_ptr->Clear();
3879 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003880}
3881
Greg Claytond8d4a572015-08-11 21:38:15 +00003882// Type Completion
Greg Claytond8d4a572015-08-11 21:38:15 +00003883
Kate Stoneb9c1b512016-09-06 20:57:50 +00003884bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3885 if (!type)
3886 return false;
3887 const bool allow_completion = true;
3888 return GetCompleteQualType(getASTContext(), GetQualType(type),
3889 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00003890}
3891
Kate Stoneb9c1b512016-09-06 20:57:50 +00003892ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
3893 std::string type_name;
3894 if (type) {
3895 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
3896 clang::QualType qual_type(GetQualType(type));
3897 printing_policy.SuppressTagKeyword = true;
3898 const clang::TypedefType *typedef_type =
3899 qual_type->getAs<clang::TypedefType>();
3900 if (typedef_type) {
3901 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3902 type_name = typedef_decl->getQualifiedNameAsString();
3903 } else {
3904 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003905 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003906 }
3907 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00003908}
3909
3910uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003911ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
3912 CompilerType *pointee_or_element_clang_type) {
3913 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003914 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003915
3916 if (pointee_or_element_clang_type)
3917 pointee_or_element_clang_type->Clear();
3918
3919 clang::QualType qual_type(GetQualType(type));
3920
3921 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3922 switch (type_class) {
Sean Callananddf802a2017-06-02 01:24:18 +00003923 case clang::Type::Attributed:
3924 return GetTypeInfo(
3925 qual_type->getAs<clang::AttributedType>()
3926 ->getModifiedType().getAsOpaquePtr(),
3927 pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003928 case clang::Type::Builtin: {
3929 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3930 qual_type->getCanonicalTypeInternal());
3931
3932 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3933 switch (builtin_type->getKind()) {
3934 case clang::BuiltinType::ObjCId:
3935 case clang::BuiltinType::ObjCClass:
3936 if (pointee_or_element_clang_type)
3937 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003938 this, getASTContext()->ObjCBuiltinClassTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003939 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3940 break;
3941
3942 case clang::BuiltinType::ObjCSel:
3943 if (pointee_or_element_clang_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003944 pointee_or_element_clang_type->SetCompilerType(
3945 this, getASTContext()->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003946 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3947 break;
3948
3949 case clang::BuiltinType::Bool:
3950 case clang::BuiltinType::Char_U:
3951 case clang::BuiltinType::UChar:
3952 case clang::BuiltinType::WChar_U:
3953 case clang::BuiltinType::Char16:
3954 case clang::BuiltinType::Char32:
3955 case clang::BuiltinType::UShort:
3956 case clang::BuiltinType::UInt:
3957 case clang::BuiltinType::ULong:
3958 case clang::BuiltinType::ULongLong:
3959 case clang::BuiltinType::UInt128:
3960 case clang::BuiltinType::Char_S:
3961 case clang::BuiltinType::SChar:
3962 case clang::BuiltinType::WChar_S:
3963 case clang::BuiltinType::Short:
3964 case clang::BuiltinType::Int:
3965 case clang::BuiltinType::Long:
3966 case clang::BuiltinType::LongLong:
3967 case clang::BuiltinType::Int128:
3968 case clang::BuiltinType::Float:
3969 case clang::BuiltinType::Double:
3970 case clang::BuiltinType::LongDouble:
3971 builtin_type_flags |= eTypeIsScalar;
3972 if (builtin_type->isInteger()) {
3973 builtin_type_flags |= eTypeIsInteger;
3974 if (builtin_type->isSignedInteger())
3975 builtin_type_flags |= eTypeIsSigned;
3976 } else if (builtin_type->isFloatingPoint())
3977 builtin_type_flags |= eTypeIsFloat;
3978 break;
3979 default:
3980 break;
3981 }
3982 return builtin_type_flags;
3983 }
3984
3985 case clang::Type::BlockPointer:
3986 if (pointee_or_element_clang_type)
3987 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003988 this, qual_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003989 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3990
3991 case clang::Type::Complex: {
3992 uint32_t complex_type_flags =
3993 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3994 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
3995 qual_type->getCanonicalTypeInternal());
3996 if (complex_type) {
3997 clang::QualType complex_element_type(complex_type->getElementType());
3998 if (complex_element_type->isIntegerType())
3999 complex_type_flags |= eTypeIsFloat;
4000 else if (complex_element_type->isFloatingType())
4001 complex_type_flags |= eTypeIsInteger;
4002 }
4003 return complex_type_flags;
4004 } break;
4005
4006 case clang::Type::ConstantArray:
4007 case clang::Type::DependentSizedArray:
4008 case clang::Type::IncompleteArray:
4009 case clang::Type::VariableArray:
4010 if (pointee_or_element_clang_type)
4011 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004012 this, llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4013 ->getElementType()
4014 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004015 return eTypeHasChildren | eTypeIsArray;
4016
4017 case clang::Type::DependentName:
4018 return 0;
4019 case clang::Type::DependentSizedExtVector:
4020 return eTypeHasChildren | eTypeIsVector;
4021 case clang::Type::DependentTemplateSpecialization:
4022 return eTypeIsTemplate;
4023 case clang::Type::Decltype:
Alex Langfordbddab072019-08-13 19:40:36 +00004024 return CompilerType(this, llvm::cast<clang::DecltypeType>(qual_type)
4025 ->getUnderlyingType()
4026 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004027 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004028
4029 case clang::Type::Enum:
4030 if (pointee_or_element_clang_type)
4031 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004032 this, llvm::cast<clang::EnumType>(qual_type)
4033 ->getDecl()
4034 ->getIntegerType()
4035 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004036 return eTypeIsEnumeration | eTypeHasValue;
4037
4038 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00004039 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
4040 ->getDeducedType()
4041 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004042 .GetTypeInfo(pointee_or_element_clang_type);
4043 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00004044 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
4045 ->getNamedType()
4046 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004047 .GetTypeInfo(pointee_or_element_clang_type);
4048 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00004049 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
4050 ->desugar()
4051 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004052 .GetTypeInfo(pointee_or_element_clang_type);
4053
4054 case clang::Type::FunctionProto:
4055 return eTypeIsFuncPrototype | eTypeHasValue;
4056 case clang::Type::FunctionNoProto:
4057 return eTypeIsFuncPrototype | eTypeHasValue;
4058 case clang::Type::InjectedClassName:
4059 return 0;
4060
4061 case clang::Type::LValueReference:
4062 case clang::Type::RValueReference:
4063 if (pointee_or_element_clang_type)
4064 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004065 this, llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4066 ->getPointeeType()
4067 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004068 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4069
4070 case clang::Type::MemberPointer:
4071 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4072
4073 case clang::Type::ObjCObjectPointer:
4074 if (pointee_or_element_clang_type)
4075 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004076 this, qual_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004077 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4078 eTypeHasValue;
4079
4080 case clang::Type::ObjCObject:
4081 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4082 case clang::Type::ObjCInterface:
4083 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4084
4085 case clang::Type::Pointer:
4086 if (pointee_or_element_clang_type)
4087 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004088 this, qual_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004089 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4090
4091 case clang::Type::Record:
4092 if (qual_type->getAsCXXRecordDecl())
4093 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4094 else
4095 return eTypeHasChildren | eTypeIsStructUnion;
4096 break;
4097 case clang::Type::SubstTemplateTypeParm:
4098 return eTypeIsTemplate;
4099 case clang::Type::TemplateTypeParm:
4100 return eTypeIsTemplate;
4101 case clang::Type::TemplateSpecialization:
4102 return eTypeIsTemplate;
4103
4104 case clang::Type::Typedef:
4105 return eTypeIsTypedef |
Alex Langfordbddab072019-08-13 19:40:36 +00004106 CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
4107 ->getDecl()
4108 ->getUnderlyingType()
4109 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004110 .GetTypeInfo(pointee_or_element_clang_type);
4111 case clang::Type::TypeOfExpr:
Alex Langfordbddab072019-08-13 19:40:36 +00004112 return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type)
4113 ->getUnderlyingExpr()
4114 ->getType()
4115 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004116 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004117 case clang::Type::TypeOf:
Alex Langfordbddab072019-08-13 19:40:36 +00004118 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
4119 ->getUnderlyingType()
4120 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004121 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004122 case clang::Type::UnresolvedUsing:
4123 return 0;
4124
4125 case clang::Type::ExtVector:
4126 case clang::Type::Vector: {
4127 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4128 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4129 qual_type->getCanonicalTypeInternal());
4130 if (vector_type) {
4131 if (vector_type->isIntegerType())
4132 vector_type_flags |= eTypeIsFloat;
4133 else if (vector_type->isFloatingType())
4134 vector_type_flags |= eTypeIsInteger;
4135 }
4136 return vector_type_flags;
4137 }
4138 default:
4139 return 0;
4140 }
4141 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004142}
4143
Greg Claytond8d4a572015-08-11 21:38:15 +00004144lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004145ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4146 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004147 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004148
4149 // If the type is a reference, then resolve it to what it refers to first:
4150 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4151 if (qual_type->isAnyPointerType()) {
4152 if (qual_type->isObjCObjectPointerType())
4153 return lldb::eLanguageTypeObjC;
Adrian Prantl1db0f0c2019-05-02 23:07:23 +00004154 if (qual_type->getPointeeCXXRecordDecl())
4155 return lldb::eLanguageTypeC_plus_plus;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004156
4157 clang::QualType pointee_type(qual_type->getPointeeType());
Adrian Prantl1db0f0c2019-05-02 23:07:23 +00004158 if (pointee_type->getPointeeCXXRecordDecl())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004159 return lldb::eLanguageTypeC_plus_plus;
4160 if (pointee_type->isObjCObjectOrInterfaceType())
4161 return lldb::eLanguageTypeObjC;
4162 if (pointee_type->isObjCClassType())
4163 return lldb::eLanguageTypeObjC;
4164 if (pointee_type.getTypePtr() ==
4165 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4166 return lldb::eLanguageTypeObjC;
4167 } else {
4168 if (qual_type->isObjCObjectOrInterfaceType())
4169 return lldb::eLanguageTypeObjC;
4170 if (qual_type->getAsCXXRecordDecl())
4171 return lldb::eLanguageTypeC_plus_plus;
4172 switch (qual_type->getTypeClass()) {
4173 default:
4174 break;
4175 case clang::Type::Builtin:
4176 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4177 default:
4178 case clang::BuiltinType::Void:
4179 case clang::BuiltinType::Bool:
4180 case clang::BuiltinType::Char_U:
4181 case clang::BuiltinType::UChar:
4182 case clang::BuiltinType::WChar_U:
4183 case clang::BuiltinType::Char16:
4184 case clang::BuiltinType::Char32:
4185 case clang::BuiltinType::UShort:
4186 case clang::BuiltinType::UInt:
4187 case clang::BuiltinType::ULong:
4188 case clang::BuiltinType::ULongLong:
4189 case clang::BuiltinType::UInt128:
4190 case clang::BuiltinType::Char_S:
4191 case clang::BuiltinType::SChar:
4192 case clang::BuiltinType::WChar_S:
4193 case clang::BuiltinType::Short:
4194 case clang::BuiltinType::Int:
4195 case clang::BuiltinType::Long:
4196 case clang::BuiltinType::LongLong:
4197 case clang::BuiltinType::Int128:
4198 case clang::BuiltinType::Float:
4199 case clang::BuiltinType::Double:
4200 case clang::BuiltinType::LongDouble:
4201 break;
4202
4203 case clang::BuiltinType::NullPtr:
4204 return eLanguageTypeC_plus_plus;
4205
4206 case clang::BuiltinType::ObjCId:
4207 case clang::BuiltinType::ObjCClass:
4208 case clang::BuiltinType::ObjCSel:
4209 return eLanguageTypeObjC;
4210
4211 case clang::BuiltinType::Dependent:
4212 case clang::BuiltinType::Overload:
4213 case clang::BuiltinType::BoundMember:
4214 case clang::BuiltinType::UnknownAny:
4215 break;
4216 }
4217 break;
4218 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00004219 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
4220 ->getDecl()
4221 ->getUnderlyingType()
4222 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004223 .GetMinimumLanguage();
4224 }
4225 }
4226 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004227}
4228
4229lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004230ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4231 if (!type)
4232 return lldb::eTypeClassInvalid;
4233
4234 clang::QualType qual_type(GetQualType(type));
4235
4236 switch (qual_type->getTypeClass()) {
4237 case clang::Type::UnaryTransform:
4238 break;
4239 case clang::Type::FunctionNoProto:
4240 return lldb::eTypeClassFunction;
4241 case clang::Type::FunctionProto:
4242 return lldb::eTypeClassFunction;
4243 case clang::Type::IncompleteArray:
4244 return lldb::eTypeClassArray;
4245 case clang::Type::VariableArray:
4246 return lldb::eTypeClassArray;
4247 case clang::Type::ConstantArray:
4248 return lldb::eTypeClassArray;
4249 case clang::Type::DependentSizedArray:
4250 return lldb::eTypeClassArray;
4251 case clang::Type::DependentSizedExtVector:
4252 return lldb::eTypeClassVector;
Fangrui Song8f284882018-07-13 22:40:40 +00004253 case clang::Type::DependentVector:
4254 return lldb::eTypeClassVector;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004255 case clang::Type::ExtVector:
4256 return lldb::eTypeClassVector;
4257 case clang::Type::Vector:
4258 return lldb::eTypeClassVector;
4259 case clang::Type::Builtin:
4260 return lldb::eTypeClassBuiltin;
4261 case clang::Type::ObjCObjectPointer:
4262 return lldb::eTypeClassObjCObjectPointer;
4263 case clang::Type::BlockPointer:
4264 return lldb::eTypeClassBlockPointer;
4265 case clang::Type::Pointer:
4266 return lldb::eTypeClassPointer;
4267 case clang::Type::LValueReference:
4268 return lldb::eTypeClassReference;
4269 case clang::Type::RValueReference:
4270 return lldb::eTypeClassReference;
4271 case clang::Type::MemberPointer:
4272 return lldb::eTypeClassMemberPointer;
4273 case clang::Type::Complex:
4274 if (qual_type->isComplexType())
4275 return lldb::eTypeClassComplexFloat;
4276 else
4277 return lldb::eTypeClassComplexInteger;
4278 case clang::Type::ObjCObject:
4279 return lldb::eTypeClassObjCObject;
4280 case clang::Type::ObjCInterface:
4281 return lldb::eTypeClassObjCInterface;
4282 case clang::Type::Record: {
4283 const clang::RecordType *record_type =
4284 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4285 const clang::RecordDecl *record_decl = record_type->getDecl();
4286 if (record_decl->isUnion())
4287 return lldb::eTypeClassUnion;
4288 else if (record_decl->isStruct())
4289 return lldb::eTypeClassStruct;
4290 else
4291 return lldb::eTypeClassClass;
4292 } break;
4293 case clang::Type::Enum:
4294 return lldb::eTypeClassEnumeration;
4295 case clang::Type::Typedef:
4296 return lldb::eTypeClassTypedef;
4297 case clang::Type::UnresolvedUsing:
4298 break;
4299 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00004300 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
4301 ->desugar()
4302 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004303 .GetTypeClass();
4304 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00004305 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
4306 ->getDeducedType()
4307 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004308 .GetTypeClass();
4309 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00004310 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
4311 ->getNamedType()
4312 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004313 .GetTypeClass();
4314
4315 case clang::Type::Attributed:
4316 break;
4317 case clang::Type::TemplateTypeParm:
4318 break;
4319 case clang::Type::SubstTemplateTypeParm:
4320 break;
4321 case clang::Type::SubstTemplateTypeParmPack:
4322 break;
4323 case clang::Type::InjectedClassName:
4324 break;
4325 case clang::Type::DependentName:
4326 break;
4327 case clang::Type::DependentTemplateSpecialization:
4328 break;
4329 case clang::Type::PackExpansion:
4330 break;
4331
4332 case clang::Type::TypeOfExpr:
Alex Langfordbddab072019-08-13 19:40:36 +00004333 return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type)
4334 ->getUnderlyingExpr()
4335 ->getType()
4336 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004337 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004338 case clang::Type::TypeOf:
Alex Langfordbddab072019-08-13 19:40:36 +00004339 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
4340 ->getUnderlyingType()
4341 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004342 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004343 case clang::Type::Decltype:
Alex Langfordbddab072019-08-13 19:40:36 +00004344 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
4345 ->getUnderlyingType()
4346 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004347 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004348 case clang::Type::TemplateSpecialization:
4349 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004350 case clang::Type::DeducedTemplateSpecialization:
4351 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004352 case clang::Type::Atomic:
4353 break;
4354 case clang::Type::Pipe:
4355 break;
4356
4357 // pointer type decayed from an array or function type.
4358 case clang::Type::Decayed:
4359 break;
4360 case clang::Type::Adjusted:
4361 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004362 case clang::Type::ObjCTypeParam:
4363 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00004364
4365 case clang::Type::DependentAddressSpace:
4366 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00004367 case clang::Type::MacroQualified:
4368 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004369 }
4370 // We don't know hot to display this type...
4371 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004372}
4373
Kate Stoneb9c1b512016-09-06 20:57:50 +00004374unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4375 if (type)
4376 return GetQualType(type).getQualifiers().getCVRQualifiers();
4377 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004378}
4379
Greg Claytond8d4a572015-08-11 21:38:15 +00004380// Creating related types
Greg Claytond8d4a572015-08-11 21:38:15 +00004381
Greg Claytona1e5dc82015-08-11 22:53:00 +00004382CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004383ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4384 uint64_t *stride) {
4385 if (type) {
4386 clang::QualType qual_type(GetCanonicalQualType(type));
4387
4388 const clang::Type *array_eletype =
4389 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4390
4391 if (!array_eletype)
4392 return CompilerType();
4393
Alex Langfordbddab072019-08-13 19:40:36 +00004394 CompilerType element_type(
4395 this, array_eletype->getCanonicalTypeUnqualified().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004396
4397 // TODO: the real stride will be >= this value.. find the real one!
4398 if (stride)
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00004399 if (Optional<uint64_t> size = element_type.GetByteSize(nullptr))
Adrian Prantld963a7c2019-01-15 18:07:52 +00004400 *stride = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004401
4402 return element_type;
4403 }
4404 return CompilerType();
4405}
4406
4407CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4408 uint64_t size) {
4409 if (type) {
4410 clang::QualType qual_type(GetCanonicalQualType(type));
4411 if (clang::ASTContext *ast_ctx = getASTContext()) {
4412 if (size != 0)
4413 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004414 this, ast_ctx
4415 ->getConstantArrayType(
Richard Smith772e2662019-10-04 01:25:59 +00004416 qual_type, llvm::APInt(64, size), nullptr,
Alex Langfordbddab072019-08-13 19:40:36 +00004417 clang::ArrayType::ArraySizeModifier::Normal, 0)
4418 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004419 else
4420 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004421 this,
4422 ast_ctx
4423 ->getIncompleteArrayType(
4424 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0)
4425 .getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00004426 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004427 }
4428
4429 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004430}
4431
Greg Claytona1e5dc82015-08-11 22:53:00 +00004432CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004433ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4434 if (type)
Alex Langfordbddab072019-08-13 19:40:36 +00004435 return CompilerType(this, GetCanonicalQualType(type).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004436 return CompilerType();
4437}
4438
4439static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4440 clang::QualType qual_type) {
4441 if (qual_type->isPointerType())
4442 qual_type = ast->getPointerType(
4443 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4444 else
4445 qual_type = qual_type.getUnqualifiedType();
4446 qual_type.removeLocalConst();
4447 qual_type.removeLocalRestrict();
4448 qual_type.removeLocalVolatile();
4449 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004450}
4451
4452CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004453ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4454 if (type)
4455 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004456 this,
4457 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004458 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004459}
4460
Kate Stoneb9c1b512016-09-06 20:57:50 +00004461int ClangASTContext::GetFunctionArgumentCount(
4462 lldb::opaque_compiler_type_t type) {
4463 if (type) {
4464 const clang::FunctionProtoType *func =
4465 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4466 if (func)
4467 return func->getNumParams();
4468 }
4469 return -1;
4470}
4471
4472CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4473 lldb::opaque_compiler_type_t type, size_t idx) {
4474 if (type) {
4475 const clang::FunctionProtoType *func =
4476 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4477 if (func) {
4478 const uint32_t num_args = func->getNumParams();
4479 if (idx < num_args)
Alex Langfordbddab072019-08-13 19:40:36 +00004480 return CompilerType(this, func->getParamType(idx).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004481 }
4482 }
4483 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004484}
4485
Greg Claytona1e5dc82015-08-11 22:53:00 +00004486CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004487ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4488 if (type) {
4489 clang::QualType qual_type(GetQualType(type));
4490 const clang::FunctionProtoType *func =
4491 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4492 if (func)
Alex Langfordbddab072019-08-13 19:40:36 +00004493 return CompilerType(this, func->getReturnType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004494 }
4495 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004496}
4497
4498size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004499ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4500 size_t num_functions = 0;
4501 if (type) {
4502 clang::QualType qual_type(GetCanonicalQualType(type));
4503 switch (qual_type->getTypeClass()) {
4504 case clang::Type::Record:
4505 if (GetCompleteQualType(getASTContext(), qual_type)) {
4506 const clang::RecordType *record_type =
4507 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4508 const clang::RecordDecl *record_decl = record_type->getDecl();
4509 assert(record_decl);
4510 const clang::CXXRecordDecl *cxx_record_decl =
4511 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4512 if (cxx_record_decl)
4513 num_functions = std::distance(cxx_record_decl->method_begin(),
4514 cxx_record_decl->method_end());
4515 }
4516 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004517
Sean Callananf9c622a2016-09-30 18:44:43 +00004518 case clang::Type::ObjCObjectPointer: {
4519 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004520 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004521 const clang::ObjCInterfaceType *objc_interface_type =
4522 objc_class_type->getInterfaceType();
4523 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004524 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4525 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004526 clang::ObjCInterfaceDecl *class_interface_decl =
4527 objc_interface_type->getDecl();
4528 if (class_interface_decl) {
4529 num_functions = std::distance(class_interface_decl->meth_begin(),
4530 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004531 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004532 }
4533 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004534 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004535
4536 case clang::Type::ObjCObject:
4537 case clang::Type::ObjCInterface:
4538 if (GetCompleteType(type)) {
4539 const clang::ObjCObjectType *objc_class_type =
4540 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4541 if (objc_class_type) {
4542 clang::ObjCInterfaceDecl *class_interface_decl =
4543 objc_class_type->getInterface();
4544 if (class_interface_decl)
4545 num_functions = std::distance(class_interface_decl->meth_begin(),
4546 class_interface_decl->meth_end());
4547 }
4548 }
4549 break;
4550
4551 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00004552 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
4553 ->getDecl()
4554 ->getUnderlyingType()
4555 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004556 .GetNumMemberFunctions();
4557
4558 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00004559 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
4560 ->getDeducedType()
4561 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004562 .GetNumMemberFunctions();
4563
4564 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00004565 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
4566 ->getNamedType()
4567 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004568 .GetNumMemberFunctions();
4569
4570 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00004571 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
4572 ->desugar()
4573 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004574 .GetNumMemberFunctions();
4575
4576 default:
4577 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004578 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004579 }
4580 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004581}
4582
4583TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004584ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4585 size_t idx) {
4586 std::string name;
4587 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4588 CompilerType clang_type;
4589 CompilerDecl clang_decl;
4590 if (type) {
4591 clang::QualType qual_type(GetCanonicalQualType(type));
4592 switch (qual_type->getTypeClass()) {
4593 case clang::Type::Record:
4594 if (GetCompleteQualType(getASTContext(), qual_type)) {
4595 const clang::RecordType *record_type =
4596 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4597 const clang::RecordDecl *record_decl = record_type->getDecl();
4598 assert(record_decl);
4599 const clang::CXXRecordDecl *cxx_record_decl =
4600 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4601 if (cxx_record_decl) {
4602 auto method_iter = cxx_record_decl->method_begin();
4603 auto method_end = cxx_record_decl->method_end();
4604 if (idx <
4605 static_cast<size_t>(std::distance(method_iter, method_end))) {
4606 std::advance(method_iter, idx);
4607 clang::CXXMethodDecl *cxx_method_decl =
4608 method_iter->getCanonicalDecl();
4609 if (cxx_method_decl) {
4610 name = cxx_method_decl->getDeclName().getAsString();
4611 if (cxx_method_decl->isStatic())
4612 kind = lldb::eMemberFunctionKindStaticMethod;
4613 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4614 kind = lldb::eMemberFunctionKindConstructor;
4615 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4616 kind = lldb::eMemberFunctionKindDestructor;
4617 else
4618 kind = lldb::eMemberFunctionKindInstanceMethod;
4619 clang_type = CompilerType(
4620 this, cxx_method_decl->getType().getAsOpaquePtr());
4621 clang_decl = CompilerDecl(this, cxx_method_decl);
4622 }
4623 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004624 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004625 }
4626 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004627
Sean Callananf9c622a2016-09-30 18:44:43 +00004628 case clang::Type::ObjCObjectPointer: {
4629 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004630 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004631 const clang::ObjCInterfaceType *objc_interface_type =
4632 objc_class_type->getInterfaceType();
4633 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004634 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4635 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004636 clang::ObjCInterfaceDecl *class_interface_decl =
4637 objc_interface_type->getDecl();
4638 if (class_interface_decl) {
4639 auto method_iter = class_interface_decl->meth_begin();
4640 auto method_end = class_interface_decl->meth_end();
4641 if (idx <
4642 static_cast<size_t>(std::distance(method_iter, method_end))) {
4643 std::advance(method_iter, idx);
4644 clang::ObjCMethodDecl *objc_method_decl =
4645 method_iter->getCanonicalDecl();
4646 if (objc_method_decl) {
4647 clang_decl = CompilerDecl(this, objc_method_decl);
4648 name = objc_method_decl->getSelector().getAsString();
4649 if (objc_method_decl->isClassMethod())
4650 kind = lldb::eMemberFunctionKindStaticMethod;
4651 else
4652 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004653 }
4654 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004655 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004656 }
4657 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004658 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004659
Kate Stoneb9c1b512016-09-06 20:57:50 +00004660 case clang::Type::ObjCObject:
4661 case clang::Type::ObjCInterface:
4662 if (GetCompleteType(type)) {
4663 const clang::ObjCObjectType *objc_class_type =
4664 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4665 if (objc_class_type) {
4666 clang::ObjCInterfaceDecl *class_interface_decl =
4667 objc_class_type->getInterface();
4668 if (class_interface_decl) {
4669 auto method_iter = class_interface_decl->meth_begin();
4670 auto method_end = class_interface_decl->meth_end();
4671 if (idx <
4672 static_cast<size_t>(std::distance(method_iter, method_end))) {
4673 std::advance(method_iter, idx);
4674 clang::ObjCMethodDecl *objc_method_decl =
4675 method_iter->getCanonicalDecl();
4676 if (objc_method_decl) {
4677 clang_decl = CompilerDecl(this, objc_method_decl);
4678 name = objc_method_decl->getSelector().getAsString();
4679 if (objc_method_decl->isClassMethod())
4680 kind = lldb::eMemberFunctionKindStaticMethod;
4681 else
4682 kind = lldb::eMemberFunctionKindInstanceMethod;
4683 }
4684 }
4685 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004686 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004687 }
4688 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004689
Kate Stoneb9c1b512016-09-06 20:57:50 +00004690 case clang::Type::Typedef:
4691 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4692 ->getDecl()
4693 ->getUnderlyingType()
4694 .getAsOpaquePtr(),
4695 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004696
Kate Stoneb9c1b512016-09-06 20:57:50 +00004697 case clang::Type::Auto:
4698 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4699 ->getDeducedType()
4700 .getAsOpaquePtr(),
4701 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004702
Kate Stoneb9c1b512016-09-06 20:57:50 +00004703 case clang::Type::Elaborated:
4704 return GetMemberFunctionAtIndex(
4705 llvm::cast<clang::ElaboratedType>(qual_type)
4706 ->getNamedType()
4707 .getAsOpaquePtr(),
4708 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004709
Kate Stoneb9c1b512016-09-06 20:57:50 +00004710 case clang::Type::Paren:
4711 return GetMemberFunctionAtIndex(
4712 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4713 idx);
4714
4715 default:
4716 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004717 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004718 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004719
Kate Stoneb9c1b512016-09-06 20:57:50 +00004720 if (kind == eMemberFunctionKindUnknown)
4721 return TypeMemberFunctionImpl();
4722 else
4723 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004724}
4725
Greg Claytona1e5dc82015-08-11 22:53:00 +00004726CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004727ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4728 if (type)
Alex Langfordbddab072019-08-13 19:40:36 +00004729 return CompilerType(
4730 this, GetQualType(type).getNonReferenceType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004731 return CompilerType();
4732}
4733
4734CompilerType ClangASTContext::CreateTypedefType(
4735 const CompilerType &type, const char *typedef_name,
4736 const CompilerDeclContext &compiler_decl_ctx) {
4737 if (type && typedef_name && typedef_name[0]) {
4738 ClangASTContext *ast =
4739 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4740 if (!ast)
4741 return CompilerType();
4742 clang::ASTContext *clang_ast = ast->getASTContext();
4743 clang::QualType qual_type(ClangUtil::GetQualType(type));
4744
4745 clang::DeclContext *decl_ctx =
4746 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4747 if (decl_ctx == nullptr)
4748 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4749
4750 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4751 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4752 &clang_ast->Idents.get(typedef_name),
4753 clang_ast->getTrivialTypeSourceInfo(qual_type));
4754
4755 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4756
Aleksandr Urakov709426b2018-09-10 08:08:43 +00004757 decl_ctx->addDecl(decl);
4758
Kate Stoneb9c1b512016-09-06 20:57:50 +00004759 // Get a uniqued clang::QualType for the typedef decl type
Alex Langfordbddab072019-08-13 19:40:36 +00004760 return CompilerType(ast, clang_ast->getTypedefType(decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004761 }
4762 return CompilerType();
4763}
4764
4765CompilerType
4766ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4767 if (type) {
4768 clang::QualType qual_type(GetQualType(type));
Alex Langfordbddab072019-08-13 19:40:36 +00004769 return CompilerType(
4770 this, qual_type.getTypePtr()->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004771 }
4772 return CompilerType();
4773}
4774
4775CompilerType
4776ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4777 if (type) {
4778 clang::QualType qual_type(GetQualType(type));
4779
4780 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4781 switch (type_class) {
4782 case clang::Type::ObjCObject:
4783 case clang::Type::ObjCInterface:
Alex Langfordbddab072019-08-13 19:40:36 +00004784 return CompilerType(this, getASTContext()
4785 ->getObjCObjectPointerType(qual_type)
4786 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004787
4788 default:
Alex Langfordbddab072019-08-13 19:40:36 +00004789 return CompilerType(
4790 this, getASTContext()->getPointerType(qual_type).getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00004791 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004792 }
4793 return CompilerType();
4794}
4795
4796CompilerType
4797ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4798 if (type)
4799 return CompilerType(this, getASTContext()
4800 ->getLValueReferenceType(GetQualType(type))
4801 .getAsOpaquePtr());
4802 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004803 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004804}
4805
Kate Stoneb9c1b512016-09-06 20:57:50 +00004806CompilerType
4807ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4808 if (type)
4809 return CompilerType(this, getASTContext()
4810 ->getRValueReferenceType(GetQualType(type))
4811 .getAsOpaquePtr());
4812 else
4813 return CompilerType();
4814}
4815
4816CompilerType
4817ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4818 if (type) {
4819 clang::QualType result(GetQualType(type));
4820 result.addConst();
4821 return CompilerType(this, result.getAsOpaquePtr());
4822 }
4823 return CompilerType();
4824}
4825
4826CompilerType
4827ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4828 if (type) {
4829 clang::QualType result(GetQualType(type));
4830 result.addVolatile();
4831 return CompilerType(this, result.getAsOpaquePtr());
4832 }
4833 return CompilerType();
4834}
4835
4836CompilerType
4837ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4838 if (type) {
4839 clang::QualType result(GetQualType(type));
4840 result.addRestrict();
4841 return CompilerType(this, result.getAsOpaquePtr());
4842 }
4843 return CompilerType();
4844}
4845
4846CompilerType
4847ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4848 const char *typedef_name,
4849 const CompilerDeclContext &compiler_decl_ctx) {
4850 if (type) {
4851 clang::ASTContext *clang_ast = getASTContext();
4852 clang::QualType qual_type(GetQualType(type));
4853
4854 clang::DeclContext *decl_ctx =
4855 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4856 if (decl_ctx == nullptr)
4857 decl_ctx = getASTContext()->getTranslationUnitDecl();
4858
4859 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4860 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4861 &clang_ast->Idents.get(typedef_name),
4862 clang_ast->getTrivialTypeSourceInfo(qual_type));
4863
4864 clang::TagDecl *tdecl = nullptr;
4865 if (!qual_type.isNull()) {
4866 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4867 tdecl = rt->getDecl();
4868 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4869 tdecl = et->getDecl();
4870 }
4871
4872 // Check whether this declaration is an anonymous struct, union, or enum,
Adrian Prantl05097242018-04-30 16:49:04 +00004873 // hidden behind a typedef. If so, we try to check whether we have a
4874 // typedef tag to attach to the original record declaration
Kate Stoneb9c1b512016-09-06 20:57:50 +00004875 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4876 tdecl->setTypedefNameForAnonDecl(decl);
4877
4878 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4879
4880 // Get a uniqued clang::QualType for the typedef decl type
4881 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4882 }
4883 return CompilerType();
4884}
4885
4886CompilerType
4887ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4888 if (type) {
4889 const clang::TypedefType *typedef_type =
4890 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4891 if (typedef_type)
Alex Langfordbddab072019-08-13 19:40:36 +00004892 return CompilerType(
4893 this, typedef_type->getDecl()->getUnderlyingType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004894 }
4895 return CompilerType();
4896}
Greg Claytond8d4a572015-08-11 21:38:15 +00004897
Greg Claytond8d4a572015-08-11 21:38:15 +00004898// Create related types using the current type's AST
Greg Claytond8d4a572015-08-11 21:38:15 +00004899
Kate Stoneb9c1b512016-09-06 20:57:50 +00004900CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
Raphael Isemannc502bae2019-11-20 12:40:08 +01004901 return ClangASTContext::GetBasicType(basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004902}
Greg Claytond8d4a572015-08-11 21:38:15 +00004903// Exploring the type
Greg Claytond8d4a572015-08-11 21:38:15 +00004904
Alex Langfordb482db62019-09-06 21:05:21 +00004905const llvm::fltSemantics &
4906ClangASTContext::GetFloatTypeSemantics(size_t byte_size) {
4907 if (auto *ast = getASTContext()) {
4908 const size_t bit_size = byte_size * 8;
4909 if (bit_size == ast->getTypeSize(ast->FloatTy))
4910 return ast->getFloatTypeSemantics(ast->FloatTy);
4911 else if (bit_size == ast->getTypeSize(ast->DoubleTy))
4912 return ast->getFloatTypeSemantics(ast->DoubleTy);
4913 else if (bit_size == ast->getTypeSize(ast->LongDoubleTy))
4914 return ast->getFloatTypeSemantics(ast->LongDoubleTy);
4915 else if (bit_size == ast->getTypeSize(ast->HalfTy))
4916 return ast->getFloatTypeSemantics(ast->HalfTy);
4917 }
4918 return llvm::APFloatBase::Bogus();
4919}
4920
Adrian Prantl2ee7b882019-01-16 21:19:20 +00004921Optional<uint64_t>
4922ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4923 ExecutionContextScope *exe_scope) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00004924 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00004925 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004926 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004927 switch (type_class) {
4928 case clang::Type::Record:
4929 if (GetCompleteType(type))
4930 return getASTContext()->getTypeSize(qual_type);
4931 else
Adrian Prantl2ee7b882019-01-16 21:19:20 +00004932 return None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004933 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004934
Kate Stoneb9c1b512016-09-06 20:57:50 +00004935 case clang::Type::ObjCInterface:
4936 case clang::Type::ObjCObject: {
4937 ExecutionContext exe_ctx(exe_scope);
4938 Process *process = exe_ctx.GetProcessPtr();
4939 if (process) {
Alex Langforde823bbe2019-06-10 20:53:23 +00004940 ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004941 if (objc_runtime) {
4942 uint64_t bit_size = 0;
4943 if (objc_runtime->GetTypeBitSize(
Alex Langfordbddab072019-08-13 19:40:36 +00004944 CompilerType(this, qual_type.getAsOpaquePtr()), bit_size))
Kate Stoneb9c1b512016-09-06 20:57:50 +00004945 return bit_size;
4946 }
4947 } else {
4948 static bool g_printed = false;
4949 if (!g_printed) {
4950 StreamString s;
4951 DumpTypeDescription(type, &s);
4952
4953 llvm::outs() << "warning: trying to determine the size of type ";
4954 llvm::outs() << s.GetString() << "\n";
4955 llvm::outs() << "without a valid ExecutionContext. this is not "
4956 "reliable. please file a bug against LLDB.\n";
4957 llvm::outs() << "backtrace:\n";
4958 llvm::sys::PrintStackTrace(llvm::outs());
4959 llvm::outs() << "\n";
4960 g_printed = true;
4961 }
4962 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004963 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004964 LLVM_FALLTHROUGH;
4965 default:
4966 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
4967 if (bit_size == 0) {
4968 if (qual_type->isIncompleteArrayType())
4969 return getASTContext()->getTypeSize(
4970 qual_type->getArrayElementTypeNoTypeQual()
4971 ->getCanonicalTypeUnqualified());
4972 }
4973 if (qual_type->isObjCObjectOrInterfaceType())
4974 return bit_size +
4975 getASTContext()->getTypeSize(
4976 getASTContext()->ObjCBuiltinClassTy);
Adrian Prantl2ee7b882019-01-16 21:19:20 +00004977 // Function types actually have a size of 0, that's not an error.
4978 if (qual_type->isFunctionProtoType())
4979 return bit_size;
4980 if (bit_size)
4981 return bit_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004982 }
4983 }
Adrian Prantl2ee7b882019-01-16 21:19:20 +00004984 return None;
Greg Claytond8d4a572015-08-11 21:38:15 +00004985}
4986
Davide Italiano36f13e42019-08-12 20:03:19 +00004987llvm::Optional<size_t>
Davide Italiano7f9bbe02019-08-12 21:49:54 +00004988ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type,
4989 ExecutionContextScope *exe_scope) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00004990 if (GetCompleteType(type))
4991 return getASTContext()->getTypeAlign(GetQualType(type));
Davide Italiano36f13e42019-08-12 20:03:19 +00004992 return {};
Kate Stoneb9c1b512016-09-06 20:57:50 +00004993}
4994
4995lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
4996 uint64_t &count) {
4997 if (!type)
4998 return lldb::eEncodingInvalid;
4999
5000 count = 1;
5001 clang::QualType qual_type(GetCanonicalQualType(type));
5002
5003 switch (qual_type->getTypeClass()) {
5004 case clang::Type::UnaryTransform:
5005 break;
5006
5007 case clang::Type::FunctionNoProto:
5008 case clang::Type::FunctionProto:
5009 break;
5010
5011 case clang::Type::IncompleteArray:
5012 case clang::Type::VariableArray:
5013 break;
5014
5015 case clang::Type::ConstantArray:
5016 break;
5017
Fangrui Song8f284882018-07-13 22:40:40 +00005018 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005019 case clang::Type::ExtVector:
5020 case clang::Type::Vector:
5021 // TODO: Set this to more than one???
5022 break;
5023
5024 case clang::Type::Builtin:
5025 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5026 case clang::BuiltinType::Void:
5027 break;
5028
5029 case clang::BuiltinType::Bool:
5030 case clang::BuiltinType::Char_S:
5031 case clang::BuiltinType::SChar:
5032 case clang::BuiltinType::WChar_S:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005033 case clang::BuiltinType::Short:
5034 case clang::BuiltinType::Int:
5035 case clang::BuiltinType::Long:
5036 case clang::BuiltinType::LongLong:
5037 case clang::BuiltinType::Int128:
5038 return lldb::eEncodingSint;
5039
5040 case clang::BuiltinType::Char_U:
5041 case clang::BuiltinType::UChar:
5042 case clang::BuiltinType::WChar_U:
Richard Smith51d12d82018-05-02 02:43:22 +00005043 case clang::BuiltinType::Char8:
5044 case clang::BuiltinType::Char16:
5045 case clang::BuiltinType::Char32:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005046 case clang::BuiltinType::UShort:
5047 case clang::BuiltinType::UInt:
5048 case clang::BuiltinType::ULong:
5049 case clang::BuiltinType::ULongLong:
5050 case clang::BuiltinType::UInt128:
5051 return lldb::eEncodingUint;
5052
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005053 // Fixed point types. Note that they are currently ignored.
5054 case clang::BuiltinType::ShortAccum:
5055 case clang::BuiltinType::Accum:
5056 case clang::BuiltinType::LongAccum:
5057 case clang::BuiltinType::UShortAccum:
5058 case clang::BuiltinType::UAccum:
5059 case clang::BuiltinType::ULongAccum:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005060 case clang::BuiltinType::ShortFract:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005061 case clang::BuiltinType::Fract:
5062 case clang::BuiltinType::LongFract:
5063 case clang::BuiltinType::UShortFract:
5064 case clang::BuiltinType::UFract:
5065 case clang::BuiltinType::ULongFract:
5066 case clang::BuiltinType::SatShortAccum:
5067 case clang::BuiltinType::SatAccum:
5068 case clang::BuiltinType::SatLongAccum:
5069 case clang::BuiltinType::SatUShortAccum:
5070 case clang::BuiltinType::SatUAccum:
5071 case clang::BuiltinType::SatULongAccum:
5072 case clang::BuiltinType::SatShortFract:
5073 case clang::BuiltinType::SatFract:
5074 case clang::BuiltinType::SatLongFract:
5075 case clang::BuiltinType::SatUShortFract:
5076 case clang::BuiltinType::SatUFract:
5077 case clang::BuiltinType::SatULongFract:
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005078 break;
5079
Kate Stoneb9c1b512016-09-06 20:57:50 +00005080 case clang::BuiltinType::Half:
5081 case clang::BuiltinType::Float:
Ted Woodward4355c7c2017-09-20 19:16:53 +00005082 case clang::BuiltinType::Float16:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005083 case clang::BuiltinType::Float128:
5084 case clang::BuiltinType::Double:
5085 case clang::BuiltinType::LongDouble:
5086 return lldb::eEncodingIEEE754;
5087
5088 case clang::BuiltinType::ObjCClass:
5089 case clang::BuiltinType::ObjCId:
5090 case clang::BuiltinType::ObjCSel:
5091 return lldb::eEncodingUint;
5092
5093 case clang::BuiltinType::NullPtr:
5094 return lldb::eEncodingUint;
5095
5096 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5097 case clang::BuiltinType::Kind::BoundMember:
5098 case clang::BuiltinType::Kind::BuiltinFn:
5099 case clang::BuiltinType::Kind::Dependent:
5100 case clang::BuiltinType::Kind::OCLClkEvent:
5101 case clang::BuiltinType::Kind::OCLEvent:
5102 case clang::BuiltinType::Kind::OCLImage1dRO:
5103 case clang::BuiltinType::Kind::OCLImage1dWO:
5104 case clang::BuiltinType::Kind::OCLImage1dRW:
5105 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5106 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5107 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5108 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5109 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5110 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5111 case clang::BuiltinType::Kind::OCLImage2dRO:
5112 case clang::BuiltinType::Kind::OCLImage2dWO:
5113 case clang::BuiltinType::Kind::OCLImage2dRW:
5114 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5115 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5116 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5117 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5118 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5119 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5120 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5121 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5122 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5123 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5124 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5125 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5126 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5127 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5128 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5129 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5130 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5131 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5132 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5133 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5134 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5135 case clang::BuiltinType::Kind::OCLImage3dRO:
5136 case clang::BuiltinType::Kind::OCLImage3dWO:
5137 case clang::BuiltinType::Kind::OCLImage3dRW:
5138 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005139 case clang::BuiltinType::Kind::OCLReserveID:
5140 case clang::BuiltinType::Kind::OCLSampler:
5141 case clang::BuiltinType::Kind::OMPArraySection:
5142 case clang::BuiltinType::Kind::Overload:
5143 case clang::BuiltinType::Kind::PseudoObject:
5144 case clang::BuiltinType::Kind::UnknownAny:
5145 break;
Jorge Gorbe Moyaa6e6c182018-11-08 22:04:58 +00005146
5147 case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload:
5148 case clang::BuiltinType::OCLIntelSubgroupAVCImePayload:
5149 case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload:
5150 case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload:
5151 case clang::BuiltinType::OCLIntelSubgroupAVCMceResult:
5152 case clang::BuiltinType::OCLIntelSubgroupAVCImeResult:
5153 case clang::BuiltinType::OCLIntelSubgroupAVCRefResult:
5154 case clang::BuiltinType::OCLIntelSubgroupAVCSicResult:
5155 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleRefStreamout:
5156 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualRefStreamout:
5157 case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleRefStreamin:
5158 case clang::BuiltinType::OCLIntelSubgroupAVCImeDualRefStreamin:
5159 break;
Raphael Isemann339b5d12019-08-09 09:58:47 +00005160
5161 case clang::BuiltinType::SveBool:
5162 case clang::BuiltinType::SveInt8:
5163 case clang::BuiltinType::SveInt16:
5164 case clang::BuiltinType::SveInt32:
5165 case clang::BuiltinType::SveInt64:
5166 case clang::BuiltinType::SveUint8:
5167 case clang::BuiltinType::SveUint16:
5168 case clang::BuiltinType::SveUint32:
5169 case clang::BuiltinType::SveUint64:
5170 case clang::BuiltinType::SveFloat16:
5171 case clang::BuiltinType::SveFloat32:
5172 case clang::BuiltinType::SveFloat64:
5173 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005174 }
5175 break;
Adrian Prantl05097242018-04-30 16:49:04 +00005176 // All pointer types are represented as unsigned integer encodings. We may
5177 // nee to add a eEncodingPointer if we ever need to know the difference
Kate Stoneb9c1b512016-09-06 20:57:50 +00005178 case clang::Type::ObjCObjectPointer:
5179 case clang::Type::BlockPointer:
5180 case clang::Type::Pointer:
5181 case clang::Type::LValueReference:
5182 case clang::Type::RValueReference:
5183 case clang::Type::MemberPointer:
5184 return lldb::eEncodingUint;
5185 case clang::Type::Complex: {
5186 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5187 if (qual_type->isComplexType())
5188 encoding = lldb::eEncodingIEEE754;
5189 else {
5190 const clang::ComplexType *complex_type =
5191 qual_type->getAsComplexIntegerType();
5192 if (complex_type)
Alex Langfordbddab072019-08-13 19:40:36 +00005193 encoding =
5194 CompilerType(this, complex_type->getElementType().getAsOpaquePtr())
5195 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005196 else
5197 encoding = lldb::eEncodingSint;
5198 }
5199 count = 2;
5200 return encoding;
5201 }
5202
5203 case clang::Type::ObjCInterface:
5204 break;
5205 case clang::Type::Record:
5206 break;
5207 case clang::Type::Enum:
5208 return lldb::eEncodingSint;
5209 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00005210 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
5211 ->getDecl()
5212 ->getUnderlyingType()
5213 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005214 .GetEncoding(count);
5215
5216 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00005217 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
5218 ->getDeducedType()
5219 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005220 .GetEncoding(count);
5221
5222 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00005223 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
5224 ->getNamedType()
5225 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005226 .GetEncoding(count);
5227
5228 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00005229 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
5230 ->desugar()
5231 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005232 .GetEncoding(count);
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005233 case clang::Type::TypeOfExpr:
Alex Langfordbddab072019-08-13 19:40:36 +00005234 return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type)
5235 ->getUnderlyingExpr()
5236 ->getType()
5237 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005238 .GetEncoding(count);
5239 case clang::Type::TypeOf:
Alex Langfordbddab072019-08-13 19:40:36 +00005240 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
5241 ->getUnderlyingType()
5242 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005243 .GetEncoding(count);
5244 case clang::Type::Decltype:
Alex Langfordbddab072019-08-13 19:40:36 +00005245 return CompilerType(this, llvm::cast<clang::DecltypeType>(qual_type)
5246 ->getUnderlyingType()
5247 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005248 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005249 case clang::Type::DependentSizedArray:
5250 case clang::Type::DependentSizedExtVector:
5251 case clang::Type::UnresolvedUsing:
5252 case clang::Type::Attributed:
5253 case clang::Type::TemplateTypeParm:
5254 case clang::Type::SubstTemplateTypeParm:
5255 case clang::Type::SubstTemplateTypeParmPack:
5256 case clang::Type::InjectedClassName:
5257 case clang::Type::DependentName:
5258 case clang::Type::DependentTemplateSpecialization:
5259 case clang::Type::PackExpansion:
5260 case clang::Type::ObjCObject:
5261
Kate Stoneb9c1b512016-09-06 20:57:50 +00005262 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005263 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005264 case clang::Type::Atomic:
5265 case clang::Type::Adjusted:
5266 case clang::Type::Pipe:
5267 break;
5268
5269 // pointer type decayed from an array or function type.
5270 case clang::Type::Decayed:
5271 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005272 case clang::Type::ObjCTypeParam:
5273 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005274
5275 case clang::Type::DependentAddressSpace:
5276 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00005277 case clang::Type::MacroQualified:
5278 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005279 }
5280 count = 0;
5281 return lldb::eEncodingInvalid;
5282}
5283
5284lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5285 if (!type)
5286 return lldb::eFormatDefault;
5287
5288 clang::QualType qual_type(GetCanonicalQualType(type));
5289
5290 switch (qual_type->getTypeClass()) {
5291 case clang::Type::UnaryTransform:
5292 break;
5293
5294 case clang::Type::FunctionNoProto:
5295 case clang::Type::FunctionProto:
5296 break;
5297
5298 case clang::Type::IncompleteArray:
5299 case clang::Type::VariableArray:
5300 break;
5301
5302 case clang::Type::ConstantArray:
5303 return lldb::eFormatVoid; // no value
5304
Fangrui Song8f284882018-07-13 22:40:40 +00005305 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005306 case clang::Type::ExtVector:
5307 case clang::Type::Vector:
5308 break;
5309
5310 case clang::Type::Builtin:
5311 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5312 // default: assert(0 && "Unknown builtin type!");
5313 case clang::BuiltinType::UnknownAny:
5314 case clang::BuiltinType::Void:
5315 case clang::BuiltinType::BoundMember:
5316 break;
5317
5318 case clang::BuiltinType::Bool:
5319 return lldb::eFormatBoolean;
5320 case clang::BuiltinType::Char_S:
5321 case clang::BuiltinType::SChar:
5322 case clang::BuiltinType::WChar_S:
5323 case clang::BuiltinType::Char_U:
5324 case clang::BuiltinType::UChar:
5325 case clang::BuiltinType::WChar_U:
5326 return lldb::eFormatChar;
5327 case clang::BuiltinType::Char16:
5328 return lldb::eFormatUnicode16;
5329 case clang::BuiltinType::Char32:
5330 return lldb::eFormatUnicode32;
5331 case clang::BuiltinType::UShort:
5332 return lldb::eFormatUnsigned;
5333 case clang::BuiltinType::Short:
5334 return lldb::eFormatDecimal;
5335 case clang::BuiltinType::UInt:
5336 return lldb::eFormatUnsigned;
5337 case clang::BuiltinType::Int:
5338 return lldb::eFormatDecimal;
5339 case clang::BuiltinType::ULong:
5340 return lldb::eFormatUnsigned;
5341 case clang::BuiltinType::Long:
5342 return lldb::eFormatDecimal;
5343 case clang::BuiltinType::ULongLong:
5344 return lldb::eFormatUnsigned;
5345 case clang::BuiltinType::LongLong:
5346 return lldb::eFormatDecimal;
5347 case clang::BuiltinType::UInt128:
5348 return lldb::eFormatUnsigned;
5349 case clang::BuiltinType::Int128:
5350 return lldb::eFormatDecimal;
5351 case clang::BuiltinType::Half:
5352 case clang::BuiltinType::Float:
5353 case clang::BuiltinType::Double:
5354 case clang::BuiltinType::LongDouble:
5355 return lldb::eFormatFloat;
5356 default:
5357 return lldb::eFormatHex;
5358 }
5359 break;
5360 case clang::Type::ObjCObjectPointer:
5361 return lldb::eFormatHex;
5362 case clang::Type::BlockPointer:
5363 return lldb::eFormatHex;
5364 case clang::Type::Pointer:
5365 return lldb::eFormatHex;
5366 case clang::Type::LValueReference:
5367 case clang::Type::RValueReference:
5368 return lldb::eFormatHex;
5369 case clang::Type::MemberPointer:
5370 break;
5371 case clang::Type::Complex: {
5372 if (qual_type->isComplexType())
5373 return lldb::eFormatComplex;
5374 else
5375 return lldb::eFormatComplexInteger;
5376 }
5377 case clang::Type::ObjCInterface:
5378 break;
5379 case clang::Type::Record:
5380 break;
5381 case clang::Type::Enum:
5382 return lldb::eFormatEnum;
5383 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00005384 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
5385 ->getDecl()
5386 ->getUnderlyingType()
5387 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005388 .GetFormat();
5389 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00005390 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
5391 ->desugar()
5392 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005393 .GetFormat();
5394 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00005395 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
5396 ->desugar()
5397 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005398 .GetFormat();
5399 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00005400 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
5401 ->getNamedType()
5402 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005403 .GetFormat();
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005404 case clang::Type::TypeOfExpr:
Alex Langfordbddab072019-08-13 19:40:36 +00005405 return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type)
5406 ->getUnderlyingExpr()
5407 ->getType()
5408 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005409 .GetFormat();
5410 case clang::Type::TypeOf:
Alex Langfordbddab072019-08-13 19:40:36 +00005411 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
5412 ->getUnderlyingType()
5413 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005414 .GetFormat();
5415 case clang::Type::Decltype:
Alex Langfordbddab072019-08-13 19:40:36 +00005416 return CompilerType(this, llvm::cast<clang::DecltypeType>(qual_type)
5417 ->getUnderlyingType()
5418 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005419 .GetFormat();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005420 case clang::Type::DependentSizedArray:
5421 case clang::Type::DependentSizedExtVector:
5422 case clang::Type::UnresolvedUsing:
5423 case clang::Type::Attributed:
5424 case clang::Type::TemplateTypeParm:
5425 case clang::Type::SubstTemplateTypeParm:
5426 case clang::Type::SubstTemplateTypeParmPack:
5427 case clang::Type::InjectedClassName:
5428 case clang::Type::DependentName:
5429 case clang::Type::DependentTemplateSpecialization:
5430 case clang::Type::PackExpansion:
5431 case clang::Type::ObjCObject:
5432
Kate Stoneb9c1b512016-09-06 20:57:50 +00005433 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005434 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005435 case clang::Type::Atomic:
5436 case clang::Type::Adjusted:
5437 case clang::Type::Pipe:
5438 break;
5439
5440 // pointer type decayed from an array or function type.
5441 case clang::Type::Decayed:
5442 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005443 case clang::Type::ObjCTypeParam:
5444 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005445
5446 case clang::Type::DependentAddressSpace:
5447 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00005448 case clang::Type::MacroQualified:
5449 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005450 }
5451 // We don't know hot to display this type...
5452 return lldb::eFormatBytes;
5453}
5454
5455static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5456 bool check_superclass) {
5457 while (class_interface_decl) {
5458 if (class_interface_decl->ivar_size() > 0)
5459 return true;
5460
5461 if (check_superclass)
5462 class_interface_decl = class_interface_decl->getSuperClass();
5463 else
5464 break;
5465 }
5466 return false;
5467}
5468
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00005469static Optional<SymbolFile::ArrayInfo>
Adrian Prantleca07c52018-11-05 20:49:07 +00005470GetDynamicArrayInfo(ClangASTContext &ast, SymbolFile *sym_file,
5471 clang::QualType qual_type,
5472 const ExecutionContext *exe_ctx) {
5473 if (qual_type->isIncompleteArrayType())
5474 if (auto *metadata = ast.GetMetadata(qual_type.getAsOpaquePtr()))
Pavel Labathffec31e2018-12-28 13:34:44 +00005475 return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(),
5476 exe_ctx);
Adrian Prantleca07c52018-11-05 20:49:07 +00005477 return llvm::None;
5478}
5479
Kate Stoneb9c1b512016-09-06 20:57:50 +00005480uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
Adrian Prantleca07c52018-11-05 20:49:07 +00005481 bool omit_empty_base_classes,
5482 const ExecutionContext *exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005483 if (!type)
5484 return 0;
5485
5486 uint32_t num_children = 0;
5487 clang::QualType qual_type(GetQualType(type));
5488 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5489 switch (type_class) {
5490 case clang::Type::Builtin:
5491 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5492 case clang::BuiltinType::ObjCId: // child is Class
5493 case clang::BuiltinType::ObjCClass: // child is Class
5494 num_children = 1;
5495 break;
5496
5497 default:
5498 break;
5499 }
5500 break;
5501
5502 case clang::Type::Complex:
5503 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005504 case clang::Type::Record:
5505 if (GetCompleteQualType(getASTContext(), qual_type)) {
5506 const clang::RecordType *record_type =
5507 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5508 const clang::RecordDecl *record_decl = record_type->getDecl();
5509 assert(record_decl);
5510 const clang::CXXRecordDecl *cxx_record_decl =
5511 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5512 if (cxx_record_decl) {
5513 if (omit_empty_base_classes) {
Adrian Prantl05097242018-04-30 16:49:04 +00005514 // Check each base classes to see if it or any of its base classes
5515 // contain any fields. This can help limit the noise in variable
5516 // views by not having to show base classes that contain no members.
Kate Stoneb9c1b512016-09-06 20:57:50 +00005517 clang::CXXRecordDecl::base_class_const_iterator base_class,
5518 base_class_end;
5519 for (base_class = cxx_record_decl->bases_begin(),
5520 base_class_end = cxx_record_decl->bases_end();
5521 base_class != base_class_end; ++base_class) {
5522 const clang::CXXRecordDecl *base_class_decl =
5523 llvm::cast<clang::CXXRecordDecl>(
5524 base_class->getType()
5525 ->getAs<clang::RecordType>()
5526 ->getDecl());
5527
5528 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00005529 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00005530 continue;
5531
5532 num_children++;
5533 }
5534 } else {
5535 // Include all base classes
5536 num_children += cxx_record_decl->getNumBases();
5537 }
5538 }
5539 clang::RecordDecl::field_iterator field, field_end;
5540 for (field = record_decl->field_begin(),
5541 field_end = record_decl->field_end();
5542 field != field_end; ++field)
5543 ++num_children;
5544 }
5545 break;
5546
5547 case clang::Type::ObjCObject:
5548 case clang::Type::ObjCInterface:
5549 if (GetCompleteQualType(getASTContext(), qual_type)) {
5550 const clang::ObjCObjectType *objc_class_type =
5551 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5552 assert(objc_class_type);
5553 if (objc_class_type) {
5554 clang::ObjCInterfaceDecl *class_interface_decl =
5555 objc_class_type->getInterface();
5556
5557 if (class_interface_decl) {
5558
5559 clang::ObjCInterfaceDecl *superclass_interface_decl =
5560 class_interface_decl->getSuperClass();
5561 if (superclass_interface_decl) {
5562 if (omit_empty_base_classes) {
5563 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5564 ++num_children;
5565 } else
5566 ++num_children;
5567 }
5568
5569 num_children += class_interface_decl->ivar_size();
5570 }
5571 }
5572 }
5573 break;
5574
5575 case clang::Type::ObjCObjectPointer: {
5576 const clang::ObjCObjectPointerType *pointer_type =
5577 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5578 clang::QualType pointee_type = pointer_type->getPointeeType();
5579 uint32_t num_pointee_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005580 CompilerType(this, pointee_type.getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005581 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005582 // If this type points to a simple type, then it has 1 child
5583 if (num_pointee_children == 0)
5584 num_children = 1;
5585 else
5586 num_children = num_pointee_children;
5587 } break;
5588
5589 case clang::Type::Vector:
5590 case clang::Type::ExtVector:
5591 num_children =
5592 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5593 break;
5594
5595 case clang::Type::ConstantArray:
5596 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5597 ->getSize()
5598 .getLimitedValue();
5599 break;
Adrian Prantleca07c52018-11-05 20:49:07 +00005600 case clang::Type::IncompleteArray:
5601 if (auto array_info =
5602 GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx))
5603 // Only 1-dimensional arrays are supported.
5604 num_children = array_info->element_orders.size()
5605 ? array_info->element_orders.back()
5606 : 0;
5607 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005608
5609 case clang::Type::Pointer: {
5610 const clang::PointerType *pointer_type =
5611 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5612 clang::QualType pointee_type(pointer_type->getPointeeType());
5613 uint32_t num_pointee_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005614 CompilerType(this, pointee_type.getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005615 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005616 if (num_pointee_children == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00005617 // We have a pointer to a pointee type that claims it has no children. We
5618 // will want to look at
Kate Stoneb9c1b512016-09-06 20:57:50 +00005619 num_children = GetNumPointeeChildren(pointee_type);
5620 } else
5621 num_children = num_pointee_children;
5622 } break;
5623
5624 case clang::Type::LValueReference:
5625 case clang::Type::RValueReference: {
5626 const clang::ReferenceType *reference_type =
5627 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5628 clang::QualType pointee_type = reference_type->getPointeeType();
5629 uint32_t num_pointee_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005630 CompilerType(this, pointee_type.getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005631 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005632 // If this type points to a simple type, then it has 1 child
5633 if (num_pointee_children == 0)
5634 num_children = 1;
5635 else
5636 num_children = num_pointee_children;
5637 } break;
5638
5639 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00005640 num_children = CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
Kate Stoneb9c1b512016-09-06 20:57:50 +00005641 ->getDecl()
Alex Langfordbddab072019-08-13 19:40:36 +00005642 ->getUnderlyingType()
5643 .getAsOpaquePtr())
5644 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005645 break;
5646
5647 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00005648 num_children = CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
5649 ->getDeducedType()
5650 .getAsOpaquePtr())
5651 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005652 break;
5653
5654 case clang::Type::Elaborated:
5655 num_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005656 CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
5657 ->getNamedType()
5658 .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
5662 case clang::Type::Paren:
5663 num_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005664 CompilerType(
5665 this,
5666 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005667 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005668 break;
5669 default:
5670 break;
5671 }
5672 return num_children;
5673}
5674
Adrian Prantl0e4c4822019-03-06 21:22:25 +00005675CompilerType ClangASTContext::GetBuiltinTypeByName(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005676 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005677}
5678
Greg Claytond8d4a572015-08-11 21:38:15 +00005679lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005680ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5681 if (type) {
5682 clang::QualType qual_type(GetQualType(type));
5683 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5684 if (type_class == clang::Type::Builtin) {
5685 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5686 case clang::BuiltinType::Void:
5687 return eBasicTypeVoid;
5688 case clang::BuiltinType::Bool:
5689 return eBasicTypeBool;
5690 case clang::BuiltinType::Char_S:
5691 return eBasicTypeSignedChar;
5692 case clang::BuiltinType::Char_U:
5693 return eBasicTypeUnsignedChar;
5694 case clang::BuiltinType::Char16:
5695 return eBasicTypeChar16;
5696 case clang::BuiltinType::Char32:
5697 return eBasicTypeChar32;
5698 case clang::BuiltinType::UChar:
5699 return eBasicTypeUnsignedChar;
5700 case clang::BuiltinType::SChar:
5701 return eBasicTypeSignedChar;
5702 case clang::BuiltinType::WChar_S:
5703 return eBasicTypeSignedWChar;
5704 case clang::BuiltinType::WChar_U:
5705 return eBasicTypeUnsignedWChar;
5706 case clang::BuiltinType::Short:
5707 return eBasicTypeShort;
5708 case clang::BuiltinType::UShort:
5709 return eBasicTypeUnsignedShort;
5710 case clang::BuiltinType::Int:
5711 return eBasicTypeInt;
5712 case clang::BuiltinType::UInt:
5713 return eBasicTypeUnsignedInt;
5714 case clang::BuiltinType::Long:
5715 return eBasicTypeLong;
5716 case clang::BuiltinType::ULong:
5717 return eBasicTypeUnsignedLong;
5718 case clang::BuiltinType::LongLong:
5719 return eBasicTypeLongLong;
5720 case clang::BuiltinType::ULongLong:
5721 return eBasicTypeUnsignedLongLong;
5722 case clang::BuiltinType::Int128:
5723 return eBasicTypeInt128;
5724 case clang::BuiltinType::UInt128:
5725 return eBasicTypeUnsignedInt128;
5726
5727 case clang::BuiltinType::Half:
5728 return eBasicTypeHalf;
5729 case clang::BuiltinType::Float:
5730 return eBasicTypeFloat;
5731 case clang::BuiltinType::Double:
5732 return eBasicTypeDouble;
5733 case clang::BuiltinType::LongDouble:
5734 return eBasicTypeLongDouble;
5735
5736 case clang::BuiltinType::NullPtr:
5737 return eBasicTypeNullPtr;
5738 case clang::BuiltinType::ObjCId:
5739 return eBasicTypeObjCID;
5740 case clang::BuiltinType::ObjCClass:
5741 return eBasicTypeObjCClass;
5742 case clang::BuiltinType::ObjCSel:
5743 return eBasicTypeObjCSel;
5744 default:
5745 return eBasicTypeOther;
5746 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005747 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005748 }
5749 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005750}
5751
Kate Stoneb9c1b512016-09-06 20:57:50 +00005752void ClangASTContext::ForEachEnumerator(
5753 lldb::opaque_compiler_type_t type,
5754 std::function<bool(const CompilerType &integer_type,
Adrian Prantl0e4c4822019-03-06 21:22:25 +00005755 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00005756 const llvm::APSInt &value)> const &callback) {
5757 const clang::EnumType *enum_type =
5758 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5759 if (enum_type) {
5760 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5761 if (enum_decl) {
5762 CompilerType integer_type(this,
5763 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005764
Kate Stoneb9c1b512016-09-06 20:57:50 +00005765 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5766 for (enum_pos = enum_decl->enumerator_begin(),
5767 enum_end_pos = enum_decl->enumerator_end();
5768 enum_pos != enum_end_pos; ++enum_pos) {
5769 ConstString name(enum_pos->getNameAsString().c_str());
5770 if (!callback(integer_type, name, enum_pos->getInitVal()))
5771 break;
5772 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005773 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005774 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005775}
5776
Greg Claytond8d4a572015-08-11 21:38:15 +00005777#pragma mark Aggregate Types
5778
Kate Stoneb9c1b512016-09-06 20:57:50 +00005779uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5780 if (!type)
5781 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005782
Kate Stoneb9c1b512016-09-06 20:57:50 +00005783 uint32_t count = 0;
5784 clang::QualType qual_type(GetCanonicalQualType(type));
5785 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5786 switch (type_class) {
5787 case clang::Type::Record:
5788 if (GetCompleteType(type)) {
5789 const clang::RecordType *record_type =
5790 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5791 if (record_type) {
5792 clang::RecordDecl *record_decl = record_type->getDecl();
5793 if (record_decl) {
5794 uint32_t field_idx = 0;
5795 clang::RecordDecl::field_iterator field, field_end;
5796 for (field = record_decl->field_begin(),
5797 field_end = record_decl->field_end();
5798 field != field_end; ++field)
5799 ++field_idx;
5800 count = field_idx;
5801 }
5802 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005803 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005804 break;
5805
5806 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00005807 count = CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
5808 ->getDecl()
5809 ->getUnderlyingType()
5810 .getAsOpaquePtr())
5811 .GetNumFields();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005812 break;
5813
5814 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00005815 count = CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
5816 ->getDeducedType()
5817 .getAsOpaquePtr())
5818 .GetNumFields();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005819 break;
5820
5821 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00005822 count = CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
5823 ->getNamedType()
5824 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005825 .GetNumFields();
5826 break;
5827
5828 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00005829 count =
5830 CompilerType(
5831 this,
5832 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())
5833 .GetNumFields();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005834 break;
5835
Sean Callananf9c622a2016-09-30 18:44:43 +00005836 case clang::Type::ObjCObjectPointer: {
5837 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005838 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005839 const clang::ObjCInterfaceType *objc_interface_type =
5840 objc_class_type->getInterfaceType();
5841 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005842 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5843 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005844 clang::ObjCInterfaceDecl *class_interface_decl =
5845 objc_interface_type->getDecl();
5846 if (class_interface_decl) {
5847 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005848 }
5849 }
5850 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005851 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005852
5853 case clang::Type::ObjCObject:
5854 case clang::Type::ObjCInterface:
5855 if (GetCompleteType(type)) {
5856 const clang::ObjCObjectType *objc_class_type =
5857 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5858 if (objc_class_type) {
5859 clang::ObjCInterfaceDecl *class_interface_decl =
5860 objc_class_type->getInterface();
5861
5862 if (class_interface_decl)
5863 count = class_interface_decl->ivar_size();
5864 }
5865 }
5866 break;
5867
5868 default:
5869 break;
5870 }
5871 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005872}
5873
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005874static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005875GetObjCFieldAtIndex(clang::ASTContext *ast,
5876 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5877 std::string &name, uint64_t *bit_offset_ptr,
5878 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5879 if (class_interface_decl) {
5880 if (idx < (class_interface_decl->ivar_size())) {
5881 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5882 ivar_end = class_interface_decl->ivar_end();
5883 uint32_t ivar_idx = 0;
5884
5885 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5886 ++ivar_pos, ++ivar_idx) {
5887 if (ivar_idx == idx) {
5888 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5889
5890 clang::QualType ivar_qual_type(ivar_decl->getType());
5891
5892 name.assign(ivar_decl->getNameAsString());
5893
5894 if (bit_offset_ptr) {
5895 const clang::ASTRecordLayout &interface_layout =
5896 ast->getASTObjCInterfaceLayout(class_interface_decl);
5897 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5898 }
5899
5900 const bool is_bitfield = ivar_pos->isBitField();
5901
5902 if (bitfield_bit_size_ptr) {
5903 *bitfield_bit_size_ptr = 0;
5904
5905 if (is_bitfield && ast) {
5906 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00005907 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005908 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00005909 bitfield_bit_size_expr->EvaluateAsInt(result, *ast)) {
5910 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005911 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5912 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005913 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005914 }
5915 if (is_bitfield_ptr)
5916 *is_bitfield_ptr = is_bitfield;
5917
5918 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005919 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005920 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005921 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005922 }
5923 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005924}
5925
Kate Stoneb9c1b512016-09-06 20:57:50 +00005926CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5927 size_t idx, std::string &name,
5928 uint64_t *bit_offset_ptr,
5929 uint32_t *bitfield_bit_size_ptr,
5930 bool *is_bitfield_ptr) {
5931 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005932 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005933
5934 clang::QualType qual_type(GetCanonicalQualType(type));
5935 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5936 switch (type_class) {
5937 case clang::Type::Record:
5938 if (GetCompleteType(type)) {
5939 const clang::RecordType *record_type =
5940 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5941 const clang::RecordDecl *record_decl = record_type->getDecl();
5942 uint32_t field_idx = 0;
5943 clang::RecordDecl::field_iterator field, field_end;
5944 for (field = record_decl->field_begin(),
5945 field_end = record_decl->field_end();
5946 field != field_end; ++field, ++field_idx) {
5947 if (idx == field_idx) {
5948 // Print the member type if requested
5949 // Print the member name and equal sign
5950 name.assign(field->getNameAsString());
5951
5952 // Figure out the type byte size (field_type_info.first) and
5953 // alignment (field_type_info.second) from the AST context.
5954 if (bit_offset_ptr) {
5955 const clang::ASTRecordLayout &record_layout =
5956 getASTContext()->getASTRecordLayout(record_decl);
5957 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5958 }
5959
5960 const bool is_bitfield = field->isBitField();
5961
5962 if (bitfield_bit_size_ptr) {
5963 *bitfield_bit_size_ptr = 0;
5964
5965 if (is_bitfield) {
5966 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00005967 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005968 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00005969 bitfield_bit_size_expr->EvaluateAsInt(result,
Kate Stoneb9c1b512016-09-06 20:57:50 +00005970 *getASTContext())) {
Hans Wennborg30ce9622018-11-28 14:30:18 +00005971 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005972 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5973 }
5974 }
5975 }
5976 if (is_bitfield_ptr)
5977 *is_bitfield_ptr = is_bitfield;
5978
Alex Langfordbddab072019-08-13 19:40:36 +00005979 return CompilerType(this, field->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00005980 }
5981 }
5982 }
5983 break;
5984
Sean Callananf9c622a2016-09-30 18:44:43 +00005985 case clang::Type::ObjCObjectPointer: {
5986 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005987 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005988 const clang::ObjCInterfaceType *objc_interface_type =
5989 objc_class_type->getInterfaceType();
5990 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005991 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5992 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005993 clang::ObjCInterfaceDecl *class_interface_decl =
5994 objc_interface_type->getDecl();
5995 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005996 return CompilerType(
5997 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5998 idx, name, bit_offset_ptr,
5999 bitfield_bit_size_ptr, is_bitfield_ptr));
6000 }
6001 }
6002 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00006003 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006004
6005 case clang::Type::ObjCObject:
6006 case clang::Type::ObjCInterface:
6007 if (GetCompleteType(type)) {
6008 const clang::ObjCObjectType *objc_class_type =
6009 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6010 assert(objc_class_type);
6011 if (objc_class_type) {
6012 clang::ObjCInterfaceDecl *class_interface_decl =
6013 objc_class_type->getInterface();
6014 return CompilerType(
6015 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6016 idx, name, bit_offset_ptr,
6017 bitfield_bit_size_ptr, is_bitfield_ptr));
6018 }
6019 }
6020 break;
6021
6022 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00006023 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
6024 ->getDecl()
6025 ->getUnderlyingType()
6026 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00006027 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6028 is_bitfield_ptr);
6029
6030 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00006031 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
6032 ->getDeducedType()
6033 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00006034 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6035 is_bitfield_ptr);
6036
6037 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00006038 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
6039 ->getNamedType()
6040 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00006041 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6042 is_bitfield_ptr);
6043
6044 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00006045 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
6046 ->desugar()
6047 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00006048 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6049 is_bitfield_ptr);
6050
6051 default:
6052 break;
6053 }
6054 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006055}
6056
Greg Clayton99558cc42015-08-24 23:46:31 +00006057uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006058ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
6059 uint32_t count = 0;
6060 clang::QualType qual_type(GetCanonicalQualType(type));
6061 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6062 switch (type_class) {
6063 case clang::Type::Record:
6064 if (GetCompleteType(type)) {
6065 const clang::CXXRecordDecl *cxx_record_decl =
6066 qual_type->getAsCXXRecordDecl();
6067 if (cxx_record_decl)
6068 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006069 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006070 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006071
Kate Stoneb9c1b512016-09-06 20:57:50 +00006072 case clang::Type::ObjCObjectPointer:
6073 count = GetPointeeType(type).GetNumDirectBaseClasses();
6074 break;
6075
6076 case clang::Type::ObjCObject:
6077 if (GetCompleteType(type)) {
6078 const clang::ObjCObjectType *objc_class_type =
6079 qual_type->getAsObjCQualifiedInterfaceType();
6080 if (objc_class_type) {
6081 clang::ObjCInterfaceDecl *class_interface_decl =
6082 objc_class_type->getInterface();
6083
6084 if (class_interface_decl && class_interface_decl->getSuperClass())
6085 count = 1;
6086 }
6087 }
6088 break;
6089 case clang::Type::ObjCInterface:
6090 if (GetCompleteType(type)) {
6091 const clang::ObjCInterfaceType *objc_interface_type =
6092 qual_type->getAs<clang::ObjCInterfaceType>();
6093 if (objc_interface_type) {
6094 clang::ObjCInterfaceDecl *class_interface_decl =
6095 objc_interface_type->getInterface();
6096
6097 if (class_interface_decl && class_interface_decl->getSuperClass())
6098 count = 1;
6099 }
6100 }
6101 break;
6102
6103 case clang::Type::Typedef:
6104 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6105 ->getDecl()
6106 ->getUnderlyingType()
6107 .getAsOpaquePtr());
6108 break;
6109
6110 case clang::Type::Auto:
6111 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6112 ->getDeducedType()
6113 .getAsOpaquePtr());
6114 break;
6115
6116 case clang::Type::Elaborated:
6117 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6118 ->getNamedType()
6119 .getAsOpaquePtr());
6120 break;
6121
6122 case clang::Type::Paren:
6123 return GetNumDirectBaseClasses(
6124 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6125
6126 default:
6127 break;
6128 }
6129 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006130}
6131
6132uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006133ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
6134 uint32_t count = 0;
6135 clang::QualType qual_type(GetCanonicalQualType(type));
6136 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6137 switch (type_class) {
6138 case clang::Type::Record:
6139 if (GetCompleteType(type)) {
6140 const clang::CXXRecordDecl *cxx_record_decl =
6141 qual_type->getAsCXXRecordDecl();
6142 if (cxx_record_decl)
6143 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006144 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006145 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006146
Kate Stoneb9c1b512016-09-06 20:57:50 +00006147 case clang::Type::Typedef:
6148 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6149 ->getDecl()
6150 ->getUnderlyingType()
6151 .getAsOpaquePtr());
6152 break;
6153
6154 case clang::Type::Auto:
6155 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6156 ->getDeducedType()
6157 .getAsOpaquePtr());
6158 break;
6159
6160 case clang::Type::Elaborated:
6161 count =
6162 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6163 ->getNamedType()
6164 .getAsOpaquePtr());
6165 break;
6166
6167 case clang::Type::Paren:
6168 count = GetNumVirtualBaseClasses(
6169 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6170 break;
6171
6172 default:
6173 break;
6174 }
6175 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006176}
6177
Kate Stoneb9c1b512016-09-06 20:57:50 +00006178CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6179 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6180 clang::QualType qual_type(GetCanonicalQualType(type));
6181 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6182 switch (type_class) {
6183 case clang::Type::Record:
6184 if (GetCompleteType(type)) {
6185 const clang::CXXRecordDecl *cxx_record_decl =
6186 qual_type->getAsCXXRecordDecl();
6187 if (cxx_record_decl) {
6188 uint32_t curr_idx = 0;
6189 clang::CXXRecordDecl::base_class_const_iterator base_class,
6190 base_class_end;
6191 for (base_class = cxx_record_decl->bases_begin(),
6192 base_class_end = cxx_record_decl->bases_end();
6193 base_class != base_class_end; ++base_class, ++curr_idx) {
6194 if (curr_idx == idx) {
6195 if (bit_offset_ptr) {
6196 const clang::ASTRecordLayout &record_layout =
6197 getASTContext()->getASTRecordLayout(cxx_record_decl);
6198 const clang::CXXRecordDecl *base_class_decl =
6199 llvm::cast<clang::CXXRecordDecl>(
6200 base_class->getType()
6201 ->getAs<clang::RecordType>()
6202 ->getDecl());
6203 if (base_class->isVirtual())
6204 *bit_offset_ptr =
6205 record_layout.getVBaseClassOffset(base_class_decl)
6206 .getQuantity() *
6207 8;
6208 else
6209 *bit_offset_ptr =
6210 record_layout.getBaseClassOffset(base_class_decl)
6211 .getQuantity() *
6212 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006213 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006214 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6215 }
6216 }
6217 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006218 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006219 break;
6220
6221 case clang::Type::ObjCObjectPointer:
6222 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6223
6224 case clang::Type::ObjCObject:
6225 if (idx == 0 && GetCompleteType(type)) {
6226 const clang::ObjCObjectType *objc_class_type =
6227 qual_type->getAsObjCQualifiedInterfaceType();
6228 if (objc_class_type) {
6229 clang::ObjCInterfaceDecl *class_interface_decl =
6230 objc_class_type->getInterface();
6231
6232 if (class_interface_decl) {
6233 clang::ObjCInterfaceDecl *superclass_interface_decl =
6234 class_interface_decl->getSuperClass();
6235 if (superclass_interface_decl) {
6236 if (bit_offset_ptr)
6237 *bit_offset_ptr = 0;
Alex Langfordbddab072019-08-13 19:40:36 +00006238 return CompilerType(this,
Kate Stoneb9c1b512016-09-06 20:57:50 +00006239 getASTContext()->getObjCInterfaceType(
Alex Langfordbddab072019-08-13 19:40:36 +00006240 superclass_interface_decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006241 }
6242 }
6243 }
6244 }
6245 break;
6246 case clang::Type::ObjCInterface:
6247 if (idx == 0 && GetCompleteType(type)) {
6248 const clang::ObjCObjectType *objc_interface_type =
6249 qual_type->getAs<clang::ObjCInterfaceType>();
6250 if (objc_interface_type) {
6251 clang::ObjCInterfaceDecl *class_interface_decl =
6252 objc_interface_type->getInterface();
6253
6254 if (class_interface_decl) {
6255 clang::ObjCInterfaceDecl *superclass_interface_decl =
6256 class_interface_decl->getSuperClass();
6257 if (superclass_interface_decl) {
6258 if (bit_offset_ptr)
6259 *bit_offset_ptr = 0;
Alex Langfordbddab072019-08-13 19:40:36 +00006260 return CompilerType(
6261 this, getASTContext()
6262 ->getObjCInterfaceType(superclass_interface_decl)
6263 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006264 }
6265 }
6266 }
6267 }
6268 break;
6269
6270 case clang::Type::Typedef:
6271 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6272 ->getDecl()
6273 ->getUnderlyingType()
6274 .getAsOpaquePtr(),
6275 idx, bit_offset_ptr);
6276
6277 case clang::Type::Auto:
6278 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6279 ->getDeducedType()
6280 .getAsOpaquePtr(),
6281 idx, bit_offset_ptr);
6282
6283 case clang::Type::Elaborated:
6284 return GetDirectBaseClassAtIndex(
6285 llvm::cast<clang::ElaboratedType>(qual_type)
6286 ->getNamedType()
6287 .getAsOpaquePtr(),
6288 idx, bit_offset_ptr);
6289
6290 case clang::Type::Paren:
6291 return GetDirectBaseClassAtIndex(
6292 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6293 idx, bit_offset_ptr);
6294
6295 default:
6296 break;
6297 }
6298 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006299}
6300
Kate Stoneb9c1b512016-09-06 20:57:50 +00006301CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6302 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6303 clang::QualType qual_type(GetCanonicalQualType(type));
6304 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6305 switch (type_class) {
6306 case clang::Type::Record:
6307 if (GetCompleteType(type)) {
6308 const clang::CXXRecordDecl *cxx_record_decl =
6309 qual_type->getAsCXXRecordDecl();
6310 if (cxx_record_decl) {
6311 uint32_t curr_idx = 0;
6312 clang::CXXRecordDecl::base_class_const_iterator base_class,
6313 base_class_end;
6314 for (base_class = cxx_record_decl->vbases_begin(),
6315 base_class_end = cxx_record_decl->vbases_end();
6316 base_class != base_class_end; ++base_class, ++curr_idx) {
6317 if (curr_idx == idx) {
6318 if (bit_offset_ptr) {
6319 const clang::ASTRecordLayout &record_layout =
6320 getASTContext()->getASTRecordLayout(cxx_record_decl);
6321 const clang::CXXRecordDecl *base_class_decl =
6322 llvm::cast<clang::CXXRecordDecl>(
6323 base_class->getType()
6324 ->getAs<clang::RecordType>()
6325 ->getDecl());
6326 *bit_offset_ptr =
6327 record_layout.getVBaseClassOffset(base_class_decl)
6328 .getQuantity() *
6329 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006330 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006331 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6332 }
6333 }
6334 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006335 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006336 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006337
Kate Stoneb9c1b512016-09-06 20:57:50 +00006338 case clang::Type::Typedef:
6339 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6340 ->getDecl()
6341 ->getUnderlyingType()
6342 .getAsOpaquePtr(),
6343 idx, bit_offset_ptr);
6344
6345 case clang::Type::Auto:
6346 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6347 ->getDeducedType()
6348 .getAsOpaquePtr(),
6349 idx, bit_offset_ptr);
6350
6351 case clang::Type::Elaborated:
6352 return GetVirtualBaseClassAtIndex(
6353 llvm::cast<clang::ElaboratedType>(qual_type)
6354 ->getNamedType()
6355 .getAsOpaquePtr(),
6356 idx, bit_offset_ptr);
6357
6358 case clang::Type::Paren:
6359 return GetVirtualBaseClassAtIndex(
6360 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6361 idx, bit_offset_ptr);
6362
6363 default:
6364 break;
6365 }
6366 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006367}
6368
Greg Claytond8d4a572015-08-11 21:38:15 +00006369// If a pointer to a pointee type (the clang_type arg) says that it has no
6370// children, then we either need to trust it, or override it and return a
6371// different result. For example, an "int *" has one child that is an integer,
6372// but a function pointer doesn't have any children. Likewise if a Record type
6373// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006374uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6375 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006376 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006377
6378 clang::QualType qual_type(type.getCanonicalType());
6379 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6380 switch (type_class) {
6381 case clang::Type::Builtin:
6382 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6383 case clang::BuiltinType::UnknownAny:
6384 case clang::BuiltinType::Void:
6385 case clang::BuiltinType::NullPtr:
6386 case clang::BuiltinType::OCLEvent:
6387 case clang::BuiltinType::OCLImage1dRO:
6388 case clang::BuiltinType::OCLImage1dWO:
6389 case clang::BuiltinType::OCLImage1dRW:
6390 case clang::BuiltinType::OCLImage1dArrayRO:
6391 case clang::BuiltinType::OCLImage1dArrayWO:
6392 case clang::BuiltinType::OCLImage1dArrayRW:
6393 case clang::BuiltinType::OCLImage1dBufferRO:
6394 case clang::BuiltinType::OCLImage1dBufferWO:
6395 case clang::BuiltinType::OCLImage1dBufferRW:
6396 case clang::BuiltinType::OCLImage2dRO:
6397 case clang::BuiltinType::OCLImage2dWO:
6398 case clang::BuiltinType::OCLImage2dRW:
6399 case clang::BuiltinType::OCLImage2dArrayRO:
6400 case clang::BuiltinType::OCLImage2dArrayWO:
6401 case clang::BuiltinType::OCLImage2dArrayRW:
6402 case clang::BuiltinType::OCLImage3dRO:
6403 case clang::BuiltinType::OCLImage3dWO:
6404 case clang::BuiltinType::OCLImage3dRW:
6405 case clang::BuiltinType::OCLSampler:
6406 return 0;
6407 case clang::BuiltinType::Bool:
6408 case clang::BuiltinType::Char_U:
6409 case clang::BuiltinType::UChar:
6410 case clang::BuiltinType::WChar_U:
6411 case clang::BuiltinType::Char16:
6412 case clang::BuiltinType::Char32:
6413 case clang::BuiltinType::UShort:
6414 case clang::BuiltinType::UInt:
6415 case clang::BuiltinType::ULong:
6416 case clang::BuiltinType::ULongLong:
6417 case clang::BuiltinType::UInt128:
6418 case clang::BuiltinType::Char_S:
6419 case clang::BuiltinType::SChar:
6420 case clang::BuiltinType::WChar_S:
6421 case clang::BuiltinType::Short:
6422 case clang::BuiltinType::Int:
6423 case clang::BuiltinType::Long:
6424 case clang::BuiltinType::LongLong:
6425 case clang::BuiltinType::Int128:
6426 case clang::BuiltinType::Float:
6427 case clang::BuiltinType::Double:
6428 case clang::BuiltinType::LongDouble:
6429 case clang::BuiltinType::Dependent:
6430 case clang::BuiltinType::Overload:
6431 case clang::BuiltinType::ObjCId:
6432 case clang::BuiltinType::ObjCClass:
6433 case clang::BuiltinType::ObjCSel:
6434 case clang::BuiltinType::BoundMember:
6435 case clang::BuiltinType::Half:
6436 case clang::BuiltinType::ARCUnbridgedCast:
6437 case clang::BuiltinType::PseudoObject:
6438 case clang::BuiltinType::BuiltinFn:
6439 case clang::BuiltinType::OMPArraySection:
6440 return 1;
6441 default:
6442 return 0;
6443 }
6444 break;
6445
6446 case clang::Type::Complex:
6447 return 1;
6448 case clang::Type::Pointer:
6449 return 1;
6450 case clang::Type::BlockPointer:
6451 return 0; // If block pointers don't have debug info, then no children for
6452 // them
6453 case clang::Type::LValueReference:
6454 return 1;
6455 case clang::Type::RValueReference:
6456 return 1;
6457 case clang::Type::MemberPointer:
6458 return 0;
6459 case clang::Type::ConstantArray:
6460 return 0;
6461 case clang::Type::IncompleteArray:
6462 return 0;
6463 case clang::Type::VariableArray:
6464 return 0;
6465 case clang::Type::DependentSizedArray:
6466 return 0;
6467 case clang::Type::DependentSizedExtVector:
6468 return 0;
6469 case clang::Type::Vector:
6470 return 0;
6471 case clang::Type::ExtVector:
6472 return 0;
6473 case clang::Type::FunctionProto:
6474 return 0; // When we function pointers, they have no children...
6475 case clang::Type::FunctionNoProto:
6476 return 0; // When we function pointers, they have no children...
6477 case clang::Type::UnresolvedUsing:
6478 return 0;
6479 case clang::Type::Paren:
6480 return GetNumPointeeChildren(
6481 llvm::cast<clang::ParenType>(qual_type)->desugar());
6482 case clang::Type::Typedef:
6483 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6484 ->getDecl()
6485 ->getUnderlyingType());
6486 case clang::Type::Auto:
6487 return GetNumPointeeChildren(
6488 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6489 case clang::Type::Elaborated:
6490 return GetNumPointeeChildren(
6491 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6492 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006493 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6494 ->getUnderlyingExpr()
6495 ->getType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006496 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006497 return GetNumPointeeChildren(
6498 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006499 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006500 return GetNumPointeeChildren(
6501 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006502 case clang::Type::Record:
6503 return 0;
6504 case clang::Type::Enum:
6505 return 1;
6506 case clang::Type::TemplateTypeParm:
6507 return 1;
6508 case clang::Type::SubstTemplateTypeParm:
6509 return 1;
6510 case clang::Type::TemplateSpecialization:
6511 return 1;
6512 case clang::Type::InjectedClassName:
6513 return 0;
6514 case clang::Type::DependentName:
6515 return 1;
6516 case clang::Type::DependentTemplateSpecialization:
6517 return 1;
6518 case clang::Type::ObjCObject:
6519 return 0;
6520 case clang::Type::ObjCInterface:
6521 return 0;
6522 case clang::Type::ObjCObjectPointer:
6523 return 1;
6524 default:
6525 break;
6526 }
6527 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006528}
6529
Kate Stoneb9c1b512016-09-06 20:57:50 +00006530CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6531 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6532 bool transparent_pointers, bool omit_empty_base_classes,
6533 bool ignore_array_bounds, std::string &child_name,
6534 uint32_t &child_byte_size, int32_t &child_byte_offset,
6535 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6536 bool &child_is_base_class, bool &child_is_deref_of_parent,
6537 ValueObject *valobj, uint64_t &language_flags) {
6538 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006539 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006540
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006541 auto get_exe_scope = [&exe_ctx]() {
6542 return exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr;
6543 };
6544
Kate Stoneb9c1b512016-09-06 20:57:50 +00006545 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6546 const clang::Type::TypeClass parent_type_class =
6547 parent_qual_type->getTypeClass();
6548 child_bitfield_bit_size = 0;
6549 child_bitfield_bit_offset = 0;
6550 child_is_base_class = false;
6551 language_flags = 0;
6552
Adrian Prantleca07c52018-11-05 20:49:07 +00006553 const bool idx_is_valid =
6554 idx < GetNumChildren(type, omit_empty_base_classes, exe_ctx);
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006555 int32_t bit_offset;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006556 switch (parent_type_class) {
6557 case clang::Type::Builtin:
6558 if (idx_is_valid) {
6559 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6560 case clang::BuiltinType::ObjCId:
6561 case clang::BuiltinType::ObjCClass:
6562 child_name = "isa";
6563 child_byte_size =
6564 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6565 CHAR_BIT;
Alex Langfordbddab072019-08-13 19:40:36 +00006566 return CompilerType(
6567 this, getASTContext()->ObjCBuiltinClassTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006568
6569 default:
6570 break;
6571 }
6572 }
6573 break;
6574
6575 case clang::Type::Record:
6576 if (idx_is_valid && GetCompleteType(type)) {
6577 const clang::RecordType *record_type =
6578 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6579 const clang::RecordDecl *record_decl = record_type->getDecl();
6580 assert(record_decl);
6581 const clang::ASTRecordLayout &record_layout =
6582 getASTContext()->getASTRecordLayout(record_decl);
6583 uint32_t child_idx = 0;
6584
6585 const clang::CXXRecordDecl *cxx_record_decl =
6586 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6587 if (cxx_record_decl) {
6588 // We might have base classes to print out first
6589 clang::CXXRecordDecl::base_class_const_iterator base_class,
6590 base_class_end;
6591 for (base_class = cxx_record_decl->bases_begin(),
6592 base_class_end = cxx_record_decl->bases_end();
6593 base_class != base_class_end; ++base_class) {
6594 const clang::CXXRecordDecl *base_class_decl = nullptr;
6595
6596 // Skip empty base classes
6597 if (omit_empty_base_classes) {
6598 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6599 base_class->getType()->getAs<clang::RecordType>()->getDecl());
Jonas Devliegherea6682a42018-12-15 00:15:33 +00006600 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00006601 continue;
6602 }
6603
6604 if (idx == child_idx) {
6605 if (base_class_decl == nullptr)
6606 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6607 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6608
6609 if (base_class->isVirtual()) {
6610 bool handled = false;
6611 if (valobj) {
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +00006612 clang::VTableContextBase *vtable_ctx =
6613 getASTContext()->getVTableContext();
6614 if (vtable_ctx)
6615 handled = GetVBaseBitOffset(*vtable_ctx, *valobj,
6616 record_layout, cxx_record_decl,
6617 base_class_decl, bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006618 }
6619 if (!handled)
6620 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6621 .getQuantity() *
6622 8;
6623 } else
6624 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6625 .getQuantity() *
6626 8;
6627
6628 // Base classes should be a multiple of 8 bits in size
6629 child_byte_offset = bit_offset / 8;
Alex Langfordbddab072019-08-13 19:40:36 +00006630 CompilerType base_class_clang_type(
6631 this, base_class->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006632 child_name = base_class_clang_type.GetTypeName().AsCString("");
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006633 Optional<uint64_t> size =
6634 base_class_clang_type.GetBitSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006635 if (!size)
6636 return {};
6637 uint64_t base_class_clang_type_bit_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006638
6639 // Base classes bit sizes should be a multiple of 8 bits in size
6640 assert(base_class_clang_type_bit_size % 8 == 0);
6641 child_byte_size = base_class_clang_type_bit_size / 8;
6642 child_is_base_class = true;
6643 return base_class_clang_type;
6644 }
6645 // We don't increment the child index in the for loop since we might
6646 // be skipping empty base classes
6647 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006648 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006649 }
6650 // Make sure index is in range...
6651 uint32_t field_idx = 0;
6652 clang::RecordDecl::field_iterator field, field_end;
6653 for (field = record_decl->field_begin(),
6654 field_end = record_decl->field_end();
6655 field != field_end; ++field, ++field_idx, ++child_idx) {
6656 if (idx == child_idx) {
6657 // Print the member type if requested
6658 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006659 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006660
6661 // Figure out the type byte size (field_type_info.first) and
6662 // alignment (field_type_info.second) from the AST context.
Alex Langfordbddab072019-08-13 19:40:36 +00006663 CompilerType field_clang_type(this,
6664 field->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006665 assert(field_idx < record_layout.getFieldCount());
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006666 Optional<uint64_t> size =
6667 field_clang_type.GetByteSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006668 if (!size)
6669 return {};
6670 child_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006671 const uint32_t child_bit_size = child_byte_size * 8;
6672
6673 // Figure out the field offset within the current struct/union/class
6674 // type
6675 bit_offset = record_layout.getFieldOffset(field_idx);
Raphael Isemann02e91132019-11-20 12:09:19 +01006676 if (FieldIsBitfield(*field, child_bitfield_bit_size)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006677 child_bitfield_bit_offset = bit_offset % child_bit_size;
6678 const uint32_t child_bit_offset =
6679 bit_offset - child_bitfield_bit_offset;
6680 child_byte_offset = child_bit_offset / 8;
6681 } else {
6682 child_byte_offset = bit_offset / 8;
6683 }
6684
6685 return field_clang_type;
6686 }
6687 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006688 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006689 break;
6690
6691 case clang::Type::ObjCObject:
6692 case clang::Type::ObjCInterface:
6693 if (idx_is_valid && GetCompleteType(type)) {
6694 const clang::ObjCObjectType *objc_class_type =
6695 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6696 assert(objc_class_type);
6697 if (objc_class_type) {
6698 uint32_t child_idx = 0;
6699 clang::ObjCInterfaceDecl *class_interface_decl =
6700 objc_class_type->getInterface();
6701
6702 if (class_interface_decl) {
6703
6704 const clang::ASTRecordLayout &interface_layout =
6705 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6706 clang::ObjCInterfaceDecl *superclass_interface_decl =
6707 class_interface_decl->getSuperClass();
6708 if (superclass_interface_decl) {
6709 if (omit_empty_base_classes) {
6710 CompilerType base_class_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00006711 this, getASTContext()
6712 ->getObjCInterfaceType(superclass_interface_decl)
6713 .getAsOpaquePtr());
Adrian Prantleca07c52018-11-05 20:49:07 +00006714 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes,
6715 exe_ctx) > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006716 if (idx == 0) {
6717 clang::QualType ivar_qual_type(
6718 getASTContext()->getObjCInterfaceType(
6719 superclass_interface_decl));
6720
6721 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006722 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006723
6724 clang::TypeInfo ivar_type_info =
6725 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6726
6727 child_byte_size = ivar_type_info.Width / 8;
6728 child_byte_offset = 0;
6729 child_is_base_class = true;
6730
Alex Langfordbddab072019-08-13 19:40:36 +00006731 return CompilerType(this, ivar_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006732 }
6733
6734 ++child_idx;
6735 }
6736 } else
6737 ++child_idx;
6738 }
6739
6740 const uint32_t superclass_idx = child_idx;
6741
6742 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6743 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6744 ivar_end = class_interface_decl->ivar_end();
6745
6746 for (ivar_pos = class_interface_decl->ivar_begin();
6747 ivar_pos != ivar_end; ++ivar_pos) {
6748 if (child_idx == idx) {
6749 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6750
6751 clang::QualType ivar_qual_type(ivar_decl->getType());
6752
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006753 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006754
6755 clang::TypeInfo ivar_type_info =
6756 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6757
6758 child_byte_size = ivar_type_info.Width / 8;
6759
6760 // Figure out the field offset within the current
Adrian Prantl05097242018-04-30 16:49:04 +00006761 // struct/union/class type For ObjC objects, we can't trust the
6762 // bit offset we get from the Clang AST, since that doesn't
6763 // account for the space taken up by unbacked properties, or
6764 // from the changing size of base classes that are newer than
6765 // this class. So if we have a process around that we can ask
6766 // about this object, do so.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006767 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6768 Process *process = nullptr;
6769 if (exe_ctx)
6770 process = exe_ctx->GetProcessPtr();
6771 if (process) {
6772 ObjCLanguageRuntime *objc_runtime =
Alex Langforde823bbe2019-06-10 20:53:23 +00006773 ObjCLanguageRuntime::Get(*process);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006774 if (objc_runtime != nullptr) {
Alex Langfordbddab072019-08-13 19:40:36 +00006775 CompilerType parent_ast_type(
6776 this, parent_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006777 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6778 parent_ast_type, ivar_decl->getNameAsString().c_str());
6779 }
6780 }
6781
Aleksandr Urakovff701722018-08-20 05:59:27 +00006782 // Setting this to INT32_MAX to make sure we don't compute it
Kate Stoneb9c1b512016-09-06 20:57:50 +00006783 // twice...
Aleksandr Urakov53459482018-08-17 07:28:24 +00006784 bit_offset = INT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006785
6786 if (child_byte_offset ==
6787 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6788 bit_offset = interface_layout.getFieldOffset(child_idx -
6789 superclass_idx);
6790 child_byte_offset = bit_offset / 8;
6791 }
6792
6793 // Note, the ObjC Ivar Byte offset is just that, it doesn't
Adrian Prantl05097242018-04-30 16:49:04 +00006794 // account for the bit offset of a bitfield within its
6795 // containing object. So regardless of where we get the byte
Kate Stoneb9c1b512016-09-06 20:57:50 +00006796 // offset from, we still need to get the bit offset for
6797 // bitfields from the layout.
6798
Raphael Isemann02e91132019-11-20 12:09:19 +01006799 if (FieldIsBitfield(ivar_decl, child_bitfield_bit_size)) {
Aleksandr Urakov53459482018-08-17 07:28:24 +00006800 if (bit_offset == INT32_MAX)
Kate Stoneb9c1b512016-09-06 20:57:50 +00006801 bit_offset = interface_layout.getFieldOffset(
6802 child_idx - superclass_idx);
6803
6804 child_bitfield_bit_offset = bit_offset % 8;
6805 }
Alex Langfordbddab072019-08-13 19:40:36 +00006806 return CompilerType(this, ivar_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006807 }
6808 ++child_idx;
6809 }
6810 }
6811 }
6812 }
6813 }
6814 break;
6815
6816 case clang::Type::ObjCObjectPointer:
6817 if (idx_is_valid) {
6818 CompilerType pointee_clang_type(GetPointeeType(type));
6819
6820 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6821 child_is_deref_of_parent = false;
6822 bool tmp_child_is_deref_of_parent = false;
6823 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6824 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6825 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6826 child_bitfield_bit_size, child_bitfield_bit_offset,
6827 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6828 language_flags);
6829 } else {
6830 child_is_deref_of_parent = true;
6831 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00006832 valobj ? valobj->GetName().GetCString() : nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006833 if (parent_name) {
6834 child_name.assign(1, '*');
6835 child_name += parent_name;
6836 }
6837
6838 // We have a pointer to an simple type
6839 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006840 if (Optional<uint64_t> size =
6841 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006842 child_byte_size = *size;
6843 child_byte_offset = 0;
6844 return pointee_clang_type;
6845 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006846 }
6847 }
6848 }
6849 break;
6850
6851 case clang::Type::Vector:
6852 case clang::Type::ExtVector:
6853 if (idx_is_valid) {
6854 const clang::VectorType *array =
6855 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6856 if (array) {
Alex Langfordbddab072019-08-13 19:40:36 +00006857 CompilerType element_type(this,
6858 array->getElementType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006859 if (element_type.GetCompleteType()) {
6860 char element_name[64];
6861 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6862 static_cast<uint64_t>(idx));
6863 child_name.assign(element_name);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006864 if (Optional<uint64_t> size =
6865 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006866 child_byte_size = *size;
6867 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6868 return element_type;
6869 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006870 }
6871 }
6872 }
6873 break;
6874
6875 case clang::Type::ConstantArray:
6876 case clang::Type::IncompleteArray:
6877 if (ignore_array_bounds || idx_is_valid) {
6878 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6879 if (array) {
Alex Langfordbddab072019-08-13 19:40:36 +00006880 CompilerType element_type(this,
6881 array->getElementType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006882 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006883 child_name = llvm::formatv("[{0}]", idx);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006884 if (Optional<uint64_t> size =
6885 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006886 child_byte_size = *size;
6887 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6888 return element_type;
6889 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006890 }
6891 }
6892 }
6893 break;
6894
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006895 case clang::Type::Pointer: {
6896 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006897
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006898 // Don't dereference "void *" pointers
6899 if (pointee_clang_type.IsVoidType())
6900 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006901
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006902 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6903 child_is_deref_of_parent = false;
6904 bool tmp_child_is_deref_of_parent = false;
6905 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6906 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6907 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6908 child_bitfield_bit_size, child_bitfield_bit_offset,
6909 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6910 language_flags);
6911 } else {
6912 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006913
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006914 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00006915 valobj ? valobj->GetName().GetCString() : nullptr;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006916 if (parent_name) {
6917 child_name.assign(1, '*');
6918 child_name += parent_name;
6919 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006920
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006921 // We have a pointer to an simple type
6922 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006923 if (Optional<uint64_t> size =
6924 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006925 child_byte_size = *size;
6926 child_byte_offset = 0;
6927 return pointee_clang_type;
6928 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006929 }
6930 }
6931 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006932 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006933
6934 case clang::Type::LValueReference:
6935 case clang::Type::RValueReference:
6936 if (idx_is_valid) {
6937 const clang::ReferenceType *reference_type =
6938 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
Alex Langfordbddab072019-08-13 19:40:36 +00006939 CompilerType pointee_clang_type(
6940 this, reference_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006941 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6942 child_is_deref_of_parent = false;
6943 bool tmp_child_is_deref_of_parent = false;
6944 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6945 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6946 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6947 child_bitfield_bit_size, child_bitfield_bit_offset,
6948 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6949 language_flags);
6950 } else {
6951 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00006952 valobj ? valobj->GetName().GetCString() : nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006953 if (parent_name) {
6954 child_name.assign(1, '&');
6955 child_name += parent_name;
6956 }
6957
6958 // We have a pointer to an simple type
6959 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006960 if (Optional<uint64_t> size =
6961 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006962 child_byte_size = *size;
6963 child_byte_offset = 0;
6964 return pointee_clang_type;
6965 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006966 }
6967 }
6968 }
6969 break;
6970
6971 case clang::Type::Typedef: {
6972 CompilerType typedefed_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00006973 this, llvm::cast<clang::TypedefType>(parent_qual_type)
6974 ->getDecl()
6975 ->getUnderlyingType()
6976 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006977 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
6978 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6979 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6980 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6981 child_is_deref_of_parent, valobj, language_flags);
6982 } break;
6983
6984 case clang::Type::Auto: {
6985 CompilerType elaborated_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00006986 this, llvm::cast<clang::AutoType>(parent_qual_type)
6987 ->getDeducedType()
6988 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006989 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6990 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6991 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6992 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6993 child_is_deref_of_parent, valobj, language_flags);
6994 }
6995
6996 case clang::Type::Elaborated: {
6997 CompilerType elaborated_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00006998 this, llvm::cast<clang::ElaboratedType>(parent_qual_type)
6999 ->getNamedType()
7000 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007001 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7002 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7003 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7004 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7005 child_is_deref_of_parent, valobj, language_flags);
7006 }
7007
7008 case clang::Type::Paren: {
Alex Langfordbddab072019-08-13 19:40:36 +00007009 CompilerType paren_clang_type(this,
7010 llvm::cast<clang::ParenType>(parent_qual_type)
7011 ->desugar()
7012 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007013 return paren_clang_type.GetChildCompilerTypeAtIndex(
7014 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7015 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7016 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7017 child_is_deref_of_parent, valobj, language_flags);
7018 }
7019
7020 default:
7021 break;
7022 }
7023 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007024}
7025
Kate Stoneb9c1b512016-09-06 20:57:50 +00007026static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
7027 const clang::CXXBaseSpecifier *base_spec,
7028 bool omit_empty_base_classes) {
7029 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007030
Kate Stoneb9c1b512016-09-06 20:57:50 +00007031 const clang::CXXRecordDecl *cxx_record_decl =
7032 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7033
7034 // const char *super_name = record_decl->getNameAsCString();
7035 // const char *base_name =
7036 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
7037 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
7038 //
7039 if (cxx_record_decl) {
7040 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
7041 for (base_class = cxx_record_decl->bases_begin(),
7042 base_class_end = cxx_record_decl->bases_end();
7043 base_class != base_class_end; ++base_class) {
7044 if (omit_empty_base_classes) {
7045 if (BaseSpecifierIsEmpty(base_class))
7046 continue;
7047 }
7048
7049 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
7050 // super_name, base_name,
7051 // child_idx,
7052 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
7053 //
7054 //
7055 if (base_class == base_spec)
7056 return child_idx;
7057 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00007058 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007059 }
7060
7061 return UINT32_MAX;
7062}
7063
7064static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
7065 clang::NamedDecl *canonical_decl,
7066 bool omit_empty_base_classes) {
7067 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
7068 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
7069 omit_empty_base_classes);
7070
7071 clang::RecordDecl::field_iterator field, field_end;
7072 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
7073 field != field_end; ++field, ++child_idx) {
7074 if (field->getCanonicalDecl() == canonical_decl)
7075 return child_idx;
7076 }
7077
7078 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00007079}
7080
7081// Look for a child member (doesn't include base classes, but it does include
Adrian Prantl05097242018-04-30 16:49:04 +00007082// their members) in the type hierarchy. Returns an index path into
7083// "clang_type" on how to reach the appropriate member.
Greg Claytond8d4a572015-08-11 21:38:15 +00007084//
7085// class A
7086// {
7087// public:
7088// int m_a;
7089// int m_b;
7090// };
7091//
7092// class B
7093// {
7094// };
7095//
7096// class C :
7097// public B,
7098// public A
7099// {
7100// };
7101//
7102// If we have a clang type that describes "class C", and we wanted to looked
7103// "m_b" in it:
7104//
Kate Stoneb9c1b512016-09-06 20:57:50 +00007105// With omit_empty_base_classes == false we would get an integer array back
Adrian Prantl05097242018-04-30 16:49:04 +00007106// with: { 1, 1 } The first index 1 is the child index for "class A" within
7107// class C The second index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007108//
Adrian Prantl05097242018-04-30 16:49:04 +00007109// With omit_empty_base_classes == true we would get an integer array back
7110// with: { 0, 1 } The first index 0 is the child index for "class A" within
7111// class C (since class B doesn't have any members it doesn't count) The second
7112// index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007113
Kate Stoneb9c1b512016-09-06 20:57:50 +00007114size_t ClangASTContext::GetIndexOfChildMemberWithName(
7115 lldb::opaque_compiler_type_t type, const char *name,
7116 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
7117 if (type && name && name[0]) {
7118 clang::QualType qual_type(GetCanonicalQualType(type));
7119 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7120 switch (type_class) {
7121 case clang::Type::Record:
7122 if (GetCompleteType(type)) {
7123 const clang::RecordType *record_type =
7124 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7125 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00007126
Kate Stoneb9c1b512016-09-06 20:57:50 +00007127 assert(record_decl);
7128 uint32_t child_idx = 0;
7129
7130 const clang::CXXRecordDecl *cxx_record_decl =
7131 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7132
7133 // Try and find a field that matches NAME
7134 clang::RecordDecl::field_iterator field, field_end;
7135 llvm::StringRef name_sref(name);
7136 for (field = record_decl->field_begin(),
7137 field_end = record_decl->field_end();
7138 field != field_end; ++field, ++child_idx) {
7139 llvm::StringRef field_name = field->getName();
7140 if (field_name.empty()) {
Alex Langfordbddab072019-08-13 19:40:36 +00007141 CompilerType field_type(this, field->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007142 child_indexes.push_back(child_idx);
7143 if (field_type.GetIndexOfChildMemberWithName(
7144 name, omit_empty_base_classes, child_indexes))
7145 return child_indexes.size();
7146 child_indexes.pop_back();
7147
7148 } else if (field_name.equals(name_sref)) {
7149 // We have to add on the number of base classes to this index!
7150 child_indexes.push_back(
7151 child_idx + ClangASTContext::GetNumBaseClasses(
7152 cxx_record_decl, omit_empty_base_classes));
7153 return child_indexes.size();
7154 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007155 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007156
Kate Stoneb9c1b512016-09-06 20:57:50 +00007157 if (cxx_record_decl) {
7158 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7159
7160 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7161
7162 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7163 // Didn't find things easily, lets let clang do its thang...
7164 clang::IdentifierInfo &ident_ref =
7165 getASTContext()->Idents.get(name_sref);
7166 clang::DeclarationName decl_name(&ident_ref);
7167
7168 clang::CXXBasePaths paths;
7169 if (cxx_record_decl->lookupInBases(
7170 [decl_name](const clang::CXXBaseSpecifier *specifier,
7171 clang::CXXBasePath &path) {
7172 return clang::CXXRecordDecl::FindOrdinaryMember(
7173 specifier, path, decl_name);
7174 },
7175 paths)) {
7176 clang::CXXBasePaths::const_paths_iterator path,
7177 path_end = paths.end();
7178 for (path = paths.begin(); path != path_end; ++path) {
7179 const size_t num_path_elements = path->size();
7180 for (size_t e = 0; e < num_path_elements; ++e) {
7181 clang::CXXBasePathElement elem = (*path)[e];
7182
7183 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7184 omit_empty_base_classes);
7185 if (child_idx == UINT32_MAX) {
7186 child_indexes.clear();
7187 return 0;
7188 } else {
7189 child_indexes.push_back(child_idx);
7190 parent_record_decl = llvm::cast<clang::RecordDecl>(
7191 elem.Base->getType()
7192 ->getAs<clang::RecordType>()
7193 ->getDecl());
7194 }
7195 }
7196 for (clang::NamedDecl *path_decl : path->Decls) {
7197 child_idx = GetIndexForRecordChild(
7198 parent_record_decl, path_decl, omit_empty_base_classes);
7199 if (child_idx == UINT32_MAX) {
7200 child_indexes.clear();
7201 return 0;
7202 } else {
7203 child_indexes.push_back(child_idx);
7204 }
7205 }
7206 }
7207 return child_indexes.size();
7208 }
7209 }
7210 }
7211 break;
7212
7213 case clang::Type::ObjCObject:
7214 case clang::Type::ObjCInterface:
7215 if (GetCompleteType(type)) {
7216 llvm::StringRef name_sref(name);
7217 const clang::ObjCObjectType *objc_class_type =
7218 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7219 assert(objc_class_type);
7220 if (objc_class_type) {
7221 uint32_t child_idx = 0;
7222 clang::ObjCInterfaceDecl *class_interface_decl =
7223 objc_class_type->getInterface();
7224
7225 if (class_interface_decl) {
7226 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7227 ivar_end = class_interface_decl->ivar_end();
7228 clang::ObjCInterfaceDecl *superclass_interface_decl =
7229 class_interface_decl->getSuperClass();
7230
7231 for (ivar_pos = class_interface_decl->ivar_begin();
7232 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7233 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7234
7235 if (ivar_decl->getName().equals(name_sref)) {
7236 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7237 (omit_empty_base_classes &&
7238 ObjCDeclHasIVars(superclass_interface_decl, true)))
7239 ++child_idx;
7240
7241 child_indexes.push_back(child_idx);
7242 return child_indexes.size();
7243 }
7244 }
7245
7246 if (superclass_interface_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00007247 // The super class index is always zero for ObjC classes, so we
7248 // push it onto the child indexes in case we find an ivar in our
7249 // superclass...
Kate Stoneb9c1b512016-09-06 20:57:50 +00007250 child_indexes.push_back(0);
7251
7252 CompilerType superclass_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00007253 this, getASTContext()
7254 ->getObjCInterfaceType(superclass_interface_decl)
7255 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007256 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7257 name, omit_empty_base_classes, child_indexes)) {
Adrian Prantl05097242018-04-30 16:49:04 +00007258 // We did find an ivar in a superclass so just return the
7259 // results!
Kate Stoneb9c1b512016-09-06 20:57:50 +00007260 return child_indexes.size();
7261 }
7262
Adrian Prantl05097242018-04-30 16:49:04 +00007263 // We didn't find an ivar matching "name" in our superclass, pop
7264 // the superclass zero index that we pushed on above.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007265 child_indexes.pop_back();
7266 }
7267 }
7268 }
7269 }
7270 break;
7271
7272 case clang::Type::ObjCObjectPointer: {
7273 CompilerType objc_object_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00007274 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7275 ->getPointeeType()
7276 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007277 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7278 name, omit_empty_base_classes, child_indexes);
7279 } break;
7280
7281 case clang::Type::ConstantArray: {
7282 // const clang::ConstantArrayType *array =
7283 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7284 // const uint64_t element_count =
7285 // array->getSize().getLimitedValue();
7286 //
7287 // if (idx < element_count)
7288 // {
7289 // std::pair<uint64_t, unsigned> field_type_info =
7290 // ast->getTypeInfo(array->getElementType());
7291 //
7292 // char element_name[32];
7293 // ::snprintf (element_name, sizeof (element_name),
7294 // "%s[%u]", parent_name ? parent_name : "", idx);
7295 //
7296 // child_name.assign(element_name);
7297 // assert(field_type_info.first % 8 == 0);
7298 // child_byte_size = field_type_info.first / 8;
7299 // child_byte_offset = idx * child_byte_size;
7300 // return array->getElementType().getAsOpaquePtr();
7301 // }
7302 } break;
7303
7304 // case clang::Type::MemberPointerType:
7305 // {
7306 // MemberPointerType *mem_ptr_type =
7307 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7308 // clang::QualType pointee_type =
7309 // mem_ptr_type->getPointeeType();
7310 //
7311 // if (ClangASTContext::IsAggregateType
7312 // (pointee_type.getAsOpaquePtr()))
7313 // {
7314 // return GetIndexOfChildWithName (ast,
7315 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7316 // name);
7317 // }
7318 // }
7319 // break;
7320 //
7321 case clang::Type::LValueReference:
7322 case clang::Type::RValueReference: {
7323 const clang::ReferenceType *reference_type =
7324 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7325 clang::QualType pointee_type(reference_type->getPointeeType());
Alex Langfordbddab072019-08-13 19:40:36 +00007326 CompilerType pointee_clang_type(this, pointee_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007327
7328 if (pointee_clang_type.IsAggregateType()) {
7329 return pointee_clang_type.GetIndexOfChildMemberWithName(
7330 name, omit_empty_base_classes, child_indexes);
7331 }
7332 } break;
7333
7334 case clang::Type::Pointer: {
7335 CompilerType pointee_clang_type(GetPointeeType(type));
7336
7337 if (pointee_clang_type.IsAggregateType()) {
7338 return pointee_clang_type.GetIndexOfChildMemberWithName(
7339 name, omit_empty_base_classes, child_indexes);
7340 }
7341 } break;
7342
7343 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00007344 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
7345 ->getDecl()
7346 ->getUnderlyingType()
7347 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007348 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7349 child_indexes);
7350
7351 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00007352 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
7353 ->getDeducedType()
7354 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007355 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7356 child_indexes);
7357
7358 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00007359 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
7360 ->getNamedType()
7361 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007362 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7363 child_indexes);
7364
7365 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00007366 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
7367 ->desugar()
7368 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007369 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7370 child_indexes);
7371
7372 default:
7373 break;
7374 }
7375 }
7376 return 0;
7377}
Greg Claytond8d4a572015-08-11 21:38:15 +00007378
7379// Get the index of the child of "clang_type" whose name matches. This function
7380// doesn't descend into the children, but only looks one level deep and name
7381// matches can include base class names.
7382
7383uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007384ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7385 const char *name,
7386 bool omit_empty_base_classes) {
7387 if (type && name && name[0]) {
7388 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007389
Kate Stoneb9c1b512016-09-06 20:57:50 +00007390 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7391
7392 switch (type_class) {
7393 case clang::Type::Record:
7394 if (GetCompleteType(type)) {
7395 const clang::RecordType *record_type =
7396 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7397 const clang::RecordDecl *record_decl = record_type->getDecl();
7398
7399 assert(record_decl);
7400 uint32_t child_idx = 0;
7401
7402 const clang::CXXRecordDecl *cxx_record_decl =
7403 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7404
7405 if (cxx_record_decl) {
7406 clang::CXXRecordDecl::base_class_const_iterator base_class,
7407 base_class_end;
7408 for (base_class = cxx_record_decl->bases_begin(),
7409 base_class_end = cxx_record_decl->bases_end();
7410 base_class != base_class_end; ++base_class) {
7411 // Skip empty base classes
7412 clang::CXXRecordDecl *base_class_decl =
7413 llvm::cast<clang::CXXRecordDecl>(
7414 base_class->getType()
7415 ->getAs<clang::RecordType>()
7416 ->getDecl());
7417 if (omit_empty_base_classes &&
Jonas Devliegherea6682a42018-12-15 00:15:33 +00007418 !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00007419 continue;
7420
Alex Langfordbddab072019-08-13 19:40:36 +00007421 CompilerType base_class_clang_type(
7422 this, base_class->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007423 std::string base_class_type_name(
7424 base_class_clang_type.GetTypeName().AsCString(""));
Jonas Devlieghere8d20cfd2018-12-21 22:46:10 +00007425 if (base_class_type_name == name)
Kate Stoneb9c1b512016-09-06 20:57:50 +00007426 return child_idx;
7427 ++child_idx;
7428 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007429 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007430
Kate Stoneb9c1b512016-09-06 20:57:50 +00007431 // Try and find a field that matches NAME
7432 clang::RecordDecl::field_iterator field, field_end;
7433 llvm::StringRef name_sref(name);
7434 for (field = record_decl->field_begin(),
7435 field_end = record_decl->field_end();
7436 field != field_end; ++field, ++child_idx) {
7437 if (field->getName().equals(name_sref))
7438 return child_idx;
7439 }
7440 }
7441 break;
7442
7443 case clang::Type::ObjCObject:
7444 case clang::Type::ObjCInterface:
7445 if (GetCompleteType(type)) {
7446 llvm::StringRef name_sref(name);
7447 const clang::ObjCObjectType *objc_class_type =
7448 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7449 assert(objc_class_type);
7450 if (objc_class_type) {
7451 uint32_t child_idx = 0;
7452 clang::ObjCInterfaceDecl *class_interface_decl =
7453 objc_class_type->getInterface();
7454
7455 if (class_interface_decl) {
7456 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7457 ivar_end = class_interface_decl->ivar_end();
7458 clang::ObjCInterfaceDecl *superclass_interface_decl =
7459 class_interface_decl->getSuperClass();
7460
7461 for (ivar_pos = class_interface_decl->ivar_begin();
7462 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7463 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7464
7465 if (ivar_decl->getName().equals(name_sref)) {
7466 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7467 (omit_empty_base_classes &&
7468 ObjCDeclHasIVars(superclass_interface_decl, true)))
7469 ++child_idx;
7470
7471 return child_idx;
7472 }
7473 }
7474
7475 if (superclass_interface_decl) {
7476 if (superclass_interface_decl->getName().equals(name_sref))
7477 return 0;
7478 }
7479 }
7480 }
7481 }
7482 break;
7483
7484 case clang::Type::ObjCObjectPointer: {
7485 CompilerType pointee_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00007486 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7487 ->getPointeeType()
7488 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007489 return pointee_clang_type.GetIndexOfChildWithName(
7490 name, omit_empty_base_classes);
7491 } break;
7492
7493 case clang::Type::ConstantArray: {
7494 // const clang::ConstantArrayType *array =
7495 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7496 // const uint64_t element_count =
7497 // array->getSize().getLimitedValue();
7498 //
7499 // if (idx < element_count)
7500 // {
7501 // std::pair<uint64_t, unsigned> field_type_info =
7502 // ast->getTypeInfo(array->getElementType());
7503 //
7504 // char element_name[32];
7505 // ::snprintf (element_name, sizeof (element_name),
7506 // "%s[%u]", parent_name ? parent_name : "", idx);
7507 //
7508 // child_name.assign(element_name);
7509 // assert(field_type_info.first % 8 == 0);
7510 // child_byte_size = field_type_info.first / 8;
7511 // child_byte_offset = idx * child_byte_size;
7512 // return array->getElementType().getAsOpaquePtr();
7513 // }
7514 } break;
7515
7516 // case clang::Type::MemberPointerType:
7517 // {
7518 // MemberPointerType *mem_ptr_type =
7519 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7520 // clang::QualType pointee_type =
7521 // mem_ptr_type->getPointeeType();
7522 //
7523 // if (ClangASTContext::IsAggregateType
7524 // (pointee_type.getAsOpaquePtr()))
7525 // {
7526 // return GetIndexOfChildWithName (ast,
7527 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7528 // name);
7529 // }
7530 // }
7531 // break;
7532 //
7533 case clang::Type::LValueReference:
7534 case clang::Type::RValueReference: {
7535 const clang::ReferenceType *reference_type =
7536 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
Alex Langfordbddab072019-08-13 19:40:36 +00007537 CompilerType pointee_type(
7538 this, reference_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007539
7540 if (pointee_type.IsAggregateType()) {
7541 return pointee_type.GetIndexOfChildWithName(name,
7542 omit_empty_base_classes);
7543 }
7544 } break;
7545
7546 case clang::Type::Pointer: {
7547 const clang::PointerType *pointer_type =
7548 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
Alex Langfordbddab072019-08-13 19:40:36 +00007549 CompilerType pointee_type(
7550 this, pointer_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007551
7552 if (pointee_type.IsAggregateType()) {
7553 return pointee_type.GetIndexOfChildWithName(name,
7554 omit_empty_base_classes);
7555 } else {
7556 // if (parent_name)
7557 // {
7558 // child_name.assign(1, '*');
7559 // child_name += parent_name;
7560 // }
7561 //
7562 // // We have a pointer to an simple type
7563 // if (idx == 0)
7564 // {
7565 // std::pair<uint64_t, unsigned> clang_type_info
7566 // = ast->getTypeInfo(pointee_type);
7567 // assert(clang_type_info.first % 8 == 0);
7568 // child_byte_size = clang_type_info.first / 8;
7569 // child_byte_offset = 0;
7570 // return pointee_type.getAsOpaquePtr();
7571 // }
7572 }
7573 } break;
7574
7575 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00007576 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
7577 ->getDeducedType()
7578 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007579 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7580
7581 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00007582 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
7583 ->getNamedType()
7584 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007585 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7586
7587 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00007588 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
7589 ->desugar()
7590 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007591 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7592
7593 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00007594 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
7595 ->getDecl()
7596 ->getUnderlyingType()
7597 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007598 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7599
7600 default:
7601 break;
7602 }
7603 }
7604 return UINT32_MAX;
7605}
Greg Claytond8d4a572015-08-11 21:38:15 +00007606
7607size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007608ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7609 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007610 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007611
Kate Stoneb9c1b512016-09-06 20:57:50 +00007612 clang::QualType qual_type(GetCanonicalQualType(type));
7613 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7614 switch (type_class) {
7615 case clang::Type::Record:
7616 if (GetCompleteType(type)) {
7617 const clang::CXXRecordDecl *cxx_record_decl =
7618 qual_type->getAsCXXRecordDecl();
7619 if (cxx_record_decl) {
7620 const clang::ClassTemplateSpecializationDecl *template_decl =
7621 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7622 cxx_record_decl);
7623 if (template_decl)
7624 return template_decl->getTemplateArgs().size();
7625 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007626 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007627 break;
7628
7629 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00007630 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
7631 ->getDecl()
7632 ->getUnderlyingType()
7633 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007634 .GetNumTemplateArguments();
7635
7636 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00007637 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
7638 ->getDeducedType()
7639 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007640 .GetNumTemplateArguments();
7641
7642 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00007643 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
7644 ->getNamedType()
7645 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007646 .GetNumTemplateArguments();
7647
7648 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00007649 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
7650 ->desugar()
7651 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007652 .GetNumTemplateArguments();
7653
7654 default:
7655 break;
7656 }
7657
7658 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007659}
7660
Pavel Labath769b21e2017-11-13 14:26:21 +00007661const clang::ClassTemplateSpecializationDecl *
7662ClangASTContext::GetAsTemplateSpecialization(
7663 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007664 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007665 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007666
7667 clang::QualType qual_type(GetCanonicalQualType(type));
7668 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7669 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007670 case clang::Type::Record: {
7671 if (! GetCompleteType(type))
7672 return nullptr;
7673 const clang::CXXRecordDecl *cxx_record_decl =
7674 qual_type->getAsCXXRecordDecl();
7675 if (!cxx_record_decl)
7676 return nullptr;
7677 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7678 cxx_record_decl);
7679 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007680
7681 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007682 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7683 ->getDecl()
7684 ->getUnderlyingType()
7685 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007686
7687 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007688 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7689 ->getDeducedType()
7690 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007691
7692 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007693 return GetAsTemplateSpecialization(
7694 llvm::cast<clang::ElaboratedType>(qual_type)
7695 ->getNamedType()
7696 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007697
7698 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007699 return GetAsTemplateSpecialization(
7700 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007701
7702 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007703 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007704 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007705}
7706
7707lldb::TemplateArgumentKind
7708ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7709 size_t arg_idx) {
7710 const clang::ClassTemplateSpecializationDecl *template_decl =
7711 GetAsTemplateSpecialization(type);
7712 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7713 return eTemplateArgumentKindNull;
7714
7715 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7716 case clang::TemplateArgument::Null:
7717 return eTemplateArgumentKindNull;
7718
7719 case clang::TemplateArgument::NullPtr:
7720 return eTemplateArgumentKindNullPtr;
7721
7722 case clang::TemplateArgument::Type:
7723 return eTemplateArgumentKindType;
7724
7725 case clang::TemplateArgument::Declaration:
7726 return eTemplateArgumentKindDeclaration;
7727
7728 case clang::TemplateArgument::Integral:
7729 return eTemplateArgumentKindIntegral;
7730
7731 case clang::TemplateArgument::Template:
7732 return eTemplateArgumentKindTemplate;
7733
7734 case clang::TemplateArgument::TemplateExpansion:
7735 return eTemplateArgumentKindTemplateExpansion;
7736
7737 case clang::TemplateArgument::Expression:
7738 return eTemplateArgumentKindExpression;
7739
7740 case clang::TemplateArgument::Pack:
7741 return eTemplateArgumentKindPack;
7742 }
7743 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7744}
7745
7746CompilerType
7747ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7748 size_t idx) {
7749 const clang::ClassTemplateSpecializationDecl *template_decl =
7750 GetAsTemplateSpecialization(type);
7751 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7752 return CompilerType();
7753
7754 const clang::TemplateArgument &template_arg =
7755 template_decl->getTemplateArgs()[idx];
7756 if (template_arg.getKind() != clang::TemplateArgument::Type)
7757 return CompilerType();
7758
Alex Langfordbddab072019-08-13 19:40:36 +00007759 return CompilerType(this, template_arg.getAsType().getAsOpaquePtr());
Pavel Labath769b21e2017-11-13 14:26:21 +00007760}
7761
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007762Optional<CompilerType::IntegralTemplateArgument>
Pavel Labath769b21e2017-11-13 14:26:21 +00007763ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7764 size_t idx) {
7765 const clang::ClassTemplateSpecializationDecl *template_decl =
7766 GetAsTemplateSpecialization(type);
7767 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
Pavel Labathf59056f2017-11-30 10:16:54 +00007768 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007769
7770 const clang::TemplateArgument &template_arg =
7771 template_decl->getTemplateArgs()[idx];
7772 if (template_arg.getKind() != clang::TemplateArgument::Integral)
Pavel Labathf59056f2017-11-30 10:16:54 +00007773 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007774
Alex Langfordbddab072019-08-13 19:40:36 +00007775 return {
7776 {template_arg.getAsIntegral(),
7777 CompilerType(this, template_arg.getIntegralType().getAsOpaquePtr())}};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007778}
7779
Kate Stoneb9c1b512016-09-06 20:57:50 +00007780CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7781 if (type)
7782 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7783 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007784}
7785
Kate Stoneb9c1b512016-09-06 20:57:50 +00007786clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7787 const clang::EnumType *enutype =
7788 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7789 if (enutype)
7790 return enutype->getDecl();
Konrad Kleine248a1302019-05-23 11:14:47 +00007791 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007792}
7793
7794clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7795 const clang::RecordType *record_type =
7796 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7797 if (record_type)
7798 return record_type->getDecl();
7799 return nullptr;
7800}
7801
7802clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
Zachary Turner1639c6b2018-12-17 16:15:13 +00007803 return ClangUtil::GetAsTagDecl(type);
Greg Claytone6b36cd2015-12-08 01:02:08 +00007804}
7805
Aleksandr Urakov709426b2018-09-10 08:08:43 +00007806clang::TypedefNameDecl *
7807ClangASTContext::GetAsTypedefDecl(const CompilerType &type) {
7808 const clang::TypedefType *typedef_type =
7809 llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type));
7810 if (typedef_type)
7811 return typedef_type->getDecl();
7812 return nullptr;
7813}
7814
Greg Claytond8d4a572015-08-11 21:38:15 +00007815clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007816ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7817 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007818}
7819
7820clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007821ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7822 const clang::ObjCObjectType *objc_class_type =
7823 llvm::dyn_cast<clang::ObjCObjectType>(
7824 ClangUtil::GetCanonicalQualType(type));
7825 if (objc_class_type)
7826 return objc_class_type->getInterface();
7827 return nullptr;
7828}
7829
7830clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007831 const CompilerType &type, llvm::StringRef name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00007832 const CompilerType &field_clang_type, AccessType access,
7833 uint32_t bitfield_bit_size) {
7834 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007835 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007836 ClangASTContext *ast =
7837 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7838 if (!ast)
7839 return nullptr;
7840 clang::ASTContext *clang_ast = ast->getASTContext();
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007841 clang::IdentifierInfo *ident = nullptr;
7842 if (!name.empty())
7843 ident = &clang_ast->Idents.get(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00007844
7845 clang::FieldDecl *field = nullptr;
7846
7847 clang::Expr *bit_width = nullptr;
7848 if (bitfield_bit_size != 0) {
7849 llvm::APInt bitfield_bit_size_apint(
7850 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7851 bit_width = new (*clang_ast)
7852 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7853 clang_ast->IntTy, clang::SourceLocation());
7854 }
7855
7856 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7857 if (record_decl) {
7858 field = clang::FieldDecl::Create(
7859 *clang_ast, record_decl, clang::SourceLocation(),
7860 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007861 ident, // Identifier
7862 ClangUtil::GetQualType(field_clang_type), // Field type
7863 nullptr, // TInfo *
7864 bit_width, // BitWidth
7865 false, // Mutable
7866 clang::ICIS_NoInit); // HasInit
Kate Stoneb9c1b512016-09-06 20:57:50 +00007867
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007868 if (name.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +00007869 // Determine whether this field corresponds to an anonymous struct or
7870 // union.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007871 if (const clang::TagType *TagT =
7872 field->getType()->getAs<clang::TagType>()) {
7873 if (clang::RecordDecl *Rec =
7874 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7875 if (!Rec->getDeclName()) {
7876 Rec->setAnonymousStructOrUnion(true);
7877 field->setImplicit();
7878 }
7879 }
7880 }
7881
7882 if (field) {
7883 field->setAccess(
7884 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7885
7886 record_decl->addDecl(field);
7887
7888#ifdef LLDB_CONFIGURATION_DEBUG
7889 VerifyDecl(field);
7890#endif
7891 }
7892 } else {
7893 clang::ObjCInterfaceDecl *class_interface_decl =
7894 ast->GetAsObjCInterfaceDecl(type);
7895
7896 if (class_interface_decl) {
7897 const bool is_synthesized = false;
7898
7899 field_clang_type.GetCompleteType();
7900
7901 field = clang::ObjCIvarDecl::Create(
7902 *clang_ast, class_interface_decl, clang::SourceLocation(),
7903 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007904 ident, // Identifier
7905 ClangUtil::GetQualType(field_clang_type), // Field type
7906 nullptr, // TypeSourceInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007907 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7908 is_synthesized);
7909
7910 if (field) {
7911 class_interface_decl->addDecl(field);
7912
7913#ifdef LLDB_CONFIGURATION_DEBUG
7914 VerifyDecl(field);
7915#endif
7916 }
7917 }
7918 }
7919 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007920}
7921
Kate Stoneb9c1b512016-09-06 20:57:50 +00007922void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7923 if (!type)
7924 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007925
Kate Stoneb9c1b512016-09-06 20:57:50 +00007926 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7927 if (!ast)
7928 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007929
Kate Stoneb9c1b512016-09-06 20:57:50 +00007930 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007931
Kate Stoneb9c1b512016-09-06 20:57:50 +00007932 if (!record_decl)
7933 return;
7934
7935 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7936
7937 IndirectFieldVector indirect_fields;
7938 clang::RecordDecl::field_iterator field_pos;
7939 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7940 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7941 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7942 last_field_pos = field_pos++) {
7943 if (field_pos->isAnonymousStructOrUnion()) {
7944 clang::QualType field_qual_type = field_pos->getType();
7945
7946 const clang::RecordType *field_record_type =
7947 field_qual_type->getAs<clang::RecordType>();
7948
7949 if (!field_record_type)
7950 continue;
7951
7952 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7953
7954 if (!field_record_decl)
7955 continue;
7956
7957 for (clang::RecordDecl::decl_iterator
7958 di = field_record_decl->decls_begin(),
7959 de = field_record_decl->decls_end();
7960 di != de; ++di) {
7961 if (clang::FieldDecl *nested_field_decl =
7962 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7963 clang::NamedDecl **chain =
7964 new (*ast->getASTContext()) clang::NamedDecl *[2];
7965 chain[0] = *field_pos;
7966 chain[1] = nested_field_decl;
7967 clang::IndirectFieldDecl *indirect_field =
7968 clang::IndirectFieldDecl::Create(
7969 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7970 nested_field_decl->getIdentifier(),
7971 nested_field_decl->getType(), {chain, 2});
7972
7973 indirect_field->setImplicit();
7974
7975 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7976 field_pos->getAccess(), nested_field_decl->getAccess()));
7977
7978 indirect_fields.push_back(indirect_field);
7979 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7980 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7981 size_t nested_chain_size =
7982 nested_indirect_field_decl->getChainingSize();
7983 clang::NamedDecl **chain = new (*ast->getASTContext())
7984 clang::NamedDecl *[nested_chain_size + 1];
7985 chain[0] = *field_pos;
7986
7987 int chain_index = 1;
7988 for (clang::IndirectFieldDecl::chain_iterator
7989 nci = nested_indirect_field_decl->chain_begin(),
7990 nce = nested_indirect_field_decl->chain_end();
7991 nci < nce; ++nci) {
7992 chain[chain_index] = *nci;
7993 chain_index++;
7994 }
7995
7996 clang::IndirectFieldDecl *indirect_field =
7997 clang::IndirectFieldDecl::Create(
7998 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7999 nested_indirect_field_decl->getIdentifier(),
8000 nested_indirect_field_decl->getType(),
8001 {chain, nested_chain_size + 1});
8002
8003 indirect_field->setImplicit();
8004
8005 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8006 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
8007
8008 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00008009 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008010 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008011 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008012 }
8013
Adrian Prantl05097242018-04-30 16:49:04 +00008014 // Check the last field to see if it has an incomplete array type as its last
8015 // member and if it does, the tell the record decl about it
Kate Stoneb9c1b512016-09-06 20:57:50 +00008016 if (last_field_pos != field_end_pos) {
8017 if (last_field_pos->getType()->isIncompleteArrayType())
8018 record_decl->hasFlexibleArrayMember();
8019 }
8020
8021 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
8022 ife = indirect_fields.end();
8023 ifi < ife; ++ifi) {
8024 record_decl->addDecl(*ifi);
8025 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008026}
8027
Kate Stoneb9c1b512016-09-06 20:57:50 +00008028void ClangASTContext::SetIsPacked(const CompilerType &type) {
8029 if (type) {
8030 ClangASTContext *ast =
8031 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8032 if (ast) {
8033 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
8034
8035 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00008036 return;
8037
Kate Stoneb9c1b512016-09-06 20:57:50 +00008038 record_decl->addAttr(
8039 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00008040 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008041 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008042}
8043
Kate Stoneb9c1b512016-09-06 20:57:50 +00008044clang::VarDecl *ClangASTContext::AddVariableToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008045 const CompilerType &type, llvm::StringRef name,
8046 const CompilerType &var_type, AccessType access) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00008047 if (!type.IsValid() || !var_type.IsValid())
8048 return nullptr;
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008049
Kate Stoneb9c1b512016-09-06 20:57:50 +00008050 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8051 if (!ast)
8052 return nullptr;
8053
8054 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008055 if (!record_decl)
8056 return nullptr;
8057
8058 clang::VarDecl *var_decl = nullptr;
8059 clang::IdentifierInfo *ident = nullptr;
8060 if (!name.empty())
8061 ident = &ast->getASTContext()->Idents.get(name);
8062
8063 var_decl = clang::VarDecl::Create(
8064 *ast->getASTContext(), // ASTContext &
8065 record_decl, // DeclContext *
8066 clang::SourceLocation(), // clang::SourceLocation StartLoc
8067 clang::SourceLocation(), // clang::SourceLocation IdLoc
8068 ident, // clang::IdentifierInfo *
8069 ClangUtil::GetQualType(var_type), // Variable clang::QualType
8070 nullptr, // TypeSourceInfo *
8071 clang::SC_Static); // StorageClass
8072 if (!var_decl)
8073 return nullptr;
8074
8075 var_decl->setAccess(
8076 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
8077 record_decl->addDecl(var_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008078
Greg Claytond8d4a572015-08-11 21:38:15 +00008079#ifdef LLDB_CONFIGURATION_DEBUG
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008080 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008081#endif
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008082
Kate Stoneb9c1b512016-09-06 20:57:50 +00008083 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008084}
8085
Kate Stoneb9c1b512016-09-06 20:57:50 +00008086clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
Davide Italiano675767a2018-03-27 19:40:50 +00008087 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008088 const CompilerType &method_clang_type, lldb::AccessType access,
8089 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
8090 bool is_attr_used, bool is_artificial) {
8091 if (!type || !method_clang_type.IsValid() || name == nullptr ||
8092 name[0] == '\0')
8093 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008094
Kate Stoneb9c1b512016-09-06 20:57:50 +00008095 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008096
Kate Stoneb9c1b512016-09-06 20:57:50 +00008097 clang::CXXRecordDecl *cxx_record_decl =
8098 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008099
Kate Stoneb9c1b512016-09-06 20:57:50 +00008100 if (cxx_record_decl == nullptr)
8101 return nullptr;
8102
8103 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8104
8105 clang::CXXMethodDecl *cxx_method_decl = nullptr;
8106
8107 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
8108
8109 const clang::FunctionType *function_type =
8110 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
8111
8112 if (function_type == nullptr)
8113 return nullptr;
8114
8115 const clang::FunctionProtoType *method_function_prototype(
8116 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
8117
8118 if (!method_function_prototype)
8119 return nullptr;
8120
8121 unsigned int num_params = method_function_prototype->getNumParams();
8122
8123 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
8124 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
8125
8126 if (is_artificial)
8127 return nullptr; // skip everything artificial
8128
Richard Smith36851a62019-05-09 04:40:57 +00008129 const clang::ExplicitSpecifier explicit_spec(
8130 nullptr /*expr*/, is_explicit
8131 ? clang::ExplicitSpecKind::ResolvedTrue
8132 : clang::ExplicitSpecKind::ResolvedFalse);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008133 if (name[0] == '~') {
8134 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
8135 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8136 clang::DeclarationNameInfo(
8137 getASTContext()->DeclarationNames.getCXXDestructorName(
8138 getASTContext()->getCanonicalType(record_qual_type)),
8139 clang::SourceLocation()),
Raphael Isemann15695cd2019-09-23 06:59:35 +00008140 method_qual_type, nullptr, is_inline, is_artificial,
8141 ConstexprSpecKind::CSK_unspecified);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008142 cxx_method_decl = cxx_dtor_decl;
8143 } else if (decl_name == cxx_record_decl->getDeclName()) {
8144 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
8145 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8146 clang::DeclarationNameInfo(
8147 getASTContext()->DeclarationNames.getCXXConstructorName(
8148 getASTContext()->getCanonicalType(record_qual_type)),
8149 clang::SourceLocation()),
8150 method_qual_type,
8151 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008152 explicit_spec, is_inline, is_artificial, CSK_unspecified);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008153 cxx_method_decl = cxx_ctor_decl;
8154 } else {
8155 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8156 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8157
8158 if (IsOperator(name, op_kind)) {
8159 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
Adrian Prantl05097242018-04-30 16:49:04 +00008160 // Check the number of operator parameters. Sometimes we have seen bad
8161 // DWARF that doesn't correctly describe operators and if we try to
8162 // create a method and add it to the class, clang will assert and
8163 // crash, so we need to make sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008164 const bool is_method = true;
8165 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8166 is_method, op_kind, num_params))
8167 return nullptr;
8168 cxx_method_decl = clang::CXXMethodDecl::Create(
8169 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8170 clang::DeclarationNameInfo(
8171 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8172 clang::SourceLocation()),
8173 method_qual_type,
8174 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008175 SC, is_inline, CSK_unspecified, clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008176 } else if (num_params == 0) {
8177 // Conversion operators don't take params...
8178 cxx_method_decl = clang::CXXConversionDecl::Create(
8179 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8180 clang::DeclarationNameInfo(
8181 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8182 getASTContext()->getCanonicalType(
8183 function_type->getReturnType())),
8184 clang::SourceLocation()),
8185 method_qual_type,
8186 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008187 is_inline, explicit_spec, CSK_unspecified,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008188 clang::SourceLocation());
8189 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008190 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008191
8192 if (cxx_method_decl == nullptr) {
8193 cxx_method_decl = clang::CXXMethodDecl::Create(
8194 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8195 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8196 method_qual_type,
8197 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008198 SC, is_inline, CSK_unspecified, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008199 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008200 }
8201
8202 clang::AccessSpecifier access_specifier =
8203 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8204
8205 cxx_method_decl->setAccess(access_specifier);
8206 cxx_method_decl->setVirtualAsWritten(is_virtual);
8207
8208 if (is_attr_used)
8209 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8210
Konrad Kleine248a1302019-05-23 11:14:47 +00008211 if (mangled_name != nullptr) {
Vedant Kumarf6bc2512019-09-25 18:00:31 +00008212 cxx_method_decl->addAttr(clang::AsmLabelAttr::CreateImplicit(
8213 *getASTContext(), mangled_name, /*literal=*/false));
Davide Italiano675767a2018-03-27 19:40:50 +00008214 }
8215
Kate Stoneb9c1b512016-09-06 20:57:50 +00008216 // Populate the method decl with parameter decls
8217
8218 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8219
8220 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8221 params.push_back(clang::ParmVarDecl::Create(
8222 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8223 clang::SourceLocation(),
8224 nullptr, // anonymous
8225 method_function_prototype->getParamType(param_index), nullptr,
8226 clang::SC_None, nullptr));
8227 }
8228
8229 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8230
8231 cxx_record_decl->addDecl(cxx_method_decl);
8232
8233 // Sometimes the debug info will mention a constructor (default/copy/move),
8234 // destructor, or assignment operator (copy/move) but there won't be any
8235 // version of this in the code. So we check if the function was artificially
8236 // generated and if it is trivial and this lets the compiler/backend know
8237 // that it can inline the IR for these when it needs to and we can avoid a
8238 // "missing function" error when running expressions.
8239
8240 if (is_artificial) {
8241 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8242 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8243 (cxx_ctor_decl->isCopyConstructor() &&
8244 cxx_record_decl->hasTrivialCopyConstructor()) ||
8245 (cxx_ctor_decl->isMoveConstructor() &&
8246 cxx_record_decl->hasTrivialMoveConstructor()))) {
8247 cxx_ctor_decl->setDefaulted();
8248 cxx_ctor_decl->setTrivial(true);
8249 } else if (cxx_dtor_decl) {
8250 if (cxx_record_decl->hasTrivialDestructor()) {
8251 cxx_dtor_decl->setDefaulted();
8252 cxx_dtor_decl->setTrivial(true);
8253 }
8254 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8255 cxx_record_decl->hasTrivialCopyAssignment()) ||
8256 (cxx_method_decl->isMoveAssignmentOperator() &&
8257 cxx_record_decl->hasTrivialMoveAssignment())) {
8258 cxx_method_decl->setDefaulted();
8259 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008260 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008261 }
8262
Greg Claytond8d4a572015-08-11 21:38:15 +00008263#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008264 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008265#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008266
Kate Stoneb9c1b512016-09-06 20:57:50 +00008267 return cxx_method_decl;
8268}
Greg Claytond8d4a572015-08-11 21:38:15 +00008269
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00008270void ClangASTContext::AddMethodOverridesForCXXRecordType(
8271 lldb::opaque_compiler_type_t type) {
8272 if (auto *record = GetAsCXXRecordDecl(type))
8273 for (auto *method : record->methods())
8274 addOverridesForMethod(method);
8275}
8276
Greg Claytond8d4a572015-08-11 21:38:15 +00008277#pragma mark C++ Base Classes
8278
Zachary Turner970f38e2018-10-25 20:44:56 +00008279std::unique_ptr<clang::CXXBaseSpecifier>
Kate Stoneb9c1b512016-09-06 20:57:50 +00008280ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8281 AccessType access, bool is_virtual,
8282 bool base_of_class) {
Zachary Turner970f38e2018-10-25 20:44:56 +00008283 if (!type)
8284 return nullptr;
8285
Jonas Devliegherea8f3ae72019-08-14 22:19:23 +00008286 return std::make_unique<clang::CXXBaseSpecifier>(
Zachary Turner970f38e2018-10-25 20:44:56 +00008287 clang::SourceRange(), is_virtual, base_of_class,
8288 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8289 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8290 clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008291}
8292
Zachary Turner970f38e2018-10-25 20:44:56 +00008293bool ClangASTContext::TransferBaseClasses(
Kate Stoneb9c1b512016-09-06 20:57:50 +00008294 lldb::opaque_compiler_type_t type,
Zachary Turner970f38e2018-10-25 20:44:56 +00008295 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) {
8296 if (!type)
8297 return false;
8298 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8299 if (!cxx_record_decl)
8300 return false;
8301 std::vector<clang::CXXBaseSpecifier *> raw_bases;
8302 raw_bases.reserve(bases.size());
8303
8304 // Clang will make a copy of them, so it's ok that we pass pointers that we're
8305 // about to destroy.
8306 for (auto &b : bases)
8307 raw_bases.push_back(b.get());
8308 cxx_record_decl->setBases(raw_bases.data(), raw_bases.size());
8309 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008310}
8311
8312bool ClangASTContext::SetObjCSuperClass(
8313 const CompilerType &type, const CompilerType &superclass_clang_type) {
8314 ClangASTContext *ast =
8315 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8316 if (!ast)
8317 return false;
8318 clang::ASTContext *clang_ast = ast->getASTContext();
8319
8320 if (type && superclass_clang_type.IsValid() &&
8321 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8322 clang::ObjCInterfaceDecl *class_interface_decl =
8323 GetAsObjCInterfaceDecl(type);
8324 clang::ObjCInterfaceDecl *super_interface_decl =
8325 GetAsObjCInterfaceDecl(superclass_clang_type);
8326 if (class_interface_decl && super_interface_decl) {
8327 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8328 clang_ast->getObjCInterfaceType(super_interface_decl)));
8329 return true;
8330 }
8331 }
8332 return false;
8333}
8334
8335bool ClangASTContext::AddObjCClassProperty(
8336 const CompilerType &type, const char *property_name,
8337 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8338 const char *property_setter_name, const char *property_getter_name,
8339 uint32_t property_attributes, ClangASTMetadata *metadata) {
8340 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8341 property_name[0] == '\0')
8342 return false;
8343 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8344 if (!ast)
8345 return false;
8346 clang::ASTContext *clang_ast = ast->getASTContext();
8347
8348 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8349
8350 if (class_interface_decl) {
8351 CompilerType property_clang_type_to_access;
8352
8353 if (property_clang_type.IsValid())
8354 property_clang_type_to_access = property_clang_type;
8355 else if (ivar_decl)
8356 property_clang_type_to_access =
Alex Langfordbddab072019-08-13 19:40:36 +00008357 CompilerType(ast, ivar_decl->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008358
8359 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8360 clang::TypeSourceInfo *prop_type_source;
8361 if (ivar_decl)
8362 prop_type_source =
8363 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8364 else
8365 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8366 ClangUtil::GetQualType(property_clang_type));
8367
8368 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8369 *clang_ast, class_interface_decl,
8370 clang::SourceLocation(), // Source Location
8371 &clang_ast->Idents.get(property_name),
8372 clang::SourceLocation(), // Source Location for AT
8373 clang::SourceLocation(), // Source location for (
8374 ivar_decl ? ivar_decl->getType()
8375 : ClangUtil::GetQualType(property_clang_type),
8376 prop_type_source);
8377
8378 if (property_decl) {
8379 if (metadata)
8380 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8381
8382 class_interface_decl->addDecl(property_decl);
8383
8384 clang::Selector setter_sel, getter_sel;
8385
8386 if (property_setter_name != nullptr) {
8387 std::string property_setter_no_colon(
8388 property_setter_name, strlen(property_setter_name) - 1);
8389 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008390 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008391 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8392 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8393 std::string setter_sel_string("set");
8394 setter_sel_string.push_back(::toupper(property_name[0]));
8395 setter_sel_string.append(&property_name[1]);
8396 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008397 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008398 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8399 }
8400 property_decl->setSetterName(setter_sel);
8401 property_decl->setPropertyAttributes(
8402 clang::ObjCPropertyDecl::OBJC_PR_setter);
8403
8404 if (property_getter_name != nullptr) {
8405 clang::IdentifierInfo *getter_ident =
8406 &clang_ast->Idents.get(property_getter_name);
8407 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8408 } else {
8409 clang::IdentifierInfo *getter_ident =
8410 &clang_ast->Idents.get(property_name);
8411 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8412 }
8413 property_decl->setGetterName(getter_sel);
8414 property_decl->setPropertyAttributes(
8415 clang::ObjCPropertyDecl::OBJC_PR_getter);
8416
8417 if (ivar_decl)
8418 property_decl->setPropertyIvarDecl(ivar_decl);
8419
8420 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8421 property_decl->setPropertyAttributes(
8422 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8423 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8424 property_decl->setPropertyAttributes(
8425 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8426 if (property_attributes & DW_APPLE_PROPERTY_assign)
8427 property_decl->setPropertyAttributes(
8428 clang::ObjCPropertyDecl::OBJC_PR_assign);
8429 if (property_attributes & DW_APPLE_PROPERTY_retain)
8430 property_decl->setPropertyAttributes(
8431 clang::ObjCPropertyDecl::OBJC_PR_retain);
8432 if (property_attributes & DW_APPLE_PROPERTY_copy)
8433 property_decl->setPropertyAttributes(
8434 clang::ObjCPropertyDecl::OBJC_PR_copy);
8435 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8436 property_decl->setPropertyAttributes(
8437 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8438 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8439 property_decl->setPropertyAttributes(
8440 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8441 if (property_attributes &
8442 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8443 property_decl->setPropertyAttributes(
8444 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8445 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8446 property_decl->setPropertyAttributes(
8447 clang::ObjCPropertyDecl::OBJC_PR_class);
8448
8449 const bool isInstance =
8450 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8451
8452 if (!getter_sel.isNull() &&
8453 !(isInstance
8454 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8455 : class_interface_decl->lookupClassMethod(getter_sel))) {
8456 const bool isVariadic = false;
Adrian Prantl454acae2019-11-08 08:58:50 -08008457 const bool isPropertyAccessor = false;
8458 const bool isSynthesizedAccessorStub = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008459 const bool isImplicitlyDeclared = true;
8460 const bool isDefined = false;
8461 const clang::ObjCMethodDecl::ImplementationControl impControl =
8462 clang::ObjCMethodDecl::None;
8463 const bool HasRelatedResultType = false;
8464
8465 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8466 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8467 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8468 nullptr, class_interface_decl, isInstance, isVariadic,
Adrian Prantl454acae2019-11-08 08:58:50 -08008469 isPropertyAccessor, isSynthesizedAccessorStub,
8470 isImplicitlyDeclared, isDefined, impControl,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008471 HasRelatedResultType);
8472
8473 if (getter && metadata)
8474 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8475
8476 if (getter) {
8477 getter->setMethodParams(*clang_ast,
8478 llvm::ArrayRef<clang::ParmVarDecl *>(),
8479 llvm::ArrayRef<clang::SourceLocation>());
8480
8481 class_interface_decl->addDecl(getter);
8482 }
8483 }
8484
8485 if (!setter_sel.isNull() &&
8486 !(isInstance
8487 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8488 : class_interface_decl->lookupClassMethod(setter_sel))) {
8489 clang::QualType result_type = clang_ast->VoidTy;
8490 const bool isVariadic = false;
Adrian Prantl454acae2019-11-08 08:58:50 -08008491 const bool isPropertyAccessor = true;
8492 const bool isSynthesizedAccessorStub = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008493 const bool isImplicitlyDeclared = true;
8494 const bool isDefined = false;
8495 const clang::ObjCMethodDecl::ImplementationControl impControl =
8496 clang::ObjCMethodDecl::None;
8497 const bool HasRelatedResultType = false;
8498
8499 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8500 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8501 setter_sel, result_type, nullptr, class_interface_decl,
Adrian Prantl454acae2019-11-08 08:58:50 -08008502 isInstance, isVariadic, isPropertyAccessor,
8503 isSynthesizedAccessorStub, isImplicitlyDeclared, isDefined,
8504 impControl, HasRelatedResultType);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008505
8506 if (setter && metadata)
8507 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8508
8509 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8510
8511 params.push_back(clang::ParmVarDecl::Create(
8512 *clang_ast, setter, clang::SourceLocation(),
8513 clang::SourceLocation(),
8514 nullptr, // anonymous
8515 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8516 clang::SC_Auto, nullptr));
8517
8518 if (setter) {
8519 setter->setMethodParams(
8520 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8521 llvm::ArrayRef<clang::SourceLocation>());
8522
8523 class_interface_decl->addDecl(setter);
8524 }
8525 }
8526
8527 return true;
8528 }
8529 }
8530 }
8531 return false;
8532}
8533
8534bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8535 bool check_superclass) {
8536 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8537 if (class_interface_decl)
8538 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8539 return false;
8540}
8541
8542clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8543 const CompilerType &type,
8544 const char *name, // the full symbol name as seen in the symbol table
8545 // (lldb::opaque_compiler_type_t type, "-[NString
8546 // stringWithCString:]")
8547 const CompilerType &method_clang_type, lldb::AccessType access,
8548 bool is_artificial, bool is_variadic) {
8549 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008550 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008551
Kate Stoneb9c1b512016-09-06 20:57:50 +00008552 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8553
8554 if (class_interface_decl == nullptr)
8555 return nullptr;
8556 ClangASTContext *lldb_ast =
8557 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8558 if (lldb_ast == nullptr)
8559 return nullptr;
8560 clang::ASTContext *ast = lldb_ast->getASTContext();
8561
8562 const char *selector_start = ::strchr(name, ' ');
8563 if (selector_start == nullptr)
8564 return nullptr;
8565
8566 selector_start++;
8567 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8568
8569 size_t len = 0;
8570 const char *start;
8571 // printf ("name = '%s'\n", name);
8572
8573 unsigned num_selectors_with_args = 0;
8574 for (start = selector_start; start && *start != '\0' && *start != ']';
8575 start += len) {
8576 len = ::strcspn(start, ":]");
8577 bool has_arg = (start[len] == ':');
8578 if (has_arg)
8579 ++num_selectors_with_args;
8580 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8581 if (has_arg)
8582 len += 1;
8583 }
8584
8585 if (selector_idents.size() == 0)
8586 return nullptr;
8587
8588 clang::Selector method_selector = ast->Selectors.getSelector(
8589 num_selectors_with_args ? selector_idents.size() : 0,
8590 selector_idents.data());
8591
8592 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8593
8594 // Populate the method decl with parameter decls
8595 const clang::Type *method_type(method_qual_type.getTypePtr());
8596
8597 if (method_type == nullptr)
8598 return nullptr;
8599
8600 const clang::FunctionProtoType *method_function_prototype(
8601 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8602
8603 if (!method_function_prototype)
8604 return nullptr;
8605
Adrian Prantl454acae2019-11-08 08:58:50 -08008606 const bool isInstance = (name[0] == '-');
Adrian Prantl8204d9f2019-11-08 09:52:58 -08008607 const bool isVariadic = is_variadic;
Adrian Prantl454acae2019-11-08 08:58:50 -08008608 const bool isPropertyAccessor = false;
8609 const bool isSynthesizedAccessorStub = false;
8610 /// Force this to true because we don't have source locations.
8611 const bool isImplicitlyDeclared = true;
8612 const bool isDefined = false;
8613 const clang::ObjCMethodDecl::ImplementationControl impControl =
Kate Stoneb9c1b512016-09-06 20:57:50 +00008614 clang::ObjCMethodDecl::None;
Adrian Prantl454acae2019-11-08 08:58:50 -08008615 const bool HasRelatedResultType = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008616
8617 const unsigned num_args = method_function_prototype->getNumParams();
8618
8619 if (num_args != num_selectors_with_args)
8620 return nullptr; // some debug information is corrupt. We are not going to
8621 // deal with it.
8622
8623 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8624 *ast,
8625 clang::SourceLocation(), // beginLoc,
8626 clang::SourceLocation(), // endLoc,
8627 method_selector, method_function_prototype->getReturnType(),
8628 nullptr, // TypeSourceInfo *ResultTInfo,
8629 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8630 ClangUtil::GetQualType(type)),
Adrian Prantl454acae2019-11-08 08:58:50 -08008631 isInstance, isVariadic, isPropertyAccessor, isSynthesizedAccessorStub,
8632 isImplicitlyDeclared, isDefined, impControl, HasRelatedResultType);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008633
8634 if (objc_method_decl == nullptr)
8635 return nullptr;
8636
8637 if (num_args > 0) {
8638 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8639
8640 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8641 params.push_back(clang::ParmVarDecl::Create(
8642 *ast, objc_method_decl, clang::SourceLocation(),
8643 clang::SourceLocation(),
8644 nullptr, // anonymous
8645 method_function_prototype->getParamType(param_index), nullptr,
8646 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008647 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008648
Kate Stoneb9c1b512016-09-06 20:57:50 +00008649 objc_method_decl->setMethodParams(
8650 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8651 llvm::ArrayRef<clang::SourceLocation>());
8652 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008653
Kate Stoneb9c1b512016-09-06 20:57:50 +00008654 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008655
Greg Claytond8d4a572015-08-11 21:38:15 +00008656#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008657 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008658#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008659
8660 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008661}
8662
Kate Stoneb9c1b512016-09-06 20:57:50 +00008663bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8664 bool has_extern) {
8665 if (!type)
8666 return false;
8667
8668 clang::QualType qual_type(GetCanonicalQualType(type));
8669
8670 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8671 switch (type_class) {
8672 case clang::Type::Record: {
8673 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8674 if (cxx_record_decl) {
8675 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8676 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8677 return true;
8678 }
8679 } break;
8680
8681 case clang::Type::Enum: {
8682 clang::EnumDecl *enum_decl =
8683 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8684 if (enum_decl) {
8685 enum_decl->setHasExternalLexicalStorage(has_extern);
8686 enum_decl->setHasExternalVisibleStorage(has_extern);
8687 return true;
8688 }
8689 } break;
8690
8691 case clang::Type::ObjCObject:
8692 case clang::Type::ObjCInterface: {
8693 const clang::ObjCObjectType *objc_class_type =
8694 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8695 assert(objc_class_type);
8696 if (objc_class_type) {
8697 clang::ObjCInterfaceDecl *class_interface_decl =
8698 objc_class_type->getInterface();
8699
8700 if (class_interface_decl) {
8701 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8702 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8703 return true;
8704 }
8705 }
8706 } break;
8707
8708 case clang::Type::Typedef:
8709 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8710 ->getDecl()
8711 ->getUnderlyingType()
8712 .getAsOpaquePtr(),
8713 has_extern);
8714
8715 case clang::Type::Auto:
8716 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8717 ->getDeducedType()
8718 .getAsOpaquePtr(),
8719 has_extern);
8720
8721 case clang::Type::Elaborated:
8722 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8723 ->getNamedType()
8724 .getAsOpaquePtr(),
8725 has_extern);
8726
8727 case clang::Type::Paren:
8728 return SetHasExternalStorage(
8729 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8730 has_extern);
8731
8732 default:
8733 break;
8734 }
8735 return false;
8736}
Greg Claytond8d4a572015-08-11 21:38:15 +00008737
8738#pragma mark TagDecl
8739
Kate Stoneb9c1b512016-09-06 20:57:50 +00008740bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8741 clang::QualType qual_type(ClangUtil::GetQualType(type));
8742 if (!qual_type.isNull()) {
8743 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8744 if (tag_type) {
8745 clang::TagDecl *tag_decl = tag_type->getDecl();
8746 if (tag_decl) {
8747 tag_decl->startDefinition();
8748 return true;
8749 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008750 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008751
8752 const clang::ObjCObjectType *object_type =
8753 qual_type->getAs<clang::ObjCObjectType>();
8754 if (object_type) {
8755 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8756 if (interface_decl) {
8757 interface_decl->startDefinition();
8758 return true;
8759 }
8760 }
8761 }
8762 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008763}
8764
Kate Stoneb9c1b512016-09-06 20:57:50 +00008765bool ClangASTContext::CompleteTagDeclarationDefinition(
8766 const CompilerType &type) {
8767 clang::QualType qual_type(ClangUtil::GetQualType(type));
8768 if (!qual_type.isNull()) {
8769 // Make sure we use the same methodology as
Adrian Prantl05097242018-04-30 16:49:04 +00008770 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end
8771 // the definition. Previously we were calling
Kate Stoneb9c1b512016-09-06 20:57:50 +00008772 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8773 if (tag_type) {
8774 clang::TagDecl *tag_decl = tag_type->getDecl();
8775 if (tag_decl) {
8776 clang::CXXRecordDecl *cxx_record_decl =
8777 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8778
8779 if (cxx_record_decl) {
8780 if (!cxx_record_decl->isCompleteDefinition())
8781 cxx_record_decl->completeDefinition();
8782 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8783 cxx_record_decl->setHasExternalLexicalStorage(false);
8784 cxx_record_decl->setHasExternalVisibleStorage(false);
8785 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008786 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008787 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008788 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008789
8790 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8791
8792 if (enutype) {
8793 clang::EnumDecl *enum_decl = enutype->getDecl();
8794
8795 if (enum_decl) {
8796 if (!enum_decl->isCompleteDefinition()) {
8797 ClangASTContext *lldb_ast =
8798 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8799 if (lldb_ast == nullptr)
8800 return false;
8801 clang::ASTContext *ast = lldb_ast->getASTContext();
8802
8803 /// TODO This really needs to be fixed.
8804
8805 QualType integer_type(enum_decl->getIntegerType());
8806 if (!integer_type.isNull()) {
8807 unsigned NumPositiveBits = 1;
8808 unsigned NumNegativeBits = 0;
8809
8810 clang::QualType promotion_qual_type;
8811 // If the enum integer type is less than an integer in bit width,
8812 // then we must promote it to an integer size.
8813 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8814 ast->getTypeSize(ast->IntTy)) {
8815 if (enum_decl->getIntegerType()->isSignedIntegerType())
8816 promotion_qual_type = ast->IntTy;
8817 else
8818 promotion_qual_type = ast->UnsignedIntTy;
8819 } else
8820 promotion_qual_type = enum_decl->getIntegerType();
8821
8822 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8823 promotion_qual_type, NumPositiveBits,
8824 NumNegativeBits);
8825 }
8826 }
8827 return true;
8828 }
8829 }
8830 }
8831 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008832}
8833
Aleksandr Urakov709426b2018-09-10 08:08:43 +00008834clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008835 const CompilerType &enum_type, const Declaration &decl, const char *name,
Zachary Turner1639c6b2018-12-17 16:15:13 +00008836 const llvm::APSInt &value) {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008837
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008838 if (!enum_type || ConstString(name).IsEmpty())
8839 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008840
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008841 lldbassert(enum_type.GetTypeSystem() == static_cast<TypeSystem *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +00008842
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008843 lldb::opaque_compiler_type_t enum_opaque_compiler_type =
8844 enum_type.GetOpaqueQualType();
8845
8846 if (!enum_opaque_compiler_type)
8847 return nullptr;
8848
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008849 clang::QualType enum_qual_type(
8850 GetCanonicalQualType(enum_opaque_compiler_type));
8851
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008852 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8853
8854 if (!clang_type)
8855 return nullptr;
8856
8857 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8858
8859 if (!enutype)
8860 return nullptr;
8861
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008862 clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create(
8863 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8864 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
Zachary Turner1639c6b2018-12-17 16:15:13 +00008865 clang::QualType(enutype, 0), nullptr, value);
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008866
8867 if (!enumerator_decl)
8868 return nullptr;
8869
8870 enutype->getDecl()->addDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008871
8872#ifdef LLDB_CONFIGURATION_DEBUG
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008873 VerifyDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008874#endif
8875
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008876 return enumerator_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008877}
8878
Zachary Turner1639c6b2018-12-17 16:15:13 +00008879clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
8880 const CompilerType &enum_type, const Declaration &decl, const char *name,
8881 int64_t enum_value, uint32_t enum_value_bit_size) {
8882 CompilerType underlying_type =
8883 GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
8884 bool is_signed = false;
8885 underlying_type.IsIntegerType(is_signed);
8886
8887 llvm::APSInt value(enum_value_bit_size, is_signed);
8888 value = enum_value;
8889
8890 return AddEnumerationValueToEnumerationType(enum_type, decl, name, value);
8891}
8892
Greg Claytona1e5dc82015-08-11 22:53:00 +00008893CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00008894ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8895 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8896 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8897 if (clang_type) {
8898 const clang::EnumType *enutype =
8899 llvm::dyn_cast<clang::EnumType>(clang_type);
8900 if (enutype) {
8901 clang::EnumDecl *enum_decl = enutype->getDecl();
8902 if (enum_decl)
Alex Langfordbddab072019-08-13 19:40:36 +00008903 return CompilerType(this, enum_decl->getIntegerType().getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00008904 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008905 }
8906 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008907}
8908
Kate Stoneb9c1b512016-09-06 20:57:50 +00008909CompilerType
8910ClangASTContext::CreateMemberPointerType(const CompilerType &type,
8911 const CompilerType &pointee_type) {
8912 if (type && pointee_type.IsValid() &&
8913 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8914 ClangASTContext *ast =
8915 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8916 if (!ast)
8917 return CompilerType();
Alex Langfordbddab072019-08-13 19:40:36 +00008918 return CompilerType(ast, ast->getASTContext()
8919 ->getMemberPointerType(
8920 ClangUtil::GetQualType(pointee_type),
8921 ClangUtil::GetQualType(type).getTypePtr())
8922 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008923 }
8924 return CompilerType();
8925}
Greg Claytond8d4a572015-08-11 21:38:15 +00008926
Greg Claytond8d4a572015-08-11 21:38:15 +00008927// Dumping types
Greg Claytond8d4a572015-08-11 21:38:15 +00008928#define DEPTH_INCREMENT 2
8929
Adrian Prantl0c72a422019-03-07 20:20:02 +00008930#ifndef NDEBUG
8931LLVM_DUMP_METHOD void
8932ClangASTContext::dump(lldb::opaque_compiler_type_t type) const {
8933 if (!type)
8934 return;
8935 clang::QualType qual_type(GetQualType(type));
8936 qual_type.dump();
8937}
8938#endif
8939
Zachary Turner49110232018-11-05 17:40:28 +00008940void ClangASTContext::Dump(Stream &s) {
Zachary Turner115209e2018-11-05 19:25:39 +00008941 Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl());
Zachary Turner49110232018-11-05 17:40:28 +00008942 tu->dump(s.AsRawOstream());
8943}
8944
Shafik Yaghmour5f469822019-10-11 16:36:20 +00008945void ClangASTContext::DumpFromSymbolFile(Stream &s,
8946 llvm::StringRef symbol_name) {
8947 SymbolFile *symfile = GetSymbolFile();
8948
8949 if (!symfile)
8950 return;
8951
8952 lldb_private::TypeList type_list;
8953 symfile->GetTypes(nullptr, eTypeClassAny, type_list);
8954 size_t ntypes = type_list.GetSize();
8955
8956 for (size_t i = 0; i < ntypes; ++i) {
8957 TypeSP type = type_list.GetTypeAtIndex(i);
8958
8959 if (!symbol_name.empty())
shafikde2c7ca2019-10-28 14:26:54 -07008960 if (symbol_name != type->GetName().GetStringRef())
Shafik Yaghmour5f469822019-10-11 16:36:20 +00008961 continue;
8962
8963 s << type->GetName().AsCString() << "\n";
8964
8965 if (clang::TagDecl *tag_decl =
8966 GetAsTagDecl(type->GetFullCompilerType()))
8967 tag_decl->dump(s.AsRawOstream());
8968 else if (clang::TypedefNameDecl *typedef_decl =
8969 GetAsTypedefDecl(type->GetFullCompilerType()))
8970 typedef_decl->dump(s.AsRawOstream());
8971 else {
8972 GetCanonicalQualType(type->GetFullCompilerType().GetOpaqueQualType())
8973 .dump(s.AsRawOstream());
8974 }
8975 }
8976}
8977
Kate Stoneb9c1b512016-09-06 20:57:50 +00008978void ClangASTContext::DumpValue(
8979 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00008980 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008981 lldb::offset_t data_byte_offset, size_t data_byte_size,
8982 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
8983 bool show_summary, bool verbose, uint32_t depth) {
8984 if (!type)
8985 return;
8986
8987 clang::QualType qual_type(GetQualType(type));
8988 switch (qual_type->getTypeClass()) {
8989 case clang::Type::Record:
8990 if (GetCompleteType(type)) {
8991 const clang::RecordType *record_type =
8992 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8993 const clang::RecordDecl *record_decl = record_type->getDecl();
8994 assert(record_decl);
8995 uint32_t field_bit_offset = 0;
8996 uint32_t field_byte_offset = 0;
8997 const clang::ASTRecordLayout &record_layout =
8998 getASTContext()->getASTRecordLayout(record_decl);
8999 uint32_t child_idx = 0;
9000
9001 const clang::CXXRecordDecl *cxx_record_decl =
9002 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9003 if (cxx_record_decl) {
9004 // We might have base classes to print out first
9005 clang::CXXRecordDecl::base_class_const_iterator base_class,
9006 base_class_end;
9007 for (base_class = cxx_record_decl->bases_begin(),
9008 base_class_end = cxx_record_decl->bases_end();
9009 base_class != base_class_end; ++base_class) {
9010 const clang::CXXRecordDecl *base_class_decl =
9011 llvm::cast<clang::CXXRecordDecl>(
9012 base_class->getType()->getAs<clang::RecordType>()->getDecl());
9013
9014 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00009015 if (!verbose && !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00009016 continue;
9017
9018 if (base_class->isVirtual())
9019 field_bit_offset =
9020 record_layout.getVBaseClassOffset(base_class_decl)
9021 .getQuantity() *
9022 8;
9023 else
9024 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
9025 .getQuantity() *
9026 8;
9027 field_byte_offset = field_bit_offset / 8;
9028 assert(field_bit_offset % 8 == 0);
9029 if (child_idx == 0)
9030 s->PutChar('{');
9031 else
9032 s->PutChar(',');
9033
9034 clang::QualType base_class_qual_type = base_class->getType();
9035 std::string base_class_type_name(base_class_qual_type.getAsString());
9036
9037 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00009038 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
9039 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009040
9041 clang::TypeInfo base_class_type_info =
9042 getASTContext()->getTypeInfo(base_class_qual_type);
9043
9044 // Dump the value of the member
Alex Langfordbddab072019-08-13 19:40:36 +00009045 CompilerType base_clang_type(this,
9046 base_class_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009047 base_clang_type.DumpValue(
9048 exe_ctx,
9049 s, // Stream to dump to
9050 base_clang_type
9051 .GetFormat(), // The format with which to display the member
9052 data, // Data buffer containing all bytes for this type
9053 data_byte_offset + field_byte_offset, // Offset into "data" where
9054 // to grab value from
9055 base_class_type_info.Width / 8, // Size of this type in bytes
9056 0, // Bitfield bit size
9057 0, // Bitfield bit offset
9058 show_types, // Boolean indicating if we should show the variable
9059 // types
9060 show_summary, // Boolean indicating if we should show a summary
9061 // for the current type
9062 verbose, // Verbose output?
9063 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9064 // children
9065
9066 ++child_idx;
9067 }
9068 }
9069 uint32_t field_idx = 0;
9070 clang::RecordDecl::field_iterator field, field_end;
9071 for (field = record_decl->field_begin(),
9072 field_end = record_decl->field_end();
9073 field != field_end; ++field, ++field_idx, ++child_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009074 // Print the starting squiggly bracket (if this is the first member) or
9075 // comma (for member 2 and beyond) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009076 if (child_idx == 0)
9077 s->PutChar('{');
9078 else
9079 s->PutChar(',');
9080
9081 // Indent
9082 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
9083
9084 clang::QualType field_type = field->getType();
9085 // Print the member type if requested
Adrian Prantl05097242018-04-30 16:49:04 +00009086 // Figure out the type byte size (field_type_info.first) and alignment
9087 // (field_type_info.second) from the AST context.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009088 clang::TypeInfo field_type_info =
9089 getASTContext()->getTypeInfo(field_type);
9090 assert(field_idx < record_layout.getFieldCount());
9091 // Figure out the field offset within the current struct/union/class
9092 // type
9093 field_bit_offset = record_layout.getFieldOffset(field_idx);
9094 field_byte_offset = field_bit_offset / 8;
9095 uint32_t field_bitfield_bit_size = 0;
9096 uint32_t field_bitfield_bit_offset = 0;
Raphael Isemann02e91132019-11-20 12:09:19 +01009097 if (FieldIsBitfield(*field, field_bitfield_bit_size))
Kate Stoneb9c1b512016-09-06 20:57:50 +00009098 field_bitfield_bit_offset = field_bit_offset % 8;
9099
9100 if (show_types) {
9101 std::string field_type_name(field_type.getAsString());
9102 if (field_bitfield_bit_size > 0)
9103 s->Printf("(%s:%u) ", field_type_name.c_str(),
9104 field_bitfield_bit_size);
9105 else
9106 s->Printf("(%s) ", field_type_name.c_str());
9107 }
9108 // Print the member name and equal sign
9109 s->Printf("%s = ", field->getNameAsString().c_str());
9110
9111 // Dump the value of the member
Alex Langfordbddab072019-08-13 19:40:36 +00009112 CompilerType field_clang_type(this, field_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009113 field_clang_type.DumpValue(
9114 exe_ctx,
9115 s, // Stream to dump to
9116 field_clang_type
9117 .GetFormat(), // The format with which to display the member
9118 data, // Data buffer containing all bytes for this type
9119 data_byte_offset + field_byte_offset, // Offset into "data" where to
9120 // grab value from
9121 field_type_info.Width / 8, // Size of this type in bytes
9122 field_bitfield_bit_size, // Bitfield bit size
9123 field_bitfield_bit_offset, // Bitfield bit offset
9124 show_types, // Boolean indicating if we should show the variable
9125 // types
9126 show_summary, // Boolean indicating if we should show a summary for
9127 // the current type
9128 verbose, // Verbose output?
9129 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9130 // children
9131 }
9132
9133 // Indent the trailing squiggly bracket
9134 if (child_idx > 0)
9135 s->Printf("\n%*s}", depth, "");
9136 }
9137 return;
9138
9139 case clang::Type::Enum:
9140 if (GetCompleteType(type)) {
9141 const clang::EnumType *enutype =
9142 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9143 const clang::EnumDecl *enum_decl = enutype->getDecl();
9144 assert(enum_decl);
9145 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9146 lldb::offset_t offset = data_byte_offset;
9147 const int64_t enum_value = data.GetMaxU64Bitfield(
9148 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9149 for (enum_pos = enum_decl->enumerator_begin(),
9150 enum_end_pos = enum_decl->enumerator_end();
9151 enum_pos != enum_end_pos; ++enum_pos) {
9152 if (enum_pos->getInitVal() == enum_value) {
9153 s->Printf("%s", enum_pos->getNameAsString().c_str());
9154 return;
9155 }
9156 }
Adrian Prantl05097242018-04-30 16:49:04 +00009157 // If we have gotten here we didn't get find the enumerator in the enum
9158 // decl, so just print the integer.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009159 s->Printf("%" PRIi64, enum_value);
9160 }
9161 return;
9162
9163 case clang::Type::ConstantArray: {
9164 const clang::ConstantArrayType *array =
9165 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9166 bool is_array_of_characters = false;
9167 clang::QualType element_qual_type = array->getElementType();
9168
9169 const clang::Type *canonical_type =
9170 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9171 if (canonical_type)
9172 is_array_of_characters = canonical_type->isCharType();
9173
9174 const uint64_t element_count = array->getSize().getLimitedValue();
9175
9176 clang::TypeInfo field_type_info =
9177 getASTContext()->getTypeInfo(element_qual_type);
9178
9179 uint32_t element_idx = 0;
9180 uint32_t element_offset = 0;
9181 uint64_t element_byte_size = field_type_info.Width / 8;
9182 uint32_t element_stride = element_byte_size;
9183
9184 if (is_array_of_characters) {
9185 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009186 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9187 element_byte_size, element_count, UINT32_MAX,
9188 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009189 s->PutChar('"');
9190 return;
9191 } else {
Alex Langfordbddab072019-08-13 19:40:36 +00009192 CompilerType element_clang_type(this, element_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009193 lldb::Format element_format = element_clang_type.GetFormat();
9194
9195 for (element_idx = 0; element_idx < element_count; ++element_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009196 // Print the starting squiggly bracket (if this is the first member) or
9197 // comman (for member 2 and beyong) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009198 if (element_idx == 0)
9199 s->PutChar('{');
9200 else
9201 s->PutChar(',');
9202
9203 // Indent and print the index
9204 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9205
9206 // Figure out the field offset within the current struct/union/class
9207 // type
9208 element_offset = element_idx * element_stride;
9209
9210 // Dump the value of the member
9211 element_clang_type.DumpValue(
9212 exe_ctx,
9213 s, // Stream to dump to
9214 element_format, // The format with which to display the element
9215 data, // Data buffer containing all bytes for this type
9216 data_byte_offset +
9217 element_offset, // Offset into "data" where to grab value from
9218 element_byte_size, // Size of this type in bytes
9219 0, // Bitfield bit size
9220 0, // Bitfield bit offset
9221 show_types, // Boolean indicating if we should show the variable
9222 // types
9223 show_summary, // Boolean indicating if we should show a summary for
9224 // the current type
9225 verbose, // Verbose output?
9226 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9227 // children
9228 }
9229
9230 // Indent the trailing squiggly bracket
9231 if (element_idx > 0)
9232 s->Printf("\n%*s}", depth, "");
9233 }
9234 }
9235 return;
9236
9237 case clang::Type::Typedef: {
9238 clang::QualType typedef_qual_type =
9239 llvm::cast<clang::TypedefType>(qual_type)
9240 ->getDecl()
9241 ->getUnderlyingType();
9242
Alex Langfordbddab072019-08-13 19:40:36 +00009243 CompilerType typedef_clang_type(this, typedef_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009244 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9245 clang::TypeInfo typedef_type_info =
9246 getASTContext()->getTypeInfo(typedef_qual_type);
9247 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9248
9249 return typedef_clang_type.DumpValue(
9250 exe_ctx,
9251 s, // Stream to dump to
9252 typedef_format, // The format with which to display the element
9253 data, // Data buffer containing all bytes for this type
9254 data_byte_offset, // Offset into "data" where to grab value from
9255 typedef_byte_size, // Size of this type in bytes
9256 bitfield_bit_size, // Bitfield bit size
9257 bitfield_bit_offset, // Bitfield bit offset
9258 show_types, // Boolean indicating if we should show the variable types
9259 show_summary, // Boolean indicating if we should show a summary for the
9260 // current type
9261 verbose, // Verbose output?
9262 depth); // Scope depth for any types that have children
9263 } break;
9264
9265 case clang::Type::Auto: {
9266 clang::QualType elaborated_qual_type =
9267 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
Alex Langfordbddab072019-08-13 19:40:36 +00009268 CompilerType elaborated_clang_type(this,
9269 elaborated_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009270 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9271 clang::TypeInfo elaborated_type_info =
9272 getASTContext()->getTypeInfo(elaborated_qual_type);
9273 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9274
9275 return elaborated_clang_type.DumpValue(
9276 exe_ctx,
9277 s, // Stream to dump to
9278 elaborated_format, // The format with which to display the element
9279 data, // Data buffer containing all bytes for this type
9280 data_byte_offset, // Offset into "data" where to grab value from
9281 elaborated_byte_size, // Size of this type in bytes
9282 bitfield_bit_size, // Bitfield bit size
9283 bitfield_bit_offset, // Bitfield bit offset
9284 show_types, // Boolean indicating if we should show the variable types
9285 show_summary, // Boolean indicating if we should show a summary for the
9286 // current type
9287 verbose, // Verbose output?
9288 depth); // Scope depth for any types that have children
9289 } break;
9290
9291 case clang::Type::Elaborated: {
9292 clang::QualType elaborated_qual_type =
9293 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
Alex Langfordbddab072019-08-13 19:40:36 +00009294 CompilerType elaborated_clang_type(this,
9295 elaborated_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009296 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9297 clang::TypeInfo elaborated_type_info =
9298 getASTContext()->getTypeInfo(elaborated_qual_type);
9299 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9300
9301 return elaborated_clang_type.DumpValue(
9302 exe_ctx,
9303 s, // Stream to dump to
9304 elaborated_format, // The format with which to display the element
9305 data, // Data buffer containing all bytes for this type
9306 data_byte_offset, // Offset into "data" where to grab value from
9307 elaborated_byte_size, // Size of this type in bytes
9308 bitfield_bit_size, // Bitfield bit size
9309 bitfield_bit_offset, // Bitfield bit offset
9310 show_types, // Boolean indicating if we should show the variable types
9311 show_summary, // Boolean indicating if we should show a summary for the
9312 // current type
9313 verbose, // Verbose output?
9314 depth); // Scope depth for any types that have children
9315 } break;
9316
9317 case clang::Type::Paren: {
9318 clang::QualType desugar_qual_type =
9319 llvm::cast<clang::ParenType>(qual_type)->desugar();
Alex Langfordbddab072019-08-13 19:40:36 +00009320 CompilerType desugar_clang_type(this, desugar_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009321
9322 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9323 clang::TypeInfo desugar_type_info =
9324 getASTContext()->getTypeInfo(desugar_qual_type);
9325 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9326
9327 return desugar_clang_type.DumpValue(
9328 exe_ctx,
9329 s, // Stream to dump to
9330 desugar_format, // The format with which to display the element
9331 data, // Data buffer containing all bytes for this type
9332 data_byte_offset, // Offset into "data" where to grab value from
9333 desugar_byte_size, // Size of this type in bytes
9334 bitfield_bit_size, // Bitfield bit size
9335 bitfield_bit_offset, // Bitfield bit offset
9336 show_types, // Boolean indicating if we should show the variable types
9337 show_summary, // Boolean indicating if we should show a summary for the
9338 // current type
9339 verbose, // Verbose output?
9340 depth); // Scope depth for any types that have children
9341 } break;
9342
9343 default:
9344 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009345 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9346 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9347 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009348
9349 if (show_summary)
9350 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9351 break;
9352 }
9353}
9354
Frederic Rissd6470fb2019-10-08 15:35:58 +00009355static bool DumpEnumValue(const clang::QualType &qual_type, Stream *s,
9356 const DataExtractor &data, lldb::offset_t byte_offset,
9357 size_t byte_size, uint32_t bitfield_bit_offset,
9358 uint32_t bitfield_bit_size) {
9359 const clang::EnumType *enutype =
9360 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9361 const clang::EnumDecl *enum_decl = enutype->getDecl();
9362 assert(enum_decl);
Frederic Rissd6470fb2019-10-08 15:35:58 +00009363 lldb::offset_t offset = byte_offset;
Frederic Riss41ff3962019-10-08 15:35:59 +00009364 const uint64_t enum_svalue = data.GetMaxS64Bitfield(
Frederic Rissd6470fb2019-10-08 15:35:58 +00009365 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
Frederic Riss41ff3962019-10-08 15:35:59 +00009366 bool can_be_bitfield = true;
9367 uint64_t covered_bits = 0;
9368 int num_enumerators = 0;
Frederic Rissd6470fb2019-10-08 15:35:58 +00009369
Frederic Riss41ff3962019-10-08 15:35:59 +00009370 // Try to find an exact match for the value.
9371 // At the same time, we're applying a heuristic to determine whether we want
9372 // to print this enum as a bitfield. We're likely dealing with a bitfield if
9373 // every enumrator is either a one bit value or a superset of the previous
9374 // enumerators. Also 0 doesn't make sense when the enumerators are used as
9375 // flags.
Frederic Rissd6470fb2019-10-08 15:35:58 +00009376 for (auto enumerator : enum_decl->enumerators()) {
Frederic Riss41ff3962019-10-08 15:35:59 +00009377 uint64_t val = enumerator->getInitVal().getSExtValue();
Frederic Riss5d415b72019-10-08 17:59:02 +00009378 val = llvm::SignExtend64(val, 8*byte_size);
Frederic Riss41ff3962019-10-08 15:35:59 +00009379 if (llvm::countPopulation(val) != 1 && (val & ~covered_bits) != 0)
9380 can_be_bitfield = false;
9381 covered_bits |= val;
9382 ++num_enumerators;
9383 if (val == enum_svalue) {
9384 // Found an exact match, that's all we need to do.
Frederic Rissd6470fb2019-10-08 15:35:58 +00009385 s->PutCString(enumerator->getNameAsString());
9386 return true;
9387 }
9388 }
Frederic Riss41ff3962019-10-08 15:35:59 +00009389
Frederic Riss41ff3962019-10-08 15:35:59 +00009390 // Unsigned values make more sense for flags.
9391 offset = byte_offset;
9392 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9393 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9394
Frederic Rissb56e3a12019-10-08 19:52:01 +00009395 // No exact match, but we don't think this is a bitfield. Print the value as
9396 // decimal.
9397 if (!can_be_bitfield) {
9398 if (qual_type->isSignedIntegerOrEnumerationType())
9399 s->Printf("%" PRIi64, enum_svalue);
9400 else
9401 s->Printf("%" PRIu64, enum_uvalue);
9402 return true;
9403 }
9404
Frederic Riss41ff3962019-10-08 15:35:59 +00009405 uint64_t remaining_value = enum_uvalue;
9406 std::vector<std::pair<uint64_t, llvm::StringRef>> values;
9407 values.reserve(num_enumerators);
9408 for (auto enumerator : enum_decl->enumerators())
9409 if (auto val = enumerator->getInitVal().getZExtValue())
9410 values.emplace_back(val, enumerator->getName());
9411
9412 // Sort in reverse order of the number of the population count, so that in
9413 // `enum {A, B, ALL = A|B }` we visit ALL first. Use a stable sort so that
9414 // A | C where A is declared before C is displayed in this order.
9415 std::stable_sort(values.begin(), values.end(), [](const auto &a, const auto &b) {
9416 return llvm::countPopulation(a.first) > llvm::countPopulation(b.first);
9417 });
9418
9419 for (const auto &val : values) {
9420 if ((remaining_value & val.first) != val.first)
9421 continue;
9422 remaining_value &= ~val.first;
9423 s->PutCString(val.second);
9424 if (remaining_value)
9425 s->PutCString(" | ");
9426 }
9427
9428 // If there is a remainder that is not covered by the value, print it as hex.
9429 if (remaining_value)
9430 s->Printf("0x%" PRIx64, remaining_value);
9431
Frederic Rissd6470fb2019-10-08 15:35:58 +00009432 return true;
9433}
9434
Kate Stoneb9c1b512016-09-06 20:57:50 +00009435bool ClangASTContext::DumpTypeValue(
9436 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009437 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9438 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009439 ExecutionContextScope *exe_scope) {
9440 if (!type)
9441 return false;
9442 if (IsAggregateType(type)) {
9443 return false;
9444 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009445 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009446
9447 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Frederic Riss41ff3962019-10-08 15:35:59 +00009448
9449 if (type_class == clang::Type::Elaborated) {
9450 qual_type = llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9451 return DumpTypeValue(qual_type.getAsOpaquePtr(), s, format, data, byte_offset, byte_size,
9452 bitfield_bit_size, bitfield_bit_offset, exe_scope);
9453 }
9454
Kate Stoneb9c1b512016-09-06 20:57:50 +00009455 switch (type_class) {
9456 case clang::Type::Typedef: {
9457 clang::QualType typedef_qual_type =
9458 llvm::cast<clang::TypedefType>(qual_type)
9459 ->getDecl()
9460 ->getUnderlyingType();
Alex Langfordbddab072019-08-13 19:40:36 +00009461 CompilerType typedef_clang_type(this, typedef_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009462 if (format == eFormatDefault)
9463 format = typedef_clang_type.GetFormat();
9464 clang::TypeInfo typedef_type_info =
9465 getASTContext()->getTypeInfo(typedef_qual_type);
9466 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9467
9468 return typedef_clang_type.DumpTypeValue(
9469 s,
9470 format, // The format with which to display the element
9471 data, // Data buffer containing all bytes for this type
9472 byte_offset, // Offset into "data" where to grab value from
9473 typedef_byte_size, // Size of this type in bytes
9474 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9475 // treat as a bitfield
9476 bitfield_bit_offset, // Offset in bits of a bitfield value if
9477 // bitfield_bit_size != 0
9478 exe_scope);
9479 } break;
9480
9481 case clang::Type::Enum:
Adrian Prantl05097242018-04-30 16:49:04 +00009482 // If our format is enum or default, show the enumeration value as its
9483 // enumeration string value, else just display it as requested.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009484 if ((format == eFormatEnum || format == eFormatDefault) &&
Frederic Rissd6470fb2019-10-08 15:35:58 +00009485 GetCompleteType(type))
9486 return DumpEnumValue(qual_type, s, data, byte_offset, byte_size,
9487 bitfield_bit_offset, bitfield_bit_size);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009488 // format was not enum, just fall through and dump the value as
9489 // requested....
9490 LLVM_FALLTHROUGH;
9491
9492 default:
9493 // We are down to a scalar type that we just need to display.
9494 {
9495 uint32_t item_count = 1;
9496 // A few formats, we might need to modify our size and count for
9497 // depending
9498 // on how we are trying to display the value...
9499 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009500 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009501 case eFormatBoolean:
9502 case eFormatBinary:
9503 case eFormatComplex:
9504 case eFormatCString: // NULL terminated C strings
9505 case eFormatDecimal:
9506 case eFormatEnum:
9507 case eFormatHex:
9508 case eFormatHexUppercase:
9509 case eFormatFloat:
9510 case eFormatOctal:
9511 case eFormatOSType:
9512 case eFormatUnsigned:
9513 case eFormatPointer:
9514 case eFormatVectorOfChar:
9515 case eFormatVectorOfSInt8:
9516 case eFormatVectorOfUInt8:
9517 case eFormatVectorOfSInt16:
9518 case eFormatVectorOfUInt16:
9519 case eFormatVectorOfSInt32:
9520 case eFormatVectorOfUInt32:
9521 case eFormatVectorOfSInt64:
9522 case eFormatVectorOfUInt64:
9523 case eFormatVectorOfFloat32:
9524 case eFormatVectorOfFloat64:
9525 case eFormatVectorOfUInt128:
9526 break;
9527
9528 case eFormatChar:
9529 case eFormatCharPrintable:
9530 case eFormatCharArray:
9531 case eFormatBytes:
9532 case eFormatBytesWithASCII:
9533 item_count = byte_size;
9534 byte_size = 1;
9535 break;
9536
9537 case eFormatUnicode16:
9538 item_count = byte_size / 2;
9539 byte_size = 2;
9540 break;
9541
9542 case eFormatUnicode32:
9543 item_count = byte_size / 4;
9544 byte_size = 4;
9545 break;
9546 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009547 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9548 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9549 bitfield_bit_size, bitfield_bit_offset,
9550 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009551 }
9552 break;
9553 }
9554 }
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00009555 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009556}
9557
9558void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9559 ExecutionContext *exe_ctx, Stream *s,
9560 const lldb_private::DataExtractor &data,
9561 lldb::offset_t data_byte_offset,
9562 size_t data_byte_size) {
9563 uint32_t length = 0;
9564 if (IsCStringType(type, length)) {
9565 if (exe_ctx) {
9566 Process *process = exe_ctx->GetProcessPtr();
9567 if (process) {
9568 lldb::offset_t offset = data_byte_offset;
9569 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9570 std::vector<uint8_t> buf;
9571 if (length > 0)
9572 buf.resize(length);
9573 else
9574 buf.resize(256);
9575
Zachary Turner29cb8682017-03-03 20:57:05 +00009576 DataExtractor cstr_data(&buf.front(), buf.size(),
9577 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009578 buf.back() = '\0';
9579 size_t bytes_read;
9580 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009581 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009582 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9583 buf.size(), error)) > 0) {
9584 const size_t len = strlen((const char *)&buf.front());
9585 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009586 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009587 if (total_cstr_len == 0)
9588 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009589 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9590 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009591 total_cstr_len += len;
9592 if (len < buf.size())
9593 break;
9594 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009595 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009596 if (total_cstr_len > 0)
9597 s->PutChar('"');
9598 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009599 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009600 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009601}
9602
Kate Stoneb9c1b512016-09-06 20:57:50 +00009603void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9604 StreamFile s(stdout, false);
9605 DumpTypeDescription(type, &s);
9606 ClangASTMetadata *metadata =
9607 ClangASTContext::GetMetadata(getASTContext(), type);
9608 if (metadata) {
9609 metadata->Dump(&s);
9610 }
9611}
Greg Claytond8d4a572015-08-11 21:38:15 +00009612
Kate Stoneb9c1b512016-09-06 20:57:50 +00009613void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9614 Stream *s) {
9615 if (type) {
9616 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009617
Kate Stoneb9c1b512016-09-06 20:57:50 +00009618 llvm::SmallVector<char, 1024> buf;
9619 llvm::raw_svector_ostream llvm_ostrm(buf);
9620
9621 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9622 switch (type_class) {
9623 case clang::Type::ObjCObject:
9624 case clang::Type::ObjCInterface: {
9625 GetCompleteType(type);
9626
9627 const clang::ObjCObjectType *objc_class_type =
9628 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9629 assert(objc_class_type);
9630 if (objc_class_type) {
9631 clang::ObjCInterfaceDecl *class_interface_decl =
9632 objc_class_type->getInterface();
9633 if (class_interface_decl) {
9634 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9635 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009636 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009637 }
9638 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009639
Kate Stoneb9c1b512016-09-06 20:57:50 +00009640 case clang::Type::Typedef: {
9641 const clang::TypedefType *typedef_type =
9642 qual_type->getAs<clang::TypedefType>();
9643 if (typedef_type) {
9644 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9645 std::string clang_typedef_name(
9646 typedef_decl->getQualifiedNameAsString());
9647 if (!clang_typedef_name.empty()) {
9648 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009649 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009650 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009651 }
9652 } break;
9653
9654 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00009655 CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
9656 ->getDeducedType()
9657 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00009658 .DumpTypeDescription(s);
9659 return;
9660
9661 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00009662 CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
9663 ->getNamedType()
9664 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00009665 .DumpTypeDescription(s);
9666 return;
9667
9668 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00009669 CompilerType(
9670 this,
9671 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00009672 .DumpTypeDescription(s);
9673 return;
9674
9675 case clang::Type::Record: {
9676 GetCompleteType(type);
9677
9678 const clang::RecordType *record_type =
9679 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9680 const clang::RecordDecl *record_decl = record_type->getDecl();
9681 const clang::CXXRecordDecl *cxx_record_decl =
9682 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9683
9684 if (cxx_record_decl)
9685 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9686 s->GetIndentLevel());
9687 else
9688 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9689 s->GetIndentLevel());
9690 } break;
9691
9692 default: {
9693 const clang::TagType *tag_type =
9694 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9695 if (tag_type) {
9696 clang::TagDecl *tag_decl = tag_type->getDecl();
9697 if (tag_decl)
9698 tag_decl->print(llvm_ostrm, 0);
9699 } else {
9700 std::string clang_type_name(qual_type.getAsString());
9701 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009702 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009703 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009704 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009705 }
9706
Kate Stoneb9c1b512016-09-06 20:57:50 +00009707 if (buf.size() > 0) {
9708 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009709 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009710 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009711}
9712
Kate Stoneb9c1b512016-09-06 20:57:50 +00009713void ClangASTContext::DumpTypeName(const CompilerType &type) {
9714 if (ClangUtil::IsClangType(type)) {
9715 clang::QualType qual_type(
9716 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9717
9718 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9719 switch (type_class) {
9720 case clang::Type::Record: {
9721 const clang::CXXRecordDecl *cxx_record_decl =
9722 qual_type->getAsCXXRecordDecl();
9723 if (cxx_record_decl)
9724 printf("class %s", cxx_record_decl->getName().str().c_str());
9725 } break;
9726
9727 case clang::Type::Enum: {
9728 clang::EnumDecl *enum_decl =
9729 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9730 if (enum_decl) {
9731 printf("enum %s", enum_decl->getName().str().c_str());
9732 }
9733 } break;
9734
9735 case clang::Type::ObjCObject:
9736 case clang::Type::ObjCInterface: {
9737 const clang::ObjCObjectType *objc_class_type =
9738 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9739 if (objc_class_type) {
9740 clang::ObjCInterfaceDecl *class_interface_decl =
9741 objc_class_type->getInterface();
Adrian Prantl05097242018-04-30 16:49:04 +00009742 // We currently can't complete objective C types through the newly
9743 // added ASTContext because it only supports TagDecl objects right
9744 // now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00009745 if (class_interface_decl)
9746 printf("@class %s", class_interface_decl->getName().str().c_str());
9747 }
9748 } break;
9749
9750 case clang::Type::Typedef:
9751 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9752 ->getDecl()
9753 ->getName()
9754 .str()
9755 .c_str());
9756 break;
9757
9758 case clang::Type::Auto:
9759 printf("auto ");
9760 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9761 llvm::cast<clang::AutoType>(qual_type)
9762 ->getDeducedType()
9763 .getAsOpaquePtr()));
9764
9765 case clang::Type::Elaborated:
9766 printf("elaborated ");
9767 return DumpTypeName(CompilerType(
9768 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9769 ->getNamedType()
9770 .getAsOpaquePtr()));
9771
9772 case clang::Type::Paren:
9773 printf("paren ");
9774 return DumpTypeName(CompilerType(
9775 type.GetTypeSystem(),
9776 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9777
9778 default:
9779 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9780 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009781 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009782 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009783}
9784
Kate Stoneb9c1b512016-09-06 20:57:50 +00009785clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9786 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9787 const char *parent_name, int tag_decl_kind,
9788 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9789 if (template_param_infos.IsValid()) {
9790 std::string template_basename(parent_name);
9791 template_basename.erase(template_basename.find('<'));
9792
9793 return CreateClassTemplateDecl(decl_ctx, access_type,
9794 template_basename.c_str(), tag_decl_kind,
9795 template_param_infos);
9796 }
Konrad Kleine248a1302019-05-23 11:14:47 +00009797 return nullptr;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009798}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009799
Kate Stoneb9c1b512016-09-06 20:57:50 +00009800void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9801 ClangASTContext *ast = (ClangASTContext *)baton;
9802 SymbolFile *sym_file = ast->GetSymbolFile();
9803 if (sym_file) {
9804 CompilerType clang_type = GetTypeForDecl(decl);
9805 if (clang_type)
9806 sym_file->CompleteType(clang_type);
9807 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009808}
9809
Kate Stoneb9c1b512016-09-06 20:57:50 +00009810void ClangASTContext::CompleteObjCInterfaceDecl(
9811 void *baton, clang::ObjCInterfaceDecl *decl) {
9812 ClangASTContext *ast = (ClangASTContext *)baton;
9813 SymbolFile *sym_file = ast->GetSymbolFile();
9814 if (sym_file) {
9815 CompilerType clang_type = GetTypeForDecl(decl);
9816 if (clang_type)
9817 sym_file->CompleteType(clang_type);
9818 }
Zachary Turner42dff792016-04-15 00:21:26 +00009819}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009820
Kate Stoneb9c1b512016-09-06 20:57:50 +00009821DWARFASTParser *ClangASTContext::GetDWARFParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009822 if (!m_dwarf_ast_parser_up)
9823 m_dwarf_ast_parser_up.reset(new DWARFASTParserClang(*this));
9824 return m_dwarf_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00009825}
9826
9827PDBASTParser *ClangASTContext::GetPDBParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009828 if (!m_pdb_ast_parser_up)
9829 m_pdb_ast_parser_up.reset(new PDBASTParser(*this));
9830 return m_pdb_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00009831}
9832
9833bool ClangASTContext::LayoutRecordType(
9834 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9835 uint64_t &alignment,
9836 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9837 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9838 &base_offsets,
9839 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9840 &vbase_offsets) {
9841 ClangASTContext *ast = (ClangASTContext *)baton;
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009842 lldb_private::ClangASTImporter *importer = nullptr;
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009843 if (ast->m_dwarf_ast_parser_up)
9844 importer = &ast->m_dwarf_ast_parser_up->GetClangASTImporter();
9845 if (!importer && ast->m_pdb_ast_parser_up)
9846 importer = &ast->m_pdb_ast_parser_up->GetClangASTImporter();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009847 if (!importer)
9848 return false;
9849
9850 return importer->LayoutRecordType(record_decl, bit_size, alignment,
9851 field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009852}
9853
Paul Hermand628cbb2015-09-15 23:44:17 +00009854// CompilerDecl override functions
Paul Hermand628cbb2015-09-15 23:44:17 +00009855
Kate Stoneb9c1b512016-09-06 20:57:50 +00009856ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9857 if (opaque_decl) {
9858 clang::NamedDecl *nd =
9859 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9860 if (nd != nullptr)
9861 return ConstString(nd->getDeclName().getAsString());
9862 }
9863 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009864}
9865
Kate Stoneb9c1b512016-09-06 20:57:50 +00009866ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9867 if (opaque_decl) {
9868 clang::NamedDecl *nd =
9869 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9870 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9871 clang::MangleContext *mc = getMangleContext();
9872 if (mc && mc->shouldMangleCXXName(nd)) {
9873 llvm::SmallVector<char, 1024> buf;
9874 llvm::raw_svector_ostream llvm_ostrm(buf);
9875 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9876 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9877 Ctor_Complete, llvm_ostrm);
9878 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9879 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9880 Dtor_Complete, llvm_ostrm);
9881 } else {
9882 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009883 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009884 if (buf.size() > 0)
9885 return ConstString(buf.data(), buf.size());
9886 }
Greg Claytonfe689042015-11-10 17:47:04 +00009887 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009888 }
9889 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009890}
9891
Kate Stoneb9c1b512016-09-06 20:57:50 +00009892CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9893 if (opaque_decl)
9894 return CompilerDeclContext(this,
9895 ((clang::Decl *)opaque_decl)->getDeclContext());
9896 else
9897 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009898}
9899
Kate Stoneb9c1b512016-09-06 20:57:50 +00009900CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9901 if (clang::FunctionDecl *func_decl =
9902 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9903 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9904 if (clang::ObjCMethodDecl *objc_method =
9905 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9906 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9907 else
Greg Claytonfe689042015-11-10 17:47:04 +00009908 return CompilerType();
9909}
9910
Kate Stoneb9c1b512016-09-06 20:57:50 +00009911size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9912 if (clang::FunctionDecl *func_decl =
9913 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9914 return func_decl->param_size();
9915 if (clang::ObjCMethodDecl *objc_method =
9916 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9917 return objc_method->param_size();
9918 else
9919 return 0;
9920}
9921
9922CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9923 size_t idx) {
9924 if (clang::FunctionDecl *func_decl =
9925 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9926 if (idx < func_decl->param_size()) {
9927 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9928 if (var_decl)
9929 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9930 }
9931 } else if (clang::ObjCMethodDecl *objc_method =
9932 llvm::dyn_cast<clang::ObjCMethodDecl>(
9933 (clang::Decl *)opaque_decl)) {
9934 if (idx < objc_method->param_size())
9935 return CompilerType(
9936 this,
9937 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9938 }
9939 return CompilerType();
9940}
9941
Greg Clayton99558cc42015-08-24 23:46:31 +00009942// CompilerDeclContext functions
Greg Clayton99558cc42015-08-24 23:46:31 +00009943
Kate Stoneb9c1b512016-09-06 20:57:50 +00009944std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9945 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9946 std::vector<CompilerDecl> found_decls;
9947 if (opaque_decl_ctx) {
9948 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9949 std::set<DeclContext *> searched;
9950 std::multimap<DeclContext *, DeclContext *> search_queue;
9951 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009952
Kate Stoneb9c1b512016-09-06 20:57:50 +00009953 for (clang::DeclContext *decl_context = root_decl_ctx;
9954 decl_context != nullptr && found_decls.empty();
9955 decl_context = decl_context->getParent()) {
9956 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +00009957
Kate Stoneb9c1b512016-09-06 20:57:50 +00009958 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9959 it++) {
9960 if (!searched.insert(it->second).second)
9961 continue;
9962 symbol_file->ParseDeclsForContext(
9963 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +00009964
Kate Stoneb9c1b512016-09-06 20:57:50 +00009965 for (clang::Decl *child : it->second->decls()) {
9966 if (clang::UsingDirectiveDecl *ud =
9967 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9968 if (ignore_using_decls)
9969 continue;
9970 clang::DeclContext *from = ud->getCommonAncestor();
9971 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9972 search_queue.insert(
9973 std::make_pair(from, ud->getNominatedNamespace()));
9974 } else if (clang::UsingDecl *ud =
9975 llvm::dyn_cast<clang::UsingDecl>(child)) {
9976 if (ignore_using_decls)
9977 continue;
9978 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9979 clang::Decl *target = usd->getTargetDecl();
9980 if (clang::NamedDecl *nd =
9981 llvm::dyn_cast<clang::NamedDecl>(target)) {
9982 IdentifierInfo *ii = nd->getIdentifier();
9983 if (ii != nullptr &&
9984 ii->getName().equals(name.AsCString(nullptr)))
9985 found_decls.push_back(CompilerDecl(this, nd));
9986 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009987 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009988 } else if (clang::NamedDecl *nd =
9989 llvm::dyn_cast<clang::NamedDecl>(child)) {
9990 IdentifierInfo *ii = nd->getIdentifier();
9991 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9992 found_decls.push_back(CompilerDecl(this, nd));
9993 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009994 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009995 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009996 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009997 }
9998 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +00009999}
10000
Dawn Perchikb5925782015-12-12 19:31:41 +000010001// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010002// and return the number of levels it took to find it, or
Adrian Prantl05097242018-04-30 16:49:04 +000010003// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
10004// declaration, its name and/or type, if set, will be used to check that the
10005// decl found in the scope is a match.
Dawn Perchikb5925782015-12-12 19:31:41 +000010006//
Kate Stoneb9c1b512016-09-06 20:57:50 +000010007// The optional name is required by languages (like C++) to handle using
Adrian Prantl05097242018-04-30 16:49:04 +000010008// declarations like:
Dawn Perchikb5925782015-12-12 19:31:41 +000010009//
10010// void poo();
10011// namespace ns {
10012// void foo();
10013// void goo();
10014// }
10015// void bar() {
10016// using ns::foo;
10017// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
10018// // LLDB_INVALID_DECL_LEVEL for 'goo'.
10019// }
10020//
10021// The optional type is useful in the case that there's a specific overload
10022// that we're looking for that might otherwise be shadowed, like:
10023//
10024// void foo(int);
10025// namespace ns {
10026// void foo();
10027// }
10028// void bar() {
10029// using ns::foo;
10030// // CountDeclLevels returns 0 for { 'foo', void() },
10031// // 1 for { 'foo', void(int) }, and
10032// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
10033// }
10034//
10035// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +000010036// function at file scope will return the same level as a function at global
Adrian Prantl05097242018-04-30 16:49:04 +000010037// scope. Ideally we'd like to treat the file scope as an additional scope just
10038// below the global scope. More work needs to be done to recognise that, if
10039// the decl we're trying to look up is static, we should compare its source
10040// file with that of the current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010041uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
10042 clang::DeclContext *child_decl_ctx,
10043 ConstString *child_name,
10044 CompilerType *child_type) {
10045 if (frame_decl_ctx) {
10046 std::set<DeclContext *> searched;
10047 std::multimap<DeclContext *, DeclContext *> search_queue;
10048 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +000010049
Kate Stoneb9c1b512016-09-06 20:57:50 +000010050 // Get the lookup scope for the decl we're trying to find.
10051 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +000010052
Kate Stoneb9c1b512016-09-06 20:57:50 +000010053 // Look for it in our scope's decl context and its parents.
10054 uint32_t level = 0;
10055 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
10056 decl_ctx = decl_ctx->getParent()) {
10057 if (!decl_ctx->isLookupContext())
10058 continue;
10059 if (decl_ctx == parent_decl_ctx)
10060 // Found it!
10061 return level;
10062 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
10063 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
10064 it++) {
10065 if (searched.find(it->second) != searched.end())
10066 continue;
10067
10068 // Currently DWARF has one shared translation unit for all Decls at top
Adrian Prantl05097242018-04-30 16:49:04 +000010069 // level, so this would erroneously find using statements anywhere. So
10070 // don't look at the top-level translation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010071 // TODO fix this and add a testcase that depends on it.
10072
10073 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
10074 continue;
10075
10076 searched.insert(it->second);
10077 symbol_file->ParseDeclsForContext(
10078 CompilerDeclContext(this, it->second));
10079
10080 for (clang::Decl *child : it->second->decls()) {
10081 if (clang::UsingDirectiveDecl *ud =
10082 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10083 clang::DeclContext *ns = ud->getNominatedNamespace();
10084 if (ns == parent_decl_ctx)
10085 // Found it!
10086 return level;
10087 clang::DeclContext *from = ud->getCommonAncestor();
10088 if (searched.find(ns) == searched.end())
10089 search_queue.insert(std::make_pair(from, ns));
10090 } else if (child_name) {
10091 if (clang::UsingDecl *ud =
10092 llvm::dyn_cast<clang::UsingDecl>(child)) {
10093 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10094 clang::Decl *target = usd->getTargetDecl();
10095 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
10096 if (!nd)
10097 continue;
10098 // Check names.
10099 IdentifierInfo *ii = nd->getIdentifier();
10100 if (ii == nullptr ||
10101 !ii->getName().equals(child_name->AsCString(nullptr)))
10102 continue;
10103 // Check types, if one was provided.
10104 if (child_type) {
10105 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
10106 if (!AreTypesSame(clang_type, *child_type,
10107 /*ignore_qualifiers=*/true))
10108 continue;
10109 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010110 // Found it!
10111 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010112 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010113 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010114 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010115 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010116 }
10117 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +000010118 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010119 }
10120 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +000010121}
10122
Kate Stoneb9c1b512016-09-06 20:57:50 +000010123bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
10124 if (opaque_decl_ctx)
10125 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
10126 else
Greg Clayton99558cc42015-08-24 23:46:31 +000010127 return false;
10128}
10129
Kate Stoneb9c1b512016-09-06 20:57:50 +000010130ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
10131 if (opaque_decl_ctx) {
10132 clang::NamedDecl *named_decl =
10133 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10134 if (named_decl)
10135 return ConstString(named_decl->getName());
10136 }
10137 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +000010138}
10139
Kate Stoneb9c1b512016-09-06 20:57:50 +000010140ConstString
10141ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
10142 if (opaque_decl_ctx) {
10143 clang::NamedDecl *named_decl =
10144 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10145 if (named_decl)
10146 return ConstString(
10147 llvm::StringRef(named_decl->getQualifiedNameAsString()));
10148 }
10149 return ConstString();
10150}
10151
10152bool ClangASTContext::DeclContextIsClassMethod(
10153 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
10154 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
10155 if (opaque_decl_ctx) {
10156 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10157 if (ObjCMethodDecl *objc_method =
10158 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
10159 if (is_instance_method_ptr)
10160 *is_instance_method_ptr = objc_method->isInstanceMethod();
10161 if (language_ptr)
10162 *language_ptr = eLanguageTypeObjC;
10163 if (language_object_name_ptr)
10164 language_object_name_ptr->SetCString("self");
10165 return true;
10166 } else if (CXXMethodDecl *cxx_method =
10167 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
10168 if (is_instance_method_ptr)
10169 *is_instance_method_ptr = cxx_method->isInstance();
10170 if (language_ptr)
10171 *language_ptr = eLanguageTypeC_plus_plus;
10172 if (language_object_name_ptr)
10173 language_object_name_ptr->SetCString("this");
10174 return true;
10175 } else if (clang::FunctionDecl *function_decl =
10176 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
10177 ClangASTMetadata *metadata =
10178 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
10179 if (metadata && metadata->HasObjectPtr()) {
10180 if (is_instance_method_ptr)
10181 *is_instance_method_ptr = true;
10182 if (language_ptr)
10183 *language_ptr = eLanguageTypeObjC;
10184 if (language_object_name_ptr)
10185 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10186 return true;
10187 }
10188 }
10189 }
10190 return false;
10191}
10192
Raphael Isemanna9469972019-03-12 07:45:04 +000010193bool ClangASTContext::DeclContextIsContainedInLookup(
10194 void *opaque_decl_ctx, void *other_opaque_decl_ctx) {
10195 auto *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10196 auto *other = (clang::DeclContext *)other_opaque_decl_ctx;
10197
10198 do {
10199 // A decl context always includes its own contents in its lookup.
10200 if (decl_ctx == other)
10201 return true;
10202
10203 // If we have an inline namespace, then the lookup of the parent context
10204 // also includes the inline namespace contents.
10205 } while (other->isInlineNamespace() && (other = other->getParent()));
10206
10207 return false;
10208}
10209
Kate Stoneb9c1b512016-09-06 20:57:50 +000010210clang::DeclContext *
10211ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10212 if (dc.IsClang())
10213 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10214 return nullptr;
10215}
Greg Clayton99558cc42015-08-24 23:46:31 +000010216
10217ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010218ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10219 if (dc.IsClang())
10220 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10221 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10222 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010223}
10224
10225CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010226ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10227 if (dc.IsClang())
10228 return llvm::dyn_cast<clang::CXXMethodDecl>(
10229 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10230 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010231}
10232
10233clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010234ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10235 if (dc.IsClang())
10236 return llvm::dyn_cast<clang::FunctionDecl>(
10237 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10238 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010239}
10240
10241clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010242ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10243 if (dc.IsClang())
10244 return llvm::dyn_cast<clang::NamespaceDecl>(
10245 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10246 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010247}
10248
10249ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010250ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10251 const void *object) {
10252 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10253 if (ast)
10254 return ClangASTContext::GetMetadata(ast, object);
10255 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010256}
10257
10258clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010259ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10260 ClangASTContext *ast =
10261 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10262 if (ast)
10263 return ast->getASTContext();
10264 return nullptr;
10265}
10266
Raphael Isemann2eb963a2019-10-02 12:26:08 +000010267ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target,
10268 ArchSpec arch)
10269 : ClangASTContext(arch), m_target_wp(target.shared_from_this()),
Kate Stoneb9c1b512016-09-06 20:57:50 +000010270 m_persistent_variables(new ClangPersistentVariables) {}
10271
10272UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010273 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010274 Expression::ResultType desired_type,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010275 const EvaluateExpressionOptions &options,
10276 ValueObject *ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000010277 TargetSP target_sp = m_target_wp.lock();
10278 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010279 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010280
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010281 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010282 desired_type, options, ctx_obj);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010283}
10284
Kate Stoneb9c1b512016-09-06 20:57:50 +000010285FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10286 const CompilerType &return_type, const Address &function_address,
10287 const ValueList &arg_value_list, const char *name) {
10288 TargetSP target_sp = m_target_wp.lock();
10289 if (!target_sp)
10290 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010291
Kate Stoneb9c1b512016-09-06 20:57:50 +000010292 Process *process = target_sp->GetProcessSP().get();
10293 if (!process)
10294 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010295
Kate Stoneb9c1b512016-09-06 20:57:50 +000010296 return new ClangFunctionCaller(*process, return_type, function_address,
10297 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010298}
10299
10300UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010301ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10302 const char *name) {
10303 TargetSP target_sp = m_target_wp.lock();
10304 if (!target_sp)
10305 return nullptr;
10306
10307 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010308}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010309
10310PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010311ClangASTContextForExpressions::GetPersistentExpressionState() {
10312 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010313}
Sean Callanan68e44232017-09-28 20:20:25 +000010314
10315clang::ExternalASTMerger &
10316ClangASTContextForExpressions::GetMergerUnchecked() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000010317 lldbassert(m_scratch_ast_source_up != nullptr);
10318 return m_scratch_ast_source_up->GetMergerUnchecked();
Sean Callanan68e44232017-09-28 20:20:25 +000010319}