blob: df2d25c8d1ed1c5dad9cf109dba3ca847e4276cf [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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 // This is an operator, set the overloaded operator kind to invalid
167 // in case this is a conversion operator...
168 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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 // Set some properties which depend solely on the input kind; it would be nice
379 // to move these to the language standard, and have the driver resolve the
380 // 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;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
415 Opts.LineComment = Std.hasLineComments();
416 Opts.C99 = Std.isC99();
417 Opts.CPlusPlus = Std.isCPlusPlus();
418 Opts.CPlusPlus11 = Std.isCPlusPlus11();
419 Opts.Digraphs = Std.hasDigraphs();
420 Opts.GNUMode = Std.isGNUMode();
421 Opts.GNUInline = !Std.isC99();
422 Opts.HexFloats = Std.hasHexFloats();
423 Opts.ImplicitInt = Std.hasImplicitInt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424
Kate Stoneb9c1b512016-09-06 20:57:50 +0000425 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427 // OpenCL has some additional defaults.
Pavel Labath47168542017-04-27 08:49:19 +0000428 if (LangStd == LangStandard::lang_opencl10) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429 Opts.OpenCL = 1;
430 Opts.AltiVec = 1;
431 Opts.CXXOperatorNames = 1;
432 Opts.LaxVectorConversions = 1;
433 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434
Kate Stoneb9c1b512016-09-06 20:57:50 +0000435 // OpenCL and C++ both have bool, true, false keywords.
436 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
441 // is specified, or -std is set to a conforming mode.
442 Opts.Trigraphs = !Opts.GNUMode;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444 Opts.OptimizeSize = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 // FIXME: Eliminate this dependency.
447 // unsigned Opt =
448 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
449 // Opts.Optimize = Opt != 0;
450 unsigned Opt = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451
Kate Stoneb9c1b512016-09-06 20:57:50 +0000452 // This is the __NO_INLINE__ define, which just depends on things like the
453 // optimization level and -fno-inline, not actually whether the backend has
454 // inlining enabled.
455 //
456 // FIXME: This is affected by other options (-fno-inline).
457 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458}
459
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460ClangASTContext::ClangASTContext(const char *target_triple)
461 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_ap(),
462 m_language_options_ap(), m_source_manager_ap(), m_diagnostics_engine_ap(),
463 m_target_options_rp(), m_target_info_ap(), m_identifier_table_ap(),
464 m_selector_table_ap(), m_builtins_ap(), m_callback_tag_decl(nullptr),
465 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
466 m_pointer_byte_size(0), m_ast_owned(false) {
467 if (target_triple && target_triple[0])
468 SetTargetTriple(target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469}
470
471//----------------------------------------------------------------------
472// Destructor
473//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474ClangASTContext::~ClangASTContext() { Finalize(); }
475
476ConstString ClangASTContext::GetPluginNameStatic() {
477 return ConstString("clang");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478}
479
Kate Stoneb9c1b512016-09-06 20:57:50 +0000480ConstString ClangASTContext::GetPluginName() {
481 return ClangASTContext::GetPluginNameStatic();
Greg Clayton56939cb2015-09-17 22:23:34 +0000482}
483
Kate Stoneb9c1b512016-09-06 20:57:50 +0000484uint32_t ClangASTContext::GetPluginVersion() { return 1; }
Greg Clayton56939cb2015-09-17 22:23:34 +0000485
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
487 lldb_private::Module *module,
488 Target *target) {
489 if (ClangASTContextSupportsLanguage(language)) {
490 ArchSpec arch;
491 if (module)
492 arch = module->GetArchitecture();
493 else if (target)
494 arch = target->GetArchitecture();
Greg Clayton56939cb2015-09-17 22:23:34 +0000495
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496 if (arch.IsValid()) {
497 ArchSpec fixed_arch = arch;
498 // LLVM wants this to be set to iOS or MacOSX; if we're working on
499 // a bare-boards type image, change the triple for llvm's benefit.
500 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
501 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
502 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
503 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
504 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
505 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
506 } else {
507 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
Greg Clayton56939cb2015-09-17 22:23:34 +0000508 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000509 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000510
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511 if (module) {
512 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
513 if (ast_sp) {
514 ast_sp->SetArchitecture(fixed_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000515 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 return ast_sp;
517 } else if (target && target->IsValid()) {
518 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
519 new ClangASTContextForExpressions(*target));
520 if (ast_sp) {
521 ast_sp->SetArchitecture(fixed_arch);
522 ast_sp->m_scratch_ast_source_ap.reset(
523 new ClangASTSource(target->shared_from_this()));
Sean Callanan68e44232017-09-28 20:20:25 +0000524 lldbassert(ast_sp->getFileManager());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000525 ast_sp->m_scratch_ast_source_ap->InstallASTContext(
Sean Callanan68e44232017-09-28 20:20:25 +0000526 *ast_sp->getASTContext(), *ast_sp->getFileManager(), true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000527 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
528 ast_sp->m_scratch_ast_source_ap->CreateProxy());
529 ast_sp->SetExternalSource(proxy_ast_source);
530 return ast_sp;
531 }
532 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000533 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000534 }
535 return lldb::TypeSystemSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536}
537
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538void ClangASTContext::EnumerateSupportedLanguages(
539 std::set<lldb::LanguageType> &languages_for_types,
540 std::set<lldb::LanguageType> &languages_for_expressions) {
541 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
542 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
543 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
544 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
545 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
546 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
547
548 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
549 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
550 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
551 lldb::eLanguageTypeC_plus_plus_14});
552
553 languages_for_types.insert(s_supported_languages_for_types.begin(),
554 s_supported_languages_for_types.end());
555 languages_for_expressions.insert(
556 s_supported_languages_for_expressions.begin(),
557 s_supported_languages_for_expressions.end());
Enrico Granata5d84a692014-08-19 21:46:37 +0000558}
559
Kate Stoneb9c1b512016-09-06 20:57:50 +0000560void ClangASTContext::Initialize() {
561 PluginManager::RegisterPlugin(GetPluginNameStatic(),
562 "clang base AST context plug-in",
563 CreateInstance, EnumerateSupportedLanguages);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564}
565
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566void ClangASTContext::Terminate() {
567 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568}
569
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570void ClangASTContext::Finalize() {
571 if (m_ast_ap.get()) {
572 GetASTMap().Erase(m_ast_ap.get());
573 if (!m_ast_owned)
574 m_ast_ap.release();
575 }
576
577 m_builtins_ap.reset();
578 m_selector_table_ap.reset();
579 m_identifier_table_ap.reset();
580 m_target_info_ap.reset();
581 m_target_options_rp.reset();
582 m_diagnostics_engine_ap.reset();
583 m_source_manager_ap.reset();
584 m_language_options_ap.reset();
585 m_ast_ap.reset();
586 m_scratch_ast_source_ap.reset();
587}
588
589void ClangASTContext::Clear() {
590 m_ast_ap.reset();
591 m_language_options_ap.reset();
592 m_source_manager_ap.reset();
593 m_diagnostics_engine_ap.reset();
594 m_target_options_rp.reset();
595 m_target_info_ap.reset();
596 m_identifier_table_ap.reset();
597 m_selector_table_ap.reset();
598 m_builtins_ap.reset();
599 m_pointer_byte_size = 0;
600}
601
602const char *ClangASTContext::GetTargetTriple() {
603 return m_target_triple.c_str();
604}
605
606void ClangASTContext::SetTargetTriple(const char *target_triple) {
607 Clear();
608 m_target_triple.assign(target_triple);
609}
610
611void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
612 SetTargetTriple(arch.GetTriple().str().c_str());
613}
614
615bool ClangASTContext::HasExternalSource() {
616 ASTContext *ast = getASTContext();
617 if (ast)
618 return ast->getExternalSource() != nullptr;
619 return false;
620}
621
622void ClangASTContext::SetExternalSource(
623 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) {
624 ASTContext *ast = getASTContext();
625 if (ast) {
626 ast->setExternalSource(ast_source_ap);
627 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
628 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
629 }
630}
631
632void ClangASTContext::RemoveExternalSource() {
633 ASTContext *ast = getASTContext();
634
635 if (ast) {
636 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
637 ast->setExternalSource(empty_ast_source_ap);
638 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
639 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
640 }
641}
642
643void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
644 if (!m_ast_owned) {
645 m_ast_ap.release();
646 }
647 m_ast_owned = false;
648 m_ast_ap.reset(ast_ctx);
649 GetASTMap().Insert(ast_ctx, this);
650}
651
652ASTContext *ClangASTContext::getASTContext() {
653 if (m_ast_ap.get() == nullptr) {
654 m_ast_owned = true;
655 m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
656 *getIdentifierTable(), *getSelectorTable(),
657 *getBuiltinContext()));
658
659 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
660
661 // This can be NULL if we don't know anything about the architecture or if
662 // the
663 // target for an architecture isn't enabled in the llvm/clang that we built
664 TargetInfo *target_info = getTargetInfo();
665 if (target_info)
666 m_ast_ap->InitBuiltinTypes(*target_info);
667
668 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
669 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
670 // m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000671 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672
673 GetASTMap().Insert(m_ast_ap.get(), this);
674
675 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap(
676 new ClangExternalASTSourceCallbacks(
677 ClangASTContext::CompleteTagDecl,
678 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
679 ClangASTContext::LayoutRecordType, this));
680 SetExternalSource(ast_source_ap);
681 }
682 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683}
684
Kate Stoneb9c1b512016-09-06 20:57:50 +0000685ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
686 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
687 return clang_ast;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000688}
689
Kate Stoneb9c1b512016-09-06 20:57:50 +0000690Builtin::Context *ClangASTContext::getBuiltinContext() {
691 if (m_builtins_ap.get() == nullptr)
692 m_builtins_ap.reset(new Builtin::Context());
693 return m_builtins_ap.get();
Sean Callanan79439e82010-11-18 02:56:27 +0000694}
695
Kate Stoneb9c1b512016-09-06 20:57:50 +0000696IdentifierTable *ClangASTContext::getIdentifierTable() {
697 if (m_identifier_table_ap.get() == nullptr)
698 m_identifier_table_ap.reset(
699 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
700 return m_identifier_table_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701}
702
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703LangOptions *ClangASTContext::getLanguageOptions() {
704 if (m_language_options_ap.get() == nullptr) {
705 m_language_options_ap.reset(new LangOptions());
Richard Smith8186cd42017-04-26 22:10:53 +0000706 ParseLangArgs(*m_language_options_ap, InputKind::ObjCXX, GetTargetTriple());
707 // InitializeLangOptions(*m_language_options_ap, InputKind::ObjCXX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000708 }
709 return m_language_options_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000710}
711
Kate Stoneb9c1b512016-09-06 20:57:50 +0000712SelectorTable *ClangASTContext::getSelectorTable() {
713 if (m_selector_table_ap.get() == nullptr)
714 m_selector_table_ap.reset(new SelectorTable());
715 return m_selector_table_ap.get();
Greg Claytonfe689042015-11-10 17:47:04 +0000716}
717
Kate Stoneb9c1b512016-09-06 20:57:50 +0000718clang::FileManager *ClangASTContext::getFileManager() {
719 if (m_file_manager_ap.get() == nullptr) {
720 clang::FileSystemOptions file_system_options;
721 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
722 }
723 return m_file_manager_ap.get();
724}
725
726clang::SourceManager *ClangASTContext::getSourceManager() {
727 if (m_source_manager_ap.get() == nullptr)
728 m_source_manager_ap.reset(
729 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
730 return m_source_manager_ap.get();
731}
732
733clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
734 if (m_diagnostics_engine_ap.get() == nullptr) {
735 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
736 m_diagnostics_engine_ap.reset(
737 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
738 }
739 return m_diagnostics_engine_ap.get();
740}
741
742clang::MangleContext *ClangASTContext::getMangleContext() {
743 if (m_mangle_ctx_ap.get() == nullptr)
744 m_mangle_ctx_ap.reset(getASTContext()->createMangleContext());
745 return m_mangle_ctx_ap.get();
746}
747
748class NullDiagnosticConsumer : public DiagnosticConsumer {
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000749public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000750 NullDiagnosticConsumer() {
751 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
752 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000753
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
755 const clang::Diagnostic &info) {
756 if (m_log) {
757 llvm::SmallVector<char, 32> diag_str(10);
758 info.FormatDiagnostic(diag_str);
759 diag_str.push_back('\0');
760 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000761 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762 }
763
764 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
765 return new NullDiagnosticConsumer();
766 }
767
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000768private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769 Log *m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000770};
771
Kate Stoneb9c1b512016-09-06 20:57:50 +0000772DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
773 if (m_diagnostic_consumer_ap.get() == nullptr)
774 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
775
776 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000777}
778
Kate Stoneb9c1b512016-09-06 20:57:50 +0000779std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
780 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) {
781 m_target_options_rp = std::make_shared<clang::TargetOptions>();
782 if (m_target_options_rp.get() != nullptr)
783 m_target_options_rp->Triple = m_target_triple;
784 }
785 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000786}
787
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788TargetInfo *ClangASTContext::getTargetInfo() {
789 // target_triple should be something like "x86_64-apple-macosx"
790 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
791 m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
792 getTargetOptions()));
793 return m_target_info_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000794}
795
796#pragma mark Basic Types
797
Kate Stoneb9c1b512016-09-06 20:57:50 +0000798static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
799 ASTContext *ast, QualType qual_type) {
800 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
801 if (qual_type_bit_size == bit_size)
802 return true;
803 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000804}
Greg Clayton56939cb2015-09-17 22:23:34 +0000805
Greg Claytona1e5dc82015-08-11 22:53:00 +0000806CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000807ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
808 size_t bit_size) {
809 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
810 getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000811}
812
Kate Stoneb9c1b512016-09-06 20:57:50 +0000813CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
814 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
815 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000816 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000817 switch (encoding) {
818 case eEncodingInvalid:
819 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
820 return CompilerType(ast, ast->VoidPtrTy);
821 break;
822
823 case eEncodingUint:
824 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
825 return CompilerType(ast, ast->UnsignedCharTy);
826 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
827 return CompilerType(ast, ast->UnsignedShortTy);
828 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
829 return CompilerType(ast, ast->UnsignedIntTy);
830 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
831 return CompilerType(ast, ast->UnsignedLongTy);
832 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
833 return CompilerType(ast, ast->UnsignedLongLongTy);
834 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
835 return CompilerType(ast, ast->UnsignedInt128Ty);
836 break;
837
838 case eEncodingSint:
839 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
840 return CompilerType(ast, ast->SignedCharTy);
841 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
842 return CompilerType(ast, ast->ShortTy);
843 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
844 return CompilerType(ast, ast->IntTy);
845 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
846 return CompilerType(ast, ast->LongTy);
847 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
848 return CompilerType(ast, ast->LongLongTy);
849 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
850 return CompilerType(ast, ast->Int128Ty);
851 break;
852
853 case eEncodingIEEE754:
854 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
855 return CompilerType(ast, ast->FloatTy);
856 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
857 return CompilerType(ast, ast->DoubleTy);
858 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
859 return CompilerType(ast, ast->LongDoubleTy);
860 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
861 return CompilerType(ast, ast->HalfTy);
862 break;
863
864 case eEncodingVector:
865 // Sanity check that bit_size is a multiple of 8's.
866 if (bit_size && !(bit_size & 0x7u))
867 return CompilerType(
868 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
869 break;
870 }
871
872 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000873}
874
Greg Clayton57ee3062013-07-11 22:46:58 +0000875lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000876ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) {
877 if (name) {
878 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
879 static TypeNameToBasicTypeMap g_type_map;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000880 static llvm::once_flag g_once_flag;
881 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000882 // "void"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000883 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000884
885 // "char"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000886 g_type_map.Append(ConstString("char"), eBasicTypeChar);
887 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
888 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
889 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
890 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
891 g_type_map.Append(ConstString("unsigned wchar_t"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000892 eBasicTypeUnsignedWChar);
893 // "short"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000894 g_type_map.Append(ConstString("short"), eBasicTypeShort);
895 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
896 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
897 g_type_map.Append(ConstString("unsigned short int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000898 eBasicTypeUnsignedShort);
899
900 // "int"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000901 g_type_map.Append(ConstString("int"), eBasicTypeInt);
902 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
903 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
904 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000905
906 // "long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000907 g_type_map.Append(ConstString("long"), eBasicTypeLong);
908 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
909 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
910 g_type_map.Append(ConstString("unsigned long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000911 eBasicTypeUnsignedLong);
912
913 // "long long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000914 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
915 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
916 g_type_map.Append(ConstString("unsigned long long"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000917 eBasicTypeUnsignedLongLong);
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000918 g_type_map.Append(ConstString("unsigned long long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000919 eBasicTypeUnsignedLongLong);
920
921 // "int128"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000922 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
923 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000924
925 // Miscellaneous
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000926 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
927 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
928 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
929 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
930 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
931 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
932 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000933 g_type_map.Sort();
934 });
935
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000936 return g_type_map.Find(name, eBasicTypeInvalid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000937 }
938 return eBasicTypeInvalid;
Greg Clayton57ee3062013-07-11 22:46:58 +0000939}
940
Kate Stoneb9c1b512016-09-06 20:57:50 +0000941CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
942 const ConstString &name) {
943 if (ast) {
944 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
945 return ClangASTContext::GetBasicType(ast, basic_type);
946 }
947 return CompilerType();
948}
949
950uint32_t ClangASTContext::GetPointerByteSize() {
951 if (m_pointer_byte_size == 0)
952 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid)
953 .GetPointerType()
954 .GetByteSize(nullptr);
955 return m_pointer_byte_size;
956}
957
958CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
959 return GetBasicType(getASTContext(), basic_type);
960}
961
962CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
963 lldb::BasicType basic_type) {
964 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000965 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000966 lldb::opaque_compiler_type_t clang_type =
967 GetOpaqueCompilerType(ast, basic_type);
968
969 if (clang_type)
970 return CompilerType(GetASTContext(ast), clang_type);
971 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +0000972}
973
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
975 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
976 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +0000977
Kate Stoneb9c1b512016-09-06 20:57:50 +0000978#define streq(a, b) strcmp(a, b) == 0
979 assert(ast != nullptr);
980 if (ast) {
981 switch (dw_ate) {
982 default:
983 break;
Greg Clayton57ee3062013-07-11 22:46:58 +0000984
Kate Stoneb9c1b512016-09-06 20:57:50 +0000985 case DW_ATE_address:
986 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
987 return CompilerType(ast, ast->VoidPtrTy);
988 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +0000989
Kate Stoneb9c1b512016-09-06 20:57:50 +0000990 case DW_ATE_boolean:
991 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
992 return CompilerType(ast, ast->BoolTy);
993 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
994 return CompilerType(ast, ast->UnsignedCharTy);
995 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
996 return CompilerType(ast, ast->UnsignedShortTy);
997 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
998 return CompilerType(ast, ast->UnsignedIntTy);
999 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001000
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001 case DW_ATE_lo_user:
1002 // This has been seen to mean DW_AT_complex_integer
1003 if (type_name) {
1004 if (::strstr(type_name, "complex")) {
1005 CompilerType complex_int_clang_type =
1006 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1007 bit_size / 2);
1008 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1009 complex_int_clang_type)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001010 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001011 }
1012 break;
1013
1014 case DW_ATE_complex_float:
1015 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1016 return CompilerType(ast, ast->FloatComplexTy);
1017 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1018 return CompilerType(ast, ast->DoubleComplexTy);
1019 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1020 return CompilerType(ast, ast->LongDoubleComplexTy);
1021 else {
1022 CompilerType complex_float_clang_type =
1023 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1024 bit_size / 2);
1025 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1026 complex_float_clang_type)));
1027 }
1028 break;
1029
1030 case DW_ATE_float:
1031 if (streq(type_name, "float") &&
1032 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1033 return CompilerType(ast, ast->FloatTy);
1034 if (streq(type_name, "double") &&
1035 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1036 return CompilerType(ast, ast->DoubleTy);
1037 if (streq(type_name, "long double") &&
1038 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1039 return CompilerType(ast, ast->LongDoubleTy);
1040 // Fall back to not requiring a name match
1041 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1042 return CompilerType(ast, ast->FloatTy);
1043 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1044 return CompilerType(ast, ast->DoubleTy);
1045 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1046 return CompilerType(ast, ast->LongDoubleTy);
1047 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1048 return CompilerType(ast, ast->HalfTy);
1049 break;
1050
1051 case DW_ATE_signed:
1052 if (type_name) {
1053 if (streq(type_name, "wchar_t") &&
1054 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1055 (getTargetInfo() &&
1056 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1057 return CompilerType(ast, ast->WCharTy);
1058 if (streq(type_name, "void") &&
1059 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1060 return CompilerType(ast, ast->VoidTy);
1061 if (strstr(type_name, "long long") &&
1062 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1063 return CompilerType(ast, ast->LongLongTy);
1064 if (strstr(type_name, "long") &&
1065 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1066 return CompilerType(ast, ast->LongTy);
1067 if (strstr(type_name, "short") &&
1068 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1069 return CompilerType(ast, ast->ShortTy);
1070 if (strstr(type_name, "char")) {
1071 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1072 return CompilerType(ast, ast->CharTy);
1073 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1074 return CompilerType(ast, ast->SignedCharTy);
1075 }
1076 if (strstr(type_name, "int")) {
1077 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1078 return CompilerType(ast, ast->IntTy);
1079 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1080 return CompilerType(ast, ast->Int128Ty);
1081 }
1082 }
1083 // We weren't able to match up a type name, just search by size
1084 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1085 return CompilerType(ast, ast->CharTy);
1086 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1087 return CompilerType(ast, ast->ShortTy);
1088 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1089 return CompilerType(ast, ast->IntTy);
1090 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1091 return CompilerType(ast, ast->LongTy);
1092 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1093 return CompilerType(ast, ast->LongLongTy);
1094 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1095 return CompilerType(ast, ast->Int128Ty);
1096 break;
1097
1098 case DW_ATE_signed_char:
1099 if (ast->getLangOpts().CharIsSigned && type_name &&
1100 streq(type_name, "char")) {
1101 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1102 return CompilerType(ast, ast->CharTy);
1103 }
1104 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1105 return CompilerType(ast, ast->SignedCharTy);
1106 break;
1107
1108 case DW_ATE_unsigned:
1109 if (type_name) {
1110 if (streq(type_name, "wchar_t")) {
1111 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1112 if (!(getTargetInfo() &&
1113 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1114 return CompilerType(ast, ast->WCharTy);
1115 }
1116 }
1117 if (strstr(type_name, "long long")) {
1118 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1119 return CompilerType(ast, ast->UnsignedLongLongTy);
1120 } else if (strstr(type_name, "long")) {
1121 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1122 return CompilerType(ast, ast->UnsignedLongTy);
1123 } else if (strstr(type_name, "short")) {
1124 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1125 return CompilerType(ast, ast->UnsignedShortTy);
1126 } else if (strstr(type_name, "char")) {
1127 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1128 return CompilerType(ast, ast->UnsignedCharTy);
1129 } else if (strstr(type_name, "int")) {
1130 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1131 return CompilerType(ast, ast->UnsignedIntTy);
1132 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1133 return CompilerType(ast, ast->UnsignedInt128Ty);
1134 }
1135 }
1136 // We weren't able to match up a type name, just search by size
1137 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1138 return CompilerType(ast, ast->UnsignedCharTy);
1139 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1140 return CompilerType(ast, ast->UnsignedShortTy);
1141 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1142 return CompilerType(ast, ast->UnsignedIntTy);
1143 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1144 return CompilerType(ast, ast->UnsignedLongTy);
1145 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1146 return CompilerType(ast, ast->UnsignedLongLongTy);
1147 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1148 return CompilerType(ast, ast->UnsignedInt128Ty);
1149 break;
1150
1151 case DW_ATE_unsigned_char:
1152 if (!ast->getLangOpts().CharIsSigned && type_name &&
1153 streq(type_name, "char")) {
1154 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1155 return CompilerType(ast, ast->CharTy);
1156 }
1157 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1158 return CompilerType(ast, ast->UnsignedCharTy);
1159 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1160 return CompilerType(ast, ast->UnsignedShortTy);
1161 break;
1162
1163 case DW_ATE_imaginary_float:
1164 break;
1165
1166 case DW_ATE_UTF:
1167 if (type_name) {
1168 if (streq(type_name, "char16_t")) {
1169 return CompilerType(ast, ast->Char16Ty);
1170 } else if (streq(type_name, "char32_t")) {
1171 return CompilerType(ast, ast->Char32Ty);
1172 }
1173 }
1174 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001175 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001176 }
1177 // This assert should fire for anything that we don't catch above so we know
1178 // to fix any issues we run into.
1179 if (type_name) {
1180 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1181 "DW_TAG_base_type '%s' encoded with "
1182 "DW_ATE = 0x%x, bit_size = %u\n",
1183 type_name, dw_ate, bit_size);
1184 } else {
1185 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1186 "DW_TAG_base_type encoded with "
1187 "DW_ATE = 0x%x, bit_size = %u\n",
1188 dw_ate, bit_size);
1189 }
1190 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191}
1192
Kate Stoneb9c1b512016-09-06 20:57:50 +00001193CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1194 if (ast)
1195 return CompilerType(ast, ast->UnknownAnyTy);
1196 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001197}
1198
Kate Stoneb9c1b512016-09-06 20:57:50 +00001199CompilerType ClangASTContext::GetCStringType(bool is_const) {
1200 ASTContext *ast = getASTContext();
1201 QualType char_type(ast->CharTy);
1202
1203 if (is_const)
1204 char_type.addConst();
1205
1206 return CompilerType(ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001207}
1208
Sean Callanan09ab4b72011-11-30 22:11:59 +00001209clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001210ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1211 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001212}
1213
Kate Stoneb9c1b512016-09-06 20:57:50 +00001214clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1215 clang::Decl *source_decl) {
1216 FileSystemOptions file_system_options;
1217 FileManager file_manager(file_system_options);
1218 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1219
1220 return importer.Import(source_decl);
Greg Clayton526e5af2010-11-13 03:52:47 +00001221}
1222
Kate Stoneb9c1b512016-09-06 20:57:50 +00001223bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1224 bool ignore_qualifiers) {
1225 ClangASTContext *ast =
1226 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1227 if (!ast || ast != type2.GetTypeSystem())
1228 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001229
Kate Stoneb9c1b512016-09-06 20:57:50 +00001230 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1231 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001232
Kate Stoneb9c1b512016-09-06 20:57:50 +00001233 QualType type1_qual = ClangUtil::GetQualType(type1);
1234 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001235
Kate Stoneb9c1b512016-09-06 20:57:50 +00001236 if (ignore_qualifiers) {
1237 type1_qual = type1_qual.getUnqualifiedType();
1238 type2_qual = type2_qual.getUnqualifiedType();
1239 }
1240
1241 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001242}
1243
Kate Stoneb9c1b512016-09-06 20:57:50 +00001244CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1245 if (clang::ObjCInterfaceDecl *interface_decl =
1246 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1247 return GetTypeForDecl(interface_decl);
1248 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1249 return GetTypeForDecl(tag_decl);
1250 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001251}
1252
Kate Stoneb9c1b512016-09-06 20:57:50 +00001253CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
1254 // No need to call the getASTContext() accessor (which can create the AST
1255 // if it isn't created yet, because we can't have created a decl in this
1256 // AST if our AST didn't already exist...
1257 ASTContext *ast = &decl->getASTContext();
1258 if (ast)
1259 return CompilerType(ast, ast->getTagDeclType(decl));
1260 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001261}
1262
Kate Stoneb9c1b512016-09-06 20:57:50 +00001263CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
1264 // No need to call the getASTContext() accessor (which can create the AST
1265 // if it isn't created yet, because we can't have created a decl in this
1266 // AST if our AST didn't already exist...
1267 ASTContext *ast = &decl->getASTContext();
1268 if (ast)
1269 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1270 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001271}
1272
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001273#pragma mark Structure, Unions, Classes
1274
Kate Stoneb9c1b512016-09-06 20:57:50 +00001275CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1276 AccessType access_type,
1277 const char *name, int kind,
1278 LanguageType language,
1279 ClangASTMetadata *metadata) {
1280 ASTContext *ast = getASTContext();
1281 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001282
Kate Stoneb9c1b512016-09-06 20:57:50 +00001283 if (decl_ctx == nullptr)
1284 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001285
Kate Stoneb9c1b512016-09-06 20:57:50 +00001286 if (language == eLanguageTypeObjC ||
1287 language == eLanguageTypeObjC_plus_plus) {
1288 bool isForwardDecl = true;
1289 bool isInternal = false;
1290 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1291 }
Greg Clayton9e409562010-07-28 02:04:09 +00001292
Kate Stoneb9c1b512016-09-06 20:57:50 +00001293 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1294 // we will need to update this code. I was told to currently always use
1295 // the CXXRecordDecl class since we often don't know from debug information
1296 // if something is struct or a class, so we default to always use the more
1297 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001298
Kate Stoneb9c1b512016-09-06 20:57:50 +00001299 bool is_anonymous = (!name) || (!name[0]);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001300
Kate Stoneb9c1b512016-09-06 20:57:50 +00001301 CXXRecordDecl *decl = CXXRecordDecl::Create(
1302 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1303 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1304
1305 if (is_anonymous)
1306 decl->setAnonymousStructOrUnion(true);
1307
1308 if (decl) {
1309 if (metadata)
1310 SetMetadata(ast, decl, *metadata);
1311
1312 if (access_type != eAccessNone)
1313 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1314
1315 if (decl_ctx)
1316 decl_ctx->addDecl(decl);
1317
1318 return CompilerType(ast, ast->getTagDeclType(decl));
1319 }
1320 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001321}
1322
Sean Callanan09e91ac2017-05-11 22:08:05 +00001323namespace {
1324 bool IsValueParam(const clang::TemplateArgument &argument) {
1325 return argument.getKind() == TemplateArgument::Integral;
1326 }
1327}
1328
Kate Stoneb9c1b512016-09-06 20:57:50 +00001329static TemplateParameterList *CreateTemplateParameterList(
1330 ASTContext *ast,
1331 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1332 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1333 const bool parameter_pack = false;
1334 const bool is_typename = false;
1335 const unsigned depth = 0;
Sean Callanan09e91ac2017-05-11 22:08:05 +00001336 const size_t num_template_params = template_param_infos.args.size();
1337 DeclContext *const decl_context =
1338 ast->getTranslationUnitDecl(); // Is this the right decl context?,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001339 for (size_t i = 0; i < num_template_params; ++i) {
1340 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001341
Kate Stoneb9c1b512016-09-06 20:57:50 +00001342 IdentifierInfo *identifier_info = nullptr;
1343 if (name && name[0])
1344 identifier_info = &ast->Idents.get(name);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001345 if (IsValueParam(template_param_infos.args[i])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001346 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001347 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001348 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1349 template_param_infos.args[i].getIntegralType(), parameter_pack,
1350 nullptr));
1351
1352 } else {
1353 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001354 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001355 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1356 is_typename, parameter_pack));
1357 }
1358 }
Eugene Zemtsova9d928c2017-09-29 03:15:08 +00001359
Sean Callanan09e91ac2017-05-11 22:08:05 +00001360 if (template_param_infos.packed_args &&
1361 template_param_infos.packed_args->args.size()) {
1362 IdentifierInfo *identifier_info = nullptr;
1363 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1364 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1365 const bool parameter_pack_true = true;
1366 if (IsValueParam(template_param_infos.packed_args->args[0])) {
1367 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1368 *ast, decl_context,
1369 SourceLocation(), SourceLocation(), depth, num_template_params,
1370 identifier_info,
1371 template_param_infos.packed_args->args[0].getIntegralType(),
1372 parameter_pack_true, nullptr));
1373 } else {
1374 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1375 *ast, decl_context,
1376 SourceLocation(), SourceLocation(), depth, num_template_params,
1377 identifier_info,
1378 is_typename, parameter_pack_true));
1379 }
1380 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001381 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1382 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1383 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1384 SourceLocation(), requires_clause);
1385 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001386}
1387
Kate Stoneb9c1b512016-09-06 20:57:50 +00001388clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1389 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1390 const char *name, const TemplateParameterInfos &template_param_infos) {
1391 // /// \brief Create a function template node.
1392 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001393
Kate Stoneb9c1b512016-09-06 20:57:50 +00001394 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001395
Kate Stoneb9c1b512016-09-06 20:57:50 +00001396 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1397 ast, template_param_infos, template_param_decls);
1398 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1399 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1400 template_param_list, func_decl);
1401
1402 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1403 i < template_param_decl_count; ++i) {
1404 // TODO: verify which decl context we should put template_param_decls into..
1405 template_param_decls[i]->setDeclContext(func_decl);
1406 }
1407
1408 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001409}
1410
Kate Stoneb9c1b512016-09-06 20:57:50 +00001411void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1412 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1413 const TemplateParameterInfos &infos) {
1414 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001415
Kate Stoneb9c1b512016-09-06 20:57:50 +00001416 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1417 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001418}
1419
Kate Stoneb9c1b512016-09-06 20:57:50 +00001420ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1421 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1422 int kind, const TemplateParameterInfos &template_param_infos) {
1423 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001424
Kate Stoneb9c1b512016-09-06 20:57:50 +00001425 ClassTemplateDecl *class_template_decl = nullptr;
1426 if (decl_ctx == nullptr)
1427 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001428
Kate Stoneb9c1b512016-09-06 20:57:50 +00001429 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1430 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001431
Kate Stoneb9c1b512016-09-06 20:57:50 +00001432 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001433
Kate Stoneb9c1b512016-09-06 20:57:50 +00001434 for (NamedDecl *decl : result) {
1435 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001436 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001437 return class_template_decl;
1438 }
1439
1440 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1441
1442 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1443 ast, template_param_infos, template_param_decls);
1444
1445 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1446 *ast, (TagDecl::TagKind)kind,
1447 decl_ctx, // What decl context do we use here? TU? The actual decl
1448 // context?
1449 SourceLocation(), SourceLocation(), &identifier_info);
1450
1451 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1452 i < template_param_decl_count; ++i) {
1453 template_param_decls[i]->setDeclContext(template_cxx_decl);
1454 }
1455
1456 // With templated classes, we say that a class is templated with
1457 // specializations, but that the bare class has no functions.
1458 // template_cxx_decl->startDefinition();
1459 // template_cxx_decl->completeDefinition();
1460
1461 class_template_decl = ClassTemplateDecl::Create(
1462 *ast,
1463 decl_ctx, // What decl context do we use here? TU? The actual decl
1464 // context?
Pavel Labath4294de32017-01-12 10:44:16 +00001465 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001466
1467 if (class_template_decl) {
1468 if (access_type != eAccessNone)
1469 class_template_decl->setAccess(
1470 ConvertAccessTypeToAccessSpecifier(access_type));
1471
1472 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1473 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1474
1475 decl_ctx->addDecl(class_template_decl);
1476
Sean Callanan5e9e1992011-10-26 01:06:27 +00001477#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001478 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001479#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001480 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001481
Kate Stoneb9c1b512016-09-06 20:57:50 +00001482 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001483}
1484
Greg Claytonf0705c82011-10-22 03:33:13 +00001485ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001486ClangASTContext::CreateClassTemplateSpecializationDecl(
1487 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1488 const TemplateParameterInfos &template_param_infos) {
1489 ASTContext *ast = getASTContext();
Sean Callanan09e91ac2017-05-11 22:08:05 +00001490 llvm::SmallVector<clang::TemplateArgument, 2> args(
1491 template_param_infos.args.size() +
1492 (template_param_infos.packed_args ? 1 : 0));
1493 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1494 args.begin());
1495 if (template_param_infos.packed_args) {
1496 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1497 *ast, template_param_infos.packed_args->args);
1498 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001499 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1500 ClassTemplateSpecializationDecl::Create(
1501 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Sean Callanan09e91ac2017-05-11 22:08:05 +00001502 SourceLocation(), class_template_decl, args,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001503 nullptr);
1504
1505 class_template_specialization_decl->setSpecializationKind(
1506 TSK_ExplicitSpecialization);
1507
1508 return class_template_specialization_decl;
1509}
1510
1511CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1512 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1513 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001514 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001515 if (ast)
1516 return CompilerType(
1517 ast, ast->getTagDeclType(class_template_specialization_decl));
1518 }
1519 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001520}
1521
Kate Stoneb9c1b512016-09-06 20:57:50 +00001522static inline bool check_op_param(bool is_method,
1523 clang::OverloadedOperatorKind op_kind,
1524 bool unary, bool binary,
1525 uint32_t num_params) {
1526 // Special-case call since it can take any number of operands
1527 if (op_kind == OO_Call)
1528 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001529
Kate Stoneb9c1b512016-09-06 20:57:50 +00001530 // The parameter count doesn't include "this"
1531 if (is_method)
1532 ++num_params;
1533 if (num_params == 1)
1534 return unary;
1535 if (num_params == 2)
1536 return binary;
1537 else
Greg Clayton090d0982011-06-19 03:43:27 +00001538 return false;
1539}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001540
Kate Stoneb9c1b512016-09-06 20:57:50 +00001541bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1542 bool is_method, clang::OverloadedOperatorKind op_kind,
1543 uint32_t num_params) {
1544 switch (op_kind) {
1545 default:
1546 break;
1547 // C++ standard allows any number of arguments to new/delete
1548 case OO_New:
1549 case OO_Array_New:
1550 case OO_Delete:
1551 case OO_Array_Delete:
1552 return true;
1553 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001554
Kate Stoneb9c1b512016-09-06 20:57:50 +00001555#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1556 case OO_##Name: \
1557 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1558 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001559#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001560 default:
1561 break;
1562 }
1563 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001564}
1565
Greg Clayton57ee3062013-07-11 22:46:58 +00001566clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001567ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1568 clang::AccessSpecifier rhs) {
1569 // Make the access equal to the stricter of the field and the nested field's
1570 // access
1571 if (lhs == AS_none || rhs == AS_none)
1572 return AS_none;
1573 if (lhs == AS_private || rhs == AS_private)
1574 return AS_private;
1575 if (lhs == AS_protected || rhs == AS_protected)
1576 return AS_protected;
1577 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001578}
1579
Kate Stoneb9c1b512016-09-06 20:57:50 +00001580bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1581 uint32_t &bitfield_bit_size) {
1582 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001583}
1584
Kate Stoneb9c1b512016-09-06 20:57:50 +00001585bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1586 uint32_t &bitfield_bit_size) {
1587 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001588 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001589
Kate Stoneb9c1b512016-09-06 20:57:50 +00001590 if (field->isBitField()) {
1591 Expr *bit_width_expr = field->getBitWidth();
1592 if (bit_width_expr) {
1593 llvm::APSInt bit_width_apsint;
1594 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1595 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001596 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001597 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001598 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001599 }
1600 return false;
1601}
1602
1603bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1604 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001605 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001606
1607 if (!record_decl->field_empty())
1608 return true;
1609
1610 // No fields, lets check this is a CXX record and check the base classes
1611 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1612 if (cxx_record_decl) {
1613 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1614 for (base_class = cxx_record_decl->bases_begin(),
1615 base_class_end = cxx_record_decl->bases_end();
1616 base_class != base_class_end; ++base_class) {
1617 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1618 base_class->getType()->getAs<RecordType>()->getDecl());
1619 if (RecordHasFields(base_class_decl))
1620 return true;
1621 }
1622 }
1623 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001624}
1625
Greg Clayton8cf05932010-07-22 18:30:50 +00001626#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001627
Kate Stoneb9c1b512016-09-06 20:57:50 +00001628CompilerType ClangASTContext::CreateObjCClass(const char *name,
1629 DeclContext *decl_ctx,
1630 bool isForwardDecl,
1631 bool isInternal,
1632 ClangASTMetadata *metadata) {
1633 ASTContext *ast = getASTContext();
1634 assert(ast != nullptr);
1635 assert(name && name[0]);
1636 if (decl_ctx == nullptr)
1637 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001638
Kate Stoneb9c1b512016-09-06 20:57:50 +00001639 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1640 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1641 nullptr, SourceLocation(),
1642 /*isForwardDecl,*/
1643 isInternal);
1644
1645 if (decl && metadata)
1646 SetMetadata(ast, decl, *metadata);
1647
1648 return CompilerType(ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001649}
1650
Kate Stoneb9c1b512016-09-06 20:57:50 +00001651static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1652 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) ==
1653 false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001654}
1655
Greg Clayton57ee3062013-07-11 22:46:58 +00001656uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001657ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1658 bool omit_empty_base_classes) {
1659 uint32_t num_bases = 0;
1660 if (cxx_record_decl) {
1661 if (omit_empty_base_classes) {
1662 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1663 for (base_class = cxx_record_decl->bases_begin(),
1664 base_class_end = cxx_record_decl->bases_end();
1665 base_class != base_class_end; ++base_class) {
1666 // Skip empty base classes
1667 if (omit_empty_base_classes) {
1668 if (BaseSpecifierIsEmpty(base_class))
1669 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001670 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001671 ++num_bases;
1672 }
1673 } else
1674 num_bases = cxx_record_decl->getNumBases();
1675 }
1676 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001677}
1678
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001679#pragma mark Namespace Declarations
1680
1681NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001682ClangASTContext::GetUniqueNamespaceDeclaration(const char *name,
1683 DeclContext *decl_ctx) {
1684 NamespaceDecl *namespace_decl = nullptr;
1685 ASTContext *ast = getASTContext();
1686 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1687 if (decl_ctx == nullptr)
1688 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001689
Kate Stoneb9c1b512016-09-06 20:57:50 +00001690 if (name) {
1691 IdentifierInfo &identifier_info = ast->Idents.get(name);
1692 DeclarationName decl_name(&identifier_info);
1693 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1694 for (NamedDecl *decl : result) {
1695 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1696 if (namespace_decl)
1697 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001698 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001699
1700 namespace_decl =
1701 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1702 SourceLocation(), &identifier_info, nullptr);
1703
1704 decl_ctx->addDecl(namespace_decl);
1705 } else {
1706 if (decl_ctx == translation_unit_decl) {
1707 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1708 if (namespace_decl)
1709 return namespace_decl;
1710
1711 namespace_decl =
1712 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1713 SourceLocation(), nullptr, nullptr);
1714 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1715 translation_unit_decl->addDecl(namespace_decl);
1716 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1717 } else {
1718 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1719 if (parent_namespace_decl) {
1720 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1721 if (namespace_decl)
1722 return namespace_decl;
1723 namespace_decl =
1724 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1725 SourceLocation(), nullptr, nullptr);
1726 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1727 parent_namespace_decl->addDecl(namespace_decl);
1728 assert(namespace_decl ==
1729 parent_namespace_decl->getAnonymousNamespace());
1730 } else {
1731 // BAD!!!
1732 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001733 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001734 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001735#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001736 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001737#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001738 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001739}
1740
Kate Stoneb9c1b512016-09-06 20:57:50 +00001741NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1742 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) {
1743 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1744 if (ast_ctx == nullptr)
1745 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +00001746
Kate Stoneb9c1b512016-09-06 20:57:50 +00001747 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
Siva Chandra03ff5c82016-02-05 19:10:04 +00001748}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001749
Paul Hermand628cbb2015-09-15 23:44:17 +00001750clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001751ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1752 if (ctx != nullptr) {
1753 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1754 clang::SourceLocation());
1755 ctx->addDecl(decl);
1756 return decl;
1757 }
1758 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001759}
1760
Kate Stoneb9c1b512016-09-06 20:57:50 +00001761clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1762 clang::DeclContext *right,
1763 clang::DeclContext *root) {
1764 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001765 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001766
1767 std::set<clang::DeclContext *> path_left;
1768 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1769 path_left.insert(d);
1770
1771 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1772 if (path_left.find(d) != path_left.end())
1773 return d;
1774
1775 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00001776}
1777
Kate Stoneb9c1b512016-09-06 20:57:50 +00001778clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1779 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1780 if (decl_ctx != nullptr && ns_decl != nullptr) {
1781 clang::TranslationUnitDecl *translation_unit =
1782 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1783 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1784 *getASTContext(), decl_ctx, clang::SourceLocation(),
1785 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1786 clang::SourceLocation(), ns_decl,
1787 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
1788 decl_ctx->addDecl(using_decl);
1789 return using_decl;
1790 }
1791 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001792}
1793
1794clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001795ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1796 clang::NamedDecl *target) {
1797 if (current_decl_ctx != nullptr && target != nullptr) {
1798 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1799 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
1800 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1801 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1802 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
1803 target);
1804 using_decl->addShadowDecl(shadow_decl);
1805 current_decl_ctx->addDecl(using_decl);
1806 return using_decl;
1807 }
1808 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001809}
1810
Kate Stoneb9c1b512016-09-06 20:57:50 +00001811clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
1812 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
1813 if (decl_context != nullptr) {
1814 clang::VarDecl *var_decl = clang::VarDecl::Create(
1815 *getASTContext(), decl_context, clang::SourceLocation(),
1816 clang::SourceLocation(),
1817 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
1818 nullptr, clang::SC_None);
1819 var_decl->setAccess(clang::AS_public);
1820 decl_context->addDecl(var_decl);
1821 return var_decl;
1822 }
1823 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001824}
1825
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001826lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001827ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
1828 lldb::BasicType basic_type) {
1829 switch (basic_type) {
1830 case eBasicTypeVoid:
1831 return ast->VoidTy.getAsOpaquePtr();
1832 case eBasicTypeChar:
1833 return ast->CharTy.getAsOpaquePtr();
1834 case eBasicTypeSignedChar:
1835 return ast->SignedCharTy.getAsOpaquePtr();
1836 case eBasicTypeUnsignedChar:
1837 return ast->UnsignedCharTy.getAsOpaquePtr();
1838 case eBasicTypeWChar:
1839 return ast->getWCharType().getAsOpaquePtr();
1840 case eBasicTypeSignedWChar:
1841 return ast->getSignedWCharType().getAsOpaquePtr();
1842 case eBasicTypeUnsignedWChar:
1843 return ast->getUnsignedWCharType().getAsOpaquePtr();
1844 case eBasicTypeChar16:
1845 return ast->Char16Ty.getAsOpaquePtr();
1846 case eBasicTypeChar32:
1847 return ast->Char32Ty.getAsOpaquePtr();
1848 case eBasicTypeShort:
1849 return ast->ShortTy.getAsOpaquePtr();
1850 case eBasicTypeUnsignedShort:
1851 return ast->UnsignedShortTy.getAsOpaquePtr();
1852 case eBasicTypeInt:
1853 return ast->IntTy.getAsOpaquePtr();
1854 case eBasicTypeUnsignedInt:
1855 return ast->UnsignedIntTy.getAsOpaquePtr();
1856 case eBasicTypeLong:
1857 return ast->LongTy.getAsOpaquePtr();
1858 case eBasicTypeUnsignedLong:
1859 return ast->UnsignedLongTy.getAsOpaquePtr();
1860 case eBasicTypeLongLong:
1861 return ast->LongLongTy.getAsOpaquePtr();
1862 case eBasicTypeUnsignedLongLong:
1863 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1864 case eBasicTypeInt128:
1865 return ast->Int128Ty.getAsOpaquePtr();
1866 case eBasicTypeUnsignedInt128:
1867 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1868 case eBasicTypeBool:
1869 return ast->BoolTy.getAsOpaquePtr();
1870 case eBasicTypeHalf:
1871 return ast->HalfTy.getAsOpaquePtr();
1872 case eBasicTypeFloat:
1873 return ast->FloatTy.getAsOpaquePtr();
1874 case eBasicTypeDouble:
1875 return ast->DoubleTy.getAsOpaquePtr();
1876 case eBasicTypeLongDouble:
1877 return ast->LongDoubleTy.getAsOpaquePtr();
1878 case eBasicTypeFloatComplex:
1879 return ast->FloatComplexTy.getAsOpaquePtr();
1880 case eBasicTypeDoubleComplex:
1881 return ast->DoubleComplexTy.getAsOpaquePtr();
1882 case eBasicTypeLongDoubleComplex:
1883 return ast->LongDoubleComplexTy.getAsOpaquePtr();
1884 case eBasicTypeObjCID:
1885 return ast->getObjCIdType().getAsOpaquePtr();
1886 case eBasicTypeObjCClass:
1887 return ast->getObjCClassType().getAsOpaquePtr();
1888 case eBasicTypeObjCSel:
1889 return ast->getObjCSelType().getAsOpaquePtr();
1890 case eBasicTypeNullPtr:
1891 return ast->NullPtrTy.getAsOpaquePtr();
1892 default:
1893 return nullptr;
1894 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001895}
1896
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001897#pragma mark Function Types
1898
Pavel Labath1ac2b202016-08-15 14:32:32 +00001899clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00001900ClangASTContext::GetDeclarationName(const char *name,
1901 const CompilerType &function_clang_type) {
1902 if (!name || !name[0])
1903 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00001904
Kate Stoneb9c1b512016-09-06 20:57:50 +00001905 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
1906 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
1907 return DeclarationName(&getASTContext()->Idents.get(
1908 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00001909
Kate Stoneb9c1b512016-09-06 20:57:50 +00001910 // Check the number of operator parameters. Sometimes we have
1911 // seen bad DWARF that doesn't correctly describe operators and
1912 // if we try to create a method and add it to the class, clang
1913 // will assert and crash, so we need to make sure things are
1914 // acceptable.
1915 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
1916 const clang::FunctionProtoType *function_type =
1917 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
1918 if (function_type == nullptr)
1919 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00001920
Kate Stoneb9c1b512016-09-06 20:57:50 +00001921 const bool is_method = false;
1922 const unsigned int num_params = function_type->getNumParams();
1923 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1924 is_method, op_kind, num_params))
1925 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00001926
Kate Stoneb9c1b512016-09-06 20:57:50 +00001927 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00001928}
1929
Kate Stoneb9c1b512016-09-06 20:57:50 +00001930FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
1931 DeclContext *decl_ctx, const char *name,
1932 const CompilerType &function_clang_type, int storage, bool is_inline) {
1933 FunctionDecl *func_decl = nullptr;
1934 ASTContext *ast = getASTContext();
1935 if (decl_ctx == nullptr)
1936 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001937
Kate Stoneb9c1b512016-09-06 20:57:50 +00001938 const bool hasWrittenPrototype = true;
1939 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00001940
Kate Stoneb9c1b512016-09-06 20:57:50 +00001941 clang::DeclarationName declarationName =
1942 GetDeclarationName(name, function_clang_type);
1943 func_decl = FunctionDecl::Create(
1944 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
1945 ClangUtil::GetQualType(function_clang_type), nullptr,
1946 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
1947 isConstexprSpecified);
1948 if (func_decl)
1949 decl_ctx->addDecl(func_decl);
1950
Sean Callanan5e9e1992011-10-26 01:06:27 +00001951#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001952 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001953#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001954
1955 return func_decl;
1956}
1957
1958CompilerType ClangASTContext::CreateFunctionType(
1959 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
1960 unsigned num_args, bool is_variadic, unsigned type_quals) {
1961 if (ast == nullptr)
1962 return CompilerType(); // invalid AST
1963
1964 if (!result_type || !ClangUtil::IsClangType(result_type))
1965 return CompilerType(); // invalid return type
1966
1967 std::vector<QualType> qual_type_args;
1968 if (num_args > 0 && args == nullptr)
1969 return CompilerType(); // invalid argument array passed in
1970
1971 // Verify that all arguments are valid and the right type
1972 for (unsigned i = 0; i < num_args; ++i) {
1973 if (args[i]) {
1974 // Make sure we have a clang type in args[i] and not a type from another
1975 // language whose name might match
1976 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
1977 lldbassert(is_clang_type);
1978 if (is_clang_type)
1979 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
1980 else
1981 return CompilerType(); // invalid argument type (must be a clang type)
1982 } else
1983 return CompilerType(); // invalid argument type (empty)
1984 }
1985
1986 // TODO: Detect calling convention in DWARF?
1987 FunctionProtoType::ExtProtoInfo proto_info;
1988 proto_info.Variadic = is_variadic;
1989 proto_info.ExceptionSpec = EST_None;
1990 proto_info.TypeQuals = type_quals;
1991 proto_info.RefQualifier = RQ_None;
1992
1993 return CompilerType(ast,
1994 ast->getFunctionType(ClangUtil::GetQualType(result_type),
1995 qual_type_args, proto_info));
1996}
1997
1998ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
1999 const char *name, const CompilerType &param_type, int storage) {
2000 ASTContext *ast = getASTContext();
2001 assert(ast != nullptr);
2002 return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(),
2003 SourceLocation(), SourceLocation(),
2004 name && name[0] ? &ast->Idents.get(name) : nullptr,
2005 ClangUtil::GetQualType(param_type), nullptr,
2006 (clang::StorageClass)storage, nullptr);
2007}
2008
2009void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2010 ParmVarDecl **params,
2011 unsigned num_params) {
2012 if (function_decl)
2013 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002014}
2015
Greg Claytona1e5dc82015-08-11 22:53:00 +00002016CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002017ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
2018 QualType block_type = m_ast_ap->getBlockPointerType(
2019 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002020
Kate Stoneb9c1b512016-09-06 20:57:50 +00002021 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002022}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002023
2024#pragma mark Array Types
2025
Kate Stoneb9c1b512016-09-06 20:57:50 +00002026CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2027 size_t element_count,
2028 bool is_vector) {
2029 if (element_type.IsValid()) {
2030 ASTContext *ast = getASTContext();
2031 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002032
Kate Stoneb9c1b512016-09-06 20:57:50 +00002033 if (is_vector) {
2034 return CompilerType(
2035 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2036 element_count));
2037 } else {
2038
2039 llvm::APInt ap_element_count(64, element_count);
2040 if (element_count == 0) {
2041 return CompilerType(ast, ast->getIncompleteArrayType(
2042 ClangUtil::GetQualType(element_type),
2043 clang::ArrayType::Normal, 0));
2044 } else {
2045 return CompilerType(
2046 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2047 ap_element_count,
2048 clang::ArrayType::Normal, 0));
2049 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002050 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002051 }
2052 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002053}
2054
Kate Stoneb9c1b512016-09-06 20:57:50 +00002055CompilerType ClangASTContext::CreateStructForIdentifier(
2056 const ConstString &type_name,
2057 const std::initializer_list<std::pair<const char *, CompilerType>>
2058 &type_fields,
2059 bool packed) {
2060 CompilerType type;
2061 if (!type_name.IsEmpty() &&
2062 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2063 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002064 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002065 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002066 }
2067
2068 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2069 clang::TTK_Struct, lldb::eLanguageTypeC);
2070 StartTagDeclarationDefinition(type);
2071 for (const auto &field : type_fields)
2072 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2073 0);
2074 if (packed)
2075 SetIsPacked(type);
2076 CompleteTagDeclarationDefinition(type);
2077 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002078}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002079
Kate Stoneb9c1b512016-09-06 20:57:50 +00002080CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
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 = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2087 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002088
Kate Stoneb9c1b512016-09-06 20:57:50 +00002089 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002090}
2091
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002092#pragma mark Enumeration Types
2093
Greg Claytona1e5dc82015-08-11 22:53:00 +00002094CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002095ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2096 const Declaration &decl,
Tamas Berghammer59765832017-11-07 10:39:22 +00002097 const CompilerType &integer_clang_type,
2098 bool is_scoped) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002099 // TODO: Do something intelligent with the Declaration object passed in
2100 // like maybe filling in the SourceLocation with it...
2101 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002102
Kate Stoneb9c1b512016-09-06 20:57:50 +00002103 // TODO: ask about these...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002104 // const bool IsFixed = false;
2105
2106 EnumDecl *enum_decl = EnumDecl::Create(
2107 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2108 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
Tamas Berghammercf6bf4c2017-11-07 13:43:55 +00002109 is_scoped, // IsScoped
2110 is_scoped, // IsScopedUsingClassTag
2111 false); // IsFixed
Kate Stoneb9c1b512016-09-06 20:57:50 +00002112
2113 if (enum_decl) {
2114 // TODO: check if we should be setting the promotion type too?
2115 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2116
2117 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2118
2119 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2120 }
2121 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002122}
2123
Kate Stoneb9c1b512016-09-06 20:57:50 +00002124CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2125 size_t bit_size,
2126 bool is_signed) {
2127 if (ast) {
2128 if (is_signed) {
2129 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2130 return CompilerType(ast, ast->SignedCharTy);
2131
2132 if (bit_size == ast->getTypeSize(ast->ShortTy))
2133 return CompilerType(ast, ast->ShortTy);
2134
2135 if (bit_size == ast->getTypeSize(ast->IntTy))
2136 return CompilerType(ast, ast->IntTy);
2137
2138 if (bit_size == ast->getTypeSize(ast->LongTy))
2139 return CompilerType(ast, ast->LongTy);
2140
2141 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2142 return CompilerType(ast, ast->LongLongTy);
2143
2144 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2145 return CompilerType(ast, ast->Int128Ty);
2146 } else {
2147 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2148 return CompilerType(ast, ast->UnsignedCharTy);
2149
2150 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2151 return CompilerType(ast, ast->UnsignedShortTy);
2152
2153 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2154 return CompilerType(ast, ast->UnsignedIntTy);
2155
2156 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2157 return CompilerType(ast, ast->UnsignedLongTy);
2158
2159 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2160 return CompilerType(ast, ast->UnsignedLongLongTy);
2161
2162 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2163 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002164 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002165 }
2166 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002167}
2168
Kate Stoneb9c1b512016-09-06 20:57:50 +00002169CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2170 bool is_signed) {
2171 if (ast)
2172 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2173 is_signed);
2174 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002175}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002176
Kate Stoneb9c1b512016-09-06 20:57:50 +00002177void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2178 if (decl_ctx) {
2179 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002180
Kate Stoneb9c1b512016-09-06 20:57:50 +00002181 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2182 if (named_decl) {
2183 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2184 named_decl->getDeclName().getAsString().c_str());
2185 } else {
2186 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002187 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002188 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002189}
2190
Kate Stoneb9c1b512016-09-06 20:57:50 +00002191void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2192 if (decl == nullptr)
2193 return;
2194 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002195
Kate Stoneb9c1b512016-09-06 20:57:50 +00002196 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2197 if (record_decl) {
2198 printf("%20s: %s%s\n", decl->getDeclKindName(),
2199 record_decl->getDeclName().getAsString().c_str(),
2200 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002201
Kate Stoneb9c1b512016-09-06 20:57:50 +00002202 } else {
2203 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2204 if (named_decl) {
2205 printf("%20s: %s\n", decl->getDeclKindName(),
2206 named_decl->getDeclName().getAsString().c_str());
2207 } else {
2208 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002209 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002210 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002211}
2212
Kate Stoneb9c1b512016-09-06 20:57:50 +00002213bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2214 clang::Decl *rhs_decl) {
2215 if (lhs_decl && rhs_decl) {
2216 //----------------------------------------------------------------------
2217 // Make sure the decl kinds match first
2218 //----------------------------------------------------------------------
2219 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2220 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002221
Kate Stoneb9c1b512016-09-06 20:57:50 +00002222 if (lhs_decl_kind == rhs_decl_kind) {
2223 //------------------------------------------------------------------
2224 // Now check that the decl contexts kinds are all equivalent
2225 // before we have to check any names of the decl contexts...
2226 //------------------------------------------------------------------
2227 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2228 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2229 if (lhs_decl_ctx && rhs_decl_ctx) {
2230 while (1) {
2231 if (lhs_decl_ctx && rhs_decl_ctx) {
2232 const clang::Decl::Kind lhs_decl_ctx_kind =
2233 lhs_decl_ctx->getDeclKind();
2234 const clang::Decl::Kind rhs_decl_ctx_kind =
2235 rhs_decl_ctx->getDeclKind();
2236 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2237 lhs_decl_ctx = lhs_decl_ctx->getParent();
2238 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002239
Kate Stoneb9c1b512016-09-06 20:57:50 +00002240 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2241 break;
2242 } else
2243 return false;
2244 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002245 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002246 }
2247
2248 //--------------------------------------------------------------
2249 // Now make sure the name of the decls match
2250 //--------------------------------------------------------------
2251 clang::NamedDecl *lhs_named_decl =
2252 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2253 clang::NamedDecl *rhs_named_decl =
2254 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2255 if (lhs_named_decl && rhs_named_decl) {
2256 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2257 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2258 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2259 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2260 return false;
2261 } else
Greg Claytona2721472011-06-25 00:44:06 +00002262 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002263 } else
2264 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002265
Kate Stoneb9c1b512016-09-06 20:57:50 +00002266 //--------------------------------------------------------------
2267 // We know that the decl context kinds all match, so now we need
2268 // to make sure the names match as well
2269 //--------------------------------------------------------------
2270 lhs_decl_ctx = lhs_decl->getDeclContext();
2271 rhs_decl_ctx = rhs_decl->getDeclContext();
2272 while (1) {
2273 switch (lhs_decl_ctx->getDeclKind()) {
2274 case clang::Decl::TranslationUnit:
2275 // We don't care about the translation unit names
2276 return true;
2277 default: {
2278 clang::NamedDecl *lhs_named_decl =
2279 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2280 clang::NamedDecl *rhs_named_decl =
2281 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2282 if (lhs_named_decl && rhs_named_decl) {
2283 clang::DeclarationName lhs_decl_name =
2284 lhs_named_decl->getDeclName();
2285 clang::DeclarationName rhs_decl_name =
2286 rhs_named_decl->getDeclName();
2287 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2288 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2289 return false;
2290 } else
2291 return false;
2292 } else
2293 return false;
2294 } break;
2295 }
2296 lhs_decl_ctx = lhs_decl_ctx->getParent();
2297 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002298 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002299 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002300 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002301 }
2302 return false;
2303}
2304bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2305 clang::Decl *decl) {
2306 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002307 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002308
Kate Stoneb9c1b512016-09-06 20:57:50 +00002309 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002310
Kate Stoneb9c1b512016-09-06 20:57:50 +00002311 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002312 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002313
2314 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2315 if (tag_decl->isCompleteDefinition())
2316 return true;
2317
2318 if (!tag_decl->hasExternalLexicalStorage())
2319 return false;
2320
2321 ast_source->CompleteType(tag_decl);
2322
2323 return !tag_decl->getTypeForDecl()->isIncompleteType();
2324 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2325 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2326 if (objc_interface_decl->getDefinition())
2327 return true;
2328
2329 if (!objc_interface_decl->hasExternalLexicalStorage())
2330 return false;
2331
2332 ast_source->CompleteType(objc_interface_decl);
2333
2334 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2335 } else {
2336 return false;
2337 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002338}
2339
Kate Stoneb9c1b512016-09-06 20:57:50 +00002340void ClangASTContext::SetMetadataAsUserID(const void *object,
2341 user_id_t user_id) {
2342 ClangASTMetadata meta_data;
2343 meta_data.SetUserID(user_id);
2344 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002345}
2346
Kate Stoneb9c1b512016-09-06 20:57:50 +00002347void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2348 ClangASTMetadata &metadata) {
2349 ClangExternalASTSourceCommon *external_source =
2350 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2351
2352 if (external_source)
2353 external_source->SetMetadata(object, metadata);
2354}
2355
2356ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2357 const void *object) {
2358 ClangExternalASTSourceCommon *external_source =
2359 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2360
2361 if (external_source && external_source->HasMetadata(object))
2362 return external_source->GetMetadata(object);
2363 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002364 return nullptr;
2365}
2366
Kate Stoneb9c1b512016-09-06 20:57:50 +00002367clang::DeclContext *
2368ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2369 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2370}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002371
Kate Stoneb9c1b512016-09-06 20:57:50 +00002372clang::DeclContext *
2373ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2374 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2375}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002376
Kate Stoneb9c1b512016-09-06 20:57:50 +00002377bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2378 int kind) const {
2379 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2380 if (clang_type) {
2381 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2382 if (tag_type) {
2383 clang::TagDecl *tag_decl =
2384 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2385 if (tag_decl) {
2386 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2387 return true;
2388 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002389 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002390 }
2391 return false;
2392}
2393
2394bool ClangASTContext::SetDefaultAccessForRecordFields(
2395 clang::RecordDecl *record_decl, int default_accessibility,
2396 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2397 if (record_decl) {
2398 uint32_t field_idx;
2399 clang::RecordDecl::field_iterator field, field_end;
2400 for (field = record_decl->field_begin(),
2401 field_end = record_decl->field_end(), field_idx = 0;
2402 field != field_end; ++field, ++field_idx) {
2403 // If no accessibility was assigned, assign the correct one
2404 if (field_idx < num_assigned_accessibilities &&
2405 assigned_accessibilities[field_idx] == clang::AS_none)
2406 field->setAccess((clang::AccessSpecifier)default_accessibility);
2407 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002408 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002409 }
2410 return false;
2411}
2412
2413clang::DeclContext *
2414ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2415 return GetDeclContextForType(ClangUtil::GetQualType(type));
2416}
2417
2418clang::DeclContext *
2419ClangASTContext::GetDeclContextForType(clang::QualType type) {
2420 if (type.isNull())
2421 return nullptr;
2422
2423 clang::QualType qual_type = type.getCanonicalType();
2424 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2425 switch (type_class) {
2426 case clang::Type::ObjCInterface:
2427 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2428 ->getInterface();
2429 case clang::Type::ObjCObjectPointer:
2430 return GetDeclContextForType(
2431 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2432 ->getPointeeType());
2433 case clang::Type::Record:
2434 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2435 case clang::Type::Enum:
2436 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2437 case clang::Type::Typedef:
2438 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2439 ->getDecl()
2440 ->getUnderlyingType());
2441 case clang::Type::Auto:
2442 return GetDeclContextForType(
2443 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2444 case clang::Type::Elaborated:
2445 return GetDeclContextForType(
2446 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2447 case clang::Type::Paren:
2448 return GetDeclContextForType(
2449 llvm::cast<clang::ParenType>(qual_type)->desugar());
2450 default:
2451 break;
2452 }
2453 // No DeclContext in this type...
2454 return nullptr;
2455}
2456
2457static bool GetCompleteQualType(clang::ASTContext *ast,
2458 clang::QualType qual_type,
2459 bool allow_completion = true) {
2460 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2461 switch (type_class) {
2462 case clang::Type::ConstantArray:
2463 case clang::Type::IncompleteArray:
2464 case clang::Type::VariableArray: {
2465 const clang::ArrayType *array_type =
2466 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2467
2468 if (array_type)
2469 return GetCompleteQualType(ast, array_type->getElementType(),
2470 allow_completion);
2471 } break;
2472 case clang::Type::Record: {
2473 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2474 if (cxx_record_decl) {
2475 if (cxx_record_decl->hasExternalLexicalStorage()) {
2476 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2477 const bool fields_loaded =
2478 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2479 if (is_complete && fields_loaded)
2480 return true;
2481
2482 if (!allow_completion)
2483 return false;
2484
2485 // Call the field_begin() accessor to for it to use the external source
2486 // to load the fields...
2487 clang::ExternalASTSource *external_ast_source =
2488 ast->getExternalSource();
2489 if (external_ast_source) {
2490 external_ast_source->CompleteType(cxx_record_decl);
2491 if (cxx_record_decl->isCompleteDefinition()) {
2492 cxx_record_decl->field_begin();
2493 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2494 }
2495 }
2496 }
2497 }
2498 const clang::TagType *tag_type =
2499 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2500 return !tag_type->isIncompleteType();
2501 } break;
2502
2503 case clang::Type::Enum: {
2504 const clang::TagType *tag_type =
2505 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2506 if (tag_type) {
2507 clang::TagDecl *tag_decl = tag_type->getDecl();
2508 if (tag_decl) {
2509 if (tag_decl->getDefinition())
2510 return true;
2511
2512 if (!allow_completion)
2513 return false;
2514
2515 if (tag_decl->hasExternalLexicalStorage()) {
2516 if (ast) {
2517 clang::ExternalASTSource *external_ast_source =
2518 ast->getExternalSource();
2519 if (external_ast_source) {
2520 external_ast_source->CompleteType(tag_decl);
2521 return !tag_type->isIncompleteType();
2522 }
2523 }
2524 }
2525 return false;
2526 }
2527 }
2528
2529 } break;
2530 case clang::Type::ObjCObject:
2531 case clang::Type::ObjCInterface: {
2532 const clang::ObjCObjectType *objc_class_type =
2533 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2534 if (objc_class_type) {
2535 clang::ObjCInterfaceDecl *class_interface_decl =
2536 objc_class_type->getInterface();
2537 // We currently can't complete objective C types through the newly added
2538 // ASTContext
2539 // because it only supports TagDecl objects right now...
2540 if (class_interface_decl) {
2541 if (class_interface_decl->getDefinition())
2542 return true;
2543
2544 if (!allow_completion)
2545 return false;
2546
2547 if (class_interface_decl->hasExternalLexicalStorage()) {
2548 if (ast) {
2549 clang::ExternalASTSource *external_ast_source =
2550 ast->getExternalSource();
2551 if (external_ast_source) {
2552 external_ast_source->CompleteType(class_interface_decl);
2553 return !objc_class_type->isIncompleteType();
2554 }
2555 }
2556 }
2557 return false;
2558 }
2559 }
2560 } break;
2561
2562 case clang::Type::Typedef:
2563 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2564 ->getDecl()
2565 ->getUnderlyingType(),
2566 allow_completion);
2567
2568 case clang::Type::Auto:
2569 return GetCompleteQualType(
2570 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2571 allow_completion);
2572
2573 case clang::Type::Elaborated:
2574 return GetCompleteQualType(
2575 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2576 allow_completion);
2577
2578 case clang::Type::Paren:
2579 return GetCompleteQualType(
2580 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2581 allow_completion);
2582
2583 case clang::Type::Attributed:
2584 return GetCompleteQualType(
2585 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2586 allow_completion);
2587
2588 default:
2589 break;
2590 }
2591
2592 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002593}
2594
2595static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002596ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2597 switch (access) {
2598 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002599 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002600 case eAccessPublic:
2601 return clang::ObjCIvarDecl::Public;
2602 case eAccessPrivate:
2603 return clang::ObjCIvarDecl::Private;
2604 case eAccessProtected:
2605 return clang::ObjCIvarDecl::Protected;
2606 case eAccessPackage:
2607 return clang::ObjCIvarDecl::Package;
2608 }
2609 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002610}
2611
Greg Claytond8d4a572015-08-11 21:38:15 +00002612//----------------------------------------------------------------------
2613// Tests
2614//----------------------------------------------------------------------
2615
Kate Stoneb9c1b512016-09-06 20:57:50 +00002616bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2617 clang::QualType qual_type(GetCanonicalQualType(type));
2618
2619 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2620 switch (type_class) {
2621 case clang::Type::IncompleteArray:
2622 case clang::Type::VariableArray:
2623 case clang::Type::ConstantArray:
2624 case clang::Type::ExtVector:
2625 case clang::Type::Vector:
2626 case clang::Type::Record:
2627 case clang::Type::ObjCObject:
2628 case clang::Type::ObjCInterface:
2629 return true;
2630 case clang::Type::Auto:
2631 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2632 ->getDeducedType()
2633 .getAsOpaquePtr());
2634 case clang::Type::Elaborated:
2635 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2636 ->getNamedType()
2637 .getAsOpaquePtr());
2638 case clang::Type::Typedef:
2639 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2640 ->getDecl()
2641 ->getUnderlyingType()
2642 .getAsOpaquePtr());
2643 case clang::Type::Paren:
2644 return IsAggregateType(
2645 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2646 default:
2647 break;
2648 }
2649 // The clang type does have a value
2650 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002651}
2652
Kate Stoneb9c1b512016-09-06 20:57:50 +00002653bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2654 clang::QualType qual_type(GetCanonicalQualType(type));
2655
2656 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2657 switch (type_class) {
2658 case clang::Type::Record: {
2659 if (const clang::RecordType *record_type =
2660 llvm::dyn_cast_or_null<clang::RecordType>(
2661 qual_type.getTypePtrOrNull())) {
2662 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2663 return record_decl->isAnonymousStructOrUnion();
2664 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002665 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002666 break;
2667 }
2668 case clang::Type::Auto:
2669 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2670 ->getDeducedType()
2671 .getAsOpaquePtr());
2672 case clang::Type::Elaborated:
2673 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2674 ->getNamedType()
2675 .getAsOpaquePtr());
2676 case clang::Type::Typedef:
2677 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2678 ->getDecl()
2679 ->getUnderlyingType()
2680 .getAsOpaquePtr());
2681 case clang::Type::Paren:
2682 return IsAnonymousType(
2683 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2684 default:
2685 break;
2686 }
2687 // The clang type does have a value
2688 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002689}
2690
Kate Stoneb9c1b512016-09-06 20:57:50 +00002691bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2692 CompilerType *element_type_ptr,
2693 uint64_t *size, bool *is_incomplete) {
2694 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002695
Kate Stoneb9c1b512016-09-06 20:57:50 +00002696 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2697 switch (type_class) {
2698 default:
2699 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002700
Kate Stoneb9c1b512016-09-06 20:57:50 +00002701 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002702 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002703 element_type_ptr->SetCompilerType(
2704 getASTContext(),
2705 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002706 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002707 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2708 ->getSize()
2709 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002710 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002711 *is_incomplete = false;
2712 return true;
2713
2714 case clang::Type::IncompleteArray:
2715 if (element_type_ptr)
2716 element_type_ptr->SetCompilerType(
2717 getASTContext(),
2718 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2719 if (size)
2720 *size = 0;
2721 if (is_incomplete)
2722 *is_incomplete = true;
2723 return true;
2724
2725 case clang::Type::VariableArray:
2726 if (element_type_ptr)
2727 element_type_ptr->SetCompilerType(
2728 getASTContext(),
2729 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2730 if (size)
2731 *size = 0;
2732 if (is_incomplete)
2733 *is_incomplete = false;
2734 return true;
2735
2736 case clang::Type::DependentSizedArray:
2737 if (element_type_ptr)
2738 element_type_ptr->SetCompilerType(
2739 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2740 ->getElementType());
2741 if (size)
2742 *size = 0;
2743 if (is_incomplete)
2744 *is_incomplete = false;
2745 return true;
2746
2747 case clang::Type::Typedef:
2748 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2749 ->getDecl()
2750 ->getUnderlyingType()
2751 .getAsOpaquePtr(),
2752 element_type_ptr, size, is_incomplete);
2753 case clang::Type::Auto:
2754 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2755 ->getDeducedType()
2756 .getAsOpaquePtr(),
2757 element_type_ptr, size, is_incomplete);
2758 case clang::Type::Elaborated:
2759 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2760 ->getNamedType()
2761 .getAsOpaquePtr(),
2762 element_type_ptr, size, is_incomplete);
2763 case clang::Type::Paren:
2764 return IsArrayType(
2765 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2766 element_type_ptr, size, is_incomplete);
2767 }
2768 if (element_type_ptr)
2769 element_type_ptr->Clear();
2770 if (size)
2771 *size = 0;
2772 if (is_incomplete)
2773 *is_incomplete = false;
2774 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002775}
2776
Kate Stoneb9c1b512016-09-06 20:57:50 +00002777bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2778 CompilerType *element_type, uint64_t *size) {
2779 clang::QualType qual_type(GetCanonicalQualType(type));
2780
2781 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2782 switch (type_class) {
2783 case clang::Type::Vector: {
2784 const clang::VectorType *vector_type =
2785 qual_type->getAs<clang::VectorType>();
2786 if (vector_type) {
2787 if (size)
2788 *size = vector_type->getNumElements();
2789 if (element_type)
2790 *element_type =
2791 CompilerType(getASTContext(), vector_type->getElementType());
2792 }
2793 return true;
2794 } break;
2795 case clang::Type::ExtVector: {
2796 const clang::ExtVectorType *ext_vector_type =
2797 qual_type->getAs<clang::ExtVectorType>();
2798 if (ext_vector_type) {
2799 if (size)
2800 *size = ext_vector_type->getNumElements();
2801 if (element_type)
2802 *element_type =
2803 CompilerType(getASTContext(), ext_vector_type->getElementType());
2804 }
2805 return true;
2806 }
2807 default:
2808 break;
2809 }
2810 return false;
2811}
2812
2813bool ClangASTContext::IsRuntimeGeneratedType(
2814 lldb::opaque_compiler_type_t type) {
2815 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
2816 ->GetDeclContextForType(GetQualType(type));
2817 if (!decl_ctx)
2818 return false;
2819
2820 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2821 return false;
2822
2823 clang::ObjCInterfaceDecl *result_iface_decl =
2824 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2825
2826 ClangASTMetadata *ast_metadata =
2827 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2828 if (!ast_metadata)
2829 return false;
2830 return (ast_metadata->GetISAPtr() != 0);
2831}
2832
2833bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
2834 return GetQualType(type).getUnqualifiedType()->isCharType();
2835}
2836
2837bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
2838 const bool allow_completion = false;
2839 return GetCompleteQualType(getASTContext(), GetQualType(type),
2840 allow_completion);
2841}
2842
2843bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
2844 return GetQualType(type).isConstQualified();
2845}
2846
2847bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
2848 uint32_t &length) {
2849 CompilerType pointee_or_element_clang_type;
2850 length = 0;
2851 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2852
2853 if (!pointee_or_element_clang_type.IsValid())
2854 return false;
2855
2856 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2857 if (pointee_or_element_clang_type.IsCharType()) {
2858 if (type_flags.Test(eTypeIsArray)) {
2859 // We know the size of the array and it could be a C string
2860 // since it is an array of characters
2861 length = llvm::cast<clang::ConstantArrayType>(
2862 GetCanonicalQualType(type).getTypePtr())
2863 ->getSize()
2864 .getLimitedValue();
2865 }
2866 return true;
2867 }
2868 }
2869 return false;
2870}
2871
2872bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
2873 bool *is_variadic_ptr) {
2874 if (type) {
2875 clang::QualType qual_type(GetCanonicalQualType(type));
2876
2877 if (qual_type->isFunctionType()) {
2878 if (is_variadic_ptr) {
2879 const clang::FunctionProtoType *function_proto_type =
2880 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2881 if (function_proto_type)
2882 *is_variadic_ptr = function_proto_type->isVariadic();
2883 else
2884 *is_variadic_ptr = false;
2885 }
2886 return true;
2887 }
2888
Greg Claytond8d4a572015-08-11 21:38:15 +00002889 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002890 switch (type_class) {
2891 default:
2892 break;
2893 case clang::Type::Typedef:
2894 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
2895 ->getDecl()
2896 ->getUnderlyingType()
2897 .getAsOpaquePtr(),
2898 nullptr);
2899 case clang::Type::Auto:
2900 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
2901 ->getDeducedType()
2902 .getAsOpaquePtr(),
2903 nullptr);
2904 case clang::Type::Elaborated:
2905 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
2906 ->getNamedType()
2907 .getAsOpaquePtr(),
2908 nullptr);
2909 case clang::Type::Paren:
2910 return IsFunctionType(
2911 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2912 nullptr);
2913 case clang::Type::LValueReference:
2914 case clang::Type::RValueReference: {
2915 const clang::ReferenceType *reference_type =
2916 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2917 if (reference_type)
2918 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
2919 nullptr);
2920 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00002921 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002922 }
2923 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002924}
2925
2926// Used to detect "Homogeneous Floating-point Aggregates"
2927uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00002928ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
2929 CompilerType *base_type_ptr) {
2930 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002931 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002932
2933 clang::QualType qual_type(GetCanonicalQualType(type));
2934 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2935 switch (type_class) {
2936 case clang::Type::Record:
2937 if (GetCompleteType(type)) {
2938 const clang::CXXRecordDecl *cxx_record_decl =
2939 qual_type->getAsCXXRecordDecl();
2940 if (cxx_record_decl) {
2941 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
2942 return 0;
2943 }
2944 const clang::RecordType *record_type =
2945 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
2946 if (record_type) {
2947 const clang::RecordDecl *record_decl = record_type->getDecl();
2948 if (record_decl) {
2949 // We are looking for a structure that contains only floating point
2950 // types
2951 clang::RecordDecl::field_iterator field_pos,
2952 field_end = record_decl->field_end();
2953 uint32_t num_fields = 0;
2954 bool is_hva = false;
2955 bool is_hfa = false;
2956 clang::QualType base_qual_type;
2957 uint64_t base_bitwidth = 0;
2958 for (field_pos = record_decl->field_begin(); field_pos != field_end;
2959 ++field_pos) {
2960 clang::QualType field_qual_type = field_pos->getType();
2961 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
2962 if (field_qual_type->isFloatingType()) {
2963 if (field_qual_type->isComplexType())
2964 return 0;
2965 else {
2966 if (num_fields == 0)
2967 base_qual_type = field_qual_type;
2968 else {
2969 if (is_hva)
2970 return 0;
2971 is_hfa = true;
2972 if (field_qual_type.getTypePtr() !=
2973 base_qual_type.getTypePtr())
2974 return 0;
2975 }
2976 }
2977 } else if (field_qual_type->isVectorType() ||
2978 field_qual_type->isExtVectorType()) {
2979 if (num_fields == 0) {
2980 base_qual_type = field_qual_type;
2981 base_bitwidth = field_bitwidth;
2982 } else {
2983 if (is_hfa)
2984 return 0;
2985 is_hva = true;
2986 if (base_bitwidth != field_bitwidth)
2987 return 0;
2988 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
2989 return 0;
2990 }
2991 } else
2992 return 0;
2993 ++num_fields;
2994 }
2995 if (base_type_ptr)
2996 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
2997 return num_fields;
2998 }
2999 }
3000 }
3001 break;
3002
3003 case clang::Type::Typedef:
3004 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3005 ->getDecl()
3006 ->getUnderlyingType()
3007 .getAsOpaquePtr(),
3008 base_type_ptr);
3009
3010 case clang::Type::Auto:
3011 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3012 ->getDeducedType()
3013 .getAsOpaquePtr(),
3014 base_type_ptr);
3015
3016 case clang::Type::Elaborated:
3017 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3018 ->getNamedType()
3019 .getAsOpaquePtr(),
3020 base_type_ptr);
3021 default:
3022 break;
3023 }
3024 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003025}
3026
Kate Stoneb9c1b512016-09-06 20:57:50 +00003027size_t ClangASTContext::GetNumberOfFunctionArguments(
3028 lldb::opaque_compiler_type_t type) {
3029 if (type) {
3030 clang::QualType qual_type(GetCanonicalQualType(type));
3031 const clang::FunctionProtoType *func =
3032 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3033 if (func)
3034 return func->getNumParams();
3035 }
3036 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003037}
3038
Greg Claytona1e5dc82015-08-11 22:53:00 +00003039CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003040ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3041 const size_t index) {
3042 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003043 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003044 const clang::FunctionProtoType *func =
3045 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3046 if (func) {
3047 if (index < func->getNumParams())
3048 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003049 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003050 }
3051 return CompilerType();
3052}
3053
3054bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3055 if (type) {
3056 clang::QualType qual_type(GetCanonicalQualType(type));
3057
3058 if (qual_type->isFunctionPointerType())
3059 return true;
3060
3061 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3062 switch (type_class) {
3063 default:
3064 break;
3065 case clang::Type::Typedef:
3066 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3067 ->getDecl()
3068 ->getUnderlyingType()
3069 .getAsOpaquePtr());
3070 case clang::Type::Auto:
3071 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3072 ->getDeducedType()
3073 .getAsOpaquePtr());
3074 case clang::Type::Elaborated:
3075 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3076 ->getNamedType()
3077 .getAsOpaquePtr());
3078 case clang::Type::Paren:
3079 return IsFunctionPointerType(
3080 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3081
3082 case clang::Type::LValueReference:
3083 case clang::Type::RValueReference: {
3084 const clang::ReferenceType *reference_type =
3085 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3086 if (reference_type)
3087 return IsFunctionPointerType(
3088 reference_type->getPointeeType().getAsOpaquePtr());
3089 } break;
3090 }
3091 }
3092 return false;
3093}
3094
3095bool ClangASTContext::IsBlockPointerType(
3096 lldb::opaque_compiler_type_t type,
3097 CompilerType *function_pointer_type_ptr) {
3098 if (type) {
3099 clang::QualType qual_type(GetCanonicalQualType(type));
3100
3101 if (qual_type->isBlockPointerType()) {
3102 if (function_pointer_type_ptr) {
3103 const clang::BlockPointerType *block_pointer_type =
3104 qual_type->getAs<clang::BlockPointerType>();
3105 QualType pointee_type = block_pointer_type->getPointeeType();
3106 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3107 *function_pointer_type_ptr =
3108 CompilerType(getASTContext(), function_pointer_type);
3109 }
3110 return true;
3111 }
3112
3113 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3114 switch (type_class) {
3115 default:
3116 break;
3117 case clang::Type::Typedef:
3118 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3119 ->getDecl()
3120 ->getUnderlyingType()
3121 .getAsOpaquePtr(),
3122 function_pointer_type_ptr);
3123 case clang::Type::Auto:
3124 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3125 ->getDeducedType()
3126 .getAsOpaquePtr(),
3127 function_pointer_type_ptr);
3128 case clang::Type::Elaborated:
3129 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3130 ->getNamedType()
3131 .getAsOpaquePtr(),
3132 function_pointer_type_ptr);
3133 case clang::Type::Paren:
3134 return IsBlockPointerType(
3135 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3136 function_pointer_type_ptr);
3137
3138 case clang::Type::LValueReference:
3139 case clang::Type::RValueReference: {
3140 const clang::ReferenceType *reference_type =
3141 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3142 if (reference_type)
3143 return IsBlockPointerType(
3144 reference_type->getPointeeType().getAsOpaquePtr(),
3145 function_pointer_type_ptr);
3146 } break;
3147 }
3148 }
3149 return false;
3150}
3151
3152bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3153 bool &is_signed) {
3154 if (!type)
3155 return false;
3156
3157 clang::QualType qual_type(GetCanonicalQualType(type));
3158 const clang::BuiltinType *builtin_type =
3159 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3160
3161 if (builtin_type) {
3162 if (builtin_type->isInteger()) {
3163 is_signed = builtin_type->isSignedInteger();
3164 return true;
3165 }
3166 }
3167
3168 return false;
3169}
3170
3171bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3172 bool &is_signed) {
3173 if (type) {
3174 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3175 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3176
3177 if (enum_type) {
3178 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3179 is_signed);
3180 return true;
3181 }
3182 }
3183
3184 return false;
3185}
3186
3187bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3188 CompilerType *pointee_type) {
3189 if (type) {
3190 clang::QualType qual_type(GetCanonicalQualType(type));
3191 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3192 switch (type_class) {
3193 case clang::Type::Builtin:
3194 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3195 default:
3196 break;
3197 case clang::BuiltinType::ObjCId:
3198 case clang::BuiltinType::ObjCClass:
3199 return true;
3200 }
3201 return false;
3202 case clang::Type::ObjCObjectPointer:
3203 if (pointee_type)
3204 pointee_type->SetCompilerType(
3205 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3206 ->getPointeeType());
3207 return true;
3208 case clang::Type::BlockPointer:
3209 if (pointee_type)
3210 pointee_type->SetCompilerType(
3211 getASTContext(),
3212 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3213 return true;
3214 case clang::Type::Pointer:
3215 if (pointee_type)
3216 pointee_type->SetCompilerType(
3217 getASTContext(),
3218 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3219 return true;
3220 case clang::Type::MemberPointer:
3221 if (pointee_type)
3222 pointee_type->SetCompilerType(
3223 getASTContext(),
3224 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3225 return true;
3226 case clang::Type::Typedef:
3227 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3228 ->getDecl()
3229 ->getUnderlyingType()
3230 .getAsOpaquePtr(),
3231 pointee_type);
3232 case clang::Type::Auto:
3233 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3234 ->getDeducedType()
3235 .getAsOpaquePtr(),
3236 pointee_type);
3237 case clang::Type::Elaborated:
3238 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3239 ->getNamedType()
3240 .getAsOpaquePtr(),
3241 pointee_type);
3242 case clang::Type::Paren:
3243 return IsPointerType(
3244 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3245 pointee_type);
3246 default:
3247 break;
3248 }
3249 }
3250 if (pointee_type)
3251 pointee_type->Clear();
3252 return false;
3253}
3254
3255bool ClangASTContext::IsPointerOrReferenceType(
3256 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3257 if (type) {
3258 clang::QualType qual_type(GetCanonicalQualType(type));
3259 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3260 switch (type_class) {
3261 case clang::Type::Builtin:
3262 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3263 default:
3264 break;
3265 case clang::BuiltinType::ObjCId:
3266 case clang::BuiltinType::ObjCClass:
3267 return true;
3268 }
3269 return false;
3270 case clang::Type::ObjCObjectPointer:
3271 if (pointee_type)
3272 pointee_type->SetCompilerType(
3273 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3274 ->getPointeeType());
3275 return true;
3276 case clang::Type::BlockPointer:
3277 if (pointee_type)
3278 pointee_type->SetCompilerType(
3279 getASTContext(),
3280 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3281 return true;
3282 case clang::Type::Pointer:
3283 if (pointee_type)
3284 pointee_type->SetCompilerType(
3285 getASTContext(),
3286 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3287 return true;
3288 case clang::Type::MemberPointer:
3289 if (pointee_type)
3290 pointee_type->SetCompilerType(
3291 getASTContext(),
3292 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3293 return true;
3294 case clang::Type::LValueReference:
3295 if (pointee_type)
3296 pointee_type->SetCompilerType(
3297 getASTContext(),
3298 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3299 return true;
3300 case clang::Type::RValueReference:
3301 if (pointee_type)
3302 pointee_type->SetCompilerType(
3303 getASTContext(),
3304 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3305 return true;
3306 case clang::Type::Typedef:
3307 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3308 ->getDecl()
3309 ->getUnderlyingType()
3310 .getAsOpaquePtr(),
3311 pointee_type);
3312 case clang::Type::Auto:
3313 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3314 ->getDeducedType()
3315 .getAsOpaquePtr(),
3316 pointee_type);
3317 case clang::Type::Elaborated:
3318 return IsPointerOrReferenceType(
3319 llvm::cast<clang::ElaboratedType>(qual_type)
3320 ->getNamedType()
3321 .getAsOpaquePtr(),
3322 pointee_type);
3323 case clang::Type::Paren:
3324 return IsPointerOrReferenceType(
3325 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3326 pointee_type);
3327 default:
3328 break;
3329 }
3330 }
3331 if (pointee_type)
3332 pointee_type->Clear();
3333 return false;
3334}
3335
3336bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3337 CompilerType *pointee_type,
3338 bool *is_rvalue) {
3339 if (type) {
3340 clang::QualType qual_type(GetCanonicalQualType(type));
3341 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3342
3343 switch (type_class) {
3344 case clang::Type::LValueReference:
3345 if (pointee_type)
3346 pointee_type->SetCompilerType(
3347 getASTContext(),
3348 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3349 if (is_rvalue)
3350 *is_rvalue = false;
3351 return true;
3352 case clang::Type::RValueReference:
3353 if (pointee_type)
3354 pointee_type->SetCompilerType(
3355 getASTContext(),
3356 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3357 if (is_rvalue)
3358 *is_rvalue = true;
3359 return true;
3360 case clang::Type::Typedef:
3361 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3362 ->getDecl()
3363 ->getUnderlyingType()
3364 .getAsOpaquePtr(),
3365 pointee_type, is_rvalue);
3366 case clang::Type::Auto:
3367 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3368 ->getDeducedType()
3369 .getAsOpaquePtr(),
3370 pointee_type, is_rvalue);
3371 case clang::Type::Elaborated:
3372 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3373 ->getNamedType()
3374 .getAsOpaquePtr(),
3375 pointee_type, is_rvalue);
3376 case clang::Type::Paren:
3377 return IsReferenceType(
3378 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3379 pointee_type, is_rvalue);
3380
3381 default:
3382 break;
3383 }
3384 }
3385 if (pointee_type)
3386 pointee_type->Clear();
3387 return false;
3388}
3389
3390bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3391 uint32_t &count, bool &is_complex) {
3392 if (type) {
3393 clang::QualType qual_type(GetCanonicalQualType(type));
3394
3395 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3396 qual_type->getCanonicalTypeInternal())) {
3397 clang::BuiltinType::Kind kind = BT->getKind();
3398 if (kind >= clang::BuiltinType::Float &&
3399 kind <= clang::BuiltinType::LongDouble) {
3400 count = 1;
3401 is_complex = false;
3402 return true;
3403 }
3404 } else if (const clang::ComplexType *CT =
3405 llvm::dyn_cast<clang::ComplexType>(
3406 qual_type->getCanonicalTypeInternal())) {
3407 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3408 is_complex)) {
3409 count = 2;
3410 is_complex = true;
3411 return true;
3412 }
3413 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3414 qual_type->getCanonicalTypeInternal())) {
3415 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3416 is_complex)) {
3417 count = VT->getNumElements();
3418 is_complex = false;
3419 return true;
3420 }
3421 }
3422 }
3423 count = 0;
3424 is_complex = false;
3425 return false;
3426}
3427
3428bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3429 if (!type)
3430 return false;
3431
3432 clang::QualType qual_type(GetQualType(type));
3433 const clang::TagType *tag_type =
3434 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3435 if (tag_type) {
3436 clang::TagDecl *tag_decl = tag_type->getDecl();
3437 if (tag_decl)
3438 return tag_decl->isCompleteDefinition();
3439 return false;
3440 } else {
3441 const clang::ObjCObjectType *objc_class_type =
3442 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3443 if (objc_class_type) {
3444 clang::ObjCInterfaceDecl *class_interface_decl =
3445 objc_class_type->getInterface();
3446 if (class_interface_decl)
3447 return class_interface_decl->getDefinition() != nullptr;
3448 return false;
3449 }
3450 }
3451 return true;
3452}
3453
3454bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3455 if (type) {
3456 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3457
3458 const clang::ObjCObjectPointerType *obj_pointer_type =
3459 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3460
3461 if (obj_pointer_type)
3462 return obj_pointer_type->isObjCClassType();
3463 }
3464 return false;
3465}
3466
3467bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3468 if (ClangUtil::IsClangType(type))
3469 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3470 return false;
3471}
3472
3473bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3474 if (!type)
3475 return false;
3476 clang::QualType qual_type(GetCanonicalQualType(type));
3477 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3478 return (type_class == clang::Type::Record);
3479}
3480
3481bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3482 if (!type)
3483 return false;
3484 clang::QualType qual_type(GetCanonicalQualType(type));
3485 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3486 return (type_class == clang::Type::Enum);
3487}
3488
3489bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3490 if (type) {
3491 clang::QualType qual_type(GetCanonicalQualType(type));
3492 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3493 switch (type_class) {
3494 case clang::Type::Record:
3495 if (GetCompleteType(type)) {
3496 const clang::RecordType *record_type =
3497 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3498 const clang::RecordDecl *record_decl = record_type->getDecl();
3499 if (record_decl) {
3500 const clang::CXXRecordDecl *cxx_record_decl =
3501 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3502 if (cxx_record_decl)
3503 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003504 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003505 }
3506 break;
3507
3508 default:
3509 break;
3510 }
3511 }
3512 return false;
3513}
3514
3515bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3516 CompilerType *dynamic_pointee_type,
3517 bool check_cplusplus,
3518 bool check_objc) {
3519 clang::QualType pointee_qual_type;
3520 if (type) {
3521 clang::QualType qual_type(GetCanonicalQualType(type));
3522 bool success = false;
3523 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3524 switch (type_class) {
3525 case clang::Type::Builtin:
3526 if (check_objc &&
3527 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3528 clang::BuiltinType::ObjCId) {
3529 if (dynamic_pointee_type)
3530 dynamic_pointee_type->SetCompilerType(this, type);
3531 return true;
3532 }
3533 break;
3534
3535 case clang::Type::ObjCObjectPointer:
3536 if (check_objc) {
3537 if (auto objc_pointee_type =
3538 qual_type->getPointeeType().getTypePtrOrNull()) {
3539 if (auto objc_object_type =
3540 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3541 objc_pointee_type)) {
3542 if (objc_object_type->isObjCClass())
3543 return false;
3544 }
3545 }
3546 if (dynamic_pointee_type)
3547 dynamic_pointee_type->SetCompilerType(
3548 getASTContext(),
3549 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3550 ->getPointeeType());
3551 return true;
3552 }
3553 break;
3554
3555 case clang::Type::Pointer:
3556 pointee_qual_type =
3557 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3558 success = true;
3559 break;
3560
3561 case clang::Type::LValueReference:
3562 case clang::Type::RValueReference:
3563 pointee_qual_type =
3564 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3565 success = true;
3566 break;
3567
3568 case clang::Type::Typedef:
3569 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3570 ->getDecl()
3571 ->getUnderlyingType()
3572 .getAsOpaquePtr(),
3573 dynamic_pointee_type, check_cplusplus,
3574 check_objc);
3575
3576 case clang::Type::Auto:
3577 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3578 ->getDeducedType()
3579 .getAsOpaquePtr(),
3580 dynamic_pointee_type, check_cplusplus,
3581 check_objc);
3582
3583 case clang::Type::Elaborated:
3584 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3585 ->getNamedType()
3586 .getAsOpaquePtr(),
3587 dynamic_pointee_type, check_cplusplus,
3588 check_objc);
3589
3590 case clang::Type::Paren:
3591 return IsPossibleDynamicType(
3592 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3593 dynamic_pointee_type, check_cplusplus, check_objc);
3594 default:
3595 break;
3596 }
3597
3598 if (success) {
3599 // Check to make sure what we are pointing too is a possible dynamic C++
3600 // type
3601 // We currently accept any "void *" (in case we have a class that has been
3602 // watered down to an opaque pointer) and virtual C++ classes.
3603 const clang::Type::TypeClass pointee_type_class =
3604 pointee_qual_type.getCanonicalType()->getTypeClass();
3605 switch (pointee_type_class) {
3606 case clang::Type::Builtin:
3607 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3608 case clang::BuiltinType::UnknownAny:
3609 case clang::BuiltinType::Void:
3610 if (dynamic_pointee_type)
3611 dynamic_pointee_type->SetCompilerType(getASTContext(),
3612 pointee_qual_type);
3613 return true;
3614 default:
3615 break;
3616 }
3617 break;
3618
3619 case clang::Type::Record:
3620 if (check_cplusplus) {
3621 clang::CXXRecordDecl *cxx_record_decl =
3622 pointee_qual_type->getAsCXXRecordDecl();
3623 if (cxx_record_decl) {
3624 bool is_complete = cxx_record_decl->isCompleteDefinition();
3625
3626 if (is_complete)
3627 success = cxx_record_decl->isDynamicClass();
3628 else {
3629 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3630 getASTContext(), cxx_record_decl);
3631 if (metadata)
3632 success = metadata->GetIsDynamicCXXType();
3633 else {
3634 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3635 .GetCompleteType();
3636 if (is_complete)
3637 success = cxx_record_decl->isDynamicClass();
3638 else
3639 success = false;
3640 }
3641 }
3642
3643 if (success) {
3644 if (dynamic_pointee_type)
3645 dynamic_pointee_type->SetCompilerType(getASTContext(),
3646 pointee_qual_type);
3647 return true;
3648 }
3649 }
3650 }
3651 break;
3652
3653 case clang::Type::ObjCObject:
3654 case clang::Type::ObjCInterface:
3655 if (check_objc) {
3656 if (dynamic_pointee_type)
3657 dynamic_pointee_type->SetCompilerType(getASTContext(),
3658 pointee_qual_type);
3659 return true;
3660 }
3661 break;
3662
3663 default:
3664 break;
3665 }
3666 }
3667 }
3668 if (dynamic_pointee_type)
3669 dynamic_pointee_type->Clear();
3670 return false;
3671}
3672
3673bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3674 if (!type)
3675 return false;
3676
3677 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3678}
3679
3680bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3681 if (!type)
3682 return false;
3683 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3684}
3685
3686bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3687 if (!type)
3688 return false;
3689 return GetCanonicalQualType(type)->isVoidType();
3690}
3691
3692bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3693 return ClangASTContextSupportsLanguage(language);
3694}
3695
3696bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3697 std::string &class_name) {
3698 if (type) {
3699 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3700 if (!qual_type.isNull()) {
3701 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3702 if (cxx_record_decl) {
3703 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3704 return true;
3705 }
3706 }
3707 }
3708 class_name.clear();
3709 return false;
3710}
3711
3712bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3713 if (!type)
3714 return false;
3715
3716 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3717 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
3718 return true;
3719 return false;
3720}
3721
3722bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3723 if (!type)
3724 return false;
3725 clang::QualType qual_type(GetCanonicalQualType(type));
3726 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3727 if (tag_type)
3728 return tag_type->isBeingDefined();
3729 return false;
3730}
3731
3732bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3733 CompilerType *class_type_ptr) {
3734 if (!type)
3735 return false;
3736
3737 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3738
3739 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3740 if (class_type_ptr) {
3741 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3742 const clang::ObjCObjectPointerType *obj_pointer_type =
3743 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3744 if (obj_pointer_type == nullptr)
3745 class_type_ptr->Clear();
3746 else
3747 class_type_ptr->SetCompilerType(
3748 type.GetTypeSystem(),
3749 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3750 .getAsOpaquePtr());
3751 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003752 }
3753 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003754 }
3755 if (class_type_ptr)
3756 class_type_ptr->Clear();
3757 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003758}
3759
Kate Stoneb9c1b512016-09-06 20:57:50 +00003760bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3761 std::string &class_name) {
3762 if (!type)
3763 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003764
Kate Stoneb9c1b512016-09-06 20:57:50 +00003765 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3766
3767 const clang::ObjCObjectType *object_type =
3768 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3769 if (object_type) {
3770 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3771 if (interface) {
3772 class_name = interface->getNameAsString();
3773 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00003774 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003775 }
3776 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003777}
3778
Greg Claytond8d4a572015-08-11 21:38:15 +00003779//----------------------------------------------------------------------
3780// Type Completion
3781//----------------------------------------------------------------------
3782
Kate Stoneb9c1b512016-09-06 20:57:50 +00003783bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3784 if (!type)
3785 return false;
3786 const bool allow_completion = true;
3787 return GetCompleteQualType(getASTContext(), GetQualType(type),
3788 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00003789}
3790
Kate Stoneb9c1b512016-09-06 20:57:50 +00003791ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
3792 std::string type_name;
3793 if (type) {
3794 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
3795 clang::QualType qual_type(GetQualType(type));
3796 printing_policy.SuppressTagKeyword = true;
3797 const clang::TypedefType *typedef_type =
3798 qual_type->getAs<clang::TypedefType>();
3799 if (typedef_type) {
3800 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3801 type_name = typedef_decl->getQualifiedNameAsString();
3802 } else {
3803 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003804 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003805 }
3806 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00003807}
3808
3809uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003810ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
3811 CompilerType *pointee_or_element_clang_type) {
3812 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003813 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003814
3815 if (pointee_or_element_clang_type)
3816 pointee_or_element_clang_type->Clear();
3817
3818 clang::QualType qual_type(GetQualType(type));
3819
3820 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3821 switch (type_class) {
Sean Callananddf802a2017-06-02 01:24:18 +00003822 case clang::Type::Attributed:
3823 return GetTypeInfo(
3824 qual_type->getAs<clang::AttributedType>()
3825 ->getModifiedType().getAsOpaquePtr(),
3826 pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003827 case clang::Type::Builtin: {
3828 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3829 qual_type->getCanonicalTypeInternal());
3830
3831 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3832 switch (builtin_type->getKind()) {
3833 case clang::BuiltinType::ObjCId:
3834 case clang::BuiltinType::ObjCClass:
3835 if (pointee_or_element_clang_type)
3836 pointee_or_element_clang_type->SetCompilerType(
3837 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
3838 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3839 break;
3840
3841 case clang::BuiltinType::ObjCSel:
3842 if (pointee_or_element_clang_type)
3843 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
3844 getASTContext()->CharTy);
3845 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3846 break;
3847
3848 case clang::BuiltinType::Bool:
3849 case clang::BuiltinType::Char_U:
3850 case clang::BuiltinType::UChar:
3851 case clang::BuiltinType::WChar_U:
3852 case clang::BuiltinType::Char16:
3853 case clang::BuiltinType::Char32:
3854 case clang::BuiltinType::UShort:
3855 case clang::BuiltinType::UInt:
3856 case clang::BuiltinType::ULong:
3857 case clang::BuiltinType::ULongLong:
3858 case clang::BuiltinType::UInt128:
3859 case clang::BuiltinType::Char_S:
3860 case clang::BuiltinType::SChar:
3861 case clang::BuiltinType::WChar_S:
3862 case clang::BuiltinType::Short:
3863 case clang::BuiltinType::Int:
3864 case clang::BuiltinType::Long:
3865 case clang::BuiltinType::LongLong:
3866 case clang::BuiltinType::Int128:
3867 case clang::BuiltinType::Float:
3868 case clang::BuiltinType::Double:
3869 case clang::BuiltinType::LongDouble:
3870 builtin_type_flags |= eTypeIsScalar;
3871 if (builtin_type->isInteger()) {
3872 builtin_type_flags |= eTypeIsInteger;
3873 if (builtin_type->isSignedInteger())
3874 builtin_type_flags |= eTypeIsSigned;
3875 } else if (builtin_type->isFloatingPoint())
3876 builtin_type_flags |= eTypeIsFloat;
3877 break;
3878 default:
3879 break;
3880 }
3881 return builtin_type_flags;
3882 }
3883
3884 case clang::Type::BlockPointer:
3885 if (pointee_or_element_clang_type)
3886 pointee_or_element_clang_type->SetCompilerType(
3887 getASTContext(), qual_type->getPointeeType());
3888 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3889
3890 case clang::Type::Complex: {
3891 uint32_t complex_type_flags =
3892 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3893 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
3894 qual_type->getCanonicalTypeInternal());
3895 if (complex_type) {
3896 clang::QualType complex_element_type(complex_type->getElementType());
3897 if (complex_element_type->isIntegerType())
3898 complex_type_flags |= eTypeIsFloat;
3899 else if (complex_element_type->isFloatingType())
3900 complex_type_flags |= eTypeIsInteger;
3901 }
3902 return complex_type_flags;
3903 } break;
3904
3905 case clang::Type::ConstantArray:
3906 case clang::Type::DependentSizedArray:
3907 case clang::Type::IncompleteArray:
3908 case clang::Type::VariableArray:
3909 if (pointee_or_element_clang_type)
3910 pointee_or_element_clang_type->SetCompilerType(
3911 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
3912 ->getElementType());
3913 return eTypeHasChildren | eTypeIsArray;
3914
3915 case clang::Type::DependentName:
3916 return 0;
3917 case clang::Type::DependentSizedExtVector:
3918 return eTypeHasChildren | eTypeIsVector;
3919 case clang::Type::DependentTemplateSpecialization:
3920 return eTypeIsTemplate;
3921 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00003922 return CompilerType(
3923 getASTContext(),
3924 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
3925 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003926
3927 case clang::Type::Enum:
3928 if (pointee_or_element_clang_type)
3929 pointee_or_element_clang_type->SetCompilerType(
3930 getASTContext(),
3931 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
3932 return eTypeIsEnumeration | eTypeHasValue;
3933
3934 case clang::Type::Auto:
3935 return CompilerType(
3936 getASTContext(),
3937 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
3938 .GetTypeInfo(pointee_or_element_clang_type);
3939 case clang::Type::Elaborated:
3940 return CompilerType(
3941 getASTContext(),
3942 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
3943 .GetTypeInfo(pointee_or_element_clang_type);
3944 case clang::Type::Paren:
3945 return CompilerType(getASTContext(),
3946 llvm::cast<clang::ParenType>(qual_type)->desugar())
3947 .GetTypeInfo(pointee_or_element_clang_type);
3948
3949 case clang::Type::FunctionProto:
3950 return eTypeIsFuncPrototype | eTypeHasValue;
3951 case clang::Type::FunctionNoProto:
3952 return eTypeIsFuncPrototype | eTypeHasValue;
3953 case clang::Type::InjectedClassName:
3954 return 0;
3955
3956 case clang::Type::LValueReference:
3957 case clang::Type::RValueReference:
3958 if (pointee_or_element_clang_type)
3959 pointee_or_element_clang_type->SetCompilerType(
3960 getASTContext(),
3961 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
3962 ->getPointeeType());
3963 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
3964
3965 case clang::Type::MemberPointer:
3966 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
3967
3968 case clang::Type::ObjCObjectPointer:
3969 if (pointee_or_element_clang_type)
3970 pointee_or_element_clang_type->SetCompilerType(
3971 getASTContext(), qual_type->getPointeeType());
3972 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
3973 eTypeHasValue;
3974
3975 case clang::Type::ObjCObject:
3976 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3977 case clang::Type::ObjCInterface:
3978 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3979
3980 case clang::Type::Pointer:
3981 if (pointee_or_element_clang_type)
3982 pointee_or_element_clang_type->SetCompilerType(
3983 getASTContext(), qual_type->getPointeeType());
3984 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
3985
3986 case clang::Type::Record:
3987 if (qual_type->getAsCXXRecordDecl())
3988 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
3989 else
3990 return eTypeHasChildren | eTypeIsStructUnion;
3991 break;
3992 case clang::Type::SubstTemplateTypeParm:
3993 return eTypeIsTemplate;
3994 case clang::Type::TemplateTypeParm:
3995 return eTypeIsTemplate;
3996 case clang::Type::TemplateSpecialization:
3997 return eTypeIsTemplate;
3998
3999 case clang::Type::Typedef:
4000 return eTypeIsTypedef |
4001 CompilerType(getASTContext(),
4002 llvm::cast<clang::TypedefType>(qual_type)
4003 ->getDecl()
4004 ->getUnderlyingType())
4005 .GetTypeInfo(pointee_or_element_clang_type);
4006 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004007 return CompilerType(getASTContext(),
4008 llvm::cast<clang::TypeOfExprType>(qual_type)
4009 ->getUnderlyingExpr()
4010 ->getType())
4011 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004012 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004013 return CompilerType(
4014 getASTContext(),
4015 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4016 .GetTypeInfo(pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00004017 case clang::Type::UnresolvedUsing:
4018 return 0;
4019
4020 case clang::Type::ExtVector:
4021 case clang::Type::Vector: {
4022 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4023 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4024 qual_type->getCanonicalTypeInternal());
4025 if (vector_type) {
4026 if (vector_type->isIntegerType())
4027 vector_type_flags |= eTypeIsFloat;
4028 else if (vector_type->isFloatingType())
4029 vector_type_flags |= eTypeIsInteger;
4030 }
4031 return vector_type_flags;
4032 }
4033 default:
4034 return 0;
4035 }
4036 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004037}
4038
Greg Claytond8d4a572015-08-11 21:38:15 +00004039lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004040ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4041 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004042 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004043
4044 // If the type is a reference, then resolve it to what it refers to first:
4045 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4046 if (qual_type->isAnyPointerType()) {
4047 if (qual_type->isObjCObjectPointerType())
4048 return lldb::eLanguageTypeObjC;
4049
4050 clang::QualType pointee_type(qual_type->getPointeeType());
4051 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4052 return lldb::eLanguageTypeC_plus_plus;
4053 if (pointee_type->isObjCObjectOrInterfaceType())
4054 return lldb::eLanguageTypeObjC;
4055 if (pointee_type->isObjCClassType())
4056 return lldb::eLanguageTypeObjC;
4057 if (pointee_type.getTypePtr() ==
4058 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4059 return lldb::eLanguageTypeObjC;
4060 } else {
4061 if (qual_type->isObjCObjectOrInterfaceType())
4062 return lldb::eLanguageTypeObjC;
4063 if (qual_type->getAsCXXRecordDecl())
4064 return lldb::eLanguageTypeC_plus_plus;
4065 switch (qual_type->getTypeClass()) {
4066 default:
4067 break;
4068 case clang::Type::Builtin:
4069 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4070 default:
4071 case clang::BuiltinType::Void:
4072 case clang::BuiltinType::Bool:
4073 case clang::BuiltinType::Char_U:
4074 case clang::BuiltinType::UChar:
4075 case clang::BuiltinType::WChar_U:
4076 case clang::BuiltinType::Char16:
4077 case clang::BuiltinType::Char32:
4078 case clang::BuiltinType::UShort:
4079 case clang::BuiltinType::UInt:
4080 case clang::BuiltinType::ULong:
4081 case clang::BuiltinType::ULongLong:
4082 case clang::BuiltinType::UInt128:
4083 case clang::BuiltinType::Char_S:
4084 case clang::BuiltinType::SChar:
4085 case clang::BuiltinType::WChar_S:
4086 case clang::BuiltinType::Short:
4087 case clang::BuiltinType::Int:
4088 case clang::BuiltinType::Long:
4089 case clang::BuiltinType::LongLong:
4090 case clang::BuiltinType::Int128:
4091 case clang::BuiltinType::Float:
4092 case clang::BuiltinType::Double:
4093 case clang::BuiltinType::LongDouble:
4094 break;
4095
4096 case clang::BuiltinType::NullPtr:
4097 return eLanguageTypeC_plus_plus;
4098
4099 case clang::BuiltinType::ObjCId:
4100 case clang::BuiltinType::ObjCClass:
4101 case clang::BuiltinType::ObjCSel:
4102 return eLanguageTypeObjC;
4103
4104 case clang::BuiltinType::Dependent:
4105 case clang::BuiltinType::Overload:
4106 case clang::BuiltinType::BoundMember:
4107 case clang::BuiltinType::UnknownAny:
4108 break;
4109 }
4110 break;
4111 case clang::Type::Typedef:
4112 return CompilerType(getASTContext(),
4113 llvm::cast<clang::TypedefType>(qual_type)
4114 ->getDecl()
4115 ->getUnderlyingType())
4116 .GetMinimumLanguage();
4117 }
4118 }
4119 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004120}
4121
4122lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004123ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4124 if (!type)
4125 return lldb::eTypeClassInvalid;
4126
4127 clang::QualType qual_type(GetQualType(type));
4128
4129 switch (qual_type->getTypeClass()) {
4130 case clang::Type::UnaryTransform:
4131 break;
4132 case clang::Type::FunctionNoProto:
4133 return lldb::eTypeClassFunction;
4134 case clang::Type::FunctionProto:
4135 return lldb::eTypeClassFunction;
4136 case clang::Type::IncompleteArray:
4137 return lldb::eTypeClassArray;
4138 case clang::Type::VariableArray:
4139 return lldb::eTypeClassArray;
4140 case clang::Type::ConstantArray:
4141 return lldb::eTypeClassArray;
4142 case clang::Type::DependentSizedArray:
4143 return lldb::eTypeClassArray;
4144 case clang::Type::DependentSizedExtVector:
4145 return lldb::eTypeClassVector;
4146 case clang::Type::ExtVector:
4147 return lldb::eTypeClassVector;
4148 case clang::Type::Vector:
4149 return lldb::eTypeClassVector;
4150 case clang::Type::Builtin:
4151 return lldb::eTypeClassBuiltin;
4152 case clang::Type::ObjCObjectPointer:
4153 return lldb::eTypeClassObjCObjectPointer;
4154 case clang::Type::BlockPointer:
4155 return lldb::eTypeClassBlockPointer;
4156 case clang::Type::Pointer:
4157 return lldb::eTypeClassPointer;
4158 case clang::Type::LValueReference:
4159 return lldb::eTypeClassReference;
4160 case clang::Type::RValueReference:
4161 return lldb::eTypeClassReference;
4162 case clang::Type::MemberPointer:
4163 return lldb::eTypeClassMemberPointer;
4164 case clang::Type::Complex:
4165 if (qual_type->isComplexType())
4166 return lldb::eTypeClassComplexFloat;
4167 else
4168 return lldb::eTypeClassComplexInteger;
4169 case clang::Type::ObjCObject:
4170 return lldb::eTypeClassObjCObject;
4171 case clang::Type::ObjCInterface:
4172 return lldb::eTypeClassObjCInterface;
4173 case clang::Type::Record: {
4174 const clang::RecordType *record_type =
4175 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4176 const clang::RecordDecl *record_decl = record_type->getDecl();
4177 if (record_decl->isUnion())
4178 return lldb::eTypeClassUnion;
4179 else if (record_decl->isStruct())
4180 return lldb::eTypeClassStruct;
4181 else
4182 return lldb::eTypeClassClass;
4183 } break;
4184 case clang::Type::Enum:
4185 return lldb::eTypeClassEnumeration;
4186 case clang::Type::Typedef:
4187 return lldb::eTypeClassTypedef;
4188 case clang::Type::UnresolvedUsing:
4189 break;
4190 case clang::Type::Paren:
4191 return CompilerType(getASTContext(),
4192 llvm::cast<clang::ParenType>(qual_type)->desugar())
4193 .GetTypeClass();
4194 case clang::Type::Auto:
4195 return CompilerType(
4196 getASTContext(),
4197 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4198 .GetTypeClass();
4199 case clang::Type::Elaborated:
4200 return CompilerType(
4201 getASTContext(),
4202 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4203 .GetTypeClass();
4204
4205 case clang::Type::Attributed:
4206 break;
4207 case clang::Type::TemplateTypeParm:
4208 break;
4209 case clang::Type::SubstTemplateTypeParm:
4210 break;
4211 case clang::Type::SubstTemplateTypeParmPack:
4212 break;
4213 case clang::Type::InjectedClassName:
4214 break;
4215 case clang::Type::DependentName:
4216 break;
4217 case clang::Type::DependentTemplateSpecialization:
4218 break;
4219 case clang::Type::PackExpansion:
4220 break;
4221
4222 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004223 return CompilerType(getASTContext(),
4224 llvm::cast<clang::TypeOfExprType>(qual_type)
4225 ->getUnderlyingExpr()
4226 ->getType())
4227 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004228 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004229 return CompilerType(
4230 getASTContext(),
4231 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4232 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004233 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00004234 return CompilerType(
4235 getASTContext(),
4236 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4237 .GetTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004238 case clang::Type::TemplateSpecialization:
4239 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004240 case clang::Type::DeducedTemplateSpecialization:
4241 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004242 case clang::Type::Atomic:
4243 break;
4244 case clang::Type::Pipe:
4245 break;
4246
4247 // pointer type decayed from an array or function type.
4248 case clang::Type::Decayed:
4249 break;
4250 case clang::Type::Adjusted:
4251 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004252 case clang::Type::ObjCTypeParam:
4253 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00004254
4255 case clang::Type::DependentAddressSpace:
4256 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004257 }
4258 // We don't know hot to display this type...
4259 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004260}
4261
Kate Stoneb9c1b512016-09-06 20:57:50 +00004262unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4263 if (type)
4264 return GetQualType(type).getQualifiers().getCVRQualifiers();
4265 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004266}
4267
4268//----------------------------------------------------------------------
4269// Creating related types
4270//----------------------------------------------------------------------
4271
Greg Claytona1e5dc82015-08-11 22:53:00 +00004272CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004273ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4274 uint64_t *stride) {
4275 if (type) {
4276 clang::QualType qual_type(GetCanonicalQualType(type));
4277
4278 const clang::Type *array_eletype =
4279 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4280
4281 if (!array_eletype)
4282 return CompilerType();
4283
4284 CompilerType element_type(getASTContext(),
4285 array_eletype->getCanonicalTypeUnqualified());
4286
4287 // TODO: the real stride will be >= this value.. find the real one!
4288 if (stride)
4289 *stride = element_type.GetByteSize(nullptr);
4290
4291 return element_type;
4292 }
4293 return CompilerType();
4294}
4295
4296CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4297 uint64_t size) {
4298 if (type) {
4299 clang::QualType qual_type(GetCanonicalQualType(type));
4300 if (clang::ASTContext *ast_ctx = getASTContext()) {
4301 if (size != 0)
4302 return CompilerType(
4303 ast_ctx, ast_ctx->getConstantArrayType(
4304 qual_type, llvm::APInt(64, size),
4305 clang::ArrayType::ArraySizeModifier::Normal, 0));
4306 else
4307 return CompilerType(
4308 ast_ctx,
4309 ast_ctx->getIncompleteArrayType(
4310 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
Greg Claytond8d4a572015-08-11 21:38:15 +00004311 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004312 }
4313
4314 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004315}
4316
Greg Claytona1e5dc82015-08-11 22:53:00 +00004317CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004318ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4319 if (type)
4320 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4321 return CompilerType();
4322}
4323
4324static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4325 clang::QualType qual_type) {
4326 if (qual_type->isPointerType())
4327 qual_type = ast->getPointerType(
4328 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4329 else
4330 qual_type = qual_type.getUnqualifiedType();
4331 qual_type.removeLocalConst();
4332 qual_type.removeLocalRestrict();
4333 qual_type.removeLocalVolatile();
4334 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004335}
4336
4337CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004338ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4339 if (type)
4340 return CompilerType(
4341 getASTContext(),
4342 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4343 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004344}
4345
Kate Stoneb9c1b512016-09-06 20:57:50 +00004346int ClangASTContext::GetFunctionArgumentCount(
4347 lldb::opaque_compiler_type_t type) {
4348 if (type) {
4349 const clang::FunctionProtoType *func =
4350 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4351 if (func)
4352 return func->getNumParams();
4353 }
4354 return -1;
4355}
4356
4357CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4358 lldb::opaque_compiler_type_t type, size_t idx) {
4359 if (type) {
4360 const clang::FunctionProtoType *func =
4361 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4362 if (func) {
4363 const uint32_t num_args = func->getNumParams();
4364 if (idx < num_args)
4365 return CompilerType(getASTContext(), func->getParamType(idx));
4366 }
4367 }
4368 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004369}
4370
Greg Claytona1e5dc82015-08-11 22:53:00 +00004371CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004372ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4373 if (type) {
4374 clang::QualType qual_type(GetQualType(type));
4375 const clang::FunctionProtoType *func =
4376 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4377 if (func)
4378 return CompilerType(getASTContext(), func->getReturnType());
4379 }
4380 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004381}
4382
4383size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004384ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4385 size_t num_functions = 0;
4386 if (type) {
4387 clang::QualType qual_type(GetCanonicalQualType(type));
4388 switch (qual_type->getTypeClass()) {
4389 case clang::Type::Record:
4390 if (GetCompleteQualType(getASTContext(), qual_type)) {
4391 const clang::RecordType *record_type =
4392 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4393 const clang::RecordDecl *record_decl = record_type->getDecl();
4394 assert(record_decl);
4395 const clang::CXXRecordDecl *cxx_record_decl =
4396 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4397 if (cxx_record_decl)
4398 num_functions = std::distance(cxx_record_decl->method_begin(),
4399 cxx_record_decl->method_end());
4400 }
4401 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004402
Sean Callananf9c622a2016-09-30 18:44:43 +00004403 case clang::Type::ObjCObjectPointer: {
4404 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004405 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004406 const clang::ObjCInterfaceType *objc_interface_type =
4407 objc_class_type->getInterfaceType();
4408 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004409 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4410 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004411 clang::ObjCInterfaceDecl *class_interface_decl =
4412 objc_interface_type->getDecl();
4413 if (class_interface_decl) {
4414 num_functions = std::distance(class_interface_decl->meth_begin(),
4415 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004416 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004417 }
4418 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004419 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004420
4421 case clang::Type::ObjCObject:
4422 case clang::Type::ObjCInterface:
4423 if (GetCompleteType(type)) {
4424 const clang::ObjCObjectType *objc_class_type =
4425 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4426 if (objc_class_type) {
4427 clang::ObjCInterfaceDecl *class_interface_decl =
4428 objc_class_type->getInterface();
4429 if (class_interface_decl)
4430 num_functions = std::distance(class_interface_decl->meth_begin(),
4431 class_interface_decl->meth_end());
4432 }
4433 }
4434 break;
4435
4436 case clang::Type::Typedef:
4437 return CompilerType(getASTContext(),
4438 llvm::cast<clang::TypedefType>(qual_type)
4439 ->getDecl()
4440 ->getUnderlyingType())
4441 .GetNumMemberFunctions();
4442
4443 case clang::Type::Auto:
4444 return CompilerType(
4445 getASTContext(),
4446 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4447 .GetNumMemberFunctions();
4448
4449 case clang::Type::Elaborated:
4450 return CompilerType(
4451 getASTContext(),
4452 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4453 .GetNumMemberFunctions();
4454
4455 case clang::Type::Paren:
4456 return CompilerType(getASTContext(),
4457 llvm::cast<clang::ParenType>(qual_type)->desugar())
4458 .GetNumMemberFunctions();
4459
4460 default:
4461 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004462 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004463 }
4464 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004465}
4466
4467TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004468ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4469 size_t idx) {
4470 std::string name;
4471 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4472 CompilerType clang_type;
4473 CompilerDecl clang_decl;
4474 if (type) {
4475 clang::QualType qual_type(GetCanonicalQualType(type));
4476 switch (qual_type->getTypeClass()) {
4477 case clang::Type::Record:
4478 if (GetCompleteQualType(getASTContext(), qual_type)) {
4479 const clang::RecordType *record_type =
4480 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4481 const clang::RecordDecl *record_decl = record_type->getDecl();
4482 assert(record_decl);
4483 const clang::CXXRecordDecl *cxx_record_decl =
4484 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4485 if (cxx_record_decl) {
4486 auto method_iter = cxx_record_decl->method_begin();
4487 auto method_end = cxx_record_decl->method_end();
4488 if (idx <
4489 static_cast<size_t>(std::distance(method_iter, method_end))) {
4490 std::advance(method_iter, idx);
4491 clang::CXXMethodDecl *cxx_method_decl =
4492 method_iter->getCanonicalDecl();
4493 if (cxx_method_decl) {
4494 name = cxx_method_decl->getDeclName().getAsString();
4495 if (cxx_method_decl->isStatic())
4496 kind = lldb::eMemberFunctionKindStaticMethod;
4497 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4498 kind = lldb::eMemberFunctionKindConstructor;
4499 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4500 kind = lldb::eMemberFunctionKindDestructor;
4501 else
4502 kind = lldb::eMemberFunctionKindInstanceMethod;
4503 clang_type = CompilerType(
4504 this, cxx_method_decl->getType().getAsOpaquePtr());
4505 clang_decl = CompilerDecl(this, cxx_method_decl);
4506 }
4507 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004508 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004509 }
4510 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004511
Sean Callananf9c622a2016-09-30 18:44:43 +00004512 case clang::Type::ObjCObjectPointer: {
4513 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004514 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004515 const clang::ObjCInterfaceType *objc_interface_type =
4516 objc_class_type->getInterfaceType();
4517 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004518 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4519 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004520 clang::ObjCInterfaceDecl *class_interface_decl =
4521 objc_interface_type->getDecl();
4522 if (class_interface_decl) {
4523 auto method_iter = class_interface_decl->meth_begin();
4524 auto method_end = class_interface_decl->meth_end();
4525 if (idx <
4526 static_cast<size_t>(std::distance(method_iter, method_end))) {
4527 std::advance(method_iter, idx);
4528 clang::ObjCMethodDecl *objc_method_decl =
4529 method_iter->getCanonicalDecl();
4530 if (objc_method_decl) {
4531 clang_decl = CompilerDecl(this, objc_method_decl);
4532 name = objc_method_decl->getSelector().getAsString();
4533 if (objc_method_decl->isClassMethod())
4534 kind = lldb::eMemberFunctionKindStaticMethod;
4535 else
4536 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004537 }
4538 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004539 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004540 }
4541 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004542 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004543
Kate Stoneb9c1b512016-09-06 20:57:50 +00004544 case clang::Type::ObjCObject:
4545 case clang::Type::ObjCInterface:
4546 if (GetCompleteType(type)) {
4547 const clang::ObjCObjectType *objc_class_type =
4548 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4549 if (objc_class_type) {
4550 clang::ObjCInterfaceDecl *class_interface_decl =
4551 objc_class_type->getInterface();
4552 if (class_interface_decl) {
4553 auto method_iter = class_interface_decl->meth_begin();
4554 auto method_end = class_interface_decl->meth_end();
4555 if (idx <
4556 static_cast<size_t>(std::distance(method_iter, method_end))) {
4557 std::advance(method_iter, idx);
4558 clang::ObjCMethodDecl *objc_method_decl =
4559 method_iter->getCanonicalDecl();
4560 if (objc_method_decl) {
4561 clang_decl = CompilerDecl(this, objc_method_decl);
4562 name = objc_method_decl->getSelector().getAsString();
4563 if (objc_method_decl->isClassMethod())
4564 kind = lldb::eMemberFunctionKindStaticMethod;
4565 else
4566 kind = lldb::eMemberFunctionKindInstanceMethod;
4567 }
4568 }
4569 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004570 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004571 }
4572 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004573
Kate Stoneb9c1b512016-09-06 20:57:50 +00004574 case clang::Type::Typedef:
4575 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4576 ->getDecl()
4577 ->getUnderlyingType()
4578 .getAsOpaquePtr(),
4579 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004580
Kate Stoneb9c1b512016-09-06 20:57:50 +00004581 case clang::Type::Auto:
4582 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4583 ->getDeducedType()
4584 .getAsOpaquePtr(),
4585 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004586
Kate Stoneb9c1b512016-09-06 20:57:50 +00004587 case clang::Type::Elaborated:
4588 return GetMemberFunctionAtIndex(
4589 llvm::cast<clang::ElaboratedType>(qual_type)
4590 ->getNamedType()
4591 .getAsOpaquePtr(),
4592 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004593
Kate Stoneb9c1b512016-09-06 20:57:50 +00004594 case clang::Type::Paren:
4595 return GetMemberFunctionAtIndex(
4596 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4597 idx);
4598
4599 default:
4600 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004601 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004602 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004603
Kate Stoneb9c1b512016-09-06 20:57:50 +00004604 if (kind == eMemberFunctionKindUnknown)
4605 return TypeMemberFunctionImpl();
4606 else
4607 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004608}
4609
Greg Claytona1e5dc82015-08-11 22:53:00 +00004610CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004611ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4612 if (type)
4613 return CompilerType(getASTContext(),
4614 GetQualType(type).getNonReferenceType());
4615 return CompilerType();
4616}
4617
4618CompilerType ClangASTContext::CreateTypedefType(
4619 const CompilerType &type, const char *typedef_name,
4620 const CompilerDeclContext &compiler_decl_ctx) {
4621 if (type && typedef_name && typedef_name[0]) {
4622 ClangASTContext *ast =
4623 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4624 if (!ast)
4625 return CompilerType();
4626 clang::ASTContext *clang_ast = ast->getASTContext();
4627 clang::QualType qual_type(ClangUtil::GetQualType(type));
4628
4629 clang::DeclContext *decl_ctx =
4630 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4631 if (decl_ctx == nullptr)
4632 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4633
4634 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4635 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4636 &clang_ast->Idents.get(typedef_name),
4637 clang_ast->getTrivialTypeSourceInfo(qual_type));
4638
4639 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4640
4641 // Get a uniqued clang::QualType for the typedef decl type
4642 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4643 }
4644 return CompilerType();
4645}
4646
4647CompilerType
4648ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4649 if (type) {
4650 clang::QualType qual_type(GetQualType(type));
4651 return CompilerType(getASTContext(),
4652 qual_type.getTypePtr()->getPointeeType());
4653 }
4654 return CompilerType();
4655}
4656
4657CompilerType
4658ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4659 if (type) {
4660 clang::QualType qual_type(GetQualType(type));
4661
4662 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4663 switch (type_class) {
4664 case clang::Type::ObjCObject:
4665 case clang::Type::ObjCInterface:
4666 return CompilerType(getASTContext(),
4667 getASTContext()->getObjCObjectPointerType(qual_type));
4668
4669 default:
4670 return CompilerType(getASTContext(),
4671 getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004672 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004673 }
4674 return CompilerType();
4675}
4676
4677CompilerType
4678ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4679 if (type)
4680 return CompilerType(this, getASTContext()
4681 ->getLValueReferenceType(GetQualType(type))
4682 .getAsOpaquePtr());
4683 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004684 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004685}
4686
Kate Stoneb9c1b512016-09-06 20:57:50 +00004687CompilerType
4688ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4689 if (type)
4690 return CompilerType(this, getASTContext()
4691 ->getRValueReferenceType(GetQualType(type))
4692 .getAsOpaquePtr());
4693 else
4694 return CompilerType();
4695}
4696
4697CompilerType
4698ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4699 if (type) {
4700 clang::QualType result(GetQualType(type));
4701 result.addConst();
4702 return CompilerType(this, result.getAsOpaquePtr());
4703 }
4704 return CompilerType();
4705}
4706
4707CompilerType
4708ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4709 if (type) {
4710 clang::QualType result(GetQualType(type));
4711 result.addVolatile();
4712 return CompilerType(this, result.getAsOpaquePtr());
4713 }
4714 return CompilerType();
4715}
4716
4717CompilerType
4718ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4719 if (type) {
4720 clang::QualType result(GetQualType(type));
4721 result.addRestrict();
4722 return CompilerType(this, result.getAsOpaquePtr());
4723 }
4724 return CompilerType();
4725}
4726
4727CompilerType
4728ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4729 const char *typedef_name,
4730 const CompilerDeclContext &compiler_decl_ctx) {
4731 if (type) {
4732 clang::ASTContext *clang_ast = getASTContext();
4733 clang::QualType qual_type(GetQualType(type));
4734
4735 clang::DeclContext *decl_ctx =
4736 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4737 if (decl_ctx == nullptr)
4738 decl_ctx = getASTContext()->getTranslationUnitDecl();
4739
4740 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4741 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4742 &clang_ast->Idents.get(typedef_name),
4743 clang_ast->getTrivialTypeSourceInfo(qual_type));
4744
4745 clang::TagDecl *tdecl = nullptr;
4746 if (!qual_type.isNull()) {
4747 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4748 tdecl = rt->getDecl();
4749 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4750 tdecl = et->getDecl();
4751 }
4752
4753 // Check whether this declaration is an anonymous struct, union, or enum,
4754 // hidden behind a typedef. If so, we
4755 // try to check whether we have a typedef tag to attach to the original
4756 // record declaration
4757 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4758 tdecl->setTypedefNameForAnonDecl(decl);
4759
4760 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4761
4762 // Get a uniqued clang::QualType for the typedef decl type
4763 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4764 }
4765 return CompilerType();
4766}
4767
4768CompilerType
4769ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4770 if (type) {
4771 const clang::TypedefType *typedef_type =
4772 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4773 if (typedef_type)
4774 return CompilerType(getASTContext(),
4775 typedef_type->getDecl()->getUnderlyingType());
4776 }
4777 return CompilerType();
4778}
Greg Claytond8d4a572015-08-11 21:38:15 +00004779
4780//----------------------------------------------------------------------
4781// Create related types using the current type's AST
4782//----------------------------------------------------------------------
4783
Kate Stoneb9c1b512016-09-06 20:57:50 +00004784CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4785 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004786}
4787//----------------------------------------------------------------------
4788// Exploring the type
4789//----------------------------------------------------------------------
4790
Kate Stoneb9c1b512016-09-06 20:57:50 +00004791uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4792 ExecutionContextScope *exe_scope) {
4793 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00004794 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004795 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004796 switch (type_class) {
4797 case clang::Type::Record:
4798 if (GetCompleteType(type))
4799 return getASTContext()->getTypeSize(qual_type);
4800 else
4801 return 0;
4802 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004803
Kate Stoneb9c1b512016-09-06 20:57:50 +00004804 case clang::Type::ObjCInterface:
4805 case clang::Type::ObjCObject: {
4806 ExecutionContext exe_ctx(exe_scope);
4807 Process *process = exe_ctx.GetProcessPtr();
4808 if (process) {
4809 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4810 if (objc_runtime) {
4811 uint64_t bit_size = 0;
4812 if (objc_runtime->GetTypeBitSize(
4813 CompilerType(getASTContext(), qual_type), bit_size))
4814 return bit_size;
4815 }
4816 } else {
4817 static bool g_printed = false;
4818 if (!g_printed) {
4819 StreamString s;
4820 DumpTypeDescription(type, &s);
4821
4822 llvm::outs() << "warning: trying to determine the size of type ";
4823 llvm::outs() << s.GetString() << "\n";
4824 llvm::outs() << "without a valid ExecutionContext. this is not "
4825 "reliable. please file a bug against LLDB.\n";
4826 llvm::outs() << "backtrace:\n";
4827 llvm::sys::PrintStackTrace(llvm::outs());
4828 llvm::outs() << "\n";
4829 g_printed = true;
4830 }
4831 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004832 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004833 LLVM_FALLTHROUGH;
4834 default:
4835 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
4836 if (bit_size == 0) {
4837 if (qual_type->isIncompleteArrayType())
4838 return getASTContext()->getTypeSize(
4839 qual_type->getArrayElementTypeNoTypeQual()
4840 ->getCanonicalTypeUnqualified());
4841 }
4842 if (qual_type->isObjCObjectOrInterfaceType())
4843 return bit_size +
4844 getASTContext()->getTypeSize(
4845 getASTContext()->ObjCBuiltinClassTy);
4846 return bit_size;
4847 }
4848 }
4849 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004850}
4851
Kate Stoneb9c1b512016-09-06 20:57:50 +00004852size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
4853 if (GetCompleteType(type))
4854 return getASTContext()->getTypeAlign(GetQualType(type));
4855 return 0;
4856}
4857
4858lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
4859 uint64_t &count) {
4860 if (!type)
4861 return lldb::eEncodingInvalid;
4862
4863 count = 1;
4864 clang::QualType qual_type(GetCanonicalQualType(type));
4865
4866 switch (qual_type->getTypeClass()) {
4867 case clang::Type::UnaryTransform:
4868 break;
4869
4870 case clang::Type::FunctionNoProto:
4871 case clang::Type::FunctionProto:
4872 break;
4873
4874 case clang::Type::IncompleteArray:
4875 case clang::Type::VariableArray:
4876 break;
4877
4878 case clang::Type::ConstantArray:
4879 break;
4880
4881 case clang::Type::ExtVector:
4882 case clang::Type::Vector:
4883 // TODO: Set this to more than one???
4884 break;
4885
4886 case clang::Type::Builtin:
4887 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4888 case clang::BuiltinType::Void:
4889 break;
4890
4891 case clang::BuiltinType::Bool:
4892 case clang::BuiltinType::Char_S:
4893 case clang::BuiltinType::SChar:
4894 case clang::BuiltinType::WChar_S:
4895 case clang::BuiltinType::Char16:
4896 case clang::BuiltinType::Char32:
4897 case clang::BuiltinType::Short:
4898 case clang::BuiltinType::Int:
4899 case clang::BuiltinType::Long:
4900 case clang::BuiltinType::LongLong:
4901 case clang::BuiltinType::Int128:
4902 return lldb::eEncodingSint;
4903
4904 case clang::BuiltinType::Char_U:
4905 case clang::BuiltinType::UChar:
4906 case clang::BuiltinType::WChar_U:
4907 case clang::BuiltinType::UShort:
4908 case clang::BuiltinType::UInt:
4909 case clang::BuiltinType::ULong:
4910 case clang::BuiltinType::ULongLong:
4911 case clang::BuiltinType::UInt128:
4912 return lldb::eEncodingUint;
4913
4914 case clang::BuiltinType::Half:
4915 case clang::BuiltinType::Float:
Ted Woodward4355c7c2017-09-20 19:16:53 +00004916 case clang::BuiltinType::Float16:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004917 case clang::BuiltinType::Float128:
4918 case clang::BuiltinType::Double:
4919 case clang::BuiltinType::LongDouble:
4920 return lldb::eEncodingIEEE754;
4921
4922 case clang::BuiltinType::ObjCClass:
4923 case clang::BuiltinType::ObjCId:
4924 case clang::BuiltinType::ObjCSel:
4925 return lldb::eEncodingUint;
4926
4927 case clang::BuiltinType::NullPtr:
4928 return lldb::eEncodingUint;
4929
4930 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4931 case clang::BuiltinType::Kind::BoundMember:
4932 case clang::BuiltinType::Kind::BuiltinFn:
4933 case clang::BuiltinType::Kind::Dependent:
4934 case clang::BuiltinType::Kind::OCLClkEvent:
4935 case clang::BuiltinType::Kind::OCLEvent:
4936 case clang::BuiltinType::Kind::OCLImage1dRO:
4937 case clang::BuiltinType::Kind::OCLImage1dWO:
4938 case clang::BuiltinType::Kind::OCLImage1dRW:
4939 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
4940 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
4941 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
4942 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
4943 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
4944 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
4945 case clang::BuiltinType::Kind::OCLImage2dRO:
4946 case clang::BuiltinType::Kind::OCLImage2dWO:
4947 case clang::BuiltinType::Kind::OCLImage2dRW:
4948 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
4949 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
4950 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
4951 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
4952 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
4953 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
4954 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
4955 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
4956 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
4957 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
4958 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
4959 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
4960 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
4961 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
4962 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
4963 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
4964 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
4965 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
4966 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
4967 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
4968 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
4969 case clang::BuiltinType::Kind::OCLImage3dRO:
4970 case clang::BuiltinType::Kind::OCLImage3dWO:
4971 case clang::BuiltinType::Kind::OCLImage3dRW:
4972 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004973 case clang::BuiltinType::Kind::OCLReserveID:
4974 case clang::BuiltinType::Kind::OCLSampler:
4975 case clang::BuiltinType::Kind::OMPArraySection:
4976 case clang::BuiltinType::Kind::Overload:
4977 case clang::BuiltinType::Kind::PseudoObject:
4978 case clang::BuiltinType::Kind::UnknownAny:
4979 break;
4980 }
4981 break;
4982 // All pointer types are represented as unsigned integer encodings.
4983 // We may nee to add a eEncodingPointer if we ever need to know the
4984 // difference
4985 case clang::Type::ObjCObjectPointer:
4986 case clang::Type::BlockPointer:
4987 case clang::Type::Pointer:
4988 case clang::Type::LValueReference:
4989 case clang::Type::RValueReference:
4990 case clang::Type::MemberPointer:
4991 return lldb::eEncodingUint;
4992 case clang::Type::Complex: {
4993 lldb::Encoding encoding = lldb::eEncodingIEEE754;
4994 if (qual_type->isComplexType())
4995 encoding = lldb::eEncodingIEEE754;
4996 else {
4997 const clang::ComplexType *complex_type =
4998 qual_type->getAsComplexIntegerType();
4999 if (complex_type)
5000 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5001 .GetEncoding(count);
5002 else
5003 encoding = lldb::eEncodingSint;
5004 }
5005 count = 2;
5006 return encoding;
5007 }
5008
5009 case clang::Type::ObjCInterface:
5010 break;
5011 case clang::Type::Record:
5012 break;
5013 case clang::Type::Enum:
5014 return lldb::eEncodingSint;
5015 case clang::Type::Typedef:
5016 return CompilerType(getASTContext(),
5017 llvm::cast<clang::TypedefType>(qual_type)
5018 ->getDecl()
5019 ->getUnderlyingType())
5020 .GetEncoding(count);
5021
5022 case clang::Type::Auto:
5023 return CompilerType(
5024 getASTContext(),
5025 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5026 .GetEncoding(count);
5027
5028 case clang::Type::Elaborated:
5029 return CompilerType(
5030 getASTContext(),
5031 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5032 .GetEncoding(count);
5033
5034 case clang::Type::Paren:
5035 return CompilerType(getASTContext(),
5036 llvm::cast<clang::ParenType>(qual_type)->desugar())
5037 .GetEncoding(count);
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005038 case clang::Type::TypeOfExpr:
5039 return CompilerType(getASTContext(),
5040 llvm::cast<clang::TypeOfExprType>(qual_type)
5041 ->getUnderlyingExpr()
5042 ->getType())
5043 .GetEncoding(count);
5044 case clang::Type::TypeOf:
5045 return CompilerType(
5046 getASTContext(),
5047 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5048 .GetEncoding(count);
5049 case clang::Type::Decltype:
5050 return CompilerType(
5051 getASTContext(),
5052 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5053 .GetEncoding(count);
Kate Stoneb9c1b512016-09-06 20:57:50 +00005054 case clang::Type::DependentSizedArray:
5055 case clang::Type::DependentSizedExtVector:
5056 case clang::Type::UnresolvedUsing:
5057 case clang::Type::Attributed:
5058 case clang::Type::TemplateTypeParm:
5059 case clang::Type::SubstTemplateTypeParm:
5060 case clang::Type::SubstTemplateTypeParmPack:
5061 case clang::Type::InjectedClassName:
5062 case clang::Type::DependentName:
5063 case clang::Type::DependentTemplateSpecialization:
5064 case clang::Type::PackExpansion:
5065 case clang::Type::ObjCObject:
5066
Kate Stoneb9c1b512016-09-06 20:57:50 +00005067 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005068 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005069 case clang::Type::Atomic:
5070 case clang::Type::Adjusted:
5071 case clang::Type::Pipe:
5072 break;
5073
5074 // pointer type decayed from an array or function type.
5075 case clang::Type::Decayed:
5076 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005077 case clang::Type::ObjCTypeParam:
5078 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005079
5080 case clang::Type::DependentAddressSpace:
5081 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005082 }
5083 count = 0;
5084 return lldb::eEncodingInvalid;
5085}
5086
5087lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5088 if (!type)
5089 return lldb::eFormatDefault;
5090
5091 clang::QualType qual_type(GetCanonicalQualType(type));
5092
5093 switch (qual_type->getTypeClass()) {
5094 case clang::Type::UnaryTransform:
5095 break;
5096
5097 case clang::Type::FunctionNoProto:
5098 case clang::Type::FunctionProto:
5099 break;
5100
5101 case clang::Type::IncompleteArray:
5102 case clang::Type::VariableArray:
5103 break;
5104
5105 case clang::Type::ConstantArray:
5106 return lldb::eFormatVoid; // no value
5107
5108 case clang::Type::ExtVector:
5109 case clang::Type::Vector:
5110 break;
5111
5112 case clang::Type::Builtin:
5113 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5114 // default: assert(0 && "Unknown builtin type!");
5115 case clang::BuiltinType::UnknownAny:
5116 case clang::BuiltinType::Void:
5117 case clang::BuiltinType::BoundMember:
5118 break;
5119
5120 case clang::BuiltinType::Bool:
5121 return lldb::eFormatBoolean;
5122 case clang::BuiltinType::Char_S:
5123 case clang::BuiltinType::SChar:
5124 case clang::BuiltinType::WChar_S:
5125 case clang::BuiltinType::Char_U:
5126 case clang::BuiltinType::UChar:
5127 case clang::BuiltinType::WChar_U:
5128 return lldb::eFormatChar;
5129 case clang::BuiltinType::Char16:
5130 return lldb::eFormatUnicode16;
5131 case clang::BuiltinType::Char32:
5132 return lldb::eFormatUnicode32;
5133 case clang::BuiltinType::UShort:
5134 return lldb::eFormatUnsigned;
5135 case clang::BuiltinType::Short:
5136 return lldb::eFormatDecimal;
5137 case clang::BuiltinType::UInt:
5138 return lldb::eFormatUnsigned;
5139 case clang::BuiltinType::Int:
5140 return lldb::eFormatDecimal;
5141 case clang::BuiltinType::ULong:
5142 return lldb::eFormatUnsigned;
5143 case clang::BuiltinType::Long:
5144 return lldb::eFormatDecimal;
5145 case clang::BuiltinType::ULongLong:
5146 return lldb::eFormatUnsigned;
5147 case clang::BuiltinType::LongLong:
5148 return lldb::eFormatDecimal;
5149 case clang::BuiltinType::UInt128:
5150 return lldb::eFormatUnsigned;
5151 case clang::BuiltinType::Int128:
5152 return lldb::eFormatDecimal;
5153 case clang::BuiltinType::Half:
5154 case clang::BuiltinType::Float:
5155 case clang::BuiltinType::Double:
5156 case clang::BuiltinType::LongDouble:
5157 return lldb::eFormatFloat;
5158 default:
5159 return lldb::eFormatHex;
5160 }
5161 break;
5162 case clang::Type::ObjCObjectPointer:
5163 return lldb::eFormatHex;
5164 case clang::Type::BlockPointer:
5165 return lldb::eFormatHex;
5166 case clang::Type::Pointer:
5167 return lldb::eFormatHex;
5168 case clang::Type::LValueReference:
5169 case clang::Type::RValueReference:
5170 return lldb::eFormatHex;
5171 case clang::Type::MemberPointer:
5172 break;
5173 case clang::Type::Complex: {
5174 if (qual_type->isComplexType())
5175 return lldb::eFormatComplex;
5176 else
5177 return lldb::eFormatComplexInteger;
5178 }
5179 case clang::Type::ObjCInterface:
5180 break;
5181 case clang::Type::Record:
5182 break;
5183 case clang::Type::Enum:
5184 return lldb::eFormatEnum;
5185 case clang::Type::Typedef:
5186 return CompilerType(getASTContext(),
5187 llvm::cast<clang::TypedefType>(qual_type)
5188 ->getDecl()
5189 ->getUnderlyingType())
5190 .GetFormat();
5191 case clang::Type::Auto:
5192 return CompilerType(getASTContext(),
5193 llvm::cast<clang::AutoType>(qual_type)->desugar())
5194 .GetFormat();
5195 case clang::Type::Paren:
5196 return CompilerType(getASTContext(),
5197 llvm::cast<clang::ParenType>(qual_type)->desugar())
5198 .GetFormat();
5199 case clang::Type::Elaborated:
5200 return CompilerType(
5201 getASTContext(),
5202 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5203 .GetFormat();
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00005204 case clang::Type::TypeOfExpr:
5205 return CompilerType(getASTContext(),
5206 llvm::cast<clang::TypeOfExprType>(qual_type)
5207 ->getUnderlyingExpr()
5208 ->getType())
5209 .GetFormat();
5210 case clang::Type::TypeOf:
5211 return CompilerType(
5212 getASTContext(),
5213 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5214 .GetFormat();
5215 case clang::Type::Decltype:
5216 return CompilerType(
5217 getASTContext(),
5218 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5219 .GetFormat();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005220 case clang::Type::DependentSizedArray:
5221 case clang::Type::DependentSizedExtVector:
5222 case clang::Type::UnresolvedUsing:
5223 case clang::Type::Attributed:
5224 case clang::Type::TemplateTypeParm:
5225 case clang::Type::SubstTemplateTypeParm:
5226 case clang::Type::SubstTemplateTypeParmPack:
5227 case clang::Type::InjectedClassName:
5228 case clang::Type::DependentName:
5229 case clang::Type::DependentTemplateSpecialization:
5230 case clang::Type::PackExpansion:
5231 case clang::Type::ObjCObject:
5232
Kate Stoneb9c1b512016-09-06 20:57:50 +00005233 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005234 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005235 case clang::Type::Atomic:
5236 case clang::Type::Adjusted:
5237 case clang::Type::Pipe:
5238 break;
5239
5240 // pointer type decayed from an array or function type.
5241 case clang::Type::Decayed:
5242 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005243 case clang::Type::ObjCTypeParam:
5244 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005245
5246 case clang::Type::DependentAddressSpace:
5247 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005248 }
5249 // We don't know hot to display this type...
5250 return lldb::eFormatBytes;
5251}
5252
5253static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5254 bool check_superclass) {
5255 while (class_interface_decl) {
5256 if (class_interface_decl->ivar_size() > 0)
5257 return true;
5258
5259 if (check_superclass)
5260 class_interface_decl = class_interface_decl->getSuperClass();
5261 else
5262 break;
5263 }
5264 return false;
5265}
5266
5267uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
5268 bool omit_empty_base_classes) {
5269 if (!type)
5270 return 0;
5271
5272 uint32_t num_children = 0;
5273 clang::QualType qual_type(GetQualType(type));
5274 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5275 switch (type_class) {
5276 case clang::Type::Builtin:
5277 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5278 case clang::BuiltinType::ObjCId: // child is Class
5279 case clang::BuiltinType::ObjCClass: // child is Class
5280 num_children = 1;
5281 break;
5282
5283 default:
5284 break;
5285 }
5286 break;
5287
5288 case clang::Type::Complex:
5289 return 0;
5290
5291 case clang::Type::Record:
5292 if (GetCompleteQualType(getASTContext(), qual_type)) {
5293 const clang::RecordType *record_type =
5294 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5295 const clang::RecordDecl *record_decl = record_type->getDecl();
5296 assert(record_decl);
5297 const clang::CXXRecordDecl *cxx_record_decl =
5298 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5299 if (cxx_record_decl) {
5300 if (omit_empty_base_classes) {
5301 // Check each base classes to see if it or any of its
5302 // base classes contain any fields. This can help
5303 // limit the noise in variable views by not having to
5304 // show base classes that contain no members.
5305 clang::CXXRecordDecl::base_class_const_iterator base_class,
5306 base_class_end;
5307 for (base_class = cxx_record_decl->bases_begin(),
5308 base_class_end = cxx_record_decl->bases_end();
5309 base_class != base_class_end; ++base_class) {
5310 const clang::CXXRecordDecl *base_class_decl =
5311 llvm::cast<clang::CXXRecordDecl>(
5312 base_class->getType()
5313 ->getAs<clang::RecordType>()
5314 ->getDecl());
5315
5316 // Skip empty base classes
5317 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5318 continue;
5319
5320 num_children++;
5321 }
5322 } else {
5323 // Include all base classes
5324 num_children += cxx_record_decl->getNumBases();
5325 }
5326 }
5327 clang::RecordDecl::field_iterator field, field_end;
5328 for (field = record_decl->field_begin(),
5329 field_end = record_decl->field_end();
5330 field != field_end; ++field)
5331 ++num_children;
5332 }
5333 break;
5334
5335 case clang::Type::ObjCObject:
5336 case clang::Type::ObjCInterface:
5337 if (GetCompleteQualType(getASTContext(), qual_type)) {
5338 const clang::ObjCObjectType *objc_class_type =
5339 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5340 assert(objc_class_type);
5341 if (objc_class_type) {
5342 clang::ObjCInterfaceDecl *class_interface_decl =
5343 objc_class_type->getInterface();
5344
5345 if (class_interface_decl) {
5346
5347 clang::ObjCInterfaceDecl *superclass_interface_decl =
5348 class_interface_decl->getSuperClass();
5349 if (superclass_interface_decl) {
5350 if (omit_empty_base_classes) {
5351 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5352 ++num_children;
5353 } else
5354 ++num_children;
5355 }
5356
5357 num_children += class_interface_decl->ivar_size();
5358 }
5359 }
5360 }
5361 break;
5362
5363 case clang::Type::ObjCObjectPointer: {
5364 const clang::ObjCObjectPointerType *pointer_type =
5365 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5366 clang::QualType pointee_type = pointer_type->getPointeeType();
5367 uint32_t num_pointee_children =
5368 CompilerType(getASTContext(), pointee_type)
5369 .GetNumChildren(omit_empty_base_classes);
5370 // If this type points to a simple type, then it has 1 child
5371 if (num_pointee_children == 0)
5372 num_children = 1;
5373 else
5374 num_children = num_pointee_children;
5375 } break;
5376
5377 case clang::Type::Vector:
5378 case clang::Type::ExtVector:
5379 num_children =
5380 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5381 break;
5382
5383 case clang::Type::ConstantArray:
5384 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5385 ->getSize()
5386 .getLimitedValue();
5387 break;
5388
5389 case clang::Type::Pointer: {
5390 const clang::PointerType *pointer_type =
5391 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5392 clang::QualType pointee_type(pointer_type->getPointeeType());
5393 uint32_t num_pointee_children =
5394 CompilerType(getASTContext(), pointee_type)
5395 .GetNumChildren(omit_empty_base_classes);
5396 if (num_pointee_children == 0) {
5397 // We have a pointer to a pointee type that claims it has no children.
5398 // We will want to look at
5399 num_children = GetNumPointeeChildren(pointee_type);
5400 } else
5401 num_children = num_pointee_children;
5402 } break;
5403
5404 case clang::Type::LValueReference:
5405 case clang::Type::RValueReference: {
5406 const clang::ReferenceType *reference_type =
5407 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5408 clang::QualType pointee_type = reference_type->getPointeeType();
5409 uint32_t num_pointee_children =
5410 CompilerType(getASTContext(), pointee_type)
5411 .GetNumChildren(omit_empty_base_classes);
5412 // If this type points to a simple type, then it has 1 child
5413 if (num_pointee_children == 0)
5414 num_children = 1;
5415 else
5416 num_children = num_pointee_children;
5417 } break;
5418
5419 case clang::Type::Typedef:
5420 num_children =
5421 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5422 ->getDecl()
5423 ->getUnderlyingType())
5424 .GetNumChildren(omit_empty_base_classes);
5425 break;
5426
5427 case clang::Type::Auto:
5428 num_children =
5429 CompilerType(getASTContext(),
5430 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5431 .GetNumChildren(omit_empty_base_classes);
5432 break;
5433
5434 case clang::Type::Elaborated:
5435 num_children =
5436 CompilerType(
5437 getASTContext(),
5438 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5439 .GetNumChildren(omit_empty_base_classes);
5440 break;
5441
5442 case clang::Type::Paren:
5443 num_children =
5444 CompilerType(getASTContext(),
5445 llvm::cast<clang::ParenType>(qual_type)->desugar())
5446 .GetNumChildren(omit_empty_base_classes);
5447 break;
5448 default:
5449 break;
5450 }
5451 return num_children;
5452}
5453
5454CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5455 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005456}
5457
Greg Claytond8d4a572015-08-11 21:38:15 +00005458lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005459ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5460 if (type) {
5461 clang::QualType qual_type(GetQualType(type));
5462 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5463 if (type_class == clang::Type::Builtin) {
5464 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5465 case clang::BuiltinType::Void:
5466 return eBasicTypeVoid;
5467 case clang::BuiltinType::Bool:
5468 return eBasicTypeBool;
5469 case clang::BuiltinType::Char_S:
5470 return eBasicTypeSignedChar;
5471 case clang::BuiltinType::Char_U:
5472 return eBasicTypeUnsignedChar;
5473 case clang::BuiltinType::Char16:
5474 return eBasicTypeChar16;
5475 case clang::BuiltinType::Char32:
5476 return eBasicTypeChar32;
5477 case clang::BuiltinType::UChar:
5478 return eBasicTypeUnsignedChar;
5479 case clang::BuiltinType::SChar:
5480 return eBasicTypeSignedChar;
5481 case clang::BuiltinType::WChar_S:
5482 return eBasicTypeSignedWChar;
5483 case clang::BuiltinType::WChar_U:
5484 return eBasicTypeUnsignedWChar;
5485 case clang::BuiltinType::Short:
5486 return eBasicTypeShort;
5487 case clang::BuiltinType::UShort:
5488 return eBasicTypeUnsignedShort;
5489 case clang::BuiltinType::Int:
5490 return eBasicTypeInt;
5491 case clang::BuiltinType::UInt:
5492 return eBasicTypeUnsignedInt;
5493 case clang::BuiltinType::Long:
5494 return eBasicTypeLong;
5495 case clang::BuiltinType::ULong:
5496 return eBasicTypeUnsignedLong;
5497 case clang::BuiltinType::LongLong:
5498 return eBasicTypeLongLong;
5499 case clang::BuiltinType::ULongLong:
5500 return eBasicTypeUnsignedLongLong;
5501 case clang::BuiltinType::Int128:
5502 return eBasicTypeInt128;
5503 case clang::BuiltinType::UInt128:
5504 return eBasicTypeUnsignedInt128;
5505
5506 case clang::BuiltinType::Half:
5507 return eBasicTypeHalf;
5508 case clang::BuiltinType::Float:
5509 return eBasicTypeFloat;
5510 case clang::BuiltinType::Double:
5511 return eBasicTypeDouble;
5512 case clang::BuiltinType::LongDouble:
5513 return eBasicTypeLongDouble;
5514
5515 case clang::BuiltinType::NullPtr:
5516 return eBasicTypeNullPtr;
5517 case clang::BuiltinType::ObjCId:
5518 return eBasicTypeObjCID;
5519 case clang::BuiltinType::ObjCClass:
5520 return eBasicTypeObjCClass;
5521 case clang::BuiltinType::ObjCSel:
5522 return eBasicTypeObjCSel;
5523 default:
5524 return eBasicTypeOther;
5525 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005526 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005527 }
5528 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005529}
5530
Kate Stoneb9c1b512016-09-06 20:57:50 +00005531void ClangASTContext::ForEachEnumerator(
5532 lldb::opaque_compiler_type_t type,
5533 std::function<bool(const CompilerType &integer_type,
5534 const ConstString &name,
5535 const llvm::APSInt &value)> const &callback) {
5536 const clang::EnumType *enum_type =
5537 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5538 if (enum_type) {
5539 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5540 if (enum_decl) {
5541 CompilerType integer_type(this,
5542 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005543
Kate Stoneb9c1b512016-09-06 20:57:50 +00005544 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5545 for (enum_pos = enum_decl->enumerator_begin(),
5546 enum_end_pos = enum_decl->enumerator_end();
5547 enum_pos != enum_end_pos; ++enum_pos) {
5548 ConstString name(enum_pos->getNameAsString().c_str());
5549 if (!callback(integer_type, name, enum_pos->getInitVal()))
5550 break;
5551 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005552 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005553 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005554}
5555
Greg Claytond8d4a572015-08-11 21:38:15 +00005556#pragma mark Aggregate Types
5557
Kate Stoneb9c1b512016-09-06 20:57:50 +00005558uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5559 if (!type)
5560 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005561
Kate Stoneb9c1b512016-09-06 20:57:50 +00005562 uint32_t count = 0;
5563 clang::QualType qual_type(GetCanonicalQualType(type));
5564 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5565 switch (type_class) {
5566 case clang::Type::Record:
5567 if (GetCompleteType(type)) {
5568 const clang::RecordType *record_type =
5569 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5570 if (record_type) {
5571 clang::RecordDecl *record_decl = record_type->getDecl();
5572 if (record_decl) {
5573 uint32_t field_idx = 0;
5574 clang::RecordDecl::field_iterator field, field_end;
5575 for (field = record_decl->field_begin(),
5576 field_end = record_decl->field_end();
5577 field != field_end; ++field)
5578 ++field_idx;
5579 count = field_idx;
5580 }
5581 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005582 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005583 break;
5584
5585 case clang::Type::Typedef:
5586 count =
5587 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5588 ->getDecl()
5589 ->getUnderlyingType())
5590 .GetNumFields();
5591 break;
5592
5593 case clang::Type::Auto:
5594 count =
5595 CompilerType(getASTContext(),
5596 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5597 .GetNumFields();
5598 break;
5599
5600 case clang::Type::Elaborated:
5601 count = CompilerType(
5602 getASTContext(),
5603 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5604 .GetNumFields();
5605 break;
5606
5607 case clang::Type::Paren:
5608 count = CompilerType(getASTContext(),
5609 llvm::cast<clang::ParenType>(qual_type)->desugar())
5610 .GetNumFields();
5611 break;
5612
Sean Callananf9c622a2016-09-30 18:44:43 +00005613 case clang::Type::ObjCObjectPointer: {
5614 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005615 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005616 const clang::ObjCInterfaceType *objc_interface_type =
5617 objc_class_type->getInterfaceType();
5618 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005619 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5620 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005621 clang::ObjCInterfaceDecl *class_interface_decl =
5622 objc_interface_type->getDecl();
5623 if (class_interface_decl) {
5624 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005625 }
5626 }
5627 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005628 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005629
5630 case clang::Type::ObjCObject:
5631 case clang::Type::ObjCInterface:
5632 if (GetCompleteType(type)) {
5633 const clang::ObjCObjectType *objc_class_type =
5634 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5635 if (objc_class_type) {
5636 clang::ObjCInterfaceDecl *class_interface_decl =
5637 objc_class_type->getInterface();
5638
5639 if (class_interface_decl)
5640 count = class_interface_decl->ivar_size();
5641 }
5642 }
5643 break;
5644
5645 default:
5646 break;
5647 }
5648 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005649}
5650
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005651static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005652GetObjCFieldAtIndex(clang::ASTContext *ast,
5653 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5654 std::string &name, uint64_t *bit_offset_ptr,
5655 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5656 if (class_interface_decl) {
5657 if (idx < (class_interface_decl->ivar_size())) {
5658 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5659 ivar_end = class_interface_decl->ivar_end();
5660 uint32_t ivar_idx = 0;
5661
5662 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5663 ++ivar_pos, ++ivar_idx) {
5664 if (ivar_idx == idx) {
5665 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5666
5667 clang::QualType ivar_qual_type(ivar_decl->getType());
5668
5669 name.assign(ivar_decl->getNameAsString());
5670
5671 if (bit_offset_ptr) {
5672 const clang::ASTRecordLayout &interface_layout =
5673 ast->getASTObjCInterfaceLayout(class_interface_decl);
5674 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5675 }
5676
5677 const bool is_bitfield = ivar_pos->isBitField();
5678
5679 if (bitfield_bit_size_ptr) {
5680 *bitfield_bit_size_ptr = 0;
5681
5682 if (is_bitfield && ast) {
5683 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5684 llvm::APSInt bitfield_apsint;
5685 if (bitfield_bit_size_expr &&
5686 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5687 *ast)) {
5688 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5689 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005690 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005691 }
5692 if (is_bitfield_ptr)
5693 *is_bitfield_ptr = is_bitfield;
5694
5695 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005696 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005697 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005698 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005699 }
5700 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005701}
5702
Kate Stoneb9c1b512016-09-06 20:57:50 +00005703CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5704 size_t idx, std::string &name,
5705 uint64_t *bit_offset_ptr,
5706 uint32_t *bitfield_bit_size_ptr,
5707 bool *is_bitfield_ptr) {
5708 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005709 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005710
5711 clang::QualType qual_type(GetCanonicalQualType(type));
5712 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5713 switch (type_class) {
5714 case clang::Type::Record:
5715 if (GetCompleteType(type)) {
5716 const clang::RecordType *record_type =
5717 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5718 const clang::RecordDecl *record_decl = record_type->getDecl();
5719 uint32_t field_idx = 0;
5720 clang::RecordDecl::field_iterator field, field_end;
5721 for (field = record_decl->field_begin(),
5722 field_end = record_decl->field_end();
5723 field != field_end; ++field, ++field_idx) {
5724 if (idx == field_idx) {
5725 // Print the member type if requested
5726 // Print the member name and equal sign
5727 name.assign(field->getNameAsString());
5728
5729 // Figure out the type byte size (field_type_info.first) and
5730 // alignment (field_type_info.second) from the AST context.
5731 if (bit_offset_ptr) {
5732 const clang::ASTRecordLayout &record_layout =
5733 getASTContext()->getASTRecordLayout(record_decl);
5734 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5735 }
5736
5737 const bool is_bitfield = field->isBitField();
5738
5739 if (bitfield_bit_size_ptr) {
5740 *bitfield_bit_size_ptr = 0;
5741
5742 if (is_bitfield) {
5743 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5744 llvm::APSInt bitfield_apsint;
5745 if (bitfield_bit_size_expr &&
5746 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5747 *getASTContext())) {
5748 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5749 }
5750 }
5751 }
5752 if (is_bitfield_ptr)
5753 *is_bitfield_ptr = is_bitfield;
5754
5755 return CompilerType(getASTContext(), field->getType());
5756 }
5757 }
5758 }
5759 break;
5760
Sean Callananf9c622a2016-09-30 18:44:43 +00005761 case clang::Type::ObjCObjectPointer: {
5762 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005763 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005764 const clang::ObjCInterfaceType *objc_interface_type =
5765 objc_class_type->getInterfaceType();
5766 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005767 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5768 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005769 clang::ObjCInterfaceDecl *class_interface_decl =
5770 objc_interface_type->getDecl();
5771 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005772 return CompilerType(
5773 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5774 idx, name, bit_offset_ptr,
5775 bitfield_bit_size_ptr, is_bitfield_ptr));
5776 }
5777 }
5778 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005779 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005780
5781 case clang::Type::ObjCObject:
5782 case clang::Type::ObjCInterface:
5783 if (GetCompleteType(type)) {
5784 const clang::ObjCObjectType *objc_class_type =
5785 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5786 assert(objc_class_type);
5787 if (objc_class_type) {
5788 clang::ObjCInterfaceDecl *class_interface_decl =
5789 objc_class_type->getInterface();
5790 return CompilerType(
5791 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5792 idx, name, bit_offset_ptr,
5793 bitfield_bit_size_ptr, is_bitfield_ptr));
5794 }
5795 }
5796 break;
5797
5798 case clang::Type::Typedef:
5799 return CompilerType(getASTContext(),
5800 llvm::cast<clang::TypedefType>(qual_type)
5801 ->getDecl()
5802 ->getUnderlyingType())
5803 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5804 is_bitfield_ptr);
5805
5806 case clang::Type::Auto:
5807 return CompilerType(
5808 getASTContext(),
5809 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5810 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5811 is_bitfield_ptr);
5812
5813 case clang::Type::Elaborated:
5814 return CompilerType(
5815 getASTContext(),
5816 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5817 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5818 is_bitfield_ptr);
5819
5820 case clang::Type::Paren:
5821 return CompilerType(getASTContext(),
5822 llvm::cast<clang::ParenType>(qual_type)->desugar())
5823 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5824 is_bitfield_ptr);
5825
5826 default:
5827 break;
5828 }
5829 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005830}
5831
Greg Clayton99558cc42015-08-24 23:46:31 +00005832uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005833ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
5834 uint32_t count = 0;
5835 clang::QualType qual_type(GetCanonicalQualType(type));
5836 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5837 switch (type_class) {
5838 case clang::Type::Record:
5839 if (GetCompleteType(type)) {
5840 const clang::CXXRecordDecl *cxx_record_decl =
5841 qual_type->getAsCXXRecordDecl();
5842 if (cxx_record_decl)
5843 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00005844 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005845 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00005846
Kate Stoneb9c1b512016-09-06 20:57:50 +00005847 case clang::Type::ObjCObjectPointer:
5848 count = GetPointeeType(type).GetNumDirectBaseClasses();
5849 break;
5850
5851 case clang::Type::ObjCObject:
5852 if (GetCompleteType(type)) {
5853 const clang::ObjCObjectType *objc_class_type =
5854 qual_type->getAsObjCQualifiedInterfaceType();
5855 if (objc_class_type) {
5856 clang::ObjCInterfaceDecl *class_interface_decl =
5857 objc_class_type->getInterface();
5858
5859 if (class_interface_decl && class_interface_decl->getSuperClass())
5860 count = 1;
5861 }
5862 }
5863 break;
5864 case clang::Type::ObjCInterface:
5865 if (GetCompleteType(type)) {
5866 const clang::ObjCInterfaceType *objc_interface_type =
5867 qual_type->getAs<clang::ObjCInterfaceType>();
5868 if (objc_interface_type) {
5869 clang::ObjCInterfaceDecl *class_interface_decl =
5870 objc_interface_type->getInterface();
5871
5872 if (class_interface_decl && class_interface_decl->getSuperClass())
5873 count = 1;
5874 }
5875 }
5876 break;
5877
5878 case clang::Type::Typedef:
5879 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5880 ->getDecl()
5881 ->getUnderlyingType()
5882 .getAsOpaquePtr());
5883 break;
5884
5885 case clang::Type::Auto:
5886 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5887 ->getDeducedType()
5888 .getAsOpaquePtr());
5889 break;
5890
5891 case clang::Type::Elaborated:
5892 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5893 ->getNamedType()
5894 .getAsOpaquePtr());
5895 break;
5896
5897 case clang::Type::Paren:
5898 return GetNumDirectBaseClasses(
5899 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5900
5901 default:
5902 break;
5903 }
5904 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00005905}
5906
5907uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005908ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
5909 uint32_t count = 0;
5910 clang::QualType qual_type(GetCanonicalQualType(type));
5911 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5912 switch (type_class) {
5913 case clang::Type::Record:
5914 if (GetCompleteType(type)) {
5915 const clang::CXXRecordDecl *cxx_record_decl =
5916 qual_type->getAsCXXRecordDecl();
5917 if (cxx_record_decl)
5918 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00005919 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005920 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00005921
Kate Stoneb9c1b512016-09-06 20:57:50 +00005922 case clang::Type::Typedef:
5923 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5924 ->getDecl()
5925 ->getUnderlyingType()
5926 .getAsOpaquePtr());
5927 break;
5928
5929 case clang::Type::Auto:
5930 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5931 ->getDeducedType()
5932 .getAsOpaquePtr());
5933 break;
5934
5935 case clang::Type::Elaborated:
5936 count =
5937 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5938 ->getNamedType()
5939 .getAsOpaquePtr());
5940 break;
5941
5942 case clang::Type::Paren:
5943 count = GetNumVirtualBaseClasses(
5944 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5945 break;
5946
5947 default:
5948 break;
5949 }
5950 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00005951}
5952
Kate Stoneb9c1b512016-09-06 20:57:50 +00005953CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
5954 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
5955 clang::QualType qual_type(GetCanonicalQualType(type));
5956 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5957 switch (type_class) {
5958 case clang::Type::Record:
5959 if (GetCompleteType(type)) {
5960 const clang::CXXRecordDecl *cxx_record_decl =
5961 qual_type->getAsCXXRecordDecl();
5962 if (cxx_record_decl) {
5963 uint32_t curr_idx = 0;
5964 clang::CXXRecordDecl::base_class_const_iterator base_class,
5965 base_class_end;
5966 for (base_class = cxx_record_decl->bases_begin(),
5967 base_class_end = cxx_record_decl->bases_end();
5968 base_class != base_class_end; ++base_class, ++curr_idx) {
5969 if (curr_idx == idx) {
5970 if (bit_offset_ptr) {
5971 const clang::ASTRecordLayout &record_layout =
5972 getASTContext()->getASTRecordLayout(cxx_record_decl);
5973 const clang::CXXRecordDecl *base_class_decl =
5974 llvm::cast<clang::CXXRecordDecl>(
5975 base_class->getType()
5976 ->getAs<clang::RecordType>()
5977 ->getDecl());
5978 if (base_class->isVirtual())
5979 *bit_offset_ptr =
5980 record_layout.getVBaseClassOffset(base_class_decl)
5981 .getQuantity() *
5982 8;
5983 else
5984 *bit_offset_ptr =
5985 record_layout.getBaseClassOffset(base_class_decl)
5986 .getQuantity() *
5987 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00005988 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005989 return CompilerType(this, base_class->getType().getAsOpaquePtr());
5990 }
5991 }
5992 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005993 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005994 break;
5995
5996 case clang::Type::ObjCObjectPointer:
5997 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
5998
5999 case clang::Type::ObjCObject:
6000 if (idx == 0 && GetCompleteType(type)) {
6001 const clang::ObjCObjectType *objc_class_type =
6002 qual_type->getAsObjCQualifiedInterfaceType();
6003 if (objc_class_type) {
6004 clang::ObjCInterfaceDecl *class_interface_decl =
6005 objc_class_type->getInterface();
6006
6007 if (class_interface_decl) {
6008 clang::ObjCInterfaceDecl *superclass_interface_decl =
6009 class_interface_decl->getSuperClass();
6010 if (superclass_interface_decl) {
6011 if (bit_offset_ptr)
6012 *bit_offset_ptr = 0;
6013 return CompilerType(getASTContext(),
6014 getASTContext()->getObjCInterfaceType(
6015 superclass_interface_decl));
6016 }
6017 }
6018 }
6019 }
6020 break;
6021 case clang::Type::ObjCInterface:
6022 if (idx == 0 && GetCompleteType(type)) {
6023 const clang::ObjCObjectType *objc_interface_type =
6024 qual_type->getAs<clang::ObjCInterfaceType>();
6025 if (objc_interface_type) {
6026 clang::ObjCInterfaceDecl *class_interface_decl =
6027 objc_interface_type->getInterface();
6028
6029 if (class_interface_decl) {
6030 clang::ObjCInterfaceDecl *superclass_interface_decl =
6031 class_interface_decl->getSuperClass();
6032 if (superclass_interface_decl) {
6033 if (bit_offset_ptr)
6034 *bit_offset_ptr = 0;
6035 return CompilerType(getASTContext(),
6036 getASTContext()->getObjCInterfaceType(
6037 superclass_interface_decl));
6038 }
6039 }
6040 }
6041 }
6042 break;
6043
6044 case clang::Type::Typedef:
6045 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6046 ->getDecl()
6047 ->getUnderlyingType()
6048 .getAsOpaquePtr(),
6049 idx, bit_offset_ptr);
6050
6051 case clang::Type::Auto:
6052 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6053 ->getDeducedType()
6054 .getAsOpaquePtr(),
6055 idx, bit_offset_ptr);
6056
6057 case clang::Type::Elaborated:
6058 return GetDirectBaseClassAtIndex(
6059 llvm::cast<clang::ElaboratedType>(qual_type)
6060 ->getNamedType()
6061 .getAsOpaquePtr(),
6062 idx, bit_offset_ptr);
6063
6064 case clang::Type::Paren:
6065 return GetDirectBaseClassAtIndex(
6066 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6067 idx, bit_offset_ptr);
6068
6069 default:
6070 break;
6071 }
6072 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006073}
6074
Kate Stoneb9c1b512016-09-06 20:57:50 +00006075CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6076 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6077 clang::QualType qual_type(GetCanonicalQualType(type));
6078 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6079 switch (type_class) {
6080 case clang::Type::Record:
6081 if (GetCompleteType(type)) {
6082 const clang::CXXRecordDecl *cxx_record_decl =
6083 qual_type->getAsCXXRecordDecl();
6084 if (cxx_record_decl) {
6085 uint32_t curr_idx = 0;
6086 clang::CXXRecordDecl::base_class_const_iterator base_class,
6087 base_class_end;
6088 for (base_class = cxx_record_decl->vbases_begin(),
6089 base_class_end = cxx_record_decl->vbases_end();
6090 base_class != base_class_end; ++base_class, ++curr_idx) {
6091 if (curr_idx == idx) {
6092 if (bit_offset_ptr) {
6093 const clang::ASTRecordLayout &record_layout =
6094 getASTContext()->getASTRecordLayout(cxx_record_decl);
6095 const clang::CXXRecordDecl *base_class_decl =
6096 llvm::cast<clang::CXXRecordDecl>(
6097 base_class->getType()
6098 ->getAs<clang::RecordType>()
6099 ->getDecl());
6100 *bit_offset_ptr =
6101 record_layout.getVBaseClassOffset(base_class_decl)
6102 .getQuantity() *
6103 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006104 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006105 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6106 }
6107 }
6108 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006109 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006110 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006111
Kate Stoneb9c1b512016-09-06 20:57:50 +00006112 case clang::Type::Typedef:
6113 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6114 ->getDecl()
6115 ->getUnderlyingType()
6116 .getAsOpaquePtr(),
6117 idx, bit_offset_ptr);
6118
6119 case clang::Type::Auto:
6120 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6121 ->getDeducedType()
6122 .getAsOpaquePtr(),
6123 idx, bit_offset_ptr);
6124
6125 case clang::Type::Elaborated:
6126 return GetVirtualBaseClassAtIndex(
6127 llvm::cast<clang::ElaboratedType>(qual_type)
6128 ->getNamedType()
6129 .getAsOpaquePtr(),
6130 idx, bit_offset_ptr);
6131
6132 case clang::Type::Paren:
6133 return GetVirtualBaseClassAtIndex(
6134 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6135 idx, bit_offset_ptr);
6136
6137 default:
6138 break;
6139 }
6140 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006141}
6142
Greg Claytond8d4a572015-08-11 21:38:15 +00006143// If a pointer to a pointee type (the clang_type arg) says that it has no
6144// children, then we either need to trust it, or override it and return a
6145// different result. For example, an "int *" has one child that is an integer,
6146// but a function pointer doesn't have any children. Likewise if a Record type
6147// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006148uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6149 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006150 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006151
6152 clang::QualType qual_type(type.getCanonicalType());
6153 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6154 switch (type_class) {
6155 case clang::Type::Builtin:
6156 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6157 case clang::BuiltinType::UnknownAny:
6158 case clang::BuiltinType::Void:
6159 case clang::BuiltinType::NullPtr:
6160 case clang::BuiltinType::OCLEvent:
6161 case clang::BuiltinType::OCLImage1dRO:
6162 case clang::BuiltinType::OCLImage1dWO:
6163 case clang::BuiltinType::OCLImage1dRW:
6164 case clang::BuiltinType::OCLImage1dArrayRO:
6165 case clang::BuiltinType::OCLImage1dArrayWO:
6166 case clang::BuiltinType::OCLImage1dArrayRW:
6167 case clang::BuiltinType::OCLImage1dBufferRO:
6168 case clang::BuiltinType::OCLImage1dBufferWO:
6169 case clang::BuiltinType::OCLImage1dBufferRW:
6170 case clang::BuiltinType::OCLImage2dRO:
6171 case clang::BuiltinType::OCLImage2dWO:
6172 case clang::BuiltinType::OCLImage2dRW:
6173 case clang::BuiltinType::OCLImage2dArrayRO:
6174 case clang::BuiltinType::OCLImage2dArrayWO:
6175 case clang::BuiltinType::OCLImage2dArrayRW:
6176 case clang::BuiltinType::OCLImage3dRO:
6177 case clang::BuiltinType::OCLImage3dWO:
6178 case clang::BuiltinType::OCLImage3dRW:
6179 case clang::BuiltinType::OCLSampler:
6180 return 0;
6181 case clang::BuiltinType::Bool:
6182 case clang::BuiltinType::Char_U:
6183 case clang::BuiltinType::UChar:
6184 case clang::BuiltinType::WChar_U:
6185 case clang::BuiltinType::Char16:
6186 case clang::BuiltinType::Char32:
6187 case clang::BuiltinType::UShort:
6188 case clang::BuiltinType::UInt:
6189 case clang::BuiltinType::ULong:
6190 case clang::BuiltinType::ULongLong:
6191 case clang::BuiltinType::UInt128:
6192 case clang::BuiltinType::Char_S:
6193 case clang::BuiltinType::SChar:
6194 case clang::BuiltinType::WChar_S:
6195 case clang::BuiltinType::Short:
6196 case clang::BuiltinType::Int:
6197 case clang::BuiltinType::Long:
6198 case clang::BuiltinType::LongLong:
6199 case clang::BuiltinType::Int128:
6200 case clang::BuiltinType::Float:
6201 case clang::BuiltinType::Double:
6202 case clang::BuiltinType::LongDouble:
6203 case clang::BuiltinType::Dependent:
6204 case clang::BuiltinType::Overload:
6205 case clang::BuiltinType::ObjCId:
6206 case clang::BuiltinType::ObjCClass:
6207 case clang::BuiltinType::ObjCSel:
6208 case clang::BuiltinType::BoundMember:
6209 case clang::BuiltinType::Half:
6210 case clang::BuiltinType::ARCUnbridgedCast:
6211 case clang::BuiltinType::PseudoObject:
6212 case clang::BuiltinType::BuiltinFn:
6213 case clang::BuiltinType::OMPArraySection:
6214 return 1;
6215 default:
6216 return 0;
6217 }
6218 break;
6219
6220 case clang::Type::Complex:
6221 return 1;
6222 case clang::Type::Pointer:
6223 return 1;
6224 case clang::Type::BlockPointer:
6225 return 0; // If block pointers don't have debug info, then no children for
6226 // them
6227 case clang::Type::LValueReference:
6228 return 1;
6229 case clang::Type::RValueReference:
6230 return 1;
6231 case clang::Type::MemberPointer:
6232 return 0;
6233 case clang::Type::ConstantArray:
6234 return 0;
6235 case clang::Type::IncompleteArray:
6236 return 0;
6237 case clang::Type::VariableArray:
6238 return 0;
6239 case clang::Type::DependentSizedArray:
6240 return 0;
6241 case clang::Type::DependentSizedExtVector:
6242 return 0;
6243 case clang::Type::Vector:
6244 return 0;
6245 case clang::Type::ExtVector:
6246 return 0;
6247 case clang::Type::FunctionProto:
6248 return 0; // When we function pointers, they have no children...
6249 case clang::Type::FunctionNoProto:
6250 return 0; // When we function pointers, they have no children...
6251 case clang::Type::UnresolvedUsing:
6252 return 0;
6253 case clang::Type::Paren:
6254 return GetNumPointeeChildren(
6255 llvm::cast<clang::ParenType>(qual_type)->desugar());
6256 case clang::Type::Typedef:
6257 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6258 ->getDecl()
6259 ->getUnderlyingType());
6260 case clang::Type::Auto:
6261 return GetNumPointeeChildren(
6262 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6263 case clang::Type::Elaborated:
6264 return GetNumPointeeChildren(
6265 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6266 case clang::Type::TypeOfExpr:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006267 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6268 ->getUnderlyingExpr()
6269 ->getType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006270 case clang::Type::TypeOf:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006271 return GetNumPointeeChildren(
6272 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006273 case clang::Type::Decltype:
Jonas Devlieghere65d2d5b2018-02-20 10:15:08 +00006274 return GetNumPointeeChildren(
6275 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006276 case clang::Type::Record:
6277 return 0;
6278 case clang::Type::Enum:
6279 return 1;
6280 case clang::Type::TemplateTypeParm:
6281 return 1;
6282 case clang::Type::SubstTemplateTypeParm:
6283 return 1;
6284 case clang::Type::TemplateSpecialization:
6285 return 1;
6286 case clang::Type::InjectedClassName:
6287 return 0;
6288 case clang::Type::DependentName:
6289 return 1;
6290 case clang::Type::DependentTemplateSpecialization:
6291 return 1;
6292 case clang::Type::ObjCObject:
6293 return 0;
6294 case clang::Type::ObjCInterface:
6295 return 0;
6296 case clang::Type::ObjCObjectPointer:
6297 return 1;
6298 default:
6299 break;
6300 }
6301 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006302}
6303
Kate Stoneb9c1b512016-09-06 20:57:50 +00006304CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6305 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6306 bool transparent_pointers, bool omit_empty_base_classes,
6307 bool ignore_array_bounds, std::string &child_name,
6308 uint32_t &child_byte_size, int32_t &child_byte_offset,
6309 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6310 bool &child_is_base_class, bool &child_is_deref_of_parent,
6311 ValueObject *valobj, uint64_t &language_flags) {
6312 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006313 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006314
Kate Stoneb9c1b512016-09-06 20:57:50 +00006315 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6316 const clang::Type::TypeClass parent_type_class =
6317 parent_qual_type->getTypeClass();
6318 child_bitfield_bit_size = 0;
6319 child_bitfield_bit_offset = 0;
6320 child_is_base_class = false;
6321 language_flags = 0;
6322
6323 const bool idx_is_valid = idx < GetNumChildren(type, omit_empty_base_classes);
6324 uint32_t bit_offset;
6325 switch (parent_type_class) {
6326 case clang::Type::Builtin:
6327 if (idx_is_valid) {
6328 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6329 case clang::BuiltinType::ObjCId:
6330 case clang::BuiltinType::ObjCClass:
6331 child_name = "isa";
6332 child_byte_size =
6333 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6334 CHAR_BIT;
6335 return CompilerType(getASTContext(),
6336 getASTContext()->ObjCBuiltinClassTy);
6337
6338 default:
6339 break;
6340 }
6341 }
6342 break;
6343
6344 case clang::Type::Record:
6345 if (idx_is_valid && GetCompleteType(type)) {
6346 const clang::RecordType *record_type =
6347 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6348 const clang::RecordDecl *record_decl = record_type->getDecl();
6349 assert(record_decl);
6350 const clang::ASTRecordLayout &record_layout =
6351 getASTContext()->getASTRecordLayout(record_decl);
6352 uint32_t child_idx = 0;
6353
6354 const clang::CXXRecordDecl *cxx_record_decl =
6355 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6356 if (cxx_record_decl) {
6357 // We might have base classes to print out first
6358 clang::CXXRecordDecl::base_class_const_iterator base_class,
6359 base_class_end;
6360 for (base_class = cxx_record_decl->bases_begin(),
6361 base_class_end = cxx_record_decl->bases_end();
6362 base_class != base_class_end; ++base_class) {
6363 const clang::CXXRecordDecl *base_class_decl = nullptr;
6364
6365 // Skip empty base classes
6366 if (omit_empty_base_classes) {
6367 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6368 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6369 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
6370 continue;
6371 }
6372
6373 if (idx == child_idx) {
6374 if (base_class_decl == nullptr)
6375 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6376 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6377
6378 if (base_class->isVirtual()) {
6379 bool handled = false;
6380 if (valobj) {
Zachary Turner97206d52017-05-12 04:51:55 +00006381 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006382 AddressType addr_type = eAddressTypeInvalid;
6383 lldb::addr_t vtable_ptr_addr =
6384 valobj->GetCPPVTableAddress(addr_type);
6385
6386 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS &&
6387 addr_type == eAddressTypeLoad) {
6388
6389 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
6390 Process *process = exe_ctx.GetProcessPtr();
6391 if (process) {
6392 clang::VTableContextBase *vtable_ctx =
6393 getASTContext()->getVTableContext();
6394 if (vtable_ctx) {
6395 if (vtable_ctx->isMicrosoft()) {
6396 clang::MicrosoftVTableContext *msoft_vtable_ctx =
6397 static_cast<clang::MicrosoftVTableContext *>(
6398 vtable_ctx);
6399
6400 if (vtable_ptr_addr) {
6401 const lldb::addr_t vbtable_ptr_addr =
6402 vtable_ptr_addr +
6403 record_layout.getVBPtrOffset().getQuantity();
6404
6405 const lldb::addr_t vbtable_ptr =
6406 process->ReadPointerFromMemory(vbtable_ptr_addr,
6407 err);
6408 if (vbtable_ptr != LLDB_INVALID_ADDRESS) {
6409 // Get the index into the virtual base table. The
6410 // index is the index in uint32_t from vbtable_ptr
6411 const unsigned vbtable_index =
6412 msoft_vtable_ctx->getVBTableIndex(
6413 cxx_record_decl, base_class_decl);
6414 const lldb::addr_t base_offset_addr =
6415 vbtable_ptr + vbtable_index * 4;
6416 const uint32_t base_offset =
6417 process->ReadUnsignedIntegerFromMemory(
6418 base_offset_addr, 4, UINT32_MAX, err);
6419 if (base_offset != UINT32_MAX) {
6420 handled = true;
6421 bit_offset = base_offset * 8;
6422 }
6423 }
6424 }
6425 } else {
6426 clang::ItaniumVTableContext *itanium_vtable_ctx =
6427 static_cast<clang::ItaniumVTableContext *>(
6428 vtable_ctx);
6429 if (vtable_ptr_addr) {
6430 const lldb::addr_t vtable_ptr =
6431 process->ReadPointerFromMemory(vtable_ptr_addr,
6432 err);
6433 if (vtable_ptr != LLDB_INVALID_ADDRESS) {
6434 clang::CharUnits base_offset_offset =
6435 itanium_vtable_ctx->getVirtualBaseOffsetOffset(
6436 cxx_record_decl, base_class_decl);
6437 const lldb::addr_t base_offset_addr =
6438 vtable_ptr + base_offset_offset.getQuantity();
6439 const uint32_t base_offset_size =
6440 process->GetAddressByteSize();
6441 const uint64_t base_offset =
6442 process->ReadUnsignedIntegerFromMemory(
6443 base_offset_addr, base_offset_size,
6444 UINT32_MAX, err);
6445 if (base_offset < UINT32_MAX) {
6446 handled = true;
6447 bit_offset = base_offset * 8;
6448 }
6449 }
6450 }
6451 }
6452 }
6453 }
6454 }
6455 }
6456 if (!handled)
6457 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6458 .getQuantity() *
6459 8;
6460 } else
6461 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6462 .getQuantity() *
6463 8;
6464
6465 // Base classes should be a multiple of 8 bits in size
6466 child_byte_offset = bit_offset / 8;
6467 CompilerType base_class_clang_type(getASTContext(),
6468 base_class->getType());
6469 child_name = base_class_clang_type.GetTypeName().AsCString("");
6470 uint64_t base_class_clang_type_bit_size =
6471 base_class_clang_type.GetBitSize(
6472 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6473
6474 // Base classes bit sizes should be a multiple of 8 bits in size
6475 assert(base_class_clang_type_bit_size % 8 == 0);
6476 child_byte_size = base_class_clang_type_bit_size / 8;
6477 child_is_base_class = true;
6478 return base_class_clang_type;
6479 }
6480 // We don't increment the child index in the for loop since we might
6481 // be skipping empty base classes
6482 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006483 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006484 }
6485 // Make sure index is in range...
6486 uint32_t field_idx = 0;
6487 clang::RecordDecl::field_iterator field, field_end;
6488 for (field = record_decl->field_begin(),
6489 field_end = record_decl->field_end();
6490 field != field_end; ++field, ++field_idx, ++child_idx) {
6491 if (idx == child_idx) {
6492 // Print the member type if requested
6493 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006494 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006495
6496 // Figure out the type byte size (field_type_info.first) and
6497 // alignment (field_type_info.second) from the AST context.
6498 CompilerType field_clang_type(getASTContext(), field->getType());
6499 assert(field_idx < record_layout.getFieldCount());
6500 child_byte_size = field_clang_type.GetByteSize(
6501 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6502 const uint32_t child_bit_size = child_byte_size * 8;
6503
6504 // Figure out the field offset within the current struct/union/class
6505 // type
6506 bit_offset = record_layout.getFieldOffset(field_idx);
6507 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6508 child_bitfield_bit_size)) {
6509 child_bitfield_bit_offset = bit_offset % child_bit_size;
6510 const uint32_t child_bit_offset =
6511 bit_offset - child_bitfield_bit_offset;
6512 child_byte_offset = child_bit_offset / 8;
6513 } else {
6514 child_byte_offset = bit_offset / 8;
6515 }
6516
6517 return field_clang_type;
6518 }
6519 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006520 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006521 break;
6522
6523 case clang::Type::ObjCObject:
6524 case clang::Type::ObjCInterface:
6525 if (idx_is_valid && GetCompleteType(type)) {
6526 const clang::ObjCObjectType *objc_class_type =
6527 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6528 assert(objc_class_type);
6529 if (objc_class_type) {
6530 uint32_t child_idx = 0;
6531 clang::ObjCInterfaceDecl *class_interface_decl =
6532 objc_class_type->getInterface();
6533
6534 if (class_interface_decl) {
6535
6536 const clang::ASTRecordLayout &interface_layout =
6537 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6538 clang::ObjCInterfaceDecl *superclass_interface_decl =
6539 class_interface_decl->getSuperClass();
6540 if (superclass_interface_decl) {
6541 if (omit_empty_base_classes) {
6542 CompilerType base_class_clang_type(
6543 getASTContext(), getASTContext()->getObjCInterfaceType(
6544 superclass_interface_decl));
6545 if (base_class_clang_type.GetNumChildren(
6546 omit_empty_base_classes) > 0) {
6547 if (idx == 0) {
6548 clang::QualType ivar_qual_type(
6549 getASTContext()->getObjCInterfaceType(
6550 superclass_interface_decl));
6551
6552 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006553 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006554
6555 clang::TypeInfo ivar_type_info =
6556 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6557
6558 child_byte_size = ivar_type_info.Width / 8;
6559 child_byte_offset = 0;
6560 child_is_base_class = true;
6561
6562 return CompilerType(getASTContext(), ivar_qual_type);
6563 }
6564
6565 ++child_idx;
6566 }
6567 } else
6568 ++child_idx;
6569 }
6570
6571 const uint32_t superclass_idx = child_idx;
6572
6573 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6574 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6575 ivar_end = class_interface_decl->ivar_end();
6576
6577 for (ivar_pos = class_interface_decl->ivar_begin();
6578 ivar_pos != ivar_end; ++ivar_pos) {
6579 if (child_idx == idx) {
6580 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6581
6582 clang::QualType ivar_qual_type(ivar_decl->getType());
6583
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006584 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006585
6586 clang::TypeInfo ivar_type_info =
6587 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6588
6589 child_byte_size = ivar_type_info.Width / 8;
6590
6591 // Figure out the field offset within the current
6592 // struct/union/class type
6593 // For ObjC objects, we can't trust the bit offset we get from
6594 // the Clang AST, since
6595 // that doesn't account for the space taken up by unbacked
6596 // properties, or from
6597 // the changing size of base classes that are newer than this
6598 // class.
6599 // So if we have a process around that we can ask about this
6600 // object, do so.
6601 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6602 Process *process = nullptr;
6603 if (exe_ctx)
6604 process = exe_ctx->GetProcessPtr();
6605 if (process) {
6606 ObjCLanguageRuntime *objc_runtime =
6607 process->GetObjCLanguageRuntime();
6608 if (objc_runtime != nullptr) {
6609 CompilerType parent_ast_type(getASTContext(),
6610 parent_qual_type);
6611 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6612 parent_ast_type, ivar_decl->getNameAsString().c_str());
6613 }
6614 }
6615
6616 // Setting this to UINT32_MAX to make sure we don't compute it
6617 // twice...
6618 bit_offset = UINT32_MAX;
6619
6620 if (child_byte_offset ==
6621 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6622 bit_offset = interface_layout.getFieldOffset(child_idx -
6623 superclass_idx);
6624 child_byte_offset = bit_offset / 8;
6625 }
6626
6627 // Note, the ObjC Ivar Byte offset is just that, it doesn't
6628 // account for the bit offset
6629 // of a bitfield within its containing object. So regardless of
6630 // where we get the byte
6631 // offset from, we still need to get the bit offset for
6632 // bitfields from the layout.
6633
6634 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6635 child_bitfield_bit_size)) {
6636 if (bit_offset == UINT32_MAX)
6637 bit_offset = interface_layout.getFieldOffset(
6638 child_idx - superclass_idx);
6639
6640 child_bitfield_bit_offset = bit_offset % 8;
6641 }
6642 return CompilerType(getASTContext(), ivar_qual_type);
6643 }
6644 ++child_idx;
6645 }
6646 }
6647 }
6648 }
6649 }
6650 break;
6651
6652 case clang::Type::ObjCObjectPointer:
6653 if (idx_is_valid) {
6654 CompilerType pointee_clang_type(GetPointeeType(type));
6655
6656 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6657 child_is_deref_of_parent = false;
6658 bool tmp_child_is_deref_of_parent = false;
6659 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6660 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6661 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6662 child_bitfield_bit_size, child_bitfield_bit_offset,
6663 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6664 language_flags);
6665 } else {
6666 child_is_deref_of_parent = true;
6667 const char *parent_name =
6668 valobj ? valobj->GetName().GetCString() : NULL;
6669 if (parent_name) {
6670 child_name.assign(1, '*');
6671 child_name += parent_name;
6672 }
6673
6674 // We have a pointer to an simple type
6675 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
6676 child_byte_size = pointee_clang_type.GetByteSize(
6677 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6678 child_byte_offset = 0;
6679 return pointee_clang_type;
6680 }
6681 }
6682 }
6683 break;
6684
6685 case clang::Type::Vector:
6686 case clang::Type::ExtVector:
6687 if (idx_is_valid) {
6688 const clang::VectorType *array =
6689 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6690 if (array) {
6691 CompilerType element_type(getASTContext(), array->getElementType());
6692 if (element_type.GetCompleteType()) {
6693 char element_name[64];
6694 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6695 static_cast<uint64_t>(idx));
6696 child_name.assign(element_name);
6697 child_byte_size = element_type.GetByteSize(
6698 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6699 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6700 return element_type;
6701 }
6702 }
6703 }
6704 break;
6705
6706 case clang::Type::ConstantArray:
6707 case clang::Type::IncompleteArray:
6708 if (ignore_array_bounds || idx_is_valid) {
6709 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6710 if (array) {
6711 CompilerType element_type(getASTContext(), array->getElementType());
6712 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006713 child_name = llvm::formatv("[{0}]", idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006714 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
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006723 case clang::Type::Pointer: {
6724 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006725
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006726 // Don't dereference "void *" pointers
6727 if (pointee_clang_type.IsVoidType())
6728 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006729
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006730 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6731 child_is_deref_of_parent = false;
6732 bool tmp_child_is_deref_of_parent = false;
6733 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6734 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6735 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6736 child_bitfield_bit_size, child_bitfield_bit_offset,
6737 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6738 language_flags);
6739 } else {
6740 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006741
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006742 const char *parent_name =
6743 valobj ? valobj->GetName().GetCString() : NULL;
6744 if (parent_name) {
6745 child_name.assign(1, '*');
6746 child_name += parent_name;
6747 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006748
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006749 // We have a pointer to an simple type
6750 if (idx == 0) {
6751 child_byte_size = pointee_clang_type.GetByteSize(
6752 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6753 child_byte_offset = 0;
6754 return pointee_clang_type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006755 }
6756 }
6757 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006758 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006759
6760 case clang::Type::LValueReference:
6761 case clang::Type::RValueReference:
6762 if (idx_is_valid) {
6763 const clang::ReferenceType *reference_type =
6764 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6765 CompilerType pointee_clang_type(getASTContext(),
6766 reference_type->getPointeeType());
6767 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6768 child_is_deref_of_parent = false;
6769 bool tmp_child_is_deref_of_parent = false;
6770 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6771 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6772 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6773 child_bitfield_bit_size, child_bitfield_bit_offset,
6774 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6775 language_flags);
6776 } else {
6777 const char *parent_name =
6778 valobj ? valobj->GetName().GetCString() : NULL;
6779 if (parent_name) {
6780 child_name.assign(1, '&');
6781 child_name += parent_name;
6782 }
6783
6784 // We have a pointer to an simple type
6785 if (idx == 0) {
6786 child_byte_size = pointee_clang_type.GetByteSize(
6787 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6788 child_byte_offset = 0;
6789 return pointee_clang_type;
6790 }
6791 }
6792 }
6793 break;
6794
6795 case clang::Type::Typedef: {
6796 CompilerType typedefed_clang_type(
6797 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
6798 ->getDecl()
6799 ->getUnderlyingType());
6800 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
6801 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6802 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6803 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6804 child_is_deref_of_parent, valobj, language_flags);
6805 } break;
6806
6807 case clang::Type::Auto: {
6808 CompilerType elaborated_clang_type(
6809 getASTContext(),
6810 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
6811 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6812 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6813 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6814 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6815 child_is_deref_of_parent, valobj, language_flags);
6816 }
6817
6818 case clang::Type::Elaborated: {
6819 CompilerType elaborated_clang_type(
6820 getASTContext(),
6821 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
6822 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6823 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6824 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6825 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6826 child_is_deref_of_parent, valobj, language_flags);
6827 }
6828
6829 case clang::Type::Paren: {
6830 CompilerType paren_clang_type(
6831 getASTContext(),
6832 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
6833 return paren_clang_type.GetChildCompilerTypeAtIndex(
6834 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6835 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6836 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6837 child_is_deref_of_parent, valobj, language_flags);
6838 }
6839
6840 default:
6841 break;
6842 }
6843 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006844}
6845
Kate Stoneb9c1b512016-09-06 20:57:50 +00006846static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
6847 const clang::CXXBaseSpecifier *base_spec,
6848 bool omit_empty_base_classes) {
6849 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006850
Kate Stoneb9c1b512016-09-06 20:57:50 +00006851 const clang::CXXRecordDecl *cxx_record_decl =
6852 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6853
6854 // const char *super_name = record_decl->getNameAsCString();
6855 // const char *base_name =
6856 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6857 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6858 //
6859 if (cxx_record_decl) {
6860 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6861 for (base_class = cxx_record_decl->bases_begin(),
6862 base_class_end = cxx_record_decl->bases_end();
6863 base_class != base_class_end; ++base_class) {
6864 if (omit_empty_base_classes) {
6865 if (BaseSpecifierIsEmpty(base_class))
6866 continue;
6867 }
6868
6869 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
6870 // super_name, base_name,
6871 // child_idx,
6872 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6873 //
6874 //
6875 if (base_class == base_spec)
6876 return child_idx;
6877 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006878 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006879 }
6880
6881 return UINT32_MAX;
6882}
6883
6884static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
6885 clang::NamedDecl *canonical_decl,
6886 bool omit_empty_base_classes) {
6887 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
6888 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6889 omit_empty_base_classes);
6890
6891 clang::RecordDecl::field_iterator field, field_end;
6892 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6893 field != field_end; ++field, ++child_idx) {
6894 if (field->getCanonicalDecl() == canonical_decl)
6895 return child_idx;
6896 }
6897
6898 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00006899}
6900
6901// Look for a child member (doesn't include base classes, but it does include
6902// their members) in the type hierarchy. Returns an index path into "clang_type"
6903// on how to reach the appropriate member.
6904//
6905// class A
6906// {
6907// public:
6908// int m_a;
6909// int m_b;
6910// };
6911//
6912// class B
6913// {
6914// };
6915//
6916// class C :
6917// public B,
6918// public A
6919// {
6920// };
6921//
6922// If we have a clang type that describes "class C", and we wanted to looked
6923// "m_b" in it:
6924//
Kate Stoneb9c1b512016-09-06 20:57:50 +00006925// With omit_empty_base_classes == false we would get an integer array back
6926// with:
Greg Claytond8d4a572015-08-11 21:38:15 +00006927// { 1, 1 }
6928// The first index 1 is the child index for "class A" within class C
6929// The second index 1 is the child index for "m_b" within class A
6930//
6931// With omit_empty_base_classes == true we would get an integer array back with:
6932// { 0, 1 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006933// The first index 0 is the child index for "class A" within class C (since
6934// class B doesn't have any members it doesn't count)
Greg Claytond8d4a572015-08-11 21:38:15 +00006935// The second index 1 is the child index for "m_b" within class A
6936
Kate Stoneb9c1b512016-09-06 20:57:50 +00006937size_t ClangASTContext::GetIndexOfChildMemberWithName(
6938 lldb::opaque_compiler_type_t type, const char *name,
6939 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
6940 if (type && name && name[0]) {
6941 clang::QualType qual_type(GetCanonicalQualType(type));
6942 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6943 switch (type_class) {
6944 case clang::Type::Record:
6945 if (GetCompleteType(type)) {
6946 const clang::RecordType *record_type =
6947 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6948 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00006949
Kate Stoneb9c1b512016-09-06 20:57:50 +00006950 assert(record_decl);
6951 uint32_t child_idx = 0;
6952
6953 const clang::CXXRecordDecl *cxx_record_decl =
6954 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6955
6956 // Try and find a field that matches NAME
6957 clang::RecordDecl::field_iterator field, field_end;
6958 llvm::StringRef name_sref(name);
6959 for (field = record_decl->field_begin(),
6960 field_end = record_decl->field_end();
6961 field != field_end; ++field, ++child_idx) {
6962 llvm::StringRef field_name = field->getName();
6963 if (field_name.empty()) {
6964 CompilerType field_type(getASTContext(), field->getType());
6965 child_indexes.push_back(child_idx);
6966 if (field_type.GetIndexOfChildMemberWithName(
6967 name, omit_empty_base_classes, child_indexes))
6968 return child_indexes.size();
6969 child_indexes.pop_back();
6970
6971 } else if (field_name.equals(name_sref)) {
6972 // We have to add on the number of base classes to this index!
6973 child_indexes.push_back(
6974 child_idx + ClangASTContext::GetNumBaseClasses(
6975 cxx_record_decl, omit_empty_base_classes));
6976 return child_indexes.size();
6977 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006978 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006979
Kate Stoneb9c1b512016-09-06 20:57:50 +00006980 if (cxx_record_decl) {
6981 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
6982
6983 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
6984
6985 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
6986 // Didn't find things easily, lets let clang do its thang...
6987 clang::IdentifierInfo &ident_ref =
6988 getASTContext()->Idents.get(name_sref);
6989 clang::DeclarationName decl_name(&ident_ref);
6990
6991 clang::CXXBasePaths paths;
6992 if (cxx_record_decl->lookupInBases(
6993 [decl_name](const clang::CXXBaseSpecifier *specifier,
6994 clang::CXXBasePath &path) {
6995 return clang::CXXRecordDecl::FindOrdinaryMember(
6996 specifier, path, decl_name);
6997 },
6998 paths)) {
6999 clang::CXXBasePaths::const_paths_iterator path,
7000 path_end = paths.end();
7001 for (path = paths.begin(); path != path_end; ++path) {
7002 const size_t num_path_elements = path->size();
7003 for (size_t e = 0; e < num_path_elements; ++e) {
7004 clang::CXXBasePathElement elem = (*path)[e];
7005
7006 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7007 omit_empty_base_classes);
7008 if (child_idx == UINT32_MAX) {
7009 child_indexes.clear();
7010 return 0;
7011 } else {
7012 child_indexes.push_back(child_idx);
7013 parent_record_decl = llvm::cast<clang::RecordDecl>(
7014 elem.Base->getType()
7015 ->getAs<clang::RecordType>()
7016 ->getDecl());
7017 }
7018 }
7019 for (clang::NamedDecl *path_decl : path->Decls) {
7020 child_idx = GetIndexForRecordChild(
7021 parent_record_decl, path_decl, 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 }
7028 }
7029 }
7030 return child_indexes.size();
7031 }
7032 }
7033 }
7034 break;
7035
7036 case clang::Type::ObjCObject:
7037 case clang::Type::ObjCInterface:
7038 if (GetCompleteType(type)) {
7039 llvm::StringRef name_sref(name);
7040 const clang::ObjCObjectType *objc_class_type =
7041 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7042 assert(objc_class_type);
7043 if (objc_class_type) {
7044 uint32_t child_idx = 0;
7045 clang::ObjCInterfaceDecl *class_interface_decl =
7046 objc_class_type->getInterface();
7047
7048 if (class_interface_decl) {
7049 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7050 ivar_end = class_interface_decl->ivar_end();
7051 clang::ObjCInterfaceDecl *superclass_interface_decl =
7052 class_interface_decl->getSuperClass();
7053
7054 for (ivar_pos = class_interface_decl->ivar_begin();
7055 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7056 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7057
7058 if (ivar_decl->getName().equals(name_sref)) {
7059 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7060 (omit_empty_base_classes &&
7061 ObjCDeclHasIVars(superclass_interface_decl, true)))
7062 ++child_idx;
7063
7064 child_indexes.push_back(child_idx);
7065 return child_indexes.size();
7066 }
7067 }
7068
7069 if (superclass_interface_decl) {
7070 // The super class index is always zero for ObjC classes,
7071 // so we push it onto the child indexes in case we find
7072 // an ivar in our superclass...
7073 child_indexes.push_back(0);
7074
7075 CompilerType superclass_clang_type(
7076 getASTContext(), getASTContext()->getObjCInterfaceType(
7077 superclass_interface_decl));
7078 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7079 name, omit_empty_base_classes, child_indexes)) {
7080 // We did find an ivar in a superclass so just
7081 // return the results!
7082 return child_indexes.size();
7083 }
7084
7085 // We didn't find an ivar matching "name" in our
7086 // superclass, pop the superclass zero index that
7087 // we pushed on above.
7088 child_indexes.pop_back();
7089 }
7090 }
7091 }
7092 }
7093 break;
7094
7095 case clang::Type::ObjCObjectPointer: {
7096 CompilerType objc_object_clang_type(
7097 getASTContext(),
7098 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7099 ->getPointeeType());
7100 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7101 name, omit_empty_base_classes, child_indexes);
7102 } break;
7103
7104 case clang::Type::ConstantArray: {
7105 // const clang::ConstantArrayType *array =
7106 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7107 // const uint64_t element_count =
7108 // array->getSize().getLimitedValue();
7109 //
7110 // if (idx < element_count)
7111 // {
7112 // std::pair<uint64_t, unsigned> field_type_info =
7113 // ast->getTypeInfo(array->getElementType());
7114 //
7115 // char element_name[32];
7116 // ::snprintf (element_name, sizeof (element_name),
7117 // "%s[%u]", parent_name ? parent_name : "", idx);
7118 //
7119 // child_name.assign(element_name);
7120 // assert(field_type_info.first % 8 == 0);
7121 // child_byte_size = field_type_info.first / 8;
7122 // child_byte_offset = idx * child_byte_size;
7123 // return array->getElementType().getAsOpaquePtr();
7124 // }
7125 } break;
7126
7127 // case clang::Type::MemberPointerType:
7128 // {
7129 // MemberPointerType *mem_ptr_type =
7130 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7131 // clang::QualType pointee_type =
7132 // mem_ptr_type->getPointeeType();
7133 //
7134 // if (ClangASTContext::IsAggregateType
7135 // (pointee_type.getAsOpaquePtr()))
7136 // {
7137 // return GetIndexOfChildWithName (ast,
7138 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7139 // name);
7140 // }
7141 // }
7142 // break;
7143 //
7144 case clang::Type::LValueReference:
7145 case clang::Type::RValueReference: {
7146 const clang::ReferenceType *reference_type =
7147 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7148 clang::QualType pointee_type(reference_type->getPointeeType());
7149 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7150
7151 if (pointee_clang_type.IsAggregateType()) {
7152 return pointee_clang_type.GetIndexOfChildMemberWithName(
7153 name, omit_empty_base_classes, child_indexes);
7154 }
7155 } break;
7156
7157 case clang::Type::Pointer: {
7158 CompilerType pointee_clang_type(GetPointeeType(type));
7159
7160 if (pointee_clang_type.IsAggregateType()) {
7161 return pointee_clang_type.GetIndexOfChildMemberWithName(
7162 name, omit_empty_base_classes, child_indexes);
7163 }
7164 } break;
7165
7166 case clang::Type::Typedef:
7167 return CompilerType(getASTContext(),
7168 llvm::cast<clang::TypedefType>(qual_type)
7169 ->getDecl()
7170 ->getUnderlyingType())
7171 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7172 child_indexes);
7173
7174 case clang::Type::Auto:
7175 return CompilerType(
7176 getASTContext(),
7177 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7178 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7179 child_indexes);
7180
7181 case clang::Type::Elaborated:
7182 return CompilerType(
7183 getASTContext(),
7184 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7185 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7186 child_indexes);
7187
7188 case clang::Type::Paren:
7189 return CompilerType(getASTContext(),
7190 llvm::cast<clang::ParenType>(qual_type)->desugar())
7191 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7192 child_indexes);
7193
7194 default:
7195 break;
7196 }
7197 }
7198 return 0;
7199}
Greg Claytond8d4a572015-08-11 21:38:15 +00007200
7201// Get the index of the child of "clang_type" whose name matches. This function
7202// doesn't descend into the children, but only looks one level deep and name
7203// matches can include base class names.
7204
7205uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007206ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7207 const char *name,
7208 bool omit_empty_base_classes) {
7209 if (type && name && name[0]) {
7210 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007211
Kate Stoneb9c1b512016-09-06 20:57:50 +00007212 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7213
7214 switch (type_class) {
7215 case clang::Type::Record:
7216 if (GetCompleteType(type)) {
7217 const clang::RecordType *record_type =
7218 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7219 const clang::RecordDecl *record_decl = record_type->getDecl();
7220
7221 assert(record_decl);
7222 uint32_t child_idx = 0;
7223
7224 const clang::CXXRecordDecl *cxx_record_decl =
7225 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7226
7227 if (cxx_record_decl) {
7228 clang::CXXRecordDecl::base_class_const_iterator base_class,
7229 base_class_end;
7230 for (base_class = cxx_record_decl->bases_begin(),
7231 base_class_end = cxx_record_decl->bases_end();
7232 base_class != base_class_end; ++base_class) {
7233 // Skip empty base classes
7234 clang::CXXRecordDecl *base_class_decl =
7235 llvm::cast<clang::CXXRecordDecl>(
7236 base_class->getType()
7237 ->getAs<clang::RecordType>()
7238 ->getDecl());
7239 if (omit_empty_base_classes &&
7240 ClangASTContext::RecordHasFields(base_class_decl) == false)
7241 continue;
7242
7243 CompilerType base_class_clang_type(getASTContext(),
7244 base_class->getType());
7245 std::string base_class_type_name(
7246 base_class_clang_type.GetTypeName().AsCString(""));
7247 if (base_class_type_name.compare(name) == 0)
7248 return child_idx;
7249 ++child_idx;
7250 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007251 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007252
Kate Stoneb9c1b512016-09-06 20:57:50 +00007253 // Try and find a field that matches NAME
7254 clang::RecordDecl::field_iterator field, field_end;
7255 llvm::StringRef name_sref(name);
7256 for (field = record_decl->field_begin(),
7257 field_end = record_decl->field_end();
7258 field != field_end; ++field, ++child_idx) {
7259 if (field->getName().equals(name_sref))
7260 return child_idx;
7261 }
7262 }
7263 break;
7264
7265 case clang::Type::ObjCObject:
7266 case clang::Type::ObjCInterface:
7267 if (GetCompleteType(type)) {
7268 llvm::StringRef name_sref(name);
7269 const clang::ObjCObjectType *objc_class_type =
7270 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7271 assert(objc_class_type);
7272 if (objc_class_type) {
7273 uint32_t child_idx = 0;
7274 clang::ObjCInterfaceDecl *class_interface_decl =
7275 objc_class_type->getInterface();
7276
7277 if (class_interface_decl) {
7278 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7279 ivar_end = class_interface_decl->ivar_end();
7280 clang::ObjCInterfaceDecl *superclass_interface_decl =
7281 class_interface_decl->getSuperClass();
7282
7283 for (ivar_pos = class_interface_decl->ivar_begin();
7284 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7285 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7286
7287 if (ivar_decl->getName().equals(name_sref)) {
7288 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7289 (omit_empty_base_classes &&
7290 ObjCDeclHasIVars(superclass_interface_decl, true)))
7291 ++child_idx;
7292
7293 return child_idx;
7294 }
7295 }
7296
7297 if (superclass_interface_decl) {
7298 if (superclass_interface_decl->getName().equals(name_sref))
7299 return 0;
7300 }
7301 }
7302 }
7303 }
7304 break;
7305
7306 case clang::Type::ObjCObjectPointer: {
7307 CompilerType pointee_clang_type(
7308 getASTContext(),
7309 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7310 ->getPointeeType());
7311 return pointee_clang_type.GetIndexOfChildWithName(
7312 name, omit_empty_base_classes);
7313 } break;
7314
7315 case clang::Type::ConstantArray: {
7316 // const clang::ConstantArrayType *array =
7317 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7318 // const uint64_t element_count =
7319 // array->getSize().getLimitedValue();
7320 //
7321 // if (idx < element_count)
7322 // {
7323 // std::pair<uint64_t, unsigned> field_type_info =
7324 // ast->getTypeInfo(array->getElementType());
7325 //
7326 // char element_name[32];
7327 // ::snprintf (element_name, sizeof (element_name),
7328 // "%s[%u]", parent_name ? parent_name : "", idx);
7329 //
7330 // child_name.assign(element_name);
7331 // assert(field_type_info.first % 8 == 0);
7332 // child_byte_size = field_type_info.first / 8;
7333 // child_byte_offset = idx * child_byte_size;
7334 // return array->getElementType().getAsOpaquePtr();
7335 // }
7336 } break;
7337
7338 // case clang::Type::MemberPointerType:
7339 // {
7340 // MemberPointerType *mem_ptr_type =
7341 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7342 // clang::QualType pointee_type =
7343 // mem_ptr_type->getPointeeType();
7344 //
7345 // if (ClangASTContext::IsAggregateType
7346 // (pointee_type.getAsOpaquePtr()))
7347 // {
7348 // return GetIndexOfChildWithName (ast,
7349 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7350 // name);
7351 // }
7352 // }
7353 // break;
7354 //
7355 case clang::Type::LValueReference:
7356 case clang::Type::RValueReference: {
7357 const clang::ReferenceType *reference_type =
7358 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7359 CompilerType pointee_type(getASTContext(),
7360 reference_type->getPointeeType());
7361
7362 if (pointee_type.IsAggregateType()) {
7363 return pointee_type.GetIndexOfChildWithName(name,
7364 omit_empty_base_classes);
7365 }
7366 } break;
7367
7368 case clang::Type::Pointer: {
7369 const clang::PointerType *pointer_type =
7370 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7371 CompilerType pointee_type(getASTContext(),
7372 pointer_type->getPointeeType());
7373
7374 if (pointee_type.IsAggregateType()) {
7375 return pointee_type.GetIndexOfChildWithName(name,
7376 omit_empty_base_classes);
7377 } else {
7378 // if (parent_name)
7379 // {
7380 // child_name.assign(1, '*');
7381 // child_name += parent_name;
7382 // }
7383 //
7384 // // We have a pointer to an simple type
7385 // if (idx == 0)
7386 // {
7387 // std::pair<uint64_t, unsigned> clang_type_info
7388 // = ast->getTypeInfo(pointee_type);
7389 // assert(clang_type_info.first % 8 == 0);
7390 // child_byte_size = clang_type_info.first / 8;
7391 // child_byte_offset = 0;
7392 // return pointee_type.getAsOpaquePtr();
7393 // }
7394 }
7395 } break;
7396
7397 case clang::Type::Auto:
7398 return CompilerType(
7399 getASTContext(),
7400 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7401 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7402
7403 case clang::Type::Elaborated:
7404 return CompilerType(
7405 getASTContext(),
7406 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7407 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7408
7409 case clang::Type::Paren:
7410 return CompilerType(getASTContext(),
7411 llvm::cast<clang::ParenType>(qual_type)->desugar())
7412 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7413
7414 case clang::Type::Typedef:
7415 return CompilerType(getASTContext(),
7416 llvm::cast<clang::TypedefType>(qual_type)
7417 ->getDecl()
7418 ->getUnderlyingType())
7419 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7420
7421 default:
7422 break;
7423 }
7424 }
7425 return UINT32_MAX;
7426}
Greg Claytond8d4a572015-08-11 21:38:15 +00007427
7428size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007429ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7430 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007431 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007432
Kate Stoneb9c1b512016-09-06 20:57:50 +00007433 clang::QualType qual_type(GetCanonicalQualType(type));
7434 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7435 switch (type_class) {
7436 case clang::Type::Record:
7437 if (GetCompleteType(type)) {
7438 const clang::CXXRecordDecl *cxx_record_decl =
7439 qual_type->getAsCXXRecordDecl();
7440 if (cxx_record_decl) {
7441 const clang::ClassTemplateSpecializationDecl *template_decl =
7442 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7443 cxx_record_decl);
7444 if (template_decl)
7445 return template_decl->getTemplateArgs().size();
7446 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007447 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007448 break;
7449
7450 case clang::Type::Typedef:
7451 return (CompilerType(getASTContext(),
7452 llvm::cast<clang::TypedefType>(qual_type)
7453 ->getDecl()
7454 ->getUnderlyingType()))
7455 .GetNumTemplateArguments();
7456
7457 case clang::Type::Auto:
7458 return (CompilerType(
7459 getASTContext(),
7460 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7461 .GetNumTemplateArguments();
7462
7463 case clang::Type::Elaborated:
7464 return (CompilerType(
7465 getASTContext(),
7466 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7467 .GetNumTemplateArguments();
7468
7469 case clang::Type::Paren:
7470 return (CompilerType(getASTContext(),
7471 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7472 .GetNumTemplateArguments();
7473
7474 default:
7475 break;
7476 }
7477
7478 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007479}
7480
Pavel Labath769b21e2017-11-13 14:26:21 +00007481const clang::ClassTemplateSpecializationDecl *
7482ClangASTContext::GetAsTemplateSpecialization(
7483 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007484 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007485 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007486
7487 clang::QualType qual_type(GetCanonicalQualType(type));
7488 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7489 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007490 case clang::Type::Record: {
7491 if (! GetCompleteType(type))
7492 return nullptr;
7493 const clang::CXXRecordDecl *cxx_record_decl =
7494 qual_type->getAsCXXRecordDecl();
7495 if (!cxx_record_decl)
7496 return nullptr;
7497 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7498 cxx_record_decl);
7499 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007500
7501 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007502 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7503 ->getDecl()
7504 ->getUnderlyingType()
7505 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007506
7507 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007508 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7509 ->getDeducedType()
7510 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007511
7512 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007513 return GetAsTemplateSpecialization(
7514 llvm::cast<clang::ElaboratedType>(qual_type)
7515 ->getNamedType()
7516 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007517
7518 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007519 return GetAsTemplateSpecialization(
7520 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007521
7522 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007523 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007524 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007525}
7526
7527lldb::TemplateArgumentKind
7528ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7529 size_t arg_idx) {
7530 const clang::ClassTemplateSpecializationDecl *template_decl =
7531 GetAsTemplateSpecialization(type);
7532 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7533 return eTemplateArgumentKindNull;
7534
7535 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7536 case clang::TemplateArgument::Null:
7537 return eTemplateArgumentKindNull;
7538
7539 case clang::TemplateArgument::NullPtr:
7540 return eTemplateArgumentKindNullPtr;
7541
7542 case clang::TemplateArgument::Type:
7543 return eTemplateArgumentKindType;
7544
7545 case clang::TemplateArgument::Declaration:
7546 return eTemplateArgumentKindDeclaration;
7547
7548 case clang::TemplateArgument::Integral:
7549 return eTemplateArgumentKindIntegral;
7550
7551 case clang::TemplateArgument::Template:
7552 return eTemplateArgumentKindTemplate;
7553
7554 case clang::TemplateArgument::TemplateExpansion:
7555 return eTemplateArgumentKindTemplateExpansion;
7556
7557 case clang::TemplateArgument::Expression:
7558 return eTemplateArgumentKindExpression;
7559
7560 case clang::TemplateArgument::Pack:
7561 return eTemplateArgumentKindPack;
7562 }
7563 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7564}
7565
7566CompilerType
7567ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7568 size_t idx) {
7569 const clang::ClassTemplateSpecializationDecl *template_decl =
7570 GetAsTemplateSpecialization(type);
7571 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7572 return CompilerType();
7573
7574 const clang::TemplateArgument &template_arg =
7575 template_decl->getTemplateArgs()[idx];
7576 if (template_arg.getKind() != clang::TemplateArgument::Type)
7577 return CompilerType();
7578
7579 return CompilerType(getASTContext(), template_arg.getAsType());
7580}
7581
Pavel Labathf59056f2017-11-30 10:16:54 +00007582llvm::Optional<CompilerType::IntegralTemplateArgument>
Pavel Labath769b21e2017-11-13 14:26:21 +00007583ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7584 size_t idx) {
7585 const clang::ClassTemplateSpecializationDecl *template_decl =
7586 GetAsTemplateSpecialization(type);
7587 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
Pavel Labathf59056f2017-11-30 10:16:54 +00007588 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007589
7590 const clang::TemplateArgument &template_arg =
7591 template_decl->getTemplateArgs()[idx];
7592 if (template_arg.getKind() != clang::TemplateArgument::Integral)
Pavel Labathf59056f2017-11-30 10:16:54 +00007593 return llvm::None;
Pavel Labath769b21e2017-11-13 14:26:21 +00007594
Pavel Labathf59056f2017-11-30 10:16:54 +00007595 return {{template_arg.getAsIntegral(),
7596 CompilerType(getASTContext(), template_arg.getIntegralType())}};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007597}
7598
Kate Stoneb9c1b512016-09-06 20:57:50 +00007599CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7600 if (type)
7601 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7602 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007603}
7604
Kate Stoneb9c1b512016-09-06 20:57:50 +00007605clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7606 const clang::EnumType *enutype =
7607 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7608 if (enutype)
7609 return enutype->getDecl();
7610 return NULL;
7611}
7612
7613clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7614 const clang::RecordType *record_type =
7615 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7616 if (record_type)
7617 return record_type->getDecl();
7618 return nullptr;
7619}
7620
7621clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
7622 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
7623 if (qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00007624 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007625 else
7626 return qual_type->getAsTagDecl();
Greg Claytone6b36cd2015-12-08 01:02:08 +00007627}
7628
Greg Claytond8d4a572015-08-11 21:38:15 +00007629clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007630ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7631 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007632}
7633
7634clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007635ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7636 const clang::ObjCObjectType *objc_class_type =
7637 llvm::dyn_cast<clang::ObjCObjectType>(
7638 ClangUtil::GetCanonicalQualType(type));
7639 if (objc_class_type)
7640 return objc_class_type->getInterface();
7641 return nullptr;
7642}
7643
7644clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
7645 const CompilerType &type, const char *name,
7646 const CompilerType &field_clang_type, AccessType access,
7647 uint32_t bitfield_bit_size) {
7648 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007649 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007650 ClangASTContext *ast =
7651 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7652 if (!ast)
7653 return nullptr;
7654 clang::ASTContext *clang_ast = ast->getASTContext();
7655
7656 clang::FieldDecl *field = nullptr;
7657
7658 clang::Expr *bit_width = nullptr;
7659 if (bitfield_bit_size != 0) {
7660 llvm::APInt bitfield_bit_size_apint(
7661 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7662 bit_width = new (*clang_ast)
7663 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7664 clang_ast->IntTy, clang::SourceLocation());
7665 }
7666
7667 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7668 if (record_decl) {
7669 field = clang::FieldDecl::Create(
7670 *clang_ast, record_decl, clang::SourceLocation(),
7671 clang::SourceLocation(),
7672 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7673 ClangUtil::GetQualType(field_clang_type), // Field type
7674 nullptr, // TInfo *
7675 bit_width, // BitWidth
7676 false, // Mutable
7677 clang::ICIS_NoInit); // HasInit
7678
7679 if (!name) {
7680 // Determine whether this field corresponds to an anonymous
7681 // struct or union.
7682 if (const clang::TagType *TagT =
7683 field->getType()->getAs<clang::TagType>()) {
7684 if (clang::RecordDecl *Rec =
7685 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7686 if (!Rec->getDeclName()) {
7687 Rec->setAnonymousStructOrUnion(true);
7688 field->setImplicit();
7689 }
7690 }
7691 }
7692
7693 if (field) {
7694 field->setAccess(
7695 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7696
7697 record_decl->addDecl(field);
7698
7699#ifdef LLDB_CONFIGURATION_DEBUG
7700 VerifyDecl(field);
7701#endif
7702 }
7703 } else {
7704 clang::ObjCInterfaceDecl *class_interface_decl =
7705 ast->GetAsObjCInterfaceDecl(type);
7706
7707 if (class_interface_decl) {
7708 const bool is_synthesized = false;
7709
7710 field_clang_type.GetCompleteType();
7711
7712 field = clang::ObjCIvarDecl::Create(
7713 *clang_ast, class_interface_decl, clang::SourceLocation(),
7714 clang::SourceLocation(),
7715 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7716 ClangUtil::GetQualType(field_clang_type), // Field type
7717 nullptr, // TypeSourceInfo *
7718 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7719 is_synthesized);
7720
7721 if (field) {
7722 class_interface_decl->addDecl(field);
7723
7724#ifdef LLDB_CONFIGURATION_DEBUG
7725 VerifyDecl(field);
7726#endif
7727 }
7728 }
7729 }
7730 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007731}
7732
Kate Stoneb9c1b512016-09-06 20:57:50 +00007733void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7734 if (!type)
7735 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007736
Kate Stoneb9c1b512016-09-06 20:57:50 +00007737 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7738 if (!ast)
7739 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007740
Kate Stoneb9c1b512016-09-06 20:57:50 +00007741 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007742
Kate Stoneb9c1b512016-09-06 20:57:50 +00007743 if (!record_decl)
7744 return;
7745
7746 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7747
7748 IndirectFieldVector indirect_fields;
7749 clang::RecordDecl::field_iterator field_pos;
7750 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7751 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7752 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7753 last_field_pos = field_pos++) {
7754 if (field_pos->isAnonymousStructOrUnion()) {
7755 clang::QualType field_qual_type = field_pos->getType();
7756
7757 const clang::RecordType *field_record_type =
7758 field_qual_type->getAs<clang::RecordType>();
7759
7760 if (!field_record_type)
7761 continue;
7762
7763 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7764
7765 if (!field_record_decl)
7766 continue;
7767
7768 for (clang::RecordDecl::decl_iterator
7769 di = field_record_decl->decls_begin(),
7770 de = field_record_decl->decls_end();
7771 di != de; ++di) {
7772 if (clang::FieldDecl *nested_field_decl =
7773 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7774 clang::NamedDecl **chain =
7775 new (*ast->getASTContext()) clang::NamedDecl *[2];
7776 chain[0] = *field_pos;
7777 chain[1] = nested_field_decl;
7778 clang::IndirectFieldDecl *indirect_field =
7779 clang::IndirectFieldDecl::Create(
7780 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7781 nested_field_decl->getIdentifier(),
7782 nested_field_decl->getType(), {chain, 2});
7783
7784 indirect_field->setImplicit();
7785
7786 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7787 field_pos->getAccess(), nested_field_decl->getAccess()));
7788
7789 indirect_fields.push_back(indirect_field);
7790 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7791 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7792 size_t nested_chain_size =
7793 nested_indirect_field_decl->getChainingSize();
7794 clang::NamedDecl **chain = new (*ast->getASTContext())
7795 clang::NamedDecl *[nested_chain_size + 1];
7796 chain[0] = *field_pos;
7797
7798 int chain_index = 1;
7799 for (clang::IndirectFieldDecl::chain_iterator
7800 nci = nested_indirect_field_decl->chain_begin(),
7801 nce = nested_indirect_field_decl->chain_end();
7802 nci < nce; ++nci) {
7803 chain[chain_index] = *nci;
7804 chain_index++;
7805 }
7806
7807 clang::IndirectFieldDecl *indirect_field =
7808 clang::IndirectFieldDecl::Create(
7809 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7810 nested_indirect_field_decl->getIdentifier(),
7811 nested_indirect_field_decl->getType(),
7812 {chain, nested_chain_size + 1});
7813
7814 indirect_field->setImplicit();
7815
7816 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7817 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
7818
7819 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00007820 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007821 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007822 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007823 }
7824
7825 // Check the last field to see if it has an incomplete array type as its
7826 // last member and if it does, the tell the record decl about it
7827 if (last_field_pos != field_end_pos) {
7828 if (last_field_pos->getType()->isIncompleteArrayType())
7829 record_decl->hasFlexibleArrayMember();
7830 }
7831
7832 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
7833 ife = indirect_fields.end();
7834 ifi < ife; ++ifi) {
7835 record_decl->addDecl(*ifi);
7836 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007837}
7838
Kate Stoneb9c1b512016-09-06 20:57:50 +00007839void ClangASTContext::SetIsPacked(const CompilerType &type) {
7840 if (type) {
7841 ClangASTContext *ast =
7842 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7843 if (ast) {
7844 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
7845
7846 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00007847 return;
7848
Kate Stoneb9c1b512016-09-06 20:57:50 +00007849 record_decl->addAttr(
7850 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00007851 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007852 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007853}
7854
Kate Stoneb9c1b512016-09-06 20:57:50 +00007855clang::VarDecl *ClangASTContext::AddVariableToRecordType(
7856 const CompilerType &type, const char *name, const CompilerType &var_type,
7857 AccessType access) {
7858 clang::VarDecl *var_decl = nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007859
Kate Stoneb9c1b512016-09-06 20:57:50 +00007860 if (!type.IsValid() || !var_type.IsValid())
7861 return nullptr;
7862 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7863 if (!ast)
7864 return nullptr;
7865
7866 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7867 if (record_decl) {
7868 var_decl = clang::VarDecl::Create(
7869 *ast->getASTContext(), // ASTContext &
7870 record_decl, // DeclContext *
7871 clang::SourceLocation(), // clang::SourceLocation StartLoc
7872 clang::SourceLocation(), // clang::SourceLocation IdLoc
7873 name ? &ast->getASTContext()->Idents.get(name)
7874 : nullptr, // clang::IdentifierInfo *
7875 ClangUtil::GetQualType(var_type), // Variable clang::QualType
7876 nullptr, // TypeSourceInfo *
7877 clang::SC_Static); // StorageClass
7878 if (var_decl) {
7879 var_decl->setAccess(
7880 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7881 record_decl->addDecl(var_decl);
7882
Greg Claytond8d4a572015-08-11 21:38:15 +00007883#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00007884 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00007885#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00007886 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007887 }
7888 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00007889}
7890
Kate Stoneb9c1b512016-09-06 20:57:50 +00007891clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
7892 lldb::opaque_compiler_type_t type, const char *name,
7893 const CompilerType &method_clang_type, lldb::AccessType access,
7894 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
7895 bool is_attr_used, bool is_artificial) {
7896 if (!type || !method_clang_type.IsValid() || name == nullptr ||
7897 name[0] == '\0')
7898 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007899
Kate Stoneb9c1b512016-09-06 20:57:50 +00007900 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007901
Kate Stoneb9c1b512016-09-06 20:57:50 +00007902 clang::CXXRecordDecl *cxx_record_decl =
7903 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007904
Kate Stoneb9c1b512016-09-06 20:57:50 +00007905 if (cxx_record_decl == nullptr)
7906 return nullptr;
7907
7908 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
7909
7910 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7911
7912 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
7913
7914 const clang::FunctionType *function_type =
7915 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7916
7917 if (function_type == nullptr)
7918 return nullptr;
7919
7920 const clang::FunctionProtoType *method_function_prototype(
7921 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7922
7923 if (!method_function_prototype)
7924 return nullptr;
7925
7926 unsigned int num_params = method_function_prototype->getNumParams();
7927
7928 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7929 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7930
7931 if (is_artificial)
7932 return nullptr; // skip everything artificial
7933
7934 if (name[0] == '~') {
7935 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
7936 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7937 clang::DeclarationNameInfo(
7938 getASTContext()->DeclarationNames.getCXXDestructorName(
7939 getASTContext()->getCanonicalType(record_qual_type)),
7940 clang::SourceLocation()),
7941 method_qual_type, nullptr, is_inline, is_artificial);
7942 cxx_method_decl = cxx_dtor_decl;
7943 } else if (decl_name == cxx_record_decl->getDeclName()) {
7944 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
7945 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7946 clang::DeclarationNameInfo(
7947 getASTContext()->DeclarationNames.getCXXConstructorName(
7948 getASTContext()->getCanonicalType(record_qual_type)),
7949 clang::SourceLocation()),
7950 method_qual_type,
7951 nullptr, // TypeSourceInfo *
7952 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
7953 cxx_method_decl = cxx_ctor_decl;
7954 } else {
7955 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7956 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7957
7958 if (IsOperator(name, op_kind)) {
7959 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
7960 // Check the number of operator parameters. Sometimes we have
7961 // seen bad DWARF that doesn't correctly describe operators and
7962 // if we try to create a method and add it to the class, clang
7963 // will assert and crash, so we need to make sure things are
7964 // acceptable.
7965 const bool is_method = true;
7966 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
7967 is_method, op_kind, num_params))
7968 return nullptr;
7969 cxx_method_decl = clang::CXXMethodDecl::Create(
7970 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7971 clang::DeclarationNameInfo(
7972 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
7973 clang::SourceLocation()),
7974 method_qual_type,
7975 nullptr, // TypeSourceInfo *
7976 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
7977 } else if (num_params == 0) {
7978 // Conversion operators don't take params...
7979 cxx_method_decl = clang::CXXConversionDecl::Create(
7980 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7981 clang::DeclarationNameInfo(
7982 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
7983 getASTContext()->getCanonicalType(
7984 function_type->getReturnType())),
7985 clang::SourceLocation()),
7986 method_qual_type,
7987 nullptr, // TypeSourceInfo *
7988 is_inline, is_explicit, false /*is_constexpr*/,
7989 clang::SourceLocation());
7990 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007991 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007992
7993 if (cxx_method_decl == nullptr) {
7994 cxx_method_decl = clang::CXXMethodDecl::Create(
7995 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7996 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
7997 method_qual_type,
7998 nullptr, // TypeSourceInfo *
7999 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008000 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008001 }
8002
8003 clang::AccessSpecifier access_specifier =
8004 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8005
8006 cxx_method_decl->setAccess(access_specifier);
8007 cxx_method_decl->setVirtualAsWritten(is_virtual);
8008
8009 if (is_attr_used)
8010 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8011
8012 // Populate the method decl with parameter decls
8013
8014 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8015
8016 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8017 params.push_back(clang::ParmVarDecl::Create(
8018 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8019 clang::SourceLocation(),
8020 nullptr, // anonymous
8021 method_function_prototype->getParamType(param_index), nullptr,
8022 clang::SC_None, nullptr));
8023 }
8024
8025 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8026
8027 cxx_record_decl->addDecl(cxx_method_decl);
8028
8029 // Sometimes the debug info will mention a constructor (default/copy/move),
8030 // destructor, or assignment operator (copy/move) but there won't be any
8031 // version of this in the code. So we check if the function was artificially
8032 // generated and if it is trivial and this lets the compiler/backend know
8033 // that it can inline the IR for these when it needs to and we can avoid a
8034 // "missing function" error when running expressions.
8035
8036 if (is_artificial) {
8037 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8038 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8039 (cxx_ctor_decl->isCopyConstructor() &&
8040 cxx_record_decl->hasTrivialCopyConstructor()) ||
8041 (cxx_ctor_decl->isMoveConstructor() &&
8042 cxx_record_decl->hasTrivialMoveConstructor()))) {
8043 cxx_ctor_decl->setDefaulted();
8044 cxx_ctor_decl->setTrivial(true);
8045 } else if (cxx_dtor_decl) {
8046 if (cxx_record_decl->hasTrivialDestructor()) {
8047 cxx_dtor_decl->setDefaulted();
8048 cxx_dtor_decl->setTrivial(true);
8049 }
8050 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8051 cxx_record_decl->hasTrivialCopyAssignment()) ||
8052 (cxx_method_decl->isMoveAssignmentOperator() &&
8053 cxx_record_decl->hasTrivialMoveAssignment())) {
8054 cxx_method_decl->setDefaulted();
8055 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008056 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008057 }
8058
Greg Claytond8d4a572015-08-11 21:38:15 +00008059#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008060 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008061#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008062
Kate Stoneb9c1b512016-09-06 20:57:50 +00008063 // printf ("decl->isPolymorphic() = %i\n",
8064 // cxx_record_decl->isPolymorphic());
8065 // printf ("decl->isAggregate() = %i\n",
8066 // cxx_record_decl->isAggregate());
8067 // printf ("decl->isPOD() = %i\n",
8068 // cxx_record_decl->isPOD());
8069 // printf ("decl->isEmpty() = %i\n",
8070 // cxx_record_decl->isEmpty());
8071 // printf ("decl->isAbstract() = %i\n",
8072 // cxx_record_decl->isAbstract());
8073 // printf ("decl->hasTrivialConstructor() = %i\n",
8074 // cxx_record_decl->hasTrivialConstructor());
8075 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8076 // cxx_record_decl->hasTrivialCopyConstructor());
8077 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8078 // cxx_record_decl->hasTrivialCopyAssignment());
8079 // printf ("decl->hasTrivialDestructor() = %i\n",
8080 // cxx_record_decl->hasTrivialDestructor());
8081 return cxx_method_decl;
8082}
Greg Claytond8d4a572015-08-11 21:38:15 +00008083
8084#pragma mark C++ Base Classes
8085
8086clang::CXXBaseSpecifier *
Kate Stoneb9c1b512016-09-06 20:57:50 +00008087ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8088 AccessType access, bool is_virtual,
8089 bool base_of_class) {
8090 if (type)
8091 return new clang::CXXBaseSpecifier(
8092 clang::SourceRange(), is_virtual, base_of_class,
8093 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8094 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8095 clang::SourceLocation());
8096 return nullptr;
8097}
8098
8099void ClangASTContext::DeleteBaseClassSpecifiers(
8100 clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes) {
8101 for (unsigned i = 0; i < num_base_classes; ++i) {
8102 delete base_classes[i];
8103 base_classes[i] = nullptr;
8104 }
8105}
8106
8107bool ClangASTContext::SetBaseClassesForClassType(
8108 lldb::opaque_compiler_type_t type,
8109 clang::CXXBaseSpecifier const *const *base_classes,
8110 unsigned num_base_classes) {
8111 if (type) {
8112 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8113 if (cxx_record_decl) {
8114 cxx_record_decl->setBases(base_classes, num_base_classes);
8115 return true;
8116 }
8117 }
8118 return false;
8119}
8120
8121bool ClangASTContext::SetObjCSuperClass(
8122 const CompilerType &type, const CompilerType &superclass_clang_type) {
8123 ClangASTContext *ast =
8124 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8125 if (!ast)
8126 return false;
8127 clang::ASTContext *clang_ast = ast->getASTContext();
8128
8129 if (type && superclass_clang_type.IsValid() &&
8130 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8131 clang::ObjCInterfaceDecl *class_interface_decl =
8132 GetAsObjCInterfaceDecl(type);
8133 clang::ObjCInterfaceDecl *super_interface_decl =
8134 GetAsObjCInterfaceDecl(superclass_clang_type);
8135 if (class_interface_decl && super_interface_decl) {
8136 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8137 clang_ast->getObjCInterfaceType(super_interface_decl)));
8138 return true;
8139 }
8140 }
8141 return false;
8142}
8143
8144bool ClangASTContext::AddObjCClassProperty(
8145 const CompilerType &type, const char *property_name,
8146 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8147 const char *property_setter_name, const char *property_getter_name,
8148 uint32_t property_attributes, ClangASTMetadata *metadata) {
8149 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8150 property_name[0] == '\0')
8151 return false;
8152 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8153 if (!ast)
8154 return false;
8155 clang::ASTContext *clang_ast = ast->getASTContext();
8156
8157 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8158
8159 if (class_interface_decl) {
8160 CompilerType property_clang_type_to_access;
8161
8162 if (property_clang_type.IsValid())
8163 property_clang_type_to_access = property_clang_type;
8164 else if (ivar_decl)
8165 property_clang_type_to_access =
8166 CompilerType(clang_ast, ivar_decl->getType());
8167
8168 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8169 clang::TypeSourceInfo *prop_type_source;
8170 if (ivar_decl)
8171 prop_type_source =
8172 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8173 else
8174 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8175 ClangUtil::GetQualType(property_clang_type));
8176
8177 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8178 *clang_ast, class_interface_decl,
8179 clang::SourceLocation(), // Source Location
8180 &clang_ast->Idents.get(property_name),
8181 clang::SourceLocation(), // Source Location for AT
8182 clang::SourceLocation(), // Source location for (
8183 ivar_decl ? ivar_decl->getType()
8184 : ClangUtil::GetQualType(property_clang_type),
8185 prop_type_source);
8186
8187 if (property_decl) {
8188 if (metadata)
8189 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8190
8191 class_interface_decl->addDecl(property_decl);
8192
8193 clang::Selector setter_sel, getter_sel;
8194
8195 if (property_setter_name != nullptr) {
8196 std::string property_setter_no_colon(
8197 property_setter_name, strlen(property_setter_name) - 1);
8198 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008199 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008200 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8201 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8202 std::string setter_sel_string("set");
8203 setter_sel_string.push_back(::toupper(property_name[0]));
8204 setter_sel_string.append(&property_name[1]);
8205 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008206 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008207 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8208 }
8209 property_decl->setSetterName(setter_sel);
8210 property_decl->setPropertyAttributes(
8211 clang::ObjCPropertyDecl::OBJC_PR_setter);
8212
8213 if (property_getter_name != nullptr) {
8214 clang::IdentifierInfo *getter_ident =
8215 &clang_ast->Idents.get(property_getter_name);
8216 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8217 } else {
8218 clang::IdentifierInfo *getter_ident =
8219 &clang_ast->Idents.get(property_name);
8220 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8221 }
8222 property_decl->setGetterName(getter_sel);
8223 property_decl->setPropertyAttributes(
8224 clang::ObjCPropertyDecl::OBJC_PR_getter);
8225
8226 if (ivar_decl)
8227 property_decl->setPropertyIvarDecl(ivar_decl);
8228
8229 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8230 property_decl->setPropertyAttributes(
8231 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8232 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8233 property_decl->setPropertyAttributes(
8234 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8235 if (property_attributes & DW_APPLE_PROPERTY_assign)
8236 property_decl->setPropertyAttributes(
8237 clang::ObjCPropertyDecl::OBJC_PR_assign);
8238 if (property_attributes & DW_APPLE_PROPERTY_retain)
8239 property_decl->setPropertyAttributes(
8240 clang::ObjCPropertyDecl::OBJC_PR_retain);
8241 if (property_attributes & DW_APPLE_PROPERTY_copy)
8242 property_decl->setPropertyAttributes(
8243 clang::ObjCPropertyDecl::OBJC_PR_copy);
8244 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8245 property_decl->setPropertyAttributes(
8246 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8247 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8248 property_decl->setPropertyAttributes(
8249 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8250 if (property_attributes &
8251 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8252 property_decl->setPropertyAttributes(
8253 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8254 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8255 property_decl->setPropertyAttributes(
8256 clang::ObjCPropertyDecl::OBJC_PR_class);
8257
8258 const bool isInstance =
8259 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8260
8261 if (!getter_sel.isNull() &&
8262 !(isInstance
8263 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8264 : class_interface_decl->lookupClassMethod(getter_sel))) {
8265 const bool isVariadic = false;
8266 const bool isSynthesized = false;
8267 const bool isImplicitlyDeclared = true;
8268 const bool isDefined = false;
8269 const clang::ObjCMethodDecl::ImplementationControl impControl =
8270 clang::ObjCMethodDecl::None;
8271 const bool HasRelatedResultType = false;
8272
8273 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8274 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8275 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8276 nullptr, class_interface_decl, isInstance, isVariadic,
8277 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8278 HasRelatedResultType);
8279
8280 if (getter && metadata)
8281 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8282
8283 if (getter) {
8284 getter->setMethodParams(*clang_ast,
8285 llvm::ArrayRef<clang::ParmVarDecl *>(),
8286 llvm::ArrayRef<clang::SourceLocation>());
8287
8288 class_interface_decl->addDecl(getter);
8289 }
8290 }
8291
8292 if (!setter_sel.isNull() &&
8293 !(isInstance
8294 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8295 : class_interface_decl->lookupClassMethod(setter_sel))) {
8296 clang::QualType result_type = clang_ast->VoidTy;
8297 const bool isVariadic = false;
8298 const bool isSynthesized = false;
8299 const bool isImplicitlyDeclared = true;
8300 const bool isDefined = false;
8301 const clang::ObjCMethodDecl::ImplementationControl impControl =
8302 clang::ObjCMethodDecl::None;
8303 const bool HasRelatedResultType = false;
8304
8305 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8306 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8307 setter_sel, result_type, nullptr, class_interface_decl,
8308 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8309 isDefined, impControl, HasRelatedResultType);
8310
8311 if (setter && metadata)
8312 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8313
8314 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8315
8316 params.push_back(clang::ParmVarDecl::Create(
8317 *clang_ast, setter, clang::SourceLocation(),
8318 clang::SourceLocation(),
8319 nullptr, // anonymous
8320 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8321 clang::SC_Auto, nullptr));
8322
8323 if (setter) {
8324 setter->setMethodParams(
8325 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8326 llvm::ArrayRef<clang::SourceLocation>());
8327
8328 class_interface_decl->addDecl(setter);
8329 }
8330 }
8331
8332 return true;
8333 }
8334 }
8335 }
8336 return false;
8337}
8338
8339bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8340 bool check_superclass) {
8341 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8342 if (class_interface_decl)
8343 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8344 return false;
8345}
8346
8347clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8348 const CompilerType &type,
8349 const char *name, // the full symbol name as seen in the symbol table
8350 // (lldb::opaque_compiler_type_t type, "-[NString
8351 // stringWithCString:]")
8352 const CompilerType &method_clang_type, lldb::AccessType access,
8353 bool is_artificial, bool is_variadic) {
8354 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008355 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008356
Kate Stoneb9c1b512016-09-06 20:57:50 +00008357 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8358
8359 if (class_interface_decl == nullptr)
8360 return nullptr;
8361 ClangASTContext *lldb_ast =
8362 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8363 if (lldb_ast == nullptr)
8364 return nullptr;
8365 clang::ASTContext *ast = lldb_ast->getASTContext();
8366
8367 const char *selector_start = ::strchr(name, ' ');
8368 if (selector_start == nullptr)
8369 return nullptr;
8370
8371 selector_start++;
8372 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8373
8374 size_t len = 0;
8375 const char *start;
8376 // printf ("name = '%s'\n", name);
8377
8378 unsigned num_selectors_with_args = 0;
8379 for (start = selector_start; start && *start != '\0' && *start != ']';
8380 start += len) {
8381 len = ::strcspn(start, ":]");
8382 bool has_arg = (start[len] == ':');
8383 if (has_arg)
8384 ++num_selectors_with_args;
8385 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8386 if (has_arg)
8387 len += 1;
8388 }
8389
8390 if (selector_idents.size() == 0)
8391 return nullptr;
8392
8393 clang::Selector method_selector = ast->Selectors.getSelector(
8394 num_selectors_with_args ? selector_idents.size() : 0,
8395 selector_idents.data());
8396
8397 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8398
8399 // Populate the method decl with parameter decls
8400 const clang::Type *method_type(method_qual_type.getTypePtr());
8401
8402 if (method_type == nullptr)
8403 return nullptr;
8404
8405 const clang::FunctionProtoType *method_function_prototype(
8406 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8407
8408 if (!method_function_prototype)
8409 return nullptr;
8410
8411 bool is_synthesized = false;
8412 bool is_defined = false;
8413 clang::ObjCMethodDecl::ImplementationControl imp_control =
8414 clang::ObjCMethodDecl::None;
8415
8416 const unsigned num_args = method_function_prototype->getNumParams();
8417
8418 if (num_args != num_selectors_with_args)
8419 return nullptr; // some debug information is corrupt. We are not going to
8420 // deal with it.
8421
8422 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8423 *ast,
8424 clang::SourceLocation(), // beginLoc,
8425 clang::SourceLocation(), // endLoc,
8426 method_selector, method_function_prototype->getReturnType(),
8427 nullptr, // TypeSourceInfo *ResultTInfo,
8428 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8429 ClangUtil::GetQualType(type)),
8430 name[0] == '-', is_variadic, is_synthesized,
8431 true, // is_implicitly_declared; we force this to true because we don't
8432 // have source locations
8433 is_defined, imp_control, false /*has_related_result_type*/);
8434
8435 if (objc_method_decl == nullptr)
8436 return nullptr;
8437
8438 if (num_args > 0) {
8439 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8440
8441 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8442 params.push_back(clang::ParmVarDecl::Create(
8443 *ast, objc_method_decl, clang::SourceLocation(),
8444 clang::SourceLocation(),
8445 nullptr, // anonymous
8446 method_function_prototype->getParamType(param_index), nullptr,
8447 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008448 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008449
Kate Stoneb9c1b512016-09-06 20:57:50 +00008450 objc_method_decl->setMethodParams(
8451 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8452 llvm::ArrayRef<clang::SourceLocation>());
8453 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008454
Kate Stoneb9c1b512016-09-06 20:57:50 +00008455 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008456
Greg Claytond8d4a572015-08-11 21:38:15 +00008457#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008458 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008459#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008460
8461 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008462}
8463
Kate Stoneb9c1b512016-09-06 20:57:50 +00008464bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8465 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008466 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008467
Kate Stoneb9c1b512016-09-06 20:57:50 +00008468 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008469
Kate Stoneb9c1b512016-09-06 20:57:50 +00008470 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8471 switch (type_class) {
8472 case clang::Type::Record: {
8473 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8474 if (cxx_record_decl)
8475 return cxx_record_decl->hasExternalLexicalStorage() ||
8476 cxx_record_decl->hasExternalVisibleStorage();
8477 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008478
Kate Stoneb9c1b512016-09-06 20:57:50 +00008479 case clang::Type::Enum: {
8480 clang::EnumDecl *enum_decl =
8481 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8482 if (enum_decl)
8483 return enum_decl->hasExternalLexicalStorage() ||
8484 enum_decl->hasExternalVisibleStorage();
8485 } break;
8486
8487 case clang::Type::ObjCObject:
8488 case clang::Type::ObjCInterface: {
8489 const clang::ObjCObjectType *objc_class_type =
8490 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8491 assert(objc_class_type);
8492 if (objc_class_type) {
8493 clang::ObjCInterfaceDecl *class_interface_decl =
8494 objc_class_type->getInterface();
8495
8496 if (class_interface_decl)
8497 return class_interface_decl->hasExternalLexicalStorage() ||
8498 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008499 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008500 } break;
8501
8502 case clang::Type::Typedef:
8503 return GetHasExternalStorage(CompilerType(
8504 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8505 ->getDecl()
8506 ->getUnderlyingType()
8507 .getAsOpaquePtr()));
8508
8509 case clang::Type::Auto:
8510 return GetHasExternalStorage(CompilerType(
8511 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8512 ->getDeducedType()
8513 .getAsOpaquePtr()));
8514
8515 case clang::Type::Elaborated:
8516 return GetHasExternalStorage(CompilerType(
8517 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8518 ->getNamedType()
8519 .getAsOpaquePtr()));
8520
8521 case clang::Type::Paren:
8522 return GetHasExternalStorage(CompilerType(
8523 type.GetTypeSystem(),
8524 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8525
8526 default:
8527 break;
8528 }
8529 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008530}
8531
Kate Stoneb9c1b512016-09-06 20:57:50 +00008532bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8533 bool has_extern) {
8534 if (!type)
8535 return false;
8536
8537 clang::QualType qual_type(GetCanonicalQualType(type));
8538
8539 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8540 switch (type_class) {
8541 case clang::Type::Record: {
8542 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8543 if (cxx_record_decl) {
8544 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8545 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8546 return true;
8547 }
8548 } break;
8549
8550 case clang::Type::Enum: {
8551 clang::EnumDecl *enum_decl =
8552 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8553 if (enum_decl) {
8554 enum_decl->setHasExternalLexicalStorage(has_extern);
8555 enum_decl->setHasExternalVisibleStorage(has_extern);
8556 return true;
8557 }
8558 } break;
8559
8560 case clang::Type::ObjCObject:
8561 case clang::Type::ObjCInterface: {
8562 const clang::ObjCObjectType *objc_class_type =
8563 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8564 assert(objc_class_type);
8565 if (objc_class_type) {
8566 clang::ObjCInterfaceDecl *class_interface_decl =
8567 objc_class_type->getInterface();
8568
8569 if (class_interface_decl) {
8570 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8571 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8572 return true;
8573 }
8574 }
8575 } break;
8576
8577 case clang::Type::Typedef:
8578 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8579 ->getDecl()
8580 ->getUnderlyingType()
8581 .getAsOpaquePtr(),
8582 has_extern);
8583
8584 case clang::Type::Auto:
8585 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8586 ->getDeducedType()
8587 .getAsOpaquePtr(),
8588 has_extern);
8589
8590 case clang::Type::Elaborated:
8591 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8592 ->getNamedType()
8593 .getAsOpaquePtr(),
8594 has_extern);
8595
8596 case clang::Type::Paren:
8597 return SetHasExternalStorage(
8598 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8599 has_extern);
8600
8601 default:
8602 break;
8603 }
8604 return false;
8605}
Greg Claytond8d4a572015-08-11 21:38:15 +00008606
8607#pragma mark TagDecl
8608
Kate Stoneb9c1b512016-09-06 20:57:50 +00008609bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8610 clang::QualType qual_type(ClangUtil::GetQualType(type));
8611 if (!qual_type.isNull()) {
8612 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8613 if (tag_type) {
8614 clang::TagDecl *tag_decl = tag_type->getDecl();
8615 if (tag_decl) {
8616 tag_decl->startDefinition();
8617 return true;
8618 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008619 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008620
8621 const clang::ObjCObjectType *object_type =
8622 qual_type->getAs<clang::ObjCObjectType>();
8623 if (object_type) {
8624 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8625 if (interface_decl) {
8626 interface_decl->startDefinition();
8627 return true;
8628 }
8629 }
8630 }
8631 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008632}
8633
Kate Stoneb9c1b512016-09-06 20:57:50 +00008634bool ClangASTContext::CompleteTagDeclarationDefinition(
8635 const CompilerType &type) {
8636 clang::QualType qual_type(ClangUtil::GetQualType(type));
8637 if (!qual_type.isNull()) {
8638 // Make sure we use the same methodology as
8639 // ClangASTContext::StartTagDeclarationDefinition()
8640 // as to how we start/end the definition. Previously we were calling
8641 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8642 if (tag_type) {
8643 clang::TagDecl *tag_decl = tag_type->getDecl();
8644 if (tag_decl) {
8645 clang::CXXRecordDecl *cxx_record_decl =
8646 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8647
8648 if (cxx_record_decl) {
8649 if (!cxx_record_decl->isCompleteDefinition())
8650 cxx_record_decl->completeDefinition();
8651 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8652 cxx_record_decl->setHasExternalLexicalStorage(false);
8653 cxx_record_decl->setHasExternalVisibleStorage(false);
8654 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008655 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008656 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008657 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008658
8659 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8660
8661 if (enutype) {
8662 clang::EnumDecl *enum_decl = enutype->getDecl();
8663
8664 if (enum_decl) {
8665 if (!enum_decl->isCompleteDefinition()) {
8666 ClangASTContext *lldb_ast =
8667 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8668 if (lldb_ast == nullptr)
8669 return false;
8670 clang::ASTContext *ast = lldb_ast->getASTContext();
8671
8672 /// TODO This really needs to be fixed.
8673
8674 QualType integer_type(enum_decl->getIntegerType());
8675 if (!integer_type.isNull()) {
8676 unsigned NumPositiveBits = 1;
8677 unsigned NumNegativeBits = 0;
8678
8679 clang::QualType promotion_qual_type;
8680 // If the enum integer type is less than an integer in bit width,
8681 // then we must promote it to an integer size.
8682 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8683 ast->getTypeSize(ast->IntTy)) {
8684 if (enum_decl->getIntegerType()->isSignedIntegerType())
8685 promotion_qual_type = ast->IntTy;
8686 else
8687 promotion_qual_type = ast->UnsignedIntTy;
8688 } else
8689 promotion_qual_type = enum_decl->getIntegerType();
8690
8691 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8692 promotion_qual_type, NumPositiveBits,
8693 NumNegativeBits);
8694 }
8695 }
8696 return true;
8697 }
8698 }
8699 }
8700 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008701}
8702
Kate Stoneb9c1b512016-09-06 20:57:50 +00008703bool ClangASTContext::AddEnumerationValueToEnumerationType(
8704 lldb::opaque_compiler_type_t type,
8705 const CompilerType &enumerator_clang_type, const Declaration &decl,
8706 const char *name, int64_t enum_value, uint32_t enum_value_bit_size) {
8707 if (type && enumerator_clang_type.IsValid() && name && name[0]) {
8708 clang::QualType enum_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008709
Kate Stoneb9c1b512016-09-06 20:57:50 +00008710 bool is_signed = false;
8711 enumerator_clang_type.IsIntegerType(is_signed);
Greg Claytond8d4a572015-08-11 21:38:15 +00008712 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Kate Stoneb9c1b512016-09-06 20:57:50 +00008713 if (clang_type) {
8714 const clang::EnumType *enutype =
8715 llvm::dyn_cast<clang::EnumType>(clang_type);
8716
8717 if (enutype) {
8718 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8719 enum_llvm_apsint = enum_value;
8720 clang::EnumConstantDecl *enumerator_decl =
8721 clang::EnumConstantDecl::Create(
8722 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8723 name ? &getASTContext()->Idents.get(name)
8724 : nullptr, // Identifier
8725 ClangUtil::GetQualType(enumerator_clang_type),
8726 nullptr, enum_llvm_apsint);
8727
8728 if (enumerator_decl) {
8729 enutype->getDecl()->addDecl(enumerator_decl);
8730
8731#ifdef LLDB_CONFIGURATION_DEBUG
8732 VerifyDecl(enumerator_decl);
8733#endif
8734
8735 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008736 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008737 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008738 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008739 }
8740 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008741}
8742
Greg Claytona1e5dc82015-08-11 22:53:00 +00008743CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00008744ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8745 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8746 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8747 if (clang_type) {
8748 const clang::EnumType *enutype =
8749 llvm::dyn_cast<clang::EnumType>(clang_type);
8750 if (enutype) {
8751 clang::EnumDecl *enum_decl = enutype->getDecl();
8752 if (enum_decl)
8753 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008754 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008755 }
8756 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008757}
8758
Kate Stoneb9c1b512016-09-06 20:57:50 +00008759CompilerType
8760ClangASTContext::CreateMemberPointerType(const CompilerType &type,
8761 const CompilerType &pointee_type) {
8762 if (type && pointee_type.IsValid() &&
8763 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8764 ClangASTContext *ast =
8765 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8766 if (!ast)
8767 return CompilerType();
8768 return CompilerType(ast->getASTContext(),
8769 ast->getASTContext()->getMemberPointerType(
8770 ClangUtil::GetQualType(pointee_type),
8771 ClangUtil::GetQualType(type).getTypePtr()));
8772 }
8773 return CompilerType();
8774}
Greg Claytond8d4a572015-08-11 21:38:15 +00008775
8776size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00008777ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
8778 const char *s, uint8_t *dst,
8779 size_t dst_size) {
8780 if (type) {
8781 clang::QualType qual_type(GetCanonicalQualType(type));
8782 uint32_t count = 0;
8783 bool is_complex = false;
8784 if (IsFloatingPointType(type, count, is_complex)) {
8785 // TODO: handle complex and vector types
8786 if (count != 1)
8787 return false;
8788
8789 llvm::StringRef s_sref(s);
8790 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
8791 s_sref);
8792
8793 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
8794 const uint64_t byte_size = bit_size / 8;
8795 if (dst_size >= byte_size) {
8796 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
8797 llvm::NextPowerOf2(byte_size) * 8);
Zachary Turner97206d52017-05-12 04:51:55 +00008798 lldb_private::Status get_data_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008799 if (scalar.GetAsMemoryData(dst, byte_size,
8800 lldb_private::endian::InlHostByteOrder(),
8801 get_data_error))
8802 return byte_size;
8803 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008804 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008805 }
8806 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00008807}
8808
Greg Claytond8d4a572015-08-11 21:38:15 +00008809//----------------------------------------------------------------------
8810// Dumping types
8811//----------------------------------------------------------------------
8812#define DEPTH_INCREMENT 2
8813
Kate Stoneb9c1b512016-09-06 20:57:50 +00008814void ClangASTContext::DumpValue(
8815 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00008816 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008817 lldb::offset_t data_byte_offset, size_t data_byte_size,
8818 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
8819 bool show_summary, bool verbose, uint32_t depth) {
8820 if (!type)
8821 return;
8822
8823 clang::QualType qual_type(GetQualType(type));
8824 switch (qual_type->getTypeClass()) {
8825 case clang::Type::Record:
8826 if (GetCompleteType(type)) {
8827 const clang::RecordType *record_type =
8828 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8829 const clang::RecordDecl *record_decl = record_type->getDecl();
8830 assert(record_decl);
8831 uint32_t field_bit_offset = 0;
8832 uint32_t field_byte_offset = 0;
8833 const clang::ASTRecordLayout &record_layout =
8834 getASTContext()->getASTRecordLayout(record_decl);
8835 uint32_t child_idx = 0;
8836
8837 const clang::CXXRecordDecl *cxx_record_decl =
8838 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8839 if (cxx_record_decl) {
8840 // We might have base classes to print out first
8841 clang::CXXRecordDecl::base_class_const_iterator base_class,
8842 base_class_end;
8843 for (base_class = cxx_record_decl->bases_begin(),
8844 base_class_end = cxx_record_decl->bases_end();
8845 base_class != base_class_end; ++base_class) {
8846 const clang::CXXRecordDecl *base_class_decl =
8847 llvm::cast<clang::CXXRecordDecl>(
8848 base_class->getType()->getAs<clang::RecordType>()->getDecl());
8849
8850 // Skip empty base classes
8851 if (verbose == false &&
8852 ClangASTContext::RecordHasFields(base_class_decl) == false)
8853 continue;
8854
8855 if (base_class->isVirtual())
8856 field_bit_offset =
8857 record_layout.getVBaseClassOffset(base_class_decl)
8858 .getQuantity() *
8859 8;
8860 else
8861 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
8862 .getQuantity() *
8863 8;
8864 field_byte_offset = field_bit_offset / 8;
8865 assert(field_bit_offset % 8 == 0);
8866 if (child_idx == 0)
8867 s->PutChar('{');
8868 else
8869 s->PutChar(',');
8870
8871 clang::QualType base_class_qual_type = base_class->getType();
8872 std::string base_class_type_name(base_class_qual_type.getAsString());
8873
8874 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00008875 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
8876 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008877
8878 clang::TypeInfo base_class_type_info =
8879 getASTContext()->getTypeInfo(base_class_qual_type);
8880
8881 // Dump the value of the member
8882 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
8883 base_clang_type.DumpValue(
8884 exe_ctx,
8885 s, // Stream to dump to
8886 base_clang_type
8887 .GetFormat(), // The format with which to display the member
8888 data, // Data buffer containing all bytes for this type
8889 data_byte_offset + field_byte_offset, // Offset into "data" where
8890 // to grab value from
8891 base_class_type_info.Width / 8, // Size of this type in bytes
8892 0, // Bitfield bit size
8893 0, // Bitfield bit offset
8894 show_types, // Boolean indicating if we should show the variable
8895 // types
8896 show_summary, // Boolean indicating if we should show a summary
8897 // for the current type
8898 verbose, // Verbose output?
8899 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8900 // children
8901
8902 ++child_idx;
8903 }
8904 }
8905 uint32_t field_idx = 0;
8906 clang::RecordDecl::field_iterator field, field_end;
8907 for (field = record_decl->field_begin(),
8908 field_end = record_decl->field_end();
8909 field != field_end; ++field, ++field_idx, ++child_idx) {
8910 // Print the starting squiggly bracket (if this is the
8911 // first member) or comma (for member 2 and beyond) for
8912 // the struct/union/class member.
8913 if (child_idx == 0)
8914 s->PutChar('{');
8915 else
8916 s->PutChar(',');
8917
8918 // Indent
8919 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8920
8921 clang::QualType field_type = field->getType();
8922 // Print the member type if requested
8923 // Figure out the type byte size (field_type_info.first) and
8924 // alignment (field_type_info.second) from the AST context.
8925 clang::TypeInfo field_type_info =
8926 getASTContext()->getTypeInfo(field_type);
8927 assert(field_idx < record_layout.getFieldCount());
8928 // Figure out the field offset within the current struct/union/class
8929 // type
8930 field_bit_offset = record_layout.getFieldOffset(field_idx);
8931 field_byte_offset = field_bit_offset / 8;
8932 uint32_t field_bitfield_bit_size = 0;
8933 uint32_t field_bitfield_bit_offset = 0;
8934 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
8935 field_bitfield_bit_size))
8936 field_bitfield_bit_offset = field_bit_offset % 8;
8937
8938 if (show_types) {
8939 std::string field_type_name(field_type.getAsString());
8940 if (field_bitfield_bit_size > 0)
8941 s->Printf("(%s:%u) ", field_type_name.c_str(),
8942 field_bitfield_bit_size);
8943 else
8944 s->Printf("(%s) ", field_type_name.c_str());
8945 }
8946 // Print the member name and equal sign
8947 s->Printf("%s = ", field->getNameAsString().c_str());
8948
8949 // Dump the value of the member
8950 CompilerType field_clang_type(getASTContext(), field_type);
8951 field_clang_type.DumpValue(
8952 exe_ctx,
8953 s, // Stream to dump to
8954 field_clang_type
8955 .GetFormat(), // The format with which to display the member
8956 data, // Data buffer containing all bytes for this type
8957 data_byte_offset + field_byte_offset, // Offset into "data" where to
8958 // grab value from
8959 field_type_info.Width / 8, // Size of this type in bytes
8960 field_bitfield_bit_size, // Bitfield bit size
8961 field_bitfield_bit_offset, // Bitfield bit offset
8962 show_types, // Boolean indicating if we should show the variable
8963 // types
8964 show_summary, // Boolean indicating if we should show a summary for
8965 // the current type
8966 verbose, // Verbose output?
8967 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8968 // children
8969 }
8970
8971 // Indent the trailing squiggly bracket
8972 if (child_idx > 0)
8973 s->Printf("\n%*s}", depth, "");
8974 }
8975 return;
8976
8977 case clang::Type::Enum:
8978 if (GetCompleteType(type)) {
8979 const clang::EnumType *enutype =
8980 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8981 const clang::EnumDecl *enum_decl = enutype->getDecl();
8982 assert(enum_decl);
8983 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8984 lldb::offset_t offset = data_byte_offset;
8985 const int64_t enum_value = data.GetMaxU64Bitfield(
8986 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
8987 for (enum_pos = enum_decl->enumerator_begin(),
8988 enum_end_pos = enum_decl->enumerator_end();
8989 enum_pos != enum_end_pos; ++enum_pos) {
8990 if (enum_pos->getInitVal() == enum_value) {
8991 s->Printf("%s", enum_pos->getNameAsString().c_str());
8992 return;
8993 }
8994 }
8995 // If we have gotten here we didn't get find the enumerator in the
8996 // enum decl, so just print the integer.
8997 s->Printf("%" PRIi64, enum_value);
8998 }
8999 return;
9000
9001 case clang::Type::ConstantArray: {
9002 const clang::ConstantArrayType *array =
9003 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9004 bool is_array_of_characters = false;
9005 clang::QualType element_qual_type = array->getElementType();
9006
9007 const clang::Type *canonical_type =
9008 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9009 if (canonical_type)
9010 is_array_of_characters = canonical_type->isCharType();
9011
9012 const uint64_t element_count = array->getSize().getLimitedValue();
9013
9014 clang::TypeInfo field_type_info =
9015 getASTContext()->getTypeInfo(element_qual_type);
9016
9017 uint32_t element_idx = 0;
9018 uint32_t element_offset = 0;
9019 uint64_t element_byte_size = field_type_info.Width / 8;
9020 uint32_t element_stride = element_byte_size;
9021
9022 if (is_array_of_characters) {
9023 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009024 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9025 element_byte_size, element_count, UINT32_MAX,
9026 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009027 s->PutChar('"');
9028 return;
9029 } else {
9030 CompilerType element_clang_type(getASTContext(), element_qual_type);
9031 lldb::Format element_format = element_clang_type.GetFormat();
9032
9033 for (element_idx = 0; element_idx < element_count; ++element_idx) {
9034 // Print the starting squiggly bracket (if this is the
9035 // first member) or comman (for member 2 and beyong) for
9036 // the struct/union/class member.
9037 if (element_idx == 0)
9038 s->PutChar('{');
9039 else
9040 s->PutChar(',');
9041
9042 // Indent and print the index
9043 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9044
9045 // Figure out the field offset within the current struct/union/class
9046 // type
9047 element_offset = element_idx * element_stride;
9048
9049 // Dump the value of the member
9050 element_clang_type.DumpValue(
9051 exe_ctx,
9052 s, // Stream to dump to
9053 element_format, // The format with which to display the element
9054 data, // Data buffer containing all bytes for this type
9055 data_byte_offset +
9056 element_offset, // Offset into "data" where to grab value from
9057 element_byte_size, // Size of this type in bytes
9058 0, // Bitfield bit size
9059 0, // Bitfield bit offset
9060 show_types, // Boolean indicating if we should show the variable
9061 // types
9062 show_summary, // Boolean indicating if we should show a summary for
9063 // the current type
9064 verbose, // Verbose output?
9065 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9066 // children
9067 }
9068
9069 // Indent the trailing squiggly bracket
9070 if (element_idx > 0)
9071 s->Printf("\n%*s}", depth, "");
9072 }
9073 }
9074 return;
9075
9076 case clang::Type::Typedef: {
9077 clang::QualType typedef_qual_type =
9078 llvm::cast<clang::TypedefType>(qual_type)
9079 ->getDecl()
9080 ->getUnderlyingType();
9081
9082 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9083 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9084 clang::TypeInfo typedef_type_info =
9085 getASTContext()->getTypeInfo(typedef_qual_type);
9086 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9087
9088 return typedef_clang_type.DumpValue(
9089 exe_ctx,
9090 s, // Stream to dump to
9091 typedef_format, // The format with which to display the element
9092 data, // Data buffer containing all bytes for this type
9093 data_byte_offset, // Offset into "data" where to grab value from
9094 typedef_byte_size, // Size of this type in bytes
9095 bitfield_bit_size, // Bitfield bit size
9096 bitfield_bit_offset, // Bitfield bit offset
9097 show_types, // Boolean indicating if we should show the variable types
9098 show_summary, // Boolean indicating if we should show a summary for the
9099 // current type
9100 verbose, // Verbose output?
9101 depth); // Scope depth for any types that have children
9102 } break;
9103
9104 case clang::Type::Auto: {
9105 clang::QualType elaborated_qual_type =
9106 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9107 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9108 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9109 clang::TypeInfo elaborated_type_info =
9110 getASTContext()->getTypeInfo(elaborated_qual_type);
9111 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9112
9113 return elaborated_clang_type.DumpValue(
9114 exe_ctx,
9115 s, // Stream to dump to
9116 elaborated_format, // The format with which to display the element
9117 data, // Data buffer containing all bytes for this type
9118 data_byte_offset, // Offset into "data" where to grab value from
9119 elaborated_byte_size, // Size of this type in bytes
9120 bitfield_bit_size, // Bitfield bit size
9121 bitfield_bit_offset, // Bitfield bit offset
9122 show_types, // Boolean indicating if we should show the variable types
9123 show_summary, // Boolean indicating if we should show a summary for the
9124 // current type
9125 verbose, // Verbose output?
9126 depth); // Scope depth for any types that have children
9127 } break;
9128
9129 case clang::Type::Elaborated: {
9130 clang::QualType elaborated_qual_type =
9131 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9132 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9133 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9134 clang::TypeInfo elaborated_type_info =
9135 getASTContext()->getTypeInfo(elaborated_qual_type);
9136 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9137
9138 return elaborated_clang_type.DumpValue(
9139 exe_ctx,
9140 s, // Stream to dump to
9141 elaborated_format, // The format with which to display the element
9142 data, // Data buffer containing all bytes for this type
9143 data_byte_offset, // Offset into "data" where to grab value from
9144 elaborated_byte_size, // Size of this type in bytes
9145 bitfield_bit_size, // Bitfield bit size
9146 bitfield_bit_offset, // Bitfield bit offset
9147 show_types, // Boolean indicating if we should show the variable types
9148 show_summary, // Boolean indicating if we should show a summary for the
9149 // current type
9150 verbose, // Verbose output?
9151 depth); // Scope depth for any types that have children
9152 } break;
9153
9154 case clang::Type::Paren: {
9155 clang::QualType desugar_qual_type =
9156 llvm::cast<clang::ParenType>(qual_type)->desugar();
9157 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9158
9159 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9160 clang::TypeInfo desugar_type_info =
9161 getASTContext()->getTypeInfo(desugar_qual_type);
9162 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9163
9164 return desugar_clang_type.DumpValue(
9165 exe_ctx,
9166 s, // Stream to dump to
9167 desugar_format, // The format with which to display the element
9168 data, // Data buffer containing all bytes for this type
9169 data_byte_offset, // Offset into "data" where to grab value from
9170 desugar_byte_size, // Size of this type in bytes
9171 bitfield_bit_size, // Bitfield bit size
9172 bitfield_bit_offset, // Bitfield bit offset
9173 show_types, // Boolean indicating if we should show the variable types
9174 show_summary, // Boolean indicating if we should show a summary for the
9175 // current type
9176 verbose, // Verbose output?
9177 depth); // Scope depth for any types that have children
9178 } break;
9179
9180 default:
9181 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009182 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9183 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9184 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009185
9186 if (show_summary)
9187 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9188 break;
9189 }
9190}
9191
9192bool ClangASTContext::DumpTypeValue(
9193 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009194 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9195 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009196 ExecutionContextScope *exe_scope) {
9197 if (!type)
9198 return false;
9199 if (IsAggregateType(type)) {
9200 return false;
9201 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009202 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009203
9204 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9205 switch (type_class) {
9206 case clang::Type::Typedef: {
9207 clang::QualType typedef_qual_type =
9208 llvm::cast<clang::TypedefType>(qual_type)
9209 ->getDecl()
9210 ->getUnderlyingType();
9211 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9212 if (format == eFormatDefault)
9213 format = typedef_clang_type.GetFormat();
9214 clang::TypeInfo typedef_type_info =
9215 getASTContext()->getTypeInfo(typedef_qual_type);
9216 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9217
9218 return typedef_clang_type.DumpTypeValue(
9219 s,
9220 format, // The format with which to display the element
9221 data, // Data buffer containing all bytes for this type
9222 byte_offset, // Offset into "data" where to grab value from
9223 typedef_byte_size, // Size of this type in bytes
9224 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9225 // treat as a bitfield
9226 bitfield_bit_offset, // Offset in bits of a bitfield value if
9227 // bitfield_bit_size != 0
9228 exe_scope);
9229 } break;
9230
9231 case clang::Type::Enum:
9232 // If our format is enum or default, show the enumeration value as
9233 // its enumeration string value, else just display it as requested.
9234 if ((format == eFormatEnum || format == eFormatDefault) &&
9235 GetCompleteType(type)) {
9236 const clang::EnumType *enutype =
9237 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9238 const clang::EnumDecl *enum_decl = enutype->getDecl();
9239 assert(enum_decl);
9240 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9241 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9242 lldb::offset_t offset = byte_offset;
9243 if (is_signed) {
9244 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9245 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9246 for (enum_pos = enum_decl->enumerator_begin(),
9247 enum_end_pos = enum_decl->enumerator_end();
9248 enum_pos != enum_end_pos; ++enum_pos) {
9249 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009250 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009251 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009252 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009253 }
9254 // If we have gotten here we didn't get find the enumerator in the
9255 // enum decl, so just print the integer.
9256 s->Printf("%" PRIi64, enum_svalue);
9257 } else {
9258 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9259 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9260 for (enum_pos = enum_decl->enumerator_begin(),
9261 enum_end_pos = enum_decl->enumerator_end();
9262 enum_pos != enum_end_pos; ++enum_pos) {
9263 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009264 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009265 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009266 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009267 }
9268 // If we have gotten here we didn't get find the enumerator in the
9269 // enum decl, so just print the integer.
9270 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009271 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009272 return true;
9273 }
9274 // format was not enum, just fall through and dump the value as
9275 // requested....
9276 LLVM_FALLTHROUGH;
9277
9278 default:
9279 // We are down to a scalar type that we just need to display.
9280 {
9281 uint32_t item_count = 1;
9282 // A few formats, we might need to modify our size and count for
9283 // depending
9284 // on how we are trying to display the value...
9285 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009286 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009287 case eFormatBoolean:
9288 case eFormatBinary:
9289 case eFormatComplex:
9290 case eFormatCString: // NULL terminated C strings
9291 case eFormatDecimal:
9292 case eFormatEnum:
9293 case eFormatHex:
9294 case eFormatHexUppercase:
9295 case eFormatFloat:
9296 case eFormatOctal:
9297 case eFormatOSType:
9298 case eFormatUnsigned:
9299 case eFormatPointer:
9300 case eFormatVectorOfChar:
9301 case eFormatVectorOfSInt8:
9302 case eFormatVectorOfUInt8:
9303 case eFormatVectorOfSInt16:
9304 case eFormatVectorOfUInt16:
9305 case eFormatVectorOfSInt32:
9306 case eFormatVectorOfUInt32:
9307 case eFormatVectorOfSInt64:
9308 case eFormatVectorOfUInt64:
9309 case eFormatVectorOfFloat32:
9310 case eFormatVectorOfFloat64:
9311 case eFormatVectorOfUInt128:
9312 break;
9313
9314 case eFormatChar:
9315 case eFormatCharPrintable:
9316 case eFormatCharArray:
9317 case eFormatBytes:
9318 case eFormatBytesWithASCII:
9319 item_count = byte_size;
9320 byte_size = 1;
9321 break;
9322
9323 case eFormatUnicode16:
9324 item_count = byte_size / 2;
9325 byte_size = 2;
9326 break;
9327
9328 case eFormatUnicode32:
9329 item_count = byte_size / 4;
9330 byte_size = 4;
9331 break;
9332 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009333 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9334 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9335 bitfield_bit_size, bitfield_bit_offset,
9336 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009337 }
9338 break;
9339 }
9340 }
9341 return 0;
9342}
9343
9344void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9345 ExecutionContext *exe_ctx, Stream *s,
9346 const lldb_private::DataExtractor &data,
9347 lldb::offset_t data_byte_offset,
9348 size_t data_byte_size) {
9349 uint32_t length = 0;
9350 if (IsCStringType(type, length)) {
9351 if (exe_ctx) {
9352 Process *process = exe_ctx->GetProcessPtr();
9353 if (process) {
9354 lldb::offset_t offset = data_byte_offset;
9355 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9356 std::vector<uint8_t> buf;
9357 if (length > 0)
9358 buf.resize(length);
9359 else
9360 buf.resize(256);
9361
Zachary Turner29cb8682017-03-03 20:57:05 +00009362 DataExtractor cstr_data(&buf.front(), buf.size(),
9363 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009364 buf.back() = '\0';
9365 size_t bytes_read;
9366 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009367 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009368 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9369 buf.size(), error)) > 0) {
9370 const size_t len = strlen((const char *)&buf.front());
9371 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009372 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009373 if (total_cstr_len == 0)
9374 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009375 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9376 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009377 total_cstr_len += len;
9378 if (len < buf.size())
9379 break;
9380 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009381 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009382 if (total_cstr_len > 0)
9383 s->PutChar('"');
9384 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009385 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009386 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009387}
9388
Kate Stoneb9c1b512016-09-06 20:57:50 +00009389void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9390 StreamFile s(stdout, false);
9391 DumpTypeDescription(type, &s);
9392 ClangASTMetadata *metadata =
9393 ClangASTContext::GetMetadata(getASTContext(), type);
9394 if (metadata) {
9395 metadata->Dump(&s);
9396 }
9397}
Greg Claytond8d4a572015-08-11 21:38:15 +00009398
Kate Stoneb9c1b512016-09-06 20:57:50 +00009399void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9400 Stream *s) {
9401 if (type) {
9402 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009403
Kate Stoneb9c1b512016-09-06 20:57:50 +00009404 llvm::SmallVector<char, 1024> buf;
9405 llvm::raw_svector_ostream llvm_ostrm(buf);
9406
9407 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9408 switch (type_class) {
9409 case clang::Type::ObjCObject:
9410 case clang::Type::ObjCInterface: {
9411 GetCompleteType(type);
9412
9413 const clang::ObjCObjectType *objc_class_type =
9414 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9415 assert(objc_class_type);
9416 if (objc_class_type) {
9417 clang::ObjCInterfaceDecl *class_interface_decl =
9418 objc_class_type->getInterface();
9419 if (class_interface_decl) {
9420 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9421 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009422 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009423 }
9424 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009425
Kate Stoneb9c1b512016-09-06 20:57:50 +00009426 case clang::Type::Typedef: {
9427 const clang::TypedefType *typedef_type =
9428 qual_type->getAs<clang::TypedefType>();
9429 if (typedef_type) {
9430 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9431 std::string clang_typedef_name(
9432 typedef_decl->getQualifiedNameAsString());
9433 if (!clang_typedef_name.empty()) {
9434 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009435 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009436 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009437 }
9438 } break;
9439
9440 case clang::Type::Auto:
9441 CompilerType(getASTContext(),
9442 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9443 .DumpTypeDescription(s);
9444 return;
9445
9446 case clang::Type::Elaborated:
9447 CompilerType(getASTContext(),
9448 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9449 .DumpTypeDescription(s);
9450 return;
9451
9452 case clang::Type::Paren:
9453 CompilerType(getASTContext(),
9454 llvm::cast<clang::ParenType>(qual_type)->desugar())
9455 .DumpTypeDescription(s);
9456 return;
9457
9458 case clang::Type::Record: {
9459 GetCompleteType(type);
9460
9461 const clang::RecordType *record_type =
9462 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9463 const clang::RecordDecl *record_decl = record_type->getDecl();
9464 const clang::CXXRecordDecl *cxx_record_decl =
9465 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9466
9467 if (cxx_record_decl)
9468 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9469 s->GetIndentLevel());
9470 else
9471 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9472 s->GetIndentLevel());
9473 } break;
9474
9475 default: {
9476 const clang::TagType *tag_type =
9477 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9478 if (tag_type) {
9479 clang::TagDecl *tag_decl = tag_type->getDecl();
9480 if (tag_decl)
9481 tag_decl->print(llvm_ostrm, 0);
9482 } else {
9483 std::string clang_type_name(qual_type.getAsString());
9484 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009485 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009486 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009487 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009488 }
9489
Kate Stoneb9c1b512016-09-06 20:57:50 +00009490 if (buf.size() > 0) {
9491 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009492 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009493 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009494}
9495
Kate Stoneb9c1b512016-09-06 20:57:50 +00009496void ClangASTContext::DumpTypeName(const CompilerType &type) {
9497 if (ClangUtil::IsClangType(type)) {
9498 clang::QualType qual_type(
9499 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9500
9501 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9502 switch (type_class) {
9503 case clang::Type::Record: {
9504 const clang::CXXRecordDecl *cxx_record_decl =
9505 qual_type->getAsCXXRecordDecl();
9506 if (cxx_record_decl)
9507 printf("class %s", cxx_record_decl->getName().str().c_str());
9508 } break;
9509
9510 case clang::Type::Enum: {
9511 clang::EnumDecl *enum_decl =
9512 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9513 if (enum_decl) {
9514 printf("enum %s", enum_decl->getName().str().c_str());
9515 }
9516 } break;
9517
9518 case clang::Type::ObjCObject:
9519 case clang::Type::ObjCInterface: {
9520 const clang::ObjCObjectType *objc_class_type =
9521 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9522 if (objc_class_type) {
9523 clang::ObjCInterfaceDecl *class_interface_decl =
9524 objc_class_type->getInterface();
9525 // We currently can't complete objective C types through the newly added
9526 // ASTContext
9527 // because it only supports TagDecl objects right now...
9528 if (class_interface_decl)
9529 printf("@class %s", class_interface_decl->getName().str().c_str());
9530 }
9531 } break;
9532
9533 case clang::Type::Typedef:
9534 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9535 ->getDecl()
9536 ->getName()
9537 .str()
9538 .c_str());
9539 break;
9540
9541 case clang::Type::Auto:
9542 printf("auto ");
9543 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9544 llvm::cast<clang::AutoType>(qual_type)
9545 ->getDeducedType()
9546 .getAsOpaquePtr()));
9547
9548 case clang::Type::Elaborated:
9549 printf("elaborated ");
9550 return DumpTypeName(CompilerType(
9551 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9552 ->getNamedType()
9553 .getAsOpaquePtr()));
9554
9555 case clang::Type::Paren:
9556 printf("paren ");
9557 return DumpTypeName(CompilerType(
9558 type.GetTypeSystem(),
9559 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9560
9561 default:
9562 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9563 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009564 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009565 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009566}
9567
Kate Stoneb9c1b512016-09-06 20:57:50 +00009568clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9569 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9570 const char *parent_name, int tag_decl_kind,
9571 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9572 if (template_param_infos.IsValid()) {
9573 std::string template_basename(parent_name);
9574 template_basename.erase(template_basename.find('<'));
9575
9576 return CreateClassTemplateDecl(decl_ctx, access_type,
9577 template_basename.c_str(), tag_decl_kind,
9578 template_param_infos);
9579 }
9580 return NULL;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009581}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009582
Kate Stoneb9c1b512016-09-06 20:57:50 +00009583void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9584 ClangASTContext *ast = (ClangASTContext *)baton;
9585 SymbolFile *sym_file = ast->GetSymbolFile();
9586 if (sym_file) {
9587 CompilerType clang_type = GetTypeForDecl(decl);
9588 if (clang_type)
9589 sym_file->CompleteType(clang_type);
9590 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009591}
9592
Kate Stoneb9c1b512016-09-06 20:57:50 +00009593void ClangASTContext::CompleteObjCInterfaceDecl(
9594 void *baton, clang::ObjCInterfaceDecl *decl) {
9595 ClangASTContext *ast = (ClangASTContext *)baton;
9596 SymbolFile *sym_file = ast->GetSymbolFile();
9597 if (sym_file) {
9598 CompilerType clang_type = GetTypeForDecl(decl);
9599 if (clang_type)
9600 sym_file->CompleteType(clang_type);
9601 }
Zachary Turner42dff792016-04-15 00:21:26 +00009602}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009603
Kate Stoneb9c1b512016-09-06 20:57:50 +00009604DWARFASTParser *ClangASTContext::GetDWARFParser() {
9605 if (!m_dwarf_ast_parser_ap)
9606 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9607 return m_dwarf_ast_parser_ap.get();
9608}
9609
9610PDBASTParser *ClangASTContext::GetPDBParser() {
9611 if (!m_pdb_ast_parser_ap)
9612 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9613 return m_pdb_ast_parser_ap.get();
9614}
9615
9616bool ClangASTContext::LayoutRecordType(
9617 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9618 uint64_t &alignment,
9619 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9620 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9621 &base_offsets,
9622 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9623 &vbase_offsets) {
9624 ClangASTContext *ast = (ClangASTContext *)baton;
9625 DWARFASTParserClang *dwarf_ast_parser =
9626 (DWARFASTParserClang *)ast->GetDWARFParser();
9627 return dwarf_ast_parser->GetClangASTImporter().LayoutRecordType(
9628 record_decl, bit_size, alignment, field_offsets, base_offsets,
9629 vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009630}
9631
Greg Clayton99558cc42015-08-24 23:46:31 +00009632//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009633// CompilerDecl override functions
9634//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009635
Kate Stoneb9c1b512016-09-06 20:57:50 +00009636ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9637 if (opaque_decl) {
9638 clang::NamedDecl *nd =
9639 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9640 if (nd != nullptr)
9641 return ConstString(nd->getDeclName().getAsString());
9642 }
9643 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009644}
9645
Kate Stoneb9c1b512016-09-06 20:57:50 +00009646ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9647 if (opaque_decl) {
9648 clang::NamedDecl *nd =
9649 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9650 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9651 clang::MangleContext *mc = getMangleContext();
9652 if (mc && mc->shouldMangleCXXName(nd)) {
9653 llvm::SmallVector<char, 1024> buf;
9654 llvm::raw_svector_ostream llvm_ostrm(buf);
9655 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9656 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9657 Ctor_Complete, llvm_ostrm);
9658 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9659 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9660 Dtor_Complete, llvm_ostrm);
9661 } else {
9662 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009663 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009664 if (buf.size() > 0)
9665 return ConstString(buf.data(), buf.size());
9666 }
Greg Claytonfe689042015-11-10 17:47:04 +00009667 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009668 }
9669 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009670}
9671
Kate Stoneb9c1b512016-09-06 20:57:50 +00009672CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9673 if (opaque_decl)
9674 return CompilerDeclContext(this,
9675 ((clang::Decl *)opaque_decl)->getDeclContext());
9676 else
9677 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009678}
9679
Kate Stoneb9c1b512016-09-06 20:57:50 +00009680CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9681 if (clang::FunctionDecl *func_decl =
9682 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9683 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9684 if (clang::ObjCMethodDecl *objc_method =
9685 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9686 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9687 else
Greg Claytonfe689042015-11-10 17:47:04 +00009688 return CompilerType();
9689}
9690
Kate Stoneb9c1b512016-09-06 20:57:50 +00009691size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9692 if (clang::FunctionDecl *func_decl =
9693 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9694 return func_decl->param_size();
9695 if (clang::ObjCMethodDecl *objc_method =
9696 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9697 return objc_method->param_size();
9698 else
9699 return 0;
9700}
9701
9702CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9703 size_t idx) {
9704 if (clang::FunctionDecl *func_decl =
9705 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9706 if (idx < func_decl->param_size()) {
9707 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9708 if (var_decl)
9709 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9710 }
9711 } else if (clang::ObjCMethodDecl *objc_method =
9712 llvm::dyn_cast<clang::ObjCMethodDecl>(
9713 (clang::Decl *)opaque_decl)) {
9714 if (idx < objc_method->param_size())
9715 return CompilerType(
9716 this,
9717 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9718 }
9719 return CompilerType();
9720}
9721
Paul Hermand628cbb2015-09-15 23:44:17 +00009722//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00009723// CompilerDeclContext functions
9724//----------------------------------------------------------------------
9725
Kate Stoneb9c1b512016-09-06 20:57:50 +00009726std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9727 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9728 std::vector<CompilerDecl> found_decls;
9729 if (opaque_decl_ctx) {
9730 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9731 std::set<DeclContext *> searched;
9732 std::multimap<DeclContext *, DeclContext *> search_queue;
9733 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009734
Kate Stoneb9c1b512016-09-06 20:57:50 +00009735 for (clang::DeclContext *decl_context = root_decl_ctx;
9736 decl_context != nullptr && found_decls.empty();
9737 decl_context = decl_context->getParent()) {
9738 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +00009739
Kate Stoneb9c1b512016-09-06 20:57:50 +00009740 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9741 it++) {
9742 if (!searched.insert(it->second).second)
9743 continue;
9744 symbol_file->ParseDeclsForContext(
9745 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +00009746
Kate Stoneb9c1b512016-09-06 20:57:50 +00009747 for (clang::Decl *child : it->second->decls()) {
9748 if (clang::UsingDirectiveDecl *ud =
9749 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9750 if (ignore_using_decls)
9751 continue;
9752 clang::DeclContext *from = ud->getCommonAncestor();
9753 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9754 search_queue.insert(
9755 std::make_pair(from, ud->getNominatedNamespace()));
9756 } else if (clang::UsingDecl *ud =
9757 llvm::dyn_cast<clang::UsingDecl>(child)) {
9758 if (ignore_using_decls)
9759 continue;
9760 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9761 clang::Decl *target = usd->getTargetDecl();
9762 if (clang::NamedDecl *nd =
9763 llvm::dyn_cast<clang::NamedDecl>(target)) {
9764 IdentifierInfo *ii = nd->getIdentifier();
9765 if (ii != nullptr &&
9766 ii->getName().equals(name.AsCString(nullptr)))
9767 found_decls.push_back(CompilerDecl(this, nd));
9768 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009769 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009770 } else if (clang::NamedDecl *nd =
9771 llvm::dyn_cast<clang::NamedDecl>(child)) {
9772 IdentifierInfo *ii = nd->getIdentifier();
9773 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9774 found_decls.push_back(CompilerDecl(this, nd));
9775 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009776 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009777 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009778 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009779 }
9780 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +00009781}
9782
Dawn Perchikb5925782015-12-12 19:31:41 +00009783// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009784// and return the number of levels it took to find it, or
9785// LLDB_INVALID_DECL_LEVEL
9786// if not found. If the decl was imported via a using declaration, its name
9787// and/or
9788// type, if set, will be used to check that the decl found in the scope is a
9789// match.
Dawn Perchikb5925782015-12-12 19:31:41 +00009790//
Kate Stoneb9c1b512016-09-06 20:57:50 +00009791// The optional name is required by languages (like C++) to handle using
9792// declarations
Dawn Perchikb5925782015-12-12 19:31:41 +00009793// like:
9794//
9795// void poo();
9796// namespace ns {
9797// void foo();
9798// void goo();
9799// }
9800// void bar() {
9801// using ns::foo;
9802// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
9803// // LLDB_INVALID_DECL_LEVEL for 'goo'.
9804// }
9805//
9806// The optional type is useful in the case that there's a specific overload
9807// that we're looking for that might otherwise be shadowed, like:
9808//
9809// void foo(int);
9810// namespace ns {
9811// void foo();
9812// }
9813// void bar() {
9814// using ns::foo;
9815// // CountDeclLevels returns 0 for { 'foo', void() },
9816// // 1 for { 'foo', void(int) }, and
9817// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
9818// }
9819//
9820// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +00009821// function at file scope will return the same level as a function at global
9822// scope.
9823// Ideally we'd like to treat the file scope as an additional scope just below
9824// the
9825// global scope. More work needs to be done to recognise that, if the decl
9826// we're
9827// trying to look up is static, we should compare its source file with that of
9828// the
Dawn Perchikb5925782015-12-12 19:31:41 +00009829// current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009830uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
9831 clang::DeclContext *child_decl_ctx,
9832 ConstString *child_name,
9833 CompilerType *child_type) {
9834 if (frame_decl_ctx) {
9835 std::set<DeclContext *> searched;
9836 std::multimap<DeclContext *, DeclContext *> search_queue;
9837 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +00009838
Kate Stoneb9c1b512016-09-06 20:57:50 +00009839 // Get the lookup scope for the decl we're trying to find.
9840 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +00009841
Kate Stoneb9c1b512016-09-06 20:57:50 +00009842 // Look for it in our scope's decl context and its parents.
9843 uint32_t level = 0;
9844 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
9845 decl_ctx = decl_ctx->getParent()) {
9846 if (!decl_ctx->isLookupContext())
9847 continue;
9848 if (decl_ctx == parent_decl_ctx)
9849 // Found it!
9850 return level;
9851 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
9852 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
9853 it++) {
9854 if (searched.find(it->second) != searched.end())
9855 continue;
9856
9857 // Currently DWARF has one shared translation unit for all Decls at top
9858 // level, so this
9859 // would erroneously find using statements anywhere. So don't look at
9860 // the top-level
9861 // translation unit.
9862 // TODO fix this and add a testcase that depends on it.
9863
9864 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
9865 continue;
9866
9867 searched.insert(it->second);
9868 symbol_file->ParseDeclsForContext(
9869 CompilerDeclContext(this, it->second));
9870
9871 for (clang::Decl *child : it->second->decls()) {
9872 if (clang::UsingDirectiveDecl *ud =
9873 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9874 clang::DeclContext *ns = ud->getNominatedNamespace();
9875 if (ns == parent_decl_ctx)
9876 // Found it!
9877 return level;
9878 clang::DeclContext *from = ud->getCommonAncestor();
9879 if (searched.find(ns) == searched.end())
9880 search_queue.insert(std::make_pair(from, ns));
9881 } else if (child_name) {
9882 if (clang::UsingDecl *ud =
9883 llvm::dyn_cast<clang::UsingDecl>(child)) {
9884 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9885 clang::Decl *target = usd->getTargetDecl();
9886 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
9887 if (!nd)
9888 continue;
9889 // Check names.
9890 IdentifierInfo *ii = nd->getIdentifier();
9891 if (ii == nullptr ||
9892 !ii->getName().equals(child_name->AsCString(nullptr)))
9893 continue;
9894 // Check types, if one was provided.
9895 if (child_type) {
9896 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
9897 if (!AreTypesSame(clang_type, *child_type,
9898 /*ignore_qualifiers=*/true))
9899 continue;
9900 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009901 // Found it!
9902 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009903 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009904 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009905 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009906 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009907 }
9908 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +00009909 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009910 }
9911 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +00009912}
9913
Kate Stoneb9c1b512016-09-06 20:57:50 +00009914bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
9915 if (opaque_decl_ctx)
9916 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
9917 else
Greg Clayton99558cc42015-08-24 23:46:31 +00009918 return false;
9919}
9920
Kate Stoneb9c1b512016-09-06 20:57:50 +00009921ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
9922 if (opaque_decl_ctx) {
9923 clang::NamedDecl *named_decl =
9924 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9925 if (named_decl)
9926 return ConstString(named_decl->getName());
9927 }
9928 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +00009929}
9930
Kate Stoneb9c1b512016-09-06 20:57:50 +00009931ConstString
9932ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
9933 if (opaque_decl_ctx) {
9934 clang::NamedDecl *named_decl =
9935 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9936 if (named_decl)
9937 return ConstString(
9938 llvm::StringRef(named_decl->getQualifiedNameAsString()));
9939 }
9940 return ConstString();
9941}
9942
9943bool ClangASTContext::DeclContextIsClassMethod(
9944 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
9945 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
9946 if (opaque_decl_ctx) {
9947 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9948 if (ObjCMethodDecl *objc_method =
9949 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
9950 if (is_instance_method_ptr)
9951 *is_instance_method_ptr = objc_method->isInstanceMethod();
9952 if (language_ptr)
9953 *language_ptr = eLanguageTypeObjC;
9954 if (language_object_name_ptr)
9955 language_object_name_ptr->SetCString("self");
9956 return true;
9957 } else if (CXXMethodDecl *cxx_method =
9958 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
9959 if (is_instance_method_ptr)
9960 *is_instance_method_ptr = cxx_method->isInstance();
9961 if (language_ptr)
9962 *language_ptr = eLanguageTypeC_plus_plus;
9963 if (language_object_name_ptr)
9964 language_object_name_ptr->SetCString("this");
9965 return true;
9966 } else if (clang::FunctionDecl *function_decl =
9967 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
9968 ClangASTMetadata *metadata =
9969 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
9970 if (metadata && metadata->HasObjectPtr()) {
9971 if (is_instance_method_ptr)
9972 *is_instance_method_ptr = true;
9973 if (language_ptr)
9974 *language_ptr = eLanguageTypeObjC;
9975 if (language_object_name_ptr)
9976 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
9977 return true;
9978 }
9979 }
9980 }
9981 return false;
9982}
9983
9984clang::DeclContext *
9985ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
9986 if (dc.IsClang())
9987 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
9988 return nullptr;
9989}
Greg Clayton99558cc42015-08-24 23:46:31 +00009990
9991ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00009992ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
9993 if (dc.IsClang())
9994 return llvm::dyn_cast<clang::ObjCMethodDecl>(
9995 (clang::DeclContext *)dc.GetOpaqueDeclContext());
9996 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +00009997}
9998
9999CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010000ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10001 if (dc.IsClang())
10002 return llvm::dyn_cast<clang::CXXMethodDecl>(
10003 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10004 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010005}
10006
10007clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010008ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10009 if (dc.IsClang())
10010 return llvm::dyn_cast<clang::FunctionDecl>(
10011 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10012 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010013}
10014
10015clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010016ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10017 if (dc.IsClang())
10018 return llvm::dyn_cast<clang::NamespaceDecl>(
10019 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10020 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010021}
10022
10023ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010024ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10025 const void *object) {
10026 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10027 if (ast)
10028 return ClangASTContext::GetMetadata(ast, object);
10029 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010030}
10031
10032clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010033ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10034 ClangASTContext *ast =
10035 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10036 if (ast)
10037 return ast->getASTContext();
10038 return nullptr;
10039}
10040
10041ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10042 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10043 m_target_wp(target.shared_from_this()),
10044 m_persistent_variables(new ClangPersistentVariables) {}
10045
10046UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010047 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010048 Expression::ResultType desired_type,
10049 const EvaluateExpressionOptions &options) {
10050 TargetSP target_sp = m_target_wp.lock();
10051 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010052 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010053
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010054 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010055 desired_type, options);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010056}
10057
Kate Stoneb9c1b512016-09-06 20:57:50 +000010058FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10059 const CompilerType &return_type, const Address &function_address,
10060 const ValueList &arg_value_list, const char *name) {
10061 TargetSP target_sp = m_target_wp.lock();
10062 if (!target_sp)
10063 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010064
Kate Stoneb9c1b512016-09-06 20:57:50 +000010065 Process *process = target_sp->GetProcessSP().get();
10066 if (!process)
10067 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010068
Kate Stoneb9c1b512016-09-06 20:57:50 +000010069 return new ClangFunctionCaller(*process, return_type, function_address,
10070 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010071}
10072
10073UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010074ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10075 const char *name) {
10076 TargetSP target_sp = m_target_wp.lock();
10077 if (!target_sp)
10078 return nullptr;
10079
10080 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010081}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010082
10083PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010084ClangASTContextForExpressions::GetPersistentExpressionState() {
10085 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010086}
Sean Callanan68e44232017-09-28 20:20:25 +000010087
10088clang::ExternalASTMerger &
10089ClangASTContextForExpressions::GetMergerUnchecked() {
Eugene Zemtsova9d928c2017-09-29 03:15:08 +000010090 lldbassert(m_scratch_ast_source_ap != nullptr);
Sean Callanan68e44232017-09-28 20:20:25 +000010091 return m_scratch_ast_source_ap->GetMergerUnchecked();
10092}