blob: f98135216a5ad46e5730982ce5a5695c679e7111 [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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015// C Includes
16// C++ Includes
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000017#include <mutex>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include <string>
Sean Callananfe38c852015-10-08 23:07:53 +000019#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
21// Other libraries and framework includes
Greg Clayton6beaaa62011-01-17 03:46:26 +000022
Kate Stoneb9c1b512016-09-06 20:57:50 +000023// Clang headers like to use NDEBUG inside of them to enable/disable debug
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000024// related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
Greg Clayton6beaaa62011-01-17 03:46:26 +000025// or another. This is bad because it means that if clang was built in release
26// mode, it assumes that you are building in release mode which is not always
27// the case. You can end up with functions that are defined as empty in header
28// files when NDEBUG is not defined, and this can cause link errors with the
29// clang .a files that you have since you might be missing functions in the .a
30// file. So we have to define NDEBUG when including clang headers to avoid any
31// mismatches. This is covered by rdar://problem/8691220
32
Sean Callanan3b1d4f62011-10-26 17:46:51 +000033#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000034#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000035#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000036// Need to include assert.h so it is as clang would expect it to be (disabled)
37#include <assert.h>
38#endif
39
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "clang/AST/ASTContext.h"
41#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000042#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000044#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000045#include "clang/AST/DeclTemplate.h"
Greg Claytonfe689042015-11-10 17:47:04 +000046#include "clang/AST/Mangle.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "clang/AST/RecordLayout.h"
48#include "clang/AST/Type.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000049#include "clang/AST/VTableBuilder.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000051#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000053#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054#include "clang/Basic/SourceManager.h"
55#include "clang/Basic/TargetInfo.h"
56#include "clang/Basic/TargetOptions.h"
57#include "clang/Frontend/FrontendOptions.h"
58#include "clang/Frontend/LangStandard.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000059
60#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000061#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000062#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
63// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
64#include <assert.h>
65#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066
Greg Claytond8d4a572015-08-11 21:38:15 +000067#include "llvm/Support/Signals.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000068#include "llvm/Support/Threading.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000069
Zachary Turnerd133f6a2016-03-28 22:53:41 +000070#include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
71#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
72#include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000073#include "lldb/Utility/ArchSpec.h"
Zachary Turner01c32432017-02-14 19:06:07 +000074#include "lldb/Utility/Flags.h"
75
Zachary Turner29cb8682017-03-03 20:57:05 +000076#include "lldb/Core/DumpDataExtractor.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000077#include "lldb/Core/Module.h"
78#include "lldb/Core/PluginManager.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000079#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000080#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000081#include "lldb/Core/UniqueCStringMap.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000082#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000083#include "lldb/Symbol/ClangASTImporter.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000084#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000085#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000086#include "lldb/Symbol/ClangUtil.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000087#include "lldb/Symbol/ObjectFile.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000088#include "lldb/Symbol/SymbolFile.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000089#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000090#include "lldb/Target/ExecutionContext.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000091#include "lldb/Target/Language.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000092#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham151c0322015-09-15 21:13:50 +000093#include "lldb/Target/Process.h"
94#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000095#include "lldb/Utility/DataExtractor.h"
Sean Callananc530ba92016-05-02 21:15:31 +000096#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000097#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000098#include "lldb/Utility/RegularExpression.h"
Pavel Labathd821c992018-08-07 11:07:21 +000099#include "lldb/Utility/Scalar.h"
Jim Inghamd555bac2011-06-24 22:03:24 +0000100
Greg Clayton261ac3f2015-08-28 01:01:03 +0000101#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
Zachary Turner42dff792016-04-15 00:21:26 +0000102#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +0000103
Eli Friedman932197d2010-06-13 19:06:42 +0000104#include <stdio.h>
105
Greg Clayton1341baf2013-07-11 23:36:31 +0000106#include <mutex>
107
Greg Claytonc86103d2010-08-05 01:57:25 +0000108using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109using namespace lldb_private;
110using namespace llvm;
111using namespace clang;
112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113namespace {
114static inline bool
115ClangASTContextSupportsLanguage(lldb::LanguageType language) {
116 return language == eLanguageTypeUnknown || // Clang is the default type system
117 Language::LanguageIsC(language) ||
118 Language::LanguageIsCPlusPlus(language) ||
119 Language::LanguageIsObjC(language) ||
120 Language::LanguageIsPascal(language) ||
121 // Use Clang for Rust until there is a proper language plugin for it
122 language == eLanguageTypeRust ||
Johan Engelen04799572016-11-25 11:01:12 +0000123 language == eLanguageTypeExtRenderScript ||
124 // Use Clang for D until there is a proper language plugin for it
125 language == eLanguageTypeD;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126}
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000127
128// Checks whether m1 is an overload of m2 (as opposed to an override). This is
129// called by addOverridesForMethod to distinguish overrides (which share a
130// vtable entry) from overloads (which require distinct entries).
131bool isOverload(clang::CXXMethodDecl *m1, clang::CXXMethodDecl *m2) {
132 // FIXME: This should detect covariant return types, but currently doesn't.
133 lldbassert(&m1->getASTContext() == &m2->getASTContext() &&
134 "Methods should have the same AST context");
135 clang::ASTContext &context = m1->getASTContext();
136
137 const auto *m1Type = llvm::cast<clang::FunctionProtoType>(
138 context.getCanonicalType(m1->getType()));
139
140 const auto *m2Type = llvm::cast<clang::FunctionProtoType>(
141 context.getCanonicalType(m2->getType()));
142
143 auto compareArgTypes = [&context](const clang::QualType &m1p,
144 const clang::QualType &m2p) {
145 return context.hasSameType(m1p.getUnqualifiedType(),
146 m2p.getUnqualifiedType());
147 };
148
149 // FIXME: In C++14 and later, we can just pass m2Type->param_type_end()
150 // as a fourth parameter to std::equal().
151 return (m1->getNumParams() != m2->getNumParams()) ||
152 !std::equal(m1Type->param_type_begin(), m1Type->param_type_end(),
153 m2Type->param_type_begin(), compareArgTypes);
154}
155
156// If decl is a virtual method, walk the base classes looking for methods that
157// decl overrides. This table of overridden methods is used by IRGen to
158// determine the vtable layout for decl's parent class.
159void addOverridesForMethod(clang::CXXMethodDecl *decl) {
160 if (!decl->isVirtual())
161 return;
162
163 clang::CXXBasePaths paths;
164
165 auto find_overridden_methods =
166 [decl](const clang::CXXBaseSpecifier *specifier,
167 clang::CXXBasePath &path) {
168 if (auto *base_record = llvm::dyn_cast<clang::CXXRecordDecl>(
169 specifier->getType()->getAs<clang::RecordType>()->getDecl())) {
170
171 clang::DeclarationName name = decl->getDeclName();
172
173 // If this is a destructor, check whether the base class destructor is
174 // virtual.
175 if (name.getNameKind() == clang::DeclarationName::CXXDestructorName)
176 if (auto *baseDtorDecl = base_record->getDestructor()) {
177 if (baseDtorDecl->isVirtual()) {
178 path.Decls = baseDtorDecl;
179 return true;
180 } else
181 return false;
182 }
183
184 // Otherwise, search for name in the base class.
185 for (path.Decls = base_record->lookup(name); !path.Decls.empty();
186 path.Decls = path.Decls.slice(1)) {
187 if (auto *method_decl =
188 llvm::dyn_cast<clang::CXXMethodDecl>(path.Decls.front()))
189 if (method_decl->isVirtual() && !isOverload(decl, method_decl)) {
190 path.Decls = method_decl;
191 return true;
192 }
193 }
194 }
195
196 return false;
197 };
198
199 if (decl->getParent()->lookupInBases(find_overridden_methods, paths)) {
200 for (auto *overridden_decl : paths.found_decls())
201 decl->addOverriddenMethod(
202 llvm::cast<clang::CXXMethodDecl>(overridden_decl));
203 }
204}
Greg Clayton56939cb2015-09-17 22:23:34 +0000205}
206
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *>
208 ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000209
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210static ClangASTMap &GetASTMap() {
211 static ClangASTMap *g_map_ptr = nullptr;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000212 static llvm::once_flag g_once_flag;
213 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
215 });
216 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000217}
218
Davide Italiano7e3ef4d2018-03-20 19:46:32 +0000219bool ClangASTContext::IsOperator(const char *name,
220 clang::OverloadedOperatorKind &op_kind) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 if (name == nullptr || name[0] == '\0')
222 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000223
224#define OPERATOR_PREFIX "operator"
225#define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1)
226
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 const char *post_op_name = nullptr;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000228
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 bool no_space = true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
232 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000233
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234 post_op_name = name + OPERATOR_PREFIX_LENGTH;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000235
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236 if (post_op_name[0] == ' ') {
237 post_op_name++;
238 no_space = false;
239 }
Pavel Labath1ac2b202016-08-15 14:32:32 +0000240
241#undef OPERATOR_PREFIX
242#undef OPERATOR_PREFIX_LENGTH
243
Adrian Prantl05097242018-04-30 16:49:04 +0000244 // This is an operator, set the overloaded operator kind to invalid in case
245 // this is a conversion operator...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 op_kind = clang::NUM_OVERLOADED_OPERATORS;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000247
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 switch (post_op_name[0]) {
249 default:
250 if (no_space)
251 return false;
252 break;
253 case 'n':
254 if (no_space)
255 return false;
256 if (strcmp(post_op_name, "new") == 0)
257 op_kind = clang::OO_New;
258 else if (strcmp(post_op_name, "new[]") == 0)
259 op_kind = clang::OO_Array_New;
260 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 case 'd':
263 if (no_space)
264 return false;
265 if (strcmp(post_op_name, "delete") == 0)
266 op_kind = clang::OO_Delete;
267 else if (strcmp(post_op_name, "delete[]") == 0)
268 op_kind = clang::OO_Array_Delete;
269 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000270
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 case '+':
272 if (post_op_name[1] == '\0')
273 op_kind = clang::OO_Plus;
274 else if (post_op_name[2] == '\0') {
275 if (post_op_name[1] == '=')
276 op_kind = clang::OO_PlusEqual;
277 else if (post_op_name[1] == '+')
278 op_kind = clang::OO_PlusPlus;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000279 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000281
Kate Stoneb9c1b512016-09-06 20:57:50 +0000282 case '-':
283 if (post_op_name[1] == '\0')
284 op_kind = clang::OO_Minus;
285 else if (post_op_name[2] == '\0') {
286 switch (post_op_name[1]) {
287 case '=':
288 op_kind = clang::OO_MinusEqual;
289 break;
290 case '-':
291 op_kind = clang::OO_MinusMinus;
292 break;
293 case '>':
294 op_kind = clang::OO_Arrow;
295 break;
296 }
297 } else if (post_op_name[3] == '\0') {
298 if (post_op_name[2] == '*')
299 op_kind = clang::OO_ArrowStar;
300 break;
301 }
302 break;
303
304 case '*':
305 if (post_op_name[1] == '\0')
306 op_kind = clang::OO_Star;
307 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
308 op_kind = clang::OO_StarEqual;
309 break;
310
311 case '/':
312 if (post_op_name[1] == '\0')
313 op_kind = clang::OO_Slash;
314 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
315 op_kind = clang::OO_SlashEqual;
316 break;
317
318 case '%':
319 if (post_op_name[1] == '\0')
320 op_kind = clang::OO_Percent;
321 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
322 op_kind = clang::OO_PercentEqual;
323 break;
324
325 case '^':
326 if (post_op_name[1] == '\0')
327 op_kind = clang::OO_Caret;
328 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
329 op_kind = clang::OO_CaretEqual;
330 break;
331
332 case '&':
333 if (post_op_name[1] == '\0')
334 op_kind = clang::OO_Amp;
335 else if (post_op_name[2] == '\0') {
336 switch (post_op_name[1]) {
337 case '=':
338 op_kind = clang::OO_AmpEqual;
339 break;
340 case '&':
341 op_kind = clang::OO_AmpAmp;
342 break;
343 }
344 }
345 break;
346
347 case '|':
348 if (post_op_name[1] == '\0')
349 op_kind = clang::OO_Pipe;
350 else if (post_op_name[2] == '\0') {
351 switch (post_op_name[1]) {
352 case '=':
353 op_kind = clang::OO_PipeEqual;
354 break;
355 case '|':
356 op_kind = clang::OO_PipePipe;
357 break;
358 }
359 }
360 break;
361
362 case '~':
363 if (post_op_name[1] == '\0')
364 op_kind = clang::OO_Tilde;
365 break;
366
367 case '!':
368 if (post_op_name[1] == '\0')
369 op_kind = clang::OO_Exclaim;
370 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
371 op_kind = clang::OO_ExclaimEqual;
372 break;
373
374 case '=':
375 if (post_op_name[1] == '\0')
376 op_kind = clang::OO_Equal;
377 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
378 op_kind = clang::OO_EqualEqual;
379 break;
380
381 case '<':
382 if (post_op_name[1] == '\0')
383 op_kind = clang::OO_Less;
384 else if (post_op_name[2] == '\0') {
385 switch (post_op_name[1]) {
386 case '<':
387 op_kind = clang::OO_LessLess;
388 break;
389 case '=':
390 op_kind = clang::OO_LessEqual;
391 break;
392 }
393 } else if (post_op_name[3] == '\0') {
394 if (post_op_name[2] == '=')
395 op_kind = clang::OO_LessLessEqual;
396 }
397 break;
398
399 case '>':
400 if (post_op_name[1] == '\0')
401 op_kind = clang::OO_Greater;
402 else if (post_op_name[2] == '\0') {
403 switch (post_op_name[1]) {
404 case '>':
405 op_kind = clang::OO_GreaterGreater;
406 break;
407 case '=':
408 op_kind = clang::OO_GreaterEqual;
409 break;
410 }
411 } else if (post_op_name[1] == '>' && post_op_name[2] == '=' &&
412 post_op_name[3] == '\0') {
413 op_kind = clang::OO_GreaterGreaterEqual;
414 }
415 break;
416
417 case ',':
418 if (post_op_name[1] == '\0')
419 op_kind = clang::OO_Comma;
420 break;
421
422 case '(':
423 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
424 op_kind = clang::OO_Call;
425 break;
426
427 case '[':
428 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
429 op_kind = clang::OO_Subscript;
430 break;
431 }
432
433 return true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000434}
Enrico Granata5d84a692014-08-19 21:46:37 +0000435
Greg Clayton57ee3062013-07-11 22:46:58 +0000436clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +0000437ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
438 switch (access) {
439 default:
440 break;
441 case eAccessNone:
Greg Clayton8cf05932010-07-22 18:30:50 +0000442 return AS_none;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443 case eAccessPublic:
444 return AS_public;
445 case eAccessPrivate:
446 return AS_private;
447 case eAccessProtected:
448 return AS_protected;
449 }
450 return AS_none;
Greg Clayton8cf05932010-07-22 18:30:50 +0000451}
452
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
454 // FIXME: Cleanup per-file based stuff.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455
Adrian Prantl05097242018-04-30 16:49:04 +0000456 // Set some properties which depend solely on the input kind; it would be
457 // nice to move these to the language standard, and have the driver resolve
458 // the input kind + language standard.
Richard Smith8186cd42017-04-26 22:10:53 +0000459 if (IK.getLanguage() == InputKind::Asm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 Opts.AsmPreprocessor = 1;
Richard Smith8186cd42017-04-26 22:10:53 +0000461 } else if (IK.isObjectiveC()) {
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000462 Opts.ObjC = 1;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463 }
464
465 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
466
467 if (LangStd == LangStandard::lang_unspecified) {
468 // Based on the base language, pick one.
Richard Smith8186cd42017-04-26 22:10:53 +0000469 switch (IK.getLanguage()) {
470 case InputKind::Unknown:
471 case InputKind::LLVM_IR:
472 case InputKind::RenderScript:
David Blaikiea322f362017-01-06 00:38:06 +0000473 llvm_unreachable("Invalid input kind!");
Richard Smith8186cd42017-04-26 22:10:53 +0000474 case InputKind::OpenCL:
Pavel Labath47168542017-04-27 08:49:19 +0000475 LangStd = LangStandard::lang_opencl10;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000476 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000477 case InputKind::CUDA:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478 LangStd = LangStandard::lang_cuda;
479 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000480 case InputKind::Asm:
481 case InputKind::C:
482 case InputKind::ObjC:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483 LangStd = LangStandard::lang_gnu99;
484 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000485 case InputKind::CXX:
486 case InputKind::ObjCXX:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487 LangStd = LangStandard::lang_gnucxx98;
488 break;
Benjamin Kramer0d97c222018-04-25 13:22:47 +0000489 case InputKind::HIP:
490 LangStd = LangStandard::lang_hip;
491 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000493 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
496 Opts.LineComment = Std.hasLineComments();
497 Opts.C99 = Std.isC99();
498 Opts.CPlusPlus = Std.isCPlusPlus();
499 Opts.CPlusPlus11 = Std.isCPlusPlus11();
500 Opts.Digraphs = Std.hasDigraphs();
501 Opts.GNUMode = Std.isGNUMode();
502 Opts.GNUInline = !Std.isC99();
503 Opts.HexFloats = Std.hasHexFloats();
504 Opts.ImplicitInt = Std.hasImplicitInt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508 // OpenCL has some additional defaults.
Pavel Labath47168542017-04-27 08:49:19 +0000509 if (LangStd == LangStandard::lang_opencl10) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 Opts.OpenCL = 1;
511 Opts.AltiVec = 1;
512 Opts.CXXOperatorNames = 1;
513 Opts.LaxVectorConversions = 1;
514 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 // OpenCL and C++ both have bool, true, false keywords.
517 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000518
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000520
Adrian Prantl05097242018-04-30 16:49:04 +0000521 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is
522 // specified, or -std is set to a conforming mode.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000523 Opts.Trigraphs = !Opts.GNUMode;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000525 Opts.OptimizeSize = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000526
Kate Stoneb9c1b512016-09-06 20:57:50 +0000527 // FIXME: Eliminate this dependency.
528 // unsigned Opt =
529 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
530 // Opts.Optimize = Opt != 0;
531 unsigned Opt = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000532
Kate Stoneb9c1b512016-09-06 20:57:50 +0000533 // This is the __NO_INLINE__ define, which just depends on things like the
534 // optimization level and -fno-inline, not actually whether the backend has
535 // inlining enabled.
536 //
537 // FIXME: This is affected by other options (-fno-inline).
538 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000539}
540
Kate Stoneb9c1b512016-09-06 20:57:50 +0000541ClangASTContext::ClangASTContext(const char *target_triple)
542 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_ap(),
543 m_language_options_ap(), m_source_manager_ap(), m_diagnostics_engine_ap(),
544 m_target_options_rp(), m_target_info_ap(), m_identifier_table_ap(),
545 m_selector_table_ap(), m_builtins_ap(), m_callback_tag_decl(nullptr),
546 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
547 m_pointer_byte_size(0), m_ast_owned(false) {
548 if (target_triple && target_triple[0])
549 SetTargetTriple(target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550}
551
552//----------------------------------------------------------------------
553// Destructor
554//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555ClangASTContext::~ClangASTContext() { Finalize(); }
556
557ConstString ClangASTContext::GetPluginNameStatic() {
558 return ConstString("clang");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559}
560
Kate Stoneb9c1b512016-09-06 20:57:50 +0000561ConstString ClangASTContext::GetPluginName() {
562 return ClangASTContext::GetPluginNameStatic();
Greg Clayton56939cb2015-09-17 22:23:34 +0000563}
564
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565uint32_t ClangASTContext::GetPluginVersion() { return 1; }
Greg Clayton56939cb2015-09-17 22:23:34 +0000566
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
568 lldb_private::Module *module,
569 Target *target) {
570 if (ClangASTContextSupportsLanguage(language)) {
571 ArchSpec arch;
572 if (module)
573 arch = module->GetArchitecture();
574 else if (target)
575 arch = target->GetArchitecture();
Greg Clayton56939cb2015-09-17 22:23:34 +0000576
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577 if (arch.IsValid()) {
578 ArchSpec fixed_arch = arch;
579 // LLVM wants this to be set to iOS or MacOSX; if we're working on
580 // a bare-boards type image, change the triple for llvm's benefit.
581 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
582 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
583 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
584 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
585 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
586 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
587 } else {
588 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
Greg Clayton56939cb2015-09-17 22:23:34 +0000589 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000591
Kate Stoneb9c1b512016-09-06 20:57:50 +0000592 if (module) {
593 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
594 if (ast_sp) {
595 ast_sp->SetArchitecture(fixed_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000596 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597 return ast_sp;
598 } else if (target && target->IsValid()) {
599 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
600 new ClangASTContextForExpressions(*target));
601 if (ast_sp) {
602 ast_sp->SetArchitecture(fixed_arch);
603 ast_sp->m_scratch_ast_source_ap.reset(
604 new ClangASTSource(target->shared_from_this()));
Sean Callanan68e44232017-09-28 20:20:25 +0000605 lldbassert(ast_sp->getFileManager());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000606 ast_sp->m_scratch_ast_source_ap->InstallASTContext(
Sean Callanan68e44232017-09-28 20:20:25 +0000607 *ast_sp->getASTContext(), *ast_sp->getFileManager(), true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000608 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
609 ast_sp->m_scratch_ast_source_ap->CreateProxy());
610 ast_sp->SetExternalSource(proxy_ast_source);
611 return ast_sp;
612 }
613 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615 }
616 return lldb::TypeSystemSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617}
618
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619void ClangASTContext::EnumerateSupportedLanguages(
620 std::set<lldb::LanguageType> &languages_for_types,
621 std::set<lldb::LanguageType> &languages_for_expressions) {
622 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
623 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
624 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
625 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
626 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
627 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
628
629 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
630 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
631 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
632 lldb::eLanguageTypeC_plus_plus_14});
633
634 languages_for_types.insert(s_supported_languages_for_types.begin(),
635 s_supported_languages_for_types.end());
636 languages_for_expressions.insert(
637 s_supported_languages_for_expressions.begin(),
638 s_supported_languages_for_expressions.end());
Enrico Granata5d84a692014-08-19 21:46:37 +0000639}
640
Kate Stoneb9c1b512016-09-06 20:57:50 +0000641void ClangASTContext::Initialize() {
642 PluginManager::RegisterPlugin(GetPluginNameStatic(),
643 "clang base AST context plug-in",
644 CreateInstance, EnumerateSupportedLanguages);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000645}
646
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647void ClangASTContext::Terminate() {
648 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000649}
650
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651void ClangASTContext::Finalize() {
652 if (m_ast_ap.get()) {
653 GetASTMap().Erase(m_ast_ap.get());
654 if (!m_ast_owned)
655 m_ast_ap.release();
656 }
657
658 m_builtins_ap.reset();
659 m_selector_table_ap.reset();
660 m_identifier_table_ap.reset();
661 m_target_info_ap.reset();
662 m_target_options_rp.reset();
663 m_diagnostics_engine_ap.reset();
664 m_source_manager_ap.reset();
665 m_language_options_ap.reset();
666 m_ast_ap.reset();
667 m_scratch_ast_source_ap.reset();
668}
669
670void ClangASTContext::Clear() {
671 m_ast_ap.reset();
672 m_language_options_ap.reset();
673 m_source_manager_ap.reset();
674 m_diagnostics_engine_ap.reset();
675 m_target_options_rp.reset();
676 m_target_info_ap.reset();
677 m_identifier_table_ap.reset();
678 m_selector_table_ap.reset();
679 m_builtins_ap.reset();
680 m_pointer_byte_size = 0;
681}
682
683const char *ClangASTContext::GetTargetTriple() {
684 return m_target_triple.c_str();
685}
686
687void ClangASTContext::SetTargetTriple(const char *target_triple) {
688 Clear();
689 m_target_triple.assign(target_triple);
690}
691
692void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
693 SetTargetTriple(arch.GetTriple().str().c_str());
694}
695
696bool ClangASTContext::HasExternalSource() {
697 ASTContext *ast = getASTContext();
698 if (ast)
699 return ast->getExternalSource() != nullptr;
700 return false;
701}
702
703void ClangASTContext::SetExternalSource(
704 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) {
705 ASTContext *ast = getASTContext();
706 if (ast) {
707 ast->setExternalSource(ast_source_ap);
708 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000709 }
710}
711
712void ClangASTContext::RemoveExternalSource() {
713 ASTContext *ast = getASTContext();
714
715 if (ast) {
716 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
717 ast->setExternalSource(empty_ast_source_ap);
718 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000719 }
720}
721
722void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
723 if (!m_ast_owned) {
724 m_ast_ap.release();
725 }
726 m_ast_owned = false;
727 m_ast_ap.reset(ast_ctx);
728 GetASTMap().Insert(ast_ctx, this);
729}
730
731ASTContext *ClangASTContext::getASTContext() {
732 if (m_ast_ap.get() == nullptr) {
733 m_ast_owned = true;
734 m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
735 *getIdentifierTable(), *getSelectorTable(),
736 *getBuiltinContext()));
737
738 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
739
740 // This can be NULL if we don't know anything about the architecture or if
Adrian Prantl05097242018-04-30 16:49:04 +0000741 // the target for an architecture isn't enabled in the llvm/clang that we
742 // built
Kate Stoneb9c1b512016-09-06 20:57:50 +0000743 TargetInfo *target_info = getTargetInfo();
744 if (target_info)
745 m_ast_ap->InitBuiltinTypes(*target_info);
746
747 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
748 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
749 // m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000751
752 GetASTMap().Insert(m_ast_ap.get(), this);
753
754 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap(
755 new ClangExternalASTSourceCallbacks(
756 ClangASTContext::CompleteTagDecl,
757 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
758 ClangASTContext::LayoutRecordType, this));
759 SetExternalSource(ast_source_ap);
760 }
761 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762}
763
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
765 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
766 return clang_ast;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000767}
768
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769Builtin::Context *ClangASTContext::getBuiltinContext() {
770 if (m_builtins_ap.get() == nullptr)
771 m_builtins_ap.reset(new Builtin::Context());
772 return m_builtins_ap.get();
Sean Callanan79439e82010-11-18 02:56:27 +0000773}
774
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775IdentifierTable *ClangASTContext::getIdentifierTable() {
776 if (m_identifier_table_ap.get() == nullptr)
777 m_identifier_table_ap.reset(
778 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
779 return m_identifier_table_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000780}
781
Kate Stoneb9c1b512016-09-06 20:57:50 +0000782LangOptions *ClangASTContext::getLanguageOptions() {
783 if (m_language_options_ap.get() == nullptr) {
784 m_language_options_ap.reset(new LangOptions());
Richard Smith8186cd42017-04-26 22:10:53 +0000785 ParseLangArgs(*m_language_options_ap, InputKind::ObjCXX, GetTargetTriple());
786 // InitializeLangOptions(*m_language_options_ap, InputKind::ObjCXX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000787 }
788 return m_language_options_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000789}
790
Kate Stoneb9c1b512016-09-06 20:57:50 +0000791SelectorTable *ClangASTContext::getSelectorTable() {
792 if (m_selector_table_ap.get() == nullptr)
793 m_selector_table_ap.reset(new SelectorTable());
794 return m_selector_table_ap.get();
Greg Claytonfe689042015-11-10 17:47:04 +0000795}
796
Kate Stoneb9c1b512016-09-06 20:57:50 +0000797clang::FileManager *ClangASTContext::getFileManager() {
798 if (m_file_manager_ap.get() == nullptr) {
799 clang::FileSystemOptions file_system_options;
800 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
801 }
802 return m_file_manager_ap.get();
803}
804
805clang::SourceManager *ClangASTContext::getSourceManager() {
806 if (m_source_manager_ap.get() == nullptr)
807 m_source_manager_ap.reset(
808 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
809 return m_source_manager_ap.get();
810}
811
812clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
813 if (m_diagnostics_engine_ap.get() == nullptr) {
814 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
815 m_diagnostics_engine_ap.reset(
816 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
817 }
818 return m_diagnostics_engine_ap.get();
819}
820
821clang::MangleContext *ClangASTContext::getMangleContext() {
822 if (m_mangle_ctx_ap.get() == nullptr)
823 m_mangle_ctx_ap.reset(getASTContext()->createMangleContext());
824 return m_mangle_ctx_ap.get();
825}
826
827class NullDiagnosticConsumer : public DiagnosticConsumer {
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000828public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000829 NullDiagnosticConsumer() {
830 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
831 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000832
Kate Stoneb9c1b512016-09-06 20:57:50 +0000833 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
834 const clang::Diagnostic &info) {
835 if (m_log) {
836 llvm::SmallVector<char, 32> diag_str(10);
837 info.FormatDiagnostic(diag_str);
838 diag_str.push_back('\0');
839 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000840 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000841 }
842
843 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
844 return new NullDiagnosticConsumer();
845 }
846
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000847private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000848 Log *m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000849};
850
Kate Stoneb9c1b512016-09-06 20:57:50 +0000851DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
852 if (m_diagnostic_consumer_ap.get() == nullptr)
853 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
854
855 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000856}
857
Kate Stoneb9c1b512016-09-06 20:57:50 +0000858std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
859 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) {
860 m_target_options_rp = std::make_shared<clang::TargetOptions>();
861 if (m_target_options_rp.get() != nullptr)
862 m_target_options_rp->Triple = m_target_triple;
863 }
864 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000865}
866
Kate Stoneb9c1b512016-09-06 20:57:50 +0000867TargetInfo *ClangASTContext::getTargetInfo() {
868 // target_triple should be something like "x86_64-apple-macosx"
869 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
870 m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
871 getTargetOptions()));
872 return m_target_info_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000873}
874
875#pragma mark Basic Types
876
Kate Stoneb9c1b512016-09-06 20:57:50 +0000877static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
878 ASTContext *ast, QualType qual_type) {
879 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
880 if (qual_type_bit_size == bit_size)
881 return true;
882 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000883}
Greg Clayton56939cb2015-09-17 22:23:34 +0000884
Greg Claytona1e5dc82015-08-11 22:53:00 +0000885CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000886ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
887 size_t bit_size) {
888 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
889 getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000890}
891
Kate Stoneb9c1b512016-09-06 20:57:50 +0000892CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
893 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
894 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000895 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000896 switch (encoding) {
897 case eEncodingInvalid:
898 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
899 return CompilerType(ast, ast->VoidPtrTy);
900 break;
901
902 case eEncodingUint:
903 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
904 return CompilerType(ast, ast->UnsignedCharTy);
905 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
906 return CompilerType(ast, ast->UnsignedShortTy);
907 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
908 return CompilerType(ast, ast->UnsignedIntTy);
909 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
910 return CompilerType(ast, ast->UnsignedLongTy);
911 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
912 return CompilerType(ast, ast->UnsignedLongLongTy);
913 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
914 return CompilerType(ast, ast->UnsignedInt128Ty);
915 break;
916
917 case eEncodingSint:
918 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
919 return CompilerType(ast, ast->SignedCharTy);
920 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
921 return CompilerType(ast, ast->ShortTy);
922 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
923 return CompilerType(ast, ast->IntTy);
924 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
925 return CompilerType(ast, ast->LongTy);
926 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
927 return CompilerType(ast, ast->LongLongTy);
928 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
929 return CompilerType(ast, ast->Int128Ty);
930 break;
931
932 case eEncodingIEEE754:
933 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
934 return CompilerType(ast, ast->FloatTy);
935 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
936 return CompilerType(ast, ast->DoubleTy);
937 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
938 return CompilerType(ast, ast->LongDoubleTy);
939 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
940 return CompilerType(ast, ast->HalfTy);
941 break;
942
943 case eEncodingVector:
944 // Sanity check that bit_size is a multiple of 8's.
945 if (bit_size && !(bit_size & 0x7u))
946 return CompilerType(
947 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
948 break;
949 }
950
951 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000952}
953
Greg Clayton57ee3062013-07-11 22:46:58 +0000954lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000955ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) {
956 if (name) {
957 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
958 static TypeNameToBasicTypeMap g_type_map;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000959 static llvm::once_flag g_once_flag;
960 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000961 // "void"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000962 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000963
964 // "char"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000965 g_type_map.Append(ConstString("char"), eBasicTypeChar);
966 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
967 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
968 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
969 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
970 g_type_map.Append(ConstString("unsigned wchar_t"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000971 eBasicTypeUnsignedWChar);
972 // "short"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000973 g_type_map.Append(ConstString("short"), eBasicTypeShort);
974 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
975 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
976 g_type_map.Append(ConstString("unsigned short int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000977 eBasicTypeUnsignedShort);
978
979 // "int"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000980 g_type_map.Append(ConstString("int"), eBasicTypeInt);
981 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
982 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
983 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000984
985 // "long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000986 g_type_map.Append(ConstString("long"), eBasicTypeLong);
987 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
988 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
989 g_type_map.Append(ConstString("unsigned long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000990 eBasicTypeUnsignedLong);
991
992 // "long long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000993 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
994 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
995 g_type_map.Append(ConstString("unsigned long long"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000996 eBasicTypeUnsignedLongLong);
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000997 g_type_map.Append(ConstString("unsigned long long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000998 eBasicTypeUnsignedLongLong);
999
1000 // "int128"
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001001 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
1002 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001003
1004 // Miscellaneous
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001005 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
1006 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
1007 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
1008 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
1009 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
1010 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
1011 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012 g_type_map.Sort();
1013 });
1014
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001015 return g_type_map.Find(name, eBasicTypeInvalid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001016 }
1017 return eBasicTypeInvalid;
Greg Clayton57ee3062013-07-11 22:46:58 +00001018}
1019
Kate Stoneb9c1b512016-09-06 20:57:50 +00001020CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1021 const ConstString &name) {
1022 if (ast) {
1023 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1024 return ClangASTContext::GetBasicType(ast, basic_type);
1025 }
1026 return CompilerType();
1027}
1028
1029uint32_t ClangASTContext::GetPointerByteSize() {
1030 if (m_pointer_byte_size == 0)
1031 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid)
1032 .GetPointerType()
1033 .GetByteSize(nullptr);
1034 return m_pointer_byte_size;
1035}
1036
1037CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1038 return GetBasicType(getASTContext(), basic_type);
1039}
1040
1041CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1042 lldb::BasicType basic_type) {
1043 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001044 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001045 lldb::opaque_compiler_type_t clang_type =
1046 GetOpaqueCompilerType(ast, basic_type);
1047
1048 if (clang_type)
1049 return CompilerType(GetASTContext(ast), clang_type);
1050 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001051}
1052
Kate Stoneb9c1b512016-09-06 20:57:50 +00001053CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1054 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1055 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001056
Kate Stoneb9c1b512016-09-06 20:57:50 +00001057#define streq(a, b) strcmp(a, b) == 0
1058 assert(ast != nullptr);
1059 if (ast) {
1060 switch (dw_ate) {
1061 default:
1062 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001063
Kate Stoneb9c1b512016-09-06 20:57:50 +00001064 case DW_ATE_address:
1065 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1066 return CompilerType(ast, ast->VoidPtrTy);
1067 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001068
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069 case DW_ATE_boolean:
1070 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
1071 return CompilerType(ast, ast->BoolTy);
1072 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1073 return CompilerType(ast, ast->UnsignedCharTy);
1074 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1075 return CompilerType(ast, ast->UnsignedShortTy);
1076 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1077 return CompilerType(ast, ast->UnsignedIntTy);
1078 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001079
Kate Stoneb9c1b512016-09-06 20:57:50 +00001080 case DW_ATE_lo_user:
1081 // This has been seen to mean DW_AT_complex_integer
1082 if (type_name) {
1083 if (::strstr(type_name, "complex")) {
1084 CompilerType complex_int_clang_type =
1085 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1086 bit_size / 2);
1087 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1088 complex_int_clang_type)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001090 }
1091 break;
1092
1093 case DW_ATE_complex_float:
1094 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1095 return CompilerType(ast, ast->FloatComplexTy);
1096 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1097 return CompilerType(ast, ast->DoubleComplexTy);
1098 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1099 return CompilerType(ast, ast->LongDoubleComplexTy);
1100 else {
1101 CompilerType complex_float_clang_type =
1102 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1103 bit_size / 2);
1104 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1105 complex_float_clang_type)));
1106 }
1107 break;
1108
1109 case DW_ATE_float:
1110 if (streq(type_name, "float") &&
1111 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1112 return CompilerType(ast, ast->FloatTy);
1113 if (streq(type_name, "double") &&
1114 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1115 return CompilerType(ast, ast->DoubleTy);
1116 if (streq(type_name, "long double") &&
1117 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1118 return CompilerType(ast, ast->LongDoubleTy);
1119 // Fall back to not requiring a name match
1120 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1121 return CompilerType(ast, ast->FloatTy);
1122 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1123 return CompilerType(ast, ast->DoubleTy);
1124 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1125 return CompilerType(ast, ast->LongDoubleTy);
1126 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1127 return CompilerType(ast, ast->HalfTy);
1128 break;
1129
1130 case DW_ATE_signed:
1131 if (type_name) {
1132 if (streq(type_name, "wchar_t") &&
1133 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1134 (getTargetInfo() &&
1135 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1136 return CompilerType(ast, ast->WCharTy);
1137 if (streq(type_name, "void") &&
1138 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1139 return CompilerType(ast, ast->VoidTy);
1140 if (strstr(type_name, "long long") &&
1141 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1142 return CompilerType(ast, ast->LongLongTy);
1143 if (strstr(type_name, "long") &&
1144 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1145 return CompilerType(ast, ast->LongTy);
1146 if (strstr(type_name, "short") &&
1147 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1148 return CompilerType(ast, ast->ShortTy);
1149 if (strstr(type_name, "char")) {
1150 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1151 return CompilerType(ast, ast->CharTy);
1152 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1153 return CompilerType(ast, ast->SignedCharTy);
1154 }
1155 if (strstr(type_name, "int")) {
1156 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1157 return CompilerType(ast, ast->IntTy);
1158 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1159 return CompilerType(ast, ast->Int128Ty);
1160 }
1161 }
1162 // We weren't able to match up a type name, just search by size
1163 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1164 return CompilerType(ast, ast->CharTy);
1165 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1166 return CompilerType(ast, ast->ShortTy);
1167 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1168 return CompilerType(ast, ast->IntTy);
1169 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1170 return CompilerType(ast, ast->LongTy);
1171 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1172 return CompilerType(ast, ast->LongLongTy);
1173 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1174 return CompilerType(ast, ast->Int128Ty);
1175 break;
1176
1177 case DW_ATE_signed_char:
1178 if (ast->getLangOpts().CharIsSigned && type_name &&
1179 streq(type_name, "char")) {
1180 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1181 return CompilerType(ast, ast->CharTy);
1182 }
1183 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1184 return CompilerType(ast, ast->SignedCharTy);
1185 break;
1186
1187 case DW_ATE_unsigned:
1188 if (type_name) {
1189 if (streq(type_name, "wchar_t")) {
1190 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1191 if (!(getTargetInfo() &&
1192 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1193 return CompilerType(ast, ast->WCharTy);
1194 }
1195 }
1196 if (strstr(type_name, "long long")) {
1197 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1198 return CompilerType(ast, ast->UnsignedLongLongTy);
1199 } else if (strstr(type_name, "long")) {
1200 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1201 return CompilerType(ast, ast->UnsignedLongTy);
1202 } else if (strstr(type_name, "short")) {
1203 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1204 return CompilerType(ast, ast->UnsignedShortTy);
1205 } else if (strstr(type_name, "char")) {
1206 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1207 return CompilerType(ast, ast->UnsignedCharTy);
1208 } else if (strstr(type_name, "int")) {
1209 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1210 return CompilerType(ast, ast->UnsignedIntTy);
1211 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1212 return CompilerType(ast, ast->UnsignedInt128Ty);
1213 }
1214 }
1215 // We weren't able to match up a type name, just search by size
1216 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1217 return CompilerType(ast, ast->UnsignedCharTy);
1218 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1219 return CompilerType(ast, ast->UnsignedShortTy);
1220 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1221 return CompilerType(ast, ast->UnsignedIntTy);
1222 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1223 return CompilerType(ast, ast->UnsignedLongTy);
1224 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1225 return CompilerType(ast, ast->UnsignedLongLongTy);
1226 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1227 return CompilerType(ast, ast->UnsignedInt128Ty);
1228 break;
1229
1230 case DW_ATE_unsigned_char:
1231 if (!ast->getLangOpts().CharIsSigned && type_name &&
1232 streq(type_name, "char")) {
1233 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1234 return CompilerType(ast, ast->CharTy);
1235 }
1236 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1237 return CompilerType(ast, ast->UnsignedCharTy);
1238 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1239 return CompilerType(ast, ast->UnsignedShortTy);
1240 break;
1241
1242 case DW_ATE_imaginary_float:
1243 break;
1244
1245 case DW_ATE_UTF:
1246 if (type_name) {
1247 if (streq(type_name, "char16_t")) {
1248 return CompilerType(ast, ast->Char16Ty);
1249 } else if (streq(type_name, "char32_t")) {
1250 return CompilerType(ast, ast->Char32Ty);
1251 }
1252 }
1253 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001254 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001255 }
1256 // This assert should fire for anything that we don't catch above so we know
1257 // to fix any issues we run into.
1258 if (type_name) {
1259 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1260 "DW_TAG_base_type '%s' encoded with "
1261 "DW_ATE = 0x%x, bit_size = %u\n",
1262 type_name, dw_ate, bit_size);
1263 } else {
1264 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1265 "DW_TAG_base_type encoded with "
1266 "DW_ATE = 0x%x, bit_size = %u\n",
1267 dw_ate, bit_size);
1268 }
1269 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270}
1271
Kate Stoneb9c1b512016-09-06 20:57:50 +00001272CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1273 if (ast)
1274 return CompilerType(ast, ast->UnknownAnyTy);
1275 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001276}
1277
Kate Stoneb9c1b512016-09-06 20:57:50 +00001278CompilerType ClangASTContext::GetCStringType(bool is_const) {
1279 ASTContext *ast = getASTContext();
1280 QualType char_type(ast->CharTy);
1281
1282 if (is_const)
1283 char_type.addConst();
1284
1285 return CompilerType(ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001286}
1287
Zachary Turner115209e2018-11-05 19:25:39 +00001288clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001289ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1290 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001291}
1292
Kate Stoneb9c1b512016-09-06 20:57:50 +00001293clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1294 clang::Decl *source_decl) {
1295 FileSystemOptions file_system_options;
1296 FileManager file_manager(file_system_options);
1297 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1298
1299 return importer.Import(source_decl);
Greg Clayton526e5af2010-11-13 03:52:47 +00001300}
1301
Kate Stoneb9c1b512016-09-06 20:57:50 +00001302bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1303 bool ignore_qualifiers) {
1304 ClangASTContext *ast =
1305 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1306 if (!ast || ast != type2.GetTypeSystem())
1307 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001308
Kate Stoneb9c1b512016-09-06 20:57:50 +00001309 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1310 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001311
Kate Stoneb9c1b512016-09-06 20:57:50 +00001312 QualType type1_qual = ClangUtil::GetQualType(type1);
1313 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001314
Kate Stoneb9c1b512016-09-06 20:57:50 +00001315 if (ignore_qualifiers) {
1316 type1_qual = type1_qual.getUnqualifiedType();
1317 type2_qual = type2_qual.getUnqualifiedType();
1318 }
1319
1320 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001321}
1322
Kate Stoneb9c1b512016-09-06 20:57:50 +00001323CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1324 if (clang::ObjCInterfaceDecl *interface_decl =
1325 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1326 return GetTypeForDecl(interface_decl);
1327 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1328 return GetTypeForDecl(tag_decl);
1329 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001330}
1331
Kate Stoneb9c1b512016-09-06 20:57:50 +00001332CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001333 // No need to call the getASTContext() accessor (which can create the AST if
1334 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001335 // AST if our AST didn't already exist...
1336 ASTContext *ast = &decl->getASTContext();
1337 if (ast)
1338 return CompilerType(ast, ast->getTagDeclType(decl));
1339 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001340}
1341
Kate Stoneb9c1b512016-09-06 20:57:50 +00001342CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001343 // No need to call the getASTContext() accessor (which can create the AST if
1344 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001345 // AST if our AST didn't already exist...
1346 ASTContext *ast = &decl->getASTContext();
1347 if (ast)
1348 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1349 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001350}
1351
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001352#pragma mark Structure, Unions, Classes
1353
Kate Stoneb9c1b512016-09-06 20:57:50 +00001354CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1355 AccessType access_type,
1356 const char *name, int kind,
1357 LanguageType language,
1358 ClangASTMetadata *metadata) {
1359 ASTContext *ast = getASTContext();
1360 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001361
Kate Stoneb9c1b512016-09-06 20:57:50 +00001362 if (decl_ctx == nullptr)
1363 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001364
Kate Stoneb9c1b512016-09-06 20:57:50 +00001365 if (language == eLanguageTypeObjC ||
1366 language == eLanguageTypeObjC_plus_plus) {
1367 bool isForwardDecl = true;
1368 bool isInternal = false;
1369 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1370 }
Greg Clayton9e409562010-07-28 02:04:09 +00001371
Kate Stoneb9c1b512016-09-06 20:57:50 +00001372 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
Adrian Prantl05097242018-04-30 16:49:04 +00001373 // we will need to update this code. I was told to currently always use the
1374 // CXXRecordDecl class since we often don't know from debug information if
1375 // something is struct or a class, so we default to always use the more
Kate Stoneb9c1b512016-09-06 20:57:50 +00001376 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001377
Kate Stoneb9c1b512016-09-06 20:57:50 +00001378 bool is_anonymous = (!name) || (!name[0]);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001379
Kate Stoneb9c1b512016-09-06 20:57:50 +00001380 CXXRecordDecl *decl = CXXRecordDecl::Create(
1381 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1382 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1383
1384 if (is_anonymous)
1385 decl->setAnonymousStructOrUnion(true);
1386
1387 if (decl) {
1388 if (metadata)
1389 SetMetadata(ast, decl, *metadata);
1390
1391 if (access_type != eAccessNone)
1392 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1393
1394 if (decl_ctx)
1395 decl_ctx->addDecl(decl);
1396
1397 return CompilerType(ast, ast->getTagDeclType(decl));
1398 }
1399 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001400}
1401
Sean Callanan09e91ac2017-05-11 22:08:05 +00001402namespace {
1403 bool IsValueParam(const clang::TemplateArgument &argument) {
1404 return argument.getKind() == TemplateArgument::Integral;
1405 }
1406}
1407
Kate Stoneb9c1b512016-09-06 20:57:50 +00001408static TemplateParameterList *CreateTemplateParameterList(
1409 ASTContext *ast,
1410 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1411 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1412 const bool parameter_pack = false;
1413 const bool is_typename = false;
1414 const unsigned depth = 0;
Sean Callanan09e91ac2017-05-11 22:08:05 +00001415 const size_t num_template_params = template_param_infos.args.size();
1416 DeclContext *const decl_context =
1417 ast->getTranslationUnitDecl(); // Is this the right decl context?,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001418 for (size_t i = 0; i < num_template_params; ++i) {
1419 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001420
Kate Stoneb9c1b512016-09-06 20:57:50 +00001421 IdentifierInfo *identifier_info = nullptr;
1422 if (name && name[0])
1423 identifier_info = &ast->Idents.get(name);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001424 if (IsValueParam(template_param_infos.args[i])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001425 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001426 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001427 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1428 template_param_infos.args[i].getIntegralType(), parameter_pack,
1429 nullptr));
1430
1431 } else {
1432 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001433 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001434 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1435 is_typename, parameter_pack));
1436 }
1437 }
Eugene Zemtsova9d928c2017-09-29 03:15:08 +00001438
Sean Callanan09e91ac2017-05-11 22:08:05 +00001439 if (template_param_infos.packed_args &&
1440 template_param_infos.packed_args->args.size()) {
1441 IdentifierInfo *identifier_info = nullptr;
1442 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1443 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1444 const bool parameter_pack_true = true;
1445 if (IsValueParam(template_param_infos.packed_args->args[0])) {
1446 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1447 *ast, decl_context,
1448 SourceLocation(), SourceLocation(), depth, num_template_params,
1449 identifier_info,
1450 template_param_infos.packed_args->args[0].getIntegralType(),
1451 parameter_pack_true, nullptr));
1452 } else {
1453 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1454 *ast, decl_context,
1455 SourceLocation(), SourceLocation(), depth, num_template_params,
1456 identifier_info,
1457 is_typename, parameter_pack_true));
1458 }
1459 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001460 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1461 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1462 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1463 SourceLocation(), requires_clause);
1464 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001465}
1466
Kate Stoneb9c1b512016-09-06 20:57:50 +00001467clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1468 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1469 const char *name, const TemplateParameterInfos &template_param_infos) {
Adrian Prantld8f460e2018-05-02 16:55:16 +00001470 // /// Create a function template node.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001471 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001472
Kate Stoneb9c1b512016-09-06 20:57:50 +00001473 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001474
Kate Stoneb9c1b512016-09-06 20:57:50 +00001475 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1476 ast, template_param_infos, template_param_decls);
1477 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1478 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1479 template_param_list, func_decl);
1480
1481 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1482 i < template_param_decl_count; ++i) {
1483 // TODO: verify which decl context we should put template_param_decls into..
1484 template_param_decls[i]->setDeclContext(func_decl);
1485 }
1486
1487 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001488}
1489
Kate Stoneb9c1b512016-09-06 20:57:50 +00001490void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1491 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1492 const TemplateParameterInfos &infos) {
1493 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001494
Kate Stoneb9c1b512016-09-06 20:57:50 +00001495 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1496 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001497}
1498
Kate Stoneb9c1b512016-09-06 20:57:50 +00001499ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1500 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1501 int kind, const TemplateParameterInfos &template_param_infos) {
1502 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001503
Kate Stoneb9c1b512016-09-06 20:57:50 +00001504 ClassTemplateDecl *class_template_decl = nullptr;
1505 if (decl_ctx == nullptr)
1506 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001507
Kate Stoneb9c1b512016-09-06 20:57:50 +00001508 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1509 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001510
Kate Stoneb9c1b512016-09-06 20:57:50 +00001511 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001512
Kate Stoneb9c1b512016-09-06 20:57:50 +00001513 for (NamedDecl *decl : result) {
1514 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001515 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001516 return class_template_decl;
1517 }
1518
1519 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1520
1521 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1522 ast, template_param_infos, template_param_decls);
1523
1524 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1525 *ast, (TagDecl::TagKind)kind,
1526 decl_ctx, // What decl context do we use here? TU? The actual decl
1527 // context?
1528 SourceLocation(), SourceLocation(), &identifier_info);
1529
1530 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1531 i < template_param_decl_count; ++i) {
1532 template_param_decls[i]->setDeclContext(template_cxx_decl);
1533 }
1534
1535 // With templated classes, we say that a class is templated with
1536 // specializations, but that the bare class has no functions.
1537 // template_cxx_decl->startDefinition();
1538 // template_cxx_decl->completeDefinition();
1539
1540 class_template_decl = ClassTemplateDecl::Create(
1541 *ast,
1542 decl_ctx, // What decl context do we use here? TU? The actual decl
1543 // context?
Pavel Labath4294de32017-01-12 10:44:16 +00001544 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001545
1546 if (class_template_decl) {
1547 if (access_type != eAccessNone)
1548 class_template_decl->setAccess(
1549 ConvertAccessTypeToAccessSpecifier(access_type));
1550
1551 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1552 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1553
1554 decl_ctx->addDecl(class_template_decl);
1555
Sean Callanan5e9e1992011-10-26 01:06:27 +00001556#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001557 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001558#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001559 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001560
Kate Stoneb9c1b512016-09-06 20:57:50 +00001561 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001562}
1563
Frederic Rissf4e7e522018-04-02 16:18:32 +00001564TemplateTemplateParmDecl *
1565ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) {
1566 ASTContext *ast = getASTContext();
1567
1568 auto *decl_ctx = ast->getTranslationUnitDecl();
1569
1570 IdentifierInfo &identifier_info = ast->Idents.get(template_name);
1571 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1572
1573 ClangASTContext::TemplateParameterInfos template_param_infos;
1574 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1575 ast, template_param_infos, template_param_decls);
1576
1577 // LLDB needs to create those decls only to be able to display a
Adrian Prantl05097242018-04-30 16:49:04 +00001578 // type that includes a template template argument. Only the name matters for
1579 // this purpose, so we use dummy values for the other characterisitcs of the
1580 // type.
Frederic Rissf4e7e522018-04-02 16:18:32 +00001581 return TemplateTemplateParmDecl::Create(
1582 *ast, decl_ctx, SourceLocation(),
1583 /*Depth*/ 0, /*Position*/ 0,
1584 /*IsParameterPack*/ false, &identifier_info, template_param_list);
1585}
1586
Greg Claytonf0705c82011-10-22 03:33:13 +00001587ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001588ClangASTContext::CreateClassTemplateSpecializationDecl(
1589 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1590 const TemplateParameterInfos &template_param_infos) {
1591 ASTContext *ast = getASTContext();
Sean Callanan09e91ac2017-05-11 22:08:05 +00001592 llvm::SmallVector<clang::TemplateArgument, 2> args(
1593 template_param_infos.args.size() +
1594 (template_param_infos.packed_args ? 1 : 0));
1595 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1596 args.begin());
1597 if (template_param_infos.packed_args) {
1598 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1599 *ast, template_param_infos.packed_args->args);
1600 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001601 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1602 ClassTemplateSpecializationDecl::Create(
1603 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Sean Callanan09e91ac2017-05-11 22:08:05 +00001604 SourceLocation(), class_template_decl, args,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001605 nullptr);
1606
1607 class_template_specialization_decl->setSpecializationKind(
1608 TSK_ExplicitSpecialization);
1609
1610 return class_template_specialization_decl;
1611}
1612
1613CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1614 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1615 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001616 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001617 if (ast)
1618 return CompilerType(
1619 ast, ast->getTagDeclType(class_template_specialization_decl));
1620 }
1621 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001622}
1623
Kate Stoneb9c1b512016-09-06 20:57:50 +00001624static inline bool check_op_param(bool is_method,
1625 clang::OverloadedOperatorKind op_kind,
1626 bool unary, bool binary,
1627 uint32_t num_params) {
1628 // Special-case call since it can take any number of operands
1629 if (op_kind == OO_Call)
1630 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001631
Kate Stoneb9c1b512016-09-06 20:57:50 +00001632 // The parameter count doesn't include "this"
1633 if (is_method)
1634 ++num_params;
1635 if (num_params == 1)
1636 return unary;
1637 if (num_params == 2)
1638 return binary;
1639 else
Greg Clayton090d0982011-06-19 03:43:27 +00001640 return false;
1641}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001642
Kate Stoneb9c1b512016-09-06 20:57:50 +00001643bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1644 bool is_method, clang::OverloadedOperatorKind op_kind,
1645 uint32_t num_params) {
1646 switch (op_kind) {
1647 default:
1648 break;
1649 // C++ standard allows any number of arguments to new/delete
1650 case OO_New:
1651 case OO_Array_New:
1652 case OO_Delete:
1653 case OO_Array_Delete:
1654 return true;
1655 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001656
Kate Stoneb9c1b512016-09-06 20:57:50 +00001657#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1658 case OO_##Name: \
1659 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1660 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001661#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001662 default:
1663 break;
1664 }
1665 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001666}
1667
Greg Clayton57ee3062013-07-11 22:46:58 +00001668clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001669ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1670 clang::AccessSpecifier rhs) {
1671 // Make the access equal to the stricter of the field and the nested field's
1672 // access
1673 if (lhs == AS_none || rhs == AS_none)
1674 return AS_none;
1675 if (lhs == AS_private || rhs == AS_private)
1676 return AS_private;
1677 if (lhs == AS_protected || rhs == AS_protected)
1678 return AS_protected;
1679 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001680}
1681
Kate Stoneb9c1b512016-09-06 20:57:50 +00001682bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1683 uint32_t &bitfield_bit_size) {
1684 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001685}
1686
Kate Stoneb9c1b512016-09-06 20:57:50 +00001687bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1688 uint32_t &bitfield_bit_size) {
1689 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001690 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001691
Kate Stoneb9c1b512016-09-06 20:57:50 +00001692 if (field->isBitField()) {
1693 Expr *bit_width_expr = field->getBitWidth();
1694 if (bit_width_expr) {
1695 llvm::APSInt bit_width_apsint;
1696 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1697 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001698 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001699 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001700 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001701 }
1702 return false;
1703}
1704
1705bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1706 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001707 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001708
1709 if (!record_decl->field_empty())
1710 return true;
1711
1712 // No fields, lets check this is a CXX record and check the base classes
1713 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1714 if (cxx_record_decl) {
1715 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1716 for (base_class = cxx_record_decl->bases_begin(),
1717 base_class_end = cxx_record_decl->bases_end();
1718 base_class != base_class_end; ++base_class) {
1719 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1720 base_class->getType()->getAs<RecordType>()->getDecl());
1721 if (RecordHasFields(base_class_decl))
1722 return true;
1723 }
1724 }
1725 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001726}
1727
Adrian Prantl4e8be2c2018-06-13 16:21:24 +00001728#pragma mark Objective-C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001729
Kate Stoneb9c1b512016-09-06 20:57:50 +00001730CompilerType ClangASTContext::CreateObjCClass(const char *name,
1731 DeclContext *decl_ctx,
1732 bool isForwardDecl,
1733 bool isInternal,
1734 ClangASTMetadata *metadata) {
1735 ASTContext *ast = getASTContext();
1736 assert(ast != nullptr);
1737 assert(name && name[0]);
1738 if (decl_ctx == nullptr)
1739 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001740
Kate Stoneb9c1b512016-09-06 20:57:50 +00001741 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1742 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1743 nullptr, SourceLocation(),
1744 /*isForwardDecl,*/
1745 isInternal);
1746
1747 if (decl && metadata)
1748 SetMetadata(ast, decl, *metadata);
1749
1750 return CompilerType(ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001751}
1752
Kate Stoneb9c1b512016-09-06 20:57:50 +00001753static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1754 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) ==
1755 false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001756}
1757
Greg Clayton57ee3062013-07-11 22:46:58 +00001758uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001759ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1760 bool omit_empty_base_classes) {
1761 uint32_t num_bases = 0;
1762 if (cxx_record_decl) {
1763 if (omit_empty_base_classes) {
1764 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1765 for (base_class = cxx_record_decl->bases_begin(),
1766 base_class_end = cxx_record_decl->bases_end();
1767 base_class != base_class_end; ++base_class) {
1768 // Skip empty base classes
1769 if (omit_empty_base_classes) {
1770 if (BaseSpecifierIsEmpty(base_class))
1771 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001772 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001773 ++num_bases;
1774 }
1775 } else
1776 num_bases = cxx_record_decl->getNumBases();
1777 }
1778 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001779}
1780
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001781#pragma mark Namespace Declarations
1782
1783NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001784ClangASTContext::GetUniqueNamespaceDeclaration(const char *name,
1785 DeclContext *decl_ctx) {
1786 NamespaceDecl *namespace_decl = nullptr;
1787 ASTContext *ast = getASTContext();
1788 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1789 if (decl_ctx == nullptr)
1790 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001791
Kate Stoneb9c1b512016-09-06 20:57:50 +00001792 if (name) {
1793 IdentifierInfo &identifier_info = ast->Idents.get(name);
1794 DeclarationName decl_name(&identifier_info);
1795 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1796 for (NamedDecl *decl : result) {
1797 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1798 if (namespace_decl)
1799 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001800 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001801
1802 namespace_decl =
1803 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1804 SourceLocation(), &identifier_info, nullptr);
1805
1806 decl_ctx->addDecl(namespace_decl);
1807 } else {
1808 if (decl_ctx == translation_unit_decl) {
1809 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1810 if (namespace_decl)
1811 return namespace_decl;
1812
1813 namespace_decl =
1814 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1815 SourceLocation(), nullptr, nullptr);
1816 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1817 translation_unit_decl->addDecl(namespace_decl);
1818 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1819 } else {
1820 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1821 if (parent_namespace_decl) {
1822 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1823 if (namespace_decl)
1824 return namespace_decl;
1825 namespace_decl =
1826 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1827 SourceLocation(), nullptr, nullptr);
1828 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1829 parent_namespace_decl->addDecl(namespace_decl);
1830 assert(namespace_decl ==
1831 parent_namespace_decl->getAnonymousNamespace());
1832 } else {
1833 // BAD!!!
1834 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001835 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001836 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001837#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001838 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001839#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001840 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001841}
1842
Kate Stoneb9c1b512016-09-06 20:57:50 +00001843NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1844 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) {
1845 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1846 if (ast_ctx == nullptr)
1847 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +00001848
Kate Stoneb9c1b512016-09-06 20:57:50 +00001849 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
Siva Chandra03ff5c82016-02-05 19:10:04 +00001850}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001851
Paul Hermand628cbb2015-09-15 23:44:17 +00001852clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001853ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1854 if (ctx != nullptr) {
1855 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1856 clang::SourceLocation());
1857 ctx->addDecl(decl);
1858 return decl;
1859 }
1860 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001861}
1862
Kate Stoneb9c1b512016-09-06 20:57:50 +00001863clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1864 clang::DeclContext *right,
1865 clang::DeclContext *root) {
1866 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001867 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001868
1869 std::set<clang::DeclContext *> path_left;
1870 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1871 path_left.insert(d);
1872
1873 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1874 if (path_left.find(d) != path_left.end())
1875 return d;
1876
1877 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00001878}
1879
Kate Stoneb9c1b512016-09-06 20:57:50 +00001880clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1881 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1882 if (decl_ctx != nullptr && ns_decl != nullptr) {
1883 clang::TranslationUnitDecl *translation_unit =
1884 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1885 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1886 *getASTContext(), decl_ctx, clang::SourceLocation(),
1887 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1888 clang::SourceLocation(), ns_decl,
1889 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
1890 decl_ctx->addDecl(using_decl);
1891 return using_decl;
1892 }
1893 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001894}
1895
1896clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001897ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1898 clang::NamedDecl *target) {
1899 if (current_decl_ctx != nullptr && target != nullptr) {
1900 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1901 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
1902 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1903 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1904 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
1905 target);
1906 using_decl->addShadowDecl(shadow_decl);
1907 current_decl_ctx->addDecl(using_decl);
1908 return using_decl;
1909 }
1910 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001911}
1912
Kate Stoneb9c1b512016-09-06 20:57:50 +00001913clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
1914 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
1915 if (decl_context != nullptr) {
1916 clang::VarDecl *var_decl = clang::VarDecl::Create(
1917 *getASTContext(), decl_context, clang::SourceLocation(),
1918 clang::SourceLocation(),
1919 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
1920 nullptr, clang::SC_None);
1921 var_decl->setAccess(clang::AS_public);
1922 decl_context->addDecl(var_decl);
1923 return var_decl;
1924 }
1925 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001926}
1927
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001928lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001929ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
1930 lldb::BasicType basic_type) {
1931 switch (basic_type) {
1932 case eBasicTypeVoid:
1933 return ast->VoidTy.getAsOpaquePtr();
1934 case eBasicTypeChar:
1935 return ast->CharTy.getAsOpaquePtr();
1936 case eBasicTypeSignedChar:
1937 return ast->SignedCharTy.getAsOpaquePtr();
1938 case eBasicTypeUnsignedChar:
1939 return ast->UnsignedCharTy.getAsOpaquePtr();
1940 case eBasicTypeWChar:
1941 return ast->getWCharType().getAsOpaquePtr();
1942 case eBasicTypeSignedWChar:
1943 return ast->getSignedWCharType().getAsOpaquePtr();
1944 case eBasicTypeUnsignedWChar:
1945 return ast->getUnsignedWCharType().getAsOpaquePtr();
1946 case eBasicTypeChar16:
1947 return ast->Char16Ty.getAsOpaquePtr();
1948 case eBasicTypeChar32:
1949 return ast->Char32Ty.getAsOpaquePtr();
1950 case eBasicTypeShort:
1951 return ast->ShortTy.getAsOpaquePtr();
1952 case eBasicTypeUnsignedShort:
1953 return ast->UnsignedShortTy.getAsOpaquePtr();
1954 case eBasicTypeInt:
1955 return ast->IntTy.getAsOpaquePtr();
1956 case eBasicTypeUnsignedInt:
1957 return ast->UnsignedIntTy.getAsOpaquePtr();
1958 case eBasicTypeLong:
1959 return ast->LongTy.getAsOpaquePtr();
1960 case eBasicTypeUnsignedLong:
1961 return ast->UnsignedLongTy.getAsOpaquePtr();
1962 case eBasicTypeLongLong:
1963 return ast->LongLongTy.getAsOpaquePtr();
1964 case eBasicTypeUnsignedLongLong:
1965 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1966 case eBasicTypeInt128:
1967 return ast->Int128Ty.getAsOpaquePtr();
1968 case eBasicTypeUnsignedInt128:
1969 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1970 case eBasicTypeBool:
1971 return ast->BoolTy.getAsOpaquePtr();
1972 case eBasicTypeHalf:
1973 return ast->HalfTy.getAsOpaquePtr();
1974 case eBasicTypeFloat:
1975 return ast->FloatTy.getAsOpaquePtr();
1976 case eBasicTypeDouble:
1977 return ast->DoubleTy.getAsOpaquePtr();
1978 case eBasicTypeLongDouble:
1979 return ast->LongDoubleTy.getAsOpaquePtr();
1980 case eBasicTypeFloatComplex:
1981 return ast->FloatComplexTy.getAsOpaquePtr();
1982 case eBasicTypeDoubleComplex:
1983 return ast->DoubleComplexTy.getAsOpaquePtr();
1984 case eBasicTypeLongDoubleComplex:
1985 return ast->LongDoubleComplexTy.getAsOpaquePtr();
1986 case eBasicTypeObjCID:
1987 return ast->getObjCIdType().getAsOpaquePtr();
1988 case eBasicTypeObjCClass:
1989 return ast->getObjCClassType().getAsOpaquePtr();
1990 case eBasicTypeObjCSel:
1991 return ast->getObjCSelType().getAsOpaquePtr();
1992 case eBasicTypeNullPtr:
1993 return ast->NullPtrTy.getAsOpaquePtr();
1994 default:
1995 return nullptr;
1996 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001997}
1998
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001999#pragma mark Function Types
2000
Pavel Labath1ac2b202016-08-15 14:32:32 +00002001clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00002002ClangASTContext::GetDeclarationName(const char *name,
2003 const CompilerType &function_clang_type) {
2004 if (!name || !name[0])
2005 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002006
Kate Stoneb9c1b512016-09-06 20:57:50 +00002007 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
2008 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
2009 return DeclarationName(&getASTContext()->Idents.get(
2010 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00002011
Adrian Prantl05097242018-04-30 16:49:04 +00002012 // Check the number of operator parameters. Sometimes we have seen bad DWARF
2013 // that doesn't correctly describe operators and if we try to create a method
2014 // and add it to the class, clang will assert and crash, so we need to make
2015 // sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002016 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
2017 const clang::FunctionProtoType *function_type =
2018 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
2019 if (function_type == nullptr)
2020 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002021
Kate Stoneb9c1b512016-09-06 20:57:50 +00002022 const bool is_method = false;
2023 const unsigned int num_params = function_type->getNumParams();
2024 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
2025 is_method, op_kind, num_params))
2026 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002027
Kate Stoneb9c1b512016-09-06 20:57:50 +00002028 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00002029}
2030
Kate Stoneb9c1b512016-09-06 20:57:50 +00002031FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
2032 DeclContext *decl_ctx, const char *name,
2033 const CompilerType &function_clang_type, int storage, bool is_inline) {
2034 FunctionDecl *func_decl = nullptr;
2035 ASTContext *ast = getASTContext();
2036 if (decl_ctx == nullptr)
2037 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002038
Kate Stoneb9c1b512016-09-06 20:57:50 +00002039 const bool hasWrittenPrototype = true;
2040 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00002041
Kate Stoneb9c1b512016-09-06 20:57:50 +00002042 clang::DeclarationName declarationName =
2043 GetDeclarationName(name, function_clang_type);
2044 func_decl = FunctionDecl::Create(
2045 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2046 ClangUtil::GetQualType(function_clang_type), nullptr,
2047 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
2048 isConstexprSpecified);
2049 if (func_decl)
2050 decl_ctx->addDecl(func_decl);
2051
Sean Callanan5e9e1992011-10-26 01:06:27 +00002052#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00002053 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002054#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002055
2056 return func_decl;
2057}
2058
2059CompilerType ClangASTContext::CreateFunctionType(
2060 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002061 unsigned num_args, bool is_variadic, unsigned type_quals,
2062 clang::CallingConv cc) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002063 if (ast == nullptr)
2064 return CompilerType(); // invalid AST
2065
2066 if (!result_type || !ClangUtil::IsClangType(result_type))
2067 return CompilerType(); // invalid return type
2068
2069 std::vector<QualType> qual_type_args;
2070 if (num_args > 0 && args == nullptr)
2071 return CompilerType(); // invalid argument array passed in
2072
2073 // Verify that all arguments are valid and the right type
2074 for (unsigned i = 0; i < num_args; ++i) {
2075 if (args[i]) {
2076 // Make sure we have a clang type in args[i] and not a type from another
2077 // language whose name might match
2078 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2079 lldbassert(is_clang_type);
2080 if (is_clang_type)
2081 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2082 else
2083 return CompilerType(); // invalid argument type (must be a clang type)
2084 } else
2085 return CompilerType(); // invalid argument type (empty)
2086 }
2087
2088 // TODO: Detect calling convention in DWARF?
2089 FunctionProtoType::ExtProtoInfo proto_info;
Aleksandr Urakovbc4707c2018-09-26 09:03:34 +00002090 proto_info.ExtInfo = cc;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002091 proto_info.Variadic = is_variadic;
2092 proto_info.ExceptionSpec = EST_None;
2093 proto_info.TypeQuals = type_quals;
2094 proto_info.RefQualifier = RQ_None;
2095
2096 return CompilerType(ast,
2097 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2098 qual_type_args, proto_info));
2099}
2100
2101ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
2102 const char *name, const CompilerType &param_type, int storage) {
2103 ASTContext *ast = getASTContext();
2104 assert(ast != nullptr);
2105 return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(),
2106 SourceLocation(), SourceLocation(),
2107 name && name[0] ? &ast->Idents.get(name) : nullptr,
2108 ClangUtil::GetQualType(param_type), nullptr,
2109 (clang::StorageClass)storage, nullptr);
2110}
2111
2112void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2113 ParmVarDecl **params,
2114 unsigned num_params) {
2115 if (function_decl)
2116 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002117}
2118
Greg Claytona1e5dc82015-08-11 22:53:00 +00002119CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002120ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
2121 QualType block_type = m_ast_ap->getBlockPointerType(
2122 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002123
Kate Stoneb9c1b512016-09-06 20:57:50 +00002124 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002125}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002126
2127#pragma mark Array Types
2128
Kate Stoneb9c1b512016-09-06 20:57:50 +00002129CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2130 size_t element_count,
2131 bool is_vector) {
2132 if (element_type.IsValid()) {
2133 ASTContext *ast = getASTContext();
2134 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002135
Kate Stoneb9c1b512016-09-06 20:57:50 +00002136 if (is_vector) {
2137 return CompilerType(
2138 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2139 element_count));
2140 } else {
2141
2142 llvm::APInt ap_element_count(64, element_count);
2143 if (element_count == 0) {
2144 return CompilerType(ast, ast->getIncompleteArrayType(
2145 ClangUtil::GetQualType(element_type),
2146 clang::ArrayType::Normal, 0));
2147 } else {
2148 return CompilerType(
2149 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2150 ap_element_count,
2151 clang::ArrayType::Normal, 0));
2152 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002153 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002154 }
2155 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002156}
2157
Kate Stoneb9c1b512016-09-06 20:57:50 +00002158CompilerType ClangASTContext::CreateStructForIdentifier(
2159 const ConstString &type_name,
2160 const std::initializer_list<std::pair<const char *, CompilerType>>
2161 &type_fields,
2162 bool packed) {
2163 CompilerType type;
2164 if (!type_name.IsEmpty() &&
2165 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2166 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002167 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002168 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002169 }
2170
2171 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2172 clang::TTK_Struct, lldb::eLanguageTypeC);
2173 StartTagDeclarationDefinition(type);
2174 for (const auto &field : type_fields)
2175 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2176 0);
2177 if (packed)
2178 SetIsPacked(type);
2179 CompleteTagDeclarationDefinition(type);
2180 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002181}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002182
Kate Stoneb9c1b512016-09-06 20:57:50 +00002183CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
2184 const ConstString &type_name,
2185 const std::initializer_list<std::pair<const char *, CompilerType>>
2186 &type_fields,
2187 bool packed) {
2188 CompilerType type;
2189 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2190 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002191
Kate Stoneb9c1b512016-09-06 20:57:50 +00002192 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002193}
2194
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002195#pragma mark Enumeration Types
2196
Greg Claytona1e5dc82015-08-11 22:53:00 +00002197CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002198ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2199 const Declaration &decl,
Tamas Berghammer59765832017-11-07 10:39:22 +00002200 const CompilerType &integer_clang_type,
2201 bool is_scoped) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002202 // TODO: Do something intelligent with the Declaration object passed in
2203 // like maybe filling in the SourceLocation with it...
2204 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002205
Kate Stoneb9c1b512016-09-06 20:57:50 +00002206 // TODO: ask about these...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002207 // const bool IsFixed = false;
2208
2209 EnumDecl *enum_decl = EnumDecl::Create(
2210 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2211 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
Tamas Berghammercf6bf4c2017-11-07 13:43:55 +00002212 is_scoped, // IsScoped
2213 is_scoped, // IsScopedUsingClassTag
2214 false); // IsFixed
Kate Stoneb9c1b512016-09-06 20:57:50 +00002215
2216 if (enum_decl) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00002217 if (decl_ctx)
2218 decl_ctx->addDecl(enum_decl);
2219
Kate Stoneb9c1b512016-09-06 20:57:50 +00002220 // TODO: check if we should be setting the promotion type too?
2221 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2222
2223 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2224
2225 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2226 }
2227 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002228}
2229
Kate Stoneb9c1b512016-09-06 20:57:50 +00002230CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2231 size_t bit_size,
2232 bool is_signed) {
2233 if (ast) {
2234 if (is_signed) {
2235 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2236 return CompilerType(ast, ast->SignedCharTy);
2237
2238 if (bit_size == ast->getTypeSize(ast->ShortTy))
2239 return CompilerType(ast, ast->ShortTy);
2240
2241 if (bit_size == ast->getTypeSize(ast->IntTy))
2242 return CompilerType(ast, ast->IntTy);
2243
2244 if (bit_size == ast->getTypeSize(ast->LongTy))
2245 return CompilerType(ast, ast->LongTy);
2246
2247 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2248 return CompilerType(ast, ast->LongLongTy);
2249
2250 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2251 return CompilerType(ast, ast->Int128Ty);
2252 } else {
2253 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2254 return CompilerType(ast, ast->UnsignedCharTy);
2255
2256 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2257 return CompilerType(ast, ast->UnsignedShortTy);
2258
2259 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2260 return CompilerType(ast, ast->UnsignedIntTy);
2261
2262 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2263 return CompilerType(ast, ast->UnsignedLongTy);
2264
2265 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2266 return CompilerType(ast, ast->UnsignedLongLongTy);
2267
2268 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2269 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002270 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002271 }
2272 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002273}
2274
Kate Stoneb9c1b512016-09-06 20:57:50 +00002275CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2276 bool is_signed) {
2277 if (ast)
2278 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2279 is_signed);
2280 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002281}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002282
Kate Stoneb9c1b512016-09-06 20:57:50 +00002283void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2284 if (decl_ctx) {
2285 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002286
Kate Stoneb9c1b512016-09-06 20:57:50 +00002287 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2288 if (named_decl) {
2289 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2290 named_decl->getDeclName().getAsString().c_str());
2291 } else {
2292 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002293 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002294 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002295}
2296
Kate Stoneb9c1b512016-09-06 20:57:50 +00002297void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2298 if (decl == nullptr)
2299 return;
2300 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002301
Kate Stoneb9c1b512016-09-06 20:57:50 +00002302 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2303 if (record_decl) {
2304 printf("%20s: %s%s\n", decl->getDeclKindName(),
2305 record_decl->getDeclName().getAsString().c_str(),
2306 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002307
Kate Stoneb9c1b512016-09-06 20:57:50 +00002308 } else {
2309 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2310 if (named_decl) {
2311 printf("%20s: %s\n", decl->getDeclKindName(),
2312 named_decl->getDeclName().getAsString().c_str());
2313 } else {
2314 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002315 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002316 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002317}
2318
Kate Stoneb9c1b512016-09-06 20:57:50 +00002319bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2320 clang::Decl *rhs_decl) {
2321 if (lhs_decl && rhs_decl) {
2322 //----------------------------------------------------------------------
2323 // Make sure the decl kinds match first
2324 //----------------------------------------------------------------------
2325 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2326 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002327
Kate Stoneb9c1b512016-09-06 20:57:50 +00002328 if (lhs_decl_kind == rhs_decl_kind) {
2329 //------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00002330 // Now check that the decl contexts kinds are all equivalent before we
2331 // have to check any names of the decl contexts...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002332 //------------------------------------------------------------------
2333 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2334 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2335 if (lhs_decl_ctx && rhs_decl_ctx) {
2336 while (1) {
2337 if (lhs_decl_ctx && rhs_decl_ctx) {
2338 const clang::Decl::Kind lhs_decl_ctx_kind =
2339 lhs_decl_ctx->getDeclKind();
2340 const clang::Decl::Kind rhs_decl_ctx_kind =
2341 rhs_decl_ctx->getDeclKind();
2342 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2343 lhs_decl_ctx = lhs_decl_ctx->getParent();
2344 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002345
Kate Stoneb9c1b512016-09-06 20:57:50 +00002346 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2347 break;
2348 } else
2349 return false;
2350 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002351 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002352 }
2353
2354 //--------------------------------------------------------------
2355 // Now make sure the name of the decls match
2356 //--------------------------------------------------------------
2357 clang::NamedDecl *lhs_named_decl =
2358 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2359 clang::NamedDecl *rhs_named_decl =
2360 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2361 if (lhs_named_decl && rhs_named_decl) {
2362 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2363 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2364 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2365 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2366 return false;
2367 } else
Greg Claytona2721472011-06-25 00:44:06 +00002368 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002369 } else
2370 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002371
Kate Stoneb9c1b512016-09-06 20:57:50 +00002372 //--------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00002373 // We know that the decl context kinds all match, so now we need to
2374 // make sure the names match as well
Kate Stoneb9c1b512016-09-06 20:57:50 +00002375 //--------------------------------------------------------------
2376 lhs_decl_ctx = lhs_decl->getDeclContext();
2377 rhs_decl_ctx = rhs_decl->getDeclContext();
2378 while (1) {
2379 switch (lhs_decl_ctx->getDeclKind()) {
2380 case clang::Decl::TranslationUnit:
2381 // We don't care about the translation unit names
2382 return true;
2383 default: {
2384 clang::NamedDecl *lhs_named_decl =
2385 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2386 clang::NamedDecl *rhs_named_decl =
2387 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2388 if (lhs_named_decl && rhs_named_decl) {
2389 clang::DeclarationName lhs_decl_name =
2390 lhs_named_decl->getDeclName();
2391 clang::DeclarationName rhs_decl_name =
2392 rhs_named_decl->getDeclName();
2393 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2394 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2395 return false;
2396 } else
2397 return false;
2398 } else
2399 return false;
2400 } break;
2401 }
2402 lhs_decl_ctx = lhs_decl_ctx->getParent();
2403 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002404 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002405 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002406 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002407 }
2408 return false;
2409}
2410bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2411 clang::Decl *decl) {
2412 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002413 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002414
Kate Stoneb9c1b512016-09-06 20:57:50 +00002415 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002416
Kate Stoneb9c1b512016-09-06 20:57:50 +00002417 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002418 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002419
2420 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2421 if (tag_decl->isCompleteDefinition())
2422 return true;
2423
2424 if (!tag_decl->hasExternalLexicalStorage())
2425 return false;
2426
2427 ast_source->CompleteType(tag_decl);
2428
2429 return !tag_decl->getTypeForDecl()->isIncompleteType();
2430 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2431 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2432 if (objc_interface_decl->getDefinition())
2433 return true;
2434
2435 if (!objc_interface_decl->hasExternalLexicalStorage())
2436 return false;
2437
2438 ast_source->CompleteType(objc_interface_decl);
2439
2440 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2441 } else {
2442 return false;
2443 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002444}
2445
Kate Stoneb9c1b512016-09-06 20:57:50 +00002446void ClangASTContext::SetMetadataAsUserID(const void *object,
2447 user_id_t user_id) {
2448 ClangASTMetadata meta_data;
2449 meta_data.SetUserID(user_id);
2450 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002451}
2452
Kate Stoneb9c1b512016-09-06 20:57:50 +00002453void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2454 ClangASTMetadata &metadata) {
2455 ClangExternalASTSourceCommon *external_source =
2456 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2457
2458 if (external_source)
2459 external_source->SetMetadata(object, metadata);
2460}
2461
2462ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2463 const void *object) {
2464 ClangExternalASTSourceCommon *external_source =
2465 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2466
2467 if (external_source && external_source->HasMetadata(object))
2468 return external_source->GetMetadata(object);
2469 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002470 return nullptr;
2471}
2472
Kate Stoneb9c1b512016-09-06 20:57:50 +00002473clang::DeclContext *
2474ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2475 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2476}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002477
Kate Stoneb9c1b512016-09-06 20:57:50 +00002478clang::DeclContext *
2479ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2480 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2481}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002482
Kate Stoneb9c1b512016-09-06 20:57:50 +00002483bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2484 int kind) const {
2485 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2486 if (clang_type) {
2487 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2488 if (tag_type) {
2489 clang::TagDecl *tag_decl =
2490 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2491 if (tag_decl) {
2492 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2493 return true;
2494 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002495 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002496 }
2497 return false;
2498}
2499
2500bool ClangASTContext::SetDefaultAccessForRecordFields(
2501 clang::RecordDecl *record_decl, int default_accessibility,
2502 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2503 if (record_decl) {
2504 uint32_t field_idx;
2505 clang::RecordDecl::field_iterator field, field_end;
2506 for (field = record_decl->field_begin(),
2507 field_end = record_decl->field_end(), field_idx = 0;
2508 field != field_end; ++field, ++field_idx) {
2509 // If no accessibility was assigned, assign the correct one
2510 if (field_idx < num_assigned_accessibilities &&
2511 assigned_accessibilities[field_idx] == clang::AS_none)
2512 field->setAccess((clang::AccessSpecifier)default_accessibility);
2513 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002514 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002515 }
2516 return false;
2517}
2518
2519clang::DeclContext *
2520ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2521 return GetDeclContextForType(ClangUtil::GetQualType(type));
2522}
2523
2524clang::DeclContext *
2525ClangASTContext::GetDeclContextForType(clang::QualType type) {
2526 if (type.isNull())
2527 return nullptr;
2528
2529 clang::QualType qual_type = type.getCanonicalType();
2530 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2531 switch (type_class) {
2532 case clang::Type::ObjCInterface:
2533 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2534 ->getInterface();
2535 case clang::Type::ObjCObjectPointer:
2536 return GetDeclContextForType(
2537 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2538 ->getPointeeType());
2539 case clang::Type::Record:
2540 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2541 case clang::Type::Enum:
2542 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2543 case clang::Type::Typedef:
2544 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2545 ->getDecl()
2546 ->getUnderlyingType());
2547 case clang::Type::Auto:
2548 return GetDeclContextForType(
2549 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2550 case clang::Type::Elaborated:
2551 return GetDeclContextForType(
2552 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2553 case clang::Type::Paren:
2554 return GetDeclContextForType(
2555 llvm::cast<clang::ParenType>(qual_type)->desugar());
2556 default:
2557 break;
2558 }
2559 // No DeclContext in this type...
2560 return nullptr;
2561}
2562
2563static bool GetCompleteQualType(clang::ASTContext *ast,
2564 clang::QualType qual_type,
2565 bool allow_completion = true) {
2566 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2567 switch (type_class) {
2568 case clang::Type::ConstantArray:
2569 case clang::Type::IncompleteArray:
2570 case clang::Type::VariableArray: {
2571 const clang::ArrayType *array_type =
2572 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2573
2574 if (array_type)
2575 return GetCompleteQualType(ast, array_type->getElementType(),
2576 allow_completion);
2577 } break;
2578 case clang::Type::Record: {
2579 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2580 if (cxx_record_decl) {
2581 if (cxx_record_decl->hasExternalLexicalStorage()) {
2582 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2583 const bool fields_loaded =
2584 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2585 if (is_complete && fields_loaded)
2586 return true;
2587
2588 if (!allow_completion)
2589 return false;
2590
2591 // Call the field_begin() accessor to for it to use the external source
2592 // to load the fields...
2593 clang::ExternalASTSource *external_ast_source =
2594 ast->getExternalSource();
2595 if (external_ast_source) {
2596 external_ast_source->CompleteType(cxx_record_decl);
2597 if (cxx_record_decl->isCompleteDefinition()) {
2598 cxx_record_decl->field_begin();
2599 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2600 }
2601 }
2602 }
2603 }
2604 const clang::TagType *tag_type =
2605 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2606 return !tag_type->isIncompleteType();
2607 } break;
2608
2609 case clang::Type::Enum: {
2610 const clang::TagType *tag_type =
2611 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2612 if (tag_type) {
2613 clang::TagDecl *tag_decl = tag_type->getDecl();
2614 if (tag_decl) {
2615 if (tag_decl->getDefinition())
2616 return true;
2617
2618 if (!allow_completion)
2619 return false;
2620
2621 if (tag_decl->hasExternalLexicalStorage()) {
2622 if (ast) {
2623 clang::ExternalASTSource *external_ast_source =
2624 ast->getExternalSource();
2625 if (external_ast_source) {
2626 external_ast_source->CompleteType(tag_decl);
2627 return !tag_type->isIncompleteType();
2628 }
2629 }
2630 }
2631 return false;
2632 }
2633 }
2634
2635 } break;
2636 case clang::Type::ObjCObject:
2637 case clang::Type::ObjCInterface: {
2638 const clang::ObjCObjectType *objc_class_type =
2639 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2640 if (objc_class_type) {
2641 clang::ObjCInterfaceDecl *class_interface_decl =
2642 objc_class_type->getInterface();
2643 // We currently can't complete objective C types through the newly added
Adrian Prantl05097242018-04-30 16:49:04 +00002644 // ASTContext because it only supports TagDecl objects right now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002645 if (class_interface_decl) {
2646 if (class_interface_decl->getDefinition())
2647 return true;
2648
2649 if (!allow_completion)
2650 return false;
2651
2652 if (class_interface_decl->hasExternalLexicalStorage()) {
2653 if (ast) {
2654 clang::ExternalASTSource *external_ast_source =
2655 ast->getExternalSource();
2656 if (external_ast_source) {
2657 external_ast_source->CompleteType(class_interface_decl);
2658 return !objc_class_type->isIncompleteType();
2659 }
2660 }
2661 }
2662 return false;
2663 }
2664 }
2665 } break;
2666
2667 case clang::Type::Typedef:
2668 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2669 ->getDecl()
2670 ->getUnderlyingType(),
2671 allow_completion);
2672
2673 case clang::Type::Auto:
2674 return GetCompleteQualType(
2675 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2676 allow_completion);
2677
2678 case clang::Type::Elaborated:
2679 return GetCompleteQualType(
2680 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2681 allow_completion);
2682
2683 case clang::Type::Paren:
2684 return GetCompleteQualType(
2685 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2686 allow_completion);
2687
2688 case clang::Type::Attributed:
2689 return GetCompleteQualType(
2690 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2691 allow_completion);
2692
2693 default:
2694 break;
2695 }
2696
2697 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002698}
2699
2700static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002701ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2702 switch (access) {
2703 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002704 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002705 case eAccessPublic:
2706 return clang::ObjCIvarDecl::Public;
2707 case eAccessPrivate:
2708 return clang::ObjCIvarDecl::Private;
2709 case eAccessProtected:
2710 return clang::ObjCIvarDecl::Protected;
2711 case eAccessPackage:
2712 return clang::ObjCIvarDecl::Package;
2713 }
2714 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002715}
2716
Greg Claytond8d4a572015-08-11 21:38:15 +00002717//----------------------------------------------------------------------
2718// Tests
2719//----------------------------------------------------------------------
2720
Kate Stoneb9c1b512016-09-06 20:57:50 +00002721bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2722 clang::QualType qual_type(GetCanonicalQualType(type));
2723
2724 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2725 switch (type_class) {
2726 case clang::Type::IncompleteArray:
2727 case clang::Type::VariableArray:
2728 case clang::Type::ConstantArray:
2729 case clang::Type::ExtVector:
2730 case clang::Type::Vector:
2731 case clang::Type::Record:
2732 case clang::Type::ObjCObject:
2733 case clang::Type::ObjCInterface:
2734 return true;
2735 case clang::Type::Auto:
2736 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2737 ->getDeducedType()
2738 .getAsOpaquePtr());
2739 case clang::Type::Elaborated:
2740 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2741 ->getNamedType()
2742 .getAsOpaquePtr());
2743 case clang::Type::Typedef:
2744 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2745 ->getDecl()
2746 ->getUnderlyingType()
2747 .getAsOpaquePtr());
2748 case clang::Type::Paren:
2749 return IsAggregateType(
2750 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2751 default:
2752 break;
2753 }
2754 // The clang type does have a value
2755 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002756}
2757
Kate Stoneb9c1b512016-09-06 20:57:50 +00002758bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2759 clang::QualType qual_type(GetCanonicalQualType(type));
2760
2761 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2762 switch (type_class) {
2763 case clang::Type::Record: {
2764 if (const clang::RecordType *record_type =
2765 llvm::dyn_cast_or_null<clang::RecordType>(
2766 qual_type.getTypePtrOrNull())) {
2767 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2768 return record_decl->isAnonymousStructOrUnion();
2769 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002770 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002771 break;
2772 }
2773 case clang::Type::Auto:
2774 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2775 ->getDeducedType()
2776 .getAsOpaquePtr());
2777 case clang::Type::Elaborated:
2778 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2779 ->getNamedType()
2780 .getAsOpaquePtr());
2781 case clang::Type::Typedef:
2782 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2783 ->getDecl()
2784 ->getUnderlyingType()
2785 .getAsOpaquePtr());
2786 case clang::Type::Paren:
2787 return IsAnonymousType(
2788 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2789 default:
2790 break;
2791 }
2792 // The clang type does have a value
2793 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002794}
2795
Kate Stoneb9c1b512016-09-06 20:57:50 +00002796bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2797 CompilerType *element_type_ptr,
2798 uint64_t *size, bool *is_incomplete) {
2799 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002800
Kate Stoneb9c1b512016-09-06 20:57:50 +00002801 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2802 switch (type_class) {
2803 default:
2804 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002805
Kate Stoneb9c1b512016-09-06 20:57:50 +00002806 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002807 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002808 element_type_ptr->SetCompilerType(
2809 getASTContext(),
2810 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002811 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002812 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2813 ->getSize()
2814 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002815 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002816 *is_incomplete = false;
2817 return true;
2818
2819 case clang::Type::IncompleteArray:
2820 if (element_type_ptr)
2821 element_type_ptr->SetCompilerType(
2822 getASTContext(),
2823 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2824 if (size)
2825 *size = 0;
2826 if (is_incomplete)
2827 *is_incomplete = true;
2828 return true;
2829
2830 case clang::Type::VariableArray:
2831 if (element_type_ptr)
2832 element_type_ptr->SetCompilerType(
2833 getASTContext(),
2834 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2835 if (size)
2836 *size = 0;
2837 if (is_incomplete)
2838 *is_incomplete = false;
2839 return true;
2840
2841 case clang::Type::DependentSizedArray:
2842 if (element_type_ptr)
2843 element_type_ptr->SetCompilerType(
2844 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2845 ->getElementType());
2846 if (size)
2847 *size = 0;
2848 if (is_incomplete)
2849 *is_incomplete = false;
2850 return true;
2851
2852 case clang::Type::Typedef:
2853 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2854 ->getDecl()
2855 ->getUnderlyingType()
2856 .getAsOpaquePtr(),
2857 element_type_ptr, size, is_incomplete);
2858 case clang::Type::Auto:
2859 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2860 ->getDeducedType()
2861 .getAsOpaquePtr(),
2862 element_type_ptr, size, is_incomplete);
2863 case clang::Type::Elaborated:
2864 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2865 ->getNamedType()
2866 .getAsOpaquePtr(),
2867 element_type_ptr, size, is_incomplete);
2868 case clang::Type::Paren:
2869 return IsArrayType(
2870 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2871 element_type_ptr, size, is_incomplete);
2872 }
2873 if (element_type_ptr)
2874 element_type_ptr->Clear();
2875 if (size)
2876 *size = 0;
2877 if (is_incomplete)
2878 *is_incomplete = false;
2879 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002880}
2881
Kate Stoneb9c1b512016-09-06 20:57:50 +00002882bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2883 CompilerType *element_type, uint64_t *size) {
2884 clang::QualType qual_type(GetCanonicalQualType(type));
2885
2886 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2887 switch (type_class) {
2888 case clang::Type::Vector: {
2889 const clang::VectorType *vector_type =
2890 qual_type->getAs<clang::VectorType>();
2891 if (vector_type) {
2892 if (size)
2893 *size = vector_type->getNumElements();
2894 if (element_type)
2895 *element_type =
2896 CompilerType(getASTContext(), vector_type->getElementType());
2897 }
2898 return true;
2899 } break;
2900 case clang::Type::ExtVector: {
2901 const clang::ExtVectorType *ext_vector_type =
2902 qual_type->getAs<clang::ExtVectorType>();
2903 if (ext_vector_type) {
2904 if (size)
2905 *size = ext_vector_type->getNumElements();
2906 if (element_type)
2907 *element_type =
2908 CompilerType(getASTContext(), ext_vector_type->getElementType());
2909 }
2910 return true;
2911 }
2912 default:
2913 break;
2914 }
2915 return false;
2916}
2917
2918bool ClangASTContext::IsRuntimeGeneratedType(
2919 lldb::opaque_compiler_type_t type) {
2920 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
2921 ->GetDeclContextForType(GetQualType(type));
2922 if (!decl_ctx)
2923 return false;
2924
2925 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2926 return false;
2927
2928 clang::ObjCInterfaceDecl *result_iface_decl =
2929 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2930
2931 ClangASTMetadata *ast_metadata =
2932 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2933 if (!ast_metadata)
2934 return false;
2935 return (ast_metadata->GetISAPtr() != 0);
2936}
2937
2938bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
2939 return GetQualType(type).getUnqualifiedType()->isCharType();
2940}
2941
2942bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
2943 const bool allow_completion = false;
2944 return GetCompleteQualType(getASTContext(), GetQualType(type),
2945 allow_completion);
2946}
2947
2948bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
2949 return GetQualType(type).isConstQualified();
2950}
2951
2952bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
2953 uint32_t &length) {
2954 CompilerType pointee_or_element_clang_type;
2955 length = 0;
2956 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2957
2958 if (!pointee_or_element_clang_type.IsValid())
2959 return false;
2960
2961 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2962 if (pointee_or_element_clang_type.IsCharType()) {
2963 if (type_flags.Test(eTypeIsArray)) {
Adrian Prantl05097242018-04-30 16:49:04 +00002964 // We know the size of the array and it could be a C string since it is
2965 // an array of characters
Kate Stoneb9c1b512016-09-06 20:57:50 +00002966 length = llvm::cast<clang::ConstantArrayType>(
2967 GetCanonicalQualType(type).getTypePtr())
2968 ->getSize()
2969 .getLimitedValue();
2970 }
2971 return true;
2972 }
2973 }
2974 return false;
2975}
2976
2977bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
2978 bool *is_variadic_ptr) {
2979 if (type) {
2980 clang::QualType qual_type(GetCanonicalQualType(type));
2981
2982 if (qual_type->isFunctionType()) {
2983 if (is_variadic_ptr) {
2984 const clang::FunctionProtoType *function_proto_type =
2985 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2986 if (function_proto_type)
2987 *is_variadic_ptr = function_proto_type->isVariadic();
2988 else
2989 *is_variadic_ptr = false;
2990 }
2991 return true;
2992 }
2993
Greg Claytond8d4a572015-08-11 21:38:15 +00002994 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002995 switch (type_class) {
2996 default:
2997 break;
2998 case clang::Type::Typedef:
2999 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
3000 ->getDecl()
3001 ->getUnderlyingType()
3002 .getAsOpaquePtr(),
3003 nullptr);
3004 case clang::Type::Auto:
3005 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3006 ->getDeducedType()
3007 .getAsOpaquePtr(),
3008 nullptr);
3009 case clang::Type::Elaborated:
3010 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3011 ->getNamedType()
3012 .getAsOpaquePtr(),
3013 nullptr);
3014 case clang::Type::Paren:
3015 return IsFunctionType(
3016 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3017 nullptr);
3018 case clang::Type::LValueReference:
3019 case clang::Type::RValueReference: {
3020 const clang::ReferenceType *reference_type =
3021 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3022 if (reference_type)
3023 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3024 nullptr);
3025 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00003026 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003027 }
3028 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003029}
3030
3031// Used to detect "Homogeneous Floating-point Aggregates"
3032uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003033ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3034 CompilerType *base_type_ptr) {
3035 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003036 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003037
3038 clang::QualType qual_type(GetCanonicalQualType(type));
3039 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3040 switch (type_class) {
3041 case clang::Type::Record:
3042 if (GetCompleteType(type)) {
3043 const clang::CXXRecordDecl *cxx_record_decl =
3044 qual_type->getAsCXXRecordDecl();
3045 if (cxx_record_decl) {
3046 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3047 return 0;
3048 }
3049 const clang::RecordType *record_type =
3050 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3051 if (record_type) {
3052 const clang::RecordDecl *record_decl = record_type->getDecl();
3053 if (record_decl) {
3054 // We are looking for a structure that contains only floating point
3055 // types
3056 clang::RecordDecl::field_iterator field_pos,
3057 field_end = record_decl->field_end();
3058 uint32_t num_fields = 0;
3059 bool is_hva = false;
3060 bool is_hfa = false;
3061 clang::QualType base_qual_type;
3062 uint64_t base_bitwidth = 0;
3063 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3064 ++field_pos) {
3065 clang::QualType field_qual_type = field_pos->getType();
3066 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3067 if (field_qual_type->isFloatingType()) {
3068 if (field_qual_type->isComplexType())
3069 return 0;
3070 else {
3071 if (num_fields == 0)
3072 base_qual_type = field_qual_type;
3073 else {
3074 if (is_hva)
3075 return 0;
3076 is_hfa = true;
3077 if (field_qual_type.getTypePtr() !=
3078 base_qual_type.getTypePtr())
3079 return 0;
3080 }
3081 }
3082 } else if (field_qual_type->isVectorType() ||
3083 field_qual_type->isExtVectorType()) {
3084 if (num_fields == 0) {
3085 base_qual_type = field_qual_type;
3086 base_bitwidth = field_bitwidth;
3087 } else {
3088 if (is_hfa)
3089 return 0;
3090 is_hva = true;
3091 if (base_bitwidth != field_bitwidth)
3092 return 0;
3093 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3094 return 0;
3095 }
3096 } else
3097 return 0;
3098 ++num_fields;
3099 }
3100 if (base_type_ptr)
3101 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3102 return num_fields;
3103 }
3104 }
3105 }
3106 break;
3107
3108 case clang::Type::Typedef:
3109 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3110 ->getDecl()
3111 ->getUnderlyingType()
3112 .getAsOpaquePtr(),
3113 base_type_ptr);
3114
3115 case clang::Type::Auto:
3116 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3117 ->getDeducedType()
3118 .getAsOpaquePtr(),
3119 base_type_ptr);
3120
3121 case clang::Type::Elaborated:
3122 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3123 ->getNamedType()
3124 .getAsOpaquePtr(),
3125 base_type_ptr);
3126 default:
3127 break;
3128 }
3129 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003130}
3131
Kate Stoneb9c1b512016-09-06 20:57:50 +00003132size_t ClangASTContext::GetNumberOfFunctionArguments(
3133 lldb::opaque_compiler_type_t type) {
3134 if (type) {
3135 clang::QualType qual_type(GetCanonicalQualType(type));
3136 const clang::FunctionProtoType *func =
3137 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3138 if (func)
3139 return func->getNumParams();
3140 }
3141 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003142}
3143
Greg Claytona1e5dc82015-08-11 22:53:00 +00003144CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003145ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3146 const size_t index) {
3147 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003148 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003149 const clang::FunctionProtoType *func =
3150 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3151 if (func) {
3152 if (index < func->getNumParams())
3153 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003154 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003155 }
3156 return CompilerType();
3157}
3158
3159bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3160 if (type) {
3161 clang::QualType qual_type(GetCanonicalQualType(type));
3162
3163 if (qual_type->isFunctionPointerType())
3164 return true;
3165
3166 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3167 switch (type_class) {
3168 default:
3169 break;
3170 case clang::Type::Typedef:
3171 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3172 ->getDecl()
3173 ->getUnderlyingType()
3174 .getAsOpaquePtr());
3175 case clang::Type::Auto:
3176 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3177 ->getDeducedType()
3178 .getAsOpaquePtr());
3179 case clang::Type::Elaborated:
3180 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3181 ->getNamedType()
3182 .getAsOpaquePtr());
3183 case clang::Type::Paren:
3184 return IsFunctionPointerType(
3185 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3186
3187 case clang::Type::LValueReference:
3188 case clang::Type::RValueReference: {
3189 const clang::ReferenceType *reference_type =
3190 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3191 if (reference_type)
3192 return IsFunctionPointerType(
3193 reference_type->getPointeeType().getAsOpaquePtr());
3194 } break;
3195 }
3196 }
3197 return false;
3198}
3199
3200bool ClangASTContext::IsBlockPointerType(
3201 lldb::opaque_compiler_type_t type,
3202 CompilerType *function_pointer_type_ptr) {
3203 if (type) {
3204 clang::QualType qual_type(GetCanonicalQualType(type));
3205
3206 if (qual_type->isBlockPointerType()) {
3207 if (function_pointer_type_ptr) {
3208 const clang::BlockPointerType *block_pointer_type =
3209 qual_type->getAs<clang::BlockPointerType>();
3210 QualType pointee_type = block_pointer_type->getPointeeType();
3211 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3212 *function_pointer_type_ptr =
3213 CompilerType(getASTContext(), function_pointer_type);
3214 }
3215 return true;
3216 }
3217
3218 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3219 switch (type_class) {
3220 default:
3221 break;
3222 case clang::Type::Typedef:
3223 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3224 ->getDecl()
3225 ->getUnderlyingType()
3226 .getAsOpaquePtr(),
3227 function_pointer_type_ptr);
3228 case clang::Type::Auto:
3229 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3230 ->getDeducedType()
3231 .getAsOpaquePtr(),
3232 function_pointer_type_ptr);
3233 case clang::Type::Elaborated:
3234 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3235 ->getNamedType()
3236 .getAsOpaquePtr(),
3237 function_pointer_type_ptr);
3238 case clang::Type::Paren:
3239 return IsBlockPointerType(
3240 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3241 function_pointer_type_ptr);
3242
3243 case clang::Type::LValueReference:
3244 case clang::Type::RValueReference: {
3245 const clang::ReferenceType *reference_type =
3246 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3247 if (reference_type)
3248 return IsBlockPointerType(
3249 reference_type->getPointeeType().getAsOpaquePtr(),
3250 function_pointer_type_ptr);
3251 } break;
3252 }
3253 }
3254 return false;
3255}
3256
3257bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3258 bool &is_signed) {
3259 if (!type)
3260 return false;
3261
3262 clang::QualType qual_type(GetCanonicalQualType(type));
3263 const clang::BuiltinType *builtin_type =
3264 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3265
3266 if (builtin_type) {
3267 if (builtin_type->isInteger()) {
3268 is_signed = builtin_type->isSignedInteger();
3269 return true;
3270 }
3271 }
3272
3273 return false;
3274}
3275
3276bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3277 bool &is_signed) {
3278 if (type) {
3279 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3280 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3281
3282 if (enum_type) {
3283 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3284 is_signed);
3285 return true;
3286 }
3287 }
3288
3289 return false;
3290}
3291
3292bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3293 CompilerType *pointee_type) {
3294 if (type) {
3295 clang::QualType qual_type(GetCanonicalQualType(type));
3296 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3297 switch (type_class) {
3298 case clang::Type::Builtin:
3299 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3300 default:
3301 break;
3302 case clang::BuiltinType::ObjCId:
3303 case clang::BuiltinType::ObjCClass:
3304 return true;
3305 }
3306 return false;
3307 case clang::Type::ObjCObjectPointer:
3308 if (pointee_type)
3309 pointee_type->SetCompilerType(
3310 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3311 ->getPointeeType());
3312 return true;
3313 case clang::Type::BlockPointer:
3314 if (pointee_type)
3315 pointee_type->SetCompilerType(
3316 getASTContext(),
3317 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3318 return true;
3319 case clang::Type::Pointer:
3320 if (pointee_type)
3321 pointee_type->SetCompilerType(
3322 getASTContext(),
3323 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3324 return true;
3325 case clang::Type::MemberPointer:
3326 if (pointee_type)
3327 pointee_type->SetCompilerType(
3328 getASTContext(),
3329 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3330 return true;
3331 case clang::Type::Typedef:
3332 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3333 ->getDecl()
3334 ->getUnderlyingType()
3335 .getAsOpaquePtr(),
3336 pointee_type);
3337 case clang::Type::Auto:
3338 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3339 ->getDeducedType()
3340 .getAsOpaquePtr(),
3341 pointee_type);
3342 case clang::Type::Elaborated:
3343 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3344 ->getNamedType()
3345 .getAsOpaquePtr(),
3346 pointee_type);
3347 case clang::Type::Paren:
3348 return IsPointerType(
3349 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3350 pointee_type);
3351 default:
3352 break;
3353 }
3354 }
3355 if (pointee_type)
3356 pointee_type->Clear();
3357 return false;
3358}
3359
3360bool ClangASTContext::IsPointerOrReferenceType(
3361 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3362 if (type) {
3363 clang::QualType qual_type(GetCanonicalQualType(type));
3364 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3365 switch (type_class) {
3366 case clang::Type::Builtin:
3367 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3368 default:
3369 break;
3370 case clang::BuiltinType::ObjCId:
3371 case clang::BuiltinType::ObjCClass:
3372 return true;
3373 }
3374 return false;
3375 case clang::Type::ObjCObjectPointer:
3376 if (pointee_type)
3377 pointee_type->SetCompilerType(
3378 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3379 ->getPointeeType());
3380 return true;
3381 case clang::Type::BlockPointer:
3382 if (pointee_type)
3383 pointee_type->SetCompilerType(
3384 getASTContext(),
3385 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3386 return true;
3387 case clang::Type::Pointer:
3388 if (pointee_type)
3389 pointee_type->SetCompilerType(
3390 getASTContext(),
3391 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3392 return true;
3393 case clang::Type::MemberPointer:
3394 if (pointee_type)
3395 pointee_type->SetCompilerType(
3396 getASTContext(),
3397 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3398 return true;
3399 case clang::Type::LValueReference:
3400 if (pointee_type)
3401 pointee_type->SetCompilerType(
3402 getASTContext(),
3403 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3404 return true;
3405 case clang::Type::RValueReference:
3406 if (pointee_type)
3407 pointee_type->SetCompilerType(
3408 getASTContext(),
3409 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3410 return true;
3411 case clang::Type::Typedef:
3412 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3413 ->getDecl()
3414 ->getUnderlyingType()
3415 .getAsOpaquePtr(),
3416 pointee_type);
3417 case clang::Type::Auto:
3418 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3419 ->getDeducedType()
3420 .getAsOpaquePtr(),
3421 pointee_type);
3422 case clang::Type::Elaborated:
3423 return IsPointerOrReferenceType(
3424 llvm::cast<clang::ElaboratedType>(qual_type)
3425 ->getNamedType()
3426 .getAsOpaquePtr(),
3427 pointee_type);
3428 case clang::Type::Paren:
3429 return IsPointerOrReferenceType(
3430 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3431 pointee_type);
3432 default:
3433 break;
3434 }
3435 }
3436 if (pointee_type)
3437 pointee_type->Clear();
3438 return false;
3439}
3440
3441bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3442 CompilerType *pointee_type,
3443 bool *is_rvalue) {
3444 if (type) {
3445 clang::QualType qual_type(GetCanonicalQualType(type));
3446 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3447
3448 switch (type_class) {
3449 case clang::Type::LValueReference:
3450 if (pointee_type)
3451 pointee_type->SetCompilerType(
3452 getASTContext(),
3453 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3454 if (is_rvalue)
3455 *is_rvalue = false;
3456 return true;
3457 case clang::Type::RValueReference:
3458 if (pointee_type)
3459 pointee_type->SetCompilerType(
3460 getASTContext(),
3461 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3462 if (is_rvalue)
3463 *is_rvalue = true;
3464 return true;
3465 case clang::Type::Typedef:
3466 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3467 ->getDecl()
3468 ->getUnderlyingType()
3469 .getAsOpaquePtr(),
3470 pointee_type, is_rvalue);
3471 case clang::Type::Auto:
3472 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3473 ->getDeducedType()
3474 .getAsOpaquePtr(),
3475 pointee_type, is_rvalue);
3476 case clang::Type::Elaborated:
3477 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3478 ->getNamedType()
3479 .getAsOpaquePtr(),
3480 pointee_type, is_rvalue);
3481 case clang::Type::Paren:
3482 return IsReferenceType(
3483 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3484 pointee_type, is_rvalue);
3485
3486 default:
3487 break;
3488 }
3489 }
3490 if (pointee_type)
3491 pointee_type->Clear();
3492 return false;
3493}
3494
3495bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3496 uint32_t &count, bool &is_complex) {
3497 if (type) {
3498 clang::QualType qual_type(GetCanonicalQualType(type));
3499
3500 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3501 qual_type->getCanonicalTypeInternal())) {
3502 clang::BuiltinType::Kind kind = BT->getKind();
3503 if (kind >= clang::BuiltinType::Float &&
3504 kind <= clang::BuiltinType::LongDouble) {
3505 count = 1;
3506 is_complex = false;
3507 return true;
3508 }
3509 } else if (const clang::ComplexType *CT =
3510 llvm::dyn_cast<clang::ComplexType>(
3511 qual_type->getCanonicalTypeInternal())) {
3512 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3513 is_complex)) {
3514 count = 2;
3515 is_complex = true;
3516 return true;
3517 }
3518 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3519 qual_type->getCanonicalTypeInternal())) {
3520 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3521 is_complex)) {
3522 count = VT->getNumElements();
3523 is_complex = false;
3524 return true;
3525 }
3526 }
3527 }
3528 count = 0;
3529 is_complex = false;
3530 return false;
3531}
3532
3533bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3534 if (!type)
3535 return false;
3536
3537 clang::QualType qual_type(GetQualType(type));
3538 const clang::TagType *tag_type =
3539 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3540 if (tag_type) {
3541 clang::TagDecl *tag_decl = tag_type->getDecl();
3542 if (tag_decl)
3543 return tag_decl->isCompleteDefinition();
3544 return false;
3545 } else {
3546 const clang::ObjCObjectType *objc_class_type =
3547 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3548 if (objc_class_type) {
3549 clang::ObjCInterfaceDecl *class_interface_decl =
3550 objc_class_type->getInterface();
3551 if (class_interface_decl)
3552 return class_interface_decl->getDefinition() != nullptr;
3553 return false;
3554 }
3555 }
3556 return true;
3557}
3558
3559bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3560 if (type) {
3561 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3562
3563 const clang::ObjCObjectPointerType *obj_pointer_type =
3564 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3565
3566 if (obj_pointer_type)
3567 return obj_pointer_type->isObjCClassType();
3568 }
3569 return false;
3570}
3571
3572bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3573 if (ClangUtil::IsClangType(type))
3574 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3575 return false;
3576}
3577
3578bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3579 if (!type)
3580 return false;
3581 clang::QualType qual_type(GetCanonicalQualType(type));
3582 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3583 return (type_class == clang::Type::Record);
3584}
3585
3586bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3587 if (!type)
3588 return false;
3589 clang::QualType qual_type(GetCanonicalQualType(type));
3590 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3591 return (type_class == clang::Type::Enum);
3592}
3593
3594bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3595 if (type) {
3596 clang::QualType qual_type(GetCanonicalQualType(type));
3597 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3598 switch (type_class) {
3599 case clang::Type::Record:
3600 if (GetCompleteType(type)) {
3601 const clang::RecordType *record_type =
3602 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3603 const clang::RecordDecl *record_decl = record_type->getDecl();
3604 if (record_decl) {
3605 const clang::CXXRecordDecl *cxx_record_decl =
3606 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3607 if (cxx_record_decl)
3608 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003609 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003610 }
3611 break;
3612
3613 default:
3614 break;
3615 }
3616 }
3617 return false;
3618}
3619
3620bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3621 CompilerType *dynamic_pointee_type,
3622 bool check_cplusplus,
3623 bool check_objc) {
3624 clang::QualType pointee_qual_type;
3625 if (type) {
3626 clang::QualType qual_type(GetCanonicalQualType(type));
3627 bool success = false;
3628 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3629 switch (type_class) {
3630 case clang::Type::Builtin:
3631 if (check_objc &&
3632 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3633 clang::BuiltinType::ObjCId) {
3634 if (dynamic_pointee_type)
3635 dynamic_pointee_type->SetCompilerType(this, type);
3636 return true;
3637 }
3638 break;
3639
3640 case clang::Type::ObjCObjectPointer:
3641 if (check_objc) {
3642 if (auto objc_pointee_type =
3643 qual_type->getPointeeType().getTypePtrOrNull()) {
3644 if (auto objc_object_type =
3645 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3646 objc_pointee_type)) {
3647 if (objc_object_type->isObjCClass())
3648 return false;
3649 }
3650 }
3651 if (dynamic_pointee_type)
3652 dynamic_pointee_type->SetCompilerType(
3653 getASTContext(),
3654 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3655 ->getPointeeType());
3656 return true;
3657 }
3658 break;
3659
3660 case clang::Type::Pointer:
3661 pointee_qual_type =
3662 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3663 success = true;
3664 break;
3665
3666 case clang::Type::LValueReference:
3667 case clang::Type::RValueReference:
3668 pointee_qual_type =
3669 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3670 success = true;
3671 break;
3672
3673 case clang::Type::Typedef:
3674 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3675 ->getDecl()
3676 ->getUnderlyingType()
3677 .getAsOpaquePtr(),
3678 dynamic_pointee_type, check_cplusplus,
3679 check_objc);
3680
3681 case clang::Type::Auto:
3682 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3683 ->getDeducedType()
3684 .getAsOpaquePtr(),
3685 dynamic_pointee_type, check_cplusplus,
3686 check_objc);
3687
3688 case clang::Type::Elaborated:
3689 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3690 ->getNamedType()
3691 .getAsOpaquePtr(),
3692 dynamic_pointee_type, check_cplusplus,
3693 check_objc);
3694
3695 case clang::Type::Paren:
3696 return IsPossibleDynamicType(
3697 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3698 dynamic_pointee_type, check_cplusplus, check_objc);
3699 default:
3700 break;
3701 }
3702
3703 if (success) {
3704 // Check to make sure what we are pointing too is a possible dynamic C++
Adrian Prantl05097242018-04-30 16:49:04 +00003705 // type We currently accept any "void *" (in case we have a class that
3706 // has been watered down to an opaque pointer) and virtual C++ classes.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003707 const clang::Type::TypeClass pointee_type_class =
3708 pointee_qual_type.getCanonicalType()->getTypeClass();
3709 switch (pointee_type_class) {
3710 case clang::Type::Builtin:
3711 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3712 case clang::BuiltinType::UnknownAny:
3713 case clang::BuiltinType::Void:
3714 if (dynamic_pointee_type)
3715 dynamic_pointee_type->SetCompilerType(getASTContext(),
3716 pointee_qual_type);
3717 return true;
3718 default:
3719 break;
3720 }
3721 break;
3722
3723 case clang::Type::Record:
3724 if (check_cplusplus) {
3725 clang::CXXRecordDecl *cxx_record_decl =
3726 pointee_qual_type->getAsCXXRecordDecl();
3727 if (cxx_record_decl) {
3728 bool is_complete = cxx_record_decl->isCompleteDefinition();
3729
3730 if (is_complete)
3731 success = cxx_record_decl->isDynamicClass();
3732 else {
3733 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3734 getASTContext(), cxx_record_decl);
3735 if (metadata)
3736 success = metadata->GetIsDynamicCXXType();
3737 else {
3738 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3739 .GetCompleteType();
3740 if (is_complete)
3741 success = cxx_record_decl->isDynamicClass();
3742 else
3743 success = false;
3744 }
3745 }
3746
3747 if (success) {
3748 if (dynamic_pointee_type)
3749 dynamic_pointee_type->SetCompilerType(getASTContext(),
3750 pointee_qual_type);
3751 return true;
3752 }
3753 }
3754 }
3755 break;
3756
3757 case clang::Type::ObjCObject:
3758 case clang::Type::ObjCInterface:
3759 if (check_objc) {
3760 if (dynamic_pointee_type)
3761 dynamic_pointee_type->SetCompilerType(getASTContext(),
3762 pointee_qual_type);
3763 return true;
3764 }
3765 break;
3766
3767 default:
3768 break;
3769 }
3770 }
3771 }
3772 if (dynamic_pointee_type)
3773 dynamic_pointee_type->Clear();
3774 return false;
3775}
3776
3777bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3778 if (!type)
3779 return false;
3780
3781 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3782}
3783
3784bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3785 if (!type)
3786 return false;
3787 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3788}
3789
3790bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3791 if (!type)
3792 return false;
3793 return GetCanonicalQualType(type)->isVoidType();
3794}
3795
3796bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3797 return ClangASTContextSupportsLanguage(language);
3798}
3799
3800bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3801 std::string &class_name) {
3802 if (type) {
3803 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3804 if (!qual_type.isNull()) {
3805 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3806 if (cxx_record_decl) {
3807 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3808 return true;
3809 }
3810 }
3811 }
3812 class_name.clear();
3813 return false;
3814}
3815
3816bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3817 if (!type)
3818 return false;
3819
3820 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3821 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
3822 return true;
3823 return false;
3824}
3825
3826bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3827 if (!type)
3828 return false;
3829 clang::QualType qual_type(GetCanonicalQualType(type));
3830 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3831 if (tag_type)
3832 return tag_type->isBeingDefined();
3833 return false;
3834}
3835
3836bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3837 CompilerType *class_type_ptr) {
3838 if (!type)
3839 return false;
3840
3841 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3842
3843 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3844 if (class_type_ptr) {
3845 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3846 const clang::ObjCObjectPointerType *obj_pointer_type =
3847 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3848 if (obj_pointer_type == nullptr)
3849 class_type_ptr->Clear();
3850 else
3851 class_type_ptr->SetCompilerType(
3852 type.GetTypeSystem(),
3853 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3854 .getAsOpaquePtr());
3855 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003856 }
3857 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003858 }
3859 if (class_type_ptr)
3860 class_type_ptr->Clear();
3861 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003862}
3863
Kate Stoneb9c1b512016-09-06 20:57:50 +00003864bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3865 std::string &class_name) {
3866 if (!type)
3867 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003868
Kate Stoneb9c1b512016-09-06 20:57:50 +00003869 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3870
3871 const clang::ObjCObjectType *object_type =
3872 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3873 if (object_type) {
3874 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3875 if (interface) {
3876 class_name = interface->getNameAsString();
3877 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00003878 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003879 }
3880 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003881}
3882
Greg Claytond8d4a572015-08-11 21:38:15 +00003883//----------------------------------------------------------------------
3884// Type Completion
3885//----------------------------------------------------------------------
3886
Kate Stoneb9c1b512016-09-06 20:57:50 +00003887bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3888 if (!type)
3889 return false;
3890 const bool allow_completion = true;
3891 return GetCompleteQualType(getASTContext(), GetQualType(type),
3892 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00003893}
3894
Kate Stoneb9c1b512016-09-06 20:57:50 +00003895ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
3896 std::string type_name;
3897 if (type) {
3898 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
3899 clang::QualType qual_type(GetQualType(type));
3900 printing_policy.SuppressTagKeyword = true;
3901 const clang::TypedefType *typedef_type =
3902 qual_type->getAs<clang::TypedefType>();
3903 if (typedef_type) {
3904 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3905 type_name = typedef_decl->getQualifiedNameAsString();
3906 } else {
3907 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003908 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003909 }
3910 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00003911}
3912
3913uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003914ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
3915 CompilerType *pointee_or_element_clang_type) {
3916 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003917 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003918
3919 if (pointee_or_element_clang_type)
3920 pointee_or_element_clang_type->Clear();
3921
3922 clang::QualType qual_type(GetQualType(type));
3923
3924 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3925 switch (type_class) {
Sean Callananddf802a2017-06-02 01:24:18 +00003926 case clang::Type::Attributed:
3927 return GetTypeInfo(
3928 qual_type->getAs<clang::AttributedType>()
3929 ->getModifiedType().getAsOpaquePtr(),
3930 pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003931 case clang::Type::Builtin: {
3932 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3933 qual_type->getCanonicalTypeInternal());
3934
3935 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3936 switch (builtin_type->getKind()) {
3937 case clang::BuiltinType::ObjCId:
3938 case clang::BuiltinType::ObjCClass:
3939 if (pointee_or_element_clang_type)
3940 pointee_or_element_clang_type->SetCompilerType(
3941 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
3942 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3943 break;
3944
3945 case clang::BuiltinType::ObjCSel:
3946 if (pointee_or_element_clang_type)
3947 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
3948 getASTContext()->CharTy);
3949 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3950 break;
3951
3952 case clang::BuiltinType::Bool:
3953 case clang::BuiltinType::Char_U:
3954 case clang::BuiltinType::UChar:
3955 case clang::BuiltinType::WChar_U:
3956 case clang::BuiltinType::Char16:
3957 case clang::BuiltinType::Char32:
3958 case clang::BuiltinType::UShort:
3959 case clang::BuiltinType::UInt:
3960 case clang::BuiltinType::ULong:
3961 case clang::BuiltinType::ULongLong:
3962 case clang::BuiltinType::UInt128:
3963 case clang::BuiltinType::Char_S:
3964 case clang::BuiltinType::SChar:
3965 case clang::BuiltinType::WChar_S:
3966 case clang::BuiltinType::Short:
3967 case clang::BuiltinType::Int:
3968 case clang::BuiltinType::Long:
3969 case clang::BuiltinType::LongLong:
3970 case clang::BuiltinType::Int128:
3971 case clang::BuiltinType::Float:
3972 case clang::BuiltinType::Double:
3973 case clang::BuiltinType::LongDouble:
3974 builtin_type_flags |= eTypeIsScalar;
3975 if (builtin_type->isInteger()) {
3976 builtin_type_flags |= eTypeIsInteger;
3977 if (builtin_type->isSignedInteger())
3978 builtin_type_flags |= eTypeIsSigned;
3979 } else if (builtin_type->isFloatingPoint())
3980 builtin_type_flags |= eTypeIsFloat;
3981 break;
3982 default:
3983 break;
3984 }
3985 return builtin_type_flags;
3986 }
3987
3988 case clang::Type::BlockPointer:
3989 if (pointee_or_element_clang_type)
3990 pointee_or_element_clang_type->SetCompilerType(
3991 getASTContext(), qual_type->getPointeeType());
3992 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3993
3994 case clang::Type::Complex: {
3995 uint32_t complex_type_flags =
3996 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3997 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
3998 qual_type->getCanonicalTypeInternal());
3999 if (complex_type) {
4000 clang::QualType complex_element_type(complex_type->getElementType());
4001 if (complex_element_type->isIntegerType())
4002 complex_type_flags |= eTypeIsFloat;
4003 else if (complex_element_type->isFloatingType())
4004 complex_type_flags |= eTypeIsInteger;
4005 }
4006 return complex_type_flags;
4007 } break;
4008
4009 case clang::Type::ConstantArray:
4010 case clang::Type::DependentSizedArray:
4011 case clang::Type::IncompleteArray:
4012 case clang::Type::VariableArray:
4013 if (pointee_or_element_clang_type)
4014 pointee_or_element_clang_type->SetCompilerType(
4015 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4016 ->getElementType());
4017 return eTypeHasChildren | eTypeIsArray;
4018
4019 case clang::Type::DependentName:
4020 return 0;
4021 case clang::Type::DependentSizedExtVector:
4022 return eTypeHasChildren | eTypeIsVector;
4023 case clang::Type::DependentTemplateSpecialization:
4024 return eTypeIsTemplate;
4025 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004026 return CompilerType(
4027 getASTContext(),
4028 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
4029 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004030
4031 case clang::Type::Enum:
4032 if (pointee_or_element_clang_type)
4033 pointee_or_element_clang_type->SetCompilerType(
4034 getASTContext(),
4035 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4036 return eTypeIsEnumeration | eTypeHasValue;
4037
4038 case clang::Type::Auto:
4039 return CompilerType(
4040 getASTContext(),
4041 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4042 .GetTypeInfo(pointee_or_element_clang_type);
4043 case clang::Type::Elaborated:
4044 return CompilerType(
4045 getASTContext(),
4046 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4047 .GetTypeInfo(pointee_or_element_clang_type);
4048 case clang::Type::Paren:
4049 return CompilerType(getASTContext(),
4050 llvm::cast<clang::ParenType>(qual_type)->desugar())
4051 .GetTypeInfo(pointee_or_element_clang_type);
4052
4053 case clang::Type::FunctionProto:
4054 return eTypeIsFuncPrototype | eTypeHasValue;
4055 case clang::Type::FunctionNoProto:
4056 return eTypeIsFuncPrototype | eTypeHasValue;
4057 case clang::Type::InjectedClassName:
4058 return 0;
4059
4060 case clang::Type::LValueReference:
4061 case clang::Type::RValueReference:
4062 if (pointee_or_element_clang_type)
4063 pointee_or_element_clang_type->SetCompilerType(
4064 getASTContext(),
4065 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4066 ->getPointeeType());
4067 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4068
4069 case clang::Type::MemberPointer:
4070 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4071
4072 case clang::Type::ObjCObjectPointer:
4073 if (pointee_or_element_clang_type)
4074 pointee_or_element_clang_type->SetCompilerType(
4075 getASTContext(), qual_type->getPointeeType());
4076 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4077 eTypeHasValue;
4078
4079 case clang::Type::ObjCObject:
4080 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4081 case clang::Type::ObjCInterface:
4082 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4083
4084 case clang::Type::Pointer:
4085 if (pointee_or_element_clang_type)
4086 pointee_or_element_clang_type->SetCompilerType(
4087 getASTContext(), qual_type->getPointeeType());
4088 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4089
4090 case clang::Type::Record:
4091 if (qual_type->getAsCXXRecordDecl())
4092 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4093 else
4094 return eTypeHasChildren | eTypeIsStructUnion;
4095 break;
4096 case clang::Type::SubstTemplateTypeParm:
4097 return eTypeIsTemplate;
4098 case clang::Type::TemplateTypeParm:
4099 return eTypeIsTemplate;
4100 case clang::Type::TemplateSpecialization:
4101 return eTypeIsTemplate;
4102
4103 case clang::Type::Typedef:
4104 return eTypeIsTypedef |
4105 CompilerType(getASTContext(),
4106 llvm::cast<clang::TypedefType>(qual_type)
4107 ->getDecl()
4108 ->getUnderlyingType())
4109 .GetTypeInfo(pointee_or_element_clang_type);
4110 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004111 return CompilerType(getASTContext(),
4112 llvm::cast<clang::TypeOfExprType>(qual_type)
4113 ->getUnderlyingExpr()
4114 ->getType())
4115 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004116 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004117 return CompilerType(
4118 getASTContext(),
4119 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4120 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004121 case clang::Type::UnresolvedUsing:
4122 return 0;
4123
4124 case clang::Type::ExtVector:
4125 case clang::Type::Vector: {
4126 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4127 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4128 qual_type->getCanonicalTypeInternal());
4129 if (vector_type) {
4130 if (vector_type->isIntegerType())
4131 vector_type_flags |= eTypeIsFloat;
4132 else if (vector_type->isFloatingType())
4133 vector_type_flags |= eTypeIsInteger;
4134 }
4135 return vector_type_flags;
4136 }
4137 default:
4138 return 0;
4139 }
4140 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004141}
4142
Greg Claytond8d4a572015-08-11 21:38:15 +00004143lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004144ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4145 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004146 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004147
4148 // If the type is a reference, then resolve it to what it refers to first:
4149 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4150 if (qual_type->isAnyPointerType()) {
4151 if (qual_type->isObjCObjectPointerType())
4152 return lldb::eLanguageTypeObjC;
4153
4154 clang::QualType pointee_type(qual_type->getPointeeType());
4155 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4156 return lldb::eLanguageTypeC_plus_plus;
4157 if (pointee_type->isObjCObjectOrInterfaceType())
4158 return lldb::eLanguageTypeObjC;
4159 if (pointee_type->isObjCClassType())
4160 return lldb::eLanguageTypeObjC;
4161 if (pointee_type.getTypePtr() ==
4162 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4163 return lldb::eLanguageTypeObjC;
4164 } else {
4165 if (qual_type->isObjCObjectOrInterfaceType())
4166 return lldb::eLanguageTypeObjC;
4167 if (qual_type->getAsCXXRecordDecl())
4168 return lldb::eLanguageTypeC_plus_plus;
4169 switch (qual_type->getTypeClass()) {
4170 default:
4171 break;
4172 case clang::Type::Builtin:
4173 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4174 default:
4175 case clang::BuiltinType::Void:
4176 case clang::BuiltinType::Bool:
4177 case clang::BuiltinType::Char_U:
4178 case clang::BuiltinType::UChar:
4179 case clang::BuiltinType::WChar_U:
4180 case clang::BuiltinType::Char16:
4181 case clang::BuiltinType::Char32:
4182 case clang::BuiltinType::UShort:
4183 case clang::BuiltinType::UInt:
4184 case clang::BuiltinType::ULong:
4185 case clang::BuiltinType::ULongLong:
4186 case clang::BuiltinType::UInt128:
4187 case clang::BuiltinType::Char_S:
4188 case clang::BuiltinType::SChar:
4189 case clang::BuiltinType::WChar_S:
4190 case clang::BuiltinType::Short:
4191 case clang::BuiltinType::Int:
4192 case clang::BuiltinType::Long:
4193 case clang::BuiltinType::LongLong:
4194 case clang::BuiltinType::Int128:
4195 case clang::BuiltinType::Float:
4196 case clang::BuiltinType::Double:
4197 case clang::BuiltinType::LongDouble:
4198 break;
4199
4200 case clang::BuiltinType::NullPtr:
4201 return eLanguageTypeC_plus_plus;
4202
4203 case clang::BuiltinType::ObjCId:
4204 case clang::BuiltinType::ObjCClass:
4205 case clang::BuiltinType::ObjCSel:
4206 return eLanguageTypeObjC;
4207
4208 case clang::BuiltinType::Dependent:
4209 case clang::BuiltinType::Overload:
4210 case clang::BuiltinType::BoundMember:
4211 case clang::BuiltinType::UnknownAny:
4212 break;
4213 }
4214 break;
4215 case clang::Type::Typedef:
4216 return CompilerType(getASTContext(),
4217 llvm::cast<clang::TypedefType>(qual_type)
4218 ->getDecl()
4219 ->getUnderlyingType())
4220 .GetMinimumLanguage();
4221 }
4222 }
4223 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004224}
4225
4226lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004227ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4228 if (!type)
4229 return lldb::eTypeClassInvalid;
4230
4231 clang::QualType qual_type(GetQualType(type));
4232
4233 switch (qual_type->getTypeClass()) {
4234 case clang::Type::UnaryTransform:
4235 break;
4236 case clang::Type::FunctionNoProto:
4237 return lldb::eTypeClassFunction;
4238 case clang::Type::FunctionProto:
4239 return lldb::eTypeClassFunction;
4240 case clang::Type::IncompleteArray:
4241 return lldb::eTypeClassArray;
4242 case clang::Type::VariableArray:
4243 return lldb::eTypeClassArray;
4244 case clang::Type::ConstantArray:
4245 return lldb::eTypeClassArray;
4246 case clang::Type::DependentSizedArray:
4247 return lldb::eTypeClassArray;
4248 case clang::Type::DependentSizedExtVector:
4249 return lldb::eTypeClassVector;
Fangrui Song8f284882018-07-13 22:40:40 +00004250 case clang::Type::DependentVector:
4251 return lldb::eTypeClassVector;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004252 case clang::Type::ExtVector:
4253 return lldb::eTypeClassVector;
4254 case clang::Type::Vector:
4255 return lldb::eTypeClassVector;
4256 case clang::Type::Builtin:
4257 return lldb::eTypeClassBuiltin;
4258 case clang::Type::ObjCObjectPointer:
4259 return lldb::eTypeClassObjCObjectPointer;
4260 case clang::Type::BlockPointer:
4261 return lldb::eTypeClassBlockPointer;
4262 case clang::Type::Pointer:
4263 return lldb::eTypeClassPointer;
4264 case clang::Type::LValueReference:
4265 return lldb::eTypeClassReference;
4266 case clang::Type::RValueReference:
4267 return lldb::eTypeClassReference;
4268 case clang::Type::MemberPointer:
4269 return lldb::eTypeClassMemberPointer;
4270 case clang::Type::Complex:
4271 if (qual_type->isComplexType())
4272 return lldb::eTypeClassComplexFloat;
4273 else
4274 return lldb::eTypeClassComplexInteger;
4275 case clang::Type::ObjCObject:
4276 return lldb::eTypeClassObjCObject;
4277 case clang::Type::ObjCInterface:
4278 return lldb::eTypeClassObjCInterface;
4279 case clang::Type::Record: {
4280 const clang::RecordType *record_type =
4281 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4282 const clang::RecordDecl *record_decl = record_type->getDecl();
4283 if (record_decl->isUnion())
4284 return lldb::eTypeClassUnion;
4285 else if (record_decl->isStruct())
4286 return lldb::eTypeClassStruct;
4287 else
4288 return lldb::eTypeClassClass;
4289 } break;
4290 case clang::Type::Enum:
4291 return lldb::eTypeClassEnumeration;
4292 case clang::Type::Typedef:
4293 return lldb::eTypeClassTypedef;
4294 case clang::Type::UnresolvedUsing:
4295 break;
4296 case clang::Type::Paren:
4297 return CompilerType(getASTContext(),
4298 llvm::cast<clang::ParenType>(qual_type)->desugar())
4299 .GetTypeClass();
4300 case clang::Type::Auto:
4301 return CompilerType(
4302 getASTContext(),
4303 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4304 .GetTypeClass();
4305 case clang::Type::Elaborated:
4306 return CompilerType(
4307 getASTContext(),
4308 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4309 .GetTypeClass();
4310
4311 case clang::Type::Attributed:
4312 break;
4313 case clang::Type::TemplateTypeParm:
4314 break;
4315 case clang::Type::SubstTemplateTypeParm:
4316 break;
4317 case clang::Type::SubstTemplateTypeParmPack:
4318 break;
4319 case clang::Type::InjectedClassName:
4320 break;
4321 case clang::Type::DependentName:
4322 break;
4323 case clang::Type::DependentTemplateSpecialization:
4324 break;
4325 case clang::Type::PackExpansion:
4326 break;
4327
4328 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004329 return CompilerType(getASTContext(),
4330 llvm::cast<clang::TypeOfExprType>(qual_type)
4331 ->getUnderlyingExpr()
4332 ->getType())
4333 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004334 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004335 return CompilerType(
4336 getASTContext(),
4337 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4338 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004339 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004340 return CompilerType(
4341 getASTContext(),
4342 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4343 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004344 case clang::Type::TemplateSpecialization:
4345 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004346 case clang::Type::DeducedTemplateSpecialization:
4347 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004348 case clang::Type::Atomic:
4349 break;
4350 case clang::Type::Pipe:
4351 break;
4352
4353 // pointer type decayed from an array or function type.
4354 case clang::Type::Decayed:
4355 break;
4356 case clang::Type::Adjusted:
4357 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004358 case clang::Type::ObjCTypeParam:
4359 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00004360
4361 case clang::Type::DependentAddressSpace:
4362 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004363 }
4364 // We don't know hot to display this type...
4365 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004366}
4367
Kate Stoneb9c1b512016-09-06 20:57:50 +00004368unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4369 if (type)
4370 return GetQualType(type).getQualifiers().getCVRQualifiers();
4371 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004372}
4373
4374//----------------------------------------------------------------------
4375// Creating related types
4376//----------------------------------------------------------------------
4377
Greg Claytona1e5dc82015-08-11 22:53:00 +00004378CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004379ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4380 uint64_t *stride) {
4381 if (type) {
4382 clang::QualType qual_type(GetCanonicalQualType(type));
4383
4384 const clang::Type *array_eletype =
4385 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4386
4387 if (!array_eletype)
4388 return CompilerType();
4389
4390 CompilerType element_type(getASTContext(),
4391 array_eletype->getCanonicalTypeUnqualified());
4392
4393 // TODO: the real stride will be >= this value.. find the real one!
4394 if (stride)
4395 *stride = element_type.GetByteSize(nullptr);
4396
4397 return element_type;
4398 }
4399 return CompilerType();
4400}
4401
4402CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4403 uint64_t size) {
4404 if (type) {
4405 clang::QualType qual_type(GetCanonicalQualType(type));
4406 if (clang::ASTContext *ast_ctx = getASTContext()) {
4407 if (size != 0)
4408 return CompilerType(
4409 ast_ctx, ast_ctx->getConstantArrayType(
4410 qual_type, llvm::APInt(64, size),
4411 clang::ArrayType::ArraySizeModifier::Normal, 0));
4412 else
4413 return CompilerType(
4414 ast_ctx,
4415 ast_ctx->getIncompleteArrayType(
4416 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
Greg Claytond8d4a572015-08-11 21:38:15 +00004417 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004418 }
4419
4420 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004421}
4422
Greg Claytona1e5dc82015-08-11 22:53:00 +00004423CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004424ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4425 if (type)
4426 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4427 return CompilerType();
4428}
4429
4430static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4431 clang::QualType qual_type) {
4432 if (qual_type->isPointerType())
4433 qual_type = ast->getPointerType(
4434 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4435 else
4436 qual_type = qual_type.getUnqualifiedType();
4437 qual_type.removeLocalConst();
4438 qual_type.removeLocalRestrict();
4439 qual_type.removeLocalVolatile();
4440 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004441}
4442
4443CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004444ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4445 if (type)
4446 return CompilerType(
4447 getASTContext(),
4448 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4449 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004450}
4451
Kate Stoneb9c1b512016-09-06 20:57:50 +00004452int ClangASTContext::GetFunctionArgumentCount(
4453 lldb::opaque_compiler_type_t type) {
4454 if (type) {
4455 const clang::FunctionProtoType *func =
4456 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4457 if (func)
4458 return func->getNumParams();
4459 }
4460 return -1;
4461}
4462
4463CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4464 lldb::opaque_compiler_type_t type, size_t idx) {
4465 if (type) {
4466 const clang::FunctionProtoType *func =
4467 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4468 if (func) {
4469 const uint32_t num_args = func->getNumParams();
4470 if (idx < num_args)
4471 return CompilerType(getASTContext(), func->getParamType(idx));
4472 }
4473 }
4474 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004475}
4476
Greg Claytona1e5dc82015-08-11 22:53:00 +00004477CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004478ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4479 if (type) {
4480 clang::QualType qual_type(GetQualType(type));
4481 const clang::FunctionProtoType *func =
4482 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4483 if (func)
4484 return CompilerType(getASTContext(), func->getReturnType());
4485 }
4486 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004487}
4488
4489size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004490ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4491 size_t num_functions = 0;
4492 if (type) {
4493 clang::QualType qual_type(GetCanonicalQualType(type));
4494 switch (qual_type->getTypeClass()) {
4495 case clang::Type::Record:
4496 if (GetCompleteQualType(getASTContext(), qual_type)) {
4497 const clang::RecordType *record_type =
4498 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4499 const clang::RecordDecl *record_decl = record_type->getDecl();
4500 assert(record_decl);
4501 const clang::CXXRecordDecl *cxx_record_decl =
4502 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4503 if (cxx_record_decl)
4504 num_functions = std::distance(cxx_record_decl->method_begin(),
4505 cxx_record_decl->method_end());
4506 }
4507 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004508
Sean Callananf9c622a2016-09-30 18:44:43 +00004509 case clang::Type::ObjCObjectPointer: {
4510 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004511 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004512 const clang::ObjCInterfaceType *objc_interface_type =
4513 objc_class_type->getInterfaceType();
4514 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004515 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4516 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004517 clang::ObjCInterfaceDecl *class_interface_decl =
4518 objc_interface_type->getDecl();
4519 if (class_interface_decl) {
4520 num_functions = std::distance(class_interface_decl->meth_begin(),
4521 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004522 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004523 }
4524 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004525 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004526
4527 case clang::Type::ObjCObject:
4528 case clang::Type::ObjCInterface:
4529 if (GetCompleteType(type)) {
4530 const clang::ObjCObjectType *objc_class_type =
4531 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4532 if (objc_class_type) {
4533 clang::ObjCInterfaceDecl *class_interface_decl =
4534 objc_class_type->getInterface();
4535 if (class_interface_decl)
4536 num_functions = std::distance(class_interface_decl->meth_begin(),
4537 class_interface_decl->meth_end());
4538 }
4539 }
4540 break;
4541
4542 case clang::Type::Typedef:
4543 return CompilerType(getASTContext(),
4544 llvm::cast<clang::TypedefType>(qual_type)
4545 ->getDecl()
4546 ->getUnderlyingType())
4547 .GetNumMemberFunctions();
4548
4549 case clang::Type::Auto:
4550 return CompilerType(
4551 getASTContext(),
4552 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4553 .GetNumMemberFunctions();
4554
4555 case clang::Type::Elaborated:
4556 return CompilerType(
4557 getASTContext(),
4558 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4559 .GetNumMemberFunctions();
4560
4561 case clang::Type::Paren:
4562 return CompilerType(getASTContext(),
4563 llvm::cast<clang::ParenType>(qual_type)->desugar())
4564 .GetNumMemberFunctions();
4565
4566 default:
4567 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004568 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004569 }
4570 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004571}
4572
4573TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004574ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4575 size_t idx) {
4576 std::string name;
4577 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4578 CompilerType clang_type;
4579 CompilerDecl clang_decl;
4580 if (type) {
4581 clang::QualType qual_type(GetCanonicalQualType(type));
4582 switch (qual_type->getTypeClass()) {
4583 case clang::Type::Record:
4584 if (GetCompleteQualType(getASTContext(), qual_type)) {
4585 const clang::RecordType *record_type =
4586 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4587 const clang::RecordDecl *record_decl = record_type->getDecl();
4588 assert(record_decl);
4589 const clang::CXXRecordDecl *cxx_record_decl =
4590 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4591 if (cxx_record_decl) {
4592 auto method_iter = cxx_record_decl->method_begin();
4593 auto method_end = cxx_record_decl->method_end();
4594 if (idx <
4595 static_cast<size_t>(std::distance(method_iter, method_end))) {
4596 std::advance(method_iter, idx);
4597 clang::CXXMethodDecl *cxx_method_decl =
4598 method_iter->getCanonicalDecl();
4599 if (cxx_method_decl) {
4600 name = cxx_method_decl->getDeclName().getAsString();
4601 if (cxx_method_decl->isStatic())
4602 kind = lldb::eMemberFunctionKindStaticMethod;
4603 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4604 kind = lldb::eMemberFunctionKindConstructor;
4605 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4606 kind = lldb::eMemberFunctionKindDestructor;
4607 else
4608 kind = lldb::eMemberFunctionKindInstanceMethod;
4609 clang_type = CompilerType(
4610 this, cxx_method_decl->getType().getAsOpaquePtr());
4611 clang_decl = CompilerDecl(this, cxx_method_decl);
4612 }
4613 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004614 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004615 }
4616 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004617
Sean Callananf9c622a2016-09-30 18:44:43 +00004618 case clang::Type::ObjCObjectPointer: {
4619 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004620 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004621 const clang::ObjCInterfaceType *objc_interface_type =
4622 objc_class_type->getInterfaceType();
4623 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004624 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4625 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004626 clang::ObjCInterfaceDecl *class_interface_decl =
4627 objc_interface_type->getDecl();
4628 if (class_interface_decl) {
4629 auto method_iter = class_interface_decl->meth_begin();
4630 auto method_end = class_interface_decl->meth_end();
4631 if (idx <
4632 static_cast<size_t>(std::distance(method_iter, method_end))) {
4633 std::advance(method_iter, idx);
4634 clang::ObjCMethodDecl *objc_method_decl =
4635 method_iter->getCanonicalDecl();
4636 if (objc_method_decl) {
4637 clang_decl = CompilerDecl(this, objc_method_decl);
4638 name = objc_method_decl->getSelector().getAsString();
4639 if (objc_method_decl->isClassMethod())
4640 kind = lldb::eMemberFunctionKindStaticMethod;
4641 else
4642 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004643 }
4644 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004645 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004646 }
4647 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004648 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004649
Kate Stoneb9c1b512016-09-06 20:57:50 +00004650 case clang::Type::ObjCObject:
4651 case clang::Type::ObjCInterface:
4652 if (GetCompleteType(type)) {
4653 const clang::ObjCObjectType *objc_class_type =
4654 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4655 if (objc_class_type) {
4656 clang::ObjCInterfaceDecl *class_interface_decl =
4657 objc_class_type->getInterface();
4658 if (class_interface_decl) {
4659 auto method_iter = class_interface_decl->meth_begin();
4660 auto method_end = class_interface_decl->meth_end();
4661 if (idx <
4662 static_cast<size_t>(std::distance(method_iter, method_end))) {
4663 std::advance(method_iter, idx);
4664 clang::ObjCMethodDecl *objc_method_decl =
4665 method_iter->getCanonicalDecl();
4666 if (objc_method_decl) {
4667 clang_decl = CompilerDecl(this, objc_method_decl);
4668 name = objc_method_decl->getSelector().getAsString();
4669 if (objc_method_decl->isClassMethod())
4670 kind = lldb::eMemberFunctionKindStaticMethod;
4671 else
4672 kind = lldb::eMemberFunctionKindInstanceMethod;
4673 }
4674 }
4675 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004676 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004677 }
4678 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004679
Kate Stoneb9c1b512016-09-06 20:57:50 +00004680 case clang::Type::Typedef:
4681 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4682 ->getDecl()
4683 ->getUnderlyingType()
4684 .getAsOpaquePtr(),
4685 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004686
Kate Stoneb9c1b512016-09-06 20:57:50 +00004687 case clang::Type::Auto:
4688 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4689 ->getDeducedType()
4690 .getAsOpaquePtr(),
4691 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004692
Kate Stoneb9c1b512016-09-06 20:57:50 +00004693 case clang::Type::Elaborated:
4694 return GetMemberFunctionAtIndex(
4695 llvm::cast<clang::ElaboratedType>(qual_type)
4696 ->getNamedType()
4697 .getAsOpaquePtr(),
4698 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004699
Kate Stoneb9c1b512016-09-06 20:57:50 +00004700 case clang::Type::Paren:
4701 return GetMemberFunctionAtIndex(
4702 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4703 idx);
4704
4705 default:
4706 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004707 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004708 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004709
Kate Stoneb9c1b512016-09-06 20:57:50 +00004710 if (kind == eMemberFunctionKindUnknown)
4711 return TypeMemberFunctionImpl();
4712 else
4713 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004714}
4715
Greg Claytona1e5dc82015-08-11 22:53:00 +00004716CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004717ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4718 if (type)
4719 return CompilerType(getASTContext(),
4720 GetQualType(type).getNonReferenceType());
4721 return CompilerType();
4722}
4723
4724CompilerType ClangASTContext::CreateTypedefType(
4725 const CompilerType &type, const char *typedef_name,
4726 const CompilerDeclContext &compiler_decl_ctx) {
4727 if (type && typedef_name && typedef_name[0]) {
4728 ClangASTContext *ast =
4729 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4730 if (!ast)
4731 return CompilerType();
4732 clang::ASTContext *clang_ast = ast->getASTContext();
4733 clang::QualType qual_type(ClangUtil::GetQualType(type));
4734
4735 clang::DeclContext *decl_ctx =
4736 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4737 if (decl_ctx == nullptr)
4738 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4739
4740 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4741 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4742 &clang_ast->Idents.get(typedef_name),
4743 clang_ast->getTrivialTypeSourceInfo(qual_type));
4744
4745 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4746
Aleksandr Urakov709426b2018-09-10 08:08:43 +00004747 decl_ctx->addDecl(decl);
4748
Kate Stoneb9c1b512016-09-06 20:57:50 +00004749 // Get a uniqued clang::QualType for the typedef decl type
4750 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4751 }
4752 return CompilerType();
4753}
4754
4755CompilerType
4756ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4757 if (type) {
4758 clang::QualType qual_type(GetQualType(type));
4759 return CompilerType(getASTContext(),
4760 qual_type.getTypePtr()->getPointeeType());
4761 }
4762 return CompilerType();
4763}
4764
4765CompilerType
4766ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4767 if (type) {
4768 clang::QualType qual_type(GetQualType(type));
4769
4770 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4771 switch (type_class) {
4772 case clang::Type::ObjCObject:
4773 case clang::Type::ObjCInterface:
4774 return CompilerType(getASTContext(),
4775 getASTContext()->getObjCObjectPointerType(qual_type));
4776
4777 default:
4778 return CompilerType(getASTContext(),
4779 getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004780 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004781 }
4782 return CompilerType();
4783}
4784
4785CompilerType
4786ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4787 if (type)
4788 return CompilerType(this, getASTContext()
4789 ->getLValueReferenceType(GetQualType(type))
4790 .getAsOpaquePtr());
4791 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004792 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004793}
4794
Kate Stoneb9c1b512016-09-06 20:57:50 +00004795CompilerType
4796ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4797 if (type)
4798 return CompilerType(this, getASTContext()
4799 ->getRValueReferenceType(GetQualType(type))
4800 .getAsOpaquePtr());
4801 else
4802 return CompilerType();
4803}
4804
4805CompilerType
4806ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4807 if (type) {
4808 clang::QualType result(GetQualType(type));
4809 result.addConst();
4810 return CompilerType(this, result.getAsOpaquePtr());
4811 }
4812 return CompilerType();
4813}
4814
4815CompilerType
4816ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4817 if (type) {
4818 clang::QualType result(GetQualType(type));
4819 result.addVolatile();
4820 return CompilerType(this, result.getAsOpaquePtr());
4821 }
4822 return CompilerType();
4823}
4824
4825CompilerType
4826ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4827 if (type) {
4828 clang::QualType result(GetQualType(type));
4829 result.addRestrict();
4830 return CompilerType(this, result.getAsOpaquePtr());
4831 }
4832 return CompilerType();
4833}
4834
4835CompilerType
4836ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4837 const char *typedef_name,
4838 const CompilerDeclContext &compiler_decl_ctx) {
4839 if (type) {
4840 clang::ASTContext *clang_ast = getASTContext();
4841 clang::QualType qual_type(GetQualType(type));
4842
4843 clang::DeclContext *decl_ctx =
4844 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4845 if (decl_ctx == nullptr)
4846 decl_ctx = getASTContext()->getTranslationUnitDecl();
4847
4848 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4849 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4850 &clang_ast->Idents.get(typedef_name),
4851 clang_ast->getTrivialTypeSourceInfo(qual_type));
4852
4853 clang::TagDecl *tdecl = nullptr;
4854 if (!qual_type.isNull()) {
4855 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4856 tdecl = rt->getDecl();
4857 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4858 tdecl = et->getDecl();
4859 }
4860
4861 // Check whether this declaration is an anonymous struct, union, or enum,
Adrian Prantl05097242018-04-30 16:49:04 +00004862 // hidden behind a typedef. If so, we try to check whether we have a
4863 // typedef tag to attach to the original record declaration
Kate Stoneb9c1b512016-09-06 20:57:50 +00004864 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4865 tdecl->setTypedefNameForAnonDecl(decl);
4866
4867 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4868
4869 // Get a uniqued clang::QualType for the typedef decl type
4870 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4871 }
4872 return CompilerType();
4873}
4874
4875CompilerType
4876ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4877 if (type) {
4878 const clang::TypedefType *typedef_type =
4879 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4880 if (typedef_type)
4881 return CompilerType(getASTContext(),
4882 typedef_type->getDecl()->getUnderlyingType());
4883 }
4884 return CompilerType();
4885}
Greg Claytond8d4a572015-08-11 21:38:15 +00004886
4887//----------------------------------------------------------------------
4888// Create related types using the current type's AST
4889//----------------------------------------------------------------------
4890
Kate Stoneb9c1b512016-09-06 20:57:50 +00004891CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4892 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004893}
4894//----------------------------------------------------------------------
4895// Exploring the type
4896//----------------------------------------------------------------------
4897
Kate Stoneb9c1b512016-09-06 20:57:50 +00004898uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4899 ExecutionContextScope *exe_scope) {
4900 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00004901 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004902 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004903 switch (type_class) {
4904 case clang::Type::Record:
4905 if (GetCompleteType(type))
4906 return getASTContext()->getTypeSize(qual_type);
4907 else
4908 return 0;
4909 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004910
Kate Stoneb9c1b512016-09-06 20:57:50 +00004911 case clang::Type::ObjCInterface:
4912 case clang::Type::ObjCObject: {
4913 ExecutionContext exe_ctx(exe_scope);
4914 Process *process = exe_ctx.GetProcessPtr();
4915 if (process) {
4916 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4917 if (objc_runtime) {
4918 uint64_t bit_size = 0;
4919 if (objc_runtime->GetTypeBitSize(
4920 CompilerType(getASTContext(), qual_type), bit_size))
4921 return bit_size;
4922 }
4923 } else {
4924 static bool g_printed = false;
4925 if (!g_printed) {
4926 StreamString s;
4927 DumpTypeDescription(type, &s);
4928
4929 llvm::outs() << "warning: trying to determine the size of type ";
4930 llvm::outs() << s.GetString() << "\n";
4931 llvm::outs() << "without a valid ExecutionContext. this is not "
4932 "reliable. please file a bug against LLDB.\n";
4933 llvm::outs() << "backtrace:\n";
4934 llvm::sys::PrintStackTrace(llvm::outs());
4935 llvm::outs() << "\n";
4936 g_printed = true;
4937 }
4938 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004939 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004940 LLVM_FALLTHROUGH;
4941 default:
4942 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
4943 if (bit_size == 0) {
4944 if (qual_type->isIncompleteArrayType())
4945 return getASTContext()->getTypeSize(
4946 qual_type->getArrayElementTypeNoTypeQual()
4947 ->getCanonicalTypeUnqualified());
4948 }
4949 if (qual_type->isObjCObjectOrInterfaceType())
4950 return bit_size +
4951 getASTContext()->getTypeSize(
4952 getASTContext()->ObjCBuiltinClassTy);
4953 return bit_size;
4954 }
4955 }
4956 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004957}
4958
Kate Stoneb9c1b512016-09-06 20:57:50 +00004959size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
4960 if (GetCompleteType(type))
4961 return getASTContext()->getTypeAlign(GetQualType(type));
4962 return 0;
4963}
4964
4965lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
4966 uint64_t &count) {
4967 if (!type)
4968 return lldb::eEncodingInvalid;
4969
4970 count = 1;
4971 clang::QualType qual_type(GetCanonicalQualType(type));
4972
4973 switch (qual_type->getTypeClass()) {
4974 case clang::Type::UnaryTransform:
4975 break;
4976
4977 case clang::Type::FunctionNoProto:
4978 case clang::Type::FunctionProto:
4979 break;
4980
4981 case clang::Type::IncompleteArray:
4982 case clang::Type::VariableArray:
4983 break;
4984
4985 case clang::Type::ConstantArray:
4986 break;
4987
Fangrui Song8f284882018-07-13 22:40:40 +00004988 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004989 case clang::Type::ExtVector:
4990 case clang::Type::Vector:
4991 // TODO: Set this to more than one???
4992 break;
4993
4994 case clang::Type::Builtin:
4995 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4996 case clang::BuiltinType::Void:
4997 break;
4998
4999 case clang::BuiltinType::Bool:
5000 case clang::BuiltinType::Char_S:
5001 case clang::BuiltinType::SChar:
5002 case clang::BuiltinType::WChar_S:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005003 case clang::BuiltinType::Short:
5004 case clang::BuiltinType::Int:
5005 case clang::BuiltinType::Long:
5006 case clang::BuiltinType::LongLong:
5007 case clang::BuiltinType::Int128:
5008 return lldb::eEncodingSint;
5009
5010 case clang::BuiltinType::Char_U:
5011 case clang::BuiltinType::UChar:
5012 case clang::BuiltinType::WChar_U:
Richard Smith51d12d82018-05-02 02:43:22 +00005013 case clang::BuiltinType::Char8:
5014 case clang::BuiltinType::Char16:
5015 case clang::BuiltinType::Char32:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005016 case clang::BuiltinType::UShort:
5017 case clang::BuiltinType::UInt:
5018 case clang::BuiltinType::ULong:
5019 case clang::BuiltinType::ULongLong:
5020 case clang::BuiltinType::UInt128:
5021 return lldb::eEncodingUint;
5022
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005023 // Fixed point types. Note that they are currently ignored.
5024 case clang::BuiltinType::ShortAccum:
5025 case clang::BuiltinType::Accum:
5026 case clang::BuiltinType::LongAccum:
5027 case clang::BuiltinType::UShortAccum:
5028 case clang::BuiltinType::UAccum:
5029 case clang::BuiltinType::ULongAccum:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005030 case clang::BuiltinType::ShortFract:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005031 case clang::BuiltinType::Fract:
5032 case clang::BuiltinType::LongFract:
5033 case clang::BuiltinType::UShortFract:
5034 case clang::BuiltinType::UFract:
5035 case clang::BuiltinType::ULongFract:
5036 case clang::BuiltinType::SatShortAccum:
5037 case clang::BuiltinType::SatAccum:
5038 case clang::BuiltinType::SatLongAccum:
5039 case clang::BuiltinType::SatUShortAccum:
5040 case clang::BuiltinType::SatUAccum:
5041 case clang::BuiltinType::SatULongAccum:
5042 case clang::BuiltinType::SatShortFract:
5043 case clang::BuiltinType::SatFract:
5044 case clang::BuiltinType::SatLongFract:
5045 case clang::BuiltinType::SatUShortFract:
5046 case clang::BuiltinType::SatUFract:
5047 case clang::BuiltinType::SatULongFract:
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005048 break;
5049
Kate Stoneb9c1b512016-09-06 20:57:50 +00005050 case clang::BuiltinType::Half:
5051 case clang::BuiltinType::Float:
Ted Woodward4355c7c2017-09-20 19:16:53 +00005052 case clang::BuiltinType::Float16:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005053 case clang::BuiltinType::Float128:
5054 case clang::BuiltinType::Double:
5055 case clang::BuiltinType::LongDouble:
5056 return lldb::eEncodingIEEE754;
5057
5058 case clang::BuiltinType::ObjCClass:
5059 case clang::BuiltinType::ObjCId:
5060 case clang::BuiltinType::ObjCSel:
5061 return lldb::eEncodingUint;
5062
5063 case clang::BuiltinType::NullPtr:
5064 return lldb::eEncodingUint;
5065
5066 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5067 case clang::BuiltinType::Kind::BoundMember:
5068 case clang::BuiltinType::Kind::BuiltinFn:
5069 case clang::BuiltinType::Kind::Dependent:
5070 case clang::BuiltinType::Kind::OCLClkEvent:
5071 case clang::BuiltinType::Kind::OCLEvent:
5072 case clang::BuiltinType::Kind::OCLImage1dRO:
5073 case clang::BuiltinType::Kind::OCLImage1dWO:
5074 case clang::BuiltinType::Kind::OCLImage1dRW:
5075 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5076 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5077 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5078 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5079 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5080 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5081 case clang::BuiltinType::Kind::OCLImage2dRO:
5082 case clang::BuiltinType::Kind::OCLImage2dWO:
5083 case clang::BuiltinType::Kind::OCLImage2dRW:
5084 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5085 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5086 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5087 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5088 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5089 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5090 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5091 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5092 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5093 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5094 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5095 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5096 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5097 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5098 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5099 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5100 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5101 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5102 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5103 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5104 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5105 case clang::BuiltinType::Kind::OCLImage3dRO:
5106 case clang::BuiltinType::Kind::OCLImage3dWO:
5107 case clang::BuiltinType::Kind::OCLImage3dRW:
5108 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005109 case clang::BuiltinType::Kind::OCLReserveID:
5110 case clang::BuiltinType::Kind::OCLSampler:
5111 case clang::BuiltinType::Kind::OMPArraySection:
5112 case clang::BuiltinType::Kind::Overload:
5113 case clang::BuiltinType::Kind::PseudoObject:
5114 case clang::BuiltinType::Kind::UnknownAny:
5115 break;
5116 }
5117 break;
Adrian Prantl05097242018-04-30 16:49:04 +00005118 // All pointer types are represented as unsigned integer encodings. We may
5119 // nee to add a eEncodingPointer if we ever need to know the difference
Kate Stoneb9c1b512016-09-06 20:57:50 +00005120 case clang::Type::ObjCObjectPointer:
5121 case clang::Type::BlockPointer:
5122 case clang::Type::Pointer:
5123 case clang::Type::LValueReference:
5124 case clang::Type::RValueReference:
5125 case clang::Type::MemberPointer:
5126 return lldb::eEncodingUint;
5127 case clang::Type::Complex: {
5128 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5129 if (qual_type->isComplexType())
5130 encoding = lldb::eEncodingIEEE754;
5131 else {
5132 const clang::ComplexType *complex_type =
5133 qual_type->getAsComplexIntegerType();
5134 if (complex_type)
5135 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5136 .GetEncoding(count);
5137 else
5138 encoding = lldb::eEncodingSint;
5139 }
5140 count = 2;
5141 return encoding;
5142 }
5143
5144 case clang::Type::ObjCInterface:
5145 break;
5146 case clang::Type::Record:
5147 break;
5148 case clang::Type::Enum:
5149 return lldb::eEncodingSint;
5150 case clang::Type::Typedef:
5151 return CompilerType(getASTContext(),
5152 llvm::cast<clang::TypedefType>(qual_type)
5153 ->getDecl()
5154 ->getUnderlyingType())
5155 .GetEncoding(count);
5156
5157 case clang::Type::Auto:
5158 return CompilerType(
5159 getASTContext(),
5160 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5161 .GetEncoding(count);
5162
5163 case clang::Type::Elaborated:
5164 return CompilerType(
5165 getASTContext(),
5166 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5167 .GetEncoding(count);
5168
5169 case clang::Type::Paren:
5170 return CompilerType(getASTContext(),
5171 llvm::cast<clang::ParenType>(qual_type)->desugar())
5172 .GetEncoding(count);
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005173 case clang::Type::TypeOfExpr:
5174 return CompilerType(getASTContext(),
5175 llvm::cast<clang::TypeOfExprType>(qual_type)
5176 ->getUnderlyingExpr()
5177 ->getType())
5178 .GetEncoding(count);
5179 case clang::Type::TypeOf:
5180 return CompilerType(
5181 getASTContext(),
5182 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5183 .GetEncoding(count);
5184 case clang::Type::Decltype:
5185 return CompilerType(
5186 getASTContext(),
5187 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5188 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005189 case clang::Type::DependentSizedArray:
5190 case clang::Type::DependentSizedExtVector:
5191 case clang::Type::UnresolvedUsing:
5192 case clang::Type::Attributed:
5193 case clang::Type::TemplateTypeParm:
5194 case clang::Type::SubstTemplateTypeParm:
5195 case clang::Type::SubstTemplateTypeParmPack:
5196 case clang::Type::InjectedClassName:
5197 case clang::Type::DependentName:
5198 case clang::Type::DependentTemplateSpecialization:
5199 case clang::Type::PackExpansion:
5200 case clang::Type::ObjCObject:
5201
Kate Stoneb9c1b512016-09-06 20:57:50 +00005202 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005203 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005204 case clang::Type::Atomic:
5205 case clang::Type::Adjusted:
5206 case clang::Type::Pipe:
5207 break;
5208
5209 // pointer type decayed from an array or function type.
5210 case clang::Type::Decayed:
5211 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005212 case clang::Type::ObjCTypeParam:
5213 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005214
5215 case clang::Type::DependentAddressSpace:
5216 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005217 }
5218 count = 0;
5219 return lldb::eEncodingInvalid;
5220}
5221
5222lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5223 if (!type)
5224 return lldb::eFormatDefault;
5225
5226 clang::QualType qual_type(GetCanonicalQualType(type));
5227
5228 switch (qual_type->getTypeClass()) {
5229 case clang::Type::UnaryTransform:
5230 break;
5231
5232 case clang::Type::FunctionNoProto:
5233 case clang::Type::FunctionProto:
5234 break;
5235
5236 case clang::Type::IncompleteArray:
5237 case clang::Type::VariableArray:
5238 break;
5239
5240 case clang::Type::ConstantArray:
5241 return lldb::eFormatVoid; // no value
5242
Fangrui Song8f284882018-07-13 22:40:40 +00005243 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005244 case clang::Type::ExtVector:
5245 case clang::Type::Vector:
5246 break;
5247
5248 case clang::Type::Builtin:
5249 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5250 // default: assert(0 && "Unknown builtin type!");
5251 case clang::BuiltinType::UnknownAny:
5252 case clang::BuiltinType::Void:
5253 case clang::BuiltinType::BoundMember:
5254 break;
5255
5256 case clang::BuiltinType::Bool:
5257 return lldb::eFormatBoolean;
5258 case clang::BuiltinType::Char_S:
5259 case clang::BuiltinType::SChar:
5260 case clang::BuiltinType::WChar_S:
5261 case clang::BuiltinType::Char_U:
5262 case clang::BuiltinType::UChar:
5263 case clang::BuiltinType::WChar_U:
5264 return lldb::eFormatChar;
5265 case clang::BuiltinType::Char16:
5266 return lldb::eFormatUnicode16;
5267 case clang::BuiltinType::Char32:
5268 return lldb::eFormatUnicode32;
5269 case clang::BuiltinType::UShort:
5270 return lldb::eFormatUnsigned;
5271 case clang::BuiltinType::Short:
5272 return lldb::eFormatDecimal;
5273 case clang::BuiltinType::UInt:
5274 return lldb::eFormatUnsigned;
5275 case clang::BuiltinType::Int:
5276 return lldb::eFormatDecimal;
5277 case clang::BuiltinType::ULong:
5278 return lldb::eFormatUnsigned;
5279 case clang::BuiltinType::Long:
5280 return lldb::eFormatDecimal;
5281 case clang::BuiltinType::ULongLong:
5282 return lldb::eFormatUnsigned;
5283 case clang::BuiltinType::LongLong:
5284 return lldb::eFormatDecimal;
5285 case clang::BuiltinType::UInt128:
5286 return lldb::eFormatUnsigned;
5287 case clang::BuiltinType::Int128:
5288 return lldb::eFormatDecimal;
5289 case clang::BuiltinType::Half:
5290 case clang::BuiltinType::Float:
5291 case clang::BuiltinType::Double:
5292 case clang::BuiltinType::LongDouble:
5293 return lldb::eFormatFloat;
5294 default:
5295 return lldb::eFormatHex;
5296 }
5297 break;
5298 case clang::Type::ObjCObjectPointer:
5299 return lldb::eFormatHex;
5300 case clang::Type::BlockPointer:
5301 return lldb::eFormatHex;
5302 case clang::Type::Pointer:
5303 return lldb::eFormatHex;
5304 case clang::Type::LValueReference:
5305 case clang::Type::RValueReference:
5306 return lldb::eFormatHex;
5307 case clang::Type::MemberPointer:
5308 break;
5309 case clang::Type::Complex: {
5310 if (qual_type->isComplexType())
5311 return lldb::eFormatComplex;
5312 else
5313 return lldb::eFormatComplexInteger;
5314 }
5315 case clang::Type::ObjCInterface:
5316 break;
5317 case clang::Type::Record:
5318 break;
5319 case clang::Type::Enum:
5320 return lldb::eFormatEnum;
5321 case clang::Type::Typedef:
5322 return CompilerType(getASTContext(),
5323 llvm::cast<clang::TypedefType>(qual_type)
5324 ->getDecl()
5325 ->getUnderlyingType())
5326 .GetFormat();
5327 case clang::Type::Auto:
5328 return CompilerType(getASTContext(),
5329 llvm::cast<clang::AutoType>(qual_type)->desugar())
5330 .GetFormat();
5331 case clang::Type::Paren:
5332 return CompilerType(getASTContext(),
5333 llvm::cast<clang::ParenType>(qual_type)->desugar())
5334 .GetFormat();
5335 case clang::Type::Elaborated:
5336 return CompilerType(
5337 getASTContext(),
5338 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5339 .GetFormat();
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005340 case clang::Type::TypeOfExpr:
5341 return CompilerType(getASTContext(),
5342 llvm::cast<clang::TypeOfExprType>(qual_type)
5343 ->getUnderlyingExpr()
5344 ->getType())
5345 .GetFormat();
5346 case clang::Type::TypeOf:
5347 return CompilerType(
5348 getASTContext(),
5349 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5350 .GetFormat();
5351 case clang::Type::Decltype:
5352 return CompilerType(
5353 getASTContext(),
5354 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5355 .GetFormat();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005356 case clang::Type::DependentSizedArray:
5357 case clang::Type::DependentSizedExtVector:
5358 case clang::Type::UnresolvedUsing:
5359 case clang::Type::Attributed:
5360 case clang::Type::TemplateTypeParm:
5361 case clang::Type::SubstTemplateTypeParm:
5362 case clang::Type::SubstTemplateTypeParmPack:
5363 case clang::Type::InjectedClassName:
5364 case clang::Type::DependentName:
5365 case clang::Type::DependentTemplateSpecialization:
5366 case clang::Type::PackExpansion:
5367 case clang::Type::ObjCObject:
5368
Kate Stoneb9c1b512016-09-06 20:57:50 +00005369 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005370 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005371 case clang::Type::Atomic:
5372 case clang::Type::Adjusted:
5373 case clang::Type::Pipe:
5374 break;
5375
5376 // pointer type decayed from an array or function type.
5377 case clang::Type::Decayed:
5378 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005379 case clang::Type::ObjCTypeParam:
5380 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005381
5382 case clang::Type::DependentAddressSpace:
5383 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005384 }
5385 // We don't know hot to display this type...
5386 return lldb::eFormatBytes;
5387}
5388
5389static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5390 bool check_superclass) {
5391 while (class_interface_decl) {
5392 if (class_interface_decl->ivar_size() > 0)
5393 return true;
5394
5395 if (check_superclass)
5396 class_interface_decl = class_interface_decl->getSuperClass();
5397 else
5398 break;
5399 }
5400 return false;
5401}
5402
Adrian Prantleca07c52018-11-05 20:49:07 +00005403static llvm::Optional<SymbolFile::ArrayInfo>
5404GetDynamicArrayInfo(ClangASTContext &ast, SymbolFile *sym_file,
5405 clang::QualType qual_type,
5406 const ExecutionContext *exe_ctx) {
5407 if (qual_type->isIncompleteArrayType())
5408 if (auto *metadata = ast.GetMetadata(qual_type.getAsOpaquePtr()))
5409 if (auto *dwarf_parser = ast.GetDWARFParser())
5410 return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(),
5411 exe_ctx);
5412 return llvm::None;
5413}
5414
Kate Stoneb9c1b512016-09-06 20:57:50 +00005415uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
Adrian Prantleca07c52018-11-05 20:49:07 +00005416 bool omit_empty_base_classes,
5417 const ExecutionContext *exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005418 if (!type)
5419 return 0;
5420
5421 uint32_t num_children = 0;
5422 clang::QualType qual_type(GetQualType(type));
5423 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5424 switch (type_class) {
5425 case clang::Type::Builtin:
5426 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5427 case clang::BuiltinType::ObjCId: // child is Class
5428 case clang::BuiltinType::ObjCClass: // child is Class
5429 num_children = 1;
5430 break;
5431
5432 default:
5433 break;
5434 }
5435 break;
5436
5437 case clang::Type::Complex:
5438 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005439 case clang::Type::Record:
5440 if (GetCompleteQualType(getASTContext(), qual_type)) {
5441 const clang::RecordType *record_type =
5442 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5443 const clang::RecordDecl *record_decl = record_type->getDecl();
5444 assert(record_decl);
5445 const clang::CXXRecordDecl *cxx_record_decl =
5446 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5447 if (cxx_record_decl) {
5448 if (omit_empty_base_classes) {
Adrian Prantl05097242018-04-30 16:49:04 +00005449 // Check each base classes to see if it or any of its base classes
5450 // contain any fields. This can help limit the noise in variable
5451 // views by not having to show base classes that contain no members.
Kate Stoneb9c1b512016-09-06 20:57:50 +00005452 clang::CXXRecordDecl::base_class_const_iterator base_class,
5453 base_class_end;
5454 for (base_class = cxx_record_decl->bases_begin(),
5455 base_class_end = cxx_record_decl->bases_end();
5456 base_class != base_class_end; ++base_class) {
5457 const clang::CXXRecordDecl *base_class_decl =
5458 llvm::cast<clang::CXXRecordDecl>(
5459 base_class->getType()
5460 ->getAs<clang::RecordType>()
5461 ->getDecl());
5462
5463 // Skip empty base classes
5464 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5465 continue;
5466
5467 num_children++;
5468 }
5469 } else {
5470 // Include all base classes
5471 num_children += cxx_record_decl->getNumBases();
5472 }
5473 }
5474 clang::RecordDecl::field_iterator field, field_end;
5475 for (field = record_decl->field_begin(),
5476 field_end = record_decl->field_end();
5477 field != field_end; ++field)
5478 ++num_children;
5479 }
5480 break;
5481
5482 case clang::Type::ObjCObject:
5483 case clang::Type::ObjCInterface:
5484 if (GetCompleteQualType(getASTContext(), qual_type)) {
5485 const clang::ObjCObjectType *objc_class_type =
5486 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5487 assert(objc_class_type);
5488 if (objc_class_type) {
5489 clang::ObjCInterfaceDecl *class_interface_decl =
5490 objc_class_type->getInterface();
5491
5492 if (class_interface_decl) {
5493
5494 clang::ObjCInterfaceDecl *superclass_interface_decl =
5495 class_interface_decl->getSuperClass();
5496 if (superclass_interface_decl) {
5497 if (omit_empty_base_classes) {
5498 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5499 ++num_children;
5500 } else
5501 ++num_children;
5502 }
5503
5504 num_children += class_interface_decl->ivar_size();
5505 }
5506 }
5507 }
5508 break;
5509
5510 case clang::Type::ObjCObjectPointer: {
5511 const clang::ObjCObjectPointerType *pointer_type =
5512 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5513 clang::QualType pointee_type = pointer_type->getPointeeType();
5514 uint32_t num_pointee_children =
5515 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005516 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005517 // If this type points to a simple type, then it has 1 child
5518 if (num_pointee_children == 0)
5519 num_children = 1;
5520 else
5521 num_children = num_pointee_children;
5522 } break;
5523
5524 case clang::Type::Vector:
5525 case clang::Type::ExtVector:
5526 num_children =
5527 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5528 break;
5529
5530 case clang::Type::ConstantArray:
5531 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5532 ->getSize()
5533 .getLimitedValue();
5534 break;
Adrian Prantleca07c52018-11-05 20:49:07 +00005535 case clang::Type::IncompleteArray:
5536 if (auto array_info =
5537 GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx))
5538 // Only 1-dimensional arrays are supported.
5539 num_children = array_info->element_orders.size()
5540 ? array_info->element_orders.back()
5541 : 0;
5542 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005543
5544 case clang::Type::Pointer: {
5545 const clang::PointerType *pointer_type =
5546 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5547 clang::QualType pointee_type(pointer_type->getPointeeType());
5548 uint32_t num_pointee_children =
5549 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005550 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005551 if (num_pointee_children == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00005552 // We have a pointer to a pointee type that claims it has no children. We
5553 // will want to look at
Kate Stoneb9c1b512016-09-06 20:57:50 +00005554 num_children = GetNumPointeeChildren(pointee_type);
5555 } else
5556 num_children = num_pointee_children;
5557 } break;
5558
5559 case clang::Type::LValueReference:
5560 case clang::Type::RValueReference: {
5561 const clang::ReferenceType *reference_type =
5562 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5563 clang::QualType pointee_type = reference_type->getPointeeType();
5564 uint32_t num_pointee_children =
5565 CompilerType(getASTContext(), pointee_type)
Adrian Prantleca07c52018-11-05 20:49:07 +00005566 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005567 // If this type points to a simple type, then it has 1 child
5568 if (num_pointee_children == 0)
5569 num_children = 1;
5570 else
5571 num_children = num_pointee_children;
5572 } break;
5573
5574 case clang::Type::Typedef:
5575 num_children =
5576 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5577 ->getDecl()
5578 ->getUnderlyingType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005579 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005580 break;
5581
5582 case clang::Type::Auto:
5583 num_children =
5584 CompilerType(getASTContext(),
5585 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005586 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005587 break;
5588
5589 case clang::Type::Elaborated:
5590 num_children =
5591 CompilerType(
5592 getASTContext(),
5593 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
Adrian Prantleca07c52018-11-05 20:49:07 +00005594 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005595 break;
5596
5597 case clang::Type::Paren:
5598 num_children =
5599 CompilerType(getASTContext(),
5600 llvm::cast<clang::ParenType>(qual_type)->desugar())
Adrian Prantleca07c52018-11-05 20:49:07 +00005601 .GetNumChildren(omit_empty_base_classes, exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005602 break;
5603 default:
5604 break;
5605 }
5606 return num_children;
5607}
5608
5609CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5610 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005611}
5612
Greg Claytond8d4a572015-08-11 21:38:15 +00005613lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005614ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5615 if (type) {
5616 clang::QualType qual_type(GetQualType(type));
5617 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5618 if (type_class == clang::Type::Builtin) {
5619 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5620 case clang::BuiltinType::Void:
5621 return eBasicTypeVoid;
5622 case clang::BuiltinType::Bool:
5623 return eBasicTypeBool;
5624 case clang::BuiltinType::Char_S:
5625 return eBasicTypeSignedChar;
5626 case clang::BuiltinType::Char_U:
5627 return eBasicTypeUnsignedChar;
5628 case clang::BuiltinType::Char16:
5629 return eBasicTypeChar16;
5630 case clang::BuiltinType::Char32:
5631 return eBasicTypeChar32;
5632 case clang::BuiltinType::UChar:
5633 return eBasicTypeUnsignedChar;
5634 case clang::BuiltinType::SChar:
5635 return eBasicTypeSignedChar;
5636 case clang::BuiltinType::WChar_S:
5637 return eBasicTypeSignedWChar;
5638 case clang::BuiltinType::WChar_U:
5639 return eBasicTypeUnsignedWChar;
5640 case clang::BuiltinType::Short:
5641 return eBasicTypeShort;
5642 case clang::BuiltinType::UShort:
5643 return eBasicTypeUnsignedShort;
5644 case clang::BuiltinType::Int:
5645 return eBasicTypeInt;
5646 case clang::BuiltinType::UInt:
5647 return eBasicTypeUnsignedInt;
5648 case clang::BuiltinType::Long:
5649 return eBasicTypeLong;
5650 case clang::BuiltinType::ULong:
5651 return eBasicTypeUnsignedLong;
5652 case clang::BuiltinType::LongLong:
5653 return eBasicTypeLongLong;
5654 case clang::BuiltinType::ULongLong:
5655 return eBasicTypeUnsignedLongLong;
5656 case clang::BuiltinType::Int128:
5657 return eBasicTypeInt128;
5658 case clang::BuiltinType::UInt128:
5659 return eBasicTypeUnsignedInt128;
5660
5661 case clang::BuiltinType::Half:
5662 return eBasicTypeHalf;
5663 case clang::BuiltinType::Float:
5664 return eBasicTypeFloat;
5665 case clang::BuiltinType::Double:
5666 return eBasicTypeDouble;
5667 case clang::BuiltinType::LongDouble:
5668 return eBasicTypeLongDouble;
5669
5670 case clang::BuiltinType::NullPtr:
5671 return eBasicTypeNullPtr;
5672 case clang::BuiltinType::ObjCId:
5673 return eBasicTypeObjCID;
5674 case clang::BuiltinType::ObjCClass:
5675 return eBasicTypeObjCClass;
5676 case clang::BuiltinType::ObjCSel:
5677 return eBasicTypeObjCSel;
5678 default:
5679 return eBasicTypeOther;
5680 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005681 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005682 }
5683 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005684}
5685
Kate Stoneb9c1b512016-09-06 20:57:50 +00005686void ClangASTContext::ForEachEnumerator(
5687 lldb::opaque_compiler_type_t type,
5688 std::function<bool(const CompilerType &integer_type,
5689 const ConstString &name,
5690 const llvm::APSInt &value)> const &callback) {
5691 const clang::EnumType *enum_type =
5692 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5693 if (enum_type) {
5694 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5695 if (enum_decl) {
5696 CompilerType integer_type(this,
5697 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005698
Kate Stoneb9c1b512016-09-06 20:57:50 +00005699 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5700 for (enum_pos = enum_decl->enumerator_begin(),
5701 enum_end_pos = enum_decl->enumerator_end();
5702 enum_pos != enum_end_pos; ++enum_pos) {
5703 ConstString name(enum_pos->getNameAsString().c_str());
5704 if (!callback(integer_type, name, enum_pos->getInitVal()))
5705 break;
5706 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005707 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005708 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005709}
5710
Greg Claytond8d4a572015-08-11 21:38:15 +00005711#pragma mark Aggregate Types
5712
Kate Stoneb9c1b512016-09-06 20:57:50 +00005713uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5714 if (!type)
5715 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005716
Kate Stoneb9c1b512016-09-06 20:57:50 +00005717 uint32_t count = 0;
5718 clang::QualType qual_type(GetCanonicalQualType(type));
5719 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5720 switch (type_class) {
5721 case clang::Type::Record:
5722 if (GetCompleteType(type)) {
5723 const clang::RecordType *record_type =
5724 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5725 if (record_type) {
5726 clang::RecordDecl *record_decl = record_type->getDecl();
5727 if (record_decl) {
5728 uint32_t field_idx = 0;
5729 clang::RecordDecl::field_iterator field, field_end;
5730 for (field = record_decl->field_begin(),
5731 field_end = record_decl->field_end();
5732 field != field_end; ++field)
5733 ++field_idx;
5734 count = field_idx;
5735 }
5736 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005737 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005738 break;
5739
5740 case clang::Type::Typedef:
5741 count =
5742 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5743 ->getDecl()
5744 ->getUnderlyingType())
5745 .GetNumFields();
5746 break;
5747
5748 case clang::Type::Auto:
5749 count =
5750 CompilerType(getASTContext(),
5751 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5752 .GetNumFields();
5753 break;
5754
5755 case clang::Type::Elaborated:
5756 count = CompilerType(
5757 getASTContext(),
5758 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5759 .GetNumFields();
5760 break;
5761
5762 case clang::Type::Paren:
5763 count = CompilerType(getASTContext(),
5764 llvm::cast<clang::ParenType>(qual_type)->desugar())
5765 .GetNumFields();
5766 break;
5767
Sean Callananf9c622a2016-09-30 18:44:43 +00005768 case clang::Type::ObjCObjectPointer: {
5769 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005770 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005771 const clang::ObjCInterfaceType *objc_interface_type =
5772 objc_class_type->getInterfaceType();
5773 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005774 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5775 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005776 clang::ObjCInterfaceDecl *class_interface_decl =
5777 objc_interface_type->getDecl();
5778 if (class_interface_decl) {
5779 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005780 }
5781 }
5782 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005783 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005784
5785 case clang::Type::ObjCObject:
5786 case clang::Type::ObjCInterface:
5787 if (GetCompleteType(type)) {
5788 const clang::ObjCObjectType *objc_class_type =
5789 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5790 if (objc_class_type) {
5791 clang::ObjCInterfaceDecl *class_interface_decl =
5792 objc_class_type->getInterface();
5793
5794 if (class_interface_decl)
5795 count = class_interface_decl->ivar_size();
5796 }
5797 }
5798 break;
5799
5800 default:
5801 break;
5802 }
5803 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005804}
5805
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005806static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005807GetObjCFieldAtIndex(clang::ASTContext *ast,
5808 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5809 std::string &name, uint64_t *bit_offset_ptr,
5810 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5811 if (class_interface_decl) {
5812 if (idx < (class_interface_decl->ivar_size())) {
5813 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5814 ivar_end = class_interface_decl->ivar_end();
5815 uint32_t ivar_idx = 0;
5816
5817 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5818 ++ivar_pos, ++ivar_idx) {
5819 if (ivar_idx == idx) {
5820 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5821
5822 clang::QualType ivar_qual_type(ivar_decl->getType());
5823
5824 name.assign(ivar_decl->getNameAsString());
5825
5826 if (bit_offset_ptr) {
5827 const clang::ASTRecordLayout &interface_layout =
5828 ast->getASTObjCInterfaceLayout(class_interface_decl);
5829 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5830 }
5831
5832 const bool is_bitfield = ivar_pos->isBitField();
5833
5834 if (bitfield_bit_size_ptr) {
5835 *bitfield_bit_size_ptr = 0;
5836
5837 if (is_bitfield && ast) {
5838 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5839 llvm::APSInt bitfield_apsint;
5840 if (bitfield_bit_size_expr &&
5841 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5842 *ast)) {
5843 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5844 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005845 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005846 }
5847 if (is_bitfield_ptr)
5848 *is_bitfield_ptr = is_bitfield;
5849
5850 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005851 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005852 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005853 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005854 }
5855 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005856}
5857
Kate Stoneb9c1b512016-09-06 20:57:50 +00005858CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5859 size_t idx, std::string &name,
5860 uint64_t *bit_offset_ptr,
5861 uint32_t *bitfield_bit_size_ptr,
5862 bool *is_bitfield_ptr) {
5863 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005864 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005865
5866 clang::QualType qual_type(GetCanonicalQualType(type));
5867 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5868 switch (type_class) {
5869 case clang::Type::Record:
5870 if (GetCompleteType(type)) {
5871 const clang::RecordType *record_type =
5872 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5873 const clang::RecordDecl *record_decl = record_type->getDecl();
5874 uint32_t field_idx = 0;
5875 clang::RecordDecl::field_iterator field, field_end;
5876 for (field = record_decl->field_begin(),
5877 field_end = record_decl->field_end();
5878 field != field_end; ++field, ++field_idx) {
5879 if (idx == field_idx) {
5880 // Print the member type if requested
5881 // Print the member name and equal sign
5882 name.assign(field->getNameAsString());
5883
5884 // Figure out the type byte size (field_type_info.first) and
5885 // alignment (field_type_info.second) from the AST context.
5886 if (bit_offset_ptr) {
5887 const clang::ASTRecordLayout &record_layout =
5888 getASTContext()->getASTRecordLayout(record_decl);
5889 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5890 }
5891
5892 const bool is_bitfield = field->isBitField();
5893
5894 if (bitfield_bit_size_ptr) {
5895 *bitfield_bit_size_ptr = 0;
5896
5897 if (is_bitfield) {
5898 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5899 llvm::APSInt bitfield_apsint;
5900 if (bitfield_bit_size_expr &&
5901 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5902 *getASTContext())) {
5903 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5904 }
5905 }
5906 }
5907 if (is_bitfield_ptr)
5908 *is_bitfield_ptr = is_bitfield;
5909
5910 return CompilerType(getASTContext(), field->getType());
5911 }
5912 }
5913 }
5914 break;
5915
Sean Callananf9c622a2016-09-30 18:44:43 +00005916 case clang::Type::ObjCObjectPointer: {
5917 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005918 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005919 const clang::ObjCInterfaceType *objc_interface_type =
5920 objc_class_type->getInterfaceType();
5921 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005922 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5923 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005924 clang::ObjCInterfaceDecl *class_interface_decl =
5925 objc_interface_type->getDecl();
5926 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005927 return CompilerType(
5928 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5929 idx, name, bit_offset_ptr,
5930 bitfield_bit_size_ptr, is_bitfield_ptr));
5931 }
5932 }
5933 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005934 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005935
5936 case clang::Type::ObjCObject:
5937 case clang::Type::ObjCInterface:
5938 if (GetCompleteType(type)) {
5939 const clang::ObjCObjectType *objc_class_type =
5940 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5941 assert(objc_class_type);
5942 if (objc_class_type) {
5943 clang::ObjCInterfaceDecl *class_interface_decl =
5944 objc_class_type->getInterface();
5945 return CompilerType(
5946 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5947 idx, name, bit_offset_ptr,
5948 bitfield_bit_size_ptr, is_bitfield_ptr));
5949 }
5950 }
5951 break;
5952
5953 case clang::Type::Typedef:
5954 return CompilerType(getASTContext(),
5955 llvm::cast<clang::TypedefType>(qual_type)
5956 ->getDecl()
5957 ->getUnderlyingType())
5958 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5959 is_bitfield_ptr);
5960
5961 case clang::Type::Auto:
5962 return CompilerType(
5963 getASTContext(),
5964 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5965 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5966 is_bitfield_ptr);
5967
5968 case clang::Type::Elaborated:
5969 return CompilerType(
5970 getASTContext(),
5971 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5972 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5973 is_bitfield_ptr);
5974
5975 case clang::Type::Paren:
5976 return CompilerType(getASTContext(),
5977 llvm::cast<clang::ParenType>(qual_type)->desugar())
5978 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5979 is_bitfield_ptr);
5980
5981 default:
5982 break;
5983 }
5984 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005985}
5986
Greg Clayton99558cc42015-08-24 23:46:31 +00005987uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005988ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
5989 uint32_t count = 0;
5990 clang::QualType qual_type(GetCanonicalQualType(type));
5991 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5992 switch (type_class) {
5993 case clang::Type::Record:
5994 if (GetCompleteType(type)) {
5995 const clang::CXXRecordDecl *cxx_record_decl =
5996 qual_type->getAsCXXRecordDecl();
5997 if (cxx_record_decl)
5998 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00005999 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006000 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006001
Kate Stoneb9c1b512016-09-06 20:57:50 +00006002 case clang::Type::ObjCObjectPointer:
6003 count = GetPointeeType(type).GetNumDirectBaseClasses();
6004 break;
6005
6006 case clang::Type::ObjCObject:
6007 if (GetCompleteType(type)) {
6008 const clang::ObjCObjectType *objc_class_type =
6009 qual_type->getAsObjCQualifiedInterfaceType();
6010 if (objc_class_type) {
6011 clang::ObjCInterfaceDecl *class_interface_decl =
6012 objc_class_type->getInterface();
6013
6014 if (class_interface_decl && class_interface_decl->getSuperClass())
6015 count = 1;
6016 }
6017 }
6018 break;
6019 case clang::Type::ObjCInterface:
6020 if (GetCompleteType(type)) {
6021 const clang::ObjCInterfaceType *objc_interface_type =
6022 qual_type->getAs<clang::ObjCInterfaceType>();
6023 if (objc_interface_type) {
6024 clang::ObjCInterfaceDecl *class_interface_decl =
6025 objc_interface_type->getInterface();
6026
6027 if (class_interface_decl && class_interface_decl->getSuperClass())
6028 count = 1;
6029 }
6030 }
6031 break;
6032
6033 case clang::Type::Typedef:
6034 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6035 ->getDecl()
6036 ->getUnderlyingType()
6037 .getAsOpaquePtr());
6038 break;
6039
6040 case clang::Type::Auto:
6041 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6042 ->getDeducedType()
6043 .getAsOpaquePtr());
6044 break;
6045
6046 case clang::Type::Elaborated:
6047 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6048 ->getNamedType()
6049 .getAsOpaquePtr());
6050 break;
6051
6052 case clang::Type::Paren:
6053 return GetNumDirectBaseClasses(
6054 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6055
6056 default:
6057 break;
6058 }
6059 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006060}
6061
6062uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006063ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
6064 uint32_t count = 0;
6065 clang::QualType qual_type(GetCanonicalQualType(type));
6066 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6067 switch (type_class) {
6068 case clang::Type::Record:
6069 if (GetCompleteType(type)) {
6070 const clang::CXXRecordDecl *cxx_record_decl =
6071 qual_type->getAsCXXRecordDecl();
6072 if (cxx_record_decl)
6073 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006074 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006075 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006076
Kate Stoneb9c1b512016-09-06 20:57:50 +00006077 case clang::Type::Typedef:
6078 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6079 ->getDecl()
6080 ->getUnderlyingType()
6081 .getAsOpaquePtr());
6082 break;
6083
6084 case clang::Type::Auto:
6085 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6086 ->getDeducedType()
6087 .getAsOpaquePtr());
6088 break;
6089
6090 case clang::Type::Elaborated:
6091 count =
6092 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6093 ->getNamedType()
6094 .getAsOpaquePtr());
6095 break;
6096
6097 case clang::Type::Paren:
6098 count = GetNumVirtualBaseClasses(
6099 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6100 break;
6101
6102 default:
6103 break;
6104 }
6105 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006106}
6107
Kate Stoneb9c1b512016-09-06 20:57:50 +00006108CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6109 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6110 clang::QualType qual_type(GetCanonicalQualType(type));
6111 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6112 switch (type_class) {
6113 case clang::Type::Record:
6114 if (GetCompleteType(type)) {
6115 const clang::CXXRecordDecl *cxx_record_decl =
6116 qual_type->getAsCXXRecordDecl();
6117 if (cxx_record_decl) {
6118 uint32_t curr_idx = 0;
6119 clang::CXXRecordDecl::base_class_const_iterator base_class,
6120 base_class_end;
6121 for (base_class = cxx_record_decl->bases_begin(),
6122 base_class_end = cxx_record_decl->bases_end();
6123 base_class != base_class_end; ++base_class, ++curr_idx) {
6124 if (curr_idx == idx) {
6125 if (bit_offset_ptr) {
6126 const clang::ASTRecordLayout &record_layout =
6127 getASTContext()->getASTRecordLayout(cxx_record_decl);
6128 const clang::CXXRecordDecl *base_class_decl =
6129 llvm::cast<clang::CXXRecordDecl>(
6130 base_class->getType()
6131 ->getAs<clang::RecordType>()
6132 ->getDecl());
6133 if (base_class->isVirtual())
6134 *bit_offset_ptr =
6135 record_layout.getVBaseClassOffset(base_class_decl)
6136 .getQuantity() *
6137 8;
6138 else
6139 *bit_offset_ptr =
6140 record_layout.getBaseClassOffset(base_class_decl)
6141 .getQuantity() *
6142 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006143 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006144 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6145 }
6146 }
6147 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006148 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006149 break;
6150
6151 case clang::Type::ObjCObjectPointer:
6152 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6153
6154 case clang::Type::ObjCObject:
6155 if (idx == 0 && GetCompleteType(type)) {
6156 const clang::ObjCObjectType *objc_class_type =
6157 qual_type->getAsObjCQualifiedInterfaceType();
6158 if (objc_class_type) {
6159 clang::ObjCInterfaceDecl *class_interface_decl =
6160 objc_class_type->getInterface();
6161
6162 if (class_interface_decl) {
6163 clang::ObjCInterfaceDecl *superclass_interface_decl =
6164 class_interface_decl->getSuperClass();
6165 if (superclass_interface_decl) {
6166 if (bit_offset_ptr)
6167 *bit_offset_ptr = 0;
6168 return CompilerType(getASTContext(),
6169 getASTContext()->getObjCInterfaceType(
6170 superclass_interface_decl));
6171 }
6172 }
6173 }
6174 }
6175 break;
6176 case clang::Type::ObjCInterface:
6177 if (idx == 0 && GetCompleteType(type)) {
6178 const clang::ObjCObjectType *objc_interface_type =
6179 qual_type->getAs<clang::ObjCInterfaceType>();
6180 if (objc_interface_type) {
6181 clang::ObjCInterfaceDecl *class_interface_decl =
6182 objc_interface_type->getInterface();
6183
6184 if (class_interface_decl) {
6185 clang::ObjCInterfaceDecl *superclass_interface_decl =
6186 class_interface_decl->getSuperClass();
6187 if (superclass_interface_decl) {
6188 if (bit_offset_ptr)
6189 *bit_offset_ptr = 0;
6190 return CompilerType(getASTContext(),
6191 getASTContext()->getObjCInterfaceType(
6192 superclass_interface_decl));
6193 }
6194 }
6195 }
6196 }
6197 break;
6198
6199 case clang::Type::Typedef:
6200 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6201 ->getDecl()
6202 ->getUnderlyingType()
6203 .getAsOpaquePtr(),
6204 idx, bit_offset_ptr);
6205
6206 case clang::Type::Auto:
6207 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6208 ->getDeducedType()
6209 .getAsOpaquePtr(),
6210 idx, bit_offset_ptr);
6211
6212 case clang::Type::Elaborated:
6213 return GetDirectBaseClassAtIndex(
6214 llvm::cast<clang::ElaboratedType>(qual_type)
6215 ->getNamedType()
6216 .getAsOpaquePtr(),
6217 idx, bit_offset_ptr);
6218
6219 case clang::Type::Paren:
6220 return GetDirectBaseClassAtIndex(
6221 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6222 idx, bit_offset_ptr);
6223
6224 default:
6225 break;
6226 }
6227 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006228}
6229
Kate Stoneb9c1b512016-09-06 20:57:50 +00006230CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6231 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6232 clang::QualType qual_type(GetCanonicalQualType(type));
6233 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6234 switch (type_class) {
6235 case clang::Type::Record:
6236 if (GetCompleteType(type)) {
6237 const clang::CXXRecordDecl *cxx_record_decl =
6238 qual_type->getAsCXXRecordDecl();
6239 if (cxx_record_decl) {
6240 uint32_t curr_idx = 0;
6241 clang::CXXRecordDecl::base_class_const_iterator base_class,
6242 base_class_end;
6243 for (base_class = cxx_record_decl->vbases_begin(),
6244 base_class_end = cxx_record_decl->vbases_end();
6245 base_class != base_class_end; ++base_class, ++curr_idx) {
6246 if (curr_idx == idx) {
6247 if (bit_offset_ptr) {
6248 const clang::ASTRecordLayout &record_layout =
6249 getASTContext()->getASTRecordLayout(cxx_record_decl);
6250 const clang::CXXRecordDecl *base_class_decl =
6251 llvm::cast<clang::CXXRecordDecl>(
6252 base_class->getType()
6253 ->getAs<clang::RecordType>()
6254 ->getDecl());
6255 *bit_offset_ptr =
6256 record_layout.getVBaseClassOffset(base_class_decl)
6257 .getQuantity() *
6258 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006259 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006260 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6261 }
6262 }
6263 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006264 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006265 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006266
Kate Stoneb9c1b512016-09-06 20:57:50 +00006267 case clang::Type::Typedef:
6268 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6269 ->getDecl()
6270 ->getUnderlyingType()
6271 .getAsOpaquePtr(),
6272 idx, bit_offset_ptr);
6273
6274 case clang::Type::Auto:
6275 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6276 ->getDeducedType()
6277 .getAsOpaquePtr(),
6278 idx, bit_offset_ptr);
6279
6280 case clang::Type::Elaborated:
6281 return GetVirtualBaseClassAtIndex(
6282 llvm::cast<clang::ElaboratedType>(qual_type)
6283 ->getNamedType()
6284 .getAsOpaquePtr(),
6285 idx, bit_offset_ptr);
6286
6287 case clang::Type::Paren:
6288 return GetVirtualBaseClassAtIndex(
6289 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6290 idx, bit_offset_ptr);
6291
6292 default:
6293 break;
6294 }
6295 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006296}
6297
Greg Claytond8d4a572015-08-11 21:38:15 +00006298// If a pointer to a pointee type (the clang_type arg) says that it has no
6299// children, then we either need to trust it, or override it and return a
6300// different result. For example, an "int *" has one child that is an integer,
6301// but a function pointer doesn't have any children. Likewise if a Record type
6302// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006303uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6304 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006305 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006306
6307 clang::QualType qual_type(type.getCanonicalType());
6308 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6309 switch (type_class) {
6310 case clang::Type::Builtin:
6311 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6312 case clang::BuiltinType::UnknownAny:
6313 case clang::BuiltinType::Void:
6314 case clang::BuiltinType::NullPtr:
6315 case clang::BuiltinType::OCLEvent:
6316 case clang::BuiltinType::OCLImage1dRO:
6317 case clang::BuiltinType::OCLImage1dWO:
6318 case clang::BuiltinType::OCLImage1dRW:
6319 case clang::BuiltinType::OCLImage1dArrayRO:
6320 case clang::BuiltinType::OCLImage1dArrayWO:
6321 case clang::BuiltinType::OCLImage1dArrayRW:
6322 case clang::BuiltinType::OCLImage1dBufferRO:
6323 case clang::BuiltinType::OCLImage1dBufferWO:
6324 case clang::BuiltinType::OCLImage1dBufferRW:
6325 case clang::BuiltinType::OCLImage2dRO:
6326 case clang::BuiltinType::OCLImage2dWO:
6327 case clang::BuiltinType::OCLImage2dRW:
6328 case clang::BuiltinType::OCLImage2dArrayRO:
6329 case clang::BuiltinType::OCLImage2dArrayWO:
6330 case clang::BuiltinType::OCLImage2dArrayRW:
6331 case clang::BuiltinType::OCLImage3dRO:
6332 case clang::BuiltinType::OCLImage3dWO:
6333 case clang::BuiltinType::OCLImage3dRW:
6334 case clang::BuiltinType::OCLSampler:
6335 return 0;
6336 case clang::BuiltinType::Bool:
6337 case clang::BuiltinType::Char_U:
6338 case clang::BuiltinType::UChar:
6339 case clang::BuiltinType::WChar_U:
6340 case clang::BuiltinType::Char16:
6341 case clang::BuiltinType::Char32:
6342 case clang::BuiltinType::UShort:
6343 case clang::BuiltinType::UInt:
6344 case clang::BuiltinType::ULong:
6345 case clang::BuiltinType::ULongLong:
6346 case clang::BuiltinType::UInt128:
6347 case clang::BuiltinType::Char_S:
6348 case clang::BuiltinType::SChar:
6349 case clang::BuiltinType::WChar_S:
6350 case clang::BuiltinType::Short:
6351 case clang::BuiltinType::Int:
6352 case clang::BuiltinType::Long:
6353 case clang::BuiltinType::LongLong:
6354 case clang::BuiltinType::Int128:
6355 case clang::BuiltinType::Float:
6356 case clang::BuiltinType::Double:
6357 case clang::BuiltinType::LongDouble:
6358 case clang::BuiltinType::Dependent:
6359 case clang::BuiltinType::Overload:
6360 case clang::BuiltinType::ObjCId:
6361 case clang::BuiltinType::ObjCClass:
6362 case clang::BuiltinType::ObjCSel:
6363 case clang::BuiltinType::BoundMember:
6364 case clang::BuiltinType::Half:
6365 case clang::BuiltinType::ARCUnbridgedCast:
6366 case clang::BuiltinType::PseudoObject:
6367 case clang::BuiltinType::BuiltinFn:
6368 case clang::BuiltinType::OMPArraySection:
6369 return 1;
6370 default:
6371 return 0;
6372 }
6373 break;
6374
6375 case clang::Type::Complex:
6376 return 1;
6377 case clang::Type::Pointer:
6378 return 1;
6379 case clang::Type::BlockPointer:
6380 return 0; // If block pointers don't have debug info, then no children for
6381 // them
6382 case clang::Type::LValueReference:
6383 return 1;
6384 case clang::Type::RValueReference:
6385 return 1;
6386 case clang::Type::MemberPointer:
6387 return 0;
6388 case clang::Type::ConstantArray:
6389 return 0;
6390 case clang::Type::IncompleteArray:
6391 return 0;
6392 case clang::Type::VariableArray:
6393 return 0;
6394 case clang::Type::DependentSizedArray:
6395 return 0;
6396 case clang::Type::DependentSizedExtVector:
6397 return 0;
6398 case clang::Type::Vector:
6399 return 0;
6400 case clang::Type::ExtVector:
6401 return 0;
6402 case clang::Type::FunctionProto:
6403 return 0; // When we function pointers, they have no children...
6404 case clang::Type::FunctionNoProto:
6405 return 0; // When we function pointers, they have no children...
6406 case clang::Type::UnresolvedUsing:
6407 return 0;
6408 case clang::Type::Paren:
6409 return GetNumPointeeChildren(
6410 llvm::cast<clang::ParenType>(qual_type)->desugar());
6411 case clang::Type::Typedef:
6412 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6413 ->getDecl()
6414 ->getUnderlyingType());
6415 case clang::Type::Auto:
6416 return GetNumPointeeChildren(
6417 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6418 case clang::Type::Elaborated:
6419 return GetNumPointeeChildren(
6420 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6421 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006422 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6423 ->getUnderlyingExpr()
6424 ->getType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006425 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006426 return GetNumPointeeChildren(
6427 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006428 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006429 return GetNumPointeeChildren(
6430 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006431 case clang::Type::Record:
6432 return 0;
6433 case clang::Type::Enum:
6434 return 1;
6435 case clang::Type::TemplateTypeParm:
6436 return 1;
6437 case clang::Type::SubstTemplateTypeParm:
6438 return 1;
6439 case clang::Type::TemplateSpecialization:
6440 return 1;
6441 case clang::Type::InjectedClassName:
6442 return 0;
6443 case clang::Type::DependentName:
6444 return 1;
6445 case clang::Type::DependentTemplateSpecialization:
6446 return 1;
6447 case clang::Type::ObjCObject:
6448 return 0;
6449 case clang::Type::ObjCInterface:
6450 return 0;
6451 case clang::Type::ObjCObjectPointer:
6452 return 1;
6453 default:
6454 break;
6455 }
6456 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006457}
6458
Kate Stoneb9c1b512016-09-06 20:57:50 +00006459CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6460 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6461 bool transparent_pointers, bool omit_empty_base_classes,
6462 bool ignore_array_bounds, std::string &child_name,
6463 uint32_t &child_byte_size, int32_t &child_byte_offset,
6464 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6465 bool &child_is_base_class, bool &child_is_deref_of_parent,
6466 ValueObject *valobj, uint64_t &language_flags) {
6467 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006468 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006469
Kate Stoneb9c1b512016-09-06 20:57:50 +00006470 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6471 const clang::Type::TypeClass parent_type_class =
6472 parent_qual_type->getTypeClass();
6473 child_bitfield_bit_size = 0;
6474 child_bitfield_bit_offset = 0;
6475 child_is_base_class = false;
6476 language_flags = 0;
6477
Adrian Prantleca07c52018-11-05 20:49:07 +00006478 const bool idx_is_valid =
6479 idx < GetNumChildren(type, omit_empty_base_classes, exe_ctx);
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006480 int32_t bit_offset;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006481 switch (parent_type_class) {
6482 case clang::Type::Builtin:
6483 if (idx_is_valid) {
6484 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6485 case clang::BuiltinType::ObjCId:
6486 case clang::BuiltinType::ObjCClass:
6487 child_name = "isa";
6488 child_byte_size =
6489 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6490 CHAR_BIT;
6491 return CompilerType(getASTContext(),
6492 getASTContext()->ObjCBuiltinClassTy);
6493
6494 default:
6495 break;
6496 }
6497 }
6498 break;
6499
6500 case clang::Type::Record:
6501 if (idx_is_valid && GetCompleteType(type)) {
6502 const clang::RecordType *record_type =
6503 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6504 const clang::RecordDecl *record_decl = record_type->getDecl();
6505 assert(record_decl);
6506 const clang::ASTRecordLayout &record_layout =
6507 getASTContext()->getASTRecordLayout(record_decl);
6508 uint32_t child_idx = 0;
6509
6510 const clang::CXXRecordDecl *cxx_record_decl =
6511 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6512 if (cxx_record_decl) {
6513 // We might have base classes to print out first
6514 clang::CXXRecordDecl::base_class_const_iterator base_class,
6515 base_class_end;
6516 for (base_class = cxx_record_decl->bases_begin(),
6517 base_class_end = cxx_record_decl->bases_end();
6518 base_class != base_class_end; ++base_class) {
6519 const clang::CXXRecordDecl *base_class_decl = nullptr;
6520
6521 // Skip empty base classes
6522 if (omit_empty_base_classes) {
6523 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6524 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6525 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
6526 continue;
6527 }
6528
6529 if (idx == child_idx) {
6530 if (base_class_decl == nullptr)
6531 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6532 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6533
6534 if (base_class->isVirtual()) {
6535 bool handled = false;
6536 if (valobj) {
Zachary Turner97206d52017-05-12 04:51:55 +00006537 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006538 AddressType addr_type = eAddressTypeInvalid;
6539 lldb::addr_t vtable_ptr_addr =
6540 valobj->GetCPPVTableAddress(addr_type);
6541
6542 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS &&
6543 addr_type == eAddressTypeLoad) {
6544
6545 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
6546 Process *process = exe_ctx.GetProcessPtr();
6547 if (process) {
6548 clang::VTableContextBase *vtable_ctx =
6549 getASTContext()->getVTableContext();
6550 if (vtable_ctx) {
6551 if (vtable_ctx->isMicrosoft()) {
6552 clang::MicrosoftVTableContext *msoft_vtable_ctx =
6553 static_cast<clang::MicrosoftVTableContext *>(
6554 vtable_ctx);
6555
6556 if (vtable_ptr_addr) {
6557 const lldb::addr_t vbtable_ptr_addr =
6558 vtable_ptr_addr +
6559 record_layout.getVBPtrOffset().getQuantity();
6560
6561 const lldb::addr_t vbtable_ptr =
6562 process->ReadPointerFromMemory(vbtable_ptr_addr,
6563 err);
6564 if (vbtable_ptr != LLDB_INVALID_ADDRESS) {
6565 // Get the index into the virtual base table. The
6566 // index is the index in uint32_t from vbtable_ptr
6567 const unsigned vbtable_index =
6568 msoft_vtable_ctx->getVBTableIndex(
6569 cxx_record_decl, base_class_decl);
6570 const lldb::addr_t base_offset_addr =
6571 vbtable_ptr + vbtable_index * 4;
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006572 const int32_t base_offset =
6573 process->ReadSignedIntegerFromMemory(
Aleksandr Urakov53459482018-08-17 07:28:24 +00006574 base_offset_addr, 4, INT32_MAX, err);
6575 if (base_offset != INT32_MAX) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006576 handled = true;
6577 bit_offset = base_offset * 8;
6578 }
6579 }
6580 }
6581 } else {
6582 clang::ItaniumVTableContext *itanium_vtable_ctx =
6583 static_cast<clang::ItaniumVTableContext *>(
6584 vtable_ctx);
6585 if (vtable_ptr_addr) {
6586 const lldb::addr_t vtable_ptr =
6587 process->ReadPointerFromMemory(vtable_ptr_addr,
6588 err);
6589 if (vtable_ptr != LLDB_INVALID_ADDRESS) {
6590 clang::CharUnits base_offset_offset =
6591 itanium_vtable_ctx->getVirtualBaseOffsetOffset(
6592 cxx_record_decl, base_class_decl);
6593 const lldb::addr_t base_offset_addr =
6594 vtable_ptr + base_offset_offset.getQuantity();
6595 const uint32_t base_offset_size =
6596 process->GetAddressByteSize();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006597 const int64_t base_offset =
6598 process->ReadSignedIntegerFromMemory(
Kate Stoneb9c1b512016-09-06 20:57:50 +00006599 base_offset_addr, base_offset_size,
6600 UINT32_MAX, err);
6601 if (base_offset < UINT32_MAX) {
6602 handled = true;
6603 bit_offset = base_offset * 8;
6604 }
6605 }
6606 }
6607 }
6608 }
6609 }
6610 }
6611 }
6612 if (!handled)
6613 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6614 .getQuantity() *
6615 8;
6616 } else
6617 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6618 .getQuantity() *
6619 8;
6620
6621 // Base classes should be a multiple of 8 bits in size
6622 child_byte_offset = bit_offset / 8;
6623 CompilerType base_class_clang_type(getASTContext(),
6624 base_class->getType());
6625 child_name = base_class_clang_type.GetTypeName().AsCString("");
6626 uint64_t base_class_clang_type_bit_size =
6627 base_class_clang_type.GetBitSize(
6628 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6629
6630 // Base classes bit sizes should be a multiple of 8 bits in size
6631 assert(base_class_clang_type_bit_size % 8 == 0);
6632 child_byte_size = base_class_clang_type_bit_size / 8;
6633 child_is_base_class = true;
6634 return base_class_clang_type;
6635 }
6636 // We don't increment the child index in the for loop since we might
6637 // be skipping empty base classes
6638 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006639 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006640 }
6641 // Make sure index is in range...
6642 uint32_t field_idx = 0;
6643 clang::RecordDecl::field_iterator field, field_end;
6644 for (field = record_decl->field_begin(),
6645 field_end = record_decl->field_end();
6646 field != field_end; ++field, ++field_idx, ++child_idx) {
6647 if (idx == child_idx) {
6648 // Print the member type if requested
6649 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006650 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006651
6652 // Figure out the type byte size (field_type_info.first) and
6653 // alignment (field_type_info.second) from the AST context.
6654 CompilerType field_clang_type(getASTContext(), field->getType());
6655 assert(field_idx < record_layout.getFieldCount());
6656 child_byte_size = field_clang_type.GetByteSize(
6657 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6658 const uint32_t child_bit_size = child_byte_size * 8;
6659
6660 // Figure out the field offset within the current struct/union/class
6661 // type
6662 bit_offset = record_layout.getFieldOffset(field_idx);
6663 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6664 child_bitfield_bit_size)) {
6665 child_bitfield_bit_offset = bit_offset % child_bit_size;
6666 const uint32_t child_bit_offset =
6667 bit_offset - child_bitfield_bit_offset;
6668 child_byte_offset = child_bit_offset / 8;
6669 } else {
6670 child_byte_offset = bit_offset / 8;
6671 }
6672
6673 return field_clang_type;
6674 }
6675 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006676 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006677 break;
6678
6679 case clang::Type::ObjCObject:
6680 case clang::Type::ObjCInterface:
6681 if (idx_is_valid && GetCompleteType(type)) {
6682 const clang::ObjCObjectType *objc_class_type =
6683 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6684 assert(objc_class_type);
6685 if (objc_class_type) {
6686 uint32_t child_idx = 0;
6687 clang::ObjCInterfaceDecl *class_interface_decl =
6688 objc_class_type->getInterface();
6689
6690 if (class_interface_decl) {
6691
6692 const clang::ASTRecordLayout &interface_layout =
6693 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6694 clang::ObjCInterfaceDecl *superclass_interface_decl =
6695 class_interface_decl->getSuperClass();
6696 if (superclass_interface_decl) {
6697 if (omit_empty_base_classes) {
6698 CompilerType base_class_clang_type(
6699 getASTContext(), getASTContext()->getObjCInterfaceType(
6700 superclass_interface_decl));
Adrian Prantleca07c52018-11-05 20:49:07 +00006701 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes,
6702 exe_ctx) > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00006703 if (idx == 0) {
6704 clang::QualType ivar_qual_type(
6705 getASTContext()->getObjCInterfaceType(
6706 superclass_interface_decl));
6707
6708 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006709 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006710
6711 clang::TypeInfo ivar_type_info =
6712 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6713
6714 child_byte_size = ivar_type_info.Width / 8;
6715 child_byte_offset = 0;
6716 child_is_base_class = true;
6717
6718 return CompilerType(getASTContext(), ivar_qual_type);
6719 }
6720
6721 ++child_idx;
6722 }
6723 } else
6724 ++child_idx;
6725 }
6726
6727 const uint32_t superclass_idx = child_idx;
6728
6729 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6730 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6731 ivar_end = class_interface_decl->ivar_end();
6732
6733 for (ivar_pos = class_interface_decl->ivar_begin();
6734 ivar_pos != ivar_end; ++ivar_pos) {
6735 if (child_idx == idx) {
6736 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6737
6738 clang::QualType ivar_qual_type(ivar_decl->getType());
6739
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006740 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006741
6742 clang::TypeInfo ivar_type_info =
6743 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6744
6745 child_byte_size = ivar_type_info.Width / 8;
6746
6747 // Figure out the field offset within the current
Adrian Prantl05097242018-04-30 16:49:04 +00006748 // struct/union/class type For ObjC objects, we can't trust the
6749 // bit offset we get from the Clang AST, since that doesn't
6750 // account for the space taken up by unbacked properties, or
6751 // from the changing size of base classes that are newer than
6752 // this class. So if we have a process around that we can ask
6753 // about this object, do so.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006754 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6755 Process *process = nullptr;
6756 if (exe_ctx)
6757 process = exe_ctx->GetProcessPtr();
6758 if (process) {
6759 ObjCLanguageRuntime *objc_runtime =
6760 process->GetObjCLanguageRuntime();
6761 if (objc_runtime != nullptr) {
6762 CompilerType parent_ast_type(getASTContext(),
6763 parent_qual_type);
6764 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6765 parent_ast_type, ivar_decl->getNameAsString().c_str());
6766 }
6767 }
6768
Aleksandr Urakovff701722018-08-20 05:59:27 +00006769 // Setting this to INT32_MAX to make sure we don't compute it
Kate Stoneb9c1b512016-09-06 20:57:50 +00006770 // twice...
Aleksandr Urakov53459482018-08-17 07:28:24 +00006771 bit_offset = INT32_MAX;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006772
6773 if (child_byte_offset ==
6774 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6775 bit_offset = interface_layout.getFieldOffset(child_idx -
6776 superclass_idx);
6777 child_byte_offset = bit_offset / 8;
6778 }
6779
6780 // Note, the ObjC Ivar Byte offset is just that, it doesn't
Adrian Prantl05097242018-04-30 16:49:04 +00006781 // account for the bit offset of a bitfield within its
6782 // containing object. So regardless of where we get the byte
Kate Stoneb9c1b512016-09-06 20:57:50 +00006783 // offset from, we still need to get the bit offset for
6784 // bitfields from the layout.
6785
6786 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6787 child_bitfield_bit_size)) {
Aleksandr Urakov53459482018-08-17 07:28:24 +00006788 if (bit_offset == INT32_MAX)
Kate Stoneb9c1b512016-09-06 20:57:50 +00006789 bit_offset = interface_layout.getFieldOffset(
6790 child_idx - superclass_idx);
6791
6792 child_bitfield_bit_offset = bit_offset % 8;
6793 }
6794 return CompilerType(getASTContext(), ivar_qual_type);
6795 }
6796 ++child_idx;
6797 }
6798 }
6799 }
6800 }
6801 }
6802 break;
6803
6804 case clang::Type::ObjCObjectPointer:
6805 if (idx_is_valid) {
6806 CompilerType pointee_clang_type(GetPointeeType(type));
6807
6808 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6809 child_is_deref_of_parent = false;
6810 bool tmp_child_is_deref_of_parent = false;
6811 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6812 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6813 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6814 child_bitfield_bit_size, child_bitfield_bit_offset,
6815 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6816 language_flags);
6817 } else {
6818 child_is_deref_of_parent = true;
6819 const char *parent_name =
6820 valobj ? valobj->GetName().GetCString() : NULL;
6821 if (parent_name) {
6822 child_name.assign(1, '*');
6823 child_name += parent_name;
6824 }
6825
6826 // We have a pointer to an simple type
6827 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
6828 child_byte_size = pointee_clang_type.GetByteSize(
6829 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6830 child_byte_offset = 0;
6831 return pointee_clang_type;
6832 }
6833 }
6834 }
6835 break;
6836
6837 case clang::Type::Vector:
6838 case clang::Type::ExtVector:
6839 if (idx_is_valid) {
6840 const clang::VectorType *array =
6841 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6842 if (array) {
6843 CompilerType element_type(getASTContext(), array->getElementType());
6844 if (element_type.GetCompleteType()) {
6845 char element_name[64];
6846 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6847 static_cast<uint64_t>(idx));
6848 child_name.assign(element_name);
6849 child_byte_size = element_type.GetByteSize(
6850 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6851 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6852 return element_type;
6853 }
6854 }
6855 }
6856 break;
6857
6858 case clang::Type::ConstantArray:
6859 case clang::Type::IncompleteArray:
6860 if (ignore_array_bounds || idx_is_valid) {
6861 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6862 if (array) {
6863 CompilerType element_type(getASTContext(), array->getElementType());
6864 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006865 child_name = llvm::formatv("[{0}]", idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006866 child_byte_size = element_type.GetByteSize(
6867 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6868 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6869 return element_type;
6870 }
6871 }
6872 }
6873 break;
6874
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006875 case clang::Type::Pointer: {
6876 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006877
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006878 // Don't dereference "void *" pointers
6879 if (pointee_clang_type.IsVoidType())
6880 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006881
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006882 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6883 child_is_deref_of_parent = false;
6884 bool tmp_child_is_deref_of_parent = false;
6885 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6886 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6887 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6888 child_bitfield_bit_size, child_bitfield_bit_offset,
6889 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6890 language_flags);
6891 } else {
6892 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006893
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006894 const char *parent_name =
6895 valobj ? valobj->GetName().GetCString() : NULL;
6896 if (parent_name) {
6897 child_name.assign(1, '*');
6898 child_name += parent_name;
6899 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006900
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006901 // We have a pointer to an simple type
6902 if (idx == 0) {
6903 child_byte_size = pointee_clang_type.GetByteSize(
6904 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6905 child_byte_offset = 0;
6906 return pointee_clang_type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006907 }
6908 }
6909 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006910 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006911
6912 case clang::Type::LValueReference:
6913 case clang::Type::RValueReference:
6914 if (idx_is_valid) {
6915 const clang::ReferenceType *reference_type =
6916 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6917 CompilerType pointee_clang_type(getASTContext(),
6918 reference_type->getPointeeType());
6919 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6920 child_is_deref_of_parent = false;
6921 bool tmp_child_is_deref_of_parent = false;
6922 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6923 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6924 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6925 child_bitfield_bit_size, child_bitfield_bit_offset,
6926 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6927 language_flags);
6928 } else {
6929 const char *parent_name =
6930 valobj ? valobj->GetName().GetCString() : NULL;
6931 if (parent_name) {
6932 child_name.assign(1, '&');
6933 child_name += parent_name;
6934 }
6935
6936 // We have a pointer to an simple type
6937 if (idx == 0) {
6938 child_byte_size = pointee_clang_type.GetByteSize(
6939 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6940 child_byte_offset = 0;
6941 return pointee_clang_type;
6942 }
6943 }
6944 }
6945 break;
6946
6947 case clang::Type::Typedef: {
6948 CompilerType typedefed_clang_type(
6949 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
6950 ->getDecl()
6951 ->getUnderlyingType());
6952 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
6953 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6954 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6955 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6956 child_is_deref_of_parent, valobj, language_flags);
6957 } break;
6958
6959 case clang::Type::Auto: {
6960 CompilerType elaborated_clang_type(
6961 getASTContext(),
6962 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
6963 return elaborated_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 }
6969
6970 case clang::Type::Elaborated: {
6971 CompilerType elaborated_clang_type(
6972 getASTContext(),
6973 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
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::Paren: {
6982 CompilerType paren_clang_type(
6983 getASTContext(),
6984 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
6985 return paren_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 default:
6993 break;
6994 }
6995 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006996}
6997
Kate Stoneb9c1b512016-09-06 20:57:50 +00006998static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
6999 const clang::CXXBaseSpecifier *base_spec,
7000 bool omit_empty_base_classes) {
7001 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007002
Kate Stoneb9c1b512016-09-06 20:57:50 +00007003 const clang::CXXRecordDecl *cxx_record_decl =
7004 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7005
7006 // const char *super_name = record_decl->getNameAsCString();
7007 // const char *base_name =
7008 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
7009 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
7010 //
7011 if (cxx_record_decl) {
7012 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
7013 for (base_class = cxx_record_decl->bases_begin(),
7014 base_class_end = cxx_record_decl->bases_end();
7015 base_class != base_class_end; ++base_class) {
7016 if (omit_empty_base_classes) {
7017 if (BaseSpecifierIsEmpty(base_class))
7018 continue;
7019 }
7020
7021 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
7022 // super_name, base_name,
7023 // child_idx,
7024 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
7025 //
7026 //
7027 if (base_class == base_spec)
7028 return child_idx;
7029 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00007030 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007031 }
7032
7033 return UINT32_MAX;
7034}
7035
7036static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
7037 clang::NamedDecl *canonical_decl,
7038 bool omit_empty_base_classes) {
7039 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
7040 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
7041 omit_empty_base_classes);
7042
7043 clang::RecordDecl::field_iterator field, field_end;
7044 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
7045 field != field_end; ++field, ++child_idx) {
7046 if (field->getCanonicalDecl() == canonical_decl)
7047 return child_idx;
7048 }
7049
7050 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00007051}
7052
7053// Look for a child member (doesn't include base classes, but it does include
Adrian Prantl05097242018-04-30 16:49:04 +00007054// their members) in the type hierarchy. Returns an index path into
7055// "clang_type" on how to reach the appropriate member.
Greg Claytond8d4a572015-08-11 21:38:15 +00007056//
7057// class A
7058// {
7059// public:
7060// int m_a;
7061// int m_b;
7062// };
7063//
7064// class B
7065// {
7066// };
7067//
7068// class C :
7069// public B,
7070// public A
7071// {
7072// };
7073//
7074// If we have a clang type that describes "class C", and we wanted to looked
7075// "m_b" in it:
7076//
Kate Stoneb9c1b512016-09-06 20:57:50 +00007077// With omit_empty_base_classes == false we would get an integer array back
Adrian Prantl05097242018-04-30 16:49:04 +00007078// with: { 1, 1 } The first index 1 is the child index for "class A" within
7079// class C The second index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007080//
Adrian Prantl05097242018-04-30 16:49:04 +00007081// With omit_empty_base_classes == true we would get an integer array back
7082// with: { 0, 1 } The first index 0 is the child index for "class A" within
7083// class C (since class B doesn't have any members it doesn't count) The second
7084// index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007085
Kate Stoneb9c1b512016-09-06 20:57:50 +00007086size_t ClangASTContext::GetIndexOfChildMemberWithName(
7087 lldb::opaque_compiler_type_t type, const char *name,
7088 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
7089 if (type && name && name[0]) {
7090 clang::QualType qual_type(GetCanonicalQualType(type));
7091 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7092 switch (type_class) {
7093 case clang::Type::Record:
7094 if (GetCompleteType(type)) {
7095 const clang::RecordType *record_type =
7096 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7097 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00007098
Kate Stoneb9c1b512016-09-06 20:57:50 +00007099 assert(record_decl);
7100 uint32_t child_idx = 0;
7101
7102 const clang::CXXRecordDecl *cxx_record_decl =
7103 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7104
7105 // Try and find a field that matches NAME
7106 clang::RecordDecl::field_iterator field, field_end;
7107 llvm::StringRef name_sref(name);
7108 for (field = record_decl->field_begin(),
7109 field_end = record_decl->field_end();
7110 field != field_end; ++field, ++child_idx) {
7111 llvm::StringRef field_name = field->getName();
7112 if (field_name.empty()) {
7113 CompilerType field_type(getASTContext(), field->getType());
7114 child_indexes.push_back(child_idx);
7115 if (field_type.GetIndexOfChildMemberWithName(
7116 name, omit_empty_base_classes, child_indexes))
7117 return child_indexes.size();
7118 child_indexes.pop_back();
7119
7120 } else if (field_name.equals(name_sref)) {
7121 // We have to add on the number of base classes to this index!
7122 child_indexes.push_back(
7123 child_idx + ClangASTContext::GetNumBaseClasses(
7124 cxx_record_decl, omit_empty_base_classes));
7125 return child_indexes.size();
7126 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007127 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007128
Kate Stoneb9c1b512016-09-06 20:57:50 +00007129 if (cxx_record_decl) {
7130 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7131
7132 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7133
7134 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7135 // Didn't find things easily, lets let clang do its thang...
7136 clang::IdentifierInfo &ident_ref =
7137 getASTContext()->Idents.get(name_sref);
7138 clang::DeclarationName decl_name(&ident_ref);
7139
7140 clang::CXXBasePaths paths;
7141 if (cxx_record_decl->lookupInBases(
7142 [decl_name](const clang::CXXBaseSpecifier *specifier,
7143 clang::CXXBasePath &path) {
7144 return clang::CXXRecordDecl::FindOrdinaryMember(
7145 specifier, path, decl_name);
7146 },
7147 paths)) {
7148 clang::CXXBasePaths::const_paths_iterator path,
7149 path_end = paths.end();
7150 for (path = paths.begin(); path != path_end; ++path) {
7151 const size_t num_path_elements = path->size();
7152 for (size_t e = 0; e < num_path_elements; ++e) {
7153 clang::CXXBasePathElement elem = (*path)[e];
7154
7155 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7156 omit_empty_base_classes);
7157 if (child_idx == UINT32_MAX) {
7158 child_indexes.clear();
7159 return 0;
7160 } else {
7161 child_indexes.push_back(child_idx);
7162 parent_record_decl = llvm::cast<clang::RecordDecl>(
7163 elem.Base->getType()
7164 ->getAs<clang::RecordType>()
7165 ->getDecl());
7166 }
7167 }
7168 for (clang::NamedDecl *path_decl : path->Decls) {
7169 child_idx = GetIndexForRecordChild(
7170 parent_record_decl, path_decl, omit_empty_base_classes);
7171 if (child_idx == UINT32_MAX) {
7172 child_indexes.clear();
7173 return 0;
7174 } else {
7175 child_indexes.push_back(child_idx);
7176 }
7177 }
7178 }
7179 return child_indexes.size();
7180 }
7181 }
7182 }
7183 break;
7184
7185 case clang::Type::ObjCObject:
7186 case clang::Type::ObjCInterface:
7187 if (GetCompleteType(type)) {
7188 llvm::StringRef name_sref(name);
7189 const clang::ObjCObjectType *objc_class_type =
7190 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7191 assert(objc_class_type);
7192 if (objc_class_type) {
7193 uint32_t child_idx = 0;
7194 clang::ObjCInterfaceDecl *class_interface_decl =
7195 objc_class_type->getInterface();
7196
7197 if (class_interface_decl) {
7198 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7199 ivar_end = class_interface_decl->ivar_end();
7200 clang::ObjCInterfaceDecl *superclass_interface_decl =
7201 class_interface_decl->getSuperClass();
7202
7203 for (ivar_pos = class_interface_decl->ivar_begin();
7204 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7205 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7206
7207 if (ivar_decl->getName().equals(name_sref)) {
7208 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7209 (omit_empty_base_classes &&
7210 ObjCDeclHasIVars(superclass_interface_decl, true)))
7211 ++child_idx;
7212
7213 child_indexes.push_back(child_idx);
7214 return child_indexes.size();
7215 }
7216 }
7217
7218 if (superclass_interface_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00007219 // The super class index is always zero for ObjC classes, so we
7220 // push it onto the child indexes in case we find an ivar in our
7221 // superclass...
Kate Stoneb9c1b512016-09-06 20:57:50 +00007222 child_indexes.push_back(0);
7223
7224 CompilerType superclass_clang_type(
7225 getASTContext(), getASTContext()->getObjCInterfaceType(
7226 superclass_interface_decl));
7227 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7228 name, omit_empty_base_classes, child_indexes)) {
Adrian Prantl05097242018-04-30 16:49:04 +00007229 // We did find an ivar in a superclass so just return the
7230 // results!
Kate Stoneb9c1b512016-09-06 20:57:50 +00007231 return child_indexes.size();
7232 }
7233
Adrian Prantl05097242018-04-30 16:49:04 +00007234 // We didn't find an ivar matching "name" in our superclass, pop
7235 // the superclass zero index that we pushed on above.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007236 child_indexes.pop_back();
7237 }
7238 }
7239 }
7240 }
7241 break;
7242
7243 case clang::Type::ObjCObjectPointer: {
7244 CompilerType objc_object_clang_type(
7245 getASTContext(),
7246 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7247 ->getPointeeType());
7248 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7249 name, omit_empty_base_classes, child_indexes);
7250 } break;
7251
7252 case clang::Type::ConstantArray: {
7253 // const clang::ConstantArrayType *array =
7254 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7255 // const uint64_t element_count =
7256 // array->getSize().getLimitedValue();
7257 //
7258 // if (idx < element_count)
7259 // {
7260 // std::pair<uint64_t, unsigned> field_type_info =
7261 // ast->getTypeInfo(array->getElementType());
7262 //
7263 // char element_name[32];
7264 // ::snprintf (element_name, sizeof (element_name),
7265 // "%s[%u]", parent_name ? parent_name : "", idx);
7266 //
7267 // child_name.assign(element_name);
7268 // assert(field_type_info.first % 8 == 0);
7269 // child_byte_size = field_type_info.first / 8;
7270 // child_byte_offset = idx * child_byte_size;
7271 // return array->getElementType().getAsOpaquePtr();
7272 // }
7273 } break;
7274
7275 // case clang::Type::MemberPointerType:
7276 // {
7277 // MemberPointerType *mem_ptr_type =
7278 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7279 // clang::QualType pointee_type =
7280 // mem_ptr_type->getPointeeType();
7281 //
7282 // if (ClangASTContext::IsAggregateType
7283 // (pointee_type.getAsOpaquePtr()))
7284 // {
7285 // return GetIndexOfChildWithName (ast,
7286 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7287 // name);
7288 // }
7289 // }
7290 // break;
7291 //
7292 case clang::Type::LValueReference:
7293 case clang::Type::RValueReference: {
7294 const clang::ReferenceType *reference_type =
7295 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7296 clang::QualType pointee_type(reference_type->getPointeeType());
7297 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7298
7299 if (pointee_clang_type.IsAggregateType()) {
7300 return pointee_clang_type.GetIndexOfChildMemberWithName(
7301 name, omit_empty_base_classes, child_indexes);
7302 }
7303 } break;
7304
7305 case clang::Type::Pointer: {
7306 CompilerType pointee_clang_type(GetPointeeType(type));
7307
7308 if (pointee_clang_type.IsAggregateType()) {
7309 return pointee_clang_type.GetIndexOfChildMemberWithName(
7310 name, omit_empty_base_classes, child_indexes);
7311 }
7312 } break;
7313
7314 case clang::Type::Typedef:
7315 return CompilerType(getASTContext(),
7316 llvm::cast<clang::TypedefType>(qual_type)
7317 ->getDecl()
7318 ->getUnderlyingType())
7319 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7320 child_indexes);
7321
7322 case clang::Type::Auto:
7323 return CompilerType(
7324 getASTContext(),
7325 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7326 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7327 child_indexes);
7328
7329 case clang::Type::Elaborated:
7330 return CompilerType(
7331 getASTContext(),
7332 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7333 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7334 child_indexes);
7335
7336 case clang::Type::Paren:
7337 return CompilerType(getASTContext(),
7338 llvm::cast<clang::ParenType>(qual_type)->desugar())
7339 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7340 child_indexes);
7341
7342 default:
7343 break;
7344 }
7345 }
7346 return 0;
7347}
Greg Claytond8d4a572015-08-11 21:38:15 +00007348
7349// Get the index of the child of "clang_type" whose name matches. This function
7350// doesn't descend into the children, but only looks one level deep and name
7351// matches can include base class names.
7352
7353uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007354ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7355 const char *name,
7356 bool omit_empty_base_classes) {
7357 if (type && name && name[0]) {
7358 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007359
Kate Stoneb9c1b512016-09-06 20:57:50 +00007360 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7361
7362 switch (type_class) {
7363 case clang::Type::Record:
7364 if (GetCompleteType(type)) {
7365 const clang::RecordType *record_type =
7366 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7367 const clang::RecordDecl *record_decl = record_type->getDecl();
7368
7369 assert(record_decl);
7370 uint32_t child_idx = 0;
7371
7372 const clang::CXXRecordDecl *cxx_record_decl =
7373 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7374
7375 if (cxx_record_decl) {
7376 clang::CXXRecordDecl::base_class_const_iterator base_class,
7377 base_class_end;
7378 for (base_class = cxx_record_decl->bases_begin(),
7379 base_class_end = cxx_record_decl->bases_end();
7380 base_class != base_class_end; ++base_class) {
7381 // Skip empty base classes
7382 clang::CXXRecordDecl *base_class_decl =
7383 llvm::cast<clang::CXXRecordDecl>(
7384 base_class->getType()
7385 ->getAs<clang::RecordType>()
7386 ->getDecl());
7387 if (omit_empty_base_classes &&
7388 ClangASTContext::RecordHasFields(base_class_decl) == false)
7389 continue;
7390
7391 CompilerType base_class_clang_type(getASTContext(),
7392 base_class->getType());
7393 std::string base_class_type_name(
7394 base_class_clang_type.GetTypeName().AsCString(""));
7395 if (base_class_type_name.compare(name) == 0)
7396 return child_idx;
7397 ++child_idx;
7398 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007399 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007400
Kate Stoneb9c1b512016-09-06 20:57:50 +00007401 // Try and find a field that matches NAME
7402 clang::RecordDecl::field_iterator field, field_end;
7403 llvm::StringRef name_sref(name);
7404 for (field = record_decl->field_begin(),
7405 field_end = record_decl->field_end();
7406 field != field_end; ++field, ++child_idx) {
7407 if (field->getName().equals(name_sref))
7408 return child_idx;
7409 }
7410 }
7411 break;
7412
7413 case clang::Type::ObjCObject:
7414 case clang::Type::ObjCInterface:
7415 if (GetCompleteType(type)) {
7416 llvm::StringRef name_sref(name);
7417 const clang::ObjCObjectType *objc_class_type =
7418 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7419 assert(objc_class_type);
7420 if (objc_class_type) {
7421 uint32_t child_idx = 0;
7422 clang::ObjCInterfaceDecl *class_interface_decl =
7423 objc_class_type->getInterface();
7424
7425 if (class_interface_decl) {
7426 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7427 ivar_end = class_interface_decl->ivar_end();
7428 clang::ObjCInterfaceDecl *superclass_interface_decl =
7429 class_interface_decl->getSuperClass();
7430
7431 for (ivar_pos = class_interface_decl->ivar_begin();
7432 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7433 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7434
7435 if (ivar_decl->getName().equals(name_sref)) {
7436 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7437 (omit_empty_base_classes &&
7438 ObjCDeclHasIVars(superclass_interface_decl, true)))
7439 ++child_idx;
7440
7441 return child_idx;
7442 }
7443 }
7444
7445 if (superclass_interface_decl) {
7446 if (superclass_interface_decl->getName().equals(name_sref))
7447 return 0;
7448 }
7449 }
7450 }
7451 }
7452 break;
7453
7454 case clang::Type::ObjCObjectPointer: {
7455 CompilerType pointee_clang_type(
7456 getASTContext(),
7457 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7458 ->getPointeeType());
7459 return pointee_clang_type.GetIndexOfChildWithName(
7460 name, omit_empty_base_classes);
7461 } break;
7462
7463 case clang::Type::ConstantArray: {
7464 // const clang::ConstantArrayType *array =
7465 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7466 // const uint64_t element_count =
7467 // array->getSize().getLimitedValue();
7468 //
7469 // if (idx < element_count)
7470 // {
7471 // std::pair<uint64_t, unsigned> field_type_info =
7472 // ast->getTypeInfo(array->getElementType());
7473 //
7474 // char element_name[32];
7475 // ::snprintf (element_name, sizeof (element_name),
7476 // "%s[%u]", parent_name ? parent_name : "", idx);
7477 //
7478 // child_name.assign(element_name);
7479 // assert(field_type_info.first % 8 == 0);
7480 // child_byte_size = field_type_info.first / 8;
7481 // child_byte_offset = idx * child_byte_size;
7482 // return array->getElementType().getAsOpaquePtr();
7483 // }
7484 } break;
7485
7486 // case clang::Type::MemberPointerType:
7487 // {
7488 // MemberPointerType *mem_ptr_type =
7489 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7490 // clang::QualType pointee_type =
7491 // mem_ptr_type->getPointeeType();
7492 //
7493 // if (ClangASTContext::IsAggregateType
7494 // (pointee_type.getAsOpaquePtr()))
7495 // {
7496 // return GetIndexOfChildWithName (ast,
7497 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7498 // name);
7499 // }
7500 // }
7501 // break;
7502 //
7503 case clang::Type::LValueReference:
7504 case clang::Type::RValueReference: {
7505 const clang::ReferenceType *reference_type =
7506 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7507 CompilerType pointee_type(getASTContext(),
7508 reference_type->getPointeeType());
7509
7510 if (pointee_type.IsAggregateType()) {
7511 return pointee_type.GetIndexOfChildWithName(name,
7512 omit_empty_base_classes);
7513 }
7514 } break;
7515
7516 case clang::Type::Pointer: {
7517 const clang::PointerType *pointer_type =
7518 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7519 CompilerType pointee_type(getASTContext(),
7520 pointer_type->getPointeeType());
7521
7522 if (pointee_type.IsAggregateType()) {
7523 return pointee_type.GetIndexOfChildWithName(name,
7524 omit_empty_base_classes);
7525 } else {
7526 // if (parent_name)
7527 // {
7528 // child_name.assign(1, '*');
7529 // child_name += parent_name;
7530 // }
7531 //
7532 // // We have a pointer to an simple type
7533 // if (idx == 0)
7534 // {
7535 // std::pair<uint64_t, unsigned> clang_type_info
7536 // = ast->getTypeInfo(pointee_type);
7537 // assert(clang_type_info.first % 8 == 0);
7538 // child_byte_size = clang_type_info.first / 8;
7539 // child_byte_offset = 0;
7540 // return pointee_type.getAsOpaquePtr();
7541 // }
7542 }
7543 } break;
7544
7545 case clang::Type::Auto:
7546 return CompilerType(
7547 getASTContext(),
7548 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7549 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7550
7551 case clang::Type::Elaborated:
7552 return CompilerType(
7553 getASTContext(),
7554 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7555 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7556
7557 case clang::Type::Paren:
7558 return CompilerType(getASTContext(),
7559 llvm::cast<clang::ParenType>(qual_type)->desugar())
7560 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7561
7562 case clang::Type::Typedef:
7563 return CompilerType(getASTContext(),
7564 llvm::cast<clang::TypedefType>(qual_type)
7565 ->getDecl()
7566 ->getUnderlyingType())
7567 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7568
7569 default:
7570 break;
7571 }
7572 }
7573 return UINT32_MAX;
7574}
Greg Claytond8d4a572015-08-11 21:38:15 +00007575
7576size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007577ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7578 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007579 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007580
Kate Stoneb9c1b512016-09-06 20:57:50 +00007581 clang::QualType qual_type(GetCanonicalQualType(type));
7582 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7583 switch (type_class) {
7584 case clang::Type::Record:
7585 if (GetCompleteType(type)) {
7586 const clang::CXXRecordDecl *cxx_record_decl =
7587 qual_type->getAsCXXRecordDecl();
7588 if (cxx_record_decl) {
7589 const clang::ClassTemplateSpecializationDecl *template_decl =
7590 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7591 cxx_record_decl);
7592 if (template_decl)
7593 return template_decl->getTemplateArgs().size();
7594 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007595 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007596 break;
7597
7598 case clang::Type::Typedef:
7599 return (CompilerType(getASTContext(),
7600 llvm::cast<clang::TypedefType>(qual_type)
7601 ->getDecl()
7602 ->getUnderlyingType()))
7603 .GetNumTemplateArguments();
7604
7605 case clang::Type::Auto:
7606 return (CompilerType(
7607 getASTContext(),
7608 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7609 .GetNumTemplateArguments();
7610
7611 case clang::Type::Elaborated:
7612 return (CompilerType(
7613 getASTContext(),
7614 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7615 .GetNumTemplateArguments();
7616
7617 case clang::Type::Paren:
7618 return (CompilerType(getASTContext(),
7619 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7620 .GetNumTemplateArguments();
7621
7622 default:
7623 break;
7624 }
7625
7626 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007627}
7628
Pavel Labath769b21e2017-11-13 14:26:21 +00007629const clang::ClassTemplateSpecializationDecl *
7630ClangASTContext::GetAsTemplateSpecialization(
7631 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007632 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007633 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007634
7635 clang::QualType qual_type(GetCanonicalQualType(type));
7636 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7637 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007638 case clang::Type::Record: {
7639 if (! GetCompleteType(type))
7640 return nullptr;
7641 const clang::CXXRecordDecl *cxx_record_decl =
7642 qual_type->getAsCXXRecordDecl();
7643 if (!cxx_record_decl)
7644 return nullptr;
7645 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7646 cxx_record_decl);
7647 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007648
7649 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007650 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7651 ->getDecl()
7652 ->getUnderlyingType()
7653 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007654
7655 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007656 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7657 ->getDeducedType()
7658 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007659
7660 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007661 return GetAsTemplateSpecialization(
7662 llvm::cast<clang::ElaboratedType>(qual_type)
7663 ->getNamedType()
7664 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007665
7666 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007667 return GetAsTemplateSpecialization(
7668 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007669
7670 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007671 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007672 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007673}
7674
7675lldb::TemplateArgumentKind
7676ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7677 size_t arg_idx) {
7678 const clang::ClassTemplateSpecializationDecl *template_decl =
7679 GetAsTemplateSpecialization(type);
7680 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7681 return eTemplateArgumentKindNull;
7682
7683 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7684 case clang::TemplateArgument::Null:
7685 return eTemplateArgumentKindNull;
7686
7687 case clang::TemplateArgument::NullPtr:
7688 return eTemplateArgumentKindNullPtr;
7689
7690 case clang::TemplateArgument::Type:
7691 return eTemplateArgumentKindType;
7692
7693 case clang::TemplateArgument::Declaration:
7694 return eTemplateArgumentKindDeclaration;
7695
7696 case clang::TemplateArgument::Integral:
7697 return eTemplateArgumentKindIntegral;
7698
7699 case clang::TemplateArgument::Template:
7700 return eTemplateArgumentKindTemplate;
7701
7702 case clang::TemplateArgument::TemplateExpansion:
7703 return eTemplateArgumentKindTemplateExpansion;
7704
7705 case clang::TemplateArgument::Expression:
7706 return eTemplateArgumentKindExpression;
7707
7708 case clang::TemplateArgument::Pack:
7709 return eTemplateArgumentKindPack;
7710 }
7711 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7712}
7713
7714CompilerType
7715ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7716 size_t idx) {
7717 const clang::ClassTemplateSpecializationDecl *template_decl =
7718 GetAsTemplateSpecialization(type);
7719 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7720 return CompilerType();
7721
7722 const clang::TemplateArgument &template_arg =
7723 template_decl->getTemplateArgs()[idx];
7724 if (template_arg.getKind() != clang::TemplateArgument::Type)
7725 return CompilerType();
7726
7727 return CompilerType(getASTContext(), template_arg.getAsType());
7728}
7729
Pavel Labathf59056f2017-11-30 10:16:54 +00007730llvm::Optional<CompilerType::IntegralTemplateArgument>
Pavel Labath769b21e2017-11-13 14:26:21 +00007731ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7732 size_t idx) {
7733 const clang::ClassTemplateSpecializationDecl *template_decl =
7734 GetAsTemplateSpecialization(type);
7735 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
Pavel Labathf59056f2017-11-30 10:16:54 +00007736 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007737
7738 const clang::TemplateArgument &template_arg =
7739 template_decl->getTemplateArgs()[idx];
7740 if (template_arg.getKind() != clang::TemplateArgument::Integral)
Pavel Labathf59056f2017-11-30 10:16:54 +00007741 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007742
Pavel Labathf59056f2017-11-30 10:16:54 +00007743 return {{template_arg.getAsIntegral(),
7744 CompilerType(getASTContext(), template_arg.getIntegralType())}};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007745}
7746
Kate Stoneb9c1b512016-09-06 20:57:50 +00007747CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7748 if (type)
7749 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7750 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007751}
7752
Kate Stoneb9c1b512016-09-06 20:57:50 +00007753clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7754 const clang::EnumType *enutype =
7755 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7756 if (enutype)
7757 return enutype->getDecl();
7758 return NULL;
7759}
7760
7761clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7762 const clang::RecordType *record_type =
7763 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7764 if (record_type)
7765 return record_type->getDecl();
7766 return nullptr;
7767}
7768
7769clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
7770 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
7771 if (qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00007772 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007773 else
7774 return qual_type->getAsTagDecl();
Greg Claytone6b36cd2015-12-08 01:02:08 +00007775}
7776
Aleksandr Urakov709426b2018-09-10 08:08:43 +00007777clang::TypedefNameDecl *
7778ClangASTContext::GetAsTypedefDecl(const CompilerType &type) {
7779 const clang::TypedefType *typedef_type =
7780 llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type));
7781 if (typedef_type)
7782 return typedef_type->getDecl();
7783 return nullptr;
7784}
7785
Greg Claytond8d4a572015-08-11 21:38:15 +00007786clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007787ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7788 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007789}
7790
7791clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007792ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7793 const clang::ObjCObjectType *objc_class_type =
7794 llvm::dyn_cast<clang::ObjCObjectType>(
7795 ClangUtil::GetCanonicalQualType(type));
7796 if (objc_class_type)
7797 return objc_class_type->getInterface();
7798 return nullptr;
7799}
7800
7801clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007802 const CompilerType &type, llvm::StringRef name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00007803 const CompilerType &field_clang_type, AccessType access,
7804 uint32_t bitfield_bit_size) {
7805 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007806 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007807 ClangASTContext *ast =
7808 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7809 if (!ast)
7810 return nullptr;
7811 clang::ASTContext *clang_ast = ast->getASTContext();
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007812 clang::IdentifierInfo *ident = nullptr;
7813 if (!name.empty())
7814 ident = &clang_ast->Idents.get(name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00007815
7816 clang::FieldDecl *field = nullptr;
7817
7818 clang::Expr *bit_width = nullptr;
7819 if (bitfield_bit_size != 0) {
7820 llvm::APInt bitfield_bit_size_apint(
7821 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7822 bit_width = new (*clang_ast)
7823 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7824 clang_ast->IntTy, clang::SourceLocation());
7825 }
7826
7827 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7828 if (record_decl) {
7829 field = clang::FieldDecl::Create(
7830 *clang_ast, record_decl, clang::SourceLocation(),
7831 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007832 ident, // Identifier
7833 ClangUtil::GetQualType(field_clang_type), // Field type
7834 nullptr, // TInfo *
7835 bit_width, // BitWidth
7836 false, // Mutable
7837 clang::ICIS_NoInit); // HasInit
Kate Stoneb9c1b512016-09-06 20:57:50 +00007838
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007839 if (name.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +00007840 // Determine whether this field corresponds to an anonymous struct or
7841 // union.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007842 if (const clang::TagType *TagT =
7843 field->getType()->getAs<clang::TagType>()) {
7844 if (clang::RecordDecl *Rec =
7845 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7846 if (!Rec->getDeclName()) {
7847 Rec->setAnonymousStructOrUnion(true);
7848 field->setImplicit();
7849 }
7850 }
7851 }
7852
7853 if (field) {
7854 field->setAccess(
7855 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7856
7857 record_decl->addDecl(field);
7858
7859#ifdef LLDB_CONFIGURATION_DEBUG
7860 VerifyDecl(field);
7861#endif
7862 }
7863 } else {
7864 clang::ObjCInterfaceDecl *class_interface_decl =
7865 ast->GetAsObjCInterfaceDecl(type);
7866
7867 if (class_interface_decl) {
7868 const bool is_synthesized = false;
7869
7870 field_clang_type.GetCompleteType();
7871
7872 field = clang::ObjCIvarDecl::Create(
7873 *clang_ast, class_interface_decl, clang::SourceLocation(),
7874 clang::SourceLocation(),
Zachary Turnera3e2ea12018-10-23 17:22:02 +00007875 ident, // Identifier
7876 ClangUtil::GetQualType(field_clang_type), // Field type
7877 nullptr, // TypeSourceInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007878 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7879 is_synthesized);
7880
7881 if (field) {
7882 class_interface_decl->addDecl(field);
7883
7884#ifdef LLDB_CONFIGURATION_DEBUG
7885 VerifyDecl(field);
7886#endif
7887 }
7888 }
7889 }
7890 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007891}
7892
Kate Stoneb9c1b512016-09-06 20:57:50 +00007893void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7894 if (!type)
7895 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007896
Kate Stoneb9c1b512016-09-06 20:57:50 +00007897 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7898 if (!ast)
7899 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007900
Kate Stoneb9c1b512016-09-06 20:57:50 +00007901 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007902
Kate Stoneb9c1b512016-09-06 20:57:50 +00007903 if (!record_decl)
7904 return;
7905
7906 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7907
7908 IndirectFieldVector indirect_fields;
7909 clang::RecordDecl::field_iterator field_pos;
7910 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7911 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7912 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7913 last_field_pos = field_pos++) {
7914 if (field_pos->isAnonymousStructOrUnion()) {
7915 clang::QualType field_qual_type = field_pos->getType();
7916
7917 const clang::RecordType *field_record_type =
7918 field_qual_type->getAs<clang::RecordType>();
7919
7920 if (!field_record_type)
7921 continue;
7922
7923 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7924
7925 if (!field_record_decl)
7926 continue;
7927
7928 for (clang::RecordDecl::decl_iterator
7929 di = field_record_decl->decls_begin(),
7930 de = field_record_decl->decls_end();
7931 di != de; ++di) {
7932 if (clang::FieldDecl *nested_field_decl =
7933 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7934 clang::NamedDecl **chain =
7935 new (*ast->getASTContext()) clang::NamedDecl *[2];
7936 chain[0] = *field_pos;
7937 chain[1] = nested_field_decl;
7938 clang::IndirectFieldDecl *indirect_field =
7939 clang::IndirectFieldDecl::Create(
7940 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7941 nested_field_decl->getIdentifier(),
7942 nested_field_decl->getType(), {chain, 2});
7943
7944 indirect_field->setImplicit();
7945
7946 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7947 field_pos->getAccess(), nested_field_decl->getAccess()));
7948
7949 indirect_fields.push_back(indirect_field);
7950 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7951 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7952 size_t nested_chain_size =
7953 nested_indirect_field_decl->getChainingSize();
7954 clang::NamedDecl **chain = new (*ast->getASTContext())
7955 clang::NamedDecl *[nested_chain_size + 1];
7956 chain[0] = *field_pos;
7957
7958 int chain_index = 1;
7959 for (clang::IndirectFieldDecl::chain_iterator
7960 nci = nested_indirect_field_decl->chain_begin(),
7961 nce = nested_indirect_field_decl->chain_end();
7962 nci < nce; ++nci) {
7963 chain[chain_index] = *nci;
7964 chain_index++;
7965 }
7966
7967 clang::IndirectFieldDecl *indirect_field =
7968 clang::IndirectFieldDecl::Create(
7969 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7970 nested_indirect_field_decl->getIdentifier(),
7971 nested_indirect_field_decl->getType(),
7972 {chain, nested_chain_size + 1});
7973
7974 indirect_field->setImplicit();
7975
7976 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7977 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
7978
7979 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00007980 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007981 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007982 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007983 }
7984
Adrian Prantl05097242018-04-30 16:49:04 +00007985 // Check the last field to see if it has an incomplete array type as its last
7986 // member and if it does, the tell the record decl about it
Kate Stoneb9c1b512016-09-06 20:57:50 +00007987 if (last_field_pos != field_end_pos) {
7988 if (last_field_pos->getType()->isIncompleteArrayType())
7989 record_decl->hasFlexibleArrayMember();
7990 }
7991
7992 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
7993 ife = indirect_fields.end();
7994 ifi < ife; ++ifi) {
7995 record_decl->addDecl(*ifi);
7996 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007997}
7998
Kate Stoneb9c1b512016-09-06 20:57:50 +00007999void ClangASTContext::SetIsPacked(const CompilerType &type) {
8000 if (type) {
8001 ClangASTContext *ast =
8002 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8003 if (ast) {
8004 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
8005
8006 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00008007 return;
8008
Kate Stoneb9c1b512016-09-06 20:57:50 +00008009 record_decl->addAttr(
8010 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00008011 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008012 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008013}
8014
Kate Stoneb9c1b512016-09-06 20:57:50 +00008015clang::VarDecl *ClangASTContext::AddVariableToRecordType(
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008016 const CompilerType &type, llvm::StringRef name,
8017 const CompilerType &var_type, AccessType access) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00008018 if (!type.IsValid() || !var_type.IsValid())
8019 return nullptr;
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008020
Kate Stoneb9c1b512016-09-06 20:57:50 +00008021 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8022 if (!ast)
8023 return nullptr;
8024
8025 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008026 if (!record_decl)
8027 return nullptr;
8028
8029 clang::VarDecl *var_decl = nullptr;
8030 clang::IdentifierInfo *ident = nullptr;
8031 if (!name.empty())
8032 ident = &ast->getASTContext()->Idents.get(name);
8033
8034 var_decl = clang::VarDecl::Create(
8035 *ast->getASTContext(), // ASTContext &
8036 record_decl, // DeclContext *
8037 clang::SourceLocation(), // clang::SourceLocation StartLoc
8038 clang::SourceLocation(), // clang::SourceLocation IdLoc
8039 ident, // clang::IdentifierInfo *
8040 ClangUtil::GetQualType(var_type), // Variable clang::QualType
8041 nullptr, // TypeSourceInfo *
8042 clang::SC_Static); // StorageClass
8043 if (!var_decl)
8044 return nullptr;
8045
8046 var_decl->setAccess(
8047 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
8048 record_decl->addDecl(var_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008049
Greg Claytond8d4a572015-08-11 21:38:15 +00008050#ifdef LLDB_CONFIGURATION_DEBUG
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008051 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008052#endif
Zachary Turnera3e2ea12018-10-23 17:22:02 +00008053
Kate Stoneb9c1b512016-09-06 20:57:50 +00008054 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008055}
8056
Kate Stoneb9c1b512016-09-06 20:57:50 +00008057clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
Davide Italiano675767a2018-03-27 19:40:50 +00008058 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008059 const CompilerType &method_clang_type, lldb::AccessType access,
8060 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
8061 bool is_attr_used, bool is_artificial) {
8062 if (!type || !method_clang_type.IsValid() || name == nullptr ||
8063 name[0] == '\0')
8064 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008065
Kate Stoneb9c1b512016-09-06 20:57:50 +00008066 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008067
Kate Stoneb9c1b512016-09-06 20:57:50 +00008068 clang::CXXRecordDecl *cxx_record_decl =
8069 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008070
Kate Stoneb9c1b512016-09-06 20:57:50 +00008071 if (cxx_record_decl == nullptr)
8072 return nullptr;
8073
8074 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8075
8076 clang::CXXMethodDecl *cxx_method_decl = nullptr;
8077
8078 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
8079
8080 const clang::FunctionType *function_type =
8081 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
8082
8083 if (function_type == nullptr)
8084 return nullptr;
8085
8086 const clang::FunctionProtoType *method_function_prototype(
8087 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
8088
8089 if (!method_function_prototype)
8090 return nullptr;
8091
8092 unsigned int num_params = method_function_prototype->getNumParams();
8093
8094 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
8095 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
8096
8097 if (is_artificial)
8098 return nullptr; // skip everything artificial
8099
8100 if (name[0] == '~') {
8101 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
8102 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8103 clang::DeclarationNameInfo(
8104 getASTContext()->DeclarationNames.getCXXDestructorName(
8105 getASTContext()->getCanonicalType(record_qual_type)),
8106 clang::SourceLocation()),
8107 method_qual_type, nullptr, is_inline, is_artificial);
8108 cxx_method_decl = cxx_dtor_decl;
8109 } else if (decl_name == cxx_record_decl->getDeclName()) {
8110 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
8111 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8112 clang::DeclarationNameInfo(
8113 getASTContext()->DeclarationNames.getCXXConstructorName(
8114 getASTContext()->getCanonicalType(record_qual_type)),
8115 clang::SourceLocation()),
8116 method_qual_type,
8117 nullptr, // TypeSourceInfo *
8118 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
8119 cxx_method_decl = cxx_ctor_decl;
8120 } else {
8121 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8122 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8123
8124 if (IsOperator(name, op_kind)) {
8125 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
Adrian Prantl05097242018-04-30 16:49:04 +00008126 // Check the number of operator parameters. Sometimes we have seen bad
8127 // DWARF that doesn't correctly describe operators and if we try to
8128 // create a method and add it to the class, clang will assert and
8129 // crash, so we need to make sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008130 const bool is_method = true;
8131 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8132 is_method, op_kind, num_params))
8133 return nullptr;
8134 cxx_method_decl = clang::CXXMethodDecl::Create(
8135 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8136 clang::DeclarationNameInfo(
8137 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8138 clang::SourceLocation()),
8139 method_qual_type,
8140 nullptr, // TypeSourceInfo *
8141 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8142 } else if (num_params == 0) {
8143 // Conversion operators don't take params...
8144 cxx_method_decl = clang::CXXConversionDecl::Create(
8145 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8146 clang::DeclarationNameInfo(
8147 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8148 getASTContext()->getCanonicalType(
8149 function_type->getReturnType())),
8150 clang::SourceLocation()),
8151 method_qual_type,
8152 nullptr, // TypeSourceInfo *
8153 is_inline, is_explicit, false /*is_constexpr*/,
8154 clang::SourceLocation());
8155 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008156 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008157
8158 if (cxx_method_decl == nullptr) {
8159 cxx_method_decl = clang::CXXMethodDecl::Create(
8160 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8161 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8162 method_qual_type,
8163 nullptr, // TypeSourceInfo *
8164 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008165 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008166 }
8167
8168 clang::AccessSpecifier access_specifier =
8169 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8170
8171 cxx_method_decl->setAccess(access_specifier);
8172 cxx_method_decl->setVirtualAsWritten(is_virtual);
8173
8174 if (is_attr_used)
8175 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8176
Davide Italiano675767a2018-03-27 19:40:50 +00008177 if (mangled_name != NULL) {
8178 cxx_method_decl->addAttr(
8179 clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name));
8180 }
8181
Kate Stoneb9c1b512016-09-06 20:57:50 +00008182 // Populate the method decl with parameter decls
8183
8184 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8185
8186 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8187 params.push_back(clang::ParmVarDecl::Create(
8188 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8189 clang::SourceLocation(),
8190 nullptr, // anonymous
8191 method_function_prototype->getParamType(param_index), nullptr,
8192 clang::SC_None, nullptr));
8193 }
8194
8195 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8196
8197 cxx_record_decl->addDecl(cxx_method_decl);
8198
8199 // Sometimes the debug info will mention a constructor (default/copy/move),
8200 // destructor, or assignment operator (copy/move) but there won't be any
8201 // version of this in the code. So we check if the function was artificially
8202 // generated and if it is trivial and this lets the compiler/backend know
8203 // that it can inline the IR for these when it needs to and we can avoid a
8204 // "missing function" error when running expressions.
8205
8206 if (is_artificial) {
8207 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8208 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8209 (cxx_ctor_decl->isCopyConstructor() &&
8210 cxx_record_decl->hasTrivialCopyConstructor()) ||
8211 (cxx_ctor_decl->isMoveConstructor() &&
8212 cxx_record_decl->hasTrivialMoveConstructor()))) {
8213 cxx_ctor_decl->setDefaulted();
8214 cxx_ctor_decl->setTrivial(true);
8215 } else if (cxx_dtor_decl) {
8216 if (cxx_record_decl->hasTrivialDestructor()) {
8217 cxx_dtor_decl->setDefaulted();
8218 cxx_dtor_decl->setTrivial(true);
8219 }
8220 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8221 cxx_record_decl->hasTrivialCopyAssignment()) ||
8222 (cxx_method_decl->isMoveAssignmentOperator() &&
8223 cxx_record_decl->hasTrivialMoveAssignment())) {
8224 cxx_method_decl->setDefaulted();
8225 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008226 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008227 }
8228
Greg Claytond8d4a572015-08-11 21:38:15 +00008229#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008230 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008231#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008232
Kate Stoneb9c1b512016-09-06 20:57:50 +00008233 // printf ("decl->isPolymorphic() = %i\n",
8234 // cxx_record_decl->isPolymorphic());
8235 // printf ("decl->isAggregate() = %i\n",
8236 // cxx_record_decl->isAggregate());
8237 // printf ("decl->isPOD() = %i\n",
8238 // cxx_record_decl->isPOD());
8239 // printf ("decl->isEmpty() = %i\n",
8240 // cxx_record_decl->isEmpty());
8241 // printf ("decl->isAbstract() = %i\n",
8242 // cxx_record_decl->isAbstract());
8243 // printf ("decl->hasTrivialConstructor() = %i\n",
8244 // cxx_record_decl->hasTrivialConstructor());
8245 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8246 // cxx_record_decl->hasTrivialCopyConstructor());
8247 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8248 // cxx_record_decl->hasTrivialCopyAssignment());
8249 // printf ("decl->hasTrivialDestructor() = %i\n",
8250 // cxx_record_decl->hasTrivialDestructor());
8251 return cxx_method_decl;
8252}
Greg Claytond8d4a572015-08-11 21:38:15 +00008253
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00008254void ClangASTContext::AddMethodOverridesForCXXRecordType(
8255 lldb::opaque_compiler_type_t type) {
8256 if (auto *record = GetAsCXXRecordDecl(type))
8257 for (auto *method : record->methods())
8258 addOverridesForMethod(method);
8259}
8260
Greg Claytond8d4a572015-08-11 21:38:15 +00008261#pragma mark C++ Base Classes
8262
Zachary Turner970f38e2018-10-25 20:44:56 +00008263std::unique_ptr<clang::CXXBaseSpecifier>
Kate Stoneb9c1b512016-09-06 20:57:50 +00008264ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8265 AccessType access, bool is_virtual,
8266 bool base_of_class) {
Zachary Turner970f38e2018-10-25 20:44:56 +00008267 if (!type)
8268 return nullptr;
8269
8270 return llvm::make_unique<clang::CXXBaseSpecifier>(
8271 clang::SourceRange(), is_virtual, base_of_class,
8272 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8273 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8274 clang::SourceLocation());
Kate Stoneb9c1b512016-09-06 20:57:50 +00008275}
8276
Zachary Turner970f38e2018-10-25 20:44:56 +00008277bool ClangASTContext::TransferBaseClasses(
Kate Stoneb9c1b512016-09-06 20:57:50 +00008278 lldb::opaque_compiler_type_t type,
Zachary Turner970f38e2018-10-25 20:44:56 +00008279 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) {
8280 if (!type)
8281 return false;
8282 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8283 if (!cxx_record_decl)
8284 return false;
8285 std::vector<clang::CXXBaseSpecifier *> raw_bases;
8286 raw_bases.reserve(bases.size());
8287
8288 // Clang will make a copy of them, so it's ok that we pass pointers that we're
8289 // about to destroy.
8290 for (auto &b : bases)
8291 raw_bases.push_back(b.get());
8292 cxx_record_decl->setBases(raw_bases.data(), raw_bases.size());
8293 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008294}
8295
8296bool ClangASTContext::SetObjCSuperClass(
8297 const CompilerType &type, const CompilerType &superclass_clang_type) {
8298 ClangASTContext *ast =
8299 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8300 if (!ast)
8301 return false;
8302 clang::ASTContext *clang_ast = ast->getASTContext();
8303
8304 if (type && superclass_clang_type.IsValid() &&
8305 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8306 clang::ObjCInterfaceDecl *class_interface_decl =
8307 GetAsObjCInterfaceDecl(type);
8308 clang::ObjCInterfaceDecl *super_interface_decl =
8309 GetAsObjCInterfaceDecl(superclass_clang_type);
8310 if (class_interface_decl && super_interface_decl) {
8311 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8312 clang_ast->getObjCInterfaceType(super_interface_decl)));
8313 return true;
8314 }
8315 }
8316 return false;
8317}
8318
8319bool ClangASTContext::AddObjCClassProperty(
8320 const CompilerType &type, const char *property_name,
8321 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8322 const char *property_setter_name, const char *property_getter_name,
8323 uint32_t property_attributes, ClangASTMetadata *metadata) {
8324 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8325 property_name[0] == '\0')
8326 return false;
8327 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8328 if (!ast)
8329 return false;
8330 clang::ASTContext *clang_ast = ast->getASTContext();
8331
8332 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8333
8334 if (class_interface_decl) {
8335 CompilerType property_clang_type_to_access;
8336
8337 if (property_clang_type.IsValid())
8338 property_clang_type_to_access = property_clang_type;
8339 else if (ivar_decl)
8340 property_clang_type_to_access =
8341 CompilerType(clang_ast, ivar_decl->getType());
8342
8343 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8344 clang::TypeSourceInfo *prop_type_source;
8345 if (ivar_decl)
8346 prop_type_source =
8347 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8348 else
8349 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8350 ClangUtil::GetQualType(property_clang_type));
8351
8352 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8353 *clang_ast, class_interface_decl,
8354 clang::SourceLocation(), // Source Location
8355 &clang_ast->Idents.get(property_name),
8356 clang::SourceLocation(), // Source Location for AT
8357 clang::SourceLocation(), // Source location for (
8358 ivar_decl ? ivar_decl->getType()
8359 : ClangUtil::GetQualType(property_clang_type),
8360 prop_type_source);
8361
8362 if (property_decl) {
8363 if (metadata)
8364 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8365
8366 class_interface_decl->addDecl(property_decl);
8367
8368 clang::Selector setter_sel, getter_sel;
8369
8370 if (property_setter_name != nullptr) {
8371 std::string property_setter_no_colon(
8372 property_setter_name, strlen(property_setter_name) - 1);
8373 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008374 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008375 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8376 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8377 std::string setter_sel_string("set");
8378 setter_sel_string.push_back(::toupper(property_name[0]));
8379 setter_sel_string.append(&property_name[1]);
8380 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008381 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008382 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8383 }
8384 property_decl->setSetterName(setter_sel);
8385 property_decl->setPropertyAttributes(
8386 clang::ObjCPropertyDecl::OBJC_PR_setter);
8387
8388 if (property_getter_name != nullptr) {
8389 clang::IdentifierInfo *getter_ident =
8390 &clang_ast->Idents.get(property_getter_name);
8391 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8392 } else {
8393 clang::IdentifierInfo *getter_ident =
8394 &clang_ast->Idents.get(property_name);
8395 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8396 }
8397 property_decl->setGetterName(getter_sel);
8398 property_decl->setPropertyAttributes(
8399 clang::ObjCPropertyDecl::OBJC_PR_getter);
8400
8401 if (ivar_decl)
8402 property_decl->setPropertyIvarDecl(ivar_decl);
8403
8404 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8405 property_decl->setPropertyAttributes(
8406 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8407 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8408 property_decl->setPropertyAttributes(
8409 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8410 if (property_attributes & DW_APPLE_PROPERTY_assign)
8411 property_decl->setPropertyAttributes(
8412 clang::ObjCPropertyDecl::OBJC_PR_assign);
8413 if (property_attributes & DW_APPLE_PROPERTY_retain)
8414 property_decl->setPropertyAttributes(
8415 clang::ObjCPropertyDecl::OBJC_PR_retain);
8416 if (property_attributes & DW_APPLE_PROPERTY_copy)
8417 property_decl->setPropertyAttributes(
8418 clang::ObjCPropertyDecl::OBJC_PR_copy);
8419 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8420 property_decl->setPropertyAttributes(
8421 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8422 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8423 property_decl->setPropertyAttributes(
8424 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8425 if (property_attributes &
8426 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8427 property_decl->setPropertyAttributes(
8428 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8429 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8430 property_decl->setPropertyAttributes(
8431 clang::ObjCPropertyDecl::OBJC_PR_class);
8432
8433 const bool isInstance =
8434 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8435
8436 if (!getter_sel.isNull() &&
8437 !(isInstance
8438 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8439 : class_interface_decl->lookupClassMethod(getter_sel))) {
8440 const bool isVariadic = false;
8441 const bool isSynthesized = false;
8442 const bool isImplicitlyDeclared = true;
8443 const bool isDefined = false;
8444 const clang::ObjCMethodDecl::ImplementationControl impControl =
8445 clang::ObjCMethodDecl::None;
8446 const bool HasRelatedResultType = false;
8447
8448 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8449 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8450 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8451 nullptr, class_interface_decl, isInstance, isVariadic,
8452 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8453 HasRelatedResultType);
8454
8455 if (getter && metadata)
8456 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8457
8458 if (getter) {
8459 getter->setMethodParams(*clang_ast,
8460 llvm::ArrayRef<clang::ParmVarDecl *>(),
8461 llvm::ArrayRef<clang::SourceLocation>());
8462
8463 class_interface_decl->addDecl(getter);
8464 }
8465 }
8466
8467 if (!setter_sel.isNull() &&
8468 !(isInstance
8469 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8470 : class_interface_decl->lookupClassMethod(setter_sel))) {
8471 clang::QualType result_type = clang_ast->VoidTy;
8472 const bool isVariadic = false;
8473 const bool isSynthesized = false;
8474 const bool isImplicitlyDeclared = true;
8475 const bool isDefined = false;
8476 const clang::ObjCMethodDecl::ImplementationControl impControl =
8477 clang::ObjCMethodDecl::None;
8478 const bool HasRelatedResultType = false;
8479
8480 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8481 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8482 setter_sel, result_type, nullptr, class_interface_decl,
8483 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8484 isDefined, impControl, HasRelatedResultType);
8485
8486 if (setter && metadata)
8487 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8488
8489 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8490
8491 params.push_back(clang::ParmVarDecl::Create(
8492 *clang_ast, setter, clang::SourceLocation(),
8493 clang::SourceLocation(),
8494 nullptr, // anonymous
8495 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8496 clang::SC_Auto, nullptr));
8497
8498 if (setter) {
8499 setter->setMethodParams(
8500 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8501 llvm::ArrayRef<clang::SourceLocation>());
8502
8503 class_interface_decl->addDecl(setter);
8504 }
8505 }
8506
8507 return true;
8508 }
8509 }
8510 }
8511 return false;
8512}
8513
8514bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8515 bool check_superclass) {
8516 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8517 if (class_interface_decl)
8518 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8519 return false;
8520}
8521
8522clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8523 const CompilerType &type,
8524 const char *name, // the full symbol name as seen in the symbol table
8525 // (lldb::opaque_compiler_type_t type, "-[NString
8526 // stringWithCString:]")
8527 const CompilerType &method_clang_type, lldb::AccessType access,
8528 bool is_artificial, bool is_variadic) {
8529 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008530 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008531
Kate Stoneb9c1b512016-09-06 20:57:50 +00008532 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8533
8534 if (class_interface_decl == nullptr)
8535 return nullptr;
8536 ClangASTContext *lldb_ast =
8537 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8538 if (lldb_ast == nullptr)
8539 return nullptr;
8540 clang::ASTContext *ast = lldb_ast->getASTContext();
8541
8542 const char *selector_start = ::strchr(name, ' ');
8543 if (selector_start == nullptr)
8544 return nullptr;
8545
8546 selector_start++;
8547 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8548
8549 size_t len = 0;
8550 const char *start;
8551 // printf ("name = '%s'\n", name);
8552
8553 unsigned num_selectors_with_args = 0;
8554 for (start = selector_start; start && *start != '\0' && *start != ']';
8555 start += len) {
8556 len = ::strcspn(start, ":]");
8557 bool has_arg = (start[len] == ':');
8558 if (has_arg)
8559 ++num_selectors_with_args;
8560 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8561 if (has_arg)
8562 len += 1;
8563 }
8564
8565 if (selector_idents.size() == 0)
8566 return nullptr;
8567
8568 clang::Selector method_selector = ast->Selectors.getSelector(
8569 num_selectors_with_args ? selector_idents.size() : 0,
8570 selector_idents.data());
8571
8572 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8573
8574 // Populate the method decl with parameter decls
8575 const clang::Type *method_type(method_qual_type.getTypePtr());
8576
8577 if (method_type == nullptr)
8578 return nullptr;
8579
8580 const clang::FunctionProtoType *method_function_prototype(
8581 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8582
8583 if (!method_function_prototype)
8584 return nullptr;
8585
8586 bool is_synthesized = false;
8587 bool is_defined = false;
8588 clang::ObjCMethodDecl::ImplementationControl imp_control =
8589 clang::ObjCMethodDecl::None;
8590
8591 const unsigned num_args = method_function_prototype->getNumParams();
8592
8593 if (num_args != num_selectors_with_args)
8594 return nullptr; // some debug information is corrupt. We are not going to
8595 // deal with it.
8596
8597 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8598 *ast,
8599 clang::SourceLocation(), // beginLoc,
8600 clang::SourceLocation(), // endLoc,
8601 method_selector, method_function_prototype->getReturnType(),
8602 nullptr, // TypeSourceInfo *ResultTInfo,
8603 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8604 ClangUtil::GetQualType(type)),
8605 name[0] == '-', is_variadic, is_synthesized,
8606 true, // is_implicitly_declared; we force this to true because we don't
8607 // have source locations
8608 is_defined, imp_control, false /*has_related_result_type*/);
8609
8610 if (objc_method_decl == nullptr)
8611 return nullptr;
8612
8613 if (num_args > 0) {
8614 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8615
8616 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8617 params.push_back(clang::ParmVarDecl::Create(
8618 *ast, objc_method_decl, clang::SourceLocation(),
8619 clang::SourceLocation(),
8620 nullptr, // anonymous
8621 method_function_prototype->getParamType(param_index), nullptr,
8622 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008623 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008624
Kate Stoneb9c1b512016-09-06 20:57:50 +00008625 objc_method_decl->setMethodParams(
8626 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8627 llvm::ArrayRef<clang::SourceLocation>());
8628 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008629
Kate Stoneb9c1b512016-09-06 20:57:50 +00008630 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008631
Greg Claytond8d4a572015-08-11 21:38:15 +00008632#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008633 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008634#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008635
8636 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008637}
8638
Kate Stoneb9c1b512016-09-06 20:57:50 +00008639bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8640 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008641 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008642
Kate Stoneb9c1b512016-09-06 20:57:50 +00008643 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008644
Kate Stoneb9c1b512016-09-06 20:57:50 +00008645 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8646 switch (type_class) {
8647 case clang::Type::Record: {
8648 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8649 if (cxx_record_decl)
8650 return cxx_record_decl->hasExternalLexicalStorage() ||
8651 cxx_record_decl->hasExternalVisibleStorage();
8652 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008653
Kate Stoneb9c1b512016-09-06 20:57:50 +00008654 case clang::Type::Enum: {
8655 clang::EnumDecl *enum_decl =
8656 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8657 if (enum_decl)
8658 return enum_decl->hasExternalLexicalStorage() ||
8659 enum_decl->hasExternalVisibleStorage();
8660 } break;
8661
8662 case clang::Type::ObjCObject:
8663 case clang::Type::ObjCInterface: {
8664 const clang::ObjCObjectType *objc_class_type =
8665 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8666 assert(objc_class_type);
8667 if (objc_class_type) {
8668 clang::ObjCInterfaceDecl *class_interface_decl =
8669 objc_class_type->getInterface();
8670
8671 if (class_interface_decl)
8672 return class_interface_decl->hasExternalLexicalStorage() ||
8673 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008674 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008675 } break;
8676
8677 case clang::Type::Typedef:
8678 return GetHasExternalStorage(CompilerType(
8679 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8680 ->getDecl()
8681 ->getUnderlyingType()
8682 .getAsOpaquePtr()));
8683
8684 case clang::Type::Auto:
8685 return GetHasExternalStorage(CompilerType(
8686 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8687 ->getDeducedType()
8688 .getAsOpaquePtr()));
8689
8690 case clang::Type::Elaborated:
8691 return GetHasExternalStorage(CompilerType(
8692 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8693 ->getNamedType()
8694 .getAsOpaquePtr()));
8695
8696 case clang::Type::Paren:
8697 return GetHasExternalStorage(CompilerType(
8698 type.GetTypeSystem(),
8699 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8700
8701 default:
8702 break;
8703 }
8704 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008705}
8706
Kate Stoneb9c1b512016-09-06 20:57:50 +00008707bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8708 bool has_extern) {
8709 if (!type)
8710 return false;
8711
8712 clang::QualType qual_type(GetCanonicalQualType(type));
8713
8714 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8715 switch (type_class) {
8716 case clang::Type::Record: {
8717 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8718 if (cxx_record_decl) {
8719 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8720 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8721 return true;
8722 }
8723 } break;
8724
8725 case clang::Type::Enum: {
8726 clang::EnumDecl *enum_decl =
8727 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8728 if (enum_decl) {
8729 enum_decl->setHasExternalLexicalStorage(has_extern);
8730 enum_decl->setHasExternalVisibleStorage(has_extern);
8731 return true;
8732 }
8733 } break;
8734
8735 case clang::Type::ObjCObject:
8736 case clang::Type::ObjCInterface: {
8737 const clang::ObjCObjectType *objc_class_type =
8738 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8739 assert(objc_class_type);
8740 if (objc_class_type) {
8741 clang::ObjCInterfaceDecl *class_interface_decl =
8742 objc_class_type->getInterface();
8743
8744 if (class_interface_decl) {
8745 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8746 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8747 return true;
8748 }
8749 }
8750 } break;
8751
8752 case clang::Type::Typedef:
8753 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8754 ->getDecl()
8755 ->getUnderlyingType()
8756 .getAsOpaquePtr(),
8757 has_extern);
8758
8759 case clang::Type::Auto:
8760 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8761 ->getDeducedType()
8762 .getAsOpaquePtr(),
8763 has_extern);
8764
8765 case clang::Type::Elaborated:
8766 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8767 ->getNamedType()
8768 .getAsOpaquePtr(),
8769 has_extern);
8770
8771 case clang::Type::Paren:
8772 return SetHasExternalStorage(
8773 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8774 has_extern);
8775
8776 default:
8777 break;
8778 }
8779 return false;
8780}
Greg Claytond8d4a572015-08-11 21:38:15 +00008781
8782#pragma mark TagDecl
8783
Kate Stoneb9c1b512016-09-06 20:57:50 +00008784bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8785 clang::QualType qual_type(ClangUtil::GetQualType(type));
8786 if (!qual_type.isNull()) {
8787 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8788 if (tag_type) {
8789 clang::TagDecl *tag_decl = tag_type->getDecl();
8790 if (tag_decl) {
8791 tag_decl->startDefinition();
8792 return true;
8793 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008794 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008795
8796 const clang::ObjCObjectType *object_type =
8797 qual_type->getAs<clang::ObjCObjectType>();
8798 if (object_type) {
8799 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8800 if (interface_decl) {
8801 interface_decl->startDefinition();
8802 return true;
8803 }
8804 }
8805 }
8806 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008807}
8808
Kate Stoneb9c1b512016-09-06 20:57:50 +00008809bool ClangASTContext::CompleteTagDeclarationDefinition(
8810 const CompilerType &type) {
8811 clang::QualType qual_type(ClangUtil::GetQualType(type));
8812 if (!qual_type.isNull()) {
8813 // Make sure we use the same methodology as
Adrian Prantl05097242018-04-30 16:49:04 +00008814 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end
8815 // the definition. Previously we were calling
Kate Stoneb9c1b512016-09-06 20:57:50 +00008816 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8817 if (tag_type) {
8818 clang::TagDecl *tag_decl = tag_type->getDecl();
8819 if (tag_decl) {
8820 clang::CXXRecordDecl *cxx_record_decl =
8821 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8822
8823 if (cxx_record_decl) {
8824 if (!cxx_record_decl->isCompleteDefinition())
8825 cxx_record_decl->completeDefinition();
8826 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8827 cxx_record_decl->setHasExternalLexicalStorage(false);
8828 cxx_record_decl->setHasExternalVisibleStorage(false);
8829 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008830 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008831 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008832 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008833
8834 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8835
8836 if (enutype) {
8837 clang::EnumDecl *enum_decl = enutype->getDecl();
8838
8839 if (enum_decl) {
8840 if (!enum_decl->isCompleteDefinition()) {
8841 ClangASTContext *lldb_ast =
8842 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8843 if (lldb_ast == nullptr)
8844 return false;
8845 clang::ASTContext *ast = lldb_ast->getASTContext();
8846
8847 /// TODO This really needs to be fixed.
8848
8849 QualType integer_type(enum_decl->getIntegerType());
8850 if (!integer_type.isNull()) {
8851 unsigned NumPositiveBits = 1;
8852 unsigned NumNegativeBits = 0;
8853
8854 clang::QualType promotion_qual_type;
8855 // If the enum integer type is less than an integer in bit width,
8856 // then we must promote it to an integer size.
8857 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8858 ast->getTypeSize(ast->IntTy)) {
8859 if (enum_decl->getIntegerType()->isSignedIntegerType())
8860 promotion_qual_type = ast->IntTy;
8861 else
8862 promotion_qual_type = ast->UnsignedIntTy;
8863 } else
8864 promotion_qual_type = enum_decl->getIntegerType();
8865
8866 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8867 promotion_qual_type, NumPositiveBits,
8868 NumNegativeBits);
8869 }
8870 }
8871 return true;
8872 }
8873 }
8874 }
8875 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008876}
8877
Aleksandr Urakov709426b2018-09-10 08:08:43 +00008878clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
Kate Stoneb9c1b512016-09-06 20:57:50 +00008879 lldb::opaque_compiler_type_t type,
8880 const CompilerType &enumerator_clang_type, const Declaration &decl,
8881 const char *name, int64_t enum_value, uint32_t enum_value_bit_size) {
8882 if (type && enumerator_clang_type.IsValid() && name && name[0]) {
8883 clang::QualType enum_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008884
Kate Stoneb9c1b512016-09-06 20:57:50 +00008885 bool is_signed = false;
8886 enumerator_clang_type.IsIntegerType(is_signed);
Greg Claytond8d4a572015-08-11 21:38:15 +00008887 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Kate Stoneb9c1b512016-09-06 20:57:50 +00008888 if (clang_type) {
8889 const clang::EnumType *enutype =
8890 llvm::dyn_cast<clang::EnumType>(clang_type);
8891
8892 if (enutype) {
8893 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8894 enum_llvm_apsint = enum_value;
8895 clang::EnumConstantDecl *enumerator_decl =
8896 clang::EnumConstantDecl::Create(
8897 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8898 name ? &getASTContext()->Idents.get(name)
8899 : nullptr, // Identifier
8900 ClangUtil::GetQualType(enumerator_clang_type),
8901 nullptr, enum_llvm_apsint);
8902
8903 if (enumerator_decl) {
8904 enutype->getDecl()->addDecl(enumerator_decl);
8905
8906#ifdef LLDB_CONFIGURATION_DEBUG
8907 VerifyDecl(enumerator_decl);
8908#endif
8909
Aleksandr Urakov709426b2018-09-10 08:08:43 +00008910 return enumerator_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008911 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008912 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008913 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008914 }
Aleksandr Urakov709426b2018-09-10 08:08:43 +00008915 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008916}
8917
Greg Claytona1e5dc82015-08-11 22:53:00 +00008918CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00008919ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8920 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8921 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8922 if (clang_type) {
8923 const clang::EnumType *enutype =
8924 llvm::dyn_cast<clang::EnumType>(clang_type);
8925 if (enutype) {
8926 clang::EnumDecl *enum_decl = enutype->getDecl();
8927 if (enum_decl)
8928 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008929 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008930 }
8931 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008932}
8933
Kate Stoneb9c1b512016-09-06 20:57:50 +00008934CompilerType
8935ClangASTContext::CreateMemberPointerType(const CompilerType &type,
8936 const CompilerType &pointee_type) {
8937 if (type && pointee_type.IsValid() &&
8938 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8939 ClangASTContext *ast =
8940 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8941 if (!ast)
8942 return CompilerType();
8943 return CompilerType(ast->getASTContext(),
8944 ast->getASTContext()->getMemberPointerType(
8945 ClangUtil::GetQualType(pointee_type),
8946 ClangUtil::GetQualType(type).getTypePtr()));
8947 }
8948 return CompilerType();
8949}
Greg Claytond8d4a572015-08-11 21:38:15 +00008950
8951size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00008952ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
8953 const char *s, uint8_t *dst,
8954 size_t dst_size) {
8955 if (type) {
8956 clang::QualType qual_type(GetCanonicalQualType(type));
8957 uint32_t count = 0;
8958 bool is_complex = false;
8959 if (IsFloatingPointType(type, count, is_complex)) {
8960 // TODO: handle complex and vector types
8961 if (count != 1)
8962 return false;
8963
8964 llvm::StringRef s_sref(s);
8965 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
8966 s_sref);
8967
8968 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
8969 const uint64_t byte_size = bit_size / 8;
8970 if (dst_size >= byte_size) {
8971 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
8972 llvm::NextPowerOf2(byte_size) * 8);
Zachary Turner97206d52017-05-12 04:51:55 +00008973 lldb_private::Status get_data_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008974 if (scalar.GetAsMemoryData(dst, byte_size,
8975 lldb_private::endian::InlHostByteOrder(),
8976 get_data_error))
8977 return byte_size;
8978 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008979 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008980 }
8981 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00008982}
8983
Greg Claytond8d4a572015-08-11 21:38:15 +00008984//----------------------------------------------------------------------
8985// Dumping types
8986//----------------------------------------------------------------------
8987#define DEPTH_INCREMENT 2
8988
Zachary Turner49110232018-11-05 17:40:28 +00008989void ClangASTContext::Dump(Stream &s) {
Zachary Turner115209e2018-11-05 19:25:39 +00008990 Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl());
Zachary Turner49110232018-11-05 17:40:28 +00008991 tu->dump(s.AsRawOstream());
8992}
8993
Kate Stoneb9c1b512016-09-06 20:57:50 +00008994void ClangASTContext::DumpValue(
8995 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00008996 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008997 lldb::offset_t data_byte_offset, size_t data_byte_size,
8998 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
8999 bool show_summary, bool verbose, uint32_t depth) {
9000 if (!type)
9001 return;
9002
9003 clang::QualType qual_type(GetQualType(type));
9004 switch (qual_type->getTypeClass()) {
9005 case clang::Type::Record:
9006 if (GetCompleteType(type)) {
9007 const clang::RecordType *record_type =
9008 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9009 const clang::RecordDecl *record_decl = record_type->getDecl();
9010 assert(record_decl);
9011 uint32_t field_bit_offset = 0;
9012 uint32_t field_byte_offset = 0;
9013 const clang::ASTRecordLayout &record_layout =
9014 getASTContext()->getASTRecordLayout(record_decl);
9015 uint32_t child_idx = 0;
9016
9017 const clang::CXXRecordDecl *cxx_record_decl =
9018 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9019 if (cxx_record_decl) {
9020 // We might have base classes to print out first
9021 clang::CXXRecordDecl::base_class_const_iterator base_class,
9022 base_class_end;
9023 for (base_class = cxx_record_decl->bases_begin(),
9024 base_class_end = cxx_record_decl->bases_end();
9025 base_class != base_class_end; ++base_class) {
9026 const clang::CXXRecordDecl *base_class_decl =
9027 llvm::cast<clang::CXXRecordDecl>(
9028 base_class->getType()->getAs<clang::RecordType>()->getDecl());
9029
9030 // Skip empty base classes
9031 if (verbose == false &&
9032 ClangASTContext::RecordHasFields(base_class_decl) == false)
9033 continue;
9034
9035 if (base_class->isVirtual())
9036 field_bit_offset =
9037 record_layout.getVBaseClassOffset(base_class_decl)
9038 .getQuantity() *
9039 8;
9040 else
9041 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
9042 .getQuantity() *
9043 8;
9044 field_byte_offset = field_bit_offset / 8;
9045 assert(field_bit_offset % 8 == 0);
9046 if (child_idx == 0)
9047 s->PutChar('{');
9048 else
9049 s->PutChar(',');
9050
9051 clang::QualType base_class_qual_type = base_class->getType();
9052 std::string base_class_type_name(base_class_qual_type.getAsString());
9053
9054 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00009055 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
9056 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009057
9058 clang::TypeInfo base_class_type_info =
9059 getASTContext()->getTypeInfo(base_class_qual_type);
9060
9061 // Dump the value of the member
9062 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
9063 base_clang_type.DumpValue(
9064 exe_ctx,
9065 s, // Stream to dump to
9066 base_clang_type
9067 .GetFormat(), // The format with which to display the member
9068 data, // Data buffer containing all bytes for this type
9069 data_byte_offset + field_byte_offset, // Offset into "data" where
9070 // to grab value from
9071 base_class_type_info.Width / 8, // Size of this type in bytes
9072 0, // Bitfield bit size
9073 0, // Bitfield bit offset
9074 show_types, // Boolean indicating if we should show the variable
9075 // types
9076 show_summary, // Boolean indicating if we should show a summary
9077 // for the current type
9078 verbose, // Verbose output?
9079 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9080 // children
9081
9082 ++child_idx;
9083 }
9084 }
9085 uint32_t field_idx = 0;
9086 clang::RecordDecl::field_iterator field, field_end;
9087 for (field = record_decl->field_begin(),
9088 field_end = record_decl->field_end();
9089 field != field_end; ++field, ++field_idx, ++child_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009090 // Print the starting squiggly bracket (if this is the first member) or
9091 // comma (for member 2 and beyond) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009092 if (child_idx == 0)
9093 s->PutChar('{');
9094 else
9095 s->PutChar(',');
9096
9097 // Indent
9098 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
9099
9100 clang::QualType field_type = field->getType();
9101 // Print the member type if requested
Adrian Prantl05097242018-04-30 16:49:04 +00009102 // Figure out the type byte size (field_type_info.first) and alignment
9103 // (field_type_info.second) from the AST context.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009104 clang::TypeInfo field_type_info =
9105 getASTContext()->getTypeInfo(field_type);
9106 assert(field_idx < record_layout.getFieldCount());
9107 // Figure out the field offset within the current struct/union/class
9108 // type
9109 field_bit_offset = record_layout.getFieldOffset(field_idx);
9110 field_byte_offset = field_bit_offset / 8;
9111 uint32_t field_bitfield_bit_size = 0;
9112 uint32_t field_bitfield_bit_offset = 0;
9113 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
9114 field_bitfield_bit_size))
9115 field_bitfield_bit_offset = field_bit_offset % 8;
9116
9117 if (show_types) {
9118 std::string field_type_name(field_type.getAsString());
9119 if (field_bitfield_bit_size > 0)
9120 s->Printf("(%s:%u) ", field_type_name.c_str(),
9121 field_bitfield_bit_size);
9122 else
9123 s->Printf("(%s) ", field_type_name.c_str());
9124 }
9125 // Print the member name and equal sign
9126 s->Printf("%s = ", field->getNameAsString().c_str());
9127
9128 // Dump the value of the member
9129 CompilerType field_clang_type(getASTContext(), field_type);
9130 field_clang_type.DumpValue(
9131 exe_ctx,
9132 s, // Stream to dump to
9133 field_clang_type
9134 .GetFormat(), // The format with which to display the member
9135 data, // Data buffer containing all bytes for this type
9136 data_byte_offset + field_byte_offset, // Offset into "data" where to
9137 // grab value from
9138 field_type_info.Width / 8, // Size of this type in bytes
9139 field_bitfield_bit_size, // Bitfield bit size
9140 field_bitfield_bit_offset, // Bitfield bit offset
9141 show_types, // Boolean indicating if we should show the variable
9142 // types
9143 show_summary, // Boolean indicating if we should show a summary for
9144 // the current type
9145 verbose, // Verbose output?
9146 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9147 // children
9148 }
9149
9150 // Indent the trailing squiggly bracket
9151 if (child_idx > 0)
9152 s->Printf("\n%*s}", depth, "");
9153 }
9154 return;
9155
9156 case clang::Type::Enum:
9157 if (GetCompleteType(type)) {
9158 const clang::EnumType *enutype =
9159 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9160 const clang::EnumDecl *enum_decl = enutype->getDecl();
9161 assert(enum_decl);
9162 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9163 lldb::offset_t offset = data_byte_offset;
9164 const int64_t enum_value = data.GetMaxU64Bitfield(
9165 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9166 for (enum_pos = enum_decl->enumerator_begin(),
9167 enum_end_pos = enum_decl->enumerator_end();
9168 enum_pos != enum_end_pos; ++enum_pos) {
9169 if (enum_pos->getInitVal() == enum_value) {
9170 s->Printf("%s", enum_pos->getNameAsString().c_str());
9171 return;
9172 }
9173 }
Adrian Prantl05097242018-04-30 16:49:04 +00009174 // If we have gotten here we didn't get find the enumerator in the enum
9175 // decl, so just print the integer.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009176 s->Printf("%" PRIi64, enum_value);
9177 }
9178 return;
9179
9180 case clang::Type::ConstantArray: {
9181 const clang::ConstantArrayType *array =
9182 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9183 bool is_array_of_characters = false;
9184 clang::QualType element_qual_type = array->getElementType();
9185
9186 const clang::Type *canonical_type =
9187 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9188 if (canonical_type)
9189 is_array_of_characters = canonical_type->isCharType();
9190
9191 const uint64_t element_count = array->getSize().getLimitedValue();
9192
9193 clang::TypeInfo field_type_info =
9194 getASTContext()->getTypeInfo(element_qual_type);
9195
9196 uint32_t element_idx = 0;
9197 uint32_t element_offset = 0;
9198 uint64_t element_byte_size = field_type_info.Width / 8;
9199 uint32_t element_stride = element_byte_size;
9200
9201 if (is_array_of_characters) {
9202 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009203 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9204 element_byte_size, element_count, UINT32_MAX,
9205 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009206 s->PutChar('"');
9207 return;
9208 } else {
9209 CompilerType element_clang_type(getASTContext(), element_qual_type);
9210 lldb::Format element_format = element_clang_type.GetFormat();
9211
9212 for (element_idx = 0; element_idx < element_count; ++element_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009213 // Print the starting squiggly bracket (if this is the first member) or
9214 // comman (for member 2 and beyong) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009215 if (element_idx == 0)
9216 s->PutChar('{');
9217 else
9218 s->PutChar(',');
9219
9220 // Indent and print the index
9221 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9222
9223 // Figure out the field offset within the current struct/union/class
9224 // type
9225 element_offset = element_idx * element_stride;
9226
9227 // Dump the value of the member
9228 element_clang_type.DumpValue(
9229 exe_ctx,
9230 s, // Stream to dump to
9231 element_format, // The format with which to display the element
9232 data, // Data buffer containing all bytes for this type
9233 data_byte_offset +
9234 element_offset, // Offset into "data" where to grab value from
9235 element_byte_size, // Size of this type in bytes
9236 0, // Bitfield bit size
9237 0, // Bitfield bit offset
9238 show_types, // Boolean indicating if we should show the variable
9239 // types
9240 show_summary, // Boolean indicating if we should show a summary for
9241 // the current type
9242 verbose, // Verbose output?
9243 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9244 // children
9245 }
9246
9247 // Indent the trailing squiggly bracket
9248 if (element_idx > 0)
9249 s->Printf("\n%*s}", depth, "");
9250 }
9251 }
9252 return;
9253
9254 case clang::Type::Typedef: {
9255 clang::QualType typedef_qual_type =
9256 llvm::cast<clang::TypedefType>(qual_type)
9257 ->getDecl()
9258 ->getUnderlyingType();
9259
9260 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9261 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9262 clang::TypeInfo typedef_type_info =
9263 getASTContext()->getTypeInfo(typedef_qual_type);
9264 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9265
9266 return typedef_clang_type.DumpValue(
9267 exe_ctx,
9268 s, // Stream to dump to
9269 typedef_format, // The format with which to display the element
9270 data, // Data buffer containing all bytes for this type
9271 data_byte_offset, // Offset into "data" where to grab value from
9272 typedef_byte_size, // Size of this type in bytes
9273 bitfield_bit_size, // Bitfield bit size
9274 bitfield_bit_offset, // Bitfield bit offset
9275 show_types, // Boolean indicating if we should show the variable types
9276 show_summary, // Boolean indicating if we should show a summary for the
9277 // current type
9278 verbose, // Verbose output?
9279 depth); // Scope depth for any types that have children
9280 } break;
9281
9282 case clang::Type::Auto: {
9283 clang::QualType elaborated_qual_type =
9284 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9285 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9286 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9287 clang::TypeInfo elaborated_type_info =
9288 getASTContext()->getTypeInfo(elaborated_qual_type);
9289 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9290
9291 return elaborated_clang_type.DumpValue(
9292 exe_ctx,
9293 s, // Stream to dump to
9294 elaborated_format, // The format with which to display the element
9295 data, // Data buffer containing all bytes for this type
9296 data_byte_offset, // Offset into "data" where to grab value from
9297 elaborated_byte_size, // Size of this type in bytes
9298 bitfield_bit_size, // Bitfield bit size
9299 bitfield_bit_offset, // Bitfield bit offset
9300 show_types, // Boolean indicating if we should show the variable types
9301 show_summary, // Boolean indicating if we should show a summary for the
9302 // current type
9303 verbose, // Verbose output?
9304 depth); // Scope depth for any types that have children
9305 } break;
9306
9307 case clang::Type::Elaborated: {
9308 clang::QualType elaborated_qual_type =
9309 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9310 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9311 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9312 clang::TypeInfo elaborated_type_info =
9313 getASTContext()->getTypeInfo(elaborated_qual_type);
9314 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9315
9316 return elaborated_clang_type.DumpValue(
9317 exe_ctx,
9318 s, // Stream to dump to
9319 elaborated_format, // The format with which to display the element
9320 data, // Data buffer containing all bytes for this type
9321 data_byte_offset, // Offset into "data" where to grab value from
9322 elaborated_byte_size, // Size of this type in bytes
9323 bitfield_bit_size, // Bitfield bit size
9324 bitfield_bit_offset, // Bitfield bit offset
9325 show_types, // Boolean indicating if we should show the variable types
9326 show_summary, // Boolean indicating if we should show a summary for the
9327 // current type
9328 verbose, // Verbose output?
9329 depth); // Scope depth for any types that have children
9330 } break;
9331
9332 case clang::Type::Paren: {
9333 clang::QualType desugar_qual_type =
9334 llvm::cast<clang::ParenType>(qual_type)->desugar();
9335 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9336
9337 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9338 clang::TypeInfo desugar_type_info =
9339 getASTContext()->getTypeInfo(desugar_qual_type);
9340 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9341
9342 return desugar_clang_type.DumpValue(
9343 exe_ctx,
9344 s, // Stream to dump to
9345 desugar_format, // The format with which to display the element
9346 data, // Data buffer containing all bytes for this type
9347 data_byte_offset, // Offset into "data" where to grab value from
9348 desugar_byte_size, // Size of this type in bytes
9349 bitfield_bit_size, // Bitfield bit size
9350 bitfield_bit_offset, // Bitfield bit offset
9351 show_types, // Boolean indicating if we should show the variable types
9352 show_summary, // Boolean indicating if we should show a summary for the
9353 // current type
9354 verbose, // Verbose output?
9355 depth); // Scope depth for any types that have children
9356 } break;
9357
9358 default:
9359 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009360 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9361 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9362 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009363
9364 if (show_summary)
9365 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9366 break;
9367 }
9368}
9369
9370bool ClangASTContext::DumpTypeValue(
9371 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009372 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9373 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009374 ExecutionContextScope *exe_scope) {
9375 if (!type)
9376 return false;
9377 if (IsAggregateType(type)) {
9378 return false;
9379 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009380 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009381
9382 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9383 switch (type_class) {
9384 case clang::Type::Typedef: {
9385 clang::QualType typedef_qual_type =
9386 llvm::cast<clang::TypedefType>(qual_type)
9387 ->getDecl()
9388 ->getUnderlyingType();
9389 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9390 if (format == eFormatDefault)
9391 format = typedef_clang_type.GetFormat();
9392 clang::TypeInfo typedef_type_info =
9393 getASTContext()->getTypeInfo(typedef_qual_type);
9394 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9395
9396 return typedef_clang_type.DumpTypeValue(
9397 s,
9398 format, // The format with which to display the element
9399 data, // Data buffer containing all bytes for this type
9400 byte_offset, // Offset into "data" where to grab value from
9401 typedef_byte_size, // Size of this type in bytes
9402 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9403 // treat as a bitfield
9404 bitfield_bit_offset, // Offset in bits of a bitfield value if
9405 // bitfield_bit_size != 0
9406 exe_scope);
9407 } break;
9408
9409 case clang::Type::Enum:
Adrian Prantl05097242018-04-30 16:49:04 +00009410 // If our format is enum or default, show the enumeration value as its
9411 // enumeration string value, else just display it as requested.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009412 if ((format == eFormatEnum || format == eFormatDefault) &&
9413 GetCompleteType(type)) {
9414 const clang::EnumType *enutype =
9415 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9416 const clang::EnumDecl *enum_decl = enutype->getDecl();
9417 assert(enum_decl);
9418 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9419 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9420 lldb::offset_t offset = byte_offset;
9421 if (is_signed) {
9422 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9423 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9424 for (enum_pos = enum_decl->enumerator_begin(),
9425 enum_end_pos = enum_decl->enumerator_end();
9426 enum_pos != enum_end_pos; ++enum_pos) {
9427 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009428 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009429 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009430 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009431 }
9432 // If we have gotten here we didn't get find the enumerator in the
9433 // enum decl, so just print the integer.
9434 s->Printf("%" PRIi64, enum_svalue);
9435 } else {
9436 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9437 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9438 for (enum_pos = enum_decl->enumerator_begin(),
9439 enum_end_pos = enum_decl->enumerator_end();
9440 enum_pos != enum_end_pos; ++enum_pos) {
9441 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009442 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009443 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009444 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009445 }
9446 // If we have gotten here we didn't get find the enumerator in the
9447 // enum decl, so just print the integer.
9448 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009449 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009450 return true;
9451 }
9452 // format was not enum, just fall through and dump the value as
9453 // requested....
9454 LLVM_FALLTHROUGH;
9455
9456 default:
9457 // We are down to a scalar type that we just need to display.
9458 {
9459 uint32_t item_count = 1;
9460 // A few formats, we might need to modify our size and count for
9461 // depending
9462 // on how we are trying to display the value...
9463 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009464 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009465 case eFormatBoolean:
9466 case eFormatBinary:
9467 case eFormatComplex:
9468 case eFormatCString: // NULL terminated C strings
9469 case eFormatDecimal:
9470 case eFormatEnum:
9471 case eFormatHex:
9472 case eFormatHexUppercase:
9473 case eFormatFloat:
9474 case eFormatOctal:
9475 case eFormatOSType:
9476 case eFormatUnsigned:
9477 case eFormatPointer:
9478 case eFormatVectorOfChar:
9479 case eFormatVectorOfSInt8:
9480 case eFormatVectorOfUInt8:
9481 case eFormatVectorOfSInt16:
9482 case eFormatVectorOfUInt16:
9483 case eFormatVectorOfSInt32:
9484 case eFormatVectorOfUInt32:
9485 case eFormatVectorOfSInt64:
9486 case eFormatVectorOfUInt64:
9487 case eFormatVectorOfFloat32:
9488 case eFormatVectorOfFloat64:
9489 case eFormatVectorOfUInt128:
9490 break;
9491
9492 case eFormatChar:
9493 case eFormatCharPrintable:
9494 case eFormatCharArray:
9495 case eFormatBytes:
9496 case eFormatBytesWithASCII:
9497 item_count = byte_size;
9498 byte_size = 1;
9499 break;
9500
9501 case eFormatUnicode16:
9502 item_count = byte_size / 2;
9503 byte_size = 2;
9504 break;
9505
9506 case eFormatUnicode32:
9507 item_count = byte_size / 4;
9508 byte_size = 4;
9509 break;
9510 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009511 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9512 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9513 bitfield_bit_size, bitfield_bit_offset,
9514 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009515 }
9516 break;
9517 }
9518 }
9519 return 0;
9520}
9521
9522void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9523 ExecutionContext *exe_ctx, Stream *s,
9524 const lldb_private::DataExtractor &data,
9525 lldb::offset_t data_byte_offset,
9526 size_t data_byte_size) {
9527 uint32_t length = 0;
9528 if (IsCStringType(type, length)) {
9529 if (exe_ctx) {
9530 Process *process = exe_ctx->GetProcessPtr();
9531 if (process) {
9532 lldb::offset_t offset = data_byte_offset;
9533 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9534 std::vector<uint8_t> buf;
9535 if (length > 0)
9536 buf.resize(length);
9537 else
9538 buf.resize(256);
9539
Zachary Turner29cb8682017-03-03 20:57:05 +00009540 DataExtractor cstr_data(&buf.front(), buf.size(),
9541 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009542 buf.back() = '\0';
9543 size_t bytes_read;
9544 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009545 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009546 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9547 buf.size(), error)) > 0) {
9548 const size_t len = strlen((const char *)&buf.front());
9549 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009550 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009551 if (total_cstr_len == 0)
9552 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009553 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9554 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009555 total_cstr_len += len;
9556 if (len < buf.size())
9557 break;
9558 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009559 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009560 if (total_cstr_len > 0)
9561 s->PutChar('"');
9562 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009563 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009564 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009565}
9566
Kate Stoneb9c1b512016-09-06 20:57:50 +00009567void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9568 StreamFile s(stdout, false);
9569 DumpTypeDescription(type, &s);
9570 ClangASTMetadata *metadata =
9571 ClangASTContext::GetMetadata(getASTContext(), type);
9572 if (metadata) {
9573 metadata->Dump(&s);
9574 }
9575}
Greg Claytond8d4a572015-08-11 21:38:15 +00009576
Kate Stoneb9c1b512016-09-06 20:57:50 +00009577void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9578 Stream *s) {
9579 if (type) {
9580 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009581
Kate Stoneb9c1b512016-09-06 20:57:50 +00009582 llvm::SmallVector<char, 1024> buf;
9583 llvm::raw_svector_ostream llvm_ostrm(buf);
9584
9585 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9586 switch (type_class) {
9587 case clang::Type::ObjCObject:
9588 case clang::Type::ObjCInterface: {
9589 GetCompleteType(type);
9590
9591 const clang::ObjCObjectType *objc_class_type =
9592 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9593 assert(objc_class_type);
9594 if (objc_class_type) {
9595 clang::ObjCInterfaceDecl *class_interface_decl =
9596 objc_class_type->getInterface();
9597 if (class_interface_decl) {
9598 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9599 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009600 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009601 }
9602 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009603
Kate Stoneb9c1b512016-09-06 20:57:50 +00009604 case clang::Type::Typedef: {
9605 const clang::TypedefType *typedef_type =
9606 qual_type->getAs<clang::TypedefType>();
9607 if (typedef_type) {
9608 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9609 std::string clang_typedef_name(
9610 typedef_decl->getQualifiedNameAsString());
9611 if (!clang_typedef_name.empty()) {
9612 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009613 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009614 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009615 }
9616 } break;
9617
9618 case clang::Type::Auto:
9619 CompilerType(getASTContext(),
9620 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9621 .DumpTypeDescription(s);
9622 return;
9623
9624 case clang::Type::Elaborated:
9625 CompilerType(getASTContext(),
9626 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9627 .DumpTypeDescription(s);
9628 return;
9629
9630 case clang::Type::Paren:
9631 CompilerType(getASTContext(),
9632 llvm::cast<clang::ParenType>(qual_type)->desugar())
9633 .DumpTypeDescription(s);
9634 return;
9635
9636 case clang::Type::Record: {
9637 GetCompleteType(type);
9638
9639 const clang::RecordType *record_type =
9640 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9641 const clang::RecordDecl *record_decl = record_type->getDecl();
9642 const clang::CXXRecordDecl *cxx_record_decl =
9643 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9644
9645 if (cxx_record_decl)
9646 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9647 s->GetIndentLevel());
9648 else
9649 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9650 s->GetIndentLevel());
9651 } break;
9652
9653 default: {
9654 const clang::TagType *tag_type =
9655 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9656 if (tag_type) {
9657 clang::TagDecl *tag_decl = tag_type->getDecl();
9658 if (tag_decl)
9659 tag_decl->print(llvm_ostrm, 0);
9660 } else {
9661 std::string clang_type_name(qual_type.getAsString());
9662 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009663 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009664 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009665 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009666 }
9667
Kate Stoneb9c1b512016-09-06 20:57:50 +00009668 if (buf.size() > 0) {
9669 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009670 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009671 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009672}
9673
Kate Stoneb9c1b512016-09-06 20:57:50 +00009674void ClangASTContext::DumpTypeName(const CompilerType &type) {
9675 if (ClangUtil::IsClangType(type)) {
9676 clang::QualType qual_type(
9677 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9678
9679 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9680 switch (type_class) {
9681 case clang::Type::Record: {
9682 const clang::CXXRecordDecl *cxx_record_decl =
9683 qual_type->getAsCXXRecordDecl();
9684 if (cxx_record_decl)
9685 printf("class %s", cxx_record_decl->getName().str().c_str());
9686 } break;
9687
9688 case clang::Type::Enum: {
9689 clang::EnumDecl *enum_decl =
9690 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9691 if (enum_decl) {
9692 printf("enum %s", enum_decl->getName().str().c_str());
9693 }
9694 } break;
9695
9696 case clang::Type::ObjCObject:
9697 case clang::Type::ObjCInterface: {
9698 const clang::ObjCObjectType *objc_class_type =
9699 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9700 if (objc_class_type) {
9701 clang::ObjCInterfaceDecl *class_interface_decl =
9702 objc_class_type->getInterface();
Adrian Prantl05097242018-04-30 16:49:04 +00009703 // We currently can't complete objective C types through the newly
9704 // added ASTContext because it only supports TagDecl objects right
9705 // now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00009706 if (class_interface_decl)
9707 printf("@class %s", class_interface_decl->getName().str().c_str());
9708 }
9709 } break;
9710
9711 case clang::Type::Typedef:
9712 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9713 ->getDecl()
9714 ->getName()
9715 .str()
9716 .c_str());
9717 break;
9718
9719 case clang::Type::Auto:
9720 printf("auto ");
9721 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9722 llvm::cast<clang::AutoType>(qual_type)
9723 ->getDeducedType()
9724 .getAsOpaquePtr()));
9725
9726 case clang::Type::Elaborated:
9727 printf("elaborated ");
9728 return DumpTypeName(CompilerType(
9729 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9730 ->getNamedType()
9731 .getAsOpaquePtr()));
9732
9733 case clang::Type::Paren:
9734 printf("paren ");
9735 return DumpTypeName(CompilerType(
9736 type.GetTypeSystem(),
9737 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9738
9739 default:
9740 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9741 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009742 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009743 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009744}
9745
Kate Stoneb9c1b512016-09-06 20:57:50 +00009746clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9747 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9748 const char *parent_name, int tag_decl_kind,
9749 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9750 if (template_param_infos.IsValid()) {
9751 std::string template_basename(parent_name);
9752 template_basename.erase(template_basename.find('<'));
9753
9754 return CreateClassTemplateDecl(decl_ctx, access_type,
9755 template_basename.c_str(), tag_decl_kind,
9756 template_param_infos);
9757 }
9758 return NULL;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009759}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009760
Kate Stoneb9c1b512016-09-06 20:57:50 +00009761void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9762 ClangASTContext *ast = (ClangASTContext *)baton;
9763 SymbolFile *sym_file = ast->GetSymbolFile();
9764 if (sym_file) {
9765 CompilerType clang_type = GetTypeForDecl(decl);
9766 if (clang_type)
9767 sym_file->CompleteType(clang_type);
9768 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009769}
9770
Kate Stoneb9c1b512016-09-06 20:57:50 +00009771void ClangASTContext::CompleteObjCInterfaceDecl(
9772 void *baton, clang::ObjCInterfaceDecl *decl) {
9773 ClangASTContext *ast = (ClangASTContext *)baton;
9774 SymbolFile *sym_file = ast->GetSymbolFile();
9775 if (sym_file) {
9776 CompilerType clang_type = GetTypeForDecl(decl);
9777 if (clang_type)
9778 sym_file->CompleteType(clang_type);
9779 }
Zachary Turner42dff792016-04-15 00:21:26 +00009780}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009781
Kate Stoneb9c1b512016-09-06 20:57:50 +00009782DWARFASTParser *ClangASTContext::GetDWARFParser() {
9783 if (!m_dwarf_ast_parser_ap)
9784 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9785 return m_dwarf_ast_parser_ap.get();
9786}
9787
9788PDBASTParser *ClangASTContext::GetPDBParser() {
9789 if (!m_pdb_ast_parser_ap)
9790 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9791 return m_pdb_ast_parser_ap.get();
9792}
9793
9794bool ClangASTContext::LayoutRecordType(
9795 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9796 uint64_t &alignment,
9797 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9798 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9799 &base_offsets,
9800 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9801 &vbase_offsets) {
9802 ClangASTContext *ast = (ClangASTContext *)baton;
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009803 lldb_private::ClangASTImporter *importer = nullptr;
9804 if (ast->m_dwarf_ast_parser_ap)
9805 importer = &ast->m_dwarf_ast_parser_ap->GetClangASTImporter();
9806 if (!importer && ast->m_pdb_ast_parser_ap)
9807 importer = &ast->m_pdb_ast_parser_ap->GetClangASTImporter();
9808 if (!importer)
9809 return false;
9810
9811 return importer->LayoutRecordType(record_decl, bit_size, alignment,
9812 field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009813}
9814
Greg Clayton99558cc42015-08-24 23:46:31 +00009815//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009816// CompilerDecl override functions
9817//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009818
Kate Stoneb9c1b512016-09-06 20:57:50 +00009819ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9820 if (opaque_decl) {
9821 clang::NamedDecl *nd =
9822 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9823 if (nd != nullptr)
9824 return ConstString(nd->getDeclName().getAsString());
9825 }
9826 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009827}
9828
Kate Stoneb9c1b512016-09-06 20:57:50 +00009829ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9830 if (opaque_decl) {
9831 clang::NamedDecl *nd =
9832 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9833 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9834 clang::MangleContext *mc = getMangleContext();
9835 if (mc && mc->shouldMangleCXXName(nd)) {
9836 llvm::SmallVector<char, 1024> buf;
9837 llvm::raw_svector_ostream llvm_ostrm(buf);
9838 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9839 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9840 Ctor_Complete, llvm_ostrm);
9841 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9842 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9843 Dtor_Complete, llvm_ostrm);
9844 } else {
9845 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009846 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009847 if (buf.size() > 0)
9848 return ConstString(buf.data(), buf.size());
9849 }
Greg Claytonfe689042015-11-10 17:47:04 +00009850 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009851 }
9852 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009853}
9854
Kate Stoneb9c1b512016-09-06 20:57:50 +00009855CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9856 if (opaque_decl)
9857 return CompilerDeclContext(this,
9858 ((clang::Decl *)opaque_decl)->getDeclContext());
9859 else
9860 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009861}
9862
Kate Stoneb9c1b512016-09-06 20:57:50 +00009863CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9864 if (clang::FunctionDecl *func_decl =
9865 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9866 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9867 if (clang::ObjCMethodDecl *objc_method =
9868 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9869 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9870 else
Greg Claytonfe689042015-11-10 17:47:04 +00009871 return CompilerType();
9872}
9873
Kate Stoneb9c1b512016-09-06 20:57:50 +00009874size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9875 if (clang::FunctionDecl *func_decl =
9876 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9877 return func_decl->param_size();
9878 if (clang::ObjCMethodDecl *objc_method =
9879 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9880 return objc_method->param_size();
9881 else
9882 return 0;
9883}
9884
9885CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9886 size_t idx) {
9887 if (clang::FunctionDecl *func_decl =
9888 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9889 if (idx < func_decl->param_size()) {
9890 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9891 if (var_decl)
9892 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9893 }
9894 } else if (clang::ObjCMethodDecl *objc_method =
9895 llvm::dyn_cast<clang::ObjCMethodDecl>(
9896 (clang::Decl *)opaque_decl)) {
9897 if (idx < objc_method->param_size())
9898 return CompilerType(
9899 this,
9900 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9901 }
9902 return CompilerType();
9903}
9904
Paul Hermand628cbb2015-09-15 23:44:17 +00009905//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00009906// CompilerDeclContext functions
9907//----------------------------------------------------------------------
9908
Kate Stoneb9c1b512016-09-06 20:57:50 +00009909std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9910 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9911 std::vector<CompilerDecl> found_decls;
9912 if (opaque_decl_ctx) {
9913 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9914 std::set<DeclContext *> searched;
9915 std::multimap<DeclContext *, DeclContext *> search_queue;
9916 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009917
Kate Stoneb9c1b512016-09-06 20:57:50 +00009918 for (clang::DeclContext *decl_context = root_decl_ctx;
9919 decl_context != nullptr && found_decls.empty();
9920 decl_context = decl_context->getParent()) {
9921 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +00009922
Kate Stoneb9c1b512016-09-06 20:57:50 +00009923 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9924 it++) {
9925 if (!searched.insert(it->second).second)
9926 continue;
9927 symbol_file->ParseDeclsForContext(
9928 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +00009929
Kate Stoneb9c1b512016-09-06 20:57:50 +00009930 for (clang::Decl *child : it->second->decls()) {
9931 if (clang::UsingDirectiveDecl *ud =
9932 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9933 if (ignore_using_decls)
9934 continue;
9935 clang::DeclContext *from = ud->getCommonAncestor();
9936 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9937 search_queue.insert(
9938 std::make_pair(from, ud->getNominatedNamespace()));
9939 } else if (clang::UsingDecl *ud =
9940 llvm::dyn_cast<clang::UsingDecl>(child)) {
9941 if (ignore_using_decls)
9942 continue;
9943 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9944 clang::Decl *target = usd->getTargetDecl();
9945 if (clang::NamedDecl *nd =
9946 llvm::dyn_cast<clang::NamedDecl>(target)) {
9947 IdentifierInfo *ii = nd->getIdentifier();
9948 if (ii != nullptr &&
9949 ii->getName().equals(name.AsCString(nullptr)))
9950 found_decls.push_back(CompilerDecl(this, nd));
9951 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009952 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009953 } else if (clang::NamedDecl *nd =
9954 llvm::dyn_cast<clang::NamedDecl>(child)) {
9955 IdentifierInfo *ii = nd->getIdentifier();
9956 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9957 found_decls.push_back(CompilerDecl(this, nd));
9958 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009959 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009960 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009961 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009962 }
9963 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +00009964}
9965
Dawn Perchikb5925782015-12-12 19:31:41 +00009966// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009967// and return the number of levels it took to find it, or
Adrian Prantl05097242018-04-30 16:49:04 +00009968// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
9969// declaration, its name and/or type, if set, will be used to check that the
9970// decl found in the scope is a match.
Dawn Perchikb5925782015-12-12 19:31:41 +00009971//
Kate Stoneb9c1b512016-09-06 20:57:50 +00009972// The optional name is required by languages (like C++) to handle using
Adrian Prantl05097242018-04-30 16:49:04 +00009973// declarations like:
Dawn Perchikb5925782015-12-12 19:31:41 +00009974//
9975// void poo();
9976// namespace ns {
9977// void foo();
9978// void goo();
9979// }
9980// void bar() {
9981// using ns::foo;
9982// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
9983// // LLDB_INVALID_DECL_LEVEL for 'goo'.
9984// }
9985//
9986// The optional type is useful in the case that there's a specific overload
9987// that we're looking for that might otherwise be shadowed, like:
9988//
9989// void foo(int);
9990// namespace ns {
9991// void foo();
9992// }
9993// void bar() {
9994// using ns::foo;
9995// // CountDeclLevels returns 0 for { 'foo', void() },
9996// // 1 for { 'foo', void(int) }, and
9997// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
9998// }
9999//
10000// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +000010001// function at file scope will return the same level as a function at global
Adrian Prantl05097242018-04-30 16:49:04 +000010002// scope. Ideally we'd like to treat the file scope as an additional scope just
10003// below the global scope. More work needs to be done to recognise that, if
10004// the decl we're trying to look up is static, we should compare its source
10005// file with that of the current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010006uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
10007 clang::DeclContext *child_decl_ctx,
10008 ConstString *child_name,
10009 CompilerType *child_type) {
10010 if (frame_decl_ctx) {
10011 std::set<DeclContext *> searched;
10012 std::multimap<DeclContext *, DeclContext *> search_queue;
10013 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +000010014
Kate Stoneb9c1b512016-09-06 20:57:50 +000010015 // Get the lookup scope for the decl we're trying to find.
10016 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +000010017
Kate Stoneb9c1b512016-09-06 20:57:50 +000010018 // Look for it in our scope's decl context and its parents.
10019 uint32_t level = 0;
10020 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
10021 decl_ctx = decl_ctx->getParent()) {
10022 if (!decl_ctx->isLookupContext())
10023 continue;
10024 if (decl_ctx == parent_decl_ctx)
10025 // Found it!
10026 return level;
10027 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
10028 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
10029 it++) {
10030 if (searched.find(it->second) != searched.end())
10031 continue;
10032
10033 // Currently DWARF has one shared translation unit for all Decls at top
Adrian Prantl05097242018-04-30 16:49:04 +000010034 // level, so this would erroneously find using statements anywhere. So
10035 // don't look at the top-level translation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +000010036 // TODO fix this and add a testcase that depends on it.
10037
10038 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
10039 continue;
10040
10041 searched.insert(it->second);
10042 symbol_file->ParseDeclsForContext(
10043 CompilerDeclContext(this, it->second));
10044
10045 for (clang::Decl *child : it->second->decls()) {
10046 if (clang::UsingDirectiveDecl *ud =
10047 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10048 clang::DeclContext *ns = ud->getNominatedNamespace();
10049 if (ns == parent_decl_ctx)
10050 // Found it!
10051 return level;
10052 clang::DeclContext *from = ud->getCommonAncestor();
10053 if (searched.find(ns) == searched.end())
10054 search_queue.insert(std::make_pair(from, ns));
10055 } else if (child_name) {
10056 if (clang::UsingDecl *ud =
10057 llvm::dyn_cast<clang::UsingDecl>(child)) {
10058 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10059 clang::Decl *target = usd->getTargetDecl();
10060 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
10061 if (!nd)
10062 continue;
10063 // Check names.
10064 IdentifierInfo *ii = nd->getIdentifier();
10065 if (ii == nullptr ||
10066 !ii->getName().equals(child_name->AsCString(nullptr)))
10067 continue;
10068 // Check types, if one was provided.
10069 if (child_type) {
10070 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
10071 if (!AreTypesSame(clang_type, *child_type,
10072 /*ignore_qualifiers=*/true))
10073 continue;
10074 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010075 // Found it!
10076 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010077 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010078 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010079 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010080 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010081 }
10082 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +000010083 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010084 }
10085 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +000010086}
10087
Kate Stoneb9c1b512016-09-06 20:57:50 +000010088bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
10089 if (opaque_decl_ctx)
10090 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
10091 else
Greg Clayton99558cc42015-08-24 23:46:31 +000010092 return false;
10093}
10094
Kate Stoneb9c1b512016-09-06 20:57:50 +000010095ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
10096 if (opaque_decl_ctx) {
10097 clang::NamedDecl *named_decl =
10098 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10099 if (named_decl)
10100 return ConstString(named_decl->getName());
10101 }
10102 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +000010103}
10104
Kate Stoneb9c1b512016-09-06 20:57:50 +000010105ConstString
10106ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
10107 if (opaque_decl_ctx) {
10108 clang::NamedDecl *named_decl =
10109 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10110 if (named_decl)
10111 return ConstString(
10112 llvm::StringRef(named_decl->getQualifiedNameAsString()));
10113 }
10114 return ConstString();
10115}
10116
10117bool ClangASTContext::DeclContextIsClassMethod(
10118 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
10119 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
10120 if (opaque_decl_ctx) {
10121 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10122 if (ObjCMethodDecl *objc_method =
10123 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
10124 if (is_instance_method_ptr)
10125 *is_instance_method_ptr = objc_method->isInstanceMethod();
10126 if (language_ptr)
10127 *language_ptr = eLanguageTypeObjC;
10128 if (language_object_name_ptr)
10129 language_object_name_ptr->SetCString("self");
10130 return true;
10131 } else if (CXXMethodDecl *cxx_method =
10132 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
10133 if (is_instance_method_ptr)
10134 *is_instance_method_ptr = cxx_method->isInstance();
10135 if (language_ptr)
10136 *language_ptr = eLanguageTypeC_plus_plus;
10137 if (language_object_name_ptr)
10138 language_object_name_ptr->SetCString("this");
10139 return true;
10140 } else if (clang::FunctionDecl *function_decl =
10141 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
10142 ClangASTMetadata *metadata =
10143 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
10144 if (metadata && metadata->HasObjectPtr()) {
10145 if (is_instance_method_ptr)
10146 *is_instance_method_ptr = true;
10147 if (language_ptr)
10148 *language_ptr = eLanguageTypeObjC;
10149 if (language_object_name_ptr)
10150 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10151 return true;
10152 }
10153 }
10154 }
10155 return false;
10156}
10157
10158clang::DeclContext *
10159ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10160 if (dc.IsClang())
10161 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10162 return nullptr;
10163}
Greg Clayton99558cc42015-08-24 23:46:31 +000010164
10165ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010166ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10167 if (dc.IsClang())
10168 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10169 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10170 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010171}
10172
10173CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010174ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10175 if (dc.IsClang())
10176 return llvm::dyn_cast<clang::CXXMethodDecl>(
10177 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10178 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010179}
10180
10181clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010182ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10183 if (dc.IsClang())
10184 return llvm::dyn_cast<clang::FunctionDecl>(
10185 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10186 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010187}
10188
10189clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010190ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10191 if (dc.IsClang())
10192 return llvm::dyn_cast<clang::NamespaceDecl>(
10193 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10194 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010195}
10196
10197ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010198ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10199 const void *object) {
10200 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10201 if (ast)
10202 return ClangASTContext::GetMetadata(ast, object);
10203 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010204}
10205
10206clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010207ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10208 ClangASTContext *ast =
10209 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10210 if (ast)
10211 return ast->getASTContext();
10212 return nullptr;
10213}
10214
10215ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10216 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10217 m_target_wp(target.shared_from_this()),
10218 m_persistent_variables(new ClangPersistentVariables) {}
10219
10220UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010221 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010222 Expression::ResultType desired_type,
10223 const EvaluateExpressionOptions &options) {
10224 TargetSP target_sp = m_target_wp.lock();
10225 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010226 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010227
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010228 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010229 desired_type, options);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010230}
10231
Kate Stoneb9c1b512016-09-06 20:57:50 +000010232FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10233 const CompilerType &return_type, const Address &function_address,
10234 const ValueList &arg_value_list, const char *name) {
10235 TargetSP target_sp = m_target_wp.lock();
10236 if (!target_sp)
10237 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010238
Kate Stoneb9c1b512016-09-06 20:57:50 +000010239 Process *process = target_sp->GetProcessSP().get();
10240 if (!process)
10241 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010242
Kate Stoneb9c1b512016-09-06 20:57:50 +000010243 return new ClangFunctionCaller(*process, return_type, function_address,
10244 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010245}
10246
10247UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010248ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10249 const char *name) {
10250 TargetSP target_sp = m_target_wp.lock();
10251 if (!target_sp)
10252 return nullptr;
10253
10254 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010255}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010256
10257PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010258ClangASTContextForExpressions::GetPersistentExpressionState() {
10259 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010260}
Sean Callanan68e44232017-09-28 20:20:25 +000010261
10262clang::ExternalASTMerger &
10263ClangASTContextForExpressions::GetMergerUnchecked() {
Eugene Zemtsova9d928c2017-09-29 03:15:08 +000010264 lldbassert(m_scratch_ast_source_ap != nullptr);
Sean Callanan68e44232017-09-28 20:20:25 +000010265 return m_scratch_ast_source_ap->GetMergerUnchecked();
10266}