blob: 5c7000ac2f36c368e44c8cd9f05d3db231944cc6 [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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000990CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000991 ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992 if (ast) {
993 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
994 return ClangASTContext::GetBasicType(ast, basic_type);
995 }
996 return CompilerType();
997}
998
999uint32_t ClangASTContext::GetPointerByteSize() {
1000 if (m_pointer_byte_size == 0)
Adrian Prantld963a7c2019-01-15 18:07:52 +00001001 if (auto size = GetBasicType(lldb::eBasicTypeVoid)
1002 .GetPointerType()
1003 .GetByteSize(nullptr))
1004 m_pointer_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005 return m_pointer_byte_size;
1006}
1007
1008CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1009 return GetBasicType(getASTContext(), basic_type);
1010}
1011
1012CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1013 lldb::BasicType basic_type) {
1014 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001015 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001016 lldb::opaque_compiler_type_t clang_type =
1017 GetOpaqueCompilerType(ast, basic_type);
1018
1019 if (clang_type)
1020 return CompilerType(GetASTContext(ast), clang_type);
1021 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001022}
1023
Kate Stoneb9c1b512016-09-06 20:57:50 +00001024CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1025 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1026 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001027
Kate Stoneb9c1b512016-09-06 20:57:50 +00001028#define streq(a, b) strcmp(a, b) == 0
1029 assert(ast != nullptr);
1030 if (ast) {
1031 switch (dw_ate) {
1032 default:
1033 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001034
Kate Stoneb9c1b512016-09-06 20:57:50 +00001035 case DW_ATE_address:
1036 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001037 return CompilerType(this, ast->VoidPtrTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001038 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001039
Kate Stoneb9c1b512016-09-06 20:57:50 +00001040 case DW_ATE_boolean:
1041 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001042 return CompilerType(this, ast->BoolTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001043 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001044 return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001045 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001046 return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001047 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001048 return CompilerType(this, ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001049 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001050
Kate Stoneb9c1b512016-09-06 20:57:50 +00001051 case DW_ATE_lo_user:
1052 // This has been seen to mean DW_AT_complex_integer
1053 if (type_name) {
1054 if (::strstr(type_name, "complex")) {
1055 CompilerType complex_int_clang_type =
1056 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1057 bit_size / 2);
Alex Langfordbddab072019-08-13 19:40:36 +00001058 return CompilerType(
1059 this, ast->getComplexType(
1060 ClangUtil::GetQualType(complex_int_clang_type))
1061 .getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001062 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001063 }
1064 break;
1065
1066 case DW_ATE_complex_float:
1067 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001068 return CompilerType(this, ast->FloatComplexTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001070 return CompilerType(this, ast->DoubleComplexTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001071 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001072 return CompilerType(this, ast->LongDoubleComplexTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001073 else {
1074 CompilerType complex_float_clang_type =
1075 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1076 bit_size / 2);
Alex Langfordbddab072019-08-13 19:40:36 +00001077 return CompilerType(
1078 this, ast->getComplexType(
1079 ClangUtil::GetQualType(complex_float_clang_type))
1080 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001081 }
1082 break;
1083
1084 case DW_ATE_float:
1085 if (streq(type_name, "float") &&
1086 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 (streq(type_name, "double") &&
1089 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001090 return CompilerType(this, ast->DoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001091 if (streq(type_name, "long double") &&
1092 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001093 return CompilerType(this, ast->LongDoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094 // Fall back to not requiring a name match
1095 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001096 return CompilerType(this, ast->FloatTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001097 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001098 return CompilerType(this, ast->DoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001099 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001100 return CompilerType(this, ast->LongDoubleTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001101 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001102 return CompilerType(this, ast->HalfTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001103 break;
1104
1105 case DW_ATE_signed:
1106 if (type_name) {
1107 if (streq(type_name, "wchar_t") &&
1108 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1109 (getTargetInfo() &&
1110 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
Alex Langfordbddab072019-08-13 19:40:36 +00001111 return CompilerType(this, ast->WCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001112 if (streq(type_name, "void") &&
1113 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001114 return CompilerType(this, ast->VoidTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001115 if (strstr(type_name, "long long") &&
1116 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001117 return CompilerType(this, ast->LongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001118 if (strstr(type_name, "long") &&
1119 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001120 return CompilerType(this, ast->LongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001121 if (strstr(type_name, "short") &&
1122 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001123 return CompilerType(this, ast->ShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001124 if (strstr(type_name, "char")) {
1125 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001126 return CompilerType(this, ast->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001127 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001128 return CompilerType(this, ast->SignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001129 }
1130 if (strstr(type_name, "int")) {
1131 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001132 return CompilerType(this, ast->IntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001133 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00001134 return CompilerType(this, ast->Int128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001135 }
1136 }
1137 // We weren't able to match up a type name, just search by size
1138 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001139 return CompilerType(this, ast->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001140 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001141 return CompilerType(this, ast->ShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001142 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001143 return CompilerType(this, ast->IntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001144 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001145 return CompilerType(this, ast->LongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001146 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001147 return CompilerType(this, ast->LongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001148 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00001149 return CompilerType(this, ast->Int128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001150 break;
1151
1152 case DW_ATE_signed_char:
1153 if (ast->getLangOpts().CharIsSigned && type_name &&
1154 streq(type_name, "char")) {
1155 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001156 return CompilerType(this, ast->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001157 }
1158 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001159 return CompilerType(this, ast->SignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001160 break;
1161
1162 case DW_ATE_unsigned:
1163 if (type_name) {
1164 if (streq(type_name, "wchar_t")) {
1165 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1166 if (!(getTargetInfo() &&
1167 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
Alex Langfordbddab072019-08-13 19:40:36 +00001168 return CompilerType(this, ast->WCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001169 }
1170 }
1171 if (strstr(type_name, "long long")) {
1172 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001173 return CompilerType(this, ast->UnsignedLongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001174 } else if (strstr(type_name, "long")) {
1175 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001176 return CompilerType(this, ast->UnsignedLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001177 } else if (strstr(type_name, "short")) {
1178 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001179 return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001180 } else if (strstr(type_name, "char")) {
1181 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001182 return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001183 } else if (strstr(type_name, "int")) {
1184 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001185 return CompilerType(this, ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001186 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00001187 return CompilerType(this, ast->UnsignedInt128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001188 }
1189 }
1190 // We weren't able to match up a type name, just search by size
1191 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001192 return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001193 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001194 return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001195 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001196 return CompilerType(this, ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001197 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001198 return CompilerType(this, ast->UnsignedLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001199 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001200 return CompilerType(this, ast->UnsignedLongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001201 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00001202 return CompilerType(this, ast->UnsignedInt128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001203 break;
1204
1205 case DW_ATE_unsigned_char:
1206 if (!ast->getLangOpts().CharIsSigned && type_name &&
1207 streq(type_name, "char")) {
1208 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001209 return CompilerType(this, ast->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001210 }
1211 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001212 return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001213 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00001214 return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001215 break;
1216
1217 case DW_ATE_imaginary_float:
1218 break;
1219
1220 case DW_ATE_UTF:
1221 if (type_name) {
Jonas Devliegherec46d39b2019-08-21 21:30:55 +00001222 if (streq(type_name, "char16_t"))
Alex Langfordbddab072019-08-13 19:40:36 +00001223 return CompilerType(this, ast->Char16Ty.getAsOpaquePtr());
Jonas Devlieghere6c9dc122019-08-23 04:11:38 +00001224 if (streq(type_name, "char32_t"))
Alex Langfordbddab072019-08-13 19:40:36 +00001225 return CompilerType(this, ast->Char32Ty.getAsOpaquePtr());
Jonas Devlieghere6c9dc122019-08-23 04:11:38 +00001226 if (streq(type_name, "char8_t"))
Jonas Devliegherec46d39b2019-08-21 21:30:55 +00001227 return CompilerType(this, ast->Char8Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001228 }
1229 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001230 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001231 }
1232 // This assert should fire for anything that we don't catch above so we know
1233 // to fix any issues we run into.
1234 if (type_name) {
1235 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1236 "DW_TAG_base_type '%s' encoded with "
1237 "DW_ATE = 0x%x, bit_size = %u\n",
1238 type_name, dw_ate, bit_size);
1239 } else {
1240 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1241 "DW_TAG_base_type encoded with "
1242 "DW_ATE = 0x%x, bit_size = %u\n",
1243 dw_ate, bit_size);
1244 }
1245 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001246}
1247
Kate Stoneb9c1b512016-09-06 20:57:50 +00001248CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1249 if (ast)
Alex Langfordbddab072019-08-13 19:40:36 +00001250 return CompilerType(ClangASTContext::GetASTContext(ast),
1251 ast->UnknownAnyTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001252 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001253}
1254
Kate Stoneb9c1b512016-09-06 20:57:50 +00001255CompilerType ClangASTContext::GetCStringType(bool is_const) {
1256 ASTContext *ast = getASTContext();
1257 QualType char_type(ast->CharTy);
1258
1259 if (is_const)
1260 char_type.addConst();
1261
Alex Langfordbddab072019-08-13 19:40:36 +00001262 return CompilerType(this, ast->getPointerType(char_type).getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001263}
1264
Zachary Turner115209e2018-11-05 19:25:39 +00001265clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001266ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1267 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001268}
1269
Kate Stoneb9c1b512016-09-06 20:57:50 +00001270clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1271 clang::Decl *source_decl) {
1272 FileSystemOptions file_system_options;
1273 FileManager file_manager(file_system_options);
1274 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1275
Gabor Marton5ac6d492019-05-15 10:29:48 +00001276 if (llvm::Expected<clang::Decl *> ret_or_error =
1277 importer.Import(source_decl)) {
1278 return *ret_or_error;
1279 } else {
1280 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
1281 LLDB_LOG_ERROR(log, ret_or_error.takeError(), "Couldn't import decl: {0}");
1282 return nullptr;
1283 }
Greg Clayton526e5af2010-11-13 03:52:47 +00001284}
1285
Kate Stoneb9c1b512016-09-06 20:57:50 +00001286bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1287 bool ignore_qualifiers) {
1288 ClangASTContext *ast =
1289 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1290 if (!ast || ast != type2.GetTypeSystem())
1291 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001292
Kate Stoneb9c1b512016-09-06 20:57:50 +00001293 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1294 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001295
Kate Stoneb9c1b512016-09-06 20:57:50 +00001296 QualType type1_qual = ClangUtil::GetQualType(type1);
1297 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001298
Kate Stoneb9c1b512016-09-06 20:57:50 +00001299 if (ignore_qualifiers) {
1300 type1_qual = type1_qual.getUnqualifiedType();
1301 type2_qual = type2_qual.getUnqualifiedType();
1302 }
1303
1304 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001305}
1306
Alex Langfordcb68bd72019-08-23 06:11:32 +00001307CompilerType ClangASTContext::GetTypeForDecl(void *opaque_decl) {
1308 if (!opaque_decl)
1309 return CompilerType();
1310
1311 clang::Decl *decl = static_cast<clang::Decl *>(opaque_decl);
1312 if (auto *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl))
1313 return GetTypeForDecl(named_decl);
1314 return CompilerType();
1315}
1316
Kate Stoneb9c1b512016-09-06 20:57:50 +00001317CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1318 if (clang::ObjCInterfaceDecl *interface_decl =
1319 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1320 return GetTypeForDecl(interface_decl);
1321 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1322 return GetTypeForDecl(tag_decl);
1323 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001324}
1325
Kate Stoneb9c1b512016-09-06 20:57:50 +00001326CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001327 // No need to call the getASTContext() accessor (which can create the AST if
1328 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001329 // AST if our AST didn't already exist...
1330 ASTContext *ast = &decl->getASTContext();
1331 if (ast)
Alex Langfordbddab072019-08-13 19:40:36 +00001332 return CompilerType(ClangASTContext::GetASTContext(ast),
1333 ast->getTagDeclType(decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001334 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001335}
1336
Kate Stoneb9c1b512016-09-06 20:57:50 +00001337CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001338 // No need to call the getASTContext() accessor (which can create the AST if
1339 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001340 // AST if our AST didn't already exist...
1341 ASTContext *ast = &decl->getASTContext();
1342 if (ast)
Alex Langfordbddab072019-08-13 19:40:36 +00001343 return CompilerType(ClangASTContext::GetASTContext(ast),
1344 ast->getObjCInterfaceType(decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001345 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001346}
1347
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001348#pragma mark Structure, Unions, Classes
1349
shafikde2c7ca2019-10-28 14:26:54 -07001350CompilerType ClangASTContext::CreateRecordType(
1351 DeclContext *decl_ctx, AccessType access_type, const char *name, int kind,
1352 LanguageType language, ClangASTMetadata *metadata, bool exports_symbols) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001353 ASTContext *ast = getASTContext();
1354 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001355
Kate Stoneb9c1b512016-09-06 20:57:50 +00001356 if (decl_ctx == nullptr)
1357 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001358
Kate Stoneb9c1b512016-09-06 20:57:50 +00001359 if (language == eLanguageTypeObjC ||
1360 language == eLanguageTypeObjC_plus_plus) {
1361 bool isForwardDecl = true;
1362 bool isInternal = false;
1363 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1364 }
Greg Clayton9e409562010-07-28 02:04:09 +00001365
Kate Stoneb9c1b512016-09-06 20:57:50 +00001366 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
Adrian Prantl05097242018-04-30 16:49:04 +00001367 // we will need to update this code. I was told to currently always use the
1368 // CXXRecordDecl class since we often don't know from debug information if
1369 // something is struct or a class, so we default to always use the more
Kate Stoneb9c1b512016-09-06 20:57:50 +00001370 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001371
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001372 bool has_name = name && name[0];
Greg Claytonc4ffd662013-03-08 01:37:30 +00001373
Kate Stoneb9c1b512016-09-06 20:57:50 +00001374 CXXRecordDecl *decl = CXXRecordDecl::Create(
1375 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001376 SourceLocation(), has_name ? &ast->Idents.get(name) : nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001377
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001378 if (!has_name) {
1379 // In C++ a lambda is also represented as an unnamed class. This is
1380 // different from an *anonymous class* that the user wrote:
1381 //
1382 // struct A {
1383 // // anonymous class (GNU/MSVC extension)
1384 // struct {
1385 // int x;
1386 // };
1387 // // unnamed class within a class
1388 // struct {
1389 // int y;
1390 // } B;
1391 // };
1392 //
1393 // void f() {
1394 // // unammed class outside of a class
1395 // struct {
1396 // int z;
1397 // } C;
1398 // }
1399 //
1400 // Anonymous classes is a GNU/MSVC extension that clang supports. It
1401 // requires the anonymous class be embedded within a class. So the new
1402 // heuristic verifies this condition.
shafikde2c7ca2019-10-28 14:26:54 -07001403 if (isa<CXXRecordDecl>(decl_ctx) && exports_symbols)
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001404 decl->setAnonymousStructOrUnion(true);
Shafik Yaghmour62abe492019-08-14 22:30:29 +00001405 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001406
1407 if (decl) {
1408 if (metadata)
1409 SetMetadata(ast, decl, *metadata);
1410
1411 if (access_type != eAccessNone)
1412 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1413
1414 if (decl_ctx)
1415 decl_ctx->addDecl(decl);
1416
Alex Langfordbddab072019-08-13 19:40:36 +00001417 return CompilerType(this, ast->getTagDeclType(decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001418 }
1419 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001420}
1421
Sean Callanan09e91ac2017-05-11 22:08:05 +00001422namespace {
1423 bool IsValueParam(const clang::TemplateArgument &argument) {
1424 return argument.getKind() == TemplateArgument::Integral;
1425 }
1426}
1427
Kate Stoneb9c1b512016-09-06 20:57:50 +00001428static TemplateParameterList *CreateTemplateParameterList(
1429 ASTContext *ast,
1430 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1431 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1432 const bool parameter_pack = false;
1433 const bool is_typename = false;
1434 const unsigned depth = 0;
Sean Callanan09e91ac2017-05-11 22:08:05 +00001435 const size_t num_template_params = template_param_infos.args.size();
1436 DeclContext *const decl_context =
1437 ast->getTranslationUnitDecl(); // Is this the right decl context?,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001438 for (size_t i = 0; i < num_template_params; ++i) {
1439 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001440
Kate Stoneb9c1b512016-09-06 20:57:50 +00001441 IdentifierInfo *identifier_info = nullptr;
1442 if (name && name[0])
1443 identifier_info = &ast->Idents.get(name);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001444 if (IsValueParam(template_param_infos.args[i])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001445 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001446 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001447 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1448 template_param_infos.args[i].getIntegralType(), parameter_pack,
1449 nullptr));
1450
1451 } else {
1452 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001453 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001454 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1455 is_typename, parameter_pack));
1456 }
1457 }
Eugene Zemtsova9d928c2017-09-29 03:15:08 +00001458
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001459 if (template_param_infos.packed_args) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001460 IdentifierInfo *identifier_info = nullptr;
1461 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1462 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1463 const bool parameter_pack_true = true;
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001464
1465 if (!template_param_infos.packed_args->args.empty() &&
1466 IsValueParam(template_param_infos.packed_args->args[0])) {
Sean Callanan09e91ac2017-05-11 22:08:05 +00001467 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001468 *ast, decl_context, SourceLocation(), SourceLocation(), depth,
1469 num_template_params, identifier_info,
Sean Callanan09e91ac2017-05-11 22:08:05 +00001470 template_param_infos.packed_args->args[0].getIntegralType(),
1471 parameter_pack_true, nullptr));
1472 } else {
1473 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Shafik Yaghmour1849dd42019-01-30 21:48:56 +00001474 *ast, decl_context, SourceLocation(), SourceLocation(), depth,
1475 num_template_params, identifier_info, is_typename,
1476 parameter_pack_true));
Sean Callanan09e91ac2017-05-11 22:08:05 +00001477 }
1478 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001479 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1480 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1481 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1482 SourceLocation(), requires_clause);
1483 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001484}
1485
Kate Stoneb9c1b512016-09-06 20:57:50 +00001486clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1487 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1488 const char *name, const TemplateParameterInfos &template_param_infos) {
Adrian Prantld8f460e2018-05-02 16:55:16 +00001489 // /// Create a function template node.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001490 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001491
Kate Stoneb9c1b512016-09-06 20:57:50 +00001492 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001493
Kate Stoneb9c1b512016-09-06 20:57:50 +00001494 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1495 ast, template_param_infos, template_param_decls);
1496 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1497 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1498 template_param_list, func_decl);
1499
1500 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1501 i < template_param_decl_count; ++i) {
1502 // TODO: verify which decl context we should put template_param_decls into..
1503 template_param_decls[i]->setDeclContext(func_decl);
1504 }
1505
1506 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001507}
1508
Kate Stoneb9c1b512016-09-06 20:57:50 +00001509void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1510 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1511 const TemplateParameterInfos &infos) {
Shafik Yaghmoura0858e22019-07-17 20:16:13 +00001512 TemplateArgumentList *template_args_ptr =
1513 TemplateArgumentList::CreateCopy(func_decl->getASTContext(), infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001514
Shafik Yaghmoura0858e22019-07-17 20:16:13 +00001515 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl,
1516 template_args_ptr, nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001517}
1518
Kate Stoneb9c1b512016-09-06 20:57:50 +00001519ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1520 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1521 int kind, const TemplateParameterInfos &template_param_infos) {
1522 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001523
Kate Stoneb9c1b512016-09-06 20:57:50 +00001524 ClassTemplateDecl *class_template_decl = nullptr;
1525 if (decl_ctx == nullptr)
1526 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001527
Kate Stoneb9c1b512016-09-06 20:57:50 +00001528 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1529 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001530
Kate Stoneb9c1b512016-09-06 20:57:50 +00001531 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001532
Kate Stoneb9c1b512016-09-06 20:57:50 +00001533 for (NamedDecl *decl : result) {
1534 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001535 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001536 return class_template_decl;
1537 }
1538
1539 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1540
1541 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1542 ast, template_param_infos, template_param_decls);
1543
1544 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1545 *ast, (TagDecl::TagKind)kind,
1546 decl_ctx, // What decl context do we use here? TU? The actual decl
1547 // context?
1548 SourceLocation(), SourceLocation(), &identifier_info);
1549
1550 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1551 i < template_param_decl_count; ++i) {
1552 template_param_decls[i]->setDeclContext(template_cxx_decl);
1553 }
1554
1555 // With templated classes, we say that a class is templated with
1556 // specializations, but that the bare class has no functions.
1557 // template_cxx_decl->startDefinition();
1558 // template_cxx_decl->completeDefinition();
1559
1560 class_template_decl = ClassTemplateDecl::Create(
1561 *ast,
1562 decl_ctx, // What decl context do we use here? TU? The actual decl
1563 // context?
Pavel Labath4294de32017-01-12 10:44:16 +00001564 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
Richard Smith35b007e2019-02-15 21:48:09 +00001565 template_cxx_decl->setDescribedClassTemplate(class_template_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001566
1567 if (class_template_decl) {
1568 if (access_type != eAccessNone)
1569 class_template_decl->setAccess(
1570 ConvertAccessTypeToAccessSpecifier(access_type));
1571
1572 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1573 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1574
1575 decl_ctx->addDecl(class_template_decl);
1576
Sean Callanan5e9e1992011-10-26 01:06:27 +00001577#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001578 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001579#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001580 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001581
Kate Stoneb9c1b512016-09-06 20:57:50 +00001582 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001583}
1584
Frederic Rissf4e7e522018-04-02 16:18:32 +00001585TemplateTemplateParmDecl *
1586ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) {
1587 ASTContext *ast = getASTContext();
1588
1589 auto *decl_ctx = ast->getTranslationUnitDecl();
1590
1591 IdentifierInfo &identifier_info = ast->Idents.get(template_name);
1592 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1593
1594 ClangASTContext::TemplateParameterInfos template_param_infos;
1595 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1596 ast, template_param_infos, template_param_decls);
1597
1598 // LLDB needs to create those decls only to be able to display a
Adrian Prantl05097242018-04-30 16:49:04 +00001599 // type that includes a template template argument. Only the name matters for
1600 // this purpose, so we use dummy values for the other characterisitcs of the
1601 // type.
Frederic Rissf4e7e522018-04-02 16:18:32 +00001602 return TemplateTemplateParmDecl::Create(
1603 *ast, decl_ctx, SourceLocation(),
1604 /*Depth*/ 0, /*Position*/ 0,
1605 /*IsParameterPack*/ false, &identifier_info, template_param_list);
1606}
1607
Greg Claytonf0705c82011-10-22 03:33:13 +00001608ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001609ClangASTContext::CreateClassTemplateSpecializationDecl(
1610 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1611 const TemplateParameterInfos &template_param_infos) {
1612 ASTContext *ast = getASTContext();
Sean Callanan09e91ac2017-05-11 22:08:05 +00001613 llvm::SmallVector<clang::TemplateArgument, 2> args(
1614 template_param_infos.args.size() +
1615 (template_param_infos.packed_args ? 1 : 0));
1616 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1617 args.begin());
1618 if (template_param_infos.packed_args) {
1619 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1620 *ast, template_param_infos.packed_args->args);
1621 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001622 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1623 ClassTemplateSpecializationDecl::Create(
1624 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Sean Callanan09e91ac2017-05-11 22:08:05 +00001625 SourceLocation(), class_template_decl, args,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001626 nullptr);
1627
1628 class_template_specialization_decl->setSpecializationKind(
1629 TSK_ExplicitSpecialization);
1630
1631 return class_template_specialization_decl;
1632}
1633
1634CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1635 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1636 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001637 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001638 if (ast)
1639 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00001640 this, ast->getTagDeclType(class_template_specialization_decl)
1641 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001642 }
1643 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001644}
1645
Kate Stoneb9c1b512016-09-06 20:57:50 +00001646static inline bool check_op_param(bool is_method,
1647 clang::OverloadedOperatorKind op_kind,
1648 bool unary, bool binary,
1649 uint32_t num_params) {
1650 // Special-case call since it can take any number of operands
1651 if (op_kind == OO_Call)
1652 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001653
Kate Stoneb9c1b512016-09-06 20:57:50 +00001654 // The parameter count doesn't include "this"
1655 if (is_method)
1656 ++num_params;
1657 if (num_params == 1)
1658 return unary;
1659 if (num_params == 2)
1660 return binary;
1661 else
Greg Clayton090d0982011-06-19 03:43:27 +00001662 return false;
1663}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001664
Kate Stoneb9c1b512016-09-06 20:57:50 +00001665bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1666 bool is_method, clang::OverloadedOperatorKind op_kind,
1667 uint32_t num_params) {
1668 switch (op_kind) {
1669 default:
1670 break;
1671 // C++ standard allows any number of arguments to new/delete
1672 case OO_New:
1673 case OO_Array_New:
1674 case OO_Delete:
1675 case OO_Array_Delete:
1676 return true;
1677 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001678
Kate Stoneb9c1b512016-09-06 20:57:50 +00001679#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1680 case OO_##Name: \
1681 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1682 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001683#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001684 default:
1685 break;
1686 }
1687 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001688}
1689
Greg Clayton57ee3062013-07-11 22:46:58 +00001690clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001691ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1692 clang::AccessSpecifier rhs) {
1693 // Make the access equal to the stricter of the field and the nested field's
1694 // access
1695 if (lhs == AS_none || rhs == AS_none)
1696 return AS_none;
1697 if (lhs == AS_private || rhs == AS_private)
1698 return AS_private;
1699 if (lhs == AS_protected || rhs == AS_protected)
1700 return AS_protected;
1701 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001702}
1703
Kate Stoneb9c1b512016-09-06 20:57:50 +00001704bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1705 uint32_t &bitfield_bit_size) {
1706 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001707}
1708
Kate Stoneb9c1b512016-09-06 20:57:50 +00001709bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1710 uint32_t &bitfield_bit_size) {
1711 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001712 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001713
Kate Stoneb9c1b512016-09-06 20:57:50 +00001714 if (field->isBitField()) {
1715 Expr *bit_width_expr = field->getBitWidth();
1716 if (bit_width_expr) {
1717 llvm::APSInt bit_width_apsint;
1718 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1719 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001720 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001721 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001722 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001723 }
1724 return false;
1725}
1726
1727bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1728 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001729 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001730
1731 if (!record_decl->field_empty())
1732 return true;
1733
1734 // No fields, lets check this is a CXX record and check the base classes
1735 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1736 if (cxx_record_decl) {
1737 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1738 for (base_class = cxx_record_decl->bases_begin(),
1739 base_class_end = cxx_record_decl->bases_end();
1740 base_class != base_class_end; ++base_class) {
1741 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1742 base_class->getType()->getAs<RecordType>()->getDecl());
1743 if (RecordHasFields(base_class_decl))
1744 return true;
1745 }
1746 }
1747 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001748}
1749
Adrian Prantl4e8be2c2018-06-13 16:21:24 +00001750#pragma mark Objective-C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001751
Kate Stoneb9c1b512016-09-06 20:57:50 +00001752CompilerType ClangASTContext::CreateObjCClass(const char *name,
1753 DeclContext *decl_ctx,
1754 bool isForwardDecl,
1755 bool isInternal,
1756 ClangASTMetadata *metadata) {
1757 ASTContext *ast = getASTContext();
1758 assert(ast != nullptr);
1759 assert(name && name[0]);
1760 if (decl_ctx == nullptr)
1761 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001762
Kate Stoneb9c1b512016-09-06 20:57:50 +00001763 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1764 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1765 nullptr, SourceLocation(),
1766 /*isForwardDecl,*/
1767 isInternal);
1768
1769 if (decl && metadata)
1770 SetMetadata(ast, decl, *metadata);
1771
Alex Langfordbddab072019-08-13 19:40:36 +00001772 return CompilerType(this, ast->getObjCInterfaceType(decl).getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001773}
1774
Kate Stoneb9c1b512016-09-06 20:57:50 +00001775static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001776 return !ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001777}
1778
Greg Clayton57ee3062013-07-11 22:46:58 +00001779uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001780ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1781 bool omit_empty_base_classes) {
1782 uint32_t num_bases = 0;
1783 if (cxx_record_decl) {
1784 if (omit_empty_base_classes) {
1785 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1786 for (base_class = cxx_record_decl->bases_begin(),
1787 base_class_end = cxx_record_decl->bases_end();
1788 base_class != base_class_end; ++base_class) {
1789 // Skip empty base classes
1790 if (omit_empty_base_classes) {
1791 if (BaseSpecifierIsEmpty(base_class))
1792 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001793 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001794 ++num_bases;
1795 }
1796 } else
1797 num_bases = cxx_record_decl->getNumBases();
1798 }
1799 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001800}
1801
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001802#pragma mark Namespace Declarations
1803
Raphael Isemanna9469972019-03-12 07:45:04 +00001804NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1805 const char *name, DeclContext *decl_ctx, bool is_inline) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001806 NamespaceDecl *namespace_decl = nullptr;
1807 ASTContext *ast = getASTContext();
1808 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1809 if (decl_ctx == nullptr)
1810 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001811
Kate Stoneb9c1b512016-09-06 20:57:50 +00001812 if (name) {
1813 IdentifierInfo &identifier_info = ast->Idents.get(name);
1814 DeclarationName decl_name(&identifier_info);
1815 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1816 for (NamedDecl *decl : result) {
1817 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1818 if (namespace_decl)
1819 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001820 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001821
1822 namespace_decl =
Raphael Isemanna9469972019-03-12 07:45:04 +00001823 NamespaceDecl::Create(*ast, decl_ctx, is_inline, SourceLocation(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001824 SourceLocation(), &identifier_info, nullptr);
1825
1826 decl_ctx->addDecl(namespace_decl);
1827 } else {
1828 if (decl_ctx == translation_unit_decl) {
1829 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1830 if (namespace_decl)
1831 return namespace_decl;
1832
1833 namespace_decl =
1834 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1835 SourceLocation(), nullptr, nullptr);
1836 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1837 translation_unit_decl->addDecl(namespace_decl);
1838 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1839 } else {
1840 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1841 if (parent_namespace_decl) {
1842 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1843 if (namespace_decl)
1844 return namespace_decl;
1845 namespace_decl =
1846 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1847 SourceLocation(), nullptr, nullptr);
1848 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1849 parent_namespace_decl->addDecl(namespace_decl);
1850 assert(namespace_decl ==
1851 parent_namespace_decl->getAnonymousNamespace());
1852 } else {
Raphael Isemannc4944812019-07-04 19:49:31 +00001853 assert(false && "GetUniqueNamespaceDeclaration called with no name and "
1854 "no namespace as decl_ctx");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001855 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001856 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001857 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001858#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001859 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001860#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001861 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001862}
1863
Kate Stoneb9c1b512016-09-06 20:57:50 +00001864NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
Raphael Isemanna9469972019-03-12 07:45:04 +00001865 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx,
1866 bool is_inline) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001867 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1868 if (ast_ctx == nullptr)
1869 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +00001870
Raphael Isemanna9469972019-03-12 07:45:04 +00001871 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx, is_inline);
Siva Chandra03ff5c82016-02-05 19:10:04 +00001872}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001873
Paul Hermand628cbb2015-09-15 23:44:17 +00001874clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001875ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1876 if (ctx != nullptr) {
1877 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1878 clang::SourceLocation());
1879 ctx->addDecl(decl);
1880 return decl;
1881 }
1882 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001883}
1884
Kate Stoneb9c1b512016-09-06 20:57:50 +00001885clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1886 clang::DeclContext *right,
1887 clang::DeclContext *root) {
1888 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001889 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001890
1891 std::set<clang::DeclContext *> path_left;
1892 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1893 path_left.insert(d);
1894
1895 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1896 if (path_left.find(d) != path_left.end())
1897 return d;
1898
1899 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00001900}
1901
Kate Stoneb9c1b512016-09-06 20:57:50 +00001902clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1903 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1904 if (decl_ctx != nullptr && ns_decl != nullptr) {
1905 clang::TranslationUnitDecl *translation_unit =
1906 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1907 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1908 *getASTContext(), decl_ctx, clang::SourceLocation(),
1909 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1910 clang::SourceLocation(), ns_decl,
1911 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
1912 decl_ctx->addDecl(using_decl);
1913 return using_decl;
1914 }
1915 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001916}
1917
1918clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001919ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1920 clang::NamedDecl *target) {
1921 if (current_decl_ctx != nullptr && target != nullptr) {
1922 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1923 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
1924 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1925 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1926 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
1927 target);
1928 using_decl->addShadowDecl(shadow_decl);
1929 current_decl_ctx->addDecl(using_decl);
1930 return using_decl;
1931 }
1932 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001933}
1934
Kate Stoneb9c1b512016-09-06 20:57:50 +00001935clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
1936 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
1937 if (decl_context != nullptr) {
1938 clang::VarDecl *var_decl = clang::VarDecl::Create(
1939 *getASTContext(), decl_context, clang::SourceLocation(),
1940 clang::SourceLocation(),
1941 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
1942 nullptr, clang::SC_None);
1943 var_decl->setAccess(clang::AS_public);
1944 decl_context->addDecl(var_decl);
1945 return var_decl;
1946 }
1947 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001948}
1949
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001950lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001951ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
1952 lldb::BasicType basic_type) {
1953 switch (basic_type) {
1954 case eBasicTypeVoid:
1955 return ast->VoidTy.getAsOpaquePtr();
1956 case eBasicTypeChar:
1957 return ast->CharTy.getAsOpaquePtr();
1958 case eBasicTypeSignedChar:
1959 return ast->SignedCharTy.getAsOpaquePtr();
1960 case eBasicTypeUnsignedChar:
1961 return ast->UnsignedCharTy.getAsOpaquePtr();
1962 case eBasicTypeWChar:
1963 return ast->getWCharType().getAsOpaquePtr();
1964 case eBasicTypeSignedWChar:
1965 return ast->getSignedWCharType().getAsOpaquePtr();
1966 case eBasicTypeUnsignedWChar:
1967 return ast->getUnsignedWCharType().getAsOpaquePtr();
1968 case eBasicTypeChar16:
1969 return ast->Char16Ty.getAsOpaquePtr();
1970 case eBasicTypeChar32:
1971 return ast->Char32Ty.getAsOpaquePtr();
1972 case eBasicTypeShort:
1973 return ast->ShortTy.getAsOpaquePtr();
1974 case eBasicTypeUnsignedShort:
1975 return ast->UnsignedShortTy.getAsOpaquePtr();
1976 case eBasicTypeInt:
1977 return ast->IntTy.getAsOpaquePtr();
1978 case eBasicTypeUnsignedInt:
1979 return ast->UnsignedIntTy.getAsOpaquePtr();
1980 case eBasicTypeLong:
1981 return ast->LongTy.getAsOpaquePtr();
1982 case eBasicTypeUnsignedLong:
1983 return ast->UnsignedLongTy.getAsOpaquePtr();
1984 case eBasicTypeLongLong:
1985 return ast->LongLongTy.getAsOpaquePtr();
1986 case eBasicTypeUnsignedLongLong:
1987 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1988 case eBasicTypeInt128:
1989 return ast->Int128Ty.getAsOpaquePtr();
1990 case eBasicTypeUnsignedInt128:
1991 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1992 case eBasicTypeBool:
1993 return ast->BoolTy.getAsOpaquePtr();
1994 case eBasicTypeHalf:
1995 return ast->HalfTy.getAsOpaquePtr();
1996 case eBasicTypeFloat:
1997 return ast->FloatTy.getAsOpaquePtr();
1998 case eBasicTypeDouble:
1999 return ast->DoubleTy.getAsOpaquePtr();
2000 case eBasicTypeLongDouble:
2001 return ast->LongDoubleTy.getAsOpaquePtr();
2002 case eBasicTypeFloatComplex:
2003 return ast->FloatComplexTy.getAsOpaquePtr();
2004 case eBasicTypeDoubleComplex:
2005 return ast->DoubleComplexTy.getAsOpaquePtr();
2006 case eBasicTypeLongDoubleComplex:
2007 return ast->LongDoubleComplexTy.getAsOpaquePtr();
2008 case eBasicTypeObjCID:
2009 return ast->getObjCIdType().getAsOpaquePtr();
2010 case eBasicTypeObjCClass:
2011 return ast->getObjCClassType().getAsOpaquePtr();
2012 case eBasicTypeObjCSel:
2013 return ast->getObjCSelType().getAsOpaquePtr();
2014 case eBasicTypeNullPtr:
2015 return ast->NullPtrTy.getAsOpaquePtr();
2016 default:
2017 return nullptr;
2018 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00002019}
2020
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002021#pragma mark Function Types
2022
Pavel Labath1ac2b202016-08-15 14:32:32 +00002023clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00002024ClangASTContext::GetDeclarationName(const char *name,
2025 const CompilerType &function_clang_type) {
2026 if (!name || !name[0])
2027 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002028
Kate Stoneb9c1b512016-09-06 20:57:50 +00002029 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
2030 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
2031 return DeclarationName(&getASTContext()->Idents.get(
2032 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00002033
Adrian Prantl05097242018-04-30 16:49:04 +00002034 // Check the number of operator parameters. Sometimes we have seen bad DWARF
2035 // that doesn't correctly describe operators and if we try to create a method
2036 // and add it to the class, clang will assert and crash, so we need to make
2037 // sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002038 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
2039 const clang::FunctionProtoType *function_type =
2040 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
2041 if (function_type == nullptr)
2042 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002043
Kate Stoneb9c1b512016-09-06 20:57:50 +00002044 const bool is_method = false;
2045 const unsigned int num_params = function_type->getNumParams();
2046 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
2047 is_method, op_kind, num_params))
2048 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002049
Kate Stoneb9c1b512016-09-06 20:57:50 +00002050 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00002051}
2052
Kate Stoneb9c1b512016-09-06 20:57:50 +00002053FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
2054 DeclContext *decl_ctx, const char *name,
2055 const CompilerType &function_clang_type, int storage, bool is_inline) {
2056 FunctionDecl *func_decl = nullptr;
2057 ASTContext *ast = getASTContext();
2058 if (decl_ctx == nullptr)
2059 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002060
Kate Stoneb9c1b512016-09-06 20:57:50 +00002061 const bool hasWrittenPrototype = true;
2062 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00002063
Kate Stoneb9c1b512016-09-06 20:57:50 +00002064 clang::DeclarationName declarationName =
2065 GetDeclarationName(name, function_clang_type);
2066 func_decl = FunctionDecl::Create(
2067 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2068 ClangUtil::GetQualType(function_clang_type), nullptr,
2069 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002070 isConstexprSpecified ? CSK_constexpr : CSK_unspecified);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002071 if (func_decl)
2072 decl_ctx->addDecl(func_decl);
2073
Sean Callanan5e9e1992011-10-26 01:06:27 +00002074#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00002075 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002076#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002077
2078 return func_decl;
2079}
2080
2081CompilerType ClangASTContext::CreateFunctionType(
2082 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002083 unsigned num_args, bool is_variadic, unsigned type_quals,
2084 clang::CallingConv cc) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002085 if (ast == nullptr)
2086 return CompilerType(); // invalid AST
2087
2088 if (!result_type || !ClangUtil::IsClangType(result_type))
2089 return CompilerType(); // invalid return type
2090
2091 std::vector<QualType> qual_type_args;
2092 if (num_args > 0 && args == nullptr)
2093 return CompilerType(); // invalid argument array passed in
2094
2095 // Verify that all arguments are valid and the right type
2096 for (unsigned i = 0; i < num_args; ++i) {
2097 if (args[i]) {
2098 // Make sure we have a clang type in args[i] and not a type from another
2099 // language whose name might match
2100 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2101 lldbassert(is_clang_type);
2102 if (is_clang_type)
2103 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2104 else
2105 return CompilerType(); // invalid argument type (must be a clang type)
2106 } else
2107 return CompilerType(); // invalid argument type (empty)
2108 }
2109
2110 // TODO: Detect calling convention in DWARF?
2111 FunctionProtoType::ExtProtoInfo proto_info;
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002112 proto_info.ExtInfo = cc;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002113 proto_info.Variadic = is_variadic;
2114 proto_info.ExceptionSpec = EST_None;
Mikael Nilsson8b3bf6c2018-12-13 10:17:26 +00002115 proto_info.TypeQuals = clang::Qualifiers::fromFastMask(type_quals);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002116 proto_info.RefQualifier = RQ_None;
2117
Alex Langfordbddab072019-08-13 19:40:36 +00002118 return CompilerType(ClangASTContext::GetASTContext(ast),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002119 ast->getFunctionType(ClangUtil::GetQualType(result_type),
Alex Langfordbddab072019-08-13 19:40:36 +00002120 qual_type_args, proto_info).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002121}
2122
2123ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
Zachary Turner6753d2d2018-12-12 17:17:53 +00002124 clang::DeclContext *decl_ctx, const char *name,
Shafik Yaghmourfa5c3402019-08-02 21:41:50 +00002125 const CompilerType &param_type, int storage, bool add_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002126 ASTContext *ast = getASTContext();
2127 assert(ast != nullptr);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00002128 auto *decl =
2129 ParmVarDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(),
2130 name && name[0] ? &ast->Idents.get(name) : nullptr,
2131 ClangUtil::GetQualType(param_type), nullptr,
2132 (clang::StorageClass)storage, nullptr);
Shafik Yaghmourfa5c3402019-08-02 21:41:50 +00002133 if (add_decl)
2134 decl_ctx->addDecl(decl);
2135
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00002136 return decl;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002137}
2138
2139void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2140 ParmVarDecl **params,
2141 unsigned num_params) {
2142 if (function_decl)
2143 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002144}
2145
Greg Claytona1e5dc82015-08-11 22:53:00 +00002146CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002147ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00002148 QualType block_type = m_ast_up->getBlockPointerType(
Kate Stoneb9c1b512016-09-06 20:57:50 +00002149 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002150
Kate Stoneb9c1b512016-09-06 20:57:50 +00002151 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002152}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002153
2154#pragma mark Array Types
2155
Kate Stoneb9c1b512016-09-06 20:57:50 +00002156CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2157 size_t element_count,
2158 bool is_vector) {
2159 if (element_type.IsValid()) {
2160 ASTContext *ast = getASTContext();
2161 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002162
Kate Stoneb9c1b512016-09-06 20:57:50 +00002163 if (is_vector) {
2164 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00002165 this, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2166 element_count)
2167 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002168 } else {
2169
2170 llvm::APInt ap_element_count(64, element_count);
2171 if (element_count == 0) {
Alex Langfordbddab072019-08-13 19:40:36 +00002172 return CompilerType(this, ast->getIncompleteArrayType(
2173 ClangUtil::GetQualType(element_type),
2174 clang::ArrayType::Normal, 0)
2175 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002176 } else {
Alex Langfordbddab072019-08-13 19:40:36 +00002177 return CompilerType(this, ast->getConstantArrayType(
2178 ClangUtil::GetQualType(element_type),
Richard Smith772e2662019-10-04 01:25:59 +00002179 ap_element_count, nullptr,
Alex Langfordbddab072019-08-13 19:40:36 +00002180 clang::ArrayType::Normal, 0)
2181 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002182 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002183 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002184 }
2185 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002186}
2187
Kate Stoneb9c1b512016-09-06 20:57:50 +00002188CompilerType ClangASTContext::CreateStructForIdentifier(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002189 ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002190 const std::initializer_list<std::pair<const char *, CompilerType>>
2191 &type_fields,
2192 bool packed) {
2193 CompilerType type;
2194 if (!type_name.IsEmpty() &&
2195 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2196 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002197 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002198 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002199 }
2200
2201 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2202 clang::TTK_Struct, lldb::eLanguageTypeC);
2203 StartTagDeclarationDefinition(type);
2204 for (const auto &field : type_fields)
2205 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2206 0);
2207 if (packed)
2208 SetIsPacked(type);
2209 CompleteTagDeclarationDefinition(type);
2210 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002211}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002212
Kate Stoneb9c1b512016-09-06 20:57:50 +00002213CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00002214 ConstString type_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002215 const std::initializer_list<std::pair<const char *, CompilerType>>
2216 &type_fields,
2217 bool packed) {
2218 CompilerType type;
2219 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2220 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002221
Kate Stoneb9c1b512016-09-06 20:57:50 +00002222 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002223}
2224
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002225#pragma mark Enumeration Types
2226
Greg Claytona1e5dc82015-08-11 22:53:00 +00002227CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002228ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2229 const Declaration &decl,
Tamas Berghammer59765832017-11-07 10:39:22 +00002230 const CompilerType &integer_clang_type,
2231 bool is_scoped) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002232 // TODO: Do something intelligent with the Declaration object passed in
2233 // like maybe filling in the SourceLocation with it...
2234 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002235
Kate Stoneb9c1b512016-09-06 20:57:50 +00002236 // TODO: ask about these...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002237 // const bool IsFixed = false;
2238
2239 EnumDecl *enum_decl = EnumDecl::Create(
2240 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2241 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
Tamas Berghammercf6bf4c2017-11-07 13:43:55 +00002242 is_scoped, // IsScoped
2243 is_scoped, // IsScopedUsingClassTag
2244 false); // IsFixed
Kate Stoneb9c1b512016-09-06 20:57:50 +00002245
2246 if (enum_decl) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00002247 if (decl_ctx)
2248 decl_ctx->addDecl(enum_decl);
2249
Kate Stoneb9c1b512016-09-06 20:57:50 +00002250 // TODO: check if we should be setting the promotion type too?
2251 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2252
2253 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2254
Alex Langfordbddab072019-08-13 19:40:36 +00002255 return CompilerType(this, ast->getTagDeclType(enum_decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002256 }
2257 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002258}
2259
Kate Stoneb9c1b512016-09-06 20:57:50 +00002260CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2261 size_t bit_size,
2262 bool is_signed) {
2263 if (ast) {
Alex Langfordbddab072019-08-13 19:40:36 +00002264 auto *clang_ast_context = ClangASTContext::GetASTContext(ast);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002265 if (is_signed) {
2266 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002267 return CompilerType(clang_ast_context,
2268 ast->SignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002269
2270 if (bit_size == ast->getTypeSize(ast->ShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002271 return CompilerType(clang_ast_context, ast->ShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002272
2273 if (bit_size == ast->getTypeSize(ast->IntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002274 return CompilerType(clang_ast_context, ast->IntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002275
2276 if (bit_size == ast->getTypeSize(ast->LongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002277 return CompilerType(clang_ast_context, ast->LongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002278
2279 if (bit_size == ast->getTypeSize(ast->LongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002280 return CompilerType(clang_ast_context,
2281 ast->LongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002282
2283 if (bit_size == ast->getTypeSize(ast->Int128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00002284 return CompilerType(clang_ast_context, ast->Int128Ty.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002285 } else {
2286 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002287 return CompilerType(clang_ast_context,
2288 ast->UnsignedCharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002289
2290 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002291 return CompilerType(clang_ast_context,
2292 ast->UnsignedShortTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002293
2294 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002295 return CompilerType(clang_ast_context,
2296 ast->UnsignedIntTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002297
2298 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002299 return CompilerType(clang_ast_context,
2300 ast->UnsignedLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002301
2302 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
Alex Langfordbddab072019-08-13 19:40:36 +00002303 return CompilerType(clang_ast_context,
2304 ast->UnsignedLongLongTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002305
2306 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
Alex Langfordbddab072019-08-13 19:40:36 +00002307 return CompilerType(clang_ast_context,
2308 ast->UnsignedInt128Ty.getAsOpaquePtr());
Enrico Granatae8bf7492014-08-15 23:00:02 +00002309 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002310 }
2311 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002312}
2313
Kate Stoneb9c1b512016-09-06 20:57:50 +00002314CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2315 bool is_signed) {
2316 if (ast)
2317 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2318 is_signed);
2319 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002320}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002321
Kate Stoneb9c1b512016-09-06 20:57:50 +00002322void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2323 if (decl_ctx) {
2324 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002325
Kate Stoneb9c1b512016-09-06 20:57:50 +00002326 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2327 if (named_decl) {
2328 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2329 named_decl->getDeclName().getAsString().c_str());
2330 } else {
2331 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002332 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002333 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002334}
2335
Kate Stoneb9c1b512016-09-06 20:57:50 +00002336void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2337 if (decl == nullptr)
2338 return;
2339 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002340
Kate Stoneb9c1b512016-09-06 20:57:50 +00002341 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2342 if (record_decl) {
2343 printf("%20s: %s%s\n", decl->getDeclKindName(),
2344 record_decl->getDeclName().getAsString().c_str(),
2345 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002346
Kate Stoneb9c1b512016-09-06 20:57:50 +00002347 } else {
2348 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2349 if (named_decl) {
2350 printf("%20s: %s\n", decl->getDeclKindName(),
2351 named_decl->getDeclName().getAsString().c_str());
2352 } else {
2353 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002354 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002355 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002356}
2357
Kate Stoneb9c1b512016-09-06 20:57:50 +00002358bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2359 clang::Decl *rhs_decl) {
2360 if (lhs_decl && rhs_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002361 // Make sure the decl kinds match first
Kate Stoneb9c1b512016-09-06 20:57:50 +00002362 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2363 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002364
Kate Stoneb9c1b512016-09-06 20:57:50 +00002365 if (lhs_decl_kind == rhs_decl_kind) {
Adrian Prantl05097242018-04-30 16:49:04 +00002366 // Now check that the decl contexts kinds are all equivalent before we
2367 // have to check any names of the decl contexts...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002368 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2369 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2370 if (lhs_decl_ctx && rhs_decl_ctx) {
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002371 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002372 if (lhs_decl_ctx && rhs_decl_ctx) {
2373 const clang::Decl::Kind lhs_decl_ctx_kind =
2374 lhs_decl_ctx->getDeclKind();
2375 const clang::Decl::Kind rhs_decl_ctx_kind =
2376 rhs_decl_ctx->getDeclKind();
2377 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2378 lhs_decl_ctx = lhs_decl_ctx->getParent();
2379 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002380
Kate Stoneb9c1b512016-09-06 20:57:50 +00002381 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2382 break;
2383 } else
2384 return false;
2385 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002386 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002387 }
2388
Kate Stoneb9c1b512016-09-06 20:57:50 +00002389 // Now make sure the name of the decls match
Kate Stoneb9c1b512016-09-06 20:57:50 +00002390 clang::NamedDecl *lhs_named_decl =
2391 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2392 clang::NamedDecl *rhs_named_decl =
2393 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2394 if (lhs_named_decl && rhs_named_decl) {
2395 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2396 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2397 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2398 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2399 return false;
2400 } else
Greg Claytona2721472011-06-25 00:44:06 +00002401 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002402 } else
2403 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002404
Adrian Prantl05097242018-04-30 16:49:04 +00002405 // We know that the decl context kinds all match, so now we need to
2406 // make sure the names match as well
Kate Stoneb9c1b512016-09-06 20:57:50 +00002407 lhs_decl_ctx = lhs_decl->getDeclContext();
2408 rhs_decl_ctx = rhs_decl->getDeclContext();
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00002409 while (true) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002410 switch (lhs_decl_ctx->getDeclKind()) {
2411 case clang::Decl::TranslationUnit:
2412 // We don't care about the translation unit names
2413 return true;
2414 default: {
2415 clang::NamedDecl *lhs_named_decl =
2416 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2417 clang::NamedDecl *rhs_named_decl =
2418 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2419 if (lhs_named_decl && rhs_named_decl) {
2420 clang::DeclarationName lhs_decl_name =
2421 lhs_named_decl->getDeclName();
2422 clang::DeclarationName rhs_decl_name =
2423 rhs_named_decl->getDeclName();
2424 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2425 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2426 return false;
2427 } else
2428 return false;
2429 } else
2430 return false;
2431 } break;
2432 }
2433 lhs_decl_ctx = lhs_decl_ctx->getParent();
2434 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002435 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002436 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002437 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002438 }
2439 return false;
2440}
2441bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2442 clang::Decl *decl) {
2443 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002444 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002445
Kate Stoneb9c1b512016-09-06 20:57:50 +00002446 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002447
Kate Stoneb9c1b512016-09-06 20:57:50 +00002448 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002449 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002450
2451 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2452 if (tag_decl->isCompleteDefinition())
2453 return true;
2454
2455 if (!tag_decl->hasExternalLexicalStorage())
2456 return false;
2457
2458 ast_source->CompleteType(tag_decl);
2459
2460 return !tag_decl->getTypeForDecl()->isIncompleteType();
2461 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2462 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2463 if (objc_interface_decl->getDefinition())
2464 return true;
2465
2466 if (!objc_interface_decl->hasExternalLexicalStorage())
2467 return false;
2468
2469 ast_source->CompleteType(objc_interface_decl);
2470
2471 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2472 } else {
2473 return false;
2474 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002475}
2476
Kate Stoneb9c1b512016-09-06 20:57:50 +00002477void ClangASTContext::SetMetadataAsUserID(const void *object,
2478 user_id_t user_id) {
2479 ClangASTMetadata meta_data;
2480 meta_data.SetUserID(user_id);
2481 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002482}
2483
Kate Stoneb9c1b512016-09-06 20:57:50 +00002484void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2485 ClangASTMetadata &metadata) {
2486 ClangExternalASTSourceCommon *external_source =
2487 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2488
2489 if (external_source)
2490 external_source->SetMetadata(object, metadata);
2491}
2492
2493ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2494 const void *object) {
2495 ClangExternalASTSourceCommon *external_source =
2496 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2497
2498 if (external_source && external_source->HasMetadata(object))
2499 return external_source->GetMetadata(object);
2500 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002501 return nullptr;
2502}
2503
Kate Stoneb9c1b512016-09-06 20:57:50 +00002504clang::DeclContext *
2505ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2506 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2507}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002508
Kate Stoneb9c1b512016-09-06 20:57:50 +00002509clang::DeclContext *
2510ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2511 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2512}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002513
Kate Stoneb9c1b512016-09-06 20:57:50 +00002514bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2515 int kind) const {
2516 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2517 if (clang_type) {
2518 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2519 if (tag_type) {
2520 clang::TagDecl *tag_decl =
2521 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2522 if (tag_decl) {
2523 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2524 return true;
2525 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002526 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002527 }
2528 return false;
2529}
2530
2531bool ClangASTContext::SetDefaultAccessForRecordFields(
2532 clang::RecordDecl *record_decl, int default_accessibility,
2533 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2534 if (record_decl) {
2535 uint32_t field_idx;
2536 clang::RecordDecl::field_iterator field, field_end;
2537 for (field = record_decl->field_begin(),
2538 field_end = record_decl->field_end(), field_idx = 0;
2539 field != field_end; ++field, ++field_idx) {
2540 // If no accessibility was assigned, assign the correct one
2541 if (field_idx < num_assigned_accessibilities &&
2542 assigned_accessibilities[field_idx] == clang::AS_none)
2543 field->setAccess((clang::AccessSpecifier)default_accessibility);
2544 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002545 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002546 }
2547 return false;
2548}
2549
2550clang::DeclContext *
2551ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2552 return GetDeclContextForType(ClangUtil::GetQualType(type));
2553}
2554
2555clang::DeclContext *
2556ClangASTContext::GetDeclContextForType(clang::QualType type) {
2557 if (type.isNull())
2558 return nullptr;
2559
2560 clang::QualType qual_type = type.getCanonicalType();
2561 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2562 switch (type_class) {
2563 case clang::Type::ObjCInterface:
2564 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2565 ->getInterface();
2566 case clang::Type::ObjCObjectPointer:
2567 return GetDeclContextForType(
2568 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2569 ->getPointeeType());
2570 case clang::Type::Record:
2571 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2572 case clang::Type::Enum:
2573 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2574 case clang::Type::Typedef:
2575 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2576 ->getDecl()
2577 ->getUnderlyingType());
2578 case clang::Type::Auto:
2579 return GetDeclContextForType(
2580 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2581 case clang::Type::Elaborated:
2582 return GetDeclContextForType(
2583 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2584 case clang::Type::Paren:
2585 return GetDeclContextForType(
2586 llvm::cast<clang::ParenType>(qual_type)->desugar());
2587 default:
2588 break;
2589 }
2590 // No DeclContext in this type...
2591 return nullptr;
2592}
2593
2594static bool GetCompleteQualType(clang::ASTContext *ast,
2595 clang::QualType qual_type,
2596 bool allow_completion = true) {
2597 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2598 switch (type_class) {
2599 case clang::Type::ConstantArray:
2600 case clang::Type::IncompleteArray:
2601 case clang::Type::VariableArray: {
2602 const clang::ArrayType *array_type =
2603 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2604
2605 if (array_type)
2606 return GetCompleteQualType(ast, array_type->getElementType(),
2607 allow_completion);
2608 } break;
2609 case clang::Type::Record: {
2610 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2611 if (cxx_record_decl) {
2612 if (cxx_record_decl->hasExternalLexicalStorage()) {
2613 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2614 const bool fields_loaded =
2615 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2616 if (is_complete && fields_loaded)
2617 return true;
2618
2619 if (!allow_completion)
2620 return false;
2621
2622 // Call the field_begin() accessor to for it to use the external source
2623 // to load the fields...
2624 clang::ExternalASTSource *external_ast_source =
2625 ast->getExternalSource();
2626 if (external_ast_source) {
2627 external_ast_source->CompleteType(cxx_record_decl);
2628 if (cxx_record_decl->isCompleteDefinition()) {
2629 cxx_record_decl->field_begin();
2630 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2631 }
2632 }
2633 }
2634 }
2635 const clang::TagType *tag_type =
2636 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2637 return !tag_type->isIncompleteType();
2638 } break;
2639
2640 case clang::Type::Enum: {
2641 const clang::TagType *tag_type =
2642 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2643 if (tag_type) {
2644 clang::TagDecl *tag_decl = tag_type->getDecl();
2645 if (tag_decl) {
2646 if (tag_decl->getDefinition())
2647 return true;
2648
2649 if (!allow_completion)
2650 return false;
2651
2652 if (tag_decl->hasExternalLexicalStorage()) {
2653 if (ast) {
2654 clang::ExternalASTSource *external_ast_source =
2655 ast->getExternalSource();
2656 if (external_ast_source) {
2657 external_ast_source->CompleteType(tag_decl);
2658 return !tag_type->isIncompleteType();
2659 }
2660 }
2661 }
2662 return false;
2663 }
2664 }
2665
2666 } break;
2667 case clang::Type::ObjCObject:
2668 case clang::Type::ObjCInterface: {
2669 const clang::ObjCObjectType *objc_class_type =
2670 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2671 if (objc_class_type) {
2672 clang::ObjCInterfaceDecl *class_interface_decl =
2673 objc_class_type->getInterface();
2674 // We currently can't complete objective C types through the newly added
Adrian Prantl05097242018-04-30 16:49:04 +00002675 // ASTContext because it only supports TagDecl objects right now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002676 if (class_interface_decl) {
2677 if (class_interface_decl->getDefinition())
2678 return true;
2679
2680 if (!allow_completion)
2681 return false;
2682
2683 if (class_interface_decl->hasExternalLexicalStorage()) {
2684 if (ast) {
2685 clang::ExternalASTSource *external_ast_source =
2686 ast->getExternalSource();
2687 if (external_ast_source) {
2688 external_ast_source->CompleteType(class_interface_decl);
2689 return !objc_class_type->isIncompleteType();
2690 }
2691 }
2692 }
2693 return false;
2694 }
2695 }
2696 } break;
2697
2698 case clang::Type::Typedef:
2699 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2700 ->getDecl()
2701 ->getUnderlyingType(),
2702 allow_completion);
2703
2704 case clang::Type::Auto:
2705 return GetCompleteQualType(
2706 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2707 allow_completion);
2708
2709 case clang::Type::Elaborated:
2710 return GetCompleteQualType(
2711 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2712 allow_completion);
2713
2714 case clang::Type::Paren:
2715 return GetCompleteQualType(
2716 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2717 allow_completion);
2718
2719 case clang::Type::Attributed:
2720 return GetCompleteQualType(
2721 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2722 allow_completion);
2723
2724 default:
2725 break;
2726 }
2727
2728 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002729}
2730
2731static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002732ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2733 switch (access) {
2734 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002735 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002736 case eAccessPublic:
2737 return clang::ObjCIvarDecl::Public;
2738 case eAccessPrivate:
2739 return clang::ObjCIvarDecl::Private;
2740 case eAccessProtected:
2741 return clang::ObjCIvarDecl::Protected;
2742 case eAccessPackage:
2743 return clang::ObjCIvarDecl::Package;
2744 }
2745 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002746}
2747
Greg Claytond8d4a572015-08-11 21:38:15 +00002748// Tests
Greg Claytond8d4a572015-08-11 21:38:15 +00002749
Kate Stoneb9c1b512016-09-06 20:57:50 +00002750bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2751 clang::QualType qual_type(GetCanonicalQualType(type));
2752
2753 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2754 switch (type_class) {
2755 case clang::Type::IncompleteArray:
2756 case clang::Type::VariableArray:
2757 case clang::Type::ConstantArray:
2758 case clang::Type::ExtVector:
2759 case clang::Type::Vector:
2760 case clang::Type::Record:
2761 case clang::Type::ObjCObject:
2762 case clang::Type::ObjCInterface:
2763 return true;
2764 case clang::Type::Auto:
2765 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2766 ->getDeducedType()
2767 .getAsOpaquePtr());
2768 case clang::Type::Elaborated:
2769 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2770 ->getNamedType()
2771 .getAsOpaquePtr());
2772 case clang::Type::Typedef:
2773 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2774 ->getDecl()
2775 ->getUnderlyingType()
2776 .getAsOpaquePtr());
2777 case clang::Type::Paren:
2778 return IsAggregateType(
2779 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2780 default:
2781 break;
2782 }
2783 // The clang type does have a value
2784 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002785}
2786
Kate Stoneb9c1b512016-09-06 20:57:50 +00002787bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2788 clang::QualType qual_type(GetCanonicalQualType(type));
2789
2790 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2791 switch (type_class) {
2792 case clang::Type::Record: {
2793 if (const clang::RecordType *record_type =
2794 llvm::dyn_cast_or_null<clang::RecordType>(
2795 qual_type.getTypePtrOrNull())) {
2796 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2797 return record_decl->isAnonymousStructOrUnion();
2798 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002799 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002800 break;
2801 }
2802 case clang::Type::Auto:
2803 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2804 ->getDeducedType()
2805 .getAsOpaquePtr());
2806 case clang::Type::Elaborated:
2807 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2808 ->getNamedType()
2809 .getAsOpaquePtr());
2810 case clang::Type::Typedef:
2811 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2812 ->getDecl()
2813 ->getUnderlyingType()
2814 .getAsOpaquePtr());
2815 case clang::Type::Paren:
2816 return IsAnonymousType(
2817 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2818 default:
2819 break;
2820 }
2821 // The clang type does have a value
2822 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002823}
2824
Kate Stoneb9c1b512016-09-06 20:57:50 +00002825bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2826 CompilerType *element_type_ptr,
2827 uint64_t *size, bool *is_incomplete) {
2828 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002829
Kate Stoneb9c1b512016-09-06 20:57:50 +00002830 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2831 switch (type_class) {
2832 default:
2833 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002834
Kate Stoneb9c1b512016-09-06 20:57:50 +00002835 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002836 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002837 element_type_ptr->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00002838 this, llvm::cast<clang::ConstantArrayType>(qual_type)
2839 ->getElementType()
2840 .getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00002841 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002842 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2843 ->getSize()
2844 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002845 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002846 *is_incomplete = false;
2847 return true;
2848
2849 case clang::Type::IncompleteArray:
2850 if (element_type_ptr)
2851 element_type_ptr->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00002852 this, llvm::cast<clang::IncompleteArrayType>(qual_type)
2853 ->getElementType()
2854 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002855 if (size)
2856 *size = 0;
2857 if (is_incomplete)
2858 *is_incomplete = true;
2859 return true;
2860
2861 case clang::Type::VariableArray:
2862 if (element_type_ptr)
2863 element_type_ptr->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00002864 this, llvm::cast<clang::VariableArrayType>(qual_type)
2865 ->getElementType()
2866 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002867 if (size)
2868 *size = 0;
2869 if (is_incomplete)
2870 *is_incomplete = false;
2871 return true;
2872
2873 case clang::Type::DependentSizedArray:
2874 if (element_type_ptr)
2875 element_type_ptr->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00002876 this, llvm::cast<clang::DependentSizedArrayType>(qual_type)
2877 ->getElementType()
2878 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002879 if (size)
2880 *size = 0;
2881 if (is_incomplete)
2882 *is_incomplete = false;
2883 return true;
2884
2885 case clang::Type::Typedef:
2886 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2887 ->getDecl()
2888 ->getUnderlyingType()
2889 .getAsOpaquePtr(),
2890 element_type_ptr, size, is_incomplete);
2891 case clang::Type::Auto:
2892 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2893 ->getDeducedType()
2894 .getAsOpaquePtr(),
2895 element_type_ptr, size, is_incomplete);
2896 case clang::Type::Elaborated:
2897 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2898 ->getNamedType()
2899 .getAsOpaquePtr(),
2900 element_type_ptr, size, is_incomplete);
2901 case clang::Type::Paren:
2902 return IsArrayType(
2903 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2904 element_type_ptr, size, is_incomplete);
2905 }
2906 if (element_type_ptr)
2907 element_type_ptr->Clear();
2908 if (size)
2909 *size = 0;
2910 if (is_incomplete)
2911 *is_incomplete = false;
2912 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002913}
2914
Kate Stoneb9c1b512016-09-06 20:57:50 +00002915bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2916 CompilerType *element_type, uint64_t *size) {
2917 clang::QualType qual_type(GetCanonicalQualType(type));
2918
2919 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2920 switch (type_class) {
2921 case clang::Type::Vector: {
2922 const clang::VectorType *vector_type =
2923 qual_type->getAs<clang::VectorType>();
2924 if (vector_type) {
2925 if (size)
2926 *size = vector_type->getNumElements();
2927 if (element_type)
2928 *element_type =
Alex Langfordbddab072019-08-13 19:40:36 +00002929 CompilerType(this, vector_type->getElementType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002930 }
2931 return true;
2932 } break;
2933 case clang::Type::ExtVector: {
2934 const clang::ExtVectorType *ext_vector_type =
2935 qual_type->getAs<clang::ExtVectorType>();
2936 if (ext_vector_type) {
2937 if (size)
2938 *size = ext_vector_type->getNumElements();
2939 if (element_type)
2940 *element_type =
Alex Langfordbddab072019-08-13 19:40:36 +00002941 CompilerType(this, ext_vector_type->getElementType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002942 }
2943 return true;
2944 }
2945 default:
2946 break;
2947 }
2948 return false;
2949}
2950
2951bool ClangASTContext::IsRuntimeGeneratedType(
2952 lldb::opaque_compiler_type_t type) {
2953 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
2954 ->GetDeclContextForType(GetQualType(type));
2955 if (!decl_ctx)
2956 return false;
2957
2958 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2959 return false;
2960
2961 clang::ObjCInterfaceDecl *result_iface_decl =
2962 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2963
2964 ClangASTMetadata *ast_metadata =
2965 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2966 if (!ast_metadata)
2967 return false;
2968 return (ast_metadata->GetISAPtr() != 0);
2969}
2970
2971bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
2972 return GetQualType(type).getUnqualifiedType()->isCharType();
2973}
2974
2975bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
2976 const bool allow_completion = false;
2977 return GetCompleteQualType(getASTContext(), GetQualType(type),
2978 allow_completion);
2979}
2980
2981bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
2982 return GetQualType(type).isConstQualified();
2983}
2984
2985bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
2986 uint32_t &length) {
2987 CompilerType pointee_or_element_clang_type;
2988 length = 0;
2989 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2990
2991 if (!pointee_or_element_clang_type.IsValid())
2992 return false;
2993
2994 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2995 if (pointee_or_element_clang_type.IsCharType()) {
2996 if (type_flags.Test(eTypeIsArray)) {
Adrian Prantl05097242018-04-30 16:49:04 +00002997 // We know the size of the array and it could be a C string since it is
2998 // an array of characters
Kate Stoneb9c1b512016-09-06 20:57:50 +00002999 length = llvm::cast<clang::ConstantArrayType>(
3000 GetCanonicalQualType(type).getTypePtr())
3001 ->getSize()
3002 .getLimitedValue();
3003 }
3004 return true;
3005 }
3006 }
3007 return false;
3008}
3009
3010bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
3011 bool *is_variadic_ptr) {
3012 if (type) {
3013 clang::QualType qual_type(GetCanonicalQualType(type));
3014
3015 if (qual_type->isFunctionType()) {
3016 if (is_variadic_ptr) {
3017 const clang::FunctionProtoType *function_proto_type =
3018 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3019 if (function_proto_type)
3020 *is_variadic_ptr = function_proto_type->isVariadic();
3021 else
3022 *is_variadic_ptr = false;
3023 }
3024 return true;
3025 }
3026
Greg Claytond8d4a572015-08-11 21:38:15 +00003027 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003028 switch (type_class) {
3029 default:
3030 break;
3031 case clang::Type::Typedef:
3032 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
3033 ->getDecl()
3034 ->getUnderlyingType()
3035 .getAsOpaquePtr(),
3036 nullptr);
3037 case clang::Type::Auto:
3038 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3039 ->getDeducedType()
3040 .getAsOpaquePtr(),
3041 nullptr);
3042 case clang::Type::Elaborated:
3043 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3044 ->getNamedType()
3045 .getAsOpaquePtr(),
3046 nullptr);
3047 case clang::Type::Paren:
3048 return IsFunctionType(
3049 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3050 nullptr);
3051 case clang::Type::LValueReference:
3052 case clang::Type::RValueReference: {
3053 const clang::ReferenceType *reference_type =
3054 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3055 if (reference_type)
3056 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3057 nullptr);
3058 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00003059 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003060 }
3061 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003062}
3063
3064// Used to detect "Homogeneous Floating-point Aggregates"
3065uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003066ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3067 CompilerType *base_type_ptr) {
3068 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003069 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003070
3071 clang::QualType qual_type(GetCanonicalQualType(type));
3072 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3073 switch (type_class) {
3074 case clang::Type::Record:
3075 if (GetCompleteType(type)) {
3076 const clang::CXXRecordDecl *cxx_record_decl =
3077 qual_type->getAsCXXRecordDecl();
3078 if (cxx_record_decl) {
3079 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3080 return 0;
3081 }
3082 const clang::RecordType *record_type =
3083 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3084 if (record_type) {
3085 const clang::RecordDecl *record_decl = record_type->getDecl();
3086 if (record_decl) {
3087 // We are looking for a structure that contains only floating point
3088 // types
3089 clang::RecordDecl::field_iterator field_pos,
3090 field_end = record_decl->field_end();
3091 uint32_t num_fields = 0;
3092 bool is_hva = false;
3093 bool is_hfa = false;
3094 clang::QualType base_qual_type;
3095 uint64_t base_bitwidth = 0;
3096 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3097 ++field_pos) {
3098 clang::QualType field_qual_type = field_pos->getType();
3099 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3100 if (field_qual_type->isFloatingType()) {
3101 if (field_qual_type->isComplexType())
3102 return 0;
3103 else {
3104 if (num_fields == 0)
3105 base_qual_type = field_qual_type;
3106 else {
3107 if (is_hva)
3108 return 0;
3109 is_hfa = true;
3110 if (field_qual_type.getTypePtr() !=
3111 base_qual_type.getTypePtr())
3112 return 0;
3113 }
3114 }
3115 } else if (field_qual_type->isVectorType() ||
3116 field_qual_type->isExtVectorType()) {
3117 if (num_fields == 0) {
3118 base_qual_type = field_qual_type;
3119 base_bitwidth = field_bitwidth;
3120 } else {
3121 if (is_hfa)
3122 return 0;
3123 is_hva = true;
3124 if (base_bitwidth != field_bitwidth)
3125 return 0;
3126 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3127 return 0;
3128 }
3129 } else
3130 return 0;
3131 ++num_fields;
3132 }
3133 if (base_type_ptr)
Alex Langfordbddab072019-08-13 19:40:36 +00003134 *base_type_ptr = CompilerType(this, base_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003135 return num_fields;
3136 }
3137 }
3138 }
3139 break;
3140
3141 case clang::Type::Typedef:
3142 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3143 ->getDecl()
3144 ->getUnderlyingType()
3145 .getAsOpaquePtr(),
3146 base_type_ptr);
3147
3148 case clang::Type::Auto:
3149 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3150 ->getDeducedType()
3151 .getAsOpaquePtr(),
3152 base_type_ptr);
3153
3154 case clang::Type::Elaborated:
3155 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3156 ->getNamedType()
3157 .getAsOpaquePtr(),
3158 base_type_ptr);
3159 default:
3160 break;
3161 }
3162 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003163}
3164
Kate Stoneb9c1b512016-09-06 20:57:50 +00003165size_t ClangASTContext::GetNumberOfFunctionArguments(
3166 lldb::opaque_compiler_type_t type) {
3167 if (type) {
3168 clang::QualType qual_type(GetCanonicalQualType(type));
3169 const clang::FunctionProtoType *func =
3170 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3171 if (func)
3172 return func->getNumParams();
3173 }
3174 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003175}
3176
Greg Claytona1e5dc82015-08-11 22:53:00 +00003177CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003178ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3179 const size_t index) {
3180 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003181 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003182 const clang::FunctionProtoType *func =
3183 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3184 if (func) {
3185 if (index < func->getNumParams())
Alex Langfordbddab072019-08-13 19:40:36 +00003186 return CompilerType(this, func->getParamType(index).getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003187 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003188 }
3189 return CompilerType();
3190}
3191
3192bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3193 if (type) {
3194 clang::QualType qual_type(GetCanonicalQualType(type));
3195
3196 if (qual_type->isFunctionPointerType())
3197 return true;
3198
3199 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3200 switch (type_class) {
3201 default:
3202 break;
3203 case clang::Type::Typedef:
3204 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3205 ->getDecl()
3206 ->getUnderlyingType()
3207 .getAsOpaquePtr());
3208 case clang::Type::Auto:
3209 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3210 ->getDeducedType()
3211 .getAsOpaquePtr());
3212 case clang::Type::Elaborated:
3213 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3214 ->getNamedType()
3215 .getAsOpaquePtr());
3216 case clang::Type::Paren:
3217 return IsFunctionPointerType(
3218 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3219
3220 case clang::Type::LValueReference:
3221 case clang::Type::RValueReference: {
3222 const clang::ReferenceType *reference_type =
3223 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3224 if (reference_type)
3225 return IsFunctionPointerType(
3226 reference_type->getPointeeType().getAsOpaquePtr());
3227 } break;
3228 }
3229 }
3230 return false;
3231}
3232
3233bool ClangASTContext::IsBlockPointerType(
3234 lldb::opaque_compiler_type_t type,
3235 CompilerType *function_pointer_type_ptr) {
3236 if (type) {
3237 clang::QualType qual_type(GetCanonicalQualType(type));
3238
3239 if (qual_type->isBlockPointerType()) {
3240 if (function_pointer_type_ptr) {
3241 const clang::BlockPointerType *block_pointer_type =
3242 qual_type->getAs<clang::BlockPointerType>();
3243 QualType pointee_type = block_pointer_type->getPointeeType();
Jonas Devlieghered5b44032019-02-13 06:25:41 +00003244 QualType function_pointer_type = m_ast_up->getPointerType(pointee_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003245 *function_pointer_type_ptr =
Alex Langfordbddab072019-08-13 19:40:36 +00003246 CompilerType(this, function_pointer_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003247 }
3248 return true;
3249 }
3250
3251 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3252 switch (type_class) {
3253 default:
3254 break;
3255 case clang::Type::Typedef:
3256 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3257 ->getDecl()
3258 ->getUnderlyingType()
3259 .getAsOpaquePtr(),
3260 function_pointer_type_ptr);
3261 case clang::Type::Auto:
3262 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3263 ->getDeducedType()
3264 .getAsOpaquePtr(),
3265 function_pointer_type_ptr);
3266 case clang::Type::Elaborated:
3267 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3268 ->getNamedType()
3269 .getAsOpaquePtr(),
3270 function_pointer_type_ptr);
3271 case clang::Type::Paren:
3272 return IsBlockPointerType(
3273 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3274 function_pointer_type_ptr);
3275
3276 case clang::Type::LValueReference:
3277 case clang::Type::RValueReference: {
3278 const clang::ReferenceType *reference_type =
3279 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3280 if (reference_type)
3281 return IsBlockPointerType(
3282 reference_type->getPointeeType().getAsOpaquePtr(),
3283 function_pointer_type_ptr);
3284 } break;
3285 }
3286 }
3287 return false;
3288}
3289
3290bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3291 bool &is_signed) {
3292 if (!type)
3293 return false;
3294
3295 clang::QualType qual_type(GetCanonicalQualType(type));
3296 const clang::BuiltinType *builtin_type =
3297 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3298
3299 if (builtin_type) {
3300 if (builtin_type->isInteger()) {
3301 is_signed = builtin_type->isSignedInteger();
3302 return true;
3303 }
3304 }
3305
3306 return false;
3307}
3308
3309bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3310 bool &is_signed) {
3311 if (type) {
3312 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3313 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3314
3315 if (enum_type) {
3316 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3317 is_signed);
3318 return true;
3319 }
3320 }
3321
3322 return false;
3323}
3324
3325bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3326 CompilerType *pointee_type) {
3327 if (type) {
3328 clang::QualType qual_type(GetCanonicalQualType(type));
3329 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3330 switch (type_class) {
3331 case clang::Type::Builtin:
3332 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3333 default:
3334 break;
3335 case clang::BuiltinType::ObjCId:
3336 case clang::BuiltinType::ObjCClass:
3337 return true;
3338 }
3339 return false;
3340 case clang::Type::ObjCObjectPointer:
3341 if (pointee_type)
3342 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003343 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3344 ->getPointeeType()
3345 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003346 return true;
3347 case clang::Type::BlockPointer:
3348 if (pointee_type)
3349 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003350 this, llvm::cast<clang::BlockPointerType>(qual_type)
3351 ->getPointeeType()
3352 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003353 return true;
3354 case clang::Type::Pointer:
3355 if (pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003356 pointee_type->SetCompilerType(this,
3357 llvm::cast<clang::PointerType>(qual_type)
3358 ->getPointeeType()
3359 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003360 return true;
3361 case clang::Type::MemberPointer:
3362 if (pointee_type)
3363 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003364 this, llvm::cast<clang::MemberPointerType>(qual_type)
3365 ->getPointeeType()
3366 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003367 return true;
3368 case clang::Type::Typedef:
3369 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3370 ->getDecl()
3371 ->getUnderlyingType()
3372 .getAsOpaquePtr(),
3373 pointee_type);
3374 case clang::Type::Auto:
3375 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3376 ->getDeducedType()
3377 .getAsOpaquePtr(),
3378 pointee_type);
3379 case clang::Type::Elaborated:
3380 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3381 ->getNamedType()
3382 .getAsOpaquePtr(),
3383 pointee_type);
3384 case clang::Type::Paren:
3385 return IsPointerType(
3386 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3387 pointee_type);
3388 default:
3389 break;
3390 }
3391 }
3392 if (pointee_type)
3393 pointee_type->Clear();
3394 return false;
3395}
3396
3397bool ClangASTContext::IsPointerOrReferenceType(
3398 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3399 if (type) {
3400 clang::QualType qual_type(GetCanonicalQualType(type));
3401 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3402 switch (type_class) {
3403 case clang::Type::Builtin:
3404 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3405 default:
3406 break;
3407 case clang::BuiltinType::ObjCId:
3408 case clang::BuiltinType::ObjCClass:
3409 return true;
3410 }
3411 return false;
3412 case clang::Type::ObjCObjectPointer:
3413 if (pointee_type)
3414 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003415 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3416 ->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003417 return true;
3418 case clang::Type::BlockPointer:
3419 if (pointee_type)
3420 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003421 this, llvm::cast<clang::BlockPointerType>(qual_type)
3422 ->getPointeeType()
3423 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003424 return true;
3425 case clang::Type::Pointer:
3426 if (pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003427 pointee_type->SetCompilerType(this,
3428 llvm::cast<clang::PointerType>(qual_type)
3429 ->getPointeeType()
3430 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003431 return true;
3432 case clang::Type::MemberPointer:
3433 if (pointee_type)
3434 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003435 this, llvm::cast<clang::MemberPointerType>(qual_type)
3436 ->getPointeeType()
3437 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003438 return true;
3439 case clang::Type::LValueReference:
3440 if (pointee_type)
3441 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003442 this, llvm::cast<clang::LValueReferenceType>(qual_type)
3443 ->desugar()
3444 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003445 return true;
3446 case clang::Type::RValueReference:
3447 if (pointee_type)
3448 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003449 this, llvm::cast<clang::RValueReferenceType>(qual_type)
3450 ->desugar()
3451 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003452 return true;
3453 case clang::Type::Typedef:
3454 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3455 ->getDecl()
3456 ->getUnderlyingType()
3457 .getAsOpaquePtr(),
3458 pointee_type);
3459 case clang::Type::Auto:
3460 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3461 ->getDeducedType()
3462 .getAsOpaquePtr(),
3463 pointee_type);
3464 case clang::Type::Elaborated:
3465 return IsPointerOrReferenceType(
3466 llvm::cast<clang::ElaboratedType>(qual_type)
3467 ->getNamedType()
3468 .getAsOpaquePtr(),
3469 pointee_type);
3470 case clang::Type::Paren:
3471 return IsPointerOrReferenceType(
3472 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3473 pointee_type);
3474 default:
3475 break;
3476 }
3477 }
3478 if (pointee_type)
3479 pointee_type->Clear();
3480 return false;
3481}
3482
3483bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3484 CompilerType *pointee_type,
3485 bool *is_rvalue) {
3486 if (type) {
3487 clang::QualType qual_type(GetCanonicalQualType(type));
3488 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3489
3490 switch (type_class) {
3491 case clang::Type::LValueReference:
3492 if (pointee_type)
3493 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003494 this, llvm::cast<clang::LValueReferenceType>(qual_type)
3495 ->desugar()
3496 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003497 if (is_rvalue)
3498 *is_rvalue = false;
3499 return true;
3500 case clang::Type::RValueReference:
3501 if (pointee_type)
3502 pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003503 this, llvm::cast<clang::RValueReferenceType>(qual_type)
3504 ->desugar()
3505 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003506 if (is_rvalue)
3507 *is_rvalue = true;
3508 return true;
3509 case clang::Type::Typedef:
3510 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3511 ->getDecl()
3512 ->getUnderlyingType()
3513 .getAsOpaquePtr(),
3514 pointee_type, is_rvalue);
3515 case clang::Type::Auto:
3516 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3517 ->getDeducedType()
3518 .getAsOpaquePtr(),
3519 pointee_type, is_rvalue);
3520 case clang::Type::Elaborated:
3521 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3522 ->getNamedType()
3523 .getAsOpaquePtr(),
3524 pointee_type, is_rvalue);
3525 case clang::Type::Paren:
3526 return IsReferenceType(
3527 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3528 pointee_type, is_rvalue);
3529
3530 default:
3531 break;
3532 }
3533 }
3534 if (pointee_type)
3535 pointee_type->Clear();
3536 return false;
3537}
3538
3539bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3540 uint32_t &count, bool &is_complex) {
3541 if (type) {
3542 clang::QualType qual_type(GetCanonicalQualType(type));
3543
3544 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3545 qual_type->getCanonicalTypeInternal())) {
3546 clang::BuiltinType::Kind kind = BT->getKind();
3547 if (kind >= clang::BuiltinType::Float &&
3548 kind <= clang::BuiltinType::LongDouble) {
3549 count = 1;
3550 is_complex = false;
3551 return true;
3552 }
3553 } else if (const clang::ComplexType *CT =
3554 llvm::dyn_cast<clang::ComplexType>(
3555 qual_type->getCanonicalTypeInternal())) {
3556 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3557 is_complex)) {
3558 count = 2;
3559 is_complex = true;
3560 return true;
3561 }
3562 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3563 qual_type->getCanonicalTypeInternal())) {
3564 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3565 is_complex)) {
3566 count = VT->getNumElements();
3567 is_complex = false;
3568 return true;
3569 }
3570 }
3571 }
3572 count = 0;
3573 is_complex = false;
3574 return false;
3575}
3576
3577bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3578 if (!type)
3579 return false;
3580
3581 clang::QualType qual_type(GetQualType(type));
3582 const clang::TagType *tag_type =
3583 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3584 if (tag_type) {
3585 clang::TagDecl *tag_decl = tag_type->getDecl();
3586 if (tag_decl)
3587 return tag_decl->isCompleteDefinition();
3588 return false;
3589 } else {
3590 const clang::ObjCObjectType *objc_class_type =
3591 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3592 if (objc_class_type) {
3593 clang::ObjCInterfaceDecl *class_interface_decl =
3594 objc_class_type->getInterface();
3595 if (class_interface_decl)
3596 return class_interface_decl->getDefinition() != nullptr;
3597 return false;
3598 }
3599 }
3600 return true;
3601}
3602
3603bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
Raphael Isemann79b3cce2019-11-08 12:03:28 +01003604 if (ClangUtil::IsClangType(type)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003605 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3606
3607 const clang::ObjCObjectPointerType *obj_pointer_type =
3608 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3609
3610 if (obj_pointer_type)
3611 return obj_pointer_type->isObjCClassType();
3612 }
3613 return false;
3614}
3615
3616bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3617 if (ClangUtil::IsClangType(type))
3618 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3619 return false;
3620}
3621
3622bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3623 if (!type)
3624 return false;
3625 clang::QualType qual_type(GetCanonicalQualType(type));
3626 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3627 return (type_class == clang::Type::Record);
3628}
3629
3630bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3631 if (!type)
3632 return false;
3633 clang::QualType qual_type(GetCanonicalQualType(type));
3634 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3635 return (type_class == clang::Type::Enum);
3636}
3637
3638bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3639 if (type) {
3640 clang::QualType qual_type(GetCanonicalQualType(type));
3641 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3642 switch (type_class) {
3643 case clang::Type::Record:
3644 if (GetCompleteType(type)) {
3645 const clang::RecordType *record_type =
3646 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3647 const clang::RecordDecl *record_decl = record_type->getDecl();
3648 if (record_decl) {
3649 const clang::CXXRecordDecl *cxx_record_decl =
3650 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3651 if (cxx_record_decl)
3652 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003653 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003654 }
3655 break;
3656
3657 default:
3658 break;
3659 }
3660 }
3661 return false;
3662}
3663
3664bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3665 CompilerType *dynamic_pointee_type,
3666 bool check_cplusplus,
3667 bool check_objc) {
3668 clang::QualType pointee_qual_type;
3669 if (type) {
3670 clang::QualType qual_type(GetCanonicalQualType(type));
3671 bool success = false;
3672 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3673 switch (type_class) {
3674 case clang::Type::Builtin:
3675 if (check_objc &&
3676 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3677 clang::BuiltinType::ObjCId) {
3678 if (dynamic_pointee_type)
3679 dynamic_pointee_type->SetCompilerType(this, type);
3680 return true;
3681 }
3682 break;
3683
3684 case clang::Type::ObjCObjectPointer:
3685 if (check_objc) {
3686 if (auto objc_pointee_type =
3687 qual_type->getPointeeType().getTypePtrOrNull()) {
3688 if (auto objc_object_type =
3689 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3690 objc_pointee_type)) {
3691 if (objc_object_type->isObjCClass())
3692 return false;
3693 }
3694 }
3695 if (dynamic_pointee_type)
3696 dynamic_pointee_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003697 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3698 ->getPointeeType()
3699 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003700 return true;
3701 }
3702 break;
3703
3704 case clang::Type::Pointer:
3705 pointee_qual_type =
3706 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3707 success = true;
3708 break;
3709
3710 case clang::Type::LValueReference:
3711 case clang::Type::RValueReference:
3712 pointee_qual_type =
3713 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3714 success = true;
3715 break;
3716
3717 case clang::Type::Typedef:
3718 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3719 ->getDecl()
3720 ->getUnderlyingType()
3721 .getAsOpaquePtr(),
3722 dynamic_pointee_type, check_cplusplus,
3723 check_objc);
3724
3725 case clang::Type::Auto:
3726 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3727 ->getDeducedType()
3728 .getAsOpaquePtr(),
3729 dynamic_pointee_type, check_cplusplus,
3730 check_objc);
3731
3732 case clang::Type::Elaborated:
3733 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3734 ->getNamedType()
3735 .getAsOpaquePtr(),
3736 dynamic_pointee_type, check_cplusplus,
3737 check_objc);
3738
3739 case clang::Type::Paren:
3740 return IsPossibleDynamicType(
3741 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3742 dynamic_pointee_type, check_cplusplus, check_objc);
3743 default:
3744 break;
3745 }
3746
3747 if (success) {
3748 // Check to make sure what we are pointing too is a possible dynamic C++
Adrian Prantl05097242018-04-30 16:49:04 +00003749 // type We currently accept any "void *" (in case we have a class that
3750 // has been watered down to an opaque pointer) and virtual C++ classes.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003751 const clang::Type::TypeClass pointee_type_class =
3752 pointee_qual_type.getCanonicalType()->getTypeClass();
3753 switch (pointee_type_class) {
3754 case clang::Type::Builtin:
3755 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3756 case clang::BuiltinType::UnknownAny:
3757 case clang::BuiltinType::Void:
3758 if (dynamic_pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003759 dynamic_pointee_type->SetCompilerType(
3760 this, pointee_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003761 return true;
3762 default:
3763 break;
3764 }
3765 break;
3766
3767 case clang::Type::Record:
3768 if (check_cplusplus) {
3769 clang::CXXRecordDecl *cxx_record_decl =
3770 pointee_qual_type->getAsCXXRecordDecl();
3771 if (cxx_record_decl) {
3772 bool is_complete = cxx_record_decl->isCompleteDefinition();
3773
3774 if (is_complete)
3775 success = cxx_record_decl->isDynamicClass();
3776 else {
3777 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3778 getASTContext(), cxx_record_decl);
3779 if (metadata)
3780 success = metadata->GetIsDynamicCXXType();
3781 else {
Alex Langfordbddab072019-08-13 19:40:36 +00003782 is_complete =
3783 CompilerType(this, pointee_qual_type.getAsOpaquePtr())
3784 .GetCompleteType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003785 if (is_complete)
3786 success = cxx_record_decl->isDynamicClass();
3787 else
3788 success = false;
3789 }
3790 }
3791
3792 if (success) {
3793 if (dynamic_pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003794 dynamic_pointee_type->SetCompilerType(
3795 this, pointee_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003796 return true;
3797 }
3798 }
3799 }
3800 break;
3801
3802 case clang::Type::ObjCObject:
3803 case clang::Type::ObjCInterface:
3804 if (check_objc) {
3805 if (dynamic_pointee_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003806 dynamic_pointee_type->SetCompilerType(
3807 this, pointee_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003808 return true;
3809 }
3810 break;
3811
3812 default:
3813 break;
3814 }
3815 }
3816 }
3817 if (dynamic_pointee_type)
3818 dynamic_pointee_type->Clear();
3819 return false;
3820}
3821
3822bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3823 if (!type)
3824 return false;
3825
3826 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3827}
3828
3829bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3830 if (!type)
3831 return false;
3832 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3833}
3834
3835bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3836 if (!type)
3837 return false;
3838 return GetCanonicalQualType(type)->isVoidType();
3839}
3840
Alex Langforda03e2b22019-06-04 19:29:59 +00003841bool ClangASTContext::CanPassInRegisters(const CompilerType &type) {
3842 if (auto *record_decl =
3843 ClangASTContext::GetAsRecordDecl(type)) {
3844 return record_decl->canPassInRegisters();
3845 }
3846 return false;
3847}
3848
Kate Stoneb9c1b512016-09-06 20:57:50 +00003849bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3850 return ClangASTContextSupportsLanguage(language);
3851}
3852
Alex Langforddb542452019-10-30 12:50:05 -07003853Optional<std::string>
3854ClangASTContext::GetCXXClassName(const CompilerType &type) {
3855 if (!type)
3856 return llvm::None;
3857
3858 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3859 if (qual_type.isNull())
3860 return llvm::None;
3861
3862 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3863 if (!cxx_record_decl)
3864 return llvm::None;
3865
3866 return std::string(cxx_record_decl->getIdentifier()->getNameStart());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003867}
3868
3869bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3870 if (!type)
3871 return false;
3872
3873 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Jonas Devliegherea6682a42018-12-15 00:15:33 +00003874 return !qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003875}
3876
3877bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3878 if (!type)
3879 return false;
3880 clang::QualType qual_type(GetCanonicalQualType(type));
3881 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3882 if (tag_type)
3883 return tag_type->isBeingDefined();
3884 return false;
3885}
3886
3887bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3888 CompilerType *class_type_ptr) {
Raphael Isemann79b3cce2019-11-08 12:03:28 +01003889 if (!ClangUtil::IsClangType(type))
Kate Stoneb9c1b512016-09-06 20:57:50 +00003890 return false;
3891
3892 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3893
3894 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3895 if (class_type_ptr) {
3896 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3897 const clang::ObjCObjectPointerType *obj_pointer_type =
3898 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3899 if (obj_pointer_type == nullptr)
3900 class_type_ptr->Clear();
3901 else
3902 class_type_ptr->SetCompilerType(
3903 type.GetTypeSystem(),
3904 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3905 .getAsOpaquePtr());
3906 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003907 }
3908 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003909 }
3910 if (class_type_ptr)
3911 class_type_ptr->Clear();
3912 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003913}
3914
Greg Claytond8d4a572015-08-11 21:38:15 +00003915// Type Completion
Greg Claytond8d4a572015-08-11 21:38:15 +00003916
Kate Stoneb9c1b512016-09-06 20:57:50 +00003917bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3918 if (!type)
3919 return false;
3920 const bool allow_completion = true;
3921 return GetCompleteQualType(getASTContext(), GetQualType(type),
3922 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00003923}
3924
Kate Stoneb9c1b512016-09-06 20:57:50 +00003925ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
3926 std::string type_name;
3927 if (type) {
3928 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
3929 clang::QualType qual_type(GetQualType(type));
3930 printing_policy.SuppressTagKeyword = true;
3931 const clang::TypedefType *typedef_type =
3932 qual_type->getAs<clang::TypedefType>();
3933 if (typedef_type) {
3934 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3935 type_name = typedef_decl->getQualifiedNameAsString();
3936 } else {
3937 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003938 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003939 }
3940 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00003941}
3942
3943uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003944ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
3945 CompilerType *pointee_or_element_clang_type) {
3946 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003947 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003948
3949 if (pointee_or_element_clang_type)
3950 pointee_or_element_clang_type->Clear();
3951
3952 clang::QualType qual_type(GetQualType(type));
3953
3954 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3955 switch (type_class) {
Sean Callananddf802a2017-06-02 01:24:18 +00003956 case clang::Type::Attributed:
3957 return GetTypeInfo(
3958 qual_type->getAs<clang::AttributedType>()
3959 ->getModifiedType().getAsOpaquePtr(),
3960 pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003961 case clang::Type::Builtin: {
3962 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3963 qual_type->getCanonicalTypeInternal());
3964
3965 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3966 switch (builtin_type->getKind()) {
3967 case clang::BuiltinType::ObjCId:
3968 case clang::BuiltinType::ObjCClass:
3969 if (pointee_or_element_clang_type)
3970 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00003971 this, getASTContext()->ObjCBuiltinClassTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003972 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3973 break;
3974
3975 case clang::BuiltinType::ObjCSel:
3976 if (pointee_or_element_clang_type)
Alex Langfordbddab072019-08-13 19:40:36 +00003977 pointee_or_element_clang_type->SetCompilerType(
3978 this, getASTContext()->CharTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003979 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3980 break;
3981
3982 case clang::BuiltinType::Bool:
3983 case clang::BuiltinType::Char_U:
3984 case clang::BuiltinType::UChar:
3985 case clang::BuiltinType::WChar_U:
3986 case clang::BuiltinType::Char16:
3987 case clang::BuiltinType::Char32:
3988 case clang::BuiltinType::UShort:
3989 case clang::BuiltinType::UInt:
3990 case clang::BuiltinType::ULong:
3991 case clang::BuiltinType::ULongLong:
3992 case clang::BuiltinType::UInt128:
3993 case clang::BuiltinType::Char_S:
3994 case clang::BuiltinType::SChar:
3995 case clang::BuiltinType::WChar_S:
3996 case clang::BuiltinType::Short:
3997 case clang::BuiltinType::Int:
3998 case clang::BuiltinType::Long:
3999 case clang::BuiltinType::LongLong:
4000 case clang::BuiltinType::Int128:
4001 case clang::BuiltinType::Float:
4002 case clang::BuiltinType::Double:
4003 case clang::BuiltinType::LongDouble:
4004 builtin_type_flags |= eTypeIsScalar;
4005 if (builtin_type->isInteger()) {
4006 builtin_type_flags |= eTypeIsInteger;
4007 if (builtin_type->isSignedInteger())
4008 builtin_type_flags |= eTypeIsSigned;
4009 } else if (builtin_type->isFloatingPoint())
4010 builtin_type_flags |= eTypeIsFloat;
4011 break;
4012 default:
4013 break;
4014 }
4015 return builtin_type_flags;
4016 }
4017
4018 case clang::Type::BlockPointer:
4019 if (pointee_or_element_clang_type)
4020 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004021 this, qual_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004022 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
4023
4024 case clang::Type::Complex: {
4025 uint32_t complex_type_flags =
4026 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
4027 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
4028 qual_type->getCanonicalTypeInternal());
4029 if (complex_type) {
4030 clang::QualType complex_element_type(complex_type->getElementType());
4031 if (complex_element_type->isIntegerType())
4032 complex_type_flags |= eTypeIsFloat;
4033 else if (complex_element_type->isFloatingType())
4034 complex_type_flags |= eTypeIsInteger;
4035 }
4036 return complex_type_flags;
4037 } break;
4038
4039 case clang::Type::ConstantArray:
4040 case clang::Type::DependentSizedArray:
4041 case clang::Type::IncompleteArray:
4042 case clang::Type::VariableArray:
4043 if (pointee_or_element_clang_type)
4044 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004045 this, llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4046 ->getElementType()
4047 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004048 return eTypeHasChildren | eTypeIsArray;
4049
4050 case clang::Type::DependentName:
4051 return 0;
4052 case clang::Type::DependentSizedExtVector:
4053 return eTypeHasChildren | eTypeIsVector;
4054 case clang::Type::DependentTemplateSpecialization:
4055 return eTypeIsTemplate;
4056 case clang::Type::Decltype:
Alex Langfordbddab072019-08-13 19:40:36 +00004057 return CompilerType(this, llvm::cast<clang::DecltypeType>(qual_type)
4058 ->getUnderlyingType()
4059 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004060 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004061
4062 case clang::Type::Enum:
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::EnumType>(qual_type)
4066 ->getDecl()
4067 ->getIntegerType()
4068 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004069 return eTypeIsEnumeration | eTypeHasValue;
4070
4071 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00004072 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
4073 ->getDeducedType()
4074 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004075 .GetTypeInfo(pointee_or_element_clang_type);
4076 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00004077 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
4078 ->getNamedType()
4079 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004080 .GetTypeInfo(pointee_or_element_clang_type);
4081 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00004082 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
4083 ->desugar()
4084 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004085 .GetTypeInfo(pointee_or_element_clang_type);
4086
4087 case clang::Type::FunctionProto:
4088 return eTypeIsFuncPrototype | eTypeHasValue;
4089 case clang::Type::FunctionNoProto:
4090 return eTypeIsFuncPrototype | eTypeHasValue;
4091 case clang::Type::InjectedClassName:
4092 return 0;
4093
4094 case clang::Type::LValueReference:
4095 case clang::Type::RValueReference:
4096 if (pointee_or_element_clang_type)
4097 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004098 this, llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4099 ->getPointeeType()
4100 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004101 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4102
4103 case clang::Type::MemberPointer:
4104 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4105
4106 case clang::Type::ObjCObjectPointer:
4107 if (pointee_or_element_clang_type)
4108 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004109 this, qual_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004110 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4111 eTypeHasValue;
4112
4113 case clang::Type::ObjCObject:
4114 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4115 case clang::Type::ObjCInterface:
4116 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4117
4118 case clang::Type::Pointer:
4119 if (pointee_or_element_clang_type)
4120 pointee_or_element_clang_type->SetCompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004121 this, qual_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004122 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4123
4124 case clang::Type::Record:
4125 if (qual_type->getAsCXXRecordDecl())
4126 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4127 else
4128 return eTypeHasChildren | eTypeIsStructUnion;
4129 break;
4130 case clang::Type::SubstTemplateTypeParm:
4131 return eTypeIsTemplate;
4132 case clang::Type::TemplateTypeParm:
4133 return eTypeIsTemplate;
4134 case clang::Type::TemplateSpecialization:
4135 return eTypeIsTemplate;
4136
4137 case clang::Type::Typedef:
4138 return eTypeIsTypedef |
Alex Langfordbddab072019-08-13 19:40:36 +00004139 CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
4140 ->getDecl()
4141 ->getUnderlyingType()
4142 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004143 .GetTypeInfo(pointee_or_element_clang_type);
4144 case clang::Type::TypeOfExpr:
Alex Langfordbddab072019-08-13 19:40:36 +00004145 return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type)
4146 ->getUnderlyingExpr()
4147 ->getType()
4148 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004149 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004150 case clang::Type::TypeOf:
Alex Langfordbddab072019-08-13 19:40:36 +00004151 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
4152 ->getUnderlyingType()
4153 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004154 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004155 case clang::Type::UnresolvedUsing:
4156 return 0;
4157
4158 case clang::Type::ExtVector:
4159 case clang::Type::Vector: {
4160 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4161 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4162 qual_type->getCanonicalTypeInternal());
4163 if (vector_type) {
4164 if (vector_type->isIntegerType())
4165 vector_type_flags |= eTypeIsFloat;
4166 else if (vector_type->isFloatingType())
4167 vector_type_flags |= eTypeIsInteger;
4168 }
4169 return vector_type_flags;
4170 }
4171 default:
4172 return 0;
4173 }
4174 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004175}
4176
Greg Claytond8d4a572015-08-11 21:38:15 +00004177lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004178ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4179 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004180 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004181
4182 // If the type is a reference, then resolve it to what it refers to first:
4183 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4184 if (qual_type->isAnyPointerType()) {
4185 if (qual_type->isObjCObjectPointerType())
4186 return lldb::eLanguageTypeObjC;
Adrian Prantl1db0f0c2019-05-02 23:07:23 +00004187 if (qual_type->getPointeeCXXRecordDecl())
4188 return lldb::eLanguageTypeC_plus_plus;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004189
4190 clang::QualType pointee_type(qual_type->getPointeeType());
Adrian Prantl1db0f0c2019-05-02 23:07:23 +00004191 if (pointee_type->getPointeeCXXRecordDecl())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004192 return lldb::eLanguageTypeC_plus_plus;
4193 if (pointee_type->isObjCObjectOrInterfaceType())
4194 return lldb::eLanguageTypeObjC;
4195 if (pointee_type->isObjCClassType())
4196 return lldb::eLanguageTypeObjC;
4197 if (pointee_type.getTypePtr() ==
4198 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4199 return lldb::eLanguageTypeObjC;
4200 } else {
4201 if (qual_type->isObjCObjectOrInterfaceType())
4202 return lldb::eLanguageTypeObjC;
4203 if (qual_type->getAsCXXRecordDecl())
4204 return lldb::eLanguageTypeC_plus_plus;
4205 switch (qual_type->getTypeClass()) {
4206 default:
4207 break;
4208 case clang::Type::Builtin:
4209 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4210 default:
4211 case clang::BuiltinType::Void:
4212 case clang::BuiltinType::Bool:
4213 case clang::BuiltinType::Char_U:
4214 case clang::BuiltinType::UChar:
4215 case clang::BuiltinType::WChar_U:
4216 case clang::BuiltinType::Char16:
4217 case clang::BuiltinType::Char32:
4218 case clang::BuiltinType::UShort:
4219 case clang::BuiltinType::UInt:
4220 case clang::BuiltinType::ULong:
4221 case clang::BuiltinType::ULongLong:
4222 case clang::BuiltinType::UInt128:
4223 case clang::BuiltinType::Char_S:
4224 case clang::BuiltinType::SChar:
4225 case clang::BuiltinType::WChar_S:
4226 case clang::BuiltinType::Short:
4227 case clang::BuiltinType::Int:
4228 case clang::BuiltinType::Long:
4229 case clang::BuiltinType::LongLong:
4230 case clang::BuiltinType::Int128:
4231 case clang::BuiltinType::Float:
4232 case clang::BuiltinType::Double:
4233 case clang::BuiltinType::LongDouble:
4234 break;
4235
4236 case clang::BuiltinType::NullPtr:
4237 return eLanguageTypeC_plus_plus;
4238
4239 case clang::BuiltinType::ObjCId:
4240 case clang::BuiltinType::ObjCClass:
4241 case clang::BuiltinType::ObjCSel:
4242 return eLanguageTypeObjC;
4243
4244 case clang::BuiltinType::Dependent:
4245 case clang::BuiltinType::Overload:
4246 case clang::BuiltinType::BoundMember:
4247 case clang::BuiltinType::UnknownAny:
4248 break;
4249 }
4250 break;
4251 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00004252 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
4253 ->getDecl()
4254 ->getUnderlyingType()
4255 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004256 .GetMinimumLanguage();
4257 }
4258 }
4259 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004260}
4261
4262lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004263ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4264 if (!type)
4265 return lldb::eTypeClassInvalid;
4266
4267 clang::QualType qual_type(GetQualType(type));
4268
4269 switch (qual_type->getTypeClass()) {
4270 case clang::Type::UnaryTransform:
4271 break;
4272 case clang::Type::FunctionNoProto:
4273 return lldb::eTypeClassFunction;
4274 case clang::Type::FunctionProto:
4275 return lldb::eTypeClassFunction;
4276 case clang::Type::IncompleteArray:
4277 return lldb::eTypeClassArray;
4278 case clang::Type::VariableArray:
4279 return lldb::eTypeClassArray;
4280 case clang::Type::ConstantArray:
4281 return lldb::eTypeClassArray;
4282 case clang::Type::DependentSizedArray:
4283 return lldb::eTypeClassArray;
4284 case clang::Type::DependentSizedExtVector:
4285 return lldb::eTypeClassVector;
Fangrui Song8f284882018-07-13 22:40:40 +00004286 case clang::Type::DependentVector:
4287 return lldb::eTypeClassVector;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004288 case clang::Type::ExtVector:
4289 return lldb::eTypeClassVector;
4290 case clang::Type::Vector:
4291 return lldb::eTypeClassVector;
4292 case clang::Type::Builtin:
4293 return lldb::eTypeClassBuiltin;
4294 case clang::Type::ObjCObjectPointer:
4295 return lldb::eTypeClassObjCObjectPointer;
4296 case clang::Type::BlockPointer:
4297 return lldb::eTypeClassBlockPointer;
4298 case clang::Type::Pointer:
4299 return lldb::eTypeClassPointer;
4300 case clang::Type::LValueReference:
4301 return lldb::eTypeClassReference;
4302 case clang::Type::RValueReference:
4303 return lldb::eTypeClassReference;
4304 case clang::Type::MemberPointer:
4305 return lldb::eTypeClassMemberPointer;
4306 case clang::Type::Complex:
4307 if (qual_type->isComplexType())
4308 return lldb::eTypeClassComplexFloat;
4309 else
4310 return lldb::eTypeClassComplexInteger;
4311 case clang::Type::ObjCObject:
4312 return lldb::eTypeClassObjCObject;
4313 case clang::Type::ObjCInterface:
4314 return lldb::eTypeClassObjCInterface;
4315 case clang::Type::Record: {
4316 const clang::RecordType *record_type =
4317 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4318 const clang::RecordDecl *record_decl = record_type->getDecl();
4319 if (record_decl->isUnion())
4320 return lldb::eTypeClassUnion;
4321 else if (record_decl->isStruct())
4322 return lldb::eTypeClassStruct;
4323 else
4324 return lldb::eTypeClassClass;
4325 } break;
4326 case clang::Type::Enum:
4327 return lldb::eTypeClassEnumeration;
4328 case clang::Type::Typedef:
4329 return lldb::eTypeClassTypedef;
4330 case clang::Type::UnresolvedUsing:
4331 break;
4332 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00004333 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
4334 ->desugar()
4335 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004336 .GetTypeClass();
4337 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00004338 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
4339 ->getDeducedType()
4340 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004341 .GetTypeClass();
4342 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00004343 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
4344 ->getNamedType()
4345 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004346 .GetTypeClass();
4347
4348 case clang::Type::Attributed:
4349 break;
4350 case clang::Type::TemplateTypeParm:
4351 break;
4352 case clang::Type::SubstTemplateTypeParm:
4353 break;
4354 case clang::Type::SubstTemplateTypeParmPack:
4355 break;
4356 case clang::Type::InjectedClassName:
4357 break;
4358 case clang::Type::DependentName:
4359 break;
4360 case clang::Type::DependentTemplateSpecialization:
4361 break;
4362 case clang::Type::PackExpansion:
4363 break;
4364
4365 case clang::Type::TypeOfExpr:
Alex Langfordbddab072019-08-13 19:40:36 +00004366 return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type)
4367 ->getUnderlyingExpr()
4368 ->getType()
4369 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004370 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004371 case clang::Type::TypeOf:
Alex Langfordbddab072019-08-13 19:40:36 +00004372 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
4373 ->getUnderlyingType()
4374 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004375 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004376 case clang::Type::Decltype:
Alex Langfordbddab072019-08-13 19:40:36 +00004377 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
4378 ->getUnderlyingType()
4379 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004380 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004381 case clang::Type::TemplateSpecialization:
4382 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004383 case clang::Type::DeducedTemplateSpecialization:
4384 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004385 case clang::Type::Atomic:
4386 break;
4387 case clang::Type::Pipe:
4388 break;
4389
4390 // pointer type decayed from an array or function type.
4391 case clang::Type::Decayed:
4392 break;
4393 case clang::Type::Adjusted:
4394 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004395 case clang::Type::ObjCTypeParam:
4396 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00004397
4398 case clang::Type::DependentAddressSpace:
4399 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00004400 case clang::Type::MacroQualified:
4401 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004402 }
4403 // We don't know hot to display this type...
4404 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004405}
4406
Kate Stoneb9c1b512016-09-06 20:57:50 +00004407unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4408 if (type)
4409 return GetQualType(type).getQualifiers().getCVRQualifiers();
4410 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004411}
4412
Greg Claytond8d4a572015-08-11 21:38:15 +00004413// Creating related types
Greg Claytond8d4a572015-08-11 21:38:15 +00004414
Greg Claytona1e5dc82015-08-11 22:53:00 +00004415CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004416ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4417 uint64_t *stride) {
4418 if (type) {
4419 clang::QualType qual_type(GetCanonicalQualType(type));
4420
4421 const clang::Type *array_eletype =
4422 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4423
4424 if (!array_eletype)
4425 return CompilerType();
4426
Alex Langfordbddab072019-08-13 19:40:36 +00004427 CompilerType element_type(
4428 this, array_eletype->getCanonicalTypeUnqualified().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004429
4430 // TODO: the real stride will be >= this value.. find the real one!
4431 if (stride)
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00004432 if (Optional<uint64_t> size = element_type.GetByteSize(nullptr))
Adrian Prantld963a7c2019-01-15 18:07:52 +00004433 *stride = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004434
4435 return element_type;
4436 }
4437 return CompilerType();
4438}
4439
4440CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4441 uint64_t size) {
4442 if (type) {
4443 clang::QualType qual_type(GetCanonicalQualType(type));
4444 if (clang::ASTContext *ast_ctx = getASTContext()) {
4445 if (size != 0)
4446 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004447 this, ast_ctx
4448 ->getConstantArrayType(
Richard Smith772e2662019-10-04 01:25:59 +00004449 qual_type, llvm::APInt(64, size), nullptr,
Alex Langfordbddab072019-08-13 19:40:36 +00004450 clang::ArrayType::ArraySizeModifier::Normal, 0)
4451 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004452 else
4453 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004454 this,
4455 ast_ctx
4456 ->getIncompleteArrayType(
4457 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0)
4458 .getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00004459 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004460 }
4461
4462 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004463}
4464
Greg Claytona1e5dc82015-08-11 22:53:00 +00004465CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004466ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4467 if (type)
Alex Langfordbddab072019-08-13 19:40:36 +00004468 return CompilerType(this, GetCanonicalQualType(type).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004469 return CompilerType();
4470}
4471
4472static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4473 clang::QualType qual_type) {
4474 if (qual_type->isPointerType())
4475 qual_type = ast->getPointerType(
4476 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4477 else
4478 qual_type = qual_type.getUnqualifiedType();
4479 qual_type.removeLocalConst();
4480 qual_type.removeLocalRestrict();
4481 qual_type.removeLocalVolatile();
4482 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004483}
4484
4485CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004486ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4487 if (type)
4488 return CompilerType(
Alex Langfordbddab072019-08-13 19:40:36 +00004489 this,
4490 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004491 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004492}
4493
Kate Stoneb9c1b512016-09-06 20:57:50 +00004494int ClangASTContext::GetFunctionArgumentCount(
4495 lldb::opaque_compiler_type_t type) {
4496 if (type) {
4497 const clang::FunctionProtoType *func =
4498 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4499 if (func)
4500 return func->getNumParams();
4501 }
4502 return -1;
4503}
4504
4505CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4506 lldb::opaque_compiler_type_t type, size_t idx) {
4507 if (type) {
4508 const clang::FunctionProtoType *func =
4509 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4510 if (func) {
4511 const uint32_t num_args = func->getNumParams();
4512 if (idx < num_args)
Alex Langfordbddab072019-08-13 19:40:36 +00004513 return CompilerType(this, func->getParamType(idx).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004514 }
4515 }
4516 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004517}
4518
Greg Claytona1e5dc82015-08-11 22:53:00 +00004519CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004520ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4521 if (type) {
4522 clang::QualType qual_type(GetQualType(type));
4523 const clang::FunctionProtoType *func =
4524 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4525 if (func)
Alex Langfordbddab072019-08-13 19:40:36 +00004526 return CompilerType(this, func->getReturnType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004527 }
4528 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004529}
4530
4531size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004532ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4533 size_t num_functions = 0;
4534 if (type) {
4535 clang::QualType qual_type(GetCanonicalQualType(type));
4536 switch (qual_type->getTypeClass()) {
4537 case clang::Type::Record:
4538 if (GetCompleteQualType(getASTContext(), qual_type)) {
4539 const clang::RecordType *record_type =
4540 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4541 const clang::RecordDecl *record_decl = record_type->getDecl();
4542 assert(record_decl);
4543 const clang::CXXRecordDecl *cxx_record_decl =
4544 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4545 if (cxx_record_decl)
4546 num_functions = std::distance(cxx_record_decl->method_begin(),
4547 cxx_record_decl->method_end());
4548 }
4549 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004550
Sean Callananf9c622a2016-09-30 18:44:43 +00004551 case clang::Type::ObjCObjectPointer: {
4552 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004553 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004554 const clang::ObjCInterfaceType *objc_interface_type =
4555 objc_class_type->getInterfaceType();
4556 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004557 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4558 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004559 clang::ObjCInterfaceDecl *class_interface_decl =
4560 objc_interface_type->getDecl();
4561 if (class_interface_decl) {
4562 num_functions = std::distance(class_interface_decl->meth_begin(),
4563 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004564 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004565 }
4566 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004567 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004568
4569 case clang::Type::ObjCObject:
4570 case clang::Type::ObjCInterface:
4571 if (GetCompleteType(type)) {
4572 const clang::ObjCObjectType *objc_class_type =
4573 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4574 if (objc_class_type) {
4575 clang::ObjCInterfaceDecl *class_interface_decl =
4576 objc_class_type->getInterface();
4577 if (class_interface_decl)
4578 num_functions = std::distance(class_interface_decl->meth_begin(),
4579 class_interface_decl->meth_end());
4580 }
4581 }
4582 break;
4583
4584 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00004585 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
4586 ->getDecl()
4587 ->getUnderlyingType()
4588 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004589 .GetNumMemberFunctions();
4590
4591 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00004592 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
4593 ->getDeducedType()
4594 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004595 .GetNumMemberFunctions();
4596
4597 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00004598 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
4599 ->getNamedType()
4600 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004601 .GetNumMemberFunctions();
4602
4603 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00004604 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
4605 ->desugar()
4606 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004607 .GetNumMemberFunctions();
4608
4609 default:
4610 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004611 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004612 }
4613 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004614}
4615
4616TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004617ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4618 size_t idx) {
4619 std::string name;
4620 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4621 CompilerType clang_type;
4622 CompilerDecl clang_decl;
4623 if (type) {
4624 clang::QualType qual_type(GetCanonicalQualType(type));
4625 switch (qual_type->getTypeClass()) {
4626 case clang::Type::Record:
4627 if (GetCompleteQualType(getASTContext(), qual_type)) {
4628 const clang::RecordType *record_type =
4629 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4630 const clang::RecordDecl *record_decl = record_type->getDecl();
4631 assert(record_decl);
4632 const clang::CXXRecordDecl *cxx_record_decl =
4633 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4634 if (cxx_record_decl) {
4635 auto method_iter = cxx_record_decl->method_begin();
4636 auto method_end = cxx_record_decl->method_end();
4637 if (idx <
4638 static_cast<size_t>(std::distance(method_iter, method_end))) {
4639 std::advance(method_iter, idx);
4640 clang::CXXMethodDecl *cxx_method_decl =
4641 method_iter->getCanonicalDecl();
4642 if (cxx_method_decl) {
4643 name = cxx_method_decl->getDeclName().getAsString();
4644 if (cxx_method_decl->isStatic())
4645 kind = lldb::eMemberFunctionKindStaticMethod;
4646 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4647 kind = lldb::eMemberFunctionKindConstructor;
4648 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4649 kind = lldb::eMemberFunctionKindDestructor;
4650 else
4651 kind = lldb::eMemberFunctionKindInstanceMethod;
4652 clang_type = CompilerType(
4653 this, cxx_method_decl->getType().getAsOpaquePtr());
4654 clang_decl = CompilerDecl(this, cxx_method_decl);
4655 }
4656 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004657 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004658 }
4659 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004660
Sean Callananf9c622a2016-09-30 18:44:43 +00004661 case clang::Type::ObjCObjectPointer: {
4662 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004663 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004664 const clang::ObjCInterfaceType *objc_interface_type =
4665 objc_class_type->getInterfaceType();
4666 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004667 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4668 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004669 clang::ObjCInterfaceDecl *class_interface_decl =
4670 objc_interface_type->getDecl();
4671 if (class_interface_decl) {
4672 auto method_iter = class_interface_decl->meth_begin();
4673 auto method_end = class_interface_decl->meth_end();
4674 if (idx <
4675 static_cast<size_t>(std::distance(method_iter, method_end))) {
4676 std::advance(method_iter, idx);
4677 clang::ObjCMethodDecl *objc_method_decl =
4678 method_iter->getCanonicalDecl();
4679 if (objc_method_decl) {
4680 clang_decl = CompilerDecl(this, objc_method_decl);
4681 name = objc_method_decl->getSelector().getAsString();
4682 if (objc_method_decl->isClassMethod())
4683 kind = lldb::eMemberFunctionKindStaticMethod;
4684 else
4685 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004686 }
4687 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004688 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004689 }
4690 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004691 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004692
Kate Stoneb9c1b512016-09-06 20:57:50 +00004693 case clang::Type::ObjCObject:
4694 case clang::Type::ObjCInterface:
4695 if (GetCompleteType(type)) {
4696 const clang::ObjCObjectType *objc_class_type =
4697 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4698 if (objc_class_type) {
4699 clang::ObjCInterfaceDecl *class_interface_decl =
4700 objc_class_type->getInterface();
4701 if (class_interface_decl) {
4702 auto method_iter = class_interface_decl->meth_begin();
4703 auto method_end = class_interface_decl->meth_end();
4704 if (idx <
4705 static_cast<size_t>(std::distance(method_iter, method_end))) {
4706 std::advance(method_iter, idx);
4707 clang::ObjCMethodDecl *objc_method_decl =
4708 method_iter->getCanonicalDecl();
4709 if (objc_method_decl) {
4710 clang_decl = CompilerDecl(this, objc_method_decl);
4711 name = objc_method_decl->getSelector().getAsString();
4712 if (objc_method_decl->isClassMethod())
4713 kind = lldb::eMemberFunctionKindStaticMethod;
4714 else
4715 kind = lldb::eMemberFunctionKindInstanceMethod;
4716 }
4717 }
4718 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004719 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004720 }
4721 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004722
Kate Stoneb9c1b512016-09-06 20:57:50 +00004723 case clang::Type::Typedef:
4724 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4725 ->getDecl()
4726 ->getUnderlyingType()
4727 .getAsOpaquePtr(),
4728 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004729
Kate Stoneb9c1b512016-09-06 20:57:50 +00004730 case clang::Type::Auto:
4731 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4732 ->getDeducedType()
4733 .getAsOpaquePtr(),
4734 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004735
Kate Stoneb9c1b512016-09-06 20:57:50 +00004736 case clang::Type::Elaborated:
4737 return GetMemberFunctionAtIndex(
4738 llvm::cast<clang::ElaboratedType>(qual_type)
4739 ->getNamedType()
4740 .getAsOpaquePtr(),
4741 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004742
Kate Stoneb9c1b512016-09-06 20:57:50 +00004743 case clang::Type::Paren:
4744 return GetMemberFunctionAtIndex(
4745 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4746 idx);
4747
4748 default:
4749 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004750 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004751 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004752
Kate Stoneb9c1b512016-09-06 20:57:50 +00004753 if (kind == eMemberFunctionKindUnknown)
4754 return TypeMemberFunctionImpl();
4755 else
4756 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004757}
4758
Greg Claytona1e5dc82015-08-11 22:53:00 +00004759CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004760ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4761 if (type)
Alex Langfordbddab072019-08-13 19:40:36 +00004762 return CompilerType(
4763 this, GetQualType(type).getNonReferenceType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004764 return CompilerType();
4765}
4766
4767CompilerType ClangASTContext::CreateTypedefType(
4768 const CompilerType &type, const char *typedef_name,
4769 const CompilerDeclContext &compiler_decl_ctx) {
4770 if (type && typedef_name && typedef_name[0]) {
4771 ClangASTContext *ast =
4772 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4773 if (!ast)
4774 return CompilerType();
4775 clang::ASTContext *clang_ast = ast->getASTContext();
4776 clang::QualType qual_type(ClangUtil::GetQualType(type));
4777
4778 clang::DeclContext *decl_ctx =
4779 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4780 if (decl_ctx == nullptr)
4781 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4782
4783 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4784 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4785 &clang_ast->Idents.get(typedef_name),
4786 clang_ast->getTrivialTypeSourceInfo(qual_type));
4787
4788 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4789
Aleksandr Urakov709426b2018-09-10 08:08:43 +00004790 decl_ctx->addDecl(decl);
4791
Kate Stoneb9c1b512016-09-06 20:57:50 +00004792 // Get a uniqued clang::QualType for the typedef decl type
Alex Langfordbddab072019-08-13 19:40:36 +00004793 return CompilerType(ast, clang_ast->getTypedefType(decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004794 }
4795 return CompilerType();
4796}
4797
4798CompilerType
4799ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4800 if (type) {
4801 clang::QualType qual_type(GetQualType(type));
Alex Langfordbddab072019-08-13 19:40:36 +00004802 return CompilerType(
4803 this, qual_type.getTypePtr()->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004804 }
4805 return CompilerType();
4806}
4807
4808CompilerType
4809ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4810 if (type) {
4811 clang::QualType qual_type(GetQualType(type));
4812
4813 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4814 switch (type_class) {
4815 case clang::Type::ObjCObject:
4816 case clang::Type::ObjCInterface:
Alex Langfordbddab072019-08-13 19:40:36 +00004817 return CompilerType(this, getASTContext()
4818 ->getObjCObjectPointerType(qual_type)
4819 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004820
4821 default:
Alex Langfordbddab072019-08-13 19:40:36 +00004822 return CompilerType(
4823 this, getASTContext()->getPointerType(qual_type).getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00004824 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004825 }
4826 return CompilerType();
4827}
4828
4829CompilerType
4830ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4831 if (type)
4832 return CompilerType(this, getASTContext()
4833 ->getLValueReferenceType(GetQualType(type))
4834 .getAsOpaquePtr());
4835 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004836 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004837}
4838
Kate Stoneb9c1b512016-09-06 20:57:50 +00004839CompilerType
4840ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4841 if (type)
4842 return CompilerType(this, getASTContext()
4843 ->getRValueReferenceType(GetQualType(type))
4844 .getAsOpaquePtr());
4845 else
4846 return CompilerType();
4847}
4848
4849CompilerType
4850ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4851 if (type) {
4852 clang::QualType result(GetQualType(type));
4853 result.addConst();
4854 return CompilerType(this, result.getAsOpaquePtr());
4855 }
4856 return CompilerType();
4857}
4858
4859CompilerType
4860ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4861 if (type) {
4862 clang::QualType result(GetQualType(type));
4863 result.addVolatile();
4864 return CompilerType(this, result.getAsOpaquePtr());
4865 }
4866 return CompilerType();
4867}
4868
4869CompilerType
4870ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4871 if (type) {
4872 clang::QualType result(GetQualType(type));
4873 result.addRestrict();
4874 return CompilerType(this, result.getAsOpaquePtr());
4875 }
4876 return CompilerType();
4877}
4878
4879CompilerType
4880ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4881 const char *typedef_name,
4882 const CompilerDeclContext &compiler_decl_ctx) {
4883 if (type) {
4884 clang::ASTContext *clang_ast = getASTContext();
4885 clang::QualType qual_type(GetQualType(type));
4886
4887 clang::DeclContext *decl_ctx =
4888 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4889 if (decl_ctx == nullptr)
4890 decl_ctx = getASTContext()->getTranslationUnitDecl();
4891
4892 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4893 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4894 &clang_ast->Idents.get(typedef_name),
4895 clang_ast->getTrivialTypeSourceInfo(qual_type));
4896
4897 clang::TagDecl *tdecl = nullptr;
4898 if (!qual_type.isNull()) {
4899 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4900 tdecl = rt->getDecl();
4901 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4902 tdecl = et->getDecl();
4903 }
4904
4905 // Check whether this declaration is an anonymous struct, union, or enum,
Adrian Prantl05097242018-04-30 16:49:04 +00004906 // hidden behind a typedef. If so, we try to check whether we have a
4907 // typedef tag to attach to the original record declaration
Kate Stoneb9c1b512016-09-06 20:57:50 +00004908 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4909 tdecl->setTypedefNameForAnonDecl(decl);
4910
4911 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4912
4913 // Get a uniqued clang::QualType for the typedef decl type
4914 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4915 }
4916 return CompilerType();
4917}
4918
4919CompilerType
4920ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4921 if (type) {
4922 const clang::TypedefType *typedef_type =
4923 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4924 if (typedef_type)
Alex Langfordbddab072019-08-13 19:40:36 +00004925 return CompilerType(
4926 this, typedef_type->getDecl()->getUnderlyingType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004927 }
4928 return CompilerType();
4929}
Greg Claytond8d4a572015-08-11 21:38:15 +00004930
Greg Claytond8d4a572015-08-11 21:38:15 +00004931// Create related types using the current type's AST
Greg Claytond8d4a572015-08-11 21:38:15 +00004932
Kate Stoneb9c1b512016-09-06 20:57:50 +00004933CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4934 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004935}
Greg Claytond8d4a572015-08-11 21:38:15 +00004936// Exploring the type
Greg Claytond8d4a572015-08-11 21:38:15 +00004937
Alex Langfordb482db62019-09-06 21:05:21 +00004938const llvm::fltSemantics &
4939ClangASTContext::GetFloatTypeSemantics(size_t byte_size) {
4940 if (auto *ast = getASTContext()) {
4941 const size_t bit_size = byte_size * 8;
4942 if (bit_size == ast->getTypeSize(ast->FloatTy))
4943 return ast->getFloatTypeSemantics(ast->FloatTy);
4944 else if (bit_size == ast->getTypeSize(ast->DoubleTy))
4945 return ast->getFloatTypeSemantics(ast->DoubleTy);
4946 else if (bit_size == ast->getTypeSize(ast->LongDoubleTy))
4947 return ast->getFloatTypeSemantics(ast->LongDoubleTy);
4948 else if (bit_size == ast->getTypeSize(ast->HalfTy))
4949 return ast->getFloatTypeSemantics(ast->HalfTy);
4950 }
4951 return llvm::APFloatBase::Bogus();
4952}
4953
Adrian Prantl2ee7b882019-01-16 21:19:20 +00004954Optional<uint64_t>
4955ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4956 ExecutionContextScope *exe_scope) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00004957 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00004958 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004959 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004960 switch (type_class) {
4961 case clang::Type::Record:
4962 if (GetCompleteType(type))
4963 return getASTContext()->getTypeSize(qual_type);
4964 else
Adrian Prantl2ee7b882019-01-16 21:19:20 +00004965 return None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004966 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004967
Kate Stoneb9c1b512016-09-06 20:57:50 +00004968 case clang::Type::ObjCInterface:
4969 case clang::Type::ObjCObject: {
4970 ExecutionContext exe_ctx(exe_scope);
4971 Process *process = exe_ctx.GetProcessPtr();
4972 if (process) {
Alex Langforde823bbe2019-06-10 20:53:23 +00004973 ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004974 if (objc_runtime) {
4975 uint64_t bit_size = 0;
4976 if (objc_runtime->GetTypeBitSize(
Alex Langfordbddab072019-08-13 19:40:36 +00004977 CompilerType(this, qual_type.getAsOpaquePtr()), bit_size))
Kate Stoneb9c1b512016-09-06 20:57:50 +00004978 return bit_size;
4979 }
4980 } else {
4981 static bool g_printed = false;
4982 if (!g_printed) {
4983 StreamString s;
4984 DumpTypeDescription(type, &s);
4985
4986 llvm::outs() << "warning: trying to determine the size of type ";
4987 llvm::outs() << s.GetString() << "\n";
4988 llvm::outs() << "without a valid ExecutionContext. this is not "
4989 "reliable. please file a bug against LLDB.\n";
4990 llvm::outs() << "backtrace:\n";
4991 llvm::sys::PrintStackTrace(llvm::outs());
4992 llvm::outs() << "\n";
4993 g_printed = true;
4994 }
4995 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004996 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004997 LLVM_FALLTHROUGH;
4998 default:
4999 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
5000 if (bit_size == 0) {
5001 if (qual_type->isIncompleteArrayType())
5002 return getASTContext()->getTypeSize(
5003 qual_type->getArrayElementTypeNoTypeQual()
5004 ->getCanonicalTypeUnqualified());
5005 }
5006 if (qual_type->isObjCObjectOrInterfaceType())
5007 return bit_size +
5008 getASTContext()->getTypeSize(
5009 getASTContext()->ObjCBuiltinClassTy);
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005010 // Function types actually have a size of 0, that's not an error.
5011 if (qual_type->isFunctionProtoType())
5012 return bit_size;
5013 if (bit_size)
5014 return bit_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005015 }
5016 }
Adrian Prantl2ee7b882019-01-16 21:19:20 +00005017 return None;
Greg Claytond8d4a572015-08-11 21:38:15 +00005018}
5019
Davide Italiano36f13e42019-08-12 20:03:19 +00005020llvm::Optional<size_t>
Davide Italiano7f9bbe02019-08-12 21:49:54 +00005021ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type,
5022 ExecutionContextScope *exe_scope) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005023 if (GetCompleteType(type))
5024 return getASTContext()->getTypeAlign(GetQualType(type));
Davide Italiano36f13e42019-08-12 20:03:19 +00005025 return {};
Kate Stoneb9c1b512016-09-06 20:57:50 +00005026}
5027
5028lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
5029 uint64_t &count) {
5030 if (!type)
5031 return lldb::eEncodingInvalid;
5032
5033 count = 1;
5034 clang::QualType qual_type(GetCanonicalQualType(type));
5035
5036 switch (qual_type->getTypeClass()) {
5037 case clang::Type::UnaryTransform:
5038 break;
5039
5040 case clang::Type::FunctionNoProto:
5041 case clang::Type::FunctionProto:
5042 break;
5043
5044 case clang::Type::IncompleteArray:
5045 case clang::Type::VariableArray:
5046 break;
5047
5048 case clang::Type::ConstantArray:
5049 break;
5050
Fangrui Song8f284882018-07-13 22:40:40 +00005051 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005052 case clang::Type::ExtVector:
5053 case clang::Type::Vector:
5054 // TODO: Set this to more than one???
5055 break;
5056
5057 case clang::Type::Builtin:
5058 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5059 case clang::BuiltinType::Void:
5060 break;
5061
5062 case clang::BuiltinType::Bool:
5063 case clang::BuiltinType::Char_S:
5064 case clang::BuiltinType::SChar:
5065 case clang::BuiltinType::WChar_S:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005066 case clang::BuiltinType::Short:
5067 case clang::BuiltinType::Int:
5068 case clang::BuiltinType::Long:
5069 case clang::BuiltinType::LongLong:
5070 case clang::BuiltinType::Int128:
5071 return lldb::eEncodingSint;
5072
5073 case clang::BuiltinType::Char_U:
5074 case clang::BuiltinType::UChar:
5075 case clang::BuiltinType::WChar_U:
Richard Smith51d12d82018-05-02 02:43:22 +00005076 case clang::BuiltinType::Char8:
5077 case clang::BuiltinType::Char16:
5078 case clang::BuiltinType::Char32:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005079 case clang::BuiltinType::UShort:
5080 case clang::BuiltinType::UInt:
5081 case clang::BuiltinType::ULong:
5082 case clang::BuiltinType::ULongLong:
5083 case clang::BuiltinType::UInt128:
5084 return lldb::eEncodingUint;
5085
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005086 // Fixed point types. Note that they are currently ignored.
5087 case clang::BuiltinType::ShortAccum:
5088 case clang::BuiltinType::Accum:
5089 case clang::BuiltinType::LongAccum:
5090 case clang::BuiltinType::UShortAccum:
5091 case clang::BuiltinType::UAccum:
5092 case clang::BuiltinType::ULongAccum:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005093 case clang::BuiltinType::ShortFract:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005094 case clang::BuiltinType::Fract:
5095 case clang::BuiltinType::LongFract:
5096 case clang::BuiltinType::UShortFract:
5097 case clang::BuiltinType::UFract:
5098 case clang::BuiltinType::ULongFract:
5099 case clang::BuiltinType::SatShortAccum:
5100 case clang::BuiltinType::SatAccum:
5101 case clang::BuiltinType::SatLongAccum:
5102 case clang::BuiltinType::SatUShortAccum:
5103 case clang::BuiltinType::SatUAccum:
5104 case clang::BuiltinType::SatULongAccum:
5105 case clang::BuiltinType::SatShortFract:
5106 case clang::BuiltinType::SatFract:
5107 case clang::BuiltinType::SatLongFract:
5108 case clang::BuiltinType::SatUShortFract:
5109 case clang::BuiltinType::SatUFract:
5110 case clang::BuiltinType::SatULongFract:
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005111 break;
5112
Kate Stoneb9c1b512016-09-06 20:57:50 +00005113 case clang::BuiltinType::Half:
5114 case clang::BuiltinType::Float:
Ted Woodward4355c7c2017-09-20 19:16:53 +00005115 case clang::BuiltinType::Float16:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005116 case clang::BuiltinType::Float128:
5117 case clang::BuiltinType::Double:
5118 case clang::BuiltinType::LongDouble:
5119 return lldb::eEncodingIEEE754;
5120
5121 case clang::BuiltinType::ObjCClass:
5122 case clang::BuiltinType::ObjCId:
5123 case clang::BuiltinType::ObjCSel:
5124 return lldb::eEncodingUint;
5125
5126 case clang::BuiltinType::NullPtr:
5127 return lldb::eEncodingUint;
5128
5129 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5130 case clang::BuiltinType::Kind::BoundMember:
5131 case clang::BuiltinType::Kind::BuiltinFn:
5132 case clang::BuiltinType::Kind::Dependent:
5133 case clang::BuiltinType::Kind::OCLClkEvent:
5134 case clang::BuiltinType::Kind::OCLEvent:
5135 case clang::BuiltinType::Kind::OCLImage1dRO:
5136 case clang::BuiltinType::Kind::OCLImage1dWO:
5137 case clang::BuiltinType::Kind::OCLImage1dRW:
5138 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5139 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5140 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5141 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5142 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5143 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5144 case clang::BuiltinType::Kind::OCLImage2dRO:
5145 case clang::BuiltinType::Kind::OCLImage2dWO:
5146 case clang::BuiltinType::Kind::OCLImage2dRW:
5147 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5148 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5149 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5150 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5151 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5152 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5153 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5154 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5155 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5156 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5157 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5158 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5159 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5160 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5161 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5162 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5163 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5164 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5165 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5166 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5167 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5168 case clang::BuiltinType::Kind::OCLImage3dRO:
5169 case clang::BuiltinType::Kind::OCLImage3dWO:
5170 case clang::BuiltinType::Kind::OCLImage3dRW:
5171 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005172 case clang::BuiltinType::Kind::OCLReserveID:
5173 case clang::BuiltinType::Kind::OCLSampler:
5174 case clang::BuiltinType::Kind::OMPArraySection:
5175 case clang::BuiltinType::Kind::Overload:
5176 case clang::BuiltinType::Kind::PseudoObject:
5177 case clang::BuiltinType::Kind::UnknownAny:
5178 break;
Jorge Gorbe Moyaa6e6c182018-11-08 22:04:58 +00005179
5180 case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload:
5181 case clang::BuiltinType::OCLIntelSubgroupAVCImePayload:
5182 case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload:
5183 case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload:
5184 case clang::BuiltinType::OCLIntelSubgroupAVCMceResult:
5185 case clang::BuiltinType::OCLIntelSubgroupAVCImeResult:
5186 case clang::BuiltinType::OCLIntelSubgroupAVCRefResult:
5187 case clang::BuiltinType::OCLIntelSubgroupAVCSicResult:
5188 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleRefStreamout:
5189 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualRefStreamout:
5190 case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleRefStreamin:
5191 case clang::BuiltinType::OCLIntelSubgroupAVCImeDualRefStreamin:
5192 break;
Raphael Isemann339b5d12019-08-09 09:58:47 +00005193
5194 case clang::BuiltinType::SveBool:
5195 case clang::BuiltinType::SveInt8:
5196 case clang::BuiltinType::SveInt16:
5197 case clang::BuiltinType::SveInt32:
5198 case clang::BuiltinType::SveInt64:
5199 case clang::BuiltinType::SveUint8:
5200 case clang::BuiltinType::SveUint16:
5201 case clang::BuiltinType::SveUint32:
5202 case clang::BuiltinType::SveUint64:
5203 case clang::BuiltinType::SveFloat16:
5204 case clang::BuiltinType::SveFloat32:
5205 case clang::BuiltinType::SveFloat64:
5206 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005207 }
5208 break;
Adrian Prantl05097242018-04-30 16:49:04 +00005209 // All pointer types are represented as unsigned integer encodings. We may
5210 // nee to add a eEncodingPointer if we ever need to know the difference
Kate Stoneb9c1b512016-09-06 20:57:50 +00005211 case clang::Type::ObjCObjectPointer:
5212 case clang::Type::BlockPointer:
5213 case clang::Type::Pointer:
5214 case clang::Type::LValueReference:
5215 case clang::Type::RValueReference:
5216 case clang::Type::MemberPointer:
5217 return lldb::eEncodingUint;
5218 case clang::Type::Complex: {
5219 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5220 if (qual_type->isComplexType())
5221 encoding = lldb::eEncodingIEEE754;
5222 else {
5223 const clang::ComplexType *complex_type =
5224 qual_type->getAsComplexIntegerType();
5225 if (complex_type)
Alex Langfordbddab072019-08-13 19:40:36 +00005226 encoding =
5227 CompilerType(this, complex_type->getElementType().getAsOpaquePtr())
5228 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005229 else
5230 encoding = lldb::eEncodingSint;
5231 }
5232 count = 2;
5233 return encoding;
5234 }
5235
5236 case clang::Type::ObjCInterface:
5237 break;
5238 case clang::Type::Record:
5239 break;
5240 case clang::Type::Enum:
5241 return lldb::eEncodingSint;
5242 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00005243 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
5244 ->getDecl()
5245 ->getUnderlyingType()
5246 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005247 .GetEncoding(count);
5248
5249 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00005250 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
5251 ->getDeducedType()
5252 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005253 .GetEncoding(count);
5254
5255 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00005256 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
5257 ->getNamedType()
5258 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005259 .GetEncoding(count);
5260
5261 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00005262 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
5263 ->desugar()
5264 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005265 .GetEncoding(count);
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005266 case clang::Type::TypeOfExpr:
Alex Langfordbddab072019-08-13 19:40:36 +00005267 return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type)
5268 ->getUnderlyingExpr()
5269 ->getType()
5270 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005271 .GetEncoding(count);
5272 case clang::Type::TypeOf:
Alex Langfordbddab072019-08-13 19:40:36 +00005273 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
5274 ->getUnderlyingType()
5275 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005276 .GetEncoding(count);
5277 case clang::Type::Decltype:
Alex Langfordbddab072019-08-13 19:40:36 +00005278 return CompilerType(this, llvm::cast<clang::DecltypeType>(qual_type)
5279 ->getUnderlyingType()
5280 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005281 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005282 case clang::Type::DependentSizedArray:
5283 case clang::Type::DependentSizedExtVector:
5284 case clang::Type::UnresolvedUsing:
5285 case clang::Type::Attributed:
5286 case clang::Type::TemplateTypeParm:
5287 case clang::Type::SubstTemplateTypeParm:
5288 case clang::Type::SubstTemplateTypeParmPack:
5289 case clang::Type::InjectedClassName:
5290 case clang::Type::DependentName:
5291 case clang::Type::DependentTemplateSpecialization:
5292 case clang::Type::PackExpansion:
5293 case clang::Type::ObjCObject:
5294
Kate Stoneb9c1b512016-09-06 20:57:50 +00005295 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005296 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005297 case clang::Type::Atomic:
5298 case clang::Type::Adjusted:
5299 case clang::Type::Pipe:
5300 break;
5301
5302 // pointer type decayed from an array or function type.
5303 case clang::Type::Decayed:
5304 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005305 case clang::Type::ObjCTypeParam:
5306 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005307
5308 case clang::Type::DependentAddressSpace:
5309 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00005310 case clang::Type::MacroQualified:
5311 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005312 }
5313 count = 0;
5314 return lldb::eEncodingInvalid;
5315}
5316
5317lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5318 if (!type)
5319 return lldb::eFormatDefault;
5320
5321 clang::QualType qual_type(GetCanonicalQualType(type));
5322
5323 switch (qual_type->getTypeClass()) {
5324 case clang::Type::UnaryTransform:
5325 break;
5326
5327 case clang::Type::FunctionNoProto:
5328 case clang::Type::FunctionProto:
5329 break;
5330
5331 case clang::Type::IncompleteArray:
5332 case clang::Type::VariableArray:
5333 break;
5334
5335 case clang::Type::ConstantArray:
5336 return lldb::eFormatVoid; // no value
5337
Fangrui Song8f284882018-07-13 22:40:40 +00005338 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005339 case clang::Type::ExtVector:
5340 case clang::Type::Vector:
5341 break;
5342
5343 case clang::Type::Builtin:
5344 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5345 // default: assert(0 && "Unknown builtin type!");
5346 case clang::BuiltinType::UnknownAny:
5347 case clang::BuiltinType::Void:
5348 case clang::BuiltinType::BoundMember:
5349 break;
5350
5351 case clang::BuiltinType::Bool:
5352 return lldb::eFormatBoolean;
5353 case clang::BuiltinType::Char_S:
5354 case clang::BuiltinType::SChar:
5355 case clang::BuiltinType::WChar_S:
5356 case clang::BuiltinType::Char_U:
5357 case clang::BuiltinType::UChar:
5358 case clang::BuiltinType::WChar_U:
5359 return lldb::eFormatChar;
5360 case clang::BuiltinType::Char16:
5361 return lldb::eFormatUnicode16;
5362 case clang::BuiltinType::Char32:
5363 return lldb::eFormatUnicode32;
5364 case clang::BuiltinType::UShort:
5365 return lldb::eFormatUnsigned;
5366 case clang::BuiltinType::Short:
5367 return lldb::eFormatDecimal;
5368 case clang::BuiltinType::UInt:
5369 return lldb::eFormatUnsigned;
5370 case clang::BuiltinType::Int:
5371 return lldb::eFormatDecimal;
5372 case clang::BuiltinType::ULong:
5373 return lldb::eFormatUnsigned;
5374 case clang::BuiltinType::Long:
5375 return lldb::eFormatDecimal;
5376 case clang::BuiltinType::ULongLong:
5377 return lldb::eFormatUnsigned;
5378 case clang::BuiltinType::LongLong:
5379 return lldb::eFormatDecimal;
5380 case clang::BuiltinType::UInt128:
5381 return lldb::eFormatUnsigned;
5382 case clang::BuiltinType::Int128:
5383 return lldb::eFormatDecimal;
5384 case clang::BuiltinType::Half:
5385 case clang::BuiltinType::Float:
5386 case clang::BuiltinType::Double:
5387 case clang::BuiltinType::LongDouble:
5388 return lldb::eFormatFloat;
5389 default:
5390 return lldb::eFormatHex;
5391 }
5392 break;
5393 case clang::Type::ObjCObjectPointer:
5394 return lldb::eFormatHex;
5395 case clang::Type::BlockPointer:
5396 return lldb::eFormatHex;
5397 case clang::Type::Pointer:
5398 return lldb::eFormatHex;
5399 case clang::Type::LValueReference:
5400 case clang::Type::RValueReference:
5401 return lldb::eFormatHex;
5402 case clang::Type::MemberPointer:
5403 break;
5404 case clang::Type::Complex: {
5405 if (qual_type->isComplexType())
5406 return lldb::eFormatComplex;
5407 else
5408 return lldb::eFormatComplexInteger;
5409 }
5410 case clang::Type::ObjCInterface:
5411 break;
5412 case clang::Type::Record:
5413 break;
5414 case clang::Type::Enum:
5415 return lldb::eFormatEnum;
5416 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00005417 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
5418 ->getDecl()
5419 ->getUnderlyingType()
5420 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005421 .GetFormat();
5422 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00005423 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
5424 ->desugar()
5425 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005426 .GetFormat();
5427 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00005428 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
5429 ->desugar()
5430 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005431 .GetFormat();
5432 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00005433 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
5434 ->getNamedType()
5435 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005436 .GetFormat();
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005437 case clang::Type::TypeOfExpr:
Alex Langfordbddab072019-08-13 19:40:36 +00005438 return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type)
5439 ->getUnderlyingExpr()
5440 ->getType()
5441 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005442 .GetFormat();
5443 case clang::Type::TypeOf:
Alex Langfordbddab072019-08-13 19:40:36 +00005444 return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type)
5445 ->getUnderlyingType()
5446 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005447 .GetFormat();
5448 case clang::Type::Decltype:
Alex Langfordbddab072019-08-13 19:40:36 +00005449 return CompilerType(this, llvm::cast<clang::DecltypeType>(qual_type)
5450 ->getUnderlyingType()
5451 .getAsOpaquePtr())
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005452 .GetFormat();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005453 case clang::Type::DependentSizedArray:
5454 case clang::Type::DependentSizedExtVector:
5455 case clang::Type::UnresolvedUsing:
5456 case clang::Type::Attributed:
5457 case clang::Type::TemplateTypeParm:
5458 case clang::Type::SubstTemplateTypeParm:
5459 case clang::Type::SubstTemplateTypeParmPack:
5460 case clang::Type::InjectedClassName:
5461 case clang::Type::DependentName:
5462 case clang::Type::DependentTemplateSpecialization:
5463 case clang::Type::PackExpansion:
5464 case clang::Type::ObjCObject:
5465
Kate Stoneb9c1b512016-09-06 20:57:50 +00005466 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005467 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005468 case clang::Type::Atomic:
5469 case clang::Type::Adjusted:
5470 case clang::Type::Pipe:
5471 break;
5472
5473 // pointer type decayed from an array or function type.
5474 case clang::Type::Decayed:
5475 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005476 case clang::Type::ObjCTypeParam:
5477 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005478
5479 case clang::Type::DependentAddressSpace:
5480 break;
Krasimir Georgiev435e76a2019-05-07 13:59:30 +00005481 case clang::Type::MacroQualified:
5482 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005483 }
5484 // We don't know hot to display this type...
5485 return lldb::eFormatBytes;
5486}
5487
5488static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5489 bool check_superclass) {
5490 while (class_interface_decl) {
5491 if (class_interface_decl->ivar_size() > 0)
5492 return true;
5493
5494 if (check_superclass)
5495 class_interface_decl = class_interface_decl->getSuperClass();
5496 else
5497 break;
5498 }
5499 return false;
5500}
5501
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00005502static Optional<SymbolFile::ArrayInfo>
Adrian Prantleca07c52018-11-05 20:49:07 +00005503GetDynamicArrayInfo(ClangASTContext &ast, SymbolFile *sym_file,
5504 clang::QualType qual_type,
5505 const ExecutionContext *exe_ctx) {
5506 if (qual_type->isIncompleteArrayType())
5507 if (auto *metadata = ast.GetMetadata(qual_type.getAsOpaquePtr()))
Pavel Labathffec31e2018-12-28 13:34:44 +00005508 return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(),
5509 exe_ctx);
Adrian Prantleca07c52018-11-05 20:49:07 +00005510 return llvm::None;
5511}
5512
Kate Stoneb9c1b512016-09-06 20:57:50 +00005513uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
Adrian Prantleca07c52018-11-05 20:49:07 +00005514 bool omit_empty_base_classes,
5515 const ExecutionContext *exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005516 if (!type)
5517 return 0;
5518
5519 uint32_t num_children = 0;
5520 clang::QualType qual_type(GetQualType(type));
5521 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5522 switch (type_class) {
5523 case clang::Type::Builtin:
5524 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5525 case clang::BuiltinType::ObjCId: // child is Class
5526 case clang::BuiltinType::ObjCClass: // child is Class
5527 num_children = 1;
5528 break;
5529
5530 default:
5531 break;
5532 }
5533 break;
5534
5535 case clang::Type::Complex:
5536 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005537 case clang::Type::Record:
5538 if (GetCompleteQualType(getASTContext(), qual_type)) {
5539 const clang::RecordType *record_type =
5540 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5541 const clang::RecordDecl *record_decl = record_type->getDecl();
5542 assert(record_decl);
5543 const clang::CXXRecordDecl *cxx_record_decl =
5544 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5545 if (cxx_record_decl) {
5546 if (omit_empty_base_classes) {
Adrian Prantl05097242018-04-30 16:49:04 +00005547 // Check each base classes to see if it or any of its base classes
5548 // contain any fields. This can help limit the noise in variable
5549 // views by not having to show base classes that contain no members.
Kate Stoneb9c1b512016-09-06 20:57:50 +00005550 clang::CXXRecordDecl::base_class_const_iterator base_class,
5551 base_class_end;
5552 for (base_class = cxx_record_decl->bases_begin(),
5553 base_class_end = cxx_record_decl->bases_end();
5554 base_class != base_class_end; ++base_class) {
5555 const clang::CXXRecordDecl *base_class_decl =
5556 llvm::cast<clang::CXXRecordDecl>(
5557 base_class->getType()
5558 ->getAs<clang::RecordType>()
5559 ->getDecl());
5560
5561 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00005562 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00005563 continue;
5564
5565 num_children++;
5566 }
5567 } else {
5568 // Include all base classes
5569 num_children += cxx_record_decl->getNumBases();
5570 }
5571 }
5572 clang::RecordDecl::field_iterator field, field_end;
5573 for (field = record_decl->field_begin(),
5574 field_end = record_decl->field_end();
5575 field != field_end; ++field)
5576 ++num_children;
5577 }
5578 break;
5579
5580 case clang::Type::ObjCObject:
5581 case clang::Type::ObjCInterface:
5582 if (GetCompleteQualType(getASTContext(), qual_type)) {
5583 const clang::ObjCObjectType *objc_class_type =
5584 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5585 assert(objc_class_type);
5586 if (objc_class_type) {
5587 clang::ObjCInterfaceDecl *class_interface_decl =
5588 objc_class_type->getInterface();
5589
5590 if (class_interface_decl) {
5591
5592 clang::ObjCInterfaceDecl *superclass_interface_decl =
5593 class_interface_decl->getSuperClass();
5594 if (superclass_interface_decl) {
5595 if (omit_empty_base_classes) {
5596 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5597 ++num_children;
5598 } else
5599 ++num_children;
5600 }
5601
5602 num_children += class_interface_decl->ivar_size();
5603 }
5604 }
5605 }
5606 break;
5607
5608 case clang::Type::ObjCObjectPointer: {
5609 const clang::ObjCObjectPointerType *pointer_type =
5610 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5611 clang::QualType pointee_type = pointer_type->getPointeeType();
5612 uint32_t num_pointee_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005613 CompilerType(this, pointee_type.getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005614 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005615 // If this type points to a simple type, then it has 1 child
5616 if (num_pointee_children == 0)
5617 num_children = 1;
5618 else
5619 num_children = num_pointee_children;
5620 } break;
5621
5622 case clang::Type::Vector:
5623 case clang::Type::ExtVector:
5624 num_children =
5625 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5626 break;
5627
5628 case clang::Type::ConstantArray:
5629 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5630 ->getSize()
5631 .getLimitedValue();
5632 break;
Adrian Prantleca07c52018-11-05 20:49:07 +00005633 case clang::Type::IncompleteArray:
5634 if (auto array_info =
5635 GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx))
5636 // Only 1-dimensional arrays are supported.
5637 num_children = array_info->element_orders.size()
5638 ? array_info->element_orders.back()
5639 : 0;
5640 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005641
5642 case clang::Type::Pointer: {
5643 const clang::PointerType *pointer_type =
5644 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5645 clang::QualType pointee_type(pointer_type->getPointeeType());
5646 uint32_t num_pointee_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005647 CompilerType(this, pointee_type.getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005648 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005649 if (num_pointee_children == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00005650 // We have a pointer to a pointee type that claims it has no children. We
5651 // will want to look at
Kate Stoneb9c1b512016-09-06 20:57:50 +00005652 num_children = GetNumPointeeChildren(pointee_type);
5653 } else
5654 num_children = num_pointee_children;
5655 } break;
5656
5657 case clang::Type::LValueReference:
5658 case clang::Type::RValueReference: {
5659 const clang::ReferenceType *reference_type =
5660 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5661 clang::QualType pointee_type = reference_type->getPointeeType();
5662 uint32_t num_pointee_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005663 CompilerType(this, pointee_type.getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005664 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005665 // If this type points to a simple type, then it has 1 child
5666 if (num_pointee_children == 0)
5667 num_children = 1;
5668 else
5669 num_children = num_pointee_children;
5670 } break;
5671
5672 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00005673 num_children = CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
Kate Stoneb9c1b512016-09-06 20:57:50 +00005674 ->getDecl()
Alex Langfordbddab072019-08-13 19:40:36 +00005675 ->getUnderlyingType()
5676 .getAsOpaquePtr())
5677 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005678 break;
5679
5680 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00005681 num_children = CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
5682 ->getDeducedType()
5683 .getAsOpaquePtr())
5684 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005685 break;
5686
5687 case clang::Type::Elaborated:
5688 num_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005689 CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
5690 ->getNamedType()
5691 .getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005692 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005693 break;
5694
5695 case clang::Type::Paren:
5696 num_children =
Alex Langfordbddab072019-08-13 19:40:36 +00005697 CompilerType(
5698 this,
5699 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())
Adrian Prantleca07c52018-11-05 20:49:07 +00005700 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005701 break;
5702 default:
5703 break;
5704 }
5705 return num_children;
5706}
5707
Adrian Prantl0e4c4822019-03-06 21:22:25 +00005708CompilerType ClangASTContext::GetBuiltinTypeByName(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005709 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005710}
5711
Greg Claytond8d4a572015-08-11 21:38:15 +00005712lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005713ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5714 if (type) {
5715 clang::QualType qual_type(GetQualType(type));
5716 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5717 if (type_class == clang::Type::Builtin) {
5718 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5719 case clang::BuiltinType::Void:
5720 return eBasicTypeVoid;
5721 case clang::BuiltinType::Bool:
5722 return eBasicTypeBool;
5723 case clang::BuiltinType::Char_S:
5724 return eBasicTypeSignedChar;
5725 case clang::BuiltinType::Char_U:
5726 return eBasicTypeUnsignedChar;
5727 case clang::BuiltinType::Char16:
5728 return eBasicTypeChar16;
5729 case clang::BuiltinType::Char32:
5730 return eBasicTypeChar32;
5731 case clang::BuiltinType::UChar:
5732 return eBasicTypeUnsignedChar;
5733 case clang::BuiltinType::SChar:
5734 return eBasicTypeSignedChar;
5735 case clang::BuiltinType::WChar_S:
5736 return eBasicTypeSignedWChar;
5737 case clang::BuiltinType::WChar_U:
5738 return eBasicTypeUnsignedWChar;
5739 case clang::BuiltinType::Short:
5740 return eBasicTypeShort;
5741 case clang::BuiltinType::UShort:
5742 return eBasicTypeUnsignedShort;
5743 case clang::BuiltinType::Int:
5744 return eBasicTypeInt;
5745 case clang::BuiltinType::UInt:
5746 return eBasicTypeUnsignedInt;
5747 case clang::BuiltinType::Long:
5748 return eBasicTypeLong;
5749 case clang::BuiltinType::ULong:
5750 return eBasicTypeUnsignedLong;
5751 case clang::BuiltinType::LongLong:
5752 return eBasicTypeLongLong;
5753 case clang::BuiltinType::ULongLong:
5754 return eBasicTypeUnsignedLongLong;
5755 case clang::BuiltinType::Int128:
5756 return eBasicTypeInt128;
5757 case clang::BuiltinType::UInt128:
5758 return eBasicTypeUnsignedInt128;
5759
5760 case clang::BuiltinType::Half:
5761 return eBasicTypeHalf;
5762 case clang::BuiltinType::Float:
5763 return eBasicTypeFloat;
5764 case clang::BuiltinType::Double:
5765 return eBasicTypeDouble;
5766 case clang::BuiltinType::LongDouble:
5767 return eBasicTypeLongDouble;
5768
5769 case clang::BuiltinType::NullPtr:
5770 return eBasicTypeNullPtr;
5771 case clang::BuiltinType::ObjCId:
5772 return eBasicTypeObjCID;
5773 case clang::BuiltinType::ObjCClass:
5774 return eBasicTypeObjCClass;
5775 case clang::BuiltinType::ObjCSel:
5776 return eBasicTypeObjCSel;
5777 default:
5778 return eBasicTypeOther;
5779 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005780 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005781 }
5782 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005783}
5784
Kate Stoneb9c1b512016-09-06 20:57:50 +00005785void ClangASTContext::ForEachEnumerator(
5786 lldb::opaque_compiler_type_t type,
5787 std::function<bool(const CompilerType &integer_type,
Adrian Prantl0e4c4822019-03-06 21:22:25 +00005788 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00005789 const llvm::APSInt &value)> const &callback) {
5790 const clang::EnumType *enum_type =
5791 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5792 if (enum_type) {
5793 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5794 if (enum_decl) {
5795 CompilerType integer_type(this,
5796 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005797
Kate Stoneb9c1b512016-09-06 20:57:50 +00005798 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5799 for (enum_pos = enum_decl->enumerator_begin(),
5800 enum_end_pos = enum_decl->enumerator_end();
5801 enum_pos != enum_end_pos; ++enum_pos) {
5802 ConstString name(enum_pos->getNameAsString().c_str());
5803 if (!callback(integer_type, name, enum_pos->getInitVal()))
5804 break;
5805 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005806 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005807 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005808}
5809
Greg Claytond8d4a572015-08-11 21:38:15 +00005810#pragma mark Aggregate Types
5811
Kate Stoneb9c1b512016-09-06 20:57:50 +00005812uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5813 if (!type)
5814 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005815
Kate Stoneb9c1b512016-09-06 20:57:50 +00005816 uint32_t count = 0;
5817 clang::QualType qual_type(GetCanonicalQualType(type));
5818 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5819 switch (type_class) {
5820 case clang::Type::Record:
5821 if (GetCompleteType(type)) {
5822 const clang::RecordType *record_type =
5823 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5824 if (record_type) {
5825 clang::RecordDecl *record_decl = record_type->getDecl();
5826 if (record_decl) {
5827 uint32_t field_idx = 0;
5828 clang::RecordDecl::field_iterator field, field_end;
5829 for (field = record_decl->field_begin(),
5830 field_end = record_decl->field_end();
5831 field != field_end; ++field)
5832 ++field_idx;
5833 count = field_idx;
5834 }
5835 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005836 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005837 break;
5838
5839 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00005840 count = CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
5841 ->getDecl()
5842 ->getUnderlyingType()
5843 .getAsOpaquePtr())
5844 .GetNumFields();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005845 break;
5846
5847 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00005848 count = CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
5849 ->getDeducedType()
5850 .getAsOpaquePtr())
5851 .GetNumFields();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005852 break;
5853
5854 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00005855 count = CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
5856 ->getNamedType()
5857 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00005858 .GetNumFields();
5859 break;
5860
5861 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00005862 count =
5863 CompilerType(
5864 this,
5865 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())
5866 .GetNumFields();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005867 break;
5868
Sean Callananf9c622a2016-09-30 18:44:43 +00005869 case clang::Type::ObjCObjectPointer: {
5870 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005871 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005872 const clang::ObjCInterfaceType *objc_interface_type =
5873 objc_class_type->getInterfaceType();
5874 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005875 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5876 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005877 clang::ObjCInterfaceDecl *class_interface_decl =
5878 objc_interface_type->getDecl();
5879 if (class_interface_decl) {
5880 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005881 }
5882 }
5883 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005884 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005885
5886 case clang::Type::ObjCObject:
5887 case clang::Type::ObjCInterface:
5888 if (GetCompleteType(type)) {
5889 const clang::ObjCObjectType *objc_class_type =
5890 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5891 if (objc_class_type) {
5892 clang::ObjCInterfaceDecl *class_interface_decl =
5893 objc_class_type->getInterface();
5894
5895 if (class_interface_decl)
5896 count = class_interface_decl->ivar_size();
5897 }
5898 }
5899 break;
5900
5901 default:
5902 break;
5903 }
5904 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005905}
5906
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005907static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005908GetObjCFieldAtIndex(clang::ASTContext *ast,
5909 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5910 std::string &name, uint64_t *bit_offset_ptr,
5911 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5912 if (class_interface_decl) {
5913 if (idx < (class_interface_decl->ivar_size())) {
5914 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5915 ivar_end = class_interface_decl->ivar_end();
5916 uint32_t ivar_idx = 0;
5917
5918 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5919 ++ivar_pos, ++ivar_idx) {
5920 if (ivar_idx == idx) {
5921 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5922
5923 clang::QualType ivar_qual_type(ivar_decl->getType());
5924
5925 name.assign(ivar_decl->getNameAsString());
5926
5927 if (bit_offset_ptr) {
5928 const clang::ASTRecordLayout &interface_layout =
5929 ast->getASTObjCInterfaceLayout(class_interface_decl);
5930 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5931 }
5932
5933 const bool is_bitfield = ivar_pos->isBitField();
5934
5935 if (bitfield_bit_size_ptr) {
5936 *bitfield_bit_size_ptr = 0;
5937
5938 if (is_bitfield && ast) {
5939 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00005940 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005941 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00005942 bitfield_bit_size_expr->EvaluateAsInt(result, *ast)) {
5943 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005944 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5945 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005946 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005947 }
5948 if (is_bitfield_ptr)
5949 *is_bitfield_ptr = is_bitfield;
5950
5951 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005952 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005953 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005954 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005955 }
5956 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005957}
5958
Kate Stoneb9c1b512016-09-06 20:57:50 +00005959CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5960 size_t idx, std::string &name,
5961 uint64_t *bit_offset_ptr,
5962 uint32_t *bitfield_bit_size_ptr,
5963 bool *is_bitfield_ptr) {
5964 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005965 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005966
5967 clang::QualType qual_type(GetCanonicalQualType(type));
5968 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5969 switch (type_class) {
5970 case clang::Type::Record:
5971 if (GetCompleteType(type)) {
5972 const clang::RecordType *record_type =
5973 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5974 const clang::RecordDecl *record_decl = record_type->getDecl();
5975 uint32_t field_idx = 0;
5976 clang::RecordDecl::field_iterator field, field_end;
5977 for (field = record_decl->field_begin(),
5978 field_end = record_decl->field_end();
5979 field != field_end; ++field, ++field_idx) {
5980 if (idx == field_idx) {
5981 // Print the member type if requested
5982 // Print the member name and equal sign
5983 name.assign(field->getNameAsString());
5984
5985 // Figure out the type byte size (field_type_info.first) and
5986 // alignment (field_type_info.second) from the AST context.
5987 if (bit_offset_ptr) {
5988 const clang::ASTRecordLayout &record_layout =
5989 getASTContext()->getASTRecordLayout(record_decl);
5990 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5991 }
5992
5993 const bool is_bitfield = field->isBitField();
5994
5995 if (bitfield_bit_size_ptr) {
5996 *bitfield_bit_size_ptr = 0;
5997
5998 if (is_bitfield) {
5999 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
Hans Wennborg30ce9622018-11-28 14:30:18 +00006000 clang::Expr::EvalResult result;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006001 if (bitfield_bit_size_expr &&
Hans Wennborg30ce9622018-11-28 14:30:18 +00006002 bitfield_bit_size_expr->EvaluateAsInt(result,
Kate Stoneb9c1b512016-09-06 20:57:50 +00006003 *getASTContext())) {
Hans Wennborg30ce9622018-11-28 14:30:18 +00006004 llvm::APSInt bitfield_apsint = result.Val.getInt();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006005 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
6006 }
6007 }
6008 }
6009 if (is_bitfield_ptr)
6010 *is_bitfield_ptr = is_bitfield;
6011
Alex Langfordbddab072019-08-13 19:40:36 +00006012 return CompilerType(this, field->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006013 }
6014 }
6015 }
6016 break;
6017
Sean Callananf9c622a2016-09-30 18:44:43 +00006018 case clang::Type::ObjCObjectPointer: {
6019 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00006020 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00006021 const clang::ObjCInterfaceType *objc_interface_type =
6022 objc_class_type->getInterfaceType();
6023 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00006024 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
6025 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00006026 clang::ObjCInterfaceDecl *class_interface_decl =
6027 objc_interface_type->getDecl();
6028 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006029 return CompilerType(
6030 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6031 idx, name, bit_offset_ptr,
6032 bitfield_bit_size_ptr, is_bitfield_ptr));
6033 }
6034 }
6035 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00006036 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006037
6038 case clang::Type::ObjCObject:
6039 case clang::Type::ObjCInterface:
6040 if (GetCompleteType(type)) {
6041 const clang::ObjCObjectType *objc_class_type =
6042 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6043 assert(objc_class_type);
6044 if (objc_class_type) {
6045 clang::ObjCInterfaceDecl *class_interface_decl =
6046 objc_class_type->getInterface();
6047 return CompilerType(
6048 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6049 idx, name, bit_offset_ptr,
6050 bitfield_bit_size_ptr, is_bitfield_ptr));
6051 }
6052 }
6053 break;
6054
6055 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00006056 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
6057 ->getDecl()
6058 ->getUnderlyingType()
6059 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00006060 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6061 is_bitfield_ptr);
6062
6063 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00006064 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
6065 ->getDeducedType()
6066 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00006067 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6068 is_bitfield_ptr);
6069
6070 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00006071 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
6072 ->getNamedType()
6073 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00006074 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6075 is_bitfield_ptr);
6076
6077 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00006078 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
6079 ->desugar()
6080 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00006081 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6082 is_bitfield_ptr);
6083
6084 default:
6085 break;
6086 }
6087 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006088}
6089
Greg Clayton99558cc42015-08-24 23:46:31 +00006090uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006091ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
6092 uint32_t count = 0;
6093 clang::QualType qual_type(GetCanonicalQualType(type));
6094 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6095 switch (type_class) {
6096 case clang::Type::Record:
6097 if (GetCompleteType(type)) {
6098 const clang::CXXRecordDecl *cxx_record_decl =
6099 qual_type->getAsCXXRecordDecl();
6100 if (cxx_record_decl)
6101 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006102 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006103 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006104
Kate Stoneb9c1b512016-09-06 20:57:50 +00006105 case clang::Type::ObjCObjectPointer:
6106 count = GetPointeeType(type).GetNumDirectBaseClasses();
6107 break;
6108
6109 case clang::Type::ObjCObject:
6110 if (GetCompleteType(type)) {
6111 const clang::ObjCObjectType *objc_class_type =
6112 qual_type->getAsObjCQualifiedInterfaceType();
6113 if (objc_class_type) {
6114 clang::ObjCInterfaceDecl *class_interface_decl =
6115 objc_class_type->getInterface();
6116
6117 if (class_interface_decl && class_interface_decl->getSuperClass())
6118 count = 1;
6119 }
6120 }
6121 break;
6122 case clang::Type::ObjCInterface:
6123 if (GetCompleteType(type)) {
6124 const clang::ObjCInterfaceType *objc_interface_type =
6125 qual_type->getAs<clang::ObjCInterfaceType>();
6126 if (objc_interface_type) {
6127 clang::ObjCInterfaceDecl *class_interface_decl =
6128 objc_interface_type->getInterface();
6129
6130 if (class_interface_decl && class_interface_decl->getSuperClass())
6131 count = 1;
6132 }
6133 }
6134 break;
6135
6136 case clang::Type::Typedef:
6137 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6138 ->getDecl()
6139 ->getUnderlyingType()
6140 .getAsOpaquePtr());
6141 break;
6142
6143 case clang::Type::Auto:
6144 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6145 ->getDeducedType()
6146 .getAsOpaquePtr());
6147 break;
6148
6149 case clang::Type::Elaborated:
6150 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6151 ->getNamedType()
6152 .getAsOpaquePtr());
6153 break;
6154
6155 case clang::Type::Paren:
6156 return GetNumDirectBaseClasses(
6157 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6158
6159 default:
6160 break;
6161 }
6162 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006163}
6164
6165uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006166ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
6167 uint32_t count = 0;
6168 clang::QualType qual_type(GetCanonicalQualType(type));
6169 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6170 switch (type_class) {
6171 case clang::Type::Record:
6172 if (GetCompleteType(type)) {
6173 const clang::CXXRecordDecl *cxx_record_decl =
6174 qual_type->getAsCXXRecordDecl();
6175 if (cxx_record_decl)
6176 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006177 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006178 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006179
Kate Stoneb9c1b512016-09-06 20:57:50 +00006180 case clang::Type::Typedef:
6181 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6182 ->getDecl()
6183 ->getUnderlyingType()
6184 .getAsOpaquePtr());
6185 break;
6186
6187 case clang::Type::Auto:
6188 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6189 ->getDeducedType()
6190 .getAsOpaquePtr());
6191 break;
6192
6193 case clang::Type::Elaborated:
6194 count =
6195 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6196 ->getNamedType()
6197 .getAsOpaquePtr());
6198 break;
6199
6200 case clang::Type::Paren:
6201 count = GetNumVirtualBaseClasses(
6202 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6203 break;
6204
6205 default:
6206 break;
6207 }
6208 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006209}
6210
Kate Stoneb9c1b512016-09-06 20:57:50 +00006211CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6212 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6213 clang::QualType qual_type(GetCanonicalQualType(type));
6214 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6215 switch (type_class) {
6216 case clang::Type::Record:
6217 if (GetCompleteType(type)) {
6218 const clang::CXXRecordDecl *cxx_record_decl =
6219 qual_type->getAsCXXRecordDecl();
6220 if (cxx_record_decl) {
6221 uint32_t curr_idx = 0;
6222 clang::CXXRecordDecl::base_class_const_iterator base_class,
6223 base_class_end;
6224 for (base_class = cxx_record_decl->bases_begin(),
6225 base_class_end = cxx_record_decl->bases_end();
6226 base_class != base_class_end; ++base_class, ++curr_idx) {
6227 if (curr_idx == idx) {
6228 if (bit_offset_ptr) {
6229 const clang::ASTRecordLayout &record_layout =
6230 getASTContext()->getASTRecordLayout(cxx_record_decl);
6231 const clang::CXXRecordDecl *base_class_decl =
6232 llvm::cast<clang::CXXRecordDecl>(
6233 base_class->getType()
6234 ->getAs<clang::RecordType>()
6235 ->getDecl());
6236 if (base_class->isVirtual())
6237 *bit_offset_ptr =
6238 record_layout.getVBaseClassOffset(base_class_decl)
6239 .getQuantity() *
6240 8;
6241 else
6242 *bit_offset_ptr =
6243 record_layout.getBaseClassOffset(base_class_decl)
6244 .getQuantity() *
6245 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006246 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006247 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6248 }
6249 }
6250 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006251 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006252 break;
6253
6254 case clang::Type::ObjCObjectPointer:
6255 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6256
6257 case clang::Type::ObjCObject:
6258 if (idx == 0 && GetCompleteType(type)) {
6259 const clang::ObjCObjectType *objc_class_type =
6260 qual_type->getAsObjCQualifiedInterfaceType();
6261 if (objc_class_type) {
6262 clang::ObjCInterfaceDecl *class_interface_decl =
6263 objc_class_type->getInterface();
6264
6265 if (class_interface_decl) {
6266 clang::ObjCInterfaceDecl *superclass_interface_decl =
6267 class_interface_decl->getSuperClass();
6268 if (superclass_interface_decl) {
6269 if (bit_offset_ptr)
6270 *bit_offset_ptr = 0;
Alex Langfordbddab072019-08-13 19:40:36 +00006271 return CompilerType(this,
Kate Stoneb9c1b512016-09-06 20:57:50 +00006272 getASTContext()->getObjCInterfaceType(
Alex Langfordbddab072019-08-13 19:40:36 +00006273 superclass_interface_decl).getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006274 }
6275 }
6276 }
6277 }
6278 break;
6279 case clang::Type::ObjCInterface:
6280 if (idx == 0 && GetCompleteType(type)) {
6281 const clang::ObjCObjectType *objc_interface_type =
6282 qual_type->getAs<clang::ObjCInterfaceType>();
6283 if (objc_interface_type) {
6284 clang::ObjCInterfaceDecl *class_interface_decl =
6285 objc_interface_type->getInterface();
6286
6287 if (class_interface_decl) {
6288 clang::ObjCInterfaceDecl *superclass_interface_decl =
6289 class_interface_decl->getSuperClass();
6290 if (superclass_interface_decl) {
6291 if (bit_offset_ptr)
6292 *bit_offset_ptr = 0;
Alex Langfordbddab072019-08-13 19:40:36 +00006293 return CompilerType(
6294 this, getASTContext()
6295 ->getObjCInterfaceType(superclass_interface_decl)
6296 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006297 }
6298 }
6299 }
6300 }
6301 break;
6302
6303 case clang::Type::Typedef:
6304 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6305 ->getDecl()
6306 ->getUnderlyingType()
6307 .getAsOpaquePtr(),
6308 idx, bit_offset_ptr);
6309
6310 case clang::Type::Auto:
6311 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6312 ->getDeducedType()
6313 .getAsOpaquePtr(),
6314 idx, bit_offset_ptr);
6315
6316 case clang::Type::Elaborated:
6317 return GetDirectBaseClassAtIndex(
6318 llvm::cast<clang::ElaboratedType>(qual_type)
6319 ->getNamedType()
6320 .getAsOpaquePtr(),
6321 idx, bit_offset_ptr);
6322
6323 case clang::Type::Paren:
6324 return GetDirectBaseClassAtIndex(
6325 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6326 idx, bit_offset_ptr);
6327
6328 default:
6329 break;
6330 }
6331 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006332}
6333
Kate Stoneb9c1b512016-09-06 20:57:50 +00006334CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6335 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6336 clang::QualType qual_type(GetCanonicalQualType(type));
6337 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6338 switch (type_class) {
6339 case clang::Type::Record:
6340 if (GetCompleteType(type)) {
6341 const clang::CXXRecordDecl *cxx_record_decl =
6342 qual_type->getAsCXXRecordDecl();
6343 if (cxx_record_decl) {
6344 uint32_t curr_idx = 0;
6345 clang::CXXRecordDecl::base_class_const_iterator base_class,
6346 base_class_end;
6347 for (base_class = cxx_record_decl->vbases_begin(),
6348 base_class_end = cxx_record_decl->vbases_end();
6349 base_class != base_class_end; ++base_class, ++curr_idx) {
6350 if (curr_idx == idx) {
6351 if (bit_offset_ptr) {
6352 const clang::ASTRecordLayout &record_layout =
6353 getASTContext()->getASTRecordLayout(cxx_record_decl);
6354 const clang::CXXRecordDecl *base_class_decl =
6355 llvm::cast<clang::CXXRecordDecl>(
6356 base_class->getType()
6357 ->getAs<clang::RecordType>()
6358 ->getDecl());
6359 *bit_offset_ptr =
6360 record_layout.getVBaseClassOffset(base_class_decl)
6361 .getQuantity() *
6362 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006363 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006364 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6365 }
6366 }
6367 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006368 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006369 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006370
Kate Stoneb9c1b512016-09-06 20:57:50 +00006371 case clang::Type::Typedef:
6372 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6373 ->getDecl()
6374 ->getUnderlyingType()
6375 .getAsOpaquePtr(),
6376 idx, bit_offset_ptr);
6377
6378 case clang::Type::Auto:
6379 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6380 ->getDeducedType()
6381 .getAsOpaquePtr(),
6382 idx, bit_offset_ptr);
6383
6384 case clang::Type::Elaborated:
6385 return GetVirtualBaseClassAtIndex(
6386 llvm::cast<clang::ElaboratedType>(qual_type)
6387 ->getNamedType()
6388 .getAsOpaquePtr(),
6389 idx, bit_offset_ptr);
6390
6391 case clang::Type::Paren:
6392 return GetVirtualBaseClassAtIndex(
6393 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6394 idx, bit_offset_ptr);
6395
6396 default:
6397 break;
6398 }
6399 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006400}
6401
Greg Claytond8d4a572015-08-11 21:38:15 +00006402// If a pointer to a pointee type (the clang_type arg) says that it has no
6403// children, then we either need to trust it, or override it and return a
6404// different result. For example, an "int *" has one child that is an integer,
6405// but a function pointer doesn't have any children. Likewise if a Record type
6406// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006407uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6408 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006409 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006410
6411 clang::QualType qual_type(type.getCanonicalType());
6412 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6413 switch (type_class) {
6414 case clang::Type::Builtin:
6415 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6416 case clang::BuiltinType::UnknownAny:
6417 case clang::BuiltinType::Void:
6418 case clang::BuiltinType::NullPtr:
6419 case clang::BuiltinType::OCLEvent:
6420 case clang::BuiltinType::OCLImage1dRO:
6421 case clang::BuiltinType::OCLImage1dWO:
6422 case clang::BuiltinType::OCLImage1dRW:
6423 case clang::BuiltinType::OCLImage1dArrayRO:
6424 case clang::BuiltinType::OCLImage1dArrayWO:
6425 case clang::BuiltinType::OCLImage1dArrayRW:
6426 case clang::BuiltinType::OCLImage1dBufferRO:
6427 case clang::BuiltinType::OCLImage1dBufferWO:
6428 case clang::BuiltinType::OCLImage1dBufferRW:
6429 case clang::BuiltinType::OCLImage2dRO:
6430 case clang::BuiltinType::OCLImage2dWO:
6431 case clang::BuiltinType::OCLImage2dRW:
6432 case clang::BuiltinType::OCLImage2dArrayRO:
6433 case clang::BuiltinType::OCLImage2dArrayWO:
6434 case clang::BuiltinType::OCLImage2dArrayRW:
6435 case clang::BuiltinType::OCLImage3dRO:
6436 case clang::BuiltinType::OCLImage3dWO:
6437 case clang::BuiltinType::OCLImage3dRW:
6438 case clang::BuiltinType::OCLSampler:
6439 return 0;
6440 case clang::BuiltinType::Bool:
6441 case clang::BuiltinType::Char_U:
6442 case clang::BuiltinType::UChar:
6443 case clang::BuiltinType::WChar_U:
6444 case clang::BuiltinType::Char16:
6445 case clang::BuiltinType::Char32:
6446 case clang::BuiltinType::UShort:
6447 case clang::BuiltinType::UInt:
6448 case clang::BuiltinType::ULong:
6449 case clang::BuiltinType::ULongLong:
6450 case clang::BuiltinType::UInt128:
6451 case clang::BuiltinType::Char_S:
6452 case clang::BuiltinType::SChar:
6453 case clang::BuiltinType::WChar_S:
6454 case clang::BuiltinType::Short:
6455 case clang::BuiltinType::Int:
6456 case clang::BuiltinType::Long:
6457 case clang::BuiltinType::LongLong:
6458 case clang::BuiltinType::Int128:
6459 case clang::BuiltinType::Float:
6460 case clang::BuiltinType::Double:
6461 case clang::BuiltinType::LongDouble:
6462 case clang::BuiltinType::Dependent:
6463 case clang::BuiltinType::Overload:
6464 case clang::BuiltinType::ObjCId:
6465 case clang::BuiltinType::ObjCClass:
6466 case clang::BuiltinType::ObjCSel:
6467 case clang::BuiltinType::BoundMember:
6468 case clang::BuiltinType::Half:
6469 case clang::BuiltinType::ARCUnbridgedCast:
6470 case clang::BuiltinType::PseudoObject:
6471 case clang::BuiltinType::BuiltinFn:
6472 case clang::BuiltinType::OMPArraySection:
6473 return 1;
6474 default:
6475 return 0;
6476 }
6477 break;
6478
6479 case clang::Type::Complex:
6480 return 1;
6481 case clang::Type::Pointer:
6482 return 1;
6483 case clang::Type::BlockPointer:
6484 return 0; // If block pointers don't have debug info, then no children for
6485 // them
6486 case clang::Type::LValueReference:
6487 return 1;
6488 case clang::Type::RValueReference:
6489 return 1;
6490 case clang::Type::MemberPointer:
6491 return 0;
6492 case clang::Type::ConstantArray:
6493 return 0;
6494 case clang::Type::IncompleteArray:
6495 return 0;
6496 case clang::Type::VariableArray:
6497 return 0;
6498 case clang::Type::DependentSizedArray:
6499 return 0;
6500 case clang::Type::DependentSizedExtVector:
6501 return 0;
6502 case clang::Type::Vector:
6503 return 0;
6504 case clang::Type::ExtVector:
6505 return 0;
6506 case clang::Type::FunctionProto:
6507 return 0; // When we function pointers, they have no children...
6508 case clang::Type::FunctionNoProto:
6509 return 0; // When we function pointers, they have no children...
6510 case clang::Type::UnresolvedUsing:
6511 return 0;
6512 case clang::Type::Paren:
6513 return GetNumPointeeChildren(
6514 llvm::cast<clang::ParenType>(qual_type)->desugar());
6515 case clang::Type::Typedef:
6516 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6517 ->getDecl()
6518 ->getUnderlyingType());
6519 case clang::Type::Auto:
6520 return GetNumPointeeChildren(
6521 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6522 case clang::Type::Elaborated:
6523 return GetNumPointeeChildren(
6524 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6525 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006526 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6527 ->getUnderlyingExpr()
6528 ->getType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006529 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006530 return GetNumPointeeChildren(
6531 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006532 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006533 return GetNumPointeeChildren(
6534 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006535 case clang::Type::Record:
6536 return 0;
6537 case clang::Type::Enum:
6538 return 1;
6539 case clang::Type::TemplateTypeParm:
6540 return 1;
6541 case clang::Type::SubstTemplateTypeParm:
6542 return 1;
6543 case clang::Type::TemplateSpecialization:
6544 return 1;
6545 case clang::Type::InjectedClassName:
6546 return 0;
6547 case clang::Type::DependentName:
6548 return 1;
6549 case clang::Type::DependentTemplateSpecialization:
6550 return 1;
6551 case clang::Type::ObjCObject:
6552 return 0;
6553 case clang::Type::ObjCInterface:
6554 return 0;
6555 case clang::Type::ObjCObjectPointer:
6556 return 1;
6557 default:
6558 break;
6559 }
6560 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006561}
6562
Kate Stoneb9c1b512016-09-06 20:57:50 +00006563CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6564 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6565 bool transparent_pointers, bool omit_empty_base_classes,
6566 bool ignore_array_bounds, std::string &child_name,
6567 uint32_t &child_byte_size, int32_t &child_byte_offset,
6568 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6569 bool &child_is_base_class, bool &child_is_deref_of_parent,
6570 ValueObject *valobj, uint64_t &language_flags) {
6571 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006572 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006573
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006574 auto get_exe_scope = [&exe_ctx]() {
6575 return exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr;
6576 };
6577
Kate Stoneb9c1b512016-09-06 20:57:50 +00006578 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6579 const clang::Type::TypeClass parent_type_class =
6580 parent_qual_type->getTypeClass();
6581 child_bitfield_bit_size = 0;
6582 child_bitfield_bit_offset = 0;
6583 child_is_base_class = false;
6584 language_flags = 0;
6585
Adrian Prantleca07c52018-11-05 20:49:07 +00006586 const bool idx_is_valid =
6587 idx < GetNumChildren(type, omit_empty_base_classes, exe_ctx);
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006588 int32_t bit_offset;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006589 switch (parent_type_class) {
6590 case clang::Type::Builtin:
6591 if (idx_is_valid) {
6592 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6593 case clang::BuiltinType::ObjCId:
6594 case clang::BuiltinType::ObjCClass:
6595 child_name = "isa";
6596 child_byte_size =
6597 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6598 CHAR_BIT;
Alex Langfordbddab072019-08-13 19:40:36 +00006599 return CompilerType(
6600 this, getASTContext()->ObjCBuiltinClassTy.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006601
6602 default:
6603 break;
6604 }
6605 }
6606 break;
6607
6608 case clang::Type::Record:
6609 if (idx_is_valid && GetCompleteType(type)) {
6610 const clang::RecordType *record_type =
6611 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6612 const clang::RecordDecl *record_decl = record_type->getDecl();
6613 assert(record_decl);
6614 const clang::ASTRecordLayout &record_layout =
6615 getASTContext()->getASTRecordLayout(record_decl);
6616 uint32_t child_idx = 0;
6617
6618 const clang::CXXRecordDecl *cxx_record_decl =
6619 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6620 if (cxx_record_decl) {
6621 // We might have base classes to print out first
6622 clang::CXXRecordDecl::base_class_const_iterator base_class,
6623 base_class_end;
6624 for (base_class = cxx_record_decl->bases_begin(),
6625 base_class_end = cxx_record_decl->bases_end();
6626 base_class != base_class_end; ++base_class) {
6627 const clang::CXXRecordDecl *base_class_decl = nullptr;
6628
6629 // Skip empty base classes
6630 if (omit_empty_base_classes) {
6631 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6632 base_class->getType()->getAs<clang::RecordType>()->getDecl());
Jonas Devliegherea6682a42018-12-15 00:15:33 +00006633 if (!ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00006634 continue;
6635 }
6636
6637 if (idx == child_idx) {
6638 if (base_class_decl == nullptr)
6639 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6640 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6641
6642 if (base_class->isVirtual()) {
6643 bool handled = false;
6644 if (valobj) {
Aleksandr Urakov1dc51db2018-11-12 16:23:50 +00006645 clang::VTableContextBase *vtable_ctx =
6646 getASTContext()->getVTableContext();
6647 if (vtable_ctx)
6648 handled = GetVBaseBitOffset(*vtable_ctx, *valobj,
6649 record_layout, cxx_record_decl,
6650 base_class_decl, bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006651 }
6652 if (!handled)
6653 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6654 .getQuantity() *
6655 8;
6656 } else
6657 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6658 .getQuantity() *
6659 8;
6660
6661 // Base classes should be a multiple of 8 bits in size
6662 child_byte_offset = bit_offset / 8;
Alex Langfordbddab072019-08-13 19:40:36 +00006663 CompilerType base_class_clang_type(
6664 this, base_class->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006665 child_name = base_class_clang_type.GetTypeName().AsCString("");
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006666 Optional<uint64_t> size =
6667 base_class_clang_type.GetBitSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006668 if (!size)
6669 return {};
6670 uint64_t base_class_clang_type_bit_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006671
6672 // Base classes bit sizes should be a multiple of 8 bits in size
6673 assert(base_class_clang_type_bit_size % 8 == 0);
6674 child_byte_size = base_class_clang_type_bit_size / 8;
6675 child_is_base_class = true;
6676 return base_class_clang_type;
6677 }
6678 // We don't increment the child index in the for loop since we might
6679 // be skipping empty base classes
6680 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006681 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006682 }
6683 // Make sure index is in range...
6684 uint32_t field_idx = 0;
6685 clang::RecordDecl::field_iterator field, field_end;
6686 for (field = record_decl->field_begin(),
6687 field_end = record_decl->field_end();
6688 field != field_end; ++field, ++field_idx, ++child_idx) {
6689 if (idx == child_idx) {
6690 // Print the member type if requested
6691 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006692 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006693
6694 // Figure out the type byte size (field_type_info.first) and
6695 // alignment (field_type_info.second) from the AST context.
Alex Langfordbddab072019-08-13 19:40:36 +00006696 CompilerType field_clang_type(this,
6697 field->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006698 assert(field_idx < record_layout.getFieldCount());
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006699 Optional<uint64_t> size =
6700 field_clang_type.GetByteSize(get_exe_scope());
Adrian Prantld963a7c2019-01-15 18:07:52 +00006701 if (!size)
6702 return {};
6703 child_byte_size = *size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006704 const uint32_t child_bit_size = child_byte_size * 8;
6705
6706 // Figure out the field offset within the current struct/union/class
6707 // type
6708 bit_offset = record_layout.getFieldOffset(field_idx);
6709 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6710 child_bitfield_bit_size)) {
6711 child_bitfield_bit_offset = bit_offset % child_bit_size;
6712 const uint32_t child_bit_offset =
6713 bit_offset - child_bitfield_bit_offset;
6714 child_byte_offset = child_bit_offset / 8;
6715 } else {
6716 child_byte_offset = bit_offset / 8;
6717 }
6718
6719 return field_clang_type;
6720 }
6721 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006722 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006723 break;
6724
6725 case clang::Type::ObjCObject:
6726 case clang::Type::ObjCInterface:
6727 if (idx_is_valid && GetCompleteType(type)) {
6728 const clang::ObjCObjectType *objc_class_type =
6729 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6730 assert(objc_class_type);
6731 if (objc_class_type) {
6732 uint32_t child_idx = 0;
6733 clang::ObjCInterfaceDecl *class_interface_decl =
6734 objc_class_type->getInterface();
6735
6736 if (class_interface_decl) {
6737
6738 const clang::ASTRecordLayout &interface_layout =
6739 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6740 clang::ObjCInterfaceDecl *superclass_interface_decl =
6741 class_interface_decl->getSuperClass();
6742 if (superclass_interface_decl) {
6743 if (omit_empty_base_classes) {
6744 CompilerType base_class_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00006745 this, getASTContext()
6746 ->getObjCInterfaceType(superclass_interface_decl)
6747 .getAsOpaquePtr());
Adrian Prantleca07c52018-11-05 20:49:07 +00006748 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes,
6749 exe_ctx) > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006750 if (idx == 0) {
6751 clang::QualType ivar_qual_type(
6752 getASTContext()->getObjCInterfaceType(
6753 superclass_interface_decl));
6754
6755 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006756 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006757
6758 clang::TypeInfo ivar_type_info =
6759 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6760
6761 child_byte_size = ivar_type_info.Width / 8;
6762 child_byte_offset = 0;
6763 child_is_base_class = true;
6764
Alex Langfordbddab072019-08-13 19:40:36 +00006765 return CompilerType(this, ivar_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006766 }
6767
6768 ++child_idx;
6769 }
6770 } else
6771 ++child_idx;
6772 }
6773
6774 const uint32_t superclass_idx = child_idx;
6775
6776 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6777 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6778 ivar_end = class_interface_decl->ivar_end();
6779
6780 for (ivar_pos = class_interface_decl->ivar_begin();
6781 ivar_pos != ivar_end; ++ivar_pos) {
6782 if (child_idx == idx) {
6783 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6784
6785 clang::QualType ivar_qual_type(ivar_decl->getType());
6786
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006787 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006788
6789 clang::TypeInfo ivar_type_info =
6790 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6791
6792 child_byte_size = ivar_type_info.Width / 8;
6793
6794 // Figure out the field offset within the current
Adrian Prantl05097242018-04-30 16:49:04 +00006795 // struct/union/class type For ObjC objects, we can't trust the
6796 // bit offset we get from the Clang AST, since that doesn't
6797 // account for the space taken up by unbacked properties, or
6798 // from the changing size of base classes that are newer than
6799 // this class. So if we have a process around that we can ask
6800 // about this object, do so.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006801 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6802 Process *process = nullptr;
6803 if (exe_ctx)
6804 process = exe_ctx->GetProcessPtr();
6805 if (process) {
6806 ObjCLanguageRuntime *objc_runtime =
Alex Langforde823bbe2019-06-10 20:53:23 +00006807 ObjCLanguageRuntime::Get(*process);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006808 if (objc_runtime != nullptr) {
Alex Langfordbddab072019-08-13 19:40:36 +00006809 CompilerType parent_ast_type(
6810 this, parent_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006811 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6812 parent_ast_type, ivar_decl->getNameAsString().c_str());
6813 }
6814 }
6815
Aleksandr Urakovff701722018-08-20 05:59:27 +00006816 // Setting this to INT32_MAX to make sure we don't compute it
Kate Stoneb9c1b512016-09-06 20:57:50 +00006817 // twice...
Aleksandr Urakov53459482018-08-17 07:28:24 +00006818 bit_offset = INT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006819
6820 if (child_byte_offset ==
6821 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6822 bit_offset = interface_layout.getFieldOffset(child_idx -
6823 superclass_idx);
6824 child_byte_offset = bit_offset / 8;
6825 }
6826
6827 // Note, the ObjC Ivar Byte offset is just that, it doesn't
Adrian Prantl05097242018-04-30 16:49:04 +00006828 // account for the bit offset of a bitfield within its
6829 // containing object. So regardless of where we get the byte
Kate Stoneb9c1b512016-09-06 20:57:50 +00006830 // offset from, we still need to get the bit offset for
6831 // bitfields from the layout.
6832
6833 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6834 child_bitfield_bit_size)) {
Aleksandr Urakov53459482018-08-17 07:28:24 +00006835 if (bit_offset == INT32_MAX)
Kate Stoneb9c1b512016-09-06 20:57:50 +00006836 bit_offset = interface_layout.getFieldOffset(
6837 child_idx - superclass_idx);
6838
6839 child_bitfield_bit_offset = bit_offset % 8;
6840 }
Alex Langfordbddab072019-08-13 19:40:36 +00006841 return CompilerType(this, ivar_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006842 }
6843 ++child_idx;
6844 }
6845 }
6846 }
6847 }
6848 }
6849 break;
6850
6851 case clang::Type::ObjCObjectPointer:
6852 if (idx_is_valid) {
6853 CompilerType pointee_clang_type(GetPointeeType(type));
6854
6855 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6856 child_is_deref_of_parent = false;
6857 bool tmp_child_is_deref_of_parent = false;
6858 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6859 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6860 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6861 child_bitfield_bit_size, child_bitfield_bit_offset,
6862 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6863 language_flags);
6864 } else {
6865 child_is_deref_of_parent = true;
6866 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00006867 valobj ? valobj->GetName().GetCString() : nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006868 if (parent_name) {
6869 child_name.assign(1, '*');
6870 child_name += parent_name;
6871 }
6872
6873 // We have a pointer to an simple type
6874 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006875 if (Optional<uint64_t> size =
6876 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006877 child_byte_size = *size;
6878 child_byte_offset = 0;
6879 return pointee_clang_type;
6880 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006881 }
6882 }
6883 }
6884 break;
6885
6886 case clang::Type::Vector:
6887 case clang::Type::ExtVector:
6888 if (idx_is_valid) {
6889 const clang::VectorType *array =
6890 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6891 if (array) {
Alex Langfordbddab072019-08-13 19:40:36 +00006892 CompilerType element_type(this,
6893 array->getElementType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006894 if (element_type.GetCompleteType()) {
6895 char element_name[64];
6896 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6897 static_cast<uint64_t>(idx));
6898 child_name.assign(element_name);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006899 if (Optional<uint64_t> size =
6900 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006901 child_byte_size = *size;
6902 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6903 return element_type;
6904 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006905 }
6906 }
6907 }
6908 break;
6909
6910 case clang::Type::ConstantArray:
6911 case clang::Type::IncompleteArray:
6912 if (ignore_array_bounds || idx_is_valid) {
6913 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6914 if (array) {
Alex Langfordbddab072019-08-13 19:40:36 +00006915 CompilerType element_type(this,
6916 array->getElementType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006917 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006918 child_name = llvm::formatv("[{0}]", idx);
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006919 if (Optional<uint64_t> size =
6920 element_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006921 child_byte_size = *size;
6922 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6923 return element_type;
6924 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006925 }
6926 }
6927 }
6928 break;
6929
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006930 case clang::Type::Pointer: {
6931 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006932
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006933 // Don't dereference "void *" pointers
6934 if (pointee_clang_type.IsVoidType())
6935 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006936
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006937 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6938 child_is_deref_of_parent = false;
6939 bool tmp_child_is_deref_of_parent = false;
6940 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6941 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6942 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6943 child_bitfield_bit_size, child_bitfield_bit_offset,
6944 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6945 language_flags);
6946 } else {
6947 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006948
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006949 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00006950 valobj ? valobj->GetName().GetCString() : nullptr;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006951 if (parent_name) {
6952 child_name.assign(1, '*');
6953 child_name += parent_name;
6954 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006955
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006956 // We have a pointer to an simple type
6957 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006958 if (Optional<uint64_t> size =
6959 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006960 child_byte_size = *size;
6961 child_byte_offset = 0;
6962 return pointee_clang_type;
6963 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006964 }
6965 }
6966 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006967 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006968
6969 case clang::Type::LValueReference:
6970 case clang::Type::RValueReference:
6971 if (idx_is_valid) {
6972 const clang::ReferenceType *reference_type =
6973 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
Alex Langfordbddab072019-08-13 19:40:36 +00006974 CompilerType pointee_clang_type(
6975 this, reference_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006976 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6977 child_is_deref_of_parent = false;
6978 bool tmp_child_is_deref_of_parent = false;
6979 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6980 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6981 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6982 child_bitfield_bit_size, child_bitfield_bit_offset,
6983 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6984 language_flags);
6985 } else {
6986 const char *parent_name =
Konrad Kleine248a1302019-05-23 11:14:47 +00006987 valobj ? valobj->GetName().GetCString() : nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006988 if (parent_name) {
6989 child_name.assign(1, '&');
6990 child_name += parent_name;
6991 }
6992
6993 // We have a pointer to an simple type
6994 if (idx == 0) {
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00006995 if (Optional<uint64_t> size =
6996 pointee_clang_type.GetByteSize(get_exe_scope())) {
Adrian Prantld963a7c2019-01-15 18:07:52 +00006997 child_byte_size = *size;
6998 child_byte_offset = 0;
6999 return pointee_clang_type;
7000 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007001 }
7002 }
7003 }
7004 break;
7005
7006 case clang::Type::Typedef: {
7007 CompilerType typedefed_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00007008 this, llvm::cast<clang::TypedefType>(parent_qual_type)
7009 ->getDecl()
7010 ->getUnderlyingType()
7011 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007012 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
7013 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7014 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7015 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7016 child_is_deref_of_parent, valobj, language_flags);
7017 } break;
7018
7019 case clang::Type::Auto: {
7020 CompilerType elaborated_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00007021 this, llvm::cast<clang::AutoType>(parent_qual_type)
7022 ->getDeducedType()
7023 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007024 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7025 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7026 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7027 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7028 child_is_deref_of_parent, valobj, language_flags);
7029 }
7030
7031 case clang::Type::Elaborated: {
7032 CompilerType elaborated_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00007033 this, llvm::cast<clang::ElaboratedType>(parent_qual_type)
7034 ->getNamedType()
7035 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007036 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7037 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7038 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7039 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7040 child_is_deref_of_parent, valobj, language_flags);
7041 }
7042
7043 case clang::Type::Paren: {
Alex Langfordbddab072019-08-13 19:40:36 +00007044 CompilerType paren_clang_type(this,
7045 llvm::cast<clang::ParenType>(parent_qual_type)
7046 ->desugar()
7047 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007048 return paren_clang_type.GetChildCompilerTypeAtIndex(
7049 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7050 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7051 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7052 child_is_deref_of_parent, valobj, language_flags);
7053 }
7054
7055 default:
7056 break;
7057 }
7058 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007059}
7060
Kate Stoneb9c1b512016-09-06 20:57:50 +00007061static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
7062 const clang::CXXBaseSpecifier *base_spec,
7063 bool omit_empty_base_classes) {
7064 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007065
Kate Stoneb9c1b512016-09-06 20:57:50 +00007066 const clang::CXXRecordDecl *cxx_record_decl =
7067 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7068
7069 // const char *super_name = record_decl->getNameAsCString();
7070 // const char *base_name =
7071 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
7072 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
7073 //
7074 if (cxx_record_decl) {
7075 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
7076 for (base_class = cxx_record_decl->bases_begin(),
7077 base_class_end = cxx_record_decl->bases_end();
7078 base_class != base_class_end; ++base_class) {
7079 if (omit_empty_base_classes) {
7080 if (BaseSpecifierIsEmpty(base_class))
7081 continue;
7082 }
7083
7084 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
7085 // super_name, base_name,
7086 // child_idx,
7087 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
7088 //
7089 //
7090 if (base_class == base_spec)
7091 return child_idx;
7092 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00007093 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007094 }
7095
7096 return UINT32_MAX;
7097}
7098
7099static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
7100 clang::NamedDecl *canonical_decl,
7101 bool omit_empty_base_classes) {
7102 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
7103 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
7104 omit_empty_base_classes);
7105
7106 clang::RecordDecl::field_iterator field, field_end;
7107 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
7108 field != field_end; ++field, ++child_idx) {
7109 if (field->getCanonicalDecl() == canonical_decl)
7110 return child_idx;
7111 }
7112
7113 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00007114}
7115
7116// Look for a child member (doesn't include base classes, but it does include
Adrian Prantl05097242018-04-30 16:49:04 +00007117// their members) in the type hierarchy. Returns an index path into
7118// "clang_type" on how to reach the appropriate member.
Greg Claytond8d4a572015-08-11 21:38:15 +00007119//
7120// class A
7121// {
7122// public:
7123// int m_a;
7124// int m_b;
7125// };
7126//
7127// class B
7128// {
7129// };
7130//
7131// class C :
7132// public B,
7133// public A
7134// {
7135// };
7136//
7137// If we have a clang type that describes "class C", and we wanted to looked
7138// "m_b" in it:
7139//
Kate Stoneb9c1b512016-09-06 20:57:50 +00007140// With omit_empty_base_classes == false we would get an integer array back
Adrian Prantl05097242018-04-30 16:49:04 +00007141// with: { 1, 1 } The first index 1 is the child index for "class A" within
7142// class C The second index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007143//
Adrian Prantl05097242018-04-30 16:49:04 +00007144// With omit_empty_base_classes == true we would get an integer array back
7145// with: { 0, 1 } The first index 0 is the child index for "class A" within
7146// class C (since class B doesn't have any members it doesn't count) The second
7147// index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007148
Kate Stoneb9c1b512016-09-06 20:57:50 +00007149size_t ClangASTContext::GetIndexOfChildMemberWithName(
7150 lldb::opaque_compiler_type_t type, const char *name,
7151 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
7152 if (type && name && name[0]) {
7153 clang::QualType qual_type(GetCanonicalQualType(type));
7154 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7155 switch (type_class) {
7156 case clang::Type::Record:
7157 if (GetCompleteType(type)) {
7158 const clang::RecordType *record_type =
7159 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7160 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00007161
Kate Stoneb9c1b512016-09-06 20:57:50 +00007162 assert(record_decl);
7163 uint32_t child_idx = 0;
7164
7165 const clang::CXXRecordDecl *cxx_record_decl =
7166 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7167
7168 // Try and find a field that matches NAME
7169 clang::RecordDecl::field_iterator field, field_end;
7170 llvm::StringRef name_sref(name);
7171 for (field = record_decl->field_begin(),
7172 field_end = record_decl->field_end();
7173 field != field_end; ++field, ++child_idx) {
7174 llvm::StringRef field_name = field->getName();
7175 if (field_name.empty()) {
Alex Langfordbddab072019-08-13 19:40:36 +00007176 CompilerType field_type(this, field->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007177 child_indexes.push_back(child_idx);
7178 if (field_type.GetIndexOfChildMemberWithName(
7179 name, omit_empty_base_classes, child_indexes))
7180 return child_indexes.size();
7181 child_indexes.pop_back();
7182
7183 } else if (field_name.equals(name_sref)) {
7184 // We have to add on the number of base classes to this index!
7185 child_indexes.push_back(
7186 child_idx + ClangASTContext::GetNumBaseClasses(
7187 cxx_record_decl, omit_empty_base_classes));
7188 return child_indexes.size();
7189 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007190 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007191
Kate Stoneb9c1b512016-09-06 20:57:50 +00007192 if (cxx_record_decl) {
7193 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7194
7195 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7196
7197 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7198 // Didn't find things easily, lets let clang do its thang...
7199 clang::IdentifierInfo &ident_ref =
7200 getASTContext()->Idents.get(name_sref);
7201 clang::DeclarationName decl_name(&ident_ref);
7202
7203 clang::CXXBasePaths paths;
7204 if (cxx_record_decl->lookupInBases(
7205 [decl_name](const clang::CXXBaseSpecifier *specifier,
7206 clang::CXXBasePath &path) {
7207 return clang::CXXRecordDecl::FindOrdinaryMember(
7208 specifier, path, decl_name);
7209 },
7210 paths)) {
7211 clang::CXXBasePaths::const_paths_iterator path,
7212 path_end = paths.end();
7213 for (path = paths.begin(); path != path_end; ++path) {
7214 const size_t num_path_elements = path->size();
7215 for (size_t e = 0; e < num_path_elements; ++e) {
7216 clang::CXXBasePathElement elem = (*path)[e];
7217
7218 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7219 omit_empty_base_classes);
7220 if (child_idx == UINT32_MAX) {
7221 child_indexes.clear();
7222 return 0;
7223 } else {
7224 child_indexes.push_back(child_idx);
7225 parent_record_decl = llvm::cast<clang::RecordDecl>(
7226 elem.Base->getType()
7227 ->getAs<clang::RecordType>()
7228 ->getDecl());
7229 }
7230 }
7231 for (clang::NamedDecl *path_decl : path->Decls) {
7232 child_idx = GetIndexForRecordChild(
7233 parent_record_decl, path_decl, omit_empty_base_classes);
7234 if (child_idx == UINT32_MAX) {
7235 child_indexes.clear();
7236 return 0;
7237 } else {
7238 child_indexes.push_back(child_idx);
7239 }
7240 }
7241 }
7242 return child_indexes.size();
7243 }
7244 }
7245 }
7246 break;
7247
7248 case clang::Type::ObjCObject:
7249 case clang::Type::ObjCInterface:
7250 if (GetCompleteType(type)) {
7251 llvm::StringRef name_sref(name);
7252 const clang::ObjCObjectType *objc_class_type =
7253 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7254 assert(objc_class_type);
7255 if (objc_class_type) {
7256 uint32_t child_idx = 0;
7257 clang::ObjCInterfaceDecl *class_interface_decl =
7258 objc_class_type->getInterface();
7259
7260 if (class_interface_decl) {
7261 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7262 ivar_end = class_interface_decl->ivar_end();
7263 clang::ObjCInterfaceDecl *superclass_interface_decl =
7264 class_interface_decl->getSuperClass();
7265
7266 for (ivar_pos = class_interface_decl->ivar_begin();
7267 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7268 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7269
7270 if (ivar_decl->getName().equals(name_sref)) {
7271 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7272 (omit_empty_base_classes &&
7273 ObjCDeclHasIVars(superclass_interface_decl, true)))
7274 ++child_idx;
7275
7276 child_indexes.push_back(child_idx);
7277 return child_indexes.size();
7278 }
7279 }
7280
7281 if (superclass_interface_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00007282 // The super class index is always zero for ObjC classes, so we
7283 // push it onto the child indexes in case we find an ivar in our
7284 // superclass...
Kate Stoneb9c1b512016-09-06 20:57:50 +00007285 child_indexes.push_back(0);
7286
7287 CompilerType superclass_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00007288 this, getASTContext()
7289 ->getObjCInterfaceType(superclass_interface_decl)
7290 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007291 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7292 name, omit_empty_base_classes, child_indexes)) {
Adrian Prantl05097242018-04-30 16:49:04 +00007293 // We did find an ivar in a superclass so just return the
7294 // results!
Kate Stoneb9c1b512016-09-06 20:57:50 +00007295 return child_indexes.size();
7296 }
7297
Adrian Prantl05097242018-04-30 16:49:04 +00007298 // We didn't find an ivar matching "name" in our superclass, pop
7299 // the superclass zero index that we pushed on above.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007300 child_indexes.pop_back();
7301 }
7302 }
7303 }
7304 }
7305 break;
7306
7307 case clang::Type::ObjCObjectPointer: {
7308 CompilerType objc_object_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00007309 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7310 ->getPointeeType()
7311 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007312 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7313 name, omit_empty_base_classes, child_indexes);
7314 } break;
7315
7316 case clang::Type::ConstantArray: {
7317 // const clang::ConstantArrayType *array =
7318 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7319 // const uint64_t element_count =
7320 // array->getSize().getLimitedValue();
7321 //
7322 // if (idx < element_count)
7323 // {
7324 // std::pair<uint64_t, unsigned> field_type_info =
7325 // ast->getTypeInfo(array->getElementType());
7326 //
7327 // char element_name[32];
7328 // ::snprintf (element_name, sizeof (element_name),
7329 // "%s[%u]", parent_name ? parent_name : "", idx);
7330 //
7331 // child_name.assign(element_name);
7332 // assert(field_type_info.first % 8 == 0);
7333 // child_byte_size = field_type_info.first / 8;
7334 // child_byte_offset = idx * child_byte_size;
7335 // return array->getElementType().getAsOpaquePtr();
7336 // }
7337 } break;
7338
7339 // case clang::Type::MemberPointerType:
7340 // {
7341 // MemberPointerType *mem_ptr_type =
7342 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7343 // clang::QualType pointee_type =
7344 // mem_ptr_type->getPointeeType();
7345 //
7346 // if (ClangASTContext::IsAggregateType
7347 // (pointee_type.getAsOpaquePtr()))
7348 // {
7349 // return GetIndexOfChildWithName (ast,
7350 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7351 // name);
7352 // }
7353 // }
7354 // break;
7355 //
7356 case clang::Type::LValueReference:
7357 case clang::Type::RValueReference: {
7358 const clang::ReferenceType *reference_type =
7359 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7360 clang::QualType pointee_type(reference_type->getPointeeType());
Alex Langfordbddab072019-08-13 19:40:36 +00007361 CompilerType pointee_clang_type(this, pointee_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007362
7363 if (pointee_clang_type.IsAggregateType()) {
7364 return pointee_clang_type.GetIndexOfChildMemberWithName(
7365 name, omit_empty_base_classes, child_indexes);
7366 }
7367 } break;
7368
7369 case clang::Type::Pointer: {
7370 CompilerType pointee_clang_type(GetPointeeType(type));
7371
7372 if (pointee_clang_type.IsAggregateType()) {
7373 return pointee_clang_type.GetIndexOfChildMemberWithName(
7374 name, omit_empty_base_classes, child_indexes);
7375 }
7376 } break;
7377
7378 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00007379 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
7380 ->getDecl()
7381 ->getUnderlyingType()
7382 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007383 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7384 child_indexes);
7385
7386 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00007387 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
7388 ->getDeducedType()
7389 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007390 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7391 child_indexes);
7392
7393 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00007394 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
7395 ->getNamedType()
7396 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007397 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7398 child_indexes);
7399
7400 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00007401 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
7402 ->desugar()
7403 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007404 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7405 child_indexes);
7406
7407 default:
7408 break;
7409 }
7410 }
7411 return 0;
7412}
Greg Claytond8d4a572015-08-11 21:38:15 +00007413
7414// Get the index of the child of "clang_type" whose name matches. This function
7415// doesn't descend into the children, but only looks one level deep and name
7416// matches can include base class names.
7417
7418uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007419ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7420 const char *name,
7421 bool omit_empty_base_classes) {
7422 if (type && name && name[0]) {
7423 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007424
Kate Stoneb9c1b512016-09-06 20:57:50 +00007425 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7426
7427 switch (type_class) {
7428 case clang::Type::Record:
7429 if (GetCompleteType(type)) {
7430 const clang::RecordType *record_type =
7431 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7432 const clang::RecordDecl *record_decl = record_type->getDecl();
7433
7434 assert(record_decl);
7435 uint32_t child_idx = 0;
7436
7437 const clang::CXXRecordDecl *cxx_record_decl =
7438 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7439
7440 if (cxx_record_decl) {
7441 clang::CXXRecordDecl::base_class_const_iterator base_class,
7442 base_class_end;
7443 for (base_class = cxx_record_decl->bases_begin(),
7444 base_class_end = cxx_record_decl->bases_end();
7445 base_class != base_class_end; ++base_class) {
7446 // Skip empty base classes
7447 clang::CXXRecordDecl *base_class_decl =
7448 llvm::cast<clang::CXXRecordDecl>(
7449 base_class->getType()
7450 ->getAs<clang::RecordType>()
7451 ->getDecl());
7452 if (omit_empty_base_classes &&
Jonas Devliegherea6682a42018-12-15 00:15:33 +00007453 !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00007454 continue;
7455
Alex Langfordbddab072019-08-13 19:40:36 +00007456 CompilerType base_class_clang_type(
7457 this, base_class->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007458 std::string base_class_type_name(
7459 base_class_clang_type.GetTypeName().AsCString(""));
Jonas Devlieghere8d20cfd2018-12-21 22:46:10 +00007460 if (base_class_type_name == name)
Kate Stoneb9c1b512016-09-06 20:57:50 +00007461 return child_idx;
7462 ++child_idx;
7463 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007464 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007465
Kate Stoneb9c1b512016-09-06 20:57:50 +00007466 // Try and find a field that matches NAME
7467 clang::RecordDecl::field_iterator field, field_end;
7468 llvm::StringRef name_sref(name);
7469 for (field = record_decl->field_begin(),
7470 field_end = record_decl->field_end();
7471 field != field_end; ++field, ++child_idx) {
7472 if (field->getName().equals(name_sref))
7473 return child_idx;
7474 }
7475 }
7476 break;
7477
7478 case clang::Type::ObjCObject:
7479 case clang::Type::ObjCInterface:
7480 if (GetCompleteType(type)) {
7481 llvm::StringRef name_sref(name);
7482 const clang::ObjCObjectType *objc_class_type =
7483 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7484 assert(objc_class_type);
7485 if (objc_class_type) {
7486 uint32_t child_idx = 0;
7487 clang::ObjCInterfaceDecl *class_interface_decl =
7488 objc_class_type->getInterface();
7489
7490 if (class_interface_decl) {
7491 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7492 ivar_end = class_interface_decl->ivar_end();
7493 clang::ObjCInterfaceDecl *superclass_interface_decl =
7494 class_interface_decl->getSuperClass();
7495
7496 for (ivar_pos = class_interface_decl->ivar_begin();
7497 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7498 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7499
7500 if (ivar_decl->getName().equals(name_sref)) {
7501 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7502 (omit_empty_base_classes &&
7503 ObjCDeclHasIVars(superclass_interface_decl, true)))
7504 ++child_idx;
7505
7506 return child_idx;
7507 }
7508 }
7509
7510 if (superclass_interface_decl) {
7511 if (superclass_interface_decl->getName().equals(name_sref))
7512 return 0;
7513 }
7514 }
7515 }
7516 }
7517 break;
7518
7519 case clang::Type::ObjCObjectPointer: {
7520 CompilerType pointee_clang_type(
Alex Langfordbddab072019-08-13 19:40:36 +00007521 this, llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7522 ->getPointeeType()
7523 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007524 return pointee_clang_type.GetIndexOfChildWithName(
7525 name, omit_empty_base_classes);
7526 } break;
7527
7528 case clang::Type::ConstantArray: {
7529 // const clang::ConstantArrayType *array =
7530 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7531 // const uint64_t element_count =
7532 // array->getSize().getLimitedValue();
7533 //
7534 // if (idx < element_count)
7535 // {
7536 // std::pair<uint64_t, unsigned> field_type_info =
7537 // ast->getTypeInfo(array->getElementType());
7538 //
7539 // char element_name[32];
7540 // ::snprintf (element_name, sizeof (element_name),
7541 // "%s[%u]", parent_name ? parent_name : "", idx);
7542 //
7543 // child_name.assign(element_name);
7544 // assert(field_type_info.first % 8 == 0);
7545 // child_byte_size = field_type_info.first / 8;
7546 // child_byte_offset = idx * child_byte_size;
7547 // return array->getElementType().getAsOpaquePtr();
7548 // }
7549 } break;
7550
7551 // case clang::Type::MemberPointerType:
7552 // {
7553 // MemberPointerType *mem_ptr_type =
7554 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7555 // clang::QualType pointee_type =
7556 // mem_ptr_type->getPointeeType();
7557 //
7558 // if (ClangASTContext::IsAggregateType
7559 // (pointee_type.getAsOpaquePtr()))
7560 // {
7561 // return GetIndexOfChildWithName (ast,
7562 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7563 // name);
7564 // }
7565 // }
7566 // break;
7567 //
7568 case clang::Type::LValueReference:
7569 case clang::Type::RValueReference: {
7570 const clang::ReferenceType *reference_type =
7571 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
Alex Langfordbddab072019-08-13 19:40:36 +00007572 CompilerType pointee_type(
7573 this, reference_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007574
7575 if (pointee_type.IsAggregateType()) {
7576 return pointee_type.GetIndexOfChildWithName(name,
7577 omit_empty_base_classes);
7578 }
7579 } break;
7580
7581 case clang::Type::Pointer: {
7582 const clang::PointerType *pointer_type =
7583 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
Alex Langfordbddab072019-08-13 19:40:36 +00007584 CompilerType pointee_type(
7585 this, pointer_type->getPointeeType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007586
7587 if (pointee_type.IsAggregateType()) {
7588 return pointee_type.GetIndexOfChildWithName(name,
7589 omit_empty_base_classes);
7590 } else {
7591 // if (parent_name)
7592 // {
7593 // child_name.assign(1, '*');
7594 // child_name += parent_name;
7595 // }
7596 //
7597 // // We have a pointer to an simple type
7598 // if (idx == 0)
7599 // {
7600 // std::pair<uint64_t, unsigned> clang_type_info
7601 // = ast->getTypeInfo(pointee_type);
7602 // assert(clang_type_info.first % 8 == 0);
7603 // child_byte_size = clang_type_info.first / 8;
7604 // child_byte_offset = 0;
7605 // return pointee_type.getAsOpaquePtr();
7606 // }
7607 }
7608 } break;
7609
7610 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00007611 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
7612 ->getDeducedType()
7613 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007614 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7615
7616 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00007617 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
7618 ->getNamedType()
7619 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007620 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7621
7622 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00007623 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
7624 ->desugar()
7625 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007626 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7627
7628 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00007629 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
7630 ->getDecl()
7631 ->getUnderlyingType()
7632 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007633 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7634
7635 default:
7636 break;
7637 }
7638 }
7639 return UINT32_MAX;
7640}
Greg Claytond8d4a572015-08-11 21:38:15 +00007641
7642size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007643ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7644 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007645 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007646
Kate Stoneb9c1b512016-09-06 20:57:50 +00007647 clang::QualType qual_type(GetCanonicalQualType(type));
7648 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7649 switch (type_class) {
7650 case clang::Type::Record:
7651 if (GetCompleteType(type)) {
7652 const clang::CXXRecordDecl *cxx_record_decl =
7653 qual_type->getAsCXXRecordDecl();
7654 if (cxx_record_decl) {
7655 const clang::ClassTemplateSpecializationDecl *template_decl =
7656 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7657 cxx_record_decl);
7658 if (template_decl)
7659 return template_decl->getTemplateArgs().size();
7660 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007661 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007662 break;
7663
7664 case clang::Type::Typedef:
Alex Langfordbddab072019-08-13 19:40:36 +00007665 return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type)
7666 ->getDecl()
7667 ->getUnderlyingType()
7668 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007669 .GetNumTemplateArguments();
7670
7671 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00007672 return CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
7673 ->getDeducedType()
7674 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007675 .GetNumTemplateArguments();
7676
7677 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00007678 return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
7679 ->getNamedType()
7680 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007681 .GetNumTemplateArguments();
7682
7683 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00007684 return CompilerType(this, llvm::cast<clang::ParenType>(qual_type)
7685 ->desugar()
7686 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00007687 .GetNumTemplateArguments();
7688
7689 default:
7690 break;
7691 }
7692
7693 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007694}
7695
Pavel Labath769b21e2017-11-13 14:26:21 +00007696const clang::ClassTemplateSpecializationDecl *
7697ClangASTContext::GetAsTemplateSpecialization(
7698 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007699 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007700 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007701
7702 clang::QualType qual_type(GetCanonicalQualType(type));
7703 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7704 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007705 case clang::Type::Record: {
7706 if (! GetCompleteType(type))
7707 return nullptr;
7708 const clang::CXXRecordDecl *cxx_record_decl =
7709 qual_type->getAsCXXRecordDecl();
7710 if (!cxx_record_decl)
7711 return nullptr;
7712 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7713 cxx_record_decl);
7714 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007715
7716 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007717 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7718 ->getDecl()
7719 ->getUnderlyingType()
7720 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007721
7722 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007723 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7724 ->getDeducedType()
7725 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007726
7727 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007728 return GetAsTemplateSpecialization(
7729 llvm::cast<clang::ElaboratedType>(qual_type)
7730 ->getNamedType()
7731 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007732
7733 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007734 return GetAsTemplateSpecialization(
7735 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007736
7737 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007738 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007739 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007740}
7741
7742lldb::TemplateArgumentKind
7743ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7744 size_t arg_idx) {
7745 const clang::ClassTemplateSpecializationDecl *template_decl =
7746 GetAsTemplateSpecialization(type);
7747 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7748 return eTemplateArgumentKindNull;
7749
7750 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7751 case clang::TemplateArgument::Null:
7752 return eTemplateArgumentKindNull;
7753
7754 case clang::TemplateArgument::NullPtr:
7755 return eTemplateArgumentKindNullPtr;
7756
7757 case clang::TemplateArgument::Type:
7758 return eTemplateArgumentKindType;
7759
7760 case clang::TemplateArgument::Declaration:
7761 return eTemplateArgumentKindDeclaration;
7762
7763 case clang::TemplateArgument::Integral:
7764 return eTemplateArgumentKindIntegral;
7765
7766 case clang::TemplateArgument::Template:
7767 return eTemplateArgumentKindTemplate;
7768
7769 case clang::TemplateArgument::TemplateExpansion:
7770 return eTemplateArgumentKindTemplateExpansion;
7771
7772 case clang::TemplateArgument::Expression:
7773 return eTemplateArgumentKindExpression;
7774
7775 case clang::TemplateArgument::Pack:
7776 return eTemplateArgumentKindPack;
7777 }
7778 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7779}
7780
7781CompilerType
7782ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7783 size_t idx) {
7784 const clang::ClassTemplateSpecializationDecl *template_decl =
7785 GetAsTemplateSpecialization(type);
7786 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7787 return CompilerType();
7788
7789 const clang::TemplateArgument &template_arg =
7790 template_decl->getTemplateArgs()[idx];
7791 if (template_arg.getKind() != clang::TemplateArgument::Type)
7792 return CompilerType();
7793
Alex Langfordbddab072019-08-13 19:40:36 +00007794 return CompilerType(this, template_arg.getAsType().getAsOpaquePtr());
Pavel Labath769b21e2017-11-13 14:26:21 +00007795}
7796
Adrian Prantl2f1fa7a2019-01-15 21:04:19 +00007797Optional<CompilerType::IntegralTemplateArgument>
Pavel Labath769b21e2017-11-13 14:26:21 +00007798ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7799 size_t idx) {
7800 const clang::ClassTemplateSpecializationDecl *template_decl =
7801 GetAsTemplateSpecialization(type);
7802 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
Pavel Labathf59056f2017-11-30 10:16:54 +00007803 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007804
7805 const clang::TemplateArgument &template_arg =
7806 template_decl->getTemplateArgs()[idx];
7807 if (template_arg.getKind() != clang::TemplateArgument::Integral)
Pavel Labathf59056f2017-11-30 10:16:54 +00007808 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007809
Alex Langfordbddab072019-08-13 19:40:36 +00007810 return {
7811 {template_arg.getAsIntegral(),
7812 CompilerType(this, template_arg.getIntegralType().getAsOpaquePtr())}};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007813}
7814
Kate Stoneb9c1b512016-09-06 20:57:50 +00007815CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7816 if (type)
7817 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7818 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007819}
7820
Kate Stoneb9c1b512016-09-06 20:57:50 +00007821clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7822 const clang::EnumType *enutype =
7823 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7824 if (enutype)
7825 return enutype->getDecl();
Konrad Kleine248a1302019-05-23 11:14:47 +00007826 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007827}
7828
7829clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7830 const clang::RecordType *record_type =
7831 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7832 if (record_type)
7833 return record_type->getDecl();
7834 return nullptr;
7835}
7836
7837clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
Zachary Turner1639c6b2018-12-17 16:15:13 +00007838 return ClangUtil::GetAsTagDecl(type);
Greg Claytone6b36cd2015-12-08 01:02:08 +00007839}
7840
Aleksandr Urakov709426b2018-09-10 08:08:43 +00007841clang::TypedefNameDecl *
7842ClangASTContext::GetAsTypedefDecl(const CompilerType &type) {
7843 const clang::TypedefType *typedef_type =
7844 llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type));
7845 if (typedef_type)
7846 return typedef_type->getDecl();
7847 return nullptr;
7848}
7849
Greg Claytond8d4a572015-08-11 21:38:15 +00007850clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007851ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7852 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007853}
7854
7855clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007856ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7857 const clang::ObjCObjectType *objc_class_type =
7858 llvm::dyn_cast<clang::ObjCObjectType>(
7859 ClangUtil::GetCanonicalQualType(type));
7860 if (objc_class_type)
7861 return objc_class_type->getInterface();
7862 return nullptr;
7863}
7864
7865clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007866 const CompilerType &type, llvm::StringRef name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00007867 const CompilerType &field_clang_type, AccessType access,
7868 uint32_t bitfield_bit_size) {
7869 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007870 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007871 ClangASTContext *ast =
7872 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7873 if (!ast)
7874 return nullptr;
7875 clang::ASTContext *clang_ast = ast->getASTContext();
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007876 clang::IdentifierInfo *ident = nullptr;
7877 if (!name.empty())
7878 ident = &clang_ast->Idents.get(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00007879
7880 clang::FieldDecl *field = nullptr;
7881
7882 clang::Expr *bit_width = nullptr;
7883 if (bitfield_bit_size != 0) {
7884 llvm::APInt bitfield_bit_size_apint(
7885 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7886 bit_width = new (*clang_ast)
7887 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7888 clang_ast->IntTy, clang::SourceLocation());
7889 }
7890
7891 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7892 if (record_decl) {
7893 field = clang::FieldDecl::Create(
7894 *clang_ast, record_decl, clang::SourceLocation(),
7895 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007896 ident, // Identifier
7897 ClangUtil::GetQualType(field_clang_type), // Field type
7898 nullptr, // TInfo *
7899 bit_width, // BitWidth
7900 false, // Mutable
7901 clang::ICIS_NoInit); // HasInit
Kate Stoneb9c1b512016-09-06 20:57:50 +00007902
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007903 if (name.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +00007904 // Determine whether this field corresponds to an anonymous struct or
7905 // union.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007906 if (const clang::TagType *TagT =
7907 field->getType()->getAs<clang::TagType>()) {
7908 if (clang::RecordDecl *Rec =
7909 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7910 if (!Rec->getDeclName()) {
7911 Rec->setAnonymousStructOrUnion(true);
7912 field->setImplicit();
7913 }
7914 }
7915 }
7916
7917 if (field) {
7918 field->setAccess(
7919 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7920
7921 record_decl->addDecl(field);
7922
7923#ifdef LLDB_CONFIGURATION_DEBUG
7924 VerifyDecl(field);
7925#endif
7926 }
7927 } else {
7928 clang::ObjCInterfaceDecl *class_interface_decl =
7929 ast->GetAsObjCInterfaceDecl(type);
7930
7931 if (class_interface_decl) {
7932 const bool is_synthesized = false;
7933
7934 field_clang_type.GetCompleteType();
7935
7936 field = clang::ObjCIvarDecl::Create(
7937 *clang_ast, class_interface_decl, clang::SourceLocation(),
7938 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007939 ident, // Identifier
7940 ClangUtil::GetQualType(field_clang_type), // Field type
7941 nullptr, // TypeSourceInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007942 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7943 is_synthesized);
7944
7945 if (field) {
7946 class_interface_decl->addDecl(field);
7947
7948#ifdef LLDB_CONFIGURATION_DEBUG
7949 VerifyDecl(field);
7950#endif
7951 }
7952 }
7953 }
7954 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007955}
7956
Kate Stoneb9c1b512016-09-06 20:57:50 +00007957void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7958 if (!type)
7959 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007960
Kate Stoneb9c1b512016-09-06 20:57:50 +00007961 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7962 if (!ast)
7963 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007964
Kate Stoneb9c1b512016-09-06 20:57:50 +00007965 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007966
Kate Stoneb9c1b512016-09-06 20:57:50 +00007967 if (!record_decl)
7968 return;
7969
7970 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7971
7972 IndirectFieldVector indirect_fields;
7973 clang::RecordDecl::field_iterator field_pos;
7974 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7975 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7976 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7977 last_field_pos = field_pos++) {
7978 if (field_pos->isAnonymousStructOrUnion()) {
7979 clang::QualType field_qual_type = field_pos->getType();
7980
7981 const clang::RecordType *field_record_type =
7982 field_qual_type->getAs<clang::RecordType>();
7983
7984 if (!field_record_type)
7985 continue;
7986
7987 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7988
7989 if (!field_record_decl)
7990 continue;
7991
7992 for (clang::RecordDecl::decl_iterator
7993 di = field_record_decl->decls_begin(),
7994 de = field_record_decl->decls_end();
7995 di != de; ++di) {
7996 if (clang::FieldDecl *nested_field_decl =
7997 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7998 clang::NamedDecl **chain =
7999 new (*ast->getASTContext()) clang::NamedDecl *[2];
8000 chain[0] = *field_pos;
8001 chain[1] = nested_field_decl;
8002 clang::IndirectFieldDecl *indirect_field =
8003 clang::IndirectFieldDecl::Create(
8004 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8005 nested_field_decl->getIdentifier(),
8006 nested_field_decl->getType(), {chain, 2});
8007
8008 indirect_field->setImplicit();
8009
8010 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8011 field_pos->getAccess(), nested_field_decl->getAccess()));
8012
8013 indirect_fields.push_back(indirect_field);
8014 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
8015 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
8016 size_t nested_chain_size =
8017 nested_indirect_field_decl->getChainingSize();
8018 clang::NamedDecl **chain = new (*ast->getASTContext())
8019 clang::NamedDecl *[nested_chain_size + 1];
8020 chain[0] = *field_pos;
8021
8022 int chain_index = 1;
8023 for (clang::IndirectFieldDecl::chain_iterator
8024 nci = nested_indirect_field_decl->chain_begin(),
8025 nce = nested_indirect_field_decl->chain_end();
8026 nci < nce; ++nci) {
8027 chain[chain_index] = *nci;
8028 chain_index++;
8029 }
8030
8031 clang::IndirectFieldDecl *indirect_field =
8032 clang::IndirectFieldDecl::Create(
8033 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8034 nested_indirect_field_decl->getIdentifier(),
8035 nested_indirect_field_decl->getType(),
8036 {chain, nested_chain_size + 1});
8037
8038 indirect_field->setImplicit();
8039
8040 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8041 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
8042
8043 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00008044 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008045 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008046 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008047 }
8048
Adrian Prantl05097242018-04-30 16:49:04 +00008049 // Check the last field to see if it has an incomplete array type as its last
8050 // member and if it does, the tell the record decl about it
Kate Stoneb9c1b512016-09-06 20:57:50 +00008051 if (last_field_pos != field_end_pos) {
8052 if (last_field_pos->getType()->isIncompleteArrayType())
8053 record_decl->hasFlexibleArrayMember();
8054 }
8055
8056 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
8057 ife = indirect_fields.end();
8058 ifi < ife; ++ifi) {
8059 record_decl->addDecl(*ifi);
8060 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008061}
8062
Kate Stoneb9c1b512016-09-06 20:57:50 +00008063void ClangASTContext::SetIsPacked(const CompilerType &type) {
8064 if (type) {
8065 ClangASTContext *ast =
8066 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8067 if (ast) {
8068 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
8069
8070 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00008071 return;
8072
Kate Stoneb9c1b512016-09-06 20:57:50 +00008073 record_decl->addAttr(
8074 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00008075 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008076 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008077}
8078
Kate Stoneb9c1b512016-09-06 20:57:50 +00008079clang::VarDecl *ClangASTContext::AddVariableToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008080 const CompilerType &type, llvm::StringRef name,
8081 const CompilerType &var_type, AccessType access) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00008082 if (!type.IsValid() || !var_type.IsValid())
8083 return nullptr;
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008084
Kate Stoneb9c1b512016-09-06 20:57:50 +00008085 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8086 if (!ast)
8087 return nullptr;
8088
8089 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008090 if (!record_decl)
8091 return nullptr;
8092
8093 clang::VarDecl *var_decl = nullptr;
8094 clang::IdentifierInfo *ident = nullptr;
8095 if (!name.empty())
8096 ident = &ast->getASTContext()->Idents.get(name);
8097
8098 var_decl = clang::VarDecl::Create(
8099 *ast->getASTContext(), // ASTContext &
8100 record_decl, // DeclContext *
8101 clang::SourceLocation(), // clang::SourceLocation StartLoc
8102 clang::SourceLocation(), // clang::SourceLocation IdLoc
8103 ident, // clang::IdentifierInfo *
8104 ClangUtil::GetQualType(var_type), // Variable clang::QualType
8105 nullptr, // TypeSourceInfo *
8106 clang::SC_Static); // StorageClass
8107 if (!var_decl)
8108 return nullptr;
8109
8110 var_decl->setAccess(
8111 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
8112 record_decl->addDecl(var_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008113
Greg Claytond8d4a572015-08-11 21:38:15 +00008114#ifdef LLDB_CONFIGURATION_DEBUG
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008115 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008116#endif
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008117
Kate Stoneb9c1b512016-09-06 20:57:50 +00008118 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008119}
8120
Kate Stoneb9c1b512016-09-06 20:57:50 +00008121clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
Davide Italiano675767a2018-03-27 19:40:50 +00008122 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008123 const CompilerType &method_clang_type, lldb::AccessType access,
8124 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
8125 bool is_attr_used, bool is_artificial) {
8126 if (!type || !method_clang_type.IsValid() || name == nullptr ||
8127 name[0] == '\0')
8128 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008129
Kate Stoneb9c1b512016-09-06 20:57:50 +00008130 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008131
Kate Stoneb9c1b512016-09-06 20:57:50 +00008132 clang::CXXRecordDecl *cxx_record_decl =
8133 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008134
Kate Stoneb9c1b512016-09-06 20:57:50 +00008135 if (cxx_record_decl == nullptr)
8136 return nullptr;
8137
8138 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8139
8140 clang::CXXMethodDecl *cxx_method_decl = nullptr;
8141
8142 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
8143
8144 const clang::FunctionType *function_type =
8145 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
8146
8147 if (function_type == nullptr)
8148 return nullptr;
8149
8150 const clang::FunctionProtoType *method_function_prototype(
8151 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
8152
8153 if (!method_function_prototype)
8154 return nullptr;
8155
8156 unsigned int num_params = method_function_prototype->getNumParams();
8157
8158 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
8159 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
8160
8161 if (is_artificial)
8162 return nullptr; // skip everything artificial
8163
Richard Smith36851a62019-05-09 04:40:57 +00008164 const clang::ExplicitSpecifier explicit_spec(
8165 nullptr /*expr*/, is_explicit
8166 ? clang::ExplicitSpecKind::ResolvedTrue
8167 : clang::ExplicitSpecKind::ResolvedFalse);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008168 if (name[0] == '~') {
8169 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
8170 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8171 clang::DeclarationNameInfo(
8172 getASTContext()->DeclarationNames.getCXXDestructorName(
8173 getASTContext()->getCanonicalType(record_qual_type)),
8174 clang::SourceLocation()),
Raphael Isemann15695cd2019-09-23 06:59:35 +00008175 method_qual_type, nullptr, is_inline, is_artificial,
8176 ConstexprSpecKind::CSK_unspecified);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008177 cxx_method_decl = cxx_dtor_decl;
8178 } else if (decl_name == cxx_record_decl->getDeclName()) {
8179 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
8180 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8181 clang::DeclarationNameInfo(
8182 getASTContext()->DeclarationNames.getCXXConstructorName(
8183 getASTContext()->getCanonicalType(record_qual_type)),
8184 clang::SourceLocation()),
8185 method_qual_type,
8186 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008187 explicit_spec, is_inline, is_artificial, CSK_unspecified);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008188 cxx_method_decl = cxx_ctor_decl;
8189 } else {
8190 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8191 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8192
8193 if (IsOperator(name, op_kind)) {
8194 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
Adrian Prantl05097242018-04-30 16:49:04 +00008195 // Check the number of operator parameters. Sometimes we have seen bad
8196 // DWARF that doesn't correctly describe operators and if we try to
8197 // create a method and add it to the class, clang will assert and
8198 // crash, so we need to make sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008199 const bool is_method = true;
8200 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8201 is_method, op_kind, num_params))
8202 return nullptr;
8203 cxx_method_decl = clang::CXXMethodDecl::Create(
8204 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8205 clang::DeclarationNameInfo(
8206 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8207 clang::SourceLocation()),
8208 method_qual_type,
8209 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008210 SC, is_inline, CSK_unspecified, clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008211 } else if (num_params == 0) {
8212 // Conversion operators don't take params...
8213 cxx_method_decl = clang::CXXConversionDecl::Create(
8214 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8215 clang::DeclarationNameInfo(
8216 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8217 getASTContext()->getCanonicalType(
8218 function_type->getReturnType())),
8219 clang::SourceLocation()),
8220 method_qual_type,
8221 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008222 is_inline, explicit_spec, CSK_unspecified,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008223 clang::SourceLocation());
8224 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008225 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008226
8227 if (cxx_method_decl == nullptr) {
8228 cxx_method_decl = clang::CXXMethodDecl::Create(
8229 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8230 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8231 method_qual_type,
8232 nullptr, // TypeSourceInfo *
Gauthier Harnisch796ed032019-06-14 08:56:20 +00008233 SC, is_inline, CSK_unspecified, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008234 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008235 }
8236
8237 clang::AccessSpecifier access_specifier =
8238 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8239
8240 cxx_method_decl->setAccess(access_specifier);
8241 cxx_method_decl->setVirtualAsWritten(is_virtual);
8242
8243 if (is_attr_used)
8244 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8245
Konrad Kleine248a1302019-05-23 11:14:47 +00008246 if (mangled_name != nullptr) {
Vedant Kumarf6bc2512019-09-25 18:00:31 +00008247 cxx_method_decl->addAttr(clang::AsmLabelAttr::CreateImplicit(
8248 *getASTContext(), mangled_name, /*literal=*/false));
Davide Italiano675767a2018-03-27 19:40:50 +00008249 }
8250
Kate Stoneb9c1b512016-09-06 20:57:50 +00008251 // Populate the method decl with parameter decls
8252
8253 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8254
8255 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8256 params.push_back(clang::ParmVarDecl::Create(
8257 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8258 clang::SourceLocation(),
8259 nullptr, // anonymous
8260 method_function_prototype->getParamType(param_index), nullptr,
8261 clang::SC_None, nullptr));
8262 }
8263
8264 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8265
8266 cxx_record_decl->addDecl(cxx_method_decl);
8267
8268 // Sometimes the debug info will mention a constructor (default/copy/move),
8269 // destructor, or assignment operator (copy/move) but there won't be any
8270 // version of this in the code. So we check if the function was artificially
8271 // generated and if it is trivial and this lets the compiler/backend know
8272 // that it can inline the IR for these when it needs to and we can avoid a
8273 // "missing function" error when running expressions.
8274
8275 if (is_artificial) {
8276 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8277 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8278 (cxx_ctor_decl->isCopyConstructor() &&
8279 cxx_record_decl->hasTrivialCopyConstructor()) ||
8280 (cxx_ctor_decl->isMoveConstructor() &&
8281 cxx_record_decl->hasTrivialMoveConstructor()))) {
8282 cxx_ctor_decl->setDefaulted();
8283 cxx_ctor_decl->setTrivial(true);
8284 } else if (cxx_dtor_decl) {
8285 if (cxx_record_decl->hasTrivialDestructor()) {
8286 cxx_dtor_decl->setDefaulted();
8287 cxx_dtor_decl->setTrivial(true);
8288 }
8289 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8290 cxx_record_decl->hasTrivialCopyAssignment()) ||
8291 (cxx_method_decl->isMoveAssignmentOperator() &&
8292 cxx_record_decl->hasTrivialMoveAssignment())) {
8293 cxx_method_decl->setDefaulted();
8294 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008295 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008296 }
8297
Greg Claytond8d4a572015-08-11 21:38:15 +00008298#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008299 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008300#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008301
Kate Stoneb9c1b512016-09-06 20:57:50 +00008302 return cxx_method_decl;
8303}
Greg Claytond8d4a572015-08-11 21:38:15 +00008304
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00008305void ClangASTContext::AddMethodOverridesForCXXRecordType(
8306 lldb::opaque_compiler_type_t type) {
8307 if (auto *record = GetAsCXXRecordDecl(type))
8308 for (auto *method : record->methods())
8309 addOverridesForMethod(method);
8310}
8311
Greg Claytond8d4a572015-08-11 21:38:15 +00008312#pragma mark C++ Base Classes
8313
Zachary Turner970f38e2018-10-25 20:44:56 +00008314std::unique_ptr<clang::CXXBaseSpecifier>
Kate Stoneb9c1b512016-09-06 20:57:50 +00008315ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8316 AccessType access, bool is_virtual,
8317 bool base_of_class) {
Zachary Turner970f38e2018-10-25 20:44:56 +00008318 if (!type)
8319 return nullptr;
8320
Jonas Devliegherea8f3ae72019-08-14 22:19:23 +00008321 return std::make_unique<clang::CXXBaseSpecifier>(
Zachary Turner970f38e2018-10-25 20:44:56 +00008322 clang::SourceRange(), is_virtual, base_of_class,
8323 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8324 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8325 clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008326}
8327
Zachary Turner970f38e2018-10-25 20:44:56 +00008328bool ClangASTContext::TransferBaseClasses(
Kate Stoneb9c1b512016-09-06 20:57:50 +00008329 lldb::opaque_compiler_type_t type,
Zachary Turner970f38e2018-10-25 20:44:56 +00008330 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) {
8331 if (!type)
8332 return false;
8333 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8334 if (!cxx_record_decl)
8335 return false;
8336 std::vector<clang::CXXBaseSpecifier *> raw_bases;
8337 raw_bases.reserve(bases.size());
8338
8339 // Clang will make a copy of them, so it's ok that we pass pointers that we're
8340 // about to destroy.
8341 for (auto &b : bases)
8342 raw_bases.push_back(b.get());
8343 cxx_record_decl->setBases(raw_bases.data(), raw_bases.size());
8344 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008345}
8346
8347bool ClangASTContext::SetObjCSuperClass(
8348 const CompilerType &type, const CompilerType &superclass_clang_type) {
8349 ClangASTContext *ast =
8350 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8351 if (!ast)
8352 return false;
8353 clang::ASTContext *clang_ast = ast->getASTContext();
8354
8355 if (type && superclass_clang_type.IsValid() &&
8356 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8357 clang::ObjCInterfaceDecl *class_interface_decl =
8358 GetAsObjCInterfaceDecl(type);
8359 clang::ObjCInterfaceDecl *super_interface_decl =
8360 GetAsObjCInterfaceDecl(superclass_clang_type);
8361 if (class_interface_decl && super_interface_decl) {
8362 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8363 clang_ast->getObjCInterfaceType(super_interface_decl)));
8364 return true;
8365 }
8366 }
8367 return false;
8368}
8369
8370bool ClangASTContext::AddObjCClassProperty(
8371 const CompilerType &type, const char *property_name,
8372 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8373 const char *property_setter_name, const char *property_getter_name,
8374 uint32_t property_attributes, ClangASTMetadata *metadata) {
8375 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8376 property_name[0] == '\0')
8377 return false;
8378 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8379 if (!ast)
8380 return false;
8381 clang::ASTContext *clang_ast = ast->getASTContext();
8382
8383 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8384
8385 if (class_interface_decl) {
8386 CompilerType property_clang_type_to_access;
8387
8388 if (property_clang_type.IsValid())
8389 property_clang_type_to_access = property_clang_type;
8390 else if (ivar_decl)
8391 property_clang_type_to_access =
Alex Langfordbddab072019-08-13 19:40:36 +00008392 CompilerType(ast, ivar_decl->getType().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008393
8394 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8395 clang::TypeSourceInfo *prop_type_source;
8396 if (ivar_decl)
8397 prop_type_source =
8398 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8399 else
8400 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8401 ClangUtil::GetQualType(property_clang_type));
8402
8403 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8404 *clang_ast, class_interface_decl,
8405 clang::SourceLocation(), // Source Location
8406 &clang_ast->Idents.get(property_name),
8407 clang::SourceLocation(), // Source Location for AT
8408 clang::SourceLocation(), // Source location for (
8409 ivar_decl ? ivar_decl->getType()
8410 : ClangUtil::GetQualType(property_clang_type),
8411 prop_type_source);
8412
8413 if (property_decl) {
8414 if (metadata)
8415 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8416
8417 class_interface_decl->addDecl(property_decl);
8418
8419 clang::Selector setter_sel, getter_sel;
8420
8421 if (property_setter_name != nullptr) {
8422 std::string property_setter_no_colon(
8423 property_setter_name, strlen(property_setter_name) - 1);
8424 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008425 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008426 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8427 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8428 std::string setter_sel_string("set");
8429 setter_sel_string.push_back(::toupper(property_name[0]));
8430 setter_sel_string.append(&property_name[1]);
8431 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008432 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008433 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8434 }
8435 property_decl->setSetterName(setter_sel);
8436 property_decl->setPropertyAttributes(
8437 clang::ObjCPropertyDecl::OBJC_PR_setter);
8438
8439 if (property_getter_name != nullptr) {
8440 clang::IdentifierInfo *getter_ident =
8441 &clang_ast->Idents.get(property_getter_name);
8442 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8443 } else {
8444 clang::IdentifierInfo *getter_ident =
8445 &clang_ast->Idents.get(property_name);
8446 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8447 }
8448 property_decl->setGetterName(getter_sel);
8449 property_decl->setPropertyAttributes(
8450 clang::ObjCPropertyDecl::OBJC_PR_getter);
8451
8452 if (ivar_decl)
8453 property_decl->setPropertyIvarDecl(ivar_decl);
8454
8455 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8456 property_decl->setPropertyAttributes(
8457 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8458 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8459 property_decl->setPropertyAttributes(
8460 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8461 if (property_attributes & DW_APPLE_PROPERTY_assign)
8462 property_decl->setPropertyAttributes(
8463 clang::ObjCPropertyDecl::OBJC_PR_assign);
8464 if (property_attributes & DW_APPLE_PROPERTY_retain)
8465 property_decl->setPropertyAttributes(
8466 clang::ObjCPropertyDecl::OBJC_PR_retain);
8467 if (property_attributes & DW_APPLE_PROPERTY_copy)
8468 property_decl->setPropertyAttributes(
8469 clang::ObjCPropertyDecl::OBJC_PR_copy);
8470 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8471 property_decl->setPropertyAttributes(
8472 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8473 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8474 property_decl->setPropertyAttributes(
8475 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8476 if (property_attributes &
8477 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8478 property_decl->setPropertyAttributes(
8479 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8480 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8481 property_decl->setPropertyAttributes(
8482 clang::ObjCPropertyDecl::OBJC_PR_class);
8483
8484 const bool isInstance =
8485 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8486
8487 if (!getter_sel.isNull() &&
8488 !(isInstance
8489 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8490 : class_interface_decl->lookupClassMethod(getter_sel))) {
8491 const bool isVariadic = false;
Adrian Prantl454acae2019-11-08 08:58:50 -08008492 const bool isPropertyAccessor = false;
8493 const bool isSynthesizedAccessorStub = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008494 const bool isImplicitlyDeclared = true;
8495 const bool isDefined = false;
8496 const clang::ObjCMethodDecl::ImplementationControl impControl =
8497 clang::ObjCMethodDecl::None;
8498 const bool HasRelatedResultType = false;
8499
8500 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8501 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8502 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8503 nullptr, class_interface_decl, isInstance, isVariadic,
Adrian Prantl454acae2019-11-08 08:58:50 -08008504 isPropertyAccessor, isSynthesizedAccessorStub,
8505 isImplicitlyDeclared, isDefined, impControl,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008506 HasRelatedResultType);
8507
8508 if (getter && metadata)
8509 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8510
8511 if (getter) {
8512 getter->setMethodParams(*clang_ast,
8513 llvm::ArrayRef<clang::ParmVarDecl *>(),
8514 llvm::ArrayRef<clang::SourceLocation>());
8515
8516 class_interface_decl->addDecl(getter);
8517 }
8518 }
8519
8520 if (!setter_sel.isNull() &&
8521 !(isInstance
8522 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8523 : class_interface_decl->lookupClassMethod(setter_sel))) {
8524 clang::QualType result_type = clang_ast->VoidTy;
8525 const bool isVariadic = false;
Adrian Prantl454acae2019-11-08 08:58:50 -08008526 const bool isPropertyAccessor = true;
8527 const bool isSynthesizedAccessorStub = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008528 const bool isImplicitlyDeclared = true;
8529 const bool isDefined = false;
8530 const clang::ObjCMethodDecl::ImplementationControl impControl =
8531 clang::ObjCMethodDecl::None;
8532 const bool HasRelatedResultType = false;
8533
8534 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8535 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8536 setter_sel, result_type, nullptr, class_interface_decl,
Adrian Prantl454acae2019-11-08 08:58:50 -08008537 isInstance, isVariadic, isPropertyAccessor,
8538 isSynthesizedAccessorStub, isImplicitlyDeclared, isDefined,
8539 impControl, HasRelatedResultType);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008540
8541 if (setter && metadata)
8542 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8543
8544 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8545
8546 params.push_back(clang::ParmVarDecl::Create(
8547 *clang_ast, setter, clang::SourceLocation(),
8548 clang::SourceLocation(),
8549 nullptr, // anonymous
8550 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8551 clang::SC_Auto, nullptr));
8552
8553 if (setter) {
8554 setter->setMethodParams(
8555 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8556 llvm::ArrayRef<clang::SourceLocation>());
8557
8558 class_interface_decl->addDecl(setter);
8559 }
8560 }
8561
8562 return true;
8563 }
8564 }
8565 }
8566 return false;
8567}
8568
8569bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8570 bool check_superclass) {
8571 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8572 if (class_interface_decl)
8573 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8574 return false;
8575}
8576
8577clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8578 const CompilerType &type,
8579 const char *name, // the full symbol name as seen in the symbol table
8580 // (lldb::opaque_compiler_type_t type, "-[NString
8581 // stringWithCString:]")
8582 const CompilerType &method_clang_type, lldb::AccessType access,
8583 bool is_artificial, bool is_variadic) {
8584 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008585 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008586
Kate Stoneb9c1b512016-09-06 20:57:50 +00008587 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8588
8589 if (class_interface_decl == nullptr)
8590 return nullptr;
8591 ClangASTContext *lldb_ast =
8592 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8593 if (lldb_ast == nullptr)
8594 return nullptr;
8595 clang::ASTContext *ast = lldb_ast->getASTContext();
8596
8597 const char *selector_start = ::strchr(name, ' ');
8598 if (selector_start == nullptr)
8599 return nullptr;
8600
8601 selector_start++;
8602 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8603
8604 size_t len = 0;
8605 const char *start;
8606 // printf ("name = '%s'\n", name);
8607
8608 unsigned num_selectors_with_args = 0;
8609 for (start = selector_start; start && *start != '\0' && *start != ']';
8610 start += len) {
8611 len = ::strcspn(start, ":]");
8612 bool has_arg = (start[len] == ':');
8613 if (has_arg)
8614 ++num_selectors_with_args;
8615 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8616 if (has_arg)
8617 len += 1;
8618 }
8619
8620 if (selector_idents.size() == 0)
8621 return nullptr;
8622
8623 clang::Selector method_selector = ast->Selectors.getSelector(
8624 num_selectors_with_args ? selector_idents.size() : 0,
8625 selector_idents.data());
8626
8627 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8628
8629 // Populate the method decl with parameter decls
8630 const clang::Type *method_type(method_qual_type.getTypePtr());
8631
8632 if (method_type == nullptr)
8633 return nullptr;
8634
8635 const clang::FunctionProtoType *method_function_prototype(
8636 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8637
8638 if (!method_function_prototype)
8639 return nullptr;
8640
Adrian Prantl454acae2019-11-08 08:58:50 -08008641 const bool isInstance = (name[0] == '-');
8642 const bool isVariadic = false;
8643 const bool isPropertyAccessor = false;
8644 const bool isSynthesizedAccessorStub = false;
8645 /// Force this to true because we don't have source locations.
8646 const bool isImplicitlyDeclared = true;
8647 const bool isDefined = false;
8648 const clang::ObjCMethodDecl::ImplementationControl impControl =
Kate Stoneb9c1b512016-09-06 20:57:50 +00008649 clang::ObjCMethodDecl::None;
Adrian Prantl454acae2019-11-08 08:58:50 -08008650 const bool HasRelatedResultType = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008651
8652 const unsigned num_args = method_function_prototype->getNumParams();
8653
8654 if (num_args != num_selectors_with_args)
8655 return nullptr; // some debug information is corrupt. We are not going to
8656 // deal with it.
8657
8658 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8659 *ast,
8660 clang::SourceLocation(), // beginLoc,
8661 clang::SourceLocation(), // endLoc,
8662 method_selector, method_function_prototype->getReturnType(),
8663 nullptr, // TypeSourceInfo *ResultTInfo,
8664 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8665 ClangUtil::GetQualType(type)),
Adrian Prantl454acae2019-11-08 08:58:50 -08008666 isInstance, isVariadic, isPropertyAccessor, isSynthesizedAccessorStub,
8667 isImplicitlyDeclared, isDefined, impControl, HasRelatedResultType);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008668
8669 if (objc_method_decl == nullptr)
8670 return nullptr;
8671
8672 if (num_args > 0) {
8673 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8674
8675 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8676 params.push_back(clang::ParmVarDecl::Create(
8677 *ast, objc_method_decl, clang::SourceLocation(),
8678 clang::SourceLocation(),
8679 nullptr, // anonymous
8680 method_function_prototype->getParamType(param_index), nullptr,
8681 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008682 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008683
Kate Stoneb9c1b512016-09-06 20:57:50 +00008684 objc_method_decl->setMethodParams(
8685 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8686 llvm::ArrayRef<clang::SourceLocation>());
8687 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008688
Kate Stoneb9c1b512016-09-06 20:57:50 +00008689 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008690
Greg Claytond8d4a572015-08-11 21:38:15 +00008691#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008692 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008693#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008694
8695 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008696}
8697
Kate Stoneb9c1b512016-09-06 20:57:50 +00008698bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8699 bool has_extern) {
8700 if (!type)
8701 return false;
8702
8703 clang::QualType qual_type(GetCanonicalQualType(type));
8704
8705 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8706 switch (type_class) {
8707 case clang::Type::Record: {
8708 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8709 if (cxx_record_decl) {
8710 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8711 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8712 return true;
8713 }
8714 } break;
8715
8716 case clang::Type::Enum: {
8717 clang::EnumDecl *enum_decl =
8718 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8719 if (enum_decl) {
8720 enum_decl->setHasExternalLexicalStorage(has_extern);
8721 enum_decl->setHasExternalVisibleStorage(has_extern);
8722 return true;
8723 }
8724 } break;
8725
8726 case clang::Type::ObjCObject:
8727 case clang::Type::ObjCInterface: {
8728 const clang::ObjCObjectType *objc_class_type =
8729 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8730 assert(objc_class_type);
8731 if (objc_class_type) {
8732 clang::ObjCInterfaceDecl *class_interface_decl =
8733 objc_class_type->getInterface();
8734
8735 if (class_interface_decl) {
8736 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8737 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8738 return true;
8739 }
8740 }
8741 } break;
8742
8743 case clang::Type::Typedef:
8744 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8745 ->getDecl()
8746 ->getUnderlyingType()
8747 .getAsOpaquePtr(),
8748 has_extern);
8749
8750 case clang::Type::Auto:
8751 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8752 ->getDeducedType()
8753 .getAsOpaquePtr(),
8754 has_extern);
8755
8756 case clang::Type::Elaborated:
8757 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8758 ->getNamedType()
8759 .getAsOpaquePtr(),
8760 has_extern);
8761
8762 case clang::Type::Paren:
8763 return SetHasExternalStorage(
8764 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8765 has_extern);
8766
8767 default:
8768 break;
8769 }
8770 return false;
8771}
Greg Claytond8d4a572015-08-11 21:38:15 +00008772
8773#pragma mark TagDecl
8774
Kate Stoneb9c1b512016-09-06 20:57:50 +00008775bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8776 clang::QualType qual_type(ClangUtil::GetQualType(type));
8777 if (!qual_type.isNull()) {
8778 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8779 if (tag_type) {
8780 clang::TagDecl *tag_decl = tag_type->getDecl();
8781 if (tag_decl) {
8782 tag_decl->startDefinition();
8783 return true;
8784 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008785 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008786
8787 const clang::ObjCObjectType *object_type =
8788 qual_type->getAs<clang::ObjCObjectType>();
8789 if (object_type) {
8790 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8791 if (interface_decl) {
8792 interface_decl->startDefinition();
8793 return true;
8794 }
8795 }
8796 }
8797 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008798}
8799
Kate Stoneb9c1b512016-09-06 20:57:50 +00008800bool ClangASTContext::CompleteTagDeclarationDefinition(
8801 const CompilerType &type) {
8802 clang::QualType qual_type(ClangUtil::GetQualType(type));
8803 if (!qual_type.isNull()) {
8804 // Make sure we use the same methodology as
Adrian Prantl05097242018-04-30 16:49:04 +00008805 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end
8806 // the definition. Previously we were calling
Kate Stoneb9c1b512016-09-06 20:57:50 +00008807 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8808 if (tag_type) {
8809 clang::TagDecl *tag_decl = tag_type->getDecl();
8810 if (tag_decl) {
8811 clang::CXXRecordDecl *cxx_record_decl =
8812 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8813
8814 if (cxx_record_decl) {
8815 if (!cxx_record_decl->isCompleteDefinition())
8816 cxx_record_decl->completeDefinition();
8817 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8818 cxx_record_decl->setHasExternalLexicalStorage(false);
8819 cxx_record_decl->setHasExternalVisibleStorage(false);
8820 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008821 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008822 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008823 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008824
8825 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8826
8827 if (enutype) {
8828 clang::EnumDecl *enum_decl = enutype->getDecl();
8829
8830 if (enum_decl) {
8831 if (!enum_decl->isCompleteDefinition()) {
8832 ClangASTContext *lldb_ast =
8833 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8834 if (lldb_ast == nullptr)
8835 return false;
8836 clang::ASTContext *ast = lldb_ast->getASTContext();
8837
8838 /// TODO This really needs to be fixed.
8839
8840 QualType integer_type(enum_decl->getIntegerType());
8841 if (!integer_type.isNull()) {
8842 unsigned NumPositiveBits = 1;
8843 unsigned NumNegativeBits = 0;
8844
8845 clang::QualType promotion_qual_type;
8846 // If the enum integer type is less than an integer in bit width,
8847 // then we must promote it to an integer size.
8848 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8849 ast->getTypeSize(ast->IntTy)) {
8850 if (enum_decl->getIntegerType()->isSignedIntegerType())
8851 promotion_qual_type = ast->IntTy;
8852 else
8853 promotion_qual_type = ast->UnsignedIntTy;
8854 } else
8855 promotion_qual_type = enum_decl->getIntegerType();
8856
8857 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8858 promotion_qual_type, NumPositiveBits,
8859 NumNegativeBits);
8860 }
8861 }
8862 return true;
8863 }
8864 }
8865 }
8866 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008867}
8868
Aleksandr Urakov709426b2018-09-10 08:08:43 +00008869clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008870 const CompilerType &enum_type, const Declaration &decl, const char *name,
Zachary Turner1639c6b2018-12-17 16:15:13 +00008871 const llvm::APSInt &value) {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008872
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008873 if (!enum_type || ConstString(name).IsEmpty())
8874 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008875
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008876 lldbassert(enum_type.GetTypeSystem() == static_cast<TypeSystem *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +00008877
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008878 lldb::opaque_compiler_type_t enum_opaque_compiler_type =
8879 enum_type.GetOpaqueQualType();
8880
8881 if (!enum_opaque_compiler_type)
8882 return nullptr;
8883
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008884 clang::QualType enum_qual_type(
8885 GetCanonicalQualType(enum_opaque_compiler_type));
8886
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008887 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8888
8889 if (!clang_type)
8890 return nullptr;
8891
8892 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8893
8894 if (!enutype)
8895 return nullptr;
8896
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008897 clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create(
8898 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8899 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
Zachary Turner1639c6b2018-12-17 16:15:13 +00008900 clang::QualType(enutype, 0), nullptr, value);
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008901
8902 if (!enumerator_decl)
8903 return nullptr;
8904
8905 enutype->getDecl()->addDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008906
8907#ifdef LLDB_CONFIGURATION_DEBUG
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008908 VerifyDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008909#endif
8910
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008911 return enumerator_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008912}
8913
Zachary Turner1639c6b2018-12-17 16:15:13 +00008914clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
8915 const CompilerType &enum_type, const Declaration &decl, const char *name,
8916 int64_t enum_value, uint32_t enum_value_bit_size) {
8917 CompilerType underlying_type =
8918 GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
8919 bool is_signed = false;
8920 underlying_type.IsIntegerType(is_signed);
8921
8922 llvm::APSInt value(enum_value_bit_size, is_signed);
8923 value = enum_value;
8924
8925 return AddEnumerationValueToEnumerationType(enum_type, decl, name, value);
8926}
8927
Greg Claytona1e5dc82015-08-11 22:53:00 +00008928CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00008929ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8930 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8931 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8932 if (clang_type) {
8933 const clang::EnumType *enutype =
8934 llvm::dyn_cast<clang::EnumType>(clang_type);
8935 if (enutype) {
8936 clang::EnumDecl *enum_decl = enutype->getDecl();
8937 if (enum_decl)
Alex Langfordbddab072019-08-13 19:40:36 +00008938 return CompilerType(this, enum_decl->getIntegerType().getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00008939 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008940 }
8941 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008942}
8943
Kate Stoneb9c1b512016-09-06 20:57:50 +00008944CompilerType
8945ClangASTContext::CreateMemberPointerType(const CompilerType &type,
8946 const CompilerType &pointee_type) {
8947 if (type && pointee_type.IsValid() &&
8948 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8949 ClangASTContext *ast =
8950 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8951 if (!ast)
8952 return CompilerType();
Alex Langfordbddab072019-08-13 19:40:36 +00008953 return CompilerType(ast, ast->getASTContext()
8954 ->getMemberPointerType(
8955 ClangUtil::GetQualType(pointee_type),
8956 ClangUtil::GetQualType(type).getTypePtr())
8957 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008958 }
8959 return CompilerType();
8960}
Greg Claytond8d4a572015-08-11 21:38:15 +00008961
Greg Claytond8d4a572015-08-11 21:38:15 +00008962// Dumping types
Greg Claytond8d4a572015-08-11 21:38:15 +00008963#define DEPTH_INCREMENT 2
8964
Adrian Prantl0c72a422019-03-07 20:20:02 +00008965#ifndef NDEBUG
8966LLVM_DUMP_METHOD void
8967ClangASTContext::dump(lldb::opaque_compiler_type_t type) const {
8968 if (!type)
8969 return;
8970 clang::QualType qual_type(GetQualType(type));
8971 qual_type.dump();
8972}
8973#endif
8974
Zachary Turner49110232018-11-05 17:40:28 +00008975void ClangASTContext::Dump(Stream &s) {
Zachary Turner115209e2018-11-05 19:25:39 +00008976 Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl());
Zachary Turner49110232018-11-05 17:40:28 +00008977 tu->dump(s.AsRawOstream());
8978}
8979
Shafik Yaghmour5f469822019-10-11 16:36:20 +00008980void ClangASTContext::DumpFromSymbolFile(Stream &s,
8981 llvm::StringRef symbol_name) {
8982 SymbolFile *symfile = GetSymbolFile();
8983
8984 if (!symfile)
8985 return;
8986
8987 lldb_private::TypeList type_list;
8988 symfile->GetTypes(nullptr, eTypeClassAny, type_list);
8989 size_t ntypes = type_list.GetSize();
8990
8991 for (size_t i = 0; i < ntypes; ++i) {
8992 TypeSP type = type_list.GetTypeAtIndex(i);
8993
8994 if (!symbol_name.empty())
shafikde2c7ca2019-10-28 14:26:54 -07008995 if (symbol_name != type->GetName().GetStringRef())
Shafik Yaghmour5f469822019-10-11 16:36:20 +00008996 continue;
8997
8998 s << type->GetName().AsCString() << "\n";
8999
9000 if (clang::TagDecl *tag_decl =
9001 GetAsTagDecl(type->GetFullCompilerType()))
9002 tag_decl->dump(s.AsRawOstream());
9003 else if (clang::TypedefNameDecl *typedef_decl =
9004 GetAsTypedefDecl(type->GetFullCompilerType()))
9005 typedef_decl->dump(s.AsRawOstream());
9006 else {
9007 GetCanonicalQualType(type->GetFullCompilerType().GetOpaqueQualType())
9008 .dump(s.AsRawOstream());
9009 }
9010 }
9011}
9012
Kate Stoneb9c1b512016-09-06 20:57:50 +00009013void ClangASTContext::DumpValue(
9014 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00009015 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009016 lldb::offset_t data_byte_offset, size_t data_byte_size,
9017 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
9018 bool show_summary, bool verbose, uint32_t depth) {
9019 if (!type)
9020 return;
9021
9022 clang::QualType qual_type(GetQualType(type));
9023 switch (qual_type->getTypeClass()) {
9024 case clang::Type::Record:
9025 if (GetCompleteType(type)) {
9026 const clang::RecordType *record_type =
9027 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9028 const clang::RecordDecl *record_decl = record_type->getDecl();
9029 assert(record_decl);
9030 uint32_t field_bit_offset = 0;
9031 uint32_t field_byte_offset = 0;
9032 const clang::ASTRecordLayout &record_layout =
9033 getASTContext()->getASTRecordLayout(record_decl);
9034 uint32_t child_idx = 0;
9035
9036 const clang::CXXRecordDecl *cxx_record_decl =
9037 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9038 if (cxx_record_decl) {
9039 // We might have base classes to print out first
9040 clang::CXXRecordDecl::base_class_const_iterator base_class,
9041 base_class_end;
9042 for (base_class = cxx_record_decl->bases_begin(),
9043 base_class_end = cxx_record_decl->bases_end();
9044 base_class != base_class_end; ++base_class) {
9045 const clang::CXXRecordDecl *base_class_decl =
9046 llvm::cast<clang::CXXRecordDecl>(
9047 base_class->getType()->getAs<clang::RecordType>()->getDecl());
9048
9049 // Skip empty base classes
Jonas Devliegherea6682a42018-12-15 00:15:33 +00009050 if (!verbose && !ClangASTContext::RecordHasFields(base_class_decl))
Kate Stoneb9c1b512016-09-06 20:57:50 +00009051 continue;
9052
9053 if (base_class->isVirtual())
9054 field_bit_offset =
9055 record_layout.getVBaseClassOffset(base_class_decl)
9056 .getQuantity() *
9057 8;
9058 else
9059 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
9060 .getQuantity() *
9061 8;
9062 field_byte_offset = field_bit_offset / 8;
9063 assert(field_bit_offset % 8 == 0);
9064 if (child_idx == 0)
9065 s->PutChar('{');
9066 else
9067 s->PutChar(',');
9068
9069 clang::QualType base_class_qual_type = base_class->getType();
9070 std::string base_class_type_name(base_class_qual_type.getAsString());
9071
9072 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00009073 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
9074 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009075
9076 clang::TypeInfo base_class_type_info =
9077 getASTContext()->getTypeInfo(base_class_qual_type);
9078
9079 // Dump the value of the member
Alex Langfordbddab072019-08-13 19:40:36 +00009080 CompilerType base_clang_type(this,
9081 base_class_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009082 base_clang_type.DumpValue(
9083 exe_ctx,
9084 s, // Stream to dump to
9085 base_clang_type
9086 .GetFormat(), // The format with which to display the member
9087 data, // Data buffer containing all bytes for this type
9088 data_byte_offset + field_byte_offset, // Offset into "data" where
9089 // to grab value from
9090 base_class_type_info.Width / 8, // Size of this type in bytes
9091 0, // Bitfield bit size
9092 0, // Bitfield bit offset
9093 show_types, // Boolean indicating if we should show the variable
9094 // types
9095 show_summary, // Boolean indicating if we should show a summary
9096 // for the current type
9097 verbose, // Verbose output?
9098 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9099 // children
9100
9101 ++child_idx;
9102 }
9103 }
9104 uint32_t field_idx = 0;
9105 clang::RecordDecl::field_iterator field, field_end;
9106 for (field = record_decl->field_begin(),
9107 field_end = record_decl->field_end();
9108 field != field_end; ++field, ++field_idx, ++child_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009109 // Print the starting squiggly bracket (if this is the first member) or
9110 // comma (for member 2 and beyond) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009111 if (child_idx == 0)
9112 s->PutChar('{');
9113 else
9114 s->PutChar(',');
9115
9116 // Indent
9117 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
9118
9119 clang::QualType field_type = field->getType();
9120 // Print the member type if requested
Adrian Prantl05097242018-04-30 16:49:04 +00009121 // Figure out the type byte size (field_type_info.first) and alignment
9122 // (field_type_info.second) from the AST context.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009123 clang::TypeInfo field_type_info =
9124 getASTContext()->getTypeInfo(field_type);
9125 assert(field_idx < record_layout.getFieldCount());
9126 // Figure out the field offset within the current struct/union/class
9127 // type
9128 field_bit_offset = record_layout.getFieldOffset(field_idx);
9129 field_byte_offset = field_bit_offset / 8;
9130 uint32_t field_bitfield_bit_size = 0;
9131 uint32_t field_bitfield_bit_offset = 0;
9132 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
9133 field_bitfield_bit_size))
9134 field_bitfield_bit_offset = field_bit_offset % 8;
9135
9136 if (show_types) {
9137 std::string field_type_name(field_type.getAsString());
9138 if (field_bitfield_bit_size > 0)
9139 s->Printf("(%s:%u) ", field_type_name.c_str(),
9140 field_bitfield_bit_size);
9141 else
9142 s->Printf("(%s) ", field_type_name.c_str());
9143 }
9144 // Print the member name and equal sign
9145 s->Printf("%s = ", field->getNameAsString().c_str());
9146
9147 // Dump the value of the member
Alex Langfordbddab072019-08-13 19:40:36 +00009148 CompilerType field_clang_type(this, field_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009149 field_clang_type.DumpValue(
9150 exe_ctx,
9151 s, // Stream to dump to
9152 field_clang_type
9153 .GetFormat(), // The format with which to display the member
9154 data, // Data buffer containing all bytes for this type
9155 data_byte_offset + field_byte_offset, // Offset into "data" where to
9156 // grab value from
9157 field_type_info.Width / 8, // Size of this type in bytes
9158 field_bitfield_bit_size, // Bitfield bit size
9159 field_bitfield_bit_offset, // Bitfield bit offset
9160 show_types, // Boolean indicating if we should show the variable
9161 // types
9162 show_summary, // Boolean indicating if we should show a summary for
9163 // the current type
9164 verbose, // Verbose output?
9165 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9166 // children
9167 }
9168
9169 // Indent the trailing squiggly bracket
9170 if (child_idx > 0)
9171 s->Printf("\n%*s}", depth, "");
9172 }
9173 return;
9174
9175 case clang::Type::Enum:
9176 if (GetCompleteType(type)) {
9177 const clang::EnumType *enutype =
9178 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9179 const clang::EnumDecl *enum_decl = enutype->getDecl();
9180 assert(enum_decl);
9181 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9182 lldb::offset_t offset = data_byte_offset;
9183 const int64_t enum_value = data.GetMaxU64Bitfield(
9184 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9185 for (enum_pos = enum_decl->enumerator_begin(),
9186 enum_end_pos = enum_decl->enumerator_end();
9187 enum_pos != enum_end_pos; ++enum_pos) {
9188 if (enum_pos->getInitVal() == enum_value) {
9189 s->Printf("%s", enum_pos->getNameAsString().c_str());
9190 return;
9191 }
9192 }
Adrian Prantl05097242018-04-30 16:49:04 +00009193 // If we have gotten here we didn't get find the enumerator in the enum
9194 // decl, so just print the integer.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009195 s->Printf("%" PRIi64, enum_value);
9196 }
9197 return;
9198
9199 case clang::Type::ConstantArray: {
9200 const clang::ConstantArrayType *array =
9201 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9202 bool is_array_of_characters = false;
9203 clang::QualType element_qual_type = array->getElementType();
9204
9205 const clang::Type *canonical_type =
9206 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9207 if (canonical_type)
9208 is_array_of_characters = canonical_type->isCharType();
9209
9210 const uint64_t element_count = array->getSize().getLimitedValue();
9211
9212 clang::TypeInfo field_type_info =
9213 getASTContext()->getTypeInfo(element_qual_type);
9214
9215 uint32_t element_idx = 0;
9216 uint32_t element_offset = 0;
9217 uint64_t element_byte_size = field_type_info.Width / 8;
9218 uint32_t element_stride = element_byte_size;
9219
9220 if (is_array_of_characters) {
9221 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009222 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9223 element_byte_size, element_count, UINT32_MAX,
9224 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009225 s->PutChar('"');
9226 return;
9227 } else {
Alex Langfordbddab072019-08-13 19:40:36 +00009228 CompilerType element_clang_type(this, element_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009229 lldb::Format element_format = element_clang_type.GetFormat();
9230
9231 for (element_idx = 0; element_idx < element_count; ++element_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009232 // Print the starting squiggly bracket (if this is the first member) or
9233 // comman (for member 2 and beyong) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009234 if (element_idx == 0)
9235 s->PutChar('{');
9236 else
9237 s->PutChar(',');
9238
9239 // Indent and print the index
9240 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9241
9242 // Figure out the field offset within the current struct/union/class
9243 // type
9244 element_offset = element_idx * element_stride;
9245
9246 // Dump the value of the member
9247 element_clang_type.DumpValue(
9248 exe_ctx,
9249 s, // Stream to dump to
9250 element_format, // The format with which to display the element
9251 data, // Data buffer containing all bytes for this type
9252 data_byte_offset +
9253 element_offset, // Offset into "data" where to grab value from
9254 element_byte_size, // Size of this type in bytes
9255 0, // Bitfield bit size
9256 0, // Bitfield bit offset
9257 show_types, // Boolean indicating if we should show the variable
9258 // types
9259 show_summary, // Boolean indicating if we should show a summary for
9260 // the current type
9261 verbose, // Verbose output?
9262 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9263 // children
9264 }
9265
9266 // Indent the trailing squiggly bracket
9267 if (element_idx > 0)
9268 s->Printf("\n%*s}", depth, "");
9269 }
9270 }
9271 return;
9272
9273 case clang::Type::Typedef: {
9274 clang::QualType typedef_qual_type =
9275 llvm::cast<clang::TypedefType>(qual_type)
9276 ->getDecl()
9277 ->getUnderlyingType();
9278
Alex Langfordbddab072019-08-13 19:40:36 +00009279 CompilerType typedef_clang_type(this, typedef_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009280 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9281 clang::TypeInfo typedef_type_info =
9282 getASTContext()->getTypeInfo(typedef_qual_type);
9283 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9284
9285 return typedef_clang_type.DumpValue(
9286 exe_ctx,
9287 s, // Stream to dump to
9288 typedef_format, // The format with which to display the element
9289 data, // Data buffer containing all bytes for this type
9290 data_byte_offset, // Offset into "data" where to grab value from
9291 typedef_byte_size, // Size of this type in bytes
9292 bitfield_bit_size, // Bitfield bit size
9293 bitfield_bit_offset, // Bitfield bit offset
9294 show_types, // Boolean indicating if we should show the variable types
9295 show_summary, // Boolean indicating if we should show a summary for the
9296 // current type
9297 verbose, // Verbose output?
9298 depth); // Scope depth for any types that have children
9299 } break;
9300
9301 case clang::Type::Auto: {
9302 clang::QualType elaborated_qual_type =
9303 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
Alex Langfordbddab072019-08-13 19:40:36 +00009304 CompilerType elaborated_clang_type(this,
9305 elaborated_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009306 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9307 clang::TypeInfo elaborated_type_info =
9308 getASTContext()->getTypeInfo(elaborated_qual_type);
9309 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9310
9311 return elaborated_clang_type.DumpValue(
9312 exe_ctx,
9313 s, // Stream to dump to
9314 elaborated_format, // The format with which to display the element
9315 data, // Data buffer containing all bytes for this type
9316 data_byte_offset, // Offset into "data" where to grab value from
9317 elaborated_byte_size, // Size of this type in bytes
9318 bitfield_bit_size, // Bitfield bit size
9319 bitfield_bit_offset, // Bitfield bit offset
9320 show_types, // Boolean indicating if we should show the variable types
9321 show_summary, // Boolean indicating if we should show a summary for the
9322 // current type
9323 verbose, // Verbose output?
9324 depth); // Scope depth for any types that have children
9325 } break;
9326
9327 case clang::Type::Elaborated: {
9328 clang::QualType elaborated_qual_type =
9329 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
Alex Langfordbddab072019-08-13 19:40:36 +00009330 CompilerType elaborated_clang_type(this,
9331 elaborated_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009332 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9333 clang::TypeInfo elaborated_type_info =
9334 getASTContext()->getTypeInfo(elaborated_qual_type);
9335 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9336
9337 return elaborated_clang_type.DumpValue(
9338 exe_ctx,
9339 s, // Stream to dump to
9340 elaborated_format, // The format with which to display the element
9341 data, // Data buffer containing all bytes for this type
9342 data_byte_offset, // Offset into "data" where to grab value from
9343 elaborated_byte_size, // Size of this type in bytes
9344 bitfield_bit_size, // Bitfield bit size
9345 bitfield_bit_offset, // Bitfield bit offset
9346 show_types, // Boolean indicating if we should show the variable types
9347 show_summary, // Boolean indicating if we should show a summary for the
9348 // current type
9349 verbose, // Verbose output?
9350 depth); // Scope depth for any types that have children
9351 } break;
9352
9353 case clang::Type::Paren: {
9354 clang::QualType desugar_qual_type =
9355 llvm::cast<clang::ParenType>(qual_type)->desugar();
Alex Langfordbddab072019-08-13 19:40:36 +00009356 CompilerType desugar_clang_type(this, desugar_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009357
9358 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9359 clang::TypeInfo desugar_type_info =
9360 getASTContext()->getTypeInfo(desugar_qual_type);
9361 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9362
9363 return desugar_clang_type.DumpValue(
9364 exe_ctx,
9365 s, // Stream to dump to
9366 desugar_format, // The format with which to display the element
9367 data, // Data buffer containing all bytes for this type
9368 data_byte_offset, // Offset into "data" where to grab value from
9369 desugar_byte_size, // Size of this type in bytes
9370 bitfield_bit_size, // Bitfield bit size
9371 bitfield_bit_offset, // Bitfield bit offset
9372 show_types, // Boolean indicating if we should show the variable types
9373 show_summary, // Boolean indicating if we should show a summary for the
9374 // current type
9375 verbose, // Verbose output?
9376 depth); // Scope depth for any types that have children
9377 } break;
9378
9379 default:
9380 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009381 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9382 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9383 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009384
9385 if (show_summary)
9386 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9387 break;
9388 }
9389}
9390
Frederic Rissd6470fb2019-10-08 15:35:58 +00009391static bool DumpEnumValue(const clang::QualType &qual_type, Stream *s,
9392 const DataExtractor &data, lldb::offset_t byte_offset,
9393 size_t byte_size, uint32_t bitfield_bit_offset,
9394 uint32_t bitfield_bit_size) {
9395 const clang::EnumType *enutype =
9396 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9397 const clang::EnumDecl *enum_decl = enutype->getDecl();
9398 assert(enum_decl);
Frederic Rissd6470fb2019-10-08 15:35:58 +00009399 lldb::offset_t offset = byte_offset;
Frederic Riss41ff3962019-10-08 15:35:59 +00009400 const uint64_t enum_svalue = data.GetMaxS64Bitfield(
Frederic Rissd6470fb2019-10-08 15:35:58 +00009401 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
Frederic Riss41ff3962019-10-08 15:35:59 +00009402 bool can_be_bitfield = true;
9403 uint64_t covered_bits = 0;
9404 int num_enumerators = 0;
Frederic Rissd6470fb2019-10-08 15:35:58 +00009405
Frederic Riss41ff3962019-10-08 15:35:59 +00009406 // Try to find an exact match for the value.
9407 // At the same time, we're applying a heuristic to determine whether we want
9408 // to print this enum as a bitfield. We're likely dealing with a bitfield if
9409 // every enumrator is either a one bit value or a superset of the previous
9410 // enumerators. Also 0 doesn't make sense when the enumerators are used as
9411 // flags.
Frederic Rissd6470fb2019-10-08 15:35:58 +00009412 for (auto enumerator : enum_decl->enumerators()) {
Frederic Riss41ff3962019-10-08 15:35:59 +00009413 uint64_t val = enumerator->getInitVal().getSExtValue();
Frederic Riss5d415b72019-10-08 17:59:02 +00009414 val = llvm::SignExtend64(val, 8*byte_size);
Frederic Riss41ff3962019-10-08 15:35:59 +00009415 if (llvm::countPopulation(val) != 1 && (val & ~covered_bits) != 0)
9416 can_be_bitfield = false;
9417 covered_bits |= val;
9418 ++num_enumerators;
9419 if (val == enum_svalue) {
9420 // Found an exact match, that's all we need to do.
Frederic Rissd6470fb2019-10-08 15:35:58 +00009421 s->PutCString(enumerator->getNameAsString());
9422 return true;
9423 }
9424 }
Frederic Riss41ff3962019-10-08 15:35:59 +00009425
Frederic Riss41ff3962019-10-08 15:35:59 +00009426 // Unsigned values make more sense for flags.
9427 offset = byte_offset;
9428 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9429 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9430
Frederic Rissb56e3a12019-10-08 19:52:01 +00009431 // No exact match, but we don't think this is a bitfield. Print the value as
9432 // decimal.
9433 if (!can_be_bitfield) {
9434 if (qual_type->isSignedIntegerOrEnumerationType())
9435 s->Printf("%" PRIi64, enum_svalue);
9436 else
9437 s->Printf("%" PRIu64, enum_uvalue);
9438 return true;
9439 }
9440
Frederic Riss41ff3962019-10-08 15:35:59 +00009441 uint64_t remaining_value = enum_uvalue;
9442 std::vector<std::pair<uint64_t, llvm::StringRef>> values;
9443 values.reserve(num_enumerators);
9444 for (auto enumerator : enum_decl->enumerators())
9445 if (auto val = enumerator->getInitVal().getZExtValue())
9446 values.emplace_back(val, enumerator->getName());
9447
9448 // Sort in reverse order of the number of the population count, so that in
9449 // `enum {A, B, ALL = A|B }` we visit ALL first. Use a stable sort so that
9450 // A | C where A is declared before C is displayed in this order.
9451 std::stable_sort(values.begin(), values.end(), [](const auto &a, const auto &b) {
9452 return llvm::countPopulation(a.first) > llvm::countPopulation(b.first);
9453 });
9454
9455 for (const auto &val : values) {
9456 if ((remaining_value & val.first) != val.first)
9457 continue;
9458 remaining_value &= ~val.first;
9459 s->PutCString(val.second);
9460 if (remaining_value)
9461 s->PutCString(" | ");
9462 }
9463
9464 // If there is a remainder that is not covered by the value, print it as hex.
9465 if (remaining_value)
9466 s->Printf("0x%" PRIx64, remaining_value);
9467
Frederic Rissd6470fb2019-10-08 15:35:58 +00009468 return true;
9469}
9470
Kate Stoneb9c1b512016-09-06 20:57:50 +00009471bool ClangASTContext::DumpTypeValue(
9472 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009473 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9474 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009475 ExecutionContextScope *exe_scope) {
9476 if (!type)
9477 return false;
9478 if (IsAggregateType(type)) {
9479 return false;
9480 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009481 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009482
9483 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Frederic Riss41ff3962019-10-08 15:35:59 +00009484
9485 if (type_class == clang::Type::Elaborated) {
9486 qual_type = llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9487 return DumpTypeValue(qual_type.getAsOpaquePtr(), s, format, data, byte_offset, byte_size,
9488 bitfield_bit_size, bitfield_bit_offset, exe_scope);
9489 }
9490
Kate Stoneb9c1b512016-09-06 20:57:50 +00009491 switch (type_class) {
9492 case clang::Type::Typedef: {
9493 clang::QualType typedef_qual_type =
9494 llvm::cast<clang::TypedefType>(qual_type)
9495 ->getDecl()
9496 ->getUnderlyingType();
Alex Langfordbddab072019-08-13 19:40:36 +00009497 CompilerType typedef_clang_type(this, typedef_qual_type.getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009498 if (format == eFormatDefault)
9499 format = typedef_clang_type.GetFormat();
9500 clang::TypeInfo typedef_type_info =
9501 getASTContext()->getTypeInfo(typedef_qual_type);
9502 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9503
9504 return typedef_clang_type.DumpTypeValue(
9505 s,
9506 format, // The format with which to display the element
9507 data, // Data buffer containing all bytes for this type
9508 byte_offset, // Offset into "data" where to grab value from
9509 typedef_byte_size, // Size of this type in bytes
9510 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9511 // treat as a bitfield
9512 bitfield_bit_offset, // Offset in bits of a bitfield value if
9513 // bitfield_bit_size != 0
9514 exe_scope);
9515 } break;
9516
9517 case clang::Type::Enum:
Adrian Prantl05097242018-04-30 16:49:04 +00009518 // If our format is enum or default, show the enumeration value as its
9519 // enumeration string value, else just display it as requested.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009520 if ((format == eFormatEnum || format == eFormatDefault) &&
Frederic Rissd6470fb2019-10-08 15:35:58 +00009521 GetCompleteType(type))
9522 return DumpEnumValue(qual_type, s, data, byte_offset, byte_size,
9523 bitfield_bit_offset, bitfield_bit_size);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009524 // format was not enum, just fall through and dump the value as
9525 // requested....
9526 LLVM_FALLTHROUGH;
9527
9528 default:
9529 // We are down to a scalar type that we just need to display.
9530 {
9531 uint32_t item_count = 1;
9532 // A few formats, we might need to modify our size and count for
9533 // depending
9534 // on how we are trying to display the value...
9535 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009536 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009537 case eFormatBoolean:
9538 case eFormatBinary:
9539 case eFormatComplex:
9540 case eFormatCString: // NULL terminated C strings
9541 case eFormatDecimal:
9542 case eFormatEnum:
9543 case eFormatHex:
9544 case eFormatHexUppercase:
9545 case eFormatFloat:
9546 case eFormatOctal:
9547 case eFormatOSType:
9548 case eFormatUnsigned:
9549 case eFormatPointer:
9550 case eFormatVectorOfChar:
9551 case eFormatVectorOfSInt8:
9552 case eFormatVectorOfUInt8:
9553 case eFormatVectorOfSInt16:
9554 case eFormatVectorOfUInt16:
9555 case eFormatVectorOfSInt32:
9556 case eFormatVectorOfUInt32:
9557 case eFormatVectorOfSInt64:
9558 case eFormatVectorOfUInt64:
9559 case eFormatVectorOfFloat32:
9560 case eFormatVectorOfFloat64:
9561 case eFormatVectorOfUInt128:
9562 break;
9563
9564 case eFormatChar:
9565 case eFormatCharPrintable:
9566 case eFormatCharArray:
9567 case eFormatBytes:
9568 case eFormatBytesWithASCII:
9569 item_count = byte_size;
9570 byte_size = 1;
9571 break;
9572
9573 case eFormatUnicode16:
9574 item_count = byte_size / 2;
9575 byte_size = 2;
9576 break;
9577
9578 case eFormatUnicode32:
9579 item_count = byte_size / 4;
9580 byte_size = 4;
9581 break;
9582 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009583 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9584 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9585 bitfield_bit_size, bitfield_bit_offset,
9586 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009587 }
9588 break;
9589 }
9590 }
Jonas Devlieghere09ad8c82019-05-24 00:44:33 +00009591 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009592}
9593
9594void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9595 ExecutionContext *exe_ctx, Stream *s,
9596 const lldb_private::DataExtractor &data,
9597 lldb::offset_t data_byte_offset,
9598 size_t data_byte_size) {
9599 uint32_t length = 0;
9600 if (IsCStringType(type, length)) {
9601 if (exe_ctx) {
9602 Process *process = exe_ctx->GetProcessPtr();
9603 if (process) {
9604 lldb::offset_t offset = data_byte_offset;
9605 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9606 std::vector<uint8_t> buf;
9607 if (length > 0)
9608 buf.resize(length);
9609 else
9610 buf.resize(256);
9611
Zachary Turner29cb8682017-03-03 20:57:05 +00009612 DataExtractor cstr_data(&buf.front(), buf.size(),
9613 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009614 buf.back() = '\0';
9615 size_t bytes_read;
9616 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009617 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009618 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9619 buf.size(), error)) > 0) {
9620 const size_t len = strlen((const char *)&buf.front());
9621 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009622 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009623 if (total_cstr_len == 0)
9624 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009625 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9626 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009627 total_cstr_len += len;
9628 if (len < buf.size())
9629 break;
9630 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009631 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009632 if (total_cstr_len > 0)
9633 s->PutChar('"');
9634 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009635 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009636 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009637}
9638
Kate Stoneb9c1b512016-09-06 20:57:50 +00009639void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9640 StreamFile s(stdout, false);
9641 DumpTypeDescription(type, &s);
9642 ClangASTMetadata *metadata =
9643 ClangASTContext::GetMetadata(getASTContext(), type);
9644 if (metadata) {
9645 metadata->Dump(&s);
9646 }
9647}
Greg Claytond8d4a572015-08-11 21:38:15 +00009648
Kate Stoneb9c1b512016-09-06 20:57:50 +00009649void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9650 Stream *s) {
9651 if (type) {
9652 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009653
Kate Stoneb9c1b512016-09-06 20:57:50 +00009654 llvm::SmallVector<char, 1024> buf;
9655 llvm::raw_svector_ostream llvm_ostrm(buf);
9656
9657 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9658 switch (type_class) {
9659 case clang::Type::ObjCObject:
9660 case clang::Type::ObjCInterface: {
9661 GetCompleteType(type);
9662
9663 const clang::ObjCObjectType *objc_class_type =
9664 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9665 assert(objc_class_type);
9666 if (objc_class_type) {
9667 clang::ObjCInterfaceDecl *class_interface_decl =
9668 objc_class_type->getInterface();
9669 if (class_interface_decl) {
9670 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9671 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009672 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009673 }
9674 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009675
Kate Stoneb9c1b512016-09-06 20:57:50 +00009676 case clang::Type::Typedef: {
9677 const clang::TypedefType *typedef_type =
9678 qual_type->getAs<clang::TypedefType>();
9679 if (typedef_type) {
9680 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9681 std::string clang_typedef_name(
9682 typedef_decl->getQualifiedNameAsString());
9683 if (!clang_typedef_name.empty()) {
9684 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009685 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009686 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009687 }
9688 } break;
9689
9690 case clang::Type::Auto:
Alex Langfordbddab072019-08-13 19:40:36 +00009691 CompilerType(this, llvm::cast<clang::AutoType>(qual_type)
9692 ->getDeducedType()
9693 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00009694 .DumpTypeDescription(s);
9695 return;
9696
9697 case clang::Type::Elaborated:
Alex Langfordbddab072019-08-13 19:40:36 +00009698 CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type)
9699 ->getNamedType()
9700 .getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00009701 .DumpTypeDescription(s);
9702 return;
9703
9704 case clang::Type::Paren:
Alex Langfordbddab072019-08-13 19:40:36 +00009705 CompilerType(
9706 this,
9707 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())
Kate Stoneb9c1b512016-09-06 20:57:50 +00009708 .DumpTypeDescription(s);
9709 return;
9710
9711 case clang::Type::Record: {
9712 GetCompleteType(type);
9713
9714 const clang::RecordType *record_type =
9715 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9716 const clang::RecordDecl *record_decl = record_type->getDecl();
9717 const clang::CXXRecordDecl *cxx_record_decl =
9718 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9719
9720 if (cxx_record_decl)
9721 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9722 s->GetIndentLevel());
9723 else
9724 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9725 s->GetIndentLevel());
9726 } break;
9727
9728 default: {
9729 const clang::TagType *tag_type =
9730 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9731 if (tag_type) {
9732 clang::TagDecl *tag_decl = tag_type->getDecl();
9733 if (tag_decl)
9734 tag_decl->print(llvm_ostrm, 0);
9735 } else {
9736 std::string clang_type_name(qual_type.getAsString());
9737 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009738 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009739 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009740 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009741 }
9742
Kate Stoneb9c1b512016-09-06 20:57:50 +00009743 if (buf.size() > 0) {
9744 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009745 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009746 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009747}
9748
Kate Stoneb9c1b512016-09-06 20:57:50 +00009749void ClangASTContext::DumpTypeName(const CompilerType &type) {
9750 if (ClangUtil::IsClangType(type)) {
9751 clang::QualType qual_type(
9752 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9753
9754 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9755 switch (type_class) {
9756 case clang::Type::Record: {
9757 const clang::CXXRecordDecl *cxx_record_decl =
9758 qual_type->getAsCXXRecordDecl();
9759 if (cxx_record_decl)
9760 printf("class %s", cxx_record_decl->getName().str().c_str());
9761 } break;
9762
9763 case clang::Type::Enum: {
9764 clang::EnumDecl *enum_decl =
9765 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9766 if (enum_decl) {
9767 printf("enum %s", enum_decl->getName().str().c_str());
9768 }
9769 } break;
9770
9771 case clang::Type::ObjCObject:
9772 case clang::Type::ObjCInterface: {
9773 const clang::ObjCObjectType *objc_class_type =
9774 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9775 if (objc_class_type) {
9776 clang::ObjCInterfaceDecl *class_interface_decl =
9777 objc_class_type->getInterface();
Adrian Prantl05097242018-04-30 16:49:04 +00009778 // We currently can't complete objective C types through the newly
9779 // added ASTContext because it only supports TagDecl objects right
9780 // now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00009781 if (class_interface_decl)
9782 printf("@class %s", class_interface_decl->getName().str().c_str());
9783 }
9784 } break;
9785
9786 case clang::Type::Typedef:
9787 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9788 ->getDecl()
9789 ->getName()
9790 .str()
9791 .c_str());
9792 break;
9793
9794 case clang::Type::Auto:
9795 printf("auto ");
9796 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9797 llvm::cast<clang::AutoType>(qual_type)
9798 ->getDeducedType()
9799 .getAsOpaquePtr()));
9800
9801 case clang::Type::Elaborated:
9802 printf("elaborated ");
9803 return DumpTypeName(CompilerType(
9804 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9805 ->getNamedType()
9806 .getAsOpaquePtr()));
9807
9808 case clang::Type::Paren:
9809 printf("paren ");
9810 return DumpTypeName(CompilerType(
9811 type.GetTypeSystem(),
9812 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9813
9814 default:
9815 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9816 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009817 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009818 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009819}
9820
Kate Stoneb9c1b512016-09-06 20:57:50 +00009821clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9822 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9823 const char *parent_name, int tag_decl_kind,
9824 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9825 if (template_param_infos.IsValid()) {
9826 std::string template_basename(parent_name);
9827 template_basename.erase(template_basename.find('<'));
9828
9829 return CreateClassTemplateDecl(decl_ctx, access_type,
9830 template_basename.c_str(), tag_decl_kind,
9831 template_param_infos);
9832 }
Konrad Kleine248a1302019-05-23 11:14:47 +00009833 return nullptr;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009834}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009835
Kate Stoneb9c1b512016-09-06 20:57:50 +00009836void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9837 ClangASTContext *ast = (ClangASTContext *)baton;
9838 SymbolFile *sym_file = ast->GetSymbolFile();
9839 if (sym_file) {
9840 CompilerType clang_type = GetTypeForDecl(decl);
9841 if (clang_type)
9842 sym_file->CompleteType(clang_type);
9843 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009844}
9845
Kate Stoneb9c1b512016-09-06 20:57:50 +00009846void ClangASTContext::CompleteObjCInterfaceDecl(
9847 void *baton, clang::ObjCInterfaceDecl *decl) {
9848 ClangASTContext *ast = (ClangASTContext *)baton;
9849 SymbolFile *sym_file = ast->GetSymbolFile();
9850 if (sym_file) {
9851 CompilerType clang_type = GetTypeForDecl(decl);
9852 if (clang_type)
9853 sym_file->CompleteType(clang_type);
9854 }
Zachary Turner42dff792016-04-15 00:21:26 +00009855}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009856
Kate Stoneb9c1b512016-09-06 20:57:50 +00009857DWARFASTParser *ClangASTContext::GetDWARFParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009858 if (!m_dwarf_ast_parser_up)
9859 m_dwarf_ast_parser_up.reset(new DWARFASTParserClang(*this));
9860 return m_dwarf_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00009861}
9862
9863PDBASTParser *ClangASTContext::GetPDBParser() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009864 if (!m_pdb_ast_parser_up)
9865 m_pdb_ast_parser_up.reset(new PDBASTParser(*this));
9866 return m_pdb_ast_parser_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00009867}
9868
9869bool ClangASTContext::LayoutRecordType(
9870 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9871 uint64_t &alignment,
9872 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9873 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9874 &base_offsets,
9875 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9876 &vbase_offsets) {
9877 ClangASTContext *ast = (ClangASTContext *)baton;
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009878 lldb_private::ClangASTImporter *importer = nullptr;
Jonas Devlieghered5b44032019-02-13 06:25:41 +00009879 if (ast->m_dwarf_ast_parser_up)
9880 importer = &ast->m_dwarf_ast_parser_up->GetClangASTImporter();
9881 if (!importer && ast->m_pdb_ast_parser_up)
9882 importer = &ast->m_pdb_ast_parser_up->GetClangASTImporter();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009883 if (!importer)
9884 return false;
9885
9886 return importer->LayoutRecordType(record_decl, bit_size, alignment,
9887 field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009888}
9889
Paul Hermand628cbb2015-09-15 23:44:17 +00009890// CompilerDecl override functions
Paul Hermand628cbb2015-09-15 23:44:17 +00009891
Kate Stoneb9c1b512016-09-06 20:57:50 +00009892ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9893 if (opaque_decl) {
9894 clang::NamedDecl *nd =
9895 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9896 if (nd != nullptr)
9897 return ConstString(nd->getDeclName().getAsString());
9898 }
9899 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009900}
9901
Kate Stoneb9c1b512016-09-06 20:57:50 +00009902ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9903 if (opaque_decl) {
9904 clang::NamedDecl *nd =
9905 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9906 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9907 clang::MangleContext *mc = getMangleContext();
9908 if (mc && mc->shouldMangleCXXName(nd)) {
9909 llvm::SmallVector<char, 1024> buf;
9910 llvm::raw_svector_ostream llvm_ostrm(buf);
9911 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9912 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9913 Ctor_Complete, llvm_ostrm);
9914 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9915 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9916 Dtor_Complete, llvm_ostrm);
9917 } else {
9918 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009919 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009920 if (buf.size() > 0)
9921 return ConstString(buf.data(), buf.size());
9922 }
Greg Claytonfe689042015-11-10 17:47:04 +00009923 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009924 }
9925 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009926}
9927
Kate Stoneb9c1b512016-09-06 20:57:50 +00009928CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9929 if (opaque_decl)
9930 return CompilerDeclContext(this,
9931 ((clang::Decl *)opaque_decl)->getDeclContext());
9932 else
9933 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009934}
9935
Kate Stoneb9c1b512016-09-06 20:57:50 +00009936CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9937 if (clang::FunctionDecl *func_decl =
9938 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9939 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9940 if (clang::ObjCMethodDecl *objc_method =
9941 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9942 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9943 else
Greg Claytonfe689042015-11-10 17:47:04 +00009944 return CompilerType();
9945}
9946
Kate Stoneb9c1b512016-09-06 20:57:50 +00009947size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9948 if (clang::FunctionDecl *func_decl =
9949 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9950 return func_decl->param_size();
9951 if (clang::ObjCMethodDecl *objc_method =
9952 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9953 return objc_method->param_size();
9954 else
9955 return 0;
9956}
9957
9958CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9959 size_t idx) {
9960 if (clang::FunctionDecl *func_decl =
9961 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9962 if (idx < func_decl->param_size()) {
9963 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9964 if (var_decl)
9965 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9966 }
9967 } else if (clang::ObjCMethodDecl *objc_method =
9968 llvm::dyn_cast<clang::ObjCMethodDecl>(
9969 (clang::Decl *)opaque_decl)) {
9970 if (idx < objc_method->param_size())
9971 return CompilerType(
9972 this,
9973 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9974 }
9975 return CompilerType();
9976}
9977
Greg Clayton99558cc42015-08-24 23:46:31 +00009978// CompilerDeclContext functions
Greg Clayton99558cc42015-08-24 23:46:31 +00009979
Kate Stoneb9c1b512016-09-06 20:57:50 +00009980std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9981 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9982 std::vector<CompilerDecl> found_decls;
9983 if (opaque_decl_ctx) {
9984 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9985 std::set<DeclContext *> searched;
9986 std::multimap<DeclContext *, DeclContext *> search_queue;
9987 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009988
Kate Stoneb9c1b512016-09-06 20:57:50 +00009989 for (clang::DeclContext *decl_context = root_decl_ctx;
9990 decl_context != nullptr && found_decls.empty();
9991 decl_context = decl_context->getParent()) {
9992 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +00009993
Kate Stoneb9c1b512016-09-06 20:57:50 +00009994 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9995 it++) {
9996 if (!searched.insert(it->second).second)
9997 continue;
9998 symbol_file->ParseDeclsForContext(
9999 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +000010000
Kate Stoneb9c1b512016-09-06 20:57:50 +000010001 for (clang::Decl *child : it->second->decls()) {
10002 if (clang::UsingDirectiveDecl *ud =
10003 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10004 if (ignore_using_decls)
10005 continue;
10006 clang::DeclContext *from = ud->getCommonAncestor();
10007 if (searched.find(ud->getNominatedNamespace()) == searched.end())
10008 search_queue.insert(
10009 std::make_pair(from, ud->getNominatedNamespace()));
10010 } else if (clang::UsingDecl *ud =
10011 llvm::dyn_cast<clang::UsingDecl>(child)) {
10012 if (ignore_using_decls)
10013 continue;
10014 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10015 clang::Decl *target = usd->getTargetDecl();
10016 if (clang::NamedDecl *nd =
10017 llvm::dyn_cast<clang::NamedDecl>(target)) {
10018 IdentifierInfo *ii = nd->getIdentifier();
10019 if (ii != nullptr &&
10020 ii->getName().equals(name.AsCString(nullptr)))
10021 found_decls.push_back(CompilerDecl(this, nd));
10022 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010023 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010024 } else if (clang::NamedDecl *nd =
10025 llvm::dyn_cast<clang::NamedDecl>(child)) {
10026 IdentifierInfo *ii = nd->getIdentifier();
10027 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
10028 found_decls.push_back(CompilerDecl(this, nd));
10029 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010030 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010031 }
Paul Hermand628cbb2015-09-15 23:44:17 +000010032 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010033 }
10034 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +000010035}
10036
Dawn Perchikb5925782015-12-12 19:31:41 +000010037// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010038// and return the number of levels it took to find it, or
Adrian Prantl05097242018-04-30 16:49:04 +000010039// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
10040// declaration, its name and/or type, if set, will be used to check that the
10041// decl found in the scope is a match.
Dawn Perchikb5925782015-12-12 19:31:41 +000010042//
Kate Stoneb9c1b512016-09-06 20:57:50 +000010043// The optional name is required by languages (like C++) to handle using
Adrian Prantl05097242018-04-30 16:49:04 +000010044// declarations like:
Dawn Perchikb5925782015-12-12 19:31:41 +000010045//
10046// void poo();
10047// namespace ns {
10048// void foo();
10049// void goo();
10050// }
10051// void bar() {
10052// using ns::foo;
10053// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
10054// // LLDB_INVALID_DECL_LEVEL for 'goo'.
10055// }
10056//
10057// The optional type is useful in the case that there's a specific overload
10058// that we're looking for that might otherwise be shadowed, like:
10059//
10060// void foo(int);
10061// namespace ns {
10062// void foo();
10063// }
10064// void bar() {
10065// using ns::foo;
10066// // CountDeclLevels returns 0 for { 'foo', void() },
10067// // 1 for { 'foo', void(int) }, and
10068// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
10069// }
10070//
10071// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +000010072// function at file scope will return the same level as a function at global
Adrian Prantl05097242018-04-30 16:49:04 +000010073// scope. Ideally we'd like to treat the file scope as an additional scope just
10074// below the global scope. More work needs to be done to recognise that, if
10075// the decl we're trying to look up is static, we should compare its source
10076// file with that of the current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010077uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
10078 clang::DeclContext *child_decl_ctx,
10079 ConstString *child_name,
10080 CompilerType *child_type) {
10081 if (frame_decl_ctx) {
10082 std::set<DeclContext *> searched;
10083 std::multimap<DeclContext *, DeclContext *> search_queue;
10084 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +000010085
Kate Stoneb9c1b512016-09-06 20:57:50 +000010086 // Get the lookup scope for the decl we're trying to find.
10087 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +000010088
Kate Stoneb9c1b512016-09-06 20:57:50 +000010089 // Look for it in our scope's decl context and its parents.
10090 uint32_t level = 0;
10091 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
10092 decl_ctx = decl_ctx->getParent()) {
10093 if (!decl_ctx->isLookupContext())
10094 continue;
10095 if (decl_ctx == parent_decl_ctx)
10096 // Found it!
10097 return level;
10098 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
10099 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
10100 it++) {
10101 if (searched.find(it->second) != searched.end())
10102 continue;
10103
10104 // Currently DWARF has one shared translation unit for all Decls at top
Adrian Prantl05097242018-04-30 16:49:04 +000010105 // level, so this would erroneously find using statements anywhere. So
10106 // don't look at the top-level translation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010107 // TODO fix this and add a testcase that depends on it.
10108
10109 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
10110 continue;
10111
10112 searched.insert(it->second);
10113 symbol_file->ParseDeclsForContext(
10114 CompilerDeclContext(this, it->second));
10115
10116 for (clang::Decl *child : it->second->decls()) {
10117 if (clang::UsingDirectiveDecl *ud =
10118 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10119 clang::DeclContext *ns = ud->getNominatedNamespace();
10120 if (ns == parent_decl_ctx)
10121 // Found it!
10122 return level;
10123 clang::DeclContext *from = ud->getCommonAncestor();
10124 if (searched.find(ns) == searched.end())
10125 search_queue.insert(std::make_pair(from, ns));
10126 } else if (child_name) {
10127 if (clang::UsingDecl *ud =
10128 llvm::dyn_cast<clang::UsingDecl>(child)) {
10129 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10130 clang::Decl *target = usd->getTargetDecl();
10131 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
10132 if (!nd)
10133 continue;
10134 // Check names.
10135 IdentifierInfo *ii = nd->getIdentifier();
10136 if (ii == nullptr ||
10137 !ii->getName().equals(child_name->AsCString(nullptr)))
10138 continue;
10139 // Check types, if one was provided.
10140 if (child_type) {
10141 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
10142 if (!AreTypesSame(clang_type, *child_type,
10143 /*ignore_qualifiers=*/true))
10144 continue;
10145 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010146 // Found it!
10147 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010148 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010149 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010150 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010151 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010152 }
10153 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +000010154 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010155 }
10156 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +000010157}
10158
Kate Stoneb9c1b512016-09-06 20:57:50 +000010159bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
10160 if (opaque_decl_ctx)
10161 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
10162 else
Greg Clayton99558cc42015-08-24 23:46:31 +000010163 return false;
10164}
10165
Kate Stoneb9c1b512016-09-06 20:57:50 +000010166ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
10167 if (opaque_decl_ctx) {
10168 clang::NamedDecl *named_decl =
10169 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10170 if (named_decl)
10171 return ConstString(named_decl->getName());
10172 }
10173 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +000010174}
10175
Kate Stoneb9c1b512016-09-06 20:57:50 +000010176ConstString
10177ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
10178 if (opaque_decl_ctx) {
10179 clang::NamedDecl *named_decl =
10180 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10181 if (named_decl)
10182 return ConstString(
10183 llvm::StringRef(named_decl->getQualifiedNameAsString()));
10184 }
10185 return ConstString();
10186}
10187
10188bool ClangASTContext::DeclContextIsClassMethod(
10189 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
10190 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
10191 if (opaque_decl_ctx) {
10192 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10193 if (ObjCMethodDecl *objc_method =
10194 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
10195 if (is_instance_method_ptr)
10196 *is_instance_method_ptr = objc_method->isInstanceMethod();
10197 if (language_ptr)
10198 *language_ptr = eLanguageTypeObjC;
10199 if (language_object_name_ptr)
10200 language_object_name_ptr->SetCString("self");
10201 return true;
10202 } else if (CXXMethodDecl *cxx_method =
10203 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
10204 if (is_instance_method_ptr)
10205 *is_instance_method_ptr = cxx_method->isInstance();
10206 if (language_ptr)
10207 *language_ptr = eLanguageTypeC_plus_plus;
10208 if (language_object_name_ptr)
10209 language_object_name_ptr->SetCString("this");
10210 return true;
10211 } else if (clang::FunctionDecl *function_decl =
10212 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
10213 ClangASTMetadata *metadata =
10214 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
10215 if (metadata && metadata->HasObjectPtr()) {
10216 if (is_instance_method_ptr)
10217 *is_instance_method_ptr = true;
10218 if (language_ptr)
10219 *language_ptr = eLanguageTypeObjC;
10220 if (language_object_name_ptr)
10221 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10222 return true;
10223 }
10224 }
10225 }
10226 return false;
10227}
10228
Raphael Isemanna9469972019-03-12 07:45:04 +000010229bool ClangASTContext::DeclContextIsContainedInLookup(
10230 void *opaque_decl_ctx, void *other_opaque_decl_ctx) {
10231 auto *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10232 auto *other = (clang::DeclContext *)other_opaque_decl_ctx;
10233
10234 do {
10235 // A decl context always includes its own contents in its lookup.
10236 if (decl_ctx == other)
10237 return true;
10238
10239 // If we have an inline namespace, then the lookup of the parent context
10240 // also includes the inline namespace contents.
10241 } while (other->isInlineNamespace() && (other = other->getParent()));
10242
10243 return false;
10244}
10245
Kate Stoneb9c1b512016-09-06 20:57:50 +000010246clang::DeclContext *
10247ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10248 if (dc.IsClang())
10249 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10250 return nullptr;
10251}
Greg Clayton99558cc42015-08-24 23:46:31 +000010252
10253ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010254ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10255 if (dc.IsClang())
10256 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10257 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10258 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010259}
10260
10261CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010262ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10263 if (dc.IsClang())
10264 return llvm::dyn_cast<clang::CXXMethodDecl>(
10265 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10266 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010267}
10268
10269clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010270ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10271 if (dc.IsClang())
10272 return llvm::dyn_cast<clang::FunctionDecl>(
10273 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10274 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010275}
10276
10277clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010278ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10279 if (dc.IsClang())
10280 return llvm::dyn_cast<clang::NamespaceDecl>(
10281 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10282 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010283}
10284
10285ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010286ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10287 const void *object) {
10288 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10289 if (ast)
10290 return ClangASTContext::GetMetadata(ast, object);
10291 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010292}
10293
10294clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010295ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10296 ClangASTContext *ast =
10297 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10298 if (ast)
10299 return ast->getASTContext();
10300 return nullptr;
10301}
10302
Raphael Isemann2eb963a2019-10-02 12:26:08 +000010303ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target,
10304 ArchSpec arch)
10305 : ClangASTContext(arch), m_target_wp(target.shared_from_this()),
Kate Stoneb9c1b512016-09-06 20:57:50 +000010306 m_persistent_variables(new ClangPersistentVariables) {}
10307
10308UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010309 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010310 Expression::ResultType desired_type,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010311 const EvaluateExpressionOptions &options,
10312 ValueObject *ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000010313 TargetSP target_sp = m_target_wp.lock();
10314 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010315 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010316
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010317 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Aleksandr Urakov40624a02019-02-05 09:14:36 +000010318 desired_type, options, ctx_obj);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010319}
10320
Kate Stoneb9c1b512016-09-06 20:57:50 +000010321FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10322 const CompilerType &return_type, const Address &function_address,
10323 const ValueList &arg_value_list, const char *name) {
10324 TargetSP target_sp = m_target_wp.lock();
10325 if (!target_sp)
10326 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010327
Kate Stoneb9c1b512016-09-06 20:57:50 +000010328 Process *process = target_sp->GetProcessSP().get();
10329 if (!process)
10330 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010331
Kate Stoneb9c1b512016-09-06 20:57:50 +000010332 return new ClangFunctionCaller(*process, return_type, function_address,
10333 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010334}
10335
10336UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010337ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10338 const char *name) {
10339 TargetSP target_sp = m_target_wp.lock();
10340 if (!target_sp)
10341 return nullptr;
10342
10343 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010344}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010345
10346PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010347ClangASTContextForExpressions::GetPersistentExpressionState() {
10348 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010349}
Sean Callanan68e44232017-09-28 20:20:25 +000010350
10351clang::ExternalASTMerger &
10352ClangASTContextForExpressions::GetMergerUnchecked() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000010353 lldbassert(m_scratch_ast_source_up != nullptr);
10354 return m_scratch_ast_source_up->GetMergerUnchecked();
Sean Callanan68e44232017-09-28 20:20:25 +000010355}