blob: ac16f91285cb29cb984cfd0334e7687d2563b6ec [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"
Greg Clayton514487e2011-02-15 21:59:32 +000073#include "lldb/Core/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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141static bool IsOperator(const char *name,
142 clang::OverloadedOperatorKind &op_kind) {
143 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 // if (Opts.CPlusPlus)
439 // Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
440 //
441 // if (Args.hasArg(OPT_fobjc_gc_only))
442 // Opts.setGCMode(LangOptions::GCOnly);
443 // else if (Args.hasArg(OPT_fobjc_gc))
444 // Opts.setGCMode(LangOptions::HybridGC);
445 //
446 // if (Args.hasArg(OPT_print_ivar_layout))
447 // Opts.ObjCGCBitmapPrint = 1;
448 //
449 // if (Args.hasArg(OPT_faltivec))
450 // Opts.AltiVec = 1;
451 //
452 // if (Args.hasArg(OPT_pthread))
453 // Opts.POSIXThreads = 1;
454 //
455 // llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
456 // "default");
457 // if (Vis == "default")
458 Opts.setValueVisibilityMode(DefaultVisibility);
459 // else if (Vis == "hidden")
460 // Opts.setVisibilityMode(LangOptions::Hidden);
461 // else if (Vis == "protected")
462 // Opts.setVisibilityMode(LangOptions::Protected);
463 // else
464 // Diags.Report(diag::err_drv_invalid_value)
465 // << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 // Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
470 // is specified, or -std is set to a conforming mode.
471 Opts.Trigraphs = !Opts.GNUMode;
472 // if (Args.hasArg(OPT_trigraphs))
473 // Opts.Trigraphs = 1;
474 //
475 // Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
476 // OPT_fno_dollars_in_identifiers,
477 // !Opts.AsmPreprocessor);
478 // Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
479 // Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
480 // Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
481 // if (Args.hasArg(OPT_fno_lax_vector_conversions))
482 // Opts.LaxVectorConversions = 0;
483 // Opts.Exceptions = Args.hasArg(OPT_fexceptions);
484 // Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
485 // Opts.Blocks = Args.hasArg(OPT_fblocks);
486 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
487 // Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
488 // Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
489 // Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
490 // Opts.AssumeSaneOperatorNew =
491 // !Args.hasArg(OPT_fno_assume_sane_operator_new);
492 // Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
493 // Opts.AccessControl = Args.hasArg(OPT_faccess_control);
494 // Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
495 // Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
496 // Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth,
497 // 99,
498 // Diags);
499 // Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
500 // Opts.ObjCConstantStringClass = getLastArgValue(Args,
501 // OPT_fconstant_string_class);
502 // Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
503 // Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
504 // Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
505 // Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
506 // Opts.Static = Args.hasArg(OPT_static_define);
507 Opts.OptimizeSize = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508
Kate Stoneb9c1b512016-09-06 20:57:50 +0000509 // FIXME: Eliminate this dependency.
510 // unsigned Opt =
511 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
512 // Opts.Optimize = Opt != 0;
513 unsigned Opt = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515 // This is the __NO_INLINE__ define, which just depends on things like the
516 // optimization level and -fno-inline, not actually whether the backend has
517 // inlining enabled.
518 //
519 // FIXME: This is affected by other options (-fno-inline).
520 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521
Kate Stoneb9c1b512016-09-06 20:57:50 +0000522 // unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
523 // switch (SSP) {
524 // default:
525 // Diags.Report(diag::err_drv_invalid_value)
526 // << Args.getLastArg(OPT_stack_protector)->getAsString(Args) <<
527 // SSP;
528 // break;
529 // case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
530 // case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
531 // case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
532 // }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000533}
534
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535ClangASTContext::ClangASTContext(const char *target_triple)
536 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_ap(),
537 m_language_options_ap(), m_source_manager_ap(), m_diagnostics_engine_ap(),
538 m_target_options_rp(), m_target_info_ap(), m_identifier_table_ap(),
539 m_selector_table_ap(), m_builtins_ap(), m_callback_tag_decl(nullptr),
540 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
541 m_pointer_byte_size(0), m_ast_owned(false) {
542 if (target_triple && target_triple[0])
543 SetTargetTriple(target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000544}
545
546//----------------------------------------------------------------------
547// Destructor
548//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000549ClangASTContext::~ClangASTContext() { Finalize(); }
550
551ConstString ClangASTContext::GetPluginNameStatic() {
552 return ConstString("clang");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553}
554
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555ConstString ClangASTContext::GetPluginName() {
556 return ClangASTContext::GetPluginNameStatic();
Greg Clayton56939cb2015-09-17 22:23:34 +0000557}
558
Kate Stoneb9c1b512016-09-06 20:57:50 +0000559uint32_t ClangASTContext::GetPluginVersion() { return 1; }
Greg Clayton56939cb2015-09-17 22:23:34 +0000560
Kate Stoneb9c1b512016-09-06 20:57:50 +0000561lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
562 lldb_private::Module *module,
563 Target *target) {
564 if (ClangASTContextSupportsLanguage(language)) {
565 ArchSpec arch;
566 if (module)
567 arch = module->GetArchitecture();
568 else if (target)
569 arch = target->GetArchitecture();
Greg Clayton56939cb2015-09-17 22:23:34 +0000570
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571 if (arch.IsValid()) {
572 ArchSpec fixed_arch = arch;
573 // LLVM wants this to be set to iOS or MacOSX; if we're working on
574 // a bare-boards type image, change the triple for llvm's benefit.
575 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
576 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
577 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
578 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
579 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
580 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
581 } else {
582 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
Greg Clayton56939cb2015-09-17 22:23:34 +0000583 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000584 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000585
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586 if (module) {
587 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
588 if (ast_sp) {
589 ast_sp->SetArchitecture(fixed_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000590 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591 return ast_sp;
592 } else if (target && target->IsValid()) {
593 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
594 new ClangASTContextForExpressions(*target));
595 if (ast_sp) {
596 ast_sp->SetArchitecture(fixed_arch);
597 ast_sp->m_scratch_ast_source_ap.reset(
598 new ClangASTSource(target->shared_from_this()));
Sean Callanan68e44232017-09-28 20:20:25 +0000599 lldbassert(ast_sp->getFileManager());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000600 ast_sp->m_scratch_ast_source_ap->InstallASTContext(
Sean Callanan68e44232017-09-28 20:20:25 +0000601 *ast_sp->getASTContext(), *ast_sp->getFileManager(), true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
603 ast_sp->m_scratch_ast_source_ap->CreateProxy());
604 ast_sp->SetExternalSource(proxy_ast_source);
605 return ast_sp;
606 }
607 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000609 }
610 return lldb::TypeSystemSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611}
612
Kate Stoneb9c1b512016-09-06 20:57:50 +0000613void ClangASTContext::EnumerateSupportedLanguages(
614 std::set<lldb::LanguageType> &languages_for_types,
615 std::set<lldb::LanguageType> &languages_for_expressions) {
616 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
617 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
618 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
619 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
620 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
621 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
622
623 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
624 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
625 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
626 lldb::eLanguageTypeC_plus_plus_14});
627
628 languages_for_types.insert(s_supported_languages_for_types.begin(),
629 s_supported_languages_for_types.end());
630 languages_for_expressions.insert(
631 s_supported_languages_for_expressions.begin(),
632 s_supported_languages_for_expressions.end());
Enrico Granata5d84a692014-08-19 21:46:37 +0000633}
634
Kate Stoneb9c1b512016-09-06 20:57:50 +0000635void ClangASTContext::Initialize() {
636 PluginManager::RegisterPlugin(GetPluginNameStatic(),
637 "clang base AST context plug-in",
638 CreateInstance, EnumerateSupportedLanguages);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639}
640
Kate Stoneb9c1b512016-09-06 20:57:50 +0000641void ClangASTContext::Terminate() {
642 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000643}
644
Kate Stoneb9c1b512016-09-06 20:57:50 +0000645void ClangASTContext::Finalize() {
646 if (m_ast_ap.get()) {
647 GetASTMap().Erase(m_ast_ap.get());
648 if (!m_ast_owned)
649 m_ast_ap.release();
650 }
651
652 m_builtins_ap.reset();
653 m_selector_table_ap.reset();
654 m_identifier_table_ap.reset();
655 m_target_info_ap.reset();
656 m_target_options_rp.reset();
657 m_diagnostics_engine_ap.reset();
658 m_source_manager_ap.reset();
659 m_language_options_ap.reset();
660 m_ast_ap.reset();
661 m_scratch_ast_source_ap.reset();
662}
663
664void ClangASTContext::Clear() {
665 m_ast_ap.reset();
666 m_language_options_ap.reset();
667 m_source_manager_ap.reset();
668 m_diagnostics_engine_ap.reset();
669 m_target_options_rp.reset();
670 m_target_info_ap.reset();
671 m_identifier_table_ap.reset();
672 m_selector_table_ap.reset();
673 m_builtins_ap.reset();
674 m_pointer_byte_size = 0;
675}
676
677const char *ClangASTContext::GetTargetTriple() {
678 return m_target_triple.c_str();
679}
680
681void ClangASTContext::SetTargetTriple(const char *target_triple) {
682 Clear();
683 m_target_triple.assign(target_triple);
684}
685
686void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
687 SetTargetTriple(arch.GetTriple().str().c_str());
688}
689
690bool ClangASTContext::HasExternalSource() {
691 ASTContext *ast = getASTContext();
692 if (ast)
693 return ast->getExternalSource() != nullptr;
694 return false;
695}
696
697void ClangASTContext::SetExternalSource(
698 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) {
699 ASTContext *ast = getASTContext();
700 if (ast) {
701 ast->setExternalSource(ast_source_ap);
702 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
703 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
704 }
705}
706
707void ClangASTContext::RemoveExternalSource() {
708 ASTContext *ast = getASTContext();
709
710 if (ast) {
711 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
712 ast->setExternalSource(empty_ast_source_ap);
713 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
714 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
715 }
716}
717
718void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
719 if (!m_ast_owned) {
720 m_ast_ap.release();
721 }
722 m_ast_owned = false;
723 m_ast_ap.reset(ast_ctx);
724 GetASTMap().Insert(ast_ctx, this);
725}
726
727ASTContext *ClangASTContext::getASTContext() {
728 if (m_ast_ap.get() == nullptr) {
729 m_ast_owned = true;
730 m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
731 *getIdentifierTable(), *getSelectorTable(),
732 *getBuiltinContext()));
733
734 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
735
736 // This can be NULL if we don't know anything about the architecture or if
737 // the
738 // target for an architecture isn't enabled in the llvm/clang that we built
739 TargetInfo *target_info = getTargetInfo();
740 if (target_info)
741 m_ast_ap->InitBuiltinTypes(*target_info);
742
743 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
744 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
745 // m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000747
748 GetASTMap().Insert(m_ast_ap.get(), this);
749
750 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap(
751 new ClangExternalASTSourceCallbacks(
752 ClangASTContext::CompleteTagDecl,
753 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
754 ClangASTContext::LayoutRecordType, this));
755 SetExternalSource(ast_source_ap);
756 }
757 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758}
759
Kate Stoneb9c1b512016-09-06 20:57:50 +0000760ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
761 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
762 return clang_ast;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763}
764
Kate Stoneb9c1b512016-09-06 20:57:50 +0000765Builtin::Context *ClangASTContext::getBuiltinContext() {
766 if (m_builtins_ap.get() == nullptr)
767 m_builtins_ap.reset(new Builtin::Context());
768 return m_builtins_ap.get();
Sean Callanan79439e82010-11-18 02:56:27 +0000769}
770
Kate Stoneb9c1b512016-09-06 20:57:50 +0000771IdentifierTable *ClangASTContext::getIdentifierTable() {
772 if (m_identifier_table_ap.get() == nullptr)
773 m_identifier_table_ap.reset(
774 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
775 return m_identifier_table_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000776}
777
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778LangOptions *ClangASTContext::getLanguageOptions() {
779 if (m_language_options_ap.get() == nullptr) {
780 m_language_options_ap.reset(new LangOptions());
Richard Smith8186cd42017-04-26 22:10:53 +0000781 ParseLangArgs(*m_language_options_ap, InputKind::ObjCXX, GetTargetTriple());
782 // InitializeLangOptions(*m_language_options_ap, InputKind::ObjCXX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000783 }
784 return m_language_options_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000785}
786
Kate Stoneb9c1b512016-09-06 20:57:50 +0000787SelectorTable *ClangASTContext::getSelectorTable() {
788 if (m_selector_table_ap.get() == nullptr)
789 m_selector_table_ap.reset(new SelectorTable());
790 return m_selector_table_ap.get();
Greg Claytonfe689042015-11-10 17:47:04 +0000791}
792
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793clang::FileManager *ClangASTContext::getFileManager() {
794 if (m_file_manager_ap.get() == nullptr) {
795 clang::FileSystemOptions file_system_options;
796 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
797 }
798 return m_file_manager_ap.get();
799}
800
801clang::SourceManager *ClangASTContext::getSourceManager() {
802 if (m_source_manager_ap.get() == nullptr)
803 m_source_manager_ap.reset(
804 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
805 return m_source_manager_ap.get();
806}
807
808clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
809 if (m_diagnostics_engine_ap.get() == nullptr) {
810 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
811 m_diagnostics_engine_ap.reset(
812 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
813 }
814 return m_diagnostics_engine_ap.get();
815}
816
817clang::MangleContext *ClangASTContext::getMangleContext() {
818 if (m_mangle_ctx_ap.get() == nullptr)
819 m_mangle_ctx_ap.reset(getASTContext()->createMangleContext());
820 return m_mangle_ctx_ap.get();
821}
822
823class NullDiagnosticConsumer : public DiagnosticConsumer {
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000824public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000825 NullDiagnosticConsumer() {
826 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
827 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000828
Kate Stoneb9c1b512016-09-06 20:57:50 +0000829 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
830 const clang::Diagnostic &info) {
831 if (m_log) {
832 llvm::SmallVector<char, 32> diag_str(10);
833 info.FormatDiagnostic(diag_str);
834 diag_str.push_back('\0');
835 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000836 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837 }
838
839 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
840 return new NullDiagnosticConsumer();
841 }
842
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000843private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844 Log *m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000845};
846
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
848 if (m_diagnostic_consumer_ap.get() == nullptr)
849 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
850
851 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000852}
853
Kate Stoneb9c1b512016-09-06 20:57:50 +0000854std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
855 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) {
856 m_target_options_rp = std::make_shared<clang::TargetOptions>();
857 if (m_target_options_rp.get() != nullptr)
858 m_target_options_rp->Triple = m_target_triple;
859 }
860 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000861}
862
Kate Stoneb9c1b512016-09-06 20:57:50 +0000863TargetInfo *ClangASTContext::getTargetInfo() {
864 // target_triple should be something like "x86_64-apple-macosx"
865 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
866 m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
867 getTargetOptions()));
868 return m_target_info_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000869}
870
871#pragma mark Basic Types
872
Kate Stoneb9c1b512016-09-06 20:57:50 +0000873static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
874 ASTContext *ast, QualType qual_type) {
875 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
876 if (qual_type_bit_size == bit_size)
877 return true;
878 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000879}
Greg Clayton56939cb2015-09-17 22:23:34 +0000880
Greg Claytona1e5dc82015-08-11 22:53:00 +0000881CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000882ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
883 size_t bit_size) {
884 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
885 getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886}
887
Kate Stoneb9c1b512016-09-06 20:57:50 +0000888CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
889 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
890 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000891 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000892 switch (encoding) {
893 case eEncodingInvalid:
894 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
895 return CompilerType(ast, ast->VoidPtrTy);
896 break;
897
898 case eEncodingUint:
899 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
900 return CompilerType(ast, ast->UnsignedCharTy);
901 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
902 return CompilerType(ast, ast->UnsignedShortTy);
903 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
904 return CompilerType(ast, ast->UnsignedIntTy);
905 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
906 return CompilerType(ast, ast->UnsignedLongTy);
907 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
908 return CompilerType(ast, ast->UnsignedLongLongTy);
909 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
910 return CompilerType(ast, ast->UnsignedInt128Ty);
911 break;
912
913 case eEncodingSint:
914 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
915 return CompilerType(ast, ast->SignedCharTy);
916 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
917 return CompilerType(ast, ast->ShortTy);
918 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
919 return CompilerType(ast, ast->IntTy);
920 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
921 return CompilerType(ast, ast->LongTy);
922 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
923 return CompilerType(ast, ast->LongLongTy);
924 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
925 return CompilerType(ast, ast->Int128Ty);
926 break;
927
928 case eEncodingIEEE754:
929 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
930 return CompilerType(ast, ast->FloatTy);
931 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
932 return CompilerType(ast, ast->DoubleTy);
933 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
934 return CompilerType(ast, ast->LongDoubleTy);
935 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
936 return CompilerType(ast, ast->HalfTy);
937 break;
938
939 case eEncodingVector:
940 // Sanity check that bit_size is a multiple of 8's.
941 if (bit_size && !(bit_size & 0x7u))
942 return CompilerType(
943 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
944 break;
945 }
946
947 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000948}
949
Greg Clayton57ee3062013-07-11 22:46:58 +0000950lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000951ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) {
952 if (name) {
953 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
954 static TypeNameToBasicTypeMap g_type_map;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000955 static llvm::once_flag g_once_flag;
956 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000957 // "void"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000958 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000959
960 // "char"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000961 g_type_map.Append(ConstString("char"), eBasicTypeChar);
962 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
963 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
964 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
965 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
966 g_type_map.Append(ConstString("unsigned wchar_t"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000967 eBasicTypeUnsignedWChar);
968 // "short"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000969 g_type_map.Append(ConstString("short"), eBasicTypeShort);
970 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
971 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
972 g_type_map.Append(ConstString("unsigned short int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000973 eBasicTypeUnsignedShort);
974
975 // "int"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000976 g_type_map.Append(ConstString("int"), eBasicTypeInt);
977 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
978 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
979 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000980
981 // "long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000982 g_type_map.Append(ConstString("long"), eBasicTypeLong);
983 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
984 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
985 g_type_map.Append(ConstString("unsigned long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000986 eBasicTypeUnsignedLong);
987
988 // "long long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000989 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
990 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
991 g_type_map.Append(ConstString("unsigned long long"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992 eBasicTypeUnsignedLongLong);
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000993 g_type_map.Append(ConstString("unsigned long long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000994 eBasicTypeUnsignedLongLong);
995
996 // "int128"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000997 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
998 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000999
1000 // Miscellaneous
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001001 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
1002 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
1003 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
1004 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
1005 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
1006 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
1007 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001008 g_type_map.Sort();
1009 });
1010
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001011 return g_type_map.Find(name, eBasicTypeInvalid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012 }
1013 return eBasicTypeInvalid;
Greg Clayton57ee3062013-07-11 22:46:58 +00001014}
1015
Kate Stoneb9c1b512016-09-06 20:57:50 +00001016CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1017 const ConstString &name) {
1018 if (ast) {
1019 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1020 return ClangASTContext::GetBasicType(ast, basic_type);
1021 }
1022 return CompilerType();
1023}
1024
1025uint32_t ClangASTContext::GetPointerByteSize() {
1026 if (m_pointer_byte_size == 0)
1027 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid)
1028 .GetPointerType()
1029 .GetByteSize(nullptr);
1030 return m_pointer_byte_size;
1031}
1032
1033CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1034 return GetBasicType(getASTContext(), basic_type);
1035}
1036
1037CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1038 lldb::BasicType basic_type) {
1039 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001040 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001041 lldb::opaque_compiler_type_t clang_type =
1042 GetOpaqueCompilerType(ast, basic_type);
1043
1044 if (clang_type)
1045 return CompilerType(GetASTContext(ast), clang_type);
1046 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001047}
1048
Kate Stoneb9c1b512016-09-06 20:57:50 +00001049CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1050 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1051 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001052
Kate Stoneb9c1b512016-09-06 20:57:50 +00001053#define streq(a, b) strcmp(a, b) == 0
1054 assert(ast != nullptr);
1055 if (ast) {
1056 switch (dw_ate) {
1057 default:
1058 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001059
Kate Stoneb9c1b512016-09-06 20:57:50 +00001060 case DW_ATE_address:
1061 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1062 return CompilerType(ast, ast->VoidPtrTy);
1063 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001064
Kate Stoneb9c1b512016-09-06 20:57:50 +00001065 case DW_ATE_boolean:
1066 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
1067 return CompilerType(ast, ast->BoolTy);
1068 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1069 return CompilerType(ast, ast->UnsignedCharTy);
1070 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1071 return CompilerType(ast, ast->UnsignedShortTy);
1072 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1073 return CompilerType(ast, ast->UnsignedIntTy);
1074 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001075
Kate Stoneb9c1b512016-09-06 20:57:50 +00001076 case DW_ATE_lo_user:
1077 // This has been seen to mean DW_AT_complex_integer
1078 if (type_name) {
1079 if (::strstr(type_name, "complex")) {
1080 CompilerType complex_int_clang_type =
1081 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1082 bit_size / 2);
1083 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1084 complex_int_clang_type)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001085 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001086 }
1087 break;
1088
1089 case DW_ATE_complex_float:
1090 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1091 return CompilerType(ast, ast->FloatComplexTy);
1092 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1093 return CompilerType(ast, ast->DoubleComplexTy);
1094 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1095 return CompilerType(ast, ast->LongDoubleComplexTy);
1096 else {
1097 CompilerType complex_float_clang_type =
1098 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1099 bit_size / 2);
1100 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1101 complex_float_clang_type)));
1102 }
1103 break;
1104
1105 case DW_ATE_float:
1106 if (streq(type_name, "float") &&
1107 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1108 return CompilerType(ast, ast->FloatTy);
1109 if (streq(type_name, "double") &&
1110 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1111 return CompilerType(ast, ast->DoubleTy);
1112 if (streq(type_name, "long double") &&
1113 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1114 return CompilerType(ast, ast->LongDoubleTy);
1115 // Fall back to not requiring a name match
1116 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1117 return CompilerType(ast, ast->FloatTy);
1118 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1119 return CompilerType(ast, ast->DoubleTy);
1120 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1121 return CompilerType(ast, ast->LongDoubleTy);
1122 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1123 return CompilerType(ast, ast->HalfTy);
1124 break;
1125
1126 case DW_ATE_signed:
1127 if (type_name) {
1128 if (streq(type_name, "wchar_t") &&
1129 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1130 (getTargetInfo() &&
1131 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1132 return CompilerType(ast, ast->WCharTy);
1133 if (streq(type_name, "void") &&
1134 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1135 return CompilerType(ast, ast->VoidTy);
1136 if (strstr(type_name, "long long") &&
1137 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1138 return CompilerType(ast, ast->LongLongTy);
1139 if (strstr(type_name, "long") &&
1140 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1141 return CompilerType(ast, ast->LongTy);
1142 if (strstr(type_name, "short") &&
1143 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1144 return CompilerType(ast, ast->ShortTy);
1145 if (strstr(type_name, "char")) {
1146 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1147 return CompilerType(ast, ast->CharTy);
1148 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1149 return CompilerType(ast, ast->SignedCharTy);
1150 }
1151 if (strstr(type_name, "int")) {
1152 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1153 return CompilerType(ast, ast->IntTy);
1154 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1155 return CompilerType(ast, ast->Int128Ty);
1156 }
1157 }
1158 // We weren't able to match up a type name, just search by size
1159 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1160 return CompilerType(ast, ast->CharTy);
1161 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1162 return CompilerType(ast, ast->ShortTy);
1163 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1164 return CompilerType(ast, ast->IntTy);
1165 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1166 return CompilerType(ast, ast->LongTy);
1167 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1168 return CompilerType(ast, ast->LongLongTy);
1169 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1170 return CompilerType(ast, ast->Int128Ty);
1171 break;
1172
1173 case DW_ATE_signed_char:
1174 if (ast->getLangOpts().CharIsSigned && type_name &&
1175 streq(type_name, "char")) {
1176 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1177 return CompilerType(ast, ast->CharTy);
1178 }
1179 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1180 return CompilerType(ast, ast->SignedCharTy);
1181 break;
1182
1183 case DW_ATE_unsigned:
1184 if (type_name) {
1185 if (streq(type_name, "wchar_t")) {
1186 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1187 if (!(getTargetInfo() &&
1188 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1189 return CompilerType(ast, ast->WCharTy);
1190 }
1191 }
1192 if (strstr(type_name, "long long")) {
1193 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1194 return CompilerType(ast, ast->UnsignedLongLongTy);
1195 } else if (strstr(type_name, "long")) {
1196 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1197 return CompilerType(ast, ast->UnsignedLongTy);
1198 } else if (strstr(type_name, "short")) {
1199 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1200 return CompilerType(ast, ast->UnsignedShortTy);
1201 } else if (strstr(type_name, "char")) {
1202 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1203 return CompilerType(ast, ast->UnsignedCharTy);
1204 } else if (strstr(type_name, "int")) {
1205 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1206 return CompilerType(ast, ast->UnsignedIntTy);
1207 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1208 return CompilerType(ast, ast->UnsignedInt128Ty);
1209 }
1210 }
1211 // We weren't able to match up a type name, just search by size
1212 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1213 return CompilerType(ast, ast->UnsignedCharTy);
1214 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1215 return CompilerType(ast, ast->UnsignedShortTy);
1216 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1217 return CompilerType(ast, ast->UnsignedIntTy);
1218 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1219 return CompilerType(ast, ast->UnsignedLongTy);
1220 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1221 return CompilerType(ast, ast->UnsignedLongLongTy);
1222 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1223 return CompilerType(ast, ast->UnsignedInt128Ty);
1224 break;
1225
1226 case DW_ATE_unsigned_char:
1227 if (!ast->getLangOpts().CharIsSigned && type_name &&
1228 streq(type_name, "char")) {
1229 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1230 return CompilerType(ast, ast->CharTy);
1231 }
1232 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1233 return CompilerType(ast, ast->UnsignedCharTy);
1234 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1235 return CompilerType(ast, ast->UnsignedShortTy);
1236 break;
1237
1238 case DW_ATE_imaginary_float:
1239 break;
1240
1241 case DW_ATE_UTF:
1242 if (type_name) {
1243 if (streq(type_name, "char16_t")) {
1244 return CompilerType(ast, ast->Char16Ty);
1245 } else if (streq(type_name, "char32_t")) {
1246 return CompilerType(ast, ast->Char32Ty);
1247 }
1248 }
1249 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001250 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001251 }
1252 // This assert should fire for anything that we don't catch above so we know
1253 // to fix any issues we run into.
1254 if (type_name) {
1255 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1256 "DW_TAG_base_type '%s' encoded with "
1257 "DW_ATE = 0x%x, bit_size = %u\n",
1258 type_name, dw_ate, bit_size);
1259 } else {
1260 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1261 "DW_TAG_base_type encoded with "
1262 "DW_ATE = 0x%x, bit_size = %u\n",
1263 dw_ate, bit_size);
1264 }
1265 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001266}
1267
Kate Stoneb9c1b512016-09-06 20:57:50 +00001268CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1269 if (ast)
1270 return CompilerType(ast, ast->UnknownAnyTy);
1271 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001272}
1273
Kate Stoneb9c1b512016-09-06 20:57:50 +00001274CompilerType ClangASTContext::GetCStringType(bool is_const) {
1275 ASTContext *ast = getASTContext();
1276 QualType char_type(ast->CharTy);
1277
1278 if (is_const)
1279 char_type.addConst();
1280
1281 return CompilerType(ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001282}
1283
Sean Callanan09ab4b72011-11-30 22:11:59 +00001284clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001285ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1286 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001287}
1288
Kate Stoneb9c1b512016-09-06 20:57:50 +00001289clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1290 clang::Decl *source_decl) {
1291 FileSystemOptions file_system_options;
1292 FileManager file_manager(file_system_options);
1293 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1294
1295 return importer.Import(source_decl);
Greg Clayton526e5af2010-11-13 03:52:47 +00001296}
1297
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1299 bool ignore_qualifiers) {
1300 ClangASTContext *ast =
1301 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1302 if (!ast || ast != type2.GetTypeSystem())
1303 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001304
Kate Stoneb9c1b512016-09-06 20:57:50 +00001305 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1306 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001307
Kate Stoneb9c1b512016-09-06 20:57:50 +00001308 QualType type1_qual = ClangUtil::GetQualType(type1);
1309 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001310
Kate Stoneb9c1b512016-09-06 20:57:50 +00001311 if (ignore_qualifiers) {
1312 type1_qual = type1_qual.getUnqualifiedType();
1313 type2_qual = type2_qual.getUnqualifiedType();
1314 }
1315
1316 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001317}
1318
Kate Stoneb9c1b512016-09-06 20:57:50 +00001319CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1320 if (clang::ObjCInterfaceDecl *interface_decl =
1321 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1322 return GetTypeForDecl(interface_decl);
1323 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1324 return GetTypeForDecl(tag_decl);
1325 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001326}
1327
Kate Stoneb9c1b512016-09-06 20:57:50 +00001328CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
1329 // No need to call the getASTContext() accessor (which can create the AST
1330 // if it isn't created yet, because we can't have created a decl in this
1331 // AST if our AST didn't already exist...
1332 ASTContext *ast = &decl->getASTContext();
1333 if (ast)
1334 return CompilerType(ast, ast->getTagDeclType(decl));
1335 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001336}
1337
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
1339 // No need to call the getASTContext() accessor (which can create the AST
1340 // if it isn't created yet, because we can't have created a decl in this
1341 // AST if our AST didn't already exist...
1342 ASTContext *ast = &decl->getASTContext();
1343 if (ast)
1344 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1345 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001346}
1347
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001348#pragma mark Structure, Unions, Classes
1349
Kate Stoneb9c1b512016-09-06 20:57:50 +00001350CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1351 AccessType access_type,
1352 const char *name, int kind,
1353 LanguageType language,
1354 ClangASTMetadata *metadata) {
1355 ASTContext *ast = getASTContext();
1356 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001357
Kate Stoneb9c1b512016-09-06 20:57:50 +00001358 if (decl_ctx == nullptr)
1359 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001360
Kate Stoneb9c1b512016-09-06 20:57:50 +00001361 if (language == eLanguageTypeObjC ||
1362 language == eLanguageTypeObjC_plus_plus) {
1363 bool isForwardDecl = true;
1364 bool isInternal = false;
1365 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1366 }
Greg Clayton9e409562010-07-28 02:04:09 +00001367
Kate Stoneb9c1b512016-09-06 20:57:50 +00001368 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1369 // we will need to update this code. I was told to currently always use
1370 // the CXXRecordDecl class since we often don't know from debug information
1371 // if something is struct or a class, so we default to always use the more
1372 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001373
Kate Stoneb9c1b512016-09-06 20:57:50 +00001374 bool is_anonymous = (!name) || (!name[0]);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001375
Kate Stoneb9c1b512016-09-06 20:57:50 +00001376 CXXRecordDecl *decl = CXXRecordDecl::Create(
1377 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1378 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1379
1380 if (is_anonymous)
1381 decl->setAnonymousStructOrUnion(true);
1382
1383 if (decl) {
1384 if (metadata)
1385 SetMetadata(ast, decl, *metadata);
1386
1387 if (access_type != eAccessNone)
1388 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1389
1390 if (decl_ctx)
1391 decl_ctx->addDecl(decl);
1392
1393 return CompilerType(ast, ast->getTagDeclType(decl));
1394 }
1395 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001396}
1397
Sean Callanan09e91ac2017-05-11 22:08:05 +00001398namespace {
1399 bool IsValueParam(const clang::TemplateArgument &argument) {
1400 return argument.getKind() == TemplateArgument::Integral;
1401 }
1402}
1403
Kate Stoneb9c1b512016-09-06 20:57:50 +00001404static TemplateParameterList *CreateTemplateParameterList(
1405 ASTContext *ast,
1406 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1407 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1408 const bool parameter_pack = false;
1409 const bool is_typename = false;
1410 const unsigned depth = 0;
Sean Callanan09e91ac2017-05-11 22:08:05 +00001411 const size_t num_template_params = template_param_infos.args.size();
1412 DeclContext *const decl_context =
1413 ast->getTranslationUnitDecl(); // Is this the right decl context?,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001414 for (size_t i = 0; i < num_template_params; ++i) {
1415 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001416
Kate Stoneb9c1b512016-09-06 20:57:50 +00001417 IdentifierInfo *identifier_info = nullptr;
1418 if (name && name[0])
1419 identifier_info = &ast->Idents.get(name);
Sean Callanan09e91ac2017-05-11 22:08:05 +00001420 if (IsValueParam(template_param_infos.args[i])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001421 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001422 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1424 template_param_infos.args[i].getIntegralType(), parameter_pack,
1425 nullptr));
1426
1427 } else {
1428 template_param_decls.push_back(TemplateTypeParmDecl::Create(
Sean Callanan09e91ac2017-05-11 22:08:05 +00001429 *ast, decl_context,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001430 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1431 is_typename, parameter_pack));
1432 }
1433 }
Eugene Zemtsova9d928c2017-09-29 03:15:08 +00001434
Sean Callanan09e91ac2017-05-11 22:08:05 +00001435 if (template_param_infos.packed_args &&
1436 template_param_infos.packed_args->args.size()) {
1437 IdentifierInfo *identifier_info = nullptr;
1438 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1439 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1440 const bool parameter_pack_true = true;
1441 if (IsValueParam(template_param_infos.packed_args->args[0])) {
1442 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1443 *ast, decl_context,
1444 SourceLocation(), SourceLocation(), depth, num_template_params,
1445 identifier_info,
1446 template_param_infos.packed_args->args[0].getIntegralType(),
1447 parameter_pack_true, nullptr));
1448 } else {
1449 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1450 *ast, decl_context,
1451 SourceLocation(), SourceLocation(), depth, num_template_params,
1452 identifier_info,
1453 is_typename, parameter_pack_true));
1454 }
1455 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001456 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1457 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1458 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1459 SourceLocation(), requires_clause);
1460 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001461}
1462
Kate Stoneb9c1b512016-09-06 20:57:50 +00001463clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1464 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1465 const char *name, const TemplateParameterInfos &template_param_infos) {
1466 // /// \brief Create a function template node.
1467 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001468
Kate Stoneb9c1b512016-09-06 20:57:50 +00001469 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001470
Kate Stoneb9c1b512016-09-06 20:57:50 +00001471 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1472 ast, template_param_infos, template_param_decls);
1473 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1474 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1475 template_param_list, func_decl);
1476
1477 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1478 i < template_param_decl_count; ++i) {
1479 // TODO: verify which decl context we should put template_param_decls into..
1480 template_param_decls[i]->setDeclContext(func_decl);
1481 }
1482
1483 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001484}
1485
Kate Stoneb9c1b512016-09-06 20:57:50 +00001486void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1487 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1488 const TemplateParameterInfos &infos) {
1489 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001490
Kate Stoneb9c1b512016-09-06 20:57:50 +00001491 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1492 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001493}
1494
Kate Stoneb9c1b512016-09-06 20:57:50 +00001495ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1496 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1497 int kind, const TemplateParameterInfos &template_param_infos) {
1498 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001499
Kate Stoneb9c1b512016-09-06 20:57:50 +00001500 ClassTemplateDecl *class_template_decl = nullptr;
1501 if (decl_ctx == nullptr)
1502 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001503
Kate Stoneb9c1b512016-09-06 20:57:50 +00001504 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1505 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001506
Kate Stoneb9c1b512016-09-06 20:57:50 +00001507 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001508
Kate Stoneb9c1b512016-09-06 20:57:50 +00001509 for (NamedDecl *decl : result) {
1510 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001511 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001512 return class_template_decl;
1513 }
1514
1515 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1516
1517 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1518 ast, template_param_infos, template_param_decls);
1519
1520 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1521 *ast, (TagDecl::TagKind)kind,
1522 decl_ctx, // What decl context do we use here? TU? The actual decl
1523 // context?
1524 SourceLocation(), SourceLocation(), &identifier_info);
1525
1526 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1527 i < template_param_decl_count; ++i) {
1528 template_param_decls[i]->setDeclContext(template_cxx_decl);
1529 }
1530
1531 // With templated classes, we say that a class is templated with
1532 // specializations, but that the bare class has no functions.
1533 // template_cxx_decl->startDefinition();
1534 // template_cxx_decl->completeDefinition();
1535
1536 class_template_decl = ClassTemplateDecl::Create(
1537 *ast,
1538 decl_ctx, // What decl context do we use here? TU? The actual decl
1539 // context?
Pavel Labath4294de32017-01-12 10:44:16 +00001540 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001541
1542 if (class_template_decl) {
1543 if (access_type != eAccessNone)
1544 class_template_decl->setAccess(
1545 ConvertAccessTypeToAccessSpecifier(access_type));
1546
1547 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1548 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1549
1550 decl_ctx->addDecl(class_template_decl);
1551
Sean Callanan5e9e1992011-10-26 01:06:27 +00001552#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001553 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001554#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001555 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001556
Kate Stoneb9c1b512016-09-06 20:57:50 +00001557 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001558}
1559
Greg Claytonf0705c82011-10-22 03:33:13 +00001560ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001561ClangASTContext::CreateClassTemplateSpecializationDecl(
1562 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1563 const TemplateParameterInfos &template_param_infos) {
1564 ASTContext *ast = getASTContext();
Sean Callanan09e91ac2017-05-11 22:08:05 +00001565 llvm::SmallVector<clang::TemplateArgument, 2> args(
1566 template_param_infos.args.size() +
1567 (template_param_infos.packed_args ? 1 : 0));
1568 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1569 args.begin());
1570 if (template_param_infos.packed_args) {
1571 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1572 *ast, template_param_infos.packed_args->args);
1573 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001574 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1575 ClassTemplateSpecializationDecl::Create(
1576 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
Sean Callanan09e91ac2017-05-11 22:08:05 +00001577 SourceLocation(), class_template_decl, args,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001578 nullptr);
1579
1580 class_template_specialization_decl->setSpecializationKind(
1581 TSK_ExplicitSpecialization);
1582
1583 return class_template_specialization_decl;
1584}
1585
1586CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1587 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1588 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001589 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001590 if (ast)
1591 return CompilerType(
1592 ast, ast->getTagDeclType(class_template_specialization_decl));
1593 }
1594 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001595}
1596
Kate Stoneb9c1b512016-09-06 20:57:50 +00001597static inline bool check_op_param(bool is_method,
1598 clang::OverloadedOperatorKind op_kind,
1599 bool unary, bool binary,
1600 uint32_t num_params) {
1601 // Special-case call since it can take any number of operands
1602 if (op_kind == OO_Call)
1603 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001604
Kate Stoneb9c1b512016-09-06 20:57:50 +00001605 // The parameter count doesn't include "this"
1606 if (is_method)
1607 ++num_params;
1608 if (num_params == 1)
1609 return unary;
1610 if (num_params == 2)
1611 return binary;
1612 else
Greg Clayton090d0982011-06-19 03:43:27 +00001613 return false;
1614}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001615
Kate Stoneb9c1b512016-09-06 20:57:50 +00001616bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1617 bool is_method, clang::OverloadedOperatorKind op_kind,
1618 uint32_t num_params) {
1619 switch (op_kind) {
1620 default:
1621 break;
1622 // C++ standard allows any number of arguments to new/delete
1623 case OO_New:
1624 case OO_Array_New:
1625 case OO_Delete:
1626 case OO_Array_Delete:
1627 return true;
1628 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001629
Kate Stoneb9c1b512016-09-06 20:57:50 +00001630#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1631 case OO_##Name: \
1632 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1633 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001634#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001635 default:
1636 break;
1637 }
1638 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001639}
1640
Greg Clayton57ee3062013-07-11 22:46:58 +00001641clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001642ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1643 clang::AccessSpecifier rhs) {
1644 // Make the access equal to the stricter of the field and the nested field's
1645 // access
1646 if (lhs == AS_none || rhs == AS_none)
1647 return AS_none;
1648 if (lhs == AS_private || rhs == AS_private)
1649 return AS_private;
1650 if (lhs == AS_protected || rhs == AS_protected)
1651 return AS_protected;
1652 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001653}
1654
Kate Stoneb9c1b512016-09-06 20:57:50 +00001655bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1656 uint32_t &bitfield_bit_size) {
1657 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001658}
1659
Kate Stoneb9c1b512016-09-06 20:57:50 +00001660bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1661 uint32_t &bitfield_bit_size) {
1662 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001663 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001664
Kate Stoneb9c1b512016-09-06 20:57:50 +00001665 if (field->isBitField()) {
1666 Expr *bit_width_expr = field->getBitWidth();
1667 if (bit_width_expr) {
1668 llvm::APSInt bit_width_apsint;
1669 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1670 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001671 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001672 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001673 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001674 }
1675 return false;
1676}
1677
1678bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1679 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001680 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001681
1682 if (!record_decl->field_empty())
1683 return true;
1684
1685 // No fields, lets check this is a CXX record and check the base classes
1686 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1687 if (cxx_record_decl) {
1688 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1689 for (base_class = cxx_record_decl->bases_begin(),
1690 base_class_end = cxx_record_decl->bases_end();
1691 base_class != base_class_end; ++base_class) {
1692 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1693 base_class->getType()->getAs<RecordType>()->getDecl());
1694 if (RecordHasFields(base_class_decl))
1695 return true;
1696 }
1697 }
1698 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001699}
1700
Greg Clayton8cf05932010-07-22 18:30:50 +00001701#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001702
Kate Stoneb9c1b512016-09-06 20:57:50 +00001703CompilerType ClangASTContext::CreateObjCClass(const char *name,
1704 DeclContext *decl_ctx,
1705 bool isForwardDecl,
1706 bool isInternal,
1707 ClangASTMetadata *metadata) {
1708 ASTContext *ast = getASTContext();
1709 assert(ast != nullptr);
1710 assert(name && name[0]);
1711 if (decl_ctx == nullptr)
1712 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001713
Kate Stoneb9c1b512016-09-06 20:57:50 +00001714 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1715 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1716 nullptr, SourceLocation(),
1717 /*isForwardDecl,*/
1718 isInternal);
1719
1720 if (decl && metadata)
1721 SetMetadata(ast, decl, *metadata);
1722
1723 return CompilerType(ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001724}
1725
Kate Stoneb9c1b512016-09-06 20:57:50 +00001726static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1727 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) ==
1728 false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001729}
1730
Greg Clayton57ee3062013-07-11 22:46:58 +00001731uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001732ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1733 bool omit_empty_base_classes) {
1734 uint32_t num_bases = 0;
1735 if (cxx_record_decl) {
1736 if (omit_empty_base_classes) {
1737 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1738 for (base_class = cxx_record_decl->bases_begin(),
1739 base_class_end = cxx_record_decl->bases_end();
1740 base_class != base_class_end; ++base_class) {
1741 // Skip empty base classes
1742 if (omit_empty_base_classes) {
1743 if (BaseSpecifierIsEmpty(base_class))
1744 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001745 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001746 ++num_bases;
1747 }
1748 } else
1749 num_bases = cxx_record_decl->getNumBases();
1750 }
1751 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001752}
1753
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001754#pragma mark Namespace Declarations
1755
1756NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001757ClangASTContext::GetUniqueNamespaceDeclaration(const char *name,
1758 DeclContext *decl_ctx) {
1759 NamespaceDecl *namespace_decl = nullptr;
1760 ASTContext *ast = getASTContext();
1761 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1762 if (decl_ctx == nullptr)
1763 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001764
Kate Stoneb9c1b512016-09-06 20:57:50 +00001765 if (name) {
1766 IdentifierInfo &identifier_info = ast->Idents.get(name);
1767 DeclarationName decl_name(&identifier_info);
1768 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1769 for (NamedDecl *decl : result) {
1770 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1771 if (namespace_decl)
1772 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001773 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001774
1775 namespace_decl =
1776 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1777 SourceLocation(), &identifier_info, nullptr);
1778
1779 decl_ctx->addDecl(namespace_decl);
1780 } else {
1781 if (decl_ctx == translation_unit_decl) {
1782 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1783 if (namespace_decl)
1784 return namespace_decl;
1785
1786 namespace_decl =
1787 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1788 SourceLocation(), nullptr, nullptr);
1789 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1790 translation_unit_decl->addDecl(namespace_decl);
1791 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1792 } else {
1793 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1794 if (parent_namespace_decl) {
1795 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1796 if (namespace_decl)
1797 return namespace_decl;
1798 namespace_decl =
1799 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1800 SourceLocation(), nullptr, nullptr);
1801 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1802 parent_namespace_decl->addDecl(namespace_decl);
1803 assert(namespace_decl ==
1804 parent_namespace_decl->getAnonymousNamespace());
1805 } else {
1806 // BAD!!!
1807 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001808 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001809 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001810#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001811 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001812#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001813 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001814}
1815
Kate Stoneb9c1b512016-09-06 20:57:50 +00001816NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1817 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) {
1818 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1819 if (ast_ctx == nullptr)
1820 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +00001821
Kate Stoneb9c1b512016-09-06 20:57:50 +00001822 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
Siva Chandra03ff5c82016-02-05 19:10:04 +00001823}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001824
Paul Hermand628cbb2015-09-15 23:44:17 +00001825clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001826ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1827 if (ctx != nullptr) {
1828 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1829 clang::SourceLocation());
1830 ctx->addDecl(decl);
1831 return decl;
1832 }
1833 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001834}
1835
Kate Stoneb9c1b512016-09-06 20:57:50 +00001836clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1837 clang::DeclContext *right,
1838 clang::DeclContext *root) {
1839 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001840 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001841
1842 std::set<clang::DeclContext *> path_left;
1843 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1844 path_left.insert(d);
1845
1846 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1847 if (path_left.find(d) != path_left.end())
1848 return d;
1849
1850 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00001851}
1852
Kate Stoneb9c1b512016-09-06 20:57:50 +00001853clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1854 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1855 if (decl_ctx != nullptr && ns_decl != nullptr) {
1856 clang::TranslationUnitDecl *translation_unit =
1857 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1858 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1859 *getASTContext(), decl_ctx, clang::SourceLocation(),
1860 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1861 clang::SourceLocation(), ns_decl,
1862 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
1863 decl_ctx->addDecl(using_decl);
1864 return using_decl;
1865 }
1866 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001867}
1868
1869clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001870ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1871 clang::NamedDecl *target) {
1872 if (current_decl_ctx != nullptr && target != nullptr) {
1873 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1874 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
1875 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1876 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1877 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
1878 target);
1879 using_decl->addShadowDecl(shadow_decl);
1880 current_decl_ctx->addDecl(using_decl);
1881 return using_decl;
1882 }
1883 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001884}
1885
Kate Stoneb9c1b512016-09-06 20:57:50 +00001886clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
1887 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
1888 if (decl_context != nullptr) {
1889 clang::VarDecl *var_decl = clang::VarDecl::Create(
1890 *getASTContext(), decl_context, clang::SourceLocation(),
1891 clang::SourceLocation(),
1892 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
1893 nullptr, clang::SC_None);
1894 var_decl->setAccess(clang::AS_public);
1895 decl_context->addDecl(var_decl);
1896 return var_decl;
1897 }
1898 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001899}
1900
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001901lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001902ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
1903 lldb::BasicType basic_type) {
1904 switch (basic_type) {
1905 case eBasicTypeVoid:
1906 return ast->VoidTy.getAsOpaquePtr();
1907 case eBasicTypeChar:
1908 return ast->CharTy.getAsOpaquePtr();
1909 case eBasicTypeSignedChar:
1910 return ast->SignedCharTy.getAsOpaquePtr();
1911 case eBasicTypeUnsignedChar:
1912 return ast->UnsignedCharTy.getAsOpaquePtr();
1913 case eBasicTypeWChar:
1914 return ast->getWCharType().getAsOpaquePtr();
1915 case eBasicTypeSignedWChar:
1916 return ast->getSignedWCharType().getAsOpaquePtr();
1917 case eBasicTypeUnsignedWChar:
1918 return ast->getUnsignedWCharType().getAsOpaquePtr();
1919 case eBasicTypeChar16:
1920 return ast->Char16Ty.getAsOpaquePtr();
1921 case eBasicTypeChar32:
1922 return ast->Char32Ty.getAsOpaquePtr();
1923 case eBasicTypeShort:
1924 return ast->ShortTy.getAsOpaquePtr();
1925 case eBasicTypeUnsignedShort:
1926 return ast->UnsignedShortTy.getAsOpaquePtr();
1927 case eBasicTypeInt:
1928 return ast->IntTy.getAsOpaquePtr();
1929 case eBasicTypeUnsignedInt:
1930 return ast->UnsignedIntTy.getAsOpaquePtr();
1931 case eBasicTypeLong:
1932 return ast->LongTy.getAsOpaquePtr();
1933 case eBasicTypeUnsignedLong:
1934 return ast->UnsignedLongTy.getAsOpaquePtr();
1935 case eBasicTypeLongLong:
1936 return ast->LongLongTy.getAsOpaquePtr();
1937 case eBasicTypeUnsignedLongLong:
1938 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1939 case eBasicTypeInt128:
1940 return ast->Int128Ty.getAsOpaquePtr();
1941 case eBasicTypeUnsignedInt128:
1942 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1943 case eBasicTypeBool:
1944 return ast->BoolTy.getAsOpaquePtr();
1945 case eBasicTypeHalf:
1946 return ast->HalfTy.getAsOpaquePtr();
1947 case eBasicTypeFloat:
1948 return ast->FloatTy.getAsOpaquePtr();
1949 case eBasicTypeDouble:
1950 return ast->DoubleTy.getAsOpaquePtr();
1951 case eBasicTypeLongDouble:
1952 return ast->LongDoubleTy.getAsOpaquePtr();
1953 case eBasicTypeFloatComplex:
1954 return ast->FloatComplexTy.getAsOpaquePtr();
1955 case eBasicTypeDoubleComplex:
1956 return ast->DoubleComplexTy.getAsOpaquePtr();
1957 case eBasicTypeLongDoubleComplex:
1958 return ast->LongDoubleComplexTy.getAsOpaquePtr();
1959 case eBasicTypeObjCID:
1960 return ast->getObjCIdType().getAsOpaquePtr();
1961 case eBasicTypeObjCClass:
1962 return ast->getObjCClassType().getAsOpaquePtr();
1963 case eBasicTypeObjCSel:
1964 return ast->getObjCSelType().getAsOpaquePtr();
1965 case eBasicTypeNullPtr:
1966 return ast->NullPtrTy.getAsOpaquePtr();
1967 default:
1968 return nullptr;
1969 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001970}
1971
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001972#pragma mark Function Types
1973
Pavel Labath1ac2b202016-08-15 14:32:32 +00001974clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00001975ClangASTContext::GetDeclarationName(const char *name,
1976 const CompilerType &function_clang_type) {
1977 if (!name || !name[0])
1978 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00001979
Kate Stoneb9c1b512016-09-06 20:57:50 +00001980 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
1981 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
1982 return DeclarationName(&getASTContext()->Idents.get(
1983 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00001984
Kate Stoneb9c1b512016-09-06 20:57:50 +00001985 // Check the number of operator parameters. Sometimes we have
1986 // seen bad DWARF that doesn't correctly describe operators and
1987 // if we try to create a method and add it to the class, clang
1988 // will assert and crash, so we need to make sure things are
1989 // acceptable.
1990 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
1991 const clang::FunctionProtoType *function_type =
1992 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
1993 if (function_type == nullptr)
1994 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00001995
Kate Stoneb9c1b512016-09-06 20:57:50 +00001996 const bool is_method = false;
1997 const unsigned int num_params = function_type->getNumParams();
1998 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1999 is_method, op_kind, num_params))
2000 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00002001
Kate Stoneb9c1b512016-09-06 20:57:50 +00002002 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00002003}
2004
Kate Stoneb9c1b512016-09-06 20:57:50 +00002005FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
2006 DeclContext *decl_ctx, const char *name,
2007 const CompilerType &function_clang_type, int storage, bool is_inline) {
2008 FunctionDecl *func_decl = nullptr;
2009 ASTContext *ast = getASTContext();
2010 if (decl_ctx == nullptr)
2011 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002012
Kate Stoneb9c1b512016-09-06 20:57:50 +00002013 const bool hasWrittenPrototype = true;
2014 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00002015
Kate Stoneb9c1b512016-09-06 20:57:50 +00002016 clang::DeclarationName declarationName =
2017 GetDeclarationName(name, function_clang_type);
2018 func_decl = FunctionDecl::Create(
2019 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2020 ClangUtil::GetQualType(function_clang_type), nullptr,
2021 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
2022 isConstexprSpecified);
2023 if (func_decl)
2024 decl_ctx->addDecl(func_decl);
2025
Sean Callanan5e9e1992011-10-26 01:06:27 +00002026#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00002027 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002028#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002029
2030 return func_decl;
2031}
2032
2033CompilerType ClangASTContext::CreateFunctionType(
2034 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
2035 unsigned num_args, bool is_variadic, unsigned type_quals) {
2036 if (ast == nullptr)
2037 return CompilerType(); // invalid AST
2038
2039 if (!result_type || !ClangUtil::IsClangType(result_type))
2040 return CompilerType(); // invalid return type
2041
2042 std::vector<QualType> qual_type_args;
2043 if (num_args > 0 && args == nullptr)
2044 return CompilerType(); // invalid argument array passed in
2045
2046 // Verify that all arguments are valid and the right type
2047 for (unsigned i = 0; i < num_args; ++i) {
2048 if (args[i]) {
2049 // Make sure we have a clang type in args[i] and not a type from another
2050 // language whose name might match
2051 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2052 lldbassert(is_clang_type);
2053 if (is_clang_type)
2054 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2055 else
2056 return CompilerType(); // invalid argument type (must be a clang type)
2057 } else
2058 return CompilerType(); // invalid argument type (empty)
2059 }
2060
2061 // TODO: Detect calling convention in DWARF?
2062 FunctionProtoType::ExtProtoInfo proto_info;
2063 proto_info.Variadic = is_variadic;
2064 proto_info.ExceptionSpec = EST_None;
2065 proto_info.TypeQuals = type_quals;
2066 proto_info.RefQualifier = RQ_None;
2067
2068 return CompilerType(ast,
2069 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2070 qual_type_args, proto_info));
2071}
2072
2073ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
2074 const char *name, const CompilerType &param_type, int storage) {
2075 ASTContext *ast = getASTContext();
2076 assert(ast != nullptr);
2077 return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(),
2078 SourceLocation(), SourceLocation(),
2079 name && name[0] ? &ast->Idents.get(name) : nullptr,
2080 ClangUtil::GetQualType(param_type), nullptr,
2081 (clang::StorageClass)storage, nullptr);
2082}
2083
2084void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2085 ParmVarDecl **params,
2086 unsigned num_params) {
2087 if (function_decl)
2088 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002089}
2090
Greg Claytona1e5dc82015-08-11 22:53:00 +00002091CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002092ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
2093 QualType block_type = m_ast_ap->getBlockPointerType(
2094 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002095
Kate Stoneb9c1b512016-09-06 20:57:50 +00002096 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002097}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002098
2099#pragma mark Array Types
2100
Kate Stoneb9c1b512016-09-06 20:57:50 +00002101CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2102 size_t element_count,
2103 bool is_vector) {
2104 if (element_type.IsValid()) {
2105 ASTContext *ast = getASTContext();
2106 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002107
Kate Stoneb9c1b512016-09-06 20:57:50 +00002108 if (is_vector) {
2109 return CompilerType(
2110 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2111 element_count));
2112 } else {
2113
2114 llvm::APInt ap_element_count(64, element_count);
2115 if (element_count == 0) {
2116 return CompilerType(ast, ast->getIncompleteArrayType(
2117 ClangUtil::GetQualType(element_type),
2118 clang::ArrayType::Normal, 0));
2119 } else {
2120 return CompilerType(
2121 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2122 ap_element_count,
2123 clang::ArrayType::Normal, 0));
2124 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002125 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002126 }
2127 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002128}
2129
Kate Stoneb9c1b512016-09-06 20:57:50 +00002130CompilerType ClangASTContext::CreateStructForIdentifier(
2131 const ConstString &type_name,
2132 const std::initializer_list<std::pair<const char *, CompilerType>>
2133 &type_fields,
2134 bool packed) {
2135 CompilerType type;
2136 if (!type_name.IsEmpty() &&
2137 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2138 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002139 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002140 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002141 }
2142
2143 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2144 clang::TTK_Struct, lldb::eLanguageTypeC);
2145 StartTagDeclarationDefinition(type);
2146 for (const auto &field : type_fields)
2147 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2148 0);
2149 if (packed)
2150 SetIsPacked(type);
2151 CompleteTagDeclarationDefinition(type);
2152 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002153}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002154
Kate Stoneb9c1b512016-09-06 20:57:50 +00002155CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
2156 const ConstString &type_name,
2157 const std::initializer_list<std::pair<const char *, CompilerType>>
2158 &type_fields,
2159 bool packed) {
2160 CompilerType type;
2161 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2162 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002163
Kate Stoneb9c1b512016-09-06 20:57:50 +00002164 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002165}
2166
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002167#pragma mark Enumeration Types
2168
Greg Claytona1e5dc82015-08-11 22:53:00 +00002169CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002170ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2171 const Declaration &decl,
Tamas Berghammer59765832017-11-07 10:39:22 +00002172 const CompilerType &integer_clang_type,
2173 bool is_scoped) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002174 // TODO: Do something intelligent with the Declaration object passed in
2175 // like maybe filling in the SourceLocation with it...
2176 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002177
Kate Stoneb9c1b512016-09-06 20:57:50 +00002178 // TODO: ask about these...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002179 // const bool IsFixed = false;
2180
2181 EnumDecl *enum_decl = EnumDecl::Create(
2182 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2183 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
Tamas Berghammercf6bf4c2017-11-07 13:43:55 +00002184 is_scoped, // IsScoped
2185 is_scoped, // IsScopedUsingClassTag
2186 false); // IsFixed
Kate Stoneb9c1b512016-09-06 20:57:50 +00002187
2188 if (enum_decl) {
2189 // TODO: check if we should be setting the promotion type too?
2190 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2191
2192 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2193
2194 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2195 }
2196 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002197}
2198
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002199// Disable this for now since I can't seem to get a nicely formatted float
2200// out of the APFloat class without just getting the float, double or quad
2201// and then using a formatted print on it which defeats the purpose. We ideally
2202// would like to get perfect string values for any kind of float semantics
2203// so we can support remote targets. The code below also requires a patch to
2204// llvm::APInt.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002205// bool
2206// ClangASTContext::ConvertFloatValueToString (ASTContext *ast,
2207// lldb::opaque_compiler_type_t clang_type, const uint8_t* bytes, size_t
2208// byte_size, int apint_byte_order, std::string &float_str)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002209//{
2210// uint32_t count = 0;
2211// bool is_complex = false;
2212// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
2213// {
2214// unsigned num_bytes_per_float = byte_size / count;
2215// unsigned num_bits_per_float = num_bytes_per_float * 8;
2216//
2217// float_str.clear();
2218// uint32_t i;
2219// for (i=0; i<count; i++)
2220// {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002221// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float,
2222// (APInt::ByteOrder)apint_byte_order);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002223// bool is_ieee = false;
2224// APFloat ap_float(ap_int, is_ieee);
2225// char s[1024];
2226// unsigned int hex_digits = 0;
2227// bool upper_case = false;
2228//
Kate Stoneb9c1b512016-09-06 20:57:50 +00002229// if (ap_float.convertToHexString(s, hex_digits, upper_case,
2230// APFloat::rmNearestTiesToEven) > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002231// {
2232// if (i > 0)
2233// float_str.append(", ");
2234// float_str.append(s);
2235// if (i == 1 && is_complex)
2236// float_str.append(1, 'i');
2237// }
2238// }
2239// return !float_str.empty();
2240// }
2241// return false;
2242//}
2243
Kate Stoneb9c1b512016-09-06 20:57:50 +00002244CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2245 size_t bit_size,
2246 bool is_signed) {
2247 if (ast) {
2248 if (is_signed) {
2249 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2250 return CompilerType(ast, ast->SignedCharTy);
2251
2252 if (bit_size == ast->getTypeSize(ast->ShortTy))
2253 return CompilerType(ast, ast->ShortTy);
2254
2255 if (bit_size == ast->getTypeSize(ast->IntTy))
2256 return CompilerType(ast, ast->IntTy);
2257
2258 if (bit_size == ast->getTypeSize(ast->LongTy))
2259 return CompilerType(ast, ast->LongTy);
2260
2261 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2262 return CompilerType(ast, ast->LongLongTy);
2263
2264 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2265 return CompilerType(ast, ast->Int128Ty);
2266 } else {
2267 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2268 return CompilerType(ast, ast->UnsignedCharTy);
2269
2270 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2271 return CompilerType(ast, ast->UnsignedShortTy);
2272
2273 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2274 return CompilerType(ast, ast->UnsignedIntTy);
2275
2276 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2277 return CompilerType(ast, ast->UnsignedLongTy);
2278
2279 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2280 return CompilerType(ast, ast->UnsignedLongLongTy);
2281
2282 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2283 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002284 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002285 }
2286 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002287}
2288
Kate Stoneb9c1b512016-09-06 20:57:50 +00002289CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2290 bool is_signed) {
2291 if (ast)
2292 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2293 is_signed);
2294 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002295}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002296
Kate Stoneb9c1b512016-09-06 20:57:50 +00002297void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2298 if (decl_ctx) {
2299 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002300
Kate Stoneb9c1b512016-09-06 20:57:50 +00002301 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2302 if (named_decl) {
2303 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2304 named_decl->getDeclName().getAsString().c_str());
2305 } else {
2306 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002307 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002308 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002309}
2310
Kate Stoneb9c1b512016-09-06 20:57:50 +00002311void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2312 if (decl == nullptr)
2313 return;
2314 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002315
Kate Stoneb9c1b512016-09-06 20:57:50 +00002316 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2317 if (record_decl) {
2318 printf("%20s: %s%s\n", decl->getDeclKindName(),
2319 record_decl->getDeclName().getAsString().c_str(),
2320 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002321
Kate Stoneb9c1b512016-09-06 20:57:50 +00002322 } else {
2323 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2324 if (named_decl) {
2325 printf("%20s: %s\n", decl->getDeclKindName(),
2326 named_decl->getDeclName().getAsString().c_str());
2327 } else {
2328 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002329 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002330 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002331}
2332
Kate Stoneb9c1b512016-09-06 20:57:50 +00002333bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2334 clang::Decl *rhs_decl) {
2335 if (lhs_decl && rhs_decl) {
2336 //----------------------------------------------------------------------
2337 // Make sure the decl kinds match first
2338 //----------------------------------------------------------------------
2339 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2340 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002341
Kate Stoneb9c1b512016-09-06 20:57:50 +00002342 if (lhs_decl_kind == rhs_decl_kind) {
2343 //------------------------------------------------------------------
2344 // Now check that the decl contexts kinds are all equivalent
2345 // before we have to check any names of the decl contexts...
2346 //------------------------------------------------------------------
2347 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2348 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2349 if (lhs_decl_ctx && rhs_decl_ctx) {
2350 while (1) {
2351 if (lhs_decl_ctx && rhs_decl_ctx) {
2352 const clang::Decl::Kind lhs_decl_ctx_kind =
2353 lhs_decl_ctx->getDeclKind();
2354 const clang::Decl::Kind rhs_decl_ctx_kind =
2355 rhs_decl_ctx->getDeclKind();
2356 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2357 lhs_decl_ctx = lhs_decl_ctx->getParent();
2358 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002359
Kate Stoneb9c1b512016-09-06 20:57:50 +00002360 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2361 break;
2362 } else
2363 return false;
2364 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002365 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002366 }
2367
2368 //--------------------------------------------------------------
2369 // Now make sure the name of the decls match
2370 //--------------------------------------------------------------
2371 clang::NamedDecl *lhs_named_decl =
2372 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2373 clang::NamedDecl *rhs_named_decl =
2374 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2375 if (lhs_named_decl && rhs_named_decl) {
2376 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2377 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2378 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2379 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2380 return false;
2381 } else
Greg Claytona2721472011-06-25 00:44:06 +00002382 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002383 } else
2384 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002385
Kate Stoneb9c1b512016-09-06 20:57:50 +00002386 //--------------------------------------------------------------
2387 // We know that the decl context kinds all match, so now we need
2388 // to make sure the names match as well
2389 //--------------------------------------------------------------
2390 lhs_decl_ctx = lhs_decl->getDeclContext();
2391 rhs_decl_ctx = rhs_decl->getDeclContext();
2392 while (1) {
2393 switch (lhs_decl_ctx->getDeclKind()) {
2394 case clang::Decl::TranslationUnit:
2395 // We don't care about the translation unit names
2396 return true;
2397 default: {
2398 clang::NamedDecl *lhs_named_decl =
2399 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2400 clang::NamedDecl *rhs_named_decl =
2401 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2402 if (lhs_named_decl && rhs_named_decl) {
2403 clang::DeclarationName lhs_decl_name =
2404 lhs_named_decl->getDeclName();
2405 clang::DeclarationName rhs_decl_name =
2406 rhs_named_decl->getDeclName();
2407 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2408 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2409 return false;
2410 } else
2411 return false;
2412 } else
2413 return false;
2414 } break;
2415 }
2416 lhs_decl_ctx = lhs_decl_ctx->getParent();
2417 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002418 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002419 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002420 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002421 }
2422 return false;
2423}
2424bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2425 clang::Decl *decl) {
2426 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002427 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002428
Kate Stoneb9c1b512016-09-06 20:57:50 +00002429 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002430
Kate Stoneb9c1b512016-09-06 20:57:50 +00002431 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002432 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002433
2434 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2435 if (tag_decl->isCompleteDefinition())
2436 return true;
2437
2438 if (!tag_decl->hasExternalLexicalStorage())
2439 return false;
2440
2441 ast_source->CompleteType(tag_decl);
2442
2443 return !tag_decl->getTypeForDecl()->isIncompleteType();
2444 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2445 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2446 if (objc_interface_decl->getDefinition())
2447 return true;
2448
2449 if (!objc_interface_decl->hasExternalLexicalStorage())
2450 return false;
2451
2452 ast_source->CompleteType(objc_interface_decl);
2453
2454 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2455 } else {
2456 return false;
2457 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002458}
2459
Kate Stoneb9c1b512016-09-06 20:57:50 +00002460void ClangASTContext::SetMetadataAsUserID(const void *object,
2461 user_id_t user_id) {
2462 ClangASTMetadata meta_data;
2463 meta_data.SetUserID(user_id);
2464 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002465}
2466
Kate Stoneb9c1b512016-09-06 20:57:50 +00002467void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2468 ClangASTMetadata &metadata) {
2469 ClangExternalASTSourceCommon *external_source =
2470 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2471
2472 if (external_source)
2473 external_source->SetMetadata(object, metadata);
2474}
2475
2476ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2477 const void *object) {
2478 ClangExternalASTSourceCommon *external_source =
2479 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2480
2481 if (external_source && external_source->HasMetadata(object))
2482 return external_source->GetMetadata(object);
2483 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002484 return nullptr;
2485}
2486
Kate Stoneb9c1b512016-09-06 20:57:50 +00002487clang::DeclContext *
2488ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2489 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2490}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002491
Kate Stoneb9c1b512016-09-06 20:57:50 +00002492clang::DeclContext *
2493ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2494 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2495}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002496
Kate Stoneb9c1b512016-09-06 20:57:50 +00002497bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2498 int kind) const {
2499 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2500 if (clang_type) {
2501 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2502 if (tag_type) {
2503 clang::TagDecl *tag_decl =
2504 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2505 if (tag_decl) {
2506 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2507 return true;
2508 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002509 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002510 }
2511 return false;
2512}
2513
2514bool ClangASTContext::SetDefaultAccessForRecordFields(
2515 clang::RecordDecl *record_decl, int default_accessibility,
2516 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2517 if (record_decl) {
2518 uint32_t field_idx;
2519 clang::RecordDecl::field_iterator field, field_end;
2520 for (field = record_decl->field_begin(),
2521 field_end = record_decl->field_end(), field_idx = 0;
2522 field != field_end; ++field, ++field_idx) {
2523 // If no accessibility was assigned, assign the correct one
2524 if (field_idx < num_assigned_accessibilities &&
2525 assigned_accessibilities[field_idx] == clang::AS_none)
2526 field->setAccess((clang::AccessSpecifier)default_accessibility);
2527 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002528 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002529 }
2530 return false;
2531}
2532
2533clang::DeclContext *
2534ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2535 return GetDeclContextForType(ClangUtil::GetQualType(type));
2536}
2537
2538clang::DeclContext *
2539ClangASTContext::GetDeclContextForType(clang::QualType type) {
2540 if (type.isNull())
2541 return nullptr;
2542
2543 clang::QualType qual_type = type.getCanonicalType();
2544 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2545 switch (type_class) {
2546 case clang::Type::ObjCInterface:
2547 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2548 ->getInterface();
2549 case clang::Type::ObjCObjectPointer:
2550 return GetDeclContextForType(
2551 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2552 ->getPointeeType());
2553 case clang::Type::Record:
2554 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2555 case clang::Type::Enum:
2556 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2557 case clang::Type::Typedef:
2558 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2559 ->getDecl()
2560 ->getUnderlyingType());
2561 case clang::Type::Auto:
2562 return GetDeclContextForType(
2563 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2564 case clang::Type::Elaborated:
2565 return GetDeclContextForType(
2566 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2567 case clang::Type::Paren:
2568 return GetDeclContextForType(
2569 llvm::cast<clang::ParenType>(qual_type)->desugar());
2570 default:
2571 break;
2572 }
2573 // No DeclContext in this type...
2574 return nullptr;
2575}
2576
2577static bool GetCompleteQualType(clang::ASTContext *ast,
2578 clang::QualType qual_type,
2579 bool allow_completion = true) {
2580 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2581 switch (type_class) {
2582 case clang::Type::ConstantArray:
2583 case clang::Type::IncompleteArray:
2584 case clang::Type::VariableArray: {
2585 const clang::ArrayType *array_type =
2586 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2587
2588 if (array_type)
2589 return GetCompleteQualType(ast, array_type->getElementType(),
2590 allow_completion);
2591 } break;
2592 case clang::Type::Record: {
2593 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2594 if (cxx_record_decl) {
2595 if (cxx_record_decl->hasExternalLexicalStorage()) {
2596 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2597 const bool fields_loaded =
2598 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2599 if (is_complete && fields_loaded)
2600 return true;
2601
2602 if (!allow_completion)
2603 return false;
2604
2605 // Call the field_begin() accessor to for it to use the external source
2606 // to load the fields...
2607 clang::ExternalASTSource *external_ast_source =
2608 ast->getExternalSource();
2609 if (external_ast_source) {
2610 external_ast_source->CompleteType(cxx_record_decl);
2611 if (cxx_record_decl->isCompleteDefinition()) {
2612 cxx_record_decl->field_begin();
2613 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2614 }
2615 }
2616 }
2617 }
2618 const clang::TagType *tag_type =
2619 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2620 return !tag_type->isIncompleteType();
2621 } break;
2622
2623 case clang::Type::Enum: {
2624 const clang::TagType *tag_type =
2625 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2626 if (tag_type) {
2627 clang::TagDecl *tag_decl = tag_type->getDecl();
2628 if (tag_decl) {
2629 if (tag_decl->getDefinition())
2630 return true;
2631
2632 if (!allow_completion)
2633 return false;
2634
2635 if (tag_decl->hasExternalLexicalStorage()) {
2636 if (ast) {
2637 clang::ExternalASTSource *external_ast_source =
2638 ast->getExternalSource();
2639 if (external_ast_source) {
2640 external_ast_source->CompleteType(tag_decl);
2641 return !tag_type->isIncompleteType();
2642 }
2643 }
2644 }
2645 return false;
2646 }
2647 }
2648
2649 } break;
2650 case clang::Type::ObjCObject:
2651 case clang::Type::ObjCInterface: {
2652 const clang::ObjCObjectType *objc_class_type =
2653 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2654 if (objc_class_type) {
2655 clang::ObjCInterfaceDecl *class_interface_decl =
2656 objc_class_type->getInterface();
2657 // We currently can't complete objective C types through the newly added
2658 // ASTContext
2659 // because it only supports TagDecl objects right now...
2660 if (class_interface_decl) {
2661 if (class_interface_decl->getDefinition())
2662 return true;
2663
2664 if (!allow_completion)
2665 return false;
2666
2667 if (class_interface_decl->hasExternalLexicalStorage()) {
2668 if (ast) {
2669 clang::ExternalASTSource *external_ast_source =
2670 ast->getExternalSource();
2671 if (external_ast_source) {
2672 external_ast_source->CompleteType(class_interface_decl);
2673 return !objc_class_type->isIncompleteType();
2674 }
2675 }
2676 }
2677 return false;
2678 }
2679 }
2680 } break;
2681
2682 case clang::Type::Typedef:
2683 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2684 ->getDecl()
2685 ->getUnderlyingType(),
2686 allow_completion);
2687
2688 case clang::Type::Auto:
2689 return GetCompleteQualType(
2690 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2691 allow_completion);
2692
2693 case clang::Type::Elaborated:
2694 return GetCompleteQualType(
2695 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2696 allow_completion);
2697
2698 case clang::Type::Paren:
2699 return GetCompleteQualType(
2700 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2701 allow_completion);
2702
2703 case clang::Type::Attributed:
2704 return GetCompleteQualType(
2705 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2706 allow_completion);
2707
2708 default:
2709 break;
2710 }
2711
2712 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002713}
2714
2715static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002716ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2717 switch (access) {
2718 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002719 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002720 case eAccessPublic:
2721 return clang::ObjCIvarDecl::Public;
2722 case eAccessPrivate:
2723 return clang::ObjCIvarDecl::Private;
2724 case eAccessProtected:
2725 return clang::ObjCIvarDecl::Protected;
2726 case eAccessPackage:
2727 return clang::ObjCIvarDecl::Package;
2728 }
2729 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002730}
2731
Greg Claytond8d4a572015-08-11 21:38:15 +00002732//----------------------------------------------------------------------
2733// Tests
2734//----------------------------------------------------------------------
2735
Kate Stoneb9c1b512016-09-06 20:57:50 +00002736bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2737 clang::QualType qual_type(GetCanonicalQualType(type));
2738
2739 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2740 switch (type_class) {
2741 case clang::Type::IncompleteArray:
2742 case clang::Type::VariableArray:
2743 case clang::Type::ConstantArray:
2744 case clang::Type::ExtVector:
2745 case clang::Type::Vector:
2746 case clang::Type::Record:
2747 case clang::Type::ObjCObject:
2748 case clang::Type::ObjCInterface:
2749 return true;
2750 case clang::Type::Auto:
2751 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2752 ->getDeducedType()
2753 .getAsOpaquePtr());
2754 case clang::Type::Elaborated:
2755 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2756 ->getNamedType()
2757 .getAsOpaquePtr());
2758 case clang::Type::Typedef:
2759 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2760 ->getDecl()
2761 ->getUnderlyingType()
2762 .getAsOpaquePtr());
2763 case clang::Type::Paren:
2764 return IsAggregateType(
2765 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2766 default:
2767 break;
2768 }
2769 // The clang type does have a value
2770 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002771}
2772
Kate Stoneb9c1b512016-09-06 20:57:50 +00002773bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2774 clang::QualType qual_type(GetCanonicalQualType(type));
2775
2776 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2777 switch (type_class) {
2778 case clang::Type::Record: {
2779 if (const clang::RecordType *record_type =
2780 llvm::dyn_cast_or_null<clang::RecordType>(
2781 qual_type.getTypePtrOrNull())) {
2782 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2783 return record_decl->isAnonymousStructOrUnion();
2784 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002785 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002786 break;
2787 }
2788 case clang::Type::Auto:
2789 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2790 ->getDeducedType()
2791 .getAsOpaquePtr());
2792 case clang::Type::Elaborated:
2793 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2794 ->getNamedType()
2795 .getAsOpaquePtr());
2796 case clang::Type::Typedef:
2797 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2798 ->getDecl()
2799 ->getUnderlyingType()
2800 .getAsOpaquePtr());
2801 case clang::Type::Paren:
2802 return IsAnonymousType(
2803 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2804 default:
2805 break;
2806 }
2807 // The clang type does have a value
2808 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002809}
2810
Kate Stoneb9c1b512016-09-06 20:57:50 +00002811bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2812 CompilerType *element_type_ptr,
2813 uint64_t *size, bool *is_incomplete) {
2814 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002815
Kate Stoneb9c1b512016-09-06 20:57:50 +00002816 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2817 switch (type_class) {
2818 default:
2819 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002820
Kate Stoneb9c1b512016-09-06 20:57:50 +00002821 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002822 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002823 element_type_ptr->SetCompilerType(
2824 getASTContext(),
2825 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002826 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002827 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2828 ->getSize()
2829 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002830 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002831 *is_incomplete = false;
2832 return true;
2833
2834 case clang::Type::IncompleteArray:
2835 if (element_type_ptr)
2836 element_type_ptr->SetCompilerType(
2837 getASTContext(),
2838 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2839 if (size)
2840 *size = 0;
2841 if (is_incomplete)
2842 *is_incomplete = true;
2843 return true;
2844
2845 case clang::Type::VariableArray:
2846 if (element_type_ptr)
2847 element_type_ptr->SetCompilerType(
2848 getASTContext(),
2849 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2850 if (size)
2851 *size = 0;
2852 if (is_incomplete)
2853 *is_incomplete = false;
2854 return true;
2855
2856 case clang::Type::DependentSizedArray:
2857 if (element_type_ptr)
2858 element_type_ptr->SetCompilerType(
2859 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2860 ->getElementType());
2861 if (size)
2862 *size = 0;
2863 if (is_incomplete)
2864 *is_incomplete = false;
2865 return true;
2866
2867 case clang::Type::Typedef:
2868 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2869 ->getDecl()
2870 ->getUnderlyingType()
2871 .getAsOpaquePtr(),
2872 element_type_ptr, size, is_incomplete);
2873 case clang::Type::Auto:
2874 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2875 ->getDeducedType()
2876 .getAsOpaquePtr(),
2877 element_type_ptr, size, is_incomplete);
2878 case clang::Type::Elaborated:
2879 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2880 ->getNamedType()
2881 .getAsOpaquePtr(),
2882 element_type_ptr, size, is_incomplete);
2883 case clang::Type::Paren:
2884 return IsArrayType(
2885 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2886 element_type_ptr, size, is_incomplete);
2887 }
2888 if (element_type_ptr)
2889 element_type_ptr->Clear();
2890 if (size)
2891 *size = 0;
2892 if (is_incomplete)
2893 *is_incomplete = false;
2894 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002895}
2896
Kate Stoneb9c1b512016-09-06 20:57:50 +00002897bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2898 CompilerType *element_type, uint64_t *size) {
2899 clang::QualType qual_type(GetCanonicalQualType(type));
2900
2901 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2902 switch (type_class) {
2903 case clang::Type::Vector: {
2904 const clang::VectorType *vector_type =
2905 qual_type->getAs<clang::VectorType>();
2906 if (vector_type) {
2907 if (size)
2908 *size = vector_type->getNumElements();
2909 if (element_type)
2910 *element_type =
2911 CompilerType(getASTContext(), vector_type->getElementType());
2912 }
2913 return true;
2914 } break;
2915 case clang::Type::ExtVector: {
2916 const clang::ExtVectorType *ext_vector_type =
2917 qual_type->getAs<clang::ExtVectorType>();
2918 if (ext_vector_type) {
2919 if (size)
2920 *size = ext_vector_type->getNumElements();
2921 if (element_type)
2922 *element_type =
2923 CompilerType(getASTContext(), ext_vector_type->getElementType());
2924 }
2925 return true;
2926 }
2927 default:
2928 break;
2929 }
2930 return false;
2931}
2932
2933bool ClangASTContext::IsRuntimeGeneratedType(
2934 lldb::opaque_compiler_type_t type) {
2935 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
2936 ->GetDeclContextForType(GetQualType(type));
2937 if (!decl_ctx)
2938 return false;
2939
2940 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2941 return false;
2942
2943 clang::ObjCInterfaceDecl *result_iface_decl =
2944 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2945
2946 ClangASTMetadata *ast_metadata =
2947 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2948 if (!ast_metadata)
2949 return false;
2950 return (ast_metadata->GetISAPtr() != 0);
2951}
2952
2953bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
2954 return GetQualType(type).getUnqualifiedType()->isCharType();
2955}
2956
2957bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
2958 const bool allow_completion = false;
2959 return GetCompleteQualType(getASTContext(), GetQualType(type),
2960 allow_completion);
2961}
2962
2963bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
2964 return GetQualType(type).isConstQualified();
2965}
2966
2967bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
2968 uint32_t &length) {
2969 CompilerType pointee_or_element_clang_type;
2970 length = 0;
2971 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2972
2973 if (!pointee_or_element_clang_type.IsValid())
2974 return false;
2975
2976 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2977 if (pointee_or_element_clang_type.IsCharType()) {
2978 if (type_flags.Test(eTypeIsArray)) {
2979 // We know the size of the array and it could be a C string
2980 // since it is an array of characters
2981 length = llvm::cast<clang::ConstantArrayType>(
2982 GetCanonicalQualType(type).getTypePtr())
2983 ->getSize()
2984 .getLimitedValue();
2985 }
2986 return true;
2987 }
2988 }
2989 return false;
2990}
2991
2992bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
2993 bool *is_variadic_ptr) {
2994 if (type) {
2995 clang::QualType qual_type(GetCanonicalQualType(type));
2996
2997 if (qual_type->isFunctionType()) {
2998 if (is_variadic_ptr) {
2999 const clang::FunctionProtoType *function_proto_type =
3000 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3001 if (function_proto_type)
3002 *is_variadic_ptr = function_proto_type->isVariadic();
3003 else
3004 *is_variadic_ptr = false;
3005 }
3006 return true;
3007 }
3008
Greg Claytond8d4a572015-08-11 21:38:15 +00003009 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003010 switch (type_class) {
3011 default:
3012 break;
3013 case clang::Type::Typedef:
3014 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
3015 ->getDecl()
3016 ->getUnderlyingType()
3017 .getAsOpaquePtr(),
3018 nullptr);
3019 case clang::Type::Auto:
3020 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3021 ->getDeducedType()
3022 .getAsOpaquePtr(),
3023 nullptr);
3024 case clang::Type::Elaborated:
3025 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3026 ->getNamedType()
3027 .getAsOpaquePtr(),
3028 nullptr);
3029 case clang::Type::Paren:
3030 return IsFunctionType(
3031 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3032 nullptr);
3033 case clang::Type::LValueReference:
3034 case clang::Type::RValueReference: {
3035 const clang::ReferenceType *reference_type =
3036 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3037 if (reference_type)
3038 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3039 nullptr);
3040 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00003041 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003042 }
3043 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003044}
3045
3046// Used to detect "Homogeneous Floating-point Aggregates"
3047uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003048ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3049 CompilerType *base_type_ptr) {
3050 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003051 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003052
3053 clang::QualType qual_type(GetCanonicalQualType(type));
3054 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3055 switch (type_class) {
3056 case clang::Type::Record:
3057 if (GetCompleteType(type)) {
3058 const clang::CXXRecordDecl *cxx_record_decl =
3059 qual_type->getAsCXXRecordDecl();
3060 if (cxx_record_decl) {
3061 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3062 return 0;
3063 }
3064 const clang::RecordType *record_type =
3065 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3066 if (record_type) {
3067 const clang::RecordDecl *record_decl = record_type->getDecl();
3068 if (record_decl) {
3069 // We are looking for a structure that contains only floating point
3070 // types
3071 clang::RecordDecl::field_iterator field_pos,
3072 field_end = record_decl->field_end();
3073 uint32_t num_fields = 0;
3074 bool is_hva = false;
3075 bool is_hfa = false;
3076 clang::QualType base_qual_type;
3077 uint64_t base_bitwidth = 0;
3078 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3079 ++field_pos) {
3080 clang::QualType field_qual_type = field_pos->getType();
3081 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3082 if (field_qual_type->isFloatingType()) {
3083 if (field_qual_type->isComplexType())
3084 return 0;
3085 else {
3086 if (num_fields == 0)
3087 base_qual_type = field_qual_type;
3088 else {
3089 if (is_hva)
3090 return 0;
3091 is_hfa = true;
3092 if (field_qual_type.getTypePtr() !=
3093 base_qual_type.getTypePtr())
3094 return 0;
3095 }
3096 }
3097 } else if (field_qual_type->isVectorType() ||
3098 field_qual_type->isExtVectorType()) {
3099 if (num_fields == 0) {
3100 base_qual_type = field_qual_type;
3101 base_bitwidth = field_bitwidth;
3102 } else {
3103 if (is_hfa)
3104 return 0;
3105 is_hva = true;
3106 if (base_bitwidth != field_bitwidth)
3107 return 0;
3108 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3109 return 0;
3110 }
3111 } else
3112 return 0;
3113 ++num_fields;
3114 }
3115 if (base_type_ptr)
3116 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3117 return num_fields;
3118 }
3119 }
3120 }
3121 break;
3122
3123 case clang::Type::Typedef:
3124 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3125 ->getDecl()
3126 ->getUnderlyingType()
3127 .getAsOpaquePtr(),
3128 base_type_ptr);
3129
3130 case clang::Type::Auto:
3131 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3132 ->getDeducedType()
3133 .getAsOpaquePtr(),
3134 base_type_ptr);
3135
3136 case clang::Type::Elaborated:
3137 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3138 ->getNamedType()
3139 .getAsOpaquePtr(),
3140 base_type_ptr);
3141 default:
3142 break;
3143 }
3144 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003145}
3146
Kate Stoneb9c1b512016-09-06 20:57:50 +00003147size_t ClangASTContext::GetNumberOfFunctionArguments(
3148 lldb::opaque_compiler_type_t type) {
3149 if (type) {
3150 clang::QualType qual_type(GetCanonicalQualType(type));
3151 const clang::FunctionProtoType *func =
3152 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3153 if (func)
3154 return func->getNumParams();
3155 }
3156 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003157}
3158
Greg Claytona1e5dc82015-08-11 22:53:00 +00003159CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003160ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3161 const size_t index) {
3162 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003163 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003164 const clang::FunctionProtoType *func =
3165 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3166 if (func) {
3167 if (index < func->getNumParams())
3168 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003169 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003170 }
3171 return CompilerType();
3172}
3173
3174bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3175 if (type) {
3176 clang::QualType qual_type(GetCanonicalQualType(type));
3177
3178 if (qual_type->isFunctionPointerType())
3179 return true;
3180
3181 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3182 switch (type_class) {
3183 default:
3184 break;
3185 case clang::Type::Typedef:
3186 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3187 ->getDecl()
3188 ->getUnderlyingType()
3189 .getAsOpaquePtr());
3190 case clang::Type::Auto:
3191 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3192 ->getDeducedType()
3193 .getAsOpaquePtr());
3194 case clang::Type::Elaborated:
3195 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3196 ->getNamedType()
3197 .getAsOpaquePtr());
3198 case clang::Type::Paren:
3199 return IsFunctionPointerType(
3200 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3201
3202 case clang::Type::LValueReference:
3203 case clang::Type::RValueReference: {
3204 const clang::ReferenceType *reference_type =
3205 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3206 if (reference_type)
3207 return IsFunctionPointerType(
3208 reference_type->getPointeeType().getAsOpaquePtr());
3209 } break;
3210 }
3211 }
3212 return false;
3213}
3214
3215bool ClangASTContext::IsBlockPointerType(
3216 lldb::opaque_compiler_type_t type,
3217 CompilerType *function_pointer_type_ptr) {
3218 if (type) {
3219 clang::QualType qual_type(GetCanonicalQualType(type));
3220
3221 if (qual_type->isBlockPointerType()) {
3222 if (function_pointer_type_ptr) {
3223 const clang::BlockPointerType *block_pointer_type =
3224 qual_type->getAs<clang::BlockPointerType>();
3225 QualType pointee_type = block_pointer_type->getPointeeType();
3226 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3227 *function_pointer_type_ptr =
3228 CompilerType(getASTContext(), function_pointer_type);
3229 }
3230 return true;
3231 }
3232
3233 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3234 switch (type_class) {
3235 default:
3236 break;
3237 case clang::Type::Typedef:
3238 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3239 ->getDecl()
3240 ->getUnderlyingType()
3241 .getAsOpaquePtr(),
3242 function_pointer_type_ptr);
3243 case clang::Type::Auto:
3244 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3245 ->getDeducedType()
3246 .getAsOpaquePtr(),
3247 function_pointer_type_ptr);
3248 case clang::Type::Elaborated:
3249 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3250 ->getNamedType()
3251 .getAsOpaquePtr(),
3252 function_pointer_type_ptr);
3253 case clang::Type::Paren:
3254 return IsBlockPointerType(
3255 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3256 function_pointer_type_ptr);
3257
3258 case clang::Type::LValueReference:
3259 case clang::Type::RValueReference: {
3260 const clang::ReferenceType *reference_type =
3261 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3262 if (reference_type)
3263 return IsBlockPointerType(
3264 reference_type->getPointeeType().getAsOpaquePtr(),
3265 function_pointer_type_ptr);
3266 } break;
3267 }
3268 }
3269 return false;
3270}
3271
3272bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3273 bool &is_signed) {
3274 if (!type)
3275 return false;
3276
3277 clang::QualType qual_type(GetCanonicalQualType(type));
3278 const clang::BuiltinType *builtin_type =
3279 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3280
3281 if (builtin_type) {
3282 if (builtin_type->isInteger()) {
3283 is_signed = builtin_type->isSignedInteger();
3284 return true;
3285 }
3286 }
3287
3288 return false;
3289}
3290
3291bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3292 bool &is_signed) {
3293 if (type) {
3294 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3295 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3296
3297 if (enum_type) {
3298 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3299 is_signed);
3300 return true;
3301 }
3302 }
3303
3304 return false;
3305}
3306
3307bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3308 CompilerType *pointee_type) {
3309 if (type) {
3310 clang::QualType qual_type(GetCanonicalQualType(type));
3311 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3312 switch (type_class) {
3313 case clang::Type::Builtin:
3314 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3315 default:
3316 break;
3317 case clang::BuiltinType::ObjCId:
3318 case clang::BuiltinType::ObjCClass:
3319 return true;
3320 }
3321 return false;
3322 case clang::Type::ObjCObjectPointer:
3323 if (pointee_type)
3324 pointee_type->SetCompilerType(
3325 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3326 ->getPointeeType());
3327 return true;
3328 case clang::Type::BlockPointer:
3329 if (pointee_type)
3330 pointee_type->SetCompilerType(
3331 getASTContext(),
3332 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3333 return true;
3334 case clang::Type::Pointer:
3335 if (pointee_type)
3336 pointee_type->SetCompilerType(
3337 getASTContext(),
3338 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3339 return true;
3340 case clang::Type::MemberPointer:
3341 if (pointee_type)
3342 pointee_type->SetCompilerType(
3343 getASTContext(),
3344 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3345 return true;
3346 case clang::Type::Typedef:
3347 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3348 ->getDecl()
3349 ->getUnderlyingType()
3350 .getAsOpaquePtr(),
3351 pointee_type);
3352 case clang::Type::Auto:
3353 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3354 ->getDeducedType()
3355 .getAsOpaquePtr(),
3356 pointee_type);
3357 case clang::Type::Elaborated:
3358 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3359 ->getNamedType()
3360 .getAsOpaquePtr(),
3361 pointee_type);
3362 case clang::Type::Paren:
3363 return IsPointerType(
3364 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3365 pointee_type);
3366 default:
3367 break;
3368 }
3369 }
3370 if (pointee_type)
3371 pointee_type->Clear();
3372 return false;
3373}
3374
3375bool ClangASTContext::IsPointerOrReferenceType(
3376 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3377 if (type) {
3378 clang::QualType qual_type(GetCanonicalQualType(type));
3379 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3380 switch (type_class) {
3381 case clang::Type::Builtin:
3382 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3383 default:
3384 break;
3385 case clang::BuiltinType::ObjCId:
3386 case clang::BuiltinType::ObjCClass:
3387 return true;
3388 }
3389 return false;
3390 case clang::Type::ObjCObjectPointer:
3391 if (pointee_type)
3392 pointee_type->SetCompilerType(
3393 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3394 ->getPointeeType());
3395 return true;
3396 case clang::Type::BlockPointer:
3397 if (pointee_type)
3398 pointee_type->SetCompilerType(
3399 getASTContext(),
3400 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3401 return true;
3402 case clang::Type::Pointer:
3403 if (pointee_type)
3404 pointee_type->SetCompilerType(
3405 getASTContext(),
3406 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3407 return true;
3408 case clang::Type::MemberPointer:
3409 if (pointee_type)
3410 pointee_type->SetCompilerType(
3411 getASTContext(),
3412 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3413 return true;
3414 case clang::Type::LValueReference:
3415 if (pointee_type)
3416 pointee_type->SetCompilerType(
3417 getASTContext(),
3418 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3419 return true;
3420 case clang::Type::RValueReference:
3421 if (pointee_type)
3422 pointee_type->SetCompilerType(
3423 getASTContext(),
3424 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3425 return true;
3426 case clang::Type::Typedef:
3427 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3428 ->getDecl()
3429 ->getUnderlyingType()
3430 .getAsOpaquePtr(),
3431 pointee_type);
3432 case clang::Type::Auto:
3433 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3434 ->getDeducedType()
3435 .getAsOpaquePtr(),
3436 pointee_type);
3437 case clang::Type::Elaborated:
3438 return IsPointerOrReferenceType(
3439 llvm::cast<clang::ElaboratedType>(qual_type)
3440 ->getNamedType()
3441 .getAsOpaquePtr(),
3442 pointee_type);
3443 case clang::Type::Paren:
3444 return IsPointerOrReferenceType(
3445 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3446 pointee_type);
3447 default:
3448 break;
3449 }
3450 }
3451 if (pointee_type)
3452 pointee_type->Clear();
3453 return false;
3454}
3455
3456bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3457 CompilerType *pointee_type,
3458 bool *is_rvalue) {
3459 if (type) {
3460 clang::QualType qual_type(GetCanonicalQualType(type));
3461 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3462
3463 switch (type_class) {
3464 case clang::Type::LValueReference:
3465 if (pointee_type)
3466 pointee_type->SetCompilerType(
3467 getASTContext(),
3468 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3469 if (is_rvalue)
3470 *is_rvalue = false;
3471 return true;
3472 case clang::Type::RValueReference:
3473 if (pointee_type)
3474 pointee_type->SetCompilerType(
3475 getASTContext(),
3476 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3477 if (is_rvalue)
3478 *is_rvalue = true;
3479 return true;
3480 case clang::Type::Typedef:
3481 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3482 ->getDecl()
3483 ->getUnderlyingType()
3484 .getAsOpaquePtr(),
3485 pointee_type, is_rvalue);
3486 case clang::Type::Auto:
3487 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3488 ->getDeducedType()
3489 .getAsOpaquePtr(),
3490 pointee_type, is_rvalue);
3491 case clang::Type::Elaborated:
3492 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3493 ->getNamedType()
3494 .getAsOpaquePtr(),
3495 pointee_type, is_rvalue);
3496 case clang::Type::Paren:
3497 return IsReferenceType(
3498 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3499 pointee_type, is_rvalue);
3500
3501 default:
3502 break;
3503 }
3504 }
3505 if (pointee_type)
3506 pointee_type->Clear();
3507 return false;
3508}
3509
3510bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3511 uint32_t &count, bool &is_complex) {
3512 if (type) {
3513 clang::QualType qual_type(GetCanonicalQualType(type));
3514
3515 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3516 qual_type->getCanonicalTypeInternal())) {
3517 clang::BuiltinType::Kind kind = BT->getKind();
3518 if (kind >= clang::BuiltinType::Float &&
3519 kind <= clang::BuiltinType::LongDouble) {
3520 count = 1;
3521 is_complex = false;
3522 return true;
3523 }
3524 } else if (const clang::ComplexType *CT =
3525 llvm::dyn_cast<clang::ComplexType>(
3526 qual_type->getCanonicalTypeInternal())) {
3527 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3528 is_complex)) {
3529 count = 2;
3530 is_complex = true;
3531 return true;
3532 }
3533 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3534 qual_type->getCanonicalTypeInternal())) {
3535 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3536 is_complex)) {
3537 count = VT->getNumElements();
3538 is_complex = false;
3539 return true;
3540 }
3541 }
3542 }
3543 count = 0;
3544 is_complex = false;
3545 return false;
3546}
3547
3548bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3549 if (!type)
3550 return false;
3551
3552 clang::QualType qual_type(GetQualType(type));
3553 const clang::TagType *tag_type =
3554 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3555 if (tag_type) {
3556 clang::TagDecl *tag_decl = tag_type->getDecl();
3557 if (tag_decl)
3558 return tag_decl->isCompleteDefinition();
3559 return false;
3560 } else {
3561 const clang::ObjCObjectType *objc_class_type =
3562 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3563 if (objc_class_type) {
3564 clang::ObjCInterfaceDecl *class_interface_decl =
3565 objc_class_type->getInterface();
3566 if (class_interface_decl)
3567 return class_interface_decl->getDefinition() != nullptr;
3568 return false;
3569 }
3570 }
3571 return true;
3572}
3573
3574bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3575 if (type) {
3576 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3577
3578 const clang::ObjCObjectPointerType *obj_pointer_type =
3579 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3580
3581 if (obj_pointer_type)
3582 return obj_pointer_type->isObjCClassType();
3583 }
3584 return false;
3585}
3586
3587bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3588 if (ClangUtil::IsClangType(type))
3589 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3590 return false;
3591}
3592
3593bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3594 if (!type)
3595 return false;
3596 clang::QualType qual_type(GetCanonicalQualType(type));
3597 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3598 return (type_class == clang::Type::Record);
3599}
3600
3601bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3602 if (!type)
3603 return false;
3604 clang::QualType qual_type(GetCanonicalQualType(type));
3605 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3606 return (type_class == clang::Type::Enum);
3607}
3608
3609bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3610 if (type) {
3611 clang::QualType qual_type(GetCanonicalQualType(type));
3612 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3613 switch (type_class) {
3614 case clang::Type::Record:
3615 if (GetCompleteType(type)) {
3616 const clang::RecordType *record_type =
3617 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3618 const clang::RecordDecl *record_decl = record_type->getDecl();
3619 if (record_decl) {
3620 const clang::CXXRecordDecl *cxx_record_decl =
3621 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3622 if (cxx_record_decl)
3623 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003624 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003625 }
3626 break;
3627
3628 default:
3629 break;
3630 }
3631 }
3632 return false;
3633}
3634
3635bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3636 CompilerType *dynamic_pointee_type,
3637 bool check_cplusplus,
3638 bool check_objc) {
3639 clang::QualType pointee_qual_type;
3640 if (type) {
3641 clang::QualType qual_type(GetCanonicalQualType(type));
3642 bool success = false;
3643 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3644 switch (type_class) {
3645 case clang::Type::Builtin:
3646 if (check_objc &&
3647 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3648 clang::BuiltinType::ObjCId) {
3649 if (dynamic_pointee_type)
3650 dynamic_pointee_type->SetCompilerType(this, type);
3651 return true;
3652 }
3653 break;
3654
3655 case clang::Type::ObjCObjectPointer:
3656 if (check_objc) {
3657 if (auto objc_pointee_type =
3658 qual_type->getPointeeType().getTypePtrOrNull()) {
3659 if (auto objc_object_type =
3660 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3661 objc_pointee_type)) {
3662 if (objc_object_type->isObjCClass())
3663 return false;
3664 }
3665 }
3666 if (dynamic_pointee_type)
3667 dynamic_pointee_type->SetCompilerType(
3668 getASTContext(),
3669 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3670 ->getPointeeType());
3671 return true;
3672 }
3673 break;
3674
3675 case clang::Type::Pointer:
3676 pointee_qual_type =
3677 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3678 success = true;
3679 break;
3680
3681 case clang::Type::LValueReference:
3682 case clang::Type::RValueReference:
3683 pointee_qual_type =
3684 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3685 success = true;
3686 break;
3687
3688 case clang::Type::Typedef:
3689 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3690 ->getDecl()
3691 ->getUnderlyingType()
3692 .getAsOpaquePtr(),
3693 dynamic_pointee_type, check_cplusplus,
3694 check_objc);
3695
3696 case clang::Type::Auto:
3697 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3698 ->getDeducedType()
3699 .getAsOpaquePtr(),
3700 dynamic_pointee_type, check_cplusplus,
3701 check_objc);
3702
3703 case clang::Type::Elaborated:
3704 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3705 ->getNamedType()
3706 .getAsOpaquePtr(),
3707 dynamic_pointee_type, check_cplusplus,
3708 check_objc);
3709
3710 case clang::Type::Paren:
3711 return IsPossibleDynamicType(
3712 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3713 dynamic_pointee_type, check_cplusplus, check_objc);
3714 default:
3715 break;
3716 }
3717
3718 if (success) {
3719 // Check to make sure what we are pointing too is a possible dynamic C++
3720 // type
3721 // We currently accept any "void *" (in case we have a class that has been
3722 // watered down to an opaque pointer) and virtual C++ classes.
3723 const clang::Type::TypeClass pointee_type_class =
3724 pointee_qual_type.getCanonicalType()->getTypeClass();
3725 switch (pointee_type_class) {
3726 case clang::Type::Builtin:
3727 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3728 case clang::BuiltinType::UnknownAny:
3729 case clang::BuiltinType::Void:
3730 if (dynamic_pointee_type)
3731 dynamic_pointee_type->SetCompilerType(getASTContext(),
3732 pointee_qual_type);
3733 return true;
3734 default:
3735 break;
3736 }
3737 break;
3738
3739 case clang::Type::Record:
3740 if (check_cplusplus) {
3741 clang::CXXRecordDecl *cxx_record_decl =
3742 pointee_qual_type->getAsCXXRecordDecl();
3743 if (cxx_record_decl) {
3744 bool is_complete = cxx_record_decl->isCompleteDefinition();
3745
3746 if (is_complete)
3747 success = cxx_record_decl->isDynamicClass();
3748 else {
3749 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3750 getASTContext(), cxx_record_decl);
3751 if (metadata)
3752 success = metadata->GetIsDynamicCXXType();
3753 else {
3754 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3755 .GetCompleteType();
3756 if (is_complete)
3757 success = cxx_record_decl->isDynamicClass();
3758 else
3759 success = false;
3760 }
3761 }
3762
3763 if (success) {
3764 if (dynamic_pointee_type)
3765 dynamic_pointee_type->SetCompilerType(getASTContext(),
3766 pointee_qual_type);
3767 return true;
3768 }
3769 }
3770 }
3771 break;
3772
3773 case clang::Type::ObjCObject:
3774 case clang::Type::ObjCInterface:
3775 if (check_objc) {
3776 if (dynamic_pointee_type)
3777 dynamic_pointee_type->SetCompilerType(getASTContext(),
3778 pointee_qual_type);
3779 return true;
3780 }
3781 break;
3782
3783 default:
3784 break;
3785 }
3786 }
3787 }
3788 if (dynamic_pointee_type)
3789 dynamic_pointee_type->Clear();
3790 return false;
3791}
3792
3793bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3794 if (!type)
3795 return false;
3796
3797 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3798}
3799
3800bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3801 if (!type)
3802 return false;
3803 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3804}
3805
3806bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3807 if (!type)
3808 return false;
3809 return GetCanonicalQualType(type)->isVoidType();
3810}
3811
3812bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3813 return ClangASTContextSupportsLanguage(language);
3814}
3815
3816bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3817 std::string &class_name) {
3818 if (type) {
3819 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3820 if (!qual_type.isNull()) {
3821 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3822 if (cxx_record_decl) {
3823 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3824 return true;
3825 }
3826 }
3827 }
3828 class_name.clear();
3829 return false;
3830}
3831
3832bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3833 if (!type)
3834 return false;
3835
3836 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3837 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
3838 return true;
3839 return false;
3840}
3841
3842bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3843 if (!type)
3844 return false;
3845 clang::QualType qual_type(GetCanonicalQualType(type));
3846 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3847 if (tag_type)
3848 return tag_type->isBeingDefined();
3849 return false;
3850}
3851
3852bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3853 CompilerType *class_type_ptr) {
3854 if (!type)
3855 return false;
3856
3857 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3858
3859 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3860 if (class_type_ptr) {
3861 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3862 const clang::ObjCObjectPointerType *obj_pointer_type =
3863 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3864 if (obj_pointer_type == nullptr)
3865 class_type_ptr->Clear();
3866 else
3867 class_type_ptr->SetCompilerType(
3868 type.GetTypeSystem(),
3869 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3870 .getAsOpaquePtr());
3871 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003872 }
3873 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003874 }
3875 if (class_type_ptr)
3876 class_type_ptr->Clear();
3877 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003878}
3879
Kate Stoneb9c1b512016-09-06 20:57:50 +00003880bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3881 std::string &class_name) {
3882 if (!type)
3883 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003884
Kate Stoneb9c1b512016-09-06 20:57:50 +00003885 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3886
3887 const clang::ObjCObjectType *object_type =
3888 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3889 if (object_type) {
3890 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3891 if (interface) {
3892 class_name = interface->getNameAsString();
3893 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00003894 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003895 }
3896 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003897}
3898
Greg Claytond8d4a572015-08-11 21:38:15 +00003899//----------------------------------------------------------------------
3900// Type Completion
3901//----------------------------------------------------------------------
3902
Kate Stoneb9c1b512016-09-06 20:57:50 +00003903bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3904 if (!type)
3905 return false;
3906 const bool allow_completion = true;
3907 return GetCompleteQualType(getASTContext(), GetQualType(type),
3908 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00003909}
3910
Kate Stoneb9c1b512016-09-06 20:57:50 +00003911ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
3912 std::string type_name;
3913 if (type) {
3914 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
3915 clang::QualType qual_type(GetQualType(type));
3916 printing_policy.SuppressTagKeyword = true;
3917 const clang::TypedefType *typedef_type =
3918 qual_type->getAs<clang::TypedefType>();
3919 if (typedef_type) {
3920 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3921 type_name = typedef_decl->getQualifiedNameAsString();
3922 } else {
3923 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003924 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003925 }
3926 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00003927}
3928
3929uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003930ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
3931 CompilerType *pointee_or_element_clang_type) {
3932 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003933 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003934
3935 if (pointee_or_element_clang_type)
3936 pointee_or_element_clang_type->Clear();
3937
3938 clang::QualType qual_type(GetQualType(type));
3939
3940 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3941 switch (type_class) {
Sean Callananddf802a2017-06-02 01:24:18 +00003942 case clang::Type::Attributed:
3943 return GetTypeInfo(
3944 qual_type->getAs<clang::AttributedType>()
3945 ->getModifiedType().getAsOpaquePtr(),
3946 pointee_or_element_clang_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003947 case clang::Type::Builtin: {
3948 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3949 qual_type->getCanonicalTypeInternal());
3950
3951 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3952 switch (builtin_type->getKind()) {
3953 case clang::BuiltinType::ObjCId:
3954 case clang::BuiltinType::ObjCClass:
3955 if (pointee_or_element_clang_type)
3956 pointee_or_element_clang_type->SetCompilerType(
3957 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
3958 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3959 break;
3960
3961 case clang::BuiltinType::ObjCSel:
3962 if (pointee_or_element_clang_type)
3963 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
3964 getASTContext()->CharTy);
3965 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3966 break;
3967
3968 case clang::BuiltinType::Bool:
3969 case clang::BuiltinType::Char_U:
3970 case clang::BuiltinType::UChar:
3971 case clang::BuiltinType::WChar_U:
3972 case clang::BuiltinType::Char16:
3973 case clang::BuiltinType::Char32:
3974 case clang::BuiltinType::UShort:
3975 case clang::BuiltinType::UInt:
3976 case clang::BuiltinType::ULong:
3977 case clang::BuiltinType::ULongLong:
3978 case clang::BuiltinType::UInt128:
3979 case clang::BuiltinType::Char_S:
3980 case clang::BuiltinType::SChar:
3981 case clang::BuiltinType::WChar_S:
3982 case clang::BuiltinType::Short:
3983 case clang::BuiltinType::Int:
3984 case clang::BuiltinType::Long:
3985 case clang::BuiltinType::LongLong:
3986 case clang::BuiltinType::Int128:
3987 case clang::BuiltinType::Float:
3988 case clang::BuiltinType::Double:
3989 case clang::BuiltinType::LongDouble:
3990 builtin_type_flags |= eTypeIsScalar;
3991 if (builtin_type->isInteger()) {
3992 builtin_type_flags |= eTypeIsInteger;
3993 if (builtin_type->isSignedInteger())
3994 builtin_type_flags |= eTypeIsSigned;
3995 } else if (builtin_type->isFloatingPoint())
3996 builtin_type_flags |= eTypeIsFloat;
3997 break;
3998 default:
3999 break;
4000 }
4001 return builtin_type_flags;
4002 }
4003
4004 case clang::Type::BlockPointer:
4005 if (pointee_or_element_clang_type)
4006 pointee_or_element_clang_type->SetCompilerType(
4007 getASTContext(), qual_type->getPointeeType());
4008 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
4009
4010 case clang::Type::Complex: {
4011 uint32_t complex_type_flags =
4012 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
4013 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
4014 qual_type->getCanonicalTypeInternal());
4015 if (complex_type) {
4016 clang::QualType complex_element_type(complex_type->getElementType());
4017 if (complex_element_type->isIntegerType())
4018 complex_type_flags |= eTypeIsFloat;
4019 else if (complex_element_type->isFloatingType())
4020 complex_type_flags |= eTypeIsInteger;
4021 }
4022 return complex_type_flags;
4023 } break;
4024
4025 case clang::Type::ConstantArray:
4026 case clang::Type::DependentSizedArray:
4027 case clang::Type::IncompleteArray:
4028 case clang::Type::VariableArray:
4029 if (pointee_or_element_clang_type)
4030 pointee_or_element_clang_type->SetCompilerType(
4031 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4032 ->getElementType());
4033 return eTypeHasChildren | eTypeIsArray;
4034
4035 case clang::Type::DependentName:
4036 return 0;
4037 case clang::Type::DependentSizedExtVector:
4038 return eTypeHasChildren | eTypeIsVector;
4039 case clang::Type::DependentTemplateSpecialization:
4040 return eTypeIsTemplate;
4041 case clang::Type::Decltype:
4042 return 0;
4043
4044 case clang::Type::Enum:
4045 if (pointee_or_element_clang_type)
4046 pointee_or_element_clang_type->SetCompilerType(
4047 getASTContext(),
4048 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4049 return eTypeIsEnumeration | eTypeHasValue;
4050
4051 case clang::Type::Auto:
4052 return CompilerType(
4053 getASTContext(),
4054 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4055 .GetTypeInfo(pointee_or_element_clang_type);
4056 case clang::Type::Elaborated:
4057 return CompilerType(
4058 getASTContext(),
4059 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4060 .GetTypeInfo(pointee_or_element_clang_type);
4061 case clang::Type::Paren:
4062 return CompilerType(getASTContext(),
4063 llvm::cast<clang::ParenType>(qual_type)->desugar())
4064 .GetTypeInfo(pointee_or_element_clang_type);
4065
4066 case clang::Type::FunctionProto:
4067 return eTypeIsFuncPrototype | eTypeHasValue;
4068 case clang::Type::FunctionNoProto:
4069 return eTypeIsFuncPrototype | eTypeHasValue;
4070 case clang::Type::InjectedClassName:
4071 return 0;
4072
4073 case clang::Type::LValueReference:
4074 case clang::Type::RValueReference:
4075 if (pointee_or_element_clang_type)
4076 pointee_or_element_clang_type->SetCompilerType(
4077 getASTContext(),
4078 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4079 ->getPointeeType());
4080 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4081
4082 case clang::Type::MemberPointer:
4083 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4084
4085 case clang::Type::ObjCObjectPointer:
4086 if (pointee_or_element_clang_type)
4087 pointee_or_element_clang_type->SetCompilerType(
4088 getASTContext(), qual_type->getPointeeType());
4089 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4090 eTypeHasValue;
4091
4092 case clang::Type::ObjCObject:
4093 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4094 case clang::Type::ObjCInterface:
4095 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4096
4097 case clang::Type::Pointer:
4098 if (pointee_or_element_clang_type)
4099 pointee_or_element_clang_type->SetCompilerType(
4100 getASTContext(), qual_type->getPointeeType());
4101 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4102
4103 case clang::Type::Record:
4104 if (qual_type->getAsCXXRecordDecl())
4105 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4106 else
4107 return eTypeHasChildren | eTypeIsStructUnion;
4108 break;
4109 case clang::Type::SubstTemplateTypeParm:
4110 return eTypeIsTemplate;
4111 case clang::Type::TemplateTypeParm:
4112 return eTypeIsTemplate;
4113 case clang::Type::TemplateSpecialization:
4114 return eTypeIsTemplate;
4115
4116 case clang::Type::Typedef:
4117 return eTypeIsTypedef |
4118 CompilerType(getASTContext(),
4119 llvm::cast<clang::TypedefType>(qual_type)
4120 ->getDecl()
4121 ->getUnderlyingType())
4122 .GetTypeInfo(pointee_or_element_clang_type);
4123 case clang::Type::TypeOfExpr:
4124 return 0;
4125 case clang::Type::TypeOf:
4126 return 0;
4127 case clang::Type::UnresolvedUsing:
4128 return 0;
4129
4130 case clang::Type::ExtVector:
4131 case clang::Type::Vector: {
4132 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4133 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4134 qual_type->getCanonicalTypeInternal());
4135 if (vector_type) {
4136 if (vector_type->isIntegerType())
4137 vector_type_flags |= eTypeIsFloat;
4138 else if (vector_type->isFloatingType())
4139 vector_type_flags |= eTypeIsInteger;
4140 }
4141 return vector_type_flags;
4142 }
4143 default:
4144 return 0;
4145 }
4146 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004147}
4148
Greg Claytond8d4a572015-08-11 21:38:15 +00004149lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004150ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4151 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004152 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004153
4154 // If the type is a reference, then resolve it to what it refers to first:
4155 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4156 if (qual_type->isAnyPointerType()) {
4157 if (qual_type->isObjCObjectPointerType())
4158 return lldb::eLanguageTypeObjC;
4159
4160 clang::QualType pointee_type(qual_type->getPointeeType());
4161 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4162 return lldb::eLanguageTypeC_plus_plus;
4163 if (pointee_type->isObjCObjectOrInterfaceType())
4164 return lldb::eLanguageTypeObjC;
4165 if (pointee_type->isObjCClassType())
4166 return lldb::eLanguageTypeObjC;
4167 if (pointee_type.getTypePtr() ==
4168 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4169 return lldb::eLanguageTypeObjC;
4170 } else {
4171 if (qual_type->isObjCObjectOrInterfaceType())
4172 return lldb::eLanguageTypeObjC;
4173 if (qual_type->getAsCXXRecordDecl())
4174 return lldb::eLanguageTypeC_plus_plus;
4175 switch (qual_type->getTypeClass()) {
4176 default:
4177 break;
4178 case clang::Type::Builtin:
4179 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4180 default:
4181 case clang::BuiltinType::Void:
4182 case clang::BuiltinType::Bool:
4183 case clang::BuiltinType::Char_U:
4184 case clang::BuiltinType::UChar:
4185 case clang::BuiltinType::WChar_U:
4186 case clang::BuiltinType::Char16:
4187 case clang::BuiltinType::Char32:
4188 case clang::BuiltinType::UShort:
4189 case clang::BuiltinType::UInt:
4190 case clang::BuiltinType::ULong:
4191 case clang::BuiltinType::ULongLong:
4192 case clang::BuiltinType::UInt128:
4193 case clang::BuiltinType::Char_S:
4194 case clang::BuiltinType::SChar:
4195 case clang::BuiltinType::WChar_S:
4196 case clang::BuiltinType::Short:
4197 case clang::BuiltinType::Int:
4198 case clang::BuiltinType::Long:
4199 case clang::BuiltinType::LongLong:
4200 case clang::BuiltinType::Int128:
4201 case clang::BuiltinType::Float:
4202 case clang::BuiltinType::Double:
4203 case clang::BuiltinType::LongDouble:
4204 break;
4205
4206 case clang::BuiltinType::NullPtr:
4207 return eLanguageTypeC_plus_plus;
4208
4209 case clang::BuiltinType::ObjCId:
4210 case clang::BuiltinType::ObjCClass:
4211 case clang::BuiltinType::ObjCSel:
4212 return eLanguageTypeObjC;
4213
4214 case clang::BuiltinType::Dependent:
4215 case clang::BuiltinType::Overload:
4216 case clang::BuiltinType::BoundMember:
4217 case clang::BuiltinType::UnknownAny:
4218 break;
4219 }
4220 break;
4221 case clang::Type::Typedef:
4222 return CompilerType(getASTContext(),
4223 llvm::cast<clang::TypedefType>(qual_type)
4224 ->getDecl()
4225 ->getUnderlyingType())
4226 .GetMinimumLanguage();
4227 }
4228 }
4229 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004230}
4231
4232lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004233ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4234 if (!type)
4235 return lldb::eTypeClassInvalid;
4236
4237 clang::QualType qual_type(GetQualType(type));
4238
4239 switch (qual_type->getTypeClass()) {
4240 case clang::Type::UnaryTransform:
4241 break;
4242 case clang::Type::FunctionNoProto:
4243 return lldb::eTypeClassFunction;
4244 case clang::Type::FunctionProto:
4245 return lldb::eTypeClassFunction;
4246 case clang::Type::IncompleteArray:
4247 return lldb::eTypeClassArray;
4248 case clang::Type::VariableArray:
4249 return lldb::eTypeClassArray;
4250 case clang::Type::ConstantArray:
4251 return lldb::eTypeClassArray;
4252 case clang::Type::DependentSizedArray:
4253 return lldb::eTypeClassArray;
4254 case clang::Type::DependentSizedExtVector:
4255 return lldb::eTypeClassVector;
4256 case clang::Type::ExtVector:
4257 return lldb::eTypeClassVector;
4258 case clang::Type::Vector:
4259 return lldb::eTypeClassVector;
4260 case clang::Type::Builtin:
4261 return lldb::eTypeClassBuiltin;
4262 case clang::Type::ObjCObjectPointer:
4263 return lldb::eTypeClassObjCObjectPointer;
4264 case clang::Type::BlockPointer:
4265 return lldb::eTypeClassBlockPointer;
4266 case clang::Type::Pointer:
4267 return lldb::eTypeClassPointer;
4268 case clang::Type::LValueReference:
4269 return lldb::eTypeClassReference;
4270 case clang::Type::RValueReference:
4271 return lldb::eTypeClassReference;
4272 case clang::Type::MemberPointer:
4273 return lldb::eTypeClassMemberPointer;
4274 case clang::Type::Complex:
4275 if (qual_type->isComplexType())
4276 return lldb::eTypeClassComplexFloat;
4277 else
4278 return lldb::eTypeClassComplexInteger;
4279 case clang::Type::ObjCObject:
4280 return lldb::eTypeClassObjCObject;
4281 case clang::Type::ObjCInterface:
4282 return lldb::eTypeClassObjCInterface;
4283 case clang::Type::Record: {
4284 const clang::RecordType *record_type =
4285 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4286 const clang::RecordDecl *record_decl = record_type->getDecl();
4287 if (record_decl->isUnion())
4288 return lldb::eTypeClassUnion;
4289 else if (record_decl->isStruct())
4290 return lldb::eTypeClassStruct;
4291 else
4292 return lldb::eTypeClassClass;
4293 } break;
4294 case clang::Type::Enum:
4295 return lldb::eTypeClassEnumeration;
4296 case clang::Type::Typedef:
4297 return lldb::eTypeClassTypedef;
4298 case clang::Type::UnresolvedUsing:
4299 break;
4300 case clang::Type::Paren:
4301 return CompilerType(getASTContext(),
4302 llvm::cast<clang::ParenType>(qual_type)->desugar())
4303 .GetTypeClass();
4304 case clang::Type::Auto:
4305 return CompilerType(
4306 getASTContext(),
4307 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4308 .GetTypeClass();
4309 case clang::Type::Elaborated:
4310 return CompilerType(
4311 getASTContext(),
4312 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4313 .GetTypeClass();
4314
4315 case clang::Type::Attributed:
4316 break;
4317 case clang::Type::TemplateTypeParm:
4318 break;
4319 case clang::Type::SubstTemplateTypeParm:
4320 break;
4321 case clang::Type::SubstTemplateTypeParmPack:
4322 break;
4323 case clang::Type::InjectedClassName:
4324 break;
4325 case clang::Type::DependentName:
4326 break;
4327 case clang::Type::DependentTemplateSpecialization:
4328 break;
4329 case clang::Type::PackExpansion:
4330 break;
4331
4332 case clang::Type::TypeOfExpr:
4333 break;
4334 case clang::Type::TypeOf:
4335 break;
4336 case clang::Type::Decltype:
4337 break;
4338 case clang::Type::TemplateSpecialization:
4339 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004340 case clang::Type::DeducedTemplateSpecialization:
4341 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004342 case clang::Type::Atomic:
4343 break;
4344 case clang::Type::Pipe:
4345 break;
4346
4347 // pointer type decayed from an array or function type.
4348 case clang::Type::Decayed:
4349 break;
4350 case clang::Type::Adjusted:
4351 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004352 case clang::Type::ObjCTypeParam:
4353 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00004354
4355 case clang::Type::DependentAddressSpace:
4356 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004357 }
4358 // We don't know hot to display this type...
4359 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004360}
4361
Kate Stoneb9c1b512016-09-06 20:57:50 +00004362unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4363 if (type)
4364 return GetQualType(type).getQualifiers().getCVRQualifiers();
4365 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004366}
4367
4368//----------------------------------------------------------------------
4369// Creating related types
4370//----------------------------------------------------------------------
4371
Greg Claytona1e5dc82015-08-11 22:53:00 +00004372CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004373ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4374 uint64_t *stride) {
4375 if (type) {
4376 clang::QualType qual_type(GetCanonicalQualType(type));
4377
4378 const clang::Type *array_eletype =
4379 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4380
4381 if (!array_eletype)
4382 return CompilerType();
4383
4384 CompilerType element_type(getASTContext(),
4385 array_eletype->getCanonicalTypeUnqualified());
4386
4387 // TODO: the real stride will be >= this value.. find the real one!
4388 if (stride)
4389 *stride = element_type.GetByteSize(nullptr);
4390
4391 return element_type;
4392 }
4393 return CompilerType();
4394}
4395
4396CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4397 uint64_t size) {
4398 if (type) {
4399 clang::QualType qual_type(GetCanonicalQualType(type));
4400 if (clang::ASTContext *ast_ctx = getASTContext()) {
4401 if (size != 0)
4402 return CompilerType(
4403 ast_ctx, ast_ctx->getConstantArrayType(
4404 qual_type, llvm::APInt(64, size),
4405 clang::ArrayType::ArraySizeModifier::Normal, 0));
4406 else
4407 return CompilerType(
4408 ast_ctx,
4409 ast_ctx->getIncompleteArrayType(
4410 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
Greg Claytond8d4a572015-08-11 21:38:15 +00004411 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004412 }
4413
4414 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004415}
4416
Greg Claytona1e5dc82015-08-11 22:53:00 +00004417CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004418ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4419 if (type)
4420 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4421 return CompilerType();
4422}
4423
4424static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4425 clang::QualType qual_type) {
4426 if (qual_type->isPointerType())
4427 qual_type = ast->getPointerType(
4428 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4429 else
4430 qual_type = qual_type.getUnqualifiedType();
4431 qual_type.removeLocalConst();
4432 qual_type.removeLocalRestrict();
4433 qual_type.removeLocalVolatile();
4434 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004435}
4436
4437CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004438ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4439 if (type)
4440 return CompilerType(
4441 getASTContext(),
4442 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4443 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004444}
4445
Kate Stoneb9c1b512016-09-06 20:57:50 +00004446int ClangASTContext::GetFunctionArgumentCount(
4447 lldb::opaque_compiler_type_t type) {
4448 if (type) {
4449 const clang::FunctionProtoType *func =
4450 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4451 if (func)
4452 return func->getNumParams();
4453 }
4454 return -1;
4455}
4456
4457CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4458 lldb::opaque_compiler_type_t type, size_t idx) {
4459 if (type) {
4460 const clang::FunctionProtoType *func =
4461 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4462 if (func) {
4463 const uint32_t num_args = func->getNumParams();
4464 if (idx < num_args)
4465 return CompilerType(getASTContext(), func->getParamType(idx));
4466 }
4467 }
4468 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004469}
4470
Greg Claytona1e5dc82015-08-11 22:53:00 +00004471CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004472ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4473 if (type) {
4474 clang::QualType qual_type(GetQualType(type));
4475 const clang::FunctionProtoType *func =
4476 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4477 if (func)
4478 return CompilerType(getASTContext(), func->getReturnType());
4479 }
4480 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004481}
4482
4483size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004484ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4485 size_t num_functions = 0;
4486 if (type) {
4487 clang::QualType qual_type(GetCanonicalQualType(type));
4488 switch (qual_type->getTypeClass()) {
4489 case clang::Type::Record:
4490 if (GetCompleteQualType(getASTContext(), qual_type)) {
4491 const clang::RecordType *record_type =
4492 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4493 const clang::RecordDecl *record_decl = record_type->getDecl();
4494 assert(record_decl);
4495 const clang::CXXRecordDecl *cxx_record_decl =
4496 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4497 if (cxx_record_decl)
4498 num_functions = std::distance(cxx_record_decl->method_begin(),
4499 cxx_record_decl->method_end());
4500 }
4501 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004502
Sean Callananf9c622a2016-09-30 18:44:43 +00004503 case clang::Type::ObjCObjectPointer: {
4504 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004505 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004506 const clang::ObjCInterfaceType *objc_interface_type =
4507 objc_class_type->getInterfaceType();
4508 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004509 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4510 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004511 clang::ObjCInterfaceDecl *class_interface_decl =
4512 objc_interface_type->getDecl();
4513 if (class_interface_decl) {
4514 num_functions = std::distance(class_interface_decl->meth_begin(),
4515 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004516 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004517 }
4518 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004519 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004520
4521 case clang::Type::ObjCObject:
4522 case clang::Type::ObjCInterface:
4523 if (GetCompleteType(type)) {
4524 const clang::ObjCObjectType *objc_class_type =
4525 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4526 if (objc_class_type) {
4527 clang::ObjCInterfaceDecl *class_interface_decl =
4528 objc_class_type->getInterface();
4529 if (class_interface_decl)
4530 num_functions = std::distance(class_interface_decl->meth_begin(),
4531 class_interface_decl->meth_end());
4532 }
4533 }
4534 break;
4535
4536 case clang::Type::Typedef:
4537 return CompilerType(getASTContext(),
4538 llvm::cast<clang::TypedefType>(qual_type)
4539 ->getDecl()
4540 ->getUnderlyingType())
4541 .GetNumMemberFunctions();
4542
4543 case clang::Type::Auto:
4544 return CompilerType(
4545 getASTContext(),
4546 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4547 .GetNumMemberFunctions();
4548
4549 case clang::Type::Elaborated:
4550 return CompilerType(
4551 getASTContext(),
4552 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4553 .GetNumMemberFunctions();
4554
4555 case clang::Type::Paren:
4556 return CompilerType(getASTContext(),
4557 llvm::cast<clang::ParenType>(qual_type)->desugar())
4558 .GetNumMemberFunctions();
4559
4560 default:
4561 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004562 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004563 }
4564 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004565}
4566
4567TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004568ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4569 size_t idx) {
4570 std::string name;
4571 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4572 CompilerType clang_type;
4573 CompilerDecl clang_decl;
4574 if (type) {
4575 clang::QualType qual_type(GetCanonicalQualType(type));
4576 switch (qual_type->getTypeClass()) {
4577 case clang::Type::Record:
4578 if (GetCompleteQualType(getASTContext(), qual_type)) {
4579 const clang::RecordType *record_type =
4580 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4581 const clang::RecordDecl *record_decl = record_type->getDecl();
4582 assert(record_decl);
4583 const clang::CXXRecordDecl *cxx_record_decl =
4584 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4585 if (cxx_record_decl) {
4586 auto method_iter = cxx_record_decl->method_begin();
4587 auto method_end = cxx_record_decl->method_end();
4588 if (idx <
4589 static_cast<size_t>(std::distance(method_iter, method_end))) {
4590 std::advance(method_iter, idx);
4591 clang::CXXMethodDecl *cxx_method_decl =
4592 method_iter->getCanonicalDecl();
4593 if (cxx_method_decl) {
4594 name = cxx_method_decl->getDeclName().getAsString();
4595 if (cxx_method_decl->isStatic())
4596 kind = lldb::eMemberFunctionKindStaticMethod;
4597 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4598 kind = lldb::eMemberFunctionKindConstructor;
4599 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4600 kind = lldb::eMemberFunctionKindDestructor;
4601 else
4602 kind = lldb::eMemberFunctionKindInstanceMethod;
4603 clang_type = CompilerType(
4604 this, cxx_method_decl->getType().getAsOpaquePtr());
4605 clang_decl = CompilerDecl(this, cxx_method_decl);
4606 }
4607 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004608 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004609 }
4610 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004611
Sean Callananf9c622a2016-09-30 18:44:43 +00004612 case clang::Type::ObjCObjectPointer: {
4613 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00004614 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00004615 const clang::ObjCInterfaceType *objc_interface_type =
4616 objc_class_type->getInterfaceType();
4617 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004618 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4619 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004620 clang::ObjCInterfaceDecl *class_interface_decl =
4621 objc_interface_type->getDecl();
4622 if (class_interface_decl) {
4623 auto method_iter = class_interface_decl->meth_begin();
4624 auto method_end = class_interface_decl->meth_end();
4625 if (idx <
4626 static_cast<size_t>(std::distance(method_iter, method_end))) {
4627 std::advance(method_iter, idx);
4628 clang::ObjCMethodDecl *objc_method_decl =
4629 method_iter->getCanonicalDecl();
4630 if (objc_method_decl) {
4631 clang_decl = CompilerDecl(this, objc_method_decl);
4632 name = objc_method_decl->getSelector().getAsString();
4633 if (objc_method_decl->isClassMethod())
4634 kind = lldb::eMemberFunctionKindStaticMethod;
4635 else
4636 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004637 }
4638 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004639 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004640 }
4641 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004642 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004643
Kate Stoneb9c1b512016-09-06 20:57:50 +00004644 case clang::Type::ObjCObject:
4645 case clang::Type::ObjCInterface:
4646 if (GetCompleteType(type)) {
4647 const clang::ObjCObjectType *objc_class_type =
4648 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4649 if (objc_class_type) {
4650 clang::ObjCInterfaceDecl *class_interface_decl =
4651 objc_class_type->getInterface();
4652 if (class_interface_decl) {
4653 auto method_iter = class_interface_decl->meth_begin();
4654 auto method_end = class_interface_decl->meth_end();
4655 if (idx <
4656 static_cast<size_t>(std::distance(method_iter, method_end))) {
4657 std::advance(method_iter, idx);
4658 clang::ObjCMethodDecl *objc_method_decl =
4659 method_iter->getCanonicalDecl();
4660 if (objc_method_decl) {
4661 clang_decl = CompilerDecl(this, objc_method_decl);
4662 name = objc_method_decl->getSelector().getAsString();
4663 if (objc_method_decl->isClassMethod())
4664 kind = lldb::eMemberFunctionKindStaticMethod;
4665 else
4666 kind = lldb::eMemberFunctionKindInstanceMethod;
4667 }
4668 }
4669 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004670 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004671 }
4672 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004673
Kate Stoneb9c1b512016-09-06 20:57:50 +00004674 case clang::Type::Typedef:
4675 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4676 ->getDecl()
4677 ->getUnderlyingType()
4678 .getAsOpaquePtr(),
4679 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004680
Kate Stoneb9c1b512016-09-06 20:57:50 +00004681 case clang::Type::Auto:
4682 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4683 ->getDeducedType()
4684 .getAsOpaquePtr(),
4685 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004686
Kate Stoneb9c1b512016-09-06 20:57:50 +00004687 case clang::Type::Elaborated:
4688 return GetMemberFunctionAtIndex(
4689 llvm::cast<clang::ElaboratedType>(qual_type)
4690 ->getNamedType()
4691 .getAsOpaquePtr(),
4692 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004693
Kate Stoneb9c1b512016-09-06 20:57:50 +00004694 case clang::Type::Paren:
4695 return GetMemberFunctionAtIndex(
4696 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4697 idx);
4698
4699 default:
4700 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004701 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004702 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004703
Kate Stoneb9c1b512016-09-06 20:57:50 +00004704 if (kind == eMemberFunctionKindUnknown)
4705 return TypeMemberFunctionImpl();
4706 else
4707 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004708}
4709
Greg Claytona1e5dc82015-08-11 22:53:00 +00004710CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004711ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4712 if (type)
4713 return CompilerType(getASTContext(),
4714 GetQualType(type).getNonReferenceType());
4715 return CompilerType();
4716}
4717
4718CompilerType ClangASTContext::CreateTypedefType(
4719 const CompilerType &type, const char *typedef_name,
4720 const CompilerDeclContext &compiler_decl_ctx) {
4721 if (type && typedef_name && typedef_name[0]) {
4722 ClangASTContext *ast =
4723 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4724 if (!ast)
4725 return CompilerType();
4726 clang::ASTContext *clang_ast = ast->getASTContext();
4727 clang::QualType qual_type(ClangUtil::GetQualType(type));
4728
4729 clang::DeclContext *decl_ctx =
4730 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4731 if (decl_ctx == nullptr)
4732 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4733
4734 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4735 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4736 &clang_ast->Idents.get(typedef_name),
4737 clang_ast->getTrivialTypeSourceInfo(qual_type));
4738
4739 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4740
4741 // Get a uniqued clang::QualType for the typedef decl type
4742 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4743 }
4744 return CompilerType();
4745}
4746
4747CompilerType
4748ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4749 if (type) {
4750 clang::QualType qual_type(GetQualType(type));
4751 return CompilerType(getASTContext(),
4752 qual_type.getTypePtr()->getPointeeType());
4753 }
4754 return CompilerType();
4755}
4756
4757CompilerType
4758ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4759 if (type) {
4760 clang::QualType qual_type(GetQualType(type));
4761
4762 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4763 switch (type_class) {
4764 case clang::Type::ObjCObject:
4765 case clang::Type::ObjCInterface:
4766 return CompilerType(getASTContext(),
4767 getASTContext()->getObjCObjectPointerType(qual_type));
4768
4769 default:
4770 return CompilerType(getASTContext(),
4771 getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004772 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004773 }
4774 return CompilerType();
4775}
4776
4777CompilerType
4778ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4779 if (type)
4780 return CompilerType(this, getASTContext()
4781 ->getLValueReferenceType(GetQualType(type))
4782 .getAsOpaquePtr());
4783 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004784 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004785}
4786
Kate Stoneb9c1b512016-09-06 20:57:50 +00004787CompilerType
4788ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4789 if (type)
4790 return CompilerType(this, getASTContext()
4791 ->getRValueReferenceType(GetQualType(type))
4792 .getAsOpaquePtr());
4793 else
4794 return CompilerType();
4795}
4796
4797CompilerType
4798ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4799 if (type) {
4800 clang::QualType result(GetQualType(type));
4801 result.addConst();
4802 return CompilerType(this, result.getAsOpaquePtr());
4803 }
4804 return CompilerType();
4805}
4806
4807CompilerType
4808ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4809 if (type) {
4810 clang::QualType result(GetQualType(type));
4811 result.addVolatile();
4812 return CompilerType(this, result.getAsOpaquePtr());
4813 }
4814 return CompilerType();
4815}
4816
4817CompilerType
4818ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4819 if (type) {
4820 clang::QualType result(GetQualType(type));
4821 result.addRestrict();
4822 return CompilerType(this, result.getAsOpaquePtr());
4823 }
4824 return CompilerType();
4825}
4826
4827CompilerType
4828ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4829 const char *typedef_name,
4830 const CompilerDeclContext &compiler_decl_ctx) {
4831 if (type) {
4832 clang::ASTContext *clang_ast = getASTContext();
4833 clang::QualType qual_type(GetQualType(type));
4834
4835 clang::DeclContext *decl_ctx =
4836 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4837 if (decl_ctx == nullptr)
4838 decl_ctx = getASTContext()->getTranslationUnitDecl();
4839
4840 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4841 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4842 &clang_ast->Idents.get(typedef_name),
4843 clang_ast->getTrivialTypeSourceInfo(qual_type));
4844
4845 clang::TagDecl *tdecl = nullptr;
4846 if (!qual_type.isNull()) {
4847 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4848 tdecl = rt->getDecl();
4849 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4850 tdecl = et->getDecl();
4851 }
4852
4853 // Check whether this declaration is an anonymous struct, union, or enum,
4854 // hidden behind a typedef. If so, we
4855 // try to check whether we have a typedef tag to attach to the original
4856 // record declaration
4857 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4858 tdecl->setTypedefNameForAnonDecl(decl);
4859
4860 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4861
4862 // Get a uniqued clang::QualType for the typedef decl type
4863 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4864 }
4865 return CompilerType();
4866}
4867
4868CompilerType
4869ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4870 if (type) {
4871 const clang::TypedefType *typedef_type =
4872 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4873 if (typedef_type)
4874 return CompilerType(getASTContext(),
4875 typedef_type->getDecl()->getUnderlyingType());
4876 }
4877 return CompilerType();
4878}
Greg Claytond8d4a572015-08-11 21:38:15 +00004879
4880//----------------------------------------------------------------------
4881// Create related types using the current type's AST
4882//----------------------------------------------------------------------
4883
Kate Stoneb9c1b512016-09-06 20:57:50 +00004884CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4885 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004886}
4887//----------------------------------------------------------------------
4888// Exploring the type
4889//----------------------------------------------------------------------
4890
Kate Stoneb9c1b512016-09-06 20:57:50 +00004891uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4892 ExecutionContextScope *exe_scope) {
4893 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00004894 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004895 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004896 switch (type_class) {
4897 case clang::Type::Record:
4898 if (GetCompleteType(type))
4899 return getASTContext()->getTypeSize(qual_type);
4900 else
4901 return 0;
4902 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004903
Kate Stoneb9c1b512016-09-06 20:57:50 +00004904 case clang::Type::ObjCInterface:
4905 case clang::Type::ObjCObject: {
4906 ExecutionContext exe_ctx(exe_scope);
4907 Process *process = exe_ctx.GetProcessPtr();
4908 if (process) {
4909 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4910 if (objc_runtime) {
4911 uint64_t bit_size = 0;
4912 if (objc_runtime->GetTypeBitSize(
4913 CompilerType(getASTContext(), qual_type), bit_size))
4914 return bit_size;
4915 }
4916 } else {
4917 static bool g_printed = false;
4918 if (!g_printed) {
4919 StreamString s;
4920 DumpTypeDescription(type, &s);
4921
4922 llvm::outs() << "warning: trying to determine the size of type ";
4923 llvm::outs() << s.GetString() << "\n";
4924 llvm::outs() << "without a valid ExecutionContext. this is not "
4925 "reliable. please file a bug against LLDB.\n";
4926 llvm::outs() << "backtrace:\n";
4927 llvm::sys::PrintStackTrace(llvm::outs());
4928 llvm::outs() << "\n";
4929 g_printed = true;
4930 }
4931 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004932 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004933 LLVM_FALLTHROUGH;
4934 default:
4935 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
4936 if (bit_size == 0) {
4937 if (qual_type->isIncompleteArrayType())
4938 return getASTContext()->getTypeSize(
4939 qual_type->getArrayElementTypeNoTypeQual()
4940 ->getCanonicalTypeUnqualified());
4941 }
4942 if (qual_type->isObjCObjectOrInterfaceType())
4943 return bit_size +
4944 getASTContext()->getTypeSize(
4945 getASTContext()->ObjCBuiltinClassTy);
4946 return bit_size;
4947 }
4948 }
4949 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004950}
4951
Kate Stoneb9c1b512016-09-06 20:57:50 +00004952size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
4953 if (GetCompleteType(type))
4954 return getASTContext()->getTypeAlign(GetQualType(type));
4955 return 0;
4956}
4957
4958lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
4959 uint64_t &count) {
4960 if (!type)
4961 return lldb::eEncodingInvalid;
4962
4963 count = 1;
4964 clang::QualType qual_type(GetCanonicalQualType(type));
4965
4966 switch (qual_type->getTypeClass()) {
4967 case clang::Type::UnaryTransform:
4968 break;
4969
4970 case clang::Type::FunctionNoProto:
4971 case clang::Type::FunctionProto:
4972 break;
4973
4974 case clang::Type::IncompleteArray:
4975 case clang::Type::VariableArray:
4976 break;
4977
4978 case clang::Type::ConstantArray:
4979 break;
4980
4981 case clang::Type::ExtVector:
4982 case clang::Type::Vector:
4983 // TODO: Set this to more than one???
4984 break;
4985
4986 case clang::Type::Builtin:
4987 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4988 case clang::BuiltinType::Void:
4989 break;
4990
4991 case clang::BuiltinType::Bool:
4992 case clang::BuiltinType::Char_S:
4993 case clang::BuiltinType::SChar:
4994 case clang::BuiltinType::WChar_S:
4995 case clang::BuiltinType::Char16:
4996 case clang::BuiltinType::Char32:
4997 case clang::BuiltinType::Short:
4998 case clang::BuiltinType::Int:
4999 case clang::BuiltinType::Long:
5000 case clang::BuiltinType::LongLong:
5001 case clang::BuiltinType::Int128:
5002 return lldb::eEncodingSint;
5003
5004 case clang::BuiltinType::Char_U:
5005 case clang::BuiltinType::UChar:
5006 case clang::BuiltinType::WChar_U:
5007 case clang::BuiltinType::UShort:
5008 case clang::BuiltinType::UInt:
5009 case clang::BuiltinType::ULong:
5010 case clang::BuiltinType::ULongLong:
5011 case clang::BuiltinType::UInt128:
5012 return lldb::eEncodingUint;
5013
5014 case clang::BuiltinType::Half:
5015 case clang::BuiltinType::Float:
Ted Woodward4355c7c2017-09-20 19:16:53 +00005016 case clang::BuiltinType::Float16:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005017 case clang::BuiltinType::Float128:
5018 case clang::BuiltinType::Double:
5019 case clang::BuiltinType::LongDouble:
5020 return lldb::eEncodingIEEE754;
5021
5022 case clang::BuiltinType::ObjCClass:
5023 case clang::BuiltinType::ObjCId:
5024 case clang::BuiltinType::ObjCSel:
5025 return lldb::eEncodingUint;
5026
5027 case clang::BuiltinType::NullPtr:
5028 return lldb::eEncodingUint;
5029
5030 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5031 case clang::BuiltinType::Kind::BoundMember:
5032 case clang::BuiltinType::Kind::BuiltinFn:
5033 case clang::BuiltinType::Kind::Dependent:
5034 case clang::BuiltinType::Kind::OCLClkEvent:
5035 case clang::BuiltinType::Kind::OCLEvent:
5036 case clang::BuiltinType::Kind::OCLImage1dRO:
5037 case clang::BuiltinType::Kind::OCLImage1dWO:
5038 case clang::BuiltinType::Kind::OCLImage1dRW:
5039 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5040 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5041 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5042 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5043 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5044 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5045 case clang::BuiltinType::Kind::OCLImage2dRO:
5046 case clang::BuiltinType::Kind::OCLImage2dWO:
5047 case clang::BuiltinType::Kind::OCLImage2dRW:
5048 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5049 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5050 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5051 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5052 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5053 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5054 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5055 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5056 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5057 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5058 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5059 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5060 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5061 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5062 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5063 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5064 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5065 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5066 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5067 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5068 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5069 case clang::BuiltinType::Kind::OCLImage3dRO:
5070 case clang::BuiltinType::Kind::OCLImage3dWO:
5071 case clang::BuiltinType::Kind::OCLImage3dRW:
5072 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005073 case clang::BuiltinType::Kind::OCLReserveID:
5074 case clang::BuiltinType::Kind::OCLSampler:
5075 case clang::BuiltinType::Kind::OMPArraySection:
5076 case clang::BuiltinType::Kind::Overload:
5077 case clang::BuiltinType::Kind::PseudoObject:
5078 case clang::BuiltinType::Kind::UnknownAny:
5079 break;
5080 }
5081 break;
5082 // All pointer types are represented as unsigned integer encodings.
5083 // We may nee to add a eEncodingPointer if we ever need to know the
5084 // difference
5085 case clang::Type::ObjCObjectPointer:
5086 case clang::Type::BlockPointer:
5087 case clang::Type::Pointer:
5088 case clang::Type::LValueReference:
5089 case clang::Type::RValueReference:
5090 case clang::Type::MemberPointer:
5091 return lldb::eEncodingUint;
5092 case clang::Type::Complex: {
5093 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5094 if (qual_type->isComplexType())
5095 encoding = lldb::eEncodingIEEE754;
5096 else {
5097 const clang::ComplexType *complex_type =
5098 qual_type->getAsComplexIntegerType();
5099 if (complex_type)
5100 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5101 .GetEncoding(count);
5102 else
5103 encoding = lldb::eEncodingSint;
5104 }
5105 count = 2;
5106 return encoding;
5107 }
5108
5109 case clang::Type::ObjCInterface:
5110 break;
5111 case clang::Type::Record:
5112 break;
5113 case clang::Type::Enum:
5114 return lldb::eEncodingSint;
5115 case clang::Type::Typedef:
5116 return CompilerType(getASTContext(),
5117 llvm::cast<clang::TypedefType>(qual_type)
5118 ->getDecl()
5119 ->getUnderlyingType())
5120 .GetEncoding(count);
5121
5122 case clang::Type::Auto:
5123 return CompilerType(
5124 getASTContext(),
5125 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5126 .GetEncoding(count);
5127
5128 case clang::Type::Elaborated:
5129 return CompilerType(
5130 getASTContext(),
5131 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5132 .GetEncoding(count);
5133
5134 case clang::Type::Paren:
5135 return CompilerType(getASTContext(),
5136 llvm::cast<clang::ParenType>(qual_type)->desugar())
5137 .GetEncoding(count);
5138
5139 case clang::Type::DependentSizedArray:
5140 case clang::Type::DependentSizedExtVector:
5141 case clang::Type::UnresolvedUsing:
5142 case clang::Type::Attributed:
5143 case clang::Type::TemplateTypeParm:
5144 case clang::Type::SubstTemplateTypeParm:
5145 case clang::Type::SubstTemplateTypeParmPack:
5146 case clang::Type::InjectedClassName:
5147 case clang::Type::DependentName:
5148 case clang::Type::DependentTemplateSpecialization:
5149 case clang::Type::PackExpansion:
5150 case clang::Type::ObjCObject:
5151
5152 case clang::Type::TypeOfExpr:
5153 case clang::Type::TypeOf:
5154 case clang::Type::Decltype:
5155 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005156 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005157 case clang::Type::Atomic:
5158 case clang::Type::Adjusted:
5159 case clang::Type::Pipe:
5160 break;
5161
5162 // pointer type decayed from an array or function type.
5163 case clang::Type::Decayed:
5164 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005165 case clang::Type::ObjCTypeParam:
5166 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005167
5168 case clang::Type::DependentAddressSpace:
5169 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005170 }
5171 count = 0;
5172 return lldb::eEncodingInvalid;
5173}
5174
5175lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5176 if (!type)
5177 return lldb::eFormatDefault;
5178
5179 clang::QualType qual_type(GetCanonicalQualType(type));
5180
5181 switch (qual_type->getTypeClass()) {
5182 case clang::Type::UnaryTransform:
5183 break;
5184
5185 case clang::Type::FunctionNoProto:
5186 case clang::Type::FunctionProto:
5187 break;
5188
5189 case clang::Type::IncompleteArray:
5190 case clang::Type::VariableArray:
5191 break;
5192
5193 case clang::Type::ConstantArray:
5194 return lldb::eFormatVoid; // no value
5195
5196 case clang::Type::ExtVector:
5197 case clang::Type::Vector:
5198 break;
5199
5200 case clang::Type::Builtin:
5201 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5202 // default: assert(0 && "Unknown builtin type!");
5203 case clang::BuiltinType::UnknownAny:
5204 case clang::BuiltinType::Void:
5205 case clang::BuiltinType::BoundMember:
5206 break;
5207
5208 case clang::BuiltinType::Bool:
5209 return lldb::eFormatBoolean;
5210 case clang::BuiltinType::Char_S:
5211 case clang::BuiltinType::SChar:
5212 case clang::BuiltinType::WChar_S:
5213 case clang::BuiltinType::Char_U:
5214 case clang::BuiltinType::UChar:
5215 case clang::BuiltinType::WChar_U:
5216 return lldb::eFormatChar;
5217 case clang::BuiltinType::Char16:
5218 return lldb::eFormatUnicode16;
5219 case clang::BuiltinType::Char32:
5220 return lldb::eFormatUnicode32;
5221 case clang::BuiltinType::UShort:
5222 return lldb::eFormatUnsigned;
5223 case clang::BuiltinType::Short:
5224 return lldb::eFormatDecimal;
5225 case clang::BuiltinType::UInt:
5226 return lldb::eFormatUnsigned;
5227 case clang::BuiltinType::Int:
5228 return lldb::eFormatDecimal;
5229 case clang::BuiltinType::ULong:
5230 return lldb::eFormatUnsigned;
5231 case clang::BuiltinType::Long:
5232 return lldb::eFormatDecimal;
5233 case clang::BuiltinType::ULongLong:
5234 return lldb::eFormatUnsigned;
5235 case clang::BuiltinType::LongLong:
5236 return lldb::eFormatDecimal;
5237 case clang::BuiltinType::UInt128:
5238 return lldb::eFormatUnsigned;
5239 case clang::BuiltinType::Int128:
5240 return lldb::eFormatDecimal;
5241 case clang::BuiltinType::Half:
5242 case clang::BuiltinType::Float:
5243 case clang::BuiltinType::Double:
5244 case clang::BuiltinType::LongDouble:
5245 return lldb::eFormatFloat;
5246 default:
5247 return lldb::eFormatHex;
5248 }
5249 break;
5250 case clang::Type::ObjCObjectPointer:
5251 return lldb::eFormatHex;
5252 case clang::Type::BlockPointer:
5253 return lldb::eFormatHex;
5254 case clang::Type::Pointer:
5255 return lldb::eFormatHex;
5256 case clang::Type::LValueReference:
5257 case clang::Type::RValueReference:
5258 return lldb::eFormatHex;
5259 case clang::Type::MemberPointer:
5260 break;
5261 case clang::Type::Complex: {
5262 if (qual_type->isComplexType())
5263 return lldb::eFormatComplex;
5264 else
5265 return lldb::eFormatComplexInteger;
5266 }
5267 case clang::Type::ObjCInterface:
5268 break;
5269 case clang::Type::Record:
5270 break;
5271 case clang::Type::Enum:
5272 return lldb::eFormatEnum;
5273 case clang::Type::Typedef:
5274 return CompilerType(getASTContext(),
5275 llvm::cast<clang::TypedefType>(qual_type)
5276 ->getDecl()
5277 ->getUnderlyingType())
5278 .GetFormat();
5279 case clang::Type::Auto:
5280 return CompilerType(getASTContext(),
5281 llvm::cast<clang::AutoType>(qual_type)->desugar())
5282 .GetFormat();
5283 case clang::Type::Paren:
5284 return CompilerType(getASTContext(),
5285 llvm::cast<clang::ParenType>(qual_type)->desugar())
5286 .GetFormat();
5287 case clang::Type::Elaborated:
5288 return CompilerType(
5289 getASTContext(),
5290 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5291 .GetFormat();
5292 case clang::Type::DependentSizedArray:
5293 case clang::Type::DependentSizedExtVector:
5294 case clang::Type::UnresolvedUsing:
5295 case clang::Type::Attributed:
5296 case clang::Type::TemplateTypeParm:
5297 case clang::Type::SubstTemplateTypeParm:
5298 case clang::Type::SubstTemplateTypeParmPack:
5299 case clang::Type::InjectedClassName:
5300 case clang::Type::DependentName:
5301 case clang::Type::DependentTemplateSpecialization:
5302 case clang::Type::PackExpansion:
5303 case clang::Type::ObjCObject:
5304
5305 case clang::Type::TypeOfExpr:
5306 case clang::Type::TypeOf:
5307 case clang::Type::Decltype:
5308 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005309 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005310 case clang::Type::Atomic:
5311 case clang::Type::Adjusted:
5312 case clang::Type::Pipe:
5313 break;
5314
5315 // pointer type decayed from an array or function type.
5316 case clang::Type::Decayed:
5317 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005318 case clang::Type::ObjCTypeParam:
5319 break;
Ted Woodward66060cf2017-10-11 22:42:21 +00005320
5321 case clang::Type::DependentAddressSpace:
5322 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005323 }
5324 // We don't know hot to display this type...
5325 return lldb::eFormatBytes;
5326}
5327
5328static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5329 bool check_superclass) {
5330 while (class_interface_decl) {
5331 if (class_interface_decl->ivar_size() > 0)
5332 return true;
5333
5334 if (check_superclass)
5335 class_interface_decl = class_interface_decl->getSuperClass();
5336 else
5337 break;
5338 }
5339 return false;
5340}
5341
5342uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
5343 bool omit_empty_base_classes) {
5344 if (!type)
5345 return 0;
5346
5347 uint32_t num_children = 0;
5348 clang::QualType qual_type(GetQualType(type));
5349 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5350 switch (type_class) {
5351 case clang::Type::Builtin:
5352 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5353 case clang::BuiltinType::ObjCId: // child is Class
5354 case clang::BuiltinType::ObjCClass: // child is Class
5355 num_children = 1;
5356 break;
5357
5358 default:
5359 break;
5360 }
5361 break;
5362
5363 case clang::Type::Complex:
5364 return 0;
5365
5366 case clang::Type::Record:
5367 if (GetCompleteQualType(getASTContext(), qual_type)) {
5368 const clang::RecordType *record_type =
5369 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5370 const clang::RecordDecl *record_decl = record_type->getDecl();
5371 assert(record_decl);
5372 const clang::CXXRecordDecl *cxx_record_decl =
5373 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5374 if (cxx_record_decl) {
5375 if (omit_empty_base_classes) {
5376 // Check each base classes to see if it or any of its
5377 // base classes contain any fields. This can help
5378 // limit the noise in variable views by not having to
5379 // show base classes that contain no members.
5380 clang::CXXRecordDecl::base_class_const_iterator base_class,
5381 base_class_end;
5382 for (base_class = cxx_record_decl->bases_begin(),
5383 base_class_end = cxx_record_decl->bases_end();
5384 base_class != base_class_end; ++base_class) {
5385 const clang::CXXRecordDecl *base_class_decl =
5386 llvm::cast<clang::CXXRecordDecl>(
5387 base_class->getType()
5388 ->getAs<clang::RecordType>()
5389 ->getDecl());
5390
5391 // Skip empty base classes
5392 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5393 continue;
5394
5395 num_children++;
5396 }
5397 } else {
5398 // Include all base classes
5399 num_children += cxx_record_decl->getNumBases();
5400 }
5401 }
5402 clang::RecordDecl::field_iterator field, field_end;
5403 for (field = record_decl->field_begin(),
5404 field_end = record_decl->field_end();
5405 field != field_end; ++field)
5406 ++num_children;
5407 }
5408 break;
5409
5410 case clang::Type::ObjCObject:
5411 case clang::Type::ObjCInterface:
5412 if (GetCompleteQualType(getASTContext(), qual_type)) {
5413 const clang::ObjCObjectType *objc_class_type =
5414 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5415 assert(objc_class_type);
5416 if (objc_class_type) {
5417 clang::ObjCInterfaceDecl *class_interface_decl =
5418 objc_class_type->getInterface();
5419
5420 if (class_interface_decl) {
5421
5422 clang::ObjCInterfaceDecl *superclass_interface_decl =
5423 class_interface_decl->getSuperClass();
5424 if (superclass_interface_decl) {
5425 if (omit_empty_base_classes) {
5426 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5427 ++num_children;
5428 } else
5429 ++num_children;
5430 }
5431
5432 num_children += class_interface_decl->ivar_size();
5433 }
5434 }
5435 }
5436 break;
5437
5438 case clang::Type::ObjCObjectPointer: {
5439 const clang::ObjCObjectPointerType *pointer_type =
5440 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5441 clang::QualType pointee_type = pointer_type->getPointeeType();
5442 uint32_t num_pointee_children =
5443 CompilerType(getASTContext(), pointee_type)
5444 .GetNumChildren(omit_empty_base_classes);
5445 // If this type points to a simple type, then it has 1 child
5446 if (num_pointee_children == 0)
5447 num_children = 1;
5448 else
5449 num_children = num_pointee_children;
5450 } break;
5451
5452 case clang::Type::Vector:
5453 case clang::Type::ExtVector:
5454 num_children =
5455 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5456 break;
5457
5458 case clang::Type::ConstantArray:
5459 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5460 ->getSize()
5461 .getLimitedValue();
5462 break;
5463
5464 case clang::Type::Pointer: {
5465 const clang::PointerType *pointer_type =
5466 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5467 clang::QualType pointee_type(pointer_type->getPointeeType());
5468 uint32_t num_pointee_children =
5469 CompilerType(getASTContext(), pointee_type)
5470 .GetNumChildren(omit_empty_base_classes);
5471 if (num_pointee_children == 0) {
5472 // We have a pointer to a pointee type that claims it has no children.
5473 // We will want to look at
5474 num_children = GetNumPointeeChildren(pointee_type);
5475 } else
5476 num_children = num_pointee_children;
5477 } break;
5478
5479 case clang::Type::LValueReference:
5480 case clang::Type::RValueReference: {
5481 const clang::ReferenceType *reference_type =
5482 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5483 clang::QualType pointee_type = reference_type->getPointeeType();
5484 uint32_t num_pointee_children =
5485 CompilerType(getASTContext(), pointee_type)
5486 .GetNumChildren(omit_empty_base_classes);
5487 // If this type points to a simple type, then it has 1 child
5488 if (num_pointee_children == 0)
5489 num_children = 1;
5490 else
5491 num_children = num_pointee_children;
5492 } break;
5493
5494 case clang::Type::Typedef:
5495 num_children =
5496 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5497 ->getDecl()
5498 ->getUnderlyingType())
5499 .GetNumChildren(omit_empty_base_classes);
5500 break;
5501
5502 case clang::Type::Auto:
5503 num_children =
5504 CompilerType(getASTContext(),
5505 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5506 .GetNumChildren(omit_empty_base_classes);
5507 break;
5508
5509 case clang::Type::Elaborated:
5510 num_children =
5511 CompilerType(
5512 getASTContext(),
5513 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5514 .GetNumChildren(omit_empty_base_classes);
5515 break;
5516
5517 case clang::Type::Paren:
5518 num_children =
5519 CompilerType(getASTContext(),
5520 llvm::cast<clang::ParenType>(qual_type)->desugar())
5521 .GetNumChildren(omit_empty_base_classes);
5522 break;
5523 default:
5524 break;
5525 }
5526 return num_children;
5527}
5528
5529CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5530 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005531}
5532
Greg Claytond8d4a572015-08-11 21:38:15 +00005533lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005534ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5535 if (type) {
5536 clang::QualType qual_type(GetQualType(type));
5537 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5538 if (type_class == clang::Type::Builtin) {
5539 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5540 case clang::BuiltinType::Void:
5541 return eBasicTypeVoid;
5542 case clang::BuiltinType::Bool:
5543 return eBasicTypeBool;
5544 case clang::BuiltinType::Char_S:
5545 return eBasicTypeSignedChar;
5546 case clang::BuiltinType::Char_U:
5547 return eBasicTypeUnsignedChar;
5548 case clang::BuiltinType::Char16:
5549 return eBasicTypeChar16;
5550 case clang::BuiltinType::Char32:
5551 return eBasicTypeChar32;
5552 case clang::BuiltinType::UChar:
5553 return eBasicTypeUnsignedChar;
5554 case clang::BuiltinType::SChar:
5555 return eBasicTypeSignedChar;
5556 case clang::BuiltinType::WChar_S:
5557 return eBasicTypeSignedWChar;
5558 case clang::BuiltinType::WChar_U:
5559 return eBasicTypeUnsignedWChar;
5560 case clang::BuiltinType::Short:
5561 return eBasicTypeShort;
5562 case clang::BuiltinType::UShort:
5563 return eBasicTypeUnsignedShort;
5564 case clang::BuiltinType::Int:
5565 return eBasicTypeInt;
5566 case clang::BuiltinType::UInt:
5567 return eBasicTypeUnsignedInt;
5568 case clang::BuiltinType::Long:
5569 return eBasicTypeLong;
5570 case clang::BuiltinType::ULong:
5571 return eBasicTypeUnsignedLong;
5572 case clang::BuiltinType::LongLong:
5573 return eBasicTypeLongLong;
5574 case clang::BuiltinType::ULongLong:
5575 return eBasicTypeUnsignedLongLong;
5576 case clang::BuiltinType::Int128:
5577 return eBasicTypeInt128;
5578 case clang::BuiltinType::UInt128:
5579 return eBasicTypeUnsignedInt128;
5580
5581 case clang::BuiltinType::Half:
5582 return eBasicTypeHalf;
5583 case clang::BuiltinType::Float:
5584 return eBasicTypeFloat;
5585 case clang::BuiltinType::Double:
5586 return eBasicTypeDouble;
5587 case clang::BuiltinType::LongDouble:
5588 return eBasicTypeLongDouble;
5589
5590 case clang::BuiltinType::NullPtr:
5591 return eBasicTypeNullPtr;
5592 case clang::BuiltinType::ObjCId:
5593 return eBasicTypeObjCID;
5594 case clang::BuiltinType::ObjCClass:
5595 return eBasicTypeObjCClass;
5596 case clang::BuiltinType::ObjCSel:
5597 return eBasicTypeObjCSel;
5598 default:
5599 return eBasicTypeOther;
5600 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005601 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005602 }
5603 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005604}
5605
Kate Stoneb9c1b512016-09-06 20:57:50 +00005606void ClangASTContext::ForEachEnumerator(
5607 lldb::opaque_compiler_type_t type,
5608 std::function<bool(const CompilerType &integer_type,
5609 const ConstString &name,
5610 const llvm::APSInt &value)> const &callback) {
5611 const clang::EnumType *enum_type =
5612 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5613 if (enum_type) {
5614 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5615 if (enum_decl) {
5616 CompilerType integer_type(this,
5617 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005618
Kate Stoneb9c1b512016-09-06 20:57:50 +00005619 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5620 for (enum_pos = enum_decl->enumerator_begin(),
5621 enum_end_pos = enum_decl->enumerator_end();
5622 enum_pos != enum_end_pos; ++enum_pos) {
5623 ConstString name(enum_pos->getNameAsString().c_str());
5624 if (!callback(integer_type, name, enum_pos->getInitVal()))
5625 break;
5626 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005627 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005628 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005629}
5630
Greg Claytond8d4a572015-08-11 21:38:15 +00005631#pragma mark Aggregate Types
5632
Kate Stoneb9c1b512016-09-06 20:57:50 +00005633uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5634 if (!type)
5635 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005636
Kate Stoneb9c1b512016-09-06 20:57:50 +00005637 uint32_t count = 0;
5638 clang::QualType qual_type(GetCanonicalQualType(type));
5639 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5640 switch (type_class) {
5641 case clang::Type::Record:
5642 if (GetCompleteType(type)) {
5643 const clang::RecordType *record_type =
5644 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5645 if (record_type) {
5646 clang::RecordDecl *record_decl = record_type->getDecl();
5647 if (record_decl) {
5648 uint32_t field_idx = 0;
5649 clang::RecordDecl::field_iterator field, field_end;
5650 for (field = record_decl->field_begin(),
5651 field_end = record_decl->field_end();
5652 field != field_end; ++field)
5653 ++field_idx;
5654 count = field_idx;
5655 }
5656 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005657 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005658 break;
5659
5660 case clang::Type::Typedef:
5661 count =
5662 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5663 ->getDecl()
5664 ->getUnderlyingType())
5665 .GetNumFields();
5666 break;
5667
5668 case clang::Type::Auto:
5669 count =
5670 CompilerType(getASTContext(),
5671 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5672 .GetNumFields();
5673 break;
5674
5675 case clang::Type::Elaborated:
5676 count = CompilerType(
5677 getASTContext(),
5678 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5679 .GetNumFields();
5680 break;
5681
5682 case clang::Type::Paren:
5683 count = CompilerType(getASTContext(),
5684 llvm::cast<clang::ParenType>(qual_type)->desugar())
5685 .GetNumFields();
5686 break;
5687
Sean Callananf9c622a2016-09-30 18:44:43 +00005688 case clang::Type::ObjCObjectPointer: {
5689 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005690 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005691 const clang::ObjCInterfaceType *objc_interface_type =
5692 objc_class_type->getInterfaceType();
5693 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005694 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5695 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005696 clang::ObjCInterfaceDecl *class_interface_decl =
5697 objc_interface_type->getDecl();
5698 if (class_interface_decl) {
5699 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005700 }
5701 }
5702 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005703 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005704
5705 case clang::Type::ObjCObject:
5706 case clang::Type::ObjCInterface:
5707 if (GetCompleteType(type)) {
5708 const clang::ObjCObjectType *objc_class_type =
5709 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5710 if (objc_class_type) {
5711 clang::ObjCInterfaceDecl *class_interface_decl =
5712 objc_class_type->getInterface();
5713
5714 if (class_interface_decl)
5715 count = class_interface_decl->ivar_size();
5716 }
5717 }
5718 break;
5719
5720 default:
5721 break;
5722 }
5723 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005724}
5725
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005726static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005727GetObjCFieldAtIndex(clang::ASTContext *ast,
5728 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5729 std::string &name, uint64_t *bit_offset_ptr,
5730 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5731 if (class_interface_decl) {
5732 if (idx < (class_interface_decl->ivar_size())) {
5733 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5734 ivar_end = class_interface_decl->ivar_end();
5735 uint32_t ivar_idx = 0;
5736
5737 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5738 ++ivar_pos, ++ivar_idx) {
5739 if (ivar_idx == idx) {
5740 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5741
5742 clang::QualType ivar_qual_type(ivar_decl->getType());
5743
5744 name.assign(ivar_decl->getNameAsString());
5745
5746 if (bit_offset_ptr) {
5747 const clang::ASTRecordLayout &interface_layout =
5748 ast->getASTObjCInterfaceLayout(class_interface_decl);
5749 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5750 }
5751
5752 const bool is_bitfield = ivar_pos->isBitField();
5753
5754 if (bitfield_bit_size_ptr) {
5755 *bitfield_bit_size_ptr = 0;
5756
5757 if (is_bitfield && ast) {
5758 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5759 llvm::APSInt bitfield_apsint;
5760 if (bitfield_bit_size_expr &&
5761 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5762 *ast)) {
5763 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5764 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005765 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005766 }
5767 if (is_bitfield_ptr)
5768 *is_bitfield_ptr = is_bitfield;
5769
5770 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005771 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005772 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005773 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005774 }
5775 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005776}
5777
Kate Stoneb9c1b512016-09-06 20:57:50 +00005778CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5779 size_t idx, std::string &name,
5780 uint64_t *bit_offset_ptr,
5781 uint32_t *bitfield_bit_size_ptr,
5782 bool *is_bitfield_ptr) {
5783 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005784 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005785
5786 clang::QualType qual_type(GetCanonicalQualType(type));
5787 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5788 switch (type_class) {
5789 case clang::Type::Record:
5790 if (GetCompleteType(type)) {
5791 const clang::RecordType *record_type =
5792 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5793 const clang::RecordDecl *record_decl = record_type->getDecl();
5794 uint32_t field_idx = 0;
5795 clang::RecordDecl::field_iterator field, field_end;
5796 for (field = record_decl->field_begin(),
5797 field_end = record_decl->field_end();
5798 field != field_end; ++field, ++field_idx) {
5799 if (idx == field_idx) {
5800 // Print the member type if requested
5801 // Print the member name and equal sign
5802 name.assign(field->getNameAsString());
5803
5804 // Figure out the type byte size (field_type_info.first) and
5805 // alignment (field_type_info.second) from the AST context.
5806 if (bit_offset_ptr) {
5807 const clang::ASTRecordLayout &record_layout =
5808 getASTContext()->getASTRecordLayout(record_decl);
5809 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5810 }
5811
5812 const bool is_bitfield = field->isBitField();
5813
5814 if (bitfield_bit_size_ptr) {
5815 *bitfield_bit_size_ptr = 0;
5816
5817 if (is_bitfield) {
5818 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5819 llvm::APSInt bitfield_apsint;
5820 if (bitfield_bit_size_expr &&
5821 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5822 *getASTContext())) {
5823 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5824 }
5825 }
5826 }
5827 if (is_bitfield_ptr)
5828 *is_bitfield_ptr = is_bitfield;
5829
5830 return CompilerType(getASTContext(), field->getType());
5831 }
5832 }
5833 }
5834 break;
5835
Sean Callananf9c622a2016-09-30 18:44:43 +00005836 case clang::Type::ObjCObjectPointer: {
5837 const clang::ObjCObjectPointerType *objc_class_type =
Sean Callanan732a6f42017-05-15 19:55:20 +00005838 qual_type->getAs<clang::ObjCObjectPointerType>();
Sean Callananf9c622a2016-09-30 18:44:43 +00005839 const clang::ObjCInterfaceType *objc_interface_type =
5840 objc_class_type->getInterfaceType();
5841 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005842 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5843 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005844 clang::ObjCInterfaceDecl *class_interface_decl =
5845 objc_interface_type->getDecl();
5846 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005847 return CompilerType(
5848 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5849 idx, name, bit_offset_ptr,
5850 bitfield_bit_size_ptr, is_bitfield_ptr));
5851 }
5852 }
5853 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005854 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005855
5856 case clang::Type::ObjCObject:
5857 case clang::Type::ObjCInterface:
5858 if (GetCompleteType(type)) {
5859 const clang::ObjCObjectType *objc_class_type =
5860 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5861 assert(objc_class_type);
5862 if (objc_class_type) {
5863 clang::ObjCInterfaceDecl *class_interface_decl =
5864 objc_class_type->getInterface();
5865 return CompilerType(
5866 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5867 idx, name, bit_offset_ptr,
5868 bitfield_bit_size_ptr, is_bitfield_ptr));
5869 }
5870 }
5871 break;
5872
5873 case clang::Type::Typedef:
5874 return CompilerType(getASTContext(),
5875 llvm::cast<clang::TypedefType>(qual_type)
5876 ->getDecl()
5877 ->getUnderlyingType())
5878 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5879 is_bitfield_ptr);
5880
5881 case clang::Type::Auto:
5882 return CompilerType(
5883 getASTContext(),
5884 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5885 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5886 is_bitfield_ptr);
5887
5888 case clang::Type::Elaborated:
5889 return CompilerType(
5890 getASTContext(),
5891 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5892 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5893 is_bitfield_ptr);
5894
5895 case clang::Type::Paren:
5896 return CompilerType(getASTContext(),
5897 llvm::cast<clang::ParenType>(qual_type)->desugar())
5898 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5899 is_bitfield_ptr);
5900
5901 default:
5902 break;
5903 }
5904 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005905}
5906
Greg Clayton99558cc42015-08-24 23:46:31 +00005907uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005908ClangASTContext::GetNumDirectBaseClasses(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->getNumBases();
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::ObjCObjectPointer:
5923 count = GetPointeeType(type).GetNumDirectBaseClasses();
5924 break;
5925
5926 case clang::Type::ObjCObject:
5927 if (GetCompleteType(type)) {
5928 const clang::ObjCObjectType *objc_class_type =
5929 qual_type->getAsObjCQualifiedInterfaceType();
5930 if (objc_class_type) {
5931 clang::ObjCInterfaceDecl *class_interface_decl =
5932 objc_class_type->getInterface();
5933
5934 if (class_interface_decl && class_interface_decl->getSuperClass())
5935 count = 1;
5936 }
5937 }
5938 break;
5939 case clang::Type::ObjCInterface:
5940 if (GetCompleteType(type)) {
5941 const clang::ObjCInterfaceType *objc_interface_type =
5942 qual_type->getAs<clang::ObjCInterfaceType>();
5943 if (objc_interface_type) {
5944 clang::ObjCInterfaceDecl *class_interface_decl =
5945 objc_interface_type->getInterface();
5946
5947 if (class_interface_decl && class_interface_decl->getSuperClass())
5948 count = 1;
5949 }
5950 }
5951 break;
5952
5953 case clang::Type::Typedef:
5954 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5955 ->getDecl()
5956 ->getUnderlyingType()
5957 .getAsOpaquePtr());
5958 break;
5959
5960 case clang::Type::Auto:
5961 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5962 ->getDeducedType()
5963 .getAsOpaquePtr());
5964 break;
5965
5966 case clang::Type::Elaborated:
5967 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5968 ->getNamedType()
5969 .getAsOpaquePtr());
5970 break;
5971
5972 case clang::Type::Paren:
5973 return GetNumDirectBaseClasses(
5974 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5975
5976 default:
5977 break;
5978 }
5979 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00005980}
5981
5982uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005983ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
5984 uint32_t count = 0;
5985 clang::QualType qual_type(GetCanonicalQualType(type));
5986 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5987 switch (type_class) {
5988 case clang::Type::Record:
5989 if (GetCompleteType(type)) {
5990 const clang::CXXRecordDecl *cxx_record_decl =
5991 qual_type->getAsCXXRecordDecl();
5992 if (cxx_record_decl)
5993 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00005994 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005995 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00005996
Kate Stoneb9c1b512016-09-06 20:57:50 +00005997 case clang::Type::Typedef:
5998 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5999 ->getDecl()
6000 ->getUnderlyingType()
6001 .getAsOpaquePtr());
6002 break;
6003
6004 case clang::Type::Auto:
6005 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6006 ->getDeducedType()
6007 .getAsOpaquePtr());
6008 break;
6009
6010 case clang::Type::Elaborated:
6011 count =
6012 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6013 ->getNamedType()
6014 .getAsOpaquePtr());
6015 break;
6016
6017 case clang::Type::Paren:
6018 count = GetNumVirtualBaseClasses(
6019 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6020 break;
6021
6022 default:
6023 break;
6024 }
6025 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00006026}
6027
Kate Stoneb9c1b512016-09-06 20:57:50 +00006028CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6029 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6030 clang::QualType qual_type(GetCanonicalQualType(type));
6031 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6032 switch (type_class) {
6033 case clang::Type::Record:
6034 if (GetCompleteType(type)) {
6035 const clang::CXXRecordDecl *cxx_record_decl =
6036 qual_type->getAsCXXRecordDecl();
6037 if (cxx_record_decl) {
6038 uint32_t curr_idx = 0;
6039 clang::CXXRecordDecl::base_class_const_iterator base_class,
6040 base_class_end;
6041 for (base_class = cxx_record_decl->bases_begin(),
6042 base_class_end = cxx_record_decl->bases_end();
6043 base_class != base_class_end; ++base_class, ++curr_idx) {
6044 if (curr_idx == idx) {
6045 if (bit_offset_ptr) {
6046 const clang::ASTRecordLayout &record_layout =
6047 getASTContext()->getASTRecordLayout(cxx_record_decl);
6048 const clang::CXXRecordDecl *base_class_decl =
6049 llvm::cast<clang::CXXRecordDecl>(
6050 base_class->getType()
6051 ->getAs<clang::RecordType>()
6052 ->getDecl());
6053 if (base_class->isVirtual())
6054 *bit_offset_ptr =
6055 record_layout.getVBaseClassOffset(base_class_decl)
6056 .getQuantity() *
6057 8;
6058 else
6059 *bit_offset_ptr =
6060 record_layout.getBaseClassOffset(base_class_decl)
6061 .getQuantity() *
6062 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006063 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006064 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6065 }
6066 }
6067 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006068 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006069 break;
6070
6071 case clang::Type::ObjCObjectPointer:
6072 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6073
6074 case clang::Type::ObjCObject:
6075 if (idx == 0 && GetCompleteType(type)) {
6076 const clang::ObjCObjectType *objc_class_type =
6077 qual_type->getAsObjCQualifiedInterfaceType();
6078 if (objc_class_type) {
6079 clang::ObjCInterfaceDecl *class_interface_decl =
6080 objc_class_type->getInterface();
6081
6082 if (class_interface_decl) {
6083 clang::ObjCInterfaceDecl *superclass_interface_decl =
6084 class_interface_decl->getSuperClass();
6085 if (superclass_interface_decl) {
6086 if (bit_offset_ptr)
6087 *bit_offset_ptr = 0;
6088 return CompilerType(getASTContext(),
6089 getASTContext()->getObjCInterfaceType(
6090 superclass_interface_decl));
6091 }
6092 }
6093 }
6094 }
6095 break;
6096 case clang::Type::ObjCInterface:
6097 if (idx == 0 && GetCompleteType(type)) {
6098 const clang::ObjCObjectType *objc_interface_type =
6099 qual_type->getAs<clang::ObjCInterfaceType>();
6100 if (objc_interface_type) {
6101 clang::ObjCInterfaceDecl *class_interface_decl =
6102 objc_interface_type->getInterface();
6103
6104 if (class_interface_decl) {
6105 clang::ObjCInterfaceDecl *superclass_interface_decl =
6106 class_interface_decl->getSuperClass();
6107 if (superclass_interface_decl) {
6108 if (bit_offset_ptr)
6109 *bit_offset_ptr = 0;
6110 return CompilerType(getASTContext(),
6111 getASTContext()->getObjCInterfaceType(
6112 superclass_interface_decl));
6113 }
6114 }
6115 }
6116 }
6117 break;
6118
6119 case clang::Type::Typedef:
6120 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6121 ->getDecl()
6122 ->getUnderlyingType()
6123 .getAsOpaquePtr(),
6124 idx, bit_offset_ptr);
6125
6126 case clang::Type::Auto:
6127 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6128 ->getDeducedType()
6129 .getAsOpaquePtr(),
6130 idx, bit_offset_ptr);
6131
6132 case clang::Type::Elaborated:
6133 return GetDirectBaseClassAtIndex(
6134 llvm::cast<clang::ElaboratedType>(qual_type)
6135 ->getNamedType()
6136 .getAsOpaquePtr(),
6137 idx, bit_offset_ptr);
6138
6139 case clang::Type::Paren:
6140 return GetDirectBaseClassAtIndex(
6141 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6142 idx, bit_offset_ptr);
6143
6144 default:
6145 break;
6146 }
6147 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006148}
6149
Kate Stoneb9c1b512016-09-06 20:57:50 +00006150CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6151 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6152 clang::QualType qual_type(GetCanonicalQualType(type));
6153 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6154 switch (type_class) {
6155 case clang::Type::Record:
6156 if (GetCompleteType(type)) {
6157 const clang::CXXRecordDecl *cxx_record_decl =
6158 qual_type->getAsCXXRecordDecl();
6159 if (cxx_record_decl) {
6160 uint32_t curr_idx = 0;
6161 clang::CXXRecordDecl::base_class_const_iterator base_class,
6162 base_class_end;
6163 for (base_class = cxx_record_decl->vbases_begin(),
6164 base_class_end = cxx_record_decl->vbases_end();
6165 base_class != base_class_end; ++base_class, ++curr_idx) {
6166 if (curr_idx == idx) {
6167 if (bit_offset_ptr) {
6168 const clang::ASTRecordLayout &record_layout =
6169 getASTContext()->getASTRecordLayout(cxx_record_decl);
6170 const clang::CXXRecordDecl *base_class_decl =
6171 llvm::cast<clang::CXXRecordDecl>(
6172 base_class->getType()
6173 ->getAs<clang::RecordType>()
6174 ->getDecl());
6175 *bit_offset_ptr =
6176 record_layout.getVBaseClassOffset(base_class_decl)
6177 .getQuantity() *
6178 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006179 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006180 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6181 }
6182 }
6183 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006184 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006185 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006186
Kate Stoneb9c1b512016-09-06 20:57:50 +00006187 case clang::Type::Typedef:
6188 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6189 ->getDecl()
6190 ->getUnderlyingType()
6191 .getAsOpaquePtr(),
6192 idx, bit_offset_ptr);
6193
6194 case clang::Type::Auto:
6195 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6196 ->getDeducedType()
6197 .getAsOpaquePtr(),
6198 idx, bit_offset_ptr);
6199
6200 case clang::Type::Elaborated:
6201 return GetVirtualBaseClassAtIndex(
6202 llvm::cast<clang::ElaboratedType>(qual_type)
6203 ->getNamedType()
6204 .getAsOpaquePtr(),
6205 idx, bit_offset_ptr);
6206
6207 case clang::Type::Paren:
6208 return GetVirtualBaseClassAtIndex(
6209 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6210 idx, bit_offset_ptr);
6211
6212 default:
6213 break;
6214 }
6215 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006216}
6217
Greg Claytond8d4a572015-08-11 21:38:15 +00006218// If a pointer to a pointee type (the clang_type arg) says that it has no
6219// children, then we either need to trust it, or override it and return a
6220// different result. For example, an "int *" has one child that is an integer,
6221// but a function pointer doesn't have any children. Likewise if a Record type
6222// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006223uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6224 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006225 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006226
6227 clang::QualType qual_type(type.getCanonicalType());
6228 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6229 switch (type_class) {
6230 case clang::Type::Builtin:
6231 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6232 case clang::BuiltinType::UnknownAny:
6233 case clang::BuiltinType::Void:
6234 case clang::BuiltinType::NullPtr:
6235 case clang::BuiltinType::OCLEvent:
6236 case clang::BuiltinType::OCLImage1dRO:
6237 case clang::BuiltinType::OCLImage1dWO:
6238 case clang::BuiltinType::OCLImage1dRW:
6239 case clang::BuiltinType::OCLImage1dArrayRO:
6240 case clang::BuiltinType::OCLImage1dArrayWO:
6241 case clang::BuiltinType::OCLImage1dArrayRW:
6242 case clang::BuiltinType::OCLImage1dBufferRO:
6243 case clang::BuiltinType::OCLImage1dBufferWO:
6244 case clang::BuiltinType::OCLImage1dBufferRW:
6245 case clang::BuiltinType::OCLImage2dRO:
6246 case clang::BuiltinType::OCLImage2dWO:
6247 case clang::BuiltinType::OCLImage2dRW:
6248 case clang::BuiltinType::OCLImage2dArrayRO:
6249 case clang::BuiltinType::OCLImage2dArrayWO:
6250 case clang::BuiltinType::OCLImage2dArrayRW:
6251 case clang::BuiltinType::OCLImage3dRO:
6252 case clang::BuiltinType::OCLImage3dWO:
6253 case clang::BuiltinType::OCLImage3dRW:
6254 case clang::BuiltinType::OCLSampler:
6255 return 0;
6256 case clang::BuiltinType::Bool:
6257 case clang::BuiltinType::Char_U:
6258 case clang::BuiltinType::UChar:
6259 case clang::BuiltinType::WChar_U:
6260 case clang::BuiltinType::Char16:
6261 case clang::BuiltinType::Char32:
6262 case clang::BuiltinType::UShort:
6263 case clang::BuiltinType::UInt:
6264 case clang::BuiltinType::ULong:
6265 case clang::BuiltinType::ULongLong:
6266 case clang::BuiltinType::UInt128:
6267 case clang::BuiltinType::Char_S:
6268 case clang::BuiltinType::SChar:
6269 case clang::BuiltinType::WChar_S:
6270 case clang::BuiltinType::Short:
6271 case clang::BuiltinType::Int:
6272 case clang::BuiltinType::Long:
6273 case clang::BuiltinType::LongLong:
6274 case clang::BuiltinType::Int128:
6275 case clang::BuiltinType::Float:
6276 case clang::BuiltinType::Double:
6277 case clang::BuiltinType::LongDouble:
6278 case clang::BuiltinType::Dependent:
6279 case clang::BuiltinType::Overload:
6280 case clang::BuiltinType::ObjCId:
6281 case clang::BuiltinType::ObjCClass:
6282 case clang::BuiltinType::ObjCSel:
6283 case clang::BuiltinType::BoundMember:
6284 case clang::BuiltinType::Half:
6285 case clang::BuiltinType::ARCUnbridgedCast:
6286 case clang::BuiltinType::PseudoObject:
6287 case clang::BuiltinType::BuiltinFn:
6288 case clang::BuiltinType::OMPArraySection:
6289 return 1;
6290 default:
6291 return 0;
6292 }
6293 break;
6294
6295 case clang::Type::Complex:
6296 return 1;
6297 case clang::Type::Pointer:
6298 return 1;
6299 case clang::Type::BlockPointer:
6300 return 0; // If block pointers don't have debug info, then no children for
6301 // them
6302 case clang::Type::LValueReference:
6303 return 1;
6304 case clang::Type::RValueReference:
6305 return 1;
6306 case clang::Type::MemberPointer:
6307 return 0;
6308 case clang::Type::ConstantArray:
6309 return 0;
6310 case clang::Type::IncompleteArray:
6311 return 0;
6312 case clang::Type::VariableArray:
6313 return 0;
6314 case clang::Type::DependentSizedArray:
6315 return 0;
6316 case clang::Type::DependentSizedExtVector:
6317 return 0;
6318 case clang::Type::Vector:
6319 return 0;
6320 case clang::Type::ExtVector:
6321 return 0;
6322 case clang::Type::FunctionProto:
6323 return 0; // When we function pointers, they have no children...
6324 case clang::Type::FunctionNoProto:
6325 return 0; // When we function pointers, they have no children...
6326 case clang::Type::UnresolvedUsing:
6327 return 0;
6328 case clang::Type::Paren:
6329 return GetNumPointeeChildren(
6330 llvm::cast<clang::ParenType>(qual_type)->desugar());
6331 case clang::Type::Typedef:
6332 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6333 ->getDecl()
6334 ->getUnderlyingType());
6335 case clang::Type::Auto:
6336 return GetNumPointeeChildren(
6337 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6338 case clang::Type::Elaborated:
6339 return GetNumPointeeChildren(
6340 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6341 case clang::Type::TypeOfExpr:
6342 return 0;
6343 case clang::Type::TypeOf:
6344 return 0;
6345 case clang::Type::Decltype:
6346 return 0;
6347 case clang::Type::Record:
6348 return 0;
6349 case clang::Type::Enum:
6350 return 1;
6351 case clang::Type::TemplateTypeParm:
6352 return 1;
6353 case clang::Type::SubstTemplateTypeParm:
6354 return 1;
6355 case clang::Type::TemplateSpecialization:
6356 return 1;
6357 case clang::Type::InjectedClassName:
6358 return 0;
6359 case clang::Type::DependentName:
6360 return 1;
6361 case clang::Type::DependentTemplateSpecialization:
6362 return 1;
6363 case clang::Type::ObjCObject:
6364 return 0;
6365 case clang::Type::ObjCInterface:
6366 return 0;
6367 case clang::Type::ObjCObjectPointer:
6368 return 1;
6369 default:
6370 break;
6371 }
6372 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006373}
6374
Kate Stoneb9c1b512016-09-06 20:57:50 +00006375CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6376 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6377 bool transparent_pointers, bool omit_empty_base_classes,
6378 bool ignore_array_bounds, std::string &child_name,
6379 uint32_t &child_byte_size, int32_t &child_byte_offset,
6380 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6381 bool &child_is_base_class, bool &child_is_deref_of_parent,
6382 ValueObject *valobj, uint64_t &language_flags) {
6383 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006384 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006385
Kate Stoneb9c1b512016-09-06 20:57:50 +00006386 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6387 const clang::Type::TypeClass parent_type_class =
6388 parent_qual_type->getTypeClass();
6389 child_bitfield_bit_size = 0;
6390 child_bitfield_bit_offset = 0;
6391 child_is_base_class = false;
6392 language_flags = 0;
6393
6394 const bool idx_is_valid = idx < GetNumChildren(type, omit_empty_base_classes);
6395 uint32_t bit_offset;
6396 switch (parent_type_class) {
6397 case clang::Type::Builtin:
6398 if (idx_is_valid) {
6399 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6400 case clang::BuiltinType::ObjCId:
6401 case clang::BuiltinType::ObjCClass:
6402 child_name = "isa";
6403 child_byte_size =
6404 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6405 CHAR_BIT;
6406 return CompilerType(getASTContext(),
6407 getASTContext()->ObjCBuiltinClassTy);
6408
6409 default:
6410 break;
6411 }
6412 }
6413 break;
6414
6415 case clang::Type::Record:
6416 if (idx_is_valid && GetCompleteType(type)) {
6417 const clang::RecordType *record_type =
6418 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6419 const clang::RecordDecl *record_decl = record_type->getDecl();
6420 assert(record_decl);
6421 const clang::ASTRecordLayout &record_layout =
6422 getASTContext()->getASTRecordLayout(record_decl);
6423 uint32_t child_idx = 0;
6424
6425 const clang::CXXRecordDecl *cxx_record_decl =
6426 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6427 if (cxx_record_decl) {
6428 // We might have base classes to print out first
6429 clang::CXXRecordDecl::base_class_const_iterator base_class,
6430 base_class_end;
6431 for (base_class = cxx_record_decl->bases_begin(),
6432 base_class_end = cxx_record_decl->bases_end();
6433 base_class != base_class_end; ++base_class) {
6434 const clang::CXXRecordDecl *base_class_decl = nullptr;
6435
6436 // Skip empty base classes
6437 if (omit_empty_base_classes) {
6438 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6439 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6440 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
6441 continue;
6442 }
6443
6444 if (idx == child_idx) {
6445 if (base_class_decl == nullptr)
6446 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6447 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6448
6449 if (base_class->isVirtual()) {
6450 bool handled = false;
6451 if (valobj) {
Zachary Turner97206d52017-05-12 04:51:55 +00006452 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006453 AddressType addr_type = eAddressTypeInvalid;
6454 lldb::addr_t vtable_ptr_addr =
6455 valobj->GetCPPVTableAddress(addr_type);
6456
6457 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS &&
6458 addr_type == eAddressTypeLoad) {
6459
6460 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
6461 Process *process = exe_ctx.GetProcessPtr();
6462 if (process) {
6463 clang::VTableContextBase *vtable_ctx =
6464 getASTContext()->getVTableContext();
6465 if (vtable_ctx) {
6466 if (vtable_ctx->isMicrosoft()) {
6467 clang::MicrosoftVTableContext *msoft_vtable_ctx =
6468 static_cast<clang::MicrosoftVTableContext *>(
6469 vtable_ctx);
6470
6471 if (vtable_ptr_addr) {
6472 const lldb::addr_t vbtable_ptr_addr =
6473 vtable_ptr_addr +
6474 record_layout.getVBPtrOffset().getQuantity();
6475
6476 const lldb::addr_t vbtable_ptr =
6477 process->ReadPointerFromMemory(vbtable_ptr_addr,
6478 err);
6479 if (vbtable_ptr != LLDB_INVALID_ADDRESS) {
6480 // Get the index into the virtual base table. The
6481 // index is the index in uint32_t from vbtable_ptr
6482 const unsigned vbtable_index =
6483 msoft_vtable_ctx->getVBTableIndex(
6484 cxx_record_decl, base_class_decl);
6485 const lldb::addr_t base_offset_addr =
6486 vbtable_ptr + vbtable_index * 4;
6487 const uint32_t base_offset =
6488 process->ReadUnsignedIntegerFromMemory(
6489 base_offset_addr, 4, UINT32_MAX, err);
6490 if (base_offset != UINT32_MAX) {
6491 handled = true;
6492 bit_offset = base_offset * 8;
6493 }
6494 }
6495 }
6496 } else {
6497 clang::ItaniumVTableContext *itanium_vtable_ctx =
6498 static_cast<clang::ItaniumVTableContext *>(
6499 vtable_ctx);
6500 if (vtable_ptr_addr) {
6501 const lldb::addr_t vtable_ptr =
6502 process->ReadPointerFromMemory(vtable_ptr_addr,
6503 err);
6504 if (vtable_ptr != LLDB_INVALID_ADDRESS) {
6505 clang::CharUnits base_offset_offset =
6506 itanium_vtable_ctx->getVirtualBaseOffsetOffset(
6507 cxx_record_decl, base_class_decl);
6508 const lldb::addr_t base_offset_addr =
6509 vtable_ptr + base_offset_offset.getQuantity();
6510 const uint32_t base_offset_size =
6511 process->GetAddressByteSize();
6512 const uint64_t base_offset =
6513 process->ReadUnsignedIntegerFromMemory(
6514 base_offset_addr, base_offset_size,
6515 UINT32_MAX, err);
6516 if (base_offset < UINT32_MAX) {
6517 handled = true;
6518 bit_offset = base_offset * 8;
6519 }
6520 }
6521 }
6522 }
6523 }
6524 }
6525 }
6526 }
6527 if (!handled)
6528 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6529 .getQuantity() *
6530 8;
6531 } else
6532 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6533 .getQuantity() *
6534 8;
6535
6536 // Base classes should be a multiple of 8 bits in size
6537 child_byte_offset = bit_offset / 8;
6538 CompilerType base_class_clang_type(getASTContext(),
6539 base_class->getType());
6540 child_name = base_class_clang_type.GetTypeName().AsCString("");
6541 uint64_t base_class_clang_type_bit_size =
6542 base_class_clang_type.GetBitSize(
6543 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6544
6545 // Base classes bit sizes should be a multiple of 8 bits in size
6546 assert(base_class_clang_type_bit_size % 8 == 0);
6547 child_byte_size = base_class_clang_type_bit_size / 8;
6548 child_is_base_class = true;
6549 return base_class_clang_type;
6550 }
6551 // We don't increment the child index in the for loop since we might
6552 // be skipping empty base classes
6553 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006554 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006555 }
6556 // Make sure index is in range...
6557 uint32_t field_idx = 0;
6558 clang::RecordDecl::field_iterator field, field_end;
6559 for (field = record_decl->field_begin(),
6560 field_end = record_decl->field_end();
6561 field != field_end; ++field, ++field_idx, ++child_idx) {
6562 if (idx == child_idx) {
6563 // Print the member type if requested
6564 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006565 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006566
6567 // Figure out the type byte size (field_type_info.first) and
6568 // alignment (field_type_info.second) from the AST context.
6569 CompilerType field_clang_type(getASTContext(), field->getType());
6570 assert(field_idx < record_layout.getFieldCount());
6571 child_byte_size = field_clang_type.GetByteSize(
6572 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6573 const uint32_t child_bit_size = child_byte_size * 8;
6574
6575 // Figure out the field offset within the current struct/union/class
6576 // type
6577 bit_offset = record_layout.getFieldOffset(field_idx);
6578 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6579 child_bitfield_bit_size)) {
6580 child_bitfield_bit_offset = bit_offset % child_bit_size;
6581 const uint32_t child_bit_offset =
6582 bit_offset - child_bitfield_bit_offset;
6583 child_byte_offset = child_bit_offset / 8;
6584 } else {
6585 child_byte_offset = bit_offset / 8;
6586 }
6587
6588 return field_clang_type;
6589 }
6590 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006591 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006592 break;
6593
6594 case clang::Type::ObjCObject:
6595 case clang::Type::ObjCInterface:
6596 if (idx_is_valid && GetCompleteType(type)) {
6597 const clang::ObjCObjectType *objc_class_type =
6598 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6599 assert(objc_class_type);
6600 if (objc_class_type) {
6601 uint32_t child_idx = 0;
6602 clang::ObjCInterfaceDecl *class_interface_decl =
6603 objc_class_type->getInterface();
6604
6605 if (class_interface_decl) {
6606
6607 const clang::ASTRecordLayout &interface_layout =
6608 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6609 clang::ObjCInterfaceDecl *superclass_interface_decl =
6610 class_interface_decl->getSuperClass();
6611 if (superclass_interface_decl) {
6612 if (omit_empty_base_classes) {
6613 CompilerType base_class_clang_type(
6614 getASTContext(), getASTContext()->getObjCInterfaceType(
6615 superclass_interface_decl));
6616 if (base_class_clang_type.GetNumChildren(
6617 omit_empty_base_classes) > 0) {
6618 if (idx == 0) {
6619 clang::QualType ivar_qual_type(
6620 getASTContext()->getObjCInterfaceType(
6621 superclass_interface_decl));
6622
6623 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006624 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006625
6626 clang::TypeInfo ivar_type_info =
6627 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6628
6629 child_byte_size = ivar_type_info.Width / 8;
6630 child_byte_offset = 0;
6631 child_is_base_class = true;
6632
6633 return CompilerType(getASTContext(), ivar_qual_type);
6634 }
6635
6636 ++child_idx;
6637 }
6638 } else
6639 ++child_idx;
6640 }
6641
6642 const uint32_t superclass_idx = child_idx;
6643
6644 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6645 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6646 ivar_end = class_interface_decl->ivar_end();
6647
6648 for (ivar_pos = class_interface_decl->ivar_begin();
6649 ivar_pos != ivar_end; ++ivar_pos) {
6650 if (child_idx == idx) {
6651 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6652
6653 clang::QualType ivar_qual_type(ivar_decl->getType());
6654
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006655 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006656
6657 clang::TypeInfo ivar_type_info =
6658 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6659
6660 child_byte_size = ivar_type_info.Width / 8;
6661
6662 // Figure out the field offset within the current
6663 // struct/union/class type
6664 // For ObjC objects, we can't trust the bit offset we get from
6665 // the Clang AST, since
6666 // that doesn't account for the space taken up by unbacked
6667 // properties, or from
6668 // the changing size of base classes that are newer than this
6669 // class.
6670 // So if we have a process around that we can ask about this
6671 // object, do so.
6672 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6673 Process *process = nullptr;
6674 if (exe_ctx)
6675 process = exe_ctx->GetProcessPtr();
6676 if (process) {
6677 ObjCLanguageRuntime *objc_runtime =
6678 process->GetObjCLanguageRuntime();
6679 if (objc_runtime != nullptr) {
6680 CompilerType parent_ast_type(getASTContext(),
6681 parent_qual_type);
6682 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6683 parent_ast_type, ivar_decl->getNameAsString().c_str());
6684 }
6685 }
6686
6687 // Setting this to UINT32_MAX to make sure we don't compute it
6688 // twice...
6689 bit_offset = UINT32_MAX;
6690
6691 if (child_byte_offset ==
6692 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6693 bit_offset = interface_layout.getFieldOffset(child_idx -
6694 superclass_idx);
6695 child_byte_offset = bit_offset / 8;
6696 }
6697
6698 // Note, the ObjC Ivar Byte offset is just that, it doesn't
6699 // account for the bit offset
6700 // of a bitfield within its containing object. So regardless of
6701 // where we get the byte
6702 // offset from, we still need to get the bit offset for
6703 // bitfields from the layout.
6704
6705 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6706 child_bitfield_bit_size)) {
6707 if (bit_offset == UINT32_MAX)
6708 bit_offset = interface_layout.getFieldOffset(
6709 child_idx - superclass_idx);
6710
6711 child_bitfield_bit_offset = bit_offset % 8;
6712 }
6713 return CompilerType(getASTContext(), ivar_qual_type);
6714 }
6715 ++child_idx;
6716 }
6717 }
6718 }
6719 }
6720 }
6721 break;
6722
6723 case clang::Type::ObjCObjectPointer:
6724 if (idx_is_valid) {
6725 CompilerType pointee_clang_type(GetPointeeType(type));
6726
6727 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6728 child_is_deref_of_parent = false;
6729 bool tmp_child_is_deref_of_parent = false;
6730 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6731 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6732 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6733 child_bitfield_bit_size, child_bitfield_bit_offset,
6734 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6735 language_flags);
6736 } else {
6737 child_is_deref_of_parent = true;
6738 const char *parent_name =
6739 valobj ? valobj->GetName().GetCString() : NULL;
6740 if (parent_name) {
6741 child_name.assign(1, '*');
6742 child_name += parent_name;
6743 }
6744
6745 // We have a pointer to an simple type
6746 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
6747 child_byte_size = pointee_clang_type.GetByteSize(
6748 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6749 child_byte_offset = 0;
6750 return pointee_clang_type;
6751 }
6752 }
6753 }
6754 break;
6755
6756 case clang::Type::Vector:
6757 case clang::Type::ExtVector:
6758 if (idx_is_valid) {
6759 const clang::VectorType *array =
6760 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6761 if (array) {
6762 CompilerType element_type(getASTContext(), array->getElementType());
6763 if (element_type.GetCompleteType()) {
6764 char element_name[64];
6765 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6766 static_cast<uint64_t>(idx));
6767 child_name.assign(element_name);
6768 child_byte_size = element_type.GetByteSize(
6769 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6770 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6771 return element_type;
6772 }
6773 }
6774 }
6775 break;
6776
6777 case clang::Type::ConstantArray:
6778 case clang::Type::IncompleteArray:
6779 if (ignore_array_bounds || idx_is_valid) {
6780 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6781 if (array) {
6782 CompilerType element_type(getASTContext(), array->getElementType());
6783 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006784 child_name = llvm::formatv("[{0}]", idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006785 child_byte_size = element_type.GetByteSize(
6786 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6787 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6788 return element_type;
6789 }
6790 }
6791 }
6792 break;
6793
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006794 case clang::Type::Pointer: {
6795 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006796
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006797 // Don't dereference "void *" pointers
6798 if (pointee_clang_type.IsVoidType())
6799 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006800
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006801 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6802 child_is_deref_of_parent = false;
6803 bool tmp_child_is_deref_of_parent = false;
6804 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6805 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6806 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6807 child_bitfield_bit_size, child_bitfield_bit_offset,
6808 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6809 language_flags);
6810 } else {
6811 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006812
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006813 const char *parent_name =
6814 valobj ? valobj->GetName().GetCString() : NULL;
6815 if (parent_name) {
6816 child_name.assign(1, '*');
6817 child_name += parent_name;
6818 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006819
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006820 // We have a pointer to an simple type
6821 if (idx == 0) {
6822 child_byte_size = pointee_clang_type.GetByteSize(
6823 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6824 child_byte_offset = 0;
6825 return pointee_clang_type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006826 }
6827 }
6828 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006829 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006830
6831 case clang::Type::LValueReference:
6832 case clang::Type::RValueReference:
6833 if (idx_is_valid) {
6834 const clang::ReferenceType *reference_type =
6835 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6836 CompilerType pointee_clang_type(getASTContext(),
6837 reference_type->getPointeeType());
6838 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6839 child_is_deref_of_parent = false;
6840 bool tmp_child_is_deref_of_parent = false;
6841 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6842 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6843 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6844 child_bitfield_bit_size, child_bitfield_bit_offset,
6845 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6846 language_flags);
6847 } else {
6848 const char *parent_name =
6849 valobj ? valobj->GetName().GetCString() : NULL;
6850 if (parent_name) {
6851 child_name.assign(1, '&');
6852 child_name += parent_name;
6853 }
6854
6855 // We have a pointer to an simple type
6856 if (idx == 0) {
6857 child_byte_size = pointee_clang_type.GetByteSize(
6858 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6859 child_byte_offset = 0;
6860 return pointee_clang_type;
6861 }
6862 }
6863 }
6864 break;
6865
6866 case clang::Type::Typedef: {
6867 CompilerType typedefed_clang_type(
6868 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
6869 ->getDecl()
6870 ->getUnderlyingType());
6871 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
6872 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6873 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6874 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6875 child_is_deref_of_parent, valobj, language_flags);
6876 } break;
6877
6878 case clang::Type::Auto: {
6879 CompilerType elaborated_clang_type(
6880 getASTContext(),
6881 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
6882 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6883 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6884 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6885 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6886 child_is_deref_of_parent, valobj, language_flags);
6887 }
6888
6889 case clang::Type::Elaborated: {
6890 CompilerType elaborated_clang_type(
6891 getASTContext(),
6892 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
6893 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6894 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6895 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6896 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6897 child_is_deref_of_parent, valobj, language_flags);
6898 }
6899
6900 case clang::Type::Paren: {
6901 CompilerType paren_clang_type(
6902 getASTContext(),
6903 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
6904 return paren_clang_type.GetChildCompilerTypeAtIndex(
6905 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6906 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6907 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6908 child_is_deref_of_parent, valobj, language_flags);
6909 }
6910
6911 default:
6912 break;
6913 }
6914 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006915}
6916
Kate Stoneb9c1b512016-09-06 20:57:50 +00006917static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
6918 const clang::CXXBaseSpecifier *base_spec,
6919 bool omit_empty_base_classes) {
6920 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006921
Kate Stoneb9c1b512016-09-06 20:57:50 +00006922 const clang::CXXRecordDecl *cxx_record_decl =
6923 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6924
6925 // const char *super_name = record_decl->getNameAsCString();
6926 // const char *base_name =
6927 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6928 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6929 //
6930 if (cxx_record_decl) {
6931 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6932 for (base_class = cxx_record_decl->bases_begin(),
6933 base_class_end = cxx_record_decl->bases_end();
6934 base_class != base_class_end; ++base_class) {
6935 if (omit_empty_base_classes) {
6936 if (BaseSpecifierIsEmpty(base_class))
6937 continue;
6938 }
6939
6940 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
6941 // super_name, base_name,
6942 // child_idx,
6943 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6944 //
6945 //
6946 if (base_class == base_spec)
6947 return child_idx;
6948 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006949 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006950 }
6951
6952 return UINT32_MAX;
6953}
6954
6955static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
6956 clang::NamedDecl *canonical_decl,
6957 bool omit_empty_base_classes) {
6958 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
6959 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6960 omit_empty_base_classes);
6961
6962 clang::RecordDecl::field_iterator field, field_end;
6963 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6964 field != field_end; ++field, ++child_idx) {
6965 if (field->getCanonicalDecl() == canonical_decl)
6966 return child_idx;
6967 }
6968
6969 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00006970}
6971
6972// Look for a child member (doesn't include base classes, but it does include
6973// their members) in the type hierarchy. Returns an index path into "clang_type"
6974// on how to reach the appropriate member.
6975//
6976// class A
6977// {
6978// public:
6979// int m_a;
6980// int m_b;
6981// };
6982//
6983// class B
6984// {
6985// };
6986//
6987// class C :
6988// public B,
6989// public A
6990// {
6991// };
6992//
6993// If we have a clang type that describes "class C", and we wanted to looked
6994// "m_b" in it:
6995//
Kate Stoneb9c1b512016-09-06 20:57:50 +00006996// With omit_empty_base_classes == false we would get an integer array back
6997// with:
Greg Claytond8d4a572015-08-11 21:38:15 +00006998// { 1, 1 }
6999// The first index 1 is the child index for "class A" within class C
7000// The second index 1 is the child index for "m_b" within class A
7001//
7002// With omit_empty_base_classes == true we would get an integer array back with:
7003// { 0, 1 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007004// The first index 0 is the child index for "class A" within class C (since
7005// class B doesn't have any members it doesn't count)
Greg Claytond8d4a572015-08-11 21:38:15 +00007006// The second index 1 is the child index for "m_b" within class A
7007
Kate Stoneb9c1b512016-09-06 20:57:50 +00007008size_t ClangASTContext::GetIndexOfChildMemberWithName(
7009 lldb::opaque_compiler_type_t type, const char *name,
7010 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
7011 if (type && name && name[0]) {
7012 clang::QualType qual_type(GetCanonicalQualType(type));
7013 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7014 switch (type_class) {
7015 case clang::Type::Record:
7016 if (GetCompleteType(type)) {
7017 const clang::RecordType *record_type =
7018 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7019 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00007020
Kate Stoneb9c1b512016-09-06 20:57:50 +00007021 assert(record_decl);
7022 uint32_t child_idx = 0;
7023
7024 const clang::CXXRecordDecl *cxx_record_decl =
7025 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7026
7027 // Try and find a field that matches NAME
7028 clang::RecordDecl::field_iterator field, field_end;
7029 llvm::StringRef name_sref(name);
7030 for (field = record_decl->field_begin(),
7031 field_end = record_decl->field_end();
7032 field != field_end; ++field, ++child_idx) {
7033 llvm::StringRef field_name = field->getName();
7034 if (field_name.empty()) {
7035 CompilerType field_type(getASTContext(), field->getType());
7036 child_indexes.push_back(child_idx);
7037 if (field_type.GetIndexOfChildMemberWithName(
7038 name, omit_empty_base_classes, child_indexes))
7039 return child_indexes.size();
7040 child_indexes.pop_back();
7041
7042 } else if (field_name.equals(name_sref)) {
7043 // We have to add on the number of base classes to this index!
7044 child_indexes.push_back(
7045 child_idx + ClangASTContext::GetNumBaseClasses(
7046 cxx_record_decl, omit_empty_base_classes));
7047 return child_indexes.size();
7048 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007049 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007050
Kate Stoneb9c1b512016-09-06 20:57:50 +00007051 if (cxx_record_decl) {
7052 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7053
7054 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7055
7056 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7057 // Didn't find things easily, lets let clang do its thang...
7058 clang::IdentifierInfo &ident_ref =
7059 getASTContext()->Idents.get(name_sref);
7060 clang::DeclarationName decl_name(&ident_ref);
7061
7062 clang::CXXBasePaths paths;
7063 if (cxx_record_decl->lookupInBases(
7064 [decl_name](const clang::CXXBaseSpecifier *specifier,
7065 clang::CXXBasePath &path) {
7066 return clang::CXXRecordDecl::FindOrdinaryMember(
7067 specifier, path, decl_name);
7068 },
7069 paths)) {
7070 clang::CXXBasePaths::const_paths_iterator path,
7071 path_end = paths.end();
7072 for (path = paths.begin(); path != path_end; ++path) {
7073 const size_t num_path_elements = path->size();
7074 for (size_t e = 0; e < num_path_elements; ++e) {
7075 clang::CXXBasePathElement elem = (*path)[e];
7076
7077 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7078 omit_empty_base_classes);
7079 if (child_idx == UINT32_MAX) {
7080 child_indexes.clear();
7081 return 0;
7082 } else {
7083 child_indexes.push_back(child_idx);
7084 parent_record_decl = llvm::cast<clang::RecordDecl>(
7085 elem.Base->getType()
7086 ->getAs<clang::RecordType>()
7087 ->getDecl());
7088 }
7089 }
7090 for (clang::NamedDecl *path_decl : path->Decls) {
7091 child_idx = GetIndexForRecordChild(
7092 parent_record_decl, path_decl, omit_empty_base_classes);
7093 if (child_idx == UINT32_MAX) {
7094 child_indexes.clear();
7095 return 0;
7096 } else {
7097 child_indexes.push_back(child_idx);
7098 }
7099 }
7100 }
7101 return child_indexes.size();
7102 }
7103 }
7104 }
7105 break;
7106
7107 case clang::Type::ObjCObject:
7108 case clang::Type::ObjCInterface:
7109 if (GetCompleteType(type)) {
7110 llvm::StringRef name_sref(name);
7111 const clang::ObjCObjectType *objc_class_type =
7112 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7113 assert(objc_class_type);
7114 if (objc_class_type) {
7115 uint32_t child_idx = 0;
7116 clang::ObjCInterfaceDecl *class_interface_decl =
7117 objc_class_type->getInterface();
7118
7119 if (class_interface_decl) {
7120 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7121 ivar_end = class_interface_decl->ivar_end();
7122 clang::ObjCInterfaceDecl *superclass_interface_decl =
7123 class_interface_decl->getSuperClass();
7124
7125 for (ivar_pos = class_interface_decl->ivar_begin();
7126 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7127 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7128
7129 if (ivar_decl->getName().equals(name_sref)) {
7130 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7131 (omit_empty_base_classes &&
7132 ObjCDeclHasIVars(superclass_interface_decl, true)))
7133 ++child_idx;
7134
7135 child_indexes.push_back(child_idx);
7136 return child_indexes.size();
7137 }
7138 }
7139
7140 if (superclass_interface_decl) {
7141 // The super class index is always zero for ObjC classes,
7142 // so we push it onto the child indexes in case we find
7143 // an ivar in our superclass...
7144 child_indexes.push_back(0);
7145
7146 CompilerType superclass_clang_type(
7147 getASTContext(), getASTContext()->getObjCInterfaceType(
7148 superclass_interface_decl));
7149 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7150 name, omit_empty_base_classes, child_indexes)) {
7151 // We did find an ivar in a superclass so just
7152 // return the results!
7153 return child_indexes.size();
7154 }
7155
7156 // We didn't find an ivar matching "name" in our
7157 // superclass, pop the superclass zero index that
7158 // we pushed on above.
7159 child_indexes.pop_back();
7160 }
7161 }
7162 }
7163 }
7164 break;
7165
7166 case clang::Type::ObjCObjectPointer: {
7167 CompilerType objc_object_clang_type(
7168 getASTContext(),
7169 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7170 ->getPointeeType());
7171 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7172 name, omit_empty_base_classes, child_indexes);
7173 } break;
7174
7175 case clang::Type::ConstantArray: {
7176 // const clang::ConstantArrayType *array =
7177 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7178 // const uint64_t element_count =
7179 // array->getSize().getLimitedValue();
7180 //
7181 // if (idx < element_count)
7182 // {
7183 // std::pair<uint64_t, unsigned> field_type_info =
7184 // ast->getTypeInfo(array->getElementType());
7185 //
7186 // char element_name[32];
7187 // ::snprintf (element_name, sizeof (element_name),
7188 // "%s[%u]", parent_name ? parent_name : "", idx);
7189 //
7190 // child_name.assign(element_name);
7191 // assert(field_type_info.first % 8 == 0);
7192 // child_byte_size = field_type_info.first / 8;
7193 // child_byte_offset = idx * child_byte_size;
7194 // return array->getElementType().getAsOpaquePtr();
7195 // }
7196 } break;
7197
7198 // case clang::Type::MemberPointerType:
7199 // {
7200 // MemberPointerType *mem_ptr_type =
7201 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7202 // clang::QualType pointee_type =
7203 // mem_ptr_type->getPointeeType();
7204 //
7205 // if (ClangASTContext::IsAggregateType
7206 // (pointee_type.getAsOpaquePtr()))
7207 // {
7208 // return GetIndexOfChildWithName (ast,
7209 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7210 // name);
7211 // }
7212 // }
7213 // break;
7214 //
7215 case clang::Type::LValueReference:
7216 case clang::Type::RValueReference: {
7217 const clang::ReferenceType *reference_type =
7218 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7219 clang::QualType pointee_type(reference_type->getPointeeType());
7220 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7221
7222 if (pointee_clang_type.IsAggregateType()) {
7223 return pointee_clang_type.GetIndexOfChildMemberWithName(
7224 name, omit_empty_base_classes, child_indexes);
7225 }
7226 } break;
7227
7228 case clang::Type::Pointer: {
7229 CompilerType pointee_clang_type(GetPointeeType(type));
7230
7231 if (pointee_clang_type.IsAggregateType()) {
7232 return pointee_clang_type.GetIndexOfChildMemberWithName(
7233 name, omit_empty_base_classes, child_indexes);
7234 }
7235 } break;
7236
7237 case clang::Type::Typedef:
7238 return CompilerType(getASTContext(),
7239 llvm::cast<clang::TypedefType>(qual_type)
7240 ->getDecl()
7241 ->getUnderlyingType())
7242 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7243 child_indexes);
7244
7245 case clang::Type::Auto:
7246 return CompilerType(
7247 getASTContext(),
7248 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7249 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7250 child_indexes);
7251
7252 case clang::Type::Elaborated:
7253 return CompilerType(
7254 getASTContext(),
7255 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7256 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7257 child_indexes);
7258
7259 case clang::Type::Paren:
7260 return CompilerType(getASTContext(),
7261 llvm::cast<clang::ParenType>(qual_type)->desugar())
7262 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7263 child_indexes);
7264
7265 default:
7266 break;
7267 }
7268 }
7269 return 0;
7270}
Greg Claytond8d4a572015-08-11 21:38:15 +00007271
7272// Get the index of the child of "clang_type" whose name matches. This function
7273// doesn't descend into the children, but only looks one level deep and name
7274// matches can include base class names.
7275
7276uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007277ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7278 const char *name,
7279 bool omit_empty_base_classes) {
7280 if (type && name && name[0]) {
7281 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007282
Kate Stoneb9c1b512016-09-06 20:57:50 +00007283 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7284
7285 switch (type_class) {
7286 case clang::Type::Record:
7287 if (GetCompleteType(type)) {
7288 const clang::RecordType *record_type =
7289 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7290 const clang::RecordDecl *record_decl = record_type->getDecl();
7291
7292 assert(record_decl);
7293 uint32_t child_idx = 0;
7294
7295 const clang::CXXRecordDecl *cxx_record_decl =
7296 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7297
7298 if (cxx_record_decl) {
7299 clang::CXXRecordDecl::base_class_const_iterator base_class,
7300 base_class_end;
7301 for (base_class = cxx_record_decl->bases_begin(),
7302 base_class_end = cxx_record_decl->bases_end();
7303 base_class != base_class_end; ++base_class) {
7304 // Skip empty base classes
7305 clang::CXXRecordDecl *base_class_decl =
7306 llvm::cast<clang::CXXRecordDecl>(
7307 base_class->getType()
7308 ->getAs<clang::RecordType>()
7309 ->getDecl());
7310 if (omit_empty_base_classes &&
7311 ClangASTContext::RecordHasFields(base_class_decl) == false)
7312 continue;
7313
7314 CompilerType base_class_clang_type(getASTContext(),
7315 base_class->getType());
7316 std::string base_class_type_name(
7317 base_class_clang_type.GetTypeName().AsCString(""));
7318 if (base_class_type_name.compare(name) == 0)
7319 return child_idx;
7320 ++child_idx;
7321 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007322 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007323
Kate Stoneb9c1b512016-09-06 20:57:50 +00007324 // Try and find a field that matches NAME
7325 clang::RecordDecl::field_iterator field, field_end;
7326 llvm::StringRef name_sref(name);
7327 for (field = record_decl->field_begin(),
7328 field_end = record_decl->field_end();
7329 field != field_end; ++field, ++child_idx) {
7330 if (field->getName().equals(name_sref))
7331 return child_idx;
7332 }
7333 }
7334 break;
7335
7336 case clang::Type::ObjCObject:
7337 case clang::Type::ObjCInterface:
7338 if (GetCompleteType(type)) {
7339 llvm::StringRef name_sref(name);
7340 const clang::ObjCObjectType *objc_class_type =
7341 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7342 assert(objc_class_type);
7343 if (objc_class_type) {
7344 uint32_t child_idx = 0;
7345 clang::ObjCInterfaceDecl *class_interface_decl =
7346 objc_class_type->getInterface();
7347
7348 if (class_interface_decl) {
7349 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7350 ivar_end = class_interface_decl->ivar_end();
7351 clang::ObjCInterfaceDecl *superclass_interface_decl =
7352 class_interface_decl->getSuperClass();
7353
7354 for (ivar_pos = class_interface_decl->ivar_begin();
7355 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7356 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7357
7358 if (ivar_decl->getName().equals(name_sref)) {
7359 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7360 (omit_empty_base_classes &&
7361 ObjCDeclHasIVars(superclass_interface_decl, true)))
7362 ++child_idx;
7363
7364 return child_idx;
7365 }
7366 }
7367
7368 if (superclass_interface_decl) {
7369 if (superclass_interface_decl->getName().equals(name_sref))
7370 return 0;
7371 }
7372 }
7373 }
7374 }
7375 break;
7376
7377 case clang::Type::ObjCObjectPointer: {
7378 CompilerType pointee_clang_type(
7379 getASTContext(),
7380 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7381 ->getPointeeType());
7382 return pointee_clang_type.GetIndexOfChildWithName(
7383 name, omit_empty_base_classes);
7384 } break;
7385
7386 case clang::Type::ConstantArray: {
7387 // const clang::ConstantArrayType *array =
7388 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7389 // const uint64_t element_count =
7390 // array->getSize().getLimitedValue();
7391 //
7392 // if (idx < element_count)
7393 // {
7394 // std::pair<uint64_t, unsigned> field_type_info =
7395 // ast->getTypeInfo(array->getElementType());
7396 //
7397 // char element_name[32];
7398 // ::snprintf (element_name, sizeof (element_name),
7399 // "%s[%u]", parent_name ? parent_name : "", idx);
7400 //
7401 // child_name.assign(element_name);
7402 // assert(field_type_info.first % 8 == 0);
7403 // child_byte_size = field_type_info.first / 8;
7404 // child_byte_offset = idx * child_byte_size;
7405 // return array->getElementType().getAsOpaquePtr();
7406 // }
7407 } break;
7408
7409 // case clang::Type::MemberPointerType:
7410 // {
7411 // MemberPointerType *mem_ptr_type =
7412 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7413 // clang::QualType pointee_type =
7414 // mem_ptr_type->getPointeeType();
7415 //
7416 // if (ClangASTContext::IsAggregateType
7417 // (pointee_type.getAsOpaquePtr()))
7418 // {
7419 // return GetIndexOfChildWithName (ast,
7420 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7421 // name);
7422 // }
7423 // }
7424 // break;
7425 //
7426 case clang::Type::LValueReference:
7427 case clang::Type::RValueReference: {
7428 const clang::ReferenceType *reference_type =
7429 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7430 CompilerType pointee_type(getASTContext(),
7431 reference_type->getPointeeType());
7432
7433 if (pointee_type.IsAggregateType()) {
7434 return pointee_type.GetIndexOfChildWithName(name,
7435 omit_empty_base_classes);
7436 }
7437 } break;
7438
7439 case clang::Type::Pointer: {
7440 const clang::PointerType *pointer_type =
7441 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7442 CompilerType pointee_type(getASTContext(),
7443 pointer_type->getPointeeType());
7444
7445 if (pointee_type.IsAggregateType()) {
7446 return pointee_type.GetIndexOfChildWithName(name,
7447 omit_empty_base_classes);
7448 } else {
7449 // if (parent_name)
7450 // {
7451 // child_name.assign(1, '*');
7452 // child_name += parent_name;
7453 // }
7454 //
7455 // // We have a pointer to an simple type
7456 // if (idx == 0)
7457 // {
7458 // std::pair<uint64_t, unsigned> clang_type_info
7459 // = ast->getTypeInfo(pointee_type);
7460 // assert(clang_type_info.first % 8 == 0);
7461 // child_byte_size = clang_type_info.first / 8;
7462 // child_byte_offset = 0;
7463 // return pointee_type.getAsOpaquePtr();
7464 // }
7465 }
7466 } break;
7467
7468 case clang::Type::Auto:
7469 return CompilerType(
7470 getASTContext(),
7471 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7472 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7473
7474 case clang::Type::Elaborated:
7475 return CompilerType(
7476 getASTContext(),
7477 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7478 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7479
7480 case clang::Type::Paren:
7481 return CompilerType(getASTContext(),
7482 llvm::cast<clang::ParenType>(qual_type)->desugar())
7483 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7484
7485 case clang::Type::Typedef:
7486 return CompilerType(getASTContext(),
7487 llvm::cast<clang::TypedefType>(qual_type)
7488 ->getDecl()
7489 ->getUnderlyingType())
7490 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7491
7492 default:
7493 break;
7494 }
7495 }
7496 return UINT32_MAX;
7497}
Greg Claytond8d4a572015-08-11 21:38:15 +00007498
7499size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007500ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7501 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007502 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007503
Kate Stoneb9c1b512016-09-06 20:57:50 +00007504 clang::QualType qual_type(GetCanonicalQualType(type));
7505 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7506 switch (type_class) {
7507 case clang::Type::Record:
7508 if (GetCompleteType(type)) {
7509 const clang::CXXRecordDecl *cxx_record_decl =
7510 qual_type->getAsCXXRecordDecl();
7511 if (cxx_record_decl) {
7512 const clang::ClassTemplateSpecializationDecl *template_decl =
7513 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7514 cxx_record_decl);
7515 if (template_decl)
7516 return template_decl->getTemplateArgs().size();
7517 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007518 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007519 break;
7520
7521 case clang::Type::Typedef:
7522 return (CompilerType(getASTContext(),
7523 llvm::cast<clang::TypedefType>(qual_type)
7524 ->getDecl()
7525 ->getUnderlyingType()))
7526 .GetNumTemplateArguments();
7527
7528 case clang::Type::Auto:
7529 return (CompilerType(
7530 getASTContext(),
7531 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7532 .GetNumTemplateArguments();
7533
7534 case clang::Type::Elaborated:
7535 return (CompilerType(
7536 getASTContext(),
7537 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7538 .GetNumTemplateArguments();
7539
7540 case clang::Type::Paren:
7541 return (CompilerType(getASTContext(),
7542 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7543 .GetNumTemplateArguments();
7544
7545 default:
7546 break;
7547 }
7548
7549 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007550}
7551
Pavel Labath769b21e2017-11-13 14:26:21 +00007552const clang::ClassTemplateSpecializationDecl *
7553ClangASTContext::GetAsTemplateSpecialization(
7554 lldb::opaque_compiler_type_t type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00007555 if (!type)
Pavel Labath769b21e2017-11-13 14:26:21 +00007556 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007557
7558 clang::QualType qual_type(GetCanonicalQualType(type));
7559 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7560 switch (type_class) {
Pavel Labath769b21e2017-11-13 14:26:21 +00007561 case clang::Type::Record: {
7562 if (! GetCompleteType(type))
7563 return nullptr;
7564 const clang::CXXRecordDecl *cxx_record_decl =
7565 qual_type->getAsCXXRecordDecl();
7566 if (!cxx_record_decl)
7567 return nullptr;
7568 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7569 cxx_record_decl);
7570 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007571
7572 case clang::Type::Typedef:
Pavel Labath769b21e2017-11-13 14:26:21 +00007573 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7574 ->getDecl()
7575 ->getUnderlyingType()
7576 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007577
7578 case clang::Type::Auto:
Pavel Labath769b21e2017-11-13 14:26:21 +00007579 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7580 ->getDeducedType()
7581 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007582
7583 case clang::Type::Elaborated:
Pavel Labath769b21e2017-11-13 14:26:21 +00007584 return GetAsTemplateSpecialization(
7585 llvm::cast<clang::ElaboratedType>(qual_type)
7586 ->getNamedType()
7587 .getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007588
7589 case clang::Type::Paren:
Pavel Labath769b21e2017-11-13 14:26:21 +00007590 return GetAsTemplateSpecialization(
7591 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
Kate Stoneb9c1b512016-09-06 20:57:50 +00007592
7593 default:
Pavel Labath769b21e2017-11-13 14:26:21 +00007594 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007595 }
Pavel Labath769b21e2017-11-13 14:26:21 +00007596}
7597
7598lldb::TemplateArgumentKind
7599ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7600 size_t arg_idx) {
7601 const clang::ClassTemplateSpecializationDecl *template_decl =
7602 GetAsTemplateSpecialization(type);
7603 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7604 return eTemplateArgumentKindNull;
7605
7606 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7607 case clang::TemplateArgument::Null:
7608 return eTemplateArgumentKindNull;
7609
7610 case clang::TemplateArgument::NullPtr:
7611 return eTemplateArgumentKindNullPtr;
7612
7613 case clang::TemplateArgument::Type:
7614 return eTemplateArgumentKindType;
7615
7616 case clang::TemplateArgument::Declaration:
7617 return eTemplateArgumentKindDeclaration;
7618
7619 case clang::TemplateArgument::Integral:
7620 return eTemplateArgumentKindIntegral;
7621
7622 case clang::TemplateArgument::Template:
7623 return eTemplateArgumentKindTemplate;
7624
7625 case clang::TemplateArgument::TemplateExpansion:
7626 return eTemplateArgumentKindTemplateExpansion;
7627
7628 case clang::TemplateArgument::Expression:
7629 return eTemplateArgumentKindExpression;
7630
7631 case clang::TemplateArgument::Pack:
7632 return eTemplateArgumentKindPack;
7633 }
7634 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7635}
7636
7637CompilerType
7638ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7639 size_t idx) {
7640 const clang::ClassTemplateSpecializationDecl *template_decl =
7641 GetAsTemplateSpecialization(type);
7642 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7643 return CompilerType();
7644
7645 const clang::TemplateArgument &template_arg =
7646 template_decl->getTemplateArgs()[idx];
7647 if (template_arg.getKind() != clang::TemplateArgument::Type)
7648 return CompilerType();
7649
7650 return CompilerType(getASTContext(), template_arg.getAsType());
7651}
7652
7653std::pair<llvm::APSInt, CompilerType>
7654ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7655 size_t idx) {
7656 const clang::ClassTemplateSpecializationDecl *template_decl =
7657 GetAsTemplateSpecialization(type);
7658 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
7659 return {llvm::APSInt(0), CompilerType()};
7660
7661 const clang::TemplateArgument &template_arg =
7662 template_decl->getTemplateArgs()[idx];
7663 if (template_arg.getKind() != clang::TemplateArgument::Integral)
7664 return {llvm::APSInt(0), CompilerType()};
7665
7666 return {template_arg.getAsIntegral(),
7667 CompilerType(getASTContext(), template_arg.getIntegralType())};
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007668}
7669
Kate Stoneb9c1b512016-09-06 20:57:50 +00007670CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7671 if (type)
7672 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7673 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007674}
7675
Kate Stoneb9c1b512016-09-06 20:57:50 +00007676clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7677 const clang::EnumType *enutype =
7678 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7679 if (enutype)
7680 return enutype->getDecl();
7681 return NULL;
7682}
7683
7684clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7685 const clang::RecordType *record_type =
7686 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7687 if (record_type)
7688 return record_type->getDecl();
7689 return nullptr;
7690}
7691
7692clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
7693 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
7694 if (qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00007695 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007696 else
7697 return qual_type->getAsTagDecl();
Greg Claytone6b36cd2015-12-08 01:02:08 +00007698}
7699
Greg Claytond8d4a572015-08-11 21:38:15 +00007700clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007701ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7702 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007703}
7704
7705clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007706ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7707 const clang::ObjCObjectType *objc_class_type =
7708 llvm::dyn_cast<clang::ObjCObjectType>(
7709 ClangUtil::GetCanonicalQualType(type));
7710 if (objc_class_type)
7711 return objc_class_type->getInterface();
7712 return nullptr;
7713}
7714
7715clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
7716 const CompilerType &type, const char *name,
7717 const CompilerType &field_clang_type, AccessType access,
7718 uint32_t bitfield_bit_size) {
7719 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007720 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007721 ClangASTContext *ast =
7722 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7723 if (!ast)
7724 return nullptr;
7725 clang::ASTContext *clang_ast = ast->getASTContext();
7726
7727 clang::FieldDecl *field = nullptr;
7728
7729 clang::Expr *bit_width = nullptr;
7730 if (bitfield_bit_size != 0) {
7731 llvm::APInt bitfield_bit_size_apint(
7732 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7733 bit_width = new (*clang_ast)
7734 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7735 clang_ast->IntTy, clang::SourceLocation());
7736 }
7737
7738 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7739 if (record_decl) {
7740 field = clang::FieldDecl::Create(
7741 *clang_ast, record_decl, clang::SourceLocation(),
7742 clang::SourceLocation(),
7743 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7744 ClangUtil::GetQualType(field_clang_type), // Field type
7745 nullptr, // TInfo *
7746 bit_width, // BitWidth
7747 false, // Mutable
7748 clang::ICIS_NoInit); // HasInit
7749
7750 if (!name) {
7751 // Determine whether this field corresponds to an anonymous
7752 // struct or union.
7753 if (const clang::TagType *TagT =
7754 field->getType()->getAs<clang::TagType>()) {
7755 if (clang::RecordDecl *Rec =
7756 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7757 if (!Rec->getDeclName()) {
7758 Rec->setAnonymousStructOrUnion(true);
7759 field->setImplicit();
7760 }
7761 }
7762 }
7763
7764 if (field) {
7765 field->setAccess(
7766 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7767
7768 record_decl->addDecl(field);
7769
7770#ifdef LLDB_CONFIGURATION_DEBUG
7771 VerifyDecl(field);
7772#endif
7773 }
7774 } else {
7775 clang::ObjCInterfaceDecl *class_interface_decl =
7776 ast->GetAsObjCInterfaceDecl(type);
7777
7778 if (class_interface_decl) {
7779 const bool is_synthesized = false;
7780
7781 field_clang_type.GetCompleteType();
7782
7783 field = clang::ObjCIvarDecl::Create(
7784 *clang_ast, class_interface_decl, clang::SourceLocation(),
7785 clang::SourceLocation(),
7786 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7787 ClangUtil::GetQualType(field_clang_type), // Field type
7788 nullptr, // TypeSourceInfo *
7789 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7790 is_synthesized);
7791
7792 if (field) {
7793 class_interface_decl->addDecl(field);
7794
7795#ifdef LLDB_CONFIGURATION_DEBUG
7796 VerifyDecl(field);
7797#endif
7798 }
7799 }
7800 }
7801 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007802}
7803
Kate Stoneb9c1b512016-09-06 20:57:50 +00007804void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7805 if (!type)
7806 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007807
Kate Stoneb9c1b512016-09-06 20:57:50 +00007808 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7809 if (!ast)
7810 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007811
Kate Stoneb9c1b512016-09-06 20:57:50 +00007812 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007813
Kate Stoneb9c1b512016-09-06 20:57:50 +00007814 if (!record_decl)
7815 return;
7816
7817 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7818
7819 IndirectFieldVector indirect_fields;
7820 clang::RecordDecl::field_iterator field_pos;
7821 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7822 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7823 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7824 last_field_pos = field_pos++) {
7825 if (field_pos->isAnonymousStructOrUnion()) {
7826 clang::QualType field_qual_type = field_pos->getType();
7827
7828 const clang::RecordType *field_record_type =
7829 field_qual_type->getAs<clang::RecordType>();
7830
7831 if (!field_record_type)
7832 continue;
7833
7834 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7835
7836 if (!field_record_decl)
7837 continue;
7838
7839 for (clang::RecordDecl::decl_iterator
7840 di = field_record_decl->decls_begin(),
7841 de = field_record_decl->decls_end();
7842 di != de; ++di) {
7843 if (clang::FieldDecl *nested_field_decl =
7844 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7845 clang::NamedDecl **chain =
7846 new (*ast->getASTContext()) clang::NamedDecl *[2];
7847 chain[0] = *field_pos;
7848 chain[1] = nested_field_decl;
7849 clang::IndirectFieldDecl *indirect_field =
7850 clang::IndirectFieldDecl::Create(
7851 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7852 nested_field_decl->getIdentifier(),
7853 nested_field_decl->getType(), {chain, 2});
7854
7855 indirect_field->setImplicit();
7856
7857 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7858 field_pos->getAccess(), nested_field_decl->getAccess()));
7859
7860 indirect_fields.push_back(indirect_field);
7861 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7862 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7863 size_t nested_chain_size =
7864 nested_indirect_field_decl->getChainingSize();
7865 clang::NamedDecl **chain = new (*ast->getASTContext())
7866 clang::NamedDecl *[nested_chain_size + 1];
7867 chain[0] = *field_pos;
7868
7869 int chain_index = 1;
7870 for (clang::IndirectFieldDecl::chain_iterator
7871 nci = nested_indirect_field_decl->chain_begin(),
7872 nce = nested_indirect_field_decl->chain_end();
7873 nci < nce; ++nci) {
7874 chain[chain_index] = *nci;
7875 chain_index++;
7876 }
7877
7878 clang::IndirectFieldDecl *indirect_field =
7879 clang::IndirectFieldDecl::Create(
7880 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7881 nested_indirect_field_decl->getIdentifier(),
7882 nested_indirect_field_decl->getType(),
7883 {chain, nested_chain_size + 1});
7884
7885 indirect_field->setImplicit();
7886
7887 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7888 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
7889
7890 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00007891 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007892 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007893 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007894 }
7895
7896 // Check the last field to see if it has an incomplete array type as its
7897 // last member and if it does, the tell the record decl about it
7898 if (last_field_pos != field_end_pos) {
7899 if (last_field_pos->getType()->isIncompleteArrayType())
7900 record_decl->hasFlexibleArrayMember();
7901 }
7902
7903 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
7904 ife = indirect_fields.end();
7905 ifi < ife; ++ifi) {
7906 record_decl->addDecl(*ifi);
7907 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007908}
7909
Kate Stoneb9c1b512016-09-06 20:57:50 +00007910void ClangASTContext::SetIsPacked(const CompilerType &type) {
7911 if (type) {
7912 ClangASTContext *ast =
7913 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7914 if (ast) {
7915 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
7916
7917 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00007918 return;
7919
Kate Stoneb9c1b512016-09-06 20:57:50 +00007920 record_decl->addAttr(
7921 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00007922 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007923 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007924}
7925
Kate Stoneb9c1b512016-09-06 20:57:50 +00007926clang::VarDecl *ClangASTContext::AddVariableToRecordType(
7927 const CompilerType &type, const char *name, const CompilerType &var_type,
7928 AccessType access) {
7929 clang::VarDecl *var_decl = nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007930
Kate Stoneb9c1b512016-09-06 20:57:50 +00007931 if (!type.IsValid() || !var_type.IsValid())
7932 return nullptr;
7933 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7934 if (!ast)
7935 return nullptr;
7936
7937 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7938 if (record_decl) {
7939 var_decl = clang::VarDecl::Create(
7940 *ast->getASTContext(), // ASTContext &
7941 record_decl, // DeclContext *
7942 clang::SourceLocation(), // clang::SourceLocation StartLoc
7943 clang::SourceLocation(), // clang::SourceLocation IdLoc
7944 name ? &ast->getASTContext()->Idents.get(name)
7945 : nullptr, // clang::IdentifierInfo *
7946 ClangUtil::GetQualType(var_type), // Variable clang::QualType
7947 nullptr, // TypeSourceInfo *
7948 clang::SC_Static); // StorageClass
7949 if (var_decl) {
7950 var_decl->setAccess(
7951 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7952 record_decl->addDecl(var_decl);
7953
Greg Claytond8d4a572015-08-11 21:38:15 +00007954#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00007955 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00007956#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00007957 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007958 }
7959 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00007960}
7961
Kate Stoneb9c1b512016-09-06 20:57:50 +00007962clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
7963 lldb::opaque_compiler_type_t type, const char *name,
7964 const CompilerType &method_clang_type, lldb::AccessType access,
7965 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
7966 bool is_attr_used, bool is_artificial) {
7967 if (!type || !method_clang_type.IsValid() || name == nullptr ||
7968 name[0] == '\0')
7969 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007970
Kate Stoneb9c1b512016-09-06 20:57:50 +00007971 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007972
Kate Stoneb9c1b512016-09-06 20:57:50 +00007973 clang::CXXRecordDecl *cxx_record_decl =
7974 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007975
Kate Stoneb9c1b512016-09-06 20:57:50 +00007976 if (cxx_record_decl == nullptr)
7977 return nullptr;
7978
7979 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
7980
7981 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7982
7983 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
7984
7985 const clang::FunctionType *function_type =
7986 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7987
7988 if (function_type == nullptr)
7989 return nullptr;
7990
7991 const clang::FunctionProtoType *method_function_prototype(
7992 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7993
7994 if (!method_function_prototype)
7995 return nullptr;
7996
7997 unsigned int num_params = method_function_prototype->getNumParams();
7998
7999 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
8000 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
8001
8002 if (is_artificial)
8003 return nullptr; // skip everything artificial
8004
8005 if (name[0] == '~') {
8006 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
8007 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8008 clang::DeclarationNameInfo(
8009 getASTContext()->DeclarationNames.getCXXDestructorName(
8010 getASTContext()->getCanonicalType(record_qual_type)),
8011 clang::SourceLocation()),
8012 method_qual_type, nullptr, is_inline, is_artificial);
8013 cxx_method_decl = cxx_dtor_decl;
8014 } else if (decl_name == cxx_record_decl->getDeclName()) {
8015 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
8016 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8017 clang::DeclarationNameInfo(
8018 getASTContext()->DeclarationNames.getCXXConstructorName(
8019 getASTContext()->getCanonicalType(record_qual_type)),
8020 clang::SourceLocation()),
8021 method_qual_type,
8022 nullptr, // TypeSourceInfo *
8023 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
8024 cxx_method_decl = cxx_ctor_decl;
8025 } else {
8026 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8027 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8028
8029 if (IsOperator(name, op_kind)) {
8030 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
8031 // Check the number of operator parameters. Sometimes we have
8032 // seen bad DWARF that doesn't correctly describe operators and
8033 // if we try to create a method and add it to the class, clang
8034 // will assert and crash, so we need to make sure things are
8035 // acceptable.
8036 const bool is_method = true;
8037 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8038 is_method, op_kind, num_params))
8039 return nullptr;
8040 cxx_method_decl = clang::CXXMethodDecl::Create(
8041 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8042 clang::DeclarationNameInfo(
8043 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8044 clang::SourceLocation()),
8045 method_qual_type,
8046 nullptr, // TypeSourceInfo *
8047 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8048 } else if (num_params == 0) {
8049 // Conversion operators don't take params...
8050 cxx_method_decl = clang::CXXConversionDecl::Create(
8051 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8052 clang::DeclarationNameInfo(
8053 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8054 getASTContext()->getCanonicalType(
8055 function_type->getReturnType())),
8056 clang::SourceLocation()),
8057 method_qual_type,
8058 nullptr, // TypeSourceInfo *
8059 is_inline, is_explicit, false /*is_constexpr*/,
8060 clang::SourceLocation());
8061 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008062 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008063
8064 if (cxx_method_decl == nullptr) {
8065 cxx_method_decl = clang::CXXMethodDecl::Create(
8066 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8067 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8068 method_qual_type,
8069 nullptr, // TypeSourceInfo *
8070 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008071 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008072 }
8073
8074 clang::AccessSpecifier access_specifier =
8075 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8076
8077 cxx_method_decl->setAccess(access_specifier);
8078 cxx_method_decl->setVirtualAsWritten(is_virtual);
8079
8080 if (is_attr_used)
8081 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8082
8083 // Populate the method decl with parameter decls
8084
8085 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8086
8087 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8088 params.push_back(clang::ParmVarDecl::Create(
8089 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8090 clang::SourceLocation(),
8091 nullptr, // anonymous
8092 method_function_prototype->getParamType(param_index), nullptr,
8093 clang::SC_None, nullptr));
8094 }
8095
8096 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8097
8098 cxx_record_decl->addDecl(cxx_method_decl);
8099
8100 // Sometimes the debug info will mention a constructor (default/copy/move),
8101 // destructor, or assignment operator (copy/move) but there won't be any
8102 // version of this in the code. So we check if the function was artificially
8103 // generated and if it is trivial and this lets the compiler/backend know
8104 // that it can inline the IR for these when it needs to and we can avoid a
8105 // "missing function" error when running expressions.
8106
8107 if (is_artificial) {
8108 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8109 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8110 (cxx_ctor_decl->isCopyConstructor() &&
8111 cxx_record_decl->hasTrivialCopyConstructor()) ||
8112 (cxx_ctor_decl->isMoveConstructor() &&
8113 cxx_record_decl->hasTrivialMoveConstructor()))) {
8114 cxx_ctor_decl->setDefaulted();
8115 cxx_ctor_decl->setTrivial(true);
8116 } else if (cxx_dtor_decl) {
8117 if (cxx_record_decl->hasTrivialDestructor()) {
8118 cxx_dtor_decl->setDefaulted();
8119 cxx_dtor_decl->setTrivial(true);
8120 }
8121 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8122 cxx_record_decl->hasTrivialCopyAssignment()) ||
8123 (cxx_method_decl->isMoveAssignmentOperator() &&
8124 cxx_record_decl->hasTrivialMoveAssignment())) {
8125 cxx_method_decl->setDefaulted();
8126 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008127 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008128 }
8129
Greg Claytond8d4a572015-08-11 21:38:15 +00008130#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008131 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008132#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008133
Kate Stoneb9c1b512016-09-06 20:57:50 +00008134 // printf ("decl->isPolymorphic() = %i\n",
8135 // cxx_record_decl->isPolymorphic());
8136 // printf ("decl->isAggregate() = %i\n",
8137 // cxx_record_decl->isAggregate());
8138 // printf ("decl->isPOD() = %i\n",
8139 // cxx_record_decl->isPOD());
8140 // printf ("decl->isEmpty() = %i\n",
8141 // cxx_record_decl->isEmpty());
8142 // printf ("decl->isAbstract() = %i\n",
8143 // cxx_record_decl->isAbstract());
8144 // printf ("decl->hasTrivialConstructor() = %i\n",
8145 // cxx_record_decl->hasTrivialConstructor());
8146 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8147 // cxx_record_decl->hasTrivialCopyConstructor());
8148 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8149 // cxx_record_decl->hasTrivialCopyAssignment());
8150 // printf ("decl->hasTrivialDestructor() = %i\n",
8151 // cxx_record_decl->hasTrivialDestructor());
8152 return cxx_method_decl;
8153}
Greg Claytond8d4a572015-08-11 21:38:15 +00008154
8155#pragma mark C++ Base Classes
8156
8157clang::CXXBaseSpecifier *
Kate Stoneb9c1b512016-09-06 20:57:50 +00008158ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8159 AccessType access, bool is_virtual,
8160 bool base_of_class) {
8161 if (type)
8162 return new clang::CXXBaseSpecifier(
8163 clang::SourceRange(), is_virtual, base_of_class,
8164 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8165 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8166 clang::SourceLocation());
8167 return nullptr;
8168}
8169
8170void ClangASTContext::DeleteBaseClassSpecifiers(
8171 clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes) {
8172 for (unsigned i = 0; i < num_base_classes; ++i) {
8173 delete base_classes[i];
8174 base_classes[i] = nullptr;
8175 }
8176}
8177
8178bool ClangASTContext::SetBaseClassesForClassType(
8179 lldb::opaque_compiler_type_t type,
8180 clang::CXXBaseSpecifier const *const *base_classes,
8181 unsigned num_base_classes) {
8182 if (type) {
8183 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8184 if (cxx_record_decl) {
8185 cxx_record_decl->setBases(base_classes, num_base_classes);
8186 return true;
8187 }
8188 }
8189 return false;
8190}
8191
8192bool ClangASTContext::SetObjCSuperClass(
8193 const CompilerType &type, const CompilerType &superclass_clang_type) {
8194 ClangASTContext *ast =
8195 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8196 if (!ast)
8197 return false;
8198 clang::ASTContext *clang_ast = ast->getASTContext();
8199
8200 if (type && superclass_clang_type.IsValid() &&
8201 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8202 clang::ObjCInterfaceDecl *class_interface_decl =
8203 GetAsObjCInterfaceDecl(type);
8204 clang::ObjCInterfaceDecl *super_interface_decl =
8205 GetAsObjCInterfaceDecl(superclass_clang_type);
8206 if (class_interface_decl && super_interface_decl) {
8207 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8208 clang_ast->getObjCInterfaceType(super_interface_decl)));
8209 return true;
8210 }
8211 }
8212 return false;
8213}
8214
8215bool ClangASTContext::AddObjCClassProperty(
8216 const CompilerType &type, const char *property_name,
8217 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8218 const char *property_setter_name, const char *property_getter_name,
8219 uint32_t property_attributes, ClangASTMetadata *metadata) {
8220 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8221 property_name[0] == '\0')
8222 return false;
8223 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8224 if (!ast)
8225 return false;
8226 clang::ASTContext *clang_ast = ast->getASTContext();
8227
8228 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8229
8230 if (class_interface_decl) {
8231 CompilerType property_clang_type_to_access;
8232
8233 if (property_clang_type.IsValid())
8234 property_clang_type_to_access = property_clang_type;
8235 else if (ivar_decl)
8236 property_clang_type_to_access =
8237 CompilerType(clang_ast, ivar_decl->getType());
8238
8239 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8240 clang::TypeSourceInfo *prop_type_source;
8241 if (ivar_decl)
8242 prop_type_source =
8243 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8244 else
8245 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8246 ClangUtil::GetQualType(property_clang_type));
8247
8248 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8249 *clang_ast, class_interface_decl,
8250 clang::SourceLocation(), // Source Location
8251 &clang_ast->Idents.get(property_name),
8252 clang::SourceLocation(), // Source Location for AT
8253 clang::SourceLocation(), // Source location for (
8254 ivar_decl ? ivar_decl->getType()
8255 : ClangUtil::GetQualType(property_clang_type),
8256 prop_type_source);
8257
8258 if (property_decl) {
8259 if (metadata)
8260 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8261
8262 class_interface_decl->addDecl(property_decl);
8263
8264 clang::Selector setter_sel, getter_sel;
8265
8266 if (property_setter_name != nullptr) {
8267 std::string property_setter_no_colon(
8268 property_setter_name, strlen(property_setter_name) - 1);
8269 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008270 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008271 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8272 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8273 std::string setter_sel_string("set");
8274 setter_sel_string.push_back(::toupper(property_name[0]));
8275 setter_sel_string.append(&property_name[1]);
8276 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008277 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008278 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8279 }
8280 property_decl->setSetterName(setter_sel);
8281 property_decl->setPropertyAttributes(
8282 clang::ObjCPropertyDecl::OBJC_PR_setter);
8283
8284 if (property_getter_name != nullptr) {
8285 clang::IdentifierInfo *getter_ident =
8286 &clang_ast->Idents.get(property_getter_name);
8287 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8288 } else {
8289 clang::IdentifierInfo *getter_ident =
8290 &clang_ast->Idents.get(property_name);
8291 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8292 }
8293 property_decl->setGetterName(getter_sel);
8294 property_decl->setPropertyAttributes(
8295 clang::ObjCPropertyDecl::OBJC_PR_getter);
8296
8297 if (ivar_decl)
8298 property_decl->setPropertyIvarDecl(ivar_decl);
8299
8300 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8301 property_decl->setPropertyAttributes(
8302 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8303 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8304 property_decl->setPropertyAttributes(
8305 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8306 if (property_attributes & DW_APPLE_PROPERTY_assign)
8307 property_decl->setPropertyAttributes(
8308 clang::ObjCPropertyDecl::OBJC_PR_assign);
8309 if (property_attributes & DW_APPLE_PROPERTY_retain)
8310 property_decl->setPropertyAttributes(
8311 clang::ObjCPropertyDecl::OBJC_PR_retain);
8312 if (property_attributes & DW_APPLE_PROPERTY_copy)
8313 property_decl->setPropertyAttributes(
8314 clang::ObjCPropertyDecl::OBJC_PR_copy);
8315 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8316 property_decl->setPropertyAttributes(
8317 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8318 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8319 property_decl->setPropertyAttributes(
8320 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8321 if (property_attributes &
8322 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8323 property_decl->setPropertyAttributes(
8324 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8325 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8326 property_decl->setPropertyAttributes(
8327 clang::ObjCPropertyDecl::OBJC_PR_class);
8328
8329 const bool isInstance =
8330 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8331
8332 if (!getter_sel.isNull() &&
8333 !(isInstance
8334 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8335 : class_interface_decl->lookupClassMethod(getter_sel))) {
8336 const bool isVariadic = false;
8337 const bool isSynthesized = false;
8338 const bool isImplicitlyDeclared = true;
8339 const bool isDefined = false;
8340 const clang::ObjCMethodDecl::ImplementationControl impControl =
8341 clang::ObjCMethodDecl::None;
8342 const bool HasRelatedResultType = false;
8343
8344 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8345 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8346 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8347 nullptr, class_interface_decl, isInstance, isVariadic,
8348 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8349 HasRelatedResultType);
8350
8351 if (getter && metadata)
8352 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8353
8354 if (getter) {
8355 getter->setMethodParams(*clang_ast,
8356 llvm::ArrayRef<clang::ParmVarDecl *>(),
8357 llvm::ArrayRef<clang::SourceLocation>());
8358
8359 class_interface_decl->addDecl(getter);
8360 }
8361 }
8362
8363 if (!setter_sel.isNull() &&
8364 !(isInstance
8365 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8366 : class_interface_decl->lookupClassMethod(setter_sel))) {
8367 clang::QualType result_type = clang_ast->VoidTy;
8368 const bool isVariadic = false;
8369 const bool isSynthesized = false;
8370 const bool isImplicitlyDeclared = true;
8371 const bool isDefined = false;
8372 const clang::ObjCMethodDecl::ImplementationControl impControl =
8373 clang::ObjCMethodDecl::None;
8374 const bool HasRelatedResultType = false;
8375
8376 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8377 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8378 setter_sel, result_type, nullptr, class_interface_decl,
8379 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8380 isDefined, impControl, HasRelatedResultType);
8381
8382 if (setter && metadata)
8383 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8384
8385 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8386
8387 params.push_back(clang::ParmVarDecl::Create(
8388 *clang_ast, setter, clang::SourceLocation(),
8389 clang::SourceLocation(),
8390 nullptr, // anonymous
8391 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8392 clang::SC_Auto, nullptr));
8393
8394 if (setter) {
8395 setter->setMethodParams(
8396 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8397 llvm::ArrayRef<clang::SourceLocation>());
8398
8399 class_interface_decl->addDecl(setter);
8400 }
8401 }
8402
8403 return true;
8404 }
8405 }
8406 }
8407 return false;
8408}
8409
8410bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8411 bool check_superclass) {
8412 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8413 if (class_interface_decl)
8414 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8415 return false;
8416}
8417
8418clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8419 const CompilerType &type,
8420 const char *name, // the full symbol name as seen in the symbol table
8421 // (lldb::opaque_compiler_type_t type, "-[NString
8422 // stringWithCString:]")
8423 const CompilerType &method_clang_type, lldb::AccessType access,
8424 bool is_artificial, bool is_variadic) {
8425 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008426 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008427
Kate Stoneb9c1b512016-09-06 20:57:50 +00008428 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8429
8430 if (class_interface_decl == nullptr)
8431 return nullptr;
8432 ClangASTContext *lldb_ast =
8433 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8434 if (lldb_ast == nullptr)
8435 return nullptr;
8436 clang::ASTContext *ast = lldb_ast->getASTContext();
8437
8438 const char *selector_start = ::strchr(name, ' ');
8439 if (selector_start == nullptr)
8440 return nullptr;
8441
8442 selector_start++;
8443 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8444
8445 size_t len = 0;
8446 const char *start;
8447 // printf ("name = '%s'\n", name);
8448
8449 unsigned num_selectors_with_args = 0;
8450 for (start = selector_start; start && *start != '\0' && *start != ']';
8451 start += len) {
8452 len = ::strcspn(start, ":]");
8453 bool has_arg = (start[len] == ':');
8454 if (has_arg)
8455 ++num_selectors_with_args;
8456 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8457 if (has_arg)
8458 len += 1;
8459 }
8460
8461 if (selector_idents.size() == 0)
8462 return nullptr;
8463
8464 clang::Selector method_selector = ast->Selectors.getSelector(
8465 num_selectors_with_args ? selector_idents.size() : 0,
8466 selector_idents.data());
8467
8468 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8469
8470 // Populate the method decl with parameter decls
8471 const clang::Type *method_type(method_qual_type.getTypePtr());
8472
8473 if (method_type == nullptr)
8474 return nullptr;
8475
8476 const clang::FunctionProtoType *method_function_prototype(
8477 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8478
8479 if (!method_function_prototype)
8480 return nullptr;
8481
8482 bool is_synthesized = false;
8483 bool is_defined = false;
8484 clang::ObjCMethodDecl::ImplementationControl imp_control =
8485 clang::ObjCMethodDecl::None;
8486
8487 const unsigned num_args = method_function_prototype->getNumParams();
8488
8489 if (num_args != num_selectors_with_args)
8490 return nullptr; // some debug information is corrupt. We are not going to
8491 // deal with it.
8492
8493 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8494 *ast,
8495 clang::SourceLocation(), // beginLoc,
8496 clang::SourceLocation(), // endLoc,
8497 method_selector, method_function_prototype->getReturnType(),
8498 nullptr, // TypeSourceInfo *ResultTInfo,
8499 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8500 ClangUtil::GetQualType(type)),
8501 name[0] == '-', is_variadic, is_synthesized,
8502 true, // is_implicitly_declared; we force this to true because we don't
8503 // have source locations
8504 is_defined, imp_control, false /*has_related_result_type*/);
8505
8506 if (objc_method_decl == nullptr)
8507 return nullptr;
8508
8509 if (num_args > 0) {
8510 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8511
8512 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8513 params.push_back(clang::ParmVarDecl::Create(
8514 *ast, objc_method_decl, clang::SourceLocation(),
8515 clang::SourceLocation(),
8516 nullptr, // anonymous
8517 method_function_prototype->getParamType(param_index), nullptr,
8518 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008519 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008520
Kate Stoneb9c1b512016-09-06 20:57:50 +00008521 objc_method_decl->setMethodParams(
8522 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8523 llvm::ArrayRef<clang::SourceLocation>());
8524 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008525
Kate Stoneb9c1b512016-09-06 20:57:50 +00008526 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008527
Greg Claytond8d4a572015-08-11 21:38:15 +00008528#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008529 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008530#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008531
8532 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008533}
8534
Kate Stoneb9c1b512016-09-06 20:57:50 +00008535bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8536 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008537 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008538
Kate Stoneb9c1b512016-09-06 20:57:50 +00008539 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008540
Kate Stoneb9c1b512016-09-06 20:57:50 +00008541 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8542 switch (type_class) {
8543 case clang::Type::Record: {
8544 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8545 if (cxx_record_decl)
8546 return cxx_record_decl->hasExternalLexicalStorage() ||
8547 cxx_record_decl->hasExternalVisibleStorage();
8548 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008549
Kate Stoneb9c1b512016-09-06 20:57:50 +00008550 case clang::Type::Enum: {
8551 clang::EnumDecl *enum_decl =
8552 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8553 if (enum_decl)
8554 return enum_decl->hasExternalLexicalStorage() ||
8555 enum_decl->hasExternalVisibleStorage();
8556 } break;
8557
8558 case clang::Type::ObjCObject:
8559 case clang::Type::ObjCInterface: {
8560 const clang::ObjCObjectType *objc_class_type =
8561 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8562 assert(objc_class_type);
8563 if (objc_class_type) {
8564 clang::ObjCInterfaceDecl *class_interface_decl =
8565 objc_class_type->getInterface();
8566
8567 if (class_interface_decl)
8568 return class_interface_decl->hasExternalLexicalStorage() ||
8569 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008570 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008571 } break;
8572
8573 case clang::Type::Typedef:
8574 return GetHasExternalStorage(CompilerType(
8575 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8576 ->getDecl()
8577 ->getUnderlyingType()
8578 .getAsOpaquePtr()));
8579
8580 case clang::Type::Auto:
8581 return GetHasExternalStorage(CompilerType(
8582 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8583 ->getDeducedType()
8584 .getAsOpaquePtr()));
8585
8586 case clang::Type::Elaborated:
8587 return GetHasExternalStorage(CompilerType(
8588 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8589 ->getNamedType()
8590 .getAsOpaquePtr()));
8591
8592 case clang::Type::Paren:
8593 return GetHasExternalStorage(CompilerType(
8594 type.GetTypeSystem(),
8595 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8596
8597 default:
8598 break;
8599 }
8600 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008601}
8602
Kate Stoneb9c1b512016-09-06 20:57:50 +00008603bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8604 bool has_extern) {
8605 if (!type)
8606 return false;
8607
8608 clang::QualType qual_type(GetCanonicalQualType(type));
8609
8610 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8611 switch (type_class) {
8612 case clang::Type::Record: {
8613 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8614 if (cxx_record_decl) {
8615 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8616 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8617 return true;
8618 }
8619 } break;
8620
8621 case clang::Type::Enum: {
8622 clang::EnumDecl *enum_decl =
8623 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8624 if (enum_decl) {
8625 enum_decl->setHasExternalLexicalStorage(has_extern);
8626 enum_decl->setHasExternalVisibleStorage(has_extern);
8627 return true;
8628 }
8629 } break;
8630
8631 case clang::Type::ObjCObject:
8632 case clang::Type::ObjCInterface: {
8633 const clang::ObjCObjectType *objc_class_type =
8634 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8635 assert(objc_class_type);
8636 if (objc_class_type) {
8637 clang::ObjCInterfaceDecl *class_interface_decl =
8638 objc_class_type->getInterface();
8639
8640 if (class_interface_decl) {
8641 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8642 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8643 return true;
8644 }
8645 }
8646 } break;
8647
8648 case clang::Type::Typedef:
8649 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8650 ->getDecl()
8651 ->getUnderlyingType()
8652 .getAsOpaquePtr(),
8653 has_extern);
8654
8655 case clang::Type::Auto:
8656 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8657 ->getDeducedType()
8658 .getAsOpaquePtr(),
8659 has_extern);
8660
8661 case clang::Type::Elaborated:
8662 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8663 ->getNamedType()
8664 .getAsOpaquePtr(),
8665 has_extern);
8666
8667 case clang::Type::Paren:
8668 return SetHasExternalStorage(
8669 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8670 has_extern);
8671
8672 default:
8673 break;
8674 }
8675 return false;
8676}
Greg Claytond8d4a572015-08-11 21:38:15 +00008677
8678#pragma mark TagDecl
8679
Kate Stoneb9c1b512016-09-06 20:57:50 +00008680bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8681 clang::QualType qual_type(ClangUtil::GetQualType(type));
8682 if (!qual_type.isNull()) {
8683 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8684 if (tag_type) {
8685 clang::TagDecl *tag_decl = tag_type->getDecl();
8686 if (tag_decl) {
8687 tag_decl->startDefinition();
8688 return true;
8689 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008690 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008691
8692 const clang::ObjCObjectType *object_type =
8693 qual_type->getAs<clang::ObjCObjectType>();
8694 if (object_type) {
8695 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8696 if (interface_decl) {
8697 interface_decl->startDefinition();
8698 return true;
8699 }
8700 }
8701 }
8702 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008703}
8704
Kate Stoneb9c1b512016-09-06 20:57:50 +00008705bool ClangASTContext::CompleteTagDeclarationDefinition(
8706 const CompilerType &type) {
8707 clang::QualType qual_type(ClangUtil::GetQualType(type));
8708 if (!qual_type.isNull()) {
8709 // Make sure we use the same methodology as
8710 // ClangASTContext::StartTagDeclarationDefinition()
8711 // as to how we start/end the definition. Previously we were calling
8712 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8713 if (tag_type) {
8714 clang::TagDecl *tag_decl = tag_type->getDecl();
8715 if (tag_decl) {
8716 clang::CXXRecordDecl *cxx_record_decl =
8717 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8718
8719 if (cxx_record_decl) {
8720 if (!cxx_record_decl->isCompleteDefinition())
8721 cxx_record_decl->completeDefinition();
8722 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8723 cxx_record_decl->setHasExternalLexicalStorage(false);
8724 cxx_record_decl->setHasExternalVisibleStorage(false);
8725 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008726 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008727 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008728 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008729
8730 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8731
8732 if (enutype) {
8733 clang::EnumDecl *enum_decl = enutype->getDecl();
8734
8735 if (enum_decl) {
8736 if (!enum_decl->isCompleteDefinition()) {
8737 ClangASTContext *lldb_ast =
8738 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8739 if (lldb_ast == nullptr)
8740 return false;
8741 clang::ASTContext *ast = lldb_ast->getASTContext();
8742
8743 /// TODO This really needs to be fixed.
8744
8745 QualType integer_type(enum_decl->getIntegerType());
8746 if (!integer_type.isNull()) {
8747 unsigned NumPositiveBits = 1;
8748 unsigned NumNegativeBits = 0;
8749
8750 clang::QualType promotion_qual_type;
8751 // If the enum integer type is less than an integer in bit width,
8752 // then we must promote it to an integer size.
8753 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8754 ast->getTypeSize(ast->IntTy)) {
8755 if (enum_decl->getIntegerType()->isSignedIntegerType())
8756 promotion_qual_type = ast->IntTy;
8757 else
8758 promotion_qual_type = ast->UnsignedIntTy;
8759 } else
8760 promotion_qual_type = enum_decl->getIntegerType();
8761
8762 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8763 promotion_qual_type, NumPositiveBits,
8764 NumNegativeBits);
8765 }
8766 }
8767 return true;
8768 }
8769 }
8770 }
8771 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008772}
8773
Kate Stoneb9c1b512016-09-06 20:57:50 +00008774bool ClangASTContext::AddEnumerationValueToEnumerationType(
8775 lldb::opaque_compiler_type_t type,
8776 const CompilerType &enumerator_clang_type, const Declaration &decl,
8777 const char *name, int64_t enum_value, uint32_t enum_value_bit_size) {
8778 if (type && enumerator_clang_type.IsValid() && name && name[0]) {
8779 clang::QualType enum_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008780
Kate Stoneb9c1b512016-09-06 20:57:50 +00008781 bool is_signed = false;
8782 enumerator_clang_type.IsIntegerType(is_signed);
Greg Claytond8d4a572015-08-11 21:38:15 +00008783 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Kate Stoneb9c1b512016-09-06 20:57:50 +00008784 if (clang_type) {
8785 const clang::EnumType *enutype =
8786 llvm::dyn_cast<clang::EnumType>(clang_type);
8787
8788 if (enutype) {
8789 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8790 enum_llvm_apsint = enum_value;
8791 clang::EnumConstantDecl *enumerator_decl =
8792 clang::EnumConstantDecl::Create(
8793 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8794 name ? &getASTContext()->Idents.get(name)
8795 : nullptr, // Identifier
8796 ClangUtil::GetQualType(enumerator_clang_type),
8797 nullptr, enum_llvm_apsint);
8798
8799 if (enumerator_decl) {
8800 enutype->getDecl()->addDecl(enumerator_decl);
8801
8802#ifdef LLDB_CONFIGURATION_DEBUG
8803 VerifyDecl(enumerator_decl);
8804#endif
8805
8806 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008807 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008808 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008809 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008810 }
8811 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008812}
8813
Greg Claytona1e5dc82015-08-11 22:53:00 +00008814CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00008815ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8816 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8817 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8818 if (clang_type) {
8819 const clang::EnumType *enutype =
8820 llvm::dyn_cast<clang::EnumType>(clang_type);
8821 if (enutype) {
8822 clang::EnumDecl *enum_decl = enutype->getDecl();
8823 if (enum_decl)
8824 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008825 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008826 }
8827 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008828}
8829
Kate Stoneb9c1b512016-09-06 20:57:50 +00008830CompilerType
8831ClangASTContext::CreateMemberPointerType(const CompilerType &type,
8832 const CompilerType &pointee_type) {
8833 if (type && pointee_type.IsValid() &&
8834 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8835 ClangASTContext *ast =
8836 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8837 if (!ast)
8838 return CompilerType();
8839 return CompilerType(ast->getASTContext(),
8840 ast->getASTContext()->getMemberPointerType(
8841 ClangUtil::GetQualType(pointee_type),
8842 ClangUtil::GetQualType(type).getTypePtr()));
8843 }
8844 return CompilerType();
8845}
Greg Claytond8d4a572015-08-11 21:38:15 +00008846
8847size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00008848ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
8849 const char *s, uint8_t *dst,
8850 size_t dst_size) {
8851 if (type) {
8852 clang::QualType qual_type(GetCanonicalQualType(type));
8853 uint32_t count = 0;
8854 bool is_complex = false;
8855 if (IsFloatingPointType(type, count, is_complex)) {
8856 // TODO: handle complex and vector types
8857 if (count != 1)
8858 return false;
8859
8860 llvm::StringRef s_sref(s);
8861 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
8862 s_sref);
8863
8864 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
8865 const uint64_t byte_size = bit_size / 8;
8866 if (dst_size >= byte_size) {
8867 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
8868 llvm::NextPowerOf2(byte_size) * 8);
Zachary Turner97206d52017-05-12 04:51:55 +00008869 lldb_private::Status get_data_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008870 if (scalar.GetAsMemoryData(dst, byte_size,
8871 lldb_private::endian::InlHostByteOrder(),
8872 get_data_error))
8873 return byte_size;
8874 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008875 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008876 }
8877 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00008878}
8879
Greg Claytond8d4a572015-08-11 21:38:15 +00008880//----------------------------------------------------------------------
8881// Dumping types
8882//----------------------------------------------------------------------
8883#define DEPTH_INCREMENT 2
8884
Kate Stoneb9c1b512016-09-06 20:57:50 +00008885void ClangASTContext::DumpValue(
8886 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00008887 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008888 lldb::offset_t data_byte_offset, size_t data_byte_size,
8889 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
8890 bool show_summary, bool verbose, uint32_t depth) {
8891 if (!type)
8892 return;
8893
8894 clang::QualType qual_type(GetQualType(type));
8895 switch (qual_type->getTypeClass()) {
8896 case clang::Type::Record:
8897 if (GetCompleteType(type)) {
8898 const clang::RecordType *record_type =
8899 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8900 const clang::RecordDecl *record_decl = record_type->getDecl();
8901 assert(record_decl);
8902 uint32_t field_bit_offset = 0;
8903 uint32_t field_byte_offset = 0;
8904 const clang::ASTRecordLayout &record_layout =
8905 getASTContext()->getASTRecordLayout(record_decl);
8906 uint32_t child_idx = 0;
8907
8908 const clang::CXXRecordDecl *cxx_record_decl =
8909 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8910 if (cxx_record_decl) {
8911 // We might have base classes to print out first
8912 clang::CXXRecordDecl::base_class_const_iterator base_class,
8913 base_class_end;
8914 for (base_class = cxx_record_decl->bases_begin(),
8915 base_class_end = cxx_record_decl->bases_end();
8916 base_class != base_class_end; ++base_class) {
8917 const clang::CXXRecordDecl *base_class_decl =
8918 llvm::cast<clang::CXXRecordDecl>(
8919 base_class->getType()->getAs<clang::RecordType>()->getDecl());
8920
8921 // Skip empty base classes
8922 if (verbose == false &&
8923 ClangASTContext::RecordHasFields(base_class_decl) == false)
8924 continue;
8925
8926 if (base_class->isVirtual())
8927 field_bit_offset =
8928 record_layout.getVBaseClassOffset(base_class_decl)
8929 .getQuantity() *
8930 8;
8931 else
8932 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
8933 .getQuantity() *
8934 8;
8935 field_byte_offset = field_bit_offset / 8;
8936 assert(field_bit_offset % 8 == 0);
8937 if (child_idx == 0)
8938 s->PutChar('{');
8939 else
8940 s->PutChar(',');
8941
8942 clang::QualType base_class_qual_type = base_class->getType();
8943 std::string base_class_type_name(base_class_qual_type.getAsString());
8944
8945 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00008946 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
8947 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008948
8949 clang::TypeInfo base_class_type_info =
8950 getASTContext()->getTypeInfo(base_class_qual_type);
8951
8952 // Dump the value of the member
8953 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
8954 base_clang_type.DumpValue(
8955 exe_ctx,
8956 s, // Stream to dump to
8957 base_clang_type
8958 .GetFormat(), // The format with which to display the member
8959 data, // Data buffer containing all bytes for this type
8960 data_byte_offset + field_byte_offset, // Offset into "data" where
8961 // to grab value from
8962 base_class_type_info.Width / 8, // Size of this type in bytes
8963 0, // Bitfield bit size
8964 0, // Bitfield bit offset
8965 show_types, // Boolean indicating if we should show the variable
8966 // types
8967 show_summary, // Boolean indicating if we should show a summary
8968 // for the current type
8969 verbose, // Verbose output?
8970 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8971 // children
8972
8973 ++child_idx;
8974 }
8975 }
8976 uint32_t field_idx = 0;
8977 clang::RecordDecl::field_iterator field, field_end;
8978 for (field = record_decl->field_begin(),
8979 field_end = record_decl->field_end();
8980 field != field_end; ++field, ++field_idx, ++child_idx) {
8981 // Print the starting squiggly bracket (if this is the
8982 // first member) or comma (for member 2 and beyond) for
8983 // the struct/union/class member.
8984 if (child_idx == 0)
8985 s->PutChar('{');
8986 else
8987 s->PutChar(',');
8988
8989 // Indent
8990 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8991
8992 clang::QualType field_type = field->getType();
8993 // Print the member type if requested
8994 // Figure out the type byte size (field_type_info.first) and
8995 // alignment (field_type_info.second) from the AST context.
8996 clang::TypeInfo field_type_info =
8997 getASTContext()->getTypeInfo(field_type);
8998 assert(field_idx < record_layout.getFieldCount());
8999 // Figure out the field offset within the current struct/union/class
9000 // type
9001 field_bit_offset = record_layout.getFieldOffset(field_idx);
9002 field_byte_offset = field_bit_offset / 8;
9003 uint32_t field_bitfield_bit_size = 0;
9004 uint32_t field_bitfield_bit_offset = 0;
9005 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
9006 field_bitfield_bit_size))
9007 field_bitfield_bit_offset = field_bit_offset % 8;
9008
9009 if (show_types) {
9010 std::string field_type_name(field_type.getAsString());
9011 if (field_bitfield_bit_size > 0)
9012 s->Printf("(%s:%u) ", field_type_name.c_str(),
9013 field_bitfield_bit_size);
9014 else
9015 s->Printf("(%s) ", field_type_name.c_str());
9016 }
9017 // Print the member name and equal sign
9018 s->Printf("%s = ", field->getNameAsString().c_str());
9019
9020 // Dump the value of the member
9021 CompilerType field_clang_type(getASTContext(), field_type);
9022 field_clang_type.DumpValue(
9023 exe_ctx,
9024 s, // Stream to dump to
9025 field_clang_type
9026 .GetFormat(), // The format with which to display the member
9027 data, // Data buffer containing all bytes for this type
9028 data_byte_offset + field_byte_offset, // Offset into "data" where to
9029 // grab value from
9030 field_type_info.Width / 8, // Size of this type in bytes
9031 field_bitfield_bit_size, // Bitfield bit size
9032 field_bitfield_bit_offset, // Bitfield bit offset
9033 show_types, // Boolean indicating if we should show the variable
9034 // types
9035 show_summary, // Boolean indicating if we should show a summary for
9036 // the current type
9037 verbose, // Verbose output?
9038 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9039 // children
9040 }
9041
9042 // Indent the trailing squiggly bracket
9043 if (child_idx > 0)
9044 s->Printf("\n%*s}", depth, "");
9045 }
9046 return;
9047
9048 case clang::Type::Enum:
9049 if (GetCompleteType(type)) {
9050 const clang::EnumType *enutype =
9051 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9052 const clang::EnumDecl *enum_decl = enutype->getDecl();
9053 assert(enum_decl);
9054 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9055 lldb::offset_t offset = data_byte_offset;
9056 const int64_t enum_value = data.GetMaxU64Bitfield(
9057 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9058 for (enum_pos = enum_decl->enumerator_begin(),
9059 enum_end_pos = enum_decl->enumerator_end();
9060 enum_pos != enum_end_pos; ++enum_pos) {
9061 if (enum_pos->getInitVal() == enum_value) {
9062 s->Printf("%s", enum_pos->getNameAsString().c_str());
9063 return;
9064 }
9065 }
9066 // If we have gotten here we didn't get find the enumerator in the
9067 // enum decl, so just print the integer.
9068 s->Printf("%" PRIi64, enum_value);
9069 }
9070 return;
9071
9072 case clang::Type::ConstantArray: {
9073 const clang::ConstantArrayType *array =
9074 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9075 bool is_array_of_characters = false;
9076 clang::QualType element_qual_type = array->getElementType();
9077
9078 const clang::Type *canonical_type =
9079 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9080 if (canonical_type)
9081 is_array_of_characters = canonical_type->isCharType();
9082
9083 const uint64_t element_count = array->getSize().getLimitedValue();
9084
9085 clang::TypeInfo field_type_info =
9086 getASTContext()->getTypeInfo(element_qual_type);
9087
9088 uint32_t element_idx = 0;
9089 uint32_t element_offset = 0;
9090 uint64_t element_byte_size = field_type_info.Width / 8;
9091 uint32_t element_stride = element_byte_size;
9092
9093 if (is_array_of_characters) {
9094 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009095 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9096 element_byte_size, element_count, UINT32_MAX,
9097 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009098 s->PutChar('"');
9099 return;
9100 } else {
9101 CompilerType element_clang_type(getASTContext(), element_qual_type);
9102 lldb::Format element_format = element_clang_type.GetFormat();
9103
9104 for (element_idx = 0; element_idx < element_count; ++element_idx) {
9105 // Print the starting squiggly bracket (if this is the
9106 // first member) or comman (for member 2 and beyong) for
9107 // the struct/union/class member.
9108 if (element_idx == 0)
9109 s->PutChar('{');
9110 else
9111 s->PutChar(',');
9112
9113 // Indent and print the index
9114 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9115
9116 // Figure out the field offset within the current struct/union/class
9117 // type
9118 element_offset = element_idx * element_stride;
9119
9120 // Dump the value of the member
9121 element_clang_type.DumpValue(
9122 exe_ctx,
9123 s, // Stream to dump to
9124 element_format, // The format with which to display the element
9125 data, // Data buffer containing all bytes for this type
9126 data_byte_offset +
9127 element_offset, // Offset into "data" where to grab value from
9128 element_byte_size, // Size of this type in bytes
9129 0, // Bitfield bit size
9130 0, // Bitfield bit offset
9131 show_types, // Boolean indicating if we should show the variable
9132 // types
9133 show_summary, // Boolean indicating if we should show a summary for
9134 // the current type
9135 verbose, // Verbose output?
9136 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9137 // children
9138 }
9139
9140 // Indent the trailing squiggly bracket
9141 if (element_idx > 0)
9142 s->Printf("\n%*s}", depth, "");
9143 }
9144 }
9145 return;
9146
9147 case clang::Type::Typedef: {
9148 clang::QualType typedef_qual_type =
9149 llvm::cast<clang::TypedefType>(qual_type)
9150 ->getDecl()
9151 ->getUnderlyingType();
9152
9153 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9154 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9155 clang::TypeInfo typedef_type_info =
9156 getASTContext()->getTypeInfo(typedef_qual_type);
9157 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9158
9159 return typedef_clang_type.DumpValue(
9160 exe_ctx,
9161 s, // Stream to dump to
9162 typedef_format, // The format with which to display the element
9163 data, // Data buffer containing all bytes for this type
9164 data_byte_offset, // Offset into "data" where to grab value from
9165 typedef_byte_size, // Size of this type in bytes
9166 bitfield_bit_size, // Bitfield bit size
9167 bitfield_bit_offset, // Bitfield bit offset
9168 show_types, // Boolean indicating if we should show the variable types
9169 show_summary, // Boolean indicating if we should show a summary for the
9170 // current type
9171 verbose, // Verbose output?
9172 depth); // Scope depth for any types that have children
9173 } break;
9174
9175 case clang::Type::Auto: {
9176 clang::QualType elaborated_qual_type =
9177 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9178 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9179 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9180 clang::TypeInfo elaborated_type_info =
9181 getASTContext()->getTypeInfo(elaborated_qual_type);
9182 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9183
9184 return elaborated_clang_type.DumpValue(
9185 exe_ctx,
9186 s, // Stream to dump to
9187 elaborated_format, // The format with which to display the element
9188 data, // Data buffer containing all bytes for this type
9189 data_byte_offset, // Offset into "data" where to grab value from
9190 elaborated_byte_size, // Size of this type in bytes
9191 bitfield_bit_size, // Bitfield bit size
9192 bitfield_bit_offset, // Bitfield bit offset
9193 show_types, // Boolean indicating if we should show the variable types
9194 show_summary, // Boolean indicating if we should show a summary for the
9195 // current type
9196 verbose, // Verbose output?
9197 depth); // Scope depth for any types that have children
9198 } break;
9199
9200 case clang::Type::Elaborated: {
9201 clang::QualType elaborated_qual_type =
9202 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9203 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9204 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9205 clang::TypeInfo elaborated_type_info =
9206 getASTContext()->getTypeInfo(elaborated_qual_type);
9207 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9208
9209 return elaborated_clang_type.DumpValue(
9210 exe_ctx,
9211 s, // Stream to dump to
9212 elaborated_format, // The format with which to display the element
9213 data, // Data buffer containing all bytes for this type
9214 data_byte_offset, // Offset into "data" where to grab value from
9215 elaborated_byte_size, // Size of this type in bytes
9216 bitfield_bit_size, // Bitfield bit size
9217 bitfield_bit_offset, // Bitfield bit offset
9218 show_types, // Boolean indicating if we should show the variable types
9219 show_summary, // Boolean indicating if we should show a summary for the
9220 // current type
9221 verbose, // Verbose output?
9222 depth); // Scope depth for any types that have children
9223 } break;
9224
9225 case clang::Type::Paren: {
9226 clang::QualType desugar_qual_type =
9227 llvm::cast<clang::ParenType>(qual_type)->desugar();
9228 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9229
9230 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9231 clang::TypeInfo desugar_type_info =
9232 getASTContext()->getTypeInfo(desugar_qual_type);
9233 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9234
9235 return desugar_clang_type.DumpValue(
9236 exe_ctx,
9237 s, // Stream to dump to
9238 desugar_format, // The format with which to display the element
9239 data, // Data buffer containing all bytes for this type
9240 data_byte_offset, // Offset into "data" where to grab value from
9241 desugar_byte_size, // Size of this type in bytes
9242 bitfield_bit_size, // Bitfield bit size
9243 bitfield_bit_offset, // Bitfield bit offset
9244 show_types, // Boolean indicating if we should show the variable types
9245 show_summary, // Boolean indicating if we should show a summary for the
9246 // current type
9247 verbose, // Verbose output?
9248 depth); // Scope depth for any types that have children
9249 } break;
9250
9251 default:
9252 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009253 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9254 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9255 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009256
9257 if (show_summary)
9258 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9259 break;
9260 }
9261}
9262
9263bool ClangASTContext::DumpTypeValue(
9264 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009265 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9266 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009267 ExecutionContextScope *exe_scope) {
9268 if (!type)
9269 return false;
9270 if (IsAggregateType(type)) {
9271 return false;
9272 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009273 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009274
9275 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9276 switch (type_class) {
9277 case clang::Type::Typedef: {
9278 clang::QualType typedef_qual_type =
9279 llvm::cast<clang::TypedefType>(qual_type)
9280 ->getDecl()
9281 ->getUnderlyingType();
9282 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9283 if (format == eFormatDefault)
9284 format = typedef_clang_type.GetFormat();
9285 clang::TypeInfo typedef_type_info =
9286 getASTContext()->getTypeInfo(typedef_qual_type);
9287 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9288
9289 return typedef_clang_type.DumpTypeValue(
9290 s,
9291 format, // The format with which to display the element
9292 data, // Data buffer containing all bytes for this type
9293 byte_offset, // Offset into "data" where to grab value from
9294 typedef_byte_size, // Size of this type in bytes
9295 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9296 // treat as a bitfield
9297 bitfield_bit_offset, // Offset in bits of a bitfield value if
9298 // bitfield_bit_size != 0
9299 exe_scope);
9300 } break;
9301
9302 case clang::Type::Enum:
9303 // If our format is enum or default, show the enumeration value as
9304 // its enumeration string value, else just display it as requested.
9305 if ((format == eFormatEnum || format == eFormatDefault) &&
9306 GetCompleteType(type)) {
9307 const clang::EnumType *enutype =
9308 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9309 const clang::EnumDecl *enum_decl = enutype->getDecl();
9310 assert(enum_decl);
9311 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9312 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9313 lldb::offset_t offset = byte_offset;
9314 if (is_signed) {
9315 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9316 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9317 for (enum_pos = enum_decl->enumerator_begin(),
9318 enum_end_pos = enum_decl->enumerator_end();
9319 enum_pos != enum_end_pos; ++enum_pos) {
9320 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009321 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009322 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009323 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009324 }
9325 // If we have gotten here we didn't get find the enumerator in the
9326 // enum decl, so just print the integer.
9327 s->Printf("%" PRIi64, enum_svalue);
9328 } else {
9329 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9330 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9331 for (enum_pos = enum_decl->enumerator_begin(),
9332 enum_end_pos = enum_decl->enumerator_end();
9333 enum_pos != enum_end_pos; ++enum_pos) {
9334 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009335 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009336 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009337 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009338 }
9339 // If we have gotten here we didn't get find the enumerator in the
9340 // enum decl, so just print the integer.
9341 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009342 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009343 return true;
9344 }
9345 // format was not enum, just fall through and dump the value as
9346 // requested....
9347 LLVM_FALLTHROUGH;
9348
9349 default:
9350 // We are down to a scalar type that we just need to display.
9351 {
9352 uint32_t item_count = 1;
9353 // A few formats, we might need to modify our size and count for
9354 // depending
9355 // on how we are trying to display the value...
9356 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009357 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009358 case eFormatBoolean:
9359 case eFormatBinary:
9360 case eFormatComplex:
9361 case eFormatCString: // NULL terminated C strings
9362 case eFormatDecimal:
9363 case eFormatEnum:
9364 case eFormatHex:
9365 case eFormatHexUppercase:
9366 case eFormatFloat:
9367 case eFormatOctal:
9368 case eFormatOSType:
9369 case eFormatUnsigned:
9370 case eFormatPointer:
9371 case eFormatVectorOfChar:
9372 case eFormatVectorOfSInt8:
9373 case eFormatVectorOfUInt8:
9374 case eFormatVectorOfSInt16:
9375 case eFormatVectorOfUInt16:
9376 case eFormatVectorOfSInt32:
9377 case eFormatVectorOfUInt32:
9378 case eFormatVectorOfSInt64:
9379 case eFormatVectorOfUInt64:
9380 case eFormatVectorOfFloat32:
9381 case eFormatVectorOfFloat64:
9382 case eFormatVectorOfUInt128:
9383 break;
9384
9385 case eFormatChar:
9386 case eFormatCharPrintable:
9387 case eFormatCharArray:
9388 case eFormatBytes:
9389 case eFormatBytesWithASCII:
9390 item_count = byte_size;
9391 byte_size = 1;
9392 break;
9393
9394 case eFormatUnicode16:
9395 item_count = byte_size / 2;
9396 byte_size = 2;
9397 break;
9398
9399 case eFormatUnicode32:
9400 item_count = byte_size / 4;
9401 byte_size = 4;
9402 break;
9403 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009404 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9405 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9406 bitfield_bit_size, bitfield_bit_offset,
9407 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009408 }
9409 break;
9410 }
9411 }
9412 return 0;
9413}
9414
9415void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9416 ExecutionContext *exe_ctx, Stream *s,
9417 const lldb_private::DataExtractor &data,
9418 lldb::offset_t data_byte_offset,
9419 size_t data_byte_size) {
9420 uint32_t length = 0;
9421 if (IsCStringType(type, length)) {
9422 if (exe_ctx) {
9423 Process *process = exe_ctx->GetProcessPtr();
9424 if (process) {
9425 lldb::offset_t offset = data_byte_offset;
9426 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9427 std::vector<uint8_t> buf;
9428 if (length > 0)
9429 buf.resize(length);
9430 else
9431 buf.resize(256);
9432
Zachary Turner29cb8682017-03-03 20:57:05 +00009433 DataExtractor cstr_data(&buf.front(), buf.size(),
9434 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009435 buf.back() = '\0';
9436 size_t bytes_read;
9437 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009438 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009439 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9440 buf.size(), error)) > 0) {
9441 const size_t len = strlen((const char *)&buf.front());
9442 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009443 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009444 if (total_cstr_len == 0)
9445 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009446 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9447 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009448 total_cstr_len += len;
9449 if (len < buf.size())
9450 break;
9451 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009452 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009453 if (total_cstr_len > 0)
9454 s->PutChar('"');
9455 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009456 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009457 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009458}
9459
Kate Stoneb9c1b512016-09-06 20:57:50 +00009460void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9461 StreamFile s(stdout, false);
9462 DumpTypeDescription(type, &s);
9463 ClangASTMetadata *metadata =
9464 ClangASTContext::GetMetadata(getASTContext(), type);
9465 if (metadata) {
9466 metadata->Dump(&s);
9467 }
9468}
Greg Claytond8d4a572015-08-11 21:38:15 +00009469
Kate Stoneb9c1b512016-09-06 20:57:50 +00009470void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9471 Stream *s) {
9472 if (type) {
9473 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009474
Kate Stoneb9c1b512016-09-06 20:57:50 +00009475 llvm::SmallVector<char, 1024> buf;
9476 llvm::raw_svector_ostream llvm_ostrm(buf);
9477
9478 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9479 switch (type_class) {
9480 case clang::Type::ObjCObject:
9481 case clang::Type::ObjCInterface: {
9482 GetCompleteType(type);
9483
9484 const clang::ObjCObjectType *objc_class_type =
9485 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9486 assert(objc_class_type);
9487 if (objc_class_type) {
9488 clang::ObjCInterfaceDecl *class_interface_decl =
9489 objc_class_type->getInterface();
9490 if (class_interface_decl) {
9491 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9492 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009493 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009494 }
9495 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009496
Kate Stoneb9c1b512016-09-06 20:57:50 +00009497 case clang::Type::Typedef: {
9498 const clang::TypedefType *typedef_type =
9499 qual_type->getAs<clang::TypedefType>();
9500 if (typedef_type) {
9501 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9502 std::string clang_typedef_name(
9503 typedef_decl->getQualifiedNameAsString());
9504 if (!clang_typedef_name.empty()) {
9505 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009506 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009507 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009508 }
9509 } break;
9510
9511 case clang::Type::Auto:
9512 CompilerType(getASTContext(),
9513 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9514 .DumpTypeDescription(s);
9515 return;
9516
9517 case clang::Type::Elaborated:
9518 CompilerType(getASTContext(),
9519 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9520 .DumpTypeDescription(s);
9521 return;
9522
9523 case clang::Type::Paren:
9524 CompilerType(getASTContext(),
9525 llvm::cast<clang::ParenType>(qual_type)->desugar())
9526 .DumpTypeDescription(s);
9527 return;
9528
9529 case clang::Type::Record: {
9530 GetCompleteType(type);
9531
9532 const clang::RecordType *record_type =
9533 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9534 const clang::RecordDecl *record_decl = record_type->getDecl();
9535 const clang::CXXRecordDecl *cxx_record_decl =
9536 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9537
9538 if (cxx_record_decl)
9539 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9540 s->GetIndentLevel());
9541 else
9542 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9543 s->GetIndentLevel());
9544 } break;
9545
9546 default: {
9547 const clang::TagType *tag_type =
9548 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9549 if (tag_type) {
9550 clang::TagDecl *tag_decl = tag_type->getDecl();
9551 if (tag_decl)
9552 tag_decl->print(llvm_ostrm, 0);
9553 } else {
9554 std::string clang_type_name(qual_type.getAsString());
9555 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009556 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009557 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009558 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009559 }
9560
Kate Stoneb9c1b512016-09-06 20:57:50 +00009561 if (buf.size() > 0) {
9562 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009563 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009564 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009565}
9566
Kate Stoneb9c1b512016-09-06 20:57:50 +00009567void ClangASTContext::DumpTypeName(const CompilerType &type) {
9568 if (ClangUtil::IsClangType(type)) {
9569 clang::QualType qual_type(
9570 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9571
9572 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9573 switch (type_class) {
9574 case clang::Type::Record: {
9575 const clang::CXXRecordDecl *cxx_record_decl =
9576 qual_type->getAsCXXRecordDecl();
9577 if (cxx_record_decl)
9578 printf("class %s", cxx_record_decl->getName().str().c_str());
9579 } break;
9580
9581 case clang::Type::Enum: {
9582 clang::EnumDecl *enum_decl =
9583 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9584 if (enum_decl) {
9585 printf("enum %s", enum_decl->getName().str().c_str());
9586 }
9587 } break;
9588
9589 case clang::Type::ObjCObject:
9590 case clang::Type::ObjCInterface: {
9591 const clang::ObjCObjectType *objc_class_type =
9592 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9593 if (objc_class_type) {
9594 clang::ObjCInterfaceDecl *class_interface_decl =
9595 objc_class_type->getInterface();
9596 // We currently can't complete objective C types through the newly added
9597 // ASTContext
9598 // because it only supports TagDecl objects right now...
9599 if (class_interface_decl)
9600 printf("@class %s", class_interface_decl->getName().str().c_str());
9601 }
9602 } break;
9603
9604 case clang::Type::Typedef:
9605 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9606 ->getDecl()
9607 ->getName()
9608 .str()
9609 .c_str());
9610 break;
9611
9612 case clang::Type::Auto:
9613 printf("auto ");
9614 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9615 llvm::cast<clang::AutoType>(qual_type)
9616 ->getDeducedType()
9617 .getAsOpaquePtr()));
9618
9619 case clang::Type::Elaborated:
9620 printf("elaborated ");
9621 return DumpTypeName(CompilerType(
9622 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9623 ->getNamedType()
9624 .getAsOpaquePtr()));
9625
9626 case clang::Type::Paren:
9627 printf("paren ");
9628 return DumpTypeName(CompilerType(
9629 type.GetTypeSystem(),
9630 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9631
9632 default:
9633 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9634 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009635 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009636 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009637}
9638
Kate Stoneb9c1b512016-09-06 20:57:50 +00009639clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9640 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9641 const char *parent_name, int tag_decl_kind,
9642 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9643 if (template_param_infos.IsValid()) {
9644 std::string template_basename(parent_name);
9645 template_basename.erase(template_basename.find('<'));
9646
9647 return CreateClassTemplateDecl(decl_ctx, access_type,
9648 template_basename.c_str(), tag_decl_kind,
9649 template_param_infos);
9650 }
9651 return NULL;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009652}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009653
Kate Stoneb9c1b512016-09-06 20:57:50 +00009654void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9655 ClangASTContext *ast = (ClangASTContext *)baton;
9656 SymbolFile *sym_file = ast->GetSymbolFile();
9657 if (sym_file) {
9658 CompilerType clang_type = GetTypeForDecl(decl);
9659 if (clang_type)
9660 sym_file->CompleteType(clang_type);
9661 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009662}
9663
Kate Stoneb9c1b512016-09-06 20:57:50 +00009664void ClangASTContext::CompleteObjCInterfaceDecl(
9665 void *baton, clang::ObjCInterfaceDecl *decl) {
9666 ClangASTContext *ast = (ClangASTContext *)baton;
9667 SymbolFile *sym_file = ast->GetSymbolFile();
9668 if (sym_file) {
9669 CompilerType clang_type = GetTypeForDecl(decl);
9670 if (clang_type)
9671 sym_file->CompleteType(clang_type);
9672 }
Zachary Turner42dff792016-04-15 00:21:26 +00009673}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009674
Kate Stoneb9c1b512016-09-06 20:57:50 +00009675DWARFASTParser *ClangASTContext::GetDWARFParser() {
9676 if (!m_dwarf_ast_parser_ap)
9677 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9678 return m_dwarf_ast_parser_ap.get();
9679}
9680
9681PDBASTParser *ClangASTContext::GetPDBParser() {
9682 if (!m_pdb_ast_parser_ap)
9683 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9684 return m_pdb_ast_parser_ap.get();
9685}
9686
9687bool ClangASTContext::LayoutRecordType(
9688 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9689 uint64_t &alignment,
9690 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9691 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9692 &base_offsets,
9693 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9694 &vbase_offsets) {
9695 ClangASTContext *ast = (ClangASTContext *)baton;
9696 DWARFASTParserClang *dwarf_ast_parser =
9697 (DWARFASTParserClang *)ast->GetDWARFParser();
9698 return dwarf_ast_parser->GetClangASTImporter().LayoutRecordType(
9699 record_decl, bit_size, alignment, field_offsets, base_offsets,
9700 vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009701}
9702
Greg Clayton99558cc42015-08-24 23:46:31 +00009703//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009704// CompilerDecl override functions
9705//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009706
Kate Stoneb9c1b512016-09-06 20:57:50 +00009707ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9708 if (opaque_decl) {
9709 clang::NamedDecl *nd =
9710 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9711 if (nd != nullptr)
9712 return ConstString(nd->getDeclName().getAsString());
9713 }
9714 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009715}
9716
Kate Stoneb9c1b512016-09-06 20:57:50 +00009717ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9718 if (opaque_decl) {
9719 clang::NamedDecl *nd =
9720 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9721 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9722 clang::MangleContext *mc = getMangleContext();
9723 if (mc && mc->shouldMangleCXXName(nd)) {
9724 llvm::SmallVector<char, 1024> buf;
9725 llvm::raw_svector_ostream llvm_ostrm(buf);
9726 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9727 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9728 Ctor_Complete, llvm_ostrm);
9729 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9730 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9731 Dtor_Complete, llvm_ostrm);
9732 } else {
9733 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009734 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009735 if (buf.size() > 0)
9736 return ConstString(buf.data(), buf.size());
9737 }
Greg Claytonfe689042015-11-10 17:47:04 +00009738 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009739 }
9740 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009741}
9742
Kate Stoneb9c1b512016-09-06 20:57:50 +00009743CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9744 if (opaque_decl)
9745 return CompilerDeclContext(this,
9746 ((clang::Decl *)opaque_decl)->getDeclContext());
9747 else
9748 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009749}
9750
Kate Stoneb9c1b512016-09-06 20:57:50 +00009751CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9752 if (clang::FunctionDecl *func_decl =
9753 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9754 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9755 if (clang::ObjCMethodDecl *objc_method =
9756 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9757 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9758 else
Greg Claytonfe689042015-11-10 17:47:04 +00009759 return CompilerType();
9760}
9761
Kate Stoneb9c1b512016-09-06 20:57:50 +00009762size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9763 if (clang::FunctionDecl *func_decl =
9764 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9765 return func_decl->param_size();
9766 if (clang::ObjCMethodDecl *objc_method =
9767 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9768 return objc_method->param_size();
9769 else
9770 return 0;
9771}
9772
9773CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9774 size_t idx) {
9775 if (clang::FunctionDecl *func_decl =
9776 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9777 if (idx < func_decl->param_size()) {
9778 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9779 if (var_decl)
9780 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9781 }
9782 } else if (clang::ObjCMethodDecl *objc_method =
9783 llvm::dyn_cast<clang::ObjCMethodDecl>(
9784 (clang::Decl *)opaque_decl)) {
9785 if (idx < objc_method->param_size())
9786 return CompilerType(
9787 this,
9788 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9789 }
9790 return CompilerType();
9791}
9792
Paul Hermand628cbb2015-09-15 23:44:17 +00009793//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00009794// CompilerDeclContext functions
9795//----------------------------------------------------------------------
9796
Kate Stoneb9c1b512016-09-06 20:57:50 +00009797std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9798 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9799 std::vector<CompilerDecl> found_decls;
9800 if (opaque_decl_ctx) {
9801 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9802 std::set<DeclContext *> searched;
9803 std::multimap<DeclContext *, DeclContext *> search_queue;
9804 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009805
Kate Stoneb9c1b512016-09-06 20:57:50 +00009806 for (clang::DeclContext *decl_context = root_decl_ctx;
9807 decl_context != nullptr && found_decls.empty();
9808 decl_context = decl_context->getParent()) {
9809 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +00009810
Kate Stoneb9c1b512016-09-06 20:57:50 +00009811 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9812 it++) {
9813 if (!searched.insert(it->second).second)
9814 continue;
9815 symbol_file->ParseDeclsForContext(
9816 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +00009817
Kate Stoneb9c1b512016-09-06 20:57:50 +00009818 for (clang::Decl *child : it->second->decls()) {
9819 if (clang::UsingDirectiveDecl *ud =
9820 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9821 if (ignore_using_decls)
9822 continue;
9823 clang::DeclContext *from = ud->getCommonAncestor();
9824 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9825 search_queue.insert(
9826 std::make_pair(from, ud->getNominatedNamespace()));
9827 } else if (clang::UsingDecl *ud =
9828 llvm::dyn_cast<clang::UsingDecl>(child)) {
9829 if (ignore_using_decls)
9830 continue;
9831 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9832 clang::Decl *target = usd->getTargetDecl();
9833 if (clang::NamedDecl *nd =
9834 llvm::dyn_cast<clang::NamedDecl>(target)) {
9835 IdentifierInfo *ii = nd->getIdentifier();
9836 if (ii != nullptr &&
9837 ii->getName().equals(name.AsCString(nullptr)))
9838 found_decls.push_back(CompilerDecl(this, nd));
9839 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009840 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009841 } else if (clang::NamedDecl *nd =
9842 llvm::dyn_cast<clang::NamedDecl>(child)) {
9843 IdentifierInfo *ii = nd->getIdentifier();
9844 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9845 found_decls.push_back(CompilerDecl(this, nd));
9846 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009847 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009848 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009849 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009850 }
9851 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +00009852}
9853
Dawn Perchikb5925782015-12-12 19:31:41 +00009854// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009855// and return the number of levels it took to find it, or
9856// LLDB_INVALID_DECL_LEVEL
9857// if not found. If the decl was imported via a using declaration, its name
9858// and/or
9859// type, if set, will be used to check that the decl found in the scope is a
9860// match.
Dawn Perchikb5925782015-12-12 19:31:41 +00009861//
Kate Stoneb9c1b512016-09-06 20:57:50 +00009862// The optional name is required by languages (like C++) to handle using
9863// declarations
Dawn Perchikb5925782015-12-12 19:31:41 +00009864// like:
9865//
9866// void poo();
9867// namespace ns {
9868// void foo();
9869// void goo();
9870// }
9871// void bar() {
9872// using ns::foo;
9873// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
9874// // LLDB_INVALID_DECL_LEVEL for 'goo'.
9875// }
9876//
9877// The optional type is useful in the case that there's a specific overload
9878// that we're looking for that might otherwise be shadowed, like:
9879//
9880// void foo(int);
9881// namespace ns {
9882// void foo();
9883// }
9884// void bar() {
9885// using ns::foo;
9886// // CountDeclLevels returns 0 for { 'foo', void() },
9887// // 1 for { 'foo', void(int) }, and
9888// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
9889// }
9890//
9891// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +00009892// function at file scope will return the same level as a function at global
9893// scope.
9894// Ideally we'd like to treat the file scope as an additional scope just below
9895// the
9896// global scope. More work needs to be done to recognise that, if the decl
9897// we're
9898// trying to look up is static, we should compare its source file with that of
9899// the
Dawn Perchikb5925782015-12-12 19:31:41 +00009900// current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009901uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
9902 clang::DeclContext *child_decl_ctx,
9903 ConstString *child_name,
9904 CompilerType *child_type) {
9905 if (frame_decl_ctx) {
9906 std::set<DeclContext *> searched;
9907 std::multimap<DeclContext *, DeclContext *> search_queue;
9908 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +00009909
Kate Stoneb9c1b512016-09-06 20:57:50 +00009910 // Get the lookup scope for the decl we're trying to find.
9911 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +00009912
Kate Stoneb9c1b512016-09-06 20:57:50 +00009913 // Look for it in our scope's decl context and its parents.
9914 uint32_t level = 0;
9915 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
9916 decl_ctx = decl_ctx->getParent()) {
9917 if (!decl_ctx->isLookupContext())
9918 continue;
9919 if (decl_ctx == parent_decl_ctx)
9920 // Found it!
9921 return level;
9922 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
9923 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
9924 it++) {
9925 if (searched.find(it->second) != searched.end())
9926 continue;
9927
9928 // Currently DWARF has one shared translation unit for all Decls at top
9929 // level, so this
9930 // would erroneously find using statements anywhere. So don't look at
9931 // the top-level
9932 // translation unit.
9933 // TODO fix this and add a testcase that depends on it.
9934
9935 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
9936 continue;
9937
9938 searched.insert(it->second);
9939 symbol_file->ParseDeclsForContext(
9940 CompilerDeclContext(this, it->second));
9941
9942 for (clang::Decl *child : it->second->decls()) {
9943 if (clang::UsingDirectiveDecl *ud =
9944 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9945 clang::DeclContext *ns = ud->getNominatedNamespace();
9946 if (ns == parent_decl_ctx)
9947 // Found it!
9948 return level;
9949 clang::DeclContext *from = ud->getCommonAncestor();
9950 if (searched.find(ns) == searched.end())
9951 search_queue.insert(std::make_pair(from, ns));
9952 } else if (child_name) {
9953 if (clang::UsingDecl *ud =
9954 llvm::dyn_cast<clang::UsingDecl>(child)) {
9955 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9956 clang::Decl *target = usd->getTargetDecl();
9957 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
9958 if (!nd)
9959 continue;
9960 // Check names.
9961 IdentifierInfo *ii = nd->getIdentifier();
9962 if (ii == nullptr ||
9963 !ii->getName().equals(child_name->AsCString(nullptr)))
9964 continue;
9965 // Check types, if one was provided.
9966 if (child_type) {
9967 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
9968 if (!AreTypesSame(clang_type, *child_type,
9969 /*ignore_qualifiers=*/true))
9970 continue;
9971 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009972 // Found it!
9973 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009974 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009975 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009976 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009977 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009978 }
9979 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +00009980 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009981 }
9982 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +00009983}
9984
Kate Stoneb9c1b512016-09-06 20:57:50 +00009985bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
9986 if (opaque_decl_ctx)
9987 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
9988 else
Greg Clayton99558cc42015-08-24 23:46:31 +00009989 return false;
9990}
9991
Kate Stoneb9c1b512016-09-06 20:57:50 +00009992ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
9993 if (opaque_decl_ctx) {
9994 clang::NamedDecl *named_decl =
9995 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9996 if (named_decl)
9997 return ConstString(named_decl->getName());
9998 }
9999 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +000010000}
10001
Kate Stoneb9c1b512016-09-06 20:57:50 +000010002ConstString
10003ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
10004 if (opaque_decl_ctx) {
10005 clang::NamedDecl *named_decl =
10006 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10007 if (named_decl)
10008 return ConstString(
10009 llvm::StringRef(named_decl->getQualifiedNameAsString()));
10010 }
10011 return ConstString();
10012}
10013
10014bool ClangASTContext::DeclContextIsClassMethod(
10015 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
10016 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
10017 if (opaque_decl_ctx) {
10018 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10019 if (ObjCMethodDecl *objc_method =
10020 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
10021 if (is_instance_method_ptr)
10022 *is_instance_method_ptr = objc_method->isInstanceMethod();
10023 if (language_ptr)
10024 *language_ptr = eLanguageTypeObjC;
10025 if (language_object_name_ptr)
10026 language_object_name_ptr->SetCString("self");
10027 return true;
10028 } else if (CXXMethodDecl *cxx_method =
10029 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
10030 if (is_instance_method_ptr)
10031 *is_instance_method_ptr = cxx_method->isInstance();
10032 if (language_ptr)
10033 *language_ptr = eLanguageTypeC_plus_plus;
10034 if (language_object_name_ptr)
10035 language_object_name_ptr->SetCString("this");
10036 return true;
10037 } else if (clang::FunctionDecl *function_decl =
10038 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
10039 ClangASTMetadata *metadata =
10040 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
10041 if (metadata && metadata->HasObjectPtr()) {
10042 if (is_instance_method_ptr)
10043 *is_instance_method_ptr = true;
10044 if (language_ptr)
10045 *language_ptr = eLanguageTypeObjC;
10046 if (language_object_name_ptr)
10047 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10048 return true;
10049 }
10050 }
10051 }
10052 return false;
10053}
10054
10055clang::DeclContext *
10056ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10057 if (dc.IsClang())
10058 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10059 return nullptr;
10060}
Greg Clayton99558cc42015-08-24 23:46:31 +000010061
10062ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010063ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10064 if (dc.IsClang())
10065 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10066 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10067 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010068}
10069
10070CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010071ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10072 if (dc.IsClang())
10073 return llvm::dyn_cast<clang::CXXMethodDecl>(
10074 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10075 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010076}
10077
10078clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010079ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10080 if (dc.IsClang())
10081 return llvm::dyn_cast<clang::FunctionDecl>(
10082 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10083 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010084}
10085
10086clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010087ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10088 if (dc.IsClang())
10089 return llvm::dyn_cast<clang::NamespaceDecl>(
10090 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10091 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010092}
10093
10094ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010095ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10096 const void *object) {
10097 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10098 if (ast)
10099 return ClangASTContext::GetMetadata(ast, object);
10100 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010101}
10102
10103clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010104ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10105 ClangASTContext *ast =
10106 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10107 if (ast)
10108 return ast->getASTContext();
10109 return nullptr;
10110}
10111
10112ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10113 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10114 m_target_wp(target.shared_from_this()),
10115 m_persistent_variables(new ClangPersistentVariables) {}
10116
10117UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010118 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010119 Expression::ResultType desired_type,
10120 const EvaluateExpressionOptions &options) {
10121 TargetSP target_sp = m_target_wp.lock();
10122 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010123 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010124
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010125 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010126 desired_type, options);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010127}
10128
Kate Stoneb9c1b512016-09-06 20:57:50 +000010129FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10130 const CompilerType &return_type, const Address &function_address,
10131 const ValueList &arg_value_list, const char *name) {
10132 TargetSP target_sp = m_target_wp.lock();
10133 if (!target_sp)
10134 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010135
Kate Stoneb9c1b512016-09-06 20:57:50 +000010136 Process *process = target_sp->GetProcessSP().get();
10137 if (!process)
10138 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010139
Kate Stoneb9c1b512016-09-06 20:57:50 +000010140 return new ClangFunctionCaller(*process, return_type, function_address,
10141 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010142}
10143
10144UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010145ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10146 const char *name) {
10147 TargetSP target_sp = m_target_wp.lock();
10148 if (!target_sp)
10149 return nullptr;
10150
10151 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010152}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010153
10154PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010155ClangASTContextForExpressions::GetPersistentExpressionState() {
10156 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010157}
Sean Callanan68e44232017-09-28 20:20:25 +000010158
10159clang::ExternalASTMerger &
10160ClangASTContextForExpressions::GetMergerUnchecked() {
Eugene Zemtsova9d928c2017-09-29 03:15:08 +000010161 lldbassert(m_scratch_ast_source_ap != nullptr);
Sean Callanan68e44232017-09-28 20:20:25 +000010162 return m_scratch_ast_source_ap->GetMergerUnchecked();
10163}
10164