blob: 043aaa3f2e2d30921410e9fc3996cf676bf660c7 [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()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 Opts.ObjC1 = Opts.ObjC2 = 1;
463 }
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
Sean Callanan09ab4b72011-11-30 22:11:59 +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,
2061 unsigned num_args, bool is_variadic, unsigned type_quals) {
2062 if (ast == nullptr)
2063 return CompilerType(); // invalid AST
2064
2065 if (!result_type || !ClangUtil::IsClangType(result_type))
2066 return CompilerType(); // invalid return type
2067
2068 std::vector<QualType> qual_type_args;
2069 if (num_args > 0 && args == nullptr)
2070 return CompilerType(); // invalid argument array passed in
2071
2072 // Verify that all arguments are valid and the right type
2073 for (unsigned i = 0; i < num_args; ++i) {
2074 if (args[i]) {
2075 // Make sure we have a clang type in args[i] and not a type from another
2076 // language whose name might match
2077 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2078 lldbassert(is_clang_type);
2079 if (is_clang_type)
2080 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2081 else
2082 return CompilerType(); // invalid argument type (must be a clang type)
2083 } else
2084 return CompilerType(); // invalid argument type (empty)
2085 }
2086
2087 // TODO: Detect calling convention in DWARF?
2088 FunctionProtoType::ExtProtoInfo proto_info;
2089 proto_info.Variadic = is_variadic;
2090 proto_info.ExceptionSpec = EST_None;
2091 proto_info.TypeQuals = type_quals;
2092 proto_info.RefQualifier = RQ_None;
2093
2094 return CompilerType(ast,
2095 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2096 qual_type_args, proto_info));
2097}
2098
2099ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
2100 const char *name, const CompilerType &param_type, int storage) {
2101 ASTContext *ast = getASTContext();
2102 assert(ast != nullptr);
2103 return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(),
2104 SourceLocation(), SourceLocation(),
2105 name && name[0] ? &ast->Idents.get(name) : nullptr,
2106 ClangUtil::GetQualType(param_type), nullptr,
2107 (clang::StorageClass)storage, nullptr);
2108}
2109
2110void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2111 ParmVarDecl **params,
2112 unsigned num_params) {
2113 if (function_decl)
2114 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002115}
2116
Greg Claytona1e5dc82015-08-11 22:53:00 +00002117CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002118ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
2119 QualType block_type = m_ast_ap->getBlockPointerType(
2120 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002121
Kate Stoneb9c1b512016-09-06 20:57:50 +00002122 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002123}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002124
2125#pragma mark Array Types
2126
Kate Stoneb9c1b512016-09-06 20:57:50 +00002127CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2128 size_t element_count,
2129 bool is_vector) {
2130 if (element_type.IsValid()) {
2131 ASTContext *ast = getASTContext();
2132 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002133
Kate Stoneb9c1b512016-09-06 20:57:50 +00002134 if (is_vector) {
2135 return CompilerType(
2136 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2137 element_count));
2138 } else {
2139
2140 llvm::APInt ap_element_count(64, element_count);
2141 if (element_count == 0) {
2142 return CompilerType(ast, ast->getIncompleteArrayType(
2143 ClangUtil::GetQualType(element_type),
2144 clang::ArrayType::Normal, 0));
2145 } else {
2146 return CompilerType(
2147 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2148 ap_element_count,
2149 clang::ArrayType::Normal, 0));
2150 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002151 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002152 }
2153 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002154}
2155
Kate Stoneb9c1b512016-09-06 20:57:50 +00002156CompilerType ClangASTContext::CreateStructForIdentifier(
2157 const ConstString &type_name,
2158 const std::initializer_list<std::pair<const char *, CompilerType>>
2159 &type_fields,
2160 bool packed) {
2161 CompilerType type;
2162 if (!type_name.IsEmpty() &&
2163 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2164 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002165 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002166 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002167 }
2168
2169 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2170 clang::TTK_Struct, lldb::eLanguageTypeC);
2171 StartTagDeclarationDefinition(type);
2172 for (const auto &field : type_fields)
2173 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2174 0);
2175 if (packed)
2176 SetIsPacked(type);
2177 CompleteTagDeclarationDefinition(type);
2178 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002179}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002180
Kate Stoneb9c1b512016-09-06 20:57:50 +00002181CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
2182 const ConstString &type_name,
2183 const std::initializer_list<std::pair<const char *, CompilerType>>
2184 &type_fields,
2185 bool packed) {
2186 CompilerType type;
2187 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2188 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002189
Kate Stoneb9c1b512016-09-06 20:57:50 +00002190 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002191}
2192
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002193#pragma mark Enumeration Types
2194
Greg Claytona1e5dc82015-08-11 22:53:00 +00002195CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002196ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2197 const Declaration &decl,
Tamas Berghammer59765832017-11-07 10:39:22 +00002198 const CompilerType &integer_clang_type,
2199 bool is_scoped) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002200 // TODO: Do something intelligent with the Declaration object passed in
2201 // like maybe filling in the SourceLocation with it...
2202 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002203
Kate Stoneb9c1b512016-09-06 20:57:50 +00002204 // TODO: ask about these...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002205 // const bool IsFixed = false;
2206
2207 EnumDecl *enum_decl = EnumDecl::Create(
2208 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2209 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
Tamas Berghammercf6bf4c2017-11-07 13:43:55 +00002210 is_scoped, // IsScoped
2211 is_scoped, // IsScopedUsingClassTag
2212 false); // IsFixed
Kate Stoneb9c1b512016-09-06 20:57:50 +00002213
2214 if (enum_decl) {
2215 // TODO: check if we should be setting the promotion type too?
2216 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2217
2218 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2219
2220 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2221 }
2222 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002223}
2224
Kate Stoneb9c1b512016-09-06 20:57:50 +00002225CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2226 size_t bit_size,
2227 bool is_signed) {
2228 if (ast) {
2229 if (is_signed) {
2230 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2231 return CompilerType(ast, ast->SignedCharTy);
2232
2233 if (bit_size == ast->getTypeSize(ast->ShortTy))
2234 return CompilerType(ast, ast->ShortTy);
2235
2236 if (bit_size == ast->getTypeSize(ast->IntTy))
2237 return CompilerType(ast, ast->IntTy);
2238
2239 if (bit_size == ast->getTypeSize(ast->LongTy))
2240 return CompilerType(ast, ast->LongTy);
2241
2242 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2243 return CompilerType(ast, ast->LongLongTy);
2244
2245 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2246 return CompilerType(ast, ast->Int128Ty);
2247 } else {
2248 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2249 return CompilerType(ast, ast->UnsignedCharTy);
2250
2251 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2252 return CompilerType(ast, ast->UnsignedShortTy);
2253
2254 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2255 return CompilerType(ast, ast->UnsignedIntTy);
2256
2257 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2258 return CompilerType(ast, ast->UnsignedLongTy);
2259
2260 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2261 return CompilerType(ast, ast->UnsignedLongLongTy);
2262
2263 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2264 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002265 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002266 }
2267 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002268}
2269
Kate Stoneb9c1b512016-09-06 20:57:50 +00002270CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2271 bool is_signed) {
2272 if (ast)
2273 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2274 is_signed);
2275 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002276}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002277
Kate Stoneb9c1b512016-09-06 20:57:50 +00002278void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2279 if (decl_ctx) {
2280 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002281
Kate Stoneb9c1b512016-09-06 20:57:50 +00002282 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2283 if (named_decl) {
2284 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2285 named_decl->getDeclName().getAsString().c_str());
2286 } else {
2287 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002288 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002289 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002290}
2291
Kate Stoneb9c1b512016-09-06 20:57:50 +00002292void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2293 if (decl == nullptr)
2294 return;
2295 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002296
Kate Stoneb9c1b512016-09-06 20:57:50 +00002297 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2298 if (record_decl) {
2299 printf("%20s: %s%s\n", decl->getDeclKindName(),
2300 record_decl->getDeclName().getAsString().c_str(),
2301 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002302
Kate Stoneb9c1b512016-09-06 20:57:50 +00002303 } else {
2304 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2305 if (named_decl) {
2306 printf("%20s: %s\n", decl->getDeclKindName(),
2307 named_decl->getDeclName().getAsString().c_str());
2308 } else {
2309 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002310 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002311 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002312}
2313
Kate Stoneb9c1b512016-09-06 20:57:50 +00002314bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2315 clang::Decl *rhs_decl) {
2316 if (lhs_decl && rhs_decl) {
2317 //----------------------------------------------------------------------
2318 // Make sure the decl kinds match first
2319 //----------------------------------------------------------------------
2320 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2321 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002322
Kate Stoneb9c1b512016-09-06 20:57:50 +00002323 if (lhs_decl_kind == rhs_decl_kind) {
2324 //------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00002325 // Now check that the decl contexts kinds are all equivalent before we
2326 // have to check any names of the decl contexts...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002327 //------------------------------------------------------------------
2328 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2329 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2330 if (lhs_decl_ctx && rhs_decl_ctx) {
2331 while (1) {
2332 if (lhs_decl_ctx && rhs_decl_ctx) {
2333 const clang::Decl::Kind lhs_decl_ctx_kind =
2334 lhs_decl_ctx->getDeclKind();
2335 const clang::Decl::Kind rhs_decl_ctx_kind =
2336 rhs_decl_ctx->getDeclKind();
2337 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2338 lhs_decl_ctx = lhs_decl_ctx->getParent();
2339 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002340
Kate Stoneb9c1b512016-09-06 20:57:50 +00002341 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2342 break;
2343 } else
2344 return false;
2345 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002346 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002347 }
2348
2349 //--------------------------------------------------------------
2350 // Now make sure the name of the decls match
2351 //--------------------------------------------------------------
2352 clang::NamedDecl *lhs_named_decl =
2353 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2354 clang::NamedDecl *rhs_named_decl =
2355 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2356 if (lhs_named_decl && rhs_named_decl) {
2357 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2358 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2359 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2360 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2361 return false;
2362 } else
Greg Claytona2721472011-06-25 00:44:06 +00002363 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002364 } else
2365 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002366
Kate Stoneb9c1b512016-09-06 20:57:50 +00002367 //--------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00002368 // We know that the decl context kinds all match, so now we need to
2369 // make sure the names match as well
Kate Stoneb9c1b512016-09-06 20:57:50 +00002370 //--------------------------------------------------------------
2371 lhs_decl_ctx = lhs_decl->getDeclContext();
2372 rhs_decl_ctx = rhs_decl->getDeclContext();
2373 while (1) {
2374 switch (lhs_decl_ctx->getDeclKind()) {
2375 case clang::Decl::TranslationUnit:
2376 // We don't care about the translation unit names
2377 return true;
2378 default: {
2379 clang::NamedDecl *lhs_named_decl =
2380 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2381 clang::NamedDecl *rhs_named_decl =
2382 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2383 if (lhs_named_decl && rhs_named_decl) {
2384 clang::DeclarationName lhs_decl_name =
2385 lhs_named_decl->getDeclName();
2386 clang::DeclarationName rhs_decl_name =
2387 rhs_named_decl->getDeclName();
2388 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2389 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2390 return false;
2391 } else
2392 return false;
2393 } else
2394 return false;
2395 } break;
2396 }
2397 lhs_decl_ctx = lhs_decl_ctx->getParent();
2398 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002399 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002400 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002401 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002402 }
2403 return false;
2404}
2405bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2406 clang::Decl *decl) {
2407 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002408 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002409
Kate Stoneb9c1b512016-09-06 20:57:50 +00002410 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002411
Kate Stoneb9c1b512016-09-06 20:57:50 +00002412 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002413 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002414
2415 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2416 if (tag_decl->isCompleteDefinition())
2417 return true;
2418
2419 if (!tag_decl->hasExternalLexicalStorage())
2420 return false;
2421
2422 ast_source->CompleteType(tag_decl);
2423
2424 return !tag_decl->getTypeForDecl()->isIncompleteType();
2425 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2426 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2427 if (objc_interface_decl->getDefinition())
2428 return true;
2429
2430 if (!objc_interface_decl->hasExternalLexicalStorage())
2431 return false;
2432
2433 ast_source->CompleteType(objc_interface_decl);
2434
2435 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2436 } else {
2437 return false;
2438 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002439}
2440
Kate Stoneb9c1b512016-09-06 20:57:50 +00002441void ClangASTContext::SetMetadataAsUserID(const void *object,
2442 user_id_t user_id) {
2443 ClangASTMetadata meta_data;
2444 meta_data.SetUserID(user_id);
2445 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002446}
2447
Kate Stoneb9c1b512016-09-06 20:57:50 +00002448void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2449 ClangASTMetadata &metadata) {
2450 ClangExternalASTSourceCommon *external_source =
2451 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2452
2453 if (external_source)
2454 external_source->SetMetadata(object, metadata);
2455}
2456
2457ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2458 const void *object) {
2459 ClangExternalASTSourceCommon *external_source =
2460 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2461
2462 if (external_source && external_source->HasMetadata(object))
2463 return external_source->GetMetadata(object);
2464 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002465 return nullptr;
2466}
2467
Kate Stoneb9c1b512016-09-06 20:57:50 +00002468clang::DeclContext *
2469ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2470 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2471}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002472
Kate Stoneb9c1b512016-09-06 20:57:50 +00002473clang::DeclContext *
2474ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2475 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2476}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002477
Kate Stoneb9c1b512016-09-06 20:57:50 +00002478bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2479 int kind) const {
2480 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2481 if (clang_type) {
2482 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2483 if (tag_type) {
2484 clang::TagDecl *tag_decl =
2485 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2486 if (tag_decl) {
2487 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2488 return true;
2489 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002490 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002491 }
2492 return false;
2493}
2494
2495bool ClangASTContext::SetDefaultAccessForRecordFields(
2496 clang::RecordDecl *record_decl, int default_accessibility,
2497 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2498 if (record_decl) {
2499 uint32_t field_idx;
2500 clang::RecordDecl::field_iterator field, field_end;
2501 for (field = record_decl->field_begin(),
2502 field_end = record_decl->field_end(), field_idx = 0;
2503 field != field_end; ++field, ++field_idx) {
2504 // If no accessibility was assigned, assign the correct one
2505 if (field_idx < num_assigned_accessibilities &&
2506 assigned_accessibilities[field_idx] == clang::AS_none)
2507 field->setAccess((clang::AccessSpecifier)default_accessibility);
2508 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002509 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002510 }
2511 return false;
2512}
2513
2514clang::DeclContext *
2515ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2516 return GetDeclContextForType(ClangUtil::GetQualType(type));
2517}
2518
2519clang::DeclContext *
2520ClangASTContext::GetDeclContextForType(clang::QualType type) {
2521 if (type.isNull())
2522 return nullptr;
2523
2524 clang::QualType qual_type = type.getCanonicalType();
2525 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2526 switch (type_class) {
2527 case clang::Type::ObjCInterface:
2528 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2529 ->getInterface();
2530 case clang::Type::ObjCObjectPointer:
2531 return GetDeclContextForType(
2532 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2533 ->getPointeeType());
2534 case clang::Type::Record:
2535 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2536 case clang::Type::Enum:
2537 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2538 case clang::Type::Typedef:
2539 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2540 ->getDecl()
2541 ->getUnderlyingType());
2542 case clang::Type::Auto:
2543 return GetDeclContextForType(
2544 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2545 case clang::Type::Elaborated:
2546 return GetDeclContextForType(
2547 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2548 case clang::Type::Paren:
2549 return GetDeclContextForType(
2550 llvm::cast<clang::ParenType>(qual_type)->desugar());
2551 default:
2552 break;
2553 }
2554 // No DeclContext in this type...
2555 return nullptr;
2556}
2557
2558static bool GetCompleteQualType(clang::ASTContext *ast,
2559 clang::QualType qual_type,
2560 bool allow_completion = true) {
2561 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2562 switch (type_class) {
2563 case clang::Type::ConstantArray:
2564 case clang::Type::IncompleteArray:
2565 case clang::Type::VariableArray: {
2566 const clang::ArrayType *array_type =
2567 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2568
2569 if (array_type)
2570 return GetCompleteQualType(ast, array_type->getElementType(),
2571 allow_completion);
2572 } break;
2573 case clang::Type::Record: {
2574 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2575 if (cxx_record_decl) {
2576 if (cxx_record_decl->hasExternalLexicalStorage()) {
2577 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2578 const bool fields_loaded =
2579 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2580 if (is_complete && fields_loaded)
2581 return true;
2582
2583 if (!allow_completion)
2584 return false;
2585
2586 // Call the field_begin() accessor to for it to use the external source
2587 // to load the fields...
2588 clang::ExternalASTSource *external_ast_source =
2589 ast->getExternalSource();
2590 if (external_ast_source) {
2591 external_ast_source->CompleteType(cxx_record_decl);
2592 if (cxx_record_decl->isCompleteDefinition()) {
2593 cxx_record_decl->field_begin();
2594 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2595 }
2596 }
2597 }
2598 }
2599 const clang::TagType *tag_type =
2600 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2601 return !tag_type->isIncompleteType();
2602 } break;
2603
2604 case clang::Type::Enum: {
2605 const clang::TagType *tag_type =
2606 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2607 if (tag_type) {
2608 clang::TagDecl *tag_decl = tag_type->getDecl();
2609 if (tag_decl) {
2610 if (tag_decl->getDefinition())
2611 return true;
2612
2613 if (!allow_completion)
2614 return false;
2615
2616 if (tag_decl->hasExternalLexicalStorage()) {
2617 if (ast) {
2618 clang::ExternalASTSource *external_ast_source =
2619 ast->getExternalSource();
2620 if (external_ast_source) {
2621 external_ast_source->CompleteType(tag_decl);
2622 return !tag_type->isIncompleteType();
2623 }
2624 }
2625 }
2626 return false;
2627 }
2628 }
2629
2630 } break;
2631 case clang::Type::ObjCObject:
2632 case clang::Type::ObjCInterface: {
2633 const clang::ObjCObjectType *objc_class_type =
2634 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2635 if (objc_class_type) {
2636 clang::ObjCInterfaceDecl *class_interface_decl =
2637 objc_class_type->getInterface();
2638 // We currently can't complete objective C types through the newly added
Adrian Prantl05097242018-04-30 16:49:04 +00002639 // ASTContext because it only supports TagDecl objects right now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002640 if (class_interface_decl) {
2641 if (class_interface_decl->getDefinition())
2642 return true;
2643
2644 if (!allow_completion)
2645 return false;
2646
2647 if (class_interface_decl->hasExternalLexicalStorage()) {
2648 if (ast) {
2649 clang::ExternalASTSource *external_ast_source =
2650 ast->getExternalSource();
2651 if (external_ast_source) {
2652 external_ast_source->CompleteType(class_interface_decl);
2653 return !objc_class_type->isIncompleteType();
2654 }
2655 }
2656 }
2657 return false;
2658 }
2659 }
2660 } break;
2661
2662 case clang::Type::Typedef:
2663 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2664 ->getDecl()
2665 ->getUnderlyingType(),
2666 allow_completion);
2667
2668 case clang::Type::Auto:
2669 return GetCompleteQualType(
2670 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2671 allow_completion);
2672
2673 case clang::Type::Elaborated:
2674 return GetCompleteQualType(
2675 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2676 allow_completion);
2677
2678 case clang::Type::Paren:
2679 return GetCompleteQualType(
2680 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2681 allow_completion);
2682
2683 case clang::Type::Attributed:
2684 return GetCompleteQualType(
2685 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2686 allow_completion);
2687
2688 default:
2689 break;
2690 }
2691
2692 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002693}
2694
2695static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002696ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2697 switch (access) {
2698 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002699 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002700 case eAccessPublic:
2701 return clang::ObjCIvarDecl::Public;
2702 case eAccessPrivate:
2703 return clang::ObjCIvarDecl::Private;
2704 case eAccessProtected:
2705 return clang::ObjCIvarDecl::Protected;
2706 case eAccessPackage:
2707 return clang::ObjCIvarDecl::Package;
2708 }
2709 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002710}
2711
Greg Claytond8d4a572015-08-11 21:38:15 +00002712//----------------------------------------------------------------------
2713// Tests
2714//----------------------------------------------------------------------
2715
Kate Stoneb9c1b512016-09-06 20:57:50 +00002716bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2717 clang::QualType qual_type(GetCanonicalQualType(type));
2718
2719 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2720 switch (type_class) {
2721 case clang::Type::IncompleteArray:
2722 case clang::Type::VariableArray:
2723 case clang::Type::ConstantArray:
2724 case clang::Type::ExtVector:
2725 case clang::Type::Vector:
2726 case clang::Type::Record:
2727 case clang::Type::ObjCObject:
2728 case clang::Type::ObjCInterface:
2729 return true;
2730 case clang::Type::Auto:
2731 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2732 ->getDeducedType()
2733 .getAsOpaquePtr());
2734 case clang::Type::Elaborated:
2735 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2736 ->getNamedType()
2737 .getAsOpaquePtr());
2738 case clang::Type::Typedef:
2739 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2740 ->getDecl()
2741 ->getUnderlyingType()
2742 .getAsOpaquePtr());
2743 case clang::Type::Paren:
2744 return IsAggregateType(
2745 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2746 default:
2747 break;
2748 }
2749 // The clang type does have a value
2750 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002751}
2752
Kate Stoneb9c1b512016-09-06 20:57:50 +00002753bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2754 clang::QualType qual_type(GetCanonicalQualType(type));
2755
2756 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2757 switch (type_class) {
2758 case clang::Type::Record: {
2759 if (const clang::RecordType *record_type =
2760 llvm::dyn_cast_or_null<clang::RecordType>(
2761 qual_type.getTypePtrOrNull())) {
2762 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2763 return record_decl->isAnonymousStructOrUnion();
2764 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002765 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002766 break;
2767 }
2768 case clang::Type::Auto:
2769 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2770 ->getDeducedType()
2771 .getAsOpaquePtr());
2772 case clang::Type::Elaborated:
2773 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2774 ->getNamedType()
2775 .getAsOpaquePtr());
2776 case clang::Type::Typedef:
2777 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2778 ->getDecl()
2779 ->getUnderlyingType()
2780 .getAsOpaquePtr());
2781 case clang::Type::Paren:
2782 return IsAnonymousType(
2783 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2784 default:
2785 break;
2786 }
2787 // The clang type does have a value
2788 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002789}
2790
Kate Stoneb9c1b512016-09-06 20:57:50 +00002791bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2792 CompilerType *element_type_ptr,
2793 uint64_t *size, bool *is_incomplete) {
2794 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002795
Kate Stoneb9c1b512016-09-06 20:57:50 +00002796 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2797 switch (type_class) {
2798 default:
2799 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002800
Kate Stoneb9c1b512016-09-06 20:57:50 +00002801 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002802 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002803 element_type_ptr->SetCompilerType(
2804 getASTContext(),
2805 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002806 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002807 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2808 ->getSize()
2809 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002810 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002811 *is_incomplete = false;
2812 return true;
2813
2814 case clang::Type::IncompleteArray:
2815 if (element_type_ptr)
2816 element_type_ptr->SetCompilerType(
2817 getASTContext(),
2818 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2819 if (size)
2820 *size = 0;
2821 if (is_incomplete)
2822 *is_incomplete = true;
2823 return true;
2824
2825 case clang::Type::VariableArray:
2826 if (element_type_ptr)
2827 element_type_ptr->SetCompilerType(
2828 getASTContext(),
2829 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2830 if (size)
2831 *size = 0;
2832 if (is_incomplete)
2833 *is_incomplete = false;
2834 return true;
2835
2836 case clang::Type::DependentSizedArray:
2837 if (element_type_ptr)
2838 element_type_ptr->SetCompilerType(
2839 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2840 ->getElementType());
2841 if (size)
2842 *size = 0;
2843 if (is_incomplete)
2844 *is_incomplete = false;
2845 return true;
2846
2847 case clang::Type::Typedef:
2848 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2849 ->getDecl()
2850 ->getUnderlyingType()
2851 .getAsOpaquePtr(),
2852 element_type_ptr, size, is_incomplete);
2853 case clang::Type::Auto:
2854 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2855 ->getDeducedType()
2856 .getAsOpaquePtr(),
2857 element_type_ptr, size, is_incomplete);
2858 case clang::Type::Elaborated:
2859 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2860 ->getNamedType()
2861 .getAsOpaquePtr(),
2862 element_type_ptr, size, is_incomplete);
2863 case clang::Type::Paren:
2864 return IsArrayType(
2865 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2866 element_type_ptr, size, is_incomplete);
2867 }
2868 if (element_type_ptr)
2869 element_type_ptr->Clear();
2870 if (size)
2871 *size = 0;
2872 if (is_incomplete)
2873 *is_incomplete = false;
2874 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002875}
2876
Kate Stoneb9c1b512016-09-06 20:57:50 +00002877bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2878 CompilerType *element_type, uint64_t *size) {
2879 clang::QualType qual_type(GetCanonicalQualType(type));
2880
2881 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2882 switch (type_class) {
2883 case clang::Type::Vector: {
2884 const clang::VectorType *vector_type =
2885 qual_type->getAs<clang::VectorType>();
2886 if (vector_type) {
2887 if (size)
2888 *size = vector_type->getNumElements();
2889 if (element_type)
2890 *element_type =
2891 CompilerType(getASTContext(), vector_type->getElementType());
2892 }
2893 return true;
2894 } break;
2895 case clang::Type::ExtVector: {
2896 const clang::ExtVectorType *ext_vector_type =
2897 qual_type->getAs<clang::ExtVectorType>();
2898 if (ext_vector_type) {
2899 if (size)
2900 *size = ext_vector_type->getNumElements();
2901 if (element_type)
2902 *element_type =
2903 CompilerType(getASTContext(), ext_vector_type->getElementType());
2904 }
2905 return true;
2906 }
2907 default:
2908 break;
2909 }
2910 return false;
2911}
2912
2913bool ClangASTContext::IsRuntimeGeneratedType(
2914 lldb::opaque_compiler_type_t type) {
2915 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
2916 ->GetDeclContextForType(GetQualType(type));
2917 if (!decl_ctx)
2918 return false;
2919
2920 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2921 return false;
2922
2923 clang::ObjCInterfaceDecl *result_iface_decl =
2924 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2925
2926 ClangASTMetadata *ast_metadata =
2927 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2928 if (!ast_metadata)
2929 return false;
2930 return (ast_metadata->GetISAPtr() != 0);
2931}
2932
2933bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
2934 return GetQualType(type).getUnqualifiedType()->isCharType();
2935}
2936
2937bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
2938 const bool allow_completion = false;
2939 return GetCompleteQualType(getASTContext(), GetQualType(type),
2940 allow_completion);
2941}
2942
2943bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
2944 return GetQualType(type).isConstQualified();
2945}
2946
2947bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
2948 uint32_t &length) {
2949 CompilerType pointee_or_element_clang_type;
2950 length = 0;
2951 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2952
2953 if (!pointee_or_element_clang_type.IsValid())
2954 return false;
2955
2956 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2957 if (pointee_or_element_clang_type.IsCharType()) {
2958 if (type_flags.Test(eTypeIsArray)) {
Adrian Prantl05097242018-04-30 16:49:04 +00002959 // We know the size of the array and it could be a C string since it is
2960 // an array of characters
Kate Stoneb9c1b512016-09-06 20:57:50 +00002961 length = llvm::cast<clang::ConstantArrayType>(
2962 GetCanonicalQualType(type).getTypePtr())
2963 ->getSize()
2964 .getLimitedValue();
2965 }
2966 return true;
2967 }
2968 }
2969 return false;
2970}
2971
2972bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
2973 bool *is_variadic_ptr) {
2974 if (type) {
2975 clang::QualType qual_type(GetCanonicalQualType(type));
2976
2977 if (qual_type->isFunctionType()) {
2978 if (is_variadic_ptr) {
2979 const clang::FunctionProtoType *function_proto_type =
2980 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2981 if (function_proto_type)
2982 *is_variadic_ptr = function_proto_type->isVariadic();
2983 else
2984 *is_variadic_ptr = false;
2985 }
2986 return true;
2987 }
2988
Greg Claytond8d4a572015-08-11 21:38:15 +00002989 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002990 switch (type_class) {
2991 default:
2992 break;
2993 case clang::Type::Typedef:
2994 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
2995 ->getDecl()
2996 ->getUnderlyingType()
2997 .getAsOpaquePtr(),
2998 nullptr);
2999 case clang::Type::Auto:
3000 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3001 ->getDeducedType()
3002 .getAsOpaquePtr(),
3003 nullptr);
3004 case clang::Type::Elaborated:
3005 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3006 ->getNamedType()
3007 .getAsOpaquePtr(),
3008 nullptr);
3009 case clang::Type::Paren:
3010 return IsFunctionType(
3011 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3012 nullptr);
3013 case clang::Type::LValueReference:
3014 case clang::Type::RValueReference: {
3015 const clang::ReferenceType *reference_type =
3016 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3017 if (reference_type)
3018 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3019 nullptr);
3020 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00003021 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003022 }
3023 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003024}
3025
3026// Used to detect "Homogeneous Floating-point Aggregates"
3027uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003028ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3029 CompilerType *base_type_ptr) {
3030 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003031 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003032
3033 clang::QualType qual_type(GetCanonicalQualType(type));
3034 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3035 switch (type_class) {
3036 case clang::Type::Record:
3037 if (GetCompleteType(type)) {
3038 const clang::CXXRecordDecl *cxx_record_decl =
3039 qual_type->getAsCXXRecordDecl();
3040 if (cxx_record_decl) {
3041 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3042 return 0;
3043 }
3044 const clang::RecordType *record_type =
3045 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3046 if (record_type) {
3047 const clang::RecordDecl *record_decl = record_type->getDecl();
3048 if (record_decl) {
3049 // We are looking for a structure that contains only floating point
3050 // types
3051 clang::RecordDecl::field_iterator field_pos,
3052 field_end = record_decl->field_end();
3053 uint32_t num_fields = 0;
3054 bool is_hva = false;
3055 bool is_hfa = false;
3056 clang::QualType base_qual_type;
3057 uint64_t base_bitwidth = 0;
3058 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3059 ++field_pos) {
3060 clang::QualType field_qual_type = field_pos->getType();
3061 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3062 if (field_qual_type->isFloatingType()) {
3063 if (field_qual_type->isComplexType())
3064 return 0;
3065 else {
3066 if (num_fields == 0)
3067 base_qual_type = field_qual_type;
3068 else {
3069 if (is_hva)
3070 return 0;
3071 is_hfa = true;
3072 if (field_qual_type.getTypePtr() !=
3073 base_qual_type.getTypePtr())
3074 return 0;
3075 }
3076 }
3077 } else if (field_qual_type->isVectorType() ||
3078 field_qual_type->isExtVectorType()) {
3079 if (num_fields == 0) {
3080 base_qual_type = field_qual_type;
3081 base_bitwidth = field_bitwidth;
3082 } else {
3083 if (is_hfa)
3084 return 0;
3085 is_hva = true;
3086 if (base_bitwidth != field_bitwidth)
3087 return 0;
3088 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3089 return 0;
3090 }
3091 } else
3092 return 0;
3093 ++num_fields;
3094 }
3095 if (base_type_ptr)
3096 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3097 return num_fields;
3098 }
3099 }
3100 }
3101 break;
3102
3103 case clang::Type::Typedef:
3104 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3105 ->getDecl()
3106 ->getUnderlyingType()
3107 .getAsOpaquePtr(),
3108 base_type_ptr);
3109
3110 case clang::Type::Auto:
3111 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3112 ->getDeducedType()
3113 .getAsOpaquePtr(),
3114 base_type_ptr);
3115
3116 case clang::Type::Elaborated:
3117 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3118 ->getNamedType()
3119 .getAsOpaquePtr(),
3120 base_type_ptr);
3121 default:
3122 break;
3123 }
3124 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003125}
3126
Kate Stoneb9c1b512016-09-06 20:57:50 +00003127size_t ClangASTContext::GetNumberOfFunctionArguments(
3128 lldb::opaque_compiler_type_t type) {
3129 if (type) {
3130 clang::QualType qual_type(GetCanonicalQualType(type));
3131 const clang::FunctionProtoType *func =
3132 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3133 if (func)
3134 return func->getNumParams();
3135 }
3136 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003137}
3138
Greg Claytona1e5dc82015-08-11 22:53:00 +00003139CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003140ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3141 const size_t index) {
3142 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003143 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003144 const clang::FunctionProtoType *func =
3145 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3146 if (func) {
3147 if (index < func->getNumParams())
3148 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003149 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003150 }
3151 return CompilerType();
3152}
3153
3154bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3155 if (type) {
3156 clang::QualType qual_type(GetCanonicalQualType(type));
3157
3158 if (qual_type->isFunctionPointerType())
3159 return true;
3160
3161 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3162 switch (type_class) {
3163 default:
3164 break;
3165 case clang::Type::Typedef:
3166 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3167 ->getDecl()
3168 ->getUnderlyingType()
3169 .getAsOpaquePtr());
3170 case clang::Type::Auto:
3171 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3172 ->getDeducedType()
3173 .getAsOpaquePtr());
3174 case clang::Type::Elaborated:
3175 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3176 ->getNamedType()
3177 .getAsOpaquePtr());
3178 case clang::Type::Paren:
3179 return IsFunctionPointerType(
3180 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3181
3182 case clang::Type::LValueReference:
3183 case clang::Type::RValueReference: {
3184 const clang::ReferenceType *reference_type =
3185 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3186 if (reference_type)
3187 return IsFunctionPointerType(
3188 reference_type->getPointeeType().getAsOpaquePtr());
3189 } break;
3190 }
3191 }
3192 return false;
3193}
3194
3195bool ClangASTContext::IsBlockPointerType(
3196 lldb::opaque_compiler_type_t type,
3197 CompilerType *function_pointer_type_ptr) {
3198 if (type) {
3199 clang::QualType qual_type(GetCanonicalQualType(type));
3200
3201 if (qual_type->isBlockPointerType()) {
3202 if (function_pointer_type_ptr) {
3203 const clang::BlockPointerType *block_pointer_type =
3204 qual_type->getAs<clang::BlockPointerType>();
3205 QualType pointee_type = block_pointer_type->getPointeeType();
3206 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3207 *function_pointer_type_ptr =
3208 CompilerType(getASTContext(), function_pointer_type);
3209 }
3210 return true;
3211 }
3212
3213 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3214 switch (type_class) {
3215 default:
3216 break;
3217 case clang::Type::Typedef:
3218 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3219 ->getDecl()
3220 ->getUnderlyingType()
3221 .getAsOpaquePtr(),
3222 function_pointer_type_ptr);
3223 case clang::Type::Auto:
3224 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3225 ->getDeducedType()
3226 .getAsOpaquePtr(),
3227 function_pointer_type_ptr);
3228 case clang::Type::Elaborated:
3229 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3230 ->getNamedType()
3231 .getAsOpaquePtr(),
3232 function_pointer_type_ptr);
3233 case clang::Type::Paren:
3234 return IsBlockPointerType(
3235 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3236 function_pointer_type_ptr);
3237
3238 case clang::Type::LValueReference:
3239 case clang::Type::RValueReference: {
3240 const clang::ReferenceType *reference_type =
3241 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3242 if (reference_type)
3243 return IsBlockPointerType(
3244 reference_type->getPointeeType().getAsOpaquePtr(),
3245 function_pointer_type_ptr);
3246 } break;
3247 }
3248 }
3249 return false;
3250}
3251
3252bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3253 bool &is_signed) {
3254 if (!type)
3255 return false;
3256
3257 clang::QualType qual_type(GetCanonicalQualType(type));
3258 const clang::BuiltinType *builtin_type =
3259 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3260
3261 if (builtin_type) {
3262 if (builtin_type->isInteger()) {
3263 is_signed = builtin_type->isSignedInteger();
3264 return true;
3265 }
3266 }
3267
3268 return false;
3269}
3270
3271bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3272 bool &is_signed) {
3273 if (type) {
3274 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3275 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3276
3277 if (enum_type) {
3278 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3279 is_signed);
3280 return true;
3281 }
3282 }
3283
3284 return false;
3285}
3286
3287bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3288 CompilerType *pointee_type) {
3289 if (type) {
3290 clang::QualType qual_type(GetCanonicalQualType(type));
3291 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3292 switch (type_class) {
3293 case clang::Type::Builtin:
3294 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3295 default:
3296 break;
3297 case clang::BuiltinType::ObjCId:
3298 case clang::BuiltinType::ObjCClass:
3299 return true;
3300 }
3301 return false;
3302 case clang::Type::ObjCObjectPointer:
3303 if (pointee_type)
3304 pointee_type->SetCompilerType(
3305 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3306 ->getPointeeType());
3307 return true;
3308 case clang::Type::BlockPointer:
3309 if (pointee_type)
3310 pointee_type->SetCompilerType(
3311 getASTContext(),
3312 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3313 return true;
3314 case clang::Type::Pointer:
3315 if (pointee_type)
3316 pointee_type->SetCompilerType(
3317 getASTContext(),
3318 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3319 return true;
3320 case clang::Type::MemberPointer:
3321 if (pointee_type)
3322 pointee_type->SetCompilerType(
3323 getASTContext(),
3324 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3325 return true;
3326 case clang::Type::Typedef:
3327 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3328 ->getDecl()
3329 ->getUnderlyingType()
3330 .getAsOpaquePtr(),
3331 pointee_type);
3332 case clang::Type::Auto:
3333 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3334 ->getDeducedType()
3335 .getAsOpaquePtr(),
3336 pointee_type);
3337 case clang::Type::Elaborated:
3338 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3339 ->getNamedType()
3340 .getAsOpaquePtr(),
3341 pointee_type);
3342 case clang::Type::Paren:
3343 return IsPointerType(
3344 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3345 pointee_type);
3346 default:
3347 break;
3348 }
3349 }
3350 if (pointee_type)
3351 pointee_type->Clear();
3352 return false;
3353}
3354
3355bool ClangASTContext::IsPointerOrReferenceType(
3356 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3357 if (type) {
3358 clang::QualType qual_type(GetCanonicalQualType(type));
3359 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3360 switch (type_class) {
3361 case clang::Type::Builtin:
3362 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3363 default:
3364 break;
3365 case clang::BuiltinType::ObjCId:
3366 case clang::BuiltinType::ObjCClass:
3367 return true;
3368 }
3369 return false;
3370 case clang::Type::ObjCObjectPointer:
3371 if (pointee_type)
3372 pointee_type->SetCompilerType(
3373 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3374 ->getPointeeType());
3375 return true;
3376 case clang::Type::BlockPointer:
3377 if (pointee_type)
3378 pointee_type->SetCompilerType(
3379 getASTContext(),
3380 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3381 return true;
3382 case clang::Type::Pointer:
3383 if (pointee_type)
3384 pointee_type->SetCompilerType(
3385 getASTContext(),
3386 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3387 return true;
3388 case clang::Type::MemberPointer:
3389 if (pointee_type)
3390 pointee_type->SetCompilerType(
3391 getASTContext(),
3392 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3393 return true;
3394 case clang::Type::LValueReference:
3395 if (pointee_type)
3396 pointee_type->SetCompilerType(
3397 getASTContext(),
3398 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3399 return true;
3400 case clang::Type::RValueReference:
3401 if (pointee_type)
3402 pointee_type->SetCompilerType(
3403 getASTContext(),
3404 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3405 return true;
3406 case clang::Type::Typedef:
3407 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3408 ->getDecl()
3409 ->getUnderlyingType()
3410 .getAsOpaquePtr(),
3411 pointee_type);
3412 case clang::Type::Auto:
3413 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3414 ->getDeducedType()
3415 .getAsOpaquePtr(),
3416 pointee_type);
3417 case clang::Type::Elaborated:
3418 return IsPointerOrReferenceType(
3419 llvm::cast<clang::ElaboratedType>(qual_type)
3420 ->getNamedType()
3421 .getAsOpaquePtr(),
3422 pointee_type);
3423 case clang::Type::Paren:
3424 return IsPointerOrReferenceType(
3425 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3426 pointee_type);
3427 default:
3428 break;
3429 }
3430 }
3431 if (pointee_type)
3432 pointee_type->Clear();
3433 return false;
3434}
3435
3436bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3437 CompilerType *pointee_type,
3438 bool *is_rvalue) {
3439 if (type) {
3440 clang::QualType qual_type(GetCanonicalQualType(type));
3441 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3442
3443 switch (type_class) {
3444 case clang::Type::LValueReference:
3445 if (pointee_type)
3446 pointee_type->SetCompilerType(
3447 getASTContext(),
3448 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3449 if (is_rvalue)
3450 *is_rvalue = false;
3451 return true;
3452 case clang::Type::RValueReference:
3453 if (pointee_type)
3454 pointee_type->SetCompilerType(
3455 getASTContext(),
3456 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3457 if (is_rvalue)
3458 *is_rvalue = true;
3459 return true;
3460 case clang::Type::Typedef:
3461 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3462 ->getDecl()
3463 ->getUnderlyingType()
3464 .getAsOpaquePtr(),
3465 pointee_type, is_rvalue);
3466 case clang::Type::Auto:
3467 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3468 ->getDeducedType()
3469 .getAsOpaquePtr(),
3470 pointee_type, is_rvalue);
3471 case clang::Type::Elaborated:
3472 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3473 ->getNamedType()
3474 .getAsOpaquePtr(),
3475 pointee_type, is_rvalue);
3476 case clang::Type::Paren:
3477 return IsReferenceType(
3478 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3479 pointee_type, is_rvalue);
3480
3481 default:
3482 break;
3483 }
3484 }
3485 if (pointee_type)
3486 pointee_type->Clear();
3487 return false;
3488}
3489
3490bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3491 uint32_t &count, bool &is_complex) {
3492 if (type) {
3493 clang::QualType qual_type(GetCanonicalQualType(type));
3494
3495 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3496 qual_type->getCanonicalTypeInternal())) {
3497 clang::BuiltinType::Kind kind = BT->getKind();
3498 if (kind >= clang::BuiltinType::Float &&
3499 kind <= clang::BuiltinType::LongDouble) {
3500 count = 1;
3501 is_complex = false;
3502 return true;
3503 }
3504 } else if (const clang::ComplexType *CT =
3505 llvm::dyn_cast<clang::ComplexType>(
3506 qual_type->getCanonicalTypeInternal())) {
3507 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3508 is_complex)) {
3509 count = 2;
3510 is_complex = true;
3511 return true;
3512 }
3513 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3514 qual_type->getCanonicalTypeInternal())) {
3515 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3516 is_complex)) {
3517 count = VT->getNumElements();
3518 is_complex = false;
3519 return true;
3520 }
3521 }
3522 }
3523 count = 0;
3524 is_complex = false;
3525 return false;
3526}
3527
3528bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3529 if (!type)
3530 return false;
3531
3532 clang::QualType qual_type(GetQualType(type));
3533 const clang::TagType *tag_type =
3534 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3535 if (tag_type) {
3536 clang::TagDecl *tag_decl = tag_type->getDecl();
3537 if (tag_decl)
3538 return tag_decl->isCompleteDefinition();
3539 return false;
3540 } else {
3541 const clang::ObjCObjectType *objc_class_type =
3542 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3543 if (objc_class_type) {
3544 clang::ObjCInterfaceDecl *class_interface_decl =
3545 objc_class_type->getInterface();
3546 if (class_interface_decl)
3547 return class_interface_decl->getDefinition() != nullptr;
3548 return false;
3549 }
3550 }
3551 return true;
3552}
3553
3554bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3555 if (type) {
3556 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3557
3558 const clang::ObjCObjectPointerType *obj_pointer_type =
3559 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3560
3561 if (obj_pointer_type)
3562 return obj_pointer_type->isObjCClassType();
3563 }
3564 return false;
3565}
3566
3567bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3568 if (ClangUtil::IsClangType(type))
3569 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3570 return false;
3571}
3572
3573bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3574 if (!type)
3575 return false;
3576 clang::QualType qual_type(GetCanonicalQualType(type));
3577 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3578 return (type_class == clang::Type::Record);
3579}
3580
3581bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3582 if (!type)
3583 return false;
3584 clang::QualType qual_type(GetCanonicalQualType(type));
3585 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3586 return (type_class == clang::Type::Enum);
3587}
3588
3589bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3590 if (type) {
3591 clang::QualType qual_type(GetCanonicalQualType(type));
3592 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3593 switch (type_class) {
3594 case clang::Type::Record:
3595 if (GetCompleteType(type)) {
3596 const clang::RecordType *record_type =
3597 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3598 const clang::RecordDecl *record_decl = record_type->getDecl();
3599 if (record_decl) {
3600 const clang::CXXRecordDecl *cxx_record_decl =
3601 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3602 if (cxx_record_decl)
3603 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003604 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003605 }
3606 break;
3607
3608 default:
3609 break;
3610 }
3611 }
3612 return false;
3613}
3614
3615bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3616 CompilerType *dynamic_pointee_type,
3617 bool check_cplusplus,
3618 bool check_objc) {
3619 clang::QualType pointee_qual_type;
3620 if (type) {
3621 clang::QualType qual_type(GetCanonicalQualType(type));
3622 bool success = false;
3623 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3624 switch (type_class) {
3625 case clang::Type::Builtin:
3626 if (check_objc &&
3627 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3628 clang::BuiltinType::ObjCId) {
3629 if (dynamic_pointee_type)
3630 dynamic_pointee_type->SetCompilerType(this, type);
3631 return true;
3632 }
3633 break;
3634
3635 case clang::Type::ObjCObjectPointer:
3636 if (check_objc) {
3637 if (auto objc_pointee_type =
3638 qual_type->getPointeeType().getTypePtrOrNull()) {
3639 if (auto objc_object_type =
3640 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3641 objc_pointee_type)) {
3642 if (objc_object_type->isObjCClass())
3643 return false;
3644 }
3645 }
3646 if (dynamic_pointee_type)
3647 dynamic_pointee_type->SetCompilerType(
3648 getASTContext(),
3649 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3650 ->getPointeeType());
3651 return true;
3652 }
3653 break;
3654
3655 case clang::Type::Pointer:
3656 pointee_qual_type =
3657 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3658 success = true;
3659 break;
3660
3661 case clang::Type::LValueReference:
3662 case clang::Type::RValueReference:
3663 pointee_qual_type =
3664 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3665 success = true;
3666 break;
3667
3668 case clang::Type::Typedef:
3669 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3670 ->getDecl()
3671 ->getUnderlyingType()
3672 .getAsOpaquePtr(),
3673 dynamic_pointee_type, check_cplusplus,
3674 check_objc);
3675
3676 case clang::Type::Auto:
3677 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3678 ->getDeducedType()
3679 .getAsOpaquePtr(),
3680 dynamic_pointee_type, check_cplusplus,
3681 check_objc);
3682
3683 case clang::Type::Elaborated:
3684 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3685 ->getNamedType()
3686 .getAsOpaquePtr(),
3687 dynamic_pointee_type, check_cplusplus,
3688 check_objc);
3689
3690 case clang::Type::Paren:
3691 return IsPossibleDynamicType(
3692 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3693 dynamic_pointee_type, check_cplusplus, check_objc);
3694 default:
3695 break;
3696 }
3697
3698 if (success) {
3699 // Check to make sure what we are pointing too is a possible dynamic C++
Adrian Prantl05097242018-04-30 16:49:04 +00003700 // type We currently accept any "void *" (in case we have a class that
3701 // has been watered down to an opaque pointer) and virtual C++ classes.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003702 const clang::Type::TypeClass pointee_type_class =
3703 pointee_qual_type.getCanonicalType()->getTypeClass();
3704 switch (pointee_type_class) {
3705 case clang::Type::Builtin:
3706 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3707 case clang::BuiltinType::UnknownAny:
3708 case clang::BuiltinType::Void:
3709 if (dynamic_pointee_type)
3710 dynamic_pointee_type->SetCompilerType(getASTContext(),
3711 pointee_qual_type);
3712 return true;
3713 default:
3714 break;
3715 }
3716 break;
3717
3718 case clang::Type::Record:
3719 if (check_cplusplus) {
3720 clang::CXXRecordDecl *cxx_record_decl =
3721 pointee_qual_type->getAsCXXRecordDecl();
3722 if (cxx_record_decl) {
3723 bool is_complete = cxx_record_decl->isCompleteDefinition();
3724
3725 if (is_complete)
3726 success = cxx_record_decl->isDynamicClass();
3727 else {
3728 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3729 getASTContext(), cxx_record_decl);
3730 if (metadata)
3731 success = metadata->GetIsDynamicCXXType();
3732 else {
3733 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3734 .GetCompleteType();
3735 if (is_complete)
3736 success = cxx_record_decl->isDynamicClass();
3737 else
3738 success = false;
3739 }
3740 }
3741
3742 if (success) {
3743 if (dynamic_pointee_type)
3744 dynamic_pointee_type->SetCompilerType(getASTContext(),
3745 pointee_qual_type);
3746 return true;
3747 }
3748 }
3749 }
3750 break;
3751
3752 case clang::Type::ObjCObject:
3753 case clang::Type::ObjCInterface:
3754 if (check_objc) {
3755 if (dynamic_pointee_type)
3756 dynamic_pointee_type->SetCompilerType(getASTContext(),
3757 pointee_qual_type);
3758 return true;
3759 }
3760 break;
3761
3762 default:
3763 break;
3764 }
3765 }
3766 }
3767 if (dynamic_pointee_type)
3768 dynamic_pointee_type->Clear();
3769 return false;
3770}
3771
3772bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3773 if (!type)
3774 return false;
3775
3776 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3777}
3778
3779bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3780 if (!type)
3781 return false;
3782 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3783}
3784
3785bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3786 if (!type)
3787 return false;
3788 return GetCanonicalQualType(type)->isVoidType();
3789}
3790
3791bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3792 return ClangASTContextSupportsLanguage(language);
3793}
3794
3795bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3796 std::string &class_name) {
3797 if (type) {
3798 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3799 if (!qual_type.isNull()) {
3800 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3801 if (cxx_record_decl) {
3802 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3803 return true;
3804 }
3805 }
3806 }
3807 class_name.clear();
3808 return false;
3809}
3810
3811bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3812 if (!type)
3813 return false;
3814
3815 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3816 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
3817 return true;
3818 return false;
3819}
3820
3821bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3822 if (!type)
3823 return false;
3824 clang::QualType qual_type(GetCanonicalQualType(type));
3825 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3826 if (tag_type)
3827 return tag_type->isBeingDefined();
3828 return false;
3829}
3830
3831bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3832 CompilerType *class_type_ptr) {
3833 if (!type)
3834 return false;
3835
3836 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3837
3838 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3839 if (class_type_ptr) {
3840 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3841 const clang::ObjCObjectPointerType *obj_pointer_type =
3842 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3843 if (obj_pointer_type == nullptr)
3844 class_type_ptr->Clear();
3845 else
3846 class_type_ptr->SetCompilerType(
3847 type.GetTypeSystem(),
3848 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3849 .getAsOpaquePtr());
3850 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003851 }
3852 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003853 }
3854 if (class_type_ptr)
3855 class_type_ptr->Clear();
3856 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003857}
3858
Kate Stoneb9c1b512016-09-06 20:57:50 +00003859bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3860 std::string &class_name) {
3861 if (!type)
3862 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003863
Kate Stoneb9c1b512016-09-06 20:57:50 +00003864 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3865
3866 const clang::ObjCObjectType *object_type =
3867 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3868 if (object_type) {
3869 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3870 if (interface) {
3871 class_name = interface->getNameAsString();
3872 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00003873 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003874 }
3875 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003876}
3877
Greg Claytond8d4a572015-08-11 21:38:15 +00003878//----------------------------------------------------------------------
3879// Type Completion
3880//----------------------------------------------------------------------
3881
Kate Stoneb9c1b512016-09-06 20:57:50 +00003882bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3883 if (!type)
3884 return false;
3885 const bool allow_completion = true;
3886 return GetCompleteQualType(getASTContext(), GetQualType(type),
3887 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00003888}
3889
Kate Stoneb9c1b512016-09-06 20:57:50 +00003890ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
3891 std::string type_name;
3892 if (type) {
3893 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
3894 clang::QualType qual_type(GetQualType(type));
3895 printing_policy.SuppressTagKeyword = true;
3896 const clang::TypedefType *typedef_type =
3897 qual_type->getAs<clang::TypedefType>();
3898 if (typedef_type) {
3899 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3900 type_name = typedef_decl->getQualifiedNameAsString();
3901 } else {
3902 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003903 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003904 }
3905 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00003906}
3907
3908uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003909ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
3910 CompilerType *pointee_or_element_clang_type) {
3911 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003912 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003913
3914 if (pointee_or_element_clang_type)
3915 pointee_or_element_clang_type->Clear();
3916
3917 clang::QualType qual_type(GetQualType(type));
3918
3919 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3920 switch (type_class) {
Sean Callananddf802a2017-06-02 01:24:18 +00003921 case clang::Type::Attributed:
3922 return GetTypeInfo(
3923 qual_type->getAs<clang::AttributedType>()
3924 ->getModifiedType().getAsOpaquePtr(),
3925 pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003926 case clang::Type::Builtin: {
3927 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3928 qual_type->getCanonicalTypeInternal());
3929
3930 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3931 switch (builtin_type->getKind()) {
3932 case clang::BuiltinType::ObjCId:
3933 case clang::BuiltinType::ObjCClass:
3934 if (pointee_or_element_clang_type)
3935 pointee_or_element_clang_type->SetCompilerType(
3936 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
3937 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3938 break;
3939
3940 case clang::BuiltinType::ObjCSel:
3941 if (pointee_or_element_clang_type)
3942 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
3943 getASTContext()->CharTy);
3944 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3945 break;
3946
3947 case clang::BuiltinType::Bool:
3948 case clang::BuiltinType::Char_U:
3949 case clang::BuiltinType::UChar:
3950 case clang::BuiltinType::WChar_U:
3951 case clang::BuiltinType::Char16:
3952 case clang::BuiltinType::Char32:
3953 case clang::BuiltinType::UShort:
3954 case clang::BuiltinType::UInt:
3955 case clang::BuiltinType::ULong:
3956 case clang::BuiltinType::ULongLong:
3957 case clang::BuiltinType::UInt128:
3958 case clang::BuiltinType::Char_S:
3959 case clang::BuiltinType::SChar:
3960 case clang::BuiltinType::WChar_S:
3961 case clang::BuiltinType::Short:
3962 case clang::BuiltinType::Int:
3963 case clang::BuiltinType::Long:
3964 case clang::BuiltinType::LongLong:
3965 case clang::BuiltinType::Int128:
3966 case clang::BuiltinType::Float:
3967 case clang::BuiltinType::Double:
3968 case clang::BuiltinType::LongDouble:
3969 builtin_type_flags |= eTypeIsScalar;
3970 if (builtin_type->isInteger()) {
3971 builtin_type_flags |= eTypeIsInteger;
3972 if (builtin_type->isSignedInteger())
3973 builtin_type_flags |= eTypeIsSigned;
3974 } else if (builtin_type->isFloatingPoint())
3975 builtin_type_flags |= eTypeIsFloat;
3976 break;
3977 default:
3978 break;
3979 }
3980 return builtin_type_flags;
3981 }
3982
3983 case clang::Type::BlockPointer:
3984 if (pointee_or_element_clang_type)
3985 pointee_or_element_clang_type->SetCompilerType(
3986 getASTContext(), qual_type->getPointeeType());
3987 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3988
3989 case clang::Type::Complex: {
3990 uint32_t complex_type_flags =
3991 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3992 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
3993 qual_type->getCanonicalTypeInternal());
3994 if (complex_type) {
3995 clang::QualType complex_element_type(complex_type->getElementType());
3996 if (complex_element_type->isIntegerType())
3997 complex_type_flags |= eTypeIsFloat;
3998 else if (complex_element_type->isFloatingType())
3999 complex_type_flags |= eTypeIsInteger;
4000 }
4001 return complex_type_flags;
4002 } break;
4003
4004 case clang::Type::ConstantArray:
4005 case clang::Type::DependentSizedArray:
4006 case clang::Type::IncompleteArray:
4007 case clang::Type::VariableArray:
4008 if (pointee_or_element_clang_type)
4009 pointee_or_element_clang_type->SetCompilerType(
4010 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4011 ->getElementType());
4012 return eTypeHasChildren | eTypeIsArray;
4013
4014 case clang::Type::DependentName:
4015 return 0;
4016 case clang::Type::DependentSizedExtVector:
4017 return eTypeHasChildren | eTypeIsVector;
4018 case clang::Type::DependentTemplateSpecialization:
4019 return eTypeIsTemplate;
4020 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004021 return CompilerType(
4022 getASTContext(),
4023 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
4024 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004025
4026 case clang::Type::Enum:
4027 if (pointee_or_element_clang_type)
4028 pointee_or_element_clang_type->SetCompilerType(
4029 getASTContext(),
4030 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4031 return eTypeIsEnumeration | eTypeHasValue;
4032
4033 case clang::Type::Auto:
4034 return CompilerType(
4035 getASTContext(),
4036 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4037 .GetTypeInfo(pointee_or_element_clang_type);
4038 case clang::Type::Elaborated:
4039 return CompilerType(
4040 getASTContext(),
4041 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4042 .GetTypeInfo(pointee_or_element_clang_type);
4043 case clang::Type::Paren:
4044 return CompilerType(getASTContext(),
4045 llvm::cast<clang::ParenType>(qual_type)->desugar())
4046 .GetTypeInfo(pointee_or_element_clang_type);
4047
4048 case clang::Type::FunctionProto:
4049 return eTypeIsFuncPrototype | eTypeHasValue;
4050 case clang::Type::FunctionNoProto:
4051 return eTypeIsFuncPrototype | eTypeHasValue;
4052 case clang::Type::InjectedClassName:
4053 return 0;
4054
4055 case clang::Type::LValueReference:
4056 case clang::Type::RValueReference:
4057 if (pointee_or_element_clang_type)
4058 pointee_or_element_clang_type->SetCompilerType(
4059 getASTContext(),
4060 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4061 ->getPointeeType());
4062 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4063
4064 case clang::Type::MemberPointer:
4065 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4066
4067 case clang::Type::ObjCObjectPointer:
4068 if (pointee_or_element_clang_type)
4069 pointee_or_element_clang_type->SetCompilerType(
4070 getASTContext(), qual_type->getPointeeType());
4071 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4072 eTypeHasValue;
4073
4074 case clang::Type::ObjCObject:
4075 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4076 case clang::Type::ObjCInterface:
4077 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4078
4079 case clang::Type::Pointer:
4080 if (pointee_or_element_clang_type)
4081 pointee_or_element_clang_type->SetCompilerType(
4082 getASTContext(), qual_type->getPointeeType());
4083 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4084
4085 case clang::Type::Record:
4086 if (qual_type->getAsCXXRecordDecl())
4087 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4088 else
4089 return eTypeHasChildren | eTypeIsStructUnion;
4090 break;
4091 case clang::Type::SubstTemplateTypeParm:
4092 return eTypeIsTemplate;
4093 case clang::Type::TemplateTypeParm:
4094 return eTypeIsTemplate;
4095 case clang::Type::TemplateSpecialization:
4096 return eTypeIsTemplate;
4097
4098 case clang::Type::Typedef:
4099 return eTypeIsTypedef |
4100 CompilerType(getASTContext(),
4101 llvm::cast<clang::TypedefType>(qual_type)
4102 ->getDecl()
4103 ->getUnderlyingType())
4104 .GetTypeInfo(pointee_or_element_clang_type);
4105 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004106 return CompilerType(getASTContext(),
4107 llvm::cast<clang::TypeOfExprType>(qual_type)
4108 ->getUnderlyingExpr()
4109 ->getType())
4110 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004111 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004112 return CompilerType(
4113 getASTContext(),
4114 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4115 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004116 case clang::Type::UnresolvedUsing:
4117 return 0;
4118
4119 case clang::Type::ExtVector:
4120 case clang::Type::Vector: {
4121 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4122 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4123 qual_type->getCanonicalTypeInternal());
4124 if (vector_type) {
4125 if (vector_type->isIntegerType())
4126 vector_type_flags |= eTypeIsFloat;
4127 else if (vector_type->isFloatingType())
4128 vector_type_flags |= eTypeIsInteger;
4129 }
4130 return vector_type_flags;
4131 }
4132 default:
4133 return 0;
4134 }
4135 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004136}
4137
Greg Claytond8d4a572015-08-11 21:38:15 +00004138lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004139ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4140 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004141 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004142
4143 // If the type is a reference, then resolve it to what it refers to first:
4144 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4145 if (qual_type->isAnyPointerType()) {
4146 if (qual_type->isObjCObjectPointerType())
4147 return lldb::eLanguageTypeObjC;
4148
4149 clang::QualType pointee_type(qual_type->getPointeeType());
4150 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4151 return lldb::eLanguageTypeC_plus_plus;
4152 if (pointee_type->isObjCObjectOrInterfaceType())
4153 return lldb::eLanguageTypeObjC;
4154 if (pointee_type->isObjCClassType())
4155 return lldb::eLanguageTypeObjC;
4156 if (pointee_type.getTypePtr() ==
4157 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4158 return lldb::eLanguageTypeObjC;
4159 } else {
4160 if (qual_type->isObjCObjectOrInterfaceType())
4161 return lldb::eLanguageTypeObjC;
4162 if (qual_type->getAsCXXRecordDecl())
4163 return lldb::eLanguageTypeC_plus_plus;
4164 switch (qual_type->getTypeClass()) {
4165 default:
4166 break;
4167 case clang::Type::Builtin:
4168 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4169 default:
4170 case clang::BuiltinType::Void:
4171 case clang::BuiltinType::Bool:
4172 case clang::BuiltinType::Char_U:
4173 case clang::BuiltinType::UChar:
4174 case clang::BuiltinType::WChar_U:
4175 case clang::BuiltinType::Char16:
4176 case clang::BuiltinType::Char32:
4177 case clang::BuiltinType::UShort:
4178 case clang::BuiltinType::UInt:
4179 case clang::BuiltinType::ULong:
4180 case clang::BuiltinType::ULongLong:
4181 case clang::BuiltinType::UInt128:
4182 case clang::BuiltinType::Char_S:
4183 case clang::BuiltinType::SChar:
4184 case clang::BuiltinType::WChar_S:
4185 case clang::BuiltinType::Short:
4186 case clang::BuiltinType::Int:
4187 case clang::BuiltinType::Long:
4188 case clang::BuiltinType::LongLong:
4189 case clang::BuiltinType::Int128:
4190 case clang::BuiltinType::Float:
4191 case clang::BuiltinType::Double:
4192 case clang::BuiltinType::LongDouble:
4193 break;
4194
4195 case clang::BuiltinType::NullPtr:
4196 return eLanguageTypeC_plus_plus;
4197
4198 case clang::BuiltinType::ObjCId:
4199 case clang::BuiltinType::ObjCClass:
4200 case clang::BuiltinType::ObjCSel:
4201 return eLanguageTypeObjC;
4202
4203 case clang::BuiltinType::Dependent:
4204 case clang::BuiltinType::Overload:
4205 case clang::BuiltinType::BoundMember:
4206 case clang::BuiltinType::UnknownAny:
4207 break;
4208 }
4209 break;
4210 case clang::Type::Typedef:
4211 return CompilerType(getASTContext(),
4212 llvm::cast<clang::TypedefType>(qual_type)
4213 ->getDecl()
4214 ->getUnderlyingType())
4215 .GetMinimumLanguage();
4216 }
4217 }
4218 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004219}
4220
4221lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004222ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4223 if (!type)
4224 return lldb::eTypeClassInvalid;
4225
4226 clang::QualType qual_type(GetQualType(type));
4227
4228 switch (qual_type->getTypeClass()) {
4229 case clang::Type::UnaryTransform:
4230 break;
4231 case clang::Type::FunctionNoProto:
4232 return lldb::eTypeClassFunction;
4233 case clang::Type::FunctionProto:
4234 return lldb::eTypeClassFunction;
4235 case clang::Type::IncompleteArray:
4236 return lldb::eTypeClassArray;
4237 case clang::Type::VariableArray:
4238 return lldb::eTypeClassArray;
4239 case clang::Type::ConstantArray:
4240 return lldb::eTypeClassArray;
4241 case clang::Type::DependentSizedArray:
4242 return lldb::eTypeClassArray;
4243 case clang::Type::DependentSizedExtVector:
4244 return lldb::eTypeClassVector;
Fangrui Song8f284882018-07-13 22:40:40 +00004245 case clang::Type::DependentVector:
4246 return lldb::eTypeClassVector;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004247 case clang::Type::ExtVector:
4248 return lldb::eTypeClassVector;
4249 case clang::Type::Vector:
4250 return lldb::eTypeClassVector;
4251 case clang::Type::Builtin:
4252 return lldb::eTypeClassBuiltin;
4253 case clang::Type::ObjCObjectPointer:
4254 return lldb::eTypeClassObjCObjectPointer;
4255 case clang::Type::BlockPointer:
4256 return lldb::eTypeClassBlockPointer;
4257 case clang::Type::Pointer:
4258 return lldb::eTypeClassPointer;
4259 case clang::Type::LValueReference:
4260 return lldb::eTypeClassReference;
4261 case clang::Type::RValueReference:
4262 return lldb::eTypeClassReference;
4263 case clang::Type::MemberPointer:
4264 return lldb::eTypeClassMemberPointer;
4265 case clang::Type::Complex:
4266 if (qual_type->isComplexType())
4267 return lldb::eTypeClassComplexFloat;
4268 else
4269 return lldb::eTypeClassComplexInteger;
4270 case clang::Type::ObjCObject:
4271 return lldb::eTypeClassObjCObject;
4272 case clang::Type::ObjCInterface:
4273 return lldb::eTypeClassObjCInterface;
4274 case clang::Type::Record: {
4275 const clang::RecordType *record_type =
4276 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4277 const clang::RecordDecl *record_decl = record_type->getDecl();
4278 if (record_decl->isUnion())
4279 return lldb::eTypeClassUnion;
4280 else if (record_decl->isStruct())
4281 return lldb::eTypeClassStruct;
4282 else
4283 return lldb::eTypeClassClass;
4284 } break;
4285 case clang::Type::Enum:
4286 return lldb::eTypeClassEnumeration;
4287 case clang::Type::Typedef:
4288 return lldb::eTypeClassTypedef;
4289 case clang::Type::UnresolvedUsing:
4290 break;
4291 case clang::Type::Paren:
4292 return CompilerType(getASTContext(),
4293 llvm::cast<clang::ParenType>(qual_type)->desugar())
4294 .GetTypeClass();
4295 case clang::Type::Auto:
4296 return CompilerType(
4297 getASTContext(),
4298 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4299 .GetTypeClass();
4300 case clang::Type::Elaborated:
4301 return CompilerType(
4302 getASTContext(),
4303 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4304 .GetTypeClass();
4305
4306 case clang::Type::Attributed:
4307 break;
4308 case clang::Type::TemplateTypeParm:
4309 break;
4310 case clang::Type::SubstTemplateTypeParm:
4311 break;
4312 case clang::Type::SubstTemplateTypeParmPack:
4313 break;
4314 case clang::Type::InjectedClassName:
4315 break;
4316 case clang::Type::DependentName:
4317 break;
4318 case clang::Type::DependentTemplateSpecialization:
4319 break;
4320 case clang::Type::PackExpansion:
4321 break;
4322
4323 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004324 return CompilerType(getASTContext(),
4325 llvm::cast<clang::TypeOfExprType>(qual_type)
4326 ->getUnderlyingExpr()
4327 ->getType())
4328 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004329 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004330 return CompilerType(
4331 getASTContext(),
4332 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4333 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004334 case clang::Type::Decltype:
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::TemplateSpecialization:
4340 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004341 case clang::Type::DeducedTemplateSpecialization:
4342 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004343 case clang::Type::Atomic:
4344 break;
4345 case clang::Type::Pipe:
4346 break;
4347
4348 // pointer type decayed from an array or function type.
4349 case clang::Type::Decayed:
4350 break;
4351 case clang::Type::Adjusted:
4352 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004353 case clang::Type::ObjCTypeParam:
4354 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00004355
4356 case clang::Type::DependentAddressSpace:
4357 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004358 }
4359 // We don't know hot to display this type...
4360 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004361}
4362
Kate Stoneb9c1b512016-09-06 20:57:50 +00004363unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4364 if (type)
4365 return GetQualType(type).getQualifiers().getCVRQualifiers();
4366 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004367}
4368
4369//----------------------------------------------------------------------
4370// Creating related types
4371//----------------------------------------------------------------------
4372
Greg Claytona1e5dc82015-08-11 22:53:00 +00004373CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004374ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4375 uint64_t *stride) {
4376 if (type) {
4377 clang::QualType qual_type(GetCanonicalQualType(type));
4378
4379 const clang::Type *array_eletype =
4380 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4381
4382 if (!array_eletype)
4383 return CompilerType();
4384
4385 CompilerType element_type(getASTContext(),
4386 array_eletype->getCanonicalTypeUnqualified());
4387
4388 // TODO: the real stride will be >= this value.. find the real one!
4389 if (stride)
4390 *stride = element_type.GetByteSize(nullptr);
4391
4392 return element_type;
4393 }
4394 return CompilerType();
4395}
4396
4397CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4398 uint64_t size) {
4399 if (type) {
4400 clang::QualType qual_type(GetCanonicalQualType(type));
4401 if (clang::ASTContext *ast_ctx = getASTContext()) {
4402 if (size != 0)
4403 return CompilerType(
4404 ast_ctx, ast_ctx->getConstantArrayType(
4405 qual_type, llvm::APInt(64, size),
4406 clang::ArrayType::ArraySizeModifier::Normal, 0));
4407 else
4408 return CompilerType(
4409 ast_ctx,
4410 ast_ctx->getIncompleteArrayType(
4411 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
Greg Claytond8d4a572015-08-11 21:38:15 +00004412 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004413 }
4414
4415 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004416}
4417
Greg Claytona1e5dc82015-08-11 22:53:00 +00004418CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004419ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4420 if (type)
4421 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4422 return CompilerType();
4423}
4424
4425static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4426 clang::QualType qual_type) {
4427 if (qual_type->isPointerType())
4428 qual_type = ast->getPointerType(
4429 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4430 else
4431 qual_type = qual_type.getUnqualifiedType();
4432 qual_type.removeLocalConst();
4433 qual_type.removeLocalRestrict();
4434 qual_type.removeLocalVolatile();
4435 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004436}
4437
4438CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004439ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4440 if (type)
4441 return CompilerType(
4442 getASTContext(),
4443 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4444 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004445}
4446
Kate Stoneb9c1b512016-09-06 20:57:50 +00004447int ClangASTContext::GetFunctionArgumentCount(
4448 lldb::opaque_compiler_type_t type) {
4449 if (type) {
4450 const clang::FunctionProtoType *func =
4451 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4452 if (func)
4453 return func->getNumParams();
4454 }
4455 return -1;
4456}
4457
4458CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4459 lldb::opaque_compiler_type_t type, size_t idx) {
4460 if (type) {
4461 const clang::FunctionProtoType *func =
4462 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4463 if (func) {
4464 const uint32_t num_args = func->getNumParams();
4465 if (idx < num_args)
4466 return CompilerType(getASTContext(), func->getParamType(idx));
4467 }
4468 }
4469 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004470}
4471
Greg Claytona1e5dc82015-08-11 22:53:00 +00004472CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004473ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4474 if (type) {
4475 clang::QualType qual_type(GetQualType(type));
4476 const clang::FunctionProtoType *func =
4477 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4478 if (func)
4479 return CompilerType(getASTContext(), func->getReturnType());
4480 }
4481 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004482}
4483
4484size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004485ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4486 size_t num_functions = 0;
4487 if (type) {
4488 clang::QualType qual_type(GetCanonicalQualType(type));
4489 switch (qual_type->getTypeClass()) {
4490 case clang::Type::Record:
4491 if (GetCompleteQualType(getASTContext(), qual_type)) {
4492 const clang::RecordType *record_type =
4493 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4494 const clang::RecordDecl *record_decl = record_type->getDecl();
4495 assert(record_decl);
4496 const clang::CXXRecordDecl *cxx_record_decl =
4497 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4498 if (cxx_record_decl)
4499 num_functions = std::distance(cxx_record_decl->method_begin(),
4500 cxx_record_decl->method_end());
4501 }
4502 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004503
Sean Callananf9c622a2016-09-30 18:44:43 +00004504 case clang::Type::ObjCObjectPointer: {
4505 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004506 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004507 const clang::ObjCInterfaceType *objc_interface_type =
4508 objc_class_type->getInterfaceType();
4509 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004510 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4511 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004512 clang::ObjCInterfaceDecl *class_interface_decl =
4513 objc_interface_type->getDecl();
4514 if (class_interface_decl) {
4515 num_functions = std::distance(class_interface_decl->meth_begin(),
4516 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004517 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004518 }
4519 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004520 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004521
4522 case clang::Type::ObjCObject:
4523 case clang::Type::ObjCInterface:
4524 if (GetCompleteType(type)) {
4525 const clang::ObjCObjectType *objc_class_type =
4526 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4527 if (objc_class_type) {
4528 clang::ObjCInterfaceDecl *class_interface_decl =
4529 objc_class_type->getInterface();
4530 if (class_interface_decl)
4531 num_functions = std::distance(class_interface_decl->meth_begin(),
4532 class_interface_decl->meth_end());
4533 }
4534 }
4535 break;
4536
4537 case clang::Type::Typedef:
4538 return CompilerType(getASTContext(),
4539 llvm::cast<clang::TypedefType>(qual_type)
4540 ->getDecl()
4541 ->getUnderlyingType())
4542 .GetNumMemberFunctions();
4543
4544 case clang::Type::Auto:
4545 return CompilerType(
4546 getASTContext(),
4547 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4548 .GetNumMemberFunctions();
4549
4550 case clang::Type::Elaborated:
4551 return CompilerType(
4552 getASTContext(),
4553 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4554 .GetNumMemberFunctions();
4555
4556 case clang::Type::Paren:
4557 return CompilerType(getASTContext(),
4558 llvm::cast<clang::ParenType>(qual_type)->desugar())
4559 .GetNumMemberFunctions();
4560
4561 default:
4562 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004563 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004564 }
4565 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004566}
4567
4568TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004569ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4570 size_t idx) {
4571 std::string name;
4572 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4573 CompilerType clang_type;
4574 CompilerDecl clang_decl;
4575 if (type) {
4576 clang::QualType qual_type(GetCanonicalQualType(type));
4577 switch (qual_type->getTypeClass()) {
4578 case clang::Type::Record:
4579 if (GetCompleteQualType(getASTContext(), qual_type)) {
4580 const clang::RecordType *record_type =
4581 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4582 const clang::RecordDecl *record_decl = record_type->getDecl();
4583 assert(record_decl);
4584 const clang::CXXRecordDecl *cxx_record_decl =
4585 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4586 if (cxx_record_decl) {
4587 auto method_iter = cxx_record_decl->method_begin();
4588 auto method_end = cxx_record_decl->method_end();
4589 if (idx <
4590 static_cast<size_t>(std::distance(method_iter, method_end))) {
4591 std::advance(method_iter, idx);
4592 clang::CXXMethodDecl *cxx_method_decl =
4593 method_iter->getCanonicalDecl();
4594 if (cxx_method_decl) {
4595 name = cxx_method_decl->getDeclName().getAsString();
4596 if (cxx_method_decl->isStatic())
4597 kind = lldb::eMemberFunctionKindStaticMethod;
4598 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4599 kind = lldb::eMemberFunctionKindConstructor;
4600 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4601 kind = lldb::eMemberFunctionKindDestructor;
4602 else
4603 kind = lldb::eMemberFunctionKindInstanceMethod;
4604 clang_type = CompilerType(
4605 this, cxx_method_decl->getType().getAsOpaquePtr());
4606 clang_decl = CompilerDecl(this, cxx_method_decl);
4607 }
4608 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004609 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004610 }
4611 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004612
Sean Callananf9c622a2016-09-30 18:44:43 +00004613 case clang::Type::ObjCObjectPointer: {
4614 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004615 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004616 const clang::ObjCInterfaceType *objc_interface_type =
4617 objc_class_type->getInterfaceType();
4618 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004619 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4620 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004621 clang::ObjCInterfaceDecl *class_interface_decl =
4622 objc_interface_type->getDecl();
4623 if (class_interface_decl) {
4624 auto method_iter = class_interface_decl->meth_begin();
4625 auto method_end = class_interface_decl->meth_end();
4626 if (idx <
4627 static_cast<size_t>(std::distance(method_iter, method_end))) {
4628 std::advance(method_iter, idx);
4629 clang::ObjCMethodDecl *objc_method_decl =
4630 method_iter->getCanonicalDecl();
4631 if (objc_method_decl) {
4632 clang_decl = CompilerDecl(this, objc_method_decl);
4633 name = objc_method_decl->getSelector().getAsString();
4634 if (objc_method_decl->isClassMethod())
4635 kind = lldb::eMemberFunctionKindStaticMethod;
4636 else
4637 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004638 }
4639 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004640 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004641 }
4642 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004643 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004644
Kate Stoneb9c1b512016-09-06 20:57:50 +00004645 case clang::Type::ObjCObject:
4646 case clang::Type::ObjCInterface:
4647 if (GetCompleteType(type)) {
4648 const clang::ObjCObjectType *objc_class_type =
4649 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4650 if (objc_class_type) {
4651 clang::ObjCInterfaceDecl *class_interface_decl =
4652 objc_class_type->getInterface();
4653 if (class_interface_decl) {
4654 auto method_iter = class_interface_decl->meth_begin();
4655 auto method_end = class_interface_decl->meth_end();
4656 if (idx <
4657 static_cast<size_t>(std::distance(method_iter, method_end))) {
4658 std::advance(method_iter, idx);
4659 clang::ObjCMethodDecl *objc_method_decl =
4660 method_iter->getCanonicalDecl();
4661 if (objc_method_decl) {
4662 clang_decl = CompilerDecl(this, objc_method_decl);
4663 name = objc_method_decl->getSelector().getAsString();
4664 if (objc_method_decl->isClassMethod())
4665 kind = lldb::eMemberFunctionKindStaticMethod;
4666 else
4667 kind = lldb::eMemberFunctionKindInstanceMethod;
4668 }
4669 }
4670 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004671 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004672 }
4673 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004674
Kate Stoneb9c1b512016-09-06 20:57:50 +00004675 case clang::Type::Typedef:
4676 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4677 ->getDecl()
4678 ->getUnderlyingType()
4679 .getAsOpaquePtr(),
4680 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004681
Kate Stoneb9c1b512016-09-06 20:57:50 +00004682 case clang::Type::Auto:
4683 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4684 ->getDeducedType()
4685 .getAsOpaquePtr(),
4686 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004687
Kate Stoneb9c1b512016-09-06 20:57:50 +00004688 case clang::Type::Elaborated:
4689 return GetMemberFunctionAtIndex(
4690 llvm::cast<clang::ElaboratedType>(qual_type)
4691 ->getNamedType()
4692 .getAsOpaquePtr(),
4693 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004694
Kate Stoneb9c1b512016-09-06 20:57:50 +00004695 case clang::Type::Paren:
4696 return GetMemberFunctionAtIndex(
4697 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4698 idx);
4699
4700 default:
4701 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004702 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004703 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004704
Kate Stoneb9c1b512016-09-06 20:57:50 +00004705 if (kind == eMemberFunctionKindUnknown)
4706 return TypeMemberFunctionImpl();
4707 else
4708 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004709}
4710
Greg Claytona1e5dc82015-08-11 22:53:00 +00004711CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004712ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4713 if (type)
4714 return CompilerType(getASTContext(),
4715 GetQualType(type).getNonReferenceType());
4716 return CompilerType();
4717}
4718
4719CompilerType ClangASTContext::CreateTypedefType(
4720 const CompilerType &type, const char *typedef_name,
4721 const CompilerDeclContext &compiler_decl_ctx) {
4722 if (type && typedef_name && typedef_name[0]) {
4723 ClangASTContext *ast =
4724 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4725 if (!ast)
4726 return CompilerType();
4727 clang::ASTContext *clang_ast = ast->getASTContext();
4728 clang::QualType qual_type(ClangUtil::GetQualType(type));
4729
4730 clang::DeclContext *decl_ctx =
4731 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4732 if (decl_ctx == nullptr)
4733 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4734
4735 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4736 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4737 &clang_ast->Idents.get(typedef_name),
4738 clang_ast->getTrivialTypeSourceInfo(qual_type));
4739
4740 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4741
4742 // Get a uniqued clang::QualType for the typedef decl type
4743 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4744 }
4745 return CompilerType();
4746}
4747
4748CompilerType
4749ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4750 if (type) {
4751 clang::QualType qual_type(GetQualType(type));
4752 return CompilerType(getASTContext(),
4753 qual_type.getTypePtr()->getPointeeType());
4754 }
4755 return CompilerType();
4756}
4757
4758CompilerType
4759ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4760 if (type) {
4761 clang::QualType qual_type(GetQualType(type));
4762
4763 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4764 switch (type_class) {
4765 case clang::Type::ObjCObject:
4766 case clang::Type::ObjCInterface:
4767 return CompilerType(getASTContext(),
4768 getASTContext()->getObjCObjectPointerType(qual_type));
4769
4770 default:
4771 return CompilerType(getASTContext(),
4772 getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004773 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004774 }
4775 return CompilerType();
4776}
4777
4778CompilerType
4779ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4780 if (type)
4781 return CompilerType(this, getASTContext()
4782 ->getLValueReferenceType(GetQualType(type))
4783 .getAsOpaquePtr());
4784 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004785 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004786}
4787
Kate Stoneb9c1b512016-09-06 20:57:50 +00004788CompilerType
4789ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4790 if (type)
4791 return CompilerType(this, getASTContext()
4792 ->getRValueReferenceType(GetQualType(type))
4793 .getAsOpaquePtr());
4794 else
4795 return CompilerType();
4796}
4797
4798CompilerType
4799ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4800 if (type) {
4801 clang::QualType result(GetQualType(type));
4802 result.addConst();
4803 return CompilerType(this, result.getAsOpaquePtr());
4804 }
4805 return CompilerType();
4806}
4807
4808CompilerType
4809ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4810 if (type) {
4811 clang::QualType result(GetQualType(type));
4812 result.addVolatile();
4813 return CompilerType(this, result.getAsOpaquePtr());
4814 }
4815 return CompilerType();
4816}
4817
4818CompilerType
4819ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4820 if (type) {
4821 clang::QualType result(GetQualType(type));
4822 result.addRestrict();
4823 return CompilerType(this, result.getAsOpaquePtr());
4824 }
4825 return CompilerType();
4826}
4827
4828CompilerType
4829ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4830 const char *typedef_name,
4831 const CompilerDeclContext &compiler_decl_ctx) {
4832 if (type) {
4833 clang::ASTContext *clang_ast = getASTContext();
4834 clang::QualType qual_type(GetQualType(type));
4835
4836 clang::DeclContext *decl_ctx =
4837 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4838 if (decl_ctx == nullptr)
4839 decl_ctx = getASTContext()->getTranslationUnitDecl();
4840
4841 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4842 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4843 &clang_ast->Idents.get(typedef_name),
4844 clang_ast->getTrivialTypeSourceInfo(qual_type));
4845
4846 clang::TagDecl *tdecl = nullptr;
4847 if (!qual_type.isNull()) {
4848 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4849 tdecl = rt->getDecl();
4850 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4851 tdecl = et->getDecl();
4852 }
4853
4854 // Check whether this declaration is an anonymous struct, union, or enum,
Adrian Prantl05097242018-04-30 16:49:04 +00004855 // hidden behind a typedef. If so, we try to check whether we have a
4856 // typedef tag to attach to the original record declaration
Kate Stoneb9c1b512016-09-06 20:57:50 +00004857 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4858 tdecl->setTypedefNameForAnonDecl(decl);
4859
4860 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4861
4862 // Get a uniqued clang::QualType for the typedef decl type
4863 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4864 }
4865 return CompilerType();
4866}
4867
4868CompilerType
4869ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4870 if (type) {
4871 const clang::TypedefType *typedef_type =
4872 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4873 if (typedef_type)
4874 return CompilerType(getASTContext(),
4875 typedef_type->getDecl()->getUnderlyingType());
4876 }
4877 return CompilerType();
4878}
Greg Claytond8d4a572015-08-11 21:38:15 +00004879
4880//----------------------------------------------------------------------
4881// Create related types using the current type's AST
4882//----------------------------------------------------------------------
4883
Kate Stoneb9c1b512016-09-06 20:57:50 +00004884CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4885 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004886}
4887//----------------------------------------------------------------------
4888// Exploring the type
4889//----------------------------------------------------------------------
4890
Kate Stoneb9c1b512016-09-06 20:57:50 +00004891uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4892 ExecutionContextScope *exe_scope) {
4893 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00004894 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004895 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004896 switch (type_class) {
4897 case clang::Type::Record:
4898 if (GetCompleteType(type))
4899 return getASTContext()->getTypeSize(qual_type);
4900 else
4901 return 0;
4902 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004903
Kate Stoneb9c1b512016-09-06 20:57:50 +00004904 case clang::Type::ObjCInterface:
4905 case clang::Type::ObjCObject: {
4906 ExecutionContext exe_ctx(exe_scope);
4907 Process *process = exe_ctx.GetProcessPtr();
4908 if (process) {
4909 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4910 if (objc_runtime) {
4911 uint64_t bit_size = 0;
4912 if (objc_runtime->GetTypeBitSize(
4913 CompilerType(getASTContext(), qual_type), bit_size))
4914 return bit_size;
4915 }
4916 } else {
4917 static bool g_printed = false;
4918 if (!g_printed) {
4919 StreamString s;
4920 DumpTypeDescription(type, &s);
4921
4922 llvm::outs() << "warning: trying to determine the size of type ";
4923 llvm::outs() << s.GetString() << "\n";
4924 llvm::outs() << "without a valid ExecutionContext. this is not "
4925 "reliable. please file a bug against LLDB.\n";
4926 llvm::outs() << "backtrace:\n";
4927 llvm::sys::PrintStackTrace(llvm::outs());
4928 llvm::outs() << "\n";
4929 g_printed = true;
4930 }
4931 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004932 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004933 LLVM_FALLTHROUGH;
4934 default:
4935 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
4936 if (bit_size == 0) {
4937 if (qual_type->isIncompleteArrayType())
4938 return getASTContext()->getTypeSize(
4939 qual_type->getArrayElementTypeNoTypeQual()
4940 ->getCanonicalTypeUnqualified());
4941 }
4942 if (qual_type->isObjCObjectOrInterfaceType())
4943 return bit_size +
4944 getASTContext()->getTypeSize(
4945 getASTContext()->ObjCBuiltinClassTy);
4946 return bit_size;
4947 }
4948 }
4949 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004950}
4951
Kate Stoneb9c1b512016-09-06 20:57:50 +00004952size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
4953 if (GetCompleteType(type))
4954 return getASTContext()->getTypeAlign(GetQualType(type));
4955 return 0;
4956}
4957
4958lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
4959 uint64_t &count) {
4960 if (!type)
4961 return lldb::eEncodingInvalid;
4962
4963 count = 1;
4964 clang::QualType qual_type(GetCanonicalQualType(type));
4965
4966 switch (qual_type->getTypeClass()) {
4967 case clang::Type::UnaryTransform:
4968 break;
4969
4970 case clang::Type::FunctionNoProto:
4971 case clang::Type::FunctionProto:
4972 break;
4973
4974 case clang::Type::IncompleteArray:
4975 case clang::Type::VariableArray:
4976 break;
4977
4978 case clang::Type::ConstantArray:
4979 break;
4980
Fangrui Song8f284882018-07-13 22:40:40 +00004981 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004982 case clang::Type::ExtVector:
4983 case clang::Type::Vector:
4984 // TODO: Set this to more than one???
4985 break;
4986
4987 case clang::Type::Builtin:
4988 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4989 case clang::BuiltinType::Void:
4990 break;
4991
4992 case clang::BuiltinType::Bool:
4993 case clang::BuiltinType::Char_S:
4994 case clang::BuiltinType::SChar:
4995 case clang::BuiltinType::WChar_S:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004996 case clang::BuiltinType::Short:
4997 case clang::BuiltinType::Int:
4998 case clang::BuiltinType::Long:
4999 case clang::BuiltinType::LongLong:
5000 case clang::BuiltinType::Int128:
5001 return lldb::eEncodingSint;
5002
5003 case clang::BuiltinType::Char_U:
5004 case clang::BuiltinType::UChar:
5005 case clang::BuiltinType::WChar_U:
Richard Smith51d12d82018-05-02 02:43:22 +00005006 case clang::BuiltinType::Char8:
5007 case clang::BuiltinType::Char16:
5008 case clang::BuiltinType::Char32:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005009 case clang::BuiltinType::UShort:
5010 case clang::BuiltinType::UInt:
5011 case clang::BuiltinType::ULong:
5012 case clang::BuiltinType::ULongLong:
5013 case clang::BuiltinType::UInt128:
5014 return lldb::eEncodingUint;
5015
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005016 // Fixed point types. Note that they are currently ignored.
5017 case clang::BuiltinType::ShortAccum:
5018 case clang::BuiltinType::Accum:
5019 case clang::BuiltinType::LongAccum:
5020 case clang::BuiltinType::UShortAccum:
5021 case clang::BuiltinType::UAccum:
5022 case clang::BuiltinType::ULongAccum:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005023 case clang::BuiltinType::ShortFract:
Fangrui Songa5e59c52018-06-14 18:19:40 +00005024 case clang::BuiltinType::Fract:
5025 case clang::BuiltinType::LongFract:
5026 case clang::BuiltinType::UShortFract:
5027 case clang::BuiltinType::UFract:
5028 case clang::BuiltinType::ULongFract:
5029 case clang::BuiltinType::SatShortAccum:
5030 case clang::BuiltinType::SatAccum:
5031 case clang::BuiltinType::SatLongAccum:
5032 case clang::BuiltinType::SatUShortAccum:
5033 case clang::BuiltinType::SatUAccum:
5034 case clang::BuiltinType::SatULongAccum:
5035 case clang::BuiltinType::SatShortFract:
5036 case clang::BuiltinType::SatFract:
5037 case clang::BuiltinType::SatLongFract:
5038 case clang::BuiltinType::SatUShortFract:
5039 case clang::BuiltinType::SatUFract:
5040 case clang::BuiltinType::SatULongFract:
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00005041 break;
5042
Kate Stoneb9c1b512016-09-06 20:57:50 +00005043 case clang::BuiltinType::Half:
5044 case clang::BuiltinType::Float:
Ted Woodward4355c7c2017-09-20 19:16:53 +00005045 case clang::BuiltinType::Float16:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005046 case clang::BuiltinType::Float128:
5047 case clang::BuiltinType::Double:
5048 case clang::BuiltinType::LongDouble:
5049 return lldb::eEncodingIEEE754;
5050
5051 case clang::BuiltinType::ObjCClass:
5052 case clang::BuiltinType::ObjCId:
5053 case clang::BuiltinType::ObjCSel:
5054 return lldb::eEncodingUint;
5055
5056 case clang::BuiltinType::NullPtr:
5057 return lldb::eEncodingUint;
5058
5059 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5060 case clang::BuiltinType::Kind::BoundMember:
5061 case clang::BuiltinType::Kind::BuiltinFn:
5062 case clang::BuiltinType::Kind::Dependent:
5063 case clang::BuiltinType::Kind::OCLClkEvent:
5064 case clang::BuiltinType::Kind::OCLEvent:
5065 case clang::BuiltinType::Kind::OCLImage1dRO:
5066 case clang::BuiltinType::Kind::OCLImage1dWO:
5067 case clang::BuiltinType::Kind::OCLImage1dRW:
5068 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5069 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5070 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5071 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5072 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5073 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5074 case clang::BuiltinType::Kind::OCLImage2dRO:
5075 case clang::BuiltinType::Kind::OCLImage2dWO:
5076 case clang::BuiltinType::Kind::OCLImage2dRW:
5077 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5078 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5079 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5080 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5081 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5082 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5083 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5084 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5085 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5086 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5087 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5088 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5089 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5090 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5091 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5092 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5093 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5094 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5095 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5096 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5097 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5098 case clang::BuiltinType::Kind::OCLImage3dRO:
5099 case clang::BuiltinType::Kind::OCLImage3dWO:
5100 case clang::BuiltinType::Kind::OCLImage3dRW:
5101 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005102 case clang::BuiltinType::Kind::OCLReserveID:
5103 case clang::BuiltinType::Kind::OCLSampler:
5104 case clang::BuiltinType::Kind::OMPArraySection:
5105 case clang::BuiltinType::Kind::Overload:
5106 case clang::BuiltinType::Kind::PseudoObject:
5107 case clang::BuiltinType::Kind::UnknownAny:
5108 break;
5109 }
5110 break;
Adrian Prantl05097242018-04-30 16:49:04 +00005111 // All pointer types are represented as unsigned integer encodings. We may
5112 // nee to add a eEncodingPointer if we ever need to know the difference
Kate Stoneb9c1b512016-09-06 20:57:50 +00005113 case clang::Type::ObjCObjectPointer:
5114 case clang::Type::BlockPointer:
5115 case clang::Type::Pointer:
5116 case clang::Type::LValueReference:
5117 case clang::Type::RValueReference:
5118 case clang::Type::MemberPointer:
5119 return lldb::eEncodingUint;
5120 case clang::Type::Complex: {
5121 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5122 if (qual_type->isComplexType())
5123 encoding = lldb::eEncodingIEEE754;
5124 else {
5125 const clang::ComplexType *complex_type =
5126 qual_type->getAsComplexIntegerType();
5127 if (complex_type)
5128 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5129 .GetEncoding(count);
5130 else
5131 encoding = lldb::eEncodingSint;
5132 }
5133 count = 2;
5134 return encoding;
5135 }
5136
5137 case clang::Type::ObjCInterface:
5138 break;
5139 case clang::Type::Record:
5140 break;
5141 case clang::Type::Enum:
5142 return lldb::eEncodingSint;
5143 case clang::Type::Typedef:
5144 return CompilerType(getASTContext(),
5145 llvm::cast<clang::TypedefType>(qual_type)
5146 ->getDecl()
5147 ->getUnderlyingType())
5148 .GetEncoding(count);
5149
5150 case clang::Type::Auto:
5151 return CompilerType(
5152 getASTContext(),
5153 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5154 .GetEncoding(count);
5155
5156 case clang::Type::Elaborated:
5157 return CompilerType(
5158 getASTContext(),
5159 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5160 .GetEncoding(count);
5161
5162 case clang::Type::Paren:
5163 return CompilerType(getASTContext(),
5164 llvm::cast<clang::ParenType>(qual_type)->desugar())
5165 .GetEncoding(count);
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005166 case clang::Type::TypeOfExpr:
5167 return CompilerType(getASTContext(),
5168 llvm::cast<clang::TypeOfExprType>(qual_type)
5169 ->getUnderlyingExpr()
5170 ->getType())
5171 .GetEncoding(count);
5172 case clang::Type::TypeOf:
5173 return CompilerType(
5174 getASTContext(),
5175 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5176 .GetEncoding(count);
5177 case clang::Type::Decltype:
5178 return CompilerType(
5179 getASTContext(),
5180 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5181 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005182 case clang::Type::DependentSizedArray:
5183 case clang::Type::DependentSizedExtVector:
5184 case clang::Type::UnresolvedUsing:
5185 case clang::Type::Attributed:
5186 case clang::Type::TemplateTypeParm:
5187 case clang::Type::SubstTemplateTypeParm:
5188 case clang::Type::SubstTemplateTypeParmPack:
5189 case clang::Type::InjectedClassName:
5190 case clang::Type::DependentName:
5191 case clang::Type::DependentTemplateSpecialization:
5192 case clang::Type::PackExpansion:
5193 case clang::Type::ObjCObject:
5194
Kate Stoneb9c1b512016-09-06 20:57:50 +00005195 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005196 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005197 case clang::Type::Atomic:
5198 case clang::Type::Adjusted:
5199 case clang::Type::Pipe:
5200 break;
5201
5202 // pointer type decayed from an array or function type.
5203 case clang::Type::Decayed:
5204 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005205 case clang::Type::ObjCTypeParam:
5206 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005207
5208 case clang::Type::DependentAddressSpace:
5209 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005210 }
5211 count = 0;
5212 return lldb::eEncodingInvalid;
5213}
5214
5215lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5216 if (!type)
5217 return lldb::eFormatDefault;
5218
5219 clang::QualType qual_type(GetCanonicalQualType(type));
5220
5221 switch (qual_type->getTypeClass()) {
5222 case clang::Type::UnaryTransform:
5223 break;
5224
5225 case clang::Type::FunctionNoProto:
5226 case clang::Type::FunctionProto:
5227 break;
5228
5229 case clang::Type::IncompleteArray:
5230 case clang::Type::VariableArray:
5231 break;
5232
5233 case clang::Type::ConstantArray:
5234 return lldb::eFormatVoid; // no value
5235
Fangrui Song8f284882018-07-13 22:40:40 +00005236 case clang::Type::DependentVector:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005237 case clang::Type::ExtVector:
5238 case clang::Type::Vector:
5239 break;
5240
5241 case clang::Type::Builtin:
5242 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5243 // default: assert(0 && "Unknown builtin type!");
5244 case clang::BuiltinType::UnknownAny:
5245 case clang::BuiltinType::Void:
5246 case clang::BuiltinType::BoundMember:
5247 break;
5248
5249 case clang::BuiltinType::Bool:
5250 return lldb::eFormatBoolean;
5251 case clang::BuiltinType::Char_S:
5252 case clang::BuiltinType::SChar:
5253 case clang::BuiltinType::WChar_S:
5254 case clang::BuiltinType::Char_U:
5255 case clang::BuiltinType::UChar:
5256 case clang::BuiltinType::WChar_U:
5257 return lldb::eFormatChar;
5258 case clang::BuiltinType::Char16:
5259 return lldb::eFormatUnicode16;
5260 case clang::BuiltinType::Char32:
5261 return lldb::eFormatUnicode32;
5262 case clang::BuiltinType::UShort:
5263 return lldb::eFormatUnsigned;
5264 case clang::BuiltinType::Short:
5265 return lldb::eFormatDecimal;
5266 case clang::BuiltinType::UInt:
5267 return lldb::eFormatUnsigned;
5268 case clang::BuiltinType::Int:
5269 return lldb::eFormatDecimal;
5270 case clang::BuiltinType::ULong:
5271 return lldb::eFormatUnsigned;
5272 case clang::BuiltinType::Long:
5273 return lldb::eFormatDecimal;
5274 case clang::BuiltinType::ULongLong:
5275 return lldb::eFormatUnsigned;
5276 case clang::BuiltinType::LongLong:
5277 return lldb::eFormatDecimal;
5278 case clang::BuiltinType::UInt128:
5279 return lldb::eFormatUnsigned;
5280 case clang::BuiltinType::Int128:
5281 return lldb::eFormatDecimal;
5282 case clang::BuiltinType::Half:
5283 case clang::BuiltinType::Float:
5284 case clang::BuiltinType::Double:
5285 case clang::BuiltinType::LongDouble:
5286 return lldb::eFormatFloat;
5287 default:
5288 return lldb::eFormatHex;
5289 }
5290 break;
5291 case clang::Type::ObjCObjectPointer:
5292 return lldb::eFormatHex;
5293 case clang::Type::BlockPointer:
5294 return lldb::eFormatHex;
5295 case clang::Type::Pointer:
5296 return lldb::eFormatHex;
5297 case clang::Type::LValueReference:
5298 case clang::Type::RValueReference:
5299 return lldb::eFormatHex;
5300 case clang::Type::MemberPointer:
5301 break;
5302 case clang::Type::Complex: {
5303 if (qual_type->isComplexType())
5304 return lldb::eFormatComplex;
5305 else
5306 return lldb::eFormatComplexInteger;
5307 }
5308 case clang::Type::ObjCInterface:
5309 break;
5310 case clang::Type::Record:
5311 break;
5312 case clang::Type::Enum:
5313 return lldb::eFormatEnum;
5314 case clang::Type::Typedef:
5315 return CompilerType(getASTContext(),
5316 llvm::cast<clang::TypedefType>(qual_type)
5317 ->getDecl()
5318 ->getUnderlyingType())
5319 .GetFormat();
5320 case clang::Type::Auto:
5321 return CompilerType(getASTContext(),
5322 llvm::cast<clang::AutoType>(qual_type)->desugar())
5323 .GetFormat();
5324 case clang::Type::Paren:
5325 return CompilerType(getASTContext(),
5326 llvm::cast<clang::ParenType>(qual_type)->desugar())
5327 .GetFormat();
5328 case clang::Type::Elaborated:
5329 return CompilerType(
5330 getASTContext(),
5331 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5332 .GetFormat();
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005333 case clang::Type::TypeOfExpr:
5334 return CompilerType(getASTContext(),
5335 llvm::cast<clang::TypeOfExprType>(qual_type)
5336 ->getUnderlyingExpr()
5337 ->getType())
5338 .GetFormat();
5339 case clang::Type::TypeOf:
5340 return CompilerType(
5341 getASTContext(),
5342 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5343 .GetFormat();
5344 case clang::Type::Decltype:
5345 return CompilerType(
5346 getASTContext(),
5347 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5348 .GetFormat();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005349 case clang::Type::DependentSizedArray:
5350 case clang::Type::DependentSizedExtVector:
5351 case clang::Type::UnresolvedUsing:
5352 case clang::Type::Attributed:
5353 case clang::Type::TemplateTypeParm:
5354 case clang::Type::SubstTemplateTypeParm:
5355 case clang::Type::SubstTemplateTypeParmPack:
5356 case clang::Type::InjectedClassName:
5357 case clang::Type::DependentName:
5358 case clang::Type::DependentTemplateSpecialization:
5359 case clang::Type::PackExpansion:
5360 case clang::Type::ObjCObject:
5361
Kate Stoneb9c1b512016-09-06 20:57:50 +00005362 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005363 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005364 case clang::Type::Atomic:
5365 case clang::Type::Adjusted:
5366 case clang::Type::Pipe:
5367 break;
5368
5369 // pointer type decayed from an array or function type.
5370 case clang::Type::Decayed:
5371 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005372 case clang::Type::ObjCTypeParam:
5373 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005374
5375 case clang::Type::DependentAddressSpace:
5376 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005377 }
5378 // We don't know hot to display this type...
5379 return lldb::eFormatBytes;
5380}
5381
5382static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5383 bool check_superclass) {
5384 while (class_interface_decl) {
5385 if (class_interface_decl->ivar_size() > 0)
5386 return true;
5387
5388 if (check_superclass)
5389 class_interface_decl = class_interface_decl->getSuperClass();
5390 else
5391 break;
5392 }
5393 return false;
5394}
5395
5396uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
5397 bool omit_empty_base_classes) {
5398 if (!type)
5399 return 0;
5400
5401 uint32_t num_children = 0;
5402 clang::QualType qual_type(GetQualType(type));
5403 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5404 switch (type_class) {
5405 case clang::Type::Builtin:
5406 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5407 case clang::BuiltinType::ObjCId: // child is Class
5408 case clang::BuiltinType::ObjCClass: // child is Class
5409 num_children = 1;
5410 break;
5411
5412 default:
5413 break;
5414 }
5415 break;
5416
5417 case clang::Type::Complex:
5418 return 0;
5419
5420 case clang::Type::Record:
5421 if (GetCompleteQualType(getASTContext(), qual_type)) {
5422 const clang::RecordType *record_type =
5423 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5424 const clang::RecordDecl *record_decl = record_type->getDecl();
5425 assert(record_decl);
5426 const clang::CXXRecordDecl *cxx_record_decl =
5427 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5428 if (cxx_record_decl) {
5429 if (omit_empty_base_classes) {
Adrian Prantl05097242018-04-30 16:49:04 +00005430 // Check each base classes to see if it or any of its base classes
5431 // contain any fields. This can help limit the noise in variable
5432 // views by not having to show base classes that contain no members.
Kate Stoneb9c1b512016-09-06 20:57:50 +00005433 clang::CXXRecordDecl::base_class_const_iterator base_class,
5434 base_class_end;
5435 for (base_class = cxx_record_decl->bases_begin(),
5436 base_class_end = cxx_record_decl->bases_end();
5437 base_class != base_class_end; ++base_class) {
5438 const clang::CXXRecordDecl *base_class_decl =
5439 llvm::cast<clang::CXXRecordDecl>(
5440 base_class->getType()
5441 ->getAs<clang::RecordType>()
5442 ->getDecl());
5443
5444 // Skip empty base classes
5445 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5446 continue;
5447
5448 num_children++;
5449 }
5450 } else {
5451 // Include all base classes
5452 num_children += cxx_record_decl->getNumBases();
5453 }
5454 }
5455 clang::RecordDecl::field_iterator field, field_end;
5456 for (field = record_decl->field_begin(),
5457 field_end = record_decl->field_end();
5458 field != field_end; ++field)
5459 ++num_children;
5460 }
5461 break;
5462
5463 case clang::Type::ObjCObject:
5464 case clang::Type::ObjCInterface:
5465 if (GetCompleteQualType(getASTContext(), qual_type)) {
5466 const clang::ObjCObjectType *objc_class_type =
5467 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5468 assert(objc_class_type);
5469 if (objc_class_type) {
5470 clang::ObjCInterfaceDecl *class_interface_decl =
5471 objc_class_type->getInterface();
5472
5473 if (class_interface_decl) {
5474
5475 clang::ObjCInterfaceDecl *superclass_interface_decl =
5476 class_interface_decl->getSuperClass();
5477 if (superclass_interface_decl) {
5478 if (omit_empty_base_classes) {
5479 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5480 ++num_children;
5481 } else
5482 ++num_children;
5483 }
5484
5485 num_children += class_interface_decl->ivar_size();
5486 }
5487 }
5488 }
5489 break;
5490
5491 case clang::Type::ObjCObjectPointer: {
5492 const clang::ObjCObjectPointerType *pointer_type =
5493 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5494 clang::QualType pointee_type = pointer_type->getPointeeType();
5495 uint32_t num_pointee_children =
5496 CompilerType(getASTContext(), pointee_type)
5497 .GetNumChildren(omit_empty_base_classes);
5498 // If this type points to a simple type, then it has 1 child
5499 if (num_pointee_children == 0)
5500 num_children = 1;
5501 else
5502 num_children = num_pointee_children;
5503 } break;
5504
5505 case clang::Type::Vector:
5506 case clang::Type::ExtVector:
5507 num_children =
5508 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5509 break;
5510
5511 case clang::Type::ConstantArray:
5512 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5513 ->getSize()
5514 .getLimitedValue();
5515 break;
5516
5517 case clang::Type::Pointer: {
5518 const clang::PointerType *pointer_type =
5519 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5520 clang::QualType pointee_type(pointer_type->getPointeeType());
5521 uint32_t num_pointee_children =
5522 CompilerType(getASTContext(), pointee_type)
5523 .GetNumChildren(omit_empty_base_classes);
5524 if (num_pointee_children == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00005525 // We have a pointer to a pointee type that claims it has no children. We
5526 // will want to look at
Kate Stoneb9c1b512016-09-06 20:57:50 +00005527 num_children = GetNumPointeeChildren(pointee_type);
5528 } else
5529 num_children = num_pointee_children;
5530 } break;
5531
5532 case clang::Type::LValueReference:
5533 case clang::Type::RValueReference: {
5534 const clang::ReferenceType *reference_type =
5535 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5536 clang::QualType pointee_type = reference_type->getPointeeType();
5537 uint32_t num_pointee_children =
5538 CompilerType(getASTContext(), pointee_type)
5539 .GetNumChildren(omit_empty_base_classes);
5540 // If this type points to a simple type, then it has 1 child
5541 if (num_pointee_children == 0)
5542 num_children = 1;
5543 else
5544 num_children = num_pointee_children;
5545 } break;
5546
5547 case clang::Type::Typedef:
5548 num_children =
5549 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5550 ->getDecl()
5551 ->getUnderlyingType())
5552 .GetNumChildren(omit_empty_base_classes);
5553 break;
5554
5555 case clang::Type::Auto:
5556 num_children =
5557 CompilerType(getASTContext(),
5558 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5559 .GetNumChildren(omit_empty_base_classes);
5560 break;
5561
5562 case clang::Type::Elaborated:
5563 num_children =
5564 CompilerType(
5565 getASTContext(),
5566 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5567 .GetNumChildren(omit_empty_base_classes);
5568 break;
5569
5570 case clang::Type::Paren:
5571 num_children =
5572 CompilerType(getASTContext(),
5573 llvm::cast<clang::ParenType>(qual_type)->desugar())
5574 .GetNumChildren(omit_empty_base_classes);
5575 break;
5576 default:
5577 break;
5578 }
5579 return num_children;
5580}
5581
5582CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5583 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005584}
5585
Greg Claytond8d4a572015-08-11 21:38:15 +00005586lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005587ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5588 if (type) {
5589 clang::QualType qual_type(GetQualType(type));
5590 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5591 if (type_class == clang::Type::Builtin) {
5592 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5593 case clang::BuiltinType::Void:
5594 return eBasicTypeVoid;
5595 case clang::BuiltinType::Bool:
5596 return eBasicTypeBool;
5597 case clang::BuiltinType::Char_S:
5598 return eBasicTypeSignedChar;
5599 case clang::BuiltinType::Char_U:
5600 return eBasicTypeUnsignedChar;
5601 case clang::BuiltinType::Char16:
5602 return eBasicTypeChar16;
5603 case clang::BuiltinType::Char32:
5604 return eBasicTypeChar32;
5605 case clang::BuiltinType::UChar:
5606 return eBasicTypeUnsignedChar;
5607 case clang::BuiltinType::SChar:
5608 return eBasicTypeSignedChar;
5609 case clang::BuiltinType::WChar_S:
5610 return eBasicTypeSignedWChar;
5611 case clang::BuiltinType::WChar_U:
5612 return eBasicTypeUnsignedWChar;
5613 case clang::BuiltinType::Short:
5614 return eBasicTypeShort;
5615 case clang::BuiltinType::UShort:
5616 return eBasicTypeUnsignedShort;
5617 case clang::BuiltinType::Int:
5618 return eBasicTypeInt;
5619 case clang::BuiltinType::UInt:
5620 return eBasicTypeUnsignedInt;
5621 case clang::BuiltinType::Long:
5622 return eBasicTypeLong;
5623 case clang::BuiltinType::ULong:
5624 return eBasicTypeUnsignedLong;
5625 case clang::BuiltinType::LongLong:
5626 return eBasicTypeLongLong;
5627 case clang::BuiltinType::ULongLong:
5628 return eBasicTypeUnsignedLongLong;
5629 case clang::BuiltinType::Int128:
5630 return eBasicTypeInt128;
5631 case clang::BuiltinType::UInt128:
5632 return eBasicTypeUnsignedInt128;
5633
5634 case clang::BuiltinType::Half:
5635 return eBasicTypeHalf;
5636 case clang::BuiltinType::Float:
5637 return eBasicTypeFloat;
5638 case clang::BuiltinType::Double:
5639 return eBasicTypeDouble;
5640 case clang::BuiltinType::LongDouble:
5641 return eBasicTypeLongDouble;
5642
5643 case clang::BuiltinType::NullPtr:
5644 return eBasicTypeNullPtr;
5645 case clang::BuiltinType::ObjCId:
5646 return eBasicTypeObjCID;
5647 case clang::BuiltinType::ObjCClass:
5648 return eBasicTypeObjCClass;
5649 case clang::BuiltinType::ObjCSel:
5650 return eBasicTypeObjCSel;
5651 default:
5652 return eBasicTypeOther;
5653 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005654 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005655 }
5656 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005657}
5658
Kate Stoneb9c1b512016-09-06 20:57:50 +00005659void ClangASTContext::ForEachEnumerator(
5660 lldb::opaque_compiler_type_t type,
5661 std::function<bool(const CompilerType &integer_type,
5662 const ConstString &name,
5663 const llvm::APSInt &value)> const &callback) {
5664 const clang::EnumType *enum_type =
5665 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5666 if (enum_type) {
5667 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5668 if (enum_decl) {
5669 CompilerType integer_type(this,
5670 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005671
Kate Stoneb9c1b512016-09-06 20:57:50 +00005672 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5673 for (enum_pos = enum_decl->enumerator_begin(),
5674 enum_end_pos = enum_decl->enumerator_end();
5675 enum_pos != enum_end_pos; ++enum_pos) {
5676 ConstString name(enum_pos->getNameAsString().c_str());
5677 if (!callback(integer_type, name, enum_pos->getInitVal()))
5678 break;
5679 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005680 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005681 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005682}
5683
Greg Claytond8d4a572015-08-11 21:38:15 +00005684#pragma mark Aggregate Types
5685
Kate Stoneb9c1b512016-09-06 20:57:50 +00005686uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5687 if (!type)
5688 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005689
Kate Stoneb9c1b512016-09-06 20:57:50 +00005690 uint32_t count = 0;
5691 clang::QualType qual_type(GetCanonicalQualType(type));
5692 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5693 switch (type_class) {
5694 case clang::Type::Record:
5695 if (GetCompleteType(type)) {
5696 const clang::RecordType *record_type =
5697 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5698 if (record_type) {
5699 clang::RecordDecl *record_decl = record_type->getDecl();
5700 if (record_decl) {
5701 uint32_t field_idx = 0;
5702 clang::RecordDecl::field_iterator field, field_end;
5703 for (field = record_decl->field_begin(),
5704 field_end = record_decl->field_end();
5705 field != field_end; ++field)
5706 ++field_idx;
5707 count = field_idx;
5708 }
5709 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005710 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005711 break;
5712
5713 case clang::Type::Typedef:
5714 count =
5715 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5716 ->getDecl()
5717 ->getUnderlyingType())
5718 .GetNumFields();
5719 break;
5720
5721 case clang::Type::Auto:
5722 count =
5723 CompilerType(getASTContext(),
5724 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5725 .GetNumFields();
5726 break;
5727
5728 case clang::Type::Elaborated:
5729 count = CompilerType(
5730 getASTContext(),
5731 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5732 .GetNumFields();
5733 break;
5734
5735 case clang::Type::Paren:
5736 count = CompilerType(getASTContext(),
5737 llvm::cast<clang::ParenType>(qual_type)->desugar())
5738 .GetNumFields();
5739 break;
5740
Sean Callananf9c622a2016-09-30 18:44:43 +00005741 case clang::Type::ObjCObjectPointer: {
5742 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005743 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005744 const clang::ObjCInterfaceType *objc_interface_type =
5745 objc_class_type->getInterfaceType();
5746 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005747 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5748 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005749 clang::ObjCInterfaceDecl *class_interface_decl =
5750 objc_interface_type->getDecl();
5751 if (class_interface_decl) {
5752 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005753 }
5754 }
5755 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005756 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005757
5758 case clang::Type::ObjCObject:
5759 case clang::Type::ObjCInterface:
5760 if (GetCompleteType(type)) {
5761 const clang::ObjCObjectType *objc_class_type =
5762 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5763 if (objc_class_type) {
5764 clang::ObjCInterfaceDecl *class_interface_decl =
5765 objc_class_type->getInterface();
5766
5767 if (class_interface_decl)
5768 count = class_interface_decl->ivar_size();
5769 }
5770 }
5771 break;
5772
5773 default:
5774 break;
5775 }
5776 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005777}
5778
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005779static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005780GetObjCFieldAtIndex(clang::ASTContext *ast,
5781 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5782 std::string &name, uint64_t *bit_offset_ptr,
5783 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5784 if (class_interface_decl) {
5785 if (idx < (class_interface_decl->ivar_size())) {
5786 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5787 ivar_end = class_interface_decl->ivar_end();
5788 uint32_t ivar_idx = 0;
5789
5790 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5791 ++ivar_pos, ++ivar_idx) {
5792 if (ivar_idx == idx) {
5793 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5794
5795 clang::QualType ivar_qual_type(ivar_decl->getType());
5796
5797 name.assign(ivar_decl->getNameAsString());
5798
5799 if (bit_offset_ptr) {
5800 const clang::ASTRecordLayout &interface_layout =
5801 ast->getASTObjCInterfaceLayout(class_interface_decl);
5802 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5803 }
5804
5805 const bool is_bitfield = ivar_pos->isBitField();
5806
5807 if (bitfield_bit_size_ptr) {
5808 *bitfield_bit_size_ptr = 0;
5809
5810 if (is_bitfield && ast) {
5811 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5812 llvm::APSInt bitfield_apsint;
5813 if (bitfield_bit_size_expr &&
5814 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5815 *ast)) {
5816 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5817 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005818 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005819 }
5820 if (is_bitfield_ptr)
5821 *is_bitfield_ptr = is_bitfield;
5822
5823 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005824 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005825 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005826 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005827 }
5828 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005829}
5830
Kate Stoneb9c1b512016-09-06 20:57:50 +00005831CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5832 size_t idx, std::string &name,
5833 uint64_t *bit_offset_ptr,
5834 uint32_t *bitfield_bit_size_ptr,
5835 bool *is_bitfield_ptr) {
5836 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005837 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005838
5839 clang::QualType qual_type(GetCanonicalQualType(type));
5840 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5841 switch (type_class) {
5842 case clang::Type::Record:
5843 if (GetCompleteType(type)) {
5844 const clang::RecordType *record_type =
5845 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5846 const clang::RecordDecl *record_decl = record_type->getDecl();
5847 uint32_t field_idx = 0;
5848 clang::RecordDecl::field_iterator field, field_end;
5849 for (field = record_decl->field_begin(),
5850 field_end = record_decl->field_end();
5851 field != field_end; ++field, ++field_idx) {
5852 if (idx == field_idx) {
5853 // Print the member type if requested
5854 // Print the member name and equal sign
5855 name.assign(field->getNameAsString());
5856
5857 // Figure out the type byte size (field_type_info.first) and
5858 // alignment (field_type_info.second) from the AST context.
5859 if (bit_offset_ptr) {
5860 const clang::ASTRecordLayout &record_layout =
5861 getASTContext()->getASTRecordLayout(record_decl);
5862 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5863 }
5864
5865 const bool is_bitfield = field->isBitField();
5866
5867 if (bitfield_bit_size_ptr) {
5868 *bitfield_bit_size_ptr = 0;
5869
5870 if (is_bitfield) {
5871 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5872 llvm::APSInt bitfield_apsint;
5873 if (bitfield_bit_size_expr &&
5874 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5875 *getASTContext())) {
5876 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5877 }
5878 }
5879 }
5880 if (is_bitfield_ptr)
5881 *is_bitfield_ptr = is_bitfield;
5882
5883 return CompilerType(getASTContext(), field->getType());
5884 }
5885 }
5886 }
5887 break;
5888
Sean Callananf9c622a2016-09-30 18:44:43 +00005889 case clang::Type::ObjCObjectPointer: {
5890 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005891 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005892 const clang::ObjCInterfaceType *objc_interface_type =
5893 objc_class_type->getInterfaceType();
5894 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005895 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5896 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005897 clang::ObjCInterfaceDecl *class_interface_decl =
5898 objc_interface_type->getDecl();
5899 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005900 return CompilerType(
5901 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5902 idx, name, bit_offset_ptr,
5903 bitfield_bit_size_ptr, is_bitfield_ptr));
5904 }
5905 }
5906 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005907 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005908
5909 case clang::Type::ObjCObject:
5910 case clang::Type::ObjCInterface:
5911 if (GetCompleteType(type)) {
5912 const clang::ObjCObjectType *objc_class_type =
5913 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5914 assert(objc_class_type);
5915 if (objc_class_type) {
5916 clang::ObjCInterfaceDecl *class_interface_decl =
5917 objc_class_type->getInterface();
5918 return CompilerType(
5919 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5920 idx, name, bit_offset_ptr,
5921 bitfield_bit_size_ptr, is_bitfield_ptr));
5922 }
5923 }
5924 break;
5925
5926 case clang::Type::Typedef:
5927 return CompilerType(getASTContext(),
5928 llvm::cast<clang::TypedefType>(qual_type)
5929 ->getDecl()
5930 ->getUnderlyingType())
5931 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5932 is_bitfield_ptr);
5933
5934 case clang::Type::Auto:
5935 return CompilerType(
5936 getASTContext(),
5937 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5938 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5939 is_bitfield_ptr);
5940
5941 case clang::Type::Elaborated:
5942 return CompilerType(
5943 getASTContext(),
5944 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5945 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5946 is_bitfield_ptr);
5947
5948 case clang::Type::Paren:
5949 return CompilerType(getASTContext(),
5950 llvm::cast<clang::ParenType>(qual_type)->desugar())
5951 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5952 is_bitfield_ptr);
5953
5954 default:
5955 break;
5956 }
5957 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005958}
5959
Greg Clayton99558cc42015-08-24 23:46:31 +00005960uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005961ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
5962 uint32_t count = 0;
5963 clang::QualType qual_type(GetCanonicalQualType(type));
5964 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5965 switch (type_class) {
5966 case clang::Type::Record:
5967 if (GetCompleteType(type)) {
5968 const clang::CXXRecordDecl *cxx_record_decl =
5969 qual_type->getAsCXXRecordDecl();
5970 if (cxx_record_decl)
5971 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00005972 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005973 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00005974
Kate Stoneb9c1b512016-09-06 20:57:50 +00005975 case clang::Type::ObjCObjectPointer:
5976 count = GetPointeeType(type).GetNumDirectBaseClasses();
5977 break;
5978
5979 case clang::Type::ObjCObject:
5980 if (GetCompleteType(type)) {
5981 const clang::ObjCObjectType *objc_class_type =
5982 qual_type->getAsObjCQualifiedInterfaceType();
5983 if (objc_class_type) {
5984 clang::ObjCInterfaceDecl *class_interface_decl =
5985 objc_class_type->getInterface();
5986
5987 if (class_interface_decl && class_interface_decl->getSuperClass())
5988 count = 1;
5989 }
5990 }
5991 break;
5992 case clang::Type::ObjCInterface:
5993 if (GetCompleteType(type)) {
5994 const clang::ObjCInterfaceType *objc_interface_type =
5995 qual_type->getAs<clang::ObjCInterfaceType>();
5996 if (objc_interface_type) {
5997 clang::ObjCInterfaceDecl *class_interface_decl =
5998 objc_interface_type->getInterface();
5999
6000 if (class_interface_decl && class_interface_decl->getSuperClass())
6001 count = 1;
6002 }
6003 }
6004 break;
6005
6006 case clang::Type::Typedef:
6007 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6008 ->getDecl()
6009 ->getUnderlyingType()
6010 .getAsOpaquePtr());
6011 break;
6012
6013 case clang::Type::Auto:
6014 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6015 ->getDeducedType()
6016 .getAsOpaquePtr());
6017 break;
6018
6019 case clang::Type::Elaborated:
6020 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6021 ->getNamedType()
6022 .getAsOpaquePtr());
6023 break;
6024
6025 case clang::Type::Paren:
6026 return GetNumDirectBaseClasses(
6027 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6028
6029 default:
6030 break;
6031 }
6032 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006033}
6034
6035uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00006036ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
6037 uint32_t count = 0;
6038 clang::QualType qual_type(GetCanonicalQualType(type));
6039 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6040 switch (type_class) {
6041 case clang::Type::Record:
6042 if (GetCompleteType(type)) {
6043 const clang::CXXRecordDecl *cxx_record_decl =
6044 qual_type->getAsCXXRecordDecl();
6045 if (cxx_record_decl)
6046 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00006047 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006048 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006049
Kate Stoneb9c1b512016-09-06 20:57:50 +00006050 case clang::Type::Typedef:
6051 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6052 ->getDecl()
6053 ->getUnderlyingType()
6054 .getAsOpaquePtr());
6055 break;
6056
6057 case clang::Type::Auto:
6058 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6059 ->getDeducedType()
6060 .getAsOpaquePtr());
6061 break;
6062
6063 case clang::Type::Elaborated:
6064 count =
6065 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6066 ->getNamedType()
6067 .getAsOpaquePtr());
6068 break;
6069
6070 case clang::Type::Paren:
6071 count = GetNumVirtualBaseClasses(
6072 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6073 break;
6074
6075 default:
6076 break;
6077 }
6078 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006079}
6080
Kate Stoneb9c1b512016-09-06 20:57:50 +00006081CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6082 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6083 clang::QualType qual_type(GetCanonicalQualType(type));
6084 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6085 switch (type_class) {
6086 case clang::Type::Record:
6087 if (GetCompleteType(type)) {
6088 const clang::CXXRecordDecl *cxx_record_decl =
6089 qual_type->getAsCXXRecordDecl();
6090 if (cxx_record_decl) {
6091 uint32_t curr_idx = 0;
6092 clang::CXXRecordDecl::base_class_const_iterator base_class,
6093 base_class_end;
6094 for (base_class = cxx_record_decl->bases_begin(),
6095 base_class_end = cxx_record_decl->bases_end();
6096 base_class != base_class_end; ++base_class, ++curr_idx) {
6097 if (curr_idx == idx) {
6098 if (bit_offset_ptr) {
6099 const clang::ASTRecordLayout &record_layout =
6100 getASTContext()->getASTRecordLayout(cxx_record_decl);
6101 const clang::CXXRecordDecl *base_class_decl =
6102 llvm::cast<clang::CXXRecordDecl>(
6103 base_class->getType()
6104 ->getAs<clang::RecordType>()
6105 ->getDecl());
6106 if (base_class->isVirtual())
6107 *bit_offset_ptr =
6108 record_layout.getVBaseClassOffset(base_class_decl)
6109 .getQuantity() *
6110 8;
6111 else
6112 *bit_offset_ptr =
6113 record_layout.getBaseClassOffset(base_class_decl)
6114 .getQuantity() *
6115 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006116 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006117 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6118 }
6119 }
6120 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006121 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006122 break;
6123
6124 case clang::Type::ObjCObjectPointer:
6125 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6126
6127 case clang::Type::ObjCObject:
6128 if (idx == 0 && GetCompleteType(type)) {
6129 const clang::ObjCObjectType *objc_class_type =
6130 qual_type->getAsObjCQualifiedInterfaceType();
6131 if (objc_class_type) {
6132 clang::ObjCInterfaceDecl *class_interface_decl =
6133 objc_class_type->getInterface();
6134
6135 if (class_interface_decl) {
6136 clang::ObjCInterfaceDecl *superclass_interface_decl =
6137 class_interface_decl->getSuperClass();
6138 if (superclass_interface_decl) {
6139 if (bit_offset_ptr)
6140 *bit_offset_ptr = 0;
6141 return CompilerType(getASTContext(),
6142 getASTContext()->getObjCInterfaceType(
6143 superclass_interface_decl));
6144 }
6145 }
6146 }
6147 }
6148 break;
6149 case clang::Type::ObjCInterface:
6150 if (idx == 0 && GetCompleteType(type)) {
6151 const clang::ObjCObjectType *objc_interface_type =
6152 qual_type->getAs<clang::ObjCInterfaceType>();
6153 if (objc_interface_type) {
6154 clang::ObjCInterfaceDecl *class_interface_decl =
6155 objc_interface_type->getInterface();
6156
6157 if (class_interface_decl) {
6158 clang::ObjCInterfaceDecl *superclass_interface_decl =
6159 class_interface_decl->getSuperClass();
6160 if (superclass_interface_decl) {
6161 if (bit_offset_ptr)
6162 *bit_offset_ptr = 0;
6163 return CompilerType(getASTContext(),
6164 getASTContext()->getObjCInterfaceType(
6165 superclass_interface_decl));
6166 }
6167 }
6168 }
6169 }
6170 break;
6171
6172 case clang::Type::Typedef:
6173 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6174 ->getDecl()
6175 ->getUnderlyingType()
6176 .getAsOpaquePtr(),
6177 idx, bit_offset_ptr);
6178
6179 case clang::Type::Auto:
6180 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6181 ->getDeducedType()
6182 .getAsOpaquePtr(),
6183 idx, bit_offset_ptr);
6184
6185 case clang::Type::Elaborated:
6186 return GetDirectBaseClassAtIndex(
6187 llvm::cast<clang::ElaboratedType>(qual_type)
6188 ->getNamedType()
6189 .getAsOpaquePtr(),
6190 idx, bit_offset_ptr);
6191
6192 case clang::Type::Paren:
6193 return GetDirectBaseClassAtIndex(
6194 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6195 idx, bit_offset_ptr);
6196
6197 default:
6198 break;
6199 }
6200 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006201}
6202
Kate Stoneb9c1b512016-09-06 20:57:50 +00006203CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6204 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6205 clang::QualType qual_type(GetCanonicalQualType(type));
6206 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6207 switch (type_class) {
6208 case clang::Type::Record:
6209 if (GetCompleteType(type)) {
6210 const clang::CXXRecordDecl *cxx_record_decl =
6211 qual_type->getAsCXXRecordDecl();
6212 if (cxx_record_decl) {
6213 uint32_t curr_idx = 0;
6214 clang::CXXRecordDecl::base_class_const_iterator base_class,
6215 base_class_end;
6216 for (base_class = cxx_record_decl->vbases_begin(),
6217 base_class_end = cxx_record_decl->vbases_end();
6218 base_class != base_class_end; ++base_class, ++curr_idx) {
6219 if (curr_idx == idx) {
6220 if (bit_offset_ptr) {
6221 const clang::ASTRecordLayout &record_layout =
6222 getASTContext()->getASTRecordLayout(cxx_record_decl);
6223 const clang::CXXRecordDecl *base_class_decl =
6224 llvm::cast<clang::CXXRecordDecl>(
6225 base_class->getType()
6226 ->getAs<clang::RecordType>()
6227 ->getDecl());
6228 *bit_offset_ptr =
6229 record_layout.getVBaseClassOffset(base_class_decl)
6230 .getQuantity() *
6231 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006232 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006233 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6234 }
6235 }
6236 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006237 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006238 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006239
Kate Stoneb9c1b512016-09-06 20:57:50 +00006240 case clang::Type::Typedef:
6241 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6242 ->getDecl()
6243 ->getUnderlyingType()
6244 .getAsOpaquePtr(),
6245 idx, bit_offset_ptr);
6246
6247 case clang::Type::Auto:
6248 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6249 ->getDeducedType()
6250 .getAsOpaquePtr(),
6251 idx, bit_offset_ptr);
6252
6253 case clang::Type::Elaborated:
6254 return GetVirtualBaseClassAtIndex(
6255 llvm::cast<clang::ElaboratedType>(qual_type)
6256 ->getNamedType()
6257 .getAsOpaquePtr(),
6258 idx, bit_offset_ptr);
6259
6260 case clang::Type::Paren:
6261 return GetVirtualBaseClassAtIndex(
6262 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6263 idx, bit_offset_ptr);
6264
6265 default:
6266 break;
6267 }
6268 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006269}
6270
Greg Claytond8d4a572015-08-11 21:38:15 +00006271// If a pointer to a pointee type (the clang_type arg) says that it has no
6272// children, then we either need to trust it, or override it and return a
6273// different result. For example, an "int *" has one child that is an integer,
6274// but a function pointer doesn't have any children. Likewise if a Record type
6275// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006276uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6277 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006278 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006279
6280 clang::QualType qual_type(type.getCanonicalType());
6281 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6282 switch (type_class) {
6283 case clang::Type::Builtin:
6284 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6285 case clang::BuiltinType::UnknownAny:
6286 case clang::BuiltinType::Void:
6287 case clang::BuiltinType::NullPtr:
6288 case clang::BuiltinType::OCLEvent:
6289 case clang::BuiltinType::OCLImage1dRO:
6290 case clang::BuiltinType::OCLImage1dWO:
6291 case clang::BuiltinType::OCLImage1dRW:
6292 case clang::BuiltinType::OCLImage1dArrayRO:
6293 case clang::BuiltinType::OCLImage1dArrayWO:
6294 case clang::BuiltinType::OCLImage1dArrayRW:
6295 case clang::BuiltinType::OCLImage1dBufferRO:
6296 case clang::BuiltinType::OCLImage1dBufferWO:
6297 case clang::BuiltinType::OCLImage1dBufferRW:
6298 case clang::BuiltinType::OCLImage2dRO:
6299 case clang::BuiltinType::OCLImage2dWO:
6300 case clang::BuiltinType::OCLImage2dRW:
6301 case clang::BuiltinType::OCLImage2dArrayRO:
6302 case clang::BuiltinType::OCLImage2dArrayWO:
6303 case clang::BuiltinType::OCLImage2dArrayRW:
6304 case clang::BuiltinType::OCLImage3dRO:
6305 case clang::BuiltinType::OCLImage3dWO:
6306 case clang::BuiltinType::OCLImage3dRW:
6307 case clang::BuiltinType::OCLSampler:
6308 return 0;
6309 case clang::BuiltinType::Bool:
6310 case clang::BuiltinType::Char_U:
6311 case clang::BuiltinType::UChar:
6312 case clang::BuiltinType::WChar_U:
6313 case clang::BuiltinType::Char16:
6314 case clang::BuiltinType::Char32:
6315 case clang::BuiltinType::UShort:
6316 case clang::BuiltinType::UInt:
6317 case clang::BuiltinType::ULong:
6318 case clang::BuiltinType::ULongLong:
6319 case clang::BuiltinType::UInt128:
6320 case clang::BuiltinType::Char_S:
6321 case clang::BuiltinType::SChar:
6322 case clang::BuiltinType::WChar_S:
6323 case clang::BuiltinType::Short:
6324 case clang::BuiltinType::Int:
6325 case clang::BuiltinType::Long:
6326 case clang::BuiltinType::LongLong:
6327 case clang::BuiltinType::Int128:
6328 case clang::BuiltinType::Float:
6329 case clang::BuiltinType::Double:
6330 case clang::BuiltinType::LongDouble:
6331 case clang::BuiltinType::Dependent:
6332 case clang::BuiltinType::Overload:
6333 case clang::BuiltinType::ObjCId:
6334 case clang::BuiltinType::ObjCClass:
6335 case clang::BuiltinType::ObjCSel:
6336 case clang::BuiltinType::BoundMember:
6337 case clang::BuiltinType::Half:
6338 case clang::BuiltinType::ARCUnbridgedCast:
6339 case clang::BuiltinType::PseudoObject:
6340 case clang::BuiltinType::BuiltinFn:
6341 case clang::BuiltinType::OMPArraySection:
6342 return 1;
6343 default:
6344 return 0;
6345 }
6346 break;
6347
6348 case clang::Type::Complex:
6349 return 1;
6350 case clang::Type::Pointer:
6351 return 1;
6352 case clang::Type::BlockPointer:
6353 return 0; // If block pointers don't have debug info, then no children for
6354 // them
6355 case clang::Type::LValueReference:
6356 return 1;
6357 case clang::Type::RValueReference:
6358 return 1;
6359 case clang::Type::MemberPointer:
6360 return 0;
6361 case clang::Type::ConstantArray:
6362 return 0;
6363 case clang::Type::IncompleteArray:
6364 return 0;
6365 case clang::Type::VariableArray:
6366 return 0;
6367 case clang::Type::DependentSizedArray:
6368 return 0;
6369 case clang::Type::DependentSizedExtVector:
6370 return 0;
6371 case clang::Type::Vector:
6372 return 0;
6373 case clang::Type::ExtVector:
6374 return 0;
6375 case clang::Type::FunctionProto:
6376 return 0; // When we function pointers, they have no children...
6377 case clang::Type::FunctionNoProto:
6378 return 0; // When we function pointers, they have no children...
6379 case clang::Type::UnresolvedUsing:
6380 return 0;
6381 case clang::Type::Paren:
6382 return GetNumPointeeChildren(
6383 llvm::cast<clang::ParenType>(qual_type)->desugar());
6384 case clang::Type::Typedef:
6385 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6386 ->getDecl()
6387 ->getUnderlyingType());
6388 case clang::Type::Auto:
6389 return GetNumPointeeChildren(
6390 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6391 case clang::Type::Elaborated:
6392 return GetNumPointeeChildren(
6393 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6394 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006395 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6396 ->getUnderlyingExpr()
6397 ->getType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006398 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006399 return GetNumPointeeChildren(
6400 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006401 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006402 return GetNumPointeeChildren(
6403 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006404 case clang::Type::Record:
6405 return 0;
6406 case clang::Type::Enum:
6407 return 1;
6408 case clang::Type::TemplateTypeParm:
6409 return 1;
6410 case clang::Type::SubstTemplateTypeParm:
6411 return 1;
6412 case clang::Type::TemplateSpecialization:
6413 return 1;
6414 case clang::Type::InjectedClassName:
6415 return 0;
6416 case clang::Type::DependentName:
6417 return 1;
6418 case clang::Type::DependentTemplateSpecialization:
6419 return 1;
6420 case clang::Type::ObjCObject:
6421 return 0;
6422 case clang::Type::ObjCInterface:
6423 return 0;
6424 case clang::Type::ObjCObjectPointer:
6425 return 1;
6426 default:
6427 break;
6428 }
6429 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006430}
6431
Kate Stoneb9c1b512016-09-06 20:57:50 +00006432CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6433 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6434 bool transparent_pointers, bool omit_empty_base_classes,
6435 bool ignore_array_bounds, std::string &child_name,
6436 uint32_t &child_byte_size, int32_t &child_byte_offset,
6437 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6438 bool &child_is_base_class, bool &child_is_deref_of_parent,
6439 ValueObject *valobj, uint64_t &language_flags) {
6440 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006441 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006442
Kate Stoneb9c1b512016-09-06 20:57:50 +00006443 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6444 const clang::Type::TypeClass parent_type_class =
6445 parent_qual_type->getTypeClass();
6446 child_bitfield_bit_size = 0;
6447 child_bitfield_bit_offset = 0;
6448 child_is_base_class = false;
6449 language_flags = 0;
6450
6451 const bool idx_is_valid = idx < GetNumChildren(type, omit_empty_base_classes);
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006452 int32_t bit_offset;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006453 switch (parent_type_class) {
6454 case clang::Type::Builtin:
6455 if (idx_is_valid) {
6456 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6457 case clang::BuiltinType::ObjCId:
6458 case clang::BuiltinType::ObjCClass:
6459 child_name = "isa";
6460 child_byte_size =
6461 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6462 CHAR_BIT;
6463 return CompilerType(getASTContext(),
6464 getASTContext()->ObjCBuiltinClassTy);
6465
6466 default:
6467 break;
6468 }
6469 }
6470 break;
6471
6472 case clang::Type::Record:
6473 if (idx_is_valid && GetCompleteType(type)) {
6474 const clang::RecordType *record_type =
6475 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6476 const clang::RecordDecl *record_decl = record_type->getDecl();
6477 assert(record_decl);
6478 const clang::ASTRecordLayout &record_layout =
6479 getASTContext()->getASTRecordLayout(record_decl);
6480 uint32_t child_idx = 0;
6481
6482 const clang::CXXRecordDecl *cxx_record_decl =
6483 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6484 if (cxx_record_decl) {
6485 // We might have base classes to print out first
6486 clang::CXXRecordDecl::base_class_const_iterator base_class,
6487 base_class_end;
6488 for (base_class = cxx_record_decl->bases_begin(),
6489 base_class_end = cxx_record_decl->bases_end();
6490 base_class != base_class_end; ++base_class) {
6491 const clang::CXXRecordDecl *base_class_decl = nullptr;
6492
6493 // Skip empty base classes
6494 if (omit_empty_base_classes) {
6495 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6496 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6497 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
6498 continue;
6499 }
6500
6501 if (idx == child_idx) {
6502 if (base_class_decl == nullptr)
6503 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6504 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6505
6506 if (base_class->isVirtual()) {
6507 bool handled = false;
6508 if (valobj) {
Zachary Turner97206d52017-05-12 04:51:55 +00006509 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006510 AddressType addr_type = eAddressTypeInvalid;
6511 lldb::addr_t vtable_ptr_addr =
6512 valobj->GetCPPVTableAddress(addr_type);
6513
6514 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS &&
6515 addr_type == eAddressTypeLoad) {
6516
6517 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
6518 Process *process = exe_ctx.GetProcessPtr();
6519 if (process) {
6520 clang::VTableContextBase *vtable_ctx =
6521 getASTContext()->getVTableContext();
6522 if (vtable_ctx) {
6523 if (vtable_ctx->isMicrosoft()) {
6524 clang::MicrosoftVTableContext *msoft_vtable_ctx =
6525 static_cast<clang::MicrosoftVTableContext *>(
6526 vtable_ctx);
6527
6528 if (vtable_ptr_addr) {
6529 const lldb::addr_t vbtable_ptr_addr =
6530 vtable_ptr_addr +
6531 record_layout.getVBPtrOffset().getQuantity();
6532
6533 const lldb::addr_t vbtable_ptr =
6534 process->ReadPointerFromMemory(vbtable_ptr_addr,
6535 err);
6536 if (vbtable_ptr != LLDB_INVALID_ADDRESS) {
6537 // Get the index into the virtual base table. The
6538 // index is the index in uint32_t from vbtable_ptr
6539 const unsigned vbtable_index =
6540 msoft_vtable_ctx->getVBTableIndex(
6541 cxx_record_decl, base_class_decl);
6542 const lldb::addr_t base_offset_addr =
6543 vbtable_ptr + vbtable_index * 4;
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006544 const int32_t base_offset =
6545 process->ReadSignedIntegerFromMemory(
Kate Stoneb9c1b512016-09-06 20:57:50 +00006546 base_offset_addr, 4, UINT32_MAX, err);
6547 if (base_offset != UINT32_MAX) {
6548 handled = true;
6549 bit_offset = base_offset * 8;
6550 }
6551 }
6552 }
6553 } else {
6554 clang::ItaniumVTableContext *itanium_vtable_ctx =
6555 static_cast<clang::ItaniumVTableContext *>(
6556 vtable_ctx);
6557 if (vtable_ptr_addr) {
6558 const lldb::addr_t vtable_ptr =
6559 process->ReadPointerFromMemory(vtable_ptr_addr,
6560 err);
6561 if (vtable_ptr != LLDB_INVALID_ADDRESS) {
6562 clang::CharUnits base_offset_offset =
6563 itanium_vtable_ctx->getVirtualBaseOffsetOffset(
6564 cxx_record_decl, base_class_decl);
6565 const lldb::addr_t base_offset_addr =
6566 vtable_ptr + base_offset_offset.getQuantity();
6567 const uint32_t base_offset_size =
6568 process->GetAddressByteSize();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00006569 const int64_t base_offset =
6570 process->ReadSignedIntegerFromMemory(
Kate Stoneb9c1b512016-09-06 20:57:50 +00006571 base_offset_addr, base_offset_size,
6572 UINT32_MAX, err);
6573 if (base_offset < UINT32_MAX) {
6574 handled = true;
6575 bit_offset = base_offset * 8;
6576 }
6577 }
6578 }
6579 }
6580 }
6581 }
6582 }
6583 }
6584 if (!handled)
6585 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6586 .getQuantity() *
6587 8;
6588 } else
6589 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6590 .getQuantity() *
6591 8;
6592
6593 // Base classes should be a multiple of 8 bits in size
6594 child_byte_offset = bit_offset / 8;
6595 CompilerType base_class_clang_type(getASTContext(),
6596 base_class->getType());
6597 child_name = base_class_clang_type.GetTypeName().AsCString("");
6598 uint64_t base_class_clang_type_bit_size =
6599 base_class_clang_type.GetBitSize(
6600 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6601
6602 // Base classes bit sizes should be a multiple of 8 bits in size
6603 assert(base_class_clang_type_bit_size % 8 == 0);
6604 child_byte_size = base_class_clang_type_bit_size / 8;
6605 child_is_base_class = true;
6606 return base_class_clang_type;
6607 }
6608 // We don't increment the child index in the for loop since we might
6609 // be skipping empty base classes
6610 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006611 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006612 }
6613 // Make sure index is in range...
6614 uint32_t field_idx = 0;
6615 clang::RecordDecl::field_iterator field, field_end;
6616 for (field = record_decl->field_begin(),
6617 field_end = record_decl->field_end();
6618 field != field_end; ++field, ++field_idx, ++child_idx) {
6619 if (idx == child_idx) {
6620 // Print the member type if requested
6621 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006622 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006623
6624 // Figure out the type byte size (field_type_info.first) and
6625 // alignment (field_type_info.second) from the AST context.
6626 CompilerType field_clang_type(getASTContext(), field->getType());
6627 assert(field_idx < record_layout.getFieldCount());
6628 child_byte_size = field_clang_type.GetByteSize(
6629 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6630 const uint32_t child_bit_size = child_byte_size * 8;
6631
6632 // Figure out the field offset within the current struct/union/class
6633 // type
6634 bit_offset = record_layout.getFieldOffset(field_idx);
6635 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6636 child_bitfield_bit_size)) {
6637 child_bitfield_bit_offset = bit_offset % child_bit_size;
6638 const uint32_t child_bit_offset =
6639 bit_offset - child_bitfield_bit_offset;
6640 child_byte_offset = child_bit_offset / 8;
6641 } else {
6642 child_byte_offset = bit_offset / 8;
6643 }
6644
6645 return field_clang_type;
6646 }
6647 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006648 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006649 break;
6650
6651 case clang::Type::ObjCObject:
6652 case clang::Type::ObjCInterface:
6653 if (idx_is_valid && GetCompleteType(type)) {
6654 const clang::ObjCObjectType *objc_class_type =
6655 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6656 assert(objc_class_type);
6657 if (objc_class_type) {
6658 uint32_t child_idx = 0;
6659 clang::ObjCInterfaceDecl *class_interface_decl =
6660 objc_class_type->getInterface();
6661
6662 if (class_interface_decl) {
6663
6664 const clang::ASTRecordLayout &interface_layout =
6665 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6666 clang::ObjCInterfaceDecl *superclass_interface_decl =
6667 class_interface_decl->getSuperClass();
6668 if (superclass_interface_decl) {
6669 if (omit_empty_base_classes) {
6670 CompilerType base_class_clang_type(
6671 getASTContext(), getASTContext()->getObjCInterfaceType(
6672 superclass_interface_decl));
6673 if (base_class_clang_type.GetNumChildren(
6674 omit_empty_base_classes) > 0) {
6675 if (idx == 0) {
6676 clang::QualType ivar_qual_type(
6677 getASTContext()->getObjCInterfaceType(
6678 superclass_interface_decl));
6679
6680 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006681 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006682
6683 clang::TypeInfo ivar_type_info =
6684 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6685
6686 child_byte_size = ivar_type_info.Width / 8;
6687 child_byte_offset = 0;
6688 child_is_base_class = true;
6689
6690 return CompilerType(getASTContext(), ivar_qual_type);
6691 }
6692
6693 ++child_idx;
6694 }
6695 } else
6696 ++child_idx;
6697 }
6698
6699 const uint32_t superclass_idx = child_idx;
6700
6701 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6702 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6703 ivar_end = class_interface_decl->ivar_end();
6704
6705 for (ivar_pos = class_interface_decl->ivar_begin();
6706 ivar_pos != ivar_end; ++ivar_pos) {
6707 if (child_idx == idx) {
6708 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6709
6710 clang::QualType ivar_qual_type(ivar_decl->getType());
6711
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006712 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006713
6714 clang::TypeInfo ivar_type_info =
6715 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6716
6717 child_byte_size = ivar_type_info.Width / 8;
6718
6719 // Figure out the field offset within the current
Adrian Prantl05097242018-04-30 16:49:04 +00006720 // struct/union/class type For ObjC objects, we can't trust the
6721 // bit offset we get from the Clang AST, since that doesn't
6722 // account for the space taken up by unbacked properties, or
6723 // from the changing size of base classes that are newer than
6724 // this class. So if we have a process around that we can ask
6725 // about this object, do so.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006726 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6727 Process *process = nullptr;
6728 if (exe_ctx)
6729 process = exe_ctx->GetProcessPtr();
6730 if (process) {
6731 ObjCLanguageRuntime *objc_runtime =
6732 process->GetObjCLanguageRuntime();
6733 if (objc_runtime != nullptr) {
6734 CompilerType parent_ast_type(getASTContext(),
6735 parent_qual_type);
6736 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6737 parent_ast_type, ivar_decl->getNameAsString().c_str());
6738 }
6739 }
6740
6741 // Setting this to UINT32_MAX to make sure we don't compute it
6742 // twice...
6743 bit_offset = UINT32_MAX;
6744
6745 if (child_byte_offset ==
6746 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6747 bit_offset = interface_layout.getFieldOffset(child_idx -
6748 superclass_idx);
6749 child_byte_offset = bit_offset / 8;
6750 }
6751
6752 // Note, the ObjC Ivar Byte offset is just that, it doesn't
Adrian Prantl05097242018-04-30 16:49:04 +00006753 // account for the bit offset of a bitfield within its
6754 // containing object. So regardless of where we get the byte
Kate Stoneb9c1b512016-09-06 20:57:50 +00006755 // offset from, we still need to get the bit offset for
6756 // bitfields from the layout.
6757
6758 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6759 child_bitfield_bit_size)) {
6760 if (bit_offset == UINT32_MAX)
6761 bit_offset = interface_layout.getFieldOffset(
6762 child_idx - superclass_idx);
6763
6764 child_bitfield_bit_offset = bit_offset % 8;
6765 }
6766 return CompilerType(getASTContext(), ivar_qual_type);
6767 }
6768 ++child_idx;
6769 }
6770 }
6771 }
6772 }
6773 }
6774 break;
6775
6776 case clang::Type::ObjCObjectPointer:
6777 if (idx_is_valid) {
6778 CompilerType pointee_clang_type(GetPointeeType(type));
6779
6780 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6781 child_is_deref_of_parent = false;
6782 bool tmp_child_is_deref_of_parent = false;
6783 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6784 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6785 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6786 child_bitfield_bit_size, child_bitfield_bit_offset,
6787 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6788 language_flags);
6789 } else {
6790 child_is_deref_of_parent = true;
6791 const char *parent_name =
6792 valobj ? valobj->GetName().GetCString() : NULL;
6793 if (parent_name) {
6794 child_name.assign(1, '*');
6795 child_name += parent_name;
6796 }
6797
6798 // We have a pointer to an simple type
6799 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
6800 child_byte_size = pointee_clang_type.GetByteSize(
6801 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6802 child_byte_offset = 0;
6803 return pointee_clang_type;
6804 }
6805 }
6806 }
6807 break;
6808
6809 case clang::Type::Vector:
6810 case clang::Type::ExtVector:
6811 if (idx_is_valid) {
6812 const clang::VectorType *array =
6813 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6814 if (array) {
6815 CompilerType element_type(getASTContext(), array->getElementType());
6816 if (element_type.GetCompleteType()) {
6817 char element_name[64];
6818 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6819 static_cast<uint64_t>(idx));
6820 child_name.assign(element_name);
6821 child_byte_size = element_type.GetByteSize(
6822 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6823 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6824 return element_type;
6825 }
6826 }
6827 }
6828 break;
6829
6830 case clang::Type::ConstantArray:
6831 case clang::Type::IncompleteArray:
6832 if (ignore_array_bounds || idx_is_valid) {
6833 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6834 if (array) {
6835 CompilerType element_type(getASTContext(), array->getElementType());
6836 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006837 child_name = llvm::formatv("[{0}]", idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006838 child_byte_size = element_type.GetByteSize(
6839 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6840 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6841 return element_type;
6842 }
6843 }
6844 }
6845 break;
6846
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006847 case clang::Type::Pointer: {
6848 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006849
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006850 // Don't dereference "void *" pointers
6851 if (pointee_clang_type.IsVoidType())
6852 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006853
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006854 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6855 child_is_deref_of_parent = false;
6856 bool tmp_child_is_deref_of_parent = false;
6857 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6858 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6859 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6860 child_bitfield_bit_size, child_bitfield_bit_offset,
6861 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6862 language_flags);
6863 } else {
6864 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006865
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006866 const char *parent_name =
6867 valobj ? valobj->GetName().GetCString() : NULL;
6868 if (parent_name) {
6869 child_name.assign(1, '*');
6870 child_name += parent_name;
6871 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006872
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006873 // We have a pointer to an simple type
6874 if (idx == 0) {
6875 child_byte_size = pointee_clang_type.GetByteSize(
6876 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6877 child_byte_offset = 0;
6878 return pointee_clang_type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006879 }
6880 }
6881 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006882 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006883
6884 case clang::Type::LValueReference:
6885 case clang::Type::RValueReference:
6886 if (idx_is_valid) {
6887 const clang::ReferenceType *reference_type =
6888 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6889 CompilerType pointee_clang_type(getASTContext(),
6890 reference_type->getPointeeType());
6891 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6892 child_is_deref_of_parent = false;
6893 bool tmp_child_is_deref_of_parent = false;
6894 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6895 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6896 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6897 child_bitfield_bit_size, child_bitfield_bit_offset,
6898 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6899 language_flags);
6900 } else {
6901 const char *parent_name =
6902 valobj ? valobj->GetName().GetCString() : NULL;
6903 if (parent_name) {
6904 child_name.assign(1, '&');
6905 child_name += parent_name;
6906 }
6907
6908 // We have a pointer to an simple type
6909 if (idx == 0) {
6910 child_byte_size = pointee_clang_type.GetByteSize(
6911 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6912 child_byte_offset = 0;
6913 return pointee_clang_type;
6914 }
6915 }
6916 }
6917 break;
6918
6919 case clang::Type::Typedef: {
6920 CompilerType typedefed_clang_type(
6921 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
6922 ->getDecl()
6923 ->getUnderlyingType());
6924 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
6925 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6926 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6927 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6928 child_is_deref_of_parent, valobj, language_flags);
6929 } break;
6930
6931 case clang::Type::Auto: {
6932 CompilerType elaborated_clang_type(
6933 getASTContext(),
6934 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
6935 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6936 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6937 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6938 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6939 child_is_deref_of_parent, valobj, language_flags);
6940 }
6941
6942 case clang::Type::Elaborated: {
6943 CompilerType elaborated_clang_type(
6944 getASTContext(),
6945 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
6946 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6947 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6948 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6949 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6950 child_is_deref_of_parent, valobj, language_flags);
6951 }
6952
6953 case clang::Type::Paren: {
6954 CompilerType paren_clang_type(
6955 getASTContext(),
6956 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
6957 return paren_clang_type.GetChildCompilerTypeAtIndex(
6958 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6959 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6960 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6961 child_is_deref_of_parent, valobj, language_flags);
6962 }
6963
6964 default:
6965 break;
6966 }
6967 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006968}
6969
Kate Stoneb9c1b512016-09-06 20:57:50 +00006970static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
6971 const clang::CXXBaseSpecifier *base_spec,
6972 bool omit_empty_base_classes) {
6973 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006974
Kate Stoneb9c1b512016-09-06 20:57:50 +00006975 const clang::CXXRecordDecl *cxx_record_decl =
6976 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6977
6978 // const char *super_name = record_decl->getNameAsCString();
6979 // const char *base_name =
6980 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6981 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6982 //
6983 if (cxx_record_decl) {
6984 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6985 for (base_class = cxx_record_decl->bases_begin(),
6986 base_class_end = cxx_record_decl->bases_end();
6987 base_class != base_class_end; ++base_class) {
6988 if (omit_empty_base_classes) {
6989 if (BaseSpecifierIsEmpty(base_class))
6990 continue;
6991 }
6992
6993 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
6994 // super_name, base_name,
6995 // child_idx,
6996 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6997 //
6998 //
6999 if (base_class == base_spec)
7000 return child_idx;
7001 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00007002 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007003 }
7004
7005 return UINT32_MAX;
7006}
7007
7008static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
7009 clang::NamedDecl *canonical_decl,
7010 bool omit_empty_base_classes) {
7011 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
7012 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
7013 omit_empty_base_classes);
7014
7015 clang::RecordDecl::field_iterator field, field_end;
7016 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
7017 field != field_end; ++field, ++child_idx) {
7018 if (field->getCanonicalDecl() == canonical_decl)
7019 return child_idx;
7020 }
7021
7022 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00007023}
7024
7025// Look for a child member (doesn't include base classes, but it does include
Adrian Prantl05097242018-04-30 16:49:04 +00007026// their members) in the type hierarchy. Returns an index path into
7027// "clang_type" on how to reach the appropriate member.
Greg Claytond8d4a572015-08-11 21:38:15 +00007028//
7029// class A
7030// {
7031// public:
7032// int m_a;
7033// int m_b;
7034// };
7035//
7036// class B
7037// {
7038// };
7039//
7040// class C :
7041// public B,
7042// public A
7043// {
7044// };
7045//
7046// If we have a clang type that describes "class C", and we wanted to looked
7047// "m_b" in it:
7048//
Kate Stoneb9c1b512016-09-06 20:57:50 +00007049// With omit_empty_base_classes == false we would get an integer array back
Adrian Prantl05097242018-04-30 16:49:04 +00007050// with: { 1, 1 } The first index 1 is the child index for "class A" within
7051// class C The second index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007052//
Adrian Prantl05097242018-04-30 16:49:04 +00007053// With omit_empty_base_classes == true we would get an integer array back
7054// with: { 0, 1 } The first index 0 is the child index for "class A" within
7055// class C (since class B doesn't have any members it doesn't count) The second
7056// index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00007057
Kate Stoneb9c1b512016-09-06 20:57:50 +00007058size_t ClangASTContext::GetIndexOfChildMemberWithName(
7059 lldb::opaque_compiler_type_t type, const char *name,
7060 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
7061 if (type && name && name[0]) {
7062 clang::QualType qual_type(GetCanonicalQualType(type));
7063 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7064 switch (type_class) {
7065 case clang::Type::Record:
7066 if (GetCompleteType(type)) {
7067 const clang::RecordType *record_type =
7068 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7069 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00007070
Kate Stoneb9c1b512016-09-06 20:57:50 +00007071 assert(record_decl);
7072 uint32_t child_idx = 0;
7073
7074 const clang::CXXRecordDecl *cxx_record_decl =
7075 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7076
7077 // Try and find a field that matches NAME
7078 clang::RecordDecl::field_iterator field, field_end;
7079 llvm::StringRef name_sref(name);
7080 for (field = record_decl->field_begin(),
7081 field_end = record_decl->field_end();
7082 field != field_end; ++field, ++child_idx) {
7083 llvm::StringRef field_name = field->getName();
7084 if (field_name.empty()) {
7085 CompilerType field_type(getASTContext(), field->getType());
7086 child_indexes.push_back(child_idx);
7087 if (field_type.GetIndexOfChildMemberWithName(
7088 name, omit_empty_base_classes, child_indexes))
7089 return child_indexes.size();
7090 child_indexes.pop_back();
7091
7092 } else if (field_name.equals(name_sref)) {
7093 // We have to add on the number of base classes to this index!
7094 child_indexes.push_back(
7095 child_idx + ClangASTContext::GetNumBaseClasses(
7096 cxx_record_decl, omit_empty_base_classes));
7097 return child_indexes.size();
7098 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007099 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007100
Kate Stoneb9c1b512016-09-06 20:57:50 +00007101 if (cxx_record_decl) {
7102 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7103
7104 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7105
7106 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7107 // Didn't find things easily, lets let clang do its thang...
7108 clang::IdentifierInfo &ident_ref =
7109 getASTContext()->Idents.get(name_sref);
7110 clang::DeclarationName decl_name(&ident_ref);
7111
7112 clang::CXXBasePaths paths;
7113 if (cxx_record_decl->lookupInBases(
7114 [decl_name](const clang::CXXBaseSpecifier *specifier,
7115 clang::CXXBasePath &path) {
7116 return clang::CXXRecordDecl::FindOrdinaryMember(
7117 specifier, path, decl_name);
7118 },
7119 paths)) {
7120 clang::CXXBasePaths::const_paths_iterator path,
7121 path_end = paths.end();
7122 for (path = paths.begin(); path != path_end; ++path) {
7123 const size_t num_path_elements = path->size();
7124 for (size_t e = 0; e < num_path_elements; ++e) {
7125 clang::CXXBasePathElement elem = (*path)[e];
7126
7127 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7128 omit_empty_base_classes);
7129 if (child_idx == UINT32_MAX) {
7130 child_indexes.clear();
7131 return 0;
7132 } else {
7133 child_indexes.push_back(child_idx);
7134 parent_record_decl = llvm::cast<clang::RecordDecl>(
7135 elem.Base->getType()
7136 ->getAs<clang::RecordType>()
7137 ->getDecl());
7138 }
7139 }
7140 for (clang::NamedDecl *path_decl : path->Decls) {
7141 child_idx = GetIndexForRecordChild(
7142 parent_record_decl, path_decl, omit_empty_base_classes);
7143 if (child_idx == UINT32_MAX) {
7144 child_indexes.clear();
7145 return 0;
7146 } else {
7147 child_indexes.push_back(child_idx);
7148 }
7149 }
7150 }
7151 return child_indexes.size();
7152 }
7153 }
7154 }
7155 break;
7156
7157 case clang::Type::ObjCObject:
7158 case clang::Type::ObjCInterface:
7159 if (GetCompleteType(type)) {
7160 llvm::StringRef name_sref(name);
7161 const clang::ObjCObjectType *objc_class_type =
7162 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7163 assert(objc_class_type);
7164 if (objc_class_type) {
7165 uint32_t child_idx = 0;
7166 clang::ObjCInterfaceDecl *class_interface_decl =
7167 objc_class_type->getInterface();
7168
7169 if (class_interface_decl) {
7170 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7171 ivar_end = class_interface_decl->ivar_end();
7172 clang::ObjCInterfaceDecl *superclass_interface_decl =
7173 class_interface_decl->getSuperClass();
7174
7175 for (ivar_pos = class_interface_decl->ivar_begin();
7176 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7177 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7178
7179 if (ivar_decl->getName().equals(name_sref)) {
7180 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7181 (omit_empty_base_classes &&
7182 ObjCDeclHasIVars(superclass_interface_decl, true)))
7183 ++child_idx;
7184
7185 child_indexes.push_back(child_idx);
7186 return child_indexes.size();
7187 }
7188 }
7189
7190 if (superclass_interface_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00007191 // The super class index is always zero for ObjC classes, so we
7192 // push it onto the child indexes in case we find an ivar in our
7193 // superclass...
Kate Stoneb9c1b512016-09-06 20:57:50 +00007194 child_indexes.push_back(0);
7195
7196 CompilerType superclass_clang_type(
7197 getASTContext(), getASTContext()->getObjCInterfaceType(
7198 superclass_interface_decl));
7199 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7200 name, omit_empty_base_classes, child_indexes)) {
Adrian Prantl05097242018-04-30 16:49:04 +00007201 // We did find an ivar in a superclass so just return the
7202 // results!
Kate Stoneb9c1b512016-09-06 20:57:50 +00007203 return child_indexes.size();
7204 }
7205
Adrian Prantl05097242018-04-30 16:49:04 +00007206 // We didn't find an ivar matching "name" in our superclass, pop
7207 // the superclass zero index that we pushed on above.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007208 child_indexes.pop_back();
7209 }
7210 }
7211 }
7212 }
7213 break;
7214
7215 case clang::Type::ObjCObjectPointer: {
7216 CompilerType objc_object_clang_type(
7217 getASTContext(),
7218 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7219 ->getPointeeType());
7220 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7221 name, omit_empty_base_classes, child_indexes);
7222 } break;
7223
7224 case clang::Type::ConstantArray: {
7225 // const clang::ConstantArrayType *array =
7226 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7227 // const uint64_t element_count =
7228 // array->getSize().getLimitedValue();
7229 //
7230 // if (idx < element_count)
7231 // {
7232 // std::pair<uint64_t, unsigned> field_type_info =
7233 // ast->getTypeInfo(array->getElementType());
7234 //
7235 // char element_name[32];
7236 // ::snprintf (element_name, sizeof (element_name),
7237 // "%s[%u]", parent_name ? parent_name : "", idx);
7238 //
7239 // child_name.assign(element_name);
7240 // assert(field_type_info.first % 8 == 0);
7241 // child_byte_size = field_type_info.first / 8;
7242 // child_byte_offset = idx * child_byte_size;
7243 // return array->getElementType().getAsOpaquePtr();
7244 // }
7245 } break;
7246
7247 // case clang::Type::MemberPointerType:
7248 // {
7249 // MemberPointerType *mem_ptr_type =
7250 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7251 // clang::QualType pointee_type =
7252 // mem_ptr_type->getPointeeType();
7253 //
7254 // if (ClangASTContext::IsAggregateType
7255 // (pointee_type.getAsOpaquePtr()))
7256 // {
7257 // return GetIndexOfChildWithName (ast,
7258 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7259 // name);
7260 // }
7261 // }
7262 // break;
7263 //
7264 case clang::Type::LValueReference:
7265 case clang::Type::RValueReference: {
7266 const clang::ReferenceType *reference_type =
7267 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7268 clang::QualType pointee_type(reference_type->getPointeeType());
7269 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7270
7271 if (pointee_clang_type.IsAggregateType()) {
7272 return pointee_clang_type.GetIndexOfChildMemberWithName(
7273 name, omit_empty_base_classes, child_indexes);
7274 }
7275 } break;
7276
7277 case clang::Type::Pointer: {
7278 CompilerType pointee_clang_type(GetPointeeType(type));
7279
7280 if (pointee_clang_type.IsAggregateType()) {
7281 return pointee_clang_type.GetIndexOfChildMemberWithName(
7282 name, omit_empty_base_classes, child_indexes);
7283 }
7284 } break;
7285
7286 case clang::Type::Typedef:
7287 return CompilerType(getASTContext(),
7288 llvm::cast<clang::TypedefType>(qual_type)
7289 ->getDecl()
7290 ->getUnderlyingType())
7291 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7292 child_indexes);
7293
7294 case clang::Type::Auto:
7295 return CompilerType(
7296 getASTContext(),
7297 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7298 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7299 child_indexes);
7300
7301 case clang::Type::Elaborated:
7302 return CompilerType(
7303 getASTContext(),
7304 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7305 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7306 child_indexes);
7307
7308 case clang::Type::Paren:
7309 return CompilerType(getASTContext(),
7310 llvm::cast<clang::ParenType>(qual_type)->desugar())
7311 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7312 child_indexes);
7313
7314 default:
7315 break;
7316 }
7317 }
7318 return 0;
7319}
Greg Claytond8d4a572015-08-11 21:38:15 +00007320
7321// Get the index of the child of "clang_type" whose name matches. This function
7322// doesn't descend into the children, but only looks one level deep and name
7323// matches can include base class names.
7324
7325uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007326ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7327 const char *name,
7328 bool omit_empty_base_classes) {
7329 if (type && name && name[0]) {
7330 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007331
Kate Stoneb9c1b512016-09-06 20:57:50 +00007332 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7333
7334 switch (type_class) {
7335 case clang::Type::Record:
7336 if (GetCompleteType(type)) {
7337 const clang::RecordType *record_type =
7338 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7339 const clang::RecordDecl *record_decl = record_type->getDecl();
7340
7341 assert(record_decl);
7342 uint32_t child_idx = 0;
7343
7344 const clang::CXXRecordDecl *cxx_record_decl =
7345 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7346
7347 if (cxx_record_decl) {
7348 clang::CXXRecordDecl::base_class_const_iterator base_class,
7349 base_class_end;
7350 for (base_class = cxx_record_decl->bases_begin(),
7351 base_class_end = cxx_record_decl->bases_end();
7352 base_class != base_class_end; ++base_class) {
7353 // Skip empty base classes
7354 clang::CXXRecordDecl *base_class_decl =
7355 llvm::cast<clang::CXXRecordDecl>(
7356 base_class->getType()
7357 ->getAs<clang::RecordType>()
7358 ->getDecl());
7359 if (omit_empty_base_classes &&
7360 ClangASTContext::RecordHasFields(base_class_decl) == false)
7361 continue;
7362
7363 CompilerType base_class_clang_type(getASTContext(),
7364 base_class->getType());
7365 std::string base_class_type_name(
7366 base_class_clang_type.GetTypeName().AsCString(""));
7367 if (base_class_type_name.compare(name) == 0)
7368 return child_idx;
7369 ++child_idx;
7370 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007371 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007372
Kate Stoneb9c1b512016-09-06 20:57:50 +00007373 // Try and find a field that matches NAME
7374 clang::RecordDecl::field_iterator field, field_end;
7375 llvm::StringRef name_sref(name);
7376 for (field = record_decl->field_begin(),
7377 field_end = record_decl->field_end();
7378 field != field_end; ++field, ++child_idx) {
7379 if (field->getName().equals(name_sref))
7380 return child_idx;
7381 }
7382 }
7383 break;
7384
7385 case clang::Type::ObjCObject:
7386 case clang::Type::ObjCInterface:
7387 if (GetCompleteType(type)) {
7388 llvm::StringRef name_sref(name);
7389 const clang::ObjCObjectType *objc_class_type =
7390 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7391 assert(objc_class_type);
7392 if (objc_class_type) {
7393 uint32_t child_idx = 0;
7394 clang::ObjCInterfaceDecl *class_interface_decl =
7395 objc_class_type->getInterface();
7396
7397 if (class_interface_decl) {
7398 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7399 ivar_end = class_interface_decl->ivar_end();
7400 clang::ObjCInterfaceDecl *superclass_interface_decl =
7401 class_interface_decl->getSuperClass();
7402
7403 for (ivar_pos = class_interface_decl->ivar_begin();
7404 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7405 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7406
7407 if (ivar_decl->getName().equals(name_sref)) {
7408 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7409 (omit_empty_base_classes &&
7410 ObjCDeclHasIVars(superclass_interface_decl, true)))
7411 ++child_idx;
7412
7413 return child_idx;
7414 }
7415 }
7416
7417 if (superclass_interface_decl) {
7418 if (superclass_interface_decl->getName().equals(name_sref))
7419 return 0;
7420 }
7421 }
7422 }
7423 }
7424 break;
7425
7426 case clang::Type::ObjCObjectPointer: {
7427 CompilerType pointee_clang_type(
7428 getASTContext(),
7429 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7430 ->getPointeeType());
7431 return pointee_clang_type.GetIndexOfChildWithName(
7432 name, omit_empty_base_classes);
7433 } break;
7434
7435 case clang::Type::ConstantArray: {
7436 // const clang::ConstantArrayType *array =
7437 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7438 // const uint64_t element_count =
7439 // array->getSize().getLimitedValue();
7440 //
7441 // if (idx < element_count)
7442 // {
7443 // std::pair<uint64_t, unsigned> field_type_info =
7444 // ast->getTypeInfo(array->getElementType());
7445 //
7446 // char element_name[32];
7447 // ::snprintf (element_name, sizeof (element_name),
7448 // "%s[%u]", parent_name ? parent_name : "", idx);
7449 //
7450 // child_name.assign(element_name);
7451 // assert(field_type_info.first % 8 == 0);
7452 // child_byte_size = field_type_info.first / 8;
7453 // child_byte_offset = idx * child_byte_size;
7454 // return array->getElementType().getAsOpaquePtr();
7455 // }
7456 } break;
7457
7458 // case clang::Type::MemberPointerType:
7459 // {
7460 // MemberPointerType *mem_ptr_type =
7461 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7462 // clang::QualType pointee_type =
7463 // mem_ptr_type->getPointeeType();
7464 //
7465 // if (ClangASTContext::IsAggregateType
7466 // (pointee_type.getAsOpaquePtr()))
7467 // {
7468 // return GetIndexOfChildWithName (ast,
7469 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7470 // name);
7471 // }
7472 // }
7473 // break;
7474 //
7475 case clang::Type::LValueReference:
7476 case clang::Type::RValueReference: {
7477 const clang::ReferenceType *reference_type =
7478 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7479 CompilerType pointee_type(getASTContext(),
7480 reference_type->getPointeeType());
7481
7482 if (pointee_type.IsAggregateType()) {
7483 return pointee_type.GetIndexOfChildWithName(name,
7484 omit_empty_base_classes);
7485 }
7486 } break;
7487
7488 case clang::Type::Pointer: {
7489 const clang::PointerType *pointer_type =
7490 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7491 CompilerType pointee_type(getASTContext(),
7492 pointer_type->getPointeeType());
7493
7494 if (pointee_type.IsAggregateType()) {
7495 return pointee_type.GetIndexOfChildWithName(name,
7496 omit_empty_base_classes);
7497 } else {
7498 // if (parent_name)
7499 // {
7500 // child_name.assign(1, '*');
7501 // child_name += parent_name;
7502 // }
7503 //
7504 // // We have a pointer to an simple type
7505 // if (idx == 0)
7506 // {
7507 // std::pair<uint64_t, unsigned> clang_type_info
7508 // = ast->getTypeInfo(pointee_type);
7509 // assert(clang_type_info.first % 8 == 0);
7510 // child_byte_size = clang_type_info.first / 8;
7511 // child_byte_offset = 0;
7512 // return pointee_type.getAsOpaquePtr();
7513 // }
7514 }
7515 } break;
7516
7517 case clang::Type::Auto:
7518 return CompilerType(
7519 getASTContext(),
7520 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7521 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7522
7523 case clang::Type::Elaborated:
7524 return CompilerType(
7525 getASTContext(),
7526 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7527 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7528
7529 case clang::Type::Paren:
7530 return CompilerType(getASTContext(),
7531 llvm::cast<clang::ParenType>(qual_type)->desugar())
7532 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7533
7534 case clang::Type::Typedef:
7535 return CompilerType(getASTContext(),
7536 llvm::cast<clang::TypedefType>(qual_type)
7537 ->getDecl()
7538 ->getUnderlyingType())
7539 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7540
7541 default:
7542 break;
7543 }
7544 }
7545 return UINT32_MAX;
7546}
Greg Claytond8d4a572015-08-11 21:38:15 +00007547
7548size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007549ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7550 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007551 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007552
Kate Stoneb9c1b512016-09-06 20:57:50 +00007553 clang::QualType qual_type(GetCanonicalQualType(type));
7554 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7555 switch (type_class) {
7556 case clang::Type::Record:
7557 if (GetCompleteType(type)) {
7558 const clang::CXXRecordDecl *cxx_record_decl =
7559 qual_type->getAsCXXRecordDecl();
7560 if (cxx_record_decl) {
7561 const clang::ClassTemplateSpecializationDecl *template_decl =
7562 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7563 cxx_record_decl);
7564 if (template_decl)
7565 return template_decl->getTemplateArgs().size();
7566 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007567 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007568 break;
7569
7570 case clang::Type::Typedef:
7571 return (CompilerType(getASTContext(),
7572 llvm::cast<clang::TypedefType>(qual_type)
7573 ->getDecl()
7574 ->getUnderlyingType()))
7575 .GetNumTemplateArguments();
7576
7577 case clang::Type::Auto:
7578 return (CompilerType(
7579 getASTContext(),
7580 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7581 .GetNumTemplateArguments();
7582
7583 case clang::Type::Elaborated:
7584 return (CompilerType(
7585 getASTContext(),
7586 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7587 .GetNumTemplateArguments();
7588
7589 case clang::Type::Paren:
7590 return (CompilerType(getASTContext(),
7591 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7592 .GetNumTemplateArguments();
7593
7594 default:
7595 break;
7596 }
7597
7598 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007599}
7600
Pavel Labath769b21e2017-11-13 14:26:21 +00007601const clang::ClassTemplateSpecializationDecl *
7602ClangASTContext::GetAsTemplateSpecialization(
7603 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007604 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007605 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007606
7607 clang::QualType qual_type(GetCanonicalQualType(type));
7608 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7609 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007610 case clang::Type::Record: {
7611 if (! GetCompleteType(type))
7612 return nullptr;
7613 const clang::CXXRecordDecl *cxx_record_decl =
7614 qual_type->getAsCXXRecordDecl();
7615 if (!cxx_record_decl)
7616 return nullptr;
7617 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7618 cxx_record_decl);
7619 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007620
7621 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007622 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7623 ->getDecl()
7624 ->getUnderlyingType()
7625 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007626
7627 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007628 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7629 ->getDeducedType()
7630 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007631
7632 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007633 return GetAsTemplateSpecialization(
7634 llvm::cast<clang::ElaboratedType>(qual_type)
7635 ->getNamedType()
7636 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007637
7638 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007639 return GetAsTemplateSpecialization(
7640 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007641
7642 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007643 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007644 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007645}
7646
7647lldb::TemplateArgumentKind
7648ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7649 size_t arg_idx) {
7650 const clang::ClassTemplateSpecializationDecl *template_decl =
7651 GetAsTemplateSpecialization(type);
7652 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7653 return eTemplateArgumentKindNull;
7654
7655 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7656 case clang::TemplateArgument::Null:
7657 return eTemplateArgumentKindNull;
7658
7659 case clang::TemplateArgument::NullPtr:
7660 return eTemplateArgumentKindNullPtr;
7661
7662 case clang::TemplateArgument::Type:
7663 return eTemplateArgumentKindType;
7664
7665 case clang::TemplateArgument::Declaration:
7666 return eTemplateArgumentKindDeclaration;
7667
7668 case clang::TemplateArgument::Integral:
7669 return eTemplateArgumentKindIntegral;
7670
7671 case clang::TemplateArgument::Template:
7672 return eTemplateArgumentKindTemplate;
7673
7674 case clang::TemplateArgument::TemplateExpansion:
7675 return eTemplateArgumentKindTemplateExpansion;
7676
7677 case clang::TemplateArgument::Expression:
7678 return eTemplateArgumentKindExpression;
7679
7680 case clang::TemplateArgument::Pack:
7681 return eTemplateArgumentKindPack;
7682 }
7683 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7684}
7685
7686CompilerType
7687ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7688 size_t idx) {
7689 const clang::ClassTemplateSpecializationDecl *template_decl =
7690 GetAsTemplateSpecialization(type);
7691 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7692 return CompilerType();
7693
7694 const clang::TemplateArgument &template_arg =
7695 template_decl->getTemplateArgs()[idx];
7696 if (template_arg.getKind() != clang::TemplateArgument::Type)
7697 return CompilerType();
7698
7699 return CompilerType(getASTContext(), template_arg.getAsType());
7700}
7701
Pavel Labathf59056f2017-11-30 10:16:54 +00007702llvm::Optional<CompilerType::IntegralTemplateArgument>
Pavel Labath769b21e2017-11-13 14:26:21 +00007703ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7704 size_t idx) {
7705 const clang::ClassTemplateSpecializationDecl *template_decl =
7706 GetAsTemplateSpecialization(type);
7707 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
Pavel Labathf59056f2017-11-30 10:16:54 +00007708 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007709
7710 const clang::TemplateArgument &template_arg =
7711 template_decl->getTemplateArgs()[idx];
7712 if (template_arg.getKind() != clang::TemplateArgument::Integral)
Pavel Labathf59056f2017-11-30 10:16:54 +00007713 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007714
Pavel Labathf59056f2017-11-30 10:16:54 +00007715 return {{template_arg.getAsIntegral(),
7716 CompilerType(getASTContext(), template_arg.getIntegralType())}};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007717}
7718
Kate Stoneb9c1b512016-09-06 20:57:50 +00007719CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7720 if (type)
7721 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7722 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007723}
7724
Kate Stoneb9c1b512016-09-06 20:57:50 +00007725clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7726 const clang::EnumType *enutype =
7727 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7728 if (enutype)
7729 return enutype->getDecl();
7730 return NULL;
7731}
7732
7733clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7734 const clang::RecordType *record_type =
7735 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7736 if (record_type)
7737 return record_type->getDecl();
7738 return nullptr;
7739}
7740
7741clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
7742 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
7743 if (qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00007744 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007745 else
7746 return qual_type->getAsTagDecl();
Greg Claytone6b36cd2015-12-08 01:02:08 +00007747}
7748
Greg Claytond8d4a572015-08-11 21:38:15 +00007749clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007750ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7751 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007752}
7753
7754clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007755ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7756 const clang::ObjCObjectType *objc_class_type =
7757 llvm::dyn_cast<clang::ObjCObjectType>(
7758 ClangUtil::GetCanonicalQualType(type));
7759 if (objc_class_type)
7760 return objc_class_type->getInterface();
7761 return nullptr;
7762}
7763
7764clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
7765 const CompilerType &type, const char *name,
7766 const CompilerType &field_clang_type, AccessType access,
7767 uint32_t bitfield_bit_size) {
7768 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007769 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007770 ClangASTContext *ast =
7771 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7772 if (!ast)
7773 return nullptr;
7774 clang::ASTContext *clang_ast = ast->getASTContext();
7775
7776 clang::FieldDecl *field = nullptr;
7777
7778 clang::Expr *bit_width = nullptr;
7779 if (bitfield_bit_size != 0) {
7780 llvm::APInt bitfield_bit_size_apint(
7781 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7782 bit_width = new (*clang_ast)
7783 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7784 clang_ast->IntTy, clang::SourceLocation());
7785 }
7786
7787 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7788 if (record_decl) {
7789 field = clang::FieldDecl::Create(
7790 *clang_ast, record_decl, clang::SourceLocation(),
7791 clang::SourceLocation(),
7792 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7793 ClangUtil::GetQualType(field_clang_type), // Field type
7794 nullptr, // TInfo *
7795 bit_width, // BitWidth
7796 false, // Mutable
7797 clang::ICIS_NoInit); // HasInit
7798
7799 if (!name) {
Adrian Prantl05097242018-04-30 16:49:04 +00007800 // Determine whether this field corresponds to an anonymous struct or
7801 // union.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007802 if (const clang::TagType *TagT =
7803 field->getType()->getAs<clang::TagType>()) {
7804 if (clang::RecordDecl *Rec =
7805 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7806 if (!Rec->getDeclName()) {
7807 Rec->setAnonymousStructOrUnion(true);
7808 field->setImplicit();
7809 }
7810 }
7811 }
7812
7813 if (field) {
7814 field->setAccess(
7815 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7816
7817 record_decl->addDecl(field);
7818
7819#ifdef LLDB_CONFIGURATION_DEBUG
7820 VerifyDecl(field);
7821#endif
7822 }
7823 } else {
7824 clang::ObjCInterfaceDecl *class_interface_decl =
7825 ast->GetAsObjCInterfaceDecl(type);
7826
7827 if (class_interface_decl) {
7828 const bool is_synthesized = false;
7829
7830 field_clang_type.GetCompleteType();
7831
7832 field = clang::ObjCIvarDecl::Create(
7833 *clang_ast, class_interface_decl, clang::SourceLocation(),
7834 clang::SourceLocation(),
7835 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7836 ClangUtil::GetQualType(field_clang_type), // Field type
7837 nullptr, // TypeSourceInfo *
7838 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7839 is_synthesized);
7840
7841 if (field) {
7842 class_interface_decl->addDecl(field);
7843
7844#ifdef LLDB_CONFIGURATION_DEBUG
7845 VerifyDecl(field);
7846#endif
7847 }
7848 }
7849 }
7850 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007851}
7852
Kate Stoneb9c1b512016-09-06 20:57:50 +00007853void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7854 if (!type)
7855 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007856
Kate Stoneb9c1b512016-09-06 20:57:50 +00007857 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7858 if (!ast)
7859 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007860
Kate Stoneb9c1b512016-09-06 20:57:50 +00007861 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007862
Kate Stoneb9c1b512016-09-06 20:57:50 +00007863 if (!record_decl)
7864 return;
7865
7866 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7867
7868 IndirectFieldVector indirect_fields;
7869 clang::RecordDecl::field_iterator field_pos;
7870 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7871 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7872 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7873 last_field_pos = field_pos++) {
7874 if (field_pos->isAnonymousStructOrUnion()) {
7875 clang::QualType field_qual_type = field_pos->getType();
7876
7877 const clang::RecordType *field_record_type =
7878 field_qual_type->getAs<clang::RecordType>();
7879
7880 if (!field_record_type)
7881 continue;
7882
7883 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7884
7885 if (!field_record_decl)
7886 continue;
7887
7888 for (clang::RecordDecl::decl_iterator
7889 di = field_record_decl->decls_begin(),
7890 de = field_record_decl->decls_end();
7891 di != de; ++di) {
7892 if (clang::FieldDecl *nested_field_decl =
7893 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7894 clang::NamedDecl **chain =
7895 new (*ast->getASTContext()) clang::NamedDecl *[2];
7896 chain[0] = *field_pos;
7897 chain[1] = nested_field_decl;
7898 clang::IndirectFieldDecl *indirect_field =
7899 clang::IndirectFieldDecl::Create(
7900 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7901 nested_field_decl->getIdentifier(),
7902 nested_field_decl->getType(), {chain, 2});
7903
7904 indirect_field->setImplicit();
7905
7906 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7907 field_pos->getAccess(), nested_field_decl->getAccess()));
7908
7909 indirect_fields.push_back(indirect_field);
7910 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7911 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7912 size_t nested_chain_size =
7913 nested_indirect_field_decl->getChainingSize();
7914 clang::NamedDecl **chain = new (*ast->getASTContext())
7915 clang::NamedDecl *[nested_chain_size + 1];
7916 chain[0] = *field_pos;
7917
7918 int chain_index = 1;
7919 for (clang::IndirectFieldDecl::chain_iterator
7920 nci = nested_indirect_field_decl->chain_begin(),
7921 nce = nested_indirect_field_decl->chain_end();
7922 nci < nce; ++nci) {
7923 chain[chain_index] = *nci;
7924 chain_index++;
7925 }
7926
7927 clang::IndirectFieldDecl *indirect_field =
7928 clang::IndirectFieldDecl::Create(
7929 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7930 nested_indirect_field_decl->getIdentifier(),
7931 nested_indirect_field_decl->getType(),
7932 {chain, nested_chain_size + 1});
7933
7934 indirect_field->setImplicit();
7935
7936 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7937 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
7938
7939 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00007940 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007941 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007942 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007943 }
7944
Adrian Prantl05097242018-04-30 16:49:04 +00007945 // Check the last field to see if it has an incomplete array type as its last
7946 // member and if it does, the tell the record decl about it
Kate Stoneb9c1b512016-09-06 20:57:50 +00007947 if (last_field_pos != field_end_pos) {
7948 if (last_field_pos->getType()->isIncompleteArrayType())
7949 record_decl->hasFlexibleArrayMember();
7950 }
7951
7952 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
7953 ife = indirect_fields.end();
7954 ifi < ife; ++ifi) {
7955 record_decl->addDecl(*ifi);
7956 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007957}
7958
Kate Stoneb9c1b512016-09-06 20:57:50 +00007959void ClangASTContext::SetIsPacked(const CompilerType &type) {
7960 if (type) {
7961 ClangASTContext *ast =
7962 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7963 if (ast) {
7964 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
7965
7966 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00007967 return;
7968
Kate Stoneb9c1b512016-09-06 20:57:50 +00007969 record_decl->addAttr(
7970 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00007971 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007972 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007973}
7974
Kate Stoneb9c1b512016-09-06 20:57:50 +00007975clang::VarDecl *ClangASTContext::AddVariableToRecordType(
7976 const CompilerType &type, const char *name, const CompilerType &var_type,
7977 AccessType access) {
7978 clang::VarDecl *var_decl = nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007979
Kate Stoneb9c1b512016-09-06 20:57:50 +00007980 if (!type.IsValid() || !var_type.IsValid())
7981 return nullptr;
7982 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7983 if (!ast)
7984 return nullptr;
7985
7986 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7987 if (record_decl) {
7988 var_decl = clang::VarDecl::Create(
7989 *ast->getASTContext(), // ASTContext &
7990 record_decl, // DeclContext *
7991 clang::SourceLocation(), // clang::SourceLocation StartLoc
7992 clang::SourceLocation(), // clang::SourceLocation IdLoc
7993 name ? &ast->getASTContext()->Idents.get(name)
7994 : nullptr, // clang::IdentifierInfo *
7995 ClangUtil::GetQualType(var_type), // Variable clang::QualType
7996 nullptr, // TypeSourceInfo *
7997 clang::SC_Static); // StorageClass
7998 if (var_decl) {
7999 var_decl->setAccess(
8000 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
8001 record_decl->addDecl(var_decl);
8002
Greg Claytond8d4a572015-08-11 21:38:15 +00008003#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008004 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008005#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008006 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008007 }
8008 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008009}
8010
Kate Stoneb9c1b512016-09-06 20:57:50 +00008011clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
Davide Italiano675767a2018-03-27 19:40:50 +00008012 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008013 const CompilerType &method_clang_type, lldb::AccessType access,
8014 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
8015 bool is_attr_used, bool is_artificial) {
8016 if (!type || !method_clang_type.IsValid() || name == nullptr ||
8017 name[0] == '\0')
8018 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008019
Kate Stoneb9c1b512016-09-06 20:57:50 +00008020 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008021
Kate Stoneb9c1b512016-09-06 20:57:50 +00008022 clang::CXXRecordDecl *cxx_record_decl =
8023 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008024
Kate Stoneb9c1b512016-09-06 20:57:50 +00008025 if (cxx_record_decl == nullptr)
8026 return nullptr;
8027
8028 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8029
8030 clang::CXXMethodDecl *cxx_method_decl = nullptr;
8031
8032 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
8033
8034 const clang::FunctionType *function_type =
8035 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
8036
8037 if (function_type == nullptr)
8038 return nullptr;
8039
8040 const clang::FunctionProtoType *method_function_prototype(
8041 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
8042
8043 if (!method_function_prototype)
8044 return nullptr;
8045
8046 unsigned int num_params = method_function_prototype->getNumParams();
8047
8048 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
8049 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
8050
8051 if (is_artificial)
8052 return nullptr; // skip everything artificial
8053
8054 if (name[0] == '~') {
8055 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
8056 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8057 clang::DeclarationNameInfo(
8058 getASTContext()->DeclarationNames.getCXXDestructorName(
8059 getASTContext()->getCanonicalType(record_qual_type)),
8060 clang::SourceLocation()),
8061 method_qual_type, nullptr, is_inline, is_artificial);
8062 cxx_method_decl = cxx_dtor_decl;
8063 } else if (decl_name == cxx_record_decl->getDeclName()) {
8064 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
8065 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8066 clang::DeclarationNameInfo(
8067 getASTContext()->DeclarationNames.getCXXConstructorName(
8068 getASTContext()->getCanonicalType(record_qual_type)),
8069 clang::SourceLocation()),
8070 method_qual_type,
8071 nullptr, // TypeSourceInfo *
8072 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
8073 cxx_method_decl = cxx_ctor_decl;
8074 } else {
8075 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8076 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8077
8078 if (IsOperator(name, op_kind)) {
8079 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
Adrian Prantl05097242018-04-30 16:49:04 +00008080 // Check the number of operator parameters. Sometimes we have seen bad
8081 // DWARF that doesn't correctly describe operators and if we try to
8082 // create a method and add it to the class, clang will assert and
8083 // crash, so we need to make sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008084 const bool is_method = true;
8085 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8086 is_method, op_kind, num_params))
8087 return nullptr;
8088 cxx_method_decl = clang::CXXMethodDecl::Create(
8089 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8090 clang::DeclarationNameInfo(
8091 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8092 clang::SourceLocation()),
8093 method_qual_type,
8094 nullptr, // TypeSourceInfo *
8095 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8096 } else if (num_params == 0) {
8097 // Conversion operators don't take params...
8098 cxx_method_decl = clang::CXXConversionDecl::Create(
8099 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8100 clang::DeclarationNameInfo(
8101 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8102 getASTContext()->getCanonicalType(
8103 function_type->getReturnType())),
8104 clang::SourceLocation()),
8105 method_qual_type,
8106 nullptr, // TypeSourceInfo *
8107 is_inline, is_explicit, false /*is_constexpr*/,
8108 clang::SourceLocation());
8109 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008110 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008111
8112 if (cxx_method_decl == nullptr) {
8113 cxx_method_decl = clang::CXXMethodDecl::Create(
8114 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8115 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8116 method_qual_type,
8117 nullptr, // TypeSourceInfo *
8118 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008119 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008120 }
8121
8122 clang::AccessSpecifier access_specifier =
8123 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8124
8125 cxx_method_decl->setAccess(access_specifier);
8126 cxx_method_decl->setVirtualAsWritten(is_virtual);
8127
8128 if (is_attr_used)
8129 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8130
Davide Italiano675767a2018-03-27 19:40:50 +00008131 if (mangled_name != NULL) {
8132 cxx_method_decl->addAttr(
8133 clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name));
8134 }
8135
Kate Stoneb9c1b512016-09-06 20:57:50 +00008136 // Populate the method decl with parameter decls
8137
8138 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8139
8140 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8141 params.push_back(clang::ParmVarDecl::Create(
8142 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8143 clang::SourceLocation(),
8144 nullptr, // anonymous
8145 method_function_prototype->getParamType(param_index), nullptr,
8146 clang::SC_None, nullptr));
8147 }
8148
8149 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8150
8151 cxx_record_decl->addDecl(cxx_method_decl);
8152
8153 // Sometimes the debug info will mention a constructor (default/copy/move),
8154 // destructor, or assignment operator (copy/move) but there won't be any
8155 // version of this in the code. So we check if the function was artificially
8156 // generated and if it is trivial and this lets the compiler/backend know
8157 // that it can inline the IR for these when it needs to and we can avoid a
8158 // "missing function" error when running expressions.
8159
8160 if (is_artificial) {
8161 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8162 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8163 (cxx_ctor_decl->isCopyConstructor() &&
8164 cxx_record_decl->hasTrivialCopyConstructor()) ||
8165 (cxx_ctor_decl->isMoveConstructor() &&
8166 cxx_record_decl->hasTrivialMoveConstructor()))) {
8167 cxx_ctor_decl->setDefaulted();
8168 cxx_ctor_decl->setTrivial(true);
8169 } else if (cxx_dtor_decl) {
8170 if (cxx_record_decl->hasTrivialDestructor()) {
8171 cxx_dtor_decl->setDefaulted();
8172 cxx_dtor_decl->setTrivial(true);
8173 }
8174 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8175 cxx_record_decl->hasTrivialCopyAssignment()) ||
8176 (cxx_method_decl->isMoveAssignmentOperator() &&
8177 cxx_record_decl->hasTrivialMoveAssignment())) {
8178 cxx_method_decl->setDefaulted();
8179 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008180 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008181 }
8182
Greg Claytond8d4a572015-08-11 21:38:15 +00008183#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008184 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008185#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008186
Kate Stoneb9c1b512016-09-06 20:57:50 +00008187 // printf ("decl->isPolymorphic() = %i\n",
8188 // cxx_record_decl->isPolymorphic());
8189 // printf ("decl->isAggregate() = %i\n",
8190 // cxx_record_decl->isAggregate());
8191 // printf ("decl->isPOD() = %i\n",
8192 // cxx_record_decl->isPOD());
8193 // printf ("decl->isEmpty() = %i\n",
8194 // cxx_record_decl->isEmpty());
8195 // printf ("decl->isAbstract() = %i\n",
8196 // cxx_record_decl->isAbstract());
8197 // printf ("decl->hasTrivialConstructor() = %i\n",
8198 // cxx_record_decl->hasTrivialConstructor());
8199 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8200 // cxx_record_decl->hasTrivialCopyConstructor());
8201 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8202 // cxx_record_decl->hasTrivialCopyAssignment());
8203 // printf ("decl->hasTrivialDestructor() = %i\n",
8204 // cxx_record_decl->hasTrivialDestructor());
8205 return cxx_method_decl;
8206}
Greg Claytond8d4a572015-08-11 21:38:15 +00008207
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00008208void ClangASTContext::AddMethodOverridesForCXXRecordType(
8209 lldb::opaque_compiler_type_t type) {
8210 if (auto *record = GetAsCXXRecordDecl(type))
8211 for (auto *method : record->methods())
8212 addOverridesForMethod(method);
8213}
8214
Greg Claytond8d4a572015-08-11 21:38:15 +00008215#pragma mark C++ Base Classes
8216
8217clang::CXXBaseSpecifier *
Kate Stoneb9c1b512016-09-06 20:57:50 +00008218ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8219 AccessType access, bool is_virtual,
8220 bool base_of_class) {
8221 if (type)
8222 return new clang::CXXBaseSpecifier(
8223 clang::SourceRange(), is_virtual, base_of_class,
8224 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8225 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8226 clang::SourceLocation());
8227 return nullptr;
8228}
8229
8230void ClangASTContext::DeleteBaseClassSpecifiers(
8231 clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes) {
8232 for (unsigned i = 0; i < num_base_classes; ++i) {
8233 delete base_classes[i];
8234 base_classes[i] = nullptr;
8235 }
8236}
8237
8238bool ClangASTContext::SetBaseClassesForClassType(
8239 lldb::opaque_compiler_type_t type,
8240 clang::CXXBaseSpecifier const *const *base_classes,
8241 unsigned num_base_classes) {
8242 if (type) {
8243 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8244 if (cxx_record_decl) {
8245 cxx_record_decl->setBases(base_classes, num_base_classes);
8246 return true;
8247 }
8248 }
8249 return false;
8250}
8251
8252bool ClangASTContext::SetObjCSuperClass(
8253 const CompilerType &type, const CompilerType &superclass_clang_type) {
8254 ClangASTContext *ast =
8255 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8256 if (!ast)
8257 return false;
8258 clang::ASTContext *clang_ast = ast->getASTContext();
8259
8260 if (type && superclass_clang_type.IsValid() &&
8261 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8262 clang::ObjCInterfaceDecl *class_interface_decl =
8263 GetAsObjCInterfaceDecl(type);
8264 clang::ObjCInterfaceDecl *super_interface_decl =
8265 GetAsObjCInterfaceDecl(superclass_clang_type);
8266 if (class_interface_decl && super_interface_decl) {
8267 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8268 clang_ast->getObjCInterfaceType(super_interface_decl)));
8269 return true;
8270 }
8271 }
8272 return false;
8273}
8274
8275bool ClangASTContext::AddObjCClassProperty(
8276 const CompilerType &type, const char *property_name,
8277 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8278 const char *property_setter_name, const char *property_getter_name,
8279 uint32_t property_attributes, ClangASTMetadata *metadata) {
8280 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8281 property_name[0] == '\0')
8282 return false;
8283 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8284 if (!ast)
8285 return false;
8286 clang::ASTContext *clang_ast = ast->getASTContext();
8287
8288 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8289
8290 if (class_interface_decl) {
8291 CompilerType property_clang_type_to_access;
8292
8293 if (property_clang_type.IsValid())
8294 property_clang_type_to_access = property_clang_type;
8295 else if (ivar_decl)
8296 property_clang_type_to_access =
8297 CompilerType(clang_ast, ivar_decl->getType());
8298
8299 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8300 clang::TypeSourceInfo *prop_type_source;
8301 if (ivar_decl)
8302 prop_type_source =
8303 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8304 else
8305 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8306 ClangUtil::GetQualType(property_clang_type));
8307
8308 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8309 *clang_ast, class_interface_decl,
8310 clang::SourceLocation(), // Source Location
8311 &clang_ast->Idents.get(property_name),
8312 clang::SourceLocation(), // Source Location for AT
8313 clang::SourceLocation(), // Source location for (
8314 ivar_decl ? ivar_decl->getType()
8315 : ClangUtil::GetQualType(property_clang_type),
8316 prop_type_source);
8317
8318 if (property_decl) {
8319 if (metadata)
8320 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8321
8322 class_interface_decl->addDecl(property_decl);
8323
8324 clang::Selector setter_sel, getter_sel;
8325
8326 if (property_setter_name != nullptr) {
8327 std::string property_setter_no_colon(
8328 property_setter_name, strlen(property_setter_name) - 1);
8329 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008330 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008331 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8332 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8333 std::string setter_sel_string("set");
8334 setter_sel_string.push_back(::toupper(property_name[0]));
8335 setter_sel_string.append(&property_name[1]);
8336 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008337 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008338 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8339 }
8340 property_decl->setSetterName(setter_sel);
8341 property_decl->setPropertyAttributes(
8342 clang::ObjCPropertyDecl::OBJC_PR_setter);
8343
8344 if (property_getter_name != nullptr) {
8345 clang::IdentifierInfo *getter_ident =
8346 &clang_ast->Idents.get(property_getter_name);
8347 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8348 } else {
8349 clang::IdentifierInfo *getter_ident =
8350 &clang_ast->Idents.get(property_name);
8351 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8352 }
8353 property_decl->setGetterName(getter_sel);
8354 property_decl->setPropertyAttributes(
8355 clang::ObjCPropertyDecl::OBJC_PR_getter);
8356
8357 if (ivar_decl)
8358 property_decl->setPropertyIvarDecl(ivar_decl);
8359
8360 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8361 property_decl->setPropertyAttributes(
8362 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8363 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8364 property_decl->setPropertyAttributes(
8365 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8366 if (property_attributes & DW_APPLE_PROPERTY_assign)
8367 property_decl->setPropertyAttributes(
8368 clang::ObjCPropertyDecl::OBJC_PR_assign);
8369 if (property_attributes & DW_APPLE_PROPERTY_retain)
8370 property_decl->setPropertyAttributes(
8371 clang::ObjCPropertyDecl::OBJC_PR_retain);
8372 if (property_attributes & DW_APPLE_PROPERTY_copy)
8373 property_decl->setPropertyAttributes(
8374 clang::ObjCPropertyDecl::OBJC_PR_copy);
8375 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8376 property_decl->setPropertyAttributes(
8377 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8378 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8379 property_decl->setPropertyAttributes(
8380 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8381 if (property_attributes &
8382 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8383 property_decl->setPropertyAttributes(
8384 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8385 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8386 property_decl->setPropertyAttributes(
8387 clang::ObjCPropertyDecl::OBJC_PR_class);
8388
8389 const bool isInstance =
8390 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8391
8392 if (!getter_sel.isNull() &&
8393 !(isInstance
8394 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8395 : class_interface_decl->lookupClassMethod(getter_sel))) {
8396 const bool isVariadic = false;
8397 const bool isSynthesized = false;
8398 const bool isImplicitlyDeclared = true;
8399 const bool isDefined = false;
8400 const clang::ObjCMethodDecl::ImplementationControl impControl =
8401 clang::ObjCMethodDecl::None;
8402 const bool HasRelatedResultType = false;
8403
8404 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8405 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8406 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8407 nullptr, class_interface_decl, isInstance, isVariadic,
8408 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8409 HasRelatedResultType);
8410
8411 if (getter && metadata)
8412 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8413
8414 if (getter) {
8415 getter->setMethodParams(*clang_ast,
8416 llvm::ArrayRef<clang::ParmVarDecl *>(),
8417 llvm::ArrayRef<clang::SourceLocation>());
8418
8419 class_interface_decl->addDecl(getter);
8420 }
8421 }
8422
8423 if (!setter_sel.isNull() &&
8424 !(isInstance
8425 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8426 : class_interface_decl->lookupClassMethod(setter_sel))) {
8427 clang::QualType result_type = clang_ast->VoidTy;
8428 const bool isVariadic = false;
8429 const bool isSynthesized = false;
8430 const bool isImplicitlyDeclared = true;
8431 const bool isDefined = false;
8432 const clang::ObjCMethodDecl::ImplementationControl impControl =
8433 clang::ObjCMethodDecl::None;
8434 const bool HasRelatedResultType = false;
8435
8436 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8437 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8438 setter_sel, result_type, nullptr, class_interface_decl,
8439 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8440 isDefined, impControl, HasRelatedResultType);
8441
8442 if (setter && metadata)
8443 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8444
8445 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8446
8447 params.push_back(clang::ParmVarDecl::Create(
8448 *clang_ast, setter, clang::SourceLocation(),
8449 clang::SourceLocation(),
8450 nullptr, // anonymous
8451 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8452 clang::SC_Auto, nullptr));
8453
8454 if (setter) {
8455 setter->setMethodParams(
8456 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8457 llvm::ArrayRef<clang::SourceLocation>());
8458
8459 class_interface_decl->addDecl(setter);
8460 }
8461 }
8462
8463 return true;
8464 }
8465 }
8466 }
8467 return false;
8468}
8469
8470bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8471 bool check_superclass) {
8472 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8473 if (class_interface_decl)
8474 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8475 return false;
8476}
8477
8478clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8479 const CompilerType &type,
8480 const char *name, // the full symbol name as seen in the symbol table
8481 // (lldb::opaque_compiler_type_t type, "-[NString
8482 // stringWithCString:]")
8483 const CompilerType &method_clang_type, lldb::AccessType access,
8484 bool is_artificial, bool is_variadic) {
8485 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008486 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008487
Kate Stoneb9c1b512016-09-06 20:57:50 +00008488 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8489
8490 if (class_interface_decl == nullptr)
8491 return nullptr;
8492 ClangASTContext *lldb_ast =
8493 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8494 if (lldb_ast == nullptr)
8495 return nullptr;
8496 clang::ASTContext *ast = lldb_ast->getASTContext();
8497
8498 const char *selector_start = ::strchr(name, ' ');
8499 if (selector_start == nullptr)
8500 return nullptr;
8501
8502 selector_start++;
8503 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8504
8505 size_t len = 0;
8506 const char *start;
8507 // printf ("name = '%s'\n", name);
8508
8509 unsigned num_selectors_with_args = 0;
8510 for (start = selector_start; start && *start != '\0' && *start != ']';
8511 start += len) {
8512 len = ::strcspn(start, ":]");
8513 bool has_arg = (start[len] == ':');
8514 if (has_arg)
8515 ++num_selectors_with_args;
8516 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8517 if (has_arg)
8518 len += 1;
8519 }
8520
8521 if (selector_idents.size() == 0)
8522 return nullptr;
8523
8524 clang::Selector method_selector = ast->Selectors.getSelector(
8525 num_selectors_with_args ? selector_idents.size() : 0,
8526 selector_idents.data());
8527
8528 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8529
8530 // Populate the method decl with parameter decls
8531 const clang::Type *method_type(method_qual_type.getTypePtr());
8532
8533 if (method_type == nullptr)
8534 return nullptr;
8535
8536 const clang::FunctionProtoType *method_function_prototype(
8537 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8538
8539 if (!method_function_prototype)
8540 return nullptr;
8541
8542 bool is_synthesized = false;
8543 bool is_defined = false;
8544 clang::ObjCMethodDecl::ImplementationControl imp_control =
8545 clang::ObjCMethodDecl::None;
8546
8547 const unsigned num_args = method_function_prototype->getNumParams();
8548
8549 if (num_args != num_selectors_with_args)
8550 return nullptr; // some debug information is corrupt. We are not going to
8551 // deal with it.
8552
8553 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8554 *ast,
8555 clang::SourceLocation(), // beginLoc,
8556 clang::SourceLocation(), // endLoc,
8557 method_selector, method_function_prototype->getReturnType(),
8558 nullptr, // TypeSourceInfo *ResultTInfo,
8559 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8560 ClangUtil::GetQualType(type)),
8561 name[0] == '-', is_variadic, is_synthesized,
8562 true, // is_implicitly_declared; we force this to true because we don't
8563 // have source locations
8564 is_defined, imp_control, false /*has_related_result_type*/);
8565
8566 if (objc_method_decl == nullptr)
8567 return nullptr;
8568
8569 if (num_args > 0) {
8570 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8571
8572 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8573 params.push_back(clang::ParmVarDecl::Create(
8574 *ast, objc_method_decl, clang::SourceLocation(),
8575 clang::SourceLocation(),
8576 nullptr, // anonymous
8577 method_function_prototype->getParamType(param_index), nullptr,
8578 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008579 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008580
Kate Stoneb9c1b512016-09-06 20:57:50 +00008581 objc_method_decl->setMethodParams(
8582 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8583 llvm::ArrayRef<clang::SourceLocation>());
8584 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008585
Kate Stoneb9c1b512016-09-06 20:57:50 +00008586 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008587
Greg Claytond8d4a572015-08-11 21:38:15 +00008588#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008589 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008590#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008591
8592 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008593}
8594
Kate Stoneb9c1b512016-09-06 20:57:50 +00008595bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8596 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008597 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008598
Kate Stoneb9c1b512016-09-06 20:57:50 +00008599 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008600
Kate Stoneb9c1b512016-09-06 20:57:50 +00008601 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8602 switch (type_class) {
8603 case clang::Type::Record: {
8604 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8605 if (cxx_record_decl)
8606 return cxx_record_decl->hasExternalLexicalStorage() ||
8607 cxx_record_decl->hasExternalVisibleStorage();
8608 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008609
Kate Stoneb9c1b512016-09-06 20:57:50 +00008610 case clang::Type::Enum: {
8611 clang::EnumDecl *enum_decl =
8612 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8613 if (enum_decl)
8614 return enum_decl->hasExternalLexicalStorage() ||
8615 enum_decl->hasExternalVisibleStorage();
8616 } break;
8617
8618 case clang::Type::ObjCObject:
8619 case clang::Type::ObjCInterface: {
8620 const clang::ObjCObjectType *objc_class_type =
8621 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8622 assert(objc_class_type);
8623 if (objc_class_type) {
8624 clang::ObjCInterfaceDecl *class_interface_decl =
8625 objc_class_type->getInterface();
8626
8627 if (class_interface_decl)
8628 return class_interface_decl->hasExternalLexicalStorage() ||
8629 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008630 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008631 } break;
8632
8633 case clang::Type::Typedef:
8634 return GetHasExternalStorage(CompilerType(
8635 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8636 ->getDecl()
8637 ->getUnderlyingType()
8638 .getAsOpaquePtr()));
8639
8640 case clang::Type::Auto:
8641 return GetHasExternalStorage(CompilerType(
8642 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8643 ->getDeducedType()
8644 .getAsOpaquePtr()));
8645
8646 case clang::Type::Elaborated:
8647 return GetHasExternalStorage(CompilerType(
8648 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8649 ->getNamedType()
8650 .getAsOpaquePtr()));
8651
8652 case clang::Type::Paren:
8653 return GetHasExternalStorage(CompilerType(
8654 type.GetTypeSystem(),
8655 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8656
8657 default:
8658 break;
8659 }
8660 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008661}
8662
Kate Stoneb9c1b512016-09-06 20:57:50 +00008663bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8664 bool has_extern) {
8665 if (!type)
8666 return false;
8667
8668 clang::QualType qual_type(GetCanonicalQualType(type));
8669
8670 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8671 switch (type_class) {
8672 case clang::Type::Record: {
8673 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8674 if (cxx_record_decl) {
8675 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8676 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8677 return true;
8678 }
8679 } break;
8680
8681 case clang::Type::Enum: {
8682 clang::EnumDecl *enum_decl =
8683 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8684 if (enum_decl) {
8685 enum_decl->setHasExternalLexicalStorage(has_extern);
8686 enum_decl->setHasExternalVisibleStorage(has_extern);
8687 return true;
8688 }
8689 } break;
8690
8691 case clang::Type::ObjCObject:
8692 case clang::Type::ObjCInterface: {
8693 const clang::ObjCObjectType *objc_class_type =
8694 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8695 assert(objc_class_type);
8696 if (objc_class_type) {
8697 clang::ObjCInterfaceDecl *class_interface_decl =
8698 objc_class_type->getInterface();
8699
8700 if (class_interface_decl) {
8701 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8702 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8703 return true;
8704 }
8705 }
8706 } break;
8707
8708 case clang::Type::Typedef:
8709 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8710 ->getDecl()
8711 ->getUnderlyingType()
8712 .getAsOpaquePtr(),
8713 has_extern);
8714
8715 case clang::Type::Auto:
8716 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8717 ->getDeducedType()
8718 .getAsOpaquePtr(),
8719 has_extern);
8720
8721 case clang::Type::Elaborated:
8722 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8723 ->getNamedType()
8724 .getAsOpaquePtr(),
8725 has_extern);
8726
8727 case clang::Type::Paren:
8728 return SetHasExternalStorage(
8729 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8730 has_extern);
8731
8732 default:
8733 break;
8734 }
8735 return false;
8736}
Greg Claytond8d4a572015-08-11 21:38:15 +00008737
8738#pragma mark TagDecl
8739
Kate Stoneb9c1b512016-09-06 20:57:50 +00008740bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8741 clang::QualType qual_type(ClangUtil::GetQualType(type));
8742 if (!qual_type.isNull()) {
8743 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8744 if (tag_type) {
8745 clang::TagDecl *tag_decl = tag_type->getDecl();
8746 if (tag_decl) {
8747 tag_decl->startDefinition();
8748 return true;
8749 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008750 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008751
8752 const clang::ObjCObjectType *object_type =
8753 qual_type->getAs<clang::ObjCObjectType>();
8754 if (object_type) {
8755 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8756 if (interface_decl) {
8757 interface_decl->startDefinition();
8758 return true;
8759 }
8760 }
8761 }
8762 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008763}
8764
Kate Stoneb9c1b512016-09-06 20:57:50 +00008765bool ClangASTContext::CompleteTagDeclarationDefinition(
8766 const CompilerType &type) {
8767 clang::QualType qual_type(ClangUtil::GetQualType(type));
8768 if (!qual_type.isNull()) {
8769 // Make sure we use the same methodology as
Adrian Prantl05097242018-04-30 16:49:04 +00008770 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end
8771 // the definition. Previously we were calling
Kate Stoneb9c1b512016-09-06 20:57:50 +00008772 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8773 if (tag_type) {
8774 clang::TagDecl *tag_decl = tag_type->getDecl();
8775 if (tag_decl) {
8776 clang::CXXRecordDecl *cxx_record_decl =
8777 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8778
8779 if (cxx_record_decl) {
8780 if (!cxx_record_decl->isCompleteDefinition())
8781 cxx_record_decl->completeDefinition();
8782 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8783 cxx_record_decl->setHasExternalLexicalStorage(false);
8784 cxx_record_decl->setHasExternalVisibleStorage(false);
8785 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008786 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008787 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008788 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008789
8790 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8791
8792 if (enutype) {
8793 clang::EnumDecl *enum_decl = enutype->getDecl();
8794
8795 if (enum_decl) {
8796 if (!enum_decl->isCompleteDefinition()) {
8797 ClangASTContext *lldb_ast =
8798 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8799 if (lldb_ast == nullptr)
8800 return false;
8801 clang::ASTContext *ast = lldb_ast->getASTContext();
8802
8803 /// TODO This really needs to be fixed.
8804
8805 QualType integer_type(enum_decl->getIntegerType());
8806 if (!integer_type.isNull()) {
8807 unsigned NumPositiveBits = 1;
8808 unsigned NumNegativeBits = 0;
8809
8810 clang::QualType promotion_qual_type;
8811 // If the enum integer type is less than an integer in bit width,
8812 // then we must promote it to an integer size.
8813 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8814 ast->getTypeSize(ast->IntTy)) {
8815 if (enum_decl->getIntegerType()->isSignedIntegerType())
8816 promotion_qual_type = ast->IntTy;
8817 else
8818 promotion_qual_type = ast->UnsignedIntTy;
8819 } else
8820 promotion_qual_type = enum_decl->getIntegerType();
8821
8822 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8823 promotion_qual_type, NumPositiveBits,
8824 NumNegativeBits);
8825 }
8826 }
8827 return true;
8828 }
8829 }
8830 }
8831 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008832}
8833
Kate Stoneb9c1b512016-09-06 20:57:50 +00008834bool ClangASTContext::AddEnumerationValueToEnumerationType(
8835 lldb::opaque_compiler_type_t type,
8836 const CompilerType &enumerator_clang_type, const Declaration &decl,
8837 const char *name, int64_t enum_value, uint32_t enum_value_bit_size) {
8838 if (type && enumerator_clang_type.IsValid() && name && name[0]) {
8839 clang::QualType enum_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008840
Kate Stoneb9c1b512016-09-06 20:57:50 +00008841 bool is_signed = false;
8842 enumerator_clang_type.IsIntegerType(is_signed);
Greg Claytond8d4a572015-08-11 21:38:15 +00008843 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Kate Stoneb9c1b512016-09-06 20:57:50 +00008844 if (clang_type) {
8845 const clang::EnumType *enutype =
8846 llvm::dyn_cast<clang::EnumType>(clang_type);
8847
8848 if (enutype) {
8849 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8850 enum_llvm_apsint = enum_value;
8851 clang::EnumConstantDecl *enumerator_decl =
8852 clang::EnumConstantDecl::Create(
8853 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8854 name ? &getASTContext()->Idents.get(name)
8855 : nullptr, // Identifier
8856 ClangUtil::GetQualType(enumerator_clang_type),
8857 nullptr, enum_llvm_apsint);
8858
8859 if (enumerator_decl) {
8860 enutype->getDecl()->addDecl(enumerator_decl);
8861
8862#ifdef LLDB_CONFIGURATION_DEBUG
8863 VerifyDecl(enumerator_decl);
8864#endif
8865
8866 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008867 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008868 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008869 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008870 }
8871 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008872}
8873
Greg Claytona1e5dc82015-08-11 22:53:00 +00008874CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00008875ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8876 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8877 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8878 if (clang_type) {
8879 const clang::EnumType *enutype =
8880 llvm::dyn_cast<clang::EnumType>(clang_type);
8881 if (enutype) {
8882 clang::EnumDecl *enum_decl = enutype->getDecl();
8883 if (enum_decl)
8884 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008885 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008886 }
8887 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008888}
8889
Kate Stoneb9c1b512016-09-06 20:57:50 +00008890CompilerType
8891ClangASTContext::CreateMemberPointerType(const CompilerType &type,
8892 const CompilerType &pointee_type) {
8893 if (type && pointee_type.IsValid() &&
8894 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8895 ClangASTContext *ast =
8896 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8897 if (!ast)
8898 return CompilerType();
8899 return CompilerType(ast->getASTContext(),
8900 ast->getASTContext()->getMemberPointerType(
8901 ClangUtil::GetQualType(pointee_type),
8902 ClangUtil::GetQualType(type).getTypePtr()));
8903 }
8904 return CompilerType();
8905}
Greg Claytond8d4a572015-08-11 21:38:15 +00008906
8907size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00008908ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
8909 const char *s, uint8_t *dst,
8910 size_t dst_size) {
8911 if (type) {
8912 clang::QualType qual_type(GetCanonicalQualType(type));
8913 uint32_t count = 0;
8914 bool is_complex = false;
8915 if (IsFloatingPointType(type, count, is_complex)) {
8916 // TODO: handle complex and vector types
8917 if (count != 1)
8918 return false;
8919
8920 llvm::StringRef s_sref(s);
8921 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
8922 s_sref);
8923
8924 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
8925 const uint64_t byte_size = bit_size / 8;
8926 if (dst_size >= byte_size) {
8927 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
8928 llvm::NextPowerOf2(byte_size) * 8);
Zachary Turner97206d52017-05-12 04:51:55 +00008929 lldb_private::Status get_data_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008930 if (scalar.GetAsMemoryData(dst, byte_size,
8931 lldb_private::endian::InlHostByteOrder(),
8932 get_data_error))
8933 return byte_size;
8934 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008935 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008936 }
8937 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00008938}
8939
Greg Claytond8d4a572015-08-11 21:38:15 +00008940//----------------------------------------------------------------------
8941// Dumping types
8942//----------------------------------------------------------------------
8943#define DEPTH_INCREMENT 2
8944
Kate Stoneb9c1b512016-09-06 20:57:50 +00008945void ClangASTContext::DumpValue(
8946 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00008947 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008948 lldb::offset_t data_byte_offset, size_t data_byte_size,
8949 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
8950 bool show_summary, bool verbose, uint32_t depth) {
8951 if (!type)
8952 return;
8953
8954 clang::QualType qual_type(GetQualType(type));
8955 switch (qual_type->getTypeClass()) {
8956 case clang::Type::Record:
8957 if (GetCompleteType(type)) {
8958 const clang::RecordType *record_type =
8959 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8960 const clang::RecordDecl *record_decl = record_type->getDecl();
8961 assert(record_decl);
8962 uint32_t field_bit_offset = 0;
8963 uint32_t field_byte_offset = 0;
8964 const clang::ASTRecordLayout &record_layout =
8965 getASTContext()->getASTRecordLayout(record_decl);
8966 uint32_t child_idx = 0;
8967
8968 const clang::CXXRecordDecl *cxx_record_decl =
8969 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8970 if (cxx_record_decl) {
8971 // We might have base classes to print out first
8972 clang::CXXRecordDecl::base_class_const_iterator base_class,
8973 base_class_end;
8974 for (base_class = cxx_record_decl->bases_begin(),
8975 base_class_end = cxx_record_decl->bases_end();
8976 base_class != base_class_end; ++base_class) {
8977 const clang::CXXRecordDecl *base_class_decl =
8978 llvm::cast<clang::CXXRecordDecl>(
8979 base_class->getType()->getAs<clang::RecordType>()->getDecl());
8980
8981 // Skip empty base classes
8982 if (verbose == false &&
8983 ClangASTContext::RecordHasFields(base_class_decl) == false)
8984 continue;
8985
8986 if (base_class->isVirtual())
8987 field_bit_offset =
8988 record_layout.getVBaseClassOffset(base_class_decl)
8989 .getQuantity() *
8990 8;
8991 else
8992 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
8993 .getQuantity() *
8994 8;
8995 field_byte_offset = field_bit_offset / 8;
8996 assert(field_bit_offset % 8 == 0);
8997 if (child_idx == 0)
8998 s->PutChar('{');
8999 else
9000 s->PutChar(',');
9001
9002 clang::QualType base_class_qual_type = base_class->getType();
9003 std::string base_class_type_name(base_class_qual_type.getAsString());
9004
9005 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00009006 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
9007 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009008
9009 clang::TypeInfo base_class_type_info =
9010 getASTContext()->getTypeInfo(base_class_qual_type);
9011
9012 // Dump the value of the member
9013 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
9014 base_clang_type.DumpValue(
9015 exe_ctx,
9016 s, // Stream to dump to
9017 base_clang_type
9018 .GetFormat(), // The format with which to display the member
9019 data, // Data buffer containing all bytes for this type
9020 data_byte_offset + field_byte_offset, // Offset into "data" where
9021 // to grab value from
9022 base_class_type_info.Width / 8, // Size of this type in bytes
9023 0, // Bitfield bit size
9024 0, // Bitfield bit offset
9025 show_types, // Boolean indicating if we should show the variable
9026 // types
9027 show_summary, // Boolean indicating if we should show a summary
9028 // for the current type
9029 verbose, // Verbose output?
9030 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9031 // children
9032
9033 ++child_idx;
9034 }
9035 }
9036 uint32_t field_idx = 0;
9037 clang::RecordDecl::field_iterator field, field_end;
9038 for (field = record_decl->field_begin(),
9039 field_end = record_decl->field_end();
9040 field != field_end; ++field, ++field_idx, ++child_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009041 // Print the starting squiggly bracket (if this is the first member) or
9042 // comma (for member 2 and beyond) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009043 if (child_idx == 0)
9044 s->PutChar('{');
9045 else
9046 s->PutChar(',');
9047
9048 // Indent
9049 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
9050
9051 clang::QualType field_type = field->getType();
9052 // Print the member type if requested
Adrian Prantl05097242018-04-30 16:49:04 +00009053 // Figure out the type byte size (field_type_info.first) and alignment
9054 // (field_type_info.second) from the AST context.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009055 clang::TypeInfo field_type_info =
9056 getASTContext()->getTypeInfo(field_type);
9057 assert(field_idx < record_layout.getFieldCount());
9058 // Figure out the field offset within the current struct/union/class
9059 // type
9060 field_bit_offset = record_layout.getFieldOffset(field_idx);
9061 field_byte_offset = field_bit_offset / 8;
9062 uint32_t field_bitfield_bit_size = 0;
9063 uint32_t field_bitfield_bit_offset = 0;
9064 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
9065 field_bitfield_bit_size))
9066 field_bitfield_bit_offset = field_bit_offset % 8;
9067
9068 if (show_types) {
9069 std::string field_type_name(field_type.getAsString());
9070 if (field_bitfield_bit_size > 0)
9071 s->Printf("(%s:%u) ", field_type_name.c_str(),
9072 field_bitfield_bit_size);
9073 else
9074 s->Printf("(%s) ", field_type_name.c_str());
9075 }
9076 // Print the member name and equal sign
9077 s->Printf("%s = ", field->getNameAsString().c_str());
9078
9079 // Dump the value of the member
9080 CompilerType field_clang_type(getASTContext(), field_type);
9081 field_clang_type.DumpValue(
9082 exe_ctx,
9083 s, // Stream to dump to
9084 field_clang_type
9085 .GetFormat(), // The format with which to display the member
9086 data, // Data buffer containing all bytes for this type
9087 data_byte_offset + field_byte_offset, // Offset into "data" where to
9088 // grab value from
9089 field_type_info.Width / 8, // Size of this type in bytes
9090 field_bitfield_bit_size, // Bitfield bit size
9091 field_bitfield_bit_offset, // Bitfield bit offset
9092 show_types, // Boolean indicating if we should show the variable
9093 // types
9094 show_summary, // Boolean indicating if we should show a summary for
9095 // the current type
9096 verbose, // Verbose output?
9097 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9098 // children
9099 }
9100
9101 // Indent the trailing squiggly bracket
9102 if (child_idx > 0)
9103 s->Printf("\n%*s}", depth, "");
9104 }
9105 return;
9106
9107 case clang::Type::Enum:
9108 if (GetCompleteType(type)) {
9109 const clang::EnumType *enutype =
9110 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9111 const clang::EnumDecl *enum_decl = enutype->getDecl();
9112 assert(enum_decl);
9113 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9114 lldb::offset_t offset = data_byte_offset;
9115 const int64_t enum_value = data.GetMaxU64Bitfield(
9116 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9117 for (enum_pos = enum_decl->enumerator_begin(),
9118 enum_end_pos = enum_decl->enumerator_end();
9119 enum_pos != enum_end_pos; ++enum_pos) {
9120 if (enum_pos->getInitVal() == enum_value) {
9121 s->Printf("%s", enum_pos->getNameAsString().c_str());
9122 return;
9123 }
9124 }
Adrian Prantl05097242018-04-30 16:49:04 +00009125 // If we have gotten here we didn't get find the enumerator in the enum
9126 // decl, so just print the integer.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009127 s->Printf("%" PRIi64, enum_value);
9128 }
9129 return;
9130
9131 case clang::Type::ConstantArray: {
9132 const clang::ConstantArrayType *array =
9133 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9134 bool is_array_of_characters = false;
9135 clang::QualType element_qual_type = array->getElementType();
9136
9137 const clang::Type *canonical_type =
9138 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9139 if (canonical_type)
9140 is_array_of_characters = canonical_type->isCharType();
9141
9142 const uint64_t element_count = array->getSize().getLimitedValue();
9143
9144 clang::TypeInfo field_type_info =
9145 getASTContext()->getTypeInfo(element_qual_type);
9146
9147 uint32_t element_idx = 0;
9148 uint32_t element_offset = 0;
9149 uint64_t element_byte_size = field_type_info.Width / 8;
9150 uint32_t element_stride = element_byte_size;
9151
9152 if (is_array_of_characters) {
9153 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009154 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9155 element_byte_size, element_count, UINT32_MAX,
9156 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009157 s->PutChar('"');
9158 return;
9159 } else {
9160 CompilerType element_clang_type(getASTContext(), element_qual_type);
9161 lldb::Format element_format = element_clang_type.GetFormat();
9162
9163 for (element_idx = 0; element_idx < element_count; ++element_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009164 // Print the starting squiggly bracket (if this is the first member) or
9165 // comman (for member 2 and beyong) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009166 if (element_idx == 0)
9167 s->PutChar('{');
9168 else
9169 s->PutChar(',');
9170
9171 // Indent and print the index
9172 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9173
9174 // Figure out the field offset within the current struct/union/class
9175 // type
9176 element_offset = element_idx * element_stride;
9177
9178 // Dump the value of the member
9179 element_clang_type.DumpValue(
9180 exe_ctx,
9181 s, // Stream to dump to
9182 element_format, // The format with which to display the element
9183 data, // Data buffer containing all bytes for this type
9184 data_byte_offset +
9185 element_offset, // Offset into "data" where to grab value from
9186 element_byte_size, // Size of this type in bytes
9187 0, // Bitfield bit size
9188 0, // Bitfield bit offset
9189 show_types, // Boolean indicating if we should show the variable
9190 // types
9191 show_summary, // Boolean indicating if we should show a summary for
9192 // the current type
9193 verbose, // Verbose output?
9194 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9195 // children
9196 }
9197
9198 // Indent the trailing squiggly bracket
9199 if (element_idx > 0)
9200 s->Printf("\n%*s}", depth, "");
9201 }
9202 }
9203 return;
9204
9205 case clang::Type::Typedef: {
9206 clang::QualType typedef_qual_type =
9207 llvm::cast<clang::TypedefType>(qual_type)
9208 ->getDecl()
9209 ->getUnderlyingType();
9210
9211 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9212 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9213 clang::TypeInfo typedef_type_info =
9214 getASTContext()->getTypeInfo(typedef_qual_type);
9215 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9216
9217 return typedef_clang_type.DumpValue(
9218 exe_ctx,
9219 s, // Stream to dump to
9220 typedef_format, // The format with which to display the element
9221 data, // Data buffer containing all bytes for this type
9222 data_byte_offset, // Offset into "data" where to grab value from
9223 typedef_byte_size, // Size of this type in bytes
9224 bitfield_bit_size, // Bitfield bit size
9225 bitfield_bit_offset, // Bitfield bit offset
9226 show_types, // Boolean indicating if we should show the variable types
9227 show_summary, // Boolean indicating if we should show a summary for the
9228 // current type
9229 verbose, // Verbose output?
9230 depth); // Scope depth for any types that have children
9231 } break;
9232
9233 case clang::Type::Auto: {
9234 clang::QualType elaborated_qual_type =
9235 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9236 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9237 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9238 clang::TypeInfo elaborated_type_info =
9239 getASTContext()->getTypeInfo(elaborated_qual_type);
9240 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9241
9242 return elaborated_clang_type.DumpValue(
9243 exe_ctx,
9244 s, // Stream to dump to
9245 elaborated_format, // The format with which to display the element
9246 data, // Data buffer containing all bytes for this type
9247 data_byte_offset, // Offset into "data" where to grab value from
9248 elaborated_byte_size, // Size of this type in bytes
9249 bitfield_bit_size, // Bitfield bit size
9250 bitfield_bit_offset, // Bitfield bit offset
9251 show_types, // Boolean indicating if we should show the variable types
9252 show_summary, // Boolean indicating if we should show a summary for the
9253 // current type
9254 verbose, // Verbose output?
9255 depth); // Scope depth for any types that have children
9256 } break;
9257
9258 case clang::Type::Elaborated: {
9259 clang::QualType elaborated_qual_type =
9260 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9261 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9262 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9263 clang::TypeInfo elaborated_type_info =
9264 getASTContext()->getTypeInfo(elaborated_qual_type);
9265 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9266
9267 return elaborated_clang_type.DumpValue(
9268 exe_ctx,
9269 s, // Stream to dump to
9270 elaborated_format, // The format with which to display the element
9271 data, // Data buffer containing all bytes for this type
9272 data_byte_offset, // Offset into "data" where to grab value from
9273 elaborated_byte_size, // Size of this type in bytes
9274 bitfield_bit_size, // Bitfield bit size
9275 bitfield_bit_offset, // Bitfield bit offset
9276 show_types, // Boolean indicating if we should show the variable types
9277 show_summary, // Boolean indicating if we should show a summary for the
9278 // current type
9279 verbose, // Verbose output?
9280 depth); // Scope depth for any types that have children
9281 } break;
9282
9283 case clang::Type::Paren: {
9284 clang::QualType desugar_qual_type =
9285 llvm::cast<clang::ParenType>(qual_type)->desugar();
9286 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9287
9288 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9289 clang::TypeInfo desugar_type_info =
9290 getASTContext()->getTypeInfo(desugar_qual_type);
9291 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9292
9293 return desugar_clang_type.DumpValue(
9294 exe_ctx,
9295 s, // Stream to dump to
9296 desugar_format, // The format with which to display the element
9297 data, // Data buffer containing all bytes for this type
9298 data_byte_offset, // Offset into "data" where to grab value from
9299 desugar_byte_size, // Size of this type in bytes
9300 bitfield_bit_size, // Bitfield bit size
9301 bitfield_bit_offset, // Bitfield bit offset
9302 show_types, // Boolean indicating if we should show the variable types
9303 show_summary, // Boolean indicating if we should show a summary for the
9304 // current type
9305 verbose, // Verbose output?
9306 depth); // Scope depth for any types that have children
9307 } break;
9308
9309 default:
9310 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009311 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9312 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9313 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009314
9315 if (show_summary)
9316 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9317 break;
9318 }
9319}
9320
9321bool ClangASTContext::DumpTypeValue(
9322 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009323 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9324 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009325 ExecutionContextScope *exe_scope) {
9326 if (!type)
9327 return false;
9328 if (IsAggregateType(type)) {
9329 return false;
9330 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009331 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009332
9333 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9334 switch (type_class) {
9335 case clang::Type::Typedef: {
9336 clang::QualType typedef_qual_type =
9337 llvm::cast<clang::TypedefType>(qual_type)
9338 ->getDecl()
9339 ->getUnderlyingType();
9340 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9341 if (format == eFormatDefault)
9342 format = typedef_clang_type.GetFormat();
9343 clang::TypeInfo typedef_type_info =
9344 getASTContext()->getTypeInfo(typedef_qual_type);
9345 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9346
9347 return typedef_clang_type.DumpTypeValue(
9348 s,
9349 format, // The format with which to display the element
9350 data, // Data buffer containing all bytes for this type
9351 byte_offset, // Offset into "data" where to grab value from
9352 typedef_byte_size, // Size of this type in bytes
9353 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9354 // treat as a bitfield
9355 bitfield_bit_offset, // Offset in bits of a bitfield value if
9356 // bitfield_bit_size != 0
9357 exe_scope);
9358 } break;
9359
9360 case clang::Type::Enum:
Adrian Prantl05097242018-04-30 16:49:04 +00009361 // If our format is enum or default, show the enumeration value as its
9362 // enumeration string value, else just display it as requested.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009363 if ((format == eFormatEnum || format == eFormatDefault) &&
9364 GetCompleteType(type)) {
9365 const clang::EnumType *enutype =
9366 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9367 const clang::EnumDecl *enum_decl = enutype->getDecl();
9368 assert(enum_decl);
9369 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9370 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9371 lldb::offset_t offset = byte_offset;
9372 if (is_signed) {
9373 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9374 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9375 for (enum_pos = enum_decl->enumerator_begin(),
9376 enum_end_pos = enum_decl->enumerator_end();
9377 enum_pos != enum_end_pos; ++enum_pos) {
9378 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009379 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009380 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009381 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009382 }
9383 // If we have gotten here we didn't get find the enumerator in the
9384 // enum decl, so just print the integer.
9385 s->Printf("%" PRIi64, enum_svalue);
9386 } else {
9387 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9388 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9389 for (enum_pos = enum_decl->enumerator_begin(),
9390 enum_end_pos = enum_decl->enumerator_end();
9391 enum_pos != enum_end_pos; ++enum_pos) {
9392 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009393 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009394 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009395 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009396 }
9397 // If we have gotten here we didn't get find the enumerator in the
9398 // enum decl, so just print the integer.
9399 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009400 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009401 return true;
9402 }
9403 // format was not enum, just fall through and dump the value as
9404 // requested....
9405 LLVM_FALLTHROUGH;
9406
9407 default:
9408 // We are down to a scalar type that we just need to display.
9409 {
9410 uint32_t item_count = 1;
9411 // A few formats, we might need to modify our size and count for
9412 // depending
9413 // on how we are trying to display the value...
9414 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009415 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009416 case eFormatBoolean:
9417 case eFormatBinary:
9418 case eFormatComplex:
9419 case eFormatCString: // NULL terminated C strings
9420 case eFormatDecimal:
9421 case eFormatEnum:
9422 case eFormatHex:
9423 case eFormatHexUppercase:
9424 case eFormatFloat:
9425 case eFormatOctal:
9426 case eFormatOSType:
9427 case eFormatUnsigned:
9428 case eFormatPointer:
9429 case eFormatVectorOfChar:
9430 case eFormatVectorOfSInt8:
9431 case eFormatVectorOfUInt8:
9432 case eFormatVectorOfSInt16:
9433 case eFormatVectorOfUInt16:
9434 case eFormatVectorOfSInt32:
9435 case eFormatVectorOfUInt32:
9436 case eFormatVectorOfSInt64:
9437 case eFormatVectorOfUInt64:
9438 case eFormatVectorOfFloat32:
9439 case eFormatVectorOfFloat64:
9440 case eFormatVectorOfUInt128:
9441 break;
9442
9443 case eFormatChar:
9444 case eFormatCharPrintable:
9445 case eFormatCharArray:
9446 case eFormatBytes:
9447 case eFormatBytesWithASCII:
9448 item_count = byte_size;
9449 byte_size = 1;
9450 break;
9451
9452 case eFormatUnicode16:
9453 item_count = byte_size / 2;
9454 byte_size = 2;
9455 break;
9456
9457 case eFormatUnicode32:
9458 item_count = byte_size / 4;
9459 byte_size = 4;
9460 break;
9461 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009462 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9463 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9464 bitfield_bit_size, bitfield_bit_offset,
9465 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009466 }
9467 break;
9468 }
9469 }
9470 return 0;
9471}
9472
9473void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9474 ExecutionContext *exe_ctx, Stream *s,
9475 const lldb_private::DataExtractor &data,
9476 lldb::offset_t data_byte_offset,
9477 size_t data_byte_size) {
9478 uint32_t length = 0;
9479 if (IsCStringType(type, length)) {
9480 if (exe_ctx) {
9481 Process *process = exe_ctx->GetProcessPtr();
9482 if (process) {
9483 lldb::offset_t offset = data_byte_offset;
9484 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9485 std::vector<uint8_t> buf;
9486 if (length > 0)
9487 buf.resize(length);
9488 else
9489 buf.resize(256);
9490
Zachary Turner29cb8682017-03-03 20:57:05 +00009491 DataExtractor cstr_data(&buf.front(), buf.size(),
9492 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009493 buf.back() = '\0';
9494 size_t bytes_read;
9495 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009496 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009497 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9498 buf.size(), error)) > 0) {
9499 const size_t len = strlen((const char *)&buf.front());
9500 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009501 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009502 if (total_cstr_len == 0)
9503 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009504 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9505 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009506 total_cstr_len += len;
9507 if (len < buf.size())
9508 break;
9509 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009510 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009511 if (total_cstr_len > 0)
9512 s->PutChar('"');
9513 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009514 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009515 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009516}
9517
Kate Stoneb9c1b512016-09-06 20:57:50 +00009518void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9519 StreamFile s(stdout, false);
9520 DumpTypeDescription(type, &s);
9521 ClangASTMetadata *metadata =
9522 ClangASTContext::GetMetadata(getASTContext(), type);
9523 if (metadata) {
9524 metadata->Dump(&s);
9525 }
9526}
Greg Claytond8d4a572015-08-11 21:38:15 +00009527
Kate Stoneb9c1b512016-09-06 20:57:50 +00009528void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9529 Stream *s) {
9530 if (type) {
9531 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009532
Kate Stoneb9c1b512016-09-06 20:57:50 +00009533 llvm::SmallVector<char, 1024> buf;
9534 llvm::raw_svector_ostream llvm_ostrm(buf);
9535
9536 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9537 switch (type_class) {
9538 case clang::Type::ObjCObject:
9539 case clang::Type::ObjCInterface: {
9540 GetCompleteType(type);
9541
9542 const clang::ObjCObjectType *objc_class_type =
9543 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9544 assert(objc_class_type);
9545 if (objc_class_type) {
9546 clang::ObjCInterfaceDecl *class_interface_decl =
9547 objc_class_type->getInterface();
9548 if (class_interface_decl) {
9549 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9550 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009551 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009552 }
9553 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009554
Kate Stoneb9c1b512016-09-06 20:57:50 +00009555 case clang::Type::Typedef: {
9556 const clang::TypedefType *typedef_type =
9557 qual_type->getAs<clang::TypedefType>();
9558 if (typedef_type) {
9559 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9560 std::string clang_typedef_name(
9561 typedef_decl->getQualifiedNameAsString());
9562 if (!clang_typedef_name.empty()) {
9563 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009564 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009565 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009566 }
9567 } break;
9568
9569 case clang::Type::Auto:
9570 CompilerType(getASTContext(),
9571 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9572 .DumpTypeDescription(s);
9573 return;
9574
9575 case clang::Type::Elaborated:
9576 CompilerType(getASTContext(),
9577 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9578 .DumpTypeDescription(s);
9579 return;
9580
9581 case clang::Type::Paren:
9582 CompilerType(getASTContext(),
9583 llvm::cast<clang::ParenType>(qual_type)->desugar())
9584 .DumpTypeDescription(s);
9585 return;
9586
9587 case clang::Type::Record: {
9588 GetCompleteType(type);
9589
9590 const clang::RecordType *record_type =
9591 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9592 const clang::RecordDecl *record_decl = record_type->getDecl();
9593 const clang::CXXRecordDecl *cxx_record_decl =
9594 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9595
9596 if (cxx_record_decl)
9597 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9598 s->GetIndentLevel());
9599 else
9600 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9601 s->GetIndentLevel());
9602 } break;
9603
9604 default: {
9605 const clang::TagType *tag_type =
9606 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9607 if (tag_type) {
9608 clang::TagDecl *tag_decl = tag_type->getDecl();
9609 if (tag_decl)
9610 tag_decl->print(llvm_ostrm, 0);
9611 } else {
9612 std::string clang_type_name(qual_type.getAsString());
9613 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009614 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009615 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009616 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009617 }
9618
Kate Stoneb9c1b512016-09-06 20:57:50 +00009619 if (buf.size() > 0) {
9620 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009621 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009622 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009623}
9624
Kate Stoneb9c1b512016-09-06 20:57:50 +00009625void ClangASTContext::DumpTypeName(const CompilerType &type) {
9626 if (ClangUtil::IsClangType(type)) {
9627 clang::QualType qual_type(
9628 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9629
9630 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9631 switch (type_class) {
9632 case clang::Type::Record: {
9633 const clang::CXXRecordDecl *cxx_record_decl =
9634 qual_type->getAsCXXRecordDecl();
9635 if (cxx_record_decl)
9636 printf("class %s", cxx_record_decl->getName().str().c_str());
9637 } break;
9638
9639 case clang::Type::Enum: {
9640 clang::EnumDecl *enum_decl =
9641 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9642 if (enum_decl) {
9643 printf("enum %s", enum_decl->getName().str().c_str());
9644 }
9645 } break;
9646
9647 case clang::Type::ObjCObject:
9648 case clang::Type::ObjCInterface: {
9649 const clang::ObjCObjectType *objc_class_type =
9650 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9651 if (objc_class_type) {
9652 clang::ObjCInterfaceDecl *class_interface_decl =
9653 objc_class_type->getInterface();
Adrian Prantl05097242018-04-30 16:49:04 +00009654 // We currently can't complete objective C types through the newly
9655 // added ASTContext because it only supports TagDecl objects right
9656 // now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00009657 if (class_interface_decl)
9658 printf("@class %s", class_interface_decl->getName().str().c_str());
9659 }
9660 } break;
9661
9662 case clang::Type::Typedef:
9663 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9664 ->getDecl()
9665 ->getName()
9666 .str()
9667 .c_str());
9668 break;
9669
9670 case clang::Type::Auto:
9671 printf("auto ");
9672 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9673 llvm::cast<clang::AutoType>(qual_type)
9674 ->getDeducedType()
9675 .getAsOpaquePtr()));
9676
9677 case clang::Type::Elaborated:
9678 printf("elaborated ");
9679 return DumpTypeName(CompilerType(
9680 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9681 ->getNamedType()
9682 .getAsOpaquePtr()));
9683
9684 case clang::Type::Paren:
9685 printf("paren ");
9686 return DumpTypeName(CompilerType(
9687 type.GetTypeSystem(),
9688 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9689
9690 default:
9691 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9692 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009693 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009694 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009695}
9696
Kate Stoneb9c1b512016-09-06 20:57:50 +00009697clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9698 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9699 const char *parent_name, int tag_decl_kind,
9700 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9701 if (template_param_infos.IsValid()) {
9702 std::string template_basename(parent_name);
9703 template_basename.erase(template_basename.find('<'));
9704
9705 return CreateClassTemplateDecl(decl_ctx, access_type,
9706 template_basename.c_str(), tag_decl_kind,
9707 template_param_infos);
9708 }
9709 return NULL;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009710}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009711
Kate Stoneb9c1b512016-09-06 20:57:50 +00009712void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9713 ClangASTContext *ast = (ClangASTContext *)baton;
9714 SymbolFile *sym_file = ast->GetSymbolFile();
9715 if (sym_file) {
9716 CompilerType clang_type = GetTypeForDecl(decl);
9717 if (clang_type)
9718 sym_file->CompleteType(clang_type);
9719 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009720}
9721
Kate Stoneb9c1b512016-09-06 20:57:50 +00009722void ClangASTContext::CompleteObjCInterfaceDecl(
9723 void *baton, clang::ObjCInterfaceDecl *decl) {
9724 ClangASTContext *ast = (ClangASTContext *)baton;
9725 SymbolFile *sym_file = ast->GetSymbolFile();
9726 if (sym_file) {
9727 CompilerType clang_type = GetTypeForDecl(decl);
9728 if (clang_type)
9729 sym_file->CompleteType(clang_type);
9730 }
Zachary Turner42dff792016-04-15 00:21:26 +00009731}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009732
Kate Stoneb9c1b512016-09-06 20:57:50 +00009733DWARFASTParser *ClangASTContext::GetDWARFParser() {
9734 if (!m_dwarf_ast_parser_ap)
9735 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9736 return m_dwarf_ast_parser_ap.get();
9737}
9738
9739PDBASTParser *ClangASTContext::GetPDBParser() {
9740 if (!m_pdb_ast_parser_ap)
9741 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9742 return m_pdb_ast_parser_ap.get();
9743}
9744
9745bool ClangASTContext::LayoutRecordType(
9746 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9747 uint64_t &alignment,
9748 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9749 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9750 &base_offsets,
9751 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9752 &vbase_offsets) {
9753 ClangASTContext *ast = (ClangASTContext *)baton;
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +00009754 lldb_private::ClangASTImporter *importer = nullptr;
9755 if (ast->m_dwarf_ast_parser_ap)
9756 importer = &ast->m_dwarf_ast_parser_ap->GetClangASTImporter();
9757 if (!importer && ast->m_pdb_ast_parser_ap)
9758 importer = &ast->m_pdb_ast_parser_ap->GetClangASTImporter();
9759 if (!importer)
9760 return false;
9761
9762 return importer->LayoutRecordType(record_decl, bit_size, alignment,
9763 field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009764}
9765
Greg Clayton99558cc42015-08-24 23:46:31 +00009766//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009767// CompilerDecl override functions
9768//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009769
Kate Stoneb9c1b512016-09-06 20:57:50 +00009770ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9771 if (opaque_decl) {
9772 clang::NamedDecl *nd =
9773 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9774 if (nd != nullptr)
9775 return ConstString(nd->getDeclName().getAsString());
9776 }
9777 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009778}
9779
Kate Stoneb9c1b512016-09-06 20:57:50 +00009780ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9781 if (opaque_decl) {
9782 clang::NamedDecl *nd =
9783 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9784 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9785 clang::MangleContext *mc = getMangleContext();
9786 if (mc && mc->shouldMangleCXXName(nd)) {
9787 llvm::SmallVector<char, 1024> buf;
9788 llvm::raw_svector_ostream llvm_ostrm(buf);
9789 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9790 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9791 Ctor_Complete, llvm_ostrm);
9792 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9793 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9794 Dtor_Complete, llvm_ostrm);
9795 } else {
9796 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009797 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009798 if (buf.size() > 0)
9799 return ConstString(buf.data(), buf.size());
9800 }
Greg Claytonfe689042015-11-10 17:47:04 +00009801 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009802 }
9803 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009804}
9805
Kate Stoneb9c1b512016-09-06 20:57:50 +00009806CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9807 if (opaque_decl)
9808 return CompilerDeclContext(this,
9809 ((clang::Decl *)opaque_decl)->getDeclContext());
9810 else
9811 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009812}
9813
Kate Stoneb9c1b512016-09-06 20:57:50 +00009814CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9815 if (clang::FunctionDecl *func_decl =
9816 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9817 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9818 if (clang::ObjCMethodDecl *objc_method =
9819 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9820 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9821 else
Greg Claytonfe689042015-11-10 17:47:04 +00009822 return CompilerType();
9823}
9824
Kate Stoneb9c1b512016-09-06 20:57:50 +00009825size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9826 if (clang::FunctionDecl *func_decl =
9827 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9828 return func_decl->param_size();
9829 if (clang::ObjCMethodDecl *objc_method =
9830 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9831 return objc_method->param_size();
9832 else
9833 return 0;
9834}
9835
9836CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9837 size_t idx) {
9838 if (clang::FunctionDecl *func_decl =
9839 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9840 if (idx < func_decl->param_size()) {
9841 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9842 if (var_decl)
9843 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9844 }
9845 } else if (clang::ObjCMethodDecl *objc_method =
9846 llvm::dyn_cast<clang::ObjCMethodDecl>(
9847 (clang::Decl *)opaque_decl)) {
9848 if (idx < objc_method->param_size())
9849 return CompilerType(
9850 this,
9851 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9852 }
9853 return CompilerType();
9854}
9855
Paul Hermand628cbb2015-09-15 23:44:17 +00009856//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00009857// CompilerDeclContext functions
9858//----------------------------------------------------------------------
9859
Kate Stoneb9c1b512016-09-06 20:57:50 +00009860std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9861 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9862 std::vector<CompilerDecl> found_decls;
9863 if (opaque_decl_ctx) {
9864 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9865 std::set<DeclContext *> searched;
9866 std::multimap<DeclContext *, DeclContext *> search_queue;
9867 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009868
Kate Stoneb9c1b512016-09-06 20:57:50 +00009869 for (clang::DeclContext *decl_context = root_decl_ctx;
9870 decl_context != nullptr && found_decls.empty();
9871 decl_context = decl_context->getParent()) {
9872 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +00009873
Kate Stoneb9c1b512016-09-06 20:57:50 +00009874 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9875 it++) {
9876 if (!searched.insert(it->second).second)
9877 continue;
9878 symbol_file->ParseDeclsForContext(
9879 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +00009880
Kate Stoneb9c1b512016-09-06 20:57:50 +00009881 for (clang::Decl *child : it->second->decls()) {
9882 if (clang::UsingDirectiveDecl *ud =
9883 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9884 if (ignore_using_decls)
9885 continue;
9886 clang::DeclContext *from = ud->getCommonAncestor();
9887 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9888 search_queue.insert(
9889 std::make_pair(from, ud->getNominatedNamespace()));
9890 } else if (clang::UsingDecl *ud =
9891 llvm::dyn_cast<clang::UsingDecl>(child)) {
9892 if (ignore_using_decls)
9893 continue;
9894 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9895 clang::Decl *target = usd->getTargetDecl();
9896 if (clang::NamedDecl *nd =
9897 llvm::dyn_cast<clang::NamedDecl>(target)) {
9898 IdentifierInfo *ii = nd->getIdentifier();
9899 if (ii != nullptr &&
9900 ii->getName().equals(name.AsCString(nullptr)))
9901 found_decls.push_back(CompilerDecl(this, nd));
9902 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009903 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009904 } else if (clang::NamedDecl *nd =
9905 llvm::dyn_cast<clang::NamedDecl>(child)) {
9906 IdentifierInfo *ii = nd->getIdentifier();
9907 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9908 found_decls.push_back(CompilerDecl(this, nd));
9909 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009910 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009911 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009912 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009913 }
9914 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +00009915}
9916
Dawn Perchikb5925782015-12-12 19:31:41 +00009917// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009918// and return the number of levels it took to find it, or
Adrian Prantl05097242018-04-30 16:49:04 +00009919// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
9920// declaration, its name and/or type, if set, will be used to check that the
9921// decl found in the scope is a match.
Dawn Perchikb5925782015-12-12 19:31:41 +00009922//
Kate Stoneb9c1b512016-09-06 20:57:50 +00009923// The optional name is required by languages (like C++) to handle using
Adrian Prantl05097242018-04-30 16:49:04 +00009924// declarations like:
Dawn Perchikb5925782015-12-12 19:31:41 +00009925//
9926// void poo();
9927// namespace ns {
9928// void foo();
9929// void goo();
9930// }
9931// void bar() {
9932// using ns::foo;
9933// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
9934// // LLDB_INVALID_DECL_LEVEL for 'goo'.
9935// }
9936//
9937// The optional type is useful in the case that there's a specific overload
9938// that we're looking for that might otherwise be shadowed, like:
9939//
9940// void foo(int);
9941// namespace ns {
9942// void foo();
9943// }
9944// void bar() {
9945// using ns::foo;
9946// // CountDeclLevels returns 0 for { 'foo', void() },
9947// // 1 for { 'foo', void(int) }, and
9948// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
9949// }
9950//
9951// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +00009952// function at file scope will return the same level as a function at global
Adrian Prantl05097242018-04-30 16:49:04 +00009953// scope. Ideally we'd like to treat the file scope as an additional scope just
9954// below the global scope. More work needs to be done to recognise that, if
9955// the decl we're trying to look up is static, we should compare its source
9956// file with that of the current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009957uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
9958 clang::DeclContext *child_decl_ctx,
9959 ConstString *child_name,
9960 CompilerType *child_type) {
9961 if (frame_decl_ctx) {
9962 std::set<DeclContext *> searched;
9963 std::multimap<DeclContext *, DeclContext *> search_queue;
9964 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +00009965
Kate Stoneb9c1b512016-09-06 20:57:50 +00009966 // Get the lookup scope for the decl we're trying to find.
9967 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +00009968
Kate Stoneb9c1b512016-09-06 20:57:50 +00009969 // Look for it in our scope's decl context and its parents.
9970 uint32_t level = 0;
9971 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
9972 decl_ctx = decl_ctx->getParent()) {
9973 if (!decl_ctx->isLookupContext())
9974 continue;
9975 if (decl_ctx == parent_decl_ctx)
9976 // Found it!
9977 return level;
9978 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
9979 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
9980 it++) {
9981 if (searched.find(it->second) != searched.end())
9982 continue;
9983
9984 // Currently DWARF has one shared translation unit for all Decls at top
Adrian Prantl05097242018-04-30 16:49:04 +00009985 // level, so this would erroneously find using statements anywhere. So
9986 // don't look at the top-level translation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009987 // TODO fix this and add a testcase that depends on it.
9988
9989 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
9990 continue;
9991
9992 searched.insert(it->second);
9993 symbol_file->ParseDeclsForContext(
9994 CompilerDeclContext(this, it->second));
9995
9996 for (clang::Decl *child : it->second->decls()) {
9997 if (clang::UsingDirectiveDecl *ud =
9998 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9999 clang::DeclContext *ns = ud->getNominatedNamespace();
10000 if (ns == parent_decl_ctx)
10001 // Found it!
10002 return level;
10003 clang::DeclContext *from = ud->getCommonAncestor();
10004 if (searched.find(ns) == searched.end())
10005 search_queue.insert(std::make_pair(from, ns));
10006 } else if (child_name) {
10007 if (clang::UsingDecl *ud =
10008 llvm::dyn_cast<clang::UsingDecl>(child)) {
10009 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10010 clang::Decl *target = usd->getTargetDecl();
10011 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
10012 if (!nd)
10013 continue;
10014 // Check names.
10015 IdentifierInfo *ii = nd->getIdentifier();
10016 if (ii == nullptr ||
10017 !ii->getName().equals(child_name->AsCString(nullptr)))
10018 continue;
10019 // Check types, if one was provided.
10020 if (child_type) {
10021 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
10022 if (!AreTypesSame(clang_type, *child_type,
10023 /*ignore_qualifiers=*/true))
10024 continue;
10025 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010026 // Found it!
10027 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010028 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010029 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010030 }
Dawn Perchikb5925782015-12-12 19:31:41 +000010031 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010032 }
10033 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +000010034 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000010035 }
10036 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +000010037}
10038
Kate Stoneb9c1b512016-09-06 20:57:50 +000010039bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
10040 if (opaque_decl_ctx)
10041 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
10042 else
Greg Clayton99558cc42015-08-24 23:46:31 +000010043 return false;
10044}
10045
Kate Stoneb9c1b512016-09-06 20:57:50 +000010046ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
10047 if (opaque_decl_ctx) {
10048 clang::NamedDecl *named_decl =
10049 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10050 if (named_decl)
10051 return ConstString(named_decl->getName());
10052 }
10053 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +000010054}
10055
Kate Stoneb9c1b512016-09-06 20:57:50 +000010056ConstString
10057ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
10058 if (opaque_decl_ctx) {
10059 clang::NamedDecl *named_decl =
10060 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10061 if (named_decl)
10062 return ConstString(
10063 llvm::StringRef(named_decl->getQualifiedNameAsString()));
10064 }
10065 return ConstString();
10066}
10067
10068bool ClangASTContext::DeclContextIsClassMethod(
10069 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
10070 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
10071 if (opaque_decl_ctx) {
10072 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10073 if (ObjCMethodDecl *objc_method =
10074 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
10075 if (is_instance_method_ptr)
10076 *is_instance_method_ptr = objc_method->isInstanceMethod();
10077 if (language_ptr)
10078 *language_ptr = eLanguageTypeObjC;
10079 if (language_object_name_ptr)
10080 language_object_name_ptr->SetCString("self");
10081 return true;
10082 } else if (CXXMethodDecl *cxx_method =
10083 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
10084 if (is_instance_method_ptr)
10085 *is_instance_method_ptr = cxx_method->isInstance();
10086 if (language_ptr)
10087 *language_ptr = eLanguageTypeC_plus_plus;
10088 if (language_object_name_ptr)
10089 language_object_name_ptr->SetCString("this");
10090 return true;
10091 } else if (clang::FunctionDecl *function_decl =
10092 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
10093 ClangASTMetadata *metadata =
10094 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
10095 if (metadata && metadata->HasObjectPtr()) {
10096 if (is_instance_method_ptr)
10097 *is_instance_method_ptr = true;
10098 if (language_ptr)
10099 *language_ptr = eLanguageTypeObjC;
10100 if (language_object_name_ptr)
10101 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10102 return true;
10103 }
10104 }
10105 }
10106 return false;
10107}
10108
10109clang::DeclContext *
10110ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10111 if (dc.IsClang())
10112 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10113 return nullptr;
10114}
Greg Clayton99558cc42015-08-24 23:46:31 +000010115
10116ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010117ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10118 if (dc.IsClang())
10119 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10120 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10121 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010122}
10123
10124CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010125ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10126 if (dc.IsClang())
10127 return llvm::dyn_cast<clang::CXXMethodDecl>(
10128 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10129 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010130}
10131
10132clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010133ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10134 if (dc.IsClang())
10135 return llvm::dyn_cast<clang::FunctionDecl>(
10136 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10137 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010138}
10139
10140clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010141ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10142 if (dc.IsClang())
10143 return llvm::dyn_cast<clang::NamespaceDecl>(
10144 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10145 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010146}
10147
10148ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010149ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10150 const void *object) {
10151 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10152 if (ast)
10153 return ClangASTContext::GetMetadata(ast, object);
10154 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010155}
10156
10157clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010158ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10159 ClangASTContext *ast =
10160 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10161 if (ast)
10162 return ast->getASTContext();
10163 return nullptr;
10164}
10165
10166ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10167 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10168 m_target_wp(target.shared_from_this()),
10169 m_persistent_variables(new ClangPersistentVariables) {}
10170
10171UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010172 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010173 Expression::ResultType desired_type,
10174 const EvaluateExpressionOptions &options) {
10175 TargetSP target_sp = m_target_wp.lock();
10176 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010177 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010178
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010179 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010180 desired_type, options);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010181}
10182
Kate Stoneb9c1b512016-09-06 20:57:50 +000010183FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10184 const CompilerType &return_type, const Address &function_address,
10185 const ValueList &arg_value_list, const char *name) {
10186 TargetSP target_sp = m_target_wp.lock();
10187 if (!target_sp)
10188 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010189
Kate Stoneb9c1b512016-09-06 20:57:50 +000010190 Process *process = target_sp->GetProcessSP().get();
10191 if (!process)
10192 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010193
Kate Stoneb9c1b512016-09-06 20:57:50 +000010194 return new ClangFunctionCaller(*process, return_type, function_address,
10195 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010196}
10197
10198UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010199ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10200 const char *name) {
10201 TargetSP target_sp = m_target_wp.lock();
10202 if (!target_sp)
10203 return nullptr;
10204
10205 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010206}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010207
10208PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010209ClangASTContextForExpressions::GetPersistentExpressionState() {
10210 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010211}
Sean Callanan68e44232017-09-28 20:20:25 +000010212
10213clang::ExternalASTMerger &
10214ClangASTContextForExpressions::GetMergerUnchecked() {
Eugene Zemtsova9d928c2017-09-29 03:15:08 +000010215 lldbassert(m_scratch_ast_source_ap != nullptr);
Sean Callanan68e44232017-09-28 20:20:25 +000010216 return m_scratch_ast_source_ap->GetMergerUnchecked();
10217}