blob: 86f1055f93b5786f98cdafe766c447e8e8b2b006 [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,
2172 const CompilerType &integer_clang_type) {
2173 // TODO: Do something intelligent with the Declaration object passed in
2174 // like maybe filling in the SourceLocation with it...
2175 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002176
Kate Stoneb9c1b512016-09-06 20:57:50 +00002177 // TODO: ask about these...
2178 // const bool IsScoped = false;
2179 // 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,
2184 false, // IsScoped
2185 false, // IsScopedUsingClassTag
2186 false); // IsFixed
2187
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
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007552CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00007553ClangASTContext::GetTemplateArgument(lldb::opaque_compiler_type_t type,
7554 size_t arg_idx,
7555 lldb::TemplateArgumentKind &kind) {
7556 if (!type)
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007557 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00007558
7559 clang::QualType qual_type(GetCanonicalQualType(type));
7560 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7561 switch (type_class) {
7562 case clang::Type::Record:
7563 if (GetCompleteType(type)) {
7564 const clang::CXXRecordDecl *cxx_record_decl =
7565 qual_type->getAsCXXRecordDecl();
7566 if (cxx_record_decl) {
7567 const clang::ClassTemplateSpecializationDecl *template_decl =
7568 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7569 cxx_record_decl);
7570 if (template_decl &&
7571 arg_idx < template_decl->getTemplateArgs().size()) {
7572 const clang::TemplateArgument &template_arg =
7573 template_decl->getTemplateArgs()[arg_idx];
7574 switch (template_arg.getKind()) {
7575 case clang::TemplateArgument::Null:
7576 kind = eTemplateArgumentKindNull;
7577 return CompilerType();
7578
7579 case clang::TemplateArgument::Type:
7580 kind = eTemplateArgumentKindType;
7581 return CompilerType(getASTContext(), template_arg.getAsType());
7582
7583 case clang::TemplateArgument::Declaration:
7584 kind = eTemplateArgumentKindDeclaration;
7585 return CompilerType();
7586
7587 case clang::TemplateArgument::Integral:
7588 kind = eTemplateArgumentKindIntegral;
7589 return CompilerType(getASTContext(),
7590 template_arg.getIntegralType());
7591
7592 case clang::TemplateArgument::Template:
7593 kind = eTemplateArgumentKindTemplate;
7594 return CompilerType();
7595
7596 case clang::TemplateArgument::TemplateExpansion:
7597 kind = eTemplateArgumentKindTemplateExpansion;
7598 return CompilerType();
7599
7600 case clang::TemplateArgument::Expression:
7601 kind = eTemplateArgumentKindExpression;
7602 return CompilerType();
7603
7604 case clang::TemplateArgument::Pack:
7605 kind = eTemplateArgumentKindPack;
7606 return CompilerType();
7607
7608 default:
David Blaikiea322f362017-01-06 00:38:06 +00007609 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
Kate Stoneb9c1b512016-09-06 20:57:50 +00007610 }
7611 }
7612 }
7613 }
7614 break;
7615
7616 case clang::Type::Typedef:
7617 return (CompilerType(getASTContext(),
7618 llvm::cast<clang::TypedefType>(qual_type)
7619 ->getDecl()
7620 ->getUnderlyingType()))
7621 .GetTemplateArgument(arg_idx, kind);
7622
7623 case clang::Type::Auto:
7624 return (CompilerType(
7625 getASTContext(),
7626 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7627 .GetTemplateArgument(arg_idx, kind);
7628
7629 case clang::Type::Elaborated:
7630 return (CompilerType(
7631 getASTContext(),
7632 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7633 .GetTemplateArgument(arg_idx, kind);
7634
7635 case clang::Type::Paren:
7636 return (CompilerType(getASTContext(),
7637 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7638 .GetTemplateArgument(arg_idx, kind);
7639
7640 default:
7641 break;
7642 }
7643 kind = eTemplateArgumentKindNull;
7644 return CompilerType();
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007645}
7646
Kate Stoneb9c1b512016-09-06 20:57:50 +00007647CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7648 if (type)
7649 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7650 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007651}
7652
Kate Stoneb9c1b512016-09-06 20:57:50 +00007653clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7654 const clang::EnumType *enutype =
7655 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7656 if (enutype)
7657 return enutype->getDecl();
7658 return NULL;
7659}
7660
7661clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7662 const clang::RecordType *record_type =
7663 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7664 if (record_type)
7665 return record_type->getDecl();
7666 return nullptr;
7667}
7668
7669clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
7670 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
7671 if (qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00007672 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007673 else
7674 return qual_type->getAsTagDecl();
Greg Claytone6b36cd2015-12-08 01:02:08 +00007675}
7676
Greg Claytond8d4a572015-08-11 21:38:15 +00007677clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007678ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7679 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007680}
7681
7682clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007683ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7684 const clang::ObjCObjectType *objc_class_type =
7685 llvm::dyn_cast<clang::ObjCObjectType>(
7686 ClangUtil::GetCanonicalQualType(type));
7687 if (objc_class_type)
7688 return objc_class_type->getInterface();
7689 return nullptr;
7690}
7691
7692clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
7693 const CompilerType &type, const char *name,
7694 const CompilerType &field_clang_type, AccessType access,
7695 uint32_t bitfield_bit_size) {
7696 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007697 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007698 ClangASTContext *ast =
7699 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7700 if (!ast)
7701 return nullptr;
7702 clang::ASTContext *clang_ast = ast->getASTContext();
7703
7704 clang::FieldDecl *field = nullptr;
7705
7706 clang::Expr *bit_width = nullptr;
7707 if (bitfield_bit_size != 0) {
7708 llvm::APInt bitfield_bit_size_apint(
7709 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7710 bit_width = new (*clang_ast)
7711 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7712 clang_ast->IntTy, clang::SourceLocation());
7713 }
7714
7715 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7716 if (record_decl) {
7717 field = clang::FieldDecl::Create(
7718 *clang_ast, record_decl, clang::SourceLocation(),
7719 clang::SourceLocation(),
7720 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7721 ClangUtil::GetQualType(field_clang_type), // Field type
7722 nullptr, // TInfo *
7723 bit_width, // BitWidth
7724 false, // Mutable
7725 clang::ICIS_NoInit); // HasInit
7726
7727 if (!name) {
7728 // Determine whether this field corresponds to an anonymous
7729 // struct or union.
7730 if (const clang::TagType *TagT =
7731 field->getType()->getAs<clang::TagType>()) {
7732 if (clang::RecordDecl *Rec =
7733 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7734 if (!Rec->getDeclName()) {
7735 Rec->setAnonymousStructOrUnion(true);
7736 field->setImplicit();
7737 }
7738 }
7739 }
7740
7741 if (field) {
7742 field->setAccess(
7743 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7744
7745 record_decl->addDecl(field);
7746
7747#ifdef LLDB_CONFIGURATION_DEBUG
7748 VerifyDecl(field);
7749#endif
7750 }
7751 } else {
7752 clang::ObjCInterfaceDecl *class_interface_decl =
7753 ast->GetAsObjCInterfaceDecl(type);
7754
7755 if (class_interface_decl) {
7756 const bool is_synthesized = false;
7757
7758 field_clang_type.GetCompleteType();
7759
7760 field = clang::ObjCIvarDecl::Create(
7761 *clang_ast, class_interface_decl, clang::SourceLocation(),
7762 clang::SourceLocation(),
7763 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7764 ClangUtil::GetQualType(field_clang_type), // Field type
7765 nullptr, // TypeSourceInfo *
7766 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7767 is_synthesized);
7768
7769 if (field) {
7770 class_interface_decl->addDecl(field);
7771
7772#ifdef LLDB_CONFIGURATION_DEBUG
7773 VerifyDecl(field);
7774#endif
7775 }
7776 }
7777 }
7778 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007779}
7780
Kate Stoneb9c1b512016-09-06 20:57:50 +00007781void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7782 if (!type)
7783 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007784
Kate Stoneb9c1b512016-09-06 20:57:50 +00007785 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7786 if (!ast)
7787 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007788
Kate Stoneb9c1b512016-09-06 20:57:50 +00007789 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007790
Kate Stoneb9c1b512016-09-06 20:57:50 +00007791 if (!record_decl)
7792 return;
7793
7794 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7795
7796 IndirectFieldVector indirect_fields;
7797 clang::RecordDecl::field_iterator field_pos;
7798 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7799 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7800 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7801 last_field_pos = field_pos++) {
7802 if (field_pos->isAnonymousStructOrUnion()) {
7803 clang::QualType field_qual_type = field_pos->getType();
7804
7805 const clang::RecordType *field_record_type =
7806 field_qual_type->getAs<clang::RecordType>();
7807
7808 if (!field_record_type)
7809 continue;
7810
7811 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7812
7813 if (!field_record_decl)
7814 continue;
7815
7816 for (clang::RecordDecl::decl_iterator
7817 di = field_record_decl->decls_begin(),
7818 de = field_record_decl->decls_end();
7819 di != de; ++di) {
7820 if (clang::FieldDecl *nested_field_decl =
7821 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7822 clang::NamedDecl **chain =
7823 new (*ast->getASTContext()) clang::NamedDecl *[2];
7824 chain[0] = *field_pos;
7825 chain[1] = nested_field_decl;
7826 clang::IndirectFieldDecl *indirect_field =
7827 clang::IndirectFieldDecl::Create(
7828 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7829 nested_field_decl->getIdentifier(),
7830 nested_field_decl->getType(), {chain, 2});
7831
7832 indirect_field->setImplicit();
7833
7834 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7835 field_pos->getAccess(), nested_field_decl->getAccess()));
7836
7837 indirect_fields.push_back(indirect_field);
7838 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7839 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7840 size_t nested_chain_size =
7841 nested_indirect_field_decl->getChainingSize();
7842 clang::NamedDecl **chain = new (*ast->getASTContext())
7843 clang::NamedDecl *[nested_chain_size + 1];
7844 chain[0] = *field_pos;
7845
7846 int chain_index = 1;
7847 for (clang::IndirectFieldDecl::chain_iterator
7848 nci = nested_indirect_field_decl->chain_begin(),
7849 nce = nested_indirect_field_decl->chain_end();
7850 nci < nce; ++nci) {
7851 chain[chain_index] = *nci;
7852 chain_index++;
7853 }
7854
7855 clang::IndirectFieldDecl *indirect_field =
7856 clang::IndirectFieldDecl::Create(
7857 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7858 nested_indirect_field_decl->getIdentifier(),
7859 nested_indirect_field_decl->getType(),
7860 {chain, nested_chain_size + 1});
7861
7862 indirect_field->setImplicit();
7863
7864 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7865 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
7866
7867 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00007868 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007869 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007870 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007871 }
7872
7873 // Check the last field to see if it has an incomplete array type as its
7874 // last member and if it does, the tell the record decl about it
7875 if (last_field_pos != field_end_pos) {
7876 if (last_field_pos->getType()->isIncompleteArrayType())
7877 record_decl->hasFlexibleArrayMember();
7878 }
7879
7880 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
7881 ife = indirect_fields.end();
7882 ifi < ife; ++ifi) {
7883 record_decl->addDecl(*ifi);
7884 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007885}
7886
Kate Stoneb9c1b512016-09-06 20:57:50 +00007887void ClangASTContext::SetIsPacked(const CompilerType &type) {
7888 if (type) {
7889 ClangASTContext *ast =
7890 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7891 if (ast) {
7892 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
7893
7894 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00007895 return;
7896
Kate Stoneb9c1b512016-09-06 20:57:50 +00007897 record_decl->addAttr(
7898 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00007899 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007900 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007901}
7902
Kate Stoneb9c1b512016-09-06 20:57:50 +00007903clang::VarDecl *ClangASTContext::AddVariableToRecordType(
7904 const CompilerType &type, const char *name, const CompilerType &var_type,
7905 AccessType access) {
7906 clang::VarDecl *var_decl = nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007907
Kate Stoneb9c1b512016-09-06 20:57:50 +00007908 if (!type.IsValid() || !var_type.IsValid())
7909 return nullptr;
7910 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7911 if (!ast)
7912 return nullptr;
7913
7914 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7915 if (record_decl) {
7916 var_decl = clang::VarDecl::Create(
7917 *ast->getASTContext(), // ASTContext &
7918 record_decl, // DeclContext *
7919 clang::SourceLocation(), // clang::SourceLocation StartLoc
7920 clang::SourceLocation(), // clang::SourceLocation IdLoc
7921 name ? &ast->getASTContext()->Idents.get(name)
7922 : nullptr, // clang::IdentifierInfo *
7923 ClangUtil::GetQualType(var_type), // Variable clang::QualType
7924 nullptr, // TypeSourceInfo *
7925 clang::SC_Static); // StorageClass
7926 if (var_decl) {
7927 var_decl->setAccess(
7928 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7929 record_decl->addDecl(var_decl);
7930
Greg Claytond8d4a572015-08-11 21:38:15 +00007931#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00007932 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00007933#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00007934 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007935 }
7936 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00007937}
7938
Kate Stoneb9c1b512016-09-06 20:57:50 +00007939clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
7940 lldb::opaque_compiler_type_t type, const char *name,
7941 const CompilerType &method_clang_type, lldb::AccessType access,
7942 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
7943 bool is_attr_used, bool is_artificial) {
7944 if (!type || !method_clang_type.IsValid() || name == nullptr ||
7945 name[0] == '\0')
7946 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007947
Kate Stoneb9c1b512016-09-06 20:57:50 +00007948 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007949
Kate Stoneb9c1b512016-09-06 20:57:50 +00007950 clang::CXXRecordDecl *cxx_record_decl =
7951 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007952
Kate Stoneb9c1b512016-09-06 20:57:50 +00007953 if (cxx_record_decl == nullptr)
7954 return nullptr;
7955
7956 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
7957
7958 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7959
7960 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
7961
7962 const clang::FunctionType *function_type =
7963 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7964
7965 if (function_type == nullptr)
7966 return nullptr;
7967
7968 const clang::FunctionProtoType *method_function_prototype(
7969 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7970
7971 if (!method_function_prototype)
7972 return nullptr;
7973
7974 unsigned int num_params = method_function_prototype->getNumParams();
7975
7976 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7977 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7978
7979 if (is_artificial)
7980 return nullptr; // skip everything artificial
7981
7982 if (name[0] == '~') {
7983 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
7984 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7985 clang::DeclarationNameInfo(
7986 getASTContext()->DeclarationNames.getCXXDestructorName(
7987 getASTContext()->getCanonicalType(record_qual_type)),
7988 clang::SourceLocation()),
7989 method_qual_type, nullptr, is_inline, is_artificial);
7990 cxx_method_decl = cxx_dtor_decl;
7991 } else if (decl_name == cxx_record_decl->getDeclName()) {
7992 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
7993 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7994 clang::DeclarationNameInfo(
7995 getASTContext()->DeclarationNames.getCXXConstructorName(
7996 getASTContext()->getCanonicalType(record_qual_type)),
7997 clang::SourceLocation()),
7998 method_qual_type,
7999 nullptr, // TypeSourceInfo *
8000 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
8001 cxx_method_decl = cxx_ctor_decl;
8002 } else {
8003 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8004 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8005
8006 if (IsOperator(name, op_kind)) {
8007 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
8008 // Check the number of operator parameters. Sometimes we have
8009 // seen bad DWARF that doesn't correctly describe operators and
8010 // if we try to create a method and add it to the class, clang
8011 // will assert and crash, so we need to make sure things are
8012 // acceptable.
8013 const bool is_method = true;
8014 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8015 is_method, op_kind, num_params))
8016 return nullptr;
8017 cxx_method_decl = clang::CXXMethodDecl::Create(
8018 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8019 clang::DeclarationNameInfo(
8020 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8021 clang::SourceLocation()),
8022 method_qual_type,
8023 nullptr, // TypeSourceInfo *
8024 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8025 } else if (num_params == 0) {
8026 // Conversion operators don't take params...
8027 cxx_method_decl = clang::CXXConversionDecl::Create(
8028 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8029 clang::DeclarationNameInfo(
8030 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8031 getASTContext()->getCanonicalType(
8032 function_type->getReturnType())),
8033 clang::SourceLocation()),
8034 method_qual_type,
8035 nullptr, // TypeSourceInfo *
8036 is_inline, is_explicit, false /*is_constexpr*/,
8037 clang::SourceLocation());
8038 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008039 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008040
8041 if (cxx_method_decl == nullptr) {
8042 cxx_method_decl = clang::CXXMethodDecl::Create(
8043 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8044 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8045 method_qual_type,
8046 nullptr, // TypeSourceInfo *
8047 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008048 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008049 }
8050
8051 clang::AccessSpecifier access_specifier =
8052 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8053
8054 cxx_method_decl->setAccess(access_specifier);
8055 cxx_method_decl->setVirtualAsWritten(is_virtual);
8056
8057 if (is_attr_used)
8058 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8059
8060 // Populate the method decl with parameter decls
8061
8062 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8063
8064 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8065 params.push_back(clang::ParmVarDecl::Create(
8066 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8067 clang::SourceLocation(),
8068 nullptr, // anonymous
8069 method_function_prototype->getParamType(param_index), nullptr,
8070 clang::SC_None, nullptr));
8071 }
8072
8073 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8074
8075 cxx_record_decl->addDecl(cxx_method_decl);
8076
8077 // Sometimes the debug info will mention a constructor (default/copy/move),
8078 // destructor, or assignment operator (copy/move) but there won't be any
8079 // version of this in the code. So we check if the function was artificially
8080 // generated and if it is trivial and this lets the compiler/backend know
8081 // that it can inline the IR for these when it needs to and we can avoid a
8082 // "missing function" error when running expressions.
8083
8084 if (is_artificial) {
8085 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8086 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8087 (cxx_ctor_decl->isCopyConstructor() &&
8088 cxx_record_decl->hasTrivialCopyConstructor()) ||
8089 (cxx_ctor_decl->isMoveConstructor() &&
8090 cxx_record_decl->hasTrivialMoveConstructor()))) {
8091 cxx_ctor_decl->setDefaulted();
8092 cxx_ctor_decl->setTrivial(true);
8093 } else if (cxx_dtor_decl) {
8094 if (cxx_record_decl->hasTrivialDestructor()) {
8095 cxx_dtor_decl->setDefaulted();
8096 cxx_dtor_decl->setTrivial(true);
8097 }
8098 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8099 cxx_record_decl->hasTrivialCopyAssignment()) ||
8100 (cxx_method_decl->isMoveAssignmentOperator() &&
8101 cxx_record_decl->hasTrivialMoveAssignment())) {
8102 cxx_method_decl->setDefaulted();
8103 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008104 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008105 }
8106
Greg Claytond8d4a572015-08-11 21:38:15 +00008107#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008108 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008109#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008110
Kate Stoneb9c1b512016-09-06 20:57:50 +00008111 // printf ("decl->isPolymorphic() = %i\n",
8112 // cxx_record_decl->isPolymorphic());
8113 // printf ("decl->isAggregate() = %i\n",
8114 // cxx_record_decl->isAggregate());
8115 // printf ("decl->isPOD() = %i\n",
8116 // cxx_record_decl->isPOD());
8117 // printf ("decl->isEmpty() = %i\n",
8118 // cxx_record_decl->isEmpty());
8119 // printf ("decl->isAbstract() = %i\n",
8120 // cxx_record_decl->isAbstract());
8121 // printf ("decl->hasTrivialConstructor() = %i\n",
8122 // cxx_record_decl->hasTrivialConstructor());
8123 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8124 // cxx_record_decl->hasTrivialCopyConstructor());
8125 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8126 // cxx_record_decl->hasTrivialCopyAssignment());
8127 // printf ("decl->hasTrivialDestructor() = %i\n",
8128 // cxx_record_decl->hasTrivialDestructor());
8129 return cxx_method_decl;
8130}
Greg Claytond8d4a572015-08-11 21:38:15 +00008131
8132#pragma mark C++ Base Classes
8133
8134clang::CXXBaseSpecifier *
Kate Stoneb9c1b512016-09-06 20:57:50 +00008135ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8136 AccessType access, bool is_virtual,
8137 bool base_of_class) {
8138 if (type)
8139 return new clang::CXXBaseSpecifier(
8140 clang::SourceRange(), is_virtual, base_of_class,
8141 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8142 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8143 clang::SourceLocation());
8144 return nullptr;
8145}
8146
8147void ClangASTContext::DeleteBaseClassSpecifiers(
8148 clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes) {
8149 for (unsigned i = 0; i < num_base_classes; ++i) {
8150 delete base_classes[i];
8151 base_classes[i] = nullptr;
8152 }
8153}
8154
8155bool ClangASTContext::SetBaseClassesForClassType(
8156 lldb::opaque_compiler_type_t type,
8157 clang::CXXBaseSpecifier const *const *base_classes,
8158 unsigned num_base_classes) {
8159 if (type) {
8160 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8161 if (cxx_record_decl) {
8162 cxx_record_decl->setBases(base_classes, num_base_classes);
8163 return true;
8164 }
8165 }
8166 return false;
8167}
8168
8169bool ClangASTContext::SetObjCSuperClass(
8170 const CompilerType &type, const CompilerType &superclass_clang_type) {
8171 ClangASTContext *ast =
8172 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8173 if (!ast)
8174 return false;
8175 clang::ASTContext *clang_ast = ast->getASTContext();
8176
8177 if (type && superclass_clang_type.IsValid() &&
8178 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8179 clang::ObjCInterfaceDecl *class_interface_decl =
8180 GetAsObjCInterfaceDecl(type);
8181 clang::ObjCInterfaceDecl *super_interface_decl =
8182 GetAsObjCInterfaceDecl(superclass_clang_type);
8183 if (class_interface_decl && super_interface_decl) {
8184 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8185 clang_ast->getObjCInterfaceType(super_interface_decl)));
8186 return true;
8187 }
8188 }
8189 return false;
8190}
8191
8192bool ClangASTContext::AddObjCClassProperty(
8193 const CompilerType &type, const char *property_name,
8194 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8195 const char *property_setter_name, const char *property_getter_name,
8196 uint32_t property_attributes, ClangASTMetadata *metadata) {
8197 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8198 property_name[0] == '\0')
8199 return false;
8200 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8201 if (!ast)
8202 return false;
8203 clang::ASTContext *clang_ast = ast->getASTContext();
8204
8205 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8206
8207 if (class_interface_decl) {
8208 CompilerType property_clang_type_to_access;
8209
8210 if (property_clang_type.IsValid())
8211 property_clang_type_to_access = property_clang_type;
8212 else if (ivar_decl)
8213 property_clang_type_to_access =
8214 CompilerType(clang_ast, ivar_decl->getType());
8215
8216 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8217 clang::TypeSourceInfo *prop_type_source;
8218 if (ivar_decl)
8219 prop_type_source =
8220 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8221 else
8222 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8223 ClangUtil::GetQualType(property_clang_type));
8224
8225 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8226 *clang_ast, class_interface_decl,
8227 clang::SourceLocation(), // Source Location
8228 &clang_ast->Idents.get(property_name),
8229 clang::SourceLocation(), // Source Location for AT
8230 clang::SourceLocation(), // Source location for (
8231 ivar_decl ? ivar_decl->getType()
8232 : ClangUtil::GetQualType(property_clang_type),
8233 prop_type_source);
8234
8235 if (property_decl) {
8236 if (metadata)
8237 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8238
8239 class_interface_decl->addDecl(property_decl);
8240
8241 clang::Selector setter_sel, getter_sel;
8242
8243 if (property_setter_name != nullptr) {
8244 std::string property_setter_no_colon(
8245 property_setter_name, strlen(property_setter_name) - 1);
8246 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008247 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008248 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8249 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8250 std::string setter_sel_string("set");
8251 setter_sel_string.push_back(::toupper(property_name[0]));
8252 setter_sel_string.append(&property_name[1]);
8253 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008254 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008255 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8256 }
8257 property_decl->setSetterName(setter_sel);
8258 property_decl->setPropertyAttributes(
8259 clang::ObjCPropertyDecl::OBJC_PR_setter);
8260
8261 if (property_getter_name != nullptr) {
8262 clang::IdentifierInfo *getter_ident =
8263 &clang_ast->Idents.get(property_getter_name);
8264 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8265 } else {
8266 clang::IdentifierInfo *getter_ident =
8267 &clang_ast->Idents.get(property_name);
8268 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8269 }
8270 property_decl->setGetterName(getter_sel);
8271 property_decl->setPropertyAttributes(
8272 clang::ObjCPropertyDecl::OBJC_PR_getter);
8273
8274 if (ivar_decl)
8275 property_decl->setPropertyIvarDecl(ivar_decl);
8276
8277 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8278 property_decl->setPropertyAttributes(
8279 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8280 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8281 property_decl->setPropertyAttributes(
8282 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8283 if (property_attributes & DW_APPLE_PROPERTY_assign)
8284 property_decl->setPropertyAttributes(
8285 clang::ObjCPropertyDecl::OBJC_PR_assign);
8286 if (property_attributes & DW_APPLE_PROPERTY_retain)
8287 property_decl->setPropertyAttributes(
8288 clang::ObjCPropertyDecl::OBJC_PR_retain);
8289 if (property_attributes & DW_APPLE_PROPERTY_copy)
8290 property_decl->setPropertyAttributes(
8291 clang::ObjCPropertyDecl::OBJC_PR_copy);
8292 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8293 property_decl->setPropertyAttributes(
8294 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8295 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8296 property_decl->setPropertyAttributes(
8297 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8298 if (property_attributes &
8299 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8300 property_decl->setPropertyAttributes(
8301 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8302 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8303 property_decl->setPropertyAttributes(
8304 clang::ObjCPropertyDecl::OBJC_PR_class);
8305
8306 const bool isInstance =
8307 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8308
8309 if (!getter_sel.isNull() &&
8310 !(isInstance
8311 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8312 : class_interface_decl->lookupClassMethod(getter_sel))) {
8313 const bool isVariadic = false;
8314 const bool isSynthesized = false;
8315 const bool isImplicitlyDeclared = true;
8316 const bool isDefined = false;
8317 const clang::ObjCMethodDecl::ImplementationControl impControl =
8318 clang::ObjCMethodDecl::None;
8319 const bool HasRelatedResultType = false;
8320
8321 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8322 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8323 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8324 nullptr, class_interface_decl, isInstance, isVariadic,
8325 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8326 HasRelatedResultType);
8327
8328 if (getter && metadata)
8329 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8330
8331 if (getter) {
8332 getter->setMethodParams(*clang_ast,
8333 llvm::ArrayRef<clang::ParmVarDecl *>(),
8334 llvm::ArrayRef<clang::SourceLocation>());
8335
8336 class_interface_decl->addDecl(getter);
8337 }
8338 }
8339
8340 if (!setter_sel.isNull() &&
8341 !(isInstance
8342 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8343 : class_interface_decl->lookupClassMethod(setter_sel))) {
8344 clang::QualType result_type = clang_ast->VoidTy;
8345 const bool isVariadic = false;
8346 const bool isSynthesized = false;
8347 const bool isImplicitlyDeclared = true;
8348 const bool isDefined = false;
8349 const clang::ObjCMethodDecl::ImplementationControl impControl =
8350 clang::ObjCMethodDecl::None;
8351 const bool HasRelatedResultType = false;
8352
8353 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8354 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8355 setter_sel, result_type, nullptr, class_interface_decl,
8356 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8357 isDefined, impControl, HasRelatedResultType);
8358
8359 if (setter && metadata)
8360 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8361
8362 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8363
8364 params.push_back(clang::ParmVarDecl::Create(
8365 *clang_ast, setter, clang::SourceLocation(),
8366 clang::SourceLocation(),
8367 nullptr, // anonymous
8368 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8369 clang::SC_Auto, nullptr));
8370
8371 if (setter) {
8372 setter->setMethodParams(
8373 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8374 llvm::ArrayRef<clang::SourceLocation>());
8375
8376 class_interface_decl->addDecl(setter);
8377 }
8378 }
8379
8380 return true;
8381 }
8382 }
8383 }
8384 return false;
8385}
8386
8387bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8388 bool check_superclass) {
8389 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8390 if (class_interface_decl)
8391 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8392 return false;
8393}
8394
8395clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8396 const CompilerType &type,
8397 const char *name, // the full symbol name as seen in the symbol table
8398 // (lldb::opaque_compiler_type_t type, "-[NString
8399 // stringWithCString:]")
8400 const CompilerType &method_clang_type, lldb::AccessType access,
8401 bool is_artificial, bool is_variadic) {
8402 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008403 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008404
Kate Stoneb9c1b512016-09-06 20:57:50 +00008405 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8406
8407 if (class_interface_decl == nullptr)
8408 return nullptr;
8409 ClangASTContext *lldb_ast =
8410 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8411 if (lldb_ast == nullptr)
8412 return nullptr;
8413 clang::ASTContext *ast = lldb_ast->getASTContext();
8414
8415 const char *selector_start = ::strchr(name, ' ');
8416 if (selector_start == nullptr)
8417 return nullptr;
8418
8419 selector_start++;
8420 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8421
8422 size_t len = 0;
8423 const char *start;
8424 // printf ("name = '%s'\n", name);
8425
8426 unsigned num_selectors_with_args = 0;
8427 for (start = selector_start; start && *start != '\0' && *start != ']';
8428 start += len) {
8429 len = ::strcspn(start, ":]");
8430 bool has_arg = (start[len] == ':');
8431 if (has_arg)
8432 ++num_selectors_with_args;
8433 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8434 if (has_arg)
8435 len += 1;
8436 }
8437
8438 if (selector_idents.size() == 0)
8439 return nullptr;
8440
8441 clang::Selector method_selector = ast->Selectors.getSelector(
8442 num_selectors_with_args ? selector_idents.size() : 0,
8443 selector_idents.data());
8444
8445 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8446
8447 // Populate the method decl with parameter decls
8448 const clang::Type *method_type(method_qual_type.getTypePtr());
8449
8450 if (method_type == nullptr)
8451 return nullptr;
8452
8453 const clang::FunctionProtoType *method_function_prototype(
8454 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8455
8456 if (!method_function_prototype)
8457 return nullptr;
8458
8459 bool is_synthesized = false;
8460 bool is_defined = false;
8461 clang::ObjCMethodDecl::ImplementationControl imp_control =
8462 clang::ObjCMethodDecl::None;
8463
8464 const unsigned num_args = method_function_prototype->getNumParams();
8465
8466 if (num_args != num_selectors_with_args)
8467 return nullptr; // some debug information is corrupt. We are not going to
8468 // deal with it.
8469
8470 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8471 *ast,
8472 clang::SourceLocation(), // beginLoc,
8473 clang::SourceLocation(), // endLoc,
8474 method_selector, method_function_prototype->getReturnType(),
8475 nullptr, // TypeSourceInfo *ResultTInfo,
8476 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8477 ClangUtil::GetQualType(type)),
8478 name[0] == '-', is_variadic, is_synthesized,
8479 true, // is_implicitly_declared; we force this to true because we don't
8480 // have source locations
8481 is_defined, imp_control, false /*has_related_result_type*/);
8482
8483 if (objc_method_decl == nullptr)
8484 return nullptr;
8485
8486 if (num_args > 0) {
8487 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8488
8489 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8490 params.push_back(clang::ParmVarDecl::Create(
8491 *ast, objc_method_decl, clang::SourceLocation(),
8492 clang::SourceLocation(),
8493 nullptr, // anonymous
8494 method_function_prototype->getParamType(param_index), nullptr,
8495 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008496 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008497
Kate Stoneb9c1b512016-09-06 20:57:50 +00008498 objc_method_decl->setMethodParams(
8499 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8500 llvm::ArrayRef<clang::SourceLocation>());
8501 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008502
Kate Stoneb9c1b512016-09-06 20:57:50 +00008503 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008504
Greg Claytond8d4a572015-08-11 21:38:15 +00008505#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008506 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008507#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008508
8509 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008510}
8511
Kate Stoneb9c1b512016-09-06 20:57:50 +00008512bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8513 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008514 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008515
Kate Stoneb9c1b512016-09-06 20:57:50 +00008516 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008517
Kate Stoneb9c1b512016-09-06 20:57:50 +00008518 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8519 switch (type_class) {
8520 case clang::Type::Record: {
8521 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8522 if (cxx_record_decl)
8523 return cxx_record_decl->hasExternalLexicalStorage() ||
8524 cxx_record_decl->hasExternalVisibleStorage();
8525 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008526
Kate Stoneb9c1b512016-09-06 20:57:50 +00008527 case clang::Type::Enum: {
8528 clang::EnumDecl *enum_decl =
8529 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8530 if (enum_decl)
8531 return enum_decl->hasExternalLexicalStorage() ||
8532 enum_decl->hasExternalVisibleStorage();
8533 } break;
8534
8535 case clang::Type::ObjCObject:
8536 case clang::Type::ObjCInterface: {
8537 const clang::ObjCObjectType *objc_class_type =
8538 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8539 assert(objc_class_type);
8540 if (objc_class_type) {
8541 clang::ObjCInterfaceDecl *class_interface_decl =
8542 objc_class_type->getInterface();
8543
8544 if (class_interface_decl)
8545 return class_interface_decl->hasExternalLexicalStorage() ||
8546 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008547 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008548 } break;
8549
8550 case clang::Type::Typedef:
8551 return GetHasExternalStorage(CompilerType(
8552 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8553 ->getDecl()
8554 ->getUnderlyingType()
8555 .getAsOpaquePtr()));
8556
8557 case clang::Type::Auto:
8558 return GetHasExternalStorage(CompilerType(
8559 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8560 ->getDeducedType()
8561 .getAsOpaquePtr()));
8562
8563 case clang::Type::Elaborated:
8564 return GetHasExternalStorage(CompilerType(
8565 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8566 ->getNamedType()
8567 .getAsOpaquePtr()));
8568
8569 case clang::Type::Paren:
8570 return GetHasExternalStorage(CompilerType(
8571 type.GetTypeSystem(),
8572 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8573
8574 default:
8575 break;
8576 }
8577 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008578}
8579
Kate Stoneb9c1b512016-09-06 20:57:50 +00008580bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8581 bool has_extern) {
8582 if (!type)
8583 return false;
8584
8585 clang::QualType qual_type(GetCanonicalQualType(type));
8586
8587 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8588 switch (type_class) {
8589 case clang::Type::Record: {
8590 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8591 if (cxx_record_decl) {
8592 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8593 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8594 return true;
8595 }
8596 } break;
8597
8598 case clang::Type::Enum: {
8599 clang::EnumDecl *enum_decl =
8600 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8601 if (enum_decl) {
8602 enum_decl->setHasExternalLexicalStorage(has_extern);
8603 enum_decl->setHasExternalVisibleStorage(has_extern);
8604 return true;
8605 }
8606 } break;
8607
8608 case clang::Type::ObjCObject:
8609 case clang::Type::ObjCInterface: {
8610 const clang::ObjCObjectType *objc_class_type =
8611 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8612 assert(objc_class_type);
8613 if (objc_class_type) {
8614 clang::ObjCInterfaceDecl *class_interface_decl =
8615 objc_class_type->getInterface();
8616
8617 if (class_interface_decl) {
8618 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8619 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8620 return true;
8621 }
8622 }
8623 } break;
8624
8625 case clang::Type::Typedef:
8626 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8627 ->getDecl()
8628 ->getUnderlyingType()
8629 .getAsOpaquePtr(),
8630 has_extern);
8631
8632 case clang::Type::Auto:
8633 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8634 ->getDeducedType()
8635 .getAsOpaquePtr(),
8636 has_extern);
8637
8638 case clang::Type::Elaborated:
8639 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8640 ->getNamedType()
8641 .getAsOpaquePtr(),
8642 has_extern);
8643
8644 case clang::Type::Paren:
8645 return SetHasExternalStorage(
8646 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8647 has_extern);
8648
8649 default:
8650 break;
8651 }
8652 return false;
8653}
Greg Claytond8d4a572015-08-11 21:38:15 +00008654
8655#pragma mark TagDecl
8656
Kate Stoneb9c1b512016-09-06 20:57:50 +00008657bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8658 clang::QualType qual_type(ClangUtil::GetQualType(type));
8659 if (!qual_type.isNull()) {
8660 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8661 if (tag_type) {
8662 clang::TagDecl *tag_decl = tag_type->getDecl();
8663 if (tag_decl) {
8664 tag_decl->startDefinition();
8665 return true;
8666 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008667 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008668
8669 const clang::ObjCObjectType *object_type =
8670 qual_type->getAs<clang::ObjCObjectType>();
8671 if (object_type) {
8672 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8673 if (interface_decl) {
8674 interface_decl->startDefinition();
8675 return true;
8676 }
8677 }
8678 }
8679 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008680}
8681
Kate Stoneb9c1b512016-09-06 20:57:50 +00008682bool ClangASTContext::CompleteTagDeclarationDefinition(
8683 const CompilerType &type) {
8684 clang::QualType qual_type(ClangUtil::GetQualType(type));
8685 if (!qual_type.isNull()) {
8686 // Make sure we use the same methodology as
8687 // ClangASTContext::StartTagDeclarationDefinition()
8688 // as to how we start/end the definition. Previously we were calling
8689 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8690 if (tag_type) {
8691 clang::TagDecl *tag_decl = tag_type->getDecl();
8692 if (tag_decl) {
8693 clang::CXXRecordDecl *cxx_record_decl =
8694 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8695
8696 if (cxx_record_decl) {
8697 if (!cxx_record_decl->isCompleteDefinition())
8698 cxx_record_decl->completeDefinition();
8699 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8700 cxx_record_decl->setHasExternalLexicalStorage(false);
8701 cxx_record_decl->setHasExternalVisibleStorage(false);
8702 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008703 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008704 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008705 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008706
8707 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8708
8709 if (enutype) {
8710 clang::EnumDecl *enum_decl = enutype->getDecl();
8711
8712 if (enum_decl) {
8713 if (!enum_decl->isCompleteDefinition()) {
8714 ClangASTContext *lldb_ast =
8715 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8716 if (lldb_ast == nullptr)
8717 return false;
8718 clang::ASTContext *ast = lldb_ast->getASTContext();
8719
8720 /// TODO This really needs to be fixed.
8721
8722 QualType integer_type(enum_decl->getIntegerType());
8723 if (!integer_type.isNull()) {
8724 unsigned NumPositiveBits = 1;
8725 unsigned NumNegativeBits = 0;
8726
8727 clang::QualType promotion_qual_type;
8728 // If the enum integer type is less than an integer in bit width,
8729 // then we must promote it to an integer size.
8730 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8731 ast->getTypeSize(ast->IntTy)) {
8732 if (enum_decl->getIntegerType()->isSignedIntegerType())
8733 promotion_qual_type = ast->IntTy;
8734 else
8735 promotion_qual_type = ast->UnsignedIntTy;
8736 } else
8737 promotion_qual_type = enum_decl->getIntegerType();
8738
8739 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8740 promotion_qual_type, NumPositiveBits,
8741 NumNegativeBits);
8742 }
8743 }
8744 return true;
8745 }
8746 }
8747 }
8748 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008749}
8750
Kate Stoneb9c1b512016-09-06 20:57:50 +00008751bool ClangASTContext::AddEnumerationValueToEnumerationType(
8752 lldb::opaque_compiler_type_t type,
8753 const CompilerType &enumerator_clang_type, const Declaration &decl,
8754 const char *name, int64_t enum_value, uint32_t enum_value_bit_size) {
8755 if (type && enumerator_clang_type.IsValid() && name && name[0]) {
8756 clang::QualType enum_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008757
Kate Stoneb9c1b512016-09-06 20:57:50 +00008758 bool is_signed = false;
8759 enumerator_clang_type.IsIntegerType(is_signed);
Greg Claytond8d4a572015-08-11 21:38:15 +00008760 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Kate Stoneb9c1b512016-09-06 20:57:50 +00008761 if (clang_type) {
8762 const clang::EnumType *enutype =
8763 llvm::dyn_cast<clang::EnumType>(clang_type);
8764
8765 if (enutype) {
8766 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8767 enum_llvm_apsint = enum_value;
8768 clang::EnumConstantDecl *enumerator_decl =
8769 clang::EnumConstantDecl::Create(
8770 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8771 name ? &getASTContext()->Idents.get(name)
8772 : nullptr, // Identifier
8773 ClangUtil::GetQualType(enumerator_clang_type),
8774 nullptr, enum_llvm_apsint);
8775
8776 if (enumerator_decl) {
8777 enutype->getDecl()->addDecl(enumerator_decl);
8778
8779#ifdef LLDB_CONFIGURATION_DEBUG
8780 VerifyDecl(enumerator_decl);
8781#endif
8782
8783 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008784 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008785 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008786 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008787 }
8788 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008789}
8790
Greg Claytona1e5dc82015-08-11 22:53:00 +00008791CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00008792ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8793 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8794 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8795 if (clang_type) {
8796 const clang::EnumType *enutype =
8797 llvm::dyn_cast<clang::EnumType>(clang_type);
8798 if (enutype) {
8799 clang::EnumDecl *enum_decl = enutype->getDecl();
8800 if (enum_decl)
8801 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008802 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008803 }
8804 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008805}
8806
Kate Stoneb9c1b512016-09-06 20:57:50 +00008807CompilerType
8808ClangASTContext::CreateMemberPointerType(const CompilerType &type,
8809 const CompilerType &pointee_type) {
8810 if (type && pointee_type.IsValid() &&
8811 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8812 ClangASTContext *ast =
8813 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8814 if (!ast)
8815 return CompilerType();
8816 return CompilerType(ast->getASTContext(),
8817 ast->getASTContext()->getMemberPointerType(
8818 ClangUtil::GetQualType(pointee_type),
8819 ClangUtil::GetQualType(type).getTypePtr()));
8820 }
8821 return CompilerType();
8822}
Greg Claytond8d4a572015-08-11 21:38:15 +00008823
8824size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00008825ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
8826 const char *s, uint8_t *dst,
8827 size_t dst_size) {
8828 if (type) {
8829 clang::QualType qual_type(GetCanonicalQualType(type));
8830 uint32_t count = 0;
8831 bool is_complex = false;
8832 if (IsFloatingPointType(type, count, is_complex)) {
8833 // TODO: handle complex and vector types
8834 if (count != 1)
8835 return false;
8836
8837 llvm::StringRef s_sref(s);
8838 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
8839 s_sref);
8840
8841 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
8842 const uint64_t byte_size = bit_size / 8;
8843 if (dst_size >= byte_size) {
8844 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
8845 llvm::NextPowerOf2(byte_size) * 8);
Zachary Turner97206d52017-05-12 04:51:55 +00008846 lldb_private::Status get_data_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00008847 if (scalar.GetAsMemoryData(dst, byte_size,
8848 lldb_private::endian::InlHostByteOrder(),
8849 get_data_error))
8850 return byte_size;
8851 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008852 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008853 }
8854 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00008855}
8856
Greg Claytond8d4a572015-08-11 21:38:15 +00008857//----------------------------------------------------------------------
8858// Dumping types
8859//----------------------------------------------------------------------
8860#define DEPTH_INCREMENT 2
8861
Kate Stoneb9c1b512016-09-06 20:57:50 +00008862void ClangASTContext::DumpValue(
8863 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00008864 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008865 lldb::offset_t data_byte_offset, size_t data_byte_size,
8866 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
8867 bool show_summary, bool verbose, uint32_t depth) {
8868 if (!type)
8869 return;
8870
8871 clang::QualType qual_type(GetQualType(type));
8872 switch (qual_type->getTypeClass()) {
8873 case clang::Type::Record:
8874 if (GetCompleteType(type)) {
8875 const clang::RecordType *record_type =
8876 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8877 const clang::RecordDecl *record_decl = record_type->getDecl();
8878 assert(record_decl);
8879 uint32_t field_bit_offset = 0;
8880 uint32_t field_byte_offset = 0;
8881 const clang::ASTRecordLayout &record_layout =
8882 getASTContext()->getASTRecordLayout(record_decl);
8883 uint32_t child_idx = 0;
8884
8885 const clang::CXXRecordDecl *cxx_record_decl =
8886 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8887 if (cxx_record_decl) {
8888 // We might have base classes to print out first
8889 clang::CXXRecordDecl::base_class_const_iterator base_class,
8890 base_class_end;
8891 for (base_class = cxx_record_decl->bases_begin(),
8892 base_class_end = cxx_record_decl->bases_end();
8893 base_class != base_class_end; ++base_class) {
8894 const clang::CXXRecordDecl *base_class_decl =
8895 llvm::cast<clang::CXXRecordDecl>(
8896 base_class->getType()->getAs<clang::RecordType>()->getDecl());
8897
8898 // Skip empty base classes
8899 if (verbose == false &&
8900 ClangASTContext::RecordHasFields(base_class_decl) == false)
8901 continue;
8902
8903 if (base_class->isVirtual())
8904 field_bit_offset =
8905 record_layout.getVBaseClassOffset(base_class_decl)
8906 .getQuantity() *
8907 8;
8908 else
8909 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
8910 .getQuantity() *
8911 8;
8912 field_byte_offset = field_bit_offset / 8;
8913 assert(field_bit_offset % 8 == 0);
8914 if (child_idx == 0)
8915 s->PutChar('{');
8916 else
8917 s->PutChar(',');
8918
8919 clang::QualType base_class_qual_type = base_class->getType();
8920 std::string base_class_type_name(base_class_qual_type.getAsString());
8921
8922 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00008923 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
8924 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008925
8926 clang::TypeInfo base_class_type_info =
8927 getASTContext()->getTypeInfo(base_class_qual_type);
8928
8929 // Dump the value of the member
8930 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
8931 base_clang_type.DumpValue(
8932 exe_ctx,
8933 s, // Stream to dump to
8934 base_clang_type
8935 .GetFormat(), // The format with which to display the member
8936 data, // Data buffer containing all bytes for this type
8937 data_byte_offset + field_byte_offset, // Offset into "data" where
8938 // to grab value from
8939 base_class_type_info.Width / 8, // Size of this type in bytes
8940 0, // Bitfield bit size
8941 0, // Bitfield bit offset
8942 show_types, // Boolean indicating if we should show the variable
8943 // types
8944 show_summary, // Boolean indicating if we should show a summary
8945 // for the current type
8946 verbose, // Verbose output?
8947 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8948 // children
8949
8950 ++child_idx;
8951 }
8952 }
8953 uint32_t field_idx = 0;
8954 clang::RecordDecl::field_iterator field, field_end;
8955 for (field = record_decl->field_begin(),
8956 field_end = record_decl->field_end();
8957 field != field_end; ++field, ++field_idx, ++child_idx) {
8958 // Print the starting squiggly bracket (if this is the
8959 // first member) or comma (for member 2 and beyond) for
8960 // the struct/union/class member.
8961 if (child_idx == 0)
8962 s->PutChar('{');
8963 else
8964 s->PutChar(',');
8965
8966 // Indent
8967 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8968
8969 clang::QualType field_type = field->getType();
8970 // Print the member type if requested
8971 // Figure out the type byte size (field_type_info.first) and
8972 // alignment (field_type_info.second) from the AST context.
8973 clang::TypeInfo field_type_info =
8974 getASTContext()->getTypeInfo(field_type);
8975 assert(field_idx < record_layout.getFieldCount());
8976 // Figure out the field offset within the current struct/union/class
8977 // type
8978 field_bit_offset = record_layout.getFieldOffset(field_idx);
8979 field_byte_offset = field_bit_offset / 8;
8980 uint32_t field_bitfield_bit_size = 0;
8981 uint32_t field_bitfield_bit_offset = 0;
8982 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
8983 field_bitfield_bit_size))
8984 field_bitfield_bit_offset = field_bit_offset % 8;
8985
8986 if (show_types) {
8987 std::string field_type_name(field_type.getAsString());
8988 if (field_bitfield_bit_size > 0)
8989 s->Printf("(%s:%u) ", field_type_name.c_str(),
8990 field_bitfield_bit_size);
8991 else
8992 s->Printf("(%s) ", field_type_name.c_str());
8993 }
8994 // Print the member name and equal sign
8995 s->Printf("%s = ", field->getNameAsString().c_str());
8996
8997 // Dump the value of the member
8998 CompilerType field_clang_type(getASTContext(), field_type);
8999 field_clang_type.DumpValue(
9000 exe_ctx,
9001 s, // Stream to dump to
9002 field_clang_type
9003 .GetFormat(), // The format with which to display the member
9004 data, // Data buffer containing all bytes for this type
9005 data_byte_offset + field_byte_offset, // Offset into "data" where to
9006 // grab value from
9007 field_type_info.Width / 8, // Size of this type in bytes
9008 field_bitfield_bit_size, // Bitfield bit size
9009 field_bitfield_bit_offset, // Bitfield bit offset
9010 show_types, // Boolean indicating if we should show the variable
9011 // types
9012 show_summary, // Boolean indicating if we should show a summary for
9013 // the current type
9014 verbose, // Verbose output?
9015 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9016 // children
9017 }
9018
9019 // Indent the trailing squiggly bracket
9020 if (child_idx > 0)
9021 s->Printf("\n%*s}", depth, "");
9022 }
9023 return;
9024
9025 case clang::Type::Enum:
9026 if (GetCompleteType(type)) {
9027 const clang::EnumType *enutype =
9028 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9029 const clang::EnumDecl *enum_decl = enutype->getDecl();
9030 assert(enum_decl);
9031 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9032 lldb::offset_t offset = data_byte_offset;
9033 const int64_t enum_value = data.GetMaxU64Bitfield(
9034 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9035 for (enum_pos = enum_decl->enumerator_begin(),
9036 enum_end_pos = enum_decl->enumerator_end();
9037 enum_pos != enum_end_pos; ++enum_pos) {
9038 if (enum_pos->getInitVal() == enum_value) {
9039 s->Printf("%s", enum_pos->getNameAsString().c_str());
9040 return;
9041 }
9042 }
9043 // If we have gotten here we didn't get find the enumerator in the
9044 // enum decl, so just print the integer.
9045 s->Printf("%" PRIi64, enum_value);
9046 }
9047 return;
9048
9049 case clang::Type::ConstantArray: {
9050 const clang::ConstantArrayType *array =
9051 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9052 bool is_array_of_characters = false;
9053 clang::QualType element_qual_type = array->getElementType();
9054
9055 const clang::Type *canonical_type =
9056 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9057 if (canonical_type)
9058 is_array_of_characters = canonical_type->isCharType();
9059
9060 const uint64_t element_count = array->getSize().getLimitedValue();
9061
9062 clang::TypeInfo field_type_info =
9063 getASTContext()->getTypeInfo(element_qual_type);
9064
9065 uint32_t element_idx = 0;
9066 uint32_t element_offset = 0;
9067 uint64_t element_byte_size = field_type_info.Width / 8;
9068 uint32_t element_stride = element_byte_size;
9069
9070 if (is_array_of_characters) {
9071 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009072 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9073 element_byte_size, element_count, UINT32_MAX,
9074 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009075 s->PutChar('"');
9076 return;
9077 } else {
9078 CompilerType element_clang_type(getASTContext(), element_qual_type);
9079 lldb::Format element_format = element_clang_type.GetFormat();
9080
9081 for (element_idx = 0; element_idx < element_count; ++element_idx) {
9082 // Print the starting squiggly bracket (if this is the
9083 // first member) or comman (for member 2 and beyong) for
9084 // the struct/union/class member.
9085 if (element_idx == 0)
9086 s->PutChar('{');
9087 else
9088 s->PutChar(',');
9089
9090 // Indent and print the index
9091 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9092
9093 // Figure out the field offset within the current struct/union/class
9094 // type
9095 element_offset = element_idx * element_stride;
9096
9097 // Dump the value of the member
9098 element_clang_type.DumpValue(
9099 exe_ctx,
9100 s, // Stream to dump to
9101 element_format, // The format with which to display the element
9102 data, // Data buffer containing all bytes for this type
9103 data_byte_offset +
9104 element_offset, // Offset into "data" where to grab value from
9105 element_byte_size, // Size of this type in bytes
9106 0, // Bitfield bit size
9107 0, // Bitfield bit offset
9108 show_types, // Boolean indicating if we should show the variable
9109 // types
9110 show_summary, // Boolean indicating if we should show a summary for
9111 // the current type
9112 verbose, // Verbose output?
9113 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9114 // children
9115 }
9116
9117 // Indent the trailing squiggly bracket
9118 if (element_idx > 0)
9119 s->Printf("\n%*s}", depth, "");
9120 }
9121 }
9122 return;
9123
9124 case clang::Type::Typedef: {
9125 clang::QualType typedef_qual_type =
9126 llvm::cast<clang::TypedefType>(qual_type)
9127 ->getDecl()
9128 ->getUnderlyingType();
9129
9130 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9131 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9132 clang::TypeInfo typedef_type_info =
9133 getASTContext()->getTypeInfo(typedef_qual_type);
9134 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9135
9136 return typedef_clang_type.DumpValue(
9137 exe_ctx,
9138 s, // Stream to dump to
9139 typedef_format, // The format with which to display the element
9140 data, // Data buffer containing all bytes for this type
9141 data_byte_offset, // Offset into "data" where to grab value from
9142 typedef_byte_size, // Size of this type in bytes
9143 bitfield_bit_size, // Bitfield bit size
9144 bitfield_bit_offset, // Bitfield bit offset
9145 show_types, // Boolean indicating if we should show the variable types
9146 show_summary, // Boolean indicating if we should show a summary for the
9147 // current type
9148 verbose, // Verbose output?
9149 depth); // Scope depth for any types that have children
9150 } break;
9151
9152 case clang::Type::Auto: {
9153 clang::QualType elaborated_qual_type =
9154 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9155 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9156 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9157 clang::TypeInfo elaborated_type_info =
9158 getASTContext()->getTypeInfo(elaborated_qual_type);
9159 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9160
9161 return elaborated_clang_type.DumpValue(
9162 exe_ctx,
9163 s, // Stream to dump to
9164 elaborated_format, // The format with which to display the element
9165 data, // Data buffer containing all bytes for this type
9166 data_byte_offset, // Offset into "data" where to grab value from
9167 elaborated_byte_size, // Size of this type in bytes
9168 bitfield_bit_size, // Bitfield bit size
9169 bitfield_bit_offset, // Bitfield bit offset
9170 show_types, // Boolean indicating if we should show the variable types
9171 show_summary, // Boolean indicating if we should show a summary for the
9172 // current type
9173 verbose, // Verbose output?
9174 depth); // Scope depth for any types that have children
9175 } break;
9176
9177 case clang::Type::Elaborated: {
9178 clang::QualType elaborated_qual_type =
9179 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9180 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9181 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9182 clang::TypeInfo elaborated_type_info =
9183 getASTContext()->getTypeInfo(elaborated_qual_type);
9184 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9185
9186 return elaborated_clang_type.DumpValue(
9187 exe_ctx,
9188 s, // Stream to dump to
9189 elaborated_format, // The format with which to display the element
9190 data, // Data buffer containing all bytes for this type
9191 data_byte_offset, // Offset into "data" where to grab value from
9192 elaborated_byte_size, // Size of this type in bytes
9193 bitfield_bit_size, // Bitfield bit size
9194 bitfield_bit_offset, // Bitfield bit offset
9195 show_types, // Boolean indicating if we should show the variable types
9196 show_summary, // Boolean indicating if we should show a summary for the
9197 // current type
9198 verbose, // Verbose output?
9199 depth); // Scope depth for any types that have children
9200 } break;
9201
9202 case clang::Type::Paren: {
9203 clang::QualType desugar_qual_type =
9204 llvm::cast<clang::ParenType>(qual_type)->desugar();
9205 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9206
9207 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9208 clang::TypeInfo desugar_type_info =
9209 getASTContext()->getTypeInfo(desugar_qual_type);
9210 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9211
9212 return desugar_clang_type.DumpValue(
9213 exe_ctx,
9214 s, // Stream to dump to
9215 desugar_format, // The format with which to display the element
9216 data, // Data buffer containing all bytes for this type
9217 data_byte_offset, // Offset into "data" where to grab value from
9218 desugar_byte_size, // Size of this type in bytes
9219 bitfield_bit_size, // Bitfield bit size
9220 bitfield_bit_offset, // Bitfield bit offset
9221 show_types, // Boolean indicating if we should show the variable types
9222 show_summary, // Boolean indicating if we should show a summary for the
9223 // current type
9224 verbose, // Verbose output?
9225 depth); // Scope depth for any types that have children
9226 } break;
9227
9228 default:
9229 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009230 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9231 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9232 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009233
9234 if (show_summary)
9235 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9236 break;
9237 }
9238}
9239
9240bool ClangASTContext::DumpTypeValue(
9241 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009242 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9243 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009244 ExecutionContextScope *exe_scope) {
9245 if (!type)
9246 return false;
9247 if (IsAggregateType(type)) {
9248 return false;
9249 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009250 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009251
9252 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9253 switch (type_class) {
9254 case clang::Type::Typedef: {
9255 clang::QualType typedef_qual_type =
9256 llvm::cast<clang::TypedefType>(qual_type)
9257 ->getDecl()
9258 ->getUnderlyingType();
9259 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9260 if (format == eFormatDefault)
9261 format = typedef_clang_type.GetFormat();
9262 clang::TypeInfo typedef_type_info =
9263 getASTContext()->getTypeInfo(typedef_qual_type);
9264 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9265
9266 return typedef_clang_type.DumpTypeValue(
9267 s,
9268 format, // The format with which to display the element
9269 data, // Data buffer containing all bytes for this type
9270 byte_offset, // Offset into "data" where to grab value from
9271 typedef_byte_size, // Size of this type in bytes
9272 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9273 // treat as a bitfield
9274 bitfield_bit_offset, // Offset in bits of a bitfield value if
9275 // bitfield_bit_size != 0
9276 exe_scope);
9277 } break;
9278
9279 case clang::Type::Enum:
9280 // If our format is enum or default, show the enumeration value as
9281 // its enumeration string value, else just display it as requested.
9282 if ((format == eFormatEnum || format == eFormatDefault) &&
9283 GetCompleteType(type)) {
9284 const clang::EnumType *enutype =
9285 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9286 const clang::EnumDecl *enum_decl = enutype->getDecl();
9287 assert(enum_decl);
9288 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9289 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9290 lldb::offset_t offset = byte_offset;
9291 if (is_signed) {
9292 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9293 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9294 for (enum_pos = enum_decl->enumerator_begin(),
9295 enum_end_pos = enum_decl->enumerator_end();
9296 enum_pos != enum_end_pos; ++enum_pos) {
9297 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009298 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009299 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009300 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009301 }
9302 // If we have gotten here we didn't get find the enumerator in the
9303 // enum decl, so just print the integer.
9304 s->Printf("%" PRIi64, enum_svalue);
9305 } else {
9306 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9307 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9308 for (enum_pos = enum_decl->enumerator_begin(),
9309 enum_end_pos = enum_decl->enumerator_end();
9310 enum_pos != enum_end_pos; ++enum_pos) {
9311 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009312 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009313 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009314 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009315 }
9316 // If we have gotten here we didn't get find the enumerator in the
9317 // enum decl, so just print the integer.
9318 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009319 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009320 return true;
9321 }
9322 // format was not enum, just fall through and dump the value as
9323 // requested....
9324 LLVM_FALLTHROUGH;
9325
9326 default:
9327 // We are down to a scalar type that we just need to display.
9328 {
9329 uint32_t item_count = 1;
9330 // A few formats, we might need to modify our size and count for
9331 // depending
9332 // on how we are trying to display the value...
9333 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009334 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009335 case eFormatBoolean:
9336 case eFormatBinary:
9337 case eFormatComplex:
9338 case eFormatCString: // NULL terminated C strings
9339 case eFormatDecimal:
9340 case eFormatEnum:
9341 case eFormatHex:
9342 case eFormatHexUppercase:
9343 case eFormatFloat:
9344 case eFormatOctal:
9345 case eFormatOSType:
9346 case eFormatUnsigned:
9347 case eFormatPointer:
9348 case eFormatVectorOfChar:
9349 case eFormatVectorOfSInt8:
9350 case eFormatVectorOfUInt8:
9351 case eFormatVectorOfSInt16:
9352 case eFormatVectorOfUInt16:
9353 case eFormatVectorOfSInt32:
9354 case eFormatVectorOfUInt32:
9355 case eFormatVectorOfSInt64:
9356 case eFormatVectorOfUInt64:
9357 case eFormatVectorOfFloat32:
9358 case eFormatVectorOfFloat64:
9359 case eFormatVectorOfUInt128:
9360 break;
9361
9362 case eFormatChar:
9363 case eFormatCharPrintable:
9364 case eFormatCharArray:
9365 case eFormatBytes:
9366 case eFormatBytesWithASCII:
9367 item_count = byte_size;
9368 byte_size = 1;
9369 break;
9370
9371 case eFormatUnicode16:
9372 item_count = byte_size / 2;
9373 byte_size = 2;
9374 break;
9375
9376 case eFormatUnicode32:
9377 item_count = byte_size / 4;
9378 byte_size = 4;
9379 break;
9380 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009381 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9382 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9383 bitfield_bit_size, bitfield_bit_offset,
9384 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009385 }
9386 break;
9387 }
9388 }
9389 return 0;
9390}
9391
9392void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9393 ExecutionContext *exe_ctx, Stream *s,
9394 const lldb_private::DataExtractor &data,
9395 lldb::offset_t data_byte_offset,
9396 size_t data_byte_size) {
9397 uint32_t length = 0;
9398 if (IsCStringType(type, length)) {
9399 if (exe_ctx) {
9400 Process *process = exe_ctx->GetProcessPtr();
9401 if (process) {
9402 lldb::offset_t offset = data_byte_offset;
9403 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9404 std::vector<uint8_t> buf;
9405 if (length > 0)
9406 buf.resize(length);
9407 else
9408 buf.resize(256);
9409
Zachary Turner29cb8682017-03-03 20:57:05 +00009410 DataExtractor cstr_data(&buf.front(), buf.size(),
9411 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009412 buf.back() = '\0';
9413 size_t bytes_read;
9414 size_t total_cstr_len = 0;
Zachary Turner97206d52017-05-12 04:51:55 +00009415 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009416 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9417 buf.size(), error)) > 0) {
9418 const size_t len = strlen((const char *)&buf.front());
9419 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009420 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009421 if (total_cstr_len == 0)
9422 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009423 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9424 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009425 total_cstr_len += len;
9426 if (len < buf.size())
9427 break;
9428 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009429 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009430 if (total_cstr_len > 0)
9431 s->PutChar('"');
9432 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009433 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009434 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009435}
9436
Kate Stoneb9c1b512016-09-06 20:57:50 +00009437void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9438 StreamFile s(stdout, false);
9439 DumpTypeDescription(type, &s);
9440 ClangASTMetadata *metadata =
9441 ClangASTContext::GetMetadata(getASTContext(), type);
9442 if (metadata) {
9443 metadata->Dump(&s);
9444 }
9445}
Greg Claytond8d4a572015-08-11 21:38:15 +00009446
Kate Stoneb9c1b512016-09-06 20:57:50 +00009447void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9448 Stream *s) {
9449 if (type) {
9450 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009451
Kate Stoneb9c1b512016-09-06 20:57:50 +00009452 llvm::SmallVector<char, 1024> buf;
9453 llvm::raw_svector_ostream llvm_ostrm(buf);
9454
9455 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9456 switch (type_class) {
9457 case clang::Type::ObjCObject:
9458 case clang::Type::ObjCInterface: {
9459 GetCompleteType(type);
9460
9461 const clang::ObjCObjectType *objc_class_type =
9462 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9463 assert(objc_class_type);
9464 if (objc_class_type) {
9465 clang::ObjCInterfaceDecl *class_interface_decl =
9466 objc_class_type->getInterface();
9467 if (class_interface_decl) {
9468 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9469 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009470 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009471 }
9472 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009473
Kate Stoneb9c1b512016-09-06 20:57:50 +00009474 case clang::Type::Typedef: {
9475 const clang::TypedefType *typedef_type =
9476 qual_type->getAs<clang::TypedefType>();
9477 if (typedef_type) {
9478 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9479 std::string clang_typedef_name(
9480 typedef_decl->getQualifiedNameAsString());
9481 if (!clang_typedef_name.empty()) {
9482 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009483 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009484 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009485 }
9486 } break;
9487
9488 case clang::Type::Auto:
9489 CompilerType(getASTContext(),
9490 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9491 .DumpTypeDescription(s);
9492 return;
9493
9494 case clang::Type::Elaborated:
9495 CompilerType(getASTContext(),
9496 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9497 .DumpTypeDescription(s);
9498 return;
9499
9500 case clang::Type::Paren:
9501 CompilerType(getASTContext(),
9502 llvm::cast<clang::ParenType>(qual_type)->desugar())
9503 .DumpTypeDescription(s);
9504 return;
9505
9506 case clang::Type::Record: {
9507 GetCompleteType(type);
9508
9509 const clang::RecordType *record_type =
9510 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9511 const clang::RecordDecl *record_decl = record_type->getDecl();
9512 const clang::CXXRecordDecl *cxx_record_decl =
9513 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9514
9515 if (cxx_record_decl)
9516 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9517 s->GetIndentLevel());
9518 else
9519 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9520 s->GetIndentLevel());
9521 } break;
9522
9523 default: {
9524 const clang::TagType *tag_type =
9525 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9526 if (tag_type) {
9527 clang::TagDecl *tag_decl = tag_type->getDecl();
9528 if (tag_decl)
9529 tag_decl->print(llvm_ostrm, 0);
9530 } else {
9531 std::string clang_type_name(qual_type.getAsString());
9532 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009533 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009534 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009535 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009536 }
9537
Kate Stoneb9c1b512016-09-06 20:57:50 +00009538 if (buf.size() > 0) {
9539 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009540 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009541 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009542}
9543
Kate Stoneb9c1b512016-09-06 20:57:50 +00009544void ClangASTContext::DumpTypeName(const CompilerType &type) {
9545 if (ClangUtil::IsClangType(type)) {
9546 clang::QualType qual_type(
9547 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9548
9549 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9550 switch (type_class) {
9551 case clang::Type::Record: {
9552 const clang::CXXRecordDecl *cxx_record_decl =
9553 qual_type->getAsCXXRecordDecl();
9554 if (cxx_record_decl)
9555 printf("class %s", cxx_record_decl->getName().str().c_str());
9556 } break;
9557
9558 case clang::Type::Enum: {
9559 clang::EnumDecl *enum_decl =
9560 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9561 if (enum_decl) {
9562 printf("enum %s", enum_decl->getName().str().c_str());
9563 }
9564 } break;
9565
9566 case clang::Type::ObjCObject:
9567 case clang::Type::ObjCInterface: {
9568 const clang::ObjCObjectType *objc_class_type =
9569 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9570 if (objc_class_type) {
9571 clang::ObjCInterfaceDecl *class_interface_decl =
9572 objc_class_type->getInterface();
9573 // We currently can't complete objective C types through the newly added
9574 // ASTContext
9575 // because it only supports TagDecl objects right now...
9576 if (class_interface_decl)
9577 printf("@class %s", class_interface_decl->getName().str().c_str());
9578 }
9579 } break;
9580
9581 case clang::Type::Typedef:
9582 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9583 ->getDecl()
9584 ->getName()
9585 .str()
9586 .c_str());
9587 break;
9588
9589 case clang::Type::Auto:
9590 printf("auto ");
9591 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9592 llvm::cast<clang::AutoType>(qual_type)
9593 ->getDeducedType()
9594 .getAsOpaquePtr()));
9595
9596 case clang::Type::Elaborated:
9597 printf("elaborated ");
9598 return DumpTypeName(CompilerType(
9599 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9600 ->getNamedType()
9601 .getAsOpaquePtr()));
9602
9603 case clang::Type::Paren:
9604 printf("paren ");
9605 return DumpTypeName(CompilerType(
9606 type.GetTypeSystem(),
9607 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9608
9609 default:
9610 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9611 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009612 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009613 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009614}
9615
Kate Stoneb9c1b512016-09-06 20:57:50 +00009616clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9617 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9618 const char *parent_name, int tag_decl_kind,
9619 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9620 if (template_param_infos.IsValid()) {
9621 std::string template_basename(parent_name);
9622 template_basename.erase(template_basename.find('<'));
9623
9624 return CreateClassTemplateDecl(decl_ctx, access_type,
9625 template_basename.c_str(), tag_decl_kind,
9626 template_param_infos);
9627 }
9628 return NULL;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009629}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009630
Kate Stoneb9c1b512016-09-06 20:57:50 +00009631void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9632 ClangASTContext *ast = (ClangASTContext *)baton;
9633 SymbolFile *sym_file = ast->GetSymbolFile();
9634 if (sym_file) {
9635 CompilerType clang_type = GetTypeForDecl(decl);
9636 if (clang_type)
9637 sym_file->CompleteType(clang_type);
9638 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009639}
9640
Kate Stoneb9c1b512016-09-06 20:57:50 +00009641void ClangASTContext::CompleteObjCInterfaceDecl(
9642 void *baton, clang::ObjCInterfaceDecl *decl) {
9643 ClangASTContext *ast = (ClangASTContext *)baton;
9644 SymbolFile *sym_file = ast->GetSymbolFile();
9645 if (sym_file) {
9646 CompilerType clang_type = GetTypeForDecl(decl);
9647 if (clang_type)
9648 sym_file->CompleteType(clang_type);
9649 }
Zachary Turner42dff792016-04-15 00:21:26 +00009650}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009651
Kate Stoneb9c1b512016-09-06 20:57:50 +00009652DWARFASTParser *ClangASTContext::GetDWARFParser() {
9653 if (!m_dwarf_ast_parser_ap)
9654 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9655 return m_dwarf_ast_parser_ap.get();
9656}
9657
9658PDBASTParser *ClangASTContext::GetPDBParser() {
9659 if (!m_pdb_ast_parser_ap)
9660 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9661 return m_pdb_ast_parser_ap.get();
9662}
9663
9664bool ClangASTContext::LayoutRecordType(
9665 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9666 uint64_t &alignment,
9667 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9668 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9669 &base_offsets,
9670 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9671 &vbase_offsets) {
9672 ClangASTContext *ast = (ClangASTContext *)baton;
9673 DWARFASTParserClang *dwarf_ast_parser =
9674 (DWARFASTParserClang *)ast->GetDWARFParser();
9675 return dwarf_ast_parser->GetClangASTImporter().LayoutRecordType(
9676 record_decl, bit_size, alignment, field_offsets, base_offsets,
9677 vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009678}
9679
Greg Clayton99558cc42015-08-24 23:46:31 +00009680//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009681// CompilerDecl override functions
9682//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009683
Kate Stoneb9c1b512016-09-06 20:57:50 +00009684ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9685 if (opaque_decl) {
9686 clang::NamedDecl *nd =
9687 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9688 if (nd != nullptr)
9689 return ConstString(nd->getDeclName().getAsString());
9690 }
9691 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009692}
9693
Kate Stoneb9c1b512016-09-06 20:57:50 +00009694ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9695 if (opaque_decl) {
9696 clang::NamedDecl *nd =
9697 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9698 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9699 clang::MangleContext *mc = getMangleContext();
9700 if (mc && mc->shouldMangleCXXName(nd)) {
9701 llvm::SmallVector<char, 1024> buf;
9702 llvm::raw_svector_ostream llvm_ostrm(buf);
9703 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9704 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9705 Ctor_Complete, llvm_ostrm);
9706 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9707 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9708 Dtor_Complete, llvm_ostrm);
9709 } else {
9710 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009711 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009712 if (buf.size() > 0)
9713 return ConstString(buf.data(), buf.size());
9714 }
Greg Claytonfe689042015-11-10 17:47:04 +00009715 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009716 }
9717 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009718}
9719
Kate Stoneb9c1b512016-09-06 20:57:50 +00009720CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9721 if (opaque_decl)
9722 return CompilerDeclContext(this,
9723 ((clang::Decl *)opaque_decl)->getDeclContext());
9724 else
9725 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009726}
9727
Kate Stoneb9c1b512016-09-06 20:57:50 +00009728CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9729 if (clang::FunctionDecl *func_decl =
9730 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9731 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9732 if (clang::ObjCMethodDecl *objc_method =
9733 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9734 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9735 else
Greg Claytonfe689042015-11-10 17:47:04 +00009736 return CompilerType();
9737}
9738
Kate Stoneb9c1b512016-09-06 20:57:50 +00009739size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9740 if (clang::FunctionDecl *func_decl =
9741 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9742 return func_decl->param_size();
9743 if (clang::ObjCMethodDecl *objc_method =
9744 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9745 return objc_method->param_size();
9746 else
9747 return 0;
9748}
9749
9750CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9751 size_t idx) {
9752 if (clang::FunctionDecl *func_decl =
9753 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9754 if (idx < func_decl->param_size()) {
9755 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9756 if (var_decl)
9757 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9758 }
9759 } else if (clang::ObjCMethodDecl *objc_method =
9760 llvm::dyn_cast<clang::ObjCMethodDecl>(
9761 (clang::Decl *)opaque_decl)) {
9762 if (idx < objc_method->param_size())
9763 return CompilerType(
9764 this,
9765 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9766 }
9767 return CompilerType();
9768}
9769
Paul Hermand628cbb2015-09-15 23:44:17 +00009770//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00009771// CompilerDeclContext functions
9772//----------------------------------------------------------------------
9773
Kate Stoneb9c1b512016-09-06 20:57:50 +00009774std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9775 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9776 std::vector<CompilerDecl> found_decls;
9777 if (opaque_decl_ctx) {
9778 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9779 std::set<DeclContext *> searched;
9780 std::multimap<DeclContext *, DeclContext *> search_queue;
9781 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009782
Kate Stoneb9c1b512016-09-06 20:57:50 +00009783 for (clang::DeclContext *decl_context = root_decl_ctx;
9784 decl_context != nullptr && found_decls.empty();
9785 decl_context = decl_context->getParent()) {
9786 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +00009787
Kate Stoneb9c1b512016-09-06 20:57:50 +00009788 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9789 it++) {
9790 if (!searched.insert(it->second).second)
9791 continue;
9792 symbol_file->ParseDeclsForContext(
9793 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +00009794
Kate Stoneb9c1b512016-09-06 20:57:50 +00009795 for (clang::Decl *child : it->second->decls()) {
9796 if (clang::UsingDirectiveDecl *ud =
9797 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9798 if (ignore_using_decls)
9799 continue;
9800 clang::DeclContext *from = ud->getCommonAncestor();
9801 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9802 search_queue.insert(
9803 std::make_pair(from, ud->getNominatedNamespace()));
9804 } else if (clang::UsingDecl *ud =
9805 llvm::dyn_cast<clang::UsingDecl>(child)) {
9806 if (ignore_using_decls)
9807 continue;
9808 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9809 clang::Decl *target = usd->getTargetDecl();
9810 if (clang::NamedDecl *nd =
9811 llvm::dyn_cast<clang::NamedDecl>(target)) {
9812 IdentifierInfo *ii = nd->getIdentifier();
9813 if (ii != nullptr &&
9814 ii->getName().equals(name.AsCString(nullptr)))
9815 found_decls.push_back(CompilerDecl(this, nd));
9816 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009817 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009818 } else if (clang::NamedDecl *nd =
9819 llvm::dyn_cast<clang::NamedDecl>(child)) {
9820 IdentifierInfo *ii = nd->getIdentifier();
9821 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9822 found_decls.push_back(CompilerDecl(this, nd));
9823 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009824 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009825 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009826 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009827 }
9828 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +00009829}
9830
Dawn Perchikb5925782015-12-12 19:31:41 +00009831// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009832// and return the number of levels it took to find it, or
9833// LLDB_INVALID_DECL_LEVEL
9834// if not found. If the decl was imported via a using declaration, its name
9835// and/or
9836// type, if set, will be used to check that the decl found in the scope is a
9837// match.
Dawn Perchikb5925782015-12-12 19:31:41 +00009838//
Kate Stoneb9c1b512016-09-06 20:57:50 +00009839// The optional name is required by languages (like C++) to handle using
9840// declarations
Dawn Perchikb5925782015-12-12 19:31:41 +00009841// like:
9842//
9843// void poo();
9844// namespace ns {
9845// void foo();
9846// void goo();
9847// }
9848// void bar() {
9849// using ns::foo;
9850// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
9851// // LLDB_INVALID_DECL_LEVEL for 'goo'.
9852// }
9853//
9854// The optional type is useful in the case that there's a specific overload
9855// that we're looking for that might otherwise be shadowed, like:
9856//
9857// void foo(int);
9858// namespace ns {
9859// void foo();
9860// }
9861// void bar() {
9862// using ns::foo;
9863// // CountDeclLevels returns 0 for { 'foo', void() },
9864// // 1 for { 'foo', void(int) }, and
9865// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
9866// }
9867//
9868// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +00009869// function at file scope will return the same level as a function at global
9870// scope.
9871// Ideally we'd like to treat the file scope as an additional scope just below
9872// the
9873// global scope. More work needs to be done to recognise that, if the decl
9874// we're
9875// trying to look up is static, we should compare its source file with that of
9876// the
Dawn Perchikb5925782015-12-12 19:31:41 +00009877// current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009878uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
9879 clang::DeclContext *child_decl_ctx,
9880 ConstString *child_name,
9881 CompilerType *child_type) {
9882 if (frame_decl_ctx) {
9883 std::set<DeclContext *> searched;
9884 std::multimap<DeclContext *, DeclContext *> search_queue;
9885 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +00009886
Kate Stoneb9c1b512016-09-06 20:57:50 +00009887 // Get the lookup scope for the decl we're trying to find.
9888 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +00009889
Kate Stoneb9c1b512016-09-06 20:57:50 +00009890 // Look for it in our scope's decl context and its parents.
9891 uint32_t level = 0;
9892 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
9893 decl_ctx = decl_ctx->getParent()) {
9894 if (!decl_ctx->isLookupContext())
9895 continue;
9896 if (decl_ctx == parent_decl_ctx)
9897 // Found it!
9898 return level;
9899 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
9900 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
9901 it++) {
9902 if (searched.find(it->second) != searched.end())
9903 continue;
9904
9905 // Currently DWARF has one shared translation unit for all Decls at top
9906 // level, so this
9907 // would erroneously find using statements anywhere. So don't look at
9908 // the top-level
9909 // translation unit.
9910 // TODO fix this and add a testcase that depends on it.
9911
9912 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
9913 continue;
9914
9915 searched.insert(it->second);
9916 symbol_file->ParseDeclsForContext(
9917 CompilerDeclContext(this, it->second));
9918
9919 for (clang::Decl *child : it->second->decls()) {
9920 if (clang::UsingDirectiveDecl *ud =
9921 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9922 clang::DeclContext *ns = ud->getNominatedNamespace();
9923 if (ns == parent_decl_ctx)
9924 // Found it!
9925 return level;
9926 clang::DeclContext *from = ud->getCommonAncestor();
9927 if (searched.find(ns) == searched.end())
9928 search_queue.insert(std::make_pair(from, ns));
9929 } else if (child_name) {
9930 if (clang::UsingDecl *ud =
9931 llvm::dyn_cast<clang::UsingDecl>(child)) {
9932 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9933 clang::Decl *target = usd->getTargetDecl();
9934 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
9935 if (!nd)
9936 continue;
9937 // Check names.
9938 IdentifierInfo *ii = nd->getIdentifier();
9939 if (ii == nullptr ||
9940 !ii->getName().equals(child_name->AsCString(nullptr)))
9941 continue;
9942 // Check types, if one was provided.
9943 if (child_type) {
9944 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
9945 if (!AreTypesSame(clang_type, *child_type,
9946 /*ignore_qualifiers=*/true))
9947 continue;
9948 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009949 // Found it!
9950 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009951 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009952 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009953 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009954 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009955 }
9956 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +00009957 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009958 }
9959 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +00009960}
9961
Kate Stoneb9c1b512016-09-06 20:57:50 +00009962bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
9963 if (opaque_decl_ctx)
9964 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
9965 else
Greg Clayton99558cc42015-08-24 23:46:31 +00009966 return false;
9967}
9968
Kate Stoneb9c1b512016-09-06 20:57:50 +00009969ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
9970 if (opaque_decl_ctx) {
9971 clang::NamedDecl *named_decl =
9972 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9973 if (named_decl)
9974 return ConstString(named_decl->getName());
9975 }
9976 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +00009977}
9978
Kate Stoneb9c1b512016-09-06 20:57:50 +00009979ConstString
9980ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
9981 if (opaque_decl_ctx) {
9982 clang::NamedDecl *named_decl =
9983 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9984 if (named_decl)
9985 return ConstString(
9986 llvm::StringRef(named_decl->getQualifiedNameAsString()));
9987 }
9988 return ConstString();
9989}
9990
9991bool ClangASTContext::DeclContextIsClassMethod(
9992 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
9993 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
9994 if (opaque_decl_ctx) {
9995 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9996 if (ObjCMethodDecl *objc_method =
9997 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
9998 if (is_instance_method_ptr)
9999 *is_instance_method_ptr = objc_method->isInstanceMethod();
10000 if (language_ptr)
10001 *language_ptr = eLanguageTypeObjC;
10002 if (language_object_name_ptr)
10003 language_object_name_ptr->SetCString("self");
10004 return true;
10005 } else if (CXXMethodDecl *cxx_method =
10006 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
10007 if (is_instance_method_ptr)
10008 *is_instance_method_ptr = cxx_method->isInstance();
10009 if (language_ptr)
10010 *language_ptr = eLanguageTypeC_plus_plus;
10011 if (language_object_name_ptr)
10012 language_object_name_ptr->SetCString("this");
10013 return true;
10014 } else if (clang::FunctionDecl *function_decl =
10015 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
10016 ClangASTMetadata *metadata =
10017 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
10018 if (metadata && metadata->HasObjectPtr()) {
10019 if (is_instance_method_ptr)
10020 *is_instance_method_ptr = true;
10021 if (language_ptr)
10022 *language_ptr = eLanguageTypeObjC;
10023 if (language_object_name_ptr)
10024 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10025 return true;
10026 }
10027 }
10028 }
10029 return false;
10030}
10031
10032clang::DeclContext *
10033ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10034 if (dc.IsClang())
10035 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10036 return nullptr;
10037}
Greg Clayton99558cc42015-08-24 23:46:31 +000010038
10039ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010040ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10041 if (dc.IsClang())
10042 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10043 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10044 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010045}
10046
10047CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010048ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10049 if (dc.IsClang())
10050 return llvm::dyn_cast<clang::CXXMethodDecl>(
10051 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10052 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010053}
10054
10055clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010056ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10057 if (dc.IsClang())
10058 return llvm::dyn_cast<clang::FunctionDecl>(
10059 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10060 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010061}
10062
10063clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010064ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10065 if (dc.IsClang())
10066 return llvm::dyn_cast<clang::NamespaceDecl>(
10067 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10068 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010069}
10070
10071ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010072ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10073 const void *object) {
10074 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10075 if (ast)
10076 return ClangASTContext::GetMetadata(ast, object);
10077 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010078}
10079
10080clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010081ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10082 ClangASTContext *ast =
10083 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10084 if (ast)
10085 return ast->getASTContext();
10086 return nullptr;
10087}
10088
10089ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10090 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10091 m_target_wp(target.shared_from_this()),
10092 m_persistent_variables(new ClangPersistentVariables) {}
10093
10094UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010095 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010096 Expression::ResultType desired_type,
10097 const EvaluateExpressionOptions &options) {
10098 TargetSP target_sp = m_target_wp.lock();
10099 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010100 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010101
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010102 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010103 desired_type, options);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010104}
10105
Kate Stoneb9c1b512016-09-06 20:57:50 +000010106FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10107 const CompilerType &return_type, const Address &function_address,
10108 const ValueList &arg_value_list, const char *name) {
10109 TargetSP target_sp = m_target_wp.lock();
10110 if (!target_sp)
10111 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010112
Kate Stoneb9c1b512016-09-06 20:57:50 +000010113 Process *process = target_sp->GetProcessSP().get();
10114 if (!process)
10115 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010116
Kate Stoneb9c1b512016-09-06 20:57:50 +000010117 return new ClangFunctionCaller(*process, return_type, function_address,
10118 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010119}
10120
10121UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010122ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10123 const char *name) {
10124 TargetSP target_sp = m_target_wp.lock();
10125 if (!target_sp)
10126 return nullptr;
10127
10128 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010129}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010130
10131PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010132ClangASTContextForExpressions::GetPersistentExpressionState() {
10133 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010134}
Sean Callanan68e44232017-09-28 20:20:25 +000010135
10136clang::ExternalASTMerger &
10137ClangASTContextForExpressions::GetMergerUnchecked() {
Eugene Zemtsova9d928c2017-09-29 03:15:08 +000010138 lldbassert(m_scratch_ast_source_ap != nullptr);
Sean Callanan68e44232017-09-28 20:20:25 +000010139 return m_scratch_ast_source_ap->GetMergerUnchecked();
10140}
10141