blob: 63d97af406dd20745969d9fad7c4612c3bbe6e81 [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/DataExtractor.h"
77#include "lldb/Core/DumpDataExtractor.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000078#include "lldb/Core/Module.h"
79#include "lldb/Core/PluginManager.h"
Ulrich Weigand9521ad22016-04-15 09:55:52 +000080#include "lldb/Core/Scalar.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000081#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000082#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000083#include "lldb/Core/UniqueCStringMap.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000084#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000085#include "lldb/Symbol/ClangASTImporter.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000086#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000087#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000088#include "lldb/Symbol/ClangUtil.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000089#include "lldb/Symbol/ObjectFile.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000090#include "lldb/Symbol/SymbolFile.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000091#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000092#include "lldb/Target/ExecutionContext.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000093#include "lldb/Target/Language.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000094#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham151c0322015-09-15 21:13:50 +000095#include "lldb/Target/Process.h"
96#include "lldb/Target/Target.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.
381 if (IK == IK_Asm) {
382 Opts.AsmPreprocessor = 1;
383 } else if (IK == IK_ObjC || IK == IK_ObjCXX || IK == IK_PreprocessedObjC ||
384 IK == IK_PreprocessedObjCXX) {
385 Opts.ObjC1 = Opts.ObjC2 = 1;
386 }
387
388 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
389
390 if (LangStd == LangStandard::lang_unspecified) {
391 // Based on the base language, pick one.
392 switch (IK) {
393 case IK_None:
394 case IK_AST:
395 case IK_LLVM_IR:
396 case IK_RenderScript:
David Blaikiea322f362017-01-06 00:38:06 +0000397 llvm_unreachable("Invalid input kind!");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398 case IK_OpenCL:
399 LangStd = LangStandard::lang_opencl;
400 break;
401 case IK_CUDA:
402 case IK_PreprocessedCuda:
403 LangStd = LangStandard::lang_cuda;
404 break;
405 case IK_Asm:
406 case IK_C:
407 case IK_PreprocessedC:
408 case IK_ObjC:
409 case IK_PreprocessedObjC:
410 LangStd = LangStandard::lang_gnu99;
411 break;
412 case IK_CXX:
413 case IK_PreprocessedCXX:
414 case IK_ObjCXX:
415 case IK_PreprocessedObjCXX:
416 LangStd = LangStandard::lang_gnucxx98;
417 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
422 Opts.LineComment = Std.hasLineComments();
423 Opts.C99 = Std.isC99();
424 Opts.CPlusPlus = Std.isCPlusPlus();
425 Opts.CPlusPlus11 = Std.isCPlusPlus11();
426 Opts.Digraphs = Std.hasDigraphs();
427 Opts.GNUMode = Std.isGNUMode();
428 Opts.GNUInline = !Std.isC99();
429 Opts.HexFloats = Std.hasHexFloats();
430 Opts.ImplicitInt = Std.hasImplicitInt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 // OpenCL has some additional defaults.
435 if (LangStd == LangStandard::lang_opencl) {
436 Opts.OpenCL = 1;
437 Opts.AltiVec = 1;
438 Opts.CXXOperatorNames = 1;
439 Opts.LaxVectorConversions = 1;
440 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442 // OpenCL and C++ both have bool, true, false keywords.
443 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445 // if (Opts.CPlusPlus)
446 // Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
447 //
448 // if (Args.hasArg(OPT_fobjc_gc_only))
449 // Opts.setGCMode(LangOptions::GCOnly);
450 // else if (Args.hasArg(OPT_fobjc_gc))
451 // Opts.setGCMode(LangOptions::HybridGC);
452 //
453 // if (Args.hasArg(OPT_print_ivar_layout))
454 // Opts.ObjCGCBitmapPrint = 1;
455 //
456 // if (Args.hasArg(OPT_faltivec))
457 // Opts.AltiVec = 1;
458 //
459 // if (Args.hasArg(OPT_pthread))
460 // Opts.POSIXThreads = 1;
461 //
462 // llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
463 // "default");
464 // if (Vis == "default")
465 Opts.setValueVisibilityMode(DefaultVisibility);
466 // else if (Vis == "hidden")
467 // Opts.setVisibilityMode(LangOptions::Hidden);
468 // else if (Vis == "protected")
469 // Opts.setVisibilityMode(LangOptions::Protected);
470 // else
471 // Diags.Report(diag::err_drv_invalid_value)
472 // << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474 // Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475
Kate Stoneb9c1b512016-09-06 20:57:50 +0000476 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
477 // is specified, or -std is set to a conforming mode.
478 Opts.Trigraphs = !Opts.GNUMode;
479 // if (Args.hasArg(OPT_trigraphs))
480 // Opts.Trigraphs = 1;
481 //
482 // Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
483 // OPT_fno_dollars_in_identifiers,
484 // !Opts.AsmPreprocessor);
485 // Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
486 // Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
487 // Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
488 // if (Args.hasArg(OPT_fno_lax_vector_conversions))
489 // Opts.LaxVectorConversions = 0;
490 // Opts.Exceptions = Args.hasArg(OPT_fexceptions);
491 // Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
492 // Opts.Blocks = Args.hasArg(OPT_fblocks);
493 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
494 // Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
495 // Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
496 // Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
497 // Opts.AssumeSaneOperatorNew =
498 // !Args.hasArg(OPT_fno_assume_sane_operator_new);
499 // Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
500 // Opts.AccessControl = Args.hasArg(OPT_faccess_control);
501 // Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
502 // Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
503 // Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth,
504 // 99,
505 // Diags);
506 // Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
507 // Opts.ObjCConstantStringClass = getLastArgValue(Args,
508 // OPT_fconstant_string_class);
509 // Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
510 // Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
511 // Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
512 // Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
513 // Opts.Static = Args.hasArg(OPT_static_define);
514 Opts.OptimizeSize = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 // FIXME: Eliminate this dependency.
517 // unsigned Opt =
518 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
519 // Opts.Optimize = Opt != 0;
520 unsigned Opt = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521
Kate Stoneb9c1b512016-09-06 20:57:50 +0000522 // This is the __NO_INLINE__ define, which just depends on things like the
523 // optimization level and -fno-inline, not actually whether the backend has
524 // inlining enabled.
525 //
526 // FIXME: This is affected by other options (-fno-inline).
527 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529 // unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
530 // switch (SSP) {
531 // default:
532 // Diags.Report(diag::err_drv_invalid_value)
533 // << Args.getLastArg(OPT_stack_protector)->getAsString(Args) <<
534 // SSP;
535 // break;
536 // case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
537 // case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
538 // case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
539 // }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540}
541
Kate Stoneb9c1b512016-09-06 20:57:50 +0000542ClangASTContext::ClangASTContext(const char *target_triple)
543 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_ap(),
544 m_language_options_ap(), m_source_manager_ap(), m_diagnostics_engine_ap(),
545 m_target_options_rp(), m_target_info_ap(), m_identifier_table_ap(),
546 m_selector_table_ap(), m_builtins_ap(), m_callback_tag_decl(nullptr),
547 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
548 m_pointer_byte_size(0), m_ast_owned(false) {
549 if (target_triple && target_triple[0])
550 SetTargetTriple(target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000551}
552
553//----------------------------------------------------------------------
554// Destructor
555//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556ClangASTContext::~ClangASTContext() { Finalize(); }
557
558ConstString ClangASTContext::GetPluginNameStatic() {
559 return ConstString("clang");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560}
561
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562ConstString ClangASTContext::GetPluginName() {
563 return ClangASTContext::GetPluginNameStatic();
Greg Clayton56939cb2015-09-17 22:23:34 +0000564}
565
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566uint32_t ClangASTContext::GetPluginVersion() { return 1; }
Greg Clayton56939cb2015-09-17 22:23:34 +0000567
Kate Stoneb9c1b512016-09-06 20:57:50 +0000568lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
569 lldb_private::Module *module,
570 Target *target) {
571 if (ClangASTContextSupportsLanguage(language)) {
572 ArchSpec arch;
573 if (module)
574 arch = module->GetArchitecture();
575 else if (target)
576 arch = target->GetArchitecture();
Greg Clayton56939cb2015-09-17 22:23:34 +0000577
Kate Stoneb9c1b512016-09-06 20:57:50 +0000578 if (arch.IsValid()) {
579 ArchSpec fixed_arch = arch;
580 // LLVM wants this to be set to iOS or MacOSX; if we're working on
581 // a bare-boards type image, change the triple for llvm's benefit.
582 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
583 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
584 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
585 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
586 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
587 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
588 } else {
589 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
Greg Clayton56939cb2015-09-17 22:23:34 +0000590 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000592
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593 if (module) {
594 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
595 if (ast_sp) {
596 ast_sp->SetArchitecture(fixed_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000597 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 return ast_sp;
599 } else if (target && target->IsValid()) {
600 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
601 new ClangASTContextForExpressions(*target));
602 if (ast_sp) {
603 ast_sp->SetArchitecture(fixed_arch);
604 ast_sp->m_scratch_ast_source_ap.reset(
605 new ClangASTSource(target->shared_from_this()));
606 ast_sp->m_scratch_ast_source_ap->InstallASTContext(
607 ast_sp->getASTContext());
608 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
609 ast_sp->m_scratch_ast_source_ap->CreateProxy());
610 ast_sp->SetExternalSource(proxy_ast_source);
611 return ast_sp;
612 }
613 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615 }
616 return lldb::TypeSystemSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617}
618
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619void ClangASTContext::EnumerateSupportedLanguages(
620 std::set<lldb::LanguageType> &languages_for_types,
621 std::set<lldb::LanguageType> &languages_for_expressions) {
622 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
623 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
624 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
625 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
626 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
627 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
628
629 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
630 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
631 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
632 lldb::eLanguageTypeC_plus_plus_14});
633
634 languages_for_types.insert(s_supported_languages_for_types.begin(),
635 s_supported_languages_for_types.end());
636 languages_for_expressions.insert(
637 s_supported_languages_for_expressions.begin(),
638 s_supported_languages_for_expressions.end());
Enrico Granata5d84a692014-08-19 21:46:37 +0000639}
640
Kate Stoneb9c1b512016-09-06 20:57:50 +0000641void ClangASTContext::Initialize() {
642 PluginManager::RegisterPlugin(GetPluginNameStatic(),
643 "clang base AST context plug-in",
644 CreateInstance, EnumerateSupportedLanguages);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000645}
646
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647void ClangASTContext::Terminate() {
648 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000649}
650
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651void ClangASTContext::Finalize() {
652 if (m_ast_ap.get()) {
653 GetASTMap().Erase(m_ast_ap.get());
654 if (!m_ast_owned)
655 m_ast_ap.release();
656 }
657
658 m_builtins_ap.reset();
659 m_selector_table_ap.reset();
660 m_identifier_table_ap.reset();
661 m_target_info_ap.reset();
662 m_target_options_rp.reset();
663 m_diagnostics_engine_ap.reset();
664 m_source_manager_ap.reset();
665 m_language_options_ap.reset();
666 m_ast_ap.reset();
667 m_scratch_ast_source_ap.reset();
668}
669
670void ClangASTContext::Clear() {
671 m_ast_ap.reset();
672 m_language_options_ap.reset();
673 m_source_manager_ap.reset();
674 m_diagnostics_engine_ap.reset();
675 m_target_options_rp.reset();
676 m_target_info_ap.reset();
677 m_identifier_table_ap.reset();
678 m_selector_table_ap.reset();
679 m_builtins_ap.reset();
680 m_pointer_byte_size = 0;
681}
682
683const char *ClangASTContext::GetTargetTriple() {
684 return m_target_triple.c_str();
685}
686
687void ClangASTContext::SetTargetTriple(const char *target_triple) {
688 Clear();
689 m_target_triple.assign(target_triple);
690}
691
692void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
693 SetTargetTriple(arch.GetTriple().str().c_str());
694}
695
696bool ClangASTContext::HasExternalSource() {
697 ASTContext *ast = getASTContext();
698 if (ast)
699 return ast->getExternalSource() != nullptr;
700 return false;
701}
702
703void ClangASTContext::SetExternalSource(
704 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) {
705 ASTContext *ast = getASTContext();
706 if (ast) {
707 ast->setExternalSource(ast_source_ap);
708 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
709 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
710 }
711}
712
713void ClangASTContext::RemoveExternalSource() {
714 ASTContext *ast = getASTContext();
715
716 if (ast) {
717 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
718 ast->setExternalSource(empty_ast_source_ap);
719 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
720 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
721 }
722}
723
724void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
725 if (!m_ast_owned) {
726 m_ast_ap.release();
727 }
728 m_ast_owned = false;
729 m_ast_ap.reset(ast_ctx);
730 GetASTMap().Insert(ast_ctx, this);
731}
732
733ASTContext *ClangASTContext::getASTContext() {
734 if (m_ast_ap.get() == nullptr) {
735 m_ast_owned = true;
736 m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
737 *getIdentifierTable(), *getSelectorTable(),
738 *getBuiltinContext()));
739
740 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
741
742 // This can be NULL if we don't know anything about the architecture or if
743 // the
744 // target for an architecture isn't enabled in the llvm/clang that we built
745 TargetInfo *target_info = getTargetInfo();
746 if (target_info)
747 m_ast_ap->InitBuiltinTypes(*target_info);
748
749 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
750 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
751 // m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000752 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000753
754 GetASTMap().Insert(m_ast_ap.get(), this);
755
756 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap(
757 new ClangExternalASTSourceCallbacks(
758 ClangASTContext::CompleteTagDecl,
759 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
760 ClangASTContext::LayoutRecordType, this));
761 SetExternalSource(ast_source_ap);
762 }
763 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000764}
765
Kate Stoneb9c1b512016-09-06 20:57:50 +0000766ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
767 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
768 return clang_ast;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000769}
770
Kate Stoneb9c1b512016-09-06 20:57:50 +0000771Builtin::Context *ClangASTContext::getBuiltinContext() {
772 if (m_builtins_ap.get() == nullptr)
773 m_builtins_ap.reset(new Builtin::Context());
774 return m_builtins_ap.get();
Sean Callanan79439e82010-11-18 02:56:27 +0000775}
776
Kate Stoneb9c1b512016-09-06 20:57:50 +0000777IdentifierTable *ClangASTContext::getIdentifierTable() {
778 if (m_identifier_table_ap.get() == nullptr)
779 m_identifier_table_ap.reset(
780 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
781 return m_identifier_table_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000782}
783
Kate Stoneb9c1b512016-09-06 20:57:50 +0000784LangOptions *ClangASTContext::getLanguageOptions() {
785 if (m_language_options_ap.get() == nullptr) {
786 m_language_options_ap.reset(new LangOptions());
787 ParseLangArgs(*m_language_options_ap, IK_ObjCXX, GetTargetTriple());
788 // InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
789 }
790 return m_language_options_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791}
792
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793SelectorTable *ClangASTContext::getSelectorTable() {
794 if (m_selector_table_ap.get() == nullptr)
795 m_selector_table_ap.reset(new SelectorTable());
796 return m_selector_table_ap.get();
Greg Claytonfe689042015-11-10 17:47:04 +0000797}
798
Kate Stoneb9c1b512016-09-06 20:57:50 +0000799clang::FileManager *ClangASTContext::getFileManager() {
800 if (m_file_manager_ap.get() == nullptr) {
801 clang::FileSystemOptions file_system_options;
802 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
803 }
804 return m_file_manager_ap.get();
805}
806
807clang::SourceManager *ClangASTContext::getSourceManager() {
808 if (m_source_manager_ap.get() == nullptr)
809 m_source_manager_ap.reset(
810 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
811 return m_source_manager_ap.get();
812}
813
814clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
815 if (m_diagnostics_engine_ap.get() == nullptr) {
816 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
817 m_diagnostics_engine_ap.reset(
818 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
819 }
820 return m_diagnostics_engine_ap.get();
821}
822
823clang::MangleContext *ClangASTContext::getMangleContext() {
824 if (m_mangle_ctx_ap.get() == nullptr)
825 m_mangle_ctx_ap.reset(getASTContext()->createMangleContext());
826 return m_mangle_ctx_ap.get();
827}
828
829class NullDiagnosticConsumer : public DiagnosticConsumer {
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000830public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000831 NullDiagnosticConsumer() {
832 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
833 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000834
Kate Stoneb9c1b512016-09-06 20:57:50 +0000835 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
836 const clang::Diagnostic &info) {
837 if (m_log) {
838 llvm::SmallVector<char, 32> diag_str(10);
839 info.FormatDiagnostic(diag_str);
840 diag_str.push_back('\0');
841 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000842 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000843 }
844
845 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
846 return new NullDiagnosticConsumer();
847 }
848
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000849private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000850 Log *m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000851};
852
Kate Stoneb9c1b512016-09-06 20:57:50 +0000853DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
854 if (m_diagnostic_consumer_ap.get() == nullptr)
855 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
856
857 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000858}
859
Kate Stoneb9c1b512016-09-06 20:57:50 +0000860std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
861 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) {
862 m_target_options_rp = std::make_shared<clang::TargetOptions>();
863 if (m_target_options_rp.get() != nullptr)
864 m_target_options_rp->Triple = m_target_triple;
865 }
866 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000867}
868
Kate Stoneb9c1b512016-09-06 20:57:50 +0000869TargetInfo *ClangASTContext::getTargetInfo() {
870 // target_triple should be something like "x86_64-apple-macosx"
871 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
872 m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
873 getTargetOptions()));
874 return m_target_info_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000875}
876
877#pragma mark Basic Types
878
Kate Stoneb9c1b512016-09-06 20:57:50 +0000879static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
880 ASTContext *ast, QualType qual_type) {
881 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
882 if (qual_type_bit_size == bit_size)
883 return true;
884 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000885}
Greg Clayton56939cb2015-09-17 22:23:34 +0000886
Greg Claytona1e5dc82015-08-11 22:53:00 +0000887CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000888ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
889 size_t bit_size) {
890 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
891 getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892}
893
Kate Stoneb9c1b512016-09-06 20:57:50 +0000894CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
895 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
896 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000897 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000898 switch (encoding) {
899 case eEncodingInvalid:
900 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
901 return CompilerType(ast, ast->VoidPtrTy);
902 break;
903
904 case eEncodingUint:
905 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
906 return CompilerType(ast, ast->UnsignedCharTy);
907 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
908 return CompilerType(ast, ast->UnsignedShortTy);
909 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
910 return CompilerType(ast, ast->UnsignedIntTy);
911 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
912 return CompilerType(ast, ast->UnsignedLongTy);
913 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
914 return CompilerType(ast, ast->UnsignedLongLongTy);
915 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
916 return CompilerType(ast, ast->UnsignedInt128Ty);
917 break;
918
919 case eEncodingSint:
920 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
921 return CompilerType(ast, ast->SignedCharTy);
922 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
923 return CompilerType(ast, ast->ShortTy);
924 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
925 return CompilerType(ast, ast->IntTy);
926 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
927 return CompilerType(ast, ast->LongTy);
928 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
929 return CompilerType(ast, ast->LongLongTy);
930 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
931 return CompilerType(ast, ast->Int128Ty);
932 break;
933
934 case eEncodingIEEE754:
935 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
936 return CompilerType(ast, ast->FloatTy);
937 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
938 return CompilerType(ast, ast->DoubleTy);
939 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
940 return CompilerType(ast, ast->LongDoubleTy);
941 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
942 return CompilerType(ast, ast->HalfTy);
943 break;
944
945 case eEncodingVector:
946 // Sanity check that bit_size is a multiple of 8's.
947 if (bit_size && !(bit_size & 0x7u))
948 return CompilerType(
949 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
950 break;
951 }
952
953 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000954}
955
Greg Clayton57ee3062013-07-11 22:46:58 +0000956lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000957ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) {
958 if (name) {
959 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
960 static TypeNameToBasicTypeMap g_type_map;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000961 static llvm::once_flag g_once_flag;
962 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000963 // "void"
Zachary Turner4fa098a2016-10-06 21:22:44 +0000964 g_type_map.Append(ConstString("void").GetStringRef(), eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000965
966 // "char"
Zachary Turner4fa098a2016-10-06 21:22:44 +0000967 g_type_map.Append(ConstString("char").GetStringRef(), eBasicTypeChar);
968 g_type_map.Append(ConstString("signed char").GetStringRef(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000969 eBasicTypeSignedChar);
Zachary Turner4fa098a2016-10-06 21:22:44 +0000970 g_type_map.Append(ConstString("unsigned char").GetStringRef(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000971 eBasicTypeUnsignedChar);
Zachary Turner4fa098a2016-10-06 21:22:44 +0000972 g_type_map.Append(ConstString("wchar_t").GetStringRef(), eBasicTypeWChar);
973 g_type_map.Append(ConstString("signed wchar_t").GetStringRef(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974 eBasicTypeSignedWChar);
Zachary Turner4fa098a2016-10-06 21:22:44 +0000975 g_type_map.Append(ConstString("unsigned wchar_t").GetStringRef(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000976 eBasicTypeUnsignedWChar);
977 // "short"
Zachary Turner4fa098a2016-10-06 21:22:44 +0000978 g_type_map.Append(ConstString("short").GetStringRef(), eBasicTypeShort);
979 g_type_map.Append(ConstString("short int").GetStringRef(),
980 eBasicTypeShort);
981 g_type_map.Append(ConstString("unsigned short").GetStringRef(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000982 eBasicTypeUnsignedShort);
Zachary Turner4fa098a2016-10-06 21:22:44 +0000983 g_type_map.Append(ConstString("unsigned short int").GetStringRef(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000984 eBasicTypeUnsignedShort);
985
986 // "int"
Zachary Turner4fa098a2016-10-06 21:22:44 +0000987 g_type_map.Append(ConstString("int").GetStringRef(), eBasicTypeInt);
988 g_type_map.Append(ConstString("signed int").GetStringRef(),
989 eBasicTypeInt);
990 g_type_map.Append(ConstString("unsigned int").GetStringRef(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991 eBasicTypeUnsignedInt);
Zachary Turner4fa098a2016-10-06 21:22:44 +0000992 g_type_map.Append(ConstString("unsigned").GetStringRef(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000993 eBasicTypeUnsignedInt);
994
995 // "long"
Zachary Turner4fa098a2016-10-06 21:22:44 +0000996 g_type_map.Append(ConstString("long").GetStringRef(), eBasicTypeLong);
997 g_type_map.Append(ConstString("long int").GetStringRef(), eBasicTypeLong);
998 g_type_map.Append(ConstString("unsigned long").GetStringRef(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000999 eBasicTypeUnsignedLong);
Zachary Turner4fa098a2016-10-06 21:22:44 +00001000 g_type_map.Append(ConstString("unsigned long int").GetStringRef(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001 eBasicTypeUnsignedLong);
1002
1003 // "long long"
Zachary Turner4fa098a2016-10-06 21:22:44 +00001004 g_type_map.Append(ConstString("long long").GetStringRef(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005 eBasicTypeLongLong);
Zachary Turner4fa098a2016-10-06 21:22:44 +00001006 g_type_map.Append(ConstString("long long int").GetStringRef(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001007 eBasicTypeLongLong);
Zachary Turner4fa098a2016-10-06 21:22:44 +00001008 g_type_map.Append(ConstString("unsigned long long").GetStringRef(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009 eBasicTypeUnsignedLongLong);
Zachary Turner4fa098a2016-10-06 21:22:44 +00001010 g_type_map.Append(ConstString("unsigned long long int").GetStringRef(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001011 eBasicTypeUnsignedLongLong);
1012
1013 // "int128"
Zachary Turner4fa098a2016-10-06 21:22:44 +00001014 g_type_map.Append(ConstString("__int128_t").GetStringRef(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001015 eBasicTypeInt128);
Zachary Turner4fa098a2016-10-06 21:22:44 +00001016 g_type_map.Append(ConstString("__uint128_t").GetStringRef(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001017 eBasicTypeUnsignedInt128);
1018
1019 // Miscellaneous
Zachary Turner4fa098a2016-10-06 21:22:44 +00001020 g_type_map.Append(ConstString("bool").GetStringRef(), eBasicTypeBool);
1021 g_type_map.Append(ConstString("float").GetStringRef(), eBasicTypeFloat);
1022 g_type_map.Append(ConstString("double").GetStringRef(), eBasicTypeDouble);
1023 g_type_map.Append(ConstString("long double").GetStringRef(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001024 eBasicTypeLongDouble);
Zachary Turner4fa098a2016-10-06 21:22:44 +00001025 g_type_map.Append(ConstString("id").GetStringRef(), eBasicTypeObjCID);
1026 g_type_map.Append(ConstString("SEL").GetStringRef(), eBasicTypeObjCSel);
1027 g_type_map.Append(ConstString("nullptr").GetStringRef(),
1028 eBasicTypeNullPtr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029 g_type_map.Sort();
1030 });
1031
Zachary Turner4fa098a2016-10-06 21:22:44 +00001032 return g_type_map.Find(name.GetStringRef(), eBasicTypeInvalid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001033 }
1034 return eBasicTypeInvalid;
Greg Clayton57ee3062013-07-11 22:46:58 +00001035}
1036
Kate Stoneb9c1b512016-09-06 20:57:50 +00001037CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1038 const ConstString &name) {
1039 if (ast) {
1040 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1041 return ClangASTContext::GetBasicType(ast, basic_type);
1042 }
1043 return CompilerType();
1044}
1045
1046uint32_t ClangASTContext::GetPointerByteSize() {
1047 if (m_pointer_byte_size == 0)
1048 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid)
1049 .GetPointerType()
1050 .GetByteSize(nullptr);
1051 return m_pointer_byte_size;
1052}
1053
1054CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1055 return GetBasicType(getASTContext(), basic_type);
1056}
1057
1058CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1059 lldb::BasicType basic_type) {
1060 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001061 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001062 lldb::opaque_compiler_type_t clang_type =
1063 GetOpaqueCompilerType(ast, basic_type);
1064
1065 if (clang_type)
1066 return CompilerType(GetASTContext(ast), clang_type);
1067 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001068}
1069
Kate Stoneb9c1b512016-09-06 20:57:50 +00001070CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1071 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1072 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001073
Kate Stoneb9c1b512016-09-06 20:57:50 +00001074#define streq(a, b) strcmp(a, b) == 0
1075 assert(ast != nullptr);
1076 if (ast) {
1077 switch (dw_ate) {
1078 default:
1079 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001080
Kate Stoneb9c1b512016-09-06 20:57:50 +00001081 case DW_ATE_address:
1082 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1083 return CompilerType(ast, ast->VoidPtrTy);
1084 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001085
Kate Stoneb9c1b512016-09-06 20:57:50 +00001086 case DW_ATE_boolean:
1087 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
1088 return CompilerType(ast, ast->BoolTy);
1089 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1090 return CompilerType(ast, ast->UnsignedCharTy);
1091 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1092 return CompilerType(ast, ast->UnsignedShortTy);
1093 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1094 return CompilerType(ast, ast->UnsignedIntTy);
1095 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001096
Kate Stoneb9c1b512016-09-06 20:57:50 +00001097 case DW_ATE_lo_user:
1098 // This has been seen to mean DW_AT_complex_integer
1099 if (type_name) {
1100 if (::strstr(type_name, "complex")) {
1101 CompilerType complex_int_clang_type =
1102 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1103 bit_size / 2);
1104 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1105 complex_int_clang_type)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001106 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001107 }
1108 break;
1109
1110 case DW_ATE_complex_float:
1111 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1112 return CompilerType(ast, ast->FloatComplexTy);
1113 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1114 return CompilerType(ast, ast->DoubleComplexTy);
1115 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1116 return CompilerType(ast, ast->LongDoubleComplexTy);
1117 else {
1118 CompilerType complex_float_clang_type =
1119 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1120 bit_size / 2);
1121 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1122 complex_float_clang_type)));
1123 }
1124 break;
1125
1126 case DW_ATE_float:
1127 if (streq(type_name, "float") &&
1128 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1129 return CompilerType(ast, ast->FloatTy);
1130 if (streq(type_name, "double") &&
1131 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1132 return CompilerType(ast, ast->DoubleTy);
1133 if (streq(type_name, "long double") &&
1134 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1135 return CompilerType(ast, ast->LongDoubleTy);
1136 // Fall back to not requiring a name match
1137 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1138 return CompilerType(ast, ast->FloatTy);
1139 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1140 return CompilerType(ast, ast->DoubleTy);
1141 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1142 return CompilerType(ast, ast->LongDoubleTy);
1143 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1144 return CompilerType(ast, ast->HalfTy);
1145 break;
1146
1147 case DW_ATE_signed:
1148 if (type_name) {
1149 if (streq(type_name, "wchar_t") &&
1150 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1151 (getTargetInfo() &&
1152 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1153 return CompilerType(ast, ast->WCharTy);
1154 if (streq(type_name, "void") &&
1155 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1156 return CompilerType(ast, ast->VoidTy);
1157 if (strstr(type_name, "long long") &&
1158 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1159 return CompilerType(ast, ast->LongLongTy);
1160 if (strstr(type_name, "long") &&
1161 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1162 return CompilerType(ast, ast->LongTy);
1163 if (strstr(type_name, "short") &&
1164 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1165 return CompilerType(ast, ast->ShortTy);
1166 if (strstr(type_name, "char")) {
1167 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1168 return CompilerType(ast, ast->CharTy);
1169 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1170 return CompilerType(ast, ast->SignedCharTy);
1171 }
1172 if (strstr(type_name, "int")) {
1173 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1174 return CompilerType(ast, ast->IntTy);
1175 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1176 return CompilerType(ast, ast->Int128Ty);
1177 }
1178 }
1179 // We weren't able to match up a type name, just search by size
1180 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1181 return CompilerType(ast, ast->CharTy);
1182 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1183 return CompilerType(ast, ast->ShortTy);
1184 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1185 return CompilerType(ast, ast->IntTy);
1186 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1187 return CompilerType(ast, ast->LongTy);
1188 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1189 return CompilerType(ast, ast->LongLongTy);
1190 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1191 return CompilerType(ast, ast->Int128Ty);
1192 break;
1193
1194 case DW_ATE_signed_char:
1195 if (ast->getLangOpts().CharIsSigned && type_name &&
1196 streq(type_name, "char")) {
1197 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1198 return CompilerType(ast, ast->CharTy);
1199 }
1200 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1201 return CompilerType(ast, ast->SignedCharTy);
1202 break;
1203
1204 case DW_ATE_unsigned:
1205 if (type_name) {
1206 if (streq(type_name, "wchar_t")) {
1207 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1208 if (!(getTargetInfo() &&
1209 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1210 return CompilerType(ast, ast->WCharTy);
1211 }
1212 }
1213 if (strstr(type_name, "long long")) {
1214 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1215 return CompilerType(ast, ast->UnsignedLongLongTy);
1216 } else if (strstr(type_name, "long")) {
1217 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1218 return CompilerType(ast, ast->UnsignedLongTy);
1219 } else if (strstr(type_name, "short")) {
1220 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1221 return CompilerType(ast, ast->UnsignedShortTy);
1222 } else if (strstr(type_name, "char")) {
1223 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1224 return CompilerType(ast, ast->UnsignedCharTy);
1225 } else if (strstr(type_name, "int")) {
1226 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1227 return CompilerType(ast, ast->UnsignedIntTy);
1228 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1229 return CompilerType(ast, ast->UnsignedInt128Ty);
1230 }
1231 }
1232 // We weren't able to match up a type name, just search by size
1233 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1234 return CompilerType(ast, ast->UnsignedCharTy);
1235 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1236 return CompilerType(ast, ast->UnsignedShortTy);
1237 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1238 return CompilerType(ast, ast->UnsignedIntTy);
1239 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1240 return CompilerType(ast, ast->UnsignedLongTy);
1241 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1242 return CompilerType(ast, ast->UnsignedLongLongTy);
1243 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1244 return CompilerType(ast, ast->UnsignedInt128Ty);
1245 break;
1246
1247 case DW_ATE_unsigned_char:
1248 if (!ast->getLangOpts().CharIsSigned && type_name &&
1249 streq(type_name, "char")) {
1250 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1251 return CompilerType(ast, ast->CharTy);
1252 }
1253 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1254 return CompilerType(ast, ast->UnsignedCharTy);
1255 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1256 return CompilerType(ast, ast->UnsignedShortTy);
1257 break;
1258
1259 case DW_ATE_imaginary_float:
1260 break;
1261
1262 case DW_ATE_UTF:
1263 if (type_name) {
1264 if (streq(type_name, "char16_t")) {
1265 return CompilerType(ast, ast->Char16Ty);
1266 } else if (streq(type_name, "char32_t")) {
1267 return CompilerType(ast, ast->Char32Ty);
1268 }
1269 }
1270 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001271 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001272 }
1273 // This assert should fire for anything that we don't catch above so we know
1274 // to fix any issues we run into.
1275 if (type_name) {
1276 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1277 "DW_TAG_base_type '%s' encoded with "
1278 "DW_ATE = 0x%x, bit_size = %u\n",
1279 type_name, dw_ate, bit_size);
1280 } else {
1281 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1282 "DW_TAG_base_type encoded with "
1283 "DW_ATE = 0x%x, bit_size = %u\n",
1284 dw_ate, bit_size);
1285 }
1286 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001287}
1288
Kate Stoneb9c1b512016-09-06 20:57:50 +00001289CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1290 if (ast)
1291 return CompilerType(ast, ast->UnknownAnyTy);
1292 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001293}
1294
Kate Stoneb9c1b512016-09-06 20:57:50 +00001295CompilerType ClangASTContext::GetCStringType(bool is_const) {
1296 ASTContext *ast = getASTContext();
1297 QualType char_type(ast->CharTy);
1298
1299 if (is_const)
1300 char_type.addConst();
1301
1302 return CompilerType(ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001303}
1304
Sean Callanan09ab4b72011-11-30 22:11:59 +00001305clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001306ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1307 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001308}
1309
Kate Stoneb9c1b512016-09-06 20:57:50 +00001310clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1311 clang::Decl *source_decl) {
1312 FileSystemOptions file_system_options;
1313 FileManager file_manager(file_system_options);
1314 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1315
1316 return importer.Import(source_decl);
Greg Clayton526e5af2010-11-13 03:52:47 +00001317}
1318
Kate Stoneb9c1b512016-09-06 20:57:50 +00001319bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1320 bool ignore_qualifiers) {
1321 ClangASTContext *ast =
1322 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1323 if (!ast || ast != type2.GetTypeSystem())
1324 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001325
Kate Stoneb9c1b512016-09-06 20:57:50 +00001326 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1327 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001328
Kate Stoneb9c1b512016-09-06 20:57:50 +00001329 QualType type1_qual = ClangUtil::GetQualType(type1);
1330 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001331
Kate Stoneb9c1b512016-09-06 20:57:50 +00001332 if (ignore_qualifiers) {
1333 type1_qual = type1_qual.getUnqualifiedType();
1334 type2_qual = type2_qual.getUnqualifiedType();
1335 }
1336
1337 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001338}
1339
Kate Stoneb9c1b512016-09-06 20:57:50 +00001340CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1341 if (clang::ObjCInterfaceDecl *interface_decl =
1342 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1343 return GetTypeForDecl(interface_decl);
1344 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1345 return GetTypeForDecl(tag_decl);
1346 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001347}
1348
Kate Stoneb9c1b512016-09-06 20:57:50 +00001349CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
1350 // No need to call the getASTContext() accessor (which can create the AST
1351 // if it isn't created yet, because we can't have created a decl in this
1352 // AST if our AST didn't already exist...
1353 ASTContext *ast = &decl->getASTContext();
1354 if (ast)
1355 return CompilerType(ast, ast->getTagDeclType(decl));
1356 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001357}
1358
Kate Stoneb9c1b512016-09-06 20:57:50 +00001359CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
1360 // No need to call the getASTContext() accessor (which can create the AST
1361 // if it isn't created yet, because we can't have created a decl in this
1362 // AST if our AST didn't already exist...
1363 ASTContext *ast = &decl->getASTContext();
1364 if (ast)
1365 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1366 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001367}
1368
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001369#pragma mark Structure, Unions, Classes
1370
Kate Stoneb9c1b512016-09-06 20:57:50 +00001371CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1372 AccessType access_type,
1373 const char *name, int kind,
1374 LanguageType language,
1375 ClangASTMetadata *metadata) {
1376 ASTContext *ast = getASTContext();
1377 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001378
Kate Stoneb9c1b512016-09-06 20:57:50 +00001379 if (decl_ctx == nullptr)
1380 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001381
Kate Stoneb9c1b512016-09-06 20:57:50 +00001382 if (language == eLanguageTypeObjC ||
1383 language == eLanguageTypeObjC_plus_plus) {
1384 bool isForwardDecl = true;
1385 bool isInternal = false;
1386 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1387 }
Greg Clayton9e409562010-07-28 02:04:09 +00001388
Kate Stoneb9c1b512016-09-06 20:57:50 +00001389 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1390 // we will need to update this code. I was told to currently always use
1391 // the CXXRecordDecl class since we often don't know from debug information
1392 // if something is struct or a class, so we default to always use the more
1393 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001394
Kate Stoneb9c1b512016-09-06 20:57:50 +00001395 bool is_anonymous = (!name) || (!name[0]);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001396
Kate Stoneb9c1b512016-09-06 20:57:50 +00001397 CXXRecordDecl *decl = CXXRecordDecl::Create(
1398 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1399 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1400
1401 if (is_anonymous)
1402 decl->setAnonymousStructOrUnion(true);
1403
1404 if (decl) {
1405 if (metadata)
1406 SetMetadata(ast, decl, *metadata);
1407
1408 if (access_type != eAccessNone)
1409 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1410
1411 if (decl_ctx)
1412 decl_ctx->addDecl(decl);
1413
1414 return CompilerType(ast, ast->getTagDeclType(decl));
1415 }
1416 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001417}
1418
Kate Stoneb9c1b512016-09-06 20:57:50 +00001419static TemplateParameterList *CreateTemplateParameterList(
1420 ASTContext *ast,
1421 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1422 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1423 const bool parameter_pack = false;
1424 const bool is_typename = false;
1425 const unsigned depth = 0;
1426 const size_t num_template_params = template_param_infos.GetSize();
1427 for (size_t i = 0; i < num_template_params; ++i) {
1428 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001429
Kate Stoneb9c1b512016-09-06 20:57:50 +00001430 IdentifierInfo *identifier_info = nullptr;
1431 if (name && name[0])
1432 identifier_info = &ast->Idents.get(name);
1433 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral) {
1434 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1435 *ast,
1436 ast->getTranslationUnitDecl(), // Is this the right decl context?,
1437 // SourceLocation StartLoc,
1438 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1439 template_param_infos.args[i].getIntegralType(), parameter_pack,
1440 nullptr));
1441
1442 } else {
1443 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1444 *ast,
1445 ast->getTranslationUnitDecl(), // Is this the right decl context?
1446 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1447 is_typename, parameter_pack));
1448 }
1449 }
1450
1451 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1452 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1453 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1454 SourceLocation(), requires_clause);
1455 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001456}
1457
Kate Stoneb9c1b512016-09-06 20:57:50 +00001458clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1459 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1460 const char *name, const TemplateParameterInfos &template_param_infos) {
1461 // /// \brief Create a function template node.
1462 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001463
Kate Stoneb9c1b512016-09-06 20:57:50 +00001464 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001465
Kate Stoneb9c1b512016-09-06 20:57:50 +00001466 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1467 ast, template_param_infos, template_param_decls);
1468 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1469 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1470 template_param_list, func_decl);
1471
1472 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1473 i < template_param_decl_count; ++i) {
1474 // TODO: verify which decl context we should put template_param_decls into..
1475 template_param_decls[i]->setDeclContext(func_decl);
1476 }
1477
1478 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001479}
1480
Kate Stoneb9c1b512016-09-06 20:57:50 +00001481void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1482 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1483 const TemplateParameterInfos &infos) {
1484 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001485
Kate Stoneb9c1b512016-09-06 20:57:50 +00001486 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1487 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001488}
1489
Kate Stoneb9c1b512016-09-06 20:57:50 +00001490ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1491 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1492 int kind, const TemplateParameterInfos &template_param_infos) {
1493 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001494
Kate Stoneb9c1b512016-09-06 20:57:50 +00001495 ClassTemplateDecl *class_template_decl = nullptr;
1496 if (decl_ctx == nullptr)
1497 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001498
Kate Stoneb9c1b512016-09-06 20:57:50 +00001499 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1500 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001501
Kate Stoneb9c1b512016-09-06 20:57:50 +00001502 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001503
Kate Stoneb9c1b512016-09-06 20:57:50 +00001504 for (NamedDecl *decl : result) {
1505 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001506 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001507 return class_template_decl;
1508 }
1509
1510 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1511
1512 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1513 ast, template_param_infos, template_param_decls);
1514
1515 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1516 *ast, (TagDecl::TagKind)kind,
1517 decl_ctx, // What decl context do we use here? TU? The actual decl
1518 // context?
1519 SourceLocation(), SourceLocation(), &identifier_info);
1520
1521 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1522 i < template_param_decl_count; ++i) {
1523 template_param_decls[i]->setDeclContext(template_cxx_decl);
1524 }
1525
1526 // With templated classes, we say that a class is templated with
1527 // specializations, but that the bare class has no functions.
1528 // template_cxx_decl->startDefinition();
1529 // template_cxx_decl->completeDefinition();
1530
1531 class_template_decl = ClassTemplateDecl::Create(
1532 *ast,
1533 decl_ctx, // What decl context do we use here? TU? The actual decl
1534 // context?
Pavel Labath4294de32017-01-12 10:44:16 +00001535 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001536
1537 if (class_template_decl) {
1538 if (access_type != eAccessNone)
1539 class_template_decl->setAccess(
1540 ConvertAccessTypeToAccessSpecifier(access_type));
1541
1542 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1543 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1544
1545 decl_ctx->addDecl(class_template_decl);
1546
Sean Callanan5e9e1992011-10-26 01:06:27 +00001547#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001548 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001549#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001550 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001551
Kate Stoneb9c1b512016-09-06 20:57:50 +00001552 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001553}
1554
Greg Claytonf0705c82011-10-22 03:33:13 +00001555ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001556ClangASTContext::CreateClassTemplateSpecializationDecl(
1557 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1558 const TemplateParameterInfos &template_param_infos) {
1559 ASTContext *ast = getASTContext();
1560 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1561 ClassTemplateSpecializationDecl::Create(
1562 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1563 SourceLocation(), class_template_decl, template_param_infos.args,
1564 nullptr);
1565
1566 class_template_specialization_decl->setSpecializationKind(
1567 TSK_ExplicitSpecialization);
1568
1569 return class_template_specialization_decl;
1570}
1571
1572CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1573 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1574 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001575 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001576 if (ast)
1577 return CompilerType(
1578 ast, ast->getTagDeclType(class_template_specialization_decl));
1579 }
1580 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001581}
1582
Kate Stoneb9c1b512016-09-06 20:57:50 +00001583static inline bool check_op_param(bool is_method,
1584 clang::OverloadedOperatorKind op_kind,
1585 bool unary, bool binary,
1586 uint32_t num_params) {
1587 // Special-case call since it can take any number of operands
1588 if (op_kind == OO_Call)
1589 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001590
Kate Stoneb9c1b512016-09-06 20:57:50 +00001591 // The parameter count doesn't include "this"
1592 if (is_method)
1593 ++num_params;
1594 if (num_params == 1)
1595 return unary;
1596 if (num_params == 2)
1597 return binary;
1598 else
Greg Clayton090d0982011-06-19 03:43:27 +00001599 return false;
1600}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001601
Kate Stoneb9c1b512016-09-06 20:57:50 +00001602bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1603 bool is_method, clang::OverloadedOperatorKind op_kind,
1604 uint32_t num_params) {
1605 switch (op_kind) {
1606 default:
1607 break;
1608 // C++ standard allows any number of arguments to new/delete
1609 case OO_New:
1610 case OO_Array_New:
1611 case OO_Delete:
1612 case OO_Array_Delete:
1613 return true;
1614 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001615
Kate Stoneb9c1b512016-09-06 20:57:50 +00001616#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1617 case OO_##Name: \
1618 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1619 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001620#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001621 default:
1622 break;
1623 }
1624 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001625}
1626
Greg Clayton57ee3062013-07-11 22:46:58 +00001627clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001628ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1629 clang::AccessSpecifier rhs) {
1630 // Make the access equal to the stricter of the field and the nested field's
1631 // access
1632 if (lhs == AS_none || rhs == AS_none)
1633 return AS_none;
1634 if (lhs == AS_private || rhs == AS_private)
1635 return AS_private;
1636 if (lhs == AS_protected || rhs == AS_protected)
1637 return AS_protected;
1638 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001639}
1640
Kate Stoneb9c1b512016-09-06 20:57:50 +00001641bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1642 uint32_t &bitfield_bit_size) {
1643 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001644}
1645
Kate Stoneb9c1b512016-09-06 20:57:50 +00001646bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1647 uint32_t &bitfield_bit_size) {
1648 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001649 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001650
Kate Stoneb9c1b512016-09-06 20:57:50 +00001651 if (field->isBitField()) {
1652 Expr *bit_width_expr = field->getBitWidth();
1653 if (bit_width_expr) {
1654 llvm::APSInt bit_width_apsint;
1655 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1656 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001657 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001658 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001659 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001660 }
1661 return false;
1662}
1663
1664bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1665 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001666 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001667
1668 if (!record_decl->field_empty())
1669 return true;
1670
1671 // No fields, lets check this is a CXX record and check the base classes
1672 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1673 if (cxx_record_decl) {
1674 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1675 for (base_class = cxx_record_decl->bases_begin(),
1676 base_class_end = cxx_record_decl->bases_end();
1677 base_class != base_class_end; ++base_class) {
1678 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1679 base_class->getType()->getAs<RecordType>()->getDecl());
1680 if (RecordHasFields(base_class_decl))
1681 return true;
1682 }
1683 }
1684 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001685}
1686
Greg Clayton8cf05932010-07-22 18:30:50 +00001687#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001688
Kate Stoneb9c1b512016-09-06 20:57:50 +00001689CompilerType ClangASTContext::CreateObjCClass(const char *name,
1690 DeclContext *decl_ctx,
1691 bool isForwardDecl,
1692 bool isInternal,
1693 ClangASTMetadata *metadata) {
1694 ASTContext *ast = getASTContext();
1695 assert(ast != nullptr);
1696 assert(name && name[0]);
1697 if (decl_ctx == nullptr)
1698 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001699
Kate Stoneb9c1b512016-09-06 20:57:50 +00001700 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1701 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1702 nullptr, SourceLocation(),
1703 /*isForwardDecl,*/
1704 isInternal);
1705
1706 if (decl && metadata)
1707 SetMetadata(ast, decl, *metadata);
1708
1709 return CompilerType(ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001710}
1711
Kate Stoneb9c1b512016-09-06 20:57:50 +00001712static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1713 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) ==
1714 false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001715}
1716
Greg Clayton57ee3062013-07-11 22:46:58 +00001717uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001718ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1719 bool omit_empty_base_classes) {
1720 uint32_t num_bases = 0;
1721 if (cxx_record_decl) {
1722 if (omit_empty_base_classes) {
1723 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1724 for (base_class = cxx_record_decl->bases_begin(),
1725 base_class_end = cxx_record_decl->bases_end();
1726 base_class != base_class_end; ++base_class) {
1727 // Skip empty base classes
1728 if (omit_empty_base_classes) {
1729 if (BaseSpecifierIsEmpty(base_class))
1730 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001731 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001732 ++num_bases;
1733 }
1734 } else
1735 num_bases = cxx_record_decl->getNumBases();
1736 }
1737 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001738}
1739
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001740#pragma mark Namespace Declarations
1741
1742NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001743ClangASTContext::GetUniqueNamespaceDeclaration(const char *name,
1744 DeclContext *decl_ctx) {
1745 NamespaceDecl *namespace_decl = nullptr;
1746 ASTContext *ast = getASTContext();
1747 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1748 if (decl_ctx == nullptr)
1749 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001750
Kate Stoneb9c1b512016-09-06 20:57:50 +00001751 if (name) {
1752 IdentifierInfo &identifier_info = ast->Idents.get(name);
1753 DeclarationName decl_name(&identifier_info);
1754 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1755 for (NamedDecl *decl : result) {
1756 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1757 if (namespace_decl)
1758 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001759 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001760
1761 namespace_decl =
1762 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1763 SourceLocation(), &identifier_info, nullptr);
1764
1765 decl_ctx->addDecl(namespace_decl);
1766 } else {
1767 if (decl_ctx == translation_unit_decl) {
1768 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1769 if (namespace_decl)
1770 return namespace_decl;
1771
1772 namespace_decl =
1773 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1774 SourceLocation(), nullptr, nullptr);
1775 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1776 translation_unit_decl->addDecl(namespace_decl);
1777 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1778 } else {
1779 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1780 if (parent_namespace_decl) {
1781 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1782 if (namespace_decl)
1783 return namespace_decl;
1784 namespace_decl =
1785 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1786 SourceLocation(), nullptr, nullptr);
1787 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1788 parent_namespace_decl->addDecl(namespace_decl);
1789 assert(namespace_decl ==
1790 parent_namespace_decl->getAnonymousNamespace());
1791 } else {
1792 // BAD!!!
1793 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001794 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001795 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001796#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001797 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001798#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001799 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001800}
1801
Kate Stoneb9c1b512016-09-06 20:57:50 +00001802NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1803 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) {
1804 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1805 if (ast_ctx == nullptr)
1806 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +00001807
Kate Stoneb9c1b512016-09-06 20:57:50 +00001808 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
Siva Chandra03ff5c82016-02-05 19:10:04 +00001809}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001810
Paul Hermand628cbb2015-09-15 23:44:17 +00001811clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001812ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1813 if (ctx != nullptr) {
1814 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1815 clang::SourceLocation());
1816 ctx->addDecl(decl);
1817 return decl;
1818 }
1819 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001820}
1821
Kate Stoneb9c1b512016-09-06 20:57:50 +00001822clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1823 clang::DeclContext *right,
1824 clang::DeclContext *root) {
1825 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001826 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001827
1828 std::set<clang::DeclContext *> path_left;
1829 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1830 path_left.insert(d);
1831
1832 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1833 if (path_left.find(d) != path_left.end())
1834 return d;
1835
1836 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00001837}
1838
Kate Stoneb9c1b512016-09-06 20:57:50 +00001839clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1840 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1841 if (decl_ctx != nullptr && ns_decl != nullptr) {
1842 clang::TranslationUnitDecl *translation_unit =
1843 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1844 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1845 *getASTContext(), decl_ctx, clang::SourceLocation(),
1846 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1847 clang::SourceLocation(), ns_decl,
1848 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
1849 decl_ctx->addDecl(using_decl);
1850 return using_decl;
1851 }
1852 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001853}
1854
1855clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001856ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1857 clang::NamedDecl *target) {
1858 if (current_decl_ctx != nullptr && target != nullptr) {
1859 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1860 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
1861 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1862 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1863 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
1864 target);
1865 using_decl->addShadowDecl(shadow_decl);
1866 current_decl_ctx->addDecl(using_decl);
1867 return using_decl;
1868 }
1869 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001870}
1871
Kate Stoneb9c1b512016-09-06 20:57:50 +00001872clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
1873 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
1874 if (decl_context != nullptr) {
1875 clang::VarDecl *var_decl = clang::VarDecl::Create(
1876 *getASTContext(), decl_context, clang::SourceLocation(),
1877 clang::SourceLocation(),
1878 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
1879 nullptr, clang::SC_None);
1880 var_decl->setAccess(clang::AS_public);
1881 decl_context->addDecl(var_decl);
1882 return var_decl;
1883 }
1884 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001885}
1886
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001887lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001888ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
1889 lldb::BasicType basic_type) {
1890 switch (basic_type) {
1891 case eBasicTypeVoid:
1892 return ast->VoidTy.getAsOpaquePtr();
1893 case eBasicTypeChar:
1894 return ast->CharTy.getAsOpaquePtr();
1895 case eBasicTypeSignedChar:
1896 return ast->SignedCharTy.getAsOpaquePtr();
1897 case eBasicTypeUnsignedChar:
1898 return ast->UnsignedCharTy.getAsOpaquePtr();
1899 case eBasicTypeWChar:
1900 return ast->getWCharType().getAsOpaquePtr();
1901 case eBasicTypeSignedWChar:
1902 return ast->getSignedWCharType().getAsOpaquePtr();
1903 case eBasicTypeUnsignedWChar:
1904 return ast->getUnsignedWCharType().getAsOpaquePtr();
1905 case eBasicTypeChar16:
1906 return ast->Char16Ty.getAsOpaquePtr();
1907 case eBasicTypeChar32:
1908 return ast->Char32Ty.getAsOpaquePtr();
1909 case eBasicTypeShort:
1910 return ast->ShortTy.getAsOpaquePtr();
1911 case eBasicTypeUnsignedShort:
1912 return ast->UnsignedShortTy.getAsOpaquePtr();
1913 case eBasicTypeInt:
1914 return ast->IntTy.getAsOpaquePtr();
1915 case eBasicTypeUnsignedInt:
1916 return ast->UnsignedIntTy.getAsOpaquePtr();
1917 case eBasicTypeLong:
1918 return ast->LongTy.getAsOpaquePtr();
1919 case eBasicTypeUnsignedLong:
1920 return ast->UnsignedLongTy.getAsOpaquePtr();
1921 case eBasicTypeLongLong:
1922 return ast->LongLongTy.getAsOpaquePtr();
1923 case eBasicTypeUnsignedLongLong:
1924 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1925 case eBasicTypeInt128:
1926 return ast->Int128Ty.getAsOpaquePtr();
1927 case eBasicTypeUnsignedInt128:
1928 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1929 case eBasicTypeBool:
1930 return ast->BoolTy.getAsOpaquePtr();
1931 case eBasicTypeHalf:
1932 return ast->HalfTy.getAsOpaquePtr();
1933 case eBasicTypeFloat:
1934 return ast->FloatTy.getAsOpaquePtr();
1935 case eBasicTypeDouble:
1936 return ast->DoubleTy.getAsOpaquePtr();
1937 case eBasicTypeLongDouble:
1938 return ast->LongDoubleTy.getAsOpaquePtr();
1939 case eBasicTypeFloatComplex:
1940 return ast->FloatComplexTy.getAsOpaquePtr();
1941 case eBasicTypeDoubleComplex:
1942 return ast->DoubleComplexTy.getAsOpaquePtr();
1943 case eBasicTypeLongDoubleComplex:
1944 return ast->LongDoubleComplexTy.getAsOpaquePtr();
1945 case eBasicTypeObjCID:
1946 return ast->getObjCIdType().getAsOpaquePtr();
1947 case eBasicTypeObjCClass:
1948 return ast->getObjCClassType().getAsOpaquePtr();
1949 case eBasicTypeObjCSel:
1950 return ast->getObjCSelType().getAsOpaquePtr();
1951 case eBasicTypeNullPtr:
1952 return ast->NullPtrTy.getAsOpaquePtr();
1953 default:
1954 return nullptr;
1955 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001956}
1957
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001958#pragma mark Function Types
1959
Pavel Labath1ac2b202016-08-15 14:32:32 +00001960clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00001961ClangASTContext::GetDeclarationName(const char *name,
1962 const CompilerType &function_clang_type) {
1963 if (!name || !name[0])
1964 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00001965
Kate Stoneb9c1b512016-09-06 20:57:50 +00001966 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
1967 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
1968 return DeclarationName(&getASTContext()->Idents.get(
1969 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00001970
Kate Stoneb9c1b512016-09-06 20:57:50 +00001971 // Check the number of operator parameters. Sometimes we have
1972 // seen bad DWARF that doesn't correctly describe operators and
1973 // if we try to create a method and add it to the class, clang
1974 // will assert and crash, so we need to make sure things are
1975 // acceptable.
1976 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
1977 const clang::FunctionProtoType *function_type =
1978 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
1979 if (function_type == nullptr)
1980 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00001981
Kate Stoneb9c1b512016-09-06 20:57:50 +00001982 const bool is_method = false;
1983 const unsigned int num_params = function_type->getNumParams();
1984 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1985 is_method, op_kind, num_params))
1986 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00001987
Kate Stoneb9c1b512016-09-06 20:57:50 +00001988 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00001989}
1990
Kate Stoneb9c1b512016-09-06 20:57:50 +00001991FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
1992 DeclContext *decl_ctx, const char *name,
1993 const CompilerType &function_clang_type, int storage, bool is_inline) {
1994 FunctionDecl *func_decl = nullptr;
1995 ASTContext *ast = getASTContext();
1996 if (decl_ctx == nullptr)
1997 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001998
Kate Stoneb9c1b512016-09-06 20:57:50 +00001999 const bool hasWrittenPrototype = true;
2000 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00002001
Kate Stoneb9c1b512016-09-06 20:57:50 +00002002 clang::DeclarationName declarationName =
2003 GetDeclarationName(name, function_clang_type);
2004 func_decl = FunctionDecl::Create(
2005 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2006 ClangUtil::GetQualType(function_clang_type), nullptr,
2007 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
2008 isConstexprSpecified);
2009 if (func_decl)
2010 decl_ctx->addDecl(func_decl);
2011
Sean Callanan5e9e1992011-10-26 01:06:27 +00002012#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00002013 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002014#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002015
2016 return func_decl;
2017}
2018
2019CompilerType ClangASTContext::CreateFunctionType(
2020 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
2021 unsigned num_args, bool is_variadic, unsigned type_quals) {
2022 if (ast == nullptr)
2023 return CompilerType(); // invalid AST
2024
2025 if (!result_type || !ClangUtil::IsClangType(result_type))
2026 return CompilerType(); // invalid return type
2027
2028 std::vector<QualType> qual_type_args;
2029 if (num_args > 0 && args == nullptr)
2030 return CompilerType(); // invalid argument array passed in
2031
2032 // Verify that all arguments are valid and the right type
2033 for (unsigned i = 0; i < num_args; ++i) {
2034 if (args[i]) {
2035 // Make sure we have a clang type in args[i] and not a type from another
2036 // language whose name might match
2037 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2038 lldbassert(is_clang_type);
2039 if (is_clang_type)
2040 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2041 else
2042 return CompilerType(); // invalid argument type (must be a clang type)
2043 } else
2044 return CompilerType(); // invalid argument type (empty)
2045 }
2046
2047 // TODO: Detect calling convention in DWARF?
2048 FunctionProtoType::ExtProtoInfo proto_info;
2049 proto_info.Variadic = is_variadic;
2050 proto_info.ExceptionSpec = EST_None;
2051 proto_info.TypeQuals = type_quals;
2052 proto_info.RefQualifier = RQ_None;
2053
2054 return CompilerType(ast,
2055 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2056 qual_type_args, proto_info));
2057}
2058
2059ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
2060 const char *name, const CompilerType &param_type, int storage) {
2061 ASTContext *ast = getASTContext();
2062 assert(ast != nullptr);
2063 return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(),
2064 SourceLocation(), SourceLocation(),
2065 name && name[0] ? &ast->Idents.get(name) : nullptr,
2066 ClangUtil::GetQualType(param_type), nullptr,
2067 (clang::StorageClass)storage, nullptr);
2068}
2069
2070void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2071 ParmVarDecl **params,
2072 unsigned num_params) {
2073 if (function_decl)
2074 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002075}
2076
Greg Claytona1e5dc82015-08-11 22:53:00 +00002077CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002078ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
2079 QualType block_type = m_ast_ap->getBlockPointerType(
2080 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002081
Kate Stoneb9c1b512016-09-06 20:57:50 +00002082 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002083}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002084
2085#pragma mark Array Types
2086
Kate Stoneb9c1b512016-09-06 20:57:50 +00002087CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2088 size_t element_count,
2089 bool is_vector) {
2090 if (element_type.IsValid()) {
2091 ASTContext *ast = getASTContext();
2092 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002093
Kate Stoneb9c1b512016-09-06 20:57:50 +00002094 if (is_vector) {
2095 return CompilerType(
2096 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2097 element_count));
2098 } else {
2099
2100 llvm::APInt ap_element_count(64, element_count);
2101 if (element_count == 0) {
2102 return CompilerType(ast, ast->getIncompleteArrayType(
2103 ClangUtil::GetQualType(element_type),
2104 clang::ArrayType::Normal, 0));
2105 } else {
2106 return CompilerType(
2107 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2108 ap_element_count,
2109 clang::ArrayType::Normal, 0));
2110 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002111 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002112 }
2113 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002114}
2115
Kate Stoneb9c1b512016-09-06 20:57:50 +00002116CompilerType ClangASTContext::CreateStructForIdentifier(
2117 const ConstString &type_name,
2118 const std::initializer_list<std::pair<const char *, CompilerType>>
2119 &type_fields,
2120 bool packed) {
2121 CompilerType type;
2122 if (!type_name.IsEmpty() &&
2123 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2124 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002125 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002126 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002127 }
2128
2129 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2130 clang::TTK_Struct, lldb::eLanguageTypeC);
2131 StartTagDeclarationDefinition(type);
2132 for (const auto &field : type_fields)
2133 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2134 0);
2135 if (packed)
2136 SetIsPacked(type);
2137 CompleteTagDeclarationDefinition(type);
2138 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002139}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002140
Kate Stoneb9c1b512016-09-06 20:57:50 +00002141CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
2142 const ConstString &type_name,
2143 const std::initializer_list<std::pair<const char *, CompilerType>>
2144 &type_fields,
2145 bool packed) {
2146 CompilerType type;
2147 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2148 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002149
Kate Stoneb9c1b512016-09-06 20:57:50 +00002150 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002151}
2152
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002153#pragma mark Enumeration Types
2154
Greg Claytona1e5dc82015-08-11 22:53:00 +00002155CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002156ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2157 const Declaration &decl,
2158 const CompilerType &integer_clang_type) {
2159 // TODO: Do something intelligent with the Declaration object passed in
2160 // like maybe filling in the SourceLocation with it...
2161 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002162
Kate Stoneb9c1b512016-09-06 20:57:50 +00002163 // TODO: ask about these...
2164 // const bool IsScoped = false;
2165 // const bool IsFixed = false;
2166
2167 EnumDecl *enum_decl = EnumDecl::Create(
2168 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2169 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
2170 false, // IsScoped
2171 false, // IsScopedUsingClassTag
2172 false); // IsFixed
2173
2174 if (enum_decl) {
2175 // TODO: check if we should be setting the promotion type too?
2176 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2177
2178 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2179
2180 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2181 }
2182 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002183}
2184
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002185// Disable this for now since I can't seem to get a nicely formatted float
2186// out of the APFloat class without just getting the float, double or quad
2187// and then using a formatted print on it which defeats the purpose. We ideally
2188// would like to get perfect string values for any kind of float semantics
2189// so we can support remote targets. The code below also requires a patch to
2190// llvm::APInt.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002191// bool
2192// ClangASTContext::ConvertFloatValueToString (ASTContext *ast,
2193// lldb::opaque_compiler_type_t clang_type, const uint8_t* bytes, size_t
2194// byte_size, int apint_byte_order, std::string &float_str)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002195//{
2196// uint32_t count = 0;
2197// bool is_complex = false;
2198// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
2199// {
2200// unsigned num_bytes_per_float = byte_size / count;
2201// unsigned num_bits_per_float = num_bytes_per_float * 8;
2202//
2203// float_str.clear();
2204// uint32_t i;
2205// for (i=0; i<count; i++)
2206// {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002207// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float,
2208// (APInt::ByteOrder)apint_byte_order);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002209// bool is_ieee = false;
2210// APFloat ap_float(ap_int, is_ieee);
2211// char s[1024];
2212// unsigned int hex_digits = 0;
2213// bool upper_case = false;
2214//
Kate Stoneb9c1b512016-09-06 20:57:50 +00002215// if (ap_float.convertToHexString(s, hex_digits, upper_case,
2216// APFloat::rmNearestTiesToEven) > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002217// {
2218// if (i > 0)
2219// float_str.append(", ");
2220// float_str.append(s);
2221// if (i == 1 && is_complex)
2222// float_str.append(1, 'i');
2223// }
2224// }
2225// return !float_str.empty();
2226// }
2227// return false;
2228//}
2229
Kate Stoneb9c1b512016-09-06 20:57:50 +00002230CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2231 size_t bit_size,
2232 bool is_signed) {
2233 if (ast) {
2234 if (is_signed) {
2235 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2236 return CompilerType(ast, ast->SignedCharTy);
2237
2238 if (bit_size == ast->getTypeSize(ast->ShortTy))
2239 return CompilerType(ast, ast->ShortTy);
2240
2241 if (bit_size == ast->getTypeSize(ast->IntTy))
2242 return CompilerType(ast, ast->IntTy);
2243
2244 if (bit_size == ast->getTypeSize(ast->LongTy))
2245 return CompilerType(ast, ast->LongTy);
2246
2247 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2248 return CompilerType(ast, ast->LongLongTy);
2249
2250 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2251 return CompilerType(ast, ast->Int128Ty);
2252 } else {
2253 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2254 return CompilerType(ast, ast->UnsignedCharTy);
2255
2256 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2257 return CompilerType(ast, ast->UnsignedShortTy);
2258
2259 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2260 return CompilerType(ast, ast->UnsignedIntTy);
2261
2262 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2263 return CompilerType(ast, ast->UnsignedLongTy);
2264
2265 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2266 return CompilerType(ast, ast->UnsignedLongLongTy);
2267
2268 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2269 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002270 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002271 }
2272 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002273}
2274
Kate Stoneb9c1b512016-09-06 20:57:50 +00002275CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2276 bool is_signed) {
2277 if (ast)
2278 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2279 is_signed);
2280 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002281}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002282
Kate Stoneb9c1b512016-09-06 20:57:50 +00002283void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2284 if (decl_ctx) {
2285 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002286
Kate Stoneb9c1b512016-09-06 20:57:50 +00002287 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2288 if (named_decl) {
2289 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2290 named_decl->getDeclName().getAsString().c_str());
2291 } else {
2292 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002293 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002294 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002295}
2296
Kate Stoneb9c1b512016-09-06 20:57:50 +00002297void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2298 if (decl == nullptr)
2299 return;
2300 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002301
Kate Stoneb9c1b512016-09-06 20:57:50 +00002302 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2303 if (record_decl) {
2304 printf("%20s: %s%s\n", decl->getDeclKindName(),
2305 record_decl->getDeclName().getAsString().c_str(),
2306 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002307
Kate Stoneb9c1b512016-09-06 20:57:50 +00002308 } else {
2309 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2310 if (named_decl) {
2311 printf("%20s: %s\n", decl->getDeclKindName(),
2312 named_decl->getDeclName().getAsString().c_str());
2313 } else {
2314 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002315 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002316 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002317}
2318
Kate Stoneb9c1b512016-09-06 20:57:50 +00002319bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2320 clang::Decl *rhs_decl) {
2321 if (lhs_decl && rhs_decl) {
2322 //----------------------------------------------------------------------
2323 // Make sure the decl kinds match first
2324 //----------------------------------------------------------------------
2325 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2326 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002327
Kate Stoneb9c1b512016-09-06 20:57:50 +00002328 if (lhs_decl_kind == rhs_decl_kind) {
2329 //------------------------------------------------------------------
2330 // Now check that the decl contexts kinds are all equivalent
2331 // before we have to check any names of the decl contexts...
2332 //------------------------------------------------------------------
2333 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2334 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2335 if (lhs_decl_ctx && rhs_decl_ctx) {
2336 while (1) {
2337 if (lhs_decl_ctx && rhs_decl_ctx) {
2338 const clang::Decl::Kind lhs_decl_ctx_kind =
2339 lhs_decl_ctx->getDeclKind();
2340 const clang::Decl::Kind rhs_decl_ctx_kind =
2341 rhs_decl_ctx->getDeclKind();
2342 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2343 lhs_decl_ctx = lhs_decl_ctx->getParent();
2344 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002345
Kate Stoneb9c1b512016-09-06 20:57:50 +00002346 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2347 break;
2348 } else
2349 return false;
2350 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002351 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002352 }
2353
2354 //--------------------------------------------------------------
2355 // Now make sure the name of the decls match
2356 //--------------------------------------------------------------
2357 clang::NamedDecl *lhs_named_decl =
2358 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2359 clang::NamedDecl *rhs_named_decl =
2360 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2361 if (lhs_named_decl && rhs_named_decl) {
2362 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2363 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2364 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2365 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2366 return false;
2367 } else
Greg Claytona2721472011-06-25 00:44:06 +00002368 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002369 } else
2370 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002371
Kate Stoneb9c1b512016-09-06 20:57:50 +00002372 //--------------------------------------------------------------
2373 // We know that the decl context kinds all match, so now we need
2374 // to make sure the names match as well
2375 //--------------------------------------------------------------
2376 lhs_decl_ctx = lhs_decl->getDeclContext();
2377 rhs_decl_ctx = rhs_decl->getDeclContext();
2378 while (1) {
2379 switch (lhs_decl_ctx->getDeclKind()) {
2380 case clang::Decl::TranslationUnit:
2381 // We don't care about the translation unit names
2382 return true;
2383 default: {
2384 clang::NamedDecl *lhs_named_decl =
2385 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2386 clang::NamedDecl *rhs_named_decl =
2387 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2388 if (lhs_named_decl && rhs_named_decl) {
2389 clang::DeclarationName lhs_decl_name =
2390 lhs_named_decl->getDeclName();
2391 clang::DeclarationName rhs_decl_name =
2392 rhs_named_decl->getDeclName();
2393 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2394 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2395 return false;
2396 } else
2397 return false;
2398 } else
2399 return false;
2400 } break;
2401 }
2402 lhs_decl_ctx = lhs_decl_ctx->getParent();
2403 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002404 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002405 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002406 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002407 }
2408 return false;
2409}
2410bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2411 clang::Decl *decl) {
2412 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002413 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002414
Kate Stoneb9c1b512016-09-06 20:57:50 +00002415 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002416
Kate Stoneb9c1b512016-09-06 20:57:50 +00002417 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002418 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002419
2420 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2421 if (tag_decl->isCompleteDefinition())
2422 return true;
2423
2424 if (!tag_decl->hasExternalLexicalStorage())
2425 return false;
2426
2427 ast_source->CompleteType(tag_decl);
2428
2429 return !tag_decl->getTypeForDecl()->isIncompleteType();
2430 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2431 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2432 if (objc_interface_decl->getDefinition())
2433 return true;
2434
2435 if (!objc_interface_decl->hasExternalLexicalStorage())
2436 return false;
2437
2438 ast_source->CompleteType(objc_interface_decl);
2439
2440 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2441 } else {
2442 return false;
2443 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002444}
2445
Kate Stoneb9c1b512016-09-06 20:57:50 +00002446void ClangASTContext::SetMetadataAsUserID(const void *object,
2447 user_id_t user_id) {
2448 ClangASTMetadata meta_data;
2449 meta_data.SetUserID(user_id);
2450 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002451}
2452
Kate Stoneb9c1b512016-09-06 20:57:50 +00002453void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2454 ClangASTMetadata &metadata) {
2455 ClangExternalASTSourceCommon *external_source =
2456 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2457
2458 if (external_source)
2459 external_source->SetMetadata(object, metadata);
2460}
2461
2462ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2463 const void *object) {
2464 ClangExternalASTSourceCommon *external_source =
2465 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2466
2467 if (external_source && external_source->HasMetadata(object))
2468 return external_source->GetMetadata(object);
2469 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002470 return nullptr;
2471}
2472
Kate Stoneb9c1b512016-09-06 20:57:50 +00002473clang::DeclContext *
2474ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2475 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2476}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002477
Kate Stoneb9c1b512016-09-06 20:57:50 +00002478clang::DeclContext *
2479ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2480 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2481}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002482
Kate Stoneb9c1b512016-09-06 20:57:50 +00002483bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2484 int kind) const {
2485 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2486 if (clang_type) {
2487 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2488 if (tag_type) {
2489 clang::TagDecl *tag_decl =
2490 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2491 if (tag_decl) {
2492 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2493 return true;
2494 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002495 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002496 }
2497 return false;
2498}
2499
2500bool ClangASTContext::SetDefaultAccessForRecordFields(
2501 clang::RecordDecl *record_decl, int default_accessibility,
2502 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2503 if (record_decl) {
2504 uint32_t field_idx;
2505 clang::RecordDecl::field_iterator field, field_end;
2506 for (field = record_decl->field_begin(),
2507 field_end = record_decl->field_end(), field_idx = 0;
2508 field != field_end; ++field, ++field_idx) {
2509 // If no accessibility was assigned, assign the correct one
2510 if (field_idx < num_assigned_accessibilities &&
2511 assigned_accessibilities[field_idx] == clang::AS_none)
2512 field->setAccess((clang::AccessSpecifier)default_accessibility);
2513 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002514 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002515 }
2516 return false;
2517}
2518
2519clang::DeclContext *
2520ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2521 return GetDeclContextForType(ClangUtil::GetQualType(type));
2522}
2523
2524clang::DeclContext *
2525ClangASTContext::GetDeclContextForType(clang::QualType type) {
2526 if (type.isNull())
2527 return nullptr;
2528
2529 clang::QualType qual_type = type.getCanonicalType();
2530 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2531 switch (type_class) {
2532 case clang::Type::ObjCInterface:
2533 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2534 ->getInterface();
2535 case clang::Type::ObjCObjectPointer:
2536 return GetDeclContextForType(
2537 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2538 ->getPointeeType());
2539 case clang::Type::Record:
2540 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2541 case clang::Type::Enum:
2542 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2543 case clang::Type::Typedef:
2544 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2545 ->getDecl()
2546 ->getUnderlyingType());
2547 case clang::Type::Auto:
2548 return GetDeclContextForType(
2549 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2550 case clang::Type::Elaborated:
2551 return GetDeclContextForType(
2552 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2553 case clang::Type::Paren:
2554 return GetDeclContextForType(
2555 llvm::cast<clang::ParenType>(qual_type)->desugar());
2556 default:
2557 break;
2558 }
2559 // No DeclContext in this type...
2560 return nullptr;
2561}
2562
2563static bool GetCompleteQualType(clang::ASTContext *ast,
2564 clang::QualType qual_type,
2565 bool allow_completion = true) {
2566 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2567 switch (type_class) {
2568 case clang::Type::ConstantArray:
2569 case clang::Type::IncompleteArray:
2570 case clang::Type::VariableArray: {
2571 const clang::ArrayType *array_type =
2572 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2573
2574 if (array_type)
2575 return GetCompleteQualType(ast, array_type->getElementType(),
2576 allow_completion);
2577 } break;
2578 case clang::Type::Record: {
2579 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2580 if (cxx_record_decl) {
2581 if (cxx_record_decl->hasExternalLexicalStorage()) {
2582 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2583 const bool fields_loaded =
2584 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2585 if (is_complete && fields_loaded)
2586 return true;
2587
2588 if (!allow_completion)
2589 return false;
2590
2591 // Call the field_begin() accessor to for it to use the external source
2592 // to load the fields...
2593 clang::ExternalASTSource *external_ast_source =
2594 ast->getExternalSource();
2595 if (external_ast_source) {
2596 external_ast_source->CompleteType(cxx_record_decl);
2597 if (cxx_record_decl->isCompleteDefinition()) {
2598 cxx_record_decl->field_begin();
2599 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2600 }
2601 }
2602 }
2603 }
2604 const clang::TagType *tag_type =
2605 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2606 return !tag_type->isIncompleteType();
2607 } break;
2608
2609 case clang::Type::Enum: {
2610 const clang::TagType *tag_type =
2611 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2612 if (tag_type) {
2613 clang::TagDecl *tag_decl = tag_type->getDecl();
2614 if (tag_decl) {
2615 if (tag_decl->getDefinition())
2616 return true;
2617
2618 if (!allow_completion)
2619 return false;
2620
2621 if (tag_decl->hasExternalLexicalStorage()) {
2622 if (ast) {
2623 clang::ExternalASTSource *external_ast_source =
2624 ast->getExternalSource();
2625 if (external_ast_source) {
2626 external_ast_source->CompleteType(tag_decl);
2627 return !tag_type->isIncompleteType();
2628 }
2629 }
2630 }
2631 return false;
2632 }
2633 }
2634
2635 } break;
2636 case clang::Type::ObjCObject:
2637 case clang::Type::ObjCInterface: {
2638 const clang::ObjCObjectType *objc_class_type =
2639 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2640 if (objc_class_type) {
2641 clang::ObjCInterfaceDecl *class_interface_decl =
2642 objc_class_type->getInterface();
2643 // We currently can't complete objective C types through the newly added
2644 // ASTContext
2645 // because it only supports TagDecl objects right now...
2646 if (class_interface_decl) {
2647 if (class_interface_decl->getDefinition())
2648 return true;
2649
2650 if (!allow_completion)
2651 return false;
2652
2653 if (class_interface_decl->hasExternalLexicalStorage()) {
2654 if (ast) {
2655 clang::ExternalASTSource *external_ast_source =
2656 ast->getExternalSource();
2657 if (external_ast_source) {
2658 external_ast_source->CompleteType(class_interface_decl);
2659 return !objc_class_type->isIncompleteType();
2660 }
2661 }
2662 }
2663 return false;
2664 }
2665 }
2666 } break;
2667
2668 case clang::Type::Typedef:
2669 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2670 ->getDecl()
2671 ->getUnderlyingType(),
2672 allow_completion);
2673
2674 case clang::Type::Auto:
2675 return GetCompleteQualType(
2676 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2677 allow_completion);
2678
2679 case clang::Type::Elaborated:
2680 return GetCompleteQualType(
2681 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2682 allow_completion);
2683
2684 case clang::Type::Paren:
2685 return GetCompleteQualType(
2686 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2687 allow_completion);
2688
2689 case clang::Type::Attributed:
2690 return GetCompleteQualType(
2691 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2692 allow_completion);
2693
2694 default:
2695 break;
2696 }
2697
2698 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002699}
2700
2701static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002702ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2703 switch (access) {
2704 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002705 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002706 case eAccessPublic:
2707 return clang::ObjCIvarDecl::Public;
2708 case eAccessPrivate:
2709 return clang::ObjCIvarDecl::Private;
2710 case eAccessProtected:
2711 return clang::ObjCIvarDecl::Protected;
2712 case eAccessPackage:
2713 return clang::ObjCIvarDecl::Package;
2714 }
2715 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002716}
2717
Greg Claytond8d4a572015-08-11 21:38:15 +00002718//----------------------------------------------------------------------
2719// Tests
2720//----------------------------------------------------------------------
2721
Kate Stoneb9c1b512016-09-06 20:57:50 +00002722bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2723 clang::QualType qual_type(GetCanonicalQualType(type));
2724
2725 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2726 switch (type_class) {
2727 case clang::Type::IncompleteArray:
2728 case clang::Type::VariableArray:
2729 case clang::Type::ConstantArray:
2730 case clang::Type::ExtVector:
2731 case clang::Type::Vector:
2732 case clang::Type::Record:
2733 case clang::Type::ObjCObject:
2734 case clang::Type::ObjCInterface:
2735 return true;
2736 case clang::Type::Auto:
2737 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2738 ->getDeducedType()
2739 .getAsOpaquePtr());
2740 case clang::Type::Elaborated:
2741 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2742 ->getNamedType()
2743 .getAsOpaquePtr());
2744 case clang::Type::Typedef:
2745 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2746 ->getDecl()
2747 ->getUnderlyingType()
2748 .getAsOpaquePtr());
2749 case clang::Type::Paren:
2750 return IsAggregateType(
2751 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2752 default:
2753 break;
2754 }
2755 // The clang type does have a value
2756 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002757}
2758
Kate Stoneb9c1b512016-09-06 20:57:50 +00002759bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2760 clang::QualType qual_type(GetCanonicalQualType(type));
2761
2762 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2763 switch (type_class) {
2764 case clang::Type::Record: {
2765 if (const clang::RecordType *record_type =
2766 llvm::dyn_cast_or_null<clang::RecordType>(
2767 qual_type.getTypePtrOrNull())) {
2768 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2769 return record_decl->isAnonymousStructOrUnion();
2770 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002771 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002772 break;
2773 }
2774 case clang::Type::Auto:
2775 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2776 ->getDeducedType()
2777 .getAsOpaquePtr());
2778 case clang::Type::Elaborated:
2779 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2780 ->getNamedType()
2781 .getAsOpaquePtr());
2782 case clang::Type::Typedef:
2783 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2784 ->getDecl()
2785 ->getUnderlyingType()
2786 .getAsOpaquePtr());
2787 case clang::Type::Paren:
2788 return IsAnonymousType(
2789 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2790 default:
2791 break;
2792 }
2793 // The clang type does have a value
2794 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002795}
2796
Kate Stoneb9c1b512016-09-06 20:57:50 +00002797bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2798 CompilerType *element_type_ptr,
2799 uint64_t *size, bool *is_incomplete) {
2800 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002801
Kate Stoneb9c1b512016-09-06 20:57:50 +00002802 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2803 switch (type_class) {
2804 default:
2805 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002806
Kate Stoneb9c1b512016-09-06 20:57:50 +00002807 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002808 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002809 element_type_ptr->SetCompilerType(
2810 getASTContext(),
2811 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002812 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002813 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2814 ->getSize()
2815 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002816 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002817 *is_incomplete = false;
2818 return true;
2819
2820 case clang::Type::IncompleteArray:
2821 if (element_type_ptr)
2822 element_type_ptr->SetCompilerType(
2823 getASTContext(),
2824 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2825 if (size)
2826 *size = 0;
2827 if (is_incomplete)
2828 *is_incomplete = true;
2829 return true;
2830
2831 case clang::Type::VariableArray:
2832 if (element_type_ptr)
2833 element_type_ptr->SetCompilerType(
2834 getASTContext(),
2835 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2836 if (size)
2837 *size = 0;
2838 if (is_incomplete)
2839 *is_incomplete = false;
2840 return true;
2841
2842 case clang::Type::DependentSizedArray:
2843 if (element_type_ptr)
2844 element_type_ptr->SetCompilerType(
2845 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2846 ->getElementType());
2847 if (size)
2848 *size = 0;
2849 if (is_incomplete)
2850 *is_incomplete = false;
2851 return true;
2852
2853 case clang::Type::Typedef:
2854 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2855 ->getDecl()
2856 ->getUnderlyingType()
2857 .getAsOpaquePtr(),
2858 element_type_ptr, size, is_incomplete);
2859 case clang::Type::Auto:
2860 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2861 ->getDeducedType()
2862 .getAsOpaquePtr(),
2863 element_type_ptr, size, is_incomplete);
2864 case clang::Type::Elaborated:
2865 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2866 ->getNamedType()
2867 .getAsOpaquePtr(),
2868 element_type_ptr, size, is_incomplete);
2869 case clang::Type::Paren:
2870 return IsArrayType(
2871 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2872 element_type_ptr, size, is_incomplete);
2873 }
2874 if (element_type_ptr)
2875 element_type_ptr->Clear();
2876 if (size)
2877 *size = 0;
2878 if (is_incomplete)
2879 *is_incomplete = false;
2880 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002881}
2882
Kate Stoneb9c1b512016-09-06 20:57:50 +00002883bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2884 CompilerType *element_type, uint64_t *size) {
2885 clang::QualType qual_type(GetCanonicalQualType(type));
2886
2887 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2888 switch (type_class) {
2889 case clang::Type::Vector: {
2890 const clang::VectorType *vector_type =
2891 qual_type->getAs<clang::VectorType>();
2892 if (vector_type) {
2893 if (size)
2894 *size = vector_type->getNumElements();
2895 if (element_type)
2896 *element_type =
2897 CompilerType(getASTContext(), vector_type->getElementType());
2898 }
2899 return true;
2900 } break;
2901 case clang::Type::ExtVector: {
2902 const clang::ExtVectorType *ext_vector_type =
2903 qual_type->getAs<clang::ExtVectorType>();
2904 if (ext_vector_type) {
2905 if (size)
2906 *size = ext_vector_type->getNumElements();
2907 if (element_type)
2908 *element_type =
2909 CompilerType(getASTContext(), ext_vector_type->getElementType());
2910 }
2911 return true;
2912 }
2913 default:
2914 break;
2915 }
2916 return false;
2917}
2918
2919bool ClangASTContext::IsRuntimeGeneratedType(
2920 lldb::opaque_compiler_type_t type) {
2921 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
2922 ->GetDeclContextForType(GetQualType(type));
2923 if (!decl_ctx)
2924 return false;
2925
2926 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2927 return false;
2928
2929 clang::ObjCInterfaceDecl *result_iface_decl =
2930 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2931
2932 ClangASTMetadata *ast_metadata =
2933 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2934 if (!ast_metadata)
2935 return false;
2936 return (ast_metadata->GetISAPtr() != 0);
2937}
2938
2939bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
2940 return GetQualType(type).getUnqualifiedType()->isCharType();
2941}
2942
2943bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
2944 const bool allow_completion = false;
2945 return GetCompleteQualType(getASTContext(), GetQualType(type),
2946 allow_completion);
2947}
2948
2949bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
2950 return GetQualType(type).isConstQualified();
2951}
2952
2953bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
2954 uint32_t &length) {
2955 CompilerType pointee_or_element_clang_type;
2956 length = 0;
2957 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2958
2959 if (!pointee_or_element_clang_type.IsValid())
2960 return false;
2961
2962 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2963 if (pointee_or_element_clang_type.IsCharType()) {
2964 if (type_flags.Test(eTypeIsArray)) {
2965 // We know the size of the array and it could be a C string
2966 // since it is an array of characters
2967 length = llvm::cast<clang::ConstantArrayType>(
2968 GetCanonicalQualType(type).getTypePtr())
2969 ->getSize()
2970 .getLimitedValue();
2971 }
2972 return true;
2973 }
2974 }
2975 return false;
2976}
2977
2978bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
2979 bool *is_variadic_ptr) {
2980 if (type) {
2981 clang::QualType qual_type(GetCanonicalQualType(type));
2982
2983 if (qual_type->isFunctionType()) {
2984 if (is_variadic_ptr) {
2985 const clang::FunctionProtoType *function_proto_type =
2986 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2987 if (function_proto_type)
2988 *is_variadic_ptr = function_proto_type->isVariadic();
2989 else
2990 *is_variadic_ptr = false;
2991 }
2992 return true;
2993 }
2994
Greg Claytond8d4a572015-08-11 21:38:15 +00002995 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002996 switch (type_class) {
2997 default:
2998 break;
2999 case clang::Type::Typedef:
3000 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
3001 ->getDecl()
3002 ->getUnderlyingType()
3003 .getAsOpaquePtr(),
3004 nullptr);
3005 case clang::Type::Auto:
3006 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3007 ->getDeducedType()
3008 .getAsOpaquePtr(),
3009 nullptr);
3010 case clang::Type::Elaborated:
3011 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3012 ->getNamedType()
3013 .getAsOpaquePtr(),
3014 nullptr);
3015 case clang::Type::Paren:
3016 return IsFunctionType(
3017 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3018 nullptr);
3019 case clang::Type::LValueReference:
3020 case clang::Type::RValueReference: {
3021 const clang::ReferenceType *reference_type =
3022 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3023 if (reference_type)
3024 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3025 nullptr);
3026 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00003027 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003028 }
3029 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003030}
3031
3032// Used to detect "Homogeneous Floating-point Aggregates"
3033uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003034ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3035 CompilerType *base_type_ptr) {
3036 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003037 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003038
3039 clang::QualType qual_type(GetCanonicalQualType(type));
3040 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3041 switch (type_class) {
3042 case clang::Type::Record:
3043 if (GetCompleteType(type)) {
3044 const clang::CXXRecordDecl *cxx_record_decl =
3045 qual_type->getAsCXXRecordDecl();
3046 if (cxx_record_decl) {
3047 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3048 return 0;
3049 }
3050 const clang::RecordType *record_type =
3051 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3052 if (record_type) {
3053 const clang::RecordDecl *record_decl = record_type->getDecl();
3054 if (record_decl) {
3055 // We are looking for a structure that contains only floating point
3056 // types
3057 clang::RecordDecl::field_iterator field_pos,
3058 field_end = record_decl->field_end();
3059 uint32_t num_fields = 0;
3060 bool is_hva = false;
3061 bool is_hfa = false;
3062 clang::QualType base_qual_type;
3063 uint64_t base_bitwidth = 0;
3064 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3065 ++field_pos) {
3066 clang::QualType field_qual_type = field_pos->getType();
3067 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3068 if (field_qual_type->isFloatingType()) {
3069 if (field_qual_type->isComplexType())
3070 return 0;
3071 else {
3072 if (num_fields == 0)
3073 base_qual_type = field_qual_type;
3074 else {
3075 if (is_hva)
3076 return 0;
3077 is_hfa = true;
3078 if (field_qual_type.getTypePtr() !=
3079 base_qual_type.getTypePtr())
3080 return 0;
3081 }
3082 }
3083 } else if (field_qual_type->isVectorType() ||
3084 field_qual_type->isExtVectorType()) {
3085 if (num_fields == 0) {
3086 base_qual_type = field_qual_type;
3087 base_bitwidth = field_bitwidth;
3088 } else {
3089 if (is_hfa)
3090 return 0;
3091 is_hva = true;
3092 if (base_bitwidth != field_bitwidth)
3093 return 0;
3094 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3095 return 0;
3096 }
3097 } else
3098 return 0;
3099 ++num_fields;
3100 }
3101 if (base_type_ptr)
3102 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3103 return num_fields;
3104 }
3105 }
3106 }
3107 break;
3108
3109 case clang::Type::Typedef:
3110 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3111 ->getDecl()
3112 ->getUnderlyingType()
3113 .getAsOpaquePtr(),
3114 base_type_ptr);
3115
3116 case clang::Type::Auto:
3117 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3118 ->getDeducedType()
3119 .getAsOpaquePtr(),
3120 base_type_ptr);
3121
3122 case clang::Type::Elaborated:
3123 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3124 ->getNamedType()
3125 .getAsOpaquePtr(),
3126 base_type_ptr);
3127 default:
3128 break;
3129 }
3130 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003131}
3132
Kate Stoneb9c1b512016-09-06 20:57:50 +00003133size_t ClangASTContext::GetNumberOfFunctionArguments(
3134 lldb::opaque_compiler_type_t type) {
3135 if (type) {
3136 clang::QualType qual_type(GetCanonicalQualType(type));
3137 const clang::FunctionProtoType *func =
3138 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3139 if (func)
3140 return func->getNumParams();
3141 }
3142 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003143}
3144
Greg Claytona1e5dc82015-08-11 22:53:00 +00003145CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003146ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3147 const size_t index) {
3148 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003149 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003150 const clang::FunctionProtoType *func =
3151 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3152 if (func) {
3153 if (index < func->getNumParams())
3154 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003155 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003156 }
3157 return CompilerType();
3158}
3159
3160bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3161 if (type) {
3162 clang::QualType qual_type(GetCanonicalQualType(type));
3163
3164 if (qual_type->isFunctionPointerType())
3165 return true;
3166
3167 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3168 switch (type_class) {
3169 default:
3170 break;
3171 case clang::Type::Typedef:
3172 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3173 ->getDecl()
3174 ->getUnderlyingType()
3175 .getAsOpaquePtr());
3176 case clang::Type::Auto:
3177 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3178 ->getDeducedType()
3179 .getAsOpaquePtr());
3180 case clang::Type::Elaborated:
3181 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3182 ->getNamedType()
3183 .getAsOpaquePtr());
3184 case clang::Type::Paren:
3185 return IsFunctionPointerType(
3186 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3187
3188 case clang::Type::LValueReference:
3189 case clang::Type::RValueReference: {
3190 const clang::ReferenceType *reference_type =
3191 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3192 if (reference_type)
3193 return IsFunctionPointerType(
3194 reference_type->getPointeeType().getAsOpaquePtr());
3195 } break;
3196 }
3197 }
3198 return false;
3199}
3200
3201bool ClangASTContext::IsBlockPointerType(
3202 lldb::opaque_compiler_type_t type,
3203 CompilerType *function_pointer_type_ptr) {
3204 if (type) {
3205 clang::QualType qual_type(GetCanonicalQualType(type));
3206
3207 if (qual_type->isBlockPointerType()) {
3208 if (function_pointer_type_ptr) {
3209 const clang::BlockPointerType *block_pointer_type =
3210 qual_type->getAs<clang::BlockPointerType>();
3211 QualType pointee_type = block_pointer_type->getPointeeType();
3212 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3213 *function_pointer_type_ptr =
3214 CompilerType(getASTContext(), function_pointer_type);
3215 }
3216 return true;
3217 }
3218
3219 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3220 switch (type_class) {
3221 default:
3222 break;
3223 case clang::Type::Typedef:
3224 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3225 ->getDecl()
3226 ->getUnderlyingType()
3227 .getAsOpaquePtr(),
3228 function_pointer_type_ptr);
3229 case clang::Type::Auto:
3230 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3231 ->getDeducedType()
3232 .getAsOpaquePtr(),
3233 function_pointer_type_ptr);
3234 case clang::Type::Elaborated:
3235 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3236 ->getNamedType()
3237 .getAsOpaquePtr(),
3238 function_pointer_type_ptr);
3239 case clang::Type::Paren:
3240 return IsBlockPointerType(
3241 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3242 function_pointer_type_ptr);
3243
3244 case clang::Type::LValueReference:
3245 case clang::Type::RValueReference: {
3246 const clang::ReferenceType *reference_type =
3247 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3248 if (reference_type)
3249 return IsBlockPointerType(
3250 reference_type->getPointeeType().getAsOpaquePtr(),
3251 function_pointer_type_ptr);
3252 } break;
3253 }
3254 }
3255 return false;
3256}
3257
3258bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3259 bool &is_signed) {
3260 if (!type)
3261 return false;
3262
3263 clang::QualType qual_type(GetCanonicalQualType(type));
3264 const clang::BuiltinType *builtin_type =
3265 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3266
3267 if (builtin_type) {
3268 if (builtin_type->isInteger()) {
3269 is_signed = builtin_type->isSignedInteger();
3270 return true;
3271 }
3272 }
3273
3274 return false;
3275}
3276
3277bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3278 bool &is_signed) {
3279 if (type) {
3280 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3281 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3282
3283 if (enum_type) {
3284 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3285 is_signed);
3286 return true;
3287 }
3288 }
3289
3290 return false;
3291}
3292
3293bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3294 CompilerType *pointee_type) {
3295 if (type) {
3296 clang::QualType qual_type(GetCanonicalQualType(type));
3297 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3298 switch (type_class) {
3299 case clang::Type::Builtin:
3300 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3301 default:
3302 break;
3303 case clang::BuiltinType::ObjCId:
3304 case clang::BuiltinType::ObjCClass:
3305 return true;
3306 }
3307 return false;
3308 case clang::Type::ObjCObjectPointer:
3309 if (pointee_type)
3310 pointee_type->SetCompilerType(
3311 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3312 ->getPointeeType());
3313 return true;
3314 case clang::Type::BlockPointer:
3315 if (pointee_type)
3316 pointee_type->SetCompilerType(
3317 getASTContext(),
3318 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3319 return true;
3320 case clang::Type::Pointer:
3321 if (pointee_type)
3322 pointee_type->SetCompilerType(
3323 getASTContext(),
3324 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3325 return true;
3326 case clang::Type::MemberPointer:
3327 if (pointee_type)
3328 pointee_type->SetCompilerType(
3329 getASTContext(),
3330 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3331 return true;
3332 case clang::Type::Typedef:
3333 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3334 ->getDecl()
3335 ->getUnderlyingType()
3336 .getAsOpaquePtr(),
3337 pointee_type);
3338 case clang::Type::Auto:
3339 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3340 ->getDeducedType()
3341 .getAsOpaquePtr(),
3342 pointee_type);
3343 case clang::Type::Elaborated:
3344 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3345 ->getNamedType()
3346 .getAsOpaquePtr(),
3347 pointee_type);
3348 case clang::Type::Paren:
3349 return IsPointerType(
3350 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3351 pointee_type);
3352 default:
3353 break;
3354 }
3355 }
3356 if (pointee_type)
3357 pointee_type->Clear();
3358 return false;
3359}
3360
3361bool ClangASTContext::IsPointerOrReferenceType(
3362 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3363 if (type) {
3364 clang::QualType qual_type(GetCanonicalQualType(type));
3365 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3366 switch (type_class) {
3367 case clang::Type::Builtin:
3368 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3369 default:
3370 break;
3371 case clang::BuiltinType::ObjCId:
3372 case clang::BuiltinType::ObjCClass:
3373 return true;
3374 }
3375 return false;
3376 case clang::Type::ObjCObjectPointer:
3377 if (pointee_type)
3378 pointee_type->SetCompilerType(
3379 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3380 ->getPointeeType());
3381 return true;
3382 case clang::Type::BlockPointer:
3383 if (pointee_type)
3384 pointee_type->SetCompilerType(
3385 getASTContext(),
3386 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3387 return true;
3388 case clang::Type::Pointer:
3389 if (pointee_type)
3390 pointee_type->SetCompilerType(
3391 getASTContext(),
3392 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3393 return true;
3394 case clang::Type::MemberPointer:
3395 if (pointee_type)
3396 pointee_type->SetCompilerType(
3397 getASTContext(),
3398 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3399 return true;
3400 case clang::Type::LValueReference:
3401 if (pointee_type)
3402 pointee_type->SetCompilerType(
3403 getASTContext(),
3404 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3405 return true;
3406 case clang::Type::RValueReference:
3407 if (pointee_type)
3408 pointee_type->SetCompilerType(
3409 getASTContext(),
3410 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3411 return true;
3412 case clang::Type::Typedef:
3413 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3414 ->getDecl()
3415 ->getUnderlyingType()
3416 .getAsOpaquePtr(),
3417 pointee_type);
3418 case clang::Type::Auto:
3419 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3420 ->getDeducedType()
3421 .getAsOpaquePtr(),
3422 pointee_type);
3423 case clang::Type::Elaborated:
3424 return IsPointerOrReferenceType(
3425 llvm::cast<clang::ElaboratedType>(qual_type)
3426 ->getNamedType()
3427 .getAsOpaquePtr(),
3428 pointee_type);
3429 case clang::Type::Paren:
3430 return IsPointerOrReferenceType(
3431 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3432 pointee_type);
3433 default:
3434 break;
3435 }
3436 }
3437 if (pointee_type)
3438 pointee_type->Clear();
3439 return false;
3440}
3441
3442bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3443 CompilerType *pointee_type,
3444 bool *is_rvalue) {
3445 if (type) {
3446 clang::QualType qual_type(GetCanonicalQualType(type));
3447 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3448
3449 switch (type_class) {
3450 case clang::Type::LValueReference:
3451 if (pointee_type)
3452 pointee_type->SetCompilerType(
3453 getASTContext(),
3454 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3455 if (is_rvalue)
3456 *is_rvalue = false;
3457 return true;
3458 case clang::Type::RValueReference:
3459 if (pointee_type)
3460 pointee_type->SetCompilerType(
3461 getASTContext(),
3462 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3463 if (is_rvalue)
3464 *is_rvalue = true;
3465 return true;
3466 case clang::Type::Typedef:
3467 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3468 ->getDecl()
3469 ->getUnderlyingType()
3470 .getAsOpaquePtr(),
3471 pointee_type, is_rvalue);
3472 case clang::Type::Auto:
3473 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3474 ->getDeducedType()
3475 .getAsOpaquePtr(),
3476 pointee_type, is_rvalue);
3477 case clang::Type::Elaborated:
3478 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3479 ->getNamedType()
3480 .getAsOpaquePtr(),
3481 pointee_type, is_rvalue);
3482 case clang::Type::Paren:
3483 return IsReferenceType(
3484 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3485 pointee_type, is_rvalue);
3486
3487 default:
3488 break;
3489 }
3490 }
3491 if (pointee_type)
3492 pointee_type->Clear();
3493 return false;
3494}
3495
3496bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3497 uint32_t &count, bool &is_complex) {
3498 if (type) {
3499 clang::QualType qual_type(GetCanonicalQualType(type));
3500
3501 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3502 qual_type->getCanonicalTypeInternal())) {
3503 clang::BuiltinType::Kind kind = BT->getKind();
3504 if (kind >= clang::BuiltinType::Float &&
3505 kind <= clang::BuiltinType::LongDouble) {
3506 count = 1;
3507 is_complex = false;
3508 return true;
3509 }
3510 } else if (const clang::ComplexType *CT =
3511 llvm::dyn_cast<clang::ComplexType>(
3512 qual_type->getCanonicalTypeInternal())) {
3513 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3514 is_complex)) {
3515 count = 2;
3516 is_complex = true;
3517 return true;
3518 }
3519 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3520 qual_type->getCanonicalTypeInternal())) {
3521 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3522 is_complex)) {
3523 count = VT->getNumElements();
3524 is_complex = false;
3525 return true;
3526 }
3527 }
3528 }
3529 count = 0;
3530 is_complex = false;
3531 return false;
3532}
3533
3534bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3535 if (!type)
3536 return false;
3537
3538 clang::QualType qual_type(GetQualType(type));
3539 const clang::TagType *tag_type =
3540 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3541 if (tag_type) {
3542 clang::TagDecl *tag_decl = tag_type->getDecl();
3543 if (tag_decl)
3544 return tag_decl->isCompleteDefinition();
3545 return false;
3546 } else {
3547 const clang::ObjCObjectType *objc_class_type =
3548 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3549 if (objc_class_type) {
3550 clang::ObjCInterfaceDecl *class_interface_decl =
3551 objc_class_type->getInterface();
3552 if (class_interface_decl)
3553 return class_interface_decl->getDefinition() != nullptr;
3554 return false;
3555 }
3556 }
3557 return true;
3558}
3559
3560bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3561 if (type) {
3562 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3563
3564 const clang::ObjCObjectPointerType *obj_pointer_type =
3565 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3566
3567 if (obj_pointer_type)
3568 return obj_pointer_type->isObjCClassType();
3569 }
3570 return false;
3571}
3572
3573bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3574 if (ClangUtil::IsClangType(type))
3575 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3576 return false;
3577}
3578
3579bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3580 if (!type)
3581 return false;
3582 clang::QualType qual_type(GetCanonicalQualType(type));
3583 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3584 return (type_class == clang::Type::Record);
3585}
3586
3587bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3588 if (!type)
3589 return false;
3590 clang::QualType qual_type(GetCanonicalQualType(type));
3591 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3592 return (type_class == clang::Type::Enum);
3593}
3594
3595bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3596 if (type) {
3597 clang::QualType qual_type(GetCanonicalQualType(type));
3598 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3599 switch (type_class) {
3600 case clang::Type::Record:
3601 if (GetCompleteType(type)) {
3602 const clang::RecordType *record_type =
3603 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3604 const clang::RecordDecl *record_decl = record_type->getDecl();
3605 if (record_decl) {
3606 const clang::CXXRecordDecl *cxx_record_decl =
3607 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3608 if (cxx_record_decl)
3609 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003610 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003611 }
3612 break;
3613
3614 default:
3615 break;
3616 }
3617 }
3618 return false;
3619}
3620
3621bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3622 CompilerType *dynamic_pointee_type,
3623 bool check_cplusplus,
3624 bool check_objc) {
3625 clang::QualType pointee_qual_type;
3626 if (type) {
3627 clang::QualType qual_type(GetCanonicalQualType(type));
3628 bool success = false;
3629 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3630 switch (type_class) {
3631 case clang::Type::Builtin:
3632 if (check_objc &&
3633 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3634 clang::BuiltinType::ObjCId) {
3635 if (dynamic_pointee_type)
3636 dynamic_pointee_type->SetCompilerType(this, type);
3637 return true;
3638 }
3639 break;
3640
3641 case clang::Type::ObjCObjectPointer:
3642 if (check_objc) {
3643 if (auto objc_pointee_type =
3644 qual_type->getPointeeType().getTypePtrOrNull()) {
3645 if (auto objc_object_type =
3646 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3647 objc_pointee_type)) {
3648 if (objc_object_type->isObjCClass())
3649 return false;
3650 }
3651 }
3652 if (dynamic_pointee_type)
3653 dynamic_pointee_type->SetCompilerType(
3654 getASTContext(),
3655 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3656 ->getPointeeType());
3657 return true;
3658 }
3659 break;
3660
3661 case clang::Type::Pointer:
3662 pointee_qual_type =
3663 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3664 success = true;
3665 break;
3666
3667 case clang::Type::LValueReference:
3668 case clang::Type::RValueReference:
3669 pointee_qual_type =
3670 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3671 success = true;
3672 break;
3673
3674 case clang::Type::Typedef:
3675 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3676 ->getDecl()
3677 ->getUnderlyingType()
3678 .getAsOpaquePtr(),
3679 dynamic_pointee_type, check_cplusplus,
3680 check_objc);
3681
3682 case clang::Type::Auto:
3683 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3684 ->getDeducedType()
3685 .getAsOpaquePtr(),
3686 dynamic_pointee_type, check_cplusplus,
3687 check_objc);
3688
3689 case clang::Type::Elaborated:
3690 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3691 ->getNamedType()
3692 .getAsOpaquePtr(),
3693 dynamic_pointee_type, check_cplusplus,
3694 check_objc);
3695
3696 case clang::Type::Paren:
3697 return IsPossibleDynamicType(
3698 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3699 dynamic_pointee_type, check_cplusplus, check_objc);
3700 default:
3701 break;
3702 }
3703
3704 if (success) {
3705 // Check to make sure what we are pointing too is a possible dynamic C++
3706 // type
3707 // We currently accept any "void *" (in case we have a class that has been
3708 // watered down to an opaque pointer) and virtual C++ classes.
3709 const clang::Type::TypeClass pointee_type_class =
3710 pointee_qual_type.getCanonicalType()->getTypeClass();
3711 switch (pointee_type_class) {
3712 case clang::Type::Builtin:
3713 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3714 case clang::BuiltinType::UnknownAny:
3715 case clang::BuiltinType::Void:
3716 if (dynamic_pointee_type)
3717 dynamic_pointee_type->SetCompilerType(getASTContext(),
3718 pointee_qual_type);
3719 return true;
3720 default:
3721 break;
3722 }
3723 break;
3724
3725 case clang::Type::Record:
3726 if (check_cplusplus) {
3727 clang::CXXRecordDecl *cxx_record_decl =
3728 pointee_qual_type->getAsCXXRecordDecl();
3729 if (cxx_record_decl) {
3730 bool is_complete = cxx_record_decl->isCompleteDefinition();
3731
3732 if (is_complete)
3733 success = cxx_record_decl->isDynamicClass();
3734 else {
3735 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3736 getASTContext(), cxx_record_decl);
3737 if (metadata)
3738 success = metadata->GetIsDynamicCXXType();
3739 else {
3740 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3741 .GetCompleteType();
3742 if (is_complete)
3743 success = cxx_record_decl->isDynamicClass();
3744 else
3745 success = false;
3746 }
3747 }
3748
3749 if (success) {
3750 if (dynamic_pointee_type)
3751 dynamic_pointee_type->SetCompilerType(getASTContext(),
3752 pointee_qual_type);
3753 return true;
3754 }
3755 }
3756 }
3757 break;
3758
3759 case clang::Type::ObjCObject:
3760 case clang::Type::ObjCInterface:
3761 if (check_objc) {
3762 if (dynamic_pointee_type)
3763 dynamic_pointee_type->SetCompilerType(getASTContext(),
3764 pointee_qual_type);
3765 return true;
3766 }
3767 break;
3768
3769 default:
3770 break;
3771 }
3772 }
3773 }
3774 if (dynamic_pointee_type)
3775 dynamic_pointee_type->Clear();
3776 return false;
3777}
3778
3779bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3780 if (!type)
3781 return false;
3782
3783 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3784}
3785
3786bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3787 if (!type)
3788 return false;
3789 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3790}
3791
3792bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3793 if (!type)
3794 return false;
3795 return GetCanonicalQualType(type)->isVoidType();
3796}
3797
3798bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3799 return ClangASTContextSupportsLanguage(language);
3800}
3801
3802bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3803 std::string &class_name) {
3804 if (type) {
3805 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3806 if (!qual_type.isNull()) {
3807 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3808 if (cxx_record_decl) {
3809 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3810 return true;
3811 }
3812 }
3813 }
3814 class_name.clear();
3815 return false;
3816}
3817
3818bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3819 if (!type)
3820 return false;
3821
3822 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3823 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
3824 return true;
3825 return false;
3826}
3827
3828bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3829 if (!type)
3830 return false;
3831 clang::QualType qual_type(GetCanonicalQualType(type));
3832 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3833 if (tag_type)
3834 return tag_type->isBeingDefined();
3835 return false;
3836}
3837
3838bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3839 CompilerType *class_type_ptr) {
3840 if (!type)
3841 return false;
3842
3843 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3844
3845 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3846 if (class_type_ptr) {
3847 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3848 const clang::ObjCObjectPointerType *obj_pointer_type =
3849 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3850 if (obj_pointer_type == nullptr)
3851 class_type_ptr->Clear();
3852 else
3853 class_type_ptr->SetCompilerType(
3854 type.GetTypeSystem(),
3855 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3856 .getAsOpaquePtr());
3857 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003858 }
3859 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003860 }
3861 if (class_type_ptr)
3862 class_type_ptr->Clear();
3863 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003864}
3865
Kate Stoneb9c1b512016-09-06 20:57:50 +00003866bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3867 std::string &class_name) {
3868 if (!type)
3869 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003870
Kate Stoneb9c1b512016-09-06 20:57:50 +00003871 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3872
3873 const clang::ObjCObjectType *object_type =
3874 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3875 if (object_type) {
3876 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3877 if (interface) {
3878 class_name = interface->getNameAsString();
3879 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00003880 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003881 }
3882 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003883}
3884
Greg Claytond8d4a572015-08-11 21:38:15 +00003885//----------------------------------------------------------------------
3886// Type Completion
3887//----------------------------------------------------------------------
3888
Kate Stoneb9c1b512016-09-06 20:57:50 +00003889bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3890 if (!type)
3891 return false;
3892 const bool allow_completion = true;
3893 return GetCompleteQualType(getASTContext(), GetQualType(type),
3894 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00003895}
3896
Kate Stoneb9c1b512016-09-06 20:57:50 +00003897ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
3898 std::string type_name;
3899 if (type) {
3900 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
3901 clang::QualType qual_type(GetQualType(type));
3902 printing_policy.SuppressTagKeyword = true;
3903 const clang::TypedefType *typedef_type =
3904 qual_type->getAs<clang::TypedefType>();
3905 if (typedef_type) {
3906 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3907 type_name = typedef_decl->getQualifiedNameAsString();
3908 } else {
3909 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003910 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003911 }
3912 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00003913}
3914
3915uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003916ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
3917 CompilerType *pointee_or_element_clang_type) {
3918 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003919 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003920
3921 if (pointee_or_element_clang_type)
3922 pointee_or_element_clang_type->Clear();
3923
3924 clang::QualType qual_type(GetQualType(type));
3925
3926 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3927 switch (type_class) {
3928 case clang::Type::Builtin: {
3929 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3930 qual_type->getCanonicalTypeInternal());
3931
3932 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3933 switch (builtin_type->getKind()) {
3934 case clang::BuiltinType::ObjCId:
3935 case clang::BuiltinType::ObjCClass:
3936 if (pointee_or_element_clang_type)
3937 pointee_or_element_clang_type->SetCompilerType(
3938 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
3939 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3940 break;
3941
3942 case clang::BuiltinType::ObjCSel:
3943 if (pointee_or_element_clang_type)
3944 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
3945 getASTContext()->CharTy);
3946 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3947 break;
3948
3949 case clang::BuiltinType::Bool:
3950 case clang::BuiltinType::Char_U:
3951 case clang::BuiltinType::UChar:
3952 case clang::BuiltinType::WChar_U:
3953 case clang::BuiltinType::Char16:
3954 case clang::BuiltinType::Char32:
3955 case clang::BuiltinType::UShort:
3956 case clang::BuiltinType::UInt:
3957 case clang::BuiltinType::ULong:
3958 case clang::BuiltinType::ULongLong:
3959 case clang::BuiltinType::UInt128:
3960 case clang::BuiltinType::Char_S:
3961 case clang::BuiltinType::SChar:
3962 case clang::BuiltinType::WChar_S:
3963 case clang::BuiltinType::Short:
3964 case clang::BuiltinType::Int:
3965 case clang::BuiltinType::Long:
3966 case clang::BuiltinType::LongLong:
3967 case clang::BuiltinType::Int128:
3968 case clang::BuiltinType::Float:
3969 case clang::BuiltinType::Double:
3970 case clang::BuiltinType::LongDouble:
3971 builtin_type_flags |= eTypeIsScalar;
3972 if (builtin_type->isInteger()) {
3973 builtin_type_flags |= eTypeIsInteger;
3974 if (builtin_type->isSignedInteger())
3975 builtin_type_flags |= eTypeIsSigned;
3976 } else if (builtin_type->isFloatingPoint())
3977 builtin_type_flags |= eTypeIsFloat;
3978 break;
3979 default:
3980 break;
3981 }
3982 return builtin_type_flags;
3983 }
3984
3985 case clang::Type::BlockPointer:
3986 if (pointee_or_element_clang_type)
3987 pointee_or_element_clang_type->SetCompilerType(
3988 getASTContext(), qual_type->getPointeeType());
3989 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3990
3991 case clang::Type::Complex: {
3992 uint32_t complex_type_flags =
3993 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3994 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
3995 qual_type->getCanonicalTypeInternal());
3996 if (complex_type) {
3997 clang::QualType complex_element_type(complex_type->getElementType());
3998 if (complex_element_type->isIntegerType())
3999 complex_type_flags |= eTypeIsFloat;
4000 else if (complex_element_type->isFloatingType())
4001 complex_type_flags |= eTypeIsInteger;
4002 }
4003 return complex_type_flags;
4004 } break;
4005
4006 case clang::Type::ConstantArray:
4007 case clang::Type::DependentSizedArray:
4008 case clang::Type::IncompleteArray:
4009 case clang::Type::VariableArray:
4010 if (pointee_or_element_clang_type)
4011 pointee_or_element_clang_type->SetCompilerType(
4012 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4013 ->getElementType());
4014 return eTypeHasChildren | eTypeIsArray;
4015
4016 case clang::Type::DependentName:
4017 return 0;
4018 case clang::Type::DependentSizedExtVector:
4019 return eTypeHasChildren | eTypeIsVector;
4020 case clang::Type::DependentTemplateSpecialization:
4021 return eTypeIsTemplate;
4022 case clang::Type::Decltype:
4023 return 0;
4024
4025 case clang::Type::Enum:
4026 if (pointee_or_element_clang_type)
4027 pointee_or_element_clang_type->SetCompilerType(
4028 getASTContext(),
4029 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4030 return eTypeIsEnumeration | eTypeHasValue;
4031
4032 case clang::Type::Auto:
4033 return CompilerType(
4034 getASTContext(),
4035 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4036 .GetTypeInfo(pointee_or_element_clang_type);
4037 case clang::Type::Elaborated:
4038 return CompilerType(
4039 getASTContext(),
4040 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4041 .GetTypeInfo(pointee_or_element_clang_type);
4042 case clang::Type::Paren:
4043 return CompilerType(getASTContext(),
4044 llvm::cast<clang::ParenType>(qual_type)->desugar())
4045 .GetTypeInfo(pointee_or_element_clang_type);
4046
4047 case clang::Type::FunctionProto:
4048 return eTypeIsFuncPrototype | eTypeHasValue;
4049 case clang::Type::FunctionNoProto:
4050 return eTypeIsFuncPrototype | eTypeHasValue;
4051 case clang::Type::InjectedClassName:
4052 return 0;
4053
4054 case clang::Type::LValueReference:
4055 case clang::Type::RValueReference:
4056 if (pointee_or_element_clang_type)
4057 pointee_or_element_clang_type->SetCompilerType(
4058 getASTContext(),
4059 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4060 ->getPointeeType());
4061 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4062
4063 case clang::Type::MemberPointer:
4064 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4065
4066 case clang::Type::ObjCObjectPointer:
4067 if (pointee_or_element_clang_type)
4068 pointee_or_element_clang_type->SetCompilerType(
4069 getASTContext(), qual_type->getPointeeType());
4070 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4071 eTypeHasValue;
4072
4073 case clang::Type::ObjCObject:
4074 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4075 case clang::Type::ObjCInterface:
4076 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4077
4078 case clang::Type::Pointer:
4079 if (pointee_or_element_clang_type)
4080 pointee_or_element_clang_type->SetCompilerType(
4081 getASTContext(), qual_type->getPointeeType());
4082 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4083
4084 case clang::Type::Record:
4085 if (qual_type->getAsCXXRecordDecl())
4086 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4087 else
4088 return eTypeHasChildren | eTypeIsStructUnion;
4089 break;
4090 case clang::Type::SubstTemplateTypeParm:
4091 return eTypeIsTemplate;
4092 case clang::Type::TemplateTypeParm:
4093 return eTypeIsTemplate;
4094 case clang::Type::TemplateSpecialization:
4095 return eTypeIsTemplate;
4096
4097 case clang::Type::Typedef:
4098 return eTypeIsTypedef |
4099 CompilerType(getASTContext(),
4100 llvm::cast<clang::TypedefType>(qual_type)
4101 ->getDecl()
4102 ->getUnderlyingType())
4103 .GetTypeInfo(pointee_or_element_clang_type);
4104 case clang::Type::TypeOfExpr:
4105 return 0;
4106 case clang::Type::TypeOf:
4107 return 0;
4108 case clang::Type::UnresolvedUsing:
4109 return 0;
4110
4111 case clang::Type::ExtVector:
4112 case clang::Type::Vector: {
4113 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4114 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4115 qual_type->getCanonicalTypeInternal());
4116 if (vector_type) {
4117 if (vector_type->isIntegerType())
4118 vector_type_flags |= eTypeIsFloat;
4119 else if (vector_type->isFloatingType())
4120 vector_type_flags |= eTypeIsInteger;
4121 }
4122 return vector_type_flags;
4123 }
4124 default:
4125 return 0;
4126 }
4127 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004128}
4129
Greg Claytond8d4a572015-08-11 21:38:15 +00004130lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004131ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4132 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004133 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004134
4135 // If the type is a reference, then resolve it to what it refers to first:
4136 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4137 if (qual_type->isAnyPointerType()) {
4138 if (qual_type->isObjCObjectPointerType())
4139 return lldb::eLanguageTypeObjC;
4140
4141 clang::QualType pointee_type(qual_type->getPointeeType());
4142 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4143 return lldb::eLanguageTypeC_plus_plus;
4144 if (pointee_type->isObjCObjectOrInterfaceType())
4145 return lldb::eLanguageTypeObjC;
4146 if (pointee_type->isObjCClassType())
4147 return lldb::eLanguageTypeObjC;
4148 if (pointee_type.getTypePtr() ==
4149 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4150 return lldb::eLanguageTypeObjC;
4151 } else {
4152 if (qual_type->isObjCObjectOrInterfaceType())
4153 return lldb::eLanguageTypeObjC;
4154 if (qual_type->getAsCXXRecordDecl())
4155 return lldb::eLanguageTypeC_plus_plus;
4156 switch (qual_type->getTypeClass()) {
4157 default:
4158 break;
4159 case clang::Type::Builtin:
4160 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4161 default:
4162 case clang::BuiltinType::Void:
4163 case clang::BuiltinType::Bool:
4164 case clang::BuiltinType::Char_U:
4165 case clang::BuiltinType::UChar:
4166 case clang::BuiltinType::WChar_U:
4167 case clang::BuiltinType::Char16:
4168 case clang::BuiltinType::Char32:
4169 case clang::BuiltinType::UShort:
4170 case clang::BuiltinType::UInt:
4171 case clang::BuiltinType::ULong:
4172 case clang::BuiltinType::ULongLong:
4173 case clang::BuiltinType::UInt128:
4174 case clang::BuiltinType::Char_S:
4175 case clang::BuiltinType::SChar:
4176 case clang::BuiltinType::WChar_S:
4177 case clang::BuiltinType::Short:
4178 case clang::BuiltinType::Int:
4179 case clang::BuiltinType::Long:
4180 case clang::BuiltinType::LongLong:
4181 case clang::BuiltinType::Int128:
4182 case clang::BuiltinType::Float:
4183 case clang::BuiltinType::Double:
4184 case clang::BuiltinType::LongDouble:
4185 break;
4186
4187 case clang::BuiltinType::NullPtr:
4188 return eLanguageTypeC_plus_plus;
4189
4190 case clang::BuiltinType::ObjCId:
4191 case clang::BuiltinType::ObjCClass:
4192 case clang::BuiltinType::ObjCSel:
4193 return eLanguageTypeObjC;
4194
4195 case clang::BuiltinType::Dependent:
4196 case clang::BuiltinType::Overload:
4197 case clang::BuiltinType::BoundMember:
4198 case clang::BuiltinType::UnknownAny:
4199 break;
4200 }
4201 break;
4202 case clang::Type::Typedef:
4203 return CompilerType(getASTContext(),
4204 llvm::cast<clang::TypedefType>(qual_type)
4205 ->getDecl()
4206 ->getUnderlyingType())
4207 .GetMinimumLanguage();
4208 }
4209 }
4210 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004211}
4212
4213lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004214ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4215 if (!type)
4216 return lldb::eTypeClassInvalid;
4217
4218 clang::QualType qual_type(GetQualType(type));
4219
4220 switch (qual_type->getTypeClass()) {
4221 case clang::Type::UnaryTransform:
4222 break;
4223 case clang::Type::FunctionNoProto:
4224 return lldb::eTypeClassFunction;
4225 case clang::Type::FunctionProto:
4226 return lldb::eTypeClassFunction;
4227 case clang::Type::IncompleteArray:
4228 return lldb::eTypeClassArray;
4229 case clang::Type::VariableArray:
4230 return lldb::eTypeClassArray;
4231 case clang::Type::ConstantArray:
4232 return lldb::eTypeClassArray;
4233 case clang::Type::DependentSizedArray:
4234 return lldb::eTypeClassArray;
4235 case clang::Type::DependentSizedExtVector:
4236 return lldb::eTypeClassVector;
4237 case clang::Type::ExtVector:
4238 return lldb::eTypeClassVector;
4239 case clang::Type::Vector:
4240 return lldb::eTypeClassVector;
4241 case clang::Type::Builtin:
4242 return lldb::eTypeClassBuiltin;
4243 case clang::Type::ObjCObjectPointer:
4244 return lldb::eTypeClassObjCObjectPointer;
4245 case clang::Type::BlockPointer:
4246 return lldb::eTypeClassBlockPointer;
4247 case clang::Type::Pointer:
4248 return lldb::eTypeClassPointer;
4249 case clang::Type::LValueReference:
4250 return lldb::eTypeClassReference;
4251 case clang::Type::RValueReference:
4252 return lldb::eTypeClassReference;
4253 case clang::Type::MemberPointer:
4254 return lldb::eTypeClassMemberPointer;
4255 case clang::Type::Complex:
4256 if (qual_type->isComplexType())
4257 return lldb::eTypeClassComplexFloat;
4258 else
4259 return lldb::eTypeClassComplexInteger;
4260 case clang::Type::ObjCObject:
4261 return lldb::eTypeClassObjCObject;
4262 case clang::Type::ObjCInterface:
4263 return lldb::eTypeClassObjCInterface;
4264 case clang::Type::Record: {
4265 const clang::RecordType *record_type =
4266 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4267 const clang::RecordDecl *record_decl = record_type->getDecl();
4268 if (record_decl->isUnion())
4269 return lldb::eTypeClassUnion;
4270 else if (record_decl->isStruct())
4271 return lldb::eTypeClassStruct;
4272 else
4273 return lldb::eTypeClassClass;
4274 } break;
4275 case clang::Type::Enum:
4276 return lldb::eTypeClassEnumeration;
4277 case clang::Type::Typedef:
4278 return lldb::eTypeClassTypedef;
4279 case clang::Type::UnresolvedUsing:
4280 break;
4281 case clang::Type::Paren:
4282 return CompilerType(getASTContext(),
4283 llvm::cast<clang::ParenType>(qual_type)->desugar())
4284 .GetTypeClass();
4285 case clang::Type::Auto:
4286 return CompilerType(
4287 getASTContext(),
4288 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4289 .GetTypeClass();
4290 case clang::Type::Elaborated:
4291 return CompilerType(
4292 getASTContext(),
4293 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4294 .GetTypeClass();
4295
4296 case clang::Type::Attributed:
4297 break;
4298 case clang::Type::TemplateTypeParm:
4299 break;
4300 case clang::Type::SubstTemplateTypeParm:
4301 break;
4302 case clang::Type::SubstTemplateTypeParmPack:
4303 break;
4304 case clang::Type::InjectedClassName:
4305 break;
4306 case clang::Type::DependentName:
4307 break;
4308 case clang::Type::DependentTemplateSpecialization:
4309 break;
4310 case clang::Type::PackExpansion:
4311 break;
4312
4313 case clang::Type::TypeOfExpr:
4314 break;
4315 case clang::Type::TypeOf:
4316 break;
4317 case clang::Type::Decltype:
4318 break;
4319 case clang::Type::TemplateSpecialization:
4320 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004321 case clang::Type::DeducedTemplateSpecialization:
4322 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004323 case clang::Type::Atomic:
4324 break;
4325 case clang::Type::Pipe:
4326 break;
4327
4328 // pointer type decayed from an array or function type.
4329 case clang::Type::Decayed:
4330 break;
4331 case clang::Type::Adjusted:
4332 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004333 case clang::Type::ObjCTypeParam:
4334 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004335 }
4336 // We don't know hot to display this type...
4337 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004338}
4339
Kate Stoneb9c1b512016-09-06 20:57:50 +00004340unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4341 if (type)
4342 return GetQualType(type).getQualifiers().getCVRQualifiers();
4343 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004344}
4345
4346//----------------------------------------------------------------------
4347// Creating related types
4348//----------------------------------------------------------------------
4349
Greg Claytona1e5dc82015-08-11 22:53:00 +00004350CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004351ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4352 uint64_t *stride) {
4353 if (type) {
4354 clang::QualType qual_type(GetCanonicalQualType(type));
4355
4356 const clang::Type *array_eletype =
4357 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4358
4359 if (!array_eletype)
4360 return CompilerType();
4361
4362 CompilerType element_type(getASTContext(),
4363 array_eletype->getCanonicalTypeUnqualified());
4364
4365 // TODO: the real stride will be >= this value.. find the real one!
4366 if (stride)
4367 *stride = element_type.GetByteSize(nullptr);
4368
4369 return element_type;
4370 }
4371 return CompilerType();
4372}
4373
4374CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4375 uint64_t size) {
4376 if (type) {
4377 clang::QualType qual_type(GetCanonicalQualType(type));
4378 if (clang::ASTContext *ast_ctx = getASTContext()) {
4379 if (size != 0)
4380 return CompilerType(
4381 ast_ctx, ast_ctx->getConstantArrayType(
4382 qual_type, llvm::APInt(64, size),
4383 clang::ArrayType::ArraySizeModifier::Normal, 0));
4384 else
4385 return CompilerType(
4386 ast_ctx,
4387 ast_ctx->getIncompleteArrayType(
4388 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
Greg Claytond8d4a572015-08-11 21:38:15 +00004389 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004390 }
4391
4392 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004393}
4394
Greg Claytona1e5dc82015-08-11 22:53:00 +00004395CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004396ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4397 if (type)
4398 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4399 return CompilerType();
4400}
4401
4402static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4403 clang::QualType qual_type) {
4404 if (qual_type->isPointerType())
4405 qual_type = ast->getPointerType(
4406 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4407 else
4408 qual_type = qual_type.getUnqualifiedType();
4409 qual_type.removeLocalConst();
4410 qual_type.removeLocalRestrict();
4411 qual_type.removeLocalVolatile();
4412 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004413}
4414
4415CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004416ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4417 if (type)
4418 return CompilerType(
4419 getASTContext(),
4420 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4421 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004422}
4423
Kate Stoneb9c1b512016-09-06 20:57:50 +00004424int ClangASTContext::GetFunctionArgumentCount(
4425 lldb::opaque_compiler_type_t type) {
4426 if (type) {
4427 const clang::FunctionProtoType *func =
4428 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4429 if (func)
4430 return func->getNumParams();
4431 }
4432 return -1;
4433}
4434
4435CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4436 lldb::opaque_compiler_type_t type, size_t idx) {
4437 if (type) {
4438 const clang::FunctionProtoType *func =
4439 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4440 if (func) {
4441 const uint32_t num_args = func->getNumParams();
4442 if (idx < num_args)
4443 return CompilerType(getASTContext(), func->getParamType(idx));
4444 }
4445 }
4446 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004447}
4448
Greg Claytona1e5dc82015-08-11 22:53:00 +00004449CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004450ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4451 if (type) {
4452 clang::QualType qual_type(GetQualType(type));
4453 const clang::FunctionProtoType *func =
4454 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4455 if (func)
4456 return CompilerType(getASTContext(), func->getReturnType());
4457 }
4458 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004459}
4460
4461size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004462ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4463 size_t num_functions = 0;
4464 if (type) {
4465 clang::QualType qual_type(GetCanonicalQualType(type));
4466 switch (qual_type->getTypeClass()) {
4467 case clang::Type::Record:
4468 if (GetCompleteQualType(getASTContext(), qual_type)) {
4469 const clang::RecordType *record_type =
4470 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4471 const clang::RecordDecl *record_decl = record_type->getDecl();
4472 assert(record_decl);
4473 const clang::CXXRecordDecl *cxx_record_decl =
4474 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4475 if (cxx_record_decl)
4476 num_functions = std::distance(cxx_record_decl->method_begin(),
4477 cxx_record_decl->method_end());
4478 }
4479 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004480
Sean Callananf9c622a2016-09-30 18:44:43 +00004481 case clang::Type::ObjCObjectPointer: {
4482 const clang::ObjCObjectPointerType *objc_class_type =
4483 qual_type->getAsObjCInterfacePointerType();
4484 const clang::ObjCInterfaceType *objc_interface_type =
4485 objc_class_type->getInterfaceType();
4486 if (objc_interface_type &&
4487 GetCompleteType((lldb::opaque_compiler_type_t)objc_interface_type)) {
4488 clang::ObjCInterfaceDecl *class_interface_decl =
4489 objc_interface_type->getDecl();
4490 if (class_interface_decl) {
4491 num_functions = std::distance(class_interface_decl->meth_begin(),
4492 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004493 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004494 }
4495 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004496 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004497
4498 case clang::Type::ObjCObject:
4499 case clang::Type::ObjCInterface:
4500 if (GetCompleteType(type)) {
4501 const clang::ObjCObjectType *objc_class_type =
4502 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4503 if (objc_class_type) {
4504 clang::ObjCInterfaceDecl *class_interface_decl =
4505 objc_class_type->getInterface();
4506 if (class_interface_decl)
4507 num_functions = std::distance(class_interface_decl->meth_begin(),
4508 class_interface_decl->meth_end());
4509 }
4510 }
4511 break;
4512
4513 case clang::Type::Typedef:
4514 return CompilerType(getASTContext(),
4515 llvm::cast<clang::TypedefType>(qual_type)
4516 ->getDecl()
4517 ->getUnderlyingType())
4518 .GetNumMemberFunctions();
4519
4520 case clang::Type::Auto:
4521 return CompilerType(
4522 getASTContext(),
4523 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4524 .GetNumMemberFunctions();
4525
4526 case clang::Type::Elaborated:
4527 return CompilerType(
4528 getASTContext(),
4529 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4530 .GetNumMemberFunctions();
4531
4532 case clang::Type::Paren:
4533 return CompilerType(getASTContext(),
4534 llvm::cast<clang::ParenType>(qual_type)->desugar())
4535 .GetNumMemberFunctions();
4536
4537 default:
4538 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004539 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004540 }
4541 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004542}
4543
4544TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004545ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4546 size_t idx) {
4547 std::string name;
4548 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4549 CompilerType clang_type;
4550 CompilerDecl clang_decl;
4551 if (type) {
4552 clang::QualType qual_type(GetCanonicalQualType(type));
4553 switch (qual_type->getTypeClass()) {
4554 case clang::Type::Record:
4555 if (GetCompleteQualType(getASTContext(), qual_type)) {
4556 const clang::RecordType *record_type =
4557 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4558 const clang::RecordDecl *record_decl = record_type->getDecl();
4559 assert(record_decl);
4560 const clang::CXXRecordDecl *cxx_record_decl =
4561 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4562 if (cxx_record_decl) {
4563 auto method_iter = cxx_record_decl->method_begin();
4564 auto method_end = cxx_record_decl->method_end();
4565 if (idx <
4566 static_cast<size_t>(std::distance(method_iter, method_end))) {
4567 std::advance(method_iter, idx);
4568 clang::CXXMethodDecl *cxx_method_decl =
4569 method_iter->getCanonicalDecl();
4570 if (cxx_method_decl) {
4571 name = cxx_method_decl->getDeclName().getAsString();
4572 if (cxx_method_decl->isStatic())
4573 kind = lldb::eMemberFunctionKindStaticMethod;
4574 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4575 kind = lldb::eMemberFunctionKindConstructor;
4576 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4577 kind = lldb::eMemberFunctionKindDestructor;
4578 else
4579 kind = lldb::eMemberFunctionKindInstanceMethod;
4580 clang_type = CompilerType(
4581 this, cxx_method_decl->getType().getAsOpaquePtr());
4582 clang_decl = CompilerDecl(this, cxx_method_decl);
4583 }
4584 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004585 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004586 }
4587 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004588
Sean Callananf9c622a2016-09-30 18:44:43 +00004589 case clang::Type::ObjCObjectPointer: {
4590 const clang::ObjCObjectPointerType *objc_class_type =
4591 qual_type->getAsObjCInterfacePointerType();
4592 const clang::ObjCInterfaceType *objc_interface_type =
4593 objc_class_type->getInterfaceType();
4594 if (objc_interface_type &&
4595 GetCompleteType((lldb::opaque_compiler_type_t)objc_interface_type)) {
4596 clang::ObjCInterfaceDecl *class_interface_decl =
4597 objc_interface_type->getDecl();
4598 if (class_interface_decl) {
4599 auto method_iter = class_interface_decl->meth_begin();
4600 auto method_end = class_interface_decl->meth_end();
4601 if (idx <
4602 static_cast<size_t>(std::distance(method_iter, method_end))) {
4603 std::advance(method_iter, idx);
4604 clang::ObjCMethodDecl *objc_method_decl =
4605 method_iter->getCanonicalDecl();
4606 if (objc_method_decl) {
4607 clang_decl = CompilerDecl(this, objc_method_decl);
4608 name = objc_method_decl->getSelector().getAsString();
4609 if (objc_method_decl->isClassMethod())
4610 kind = lldb::eMemberFunctionKindStaticMethod;
4611 else
4612 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004613 }
4614 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004615 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004616 }
4617 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004618 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004619
Kate Stoneb9c1b512016-09-06 20:57:50 +00004620 case clang::Type::ObjCObject:
4621 case clang::Type::ObjCInterface:
4622 if (GetCompleteType(type)) {
4623 const clang::ObjCObjectType *objc_class_type =
4624 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4625 if (objc_class_type) {
4626 clang::ObjCInterfaceDecl *class_interface_decl =
4627 objc_class_type->getInterface();
4628 if (class_interface_decl) {
4629 auto method_iter = class_interface_decl->meth_begin();
4630 auto method_end = class_interface_decl->meth_end();
4631 if (idx <
4632 static_cast<size_t>(std::distance(method_iter, method_end))) {
4633 std::advance(method_iter, idx);
4634 clang::ObjCMethodDecl *objc_method_decl =
4635 method_iter->getCanonicalDecl();
4636 if (objc_method_decl) {
4637 clang_decl = CompilerDecl(this, objc_method_decl);
4638 name = objc_method_decl->getSelector().getAsString();
4639 if (objc_method_decl->isClassMethod())
4640 kind = lldb::eMemberFunctionKindStaticMethod;
4641 else
4642 kind = lldb::eMemberFunctionKindInstanceMethod;
4643 }
4644 }
4645 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004646 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004647 }
4648 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004649
Kate Stoneb9c1b512016-09-06 20:57:50 +00004650 case clang::Type::Typedef:
4651 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4652 ->getDecl()
4653 ->getUnderlyingType()
4654 .getAsOpaquePtr(),
4655 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004656
Kate Stoneb9c1b512016-09-06 20:57:50 +00004657 case clang::Type::Auto:
4658 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4659 ->getDeducedType()
4660 .getAsOpaquePtr(),
4661 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004662
Kate Stoneb9c1b512016-09-06 20:57:50 +00004663 case clang::Type::Elaborated:
4664 return GetMemberFunctionAtIndex(
4665 llvm::cast<clang::ElaboratedType>(qual_type)
4666 ->getNamedType()
4667 .getAsOpaquePtr(),
4668 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004669
Kate Stoneb9c1b512016-09-06 20:57:50 +00004670 case clang::Type::Paren:
4671 return GetMemberFunctionAtIndex(
4672 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4673 idx);
4674
4675 default:
4676 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004677 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004678 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004679
Kate Stoneb9c1b512016-09-06 20:57:50 +00004680 if (kind == eMemberFunctionKindUnknown)
4681 return TypeMemberFunctionImpl();
4682 else
4683 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004684}
4685
Greg Claytona1e5dc82015-08-11 22:53:00 +00004686CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004687ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4688 if (type)
4689 return CompilerType(getASTContext(),
4690 GetQualType(type).getNonReferenceType());
4691 return CompilerType();
4692}
4693
4694CompilerType ClangASTContext::CreateTypedefType(
4695 const CompilerType &type, const char *typedef_name,
4696 const CompilerDeclContext &compiler_decl_ctx) {
4697 if (type && typedef_name && typedef_name[0]) {
4698 ClangASTContext *ast =
4699 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4700 if (!ast)
4701 return CompilerType();
4702 clang::ASTContext *clang_ast = ast->getASTContext();
4703 clang::QualType qual_type(ClangUtil::GetQualType(type));
4704
4705 clang::DeclContext *decl_ctx =
4706 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4707 if (decl_ctx == nullptr)
4708 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4709
4710 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4711 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4712 &clang_ast->Idents.get(typedef_name),
4713 clang_ast->getTrivialTypeSourceInfo(qual_type));
4714
4715 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4716
4717 // Get a uniqued clang::QualType for the typedef decl type
4718 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4719 }
4720 return CompilerType();
4721}
4722
4723CompilerType
4724ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4725 if (type) {
4726 clang::QualType qual_type(GetQualType(type));
4727 return CompilerType(getASTContext(),
4728 qual_type.getTypePtr()->getPointeeType());
4729 }
4730 return CompilerType();
4731}
4732
4733CompilerType
4734ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4735 if (type) {
4736 clang::QualType qual_type(GetQualType(type));
4737
4738 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4739 switch (type_class) {
4740 case clang::Type::ObjCObject:
4741 case clang::Type::ObjCInterface:
4742 return CompilerType(getASTContext(),
4743 getASTContext()->getObjCObjectPointerType(qual_type));
4744
4745 default:
4746 return CompilerType(getASTContext(),
4747 getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004748 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004749 }
4750 return CompilerType();
4751}
4752
4753CompilerType
4754ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4755 if (type)
4756 return CompilerType(this, getASTContext()
4757 ->getLValueReferenceType(GetQualType(type))
4758 .getAsOpaquePtr());
4759 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004760 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004761}
4762
Kate Stoneb9c1b512016-09-06 20:57:50 +00004763CompilerType
4764ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4765 if (type)
4766 return CompilerType(this, getASTContext()
4767 ->getRValueReferenceType(GetQualType(type))
4768 .getAsOpaquePtr());
4769 else
4770 return CompilerType();
4771}
4772
4773CompilerType
4774ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4775 if (type) {
4776 clang::QualType result(GetQualType(type));
4777 result.addConst();
4778 return CompilerType(this, result.getAsOpaquePtr());
4779 }
4780 return CompilerType();
4781}
4782
4783CompilerType
4784ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4785 if (type) {
4786 clang::QualType result(GetQualType(type));
4787 result.addVolatile();
4788 return CompilerType(this, result.getAsOpaquePtr());
4789 }
4790 return CompilerType();
4791}
4792
4793CompilerType
4794ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4795 if (type) {
4796 clang::QualType result(GetQualType(type));
4797 result.addRestrict();
4798 return CompilerType(this, result.getAsOpaquePtr());
4799 }
4800 return CompilerType();
4801}
4802
4803CompilerType
4804ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4805 const char *typedef_name,
4806 const CompilerDeclContext &compiler_decl_ctx) {
4807 if (type) {
4808 clang::ASTContext *clang_ast = getASTContext();
4809 clang::QualType qual_type(GetQualType(type));
4810
4811 clang::DeclContext *decl_ctx =
4812 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4813 if (decl_ctx == nullptr)
4814 decl_ctx = getASTContext()->getTranslationUnitDecl();
4815
4816 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4817 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4818 &clang_ast->Idents.get(typedef_name),
4819 clang_ast->getTrivialTypeSourceInfo(qual_type));
4820
4821 clang::TagDecl *tdecl = nullptr;
4822 if (!qual_type.isNull()) {
4823 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4824 tdecl = rt->getDecl();
4825 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4826 tdecl = et->getDecl();
4827 }
4828
4829 // Check whether this declaration is an anonymous struct, union, or enum,
4830 // hidden behind a typedef. If so, we
4831 // try to check whether we have a typedef tag to attach to the original
4832 // record declaration
4833 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4834 tdecl->setTypedefNameForAnonDecl(decl);
4835
4836 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4837
4838 // Get a uniqued clang::QualType for the typedef decl type
4839 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4840 }
4841 return CompilerType();
4842}
4843
4844CompilerType
4845ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4846 if (type) {
4847 const clang::TypedefType *typedef_type =
4848 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4849 if (typedef_type)
4850 return CompilerType(getASTContext(),
4851 typedef_type->getDecl()->getUnderlyingType());
4852 }
4853 return CompilerType();
4854}
Greg Claytond8d4a572015-08-11 21:38:15 +00004855
4856//----------------------------------------------------------------------
4857// Create related types using the current type's AST
4858//----------------------------------------------------------------------
4859
Kate Stoneb9c1b512016-09-06 20:57:50 +00004860CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4861 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004862}
4863//----------------------------------------------------------------------
4864// Exploring the type
4865//----------------------------------------------------------------------
4866
Kate Stoneb9c1b512016-09-06 20:57:50 +00004867uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4868 ExecutionContextScope *exe_scope) {
4869 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00004870 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004871 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004872 switch (type_class) {
4873 case clang::Type::Record:
4874 if (GetCompleteType(type))
4875 return getASTContext()->getTypeSize(qual_type);
4876 else
4877 return 0;
4878 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004879
Kate Stoneb9c1b512016-09-06 20:57:50 +00004880 case clang::Type::ObjCInterface:
4881 case clang::Type::ObjCObject: {
4882 ExecutionContext exe_ctx(exe_scope);
4883 Process *process = exe_ctx.GetProcessPtr();
4884 if (process) {
4885 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4886 if (objc_runtime) {
4887 uint64_t bit_size = 0;
4888 if (objc_runtime->GetTypeBitSize(
4889 CompilerType(getASTContext(), qual_type), bit_size))
4890 return bit_size;
4891 }
4892 } else {
4893 static bool g_printed = false;
4894 if (!g_printed) {
4895 StreamString s;
4896 DumpTypeDescription(type, &s);
4897
4898 llvm::outs() << "warning: trying to determine the size of type ";
4899 llvm::outs() << s.GetString() << "\n";
4900 llvm::outs() << "without a valid ExecutionContext. this is not "
4901 "reliable. please file a bug against LLDB.\n";
4902 llvm::outs() << "backtrace:\n";
4903 llvm::sys::PrintStackTrace(llvm::outs());
4904 llvm::outs() << "\n";
4905 g_printed = true;
4906 }
4907 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004908 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004909 LLVM_FALLTHROUGH;
4910 default:
4911 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
4912 if (bit_size == 0) {
4913 if (qual_type->isIncompleteArrayType())
4914 return getASTContext()->getTypeSize(
4915 qual_type->getArrayElementTypeNoTypeQual()
4916 ->getCanonicalTypeUnqualified());
4917 }
4918 if (qual_type->isObjCObjectOrInterfaceType())
4919 return bit_size +
4920 getASTContext()->getTypeSize(
4921 getASTContext()->ObjCBuiltinClassTy);
4922 return bit_size;
4923 }
4924 }
4925 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004926}
4927
Kate Stoneb9c1b512016-09-06 20:57:50 +00004928size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
4929 if (GetCompleteType(type))
4930 return getASTContext()->getTypeAlign(GetQualType(type));
4931 return 0;
4932}
4933
4934lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
4935 uint64_t &count) {
4936 if (!type)
4937 return lldb::eEncodingInvalid;
4938
4939 count = 1;
4940 clang::QualType qual_type(GetCanonicalQualType(type));
4941
4942 switch (qual_type->getTypeClass()) {
4943 case clang::Type::UnaryTransform:
4944 break;
4945
4946 case clang::Type::FunctionNoProto:
4947 case clang::Type::FunctionProto:
4948 break;
4949
4950 case clang::Type::IncompleteArray:
4951 case clang::Type::VariableArray:
4952 break;
4953
4954 case clang::Type::ConstantArray:
4955 break;
4956
4957 case clang::Type::ExtVector:
4958 case clang::Type::Vector:
4959 // TODO: Set this to more than one???
4960 break;
4961
4962 case clang::Type::Builtin:
4963 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4964 case clang::BuiltinType::Void:
4965 break;
4966
4967 case clang::BuiltinType::Bool:
4968 case clang::BuiltinType::Char_S:
4969 case clang::BuiltinType::SChar:
4970 case clang::BuiltinType::WChar_S:
4971 case clang::BuiltinType::Char16:
4972 case clang::BuiltinType::Char32:
4973 case clang::BuiltinType::Short:
4974 case clang::BuiltinType::Int:
4975 case clang::BuiltinType::Long:
4976 case clang::BuiltinType::LongLong:
4977 case clang::BuiltinType::Int128:
4978 return lldb::eEncodingSint;
4979
4980 case clang::BuiltinType::Char_U:
4981 case clang::BuiltinType::UChar:
4982 case clang::BuiltinType::WChar_U:
4983 case clang::BuiltinType::UShort:
4984 case clang::BuiltinType::UInt:
4985 case clang::BuiltinType::ULong:
4986 case clang::BuiltinType::ULongLong:
4987 case clang::BuiltinType::UInt128:
4988 return lldb::eEncodingUint;
4989
4990 case clang::BuiltinType::Half:
4991 case clang::BuiltinType::Float:
4992 case clang::BuiltinType::Float128:
4993 case clang::BuiltinType::Double:
4994 case clang::BuiltinType::LongDouble:
4995 return lldb::eEncodingIEEE754;
4996
4997 case clang::BuiltinType::ObjCClass:
4998 case clang::BuiltinType::ObjCId:
4999 case clang::BuiltinType::ObjCSel:
5000 return lldb::eEncodingUint;
5001
5002 case clang::BuiltinType::NullPtr:
5003 return lldb::eEncodingUint;
5004
5005 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5006 case clang::BuiltinType::Kind::BoundMember:
5007 case clang::BuiltinType::Kind::BuiltinFn:
5008 case clang::BuiltinType::Kind::Dependent:
5009 case clang::BuiltinType::Kind::OCLClkEvent:
5010 case clang::BuiltinType::Kind::OCLEvent:
5011 case clang::BuiltinType::Kind::OCLImage1dRO:
5012 case clang::BuiltinType::Kind::OCLImage1dWO:
5013 case clang::BuiltinType::Kind::OCLImage1dRW:
5014 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5015 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5016 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5017 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5018 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5019 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5020 case clang::BuiltinType::Kind::OCLImage2dRO:
5021 case clang::BuiltinType::Kind::OCLImage2dWO:
5022 case clang::BuiltinType::Kind::OCLImage2dRW:
5023 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5024 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5025 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5026 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5027 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5028 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5029 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5030 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5031 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5032 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5033 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5034 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5035 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5036 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5037 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5038 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5039 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5040 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5041 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5042 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5043 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5044 case clang::BuiltinType::Kind::OCLImage3dRO:
5045 case clang::BuiltinType::Kind::OCLImage3dWO:
5046 case clang::BuiltinType::Kind::OCLImage3dRW:
5047 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005048 case clang::BuiltinType::Kind::OCLReserveID:
5049 case clang::BuiltinType::Kind::OCLSampler:
5050 case clang::BuiltinType::Kind::OMPArraySection:
5051 case clang::BuiltinType::Kind::Overload:
5052 case clang::BuiltinType::Kind::PseudoObject:
5053 case clang::BuiltinType::Kind::UnknownAny:
5054 break;
5055 }
5056 break;
5057 // All pointer types are represented as unsigned integer encodings.
5058 // We may nee to add a eEncodingPointer if we ever need to know the
5059 // difference
5060 case clang::Type::ObjCObjectPointer:
5061 case clang::Type::BlockPointer:
5062 case clang::Type::Pointer:
5063 case clang::Type::LValueReference:
5064 case clang::Type::RValueReference:
5065 case clang::Type::MemberPointer:
5066 return lldb::eEncodingUint;
5067 case clang::Type::Complex: {
5068 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5069 if (qual_type->isComplexType())
5070 encoding = lldb::eEncodingIEEE754;
5071 else {
5072 const clang::ComplexType *complex_type =
5073 qual_type->getAsComplexIntegerType();
5074 if (complex_type)
5075 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5076 .GetEncoding(count);
5077 else
5078 encoding = lldb::eEncodingSint;
5079 }
5080 count = 2;
5081 return encoding;
5082 }
5083
5084 case clang::Type::ObjCInterface:
5085 break;
5086 case clang::Type::Record:
5087 break;
5088 case clang::Type::Enum:
5089 return lldb::eEncodingSint;
5090 case clang::Type::Typedef:
5091 return CompilerType(getASTContext(),
5092 llvm::cast<clang::TypedefType>(qual_type)
5093 ->getDecl()
5094 ->getUnderlyingType())
5095 .GetEncoding(count);
5096
5097 case clang::Type::Auto:
5098 return CompilerType(
5099 getASTContext(),
5100 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5101 .GetEncoding(count);
5102
5103 case clang::Type::Elaborated:
5104 return CompilerType(
5105 getASTContext(),
5106 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5107 .GetEncoding(count);
5108
5109 case clang::Type::Paren:
5110 return CompilerType(getASTContext(),
5111 llvm::cast<clang::ParenType>(qual_type)->desugar())
5112 .GetEncoding(count);
5113
5114 case clang::Type::DependentSizedArray:
5115 case clang::Type::DependentSizedExtVector:
5116 case clang::Type::UnresolvedUsing:
5117 case clang::Type::Attributed:
5118 case clang::Type::TemplateTypeParm:
5119 case clang::Type::SubstTemplateTypeParm:
5120 case clang::Type::SubstTemplateTypeParmPack:
5121 case clang::Type::InjectedClassName:
5122 case clang::Type::DependentName:
5123 case clang::Type::DependentTemplateSpecialization:
5124 case clang::Type::PackExpansion:
5125 case clang::Type::ObjCObject:
5126
5127 case clang::Type::TypeOfExpr:
5128 case clang::Type::TypeOf:
5129 case clang::Type::Decltype:
5130 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005131 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005132 case clang::Type::Atomic:
5133 case clang::Type::Adjusted:
5134 case clang::Type::Pipe:
5135 break;
5136
5137 // pointer type decayed from an array or function type.
5138 case clang::Type::Decayed:
5139 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005140 case clang::Type::ObjCTypeParam:
5141 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005142 }
5143 count = 0;
5144 return lldb::eEncodingInvalid;
5145}
5146
5147lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5148 if (!type)
5149 return lldb::eFormatDefault;
5150
5151 clang::QualType qual_type(GetCanonicalQualType(type));
5152
5153 switch (qual_type->getTypeClass()) {
5154 case clang::Type::UnaryTransform:
5155 break;
5156
5157 case clang::Type::FunctionNoProto:
5158 case clang::Type::FunctionProto:
5159 break;
5160
5161 case clang::Type::IncompleteArray:
5162 case clang::Type::VariableArray:
5163 break;
5164
5165 case clang::Type::ConstantArray:
5166 return lldb::eFormatVoid; // no value
5167
5168 case clang::Type::ExtVector:
5169 case clang::Type::Vector:
5170 break;
5171
5172 case clang::Type::Builtin:
5173 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5174 // default: assert(0 && "Unknown builtin type!");
5175 case clang::BuiltinType::UnknownAny:
5176 case clang::BuiltinType::Void:
5177 case clang::BuiltinType::BoundMember:
5178 break;
5179
5180 case clang::BuiltinType::Bool:
5181 return lldb::eFormatBoolean;
5182 case clang::BuiltinType::Char_S:
5183 case clang::BuiltinType::SChar:
5184 case clang::BuiltinType::WChar_S:
5185 case clang::BuiltinType::Char_U:
5186 case clang::BuiltinType::UChar:
5187 case clang::BuiltinType::WChar_U:
5188 return lldb::eFormatChar;
5189 case clang::BuiltinType::Char16:
5190 return lldb::eFormatUnicode16;
5191 case clang::BuiltinType::Char32:
5192 return lldb::eFormatUnicode32;
5193 case clang::BuiltinType::UShort:
5194 return lldb::eFormatUnsigned;
5195 case clang::BuiltinType::Short:
5196 return lldb::eFormatDecimal;
5197 case clang::BuiltinType::UInt:
5198 return lldb::eFormatUnsigned;
5199 case clang::BuiltinType::Int:
5200 return lldb::eFormatDecimal;
5201 case clang::BuiltinType::ULong:
5202 return lldb::eFormatUnsigned;
5203 case clang::BuiltinType::Long:
5204 return lldb::eFormatDecimal;
5205 case clang::BuiltinType::ULongLong:
5206 return lldb::eFormatUnsigned;
5207 case clang::BuiltinType::LongLong:
5208 return lldb::eFormatDecimal;
5209 case clang::BuiltinType::UInt128:
5210 return lldb::eFormatUnsigned;
5211 case clang::BuiltinType::Int128:
5212 return lldb::eFormatDecimal;
5213 case clang::BuiltinType::Half:
5214 case clang::BuiltinType::Float:
5215 case clang::BuiltinType::Double:
5216 case clang::BuiltinType::LongDouble:
5217 return lldb::eFormatFloat;
5218 default:
5219 return lldb::eFormatHex;
5220 }
5221 break;
5222 case clang::Type::ObjCObjectPointer:
5223 return lldb::eFormatHex;
5224 case clang::Type::BlockPointer:
5225 return lldb::eFormatHex;
5226 case clang::Type::Pointer:
5227 return lldb::eFormatHex;
5228 case clang::Type::LValueReference:
5229 case clang::Type::RValueReference:
5230 return lldb::eFormatHex;
5231 case clang::Type::MemberPointer:
5232 break;
5233 case clang::Type::Complex: {
5234 if (qual_type->isComplexType())
5235 return lldb::eFormatComplex;
5236 else
5237 return lldb::eFormatComplexInteger;
5238 }
5239 case clang::Type::ObjCInterface:
5240 break;
5241 case clang::Type::Record:
5242 break;
5243 case clang::Type::Enum:
5244 return lldb::eFormatEnum;
5245 case clang::Type::Typedef:
5246 return CompilerType(getASTContext(),
5247 llvm::cast<clang::TypedefType>(qual_type)
5248 ->getDecl()
5249 ->getUnderlyingType())
5250 .GetFormat();
5251 case clang::Type::Auto:
5252 return CompilerType(getASTContext(),
5253 llvm::cast<clang::AutoType>(qual_type)->desugar())
5254 .GetFormat();
5255 case clang::Type::Paren:
5256 return CompilerType(getASTContext(),
5257 llvm::cast<clang::ParenType>(qual_type)->desugar())
5258 .GetFormat();
5259 case clang::Type::Elaborated:
5260 return CompilerType(
5261 getASTContext(),
5262 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5263 .GetFormat();
5264 case clang::Type::DependentSizedArray:
5265 case clang::Type::DependentSizedExtVector:
5266 case clang::Type::UnresolvedUsing:
5267 case clang::Type::Attributed:
5268 case clang::Type::TemplateTypeParm:
5269 case clang::Type::SubstTemplateTypeParm:
5270 case clang::Type::SubstTemplateTypeParmPack:
5271 case clang::Type::InjectedClassName:
5272 case clang::Type::DependentName:
5273 case clang::Type::DependentTemplateSpecialization:
5274 case clang::Type::PackExpansion:
5275 case clang::Type::ObjCObject:
5276
5277 case clang::Type::TypeOfExpr:
5278 case clang::Type::TypeOf:
5279 case clang::Type::Decltype:
5280 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005281 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005282 case clang::Type::Atomic:
5283 case clang::Type::Adjusted:
5284 case clang::Type::Pipe:
5285 break;
5286
5287 // pointer type decayed from an array or function type.
5288 case clang::Type::Decayed:
5289 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005290 case clang::Type::ObjCTypeParam:
5291 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005292 }
5293 // We don't know hot to display this type...
5294 return lldb::eFormatBytes;
5295}
5296
5297static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5298 bool check_superclass) {
5299 while (class_interface_decl) {
5300 if (class_interface_decl->ivar_size() > 0)
5301 return true;
5302
5303 if (check_superclass)
5304 class_interface_decl = class_interface_decl->getSuperClass();
5305 else
5306 break;
5307 }
5308 return false;
5309}
5310
5311uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
5312 bool omit_empty_base_classes) {
5313 if (!type)
5314 return 0;
5315
5316 uint32_t num_children = 0;
5317 clang::QualType qual_type(GetQualType(type));
5318 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5319 switch (type_class) {
5320 case clang::Type::Builtin:
5321 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5322 case clang::BuiltinType::ObjCId: // child is Class
5323 case clang::BuiltinType::ObjCClass: // child is Class
5324 num_children = 1;
5325 break;
5326
5327 default:
5328 break;
5329 }
5330 break;
5331
5332 case clang::Type::Complex:
5333 return 0;
5334
5335 case clang::Type::Record:
5336 if (GetCompleteQualType(getASTContext(), qual_type)) {
5337 const clang::RecordType *record_type =
5338 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5339 const clang::RecordDecl *record_decl = record_type->getDecl();
5340 assert(record_decl);
5341 const clang::CXXRecordDecl *cxx_record_decl =
5342 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5343 if (cxx_record_decl) {
5344 if (omit_empty_base_classes) {
5345 // Check each base classes to see if it or any of its
5346 // base classes contain any fields. This can help
5347 // limit the noise in variable views by not having to
5348 // show base classes that contain no members.
5349 clang::CXXRecordDecl::base_class_const_iterator base_class,
5350 base_class_end;
5351 for (base_class = cxx_record_decl->bases_begin(),
5352 base_class_end = cxx_record_decl->bases_end();
5353 base_class != base_class_end; ++base_class) {
5354 const clang::CXXRecordDecl *base_class_decl =
5355 llvm::cast<clang::CXXRecordDecl>(
5356 base_class->getType()
5357 ->getAs<clang::RecordType>()
5358 ->getDecl());
5359
5360 // Skip empty base classes
5361 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5362 continue;
5363
5364 num_children++;
5365 }
5366 } else {
5367 // Include all base classes
5368 num_children += cxx_record_decl->getNumBases();
5369 }
5370 }
5371 clang::RecordDecl::field_iterator field, field_end;
5372 for (field = record_decl->field_begin(),
5373 field_end = record_decl->field_end();
5374 field != field_end; ++field)
5375 ++num_children;
5376 }
5377 break;
5378
5379 case clang::Type::ObjCObject:
5380 case clang::Type::ObjCInterface:
5381 if (GetCompleteQualType(getASTContext(), qual_type)) {
5382 const clang::ObjCObjectType *objc_class_type =
5383 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5384 assert(objc_class_type);
5385 if (objc_class_type) {
5386 clang::ObjCInterfaceDecl *class_interface_decl =
5387 objc_class_type->getInterface();
5388
5389 if (class_interface_decl) {
5390
5391 clang::ObjCInterfaceDecl *superclass_interface_decl =
5392 class_interface_decl->getSuperClass();
5393 if (superclass_interface_decl) {
5394 if (omit_empty_base_classes) {
5395 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5396 ++num_children;
5397 } else
5398 ++num_children;
5399 }
5400
5401 num_children += class_interface_decl->ivar_size();
5402 }
5403 }
5404 }
5405 break;
5406
5407 case clang::Type::ObjCObjectPointer: {
5408 const clang::ObjCObjectPointerType *pointer_type =
5409 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5410 clang::QualType pointee_type = pointer_type->getPointeeType();
5411 uint32_t num_pointee_children =
5412 CompilerType(getASTContext(), pointee_type)
5413 .GetNumChildren(omit_empty_base_classes);
5414 // If this type points to a simple type, then it has 1 child
5415 if (num_pointee_children == 0)
5416 num_children = 1;
5417 else
5418 num_children = num_pointee_children;
5419 } break;
5420
5421 case clang::Type::Vector:
5422 case clang::Type::ExtVector:
5423 num_children =
5424 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5425 break;
5426
5427 case clang::Type::ConstantArray:
5428 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5429 ->getSize()
5430 .getLimitedValue();
5431 break;
5432
5433 case clang::Type::Pointer: {
5434 const clang::PointerType *pointer_type =
5435 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5436 clang::QualType pointee_type(pointer_type->getPointeeType());
5437 uint32_t num_pointee_children =
5438 CompilerType(getASTContext(), pointee_type)
5439 .GetNumChildren(omit_empty_base_classes);
5440 if (num_pointee_children == 0) {
5441 // We have a pointer to a pointee type that claims it has no children.
5442 // We will want to look at
5443 num_children = GetNumPointeeChildren(pointee_type);
5444 } else
5445 num_children = num_pointee_children;
5446 } break;
5447
5448 case clang::Type::LValueReference:
5449 case clang::Type::RValueReference: {
5450 const clang::ReferenceType *reference_type =
5451 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5452 clang::QualType pointee_type = reference_type->getPointeeType();
5453 uint32_t num_pointee_children =
5454 CompilerType(getASTContext(), pointee_type)
5455 .GetNumChildren(omit_empty_base_classes);
5456 // If this type points to a simple type, then it has 1 child
5457 if (num_pointee_children == 0)
5458 num_children = 1;
5459 else
5460 num_children = num_pointee_children;
5461 } break;
5462
5463 case clang::Type::Typedef:
5464 num_children =
5465 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5466 ->getDecl()
5467 ->getUnderlyingType())
5468 .GetNumChildren(omit_empty_base_classes);
5469 break;
5470
5471 case clang::Type::Auto:
5472 num_children =
5473 CompilerType(getASTContext(),
5474 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5475 .GetNumChildren(omit_empty_base_classes);
5476 break;
5477
5478 case clang::Type::Elaborated:
5479 num_children =
5480 CompilerType(
5481 getASTContext(),
5482 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5483 .GetNumChildren(omit_empty_base_classes);
5484 break;
5485
5486 case clang::Type::Paren:
5487 num_children =
5488 CompilerType(getASTContext(),
5489 llvm::cast<clang::ParenType>(qual_type)->desugar())
5490 .GetNumChildren(omit_empty_base_classes);
5491 break;
5492 default:
5493 break;
5494 }
5495 return num_children;
5496}
5497
5498CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5499 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005500}
5501
Greg Claytond8d4a572015-08-11 21:38:15 +00005502lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005503ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5504 if (type) {
5505 clang::QualType qual_type(GetQualType(type));
5506 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5507 if (type_class == clang::Type::Builtin) {
5508 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5509 case clang::BuiltinType::Void:
5510 return eBasicTypeVoid;
5511 case clang::BuiltinType::Bool:
5512 return eBasicTypeBool;
5513 case clang::BuiltinType::Char_S:
5514 return eBasicTypeSignedChar;
5515 case clang::BuiltinType::Char_U:
5516 return eBasicTypeUnsignedChar;
5517 case clang::BuiltinType::Char16:
5518 return eBasicTypeChar16;
5519 case clang::BuiltinType::Char32:
5520 return eBasicTypeChar32;
5521 case clang::BuiltinType::UChar:
5522 return eBasicTypeUnsignedChar;
5523 case clang::BuiltinType::SChar:
5524 return eBasicTypeSignedChar;
5525 case clang::BuiltinType::WChar_S:
5526 return eBasicTypeSignedWChar;
5527 case clang::BuiltinType::WChar_U:
5528 return eBasicTypeUnsignedWChar;
5529 case clang::BuiltinType::Short:
5530 return eBasicTypeShort;
5531 case clang::BuiltinType::UShort:
5532 return eBasicTypeUnsignedShort;
5533 case clang::BuiltinType::Int:
5534 return eBasicTypeInt;
5535 case clang::BuiltinType::UInt:
5536 return eBasicTypeUnsignedInt;
5537 case clang::BuiltinType::Long:
5538 return eBasicTypeLong;
5539 case clang::BuiltinType::ULong:
5540 return eBasicTypeUnsignedLong;
5541 case clang::BuiltinType::LongLong:
5542 return eBasicTypeLongLong;
5543 case clang::BuiltinType::ULongLong:
5544 return eBasicTypeUnsignedLongLong;
5545 case clang::BuiltinType::Int128:
5546 return eBasicTypeInt128;
5547 case clang::BuiltinType::UInt128:
5548 return eBasicTypeUnsignedInt128;
5549
5550 case clang::BuiltinType::Half:
5551 return eBasicTypeHalf;
5552 case clang::BuiltinType::Float:
5553 return eBasicTypeFloat;
5554 case clang::BuiltinType::Double:
5555 return eBasicTypeDouble;
5556 case clang::BuiltinType::LongDouble:
5557 return eBasicTypeLongDouble;
5558
5559 case clang::BuiltinType::NullPtr:
5560 return eBasicTypeNullPtr;
5561 case clang::BuiltinType::ObjCId:
5562 return eBasicTypeObjCID;
5563 case clang::BuiltinType::ObjCClass:
5564 return eBasicTypeObjCClass;
5565 case clang::BuiltinType::ObjCSel:
5566 return eBasicTypeObjCSel;
5567 default:
5568 return eBasicTypeOther;
5569 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005570 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005571 }
5572 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005573}
5574
Kate Stoneb9c1b512016-09-06 20:57:50 +00005575void ClangASTContext::ForEachEnumerator(
5576 lldb::opaque_compiler_type_t type,
5577 std::function<bool(const CompilerType &integer_type,
5578 const ConstString &name,
5579 const llvm::APSInt &value)> const &callback) {
5580 const clang::EnumType *enum_type =
5581 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5582 if (enum_type) {
5583 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5584 if (enum_decl) {
5585 CompilerType integer_type(this,
5586 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005587
Kate Stoneb9c1b512016-09-06 20:57:50 +00005588 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5589 for (enum_pos = enum_decl->enumerator_begin(),
5590 enum_end_pos = enum_decl->enumerator_end();
5591 enum_pos != enum_end_pos; ++enum_pos) {
5592 ConstString name(enum_pos->getNameAsString().c_str());
5593 if (!callback(integer_type, name, enum_pos->getInitVal()))
5594 break;
5595 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005596 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005597 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005598}
5599
Greg Claytond8d4a572015-08-11 21:38:15 +00005600#pragma mark Aggregate Types
5601
Kate Stoneb9c1b512016-09-06 20:57:50 +00005602uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5603 if (!type)
5604 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005605
Kate Stoneb9c1b512016-09-06 20:57:50 +00005606 uint32_t count = 0;
5607 clang::QualType qual_type(GetCanonicalQualType(type));
5608 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5609 switch (type_class) {
5610 case clang::Type::Record:
5611 if (GetCompleteType(type)) {
5612 const clang::RecordType *record_type =
5613 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5614 if (record_type) {
5615 clang::RecordDecl *record_decl = record_type->getDecl();
5616 if (record_decl) {
5617 uint32_t field_idx = 0;
5618 clang::RecordDecl::field_iterator field, field_end;
5619 for (field = record_decl->field_begin(),
5620 field_end = record_decl->field_end();
5621 field != field_end; ++field)
5622 ++field_idx;
5623 count = field_idx;
5624 }
5625 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005626 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005627 break;
5628
5629 case clang::Type::Typedef:
5630 count =
5631 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5632 ->getDecl()
5633 ->getUnderlyingType())
5634 .GetNumFields();
5635 break;
5636
5637 case clang::Type::Auto:
5638 count =
5639 CompilerType(getASTContext(),
5640 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5641 .GetNumFields();
5642 break;
5643
5644 case clang::Type::Elaborated:
5645 count = CompilerType(
5646 getASTContext(),
5647 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5648 .GetNumFields();
5649 break;
5650
5651 case clang::Type::Paren:
5652 count = CompilerType(getASTContext(),
5653 llvm::cast<clang::ParenType>(qual_type)->desugar())
5654 .GetNumFields();
5655 break;
5656
Sean Callananf9c622a2016-09-30 18:44:43 +00005657 case clang::Type::ObjCObjectPointer: {
5658 const clang::ObjCObjectPointerType *objc_class_type =
5659 qual_type->getAsObjCInterfacePointerType();
5660 const clang::ObjCInterfaceType *objc_interface_type =
5661 objc_class_type->getInterfaceType();
5662 if (objc_interface_type &&
5663 GetCompleteType((lldb::opaque_compiler_type_t)objc_interface_type)) {
5664 clang::ObjCInterfaceDecl *class_interface_decl =
5665 objc_interface_type->getDecl();
5666 if (class_interface_decl) {
5667 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005668 }
5669 }
5670 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005671 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005672
5673 case clang::Type::ObjCObject:
5674 case clang::Type::ObjCInterface:
5675 if (GetCompleteType(type)) {
5676 const clang::ObjCObjectType *objc_class_type =
5677 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5678 if (objc_class_type) {
5679 clang::ObjCInterfaceDecl *class_interface_decl =
5680 objc_class_type->getInterface();
5681
5682 if (class_interface_decl)
5683 count = class_interface_decl->ivar_size();
5684 }
5685 }
5686 break;
5687
5688 default:
5689 break;
5690 }
5691 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005692}
5693
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005694static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005695GetObjCFieldAtIndex(clang::ASTContext *ast,
5696 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5697 std::string &name, uint64_t *bit_offset_ptr,
5698 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5699 if (class_interface_decl) {
5700 if (idx < (class_interface_decl->ivar_size())) {
5701 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5702 ivar_end = class_interface_decl->ivar_end();
5703 uint32_t ivar_idx = 0;
5704
5705 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5706 ++ivar_pos, ++ivar_idx) {
5707 if (ivar_idx == idx) {
5708 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5709
5710 clang::QualType ivar_qual_type(ivar_decl->getType());
5711
5712 name.assign(ivar_decl->getNameAsString());
5713
5714 if (bit_offset_ptr) {
5715 const clang::ASTRecordLayout &interface_layout =
5716 ast->getASTObjCInterfaceLayout(class_interface_decl);
5717 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5718 }
5719
5720 const bool is_bitfield = ivar_pos->isBitField();
5721
5722 if (bitfield_bit_size_ptr) {
5723 *bitfield_bit_size_ptr = 0;
5724
5725 if (is_bitfield && ast) {
5726 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5727 llvm::APSInt bitfield_apsint;
5728 if (bitfield_bit_size_expr &&
5729 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5730 *ast)) {
5731 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5732 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005733 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005734 }
5735 if (is_bitfield_ptr)
5736 *is_bitfield_ptr = is_bitfield;
5737
5738 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005739 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005740 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005741 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005742 }
5743 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005744}
5745
Kate Stoneb9c1b512016-09-06 20:57:50 +00005746CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5747 size_t idx, std::string &name,
5748 uint64_t *bit_offset_ptr,
5749 uint32_t *bitfield_bit_size_ptr,
5750 bool *is_bitfield_ptr) {
5751 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005752 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005753
5754 clang::QualType qual_type(GetCanonicalQualType(type));
5755 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5756 switch (type_class) {
5757 case clang::Type::Record:
5758 if (GetCompleteType(type)) {
5759 const clang::RecordType *record_type =
5760 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5761 const clang::RecordDecl *record_decl = record_type->getDecl();
5762 uint32_t field_idx = 0;
5763 clang::RecordDecl::field_iterator field, field_end;
5764 for (field = record_decl->field_begin(),
5765 field_end = record_decl->field_end();
5766 field != field_end; ++field, ++field_idx) {
5767 if (idx == field_idx) {
5768 // Print the member type if requested
5769 // Print the member name and equal sign
5770 name.assign(field->getNameAsString());
5771
5772 // Figure out the type byte size (field_type_info.first) and
5773 // alignment (field_type_info.second) from the AST context.
5774 if (bit_offset_ptr) {
5775 const clang::ASTRecordLayout &record_layout =
5776 getASTContext()->getASTRecordLayout(record_decl);
5777 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5778 }
5779
5780 const bool is_bitfield = field->isBitField();
5781
5782 if (bitfield_bit_size_ptr) {
5783 *bitfield_bit_size_ptr = 0;
5784
5785 if (is_bitfield) {
5786 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5787 llvm::APSInt bitfield_apsint;
5788 if (bitfield_bit_size_expr &&
5789 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5790 *getASTContext())) {
5791 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5792 }
5793 }
5794 }
5795 if (is_bitfield_ptr)
5796 *is_bitfield_ptr = is_bitfield;
5797
5798 return CompilerType(getASTContext(), field->getType());
5799 }
5800 }
5801 }
5802 break;
5803
Sean Callananf9c622a2016-09-30 18:44:43 +00005804 case clang::Type::ObjCObjectPointer: {
5805 const clang::ObjCObjectPointerType *objc_class_type =
5806 qual_type->getAsObjCInterfacePointerType();
5807 const clang::ObjCInterfaceType *objc_interface_type =
5808 objc_class_type->getInterfaceType();
5809 if (objc_interface_type &&
5810 GetCompleteType((lldb::opaque_compiler_type_t)objc_interface_type)) {
5811 clang::ObjCInterfaceDecl *class_interface_decl =
5812 objc_interface_type->getDecl();
5813 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005814 return CompilerType(
5815 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5816 idx, name, bit_offset_ptr,
5817 bitfield_bit_size_ptr, is_bitfield_ptr));
5818 }
5819 }
5820 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005821 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005822
5823 case clang::Type::ObjCObject:
5824 case clang::Type::ObjCInterface:
5825 if (GetCompleteType(type)) {
5826 const clang::ObjCObjectType *objc_class_type =
5827 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5828 assert(objc_class_type);
5829 if (objc_class_type) {
5830 clang::ObjCInterfaceDecl *class_interface_decl =
5831 objc_class_type->getInterface();
5832 return CompilerType(
5833 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5834 idx, name, bit_offset_ptr,
5835 bitfield_bit_size_ptr, is_bitfield_ptr));
5836 }
5837 }
5838 break;
5839
5840 case clang::Type::Typedef:
5841 return CompilerType(getASTContext(),
5842 llvm::cast<clang::TypedefType>(qual_type)
5843 ->getDecl()
5844 ->getUnderlyingType())
5845 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5846 is_bitfield_ptr);
5847
5848 case clang::Type::Auto:
5849 return CompilerType(
5850 getASTContext(),
5851 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5852 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5853 is_bitfield_ptr);
5854
5855 case clang::Type::Elaborated:
5856 return CompilerType(
5857 getASTContext(),
5858 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5859 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5860 is_bitfield_ptr);
5861
5862 case clang::Type::Paren:
5863 return CompilerType(getASTContext(),
5864 llvm::cast<clang::ParenType>(qual_type)->desugar())
5865 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5866 is_bitfield_ptr);
5867
5868 default:
5869 break;
5870 }
5871 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005872}
5873
Greg Clayton99558cc42015-08-24 23:46:31 +00005874uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005875ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
5876 uint32_t count = 0;
5877 clang::QualType qual_type(GetCanonicalQualType(type));
5878 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5879 switch (type_class) {
5880 case clang::Type::Record:
5881 if (GetCompleteType(type)) {
5882 const clang::CXXRecordDecl *cxx_record_decl =
5883 qual_type->getAsCXXRecordDecl();
5884 if (cxx_record_decl)
5885 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00005886 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005887 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00005888
Kate Stoneb9c1b512016-09-06 20:57:50 +00005889 case clang::Type::ObjCObjectPointer:
5890 count = GetPointeeType(type).GetNumDirectBaseClasses();
5891 break;
5892
5893 case clang::Type::ObjCObject:
5894 if (GetCompleteType(type)) {
5895 const clang::ObjCObjectType *objc_class_type =
5896 qual_type->getAsObjCQualifiedInterfaceType();
5897 if (objc_class_type) {
5898 clang::ObjCInterfaceDecl *class_interface_decl =
5899 objc_class_type->getInterface();
5900
5901 if (class_interface_decl && class_interface_decl->getSuperClass())
5902 count = 1;
5903 }
5904 }
5905 break;
5906 case clang::Type::ObjCInterface:
5907 if (GetCompleteType(type)) {
5908 const clang::ObjCInterfaceType *objc_interface_type =
5909 qual_type->getAs<clang::ObjCInterfaceType>();
5910 if (objc_interface_type) {
5911 clang::ObjCInterfaceDecl *class_interface_decl =
5912 objc_interface_type->getInterface();
5913
5914 if (class_interface_decl && class_interface_decl->getSuperClass())
5915 count = 1;
5916 }
5917 }
5918 break;
5919
5920 case clang::Type::Typedef:
5921 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5922 ->getDecl()
5923 ->getUnderlyingType()
5924 .getAsOpaquePtr());
5925 break;
5926
5927 case clang::Type::Auto:
5928 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5929 ->getDeducedType()
5930 .getAsOpaquePtr());
5931 break;
5932
5933 case clang::Type::Elaborated:
5934 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5935 ->getNamedType()
5936 .getAsOpaquePtr());
5937 break;
5938
5939 case clang::Type::Paren:
5940 return GetNumDirectBaseClasses(
5941 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5942
5943 default:
5944 break;
5945 }
5946 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00005947}
5948
5949uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005950ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
5951 uint32_t count = 0;
5952 clang::QualType qual_type(GetCanonicalQualType(type));
5953 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5954 switch (type_class) {
5955 case clang::Type::Record:
5956 if (GetCompleteType(type)) {
5957 const clang::CXXRecordDecl *cxx_record_decl =
5958 qual_type->getAsCXXRecordDecl();
5959 if (cxx_record_decl)
5960 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00005961 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005962 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00005963
Kate Stoneb9c1b512016-09-06 20:57:50 +00005964 case clang::Type::Typedef:
5965 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5966 ->getDecl()
5967 ->getUnderlyingType()
5968 .getAsOpaquePtr());
5969 break;
5970
5971 case clang::Type::Auto:
5972 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5973 ->getDeducedType()
5974 .getAsOpaquePtr());
5975 break;
5976
5977 case clang::Type::Elaborated:
5978 count =
5979 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5980 ->getNamedType()
5981 .getAsOpaquePtr());
5982 break;
5983
5984 case clang::Type::Paren:
5985 count = GetNumVirtualBaseClasses(
5986 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5987 break;
5988
5989 default:
5990 break;
5991 }
5992 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00005993}
5994
Kate Stoneb9c1b512016-09-06 20:57:50 +00005995CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
5996 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
5997 clang::QualType qual_type(GetCanonicalQualType(type));
5998 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5999 switch (type_class) {
6000 case clang::Type::Record:
6001 if (GetCompleteType(type)) {
6002 const clang::CXXRecordDecl *cxx_record_decl =
6003 qual_type->getAsCXXRecordDecl();
6004 if (cxx_record_decl) {
6005 uint32_t curr_idx = 0;
6006 clang::CXXRecordDecl::base_class_const_iterator base_class,
6007 base_class_end;
6008 for (base_class = cxx_record_decl->bases_begin(),
6009 base_class_end = cxx_record_decl->bases_end();
6010 base_class != base_class_end; ++base_class, ++curr_idx) {
6011 if (curr_idx == idx) {
6012 if (bit_offset_ptr) {
6013 const clang::ASTRecordLayout &record_layout =
6014 getASTContext()->getASTRecordLayout(cxx_record_decl);
6015 const clang::CXXRecordDecl *base_class_decl =
6016 llvm::cast<clang::CXXRecordDecl>(
6017 base_class->getType()
6018 ->getAs<clang::RecordType>()
6019 ->getDecl());
6020 if (base_class->isVirtual())
6021 *bit_offset_ptr =
6022 record_layout.getVBaseClassOffset(base_class_decl)
6023 .getQuantity() *
6024 8;
6025 else
6026 *bit_offset_ptr =
6027 record_layout.getBaseClassOffset(base_class_decl)
6028 .getQuantity() *
6029 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006030 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006031 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6032 }
6033 }
6034 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006035 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006036 break;
6037
6038 case clang::Type::ObjCObjectPointer:
6039 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6040
6041 case clang::Type::ObjCObject:
6042 if (idx == 0 && GetCompleteType(type)) {
6043 const clang::ObjCObjectType *objc_class_type =
6044 qual_type->getAsObjCQualifiedInterfaceType();
6045 if (objc_class_type) {
6046 clang::ObjCInterfaceDecl *class_interface_decl =
6047 objc_class_type->getInterface();
6048
6049 if (class_interface_decl) {
6050 clang::ObjCInterfaceDecl *superclass_interface_decl =
6051 class_interface_decl->getSuperClass();
6052 if (superclass_interface_decl) {
6053 if (bit_offset_ptr)
6054 *bit_offset_ptr = 0;
6055 return CompilerType(getASTContext(),
6056 getASTContext()->getObjCInterfaceType(
6057 superclass_interface_decl));
6058 }
6059 }
6060 }
6061 }
6062 break;
6063 case clang::Type::ObjCInterface:
6064 if (idx == 0 && GetCompleteType(type)) {
6065 const clang::ObjCObjectType *objc_interface_type =
6066 qual_type->getAs<clang::ObjCInterfaceType>();
6067 if (objc_interface_type) {
6068 clang::ObjCInterfaceDecl *class_interface_decl =
6069 objc_interface_type->getInterface();
6070
6071 if (class_interface_decl) {
6072 clang::ObjCInterfaceDecl *superclass_interface_decl =
6073 class_interface_decl->getSuperClass();
6074 if (superclass_interface_decl) {
6075 if (bit_offset_ptr)
6076 *bit_offset_ptr = 0;
6077 return CompilerType(getASTContext(),
6078 getASTContext()->getObjCInterfaceType(
6079 superclass_interface_decl));
6080 }
6081 }
6082 }
6083 }
6084 break;
6085
6086 case clang::Type::Typedef:
6087 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6088 ->getDecl()
6089 ->getUnderlyingType()
6090 .getAsOpaquePtr(),
6091 idx, bit_offset_ptr);
6092
6093 case clang::Type::Auto:
6094 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6095 ->getDeducedType()
6096 .getAsOpaquePtr(),
6097 idx, bit_offset_ptr);
6098
6099 case clang::Type::Elaborated:
6100 return GetDirectBaseClassAtIndex(
6101 llvm::cast<clang::ElaboratedType>(qual_type)
6102 ->getNamedType()
6103 .getAsOpaquePtr(),
6104 idx, bit_offset_ptr);
6105
6106 case clang::Type::Paren:
6107 return GetDirectBaseClassAtIndex(
6108 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6109 idx, bit_offset_ptr);
6110
6111 default:
6112 break;
6113 }
6114 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006115}
6116
Kate Stoneb9c1b512016-09-06 20:57:50 +00006117CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6118 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6119 clang::QualType qual_type(GetCanonicalQualType(type));
6120 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6121 switch (type_class) {
6122 case clang::Type::Record:
6123 if (GetCompleteType(type)) {
6124 const clang::CXXRecordDecl *cxx_record_decl =
6125 qual_type->getAsCXXRecordDecl();
6126 if (cxx_record_decl) {
6127 uint32_t curr_idx = 0;
6128 clang::CXXRecordDecl::base_class_const_iterator base_class,
6129 base_class_end;
6130 for (base_class = cxx_record_decl->vbases_begin(),
6131 base_class_end = cxx_record_decl->vbases_end();
6132 base_class != base_class_end; ++base_class, ++curr_idx) {
6133 if (curr_idx == idx) {
6134 if (bit_offset_ptr) {
6135 const clang::ASTRecordLayout &record_layout =
6136 getASTContext()->getASTRecordLayout(cxx_record_decl);
6137 const clang::CXXRecordDecl *base_class_decl =
6138 llvm::cast<clang::CXXRecordDecl>(
6139 base_class->getType()
6140 ->getAs<clang::RecordType>()
6141 ->getDecl());
6142 *bit_offset_ptr =
6143 record_layout.getVBaseClassOffset(base_class_decl)
6144 .getQuantity() *
6145 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006146 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006147 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6148 }
6149 }
6150 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006151 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006152 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006153
Kate Stoneb9c1b512016-09-06 20:57:50 +00006154 case clang::Type::Typedef:
6155 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6156 ->getDecl()
6157 ->getUnderlyingType()
6158 .getAsOpaquePtr(),
6159 idx, bit_offset_ptr);
6160
6161 case clang::Type::Auto:
6162 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6163 ->getDeducedType()
6164 .getAsOpaquePtr(),
6165 idx, bit_offset_ptr);
6166
6167 case clang::Type::Elaborated:
6168 return GetVirtualBaseClassAtIndex(
6169 llvm::cast<clang::ElaboratedType>(qual_type)
6170 ->getNamedType()
6171 .getAsOpaquePtr(),
6172 idx, bit_offset_ptr);
6173
6174 case clang::Type::Paren:
6175 return GetVirtualBaseClassAtIndex(
6176 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6177 idx, bit_offset_ptr);
6178
6179 default:
6180 break;
6181 }
6182 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006183}
6184
Greg Claytond8d4a572015-08-11 21:38:15 +00006185// If a pointer to a pointee type (the clang_type arg) says that it has no
6186// children, then we either need to trust it, or override it and return a
6187// different result. For example, an "int *" has one child that is an integer,
6188// but a function pointer doesn't have any children. Likewise if a Record type
6189// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006190uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6191 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006192 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006193
6194 clang::QualType qual_type(type.getCanonicalType());
6195 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6196 switch (type_class) {
6197 case clang::Type::Builtin:
6198 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6199 case clang::BuiltinType::UnknownAny:
6200 case clang::BuiltinType::Void:
6201 case clang::BuiltinType::NullPtr:
6202 case clang::BuiltinType::OCLEvent:
6203 case clang::BuiltinType::OCLImage1dRO:
6204 case clang::BuiltinType::OCLImage1dWO:
6205 case clang::BuiltinType::OCLImage1dRW:
6206 case clang::BuiltinType::OCLImage1dArrayRO:
6207 case clang::BuiltinType::OCLImage1dArrayWO:
6208 case clang::BuiltinType::OCLImage1dArrayRW:
6209 case clang::BuiltinType::OCLImage1dBufferRO:
6210 case clang::BuiltinType::OCLImage1dBufferWO:
6211 case clang::BuiltinType::OCLImage1dBufferRW:
6212 case clang::BuiltinType::OCLImage2dRO:
6213 case clang::BuiltinType::OCLImage2dWO:
6214 case clang::BuiltinType::OCLImage2dRW:
6215 case clang::BuiltinType::OCLImage2dArrayRO:
6216 case clang::BuiltinType::OCLImage2dArrayWO:
6217 case clang::BuiltinType::OCLImage2dArrayRW:
6218 case clang::BuiltinType::OCLImage3dRO:
6219 case clang::BuiltinType::OCLImage3dWO:
6220 case clang::BuiltinType::OCLImage3dRW:
6221 case clang::BuiltinType::OCLSampler:
6222 return 0;
6223 case clang::BuiltinType::Bool:
6224 case clang::BuiltinType::Char_U:
6225 case clang::BuiltinType::UChar:
6226 case clang::BuiltinType::WChar_U:
6227 case clang::BuiltinType::Char16:
6228 case clang::BuiltinType::Char32:
6229 case clang::BuiltinType::UShort:
6230 case clang::BuiltinType::UInt:
6231 case clang::BuiltinType::ULong:
6232 case clang::BuiltinType::ULongLong:
6233 case clang::BuiltinType::UInt128:
6234 case clang::BuiltinType::Char_S:
6235 case clang::BuiltinType::SChar:
6236 case clang::BuiltinType::WChar_S:
6237 case clang::BuiltinType::Short:
6238 case clang::BuiltinType::Int:
6239 case clang::BuiltinType::Long:
6240 case clang::BuiltinType::LongLong:
6241 case clang::BuiltinType::Int128:
6242 case clang::BuiltinType::Float:
6243 case clang::BuiltinType::Double:
6244 case clang::BuiltinType::LongDouble:
6245 case clang::BuiltinType::Dependent:
6246 case clang::BuiltinType::Overload:
6247 case clang::BuiltinType::ObjCId:
6248 case clang::BuiltinType::ObjCClass:
6249 case clang::BuiltinType::ObjCSel:
6250 case clang::BuiltinType::BoundMember:
6251 case clang::BuiltinType::Half:
6252 case clang::BuiltinType::ARCUnbridgedCast:
6253 case clang::BuiltinType::PseudoObject:
6254 case clang::BuiltinType::BuiltinFn:
6255 case clang::BuiltinType::OMPArraySection:
6256 return 1;
6257 default:
6258 return 0;
6259 }
6260 break;
6261
6262 case clang::Type::Complex:
6263 return 1;
6264 case clang::Type::Pointer:
6265 return 1;
6266 case clang::Type::BlockPointer:
6267 return 0; // If block pointers don't have debug info, then no children for
6268 // them
6269 case clang::Type::LValueReference:
6270 return 1;
6271 case clang::Type::RValueReference:
6272 return 1;
6273 case clang::Type::MemberPointer:
6274 return 0;
6275 case clang::Type::ConstantArray:
6276 return 0;
6277 case clang::Type::IncompleteArray:
6278 return 0;
6279 case clang::Type::VariableArray:
6280 return 0;
6281 case clang::Type::DependentSizedArray:
6282 return 0;
6283 case clang::Type::DependentSizedExtVector:
6284 return 0;
6285 case clang::Type::Vector:
6286 return 0;
6287 case clang::Type::ExtVector:
6288 return 0;
6289 case clang::Type::FunctionProto:
6290 return 0; // When we function pointers, they have no children...
6291 case clang::Type::FunctionNoProto:
6292 return 0; // When we function pointers, they have no children...
6293 case clang::Type::UnresolvedUsing:
6294 return 0;
6295 case clang::Type::Paren:
6296 return GetNumPointeeChildren(
6297 llvm::cast<clang::ParenType>(qual_type)->desugar());
6298 case clang::Type::Typedef:
6299 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6300 ->getDecl()
6301 ->getUnderlyingType());
6302 case clang::Type::Auto:
6303 return GetNumPointeeChildren(
6304 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6305 case clang::Type::Elaborated:
6306 return GetNumPointeeChildren(
6307 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6308 case clang::Type::TypeOfExpr:
6309 return 0;
6310 case clang::Type::TypeOf:
6311 return 0;
6312 case clang::Type::Decltype:
6313 return 0;
6314 case clang::Type::Record:
6315 return 0;
6316 case clang::Type::Enum:
6317 return 1;
6318 case clang::Type::TemplateTypeParm:
6319 return 1;
6320 case clang::Type::SubstTemplateTypeParm:
6321 return 1;
6322 case clang::Type::TemplateSpecialization:
6323 return 1;
6324 case clang::Type::InjectedClassName:
6325 return 0;
6326 case clang::Type::DependentName:
6327 return 1;
6328 case clang::Type::DependentTemplateSpecialization:
6329 return 1;
6330 case clang::Type::ObjCObject:
6331 return 0;
6332 case clang::Type::ObjCInterface:
6333 return 0;
6334 case clang::Type::ObjCObjectPointer:
6335 return 1;
6336 default:
6337 break;
6338 }
6339 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006340}
6341
Kate Stoneb9c1b512016-09-06 20:57:50 +00006342CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6343 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6344 bool transparent_pointers, bool omit_empty_base_classes,
6345 bool ignore_array_bounds, std::string &child_name,
6346 uint32_t &child_byte_size, int32_t &child_byte_offset,
6347 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6348 bool &child_is_base_class, bool &child_is_deref_of_parent,
6349 ValueObject *valobj, uint64_t &language_flags) {
6350 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006351 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006352
Kate Stoneb9c1b512016-09-06 20:57:50 +00006353 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6354 const clang::Type::TypeClass parent_type_class =
6355 parent_qual_type->getTypeClass();
6356 child_bitfield_bit_size = 0;
6357 child_bitfield_bit_offset = 0;
6358 child_is_base_class = false;
6359 language_flags = 0;
6360
6361 const bool idx_is_valid = idx < GetNumChildren(type, omit_empty_base_classes);
6362 uint32_t bit_offset;
6363 switch (parent_type_class) {
6364 case clang::Type::Builtin:
6365 if (idx_is_valid) {
6366 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6367 case clang::BuiltinType::ObjCId:
6368 case clang::BuiltinType::ObjCClass:
6369 child_name = "isa";
6370 child_byte_size =
6371 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6372 CHAR_BIT;
6373 return CompilerType(getASTContext(),
6374 getASTContext()->ObjCBuiltinClassTy);
6375
6376 default:
6377 break;
6378 }
6379 }
6380 break;
6381
6382 case clang::Type::Record:
6383 if (idx_is_valid && GetCompleteType(type)) {
6384 const clang::RecordType *record_type =
6385 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6386 const clang::RecordDecl *record_decl = record_type->getDecl();
6387 assert(record_decl);
6388 const clang::ASTRecordLayout &record_layout =
6389 getASTContext()->getASTRecordLayout(record_decl);
6390 uint32_t child_idx = 0;
6391
6392 const clang::CXXRecordDecl *cxx_record_decl =
6393 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6394 if (cxx_record_decl) {
6395 // We might have base classes to print out first
6396 clang::CXXRecordDecl::base_class_const_iterator base_class,
6397 base_class_end;
6398 for (base_class = cxx_record_decl->bases_begin(),
6399 base_class_end = cxx_record_decl->bases_end();
6400 base_class != base_class_end; ++base_class) {
6401 const clang::CXXRecordDecl *base_class_decl = nullptr;
6402
6403 // Skip empty base classes
6404 if (omit_empty_base_classes) {
6405 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6406 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6407 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
6408 continue;
6409 }
6410
6411 if (idx == child_idx) {
6412 if (base_class_decl == nullptr)
6413 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6414 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6415
6416 if (base_class->isVirtual()) {
6417 bool handled = false;
6418 if (valobj) {
6419 Error err;
6420 AddressType addr_type = eAddressTypeInvalid;
6421 lldb::addr_t vtable_ptr_addr =
6422 valobj->GetCPPVTableAddress(addr_type);
6423
6424 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS &&
6425 addr_type == eAddressTypeLoad) {
6426
6427 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
6428 Process *process = exe_ctx.GetProcessPtr();
6429 if (process) {
6430 clang::VTableContextBase *vtable_ctx =
6431 getASTContext()->getVTableContext();
6432 if (vtable_ctx) {
6433 if (vtable_ctx->isMicrosoft()) {
6434 clang::MicrosoftVTableContext *msoft_vtable_ctx =
6435 static_cast<clang::MicrosoftVTableContext *>(
6436 vtable_ctx);
6437
6438 if (vtable_ptr_addr) {
6439 const lldb::addr_t vbtable_ptr_addr =
6440 vtable_ptr_addr +
6441 record_layout.getVBPtrOffset().getQuantity();
6442
6443 const lldb::addr_t vbtable_ptr =
6444 process->ReadPointerFromMemory(vbtable_ptr_addr,
6445 err);
6446 if (vbtable_ptr != LLDB_INVALID_ADDRESS) {
6447 // Get the index into the virtual base table. The
6448 // index is the index in uint32_t from vbtable_ptr
6449 const unsigned vbtable_index =
6450 msoft_vtable_ctx->getVBTableIndex(
6451 cxx_record_decl, base_class_decl);
6452 const lldb::addr_t base_offset_addr =
6453 vbtable_ptr + vbtable_index * 4;
6454 const uint32_t base_offset =
6455 process->ReadUnsignedIntegerFromMemory(
6456 base_offset_addr, 4, UINT32_MAX, err);
6457 if (base_offset != UINT32_MAX) {
6458 handled = true;
6459 bit_offset = base_offset * 8;
6460 }
6461 }
6462 }
6463 } else {
6464 clang::ItaniumVTableContext *itanium_vtable_ctx =
6465 static_cast<clang::ItaniumVTableContext *>(
6466 vtable_ctx);
6467 if (vtable_ptr_addr) {
6468 const lldb::addr_t vtable_ptr =
6469 process->ReadPointerFromMemory(vtable_ptr_addr,
6470 err);
6471 if (vtable_ptr != LLDB_INVALID_ADDRESS) {
6472 clang::CharUnits base_offset_offset =
6473 itanium_vtable_ctx->getVirtualBaseOffsetOffset(
6474 cxx_record_decl, base_class_decl);
6475 const lldb::addr_t base_offset_addr =
6476 vtable_ptr + base_offset_offset.getQuantity();
6477 const uint32_t base_offset_size =
6478 process->GetAddressByteSize();
6479 const uint64_t base_offset =
6480 process->ReadUnsignedIntegerFromMemory(
6481 base_offset_addr, base_offset_size,
6482 UINT32_MAX, err);
6483 if (base_offset < UINT32_MAX) {
6484 handled = true;
6485 bit_offset = base_offset * 8;
6486 }
6487 }
6488 }
6489 }
6490 }
6491 }
6492 }
6493 }
6494 if (!handled)
6495 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6496 .getQuantity() *
6497 8;
6498 } else
6499 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6500 .getQuantity() *
6501 8;
6502
6503 // Base classes should be a multiple of 8 bits in size
6504 child_byte_offset = bit_offset / 8;
6505 CompilerType base_class_clang_type(getASTContext(),
6506 base_class->getType());
6507 child_name = base_class_clang_type.GetTypeName().AsCString("");
6508 uint64_t base_class_clang_type_bit_size =
6509 base_class_clang_type.GetBitSize(
6510 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6511
6512 // Base classes bit sizes should be a multiple of 8 bits in size
6513 assert(base_class_clang_type_bit_size % 8 == 0);
6514 child_byte_size = base_class_clang_type_bit_size / 8;
6515 child_is_base_class = true;
6516 return base_class_clang_type;
6517 }
6518 // We don't increment the child index in the for loop since we might
6519 // be skipping empty base classes
6520 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006521 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006522 }
6523 // Make sure index is in range...
6524 uint32_t field_idx = 0;
6525 clang::RecordDecl::field_iterator field, field_end;
6526 for (field = record_decl->field_begin(),
6527 field_end = record_decl->field_end();
6528 field != field_end; ++field, ++field_idx, ++child_idx) {
6529 if (idx == child_idx) {
6530 // Print the member type if requested
6531 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006532 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006533
6534 // Figure out the type byte size (field_type_info.first) and
6535 // alignment (field_type_info.second) from the AST context.
6536 CompilerType field_clang_type(getASTContext(), field->getType());
6537 assert(field_idx < record_layout.getFieldCount());
6538 child_byte_size = field_clang_type.GetByteSize(
6539 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6540 const uint32_t child_bit_size = child_byte_size * 8;
6541
6542 // Figure out the field offset within the current struct/union/class
6543 // type
6544 bit_offset = record_layout.getFieldOffset(field_idx);
6545 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6546 child_bitfield_bit_size)) {
6547 child_bitfield_bit_offset = bit_offset % child_bit_size;
6548 const uint32_t child_bit_offset =
6549 bit_offset - child_bitfield_bit_offset;
6550 child_byte_offset = child_bit_offset / 8;
6551 } else {
6552 child_byte_offset = bit_offset / 8;
6553 }
6554
6555 return field_clang_type;
6556 }
6557 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006558 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006559 break;
6560
6561 case clang::Type::ObjCObject:
6562 case clang::Type::ObjCInterface:
6563 if (idx_is_valid && GetCompleteType(type)) {
6564 const clang::ObjCObjectType *objc_class_type =
6565 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6566 assert(objc_class_type);
6567 if (objc_class_type) {
6568 uint32_t child_idx = 0;
6569 clang::ObjCInterfaceDecl *class_interface_decl =
6570 objc_class_type->getInterface();
6571
6572 if (class_interface_decl) {
6573
6574 const clang::ASTRecordLayout &interface_layout =
6575 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6576 clang::ObjCInterfaceDecl *superclass_interface_decl =
6577 class_interface_decl->getSuperClass();
6578 if (superclass_interface_decl) {
6579 if (omit_empty_base_classes) {
6580 CompilerType base_class_clang_type(
6581 getASTContext(), getASTContext()->getObjCInterfaceType(
6582 superclass_interface_decl));
6583 if (base_class_clang_type.GetNumChildren(
6584 omit_empty_base_classes) > 0) {
6585 if (idx == 0) {
6586 clang::QualType ivar_qual_type(
6587 getASTContext()->getObjCInterfaceType(
6588 superclass_interface_decl));
6589
6590 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006591 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006592
6593 clang::TypeInfo ivar_type_info =
6594 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6595
6596 child_byte_size = ivar_type_info.Width / 8;
6597 child_byte_offset = 0;
6598 child_is_base_class = true;
6599
6600 return CompilerType(getASTContext(), ivar_qual_type);
6601 }
6602
6603 ++child_idx;
6604 }
6605 } else
6606 ++child_idx;
6607 }
6608
6609 const uint32_t superclass_idx = child_idx;
6610
6611 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6612 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6613 ivar_end = class_interface_decl->ivar_end();
6614
6615 for (ivar_pos = class_interface_decl->ivar_begin();
6616 ivar_pos != ivar_end; ++ivar_pos) {
6617 if (child_idx == idx) {
6618 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6619
6620 clang::QualType ivar_qual_type(ivar_decl->getType());
6621
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006622 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006623
6624 clang::TypeInfo ivar_type_info =
6625 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6626
6627 child_byte_size = ivar_type_info.Width / 8;
6628
6629 // Figure out the field offset within the current
6630 // struct/union/class type
6631 // For ObjC objects, we can't trust the bit offset we get from
6632 // the Clang AST, since
6633 // that doesn't account for the space taken up by unbacked
6634 // properties, or from
6635 // the changing size of base classes that are newer than this
6636 // class.
6637 // So if we have a process around that we can ask about this
6638 // object, do so.
6639 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6640 Process *process = nullptr;
6641 if (exe_ctx)
6642 process = exe_ctx->GetProcessPtr();
6643 if (process) {
6644 ObjCLanguageRuntime *objc_runtime =
6645 process->GetObjCLanguageRuntime();
6646 if (objc_runtime != nullptr) {
6647 CompilerType parent_ast_type(getASTContext(),
6648 parent_qual_type);
6649 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6650 parent_ast_type, ivar_decl->getNameAsString().c_str());
6651 }
6652 }
6653
6654 // Setting this to UINT32_MAX to make sure we don't compute it
6655 // twice...
6656 bit_offset = UINT32_MAX;
6657
6658 if (child_byte_offset ==
6659 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6660 bit_offset = interface_layout.getFieldOffset(child_idx -
6661 superclass_idx);
6662 child_byte_offset = bit_offset / 8;
6663 }
6664
6665 // Note, the ObjC Ivar Byte offset is just that, it doesn't
6666 // account for the bit offset
6667 // of a bitfield within its containing object. So regardless of
6668 // where we get the byte
6669 // offset from, we still need to get the bit offset for
6670 // bitfields from the layout.
6671
6672 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6673 child_bitfield_bit_size)) {
6674 if (bit_offset == UINT32_MAX)
6675 bit_offset = interface_layout.getFieldOffset(
6676 child_idx - superclass_idx);
6677
6678 child_bitfield_bit_offset = bit_offset % 8;
6679 }
6680 return CompilerType(getASTContext(), ivar_qual_type);
6681 }
6682 ++child_idx;
6683 }
6684 }
6685 }
6686 }
6687 }
6688 break;
6689
6690 case clang::Type::ObjCObjectPointer:
6691 if (idx_is_valid) {
6692 CompilerType pointee_clang_type(GetPointeeType(type));
6693
6694 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6695 child_is_deref_of_parent = false;
6696 bool tmp_child_is_deref_of_parent = false;
6697 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6698 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6699 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6700 child_bitfield_bit_size, child_bitfield_bit_offset,
6701 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6702 language_flags);
6703 } else {
6704 child_is_deref_of_parent = true;
6705 const char *parent_name =
6706 valobj ? valobj->GetName().GetCString() : NULL;
6707 if (parent_name) {
6708 child_name.assign(1, '*');
6709 child_name += parent_name;
6710 }
6711
6712 // We have a pointer to an simple type
6713 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
6714 child_byte_size = pointee_clang_type.GetByteSize(
6715 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6716 child_byte_offset = 0;
6717 return pointee_clang_type;
6718 }
6719 }
6720 }
6721 break;
6722
6723 case clang::Type::Vector:
6724 case clang::Type::ExtVector:
6725 if (idx_is_valid) {
6726 const clang::VectorType *array =
6727 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6728 if (array) {
6729 CompilerType element_type(getASTContext(), array->getElementType());
6730 if (element_type.GetCompleteType()) {
6731 char element_name[64];
6732 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6733 static_cast<uint64_t>(idx));
6734 child_name.assign(element_name);
6735 child_byte_size = element_type.GetByteSize(
6736 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6737 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6738 return element_type;
6739 }
6740 }
6741 }
6742 break;
6743
6744 case clang::Type::ConstantArray:
6745 case clang::Type::IncompleteArray:
6746 if (ignore_array_bounds || idx_is_valid) {
6747 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6748 if (array) {
6749 CompilerType element_type(getASTContext(), array->getElementType());
6750 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006751 child_name = llvm::formatv("[{0}]", idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006752 child_byte_size = element_type.GetByteSize(
6753 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6754 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6755 return element_type;
6756 }
6757 }
6758 }
6759 break;
6760
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006761 case clang::Type::Pointer: {
6762 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006763
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006764 // Don't dereference "void *" pointers
6765 if (pointee_clang_type.IsVoidType())
6766 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006767
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006768 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6769 child_is_deref_of_parent = false;
6770 bool tmp_child_is_deref_of_parent = false;
6771 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6772 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6773 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6774 child_bitfield_bit_size, child_bitfield_bit_offset,
6775 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6776 language_flags);
6777 } else {
6778 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006779
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006780 const char *parent_name =
6781 valobj ? valobj->GetName().GetCString() : NULL;
6782 if (parent_name) {
6783 child_name.assign(1, '*');
6784 child_name += parent_name;
6785 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006786
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006787 // We have a pointer to an simple type
6788 if (idx == 0) {
6789 child_byte_size = pointee_clang_type.GetByteSize(
6790 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6791 child_byte_offset = 0;
6792 return pointee_clang_type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006793 }
6794 }
6795 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006796 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006797
6798 case clang::Type::LValueReference:
6799 case clang::Type::RValueReference:
6800 if (idx_is_valid) {
6801 const clang::ReferenceType *reference_type =
6802 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6803 CompilerType pointee_clang_type(getASTContext(),
6804 reference_type->getPointeeType());
6805 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6806 child_is_deref_of_parent = false;
6807 bool tmp_child_is_deref_of_parent = false;
6808 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6809 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6810 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6811 child_bitfield_bit_size, child_bitfield_bit_offset,
6812 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6813 language_flags);
6814 } else {
6815 const char *parent_name =
6816 valobj ? valobj->GetName().GetCString() : NULL;
6817 if (parent_name) {
6818 child_name.assign(1, '&');
6819 child_name += parent_name;
6820 }
6821
6822 // We have a pointer to an simple type
6823 if (idx == 0) {
6824 child_byte_size = pointee_clang_type.GetByteSize(
6825 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6826 child_byte_offset = 0;
6827 return pointee_clang_type;
6828 }
6829 }
6830 }
6831 break;
6832
6833 case clang::Type::Typedef: {
6834 CompilerType typedefed_clang_type(
6835 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
6836 ->getDecl()
6837 ->getUnderlyingType());
6838 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
6839 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6840 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6841 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6842 child_is_deref_of_parent, valobj, language_flags);
6843 } break;
6844
6845 case clang::Type::Auto: {
6846 CompilerType elaborated_clang_type(
6847 getASTContext(),
6848 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
6849 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6850 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6851 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6852 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6853 child_is_deref_of_parent, valobj, language_flags);
6854 }
6855
6856 case clang::Type::Elaborated: {
6857 CompilerType elaborated_clang_type(
6858 getASTContext(),
6859 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
6860 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6861 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6862 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6863 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6864 child_is_deref_of_parent, valobj, language_flags);
6865 }
6866
6867 case clang::Type::Paren: {
6868 CompilerType paren_clang_type(
6869 getASTContext(),
6870 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
6871 return paren_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 }
6877
6878 default:
6879 break;
6880 }
6881 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006882}
6883
Kate Stoneb9c1b512016-09-06 20:57:50 +00006884static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
6885 const clang::CXXBaseSpecifier *base_spec,
6886 bool omit_empty_base_classes) {
6887 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006888
Kate Stoneb9c1b512016-09-06 20:57:50 +00006889 const clang::CXXRecordDecl *cxx_record_decl =
6890 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6891
6892 // const char *super_name = record_decl->getNameAsCString();
6893 // const char *base_name =
6894 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6895 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6896 //
6897 if (cxx_record_decl) {
6898 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6899 for (base_class = cxx_record_decl->bases_begin(),
6900 base_class_end = cxx_record_decl->bases_end();
6901 base_class != base_class_end; ++base_class) {
6902 if (omit_empty_base_classes) {
6903 if (BaseSpecifierIsEmpty(base_class))
6904 continue;
6905 }
6906
6907 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
6908 // super_name, base_name,
6909 // child_idx,
6910 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6911 //
6912 //
6913 if (base_class == base_spec)
6914 return child_idx;
6915 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006916 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006917 }
6918
6919 return UINT32_MAX;
6920}
6921
6922static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
6923 clang::NamedDecl *canonical_decl,
6924 bool omit_empty_base_classes) {
6925 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
6926 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6927 omit_empty_base_classes);
6928
6929 clang::RecordDecl::field_iterator field, field_end;
6930 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6931 field != field_end; ++field, ++child_idx) {
6932 if (field->getCanonicalDecl() == canonical_decl)
6933 return child_idx;
6934 }
6935
6936 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00006937}
6938
6939// Look for a child member (doesn't include base classes, but it does include
6940// their members) in the type hierarchy. Returns an index path into "clang_type"
6941// on how to reach the appropriate member.
6942//
6943// class A
6944// {
6945// public:
6946// int m_a;
6947// int m_b;
6948// };
6949//
6950// class B
6951// {
6952// };
6953//
6954// class C :
6955// public B,
6956// public A
6957// {
6958// };
6959//
6960// If we have a clang type that describes "class C", and we wanted to looked
6961// "m_b" in it:
6962//
Kate Stoneb9c1b512016-09-06 20:57:50 +00006963// With omit_empty_base_classes == false we would get an integer array back
6964// with:
Greg Claytond8d4a572015-08-11 21:38:15 +00006965// { 1, 1 }
6966// The first index 1 is the child index for "class A" within class C
6967// The second index 1 is the child index for "m_b" within class A
6968//
6969// With omit_empty_base_classes == true we would get an integer array back with:
6970// { 0, 1 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006971// The first index 0 is the child index for "class A" within class C (since
6972// class B doesn't have any members it doesn't count)
Greg Claytond8d4a572015-08-11 21:38:15 +00006973// The second index 1 is the child index for "m_b" within class A
6974
Kate Stoneb9c1b512016-09-06 20:57:50 +00006975size_t ClangASTContext::GetIndexOfChildMemberWithName(
6976 lldb::opaque_compiler_type_t type, const char *name,
6977 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
6978 if (type && name && name[0]) {
6979 clang::QualType qual_type(GetCanonicalQualType(type));
6980 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6981 switch (type_class) {
6982 case clang::Type::Record:
6983 if (GetCompleteType(type)) {
6984 const clang::RecordType *record_type =
6985 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6986 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00006987
Kate Stoneb9c1b512016-09-06 20:57:50 +00006988 assert(record_decl);
6989 uint32_t child_idx = 0;
6990
6991 const clang::CXXRecordDecl *cxx_record_decl =
6992 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6993
6994 // Try and find a field that matches NAME
6995 clang::RecordDecl::field_iterator field, field_end;
6996 llvm::StringRef name_sref(name);
6997 for (field = record_decl->field_begin(),
6998 field_end = record_decl->field_end();
6999 field != field_end; ++field, ++child_idx) {
7000 llvm::StringRef field_name = field->getName();
7001 if (field_name.empty()) {
7002 CompilerType field_type(getASTContext(), field->getType());
7003 child_indexes.push_back(child_idx);
7004 if (field_type.GetIndexOfChildMemberWithName(
7005 name, omit_empty_base_classes, child_indexes))
7006 return child_indexes.size();
7007 child_indexes.pop_back();
7008
7009 } else if (field_name.equals(name_sref)) {
7010 // We have to add on the number of base classes to this index!
7011 child_indexes.push_back(
7012 child_idx + ClangASTContext::GetNumBaseClasses(
7013 cxx_record_decl, omit_empty_base_classes));
7014 return child_indexes.size();
7015 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007016 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007017
Kate Stoneb9c1b512016-09-06 20:57:50 +00007018 if (cxx_record_decl) {
7019 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7020
7021 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7022
7023 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7024 // Didn't find things easily, lets let clang do its thang...
7025 clang::IdentifierInfo &ident_ref =
7026 getASTContext()->Idents.get(name_sref);
7027 clang::DeclarationName decl_name(&ident_ref);
7028
7029 clang::CXXBasePaths paths;
7030 if (cxx_record_decl->lookupInBases(
7031 [decl_name](const clang::CXXBaseSpecifier *specifier,
7032 clang::CXXBasePath &path) {
7033 return clang::CXXRecordDecl::FindOrdinaryMember(
7034 specifier, path, decl_name);
7035 },
7036 paths)) {
7037 clang::CXXBasePaths::const_paths_iterator path,
7038 path_end = paths.end();
7039 for (path = paths.begin(); path != path_end; ++path) {
7040 const size_t num_path_elements = path->size();
7041 for (size_t e = 0; e < num_path_elements; ++e) {
7042 clang::CXXBasePathElement elem = (*path)[e];
7043
7044 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7045 omit_empty_base_classes);
7046 if (child_idx == UINT32_MAX) {
7047 child_indexes.clear();
7048 return 0;
7049 } else {
7050 child_indexes.push_back(child_idx);
7051 parent_record_decl = llvm::cast<clang::RecordDecl>(
7052 elem.Base->getType()
7053 ->getAs<clang::RecordType>()
7054 ->getDecl());
7055 }
7056 }
7057 for (clang::NamedDecl *path_decl : path->Decls) {
7058 child_idx = GetIndexForRecordChild(
7059 parent_record_decl, path_decl, omit_empty_base_classes);
7060 if (child_idx == UINT32_MAX) {
7061 child_indexes.clear();
7062 return 0;
7063 } else {
7064 child_indexes.push_back(child_idx);
7065 }
7066 }
7067 }
7068 return child_indexes.size();
7069 }
7070 }
7071 }
7072 break;
7073
7074 case clang::Type::ObjCObject:
7075 case clang::Type::ObjCInterface:
7076 if (GetCompleteType(type)) {
7077 llvm::StringRef name_sref(name);
7078 const clang::ObjCObjectType *objc_class_type =
7079 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7080 assert(objc_class_type);
7081 if (objc_class_type) {
7082 uint32_t child_idx = 0;
7083 clang::ObjCInterfaceDecl *class_interface_decl =
7084 objc_class_type->getInterface();
7085
7086 if (class_interface_decl) {
7087 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7088 ivar_end = class_interface_decl->ivar_end();
7089 clang::ObjCInterfaceDecl *superclass_interface_decl =
7090 class_interface_decl->getSuperClass();
7091
7092 for (ivar_pos = class_interface_decl->ivar_begin();
7093 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7094 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7095
7096 if (ivar_decl->getName().equals(name_sref)) {
7097 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7098 (omit_empty_base_classes &&
7099 ObjCDeclHasIVars(superclass_interface_decl, true)))
7100 ++child_idx;
7101
7102 child_indexes.push_back(child_idx);
7103 return child_indexes.size();
7104 }
7105 }
7106
7107 if (superclass_interface_decl) {
7108 // The super class index is always zero for ObjC classes,
7109 // so we push it onto the child indexes in case we find
7110 // an ivar in our superclass...
7111 child_indexes.push_back(0);
7112
7113 CompilerType superclass_clang_type(
7114 getASTContext(), getASTContext()->getObjCInterfaceType(
7115 superclass_interface_decl));
7116 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7117 name, omit_empty_base_classes, child_indexes)) {
7118 // We did find an ivar in a superclass so just
7119 // return the results!
7120 return child_indexes.size();
7121 }
7122
7123 // We didn't find an ivar matching "name" in our
7124 // superclass, pop the superclass zero index that
7125 // we pushed on above.
7126 child_indexes.pop_back();
7127 }
7128 }
7129 }
7130 }
7131 break;
7132
7133 case clang::Type::ObjCObjectPointer: {
7134 CompilerType objc_object_clang_type(
7135 getASTContext(),
7136 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7137 ->getPointeeType());
7138 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7139 name, omit_empty_base_classes, child_indexes);
7140 } break;
7141
7142 case clang::Type::ConstantArray: {
7143 // const clang::ConstantArrayType *array =
7144 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7145 // const uint64_t element_count =
7146 // array->getSize().getLimitedValue();
7147 //
7148 // if (idx < element_count)
7149 // {
7150 // std::pair<uint64_t, unsigned> field_type_info =
7151 // ast->getTypeInfo(array->getElementType());
7152 //
7153 // char element_name[32];
7154 // ::snprintf (element_name, sizeof (element_name),
7155 // "%s[%u]", parent_name ? parent_name : "", idx);
7156 //
7157 // child_name.assign(element_name);
7158 // assert(field_type_info.first % 8 == 0);
7159 // child_byte_size = field_type_info.first / 8;
7160 // child_byte_offset = idx * child_byte_size;
7161 // return array->getElementType().getAsOpaquePtr();
7162 // }
7163 } break;
7164
7165 // case clang::Type::MemberPointerType:
7166 // {
7167 // MemberPointerType *mem_ptr_type =
7168 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7169 // clang::QualType pointee_type =
7170 // mem_ptr_type->getPointeeType();
7171 //
7172 // if (ClangASTContext::IsAggregateType
7173 // (pointee_type.getAsOpaquePtr()))
7174 // {
7175 // return GetIndexOfChildWithName (ast,
7176 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7177 // name);
7178 // }
7179 // }
7180 // break;
7181 //
7182 case clang::Type::LValueReference:
7183 case clang::Type::RValueReference: {
7184 const clang::ReferenceType *reference_type =
7185 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7186 clang::QualType pointee_type(reference_type->getPointeeType());
7187 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7188
7189 if (pointee_clang_type.IsAggregateType()) {
7190 return pointee_clang_type.GetIndexOfChildMemberWithName(
7191 name, omit_empty_base_classes, child_indexes);
7192 }
7193 } break;
7194
7195 case clang::Type::Pointer: {
7196 CompilerType pointee_clang_type(GetPointeeType(type));
7197
7198 if (pointee_clang_type.IsAggregateType()) {
7199 return pointee_clang_type.GetIndexOfChildMemberWithName(
7200 name, omit_empty_base_classes, child_indexes);
7201 }
7202 } break;
7203
7204 case clang::Type::Typedef:
7205 return CompilerType(getASTContext(),
7206 llvm::cast<clang::TypedefType>(qual_type)
7207 ->getDecl()
7208 ->getUnderlyingType())
7209 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7210 child_indexes);
7211
7212 case clang::Type::Auto:
7213 return CompilerType(
7214 getASTContext(),
7215 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7216 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7217 child_indexes);
7218
7219 case clang::Type::Elaborated:
7220 return CompilerType(
7221 getASTContext(),
7222 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7223 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7224 child_indexes);
7225
7226 case clang::Type::Paren:
7227 return CompilerType(getASTContext(),
7228 llvm::cast<clang::ParenType>(qual_type)->desugar())
7229 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7230 child_indexes);
7231
7232 default:
7233 break;
7234 }
7235 }
7236 return 0;
7237}
Greg Claytond8d4a572015-08-11 21:38:15 +00007238
7239// Get the index of the child of "clang_type" whose name matches. This function
7240// doesn't descend into the children, but only looks one level deep and name
7241// matches can include base class names.
7242
7243uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007244ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7245 const char *name,
7246 bool omit_empty_base_classes) {
7247 if (type && name && name[0]) {
7248 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007249
Kate Stoneb9c1b512016-09-06 20:57:50 +00007250 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7251
7252 switch (type_class) {
7253 case clang::Type::Record:
7254 if (GetCompleteType(type)) {
7255 const clang::RecordType *record_type =
7256 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7257 const clang::RecordDecl *record_decl = record_type->getDecl();
7258
7259 assert(record_decl);
7260 uint32_t child_idx = 0;
7261
7262 const clang::CXXRecordDecl *cxx_record_decl =
7263 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7264
7265 if (cxx_record_decl) {
7266 clang::CXXRecordDecl::base_class_const_iterator base_class,
7267 base_class_end;
7268 for (base_class = cxx_record_decl->bases_begin(),
7269 base_class_end = cxx_record_decl->bases_end();
7270 base_class != base_class_end; ++base_class) {
7271 // Skip empty base classes
7272 clang::CXXRecordDecl *base_class_decl =
7273 llvm::cast<clang::CXXRecordDecl>(
7274 base_class->getType()
7275 ->getAs<clang::RecordType>()
7276 ->getDecl());
7277 if (omit_empty_base_classes &&
7278 ClangASTContext::RecordHasFields(base_class_decl) == false)
7279 continue;
7280
7281 CompilerType base_class_clang_type(getASTContext(),
7282 base_class->getType());
7283 std::string base_class_type_name(
7284 base_class_clang_type.GetTypeName().AsCString(""));
7285 if (base_class_type_name.compare(name) == 0)
7286 return child_idx;
7287 ++child_idx;
7288 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007289 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007290
Kate Stoneb9c1b512016-09-06 20:57:50 +00007291 // Try and find a field that matches NAME
7292 clang::RecordDecl::field_iterator field, field_end;
7293 llvm::StringRef name_sref(name);
7294 for (field = record_decl->field_begin(),
7295 field_end = record_decl->field_end();
7296 field != field_end; ++field, ++child_idx) {
7297 if (field->getName().equals(name_sref))
7298 return child_idx;
7299 }
7300 }
7301 break;
7302
7303 case clang::Type::ObjCObject:
7304 case clang::Type::ObjCInterface:
7305 if (GetCompleteType(type)) {
7306 llvm::StringRef name_sref(name);
7307 const clang::ObjCObjectType *objc_class_type =
7308 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7309 assert(objc_class_type);
7310 if (objc_class_type) {
7311 uint32_t child_idx = 0;
7312 clang::ObjCInterfaceDecl *class_interface_decl =
7313 objc_class_type->getInterface();
7314
7315 if (class_interface_decl) {
7316 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7317 ivar_end = class_interface_decl->ivar_end();
7318 clang::ObjCInterfaceDecl *superclass_interface_decl =
7319 class_interface_decl->getSuperClass();
7320
7321 for (ivar_pos = class_interface_decl->ivar_begin();
7322 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7323 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7324
7325 if (ivar_decl->getName().equals(name_sref)) {
7326 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7327 (omit_empty_base_classes &&
7328 ObjCDeclHasIVars(superclass_interface_decl, true)))
7329 ++child_idx;
7330
7331 return child_idx;
7332 }
7333 }
7334
7335 if (superclass_interface_decl) {
7336 if (superclass_interface_decl->getName().equals(name_sref))
7337 return 0;
7338 }
7339 }
7340 }
7341 }
7342 break;
7343
7344 case clang::Type::ObjCObjectPointer: {
7345 CompilerType pointee_clang_type(
7346 getASTContext(),
7347 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7348 ->getPointeeType());
7349 return pointee_clang_type.GetIndexOfChildWithName(
7350 name, omit_empty_base_classes);
7351 } break;
7352
7353 case clang::Type::ConstantArray: {
7354 // const clang::ConstantArrayType *array =
7355 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7356 // const uint64_t element_count =
7357 // array->getSize().getLimitedValue();
7358 //
7359 // if (idx < element_count)
7360 // {
7361 // std::pair<uint64_t, unsigned> field_type_info =
7362 // ast->getTypeInfo(array->getElementType());
7363 //
7364 // char element_name[32];
7365 // ::snprintf (element_name, sizeof (element_name),
7366 // "%s[%u]", parent_name ? parent_name : "", idx);
7367 //
7368 // child_name.assign(element_name);
7369 // assert(field_type_info.first % 8 == 0);
7370 // child_byte_size = field_type_info.first / 8;
7371 // child_byte_offset = idx * child_byte_size;
7372 // return array->getElementType().getAsOpaquePtr();
7373 // }
7374 } break;
7375
7376 // case clang::Type::MemberPointerType:
7377 // {
7378 // MemberPointerType *mem_ptr_type =
7379 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7380 // clang::QualType pointee_type =
7381 // mem_ptr_type->getPointeeType();
7382 //
7383 // if (ClangASTContext::IsAggregateType
7384 // (pointee_type.getAsOpaquePtr()))
7385 // {
7386 // return GetIndexOfChildWithName (ast,
7387 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7388 // name);
7389 // }
7390 // }
7391 // break;
7392 //
7393 case clang::Type::LValueReference:
7394 case clang::Type::RValueReference: {
7395 const clang::ReferenceType *reference_type =
7396 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7397 CompilerType pointee_type(getASTContext(),
7398 reference_type->getPointeeType());
7399
7400 if (pointee_type.IsAggregateType()) {
7401 return pointee_type.GetIndexOfChildWithName(name,
7402 omit_empty_base_classes);
7403 }
7404 } break;
7405
7406 case clang::Type::Pointer: {
7407 const clang::PointerType *pointer_type =
7408 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7409 CompilerType pointee_type(getASTContext(),
7410 pointer_type->getPointeeType());
7411
7412 if (pointee_type.IsAggregateType()) {
7413 return pointee_type.GetIndexOfChildWithName(name,
7414 omit_empty_base_classes);
7415 } else {
7416 // if (parent_name)
7417 // {
7418 // child_name.assign(1, '*');
7419 // child_name += parent_name;
7420 // }
7421 //
7422 // // We have a pointer to an simple type
7423 // if (idx == 0)
7424 // {
7425 // std::pair<uint64_t, unsigned> clang_type_info
7426 // = ast->getTypeInfo(pointee_type);
7427 // assert(clang_type_info.first % 8 == 0);
7428 // child_byte_size = clang_type_info.first / 8;
7429 // child_byte_offset = 0;
7430 // return pointee_type.getAsOpaquePtr();
7431 // }
7432 }
7433 } break;
7434
7435 case clang::Type::Auto:
7436 return CompilerType(
7437 getASTContext(),
7438 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7439 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7440
7441 case clang::Type::Elaborated:
7442 return CompilerType(
7443 getASTContext(),
7444 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7445 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7446
7447 case clang::Type::Paren:
7448 return CompilerType(getASTContext(),
7449 llvm::cast<clang::ParenType>(qual_type)->desugar())
7450 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7451
7452 case clang::Type::Typedef:
7453 return CompilerType(getASTContext(),
7454 llvm::cast<clang::TypedefType>(qual_type)
7455 ->getDecl()
7456 ->getUnderlyingType())
7457 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7458
7459 default:
7460 break;
7461 }
7462 }
7463 return UINT32_MAX;
7464}
Greg Claytond8d4a572015-08-11 21:38:15 +00007465
7466size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007467ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7468 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007469 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007470
Kate Stoneb9c1b512016-09-06 20:57:50 +00007471 clang::QualType qual_type(GetCanonicalQualType(type));
7472 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7473 switch (type_class) {
7474 case clang::Type::Record:
7475 if (GetCompleteType(type)) {
7476 const clang::CXXRecordDecl *cxx_record_decl =
7477 qual_type->getAsCXXRecordDecl();
7478 if (cxx_record_decl) {
7479 const clang::ClassTemplateSpecializationDecl *template_decl =
7480 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7481 cxx_record_decl);
7482 if (template_decl)
7483 return template_decl->getTemplateArgs().size();
7484 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007485 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007486 break;
7487
7488 case clang::Type::Typedef:
7489 return (CompilerType(getASTContext(),
7490 llvm::cast<clang::TypedefType>(qual_type)
7491 ->getDecl()
7492 ->getUnderlyingType()))
7493 .GetNumTemplateArguments();
7494
7495 case clang::Type::Auto:
7496 return (CompilerType(
7497 getASTContext(),
7498 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7499 .GetNumTemplateArguments();
7500
7501 case clang::Type::Elaborated:
7502 return (CompilerType(
7503 getASTContext(),
7504 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7505 .GetNumTemplateArguments();
7506
7507 case clang::Type::Paren:
7508 return (CompilerType(getASTContext(),
7509 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7510 .GetNumTemplateArguments();
7511
7512 default:
7513 break;
7514 }
7515
7516 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007517}
7518
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007519CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00007520ClangASTContext::GetTemplateArgument(lldb::opaque_compiler_type_t type,
7521 size_t arg_idx,
7522 lldb::TemplateArgumentKind &kind) {
7523 if (!type)
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007524 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00007525
7526 clang::QualType qual_type(GetCanonicalQualType(type));
7527 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7528 switch (type_class) {
7529 case clang::Type::Record:
7530 if (GetCompleteType(type)) {
7531 const clang::CXXRecordDecl *cxx_record_decl =
7532 qual_type->getAsCXXRecordDecl();
7533 if (cxx_record_decl) {
7534 const clang::ClassTemplateSpecializationDecl *template_decl =
7535 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7536 cxx_record_decl);
7537 if (template_decl &&
7538 arg_idx < template_decl->getTemplateArgs().size()) {
7539 const clang::TemplateArgument &template_arg =
7540 template_decl->getTemplateArgs()[arg_idx];
7541 switch (template_arg.getKind()) {
7542 case clang::TemplateArgument::Null:
7543 kind = eTemplateArgumentKindNull;
7544 return CompilerType();
7545
7546 case clang::TemplateArgument::Type:
7547 kind = eTemplateArgumentKindType;
7548 return CompilerType(getASTContext(), template_arg.getAsType());
7549
7550 case clang::TemplateArgument::Declaration:
7551 kind = eTemplateArgumentKindDeclaration;
7552 return CompilerType();
7553
7554 case clang::TemplateArgument::Integral:
7555 kind = eTemplateArgumentKindIntegral;
7556 return CompilerType(getASTContext(),
7557 template_arg.getIntegralType());
7558
7559 case clang::TemplateArgument::Template:
7560 kind = eTemplateArgumentKindTemplate;
7561 return CompilerType();
7562
7563 case clang::TemplateArgument::TemplateExpansion:
7564 kind = eTemplateArgumentKindTemplateExpansion;
7565 return CompilerType();
7566
7567 case clang::TemplateArgument::Expression:
7568 kind = eTemplateArgumentKindExpression;
7569 return CompilerType();
7570
7571 case clang::TemplateArgument::Pack:
7572 kind = eTemplateArgumentKindPack;
7573 return CompilerType();
7574
7575 default:
David Blaikiea322f362017-01-06 00:38:06 +00007576 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
Kate Stoneb9c1b512016-09-06 20:57:50 +00007577 }
7578 }
7579 }
7580 }
7581 break;
7582
7583 case clang::Type::Typedef:
7584 return (CompilerType(getASTContext(),
7585 llvm::cast<clang::TypedefType>(qual_type)
7586 ->getDecl()
7587 ->getUnderlyingType()))
7588 .GetTemplateArgument(arg_idx, kind);
7589
7590 case clang::Type::Auto:
7591 return (CompilerType(
7592 getASTContext(),
7593 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7594 .GetTemplateArgument(arg_idx, kind);
7595
7596 case clang::Type::Elaborated:
7597 return (CompilerType(
7598 getASTContext(),
7599 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7600 .GetTemplateArgument(arg_idx, kind);
7601
7602 case clang::Type::Paren:
7603 return (CompilerType(getASTContext(),
7604 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7605 .GetTemplateArgument(arg_idx, kind);
7606
7607 default:
7608 break;
7609 }
7610 kind = eTemplateArgumentKindNull;
7611 return CompilerType();
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007612}
7613
Kate Stoneb9c1b512016-09-06 20:57:50 +00007614CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7615 if (type)
7616 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7617 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007618}
7619
Kate Stoneb9c1b512016-09-06 20:57:50 +00007620clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7621 const clang::EnumType *enutype =
7622 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7623 if (enutype)
7624 return enutype->getDecl();
7625 return NULL;
7626}
7627
7628clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7629 const clang::RecordType *record_type =
7630 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7631 if (record_type)
7632 return record_type->getDecl();
7633 return nullptr;
7634}
7635
7636clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
7637 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
7638 if (qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00007639 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007640 else
7641 return qual_type->getAsTagDecl();
Greg Claytone6b36cd2015-12-08 01:02:08 +00007642}
7643
Greg Claytond8d4a572015-08-11 21:38:15 +00007644clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007645ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7646 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007647}
7648
7649clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007650ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7651 const clang::ObjCObjectType *objc_class_type =
7652 llvm::dyn_cast<clang::ObjCObjectType>(
7653 ClangUtil::GetCanonicalQualType(type));
7654 if (objc_class_type)
7655 return objc_class_type->getInterface();
7656 return nullptr;
7657}
7658
7659clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
7660 const CompilerType &type, const char *name,
7661 const CompilerType &field_clang_type, AccessType access,
7662 uint32_t bitfield_bit_size) {
7663 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007664 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007665 ClangASTContext *ast =
7666 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7667 if (!ast)
7668 return nullptr;
7669 clang::ASTContext *clang_ast = ast->getASTContext();
7670
7671 clang::FieldDecl *field = nullptr;
7672
7673 clang::Expr *bit_width = nullptr;
7674 if (bitfield_bit_size != 0) {
7675 llvm::APInt bitfield_bit_size_apint(
7676 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7677 bit_width = new (*clang_ast)
7678 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7679 clang_ast->IntTy, clang::SourceLocation());
7680 }
7681
7682 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7683 if (record_decl) {
7684 field = clang::FieldDecl::Create(
7685 *clang_ast, record_decl, clang::SourceLocation(),
7686 clang::SourceLocation(),
7687 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7688 ClangUtil::GetQualType(field_clang_type), // Field type
7689 nullptr, // TInfo *
7690 bit_width, // BitWidth
7691 false, // Mutable
7692 clang::ICIS_NoInit); // HasInit
7693
7694 if (!name) {
7695 // Determine whether this field corresponds to an anonymous
7696 // struct or union.
7697 if (const clang::TagType *TagT =
7698 field->getType()->getAs<clang::TagType>()) {
7699 if (clang::RecordDecl *Rec =
7700 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7701 if (!Rec->getDeclName()) {
7702 Rec->setAnonymousStructOrUnion(true);
7703 field->setImplicit();
7704 }
7705 }
7706 }
7707
7708 if (field) {
7709 field->setAccess(
7710 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7711
7712 record_decl->addDecl(field);
7713
7714#ifdef LLDB_CONFIGURATION_DEBUG
7715 VerifyDecl(field);
7716#endif
7717 }
7718 } else {
7719 clang::ObjCInterfaceDecl *class_interface_decl =
7720 ast->GetAsObjCInterfaceDecl(type);
7721
7722 if (class_interface_decl) {
7723 const bool is_synthesized = false;
7724
7725 field_clang_type.GetCompleteType();
7726
7727 field = clang::ObjCIvarDecl::Create(
7728 *clang_ast, class_interface_decl, clang::SourceLocation(),
7729 clang::SourceLocation(),
7730 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7731 ClangUtil::GetQualType(field_clang_type), // Field type
7732 nullptr, // TypeSourceInfo *
7733 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7734 is_synthesized);
7735
7736 if (field) {
7737 class_interface_decl->addDecl(field);
7738
7739#ifdef LLDB_CONFIGURATION_DEBUG
7740 VerifyDecl(field);
7741#endif
7742 }
7743 }
7744 }
7745 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007746}
7747
Kate Stoneb9c1b512016-09-06 20:57:50 +00007748void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7749 if (!type)
7750 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007751
Kate Stoneb9c1b512016-09-06 20:57:50 +00007752 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7753 if (!ast)
7754 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007755
Kate Stoneb9c1b512016-09-06 20:57:50 +00007756 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007757
Kate Stoneb9c1b512016-09-06 20:57:50 +00007758 if (!record_decl)
7759 return;
7760
7761 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7762
7763 IndirectFieldVector indirect_fields;
7764 clang::RecordDecl::field_iterator field_pos;
7765 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7766 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7767 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7768 last_field_pos = field_pos++) {
7769 if (field_pos->isAnonymousStructOrUnion()) {
7770 clang::QualType field_qual_type = field_pos->getType();
7771
7772 const clang::RecordType *field_record_type =
7773 field_qual_type->getAs<clang::RecordType>();
7774
7775 if (!field_record_type)
7776 continue;
7777
7778 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7779
7780 if (!field_record_decl)
7781 continue;
7782
7783 for (clang::RecordDecl::decl_iterator
7784 di = field_record_decl->decls_begin(),
7785 de = field_record_decl->decls_end();
7786 di != de; ++di) {
7787 if (clang::FieldDecl *nested_field_decl =
7788 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7789 clang::NamedDecl **chain =
7790 new (*ast->getASTContext()) clang::NamedDecl *[2];
7791 chain[0] = *field_pos;
7792 chain[1] = nested_field_decl;
7793 clang::IndirectFieldDecl *indirect_field =
7794 clang::IndirectFieldDecl::Create(
7795 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7796 nested_field_decl->getIdentifier(),
7797 nested_field_decl->getType(), {chain, 2});
7798
7799 indirect_field->setImplicit();
7800
7801 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7802 field_pos->getAccess(), nested_field_decl->getAccess()));
7803
7804 indirect_fields.push_back(indirect_field);
7805 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7806 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7807 size_t nested_chain_size =
7808 nested_indirect_field_decl->getChainingSize();
7809 clang::NamedDecl **chain = new (*ast->getASTContext())
7810 clang::NamedDecl *[nested_chain_size + 1];
7811 chain[0] = *field_pos;
7812
7813 int chain_index = 1;
7814 for (clang::IndirectFieldDecl::chain_iterator
7815 nci = nested_indirect_field_decl->chain_begin(),
7816 nce = nested_indirect_field_decl->chain_end();
7817 nci < nce; ++nci) {
7818 chain[chain_index] = *nci;
7819 chain_index++;
7820 }
7821
7822 clang::IndirectFieldDecl *indirect_field =
7823 clang::IndirectFieldDecl::Create(
7824 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7825 nested_indirect_field_decl->getIdentifier(),
7826 nested_indirect_field_decl->getType(),
7827 {chain, nested_chain_size + 1});
7828
7829 indirect_field->setImplicit();
7830
7831 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7832 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
7833
7834 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00007835 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007836 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007837 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007838 }
7839
7840 // Check the last field to see if it has an incomplete array type as its
7841 // last member and if it does, the tell the record decl about it
7842 if (last_field_pos != field_end_pos) {
7843 if (last_field_pos->getType()->isIncompleteArrayType())
7844 record_decl->hasFlexibleArrayMember();
7845 }
7846
7847 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
7848 ife = indirect_fields.end();
7849 ifi < ife; ++ifi) {
7850 record_decl->addDecl(*ifi);
7851 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007852}
7853
Kate Stoneb9c1b512016-09-06 20:57:50 +00007854void ClangASTContext::SetIsPacked(const CompilerType &type) {
7855 if (type) {
7856 ClangASTContext *ast =
7857 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7858 if (ast) {
7859 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
7860
7861 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00007862 return;
7863
Kate Stoneb9c1b512016-09-06 20:57:50 +00007864 record_decl->addAttr(
7865 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00007866 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007867 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007868}
7869
Kate Stoneb9c1b512016-09-06 20:57:50 +00007870clang::VarDecl *ClangASTContext::AddVariableToRecordType(
7871 const CompilerType &type, const char *name, const CompilerType &var_type,
7872 AccessType access) {
7873 clang::VarDecl *var_decl = nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007874
Kate Stoneb9c1b512016-09-06 20:57:50 +00007875 if (!type.IsValid() || !var_type.IsValid())
7876 return nullptr;
7877 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7878 if (!ast)
7879 return nullptr;
7880
7881 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7882 if (record_decl) {
7883 var_decl = clang::VarDecl::Create(
7884 *ast->getASTContext(), // ASTContext &
7885 record_decl, // DeclContext *
7886 clang::SourceLocation(), // clang::SourceLocation StartLoc
7887 clang::SourceLocation(), // clang::SourceLocation IdLoc
7888 name ? &ast->getASTContext()->Idents.get(name)
7889 : nullptr, // clang::IdentifierInfo *
7890 ClangUtil::GetQualType(var_type), // Variable clang::QualType
7891 nullptr, // TypeSourceInfo *
7892 clang::SC_Static); // StorageClass
7893 if (var_decl) {
7894 var_decl->setAccess(
7895 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7896 record_decl->addDecl(var_decl);
7897
Greg Claytond8d4a572015-08-11 21:38:15 +00007898#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00007899 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00007900#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00007901 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007902 }
7903 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00007904}
7905
Kate Stoneb9c1b512016-09-06 20:57:50 +00007906clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
7907 lldb::opaque_compiler_type_t type, const char *name,
7908 const CompilerType &method_clang_type, lldb::AccessType access,
7909 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
7910 bool is_attr_used, bool is_artificial) {
7911 if (!type || !method_clang_type.IsValid() || name == nullptr ||
7912 name[0] == '\0')
7913 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007914
Kate Stoneb9c1b512016-09-06 20:57:50 +00007915 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007916
Kate Stoneb9c1b512016-09-06 20:57:50 +00007917 clang::CXXRecordDecl *cxx_record_decl =
7918 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007919
Kate Stoneb9c1b512016-09-06 20:57:50 +00007920 if (cxx_record_decl == nullptr)
7921 return nullptr;
7922
7923 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
7924
7925 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7926
7927 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
7928
7929 const clang::FunctionType *function_type =
7930 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7931
7932 if (function_type == nullptr)
7933 return nullptr;
7934
7935 const clang::FunctionProtoType *method_function_prototype(
7936 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7937
7938 if (!method_function_prototype)
7939 return nullptr;
7940
7941 unsigned int num_params = method_function_prototype->getNumParams();
7942
7943 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7944 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7945
7946 if (is_artificial)
7947 return nullptr; // skip everything artificial
7948
7949 if (name[0] == '~') {
7950 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
7951 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7952 clang::DeclarationNameInfo(
7953 getASTContext()->DeclarationNames.getCXXDestructorName(
7954 getASTContext()->getCanonicalType(record_qual_type)),
7955 clang::SourceLocation()),
7956 method_qual_type, nullptr, is_inline, is_artificial);
7957 cxx_method_decl = cxx_dtor_decl;
7958 } else if (decl_name == cxx_record_decl->getDeclName()) {
7959 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
7960 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7961 clang::DeclarationNameInfo(
7962 getASTContext()->DeclarationNames.getCXXConstructorName(
7963 getASTContext()->getCanonicalType(record_qual_type)),
7964 clang::SourceLocation()),
7965 method_qual_type,
7966 nullptr, // TypeSourceInfo *
7967 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
7968 cxx_method_decl = cxx_ctor_decl;
7969 } else {
7970 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7971 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7972
7973 if (IsOperator(name, op_kind)) {
7974 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
7975 // Check the number of operator parameters. Sometimes we have
7976 // seen bad DWARF that doesn't correctly describe operators and
7977 // if we try to create a method and add it to the class, clang
7978 // will assert and crash, so we need to make sure things are
7979 // acceptable.
7980 const bool is_method = true;
7981 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
7982 is_method, op_kind, num_params))
7983 return nullptr;
7984 cxx_method_decl = clang::CXXMethodDecl::Create(
7985 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7986 clang::DeclarationNameInfo(
7987 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
7988 clang::SourceLocation()),
7989 method_qual_type,
7990 nullptr, // TypeSourceInfo *
7991 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
7992 } else if (num_params == 0) {
7993 // Conversion operators don't take params...
7994 cxx_method_decl = clang::CXXConversionDecl::Create(
7995 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7996 clang::DeclarationNameInfo(
7997 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
7998 getASTContext()->getCanonicalType(
7999 function_type->getReturnType())),
8000 clang::SourceLocation()),
8001 method_qual_type,
8002 nullptr, // TypeSourceInfo *
8003 is_inline, is_explicit, false /*is_constexpr*/,
8004 clang::SourceLocation());
8005 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008006 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008007
8008 if (cxx_method_decl == nullptr) {
8009 cxx_method_decl = clang::CXXMethodDecl::Create(
8010 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8011 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8012 method_qual_type,
8013 nullptr, // TypeSourceInfo *
8014 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008015 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008016 }
8017
8018 clang::AccessSpecifier access_specifier =
8019 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8020
8021 cxx_method_decl->setAccess(access_specifier);
8022 cxx_method_decl->setVirtualAsWritten(is_virtual);
8023
8024 if (is_attr_used)
8025 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8026
8027 // Populate the method decl with parameter decls
8028
8029 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8030
8031 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8032 params.push_back(clang::ParmVarDecl::Create(
8033 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8034 clang::SourceLocation(),
8035 nullptr, // anonymous
8036 method_function_prototype->getParamType(param_index), nullptr,
8037 clang::SC_None, nullptr));
8038 }
8039
8040 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8041
8042 cxx_record_decl->addDecl(cxx_method_decl);
8043
8044 // Sometimes the debug info will mention a constructor (default/copy/move),
8045 // destructor, or assignment operator (copy/move) but there won't be any
8046 // version of this in the code. So we check if the function was artificially
8047 // generated and if it is trivial and this lets the compiler/backend know
8048 // that it can inline the IR for these when it needs to and we can avoid a
8049 // "missing function" error when running expressions.
8050
8051 if (is_artificial) {
8052 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8053 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8054 (cxx_ctor_decl->isCopyConstructor() &&
8055 cxx_record_decl->hasTrivialCopyConstructor()) ||
8056 (cxx_ctor_decl->isMoveConstructor() &&
8057 cxx_record_decl->hasTrivialMoveConstructor()))) {
8058 cxx_ctor_decl->setDefaulted();
8059 cxx_ctor_decl->setTrivial(true);
8060 } else if (cxx_dtor_decl) {
8061 if (cxx_record_decl->hasTrivialDestructor()) {
8062 cxx_dtor_decl->setDefaulted();
8063 cxx_dtor_decl->setTrivial(true);
8064 }
8065 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8066 cxx_record_decl->hasTrivialCopyAssignment()) ||
8067 (cxx_method_decl->isMoveAssignmentOperator() &&
8068 cxx_record_decl->hasTrivialMoveAssignment())) {
8069 cxx_method_decl->setDefaulted();
8070 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008071 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008072 }
8073
Greg Claytond8d4a572015-08-11 21:38:15 +00008074#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008075 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008076#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008077
Kate Stoneb9c1b512016-09-06 20:57:50 +00008078 // printf ("decl->isPolymorphic() = %i\n",
8079 // cxx_record_decl->isPolymorphic());
8080 // printf ("decl->isAggregate() = %i\n",
8081 // cxx_record_decl->isAggregate());
8082 // printf ("decl->isPOD() = %i\n",
8083 // cxx_record_decl->isPOD());
8084 // printf ("decl->isEmpty() = %i\n",
8085 // cxx_record_decl->isEmpty());
8086 // printf ("decl->isAbstract() = %i\n",
8087 // cxx_record_decl->isAbstract());
8088 // printf ("decl->hasTrivialConstructor() = %i\n",
8089 // cxx_record_decl->hasTrivialConstructor());
8090 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8091 // cxx_record_decl->hasTrivialCopyConstructor());
8092 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8093 // cxx_record_decl->hasTrivialCopyAssignment());
8094 // printf ("decl->hasTrivialDestructor() = %i\n",
8095 // cxx_record_decl->hasTrivialDestructor());
8096 return cxx_method_decl;
8097}
Greg Claytond8d4a572015-08-11 21:38:15 +00008098
8099#pragma mark C++ Base Classes
8100
8101clang::CXXBaseSpecifier *
Kate Stoneb9c1b512016-09-06 20:57:50 +00008102ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8103 AccessType access, bool is_virtual,
8104 bool base_of_class) {
8105 if (type)
8106 return new clang::CXXBaseSpecifier(
8107 clang::SourceRange(), is_virtual, base_of_class,
8108 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8109 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8110 clang::SourceLocation());
8111 return nullptr;
8112}
8113
8114void ClangASTContext::DeleteBaseClassSpecifiers(
8115 clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes) {
8116 for (unsigned i = 0; i < num_base_classes; ++i) {
8117 delete base_classes[i];
8118 base_classes[i] = nullptr;
8119 }
8120}
8121
8122bool ClangASTContext::SetBaseClassesForClassType(
8123 lldb::opaque_compiler_type_t type,
8124 clang::CXXBaseSpecifier const *const *base_classes,
8125 unsigned num_base_classes) {
8126 if (type) {
8127 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8128 if (cxx_record_decl) {
8129 cxx_record_decl->setBases(base_classes, num_base_classes);
8130 return true;
8131 }
8132 }
8133 return false;
8134}
8135
8136bool ClangASTContext::SetObjCSuperClass(
8137 const CompilerType &type, const CompilerType &superclass_clang_type) {
8138 ClangASTContext *ast =
8139 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8140 if (!ast)
8141 return false;
8142 clang::ASTContext *clang_ast = ast->getASTContext();
8143
8144 if (type && superclass_clang_type.IsValid() &&
8145 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8146 clang::ObjCInterfaceDecl *class_interface_decl =
8147 GetAsObjCInterfaceDecl(type);
8148 clang::ObjCInterfaceDecl *super_interface_decl =
8149 GetAsObjCInterfaceDecl(superclass_clang_type);
8150 if (class_interface_decl && super_interface_decl) {
8151 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8152 clang_ast->getObjCInterfaceType(super_interface_decl)));
8153 return true;
8154 }
8155 }
8156 return false;
8157}
8158
8159bool ClangASTContext::AddObjCClassProperty(
8160 const CompilerType &type, const char *property_name,
8161 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8162 const char *property_setter_name, const char *property_getter_name,
8163 uint32_t property_attributes, ClangASTMetadata *metadata) {
8164 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8165 property_name[0] == '\0')
8166 return false;
8167 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8168 if (!ast)
8169 return false;
8170 clang::ASTContext *clang_ast = ast->getASTContext();
8171
8172 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8173
8174 if (class_interface_decl) {
8175 CompilerType property_clang_type_to_access;
8176
8177 if (property_clang_type.IsValid())
8178 property_clang_type_to_access = property_clang_type;
8179 else if (ivar_decl)
8180 property_clang_type_to_access =
8181 CompilerType(clang_ast, ivar_decl->getType());
8182
8183 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8184 clang::TypeSourceInfo *prop_type_source;
8185 if (ivar_decl)
8186 prop_type_source =
8187 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8188 else
8189 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8190 ClangUtil::GetQualType(property_clang_type));
8191
8192 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8193 *clang_ast, class_interface_decl,
8194 clang::SourceLocation(), // Source Location
8195 &clang_ast->Idents.get(property_name),
8196 clang::SourceLocation(), // Source Location for AT
8197 clang::SourceLocation(), // Source location for (
8198 ivar_decl ? ivar_decl->getType()
8199 : ClangUtil::GetQualType(property_clang_type),
8200 prop_type_source);
8201
8202 if (property_decl) {
8203 if (metadata)
8204 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8205
8206 class_interface_decl->addDecl(property_decl);
8207
8208 clang::Selector setter_sel, getter_sel;
8209
8210 if (property_setter_name != nullptr) {
8211 std::string property_setter_no_colon(
8212 property_setter_name, strlen(property_setter_name) - 1);
8213 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008214 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008215 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8216 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8217 std::string setter_sel_string("set");
8218 setter_sel_string.push_back(::toupper(property_name[0]));
8219 setter_sel_string.append(&property_name[1]);
8220 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008221 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008222 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8223 }
8224 property_decl->setSetterName(setter_sel);
8225 property_decl->setPropertyAttributes(
8226 clang::ObjCPropertyDecl::OBJC_PR_setter);
8227
8228 if (property_getter_name != nullptr) {
8229 clang::IdentifierInfo *getter_ident =
8230 &clang_ast->Idents.get(property_getter_name);
8231 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8232 } else {
8233 clang::IdentifierInfo *getter_ident =
8234 &clang_ast->Idents.get(property_name);
8235 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8236 }
8237 property_decl->setGetterName(getter_sel);
8238 property_decl->setPropertyAttributes(
8239 clang::ObjCPropertyDecl::OBJC_PR_getter);
8240
8241 if (ivar_decl)
8242 property_decl->setPropertyIvarDecl(ivar_decl);
8243
8244 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8245 property_decl->setPropertyAttributes(
8246 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8247 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8248 property_decl->setPropertyAttributes(
8249 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8250 if (property_attributes & DW_APPLE_PROPERTY_assign)
8251 property_decl->setPropertyAttributes(
8252 clang::ObjCPropertyDecl::OBJC_PR_assign);
8253 if (property_attributes & DW_APPLE_PROPERTY_retain)
8254 property_decl->setPropertyAttributes(
8255 clang::ObjCPropertyDecl::OBJC_PR_retain);
8256 if (property_attributes & DW_APPLE_PROPERTY_copy)
8257 property_decl->setPropertyAttributes(
8258 clang::ObjCPropertyDecl::OBJC_PR_copy);
8259 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8260 property_decl->setPropertyAttributes(
8261 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8262 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8263 property_decl->setPropertyAttributes(
8264 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8265 if (property_attributes &
8266 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8267 property_decl->setPropertyAttributes(
8268 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8269 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8270 property_decl->setPropertyAttributes(
8271 clang::ObjCPropertyDecl::OBJC_PR_class);
8272
8273 const bool isInstance =
8274 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8275
8276 if (!getter_sel.isNull() &&
8277 !(isInstance
8278 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8279 : class_interface_decl->lookupClassMethod(getter_sel))) {
8280 const bool isVariadic = false;
8281 const bool isSynthesized = false;
8282 const bool isImplicitlyDeclared = true;
8283 const bool isDefined = false;
8284 const clang::ObjCMethodDecl::ImplementationControl impControl =
8285 clang::ObjCMethodDecl::None;
8286 const bool HasRelatedResultType = false;
8287
8288 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8289 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8290 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8291 nullptr, class_interface_decl, isInstance, isVariadic,
8292 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8293 HasRelatedResultType);
8294
8295 if (getter && metadata)
8296 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8297
8298 if (getter) {
8299 getter->setMethodParams(*clang_ast,
8300 llvm::ArrayRef<clang::ParmVarDecl *>(),
8301 llvm::ArrayRef<clang::SourceLocation>());
8302
8303 class_interface_decl->addDecl(getter);
8304 }
8305 }
8306
8307 if (!setter_sel.isNull() &&
8308 !(isInstance
8309 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8310 : class_interface_decl->lookupClassMethod(setter_sel))) {
8311 clang::QualType result_type = clang_ast->VoidTy;
8312 const bool isVariadic = false;
8313 const bool isSynthesized = false;
8314 const bool isImplicitlyDeclared = true;
8315 const bool isDefined = false;
8316 const clang::ObjCMethodDecl::ImplementationControl impControl =
8317 clang::ObjCMethodDecl::None;
8318 const bool HasRelatedResultType = false;
8319
8320 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8321 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8322 setter_sel, result_type, nullptr, class_interface_decl,
8323 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8324 isDefined, impControl, HasRelatedResultType);
8325
8326 if (setter && metadata)
8327 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8328
8329 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8330
8331 params.push_back(clang::ParmVarDecl::Create(
8332 *clang_ast, setter, clang::SourceLocation(),
8333 clang::SourceLocation(),
8334 nullptr, // anonymous
8335 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8336 clang::SC_Auto, nullptr));
8337
8338 if (setter) {
8339 setter->setMethodParams(
8340 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8341 llvm::ArrayRef<clang::SourceLocation>());
8342
8343 class_interface_decl->addDecl(setter);
8344 }
8345 }
8346
8347 return true;
8348 }
8349 }
8350 }
8351 return false;
8352}
8353
8354bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8355 bool check_superclass) {
8356 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8357 if (class_interface_decl)
8358 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8359 return false;
8360}
8361
8362clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8363 const CompilerType &type,
8364 const char *name, // the full symbol name as seen in the symbol table
8365 // (lldb::opaque_compiler_type_t type, "-[NString
8366 // stringWithCString:]")
8367 const CompilerType &method_clang_type, lldb::AccessType access,
8368 bool is_artificial, bool is_variadic) {
8369 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008370 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008371
Kate Stoneb9c1b512016-09-06 20:57:50 +00008372 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8373
8374 if (class_interface_decl == nullptr)
8375 return nullptr;
8376 ClangASTContext *lldb_ast =
8377 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8378 if (lldb_ast == nullptr)
8379 return nullptr;
8380 clang::ASTContext *ast = lldb_ast->getASTContext();
8381
8382 const char *selector_start = ::strchr(name, ' ');
8383 if (selector_start == nullptr)
8384 return nullptr;
8385
8386 selector_start++;
8387 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8388
8389 size_t len = 0;
8390 const char *start;
8391 // printf ("name = '%s'\n", name);
8392
8393 unsigned num_selectors_with_args = 0;
8394 for (start = selector_start; start && *start != '\0' && *start != ']';
8395 start += len) {
8396 len = ::strcspn(start, ":]");
8397 bool has_arg = (start[len] == ':');
8398 if (has_arg)
8399 ++num_selectors_with_args;
8400 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8401 if (has_arg)
8402 len += 1;
8403 }
8404
8405 if (selector_idents.size() == 0)
8406 return nullptr;
8407
8408 clang::Selector method_selector = ast->Selectors.getSelector(
8409 num_selectors_with_args ? selector_idents.size() : 0,
8410 selector_idents.data());
8411
8412 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8413
8414 // Populate the method decl with parameter decls
8415 const clang::Type *method_type(method_qual_type.getTypePtr());
8416
8417 if (method_type == nullptr)
8418 return nullptr;
8419
8420 const clang::FunctionProtoType *method_function_prototype(
8421 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8422
8423 if (!method_function_prototype)
8424 return nullptr;
8425
8426 bool is_synthesized = false;
8427 bool is_defined = false;
8428 clang::ObjCMethodDecl::ImplementationControl imp_control =
8429 clang::ObjCMethodDecl::None;
8430
8431 const unsigned num_args = method_function_prototype->getNumParams();
8432
8433 if (num_args != num_selectors_with_args)
8434 return nullptr; // some debug information is corrupt. We are not going to
8435 // deal with it.
8436
8437 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8438 *ast,
8439 clang::SourceLocation(), // beginLoc,
8440 clang::SourceLocation(), // endLoc,
8441 method_selector, method_function_prototype->getReturnType(),
8442 nullptr, // TypeSourceInfo *ResultTInfo,
8443 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8444 ClangUtil::GetQualType(type)),
8445 name[0] == '-', is_variadic, is_synthesized,
8446 true, // is_implicitly_declared; we force this to true because we don't
8447 // have source locations
8448 is_defined, imp_control, false /*has_related_result_type*/);
8449
8450 if (objc_method_decl == nullptr)
8451 return nullptr;
8452
8453 if (num_args > 0) {
8454 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8455
8456 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8457 params.push_back(clang::ParmVarDecl::Create(
8458 *ast, objc_method_decl, clang::SourceLocation(),
8459 clang::SourceLocation(),
8460 nullptr, // anonymous
8461 method_function_prototype->getParamType(param_index), nullptr,
8462 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008463 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008464
Kate Stoneb9c1b512016-09-06 20:57:50 +00008465 objc_method_decl->setMethodParams(
8466 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8467 llvm::ArrayRef<clang::SourceLocation>());
8468 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008469
Kate Stoneb9c1b512016-09-06 20:57:50 +00008470 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008471
Greg Claytond8d4a572015-08-11 21:38:15 +00008472#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008473 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008474#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008475
8476 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008477}
8478
Kate Stoneb9c1b512016-09-06 20:57:50 +00008479bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8480 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008481 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008482
Kate Stoneb9c1b512016-09-06 20:57:50 +00008483 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008484
Kate Stoneb9c1b512016-09-06 20:57:50 +00008485 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8486 switch (type_class) {
8487 case clang::Type::Record: {
8488 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8489 if (cxx_record_decl)
8490 return cxx_record_decl->hasExternalLexicalStorage() ||
8491 cxx_record_decl->hasExternalVisibleStorage();
8492 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008493
Kate Stoneb9c1b512016-09-06 20:57:50 +00008494 case clang::Type::Enum: {
8495 clang::EnumDecl *enum_decl =
8496 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8497 if (enum_decl)
8498 return enum_decl->hasExternalLexicalStorage() ||
8499 enum_decl->hasExternalVisibleStorage();
8500 } break;
8501
8502 case clang::Type::ObjCObject:
8503 case clang::Type::ObjCInterface: {
8504 const clang::ObjCObjectType *objc_class_type =
8505 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8506 assert(objc_class_type);
8507 if (objc_class_type) {
8508 clang::ObjCInterfaceDecl *class_interface_decl =
8509 objc_class_type->getInterface();
8510
8511 if (class_interface_decl)
8512 return class_interface_decl->hasExternalLexicalStorage() ||
8513 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008514 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008515 } break;
8516
8517 case clang::Type::Typedef:
8518 return GetHasExternalStorage(CompilerType(
8519 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8520 ->getDecl()
8521 ->getUnderlyingType()
8522 .getAsOpaquePtr()));
8523
8524 case clang::Type::Auto:
8525 return GetHasExternalStorage(CompilerType(
8526 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8527 ->getDeducedType()
8528 .getAsOpaquePtr()));
8529
8530 case clang::Type::Elaborated:
8531 return GetHasExternalStorage(CompilerType(
8532 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8533 ->getNamedType()
8534 .getAsOpaquePtr()));
8535
8536 case clang::Type::Paren:
8537 return GetHasExternalStorage(CompilerType(
8538 type.GetTypeSystem(),
8539 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8540
8541 default:
8542 break;
8543 }
8544 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008545}
8546
Kate Stoneb9c1b512016-09-06 20:57:50 +00008547bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8548 bool has_extern) {
8549 if (!type)
8550 return false;
8551
8552 clang::QualType qual_type(GetCanonicalQualType(type));
8553
8554 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8555 switch (type_class) {
8556 case clang::Type::Record: {
8557 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8558 if (cxx_record_decl) {
8559 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8560 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8561 return true;
8562 }
8563 } break;
8564
8565 case clang::Type::Enum: {
8566 clang::EnumDecl *enum_decl =
8567 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8568 if (enum_decl) {
8569 enum_decl->setHasExternalLexicalStorage(has_extern);
8570 enum_decl->setHasExternalVisibleStorage(has_extern);
8571 return true;
8572 }
8573 } break;
8574
8575 case clang::Type::ObjCObject:
8576 case clang::Type::ObjCInterface: {
8577 const clang::ObjCObjectType *objc_class_type =
8578 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8579 assert(objc_class_type);
8580 if (objc_class_type) {
8581 clang::ObjCInterfaceDecl *class_interface_decl =
8582 objc_class_type->getInterface();
8583
8584 if (class_interface_decl) {
8585 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8586 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8587 return true;
8588 }
8589 }
8590 } break;
8591
8592 case clang::Type::Typedef:
8593 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8594 ->getDecl()
8595 ->getUnderlyingType()
8596 .getAsOpaquePtr(),
8597 has_extern);
8598
8599 case clang::Type::Auto:
8600 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8601 ->getDeducedType()
8602 .getAsOpaquePtr(),
8603 has_extern);
8604
8605 case clang::Type::Elaborated:
8606 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8607 ->getNamedType()
8608 .getAsOpaquePtr(),
8609 has_extern);
8610
8611 case clang::Type::Paren:
8612 return SetHasExternalStorage(
8613 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8614 has_extern);
8615
8616 default:
8617 break;
8618 }
8619 return false;
8620}
Greg Claytond8d4a572015-08-11 21:38:15 +00008621
8622#pragma mark TagDecl
8623
Kate Stoneb9c1b512016-09-06 20:57:50 +00008624bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8625 clang::QualType qual_type(ClangUtil::GetQualType(type));
8626 if (!qual_type.isNull()) {
8627 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8628 if (tag_type) {
8629 clang::TagDecl *tag_decl = tag_type->getDecl();
8630 if (tag_decl) {
8631 tag_decl->startDefinition();
8632 return true;
8633 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008634 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008635
8636 const clang::ObjCObjectType *object_type =
8637 qual_type->getAs<clang::ObjCObjectType>();
8638 if (object_type) {
8639 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8640 if (interface_decl) {
8641 interface_decl->startDefinition();
8642 return true;
8643 }
8644 }
8645 }
8646 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008647}
8648
Kate Stoneb9c1b512016-09-06 20:57:50 +00008649bool ClangASTContext::CompleteTagDeclarationDefinition(
8650 const CompilerType &type) {
8651 clang::QualType qual_type(ClangUtil::GetQualType(type));
8652 if (!qual_type.isNull()) {
8653 // Make sure we use the same methodology as
8654 // ClangASTContext::StartTagDeclarationDefinition()
8655 // as to how we start/end the definition. Previously we were calling
8656 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8657 if (tag_type) {
8658 clang::TagDecl *tag_decl = tag_type->getDecl();
8659 if (tag_decl) {
8660 clang::CXXRecordDecl *cxx_record_decl =
8661 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8662
8663 if (cxx_record_decl) {
8664 if (!cxx_record_decl->isCompleteDefinition())
8665 cxx_record_decl->completeDefinition();
8666 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8667 cxx_record_decl->setHasExternalLexicalStorage(false);
8668 cxx_record_decl->setHasExternalVisibleStorage(false);
8669 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008670 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008671 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008672 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008673
8674 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8675
8676 if (enutype) {
8677 clang::EnumDecl *enum_decl = enutype->getDecl();
8678
8679 if (enum_decl) {
8680 if (!enum_decl->isCompleteDefinition()) {
8681 ClangASTContext *lldb_ast =
8682 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8683 if (lldb_ast == nullptr)
8684 return false;
8685 clang::ASTContext *ast = lldb_ast->getASTContext();
8686
8687 /// TODO This really needs to be fixed.
8688
8689 QualType integer_type(enum_decl->getIntegerType());
8690 if (!integer_type.isNull()) {
8691 unsigned NumPositiveBits = 1;
8692 unsigned NumNegativeBits = 0;
8693
8694 clang::QualType promotion_qual_type;
8695 // If the enum integer type is less than an integer in bit width,
8696 // then we must promote it to an integer size.
8697 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8698 ast->getTypeSize(ast->IntTy)) {
8699 if (enum_decl->getIntegerType()->isSignedIntegerType())
8700 promotion_qual_type = ast->IntTy;
8701 else
8702 promotion_qual_type = ast->UnsignedIntTy;
8703 } else
8704 promotion_qual_type = enum_decl->getIntegerType();
8705
8706 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8707 promotion_qual_type, NumPositiveBits,
8708 NumNegativeBits);
8709 }
8710 }
8711 return true;
8712 }
8713 }
8714 }
8715 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008716}
8717
Kate Stoneb9c1b512016-09-06 20:57:50 +00008718bool ClangASTContext::AddEnumerationValueToEnumerationType(
8719 lldb::opaque_compiler_type_t type,
8720 const CompilerType &enumerator_clang_type, const Declaration &decl,
8721 const char *name, int64_t enum_value, uint32_t enum_value_bit_size) {
8722 if (type && enumerator_clang_type.IsValid() && name && name[0]) {
8723 clang::QualType enum_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008724
Kate Stoneb9c1b512016-09-06 20:57:50 +00008725 bool is_signed = false;
8726 enumerator_clang_type.IsIntegerType(is_signed);
Greg Claytond8d4a572015-08-11 21:38:15 +00008727 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Kate Stoneb9c1b512016-09-06 20:57:50 +00008728 if (clang_type) {
8729 const clang::EnumType *enutype =
8730 llvm::dyn_cast<clang::EnumType>(clang_type);
8731
8732 if (enutype) {
8733 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8734 enum_llvm_apsint = enum_value;
8735 clang::EnumConstantDecl *enumerator_decl =
8736 clang::EnumConstantDecl::Create(
8737 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8738 name ? &getASTContext()->Idents.get(name)
8739 : nullptr, // Identifier
8740 ClangUtil::GetQualType(enumerator_clang_type),
8741 nullptr, enum_llvm_apsint);
8742
8743 if (enumerator_decl) {
8744 enutype->getDecl()->addDecl(enumerator_decl);
8745
8746#ifdef LLDB_CONFIGURATION_DEBUG
8747 VerifyDecl(enumerator_decl);
8748#endif
8749
8750 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008751 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008752 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008753 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008754 }
8755 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008756}
8757
Greg Claytona1e5dc82015-08-11 22:53:00 +00008758CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00008759ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8760 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8761 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8762 if (clang_type) {
8763 const clang::EnumType *enutype =
8764 llvm::dyn_cast<clang::EnumType>(clang_type);
8765 if (enutype) {
8766 clang::EnumDecl *enum_decl = enutype->getDecl();
8767 if (enum_decl)
8768 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008769 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008770 }
8771 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008772}
8773
Kate Stoneb9c1b512016-09-06 20:57:50 +00008774CompilerType
8775ClangASTContext::CreateMemberPointerType(const CompilerType &type,
8776 const CompilerType &pointee_type) {
8777 if (type && pointee_type.IsValid() &&
8778 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8779 ClangASTContext *ast =
8780 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8781 if (!ast)
8782 return CompilerType();
8783 return CompilerType(ast->getASTContext(),
8784 ast->getASTContext()->getMemberPointerType(
8785 ClangUtil::GetQualType(pointee_type),
8786 ClangUtil::GetQualType(type).getTypePtr()));
8787 }
8788 return CompilerType();
8789}
Greg Claytond8d4a572015-08-11 21:38:15 +00008790
8791size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00008792ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
8793 const char *s, uint8_t *dst,
8794 size_t dst_size) {
8795 if (type) {
8796 clang::QualType qual_type(GetCanonicalQualType(type));
8797 uint32_t count = 0;
8798 bool is_complex = false;
8799 if (IsFloatingPointType(type, count, is_complex)) {
8800 // TODO: handle complex and vector types
8801 if (count != 1)
8802 return false;
8803
8804 llvm::StringRef s_sref(s);
8805 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
8806 s_sref);
8807
8808 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
8809 const uint64_t byte_size = bit_size / 8;
8810 if (dst_size >= byte_size) {
8811 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
8812 llvm::NextPowerOf2(byte_size) * 8);
8813 lldb_private::Error get_data_error;
8814 if (scalar.GetAsMemoryData(dst, byte_size,
8815 lldb_private::endian::InlHostByteOrder(),
8816 get_data_error))
8817 return byte_size;
8818 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008819 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008820 }
8821 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00008822}
8823
Greg Claytond8d4a572015-08-11 21:38:15 +00008824//----------------------------------------------------------------------
8825// Dumping types
8826//----------------------------------------------------------------------
8827#define DEPTH_INCREMENT 2
8828
Kate Stoneb9c1b512016-09-06 20:57:50 +00008829void ClangASTContext::DumpValue(
8830 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00008831 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008832 lldb::offset_t data_byte_offset, size_t data_byte_size,
8833 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
8834 bool show_summary, bool verbose, uint32_t depth) {
8835 if (!type)
8836 return;
8837
8838 clang::QualType qual_type(GetQualType(type));
8839 switch (qual_type->getTypeClass()) {
8840 case clang::Type::Record:
8841 if (GetCompleteType(type)) {
8842 const clang::RecordType *record_type =
8843 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8844 const clang::RecordDecl *record_decl = record_type->getDecl();
8845 assert(record_decl);
8846 uint32_t field_bit_offset = 0;
8847 uint32_t field_byte_offset = 0;
8848 const clang::ASTRecordLayout &record_layout =
8849 getASTContext()->getASTRecordLayout(record_decl);
8850 uint32_t child_idx = 0;
8851
8852 const clang::CXXRecordDecl *cxx_record_decl =
8853 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8854 if (cxx_record_decl) {
8855 // We might have base classes to print out first
8856 clang::CXXRecordDecl::base_class_const_iterator base_class,
8857 base_class_end;
8858 for (base_class = cxx_record_decl->bases_begin(),
8859 base_class_end = cxx_record_decl->bases_end();
8860 base_class != base_class_end; ++base_class) {
8861 const clang::CXXRecordDecl *base_class_decl =
8862 llvm::cast<clang::CXXRecordDecl>(
8863 base_class->getType()->getAs<clang::RecordType>()->getDecl());
8864
8865 // Skip empty base classes
8866 if (verbose == false &&
8867 ClangASTContext::RecordHasFields(base_class_decl) == false)
8868 continue;
8869
8870 if (base_class->isVirtual())
8871 field_bit_offset =
8872 record_layout.getVBaseClassOffset(base_class_decl)
8873 .getQuantity() *
8874 8;
8875 else
8876 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
8877 .getQuantity() *
8878 8;
8879 field_byte_offset = field_bit_offset / 8;
8880 assert(field_bit_offset % 8 == 0);
8881 if (child_idx == 0)
8882 s->PutChar('{');
8883 else
8884 s->PutChar(',');
8885
8886 clang::QualType base_class_qual_type = base_class->getType();
8887 std::string base_class_type_name(base_class_qual_type.getAsString());
8888
8889 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00008890 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
8891 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008892
8893 clang::TypeInfo base_class_type_info =
8894 getASTContext()->getTypeInfo(base_class_qual_type);
8895
8896 // Dump the value of the member
8897 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
8898 base_clang_type.DumpValue(
8899 exe_ctx,
8900 s, // Stream to dump to
8901 base_clang_type
8902 .GetFormat(), // The format with which to display the member
8903 data, // Data buffer containing all bytes for this type
8904 data_byte_offset + field_byte_offset, // Offset into "data" where
8905 // to grab value from
8906 base_class_type_info.Width / 8, // Size of this type in bytes
8907 0, // Bitfield bit size
8908 0, // Bitfield bit offset
8909 show_types, // Boolean indicating if we should show the variable
8910 // types
8911 show_summary, // Boolean indicating if we should show a summary
8912 // for the current type
8913 verbose, // Verbose output?
8914 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8915 // children
8916
8917 ++child_idx;
8918 }
8919 }
8920 uint32_t field_idx = 0;
8921 clang::RecordDecl::field_iterator field, field_end;
8922 for (field = record_decl->field_begin(),
8923 field_end = record_decl->field_end();
8924 field != field_end; ++field, ++field_idx, ++child_idx) {
8925 // Print the starting squiggly bracket (if this is the
8926 // first member) or comma (for member 2 and beyond) for
8927 // the struct/union/class member.
8928 if (child_idx == 0)
8929 s->PutChar('{');
8930 else
8931 s->PutChar(',');
8932
8933 // Indent
8934 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8935
8936 clang::QualType field_type = field->getType();
8937 // Print the member type if requested
8938 // Figure out the type byte size (field_type_info.first) and
8939 // alignment (field_type_info.second) from the AST context.
8940 clang::TypeInfo field_type_info =
8941 getASTContext()->getTypeInfo(field_type);
8942 assert(field_idx < record_layout.getFieldCount());
8943 // Figure out the field offset within the current struct/union/class
8944 // type
8945 field_bit_offset = record_layout.getFieldOffset(field_idx);
8946 field_byte_offset = field_bit_offset / 8;
8947 uint32_t field_bitfield_bit_size = 0;
8948 uint32_t field_bitfield_bit_offset = 0;
8949 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
8950 field_bitfield_bit_size))
8951 field_bitfield_bit_offset = field_bit_offset % 8;
8952
8953 if (show_types) {
8954 std::string field_type_name(field_type.getAsString());
8955 if (field_bitfield_bit_size > 0)
8956 s->Printf("(%s:%u) ", field_type_name.c_str(),
8957 field_bitfield_bit_size);
8958 else
8959 s->Printf("(%s) ", field_type_name.c_str());
8960 }
8961 // Print the member name and equal sign
8962 s->Printf("%s = ", field->getNameAsString().c_str());
8963
8964 // Dump the value of the member
8965 CompilerType field_clang_type(getASTContext(), field_type);
8966 field_clang_type.DumpValue(
8967 exe_ctx,
8968 s, // Stream to dump to
8969 field_clang_type
8970 .GetFormat(), // The format with which to display the member
8971 data, // Data buffer containing all bytes for this type
8972 data_byte_offset + field_byte_offset, // Offset into "data" where to
8973 // grab value from
8974 field_type_info.Width / 8, // Size of this type in bytes
8975 field_bitfield_bit_size, // Bitfield bit size
8976 field_bitfield_bit_offset, // Bitfield bit offset
8977 show_types, // Boolean indicating if we should show the variable
8978 // types
8979 show_summary, // Boolean indicating if we should show a summary for
8980 // the current type
8981 verbose, // Verbose output?
8982 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8983 // children
8984 }
8985
8986 // Indent the trailing squiggly bracket
8987 if (child_idx > 0)
8988 s->Printf("\n%*s}", depth, "");
8989 }
8990 return;
8991
8992 case clang::Type::Enum:
8993 if (GetCompleteType(type)) {
8994 const clang::EnumType *enutype =
8995 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8996 const clang::EnumDecl *enum_decl = enutype->getDecl();
8997 assert(enum_decl);
8998 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8999 lldb::offset_t offset = data_byte_offset;
9000 const int64_t enum_value = data.GetMaxU64Bitfield(
9001 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9002 for (enum_pos = enum_decl->enumerator_begin(),
9003 enum_end_pos = enum_decl->enumerator_end();
9004 enum_pos != enum_end_pos; ++enum_pos) {
9005 if (enum_pos->getInitVal() == enum_value) {
9006 s->Printf("%s", enum_pos->getNameAsString().c_str());
9007 return;
9008 }
9009 }
9010 // If we have gotten here we didn't get find the enumerator in the
9011 // enum decl, so just print the integer.
9012 s->Printf("%" PRIi64, enum_value);
9013 }
9014 return;
9015
9016 case clang::Type::ConstantArray: {
9017 const clang::ConstantArrayType *array =
9018 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9019 bool is_array_of_characters = false;
9020 clang::QualType element_qual_type = array->getElementType();
9021
9022 const clang::Type *canonical_type =
9023 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9024 if (canonical_type)
9025 is_array_of_characters = canonical_type->isCharType();
9026
9027 const uint64_t element_count = array->getSize().getLimitedValue();
9028
9029 clang::TypeInfo field_type_info =
9030 getASTContext()->getTypeInfo(element_qual_type);
9031
9032 uint32_t element_idx = 0;
9033 uint32_t element_offset = 0;
9034 uint64_t element_byte_size = field_type_info.Width / 8;
9035 uint32_t element_stride = element_byte_size;
9036
9037 if (is_array_of_characters) {
9038 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009039 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9040 element_byte_size, element_count, UINT32_MAX,
9041 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009042 s->PutChar('"');
9043 return;
9044 } else {
9045 CompilerType element_clang_type(getASTContext(), element_qual_type);
9046 lldb::Format element_format = element_clang_type.GetFormat();
9047
9048 for (element_idx = 0; element_idx < element_count; ++element_idx) {
9049 // Print the starting squiggly bracket (if this is the
9050 // first member) or comman (for member 2 and beyong) for
9051 // the struct/union/class member.
9052 if (element_idx == 0)
9053 s->PutChar('{');
9054 else
9055 s->PutChar(',');
9056
9057 // Indent and print the index
9058 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9059
9060 // Figure out the field offset within the current struct/union/class
9061 // type
9062 element_offset = element_idx * element_stride;
9063
9064 // Dump the value of the member
9065 element_clang_type.DumpValue(
9066 exe_ctx,
9067 s, // Stream to dump to
9068 element_format, // The format with which to display the element
9069 data, // Data buffer containing all bytes for this type
9070 data_byte_offset +
9071 element_offset, // Offset into "data" where to grab value from
9072 element_byte_size, // Size of this type in bytes
9073 0, // Bitfield bit size
9074 0, // Bitfield bit offset
9075 show_types, // Boolean indicating if we should show the variable
9076 // types
9077 show_summary, // Boolean indicating if we should show a summary for
9078 // the current type
9079 verbose, // Verbose output?
9080 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9081 // children
9082 }
9083
9084 // Indent the trailing squiggly bracket
9085 if (element_idx > 0)
9086 s->Printf("\n%*s}", depth, "");
9087 }
9088 }
9089 return;
9090
9091 case clang::Type::Typedef: {
9092 clang::QualType typedef_qual_type =
9093 llvm::cast<clang::TypedefType>(qual_type)
9094 ->getDecl()
9095 ->getUnderlyingType();
9096
9097 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9098 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9099 clang::TypeInfo typedef_type_info =
9100 getASTContext()->getTypeInfo(typedef_qual_type);
9101 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9102
9103 return typedef_clang_type.DumpValue(
9104 exe_ctx,
9105 s, // Stream to dump to
9106 typedef_format, // The format with which to display the element
9107 data, // Data buffer containing all bytes for this type
9108 data_byte_offset, // Offset into "data" where to grab value from
9109 typedef_byte_size, // Size of this type in bytes
9110 bitfield_bit_size, // Bitfield bit size
9111 bitfield_bit_offset, // Bitfield bit offset
9112 show_types, // Boolean indicating if we should show the variable types
9113 show_summary, // Boolean indicating if we should show a summary for the
9114 // current type
9115 verbose, // Verbose output?
9116 depth); // Scope depth for any types that have children
9117 } break;
9118
9119 case clang::Type::Auto: {
9120 clang::QualType elaborated_qual_type =
9121 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9122 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9123 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9124 clang::TypeInfo elaborated_type_info =
9125 getASTContext()->getTypeInfo(elaborated_qual_type);
9126 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9127
9128 return elaborated_clang_type.DumpValue(
9129 exe_ctx,
9130 s, // Stream to dump to
9131 elaborated_format, // The format with which to display the element
9132 data, // Data buffer containing all bytes for this type
9133 data_byte_offset, // Offset into "data" where to grab value from
9134 elaborated_byte_size, // Size of this type in bytes
9135 bitfield_bit_size, // Bitfield bit size
9136 bitfield_bit_offset, // Bitfield bit offset
9137 show_types, // Boolean indicating if we should show the variable types
9138 show_summary, // Boolean indicating if we should show a summary for the
9139 // current type
9140 verbose, // Verbose output?
9141 depth); // Scope depth for any types that have children
9142 } break;
9143
9144 case clang::Type::Elaborated: {
9145 clang::QualType elaborated_qual_type =
9146 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9147 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9148 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9149 clang::TypeInfo elaborated_type_info =
9150 getASTContext()->getTypeInfo(elaborated_qual_type);
9151 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9152
9153 return elaborated_clang_type.DumpValue(
9154 exe_ctx,
9155 s, // Stream to dump to
9156 elaborated_format, // The format with which to display the element
9157 data, // Data buffer containing all bytes for this type
9158 data_byte_offset, // Offset into "data" where to grab value from
9159 elaborated_byte_size, // Size of this type in bytes
9160 bitfield_bit_size, // Bitfield bit size
9161 bitfield_bit_offset, // Bitfield bit offset
9162 show_types, // Boolean indicating if we should show the variable types
9163 show_summary, // Boolean indicating if we should show a summary for the
9164 // current type
9165 verbose, // Verbose output?
9166 depth); // Scope depth for any types that have children
9167 } break;
9168
9169 case clang::Type::Paren: {
9170 clang::QualType desugar_qual_type =
9171 llvm::cast<clang::ParenType>(qual_type)->desugar();
9172 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9173
9174 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9175 clang::TypeInfo desugar_type_info =
9176 getASTContext()->getTypeInfo(desugar_qual_type);
9177 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9178
9179 return desugar_clang_type.DumpValue(
9180 exe_ctx,
9181 s, // Stream to dump to
9182 desugar_format, // The format with which to display the element
9183 data, // Data buffer containing all bytes for this type
9184 data_byte_offset, // Offset into "data" where to grab value from
9185 desugar_byte_size, // Size of this type in bytes
9186 bitfield_bit_size, // Bitfield bit size
9187 bitfield_bit_offset, // Bitfield bit offset
9188 show_types, // Boolean indicating if we should show the variable types
9189 show_summary, // Boolean indicating if we should show a summary for the
9190 // current type
9191 verbose, // Verbose output?
9192 depth); // Scope depth for any types that have children
9193 } break;
9194
9195 default:
9196 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009197 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9198 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9199 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009200
9201 if (show_summary)
9202 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9203 break;
9204 }
9205}
9206
9207bool ClangASTContext::DumpTypeValue(
9208 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009209 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9210 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009211 ExecutionContextScope *exe_scope) {
9212 if (!type)
9213 return false;
9214 if (IsAggregateType(type)) {
9215 return false;
9216 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009217 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009218
9219 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9220 switch (type_class) {
9221 case clang::Type::Typedef: {
9222 clang::QualType typedef_qual_type =
9223 llvm::cast<clang::TypedefType>(qual_type)
9224 ->getDecl()
9225 ->getUnderlyingType();
9226 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9227 if (format == eFormatDefault)
9228 format = typedef_clang_type.GetFormat();
9229 clang::TypeInfo typedef_type_info =
9230 getASTContext()->getTypeInfo(typedef_qual_type);
9231 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9232
9233 return typedef_clang_type.DumpTypeValue(
9234 s,
9235 format, // The format with which to display the element
9236 data, // Data buffer containing all bytes for this type
9237 byte_offset, // Offset into "data" where to grab value from
9238 typedef_byte_size, // Size of this type in bytes
9239 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9240 // treat as a bitfield
9241 bitfield_bit_offset, // Offset in bits of a bitfield value if
9242 // bitfield_bit_size != 0
9243 exe_scope);
9244 } break;
9245
9246 case clang::Type::Enum:
9247 // If our format is enum or default, show the enumeration value as
9248 // its enumeration string value, else just display it as requested.
9249 if ((format == eFormatEnum || format == eFormatDefault) &&
9250 GetCompleteType(type)) {
9251 const clang::EnumType *enutype =
9252 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9253 const clang::EnumDecl *enum_decl = enutype->getDecl();
9254 assert(enum_decl);
9255 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9256 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9257 lldb::offset_t offset = byte_offset;
9258 if (is_signed) {
9259 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9260 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9261 for (enum_pos = enum_decl->enumerator_begin(),
9262 enum_end_pos = enum_decl->enumerator_end();
9263 enum_pos != enum_end_pos; ++enum_pos) {
9264 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009265 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009266 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009267 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009268 }
9269 // If we have gotten here we didn't get find the enumerator in the
9270 // enum decl, so just print the integer.
9271 s->Printf("%" PRIi64, enum_svalue);
9272 } else {
9273 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9274 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9275 for (enum_pos = enum_decl->enumerator_begin(),
9276 enum_end_pos = enum_decl->enumerator_end();
9277 enum_pos != enum_end_pos; ++enum_pos) {
9278 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009279 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009280 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009281 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009282 }
9283 // If we have gotten here we didn't get find the enumerator in the
9284 // enum decl, so just print the integer.
9285 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009286 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009287 return true;
9288 }
9289 // format was not enum, just fall through and dump the value as
9290 // requested....
9291 LLVM_FALLTHROUGH;
9292
9293 default:
9294 // We are down to a scalar type that we just need to display.
9295 {
9296 uint32_t item_count = 1;
9297 // A few formats, we might need to modify our size and count for
9298 // depending
9299 // on how we are trying to display the value...
9300 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009301 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009302 case eFormatBoolean:
9303 case eFormatBinary:
9304 case eFormatComplex:
9305 case eFormatCString: // NULL terminated C strings
9306 case eFormatDecimal:
9307 case eFormatEnum:
9308 case eFormatHex:
9309 case eFormatHexUppercase:
9310 case eFormatFloat:
9311 case eFormatOctal:
9312 case eFormatOSType:
9313 case eFormatUnsigned:
9314 case eFormatPointer:
9315 case eFormatVectorOfChar:
9316 case eFormatVectorOfSInt8:
9317 case eFormatVectorOfUInt8:
9318 case eFormatVectorOfSInt16:
9319 case eFormatVectorOfUInt16:
9320 case eFormatVectorOfSInt32:
9321 case eFormatVectorOfUInt32:
9322 case eFormatVectorOfSInt64:
9323 case eFormatVectorOfUInt64:
9324 case eFormatVectorOfFloat32:
9325 case eFormatVectorOfFloat64:
9326 case eFormatVectorOfUInt128:
9327 break;
9328
9329 case eFormatChar:
9330 case eFormatCharPrintable:
9331 case eFormatCharArray:
9332 case eFormatBytes:
9333 case eFormatBytesWithASCII:
9334 item_count = byte_size;
9335 byte_size = 1;
9336 break;
9337
9338 case eFormatUnicode16:
9339 item_count = byte_size / 2;
9340 byte_size = 2;
9341 break;
9342
9343 case eFormatUnicode32:
9344 item_count = byte_size / 4;
9345 byte_size = 4;
9346 break;
9347 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009348 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9349 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9350 bitfield_bit_size, bitfield_bit_offset,
9351 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009352 }
9353 break;
9354 }
9355 }
9356 return 0;
9357}
9358
9359void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9360 ExecutionContext *exe_ctx, Stream *s,
9361 const lldb_private::DataExtractor &data,
9362 lldb::offset_t data_byte_offset,
9363 size_t data_byte_size) {
9364 uint32_t length = 0;
9365 if (IsCStringType(type, length)) {
9366 if (exe_ctx) {
9367 Process *process = exe_ctx->GetProcessPtr();
9368 if (process) {
9369 lldb::offset_t offset = data_byte_offset;
9370 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9371 std::vector<uint8_t> buf;
9372 if (length > 0)
9373 buf.resize(length);
9374 else
9375 buf.resize(256);
9376
Zachary Turner29cb8682017-03-03 20:57:05 +00009377 DataExtractor cstr_data(&buf.front(), buf.size(),
9378 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009379 buf.back() = '\0';
9380 size_t bytes_read;
9381 size_t total_cstr_len = 0;
9382 Error error;
9383 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9384 buf.size(), error)) > 0) {
9385 const size_t len = strlen((const char *)&buf.front());
9386 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009387 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009388 if (total_cstr_len == 0)
9389 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009390 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9391 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009392 total_cstr_len += len;
9393 if (len < buf.size())
9394 break;
9395 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009396 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009397 if (total_cstr_len > 0)
9398 s->PutChar('"');
9399 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009400 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009401 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009402}
9403
Kate Stoneb9c1b512016-09-06 20:57:50 +00009404void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9405 StreamFile s(stdout, false);
9406 DumpTypeDescription(type, &s);
9407 ClangASTMetadata *metadata =
9408 ClangASTContext::GetMetadata(getASTContext(), type);
9409 if (metadata) {
9410 metadata->Dump(&s);
9411 }
9412}
Greg Claytond8d4a572015-08-11 21:38:15 +00009413
Kate Stoneb9c1b512016-09-06 20:57:50 +00009414void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9415 Stream *s) {
9416 if (type) {
9417 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009418
Kate Stoneb9c1b512016-09-06 20:57:50 +00009419 llvm::SmallVector<char, 1024> buf;
9420 llvm::raw_svector_ostream llvm_ostrm(buf);
9421
9422 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9423 switch (type_class) {
9424 case clang::Type::ObjCObject:
9425 case clang::Type::ObjCInterface: {
9426 GetCompleteType(type);
9427
9428 const clang::ObjCObjectType *objc_class_type =
9429 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9430 assert(objc_class_type);
9431 if (objc_class_type) {
9432 clang::ObjCInterfaceDecl *class_interface_decl =
9433 objc_class_type->getInterface();
9434 if (class_interface_decl) {
9435 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9436 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009437 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009438 }
9439 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009440
Kate Stoneb9c1b512016-09-06 20:57:50 +00009441 case clang::Type::Typedef: {
9442 const clang::TypedefType *typedef_type =
9443 qual_type->getAs<clang::TypedefType>();
9444 if (typedef_type) {
9445 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9446 std::string clang_typedef_name(
9447 typedef_decl->getQualifiedNameAsString());
9448 if (!clang_typedef_name.empty()) {
9449 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009450 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009451 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009452 }
9453 } break;
9454
9455 case clang::Type::Auto:
9456 CompilerType(getASTContext(),
9457 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9458 .DumpTypeDescription(s);
9459 return;
9460
9461 case clang::Type::Elaborated:
9462 CompilerType(getASTContext(),
9463 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9464 .DumpTypeDescription(s);
9465 return;
9466
9467 case clang::Type::Paren:
9468 CompilerType(getASTContext(),
9469 llvm::cast<clang::ParenType>(qual_type)->desugar())
9470 .DumpTypeDescription(s);
9471 return;
9472
9473 case clang::Type::Record: {
9474 GetCompleteType(type);
9475
9476 const clang::RecordType *record_type =
9477 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9478 const clang::RecordDecl *record_decl = record_type->getDecl();
9479 const clang::CXXRecordDecl *cxx_record_decl =
9480 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9481
9482 if (cxx_record_decl)
9483 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9484 s->GetIndentLevel());
9485 else
9486 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9487 s->GetIndentLevel());
9488 } break;
9489
9490 default: {
9491 const clang::TagType *tag_type =
9492 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9493 if (tag_type) {
9494 clang::TagDecl *tag_decl = tag_type->getDecl();
9495 if (tag_decl)
9496 tag_decl->print(llvm_ostrm, 0);
9497 } else {
9498 std::string clang_type_name(qual_type.getAsString());
9499 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009500 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009501 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009502 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009503 }
9504
Kate Stoneb9c1b512016-09-06 20:57:50 +00009505 if (buf.size() > 0) {
9506 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009507 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009508 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009509}
9510
Kate Stoneb9c1b512016-09-06 20:57:50 +00009511void ClangASTContext::DumpTypeName(const CompilerType &type) {
9512 if (ClangUtil::IsClangType(type)) {
9513 clang::QualType qual_type(
9514 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9515
9516 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9517 switch (type_class) {
9518 case clang::Type::Record: {
9519 const clang::CXXRecordDecl *cxx_record_decl =
9520 qual_type->getAsCXXRecordDecl();
9521 if (cxx_record_decl)
9522 printf("class %s", cxx_record_decl->getName().str().c_str());
9523 } break;
9524
9525 case clang::Type::Enum: {
9526 clang::EnumDecl *enum_decl =
9527 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9528 if (enum_decl) {
9529 printf("enum %s", enum_decl->getName().str().c_str());
9530 }
9531 } break;
9532
9533 case clang::Type::ObjCObject:
9534 case clang::Type::ObjCInterface: {
9535 const clang::ObjCObjectType *objc_class_type =
9536 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9537 if (objc_class_type) {
9538 clang::ObjCInterfaceDecl *class_interface_decl =
9539 objc_class_type->getInterface();
9540 // We currently can't complete objective C types through the newly added
9541 // ASTContext
9542 // because it only supports TagDecl objects right now...
9543 if (class_interface_decl)
9544 printf("@class %s", class_interface_decl->getName().str().c_str());
9545 }
9546 } break;
9547
9548 case clang::Type::Typedef:
9549 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9550 ->getDecl()
9551 ->getName()
9552 .str()
9553 .c_str());
9554 break;
9555
9556 case clang::Type::Auto:
9557 printf("auto ");
9558 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9559 llvm::cast<clang::AutoType>(qual_type)
9560 ->getDeducedType()
9561 .getAsOpaquePtr()));
9562
9563 case clang::Type::Elaborated:
9564 printf("elaborated ");
9565 return DumpTypeName(CompilerType(
9566 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9567 ->getNamedType()
9568 .getAsOpaquePtr()));
9569
9570 case clang::Type::Paren:
9571 printf("paren ");
9572 return DumpTypeName(CompilerType(
9573 type.GetTypeSystem(),
9574 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9575
9576 default:
9577 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9578 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009579 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009580 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009581}
9582
Kate Stoneb9c1b512016-09-06 20:57:50 +00009583clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9584 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9585 const char *parent_name, int tag_decl_kind,
9586 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9587 if (template_param_infos.IsValid()) {
9588 std::string template_basename(parent_name);
9589 template_basename.erase(template_basename.find('<'));
9590
9591 return CreateClassTemplateDecl(decl_ctx, access_type,
9592 template_basename.c_str(), tag_decl_kind,
9593 template_param_infos);
9594 }
9595 return NULL;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009596}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009597
Kate Stoneb9c1b512016-09-06 20:57:50 +00009598void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9599 ClangASTContext *ast = (ClangASTContext *)baton;
9600 SymbolFile *sym_file = ast->GetSymbolFile();
9601 if (sym_file) {
9602 CompilerType clang_type = GetTypeForDecl(decl);
9603 if (clang_type)
9604 sym_file->CompleteType(clang_type);
9605 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009606}
9607
Kate Stoneb9c1b512016-09-06 20:57:50 +00009608void ClangASTContext::CompleteObjCInterfaceDecl(
9609 void *baton, clang::ObjCInterfaceDecl *decl) {
9610 ClangASTContext *ast = (ClangASTContext *)baton;
9611 SymbolFile *sym_file = ast->GetSymbolFile();
9612 if (sym_file) {
9613 CompilerType clang_type = GetTypeForDecl(decl);
9614 if (clang_type)
9615 sym_file->CompleteType(clang_type);
9616 }
Zachary Turner42dff792016-04-15 00:21:26 +00009617}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009618
Kate Stoneb9c1b512016-09-06 20:57:50 +00009619DWARFASTParser *ClangASTContext::GetDWARFParser() {
9620 if (!m_dwarf_ast_parser_ap)
9621 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9622 return m_dwarf_ast_parser_ap.get();
9623}
9624
9625PDBASTParser *ClangASTContext::GetPDBParser() {
9626 if (!m_pdb_ast_parser_ap)
9627 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9628 return m_pdb_ast_parser_ap.get();
9629}
9630
9631bool ClangASTContext::LayoutRecordType(
9632 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9633 uint64_t &alignment,
9634 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9635 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9636 &base_offsets,
9637 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9638 &vbase_offsets) {
9639 ClangASTContext *ast = (ClangASTContext *)baton;
9640 DWARFASTParserClang *dwarf_ast_parser =
9641 (DWARFASTParserClang *)ast->GetDWARFParser();
9642 return dwarf_ast_parser->GetClangASTImporter().LayoutRecordType(
9643 record_decl, bit_size, alignment, field_offsets, base_offsets,
9644 vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009645}
9646
Greg Clayton99558cc42015-08-24 23:46:31 +00009647//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009648// CompilerDecl override functions
9649//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009650
Kate Stoneb9c1b512016-09-06 20:57:50 +00009651ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9652 if (opaque_decl) {
9653 clang::NamedDecl *nd =
9654 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9655 if (nd != nullptr)
9656 return ConstString(nd->getDeclName().getAsString());
9657 }
9658 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009659}
9660
Kate Stoneb9c1b512016-09-06 20:57:50 +00009661ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9662 if (opaque_decl) {
9663 clang::NamedDecl *nd =
9664 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9665 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9666 clang::MangleContext *mc = getMangleContext();
9667 if (mc && mc->shouldMangleCXXName(nd)) {
9668 llvm::SmallVector<char, 1024> buf;
9669 llvm::raw_svector_ostream llvm_ostrm(buf);
9670 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9671 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9672 Ctor_Complete, llvm_ostrm);
9673 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9674 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9675 Dtor_Complete, llvm_ostrm);
9676 } else {
9677 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009678 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009679 if (buf.size() > 0)
9680 return ConstString(buf.data(), buf.size());
9681 }
Greg Claytonfe689042015-11-10 17:47:04 +00009682 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009683 }
9684 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009685}
9686
Kate Stoneb9c1b512016-09-06 20:57:50 +00009687CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9688 if (opaque_decl)
9689 return CompilerDeclContext(this,
9690 ((clang::Decl *)opaque_decl)->getDeclContext());
9691 else
9692 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009693}
9694
Kate Stoneb9c1b512016-09-06 20:57:50 +00009695CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9696 if (clang::FunctionDecl *func_decl =
9697 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9698 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9699 if (clang::ObjCMethodDecl *objc_method =
9700 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9701 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9702 else
Greg Claytonfe689042015-11-10 17:47:04 +00009703 return CompilerType();
9704}
9705
Kate Stoneb9c1b512016-09-06 20:57:50 +00009706size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9707 if (clang::FunctionDecl *func_decl =
9708 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9709 return func_decl->param_size();
9710 if (clang::ObjCMethodDecl *objc_method =
9711 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9712 return objc_method->param_size();
9713 else
9714 return 0;
9715}
9716
9717CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9718 size_t idx) {
9719 if (clang::FunctionDecl *func_decl =
9720 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9721 if (idx < func_decl->param_size()) {
9722 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9723 if (var_decl)
9724 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9725 }
9726 } else if (clang::ObjCMethodDecl *objc_method =
9727 llvm::dyn_cast<clang::ObjCMethodDecl>(
9728 (clang::Decl *)opaque_decl)) {
9729 if (idx < objc_method->param_size())
9730 return CompilerType(
9731 this,
9732 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9733 }
9734 return CompilerType();
9735}
9736
Paul Hermand628cbb2015-09-15 23:44:17 +00009737//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00009738// CompilerDeclContext functions
9739//----------------------------------------------------------------------
9740
Kate Stoneb9c1b512016-09-06 20:57:50 +00009741std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9742 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9743 std::vector<CompilerDecl> found_decls;
9744 if (opaque_decl_ctx) {
9745 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9746 std::set<DeclContext *> searched;
9747 std::multimap<DeclContext *, DeclContext *> search_queue;
9748 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009749
Kate Stoneb9c1b512016-09-06 20:57:50 +00009750 for (clang::DeclContext *decl_context = root_decl_ctx;
9751 decl_context != nullptr && found_decls.empty();
9752 decl_context = decl_context->getParent()) {
9753 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +00009754
Kate Stoneb9c1b512016-09-06 20:57:50 +00009755 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9756 it++) {
9757 if (!searched.insert(it->second).second)
9758 continue;
9759 symbol_file->ParseDeclsForContext(
9760 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +00009761
Kate Stoneb9c1b512016-09-06 20:57:50 +00009762 for (clang::Decl *child : it->second->decls()) {
9763 if (clang::UsingDirectiveDecl *ud =
9764 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9765 if (ignore_using_decls)
9766 continue;
9767 clang::DeclContext *from = ud->getCommonAncestor();
9768 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9769 search_queue.insert(
9770 std::make_pair(from, ud->getNominatedNamespace()));
9771 } else if (clang::UsingDecl *ud =
9772 llvm::dyn_cast<clang::UsingDecl>(child)) {
9773 if (ignore_using_decls)
9774 continue;
9775 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9776 clang::Decl *target = usd->getTargetDecl();
9777 if (clang::NamedDecl *nd =
9778 llvm::dyn_cast<clang::NamedDecl>(target)) {
9779 IdentifierInfo *ii = nd->getIdentifier();
9780 if (ii != nullptr &&
9781 ii->getName().equals(name.AsCString(nullptr)))
9782 found_decls.push_back(CompilerDecl(this, nd));
9783 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009784 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009785 } else if (clang::NamedDecl *nd =
9786 llvm::dyn_cast<clang::NamedDecl>(child)) {
9787 IdentifierInfo *ii = nd->getIdentifier();
9788 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9789 found_decls.push_back(CompilerDecl(this, nd));
9790 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009791 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009792 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009793 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009794 }
9795 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +00009796}
9797
Dawn Perchikb5925782015-12-12 19:31:41 +00009798// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009799// and return the number of levels it took to find it, or
9800// LLDB_INVALID_DECL_LEVEL
9801// if not found. If the decl was imported via a using declaration, its name
9802// and/or
9803// type, if set, will be used to check that the decl found in the scope is a
9804// match.
Dawn Perchikb5925782015-12-12 19:31:41 +00009805//
Kate Stoneb9c1b512016-09-06 20:57:50 +00009806// The optional name is required by languages (like C++) to handle using
9807// declarations
Dawn Perchikb5925782015-12-12 19:31:41 +00009808// like:
9809//
9810// void poo();
9811// namespace ns {
9812// void foo();
9813// void goo();
9814// }
9815// void bar() {
9816// using ns::foo;
9817// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
9818// // LLDB_INVALID_DECL_LEVEL for 'goo'.
9819// }
9820//
9821// The optional type is useful in the case that there's a specific overload
9822// that we're looking for that might otherwise be shadowed, like:
9823//
9824// void foo(int);
9825// namespace ns {
9826// void foo();
9827// }
9828// void bar() {
9829// using ns::foo;
9830// // CountDeclLevels returns 0 for { 'foo', void() },
9831// // 1 for { 'foo', void(int) }, and
9832// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
9833// }
9834//
9835// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +00009836// function at file scope will return the same level as a function at global
9837// scope.
9838// Ideally we'd like to treat the file scope as an additional scope just below
9839// the
9840// global scope. More work needs to be done to recognise that, if the decl
9841// we're
9842// trying to look up is static, we should compare its source file with that of
9843// the
Dawn Perchikb5925782015-12-12 19:31:41 +00009844// current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009845uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
9846 clang::DeclContext *child_decl_ctx,
9847 ConstString *child_name,
9848 CompilerType *child_type) {
9849 if (frame_decl_ctx) {
9850 std::set<DeclContext *> searched;
9851 std::multimap<DeclContext *, DeclContext *> search_queue;
9852 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +00009853
Kate Stoneb9c1b512016-09-06 20:57:50 +00009854 // Get the lookup scope for the decl we're trying to find.
9855 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +00009856
Kate Stoneb9c1b512016-09-06 20:57:50 +00009857 // Look for it in our scope's decl context and its parents.
9858 uint32_t level = 0;
9859 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
9860 decl_ctx = decl_ctx->getParent()) {
9861 if (!decl_ctx->isLookupContext())
9862 continue;
9863 if (decl_ctx == parent_decl_ctx)
9864 // Found it!
9865 return level;
9866 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
9867 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
9868 it++) {
9869 if (searched.find(it->second) != searched.end())
9870 continue;
9871
9872 // Currently DWARF has one shared translation unit for all Decls at top
9873 // level, so this
9874 // would erroneously find using statements anywhere. So don't look at
9875 // the top-level
9876 // translation unit.
9877 // TODO fix this and add a testcase that depends on it.
9878
9879 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
9880 continue;
9881
9882 searched.insert(it->second);
9883 symbol_file->ParseDeclsForContext(
9884 CompilerDeclContext(this, it->second));
9885
9886 for (clang::Decl *child : it->second->decls()) {
9887 if (clang::UsingDirectiveDecl *ud =
9888 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9889 clang::DeclContext *ns = ud->getNominatedNamespace();
9890 if (ns == parent_decl_ctx)
9891 // Found it!
9892 return level;
9893 clang::DeclContext *from = ud->getCommonAncestor();
9894 if (searched.find(ns) == searched.end())
9895 search_queue.insert(std::make_pair(from, ns));
9896 } else if (child_name) {
9897 if (clang::UsingDecl *ud =
9898 llvm::dyn_cast<clang::UsingDecl>(child)) {
9899 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9900 clang::Decl *target = usd->getTargetDecl();
9901 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
9902 if (!nd)
9903 continue;
9904 // Check names.
9905 IdentifierInfo *ii = nd->getIdentifier();
9906 if (ii == nullptr ||
9907 !ii->getName().equals(child_name->AsCString(nullptr)))
9908 continue;
9909 // Check types, if one was provided.
9910 if (child_type) {
9911 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
9912 if (!AreTypesSame(clang_type, *child_type,
9913 /*ignore_qualifiers=*/true))
9914 continue;
9915 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009916 // Found it!
9917 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009918 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009919 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009920 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009921 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009922 }
9923 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +00009924 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009925 }
9926 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +00009927}
9928
Kate Stoneb9c1b512016-09-06 20:57:50 +00009929bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
9930 if (opaque_decl_ctx)
9931 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
9932 else
Greg Clayton99558cc42015-08-24 23:46:31 +00009933 return false;
9934}
9935
Kate Stoneb9c1b512016-09-06 20:57:50 +00009936ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
9937 if (opaque_decl_ctx) {
9938 clang::NamedDecl *named_decl =
9939 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9940 if (named_decl)
9941 return ConstString(named_decl->getName());
9942 }
9943 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +00009944}
9945
Kate Stoneb9c1b512016-09-06 20:57:50 +00009946ConstString
9947ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
9948 if (opaque_decl_ctx) {
9949 clang::NamedDecl *named_decl =
9950 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9951 if (named_decl)
9952 return ConstString(
9953 llvm::StringRef(named_decl->getQualifiedNameAsString()));
9954 }
9955 return ConstString();
9956}
9957
9958bool ClangASTContext::DeclContextIsClassMethod(
9959 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
9960 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
9961 if (opaque_decl_ctx) {
9962 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9963 if (ObjCMethodDecl *objc_method =
9964 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
9965 if (is_instance_method_ptr)
9966 *is_instance_method_ptr = objc_method->isInstanceMethod();
9967 if (language_ptr)
9968 *language_ptr = eLanguageTypeObjC;
9969 if (language_object_name_ptr)
9970 language_object_name_ptr->SetCString("self");
9971 return true;
9972 } else if (CXXMethodDecl *cxx_method =
9973 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
9974 if (is_instance_method_ptr)
9975 *is_instance_method_ptr = cxx_method->isInstance();
9976 if (language_ptr)
9977 *language_ptr = eLanguageTypeC_plus_plus;
9978 if (language_object_name_ptr)
9979 language_object_name_ptr->SetCString("this");
9980 return true;
9981 } else if (clang::FunctionDecl *function_decl =
9982 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
9983 ClangASTMetadata *metadata =
9984 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
9985 if (metadata && metadata->HasObjectPtr()) {
9986 if (is_instance_method_ptr)
9987 *is_instance_method_ptr = true;
9988 if (language_ptr)
9989 *language_ptr = eLanguageTypeObjC;
9990 if (language_object_name_ptr)
9991 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
9992 return true;
9993 }
9994 }
9995 }
9996 return false;
9997}
9998
9999clang::DeclContext *
10000ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10001 if (dc.IsClang())
10002 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10003 return nullptr;
10004}
Greg Clayton99558cc42015-08-24 23:46:31 +000010005
10006ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010007ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10008 if (dc.IsClang())
10009 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10010 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10011 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010012}
10013
10014CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010015ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10016 if (dc.IsClang())
10017 return llvm::dyn_cast<clang::CXXMethodDecl>(
10018 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10019 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010020}
10021
10022clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010023ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10024 if (dc.IsClang())
10025 return llvm::dyn_cast<clang::FunctionDecl>(
10026 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10027 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010028}
10029
10030clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010031ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10032 if (dc.IsClang())
10033 return llvm::dyn_cast<clang::NamespaceDecl>(
10034 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10035 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010036}
10037
10038ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010039ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10040 const void *object) {
10041 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10042 if (ast)
10043 return ClangASTContext::GetMetadata(ast, object);
10044 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010045}
10046
10047clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010048ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10049 ClangASTContext *ast =
10050 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10051 if (ast)
10052 return ast->getASTContext();
10053 return nullptr;
10054}
10055
10056ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10057 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10058 m_target_wp(target.shared_from_this()),
10059 m_persistent_variables(new ClangPersistentVariables) {}
10060
10061UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010062 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010063 Expression::ResultType desired_type,
10064 const EvaluateExpressionOptions &options) {
10065 TargetSP target_sp = m_target_wp.lock();
10066 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010067 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010068
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010069 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010070 desired_type, options);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010071}
10072
Kate Stoneb9c1b512016-09-06 20:57:50 +000010073FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10074 const CompilerType &return_type, const Address &function_address,
10075 const ValueList &arg_value_list, const char *name) {
10076 TargetSP target_sp = m_target_wp.lock();
10077 if (!target_sp)
10078 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010079
Kate Stoneb9c1b512016-09-06 20:57:50 +000010080 Process *process = target_sp->GetProcessSP().get();
10081 if (!process)
10082 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010083
Kate Stoneb9c1b512016-09-06 20:57:50 +000010084 return new ClangFunctionCaller(*process, return_type, function_address,
10085 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010086}
10087
10088UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010089ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10090 const char *name) {
10091 TargetSP target_sp = m_target_wp.lock();
10092 if (!target_sp)
10093 return nullptr;
10094
10095 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010096}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010097
10098PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010099ClangASTContextForExpressions::GetPersistentExpressionState() {
10100 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010101}