blob: 47c5e144b13de37ebd5beef79c5a99bb9bd934d8 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eli Friedman932197d2010-06-13 19:06:42 +000010#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011
Zachary Turner827d5d72016-12-16 04:27:00 +000012#include "llvm/Support/FormatAdapters.h"
13#include "llvm/Support/FormatVariadic.h"
14
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000015#include <mutex>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include <string>
Sean Callananfe38c852015-10-08 23:07:53 +000017#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018
Greg Clayton6beaaa62011-01-17 03:46:26 +000019
Kate Stoneb9c1b512016-09-06 20:57:50 +000020// Clang headers like to use NDEBUG inside of them to enable/disable debug
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000021// related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
Greg Clayton6beaaa62011-01-17 03:46:26 +000022// or another. This is bad because it means that if clang was built in release
23// mode, it assumes that you are building in release mode which is not always
24// the case. You can end up with functions that are defined as empty in header
25// files when NDEBUG is not defined, and this can cause link errors with the
26// clang .a files that you have since you might be missing functions in the .a
27// file. So we have to define NDEBUG when including clang headers to avoid any
28// mismatches. This is covered by rdar://problem/8691220
29
Sean Callanan3b1d4f62011-10-26 17:46:51 +000030#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000031#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000032#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000033// Need to include assert.h so it is as clang would expect it to be (disabled)
34#include <assert.h>
35#endif
36
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "clang/AST/ASTContext.h"
38#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000039#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000041#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000042#include "clang/AST/DeclTemplate.h"
Greg Claytonfe689042015-11-10 17:47:04 +000043#include "clang/AST/Mangle.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044#include "clang/AST/RecordLayout.h"
45#include "clang/AST/Type.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000046#include "clang/AST/VTableBuilder.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000048#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000050#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051#include "clang/Basic/SourceManager.h"
52#include "clang/Basic/TargetInfo.h"
53#include "clang/Basic/TargetOptions.h"
54#include "clang/Frontend/FrontendOptions.h"
55#include "clang/Frontend/LangStandard.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000056
57#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000058#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000059#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
60// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
61#include <assert.h>
62#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063
Greg Claytond8d4a572015-08-11 21:38:15 +000064#include "llvm/Support/Signals.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000065#include "llvm/Support/Threading.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000066
Zachary Turnerd133f6a2016-03-28 22:53:41 +000067#include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
68#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
69#include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000070#include "lldb/Utility/ArchSpec.h"
Zachary Turner01c32432017-02-14 19:06:07 +000071#include "lldb/Utility/Flags.h"
72
Zachary Turner29cb8682017-03-03 20:57:05 +000073#include "lldb/Core/DumpDataExtractor.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000074#include "lldb/Core/Module.h"
75#include "lldb/Core/PluginManager.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000076#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000077#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000078#include "lldb/Core/UniqueCStringMap.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000079#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000080#include "lldb/Symbol/ClangASTImporter.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000081#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000082#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000083#include "lldb/Symbol/ClangUtil.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000084#include "lldb/Symbol/ObjectFile.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000085#include "lldb/Symbol/SymbolFile.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000086#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000087#include "lldb/Target/ExecutionContext.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000088#include "lldb/Target/Language.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000089#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham151c0322015-09-15 21:13:50 +000090#include "lldb/Target/Process.h"
91#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000092#include "lldb/Utility/DataExtractor.h"
Sean Callananc530ba92016-05-02 21:15:31 +000093#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000094#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000095#include "lldb/Utility/RegularExpression.h"
Pavel Labathd821c992018-08-07 11:07:21 +000096#include "lldb/Utility/Scalar.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000097
Greg Clayton261ac3f2015-08-28 01:01:03 +000098#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
Zachary Turner42dff792016-04-15 00:21:26 +000099#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +0000100
Eli Friedman932197d2010-06-13 19:06:42 +0000101#include <stdio.h>
102
Greg Clayton1341baf2013-07-11 23:36:31 +0000103#include <mutex>
104
Greg Claytonc86103d2010-08-05 01:57:25 +0000105using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106using namespace lldb_private;
107using namespace llvm;
108using namespace clang;
109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110namespace {
111static inline bool
112ClangASTContextSupportsLanguage(lldb::LanguageType language) {
113 return language == eLanguageTypeUnknown || // Clang is the default type system
114 Language::LanguageIsC(language) ||
115 Language::LanguageIsCPlusPlus(language) ||
116 Language::LanguageIsObjC(language) ||
117 Language::LanguageIsPascal(language) ||
118 // Use Clang for Rust until there is a proper language plugin for it
119 language == eLanguageTypeRust ||
Johan Engelen04799572016-11-25 11:01:12 +0000120 language == eLanguageTypeExtRenderScript ||
121 // Use Clang for D until there is a proper language plugin for it
122 language == eLanguageTypeD;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123}
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000124
125// Checks whether m1 is an overload of m2 (as opposed to an override). This is
126// called by addOverridesForMethod to distinguish overrides (which share a
127// vtable entry) from overloads (which require distinct entries).
128bool isOverload(clang::CXXMethodDecl *m1, clang::CXXMethodDecl *m2) {
129 // FIXME: This should detect covariant return types, but currently doesn't.
130 lldbassert(&m1->getASTContext() == &m2->getASTContext() &&
131 "Methods should have the same AST context");
132 clang::ASTContext &context = m1->getASTContext();
133
134 const auto *m1Type = llvm::cast<clang::FunctionProtoType>(
135 context.getCanonicalType(m1->getType()));
136
137 const auto *m2Type = llvm::cast<clang::FunctionProtoType>(
138 context.getCanonicalType(m2->getType()));
139
140 auto compareArgTypes = [&context](const clang::QualType &m1p,
141 const clang::QualType &m2p) {
142 return context.hasSameType(m1p.getUnqualifiedType(),
143 m2p.getUnqualifiedType());
144 };
145
146 // FIXME: In C++14 and later, we can just pass m2Type->param_type_end()
147 // as a fourth parameter to std::equal().
148 return (m1->getNumParams() != m2->getNumParams()) ||
149 !std::equal(m1Type->param_type_begin(), m1Type->param_type_end(),
150 m2Type->param_type_begin(), compareArgTypes);
151}
152
153// If decl is a virtual method, walk the base classes looking for methods that
154// decl overrides. This table of overridden methods is used by IRGen to
155// determine the vtable layout for decl's parent class.
156void addOverridesForMethod(clang::CXXMethodDecl *decl) {
157 if (!decl->isVirtual())
158 return;
159
160 clang::CXXBasePaths paths;
161
162 auto find_overridden_methods =
163 [decl](const clang::CXXBaseSpecifier *specifier,
164 clang::CXXBasePath &path) {
165 if (auto *base_record = llvm::dyn_cast<clang::CXXRecordDecl>(
166 specifier->getType()->getAs<clang::RecordType>()->getDecl())) {
167
168 clang::DeclarationName name = decl->getDeclName();
169
170 // If this is a destructor, check whether the base class destructor is
171 // virtual.
172 if (name.getNameKind() == clang::DeclarationName::CXXDestructorName)
173 if (auto *baseDtorDecl = base_record->getDestructor()) {
174 if (baseDtorDecl->isVirtual()) {
175 path.Decls = baseDtorDecl;
176 return true;
177 } else
178 return false;
179 }
180
181 // Otherwise, search for name in the base class.
182 for (path.Decls = base_record->lookup(name); !path.Decls.empty();
183 path.Decls = path.Decls.slice(1)) {
184 if (auto *method_decl =
185 llvm::dyn_cast<clang::CXXMethodDecl>(path.Decls.front()))
186 if (method_decl->isVirtual() && !isOverload(decl, method_decl)) {
187 path.Decls = method_decl;
188 return true;
189 }
190 }
191 }
192
193 return false;
194 };
195
196 if (decl->getParent()->lookupInBases(find_overridden_methods, paths)) {
197 for (auto *overridden_decl : paths.found_decls())
198 decl->addOverriddenMethod(
199 llvm::cast<clang::CXXMethodDecl>(overridden_decl));
200 }
201}
Greg Clayton56939cb2015-09-17 22:23:34 +0000202}
203
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *>
205 ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000206
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207static ClangASTMap &GetASTMap() {
208 static ClangASTMap *g_map_ptr = nullptr;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000209 static llvm::once_flag g_once_flag;
210 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
212 });
213 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000214}
215
Davide Italiano7e3ef4d2018-03-20 19:46:32 +0000216bool ClangASTContext::IsOperator(const char *name,
217 clang::OverloadedOperatorKind &op_kind) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 if (name == nullptr || name[0] == '\0')
219 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000220
221#define OPERATOR_PREFIX "operator"
222#define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1)
223
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224 const char *post_op_name = nullptr;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 bool no_space = true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000227
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
229 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 post_op_name = name + OPERATOR_PREFIX_LENGTH;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000232
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 if (post_op_name[0] == ' ') {
234 post_op_name++;
235 no_space = false;
236 }
Pavel Labath1ac2b202016-08-15 14:32:32 +0000237
238#undef OPERATOR_PREFIX
239#undef OPERATOR_PREFIX_LENGTH
240
Adrian Prantl05097242018-04-30 16:49:04 +0000241 // This is an operator, set the overloaded operator kind to invalid in case
242 // this is a conversion operator...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 op_kind = clang::NUM_OVERLOADED_OPERATORS;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000244
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245 switch (post_op_name[0]) {
246 default:
247 if (no_space)
248 return false;
249 break;
250 case 'n':
251 if (no_space)
252 return false;
253 if (strcmp(post_op_name, "new") == 0)
254 op_kind = clang::OO_New;
255 else if (strcmp(post_op_name, "new[]") == 0)
256 op_kind = clang::OO_Array_New;
257 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000258
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 case 'd':
260 if (no_space)
261 return false;
262 if (strcmp(post_op_name, "delete") == 0)
263 op_kind = clang::OO_Delete;
264 else if (strcmp(post_op_name, "delete[]") == 0)
265 op_kind = clang::OO_Array_Delete;
266 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 case '+':
269 if (post_op_name[1] == '\0')
270 op_kind = clang::OO_Plus;
271 else if (post_op_name[2] == '\0') {
272 if (post_op_name[1] == '=')
273 op_kind = clang::OO_PlusEqual;
274 else if (post_op_name[1] == '+')
275 op_kind = clang::OO_PlusPlus;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000276 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000278
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 case '-':
280 if (post_op_name[1] == '\0')
281 op_kind = clang::OO_Minus;
282 else if (post_op_name[2] == '\0') {
283 switch (post_op_name[1]) {
284 case '=':
285 op_kind = clang::OO_MinusEqual;
286 break;
287 case '-':
288 op_kind = clang::OO_MinusMinus;
289 break;
290 case '>':
291 op_kind = clang::OO_Arrow;
292 break;
293 }
294 } else if (post_op_name[3] == '\0') {
295 if (post_op_name[2] == '*')
296 op_kind = clang::OO_ArrowStar;
297 break;
298 }
299 break;
300
301 case '*':
302 if (post_op_name[1] == '\0')
303 op_kind = clang::OO_Star;
304 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
305 op_kind = clang::OO_StarEqual;
306 break;
307
308 case '/':
309 if (post_op_name[1] == '\0')
310 op_kind = clang::OO_Slash;
311 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
312 op_kind = clang::OO_SlashEqual;
313 break;
314
315 case '%':
316 if (post_op_name[1] == '\0')
317 op_kind = clang::OO_Percent;
318 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
319 op_kind = clang::OO_PercentEqual;
320 break;
321
322 case '^':
323 if (post_op_name[1] == '\0')
324 op_kind = clang::OO_Caret;
325 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
326 op_kind = clang::OO_CaretEqual;
327 break;
328
329 case '&':
330 if (post_op_name[1] == '\0')
331 op_kind = clang::OO_Amp;
332 else if (post_op_name[2] == '\0') {
333 switch (post_op_name[1]) {
334 case '=':
335 op_kind = clang::OO_AmpEqual;
336 break;
337 case '&':
338 op_kind = clang::OO_AmpAmp;
339 break;
340 }
341 }
342 break;
343
344 case '|':
345 if (post_op_name[1] == '\0')
346 op_kind = clang::OO_Pipe;
347 else if (post_op_name[2] == '\0') {
348 switch (post_op_name[1]) {
349 case '=':
350 op_kind = clang::OO_PipeEqual;
351 break;
352 case '|':
353 op_kind = clang::OO_PipePipe;
354 break;
355 }
356 }
357 break;
358
359 case '~':
360 if (post_op_name[1] == '\0')
361 op_kind = clang::OO_Tilde;
362 break;
363
364 case '!':
365 if (post_op_name[1] == '\0')
366 op_kind = clang::OO_Exclaim;
367 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
368 op_kind = clang::OO_ExclaimEqual;
369 break;
370
371 case '=':
372 if (post_op_name[1] == '\0')
373 op_kind = clang::OO_Equal;
374 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
375 op_kind = clang::OO_EqualEqual;
376 break;
377
378 case '<':
379 if (post_op_name[1] == '\0')
380 op_kind = clang::OO_Less;
381 else if (post_op_name[2] == '\0') {
382 switch (post_op_name[1]) {
383 case '<':
384 op_kind = clang::OO_LessLess;
385 break;
386 case '=':
387 op_kind = clang::OO_LessEqual;
388 break;
389 }
390 } else if (post_op_name[3] == '\0') {
391 if (post_op_name[2] == '=')
392 op_kind = clang::OO_LessLessEqual;
393 }
394 break;
395
396 case '>':
397 if (post_op_name[1] == '\0')
398 op_kind = clang::OO_Greater;
399 else if (post_op_name[2] == '\0') {
400 switch (post_op_name[1]) {
401 case '>':
402 op_kind = clang::OO_GreaterGreater;
403 break;
404 case '=':
405 op_kind = clang::OO_GreaterEqual;
406 break;
407 }
408 } else if (post_op_name[1] == '>' && post_op_name[2] == '=' &&
409 post_op_name[3] == '\0') {
410 op_kind = clang::OO_GreaterGreaterEqual;
411 }
412 break;
413
414 case ',':
415 if (post_op_name[1] == '\0')
416 op_kind = clang::OO_Comma;
417 break;
418
419 case '(':
420 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
421 op_kind = clang::OO_Call;
422 break;
423
424 case '[':
425 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
426 op_kind = clang::OO_Subscript;
427 break;
428 }
429
430 return true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000431}
Enrico Granata5d84a692014-08-19 21:46:37 +0000432
Greg Clayton57ee3062013-07-11 22:46:58 +0000433clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
435 switch (access) {
436 default:
437 break;
438 case eAccessNone:
Greg Clayton8cf05932010-07-22 18:30:50 +0000439 return AS_none;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440 case eAccessPublic:
441 return AS_public;
442 case eAccessPrivate:
443 return AS_private;
444 case eAccessProtected:
445 return AS_protected;
446 }
447 return AS_none;
Greg Clayton8cf05932010-07-22 18:30:50 +0000448}
449
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
451 // FIXME: Cleanup per-file based stuff.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452
Adrian Prantl05097242018-04-30 16:49:04 +0000453 // Set some properties which depend solely on the input kind; it would be
454 // nice to move these to the language standard, and have the driver resolve
455 // the input kind + language standard.
Richard Smith8186cd42017-04-26 22:10:53 +0000456 if (IK.getLanguage() == InputKind::Asm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457 Opts.AsmPreprocessor = 1;
Richard Smith8186cd42017-04-26 22:10:53 +0000458 } else if (IK.isObjectiveC()) {
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000459 Opts.ObjC = 1;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 }
461
462 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
463
464 if (LangStd == LangStandard::lang_unspecified) {
465 // Based on the base language, pick one.
Richard Smith8186cd42017-04-26 22:10:53 +0000466 switch (IK.getLanguage()) {
467 case InputKind::Unknown:
468 case InputKind::LLVM_IR:
469 case InputKind::RenderScript:
David Blaikiea322f362017-01-06 00:38:06 +0000470 llvm_unreachable("Invalid input kind!");
Richard Smith8186cd42017-04-26 22:10:53 +0000471 case InputKind::OpenCL:
Pavel Labath47168542017-04-27 08:49:19 +0000472 LangStd = LangStandard::lang_opencl10;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000474 case InputKind::CUDA:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000475 LangStd = LangStandard::lang_cuda;
476 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000477 case InputKind::Asm:
478 case InputKind::C:
479 case InputKind::ObjC:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000480 LangStd = LangStandard::lang_gnu99;
481 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000482 case InputKind::CXX:
483 case InputKind::ObjCXX:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000484 LangStd = LangStandard::lang_gnucxx98;
485 break;
Benjamin Kramer0d97c222018-04-25 13:22:47 +0000486 case InputKind::HIP:
487 LangStd = LangStandard::lang_hip;
488 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
493 Opts.LineComment = Std.hasLineComments();
494 Opts.C99 = Std.isC99();
495 Opts.CPlusPlus = Std.isCPlusPlus();
496 Opts.CPlusPlus11 = Std.isCPlusPlus11();
497 Opts.Digraphs = Std.hasDigraphs();
498 Opts.GNUMode = Std.isGNUMode();
499 Opts.GNUInline = !Std.isC99();
500 Opts.HexFloats = Std.hasHexFloats();
501 Opts.ImplicitInt = Std.hasImplicitInt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000504
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505 // OpenCL has some additional defaults.
Pavel Labath47168542017-04-27 08:49:19 +0000506 if (LangStd == LangStandard::lang_opencl10) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000507 Opts.OpenCL = 1;
508 Opts.AltiVec = 1;
509 Opts.CXXOperatorNames = 1;
510 Opts.LaxVectorConversions = 1;
511 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 // OpenCL and C++ both have bool, true, false keywords.
514 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000517
Adrian Prantl05097242018-04-30 16:49:04 +0000518 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is
519 // specified, or -std is set to a conforming mode.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520 Opts.Trigraphs = !Opts.GNUMode;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000521 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000522 Opts.OptimizeSize = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524 // FIXME: Eliminate this dependency.
525 // unsigned Opt =
526 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
527 // Opts.Optimize = Opt != 0;
528 unsigned Opt = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000529
Kate Stoneb9c1b512016-09-06 20:57:50 +0000530 // This is the __NO_INLINE__ define, which just depends on things like the
531 // optimization level and -fno-inline, not actually whether the backend has
532 // inlining enabled.
533 //
534 // FIXME: This is affected by other options (-fno-inline).
535 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536}
537
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538ClangASTContext::ClangASTContext(const char *target_triple)
539 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_ap(),
540 m_language_options_ap(), m_source_manager_ap(), m_diagnostics_engine_ap(),
541 m_target_options_rp(), m_target_info_ap(), m_identifier_table_ap(),
542 m_selector_table_ap(), m_builtins_ap(), m_callback_tag_decl(nullptr),
543 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
544 m_pointer_byte_size(0), m_ast_owned(false) {
545 if (target_triple && target_triple[0])
546 SetTargetTriple(target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547}
548
549//----------------------------------------------------------------------
550// Destructor
551//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000552ClangASTContext::~ClangASTContext() { Finalize(); }
553
554ConstString ClangASTContext::GetPluginNameStatic() {
555 return ConstString("clang");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556}
557
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558ConstString ClangASTContext::GetPluginName() {
559 return ClangASTContext::GetPluginNameStatic();
Greg Clayton56939cb2015-09-17 22:23:34 +0000560}
561
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562uint32_t ClangASTContext::GetPluginVersion() { return 1; }
Greg Clayton56939cb2015-09-17 22:23:34 +0000563
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
565 lldb_private::Module *module,
566 Target *target) {
567 if (ClangASTContextSupportsLanguage(language)) {
568 ArchSpec arch;
569 if (module)
570 arch = module->GetArchitecture();
571 else if (target)
572 arch = target->GetArchitecture();
Greg Clayton56939cb2015-09-17 22:23:34 +0000573
Kate Stoneb9c1b512016-09-06 20:57:50 +0000574 if (arch.IsValid()) {
575 ArchSpec fixed_arch = arch;
576 // LLVM wants this to be set to iOS or MacOSX; if we're working on
577 // a bare-boards type image, change the triple for llvm's benefit.
578 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
579 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
580 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
581 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
582 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) {
590 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
591 if (ast_sp) {
592 ast_sp->SetArchitecture(fixed_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000593 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594 return ast_sp;
595 } else if (target && target->IsValid()) {
596 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
597 new ClangASTContextForExpressions(*target));
598 if (ast_sp) {
599 ast_sp->SetArchitecture(fixed_arch);
600 ast_sp->m_scratch_ast_source_ap.reset(
601 new ClangASTSource(target->shared_from_this()));
Sean Callanan68e44232017-09-28 20:20:25 +0000602 lldbassert(ast_sp->getFileManager());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000603 ast_sp->m_scratch_ast_source_ap->InstallASTContext(
Sean Callanan68e44232017-09-28 20:20:25 +0000604 *ast_sp->getASTContext(), *ast_sp->getFileManager(), true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000605 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
606 ast_sp->m_scratch_ast_source_ap->CreateProxy());
607 ast_sp->SetExternalSource(proxy_ast_source);
608 return ast_sp;
609 }
610 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612 }
613 return lldb::TypeSystemSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614}
615
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616void ClangASTContext::EnumerateSupportedLanguages(
617 std::set<lldb::LanguageType> &languages_for_types,
618 std::set<lldb::LanguageType> &languages_for_expressions) {
619 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
620 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
621 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
622 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
623 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
624 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
625
626 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
627 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
628 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
629 lldb::eLanguageTypeC_plus_plus_14});
630
631 languages_for_types.insert(s_supported_languages_for_types.begin(),
632 s_supported_languages_for_types.end());
633 languages_for_expressions.insert(
634 s_supported_languages_for_expressions.begin(),
635 s_supported_languages_for_expressions.end());
Enrico Granata5d84a692014-08-19 21:46:37 +0000636}
637
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638void ClangASTContext::Initialize() {
639 PluginManager::RegisterPlugin(GetPluginNameStatic(),
640 "clang base AST context plug-in",
641 CreateInstance, EnumerateSupportedLanguages);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000642}
643
Kate Stoneb9c1b512016-09-06 20:57:50 +0000644void ClangASTContext::Terminate() {
645 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646}
647
Kate Stoneb9c1b512016-09-06 20:57:50 +0000648void ClangASTContext::Finalize() {
649 if (m_ast_ap.get()) {
650 GetASTMap().Erase(m_ast_ap.get());
651 if (!m_ast_owned)
652 m_ast_ap.release();
653 }
654
655 m_builtins_ap.reset();
656 m_selector_table_ap.reset();
657 m_identifier_table_ap.reset();
658 m_target_info_ap.reset();
659 m_target_options_rp.reset();
660 m_diagnostics_engine_ap.reset();
661 m_source_manager_ap.reset();
662 m_language_options_ap.reset();
663 m_ast_ap.reset();
664 m_scratch_ast_source_ap.reset();
665}
666
667void ClangASTContext::Clear() {
668 m_ast_ap.reset();
669 m_language_options_ap.reset();
670 m_source_manager_ap.reset();
671 m_diagnostics_engine_ap.reset();
672 m_target_options_rp.reset();
673 m_target_info_ap.reset();
674 m_identifier_table_ap.reset();
675 m_selector_table_ap.reset();
676 m_builtins_ap.reset();
677 m_pointer_byte_size = 0;
678}
679
680const char *ClangASTContext::GetTargetTriple() {
681 return m_target_triple.c_str();
682}
683
684void ClangASTContext::SetTargetTriple(const char *target_triple) {
685 Clear();
686 m_target_triple.assign(target_triple);
687}
688
689void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
690 SetTargetTriple(arch.GetTriple().str().c_str());
691}
692
693bool ClangASTContext::HasExternalSource() {
694 ASTContext *ast = getASTContext();
695 if (ast)
696 return ast->getExternalSource() != nullptr;
697 return false;
698}
699
700void ClangASTContext::SetExternalSource(
701 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) {
702 ASTContext *ast = getASTContext();
703 if (ast) {
704 ast->setExternalSource(ast_source_ap);
705 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000706 }
707}
708
709void ClangASTContext::RemoveExternalSource() {
710 ASTContext *ast = getASTContext();
711
712 if (ast) {
713 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
714 ast->setExternalSource(empty_ast_source_ap);
715 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000716 }
717}
718
719void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
720 if (!m_ast_owned) {
721 m_ast_ap.release();
722 }
723 m_ast_owned = false;
724 m_ast_ap.reset(ast_ctx);
725 GetASTMap().Insert(ast_ctx, this);
726}
727
728ASTContext *ClangASTContext::getASTContext() {
729 if (m_ast_ap.get() == nullptr) {
730 m_ast_owned = true;
731 m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
732 *getIdentifierTable(), *getSelectorTable(),
733 *getBuiltinContext()));
734
735 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
736
737 // This can be NULL if we don't know anything about the architecture or if
Adrian Prantl05097242018-04-30 16:49:04 +0000738 // the target for an architecture isn't enabled in the llvm/clang that we
739 // built
Kate Stoneb9c1b512016-09-06 20:57:50 +0000740 TargetInfo *target_info = getTargetInfo();
741 if (target_info)
742 m_ast_ap->InitBuiltinTypes(*target_info);
743
744 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
745 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
746 // m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000748
749 GetASTMap().Insert(m_ast_ap.get(), this);
750
751 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap(
752 new ClangExternalASTSourceCallbacks(
753 ClangASTContext::CompleteTagDecl,
754 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
755 ClangASTContext::LayoutRecordType, this));
756 SetExternalSource(ast_source_ap);
757 }
758 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759}
760
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
762 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
763 return clang_ast;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000764}
765
Kate Stoneb9c1b512016-09-06 20:57:50 +0000766Builtin::Context *ClangASTContext::getBuiltinContext() {
767 if (m_builtins_ap.get() == nullptr)
768 m_builtins_ap.reset(new Builtin::Context());
769 return m_builtins_ap.get();
Sean Callanan79439e82010-11-18 02:56:27 +0000770}
771
Kate Stoneb9c1b512016-09-06 20:57:50 +0000772IdentifierTable *ClangASTContext::getIdentifierTable() {
773 if (m_identifier_table_ap.get() == nullptr)
774 m_identifier_table_ap.reset(
775 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
776 return m_identifier_table_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777}
778
Kate Stoneb9c1b512016-09-06 20:57:50 +0000779LangOptions *ClangASTContext::getLanguageOptions() {
780 if (m_language_options_ap.get() == nullptr) {
781 m_language_options_ap.reset(new LangOptions());
Richard Smith8186cd42017-04-26 22:10:53 +0000782 ParseLangArgs(*m_language_options_ap, InputKind::ObjCXX, GetTargetTriple());
783 // InitializeLangOptions(*m_language_options_ap, InputKind::ObjCXX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000784 }
785 return m_language_options_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000786}
787
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788SelectorTable *ClangASTContext::getSelectorTable() {
789 if (m_selector_table_ap.get() == nullptr)
790 m_selector_table_ap.reset(new SelectorTable());
791 return m_selector_table_ap.get();
Greg Claytonfe689042015-11-10 17:47:04 +0000792}
793
Kate Stoneb9c1b512016-09-06 20:57:50 +0000794clang::FileManager *ClangASTContext::getFileManager() {
795 if (m_file_manager_ap.get() == nullptr) {
796 clang::FileSystemOptions file_system_options;
797 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
798 }
799 return m_file_manager_ap.get();
800}
801
802clang::SourceManager *ClangASTContext::getSourceManager() {
803 if (m_source_manager_ap.get() == nullptr)
804 m_source_manager_ap.reset(
805 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
806 return m_source_manager_ap.get();
807}
808
809clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
810 if (m_diagnostics_engine_ap.get() == nullptr) {
811 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
812 m_diagnostics_engine_ap.reset(
813 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
814 }
815 return m_diagnostics_engine_ap.get();
816}
817
818clang::MangleContext *ClangASTContext::getMangleContext() {
819 if (m_mangle_ctx_ap.get() == nullptr)
820 m_mangle_ctx_ap.reset(getASTContext()->createMangleContext());
821 return m_mangle_ctx_ap.get();
822}
823
824class NullDiagnosticConsumer : public DiagnosticConsumer {
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000825public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000826 NullDiagnosticConsumer() {
827 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
828 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000829
Kate Stoneb9c1b512016-09-06 20:57:50 +0000830 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
831 const clang::Diagnostic &info) {
832 if (m_log) {
833 llvm::SmallVector<char, 32> diag_str(10);
834 info.FormatDiagnostic(diag_str);
835 diag_str.push_back('\0');
836 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000837 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838 }
839
840 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
841 return new NullDiagnosticConsumer();
842 }
843
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000844private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000845 Log *m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000846};
847
Kate Stoneb9c1b512016-09-06 20:57:50 +0000848DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
849 if (m_diagnostic_consumer_ap.get() == nullptr)
850 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
851
852 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000853}
854
Kate Stoneb9c1b512016-09-06 20:57:50 +0000855std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
856 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) {
857 m_target_options_rp = std::make_shared<clang::TargetOptions>();
858 if (m_target_options_rp.get() != nullptr)
859 m_target_options_rp->Triple = m_target_triple;
860 }
861 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000862}
863
Kate Stoneb9c1b512016-09-06 20:57:50 +0000864TargetInfo *ClangASTContext::getTargetInfo() {
865 // target_triple should be something like "x86_64-apple-macosx"
866 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
867 m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
868 getTargetOptions()));
869 return m_target_info_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000870}
871
872#pragma mark Basic Types
873
Kate Stoneb9c1b512016-09-06 20:57:50 +0000874static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
875 ASTContext *ast, QualType qual_type) {
876 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
877 if (qual_type_bit_size == bit_size)
878 return true;
879 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000880}
Greg Clayton56939cb2015-09-17 22:23:34 +0000881
Greg Claytona1e5dc82015-08-11 22:53:00 +0000882CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000883ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
884 size_t bit_size) {
885 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
886 getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000887}
888
Kate Stoneb9c1b512016-09-06 20:57:50 +0000889CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
890 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
891 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000892 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000893 switch (encoding) {
894 case eEncodingInvalid:
895 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
896 return CompilerType(ast, ast->VoidPtrTy);
897 break;
898
899 case eEncodingUint:
900 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
901 return CompilerType(ast, ast->UnsignedCharTy);
902 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
903 return CompilerType(ast, ast->UnsignedShortTy);
904 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
905 return CompilerType(ast, ast->UnsignedIntTy);
906 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
907 return CompilerType(ast, ast->UnsignedLongTy);
908 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
909 return CompilerType(ast, ast->UnsignedLongLongTy);
910 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
911 return CompilerType(ast, ast->UnsignedInt128Ty);
912 break;
913
914 case eEncodingSint:
915 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
916 return CompilerType(ast, ast->SignedCharTy);
917 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
918 return CompilerType(ast, ast->ShortTy);
919 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
920 return CompilerType(ast, ast->IntTy);
921 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
922 return CompilerType(ast, ast->LongTy);
923 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
924 return CompilerType(ast, ast->LongLongTy);
925 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
926 return CompilerType(ast, ast->Int128Ty);
927 break;
928
929 case eEncodingIEEE754:
930 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
931 return CompilerType(ast, ast->FloatTy);
932 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
933 return CompilerType(ast, ast->DoubleTy);
934 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
935 return CompilerType(ast, ast->LongDoubleTy);
936 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
937 return CompilerType(ast, ast->HalfTy);
938 break;
939
940 case eEncodingVector:
941 // Sanity check that bit_size is a multiple of 8's.
942 if (bit_size && !(bit_size & 0x7u))
943 return CompilerType(
944 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
945 break;
946 }
947
948 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000949}
950
Greg Clayton57ee3062013-07-11 22:46:58 +0000951lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000952ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) {
953 if (name) {
954 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
955 static TypeNameToBasicTypeMap g_type_map;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000956 static llvm::once_flag g_once_flag;
957 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000958 // "void"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000959 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000960
961 // "char"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000962 g_type_map.Append(ConstString("char"), eBasicTypeChar);
963 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
964 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
965 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
966 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
967 g_type_map.Append(ConstString("unsigned wchar_t"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968 eBasicTypeUnsignedWChar);
969 // "short"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000970 g_type_map.Append(ConstString("short"), eBasicTypeShort);
971 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
972 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
973 g_type_map.Append(ConstString("unsigned short int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974 eBasicTypeUnsignedShort);
975
976 // "int"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000977 g_type_map.Append(ConstString("int"), eBasicTypeInt);
978 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
979 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
980 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981
982 // "long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000983 g_type_map.Append(ConstString("long"), eBasicTypeLong);
984 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
985 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
986 g_type_map.Append(ConstString("unsigned long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000987 eBasicTypeUnsignedLong);
988
989 // "long long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000990 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
991 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
992 g_type_map.Append(ConstString("unsigned long long"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000993 eBasicTypeUnsignedLongLong);
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000994 g_type_map.Append(ConstString("unsigned long long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000995 eBasicTypeUnsignedLongLong);
996
997 // "int128"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000998 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
999 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001000
1001 // Miscellaneous
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001002 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
1003 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
1004 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
1005 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
1006 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
1007 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
1008 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009 g_type_map.Sort();
1010 });
1011
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001012 return g_type_map.Find(name, eBasicTypeInvalid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001013 }
1014 return eBasicTypeInvalid;
Greg Clayton57ee3062013-07-11 22:46:58 +00001015}
1016
Kate Stoneb9c1b512016-09-06 20:57:50 +00001017CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1018 const ConstString &name) {
1019 if (ast) {
1020 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1021 return ClangASTContext::GetBasicType(ast, basic_type);
1022 }
1023 return CompilerType();
1024}
1025
1026uint32_t ClangASTContext::GetPointerByteSize() {
1027 if (m_pointer_byte_size == 0)
1028 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid)
1029 .GetPointerType()
1030 .GetByteSize(nullptr);
1031 return m_pointer_byte_size;
1032}
1033
1034CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1035 return GetBasicType(getASTContext(), basic_type);
1036}
1037
1038CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1039 lldb::BasicType basic_type) {
1040 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001041 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001042 lldb::opaque_compiler_type_t clang_type =
1043 GetOpaqueCompilerType(ast, basic_type);
1044
1045 if (clang_type)
1046 return CompilerType(GetASTContext(ast), clang_type);
1047 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001048}
1049
Kate Stoneb9c1b512016-09-06 20:57:50 +00001050CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1051 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1052 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001053
Kate Stoneb9c1b512016-09-06 20:57:50 +00001054#define streq(a, b) strcmp(a, b) == 0
1055 assert(ast != nullptr);
1056 if (ast) {
1057 switch (dw_ate) {
1058 default:
1059 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001060
Kate Stoneb9c1b512016-09-06 20:57:50 +00001061 case DW_ATE_address:
1062 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1063 return CompilerType(ast, ast->VoidPtrTy);
1064 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001065
Kate Stoneb9c1b512016-09-06 20:57:50 +00001066 case DW_ATE_boolean:
1067 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
1068 return CompilerType(ast, ast->BoolTy);
1069 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1070 return CompilerType(ast, ast->UnsignedCharTy);
1071 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1072 return CompilerType(ast, ast->UnsignedShortTy);
1073 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1074 return CompilerType(ast, ast->UnsignedIntTy);
1075 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001076
Kate Stoneb9c1b512016-09-06 20:57:50 +00001077 case DW_ATE_lo_user:
1078 // This has been seen to mean DW_AT_complex_integer
1079 if (type_name) {
1080 if (::strstr(type_name, "complex")) {
1081 CompilerType complex_int_clang_type =
1082 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1083 bit_size / 2);
1084 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1085 complex_int_clang_type)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001086 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001087 }
1088 break;
1089
1090 case DW_ATE_complex_float:
1091 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1092 return CompilerType(ast, ast->FloatComplexTy);
1093 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1094 return CompilerType(ast, ast->DoubleComplexTy);
1095 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1096 return CompilerType(ast, ast->LongDoubleComplexTy);
1097 else {
1098 CompilerType complex_float_clang_type =
1099 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1100 bit_size / 2);
1101 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1102 complex_float_clang_type)));
1103 }
1104 break;
1105
1106 case DW_ATE_float:
1107 if (streq(type_name, "float") &&
1108 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1109 return CompilerType(ast, ast->FloatTy);
1110 if (streq(type_name, "double") &&
1111 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1112 return CompilerType(ast, ast->DoubleTy);
1113 if (streq(type_name, "long double") &&
1114 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1115 return CompilerType(ast, ast->LongDoubleTy);
1116 // Fall back to not requiring a name match
1117 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1118 return CompilerType(ast, ast->FloatTy);
1119 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1120 return CompilerType(ast, ast->DoubleTy);
1121 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1122 return CompilerType(ast, ast->LongDoubleTy);
1123 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1124 return CompilerType(ast, ast->HalfTy);
1125 break;
1126
1127 case DW_ATE_signed:
1128 if (type_name) {
1129 if (streq(type_name, "wchar_t") &&
1130 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1131 (getTargetInfo() &&
1132 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1133 return CompilerType(ast, ast->WCharTy);
1134 if (streq(type_name, "void") &&
1135 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1136 return CompilerType(ast, ast->VoidTy);
1137 if (strstr(type_name, "long long") &&
1138 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1139 return CompilerType(ast, ast->LongLongTy);
1140 if (strstr(type_name, "long") &&
1141 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1142 return CompilerType(ast, ast->LongTy);
1143 if (strstr(type_name, "short") &&
1144 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1145 return CompilerType(ast, ast->ShortTy);
1146 if (strstr(type_name, "char")) {
1147 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1148 return CompilerType(ast, ast->CharTy);
1149 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1150 return CompilerType(ast, ast->SignedCharTy);
1151 }
1152 if (strstr(type_name, "int")) {
1153 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1154 return CompilerType(ast, ast->IntTy);
1155 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1156 return CompilerType(ast, ast->Int128Ty);
1157 }
1158 }
1159 // We weren't able to match up a type name, just search by size
1160 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1161 return CompilerType(ast, ast->CharTy);
1162 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1163 return CompilerType(ast, ast->ShortTy);
1164 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1165 return CompilerType(ast, ast->IntTy);
1166 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1167 return CompilerType(ast, ast->LongTy);
1168 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1169 return CompilerType(ast, ast->LongLongTy);
1170 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1171 return CompilerType(ast, ast->Int128Ty);
1172 break;
1173
1174 case DW_ATE_signed_char:
1175 if (ast->getLangOpts().CharIsSigned && type_name &&
1176 streq(type_name, "char")) {
1177 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1178 return CompilerType(ast, ast->CharTy);
1179 }
1180 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1181 return CompilerType(ast, ast->SignedCharTy);
1182 break;
1183
1184 case DW_ATE_unsigned:
1185 if (type_name) {
1186 if (streq(type_name, "wchar_t")) {
1187 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1188 if (!(getTargetInfo() &&
1189 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1190 return CompilerType(ast, ast->WCharTy);
1191 }
1192 }
1193 if (strstr(type_name, "long long")) {
1194 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1195 return CompilerType(ast, ast->UnsignedLongLongTy);
1196 } else if (strstr(type_name, "long")) {
1197 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1198 return CompilerType(ast, ast->UnsignedLongTy);
1199 } else if (strstr(type_name, "short")) {
1200 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1201 return CompilerType(ast, ast->UnsignedShortTy);
1202 } else if (strstr(type_name, "char")) {
1203 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1204 return CompilerType(ast, ast->UnsignedCharTy);
1205 } else if (strstr(type_name, "int")) {
1206 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1207 return CompilerType(ast, ast->UnsignedIntTy);
1208 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1209 return CompilerType(ast, ast->UnsignedInt128Ty);
1210 }
1211 }
1212 // We weren't able to match up a type name, just search by size
1213 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1214 return CompilerType(ast, ast->UnsignedCharTy);
1215 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1216 return CompilerType(ast, ast->UnsignedShortTy);
1217 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1218 return CompilerType(ast, ast->UnsignedIntTy);
1219 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1220 return CompilerType(ast, ast->UnsignedLongTy);
1221 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1222 return CompilerType(ast, ast->UnsignedLongLongTy);
1223 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1224 return CompilerType(ast, ast->UnsignedInt128Ty);
1225 break;
1226
1227 case DW_ATE_unsigned_char:
1228 if (!ast->getLangOpts().CharIsSigned && type_name &&
1229 streq(type_name, "char")) {
1230 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1231 return CompilerType(ast, ast->CharTy);
1232 }
1233 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1234 return CompilerType(ast, ast->UnsignedCharTy);
1235 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1236 return CompilerType(ast, ast->UnsignedShortTy);
1237 break;
1238
1239 case DW_ATE_imaginary_float:
1240 break;
1241
1242 case DW_ATE_UTF:
1243 if (type_name) {
1244 if (streq(type_name, "char16_t")) {
1245 return CompilerType(ast, ast->Char16Ty);
1246 } else if (streq(type_name, "char32_t")) {
1247 return CompilerType(ast, ast->Char32Ty);
1248 }
1249 }
1250 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001251 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001252 }
1253 // This assert should fire for anything that we don't catch above so we know
1254 // to fix any issues we run into.
1255 if (type_name) {
1256 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1257 "DW_TAG_base_type '%s' encoded with "
1258 "DW_ATE = 0x%x, bit_size = %u\n",
1259 type_name, dw_ate, bit_size);
1260 } else {
1261 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1262 "DW_TAG_base_type encoded with "
1263 "DW_ATE = 0x%x, bit_size = %u\n",
1264 dw_ate, bit_size);
1265 }
1266 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001267}
1268
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1270 if (ast)
1271 return CompilerType(ast, ast->UnknownAnyTy);
1272 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001273}
1274
Kate Stoneb9c1b512016-09-06 20:57:50 +00001275CompilerType ClangASTContext::GetCStringType(bool is_const) {
1276 ASTContext *ast = getASTContext();
1277 QualType char_type(ast->CharTy);
1278
1279 if (is_const)
1280 char_type.addConst();
1281
1282 return CompilerType(ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001283}
1284
Zachary Turner115209e2018-11-05 19:25:39 +00001285clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001286ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1287 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001288}
1289
Kate Stoneb9c1b512016-09-06 20:57:50 +00001290clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1291 clang::Decl *source_decl) {
1292 FileSystemOptions file_system_options;
1293 FileManager file_manager(file_system_options);
1294 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1295
1296 return importer.Import(source_decl);
Greg Clayton526e5af2010-11-13 03:52:47 +00001297}
1298
Kate Stoneb9c1b512016-09-06 20:57:50 +00001299bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1300 bool ignore_qualifiers) {
1301 ClangASTContext *ast =
1302 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1303 if (!ast || ast != type2.GetTypeSystem())
1304 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001305
Kate Stoneb9c1b512016-09-06 20:57:50 +00001306 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1307 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001308
Kate Stoneb9c1b512016-09-06 20:57:50 +00001309 QualType type1_qual = ClangUtil::GetQualType(type1);
1310 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001311
Kate Stoneb9c1b512016-09-06 20:57:50 +00001312 if (ignore_qualifiers) {
1313 type1_qual = type1_qual.getUnqualifiedType();
1314 type2_qual = type2_qual.getUnqualifiedType();
1315 }
1316
1317 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001318}
1319
Kate Stoneb9c1b512016-09-06 20:57:50 +00001320CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1321 if (clang::ObjCInterfaceDecl *interface_decl =
1322 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1323 return GetTypeForDecl(interface_decl);
1324 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1325 return GetTypeForDecl(tag_decl);
1326 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001327}
1328
Kate Stoneb9c1b512016-09-06 20:57:50 +00001329CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001330 // No need to call the getASTContext() accessor (which can create the AST if
1331 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001332 // AST if our AST didn't already exist...
1333 ASTContext *ast = &decl->getASTContext();
1334 if (ast)
1335 return CompilerType(ast, ast->getTagDeclType(decl));
1336 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001337}
1338
Kate Stoneb9c1b512016-09-06 20:57:50 +00001339CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001340 // No need to call the getASTContext() accessor (which can create the AST if
1341 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001342 // AST if our AST didn't already exist...
1343 ASTContext *ast = &decl->getASTContext();
1344 if (ast)
1345 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1346 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001347}
1348
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001349#pragma mark Structure, Unions, Classes
1350
Kate Stoneb9c1b512016-09-06 20:57:50 +00001351CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1352 AccessType access_type,
1353 const char *name, int kind,
1354 LanguageType language,
1355 ClangASTMetadata *metadata) {
1356 ASTContext *ast = getASTContext();
1357 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358
Kate Stoneb9c1b512016-09-06 20:57:50 +00001359 if (decl_ctx == nullptr)
1360 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001361
Kate Stoneb9c1b512016-09-06 20:57:50 +00001362 if (language == eLanguageTypeObjC ||
1363 language == eLanguageTypeObjC_plus_plus) {
1364 bool isForwardDecl = true;
1365 bool isInternal = false;
1366 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1367 }
Greg Clayton9e409562010-07-28 02:04:09 +00001368
Kate Stoneb9c1b512016-09-06 20:57:50 +00001369 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
Adrian Prantl05097242018-04-30 16:49:04 +00001370 // we will need to update this code. I was told to currently always use the
1371 // CXXRecordDecl class since we often don't know from debug information if
1372 // something is struct or a class, so we default to always use the more
Kate Stoneb9c1b512016-09-06 20:57:50 +00001373 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001374
Kate Stoneb9c1b512016-09-06 20:57:50 +00001375 bool is_anonymous = (!name) || (!name[0]);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001376
Kate Stoneb9c1b512016-09-06 20:57:50 +00001377 CXXRecordDecl *decl = CXXRecordDecl::Create(
1378 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1379 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1380
1381 if (is_anonymous)
1382 decl->setAnonymousStructOrUnion(true);
1383
1384 if (decl) {
1385 if (metadata)
1386 SetMetadata(ast, decl, *metadata);
1387
1388 if (access_type != eAccessNone)
1389 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1390
1391 if (decl_ctx)
1392 decl_ctx->addDecl(decl);
1393
1394 return CompilerType(ast, ast->getTagDeclType(decl));
1395 }
1396 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001397}
1398
Sean Callanan09e91ac2017-05-11 22:08:05 +00001399namespace {
1400 bool IsValueParam(const clang::TemplateArgument &argument) {
1401 return argument.getKind() == TemplateArgument::Integral;
1402 }
1403}
1404
Kate Stoneb9c1b512016-09-06 20:57:50 +00001405static TemplateParameterList *CreateTemplateParameterList(
1406 ASTContext *ast,
1407 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1408 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1409 const bool parameter_pack = false;
1410 const bool is_typename = false;
1411 const unsigned depth = 0;
Sean Callanan09e91ac2017-05-11 22:08:05 +00001412 const size_t num_template_params = template_param_infos.args.size();
1413 DeclContext *const decl_context =
1414 ast->getTranslationUnitDecl(); // Is this the right decl context?,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001415 for (size_t i = 0; i < num_template_params; ++i) {
1416 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001417
Kate Stoneb9c1b512016-09-06 20:57:50 +00001418 IdentifierInfo *identifier_info = nullptr;
1419 if (name && name[0])
1420 identifier_info = &ast->Idents.get(name);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001421 if (IsValueParam(template_param_infos.args[i])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001422 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001423 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001424 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1425 template_param_infos.args[i].getIntegralType(), parameter_pack,
1426 nullptr));
1427
1428 } else {
1429 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001430 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001431 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1432 is_typename, parameter_pack));
1433 }
1434 }
Eugene Zemtsova9d928c2017-09-29 03:15:08 +00001435
Sean Callanan09e91ac2017-05-11 22:08:05 +00001436 if (template_param_infos.packed_args &&
1437 template_param_infos.packed_args->args.size()) {
1438 IdentifierInfo *identifier_info = nullptr;
1439 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1440 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1441 const bool parameter_pack_true = true;
1442 if (IsValueParam(template_param_infos.packed_args->args[0])) {
1443 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1444 *ast, decl_context,
1445 SourceLocation(), SourceLocation(), depth, num_template_params,
1446 identifier_info,
1447 template_param_infos.packed_args->args[0].getIntegralType(),
1448 parameter_pack_true, nullptr));
1449 } else {
1450 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1451 *ast, decl_context,
1452 SourceLocation(), SourceLocation(), depth, num_template_params,
1453 identifier_info,
1454 is_typename, parameter_pack_true));
1455 }
1456 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001457 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1458 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1459 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1460 SourceLocation(), requires_clause);
1461 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001462}
1463
Kate Stoneb9c1b512016-09-06 20:57:50 +00001464clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1465 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1466 const char *name, const TemplateParameterInfos &template_param_infos) {
Adrian Prantld8f460e2018-05-02 16:55:16 +00001467 // /// Create a function template node.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001468 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001469
Kate Stoneb9c1b512016-09-06 20:57:50 +00001470 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001471
Kate Stoneb9c1b512016-09-06 20:57:50 +00001472 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1473 ast, template_param_infos, template_param_decls);
1474 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1475 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1476 template_param_list, func_decl);
1477
1478 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1479 i < template_param_decl_count; ++i) {
1480 // TODO: verify which decl context we should put template_param_decls into..
1481 template_param_decls[i]->setDeclContext(func_decl);
1482 }
1483
1484 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001485}
1486
Kate Stoneb9c1b512016-09-06 20:57:50 +00001487void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1488 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1489 const TemplateParameterInfos &infos) {
1490 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001491
Kate Stoneb9c1b512016-09-06 20:57:50 +00001492 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1493 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001494}
1495
Kate Stoneb9c1b512016-09-06 20:57:50 +00001496ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1497 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1498 int kind, const TemplateParameterInfos &template_param_infos) {
1499 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001500
Kate Stoneb9c1b512016-09-06 20:57:50 +00001501 ClassTemplateDecl *class_template_decl = nullptr;
1502 if (decl_ctx == nullptr)
1503 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001504
Kate Stoneb9c1b512016-09-06 20:57:50 +00001505 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1506 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001507
Kate Stoneb9c1b512016-09-06 20:57:50 +00001508 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001509
Kate Stoneb9c1b512016-09-06 20:57:50 +00001510 for (NamedDecl *decl : result) {
1511 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001512 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001513 return class_template_decl;
1514 }
1515
1516 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1517
1518 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1519 ast, template_param_infos, template_param_decls);
1520
1521 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1522 *ast, (TagDecl::TagKind)kind,
1523 decl_ctx, // What decl context do we use here? TU? The actual decl
1524 // context?
1525 SourceLocation(), SourceLocation(), &identifier_info);
1526
1527 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1528 i < template_param_decl_count; ++i) {
1529 template_param_decls[i]->setDeclContext(template_cxx_decl);
1530 }
1531
1532 // With templated classes, we say that a class is templated with
1533 // specializations, but that the bare class has no functions.
1534 // template_cxx_decl->startDefinition();
1535 // template_cxx_decl->completeDefinition();
1536
1537 class_template_decl = ClassTemplateDecl::Create(
1538 *ast,
1539 decl_ctx, // What decl context do we use here? TU? The actual decl
1540 // context?
Pavel Labath4294de32017-01-12 10:44:16 +00001541 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001542
1543 if (class_template_decl) {
1544 if (access_type != eAccessNone)
1545 class_template_decl->setAccess(
1546 ConvertAccessTypeToAccessSpecifier(access_type));
1547
1548 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1549 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1550
1551 decl_ctx->addDecl(class_template_decl);
1552
Sean Callanan5e9e1992011-10-26 01:06:27 +00001553#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001554 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001555#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001556 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001557
Kate Stoneb9c1b512016-09-06 20:57:50 +00001558 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001559}
1560
Frederic Rissf4e7e522018-04-02 16:18:32 +00001561TemplateTemplateParmDecl *
1562ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) {
1563 ASTContext *ast = getASTContext();
1564
1565 auto *decl_ctx = ast->getTranslationUnitDecl();
1566
1567 IdentifierInfo &identifier_info = ast->Idents.get(template_name);
1568 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1569
1570 ClangASTContext::TemplateParameterInfos template_param_infos;
1571 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1572 ast, template_param_infos, template_param_decls);
1573
1574 // LLDB needs to create those decls only to be able to display a
Adrian Prantl05097242018-04-30 16:49:04 +00001575 // type that includes a template template argument. Only the name matters for
1576 // this purpose, so we use dummy values for the other characterisitcs of the
1577 // type.
Frederic Rissf4e7e522018-04-02 16:18:32 +00001578 return TemplateTemplateParmDecl::Create(
1579 *ast, decl_ctx, SourceLocation(),
1580 /*Depth*/ 0, /*Position*/ 0,
1581 /*IsParameterPack*/ false, &identifier_info, template_param_list);
1582}
1583
Greg Claytonf0705c82011-10-22 03:33:13 +00001584ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001585ClangASTContext::CreateClassTemplateSpecializationDecl(
1586 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1587 const TemplateParameterInfos &template_param_infos) {
1588 ASTContext *ast = getASTContext();
Sean Callanan09e91ac2017-05-11 22:08:05 +00001589 llvm::SmallVector<clang::TemplateArgument, 2> args(
1590 template_param_infos.args.size() +
1591 (template_param_infos.packed_args ? 1 : 0));
1592 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1593 args.begin());
1594 if (template_param_infos.packed_args) {
1595 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1596 *ast, template_param_infos.packed_args->args);
1597 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001598 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1599 ClassTemplateSpecializationDecl::Create(
1600 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Sean Callanan09e91ac2017-05-11 22:08:05 +00001601 SourceLocation(), class_template_decl, args,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001602 nullptr);
1603
1604 class_template_specialization_decl->setSpecializationKind(
1605 TSK_ExplicitSpecialization);
1606
1607 return class_template_specialization_decl;
1608}
1609
1610CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1611 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1612 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001613 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001614 if (ast)
1615 return CompilerType(
1616 ast, ast->getTagDeclType(class_template_specialization_decl));
1617 }
1618 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001619}
1620
Kate Stoneb9c1b512016-09-06 20:57:50 +00001621static inline bool check_op_param(bool is_method,
1622 clang::OverloadedOperatorKind op_kind,
1623 bool unary, bool binary,
1624 uint32_t num_params) {
1625 // Special-case call since it can take any number of operands
1626 if (op_kind == OO_Call)
1627 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001628
Kate Stoneb9c1b512016-09-06 20:57:50 +00001629 // The parameter count doesn't include "this"
1630 if (is_method)
1631 ++num_params;
1632 if (num_params == 1)
1633 return unary;
1634 if (num_params == 2)
1635 return binary;
1636 else
Greg Clayton090d0982011-06-19 03:43:27 +00001637 return false;
1638}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001639
Kate Stoneb9c1b512016-09-06 20:57:50 +00001640bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1641 bool is_method, clang::OverloadedOperatorKind op_kind,
1642 uint32_t num_params) {
1643 switch (op_kind) {
1644 default:
1645 break;
1646 // C++ standard allows any number of arguments to new/delete
1647 case OO_New:
1648 case OO_Array_New:
1649 case OO_Delete:
1650 case OO_Array_Delete:
1651 return true;
1652 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001653
Kate Stoneb9c1b512016-09-06 20:57:50 +00001654#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1655 case OO_##Name: \
1656 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1657 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001658#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001659 default:
1660 break;
1661 }
1662 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001663}
1664
Greg Clayton57ee3062013-07-11 22:46:58 +00001665clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001666ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1667 clang::AccessSpecifier rhs) {
1668 // Make the access equal to the stricter of the field and the nested field's
1669 // access
1670 if (lhs == AS_none || rhs == AS_none)
1671 return AS_none;
1672 if (lhs == AS_private || rhs == AS_private)
1673 return AS_private;
1674 if (lhs == AS_protected || rhs == AS_protected)
1675 return AS_protected;
1676 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001677}
1678
Kate Stoneb9c1b512016-09-06 20:57:50 +00001679bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1680 uint32_t &bitfield_bit_size) {
1681 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001682}
1683
Kate Stoneb9c1b512016-09-06 20:57:50 +00001684bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1685 uint32_t &bitfield_bit_size) {
1686 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001687 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001688
Kate Stoneb9c1b512016-09-06 20:57:50 +00001689 if (field->isBitField()) {
1690 Expr *bit_width_expr = field->getBitWidth();
1691 if (bit_width_expr) {
1692 llvm::APSInt bit_width_apsint;
1693 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1694 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001695 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001696 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001697 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001698 }
1699 return false;
1700}
1701
1702bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1703 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001704 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001705
1706 if (!record_decl->field_empty())
1707 return true;
1708
1709 // No fields, lets check this is a CXX record and check the base classes
1710 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1711 if (cxx_record_decl) {
1712 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1713 for (base_class = cxx_record_decl->bases_begin(),
1714 base_class_end = cxx_record_decl->bases_end();
1715 base_class != base_class_end; ++base_class) {
1716 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1717 base_class->getType()->getAs<RecordType>()->getDecl());
1718 if (RecordHasFields(base_class_decl))
1719 return true;
1720 }
1721 }
1722 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001723}
1724
Adrian Prantl4e8be2c2018-06-13 16:21:24 +00001725#pragma mark Objective-C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001726
Kate Stoneb9c1b512016-09-06 20:57:50 +00001727CompilerType ClangASTContext::CreateObjCClass(const char *name,
1728 DeclContext *decl_ctx,
1729 bool isForwardDecl,
1730 bool isInternal,
1731 ClangASTMetadata *metadata) {
1732 ASTContext *ast = getASTContext();
1733 assert(ast != nullptr);
1734 assert(name && name[0]);
1735 if (decl_ctx == nullptr)
1736 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001737
Kate Stoneb9c1b512016-09-06 20:57:50 +00001738 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1739 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1740 nullptr, SourceLocation(),
1741 /*isForwardDecl,*/
1742 isInternal);
1743
1744 if (decl && metadata)
1745 SetMetadata(ast, decl, *metadata);
1746
1747 return CompilerType(ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001748}
1749
Kate Stoneb9c1b512016-09-06 20:57:50 +00001750static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1751 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) ==
1752 false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001753}
1754
Greg Clayton57ee3062013-07-11 22:46:58 +00001755uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001756ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1757 bool omit_empty_base_classes) {
1758 uint32_t num_bases = 0;
1759 if (cxx_record_decl) {
1760 if (omit_empty_base_classes) {
1761 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1762 for (base_class = cxx_record_decl->bases_begin(),
1763 base_class_end = cxx_record_decl->bases_end();
1764 base_class != base_class_end; ++base_class) {
1765 // Skip empty base classes
1766 if (omit_empty_base_classes) {
1767 if (BaseSpecifierIsEmpty(base_class))
1768 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001769 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001770 ++num_bases;
1771 }
1772 } else
1773 num_bases = cxx_record_decl->getNumBases();
1774 }
1775 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001776}
1777
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001778#pragma mark Namespace Declarations
1779
1780NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001781ClangASTContext::GetUniqueNamespaceDeclaration(const char *name,
1782 DeclContext *decl_ctx) {
1783 NamespaceDecl *namespace_decl = nullptr;
1784 ASTContext *ast = getASTContext();
1785 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1786 if (decl_ctx == nullptr)
1787 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001788
Kate Stoneb9c1b512016-09-06 20:57:50 +00001789 if (name) {
1790 IdentifierInfo &identifier_info = ast->Idents.get(name);
1791 DeclarationName decl_name(&identifier_info);
1792 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1793 for (NamedDecl *decl : result) {
1794 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1795 if (namespace_decl)
1796 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001797 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001798
1799 namespace_decl =
1800 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1801 SourceLocation(), &identifier_info, nullptr);
1802
1803 decl_ctx->addDecl(namespace_decl);
1804 } else {
1805 if (decl_ctx == translation_unit_decl) {
1806 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1807 if (namespace_decl)
1808 return namespace_decl;
1809
1810 namespace_decl =
1811 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1812 SourceLocation(), nullptr, nullptr);
1813 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1814 translation_unit_decl->addDecl(namespace_decl);
1815 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1816 } else {
1817 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1818 if (parent_namespace_decl) {
1819 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1820 if (namespace_decl)
1821 return namespace_decl;
1822 namespace_decl =
1823 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1824 SourceLocation(), nullptr, nullptr);
1825 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1826 parent_namespace_decl->addDecl(namespace_decl);
1827 assert(namespace_decl ==
1828 parent_namespace_decl->getAnonymousNamespace());
1829 } else {
1830 // BAD!!!
1831 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001832 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001833 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001834#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001835 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001836#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001837 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001838}
1839
Kate Stoneb9c1b512016-09-06 20:57:50 +00001840NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1841 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) {
1842 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1843 if (ast_ctx == nullptr)
1844 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +00001845
Kate Stoneb9c1b512016-09-06 20:57:50 +00001846 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
Siva Chandra03ff5c82016-02-05 19:10:04 +00001847}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001848
Paul Hermand628cbb2015-09-15 23:44:17 +00001849clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001850ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1851 if (ctx != nullptr) {
1852 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1853 clang::SourceLocation());
1854 ctx->addDecl(decl);
1855 return decl;
1856 }
1857 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001858}
1859
Kate Stoneb9c1b512016-09-06 20:57:50 +00001860clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1861 clang::DeclContext *right,
1862 clang::DeclContext *root) {
1863 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001864 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001865
1866 std::set<clang::DeclContext *> path_left;
1867 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1868 path_left.insert(d);
1869
1870 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1871 if (path_left.find(d) != path_left.end())
1872 return d;
1873
1874 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00001875}
1876
Kate Stoneb9c1b512016-09-06 20:57:50 +00001877clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1878 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1879 if (decl_ctx != nullptr && ns_decl != nullptr) {
1880 clang::TranslationUnitDecl *translation_unit =
1881 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1882 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1883 *getASTContext(), decl_ctx, clang::SourceLocation(),
1884 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1885 clang::SourceLocation(), ns_decl,
1886 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
1887 decl_ctx->addDecl(using_decl);
1888 return using_decl;
1889 }
1890 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001891}
1892
1893clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001894ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1895 clang::NamedDecl *target) {
1896 if (current_decl_ctx != nullptr && target != nullptr) {
1897 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1898 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
1899 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1900 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1901 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
1902 target);
1903 using_decl->addShadowDecl(shadow_decl);
1904 current_decl_ctx->addDecl(using_decl);
1905 return using_decl;
1906 }
1907 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001908}
1909
Kate Stoneb9c1b512016-09-06 20:57:50 +00001910clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
1911 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
1912 if (decl_context != nullptr) {
1913 clang::VarDecl *var_decl = clang::VarDecl::Create(
1914 *getASTContext(), decl_context, clang::SourceLocation(),
1915 clang::SourceLocation(),
1916 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
1917 nullptr, clang::SC_None);
1918 var_decl->setAccess(clang::AS_public);
1919 decl_context->addDecl(var_decl);
1920 return var_decl;
1921 }
1922 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001923}
1924
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001925lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001926ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
1927 lldb::BasicType basic_type) {
1928 switch (basic_type) {
1929 case eBasicTypeVoid:
1930 return ast->VoidTy.getAsOpaquePtr();
1931 case eBasicTypeChar:
1932 return ast->CharTy.getAsOpaquePtr();
1933 case eBasicTypeSignedChar:
1934 return ast->SignedCharTy.getAsOpaquePtr();
1935 case eBasicTypeUnsignedChar:
1936 return ast->UnsignedCharTy.getAsOpaquePtr();
1937 case eBasicTypeWChar:
1938 return ast->getWCharType().getAsOpaquePtr();
1939 case eBasicTypeSignedWChar:
1940 return ast->getSignedWCharType().getAsOpaquePtr();
1941 case eBasicTypeUnsignedWChar:
1942 return ast->getUnsignedWCharType().getAsOpaquePtr();
1943 case eBasicTypeChar16:
1944 return ast->Char16Ty.getAsOpaquePtr();
1945 case eBasicTypeChar32:
1946 return ast->Char32Ty.getAsOpaquePtr();
1947 case eBasicTypeShort:
1948 return ast->ShortTy.getAsOpaquePtr();
1949 case eBasicTypeUnsignedShort:
1950 return ast->UnsignedShortTy.getAsOpaquePtr();
1951 case eBasicTypeInt:
1952 return ast->IntTy.getAsOpaquePtr();
1953 case eBasicTypeUnsignedInt:
1954 return ast->UnsignedIntTy.getAsOpaquePtr();
1955 case eBasicTypeLong:
1956 return ast->LongTy.getAsOpaquePtr();
1957 case eBasicTypeUnsignedLong:
1958 return ast->UnsignedLongTy.getAsOpaquePtr();
1959 case eBasicTypeLongLong:
1960 return ast->LongLongTy.getAsOpaquePtr();
1961 case eBasicTypeUnsignedLongLong:
1962 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1963 case eBasicTypeInt128:
1964 return ast->Int128Ty.getAsOpaquePtr();
1965 case eBasicTypeUnsignedInt128:
1966 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1967 case eBasicTypeBool:
1968 return ast->BoolTy.getAsOpaquePtr();
1969 case eBasicTypeHalf:
1970 return ast->HalfTy.getAsOpaquePtr();
1971 case eBasicTypeFloat:
1972 return ast->FloatTy.getAsOpaquePtr();
1973 case eBasicTypeDouble:
1974 return ast->DoubleTy.getAsOpaquePtr();
1975 case eBasicTypeLongDouble:
1976 return ast->LongDoubleTy.getAsOpaquePtr();
1977 case eBasicTypeFloatComplex:
1978 return ast->FloatComplexTy.getAsOpaquePtr();
1979 case eBasicTypeDoubleComplex:
1980 return ast->DoubleComplexTy.getAsOpaquePtr();
1981 case eBasicTypeLongDoubleComplex:
1982 return ast->LongDoubleComplexTy.getAsOpaquePtr();
1983 case eBasicTypeObjCID:
1984 return ast->getObjCIdType().getAsOpaquePtr();
1985 case eBasicTypeObjCClass:
1986 return ast->getObjCClassType().getAsOpaquePtr();
1987 case eBasicTypeObjCSel:
1988 return ast->getObjCSelType().getAsOpaquePtr();
1989 case eBasicTypeNullPtr:
1990 return ast->NullPtrTy.getAsOpaquePtr();
1991 default:
1992 return nullptr;
1993 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001994}
1995
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001996#pragma mark Function Types
1997
Pavel Labath1ac2b202016-08-15 14:32:32 +00001998clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00001999ClangASTContext::GetDeclarationName(const char *name,
2000 const CompilerType &function_clang_type) {
2001 if (!name || !name[0])
2002 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002003
Kate Stoneb9c1b512016-09-06 20:57:50 +00002004 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
2005 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
2006 return DeclarationName(&getASTContext()->Idents.get(
2007 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00002008
Adrian Prantl05097242018-04-30 16:49:04 +00002009 // Check the number of operator parameters. Sometimes we have seen bad DWARF
2010 // that doesn't correctly describe operators and if we try to create a method
2011 // and add it to the class, clang will assert and crash, so we need to make
2012 // sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002013 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
2014 const clang::FunctionProtoType *function_type =
2015 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
2016 if (function_type == nullptr)
2017 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002018
Kate Stoneb9c1b512016-09-06 20:57:50 +00002019 const bool is_method = false;
2020 const unsigned int num_params = function_type->getNumParams();
2021 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
2022 is_method, op_kind, num_params))
2023 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002024
Kate Stoneb9c1b512016-09-06 20:57:50 +00002025 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00002026}
2027
Kate Stoneb9c1b512016-09-06 20:57:50 +00002028FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
2029 DeclContext *decl_ctx, const char *name,
2030 const CompilerType &function_clang_type, int storage, bool is_inline) {
2031 FunctionDecl *func_decl = nullptr;
2032 ASTContext *ast = getASTContext();
2033 if (decl_ctx == nullptr)
2034 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002035
Kate Stoneb9c1b512016-09-06 20:57:50 +00002036 const bool hasWrittenPrototype = true;
2037 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00002038
Kate Stoneb9c1b512016-09-06 20:57:50 +00002039 clang::DeclarationName declarationName =
2040 GetDeclarationName(name, function_clang_type);
2041 func_decl = FunctionDecl::Create(
2042 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2043 ClangUtil::GetQualType(function_clang_type), nullptr,
2044 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
2045 isConstexprSpecified);
2046 if (func_decl)
2047 decl_ctx->addDecl(func_decl);
2048
Sean Callanan5e9e1992011-10-26 01:06:27 +00002049#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00002050 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002051#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002052
2053 return func_decl;
2054}
2055
2056CompilerType ClangASTContext::CreateFunctionType(
2057 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002058 unsigned num_args, bool is_variadic, unsigned type_quals,
2059 clang::CallingConv cc) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002060 if (ast == nullptr)
2061 return CompilerType(); // invalid AST
2062
2063 if (!result_type || !ClangUtil::IsClangType(result_type))
2064 return CompilerType(); // invalid return type
2065
2066 std::vector<QualType> qual_type_args;
2067 if (num_args > 0 && args == nullptr)
2068 return CompilerType(); // invalid argument array passed in
2069
2070 // Verify that all arguments are valid and the right type
2071 for (unsigned i = 0; i < num_args; ++i) {
2072 if (args[i]) {
2073 // Make sure we have a clang type in args[i] and not a type from another
2074 // language whose name might match
2075 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2076 lldbassert(is_clang_type);
2077 if (is_clang_type)
2078 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2079 else
2080 return CompilerType(); // invalid argument type (must be a clang type)
2081 } else
2082 return CompilerType(); // invalid argument type (empty)
2083 }
2084
2085 // TODO: Detect calling convention in DWARF?
2086 FunctionProtoType::ExtProtoInfo proto_info;
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002087 proto_info.ExtInfo = cc;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002088 proto_info.Variadic = is_variadic;
2089 proto_info.ExceptionSpec = EST_None;
2090 proto_info.TypeQuals = type_quals;
2091 proto_info.RefQualifier = RQ_None;
2092
2093 return CompilerType(ast,
2094 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2095 qual_type_args, proto_info));
2096}
2097
2098ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
2099 const char *name, const CompilerType &param_type, int storage) {
2100 ASTContext *ast = getASTContext();
2101 assert(ast != nullptr);
2102 return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(),
2103 SourceLocation(), SourceLocation(),
2104 name && name[0] ? &ast->Idents.get(name) : nullptr,
2105 ClangUtil::GetQualType(param_type), nullptr,
2106 (clang::StorageClass)storage, nullptr);
2107}
2108
2109void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2110 ParmVarDecl **params,
2111 unsigned num_params) {
2112 if (function_decl)
2113 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002114}
2115
Greg Claytona1e5dc82015-08-11 22:53:00 +00002116CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002117ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
2118 QualType block_type = m_ast_ap->getBlockPointerType(
2119 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002120
Kate Stoneb9c1b512016-09-06 20:57:50 +00002121 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002122}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002123
2124#pragma mark Array Types
2125
Kate Stoneb9c1b512016-09-06 20:57:50 +00002126CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2127 size_t element_count,
2128 bool is_vector) {
2129 if (element_type.IsValid()) {
2130 ASTContext *ast = getASTContext();
2131 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002132
Kate Stoneb9c1b512016-09-06 20:57:50 +00002133 if (is_vector) {
2134 return CompilerType(
2135 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2136 element_count));
2137 } else {
2138
2139 llvm::APInt ap_element_count(64, element_count);
2140 if (element_count == 0) {
2141 return CompilerType(ast, ast->getIncompleteArrayType(
2142 ClangUtil::GetQualType(element_type),
2143 clang::ArrayType::Normal, 0));
2144 } else {
2145 return CompilerType(
2146 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2147 ap_element_count,
2148 clang::ArrayType::Normal, 0));
2149 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002150 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002151 }
2152 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002153}
2154
Kate Stoneb9c1b512016-09-06 20:57:50 +00002155CompilerType ClangASTContext::CreateStructForIdentifier(
2156 const ConstString &type_name,
2157 const std::initializer_list<std::pair<const char *, CompilerType>>
2158 &type_fields,
2159 bool packed) {
2160 CompilerType type;
2161 if (!type_name.IsEmpty() &&
2162 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2163 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002164 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002165 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002166 }
2167
2168 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2169 clang::TTK_Struct, lldb::eLanguageTypeC);
2170 StartTagDeclarationDefinition(type);
2171 for (const auto &field : type_fields)
2172 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2173 0);
2174 if (packed)
2175 SetIsPacked(type);
2176 CompleteTagDeclarationDefinition(type);
2177 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002178}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002179
Kate Stoneb9c1b512016-09-06 20:57:50 +00002180CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
2181 const ConstString &type_name,
2182 const std::initializer_list<std::pair<const char *, CompilerType>>
2183 &type_fields,
2184 bool packed) {
2185 CompilerType type;
2186 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2187 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002188
Kate Stoneb9c1b512016-09-06 20:57:50 +00002189 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002190}
2191
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002192#pragma mark Enumeration Types
2193
Greg Claytona1e5dc82015-08-11 22:53:00 +00002194CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002195ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2196 const Declaration &decl,
Tamas Berghammer59765832017-11-07 10:39:22 +00002197 const CompilerType &integer_clang_type,
2198 bool is_scoped) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002199 // TODO: Do something intelligent with the Declaration object passed in
2200 // like maybe filling in the SourceLocation with it...
2201 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002202
Kate Stoneb9c1b512016-09-06 20:57:50 +00002203 // TODO: ask about these...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002204 // const bool IsFixed = false;
2205
2206 EnumDecl *enum_decl = EnumDecl::Create(
2207 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2208 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
Tamas Berghammercf6bf4c2017-11-07 13:43:55 +00002209 is_scoped, // IsScoped
2210 is_scoped, // IsScopedUsingClassTag
2211 false); // IsFixed
Kate Stoneb9c1b512016-09-06 20:57:50 +00002212
2213 if (enum_decl) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00002214 if (decl_ctx)
2215 decl_ctx->addDecl(enum_decl);
2216
Kate Stoneb9c1b512016-09-06 20:57:50 +00002217 // TODO: check if we should be setting the promotion type too?
2218 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2219
2220 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2221
2222 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2223 }
2224 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002225}
2226
Kate Stoneb9c1b512016-09-06 20:57:50 +00002227CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2228 size_t bit_size,
2229 bool is_signed) {
2230 if (ast) {
2231 if (is_signed) {
2232 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2233 return CompilerType(ast, ast->SignedCharTy);
2234
2235 if (bit_size == ast->getTypeSize(ast->ShortTy))
2236 return CompilerType(ast, ast->ShortTy);
2237
2238 if (bit_size == ast->getTypeSize(ast->IntTy))
2239 return CompilerType(ast, ast->IntTy);
2240
2241 if (bit_size == ast->getTypeSize(ast->LongTy))
2242 return CompilerType(ast, ast->LongTy);
2243
2244 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2245 return CompilerType(ast, ast->LongLongTy);
2246
2247 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2248 return CompilerType(ast, ast->Int128Ty);
2249 } else {
2250 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2251 return CompilerType(ast, ast->UnsignedCharTy);
2252
2253 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2254 return CompilerType(ast, ast->UnsignedShortTy);
2255
2256 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2257 return CompilerType(ast, ast->UnsignedIntTy);
2258
2259 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2260 return CompilerType(ast, ast->UnsignedLongTy);
2261
2262 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2263 return CompilerType(ast, ast->UnsignedLongLongTy);
2264
2265 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2266 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002267 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002268 }
2269 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002270}
2271
Kate Stoneb9c1b512016-09-06 20:57:50 +00002272CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2273 bool is_signed) {
2274 if (ast)
2275 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2276 is_signed);
2277 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002278}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002279
Kate Stoneb9c1b512016-09-06 20:57:50 +00002280void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2281 if (decl_ctx) {
2282 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002283
Kate Stoneb9c1b512016-09-06 20:57:50 +00002284 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2285 if (named_decl) {
2286 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2287 named_decl->getDeclName().getAsString().c_str());
2288 } else {
2289 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002290 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002291 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002292}
2293
Kate Stoneb9c1b512016-09-06 20:57:50 +00002294void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2295 if (decl == nullptr)
2296 return;
2297 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002298
Kate Stoneb9c1b512016-09-06 20:57:50 +00002299 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2300 if (record_decl) {
2301 printf("%20s: %s%s\n", decl->getDeclKindName(),
2302 record_decl->getDeclName().getAsString().c_str(),
2303 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002304
Kate Stoneb9c1b512016-09-06 20:57:50 +00002305 } else {
2306 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2307 if (named_decl) {
2308 printf("%20s: %s\n", decl->getDeclKindName(),
2309 named_decl->getDeclName().getAsString().c_str());
2310 } else {
2311 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002312 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002313 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002314}
2315
Kate Stoneb9c1b512016-09-06 20:57:50 +00002316bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2317 clang::Decl *rhs_decl) {
2318 if (lhs_decl && rhs_decl) {
2319 //----------------------------------------------------------------------
2320 // Make sure the decl kinds match first
2321 //----------------------------------------------------------------------
2322 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2323 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002324
Kate Stoneb9c1b512016-09-06 20:57:50 +00002325 if (lhs_decl_kind == rhs_decl_kind) {
2326 //------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00002327 // Now check that the decl contexts kinds are all equivalent before we
2328 // have to check any names of the decl contexts...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002329 //------------------------------------------------------------------
2330 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2331 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2332 if (lhs_decl_ctx && rhs_decl_ctx) {
2333 while (1) {
2334 if (lhs_decl_ctx && rhs_decl_ctx) {
2335 const clang::Decl::Kind lhs_decl_ctx_kind =
2336 lhs_decl_ctx->getDeclKind();
2337 const clang::Decl::Kind rhs_decl_ctx_kind =
2338 rhs_decl_ctx->getDeclKind();
2339 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2340 lhs_decl_ctx = lhs_decl_ctx->getParent();
2341 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002342
Kate Stoneb9c1b512016-09-06 20:57:50 +00002343 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2344 break;
2345 } else
2346 return false;
2347 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002348 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002349 }
2350
2351 //--------------------------------------------------------------
2352 // Now make sure the name of the decls match
2353 //--------------------------------------------------------------
2354 clang::NamedDecl *lhs_named_decl =
2355 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2356 clang::NamedDecl *rhs_named_decl =
2357 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2358 if (lhs_named_decl && rhs_named_decl) {
2359 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2360 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2361 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2362 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2363 return false;
2364 } else
Greg Claytona2721472011-06-25 00:44:06 +00002365 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002366 } else
2367 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002368
Kate Stoneb9c1b512016-09-06 20:57:50 +00002369 //--------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00002370 // We know that the decl context kinds all match, so now we need to
2371 // make sure the names match as well
Kate Stoneb9c1b512016-09-06 20:57:50 +00002372 //--------------------------------------------------------------
2373 lhs_decl_ctx = lhs_decl->getDeclContext();
2374 rhs_decl_ctx = rhs_decl->getDeclContext();
2375 while (1) {
2376 switch (lhs_decl_ctx->getDeclKind()) {
2377 case clang::Decl::TranslationUnit:
2378 // We don't care about the translation unit names
2379 return true;
2380 default: {
2381 clang::NamedDecl *lhs_named_decl =
2382 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2383 clang::NamedDecl *rhs_named_decl =
2384 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2385 if (lhs_named_decl && rhs_named_decl) {
2386 clang::DeclarationName lhs_decl_name =
2387 lhs_named_decl->getDeclName();
2388 clang::DeclarationName rhs_decl_name =
2389 rhs_named_decl->getDeclName();
2390 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2391 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2392 return false;
2393 } else
2394 return false;
2395 } else
2396 return false;
2397 } break;
2398 }
2399 lhs_decl_ctx = lhs_decl_ctx->getParent();
2400 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002401 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002402 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002403 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002404 }
2405 return false;
2406}
2407bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2408 clang::Decl *decl) {
2409 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002410 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002411
Kate Stoneb9c1b512016-09-06 20:57:50 +00002412 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002413
Kate Stoneb9c1b512016-09-06 20:57:50 +00002414 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002415 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002416
2417 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2418 if (tag_decl->isCompleteDefinition())
2419 return true;
2420
2421 if (!tag_decl->hasExternalLexicalStorage())
2422 return false;
2423
2424 ast_source->CompleteType(tag_decl);
2425
2426 return !tag_decl->getTypeForDecl()->isIncompleteType();
2427 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2428 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2429 if (objc_interface_decl->getDefinition())
2430 return true;
2431
2432 if (!objc_interface_decl->hasExternalLexicalStorage())
2433 return false;
2434
2435 ast_source->CompleteType(objc_interface_decl);
2436
2437 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2438 } else {
2439 return false;
2440 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002441}
2442
Kate Stoneb9c1b512016-09-06 20:57:50 +00002443void ClangASTContext::SetMetadataAsUserID(const void *object,
2444 user_id_t user_id) {
2445 ClangASTMetadata meta_data;
2446 meta_data.SetUserID(user_id);
2447 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002448}
2449
Kate Stoneb9c1b512016-09-06 20:57:50 +00002450void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2451 ClangASTMetadata &metadata) {
2452 ClangExternalASTSourceCommon *external_source =
2453 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2454
2455 if (external_source)
2456 external_source->SetMetadata(object, metadata);
2457}
2458
2459ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2460 const void *object) {
2461 ClangExternalASTSourceCommon *external_source =
2462 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2463
2464 if (external_source && external_source->HasMetadata(object))
2465 return external_source->GetMetadata(object);
2466 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002467 return nullptr;
2468}
2469
Kate Stoneb9c1b512016-09-06 20:57:50 +00002470clang::DeclContext *
2471ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2472 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2473}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002474
Kate Stoneb9c1b512016-09-06 20:57:50 +00002475clang::DeclContext *
2476ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2477 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2478}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002479
Kate Stoneb9c1b512016-09-06 20:57:50 +00002480bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2481 int kind) const {
2482 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2483 if (clang_type) {
2484 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2485 if (tag_type) {
2486 clang::TagDecl *tag_decl =
2487 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2488 if (tag_decl) {
2489 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2490 return true;
2491 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002492 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002493 }
2494 return false;
2495}
2496
2497bool ClangASTContext::SetDefaultAccessForRecordFields(
2498 clang::RecordDecl *record_decl, int default_accessibility,
2499 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2500 if (record_decl) {
2501 uint32_t field_idx;
2502 clang::RecordDecl::field_iterator field, field_end;
2503 for (field = record_decl->field_begin(),
2504 field_end = record_decl->field_end(), field_idx = 0;
2505 field != field_end; ++field, ++field_idx) {
2506 // If no accessibility was assigned, assign the correct one
2507 if (field_idx < num_assigned_accessibilities &&
2508 assigned_accessibilities[field_idx] == clang::AS_none)
2509 field->setAccess((clang::AccessSpecifier)default_accessibility);
2510 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002511 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002512 }
2513 return false;
2514}
2515
2516clang::DeclContext *
2517ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2518 return GetDeclContextForType(ClangUtil::GetQualType(type));
2519}
2520
2521clang::DeclContext *
2522ClangASTContext::GetDeclContextForType(clang::QualType type) {
2523 if (type.isNull())
2524 return nullptr;
2525
2526 clang::QualType qual_type = type.getCanonicalType();
2527 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2528 switch (type_class) {
2529 case clang::Type::ObjCInterface:
2530 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2531 ->getInterface();
2532 case clang::Type::ObjCObjectPointer:
2533 return GetDeclContextForType(
2534 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2535 ->getPointeeType());
2536 case clang::Type::Record:
2537 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2538 case clang::Type::Enum:
2539 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2540 case clang::Type::Typedef:
2541 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2542 ->getDecl()
2543 ->getUnderlyingType());
2544 case clang::Type::Auto:
2545 return GetDeclContextForType(
2546 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2547 case clang::Type::Elaborated:
2548 return GetDeclContextForType(
2549 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2550 case clang::Type::Paren:
2551 return GetDeclContextForType(
2552 llvm::cast<clang::ParenType>(qual_type)->desugar());
2553 default:
2554 break;
2555 }
2556 // No DeclContext in this type...
2557 return nullptr;
2558}
2559
2560static bool GetCompleteQualType(clang::ASTContext *ast,
2561 clang::QualType qual_type,
2562 bool allow_completion = true) {
2563 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2564 switch (type_class) {
2565 case clang::Type::ConstantArray:
2566 case clang::Type::IncompleteArray:
2567 case clang::Type::VariableArray: {
2568 const clang::ArrayType *array_type =
2569 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2570
2571 if (array_type)
2572 return GetCompleteQualType(ast, array_type->getElementType(),
2573 allow_completion);
2574 } break;
2575 case clang::Type::Record: {
2576 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2577 if (cxx_record_decl) {
2578 if (cxx_record_decl->hasExternalLexicalStorage()) {
2579 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2580 const bool fields_loaded =
2581 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2582 if (is_complete && fields_loaded)
2583 return true;
2584
2585 if (!allow_completion)
2586 return false;
2587
2588 // Call the field_begin() accessor to for it to use the external source
2589 // to load the fields...
2590 clang::ExternalASTSource *external_ast_source =
2591 ast->getExternalSource();
2592 if (external_ast_source) {
2593 external_ast_source->CompleteType(cxx_record_decl);
2594 if (cxx_record_decl->isCompleteDefinition()) {
2595 cxx_record_decl->field_begin();
2596 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2597 }
2598 }
2599 }
2600 }
2601 const clang::TagType *tag_type =
2602 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2603 return !tag_type->isIncompleteType();
2604 } break;
2605
2606 case clang::Type::Enum: {
2607 const clang::TagType *tag_type =
2608 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2609 if (tag_type) {
2610 clang::TagDecl *tag_decl = tag_type->getDecl();
2611 if (tag_decl) {
2612 if (tag_decl->getDefinition())
2613 return true;
2614
2615 if (!allow_completion)
2616 return false;
2617
2618 if (tag_decl->hasExternalLexicalStorage()) {
2619 if (ast) {
2620 clang::ExternalASTSource *external_ast_source =
2621 ast->getExternalSource();
2622 if (external_ast_source) {
2623 external_ast_source->CompleteType(tag_decl);
2624 return !tag_type->isIncompleteType();
2625 }
2626 }
2627 }
2628 return false;
2629 }
2630 }
2631
2632 } break;
2633 case clang::Type::ObjCObject:
2634 case clang::Type::ObjCInterface: {
2635 const clang::ObjCObjectType *objc_class_type =
2636 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2637 if (objc_class_type) {
2638 clang::ObjCInterfaceDecl *class_interface_decl =
2639 objc_class_type->getInterface();
2640 // We currently can't complete objective C types through the newly added
Adrian Prantl05097242018-04-30 16:49:04 +00002641 // ASTContext because it only supports TagDecl objects right now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002642 if (class_interface_decl) {
2643 if (class_interface_decl->getDefinition())
2644 return true;
2645
2646 if (!allow_completion)
2647 return false;
2648
2649 if (class_interface_decl->hasExternalLexicalStorage()) {
2650 if (ast) {
2651 clang::ExternalASTSource *external_ast_source =
2652 ast->getExternalSource();
2653 if (external_ast_source) {
2654 external_ast_source->CompleteType(class_interface_decl);
2655 return !objc_class_type->isIncompleteType();
2656 }
2657 }
2658 }
2659 return false;
2660 }
2661 }
2662 } break;
2663
2664 case clang::Type::Typedef:
2665 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2666 ->getDecl()
2667 ->getUnderlyingType(),
2668 allow_completion);
2669
2670 case clang::Type::Auto:
2671 return GetCompleteQualType(
2672 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2673 allow_completion);
2674
2675 case clang::Type::Elaborated:
2676 return GetCompleteQualType(
2677 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2678 allow_completion);
2679
2680 case clang::Type::Paren:
2681 return GetCompleteQualType(
2682 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2683 allow_completion);
2684
2685 case clang::Type::Attributed:
2686 return GetCompleteQualType(
2687 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2688 allow_completion);
2689
2690 default:
2691 break;
2692 }
2693
2694 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002695}
2696
2697static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002698ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2699 switch (access) {
2700 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002701 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002702 case eAccessPublic:
2703 return clang::ObjCIvarDecl::Public;
2704 case eAccessPrivate:
2705 return clang::ObjCIvarDecl::Private;
2706 case eAccessProtected:
2707 return clang::ObjCIvarDecl::Protected;
2708 case eAccessPackage:
2709 return clang::ObjCIvarDecl::Package;
2710 }
2711 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002712}
2713
Greg Claytond8d4a572015-08-11 21:38:15 +00002714//----------------------------------------------------------------------
2715// Tests
2716//----------------------------------------------------------------------
2717
Kate Stoneb9c1b512016-09-06 20:57:50 +00002718bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2719 clang::QualType qual_type(GetCanonicalQualType(type));
2720
2721 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2722 switch (type_class) {
2723 case clang::Type::IncompleteArray:
2724 case clang::Type::VariableArray:
2725 case clang::Type::ConstantArray:
2726 case clang::Type::ExtVector:
2727 case clang::Type::Vector:
2728 case clang::Type::Record:
2729 case clang::Type::ObjCObject:
2730 case clang::Type::ObjCInterface:
2731 return true;
2732 case clang::Type::Auto:
2733 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2734 ->getDeducedType()
2735 .getAsOpaquePtr());
2736 case clang::Type::Elaborated:
2737 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2738 ->getNamedType()
2739 .getAsOpaquePtr());
2740 case clang::Type::Typedef:
2741 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2742 ->getDecl()
2743 ->getUnderlyingType()
2744 .getAsOpaquePtr());
2745 case clang::Type::Paren:
2746 return IsAggregateType(
2747 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2748 default:
2749 break;
2750 }
2751 // The clang type does have a value
2752 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002753}
2754
Kate Stoneb9c1b512016-09-06 20:57:50 +00002755bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2756 clang::QualType qual_type(GetCanonicalQualType(type));
2757
2758 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2759 switch (type_class) {
2760 case clang::Type::Record: {
2761 if (const clang::RecordType *record_type =
2762 llvm::dyn_cast_or_null<clang::RecordType>(
2763 qual_type.getTypePtrOrNull())) {
2764 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2765 return record_decl->isAnonymousStructOrUnion();
2766 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002767 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002768 break;
2769 }
2770 case clang::Type::Auto:
2771 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2772 ->getDeducedType()
2773 .getAsOpaquePtr());
2774 case clang::Type::Elaborated:
2775 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2776 ->getNamedType()
2777 .getAsOpaquePtr());
2778 case clang::Type::Typedef:
2779 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2780 ->getDecl()
2781 ->getUnderlyingType()
2782 .getAsOpaquePtr());
2783 case clang::Type::Paren:
2784 return IsAnonymousType(
2785 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2786 default:
2787 break;
2788 }
2789 // The clang type does have a value
2790 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002791}
2792
Kate Stoneb9c1b512016-09-06 20:57:50 +00002793bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2794 CompilerType *element_type_ptr,
2795 uint64_t *size, bool *is_incomplete) {
2796 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002797
Kate Stoneb9c1b512016-09-06 20:57:50 +00002798 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2799 switch (type_class) {
2800 default:
2801 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002802
Kate Stoneb9c1b512016-09-06 20:57:50 +00002803 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002804 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002805 element_type_ptr->SetCompilerType(
2806 getASTContext(),
2807 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002808 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002809 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2810 ->getSize()
2811 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002812 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002813 *is_incomplete = false;
2814 return true;
2815
2816 case clang::Type::IncompleteArray:
2817 if (element_type_ptr)
2818 element_type_ptr->SetCompilerType(
2819 getASTContext(),
2820 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2821 if (size)
2822 *size = 0;
2823 if (is_incomplete)
2824 *is_incomplete = true;
2825 return true;
2826
2827 case clang::Type::VariableArray:
2828 if (element_type_ptr)
2829 element_type_ptr->SetCompilerType(
2830 getASTContext(),
2831 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2832 if (size)
2833 *size = 0;
2834 if (is_incomplete)
2835 *is_incomplete = false;
2836 return true;
2837
2838 case clang::Type::DependentSizedArray:
2839 if (element_type_ptr)
2840 element_type_ptr->SetCompilerType(
2841 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2842 ->getElementType());
2843 if (size)
2844 *size = 0;
2845 if (is_incomplete)
2846 *is_incomplete = false;
2847 return true;
2848
2849 case clang::Type::Typedef:
2850 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2851 ->getDecl()
2852 ->getUnderlyingType()
2853 .getAsOpaquePtr(),
2854 element_type_ptr, size, is_incomplete);
2855 case clang::Type::Auto:
2856 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2857 ->getDeducedType()
2858 .getAsOpaquePtr(),
2859 element_type_ptr, size, is_incomplete);
2860 case clang::Type::Elaborated:
2861 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2862 ->getNamedType()
2863 .getAsOpaquePtr(),
2864 element_type_ptr, size, is_incomplete);
2865 case clang::Type::Paren:
2866 return IsArrayType(
2867 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2868 element_type_ptr, size, is_incomplete);
2869 }
2870 if (element_type_ptr)
2871 element_type_ptr->Clear();
2872 if (size)
2873 *size = 0;
2874 if (is_incomplete)
2875 *is_incomplete = false;
2876 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002877}
2878
Kate Stoneb9c1b512016-09-06 20:57:50 +00002879bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2880 CompilerType *element_type, uint64_t *size) {
2881 clang::QualType qual_type(GetCanonicalQualType(type));
2882
2883 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2884 switch (type_class) {
2885 case clang::Type::Vector: {
2886 const clang::VectorType *vector_type =
2887 qual_type->getAs<clang::VectorType>();
2888 if (vector_type) {
2889 if (size)
2890 *size = vector_type->getNumElements();
2891 if (element_type)
2892 *element_type =
2893 CompilerType(getASTContext(), vector_type->getElementType());
2894 }
2895 return true;
2896 } break;
2897 case clang::Type::ExtVector: {
2898 const clang::ExtVectorType *ext_vector_type =
2899 qual_type->getAs<clang::ExtVectorType>();
2900 if (ext_vector_type) {
2901 if (size)
2902 *size = ext_vector_type->getNumElements();
2903 if (element_type)
2904 *element_type =
2905 CompilerType(getASTContext(), ext_vector_type->getElementType());
2906 }
2907 return true;
2908 }
2909 default:
2910 break;
2911 }
2912 return false;
2913}
2914
2915bool ClangASTContext::IsRuntimeGeneratedType(
2916 lldb::opaque_compiler_type_t type) {
2917 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
2918 ->GetDeclContextForType(GetQualType(type));
2919 if (!decl_ctx)
2920 return false;
2921
2922 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2923 return false;
2924
2925 clang::ObjCInterfaceDecl *result_iface_decl =
2926 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2927
2928 ClangASTMetadata *ast_metadata =
2929 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2930 if (!ast_metadata)
2931 return false;
2932 return (ast_metadata->GetISAPtr() != 0);
2933}
2934
2935bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
2936 return GetQualType(type).getUnqualifiedType()->isCharType();
2937}
2938
2939bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
2940 const bool allow_completion = false;
2941 return GetCompleteQualType(getASTContext(), GetQualType(type),
2942 allow_completion);
2943}
2944
2945bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
2946 return GetQualType(type).isConstQualified();
2947}
2948
2949bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
2950 uint32_t &length) {
2951 CompilerType pointee_or_element_clang_type;
2952 length = 0;
2953 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2954
2955 if (!pointee_or_element_clang_type.IsValid())
2956 return false;
2957
2958 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2959 if (pointee_or_element_clang_type.IsCharType()) {
2960 if (type_flags.Test(eTypeIsArray)) {
Adrian Prantl05097242018-04-30 16:49:04 +00002961 // We know the size of the array and it could be a C string since it is
2962 // an array of characters
Kate Stoneb9c1b512016-09-06 20:57:50 +00002963 length = llvm::cast<clang::ConstantArrayType>(
2964 GetCanonicalQualType(type).getTypePtr())
2965 ->getSize()
2966 .getLimitedValue();
2967 }
2968 return true;
2969 }
2970 }
2971 return false;
2972}
2973
2974bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
2975 bool *is_variadic_ptr) {
2976 if (type) {
2977 clang::QualType qual_type(GetCanonicalQualType(type));
2978
2979 if (qual_type->isFunctionType()) {
2980 if (is_variadic_ptr) {
2981 const clang::FunctionProtoType *function_proto_type =
2982 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2983 if (function_proto_type)
2984 *is_variadic_ptr = function_proto_type->isVariadic();
2985 else
2986 *is_variadic_ptr = false;
2987 }
2988 return true;
2989 }
2990
Greg Claytond8d4a572015-08-11 21:38:15 +00002991 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002992 switch (type_class) {
2993 default:
2994 break;
2995 case clang::Type::Typedef:
2996 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
2997 ->getDecl()
2998 ->getUnderlyingType()
2999 .getAsOpaquePtr(),
3000 nullptr);
3001 case clang::Type::Auto:
3002 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3003 ->getDeducedType()
3004 .getAsOpaquePtr(),
3005 nullptr);
3006 case clang::Type::Elaborated:
3007 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3008 ->getNamedType()
3009 .getAsOpaquePtr(),
3010 nullptr);
3011 case clang::Type::Paren:
3012 return IsFunctionType(
3013 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3014 nullptr);
3015 case clang::Type::LValueReference:
3016 case clang::Type::RValueReference: {
3017 const clang::ReferenceType *reference_type =
3018 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3019 if (reference_type)
3020 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3021 nullptr);
3022 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00003023 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003024 }
3025 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003026}
3027
3028// Used to detect "Homogeneous Floating-point Aggregates"
3029uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003030ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3031 CompilerType *base_type_ptr) {
3032 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003033 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003034
3035 clang::QualType qual_type(GetCanonicalQualType(type));
3036 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3037 switch (type_class) {
3038 case clang::Type::Record:
3039 if (GetCompleteType(type)) {
3040 const clang::CXXRecordDecl *cxx_record_decl =
3041 qual_type->getAsCXXRecordDecl();
3042 if (cxx_record_decl) {
3043 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3044 return 0;
3045 }
3046 const clang::RecordType *record_type =
3047 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3048 if (record_type) {
3049 const clang::RecordDecl *record_decl = record_type->getDecl();
3050 if (record_decl) {
3051 // We are looking for a structure that contains only floating point
3052 // types
3053 clang::RecordDecl::field_iterator field_pos,
3054 field_end = record_decl->field_end();
3055 uint32_t num_fields = 0;
3056 bool is_hva = false;
3057 bool is_hfa = false;
3058 clang::QualType base_qual_type;
3059 uint64_t base_bitwidth = 0;
3060 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3061 ++field_pos) {
3062 clang::QualType field_qual_type = field_pos->getType();
3063 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3064 if (field_qual_type->isFloatingType()) {
3065 if (field_qual_type->isComplexType())
3066 return 0;
3067 else {
3068 if (num_fields == 0)
3069 base_qual_type = field_qual_type;
3070 else {
3071 if (is_hva)
3072 return 0;
3073 is_hfa = true;
3074 if (field_qual_type.getTypePtr() !=
3075 base_qual_type.getTypePtr())
3076 return 0;
3077 }
3078 }
3079 } else if (field_qual_type->isVectorType() ||
3080 field_qual_type->isExtVectorType()) {
3081 if (num_fields == 0) {
3082 base_qual_type = field_qual_type;
3083 base_bitwidth = field_bitwidth;
3084 } else {
3085 if (is_hfa)
3086 return 0;
3087 is_hva = true;
3088 if (base_bitwidth != field_bitwidth)
3089 return 0;
3090 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3091 return 0;
3092 }
3093 } else
3094 return 0;
3095 ++num_fields;
3096 }
3097 if (base_type_ptr)
3098 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3099 return num_fields;
3100 }
3101 }
3102 }
3103 break;
3104
3105 case clang::Type::Typedef:
3106 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3107 ->getDecl()
3108 ->getUnderlyingType()
3109 .getAsOpaquePtr(),
3110 base_type_ptr);
3111
3112 case clang::Type::Auto:
3113 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3114 ->getDeducedType()
3115 .getAsOpaquePtr(),
3116 base_type_ptr);
3117
3118 case clang::Type::Elaborated:
3119 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3120 ->getNamedType()
3121 .getAsOpaquePtr(),
3122 base_type_ptr);
3123 default:
3124 break;
3125 }
3126 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003127}
3128
Kate Stoneb9c1b512016-09-06 20:57:50 +00003129size_t ClangASTContext::GetNumberOfFunctionArguments(
3130 lldb::opaque_compiler_type_t type) {
3131 if (type) {
3132 clang::QualType qual_type(GetCanonicalQualType(type));
3133 const clang::FunctionProtoType *func =
3134 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3135 if (func)
3136 return func->getNumParams();
3137 }
3138 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003139}
3140
Greg Claytona1e5dc82015-08-11 22:53:00 +00003141CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003142ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3143 const size_t index) {
3144 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003145 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003146 const clang::FunctionProtoType *func =
3147 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3148 if (func) {
3149 if (index < func->getNumParams())
3150 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003151 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003152 }
3153 return CompilerType();
3154}
3155
3156bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3157 if (type) {
3158 clang::QualType qual_type(GetCanonicalQualType(type));
3159
3160 if (qual_type->isFunctionPointerType())
3161 return true;
3162
3163 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3164 switch (type_class) {
3165 default:
3166 break;
3167 case clang::Type::Typedef:
3168 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3169 ->getDecl()
3170 ->getUnderlyingType()
3171 .getAsOpaquePtr());
3172 case clang::Type::Auto:
3173 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3174 ->getDeducedType()
3175 .getAsOpaquePtr());
3176 case clang::Type::Elaborated:
3177 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3178 ->getNamedType()
3179 .getAsOpaquePtr());
3180 case clang::Type::Paren:
3181 return IsFunctionPointerType(
3182 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3183
3184 case clang::Type::LValueReference:
3185 case clang::Type::RValueReference: {
3186 const clang::ReferenceType *reference_type =
3187 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3188 if (reference_type)
3189 return IsFunctionPointerType(
3190 reference_type->getPointeeType().getAsOpaquePtr());
3191 } break;
3192 }
3193 }
3194 return false;
3195}
3196
3197bool ClangASTContext::IsBlockPointerType(
3198 lldb::opaque_compiler_type_t type,
3199 CompilerType *function_pointer_type_ptr) {
3200 if (type) {
3201 clang::QualType qual_type(GetCanonicalQualType(type));
3202
3203 if (qual_type->isBlockPointerType()) {
3204 if (function_pointer_type_ptr) {
3205 const clang::BlockPointerType *block_pointer_type =
3206 qual_type->getAs<clang::BlockPointerType>();
3207 QualType pointee_type = block_pointer_type->getPointeeType();
3208 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3209 *function_pointer_type_ptr =
3210 CompilerType(getASTContext(), function_pointer_type);
3211 }
3212 return true;
3213 }
3214
3215 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3216 switch (type_class) {
3217 default:
3218 break;
3219 case clang::Type::Typedef:
3220 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3221 ->getDecl()
3222 ->getUnderlyingType()
3223 .getAsOpaquePtr(),
3224 function_pointer_type_ptr);
3225 case clang::Type::Auto:
3226 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3227 ->getDeducedType()
3228 .getAsOpaquePtr(),
3229 function_pointer_type_ptr);
3230 case clang::Type::Elaborated:
3231 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3232 ->getNamedType()
3233 .getAsOpaquePtr(),
3234 function_pointer_type_ptr);
3235 case clang::Type::Paren:
3236 return IsBlockPointerType(
3237 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3238 function_pointer_type_ptr);
3239
3240 case clang::Type::LValueReference:
3241 case clang::Type::RValueReference: {
3242 const clang::ReferenceType *reference_type =
3243 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3244 if (reference_type)
3245 return IsBlockPointerType(
3246 reference_type->getPointeeType().getAsOpaquePtr(),
3247 function_pointer_type_ptr);
3248 } break;
3249 }
3250 }
3251 return false;
3252}
3253
3254bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3255 bool &is_signed) {
3256 if (!type)
3257 return false;
3258
3259 clang::QualType qual_type(GetCanonicalQualType(type));
3260 const clang::BuiltinType *builtin_type =
3261 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3262
3263 if (builtin_type) {
3264 if (builtin_type->isInteger()) {
3265 is_signed = builtin_type->isSignedInteger();
3266 return true;
3267 }
3268 }
3269
3270 return false;
3271}
3272
3273bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3274 bool &is_signed) {
3275 if (type) {
3276 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3277 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3278
3279 if (enum_type) {
3280 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3281 is_signed);
3282 return true;
3283 }
3284 }
3285
3286 return false;
3287}
3288
3289bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3290 CompilerType *pointee_type) {
3291 if (type) {
3292 clang::QualType qual_type(GetCanonicalQualType(type));
3293 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3294 switch (type_class) {
3295 case clang::Type::Builtin:
3296 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3297 default:
3298 break;
3299 case clang::BuiltinType::ObjCId:
3300 case clang::BuiltinType::ObjCClass:
3301 return true;
3302 }
3303 return false;
3304 case clang::Type::ObjCObjectPointer:
3305 if (pointee_type)
3306 pointee_type->SetCompilerType(
3307 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3308 ->getPointeeType());
3309 return true;
3310 case clang::Type::BlockPointer:
3311 if (pointee_type)
3312 pointee_type->SetCompilerType(
3313 getASTContext(),
3314 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3315 return true;
3316 case clang::Type::Pointer:
3317 if (pointee_type)
3318 pointee_type->SetCompilerType(
3319 getASTContext(),
3320 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3321 return true;
3322 case clang::Type::MemberPointer:
3323 if (pointee_type)
3324 pointee_type->SetCompilerType(
3325 getASTContext(),
3326 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3327 return true;
3328 case clang::Type::Typedef:
3329 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3330 ->getDecl()
3331 ->getUnderlyingType()
3332 .getAsOpaquePtr(),
3333 pointee_type);
3334 case clang::Type::Auto:
3335 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3336 ->getDeducedType()
3337 .getAsOpaquePtr(),
3338 pointee_type);
3339 case clang::Type::Elaborated:
3340 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3341 ->getNamedType()
3342 .getAsOpaquePtr(),
3343 pointee_type);
3344 case clang::Type::Paren:
3345 return IsPointerType(
3346 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3347 pointee_type);
3348 default:
3349 break;
3350 }
3351 }
3352 if (pointee_type)
3353 pointee_type->Clear();
3354 return false;
3355}
3356
3357bool ClangASTContext::IsPointerOrReferenceType(
3358 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3359 if (type) {
3360 clang::QualType qual_type(GetCanonicalQualType(type));
3361 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3362 switch (type_class) {
3363 case clang::Type::Builtin:
3364 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3365 default:
3366 break;
3367 case clang::BuiltinType::ObjCId:
3368 case clang::BuiltinType::ObjCClass:
3369 return true;
3370 }
3371 return false;
3372 case clang::Type::ObjCObjectPointer:
3373 if (pointee_type)
3374 pointee_type->SetCompilerType(
3375 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3376 ->getPointeeType());
3377 return true;
3378 case clang::Type::BlockPointer:
3379 if (pointee_type)
3380 pointee_type->SetCompilerType(
3381 getASTContext(),
3382 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3383 return true;
3384 case clang::Type::Pointer:
3385 if (pointee_type)
3386 pointee_type->SetCompilerType(
3387 getASTContext(),
3388 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3389 return true;
3390 case clang::Type::MemberPointer:
3391 if (pointee_type)
3392 pointee_type->SetCompilerType(
3393 getASTContext(),
3394 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3395 return true;
3396 case clang::Type::LValueReference:
3397 if (pointee_type)
3398 pointee_type->SetCompilerType(
3399 getASTContext(),
3400 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3401 return true;
3402 case clang::Type::RValueReference:
3403 if (pointee_type)
3404 pointee_type->SetCompilerType(
3405 getASTContext(),
3406 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3407 return true;
3408 case clang::Type::Typedef:
3409 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3410 ->getDecl()
3411 ->getUnderlyingType()
3412 .getAsOpaquePtr(),
3413 pointee_type);
3414 case clang::Type::Auto:
3415 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3416 ->getDeducedType()
3417 .getAsOpaquePtr(),
3418 pointee_type);
3419 case clang::Type::Elaborated:
3420 return IsPointerOrReferenceType(
3421 llvm::cast<clang::ElaboratedType>(qual_type)
3422 ->getNamedType()
3423 .getAsOpaquePtr(),
3424 pointee_type);
3425 case clang::Type::Paren:
3426 return IsPointerOrReferenceType(
3427 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3428 pointee_type);
3429 default:
3430 break;
3431 }
3432 }
3433 if (pointee_type)
3434 pointee_type->Clear();
3435 return false;
3436}
3437
3438bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3439 CompilerType *pointee_type,
3440 bool *is_rvalue) {
3441 if (type) {
3442 clang::QualType qual_type(GetCanonicalQualType(type));
3443 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3444
3445 switch (type_class) {
3446 case clang::Type::LValueReference:
3447 if (pointee_type)
3448 pointee_type->SetCompilerType(
3449 getASTContext(),
3450 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3451 if (is_rvalue)
3452 *is_rvalue = false;
3453 return true;
3454 case clang::Type::RValueReference:
3455 if (pointee_type)
3456 pointee_type->SetCompilerType(
3457 getASTContext(),
3458 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3459 if (is_rvalue)
3460 *is_rvalue = true;
3461 return true;
3462 case clang::Type::Typedef:
3463 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3464 ->getDecl()
3465 ->getUnderlyingType()
3466 .getAsOpaquePtr(),
3467 pointee_type, is_rvalue);
3468 case clang::Type::Auto:
3469 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3470 ->getDeducedType()
3471 .getAsOpaquePtr(),
3472 pointee_type, is_rvalue);
3473 case clang::Type::Elaborated:
3474 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3475 ->getNamedType()
3476 .getAsOpaquePtr(),
3477 pointee_type, is_rvalue);
3478 case clang::Type::Paren:
3479 return IsReferenceType(
3480 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3481 pointee_type, is_rvalue);
3482
3483 default:
3484 break;
3485 }
3486 }
3487 if (pointee_type)
3488 pointee_type->Clear();
3489 return false;
3490}
3491
3492bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3493 uint32_t &count, bool &is_complex) {
3494 if (type) {
3495 clang::QualType qual_type(GetCanonicalQualType(type));
3496
3497 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3498 qual_type->getCanonicalTypeInternal())) {
3499 clang::BuiltinType::Kind kind = BT->getKind();
3500 if (kind >= clang::BuiltinType::Float &&
3501 kind <= clang::BuiltinType::LongDouble) {
3502 count = 1;
3503 is_complex = false;
3504 return true;
3505 }
3506 } else if (const clang::ComplexType *CT =
3507 llvm::dyn_cast<clang::ComplexType>(
3508 qual_type->getCanonicalTypeInternal())) {
3509 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3510 is_complex)) {
3511 count = 2;
3512 is_complex = true;
3513 return true;
3514 }
3515 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3516 qual_type->getCanonicalTypeInternal())) {
3517 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3518 is_complex)) {
3519 count = VT->getNumElements();
3520 is_complex = false;
3521 return true;
3522 }
3523 }
3524 }
3525 count = 0;
3526 is_complex = false;
3527 return false;
3528}
3529
3530bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3531 if (!type)
3532 return false;
3533
3534 clang::QualType qual_type(GetQualType(type));
3535 const clang::TagType *tag_type =
3536 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3537 if (tag_type) {
3538 clang::TagDecl *tag_decl = tag_type->getDecl();
3539 if (tag_decl)
3540 return tag_decl->isCompleteDefinition();
3541 return false;
3542 } else {
3543 const clang::ObjCObjectType *objc_class_type =
3544 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3545 if (objc_class_type) {
3546 clang::ObjCInterfaceDecl *class_interface_decl =
3547 objc_class_type->getInterface();
3548 if (class_interface_decl)
3549 return class_interface_decl->getDefinition() != nullptr;
3550 return false;
3551 }
3552 }
3553 return true;
3554}
3555
3556bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3557 if (type) {
3558 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3559
3560 const clang::ObjCObjectPointerType *obj_pointer_type =
3561 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3562
3563 if (obj_pointer_type)
3564 return obj_pointer_type->isObjCClassType();
3565 }
3566 return false;
3567}
3568
3569bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3570 if (ClangUtil::IsClangType(type))
3571 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3572 return false;
3573}
3574
3575bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3576 if (!type)
3577 return false;
3578 clang::QualType qual_type(GetCanonicalQualType(type));
3579 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3580 return (type_class == clang::Type::Record);
3581}
3582
3583bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3584 if (!type)
3585 return false;
3586 clang::QualType qual_type(GetCanonicalQualType(type));
3587 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3588 return (type_class == clang::Type::Enum);
3589}
3590
3591bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3592 if (type) {
3593 clang::QualType qual_type(GetCanonicalQualType(type));
3594 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3595 switch (type_class) {
3596 case clang::Type::Record:
3597 if (GetCompleteType(type)) {
3598 const clang::RecordType *record_type =
3599 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3600 const clang::RecordDecl *record_decl = record_type->getDecl();
3601 if (record_decl) {
3602 const clang::CXXRecordDecl *cxx_record_decl =
3603 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3604 if (cxx_record_decl)
3605 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003606 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003607 }
3608 break;
3609
3610 default:
3611 break;
3612 }
3613 }
3614 return false;
3615}
3616
3617bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3618 CompilerType *dynamic_pointee_type,
3619 bool check_cplusplus,
3620 bool check_objc) {
3621 clang::QualType pointee_qual_type;
3622 if (type) {
3623 clang::QualType qual_type(GetCanonicalQualType(type));
3624 bool success = false;
3625 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3626 switch (type_class) {
3627 case clang::Type::Builtin:
3628 if (check_objc &&
3629 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3630 clang::BuiltinType::ObjCId) {
3631 if (dynamic_pointee_type)
3632 dynamic_pointee_type->SetCompilerType(this, type);
3633 return true;
3634 }
3635 break;
3636
3637 case clang::Type::ObjCObjectPointer:
3638 if (check_objc) {
3639 if (auto objc_pointee_type =
3640 qual_type->getPointeeType().getTypePtrOrNull()) {
3641 if (auto objc_object_type =
3642 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3643 objc_pointee_type)) {
3644 if (objc_object_type->isObjCClass())
3645 return false;
3646 }
3647 }
3648 if (dynamic_pointee_type)
3649 dynamic_pointee_type->SetCompilerType(
3650 getASTContext(),
3651 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3652 ->getPointeeType());
3653 return true;
3654 }
3655 break;
3656
3657 case clang::Type::Pointer:
3658 pointee_qual_type =
3659 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3660 success = true;
3661 break;
3662
3663 case clang::Type::LValueReference:
3664 case clang::Type::RValueReference:
3665 pointee_qual_type =
3666 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3667 success = true;
3668 break;
3669
3670 case clang::Type::Typedef:
3671 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3672 ->getDecl()
3673 ->getUnderlyingType()
3674 .getAsOpaquePtr(),
3675 dynamic_pointee_type, check_cplusplus,
3676 check_objc);
3677
3678 case clang::Type::Auto:
3679 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3680 ->getDeducedType()
3681 .getAsOpaquePtr(),
3682 dynamic_pointee_type, check_cplusplus,
3683 check_objc);
3684
3685 case clang::Type::Elaborated:
3686 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3687 ->getNamedType()
3688 .getAsOpaquePtr(),
3689 dynamic_pointee_type, check_cplusplus,
3690 check_objc);
3691
3692 case clang::Type::Paren:
3693 return IsPossibleDynamicType(
3694 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3695 dynamic_pointee_type, check_cplusplus, check_objc);
3696 default:
3697 break;
3698 }
3699
3700 if (success) {
3701 // Check to make sure what we are pointing too is a possible dynamic C++
Adrian Prantl05097242018-04-30 16:49:04 +00003702 // type We currently accept any "void *" (in case we have a class that
3703 // has been watered down to an opaque pointer) and virtual C++ classes.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003704 const clang::Type::TypeClass pointee_type_class =
3705 pointee_qual_type.getCanonicalType()->getTypeClass();
3706 switch (pointee_type_class) {
3707 case clang::Type::Builtin:
3708 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3709 case clang::BuiltinType::UnknownAny:
3710 case clang::BuiltinType::Void:
3711 if (dynamic_pointee_type)
3712 dynamic_pointee_type->SetCompilerType(getASTContext(),
3713 pointee_qual_type);
3714 return true;
3715 default:
3716 break;
3717 }
3718 break;
3719
3720 case clang::Type::Record:
3721 if (check_cplusplus) {
3722 clang::CXXRecordDecl *cxx_record_decl =
3723 pointee_qual_type->getAsCXXRecordDecl();
3724 if (cxx_record_decl) {
3725 bool is_complete = cxx_record_decl->isCompleteDefinition();
3726
3727 if (is_complete)
3728 success = cxx_record_decl->isDynamicClass();
3729 else {
3730 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3731 getASTContext(), cxx_record_decl);
3732 if (metadata)
3733 success = metadata->GetIsDynamicCXXType();
3734 else {
3735 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3736 .GetCompleteType();
3737 if (is_complete)
3738 success = cxx_record_decl->isDynamicClass();
3739 else
3740 success = false;
3741 }
3742 }
3743
3744 if (success) {
3745 if (dynamic_pointee_type)
3746 dynamic_pointee_type->SetCompilerType(getASTContext(),
3747 pointee_qual_type);
3748 return true;
3749 }
3750 }
3751 }
3752 break;
3753
3754 case clang::Type::ObjCObject:
3755 case clang::Type::ObjCInterface:
3756 if (check_objc) {
3757 if (dynamic_pointee_type)
3758 dynamic_pointee_type->SetCompilerType(getASTContext(),
3759 pointee_qual_type);
3760 return true;
3761 }
3762 break;
3763
3764 default:
3765 break;
3766 }
3767 }
3768 }
3769 if (dynamic_pointee_type)
3770 dynamic_pointee_type->Clear();
3771 return false;
3772}
3773
3774bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3775 if (!type)
3776 return false;
3777
3778 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3779}
3780
3781bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3782 if (!type)
3783 return false;
3784 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3785}
3786
3787bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3788 if (!type)
3789 return false;
3790 return GetCanonicalQualType(type)->isVoidType();
3791}
3792
3793bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3794 return ClangASTContextSupportsLanguage(language);
3795}
3796
3797bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3798 std::string &class_name) {
3799 if (type) {
3800 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3801 if (!qual_type.isNull()) {
3802 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3803 if (cxx_record_decl) {
3804 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3805 return true;
3806 }
3807 }
3808 }
3809 class_name.clear();
3810 return false;
3811}
3812
3813bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3814 if (!type)
3815 return false;
3816
3817 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3818 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
3819 return true;
3820 return false;
3821}
3822
3823bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3824 if (!type)
3825 return false;
3826 clang::QualType qual_type(GetCanonicalQualType(type));
3827 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3828 if (tag_type)
3829 return tag_type->isBeingDefined();
3830 return false;
3831}
3832
3833bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3834 CompilerType *class_type_ptr) {
3835 if (!type)
3836 return false;
3837
3838 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3839
3840 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3841 if (class_type_ptr) {
3842 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3843 const clang::ObjCObjectPointerType *obj_pointer_type =
3844 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3845 if (obj_pointer_type == nullptr)
3846 class_type_ptr->Clear();
3847 else
3848 class_type_ptr->SetCompilerType(
3849 type.GetTypeSystem(),
3850 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3851 .getAsOpaquePtr());
3852 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003853 }
3854 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003855 }
3856 if (class_type_ptr)
3857 class_type_ptr->Clear();
3858 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003859}
3860
Kate Stoneb9c1b512016-09-06 20:57:50 +00003861bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3862 std::string &class_name) {
3863 if (!type)
3864 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003865
Kate Stoneb9c1b512016-09-06 20:57:50 +00003866 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3867
3868 const clang::ObjCObjectType *object_type =
3869 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3870 if (object_type) {
3871 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3872 if (interface) {
3873 class_name = interface->getNameAsString();
3874 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00003875 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003876 }
3877 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003878}
3879
Greg Claytond8d4a572015-08-11 21:38:15 +00003880//----------------------------------------------------------------------
3881// Type Completion
3882//----------------------------------------------------------------------
3883
Kate Stoneb9c1b512016-09-06 20:57:50 +00003884bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3885 if (!type)
3886 return false;
3887 const bool allow_completion = true;
3888 return GetCompleteQualType(getASTContext(), GetQualType(type),
3889 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00003890}
3891
Kate Stoneb9c1b512016-09-06 20:57:50 +00003892ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
3893 std::string type_name;
3894 if (type) {
3895 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
3896 clang::QualType qual_type(GetQualType(type));
3897 printing_policy.SuppressTagKeyword = true;
3898 const clang::TypedefType *typedef_type =
3899 qual_type->getAs<clang::TypedefType>();
3900 if (typedef_type) {
3901 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3902 type_name = typedef_decl->getQualifiedNameAsString();
3903 } else {
3904 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003905 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003906 }
3907 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00003908}
3909
3910uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003911ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
3912 CompilerType *pointee_or_element_clang_type) {
3913 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003914 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003915
3916 if (pointee_or_element_clang_type)
3917 pointee_or_element_clang_type->Clear();
3918
3919 clang::QualType qual_type(GetQualType(type));
3920
3921 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3922 switch (type_class) {
Sean Callananddf802a2017-06-02 01:24:18 +00003923 case clang::Type::Attributed:
3924 return GetTypeInfo(
3925 qual_type->getAs<clang::AttributedType>()
3926 ->getModifiedType().getAsOpaquePtr(),
3927 pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003928 case clang::Type::Builtin: {
3929 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3930 qual_type->getCanonicalTypeInternal());
3931
3932 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3933 switch (builtin_type->getKind()) {
3934 case clang::BuiltinType::ObjCId:
3935 case clang::BuiltinType::ObjCClass:
3936 if (pointee_or_element_clang_type)
3937 pointee_or_element_clang_type->SetCompilerType(
3938 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
3939 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3940 break;
3941
3942 case clang::BuiltinType::ObjCSel:
3943 if (pointee_or_element_clang_type)
3944 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
3945 getASTContext()->CharTy);
3946 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3947 break;
3948
3949 case clang::BuiltinType::Bool:
3950 case clang::BuiltinType::Char_U:
3951 case clang::BuiltinType::UChar:
3952 case clang::BuiltinType::WChar_U:
3953 case clang::BuiltinType::Char16:
3954 case clang::BuiltinType::Char32:
3955 case clang::BuiltinType::UShort:
3956 case clang::BuiltinType::UInt:
3957 case clang::BuiltinType::ULong:
3958 case clang::BuiltinType::ULongLong:
3959 case clang::BuiltinType::UInt128:
3960 case clang::BuiltinType::Char_S:
3961 case clang::BuiltinType::SChar:
3962 case clang::BuiltinType::WChar_S:
3963 case clang::BuiltinType::Short:
3964 case clang::BuiltinType::Int:
3965 case clang::BuiltinType::Long:
3966 case clang::BuiltinType::LongLong:
3967 case clang::BuiltinType::Int128:
3968 case clang::BuiltinType::Float:
3969 case clang::BuiltinType::Double:
3970 case clang::BuiltinType::LongDouble:
3971 builtin_type_flags |= eTypeIsScalar;
3972 if (builtin_type->isInteger()) {
3973 builtin_type_flags |= eTypeIsInteger;
3974 if (builtin_type->isSignedInteger())
3975 builtin_type_flags |= eTypeIsSigned;
3976 } else if (builtin_type->isFloatingPoint())
3977 builtin_type_flags |= eTypeIsFloat;
3978 break;
3979 default:
3980 break;
3981 }
3982 return builtin_type_flags;
3983 }
3984
3985 case clang::Type::BlockPointer:
3986 if (pointee_or_element_clang_type)
3987 pointee_or_element_clang_type->SetCompilerType(
3988 getASTContext(), qual_type->getPointeeType());
3989 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3990
3991 case clang::Type::Complex: {
3992 uint32_t complex_type_flags =
3993 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3994 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
3995 qual_type->getCanonicalTypeInternal());
3996 if (complex_type) {
3997 clang::QualType complex_element_type(complex_type->getElementType());
3998 if (complex_element_type->isIntegerType())
3999 complex_type_flags |= eTypeIsFloat;
4000 else if (complex_element_type->isFloatingType())
4001 complex_type_flags |= eTypeIsInteger;
4002 }
4003 return complex_type_flags;
4004 } break;
4005
4006 case clang::Type::ConstantArray:
4007 case clang::Type::DependentSizedArray:
4008 case clang::Type::IncompleteArray:
4009 case clang::Type::VariableArray:
4010 if (pointee_or_element_clang_type)
4011 pointee_or_element_clang_type->SetCompilerType(
4012 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4013 ->getElementType());
4014 return eTypeHasChildren | eTypeIsArray;
4015
4016 case clang::Type::DependentName:
4017 return 0;
4018 case clang::Type::DependentSizedExtVector:
4019 return eTypeHasChildren | eTypeIsVector;
4020 case clang::Type::DependentTemplateSpecialization:
4021 return eTypeIsTemplate;
4022 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004023 return CompilerType(
4024 getASTContext(),
4025 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
4026 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004027
4028 case clang::Type::Enum:
4029 if (pointee_or_element_clang_type)
4030 pointee_or_element_clang_type->SetCompilerType(
4031 getASTContext(),
4032 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4033 return eTypeIsEnumeration | eTypeHasValue;
4034
4035 case clang::Type::Auto:
4036 return CompilerType(
4037 getASTContext(),
4038 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4039 .GetTypeInfo(pointee_or_element_clang_type);
4040 case clang::Type::Elaborated:
4041 return CompilerType(
4042 getASTContext(),
4043 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4044 .GetTypeInfo(pointee_or_element_clang_type);
4045 case clang::Type::Paren:
4046 return CompilerType(getASTContext(),
4047 llvm::cast<clang::ParenType>(qual_type)->desugar())
4048 .GetTypeInfo(pointee_or_element_clang_type);
4049
4050 case clang::Type::FunctionProto:
4051 return eTypeIsFuncPrototype | eTypeHasValue;
4052 case clang::Type::FunctionNoProto:
4053 return eTypeIsFuncPrototype | eTypeHasValue;
4054 case clang::Type::InjectedClassName:
4055 return 0;
4056
4057 case clang::Type::LValueReference:
4058 case clang::Type::RValueReference:
4059 if (pointee_or_element_clang_type)
4060 pointee_or_element_clang_type->SetCompilerType(
4061 getASTContext(),
4062 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4063 ->getPointeeType());
4064 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4065
4066 case clang::Type::MemberPointer:
4067 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4068
4069 case clang::Type::ObjCObjectPointer:
4070 if (pointee_or_element_clang_type)
4071 pointee_or_element_clang_type->SetCompilerType(
4072 getASTContext(), qual_type->getPointeeType());
4073 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4074 eTypeHasValue;
4075
4076 case clang::Type::ObjCObject:
4077 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4078 case clang::Type::ObjCInterface:
4079 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4080
4081 case clang::Type::Pointer:
4082 if (pointee_or_element_clang_type)
4083 pointee_or_element_clang_type->SetCompilerType(
4084 getASTContext(), qual_type->getPointeeType());
4085 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4086
4087 case clang::Type::Record:
4088 if (qual_type->getAsCXXRecordDecl())
4089 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4090 else
4091 return eTypeHasChildren | eTypeIsStructUnion;
4092 break;
4093 case clang::Type::SubstTemplateTypeParm:
4094 return eTypeIsTemplate;
4095 case clang::Type::TemplateTypeParm:
4096 return eTypeIsTemplate;
4097 case clang::Type::TemplateSpecialization:
4098 return eTypeIsTemplate;
4099
4100 case clang::Type::Typedef:
4101 return eTypeIsTypedef |
4102 CompilerType(getASTContext(),
4103 llvm::cast<clang::TypedefType>(qual_type)
4104 ->getDecl()
4105 ->getUnderlyingType())
4106 .GetTypeInfo(pointee_or_element_clang_type);
4107 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004108 return CompilerType(getASTContext(),
4109 llvm::cast<clang::TypeOfExprType>(qual_type)
4110 ->getUnderlyingExpr()
4111 ->getType())
4112 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004113 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004114 return CompilerType(
4115 getASTContext(),
4116 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4117 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004118 case clang::Type::UnresolvedUsing:
4119 return 0;
4120
4121 case clang::Type::ExtVector:
4122 case clang::Type::Vector: {
4123 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4124 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4125 qual_type->getCanonicalTypeInternal());
4126 if (vector_type) {
4127 if (vector_type->isIntegerType())
4128 vector_type_flags |= eTypeIsFloat;
4129 else if (vector_type->isFloatingType())
4130 vector_type_flags |= eTypeIsInteger;
4131 }
4132 return vector_type_flags;
4133 }
4134 default:
4135 return 0;
4136 }
4137 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004138}
4139
Greg Claytond8d4a572015-08-11 21:38:15 +00004140lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004141ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4142 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004143 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004144
4145 // If the type is a reference, then resolve it to what it refers to first:
4146 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4147 if (qual_type->isAnyPointerType()) {
4148 if (qual_type->isObjCObjectPointerType())
4149 return lldb::eLanguageTypeObjC;
4150
4151 clang::QualType pointee_type(qual_type->getPointeeType());
4152 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4153 return lldb::eLanguageTypeC_plus_plus;
4154 if (pointee_type->isObjCObjectOrInterfaceType())
4155 return lldb::eLanguageTypeObjC;
4156 if (pointee_type->isObjCClassType())
4157 return lldb::eLanguageTypeObjC;
4158 if (pointee_type.getTypePtr() ==
4159 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4160 return lldb::eLanguageTypeObjC;
4161 } else {
4162 if (qual_type->isObjCObjectOrInterfaceType())
4163 return lldb::eLanguageTypeObjC;
4164 if (qual_type->getAsCXXRecordDecl())
4165 return lldb::eLanguageTypeC_plus_plus;
4166 switch (qual_type->getTypeClass()) {
4167 default:
4168 break;
4169 case clang::Type::Builtin:
4170 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4171 default:
4172 case clang::BuiltinType::Void:
4173 case clang::BuiltinType::Bool:
4174 case clang::BuiltinType::Char_U:
4175 case clang::BuiltinType::UChar:
4176 case clang::BuiltinType::WChar_U:
4177 case clang::BuiltinType::Char16:
4178 case clang::BuiltinType::Char32:
4179 case clang::BuiltinType::UShort:
4180 case clang::BuiltinType::UInt:
4181 case clang::BuiltinType::ULong:
4182 case clang::BuiltinType::ULongLong:
4183 case clang::BuiltinType::UInt128:
4184 case clang::BuiltinType::Char_S:
4185 case clang::BuiltinType::SChar:
4186 case clang::BuiltinType::WChar_S:
4187 case clang::BuiltinType::Short:
4188 case clang::BuiltinType::Int:
4189 case clang::BuiltinType::Long:
4190 case clang::BuiltinType::LongLong:
4191 case clang::BuiltinType::Int128:
4192 case clang::BuiltinType::Float:
4193 case clang::BuiltinType::Double:
4194 case clang::BuiltinType::LongDouble:
4195 break;
4196
4197 case clang::BuiltinType::NullPtr:
4198 return eLanguageTypeC_plus_plus;
4199
4200 case clang::BuiltinType::ObjCId:
4201 case clang::BuiltinType::ObjCClass:
4202 case clang::BuiltinType::ObjCSel:
4203 return eLanguageTypeObjC;
4204
4205 case clang::BuiltinType::Dependent:
4206 case clang::BuiltinType::Overload:
4207 case clang::BuiltinType::BoundMember:
4208 case clang::BuiltinType::UnknownAny:
4209 break;
4210 }
4211 break;
4212 case clang::Type::Typedef:
4213 return CompilerType(getASTContext(),
4214 llvm::cast<clang::TypedefType>(qual_type)
4215 ->getDecl()
4216 ->getUnderlyingType())
4217 .GetMinimumLanguage();
4218 }
4219 }
4220 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004221}
4222
4223lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004224ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4225 if (!type)
4226 return lldb::eTypeClassInvalid;
4227
4228 clang::QualType qual_type(GetQualType(type));
4229
4230 switch (qual_type->getTypeClass()) {
4231 case clang::Type::UnaryTransform:
4232 break;
4233 case clang::Type::FunctionNoProto:
4234 return lldb::eTypeClassFunction;
4235 case clang::Type::FunctionProto:
4236 return lldb::eTypeClassFunction;
4237 case clang::Type::IncompleteArray:
4238 return lldb::eTypeClassArray;
4239 case clang::Type::VariableArray:
4240 return lldb::eTypeClassArray;
4241 case clang::Type::ConstantArray:
4242 return lldb::eTypeClassArray;
4243 case clang::Type::DependentSizedArray:
4244 return lldb::eTypeClassArray;
4245 case clang::Type::DependentSizedExtVector:
4246 return lldb::eTypeClassVector;
Fangrui Song8f284882018-07-13 22:40:40 +00004247 case clang::Type::DependentVector:
4248 return lldb::eTypeClassVector;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004249 case clang::Type::ExtVector:
4250 return lldb::eTypeClassVector;
4251 case clang::Type::Vector:
4252 return lldb::eTypeClassVector;
4253 case clang::Type::Builtin:
4254 return lldb::eTypeClassBuiltin;
4255 case clang::Type::ObjCObjectPointer:
4256 return lldb::eTypeClassObjCObjectPointer;
4257 case clang::Type::BlockPointer:
4258 return lldb::eTypeClassBlockPointer;
4259 case clang::Type::Pointer:
4260 return lldb::eTypeClassPointer;
4261 case clang::Type::LValueReference:
4262 return lldb::eTypeClassReference;
4263 case clang::Type::RValueReference:
4264 return lldb::eTypeClassReference;
4265 case clang::Type::MemberPointer:
4266 return lldb::eTypeClassMemberPointer;
4267 case clang::Type::Complex:
4268 if (qual_type->isComplexType())
4269 return lldb::eTypeClassComplexFloat;
4270 else
4271 return lldb::eTypeClassComplexInteger;
4272 case clang::Type::ObjCObject:
4273 return lldb::eTypeClassObjCObject;
4274 case clang::Type::ObjCInterface:
4275 return lldb::eTypeClassObjCInterface;
4276 case clang::Type::Record: {
4277 const clang::RecordType *record_type =
4278 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4279 const clang::RecordDecl *record_decl = record_type->getDecl();
4280 if (record_decl->isUnion())
4281 return lldb::eTypeClassUnion;
4282 else if (record_decl->isStruct())
4283 return lldb::eTypeClassStruct;
4284 else
4285 return lldb::eTypeClassClass;
4286 } break;
4287 case clang::Type::Enum:
4288 return lldb::eTypeClassEnumeration;
4289 case clang::Type::Typedef:
4290 return lldb::eTypeClassTypedef;
4291 case clang::Type::UnresolvedUsing:
4292 break;
4293 case clang::Type::Paren:
4294 return CompilerType(getASTContext(),
4295 llvm::cast<clang::ParenType>(qual_type)->desugar())
4296 .GetTypeClass();
4297 case clang::Type::Auto:
4298 return CompilerType(
4299 getASTContext(),
4300 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4301 .GetTypeClass();
4302 case clang::Type::Elaborated:
4303 return CompilerType(
4304 getASTContext(),
4305 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4306 .GetTypeClass();
4307
4308 case clang::Type::Attributed:
4309 break;
4310 case clang::Type::TemplateTypeParm:
4311 break;
4312 case clang::Type::SubstTemplateTypeParm:
4313 break;
4314 case clang::Type::SubstTemplateTypeParmPack:
4315 break;
4316 case clang::Type::InjectedClassName:
4317 break;
4318 case clang::Type::DependentName:
4319 break;
4320 case clang::Type::DependentTemplateSpecialization:
4321 break;
4322 case clang::Type::PackExpansion:
4323 break;
4324
4325 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004326 return CompilerType(getASTContext(),
4327 llvm::cast<clang::TypeOfExprType>(qual_type)
4328 ->getUnderlyingExpr()
4329 ->getType())
4330 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004331 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004332 return CompilerType(
4333 getASTContext(),
4334 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4335 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004336 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004337 return CompilerType(
4338 getASTContext(),
4339 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4340 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004341 case clang::Type::TemplateSpecialization:
4342 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004343 case clang::Type::DeducedTemplateSpecialization:
4344 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004345 case clang::Type::Atomic:
4346 break;
4347 case clang::Type::Pipe:
4348 break;
4349
4350 // pointer type decayed from an array or function type.
4351 case clang::Type::Decayed:
4352 break;
4353 case clang::Type::Adjusted:
4354 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004355 case clang::Type::ObjCTypeParam:
4356 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00004357
4358 case clang::Type::DependentAddressSpace:
4359 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004360 }
4361 // We don't know hot to display this type...
4362 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004363}
4364
Kate Stoneb9c1b512016-09-06 20:57:50 +00004365unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4366 if (type)
4367 return GetQualType(type).getQualifiers().getCVRQualifiers();
4368 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004369}
4370
4371//----------------------------------------------------------------------
4372// Creating related types
4373//----------------------------------------------------------------------
4374
Greg Claytona1e5dc82015-08-11 22:53:00 +00004375CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004376ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4377 uint64_t *stride) {
4378 if (type) {
4379 clang::QualType qual_type(GetCanonicalQualType(type));
4380
4381 const clang::Type *array_eletype =
4382 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4383
4384 if (!array_eletype)
4385 return CompilerType();
4386
4387 CompilerType element_type(getASTContext(),
4388 array_eletype->getCanonicalTypeUnqualified());
4389
4390 // TODO: the real stride will be >= this value.. find the real one!
4391 if (stride)
4392 *stride = element_type.GetByteSize(nullptr);
4393
4394 return element_type;
4395 }
4396 return CompilerType();
4397}
4398
4399CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4400 uint64_t size) {
4401 if (type) {
4402 clang::QualType qual_type(GetCanonicalQualType(type));
4403 if (clang::ASTContext *ast_ctx = getASTContext()) {
4404 if (size != 0)
4405 return CompilerType(
4406 ast_ctx, ast_ctx->getConstantArrayType(
4407 qual_type, llvm::APInt(64, size),
4408 clang::ArrayType::ArraySizeModifier::Normal, 0));
4409 else
4410 return CompilerType(
4411 ast_ctx,
4412 ast_ctx->getIncompleteArrayType(
4413 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
Greg Claytond8d4a572015-08-11 21:38:15 +00004414 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004415 }
4416
4417 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004418}
4419
Greg Claytona1e5dc82015-08-11 22:53:00 +00004420CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004421ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4422 if (type)
4423 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4424 return CompilerType();
4425}
4426
4427static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4428 clang::QualType qual_type) {
4429 if (qual_type->isPointerType())
4430 qual_type = ast->getPointerType(
4431 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4432 else
4433 qual_type = qual_type.getUnqualifiedType();
4434 qual_type.removeLocalConst();
4435 qual_type.removeLocalRestrict();
4436 qual_type.removeLocalVolatile();
4437 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004438}
4439
4440CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004441ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4442 if (type)
4443 return CompilerType(
4444 getASTContext(),
4445 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4446 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004447}
4448
Kate Stoneb9c1b512016-09-06 20:57:50 +00004449int ClangASTContext::GetFunctionArgumentCount(
4450 lldb::opaque_compiler_type_t type) {
4451 if (type) {
4452 const clang::FunctionProtoType *func =
4453 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4454 if (func)
4455 return func->getNumParams();
4456 }
4457 return -1;
4458}
4459
4460CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4461 lldb::opaque_compiler_type_t type, size_t idx) {
4462 if (type) {
4463 const clang::FunctionProtoType *func =
4464 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4465 if (func) {
4466 const uint32_t num_args = func->getNumParams();
4467 if (idx < num_args)
4468 return CompilerType(getASTContext(), func->getParamType(idx));
4469 }
4470 }
4471 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004472}
4473
Greg Claytona1e5dc82015-08-11 22:53:00 +00004474CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004475ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4476 if (type) {
4477 clang::QualType qual_type(GetQualType(type));
4478 const clang::FunctionProtoType *func =
4479 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4480 if (func)
4481 return CompilerType(getASTContext(), func->getReturnType());
4482 }
4483 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004484}
4485
4486size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004487ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4488 size_t num_functions = 0;
4489 if (type) {
4490 clang::QualType qual_type(GetCanonicalQualType(type));
4491 switch (qual_type->getTypeClass()) {
4492 case clang::Type::Record:
4493 if (GetCompleteQualType(getASTContext(), qual_type)) {
4494 const clang::RecordType *record_type =
4495 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4496 const clang::RecordDecl *record_decl = record_type->getDecl();
4497 assert(record_decl);
4498 const clang::CXXRecordDecl *cxx_record_decl =
4499 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4500 if (cxx_record_decl)
4501 num_functions = std::distance(cxx_record_decl->method_begin(),
4502 cxx_record_decl->method_end());
4503 }
4504 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004505
Sean Callananf9c622a2016-09-30 18:44:43 +00004506 case clang::Type::ObjCObjectPointer: {
4507 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004508 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004509 const clang::ObjCInterfaceType *objc_interface_type =
4510 objc_class_type->getInterfaceType();
4511 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004512 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4513 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004514 clang::ObjCInterfaceDecl *class_interface_decl =
4515 objc_interface_type->getDecl();
4516 if (class_interface_decl) {
4517 num_functions = std::distance(class_interface_decl->meth_begin(),
4518 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004519 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004520 }
4521 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004522 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004523
4524 case clang::Type::ObjCObject:
4525 case clang::Type::ObjCInterface:
4526 if (GetCompleteType(type)) {
4527 const clang::ObjCObjectType *objc_class_type =
4528 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4529 if (objc_class_type) {
4530 clang::ObjCInterfaceDecl *class_interface_decl =
4531 objc_class_type->getInterface();
4532 if (class_interface_decl)
4533 num_functions = std::distance(class_interface_decl->meth_begin(),
4534 class_interface_decl->meth_end());
4535 }
4536 }
4537 break;
4538
4539 case clang::Type::Typedef:
4540 return CompilerType(getASTContext(),
4541 llvm::cast<clang::TypedefType>(qual_type)
4542 ->getDecl()
4543 ->getUnderlyingType())
4544 .GetNumMemberFunctions();
4545
4546 case clang::Type::Auto:
4547 return CompilerType(
4548 getASTContext(),
4549 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4550 .GetNumMemberFunctions();
4551
4552 case clang::Type::Elaborated:
4553 return CompilerType(
4554 getASTContext(),
4555 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4556 .GetNumMemberFunctions();
4557
4558 case clang::Type::Paren:
4559 return CompilerType(getASTContext(),
4560 llvm::cast<clang::ParenType>(qual_type)->desugar())
4561 .GetNumMemberFunctions();
4562
4563 default:
4564 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004565 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004566 }
4567 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004568}
4569
4570TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004571ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4572 size_t idx) {
4573 std::string name;
4574 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4575 CompilerType clang_type;
4576 CompilerDecl clang_decl;
4577 if (type) {
4578 clang::QualType qual_type(GetCanonicalQualType(type));
4579 switch (qual_type->getTypeClass()) {
4580 case clang::Type::Record:
4581 if (GetCompleteQualType(getASTContext(), qual_type)) {
4582 const clang::RecordType *record_type =
4583 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4584 const clang::RecordDecl *record_decl = record_type->getDecl();
4585 assert(record_decl);
4586 const clang::CXXRecordDecl *cxx_record_decl =
4587 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4588 if (cxx_record_decl) {
4589 auto method_iter = cxx_record_decl->method_begin();
4590 auto method_end = cxx_record_decl->method_end();
4591 if (idx <
4592 static_cast<size_t>(std::distance(method_iter, method_end))) {
4593 std::advance(method_iter, idx);
4594 clang::CXXMethodDecl *cxx_method_decl =
4595 method_iter->getCanonicalDecl();
4596 if (cxx_method_decl) {
4597 name = cxx_method_decl->getDeclName().getAsString();
4598 if (cxx_method_decl->isStatic())
4599 kind = lldb::eMemberFunctionKindStaticMethod;
4600 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4601 kind = lldb::eMemberFunctionKindConstructor;
4602 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4603 kind = lldb::eMemberFunctionKindDestructor;
4604 else
4605 kind = lldb::eMemberFunctionKindInstanceMethod;
4606 clang_type = CompilerType(
4607 this, cxx_method_decl->getType().getAsOpaquePtr());
4608 clang_decl = CompilerDecl(this, cxx_method_decl);
4609 }
4610 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004611 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004612 }
4613 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004614
Sean Callananf9c622a2016-09-30 18:44:43 +00004615 case clang::Type::ObjCObjectPointer: {
4616 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004617 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004618 const clang::ObjCInterfaceType *objc_interface_type =
4619 objc_class_type->getInterfaceType();
4620 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004621 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4622 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004623 clang::ObjCInterfaceDecl *class_interface_decl =
4624 objc_interface_type->getDecl();
4625 if (class_interface_decl) {
4626 auto method_iter = class_interface_decl->meth_begin();
4627 auto method_end = class_interface_decl->meth_end();
4628 if (idx <
4629 static_cast<size_t>(std::distance(method_iter, method_end))) {
4630 std::advance(method_iter, idx);
4631 clang::ObjCMethodDecl *objc_method_decl =
4632 method_iter->getCanonicalDecl();
4633 if (objc_method_decl) {
4634 clang_decl = CompilerDecl(this, objc_method_decl);
4635 name = objc_method_decl->getSelector().getAsString();
4636 if (objc_method_decl->isClassMethod())
4637 kind = lldb::eMemberFunctionKindStaticMethod;
4638 else
4639 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004640 }
4641 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004642 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004643 }
4644 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004645 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004646
Kate Stoneb9c1b512016-09-06 20:57:50 +00004647 case clang::Type::ObjCObject:
4648 case clang::Type::ObjCInterface:
4649 if (GetCompleteType(type)) {
4650 const clang::ObjCObjectType *objc_class_type =
4651 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4652 if (objc_class_type) {
4653 clang::ObjCInterfaceDecl *class_interface_decl =
4654 objc_class_type->getInterface();
4655 if (class_interface_decl) {
4656 auto method_iter = class_interface_decl->meth_begin();
4657 auto method_end = class_interface_decl->meth_end();
4658 if (idx <
4659 static_cast<size_t>(std::distance(method_iter, method_end))) {
4660 std::advance(method_iter, idx);
4661 clang::ObjCMethodDecl *objc_method_decl =
4662 method_iter->getCanonicalDecl();
4663 if (objc_method_decl) {
4664 clang_decl = CompilerDecl(this, objc_method_decl);
4665 name = objc_method_decl->getSelector().getAsString();
4666 if (objc_method_decl->isClassMethod())
4667 kind = lldb::eMemberFunctionKindStaticMethod;
4668 else
4669 kind = lldb::eMemberFunctionKindInstanceMethod;
4670 }
4671 }
4672 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004673 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004674 }
4675 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004676
Kate Stoneb9c1b512016-09-06 20:57:50 +00004677 case clang::Type::Typedef:
4678 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4679 ->getDecl()
4680 ->getUnderlyingType()
4681 .getAsOpaquePtr(),
4682 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004683
Kate Stoneb9c1b512016-09-06 20:57:50 +00004684 case clang::Type::Auto:
4685 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4686 ->getDeducedType()
4687 .getAsOpaquePtr(),
4688 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004689
Kate Stoneb9c1b512016-09-06 20:57:50 +00004690 case clang::Type::Elaborated:
4691 return GetMemberFunctionAtIndex(
4692 llvm::cast<clang::ElaboratedType>(qual_type)
4693 ->getNamedType()
4694 .getAsOpaquePtr(),
4695 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004696
Kate Stoneb9c1b512016-09-06 20:57:50 +00004697 case clang::Type::Paren:
4698 return GetMemberFunctionAtIndex(
4699 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4700 idx);
4701
4702 default:
4703 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004704 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004705 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004706
Kate Stoneb9c1b512016-09-06 20:57:50 +00004707 if (kind == eMemberFunctionKindUnknown)
4708 return TypeMemberFunctionImpl();
4709 else
4710 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004711}
4712
Greg Claytona1e5dc82015-08-11 22:53:00 +00004713CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004714ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4715 if (type)
4716 return CompilerType(getASTContext(),
4717 GetQualType(type).getNonReferenceType());
4718 return CompilerType();
4719}
4720
4721CompilerType ClangASTContext::CreateTypedefType(
4722 const CompilerType &type, const char *typedef_name,
4723 const CompilerDeclContext &compiler_decl_ctx) {
4724 if (type && typedef_name && typedef_name[0]) {
4725 ClangASTContext *ast =
4726 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4727 if (!ast)
4728 return CompilerType();
4729 clang::ASTContext *clang_ast = ast->getASTContext();
4730 clang::QualType qual_type(ClangUtil::GetQualType(type));
4731
4732 clang::DeclContext *decl_ctx =
4733 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4734 if (decl_ctx == nullptr)
4735 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4736
4737 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4738 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4739 &clang_ast->Idents.get(typedef_name),
4740 clang_ast->getTrivialTypeSourceInfo(qual_type));
4741
4742 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4743
Aleksandr Urakov709426b2018-09-10 08:08:43 +00004744 decl_ctx->addDecl(decl);
4745
Kate Stoneb9c1b512016-09-06 20:57:50 +00004746 // Get a uniqued clang::QualType for the typedef decl type
4747 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4748 }
4749 return CompilerType();
4750}
4751
4752CompilerType
4753ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4754 if (type) {
4755 clang::QualType qual_type(GetQualType(type));
4756 return CompilerType(getASTContext(),
4757 qual_type.getTypePtr()->getPointeeType());
4758 }
4759 return CompilerType();
4760}
4761
4762CompilerType
4763ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4764 if (type) {
4765 clang::QualType qual_type(GetQualType(type));
4766
4767 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4768 switch (type_class) {
4769 case clang::Type::ObjCObject:
4770 case clang::Type::ObjCInterface:
4771 return CompilerType(getASTContext(),
4772 getASTContext()->getObjCObjectPointerType(qual_type));
4773
4774 default:
4775 return CompilerType(getASTContext(),
4776 getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004777 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004778 }
4779 return CompilerType();
4780}
4781
4782CompilerType
4783ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4784 if (type)
4785 return CompilerType(this, getASTContext()
4786 ->getLValueReferenceType(GetQualType(type))
4787 .getAsOpaquePtr());
4788 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004789 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004790}
4791
Kate Stoneb9c1b512016-09-06 20:57:50 +00004792CompilerType
4793ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4794 if (type)
4795 return CompilerType(this, getASTContext()
4796 ->getRValueReferenceType(GetQualType(type))
4797 .getAsOpaquePtr());
4798 else
4799 return CompilerType();
4800}
4801
4802CompilerType
4803ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4804 if (type) {
4805 clang::QualType result(GetQualType(type));
4806 result.addConst();
4807 return CompilerType(this, result.getAsOpaquePtr());
4808 }
4809 return CompilerType();
4810}
4811
4812CompilerType
4813ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4814 if (type) {
4815 clang::QualType result(GetQualType(type));
4816 result.addVolatile();
4817 return CompilerType(this, result.getAsOpaquePtr());
4818 }
4819 return CompilerType();
4820}
4821
4822CompilerType
4823ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4824 if (type) {
4825 clang::QualType result(GetQualType(type));
4826 result.addRestrict();
4827 return CompilerType(this, result.getAsOpaquePtr());
4828 }
4829 return CompilerType();
4830}
4831
4832CompilerType
4833ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4834 const char *typedef_name,
4835 const CompilerDeclContext &compiler_decl_ctx) {
4836 if (type) {
4837 clang::ASTContext *clang_ast = getASTContext();
4838 clang::QualType qual_type(GetQualType(type));
4839
4840 clang::DeclContext *decl_ctx =
4841 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4842 if (decl_ctx == nullptr)
4843 decl_ctx = getASTContext()->getTranslationUnitDecl();
4844
4845 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4846 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4847 &clang_ast->Idents.get(typedef_name),
4848 clang_ast->getTrivialTypeSourceInfo(qual_type));
4849
4850 clang::TagDecl *tdecl = nullptr;
4851 if (!qual_type.isNull()) {
4852 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4853 tdecl = rt->getDecl();
4854 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4855 tdecl = et->getDecl();
4856 }
4857
4858 // Check whether this declaration is an anonymous struct, union, or enum,
Adrian Prantl05097242018-04-30 16:49:04 +00004859 // hidden behind a typedef. If so, we try to check whether we have a
4860 // typedef tag to attach to the original record declaration
Kate Stoneb9c1b512016-09-06 20:57:50 +00004861 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4862 tdecl->setTypedefNameForAnonDecl(decl);
4863
4864 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4865
4866 // Get a uniqued clang::QualType for the typedef decl type
4867 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4868 }
4869 return CompilerType();
4870}
4871
4872CompilerType
4873ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4874 if (type) {
4875 const clang::TypedefType *typedef_type =
4876 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4877 if (typedef_type)
4878 return CompilerType(getASTContext(),
4879 typedef_type->getDecl()->getUnderlyingType());
4880 }
4881 return CompilerType();
4882}
Greg Claytond8d4a572015-08-11 21:38:15 +00004883
4884//----------------------------------------------------------------------
4885// Create related types using the current type's AST
4886//----------------------------------------------------------------------
4887
Kate Stoneb9c1b512016-09-06 20:57:50 +00004888CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4889 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004890}
4891//----------------------------------------------------------------------
4892// Exploring the type
4893//----------------------------------------------------------------------
4894
Kate Stoneb9c1b512016-09-06 20:57:50 +00004895uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4896 ExecutionContextScope *exe_scope) {
4897 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00004898 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004899 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004900 switch (type_class) {
4901 case clang::Type::Record:
4902 if (GetCompleteType(type))
4903 return getASTContext()->getTypeSize(qual_type);
4904 else
4905 return 0;
4906 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004907
Kate Stoneb9c1b512016-09-06 20:57:50 +00004908 case clang::Type::ObjCInterface:
4909 case clang::Type::ObjCObject: {
4910 ExecutionContext exe_ctx(exe_scope);
4911 Process *process = exe_ctx.GetProcessPtr();
4912 if (process) {
4913 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4914 if (objc_runtime) {
4915 uint64_t bit_size = 0;
4916 if (objc_runtime->GetTypeBitSize(
4917 CompilerType(getASTContext(), qual_type), bit_size))
4918 return bit_size;
4919 }
4920 } else {
4921 static bool g_printed = false;
4922 if (!g_printed) {
4923 StreamString s;
4924 DumpTypeDescription(type, &s);
4925
4926 llvm::outs() << "warning: trying to determine the size of type ";
4927 llvm::outs() << s.GetString() << "\n";
4928 llvm::outs() << "without a valid ExecutionContext. this is not "
4929 "reliable. please file a bug against LLDB.\n";
4930 llvm::outs() << "backtrace:\n";
4931 llvm::sys::PrintStackTrace(llvm::outs());
4932 llvm::outs() << "\n";
4933 g_printed = true;
4934 }
4935 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004936 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004937 LLVM_FALLTHROUGH;
4938 default:
4939 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
4940 if (bit_size == 0) {
4941 if (qual_type->isIncompleteArrayType())
4942 return getASTContext()->getTypeSize(
4943 qual_type->getArrayElementTypeNoTypeQual()
4944 ->getCanonicalTypeUnqualified());
4945 }
4946 if (qual_type->isObjCObjectOrInterfaceType())
4947 return bit_size +
4948 getASTContext()->getTypeSize(
4949 getASTContext()->ObjCBuiltinClassTy);
4950 return bit_size;
4951 }
4952 }
4953 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004954}
4955
Kate Stoneb9c1b512016-09-06 20:57:50 +00004956size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
4957 if (GetCompleteType(type))
4958 return getASTContext()->getTypeAlign(GetQualType(type));
4959 return 0;
4960}
4961
4962lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
4963 uint64_t &count) {
4964 if (!type)
4965 return lldb::eEncodingInvalid;
4966
4967 count = 1;
4968 clang::QualType qual_type(GetCanonicalQualType(type));
4969
4970 switch (qual_type->getTypeClass()) {
4971 case clang::Type::UnaryTransform:
4972 break;
4973
4974 case clang::Type::FunctionNoProto:
4975 case clang::Type::FunctionProto:
4976 break;
4977
4978 case clang::Type::IncompleteArray:
4979 case clang::Type::VariableArray:
4980 break;
4981
4982 case clang::Type::ConstantArray:
4983 break;
4984
Fangrui Song8f284882018-07-13 22:40:40 +00004985 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004986 case clang::Type::ExtVector:
4987 case clang::Type::Vector:
4988 // TODO: Set this to more than one???
4989 break;
4990
4991 case clang::Type::Builtin:
4992 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4993 case clang::BuiltinType::Void:
4994 break;
4995
4996 case clang::BuiltinType::Bool:
4997 case clang::BuiltinType::Char_S:
4998 case clang::BuiltinType::SChar:
4999 case clang::BuiltinType::WChar_S:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005000 case clang::BuiltinType::Short:
5001 case clang::BuiltinType::Int:
5002 case clang::BuiltinType::Long:
5003 case clang::BuiltinType::LongLong:
5004 case clang::BuiltinType::Int128:
5005 return lldb::eEncodingSint;
5006
5007 case clang::BuiltinType::Char_U:
5008 case clang::BuiltinType::UChar:
5009 case clang::BuiltinType::WChar_U:
Richard Smith51d12d82018-05-02 02:43:22 +00005010 case clang::BuiltinType::Char8:
5011 case clang::BuiltinType::Char16:
5012 case clang::BuiltinType::Char32:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005013 case clang::BuiltinType::UShort:
5014 case clang::BuiltinType::UInt:
5015 case clang::BuiltinType::ULong:
5016 case clang::BuiltinType::ULongLong:
5017 case clang::BuiltinType::UInt128:
5018 return lldb::eEncodingUint;
5019
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005020 // Fixed point types. Note that they are currently ignored.
5021 case clang::BuiltinType::ShortAccum:
5022 case clang::BuiltinType::Accum:
5023 case clang::BuiltinType::LongAccum:
5024 case clang::BuiltinType::UShortAccum:
5025 case clang::BuiltinType::UAccum:
5026 case clang::BuiltinType::ULongAccum:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005027 case clang::BuiltinType::ShortFract:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005028 case clang::BuiltinType::Fract:
5029 case clang::BuiltinType::LongFract:
5030 case clang::BuiltinType::UShortFract:
5031 case clang::BuiltinType::UFract:
5032 case clang::BuiltinType::ULongFract:
5033 case clang::BuiltinType::SatShortAccum:
5034 case clang::BuiltinType::SatAccum:
5035 case clang::BuiltinType::SatLongAccum:
5036 case clang::BuiltinType::SatUShortAccum:
5037 case clang::BuiltinType::SatUAccum:
5038 case clang::BuiltinType::SatULongAccum:
5039 case clang::BuiltinType::SatShortFract:
5040 case clang::BuiltinType::SatFract:
5041 case clang::BuiltinType::SatLongFract:
5042 case clang::BuiltinType::SatUShortFract:
5043 case clang::BuiltinType::SatUFract:
5044 case clang::BuiltinType::SatULongFract:
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005045 break;
5046
Kate Stoneb9c1b512016-09-06 20:57:50 +00005047 case clang::BuiltinType::Half:
5048 case clang::BuiltinType::Float:
Ted Woodward4355c7c2017-09-20 19:16:53 +00005049 case clang::BuiltinType::Float16:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005050 case clang::BuiltinType::Float128:
5051 case clang::BuiltinType::Double:
5052 case clang::BuiltinType::LongDouble:
5053 return lldb::eEncodingIEEE754;
5054
5055 case clang::BuiltinType::ObjCClass:
5056 case clang::BuiltinType::ObjCId:
5057 case clang::BuiltinType::ObjCSel:
5058 return lldb::eEncodingUint;
5059
5060 case clang::BuiltinType::NullPtr:
5061 return lldb::eEncodingUint;
5062
5063 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5064 case clang::BuiltinType::Kind::BoundMember:
5065 case clang::BuiltinType::Kind::BuiltinFn:
5066 case clang::BuiltinType::Kind::Dependent:
5067 case clang::BuiltinType::Kind::OCLClkEvent:
5068 case clang::BuiltinType::Kind::OCLEvent:
5069 case clang::BuiltinType::Kind::OCLImage1dRO:
5070 case clang::BuiltinType::Kind::OCLImage1dWO:
5071 case clang::BuiltinType::Kind::OCLImage1dRW:
5072 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5073 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5074 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5075 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5076 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5077 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5078 case clang::BuiltinType::Kind::OCLImage2dRO:
5079 case clang::BuiltinType::Kind::OCLImage2dWO:
5080 case clang::BuiltinType::Kind::OCLImage2dRW:
5081 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5082 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5083 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5084 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5085 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5086 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5087 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5088 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5089 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5090 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5091 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5092 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5093 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5094 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5095 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5096 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5097 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5098 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5099 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5100 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5101 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5102 case clang::BuiltinType::Kind::OCLImage3dRO:
5103 case clang::BuiltinType::Kind::OCLImage3dWO:
5104 case clang::BuiltinType::Kind::OCLImage3dRW:
5105 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005106 case clang::BuiltinType::Kind::OCLReserveID:
5107 case clang::BuiltinType::Kind::OCLSampler:
5108 case clang::BuiltinType::Kind::OMPArraySection:
5109 case clang::BuiltinType::Kind::Overload:
5110 case clang::BuiltinType::Kind::PseudoObject:
5111 case clang::BuiltinType::Kind::UnknownAny:
5112 break;
Jorge Gorbe Moyaa6e6c182018-11-08 22:04:58 +00005113
5114 case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload:
5115 case clang::BuiltinType::OCLIntelSubgroupAVCImePayload:
5116 case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload:
5117 case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload:
5118 case clang::BuiltinType::OCLIntelSubgroupAVCMceResult:
5119 case clang::BuiltinType::OCLIntelSubgroupAVCImeResult:
5120 case clang::BuiltinType::OCLIntelSubgroupAVCRefResult:
5121 case clang::BuiltinType::OCLIntelSubgroupAVCSicResult:
5122 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleRefStreamout:
5123 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualRefStreamout:
5124 case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleRefStreamin:
5125 case clang::BuiltinType::OCLIntelSubgroupAVCImeDualRefStreamin:
5126 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005127 }
5128 break;
Adrian Prantl05097242018-04-30 16:49:04 +00005129 // All pointer types are represented as unsigned integer encodings. We may
5130 // nee to add a eEncodingPointer if we ever need to know the difference
Kate Stoneb9c1b512016-09-06 20:57:50 +00005131 case clang::Type::ObjCObjectPointer:
5132 case clang::Type::BlockPointer:
5133 case clang::Type::Pointer:
5134 case clang::Type::LValueReference:
5135 case clang::Type::RValueReference:
5136 case clang::Type::MemberPointer:
5137 return lldb::eEncodingUint;
5138 case clang::Type::Complex: {
5139 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5140 if (qual_type->isComplexType())
5141 encoding = lldb::eEncodingIEEE754;
5142 else {
5143 const clang::ComplexType *complex_type =
5144 qual_type->getAsComplexIntegerType();
5145 if (complex_type)
5146 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5147 .GetEncoding(count);
5148 else
5149 encoding = lldb::eEncodingSint;
5150 }
5151 count = 2;
5152 return encoding;
5153 }
5154
5155 case clang::Type::ObjCInterface:
5156 break;
5157 case clang::Type::Record:
5158 break;
5159 case clang::Type::Enum:
5160 return lldb::eEncodingSint;
5161 case clang::Type::Typedef:
5162 return CompilerType(getASTContext(),
5163 llvm::cast<clang::TypedefType>(qual_type)
5164 ->getDecl()
5165 ->getUnderlyingType())
5166 .GetEncoding(count);
5167
5168 case clang::Type::Auto:
5169 return CompilerType(
5170 getASTContext(),
5171 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5172 .GetEncoding(count);
5173
5174 case clang::Type::Elaborated:
5175 return CompilerType(
5176 getASTContext(),
5177 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5178 .GetEncoding(count);
5179
5180 case clang::Type::Paren:
5181 return CompilerType(getASTContext(),
5182 llvm::cast<clang::ParenType>(qual_type)->desugar())
5183 .GetEncoding(count);
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005184 case clang::Type::TypeOfExpr:
5185 return CompilerType(getASTContext(),
5186 llvm::cast<clang::TypeOfExprType>(qual_type)
5187 ->getUnderlyingExpr()
5188 ->getType())
5189 .GetEncoding(count);
5190 case clang::Type::TypeOf:
5191 return CompilerType(
5192 getASTContext(),
5193 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5194 .GetEncoding(count);
5195 case clang::Type::Decltype:
5196 return CompilerType(
5197 getASTContext(),
5198 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5199 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005200 case clang::Type::DependentSizedArray:
5201 case clang::Type::DependentSizedExtVector:
5202 case clang::Type::UnresolvedUsing:
5203 case clang::Type::Attributed:
5204 case clang::Type::TemplateTypeParm:
5205 case clang::Type::SubstTemplateTypeParm:
5206 case clang::Type::SubstTemplateTypeParmPack:
5207 case clang::Type::InjectedClassName:
5208 case clang::Type::DependentName:
5209 case clang::Type::DependentTemplateSpecialization:
5210 case clang::Type::PackExpansion:
5211 case clang::Type::ObjCObject:
5212
Kate Stoneb9c1b512016-09-06 20:57:50 +00005213 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005214 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005215 case clang::Type::Atomic:
5216 case clang::Type::Adjusted:
5217 case clang::Type::Pipe:
5218 break;
5219
5220 // pointer type decayed from an array or function type.
5221 case clang::Type::Decayed:
5222 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005223 case clang::Type::ObjCTypeParam:
5224 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005225
5226 case clang::Type::DependentAddressSpace:
5227 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005228 }
5229 count = 0;
5230 return lldb::eEncodingInvalid;
5231}
5232
5233lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5234 if (!type)
5235 return lldb::eFormatDefault;
5236
5237 clang::QualType qual_type(GetCanonicalQualType(type));
5238
5239 switch (qual_type->getTypeClass()) {
5240 case clang::Type::UnaryTransform:
5241 break;
5242
5243 case clang::Type::FunctionNoProto:
5244 case clang::Type::FunctionProto:
5245 break;
5246
5247 case clang::Type::IncompleteArray:
5248 case clang::Type::VariableArray:
5249 break;
5250
5251 case clang::Type::ConstantArray:
5252 return lldb::eFormatVoid; // no value
5253
Fangrui Song8f284882018-07-13 22:40:40 +00005254 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005255 case clang::Type::ExtVector:
5256 case clang::Type::Vector:
5257 break;
5258
5259 case clang::Type::Builtin:
5260 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5261 // default: assert(0 && "Unknown builtin type!");
5262 case clang::BuiltinType::UnknownAny:
5263 case clang::BuiltinType::Void:
5264 case clang::BuiltinType::BoundMember:
5265 break;
5266
5267 case clang::BuiltinType::Bool:
5268 return lldb::eFormatBoolean;
5269 case clang::BuiltinType::Char_S:
5270 case clang::BuiltinType::SChar:
5271 case clang::BuiltinType::WChar_S:
5272 case clang::BuiltinType::Char_U:
5273 case clang::BuiltinType::UChar:
5274 case clang::BuiltinType::WChar_U:
5275 return lldb::eFormatChar;
5276 case clang::BuiltinType::Char16:
5277 return lldb::eFormatUnicode16;
5278 case clang::BuiltinType::Char32:
5279 return lldb::eFormatUnicode32;
5280 case clang::BuiltinType::UShort:
5281 return lldb::eFormatUnsigned;
5282 case clang::BuiltinType::Short:
5283 return lldb::eFormatDecimal;
5284 case clang::BuiltinType::UInt:
5285 return lldb::eFormatUnsigned;
5286 case clang::BuiltinType::Int:
5287 return lldb::eFormatDecimal;
5288 case clang::BuiltinType::ULong:
5289 return lldb::eFormatUnsigned;
5290 case clang::BuiltinType::Long:
5291 return lldb::eFormatDecimal;
5292 case clang::BuiltinType::ULongLong:
5293 return lldb::eFormatUnsigned;
5294 case clang::BuiltinType::LongLong:
5295 return lldb::eFormatDecimal;
5296 case clang::BuiltinType::UInt128:
5297 return lldb::eFormatUnsigned;
5298 case clang::BuiltinType::Int128:
5299 return lldb::eFormatDecimal;
5300 case clang::BuiltinType::Half:
5301 case clang::BuiltinType::Float:
5302 case clang::BuiltinType::Double:
5303 case clang::BuiltinType::LongDouble:
5304 return lldb::eFormatFloat;
5305 default:
5306 return lldb::eFormatHex;
5307 }
5308 break;
5309 case clang::Type::ObjCObjectPointer:
5310 return lldb::eFormatHex;
5311 case clang::Type::BlockPointer:
5312 return lldb::eFormatHex;
5313 case clang::Type::Pointer:
5314 return lldb::eFormatHex;
5315 case clang::Type::LValueReference:
5316 case clang::Type::RValueReference:
5317 return lldb::eFormatHex;
5318 case clang::Type::MemberPointer:
5319 break;
5320 case clang::Type::Complex: {
5321 if (qual_type->isComplexType())
5322 return lldb::eFormatComplex;
5323 else
5324 return lldb::eFormatComplexInteger;
5325 }
5326 case clang::Type::ObjCInterface:
5327 break;
5328 case clang::Type::Record:
5329 break;
5330 case clang::Type::Enum:
5331 return lldb::eFormatEnum;
5332 case clang::Type::Typedef:
5333 return CompilerType(getASTContext(),
5334 llvm::cast<clang::TypedefType>(qual_type)
5335 ->getDecl()
5336 ->getUnderlyingType())
5337 .GetFormat();
5338 case clang::Type::Auto:
5339 return CompilerType(getASTContext(),
5340 llvm::cast<clang::AutoType>(qual_type)->desugar())
5341 .GetFormat();
5342 case clang::Type::Paren:
5343 return CompilerType(getASTContext(),
5344 llvm::cast<clang::ParenType>(qual_type)->desugar())
5345 .GetFormat();
5346 case clang::Type::Elaborated:
5347 return CompilerType(
5348 getASTContext(),
5349 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5350 .GetFormat();
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005351 case clang::Type::TypeOfExpr:
5352 return CompilerType(getASTContext(),
5353 llvm::cast<clang::TypeOfExprType>(qual_type)
5354 ->getUnderlyingExpr()
5355 ->getType())
5356 .GetFormat();
5357 case clang::Type::TypeOf:
5358 return CompilerType(
5359 getASTContext(),
5360 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5361 .GetFormat();
5362 case clang::Type::Decltype:
5363 return CompilerType(
5364 getASTContext(),
5365 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5366 .GetFormat();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005367 case clang::Type::DependentSizedArray:
5368 case clang::Type::DependentSizedExtVector:
5369 case clang::Type::UnresolvedUsing:
5370 case clang::Type::Attributed:
5371 case clang::Type::TemplateTypeParm:
5372 case clang::Type::SubstTemplateTypeParm:
5373 case clang::Type::SubstTemplateTypeParmPack:
5374 case clang::Type::InjectedClassName:
5375 case clang::Type::DependentName:
5376 case clang::Type::DependentTemplateSpecialization:
5377 case clang::Type::PackExpansion:
5378 case clang::Type::ObjCObject:
5379
Kate Stoneb9c1b512016-09-06 20:57:50 +00005380 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005381 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005382 case clang::Type::Atomic:
5383 case clang::Type::Adjusted:
5384 case clang::Type::Pipe:
5385 break;
5386
5387 // pointer type decayed from an array or function type.
5388 case clang::Type::Decayed:
5389 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005390 case clang::Type::ObjCTypeParam:
5391 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005392
5393 case clang::Type::DependentAddressSpace:
5394 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005395 }
5396 // We don't know hot to display this type...
5397 return lldb::eFormatBytes;
5398}
5399
5400static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5401 bool check_superclass) {
5402 while (class_interface_decl) {
5403 if (class_interface_decl->ivar_size() > 0)
5404 return true;
5405
5406 if (check_superclass)
5407 class_interface_decl = class_interface_decl->getSuperClass();
5408 else
5409 break;
5410 }
5411 return false;
5412}
5413
Adrian Prantleca07c52018-11-05 20:49:07 +00005414static llvm::Optional<SymbolFile::ArrayInfo>
5415GetDynamicArrayInfo(ClangASTContext &ast, SymbolFile *sym_file,
5416 clang::QualType qual_type,
5417 const ExecutionContext *exe_ctx) {
5418 if (qual_type->isIncompleteArrayType())
5419 if (auto *metadata = ast.GetMetadata(qual_type.getAsOpaquePtr()))
5420 if (auto *dwarf_parser = ast.GetDWARFParser())
5421 return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(),
5422 exe_ctx);
5423 return llvm::None;
5424}
5425
Kate Stoneb9c1b512016-09-06 20:57:50 +00005426uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
Adrian Prantleca07c52018-11-05 20:49:07 +00005427 bool omit_empty_base_classes,
5428 const ExecutionContext *exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005429 if (!type)
5430 return 0;
5431
5432 uint32_t num_children = 0;
5433 clang::QualType qual_type(GetQualType(type));
5434 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5435 switch (type_class) {
5436 case clang::Type::Builtin:
5437 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5438 case clang::BuiltinType::ObjCId: // child is Class
5439 case clang::BuiltinType::ObjCClass: // child is Class
5440 num_children = 1;
5441 break;
5442
5443 default:
5444 break;
5445 }
5446 break;
5447
5448 case clang::Type::Complex:
5449 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005450 case clang::Type::Record:
5451 if (GetCompleteQualType(getASTContext(), qual_type)) {
5452 const clang::RecordType *record_type =
5453 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5454 const clang::RecordDecl *record_decl = record_type->getDecl();
5455 assert(record_decl);
5456 const clang::CXXRecordDecl *cxx_record_decl =
5457 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5458 if (cxx_record_decl) {
5459 if (omit_empty_base_classes) {
Adrian Prantl05097242018-04-30 16:49:04 +00005460 // Check each base classes to see if it or any of its base classes
5461 // contain any fields. This can help limit the noise in variable
5462 // views by not having to show base classes that contain no members.
Kate Stoneb9c1b512016-09-06 20:57:50 +00005463 clang::CXXRecordDecl::base_class_const_iterator base_class,
5464 base_class_end;
5465 for (base_class = cxx_record_decl->bases_begin(),
5466 base_class_end = cxx_record_decl->bases_end();
5467 base_class != base_class_end; ++base_class) {
5468 const clang::CXXRecordDecl *base_class_decl =
5469 llvm::cast<clang::CXXRecordDecl>(
5470 base_class->getType()
5471 ->getAs<clang::RecordType>()
5472 ->getDecl());
5473
5474 // Skip empty base classes
5475 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5476 continue;
5477
5478 num_children++;
5479 }
5480 } else {
5481 // Include all base classes
5482 num_children += cxx_record_decl->getNumBases();
5483 }
5484 }
5485 clang::RecordDecl::field_iterator field, field_end;
5486 for (field = record_decl->field_begin(),
5487 field_end = record_decl->field_end();
5488 field != field_end; ++field)
5489 ++num_children;
5490 }
5491 break;
5492
5493 case clang::Type::ObjCObject:
5494 case clang::Type::ObjCInterface:
5495 if (GetCompleteQualType(getASTContext(), qual_type)) {
5496 const clang::ObjCObjectType *objc_class_type =
5497 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5498 assert(objc_class_type);
5499 if (objc_class_type) {
5500 clang::ObjCInterfaceDecl *class_interface_decl =
5501 objc_class_type->getInterface();
5502
5503 if (class_interface_decl) {
5504
5505 clang::ObjCInterfaceDecl *superclass_interface_decl =
5506 class_interface_decl->getSuperClass();
5507 if (superclass_interface_decl) {
5508 if (omit_empty_base_classes) {
5509 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5510 ++num_children;
5511 } else
5512 ++num_children;
5513 }
5514
5515 num_children += class_interface_decl->ivar_size();
5516 }
5517 }
5518 }
5519 break;
5520
5521 case clang::Type::ObjCObjectPointer: {
5522 const clang::ObjCObjectPointerType *pointer_type =
5523 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5524 clang::QualType pointee_type = pointer_type->getPointeeType();
5525 uint32_t num_pointee_children =
5526 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005527 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005528 // If this type points to a simple type, then it has 1 child
5529 if (num_pointee_children == 0)
5530 num_children = 1;
5531 else
5532 num_children = num_pointee_children;
5533 } break;
5534
5535 case clang::Type::Vector:
5536 case clang::Type::ExtVector:
5537 num_children =
5538 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5539 break;
5540
5541 case clang::Type::ConstantArray:
5542 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5543 ->getSize()
5544 .getLimitedValue();
5545 break;
Adrian Prantleca07c52018-11-05 20:49:07 +00005546 case clang::Type::IncompleteArray:
5547 if (auto array_info =
5548 GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx))
5549 // Only 1-dimensional arrays are supported.
5550 num_children = array_info->element_orders.size()
5551 ? array_info->element_orders.back()
5552 : 0;
5553 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005554
5555 case clang::Type::Pointer: {
5556 const clang::PointerType *pointer_type =
5557 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5558 clang::QualType pointee_type(pointer_type->getPointeeType());
5559 uint32_t num_pointee_children =
5560 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005561 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005562 if (num_pointee_children == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00005563 // We have a pointer to a pointee type that claims it has no children. We
5564 // will want to look at
Kate Stoneb9c1b512016-09-06 20:57:50 +00005565 num_children = GetNumPointeeChildren(pointee_type);
5566 } else
5567 num_children = num_pointee_children;
5568 } break;
5569
5570 case clang::Type::LValueReference:
5571 case clang::Type::RValueReference: {
5572 const clang::ReferenceType *reference_type =
5573 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5574 clang::QualType pointee_type = reference_type->getPointeeType();
5575 uint32_t num_pointee_children =
5576 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005577 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005578 // If this type points to a simple type, then it has 1 child
5579 if (num_pointee_children == 0)
5580 num_children = 1;
5581 else
5582 num_children = num_pointee_children;
5583 } break;
5584
5585 case clang::Type::Typedef:
5586 num_children =
5587 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5588 ->getDecl()
5589 ->getUnderlyingType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005590 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005591 break;
5592
5593 case clang::Type::Auto:
5594 num_children =
5595 CompilerType(getASTContext(),
5596 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005597 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005598 break;
5599
5600 case clang::Type::Elaborated:
5601 num_children =
5602 CompilerType(
5603 getASTContext(),
5604 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005605 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005606 break;
5607
5608 case clang::Type::Paren:
5609 num_children =
5610 CompilerType(getASTContext(),
5611 llvm::cast<clang::ParenType>(qual_type)->desugar())
Adrian Prantleca07c52018-11-05 20:49:07 +00005612 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005613 break;
5614 default:
5615 break;
5616 }
5617 return num_children;
5618}
5619
5620CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5621 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005622}
5623
Greg Claytond8d4a572015-08-11 21:38:15 +00005624lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005625ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5626 if (type) {
5627 clang::QualType qual_type(GetQualType(type));
5628 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5629 if (type_class == clang::Type::Builtin) {
5630 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5631 case clang::BuiltinType::Void:
5632 return eBasicTypeVoid;
5633 case clang::BuiltinType::Bool:
5634 return eBasicTypeBool;
5635 case clang::BuiltinType::Char_S:
5636 return eBasicTypeSignedChar;
5637 case clang::BuiltinType::Char_U:
5638 return eBasicTypeUnsignedChar;
5639 case clang::BuiltinType::Char16:
5640 return eBasicTypeChar16;
5641 case clang::BuiltinType::Char32:
5642 return eBasicTypeChar32;
5643 case clang::BuiltinType::UChar:
5644 return eBasicTypeUnsignedChar;
5645 case clang::BuiltinType::SChar:
5646 return eBasicTypeSignedChar;
5647 case clang::BuiltinType::WChar_S:
5648 return eBasicTypeSignedWChar;
5649 case clang::BuiltinType::WChar_U:
5650 return eBasicTypeUnsignedWChar;
5651 case clang::BuiltinType::Short:
5652 return eBasicTypeShort;
5653 case clang::BuiltinType::UShort:
5654 return eBasicTypeUnsignedShort;
5655 case clang::BuiltinType::Int:
5656 return eBasicTypeInt;
5657 case clang::BuiltinType::UInt:
5658 return eBasicTypeUnsignedInt;
5659 case clang::BuiltinType::Long:
5660 return eBasicTypeLong;
5661 case clang::BuiltinType::ULong:
5662 return eBasicTypeUnsignedLong;
5663 case clang::BuiltinType::LongLong:
5664 return eBasicTypeLongLong;
5665 case clang::BuiltinType::ULongLong:
5666 return eBasicTypeUnsignedLongLong;
5667 case clang::BuiltinType::Int128:
5668 return eBasicTypeInt128;
5669 case clang::BuiltinType::UInt128:
5670 return eBasicTypeUnsignedInt128;
5671
5672 case clang::BuiltinType::Half:
5673 return eBasicTypeHalf;
5674 case clang::BuiltinType::Float:
5675 return eBasicTypeFloat;
5676 case clang::BuiltinType::Double:
5677 return eBasicTypeDouble;
5678 case clang::BuiltinType::LongDouble:
5679 return eBasicTypeLongDouble;
5680
5681 case clang::BuiltinType::NullPtr:
5682 return eBasicTypeNullPtr;
5683 case clang::BuiltinType::ObjCId:
5684 return eBasicTypeObjCID;
5685 case clang::BuiltinType::ObjCClass:
5686 return eBasicTypeObjCClass;
5687 case clang::BuiltinType::ObjCSel:
5688 return eBasicTypeObjCSel;
5689 default:
5690 return eBasicTypeOther;
5691 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005692 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005693 }
5694 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005695}
5696
Kate Stoneb9c1b512016-09-06 20:57:50 +00005697void ClangASTContext::ForEachEnumerator(
5698 lldb::opaque_compiler_type_t type,
5699 std::function<bool(const CompilerType &integer_type,
5700 const ConstString &name,
5701 const llvm::APSInt &value)> const &callback) {
5702 const clang::EnumType *enum_type =
5703 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5704 if (enum_type) {
5705 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5706 if (enum_decl) {
5707 CompilerType integer_type(this,
5708 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005709
Kate Stoneb9c1b512016-09-06 20:57:50 +00005710 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5711 for (enum_pos = enum_decl->enumerator_begin(),
5712 enum_end_pos = enum_decl->enumerator_end();
5713 enum_pos != enum_end_pos; ++enum_pos) {
5714 ConstString name(enum_pos->getNameAsString().c_str());
5715 if (!callback(integer_type, name, enum_pos->getInitVal()))
5716 break;
5717 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005718 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005719 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005720}
5721
Greg Claytond8d4a572015-08-11 21:38:15 +00005722#pragma mark Aggregate Types
5723
Kate Stoneb9c1b512016-09-06 20:57:50 +00005724uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5725 if (!type)
5726 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005727
Kate Stoneb9c1b512016-09-06 20:57:50 +00005728 uint32_t count = 0;
5729 clang::QualType qual_type(GetCanonicalQualType(type));
5730 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5731 switch (type_class) {
5732 case clang::Type::Record:
5733 if (GetCompleteType(type)) {
5734 const clang::RecordType *record_type =
5735 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5736 if (record_type) {
5737 clang::RecordDecl *record_decl = record_type->getDecl();
5738 if (record_decl) {
5739 uint32_t field_idx = 0;
5740 clang::RecordDecl::field_iterator field, field_end;
5741 for (field = record_decl->field_begin(),
5742 field_end = record_decl->field_end();
5743 field != field_end; ++field)
5744 ++field_idx;
5745 count = field_idx;
5746 }
5747 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005748 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005749 break;
5750
5751 case clang::Type::Typedef:
5752 count =
5753 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5754 ->getDecl()
5755 ->getUnderlyingType())
5756 .GetNumFields();
5757 break;
5758
5759 case clang::Type::Auto:
5760 count =
5761 CompilerType(getASTContext(),
5762 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5763 .GetNumFields();
5764 break;
5765
5766 case clang::Type::Elaborated:
5767 count = CompilerType(
5768 getASTContext(),
5769 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5770 .GetNumFields();
5771 break;
5772
5773 case clang::Type::Paren:
5774 count = CompilerType(getASTContext(),
5775 llvm::cast<clang::ParenType>(qual_type)->desugar())
5776 .GetNumFields();
5777 break;
5778
Sean Callananf9c622a2016-09-30 18:44:43 +00005779 case clang::Type::ObjCObjectPointer: {
5780 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005781 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005782 const clang::ObjCInterfaceType *objc_interface_type =
5783 objc_class_type->getInterfaceType();
5784 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005785 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5786 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005787 clang::ObjCInterfaceDecl *class_interface_decl =
5788 objc_interface_type->getDecl();
5789 if (class_interface_decl) {
5790 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005791 }
5792 }
5793 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005794 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005795
5796 case clang::Type::ObjCObject:
5797 case clang::Type::ObjCInterface:
5798 if (GetCompleteType(type)) {
5799 const clang::ObjCObjectType *objc_class_type =
5800 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5801 if (objc_class_type) {
5802 clang::ObjCInterfaceDecl *class_interface_decl =
5803 objc_class_type->getInterface();
5804
5805 if (class_interface_decl)
5806 count = class_interface_decl->ivar_size();
5807 }
5808 }
5809 break;
5810
5811 default:
5812 break;
5813 }
5814 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005815}
5816
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005817static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005818GetObjCFieldAtIndex(clang::ASTContext *ast,
5819 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5820 std::string &name, uint64_t *bit_offset_ptr,
5821 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5822 if (class_interface_decl) {
5823 if (idx < (class_interface_decl->ivar_size())) {
5824 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5825 ivar_end = class_interface_decl->ivar_end();
5826 uint32_t ivar_idx = 0;
5827
5828 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5829 ++ivar_pos, ++ivar_idx) {
5830 if (ivar_idx == idx) {
5831 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5832
5833 clang::QualType ivar_qual_type(ivar_decl->getType());
5834
5835 name.assign(ivar_decl->getNameAsString());
5836
5837 if (bit_offset_ptr) {
5838 const clang::ASTRecordLayout &interface_layout =
5839 ast->getASTObjCInterfaceLayout(class_interface_decl);
5840 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5841 }
5842
5843 const bool is_bitfield = ivar_pos->isBitField();
5844
5845 if (bitfield_bit_size_ptr) {
5846 *bitfield_bit_size_ptr = 0;
5847
5848 if (is_bitfield && ast) {
5849 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5850 llvm::APSInt bitfield_apsint;
5851 if (bitfield_bit_size_expr &&
5852 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5853 *ast)) {
5854 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5855 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005856 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005857 }
5858 if (is_bitfield_ptr)
5859 *is_bitfield_ptr = is_bitfield;
5860
5861 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005862 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005863 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005864 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005865 }
5866 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005867}
5868
Kate Stoneb9c1b512016-09-06 20:57:50 +00005869CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5870 size_t idx, std::string &name,
5871 uint64_t *bit_offset_ptr,
5872 uint32_t *bitfield_bit_size_ptr,
5873 bool *is_bitfield_ptr) {
5874 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005875 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005876
5877 clang::QualType qual_type(GetCanonicalQualType(type));
5878 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5879 switch (type_class) {
5880 case clang::Type::Record:
5881 if (GetCompleteType(type)) {
5882 const clang::RecordType *record_type =
5883 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5884 const clang::RecordDecl *record_decl = record_type->getDecl();
5885 uint32_t field_idx = 0;
5886 clang::RecordDecl::field_iterator field, field_end;
5887 for (field = record_decl->field_begin(),
5888 field_end = record_decl->field_end();
5889 field != field_end; ++field, ++field_idx) {
5890 if (idx == field_idx) {
5891 // Print the member type if requested
5892 // Print the member name and equal sign
5893 name.assign(field->getNameAsString());
5894
5895 // Figure out the type byte size (field_type_info.first) and
5896 // alignment (field_type_info.second) from the AST context.
5897 if (bit_offset_ptr) {
5898 const clang::ASTRecordLayout &record_layout =
5899 getASTContext()->getASTRecordLayout(record_decl);
5900 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5901 }
5902
5903 const bool is_bitfield = field->isBitField();
5904
5905 if (bitfield_bit_size_ptr) {
5906 *bitfield_bit_size_ptr = 0;
5907
5908 if (is_bitfield) {
5909 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5910 llvm::APSInt bitfield_apsint;
5911 if (bitfield_bit_size_expr &&
5912 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5913 *getASTContext())) {
5914 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5915 }
5916 }
5917 }
5918 if (is_bitfield_ptr)
5919 *is_bitfield_ptr = is_bitfield;
5920
5921 return CompilerType(getASTContext(), field->getType());
5922 }
5923 }
5924 }
5925 break;
5926
Sean Callananf9c622a2016-09-30 18:44:43 +00005927 case clang::Type::ObjCObjectPointer: {
5928 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005929 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005930 const clang::ObjCInterfaceType *objc_interface_type =
5931 objc_class_type->getInterfaceType();
5932 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005933 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5934 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005935 clang::ObjCInterfaceDecl *class_interface_decl =
5936 objc_interface_type->getDecl();
5937 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005938 return CompilerType(
5939 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5940 idx, name, bit_offset_ptr,
5941 bitfield_bit_size_ptr, is_bitfield_ptr));
5942 }
5943 }
5944 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005945 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005946
5947 case clang::Type::ObjCObject:
5948 case clang::Type::ObjCInterface:
5949 if (GetCompleteType(type)) {
5950 const clang::ObjCObjectType *objc_class_type =
5951 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5952 assert(objc_class_type);
5953 if (objc_class_type) {
5954 clang::ObjCInterfaceDecl *class_interface_decl =
5955 objc_class_type->getInterface();
5956 return CompilerType(
5957 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5958 idx, name, bit_offset_ptr,
5959 bitfield_bit_size_ptr, is_bitfield_ptr));
5960 }
5961 }
5962 break;
5963
5964 case clang::Type::Typedef:
5965 return CompilerType(getASTContext(),
5966 llvm::cast<clang::TypedefType>(qual_type)
5967 ->getDecl()
5968 ->getUnderlyingType())
5969 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5970 is_bitfield_ptr);
5971
5972 case clang::Type::Auto:
5973 return CompilerType(
5974 getASTContext(),
5975 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5976 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5977 is_bitfield_ptr);
5978
5979 case clang::Type::Elaborated:
5980 return CompilerType(
5981 getASTContext(),
5982 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5983 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5984 is_bitfield_ptr);
5985
5986 case clang::Type::Paren:
5987 return CompilerType(getASTContext(),
5988 llvm::cast<clang::ParenType>(qual_type)->desugar())
5989 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5990 is_bitfield_ptr);
5991
5992 default:
5993 break;
5994 }
5995 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005996}
5997
Greg Clayton99558cc42015-08-24 23:46:31 +00005998uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005999ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
6000 uint32_t count = 0;
6001 clang::QualType qual_type(GetCanonicalQualType(type));
6002 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6003 switch (type_class) {
6004 case clang::Type::Record:
6005 if (GetCompleteType(type)) {
6006 const clang::CXXRecordDecl *cxx_record_decl =
6007 qual_type->getAsCXXRecordDecl();
6008 if (cxx_record_decl)
6009 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006010 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006011 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006012
Kate Stoneb9c1b512016-09-06 20:57:50 +00006013 case clang::Type::ObjCObjectPointer:
6014 count = GetPointeeType(type).GetNumDirectBaseClasses();
6015 break;
6016
6017 case clang::Type::ObjCObject:
6018 if (GetCompleteType(type)) {
6019 const clang::ObjCObjectType *objc_class_type =
6020 qual_type->getAsObjCQualifiedInterfaceType();
6021 if (objc_class_type) {
6022 clang::ObjCInterfaceDecl *class_interface_decl =
6023 objc_class_type->getInterface();
6024
6025 if (class_interface_decl && class_interface_decl->getSuperClass())
6026 count = 1;
6027 }
6028 }
6029 break;
6030 case clang::Type::ObjCInterface:
6031 if (GetCompleteType(type)) {
6032 const clang::ObjCInterfaceType *objc_interface_type =
6033 qual_type->getAs<clang::ObjCInterfaceType>();
6034 if (objc_interface_type) {
6035 clang::ObjCInterfaceDecl *class_interface_decl =
6036 objc_interface_type->getInterface();
6037
6038 if (class_interface_decl && class_interface_decl->getSuperClass())
6039 count = 1;
6040 }
6041 }
6042 break;
6043
6044 case clang::Type::Typedef:
6045 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6046 ->getDecl()
6047 ->getUnderlyingType()
6048 .getAsOpaquePtr());
6049 break;
6050
6051 case clang::Type::Auto:
6052 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6053 ->getDeducedType()
6054 .getAsOpaquePtr());
6055 break;
6056
6057 case clang::Type::Elaborated:
6058 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6059 ->getNamedType()
6060 .getAsOpaquePtr());
6061 break;
6062
6063 case clang::Type::Paren:
6064 return GetNumDirectBaseClasses(
6065 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6066
6067 default:
6068 break;
6069 }
6070 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006071}
6072
6073uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006074ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
6075 uint32_t count = 0;
6076 clang::QualType qual_type(GetCanonicalQualType(type));
6077 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6078 switch (type_class) {
6079 case clang::Type::Record:
6080 if (GetCompleteType(type)) {
6081 const clang::CXXRecordDecl *cxx_record_decl =
6082 qual_type->getAsCXXRecordDecl();
6083 if (cxx_record_decl)
6084 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006085 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006086 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006087
Kate Stoneb9c1b512016-09-06 20:57:50 +00006088 case clang::Type::Typedef:
6089 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6090 ->getDecl()
6091 ->getUnderlyingType()
6092 .getAsOpaquePtr());
6093 break;
6094
6095 case clang::Type::Auto:
6096 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6097 ->getDeducedType()
6098 .getAsOpaquePtr());
6099 break;
6100
6101 case clang::Type::Elaborated:
6102 count =
6103 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6104 ->getNamedType()
6105 .getAsOpaquePtr());
6106 break;
6107
6108 case clang::Type::Paren:
6109 count = GetNumVirtualBaseClasses(
6110 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6111 break;
6112
6113 default:
6114 break;
6115 }
6116 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006117}
6118
Kate Stoneb9c1b512016-09-06 20:57:50 +00006119CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6120 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6121 clang::QualType qual_type(GetCanonicalQualType(type));
6122 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6123 switch (type_class) {
6124 case clang::Type::Record:
6125 if (GetCompleteType(type)) {
6126 const clang::CXXRecordDecl *cxx_record_decl =
6127 qual_type->getAsCXXRecordDecl();
6128 if (cxx_record_decl) {
6129 uint32_t curr_idx = 0;
6130 clang::CXXRecordDecl::base_class_const_iterator base_class,
6131 base_class_end;
6132 for (base_class = cxx_record_decl->bases_begin(),
6133 base_class_end = cxx_record_decl->bases_end();
6134 base_class != base_class_end; ++base_class, ++curr_idx) {
6135 if (curr_idx == idx) {
6136 if (bit_offset_ptr) {
6137 const clang::ASTRecordLayout &record_layout =
6138 getASTContext()->getASTRecordLayout(cxx_record_decl);
6139 const clang::CXXRecordDecl *base_class_decl =
6140 llvm::cast<clang::CXXRecordDecl>(
6141 base_class->getType()
6142 ->getAs<clang::RecordType>()
6143 ->getDecl());
6144 if (base_class->isVirtual())
6145 *bit_offset_ptr =
6146 record_layout.getVBaseClassOffset(base_class_decl)
6147 .getQuantity() *
6148 8;
6149 else
6150 *bit_offset_ptr =
6151 record_layout.getBaseClassOffset(base_class_decl)
6152 .getQuantity() *
6153 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006154 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006155 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6156 }
6157 }
6158 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006159 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006160 break;
6161
6162 case clang::Type::ObjCObjectPointer:
6163 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6164
6165 case clang::Type::ObjCObject:
6166 if (idx == 0 && GetCompleteType(type)) {
6167 const clang::ObjCObjectType *objc_class_type =
6168 qual_type->getAsObjCQualifiedInterfaceType();
6169 if (objc_class_type) {
6170 clang::ObjCInterfaceDecl *class_interface_decl =
6171 objc_class_type->getInterface();
6172
6173 if (class_interface_decl) {
6174 clang::ObjCInterfaceDecl *superclass_interface_decl =
6175 class_interface_decl->getSuperClass();
6176 if (superclass_interface_decl) {
6177 if (bit_offset_ptr)
6178 *bit_offset_ptr = 0;
6179 return CompilerType(getASTContext(),
6180 getASTContext()->getObjCInterfaceType(
6181 superclass_interface_decl));
6182 }
6183 }
6184 }
6185 }
6186 break;
6187 case clang::Type::ObjCInterface:
6188 if (idx == 0 && GetCompleteType(type)) {
6189 const clang::ObjCObjectType *objc_interface_type =
6190 qual_type->getAs<clang::ObjCInterfaceType>();
6191 if (objc_interface_type) {
6192 clang::ObjCInterfaceDecl *class_interface_decl =
6193 objc_interface_type->getInterface();
6194
6195 if (class_interface_decl) {
6196 clang::ObjCInterfaceDecl *superclass_interface_decl =
6197 class_interface_decl->getSuperClass();
6198 if (superclass_interface_decl) {
6199 if (bit_offset_ptr)
6200 *bit_offset_ptr = 0;
6201 return CompilerType(getASTContext(),
6202 getASTContext()->getObjCInterfaceType(
6203 superclass_interface_decl));
6204 }
6205 }
6206 }
6207 }
6208 break;
6209
6210 case clang::Type::Typedef:
6211 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6212 ->getDecl()
6213 ->getUnderlyingType()
6214 .getAsOpaquePtr(),
6215 idx, bit_offset_ptr);
6216
6217 case clang::Type::Auto:
6218 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6219 ->getDeducedType()
6220 .getAsOpaquePtr(),
6221 idx, bit_offset_ptr);
6222
6223 case clang::Type::Elaborated:
6224 return GetDirectBaseClassAtIndex(
6225 llvm::cast<clang::ElaboratedType>(qual_type)
6226 ->getNamedType()
6227 .getAsOpaquePtr(),
6228 idx, bit_offset_ptr);
6229
6230 case clang::Type::Paren:
6231 return GetDirectBaseClassAtIndex(
6232 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6233 idx, bit_offset_ptr);
6234
6235 default:
6236 break;
6237 }
6238 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006239}
6240
Kate Stoneb9c1b512016-09-06 20:57:50 +00006241CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6242 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6243 clang::QualType qual_type(GetCanonicalQualType(type));
6244 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6245 switch (type_class) {
6246 case clang::Type::Record:
6247 if (GetCompleteType(type)) {
6248 const clang::CXXRecordDecl *cxx_record_decl =
6249 qual_type->getAsCXXRecordDecl();
6250 if (cxx_record_decl) {
6251 uint32_t curr_idx = 0;
6252 clang::CXXRecordDecl::base_class_const_iterator base_class,
6253 base_class_end;
6254 for (base_class = cxx_record_decl->vbases_begin(),
6255 base_class_end = cxx_record_decl->vbases_end();
6256 base_class != base_class_end; ++base_class, ++curr_idx) {
6257 if (curr_idx == idx) {
6258 if (bit_offset_ptr) {
6259 const clang::ASTRecordLayout &record_layout =
6260 getASTContext()->getASTRecordLayout(cxx_record_decl);
6261 const clang::CXXRecordDecl *base_class_decl =
6262 llvm::cast<clang::CXXRecordDecl>(
6263 base_class->getType()
6264 ->getAs<clang::RecordType>()
6265 ->getDecl());
6266 *bit_offset_ptr =
6267 record_layout.getVBaseClassOffset(base_class_decl)
6268 .getQuantity() *
6269 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006270 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006271 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6272 }
6273 }
6274 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006275 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006276 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006277
Kate Stoneb9c1b512016-09-06 20:57:50 +00006278 case clang::Type::Typedef:
6279 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6280 ->getDecl()
6281 ->getUnderlyingType()
6282 .getAsOpaquePtr(),
6283 idx, bit_offset_ptr);
6284
6285 case clang::Type::Auto:
6286 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6287 ->getDeducedType()
6288 .getAsOpaquePtr(),
6289 idx, bit_offset_ptr);
6290
6291 case clang::Type::Elaborated:
6292 return GetVirtualBaseClassAtIndex(
6293 llvm::cast<clang::ElaboratedType>(qual_type)
6294 ->getNamedType()
6295 .getAsOpaquePtr(),
6296 idx, bit_offset_ptr);
6297
6298 case clang::Type::Paren:
6299 return GetVirtualBaseClassAtIndex(
6300 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6301 idx, bit_offset_ptr);
6302
6303 default:
6304 break;
6305 }
6306 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006307}
6308
Greg Claytond8d4a572015-08-11 21:38:15 +00006309// If a pointer to a pointee type (the clang_type arg) says that it has no
6310// children, then we either need to trust it, or override it and return a
6311// different result. For example, an "int *" has one child that is an integer,
6312// but a function pointer doesn't have any children. Likewise if a Record type
6313// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006314uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6315 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006316 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006317
6318 clang::QualType qual_type(type.getCanonicalType());
6319 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6320 switch (type_class) {
6321 case clang::Type::Builtin:
6322 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6323 case clang::BuiltinType::UnknownAny:
6324 case clang::BuiltinType::Void:
6325 case clang::BuiltinType::NullPtr:
6326 case clang::BuiltinType::OCLEvent:
6327 case clang::BuiltinType::OCLImage1dRO:
6328 case clang::BuiltinType::OCLImage1dWO:
6329 case clang::BuiltinType::OCLImage1dRW:
6330 case clang::BuiltinType::OCLImage1dArrayRO:
6331 case clang::BuiltinType::OCLImage1dArrayWO:
6332 case clang::BuiltinType::OCLImage1dArrayRW:
6333 case clang::BuiltinType::OCLImage1dBufferRO:
6334 case clang::BuiltinType::OCLImage1dBufferWO:
6335 case clang::BuiltinType::OCLImage1dBufferRW:
6336 case clang::BuiltinType::OCLImage2dRO:
6337 case clang::BuiltinType::OCLImage2dWO:
6338 case clang::BuiltinType::OCLImage2dRW:
6339 case clang::BuiltinType::OCLImage2dArrayRO:
6340 case clang::BuiltinType::OCLImage2dArrayWO:
6341 case clang::BuiltinType::OCLImage2dArrayRW:
6342 case clang::BuiltinType::OCLImage3dRO:
6343 case clang::BuiltinType::OCLImage3dWO:
6344 case clang::BuiltinType::OCLImage3dRW:
6345 case clang::BuiltinType::OCLSampler:
6346 return 0;
6347 case clang::BuiltinType::Bool:
6348 case clang::BuiltinType::Char_U:
6349 case clang::BuiltinType::UChar:
6350 case clang::BuiltinType::WChar_U:
6351 case clang::BuiltinType::Char16:
6352 case clang::BuiltinType::Char32:
6353 case clang::BuiltinType::UShort:
6354 case clang::BuiltinType::UInt:
6355 case clang::BuiltinType::ULong:
6356 case clang::BuiltinType::ULongLong:
6357 case clang::BuiltinType::UInt128:
6358 case clang::BuiltinType::Char_S:
6359 case clang::BuiltinType::SChar:
6360 case clang::BuiltinType::WChar_S:
6361 case clang::BuiltinType::Short:
6362 case clang::BuiltinType::Int:
6363 case clang::BuiltinType::Long:
6364 case clang::BuiltinType::LongLong:
6365 case clang::BuiltinType::Int128:
6366 case clang::BuiltinType::Float:
6367 case clang::BuiltinType::Double:
6368 case clang::BuiltinType::LongDouble:
6369 case clang::BuiltinType::Dependent:
6370 case clang::BuiltinType::Overload:
6371 case clang::BuiltinType::ObjCId:
6372 case clang::BuiltinType::ObjCClass:
6373 case clang::BuiltinType::ObjCSel:
6374 case clang::BuiltinType::BoundMember:
6375 case clang::BuiltinType::Half:
6376 case clang::BuiltinType::ARCUnbridgedCast:
6377 case clang::BuiltinType::PseudoObject:
6378 case clang::BuiltinType::BuiltinFn:
6379 case clang::BuiltinType::OMPArraySection:
6380 return 1;
6381 default:
6382 return 0;
6383 }
6384 break;
6385
6386 case clang::Type::Complex:
6387 return 1;
6388 case clang::Type::Pointer:
6389 return 1;
6390 case clang::Type::BlockPointer:
6391 return 0; // If block pointers don't have debug info, then no children for
6392 // them
6393 case clang::Type::LValueReference:
6394 return 1;
6395 case clang::Type::RValueReference:
6396 return 1;
6397 case clang::Type::MemberPointer:
6398 return 0;
6399 case clang::Type::ConstantArray:
6400 return 0;
6401 case clang::Type::IncompleteArray:
6402 return 0;
6403 case clang::Type::VariableArray:
6404 return 0;
6405 case clang::Type::DependentSizedArray:
6406 return 0;
6407 case clang::Type::DependentSizedExtVector:
6408 return 0;
6409 case clang::Type::Vector:
6410 return 0;
6411 case clang::Type::ExtVector:
6412 return 0;
6413 case clang::Type::FunctionProto:
6414 return 0; // When we function pointers, they have no children...
6415 case clang::Type::FunctionNoProto:
6416 return 0; // When we function pointers, they have no children...
6417 case clang::Type::UnresolvedUsing:
6418 return 0;
6419 case clang::Type::Paren:
6420 return GetNumPointeeChildren(
6421 llvm::cast<clang::ParenType>(qual_type)->desugar());
6422 case clang::Type::Typedef:
6423 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6424 ->getDecl()
6425 ->getUnderlyingType());
6426 case clang::Type::Auto:
6427 return GetNumPointeeChildren(
6428 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6429 case clang::Type::Elaborated:
6430 return GetNumPointeeChildren(
6431 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6432 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006433 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6434 ->getUnderlyingExpr()
6435 ->getType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006436 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006437 return GetNumPointeeChildren(
6438 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006439 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006440 return GetNumPointeeChildren(
6441 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006442 case clang::Type::Record:
6443 return 0;
6444 case clang::Type::Enum:
6445 return 1;
6446 case clang::Type::TemplateTypeParm:
6447 return 1;
6448 case clang::Type::SubstTemplateTypeParm:
6449 return 1;
6450 case clang::Type::TemplateSpecialization:
6451 return 1;
6452 case clang::Type::InjectedClassName:
6453 return 0;
6454 case clang::Type::DependentName:
6455 return 1;
6456 case clang::Type::DependentTemplateSpecialization:
6457 return 1;
6458 case clang::Type::ObjCObject:
6459 return 0;
6460 case clang::Type::ObjCInterface:
6461 return 0;
6462 case clang::Type::ObjCObjectPointer:
6463 return 1;
6464 default:
6465 break;
6466 }
6467 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006468}
6469
Kate Stoneb9c1b512016-09-06 20:57:50 +00006470CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6471 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6472 bool transparent_pointers, bool omit_empty_base_classes,
6473 bool ignore_array_bounds, std::string &child_name,
6474 uint32_t &child_byte_size, int32_t &child_byte_offset,
6475 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6476 bool &child_is_base_class, bool &child_is_deref_of_parent,
6477 ValueObject *valobj, uint64_t &language_flags) {
6478 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006479 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006480
Kate Stoneb9c1b512016-09-06 20:57:50 +00006481 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6482 const clang::Type::TypeClass parent_type_class =
6483 parent_qual_type->getTypeClass();
6484 child_bitfield_bit_size = 0;
6485 child_bitfield_bit_offset = 0;
6486 child_is_base_class = false;
6487 language_flags = 0;
6488
Adrian Prantleca07c52018-11-05 20:49:07 +00006489 const bool idx_is_valid =
6490 idx < GetNumChildren(type, omit_empty_base_classes, exe_ctx);
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006491 int32_t bit_offset;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006492 switch (parent_type_class) {
6493 case clang::Type::Builtin:
6494 if (idx_is_valid) {
6495 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6496 case clang::BuiltinType::ObjCId:
6497 case clang::BuiltinType::ObjCClass:
6498 child_name = "isa";
6499 child_byte_size =
6500 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6501 CHAR_BIT;
6502 return CompilerType(getASTContext(),
6503 getASTContext()->ObjCBuiltinClassTy);
6504
6505 default:
6506 break;
6507 }
6508 }
6509 break;
6510
6511 case clang::Type::Record:
6512 if (idx_is_valid && GetCompleteType(type)) {
6513 const clang::RecordType *record_type =
6514 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6515 const clang::RecordDecl *record_decl = record_type->getDecl();
6516 assert(record_decl);
6517 const clang::ASTRecordLayout &record_layout =
6518 getASTContext()->getASTRecordLayout(record_decl);
6519 uint32_t child_idx = 0;
6520
6521 const clang::CXXRecordDecl *cxx_record_decl =
6522 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6523 if (cxx_record_decl) {
6524 // We might have base classes to print out first
6525 clang::CXXRecordDecl::base_class_const_iterator base_class,
6526 base_class_end;
6527 for (base_class = cxx_record_decl->bases_begin(),
6528 base_class_end = cxx_record_decl->bases_end();
6529 base_class != base_class_end; ++base_class) {
6530 const clang::CXXRecordDecl *base_class_decl = nullptr;
6531
6532 // Skip empty base classes
6533 if (omit_empty_base_classes) {
6534 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6535 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6536 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
6537 continue;
6538 }
6539
6540 if (idx == child_idx) {
6541 if (base_class_decl == nullptr)
6542 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6543 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6544
6545 if (base_class->isVirtual()) {
6546 bool handled = false;
6547 if (valobj) {
Zachary Turner97206d52017-05-12 04:51:55 +00006548 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006549 AddressType addr_type = eAddressTypeInvalid;
6550 lldb::addr_t vtable_ptr_addr =
6551 valobj->GetCPPVTableAddress(addr_type);
6552
6553 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS &&
6554 addr_type == eAddressTypeLoad) {
6555
6556 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
6557 Process *process = exe_ctx.GetProcessPtr();
6558 if (process) {
6559 clang::VTableContextBase *vtable_ctx =
6560 getASTContext()->getVTableContext();
6561 if (vtable_ctx) {
6562 if (vtable_ctx->isMicrosoft()) {
6563 clang::MicrosoftVTableContext *msoft_vtable_ctx =
6564 static_cast<clang::MicrosoftVTableContext *>(
6565 vtable_ctx);
6566
6567 if (vtable_ptr_addr) {
6568 const lldb::addr_t vbtable_ptr_addr =
6569 vtable_ptr_addr +
6570 record_layout.getVBPtrOffset().getQuantity();
6571
6572 const lldb::addr_t vbtable_ptr =
6573 process->ReadPointerFromMemory(vbtable_ptr_addr,
6574 err);
6575 if (vbtable_ptr != LLDB_INVALID_ADDRESS) {
6576 // Get the index into the virtual base table. The
6577 // index is the index in uint32_t from vbtable_ptr
6578 const unsigned vbtable_index =
6579 msoft_vtable_ctx->getVBTableIndex(
6580 cxx_record_decl, base_class_decl);
6581 const lldb::addr_t base_offset_addr =
6582 vbtable_ptr + vbtable_index * 4;
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006583 const int32_t base_offset =
6584 process->ReadSignedIntegerFromMemory(
Aleksandr Urakov53459482018-08-17 07:28:24 +00006585 base_offset_addr, 4, INT32_MAX, err);
6586 if (base_offset != INT32_MAX) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006587 handled = true;
6588 bit_offset = base_offset * 8;
6589 }
6590 }
6591 }
6592 } else {
6593 clang::ItaniumVTableContext *itanium_vtable_ctx =
6594 static_cast<clang::ItaniumVTableContext *>(
6595 vtable_ctx);
6596 if (vtable_ptr_addr) {
6597 const lldb::addr_t vtable_ptr =
6598 process->ReadPointerFromMemory(vtable_ptr_addr,
6599 err);
6600 if (vtable_ptr != LLDB_INVALID_ADDRESS) {
6601 clang::CharUnits base_offset_offset =
6602 itanium_vtable_ctx->getVirtualBaseOffsetOffset(
6603 cxx_record_decl, base_class_decl);
6604 const lldb::addr_t base_offset_addr =
6605 vtable_ptr + base_offset_offset.getQuantity();
6606 const uint32_t base_offset_size =
6607 process->GetAddressByteSize();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006608 const int64_t base_offset =
6609 process->ReadSignedIntegerFromMemory(
Kate Stoneb9c1b512016-09-06 20:57:50 +00006610 base_offset_addr, base_offset_size,
6611 UINT32_MAX, err);
6612 if (base_offset < UINT32_MAX) {
6613 handled = true;
6614 bit_offset = base_offset * 8;
6615 }
6616 }
6617 }
6618 }
6619 }
6620 }
6621 }
6622 }
6623 if (!handled)
6624 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6625 .getQuantity() *
6626 8;
6627 } else
6628 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6629 .getQuantity() *
6630 8;
6631
6632 // Base classes should be a multiple of 8 bits in size
6633 child_byte_offset = bit_offset / 8;
6634 CompilerType base_class_clang_type(getASTContext(),
6635 base_class->getType());
6636 child_name = base_class_clang_type.GetTypeName().AsCString("");
6637 uint64_t base_class_clang_type_bit_size =
6638 base_class_clang_type.GetBitSize(
6639 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6640
6641 // Base classes bit sizes should be a multiple of 8 bits in size
6642 assert(base_class_clang_type_bit_size % 8 == 0);
6643 child_byte_size = base_class_clang_type_bit_size / 8;
6644 child_is_base_class = true;
6645 return base_class_clang_type;
6646 }
6647 // We don't increment the child index in the for loop since we might
6648 // be skipping empty base classes
6649 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006650 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006651 }
6652 // Make sure index is in range...
6653 uint32_t field_idx = 0;
6654 clang::RecordDecl::field_iterator field, field_end;
6655 for (field = record_decl->field_begin(),
6656 field_end = record_decl->field_end();
6657 field != field_end; ++field, ++field_idx, ++child_idx) {
6658 if (idx == child_idx) {
6659 // Print the member type if requested
6660 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006661 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006662
6663 // Figure out the type byte size (field_type_info.first) and
6664 // alignment (field_type_info.second) from the AST context.
6665 CompilerType field_clang_type(getASTContext(), field->getType());
6666 assert(field_idx < record_layout.getFieldCount());
6667 child_byte_size = field_clang_type.GetByteSize(
6668 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6669 const uint32_t child_bit_size = child_byte_size * 8;
6670
6671 // Figure out the field offset within the current struct/union/class
6672 // type
6673 bit_offset = record_layout.getFieldOffset(field_idx);
6674 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6675 child_bitfield_bit_size)) {
6676 child_bitfield_bit_offset = bit_offset % child_bit_size;
6677 const uint32_t child_bit_offset =
6678 bit_offset - child_bitfield_bit_offset;
6679 child_byte_offset = child_bit_offset / 8;
6680 } else {
6681 child_byte_offset = bit_offset / 8;
6682 }
6683
6684 return field_clang_type;
6685 }
6686 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006687 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006688 break;
6689
6690 case clang::Type::ObjCObject:
6691 case clang::Type::ObjCInterface:
6692 if (idx_is_valid && GetCompleteType(type)) {
6693 const clang::ObjCObjectType *objc_class_type =
6694 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6695 assert(objc_class_type);
6696 if (objc_class_type) {
6697 uint32_t child_idx = 0;
6698 clang::ObjCInterfaceDecl *class_interface_decl =
6699 objc_class_type->getInterface();
6700
6701 if (class_interface_decl) {
6702
6703 const clang::ASTRecordLayout &interface_layout =
6704 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6705 clang::ObjCInterfaceDecl *superclass_interface_decl =
6706 class_interface_decl->getSuperClass();
6707 if (superclass_interface_decl) {
6708 if (omit_empty_base_classes) {
6709 CompilerType base_class_clang_type(
6710 getASTContext(), getASTContext()->getObjCInterfaceType(
6711 superclass_interface_decl));
Adrian Prantleca07c52018-11-05 20:49:07 +00006712 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes,
6713 exe_ctx) > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006714 if (idx == 0) {
6715 clang::QualType ivar_qual_type(
6716 getASTContext()->getObjCInterfaceType(
6717 superclass_interface_decl));
6718
6719 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006720 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006721
6722 clang::TypeInfo ivar_type_info =
6723 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6724
6725 child_byte_size = ivar_type_info.Width / 8;
6726 child_byte_offset = 0;
6727 child_is_base_class = true;
6728
6729 return CompilerType(getASTContext(), ivar_qual_type);
6730 }
6731
6732 ++child_idx;
6733 }
6734 } else
6735 ++child_idx;
6736 }
6737
6738 const uint32_t superclass_idx = child_idx;
6739
6740 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6741 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6742 ivar_end = class_interface_decl->ivar_end();
6743
6744 for (ivar_pos = class_interface_decl->ivar_begin();
6745 ivar_pos != ivar_end; ++ivar_pos) {
6746 if (child_idx == idx) {
6747 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6748
6749 clang::QualType ivar_qual_type(ivar_decl->getType());
6750
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006751 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006752
6753 clang::TypeInfo ivar_type_info =
6754 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6755
6756 child_byte_size = ivar_type_info.Width / 8;
6757
6758 // Figure out the field offset within the current
Adrian Prantl05097242018-04-30 16:49:04 +00006759 // struct/union/class type For ObjC objects, we can't trust the
6760 // bit offset we get from the Clang AST, since that doesn't
6761 // account for the space taken up by unbacked properties, or
6762 // from the changing size of base classes that are newer than
6763 // this class. So if we have a process around that we can ask
6764 // about this object, do so.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006765 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6766 Process *process = nullptr;
6767 if (exe_ctx)
6768 process = exe_ctx->GetProcessPtr();
6769 if (process) {
6770 ObjCLanguageRuntime *objc_runtime =
6771 process->GetObjCLanguageRuntime();
6772 if (objc_runtime != nullptr) {
6773 CompilerType parent_ast_type(getASTContext(),
6774 parent_qual_type);
6775 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6776 parent_ast_type, ivar_decl->getNameAsString().c_str());
6777 }
6778 }
6779
Aleksandr Urakovff701722018-08-20 05:59:27 +00006780 // Setting this to INT32_MAX to make sure we don't compute it
Kate Stoneb9c1b512016-09-06 20:57:50 +00006781 // twice...
Aleksandr Urakov53459482018-08-17 07:28:24 +00006782 bit_offset = INT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006783
6784 if (child_byte_offset ==
6785 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6786 bit_offset = interface_layout.getFieldOffset(child_idx -
6787 superclass_idx);
6788 child_byte_offset = bit_offset / 8;
6789 }
6790
6791 // Note, the ObjC Ivar Byte offset is just that, it doesn't
Adrian Prantl05097242018-04-30 16:49:04 +00006792 // account for the bit offset of a bitfield within its
6793 // containing object. So regardless of where we get the byte
Kate Stoneb9c1b512016-09-06 20:57:50 +00006794 // offset from, we still need to get the bit offset for
6795 // bitfields from the layout.
6796
6797 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6798 child_bitfield_bit_size)) {
Aleksandr Urakov53459482018-08-17 07:28:24 +00006799 if (bit_offset == INT32_MAX)
Kate Stoneb9c1b512016-09-06 20:57:50 +00006800 bit_offset = interface_layout.getFieldOffset(
6801 child_idx - superclass_idx);
6802
6803 child_bitfield_bit_offset = bit_offset % 8;
6804 }
6805 return CompilerType(getASTContext(), ivar_qual_type);
6806 }
6807 ++child_idx;
6808 }
6809 }
6810 }
6811 }
6812 }
6813 break;
6814
6815 case clang::Type::ObjCObjectPointer:
6816 if (idx_is_valid) {
6817 CompilerType pointee_clang_type(GetPointeeType(type));
6818
6819 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6820 child_is_deref_of_parent = false;
6821 bool tmp_child_is_deref_of_parent = false;
6822 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6823 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6824 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6825 child_bitfield_bit_size, child_bitfield_bit_offset,
6826 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6827 language_flags);
6828 } else {
6829 child_is_deref_of_parent = true;
6830 const char *parent_name =
6831 valobj ? valobj->GetName().GetCString() : NULL;
6832 if (parent_name) {
6833 child_name.assign(1, '*');
6834 child_name += parent_name;
6835 }
6836
6837 // We have a pointer to an simple type
6838 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
6839 child_byte_size = pointee_clang_type.GetByteSize(
6840 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6841 child_byte_offset = 0;
6842 return pointee_clang_type;
6843 }
6844 }
6845 }
6846 break;
6847
6848 case clang::Type::Vector:
6849 case clang::Type::ExtVector:
6850 if (idx_is_valid) {
6851 const clang::VectorType *array =
6852 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6853 if (array) {
6854 CompilerType element_type(getASTContext(), array->getElementType());
6855 if (element_type.GetCompleteType()) {
6856 char element_name[64];
6857 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6858 static_cast<uint64_t>(idx));
6859 child_name.assign(element_name);
6860 child_byte_size = element_type.GetByteSize(
6861 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6862 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6863 return element_type;
6864 }
6865 }
6866 }
6867 break;
6868
6869 case clang::Type::ConstantArray:
6870 case clang::Type::IncompleteArray:
6871 if (ignore_array_bounds || idx_is_valid) {
6872 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6873 if (array) {
6874 CompilerType element_type(getASTContext(), array->getElementType());
6875 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006876 child_name = llvm::formatv("[{0}]", idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006877 child_byte_size = element_type.GetByteSize(
6878 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6879 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6880 return element_type;
6881 }
6882 }
6883 }
6884 break;
6885
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006886 case clang::Type::Pointer: {
6887 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006888
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006889 // Don't dereference "void *" pointers
6890 if (pointee_clang_type.IsVoidType())
6891 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006892
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006893 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6894 child_is_deref_of_parent = false;
6895 bool tmp_child_is_deref_of_parent = false;
6896 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6897 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6898 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6899 child_bitfield_bit_size, child_bitfield_bit_offset,
6900 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6901 language_flags);
6902 } else {
6903 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006904
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006905 const char *parent_name =
6906 valobj ? valobj->GetName().GetCString() : NULL;
6907 if (parent_name) {
6908 child_name.assign(1, '*');
6909 child_name += parent_name;
6910 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006911
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006912 // We have a pointer to an simple type
6913 if (idx == 0) {
6914 child_byte_size = pointee_clang_type.GetByteSize(
6915 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6916 child_byte_offset = 0;
6917 return pointee_clang_type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006918 }
6919 }
6920 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006921 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006922
6923 case clang::Type::LValueReference:
6924 case clang::Type::RValueReference:
6925 if (idx_is_valid) {
6926 const clang::ReferenceType *reference_type =
6927 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6928 CompilerType pointee_clang_type(getASTContext(),
6929 reference_type->getPointeeType());
6930 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6931 child_is_deref_of_parent = false;
6932 bool tmp_child_is_deref_of_parent = false;
6933 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6934 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6935 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6936 child_bitfield_bit_size, child_bitfield_bit_offset,
6937 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6938 language_flags);
6939 } else {
6940 const char *parent_name =
6941 valobj ? valobj->GetName().GetCString() : NULL;
6942 if (parent_name) {
6943 child_name.assign(1, '&');
6944 child_name += parent_name;
6945 }
6946
6947 // We have a pointer to an simple type
6948 if (idx == 0) {
6949 child_byte_size = pointee_clang_type.GetByteSize(
6950 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6951 child_byte_offset = 0;
6952 return pointee_clang_type;
6953 }
6954 }
6955 }
6956 break;
6957
6958 case clang::Type::Typedef: {
6959 CompilerType typedefed_clang_type(
6960 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
6961 ->getDecl()
6962 ->getUnderlyingType());
6963 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
6964 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6965 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6966 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6967 child_is_deref_of_parent, valobj, language_flags);
6968 } break;
6969
6970 case clang::Type::Auto: {
6971 CompilerType elaborated_clang_type(
6972 getASTContext(),
6973 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
6974 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6975 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6976 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6977 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6978 child_is_deref_of_parent, valobj, language_flags);
6979 }
6980
6981 case clang::Type::Elaborated: {
6982 CompilerType elaborated_clang_type(
6983 getASTContext(),
6984 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
6985 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6986 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6987 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6988 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6989 child_is_deref_of_parent, valobj, language_flags);
6990 }
6991
6992 case clang::Type::Paren: {
6993 CompilerType paren_clang_type(
6994 getASTContext(),
6995 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
6996 return paren_clang_type.GetChildCompilerTypeAtIndex(
6997 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6998 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6999 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7000 child_is_deref_of_parent, valobj, language_flags);
7001 }
7002
7003 default:
7004 break;
7005 }
7006 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007007}
7008
Kate Stoneb9c1b512016-09-06 20:57:50 +00007009static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
7010 const clang::CXXBaseSpecifier *base_spec,
7011 bool omit_empty_base_classes) {
7012 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007013
Kate Stoneb9c1b512016-09-06 20:57:50 +00007014 const clang::CXXRecordDecl *cxx_record_decl =
7015 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7016
7017 // const char *super_name = record_decl->getNameAsCString();
7018 // const char *base_name =
7019 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
7020 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
7021 //
7022 if (cxx_record_decl) {
7023 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
7024 for (base_class = cxx_record_decl->bases_begin(),
7025 base_class_end = cxx_record_decl->bases_end();
7026 base_class != base_class_end; ++base_class) {
7027 if (omit_empty_base_classes) {
7028 if (BaseSpecifierIsEmpty(base_class))
7029 continue;
7030 }
7031
7032 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
7033 // super_name, base_name,
7034 // child_idx,
7035 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
7036 //
7037 //
7038 if (base_class == base_spec)
7039 return child_idx;
7040 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00007041 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007042 }
7043
7044 return UINT32_MAX;
7045}
7046
7047static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
7048 clang::NamedDecl *canonical_decl,
7049 bool omit_empty_base_classes) {
7050 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
7051 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
7052 omit_empty_base_classes);
7053
7054 clang::RecordDecl::field_iterator field, field_end;
7055 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
7056 field != field_end; ++field, ++child_idx) {
7057 if (field->getCanonicalDecl() == canonical_decl)
7058 return child_idx;
7059 }
7060
7061 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00007062}
7063
7064// Look for a child member (doesn't include base classes, but it does include
Adrian Prantl05097242018-04-30 16:49:04 +00007065// their members) in the type hierarchy. Returns an index path into
7066// "clang_type" on how to reach the appropriate member.
Greg Claytond8d4a572015-08-11 21:38:15 +00007067//
7068// class A
7069// {
7070// public:
7071// int m_a;
7072// int m_b;
7073// };
7074//
7075// class B
7076// {
7077// };
7078//
7079// class C :
7080// public B,
7081// public A
7082// {
7083// };
7084//
7085// If we have a clang type that describes "class C", and we wanted to looked
7086// "m_b" in it:
7087//
Kate Stoneb9c1b512016-09-06 20:57:50 +00007088// With omit_empty_base_classes == false we would get an integer array back
Adrian Prantl05097242018-04-30 16:49:04 +00007089// with: { 1, 1 } The first index 1 is the child index for "class A" within
7090// class C The second index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007091//
Adrian Prantl05097242018-04-30 16:49:04 +00007092// With omit_empty_base_classes == true we would get an integer array back
7093// with: { 0, 1 } The first index 0 is the child index for "class A" within
7094// class C (since class B doesn't have any members it doesn't count) The second
7095// index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007096
Kate Stoneb9c1b512016-09-06 20:57:50 +00007097size_t ClangASTContext::GetIndexOfChildMemberWithName(
7098 lldb::opaque_compiler_type_t type, const char *name,
7099 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
7100 if (type && name && name[0]) {
7101 clang::QualType qual_type(GetCanonicalQualType(type));
7102 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7103 switch (type_class) {
7104 case clang::Type::Record:
7105 if (GetCompleteType(type)) {
7106 const clang::RecordType *record_type =
7107 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7108 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00007109
Kate Stoneb9c1b512016-09-06 20:57:50 +00007110 assert(record_decl);
7111 uint32_t child_idx = 0;
7112
7113 const clang::CXXRecordDecl *cxx_record_decl =
7114 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7115
7116 // Try and find a field that matches NAME
7117 clang::RecordDecl::field_iterator field, field_end;
7118 llvm::StringRef name_sref(name);
7119 for (field = record_decl->field_begin(),
7120 field_end = record_decl->field_end();
7121 field != field_end; ++field, ++child_idx) {
7122 llvm::StringRef field_name = field->getName();
7123 if (field_name.empty()) {
7124 CompilerType field_type(getASTContext(), field->getType());
7125 child_indexes.push_back(child_idx);
7126 if (field_type.GetIndexOfChildMemberWithName(
7127 name, omit_empty_base_classes, child_indexes))
7128 return child_indexes.size();
7129 child_indexes.pop_back();
7130
7131 } else if (field_name.equals(name_sref)) {
7132 // We have to add on the number of base classes to this index!
7133 child_indexes.push_back(
7134 child_idx + ClangASTContext::GetNumBaseClasses(
7135 cxx_record_decl, omit_empty_base_classes));
7136 return child_indexes.size();
7137 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007138 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007139
Kate Stoneb9c1b512016-09-06 20:57:50 +00007140 if (cxx_record_decl) {
7141 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7142
7143 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7144
7145 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7146 // Didn't find things easily, lets let clang do its thang...
7147 clang::IdentifierInfo &ident_ref =
7148 getASTContext()->Idents.get(name_sref);
7149 clang::DeclarationName decl_name(&ident_ref);
7150
7151 clang::CXXBasePaths paths;
7152 if (cxx_record_decl->lookupInBases(
7153 [decl_name](const clang::CXXBaseSpecifier *specifier,
7154 clang::CXXBasePath &path) {
7155 return clang::CXXRecordDecl::FindOrdinaryMember(
7156 specifier, path, decl_name);
7157 },
7158 paths)) {
7159 clang::CXXBasePaths::const_paths_iterator path,
7160 path_end = paths.end();
7161 for (path = paths.begin(); path != path_end; ++path) {
7162 const size_t num_path_elements = path->size();
7163 for (size_t e = 0; e < num_path_elements; ++e) {
7164 clang::CXXBasePathElement elem = (*path)[e];
7165
7166 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7167 omit_empty_base_classes);
7168 if (child_idx == UINT32_MAX) {
7169 child_indexes.clear();
7170 return 0;
7171 } else {
7172 child_indexes.push_back(child_idx);
7173 parent_record_decl = llvm::cast<clang::RecordDecl>(
7174 elem.Base->getType()
7175 ->getAs<clang::RecordType>()
7176 ->getDecl());
7177 }
7178 }
7179 for (clang::NamedDecl *path_decl : path->Decls) {
7180 child_idx = GetIndexForRecordChild(
7181 parent_record_decl, path_decl, omit_empty_base_classes);
7182 if (child_idx == UINT32_MAX) {
7183 child_indexes.clear();
7184 return 0;
7185 } else {
7186 child_indexes.push_back(child_idx);
7187 }
7188 }
7189 }
7190 return child_indexes.size();
7191 }
7192 }
7193 }
7194 break;
7195
7196 case clang::Type::ObjCObject:
7197 case clang::Type::ObjCInterface:
7198 if (GetCompleteType(type)) {
7199 llvm::StringRef name_sref(name);
7200 const clang::ObjCObjectType *objc_class_type =
7201 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7202 assert(objc_class_type);
7203 if (objc_class_type) {
7204 uint32_t child_idx = 0;
7205 clang::ObjCInterfaceDecl *class_interface_decl =
7206 objc_class_type->getInterface();
7207
7208 if (class_interface_decl) {
7209 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7210 ivar_end = class_interface_decl->ivar_end();
7211 clang::ObjCInterfaceDecl *superclass_interface_decl =
7212 class_interface_decl->getSuperClass();
7213
7214 for (ivar_pos = class_interface_decl->ivar_begin();
7215 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7216 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7217
7218 if (ivar_decl->getName().equals(name_sref)) {
7219 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7220 (omit_empty_base_classes &&
7221 ObjCDeclHasIVars(superclass_interface_decl, true)))
7222 ++child_idx;
7223
7224 child_indexes.push_back(child_idx);
7225 return child_indexes.size();
7226 }
7227 }
7228
7229 if (superclass_interface_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00007230 // The super class index is always zero for ObjC classes, so we
7231 // push it onto the child indexes in case we find an ivar in our
7232 // superclass...
Kate Stoneb9c1b512016-09-06 20:57:50 +00007233 child_indexes.push_back(0);
7234
7235 CompilerType superclass_clang_type(
7236 getASTContext(), getASTContext()->getObjCInterfaceType(
7237 superclass_interface_decl));
7238 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7239 name, omit_empty_base_classes, child_indexes)) {
Adrian Prantl05097242018-04-30 16:49:04 +00007240 // We did find an ivar in a superclass so just return the
7241 // results!
Kate Stoneb9c1b512016-09-06 20:57:50 +00007242 return child_indexes.size();
7243 }
7244
Adrian Prantl05097242018-04-30 16:49:04 +00007245 // We didn't find an ivar matching "name" in our superclass, pop
7246 // the superclass zero index that we pushed on above.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007247 child_indexes.pop_back();
7248 }
7249 }
7250 }
7251 }
7252 break;
7253
7254 case clang::Type::ObjCObjectPointer: {
7255 CompilerType objc_object_clang_type(
7256 getASTContext(),
7257 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7258 ->getPointeeType());
7259 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7260 name, omit_empty_base_classes, child_indexes);
7261 } break;
7262
7263 case clang::Type::ConstantArray: {
7264 // const clang::ConstantArrayType *array =
7265 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7266 // const uint64_t element_count =
7267 // array->getSize().getLimitedValue();
7268 //
7269 // if (idx < element_count)
7270 // {
7271 // std::pair<uint64_t, unsigned> field_type_info =
7272 // ast->getTypeInfo(array->getElementType());
7273 //
7274 // char element_name[32];
7275 // ::snprintf (element_name, sizeof (element_name),
7276 // "%s[%u]", parent_name ? parent_name : "", idx);
7277 //
7278 // child_name.assign(element_name);
7279 // assert(field_type_info.first % 8 == 0);
7280 // child_byte_size = field_type_info.first / 8;
7281 // child_byte_offset = idx * child_byte_size;
7282 // return array->getElementType().getAsOpaquePtr();
7283 // }
7284 } break;
7285
7286 // case clang::Type::MemberPointerType:
7287 // {
7288 // MemberPointerType *mem_ptr_type =
7289 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7290 // clang::QualType pointee_type =
7291 // mem_ptr_type->getPointeeType();
7292 //
7293 // if (ClangASTContext::IsAggregateType
7294 // (pointee_type.getAsOpaquePtr()))
7295 // {
7296 // return GetIndexOfChildWithName (ast,
7297 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7298 // name);
7299 // }
7300 // }
7301 // break;
7302 //
7303 case clang::Type::LValueReference:
7304 case clang::Type::RValueReference: {
7305 const clang::ReferenceType *reference_type =
7306 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7307 clang::QualType pointee_type(reference_type->getPointeeType());
7308 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7309
7310 if (pointee_clang_type.IsAggregateType()) {
7311 return pointee_clang_type.GetIndexOfChildMemberWithName(
7312 name, omit_empty_base_classes, child_indexes);
7313 }
7314 } break;
7315
7316 case clang::Type::Pointer: {
7317 CompilerType pointee_clang_type(GetPointeeType(type));
7318
7319 if (pointee_clang_type.IsAggregateType()) {
7320 return pointee_clang_type.GetIndexOfChildMemberWithName(
7321 name, omit_empty_base_classes, child_indexes);
7322 }
7323 } break;
7324
7325 case clang::Type::Typedef:
7326 return CompilerType(getASTContext(),
7327 llvm::cast<clang::TypedefType>(qual_type)
7328 ->getDecl()
7329 ->getUnderlyingType())
7330 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7331 child_indexes);
7332
7333 case clang::Type::Auto:
7334 return CompilerType(
7335 getASTContext(),
7336 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7337 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7338 child_indexes);
7339
7340 case clang::Type::Elaborated:
7341 return CompilerType(
7342 getASTContext(),
7343 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7344 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7345 child_indexes);
7346
7347 case clang::Type::Paren:
7348 return CompilerType(getASTContext(),
7349 llvm::cast<clang::ParenType>(qual_type)->desugar())
7350 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7351 child_indexes);
7352
7353 default:
7354 break;
7355 }
7356 }
7357 return 0;
7358}
Greg Claytond8d4a572015-08-11 21:38:15 +00007359
7360// Get the index of the child of "clang_type" whose name matches. This function
7361// doesn't descend into the children, but only looks one level deep and name
7362// matches can include base class names.
7363
7364uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007365ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7366 const char *name,
7367 bool omit_empty_base_classes) {
7368 if (type && name && name[0]) {
7369 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007370
Kate Stoneb9c1b512016-09-06 20:57:50 +00007371 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7372
7373 switch (type_class) {
7374 case clang::Type::Record:
7375 if (GetCompleteType(type)) {
7376 const clang::RecordType *record_type =
7377 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7378 const clang::RecordDecl *record_decl = record_type->getDecl();
7379
7380 assert(record_decl);
7381 uint32_t child_idx = 0;
7382
7383 const clang::CXXRecordDecl *cxx_record_decl =
7384 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7385
7386 if (cxx_record_decl) {
7387 clang::CXXRecordDecl::base_class_const_iterator base_class,
7388 base_class_end;
7389 for (base_class = cxx_record_decl->bases_begin(),
7390 base_class_end = cxx_record_decl->bases_end();
7391 base_class != base_class_end; ++base_class) {
7392 // Skip empty base classes
7393 clang::CXXRecordDecl *base_class_decl =
7394 llvm::cast<clang::CXXRecordDecl>(
7395 base_class->getType()
7396 ->getAs<clang::RecordType>()
7397 ->getDecl());
7398 if (omit_empty_base_classes &&
7399 ClangASTContext::RecordHasFields(base_class_decl) == false)
7400 continue;
7401
7402 CompilerType base_class_clang_type(getASTContext(),
7403 base_class->getType());
7404 std::string base_class_type_name(
7405 base_class_clang_type.GetTypeName().AsCString(""));
7406 if (base_class_type_name.compare(name) == 0)
7407 return child_idx;
7408 ++child_idx;
7409 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007410 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007411
Kate Stoneb9c1b512016-09-06 20:57:50 +00007412 // Try and find a field that matches NAME
7413 clang::RecordDecl::field_iterator field, field_end;
7414 llvm::StringRef name_sref(name);
7415 for (field = record_decl->field_begin(),
7416 field_end = record_decl->field_end();
7417 field != field_end; ++field, ++child_idx) {
7418 if (field->getName().equals(name_sref))
7419 return child_idx;
7420 }
7421 }
7422 break;
7423
7424 case clang::Type::ObjCObject:
7425 case clang::Type::ObjCInterface:
7426 if (GetCompleteType(type)) {
7427 llvm::StringRef name_sref(name);
7428 const clang::ObjCObjectType *objc_class_type =
7429 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7430 assert(objc_class_type);
7431 if (objc_class_type) {
7432 uint32_t child_idx = 0;
7433 clang::ObjCInterfaceDecl *class_interface_decl =
7434 objc_class_type->getInterface();
7435
7436 if (class_interface_decl) {
7437 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7438 ivar_end = class_interface_decl->ivar_end();
7439 clang::ObjCInterfaceDecl *superclass_interface_decl =
7440 class_interface_decl->getSuperClass();
7441
7442 for (ivar_pos = class_interface_decl->ivar_begin();
7443 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7444 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7445
7446 if (ivar_decl->getName().equals(name_sref)) {
7447 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7448 (omit_empty_base_classes &&
7449 ObjCDeclHasIVars(superclass_interface_decl, true)))
7450 ++child_idx;
7451
7452 return child_idx;
7453 }
7454 }
7455
7456 if (superclass_interface_decl) {
7457 if (superclass_interface_decl->getName().equals(name_sref))
7458 return 0;
7459 }
7460 }
7461 }
7462 }
7463 break;
7464
7465 case clang::Type::ObjCObjectPointer: {
7466 CompilerType pointee_clang_type(
7467 getASTContext(),
7468 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7469 ->getPointeeType());
7470 return pointee_clang_type.GetIndexOfChildWithName(
7471 name, omit_empty_base_classes);
7472 } break;
7473
7474 case clang::Type::ConstantArray: {
7475 // const clang::ConstantArrayType *array =
7476 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7477 // const uint64_t element_count =
7478 // array->getSize().getLimitedValue();
7479 //
7480 // if (idx < element_count)
7481 // {
7482 // std::pair<uint64_t, unsigned> field_type_info =
7483 // ast->getTypeInfo(array->getElementType());
7484 //
7485 // char element_name[32];
7486 // ::snprintf (element_name, sizeof (element_name),
7487 // "%s[%u]", parent_name ? parent_name : "", idx);
7488 //
7489 // child_name.assign(element_name);
7490 // assert(field_type_info.first % 8 == 0);
7491 // child_byte_size = field_type_info.first / 8;
7492 // child_byte_offset = idx * child_byte_size;
7493 // return array->getElementType().getAsOpaquePtr();
7494 // }
7495 } break;
7496
7497 // case clang::Type::MemberPointerType:
7498 // {
7499 // MemberPointerType *mem_ptr_type =
7500 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7501 // clang::QualType pointee_type =
7502 // mem_ptr_type->getPointeeType();
7503 //
7504 // if (ClangASTContext::IsAggregateType
7505 // (pointee_type.getAsOpaquePtr()))
7506 // {
7507 // return GetIndexOfChildWithName (ast,
7508 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7509 // name);
7510 // }
7511 // }
7512 // break;
7513 //
7514 case clang::Type::LValueReference:
7515 case clang::Type::RValueReference: {
7516 const clang::ReferenceType *reference_type =
7517 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7518 CompilerType pointee_type(getASTContext(),
7519 reference_type->getPointeeType());
7520
7521 if (pointee_type.IsAggregateType()) {
7522 return pointee_type.GetIndexOfChildWithName(name,
7523 omit_empty_base_classes);
7524 }
7525 } break;
7526
7527 case clang::Type::Pointer: {
7528 const clang::PointerType *pointer_type =
7529 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7530 CompilerType pointee_type(getASTContext(),
7531 pointer_type->getPointeeType());
7532
7533 if (pointee_type.IsAggregateType()) {
7534 return pointee_type.GetIndexOfChildWithName(name,
7535 omit_empty_base_classes);
7536 } else {
7537 // if (parent_name)
7538 // {
7539 // child_name.assign(1, '*');
7540 // child_name += parent_name;
7541 // }
7542 //
7543 // // We have a pointer to an simple type
7544 // if (idx == 0)
7545 // {
7546 // std::pair<uint64_t, unsigned> clang_type_info
7547 // = ast->getTypeInfo(pointee_type);
7548 // assert(clang_type_info.first % 8 == 0);
7549 // child_byte_size = clang_type_info.first / 8;
7550 // child_byte_offset = 0;
7551 // return pointee_type.getAsOpaquePtr();
7552 // }
7553 }
7554 } break;
7555
7556 case clang::Type::Auto:
7557 return CompilerType(
7558 getASTContext(),
7559 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7560 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7561
7562 case clang::Type::Elaborated:
7563 return CompilerType(
7564 getASTContext(),
7565 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7566 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7567
7568 case clang::Type::Paren:
7569 return CompilerType(getASTContext(),
7570 llvm::cast<clang::ParenType>(qual_type)->desugar())
7571 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7572
7573 case clang::Type::Typedef:
7574 return CompilerType(getASTContext(),
7575 llvm::cast<clang::TypedefType>(qual_type)
7576 ->getDecl()
7577 ->getUnderlyingType())
7578 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7579
7580 default:
7581 break;
7582 }
7583 }
7584 return UINT32_MAX;
7585}
Greg Claytond8d4a572015-08-11 21:38:15 +00007586
7587size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007588ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7589 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007590 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007591
Kate Stoneb9c1b512016-09-06 20:57:50 +00007592 clang::QualType qual_type(GetCanonicalQualType(type));
7593 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7594 switch (type_class) {
7595 case clang::Type::Record:
7596 if (GetCompleteType(type)) {
7597 const clang::CXXRecordDecl *cxx_record_decl =
7598 qual_type->getAsCXXRecordDecl();
7599 if (cxx_record_decl) {
7600 const clang::ClassTemplateSpecializationDecl *template_decl =
7601 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7602 cxx_record_decl);
7603 if (template_decl)
7604 return template_decl->getTemplateArgs().size();
7605 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007606 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007607 break;
7608
7609 case clang::Type::Typedef:
7610 return (CompilerType(getASTContext(),
7611 llvm::cast<clang::TypedefType>(qual_type)
7612 ->getDecl()
7613 ->getUnderlyingType()))
7614 .GetNumTemplateArguments();
7615
7616 case clang::Type::Auto:
7617 return (CompilerType(
7618 getASTContext(),
7619 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7620 .GetNumTemplateArguments();
7621
7622 case clang::Type::Elaborated:
7623 return (CompilerType(
7624 getASTContext(),
7625 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7626 .GetNumTemplateArguments();
7627
7628 case clang::Type::Paren:
7629 return (CompilerType(getASTContext(),
7630 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7631 .GetNumTemplateArguments();
7632
7633 default:
7634 break;
7635 }
7636
7637 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007638}
7639
Pavel Labath769b21e2017-11-13 14:26:21 +00007640const clang::ClassTemplateSpecializationDecl *
7641ClangASTContext::GetAsTemplateSpecialization(
7642 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007643 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007644 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007645
7646 clang::QualType qual_type(GetCanonicalQualType(type));
7647 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7648 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007649 case clang::Type::Record: {
7650 if (! GetCompleteType(type))
7651 return nullptr;
7652 const clang::CXXRecordDecl *cxx_record_decl =
7653 qual_type->getAsCXXRecordDecl();
7654 if (!cxx_record_decl)
7655 return nullptr;
7656 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7657 cxx_record_decl);
7658 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007659
7660 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007661 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7662 ->getDecl()
7663 ->getUnderlyingType()
7664 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007665
7666 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007667 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7668 ->getDeducedType()
7669 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007670
7671 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007672 return GetAsTemplateSpecialization(
7673 llvm::cast<clang::ElaboratedType>(qual_type)
7674 ->getNamedType()
7675 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007676
7677 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007678 return GetAsTemplateSpecialization(
7679 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007680
7681 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007682 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007683 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007684}
7685
7686lldb::TemplateArgumentKind
7687ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7688 size_t arg_idx) {
7689 const clang::ClassTemplateSpecializationDecl *template_decl =
7690 GetAsTemplateSpecialization(type);
7691 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7692 return eTemplateArgumentKindNull;
7693
7694 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7695 case clang::TemplateArgument::Null:
7696 return eTemplateArgumentKindNull;
7697
7698 case clang::TemplateArgument::NullPtr:
7699 return eTemplateArgumentKindNullPtr;
7700
7701 case clang::TemplateArgument::Type:
7702 return eTemplateArgumentKindType;
7703
7704 case clang::TemplateArgument::Declaration:
7705 return eTemplateArgumentKindDeclaration;
7706
7707 case clang::TemplateArgument::Integral:
7708 return eTemplateArgumentKindIntegral;
7709
7710 case clang::TemplateArgument::Template:
7711 return eTemplateArgumentKindTemplate;
7712
7713 case clang::TemplateArgument::TemplateExpansion:
7714 return eTemplateArgumentKindTemplateExpansion;
7715
7716 case clang::TemplateArgument::Expression:
7717 return eTemplateArgumentKindExpression;
7718
7719 case clang::TemplateArgument::Pack:
7720 return eTemplateArgumentKindPack;
7721 }
7722 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7723}
7724
7725CompilerType
7726ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7727 size_t idx) {
7728 const clang::ClassTemplateSpecializationDecl *template_decl =
7729 GetAsTemplateSpecialization(type);
7730 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7731 return CompilerType();
7732
7733 const clang::TemplateArgument &template_arg =
7734 template_decl->getTemplateArgs()[idx];
7735 if (template_arg.getKind() != clang::TemplateArgument::Type)
7736 return CompilerType();
7737
7738 return CompilerType(getASTContext(), template_arg.getAsType());
7739}
7740
Pavel Labathf59056f2017-11-30 10:16:54 +00007741llvm::Optional<CompilerType::IntegralTemplateArgument>
Pavel Labath769b21e2017-11-13 14:26:21 +00007742ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7743 size_t idx) {
7744 const clang::ClassTemplateSpecializationDecl *template_decl =
7745 GetAsTemplateSpecialization(type);
7746 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
Pavel Labathf59056f2017-11-30 10:16:54 +00007747 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007748
7749 const clang::TemplateArgument &template_arg =
7750 template_decl->getTemplateArgs()[idx];
7751 if (template_arg.getKind() != clang::TemplateArgument::Integral)
Pavel Labathf59056f2017-11-30 10:16:54 +00007752 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007753
Pavel Labathf59056f2017-11-30 10:16:54 +00007754 return {{template_arg.getAsIntegral(),
7755 CompilerType(getASTContext(), template_arg.getIntegralType())}};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007756}
7757
Kate Stoneb9c1b512016-09-06 20:57:50 +00007758CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7759 if (type)
7760 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7761 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007762}
7763
Kate Stoneb9c1b512016-09-06 20:57:50 +00007764clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7765 const clang::EnumType *enutype =
7766 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7767 if (enutype)
7768 return enutype->getDecl();
7769 return NULL;
7770}
7771
7772clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7773 const clang::RecordType *record_type =
7774 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7775 if (record_type)
7776 return record_type->getDecl();
7777 return nullptr;
7778}
7779
7780clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
7781 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
7782 if (qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00007783 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007784 else
7785 return qual_type->getAsTagDecl();
Greg Claytone6b36cd2015-12-08 01:02:08 +00007786}
7787
Aleksandr Urakov709426b2018-09-10 08:08:43 +00007788clang::TypedefNameDecl *
7789ClangASTContext::GetAsTypedefDecl(const CompilerType &type) {
7790 const clang::TypedefType *typedef_type =
7791 llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type));
7792 if (typedef_type)
7793 return typedef_type->getDecl();
7794 return nullptr;
7795}
7796
Greg Claytond8d4a572015-08-11 21:38:15 +00007797clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007798ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7799 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007800}
7801
7802clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007803ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7804 const clang::ObjCObjectType *objc_class_type =
7805 llvm::dyn_cast<clang::ObjCObjectType>(
7806 ClangUtil::GetCanonicalQualType(type));
7807 if (objc_class_type)
7808 return objc_class_type->getInterface();
7809 return nullptr;
7810}
7811
7812clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007813 const CompilerType &type, llvm::StringRef name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00007814 const CompilerType &field_clang_type, AccessType access,
7815 uint32_t bitfield_bit_size) {
7816 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007817 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007818 ClangASTContext *ast =
7819 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7820 if (!ast)
7821 return nullptr;
7822 clang::ASTContext *clang_ast = ast->getASTContext();
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007823 clang::IdentifierInfo *ident = nullptr;
7824 if (!name.empty())
7825 ident = &clang_ast->Idents.get(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00007826
7827 clang::FieldDecl *field = nullptr;
7828
7829 clang::Expr *bit_width = nullptr;
7830 if (bitfield_bit_size != 0) {
7831 llvm::APInt bitfield_bit_size_apint(
7832 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7833 bit_width = new (*clang_ast)
7834 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7835 clang_ast->IntTy, clang::SourceLocation());
7836 }
7837
7838 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7839 if (record_decl) {
7840 field = clang::FieldDecl::Create(
7841 *clang_ast, record_decl, clang::SourceLocation(),
7842 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007843 ident, // Identifier
7844 ClangUtil::GetQualType(field_clang_type), // Field type
7845 nullptr, // TInfo *
7846 bit_width, // BitWidth
7847 false, // Mutable
7848 clang::ICIS_NoInit); // HasInit
Kate Stoneb9c1b512016-09-06 20:57:50 +00007849
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007850 if (name.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +00007851 // Determine whether this field corresponds to an anonymous struct or
7852 // union.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007853 if (const clang::TagType *TagT =
7854 field->getType()->getAs<clang::TagType>()) {
7855 if (clang::RecordDecl *Rec =
7856 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7857 if (!Rec->getDeclName()) {
7858 Rec->setAnonymousStructOrUnion(true);
7859 field->setImplicit();
7860 }
7861 }
7862 }
7863
7864 if (field) {
7865 field->setAccess(
7866 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7867
7868 record_decl->addDecl(field);
7869
7870#ifdef LLDB_CONFIGURATION_DEBUG
7871 VerifyDecl(field);
7872#endif
7873 }
7874 } else {
7875 clang::ObjCInterfaceDecl *class_interface_decl =
7876 ast->GetAsObjCInterfaceDecl(type);
7877
7878 if (class_interface_decl) {
7879 const bool is_synthesized = false;
7880
7881 field_clang_type.GetCompleteType();
7882
7883 field = clang::ObjCIvarDecl::Create(
7884 *clang_ast, class_interface_decl, clang::SourceLocation(),
7885 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007886 ident, // Identifier
7887 ClangUtil::GetQualType(field_clang_type), // Field type
7888 nullptr, // TypeSourceInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007889 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7890 is_synthesized);
7891
7892 if (field) {
7893 class_interface_decl->addDecl(field);
7894
7895#ifdef LLDB_CONFIGURATION_DEBUG
7896 VerifyDecl(field);
7897#endif
7898 }
7899 }
7900 }
7901 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007902}
7903
Kate Stoneb9c1b512016-09-06 20:57:50 +00007904void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7905 if (!type)
7906 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007907
Kate Stoneb9c1b512016-09-06 20:57:50 +00007908 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7909 if (!ast)
7910 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007911
Kate Stoneb9c1b512016-09-06 20:57:50 +00007912 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007913
Kate Stoneb9c1b512016-09-06 20:57:50 +00007914 if (!record_decl)
7915 return;
7916
7917 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7918
7919 IndirectFieldVector indirect_fields;
7920 clang::RecordDecl::field_iterator field_pos;
7921 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7922 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7923 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7924 last_field_pos = field_pos++) {
7925 if (field_pos->isAnonymousStructOrUnion()) {
7926 clang::QualType field_qual_type = field_pos->getType();
7927
7928 const clang::RecordType *field_record_type =
7929 field_qual_type->getAs<clang::RecordType>();
7930
7931 if (!field_record_type)
7932 continue;
7933
7934 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7935
7936 if (!field_record_decl)
7937 continue;
7938
7939 for (clang::RecordDecl::decl_iterator
7940 di = field_record_decl->decls_begin(),
7941 de = field_record_decl->decls_end();
7942 di != de; ++di) {
7943 if (clang::FieldDecl *nested_field_decl =
7944 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7945 clang::NamedDecl **chain =
7946 new (*ast->getASTContext()) clang::NamedDecl *[2];
7947 chain[0] = *field_pos;
7948 chain[1] = nested_field_decl;
7949 clang::IndirectFieldDecl *indirect_field =
7950 clang::IndirectFieldDecl::Create(
7951 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7952 nested_field_decl->getIdentifier(),
7953 nested_field_decl->getType(), {chain, 2});
7954
7955 indirect_field->setImplicit();
7956
7957 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7958 field_pos->getAccess(), nested_field_decl->getAccess()));
7959
7960 indirect_fields.push_back(indirect_field);
7961 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7962 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7963 size_t nested_chain_size =
7964 nested_indirect_field_decl->getChainingSize();
7965 clang::NamedDecl **chain = new (*ast->getASTContext())
7966 clang::NamedDecl *[nested_chain_size + 1];
7967 chain[0] = *field_pos;
7968
7969 int chain_index = 1;
7970 for (clang::IndirectFieldDecl::chain_iterator
7971 nci = nested_indirect_field_decl->chain_begin(),
7972 nce = nested_indirect_field_decl->chain_end();
7973 nci < nce; ++nci) {
7974 chain[chain_index] = *nci;
7975 chain_index++;
7976 }
7977
7978 clang::IndirectFieldDecl *indirect_field =
7979 clang::IndirectFieldDecl::Create(
7980 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7981 nested_indirect_field_decl->getIdentifier(),
7982 nested_indirect_field_decl->getType(),
7983 {chain, nested_chain_size + 1});
7984
7985 indirect_field->setImplicit();
7986
7987 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7988 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
7989
7990 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00007991 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007992 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007993 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007994 }
7995
Adrian Prantl05097242018-04-30 16:49:04 +00007996 // Check the last field to see if it has an incomplete array type as its last
7997 // member and if it does, the tell the record decl about it
Kate Stoneb9c1b512016-09-06 20:57:50 +00007998 if (last_field_pos != field_end_pos) {
7999 if (last_field_pos->getType()->isIncompleteArrayType())
8000 record_decl->hasFlexibleArrayMember();
8001 }
8002
8003 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
8004 ife = indirect_fields.end();
8005 ifi < ife; ++ifi) {
8006 record_decl->addDecl(*ifi);
8007 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008008}
8009
Kate Stoneb9c1b512016-09-06 20:57:50 +00008010void ClangASTContext::SetIsPacked(const CompilerType &type) {
8011 if (type) {
8012 ClangASTContext *ast =
8013 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8014 if (ast) {
8015 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
8016
8017 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00008018 return;
8019
Kate Stoneb9c1b512016-09-06 20:57:50 +00008020 record_decl->addAttr(
8021 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00008022 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008023 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008024}
8025
Kate Stoneb9c1b512016-09-06 20:57:50 +00008026clang::VarDecl *ClangASTContext::AddVariableToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008027 const CompilerType &type, llvm::StringRef name,
8028 const CompilerType &var_type, AccessType access) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00008029 if (!type.IsValid() || !var_type.IsValid())
8030 return nullptr;
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008031
Kate Stoneb9c1b512016-09-06 20:57:50 +00008032 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8033 if (!ast)
8034 return nullptr;
8035
8036 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008037 if (!record_decl)
8038 return nullptr;
8039
8040 clang::VarDecl *var_decl = nullptr;
8041 clang::IdentifierInfo *ident = nullptr;
8042 if (!name.empty())
8043 ident = &ast->getASTContext()->Idents.get(name);
8044
8045 var_decl = clang::VarDecl::Create(
8046 *ast->getASTContext(), // ASTContext &
8047 record_decl, // DeclContext *
8048 clang::SourceLocation(), // clang::SourceLocation StartLoc
8049 clang::SourceLocation(), // clang::SourceLocation IdLoc
8050 ident, // clang::IdentifierInfo *
8051 ClangUtil::GetQualType(var_type), // Variable clang::QualType
8052 nullptr, // TypeSourceInfo *
8053 clang::SC_Static); // StorageClass
8054 if (!var_decl)
8055 return nullptr;
8056
8057 var_decl->setAccess(
8058 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
8059 record_decl->addDecl(var_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008060
Greg Claytond8d4a572015-08-11 21:38:15 +00008061#ifdef LLDB_CONFIGURATION_DEBUG
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008062 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008063#endif
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008064
Kate Stoneb9c1b512016-09-06 20:57:50 +00008065 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008066}
8067
Kate Stoneb9c1b512016-09-06 20:57:50 +00008068clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
Davide Italiano675767a2018-03-27 19:40:50 +00008069 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008070 const CompilerType &method_clang_type, lldb::AccessType access,
8071 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
8072 bool is_attr_used, bool is_artificial) {
8073 if (!type || !method_clang_type.IsValid() || name == nullptr ||
8074 name[0] == '\0')
8075 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008076
Kate Stoneb9c1b512016-09-06 20:57:50 +00008077 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008078
Kate Stoneb9c1b512016-09-06 20:57:50 +00008079 clang::CXXRecordDecl *cxx_record_decl =
8080 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008081
Kate Stoneb9c1b512016-09-06 20:57:50 +00008082 if (cxx_record_decl == nullptr)
8083 return nullptr;
8084
8085 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8086
8087 clang::CXXMethodDecl *cxx_method_decl = nullptr;
8088
8089 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
8090
8091 const clang::FunctionType *function_type =
8092 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
8093
8094 if (function_type == nullptr)
8095 return nullptr;
8096
8097 const clang::FunctionProtoType *method_function_prototype(
8098 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
8099
8100 if (!method_function_prototype)
8101 return nullptr;
8102
8103 unsigned int num_params = method_function_prototype->getNumParams();
8104
8105 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
8106 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
8107
8108 if (is_artificial)
8109 return nullptr; // skip everything artificial
8110
8111 if (name[0] == '~') {
8112 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
8113 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8114 clang::DeclarationNameInfo(
8115 getASTContext()->DeclarationNames.getCXXDestructorName(
8116 getASTContext()->getCanonicalType(record_qual_type)),
8117 clang::SourceLocation()),
8118 method_qual_type, nullptr, is_inline, is_artificial);
8119 cxx_method_decl = cxx_dtor_decl;
8120 } else if (decl_name == cxx_record_decl->getDeclName()) {
8121 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
8122 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8123 clang::DeclarationNameInfo(
8124 getASTContext()->DeclarationNames.getCXXConstructorName(
8125 getASTContext()->getCanonicalType(record_qual_type)),
8126 clang::SourceLocation()),
8127 method_qual_type,
8128 nullptr, // TypeSourceInfo *
8129 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
8130 cxx_method_decl = cxx_ctor_decl;
8131 } else {
8132 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8133 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8134
8135 if (IsOperator(name, op_kind)) {
8136 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
Adrian Prantl05097242018-04-30 16:49:04 +00008137 // Check the number of operator parameters. Sometimes we have seen bad
8138 // DWARF that doesn't correctly describe operators and if we try to
8139 // create a method and add it to the class, clang will assert and
8140 // crash, so we need to make sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008141 const bool is_method = true;
8142 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8143 is_method, op_kind, num_params))
8144 return nullptr;
8145 cxx_method_decl = clang::CXXMethodDecl::Create(
8146 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8147 clang::DeclarationNameInfo(
8148 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8149 clang::SourceLocation()),
8150 method_qual_type,
8151 nullptr, // TypeSourceInfo *
8152 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8153 } else if (num_params == 0) {
8154 // Conversion operators don't take params...
8155 cxx_method_decl = clang::CXXConversionDecl::Create(
8156 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8157 clang::DeclarationNameInfo(
8158 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8159 getASTContext()->getCanonicalType(
8160 function_type->getReturnType())),
8161 clang::SourceLocation()),
8162 method_qual_type,
8163 nullptr, // TypeSourceInfo *
8164 is_inline, is_explicit, false /*is_constexpr*/,
8165 clang::SourceLocation());
8166 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008167 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008168
8169 if (cxx_method_decl == nullptr) {
8170 cxx_method_decl = clang::CXXMethodDecl::Create(
8171 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8172 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8173 method_qual_type,
8174 nullptr, // TypeSourceInfo *
8175 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008176 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008177 }
8178
8179 clang::AccessSpecifier access_specifier =
8180 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8181
8182 cxx_method_decl->setAccess(access_specifier);
8183 cxx_method_decl->setVirtualAsWritten(is_virtual);
8184
8185 if (is_attr_used)
8186 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8187
Davide Italiano675767a2018-03-27 19:40:50 +00008188 if (mangled_name != NULL) {
8189 cxx_method_decl->addAttr(
8190 clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name));
8191 }
8192
Kate Stoneb9c1b512016-09-06 20:57:50 +00008193 // Populate the method decl with parameter decls
8194
8195 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8196
8197 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8198 params.push_back(clang::ParmVarDecl::Create(
8199 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8200 clang::SourceLocation(),
8201 nullptr, // anonymous
8202 method_function_prototype->getParamType(param_index), nullptr,
8203 clang::SC_None, nullptr));
8204 }
8205
8206 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8207
8208 cxx_record_decl->addDecl(cxx_method_decl);
8209
8210 // Sometimes the debug info will mention a constructor (default/copy/move),
8211 // destructor, or assignment operator (copy/move) but there won't be any
8212 // version of this in the code. So we check if the function was artificially
8213 // generated and if it is trivial and this lets the compiler/backend know
8214 // that it can inline the IR for these when it needs to and we can avoid a
8215 // "missing function" error when running expressions.
8216
8217 if (is_artificial) {
8218 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8219 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8220 (cxx_ctor_decl->isCopyConstructor() &&
8221 cxx_record_decl->hasTrivialCopyConstructor()) ||
8222 (cxx_ctor_decl->isMoveConstructor() &&
8223 cxx_record_decl->hasTrivialMoveConstructor()))) {
8224 cxx_ctor_decl->setDefaulted();
8225 cxx_ctor_decl->setTrivial(true);
8226 } else if (cxx_dtor_decl) {
8227 if (cxx_record_decl->hasTrivialDestructor()) {
8228 cxx_dtor_decl->setDefaulted();
8229 cxx_dtor_decl->setTrivial(true);
8230 }
8231 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8232 cxx_record_decl->hasTrivialCopyAssignment()) ||
8233 (cxx_method_decl->isMoveAssignmentOperator() &&
8234 cxx_record_decl->hasTrivialMoveAssignment())) {
8235 cxx_method_decl->setDefaulted();
8236 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008237 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008238 }
8239
Greg Claytond8d4a572015-08-11 21:38:15 +00008240#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008241 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008242#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008243
Kate Stoneb9c1b512016-09-06 20:57:50 +00008244 // printf ("decl->isPolymorphic() = %i\n",
8245 // cxx_record_decl->isPolymorphic());
8246 // printf ("decl->isAggregate() = %i\n",
8247 // cxx_record_decl->isAggregate());
8248 // printf ("decl->isPOD() = %i\n",
8249 // cxx_record_decl->isPOD());
8250 // printf ("decl->isEmpty() = %i\n",
8251 // cxx_record_decl->isEmpty());
8252 // printf ("decl->isAbstract() = %i\n",
8253 // cxx_record_decl->isAbstract());
8254 // printf ("decl->hasTrivialConstructor() = %i\n",
8255 // cxx_record_decl->hasTrivialConstructor());
8256 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8257 // cxx_record_decl->hasTrivialCopyConstructor());
8258 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8259 // cxx_record_decl->hasTrivialCopyAssignment());
8260 // printf ("decl->hasTrivialDestructor() = %i\n",
8261 // cxx_record_decl->hasTrivialDestructor());
8262 return cxx_method_decl;
8263}
Greg Claytond8d4a572015-08-11 21:38:15 +00008264
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00008265void ClangASTContext::AddMethodOverridesForCXXRecordType(
8266 lldb::opaque_compiler_type_t type) {
8267 if (auto *record = GetAsCXXRecordDecl(type))
8268 for (auto *method : record->methods())
8269 addOverridesForMethod(method);
8270}
8271
Greg Claytond8d4a572015-08-11 21:38:15 +00008272#pragma mark C++ Base Classes
8273
Zachary Turner970f38e2018-10-25 20:44:56 +00008274std::unique_ptr<clang::CXXBaseSpecifier>
Kate Stoneb9c1b512016-09-06 20:57:50 +00008275ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8276 AccessType access, bool is_virtual,
8277 bool base_of_class) {
Zachary Turner970f38e2018-10-25 20:44:56 +00008278 if (!type)
8279 return nullptr;
8280
8281 return llvm::make_unique<clang::CXXBaseSpecifier>(
8282 clang::SourceRange(), is_virtual, base_of_class,
8283 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8284 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8285 clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008286}
8287
Zachary Turner970f38e2018-10-25 20:44:56 +00008288bool ClangASTContext::TransferBaseClasses(
Kate Stoneb9c1b512016-09-06 20:57:50 +00008289 lldb::opaque_compiler_type_t type,
Zachary Turner970f38e2018-10-25 20:44:56 +00008290 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) {
8291 if (!type)
8292 return false;
8293 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8294 if (!cxx_record_decl)
8295 return false;
8296 std::vector<clang::CXXBaseSpecifier *> raw_bases;
8297 raw_bases.reserve(bases.size());
8298
8299 // Clang will make a copy of them, so it's ok that we pass pointers that we're
8300 // about to destroy.
8301 for (auto &b : bases)
8302 raw_bases.push_back(b.get());
8303 cxx_record_decl->setBases(raw_bases.data(), raw_bases.size());
8304 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008305}
8306
8307bool ClangASTContext::SetObjCSuperClass(
8308 const CompilerType &type, const CompilerType &superclass_clang_type) {
8309 ClangASTContext *ast =
8310 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8311 if (!ast)
8312 return false;
8313 clang::ASTContext *clang_ast = ast->getASTContext();
8314
8315 if (type && superclass_clang_type.IsValid() &&
8316 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8317 clang::ObjCInterfaceDecl *class_interface_decl =
8318 GetAsObjCInterfaceDecl(type);
8319 clang::ObjCInterfaceDecl *super_interface_decl =
8320 GetAsObjCInterfaceDecl(superclass_clang_type);
8321 if (class_interface_decl && super_interface_decl) {
8322 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8323 clang_ast->getObjCInterfaceType(super_interface_decl)));
8324 return true;
8325 }
8326 }
8327 return false;
8328}
8329
8330bool ClangASTContext::AddObjCClassProperty(
8331 const CompilerType &type, const char *property_name,
8332 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8333 const char *property_setter_name, const char *property_getter_name,
8334 uint32_t property_attributes, ClangASTMetadata *metadata) {
8335 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8336 property_name[0] == '\0')
8337 return false;
8338 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8339 if (!ast)
8340 return false;
8341 clang::ASTContext *clang_ast = ast->getASTContext();
8342
8343 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8344
8345 if (class_interface_decl) {
8346 CompilerType property_clang_type_to_access;
8347
8348 if (property_clang_type.IsValid())
8349 property_clang_type_to_access = property_clang_type;
8350 else if (ivar_decl)
8351 property_clang_type_to_access =
8352 CompilerType(clang_ast, ivar_decl->getType());
8353
8354 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8355 clang::TypeSourceInfo *prop_type_source;
8356 if (ivar_decl)
8357 prop_type_source =
8358 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8359 else
8360 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8361 ClangUtil::GetQualType(property_clang_type));
8362
8363 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8364 *clang_ast, class_interface_decl,
8365 clang::SourceLocation(), // Source Location
8366 &clang_ast->Idents.get(property_name),
8367 clang::SourceLocation(), // Source Location for AT
8368 clang::SourceLocation(), // Source location for (
8369 ivar_decl ? ivar_decl->getType()
8370 : ClangUtil::GetQualType(property_clang_type),
8371 prop_type_source);
8372
8373 if (property_decl) {
8374 if (metadata)
8375 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8376
8377 class_interface_decl->addDecl(property_decl);
8378
8379 clang::Selector setter_sel, getter_sel;
8380
8381 if (property_setter_name != nullptr) {
8382 std::string property_setter_no_colon(
8383 property_setter_name, strlen(property_setter_name) - 1);
8384 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008385 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008386 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8387 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8388 std::string setter_sel_string("set");
8389 setter_sel_string.push_back(::toupper(property_name[0]));
8390 setter_sel_string.append(&property_name[1]);
8391 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008392 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008393 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8394 }
8395 property_decl->setSetterName(setter_sel);
8396 property_decl->setPropertyAttributes(
8397 clang::ObjCPropertyDecl::OBJC_PR_setter);
8398
8399 if (property_getter_name != nullptr) {
8400 clang::IdentifierInfo *getter_ident =
8401 &clang_ast->Idents.get(property_getter_name);
8402 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8403 } else {
8404 clang::IdentifierInfo *getter_ident =
8405 &clang_ast->Idents.get(property_name);
8406 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8407 }
8408 property_decl->setGetterName(getter_sel);
8409 property_decl->setPropertyAttributes(
8410 clang::ObjCPropertyDecl::OBJC_PR_getter);
8411
8412 if (ivar_decl)
8413 property_decl->setPropertyIvarDecl(ivar_decl);
8414
8415 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8416 property_decl->setPropertyAttributes(
8417 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8418 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8419 property_decl->setPropertyAttributes(
8420 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8421 if (property_attributes & DW_APPLE_PROPERTY_assign)
8422 property_decl->setPropertyAttributes(
8423 clang::ObjCPropertyDecl::OBJC_PR_assign);
8424 if (property_attributes & DW_APPLE_PROPERTY_retain)
8425 property_decl->setPropertyAttributes(
8426 clang::ObjCPropertyDecl::OBJC_PR_retain);
8427 if (property_attributes & DW_APPLE_PROPERTY_copy)
8428 property_decl->setPropertyAttributes(
8429 clang::ObjCPropertyDecl::OBJC_PR_copy);
8430 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8431 property_decl->setPropertyAttributes(
8432 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8433 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8434 property_decl->setPropertyAttributes(
8435 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8436 if (property_attributes &
8437 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8438 property_decl->setPropertyAttributes(
8439 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8440 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8441 property_decl->setPropertyAttributes(
8442 clang::ObjCPropertyDecl::OBJC_PR_class);
8443
8444 const bool isInstance =
8445 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8446
8447 if (!getter_sel.isNull() &&
8448 !(isInstance
8449 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8450 : class_interface_decl->lookupClassMethod(getter_sel))) {
8451 const bool isVariadic = false;
8452 const bool isSynthesized = false;
8453 const bool isImplicitlyDeclared = true;
8454 const bool isDefined = false;
8455 const clang::ObjCMethodDecl::ImplementationControl impControl =
8456 clang::ObjCMethodDecl::None;
8457 const bool HasRelatedResultType = false;
8458
8459 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8460 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8461 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8462 nullptr, class_interface_decl, isInstance, isVariadic,
8463 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8464 HasRelatedResultType);
8465
8466 if (getter && metadata)
8467 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8468
8469 if (getter) {
8470 getter->setMethodParams(*clang_ast,
8471 llvm::ArrayRef<clang::ParmVarDecl *>(),
8472 llvm::ArrayRef<clang::SourceLocation>());
8473
8474 class_interface_decl->addDecl(getter);
8475 }
8476 }
8477
8478 if (!setter_sel.isNull() &&
8479 !(isInstance
8480 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8481 : class_interface_decl->lookupClassMethod(setter_sel))) {
8482 clang::QualType result_type = clang_ast->VoidTy;
8483 const bool isVariadic = false;
8484 const bool isSynthesized = false;
8485 const bool isImplicitlyDeclared = true;
8486 const bool isDefined = false;
8487 const clang::ObjCMethodDecl::ImplementationControl impControl =
8488 clang::ObjCMethodDecl::None;
8489 const bool HasRelatedResultType = false;
8490
8491 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8492 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8493 setter_sel, result_type, nullptr, class_interface_decl,
8494 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8495 isDefined, impControl, HasRelatedResultType);
8496
8497 if (setter && metadata)
8498 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8499
8500 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8501
8502 params.push_back(clang::ParmVarDecl::Create(
8503 *clang_ast, setter, clang::SourceLocation(),
8504 clang::SourceLocation(),
8505 nullptr, // anonymous
8506 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8507 clang::SC_Auto, nullptr));
8508
8509 if (setter) {
8510 setter->setMethodParams(
8511 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8512 llvm::ArrayRef<clang::SourceLocation>());
8513
8514 class_interface_decl->addDecl(setter);
8515 }
8516 }
8517
8518 return true;
8519 }
8520 }
8521 }
8522 return false;
8523}
8524
8525bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8526 bool check_superclass) {
8527 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8528 if (class_interface_decl)
8529 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8530 return false;
8531}
8532
8533clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8534 const CompilerType &type,
8535 const char *name, // the full symbol name as seen in the symbol table
8536 // (lldb::opaque_compiler_type_t type, "-[NString
8537 // stringWithCString:]")
8538 const CompilerType &method_clang_type, lldb::AccessType access,
8539 bool is_artificial, bool is_variadic) {
8540 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008541 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008542
Kate Stoneb9c1b512016-09-06 20:57:50 +00008543 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8544
8545 if (class_interface_decl == nullptr)
8546 return nullptr;
8547 ClangASTContext *lldb_ast =
8548 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8549 if (lldb_ast == nullptr)
8550 return nullptr;
8551 clang::ASTContext *ast = lldb_ast->getASTContext();
8552
8553 const char *selector_start = ::strchr(name, ' ');
8554 if (selector_start == nullptr)
8555 return nullptr;
8556
8557 selector_start++;
8558 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8559
8560 size_t len = 0;
8561 const char *start;
8562 // printf ("name = '%s'\n", name);
8563
8564 unsigned num_selectors_with_args = 0;
8565 for (start = selector_start; start && *start != '\0' && *start != ']';
8566 start += len) {
8567 len = ::strcspn(start, ":]");
8568 bool has_arg = (start[len] == ':');
8569 if (has_arg)
8570 ++num_selectors_with_args;
8571 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8572 if (has_arg)
8573 len += 1;
8574 }
8575
8576 if (selector_idents.size() == 0)
8577 return nullptr;
8578
8579 clang::Selector method_selector = ast->Selectors.getSelector(
8580 num_selectors_with_args ? selector_idents.size() : 0,
8581 selector_idents.data());
8582
8583 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8584
8585 // Populate the method decl with parameter decls
8586 const clang::Type *method_type(method_qual_type.getTypePtr());
8587
8588 if (method_type == nullptr)
8589 return nullptr;
8590
8591 const clang::FunctionProtoType *method_function_prototype(
8592 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8593
8594 if (!method_function_prototype)
8595 return nullptr;
8596
8597 bool is_synthesized = false;
8598 bool is_defined = false;
8599 clang::ObjCMethodDecl::ImplementationControl imp_control =
8600 clang::ObjCMethodDecl::None;
8601
8602 const unsigned num_args = method_function_prototype->getNumParams();
8603
8604 if (num_args != num_selectors_with_args)
8605 return nullptr; // some debug information is corrupt. We are not going to
8606 // deal with it.
8607
8608 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8609 *ast,
8610 clang::SourceLocation(), // beginLoc,
8611 clang::SourceLocation(), // endLoc,
8612 method_selector, method_function_prototype->getReturnType(),
8613 nullptr, // TypeSourceInfo *ResultTInfo,
8614 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8615 ClangUtil::GetQualType(type)),
8616 name[0] == '-', is_variadic, is_synthesized,
8617 true, // is_implicitly_declared; we force this to true because we don't
8618 // have source locations
8619 is_defined, imp_control, false /*has_related_result_type*/);
8620
8621 if (objc_method_decl == nullptr)
8622 return nullptr;
8623
8624 if (num_args > 0) {
8625 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8626
8627 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8628 params.push_back(clang::ParmVarDecl::Create(
8629 *ast, objc_method_decl, clang::SourceLocation(),
8630 clang::SourceLocation(),
8631 nullptr, // anonymous
8632 method_function_prototype->getParamType(param_index), nullptr,
8633 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008634 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008635
Kate Stoneb9c1b512016-09-06 20:57:50 +00008636 objc_method_decl->setMethodParams(
8637 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8638 llvm::ArrayRef<clang::SourceLocation>());
8639 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008640
Kate Stoneb9c1b512016-09-06 20:57:50 +00008641 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008642
Greg Claytond8d4a572015-08-11 21:38:15 +00008643#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008644 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008645#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008646
8647 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008648}
8649
Kate Stoneb9c1b512016-09-06 20:57:50 +00008650bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8651 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008652 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008653
Kate Stoneb9c1b512016-09-06 20:57:50 +00008654 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008655
Kate Stoneb9c1b512016-09-06 20:57:50 +00008656 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8657 switch (type_class) {
8658 case clang::Type::Record: {
8659 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8660 if (cxx_record_decl)
8661 return cxx_record_decl->hasExternalLexicalStorage() ||
8662 cxx_record_decl->hasExternalVisibleStorage();
8663 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008664
Kate Stoneb9c1b512016-09-06 20:57:50 +00008665 case clang::Type::Enum: {
8666 clang::EnumDecl *enum_decl =
8667 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8668 if (enum_decl)
8669 return enum_decl->hasExternalLexicalStorage() ||
8670 enum_decl->hasExternalVisibleStorage();
8671 } break;
8672
8673 case clang::Type::ObjCObject:
8674 case clang::Type::ObjCInterface: {
8675 const clang::ObjCObjectType *objc_class_type =
8676 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8677 assert(objc_class_type);
8678 if (objc_class_type) {
8679 clang::ObjCInterfaceDecl *class_interface_decl =
8680 objc_class_type->getInterface();
8681
8682 if (class_interface_decl)
8683 return class_interface_decl->hasExternalLexicalStorage() ||
8684 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008685 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008686 } break;
8687
8688 case clang::Type::Typedef:
8689 return GetHasExternalStorage(CompilerType(
8690 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8691 ->getDecl()
8692 ->getUnderlyingType()
8693 .getAsOpaquePtr()));
8694
8695 case clang::Type::Auto:
8696 return GetHasExternalStorage(CompilerType(
8697 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8698 ->getDeducedType()
8699 .getAsOpaquePtr()));
8700
8701 case clang::Type::Elaborated:
8702 return GetHasExternalStorage(CompilerType(
8703 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8704 ->getNamedType()
8705 .getAsOpaquePtr()));
8706
8707 case clang::Type::Paren:
8708 return GetHasExternalStorage(CompilerType(
8709 type.GetTypeSystem(),
8710 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8711
8712 default:
8713 break;
8714 }
8715 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008716}
8717
Kate Stoneb9c1b512016-09-06 20:57:50 +00008718bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8719 bool has_extern) {
8720 if (!type)
8721 return false;
8722
8723 clang::QualType qual_type(GetCanonicalQualType(type));
8724
8725 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8726 switch (type_class) {
8727 case clang::Type::Record: {
8728 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8729 if (cxx_record_decl) {
8730 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8731 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8732 return true;
8733 }
8734 } break;
8735
8736 case clang::Type::Enum: {
8737 clang::EnumDecl *enum_decl =
8738 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8739 if (enum_decl) {
8740 enum_decl->setHasExternalLexicalStorage(has_extern);
8741 enum_decl->setHasExternalVisibleStorage(has_extern);
8742 return true;
8743 }
8744 } break;
8745
8746 case clang::Type::ObjCObject:
8747 case clang::Type::ObjCInterface: {
8748 const clang::ObjCObjectType *objc_class_type =
8749 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8750 assert(objc_class_type);
8751 if (objc_class_type) {
8752 clang::ObjCInterfaceDecl *class_interface_decl =
8753 objc_class_type->getInterface();
8754
8755 if (class_interface_decl) {
8756 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8757 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8758 return true;
8759 }
8760 }
8761 } break;
8762
8763 case clang::Type::Typedef:
8764 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8765 ->getDecl()
8766 ->getUnderlyingType()
8767 .getAsOpaquePtr(),
8768 has_extern);
8769
8770 case clang::Type::Auto:
8771 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8772 ->getDeducedType()
8773 .getAsOpaquePtr(),
8774 has_extern);
8775
8776 case clang::Type::Elaborated:
8777 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8778 ->getNamedType()
8779 .getAsOpaquePtr(),
8780 has_extern);
8781
8782 case clang::Type::Paren:
8783 return SetHasExternalStorage(
8784 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8785 has_extern);
8786
8787 default:
8788 break;
8789 }
8790 return false;
8791}
Greg Claytond8d4a572015-08-11 21:38:15 +00008792
8793#pragma mark TagDecl
8794
Kate Stoneb9c1b512016-09-06 20:57:50 +00008795bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8796 clang::QualType qual_type(ClangUtil::GetQualType(type));
8797 if (!qual_type.isNull()) {
8798 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8799 if (tag_type) {
8800 clang::TagDecl *tag_decl = tag_type->getDecl();
8801 if (tag_decl) {
8802 tag_decl->startDefinition();
8803 return true;
8804 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008805 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008806
8807 const clang::ObjCObjectType *object_type =
8808 qual_type->getAs<clang::ObjCObjectType>();
8809 if (object_type) {
8810 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8811 if (interface_decl) {
8812 interface_decl->startDefinition();
8813 return true;
8814 }
8815 }
8816 }
8817 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008818}
8819
Kate Stoneb9c1b512016-09-06 20:57:50 +00008820bool ClangASTContext::CompleteTagDeclarationDefinition(
8821 const CompilerType &type) {
8822 clang::QualType qual_type(ClangUtil::GetQualType(type));
8823 if (!qual_type.isNull()) {
8824 // Make sure we use the same methodology as
Adrian Prantl05097242018-04-30 16:49:04 +00008825 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end
8826 // the definition. Previously we were calling
Kate Stoneb9c1b512016-09-06 20:57:50 +00008827 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8828 if (tag_type) {
8829 clang::TagDecl *tag_decl = tag_type->getDecl();
8830 if (tag_decl) {
8831 clang::CXXRecordDecl *cxx_record_decl =
8832 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8833
8834 if (cxx_record_decl) {
8835 if (!cxx_record_decl->isCompleteDefinition())
8836 cxx_record_decl->completeDefinition();
8837 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8838 cxx_record_decl->setHasExternalLexicalStorage(false);
8839 cxx_record_decl->setHasExternalVisibleStorage(false);
8840 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008841 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008842 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008843 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008844
8845 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8846
8847 if (enutype) {
8848 clang::EnumDecl *enum_decl = enutype->getDecl();
8849
8850 if (enum_decl) {
8851 if (!enum_decl->isCompleteDefinition()) {
8852 ClangASTContext *lldb_ast =
8853 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8854 if (lldb_ast == nullptr)
8855 return false;
8856 clang::ASTContext *ast = lldb_ast->getASTContext();
8857
8858 /// TODO This really needs to be fixed.
8859
8860 QualType integer_type(enum_decl->getIntegerType());
8861 if (!integer_type.isNull()) {
8862 unsigned NumPositiveBits = 1;
8863 unsigned NumNegativeBits = 0;
8864
8865 clang::QualType promotion_qual_type;
8866 // If the enum integer type is less than an integer in bit width,
8867 // then we must promote it to an integer size.
8868 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8869 ast->getTypeSize(ast->IntTy)) {
8870 if (enum_decl->getIntegerType()->isSignedIntegerType())
8871 promotion_qual_type = ast->IntTy;
8872 else
8873 promotion_qual_type = ast->UnsignedIntTy;
8874 } else
8875 promotion_qual_type = enum_decl->getIntegerType();
8876
8877 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8878 promotion_qual_type, NumPositiveBits,
8879 NumNegativeBits);
8880 }
8881 }
8882 return true;
8883 }
8884 }
8885 }
8886 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008887}
8888
Aleksandr Urakov709426b2018-09-10 08:08:43 +00008889clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008890 const CompilerType &enum_type, const Declaration &decl, const char *name,
8891 int64_t enum_value, uint32_t enum_value_bit_size) {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008892
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008893 if (!enum_type || ConstString(name).IsEmpty())
8894 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008895
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008896 lldbassert(enum_type.GetTypeSystem() == static_cast<TypeSystem *>(this));
Kate Stoneb9c1b512016-09-06 20:57:50 +00008897
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008898 lldb::opaque_compiler_type_t enum_opaque_compiler_type =
8899 enum_type.GetOpaqueQualType();
8900
8901 if (!enum_opaque_compiler_type)
8902 return nullptr;
8903
8904 CompilerType underlying_type =
8905 GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
8906
8907 clang::QualType enum_qual_type(
8908 GetCanonicalQualType(enum_opaque_compiler_type));
8909
8910 bool is_signed = false;
8911 underlying_type.IsIntegerType(is_signed);
8912 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8913
8914 if (!clang_type)
8915 return nullptr;
8916
8917 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8918
8919 if (!enutype)
8920 return nullptr;
8921
8922 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8923 enum_llvm_apsint = enum_value;
8924 clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create(
8925 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8926 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
8927 clang::QualType(enutype, 0), nullptr, enum_llvm_apsint);
8928
8929 if (!enumerator_decl)
8930 return nullptr;
8931
8932 enutype->getDecl()->addDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008933
8934#ifdef LLDB_CONFIGURATION_DEBUG
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008935 VerifyDecl(enumerator_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008936#endif
8937
Shafik Yaghmour8c5ec1f2018-11-08 18:42:00 +00008938 return enumerator_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008939}
8940
Greg Claytona1e5dc82015-08-11 22:53:00 +00008941CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00008942ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8943 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8944 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8945 if (clang_type) {
8946 const clang::EnumType *enutype =
8947 llvm::dyn_cast<clang::EnumType>(clang_type);
8948 if (enutype) {
8949 clang::EnumDecl *enum_decl = enutype->getDecl();
8950 if (enum_decl)
8951 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008952 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008953 }
8954 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008955}
8956
Kate Stoneb9c1b512016-09-06 20:57:50 +00008957CompilerType
8958ClangASTContext::CreateMemberPointerType(const CompilerType &type,
8959 const CompilerType &pointee_type) {
8960 if (type && pointee_type.IsValid() &&
8961 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8962 ClangASTContext *ast =
8963 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8964 if (!ast)
8965 return CompilerType();
8966 return CompilerType(ast->getASTContext(),
8967 ast->getASTContext()->getMemberPointerType(
8968 ClangUtil::GetQualType(pointee_type),
8969 ClangUtil::GetQualType(type).getTypePtr()));
8970 }
8971 return CompilerType();
8972}
Greg Claytond8d4a572015-08-11 21:38:15 +00008973
8974size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00008975ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
8976 const char *s, uint8_t *dst,
8977 size_t dst_size) {
8978 if (type) {
8979 clang::QualType qual_type(GetCanonicalQualType(type));
8980 uint32_t count = 0;
8981 bool is_complex = false;
8982 if (IsFloatingPointType(type, count, is_complex)) {
8983 // TODO: handle complex and vector types
8984 if (count != 1)
8985 return false;
8986
8987 llvm::StringRef s_sref(s);
8988 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
8989 s_sref);
8990
8991 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
8992 const uint64_t byte_size = bit_size / 8;
8993 if (dst_size >= byte_size) {
8994 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
8995 llvm::NextPowerOf2(byte_size) * 8);
Zachary Turner97206d52017-05-12 04:51:55 +00008996 lldb_private::Status get_data_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008997 if (scalar.GetAsMemoryData(dst, byte_size,
8998 lldb_private::endian::InlHostByteOrder(),
8999 get_data_error))
9000 return byte_size;
9001 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009002 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009003 }
9004 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00009005}
9006
Greg Claytond8d4a572015-08-11 21:38:15 +00009007//----------------------------------------------------------------------
9008// Dumping types
9009//----------------------------------------------------------------------
9010#define DEPTH_INCREMENT 2
9011
Zachary Turner49110232018-11-05 17:40:28 +00009012void ClangASTContext::Dump(Stream &s) {
Zachary Turner115209e2018-11-05 19:25:39 +00009013 Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl());
Zachary Turner49110232018-11-05 17:40:28 +00009014 tu->dump(s.AsRawOstream());
9015}
9016
Kate Stoneb9c1b512016-09-06 20:57:50 +00009017void ClangASTContext::DumpValue(
9018 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00009019 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009020 lldb::offset_t data_byte_offset, size_t data_byte_size,
9021 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
9022 bool show_summary, bool verbose, uint32_t depth) {
9023 if (!type)
9024 return;
9025
9026 clang::QualType qual_type(GetQualType(type));
9027 switch (qual_type->getTypeClass()) {
9028 case clang::Type::Record:
9029 if (GetCompleteType(type)) {
9030 const clang::RecordType *record_type =
9031 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9032 const clang::RecordDecl *record_decl = record_type->getDecl();
9033 assert(record_decl);
9034 uint32_t field_bit_offset = 0;
9035 uint32_t field_byte_offset = 0;
9036 const clang::ASTRecordLayout &record_layout =
9037 getASTContext()->getASTRecordLayout(record_decl);
9038 uint32_t child_idx = 0;
9039
9040 const clang::CXXRecordDecl *cxx_record_decl =
9041 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9042 if (cxx_record_decl) {
9043 // We might have base classes to print out first
9044 clang::CXXRecordDecl::base_class_const_iterator base_class,
9045 base_class_end;
9046 for (base_class = cxx_record_decl->bases_begin(),
9047 base_class_end = cxx_record_decl->bases_end();
9048 base_class != base_class_end; ++base_class) {
9049 const clang::CXXRecordDecl *base_class_decl =
9050 llvm::cast<clang::CXXRecordDecl>(
9051 base_class->getType()->getAs<clang::RecordType>()->getDecl());
9052
9053 // Skip empty base classes
9054 if (verbose == false &&
9055 ClangASTContext::RecordHasFields(base_class_decl) == false)
9056 continue;
9057
9058 if (base_class->isVirtual())
9059 field_bit_offset =
9060 record_layout.getVBaseClassOffset(base_class_decl)
9061 .getQuantity() *
9062 8;
9063 else
9064 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
9065 .getQuantity() *
9066 8;
9067 field_byte_offset = field_bit_offset / 8;
9068 assert(field_bit_offset % 8 == 0);
9069 if (child_idx == 0)
9070 s->PutChar('{');
9071 else
9072 s->PutChar(',');
9073
9074 clang::QualType base_class_qual_type = base_class->getType();
9075 std::string base_class_type_name(base_class_qual_type.getAsString());
9076
9077 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00009078 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
9079 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009080
9081 clang::TypeInfo base_class_type_info =
9082 getASTContext()->getTypeInfo(base_class_qual_type);
9083
9084 // Dump the value of the member
9085 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
9086 base_clang_type.DumpValue(
9087 exe_ctx,
9088 s, // Stream to dump to
9089 base_clang_type
9090 .GetFormat(), // The format with which to display the member
9091 data, // Data buffer containing all bytes for this type
9092 data_byte_offset + field_byte_offset, // Offset into "data" where
9093 // to grab value from
9094 base_class_type_info.Width / 8, // Size of this type in bytes
9095 0, // Bitfield bit size
9096 0, // Bitfield bit offset
9097 show_types, // Boolean indicating if we should show the variable
9098 // types
9099 show_summary, // Boolean indicating if we should show a summary
9100 // for the current type
9101 verbose, // Verbose output?
9102 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9103 // children
9104
9105 ++child_idx;
9106 }
9107 }
9108 uint32_t field_idx = 0;
9109 clang::RecordDecl::field_iterator field, field_end;
9110 for (field = record_decl->field_begin(),
9111 field_end = record_decl->field_end();
9112 field != field_end; ++field, ++field_idx, ++child_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009113 // Print the starting squiggly bracket (if this is the first member) or
9114 // comma (for member 2 and beyond) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009115 if (child_idx == 0)
9116 s->PutChar('{');
9117 else
9118 s->PutChar(',');
9119
9120 // Indent
9121 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
9122
9123 clang::QualType field_type = field->getType();
9124 // Print the member type if requested
Adrian Prantl05097242018-04-30 16:49:04 +00009125 // Figure out the type byte size (field_type_info.first) and alignment
9126 // (field_type_info.second) from the AST context.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009127 clang::TypeInfo field_type_info =
9128 getASTContext()->getTypeInfo(field_type);
9129 assert(field_idx < record_layout.getFieldCount());
9130 // Figure out the field offset within the current struct/union/class
9131 // type
9132 field_bit_offset = record_layout.getFieldOffset(field_idx);
9133 field_byte_offset = field_bit_offset / 8;
9134 uint32_t field_bitfield_bit_size = 0;
9135 uint32_t field_bitfield_bit_offset = 0;
9136 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
9137 field_bitfield_bit_size))
9138 field_bitfield_bit_offset = field_bit_offset % 8;
9139
9140 if (show_types) {
9141 std::string field_type_name(field_type.getAsString());
9142 if (field_bitfield_bit_size > 0)
9143 s->Printf("(%s:%u) ", field_type_name.c_str(),
9144 field_bitfield_bit_size);
9145 else
9146 s->Printf("(%s) ", field_type_name.c_str());
9147 }
9148 // Print the member name and equal sign
9149 s->Printf("%s = ", field->getNameAsString().c_str());
9150
9151 // Dump the value of the member
9152 CompilerType field_clang_type(getASTContext(), field_type);
9153 field_clang_type.DumpValue(
9154 exe_ctx,
9155 s, // Stream to dump to
9156 field_clang_type
9157 .GetFormat(), // The format with which to display the member
9158 data, // Data buffer containing all bytes for this type
9159 data_byte_offset + field_byte_offset, // Offset into "data" where to
9160 // grab value from
9161 field_type_info.Width / 8, // Size of this type in bytes
9162 field_bitfield_bit_size, // Bitfield bit size
9163 field_bitfield_bit_offset, // Bitfield bit offset
9164 show_types, // Boolean indicating if we should show the variable
9165 // types
9166 show_summary, // Boolean indicating if we should show a summary for
9167 // the current type
9168 verbose, // Verbose output?
9169 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9170 // children
9171 }
9172
9173 // Indent the trailing squiggly bracket
9174 if (child_idx > 0)
9175 s->Printf("\n%*s}", depth, "");
9176 }
9177 return;
9178
9179 case clang::Type::Enum:
9180 if (GetCompleteType(type)) {
9181 const clang::EnumType *enutype =
9182 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9183 const clang::EnumDecl *enum_decl = enutype->getDecl();
9184 assert(enum_decl);
9185 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9186 lldb::offset_t offset = data_byte_offset;
9187 const int64_t enum_value = data.GetMaxU64Bitfield(
9188 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9189 for (enum_pos = enum_decl->enumerator_begin(),
9190 enum_end_pos = enum_decl->enumerator_end();
9191 enum_pos != enum_end_pos; ++enum_pos) {
9192 if (enum_pos->getInitVal() == enum_value) {
9193 s->Printf("%s", enum_pos->getNameAsString().c_str());
9194 return;
9195 }
9196 }
Adrian Prantl05097242018-04-30 16:49:04 +00009197 // If we have gotten here we didn't get find the enumerator in the enum
9198 // decl, so just print the integer.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009199 s->Printf("%" PRIi64, enum_value);
9200 }
9201 return;
9202
9203 case clang::Type::ConstantArray: {
9204 const clang::ConstantArrayType *array =
9205 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9206 bool is_array_of_characters = false;
9207 clang::QualType element_qual_type = array->getElementType();
9208
9209 const clang::Type *canonical_type =
9210 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9211 if (canonical_type)
9212 is_array_of_characters = canonical_type->isCharType();
9213
9214 const uint64_t element_count = array->getSize().getLimitedValue();
9215
9216 clang::TypeInfo field_type_info =
9217 getASTContext()->getTypeInfo(element_qual_type);
9218
9219 uint32_t element_idx = 0;
9220 uint32_t element_offset = 0;
9221 uint64_t element_byte_size = field_type_info.Width / 8;
9222 uint32_t element_stride = element_byte_size;
9223
9224 if (is_array_of_characters) {
9225 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009226 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9227 element_byte_size, element_count, UINT32_MAX,
9228 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009229 s->PutChar('"');
9230 return;
9231 } else {
9232 CompilerType element_clang_type(getASTContext(), element_qual_type);
9233 lldb::Format element_format = element_clang_type.GetFormat();
9234
9235 for (element_idx = 0; element_idx < element_count; ++element_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009236 // Print the starting squiggly bracket (if this is the first member) or
9237 // comman (for member 2 and beyong) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009238 if (element_idx == 0)
9239 s->PutChar('{');
9240 else
9241 s->PutChar(',');
9242
9243 // Indent and print the index
9244 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9245
9246 // Figure out the field offset within the current struct/union/class
9247 // type
9248 element_offset = element_idx * element_stride;
9249
9250 // Dump the value of the member
9251 element_clang_type.DumpValue(
9252 exe_ctx,
9253 s, // Stream to dump to
9254 element_format, // The format with which to display the element
9255 data, // Data buffer containing all bytes for this type
9256 data_byte_offset +
9257 element_offset, // Offset into "data" where to grab value from
9258 element_byte_size, // Size of this type in bytes
9259 0, // Bitfield bit size
9260 0, // Bitfield bit offset
9261 show_types, // Boolean indicating if we should show the variable
9262 // types
9263 show_summary, // Boolean indicating if we should show a summary for
9264 // the current type
9265 verbose, // Verbose output?
9266 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9267 // children
9268 }
9269
9270 // Indent the trailing squiggly bracket
9271 if (element_idx > 0)
9272 s->Printf("\n%*s}", depth, "");
9273 }
9274 }
9275 return;
9276
9277 case clang::Type::Typedef: {
9278 clang::QualType typedef_qual_type =
9279 llvm::cast<clang::TypedefType>(qual_type)
9280 ->getDecl()
9281 ->getUnderlyingType();
9282
9283 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9284 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9285 clang::TypeInfo typedef_type_info =
9286 getASTContext()->getTypeInfo(typedef_qual_type);
9287 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9288
9289 return typedef_clang_type.DumpValue(
9290 exe_ctx,
9291 s, // Stream to dump to
9292 typedef_format, // The format with which to display the element
9293 data, // Data buffer containing all bytes for this type
9294 data_byte_offset, // Offset into "data" where to grab value from
9295 typedef_byte_size, // Size of this type in bytes
9296 bitfield_bit_size, // Bitfield bit size
9297 bitfield_bit_offset, // Bitfield bit offset
9298 show_types, // Boolean indicating if we should show the variable types
9299 show_summary, // Boolean indicating if we should show a summary for the
9300 // current type
9301 verbose, // Verbose output?
9302 depth); // Scope depth for any types that have children
9303 } break;
9304
9305 case clang::Type::Auto: {
9306 clang::QualType elaborated_qual_type =
9307 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9308 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9309 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9310 clang::TypeInfo elaborated_type_info =
9311 getASTContext()->getTypeInfo(elaborated_qual_type);
9312 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9313
9314 return elaborated_clang_type.DumpValue(
9315 exe_ctx,
9316 s, // Stream to dump to
9317 elaborated_format, // The format with which to display the element
9318 data, // Data buffer containing all bytes for this type
9319 data_byte_offset, // Offset into "data" where to grab value from
9320 elaborated_byte_size, // Size of this type in bytes
9321 bitfield_bit_size, // Bitfield bit size
9322 bitfield_bit_offset, // Bitfield bit offset
9323 show_types, // Boolean indicating if we should show the variable types
9324 show_summary, // Boolean indicating if we should show a summary for the
9325 // current type
9326 verbose, // Verbose output?
9327 depth); // Scope depth for any types that have children
9328 } break;
9329
9330 case clang::Type::Elaborated: {
9331 clang::QualType elaborated_qual_type =
9332 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9333 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9334 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9335 clang::TypeInfo elaborated_type_info =
9336 getASTContext()->getTypeInfo(elaborated_qual_type);
9337 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9338
9339 return elaborated_clang_type.DumpValue(
9340 exe_ctx,
9341 s, // Stream to dump to
9342 elaborated_format, // The format with which to display the element
9343 data, // Data buffer containing all bytes for this type
9344 data_byte_offset, // Offset into "data" where to grab value from
9345 elaborated_byte_size, // Size of this type in bytes
9346 bitfield_bit_size, // Bitfield bit size
9347 bitfield_bit_offset, // Bitfield bit offset
9348 show_types, // Boolean indicating if we should show the variable types
9349 show_summary, // Boolean indicating if we should show a summary for the
9350 // current type
9351 verbose, // Verbose output?
9352 depth); // Scope depth for any types that have children
9353 } break;
9354
9355 case clang::Type::Paren: {
9356 clang::QualType desugar_qual_type =
9357 llvm::cast<clang::ParenType>(qual_type)->desugar();
9358 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9359
9360 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9361 clang::TypeInfo desugar_type_info =
9362 getASTContext()->getTypeInfo(desugar_qual_type);
9363 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9364
9365 return desugar_clang_type.DumpValue(
9366 exe_ctx,
9367 s, // Stream to dump to
9368 desugar_format, // The format with which to display the element
9369 data, // Data buffer containing all bytes for this type
9370 data_byte_offset, // Offset into "data" where to grab value from
9371 desugar_byte_size, // Size of this type in bytes
9372 bitfield_bit_size, // Bitfield bit size
9373 bitfield_bit_offset, // Bitfield bit offset
9374 show_types, // Boolean indicating if we should show the variable types
9375 show_summary, // Boolean indicating if we should show a summary for the
9376 // current type
9377 verbose, // Verbose output?
9378 depth); // Scope depth for any types that have children
9379 } break;
9380
9381 default:
9382 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009383 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9384 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9385 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009386
9387 if (show_summary)
9388 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9389 break;
9390 }
9391}
9392
9393bool ClangASTContext::DumpTypeValue(
9394 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009395 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9396 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009397 ExecutionContextScope *exe_scope) {
9398 if (!type)
9399 return false;
9400 if (IsAggregateType(type)) {
9401 return false;
9402 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009403 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009404
9405 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9406 switch (type_class) {
9407 case clang::Type::Typedef: {
9408 clang::QualType typedef_qual_type =
9409 llvm::cast<clang::TypedefType>(qual_type)
9410 ->getDecl()
9411 ->getUnderlyingType();
9412 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9413 if (format == eFormatDefault)
9414 format = typedef_clang_type.GetFormat();
9415 clang::TypeInfo typedef_type_info =
9416 getASTContext()->getTypeInfo(typedef_qual_type);
9417 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9418
9419 return typedef_clang_type.DumpTypeValue(
9420 s,
9421 format, // The format with which to display the element
9422 data, // Data buffer containing all bytes for this type
9423 byte_offset, // Offset into "data" where to grab value from
9424 typedef_byte_size, // Size of this type in bytes
9425 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9426 // treat as a bitfield
9427 bitfield_bit_offset, // Offset in bits of a bitfield value if
9428 // bitfield_bit_size != 0
9429 exe_scope);
9430 } break;
9431
9432 case clang::Type::Enum:
Adrian Prantl05097242018-04-30 16:49:04 +00009433 // If our format is enum or default, show the enumeration value as its
9434 // enumeration string value, else just display it as requested.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009435 if ((format == eFormatEnum || format == eFormatDefault) &&
9436 GetCompleteType(type)) {
9437 const clang::EnumType *enutype =
9438 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9439 const clang::EnumDecl *enum_decl = enutype->getDecl();
9440 assert(enum_decl);
9441 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9442 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9443 lldb::offset_t offset = byte_offset;
9444 if (is_signed) {
9445 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9446 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9447 for (enum_pos = enum_decl->enumerator_begin(),
9448 enum_end_pos = enum_decl->enumerator_end();
9449 enum_pos != enum_end_pos; ++enum_pos) {
9450 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009451 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009452 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009453 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009454 }
9455 // If we have gotten here we didn't get find the enumerator in the
9456 // enum decl, so just print the integer.
9457 s->Printf("%" PRIi64, enum_svalue);
9458 } else {
9459 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9460 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9461 for (enum_pos = enum_decl->enumerator_begin(),
9462 enum_end_pos = enum_decl->enumerator_end();
9463 enum_pos != enum_end_pos; ++enum_pos) {
9464 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009465 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009466 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009467 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009468 }
9469 // If we have gotten here we didn't get find the enumerator in the
9470 // enum decl, so just print the integer.
9471 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009472 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009473 return true;
9474 }
9475 // format was not enum, just fall through and dump the value as
9476 // requested....
9477 LLVM_FALLTHROUGH;
9478
9479 default:
9480 // We are down to a scalar type that we just need to display.
9481 {
9482 uint32_t item_count = 1;
9483 // A few formats, we might need to modify our size and count for
9484 // depending
9485 // on how we are trying to display the value...
9486 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009487 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009488 case eFormatBoolean:
9489 case eFormatBinary:
9490 case eFormatComplex:
9491 case eFormatCString: // NULL terminated C strings
9492 case eFormatDecimal:
9493 case eFormatEnum:
9494 case eFormatHex:
9495 case eFormatHexUppercase:
9496 case eFormatFloat:
9497 case eFormatOctal:
9498 case eFormatOSType:
9499 case eFormatUnsigned:
9500 case eFormatPointer:
9501 case eFormatVectorOfChar:
9502 case eFormatVectorOfSInt8:
9503 case eFormatVectorOfUInt8:
9504 case eFormatVectorOfSInt16:
9505 case eFormatVectorOfUInt16:
9506 case eFormatVectorOfSInt32:
9507 case eFormatVectorOfUInt32:
9508 case eFormatVectorOfSInt64:
9509 case eFormatVectorOfUInt64:
9510 case eFormatVectorOfFloat32:
9511 case eFormatVectorOfFloat64:
9512 case eFormatVectorOfUInt128:
9513 break;
9514
9515 case eFormatChar:
9516 case eFormatCharPrintable:
9517 case eFormatCharArray:
9518 case eFormatBytes:
9519 case eFormatBytesWithASCII:
9520 item_count = byte_size;
9521 byte_size = 1;
9522 break;
9523
9524 case eFormatUnicode16:
9525 item_count = byte_size / 2;
9526 byte_size = 2;
9527 break;
9528
9529 case eFormatUnicode32:
9530 item_count = byte_size / 4;
9531 byte_size = 4;
9532 break;
9533 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009534 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9535 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9536 bitfield_bit_size, bitfield_bit_offset,
9537 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009538 }
9539 break;
9540 }
9541 }
9542 return 0;
9543}
9544
9545void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9546 ExecutionContext *exe_ctx, Stream *s,
9547 const lldb_private::DataExtractor &data,
9548 lldb::offset_t data_byte_offset,
9549 size_t data_byte_size) {
9550 uint32_t length = 0;
9551 if (IsCStringType(type, length)) {
9552 if (exe_ctx) {
9553 Process *process = exe_ctx->GetProcessPtr();
9554 if (process) {
9555 lldb::offset_t offset = data_byte_offset;
9556 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9557 std::vector<uint8_t> buf;
9558 if (length > 0)
9559 buf.resize(length);
9560 else
9561 buf.resize(256);
9562
Zachary Turner29cb8682017-03-03 20:57:05 +00009563 DataExtractor cstr_data(&buf.front(), buf.size(),
9564 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009565 buf.back() = '\0';
9566 size_t bytes_read;
9567 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009568 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009569 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9570 buf.size(), error)) > 0) {
9571 const size_t len = strlen((const char *)&buf.front());
9572 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009573 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009574 if (total_cstr_len == 0)
9575 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009576 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9577 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009578 total_cstr_len += len;
9579 if (len < buf.size())
9580 break;
9581 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009582 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009583 if (total_cstr_len > 0)
9584 s->PutChar('"');
9585 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009586 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009587 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009588}
9589
Kate Stoneb9c1b512016-09-06 20:57:50 +00009590void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9591 StreamFile s(stdout, false);
9592 DumpTypeDescription(type, &s);
9593 ClangASTMetadata *metadata =
9594 ClangASTContext::GetMetadata(getASTContext(), type);
9595 if (metadata) {
9596 metadata->Dump(&s);
9597 }
9598}
Greg Claytond8d4a572015-08-11 21:38:15 +00009599
Kate Stoneb9c1b512016-09-06 20:57:50 +00009600void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9601 Stream *s) {
9602 if (type) {
9603 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009604
Kate Stoneb9c1b512016-09-06 20:57:50 +00009605 llvm::SmallVector<char, 1024> buf;
9606 llvm::raw_svector_ostream llvm_ostrm(buf);
9607
9608 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9609 switch (type_class) {
9610 case clang::Type::ObjCObject:
9611 case clang::Type::ObjCInterface: {
9612 GetCompleteType(type);
9613
9614 const clang::ObjCObjectType *objc_class_type =
9615 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9616 assert(objc_class_type);
9617 if (objc_class_type) {
9618 clang::ObjCInterfaceDecl *class_interface_decl =
9619 objc_class_type->getInterface();
9620 if (class_interface_decl) {
9621 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9622 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009623 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009624 }
9625 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009626
Kate Stoneb9c1b512016-09-06 20:57:50 +00009627 case clang::Type::Typedef: {
9628 const clang::TypedefType *typedef_type =
9629 qual_type->getAs<clang::TypedefType>();
9630 if (typedef_type) {
9631 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9632 std::string clang_typedef_name(
9633 typedef_decl->getQualifiedNameAsString());
9634 if (!clang_typedef_name.empty()) {
9635 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009636 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009637 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009638 }
9639 } break;
9640
9641 case clang::Type::Auto:
9642 CompilerType(getASTContext(),
9643 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9644 .DumpTypeDescription(s);
9645 return;
9646
9647 case clang::Type::Elaborated:
9648 CompilerType(getASTContext(),
9649 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9650 .DumpTypeDescription(s);
9651 return;
9652
9653 case clang::Type::Paren:
9654 CompilerType(getASTContext(),
9655 llvm::cast<clang::ParenType>(qual_type)->desugar())
9656 .DumpTypeDescription(s);
9657 return;
9658
9659 case clang::Type::Record: {
9660 GetCompleteType(type);
9661
9662 const clang::RecordType *record_type =
9663 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9664 const clang::RecordDecl *record_decl = record_type->getDecl();
9665 const clang::CXXRecordDecl *cxx_record_decl =
9666 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9667
9668 if (cxx_record_decl)
9669 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9670 s->GetIndentLevel());
9671 else
9672 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9673 s->GetIndentLevel());
9674 } break;
9675
9676 default: {
9677 const clang::TagType *tag_type =
9678 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9679 if (tag_type) {
9680 clang::TagDecl *tag_decl = tag_type->getDecl();
9681 if (tag_decl)
9682 tag_decl->print(llvm_ostrm, 0);
9683 } else {
9684 std::string clang_type_name(qual_type.getAsString());
9685 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009686 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009687 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009688 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009689 }
9690
Kate Stoneb9c1b512016-09-06 20:57:50 +00009691 if (buf.size() > 0) {
9692 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009693 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009694 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009695}
9696
Kate Stoneb9c1b512016-09-06 20:57:50 +00009697void ClangASTContext::DumpTypeName(const CompilerType &type) {
9698 if (ClangUtil::IsClangType(type)) {
9699 clang::QualType qual_type(
9700 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9701
9702 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9703 switch (type_class) {
9704 case clang::Type::Record: {
9705 const clang::CXXRecordDecl *cxx_record_decl =
9706 qual_type->getAsCXXRecordDecl();
9707 if (cxx_record_decl)
9708 printf("class %s", cxx_record_decl->getName().str().c_str());
9709 } break;
9710
9711 case clang::Type::Enum: {
9712 clang::EnumDecl *enum_decl =
9713 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9714 if (enum_decl) {
9715 printf("enum %s", enum_decl->getName().str().c_str());
9716 }
9717 } break;
9718
9719 case clang::Type::ObjCObject:
9720 case clang::Type::ObjCInterface: {
9721 const clang::ObjCObjectType *objc_class_type =
9722 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9723 if (objc_class_type) {
9724 clang::ObjCInterfaceDecl *class_interface_decl =
9725 objc_class_type->getInterface();
Adrian Prantl05097242018-04-30 16:49:04 +00009726 // We currently can't complete objective C types through the newly
9727 // added ASTContext because it only supports TagDecl objects right
9728 // now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00009729 if (class_interface_decl)
9730 printf("@class %s", class_interface_decl->getName().str().c_str());
9731 }
9732 } break;
9733
9734 case clang::Type::Typedef:
9735 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9736 ->getDecl()
9737 ->getName()
9738 .str()
9739 .c_str());
9740 break;
9741
9742 case clang::Type::Auto:
9743 printf("auto ");
9744 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9745 llvm::cast<clang::AutoType>(qual_type)
9746 ->getDeducedType()
9747 .getAsOpaquePtr()));
9748
9749 case clang::Type::Elaborated:
9750 printf("elaborated ");
9751 return DumpTypeName(CompilerType(
9752 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9753 ->getNamedType()
9754 .getAsOpaquePtr()));
9755
9756 case clang::Type::Paren:
9757 printf("paren ");
9758 return DumpTypeName(CompilerType(
9759 type.GetTypeSystem(),
9760 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9761
9762 default:
9763 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9764 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009765 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009766 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009767}
9768
Kate Stoneb9c1b512016-09-06 20:57:50 +00009769clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9770 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9771 const char *parent_name, int tag_decl_kind,
9772 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9773 if (template_param_infos.IsValid()) {
9774 std::string template_basename(parent_name);
9775 template_basename.erase(template_basename.find('<'));
9776
9777 return CreateClassTemplateDecl(decl_ctx, access_type,
9778 template_basename.c_str(), tag_decl_kind,
9779 template_param_infos);
9780 }
9781 return NULL;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009782}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009783
Kate Stoneb9c1b512016-09-06 20:57:50 +00009784void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9785 ClangASTContext *ast = (ClangASTContext *)baton;
9786 SymbolFile *sym_file = ast->GetSymbolFile();
9787 if (sym_file) {
9788 CompilerType clang_type = GetTypeForDecl(decl);
9789 if (clang_type)
9790 sym_file->CompleteType(clang_type);
9791 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009792}
9793
Kate Stoneb9c1b512016-09-06 20:57:50 +00009794void ClangASTContext::CompleteObjCInterfaceDecl(
9795 void *baton, clang::ObjCInterfaceDecl *decl) {
9796 ClangASTContext *ast = (ClangASTContext *)baton;
9797 SymbolFile *sym_file = ast->GetSymbolFile();
9798 if (sym_file) {
9799 CompilerType clang_type = GetTypeForDecl(decl);
9800 if (clang_type)
9801 sym_file->CompleteType(clang_type);
9802 }
Zachary Turner42dff792016-04-15 00:21:26 +00009803}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009804
Kate Stoneb9c1b512016-09-06 20:57:50 +00009805DWARFASTParser *ClangASTContext::GetDWARFParser() {
9806 if (!m_dwarf_ast_parser_ap)
9807 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9808 return m_dwarf_ast_parser_ap.get();
9809}
9810
9811PDBASTParser *ClangASTContext::GetPDBParser() {
9812 if (!m_pdb_ast_parser_ap)
9813 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9814 return m_pdb_ast_parser_ap.get();
9815}
9816
9817bool ClangASTContext::LayoutRecordType(
9818 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9819 uint64_t &alignment,
9820 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9821 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9822 &base_offsets,
9823 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9824 &vbase_offsets) {
9825 ClangASTContext *ast = (ClangASTContext *)baton;
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009826 lldb_private::ClangASTImporter *importer = nullptr;
9827 if (ast->m_dwarf_ast_parser_ap)
9828 importer = &ast->m_dwarf_ast_parser_ap->GetClangASTImporter();
9829 if (!importer && ast->m_pdb_ast_parser_ap)
9830 importer = &ast->m_pdb_ast_parser_ap->GetClangASTImporter();
9831 if (!importer)
9832 return false;
9833
9834 return importer->LayoutRecordType(record_decl, bit_size, alignment,
9835 field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009836}
9837
Greg Clayton99558cc42015-08-24 23:46:31 +00009838//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009839// CompilerDecl override functions
9840//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009841
Kate Stoneb9c1b512016-09-06 20:57:50 +00009842ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9843 if (opaque_decl) {
9844 clang::NamedDecl *nd =
9845 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9846 if (nd != nullptr)
9847 return ConstString(nd->getDeclName().getAsString());
9848 }
9849 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009850}
9851
Kate Stoneb9c1b512016-09-06 20:57:50 +00009852ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9853 if (opaque_decl) {
9854 clang::NamedDecl *nd =
9855 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9856 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9857 clang::MangleContext *mc = getMangleContext();
9858 if (mc && mc->shouldMangleCXXName(nd)) {
9859 llvm::SmallVector<char, 1024> buf;
9860 llvm::raw_svector_ostream llvm_ostrm(buf);
9861 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9862 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9863 Ctor_Complete, llvm_ostrm);
9864 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9865 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9866 Dtor_Complete, llvm_ostrm);
9867 } else {
9868 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009869 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009870 if (buf.size() > 0)
9871 return ConstString(buf.data(), buf.size());
9872 }
Greg Claytonfe689042015-11-10 17:47:04 +00009873 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009874 }
9875 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009876}
9877
Kate Stoneb9c1b512016-09-06 20:57:50 +00009878CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9879 if (opaque_decl)
9880 return CompilerDeclContext(this,
9881 ((clang::Decl *)opaque_decl)->getDeclContext());
9882 else
9883 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009884}
9885
Kate Stoneb9c1b512016-09-06 20:57:50 +00009886CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9887 if (clang::FunctionDecl *func_decl =
9888 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9889 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9890 if (clang::ObjCMethodDecl *objc_method =
9891 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9892 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9893 else
Greg Claytonfe689042015-11-10 17:47:04 +00009894 return CompilerType();
9895}
9896
Kate Stoneb9c1b512016-09-06 20:57:50 +00009897size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9898 if (clang::FunctionDecl *func_decl =
9899 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9900 return func_decl->param_size();
9901 if (clang::ObjCMethodDecl *objc_method =
9902 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9903 return objc_method->param_size();
9904 else
9905 return 0;
9906}
9907
9908CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9909 size_t idx) {
9910 if (clang::FunctionDecl *func_decl =
9911 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9912 if (idx < func_decl->param_size()) {
9913 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9914 if (var_decl)
9915 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9916 }
9917 } else if (clang::ObjCMethodDecl *objc_method =
9918 llvm::dyn_cast<clang::ObjCMethodDecl>(
9919 (clang::Decl *)opaque_decl)) {
9920 if (idx < objc_method->param_size())
9921 return CompilerType(
9922 this,
9923 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9924 }
9925 return CompilerType();
9926}
9927
Paul Hermand628cbb2015-09-15 23:44:17 +00009928//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00009929// CompilerDeclContext functions
9930//----------------------------------------------------------------------
9931
Kate Stoneb9c1b512016-09-06 20:57:50 +00009932std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9933 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9934 std::vector<CompilerDecl> found_decls;
9935 if (opaque_decl_ctx) {
9936 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9937 std::set<DeclContext *> searched;
9938 std::multimap<DeclContext *, DeclContext *> search_queue;
9939 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009940
Kate Stoneb9c1b512016-09-06 20:57:50 +00009941 for (clang::DeclContext *decl_context = root_decl_ctx;
9942 decl_context != nullptr && found_decls.empty();
9943 decl_context = decl_context->getParent()) {
9944 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +00009945
Kate Stoneb9c1b512016-09-06 20:57:50 +00009946 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9947 it++) {
9948 if (!searched.insert(it->second).second)
9949 continue;
9950 symbol_file->ParseDeclsForContext(
9951 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +00009952
Kate Stoneb9c1b512016-09-06 20:57:50 +00009953 for (clang::Decl *child : it->second->decls()) {
9954 if (clang::UsingDirectiveDecl *ud =
9955 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9956 if (ignore_using_decls)
9957 continue;
9958 clang::DeclContext *from = ud->getCommonAncestor();
9959 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9960 search_queue.insert(
9961 std::make_pair(from, ud->getNominatedNamespace()));
9962 } else if (clang::UsingDecl *ud =
9963 llvm::dyn_cast<clang::UsingDecl>(child)) {
9964 if (ignore_using_decls)
9965 continue;
9966 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9967 clang::Decl *target = usd->getTargetDecl();
9968 if (clang::NamedDecl *nd =
9969 llvm::dyn_cast<clang::NamedDecl>(target)) {
9970 IdentifierInfo *ii = nd->getIdentifier();
9971 if (ii != nullptr &&
9972 ii->getName().equals(name.AsCString(nullptr)))
9973 found_decls.push_back(CompilerDecl(this, nd));
9974 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009975 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009976 } else if (clang::NamedDecl *nd =
9977 llvm::dyn_cast<clang::NamedDecl>(child)) {
9978 IdentifierInfo *ii = nd->getIdentifier();
9979 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9980 found_decls.push_back(CompilerDecl(this, nd));
9981 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009982 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009983 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009984 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009985 }
9986 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +00009987}
9988
Dawn Perchikb5925782015-12-12 19:31:41 +00009989// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009990// and return the number of levels it took to find it, or
Adrian Prantl05097242018-04-30 16:49:04 +00009991// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
9992// declaration, its name and/or type, if set, will be used to check that the
9993// decl found in the scope is a match.
Dawn Perchikb5925782015-12-12 19:31:41 +00009994//
Kate Stoneb9c1b512016-09-06 20:57:50 +00009995// The optional name is required by languages (like C++) to handle using
Adrian Prantl05097242018-04-30 16:49:04 +00009996// declarations like:
Dawn Perchikb5925782015-12-12 19:31:41 +00009997//
9998// void poo();
9999// namespace ns {
10000// void foo();
10001// void goo();
10002// }
10003// void bar() {
10004// using ns::foo;
10005// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
10006// // LLDB_INVALID_DECL_LEVEL for 'goo'.
10007// }
10008//
10009// The optional type is useful in the case that there's a specific overload
10010// that we're looking for that might otherwise be shadowed, like:
10011//
10012// void foo(int);
10013// namespace ns {
10014// void foo();
10015// }
10016// void bar() {
10017// using ns::foo;
10018// // CountDeclLevels returns 0 for { 'foo', void() },
10019// // 1 for { 'foo', void(int) }, and
10020// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
10021// }
10022//
10023// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +000010024// function at file scope will return the same level as a function at global
Adrian Prantl05097242018-04-30 16:49:04 +000010025// scope. Ideally we'd like to treat the file scope as an additional scope just
10026// below the global scope. More work needs to be done to recognise that, if
10027// the decl we're trying to look up is static, we should compare its source
10028// file with that of the current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010029uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
10030 clang::DeclContext *child_decl_ctx,
10031 ConstString *child_name,
10032 CompilerType *child_type) {
10033 if (frame_decl_ctx) {
10034 std::set<DeclContext *> searched;
10035 std::multimap<DeclContext *, DeclContext *> search_queue;
10036 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +000010037
Kate Stoneb9c1b512016-09-06 20:57:50 +000010038 // Get the lookup scope for the decl we're trying to find.
10039 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +000010040
Kate Stoneb9c1b512016-09-06 20:57:50 +000010041 // Look for it in our scope's decl context and its parents.
10042 uint32_t level = 0;
10043 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
10044 decl_ctx = decl_ctx->getParent()) {
10045 if (!decl_ctx->isLookupContext())
10046 continue;
10047 if (decl_ctx == parent_decl_ctx)
10048 // Found it!
10049 return level;
10050 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
10051 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
10052 it++) {
10053 if (searched.find(it->second) != searched.end())
10054 continue;
10055
10056 // Currently DWARF has one shared translation unit for all Decls at top
Adrian Prantl05097242018-04-30 16:49:04 +000010057 // level, so this would erroneously find using statements anywhere. So
10058 // don't look at the top-level translation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010059 // TODO fix this and add a testcase that depends on it.
10060
10061 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
10062 continue;
10063
10064 searched.insert(it->second);
10065 symbol_file->ParseDeclsForContext(
10066 CompilerDeclContext(this, it->second));
10067
10068 for (clang::Decl *child : it->second->decls()) {
10069 if (clang::UsingDirectiveDecl *ud =
10070 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10071 clang::DeclContext *ns = ud->getNominatedNamespace();
10072 if (ns == parent_decl_ctx)
10073 // Found it!
10074 return level;
10075 clang::DeclContext *from = ud->getCommonAncestor();
10076 if (searched.find(ns) == searched.end())
10077 search_queue.insert(std::make_pair(from, ns));
10078 } else if (child_name) {
10079 if (clang::UsingDecl *ud =
10080 llvm::dyn_cast<clang::UsingDecl>(child)) {
10081 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10082 clang::Decl *target = usd->getTargetDecl();
10083 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
10084 if (!nd)
10085 continue;
10086 // Check names.
10087 IdentifierInfo *ii = nd->getIdentifier();
10088 if (ii == nullptr ||
10089 !ii->getName().equals(child_name->AsCString(nullptr)))
10090 continue;
10091 // Check types, if one was provided.
10092 if (child_type) {
10093 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
10094 if (!AreTypesSame(clang_type, *child_type,
10095 /*ignore_qualifiers=*/true))
10096 continue;
10097 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010098 // Found it!
10099 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010100 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010101 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010102 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010103 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010104 }
10105 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +000010106 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010107 }
10108 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +000010109}
10110
Kate Stoneb9c1b512016-09-06 20:57:50 +000010111bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
10112 if (opaque_decl_ctx)
10113 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
10114 else
Greg Clayton99558cc42015-08-24 23:46:31 +000010115 return false;
10116}
10117
Kate Stoneb9c1b512016-09-06 20:57:50 +000010118ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
10119 if (opaque_decl_ctx) {
10120 clang::NamedDecl *named_decl =
10121 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10122 if (named_decl)
10123 return ConstString(named_decl->getName());
10124 }
10125 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +000010126}
10127
Kate Stoneb9c1b512016-09-06 20:57:50 +000010128ConstString
10129ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
10130 if (opaque_decl_ctx) {
10131 clang::NamedDecl *named_decl =
10132 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10133 if (named_decl)
10134 return ConstString(
10135 llvm::StringRef(named_decl->getQualifiedNameAsString()));
10136 }
10137 return ConstString();
10138}
10139
10140bool ClangASTContext::DeclContextIsClassMethod(
10141 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
10142 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
10143 if (opaque_decl_ctx) {
10144 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10145 if (ObjCMethodDecl *objc_method =
10146 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
10147 if (is_instance_method_ptr)
10148 *is_instance_method_ptr = objc_method->isInstanceMethod();
10149 if (language_ptr)
10150 *language_ptr = eLanguageTypeObjC;
10151 if (language_object_name_ptr)
10152 language_object_name_ptr->SetCString("self");
10153 return true;
10154 } else if (CXXMethodDecl *cxx_method =
10155 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
10156 if (is_instance_method_ptr)
10157 *is_instance_method_ptr = cxx_method->isInstance();
10158 if (language_ptr)
10159 *language_ptr = eLanguageTypeC_plus_plus;
10160 if (language_object_name_ptr)
10161 language_object_name_ptr->SetCString("this");
10162 return true;
10163 } else if (clang::FunctionDecl *function_decl =
10164 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
10165 ClangASTMetadata *metadata =
10166 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
10167 if (metadata && metadata->HasObjectPtr()) {
10168 if (is_instance_method_ptr)
10169 *is_instance_method_ptr = true;
10170 if (language_ptr)
10171 *language_ptr = eLanguageTypeObjC;
10172 if (language_object_name_ptr)
10173 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10174 return true;
10175 }
10176 }
10177 }
10178 return false;
10179}
10180
10181clang::DeclContext *
10182ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10183 if (dc.IsClang())
10184 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10185 return nullptr;
10186}
Greg Clayton99558cc42015-08-24 23:46:31 +000010187
10188ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010189ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10190 if (dc.IsClang())
10191 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10192 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10193 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010194}
10195
10196CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010197ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10198 if (dc.IsClang())
10199 return llvm::dyn_cast<clang::CXXMethodDecl>(
10200 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10201 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010202}
10203
10204clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010205ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10206 if (dc.IsClang())
10207 return llvm::dyn_cast<clang::FunctionDecl>(
10208 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10209 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010210}
10211
10212clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010213ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10214 if (dc.IsClang())
10215 return llvm::dyn_cast<clang::NamespaceDecl>(
10216 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10217 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010218}
10219
10220ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010221ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10222 const void *object) {
10223 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10224 if (ast)
10225 return ClangASTContext::GetMetadata(ast, object);
10226 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010227}
10228
10229clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010230ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10231 ClangASTContext *ast =
10232 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10233 if (ast)
10234 return ast->getASTContext();
10235 return nullptr;
10236}
10237
10238ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10239 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10240 m_target_wp(target.shared_from_this()),
10241 m_persistent_variables(new ClangPersistentVariables) {}
10242
10243UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010244 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010245 Expression::ResultType desired_type,
10246 const EvaluateExpressionOptions &options) {
10247 TargetSP target_sp = m_target_wp.lock();
10248 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010249 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010250
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010251 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010252 desired_type, options);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010253}
10254
Kate Stoneb9c1b512016-09-06 20:57:50 +000010255FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10256 const CompilerType &return_type, const Address &function_address,
10257 const ValueList &arg_value_list, const char *name) {
10258 TargetSP target_sp = m_target_wp.lock();
10259 if (!target_sp)
10260 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010261
Kate Stoneb9c1b512016-09-06 20:57:50 +000010262 Process *process = target_sp->GetProcessSP().get();
10263 if (!process)
10264 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010265
Kate Stoneb9c1b512016-09-06 20:57:50 +000010266 return new ClangFunctionCaller(*process, return_type, function_address,
10267 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010268}
10269
10270UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010271ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10272 const char *name) {
10273 TargetSP target_sp = m_target_wp.lock();
10274 if (!target_sp)
10275 return nullptr;
10276
10277 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010278}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010279
10280PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010281ClangASTContextForExpressions::GetPersistentExpressionState() {
10282 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010283}
Sean Callanan68e44232017-09-28 20:20:25 +000010284
10285clang::ExternalASTMerger &
10286ClangASTContextForExpressions::GetMergerUnchecked() {
Eugene Zemtsova9d928c2017-09-29 03:15:08 +000010287 lldbassert(m_scratch_ast_source_ap != nullptr);
Sean Callanan68e44232017-09-28 20:20:25 +000010288 return m_scratch_ast_source_ap->GetMergerUnchecked();
10289}