blob: 87d347ff594e2aca6fccd23c807cc178d99555f1 [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
Greg Clayton8cf05932010-07-22 18:30:50 +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
4937 case clang::BuiltinType::Half:
4938 case clang::BuiltinType::Float:
Ted Woodward4355c7c2017-09-20 19:16:53 +00004939 case clang::BuiltinType::Float16:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004940 case clang::BuiltinType::Float128:
4941 case clang::BuiltinType::Double:
4942 case clang::BuiltinType::LongDouble:
4943 return lldb::eEncodingIEEE754;
4944
4945 case clang::BuiltinType::ObjCClass:
4946 case clang::BuiltinType::ObjCId:
4947 case clang::BuiltinType::ObjCSel:
4948 return lldb::eEncodingUint;
4949
4950 case clang::BuiltinType::NullPtr:
4951 return lldb::eEncodingUint;
4952
4953 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4954 case clang::BuiltinType::Kind::BoundMember:
4955 case clang::BuiltinType::Kind::BuiltinFn:
4956 case clang::BuiltinType::Kind::Dependent:
4957 case clang::BuiltinType::Kind::OCLClkEvent:
4958 case clang::BuiltinType::Kind::OCLEvent:
4959 case clang::BuiltinType::Kind::OCLImage1dRO:
4960 case clang::BuiltinType::Kind::OCLImage1dWO:
4961 case clang::BuiltinType::Kind::OCLImage1dRW:
4962 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
4963 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
4964 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
4965 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
4966 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
4967 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
4968 case clang::BuiltinType::Kind::OCLImage2dRO:
4969 case clang::BuiltinType::Kind::OCLImage2dWO:
4970 case clang::BuiltinType::Kind::OCLImage2dRW:
4971 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
4972 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
4973 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
4974 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
4975 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
4976 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
4977 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
4978 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
4979 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
4980 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
4981 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
4982 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
4983 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
4984 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
4985 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
4986 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
4987 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
4988 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
4989 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
4990 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
4991 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
4992 case clang::BuiltinType::Kind::OCLImage3dRO:
4993 case clang::BuiltinType::Kind::OCLImage3dWO:
4994 case clang::BuiltinType::Kind::OCLImage3dRW:
4995 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004996 case clang::BuiltinType::Kind::OCLReserveID:
4997 case clang::BuiltinType::Kind::OCLSampler:
4998 case clang::BuiltinType::Kind::OMPArraySection:
4999 case clang::BuiltinType::Kind::Overload:
5000 case clang::BuiltinType::Kind::PseudoObject:
5001 case clang::BuiltinType::Kind::UnknownAny:
5002 break;
5003 }
5004 break;
Adrian Prantl05097242018-04-30 16:49:04 +00005005 // All pointer types are represented as unsigned integer encodings. We may
5006 // nee to add a eEncodingPointer if we ever need to know the difference
Kate Stoneb9c1b512016-09-06 20:57:50 +00005007 case clang::Type::ObjCObjectPointer:
5008 case clang::Type::BlockPointer:
5009 case clang::Type::Pointer:
5010 case clang::Type::LValueReference:
5011 case clang::Type::RValueReference:
5012 case clang::Type::MemberPointer:
5013 return lldb::eEncodingUint;
5014 case clang::Type::Complex: {
5015 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5016 if (qual_type->isComplexType())
5017 encoding = lldb::eEncodingIEEE754;
5018 else {
5019 const clang::ComplexType *complex_type =
5020 qual_type->getAsComplexIntegerType();
5021 if (complex_type)
5022 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5023 .GetEncoding(count);
5024 else
5025 encoding = lldb::eEncodingSint;
5026 }
5027 count = 2;
5028 return encoding;
5029 }
5030
5031 case clang::Type::ObjCInterface:
5032 break;
5033 case clang::Type::Record:
5034 break;
5035 case clang::Type::Enum:
5036 return lldb::eEncodingSint;
5037 case clang::Type::Typedef:
5038 return CompilerType(getASTContext(),
5039 llvm::cast<clang::TypedefType>(qual_type)
5040 ->getDecl()
5041 ->getUnderlyingType())
5042 .GetEncoding(count);
5043
5044 case clang::Type::Auto:
5045 return CompilerType(
5046 getASTContext(),
5047 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5048 .GetEncoding(count);
5049
5050 case clang::Type::Elaborated:
5051 return CompilerType(
5052 getASTContext(),
5053 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5054 .GetEncoding(count);
5055
5056 case clang::Type::Paren:
5057 return CompilerType(getASTContext(),
5058 llvm::cast<clang::ParenType>(qual_type)->desugar())
5059 .GetEncoding(count);
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005060 case clang::Type::TypeOfExpr:
5061 return CompilerType(getASTContext(),
5062 llvm::cast<clang::TypeOfExprType>(qual_type)
5063 ->getUnderlyingExpr()
5064 ->getType())
5065 .GetEncoding(count);
5066 case clang::Type::TypeOf:
5067 return CompilerType(
5068 getASTContext(),
5069 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5070 .GetEncoding(count);
5071 case clang::Type::Decltype:
5072 return CompilerType(
5073 getASTContext(),
5074 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5075 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005076 case clang::Type::DependentSizedArray:
5077 case clang::Type::DependentSizedExtVector:
5078 case clang::Type::UnresolvedUsing:
5079 case clang::Type::Attributed:
5080 case clang::Type::TemplateTypeParm:
5081 case clang::Type::SubstTemplateTypeParm:
5082 case clang::Type::SubstTemplateTypeParmPack:
5083 case clang::Type::InjectedClassName:
5084 case clang::Type::DependentName:
5085 case clang::Type::DependentTemplateSpecialization:
5086 case clang::Type::PackExpansion:
5087 case clang::Type::ObjCObject:
5088
Kate Stoneb9c1b512016-09-06 20:57:50 +00005089 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005090 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005091 case clang::Type::Atomic:
5092 case clang::Type::Adjusted:
5093 case clang::Type::Pipe:
5094 break;
5095
5096 // pointer type decayed from an array or function type.
5097 case clang::Type::Decayed:
5098 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005099 case clang::Type::ObjCTypeParam:
5100 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005101
5102 case clang::Type::DependentAddressSpace:
5103 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005104 }
5105 count = 0;
5106 return lldb::eEncodingInvalid;
5107}
5108
5109lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5110 if (!type)
5111 return lldb::eFormatDefault;
5112
5113 clang::QualType qual_type(GetCanonicalQualType(type));
5114
5115 switch (qual_type->getTypeClass()) {
5116 case clang::Type::UnaryTransform:
5117 break;
5118
5119 case clang::Type::FunctionNoProto:
5120 case clang::Type::FunctionProto:
5121 break;
5122
5123 case clang::Type::IncompleteArray:
5124 case clang::Type::VariableArray:
5125 break;
5126
5127 case clang::Type::ConstantArray:
5128 return lldb::eFormatVoid; // no value
5129
5130 case clang::Type::ExtVector:
5131 case clang::Type::Vector:
5132 break;
5133
5134 case clang::Type::Builtin:
5135 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5136 // default: assert(0 && "Unknown builtin type!");
5137 case clang::BuiltinType::UnknownAny:
5138 case clang::BuiltinType::Void:
5139 case clang::BuiltinType::BoundMember:
5140 break;
5141
5142 case clang::BuiltinType::Bool:
5143 return lldb::eFormatBoolean;
5144 case clang::BuiltinType::Char_S:
5145 case clang::BuiltinType::SChar:
5146 case clang::BuiltinType::WChar_S:
5147 case clang::BuiltinType::Char_U:
5148 case clang::BuiltinType::UChar:
5149 case clang::BuiltinType::WChar_U:
5150 return lldb::eFormatChar;
5151 case clang::BuiltinType::Char16:
5152 return lldb::eFormatUnicode16;
5153 case clang::BuiltinType::Char32:
5154 return lldb::eFormatUnicode32;
5155 case clang::BuiltinType::UShort:
5156 return lldb::eFormatUnsigned;
5157 case clang::BuiltinType::Short:
5158 return lldb::eFormatDecimal;
5159 case clang::BuiltinType::UInt:
5160 return lldb::eFormatUnsigned;
5161 case clang::BuiltinType::Int:
5162 return lldb::eFormatDecimal;
5163 case clang::BuiltinType::ULong:
5164 return lldb::eFormatUnsigned;
5165 case clang::BuiltinType::Long:
5166 return lldb::eFormatDecimal;
5167 case clang::BuiltinType::ULongLong:
5168 return lldb::eFormatUnsigned;
5169 case clang::BuiltinType::LongLong:
5170 return lldb::eFormatDecimal;
5171 case clang::BuiltinType::UInt128:
5172 return lldb::eFormatUnsigned;
5173 case clang::BuiltinType::Int128:
5174 return lldb::eFormatDecimal;
5175 case clang::BuiltinType::Half:
5176 case clang::BuiltinType::Float:
5177 case clang::BuiltinType::Double:
5178 case clang::BuiltinType::LongDouble:
5179 return lldb::eFormatFloat;
5180 default:
5181 return lldb::eFormatHex;
5182 }
5183 break;
5184 case clang::Type::ObjCObjectPointer:
5185 return lldb::eFormatHex;
5186 case clang::Type::BlockPointer:
5187 return lldb::eFormatHex;
5188 case clang::Type::Pointer:
5189 return lldb::eFormatHex;
5190 case clang::Type::LValueReference:
5191 case clang::Type::RValueReference:
5192 return lldb::eFormatHex;
5193 case clang::Type::MemberPointer:
5194 break;
5195 case clang::Type::Complex: {
5196 if (qual_type->isComplexType())
5197 return lldb::eFormatComplex;
5198 else
5199 return lldb::eFormatComplexInteger;
5200 }
5201 case clang::Type::ObjCInterface:
5202 break;
5203 case clang::Type::Record:
5204 break;
5205 case clang::Type::Enum:
5206 return lldb::eFormatEnum;
5207 case clang::Type::Typedef:
5208 return CompilerType(getASTContext(),
5209 llvm::cast<clang::TypedefType>(qual_type)
5210 ->getDecl()
5211 ->getUnderlyingType())
5212 .GetFormat();
5213 case clang::Type::Auto:
5214 return CompilerType(getASTContext(),
5215 llvm::cast<clang::AutoType>(qual_type)->desugar())
5216 .GetFormat();
5217 case clang::Type::Paren:
5218 return CompilerType(getASTContext(),
5219 llvm::cast<clang::ParenType>(qual_type)->desugar())
5220 .GetFormat();
5221 case clang::Type::Elaborated:
5222 return CompilerType(
5223 getASTContext(),
5224 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5225 .GetFormat();
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005226 case clang::Type::TypeOfExpr:
5227 return CompilerType(getASTContext(),
5228 llvm::cast<clang::TypeOfExprType>(qual_type)
5229 ->getUnderlyingExpr()
5230 ->getType())
5231 .GetFormat();
5232 case clang::Type::TypeOf:
5233 return CompilerType(
5234 getASTContext(),
5235 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5236 .GetFormat();
5237 case clang::Type::Decltype:
5238 return CompilerType(
5239 getASTContext(),
5240 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5241 .GetFormat();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005242 case clang::Type::DependentSizedArray:
5243 case clang::Type::DependentSizedExtVector:
5244 case clang::Type::UnresolvedUsing:
5245 case clang::Type::Attributed:
5246 case clang::Type::TemplateTypeParm:
5247 case clang::Type::SubstTemplateTypeParm:
5248 case clang::Type::SubstTemplateTypeParmPack:
5249 case clang::Type::InjectedClassName:
5250 case clang::Type::DependentName:
5251 case clang::Type::DependentTemplateSpecialization:
5252 case clang::Type::PackExpansion:
5253 case clang::Type::ObjCObject:
5254
Kate Stoneb9c1b512016-09-06 20:57:50 +00005255 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005256 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005257 case clang::Type::Atomic:
5258 case clang::Type::Adjusted:
5259 case clang::Type::Pipe:
5260 break;
5261
5262 // pointer type decayed from an array or function type.
5263 case clang::Type::Decayed:
5264 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005265 case clang::Type::ObjCTypeParam:
5266 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005267
5268 case clang::Type::DependentAddressSpace:
5269 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005270 }
5271 // We don't know hot to display this type...
5272 return lldb::eFormatBytes;
5273}
5274
5275static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5276 bool check_superclass) {
5277 while (class_interface_decl) {
5278 if (class_interface_decl->ivar_size() > 0)
5279 return true;
5280
5281 if (check_superclass)
5282 class_interface_decl = class_interface_decl->getSuperClass();
5283 else
5284 break;
5285 }
5286 return false;
5287}
5288
5289uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
5290 bool omit_empty_base_classes) {
5291 if (!type)
5292 return 0;
5293
5294 uint32_t num_children = 0;
5295 clang::QualType qual_type(GetQualType(type));
5296 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5297 switch (type_class) {
5298 case clang::Type::Builtin:
5299 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5300 case clang::BuiltinType::ObjCId: // child is Class
5301 case clang::BuiltinType::ObjCClass: // child is Class
5302 num_children = 1;
5303 break;
5304
5305 default:
5306 break;
5307 }
5308 break;
5309
5310 case clang::Type::Complex:
5311 return 0;
5312
5313 case clang::Type::Record:
5314 if (GetCompleteQualType(getASTContext(), qual_type)) {
5315 const clang::RecordType *record_type =
5316 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5317 const clang::RecordDecl *record_decl = record_type->getDecl();
5318 assert(record_decl);
5319 const clang::CXXRecordDecl *cxx_record_decl =
5320 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5321 if (cxx_record_decl) {
5322 if (omit_empty_base_classes) {
Adrian Prantl05097242018-04-30 16:49:04 +00005323 // Check each base classes to see if it or any of its base classes
5324 // contain any fields. This can help limit the noise in variable
5325 // views by not having to show base classes that contain no members.
Kate Stoneb9c1b512016-09-06 20:57:50 +00005326 clang::CXXRecordDecl::base_class_const_iterator base_class,
5327 base_class_end;
5328 for (base_class = cxx_record_decl->bases_begin(),
5329 base_class_end = cxx_record_decl->bases_end();
5330 base_class != base_class_end; ++base_class) {
5331 const clang::CXXRecordDecl *base_class_decl =
5332 llvm::cast<clang::CXXRecordDecl>(
5333 base_class->getType()
5334 ->getAs<clang::RecordType>()
5335 ->getDecl());
5336
5337 // Skip empty base classes
5338 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5339 continue;
5340
5341 num_children++;
5342 }
5343 } else {
5344 // Include all base classes
5345 num_children += cxx_record_decl->getNumBases();
5346 }
5347 }
5348 clang::RecordDecl::field_iterator field, field_end;
5349 for (field = record_decl->field_begin(),
5350 field_end = record_decl->field_end();
5351 field != field_end; ++field)
5352 ++num_children;
5353 }
5354 break;
5355
5356 case clang::Type::ObjCObject:
5357 case clang::Type::ObjCInterface:
5358 if (GetCompleteQualType(getASTContext(), qual_type)) {
5359 const clang::ObjCObjectType *objc_class_type =
5360 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5361 assert(objc_class_type);
5362 if (objc_class_type) {
5363 clang::ObjCInterfaceDecl *class_interface_decl =
5364 objc_class_type->getInterface();
5365
5366 if (class_interface_decl) {
5367
5368 clang::ObjCInterfaceDecl *superclass_interface_decl =
5369 class_interface_decl->getSuperClass();
5370 if (superclass_interface_decl) {
5371 if (omit_empty_base_classes) {
5372 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5373 ++num_children;
5374 } else
5375 ++num_children;
5376 }
5377
5378 num_children += class_interface_decl->ivar_size();
5379 }
5380 }
5381 }
5382 break;
5383
5384 case clang::Type::ObjCObjectPointer: {
5385 const clang::ObjCObjectPointerType *pointer_type =
5386 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5387 clang::QualType pointee_type = pointer_type->getPointeeType();
5388 uint32_t num_pointee_children =
5389 CompilerType(getASTContext(), pointee_type)
5390 .GetNumChildren(omit_empty_base_classes);
5391 // If this type points to a simple type, then it has 1 child
5392 if (num_pointee_children == 0)
5393 num_children = 1;
5394 else
5395 num_children = num_pointee_children;
5396 } break;
5397
5398 case clang::Type::Vector:
5399 case clang::Type::ExtVector:
5400 num_children =
5401 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5402 break;
5403
5404 case clang::Type::ConstantArray:
5405 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5406 ->getSize()
5407 .getLimitedValue();
5408 break;
5409
5410 case clang::Type::Pointer: {
5411 const clang::PointerType *pointer_type =
5412 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5413 clang::QualType pointee_type(pointer_type->getPointeeType());
5414 uint32_t num_pointee_children =
5415 CompilerType(getASTContext(), pointee_type)
5416 .GetNumChildren(omit_empty_base_classes);
5417 if (num_pointee_children == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00005418 // We have a pointer to a pointee type that claims it has no children. We
5419 // will want to look at
Kate Stoneb9c1b512016-09-06 20:57:50 +00005420 num_children = GetNumPointeeChildren(pointee_type);
5421 } else
5422 num_children = num_pointee_children;
5423 } break;
5424
5425 case clang::Type::LValueReference:
5426 case clang::Type::RValueReference: {
5427 const clang::ReferenceType *reference_type =
5428 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5429 clang::QualType pointee_type = reference_type->getPointeeType();
5430 uint32_t num_pointee_children =
5431 CompilerType(getASTContext(), pointee_type)
5432 .GetNumChildren(omit_empty_base_classes);
5433 // If this type points to a simple type, then it has 1 child
5434 if (num_pointee_children == 0)
5435 num_children = 1;
5436 else
5437 num_children = num_pointee_children;
5438 } break;
5439
5440 case clang::Type::Typedef:
5441 num_children =
5442 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5443 ->getDecl()
5444 ->getUnderlyingType())
5445 .GetNumChildren(omit_empty_base_classes);
5446 break;
5447
5448 case clang::Type::Auto:
5449 num_children =
5450 CompilerType(getASTContext(),
5451 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5452 .GetNumChildren(omit_empty_base_classes);
5453 break;
5454
5455 case clang::Type::Elaborated:
5456 num_children =
5457 CompilerType(
5458 getASTContext(),
5459 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5460 .GetNumChildren(omit_empty_base_classes);
5461 break;
5462
5463 case clang::Type::Paren:
5464 num_children =
5465 CompilerType(getASTContext(),
5466 llvm::cast<clang::ParenType>(qual_type)->desugar())
5467 .GetNumChildren(omit_empty_base_classes);
5468 break;
5469 default:
5470 break;
5471 }
5472 return num_children;
5473}
5474
5475CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5476 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005477}
5478
Greg Claytond8d4a572015-08-11 21:38:15 +00005479lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005480ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5481 if (type) {
5482 clang::QualType qual_type(GetQualType(type));
5483 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5484 if (type_class == clang::Type::Builtin) {
5485 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5486 case clang::BuiltinType::Void:
5487 return eBasicTypeVoid;
5488 case clang::BuiltinType::Bool:
5489 return eBasicTypeBool;
5490 case clang::BuiltinType::Char_S:
5491 return eBasicTypeSignedChar;
5492 case clang::BuiltinType::Char_U:
5493 return eBasicTypeUnsignedChar;
5494 case clang::BuiltinType::Char16:
5495 return eBasicTypeChar16;
5496 case clang::BuiltinType::Char32:
5497 return eBasicTypeChar32;
5498 case clang::BuiltinType::UChar:
5499 return eBasicTypeUnsignedChar;
5500 case clang::BuiltinType::SChar:
5501 return eBasicTypeSignedChar;
5502 case clang::BuiltinType::WChar_S:
5503 return eBasicTypeSignedWChar;
5504 case clang::BuiltinType::WChar_U:
5505 return eBasicTypeUnsignedWChar;
5506 case clang::BuiltinType::Short:
5507 return eBasicTypeShort;
5508 case clang::BuiltinType::UShort:
5509 return eBasicTypeUnsignedShort;
5510 case clang::BuiltinType::Int:
5511 return eBasicTypeInt;
5512 case clang::BuiltinType::UInt:
5513 return eBasicTypeUnsignedInt;
5514 case clang::BuiltinType::Long:
5515 return eBasicTypeLong;
5516 case clang::BuiltinType::ULong:
5517 return eBasicTypeUnsignedLong;
5518 case clang::BuiltinType::LongLong:
5519 return eBasicTypeLongLong;
5520 case clang::BuiltinType::ULongLong:
5521 return eBasicTypeUnsignedLongLong;
5522 case clang::BuiltinType::Int128:
5523 return eBasicTypeInt128;
5524 case clang::BuiltinType::UInt128:
5525 return eBasicTypeUnsignedInt128;
5526
5527 case clang::BuiltinType::Half:
5528 return eBasicTypeHalf;
5529 case clang::BuiltinType::Float:
5530 return eBasicTypeFloat;
5531 case clang::BuiltinType::Double:
5532 return eBasicTypeDouble;
5533 case clang::BuiltinType::LongDouble:
5534 return eBasicTypeLongDouble;
5535
5536 case clang::BuiltinType::NullPtr:
5537 return eBasicTypeNullPtr;
5538 case clang::BuiltinType::ObjCId:
5539 return eBasicTypeObjCID;
5540 case clang::BuiltinType::ObjCClass:
5541 return eBasicTypeObjCClass;
5542 case clang::BuiltinType::ObjCSel:
5543 return eBasicTypeObjCSel;
5544 default:
5545 return eBasicTypeOther;
5546 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005547 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005548 }
5549 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005550}
5551
Kate Stoneb9c1b512016-09-06 20:57:50 +00005552void ClangASTContext::ForEachEnumerator(
5553 lldb::opaque_compiler_type_t type,
5554 std::function<bool(const CompilerType &integer_type,
5555 const ConstString &name,
5556 const llvm::APSInt &value)> const &callback) {
5557 const clang::EnumType *enum_type =
5558 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5559 if (enum_type) {
5560 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5561 if (enum_decl) {
5562 CompilerType integer_type(this,
5563 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005564
Kate Stoneb9c1b512016-09-06 20:57:50 +00005565 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5566 for (enum_pos = enum_decl->enumerator_begin(),
5567 enum_end_pos = enum_decl->enumerator_end();
5568 enum_pos != enum_end_pos; ++enum_pos) {
5569 ConstString name(enum_pos->getNameAsString().c_str());
5570 if (!callback(integer_type, name, enum_pos->getInitVal()))
5571 break;
5572 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005573 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005574 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005575}
5576
Greg Claytond8d4a572015-08-11 21:38:15 +00005577#pragma mark Aggregate Types
5578
Kate Stoneb9c1b512016-09-06 20:57:50 +00005579uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5580 if (!type)
5581 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005582
Kate Stoneb9c1b512016-09-06 20:57:50 +00005583 uint32_t count = 0;
5584 clang::QualType qual_type(GetCanonicalQualType(type));
5585 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5586 switch (type_class) {
5587 case clang::Type::Record:
5588 if (GetCompleteType(type)) {
5589 const clang::RecordType *record_type =
5590 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5591 if (record_type) {
5592 clang::RecordDecl *record_decl = record_type->getDecl();
5593 if (record_decl) {
5594 uint32_t field_idx = 0;
5595 clang::RecordDecl::field_iterator field, field_end;
5596 for (field = record_decl->field_begin(),
5597 field_end = record_decl->field_end();
5598 field != field_end; ++field)
5599 ++field_idx;
5600 count = field_idx;
5601 }
5602 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005603 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005604 break;
5605
5606 case clang::Type::Typedef:
5607 count =
5608 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5609 ->getDecl()
5610 ->getUnderlyingType())
5611 .GetNumFields();
5612 break;
5613
5614 case clang::Type::Auto:
5615 count =
5616 CompilerType(getASTContext(),
5617 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5618 .GetNumFields();
5619 break;
5620
5621 case clang::Type::Elaborated:
5622 count = CompilerType(
5623 getASTContext(),
5624 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5625 .GetNumFields();
5626 break;
5627
5628 case clang::Type::Paren:
5629 count = CompilerType(getASTContext(),
5630 llvm::cast<clang::ParenType>(qual_type)->desugar())
5631 .GetNumFields();
5632 break;
5633
Sean Callananf9c622a2016-09-30 18:44:43 +00005634 case clang::Type::ObjCObjectPointer: {
5635 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005636 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005637 const clang::ObjCInterfaceType *objc_interface_type =
5638 objc_class_type->getInterfaceType();
5639 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005640 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5641 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005642 clang::ObjCInterfaceDecl *class_interface_decl =
5643 objc_interface_type->getDecl();
5644 if (class_interface_decl) {
5645 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005646 }
5647 }
5648 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005649 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005650
5651 case clang::Type::ObjCObject:
5652 case clang::Type::ObjCInterface:
5653 if (GetCompleteType(type)) {
5654 const clang::ObjCObjectType *objc_class_type =
5655 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5656 if (objc_class_type) {
5657 clang::ObjCInterfaceDecl *class_interface_decl =
5658 objc_class_type->getInterface();
5659
5660 if (class_interface_decl)
5661 count = class_interface_decl->ivar_size();
5662 }
5663 }
5664 break;
5665
5666 default:
5667 break;
5668 }
5669 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005670}
5671
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005672static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005673GetObjCFieldAtIndex(clang::ASTContext *ast,
5674 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5675 std::string &name, uint64_t *bit_offset_ptr,
5676 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5677 if (class_interface_decl) {
5678 if (idx < (class_interface_decl->ivar_size())) {
5679 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5680 ivar_end = class_interface_decl->ivar_end();
5681 uint32_t ivar_idx = 0;
5682
5683 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5684 ++ivar_pos, ++ivar_idx) {
5685 if (ivar_idx == idx) {
5686 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5687
5688 clang::QualType ivar_qual_type(ivar_decl->getType());
5689
5690 name.assign(ivar_decl->getNameAsString());
5691
5692 if (bit_offset_ptr) {
5693 const clang::ASTRecordLayout &interface_layout =
5694 ast->getASTObjCInterfaceLayout(class_interface_decl);
5695 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5696 }
5697
5698 const bool is_bitfield = ivar_pos->isBitField();
5699
5700 if (bitfield_bit_size_ptr) {
5701 *bitfield_bit_size_ptr = 0;
5702
5703 if (is_bitfield && ast) {
5704 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5705 llvm::APSInt bitfield_apsint;
5706 if (bitfield_bit_size_expr &&
5707 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5708 *ast)) {
5709 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5710 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005711 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005712 }
5713 if (is_bitfield_ptr)
5714 *is_bitfield_ptr = is_bitfield;
5715
5716 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005717 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005718 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005719 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005720 }
5721 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005722}
5723
Kate Stoneb9c1b512016-09-06 20:57:50 +00005724CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5725 size_t idx, std::string &name,
5726 uint64_t *bit_offset_ptr,
5727 uint32_t *bitfield_bit_size_ptr,
5728 bool *is_bitfield_ptr) {
5729 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005730 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005731
5732 clang::QualType qual_type(GetCanonicalQualType(type));
5733 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5734 switch (type_class) {
5735 case clang::Type::Record:
5736 if (GetCompleteType(type)) {
5737 const clang::RecordType *record_type =
5738 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5739 const clang::RecordDecl *record_decl = record_type->getDecl();
5740 uint32_t field_idx = 0;
5741 clang::RecordDecl::field_iterator field, field_end;
5742 for (field = record_decl->field_begin(),
5743 field_end = record_decl->field_end();
5744 field != field_end; ++field, ++field_idx) {
5745 if (idx == field_idx) {
5746 // Print the member type if requested
5747 // Print the member name and equal sign
5748 name.assign(field->getNameAsString());
5749
5750 // Figure out the type byte size (field_type_info.first) and
5751 // alignment (field_type_info.second) from the AST context.
5752 if (bit_offset_ptr) {
5753 const clang::ASTRecordLayout &record_layout =
5754 getASTContext()->getASTRecordLayout(record_decl);
5755 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5756 }
5757
5758 const bool is_bitfield = field->isBitField();
5759
5760 if (bitfield_bit_size_ptr) {
5761 *bitfield_bit_size_ptr = 0;
5762
5763 if (is_bitfield) {
5764 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5765 llvm::APSInt bitfield_apsint;
5766 if (bitfield_bit_size_expr &&
5767 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5768 *getASTContext())) {
5769 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5770 }
5771 }
5772 }
5773 if (is_bitfield_ptr)
5774 *is_bitfield_ptr = is_bitfield;
5775
5776 return CompilerType(getASTContext(), field->getType());
5777 }
5778 }
5779 }
5780 break;
5781
Sean Callananf9c622a2016-09-30 18:44:43 +00005782 case clang::Type::ObjCObjectPointer: {
5783 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005784 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005785 const clang::ObjCInterfaceType *objc_interface_type =
5786 objc_class_type->getInterfaceType();
5787 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005788 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5789 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005790 clang::ObjCInterfaceDecl *class_interface_decl =
5791 objc_interface_type->getDecl();
5792 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005793 return CompilerType(
5794 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5795 idx, name, bit_offset_ptr,
5796 bitfield_bit_size_ptr, is_bitfield_ptr));
5797 }
5798 }
5799 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005800 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005801
5802 case clang::Type::ObjCObject:
5803 case clang::Type::ObjCInterface:
5804 if (GetCompleteType(type)) {
5805 const clang::ObjCObjectType *objc_class_type =
5806 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5807 assert(objc_class_type);
5808 if (objc_class_type) {
5809 clang::ObjCInterfaceDecl *class_interface_decl =
5810 objc_class_type->getInterface();
5811 return CompilerType(
5812 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5813 idx, name, bit_offset_ptr,
5814 bitfield_bit_size_ptr, is_bitfield_ptr));
5815 }
5816 }
5817 break;
5818
5819 case clang::Type::Typedef:
5820 return CompilerType(getASTContext(),
5821 llvm::cast<clang::TypedefType>(qual_type)
5822 ->getDecl()
5823 ->getUnderlyingType())
5824 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5825 is_bitfield_ptr);
5826
5827 case clang::Type::Auto:
5828 return CompilerType(
5829 getASTContext(),
5830 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5831 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5832 is_bitfield_ptr);
5833
5834 case clang::Type::Elaborated:
5835 return CompilerType(
5836 getASTContext(),
5837 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5838 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5839 is_bitfield_ptr);
5840
5841 case clang::Type::Paren:
5842 return CompilerType(getASTContext(),
5843 llvm::cast<clang::ParenType>(qual_type)->desugar())
5844 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5845 is_bitfield_ptr);
5846
5847 default:
5848 break;
5849 }
5850 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005851}
5852
Greg Clayton99558cc42015-08-24 23:46:31 +00005853uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005854ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
5855 uint32_t count = 0;
5856 clang::QualType qual_type(GetCanonicalQualType(type));
5857 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5858 switch (type_class) {
5859 case clang::Type::Record:
5860 if (GetCompleteType(type)) {
5861 const clang::CXXRecordDecl *cxx_record_decl =
5862 qual_type->getAsCXXRecordDecl();
5863 if (cxx_record_decl)
5864 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00005865 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005866 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00005867
Kate Stoneb9c1b512016-09-06 20:57:50 +00005868 case clang::Type::ObjCObjectPointer:
5869 count = GetPointeeType(type).GetNumDirectBaseClasses();
5870 break;
5871
5872 case clang::Type::ObjCObject:
5873 if (GetCompleteType(type)) {
5874 const clang::ObjCObjectType *objc_class_type =
5875 qual_type->getAsObjCQualifiedInterfaceType();
5876 if (objc_class_type) {
5877 clang::ObjCInterfaceDecl *class_interface_decl =
5878 objc_class_type->getInterface();
5879
5880 if (class_interface_decl && class_interface_decl->getSuperClass())
5881 count = 1;
5882 }
5883 }
5884 break;
5885 case clang::Type::ObjCInterface:
5886 if (GetCompleteType(type)) {
5887 const clang::ObjCInterfaceType *objc_interface_type =
5888 qual_type->getAs<clang::ObjCInterfaceType>();
5889 if (objc_interface_type) {
5890 clang::ObjCInterfaceDecl *class_interface_decl =
5891 objc_interface_type->getInterface();
5892
5893 if (class_interface_decl && class_interface_decl->getSuperClass())
5894 count = 1;
5895 }
5896 }
5897 break;
5898
5899 case clang::Type::Typedef:
5900 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5901 ->getDecl()
5902 ->getUnderlyingType()
5903 .getAsOpaquePtr());
5904 break;
5905
5906 case clang::Type::Auto:
5907 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5908 ->getDeducedType()
5909 .getAsOpaquePtr());
5910 break;
5911
5912 case clang::Type::Elaborated:
5913 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5914 ->getNamedType()
5915 .getAsOpaquePtr());
5916 break;
5917
5918 case clang::Type::Paren:
5919 return GetNumDirectBaseClasses(
5920 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5921
5922 default:
5923 break;
5924 }
5925 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00005926}
5927
5928uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005929ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
5930 uint32_t count = 0;
5931 clang::QualType qual_type(GetCanonicalQualType(type));
5932 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5933 switch (type_class) {
5934 case clang::Type::Record:
5935 if (GetCompleteType(type)) {
5936 const clang::CXXRecordDecl *cxx_record_decl =
5937 qual_type->getAsCXXRecordDecl();
5938 if (cxx_record_decl)
5939 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00005940 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005941 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00005942
Kate Stoneb9c1b512016-09-06 20:57:50 +00005943 case clang::Type::Typedef:
5944 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5945 ->getDecl()
5946 ->getUnderlyingType()
5947 .getAsOpaquePtr());
5948 break;
5949
5950 case clang::Type::Auto:
5951 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5952 ->getDeducedType()
5953 .getAsOpaquePtr());
5954 break;
5955
5956 case clang::Type::Elaborated:
5957 count =
5958 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5959 ->getNamedType()
5960 .getAsOpaquePtr());
5961 break;
5962
5963 case clang::Type::Paren:
5964 count = GetNumVirtualBaseClasses(
5965 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5966 break;
5967
5968 default:
5969 break;
5970 }
5971 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00005972}
5973
Kate Stoneb9c1b512016-09-06 20:57:50 +00005974CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
5975 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
5976 clang::QualType qual_type(GetCanonicalQualType(type));
5977 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5978 switch (type_class) {
5979 case clang::Type::Record:
5980 if (GetCompleteType(type)) {
5981 const clang::CXXRecordDecl *cxx_record_decl =
5982 qual_type->getAsCXXRecordDecl();
5983 if (cxx_record_decl) {
5984 uint32_t curr_idx = 0;
5985 clang::CXXRecordDecl::base_class_const_iterator base_class,
5986 base_class_end;
5987 for (base_class = cxx_record_decl->bases_begin(),
5988 base_class_end = cxx_record_decl->bases_end();
5989 base_class != base_class_end; ++base_class, ++curr_idx) {
5990 if (curr_idx == idx) {
5991 if (bit_offset_ptr) {
5992 const clang::ASTRecordLayout &record_layout =
5993 getASTContext()->getASTRecordLayout(cxx_record_decl);
5994 const clang::CXXRecordDecl *base_class_decl =
5995 llvm::cast<clang::CXXRecordDecl>(
5996 base_class->getType()
5997 ->getAs<clang::RecordType>()
5998 ->getDecl());
5999 if (base_class->isVirtual())
6000 *bit_offset_ptr =
6001 record_layout.getVBaseClassOffset(base_class_decl)
6002 .getQuantity() *
6003 8;
6004 else
6005 *bit_offset_ptr =
6006 record_layout.getBaseClassOffset(base_class_decl)
6007 .getQuantity() *
6008 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006009 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006010 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6011 }
6012 }
6013 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006014 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006015 break;
6016
6017 case clang::Type::ObjCObjectPointer:
6018 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6019
6020 case clang::Type::ObjCObject:
6021 if (idx == 0 && GetCompleteType(type)) {
6022 const clang::ObjCObjectType *objc_class_type =
6023 qual_type->getAsObjCQualifiedInterfaceType();
6024 if (objc_class_type) {
6025 clang::ObjCInterfaceDecl *class_interface_decl =
6026 objc_class_type->getInterface();
6027
6028 if (class_interface_decl) {
6029 clang::ObjCInterfaceDecl *superclass_interface_decl =
6030 class_interface_decl->getSuperClass();
6031 if (superclass_interface_decl) {
6032 if (bit_offset_ptr)
6033 *bit_offset_ptr = 0;
6034 return CompilerType(getASTContext(),
6035 getASTContext()->getObjCInterfaceType(
6036 superclass_interface_decl));
6037 }
6038 }
6039 }
6040 }
6041 break;
6042 case clang::Type::ObjCInterface:
6043 if (idx == 0 && GetCompleteType(type)) {
6044 const clang::ObjCObjectType *objc_interface_type =
6045 qual_type->getAs<clang::ObjCInterfaceType>();
6046 if (objc_interface_type) {
6047 clang::ObjCInterfaceDecl *class_interface_decl =
6048 objc_interface_type->getInterface();
6049
6050 if (class_interface_decl) {
6051 clang::ObjCInterfaceDecl *superclass_interface_decl =
6052 class_interface_decl->getSuperClass();
6053 if (superclass_interface_decl) {
6054 if (bit_offset_ptr)
6055 *bit_offset_ptr = 0;
6056 return CompilerType(getASTContext(),
6057 getASTContext()->getObjCInterfaceType(
6058 superclass_interface_decl));
6059 }
6060 }
6061 }
6062 }
6063 break;
6064
6065 case clang::Type::Typedef:
6066 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6067 ->getDecl()
6068 ->getUnderlyingType()
6069 .getAsOpaquePtr(),
6070 idx, bit_offset_ptr);
6071
6072 case clang::Type::Auto:
6073 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6074 ->getDeducedType()
6075 .getAsOpaquePtr(),
6076 idx, bit_offset_ptr);
6077
6078 case clang::Type::Elaborated:
6079 return GetDirectBaseClassAtIndex(
6080 llvm::cast<clang::ElaboratedType>(qual_type)
6081 ->getNamedType()
6082 .getAsOpaquePtr(),
6083 idx, bit_offset_ptr);
6084
6085 case clang::Type::Paren:
6086 return GetDirectBaseClassAtIndex(
6087 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6088 idx, bit_offset_ptr);
6089
6090 default:
6091 break;
6092 }
6093 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006094}
6095
Kate Stoneb9c1b512016-09-06 20:57:50 +00006096CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6097 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6098 clang::QualType qual_type(GetCanonicalQualType(type));
6099 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6100 switch (type_class) {
6101 case clang::Type::Record:
6102 if (GetCompleteType(type)) {
6103 const clang::CXXRecordDecl *cxx_record_decl =
6104 qual_type->getAsCXXRecordDecl();
6105 if (cxx_record_decl) {
6106 uint32_t curr_idx = 0;
6107 clang::CXXRecordDecl::base_class_const_iterator base_class,
6108 base_class_end;
6109 for (base_class = cxx_record_decl->vbases_begin(),
6110 base_class_end = cxx_record_decl->vbases_end();
6111 base_class != base_class_end; ++base_class, ++curr_idx) {
6112 if (curr_idx == idx) {
6113 if (bit_offset_ptr) {
6114 const clang::ASTRecordLayout &record_layout =
6115 getASTContext()->getASTRecordLayout(cxx_record_decl);
6116 const clang::CXXRecordDecl *base_class_decl =
6117 llvm::cast<clang::CXXRecordDecl>(
6118 base_class->getType()
6119 ->getAs<clang::RecordType>()
6120 ->getDecl());
6121 *bit_offset_ptr =
6122 record_layout.getVBaseClassOffset(base_class_decl)
6123 .getQuantity() *
6124 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006125 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006126 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6127 }
6128 }
6129 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006130 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006131 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006132
Kate Stoneb9c1b512016-09-06 20:57:50 +00006133 case clang::Type::Typedef:
6134 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6135 ->getDecl()
6136 ->getUnderlyingType()
6137 .getAsOpaquePtr(),
6138 idx, bit_offset_ptr);
6139
6140 case clang::Type::Auto:
6141 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6142 ->getDeducedType()
6143 .getAsOpaquePtr(),
6144 idx, bit_offset_ptr);
6145
6146 case clang::Type::Elaborated:
6147 return GetVirtualBaseClassAtIndex(
6148 llvm::cast<clang::ElaboratedType>(qual_type)
6149 ->getNamedType()
6150 .getAsOpaquePtr(),
6151 idx, bit_offset_ptr);
6152
6153 case clang::Type::Paren:
6154 return GetVirtualBaseClassAtIndex(
6155 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6156 idx, bit_offset_ptr);
6157
6158 default:
6159 break;
6160 }
6161 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006162}
6163
Greg Claytond8d4a572015-08-11 21:38:15 +00006164// If a pointer to a pointee type (the clang_type arg) says that it has no
6165// children, then we either need to trust it, or override it and return a
6166// different result. For example, an "int *" has one child that is an integer,
6167// but a function pointer doesn't have any children. Likewise if a Record type
6168// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006169uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6170 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006171 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006172
6173 clang::QualType qual_type(type.getCanonicalType());
6174 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6175 switch (type_class) {
6176 case clang::Type::Builtin:
6177 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6178 case clang::BuiltinType::UnknownAny:
6179 case clang::BuiltinType::Void:
6180 case clang::BuiltinType::NullPtr:
6181 case clang::BuiltinType::OCLEvent:
6182 case clang::BuiltinType::OCLImage1dRO:
6183 case clang::BuiltinType::OCLImage1dWO:
6184 case clang::BuiltinType::OCLImage1dRW:
6185 case clang::BuiltinType::OCLImage1dArrayRO:
6186 case clang::BuiltinType::OCLImage1dArrayWO:
6187 case clang::BuiltinType::OCLImage1dArrayRW:
6188 case clang::BuiltinType::OCLImage1dBufferRO:
6189 case clang::BuiltinType::OCLImage1dBufferWO:
6190 case clang::BuiltinType::OCLImage1dBufferRW:
6191 case clang::BuiltinType::OCLImage2dRO:
6192 case clang::BuiltinType::OCLImage2dWO:
6193 case clang::BuiltinType::OCLImage2dRW:
6194 case clang::BuiltinType::OCLImage2dArrayRO:
6195 case clang::BuiltinType::OCLImage2dArrayWO:
6196 case clang::BuiltinType::OCLImage2dArrayRW:
6197 case clang::BuiltinType::OCLImage3dRO:
6198 case clang::BuiltinType::OCLImage3dWO:
6199 case clang::BuiltinType::OCLImage3dRW:
6200 case clang::BuiltinType::OCLSampler:
6201 return 0;
6202 case clang::BuiltinType::Bool:
6203 case clang::BuiltinType::Char_U:
6204 case clang::BuiltinType::UChar:
6205 case clang::BuiltinType::WChar_U:
6206 case clang::BuiltinType::Char16:
6207 case clang::BuiltinType::Char32:
6208 case clang::BuiltinType::UShort:
6209 case clang::BuiltinType::UInt:
6210 case clang::BuiltinType::ULong:
6211 case clang::BuiltinType::ULongLong:
6212 case clang::BuiltinType::UInt128:
6213 case clang::BuiltinType::Char_S:
6214 case clang::BuiltinType::SChar:
6215 case clang::BuiltinType::WChar_S:
6216 case clang::BuiltinType::Short:
6217 case clang::BuiltinType::Int:
6218 case clang::BuiltinType::Long:
6219 case clang::BuiltinType::LongLong:
6220 case clang::BuiltinType::Int128:
6221 case clang::BuiltinType::Float:
6222 case clang::BuiltinType::Double:
6223 case clang::BuiltinType::LongDouble:
6224 case clang::BuiltinType::Dependent:
6225 case clang::BuiltinType::Overload:
6226 case clang::BuiltinType::ObjCId:
6227 case clang::BuiltinType::ObjCClass:
6228 case clang::BuiltinType::ObjCSel:
6229 case clang::BuiltinType::BoundMember:
6230 case clang::BuiltinType::Half:
6231 case clang::BuiltinType::ARCUnbridgedCast:
6232 case clang::BuiltinType::PseudoObject:
6233 case clang::BuiltinType::BuiltinFn:
6234 case clang::BuiltinType::OMPArraySection:
6235 return 1;
6236 default:
6237 return 0;
6238 }
6239 break;
6240
6241 case clang::Type::Complex:
6242 return 1;
6243 case clang::Type::Pointer:
6244 return 1;
6245 case clang::Type::BlockPointer:
6246 return 0; // If block pointers don't have debug info, then no children for
6247 // them
6248 case clang::Type::LValueReference:
6249 return 1;
6250 case clang::Type::RValueReference:
6251 return 1;
6252 case clang::Type::MemberPointer:
6253 return 0;
6254 case clang::Type::ConstantArray:
6255 return 0;
6256 case clang::Type::IncompleteArray:
6257 return 0;
6258 case clang::Type::VariableArray:
6259 return 0;
6260 case clang::Type::DependentSizedArray:
6261 return 0;
6262 case clang::Type::DependentSizedExtVector:
6263 return 0;
6264 case clang::Type::Vector:
6265 return 0;
6266 case clang::Type::ExtVector:
6267 return 0;
6268 case clang::Type::FunctionProto:
6269 return 0; // When we function pointers, they have no children...
6270 case clang::Type::FunctionNoProto:
6271 return 0; // When we function pointers, they have no children...
6272 case clang::Type::UnresolvedUsing:
6273 return 0;
6274 case clang::Type::Paren:
6275 return GetNumPointeeChildren(
6276 llvm::cast<clang::ParenType>(qual_type)->desugar());
6277 case clang::Type::Typedef:
6278 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6279 ->getDecl()
6280 ->getUnderlyingType());
6281 case clang::Type::Auto:
6282 return GetNumPointeeChildren(
6283 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6284 case clang::Type::Elaborated:
6285 return GetNumPointeeChildren(
6286 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6287 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006288 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6289 ->getUnderlyingExpr()
6290 ->getType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006291 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006292 return GetNumPointeeChildren(
6293 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006294 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006295 return GetNumPointeeChildren(
6296 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006297 case clang::Type::Record:
6298 return 0;
6299 case clang::Type::Enum:
6300 return 1;
6301 case clang::Type::TemplateTypeParm:
6302 return 1;
6303 case clang::Type::SubstTemplateTypeParm:
6304 return 1;
6305 case clang::Type::TemplateSpecialization:
6306 return 1;
6307 case clang::Type::InjectedClassName:
6308 return 0;
6309 case clang::Type::DependentName:
6310 return 1;
6311 case clang::Type::DependentTemplateSpecialization:
6312 return 1;
6313 case clang::Type::ObjCObject:
6314 return 0;
6315 case clang::Type::ObjCInterface:
6316 return 0;
6317 case clang::Type::ObjCObjectPointer:
6318 return 1;
6319 default:
6320 break;
6321 }
6322 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006323}
6324
Kate Stoneb9c1b512016-09-06 20:57:50 +00006325CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6326 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6327 bool transparent_pointers, bool omit_empty_base_classes,
6328 bool ignore_array_bounds, std::string &child_name,
6329 uint32_t &child_byte_size, int32_t &child_byte_offset,
6330 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6331 bool &child_is_base_class, bool &child_is_deref_of_parent,
6332 ValueObject *valobj, uint64_t &language_flags) {
6333 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006334 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006335
Kate Stoneb9c1b512016-09-06 20:57:50 +00006336 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6337 const clang::Type::TypeClass parent_type_class =
6338 parent_qual_type->getTypeClass();
6339 child_bitfield_bit_size = 0;
6340 child_bitfield_bit_offset = 0;
6341 child_is_base_class = false;
6342 language_flags = 0;
6343
6344 const bool idx_is_valid = idx < GetNumChildren(type, omit_empty_base_classes);
6345 uint32_t bit_offset;
6346 switch (parent_type_class) {
6347 case clang::Type::Builtin:
6348 if (idx_is_valid) {
6349 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6350 case clang::BuiltinType::ObjCId:
6351 case clang::BuiltinType::ObjCClass:
6352 child_name = "isa";
6353 child_byte_size =
6354 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6355 CHAR_BIT;
6356 return CompilerType(getASTContext(),
6357 getASTContext()->ObjCBuiltinClassTy);
6358
6359 default:
6360 break;
6361 }
6362 }
6363 break;
6364
6365 case clang::Type::Record:
6366 if (idx_is_valid && GetCompleteType(type)) {
6367 const clang::RecordType *record_type =
6368 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6369 const clang::RecordDecl *record_decl = record_type->getDecl();
6370 assert(record_decl);
6371 const clang::ASTRecordLayout &record_layout =
6372 getASTContext()->getASTRecordLayout(record_decl);
6373 uint32_t child_idx = 0;
6374
6375 const clang::CXXRecordDecl *cxx_record_decl =
6376 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6377 if (cxx_record_decl) {
6378 // We might have base classes to print out first
6379 clang::CXXRecordDecl::base_class_const_iterator base_class,
6380 base_class_end;
6381 for (base_class = cxx_record_decl->bases_begin(),
6382 base_class_end = cxx_record_decl->bases_end();
6383 base_class != base_class_end; ++base_class) {
6384 const clang::CXXRecordDecl *base_class_decl = nullptr;
6385
6386 // Skip empty base classes
6387 if (omit_empty_base_classes) {
6388 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6389 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6390 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
6391 continue;
6392 }
6393
6394 if (idx == child_idx) {
6395 if (base_class_decl == nullptr)
6396 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6397 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6398
6399 if (base_class->isVirtual()) {
6400 bool handled = false;
6401 if (valobj) {
Zachary Turner97206d52017-05-12 04:51:55 +00006402 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006403 AddressType addr_type = eAddressTypeInvalid;
6404 lldb::addr_t vtable_ptr_addr =
6405 valobj->GetCPPVTableAddress(addr_type);
6406
6407 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS &&
6408 addr_type == eAddressTypeLoad) {
6409
6410 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
6411 Process *process = exe_ctx.GetProcessPtr();
6412 if (process) {
6413 clang::VTableContextBase *vtable_ctx =
6414 getASTContext()->getVTableContext();
6415 if (vtable_ctx) {
6416 if (vtable_ctx->isMicrosoft()) {
6417 clang::MicrosoftVTableContext *msoft_vtable_ctx =
6418 static_cast<clang::MicrosoftVTableContext *>(
6419 vtable_ctx);
6420
6421 if (vtable_ptr_addr) {
6422 const lldb::addr_t vbtable_ptr_addr =
6423 vtable_ptr_addr +
6424 record_layout.getVBPtrOffset().getQuantity();
6425
6426 const lldb::addr_t vbtable_ptr =
6427 process->ReadPointerFromMemory(vbtable_ptr_addr,
6428 err);
6429 if (vbtable_ptr != LLDB_INVALID_ADDRESS) {
6430 // Get the index into the virtual base table. The
6431 // index is the index in uint32_t from vbtable_ptr
6432 const unsigned vbtable_index =
6433 msoft_vtable_ctx->getVBTableIndex(
6434 cxx_record_decl, base_class_decl);
6435 const lldb::addr_t base_offset_addr =
6436 vbtable_ptr + vbtable_index * 4;
6437 const uint32_t base_offset =
6438 process->ReadUnsignedIntegerFromMemory(
6439 base_offset_addr, 4, UINT32_MAX, err);
6440 if (base_offset != UINT32_MAX) {
6441 handled = true;
6442 bit_offset = base_offset * 8;
6443 }
6444 }
6445 }
6446 } else {
6447 clang::ItaniumVTableContext *itanium_vtable_ctx =
6448 static_cast<clang::ItaniumVTableContext *>(
6449 vtable_ctx);
6450 if (vtable_ptr_addr) {
6451 const lldb::addr_t vtable_ptr =
6452 process->ReadPointerFromMemory(vtable_ptr_addr,
6453 err);
6454 if (vtable_ptr != LLDB_INVALID_ADDRESS) {
6455 clang::CharUnits base_offset_offset =
6456 itanium_vtable_ctx->getVirtualBaseOffsetOffset(
6457 cxx_record_decl, base_class_decl);
6458 const lldb::addr_t base_offset_addr =
6459 vtable_ptr + base_offset_offset.getQuantity();
6460 const uint32_t base_offset_size =
6461 process->GetAddressByteSize();
6462 const uint64_t base_offset =
6463 process->ReadUnsignedIntegerFromMemory(
6464 base_offset_addr, base_offset_size,
6465 UINT32_MAX, err);
6466 if (base_offset < UINT32_MAX) {
6467 handled = true;
6468 bit_offset = base_offset * 8;
6469 }
6470 }
6471 }
6472 }
6473 }
6474 }
6475 }
6476 }
6477 if (!handled)
6478 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6479 .getQuantity() *
6480 8;
6481 } else
6482 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6483 .getQuantity() *
6484 8;
6485
6486 // Base classes should be a multiple of 8 bits in size
6487 child_byte_offset = bit_offset / 8;
6488 CompilerType base_class_clang_type(getASTContext(),
6489 base_class->getType());
6490 child_name = base_class_clang_type.GetTypeName().AsCString("");
6491 uint64_t base_class_clang_type_bit_size =
6492 base_class_clang_type.GetBitSize(
6493 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6494
6495 // Base classes bit sizes should be a multiple of 8 bits in size
6496 assert(base_class_clang_type_bit_size % 8 == 0);
6497 child_byte_size = base_class_clang_type_bit_size / 8;
6498 child_is_base_class = true;
6499 return base_class_clang_type;
6500 }
6501 // We don't increment the child index in the for loop since we might
6502 // be skipping empty base classes
6503 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006504 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006505 }
6506 // Make sure index is in range...
6507 uint32_t field_idx = 0;
6508 clang::RecordDecl::field_iterator field, field_end;
6509 for (field = record_decl->field_begin(),
6510 field_end = record_decl->field_end();
6511 field != field_end; ++field, ++field_idx, ++child_idx) {
6512 if (idx == child_idx) {
6513 // Print the member type if requested
6514 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006515 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006516
6517 // Figure out the type byte size (field_type_info.first) and
6518 // alignment (field_type_info.second) from the AST context.
6519 CompilerType field_clang_type(getASTContext(), field->getType());
6520 assert(field_idx < record_layout.getFieldCount());
6521 child_byte_size = field_clang_type.GetByteSize(
6522 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6523 const uint32_t child_bit_size = child_byte_size * 8;
6524
6525 // Figure out the field offset within the current struct/union/class
6526 // type
6527 bit_offset = record_layout.getFieldOffset(field_idx);
6528 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6529 child_bitfield_bit_size)) {
6530 child_bitfield_bit_offset = bit_offset % child_bit_size;
6531 const uint32_t child_bit_offset =
6532 bit_offset - child_bitfield_bit_offset;
6533 child_byte_offset = child_bit_offset / 8;
6534 } else {
6535 child_byte_offset = bit_offset / 8;
6536 }
6537
6538 return field_clang_type;
6539 }
6540 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006541 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006542 break;
6543
6544 case clang::Type::ObjCObject:
6545 case clang::Type::ObjCInterface:
6546 if (idx_is_valid && GetCompleteType(type)) {
6547 const clang::ObjCObjectType *objc_class_type =
6548 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6549 assert(objc_class_type);
6550 if (objc_class_type) {
6551 uint32_t child_idx = 0;
6552 clang::ObjCInterfaceDecl *class_interface_decl =
6553 objc_class_type->getInterface();
6554
6555 if (class_interface_decl) {
6556
6557 const clang::ASTRecordLayout &interface_layout =
6558 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6559 clang::ObjCInterfaceDecl *superclass_interface_decl =
6560 class_interface_decl->getSuperClass();
6561 if (superclass_interface_decl) {
6562 if (omit_empty_base_classes) {
6563 CompilerType base_class_clang_type(
6564 getASTContext(), getASTContext()->getObjCInterfaceType(
6565 superclass_interface_decl));
6566 if (base_class_clang_type.GetNumChildren(
6567 omit_empty_base_classes) > 0) {
6568 if (idx == 0) {
6569 clang::QualType ivar_qual_type(
6570 getASTContext()->getObjCInterfaceType(
6571 superclass_interface_decl));
6572
6573 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006574 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006575
6576 clang::TypeInfo ivar_type_info =
6577 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6578
6579 child_byte_size = ivar_type_info.Width / 8;
6580 child_byte_offset = 0;
6581 child_is_base_class = true;
6582
6583 return CompilerType(getASTContext(), ivar_qual_type);
6584 }
6585
6586 ++child_idx;
6587 }
6588 } else
6589 ++child_idx;
6590 }
6591
6592 const uint32_t superclass_idx = child_idx;
6593
6594 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6595 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6596 ivar_end = class_interface_decl->ivar_end();
6597
6598 for (ivar_pos = class_interface_decl->ivar_begin();
6599 ivar_pos != ivar_end; ++ivar_pos) {
6600 if (child_idx == idx) {
6601 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6602
6603 clang::QualType ivar_qual_type(ivar_decl->getType());
6604
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006605 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006606
6607 clang::TypeInfo ivar_type_info =
6608 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6609
6610 child_byte_size = ivar_type_info.Width / 8;
6611
6612 // Figure out the field offset within the current
Adrian Prantl05097242018-04-30 16:49:04 +00006613 // struct/union/class type For ObjC objects, we can't trust the
6614 // bit offset we get from the Clang AST, since that doesn't
6615 // account for the space taken up by unbacked properties, or
6616 // from the changing size of base classes that are newer than
6617 // this class. So if we have a process around that we can ask
6618 // about this object, do so.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006619 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6620 Process *process = nullptr;
6621 if (exe_ctx)
6622 process = exe_ctx->GetProcessPtr();
6623 if (process) {
6624 ObjCLanguageRuntime *objc_runtime =
6625 process->GetObjCLanguageRuntime();
6626 if (objc_runtime != nullptr) {
6627 CompilerType parent_ast_type(getASTContext(),
6628 parent_qual_type);
6629 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6630 parent_ast_type, ivar_decl->getNameAsString().c_str());
6631 }
6632 }
6633
6634 // Setting this to UINT32_MAX to make sure we don't compute it
6635 // twice...
6636 bit_offset = UINT32_MAX;
6637
6638 if (child_byte_offset ==
6639 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6640 bit_offset = interface_layout.getFieldOffset(child_idx -
6641 superclass_idx);
6642 child_byte_offset = bit_offset / 8;
6643 }
6644
6645 // Note, the ObjC Ivar Byte offset is just that, it doesn't
Adrian Prantl05097242018-04-30 16:49:04 +00006646 // account for the bit offset of a bitfield within its
6647 // containing object. So regardless of where we get the byte
Kate Stoneb9c1b512016-09-06 20:57:50 +00006648 // offset from, we still need to get the bit offset for
6649 // bitfields from the layout.
6650
6651 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6652 child_bitfield_bit_size)) {
6653 if (bit_offset == UINT32_MAX)
6654 bit_offset = interface_layout.getFieldOffset(
6655 child_idx - superclass_idx);
6656
6657 child_bitfield_bit_offset = bit_offset % 8;
6658 }
6659 return CompilerType(getASTContext(), ivar_qual_type);
6660 }
6661 ++child_idx;
6662 }
6663 }
6664 }
6665 }
6666 }
6667 break;
6668
6669 case clang::Type::ObjCObjectPointer:
6670 if (idx_is_valid) {
6671 CompilerType pointee_clang_type(GetPointeeType(type));
6672
6673 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6674 child_is_deref_of_parent = false;
6675 bool tmp_child_is_deref_of_parent = false;
6676 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6677 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6678 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6679 child_bitfield_bit_size, child_bitfield_bit_offset,
6680 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6681 language_flags);
6682 } else {
6683 child_is_deref_of_parent = true;
6684 const char *parent_name =
6685 valobj ? valobj->GetName().GetCString() : NULL;
6686 if (parent_name) {
6687 child_name.assign(1, '*');
6688 child_name += parent_name;
6689 }
6690
6691 // We have a pointer to an simple type
6692 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
6693 child_byte_size = pointee_clang_type.GetByteSize(
6694 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6695 child_byte_offset = 0;
6696 return pointee_clang_type;
6697 }
6698 }
6699 }
6700 break;
6701
6702 case clang::Type::Vector:
6703 case clang::Type::ExtVector:
6704 if (idx_is_valid) {
6705 const clang::VectorType *array =
6706 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6707 if (array) {
6708 CompilerType element_type(getASTContext(), array->getElementType());
6709 if (element_type.GetCompleteType()) {
6710 char element_name[64];
6711 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6712 static_cast<uint64_t>(idx));
6713 child_name.assign(element_name);
6714 child_byte_size = element_type.GetByteSize(
6715 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6716 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6717 return element_type;
6718 }
6719 }
6720 }
6721 break;
6722
6723 case clang::Type::ConstantArray:
6724 case clang::Type::IncompleteArray:
6725 if (ignore_array_bounds || idx_is_valid) {
6726 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6727 if (array) {
6728 CompilerType element_type(getASTContext(), array->getElementType());
6729 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006730 child_name = llvm::formatv("[{0}]", idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006731 child_byte_size = element_type.GetByteSize(
6732 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6733 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6734 return element_type;
6735 }
6736 }
6737 }
6738 break;
6739
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006740 case clang::Type::Pointer: {
6741 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006742
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006743 // Don't dereference "void *" pointers
6744 if (pointee_clang_type.IsVoidType())
6745 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006746
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006747 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6748 child_is_deref_of_parent = false;
6749 bool tmp_child_is_deref_of_parent = false;
6750 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6751 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6752 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6753 child_bitfield_bit_size, child_bitfield_bit_offset,
6754 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6755 language_flags);
6756 } else {
6757 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006758
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006759 const char *parent_name =
6760 valobj ? valobj->GetName().GetCString() : NULL;
6761 if (parent_name) {
6762 child_name.assign(1, '*');
6763 child_name += parent_name;
6764 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006765
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006766 // We have a pointer to an simple type
6767 if (idx == 0) {
6768 child_byte_size = pointee_clang_type.GetByteSize(
6769 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6770 child_byte_offset = 0;
6771 return pointee_clang_type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006772 }
6773 }
6774 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006775 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006776
6777 case clang::Type::LValueReference:
6778 case clang::Type::RValueReference:
6779 if (idx_is_valid) {
6780 const clang::ReferenceType *reference_type =
6781 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6782 CompilerType pointee_clang_type(getASTContext(),
6783 reference_type->getPointeeType());
6784 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6785 child_is_deref_of_parent = false;
6786 bool tmp_child_is_deref_of_parent = false;
6787 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6788 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6789 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6790 child_bitfield_bit_size, child_bitfield_bit_offset,
6791 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6792 language_flags);
6793 } else {
6794 const char *parent_name =
6795 valobj ? valobj->GetName().GetCString() : NULL;
6796 if (parent_name) {
6797 child_name.assign(1, '&');
6798 child_name += parent_name;
6799 }
6800
6801 // We have a pointer to an simple type
6802 if (idx == 0) {
6803 child_byte_size = pointee_clang_type.GetByteSize(
6804 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6805 child_byte_offset = 0;
6806 return pointee_clang_type;
6807 }
6808 }
6809 }
6810 break;
6811
6812 case clang::Type::Typedef: {
6813 CompilerType typedefed_clang_type(
6814 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
6815 ->getDecl()
6816 ->getUnderlyingType());
6817 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
6818 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6819 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6820 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6821 child_is_deref_of_parent, valobj, language_flags);
6822 } break;
6823
6824 case clang::Type::Auto: {
6825 CompilerType elaborated_clang_type(
6826 getASTContext(),
6827 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
6828 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6829 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6830 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6831 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6832 child_is_deref_of_parent, valobj, language_flags);
6833 }
6834
6835 case clang::Type::Elaborated: {
6836 CompilerType elaborated_clang_type(
6837 getASTContext(),
6838 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
6839 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6840 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6841 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6842 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6843 child_is_deref_of_parent, valobj, language_flags);
6844 }
6845
6846 case clang::Type::Paren: {
6847 CompilerType paren_clang_type(
6848 getASTContext(),
6849 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
6850 return paren_clang_type.GetChildCompilerTypeAtIndex(
6851 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6852 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6853 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6854 child_is_deref_of_parent, valobj, language_flags);
6855 }
6856
6857 default:
6858 break;
6859 }
6860 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006861}
6862
Kate Stoneb9c1b512016-09-06 20:57:50 +00006863static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
6864 const clang::CXXBaseSpecifier *base_spec,
6865 bool omit_empty_base_classes) {
6866 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006867
Kate Stoneb9c1b512016-09-06 20:57:50 +00006868 const clang::CXXRecordDecl *cxx_record_decl =
6869 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6870
6871 // const char *super_name = record_decl->getNameAsCString();
6872 // const char *base_name =
6873 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6874 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6875 //
6876 if (cxx_record_decl) {
6877 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6878 for (base_class = cxx_record_decl->bases_begin(),
6879 base_class_end = cxx_record_decl->bases_end();
6880 base_class != base_class_end; ++base_class) {
6881 if (omit_empty_base_classes) {
6882 if (BaseSpecifierIsEmpty(base_class))
6883 continue;
6884 }
6885
6886 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
6887 // super_name, base_name,
6888 // child_idx,
6889 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6890 //
6891 //
6892 if (base_class == base_spec)
6893 return child_idx;
6894 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006895 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006896 }
6897
6898 return UINT32_MAX;
6899}
6900
6901static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
6902 clang::NamedDecl *canonical_decl,
6903 bool omit_empty_base_classes) {
6904 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
6905 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6906 omit_empty_base_classes);
6907
6908 clang::RecordDecl::field_iterator field, field_end;
6909 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6910 field != field_end; ++field, ++child_idx) {
6911 if (field->getCanonicalDecl() == canonical_decl)
6912 return child_idx;
6913 }
6914
6915 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00006916}
6917
6918// Look for a child member (doesn't include base classes, but it does include
Adrian Prantl05097242018-04-30 16:49:04 +00006919// their members) in the type hierarchy. Returns an index path into
6920// "clang_type" on how to reach the appropriate member.
Greg Claytond8d4a572015-08-11 21:38:15 +00006921//
6922// class A
6923// {
6924// public:
6925// int m_a;
6926// int m_b;
6927// };
6928//
6929// class B
6930// {
6931// };
6932//
6933// class C :
6934// public B,
6935// public A
6936// {
6937// };
6938//
6939// If we have a clang type that describes "class C", and we wanted to looked
6940// "m_b" in it:
6941//
Kate Stoneb9c1b512016-09-06 20:57:50 +00006942// With omit_empty_base_classes == false we would get an integer array back
Adrian Prantl05097242018-04-30 16:49:04 +00006943// with: { 1, 1 } The first index 1 is the child index for "class A" within
6944// class C The second index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00006945//
Adrian Prantl05097242018-04-30 16:49:04 +00006946// With omit_empty_base_classes == true we would get an integer array back
6947// with: { 0, 1 } The first index 0 is the child index for "class A" within
6948// class C (since class B doesn't have any members it doesn't count) The second
6949// index 1 is the child index for "m_b" within class A
Greg Claytond8d4a572015-08-11 21:38:15 +00006950
Kate Stoneb9c1b512016-09-06 20:57:50 +00006951size_t ClangASTContext::GetIndexOfChildMemberWithName(
6952 lldb::opaque_compiler_type_t type, const char *name,
6953 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
6954 if (type && name && name[0]) {
6955 clang::QualType qual_type(GetCanonicalQualType(type));
6956 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6957 switch (type_class) {
6958 case clang::Type::Record:
6959 if (GetCompleteType(type)) {
6960 const clang::RecordType *record_type =
6961 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6962 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00006963
Kate Stoneb9c1b512016-09-06 20:57:50 +00006964 assert(record_decl);
6965 uint32_t child_idx = 0;
6966
6967 const clang::CXXRecordDecl *cxx_record_decl =
6968 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6969
6970 // Try and find a field that matches NAME
6971 clang::RecordDecl::field_iterator field, field_end;
6972 llvm::StringRef name_sref(name);
6973 for (field = record_decl->field_begin(),
6974 field_end = record_decl->field_end();
6975 field != field_end; ++field, ++child_idx) {
6976 llvm::StringRef field_name = field->getName();
6977 if (field_name.empty()) {
6978 CompilerType field_type(getASTContext(), field->getType());
6979 child_indexes.push_back(child_idx);
6980 if (field_type.GetIndexOfChildMemberWithName(
6981 name, omit_empty_base_classes, child_indexes))
6982 return child_indexes.size();
6983 child_indexes.pop_back();
6984
6985 } else if (field_name.equals(name_sref)) {
6986 // We have to add on the number of base classes to this index!
6987 child_indexes.push_back(
6988 child_idx + ClangASTContext::GetNumBaseClasses(
6989 cxx_record_decl, omit_empty_base_classes));
6990 return child_indexes.size();
6991 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006992 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006993
Kate Stoneb9c1b512016-09-06 20:57:50 +00006994 if (cxx_record_decl) {
6995 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
6996
6997 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
6998
6999 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7000 // Didn't find things easily, lets let clang do its thang...
7001 clang::IdentifierInfo &ident_ref =
7002 getASTContext()->Idents.get(name_sref);
7003 clang::DeclarationName decl_name(&ident_ref);
7004
7005 clang::CXXBasePaths paths;
7006 if (cxx_record_decl->lookupInBases(
7007 [decl_name](const clang::CXXBaseSpecifier *specifier,
7008 clang::CXXBasePath &path) {
7009 return clang::CXXRecordDecl::FindOrdinaryMember(
7010 specifier, path, decl_name);
7011 },
7012 paths)) {
7013 clang::CXXBasePaths::const_paths_iterator path,
7014 path_end = paths.end();
7015 for (path = paths.begin(); path != path_end; ++path) {
7016 const size_t num_path_elements = path->size();
7017 for (size_t e = 0; e < num_path_elements; ++e) {
7018 clang::CXXBasePathElement elem = (*path)[e];
7019
7020 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7021 omit_empty_base_classes);
7022 if (child_idx == UINT32_MAX) {
7023 child_indexes.clear();
7024 return 0;
7025 } else {
7026 child_indexes.push_back(child_idx);
7027 parent_record_decl = llvm::cast<clang::RecordDecl>(
7028 elem.Base->getType()
7029 ->getAs<clang::RecordType>()
7030 ->getDecl());
7031 }
7032 }
7033 for (clang::NamedDecl *path_decl : path->Decls) {
7034 child_idx = GetIndexForRecordChild(
7035 parent_record_decl, path_decl, omit_empty_base_classes);
7036 if (child_idx == UINT32_MAX) {
7037 child_indexes.clear();
7038 return 0;
7039 } else {
7040 child_indexes.push_back(child_idx);
7041 }
7042 }
7043 }
7044 return child_indexes.size();
7045 }
7046 }
7047 }
7048 break;
7049
7050 case clang::Type::ObjCObject:
7051 case clang::Type::ObjCInterface:
7052 if (GetCompleteType(type)) {
7053 llvm::StringRef name_sref(name);
7054 const clang::ObjCObjectType *objc_class_type =
7055 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7056 assert(objc_class_type);
7057 if (objc_class_type) {
7058 uint32_t child_idx = 0;
7059 clang::ObjCInterfaceDecl *class_interface_decl =
7060 objc_class_type->getInterface();
7061
7062 if (class_interface_decl) {
7063 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7064 ivar_end = class_interface_decl->ivar_end();
7065 clang::ObjCInterfaceDecl *superclass_interface_decl =
7066 class_interface_decl->getSuperClass();
7067
7068 for (ivar_pos = class_interface_decl->ivar_begin();
7069 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7070 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7071
7072 if (ivar_decl->getName().equals(name_sref)) {
7073 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7074 (omit_empty_base_classes &&
7075 ObjCDeclHasIVars(superclass_interface_decl, true)))
7076 ++child_idx;
7077
7078 child_indexes.push_back(child_idx);
7079 return child_indexes.size();
7080 }
7081 }
7082
7083 if (superclass_interface_decl) {
Adrian Prantl05097242018-04-30 16:49:04 +00007084 // The super class index is always zero for ObjC classes, so we
7085 // push it onto the child indexes in case we find an ivar in our
7086 // superclass...
Kate Stoneb9c1b512016-09-06 20:57:50 +00007087 child_indexes.push_back(0);
7088
7089 CompilerType superclass_clang_type(
7090 getASTContext(), getASTContext()->getObjCInterfaceType(
7091 superclass_interface_decl));
7092 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7093 name, omit_empty_base_classes, child_indexes)) {
Adrian Prantl05097242018-04-30 16:49:04 +00007094 // We did find an ivar in a superclass so just return the
7095 // results!
Kate Stoneb9c1b512016-09-06 20:57:50 +00007096 return child_indexes.size();
7097 }
7098
Adrian Prantl05097242018-04-30 16:49:04 +00007099 // We didn't find an ivar matching "name" in our superclass, pop
7100 // the superclass zero index that we pushed on above.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007101 child_indexes.pop_back();
7102 }
7103 }
7104 }
7105 }
7106 break;
7107
7108 case clang::Type::ObjCObjectPointer: {
7109 CompilerType objc_object_clang_type(
7110 getASTContext(),
7111 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7112 ->getPointeeType());
7113 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7114 name, omit_empty_base_classes, child_indexes);
7115 } break;
7116
7117 case clang::Type::ConstantArray: {
7118 // const clang::ConstantArrayType *array =
7119 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7120 // const uint64_t element_count =
7121 // array->getSize().getLimitedValue();
7122 //
7123 // if (idx < element_count)
7124 // {
7125 // std::pair<uint64_t, unsigned> field_type_info =
7126 // ast->getTypeInfo(array->getElementType());
7127 //
7128 // char element_name[32];
7129 // ::snprintf (element_name, sizeof (element_name),
7130 // "%s[%u]", parent_name ? parent_name : "", idx);
7131 //
7132 // child_name.assign(element_name);
7133 // assert(field_type_info.first % 8 == 0);
7134 // child_byte_size = field_type_info.first / 8;
7135 // child_byte_offset = idx * child_byte_size;
7136 // return array->getElementType().getAsOpaquePtr();
7137 // }
7138 } break;
7139
7140 // case clang::Type::MemberPointerType:
7141 // {
7142 // MemberPointerType *mem_ptr_type =
7143 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7144 // clang::QualType pointee_type =
7145 // mem_ptr_type->getPointeeType();
7146 //
7147 // if (ClangASTContext::IsAggregateType
7148 // (pointee_type.getAsOpaquePtr()))
7149 // {
7150 // return GetIndexOfChildWithName (ast,
7151 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7152 // name);
7153 // }
7154 // }
7155 // break;
7156 //
7157 case clang::Type::LValueReference:
7158 case clang::Type::RValueReference: {
7159 const clang::ReferenceType *reference_type =
7160 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7161 clang::QualType pointee_type(reference_type->getPointeeType());
7162 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7163
7164 if (pointee_clang_type.IsAggregateType()) {
7165 return pointee_clang_type.GetIndexOfChildMemberWithName(
7166 name, omit_empty_base_classes, child_indexes);
7167 }
7168 } break;
7169
7170 case clang::Type::Pointer: {
7171 CompilerType pointee_clang_type(GetPointeeType(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::Typedef:
7180 return CompilerType(getASTContext(),
7181 llvm::cast<clang::TypedefType>(qual_type)
7182 ->getDecl()
7183 ->getUnderlyingType())
7184 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7185 child_indexes);
7186
7187 case clang::Type::Auto:
7188 return CompilerType(
7189 getASTContext(),
7190 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7191 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7192 child_indexes);
7193
7194 case clang::Type::Elaborated:
7195 return CompilerType(
7196 getASTContext(),
7197 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7198 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7199 child_indexes);
7200
7201 case clang::Type::Paren:
7202 return CompilerType(getASTContext(),
7203 llvm::cast<clang::ParenType>(qual_type)->desugar())
7204 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7205 child_indexes);
7206
7207 default:
7208 break;
7209 }
7210 }
7211 return 0;
7212}
Greg Claytond8d4a572015-08-11 21:38:15 +00007213
7214// Get the index of the child of "clang_type" whose name matches. This function
7215// doesn't descend into the children, but only looks one level deep and name
7216// matches can include base class names.
7217
7218uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007219ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7220 const char *name,
7221 bool omit_empty_base_classes) {
7222 if (type && name && name[0]) {
7223 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007224
Kate Stoneb9c1b512016-09-06 20:57:50 +00007225 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7226
7227 switch (type_class) {
7228 case clang::Type::Record:
7229 if (GetCompleteType(type)) {
7230 const clang::RecordType *record_type =
7231 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7232 const clang::RecordDecl *record_decl = record_type->getDecl();
7233
7234 assert(record_decl);
7235 uint32_t child_idx = 0;
7236
7237 const clang::CXXRecordDecl *cxx_record_decl =
7238 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7239
7240 if (cxx_record_decl) {
7241 clang::CXXRecordDecl::base_class_const_iterator base_class,
7242 base_class_end;
7243 for (base_class = cxx_record_decl->bases_begin(),
7244 base_class_end = cxx_record_decl->bases_end();
7245 base_class != base_class_end; ++base_class) {
7246 // Skip empty base classes
7247 clang::CXXRecordDecl *base_class_decl =
7248 llvm::cast<clang::CXXRecordDecl>(
7249 base_class->getType()
7250 ->getAs<clang::RecordType>()
7251 ->getDecl());
7252 if (omit_empty_base_classes &&
7253 ClangASTContext::RecordHasFields(base_class_decl) == false)
7254 continue;
7255
7256 CompilerType base_class_clang_type(getASTContext(),
7257 base_class->getType());
7258 std::string base_class_type_name(
7259 base_class_clang_type.GetTypeName().AsCString(""));
7260 if (base_class_type_name.compare(name) == 0)
7261 return child_idx;
7262 ++child_idx;
7263 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007264 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007265
Kate Stoneb9c1b512016-09-06 20:57:50 +00007266 // Try and find a field that matches NAME
7267 clang::RecordDecl::field_iterator field, field_end;
7268 llvm::StringRef name_sref(name);
7269 for (field = record_decl->field_begin(),
7270 field_end = record_decl->field_end();
7271 field != field_end; ++field, ++child_idx) {
7272 if (field->getName().equals(name_sref))
7273 return child_idx;
7274 }
7275 }
7276 break;
7277
7278 case clang::Type::ObjCObject:
7279 case clang::Type::ObjCInterface:
7280 if (GetCompleteType(type)) {
7281 llvm::StringRef name_sref(name);
7282 const clang::ObjCObjectType *objc_class_type =
7283 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7284 assert(objc_class_type);
7285 if (objc_class_type) {
7286 uint32_t child_idx = 0;
7287 clang::ObjCInterfaceDecl *class_interface_decl =
7288 objc_class_type->getInterface();
7289
7290 if (class_interface_decl) {
7291 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7292 ivar_end = class_interface_decl->ivar_end();
7293 clang::ObjCInterfaceDecl *superclass_interface_decl =
7294 class_interface_decl->getSuperClass();
7295
7296 for (ivar_pos = class_interface_decl->ivar_begin();
7297 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7298 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7299
7300 if (ivar_decl->getName().equals(name_sref)) {
7301 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7302 (omit_empty_base_classes &&
7303 ObjCDeclHasIVars(superclass_interface_decl, true)))
7304 ++child_idx;
7305
7306 return child_idx;
7307 }
7308 }
7309
7310 if (superclass_interface_decl) {
7311 if (superclass_interface_decl->getName().equals(name_sref))
7312 return 0;
7313 }
7314 }
7315 }
7316 }
7317 break;
7318
7319 case clang::Type::ObjCObjectPointer: {
7320 CompilerType pointee_clang_type(
7321 getASTContext(),
7322 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7323 ->getPointeeType());
7324 return pointee_clang_type.GetIndexOfChildWithName(
7325 name, omit_empty_base_classes);
7326 } break;
7327
7328 case clang::Type::ConstantArray: {
7329 // const clang::ConstantArrayType *array =
7330 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7331 // const uint64_t element_count =
7332 // array->getSize().getLimitedValue();
7333 //
7334 // if (idx < element_count)
7335 // {
7336 // std::pair<uint64_t, unsigned> field_type_info =
7337 // ast->getTypeInfo(array->getElementType());
7338 //
7339 // char element_name[32];
7340 // ::snprintf (element_name, sizeof (element_name),
7341 // "%s[%u]", parent_name ? parent_name : "", idx);
7342 //
7343 // child_name.assign(element_name);
7344 // assert(field_type_info.first % 8 == 0);
7345 // child_byte_size = field_type_info.first / 8;
7346 // child_byte_offset = idx * child_byte_size;
7347 // return array->getElementType().getAsOpaquePtr();
7348 // }
7349 } break;
7350
7351 // case clang::Type::MemberPointerType:
7352 // {
7353 // MemberPointerType *mem_ptr_type =
7354 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7355 // clang::QualType pointee_type =
7356 // mem_ptr_type->getPointeeType();
7357 //
7358 // if (ClangASTContext::IsAggregateType
7359 // (pointee_type.getAsOpaquePtr()))
7360 // {
7361 // return GetIndexOfChildWithName (ast,
7362 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7363 // name);
7364 // }
7365 // }
7366 // break;
7367 //
7368 case clang::Type::LValueReference:
7369 case clang::Type::RValueReference: {
7370 const clang::ReferenceType *reference_type =
7371 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7372 CompilerType pointee_type(getASTContext(),
7373 reference_type->getPointeeType());
7374
7375 if (pointee_type.IsAggregateType()) {
7376 return pointee_type.GetIndexOfChildWithName(name,
7377 omit_empty_base_classes);
7378 }
7379 } break;
7380
7381 case clang::Type::Pointer: {
7382 const clang::PointerType *pointer_type =
7383 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7384 CompilerType pointee_type(getASTContext(),
7385 pointer_type->getPointeeType());
7386
7387 if (pointee_type.IsAggregateType()) {
7388 return pointee_type.GetIndexOfChildWithName(name,
7389 omit_empty_base_classes);
7390 } else {
7391 // if (parent_name)
7392 // {
7393 // child_name.assign(1, '*');
7394 // child_name += parent_name;
7395 // }
7396 //
7397 // // We have a pointer to an simple type
7398 // if (idx == 0)
7399 // {
7400 // std::pair<uint64_t, unsigned> clang_type_info
7401 // = ast->getTypeInfo(pointee_type);
7402 // assert(clang_type_info.first % 8 == 0);
7403 // child_byte_size = clang_type_info.first / 8;
7404 // child_byte_offset = 0;
7405 // return pointee_type.getAsOpaquePtr();
7406 // }
7407 }
7408 } break;
7409
7410 case clang::Type::Auto:
7411 return CompilerType(
7412 getASTContext(),
7413 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7414 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7415
7416 case clang::Type::Elaborated:
7417 return CompilerType(
7418 getASTContext(),
7419 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7420 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7421
7422 case clang::Type::Paren:
7423 return CompilerType(getASTContext(),
7424 llvm::cast<clang::ParenType>(qual_type)->desugar())
7425 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7426
7427 case clang::Type::Typedef:
7428 return CompilerType(getASTContext(),
7429 llvm::cast<clang::TypedefType>(qual_type)
7430 ->getDecl()
7431 ->getUnderlyingType())
7432 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7433
7434 default:
7435 break;
7436 }
7437 }
7438 return UINT32_MAX;
7439}
Greg Claytond8d4a572015-08-11 21:38:15 +00007440
7441size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007442ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7443 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007444 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007445
Kate Stoneb9c1b512016-09-06 20:57:50 +00007446 clang::QualType qual_type(GetCanonicalQualType(type));
7447 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7448 switch (type_class) {
7449 case clang::Type::Record:
7450 if (GetCompleteType(type)) {
7451 const clang::CXXRecordDecl *cxx_record_decl =
7452 qual_type->getAsCXXRecordDecl();
7453 if (cxx_record_decl) {
7454 const clang::ClassTemplateSpecializationDecl *template_decl =
7455 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7456 cxx_record_decl);
7457 if (template_decl)
7458 return template_decl->getTemplateArgs().size();
7459 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007460 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007461 break;
7462
7463 case clang::Type::Typedef:
7464 return (CompilerType(getASTContext(),
7465 llvm::cast<clang::TypedefType>(qual_type)
7466 ->getDecl()
7467 ->getUnderlyingType()))
7468 .GetNumTemplateArguments();
7469
7470 case clang::Type::Auto:
7471 return (CompilerType(
7472 getASTContext(),
7473 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7474 .GetNumTemplateArguments();
7475
7476 case clang::Type::Elaborated:
7477 return (CompilerType(
7478 getASTContext(),
7479 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7480 .GetNumTemplateArguments();
7481
7482 case clang::Type::Paren:
7483 return (CompilerType(getASTContext(),
7484 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7485 .GetNumTemplateArguments();
7486
7487 default:
7488 break;
7489 }
7490
7491 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007492}
7493
Pavel Labath769b21e2017-11-13 14:26:21 +00007494const clang::ClassTemplateSpecializationDecl *
7495ClangASTContext::GetAsTemplateSpecialization(
7496 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007497 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007498 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007499
7500 clang::QualType qual_type(GetCanonicalQualType(type));
7501 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7502 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007503 case clang::Type::Record: {
7504 if (! GetCompleteType(type))
7505 return nullptr;
7506 const clang::CXXRecordDecl *cxx_record_decl =
7507 qual_type->getAsCXXRecordDecl();
7508 if (!cxx_record_decl)
7509 return nullptr;
7510 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7511 cxx_record_decl);
7512 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007513
7514 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007515 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7516 ->getDecl()
7517 ->getUnderlyingType()
7518 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007519
7520 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007521 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7522 ->getDeducedType()
7523 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007524
7525 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007526 return GetAsTemplateSpecialization(
7527 llvm::cast<clang::ElaboratedType>(qual_type)
7528 ->getNamedType()
7529 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007530
7531 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007532 return GetAsTemplateSpecialization(
7533 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007534
7535 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007536 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007537 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007538}
7539
7540lldb::TemplateArgumentKind
7541ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7542 size_t arg_idx) {
7543 const clang::ClassTemplateSpecializationDecl *template_decl =
7544 GetAsTemplateSpecialization(type);
7545 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7546 return eTemplateArgumentKindNull;
7547
7548 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7549 case clang::TemplateArgument::Null:
7550 return eTemplateArgumentKindNull;
7551
7552 case clang::TemplateArgument::NullPtr:
7553 return eTemplateArgumentKindNullPtr;
7554
7555 case clang::TemplateArgument::Type:
7556 return eTemplateArgumentKindType;
7557
7558 case clang::TemplateArgument::Declaration:
7559 return eTemplateArgumentKindDeclaration;
7560
7561 case clang::TemplateArgument::Integral:
7562 return eTemplateArgumentKindIntegral;
7563
7564 case clang::TemplateArgument::Template:
7565 return eTemplateArgumentKindTemplate;
7566
7567 case clang::TemplateArgument::TemplateExpansion:
7568 return eTemplateArgumentKindTemplateExpansion;
7569
7570 case clang::TemplateArgument::Expression:
7571 return eTemplateArgumentKindExpression;
7572
7573 case clang::TemplateArgument::Pack:
7574 return eTemplateArgumentKindPack;
7575 }
7576 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7577}
7578
7579CompilerType
7580ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7581 size_t idx) {
7582 const clang::ClassTemplateSpecializationDecl *template_decl =
7583 GetAsTemplateSpecialization(type);
7584 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7585 return CompilerType();
7586
7587 const clang::TemplateArgument &template_arg =
7588 template_decl->getTemplateArgs()[idx];
7589 if (template_arg.getKind() != clang::TemplateArgument::Type)
7590 return CompilerType();
7591
7592 return CompilerType(getASTContext(), template_arg.getAsType());
7593}
7594
Pavel Labathf59056f2017-11-30 10:16:54 +00007595llvm::Optional<CompilerType::IntegralTemplateArgument>
Pavel Labath769b21e2017-11-13 14:26:21 +00007596ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7597 size_t idx) {
7598 const clang::ClassTemplateSpecializationDecl *template_decl =
7599 GetAsTemplateSpecialization(type);
7600 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
Pavel Labathf59056f2017-11-30 10:16:54 +00007601 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007602
7603 const clang::TemplateArgument &template_arg =
7604 template_decl->getTemplateArgs()[idx];
7605 if (template_arg.getKind() != clang::TemplateArgument::Integral)
Pavel Labathf59056f2017-11-30 10:16:54 +00007606 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007607
Pavel Labathf59056f2017-11-30 10:16:54 +00007608 return {{template_arg.getAsIntegral(),
7609 CompilerType(getASTContext(), template_arg.getIntegralType())}};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007610}
7611
Kate Stoneb9c1b512016-09-06 20:57:50 +00007612CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7613 if (type)
7614 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7615 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007616}
7617
Kate Stoneb9c1b512016-09-06 20:57:50 +00007618clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7619 const clang::EnumType *enutype =
7620 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7621 if (enutype)
7622 return enutype->getDecl();
7623 return NULL;
7624}
7625
7626clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7627 const clang::RecordType *record_type =
7628 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7629 if (record_type)
7630 return record_type->getDecl();
7631 return nullptr;
7632}
7633
7634clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
7635 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
7636 if (qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00007637 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007638 else
7639 return qual_type->getAsTagDecl();
Greg Claytone6b36cd2015-12-08 01:02:08 +00007640}
7641
Greg Claytond8d4a572015-08-11 21:38:15 +00007642clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007643ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7644 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007645}
7646
7647clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007648ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7649 const clang::ObjCObjectType *objc_class_type =
7650 llvm::dyn_cast<clang::ObjCObjectType>(
7651 ClangUtil::GetCanonicalQualType(type));
7652 if (objc_class_type)
7653 return objc_class_type->getInterface();
7654 return nullptr;
7655}
7656
7657clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
7658 const CompilerType &type, const char *name,
7659 const CompilerType &field_clang_type, AccessType access,
7660 uint32_t bitfield_bit_size) {
7661 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007662 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007663 ClangASTContext *ast =
7664 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7665 if (!ast)
7666 return nullptr;
7667 clang::ASTContext *clang_ast = ast->getASTContext();
7668
7669 clang::FieldDecl *field = nullptr;
7670
7671 clang::Expr *bit_width = nullptr;
7672 if (bitfield_bit_size != 0) {
7673 llvm::APInt bitfield_bit_size_apint(
7674 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7675 bit_width = new (*clang_ast)
7676 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7677 clang_ast->IntTy, clang::SourceLocation());
7678 }
7679
7680 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7681 if (record_decl) {
7682 field = clang::FieldDecl::Create(
7683 *clang_ast, record_decl, clang::SourceLocation(),
7684 clang::SourceLocation(),
7685 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7686 ClangUtil::GetQualType(field_clang_type), // Field type
7687 nullptr, // TInfo *
7688 bit_width, // BitWidth
7689 false, // Mutable
7690 clang::ICIS_NoInit); // HasInit
7691
7692 if (!name) {
Adrian Prantl05097242018-04-30 16:49:04 +00007693 // Determine whether this field corresponds to an anonymous struct or
7694 // union.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007695 if (const clang::TagType *TagT =
7696 field->getType()->getAs<clang::TagType>()) {
7697 if (clang::RecordDecl *Rec =
7698 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7699 if (!Rec->getDeclName()) {
7700 Rec->setAnonymousStructOrUnion(true);
7701 field->setImplicit();
7702 }
7703 }
7704 }
7705
7706 if (field) {
7707 field->setAccess(
7708 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7709
7710 record_decl->addDecl(field);
7711
7712#ifdef LLDB_CONFIGURATION_DEBUG
7713 VerifyDecl(field);
7714#endif
7715 }
7716 } else {
7717 clang::ObjCInterfaceDecl *class_interface_decl =
7718 ast->GetAsObjCInterfaceDecl(type);
7719
7720 if (class_interface_decl) {
7721 const bool is_synthesized = false;
7722
7723 field_clang_type.GetCompleteType();
7724
7725 field = clang::ObjCIvarDecl::Create(
7726 *clang_ast, class_interface_decl, clang::SourceLocation(),
7727 clang::SourceLocation(),
7728 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7729 ClangUtil::GetQualType(field_clang_type), // Field type
7730 nullptr, // TypeSourceInfo *
7731 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7732 is_synthesized);
7733
7734 if (field) {
7735 class_interface_decl->addDecl(field);
7736
7737#ifdef LLDB_CONFIGURATION_DEBUG
7738 VerifyDecl(field);
7739#endif
7740 }
7741 }
7742 }
7743 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007744}
7745
Kate Stoneb9c1b512016-09-06 20:57:50 +00007746void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7747 if (!type)
7748 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007749
Kate Stoneb9c1b512016-09-06 20:57:50 +00007750 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7751 if (!ast)
7752 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007753
Kate Stoneb9c1b512016-09-06 20:57:50 +00007754 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007755
Kate Stoneb9c1b512016-09-06 20:57:50 +00007756 if (!record_decl)
7757 return;
7758
7759 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7760
7761 IndirectFieldVector indirect_fields;
7762 clang::RecordDecl::field_iterator field_pos;
7763 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7764 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7765 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7766 last_field_pos = field_pos++) {
7767 if (field_pos->isAnonymousStructOrUnion()) {
7768 clang::QualType field_qual_type = field_pos->getType();
7769
7770 const clang::RecordType *field_record_type =
7771 field_qual_type->getAs<clang::RecordType>();
7772
7773 if (!field_record_type)
7774 continue;
7775
7776 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7777
7778 if (!field_record_decl)
7779 continue;
7780
7781 for (clang::RecordDecl::decl_iterator
7782 di = field_record_decl->decls_begin(),
7783 de = field_record_decl->decls_end();
7784 di != de; ++di) {
7785 if (clang::FieldDecl *nested_field_decl =
7786 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7787 clang::NamedDecl **chain =
7788 new (*ast->getASTContext()) clang::NamedDecl *[2];
7789 chain[0] = *field_pos;
7790 chain[1] = nested_field_decl;
7791 clang::IndirectFieldDecl *indirect_field =
7792 clang::IndirectFieldDecl::Create(
7793 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7794 nested_field_decl->getIdentifier(),
7795 nested_field_decl->getType(), {chain, 2});
7796
7797 indirect_field->setImplicit();
7798
7799 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7800 field_pos->getAccess(), nested_field_decl->getAccess()));
7801
7802 indirect_fields.push_back(indirect_field);
7803 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7804 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7805 size_t nested_chain_size =
7806 nested_indirect_field_decl->getChainingSize();
7807 clang::NamedDecl **chain = new (*ast->getASTContext())
7808 clang::NamedDecl *[nested_chain_size + 1];
7809 chain[0] = *field_pos;
7810
7811 int chain_index = 1;
7812 for (clang::IndirectFieldDecl::chain_iterator
7813 nci = nested_indirect_field_decl->chain_begin(),
7814 nce = nested_indirect_field_decl->chain_end();
7815 nci < nce; ++nci) {
7816 chain[chain_index] = *nci;
7817 chain_index++;
7818 }
7819
7820 clang::IndirectFieldDecl *indirect_field =
7821 clang::IndirectFieldDecl::Create(
7822 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7823 nested_indirect_field_decl->getIdentifier(),
7824 nested_indirect_field_decl->getType(),
7825 {chain, nested_chain_size + 1});
7826
7827 indirect_field->setImplicit();
7828
7829 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7830 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
7831
7832 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00007833 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007834 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007835 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007836 }
7837
Adrian Prantl05097242018-04-30 16:49:04 +00007838 // Check the last field to see if it has an incomplete array type as its last
7839 // member and if it does, the tell the record decl about it
Kate Stoneb9c1b512016-09-06 20:57:50 +00007840 if (last_field_pos != field_end_pos) {
7841 if (last_field_pos->getType()->isIncompleteArrayType())
7842 record_decl->hasFlexibleArrayMember();
7843 }
7844
7845 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
7846 ife = indirect_fields.end();
7847 ifi < ife; ++ifi) {
7848 record_decl->addDecl(*ifi);
7849 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007850}
7851
Kate Stoneb9c1b512016-09-06 20:57:50 +00007852void ClangASTContext::SetIsPacked(const CompilerType &type) {
7853 if (type) {
7854 ClangASTContext *ast =
7855 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7856 if (ast) {
7857 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
7858
7859 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00007860 return;
7861
Kate Stoneb9c1b512016-09-06 20:57:50 +00007862 record_decl->addAttr(
7863 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00007864 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007865 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007866}
7867
Kate Stoneb9c1b512016-09-06 20:57:50 +00007868clang::VarDecl *ClangASTContext::AddVariableToRecordType(
7869 const CompilerType &type, const char *name, const CompilerType &var_type,
7870 AccessType access) {
7871 clang::VarDecl *var_decl = nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007872
Kate Stoneb9c1b512016-09-06 20:57:50 +00007873 if (!type.IsValid() || !var_type.IsValid())
7874 return nullptr;
7875 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7876 if (!ast)
7877 return nullptr;
7878
7879 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7880 if (record_decl) {
7881 var_decl = clang::VarDecl::Create(
7882 *ast->getASTContext(), // ASTContext &
7883 record_decl, // DeclContext *
7884 clang::SourceLocation(), // clang::SourceLocation StartLoc
7885 clang::SourceLocation(), // clang::SourceLocation IdLoc
7886 name ? &ast->getASTContext()->Idents.get(name)
7887 : nullptr, // clang::IdentifierInfo *
7888 ClangUtil::GetQualType(var_type), // Variable clang::QualType
7889 nullptr, // TypeSourceInfo *
7890 clang::SC_Static); // StorageClass
7891 if (var_decl) {
7892 var_decl->setAccess(
7893 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7894 record_decl->addDecl(var_decl);
7895
Greg Claytond8d4a572015-08-11 21:38:15 +00007896#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00007897 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00007898#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00007899 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007900 }
7901 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00007902}
7903
Kate Stoneb9c1b512016-09-06 20:57:50 +00007904clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
Davide Italiano675767a2018-03-27 19:40:50 +00007905 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00007906 const CompilerType &method_clang_type, lldb::AccessType access,
7907 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
7908 bool is_attr_used, bool is_artificial) {
7909 if (!type || !method_clang_type.IsValid() || name == nullptr ||
7910 name[0] == '\0')
7911 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007912
Kate Stoneb9c1b512016-09-06 20:57:50 +00007913 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007914
Kate Stoneb9c1b512016-09-06 20:57:50 +00007915 clang::CXXRecordDecl *cxx_record_decl =
7916 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007917
Kate Stoneb9c1b512016-09-06 20:57:50 +00007918 if (cxx_record_decl == nullptr)
7919 return nullptr;
7920
7921 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
7922
7923 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7924
7925 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
7926
7927 const clang::FunctionType *function_type =
7928 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7929
7930 if (function_type == nullptr)
7931 return nullptr;
7932
7933 const clang::FunctionProtoType *method_function_prototype(
7934 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7935
7936 if (!method_function_prototype)
7937 return nullptr;
7938
7939 unsigned int num_params = method_function_prototype->getNumParams();
7940
7941 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7942 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7943
7944 if (is_artificial)
7945 return nullptr; // skip everything artificial
7946
7947 if (name[0] == '~') {
7948 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
7949 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7950 clang::DeclarationNameInfo(
7951 getASTContext()->DeclarationNames.getCXXDestructorName(
7952 getASTContext()->getCanonicalType(record_qual_type)),
7953 clang::SourceLocation()),
7954 method_qual_type, nullptr, is_inline, is_artificial);
7955 cxx_method_decl = cxx_dtor_decl;
7956 } else if (decl_name == cxx_record_decl->getDeclName()) {
7957 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
7958 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7959 clang::DeclarationNameInfo(
7960 getASTContext()->DeclarationNames.getCXXConstructorName(
7961 getASTContext()->getCanonicalType(record_qual_type)),
7962 clang::SourceLocation()),
7963 method_qual_type,
7964 nullptr, // TypeSourceInfo *
7965 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
7966 cxx_method_decl = cxx_ctor_decl;
7967 } else {
7968 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7969 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7970
7971 if (IsOperator(name, op_kind)) {
7972 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
Adrian Prantl05097242018-04-30 16:49:04 +00007973 // Check the number of operator parameters. Sometimes we have seen bad
7974 // DWARF that doesn't correctly describe operators and if we try to
7975 // create a method and add it to the class, clang will assert and
7976 // crash, so we need to make sure things are acceptable.
Kate Stoneb9c1b512016-09-06 20:57:50 +00007977 const bool is_method = true;
7978 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
7979 is_method, op_kind, num_params))
7980 return nullptr;
7981 cxx_method_decl = clang::CXXMethodDecl::Create(
7982 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7983 clang::DeclarationNameInfo(
7984 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
7985 clang::SourceLocation()),
7986 method_qual_type,
7987 nullptr, // TypeSourceInfo *
7988 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
7989 } else if (num_params == 0) {
7990 // Conversion operators don't take params...
7991 cxx_method_decl = clang::CXXConversionDecl::Create(
7992 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7993 clang::DeclarationNameInfo(
7994 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
7995 getASTContext()->getCanonicalType(
7996 function_type->getReturnType())),
7997 clang::SourceLocation()),
7998 method_qual_type,
7999 nullptr, // TypeSourceInfo *
8000 is_inline, is_explicit, false /*is_constexpr*/,
8001 clang::SourceLocation());
8002 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008003 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008004
8005 if (cxx_method_decl == nullptr) {
8006 cxx_method_decl = clang::CXXMethodDecl::Create(
8007 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8008 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8009 method_qual_type,
8010 nullptr, // TypeSourceInfo *
8011 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008012 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008013 }
8014
8015 clang::AccessSpecifier access_specifier =
8016 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8017
8018 cxx_method_decl->setAccess(access_specifier);
8019 cxx_method_decl->setVirtualAsWritten(is_virtual);
8020
8021 if (is_attr_used)
8022 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8023
Davide Italiano675767a2018-03-27 19:40:50 +00008024 if (mangled_name != NULL) {
8025 cxx_method_decl->addAttr(
8026 clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name));
8027 }
8028
Kate Stoneb9c1b512016-09-06 20:57:50 +00008029 // Populate the method decl with parameter decls
8030
8031 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8032
8033 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8034 params.push_back(clang::ParmVarDecl::Create(
8035 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8036 clang::SourceLocation(),
8037 nullptr, // anonymous
8038 method_function_prototype->getParamType(param_index), nullptr,
8039 clang::SC_None, nullptr));
8040 }
8041
8042 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8043
8044 cxx_record_decl->addDecl(cxx_method_decl);
8045
8046 // Sometimes the debug info will mention a constructor (default/copy/move),
8047 // destructor, or assignment operator (copy/move) but there won't be any
8048 // version of this in the code. So we check if the function was artificially
8049 // generated and if it is trivial and this lets the compiler/backend know
8050 // that it can inline the IR for these when it needs to and we can avoid a
8051 // "missing function" error when running expressions.
8052
8053 if (is_artificial) {
8054 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8055 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8056 (cxx_ctor_decl->isCopyConstructor() &&
8057 cxx_record_decl->hasTrivialCopyConstructor()) ||
8058 (cxx_ctor_decl->isMoveConstructor() &&
8059 cxx_record_decl->hasTrivialMoveConstructor()))) {
8060 cxx_ctor_decl->setDefaulted();
8061 cxx_ctor_decl->setTrivial(true);
8062 } else if (cxx_dtor_decl) {
8063 if (cxx_record_decl->hasTrivialDestructor()) {
8064 cxx_dtor_decl->setDefaulted();
8065 cxx_dtor_decl->setTrivial(true);
8066 }
8067 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8068 cxx_record_decl->hasTrivialCopyAssignment()) ||
8069 (cxx_method_decl->isMoveAssignmentOperator() &&
8070 cxx_record_decl->hasTrivialMoveAssignment())) {
8071 cxx_method_decl->setDefaulted();
8072 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008073 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008074 }
8075
Greg Claytond8d4a572015-08-11 21:38:15 +00008076#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008077 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008078#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008079
Kate Stoneb9c1b512016-09-06 20:57:50 +00008080 // printf ("decl->isPolymorphic() = %i\n",
8081 // cxx_record_decl->isPolymorphic());
8082 // printf ("decl->isAggregate() = %i\n",
8083 // cxx_record_decl->isAggregate());
8084 // printf ("decl->isPOD() = %i\n",
8085 // cxx_record_decl->isPOD());
8086 // printf ("decl->isEmpty() = %i\n",
8087 // cxx_record_decl->isEmpty());
8088 // printf ("decl->isAbstract() = %i\n",
8089 // cxx_record_decl->isAbstract());
8090 // printf ("decl->hasTrivialConstructor() = %i\n",
8091 // cxx_record_decl->hasTrivialConstructor());
8092 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8093 // cxx_record_decl->hasTrivialCopyConstructor());
8094 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8095 // cxx_record_decl->hasTrivialCopyAssignment());
8096 // printf ("decl->hasTrivialDestructor() = %i\n",
8097 // cxx_record_decl->hasTrivialDestructor());
8098 return cxx_method_decl;
8099}
Greg Claytond8d4a572015-08-11 21:38:15 +00008100
8101#pragma mark C++ Base Classes
8102
8103clang::CXXBaseSpecifier *
Kate Stoneb9c1b512016-09-06 20:57:50 +00008104ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8105 AccessType access, bool is_virtual,
8106 bool base_of_class) {
8107 if (type)
8108 return new clang::CXXBaseSpecifier(
8109 clang::SourceRange(), is_virtual, base_of_class,
8110 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8111 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8112 clang::SourceLocation());
8113 return nullptr;
8114}
8115
8116void ClangASTContext::DeleteBaseClassSpecifiers(
8117 clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes) {
8118 for (unsigned i = 0; i < num_base_classes; ++i) {
8119 delete base_classes[i];
8120 base_classes[i] = nullptr;
8121 }
8122}
8123
8124bool ClangASTContext::SetBaseClassesForClassType(
8125 lldb::opaque_compiler_type_t type,
8126 clang::CXXBaseSpecifier const *const *base_classes,
8127 unsigned num_base_classes) {
8128 if (type) {
8129 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8130 if (cxx_record_decl) {
8131 cxx_record_decl->setBases(base_classes, num_base_classes);
8132 return true;
8133 }
8134 }
8135 return false;
8136}
8137
8138bool ClangASTContext::SetObjCSuperClass(
8139 const CompilerType &type, const CompilerType &superclass_clang_type) {
8140 ClangASTContext *ast =
8141 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8142 if (!ast)
8143 return false;
8144 clang::ASTContext *clang_ast = ast->getASTContext();
8145
8146 if (type && superclass_clang_type.IsValid() &&
8147 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8148 clang::ObjCInterfaceDecl *class_interface_decl =
8149 GetAsObjCInterfaceDecl(type);
8150 clang::ObjCInterfaceDecl *super_interface_decl =
8151 GetAsObjCInterfaceDecl(superclass_clang_type);
8152 if (class_interface_decl && super_interface_decl) {
8153 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8154 clang_ast->getObjCInterfaceType(super_interface_decl)));
8155 return true;
8156 }
8157 }
8158 return false;
8159}
8160
8161bool ClangASTContext::AddObjCClassProperty(
8162 const CompilerType &type, const char *property_name,
8163 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8164 const char *property_setter_name, const char *property_getter_name,
8165 uint32_t property_attributes, ClangASTMetadata *metadata) {
8166 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8167 property_name[0] == '\0')
8168 return false;
8169 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8170 if (!ast)
8171 return false;
8172 clang::ASTContext *clang_ast = ast->getASTContext();
8173
8174 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8175
8176 if (class_interface_decl) {
8177 CompilerType property_clang_type_to_access;
8178
8179 if (property_clang_type.IsValid())
8180 property_clang_type_to_access = property_clang_type;
8181 else if (ivar_decl)
8182 property_clang_type_to_access =
8183 CompilerType(clang_ast, ivar_decl->getType());
8184
8185 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8186 clang::TypeSourceInfo *prop_type_source;
8187 if (ivar_decl)
8188 prop_type_source =
8189 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8190 else
8191 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8192 ClangUtil::GetQualType(property_clang_type));
8193
8194 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8195 *clang_ast, class_interface_decl,
8196 clang::SourceLocation(), // Source Location
8197 &clang_ast->Idents.get(property_name),
8198 clang::SourceLocation(), // Source Location for AT
8199 clang::SourceLocation(), // Source location for (
8200 ivar_decl ? ivar_decl->getType()
8201 : ClangUtil::GetQualType(property_clang_type),
8202 prop_type_source);
8203
8204 if (property_decl) {
8205 if (metadata)
8206 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8207
8208 class_interface_decl->addDecl(property_decl);
8209
8210 clang::Selector setter_sel, getter_sel;
8211
8212 if (property_setter_name != nullptr) {
8213 std::string property_setter_no_colon(
8214 property_setter_name, strlen(property_setter_name) - 1);
8215 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008216 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008217 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8218 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8219 std::string setter_sel_string("set");
8220 setter_sel_string.push_back(::toupper(property_name[0]));
8221 setter_sel_string.append(&property_name[1]);
8222 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008223 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008224 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8225 }
8226 property_decl->setSetterName(setter_sel);
8227 property_decl->setPropertyAttributes(
8228 clang::ObjCPropertyDecl::OBJC_PR_setter);
8229
8230 if (property_getter_name != nullptr) {
8231 clang::IdentifierInfo *getter_ident =
8232 &clang_ast->Idents.get(property_getter_name);
8233 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8234 } else {
8235 clang::IdentifierInfo *getter_ident =
8236 &clang_ast->Idents.get(property_name);
8237 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8238 }
8239 property_decl->setGetterName(getter_sel);
8240 property_decl->setPropertyAttributes(
8241 clang::ObjCPropertyDecl::OBJC_PR_getter);
8242
8243 if (ivar_decl)
8244 property_decl->setPropertyIvarDecl(ivar_decl);
8245
8246 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8247 property_decl->setPropertyAttributes(
8248 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8249 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8250 property_decl->setPropertyAttributes(
8251 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8252 if (property_attributes & DW_APPLE_PROPERTY_assign)
8253 property_decl->setPropertyAttributes(
8254 clang::ObjCPropertyDecl::OBJC_PR_assign);
8255 if (property_attributes & DW_APPLE_PROPERTY_retain)
8256 property_decl->setPropertyAttributes(
8257 clang::ObjCPropertyDecl::OBJC_PR_retain);
8258 if (property_attributes & DW_APPLE_PROPERTY_copy)
8259 property_decl->setPropertyAttributes(
8260 clang::ObjCPropertyDecl::OBJC_PR_copy);
8261 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8262 property_decl->setPropertyAttributes(
8263 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8264 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8265 property_decl->setPropertyAttributes(
8266 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8267 if (property_attributes &
8268 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8269 property_decl->setPropertyAttributes(
8270 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8271 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8272 property_decl->setPropertyAttributes(
8273 clang::ObjCPropertyDecl::OBJC_PR_class);
8274
8275 const bool isInstance =
8276 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8277
8278 if (!getter_sel.isNull() &&
8279 !(isInstance
8280 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8281 : class_interface_decl->lookupClassMethod(getter_sel))) {
8282 const bool isVariadic = false;
8283 const bool isSynthesized = false;
8284 const bool isImplicitlyDeclared = true;
8285 const bool isDefined = false;
8286 const clang::ObjCMethodDecl::ImplementationControl impControl =
8287 clang::ObjCMethodDecl::None;
8288 const bool HasRelatedResultType = false;
8289
8290 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8291 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8292 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8293 nullptr, class_interface_decl, isInstance, isVariadic,
8294 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8295 HasRelatedResultType);
8296
8297 if (getter && metadata)
8298 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8299
8300 if (getter) {
8301 getter->setMethodParams(*clang_ast,
8302 llvm::ArrayRef<clang::ParmVarDecl *>(),
8303 llvm::ArrayRef<clang::SourceLocation>());
8304
8305 class_interface_decl->addDecl(getter);
8306 }
8307 }
8308
8309 if (!setter_sel.isNull() &&
8310 !(isInstance
8311 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8312 : class_interface_decl->lookupClassMethod(setter_sel))) {
8313 clang::QualType result_type = clang_ast->VoidTy;
8314 const bool isVariadic = false;
8315 const bool isSynthesized = false;
8316 const bool isImplicitlyDeclared = true;
8317 const bool isDefined = false;
8318 const clang::ObjCMethodDecl::ImplementationControl impControl =
8319 clang::ObjCMethodDecl::None;
8320 const bool HasRelatedResultType = false;
8321
8322 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8323 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8324 setter_sel, result_type, nullptr, class_interface_decl,
8325 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8326 isDefined, impControl, HasRelatedResultType);
8327
8328 if (setter && metadata)
8329 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8330
8331 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8332
8333 params.push_back(clang::ParmVarDecl::Create(
8334 *clang_ast, setter, clang::SourceLocation(),
8335 clang::SourceLocation(),
8336 nullptr, // anonymous
8337 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8338 clang::SC_Auto, nullptr));
8339
8340 if (setter) {
8341 setter->setMethodParams(
8342 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8343 llvm::ArrayRef<clang::SourceLocation>());
8344
8345 class_interface_decl->addDecl(setter);
8346 }
8347 }
8348
8349 return true;
8350 }
8351 }
8352 }
8353 return false;
8354}
8355
8356bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8357 bool check_superclass) {
8358 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8359 if (class_interface_decl)
8360 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8361 return false;
8362}
8363
8364clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8365 const CompilerType &type,
8366 const char *name, // the full symbol name as seen in the symbol table
8367 // (lldb::opaque_compiler_type_t type, "-[NString
8368 // stringWithCString:]")
8369 const CompilerType &method_clang_type, lldb::AccessType access,
8370 bool is_artificial, bool is_variadic) {
8371 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008372 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008373
Kate Stoneb9c1b512016-09-06 20:57:50 +00008374 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8375
8376 if (class_interface_decl == nullptr)
8377 return nullptr;
8378 ClangASTContext *lldb_ast =
8379 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8380 if (lldb_ast == nullptr)
8381 return nullptr;
8382 clang::ASTContext *ast = lldb_ast->getASTContext();
8383
8384 const char *selector_start = ::strchr(name, ' ');
8385 if (selector_start == nullptr)
8386 return nullptr;
8387
8388 selector_start++;
8389 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8390
8391 size_t len = 0;
8392 const char *start;
8393 // printf ("name = '%s'\n", name);
8394
8395 unsigned num_selectors_with_args = 0;
8396 for (start = selector_start; start && *start != '\0' && *start != ']';
8397 start += len) {
8398 len = ::strcspn(start, ":]");
8399 bool has_arg = (start[len] == ':');
8400 if (has_arg)
8401 ++num_selectors_with_args;
8402 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8403 if (has_arg)
8404 len += 1;
8405 }
8406
8407 if (selector_idents.size() == 0)
8408 return nullptr;
8409
8410 clang::Selector method_selector = ast->Selectors.getSelector(
8411 num_selectors_with_args ? selector_idents.size() : 0,
8412 selector_idents.data());
8413
8414 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8415
8416 // Populate the method decl with parameter decls
8417 const clang::Type *method_type(method_qual_type.getTypePtr());
8418
8419 if (method_type == nullptr)
8420 return nullptr;
8421
8422 const clang::FunctionProtoType *method_function_prototype(
8423 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8424
8425 if (!method_function_prototype)
8426 return nullptr;
8427
8428 bool is_synthesized = false;
8429 bool is_defined = false;
8430 clang::ObjCMethodDecl::ImplementationControl imp_control =
8431 clang::ObjCMethodDecl::None;
8432
8433 const unsigned num_args = method_function_prototype->getNumParams();
8434
8435 if (num_args != num_selectors_with_args)
8436 return nullptr; // some debug information is corrupt. We are not going to
8437 // deal with it.
8438
8439 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8440 *ast,
8441 clang::SourceLocation(), // beginLoc,
8442 clang::SourceLocation(), // endLoc,
8443 method_selector, method_function_prototype->getReturnType(),
8444 nullptr, // TypeSourceInfo *ResultTInfo,
8445 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8446 ClangUtil::GetQualType(type)),
8447 name[0] == '-', is_variadic, is_synthesized,
8448 true, // is_implicitly_declared; we force this to true because we don't
8449 // have source locations
8450 is_defined, imp_control, false /*has_related_result_type*/);
8451
8452 if (objc_method_decl == nullptr)
8453 return nullptr;
8454
8455 if (num_args > 0) {
8456 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8457
8458 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8459 params.push_back(clang::ParmVarDecl::Create(
8460 *ast, objc_method_decl, clang::SourceLocation(),
8461 clang::SourceLocation(),
8462 nullptr, // anonymous
8463 method_function_prototype->getParamType(param_index), nullptr,
8464 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008465 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008466
Kate Stoneb9c1b512016-09-06 20:57:50 +00008467 objc_method_decl->setMethodParams(
8468 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8469 llvm::ArrayRef<clang::SourceLocation>());
8470 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008471
Kate Stoneb9c1b512016-09-06 20:57:50 +00008472 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008473
Greg Claytond8d4a572015-08-11 21:38:15 +00008474#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008475 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008476#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008477
8478 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008479}
8480
Kate Stoneb9c1b512016-09-06 20:57:50 +00008481bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8482 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008483 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008484
Kate Stoneb9c1b512016-09-06 20:57:50 +00008485 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008486
Kate Stoneb9c1b512016-09-06 20:57:50 +00008487 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8488 switch (type_class) {
8489 case clang::Type::Record: {
8490 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8491 if (cxx_record_decl)
8492 return cxx_record_decl->hasExternalLexicalStorage() ||
8493 cxx_record_decl->hasExternalVisibleStorage();
8494 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008495
Kate Stoneb9c1b512016-09-06 20:57:50 +00008496 case clang::Type::Enum: {
8497 clang::EnumDecl *enum_decl =
8498 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8499 if (enum_decl)
8500 return enum_decl->hasExternalLexicalStorage() ||
8501 enum_decl->hasExternalVisibleStorage();
8502 } break;
8503
8504 case clang::Type::ObjCObject:
8505 case clang::Type::ObjCInterface: {
8506 const clang::ObjCObjectType *objc_class_type =
8507 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8508 assert(objc_class_type);
8509 if (objc_class_type) {
8510 clang::ObjCInterfaceDecl *class_interface_decl =
8511 objc_class_type->getInterface();
8512
8513 if (class_interface_decl)
8514 return class_interface_decl->hasExternalLexicalStorage() ||
8515 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008516 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008517 } break;
8518
8519 case clang::Type::Typedef:
8520 return GetHasExternalStorage(CompilerType(
8521 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8522 ->getDecl()
8523 ->getUnderlyingType()
8524 .getAsOpaquePtr()));
8525
8526 case clang::Type::Auto:
8527 return GetHasExternalStorage(CompilerType(
8528 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8529 ->getDeducedType()
8530 .getAsOpaquePtr()));
8531
8532 case clang::Type::Elaborated:
8533 return GetHasExternalStorage(CompilerType(
8534 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8535 ->getNamedType()
8536 .getAsOpaquePtr()));
8537
8538 case clang::Type::Paren:
8539 return GetHasExternalStorage(CompilerType(
8540 type.GetTypeSystem(),
8541 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8542
8543 default:
8544 break;
8545 }
8546 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008547}
8548
Kate Stoneb9c1b512016-09-06 20:57:50 +00008549bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8550 bool has_extern) {
8551 if (!type)
8552 return false;
8553
8554 clang::QualType qual_type(GetCanonicalQualType(type));
8555
8556 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8557 switch (type_class) {
8558 case clang::Type::Record: {
8559 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8560 if (cxx_record_decl) {
8561 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8562 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8563 return true;
8564 }
8565 } break;
8566
8567 case clang::Type::Enum: {
8568 clang::EnumDecl *enum_decl =
8569 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8570 if (enum_decl) {
8571 enum_decl->setHasExternalLexicalStorage(has_extern);
8572 enum_decl->setHasExternalVisibleStorage(has_extern);
8573 return true;
8574 }
8575 } break;
8576
8577 case clang::Type::ObjCObject:
8578 case clang::Type::ObjCInterface: {
8579 const clang::ObjCObjectType *objc_class_type =
8580 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8581 assert(objc_class_type);
8582 if (objc_class_type) {
8583 clang::ObjCInterfaceDecl *class_interface_decl =
8584 objc_class_type->getInterface();
8585
8586 if (class_interface_decl) {
8587 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8588 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8589 return true;
8590 }
8591 }
8592 } break;
8593
8594 case clang::Type::Typedef:
8595 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8596 ->getDecl()
8597 ->getUnderlyingType()
8598 .getAsOpaquePtr(),
8599 has_extern);
8600
8601 case clang::Type::Auto:
8602 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8603 ->getDeducedType()
8604 .getAsOpaquePtr(),
8605 has_extern);
8606
8607 case clang::Type::Elaborated:
8608 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8609 ->getNamedType()
8610 .getAsOpaquePtr(),
8611 has_extern);
8612
8613 case clang::Type::Paren:
8614 return SetHasExternalStorage(
8615 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8616 has_extern);
8617
8618 default:
8619 break;
8620 }
8621 return false;
8622}
Greg Claytond8d4a572015-08-11 21:38:15 +00008623
8624#pragma mark TagDecl
8625
Kate Stoneb9c1b512016-09-06 20:57:50 +00008626bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8627 clang::QualType qual_type(ClangUtil::GetQualType(type));
8628 if (!qual_type.isNull()) {
8629 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8630 if (tag_type) {
8631 clang::TagDecl *tag_decl = tag_type->getDecl();
8632 if (tag_decl) {
8633 tag_decl->startDefinition();
8634 return true;
8635 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008636 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008637
8638 const clang::ObjCObjectType *object_type =
8639 qual_type->getAs<clang::ObjCObjectType>();
8640 if (object_type) {
8641 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8642 if (interface_decl) {
8643 interface_decl->startDefinition();
8644 return true;
8645 }
8646 }
8647 }
8648 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008649}
8650
Kate Stoneb9c1b512016-09-06 20:57:50 +00008651bool ClangASTContext::CompleteTagDeclarationDefinition(
8652 const CompilerType &type) {
8653 clang::QualType qual_type(ClangUtil::GetQualType(type));
8654 if (!qual_type.isNull()) {
8655 // Make sure we use the same methodology as
Adrian Prantl05097242018-04-30 16:49:04 +00008656 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end
8657 // the definition. Previously we were calling
Kate Stoneb9c1b512016-09-06 20:57:50 +00008658 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8659 if (tag_type) {
8660 clang::TagDecl *tag_decl = tag_type->getDecl();
8661 if (tag_decl) {
8662 clang::CXXRecordDecl *cxx_record_decl =
8663 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8664
8665 if (cxx_record_decl) {
8666 if (!cxx_record_decl->isCompleteDefinition())
8667 cxx_record_decl->completeDefinition();
8668 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8669 cxx_record_decl->setHasExternalLexicalStorage(false);
8670 cxx_record_decl->setHasExternalVisibleStorage(false);
8671 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008672 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008673 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008674 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008675
8676 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8677
8678 if (enutype) {
8679 clang::EnumDecl *enum_decl = enutype->getDecl();
8680
8681 if (enum_decl) {
8682 if (!enum_decl->isCompleteDefinition()) {
8683 ClangASTContext *lldb_ast =
8684 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8685 if (lldb_ast == nullptr)
8686 return false;
8687 clang::ASTContext *ast = lldb_ast->getASTContext();
8688
8689 /// TODO This really needs to be fixed.
8690
8691 QualType integer_type(enum_decl->getIntegerType());
8692 if (!integer_type.isNull()) {
8693 unsigned NumPositiveBits = 1;
8694 unsigned NumNegativeBits = 0;
8695
8696 clang::QualType promotion_qual_type;
8697 // If the enum integer type is less than an integer in bit width,
8698 // then we must promote it to an integer size.
8699 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8700 ast->getTypeSize(ast->IntTy)) {
8701 if (enum_decl->getIntegerType()->isSignedIntegerType())
8702 promotion_qual_type = ast->IntTy;
8703 else
8704 promotion_qual_type = ast->UnsignedIntTy;
8705 } else
8706 promotion_qual_type = enum_decl->getIntegerType();
8707
8708 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8709 promotion_qual_type, NumPositiveBits,
8710 NumNegativeBits);
8711 }
8712 }
8713 return true;
8714 }
8715 }
8716 }
8717 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008718}
8719
Kate Stoneb9c1b512016-09-06 20:57:50 +00008720bool ClangASTContext::AddEnumerationValueToEnumerationType(
8721 lldb::opaque_compiler_type_t type,
8722 const CompilerType &enumerator_clang_type, const Declaration &decl,
8723 const char *name, int64_t enum_value, uint32_t enum_value_bit_size) {
8724 if (type && enumerator_clang_type.IsValid() && name && name[0]) {
8725 clang::QualType enum_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008726
Kate Stoneb9c1b512016-09-06 20:57:50 +00008727 bool is_signed = false;
8728 enumerator_clang_type.IsIntegerType(is_signed);
Greg Claytond8d4a572015-08-11 21:38:15 +00008729 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Kate Stoneb9c1b512016-09-06 20:57:50 +00008730 if (clang_type) {
8731 const clang::EnumType *enutype =
8732 llvm::dyn_cast<clang::EnumType>(clang_type);
8733
8734 if (enutype) {
8735 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8736 enum_llvm_apsint = enum_value;
8737 clang::EnumConstantDecl *enumerator_decl =
8738 clang::EnumConstantDecl::Create(
8739 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8740 name ? &getASTContext()->Idents.get(name)
8741 : nullptr, // Identifier
8742 ClangUtil::GetQualType(enumerator_clang_type),
8743 nullptr, enum_llvm_apsint);
8744
8745 if (enumerator_decl) {
8746 enutype->getDecl()->addDecl(enumerator_decl);
8747
8748#ifdef LLDB_CONFIGURATION_DEBUG
8749 VerifyDecl(enumerator_decl);
8750#endif
8751
8752 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008753 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008754 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008755 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008756 }
8757 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008758}
8759
Greg Claytona1e5dc82015-08-11 22:53:00 +00008760CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00008761ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8762 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8763 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8764 if (clang_type) {
8765 const clang::EnumType *enutype =
8766 llvm::dyn_cast<clang::EnumType>(clang_type);
8767 if (enutype) {
8768 clang::EnumDecl *enum_decl = enutype->getDecl();
8769 if (enum_decl)
8770 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008771 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008772 }
8773 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008774}
8775
Kate Stoneb9c1b512016-09-06 20:57:50 +00008776CompilerType
8777ClangASTContext::CreateMemberPointerType(const CompilerType &type,
8778 const CompilerType &pointee_type) {
8779 if (type && pointee_type.IsValid() &&
8780 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8781 ClangASTContext *ast =
8782 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8783 if (!ast)
8784 return CompilerType();
8785 return CompilerType(ast->getASTContext(),
8786 ast->getASTContext()->getMemberPointerType(
8787 ClangUtil::GetQualType(pointee_type),
8788 ClangUtil::GetQualType(type).getTypePtr()));
8789 }
8790 return CompilerType();
8791}
Greg Claytond8d4a572015-08-11 21:38:15 +00008792
8793size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00008794ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
8795 const char *s, uint8_t *dst,
8796 size_t dst_size) {
8797 if (type) {
8798 clang::QualType qual_type(GetCanonicalQualType(type));
8799 uint32_t count = 0;
8800 bool is_complex = false;
8801 if (IsFloatingPointType(type, count, is_complex)) {
8802 // TODO: handle complex and vector types
8803 if (count != 1)
8804 return false;
8805
8806 llvm::StringRef s_sref(s);
8807 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
8808 s_sref);
8809
8810 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
8811 const uint64_t byte_size = bit_size / 8;
8812 if (dst_size >= byte_size) {
8813 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
8814 llvm::NextPowerOf2(byte_size) * 8);
Zachary Turner97206d52017-05-12 04:51:55 +00008815 lldb_private::Status get_data_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008816 if (scalar.GetAsMemoryData(dst, byte_size,
8817 lldb_private::endian::InlHostByteOrder(),
8818 get_data_error))
8819 return byte_size;
8820 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008821 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008822 }
8823 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00008824}
8825
Greg Claytond8d4a572015-08-11 21:38:15 +00008826//----------------------------------------------------------------------
8827// Dumping types
8828//----------------------------------------------------------------------
8829#define DEPTH_INCREMENT 2
8830
Kate Stoneb9c1b512016-09-06 20:57:50 +00008831void ClangASTContext::DumpValue(
8832 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00008833 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008834 lldb::offset_t data_byte_offset, size_t data_byte_size,
8835 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
8836 bool show_summary, bool verbose, uint32_t depth) {
8837 if (!type)
8838 return;
8839
8840 clang::QualType qual_type(GetQualType(type));
8841 switch (qual_type->getTypeClass()) {
8842 case clang::Type::Record:
8843 if (GetCompleteType(type)) {
8844 const clang::RecordType *record_type =
8845 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8846 const clang::RecordDecl *record_decl = record_type->getDecl();
8847 assert(record_decl);
8848 uint32_t field_bit_offset = 0;
8849 uint32_t field_byte_offset = 0;
8850 const clang::ASTRecordLayout &record_layout =
8851 getASTContext()->getASTRecordLayout(record_decl);
8852 uint32_t child_idx = 0;
8853
8854 const clang::CXXRecordDecl *cxx_record_decl =
8855 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8856 if (cxx_record_decl) {
8857 // We might have base classes to print out first
8858 clang::CXXRecordDecl::base_class_const_iterator base_class,
8859 base_class_end;
8860 for (base_class = cxx_record_decl->bases_begin(),
8861 base_class_end = cxx_record_decl->bases_end();
8862 base_class != base_class_end; ++base_class) {
8863 const clang::CXXRecordDecl *base_class_decl =
8864 llvm::cast<clang::CXXRecordDecl>(
8865 base_class->getType()->getAs<clang::RecordType>()->getDecl());
8866
8867 // Skip empty base classes
8868 if (verbose == false &&
8869 ClangASTContext::RecordHasFields(base_class_decl) == false)
8870 continue;
8871
8872 if (base_class->isVirtual())
8873 field_bit_offset =
8874 record_layout.getVBaseClassOffset(base_class_decl)
8875 .getQuantity() *
8876 8;
8877 else
8878 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
8879 .getQuantity() *
8880 8;
8881 field_byte_offset = field_bit_offset / 8;
8882 assert(field_bit_offset % 8 == 0);
8883 if (child_idx == 0)
8884 s->PutChar('{');
8885 else
8886 s->PutChar(',');
8887
8888 clang::QualType base_class_qual_type = base_class->getType();
8889 std::string base_class_type_name(base_class_qual_type.getAsString());
8890
8891 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00008892 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
8893 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008894
8895 clang::TypeInfo base_class_type_info =
8896 getASTContext()->getTypeInfo(base_class_qual_type);
8897
8898 // Dump the value of the member
8899 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
8900 base_clang_type.DumpValue(
8901 exe_ctx,
8902 s, // Stream to dump to
8903 base_clang_type
8904 .GetFormat(), // The format with which to display the member
8905 data, // Data buffer containing all bytes for this type
8906 data_byte_offset + field_byte_offset, // Offset into "data" where
8907 // to grab value from
8908 base_class_type_info.Width / 8, // Size of this type in bytes
8909 0, // Bitfield bit size
8910 0, // Bitfield bit offset
8911 show_types, // Boolean indicating if we should show the variable
8912 // types
8913 show_summary, // Boolean indicating if we should show a summary
8914 // for the current type
8915 verbose, // Verbose output?
8916 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8917 // children
8918
8919 ++child_idx;
8920 }
8921 }
8922 uint32_t field_idx = 0;
8923 clang::RecordDecl::field_iterator field, field_end;
8924 for (field = record_decl->field_begin(),
8925 field_end = record_decl->field_end();
8926 field != field_end; ++field, ++field_idx, ++child_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00008927 // Print the starting squiggly bracket (if this is the first member) or
8928 // comma (for member 2 and beyond) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008929 if (child_idx == 0)
8930 s->PutChar('{');
8931 else
8932 s->PutChar(',');
8933
8934 // Indent
8935 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8936
8937 clang::QualType field_type = field->getType();
8938 // Print the member type if requested
Adrian Prantl05097242018-04-30 16:49:04 +00008939 // Figure out the type byte size (field_type_info.first) and alignment
8940 // (field_type_info.second) from the AST context.
Kate Stoneb9c1b512016-09-06 20:57:50 +00008941 clang::TypeInfo field_type_info =
8942 getASTContext()->getTypeInfo(field_type);
8943 assert(field_idx < record_layout.getFieldCount());
8944 // Figure out the field offset within the current struct/union/class
8945 // type
8946 field_bit_offset = record_layout.getFieldOffset(field_idx);
8947 field_byte_offset = field_bit_offset / 8;
8948 uint32_t field_bitfield_bit_size = 0;
8949 uint32_t field_bitfield_bit_offset = 0;
8950 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
8951 field_bitfield_bit_size))
8952 field_bitfield_bit_offset = field_bit_offset % 8;
8953
8954 if (show_types) {
8955 std::string field_type_name(field_type.getAsString());
8956 if (field_bitfield_bit_size > 0)
8957 s->Printf("(%s:%u) ", field_type_name.c_str(),
8958 field_bitfield_bit_size);
8959 else
8960 s->Printf("(%s) ", field_type_name.c_str());
8961 }
8962 // Print the member name and equal sign
8963 s->Printf("%s = ", field->getNameAsString().c_str());
8964
8965 // Dump the value of the member
8966 CompilerType field_clang_type(getASTContext(), field_type);
8967 field_clang_type.DumpValue(
8968 exe_ctx,
8969 s, // Stream to dump to
8970 field_clang_type
8971 .GetFormat(), // The format with which to display the member
8972 data, // Data buffer containing all bytes for this type
8973 data_byte_offset + field_byte_offset, // Offset into "data" where to
8974 // grab value from
8975 field_type_info.Width / 8, // Size of this type in bytes
8976 field_bitfield_bit_size, // Bitfield bit size
8977 field_bitfield_bit_offset, // Bitfield bit offset
8978 show_types, // Boolean indicating if we should show the variable
8979 // types
8980 show_summary, // Boolean indicating if we should show a summary for
8981 // the current type
8982 verbose, // Verbose output?
8983 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8984 // children
8985 }
8986
8987 // Indent the trailing squiggly bracket
8988 if (child_idx > 0)
8989 s->Printf("\n%*s}", depth, "");
8990 }
8991 return;
8992
8993 case clang::Type::Enum:
8994 if (GetCompleteType(type)) {
8995 const clang::EnumType *enutype =
8996 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8997 const clang::EnumDecl *enum_decl = enutype->getDecl();
8998 assert(enum_decl);
8999 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9000 lldb::offset_t offset = data_byte_offset;
9001 const int64_t enum_value = data.GetMaxU64Bitfield(
9002 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9003 for (enum_pos = enum_decl->enumerator_begin(),
9004 enum_end_pos = enum_decl->enumerator_end();
9005 enum_pos != enum_end_pos; ++enum_pos) {
9006 if (enum_pos->getInitVal() == enum_value) {
9007 s->Printf("%s", enum_pos->getNameAsString().c_str());
9008 return;
9009 }
9010 }
Adrian Prantl05097242018-04-30 16:49:04 +00009011 // If we have gotten here we didn't get find the enumerator in the enum
9012 // decl, so just print the integer.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009013 s->Printf("%" PRIi64, enum_value);
9014 }
9015 return;
9016
9017 case clang::Type::ConstantArray: {
9018 const clang::ConstantArrayType *array =
9019 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9020 bool is_array_of_characters = false;
9021 clang::QualType element_qual_type = array->getElementType();
9022
9023 const clang::Type *canonical_type =
9024 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9025 if (canonical_type)
9026 is_array_of_characters = canonical_type->isCharType();
9027
9028 const uint64_t element_count = array->getSize().getLimitedValue();
9029
9030 clang::TypeInfo field_type_info =
9031 getASTContext()->getTypeInfo(element_qual_type);
9032
9033 uint32_t element_idx = 0;
9034 uint32_t element_offset = 0;
9035 uint64_t element_byte_size = field_type_info.Width / 8;
9036 uint32_t element_stride = element_byte_size;
9037
9038 if (is_array_of_characters) {
9039 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009040 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9041 element_byte_size, element_count, UINT32_MAX,
9042 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009043 s->PutChar('"');
9044 return;
9045 } else {
9046 CompilerType element_clang_type(getASTContext(), element_qual_type);
9047 lldb::Format element_format = element_clang_type.GetFormat();
9048
9049 for (element_idx = 0; element_idx < element_count; ++element_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +00009050 // Print the starting squiggly bracket (if this is the first member) or
9051 // comman (for member 2 and beyong) for the struct/union/class member.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009052 if (element_idx == 0)
9053 s->PutChar('{');
9054 else
9055 s->PutChar(',');
9056
9057 // Indent and print the index
9058 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9059
9060 // Figure out the field offset within the current struct/union/class
9061 // type
9062 element_offset = element_idx * element_stride;
9063
9064 // Dump the value of the member
9065 element_clang_type.DumpValue(
9066 exe_ctx,
9067 s, // Stream to dump to
9068 element_format, // The format with which to display the element
9069 data, // Data buffer containing all bytes for this type
9070 data_byte_offset +
9071 element_offset, // Offset into "data" where to grab value from
9072 element_byte_size, // Size of this type in bytes
9073 0, // Bitfield bit size
9074 0, // Bitfield bit offset
9075 show_types, // Boolean indicating if we should show the variable
9076 // types
9077 show_summary, // Boolean indicating if we should show a summary for
9078 // the current type
9079 verbose, // Verbose output?
9080 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9081 // children
9082 }
9083
9084 // Indent the trailing squiggly bracket
9085 if (element_idx > 0)
9086 s->Printf("\n%*s}", depth, "");
9087 }
9088 }
9089 return;
9090
9091 case clang::Type::Typedef: {
9092 clang::QualType typedef_qual_type =
9093 llvm::cast<clang::TypedefType>(qual_type)
9094 ->getDecl()
9095 ->getUnderlyingType();
9096
9097 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9098 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9099 clang::TypeInfo typedef_type_info =
9100 getASTContext()->getTypeInfo(typedef_qual_type);
9101 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9102
9103 return typedef_clang_type.DumpValue(
9104 exe_ctx,
9105 s, // Stream to dump to
9106 typedef_format, // The format with which to display the element
9107 data, // Data buffer containing all bytes for this type
9108 data_byte_offset, // Offset into "data" where to grab value from
9109 typedef_byte_size, // Size of this type in bytes
9110 bitfield_bit_size, // Bitfield bit size
9111 bitfield_bit_offset, // Bitfield bit offset
9112 show_types, // Boolean indicating if we should show the variable types
9113 show_summary, // Boolean indicating if we should show a summary for the
9114 // current type
9115 verbose, // Verbose output?
9116 depth); // Scope depth for any types that have children
9117 } break;
9118
9119 case clang::Type::Auto: {
9120 clang::QualType elaborated_qual_type =
9121 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9122 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9123 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9124 clang::TypeInfo elaborated_type_info =
9125 getASTContext()->getTypeInfo(elaborated_qual_type);
9126 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9127
9128 return elaborated_clang_type.DumpValue(
9129 exe_ctx,
9130 s, // Stream to dump to
9131 elaborated_format, // The format with which to display the element
9132 data, // Data buffer containing all bytes for this type
9133 data_byte_offset, // Offset into "data" where to grab value from
9134 elaborated_byte_size, // Size of this type in bytes
9135 bitfield_bit_size, // Bitfield bit size
9136 bitfield_bit_offset, // Bitfield bit offset
9137 show_types, // Boolean indicating if we should show the variable types
9138 show_summary, // Boolean indicating if we should show a summary for the
9139 // current type
9140 verbose, // Verbose output?
9141 depth); // Scope depth for any types that have children
9142 } break;
9143
9144 case clang::Type::Elaborated: {
9145 clang::QualType elaborated_qual_type =
9146 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9147 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9148 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9149 clang::TypeInfo elaborated_type_info =
9150 getASTContext()->getTypeInfo(elaborated_qual_type);
9151 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9152
9153 return elaborated_clang_type.DumpValue(
9154 exe_ctx,
9155 s, // Stream to dump to
9156 elaborated_format, // The format with which to display the element
9157 data, // Data buffer containing all bytes for this type
9158 data_byte_offset, // Offset into "data" where to grab value from
9159 elaborated_byte_size, // Size of this type in bytes
9160 bitfield_bit_size, // Bitfield bit size
9161 bitfield_bit_offset, // Bitfield bit offset
9162 show_types, // Boolean indicating if we should show the variable types
9163 show_summary, // Boolean indicating if we should show a summary for the
9164 // current type
9165 verbose, // Verbose output?
9166 depth); // Scope depth for any types that have children
9167 } break;
9168
9169 case clang::Type::Paren: {
9170 clang::QualType desugar_qual_type =
9171 llvm::cast<clang::ParenType>(qual_type)->desugar();
9172 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9173
9174 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9175 clang::TypeInfo desugar_type_info =
9176 getASTContext()->getTypeInfo(desugar_qual_type);
9177 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9178
9179 return desugar_clang_type.DumpValue(
9180 exe_ctx,
9181 s, // Stream to dump to
9182 desugar_format, // The format with which to display the element
9183 data, // Data buffer containing all bytes for this type
9184 data_byte_offset, // Offset into "data" where to grab value from
9185 desugar_byte_size, // Size of this type in bytes
9186 bitfield_bit_size, // Bitfield bit size
9187 bitfield_bit_offset, // Bitfield bit offset
9188 show_types, // Boolean indicating if we should show the variable types
9189 show_summary, // Boolean indicating if we should show a summary for the
9190 // current type
9191 verbose, // Verbose output?
9192 depth); // Scope depth for any types that have children
9193 } break;
9194
9195 default:
9196 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009197 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9198 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9199 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009200
9201 if (show_summary)
9202 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9203 break;
9204 }
9205}
9206
9207bool ClangASTContext::DumpTypeValue(
9208 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009209 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9210 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009211 ExecutionContextScope *exe_scope) {
9212 if (!type)
9213 return false;
9214 if (IsAggregateType(type)) {
9215 return false;
9216 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009217 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009218
9219 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9220 switch (type_class) {
9221 case clang::Type::Typedef: {
9222 clang::QualType typedef_qual_type =
9223 llvm::cast<clang::TypedefType>(qual_type)
9224 ->getDecl()
9225 ->getUnderlyingType();
9226 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9227 if (format == eFormatDefault)
9228 format = typedef_clang_type.GetFormat();
9229 clang::TypeInfo typedef_type_info =
9230 getASTContext()->getTypeInfo(typedef_qual_type);
9231 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9232
9233 return typedef_clang_type.DumpTypeValue(
9234 s,
9235 format, // The format with which to display the element
9236 data, // Data buffer containing all bytes for this type
9237 byte_offset, // Offset into "data" where to grab value from
9238 typedef_byte_size, // Size of this type in bytes
9239 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9240 // treat as a bitfield
9241 bitfield_bit_offset, // Offset in bits of a bitfield value if
9242 // bitfield_bit_size != 0
9243 exe_scope);
9244 } break;
9245
9246 case clang::Type::Enum:
Adrian Prantl05097242018-04-30 16:49:04 +00009247 // If our format is enum or default, show the enumeration value as its
9248 // enumeration string value, else just display it as requested.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009249 if ((format == eFormatEnum || format == eFormatDefault) &&
9250 GetCompleteType(type)) {
9251 const clang::EnumType *enutype =
9252 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9253 const clang::EnumDecl *enum_decl = enutype->getDecl();
9254 assert(enum_decl);
9255 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9256 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9257 lldb::offset_t offset = byte_offset;
9258 if (is_signed) {
9259 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9260 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9261 for (enum_pos = enum_decl->enumerator_begin(),
9262 enum_end_pos = enum_decl->enumerator_end();
9263 enum_pos != enum_end_pos; ++enum_pos) {
9264 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009265 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009266 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009267 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009268 }
9269 // If we have gotten here we didn't get find the enumerator in the
9270 // enum decl, so just print the integer.
9271 s->Printf("%" PRIi64, enum_svalue);
9272 } else {
9273 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9274 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9275 for (enum_pos = enum_decl->enumerator_begin(),
9276 enum_end_pos = enum_decl->enumerator_end();
9277 enum_pos != enum_end_pos; ++enum_pos) {
9278 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009279 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009280 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009281 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009282 }
9283 // If we have gotten here we didn't get find the enumerator in the
9284 // enum decl, so just print the integer.
9285 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009286 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009287 return true;
9288 }
9289 // format was not enum, just fall through and dump the value as
9290 // requested....
9291 LLVM_FALLTHROUGH;
9292
9293 default:
9294 // We are down to a scalar type that we just need to display.
9295 {
9296 uint32_t item_count = 1;
9297 // A few formats, we might need to modify our size and count for
9298 // depending
9299 // on how we are trying to display the value...
9300 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009301 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009302 case eFormatBoolean:
9303 case eFormatBinary:
9304 case eFormatComplex:
9305 case eFormatCString: // NULL terminated C strings
9306 case eFormatDecimal:
9307 case eFormatEnum:
9308 case eFormatHex:
9309 case eFormatHexUppercase:
9310 case eFormatFloat:
9311 case eFormatOctal:
9312 case eFormatOSType:
9313 case eFormatUnsigned:
9314 case eFormatPointer:
9315 case eFormatVectorOfChar:
9316 case eFormatVectorOfSInt8:
9317 case eFormatVectorOfUInt8:
9318 case eFormatVectorOfSInt16:
9319 case eFormatVectorOfUInt16:
9320 case eFormatVectorOfSInt32:
9321 case eFormatVectorOfUInt32:
9322 case eFormatVectorOfSInt64:
9323 case eFormatVectorOfUInt64:
9324 case eFormatVectorOfFloat32:
9325 case eFormatVectorOfFloat64:
9326 case eFormatVectorOfUInt128:
9327 break;
9328
9329 case eFormatChar:
9330 case eFormatCharPrintable:
9331 case eFormatCharArray:
9332 case eFormatBytes:
9333 case eFormatBytesWithASCII:
9334 item_count = byte_size;
9335 byte_size = 1;
9336 break;
9337
9338 case eFormatUnicode16:
9339 item_count = byte_size / 2;
9340 byte_size = 2;
9341 break;
9342
9343 case eFormatUnicode32:
9344 item_count = byte_size / 4;
9345 byte_size = 4;
9346 break;
9347 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009348 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9349 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9350 bitfield_bit_size, bitfield_bit_offset,
9351 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009352 }
9353 break;
9354 }
9355 }
9356 return 0;
9357}
9358
9359void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9360 ExecutionContext *exe_ctx, Stream *s,
9361 const lldb_private::DataExtractor &data,
9362 lldb::offset_t data_byte_offset,
9363 size_t data_byte_size) {
9364 uint32_t length = 0;
9365 if (IsCStringType(type, length)) {
9366 if (exe_ctx) {
9367 Process *process = exe_ctx->GetProcessPtr();
9368 if (process) {
9369 lldb::offset_t offset = data_byte_offset;
9370 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9371 std::vector<uint8_t> buf;
9372 if (length > 0)
9373 buf.resize(length);
9374 else
9375 buf.resize(256);
9376
Zachary Turner29cb8682017-03-03 20:57:05 +00009377 DataExtractor cstr_data(&buf.front(), buf.size(),
9378 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009379 buf.back() = '\0';
9380 size_t bytes_read;
9381 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009382 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009383 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9384 buf.size(), error)) > 0) {
9385 const size_t len = strlen((const char *)&buf.front());
9386 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009387 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009388 if (total_cstr_len == 0)
9389 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009390 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9391 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009392 total_cstr_len += len;
9393 if (len < buf.size())
9394 break;
9395 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009396 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009397 if (total_cstr_len > 0)
9398 s->PutChar('"');
9399 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009400 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009401 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009402}
9403
Kate Stoneb9c1b512016-09-06 20:57:50 +00009404void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9405 StreamFile s(stdout, false);
9406 DumpTypeDescription(type, &s);
9407 ClangASTMetadata *metadata =
9408 ClangASTContext::GetMetadata(getASTContext(), type);
9409 if (metadata) {
9410 metadata->Dump(&s);
9411 }
9412}
Greg Claytond8d4a572015-08-11 21:38:15 +00009413
Kate Stoneb9c1b512016-09-06 20:57:50 +00009414void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9415 Stream *s) {
9416 if (type) {
9417 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009418
Kate Stoneb9c1b512016-09-06 20:57:50 +00009419 llvm::SmallVector<char, 1024> buf;
9420 llvm::raw_svector_ostream llvm_ostrm(buf);
9421
9422 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9423 switch (type_class) {
9424 case clang::Type::ObjCObject:
9425 case clang::Type::ObjCInterface: {
9426 GetCompleteType(type);
9427
9428 const clang::ObjCObjectType *objc_class_type =
9429 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9430 assert(objc_class_type);
9431 if (objc_class_type) {
9432 clang::ObjCInterfaceDecl *class_interface_decl =
9433 objc_class_type->getInterface();
9434 if (class_interface_decl) {
9435 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9436 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009437 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009438 }
9439 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009440
Kate Stoneb9c1b512016-09-06 20:57:50 +00009441 case clang::Type::Typedef: {
9442 const clang::TypedefType *typedef_type =
9443 qual_type->getAs<clang::TypedefType>();
9444 if (typedef_type) {
9445 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9446 std::string clang_typedef_name(
9447 typedef_decl->getQualifiedNameAsString());
9448 if (!clang_typedef_name.empty()) {
9449 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009450 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009451 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009452 }
9453 } break;
9454
9455 case clang::Type::Auto:
9456 CompilerType(getASTContext(),
9457 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9458 .DumpTypeDescription(s);
9459 return;
9460
9461 case clang::Type::Elaborated:
9462 CompilerType(getASTContext(),
9463 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9464 .DumpTypeDescription(s);
9465 return;
9466
9467 case clang::Type::Paren:
9468 CompilerType(getASTContext(),
9469 llvm::cast<clang::ParenType>(qual_type)->desugar())
9470 .DumpTypeDescription(s);
9471 return;
9472
9473 case clang::Type::Record: {
9474 GetCompleteType(type);
9475
9476 const clang::RecordType *record_type =
9477 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9478 const clang::RecordDecl *record_decl = record_type->getDecl();
9479 const clang::CXXRecordDecl *cxx_record_decl =
9480 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9481
9482 if (cxx_record_decl)
9483 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9484 s->GetIndentLevel());
9485 else
9486 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9487 s->GetIndentLevel());
9488 } break;
9489
9490 default: {
9491 const clang::TagType *tag_type =
9492 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9493 if (tag_type) {
9494 clang::TagDecl *tag_decl = tag_type->getDecl();
9495 if (tag_decl)
9496 tag_decl->print(llvm_ostrm, 0);
9497 } else {
9498 std::string clang_type_name(qual_type.getAsString());
9499 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009500 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009501 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009502 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009503 }
9504
Kate Stoneb9c1b512016-09-06 20:57:50 +00009505 if (buf.size() > 0) {
9506 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009507 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009508 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009509}
9510
Kate Stoneb9c1b512016-09-06 20:57:50 +00009511void ClangASTContext::DumpTypeName(const CompilerType &type) {
9512 if (ClangUtil::IsClangType(type)) {
9513 clang::QualType qual_type(
9514 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9515
9516 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9517 switch (type_class) {
9518 case clang::Type::Record: {
9519 const clang::CXXRecordDecl *cxx_record_decl =
9520 qual_type->getAsCXXRecordDecl();
9521 if (cxx_record_decl)
9522 printf("class %s", cxx_record_decl->getName().str().c_str());
9523 } break;
9524
9525 case clang::Type::Enum: {
9526 clang::EnumDecl *enum_decl =
9527 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9528 if (enum_decl) {
9529 printf("enum %s", enum_decl->getName().str().c_str());
9530 }
9531 } break;
9532
9533 case clang::Type::ObjCObject:
9534 case clang::Type::ObjCInterface: {
9535 const clang::ObjCObjectType *objc_class_type =
9536 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9537 if (objc_class_type) {
9538 clang::ObjCInterfaceDecl *class_interface_decl =
9539 objc_class_type->getInterface();
Adrian Prantl05097242018-04-30 16:49:04 +00009540 // We currently can't complete objective C types through the newly
9541 // added ASTContext because it only supports TagDecl objects right
9542 // now...
Kate Stoneb9c1b512016-09-06 20:57:50 +00009543 if (class_interface_decl)
9544 printf("@class %s", class_interface_decl->getName().str().c_str());
9545 }
9546 } break;
9547
9548 case clang::Type::Typedef:
9549 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9550 ->getDecl()
9551 ->getName()
9552 .str()
9553 .c_str());
9554 break;
9555
9556 case clang::Type::Auto:
9557 printf("auto ");
9558 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9559 llvm::cast<clang::AutoType>(qual_type)
9560 ->getDeducedType()
9561 .getAsOpaquePtr()));
9562
9563 case clang::Type::Elaborated:
9564 printf("elaborated ");
9565 return DumpTypeName(CompilerType(
9566 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9567 ->getNamedType()
9568 .getAsOpaquePtr()));
9569
9570 case clang::Type::Paren:
9571 printf("paren ");
9572 return DumpTypeName(CompilerType(
9573 type.GetTypeSystem(),
9574 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9575
9576 default:
9577 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9578 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009579 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009580 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009581}
9582
Kate Stoneb9c1b512016-09-06 20:57:50 +00009583clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9584 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9585 const char *parent_name, int tag_decl_kind,
9586 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9587 if (template_param_infos.IsValid()) {
9588 std::string template_basename(parent_name);
9589 template_basename.erase(template_basename.find('<'));
9590
9591 return CreateClassTemplateDecl(decl_ctx, access_type,
9592 template_basename.c_str(), tag_decl_kind,
9593 template_param_infos);
9594 }
9595 return NULL;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009596}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009597
Kate Stoneb9c1b512016-09-06 20:57:50 +00009598void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9599 ClangASTContext *ast = (ClangASTContext *)baton;
9600 SymbolFile *sym_file = ast->GetSymbolFile();
9601 if (sym_file) {
9602 CompilerType clang_type = GetTypeForDecl(decl);
9603 if (clang_type)
9604 sym_file->CompleteType(clang_type);
9605 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009606}
9607
Kate Stoneb9c1b512016-09-06 20:57:50 +00009608void ClangASTContext::CompleteObjCInterfaceDecl(
9609 void *baton, clang::ObjCInterfaceDecl *decl) {
9610 ClangASTContext *ast = (ClangASTContext *)baton;
9611 SymbolFile *sym_file = ast->GetSymbolFile();
9612 if (sym_file) {
9613 CompilerType clang_type = GetTypeForDecl(decl);
9614 if (clang_type)
9615 sym_file->CompleteType(clang_type);
9616 }
Zachary Turner42dff792016-04-15 00:21:26 +00009617}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009618
Kate Stoneb9c1b512016-09-06 20:57:50 +00009619DWARFASTParser *ClangASTContext::GetDWARFParser() {
9620 if (!m_dwarf_ast_parser_ap)
9621 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9622 return m_dwarf_ast_parser_ap.get();
9623}
9624
9625PDBASTParser *ClangASTContext::GetPDBParser() {
9626 if (!m_pdb_ast_parser_ap)
9627 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9628 return m_pdb_ast_parser_ap.get();
9629}
9630
9631bool ClangASTContext::LayoutRecordType(
9632 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9633 uint64_t &alignment,
9634 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9635 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9636 &base_offsets,
9637 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9638 &vbase_offsets) {
9639 ClangASTContext *ast = (ClangASTContext *)baton;
9640 DWARFASTParserClang *dwarf_ast_parser =
9641 (DWARFASTParserClang *)ast->GetDWARFParser();
9642 return dwarf_ast_parser->GetClangASTImporter().LayoutRecordType(
9643 record_decl, bit_size, alignment, field_offsets, base_offsets,
9644 vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009645}
9646
Greg Clayton99558cc42015-08-24 23:46:31 +00009647//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009648// CompilerDecl override functions
9649//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009650
Kate Stoneb9c1b512016-09-06 20:57:50 +00009651ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9652 if (opaque_decl) {
9653 clang::NamedDecl *nd =
9654 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9655 if (nd != nullptr)
9656 return ConstString(nd->getDeclName().getAsString());
9657 }
9658 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009659}
9660
Kate Stoneb9c1b512016-09-06 20:57:50 +00009661ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9662 if (opaque_decl) {
9663 clang::NamedDecl *nd =
9664 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9665 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9666 clang::MangleContext *mc = getMangleContext();
9667 if (mc && mc->shouldMangleCXXName(nd)) {
9668 llvm::SmallVector<char, 1024> buf;
9669 llvm::raw_svector_ostream llvm_ostrm(buf);
9670 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9671 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9672 Ctor_Complete, llvm_ostrm);
9673 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9674 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9675 Dtor_Complete, llvm_ostrm);
9676 } else {
9677 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009678 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009679 if (buf.size() > 0)
9680 return ConstString(buf.data(), buf.size());
9681 }
Greg Claytonfe689042015-11-10 17:47:04 +00009682 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009683 }
9684 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009685}
9686
Kate Stoneb9c1b512016-09-06 20:57:50 +00009687CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9688 if (opaque_decl)
9689 return CompilerDeclContext(this,
9690 ((clang::Decl *)opaque_decl)->getDeclContext());
9691 else
9692 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009693}
9694
Kate Stoneb9c1b512016-09-06 20:57:50 +00009695CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9696 if (clang::FunctionDecl *func_decl =
9697 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9698 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9699 if (clang::ObjCMethodDecl *objc_method =
9700 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9701 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9702 else
Greg Claytonfe689042015-11-10 17:47:04 +00009703 return CompilerType();
9704}
9705
Kate Stoneb9c1b512016-09-06 20:57:50 +00009706size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9707 if (clang::FunctionDecl *func_decl =
9708 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9709 return func_decl->param_size();
9710 if (clang::ObjCMethodDecl *objc_method =
9711 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9712 return objc_method->param_size();
9713 else
9714 return 0;
9715}
9716
9717CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9718 size_t idx) {
9719 if (clang::FunctionDecl *func_decl =
9720 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9721 if (idx < func_decl->param_size()) {
9722 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9723 if (var_decl)
9724 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9725 }
9726 } else if (clang::ObjCMethodDecl *objc_method =
9727 llvm::dyn_cast<clang::ObjCMethodDecl>(
9728 (clang::Decl *)opaque_decl)) {
9729 if (idx < objc_method->param_size())
9730 return CompilerType(
9731 this,
9732 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9733 }
9734 return CompilerType();
9735}
9736
Paul Hermand628cbb2015-09-15 23:44:17 +00009737//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00009738// CompilerDeclContext functions
9739//----------------------------------------------------------------------
9740
Kate Stoneb9c1b512016-09-06 20:57:50 +00009741std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9742 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9743 std::vector<CompilerDecl> found_decls;
9744 if (opaque_decl_ctx) {
9745 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9746 std::set<DeclContext *> searched;
9747 std::multimap<DeclContext *, DeclContext *> search_queue;
9748 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009749
Kate Stoneb9c1b512016-09-06 20:57:50 +00009750 for (clang::DeclContext *decl_context = root_decl_ctx;
9751 decl_context != nullptr && found_decls.empty();
9752 decl_context = decl_context->getParent()) {
9753 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +00009754
Kate Stoneb9c1b512016-09-06 20:57:50 +00009755 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9756 it++) {
9757 if (!searched.insert(it->second).second)
9758 continue;
9759 symbol_file->ParseDeclsForContext(
9760 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +00009761
Kate Stoneb9c1b512016-09-06 20:57:50 +00009762 for (clang::Decl *child : it->second->decls()) {
9763 if (clang::UsingDirectiveDecl *ud =
9764 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9765 if (ignore_using_decls)
9766 continue;
9767 clang::DeclContext *from = ud->getCommonAncestor();
9768 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9769 search_queue.insert(
9770 std::make_pair(from, ud->getNominatedNamespace()));
9771 } else if (clang::UsingDecl *ud =
9772 llvm::dyn_cast<clang::UsingDecl>(child)) {
9773 if (ignore_using_decls)
9774 continue;
9775 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9776 clang::Decl *target = usd->getTargetDecl();
9777 if (clang::NamedDecl *nd =
9778 llvm::dyn_cast<clang::NamedDecl>(target)) {
9779 IdentifierInfo *ii = nd->getIdentifier();
9780 if (ii != nullptr &&
9781 ii->getName().equals(name.AsCString(nullptr)))
9782 found_decls.push_back(CompilerDecl(this, nd));
9783 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009784 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009785 } else if (clang::NamedDecl *nd =
9786 llvm::dyn_cast<clang::NamedDecl>(child)) {
9787 IdentifierInfo *ii = nd->getIdentifier();
9788 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9789 found_decls.push_back(CompilerDecl(this, nd));
9790 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009791 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009792 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009793 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009794 }
9795 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +00009796}
9797
Dawn Perchikb5925782015-12-12 19:31:41 +00009798// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009799// and return the number of levels it took to find it, or
Adrian Prantl05097242018-04-30 16:49:04 +00009800// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
9801// declaration, its name and/or type, if set, will be used to check that the
9802// decl found in the scope is a match.
Dawn Perchikb5925782015-12-12 19:31:41 +00009803//
Kate Stoneb9c1b512016-09-06 20:57:50 +00009804// The optional name is required by languages (like C++) to handle using
Adrian Prantl05097242018-04-30 16:49:04 +00009805// declarations like:
Dawn Perchikb5925782015-12-12 19:31:41 +00009806//
9807// void poo();
9808// namespace ns {
9809// void foo();
9810// void goo();
9811// }
9812// void bar() {
9813// using ns::foo;
9814// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
9815// // LLDB_INVALID_DECL_LEVEL for 'goo'.
9816// }
9817//
9818// The optional type is useful in the case that there's a specific overload
9819// that we're looking for that might otherwise be shadowed, like:
9820//
9821// void foo(int);
9822// namespace ns {
9823// void foo();
9824// }
9825// void bar() {
9826// using ns::foo;
9827// // CountDeclLevels returns 0 for { 'foo', void() },
9828// // 1 for { 'foo', void(int) }, and
9829// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
9830// }
9831//
9832// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +00009833// function at file scope will return the same level as a function at global
Adrian Prantl05097242018-04-30 16:49:04 +00009834// scope. Ideally we'd like to treat the file scope as an additional scope just
9835// below the global scope. More work needs to be done to recognise that, if
9836// the decl we're trying to look up is static, we should compare its source
9837// file with that of the current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009838uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
9839 clang::DeclContext *child_decl_ctx,
9840 ConstString *child_name,
9841 CompilerType *child_type) {
9842 if (frame_decl_ctx) {
9843 std::set<DeclContext *> searched;
9844 std::multimap<DeclContext *, DeclContext *> search_queue;
9845 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +00009846
Kate Stoneb9c1b512016-09-06 20:57:50 +00009847 // Get the lookup scope for the decl we're trying to find.
9848 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +00009849
Kate Stoneb9c1b512016-09-06 20:57:50 +00009850 // Look for it in our scope's decl context and its parents.
9851 uint32_t level = 0;
9852 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
9853 decl_ctx = decl_ctx->getParent()) {
9854 if (!decl_ctx->isLookupContext())
9855 continue;
9856 if (decl_ctx == parent_decl_ctx)
9857 // Found it!
9858 return level;
9859 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
9860 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
9861 it++) {
9862 if (searched.find(it->second) != searched.end())
9863 continue;
9864
9865 // Currently DWARF has one shared translation unit for all Decls at top
Adrian Prantl05097242018-04-30 16:49:04 +00009866 // level, so this would erroneously find using statements anywhere. So
9867 // don't look at the top-level translation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009868 // TODO fix this and add a testcase that depends on it.
9869
9870 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
9871 continue;
9872
9873 searched.insert(it->second);
9874 symbol_file->ParseDeclsForContext(
9875 CompilerDeclContext(this, it->second));
9876
9877 for (clang::Decl *child : it->second->decls()) {
9878 if (clang::UsingDirectiveDecl *ud =
9879 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9880 clang::DeclContext *ns = ud->getNominatedNamespace();
9881 if (ns == parent_decl_ctx)
9882 // Found it!
9883 return level;
9884 clang::DeclContext *from = ud->getCommonAncestor();
9885 if (searched.find(ns) == searched.end())
9886 search_queue.insert(std::make_pair(from, ns));
9887 } else if (child_name) {
9888 if (clang::UsingDecl *ud =
9889 llvm::dyn_cast<clang::UsingDecl>(child)) {
9890 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9891 clang::Decl *target = usd->getTargetDecl();
9892 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
9893 if (!nd)
9894 continue;
9895 // Check names.
9896 IdentifierInfo *ii = nd->getIdentifier();
9897 if (ii == nullptr ||
9898 !ii->getName().equals(child_name->AsCString(nullptr)))
9899 continue;
9900 // Check types, if one was provided.
9901 if (child_type) {
9902 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
9903 if (!AreTypesSame(clang_type, *child_type,
9904 /*ignore_qualifiers=*/true))
9905 continue;
9906 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009907 // Found it!
9908 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009909 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009910 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009911 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009912 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009913 }
9914 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +00009915 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009916 }
9917 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +00009918}
9919
Kate Stoneb9c1b512016-09-06 20:57:50 +00009920bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
9921 if (opaque_decl_ctx)
9922 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
9923 else
Greg Clayton99558cc42015-08-24 23:46:31 +00009924 return false;
9925}
9926
Kate Stoneb9c1b512016-09-06 20:57:50 +00009927ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
9928 if (opaque_decl_ctx) {
9929 clang::NamedDecl *named_decl =
9930 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9931 if (named_decl)
9932 return ConstString(named_decl->getName());
9933 }
9934 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +00009935}
9936
Kate Stoneb9c1b512016-09-06 20:57:50 +00009937ConstString
9938ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
9939 if (opaque_decl_ctx) {
9940 clang::NamedDecl *named_decl =
9941 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9942 if (named_decl)
9943 return ConstString(
9944 llvm::StringRef(named_decl->getQualifiedNameAsString()));
9945 }
9946 return ConstString();
9947}
9948
9949bool ClangASTContext::DeclContextIsClassMethod(
9950 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
9951 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
9952 if (opaque_decl_ctx) {
9953 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9954 if (ObjCMethodDecl *objc_method =
9955 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
9956 if (is_instance_method_ptr)
9957 *is_instance_method_ptr = objc_method->isInstanceMethod();
9958 if (language_ptr)
9959 *language_ptr = eLanguageTypeObjC;
9960 if (language_object_name_ptr)
9961 language_object_name_ptr->SetCString("self");
9962 return true;
9963 } else if (CXXMethodDecl *cxx_method =
9964 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
9965 if (is_instance_method_ptr)
9966 *is_instance_method_ptr = cxx_method->isInstance();
9967 if (language_ptr)
9968 *language_ptr = eLanguageTypeC_plus_plus;
9969 if (language_object_name_ptr)
9970 language_object_name_ptr->SetCString("this");
9971 return true;
9972 } else if (clang::FunctionDecl *function_decl =
9973 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
9974 ClangASTMetadata *metadata =
9975 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
9976 if (metadata && metadata->HasObjectPtr()) {
9977 if (is_instance_method_ptr)
9978 *is_instance_method_ptr = true;
9979 if (language_ptr)
9980 *language_ptr = eLanguageTypeObjC;
9981 if (language_object_name_ptr)
9982 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
9983 return true;
9984 }
9985 }
9986 }
9987 return false;
9988}
9989
9990clang::DeclContext *
9991ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
9992 if (dc.IsClang())
9993 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
9994 return nullptr;
9995}
Greg Clayton99558cc42015-08-24 23:46:31 +00009996
9997ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00009998ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
9999 if (dc.IsClang())
10000 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10001 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10002 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010003}
10004
10005CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010006ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10007 if (dc.IsClang())
10008 return llvm::dyn_cast<clang::CXXMethodDecl>(
10009 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10010 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010011}
10012
10013clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010014ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10015 if (dc.IsClang())
10016 return llvm::dyn_cast<clang::FunctionDecl>(
10017 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10018 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010019}
10020
10021clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010022ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10023 if (dc.IsClang())
10024 return llvm::dyn_cast<clang::NamespaceDecl>(
10025 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10026 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010027}
10028
10029ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010030ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10031 const void *object) {
10032 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10033 if (ast)
10034 return ClangASTContext::GetMetadata(ast, object);
10035 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010036}
10037
10038clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010039ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10040 ClangASTContext *ast =
10041 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10042 if (ast)
10043 return ast->getASTContext();
10044 return nullptr;
10045}
10046
10047ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10048 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10049 m_target_wp(target.shared_from_this()),
10050 m_persistent_variables(new ClangPersistentVariables) {}
10051
10052UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010053 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010054 Expression::ResultType desired_type,
10055 const EvaluateExpressionOptions &options) {
10056 TargetSP target_sp = m_target_wp.lock();
10057 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010058 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010059
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010060 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010061 desired_type, options);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010062}
10063
Kate Stoneb9c1b512016-09-06 20:57:50 +000010064FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10065 const CompilerType &return_type, const Address &function_address,
10066 const ValueList &arg_value_list, const char *name) {
10067 TargetSP target_sp = m_target_wp.lock();
10068 if (!target_sp)
10069 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010070
Kate Stoneb9c1b512016-09-06 20:57:50 +000010071 Process *process = target_sp->GetProcessSP().get();
10072 if (!process)
10073 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010074
Kate Stoneb9c1b512016-09-06 20:57:50 +000010075 return new ClangFunctionCaller(*process, return_type, function_address,
10076 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010077}
10078
10079UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010080ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10081 const char *name) {
10082 TargetSP target_sp = m_target_wp.lock();
10083 if (!target_sp)
10084 return nullptr;
10085
10086 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010087}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010088
10089PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010090ClangASTContextForExpressions::GetPersistentExpressionState() {
10091 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010092}
Sean Callanan68e44232017-09-28 20:20:25 +000010093
10094clang::ExternalASTMerger &
10095ClangASTContextForExpressions::GetMergerUnchecked() {
Eugene Zemtsova9d928c2017-09-29 03:15:08 +000010096 lldbassert(m_scratch_ast_source_ap != nullptr);
Sean Callanan68e44232017-09-28 20:20:25 +000010097 return m_scratch_ast_source_ap->GetMergerUnchecked();
10098}