blob: 61f63b746a435634917f8bbdd4ae5470b572117f [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"
Ulrich Weigand9521ad22016-04-15 09:55:52 +000079#include "lldb/Core/Scalar.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000080#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000081#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000082#include "lldb/Core/UniqueCStringMap.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000083#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000084#include "lldb/Symbol/ClangASTImporter.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000085#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000086#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000087#include "lldb/Symbol/ClangUtil.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000088#include "lldb/Symbol/ObjectFile.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000089#include "lldb/Symbol/SymbolFile.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000090#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000091#include "lldb/Target/ExecutionContext.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000092#include "lldb/Target/Language.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000093#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham151c0322015-09-15 21:13:50 +000094#include "lldb/Target/Process.h"
95#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000096#include "lldb/Utility/DataExtractor.h"
Sean Callananc530ba92016-05-02 21:15:31 +000097#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000098#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000099#include "lldb/Utility/RegularExpression.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}
Greg Clayton56939cb2015-09-17 22:23:34 +0000127}
128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *>
130 ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000131
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132static ClangASTMap &GetASTMap() {
133 static ClangASTMap *g_map_ptr = nullptr;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000134 static llvm::once_flag g_once_flag;
135 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
137 });
138 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000139}
140
Davide Italiano7e3ef4d2018-03-20 19:46:32 +0000141bool ClangASTContext::IsOperator(const char *name,
142 clang::OverloadedOperatorKind &op_kind) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 if (name == nullptr || name[0] == '\0')
144 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000145
146#define OPERATOR_PREFIX "operator"
147#define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1)
148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 const char *post_op_name = nullptr;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151 bool no_space = true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
154 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 post_op_name = name + OPERATOR_PREFIX_LENGTH;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000157
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 if (post_op_name[0] == ' ') {
159 post_op_name++;
160 no_space = false;
161 }
Pavel Labath1ac2b202016-08-15 14:32:32 +0000162
163#undef OPERATOR_PREFIX
164#undef OPERATOR_PREFIX_LENGTH
165
Adrian Prantl05097242018-04-30 16:49:04 +0000166 // This is an operator, set the overloaded operator kind to invalid in case
167 // this is a conversion operator...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 op_kind = clang::NUM_OVERLOADED_OPERATORS;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000169
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 switch (post_op_name[0]) {
171 default:
172 if (no_space)
173 return false;
174 break;
175 case 'n':
176 if (no_space)
177 return false;
178 if (strcmp(post_op_name, "new") == 0)
179 op_kind = clang::OO_New;
180 else if (strcmp(post_op_name, "new[]") == 0)
181 op_kind = clang::OO_Array_New;
182 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 case 'd':
185 if (no_space)
186 return false;
187 if (strcmp(post_op_name, "delete") == 0)
188 op_kind = clang::OO_Delete;
189 else if (strcmp(post_op_name, "delete[]") == 0)
190 op_kind = clang::OO_Array_Delete;
191 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000192
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 case '+':
194 if (post_op_name[1] == '\0')
195 op_kind = clang::OO_Plus;
196 else if (post_op_name[2] == '\0') {
197 if (post_op_name[1] == '=')
198 op_kind = clang::OO_PlusEqual;
199 else if (post_op_name[1] == '+')
200 op_kind = clang::OO_PlusPlus;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000201 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000203
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 case '-':
205 if (post_op_name[1] == '\0')
206 op_kind = clang::OO_Minus;
207 else if (post_op_name[2] == '\0') {
208 switch (post_op_name[1]) {
209 case '=':
210 op_kind = clang::OO_MinusEqual;
211 break;
212 case '-':
213 op_kind = clang::OO_MinusMinus;
214 break;
215 case '>':
216 op_kind = clang::OO_Arrow;
217 break;
218 }
219 } else if (post_op_name[3] == '\0') {
220 if (post_op_name[2] == '*')
221 op_kind = clang::OO_ArrowStar;
222 break;
223 }
224 break;
225
226 case '*':
227 if (post_op_name[1] == '\0')
228 op_kind = clang::OO_Star;
229 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
230 op_kind = clang::OO_StarEqual;
231 break;
232
233 case '/':
234 if (post_op_name[1] == '\0')
235 op_kind = clang::OO_Slash;
236 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
237 op_kind = clang::OO_SlashEqual;
238 break;
239
240 case '%':
241 if (post_op_name[1] == '\0')
242 op_kind = clang::OO_Percent;
243 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
244 op_kind = clang::OO_PercentEqual;
245 break;
246
247 case '^':
248 if (post_op_name[1] == '\0')
249 op_kind = clang::OO_Caret;
250 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
251 op_kind = clang::OO_CaretEqual;
252 break;
253
254 case '&':
255 if (post_op_name[1] == '\0')
256 op_kind = clang::OO_Amp;
257 else if (post_op_name[2] == '\0') {
258 switch (post_op_name[1]) {
259 case '=':
260 op_kind = clang::OO_AmpEqual;
261 break;
262 case '&':
263 op_kind = clang::OO_AmpAmp;
264 break;
265 }
266 }
267 break;
268
269 case '|':
270 if (post_op_name[1] == '\0')
271 op_kind = clang::OO_Pipe;
272 else if (post_op_name[2] == '\0') {
273 switch (post_op_name[1]) {
274 case '=':
275 op_kind = clang::OO_PipeEqual;
276 break;
277 case '|':
278 op_kind = clang::OO_PipePipe;
279 break;
280 }
281 }
282 break;
283
284 case '~':
285 if (post_op_name[1] == '\0')
286 op_kind = clang::OO_Tilde;
287 break;
288
289 case '!':
290 if (post_op_name[1] == '\0')
291 op_kind = clang::OO_Exclaim;
292 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
293 op_kind = clang::OO_ExclaimEqual;
294 break;
295
296 case '=':
297 if (post_op_name[1] == '\0')
298 op_kind = clang::OO_Equal;
299 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
300 op_kind = clang::OO_EqualEqual;
301 break;
302
303 case '<':
304 if (post_op_name[1] == '\0')
305 op_kind = clang::OO_Less;
306 else if (post_op_name[2] == '\0') {
307 switch (post_op_name[1]) {
308 case '<':
309 op_kind = clang::OO_LessLess;
310 break;
311 case '=':
312 op_kind = clang::OO_LessEqual;
313 break;
314 }
315 } else if (post_op_name[3] == '\0') {
316 if (post_op_name[2] == '=')
317 op_kind = clang::OO_LessLessEqual;
318 }
319 break;
320
321 case '>':
322 if (post_op_name[1] == '\0')
323 op_kind = clang::OO_Greater;
324 else if (post_op_name[2] == '\0') {
325 switch (post_op_name[1]) {
326 case '>':
327 op_kind = clang::OO_GreaterGreater;
328 break;
329 case '=':
330 op_kind = clang::OO_GreaterEqual;
331 break;
332 }
333 } else if (post_op_name[1] == '>' && post_op_name[2] == '=' &&
334 post_op_name[3] == '\0') {
335 op_kind = clang::OO_GreaterGreaterEqual;
336 }
337 break;
338
339 case ',':
340 if (post_op_name[1] == '\0')
341 op_kind = clang::OO_Comma;
342 break;
343
344 case '(':
345 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
346 op_kind = clang::OO_Call;
347 break;
348
349 case '[':
350 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
351 op_kind = clang::OO_Subscript;
352 break;
353 }
354
355 return true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000356}
Enrico Granata5d84a692014-08-19 21:46:37 +0000357
Greg Clayton57ee3062013-07-11 22:46:58 +0000358clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
360 switch (access) {
361 default:
362 break;
363 case eAccessNone:
Greg Clayton8cf05932010-07-22 18:30:50 +0000364 return AS_none;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365 case eAccessPublic:
366 return AS_public;
367 case eAccessPrivate:
368 return AS_private;
369 case eAccessProtected:
370 return AS_protected;
371 }
372 return AS_none;
Greg Clayton8cf05932010-07-22 18:30:50 +0000373}
374
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
376 // FIXME: Cleanup per-file based stuff.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377
Adrian Prantl05097242018-04-30 16:49:04 +0000378 // Set some properties which depend solely on the input kind; it would be
379 // nice to move these to the language standard, and have the driver resolve
380 // the input kind + language standard.
Richard Smith8186cd42017-04-26 22:10:53 +0000381 if (IK.getLanguage() == InputKind::Asm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 Opts.AsmPreprocessor = 1;
Richard Smith8186cd42017-04-26 22:10:53 +0000383 } else if (IK.isObjectiveC()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384 Opts.ObjC1 = Opts.ObjC2 = 1;
385 }
386
387 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
388
389 if (LangStd == LangStandard::lang_unspecified) {
390 // Based on the base language, pick one.
Richard Smith8186cd42017-04-26 22:10:53 +0000391 switch (IK.getLanguage()) {
392 case InputKind::Unknown:
393 case InputKind::LLVM_IR:
394 case InputKind::RenderScript:
David Blaikiea322f362017-01-06 00:38:06 +0000395 llvm_unreachable("Invalid input kind!");
Richard Smith8186cd42017-04-26 22:10:53 +0000396 case InputKind::OpenCL:
Pavel Labath47168542017-04-27 08:49:19 +0000397 LangStd = LangStandard::lang_opencl10;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000399 case InputKind::CUDA:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400 LangStd = LangStandard::lang_cuda;
401 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000402 case InputKind::Asm:
403 case InputKind::C:
404 case InputKind::ObjC:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 LangStd = LangStandard::lang_gnu99;
406 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000407 case InputKind::CXX:
408 case InputKind::ObjCXX:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409 LangStd = LangStandard::lang_gnucxx98;
410 break;
Benjamin Kramer0d97c222018-04-25 13:22:47 +0000411 case InputKind::HIP:
412 LangStd = LangStandard::lang_hip;
413 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416
Kate Stoneb9c1b512016-09-06 20:57:50 +0000417 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
418 Opts.LineComment = Std.hasLineComments();
419 Opts.C99 = Std.isC99();
420 Opts.CPlusPlus = Std.isCPlusPlus();
421 Opts.CPlusPlus11 = Std.isCPlusPlus11();
422 Opts.Digraphs = Std.hasDigraphs();
423 Opts.GNUMode = Std.isGNUMode();
424 Opts.GNUInline = !Std.isC99();
425 Opts.HexFloats = Std.hasHexFloats();
426 Opts.ImplicitInt = Std.hasImplicitInt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429
Kate Stoneb9c1b512016-09-06 20:57:50 +0000430 // OpenCL has some additional defaults.
Pavel Labath47168542017-04-27 08:49:19 +0000431 if (LangStd == LangStandard::lang_opencl10) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432 Opts.OpenCL = 1;
433 Opts.AltiVec = 1;
434 Opts.CXXOperatorNames = 1;
435 Opts.LaxVectorConversions = 1;
436 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 // OpenCL and C++ both have bool, true, false keywords.
439 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000442
Adrian Prantl05097242018-04-30 16:49:04 +0000443 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is
444 // specified, or -std is set to a conforming mode.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445 Opts.Trigraphs = !Opts.GNUMode;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447 Opts.OptimizeSize = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 // FIXME: Eliminate this dependency.
450 // unsigned Opt =
451 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
452 // Opts.Optimize = Opt != 0;
453 unsigned Opt = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455 // This is the __NO_INLINE__ define, which just depends on things like the
456 // optimization level and -fno-inline, not actually whether the backend has
457 // inlining enabled.
458 //
459 // FIXME: This is affected by other options (-fno-inline).
460 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461}
462
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463ClangASTContext::ClangASTContext(const char *target_triple)
464 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_ap(),
465 m_language_options_ap(), m_source_manager_ap(), m_diagnostics_engine_ap(),
466 m_target_options_rp(), m_target_info_ap(), m_identifier_table_ap(),
467 m_selector_table_ap(), m_builtins_ap(), m_callback_tag_decl(nullptr),
468 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
469 m_pointer_byte_size(0), m_ast_owned(false) {
470 if (target_triple && target_triple[0])
471 SetTargetTriple(target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472}
473
474//----------------------------------------------------------------------
475// Destructor
476//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477ClangASTContext::~ClangASTContext() { Finalize(); }
478
479ConstString ClangASTContext::GetPluginNameStatic() {
480 return ConstString("clang");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481}
482
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483ConstString ClangASTContext::GetPluginName() {
484 return ClangASTContext::GetPluginNameStatic();
Greg Clayton56939cb2015-09-17 22:23:34 +0000485}
486
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487uint32_t ClangASTContext::GetPluginVersion() { return 1; }
Greg Clayton56939cb2015-09-17 22:23:34 +0000488
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
490 lldb_private::Module *module,
491 Target *target) {
492 if (ClangASTContextSupportsLanguage(language)) {
493 ArchSpec arch;
494 if (module)
495 arch = module->GetArchitecture();
496 else if (target)
497 arch = target->GetArchitecture();
Greg Clayton56939cb2015-09-17 22:23:34 +0000498
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499 if (arch.IsValid()) {
500 ArchSpec fixed_arch = arch;
501 // LLVM wants this to be set to iOS or MacOSX; if we're working on
502 // a bare-boards type image, change the triple for llvm's benefit.
503 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
504 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
505 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
506 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
507 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
508 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
509 } else {
510 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
Greg Clayton56939cb2015-09-17 22:23:34 +0000511 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000513
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 if (module) {
515 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
516 if (ast_sp) {
517 ast_sp->SetArchitecture(fixed_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000518 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519 return ast_sp;
520 } else if (target && target->IsValid()) {
521 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
522 new ClangASTContextForExpressions(*target));
523 if (ast_sp) {
524 ast_sp->SetArchitecture(fixed_arch);
525 ast_sp->m_scratch_ast_source_ap.reset(
526 new ClangASTSource(target->shared_from_this()));
Sean Callanan68e44232017-09-28 20:20:25 +0000527 lldbassert(ast_sp->getFileManager());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528 ast_sp->m_scratch_ast_source_ap->InstallASTContext(
Sean Callanan68e44232017-09-28 20:20:25 +0000529 *ast_sp->getASTContext(), *ast_sp->getFileManager(), true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000530 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
531 ast_sp->m_scratch_ast_source_ap->CreateProxy());
532 ast_sp->SetExternalSource(proxy_ast_source);
533 return ast_sp;
534 }
535 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537 }
538 return lldb::TypeSystemSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000539}
540
Kate Stoneb9c1b512016-09-06 20:57:50 +0000541void ClangASTContext::EnumerateSupportedLanguages(
542 std::set<lldb::LanguageType> &languages_for_types,
543 std::set<lldb::LanguageType> &languages_for_expressions) {
544 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
545 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
546 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
547 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
548 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
549 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
550
551 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
552 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
553 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
554 lldb::eLanguageTypeC_plus_plus_14});
555
556 languages_for_types.insert(s_supported_languages_for_types.begin(),
557 s_supported_languages_for_types.end());
558 languages_for_expressions.insert(
559 s_supported_languages_for_expressions.begin(),
560 s_supported_languages_for_expressions.end());
Enrico Granata5d84a692014-08-19 21:46:37 +0000561}
562
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563void ClangASTContext::Initialize() {
564 PluginManager::RegisterPlugin(GetPluginNameStatic(),
565 "clang base AST context plug-in",
566 CreateInstance, EnumerateSupportedLanguages);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000567}
568
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569void ClangASTContext::Terminate() {
570 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000571}
572
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573void ClangASTContext::Finalize() {
574 if (m_ast_ap.get()) {
575 GetASTMap().Erase(m_ast_ap.get());
576 if (!m_ast_owned)
577 m_ast_ap.release();
578 }
579
580 m_builtins_ap.reset();
581 m_selector_table_ap.reset();
582 m_identifier_table_ap.reset();
583 m_target_info_ap.reset();
584 m_target_options_rp.reset();
585 m_diagnostics_engine_ap.reset();
586 m_source_manager_ap.reset();
587 m_language_options_ap.reset();
588 m_ast_ap.reset();
589 m_scratch_ast_source_ap.reset();
590}
591
592void ClangASTContext::Clear() {
593 m_ast_ap.reset();
594 m_language_options_ap.reset();
595 m_source_manager_ap.reset();
596 m_diagnostics_engine_ap.reset();
597 m_target_options_rp.reset();
598 m_target_info_ap.reset();
599 m_identifier_table_ap.reset();
600 m_selector_table_ap.reset();
601 m_builtins_ap.reset();
602 m_pointer_byte_size = 0;
603}
604
605const char *ClangASTContext::GetTargetTriple() {
606 return m_target_triple.c_str();
607}
608
609void ClangASTContext::SetTargetTriple(const char *target_triple) {
610 Clear();
611 m_target_triple.assign(target_triple);
612}
613
614void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
615 SetTargetTriple(arch.GetTriple().str().c_str());
616}
617
618bool ClangASTContext::HasExternalSource() {
619 ASTContext *ast = getASTContext();
620 if (ast)
621 return ast->getExternalSource() != nullptr;
622 return false;
623}
624
625void ClangASTContext::SetExternalSource(
626 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) {
627 ASTContext *ast = getASTContext();
628 if (ast) {
629 ast->setExternalSource(ast_source_ap);
630 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
631 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
632 }
633}
634
635void ClangASTContext::RemoveExternalSource() {
636 ASTContext *ast = getASTContext();
637
638 if (ast) {
639 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
640 ast->setExternalSource(empty_ast_source_ap);
641 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
642 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
643 }
644}
645
646void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
647 if (!m_ast_owned) {
648 m_ast_ap.release();
649 }
650 m_ast_owned = false;
651 m_ast_ap.reset(ast_ctx);
652 GetASTMap().Insert(ast_ctx, this);
653}
654
655ASTContext *ClangASTContext::getASTContext() {
656 if (m_ast_ap.get() == nullptr) {
657 m_ast_owned = true;
658 m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
659 *getIdentifierTable(), *getSelectorTable(),
660 *getBuiltinContext()));
661
662 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
663
664 // This can be NULL if we don't know anything about the architecture or if
Adrian Prantl05097242018-04-30 16:49:04 +0000665 // the target for an architecture isn't enabled in the llvm/clang that we
666 // built
Kate Stoneb9c1b512016-09-06 20:57:50 +0000667 TargetInfo *target_info = getTargetInfo();
668 if (target_info)
669 m_ast_ap->InitBuiltinTypes(*target_info);
670
671 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
672 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
673 // m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000674 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000675
676 GetASTMap().Insert(m_ast_ap.get(), this);
677
678 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap(
679 new ClangExternalASTSourceCallbacks(
680 ClangASTContext::CompleteTagDecl,
681 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
682 ClangASTContext::LayoutRecordType, this));
683 SetExternalSource(ast_source_ap);
684 }
685 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686}
687
Kate Stoneb9c1b512016-09-06 20:57:50 +0000688ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
689 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
690 return clang_ast;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000691}
692
Kate Stoneb9c1b512016-09-06 20:57:50 +0000693Builtin::Context *ClangASTContext::getBuiltinContext() {
694 if (m_builtins_ap.get() == nullptr)
695 m_builtins_ap.reset(new Builtin::Context());
696 return m_builtins_ap.get();
Sean Callanan79439e82010-11-18 02:56:27 +0000697}
698
Kate Stoneb9c1b512016-09-06 20:57:50 +0000699IdentifierTable *ClangASTContext::getIdentifierTable() {
700 if (m_identifier_table_ap.get() == nullptr)
701 m_identifier_table_ap.reset(
702 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
703 return m_identifier_table_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704}
705
Kate Stoneb9c1b512016-09-06 20:57:50 +0000706LangOptions *ClangASTContext::getLanguageOptions() {
707 if (m_language_options_ap.get() == nullptr) {
708 m_language_options_ap.reset(new LangOptions());
Richard Smith8186cd42017-04-26 22:10:53 +0000709 ParseLangArgs(*m_language_options_ap, InputKind::ObjCXX, GetTargetTriple());
710 // InitializeLangOptions(*m_language_options_ap, InputKind::ObjCXX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000711 }
712 return m_language_options_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713}
714
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715SelectorTable *ClangASTContext::getSelectorTable() {
716 if (m_selector_table_ap.get() == nullptr)
717 m_selector_table_ap.reset(new SelectorTable());
718 return m_selector_table_ap.get();
Greg Claytonfe689042015-11-10 17:47:04 +0000719}
720
Kate Stoneb9c1b512016-09-06 20:57:50 +0000721clang::FileManager *ClangASTContext::getFileManager() {
722 if (m_file_manager_ap.get() == nullptr) {
723 clang::FileSystemOptions file_system_options;
724 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
725 }
726 return m_file_manager_ap.get();
727}
728
729clang::SourceManager *ClangASTContext::getSourceManager() {
730 if (m_source_manager_ap.get() == nullptr)
731 m_source_manager_ap.reset(
732 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
733 return m_source_manager_ap.get();
734}
735
736clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
737 if (m_diagnostics_engine_ap.get() == nullptr) {
738 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
739 m_diagnostics_engine_ap.reset(
740 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
741 }
742 return m_diagnostics_engine_ap.get();
743}
744
745clang::MangleContext *ClangASTContext::getMangleContext() {
746 if (m_mangle_ctx_ap.get() == nullptr)
747 m_mangle_ctx_ap.reset(getASTContext()->createMangleContext());
748 return m_mangle_ctx_ap.get();
749}
750
751class NullDiagnosticConsumer : public DiagnosticConsumer {
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000752public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000753 NullDiagnosticConsumer() {
754 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
755 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000756
Kate Stoneb9c1b512016-09-06 20:57:50 +0000757 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
758 const clang::Diagnostic &info) {
759 if (m_log) {
760 llvm::SmallVector<char, 32> diag_str(10);
761 info.FormatDiagnostic(diag_str);
762 diag_str.push_back('\0');
763 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000764 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000765 }
766
767 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
768 return new NullDiagnosticConsumer();
769 }
770
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000771private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000772 Log *m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000773};
774
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
776 if (m_diagnostic_consumer_ap.get() == nullptr)
777 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
778
779 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000780}
781
Kate Stoneb9c1b512016-09-06 20:57:50 +0000782std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
783 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) {
784 m_target_options_rp = std::make_shared<clang::TargetOptions>();
785 if (m_target_options_rp.get() != nullptr)
786 m_target_options_rp->Triple = m_target_triple;
787 }
788 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000789}
790
Kate Stoneb9c1b512016-09-06 20:57:50 +0000791TargetInfo *ClangASTContext::getTargetInfo() {
792 // target_triple should be something like "x86_64-apple-macosx"
793 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
794 m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
795 getTargetOptions()));
796 return m_target_info_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797}
798
799#pragma mark Basic Types
800
Kate Stoneb9c1b512016-09-06 20:57:50 +0000801static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
802 ASTContext *ast, QualType qual_type) {
803 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
804 if (qual_type_bit_size == bit_size)
805 return true;
806 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000807}
Greg Clayton56939cb2015-09-17 22:23:34 +0000808
Greg Claytona1e5dc82015-08-11 22:53:00 +0000809CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000810ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
811 size_t bit_size) {
812 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
813 getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000814}
815
Kate Stoneb9c1b512016-09-06 20:57:50 +0000816CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
817 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
818 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000819 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000820 switch (encoding) {
821 case eEncodingInvalid:
822 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
823 return CompilerType(ast, ast->VoidPtrTy);
824 break;
825
826 case eEncodingUint:
827 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
828 return CompilerType(ast, ast->UnsignedCharTy);
829 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
830 return CompilerType(ast, ast->UnsignedShortTy);
831 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
832 return CompilerType(ast, ast->UnsignedIntTy);
833 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
834 return CompilerType(ast, ast->UnsignedLongTy);
835 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
836 return CompilerType(ast, ast->UnsignedLongLongTy);
837 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
838 return CompilerType(ast, ast->UnsignedInt128Ty);
839 break;
840
841 case eEncodingSint:
842 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
843 return CompilerType(ast, ast->SignedCharTy);
844 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
845 return CompilerType(ast, ast->ShortTy);
846 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
847 return CompilerType(ast, ast->IntTy);
848 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
849 return CompilerType(ast, ast->LongTy);
850 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
851 return CompilerType(ast, ast->LongLongTy);
852 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
853 return CompilerType(ast, ast->Int128Ty);
854 break;
855
856 case eEncodingIEEE754:
857 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
858 return CompilerType(ast, ast->FloatTy);
859 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
860 return CompilerType(ast, ast->DoubleTy);
861 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
862 return CompilerType(ast, ast->LongDoubleTy);
863 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
864 return CompilerType(ast, ast->HalfTy);
865 break;
866
867 case eEncodingVector:
868 // Sanity check that bit_size is a multiple of 8's.
869 if (bit_size && !(bit_size & 0x7u))
870 return CompilerType(
871 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
872 break;
873 }
874
875 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000876}
877
Greg Clayton57ee3062013-07-11 22:46:58 +0000878lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000879ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) {
880 if (name) {
881 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
882 static TypeNameToBasicTypeMap g_type_map;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000883 static llvm::once_flag g_once_flag;
884 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000885 // "void"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000886 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000887
888 // "char"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000889 g_type_map.Append(ConstString("char"), eBasicTypeChar);
890 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
891 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
892 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
893 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
894 g_type_map.Append(ConstString("unsigned wchar_t"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000895 eBasicTypeUnsignedWChar);
896 // "short"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000897 g_type_map.Append(ConstString("short"), eBasicTypeShort);
898 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
899 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
900 g_type_map.Append(ConstString("unsigned short int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901 eBasicTypeUnsignedShort);
902
903 // "int"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000904 g_type_map.Append(ConstString("int"), eBasicTypeInt);
905 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
906 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
907 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000908
909 // "long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000910 g_type_map.Append(ConstString("long"), eBasicTypeLong);
911 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
912 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
913 g_type_map.Append(ConstString("unsigned long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000914 eBasicTypeUnsignedLong);
915
916 // "long long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000917 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
918 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
919 g_type_map.Append(ConstString("unsigned long long"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000920 eBasicTypeUnsignedLongLong);
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000921 g_type_map.Append(ConstString("unsigned long long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000922 eBasicTypeUnsignedLongLong);
923
924 // "int128"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000925 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
926 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000927
928 // Miscellaneous
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000929 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
930 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
931 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
932 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
933 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
934 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
935 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000936 g_type_map.Sort();
937 });
938
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000939 return g_type_map.Find(name, eBasicTypeInvalid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000940 }
941 return eBasicTypeInvalid;
Greg Clayton57ee3062013-07-11 22:46:58 +0000942}
943
Kate Stoneb9c1b512016-09-06 20:57:50 +0000944CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
945 const ConstString &name) {
946 if (ast) {
947 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
948 return ClangASTContext::GetBasicType(ast, basic_type);
949 }
950 return CompilerType();
951}
952
953uint32_t ClangASTContext::GetPointerByteSize() {
954 if (m_pointer_byte_size == 0)
955 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid)
956 .GetPointerType()
957 .GetByteSize(nullptr);
958 return m_pointer_byte_size;
959}
960
961CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
962 return GetBasicType(getASTContext(), basic_type);
963}
964
965CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
966 lldb::BasicType basic_type) {
967 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000968 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000969 lldb::opaque_compiler_type_t clang_type =
970 GetOpaqueCompilerType(ast, basic_type);
971
972 if (clang_type)
973 return CompilerType(GetASTContext(ast), clang_type);
974 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +0000975}
976
Kate Stoneb9c1b512016-09-06 20:57:50 +0000977CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
978 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
979 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +0000980
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981#define streq(a, b) strcmp(a, b) == 0
982 assert(ast != nullptr);
983 if (ast) {
984 switch (dw_ate) {
985 default:
986 break;
Greg Clayton57ee3062013-07-11 22:46:58 +0000987
Kate Stoneb9c1b512016-09-06 20:57:50 +0000988 case DW_ATE_address:
989 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
990 return CompilerType(ast, ast->VoidPtrTy);
991 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +0000992
Kate Stoneb9c1b512016-09-06 20:57:50 +0000993 case DW_ATE_boolean:
994 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
995 return CompilerType(ast, ast->BoolTy);
996 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
997 return CompilerType(ast, ast->UnsignedCharTy);
998 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
999 return CompilerType(ast, ast->UnsignedShortTy);
1000 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1001 return CompilerType(ast, ast->UnsignedIntTy);
1002 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001003
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004 case DW_ATE_lo_user:
1005 // This has been seen to mean DW_AT_complex_integer
1006 if (type_name) {
1007 if (::strstr(type_name, "complex")) {
1008 CompilerType complex_int_clang_type =
1009 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1010 bit_size / 2);
1011 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1012 complex_int_clang_type)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001013 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014 }
1015 break;
1016
1017 case DW_ATE_complex_float:
1018 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1019 return CompilerType(ast, ast->FloatComplexTy);
1020 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1021 return CompilerType(ast, ast->DoubleComplexTy);
1022 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1023 return CompilerType(ast, ast->LongDoubleComplexTy);
1024 else {
1025 CompilerType complex_float_clang_type =
1026 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1027 bit_size / 2);
1028 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1029 complex_float_clang_type)));
1030 }
1031 break;
1032
1033 case DW_ATE_float:
1034 if (streq(type_name, "float") &&
1035 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1036 return CompilerType(ast, ast->FloatTy);
1037 if (streq(type_name, "double") &&
1038 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1039 return CompilerType(ast, ast->DoubleTy);
1040 if (streq(type_name, "long double") &&
1041 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1042 return CompilerType(ast, ast->LongDoubleTy);
1043 // Fall back to not requiring a name match
1044 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1045 return CompilerType(ast, ast->FloatTy);
1046 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1047 return CompilerType(ast, ast->DoubleTy);
1048 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1049 return CompilerType(ast, ast->LongDoubleTy);
1050 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1051 return CompilerType(ast, ast->HalfTy);
1052 break;
1053
1054 case DW_ATE_signed:
1055 if (type_name) {
1056 if (streq(type_name, "wchar_t") &&
1057 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1058 (getTargetInfo() &&
1059 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1060 return CompilerType(ast, ast->WCharTy);
1061 if (streq(type_name, "void") &&
1062 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1063 return CompilerType(ast, ast->VoidTy);
1064 if (strstr(type_name, "long long") &&
1065 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1066 return CompilerType(ast, ast->LongLongTy);
1067 if (strstr(type_name, "long") &&
1068 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1069 return CompilerType(ast, ast->LongTy);
1070 if (strstr(type_name, "short") &&
1071 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1072 return CompilerType(ast, ast->ShortTy);
1073 if (strstr(type_name, "char")) {
1074 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1075 return CompilerType(ast, ast->CharTy);
1076 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1077 return CompilerType(ast, ast->SignedCharTy);
1078 }
1079 if (strstr(type_name, "int")) {
1080 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1081 return CompilerType(ast, ast->IntTy);
1082 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1083 return CompilerType(ast, ast->Int128Ty);
1084 }
1085 }
1086 // We weren't able to match up a type name, just search by size
1087 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1088 return CompilerType(ast, ast->CharTy);
1089 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1090 return CompilerType(ast, ast->ShortTy);
1091 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1092 return CompilerType(ast, ast->IntTy);
1093 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1094 return CompilerType(ast, ast->LongTy);
1095 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1096 return CompilerType(ast, ast->LongLongTy);
1097 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1098 return CompilerType(ast, ast->Int128Ty);
1099 break;
1100
1101 case DW_ATE_signed_char:
1102 if (ast->getLangOpts().CharIsSigned && type_name &&
1103 streq(type_name, "char")) {
1104 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1105 return CompilerType(ast, ast->CharTy);
1106 }
1107 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1108 return CompilerType(ast, ast->SignedCharTy);
1109 break;
1110
1111 case DW_ATE_unsigned:
1112 if (type_name) {
1113 if (streq(type_name, "wchar_t")) {
1114 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1115 if (!(getTargetInfo() &&
1116 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1117 return CompilerType(ast, ast->WCharTy);
1118 }
1119 }
1120 if (strstr(type_name, "long long")) {
1121 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1122 return CompilerType(ast, ast->UnsignedLongLongTy);
1123 } else if (strstr(type_name, "long")) {
1124 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1125 return CompilerType(ast, ast->UnsignedLongTy);
1126 } else if (strstr(type_name, "short")) {
1127 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1128 return CompilerType(ast, ast->UnsignedShortTy);
1129 } else if (strstr(type_name, "char")) {
1130 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1131 return CompilerType(ast, ast->UnsignedCharTy);
1132 } else if (strstr(type_name, "int")) {
1133 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1134 return CompilerType(ast, ast->UnsignedIntTy);
1135 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1136 return CompilerType(ast, ast->UnsignedInt128Ty);
1137 }
1138 }
1139 // We weren't able to match up a type name, just search by size
1140 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1141 return CompilerType(ast, ast->UnsignedCharTy);
1142 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1143 return CompilerType(ast, ast->UnsignedShortTy);
1144 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1145 return CompilerType(ast, ast->UnsignedIntTy);
1146 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1147 return CompilerType(ast, ast->UnsignedLongTy);
1148 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1149 return CompilerType(ast, ast->UnsignedLongLongTy);
1150 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1151 return CompilerType(ast, ast->UnsignedInt128Ty);
1152 break;
1153
1154 case DW_ATE_unsigned_char:
1155 if (!ast->getLangOpts().CharIsSigned && type_name &&
1156 streq(type_name, "char")) {
1157 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1158 return CompilerType(ast, ast->CharTy);
1159 }
1160 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1161 return CompilerType(ast, ast->UnsignedCharTy);
1162 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1163 return CompilerType(ast, ast->UnsignedShortTy);
1164 break;
1165
1166 case DW_ATE_imaginary_float:
1167 break;
1168
1169 case DW_ATE_UTF:
1170 if (type_name) {
1171 if (streq(type_name, "char16_t")) {
1172 return CompilerType(ast, ast->Char16Ty);
1173 } else if (streq(type_name, "char32_t")) {
1174 return CompilerType(ast, ast->Char32Ty);
1175 }
1176 }
1177 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001178 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001179 }
1180 // This assert should fire for anything that we don't catch above so we know
1181 // to fix any issues we run into.
1182 if (type_name) {
1183 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1184 "DW_TAG_base_type '%s' encoded with "
1185 "DW_ATE = 0x%x, bit_size = %u\n",
1186 type_name, dw_ate, bit_size);
1187 } else {
1188 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1189 "DW_TAG_base_type encoded with "
1190 "DW_ATE = 0x%x, bit_size = %u\n",
1191 dw_ate, bit_size);
1192 }
1193 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001194}
1195
Kate Stoneb9c1b512016-09-06 20:57:50 +00001196CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1197 if (ast)
1198 return CompilerType(ast, ast->UnknownAnyTy);
1199 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001200}
1201
Kate Stoneb9c1b512016-09-06 20:57:50 +00001202CompilerType ClangASTContext::GetCStringType(bool is_const) {
1203 ASTContext *ast = getASTContext();
1204 QualType char_type(ast->CharTy);
1205
1206 if (is_const)
1207 char_type.addConst();
1208
1209 return CompilerType(ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001210}
1211
Sean Callanan09ab4b72011-11-30 22:11:59 +00001212clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001213ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1214 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001215}
1216
Kate Stoneb9c1b512016-09-06 20:57:50 +00001217clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1218 clang::Decl *source_decl) {
1219 FileSystemOptions file_system_options;
1220 FileManager file_manager(file_system_options);
1221 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1222
1223 return importer.Import(source_decl);
Greg Clayton526e5af2010-11-13 03:52:47 +00001224}
1225
Kate Stoneb9c1b512016-09-06 20:57:50 +00001226bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1227 bool ignore_qualifiers) {
1228 ClangASTContext *ast =
1229 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1230 if (!ast || ast != type2.GetTypeSystem())
1231 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001232
Kate Stoneb9c1b512016-09-06 20:57:50 +00001233 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1234 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001235
Kate Stoneb9c1b512016-09-06 20:57:50 +00001236 QualType type1_qual = ClangUtil::GetQualType(type1);
1237 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001238
Kate Stoneb9c1b512016-09-06 20:57:50 +00001239 if (ignore_qualifiers) {
1240 type1_qual = type1_qual.getUnqualifiedType();
1241 type2_qual = type2_qual.getUnqualifiedType();
1242 }
1243
1244 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001245}
1246
Kate Stoneb9c1b512016-09-06 20:57:50 +00001247CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1248 if (clang::ObjCInterfaceDecl *interface_decl =
1249 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1250 return GetTypeForDecl(interface_decl);
1251 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1252 return GetTypeForDecl(tag_decl);
1253 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001254}
1255
Kate Stoneb9c1b512016-09-06 20:57:50 +00001256CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001257 // No need to call the getASTContext() accessor (which can create the AST if
1258 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259 // AST if our AST didn't already exist...
1260 ASTContext *ast = &decl->getASTContext();
1261 if (ast)
1262 return CompilerType(ast, ast->getTagDeclType(decl));
1263 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001264}
1265
Kate Stoneb9c1b512016-09-06 20:57:50 +00001266CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00001267 // No need to call the getASTContext() accessor (which can create the AST if
1268 // it isn't created yet, because we can't have created a decl in this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269 // AST if our AST didn't already exist...
1270 ASTContext *ast = &decl->getASTContext();
1271 if (ast)
1272 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1273 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001274}
1275
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001276#pragma mark Structure, Unions, Classes
1277
Kate Stoneb9c1b512016-09-06 20:57:50 +00001278CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1279 AccessType access_type,
1280 const char *name, int kind,
1281 LanguageType language,
1282 ClangASTMetadata *metadata) {
1283 ASTContext *ast = getASTContext();
1284 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001285
Kate Stoneb9c1b512016-09-06 20:57:50 +00001286 if (decl_ctx == nullptr)
1287 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001288
Kate Stoneb9c1b512016-09-06 20:57:50 +00001289 if (language == eLanguageTypeObjC ||
1290 language == eLanguageTypeObjC_plus_plus) {
1291 bool isForwardDecl = true;
1292 bool isInternal = false;
1293 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1294 }
Greg Clayton9e409562010-07-28 02:04:09 +00001295
Kate Stoneb9c1b512016-09-06 20:57:50 +00001296 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
Adrian Prantl05097242018-04-30 16:49:04 +00001297 // we will need to update this code. I was told to currently always use the
1298 // CXXRecordDecl class since we often don't know from debug information if
1299 // something is struct or a class, so we default to always use the more
Kate Stoneb9c1b512016-09-06 20:57:50 +00001300 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001301
Kate Stoneb9c1b512016-09-06 20:57:50 +00001302 bool is_anonymous = (!name) || (!name[0]);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001303
Kate Stoneb9c1b512016-09-06 20:57:50 +00001304 CXXRecordDecl *decl = CXXRecordDecl::Create(
1305 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1306 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1307
1308 if (is_anonymous)
1309 decl->setAnonymousStructOrUnion(true);
1310
1311 if (decl) {
1312 if (metadata)
1313 SetMetadata(ast, decl, *metadata);
1314
1315 if (access_type != eAccessNone)
1316 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1317
1318 if (decl_ctx)
1319 decl_ctx->addDecl(decl);
1320
1321 return CompilerType(ast, ast->getTagDeclType(decl));
1322 }
1323 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001324}
1325
Sean Callanan09e91ac2017-05-11 22:08:05 +00001326namespace {
1327 bool IsValueParam(const clang::TemplateArgument &argument) {
1328 return argument.getKind() == TemplateArgument::Integral;
1329 }
1330}
1331
Kate Stoneb9c1b512016-09-06 20:57:50 +00001332static TemplateParameterList *CreateTemplateParameterList(
1333 ASTContext *ast,
1334 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1335 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1336 const bool parameter_pack = false;
1337 const bool is_typename = false;
1338 const unsigned depth = 0;
Sean Callanan09e91ac2017-05-11 22:08:05 +00001339 const size_t num_template_params = template_param_infos.args.size();
1340 DeclContext *const decl_context =
1341 ast->getTranslationUnitDecl(); // Is this the right decl context?,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001342 for (size_t i = 0; i < num_template_params; ++i) {
1343 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001344
Kate Stoneb9c1b512016-09-06 20:57:50 +00001345 IdentifierInfo *identifier_info = nullptr;
1346 if (name && name[0])
1347 identifier_info = &ast->Idents.get(name);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001348 if (IsValueParam(template_param_infos.args[i])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001349 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001350 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001351 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1352 template_param_infos.args[i].getIntegralType(), parameter_pack,
1353 nullptr));
1354
1355 } else {
1356 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001357 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001358 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1359 is_typename, parameter_pack));
1360 }
1361 }
Eugene Zemtsova9d928c2017-09-29 03:15:08 +00001362
Sean Callanan09e91ac2017-05-11 22:08:05 +00001363 if (template_param_infos.packed_args &&
1364 template_param_infos.packed_args->args.size()) {
1365 IdentifierInfo *identifier_info = nullptr;
1366 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1367 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1368 const bool parameter_pack_true = true;
1369 if (IsValueParam(template_param_infos.packed_args->args[0])) {
1370 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1371 *ast, decl_context,
1372 SourceLocation(), SourceLocation(), depth, num_template_params,
1373 identifier_info,
1374 template_param_infos.packed_args->args[0].getIntegralType(),
1375 parameter_pack_true, nullptr));
1376 } else {
1377 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1378 *ast, decl_context,
1379 SourceLocation(), SourceLocation(), depth, num_template_params,
1380 identifier_info,
1381 is_typename, parameter_pack_true));
1382 }
1383 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001384 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1385 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1386 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1387 SourceLocation(), requires_clause);
1388 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001389}
1390
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1392 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1393 const char *name, const TemplateParameterInfos &template_param_infos) {
Adrian Prantld8f460e2018-05-02 16:55:16 +00001394 // /// Create a function template node.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001395 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001396
Kate Stoneb9c1b512016-09-06 20:57:50 +00001397 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001398
Kate Stoneb9c1b512016-09-06 20:57:50 +00001399 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1400 ast, template_param_infos, template_param_decls);
1401 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1402 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1403 template_param_list, func_decl);
1404
1405 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1406 i < template_param_decl_count; ++i) {
1407 // TODO: verify which decl context we should put template_param_decls into..
1408 template_param_decls[i]->setDeclContext(func_decl);
1409 }
1410
1411 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001412}
1413
Kate Stoneb9c1b512016-09-06 20:57:50 +00001414void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1415 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1416 const TemplateParameterInfos &infos) {
1417 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001418
Kate Stoneb9c1b512016-09-06 20:57:50 +00001419 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1420 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001421}
1422
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1424 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1425 int kind, const TemplateParameterInfos &template_param_infos) {
1426 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001427
Kate Stoneb9c1b512016-09-06 20:57:50 +00001428 ClassTemplateDecl *class_template_decl = nullptr;
1429 if (decl_ctx == nullptr)
1430 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001431
Kate Stoneb9c1b512016-09-06 20:57:50 +00001432 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1433 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001434
Kate Stoneb9c1b512016-09-06 20:57:50 +00001435 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001436
Kate Stoneb9c1b512016-09-06 20:57:50 +00001437 for (NamedDecl *decl : result) {
1438 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001439 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001440 return class_template_decl;
1441 }
1442
1443 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1444
1445 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1446 ast, template_param_infos, template_param_decls);
1447
1448 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1449 *ast, (TagDecl::TagKind)kind,
1450 decl_ctx, // What decl context do we use here? TU? The actual decl
1451 // context?
1452 SourceLocation(), SourceLocation(), &identifier_info);
1453
1454 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1455 i < template_param_decl_count; ++i) {
1456 template_param_decls[i]->setDeclContext(template_cxx_decl);
1457 }
1458
1459 // With templated classes, we say that a class is templated with
1460 // specializations, but that the bare class has no functions.
1461 // template_cxx_decl->startDefinition();
1462 // template_cxx_decl->completeDefinition();
1463
1464 class_template_decl = ClassTemplateDecl::Create(
1465 *ast,
1466 decl_ctx, // What decl context do we use here? TU? The actual decl
1467 // context?
Pavel Labath4294de32017-01-12 10:44:16 +00001468 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001469
1470 if (class_template_decl) {
1471 if (access_type != eAccessNone)
1472 class_template_decl->setAccess(
1473 ConvertAccessTypeToAccessSpecifier(access_type));
1474
1475 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1476 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1477
1478 decl_ctx->addDecl(class_template_decl);
1479
Sean Callanan5e9e1992011-10-26 01:06:27 +00001480#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001481 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001482#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001483 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001484
Kate Stoneb9c1b512016-09-06 20:57:50 +00001485 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001486}
1487
Frederic Rissf4e7e522018-04-02 16:18:32 +00001488TemplateTemplateParmDecl *
1489ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) {
1490 ASTContext *ast = getASTContext();
1491
1492 auto *decl_ctx = ast->getTranslationUnitDecl();
1493
1494 IdentifierInfo &identifier_info = ast->Idents.get(template_name);
1495 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1496
1497 ClangASTContext::TemplateParameterInfos template_param_infos;
1498 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1499 ast, template_param_infos, template_param_decls);
1500
1501 // LLDB needs to create those decls only to be able to display a
Adrian Prantl05097242018-04-30 16:49:04 +00001502 // type that includes a template template argument. Only the name matters for
1503 // this purpose, so we use dummy values for the other characterisitcs of the
1504 // type.
Frederic Rissf4e7e522018-04-02 16:18:32 +00001505 return TemplateTemplateParmDecl::Create(
1506 *ast, decl_ctx, SourceLocation(),
1507 /*Depth*/ 0, /*Position*/ 0,
1508 /*IsParameterPack*/ false, &identifier_info, template_param_list);
1509}
1510
Greg Claytonf0705c82011-10-22 03:33:13 +00001511ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001512ClangASTContext::CreateClassTemplateSpecializationDecl(
1513 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1514 const TemplateParameterInfos &template_param_infos) {
1515 ASTContext *ast = getASTContext();
Sean Callanan09e91ac2017-05-11 22:08:05 +00001516 llvm::SmallVector<clang::TemplateArgument, 2> args(
1517 template_param_infos.args.size() +
1518 (template_param_infos.packed_args ? 1 : 0));
1519 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1520 args.begin());
1521 if (template_param_infos.packed_args) {
1522 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1523 *ast, template_param_infos.packed_args->args);
1524 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001525 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1526 ClassTemplateSpecializationDecl::Create(
1527 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Sean Callanan09e91ac2017-05-11 22:08:05 +00001528 SourceLocation(), class_template_decl, args,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001529 nullptr);
1530
1531 class_template_specialization_decl->setSpecializationKind(
1532 TSK_ExplicitSpecialization);
1533
1534 return class_template_specialization_decl;
1535}
1536
1537CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1538 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1539 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001540 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001541 if (ast)
1542 return CompilerType(
1543 ast, ast->getTagDeclType(class_template_specialization_decl));
1544 }
1545 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001546}
1547
Kate Stoneb9c1b512016-09-06 20:57:50 +00001548static inline bool check_op_param(bool is_method,
1549 clang::OverloadedOperatorKind op_kind,
1550 bool unary, bool binary,
1551 uint32_t num_params) {
1552 // Special-case call since it can take any number of operands
1553 if (op_kind == OO_Call)
1554 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001555
Kate Stoneb9c1b512016-09-06 20:57:50 +00001556 // The parameter count doesn't include "this"
1557 if (is_method)
1558 ++num_params;
1559 if (num_params == 1)
1560 return unary;
1561 if (num_params == 2)
1562 return binary;
1563 else
Greg Clayton090d0982011-06-19 03:43:27 +00001564 return false;
1565}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001566
Kate Stoneb9c1b512016-09-06 20:57:50 +00001567bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1568 bool is_method, clang::OverloadedOperatorKind op_kind,
1569 uint32_t num_params) {
1570 switch (op_kind) {
1571 default:
1572 break;
1573 // C++ standard allows any number of arguments to new/delete
1574 case OO_New:
1575 case OO_Array_New:
1576 case OO_Delete:
1577 case OO_Array_Delete:
1578 return true;
1579 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001580
Kate Stoneb9c1b512016-09-06 20:57:50 +00001581#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1582 case OO_##Name: \
1583 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1584 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001585#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001586 default:
1587 break;
1588 }
1589 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001590}
1591
Greg Clayton57ee3062013-07-11 22:46:58 +00001592clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001593ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1594 clang::AccessSpecifier rhs) {
1595 // Make the access equal to the stricter of the field and the nested field's
1596 // access
1597 if (lhs == AS_none || rhs == AS_none)
1598 return AS_none;
1599 if (lhs == AS_private || rhs == AS_private)
1600 return AS_private;
1601 if (lhs == AS_protected || rhs == AS_protected)
1602 return AS_protected;
1603 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001604}
1605
Kate Stoneb9c1b512016-09-06 20:57:50 +00001606bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1607 uint32_t &bitfield_bit_size) {
1608 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001609}
1610
Kate Stoneb9c1b512016-09-06 20:57:50 +00001611bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1612 uint32_t &bitfield_bit_size) {
1613 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001614 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001615
Kate Stoneb9c1b512016-09-06 20:57:50 +00001616 if (field->isBitField()) {
1617 Expr *bit_width_expr = field->getBitWidth();
1618 if (bit_width_expr) {
1619 llvm::APSInt bit_width_apsint;
1620 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1621 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001622 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001623 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001624 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001625 }
1626 return false;
1627}
1628
1629bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1630 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001631 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001632
1633 if (!record_decl->field_empty())
1634 return true;
1635
1636 // No fields, lets check this is a CXX record and check the base classes
1637 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1638 if (cxx_record_decl) {
1639 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1640 for (base_class = cxx_record_decl->bases_begin(),
1641 base_class_end = cxx_record_decl->bases_end();
1642 base_class != base_class_end; ++base_class) {
1643 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1644 base_class->getType()->getAs<RecordType>()->getDecl());
1645 if (RecordHasFields(base_class_decl))
1646 return true;
1647 }
1648 }
1649 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001650}
1651
Adrian Prantl4e8be2c2018-06-13 16:21:24 +00001652#pragma mark Objective-C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001653
Kate Stoneb9c1b512016-09-06 20:57:50 +00001654CompilerType ClangASTContext::CreateObjCClass(const char *name,
1655 DeclContext *decl_ctx,
1656 bool isForwardDecl,
1657 bool isInternal,
1658 ClangASTMetadata *metadata) {
1659 ASTContext *ast = getASTContext();
1660 assert(ast != nullptr);
1661 assert(name && name[0]);
1662 if (decl_ctx == nullptr)
1663 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001664
Kate Stoneb9c1b512016-09-06 20:57:50 +00001665 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1666 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1667 nullptr, SourceLocation(),
1668 /*isForwardDecl,*/
1669 isInternal);
1670
1671 if (decl && metadata)
1672 SetMetadata(ast, decl, *metadata);
1673
1674 return CompilerType(ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001675}
1676
Kate Stoneb9c1b512016-09-06 20:57:50 +00001677static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1678 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) ==
1679 false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001680}
1681
Greg Clayton57ee3062013-07-11 22:46:58 +00001682uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001683ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1684 bool omit_empty_base_classes) {
1685 uint32_t num_bases = 0;
1686 if (cxx_record_decl) {
1687 if (omit_empty_base_classes) {
1688 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1689 for (base_class = cxx_record_decl->bases_begin(),
1690 base_class_end = cxx_record_decl->bases_end();
1691 base_class != base_class_end; ++base_class) {
1692 // Skip empty base classes
1693 if (omit_empty_base_classes) {
1694 if (BaseSpecifierIsEmpty(base_class))
1695 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001696 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001697 ++num_bases;
1698 }
1699 } else
1700 num_bases = cxx_record_decl->getNumBases();
1701 }
1702 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001703}
1704
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001705#pragma mark Namespace Declarations
1706
1707NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001708ClangASTContext::GetUniqueNamespaceDeclaration(const char *name,
1709 DeclContext *decl_ctx) {
1710 NamespaceDecl *namespace_decl = nullptr;
1711 ASTContext *ast = getASTContext();
1712 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1713 if (decl_ctx == nullptr)
1714 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001715
Kate Stoneb9c1b512016-09-06 20:57:50 +00001716 if (name) {
1717 IdentifierInfo &identifier_info = ast->Idents.get(name);
1718 DeclarationName decl_name(&identifier_info);
1719 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1720 for (NamedDecl *decl : result) {
1721 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1722 if (namespace_decl)
1723 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001724 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001725
1726 namespace_decl =
1727 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1728 SourceLocation(), &identifier_info, nullptr);
1729
1730 decl_ctx->addDecl(namespace_decl);
1731 } else {
1732 if (decl_ctx == translation_unit_decl) {
1733 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1734 if (namespace_decl)
1735 return namespace_decl;
1736
1737 namespace_decl =
1738 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1739 SourceLocation(), nullptr, nullptr);
1740 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1741 translation_unit_decl->addDecl(namespace_decl);
1742 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1743 } else {
1744 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1745 if (parent_namespace_decl) {
1746 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1747 if (namespace_decl)
1748 return namespace_decl;
1749 namespace_decl =
1750 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1751 SourceLocation(), nullptr, nullptr);
1752 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1753 parent_namespace_decl->addDecl(namespace_decl);
1754 assert(namespace_decl ==
1755 parent_namespace_decl->getAnonymousNamespace());
1756 } else {
1757 // BAD!!!
1758 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001759 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001760 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001761#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001762 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001763#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001764 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001765}
1766
Kate Stoneb9c1b512016-09-06 20:57:50 +00001767NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1768 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) {
1769 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1770 if (ast_ctx == nullptr)
1771 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +00001772
Kate Stoneb9c1b512016-09-06 20:57:50 +00001773 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
Siva Chandra03ff5c82016-02-05 19:10:04 +00001774}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001775
Paul Hermand628cbb2015-09-15 23:44:17 +00001776clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001777ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1778 if (ctx != nullptr) {
1779 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1780 clang::SourceLocation());
1781 ctx->addDecl(decl);
1782 return decl;
1783 }
1784 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001785}
1786
Kate Stoneb9c1b512016-09-06 20:57:50 +00001787clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1788 clang::DeclContext *right,
1789 clang::DeclContext *root) {
1790 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001791 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001792
1793 std::set<clang::DeclContext *> path_left;
1794 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1795 path_left.insert(d);
1796
1797 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1798 if (path_left.find(d) != path_left.end())
1799 return d;
1800
1801 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00001802}
1803
Kate Stoneb9c1b512016-09-06 20:57:50 +00001804clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1805 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1806 if (decl_ctx != nullptr && ns_decl != nullptr) {
1807 clang::TranslationUnitDecl *translation_unit =
1808 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1809 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1810 *getASTContext(), decl_ctx, clang::SourceLocation(),
1811 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1812 clang::SourceLocation(), ns_decl,
1813 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
1814 decl_ctx->addDecl(using_decl);
1815 return using_decl;
1816 }
1817 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001818}
1819
1820clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001821ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1822 clang::NamedDecl *target) {
1823 if (current_decl_ctx != nullptr && target != nullptr) {
1824 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1825 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
1826 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1827 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1828 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
1829 target);
1830 using_decl->addShadowDecl(shadow_decl);
1831 current_decl_ctx->addDecl(using_decl);
1832 return using_decl;
1833 }
1834 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001835}
1836
Kate Stoneb9c1b512016-09-06 20:57:50 +00001837clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
1838 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
1839 if (decl_context != nullptr) {
1840 clang::VarDecl *var_decl = clang::VarDecl::Create(
1841 *getASTContext(), decl_context, clang::SourceLocation(),
1842 clang::SourceLocation(),
1843 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
1844 nullptr, clang::SC_None);
1845 var_decl->setAccess(clang::AS_public);
1846 decl_context->addDecl(var_decl);
1847 return var_decl;
1848 }
1849 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001850}
1851
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001852lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001853ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
1854 lldb::BasicType basic_type) {
1855 switch (basic_type) {
1856 case eBasicTypeVoid:
1857 return ast->VoidTy.getAsOpaquePtr();
1858 case eBasicTypeChar:
1859 return ast->CharTy.getAsOpaquePtr();
1860 case eBasicTypeSignedChar:
1861 return ast->SignedCharTy.getAsOpaquePtr();
1862 case eBasicTypeUnsignedChar:
1863 return ast->UnsignedCharTy.getAsOpaquePtr();
1864 case eBasicTypeWChar:
1865 return ast->getWCharType().getAsOpaquePtr();
1866 case eBasicTypeSignedWChar:
1867 return ast->getSignedWCharType().getAsOpaquePtr();
1868 case eBasicTypeUnsignedWChar:
1869 return ast->getUnsignedWCharType().getAsOpaquePtr();
1870 case eBasicTypeChar16:
1871 return ast->Char16Ty.getAsOpaquePtr();
1872 case eBasicTypeChar32:
1873 return ast->Char32Ty.getAsOpaquePtr();
1874 case eBasicTypeShort:
1875 return ast->ShortTy.getAsOpaquePtr();
1876 case eBasicTypeUnsignedShort:
1877 return ast->UnsignedShortTy.getAsOpaquePtr();
1878 case eBasicTypeInt:
1879 return ast->IntTy.getAsOpaquePtr();
1880 case eBasicTypeUnsignedInt:
1881 return ast->UnsignedIntTy.getAsOpaquePtr();
1882 case eBasicTypeLong:
1883 return ast->LongTy.getAsOpaquePtr();
1884 case eBasicTypeUnsignedLong:
1885 return ast->UnsignedLongTy.getAsOpaquePtr();
1886 case eBasicTypeLongLong:
1887 return ast->LongLongTy.getAsOpaquePtr();
1888 case eBasicTypeUnsignedLongLong:
1889 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1890 case eBasicTypeInt128:
1891 return ast->Int128Ty.getAsOpaquePtr();
1892 case eBasicTypeUnsignedInt128:
1893 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1894 case eBasicTypeBool:
1895 return ast->BoolTy.getAsOpaquePtr();
1896 case eBasicTypeHalf:
1897 return ast->HalfTy.getAsOpaquePtr();
1898 case eBasicTypeFloat:
1899 return ast->FloatTy.getAsOpaquePtr();
1900 case eBasicTypeDouble:
1901 return ast->DoubleTy.getAsOpaquePtr();
1902 case eBasicTypeLongDouble:
1903 return ast->LongDoubleTy.getAsOpaquePtr();
1904 case eBasicTypeFloatComplex:
1905 return ast->FloatComplexTy.getAsOpaquePtr();
1906 case eBasicTypeDoubleComplex:
1907 return ast->DoubleComplexTy.getAsOpaquePtr();
1908 case eBasicTypeLongDoubleComplex:
1909 return ast->LongDoubleComplexTy.getAsOpaquePtr();
1910 case eBasicTypeObjCID:
1911 return ast->getObjCIdType().getAsOpaquePtr();
1912 case eBasicTypeObjCClass:
1913 return ast->getObjCClassType().getAsOpaquePtr();
1914 case eBasicTypeObjCSel:
1915 return ast->getObjCSelType().getAsOpaquePtr();
1916 case eBasicTypeNullPtr:
1917 return ast->NullPtrTy.getAsOpaquePtr();
1918 default:
1919 return nullptr;
1920 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001921}
1922
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001923#pragma mark Function Types
1924
Pavel Labath1ac2b202016-08-15 14:32:32 +00001925clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00001926ClangASTContext::GetDeclarationName(const char *name,
1927 const CompilerType &function_clang_type) {
1928 if (!name || !name[0])
1929 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00001930
Kate Stoneb9c1b512016-09-06 20:57:50 +00001931 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
1932 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
1933 return DeclarationName(&getASTContext()->Idents.get(
1934 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00001935
Adrian Prantl05097242018-04-30 16:49:04 +00001936 // Check the number of operator parameters. Sometimes we have seen bad DWARF
1937 // that doesn't correctly describe operators and if we try to create a method
1938 // and add it to the class, clang will assert and crash, so we need to make
1939 // sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001940 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
1941 const clang::FunctionProtoType *function_type =
1942 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
1943 if (function_type == nullptr)
1944 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00001945
Kate Stoneb9c1b512016-09-06 20:57:50 +00001946 const bool is_method = false;
1947 const unsigned int num_params = function_type->getNumParams();
1948 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1949 is_method, op_kind, num_params))
1950 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00001951
Kate Stoneb9c1b512016-09-06 20:57:50 +00001952 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00001953}
1954
Kate Stoneb9c1b512016-09-06 20:57:50 +00001955FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
1956 DeclContext *decl_ctx, const char *name,
1957 const CompilerType &function_clang_type, int storage, bool is_inline) {
1958 FunctionDecl *func_decl = nullptr;
1959 ASTContext *ast = getASTContext();
1960 if (decl_ctx == nullptr)
1961 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001962
Kate Stoneb9c1b512016-09-06 20:57:50 +00001963 const bool hasWrittenPrototype = true;
1964 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00001965
Kate Stoneb9c1b512016-09-06 20:57:50 +00001966 clang::DeclarationName declarationName =
1967 GetDeclarationName(name, function_clang_type);
1968 func_decl = FunctionDecl::Create(
1969 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
1970 ClangUtil::GetQualType(function_clang_type), nullptr,
1971 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
1972 isConstexprSpecified);
1973 if (func_decl)
1974 decl_ctx->addDecl(func_decl);
1975
Sean Callanan5e9e1992011-10-26 01:06:27 +00001976#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001977 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001978#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001979
1980 return func_decl;
1981}
1982
1983CompilerType ClangASTContext::CreateFunctionType(
1984 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
1985 unsigned num_args, bool is_variadic, unsigned type_quals) {
1986 if (ast == nullptr)
1987 return CompilerType(); // invalid AST
1988
1989 if (!result_type || !ClangUtil::IsClangType(result_type))
1990 return CompilerType(); // invalid return type
1991
1992 std::vector<QualType> qual_type_args;
1993 if (num_args > 0 && args == nullptr)
1994 return CompilerType(); // invalid argument array passed in
1995
1996 // Verify that all arguments are valid and the right type
1997 for (unsigned i = 0; i < num_args; ++i) {
1998 if (args[i]) {
1999 // Make sure we have a clang type in args[i] and not a type from another
2000 // language whose name might match
2001 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2002 lldbassert(is_clang_type);
2003 if (is_clang_type)
2004 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2005 else
2006 return CompilerType(); // invalid argument type (must be a clang type)
2007 } else
2008 return CompilerType(); // invalid argument type (empty)
2009 }
2010
2011 // TODO: Detect calling convention in DWARF?
2012 FunctionProtoType::ExtProtoInfo proto_info;
2013 proto_info.Variadic = is_variadic;
2014 proto_info.ExceptionSpec = EST_None;
2015 proto_info.TypeQuals = type_quals;
2016 proto_info.RefQualifier = RQ_None;
2017
2018 return CompilerType(ast,
2019 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2020 qual_type_args, proto_info));
2021}
2022
2023ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
2024 const char *name, const CompilerType &param_type, int storage) {
2025 ASTContext *ast = getASTContext();
2026 assert(ast != nullptr);
2027 return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(),
2028 SourceLocation(), SourceLocation(),
2029 name && name[0] ? &ast->Idents.get(name) : nullptr,
2030 ClangUtil::GetQualType(param_type), nullptr,
2031 (clang::StorageClass)storage, nullptr);
2032}
2033
2034void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2035 ParmVarDecl **params,
2036 unsigned num_params) {
2037 if (function_decl)
2038 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002039}
2040
Greg Claytona1e5dc82015-08-11 22:53:00 +00002041CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002042ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
2043 QualType block_type = m_ast_ap->getBlockPointerType(
2044 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002045
Kate Stoneb9c1b512016-09-06 20:57:50 +00002046 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002047}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002048
2049#pragma mark Array Types
2050
Kate Stoneb9c1b512016-09-06 20:57:50 +00002051CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2052 size_t element_count,
2053 bool is_vector) {
2054 if (element_type.IsValid()) {
2055 ASTContext *ast = getASTContext();
2056 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002057
Kate Stoneb9c1b512016-09-06 20:57:50 +00002058 if (is_vector) {
2059 return CompilerType(
2060 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2061 element_count));
2062 } else {
2063
2064 llvm::APInt ap_element_count(64, element_count);
2065 if (element_count == 0) {
2066 return CompilerType(ast, ast->getIncompleteArrayType(
2067 ClangUtil::GetQualType(element_type),
2068 clang::ArrayType::Normal, 0));
2069 } else {
2070 return CompilerType(
2071 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2072 ap_element_count,
2073 clang::ArrayType::Normal, 0));
2074 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002075 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002076 }
2077 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002078}
2079
Kate Stoneb9c1b512016-09-06 20:57:50 +00002080CompilerType ClangASTContext::CreateStructForIdentifier(
2081 const ConstString &type_name,
2082 const std::initializer_list<std::pair<const char *, CompilerType>>
2083 &type_fields,
2084 bool packed) {
2085 CompilerType type;
2086 if (!type_name.IsEmpty() &&
2087 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2088 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002089 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002090 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002091 }
2092
2093 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2094 clang::TTK_Struct, lldb::eLanguageTypeC);
2095 StartTagDeclarationDefinition(type);
2096 for (const auto &field : type_fields)
2097 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2098 0);
2099 if (packed)
2100 SetIsPacked(type);
2101 CompleteTagDeclarationDefinition(type);
2102 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002103}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002104
Kate Stoneb9c1b512016-09-06 20:57:50 +00002105CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
2106 const ConstString &type_name,
2107 const std::initializer_list<std::pair<const char *, CompilerType>>
2108 &type_fields,
2109 bool packed) {
2110 CompilerType type;
2111 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2112 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002113
Kate Stoneb9c1b512016-09-06 20:57:50 +00002114 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002115}
2116
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002117#pragma mark Enumeration Types
2118
Greg Claytona1e5dc82015-08-11 22:53:00 +00002119CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002120ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2121 const Declaration &decl,
Tamas Berghammer59765832017-11-07 10:39:22 +00002122 const CompilerType &integer_clang_type,
2123 bool is_scoped) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002124 // TODO: Do something intelligent with the Declaration object passed in
2125 // like maybe filling in the SourceLocation with it...
2126 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002127
Kate Stoneb9c1b512016-09-06 20:57:50 +00002128 // TODO: ask about these...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002129 // const bool IsFixed = false;
2130
2131 EnumDecl *enum_decl = EnumDecl::Create(
2132 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2133 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
Tamas Berghammercf6bf4c2017-11-07 13:43:55 +00002134 is_scoped, // IsScoped
2135 is_scoped, // IsScopedUsingClassTag
2136 false); // IsFixed
Kate Stoneb9c1b512016-09-06 20:57:50 +00002137
2138 if (enum_decl) {
2139 // TODO: check if we should be setting the promotion type too?
2140 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2141
2142 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2143
2144 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2145 }
2146 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002147}
2148
Kate Stoneb9c1b512016-09-06 20:57:50 +00002149CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2150 size_t bit_size,
2151 bool is_signed) {
2152 if (ast) {
2153 if (is_signed) {
2154 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2155 return CompilerType(ast, ast->SignedCharTy);
2156
2157 if (bit_size == ast->getTypeSize(ast->ShortTy))
2158 return CompilerType(ast, ast->ShortTy);
2159
2160 if (bit_size == ast->getTypeSize(ast->IntTy))
2161 return CompilerType(ast, ast->IntTy);
2162
2163 if (bit_size == ast->getTypeSize(ast->LongTy))
2164 return CompilerType(ast, ast->LongTy);
2165
2166 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2167 return CompilerType(ast, ast->LongLongTy);
2168
2169 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2170 return CompilerType(ast, ast->Int128Ty);
2171 } else {
2172 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2173 return CompilerType(ast, ast->UnsignedCharTy);
2174
2175 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2176 return CompilerType(ast, ast->UnsignedShortTy);
2177
2178 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2179 return CompilerType(ast, ast->UnsignedIntTy);
2180
2181 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2182 return CompilerType(ast, ast->UnsignedLongTy);
2183
2184 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2185 return CompilerType(ast, ast->UnsignedLongLongTy);
2186
2187 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2188 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002189 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002190 }
2191 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002192}
2193
Kate Stoneb9c1b512016-09-06 20:57:50 +00002194CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2195 bool is_signed) {
2196 if (ast)
2197 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2198 is_signed);
2199 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002200}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002201
Kate Stoneb9c1b512016-09-06 20:57:50 +00002202void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2203 if (decl_ctx) {
2204 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002205
Kate Stoneb9c1b512016-09-06 20:57:50 +00002206 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2207 if (named_decl) {
2208 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2209 named_decl->getDeclName().getAsString().c_str());
2210 } else {
2211 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002212 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002213 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002214}
2215
Kate Stoneb9c1b512016-09-06 20:57:50 +00002216void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2217 if (decl == nullptr)
2218 return;
2219 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002220
Kate Stoneb9c1b512016-09-06 20:57:50 +00002221 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2222 if (record_decl) {
2223 printf("%20s: %s%s\n", decl->getDeclKindName(),
2224 record_decl->getDeclName().getAsString().c_str(),
2225 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002226
Kate Stoneb9c1b512016-09-06 20:57:50 +00002227 } else {
2228 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2229 if (named_decl) {
2230 printf("%20s: %s\n", decl->getDeclKindName(),
2231 named_decl->getDeclName().getAsString().c_str());
2232 } else {
2233 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002234 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002235 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002236}
2237
Kate Stoneb9c1b512016-09-06 20:57:50 +00002238bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2239 clang::Decl *rhs_decl) {
2240 if (lhs_decl && rhs_decl) {
2241 //----------------------------------------------------------------------
2242 // Make sure the decl kinds match first
2243 //----------------------------------------------------------------------
2244 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2245 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002246
Kate Stoneb9c1b512016-09-06 20:57:50 +00002247 if (lhs_decl_kind == rhs_decl_kind) {
2248 //------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00002249 // Now check that the decl contexts kinds are all equivalent before we
2250 // have to check any names of the decl contexts...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002251 //------------------------------------------------------------------
2252 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2253 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2254 if (lhs_decl_ctx && rhs_decl_ctx) {
2255 while (1) {
2256 if (lhs_decl_ctx && rhs_decl_ctx) {
2257 const clang::Decl::Kind lhs_decl_ctx_kind =
2258 lhs_decl_ctx->getDeclKind();
2259 const clang::Decl::Kind rhs_decl_ctx_kind =
2260 rhs_decl_ctx->getDeclKind();
2261 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2262 lhs_decl_ctx = lhs_decl_ctx->getParent();
2263 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002264
Kate Stoneb9c1b512016-09-06 20:57:50 +00002265 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2266 break;
2267 } else
2268 return false;
2269 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002270 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002271 }
2272
2273 //--------------------------------------------------------------
2274 // Now make sure the name of the decls match
2275 //--------------------------------------------------------------
2276 clang::NamedDecl *lhs_named_decl =
2277 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2278 clang::NamedDecl *rhs_named_decl =
2279 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2280 if (lhs_named_decl && rhs_named_decl) {
2281 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2282 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2283 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2284 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2285 return false;
2286 } else
Greg Claytona2721472011-06-25 00:44:06 +00002287 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002288 } else
2289 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002290
Kate Stoneb9c1b512016-09-06 20:57:50 +00002291 //--------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00002292 // We know that the decl context kinds all match, so now we need to
2293 // make sure the names match as well
Kate Stoneb9c1b512016-09-06 20:57:50 +00002294 //--------------------------------------------------------------
2295 lhs_decl_ctx = lhs_decl->getDeclContext();
2296 rhs_decl_ctx = rhs_decl->getDeclContext();
2297 while (1) {
2298 switch (lhs_decl_ctx->getDeclKind()) {
2299 case clang::Decl::TranslationUnit:
2300 // We don't care about the translation unit names
2301 return true;
2302 default: {
2303 clang::NamedDecl *lhs_named_decl =
2304 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2305 clang::NamedDecl *rhs_named_decl =
2306 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2307 if (lhs_named_decl && rhs_named_decl) {
2308 clang::DeclarationName lhs_decl_name =
2309 lhs_named_decl->getDeclName();
2310 clang::DeclarationName rhs_decl_name =
2311 rhs_named_decl->getDeclName();
2312 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2313 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2314 return false;
2315 } else
2316 return false;
2317 } else
2318 return false;
2319 } break;
2320 }
2321 lhs_decl_ctx = lhs_decl_ctx->getParent();
2322 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002323 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002324 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002325 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002326 }
2327 return false;
2328}
2329bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2330 clang::Decl *decl) {
2331 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002332 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002333
Kate Stoneb9c1b512016-09-06 20:57:50 +00002334 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002335
Kate Stoneb9c1b512016-09-06 20:57:50 +00002336 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002337 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002338
2339 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2340 if (tag_decl->isCompleteDefinition())
2341 return true;
2342
2343 if (!tag_decl->hasExternalLexicalStorage())
2344 return false;
2345
2346 ast_source->CompleteType(tag_decl);
2347
2348 return !tag_decl->getTypeForDecl()->isIncompleteType();
2349 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2350 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2351 if (objc_interface_decl->getDefinition())
2352 return true;
2353
2354 if (!objc_interface_decl->hasExternalLexicalStorage())
2355 return false;
2356
2357 ast_source->CompleteType(objc_interface_decl);
2358
2359 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2360 } else {
2361 return false;
2362 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002363}
2364
Kate Stoneb9c1b512016-09-06 20:57:50 +00002365void ClangASTContext::SetMetadataAsUserID(const void *object,
2366 user_id_t user_id) {
2367 ClangASTMetadata meta_data;
2368 meta_data.SetUserID(user_id);
2369 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002370}
2371
Kate Stoneb9c1b512016-09-06 20:57:50 +00002372void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2373 ClangASTMetadata &metadata) {
2374 ClangExternalASTSourceCommon *external_source =
2375 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2376
2377 if (external_source)
2378 external_source->SetMetadata(object, metadata);
2379}
2380
2381ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2382 const void *object) {
2383 ClangExternalASTSourceCommon *external_source =
2384 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2385
2386 if (external_source && external_source->HasMetadata(object))
2387 return external_source->GetMetadata(object);
2388 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002389 return nullptr;
2390}
2391
Kate Stoneb9c1b512016-09-06 20:57:50 +00002392clang::DeclContext *
2393ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2394 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2395}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002396
Kate Stoneb9c1b512016-09-06 20:57:50 +00002397clang::DeclContext *
2398ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2399 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2400}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002401
Kate Stoneb9c1b512016-09-06 20:57:50 +00002402bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2403 int kind) const {
2404 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2405 if (clang_type) {
2406 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2407 if (tag_type) {
2408 clang::TagDecl *tag_decl =
2409 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2410 if (tag_decl) {
2411 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2412 return true;
2413 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002414 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002415 }
2416 return false;
2417}
2418
2419bool ClangASTContext::SetDefaultAccessForRecordFields(
2420 clang::RecordDecl *record_decl, int default_accessibility,
2421 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2422 if (record_decl) {
2423 uint32_t field_idx;
2424 clang::RecordDecl::field_iterator field, field_end;
2425 for (field = record_decl->field_begin(),
2426 field_end = record_decl->field_end(), field_idx = 0;
2427 field != field_end; ++field, ++field_idx) {
2428 // If no accessibility was assigned, assign the correct one
2429 if (field_idx < num_assigned_accessibilities &&
2430 assigned_accessibilities[field_idx] == clang::AS_none)
2431 field->setAccess((clang::AccessSpecifier)default_accessibility);
2432 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002433 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002434 }
2435 return false;
2436}
2437
2438clang::DeclContext *
2439ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2440 return GetDeclContextForType(ClangUtil::GetQualType(type));
2441}
2442
2443clang::DeclContext *
2444ClangASTContext::GetDeclContextForType(clang::QualType type) {
2445 if (type.isNull())
2446 return nullptr;
2447
2448 clang::QualType qual_type = type.getCanonicalType();
2449 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2450 switch (type_class) {
2451 case clang::Type::ObjCInterface:
2452 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2453 ->getInterface();
2454 case clang::Type::ObjCObjectPointer:
2455 return GetDeclContextForType(
2456 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2457 ->getPointeeType());
2458 case clang::Type::Record:
2459 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2460 case clang::Type::Enum:
2461 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2462 case clang::Type::Typedef:
2463 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2464 ->getDecl()
2465 ->getUnderlyingType());
2466 case clang::Type::Auto:
2467 return GetDeclContextForType(
2468 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2469 case clang::Type::Elaborated:
2470 return GetDeclContextForType(
2471 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2472 case clang::Type::Paren:
2473 return GetDeclContextForType(
2474 llvm::cast<clang::ParenType>(qual_type)->desugar());
2475 default:
2476 break;
2477 }
2478 // No DeclContext in this type...
2479 return nullptr;
2480}
2481
2482static bool GetCompleteQualType(clang::ASTContext *ast,
2483 clang::QualType qual_type,
2484 bool allow_completion = true) {
2485 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2486 switch (type_class) {
2487 case clang::Type::ConstantArray:
2488 case clang::Type::IncompleteArray:
2489 case clang::Type::VariableArray: {
2490 const clang::ArrayType *array_type =
2491 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2492
2493 if (array_type)
2494 return GetCompleteQualType(ast, array_type->getElementType(),
2495 allow_completion);
2496 } break;
2497 case clang::Type::Record: {
2498 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2499 if (cxx_record_decl) {
2500 if (cxx_record_decl->hasExternalLexicalStorage()) {
2501 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2502 const bool fields_loaded =
2503 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2504 if (is_complete && fields_loaded)
2505 return true;
2506
2507 if (!allow_completion)
2508 return false;
2509
2510 // Call the field_begin() accessor to for it to use the external source
2511 // to load the fields...
2512 clang::ExternalASTSource *external_ast_source =
2513 ast->getExternalSource();
2514 if (external_ast_source) {
2515 external_ast_source->CompleteType(cxx_record_decl);
2516 if (cxx_record_decl->isCompleteDefinition()) {
2517 cxx_record_decl->field_begin();
2518 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2519 }
2520 }
2521 }
2522 }
2523 const clang::TagType *tag_type =
2524 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2525 return !tag_type->isIncompleteType();
2526 } break;
2527
2528 case clang::Type::Enum: {
2529 const clang::TagType *tag_type =
2530 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2531 if (tag_type) {
2532 clang::TagDecl *tag_decl = tag_type->getDecl();
2533 if (tag_decl) {
2534 if (tag_decl->getDefinition())
2535 return true;
2536
2537 if (!allow_completion)
2538 return false;
2539
2540 if (tag_decl->hasExternalLexicalStorage()) {
2541 if (ast) {
2542 clang::ExternalASTSource *external_ast_source =
2543 ast->getExternalSource();
2544 if (external_ast_source) {
2545 external_ast_source->CompleteType(tag_decl);
2546 return !tag_type->isIncompleteType();
2547 }
2548 }
2549 }
2550 return false;
2551 }
2552 }
2553
2554 } break;
2555 case clang::Type::ObjCObject:
2556 case clang::Type::ObjCInterface: {
2557 const clang::ObjCObjectType *objc_class_type =
2558 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2559 if (objc_class_type) {
2560 clang::ObjCInterfaceDecl *class_interface_decl =
2561 objc_class_type->getInterface();
2562 // We currently can't complete objective C types through the newly added
Adrian Prantl05097242018-04-30 16:49:04 +00002563 // ASTContext because it only supports TagDecl objects right now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002564 if (class_interface_decl) {
2565 if (class_interface_decl->getDefinition())
2566 return true;
2567
2568 if (!allow_completion)
2569 return false;
2570
2571 if (class_interface_decl->hasExternalLexicalStorage()) {
2572 if (ast) {
2573 clang::ExternalASTSource *external_ast_source =
2574 ast->getExternalSource();
2575 if (external_ast_source) {
2576 external_ast_source->CompleteType(class_interface_decl);
2577 return !objc_class_type->isIncompleteType();
2578 }
2579 }
2580 }
2581 return false;
2582 }
2583 }
2584 } break;
2585
2586 case clang::Type::Typedef:
2587 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2588 ->getDecl()
2589 ->getUnderlyingType(),
2590 allow_completion);
2591
2592 case clang::Type::Auto:
2593 return GetCompleteQualType(
2594 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2595 allow_completion);
2596
2597 case clang::Type::Elaborated:
2598 return GetCompleteQualType(
2599 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2600 allow_completion);
2601
2602 case clang::Type::Paren:
2603 return GetCompleteQualType(
2604 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2605 allow_completion);
2606
2607 case clang::Type::Attributed:
2608 return GetCompleteQualType(
2609 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2610 allow_completion);
2611
2612 default:
2613 break;
2614 }
2615
2616 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002617}
2618
2619static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002620ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2621 switch (access) {
2622 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002623 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002624 case eAccessPublic:
2625 return clang::ObjCIvarDecl::Public;
2626 case eAccessPrivate:
2627 return clang::ObjCIvarDecl::Private;
2628 case eAccessProtected:
2629 return clang::ObjCIvarDecl::Protected;
2630 case eAccessPackage:
2631 return clang::ObjCIvarDecl::Package;
2632 }
2633 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002634}
2635
Greg Claytond8d4a572015-08-11 21:38:15 +00002636//----------------------------------------------------------------------
2637// Tests
2638//----------------------------------------------------------------------
2639
Kate Stoneb9c1b512016-09-06 20:57:50 +00002640bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2641 clang::QualType qual_type(GetCanonicalQualType(type));
2642
2643 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2644 switch (type_class) {
2645 case clang::Type::IncompleteArray:
2646 case clang::Type::VariableArray:
2647 case clang::Type::ConstantArray:
2648 case clang::Type::ExtVector:
2649 case clang::Type::Vector:
2650 case clang::Type::Record:
2651 case clang::Type::ObjCObject:
2652 case clang::Type::ObjCInterface:
2653 return true;
2654 case clang::Type::Auto:
2655 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2656 ->getDeducedType()
2657 .getAsOpaquePtr());
2658 case clang::Type::Elaborated:
2659 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2660 ->getNamedType()
2661 .getAsOpaquePtr());
2662 case clang::Type::Typedef:
2663 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2664 ->getDecl()
2665 ->getUnderlyingType()
2666 .getAsOpaquePtr());
2667 case clang::Type::Paren:
2668 return IsAggregateType(
2669 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2670 default:
2671 break;
2672 }
2673 // The clang type does have a value
2674 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002675}
2676
Kate Stoneb9c1b512016-09-06 20:57:50 +00002677bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2678 clang::QualType qual_type(GetCanonicalQualType(type));
2679
2680 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2681 switch (type_class) {
2682 case clang::Type::Record: {
2683 if (const clang::RecordType *record_type =
2684 llvm::dyn_cast_or_null<clang::RecordType>(
2685 qual_type.getTypePtrOrNull())) {
2686 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2687 return record_decl->isAnonymousStructOrUnion();
2688 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002689 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002690 break;
2691 }
2692 case clang::Type::Auto:
2693 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2694 ->getDeducedType()
2695 .getAsOpaquePtr());
2696 case clang::Type::Elaborated:
2697 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2698 ->getNamedType()
2699 .getAsOpaquePtr());
2700 case clang::Type::Typedef:
2701 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2702 ->getDecl()
2703 ->getUnderlyingType()
2704 .getAsOpaquePtr());
2705 case clang::Type::Paren:
2706 return IsAnonymousType(
2707 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2708 default:
2709 break;
2710 }
2711 // The clang type does have a value
2712 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002713}
2714
Kate Stoneb9c1b512016-09-06 20:57:50 +00002715bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2716 CompilerType *element_type_ptr,
2717 uint64_t *size, bool *is_incomplete) {
2718 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002719
Kate Stoneb9c1b512016-09-06 20:57:50 +00002720 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2721 switch (type_class) {
2722 default:
2723 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002724
Kate Stoneb9c1b512016-09-06 20:57:50 +00002725 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002726 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002727 element_type_ptr->SetCompilerType(
2728 getASTContext(),
2729 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002730 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002731 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2732 ->getSize()
2733 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002734 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002735 *is_incomplete = false;
2736 return true;
2737
2738 case clang::Type::IncompleteArray:
2739 if (element_type_ptr)
2740 element_type_ptr->SetCompilerType(
2741 getASTContext(),
2742 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2743 if (size)
2744 *size = 0;
2745 if (is_incomplete)
2746 *is_incomplete = true;
2747 return true;
2748
2749 case clang::Type::VariableArray:
2750 if (element_type_ptr)
2751 element_type_ptr->SetCompilerType(
2752 getASTContext(),
2753 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2754 if (size)
2755 *size = 0;
2756 if (is_incomplete)
2757 *is_incomplete = false;
2758 return true;
2759
2760 case clang::Type::DependentSizedArray:
2761 if (element_type_ptr)
2762 element_type_ptr->SetCompilerType(
2763 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2764 ->getElementType());
2765 if (size)
2766 *size = 0;
2767 if (is_incomplete)
2768 *is_incomplete = false;
2769 return true;
2770
2771 case clang::Type::Typedef:
2772 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2773 ->getDecl()
2774 ->getUnderlyingType()
2775 .getAsOpaquePtr(),
2776 element_type_ptr, size, is_incomplete);
2777 case clang::Type::Auto:
2778 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2779 ->getDeducedType()
2780 .getAsOpaquePtr(),
2781 element_type_ptr, size, is_incomplete);
2782 case clang::Type::Elaborated:
2783 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2784 ->getNamedType()
2785 .getAsOpaquePtr(),
2786 element_type_ptr, size, is_incomplete);
2787 case clang::Type::Paren:
2788 return IsArrayType(
2789 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2790 element_type_ptr, size, is_incomplete);
2791 }
2792 if (element_type_ptr)
2793 element_type_ptr->Clear();
2794 if (size)
2795 *size = 0;
2796 if (is_incomplete)
2797 *is_incomplete = false;
2798 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002799}
2800
Kate Stoneb9c1b512016-09-06 20:57:50 +00002801bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2802 CompilerType *element_type, uint64_t *size) {
2803 clang::QualType qual_type(GetCanonicalQualType(type));
2804
2805 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2806 switch (type_class) {
2807 case clang::Type::Vector: {
2808 const clang::VectorType *vector_type =
2809 qual_type->getAs<clang::VectorType>();
2810 if (vector_type) {
2811 if (size)
2812 *size = vector_type->getNumElements();
2813 if (element_type)
2814 *element_type =
2815 CompilerType(getASTContext(), vector_type->getElementType());
2816 }
2817 return true;
2818 } break;
2819 case clang::Type::ExtVector: {
2820 const clang::ExtVectorType *ext_vector_type =
2821 qual_type->getAs<clang::ExtVectorType>();
2822 if (ext_vector_type) {
2823 if (size)
2824 *size = ext_vector_type->getNumElements();
2825 if (element_type)
2826 *element_type =
2827 CompilerType(getASTContext(), ext_vector_type->getElementType());
2828 }
2829 return true;
2830 }
2831 default:
2832 break;
2833 }
2834 return false;
2835}
2836
2837bool ClangASTContext::IsRuntimeGeneratedType(
2838 lldb::opaque_compiler_type_t type) {
2839 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
2840 ->GetDeclContextForType(GetQualType(type));
2841 if (!decl_ctx)
2842 return false;
2843
2844 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2845 return false;
2846
2847 clang::ObjCInterfaceDecl *result_iface_decl =
2848 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2849
2850 ClangASTMetadata *ast_metadata =
2851 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2852 if (!ast_metadata)
2853 return false;
2854 return (ast_metadata->GetISAPtr() != 0);
2855}
2856
2857bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
2858 return GetQualType(type).getUnqualifiedType()->isCharType();
2859}
2860
2861bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
2862 const bool allow_completion = false;
2863 return GetCompleteQualType(getASTContext(), GetQualType(type),
2864 allow_completion);
2865}
2866
2867bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
2868 return GetQualType(type).isConstQualified();
2869}
2870
2871bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
2872 uint32_t &length) {
2873 CompilerType pointee_or_element_clang_type;
2874 length = 0;
2875 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2876
2877 if (!pointee_or_element_clang_type.IsValid())
2878 return false;
2879
2880 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2881 if (pointee_or_element_clang_type.IsCharType()) {
2882 if (type_flags.Test(eTypeIsArray)) {
Adrian Prantl05097242018-04-30 16:49:04 +00002883 // We know the size of the array and it could be a C string since it is
2884 // an array of characters
Kate Stoneb9c1b512016-09-06 20:57:50 +00002885 length = llvm::cast<clang::ConstantArrayType>(
2886 GetCanonicalQualType(type).getTypePtr())
2887 ->getSize()
2888 .getLimitedValue();
2889 }
2890 return true;
2891 }
2892 }
2893 return false;
2894}
2895
2896bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
2897 bool *is_variadic_ptr) {
2898 if (type) {
2899 clang::QualType qual_type(GetCanonicalQualType(type));
2900
2901 if (qual_type->isFunctionType()) {
2902 if (is_variadic_ptr) {
2903 const clang::FunctionProtoType *function_proto_type =
2904 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2905 if (function_proto_type)
2906 *is_variadic_ptr = function_proto_type->isVariadic();
2907 else
2908 *is_variadic_ptr = false;
2909 }
2910 return true;
2911 }
2912
Greg Claytond8d4a572015-08-11 21:38:15 +00002913 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002914 switch (type_class) {
2915 default:
2916 break;
2917 case clang::Type::Typedef:
2918 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
2919 ->getDecl()
2920 ->getUnderlyingType()
2921 .getAsOpaquePtr(),
2922 nullptr);
2923 case clang::Type::Auto:
2924 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
2925 ->getDeducedType()
2926 .getAsOpaquePtr(),
2927 nullptr);
2928 case clang::Type::Elaborated:
2929 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
2930 ->getNamedType()
2931 .getAsOpaquePtr(),
2932 nullptr);
2933 case clang::Type::Paren:
2934 return IsFunctionType(
2935 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2936 nullptr);
2937 case clang::Type::LValueReference:
2938 case clang::Type::RValueReference: {
2939 const clang::ReferenceType *reference_type =
2940 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2941 if (reference_type)
2942 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
2943 nullptr);
2944 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00002945 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002946 }
2947 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002948}
2949
2950// Used to detect "Homogeneous Floating-point Aggregates"
2951uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00002952ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
2953 CompilerType *base_type_ptr) {
2954 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002955 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002956
2957 clang::QualType qual_type(GetCanonicalQualType(type));
2958 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2959 switch (type_class) {
2960 case clang::Type::Record:
2961 if (GetCompleteType(type)) {
2962 const clang::CXXRecordDecl *cxx_record_decl =
2963 qual_type->getAsCXXRecordDecl();
2964 if (cxx_record_decl) {
2965 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
2966 return 0;
2967 }
2968 const clang::RecordType *record_type =
2969 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
2970 if (record_type) {
2971 const clang::RecordDecl *record_decl = record_type->getDecl();
2972 if (record_decl) {
2973 // We are looking for a structure that contains only floating point
2974 // types
2975 clang::RecordDecl::field_iterator field_pos,
2976 field_end = record_decl->field_end();
2977 uint32_t num_fields = 0;
2978 bool is_hva = false;
2979 bool is_hfa = false;
2980 clang::QualType base_qual_type;
2981 uint64_t base_bitwidth = 0;
2982 for (field_pos = record_decl->field_begin(); field_pos != field_end;
2983 ++field_pos) {
2984 clang::QualType field_qual_type = field_pos->getType();
2985 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
2986 if (field_qual_type->isFloatingType()) {
2987 if (field_qual_type->isComplexType())
2988 return 0;
2989 else {
2990 if (num_fields == 0)
2991 base_qual_type = field_qual_type;
2992 else {
2993 if (is_hva)
2994 return 0;
2995 is_hfa = true;
2996 if (field_qual_type.getTypePtr() !=
2997 base_qual_type.getTypePtr())
2998 return 0;
2999 }
3000 }
3001 } else if (field_qual_type->isVectorType() ||
3002 field_qual_type->isExtVectorType()) {
3003 if (num_fields == 0) {
3004 base_qual_type = field_qual_type;
3005 base_bitwidth = field_bitwidth;
3006 } else {
3007 if (is_hfa)
3008 return 0;
3009 is_hva = true;
3010 if (base_bitwidth != field_bitwidth)
3011 return 0;
3012 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3013 return 0;
3014 }
3015 } else
3016 return 0;
3017 ++num_fields;
3018 }
3019 if (base_type_ptr)
3020 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3021 return num_fields;
3022 }
3023 }
3024 }
3025 break;
3026
3027 case clang::Type::Typedef:
3028 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3029 ->getDecl()
3030 ->getUnderlyingType()
3031 .getAsOpaquePtr(),
3032 base_type_ptr);
3033
3034 case clang::Type::Auto:
3035 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3036 ->getDeducedType()
3037 .getAsOpaquePtr(),
3038 base_type_ptr);
3039
3040 case clang::Type::Elaborated:
3041 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3042 ->getNamedType()
3043 .getAsOpaquePtr(),
3044 base_type_ptr);
3045 default:
3046 break;
3047 }
3048 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003049}
3050
Kate Stoneb9c1b512016-09-06 20:57:50 +00003051size_t ClangASTContext::GetNumberOfFunctionArguments(
3052 lldb::opaque_compiler_type_t type) {
3053 if (type) {
3054 clang::QualType qual_type(GetCanonicalQualType(type));
3055 const clang::FunctionProtoType *func =
3056 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3057 if (func)
3058 return func->getNumParams();
3059 }
3060 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003061}
3062
Greg Claytona1e5dc82015-08-11 22:53:00 +00003063CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003064ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3065 const size_t index) {
3066 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003067 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003068 const clang::FunctionProtoType *func =
3069 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3070 if (func) {
3071 if (index < func->getNumParams())
3072 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003073 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003074 }
3075 return CompilerType();
3076}
3077
3078bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3079 if (type) {
3080 clang::QualType qual_type(GetCanonicalQualType(type));
3081
3082 if (qual_type->isFunctionPointerType())
3083 return true;
3084
3085 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3086 switch (type_class) {
3087 default:
3088 break;
3089 case clang::Type::Typedef:
3090 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3091 ->getDecl()
3092 ->getUnderlyingType()
3093 .getAsOpaquePtr());
3094 case clang::Type::Auto:
3095 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3096 ->getDeducedType()
3097 .getAsOpaquePtr());
3098 case clang::Type::Elaborated:
3099 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3100 ->getNamedType()
3101 .getAsOpaquePtr());
3102 case clang::Type::Paren:
3103 return IsFunctionPointerType(
3104 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3105
3106 case clang::Type::LValueReference:
3107 case clang::Type::RValueReference: {
3108 const clang::ReferenceType *reference_type =
3109 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3110 if (reference_type)
3111 return IsFunctionPointerType(
3112 reference_type->getPointeeType().getAsOpaquePtr());
3113 } break;
3114 }
3115 }
3116 return false;
3117}
3118
3119bool ClangASTContext::IsBlockPointerType(
3120 lldb::opaque_compiler_type_t type,
3121 CompilerType *function_pointer_type_ptr) {
3122 if (type) {
3123 clang::QualType qual_type(GetCanonicalQualType(type));
3124
3125 if (qual_type->isBlockPointerType()) {
3126 if (function_pointer_type_ptr) {
3127 const clang::BlockPointerType *block_pointer_type =
3128 qual_type->getAs<clang::BlockPointerType>();
3129 QualType pointee_type = block_pointer_type->getPointeeType();
3130 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3131 *function_pointer_type_ptr =
3132 CompilerType(getASTContext(), function_pointer_type);
3133 }
3134 return true;
3135 }
3136
3137 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3138 switch (type_class) {
3139 default:
3140 break;
3141 case clang::Type::Typedef:
3142 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3143 ->getDecl()
3144 ->getUnderlyingType()
3145 .getAsOpaquePtr(),
3146 function_pointer_type_ptr);
3147 case clang::Type::Auto:
3148 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3149 ->getDeducedType()
3150 .getAsOpaquePtr(),
3151 function_pointer_type_ptr);
3152 case clang::Type::Elaborated:
3153 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3154 ->getNamedType()
3155 .getAsOpaquePtr(),
3156 function_pointer_type_ptr);
3157 case clang::Type::Paren:
3158 return IsBlockPointerType(
3159 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3160 function_pointer_type_ptr);
3161
3162 case clang::Type::LValueReference:
3163 case clang::Type::RValueReference: {
3164 const clang::ReferenceType *reference_type =
3165 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3166 if (reference_type)
3167 return IsBlockPointerType(
3168 reference_type->getPointeeType().getAsOpaquePtr(),
3169 function_pointer_type_ptr);
3170 } break;
3171 }
3172 }
3173 return false;
3174}
3175
3176bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3177 bool &is_signed) {
3178 if (!type)
3179 return false;
3180
3181 clang::QualType qual_type(GetCanonicalQualType(type));
3182 const clang::BuiltinType *builtin_type =
3183 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3184
3185 if (builtin_type) {
3186 if (builtin_type->isInteger()) {
3187 is_signed = builtin_type->isSignedInteger();
3188 return true;
3189 }
3190 }
3191
3192 return false;
3193}
3194
3195bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3196 bool &is_signed) {
3197 if (type) {
3198 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3199 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3200
3201 if (enum_type) {
3202 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3203 is_signed);
3204 return true;
3205 }
3206 }
3207
3208 return false;
3209}
3210
3211bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3212 CompilerType *pointee_type) {
3213 if (type) {
3214 clang::QualType qual_type(GetCanonicalQualType(type));
3215 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3216 switch (type_class) {
3217 case clang::Type::Builtin:
3218 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3219 default:
3220 break;
3221 case clang::BuiltinType::ObjCId:
3222 case clang::BuiltinType::ObjCClass:
3223 return true;
3224 }
3225 return false;
3226 case clang::Type::ObjCObjectPointer:
3227 if (pointee_type)
3228 pointee_type->SetCompilerType(
3229 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3230 ->getPointeeType());
3231 return true;
3232 case clang::Type::BlockPointer:
3233 if (pointee_type)
3234 pointee_type->SetCompilerType(
3235 getASTContext(),
3236 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3237 return true;
3238 case clang::Type::Pointer:
3239 if (pointee_type)
3240 pointee_type->SetCompilerType(
3241 getASTContext(),
3242 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3243 return true;
3244 case clang::Type::MemberPointer:
3245 if (pointee_type)
3246 pointee_type->SetCompilerType(
3247 getASTContext(),
3248 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3249 return true;
3250 case clang::Type::Typedef:
3251 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3252 ->getDecl()
3253 ->getUnderlyingType()
3254 .getAsOpaquePtr(),
3255 pointee_type);
3256 case clang::Type::Auto:
3257 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3258 ->getDeducedType()
3259 .getAsOpaquePtr(),
3260 pointee_type);
3261 case clang::Type::Elaborated:
3262 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3263 ->getNamedType()
3264 .getAsOpaquePtr(),
3265 pointee_type);
3266 case clang::Type::Paren:
3267 return IsPointerType(
3268 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3269 pointee_type);
3270 default:
3271 break;
3272 }
3273 }
3274 if (pointee_type)
3275 pointee_type->Clear();
3276 return false;
3277}
3278
3279bool ClangASTContext::IsPointerOrReferenceType(
3280 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3281 if (type) {
3282 clang::QualType qual_type(GetCanonicalQualType(type));
3283 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3284 switch (type_class) {
3285 case clang::Type::Builtin:
3286 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3287 default:
3288 break;
3289 case clang::BuiltinType::ObjCId:
3290 case clang::BuiltinType::ObjCClass:
3291 return true;
3292 }
3293 return false;
3294 case clang::Type::ObjCObjectPointer:
3295 if (pointee_type)
3296 pointee_type->SetCompilerType(
3297 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3298 ->getPointeeType());
3299 return true;
3300 case clang::Type::BlockPointer:
3301 if (pointee_type)
3302 pointee_type->SetCompilerType(
3303 getASTContext(),
3304 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3305 return true;
3306 case clang::Type::Pointer:
3307 if (pointee_type)
3308 pointee_type->SetCompilerType(
3309 getASTContext(),
3310 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3311 return true;
3312 case clang::Type::MemberPointer:
3313 if (pointee_type)
3314 pointee_type->SetCompilerType(
3315 getASTContext(),
3316 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3317 return true;
3318 case clang::Type::LValueReference:
3319 if (pointee_type)
3320 pointee_type->SetCompilerType(
3321 getASTContext(),
3322 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3323 return true;
3324 case clang::Type::RValueReference:
3325 if (pointee_type)
3326 pointee_type->SetCompilerType(
3327 getASTContext(),
3328 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3329 return true;
3330 case clang::Type::Typedef:
3331 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3332 ->getDecl()
3333 ->getUnderlyingType()
3334 .getAsOpaquePtr(),
3335 pointee_type);
3336 case clang::Type::Auto:
3337 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3338 ->getDeducedType()
3339 .getAsOpaquePtr(),
3340 pointee_type);
3341 case clang::Type::Elaborated:
3342 return IsPointerOrReferenceType(
3343 llvm::cast<clang::ElaboratedType>(qual_type)
3344 ->getNamedType()
3345 .getAsOpaquePtr(),
3346 pointee_type);
3347 case clang::Type::Paren:
3348 return IsPointerOrReferenceType(
3349 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3350 pointee_type);
3351 default:
3352 break;
3353 }
3354 }
3355 if (pointee_type)
3356 pointee_type->Clear();
3357 return false;
3358}
3359
3360bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3361 CompilerType *pointee_type,
3362 bool *is_rvalue) {
3363 if (type) {
3364 clang::QualType qual_type(GetCanonicalQualType(type));
3365 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3366
3367 switch (type_class) {
3368 case clang::Type::LValueReference:
3369 if (pointee_type)
3370 pointee_type->SetCompilerType(
3371 getASTContext(),
3372 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3373 if (is_rvalue)
3374 *is_rvalue = false;
3375 return true;
3376 case clang::Type::RValueReference:
3377 if (pointee_type)
3378 pointee_type->SetCompilerType(
3379 getASTContext(),
3380 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3381 if (is_rvalue)
3382 *is_rvalue = true;
3383 return true;
3384 case clang::Type::Typedef:
3385 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3386 ->getDecl()
3387 ->getUnderlyingType()
3388 .getAsOpaquePtr(),
3389 pointee_type, is_rvalue);
3390 case clang::Type::Auto:
3391 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3392 ->getDeducedType()
3393 .getAsOpaquePtr(),
3394 pointee_type, is_rvalue);
3395 case clang::Type::Elaborated:
3396 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3397 ->getNamedType()
3398 .getAsOpaquePtr(),
3399 pointee_type, is_rvalue);
3400 case clang::Type::Paren:
3401 return IsReferenceType(
3402 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3403 pointee_type, is_rvalue);
3404
3405 default:
3406 break;
3407 }
3408 }
3409 if (pointee_type)
3410 pointee_type->Clear();
3411 return false;
3412}
3413
3414bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3415 uint32_t &count, bool &is_complex) {
3416 if (type) {
3417 clang::QualType qual_type(GetCanonicalQualType(type));
3418
3419 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3420 qual_type->getCanonicalTypeInternal())) {
3421 clang::BuiltinType::Kind kind = BT->getKind();
3422 if (kind >= clang::BuiltinType::Float &&
3423 kind <= clang::BuiltinType::LongDouble) {
3424 count = 1;
3425 is_complex = false;
3426 return true;
3427 }
3428 } else if (const clang::ComplexType *CT =
3429 llvm::dyn_cast<clang::ComplexType>(
3430 qual_type->getCanonicalTypeInternal())) {
3431 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3432 is_complex)) {
3433 count = 2;
3434 is_complex = true;
3435 return true;
3436 }
3437 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3438 qual_type->getCanonicalTypeInternal())) {
3439 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3440 is_complex)) {
3441 count = VT->getNumElements();
3442 is_complex = false;
3443 return true;
3444 }
3445 }
3446 }
3447 count = 0;
3448 is_complex = false;
3449 return false;
3450}
3451
3452bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3453 if (!type)
3454 return false;
3455
3456 clang::QualType qual_type(GetQualType(type));
3457 const clang::TagType *tag_type =
3458 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3459 if (tag_type) {
3460 clang::TagDecl *tag_decl = tag_type->getDecl();
3461 if (tag_decl)
3462 return tag_decl->isCompleteDefinition();
3463 return false;
3464 } else {
3465 const clang::ObjCObjectType *objc_class_type =
3466 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3467 if (objc_class_type) {
3468 clang::ObjCInterfaceDecl *class_interface_decl =
3469 objc_class_type->getInterface();
3470 if (class_interface_decl)
3471 return class_interface_decl->getDefinition() != nullptr;
3472 return false;
3473 }
3474 }
3475 return true;
3476}
3477
3478bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3479 if (type) {
3480 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3481
3482 const clang::ObjCObjectPointerType *obj_pointer_type =
3483 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3484
3485 if (obj_pointer_type)
3486 return obj_pointer_type->isObjCClassType();
3487 }
3488 return false;
3489}
3490
3491bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3492 if (ClangUtil::IsClangType(type))
3493 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3494 return false;
3495}
3496
3497bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3498 if (!type)
3499 return false;
3500 clang::QualType qual_type(GetCanonicalQualType(type));
3501 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3502 return (type_class == clang::Type::Record);
3503}
3504
3505bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3506 if (!type)
3507 return false;
3508 clang::QualType qual_type(GetCanonicalQualType(type));
3509 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3510 return (type_class == clang::Type::Enum);
3511}
3512
3513bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3514 if (type) {
3515 clang::QualType qual_type(GetCanonicalQualType(type));
3516 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3517 switch (type_class) {
3518 case clang::Type::Record:
3519 if (GetCompleteType(type)) {
3520 const clang::RecordType *record_type =
3521 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3522 const clang::RecordDecl *record_decl = record_type->getDecl();
3523 if (record_decl) {
3524 const clang::CXXRecordDecl *cxx_record_decl =
3525 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3526 if (cxx_record_decl)
3527 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003528 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003529 }
3530 break;
3531
3532 default:
3533 break;
3534 }
3535 }
3536 return false;
3537}
3538
3539bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3540 CompilerType *dynamic_pointee_type,
3541 bool check_cplusplus,
3542 bool check_objc) {
3543 clang::QualType pointee_qual_type;
3544 if (type) {
3545 clang::QualType qual_type(GetCanonicalQualType(type));
3546 bool success = false;
3547 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3548 switch (type_class) {
3549 case clang::Type::Builtin:
3550 if (check_objc &&
3551 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3552 clang::BuiltinType::ObjCId) {
3553 if (dynamic_pointee_type)
3554 dynamic_pointee_type->SetCompilerType(this, type);
3555 return true;
3556 }
3557 break;
3558
3559 case clang::Type::ObjCObjectPointer:
3560 if (check_objc) {
3561 if (auto objc_pointee_type =
3562 qual_type->getPointeeType().getTypePtrOrNull()) {
3563 if (auto objc_object_type =
3564 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3565 objc_pointee_type)) {
3566 if (objc_object_type->isObjCClass())
3567 return false;
3568 }
3569 }
3570 if (dynamic_pointee_type)
3571 dynamic_pointee_type->SetCompilerType(
3572 getASTContext(),
3573 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3574 ->getPointeeType());
3575 return true;
3576 }
3577 break;
3578
3579 case clang::Type::Pointer:
3580 pointee_qual_type =
3581 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3582 success = true;
3583 break;
3584
3585 case clang::Type::LValueReference:
3586 case clang::Type::RValueReference:
3587 pointee_qual_type =
3588 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3589 success = true;
3590 break;
3591
3592 case clang::Type::Typedef:
3593 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3594 ->getDecl()
3595 ->getUnderlyingType()
3596 .getAsOpaquePtr(),
3597 dynamic_pointee_type, check_cplusplus,
3598 check_objc);
3599
3600 case clang::Type::Auto:
3601 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3602 ->getDeducedType()
3603 .getAsOpaquePtr(),
3604 dynamic_pointee_type, check_cplusplus,
3605 check_objc);
3606
3607 case clang::Type::Elaborated:
3608 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3609 ->getNamedType()
3610 .getAsOpaquePtr(),
3611 dynamic_pointee_type, check_cplusplus,
3612 check_objc);
3613
3614 case clang::Type::Paren:
3615 return IsPossibleDynamicType(
3616 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3617 dynamic_pointee_type, check_cplusplus, check_objc);
3618 default:
3619 break;
3620 }
3621
3622 if (success) {
3623 // Check to make sure what we are pointing too is a possible dynamic C++
Adrian Prantl05097242018-04-30 16:49:04 +00003624 // type We currently accept any "void *" (in case we have a class that
3625 // has been watered down to an opaque pointer) and virtual C++ classes.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003626 const clang::Type::TypeClass pointee_type_class =
3627 pointee_qual_type.getCanonicalType()->getTypeClass();
3628 switch (pointee_type_class) {
3629 case clang::Type::Builtin:
3630 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3631 case clang::BuiltinType::UnknownAny:
3632 case clang::BuiltinType::Void:
3633 if (dynamic_pointee_type)
3634 dynamic_pointee_type->SetCompilerType(getASTContext(),
3635 pointee_qual_type);
3636 return true;
3637 default:
3638 break;
3639 }
3640 break;
3641
3642 case clang::Type::Record:
3643 if (check_cplusplus) {
3644 clang::CXXRecordDecl *cxx_record_decl =
3645 pointee_qual_type->getAsCXXRecordDecl();
3646 if (cxx_record_decl) {
3647 bool is_complete = cxx_record_decl->isCompleteDefinition();
3648
3649 if (is_complete)
3650 success = cxx_record_decl->isDynamicClass();
3651 else {
3652 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3653 getASTContext(), cxx_record_decl);
3654 if (metadata)
3655 success = metadata->GetIsDynamicCXXType();
3656 else {
3657 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3658 .GetCompleteType();
3659 if (is_complete)
3660 success = cxx_record_decl->isDynamicClass();
3661 else
3662 success = false;
3663 }
3664 }
3665
3666 if (success) {
3667 if (dynamic_pointee_type)
3668 dynamic_pointee_type->SetCompilerType(getASTContext(),
3669 pointee_qual_type);
3670 return true;
3671 }
3672 }
3673 }
3674 break;
3675
3676 case clang::Type::ObjCObject:
3677 case clang::Type::ObjCInterface:
3678 if (check_objc) {
3679 if (dynamic_pointee_type)
3680 dynamic_pointee_type->SetCompilerType(getASTContext(),
3681 pointee_qual_type);
3682 return true;
3683 }
3684 break;
3685
3686 default:
3687 break;
3688 }
3689 }
3690 }
3691 if (dynamic_pointee_type)
3692 dynamic_pointee_type->Clear();
3693 return false;
3694}
3695
3696bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3697 if (!type)
3698 return false;
3699
3700 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3701}
3702
3703bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3704 if (!type)
3705 return false;
3706 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3707}
3708
3709bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3710 if (!type)
3711 return false;
3712 return GetCanonicalQualType(type)->isVoidType();
3713}
3714
3715bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3716 return ClangASTContextSupportsLanguage(language);
3717}
3718
3719bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3720 std::string &class_name) {
3721 if (type) {
3722 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3723 if (!qual_type.isNull()) {
3724 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3725 if (cxx_record_decl) {
3726 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3727 return true;
3728 }
3729 }
3730 }
3731 class_name.clear();
3732 return false;
3733}
3734
3735bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3736 if (!type)
3737 return false;
3738
3739 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3740 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
3741 return true;
3742 return false;
3743}
3744
3745bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3746 if (!type)
3747 return false;
3748 clang::QualType qual_type(GetCanonicalQualType(type));
3749 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3750 if (tag_type)
3751 return tag_type->isBeingDefined();
3752 return false;
3753}
3754
3755bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3756 CompilerType *class_type_ptr) {
3757 if (!type)
3758 return false;
3759
3760 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3761
3762 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3763 if (class_type_ptr) {
3764 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3765 const clang::ObjCObjectPointerType *obj_pointer_type =
3766 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3767 if (obj_pointer_type == nullptr)
3768 class_type_ptr->Clear();
3769 else
3770 class_type_ptr->SetCompilerType(
3771 type.GetTypeSystem(),
3772 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3773 .getAsOpaquePtr());
3774 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003775 }
3776 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003777 }
3778 if (class_type_ptr)
3779 class_type_ptr->Clear();
3780 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003781}
3782
Kate Stoneb9c1b512016-09-06 20:57:50 +00003783bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3784 std::string &class_name) {
3785 if (!type)
3786 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003787
Kate Stoneb9c1b512016-09-06 20:57:50 +00003788 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3789
3790 const clang::ObjCObjectType *object_type =
3791 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3792 if (object_type) {
3793 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3794 if (interface) {
3795 class_name = interface->getNameAsString();
3796 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00003797 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003798 }
3799 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003800}
3801
Greg Claytond8d4a572015-08-11 21:38:15 +00003802//----------------------------------------------------------------------
3803// Type Completion
3804//----------------------------------------------------------------------
3805
Kate Stoneb9c1b512016-09-06 20:57:50 +00003806bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3807 if (!type)
3808 return false;
3809 const bool allow_completion = true;
3810 return GetCompleteQualType(getASTContext(), GetQualType(type),
3811 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00003812}
3813
Kate Stoneb9c1b512016-09-06 20:57:50 +00003814ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
3815 std::string type_name;
3816 if (type) {
3817 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
3818 clang::QualType qual_type(GetQualType(type));
3819 printing_policy.SuppressTagKeyword = true;
3820 const clang::TypedefType *typedef_type =
3821 qual_type->getAs<clang::TypedefType>();
3822 if (typedef_type) {
3823 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3824 type_name = typedef_decl->getQualifiedNameAsString();
3825 } else {
3826 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003827 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003828 }
3829 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00003830}
3831
3832uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003833ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
3834 CompilerType *pointee_or_element_clang_type) {
3835 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003836 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003837
3838 if (pointee_or_element_clang_type)
3839 pointee_or_element_clang_type->Clear();
3840
3841 clang::QualType qual_type(GetQualType(type));
3842
3843 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3844 switch (type_class) {
Sean Callananddf802a2017-06-02 01:24:18 +00003845 case clang::Type::Attributed:
3846 return GetTypeInfo(
3847 qual_type->getAs<clang::AttributedType>()
3848 ->getModifiedType().getAsOpaquePtr(),
3849 pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003850 case clang::Type::Builtin: {
3851 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3852 qual_type->getCanonicalTypeInternal());
3853
3854 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3855 switch (builtin_type->getKind()) {
3856 case clang::BuiltinType::ObjCId:
3857 case clang::BuiltinType::ObjCClass:
3858 if (pointee_or_element_clang_type)
3859 pointee_or_element_clang_type->SetCompilerType(
3860 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
3861 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3862 break;
3863
3864 case clang::BuiltinType::ObjCSel:
3865 if (pointee_or_element_clang_type)
3866 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
3867 getASTContext()->CharTy);
3868 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3869 break;
3870
3871 case clang::BuiltinType::Bool:
3872 case clang::BuiltinType::Char_U:
3873 case clang::BuiltinType::UChar:
3874 case clang::BuiltinType::WChar_U:
3875 case clang::BuiltinType::Char16:
3876 case clang::BuiltinType::Char32:
3877 case clang::BuiltinType::UShort:
3878 case clang::BuiltinType::UInt:
3879 case clang::BuiltinType::ULong:
3880 case clang::BuiltinType::ULongLong:
3881 case clang::BuiltinType::UInt128:
3882 case clang::BuiltinType::Char_S:
3883 case clang::BuiltinType::SChar:
3884 case clang::BuiltinType::WChar_S:
3885 case clang::BuiltinType::Short:
3886 case clang::BuiltinType::Int:
3887 case clang::BuiltinType::Long:
3888 case clang::BuiltinType::LongLong:
3889 case clang::BuiltinType::Int128:
3890 case clang::BuiltinType::Float:
3891 case clang::BuiltinType::Double:
3892 case clang::BuiltinType::LongDouble:
3893 builtin_type_flags |= eTypeIsScalar;
3894 if (builtin_type->isInteger()) {
3895 builtin_type_flags |= eTypeIsInteger;
3896 if (builtin_type->isSignedInteger())
3897 builtin_type_flags |= eTypeIsSigned;
3898 } else if (builtin_type->isFloatingPoint())
3899 builtin_type_flags |= eTypeIsFloat;
3900 break;
3901 default:
3902 break;
3903 }
3904 return builtin_type_flags;
3905 }
3906
3907 case clang::Type::BlockPointer:
3908 if (pointee_or_element_clang_type)
3909 pointee_or_element_clang_type->SetCompilerType(
3910 getASTContext(), qual_type->getPointeeType());
3911 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3912
3913 case clang::Type::Complex: {
3914 uint32_t complex_type_flags =
3915 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3916 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
3917 qual_type->getCanonicalTypeInternal());
3918 if (complex_type) {
3919 clang::QualType complex_element_type(complex_type->getElementType());
3920 if (complex_element_type->isIntegerType())
3921 complex_type_flags |= eTypeIsFloat;
3922 else if (complex_element_type->isFloatingType())
3923 complex_type_flags |= eTypeIsInteger;
3924 }
3925 return complex_type_flags;
3926 } break;
3927
3928 case clang::Type::ConstantArray:
3929 case clang::Type::DependentSizedArray:
3930 case clang::Type::IncompleteArray:
3931 case clang::Type::VariableArray:
3932 if (pointee_or_element_clang_type)
3933 pointee_or_element_clang_type->SetCompilerType(
3934 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
3935 ->getElementType());
3936 return eTypeHasChildren | eTypeIsArray;
3937
3938 case clang::Type::DependentName:
3939 return 0;
3940 case clang::Type::DependentSizedExtVector:
3941 return eTypeHasChildren | eTypeIsVector;
3942 case clang::Type::DependentTemplateSpecialization:
3943 return eTypeIsTemplate;
3944 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00003945 return CompilerType(
3946 getASTContext(),
3947 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
3948 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003949
3950 case clang::Type::Enum:
3951 if (pointee_or_element_clang_type)
3952 pointee_or_element_clang_type->SetCompilerType(
3953 getASTContext(),
3954 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
3955 return eTypeIsEnumeration | eTypeHasValue;
3956
3957 case clang::Type::Auto:
3958 return CompilerType(
3959 getASTContext(),
3960 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
3961 .GetTypeInfo(pointee_or_element_clang_type);
3962 case clang::Type::Elaborated:
3963 return CompilerType(
3964 getASTContext(),
3965 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
3966 .GetTypeInfo(pointee_or_element_clang_type);
3967 case clang::Type::Paren:
3968 return CompilerType(getASTContext(),
3969 llvm::cast<clang::ParenType>(qual_type)->desugar())
3970 .GetTypeInfo(pointee_or_element_clang_type);
3971
3972 case clang::Type::FunctionProto:
3973 return eTypeIsFuncPrototype | eTypeHasValue;
3974 case clang::Type::FunctionNoProto:
3975 return eTypeIsFuncPrototype | eTypeHasValue;
3976 case clang::Type::InjectedClassName:
3977 return 0;
3978
3979 case clang::Type::LValueReference:
3980 case clang::Type::RValueReference:
3981 if (pointee_or_element_clang_type)
3982 pointee_or_element_clang_type->SetCompilerType(
3983 getASTContext(),
3984 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
3985 ->getPointeeType());
3986 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
3987
3988 case clang::Type::MemberPointer:
3989 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
3990
3991 case clang::Type::ObjCObjectPointer:
3992 if (pointee_or_element_clang_type)
3993 pointee_or_element_clang_type->SetCompilerType(
3994 getASTContext(), qual_type->getPointeeType());
3995 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
3996 eTypeHasValue;
3997
3998 case clang::Type::ObjCObject:
3999 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4000 case clang::Type::ObjCInterface:
4001 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4002
4003 case clang::Type::Pointer:
4004 if (pointee_or_element_clang_type)
4005 pointee_or_element_clang_type->SetCompilerType(
4006 getASTContext(), qual_type->getPointeeType());
4007 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4008
4009 case clang::Type::Record:
4010 if (qual_type->getAsCXXRecordDecl())
4011 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4012 else
4013 return eTypeHasChildren | eTypeIsStructUnion;
4014 break;
4015 case clang::Type::SubstTemplateTypeParm:
4016 return eTypeIsTemplate;
4017 case clang::Type::TemplateTypeParm:
4018 return eTypeIsTemplate;
4019 case clang::Type::TemplateSpecialization:
4020 return eTypeIsTemplate;
4021
4022 case clang::Type::Typedef:
4023 return eTypeIsTypedef |
4024 CompilerType(getASTContext(),
4025 llvm::cast<clang::TypedefType>(qual_type)
4026 ->getDecl()
4027 ->getUnderlyingType())
4028 .GetTypeInfo(pointee_or_element_clang_type);
4029 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004030 return CompilerType(getASTContext(),
4031 llvm::cast<clang::TypeOfExprType>(qual_type)
4032 ->getUnderlyingExpr()
4033 ->getType())
4034 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004035 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004036 return CompilerType(
4037 getASTContext(),
4038 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4039 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004040 case clang::Type::UnresolvedUsing:
4041 return 0;
4042
4043 case clang::Type::ExtVector:
4044 case clang::Type::Vector: {
4045 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4046 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4047 qual_type->getCanonicalTypeInternal());
4048 if (vector_type) {
4049 if (vector_type->isIntegerType())
4050 vector_type_flags |= eTypeIsFloat;
4051 else if (vector_type->isFloatingType())
4052 vector_type_flags |= eTypeIsInteger;
4053 }
4054 return vector_type_flags;
4055 }
4056 default:
4057 return 0;
4058 }
4059 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004060}
4061
Greg Claytond8d4a572015-08-11 21:38:15 +00004062lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004063ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4064 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004065 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004066
4067 // If the type is a reference, then resolve it to what it refers to first:
4068 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4069 if (qual_type->isAnyPointerType()) {
4070 if (qual_type->isObjCObjectPointerType())
4071 return lldb::eLanguageTypeObjC;
4072
4073 clang::QualType pointee_type(qual_type->getPointeeType());
4074 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4075 return lldb::eLanguageTypeC_plus_plus;
4076 if (pointee_type->isObjCObjectOrInterfaceType())
4077 return lldb::eLanguageTypeObjC;
4078 if (pointee_type->isObjCClassType())
4079 return lldb::eLanguageTypeObjC;
4080 if (pointee_type.getTypePtr() ==
4081 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4082 return lldb::eLanguageTypeObjC;
4083 } else {
4084 if (qual_type->isObjCObjectOrInterfaceType())
4085 return lldb::eLanguageTypeObjC;
4086 if (qual_type->getAsCXXRecordDecl())
4087 return lldb::eLanguageTypeC_plus_plus;
4088 switch (qual_type->getTypeClass()) {
4089 default:
4090 break;
4091 case clang::Type::Builtin:
4092 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4093 default:
4094 case clang::BuiltinType::Void:
4095 case clang::BuiltinType::Bool:
4096 case clang::BuiltinType::Char_U:
4097 case clang::BuiltinType::UChar:
4098 case clang::BuiltinType::WChar_U:
4099 case clang::BuiltinType::Char16:
4100 case clang::BuiltinType::Char32:
4101 case clang::BuiltinType::UShort:
4102 case clang::BuiltinType::UInt:
4103 case clang::BuiltinType::ULong:
4104 case clang::BuiltinType::ULongLong:
4105 case clang::BuiltinType::UInt128:
4106 case clang::BuiltinType::Char_S:
4107 case clang::BuiltinType::SChar:
4108 case clang::BuiltinType::WChar_S:
4109 case clang::BuiltinType::Short:
4110 case clang::BuiltinType::Int:
4111 case clang::BuiltinType::Long:
4112 case clang::BuiltinType::LongLong:
4113 case clang::BuiltinType::Int128:
4114 case clang::BuiltinType::Float:
4115 case clang::BuiltinType::Double:
4116 case clang::BuiltinType::LongDouble:
4117 break;
4118
4119 case clang::BuiltinType::NullPtr:
4120 return eLanguageTypeC_plus_plus;
4121
4122 case clang::BuiltinType::ObjCId:
4123 case clang::BuiltinType::ObjCClass:
4124 case clang::BuiltinType::ObjCSel:
4125 return eLanguageTypeObjC;
4126
4127 case clang::BuiltinType::Dependent:
4128 case clang::BuiltinType::Overload:
4129 case clang::BuiltinType::BoundMember:
4130 case clang::BuiltinType::UnknownAny:
4131 break;
4132 }
4133 break;
4134 case clang::Type::Typedef:
4135 return CompilerType(getASTContext(),
4136 llvm::cast<clang::TypedefType>(qual_type)
4137 ->getDecl()
4138 ->getUnderlyingType())
4139 .GetMinimumLanguage();
4140 }
4141 }
4142 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004143}
4144
4145lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004146ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4147 if (!type)
4148 return lldb::eTypeClassInvalid;
4149
4150 clang::QualType qual_type(GetQualType(type));
4151
4152 switch (qual_type->getTypeClass()) {
4153 case clang::Type::UnaryTransform:
4154 break;
4155 case clang::Type::FunctionNoProto:
4156 return lldb::eTypeClassFunction;
4157 case clang::Type::FunctionProto:
4158 return lldb::eTypeClassFunction;
4159 case clang::Type::IncompleteArray:
4160 return lldb::eTypeClassArray;
4161 case clang::Type::VariableArray:
4162 return lldb::eTypeClassArray;
4163 case clang::Type::ConstantArray:
4164 return lldb::eTypeClassArray;
4165 case clang::Type::DependentSizedArray:
4166 return lldb::eTypeClassArray;
4167 case clang::Type::DependentSizedExtVector:
4168 return lldb::eTypeClassVector;
4169 case clang::Type::ExtVector:
4170 return lldb::eTypeClassVector;
4171 case clang::Type::Vector:
4172 return lldb::eTypeClassVector;
4173 case clang::Type::Builtin:
4174 return lldb::eTypeClassBuiltin;
4175 case clang::Type::ObjCObjectPointer:
4176 return lldb::eTypeClassObjCObjectPointer;
4177 case clang::Type::BlockPointer:
4178 return lldb::eTypeClassBlockPointer;
4179 case clang::Type::Pointer:
4180 return lldb::eTypeClassPointer;
4181 case clang::Type::LValueReference:
4182 return lldb::eTypeClassReference;
4183 case clang::Type::RValueReference:
4184 return lldb::eTypeClassReference;
4185 case clang::Type::MemberPointer:
4186 return lldb::eTypeClassMemberPointer;
4187 case clang::Type::Complex:
4188 if (qual_type->isComplexType())
4189 return lldb::eTypeClassComplexFloat;
4190 else
4191 return lldb::eTypeClassComplexInteger;
4192 case clang::Type::ObjCObject:
4193 return lldb::eTypeClassObjCObject;
4194 case clang::Type::ObjCInterface:
4195 return lldb::eTypeClassObjCInterface;
4196 case clang::Type::Record: {
4197 const clang::RecordType *record_type =
4198 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4199 const clang::RecordDecl *record_decl = record_type->getDecl();
4200 if (record_decl->isUnion())
4201 return lldb::eTypeClassUnion;
4202 else if (record_decl->isStruct())
4203 return lldb::eTypeClassStruct;
4204 else
4205 return lldb::eTypeClassClass;
4206 } break;
4207 case clang::Type::Enum:
4208 return lldb::eTypeClassEnumeration;
4209 case clang::Type::Typedef:
4210 return lldb::eTypeClassTypedef;
4211 case clang::Type::UnresolvedUsing:
4212 break;
4213 case clang::Type::Paren:
4214 return CompilerType(getASTContext(),
4215 llvm::cast<clang::ParenType>(qual_type)->desugar())
4216 .GetTypeClass();
4217 case clang::Type::Auto:
4218 return CompilerType(
4219 getASTContext(),
4220 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4221 .GetTypeClass();
4222 case clang::Type::Elaborated:
4223 return CompilerType(
4224 getASTContext(),
4225 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4226 .GetTypeClass();
4227
4228 case clang::Type::Attributed:
4229 break;
4230 case clang::Type::TemplateTypeParm:
4231 break;
4232 case clang::Type::SubstTemplateTypeParm:
4233 break;
4234 case clang::Type::SubstTemplateTypeParmPack:
4235 break;
4236 case clang::Type::InjectedClassName:
4237 break;
4238 case clang::Type::DependentName:
4239 break;
4240 case clang::Type::DependentTemplateSpecialization:
4241 break;
4242 case clang::Type::PackExpansion:
4243 break;
4244
4245 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004246 return CompilerType(getASTContext(),
4247 llvm::cast<clang::TypeOfExprType>(qual_type)
4248 ->getUnderlyingExpr()
4249 ->getType())
4250 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004251 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004252 return CompilerType(
4253 getASTContext(),
4254 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4255 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004256 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004257 return CompilerType(
4258 getASTContext(),
4259 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4260 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004261 case clang::Type::TemplateSpecialization:
4262 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004263 case clang::Type::DeducedTemplateSpecialization:
4264 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004265 case clang::Type::Atomic:
4266 break;
4267 case clang::Type::Pipe:
4268 break;
4269
4270 // pointer type decayed from an array or function type.
4271 case clang::Type::Decayed:
4272 break;
4273 case clang::Type::Adjusted:
4274 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004275 case clang::Type::ObjCTypeParam:
4276 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00004277
4278 case clang::Type::DependentAddressSpace:
4279 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004280 }
4281 // We don't know hot to display this type...
4282 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004283}
4284
Kate Stoneb9c1b512016-09-06 20:57:50 +00004285unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4286 if (type)
4287 return GetQualType(type).getQualifiers().getCVRQualifiers();
4288 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004289}
4290
4291//----------------------------------------------------------------------
4292// Creating related types
4293//----------------------------------------------------------------------
4294
Greg Claytona1e5dc82015-08-11 22:53:00 +00004295CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004296ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4297 uint64_t *stride) {
4298 if (type) {
4299 clang::QualType qual_type(GetCanonicalQualType(type));
4300
4301 const clang::Type *array_eletype =
4302 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4303
4304 if (!array_eletype)
4305 return CompilerType();
4306
4307 CompilerType element_type(getASTContext(),
4308 array_eletype->getCanonicalTypeUnqualified());
4309
4310 // TODO: the real stride will be >= this value.. find the real one!
4311 if (stride)
4312 *stride = element_type.GetByteSize(nullptr);
4313
4314 return element_type;
4315 }
4316 return CompilerType();
4317}
4318
4319CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4320 uint64_t size) {
4321 if (type) {
4322 clang::QualType qual_type(GetCanonicalQualType(type));
4323 if (clang::ASTContext *ast_ctx = getASTContext()) {
4324 if (size != 0)
4325 return CompilerType(
4326 ast_ctx, ast_ctx->getConstantArrayType(
4327 qual_type, llvm::APInt(64, size),
4328 clang::ArrayType::ArraySizeModifier::Normal, 0));
4329 else
4330 return CompilerType(
4331 ast_ctx,
4332 ast_ctx->getIncompleteArrayType(
4333 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
Greg Claytond8d4a572015-08-11 21:38:15 +00004334 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004335 }
4336
4337 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004338}
4339
Greg Claytona1e5dc82015-08-11 22:53:00 +00004340CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004341ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4342 if (type)
4343 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4344 return CompilerType();
4345}
4346
4347static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4348 clang::QualType qual_type) {
4349 if (qual_type->isPointerType())
4350 qual_type = ast->getPointerType(
4351 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4352 else
4353 qual_type = qual_type.getUnqualifiedType();
4354 qual_type.removeLocalConst();
4355 qual_type.removeLocalRestrict();
4356 qual_type.removeLocalVolatile();
4357 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004358}
4359
4360CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004361ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4362 if (type)
4363 return CompilerType(
4364 getASTContext(),
4365 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4366 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004367}
4368
Kate Stoneb9c1b512016-09-06 20:57:50 +00004369int ClangASTContext::GetFunctionArgumentCount(
4370 lldb::opaque_compiler_type_t type) {
4371 if (type) {
4372 const clang::FunctionProtoType *func =
4373 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4374 if (func)
4375 return func->getNumParams();
4376 }
4377 return -1;
4378}
4379
4380CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4381 lldb::opaque_compiler_type_t type, size_t idx) {
4382 if (type) {
4383 const clang::FunctionProtoType *func =
4384 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4385 if (func) {
4386 const uint32_t num_args = func->getNumParams();
4387 if (idx < num_args)
4388 return CompilerType(getASTContext(), func->getParamType(idx));
4389 }
4390 }
4391 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004392}
4393
Greg Claytona1e5dc82015-08-11 22:53:00 +00004394CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004395ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4396 if (type) {
4397 clang::QualType qual_type(GetQualType(type));
4398 const clang::FunctionProtoType *func =
4399 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4400 if (func)
4401 return CompilerType(getASTContext(), func->getReturnType());
4402 }
4403 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004404}
4405
4406size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004407ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4408 size_t num_functions = 0;
4409 if (type) {
4410 clang::QualType qual_type(GetCanonicalQualType(type));
4411 switch (qual_type->getTypeClass()) {
4412 case clang::Type::Record:
4413 if (GetCompleteQualType(getASTContext(), qual_type)) {
4414 const clang::RecordType *record_type =
4415 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4416 const clang::RecordDecl *record_decl = record_type->getDecl();
4417 assert(record_decl);
4418 const clang::CXXRecordDecl *cxx_record_decl =
4419 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4420 if (cxx_record_decl)
4421 num_functions = std::distance(cxx_record_decl->method_begin(),
4422 cxx_record_decl->method_end());
4423 }
4424 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004425
Sean Callananf9c622a2016-09-30 18:44:43 +00004426 case clang::Type::ObjCObjectPointer: {
4427 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004428 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004429 const clang::ObjCInterfaceType *objc_interface_type =
4430 objc_class_type->getInterfaceType();
4431 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004432 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4433 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004434 clang::ObjCInterfaceDecl *class_interface_decl =
4435 objc_interface_type->getDecl();
4436 if (class_interface_decl) {
4437 num_functions = std::distance(class_interface_decl->meth_begin(),
4438 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004439 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004440 }
4441 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004442 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004443
4444 case clang::Type::ObjCObject:
4445 case clang::Type::ObjCInterface:
4446 if (GetCompleteType(type)) {
4447 const clang::ObjCObjectType *objc_class_type =
4448 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4449 if (objc_class_type) {
4450 clang::ObjCInterfaceDecl *class_interface_decl =
4451 objc_class_type->getInterface();
4452 if (class_interface_decl)
4453 num_functions = std::distance(class_interface_decl->meth_begin(),
4454 class_interface_decl->meth_end());
4455 }
4456 }
4457 break;
4458
4459 case clang::Type::Typedef:
4460 return CompilerType(getASTContext(),
4461 llvm::cast<clang::TypedefType>(qual_type)
4462 ->getDecl()
4463 ->getUnderlyingType())
4464 .GetNumMemberFunctions();
4465
4466 case clang::Type::Auto:
4467 return CompilerType(
4468 getASTContext(),
4469 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4470 .GetNumMemberFunctions();
4471
4472 case clang::Type::Elaborated:
4473 return CompilerType(
4474 getASTContext(),
4475 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4476 .GetNumMemberFunctions();
4477
4478 case clang::Type::Paren:
4479 return CompilerType(getASTContext(),
4480 llvm::cast<clang::ParenType>(qual_type)->desugar())
4481 .GetNumMemberFunctions();
4482
4483 default:
4484 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004485 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004486 }
4487 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004488}
4489
4490TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004491ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4492 size_t idx) {
4493 std::string name;
4494 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4495 CompilerType clang_type;
4496 CompilerDecl clang_decl;
4497 if (type) {
4498 clang::QualType qual_type(GetCanonicalQualType(type));
4499 switch (qual_type->getTypeClass()) {
4500 case clang::Type::Record:
4501 if (GetCompleteQualType(getASTContext(), qual_type)) {
4502 const clang::RecordType *record_type =
4503 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4504 const clang::RecordDecl *record_decl = record_type->getDecl();
4505 assert(record_decl);
4506 const clang::CXXRecordDecl *cxx_record_decl =
4507 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4508 if (cxx_record_decl) {
4509 auto method_iter = cxx_record_decl->method_begin();
4510 auto method_end = cxx_record_decl->method_end();
4511 if (idx <
4512 static_cast<size_t>(std::distance(method_iter, method_end))) {
4513 std::advance(method_iter, idx);
4514 clang::CXXMethodDecl *cxx_method_decl =
4515 method_iter->getCanonicalDecl();
4516 if (cxx_method_decl) {
4517 name = cxx_method_decl->getDeclName().getAsString();
4518 if (cxx_method_decl->isStatic())
4519 kind = lldb::eMemberFunctionKindStaticMethod;
4520 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4521 kind = lldb::eMemberFunctionKindConstructor;
4522 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4523 kind = lldb::eMemberFunctionKindDestructor;
4524 else
4525 kind = lldb::eMemberFunctionKindInstanceMethod;
4526 clang_type = CompilerType(
4527 this, cxx_method_decl->getType().getAsOpaquePtr());
4528 clang_decl = CompilerDecl(this, cxx_method_decl);
4529 }
4530 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004531 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004532 }
4533 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004534
Sean Callananf9c622a2016-09-30 18:44:43 +00004535 case clang::Type::ObjCObjectPointer: {
4536 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004537 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004538 const clang::ObjCInterfaceType *objc_interface_type =
4539 objc_class_type->getInterfaceType();
4540 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004541 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4542 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004543 clang::ObjCInterfaceDecl *class_interface_decl =
4544 objc_interface_type->getDecl();
4545 if (class_interface_decl) {
4546 auto method_iter = class_interface_decl->meth_begin();
4547 auto method_end = class_interface_decl->meth_end();
4548 if (idx <
4549 static_cast<size_t>(std::distance(method_iter, method_end))) {
4550 std::advance(method_iter, idx);
4551 clang::ObjCMethodDecl *objc_method_decl =
4552 method_iter->getCanonicalDecl();
4553 if (objc_method_decl) {
4554 clang_decl = CompilerDecl(this, objc_method_decl);
4555 name = objc_method_decl->getSelector().getAsString();
4556 if (objc_method_decl->isClassMethod())
4557 kind = lldb::eMemberFunctionKindStaticMethod;
4558 else
4559 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004560 }
4561 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004562 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004563 }
4564 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004565 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004566
Kate Stoneb9c1b512016-09-06 20:57:50 +00004567 case clang::Type::ObjCObject:
4568 case clang::Type::ObjCInterface:
4569 if (GetCompleteType(type)) {
4570 const clang::ObjCObjectType *objc_class_type =
4571 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4572 if (objc_class_type) {
4573 clang::ObjCInterfaceDecl *class_interface_decl =
4574 objc_class_type->getInterface();
4575 if (class_interface_decl) {
4576 auto method_iter = class_interface_decl->meth_begin();
4577 auto method_end = class_interface_decl->meth_end();
4578 if (idx <
4579 static_cast<size_t>(std::distance(method_iter, method_end))) {
4580 std::advance(method_iter, idx);
4581 clang::ObjCMethodDecl *objc_method_decl =
4582 method_iter->getCanonicalDecl();
4583 if (objc_method_decl) {
4584 clang_decl = CompilerDecl(this, objc_method_decl);
4585 name = objc_method_decl->getSelector().getAsString();
4586 if (objc_method_decl->isClassMethod())
4587 kind = lldb::eMemberFunctionKindStaticMethod;
4588 else
4589 kind = lldb::eMemberFunctionKindInstanceMethod;
4590 }
4591 }
4592 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004593 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004594 }
4595 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004596
Kate Stoneb9c1b512016-09-06 20:57:50 +00004597 case clang::Type::Typedef:
4598 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4599 ->getDecl()
4600 ->getUnderlyingType()
4601 .getAsOpaquePtr(),
4602 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004603
Kate Stoneb9c1b512016-09-06 20:57:50 +00004604 case clang::Type::Auto:
4605 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4606 ->getDeducedType()
4607 .getAsOpaquePtr(),
4608 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004609
Kate Stoneb9c1b512016-09-06 20:57:50 +00004610 case clang::Type::Elaborated:
4611 return GetMemberFunctionAtIndex(
4612 llvm::cast<clang::ElaboratedType>(qual_type)
4613 ->getNamedType()
4614 .getAsOpaquePtr(),
4615 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004616
Kate Stoneb9c1b512016-09-06 20:57:50 +00004617 case clang::Type::Paren:
4618 return GetMemberFunctionAtIndex(
4619 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4620 idx);
4621
4622 default:
4623 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004624 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004625 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004626
Kate Stoneb9c1b512016-09-06 20:57:50 +00004627 if (kind == eMemberFunctionKindUnknown)
4628 return TypeMemberFunctionImpl();
4629 else
4630 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004631}
4632
Greg Claytona1e5dc82015-08-11 22:53:00 +00004633CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004634ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4635 if (type)
4636 return CompilerType(getASTContext(),
4637 GetQualType(type).getNonReferenceType());
4638 return CompilerType();
4639}
4640
4641CompilerType ClangASTContext::CreateTypedefType(
4642 const CompilerType &type, const char *typedef_name,
4643 const CompilerDeclContext &compiler_decl_ctx) {
4644 if (type && typedef_name && typedef_name[0]) {
4645 ClangASTContext *ast =
4646 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4647 if (!ast)
4648 return CompilerType();
4649 clang::ASTContext *clang_ast = ast->getASTContext();
4650 clang::QualType qual_type(ClangUtil::GetQualType(type));
4651
4652 clang::DeclContext *decl_ctx =
4653 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4654 if (decl_ctx == nullptr)
4655 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4656
4657 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4658 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4659 &clang_ast->Idents.get(typedef_name),
4660 clang_ast->getTrivialTypeSourceInfo(qual_type));
4661
4662 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4663
4664 // Get a uniqued clang::QualType for the typedef decl type
4665 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4666 }
4667 return CompilerType();
4668}
4669
4670CompilerType
4671ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4672 if (type) {
4673 clang::QualType qual_type(GetQualType(type));
4674 return CompilerType(getASTContext(),
4675 qual_type.getTypePtr()->getPointeeType());
4676 }
4677 return CompilerType();
4678}
4679
4680CompilerType
4681ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4682 if (type) {
4683 clang::QualType qual_type(GetQualType(type));
4684
4685 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4686 switch (type_class) {
4687 case clang::Type::ObjCObject:
4688 case clang::Type::ObjCInterface:
4689 return CompilerType(getASTContext(),
4690 getASTContext()->getObjCObjectPointerType(qual_type));
4691
4692 default:
4693 return CompilerType(getASTContext(),
4694 getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004695 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004696 }
4697 return CompilerType();
4698}
4699
4700CompilerType
4701ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4702 if (type)
4703 return CompilerType(this, getASTContext()
4704 ->getLValueReferenceType(GetQualType(type))
4705 .getAsOpaquePtr());
4706 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004707 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004708}
4709
Kate Stoneb9c1b512016-09-06 20:57:50 +00004710CompilerType
4711ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4712 if (type)
4713 return CompilerType(this, getASTContext()
4714 ->getRValueReferenceType(GetQualType(type))
4715 .getAsOpaquePtr());
4716 else
4717 return CompilerType();
4718}
4719
4720CompilerType
4721ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4722 if (type) {
4723 clang::QualType result(GetQualType(type));
4724 result.addConst();
4725 return CompilerType(this, result.getAsOpaquePtr());
4726 }
4727 return CompilerType();
4728}
4729
4730CompilerType
4731ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4732 if (type) {
4733 clang::QualType result(GetQualType(type));
4734 result.addVolatile();
4735 return CompilerType(this, result.getAsOpaquePtr());
4736 }
4737 return CompilerType();
4738}
4739
4740CompilerType
4741ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4742 if (type) {
4743 clang::QualType result(GetQualType(type));
4744 result.addRestrict();
4745 return CompilerType(this, result.getAsOpaquePtr());
4746 }
4747 return CompilerType();
4748}
4749
4750CompilerType
4751ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4752 const char *typedef_name,
4753 const CompilerDeclContext &compiler_decl_ctx) {
4754 if (type) {
4755 clang::ASTContext *clang_ast = getASTContext();
4756 clang::QualType qual_type(GetQualType(type));
4757
4758 clang::DeclContext *decl_ctx =
4759 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4760 if (decl_ctx == nullptr)
4761 decl_ctx = getASTContext()->getTranslationUnitDecl();
4762
4763 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4764 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4765 &clang_ast->Idents.get(typedef_name),
4766 clang_ast->getTrivialTypeSourceInfo(qual_type));
4767
4768 clang::TagDecl *tdecl = nullptr;
4769 if (!qual_type.isNull()) {
4770 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4771 tdecl = rt->getDecl();
4772 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4773 tdecl = et->getDecl();
4774 }
4775
4776 // Check whether this declaration is an anonymous struct, union, or enum,
Adrian Prantl05097242018-04-30 16:49:04 +00004777 // hidden behind a typedef. If so, we try to check whether we have a
4778 // typedef tag to attach to the original record declaration
Kate Stoneb9c1b512016-09-06 20:57:50 +00004779 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4780 tdecl->setTypedefNameForAnonDecl(decl);
4781
4782 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4783
4784 // Get a uniqued clang::QualType for the typedef decl type
4785 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4786 }
4787 return CompilerType();
4788}
4789
4790CompilerType
4791ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4792 if (type) {
4793 const clang::TypedefType *typedef_type =
4794 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4795 if (typedef_type)
4796 return CompilerType(getASTContext(),
4797 typedef_type->getDecl()->getUnderlyingType());
4798 }
4799 return CompilerType();
4800}
Greg Claytond8d4a572015-08-11 21:38:15 +00004801
4802//----------------------------------------------------------------------
4803// Create related types using the current type's AST
4804//----------------------------------------------------------------------
4805
Kate Stoneb9c1b512016-09-06 20:57:50 +00004806CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4807 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004808}
4809//----------------------------------------------------------------------
4810// Exploring the type
4811//----------------------------------------------------------------------
4812
Kate Stoneb9c1b512016-09-06 20:57:50 +00004813uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4814 ExecutionContextScope *exe_scope) {
4815 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00004816 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004817 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004818 switch (type_class) {
4819 case clang::Type::Record:
4820 if (GetCompleteType(type))
4821 return getASTContext()->getTypeSize(qual_type);
4822 else
4823 return 0;
4824 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004825
Kate Stoneb9c1b512016-09-06 20:57:50 +00004826 case clang::Type::ObjCInterface:
4827 case clang::Type::ObjCObject: {
4828 ExecutionContext exe_ctx(exe_scope);
4829 Process *process = exe_ctx.GetProcessPtr();
4830 if (process) {
4831 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4832 if (objc_runtime) {
4833 uint64_t bit_size = 0;
4834 if (objc_runtime->GetTypeBitSize(
4835 CompilerType(getASTContext(), qual_type), bit_size))
4836 return bit_size;
4837 }
4838 } else {
4839 static bool g_printed = false;
4840 if (!g_printed) {
4841 StreamString s;
4842 DumpTypeDescription(type, &s);
4843
4844 llvm::outs() << "warning: trying to determine the size of type ";
4845 llvm::outs() << s.GetString() << "\n";
4846 llvm::outs() << "without a valid ExecutionContext. this is not "
4847 "reliable. please file a bug against LLDB.\n";
4848 llvm::outs() << "backtrace:\n";
4849 llvm::sys::PrintStackTrace(llvm::outs());
4850 llvm::outs() << "\n";
4851 g_printed = true;
4852 }
4853 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004854 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004855 LLVM_FALLTHROUGH;
4856 default:
4857 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
4858 if (bit_size == 0) {
4859 if (qual_type->isIncompleteArrayType())
4860 return getASTContext()->getTypeSize(
4861 qual_type->getArrayElementTypeNoTypeQual()
4862 ->getCanonicalTypeUnqualified());
4863 }
4864 if (qual_type->isObjCObjectOrInterfaceType())
4865 return bit_size +
4866 getASTContext()->getTypeSize(
4867 getASTContext()->ObjCBuiltinClassTy);
4868 return bit_size;
4869 }
4870 }
4871 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004872}
4873
Kate Stoneb9c1b512016-09-06 20:57:50 +00004874size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
4875 if (GetCompleteType(type))
4876 return getASTContext()->getTypeAlign(GetQualType(type));
4877 return 0;
4878}
4879
4880lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
4881 uint64_t &count) {
4882 if (!type)
4883 return lldb::eEncodingInvalid;
4884
4885 count = 1;
4886 clang::QualType qual_type(GetCanonicalQualType(type));
4887
4888 switch (qual_type->getTypeClass()) {
4889 case clang::Type::UnaryTransform:
4890 break;
4891
4892 case clang::Type::FunctionNoProto:
4893 case clang::Type::FunctionProto:
4894 break;
4895
4896 case clang::Type::IncompleteArray:
4897 case clang::Type::VariableArray:
4898 break;
4899
4900 case clang::Type::ConstantArray:
4901 break;
4902
4903 case clang::Type::ExtVector:
4904 case clang::Type::Vector:
4905 // TODO: Set this to more than one???
4906 break;
4907
4908 case clang::Type::Builtin:
4909 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4910 case clang::BuiltinType::Void:
4911 break;
4912
4913 case clang::BuiltinType::Bool:
4914 case clang::BuiltinType::Char_S:
4915 case clang::BuiltinType::SChar:
4916 case clang::BuiltinType::WChar_S:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004917 case clang::BuiltinType::Short:
4918 case clang::BuiltinType::Int:
4919 case clang::BuiltinType::Long:
4920 case clang::BuiltinType::LongLong:
4921 case clang::BuiltinType::Int128:
4922 return lldb::eEncodingSint;
4923
4924 case clang::BuiltinType::Char_U:
4925 case clang::BuiltinType::UChar:
4926 case clang::BuiltinType::WChar_U:
Richard Smith51d12d82018-05-02 02:43:22 +00004927 case clang::BuiltinType::Char8:
4928 case clang::BuiltinType::Char16:
4929 case clang::BuiltinType::Char32:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004930 case clang::BuiltinType::UShort:
4931 case clang::BuiltinType::UInt:
4932 case clang::BuiltinType::ULong:
4933 case clang::BuiltinType::ULongLong:
4934 case clang::BuiltinType::UInt128:
4935 return lldb::eEncodingUint;
4936
Ilya Biryukovfc48ac62018-06-05 10:07:07 +00004937 // Fixed point types. Note that they are currently ignored.
4938 case clang::BuiltinType::ShortAccum:
4939 case clang::BuiltinType::Accum:
4940 case clang::BuiltinType::LongAccum:
4941 case clang::BuiltinType::UShortAccum:
4942 case clang::BuiltinType::UAccum:
4943 case clang::BuiltinType::ULongAccum:
4944 break;
4945
Kate Stoneb9c1b512016-09-06 20:57:50 +00004946 case clang::BuiltinType::Half:
4947 case clang::BuiltinType::Float:
Ted Woodward4355c7c2017-09-20 19:16:53 +00004948 case clang::BuiltinType::Float16:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004949 case clang::BuiltinType::Float128:
4950 case clang::BuiltinType::Double:
4951 case clang::BuiltinType::LongDouble:
4952 return lldb::eEncodingIEEE754;
4953
4954 case clang::BuiltinType::ObjCClass:
4955 case clang::BuiltinType::ObjCId:
4956 case clang::BuiltinType::ObjCSel:
4957 return lldb::eEncodingUint;
4958
4959 case clang::BuiltinType::NullPtr:
4960 return lldb::eEncodingUint;
4961
4962 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4963 case clang::BuiltinType::Kind::BoundMember:
4964 case clang::BuiltinType::Kind::BuiltinFn:
4965 case clang::BuiltinType::Kind::Dependent:
4966 case clang::BuiltinType::Kind::OCLClkEvent:
4967 case clang::BuiltinType::Kind::OCLEvent:
4968 case clang::BuiltinType::Kind::OCLImage1dRO:
4969 case clang::BuiltinType::Kind::OCLImage1dWO:
4970 case clang::BuiltinType::Kind::OCLImage1dRW:
4971 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
4972 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
4973 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
4974 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
4975 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
4976 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
4977 case clang::BuiltinType::Kind::OCLImage2dRO:
4978 case clang::BuiltinType::Kind::OCLImage2dWO:
4979 case clang::BuiltinType::Kind::OCLImage2dRW:
4980 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
4981 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
4982 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
4983 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
4984 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
4985 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
4986 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
4987 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
4988 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
4989 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
4990 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
4991 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
4992 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
4993 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
4994 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
4995 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
4996 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
4997 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
4998 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
4999 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5000 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5001 case clang::BuiltinType::Kind::OCLImage3dRO:
5002 case clang::BuiltinType::Kind::OCLImage3dWO:
5003 case clang::BuiltinType::Kind::OCLImage3dRW:
5004 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005005 case clang::BuiltinType::Kind::OCLReserveID:
5006 case clang::BuiltinType::Kind::OCLSampler:
5007 case clang::BuiltinType::Kind::OMPArraySection:
5008 case clang::BuiltinType::Kind::Overload:
5009 case clang::BuiltinType::Kind::PseudoObject:
5010 case clang::BuiltinType::Kind::UnknownAny:
5011 break;
5012 }
5013 break;
Adrian Prantl05097242018-04-30 16:49:04 +00005014 // All pointer types are represented as unsigned integer encodings. We may
5015 // nee to add a eEncodingPointer if we ever need to know the difference
Kate Stoneb9c1b512016-09-06 20:57:50 +00005016 case clang::Type::ObjCObjectPointer:
5017 case clang::Type::BlockPointer:
5018 case clang::Type::Pointer:
5019 case clang::Type::LValueReference:
5020 case clang::Type::RValueReference:
5021 case clang::Type::MemberPointer:
5022 return lldb::eEncodingUint;
5023 case clang::Type::Complex: {
5024 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5025 if (qual_type->isComplexType())
5026 encoding = lldb::eEncodingIEEE754;
5027 else {
5028 const clang::ComplexType *complex_type =
5029 qual_type->getAsComplexIntegerType();
5030 if (complex_type)
5031 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5032 .GetEncoding(count);
5033 else
5034 encoding = lldb::eEncodingSint;
5035 }
5036 count = 2;
5037 return encoding;
5038 }
5039
5040 case clang::Type::ObjCInterface:
5041 break;
5042 case clang::Type::Record:
5043 break;
5044 case clang::Type::Enum:
5045 return lldb::eEncodingSint;
5046 case clang::Type::Typedef:
5047 return CompilerType(getASTContext(),
5048 llvm::cast<clang::TypedefType>(qual_type)
5049 ->getDecl()
5050 ->getUnderlyingType())
5051 .GetEncoding(count);
5052
5053 case clang::Type::Auto:
5054 return CompilerType(
5055 getASTContext(),
5056 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5057 .GetEncoding(count);
5058
5059 case clang::Type::Elaborated:
5060 return CompilerType(
5061 getASTContext(),
5062 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5063 .GetEncoding(count);
5064
5065 case clang::Type::Paren:
5066 return CompilerType(getASTContext(),
5067 llvm::cast<clang::ParenType>(qual_type)->desugar())
5068 .GetEncoding(count);
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005069 case clang::Type::TypeOfExpr:
5070 return CompilerType(getASTContext(),
5071 llvm::cast<clang::TypeOfExprType>(qual_type)
5072 ->getUnderlyingExpr()
5073 ->getType())
5074 .GetEncoding(count);
5075 case clang::Type::TypeOf:
5076 return CompilerType(
5077 getASTContext(),
5078 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5079 .GetEncoding(count);
5080 case clang::Type::Decltype:
5081 return CompilerType(
5082 getASTContext(),
5083 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5084 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005085 case clang::Type::DependentSizedArray:
5086 case clang::Type::DependentSizedExtVector:
5087 case clang::Type::UnresolvedUsing:
5088 case clang::Type::Attributed:
5089 case clang::Type::TemplateTypeParm:
5090 case clang::Type::SubstTemplateTypeParm:
5091 case clang::Type::SubstTemplateTypeParmPack:
5092 case clang::Type::InjectedClassName:
5093 case clang::Type::DependentName:
5094 case clang::Type::DependentTemplateSpecialization:
5095 case clang::Type::PackExpansion:
5096 case clang::Type::ObjCObject:
5097
Kate Stoneb9c1b512016-09-06 20:57:50 +00005098 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005099 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005100 case clang::Type::Atomic:
5101 case clang::Type::Adjusted:
5102 case clang::Type::Pipe:
5103 break;
5104
5105 // pointer type decayed from an array or function type.
5106 case clang::Type::Decayed:
5107 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005108 case clang::Type::ObjCTypeParam:
5109 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005110
5111 case clang::Type::DependentAddressSpace:
5112 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005113 }
5114 count = 0;
5115 return lldb::eEncodingInvalid;
5116}
5117
5118lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5119 if (!type)
5120 return lldb::eFormatDefault;
5121
5122 clang::QualType qual_type(GetCanonicalQualType(type));
5123
5124 switch (qual_type->getTypeClass()) {
5125 case clang::Type::UnaryTransform:
5126 break;
5127
5128 case clang::Type::FunctionNoProto:
5129 case clang::Type::FunctionProto:
5130 break;
5131
5132 case clang::Type::IncompleteArray:
5133 case clang::Type::VariableArray:
5134 break;
5135
5136 case clang::Type::ConstantArray:
5137 return lldb::eFormatVoid; // no value
5138
5139 case clang::Type::ExtVector:
5140 case clang::Type::Vector:
5141 break;
5142
5143 case clang::Type::Builtin:
5144 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5145 // default: assert(0 && "Unknown builtin type!");
5146 case clang::BuiltinType::UnknownAny:
5147 case clang::BuiltinType::Void:
5148 case clang::BuiltinType::BoundMember:
5149 break;
5150
5151 case clang::BuiltinType::Bool:
5152 return lldb::eFormatBoolean;
5153 case clang::BuiltinType::Char_S:
5154 case clang::BuiltinType::SChar:
5155 case clang::BuiltinType::WChar_S:
5156 case clang::BuiltinType::Char_U:
5157 case clang::BuiltinType::UChar:
5158 case clang::BuiltinType::WChar_U:
5159 return lldb::eFormatChar;
5160 case clang::BuiltinType::Char16:
5161 return lldb::eFormatUnicode16;
5162 case clang::BuiltinType::Char32:
5163 return lldb::eFormatUnicode32;
5164 case clang::BuiltinType::UShort:
5165 return lldb::eFormatUnsigned;
5166 case clang::BuiltinType::Short:
5167 return lldb::eFormatDecimal;
5168 case clang::BuiltinType::UInt:
5169 return lldb::eFormatUnsigned;
5170 case clang::BuiltinType::Int:
5171 return lldb::eFormatDecimal;
5172 case clang::BuiltinType::ULong:
5173 return lldb::eFormatUnsigned;
5174 case clang::BuiltinType::Long:
5175 return lldb::eFormatDecimal;
5176 case clang::BuiltinType::ULongLong:
5177 return lldb::eFormatUnsigned;
5178 case clang::BuiltinType::LongLong:
5179 return lldb::eFormatDecimal;
5180 case clang::BuiltinType::UInt128:
5181 return lldb::eFormatUnsigned;
5182 case clang::BuiltinType::Int128:
5183 return lldb::eFormatDecimal;
5184 case clang::BuiltinType::Half:
5185 case clang::BuiltinType::Float:
5186 case clang::BuiltinType::Double:
5187 case clang::BuiltinType::LongDouble:
5188 return lldb::eFormatFloat;
5189 default:
5190 return lldb::eFormatHex;
5191 }
5192 break;
5193 case clang::Type::ObjCObjectPointer:
5194 return lldb::eFormatHex;
5195 case clang::Type::BlockPointer:
5196 return lldb::eFormatHex;
5197 case clang::Type::Pointer:
5198 return lldb::eFormatHex;
5199 case clang::Type::LValueReference:
5200 case clang::Type::RValueReference:
5201 return lldb::eFormatHex;
5202 case clang::Type::MemberPointer:
5203 break;
5204 case clang::Type::Complex: {
5205 if (qual_type->isComplexType())
5206 return lldb::eFormatComplex;
5207 else
5208 return lldb::eFormatComplexInteger;
5209 }
5210 case clang::Type::ObjCInterface:
5211 break;
5212 case clang::Type::Record:
5213 break;
5214 case clang::Type::Enum:
5215 return lldb::eFormatEnum;
5216 case clang::Type::Typedef:
5217 return CompilerType(getASTContext(),
5218 llvm::cast<clang::TypedefType>(qual_type)
5219 ->getDecl()
5220 ->getUnderlyingType())
5221 .GetFormat();
5222 case clang::Type::Auto:
5223 return CompilerType(getASTContext(),
5224 llvm::cast<clang::AutoType>(qual_type)->desugar())
5225 .GetFormat();
5226 case clang::Type::Paren:
5227 return CompilerType(getASTContext(),
5228 llvm::cast<clang::ParenType>(qual_type)->desugar())
5229 .GetFormat();
5230 case clang::Type::Elaborated:
5231 return CompilerType(
5232 getASTContext(),
5233 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5234 .GetFormat();
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005235 case clang::Type::TypeOfExpr:
5236 return CompilerType(getASTContext(),
5237 llvm::cast<clang::TypeOfExprType>(qual_type)
5238 ->getUnderlyingExpr()
5239 ->getType())
5240 .GetFormat();
5241 case clang::Type::TypeOf:
5242 return CompilerType(
5243 getASTContext(),
5244 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5245 .GetFormat();
5246 case clang::Type::Decltype:
5247 return CompilerType(
5248 getASTContext(),
5249 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5250 .GetFormat();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005251 case clang::Type::DependentSizedArray:
5252 case clang::Type::DependentSizedExtVector:
5253 case clang::Type::UnresolvedUsing:
5254 case clang::Type::Attributed:
5255 case clang::Type::TemplateTypeParm:
5256 case clang::Type::SubstTemplateTypeParm:
5257 case clang::Type::SubstTemplateTypeParmPack:
5258 case clang::Type::InjectedClassName:
5259 case clang::Type::DependentName:
5260 case clang::Type::DependentTemplateSpecialization:
5261 case clang::Type::PackExpansion:
5262 case clang::Type::ObjCObject:
5263
Kate Stoneb9c1b512016-09-06 20:57:50 +00005264 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005265 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005266 case clang::Type::Atomic:
5267 case clang::Type::Adjusted:
5268 case clang::Type::Pipe:
5269 break;
5270
5271 // pointer type decayed from an array or function type.
5272 case clang::Type::Decayed:
5273 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005274 case clang::Type::ObjCTypeParam:
5275 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005276
5277 case clang::Type::DependentAddressSpace:
5278 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005279 }
5280 // We don't know hot to display this type...
5281 return lldb::eFormatBytes;
5282}
5283
5284static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5285 bool check_superclass) {
5286 while (class_interface_decl) {
5287 if (class_interface_decl->ivar_size() > 0)
5288 return true;
5289
5290 if (check_superclass)
5291 class_interface_decl = class_interface_decl->getSuperClass();
5292 else
5293 break;
5294 }
5295 return false;
5296}
5297
5298uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
5299 bool omit_empty_base_classes) {
5300 if (!type)
5301 return 0;
5302
5303 uint32_t num_children = 0;
5304 clang::QualType qual_type(GetQualType(type));
5305 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5306 switch (type_class) {
5307 case clang::Type::Builtin:
5308 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5309 case clang::BuiltinType::ObjCId: // child is Class
5310 case clang::BuiltinType::ObjCClass: // child is Class
5311 num_children = 1;
5312 break;
5313
5314 default:
5315 break;
5316 }
5317 break;
5318
5319 case clang::Type::Complex:
5320 return 0;
5321
5322 case clang::Type::Record:
5323 if (GetCompleteQualType(getASTContext(), qual_type)) {
5324 const clang::RecordType *record_type =
5325 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5326 const clang::RecordDecl *record_decl = record_type->getDecl();
5327 assert(record_decl);
5328 const clang::CXXRecordDecl *cxx_record_decl =
5329 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5330 if (cxx_record_decl) {
5331 if (omit_empty_base_classes) {
Adrian Prantl05097242018-04-30 16:49:04 +00005332 // Check each base classes to see if it or any of its base classes
5333 // contain any fields. This can help limit the noise in variable
5334 // views by not having to show base classes that contain no members.
Kate Stoneb9c1b512016-09-06 20:57:50 +00005335 clang::CXXRecordDecl::base_class_const_iterator base_class,
5336 base_class_end;
5337 for (base_class = cxx_record_decl->bases_begin(),
5338 base_class_end = cxx_record_decl->bases_end();
5339 base_class != base_class_end; ++base_class) {
5340 const clang::CXXRecordDecl *base_class_decl =
5341 llvm::cast<clang::CXXRecordDecl>(
5342 base_class->getType()
5343 ->getAs<clang::RecordType>()
5344 ->getDecl());
5345
5346 // Skip empty base classes
5347 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5348 continue;
5349
5350 num_children++;
5351 }
5352 } else {
5353 // Include all base classes
5354 num_children += cxx_record_decl->getNumBases();
5355 }
5356 }
5357 clang::RecordDecl::field_iterator field, field_end;
5358 for (field = record_decl->field_begin(),
5359 field_end = record_decl->field_end();
5360 field != field_end; ++field)
5361 ++num_children;
5362 }
5363 break;
5364
5365 case clang::Type::ObjCObject:
5366 case clang::Type::ObjCInterface:
5367 if (GetCompleteQualType(getASTContext(), qual_type)) {
5368 const clang::ObjCObjectType *objc_class_type =
5369 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5370 assert(objc_class_type);
5371 if (objc_class_type) {
5372 clang::ObjCInterfaceDecl *class_interface_decl =
5373 objc_class_type->getInterface();
5374
5375 if (class_interface_decl) {
5376
5377 clang::ObjCInterfaceDecl *superclass_interface_decl =
5378 class_interface_decl->getSuperClass();
5379 if (superclass_interface_decl) {
5380 if (omit_empty_base_classes) {
5381 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5382 ++num_children;
5383 } else
5384 ++num_children;
5385 }
5386
5387 num_children += class_interface_decl->ivar_size();
5388 }
5389 }
5390 }
5391 break;
5392
5393 case clang::Type::ObjCObjectPointer: {
5394 const clang::ObjCObjectPointerType *pointer_type =
5395 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5396 clang::QualType pointee_type = pointer_type->getPointeeType();
5397 uint32_t num_pointee_children =
5398 CompilerType(getASTContext(), pointee_type)
5399 .GetNumChildren(omit_empty_base_classes);
5400 // If this type points to a simple type, then it has 1 child
5401 if (num_pointee_children == 0)
5402 num_children = 1;
5403 else
5404 num_children = num_pointee_children;
5405 } break;
5406
5407 case clang::Type::Vector:
5408 case clang::Type::ExtVector:
5409 num_children =
5410 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5411 break;
5412
5413 case clang::Type::ConstantArray:
5414 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5415 ->getSize()
5416 .getLimitedValue();
5417 break;
5418
5419 case clang::Type::Pointer: {
5420 const clang::PointerType *pointer_type =
5421 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5422 clang::QualType pointee_type(pointer_type->getPointeeType());
5423 uint32_t num_pointee_children =
5424 CompilerType(getASTContext(), pointee_type)
5425 .GetNumChildren(omit_empty_base_classes);
5426 if (num_pointee_children == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00005427 // We have a pointer to a pointee type that claims it has no children. We
5428 // will want to look at
Kate Stoneb9c1b512016-09-06 20:57:50 +00005429 num_children = GetNumPointeeChildren(pointee_type);
5430 } else
5431 num_children = num_pointee_children;
5432 } break;
5433
5434 case clang::Type::LValueReference:
5435 case clang::Type::RValueReference: {
5436 const clang::ReferenceType *reference_type =
5437 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5438 clang::QualType pointee_type = reference_type->getPointeeType();
5439 uint32_t num_pointee_children =
5440 CompilerType(getASTContext(), pointee_type)
5441 .GetNumChildren(omit_empty_base_classes);
5442 // If this type points to a simple type, then it has 1 child
5443 if (num_pointee_children == 0)
5444 num_children = 1;
5445 else
5446 num_children = num_pointee_children;
5447 } break;
5448
5449 case clang::Type::Typedef:
5450 num_children =
5451 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5452 ->getDecl()
5453 ->getUnderlyingType())
5454 .GetNumChildren(omit_empty_base_classes);
5455 break;
5456
5457 case clang::Type::Auto:
5458 num_children =
5459 CompilerType(getASTContext(),
5460 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5461 .GetNumChildren(omit_empty_base_classes);
5462 break;
5463
5464 case clang::Type::Elaborated:
5465 num_children =
5466 CompilerType(
5467 getASTContext(),
5468 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5469 .GetNumChildren(omit_empty_base_classes);
5470 break;
5471
5472 case clang::Type::Paren:
5473 num_children =
5474 CompilerType(getASTContext(),
5475 llvm::cast<clang::ParenType>(qual_type)->desugar())
5476 .GetNumChildren(omit_empty_base_classes);
5477 break;
5478 default:
5479 break;
5480 }
5481 return num_children;
5482}
5483
5484CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5485 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005486}
5487
Greg Claytond8d4a572015-08-11 21:38:15 +00005488lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005489ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5490 if (type) {
5491 clang::QualType qual_type(GetQualType(type));
5492 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5493 if (type_class == clang::Type::Builtin) {
5494 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5495 case clang::BuiltinType::Void:
5496 return eBasicTypeVoid;
5497 case clang::BuiltinType::Bool:
5498 return eBasicTypeBool;
5499 case clang::BuiltinType::Char_S:
5500 return eBasicTypeSignedChar;
5501 case clang::BuiltinType::Char_U:
5502 return eBasicTypeUnsignedChar;
5503 case clang::BuiltinType::Char16:
5504 return eBasicTypeChar16;
5505 case clang::BuiltinType::Char32:
5506 return eBasicTypeChar32;
5507 case clang::BuiltinType::UChar:
5508 return eBasicTypeUnsignedChar;
5509 case clang::BuiltinType::SChar:
5510 return eBasicTypeSignedChar;
5511 case clang::BuiltinType::WChar_S:
5512 return eBasicTypeSignedWChar;
5513 case clang::BuiltinType::WChar_U:
5514 return eBasicTypeUnsignedWChar;
5515 case clang::BuiltinType::Short:
5516 return eBasicTypeShort;
5517 case clang::BuiltinType::UShort:
5518 return eBasicTypeUnsignedShort;
5519 case clang::BuiltinType::Int:
5520 return eBasicTypeInt;
5521 case clang::BuiltinType::UInt:
5522 return eBasicTypeUnsignedInt;
5523 case clang::BuiltinType::Long:
5524 return eBasicTypeLong;
5525 case clang::BuiltinType::ULong:
5526 return eBasicTypeUnsignedLong;
5527 case clang::BuiltinType::LongLong:
5528 return eBasicTypeLongLong;
5529 case clang::BuiltinType::ULongLong:
5530 return eBasicTypeUnsignedLongLong;
5531 case clang::BuiltinType::Int128:
5532 return eBasicTypeInt128;
5533 case clang::BuiltinType::UInt128:
5534 return eBasicTypeUnsignedInt128;
5535
5536 case clang::BuiltinType::Half:
5537 return eBasicTypeHalf;
5538 case clang::BuiltinType::Float:
5539 return eBasicTypeFloat;
5540 case clang::BuiltinType::Double:
5541 return eBasicTypeDouble;
5542 case clang::BuiltinType::LongDouble:
5543 return eBasicTypeLongDouble;
5544
5545 case clang::BuiltinType::NullPtr:
5546 return eBasicTypeNullPtr;
5547 case clang::BuiltinType::ObjCId:
5548 return eBasicTypeObjCID;
5549 case clang::BuiltinType::ObjCClass:
5550 return eBasicTypeObjCClass;
5551 case clang::BuiltinType::ObjCSel:
5552 return eBasicTypeObjCSel;
5553 default:
5554 return eBasicTypeOther;
5555 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005556 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005557 }
5558 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005559}
5560
Kate Stoneb9c1b512016-09-06 20:57:50 +00005561void ClangASTContext::ForEachEnumerator(
5562 lldb::opaque_compiler_type_t type,
5563 std::function<bool(const CompilerType &integer_type,
5564 const ConstString &name,
5565 const llvm::APSInt &value)> const &callback) {
5566 const clang::EnumType *enum_type =
5567 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5568 if (enum_type) {
5569 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5570 if (enum_decl) {
5571 CompilerType integer_type(this,
5572 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005573
Kate Stoneb9c1b512016-09-06 20:57:50 +00005574 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5575 for (enum_pos = enum_decl->enumerator_begin(),
5576 enum_end_pos = enum_decl->enumerator_end();
5577 enum_pos != enum_end_pos; ++enum_pos) {
5578 ConstString name(enum_pos->getNameAsString().c_str());
5579 if (!callback(integer_type, name, enum_pos->getInitVal()))
5580 break;
5581 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005582 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005583 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005584}
5585
Greg Claytond8d4a572015-08-11 21:38:15 +00005586#pragma mark Aggregate Types
5587
Kate Stoneb9c1b512016-09-06 20:57:50 +00005588uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5589 if (!type)
5590 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005591
Kate Stoneb9c1b512016-09-06 20:57:50 +00005592 uint32_t count = 0;
5593 clang::QualType qual_type(GetCanonicalQualType(type));
5594 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5595 switch (type_class) {
5596 case clang::Type::Record:
5597 if (GetCompleteType(type)) {
5598 const clang::RecordType *record_type =
5599 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5600 if (record_type) {
5601 clang::RecordDecl *record_decl = record_type->getDecl();
5602 if (record_decl) {
5603 uint32_t field_idx = 0;
5604 clang::RecordDecl::field_iterator field, field_end;
5605 for (field = record_decl->field_begin(),
5606 field_end = record_decl->field_end();
5607 field != field_end; ++field)
5608 ++field_idx;
5609 count = field_idx;
5610 }
5611 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005612 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005613 break;
5614
5615 case clang::Type::Typedef:
5616 count =
5617 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5618 ->getDecl()
5619 ->getUnderlyingType())
5620 .GetNumFields();
5621 break;
5622
5623 case clang::Type::Auto:
5624 count =
5625 CompilerType(getASTContext(),
5626 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5627 .GetNumFields();
5628 break;
5629
5630 case clang::Type::Elaborated:
5631 count = CompilerType(
5632 getASTContext(),
5633 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5634 .GetNumFields();
5635 break;
5636
5637 case clang::Type::Paren:
5638 count = CompilerType(getASTContext(),
5639 llvm::cast<clang::ParenType>(qual_type)->desugar())
5640 .GetNumFields();
5641 break;
5642
Sean Callananf9c622a2016-09-30 18:44:43 +00005643 case clang::Type::ObjCObjectPointer: {
5644 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005645 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005646 const clang::ObjCInterfaceType *objc_interface_type =
5647 objc_class_type->getInterfaceType();
5648 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005649 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5650 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005651 clang::ObjCInterfaceDecl *class_interface_decl =
5652 objc_interface_type->getDecl();
5653 if (class_interface_decl) {
5654 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005655 }
5656 }
5657 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005658 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005659
5660 case clang::Type::ObjCObject:
5661 case clang::Type::ObjCInterface:
5662 if (GetCompleteType(type)) {
5663 const clang::ObjCObjectType *objc_class_type =
5664 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5665 if (objc_class_type) {
5666 clang::ObjCInterfaceDecl *class_interface_decl =
5667 objc_class_type->getInterface();
5668
5669 if (class_interface_decl)
5670 count = class_interface_decl->ivar_size();
5671 }
5672 }
5673 break;
5674
5675 default:
5676 break;
5677 }
5678 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005679}
5680
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005681static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005682GetObjCFieldAtIndex(clang::ASTContext *ast,
5683 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5684 std::string &name, uint64_t *bit_offset_ptr,
5685 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5686 if (class_interface_decl) {
5687 if (idx < (class_interface_decl->ivar_size())) {
5688 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5689 ivar_end = class_interface_decl->ivar_end();
5690 uint32_t ivar_idx = 0;
5691
5692 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5693 ++ivar_pos, ++ivar_idx) {
5694 if (ivar_idx == idx) {
5695 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5696
5697 clang::QualType ivar_qual_type(ivar_decl->getType());
5698
5699 name.assign(ivar_decl->getNameAsString());
5700
5701 if (bit_offset_ptr) {
5702 const clang::ASTRecordLayout &interface_layout =
5703 ast->getASTObjCInterfaceLayout(class_interface_decl);
5704 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5705 }
5706
5707 const bool is_bitfield = ivar_pos->isBitField();
5708
5709 if (bitfield_bit_size_ptr) {
5710 *bitfield_bit_size_ptr = 0;
5711
5712 if (is_bitfield && ast) {
5713 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5714 llvm::APSInt bitfield_apsint;
5715 if (bitfield_bit_size_expr &&
5716 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5717 *ast)) {
5718 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5719 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005720 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005721 }
5722 if (is_bitfield_ptr)
5723 *is_bitfield_ptr = is_bitfield;
5724
5725 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005726 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005727 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005728 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005729 }
5730 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005731}
5732
Kate Stoneb9c1b512016-09-06 20:57:50 +00005733CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5734 size_t idx, std::string &name,
5735 uint64_t *bit_offset_ptr,
5736 uint32_t *bitfield_bit_size_ptr,
5737 bool *is_bitfield_ptr) {
5738 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005739 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005740
5741 clang::QualType qual_type(GetCanonicalQualType(type));
5742 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5743 switch (type_class) {
5744 case clang::Type::Record:
5745 if (GetCompleteType(type)) {
5746 const clang::RecordType *record_type =
5747 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5748 const clang::RecordDecl *record_decl = record_type->getDecl();
5749 uint32_t field_idx = 0;
5750 clang::RecordDecl::field_iterator field, field_end;
5751 for (field = record_decl->field_begin(),
5752 field_end = record_decl->field_end();
5753 field != field_end; ++field, ++field_idx) {
5754 if (idx == field_idx) {
5755 // Print the member type if requested
5756 // Print the member name and equal sign
5757 name.assign(field->getNameAsString());
5758
5759 // Figure out the type byte size (field_type_info.first) and
5760 // alignment (field_type_info.second) from the AST context.
5761 if (bit_offset_ptr) {
5762 const clang::ASTRecordLayout &record_layout =
5763 getASTContext()->getASTRecordLayout(record_decl);
5764 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5765 }
5766
5767 const bool is_bitfield = field->isBitField();
5768
5769 if (bitfield_bit_size_ptr) {
5770 *bitfield_bit_size_ptr = 0;
5771
5772 if (is_bitfield) {
5773 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5774 llvm::APSInt bitfield_apsint;
5775 if (bitfield_bit_size_expr &&
5776 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5777 *getASTContext())) {
5778 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5779 }
5780 }
5781 }
5782 if (is_bitfield_ptr)
5783 *is_bitfield_ptr = is_bitfield;
5784
5785 return CompilerType(getASTContext(), field->getType());
5786 }
5787 }
5788 }
5789 break;
5790
Sean Callananf9c622a2016-09-30 18:44:43 +00005791 case clang::Type::ObjCObjectPointer: {
5792 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005793 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005794 const clang::ObjCInterfaceType *objc_interface_type =
5795 objc_class_type->getInterfaceType();
5796 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005797 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5798 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005799 clang::ObjCInterfaceDecl *class_interface_decl =
5800 objc_interface_type->getDecl();
5801 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005802 return CompilerType(
5803 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5804 idx, name, bit_offset_ptr,
5805 bitfield_bit_size_ptr, is_bitfield_ptr));
5806 }
5807 }
5808 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005809 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005810
5811 case clang::Type::ObjCObject:
5812 case clang::Type::ObjCInterface:
5813 if (GetCompleteType(type)) {
5814 const clang::ObjCObjectType *objc_class_type =
5815 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5816 assert(objc_class_type);
5817 if (objc_class_type) {
5818 clang::ObjCInterfaceDecl *class_interface_decl =
5819 objc_class_type->getInterface();
5820 return CompilerType(
5821 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5822 idx, name, bit_offset_ptr,
5823 bitfield_bit_size_ptr, is_bitfield_ptr));
5824 }
5825 }
5826 break;
5827
5828 case clang::Type::Typedef:
5829 return CompilerType(getASTContext(),
5830 llvm::cast<clang::TypedefType>(qual_type)
5831 ->getDecl()
5832 ->getUnderlyingType())
5833 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5834 is_bitfield_ptr);
5835
5836 case clang::Type::Auto:
5837 return CompilerType(
5838 getASTContext(),
5839 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5840 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5841 is_bitfield_ptr);
5842
5843 case clang::Type::Elaborated:
5844 return CompilerType(
5845 getASTContext(),
5846 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5847 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5848 is_bitfield_ptr);
5849
5850 case clang::Type::Paren:
5851 return CompilerType(getASTContext(),
5852 llvm::cast<clang::ParenType>(qual_type)->desugar())
5853 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5854 is_bitfield_ptr);
5855
5856 default:
5857 break;
5858 }
5859 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005860}
5861
Greg Clayton99558cc42015-08-24 23:46:31 +00005862uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005863ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
5864 uint32_t count = 0;
5865 clang::QualType qual_type(GetCanonicalQualType(type));
5866 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5867 switch (type_class) {
5868 case clang::Type::Record:
5869 if (GetCompleteType(type)) {
5870 const clang::CXXRecordDecl *cxx_record_decl =
5871 qual_type->getAsCXXRecordDecl();
5872 if (cxx_record_decl)
5873 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00005874 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005875 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00005876
Kate Stoneb9c1b512016-09-06 20:57:50 +00005877 case clang::Type::ObjCObjectPointer:
5878 count = GetPointeeType(type).GetNumDirectBaseClasses();
5879 break;
5880
5881 case clang::Type::ObjCObject:
5882 if (GetCompleteType(type)) {
5883 const clang::ObjCObjectType *objc_class_type =
5884 qual_type->getAsObjCQualifiedInterfaceType();
5885 if (objc_class_type) {
5886 clang::ObjCInterfaceDecl *class_interface_decl =
5887 objc_class_type->getInterface();
5888
5889 if (class_interface_decl && class_interface_decl->getSuperClass())
5890 count = 1;
5891 }
5892 }
5893 break;
5894 case clang::Type::ObjCInterface:
5895 if (GetCompleteType(type)) {
5896 const clang::ObjCInterfaceType *objc_interface_type =
5897 qual_type->getAs<clang::ObjCInterfaceType>();
5898 if (objc_interface_type) {
5899 clang::ObjCInterfaceDecl *class_interface_decl =
5900 objc_interface_type->getInterface();
5901
5902 if (class_interface_decl && class_interface_decl->getSuperClass())
5903 count = 1;
5904 }
5905 }
5906 break;
5907
5908 case clang::Type::Typedef:
5909 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5910 ->getDecl()
5911 ->getUnderlyingType()
5912 .getAsOpaquePtr());
5913 break;
5914
5915 case clang::Type::Auto:
5916 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5917 ->getDeducedType()
5918 .getAsOpaquePtr());
5919 break;
5920
5921 case clang::Type::Elaborated:
5922 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5923 ->getNamedType()
5924 .getAsOpaquePtr());
5925 break;
5926
5927 case clang::Type::Paren:
5928 return GetNumDirectBaseClasses(
5929 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5930
5931 default:
5932 break;
5933 }
5934 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00005935}
5936
5937uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005938ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
5939 uint32_t count = 0;
5940 clang::QualType qual_type(GetCanonicalQualType(type));
5941 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5942 switch (type_class) {
5943 case clang::Type::Record:
5944 if (GetCompleteType(type)) {
5945 const clang::CXXRecordDecl *cxx_record_decl =
5946 qual_type->getAsCXXRecordDecl();
5947 if (cxx_record_decl)
5948 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00005949 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005950 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00005951
Kate Stoneb9c1b512016-09-06 20:57:50 +00005952 case clang::Type::Typedef:
5953 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5954 ->getDecl()
5955 ->getUnderlyingType()
5956 .getAsOpaquePtr());
5957 break;
5958
5959 case clang::Type::Auto:
5960 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5961 ->getDeducedType()
5962 .getAsOpaquePtr());
5963 break;
5964
5965 case clang::Type::Elaborated:
5966 count =
5967 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5968 ->getNamedType()
5969 .getAsOpaquePtr());
5970 break;
5971
5972 case clang::Type::Paren:
5973 count = GetNumVirtualBaseClasses(
5974 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5975 break;
5976
5977 default:
5978 break;
5979 }
5980 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00005981}
5982
Kate Stoneb9c1b512016-09-06 20:57:50 +00005983CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
5984 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
5985 clang::QualType qual_type(GetCanonicalQualType(type));
5986 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5987 switch (type_class) {
5988 case clang::Type::Record:
5989 if (GetCompleteType(type)) {
5990 const clang::CXXRecordDecl *cxx_record_decl =
5991 qual_type->getAsCXXRecordDecl();
5992 if (cxx_record_decl) {
5993 uint32_t curr_idx = 0;
5994 clang::CXXRecordDecl::base_class_const_iterator base_class,
5995 base_class_end;
5996 for (base_class = cxx_record_decl->bases_begin(),
5997 base_class_end = cxx_record_decl->bases_end();
5998 base_class != base_class_end; ++base_class, ++curr_idx) {
5999 if (curr_idx == idx) {
6000 if (bit_offset_ptr) {
6001 const clang::ASTRecordLayout &record_layout =
6002 getASTContext()->getASTRecordLayout(cxx_record_decl);
6003 const clang::CXXRecordDecl *base_class_decl =
6004 llvm::cast<clang::CXXRecordDecl>(
6005 base_class->getType()
6006 ->getAs<clang::RecordType>()
6007 ->getDecl());
6008 if (base_class->isVirtual())
6009 *bit_offset_ptr =
6010 record_layout.getVBaseClassOffset(base_class_decl)
6011 .getQuantity() *
6012 8;
6013 else
6014 *bit_offset_ptr =
6015 record_layout.getBaseClassOffset(base_class_decl)
6016 .getQuantity() *
6017 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006018 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006019 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6020 }
6021 }
6022 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006023 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006024 break;
6025
6026 case clang::Type::ObjCObjectPointer:
6027 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6028
6029 case clang::Type::ObjCObject:
6030 if (idx == 0 && GetCompleteType(type)) {
6031 const clang::ObjCObjectType *objc_class_type =
6032 qual_type->getAsObjCQualifiedInterfaceType();
6033 if (objc_class_type) {
6034 clang::ObjCInterfaceDecl *class_interface_decl =
6035 objc_class_type->getInterface();
6036
6037 if (class_interface_decl) {
6038 clang::ObjCInterfaceDecl *superclass_interface_decl =
6039 class_interface_decl->getSuperClass();
6040 if (superclass_interface_decl) {
6041 if (bit_offset_ptr)
6042 *bit_offset_ptr = 0;
6043 return CompilerType(getASTContext(),
6044 getASTContext()->getObjCInterfaceType(
6045 superclass_interface_decl));
6046 }
6047 }
6048 }
6049 }
6050 break;
6051 case clang::Type::ObjCInterface:
6052 if (idx == 0 && GetCompleteType(type)) {
6053 const clang::ObjCObjectType *objc_interface_type =
6054 qual_type->getAs<clang::ObjCInterfaceType>();
6055 if (objc_interface_type) {
6056 clang::ObjCInterfaceDecl *class_interface_decl =
6057 objc_interface_type->getInterface();
6058
6059 if (class_interface_decl) {
6060 clang::ObjCInterfaceDecl *superclass_interface_decl =
6061 class_interface_decl->getSuperClass();
6062 if (superclass_interface_decl) {
6063 if (bit_offset_ptr)
6064 *bit_offset_ptr = 0;
6065 return CompilerType(getASTContext(),
6066 getASTContext()->getObjCInterfaceType(
6067 superclass_interface_decl));
6068 }
6069 }
6070 }
6071 }
6072 break;
6073
6074 case clang::Type::Typedef:
6075 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6076 ->getDecl()
6077 ->getUnderlyingType()
6078 .getAsOpaquePtr(),
6079 idx, bit_offset_ptr);
6080
6081 case clang::Type::Auto:
6082 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6083 ->getDeducedType()
6084 .getAsOpaquePtr(),
6085 idx, bit_offset_ptr);
6086
6087 case clang::Type::Elaborated:
6088 return GetDirectBaseClassAtIndex(
6089 llvm::cast<clang::ElaboratedType>(qual_type)
6090 ->getNamedType()
6091 .getAsOpaquePtr(),
6092 idx, bit_offset_ptr);
6093
6094 case clang::Type::Paren:
6095 return GetDirectBaseClassAtIndex(
6096 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6097 idx, bit_offset_ptr);
6098
6099 default:
6100 break;
6101 }
6102 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006103}
6104
Kate Stoneb9c1b512016-09-06 20:57:50 +00006105CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6106 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6107 clang::QualType qual_type(GetCanonicalQualType(type));
6108 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6109 switch (type_class) {
6110 case clang::Type::Record:
6111 if (GetCompleteType(type)) {
6112 const clang::CXXRecordDecl *cxx_record_decl =
6113 qual_type->getAsCXXRecordDecl();
6114 if (cxx_record_decl) {
6115 uint32_t curr_idx = 0;
6116 clang::CXXRecordDecl::base_class_const_iterator base_class,
6117 base_class_end;
6118 for (base_class = cxx_record_decl->vbases_begin(),
6119 base_class_end = cxx_record_decl->vbases_end();
6120 base_class != base_class_end; ++base_class, ++curr_idx) {
6121 if (curr_idx == idx) {
6122 if (bit_offset_ptr) {
6123 const clang::ASTRecordLayout &record_layout =
6124 getASTContext()->getASTRecordLayout(cxx_record_decl);
6125 const clang::CXXRecordDecl *base_class_decl =
6126 llvm::cast<clang::CXXRecordDecl>(
6127 base_class->getType()
6128 ->getAs<clang::RecordType>()
6129 ->getDecl());
6130 *bit_offset_ptr =
6131 record_layout.getVBaseClassOffset(base_class_decl)
6132 .getQuantity() *
6133 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006134 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006135 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6136 }
6137 }
6138 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006139 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006140 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006141
Kate Stoneb9c1b512016-09-06 20:57:50 +00006142 case clang::Type::Typedef:
6143 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6144 ->getDecl()
6145 ->getUnderlyingType()
6146 .getAsOpaquePtr(),
6147 idx, bit_offset_ptr);
6148
6149 case clang::Type::Auto:
6150 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6151 ->getDeducedType()
6152 .getAsOpaquePtr(),
6153 idx, bit_offset_ptr);
6154
6155 case clang::Type::Elaborated:
6156 return GetVirtualBaseClassAtIndex(
6157 llvm::cast<clang::ElaboratedType>(qual_type)
6158 ->getNamedType()
6159 .getAsOpaquePtr(),
6160 idx, bit_offset_ptr);
6161
6162 case clang::Type::Paren:
6163 return GetVirtualBaseClassAtIndex(
6164 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6165 idx, bit_offset_ptr);
6166
6167 default:
6168 break;
6169 }
6170 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006171}
6172
Greg Claytond8d4a572015-08-11 21:38:15 +00006173// If a pointer to a pointee type (the clang_type arg) says that it has no
6174// children, then we either need to trust it, or override it and return a
6175// different result. For example, an "int *" has one child that is an integer,
6176// but a function pointer doesn't have any children. Likewise if a Record type
6177// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006178uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6179 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006180 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006181
6182 clang::QualType qual_type(type.getCanonicalType());
6183 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6184 switch (type_class) {
6185 case clang::Type::Builtin:
6186 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6187 case clang::BuiltinType::UnknownAny:
6188 case clang::BuiltinType::Void:
6189 case clang::BuiltinType::NullPtr:
6190 case clang::BuiltinType::OCLEvent:
6191 case clang::BuiltinType::OCLImage1dRO:
6192 case clang::BuiltinType::OCLImage1dWO:
6193 case clang::BuiltinType::OCLImage1dRW:
6194 case clang::BuiltinType::OCLImage1dArrayRO:
6195 case clang::BuiltinType::OCLImage1dArrayWO:
6196 case clang::BuiltinType::OCLImage1dArrayRW:
6197 case clang::BuiltinType::OCLImage1dBufferRO:
6198 case clang::BuiltinType::OCLImage1dBufferWO:
6199 case clang::BuiltinType::OCLImage1dBufferRW:
6200 case clang::BuiltinType::OCLImage2dRO:
6201 case clang::BuiltinType::OCLImage2dWO:
6202 case clang::BuiltinType::OCLImage2dRW:
6203 case clang::BuiltinType::OCLImage2dArrayRO:
6204 case clang::BuiltinType::OCLImage2dArrayWO:
6205 case clang::BuiltinType::OCLImage2dArrayRW:
6206 case clang::BuiltinType::OCLImage3dRO:
6207 case clang::BuiltinType::OCLImage3dWO:
6208 case clang::BuiltinType::OCLImage3dRW:
6209 case clang::BuiltinType::OCLSampler:
6210 return 0;
6211 case clang::BuiltinType::Bool:
6212 case clang::BuiltinType::Char_U:
6213 case clang::BuiltinType::UChar:
6214 case clang::BuiltinType::WChar_U:
6215 case clang::BuiltinType::Char16:
6216 case clang::BuiltinType::Char32:
6217 case clang::BuiltinType::UShort:
6218 case clang::BuiltinType::UInt:
6219 case clang::BuiltinType::ULong:
6220 case clang::BuiltinType::ULongLong:
6221 case clang::BuiltinType::UInt128:
6222 case clang::BuiltinType::Char_S:
6223 case clang::BuiltinType::SChar:
6224 case clang::BuiltinType::WChar_S:
6225 case clang::BuiltinType::Short:
6226 case clang::BuiltinType::Int:
6227 case clang::BuiltinType::Long:
6228 case clang::BuiltinType::LongLong:
6229 case clang::BuiltinType::Int128:
6230 case clang::BuiltinType::Float:
6231 case clang::BuiltinType::Double:
6232 case clang::BuiltinType::LongDouble:
6233 case clang::BuiltinType::Dependent:
6234 case clang::BuiltinType::Overload:
6235 case clang::BuiltinType::ObjCId:
6236 case clang::BuiltinType::ObjCClass:
6237 case clang::BuiltinType::ObjCSel:
6238 case clang::BuiltinType::BoundMember:
6239 case clang::BuiltinType::Half:
6240 case clang::BuiltinType::ARCUnbridgedCast:
6241 case clang::BuiltinType::PseudoObject:
6242 case clang::BuiltinType::BuiltinFn:
6243 case clang::BuiltinType::OMPArraySection:
6244 return 1;
6245 default:
6246 return 0;
6247 }
6248 break;
6249
6250 case clang::Type::Complex:
6251 return 1;
6252 case clang::Type::Pointer:
6253 return 1;
6254 case clang::Type::BlockPointer:
6255 return 0; // If block pointers don't have debug info, then no children for
6256 // them
6257 case clang::Type::LValueReference:
6258 return 1;
6259 case clang::Type::RValueReference:
6260 return 1;
6261 case clang::Type::MemberPointer:
6262 return 0;
6263 case clang::Type::ConstantArray:
6264 return 0;
6265 case clang::Type::IncompleteArray:
6266 return 0;
6267 case clang::Type::VariableArray:
6268 return 0;
6269 case clang::Type::DependentSizedArray:
6270 return 0;
6271 case clang::Type::DependentSizedExtVector:
6272 return 0;
6273 case clang::Type::Vector:
6274 return 0;
6275 case clang::Type::ExtVector:
6276 return 0;
6277 case clang::Type::FunctionProto:
6278 return 0; // When we function pointers, they have no children...
6279 case clang::Type::FunctionNoProto:
6280 return 0; // When we function pointers, they have no children...
6281 case clang::Type::UnresolvedUsing:
6282 return 0;
6283 case clang::Type::Paren:
6284 return GetNumPointeeChildren(
6285 llvm::cast<clang::ParenType>(qual_type)->desugar());
6286 case clang::Type::Typedef:
6287 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6288 ->getDecl()
6289 ->getUnderlyingType());
6290 case clang::Type::Auto:
6291 return GetNumPointeeChildren(
6292 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6293 case clang::Type::Elaborated:
6294 return GetNumPointeeChildren(
6295 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6296 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006297 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6298 ->getUnderlyingExpr()
6299 ->getType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006300 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006301 return GetNumPointeeChildren(
6302 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006303 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006304 return GetNumPointeeChildren(
6305 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006306 case clang::Type::Record:
6307 return 0;
6308 case clang::Type::Enum:
6309 return 1;
6310 case clang::Type::TemplateTypeParm:
6311 return 1;
6312 case clang::Type::SubstTemplateTypeParm:
6313 return 1;
6314 case clang::Type::TemplateSpecialization:
6315 return 1;
6316 case clang::Type::InjectedClassName:
6317 return 0;
6318 case clang::Type::DependentName:
6319 return 1;
6320 case clang::Type::DependentTemplateSpecialization:
6321 return 1;
6322 case clang::Type::ObjCObject:
6323 return 0;
6324 case clang::Type::ObjCInterface:
6325 return 0;
6326 case clang::Type::ObjCObjectPointer:
6327 return 1;
6328 default:
6329 break;
6330 }
6331 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006332}
6333
Kate Stoneb9c1b512016-09-06 20:57:50 +00006334CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6335 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6336 bool transparent_pointers, bool omit_empty_base_classes,
6337 bool ignore_array_bounds, std::string &child_name,
6338 uint32_t &child_byte_size, int32_t &child_byte_offset,
6339 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6340 bool &child_is_base_class, bool &child_is_deref_of_parent,
6341 ValueObject *valobj, uint64_t &language_flags) {
6342 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006343 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006344
Kate Stoneb9c1b512016-09-06 20:57:50 +00006345 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6346 const clang::Type::TypeClass parent_type_class =
6347 parent_qual_type->getTypeClass();
6348 child_bitfield_bit_size = 0;
6349 child_bitfield_bit_offset = 0;
6350 child_is_base_class = false;
6351 language_flags = 0;
6352
6353 const bool idx_is_valid = idx < GetNumChildren(type, omit_empty_base_classes);
6354 uint32_t bit_offset;
6355 switch (parent_type_class) {
6356 case clang::Type::Builtin:
6357 if (idx_is_valid) {
6358 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6359 case clang::BuiltinType::ObjCId:
6360 case clang::BuiltinType::ObjCClass:
6361 child_name = "isa";
6362 child_byte_size =
6363 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6364 CHAR_BIT;
6365 return CompilerType(getASTContext(),
6366 getASTContext()->ObjCBuiltinClassTy);
6367
6368 default:
6369 break;
6370 }
6371 }
6372 break;
6373
6374 case clang::Type::Record:
6375 if (idx_is_valid && GetCompleteType(type)) {
6376 const clang::RecordType *record_type =
6377 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6378 const clang::RecordDecl *record_decl = record_type->getDecl();
6379 assert(record_decl);
6380 const clang::ASTRecordLayout &record_layout =
6381 getASTContext()->getASTRecordLayout(record_decl);
6382 uint32_t child_idx = 0;
6383
6384 const clang::CXXRecordDecl *cxx_record_decl =
6385 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6386 if (cxx_record_decl) {
6387 // We might have base classes to print out first
6388 clang::CXXRecordDecl::base_class_const_iterator base_class,
6389 base_class_end;
6390 for (base_class = cxx_record_decl->bases_begin(),
6391 base_class_end = cxx_record_decl->bases_end();
6392 base_class != base_class_end; ++base_class) {
6393 const clang::CXXRecordDecl *base_class_decl = nullptr;
6394
6395 // Skip empty base classes
6396 if (omit_empty_base_classes) {
6397 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6398 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6399 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
6400 continue;
6401 }
6402
6403 if (idx == child_idx) {
6404 if (base_class_decl == nullptr)
6405 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6406 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6407
6408 if (base_class->isVirtual()) {
6409 bool handled = false;
6410 if (valobj) {
Zachary Turner97206d52017-05-12 04:51:55 +00006411 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006412 AddressType addr_type = eAddressTypeInvalid;
6413 lldb::addr_t vtable_ptr_addr =
6414 valobj->GetCPPVTableAddress(addr_type);
6415
6416 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS &&
6417 addr_type == eAddressTypeLoad) {
6418
6419 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
6420 Process *process = exe_ctx.GetProcessPtr();
6421 if (process) {
6422 clang::VTableContextBase *vtable_ctx =
6423 getASTContext()->getVTableContext();
6424 if (vtable_ctx) {
6425 if (vtable_ctx->isMicrosoft()) {
6426 clang::MicrosoftVTableContext *msoft_vtable_ctx =
6427 static_cast<clang::MicrosoftVTableContext *>(
6428 vtable_ctx);
6429
6430 if (vtable_ptr_addr) {
6431 const lldb::addr_t vbtable_ptr_addr =
6432 vtable_ptr_addr +
6433 record_layout.getVBPtrOffset().getQuantity();
6434
6435 const lldb::addr_t vbtable_ptr =
6436 process->ReadPointerFromMemory(vbtable_ptr_addr,
6437 err);
6438 if (vbtable_ptr != LLDB_INVALID_ADDRESS) {
6439 // Get the index into the virtual base table. The
6440 // index is the index in uint32_t from vbtable_ptr
6441 const unsigned vbtable_index =
6442 msoft_vtable_ctx->getVBTableIndex(
6443 cxx_record_decl, base_class_decl);
6444 const lldb::addr_t base_offset_addr =
6445 vbtable_ptr + vbtable_index * 4;
6446 const uint32_t base_offset =
6447 process->ReadUnsignedIntegerFromMemory(
6448 base_offset_addr, 4, UINT32_MAX, err);
6449 if (base_offset != UINT32_MAX) {
6450 handled = true;
6451 bit_offset = base_offset * 8;
6452 }
6453 }
6454 }
6455 } else {
6456 clang::ItaniumVTableContext *itanium_vtable_ctx =
6457 static_cast<clang::ItaniumVTableContext *>(
6458 vtable_ctx);
6459 if (vtable_ptr_addr) {
6460 const lldb::addr_t vtable_ptr =
6461 process->ReadPointerFromMemory(vtable_ptr_addr,
6462 err);
6463 if (vtable_ptr != LLDB_INVALID_ADDRESS) {
6464 clang::CharUnits base_offset_offset =
6465 itanium_vtable_ctx->getVirtualBaseOffsetOffset(
6466 cxx_record_decl, base_class_decl);
6467 const lldb::addr_t base_offset_addr =
6468 vtable_ptr + base_offset_offset.getQuantity();
6469 const uint32_t base_offset_size =
6470 process->GetAddressByteSize();
6471 const uint64_t base_offset =
6472 process->ReadUnsignedIntegerFromMemory(
6473 base_offset_addr, base_offset_size,
6474 UINT32_MAX, err);
6475 if (base_offset < UINT32_MAX) {
6476 handled = true;
6477 bit_offset = base_offset * 8;
6478 }
6479 }
6480 }
6481 }
6482 }
6483 }
6484 }
6485 }
6486 if (!handled)
6487 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6488 .getQuantity() *
6489 8;
6490 } else
6491 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6492 .getQuantity() *
6493 8;
6494
6495 // Base classes should be a multiple of 8 bits in size
6496 child_byte_offset = bit_offset / 8;
6497 CompilerType base_class_clang_type(getASTContext(),
6498 base_class->getType());
6499 child_name = base_class_clang_type.GetTypeName().AsCString("");
6500 uint64_t base_class_clang_type_bit_size =
6501 base_class_clang_type.GetBitSize(
6502 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6503
6504 // Base classes bit sizes should be a multiple of 8 bits in size
6505 assert(base_class_clang_type_bit_size % 8 == 0);
6506 child_byte_size = base_class_clang_type_bit_size / 8;
6507 child_is_base_class = true;
6508 return base_class_clang_type;
6509 }
6510 // We don't increment the child index in the for loop since we might
6511 // be skipping empty base classes
6512 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006513 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006514 }
6515 // Make sure index is in range...
6516 uint32_t field_idx = 0;
6517 clang::RecordDecl::field_iterator field, field_end;
6518 for (field = record_decl->field_begin(),
6519 field_end = record_decl->field_end();
6520 field != field_end; ++field, ++field_idx, ++child_idx) {
6521 if (idx == child_idx) {
6522 // Print the member type if requested
6523 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006524 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006525
6526 // Figure out the type byte size (field_type_info.first) and
6527 // alignment (field_type_info.second) from the AST context.
6528 CompilerType field_clang_type(getASTContext(), field->getType());
6529 assert(field_idx < record_layout.getFieldCount());
6530 child_byte_size = field_clang_type.GetByteSize(
6531 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6532 const uint32_t child_bit_size = child_byte_size * 8;
6533
6534 // Figure out the field offset within the current struct/union/class
6535 // type
6536 bit_offset = record_layout.getFieldOffset(field_idx);
6537 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6538 child_bitfield_bit_size)) {
6539 child_bitfield_bit_offset = bit_offset % child_bit_size;
6540 const uint32_t child_bit_offset =
6541 bit_offset - child_bitfield_bit_offset;
6542 child_byte_offset = child_bit_offset / 8;
6543 } else {
6544 child_byte_offset = bit_offset / 8;
6545 }
6546
6547 return field_clang_type;
6548 }
6549 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006550 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006551 break;
6552
6553 case clang::Type::ObjCObject:
6554 case clang::Type::ObjCInterface:
6555 if (idx_is_valid && GetCompleteType(type)) {
6556 const clang::ObjCObjectType *objc_class_type =
6557 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6558 assert(objc_class_type);
6559 if (objc_class_type) {
6560 uint32_t child_idx = 0;
6561 clang::ObjCInterfaceDecl *class_interface_decl =
6562 objc_class_type->getInterface();
6563
6564 if (class_interface_decl) {
6565
6566 const clang::ASTRecordLayout &interface_layout =
6567 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6568 clang::ObjCInterfaceDecl *superclass_interface_decl =
6569 class_interface_decl->getSuperClass();
6570 if (superclass_interface_decl) {
6571 if (omit_empty_base_classes) {
6572 CompilerType base_class_clang_type(
6573 getASTContext(), getASTContext()->getObjCInterfaceType(
6574 superclass_interface_decl));
6575 if (base_class_clang_type.GetNumChildren(
6576 omit_empty_base_classes) > 0) {
6577 if (idx == 0) {
6578 clang::QualType ivar_qual_type(
6579 getASTContext()->getObjCInterfaceType(
6580 superclass_interface_decl));
6581
6582 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006583 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006584
6585 clang::TypeInfo ivar_type_info =
6586 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6587
6588 child_byte_size = ivar_type_info.Width / 8;
6589 child_byte_offset = 0;
6590 child_is_base_class = true;
6591
6592 return CompilerType(getASTContext(), ivar_qual_type);
6593 }
6594
6595 ++child_idx;
6596 }
6597 } else
6598 ++child_idx;
6599 }
6600
6601 const uint32_t superclass_idx = child_idx;
6602
6603 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6604 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6605 ivar_end = class_interface_decl->ivar_end();
6606
6607 for (ivar_pos = class_interface_decl->ivar_begin();
6608 ivar_pos != ivar_end; ++ivar_pos) {
6609 if (child_idx == idx) {
6610 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6611
6612 clang::QualType ivar_qual_type(ivar_decl->getType());
6613
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006614 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006615
6616 clang::TypeInfo ivar_type_info =
6617 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6618
6619 child_byte_size = ivar_type_info.Width / 8;
6620
6621 // Figure out the field offset within the current
Adrian Prantl05097242018-04-30 16:49:04 +00006622 // struct/union/class type For ObjC objects, we can't trust the
6623 // bit offset we get from the Clang AST, since that doesn't
6624 // account for the space taken up by unbacked properties, or
6625 // from the changing size of base classes that are newer than
6626 // this class. So if we have a process around that we can ask
6627 // about this object, do so.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006628 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6629 Process *process = nullptr;
6630 if (exe_ctx)
6631 process = exe_ctx->GetProcessPtr();
6632 if (process) {
6633 ObjCLanguageRuntime *objc_runtime =
6634 process->GetObjCLanguageRuntime();
6635 if (objc_runtime != nullptr) {
6636 CompilerType parent_ast_type(getASTContext(),
6637 parent_qual_type);
6638 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6639 parent_ast_type, ivar_decl->getNameAsString().c_str());
6640 }
6641 }
6642
6643 // Setting this to UINT32_MAX to make sure we don't compute it
6644 // twice...
6645 bit_offset = UINT32_MAX;
6646
6647 if (child_byte_offset ==
6648 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6649 bit_offset = interface_layout.getFieldOffset(child_idx -
6650 superclass_idx);
6651 child_byte_offset = bit_offset / 8;
6652 }
6653
6654 // Note, the ObjC Ivar Byte offset is just that, it doesn't
Adrian Prantl05097242018-04-30 16:49:04 +00006655 // account for the bit offset of a bitfield within its
6656 // containing object. So regardless of where we get the byte
Kate Stoneb9c1b512016-09-06 20:57:50 +00006657 // offset from, we still need to get the bit offset for
6658 // bitfields from the layout.
6659
6660 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6661 child_bitfield_bit_size)) {
6662 if (bit_offset == UINT32_MAX)
6663 bit_offset = interface_layout.getFieldOffset(
6664 child_idx - superclass_idx);
6665
6666 child_bitfield_bit_offset = bit_offset % 8;
6667 }
6668 return CompilerType(getASTContext(), ivar_qual_type);
6669 }
6670 ++child_idx;
6671 }
6672 }
6673 }
6674 }
6675 }
6676 break;
6677
6678 case clang::Type::ObjCObjectPointer:
6679 if (idx_is_valid) {
6680 CompilerType pointee_clang_type(GetPointeeType(type));
6681
6682 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6683 child_is_deref_of_parent = false;
6684 bool tmp_child_is_deref_of_parent = false;
6685 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6686 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6687 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6688 child_bitfield_bit_size, child_bitfield_bit_offset,
6689 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6690 language_flags);
6691 } else {
6692 child_is_deref_of_parent = true;
6693 const char *parent_name =
6694 valobj ? valobj->GetName().GetCString() : NULL;
6695 if (parent_name) {
6696 child_name.assign(1, '*');
6697 child_name += parent_name;
6698 }
6699
6700 // We have a pointer to an simple type
6701 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
6702 child_byte_size = pointee_clang_type.GetByteSize(
6703 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6704 child_byte_offset = 0;
6705 return pointee_clang_type;
6706 }
6707 }
6708 }
6709 break;
6710
6711 case clang::Type::Vector:
6712 case clang::Type::ExtVector:
6713 if (idx_is_valid) {
6714 const clang::VectorType *array =
6715 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6716 if (array) {
6717 CompilerType element_type(getASTContext(), array->getElementType());
6718 if (element_type.GetCompleteType()) {
6719 char element_name[64];
6720 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6721 static_cast<uint64_t>(idx));
6722 child_name.assign(element_name);
6723 child_byte_size = element_type.GetByteSize(
6724 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6725 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6726 return element_type;
6727 }
6728 }
6729 }
6730 break;
6731
6732 case clang::Type::ConstantArray:
6733 case clang::Type::IncompleteArray:
6734 if (ignore_array_bounds || idx_is_valid) {
6735 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6736 if (array) {
6737 CompilerType element_type(getASTContext(), array->getElementType());
6738 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006739 child_name = llvm::formatv("[{0}]", idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006740 child_byte_size = element_type.GetByteSize(
6741 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6742 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6743 return element_type;
6744 }
6745 }
6746 }
6747 break;
6748
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006749 case clang::Type::Pointer: {
6750 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006751
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006752 // Don't dereference "void *" pointers
6753 if (pointee_clang_type.IsVoidType())
6754 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006755
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006756 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6757 child_is_deref_of_parent = false;
6758 bool tmp_child_is_deref_of_parent = false;
6759 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6760 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6761 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6762 child_bitfield_bit_size, child_bitfield_bit_offset,
6763 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6764 language_flags);
6765 } else {
6766 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006767
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006768 const char *parent_name =
6769 valobj ? valobj->GetName().GetCString() : NULL;
6770 if (parent_name) {
6771 child_name.assign(1, '*');
6772 child_name += parent_name;
6773 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006774
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006775 // We have a pointer to an simple type
6776 if (idx == 0) {
6777 child_byte_size = pointee_clang_type.GetByteSize(
6778 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6779 child_byte_offset = 0;
6780 return pointee_clang_type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006781 }
6782 }
6783 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006784 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006785
6786 case clang::Type::LValueReference:
6787 case clang::Type::RValueReference:
6788 if (idx_is_valid) {
6789 const clang::ReferenceType *reference_type =
6790 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6791 CompilerType pointee_clang_type(getASTContext(),
6792 reference_type->getPointeeType());
6793 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6794 child_is_deref_of_parent = false;
6795 bool tmp_child_is_deref_of_parent = false;
6796 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6797 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6798 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6799 child_bitfield_bit_size, child_bitfield_bit_offset,
6800 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6801 language_flags);
6802 } else {
6803 const char *parent_name =
6804 valobj ? valobj->GetName().GetCString() : NULL;
6805 if (parent_name) {
6806 child_name.assign(1, '&');
6807 child_name += parent_name;
6808 }
6809
6810 // We have a pointer to an simple type
6811 if (idx == 0) {
6812 child_byte_size = pointee_clang_type.GetByteSize(
6813 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6814 child_byte_offset = 0;
6815 return pointee_clang_type;
6816 }
6817 }
6818 }
6819 break;
6820
6821 case clang::Type::Typedef: {
6822 CompilerType typedefed_clang_type(
6823 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
6824 ->getDecl()
6825 ->getUnderlyingType());
6826 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
6827 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6828 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6829 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6830 child_is_deref_of_parent, valobj, language_flags);
6831 } break;
6832
6833 case clang::Type::Auto: {
6834 CompilerType elaborated_clang_type(
6835 getASTContext(),
6836 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
6837 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6838 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6839 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6840 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6841 child_is_deref_of_parent, valobj, language_flags);
6842 }
6843
6844 case clang::Type::Elaborated: {
6845 CompilerType elaborated_clang_type(
6846 getASTContext(),
6847 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
6848 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6849 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6850 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6851 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6852 child_is_deref_of_parent, valobj, language_flags);
6853 }
6854
6855 case clang::Type::Paren: {
6856 CompilerType paren_clang_type(
6857 getASTContext(),
6858 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
6859 return paren_clang_type.GetChildCompilerTypeAtIndex(
6860 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6861 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6862 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6863 child_is_deref_of_parent, valobj, language_flags);
6864 }
6865
6866 default:
6867 break;
6868 }
6869 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006870}
6871
Kate Stoneb9c1b512016-09-06 20:57:50 +00006872static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
6873 const clang::CXXBaseSpecifier *base_spec,
6874 bool omit_empty_base_classes) {
6875 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006876
Kate Stoneb9c1b512016-09-06 20:57:50 +00006877 const clang::CXXRecordDecl *cxx_record_decl =
6878 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6879
6880 // const char *super_name = record_decl->getNameAsCString();
6881 // const char *base_name =
6882 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6883 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6884 //
6885 if (cxx_record_decl) {
6886 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6887 for (base_class = cxx_record_decl->bases_begin(),
6888 base_class_end = cxx_record_decl->bases_end();
6889 base_class != base_class_end; ++base_class) {
6890 if (omit_empty_base_classes) {
6891 if (BaseSpecifierIsEmpty(base_class))
6892 continue;
6893 }
6894
6895 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
6896 // super_name, base_name,
6897 // child_idx,
6898 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6899 //
6900 //
6901 if (base_class == base_spec)
6902 return child_idx;
6903 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006904 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006905 }
6906
6907 return UINT32_MAX;
6908}
6909
6910static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
6911 clang::NamedDecl *canonical_decl,
6912 bool omit_empty_base_classes) {
6913 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
6914 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6915 omit_empty_base_classes);
6916
6917 clang::RecordDecl::field_iterator field, field_end;
6918 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6919 field != field_end; ++field, ++child_idx) {
6920 if (field->getCanonicalDecl() == canonical_decl)
6921 return child_idx;
6922 }
6923
6924 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00006925}
6926
6927// Look for a child member (doesn't include base classes, but it does include
Adrian Prantl05097242018-04-30 16:49:04 +00006928// their members) in the type hierarchy. Returns an index path into
6929// "clang_type" on how to reach the appropriate member.
Greg Claytond8d4a572015-08-11 21:38:15 +00006930//
6931// class A
6932// {
6933// public:
6934// int m_a;
6935// int m_b;
6936// };
6937//
6938// class B
6939// {
6940// };
6941//
6942// class C :
6943// public B,
6944// public A
6945// {
6946// };
6947//
6948// If we have a clang type that describes "class C", and we wanted to looked
6949// "m_b" in it:
6950//
Kate Stoneb9c1b512016-09-06 20:57:50 +00006951// With omit_empty_base_classes == false we would get an integer array back
Adrian Prantl05097242018-04-30 16:49:04 +00006952// with: { 1, 1 } The first index 1 is the child index for "class A" within
6953// class C The second index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00006954//
Adrian Prantl05097242018-04-30 16:49:04 +00006955// With omit_empty_base_classes == true we would get an integer array back
6956// with: { 0, 1 } The first index 0 is the child index for "class A" within
6957// class C (since class B doesn't have any members it doesn't count) The second
6958// index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00006959
Kate Stoneb9c1b512016-09-06 20:57:50 +00006960size_t ClangASTContext::GetIndexOfChildMemberWithName(
6961 lldb::opaque_compiler_type_t type, const char *name,
6962 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
6963 if (type && name && name[0]) {
6964 clang::QualType qual_type(GetCanonicalQualType(type));
6965 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6966 switch (type_class) {
6967 case clang::Type::Record:
6968 if (GetCompleteType(type)) {
6969 const clang::RecordType *record_type =
6970 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6971 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00006972
Kate Stoneb9c1b512016-09-06 20:57:50 +00006973 assert(record_decl);
6974 uint32_t child_idx = 0;
6975
6976 const clang::CXXRecordDecl *cxx_record_decl =
6977 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6978
6979 // Try and find a field that matches NAME
6980 clang::RecordDecl::field_iterator field, field_end;
6981 llvm::StringRef name_sref(name);
6982 for (field = record_decl->field_begin(),
6983 field_end = record_decl->field_end();
6984 field != field_end; ++field, ++child_idx) {
6985 llvm::StringRef field_name = field->getName();
6986 if (field_name.empty()) {
6987 CompilerType field_type(getASTContext(), field->getType());
6988 child_indexes.push_back(child_idx);
6989 if (field_type.GetIndexOfChildMemberWithName(
6990 name, omit_empty_base_classes, child_indexes))
6991 return child_indexes.size();
6992 child_indexes.pop_back();
6993
6994 } else if (field_name.equals(name_sref)) {
6995 // We have to add on the number of base classes to this index!
6996 child_indexes.push_back(
6997 child_idx + ClangASTContext::GetNumBaseClasses(
6998 cxx_record_decl, omit_empty_base_classes));
6999 return child_indexes.size();
7000 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007001 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007002
Kate Stoneb9c1b512016-09-06 20:57:50 +00007003 if (cxx_record_decl) {
7004 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7005
7006 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7007
7008 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7009 // Didn't find things easily, lets let clang do its thang...
7010 clang::IdentifierInfo &ident_ref =
7011 getASTContext()->Idents.get(name_sref);
7012 clang::DeclarationName decl_name(&ident_ref);
7013
7014 clang::CXXBasePaths paths;
7015 if (cxx_record_decl->lookupInBases(
7016 [decl_name](const clang::CXXBaseSpecifier *specifier,
7017 clang::CXXBasePath &path) {
7018 return clang::CXXRecordDecl::FindOrdinaryMember(
7019 specifier, path, decl_name);
7020 },
7021 paths)) {
7022 clang::CXXBasePaths::const_paths_iterator path,
7023 path_end = paths.end();
7024 for (path = paths.begin(); path != path_end; ++path) {
7025 const size_t num_path_elements = path->size();
7026 for (size_t e = 0; e < num_path_elements; ++e) {
7027 clang::CXXBasePathElement elem = (*path)[e];
7028
7029 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7030 omit_empty_base_classes);
7031 if (child_idx == UINT32_MAX) {
7032 child_indexes.clear();
7033 return 0;
7034 } else {
7035 child_indexes.push_back(child_idx);
7036 parent_record_decl = llvm::cast<clang::RecordDecl>(
7037 elem.Base->getType()
7038 ->getAs<clang::RecordType>()
7039 ->getDecl());
7040 }
7041 }
7042 for (clang::NamedDecl *path_decl : path->Decls) {
7043 child_idx = GetIndexForRecordChild(
7044 parent_record_decl, path_decl, omit_empty_base_classes);
7045 if (child_idx == UINT32_MAX) {
7046 child_indexes.clear();
7047 return 0;
7048 } else {
7049 child_indexes.push_back(child_idx);
7050 }
7051 }
7052 }
7053 return child_indexes.size();
7054 }
7055 }
7056 }
7057 break;
7058
7059 case clang::Type::ObjCObject:
7060 case clang::Type::ObjCInterface:
7061 if (GetCompleteType(type)) {
7062 llvm::StringRef name_sref(name);
7063 const clang::ObjCObjectType *objc_class_type =
7064 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7065 assert(objc_class_type);
7066 if (objc_class_type) {
7067 uint32_t child_idx = 0;
7068 clang::ObjCInterfaceDecl *class_interface_decl =
7069 objc_class_type->getInterface();
7070
7071 if (class_interface_decl) {
7072 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7073 ivar_end = class_interface_decl->ivar_end();
7074 clang::ObjCInterfaceDecl *superclass_interface_decl =
7075 class_interface_decl->getSuperClass();
7076
7077 for (ivar_pos = class_interface_decl->ivar_begin();
7078 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7079 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7080
7081 if (ivar_decl->getName().equals(name_sref)) {
7082 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7083 (omit_empty_base_classes &&
7084 ObjCDeclHasIVars(superclass_interface_decl, true)))
7085 ++child_idx;
7086
7087 child_indexes.push_back(child_idx);
7088 return child_indexes.size();
7089 }
7090 }
7091
7092 if (superclass_interface_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00007093 // The super class index is always zero for ObjC classes, so we
7094 // push it onto the child indexes in case we find an ivar in our
7095 // superclass...
Kate Stoneb9c1b512016-09-06 20:57:50 +00007096 child_indexes.push_back(0);
7097
7098 CompilerType superclass_clang_type(
7099 getASTContext(), getASTContext()->getObjCInterfaceType(
7100 superclass_interface_decl));
7101 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7102 name, omit_empty_base_classes, child_indexes)) {
Adrian Prantl05097242018-04-30 16:49:04 +00007103 // We did find an ivar in a superclass so just return the
7104 // results!
Kate Stoneb9c1b512016-09-06 20:57:50 +00007105 return child_indexes.size();
7106 }
7107
Adrian Prantl05097242018-04-30 16:49:04 +00007108 // We didn't find an ivar matching "name" in our superclass, pop
7109 // the superclass zero index that we pushed on above.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007110 child_indexes.pop_back();
7111 }
7112 }
7113 }
7114 }
7115 break;
7116
7117 case clang::Type::ObjCObjectPointer: {
7118 CompilerType objc_object_clang_type(
7119 getASTContext(),
7120 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7121 ->getPointeeType());
7122 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7123 name, omit_empty_base_classes, child_indexes);
7124 } break;
7125
7126 case clang::Type::ConstantArray: {
7127 // const clang::ConstantArrayType *array =
7128 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7129 // const uint64_t element_count =
7130 // array->getSize().getLimitedValue();
7131 //
7132 // if (idx < element_count)
7133 // {
7134 // std::pair<uint64_t, unsigned> field_type_info =
7135 // ast->getTypeInfo(array->getElementType());
7136 //
7137 // char element_name[32];
7138 // ::snprintf (element_name, sizeof (element_name),
7139 // "%s[%u]", parent_name ? parent_name : "", idx);
7140 //
7141 // child_name.assign(element_name);
7142 // assert(field_type_info.first % 8 == 0);
7143 // child_byte_size = field_type_info.first / 8;
7144 // child_byte_offset = idx * child_byte_size;
7145 // return array->getElementType().getAsOpaquePtr();
7146 // }
7147 } break;
7148
7149 // case clang::Type::MemberPointerType:
7150 // {
7151 // MemberPointerType *mem_ptr_type =
7152 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7153 // clang::QualType pointee_type =
7154 // mem_ptr_type->getPointeeType();
7155 //
7156 // if (ClangASTContext::IsAggregateType
7157 // (pointee_type.getAsOpaquePtr()))
7158 // {
7159 // return GetIndexOfChildWithName (ast,
7160 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7161 // name);
7162 // }
7163 // }
7164 // break;
7165 //
7166 case clang::Type::LValueReference:
7167 case clang::Type::RValueReference: {
7168 const clang::ReferenceType *reference_type =
7169 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7170 clang::QualType pointee_type(reference_type->getPointeeType());
7171 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7172
7173 if (pointee_clang_type.IsAggregateType()) {
7174 return pointee_clang_type.GetIndexOfChildMemberWithName(
7175 name, omit_empty_base_classes, child_indexes);
7176 }
7177 } break;
7178
7179 case clang::Type::Pointer: {
7180 CompilerType pointee_clang_type(GetPointeeType(type));
7181
7182 if (pointee_clang_type.IsAggregateType()) {
7183 return pointee_clang_type.GetIndexOfChildMemberWithName(
7184 name, omit_empty_base_classes, child_indexes);
7185 }
7186 } break;
7187
7188 case clang::Type::Typedef:
7189 return CompilerType(getASTContext(),
7190 llvm::cast<clang::TypedefType>(qual_type)
7191 ->getDecl()
7192 ->getUnderlyingType())
7193 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7194 child_indexes);
7195
7196 case clang::Type::Auto:
7197 return CompilerType(
7198 getASTContext(),
7199 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7200 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7201 child_indexes);
7202
7203 case clang::Type::Elaborated:
7204 return CompilerType(
7205 getASTContext(),
7206 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7207 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7208 child_indexes);
7209
7210 case clang::Type::Paren:
7211 return CompilerType(getASTContext(),
7212 llvm::cast<clang::ParenType>(qual_type)->desugar())
7213 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7214 child_indexes);
7215
7216 default:
7217 break;
7218 }
7219 }
7220 return 0;
7221}
Greg Claytond8d4a572015-08-11 21:38:15 +00007222
7223// Get the index of the child of "clang_type" whose name matches. This function
7224// doesn't descend into the children, but only looks one level deep and name
7225// matches can include base class names.
7226
7227uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007228ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7229 const char *name,
7230 bool omit_empty_base_classes) {
7231 if (type && name && name[0]) {
7232 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007233
Kate Stoneb9c1b512016-09-06 20:57:50 +00007234 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7235
7236 switch (type_class) {
7237 case clang::Type::Record:
7238 if (GetCompleteType(type)) {
7239 const clang::RecordType *record_type =
7240 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7241 const clang::RecordDecl *record_decl = record_type->getDecl();
7242
7243 assert(record_decl);
7244 uint32_t child_idx = 0;
7245
7246 const clang::CXXRecordDecl *cxx_record_decl =
7247 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7248
7249 if (cxx_record_decl) {
7250 clang::CXXRecordDecl::base_class_const_iterator base_class,
7251 base_class_end;
7252 for (base_class = cxx_record_decl->bases_begin(),
7253 base_class_end = cxx_record_decl->bases_end();
7254 base_class != base_class_end; ++base_class) {
7255 // Skip empty base classes
7256 clang::CXXRecordDecl *base_class_decl =
7257 llvm::cast<clang::CXXRecordDecl>(
7258 base_class->getType()
7259 ->getAs<clang::RecordType>()
7260 ->getDecl());
7261 if (omit_empty_base_classes &&
7262 ClangASTContext::RecordHasFields(base_class_decl) == false)
7263 continue;
7264
7265 CompilerType base_class_clang_type(getASTContext(),
7266 base_class->getType());
7267 std::string base_class_type_name(
7268 base_class_clang_type.GetTypeName().AsCString(""));
7269 if (base_class_type_name.compare(name) == 0)
7270 return child_idx;
7271 ++child_idx;
7272 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007273 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007274
Kate Stoneb9c1b512016-09-06 20:57:50 +00007275 // Try and find a field that matches NAME
7276 clang::RecordDecl::field_iterator field, field_end;
7277 llvm::StringRef name_sref(name);
7278 for (field = record_decl->field_begin(),
7279 field_end = record_decl->field_end();
7280 field != field_end; ++field, ++child_idx) {
7281 if (field->getName().equals(name_sref))
7282 return child_idx;
7283 }
7284 }
7285 break;
7286
7287 case clang::Type::ObjCObject:
7288 case clang::Type::ObjCInterface:
7289 if (GetCompleteType(type)) {
7290 llvm::StringRef name_sref(name);
7291 const clang::ObjCObjectType *objc_class_type =
7292 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7293 assert(objc_class_type);
7294 if (objc_class_type) {
7295 uint32_t child_idx = 0;
7296 clang::ObjCInterfaceDecl *class_interface_decl =
7297 objc_class_type->getInterface();
7298
7299 if (class_interface_decl) {
7300 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7301 ivar_end = class_interface_decl->ivar_end();
7302 clang::ObjCInterfaceDecl *superclass_interface_decl =
7303 class_interface_decl->getSuperClass();
7304
7305 for (ivar_pos = class_interface_decl->ivar_begin();
7306 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7307 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7308
7309 if (ivar_decl->getName().equals(name_sref)) {
7310 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7311 (omit_empty_base_classes &&
7312 ObjCDeclHasIVars(superclass_interface_decl, true)))
7313 ++child_idx;
7314
7315 return child_idx;
7316 }
7317 }
7318
7319 if (superclass_interface_decl) {
7320 if (superclass_interface_decl->getName().equals(name_sref))
7321 return 0;
7322 }
7323 }
7324 }
7325 }
7326 break;
7327
7328 case clang::Type::ObjCObjectPointer: {
7329 CompilerType pointee_clang_type(
7330 getASTContext(),
7331 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7332 ->getPointeeType());
7333 return pointee_clang_type.GetIndexOfChildWithName(
7334 name, omit_empty_base_classes);
7335 } break;
7336
7337 case clang::Type::ConstantArray: {
7338 // const clang::ConstantArrayType *array =
7339 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7340 // const uint64_t element_count =
7341 // array->getSize().getLimitedValue();
7342 //
7343 // if (idx < element_count)
7344 // {
7345 // std::pair<uint64_t, unsigned> field_type_info =
7346 // ast->getTypeInfo(array->getElementType());
7347 //
7348 // char element_name[32];
7349 // ::snprintf (element_name, sizeof (element_name),
7350 // "%s[%u]", parent_name ? parent_name : "", idx);
7351 //
7352 // child_name.assign(element_name);
7353 // assert(field_type_info.first % 8 == 0);
7354 // child_byte_size = field_type_info.first / 8;
7355 // child_byte_offset = idx * child_byte_size;
7356 // return array->getElementType().getAsOpaquePtr();
7357 // }
7358 } break;
7359
7360 // case clang::Type::MemberPointerType:
7361 // {
7362 // MemberPointerType *mem_ptr_type =
7363 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7364 // clang::QualType pointee_type =
7365 // mem_ptr_type->getPointeeType();
7366 //
7367 // if (ClangASTContext::IsAggregateType
7368 // (pointee_type.getAsOpaquePtr()))
7369 // {
7370 // return GetIndexOfChildWithName (ast,
7371 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7372 // name);
7373 // }
7374 // }
7375 // break;
7376 //
7377 case clang::Type::LValueReference:
7378 case clang::Type::RValueReference: {
7379 const clang::ReferenceType *reference_type =
7380 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7381 CompilerType pointee_type(getASTContext(),
7382 reference_type->getPointeeType());
7383
7384 if (pointee_type.IsAggregateType()) {
7385 return pointee_type.GetIndexOfChildWithName(name,
7386 omit_empty_base_classes);
7387 }
7388 } break;
7389
7390 case clang::Type::Pointer: {
7391 const clang::PointerType *pointer_type =
7392 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7393 CompilerType pointee_type(getASTContext(),
7394 pointer_type->getPointeeType());
7395
7396 if (pointee_type.IsAggregateType()) {
7397 return pointee_type.GetIndexOfChildWithName(name,
7398 omit_empty_base_classes);
7399 } else {
7400 // if (parent_name)
7401 // {
7402 // child_name.assign(1, '*');
7403 // child_name += parent_name;
7404 // }
7405 //
7406 // // We have a pointer to an simple type
7407 // if (idx == 0)
7408 // {
7409 // std::pair<uint64_t, unsigned> clang_type_info
7410 // = ast->getTypeInfo(pointee_type);
7411 // assert(clang_type_info.first % 8 == 0);
7412 // child_byte_size = clang_type_info.first / 8;
7413 // child_byte_offset = 0;
7414 // return pointee_type.getAsOpaquePtr();
7415 // }
7416 }
7417 } break;
7418
7419 case clang::Type::Auto:
7420 return CompilerType(
7421 getASTContext(),
7422 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7423 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7424
7425 case clang::Type::Elaborated:
7426 return CompilerType(
7427 getASTContext(),
7428 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7429 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7430
7431 case clang::Type::Paren:
7432 return CompilerType(getASTContext(),
7433 llvm::cast<clang::ParenType>(qual_type)->desugar())
7434 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7435
7436 case clang::Type::Typedef:
7437 return CompilerType(getASTContext(),
7438 llvm::cast<clang::TypedefType>(qual_type)
7439 ->getDecl()
7440 ->getUnderlyingType())
7441 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7442
7443 default:
7444 break;
7445 }
7446 }
7447 return UINT32_MAX;
7448}
Greg Claytond8d4a572015-08-11 21:38:15 +00007449
7450size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007451ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7452 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007453 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007454
Kate Stoneb9c1b512016-09-06 20:57:50 +00007455 clang::QualType qual_type(GetCanonicalQualType(type));
7456 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7457 switch (type_class) {
7458 case clang::Type::Record:
7459 if (GetCompleteType(type)) {
7460 const clang::CXXRecordDecl *cxx_record_decl =
7461 qual_type->getAsCXXRecordDecl();
7462 if (cxx_record_decl) {
7463 const clang::ClassTemplateSpecializationDecl *template_decl =
7464 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7465 cxx_record_decl);
7466 if (template_decl)
7467 return template_decl->getTemplateArgs().size();
7468 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007469 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007470 break;
7471
7472 case clang::Type::Typedef:
7473 return (CompilerType(getASTContext(),
7474 llvm::cast<clang::TypedefType>(qual_type)
7475 ->getDecl()
7476 ->getUnderlyingType()))
7477 .GetNumTemplateArguments();
7478
7479 case clang::Type::Auto:
7480 return (CompilerType(
7481 getASTContext(),
7482 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7483 .GetNumTemplateArguments();
7484
7485 case clang::Type::Elaborated:
7486 return (CompilerType(
7487 getASTContext(),
7488 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7489 .GetNumTemplateArguments();
7490
7491 case clang::Type::Paren:
7492 return (CompilerType(getASTContext(),
7493 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7494 .GetNumTemplateArguments();
7495
7496 default:
7497 break;
7498 }
7499
7500 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007501}
7502
Pavel Labath769b21e2017-11-13 14:26:21 +00007503const clang::ClassTemplateSpecializationDecl *
7504ClangASTContext::GetAsTemplateSpecialization(
7505 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007506 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007507 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007508
7509 clang::QualType qual_type(GetCanonicalQualType(type));
7510 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7511 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007512 case clang::Type::Record: {
7513 if (! GetCompleteType(type))
7514 return nullptr;
7515 const clang::CXXRecordDecl *cxx_record_decl =
7516 qual_type->getAsCXXRecordDecl();
7517 if (!cxx_record_decl)
7518 return nullptr;
7519 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7520 cxx_record_decl);
7521 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007522
7523 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007524 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7525 ->getDecl()
7526 ->getUnderlyingType()
7527 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007528
7529 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007530 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7531 ->getDeducedType()
7532 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007533
7534 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007535 return GetAsTemplateSpecialization(
7536 llvm::cast<clang::ElaboratedType>(qual_type)
7537 ->getNamedType()
7538 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007539
7540 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007541 return GetAsTemplateSpecialization(
7542 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007543
7544 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007545 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007546 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007547}
7548
7549lldb::TemplateArgumentKind
7550ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7551 size_t arg_idx) {
7552 const clang::ClassTemplateSpecializationDecl *template_decl =
7553 GetAsTemplateSpecialization(type);
7554 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7555 return eTemplateArgumentKindNull;
7556
7557 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7558 case clang::TemplateArgument::Null:
7559 return eTemplateArgumentKindNull;
7560
7561 case clang::TemplateArgument::NullPtr:
7562 return eTemplateArgumentKindNullPtr;
7563
7564 case clang::TemplateArgument::Type:
7565 return eTemplateArgumentKindType;
7566
7567 case clang::TemplateArgument::Declaration:
7568 return eTemplateArgumentKindDeclaration;
7569
7570 case clang::TemplateArgument::Integral:
7571 return eTemplateArgumentKindIntegral;
7572
7573 case clang::TemplateArgument::Template:
7574 return eTemplateArgumentKindTemplate;
7575
7576 case clang::TemplateArgument::TemplateExpansion:
7577 return eTemplateArgumentKindTemplateExpansion;
7578
7579 case clang::TemplateArgument::Expression:
7580 return eTemplateArgumentKindExpression;
7581
7582 case clang::TemplateArgument::Pack:
7583 return eTemplateArgumentKindPack;
7584 }
7585 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7586}
7587
7588CompilerType
7589ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7590 size_t idx) {
7591 const clang::ClassTemplateSpecializationDecl *template_decl =
7592 GetAsTemplateSpecialization(type);
7593 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7594 return CompilerType();
7595
7596 const clang::TemplateArgument &template_arg =
7597 template_decl->getTemplateArgs()[idx];
7598 if (template_arg.getKind() != clang::TemplateArgument::Type)
7599 return CompilerType();
7600
7601 return CompilerType(getASTContext(), template_arg.getAsType());
7602}
7603
Pavel Labathf59056f2017-11-30 10:16:54 +00007604llvm::Optional<CompilerType::IntegralTemplateArgument>
Pavel Labath769b21e2017-11-13 14:26:21 +00007605ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7606 size_t idx) {
7607 const clang::ClassTemplateSpecializationDecl *template_decl =
7608 GetAsTemplateSpecialization(type);
7609 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
Pavel Labathf59056f2017-11-30 10:16:54 +00007610 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007611
7612 const clang::TemplateArgument &template_arg =
7613 template_decl->getTemplateArgs()[idx];
7614 if (template_arg.getKind() != clang::TemplateArgument::Integral)
Pavel Labathf59056f2017-11-30 10:16:54 +00007615 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007616
Pavel Labathf59056f2017-11-30 10:16:54 +00007617 return {{template_arg.getAsIntegral(),
7618 CompilerType(getASTContext(), template_arg.getIntegralType())}};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007619}
7620
Kate Stoneb9c1b512016-09-06 20:57:50 +00007621CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7622 if (type)
7623 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7624 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007625}
7626
Kate Stoneb9c1b512016-09-06 20:57:50 +00007627clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7628 const clang::EnumType *enutype =
7629 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7630 if (enutype)
7631 return enutype->getDecl();
7632 return NULL;
7633}
7634
7635clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7636 const clang::RecordType *record_type =
7637 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7638 if (record_type)
7639 return record_type->getDecl();
7640 return nullptr;
7641}
7642
7643clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
7644 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
7645 if (qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00007646 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007647 else
7648 return qual_type->getAsTagDecl();
Greg Claytone6b36cd2015-12-08 01:02:08 +00007649}
7650
Greg Claytond8d4a572015-08-11 21:38:15 +00007651clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007652ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7653 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007654}
7655
7656clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007657ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7658 const clang::ObjCObjectType *objc_class_type =
7659 llvm::dyn_cast<clang::ObjCObjectType>(
7660 ClangUtil::GetCanonicalQualType(type));
7661 if (objc_class_type)
7662 return objc_class_type->getInterface();
7663 return nullptr;
7664}
7665
7666clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
7667 const CompilerType &type, const char *name,
7668 const CompilerType &field_clang_type, AccessType access,
7669 uint32_t bitfield_bit_size) {
7670 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007671 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007672 ClangASTContext *ast =
7673 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7674 if (!ast)
7675 return nullptr;
7676 clang::ASTContext *clang_ast = ast->getASTContext();
7677
7678 clang::FieldDecl *field = nullptr;
7679
7680 clang::Expr *bit_width = nullptr;
7681 if (bitfield_bit_size != 0) {
7682 llvm::APInt bitfield_bit_size_apint(
7683 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7684 bit_width = new (*clang_ast)
7685 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7686 clang_ast->IntTy, clang::SourceLocation());
7687 }
7688
7689 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7690 if (record_decl) {
7691 field = clang::FieldDecl::Create(
7692 *clang_ast, record_decl, clang::SourceLocation(),
7693 clang::SourceLocation(),
7694 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7695 ClangUtil::GetQualType(field_clang_type), // Field type
7696 nullptr, // TInfo *
7697 bit_width, // BitWidth
7698 false, // Mutable
7699 clang::ICIS_NoInit); // HasInit
7700
7701 if (!name) {
Adrian Prantl05097242018-04-30 16:49:04 +00007702 // Determine whether this field corresponds to an anonymous struct or
7703 // union.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007704 if (const clang::TagType *TagT =
7705 field->getType()->getAs<clang::TagType>()) {
7706 if (clang::RecordDecl *Rec =
7707 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7708 if (!Rec->getDeclName()) {
7709 Rec->setAnonymousStructOrUnion(true);
7710 field->setImplicit();
7711 }
7712 }
7713 }
7714
7715 if (field) {
7716 field->setAccess(
7717 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7718
7719 record_decl->addDecl(field);
7720
7721#ifdef LLDB_CONFIGURATION_DEBUG
7722 VerifyDecl(field);
7723#endif
7724 }
7725 } else {
7726 clang::ObjCInterfaceDecl *class_interface_decl =
7727 ast->GetAsObjCInterfaceDecl(type);
7728
7729 if (class_interface_decl) {
7730 const bool is_synthesized = false;
7731
7732 field_clang_type.GetCompleteType();
7733
7734 field = clang::ObjCIvarDecl::Create(
7735 *clang_ast, class_interface_decl, clang::SourceLocation(),
7736 clang::SourceLocation(),
7737 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7738 ClangUtil::GetQualType(field_clang_type), // Field type
7739 nullptr, // TypeSourceInfo *
7740 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7741 is_synthesized);
7742
7743 if (field) {
7744 class_interface_decl->addDecl(field);
7745
7746#ifdef LLDB_CONFIGURATION_DEBUG
7747 VerifyDecl(field);
7748#endif
7749 }
7750 }
7751 }
7752 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007753}
7754
Kate Stoneb9c1b512016-09-06 20:57:50 +00007755void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7756 if (!type)
7757 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007758
Kate Stoneb9c1b512016-09-06 20:57:50 +00007759 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7760 if (!ast)
7761 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007762
Kate Stoneb9c1b512016-09-06 20:57:50 +00007763 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007764
Kate Stoneb9c1b512016-09-06 20:57:50 +00007765 if (!record_decl)
7766 return;
7767
7768 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7769
7770 IndirectFieldVector indirect_fields;
7771 clang::RecordDecl::field_iterator field_pos;
7772 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7773 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7774 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7775 last_field_pos = field_pos++) {
7776 if (field_pos->isAnonymousStructOrUnion()) {
7777 clang::QualType field_qual_type = field_pos->getType();
7778
7779 const clang::RecordType *field_record_type =
7780 field_qual_type->getAs<clang::RecordType>();
7781
7782 if (!field_record_type)
7783 continue;
7784
7785 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7786
7787 if (!field_record_decl)
7788 continue;
7789
7790 for (clang::RecordDecl::decl_iterator
7791 di = field_record_decl->decls_begin(),
7792 de = field_record_decl->decls_end();
7793 di != de; ++di) {
7794 if (clang::FieldDecl *nested_field_decl =
7795 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7796 clang::NamedDecl **chain =
7797 new (*ast->getASTContext()) clang::NamedDecl *[2];
7798 chain[0] = *field_pos;
7799 chain[1] = nested_field_decl;
7800 clang::IndirectFieldDecl *indirect_field =
7801 clang::IndirectFieldDecl::Create(
7802 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7803 nested_field_decl->getIdentifier(),
7804 nested_field_decl->getType(), {chain, 2});
7805
7806 indirect_field->setImplicit();
7807
7808 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7809 field_pos->getAccess(), nested_field_decl->getAccess()));
7810
7811 indirect_fields.push_back(indirect_field);
7812 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7813 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7814 size_t nested_chain_size =
7815 nested_indirect_field_decl->getChainingSize();
7816 clang::NamedDecl **chain = new (*ast->getASTContext())
7817 clang::NamedDecl *[nested_chain_size + 1];
7818 chain[0] = *field_pos;
7819
7820 int chain_index = 1;
7821 for (clang::IndirectFieldDecl::chain_iterator
7822 nci = nested_indirect_field_decl->chain_begin(),
7823 nce = nested_indirect_field_decl->chain_end();
7824 nci < nce; ++nci) {
7825 chain[chain_index] = *nci;
7826 chain_index++;
7827 }
7828
7829 clang::IndirectFieldDecl *indirect_field =
7830 clang::IndirectFieldDecl::Create(
7831 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7832 nested_indirect_field_decl->getIdentifier(),
7833 nested_indirect_field_decl->getType(),
7834 {chain, nested_chain_size + 1});
7835
7836 indirect_field->setImplicit();
7837
7838 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7839 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
7840
7841 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00007842 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007843 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007844 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007845 }
7846
Adrian Prantl05097242018-04-30 16:49:04 +00007847 // Check the last field to see if it has an incomplete array type as its last
7848 // member and if it does, the tell the record decl about it
Kate Stoneb9c1b512016-09-06 20:57:50 +00007849 if (last_field_pos != field_end_pos) {
7850 if (last_field_pos->getType()->isIncompleteArrayType())
7851 record_decl->hasFlexibleArrayMember();
7852 }
7853
7854 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
7855 ife = indirect_fields.end();
7856 ifi < ife; ++ifi) {
7857 record_decl->addDecl(*ifi);
7858 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007859}
7860
Kate Stoneb9c1b512016-09-06 20:57:50 +00007861void ClangASTContext::SetIsPacked(const CompilerType &type) {
7862 if (type) {
7863 ClangASTContext *ast =
7864 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7865 if (ast) {
7866 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
7867
7868 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00007869 return;
7870
Kate Stoneb9c1b512016-09-06 20:57:50 +00007871 record_decl->addAttr(
7872 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00007873 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007874 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007875}
7876
Kate Stoneb9c1b512016-09-06 20:57:50 +00007877clang::VarDecl *ClangASTContext::AddVariableToRecordType(
7878 const CompilerType &type, const char *name, const CompilerType &var_type,
7879 AccessType access) {
7880 clang::VarDecl *var_decl = nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007881
Kate Stoneb9c1b512016-09-06 20:57:50 +00007882 if (!type.IsValid() || !var_type.IsValid())
7883 return nullptr;
7884 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7885 if (!ast)
7886 return nullptr;
7887
7888 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7889 if (record_decl) {
7890 var_decl = clang::VarDecl::Create(
7891 *ast->getASTContext(), // ASTContext &
7892 record_decl, // DeclContext *
7893 clang::SourceLocation(), // clang::SourceLocation StartLoc
7894 clang::SourceLocation(), // clang::SourceLocation IdLoc
7895 name ? &ast->getASTContext()->Idents.get(name)
7896 : nullptr, // clang::IdentifierInfo *
7897 ClangUtil::GetQualType(var_type), // Variable clang::QualType
7898 nullptr, // TypeSourceInfo *
7899 clang::SC_Static); // StorageClass
7900 if (var_decl) {
7901 var_decl->setAccess(
7902 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7903 record_decl->addDecl(var_decl);
7904
Greg Claytond8d4a572015-08-11 21:38:15 +00007905#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00007906 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00007907#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00007908 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007909 }
7910 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00007911}
7912
Kate Stoneb9c1b512016-09-06 20:57:50 +00007913clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
Davide Italiano675767a2018-03-27 19:40:50 +00007914 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00007915 const CompilerType &method_clang_type, lldb::AccessType access,
7916 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
7917 bool is_attr_used, bool is_artificial) {
7918 if (!type || !method_clang_type.IsValid() || name == nullptr ||
7919 name[0] == '\0')
7920 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007921
Kate Stoneb9c1b512016-09-06 20:57:50 +00007922 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007923
Kate Stoneb9c1b512016-09-06 20:57:50 +00007924 clang::CXXRecordDecl *cxx_record_decl =
7925 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007926
Kate Stoneb9c1b512016-09-06 20:57:50 +00007927 if (cxx_record_decl == nullptr)
7928 return nullptr;
7929
7930 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
7931
7932 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7933
7934 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
7935
7936 const clang::FunctionType *function_type =
7937 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7938
7939 if (function_type == nullptr)
7940 return nullptr;
7941
7942 const clang::FunctionProtoType *method_function_prototype(
7943 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7944
7945 if (!method_function_prototype)
7946 return nullptr;
7947
7948 unsigned int num_params = method_function_prototype->getNumParams();
7949
7950 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7951 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7952
7953 if (is_artificial)
7954 return nullptr; // skip everything artificial
7955
7956 if (name[0] == '~') {
7957 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
7958 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7959 clang::DeclarationNameInfo(
7960 getASTContext()->DeclarationNames.getCXXDestructorName(
7961 getASTContext()->getCanonicalType(record_qual_type)),
7962 clang::SourceLocation()),
7963 method_qual_type, nullptr, is_inline, is_artificial);
7964 cxx_method_decl = cxx_dtor_decl;
7965 } else if (decl_name == cxx_record_decl->getDeclName()) {
7966 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
7967 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7968 clang::DeclarationNameInfo(
7969 getASTContext()->DeclarationNames.getCXXConstructorName(
7970 getASTContext()->getCanonicalType(record_qual_type)),
7971 clang::SourceLocation()),
7972 method_qual_type,
7973 nullptr, // TypeSourceInfo *
7974 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
7975 cxx_method_decl = cxx_ctor_decl;
7976 } else {
7977 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7978 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7979
7980 if (IsOperator(name, op_kind)) {
7981 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
Adrian Prantl05097242018-04-30 16:49:04 +00007982 // Check the number of operator parameters. Sometimes we have seen bad
7983 // DWARF that doesn't correctly describe operators and if we try to
7984 // create a method and add it to the class, clang will assert and
7985 // crash, so we need to make sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007986 const bool is_method = true;
7987 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
7988 is_method, op_kind, num_params))
7989 return nullptr;
7990 cxx_method_decl = clang::CXXMethodDecl::Create(
7991 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7992 clang::DeclarationNameInfo(
7993 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
7994 clang::SourceLocation()),
7995 method_qual_type,
7996 nullptr, // TypeSourceInfo *
7997 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
7998 } else if (num_params == 0) {
7999 // Conversion operators don't take params...
8000 cxx_method_decl = clang::CXXConversionDecl::Create(
8001 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8002 clang::DeclarationNameInfo(
8003 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8004 getASTContext()->getCanonicalType(
8005 function_type->getReturnType())),
8006 clang::SourceLocation()),
8007 method_qual_type,
8008 nullptr, // TypeSourceInfo *
8009 is_inline, is_explicit, false /*is_constexpr*/,
8010 clang::SourceLocation());
8011 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008012 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008013
8014 if (cxx_method_decl == nullptr) {
8015 cxx_method_decl = clang::CXXMethodDecl::Create(
8016 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8017 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8018 method_qual_type,
8019 nullptr, // TypeSourceInfo *
8020 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008021 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008022 }
8023
8024 clang::AccessSpecifier access_specifier =
8025 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8026
8027 cxx_method_decl->setAccess(access_specifier);
8028 cxx_method_decl->setVirtualAsWritten(is_virtual);
8029
8030 if (is_attr_used)
8031 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8032
Davide Italiano675767a2018-03-27 19:40:50 +00008033 if (mangled_name != NULL) {
8034 cxx_method_decl->addAttr(
8035 clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name));
8036 }
8037
Kate Stoneb9c1b512016-09-06 20:57:50 +00008038 // Populate the method decl with parameter decls
8039
8040 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8041
8042 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8043 params.push_back(clang::ParmVarDecl::Create(
8044 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8045 clang::SourceLocation(),
8046 nullptr, // anonymous
8047 method_function_prototype->getParamType(param_index), nullptr,
8048 clang::SC_None, nullptr));
8049 }
8050
8051 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8052
8053 cxx_record_decl->addDecl(cxx_method_decl);
8054
8055 // Sometimes the debug info will mention a constructor (default/copy/move),
8056 // destructor, or assignment operator (copy/move) but there won't be any
8057 // version of this in the code. So we check if the function was artificially
8058 // generated and if it is trivial and this lets the compiler/backend know
8059 // that it can inline the IR for these when it needs to and we can avoid a
8060 // "missing function" error when running expressions.
8061
8062 if (is_artificial) {
8063 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8064 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8065 (cxx_ctor_decl->isCopyConstructor() &&
8066 cxx_record_decl->hasTrivialCopyConstructor()) ||
8067 (cxx_ctor_decl->isMoveConstructor() &&
8068 cxx_record_decl->hasTrivialMoveConstructor()))) {
8069 cxx_ctor_decl->setDefaulted();
8070 cxx_ctor_decl->setTrivial(true);
8071 } else if (cxx_dtor_decl) {
8072 if (cxx_record_decl->hasTrivialDestructor()) {
8073 cxx_dtor_decl->setDefaulted();
8074 cxx_dtor_decl->setTrivial(true);
8075 }
8076 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8077 cxx_record_decl->hasTrivialCopyAssignment()) ||
8078 (cxx_method_decl->isMoveAssignmentOperator() &&
8079 cxx_record_decl->hasTrivialMoveAssignment())) {
8080 cxx_method_decl->setDefaulted();
8081 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008082 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008083 }
8084
Greg Claytond8d4a572015-08-11 21:38:15 +00008085#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008086 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008087#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008088
Kate Stoneb9c1b512016-09-06 20:57:50 +00008089 // printf ("decl->isPolymorphic() = %i\n",
8090 // cxx_record_decl->isPolymorphic());
8091 // printf ("decl->isAggregate() = %i\n",
8092 // cxx_record_decl->isAggregate());
8093 // printf ("decl->isPOD() = %i\n",
8094 // cxx_record_decl->isPOD());
8095 // printf ("decl->isEmpty() = %i\n",
8096 // cxx_record_decl->isEmpty());
8097 // printf ("decl->isAbstract() = %i\n",
8098 // cxx_record_decl->isAbstract());
8099 // printf ("decl->hasTrivialConstructor() = %i\n",
8100 // cxx_record_decl->hasTrivialConstructor());
8101 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8102 // cxx_record_decl->hasTrivialCopyConstructor());
8103 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8104 // cxx_record_decl->hasTrivialCopyAssignment());
8105 // printf ("decl->hasTrivialDestructor() = %i\n",
8106 // cxx_record_decl->hasTrivialDestructor());
8107 return cxx_method_decl;
8108}
Greg Claytond8d4a572015-08-11 21:38:15 +00008109
8110#pragma mark C++ Base Classes
8111
8112clang::CXXBaseSpecifier *
Kate Stoneb9c1b512016-09-06 20:57:50 +00008113ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8114 AccessType access, bool is_virtual,
8115 bool base_of_class) {
8116 if (type)
8117 return new clang::CXXBaseSpecifier(
8118 clang::SourceRange(), is_virtual, base_of_class,
8119 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8120 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8121 clang::SourceLocation());
8122 return nullptr;
8123}
8124
8125void ClangASTContext::DeleteBaseClassSpecifiers(
8126 clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes) {
8127 for (unsigned i = 0; i < num_base_classes; ++i) {
8128 delete base_classes[i];
8129 base_classes[i] = nullptr;
8130 }
8131}
8132
8133bool ClangASTContext::SetBaseClassesForClassType(
8134 lldb::opaque_compiler_type_t type,
8135 clang::CXXBaseSpecifier const *const *base_classes,
8136 unsigned num_base_classes) {
8137 if (type) {
8138 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8139 if (cxx_record_decl) {
8140 cxx_record_decl->setBases(base_classes, num_base_classes);
8141 return true;
8142 }
8143 }
8144 return false;
8145}
8146
8147bool ClangASTContext::SetObjCSuperClass(
8148 const CompilerType &type, const CompilerType &superclass_clang_type) {
8149 ClangASTContext *ast =
8150 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8151 if (!ast)
8152 return false;
8153 clang::ASTContext *clang_ast = ast->getASTContext();
8154
8155 if (type && superclass_clang_type.IsValid() &&
8156 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8157 clang::ObjCInterfaceDecl *class_interface_decl =
8158 GetAsObjCInterfaceDecl(type);
8159 clang::ObjCInterfaceDecl *super_interface_decl =
8160 GetAsObjCInterfaceDecl(superclass_clang_type);
8161 if (class_interface_decl && super_interface_decl) {
8162 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8163 clang_ast->getObjCInterfaceType(super_interface_decl)));
8164 return true;
8165 }
8166 }
8167 return false;
8168}
8169
8170bool ClangASTContext::AddObjCClassProperty(
8171 const CompilerType &type, const char *property_name,
8172 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8173 const char *property_setter_name, const char *property_getter_name,
8174 uint32_t property_attributes, ClangASTMetadata *metadata) {
8175 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8176 property_name[0] == '\0')
8177 return false;
8178 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8179 if (!ast)
8180 return false;
8181 clang::ASTContext *clang_ast = ast->getASTContext();
8182
8183 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8184
8185 if (class_interface_decl) {
8186 CompilerType property_clang_type_to_access;
8187
8188 if (property_clang_type.IsValid())
8189 property_clang_type_to_access = property_clang_type;
8190 else if (ivar_decl)
8191 property_clang_type_to_access =
8192 CompilerType(clang_ast, ivar_decl->getType());
8193
8194 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8195 clang::TypeSourceInfo *prop_type_source;
8196 if (ivar_decl)
8197 prop_type_source =
8198 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8199 else
8200 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8201 ClangUtil::GetQualType(property_clang_type));
8202
8203 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8204 *clang_ast, class_interface_decl,
8205 clang::SourceLocation(), // Source Location
8206 &clang_ast->Idents.get(property_name),
8207 clang::SourceLocation(), // Source Location for AT
8208 clang::SourceLocation(), // Source location for (
8209 ivar_decl ? ivar_decl->getType()
8210 : ClangUtil::GetQualType(property_clang_type),
8211 prop_type_source);
8212
8213 if (property_decl) {
8214 if (metadata)
8215 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8216
8217 class_interface_decl->addDecl(property_decl);
8218
8219 clang::Selector setter_sel, getter_sel;
8220
8221 if (property_setter_name != nullptr) {
8222 std::string property_setter_no_colon(
8223 property_setter_name, strlen(property_setter_name) - 1);
8224 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008225 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008226 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8227 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8228 std::string setter_sel_string("set");
8229 setter_sel_string.push_back(::toupper(property_name[0]));
8230 setter_sel_string.append(&property_name[1]);
8231 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008232 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008233 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8234 }
8235 property_decl->setSetterName(setter_sel);
8236 property_decl->setPropertyAttributes(
8237 clang::ObjCPropertyDecl::OBJC_PR_setter);
8238
8239 if (property_getter_name != nullptr) {
8240 clang::IdentifierInfo *getter_ident =
8241 &clang_ast->Idents.get(property_getter_name);
8242 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8243 } else {
8244 clang::IdentifierInfo *getter_ident =
8245 &clang_ast->Idents.get(property_name);
8246 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8247 }
8248 property_decl->setGetterName(getter_sel);
8249 property_decl->setPropertyAttributes(
8250 clang::ObjCPropertyDecl::OBJC_PR_getter);
8251
8252 if (ivar_decl)
8253 property_decl->setPropertyIvarDecl(ivar_decl);
8254
8255 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8256 property_decl->setPropertyAttributes(
8257 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8258 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8259 property_decl->setPropertyAttributes(
8260 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8261 if (property_attributes & DW_APPLE_PROPERTY_assign)
8262 property_decl->setPropertyAttributes(
8263 clang::ObjCPropertyDecl::OBJC_PR_assign);
8264 if (property_attributes & DW_APPLE_PROPERTY_retain)
8265 property_decl->setPropertyAttributes(
8266 clang::ObjCPropertyDecl::OBJC_PR_retain);
8267 if (property_attributes & DW_APPLE_PROPERTY_copy)
8268 property_decl->setPropertyAttributes(
8269 clang::ObjCPropertyDecl::OBJC_PR_copy);
8270 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8271 property_decl->setPropertyAttributes(
8272 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8273 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8274 property_decl->setPropertyAttributes(
8275 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8276 if (property_attributes &
8277 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8278 property_decl->setPropertyAttributes(
8279 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8280 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8281 property_decl->setPropertyAttributes(
8282 clang::ObjCPropertyDecl::OBJC_PR_class);
8283
8284 const bool isInstance =
8285 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8286
8287 if (!getter_sel.isNull() &&
8288 !(isInstance
8289 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8290 : class_interface_decl->lookupClassMethod(getter_sel))) {
8291 const bool isVariadic = false;
8292 const bool isSynthesized = false;
8293 const bool isImplicitlyDeclared = true;
8294 const bool isDefined = false;
8295 const clang::ObjCMethodDecl::ImplementationControl impControl =
8296 clang::ObjCMethodDecl::None;
8297 const bool HasRelatedResultType = false;
8298
8299 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8300 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8301 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8302 nullptr, class_interface_decl, isInstance, isVariadic,
8303 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8304 HasRelatedResultType);
8305
8306 if (getter && metadata)
8307 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8308
8309 if (getter) {
8310 getter->setMethodParams(*clang_ast,
8311 llvm::ArrayRef<clang::ParmVarDecl *>(),
8312 llvm::ArrayRef<clang::SourceLocation>());
8313
8314 class_interface_decl->addDecl(getter);
8315 }
8316 }
8317
8318 if (!setter_sel.isNull() &&
8319 !(isInstance
8320 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8321 : class_interface_decl->lookupClassMethod(setter_sel))) {
8322 clang::QualType result_type = clang_ast->VoidTy;
8323 const bool isVariadic = false;
8324 const bool isSynthesized = false;
8325 const bool isImplicitlyDeclared = true;
8326 const bool isDefined = false;
8327 const clang::ObjCMethodDecl::ImplementationControl impControl =
8328 clang::ObjCMethodDecl::None;
8329 const bool HasRelatedResultType = false;
8330
8331 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8332 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8333 setter_sel, result_type, nullptr, class_interface_decl,
8334 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8335 isDefined, impControl, HasRelatedResultType);
8336
8337 if (setter && metadata)
8338 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8339
8340 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8341
8342 params.push_back(clang::ParmVarDecl::Create(
8343 *clang_ast, setter, clang::SourceLocation(),
8344 clang::SourceLocation(),
8345 nullptr, // anonymous
8346 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8347 clang::SC_Auto, nullptr));
8348
8349 if (setter) {
8350 setter->setMethodParams(
8351 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8352 llvm::ArrayRef<clang::SourceLocation>());
8353
8354 class_interface_decl->addDecl(setter);
8355 }
8356 }
8357
8358 return true;
8359 }
8360 }
8361 }
8362 return false;
8363}
8364
8365bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8366 bool check_superclass) {
8367 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8368 if (class_interface_decl)
8369 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8370 return false;
8371}
8372
8373clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8374 const CompilerType &type,
8375 const char *name, // the full symbol name as seen in the symbol table
8376 // (lldb::opaque_compiler_type_t type, "-[NString
8377 // stringWithCString:]")
8378 const CompilerType &method_clang_type, lldb::AccessType access,
8379 bool is_artificial, bool is_variadic) {
8380 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008381 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008382
Kate Stoneb9c1b512016-09-06 20:57:50 +00008383 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8384
8385 if (class_interface_decl == nullptr)
8386 return nullptr;
8387 ClangASTContext *lldb_ast =
8388 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8389 if (lldb_ast == nullptr)
8390 return nullptr;
8391 clang::ASTContext *ast = lldb_ast->getASTContext();
8392
8393 const char *selector_start = ::strchr(name, ' ');
8394 if (selector_start == nullptr)
8395 return nullptr;
8396
8397 selector_start++;
8398 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8399
8400 size_t len = 0;
8401 const char *start;
8402 // printf ("name = '%s'\n", name);
8403
8404 unsigned num_selectors_with_args = 0;
8405 for (start = selector_start; start && *start != '\0' && *start != ']';
8406 start += len) {
8407 len = ::strcspn(start, ":]");
8408 bool has_arg = (start[len] == ':');
8409 if (has_arg)
8410 ++num_selectors_with_args;
8411 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8412 if (has_arg)
8413 len += 1;
8414 }
8415
8416 if (selector_idents.size() == 0)
8417 return nullptr;
8418
8419 clang::Selector method_selector = ast->Selectors.getSelector(
8420 num_selectors_with_args ? selector_idents.size() : 0,
8421 selector_idents.data());
8422
8423 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8424
8425 // Populate the method decl with parameter decls
8426 const clang::Type *method_type(method_qual_type.getTypePtr());
8427
8428 if (method_type == nullptr)
8429 return nullptr;
8430
8431 const clang::FunctionProtoType *method_function_prototype(
8432 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8433
8434 if (!method_function_prototype)
8435 return nullptr;
8436
8437 bool is_synthesized = false;
8438 bool is_defined = false;
8439 clang::ObjCMethodDecl::ImplementationControl imp_control =
8440 clang::ObjCMethodDecl::None;
8441
8442 const unsigned num_args = method_function_prototype->getNumParams();
8443
8444 if (num_args != num_selectors_with_args)
8445 return nullptr; // some debug information is corrupt. We are not going to
8446 // deal with it.
8447
8448 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8449 *ast,
8450 clang::SourceLocation(), // beginLoc,
8451 clang::SourceLocation(), // endLoc,
8452 method_selector, method_function_prototype->getReturnType(),
8453 nullptr, // TypeSourceInfo *ResultTInfo,
8454 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8455 ClangUtil::GetQualType(type)),
8456 name[0] == '-', is_variadic, is_synthesized,
8457 true, // is_implicitly_declared; we force this to true because we don't
8458 // have source locations
8459 is_defined, imp_control, false /*has_related_result_type*/);
8460
8461 if (objc_method_decl == nullptr)
8462 return nullptr;
8463
8464 if (num_args > 0) {
8465 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8466
8467 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8468 params.push_back(clang::ParmVarDecl::Create(
8469 *ast, objc_method_decl, clang::SourceLocation(),
8470 clang::SourceLocation(),
8471 nullptr, // anonymous
8472 method_function_prototype->getParamType(param_index), nullptr,
8473 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008474 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008475
Kate Stoneb9c1b512016-09-06 20:57:50 +00008476 objc_method_decl->setMethodParams(
8477 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8478 llvm::ArrayRef<clang::SourceLocation>());
8479 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008480
Kate Stoneb9c1b512016-09-06 20:57:50 +00008481 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008482
Greg Claytond8d4a572015-08-11 21:38:15 +00008483#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008484 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008485#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008486
8487 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008488}
8489
Kate Stoneb9c1b512016-09-06 20:57:50 +00008490bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8491 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008492 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008493
Kate Stoneb9c1b512016-09-06 20:57:50 +00008494 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008495
Kate Stoneb9c1b512016-09-06 20:57:50 +00008496 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8497 switch (type_class) {
8498 case clang::Type::Record: {
8499 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8500 if (cxx_record_decl)
8501 return cxx_record_decl->hasExternalLexicalStorage() ||
8502 cxx_record_decl->hasExternalVisibleStorage();
8503 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008504
Kate Stoneb9c1b512016-09-06 20:57:50 +00008505 case clang::Type::Enum: {
8506 clang::EnumDecl *enum_decl =
8507 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8508 if (enum_decl)
8509 return enum_decl->hasExternalLexicalStorage() ||
8510 enum_decl->hasExternalVisibleStorage();
8511 } break;
8512
8513 case clang::Type::ObjCObject:
8514 case clang::Type::ObjCInterface: {
8515 const clang::ObjCObjectType *objc_class_type =
8516 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8517 assert(objc_class_type);
8518 if (objc_class_type) {
8519 clang::ObjCInterfaceDecl *class_interface_decl =
8520 objc_class_type->getInterface();
8521
8522 if (class_interface_decl)
8523 return class_interface_decl->hasExternalLexicalStorage() ||
8524 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008525 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008526 } break;
8527
8528 case clang::Type::Typedef:
8529 return GetHasExternalStorage(CompilerType(
8530 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8531 ->getDecl()
8532 ->getUnderlyingType()
8533 .getAsOpaquePtr()));
8534
8535 case clang::Type::Auto:
8536 return GetHasExternalStorage(CompilerType(
8537 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8538 ->getDeducedType()
8539 .getAsOpaquePtr()));
8540
8541 case clang::Type::Elaborated:
8542 return GetHasExternalStorage(CompilerType(
8543 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8544 ->getNamedType()
8545 .getAsOpaquePtr()));
8546
8547 case clang::Type::Paren:
8548 return GetHasExternalStorage(CompilerType(
8549 type.GetTypeSystem(),
8550 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8551
8552 default:
8553 break;
8554 }
8555 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008556}
8557
Kate Stoneb9c1b512016-09-06 20:57:50 +00008558bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8559 bool has_extern) {
8560 if (!type)
8561 return false;
8562
8563 clang::QualType qual_type(GetCanonicalQualType(type));
8564
8565 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8566 switch (type_class) {
8567 case clang::Type::Record: {
8568 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8569 if (cxx_record_decl) {
8570 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8571 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8572 return true;
8573 }
8574 } break;
8575
8576 case clang::Type::Enum: {
8577 clang::EnumDecl *enum_decl =
8578 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8579 if (enum_decl) {
8580 enum_decl->setHasExternalLexicalStorage(has_extern);
8581 enum_decl->setHasExternalVisibleStorage(has_extern);
8582 return true;
8583 }
8584 } break;
8585
8586 case clang::Type::ObjCObject:
8587 case clang::Type::ObjCInterface: {
8588 const clang::ObjCObjectType *objc_class_type =
8589 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8590 assert(objc_class_type);
8591 if (objc_class_type) {
8592 clang::ObjCInterfaceDecl *class_interface_decl =
8593 objc_class_type->getInterface();
8594
8595 if (class_interface_decl) {
8596 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8597 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8598 return true;
8599 }
8600 }
8601 } break;
8602
8603 case clang::Type::Typedef:
8604 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8605 ->getDecl()
8606 ->getUnderlyingType()
8607 .getAsOpaquePtr(),
8608 has_extern);
8609
8610 case clang::Type::Auto:
8611 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8612 ->getDeducedType()
8613 .getAsOpaquePtr(),
8614 has_extern);
8615
8616 case clang::Type::Elaborated:
8617 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8618 ->getNamedType()
8619 .getAsOpaquePtr(),
8620 has_extern);
8621
8622 case clang::Type::Paren:
8623 return SetHasExternalStorage(
8624 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8625 has_extern);
8626
8627 default:
8628 break;
8629 }
8630 return false;
8631}
Greg Claytond8d4a572015-08-11 21:38:15 +00008632
8633#pragma mark TagDecl
8634
Kate Stoneb9c1b512016-09-06 20:57:50 +00008635bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8636 clang::QualType qual_type(ClangUtil::GetQualType(type));
8637 if (!qual_type.isNull()) {
8638 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8639 if (tag_type) {
8640 clang::TagDecl *tag_decl = tag_type->getDecl();
8641 if (tag_decl) {
8642 tag_decl->startDefinition();
8643 return true;
8644 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008645 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008646
8647 const clang::ObjCObjectType *object_type =
8648 qual_type->getAs<clang::ObjCObjectType>();
8649 if (object_type) {
8650 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8651 if (interface_decl) {
8652 interface_decl->startDefinition();
8653 return true;
8654 }
8655 }
8656 }
8657 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008658}
8659
Kate Stoneb9c1b512016-09-06 20:57:50 +00008660bool ClangASTContext::CompleteTagDeclarationDefinition(
8661 const CompilerType &type) {
8662 clang::QualType qual_type(ClangUtil::GetQualType(type));
8663 if (!qual_type.isNull()) {
8664 // Make sure we use the same methodology as
Adrian Prantl05097242018-04-30 16:49:04 +00008665 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end
8666 // the definition. Previously we were calling
Kate Stoneb9c1b512016-09-06 20:57:50 +00008667 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8668 if (tag_type) {
8669 clang::TagDecl *tag_decl = tag_type->getDecl();
8670 if (tag_decl) {
8671 clang::CXXRecordDecl *cxx_record_decl =
8672 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8673
8674 if (cxx_record_decl) {
8675 if (!cxx_record_decl->isCompleteDefinition())
8676 cxx_record_decl->completeDefinition();
8677 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8678 cxx_record_decl->setHasExternalLexicalStorage(false);
8679 cxx_record_decl->setHasExternalVisibleStorage(false);
8680 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008681 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008682 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008683 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008684
8685 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8686
8687 if (enutype) {
8688 clang::EnumDecl *enum_decl = enutype->getDecl();
8689
8690 if (enum_decl) {
8691 if (!enum_decl->isCompleteDefinition()) {
8692 ClangASTContext *lldb_ast =
8693 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8694 if (lldb_ast == nullptr)
8695 return false;
8696 clang::ASTContext *ast = lldb_ast->getASTContext();
8697
8698 /// TODO This really needs to be fixed.
8699
8700 QualType integer_type(enum_decl->getIntegerType());
8701 if (!integer_type.isNull()) {
8702 unsigned NumPositiveBits = 1;
8703 unsigned NumNegativeBits = 0;
8704
8705 clang::QualType promotion_qual_type;
8706 // If the enum integer type is less than an integer in bit width,
8707 // then we must promote it to an integer size.
8708 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8709 ast->getTypeSize(ast->IntTy)) {
8710 if (enum_decl->getIntegerType()->isSignedIntegerType())
8711 promotion_qual_type = ast->IntTy;
8712 else
8713 promotion_qual_type = ast->UnsignedIntTy;
8714 } else
8715 promotion_qual_type = enum_decl->getIntegerType();
8716
8717 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8718 promotion_qual_type, NumPositiveBits,
8719 NumNegativeBits);
8720 }
8721 }
8722 return true;
8723 }
8724 }
8725 }
8726 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008727}
8728
Kate Stoneb9c1b512016-09-06 20:57:50 +00008729bool ClangASTContext::AddEnumerationValueToEnumerationType(
8730 lldb::opaque_compiler_type_t type,
8731 const CompilerType &enumerator_clang_type, const Declaration &decl,
8732 const char *name, int64_t enum_value, uint32_t enum_value_bit_size) {
8733 if (type && enumerator_clang_type.IsValid() && name && name[0]) {
8734 clang::QualType enum_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008735
Kate Stoneb9c1b512016-09-06 20:57:50 +00008736 bool is_signed = false;
8737 enumerator_clang_type.IsIntegerType(is_signed);
Greg Claytond8d4a572015-08-11 21:38:15 +00008738 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Kate Stoneb9c1b512016-09-06 20:57:50 +00008739 if (clang_type) {
8740 const clang::EnumType *enutype =
8741 llvm::dyn_cast<clang::EnumType>(clang_type);
8742
8743 if (enutype) {
8744 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8745 enum_llvm_apsint = enum_value;
8746 clang::EnumConstantDecl *enumerator_decl =
8747 clang::EnumConstantDecl::Create(
8748 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8749 name ? &getASTContext()->Idents.get(name)
8750 : nullptr, // Identifier
8751 ClangUtil::GetQualType(enumerator_clang_type),
8752 nullptr, enum_llvm_apsint);
8753
8754 if (enumerator_decl) {
8755 enutype->getDecl()->addDecl(enumerator_decl);
8756
8757#ifdef LLDB_CONFIGURATION_DEBUG
8758 VerifyDecl(enumerator_decl);
8759#endif
8760
8761 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008762 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008763 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008764 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008765 }
8766 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008767}
8768
Greg Claytona1e5dc82015-08-11 22:53:00 +00008769CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00008770ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8771 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8772 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8773 if (clang_type) {
8774 const clang::EnumType *enutype =
8775 llvm::dyn_cast<clang::EnumType>(clang_type);
8776 if (enutype) {
8777 clang::EnumDecl *enum_decl = enutype->getDecl();
8778 if (enum_decl)
8779 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008780 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008781 }
8782 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008783}
8784
Kate Stoneb9c1b512016-09-06 20:57:50 +00008785CompilerType
8786ClangASTContext::CreateMemberPointerType(const CompilerType &type,
8787 const CompilerType &pointee_type) {
8788 if (type && pointee_type.IsValid() &&
8789 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8790 ClangASTContext *ast =
8791 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8792 if (!ast)
8793 return CompilerType();
8794 return CompilerType(ast->getASTContext(),
8795 ast->getASTContext()->getMemberPointerType(
8796 ClangUtil::GetQualType(pointee_type),
8797 ClangUtil::GetQualType(type).getTypePtr()));
8798 }
8799 return CompilerType();
8800}
Greg Claytond8d4a572015-08-11 21:38:15 +00008801
8802size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00008803ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
8804 const char *s, uint8_t *dst,
8805 size_t dst_size) {
8806 if (type) {
8807 clang::QualType qual_type(GetCanonicalQualType(type));
8808 uint32_t count = 0;
8809 bool is_complex = false;
8810 if (IsFloatingPointType(type, count, is_complex)) {
8811 // TODO: handle complex and vector types
8812 if (count != 1)
8813 return false;
8814
8815 llvm::StringRef s_sref(s);
8816 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
8817 s_sref);
8818
8819 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
8820 const uint64_t byte_size = bit_size / 8;
8821 if (dst_size >= byte_size) {
8822 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
8823 llvm::NextPowerOf2(byte_size) * 8);
Zachary Turner97206d52017-05-12 04:51:55 +00008824 lldb_private::Status get_data_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008825 if (scalar.GetAsMemoryData(dst, byte_size,
8826 lldb_private::endian::InlHostByteOrder(),
8827 get_data_error))
8828 return byte_size;
8829 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008830 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008831 }
8832 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00008833}
8834
Greg Claytond8d4a572015-08-11 21:38:15 +00008835//----------------------------------------------------------------------
8836// Dumping types
8837//----------------------------------------------------------------------
8838#define DEPTH_INCREMENT 2
8839
Kate Stoneb9c1b512016-09-06 20:57:50 +00008840void ClangASTContext::DumpValue(
8841 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00008842 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008843 lldb::offset_t data_byte_offset, size_t data_byte_size,
8844 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
8845 bool show_summary, bool verbose, uint32_t depth) {
8846 if (!type)
8847 return;
8848
8849 clang::QualType qual_type(GetQualType(type));
8850 switch (qual_type->getTypeClass()) {
8851 case clang::Type::Record:
8852 if (GetCompleteType(type)) {
8853 const clang::RecordType *record_type =
8854 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8855 const clang::RecordDecl *record_decl = record_type->getDecl();
8856 assert(record_decl);
8857 uint32_t field_bit_offset = 0;
8858 uint32_t field_byte_offset = 0;
8859 const clang::ASTRecordLayout &record_layout =
8860 getASTContext()->getASTRecordLayout(record_decl);
8861 uint32_t child_idx = 0;
8862
8863 const clang::CXXRecordDecl *cxx_record_decl =
8864 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8865 if (cxx_record_decl) {
8866 // We might have base classes to print out first
8867 clang::CXXRecordDecl::base_class_const_iterator base_class,
8868 base_class_end;
8869 for (base_class = cxx_record_decl->bases_begin(),
8870 base_class_end = cxx_record_decl->bases_end();
8871 base_class != base_class_end; ++base_class) {
8872 const clang::CXXRecordDecl *base_class_decl =
8873 llvm::cast<clang::CXXRecordDecl>(
8874 base_class->getType()->getAs<clang::RecordType>()->getDecl());
8875
8876 // Skip empty base classes
8877 if (verbose == false &&
8878 ClangASTContext::RecordHasFields(base_class_decl) == false)
8879 continue;
8880
8881 if (base_class->isVirtual())
8882 field_bit_offset =
8883 record_layout.getVBaseClassOffset(base_class_decl)
8884 .getQuantity() *
8885 8;
8886 else
8887 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
8888 .getQuantity() *
8889 8;
8890 field_byte_offset = field_bit_offset / 8;
8891 assert(field_bit_offset % 8 == 0);
8892 if (child_idx == 0)
8893 s->PutChar('{');
8894 else
8895 s->PutChar(',');
8896
8897 clang::QualType base_class_qual_type = base_class->getType();
8898 std::string base_class_type_name(base_class_qual_type.getAsString());
8899
8900 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00008901 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
8902 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008903
8904 clang::TypeInfo base_class_type_info =
8905 getASTContext()->getTypeInfo(base_class_qual_type);
8906
8907 // Dump the value of the member
8908 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
8909 base_clang_type.DumpValue(
8910 exe_ctx,
8911 s, // Stream to dump to
8912 base_clang_type
8913 .GetFormat(), // The format with which to display the member
8914 data, // Data buffer containing all bytes for this type
8915 data_byte_offset + field_byte_offset, // Offset into "data" where
8916 // to grab value from
8917 base_class_type_info.Width / 8, // Size of this type in bytes
8918 0, // Bitfield bit size
8919 0, // Bitfield bit offset
8920 show_types, // Boolean indicating if we should show the variable
8921 // types
8922 show_summary, // Boolean indicating if we should show a summary
8923 // for the current type
8924 verbose, // Verbose output?
8925 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8926 // children
8927
8928 ++child_idx;
8929 }
8930 }
8931 uint32_t field_idx = 0;
8932 clang::RecordDecl::field_iterator field, field_end;
8933 for (field = record_decl->field_begin(),
8934 field_end = record_decl->field_end();
8935 field != field_end; ++field, ++field_idx, ++child_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00008936 // Print the starting squiggly bracket (if this is the first member) or
8937 // comma (for member 2 and beyond) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008938 if (child_idx == 0)
8939 s->PutChar('{');
8940 else
8941 s->PutChar(',');
8942
8943 // Indent
8944 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8945
8946 clang::QualType field_type = field->getType();
8947 // Print the member type if requested
Adrian Prantl05097242018-04-30 16:49:04 +00008948 // Figure out the type byte size (field_type_info.first) and alignment
8949 // (field_type_info.second) from the AST context.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008950 clang::TypeInfo field_type_info =
8951 getASTContext()->getTypeInfo(field_type);
8952 assert(field_idx < record_layout.getFieldCount());
8953 // Figure out the field offset within the current struct/union/class
8954 // type
8955 field_bit_offset = record_layout.getFieldOffset(field_idx);
8956 field_byte_offset = field_bit_offset / 8;
8957 uint32_t field_bitfield_bit_size = 0;
8958 uint32_t field_bitfield_bit_offset = 0;
8959 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
8960 field_bitfield_bit_size))
8961 field_bitfield_bit_offset = field_bit_offset % 8;
8962
8963 if (show_types) {
8964 std::string field_type_name(field_type.getAsString());
8965 if (field_bitfield_bit_size > 0)
8966 s->Printf("(%s:%u) ", field_type_name.c_str(),
8967 field_bitfield_bit_size);
8968 else
8969 s->Printf("(%s) ", field_type_name.c_str());
8970 }
8971 // Print the member name and equal sign
8972 s->Printf("%s = ", field->getNameAsString().c_str());
8973
8974 // Dump the value of the member
8975 CompilerType field_clang_type(getASTContext(), field_type);
8976 field_clang_type.DumpValue(
8977 exe_ctx,
8978 s, // Stream to dump to
8979 field_clang_type
8980 .GetFormat(), // The format with which to display the member
8981 data, // Data buffer containing all bytes for this type
8982 data_byte_offset + field_byte_offset, // Offset into "data" where to
8983 // grab value from
8984 field_type_info.Width / 8, // Size of this type in bytes
8985 field_bitfield_bit_size, // Bitfield bit size
8986 field_bitfield_bit_offset, // Bitfield bit offset
8987 show_types, // Boolean indicating if we should show the variable
8988 // types
8989 show_summary, // Boolean indicating if we should show a summary for
8990 // the current type
8991 verbose, // Verbose output?
8992 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8993 // children
8994 }
8995
8996 // Indent the trailing squiggly bracket
8997 if (child_idx > 0)
8998 s->Printf("\n%*s}", depth, "");
8999 }
9000 return;
9001
9002 case clang::Type::Enum:
9003 if (GetCompleteType(type)) {
9004 const clang::EnumType *enutype =
9005 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9006 const clang::EnumDecl *enum_decl = enutype->getDecl();
9007 assert(enum_decl);
9008 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9009 lldb::offset_t offset = data_byte_offset;
9010 const int64_t enum_value = data.GetMaxU64Bitfield(
9011 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9012 for (enum_pos = enum_decl->enumerator_begin(),
9013 enum_end_pos = enum_decl->enumerator_end();
9014 enum_pos != enum_end_pos; ++enum_pos) {
9015 if (enum_pos->getInitVal() == enum_value) {
9016 s->Printf("%s", enum_pos->getNameAsString().c_str());
9017 return;
9018 }
9019 }
Adrian Prantl05097242018-04-30 16:49:04 +00009020 // If we have gotten here we didn't get find the enumerator in the enum
9021 // decl, so just print the integer.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009022 s->Printf("%" PRIi64, enum_value);
9023 }
9024 return;
9025
9026 case clang::Type::ConstantArray: {
9027 const clang::ConstantArrayType *array =
9028 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9029 bool is_array_of_characters = false;
9030 clang::QualType element_qual_type = array->getElementType();
9031
9032 const clang::Type *canonical_type =
9033 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9034 if (canonical_type)
9035 is_array_of_characters = canonical_type->isCharType();
9036
9037 const uint64_t element_count = array->getSize().getLimitedValue();
9038
9039 clang::TypeInfo field_type_info =
9040 getASTContext()->getTypeInfo(element_qual_type);
9041
9042 uint32_t element_idx = 0;
9043 uint32_t element_offset = 0;
9044 uint64_t element_byte_size = field_type_info.Width / 8;
9045 uint32_t element_stride = element_byte_size;
9046
9047 if (is_array_of_characters) {
9048 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009049 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9050 element_byte_size, element_count, UINT32_MAX,
9051 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009052 s->PutChar('"');
9053 return;
9054 } else {
9055 CompilerType element_clang_type(getASTContext(), element_qual_type);
9056 lldb::Format element_format = element_clang_type.GetFormat();
9057
9058 for (element_idx = 0; element_idx < element_count; ++element_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009059 // Print the starting squiggly bracket (if this is the first member) or
9060 // comman (for member 2 and beyong) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009061 if (element_idx == 0)
9062 s->PutChar('{');
9063 else
9064 s->PutChar(',');
9065
9066 // Indent and print the index
9067 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9068
9069 // Figure out the field offset within the current struct/union/class
9070 // type
9071 element_offset = element_idx * element_stride;
9072
9073 // Dump the value of the member
9074 element_clang_type.DumpValue(
9075 exe_ctx,
9076 s, // Stream to dump to
9077 element_format, // The format with which to display the element
9078 data, // Data buffer containing all bytes for this type
9079 data_byte_offset +
9080 element_offset, // Offset into "data" where to grab value from
9081 element_byte_size, // Size of this type in bytes
9082 0, // Bitfield bit size
9083 0, // Bitfield bit offset
9084 show_types, // Boolean indicating if we should show the variable
9085 // types
9086 show_summary, // Boolean indicating if we should show a summary for
9087 // the current type
9088 verbose, // Verbose output?
9089 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9090 // children
9091 }
9092
9093 // Indent the trailing squiggly bracket
9094 if (element_idx > 0)
9095 s->Printf("\n%*s}", depth, "");
9096 }
9097 }
9098 return;
9099
9100 case clang::Type::Typedef: {
9101 clang::QualType typedef_qual_type =
9102 llvm::cast<clang::TypedefType>(qual_type)
9103 ->getDecl()
9104 ->getUnderlyingType();
9105
9106 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9107 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9108 clang::TypeInfo typedef_type_info =
9109 getASTContext()->getTypeInfo(typedef_qual_type);
9110 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9111
9112 return typedef_clang_type.DumpValue(
9113 exe_ctx,
9114 s, // Stream to dump to
9115 typedef_format, // The format with which to display the element
9116 data, // Data buffer containing all bytes for this type
9117 data_byte_offset, // Offset into "data" where to grab value from
9118 typedef_byte_size, // Size of this type in bytes
9119 bitfield_bit_size, // Bitfield bit size
9120 bitfield_bit_offset, // Bitfield bit offset
9121 show_types, // Boolean indicating if we should show the variable types
9122 show_summary, // Boolean indicating if we should show a summary for the
9123 // current type
9124 verbose, // Verbose output?
9125 depth); // Scope depth for any types that have children
9126 } break;
9127
9128 case clang::Type::Auto: {
9129 clang::QualType elaborated_qual_type =
9130 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9131 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9132 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9133 clang::TypeInfo elaborated_type_info =
9134 getASTContext()->getTypeInfo(elaborated_qual_type);
9135 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9136
9137 return elaborated_clang_type.DumpValue(
9138 exe_ctx,
9139 s, // Stream to dump to
9140 elaborated_format, // The format with which to display the element
9141 data, // Data buffer containing all bytes for this type
9142 data_byte_offset, // Offset into "data" where to grab value from
9143 elaborated_byte_size, // Size of this type in bytes
9144 bitfield_bit_size, // Bitfield bit size
9145 bitfield_bit_offset, // Bitfield bit offset
9146 show_types, // Boolean indicating if we should show the variable types
9147 show_summary, // Boolean indicating if we should show a summary for the
9148 // current type
9149 verbose, // Verbose output?
9150 depth); // Scope depth for any types that have children
9151 } break;
9152
9153 case clang::Type::Elaborated: {
9154 clang::QualType elaborated_qual_type =
9155 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9156 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9157 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9158 clang::TypeInfo elaborated_type_info =
9159 getASTContext()->getTypeInfo(elaborated_qual_type);
9160 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9161
9162 return elaborated_clang_type.DumpValue(
9163 exe_ctx,
9164 s, // Stream to dump to
9165 elaborated_format, // The format with which to display the element
9166 data, // Data buffer containing all bytes for this type
9167 data_byte_offset, // Offset into "data" where to grab value from
9168 elaborated_byte_size, // Size of this type in bytes
9169 bitfield_bit_size, // Bitfield bit size
9170 bitfield_bit_offset, // Bitfield bit offset
9171 show_types, // Boolean indicating if we should show the variable types
9172 show_summary, // Boolean indicating if we should show a summary for the
9173 // current type
9174 verbose, // Verbose output?
9175 depth); // Scope depth for any types that have children
9176 } break;
9177
9178 case clang::Type::Paren: {
9179 clang::QualType desugar_qual_type =
9180 llvm::cast<clang::ParenType>(qual_type)->desugar();
9181 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9182
9183 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9184 clang::TypeInfo desugar_type_info =
9185 getASTContext()->getTypeInfo(desugar_qual_type);
9186 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9187
9188 return desugar_clang_type.DumpValue(
9189 exe_ctx,
9190 s, // Stream to dump to
9191 desugar_format, // The format with which to display the element
9192 data, // Data buffer containing all bytes for this type
9193 data_byte_offset, // Offset into "data" where to grab value from
9194 desugar_byte_size, // Size of this type in bytes
9195 bitfield_bit_size, // Bitfield bit size
9196 bitfield_bit_offset, // Bitfield bit offset
9197 show_types, // Boolean indicating if we should show the variable types
9198 show_summary, // Boolean indicating if we should show a summary for the
9199 // current type
9200 verbose, // Verbose output?
9201 depth); // Scope depth for any types that have children
9202 } break;
9203
9204 default:
9205 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009206 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9207 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9208 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009209
9210 if (show_summary)
9211 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9212 break;
9213 }
9214}
9215
9216bool ClangASTContext::DumpTypeValue(
9217 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009218 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9219 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009220 ExecutionContextScope *exe_scope) {
9221 if (!type)
9222 return false;
9223 if (IsAggregateType(type)) {
9224 return false;
9225 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009226 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009227
9228 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9229 switch (type_class) {
9230 case clang::Type::Typedef: {
9231 clang::QualType typedef_qual_type =
9232 llvm::cast<clang::TypedefType>(qual_type)
9233 ->getDecl()
9234 ->getUnderlyingType();
9235 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9236 if (format == eFormatDefault)
9237 format = typedef_clang_type.GetFormat();
9238 clang::TypeInfo typedef_type_info =
9239 getASTContext()->getTypeInfo(typedef_qual_type);
9240 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9241
9242 return typedef_clang_type.DumpTypeValue(
9243 s,
9244 format, // The format with which to display the element
9245 data, // Data buffer containing all bytes for this type
9246 byte_offset, // Offset into "data" where to grab value from
9247 typedef_byte_size, // Size of this type in bytes
9248 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9249 // treat as a bitfield
9250 bitfield_bit_offset, // Offset in bits of a bitfield value if
9251 // bitfield_bit_size != 0
9252 exe_scope);
9253 } break;
9254
9255 case clang::Type::Enum:
Adrian Prantl05097242018-04-30 16:49:04 +00009256 // If our format is enum or default, show the enumeration value as its
9257 // enumeration string value, else just display it as requested.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009258 if ((format == eFormatEnum || format == eFormatDefault) &&
9259 GetCompleteType(type)) {
9260 const clang::EnumType *enutype =
9261 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9262 const clang::EnumDecl *enum_decl = enutype->getDecl();
9263 assert(enum_decl);
9264 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9265 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9266 lldb::offset_t offset = byte_offset;
9267 if (is_signed) {
9268 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9269 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9270 for (enum_pos = enum_decl->enumerator_begin(),
9271 enum_end_pos = enum_decl->enumerator_end();
9272 enum_pos != enum_end_pos; ++enum_pos) {
9273 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009274 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009275 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009276 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009277 }
9278 // If we have gotten here we didn't get find the enumerator in the
9279 // enum decl, so just print the integer.
9280 s->Printf("%" PRIi64, enum_svalue);
9281 } else {
9282 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9283 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9284 for (enum_pos = enum_decl->enumerator_begin(),
9285 enum_end_pos = enum_decl->enumerator_end();
9286 enum_pos != enum_end_pos; ++enum_pos) {
9287 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009288 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009289 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009290 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009291 }
9292 // If we have gotten here we didn't get find the enumerator in the
9293 // enum decl, so just print the integer.
9294 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009295 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009296 return true;
9297 }
9298 // format was not enum, just fall through and dump the value as
9299 // requested....
9300 LLVM_FALLTHROUGH;
9301
9302 default:
9303 // We are down to a scalar type that we just need to display.
9304 {
9305 uint32_t item_count = 1;
9306 // A few formats, we might need to modify our size and count for
9307 // depending
9308 // on how we are trying to display the value...
9309 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009310 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009311 case eFormatBoolean:
9312 case eFormatBinary:
9313 case eFormatComplex:
9314 case eFormatCString: // NULL terminated C strings
9315 case eFormatDecimal:
9316 case eFormatEnum:
9317 case eFormatHex:
9318 case eFormatHexUppercase:
9319 case eFormatFloat:
9320 case eFormatOctal:
9321 case eFormatOSType:
9322 case eFormatUnsigned:
9323 case eFormatPointer:
9324 case eFormatVectorOfChar:
9325 case eFormatVectorOfSInt8:
9326 case eFormatVectorOfUInt8:
9327 case eFormatVectorOfSInt16:
9328 case eFormatVectorOfUInt16:
9329 case eFormatVectorOfSInt32:
9330 case eFormatVectorOfUInt32:
9331 case eFormatVectorOfSInt64:
9332 case eFormatVectorOfUInt64:
9333 case eFormatVectorOfFloat32:
9334 case eFormatVectorOfFloat64:
9335 case eFormatVectorOfUInt128:
9336 break;
9337
9338 case eFormatChar:
9339 case eFormatCharPrintable:
9340 case eFormatCharArray:
9341 case eFormatBytes:
9342 case eFormatBytesWithASCII:
9343 item_count = byte_size;
9344 byte_size = 1;
9345 break;
9346
9347 case eFormatUnicode16:
9348 item_count = byte_size / 2;
9349 byte_size = 2;
9350 break;
9351
9352 case eFormatUnicode32:
9353 item_count = byte_size / 4;
9354 byte_size = 4;
9355 break;
9356 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009357 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9358 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9359 bitfield_bit_size, bitfield_bit_offset,
9360 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009361 }
9362 break;
9363 }
9364 }
9365 return 0;
9366}
9367
9368void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9369 ExecutionContext *exe_ctx, Stream *s,
9370 const lldb_private::DataExtractor &data,
9371 lldb::offset_t data_byte_offset,
9372 size_t data_byte_size) {
9373 uint32_t length = 0;
9374 if (IsCStringType(type, length)) {
9375 if (exe_ctx) {
9376 Process *process = exe_ctx->GetProcessPtr();
9377 if (process) {
9378 lldb::offset_t offset = data_byte_offset;
9379 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9380 std::vector<uint8_t> buf;
9381 if (length > 0)
9382 buf.resize(length);
9383 else
9384 buf.resize(256);
9385
Zachary Turner29cb8682017-03-03 20:57:05 +00009386 DataExtractor cstr_data(&buf.front(), buf.size(),
9387 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009388 buf.back() = '\0';
9389 size_t bytes_read;
9390 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009391 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009392 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9393 buf.size(), error)) > 0) {
9394 const size_t len = strlen((const char *)&buf.front());
9395 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009396 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009397 if (total_cstr_len == 0)
9398 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009399 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9400 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009401 total_cstr_len += len;
9402 if (len < buf.size())
9403 break;
9404 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009405 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009406 if (total_cstr_len > 0)
9407 s->PutChar('"');
9408 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009409 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009410 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009411}
9412
Kate Stoneb9c1b512016-09-06 20:57:50 +00009413void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9414 StreamFile s(stdout, false);
9415 DumpTypeDescription(type, &s);
9416 ClangASTMetadata *metadata =
9417 ClangASTContext::GetMetadata(getASTContext(), type);
9418 if (metadata) {
9419 metadata->Dump(&s);
9420 }
9421}
Greg Claytond8d4a572015-08-11 21:38:15 +00009422
Kate Stoneb9c1b512016-09-06 20:57:50 +00009423void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9424 Stream *s) {
9425 if (type) {
9426 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009427
Kate Stoneb9c1b512016-09-06 20:57:50 +00009428 llvm::SmallVector<char, 1024> buf;
9429 llvm::raw_svector_ostream llvm_ostrm(buf);
9430
9431 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9432 switch (type_class) {
9433 case clang::Type::ObjCObject:
9434 case clang::Type::ObjCInterface: {
9435 GetCompleteType(type);
9436
9437 const clang::ObjCObjectType *objc_class_type =
9438 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9439 assert(objc_class_type);
9440 if (objc_class_type) {
9441 clang::ObjCInterfaceDecl *class_interface_decl =
9442 objc_class_type->getInterface();
9443 if (class_interface_decl) {
9444 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9445 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009446 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009447 }
9448 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009449
Kate Stoneb9c1b512016-09-06 20:57:50 +00009450 case clang::Type::Typedef: {
9451 const clang::TypedefType *typedef_type =
9452 qual_type->getAs<clang::TypedefType>();
9453 if (typedef_type) {
9454 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9455 std::string clang_typedef_name(
9456 typedef_decl->getQualifiedNameAsString());
9457 if (!clang_typedef_name.empty()) {
9458 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009459 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009460 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009461 }
9462 } break;
9463
9464 case clang::Type::Auto:
9465 CompilerType(getASTContext(),
9466 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9467 .DumpTypeDescription(s);
9468 return;
9469
9470 case clang::Type::Elaborated:
9471 CompilerType(getASTContext(),
9472 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9473 .DumpTypeDescription(s);
9474 return;
9475
9476 case clang::Type::Paren:
9477 CompilerType(getASTContext(),
9478 llvm::cast<clang::ParenType>(qual_type)->desugar())
9479 .DumpTypeDescription(s);
9480 return;
9481
9482 case clang::Type::Record: {
9483 GetCompleteType(type);
9484
9485 const clang::RecordType *record_type =
9486 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9487 const clang::RecordDecl *record_decl = record_type->getDecl();
9488 const clang::CXXRecordDecl *cxx_record_decl =
9489 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9490
9491 if (cxx_record_decl)
9492 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9493 s->GetIndentLevel());
9494 else
9495 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9496 s->GetIndentLevel());
9497 } break;
9498
9499 default: {
9500 const clang::TagType *tag_type =
9501 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9502 if (tag_type) {
9503 clang::TagDecl *tag_decl = tag_type->getDecl();
9504 if (tag_decl)
9505 tag_decl->print(llvm_ostrm, 0);
9506 } else {
9507 std::string clang_type_name(qual_type.getAsString());
9508 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009509 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009510 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009511 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009512 }
9513
Kate Stoneb9c1b512016-09-06 20:57:50 +00009514 if (buf.size() > 0) {
9515 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009516 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009517 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009518}
9519
Kate Stoneb9c1b512016-09-06 20:57:50 +00009520void ClangASTContext::DumpTypeName(const CompilerType &type) {
9521 if (ClangUtil::IsClangType(type)) {
9522 clang::QualType qual_type(
9523 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9524
9525 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9526 switch (type_class) {
9527 case clang::Type::Record: {
9528 const clang::CXXRecordDecl *cxx_record_decl =
9529 qual_type->getAsCXXRecordDecl();
9530 if (cxx_record_decl)
9531 printf("class %s", cxx_record_decl->getName().str().c_str());
9532 } break;
9533
9534 case clang::Type::Enum: {
9535 clang::EnumDecl *enum_decl =
9536 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9537 if (enum_decl) {
9538 printf("enum %s", enum_decl->getName().str().c_str());
9539 }
9540 } break;
9541
9542 case clang::Type::ObjCObject:
9543 case clang::Type::ObjCInterface: {
9544 const clang::ObjCObjectType *objc_class_type =
9545 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9546 if (objc_class_type) {
9547 clang::ObjCInterfaceDecl *class_interface_decl =
9548 objc_class_type->getInterface();
Adrian Prantl05097242018-04-30 16:49:04 +00009549 // We currently can't complete objective C types through the newly
9550 // added ASTContext because it only supports TagDecl objects right
9551 // now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00009552 if (class_interface_decl)
9553 printf("@class %s", class_interface_decl->getName().str().c_str());
9554 }
9555 } break;
9556
9557 case clang::Type::Typedef:
9558 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9559 ->getDecl()
9560 ->getName()
9561 .str()
9562 .c_str());
9563 break;
9564
9565 case clang::Type::Auto:
9566 printf("auto ");
9567 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9568 llvm::cast<clang::AutoType>(qual_type)
9569 ->getDeducedType()
9570 .getAsOpaquePtr()));
9571
9572 case clang::Type::Elaborated:
9573 printf("elaborated ");
9574 return DumpTypeName(CompilerType(
9575 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9576 ->getNamedType()
9577 .getAsOpaquePtr()));
9578
9579 case clang::Type::Paren:
9580 printf("paren ");
9581 return DumpTypeName(CompilerType(
9582 type.GetTypeSystem(),
9583 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9584
9585 default:
9586 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9587 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009588 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009589 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009590}
9591
Kate Stoneb9c1b512016-09-06 20:57:50 +00009592clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9593 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9594 const char *parent_name, int tag_decl_kind,
9595 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9596 if (template_param_infos.IsValid()) {
9597 std::string template_basename(parent_name);
9598 template_basename.erase(template_basename.find('<'));
9599
9600 return CreateClassTemplateDecl(decl_ctx, access_type,
9601 template_basename.c_str(), tag_decl_kind,
9602 template_param_infos);
9603 }
9604 return NULL;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009605}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009606
Kate Stoneb9c1b512016-09-06 20:57:50 +00009607void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9608 ClangASTContext *ast = (ClangASTContext *)baton;
9609 SymbolFile *sym_file = ast->GetSymbolFile();
9610 if (sym_file) {
9611 CompilerType clang_type = GetTypeForDecl(decl);
9612 if (clang_type)
9613 sym_file->CompleteType(clang_type);
9614 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009615}
9616
Kate Stoneb9c1b512016-09-06 20:57:50 +00009617void ClangASTContext::CompleteObjCInterfaceDecl(
9618 void *baton, clang::ObjCInterfaceDecl *decl) {
9619 ClangASTContext *ast = (ClangASTContext *)baton;
9620 SymbolFile *sym_file = ast->GetSymbolFile();
9621 if (sym_file) {
9622 CompilerType clang_type = GetTypeForDecl(decl);
9623 if (clang_type)
9624 sym_file->CompleteType(clang_type);
9625 }
Zachary Turner42dff792016-04-15 00:21:26 +00009626}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009627
Kate Stoneb9c1b512016-09-06 20:57:50 +00009628DWARFASTParser *ClangASTContext::GetDWARFParser() {
9629 if (!m_dwarf_ast_parser_ap)
9630 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9631 return m_dwarf_ast_parser_ap.get();
9632}
9633
9634PDBASTParser *ClangASTContext::GetPDBParser() {
9635 if (!m_pdb_ast_parser_ap)
9636 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9637 return m_pdb_ast_parser_ap.get();
9638}
9639
9640bool ClangASTContext::LayoutRecordType(
9641 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9642 uint64_t &alignment,
9643 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9644 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9645 &base_offsets,
9646 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9647 &vbase_offsets) {
9648 ClangASTContext *ast = (ClangASTContext *)baton;
9649 DWARFASTParserClang *dwarf_ast_parser =
9650 (DWARFASTParserClang *)ast->GetDWARFParser();
9651 return dwarf_ast_parser->GetClangASTImporter().LayoutRecordType(
9652 record_decl, bit_size, alignment, field_offsets, base_offsets,
9653 vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009654}
9655
Greg Clayton99558cc42015-08-24 23:46:31 +00009656//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009657// CompilerDecl override functions
9658//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009659
Kate Stoneb9c1b512016-09-06 20:57:50 +00009660ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9661 if (opaque_decl) {
9662 clang::NamedDecl *nd =
9663 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9664 if (nd != nullptr)
9665 return ConstString(nd->getDeclName().getAsString());
9666 }
9667 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009668}
9669
Kate Stoneb9c1b512016-09-06 20:57:50 +00009670ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9671 if (opaque_decl) {
9672 clang::NamedDecl *nd =
9673 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9674 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9675 clang::MangleContext *mc = getMangleContext();
9676 if (mc && mc->shouldMangleCXXName(nd)) {
9677 llvm::SmallVector<char, 1024> buf;
9678 llvm::raw_svector_ostream llvm_ostrm(buf);
9679 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9680 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9681 Ctor_Complete, llvm_ostrm);
9682 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9683 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9684 Dtor_Complete, llvm_ostrm);
9685 } else {
9686 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009687 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009688 if (buf.size() > 0)
9689 return ConstString(buf.data(), buf.size());
9690 }
Greg Claytonfe689042015-11-10 17:47:04 +00009691 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009692 }
9693 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009694}
9695
Kate Stoneb9c1b512016-09-06 20:57:50 +00009696CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9697 if (opaque_decl)
9698 return CompilerDeclContext(this,
9699 ((clang::Decl *)opaque_decl)->getDeclContext());
9700 else
9701 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009702}
9703
Kate Stoneb9c1b512016-09-06 20:57:50 +00009704CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9705 if (clang::FunctionDecl *func_decl =
9706 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9707 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9708 if (clang::ObjCMethodDecl *objc_method =
9709 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9710 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9711 else
Greg Claytonfe689042015-11-10 17:47:04 +00009712 return CompilerType();
9713}
9714
Kate Stoneb9c1b512016-09-06 20:57:50 +00009715size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9716 if (clang::FunctionDecl *func_decl =
9717 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9718 return func_decl->param_size();
9719 if (clang::ObjCMethodDecl *objc_method =
9720 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9721 return objc_method->param_size();
9722 else
9723 return 0;
9724}
9725
9726CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9727 size_t idx) {
9728 if (clang::FunctionDecl *func_decl =
9729 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9730 if (idx < func_decl->param_size()) {
9731 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9732 if (var_decl)
9733 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9734 }
9735 } else if (clang::ObjCMethodDecl *objc_method =
9736 llvm::dyn_cast<clang::ObjCMethodDecl>(
9737 (clang::Decl *)opaque_decl)) {
9738 if (idx < objc_method->param_size())
9739 return CompilerType(
9740 this,
9741 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9742 }
9743 return CompilerType();
9744}
9745
Paul Hermand628cbb2015-09-15 23:44:17 +00009746//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00009747// CompilerDeclContext functions
9748//----------------------------------------------------------------------
9749
Kate Stoneb9c1b512016-09-06 20:57:50 +00009750std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9751 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9752 std::vector<CompilerDecl> found_decls;
9753 if (opaque_decl_ctx) {
9754 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9755 std::set<DeclContext *> searched;
9756 std::multimap<DeclContext *, DeclContext *> search_queue;
9757 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009758
Kate Stoneb9c1b512016-09-06 20:57:50 +00009759 for (clang::DeclContext *decl_context = root_decl_ctx;
9760 decl_context != nullptr && found_decls.empty();
9761 decl_context = decl_context->getParent()) {
9762 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +00009763
Kate Stoneb9c1b512016-09-06 20:57:50 +00009764 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9765 it++) {
9766 if (!searched.insert(it->second).second)
9767 continue;
9768 symbol_file->ParseDeclsForContext(
9769 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +00009770
Kate Stoneb9c1b512016-09-06 20:57:50 +00009771 for (clang::Decl *child : it->second->decls()) {
9772 if (clang::UsingDirectiveDecl *ud =
9773 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9774 if (ignore_using_decls)
9775 continue;
9776 clang::DeclContext *from = ud->getCommonAncestor();
9777 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9778 search_queue.insert(
9779 std::make_pair(from, ud->getNominatedNamespace()));
9780 } else if (clang::UsingDecl *ud =
9781 llvm::dyn_cast<clang::UsingDecl>(child)) {
9782 if (ignore_using_decls)
9783 continue;
9784 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9785 clang::Decl *target = usd->getTargetDecl();
9786 if (clang::NamedDecl *nd =
9787 llvm::dyn_cast<clang::NamedDecl>(target)) {
9788 IdentifierInfo *ii = nd->getIdentifier();
9789 if (ii != nullptr &&
9790 ii->getName().equals(name.AsCString(nullptr)))
9791 found_decls.push_back(CompilerDecl(this, nd));
9792 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009793 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009794 } else if (clang::NamedDecl *nd =
9795 llvm::dyn_cast<clang::NamedDecl>(child)) {
9796 IdentifierInfo *ii = nd->getIdentifier();
9797 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9798 found_decls.push_back(CompilerDecl(this, nd));
9799 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009800 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009801 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009802 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009803 }
9804 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +00009805}
9806
Dawn Perchikb5925782015-12-12 19:31:41 +00009807// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009808// and return the number of levels it took to find it, or
Adrian Prantl05097242018-04-30 16:49:04 +00009809// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
9810// declaration, its name and/or type, if set, will be used to check that the
9811// decl found in the scope is a match.
Dawn Perchikb5925782015-12-12 19:31:41 +00009812//
Kate Stoneb9c1b512016-09-06 20:57:50 +00009813// The optional name is required by languages (like C++) to handle using
Adrian Prantl05097242018-04-30 16:49:04 +00009814// declarations like:
Dawn Perchikb5925782015-12-12 19:31:41 +00009815//
9816// void poo();
9817// namespace ns {
9818// void foo();
9819// void goo();
9820// }
9821// void bar() {
9822// using ns::foo;
9823// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
9824// // LLDB_INVALID_DECL_LEVEL for 'goo'.
9825// }
9826//
9827// The optional type is useful in the case that there's a specific overload
9828// that we're looking for that might otherwise be shadowed, like:
9829//
9830// void foo(int);
9831// namespace ns {
9832// void foo();
9833// }
9834// void bar() {
9835// using ns::foo;
9836// // CountDeclLevels returns 0 for { 'foo', void() },
9837// // 1 for { 'foo', void(int) }, and
9838// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
9839// }
9840//
9841// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +00009842// function at file scope will return the same level as a function at global
Adrian Prantl05097242018-04-30 16:49:04 +00009843// scope. Ideally we'd like to treat the file scope as an additional scope just
9844// below the global scope. More work needs to be done to recognise that, if
9845// the decl we're trying to look up is static, we should compare its source
9846// file with that of the current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009847uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
9848 clang::DeclContext *child_decl_ctx,
9849 ConstString *child_name,
9850 CompilerType *child_type) {
9851 if (frame_decl_ctx) {
9852 std::set<DeclContext *> searched;
9853 std::multimap<DeclContext *, DeclContext *> search_queue;
9854 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +00009855
Kate Stoneb9c1b512016-09-06 20:57:50 +00009856 // Get the lookup scope for the decl we're trying to find.
9857 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +00009858
Kate Stoneb9c1b512016-09-06 20:57:50 +00009859 // Look for it in our scope's decl context and its parents.
9860 uint32_t level = 0;
9861 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
9862 decl_ctx = decl_ctx->getParent()) {
9863 if (!decl_ctx->isLookupContext())
9864 continue;
9865 if (decl_ctx == parent_decl_ctx)
9866 // Found it!
9867 return level;
9868 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
9869 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
9870 it++) {
9871 if (searched.find(it->second) != searched.end())
9872 continue;
9873
9874 // Currently DWARF has one shared translation unit for all Decls at top
Adrian Prantl05097242018-04-30 16:49:04 +00009875 // level, so this would erroneously find using statements anywhere. So
9876 // don't look at the top-level translation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009877 // TODO fix this and add a testcase that depends on it.
9878
9879 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
9880 continue;
9881
9882 searched.insert(it->second);
9883 symbol_file->ParseDeclsForContext(
9884 CompilerDeclContext(this, it->second));
9885
9886 for (clang::Decl *child : it->second->decls()) {
9887 if (clang::UsingDirectiveDecl *ud =
9888 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9889 clang::DeclContext *ns = ud->getNominatedNamespace();
9890 if (ns == parent_decl_ctx)
9891 // Found it!
9892 return level;
9893 clang::DeclContext *from = ud->getCommonAncestor();
9894 if (searched.find(ns) == searched.end())
9895 search_queue.insert(std::make_pair(from, ns));
9896 } else if (child_name) {
9897 if (clang::UsingDecl *ud =
9898 llvm::dyn_cast<clang::UsingDecl>(child)) {
9899 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9900 clang::Decl *target = usd->getTargetDecl();
9901 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
9902 if (!nd)
9903 continue;
9904 // Check names.
9905 IdentifierInfo *ii = nd->getIdentifier();
9906 if (ii == nullptr ||
9907 !ii->getName().equals(child_name->AsCString(nullptr)))
9908 continue;
9909 // Check types, if one was provided.
9910 if (child_type) {
9911 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
9912 if (!AreTypesSame(clang_type, *child_type,
9913 /*ignore_qualifiers=*/true))
9914 continue;
9915 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009916 // Found it!
9917 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009918 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009919 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009920 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009921 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009922 }
9923 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +00009924 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009925 }
9926 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +00009927}
9928
Kate Stoneb9c1b512016-09-06 20:57:50 +00009929bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
9930 if (opaque_decl_ctx)
9931 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
9932 else
Greg Clayton99558cc42015-08-24 23:46:31 +00009933 return false;
9934}
9935
Kate Stoneb9c1b512016-09-06 20:57:50 +00009936ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
9937 if (opaque_decl_ctx) {
9938 clang::NamedDecl *named_decl =
9939 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9940 if (named_decl)
9941 return ConstString(named_decl->getName());
9942 }
9943 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +00009944}
9945
Kate Stoneb9c1b512016-09-06 20:57:50 +00009946ConstString
9947ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
9948 if (opaque_decl_ctx) {
9949 clang::NamedDecl *named_decl =
9950 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9951 if (named_decl)
9952 return ConstString(
9953 llvm::StringRef(named_decl->getQualifiedNameAsString()));
9954 }
9955 return ConstString();
9956}
9957
9958bool ClangASTContext::DeclContextIsClassMethod(
9959 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
9960 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
9961 if (opaque_decl_ctx) {
9962 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9963 if (ObjCMethodDecl *objc_method =
9964 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
9965 if (is_instance_method_ptr)
9966 *is_instance_method_ptr = objc_method->isInstanceMethod();
9967 if (language_ptr)
9968 *language_ptr = eLanguageTypeObjC;
9969 if (language_object_name_ptr)
9970 language_object_name_ptr->SetCString("self");
9971 return true;
9972 } else if (CXXMethodDecl *cxx_method =
9973 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
9974 if (is_instance_method_ptr)
9975 *is_instance_method_ptr = cxx_method->isInstance();
9976 if (language_ptr)
9977 *language_ptr = eLanguageTypeC_plus_plus;
9978 if (language_object_name_ptr)
9979 language_object_name_ptr->SetCString("this");
9980 return true;
9981 } else if (clang::FunctionDecl *function_decl =
9982 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
9983 ClangASTMetadata *metadata =
9984 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
9985 if (metadata && metadata->HasObjectPtr()) {
9986 if (is_instance_method_ptr)
9987 *is_instance_method_ptr = true;
9988 if (language_ptr)
9989 *language_ptr = eLanguageTypeObjC;
9990 if (language_object_name_ptr)
9991 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
9992 return true;
9993 }
9994 }
9995 }
9996 return false;
9997}
9998
9999clang::DeclContext *
10000ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10001 if (dc.IsClang())
10002 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10003 return nullptr;
10004}
Greg Clayton99558cc42015-08-24 23:46:31 +000010005
10006ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010007ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10008 if (dc.IsClang())
10009 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10010 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10011 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010012}
10013
10014CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010015ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10016 if (dc.IsClang())
10017 return llvm::dyn_cast<clang::CXXMethodDecl>(
10018 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10019 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010020}
10021
10022clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010023ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10024 if (dc.IsClang())
10025 return llvm::dyn_cast<clang::FunctionDecl>(
10026 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10027 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010028}
10029
10030clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010031ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10032 if (dc.IsClang())
10033 return llvm::dyn_cast<clang::NamespaceDecl>(
10034 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10035 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010036}
10037
10038ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010039ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10040 const void *object) {
10041 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10042 if (ast)
10043 return ClangASTContext::GetMetadata(ast, object);
10044 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010045}
10046
10047clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010048ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10049 ClangASTContext *ast =
10050 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10051 if (ast)
10052 return ast->getASTContext();
10053 return nullptr;
10054}
10055
10056ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10057 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10058 m_target_wp(target.shared_from_this()),
10059 m_persistent_variables(new ClangPersistentVariables) {}
10060
10061UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010062 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010063 Expression::ResultType desired_type,
10064 const EvaluateExpressionOptions &options) {
10065 TargetSP target_sp = m_target_wp.lock();
10066 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010067 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010068
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010069 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010070 desired_type, options);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010071}
10072
Kate Stoneb9c1b512016-09-06 20:57:50 +000010073FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10074 const CompilerType &return_type, const Address &function_address,
10075 const ValueList &arg_value_list, const char *name) {
10076 TargetSP target_sp = m_target_wp.lock();
10077 if (!target_sp)
10078 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010079
Kate Stoneb9c1b512016-09-06 20:57:50 +000010080 Process *process = target_sp->GetProcessSP().get();
10081 if (!process)
10082 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010083
Kate Stoneb9c1b512016-09-06 20:57:50 +000010084 return new ClangFunctionCaller(*process, return_type, function_address,
10085 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010086}
10087
10088UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010089ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10090 const char *name) {
10091 TargetSP target_sp = m_target_wp.lock();
10092 if (!target_sp)
10093 return nullptr;
10094
10095 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010096}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010097
10098PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010099ClangASTContextForExpressions::GetPersistentExpressionState() {
10100 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010101}
Sean Callanan68e44232017-09-28 20:20:25 +000010102
10103clang::ExternalASTMerger &
10104ClangASTContextForExpressions::GetMergerUnchecked() {
Eugene Zemtsova9d928c2017-09-29 03:15:08 +000010105 lldbassert(m_scratch_ast_source_ap != nullptr);
Sean Callanan68e44232017-09-28 20:20:25 +000010106 return m_scratch_ast_source_ap->GetMergerUnchecked();
10107}