blob: ba7ce0d19ccfd19f34261a5b6fa757bc4fe667ec [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
12// C Includes
13// C++ Includes
Greg Claytonff48e4b2015-02-03 02:05:44 +000014#include <mutex> // std::once
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include <string>
Sean Callananfe38c852015-10-08 23:07:53 +000016#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017
18// Other libraries and framework includes
Greg Clayton6beaaa62011-01-17 03:46:26 +000019
Kate Stoneb9c1b512016-09-06 20:57:50 +000020// Clang headers like to use NDEBUG inside of them to enable/disable debug
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000021// related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
Greg Clayton6beaaa62011-01-17 03:46:26 +000022// or another. This is bad because it means that if clang was built in release
23// mode, it assumes that you are building in release mode which is not always
24// the case. You can end up with functions that are defined as empty in header
25// files when NDEBUG is not defined, and this can cause link errors with the
26// clang .a files that you have since you might be missing functions in the .a
27// file. So we have to define NDEBUG when including clang headers to avoid any
28// mismatches. This is covered by rdar://problem/8691220
29
Sean Callanan3b1d4f62011-10-26 17:46:51 +000030#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000031#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000032#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000033// Need to include assert.h so it is as clang would expect it to be (disabled)
34#include <assert.h>
35#endif
36
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "clang/AST/ASTContext.h"
38#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000039#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000041#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000042#include "clang/AST/DeclTemplate.h"
Greg Claytonfe689042015-11-10 17:47:04 +000043#include "clang/AST/Mangle.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044#include "clang/AST/RecordLayout.h"
45#include "clang/AST/Type.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000046#include "clang/AST/VTableBuilder.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000048#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000050#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051#include "clang/Basic/SourceManager.h"
52#include "clang/Basic/TargetInfo.h"
53#include "clang/Basic/TargetOptions.h"
54#include "clang/Frontend/FrontendOptions.h"
55#include "clang/Frontend/LangStandard.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000056
57#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000058#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000059#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
60// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
61#include <assert.h>
62#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063
Greg Claytond8d4a572015-08-11 21:38:15 +000064#include "llvm/Support/Signals.h"
65
Zachary Turnerd133f6a2016-03-28 22:53:41 +000066#include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
67#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
68#include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
Greg Clayton514487e2011-02-15 21:59:32 +000069#include "lldb/Core/ArchSpec.h"
Greg Clayton73b472d2010-10-27 03:32:59 +000070#include "lldb/Core/Flags.h"
Sean Callananfb8b7092010-10-28 18:19:36 +000071#include "lldb/Core/Log.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000072#include "lldb/Core/Module.h"
73#include "lldb/Core/PluginManager.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000074#include "lldb/Core/RegularExpression.h"
Ulrich Weigand9521ad22016-04-15 09:55:52 +000075#include "lldb/Core/Scalar.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000076#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000077#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000078#include "lldb/Core/UniqueCStringMap.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000079#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000080#include "lldb/Symbol/ClangASTImporter.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000081#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000082#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000083#include "lldb/Symbol/ClangUtil.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000084#include "lldb/Symbol/ObjectFile.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000085#include "lldb/Symbol/SymbolFile.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000086#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000087#include "lldb/Target/ExecutionContext.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000088#include "lldb/Target/Language.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000089#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham151c0322015-09-15 21:13:50 +000090#include "lldb/Target/Process.h"
91#include "lldb/Target/Target.h"
Sean Callananc530ba92016-05-02 21:15:31 +000092#include "lldb/Utility/LLDBAssert.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000093
Greg Clayton261ac3f2015-08-28 01:01:03 +000094#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
Zachary Turner42dff792016-04-15 00:21:26 +000095#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000096
Eli Friedman932197d2010-06-13 19:06:42 +000097#include <stdio.h>
98
Greg Clayton1341baf2013-07-11 23:36:31 +000099#include <mutex>
100
Greg Claytonc86103d2010-08-05 01:57:25 +0000101using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102using namespace lldb_private;
103using namespace llvm;
104using namespace clang;
105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106namespace {
107static inline bool
108ClangASTContextSupportsLanguage(lldb::LanguageType language) {
109 return language == eLanguageTypeUnknown || // Clang is the default type system
110 Language::LanguageIsC(language) ||
111 Language::LanguageIsCPlusPlus(language) ||
112 Language::LanguageIsObjC(language) ||
113 Language::LanguageIsPascal(language) ||
114 // Use Clang for Rust until there is a proper language plugin for it
115 language == eLanguageTypeRust ||
116 language == eLanguageTypeExtRenderScript;
117}
Greg Clayton56939cb2015-09-17 22:23:34 +0000118}
119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *>
121 ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123static ClangASTMap &GetASTMap() {
124 static ClangASTMap *g_map_ptr = nullptr;
125 static std::once_flag g_once_flag;
126 std::call_once(g_once_flag, []() {
127 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
128 });
129 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000130}
131
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132static bool IsOperator(const char *name,
133 clang::OverloadedOperatorKind &op_kind) {
134 if (name == nullptr || name[0] == '\0')
135 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000136
137#define OPERATOR_PREFIX "operator"
138#define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1)
139
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 const char *post_op_name = nullptr;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 bool no_space = true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
145 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000146
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147 post_op_name = name + OPERATOR_PREFIX_LENGTH;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 if (post_op_name[0] == ' ') {
150 post_op_name++;
151 no_space = false;
152 }
Pavel Labath1ac2b202016-08-15 14:32:32 +0000153
154#undef OPERATOR_PREFIX
155#undef OPERATOR_PREFIX_LENGTH
156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157 // This is an operator, set the overloaded operator kind to invalid
158 // in case this is a conversion operator...
159 op_kind = clang::NUM_OVERLOADED_OPERATORS;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 switch (post_op_name[0]) {
162 default:
163 if (no_space)
164 return false;
165 break;
166 case 'n':
167 if (no_space)
168 return false;
169 if (strcmp(post_op_name, "new") == 0)
170 op_kind = clang::OO_New;
171 else if (strcmp(post_op_name, "new[]") == 0)
172 op_kind = clang::OO_Array_New;
173 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000174
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 case 'd':
176 if (no_space)
177 return false;
178 if (strcmp(post_op_name, "delete") == 0)
179 op_kind = clang::OO_Delete;
180 else if (strcmp(post_op_name, "delete[]") == 0)
181 op_kind = clang::OO_Array_Delete;
182 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 case '+':
185 if (post_op_name[1] == '\0')
186 op_kind = clang::OO_Plus;
187 else if (post_op_name[2] == '\0') {
188 if (post_op_name[1] == '=')
189 op_kind = clang::OO_PlusEqual;
190 else if (post_op_name[1] == '+')
191 op_kind = clang::OO_PlusPlus;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000192 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000194
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 case '-':
196 if (post_op_name[1] == '\0')
197 op_kind = clang::OO_Minus;
198 else if (post_op_name[2] == '\0') {
199 switch (post_op_name[1]) {
200 case '=':
201 op_kind = clang::OO_MinusEqual;
202 break;
203 case '-':
204 op_kind = clang::OO_MinusMinus;
205 break;
206 case '>':
207 op_kind = clang::OO_Arrow;
208 break;
209 }
210 } else if (post_op_name[3] == '\0') {
211 if (post_op_name[2] == '*')
212 op_kind = clang::OO_ArrowStar;
213 break;
214 }
215 break;
216
217 case '*':
218 if (post_op_name[1] == '\0')
219 op_kind = clang::OO_Star;
220 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
221 op_kind = clang::OO_StarEqual;
222 break;
223
224 case '/':
225 if (post_op_name[1] == '\0')
226 op_kind = clang::OO_Slash;
227 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
228 op_kind = clang::OO_SlashEqual;
229 break;
230
231 case '%':
232 if (post_op_name[1] == '\0')
233 op_kind = clang::OO_Percent;
234 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
235 op_kind = clang::OO_PercentEqual;
236 break;
237
238 case '^':
239 if (post_op_name[1] == '\0')
240 op_kind = clang::OO_Caret;
241 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
242 op_kind = clang::OO_CaretEqual;
243 break;
244
245 case '&':
246 if (post_op_name[1] == '\0')
247 op_kind = clang::OO_Amp;
248 else if (post_op_name[2] == '\0') {
249 switch (post_op_name[1]) {
250 case '=':
251 op_kind = clang::OO_AmpEqual;
252 break;
253 case '&':
254 op_kind = clang::OO_AmpAmp;
255 break;
256 }
257 }
258 break;
259
260 case '|':
261 if (post_op_name[1] == '\0')
262 op_kind = clang::OO_Pipe;
263 else if (post_op_name[2] == '\0') {
264 switch (post_op_name[1]) {
265 case '=':
266 op_kind = clang::OO_PipeEqual;
267 break;
268 case '|':
269 op_kind = clang::OO_PipePipe;
270 break;
271 }
272 }
273 break;
274
275 case '~':
276 if (post_op_name[1] == '\0')
277 op_kind = clang::OO_Tilde;
278 break;
279
280 case '!':
281 if (post_op_name[1] == '\0')
282 op_kind = clang::OO_Exclaim;
283 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
284 op_kind = clang::OO_ExclaimEqual;
285 break;
286
287 case '=':
288 if (post_op_name[1] == '\0')
289 op_kind = clang::OO_Equal;
290 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
291 op_kind = clang::OO_EqualEqual;
292 break;
293
294 case '<':
295 if (post_op_name[1] == '\0')
296 op_kind = clang::OO_Less;
297 else if (post_op_name[2] == '\0') {
298 switch (post_op_name[1]) {
299 case '<':
300 op_kind = clang::OO_LessLess;
301 break;
302 case '=':
303 op_kind = clang::OO_LessEqual;
304 break;
305 }
306 } else if (post_op_name[3] == '\0') {
307 if (post_op_name[2] == '=')
308 op_kind = clang::OO_LessLessEqual;
309 }
310 break;
311
312 case '>':
313 if (post_op_name[1] == '\0')
314 op_kind = clang::OO_Greater;
315 else if (post_op_name[2] == '\0') {
316 switch (post_op_name[1]) {
317 case '>':
318 op_kind = clang::OO_GreaterGreater;
319 break;
320 case '=':
321 op_kind = clang::OO_GreaterEqual;
322 break;
323 }
324 } else if (post_op_name[1] == '>' && post_op_name[2] == '=' &&
325 post_op_name[3] == '\0') {
326 op_kind = clang::OO_GreaterGreaterEqual;
327 }
328 break;
329
330 case ',':
331 if (post_op_name[1] == '\0')
332 op_kind = clang::OO_Comma;
333 break;
334
335 case '(':
336 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
337 op_kind = clang::OO_Call;
338 break;
339
340 case '[':
341 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
342 op_kind = clang::OO_Subscript;
343 break;
344 }
345
346 return true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000347}
Enrico Granata5d84a692014-08-19 21:46:37 +0000348
Greg Clayton57ee3062013-07-11 22:46:58 +0000349clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
351 switch (access) {
352 default:
353 break;
354 case eAccessNone:
Greg Clayton8cf05932010-07-22 18:30:50 +0000355 return AS_none;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 case eAccessPublic:
357 return AS_public;
358 case eAccessPrivate:
359 return AS_private;
360 case eAccessProtected:
361 return AS_protected;
362 }
363 return AS_none;
Greg Clayton8cf05932010-07-22 18:30:50 +0000364}
365
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
367 // FIXME: Cleanup per-file based stuff.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369 // Set some properties which depend solely on the input kind; it would be nice
370 // to move these to the language standard, and have the driver resolve the
371 // input kind + language standard.
372 if (IK == IK_Asm) {
373 Opts.AsmPreprocessor = 1;
374 } else if (IK == IK_ObjC || IK == IK_ObjCXX || IK == IK_PreprocessedObjC ||
375 IK == IK_PreprocessedObjCXX) {
376 Opts.ObjC1 = Opts.ObjC2 = 1;
377 }
378
379 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
380
381 if (LangStd == LangStandard::lang_unspecified) {
382 // Based on the base language, pick one.
383 switch (IK) {
384 case IK_None:
385 case IK_AST:
386 case IK_LLVM_IR:
387 case IK_RenderScript:
388 assert(!"Invalid input kind!");
389 case IK_OpenCL:
390 LangStd = LangStandard::lang_opencl;
391 break;
392 case IK_CUDA:
393 case IK_PreprocessedCuda:
394 LangStd = LangStandard::lang_cuda;
395 break;
396 case IK_Asm:
397 case IK_C:
398 case IK_PreprocessedC:
399 case IK_ObjC:
400 case IK_PreprocessedObjC:
401 LangStd = LangStandard::lang_gnu99;
402 break;
403 case IK_CXX:
404 case IK_PreprocessedCXX:
405 case IK_ObjCXX:
406 case IK_PreprocessedObjCXX:
407 LangStd = LangStandard::lang_gnucxx98;
408 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000410 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
413 Opts.LineComment = Std.hasLineComments();
414 Opts.C99 = Std.isC99();
415 Opts.CPlusPlus = Std.isCPlusPlus();
416 Opts.CPlusPlus11 = Std.isCPlusPlus11();
417 Opts.Digraphs = Std.hasDigraphs();
418 Opts.GNUMode = Std.isGNUMode();
419 Opts.GNUInline = !Std.isC99();
420 Opts.HexFloats = Std.hasHexFloats();
421 Opts.ImplicitInt = Std.hasImplicitInt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424
Kate Stoneb9c1b512016-09-06 20:57:50 +0000425 // OpenCL has some additional defaults.
426 if (LangStd == LangStandard::lang_opencl) {
427 Opts.OpenCL = 1;
428 Opts.AltiVec = 1;
429 Opts.CXXOperatorNames = 1;
430 Opts.LaxVectorConversions = 1;
431 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 // OpenCL and C++ both have bool, true, false keywords.
434 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436 // if (Opts.CPlusPlus)
437 // Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
438 //
439 // if (Args.hasArg(OPT_fobjc_gc_only))
440 // Opts.setGCMode(LangOptions::GCOnly);
441 // else if (Args.hasArg(OPT_fobjc_gc))
442 // Opts.setGCMode(LangOptions::HybridGC);
443 //
444 // if (Args.hasArg(OPT_print_ivar_layout))
445 // Opts.ObjCGCBitmapPrint = 1;
446 //
447 // if (Args.hasArg(OPT_faltivec))
448 // Opts.AltiVec = 1;
449 //
450 // if (Args.hasArg(OPT_pthread))
451 // Opts.POSIXThreads = 1;
452 //
453 // llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
454 // "default");
455 // if (Vis == "default")
456 Opts.setValueVisibilityMode(DefaultVisibility);
457 // else if (Vis == "hidden")
458 // Opts.setVisibilityMode(LangOptions::Hidden);
459 // else if (Vis == "protected")
460 // Opts.setVisibilityMode(LangOptions::Protected);
461 // else
462 // Diags.Report(diag::err_drv_invalid_value)
463 // << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465 // Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
468 // is specified, or -std is set to a conforming mode.
469 Opts.Trigraphs = !Opts.GNUMode;
470 // if (Args.hasArg(OPT_trigraphs))
471 // Opts.Trigraphs = 1;
472 //
473 // Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
474 // OPT_fno_dollars_in_identifiers,
475 // !Opts.AsmPreprocessor);
476 // Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
477 // Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
478 // Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
479 // if (Args.hasArg(OPT_fno_lax_vector_conversions))
480 // Opts.LaxVectorConversions = 0;
481 // Opts.Exceptions = Args.hasArg(OPT_fexceptions);
482 // Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
483 // Opts.Blocks = Args.hasArg(OPT_fblocks);
484 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
485 // Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
486 // Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
487 // Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
488 // Opts.AssumeSaneOperatorNew =
489 // !Args.hasArg(OPT_fno_assume_sane_operator_new);
490 // Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
491 // Opts.AccessControl = Args.hasArg(OPT_faccess_control);
492 // Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
493 // Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
494 // Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth,
495 // 99,
496 // Diags);
497 // Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
498 // Opts.ObjCConstantStringClass = getLastArgValue(Args,
499 // OPT_fconstant_string_class);
500 // Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
501 // Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
502 // Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
503 // Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
504 // Opts.Static = Args.hasArg(OPT_static_define);
505 Opts.OptimizeSize = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506
Kate Stoneb9c1b512016-09-06 20:57:50 +0000507 // FIXME: Eliminate this dependency.
508 // unsigned Opt =
509 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
510 // Opts.Optimize = Opt != 0;
511 unsigned Opt = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 // This is the __NO_INLINE__ define, which just depends on things like the
514 // optimization level and -fno-inline, not actually whether the backend has
515 // inlining enabled.
516 //
517 // FIXME: This is affected by other options (-fno-inline).
518 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520 // unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
521 // switch (SSP) {
522 // default:
523 // Diags.Report(diag::err_drv_invalid_value)
524 // << Args.getLastArg(OPT_stack_protector)->getAsString(Args) <<
525 // SSP;
526 // break;
527 // case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
528 // case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
529 // case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
530 // }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531}
532
Kate Stoneb9c1b512016-09-06 20:57:50 +0000533ClangASTContext::ClangASTContext(const char *target_triple)
534 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_ap(),
535 m_language_options_ap(), m_source_manager_ap(), m_diagnostics_engine_ap(),
536 m_target_options_rp(), m_target_info_ap(), m_identifier_table_ap(),
537 m_selector_table_ap(), m_builtins_ap(), m_callback_tag_decl(nullptr),
538 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
539 m_pointer_byte_size(0), m_ast_owned(false) {
540 if (target_triple && target_triple[0])
541 SetTargetTriple(target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542}
543
544//----------------------------------------------------------------------
545// Destructor
546//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000547ClangASTContext::~ClangASTContext() { Finalize(); }
548
549ConstString ClangASTContext::GetPluginNameStatic() {
550 return ConstString("clang");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000551}
552
Kate Stoneb9c1b512016-09-06 20:57:50 +0000553ConstString ClangASTContext::GetPluginName() {
554 return ClangASTContext::GetPluginNameStatic();
Greg Clayton56939cb2015-09-17 22:23:34 +0000555}
556
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557uint32_t ClangASTContext::GetPluginVersion() { return 1; }
Greg Clayton56939cb2015-09-17 22:23:34 +0000558
Kate Stoneb9c1b512016-09-06 20:57:50 +0000559lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
560 lldb_private::Module *module,
561 Target *target) {
562 if (ClangASTContextSupportsLanguage(language)) {
563 ArchSpec arch;
564 if (module)
565 arch = module->GetArchitecture();
566 else if (target)
567 arch = target->GetArchitecture();
Greg Clayton56939cb2015-09-17 22:23:34 +0000568
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569 if (arch.IsValid()) {
570 ArchSpec fixed_arch = arch;
571 // LLVM wants this to be set to iOS or MacOSX; if we're working on
572 // a bare-boards type image, change the triple for llvm's benefit.
573 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
574 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
575 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
576 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
577 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
578 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
579 } else {
580 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
Greg Clayton56939cb2015-09-17 22:23:34 +0000581 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000582 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000583
Kate Stoneb9c1b512016-09-06 20:57:50 +0000584 if (module) {
585 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
586 if (ast_sp) {
587 ast_sp->SetArchitecture(fixed_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000588 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589 return ast_sp;
590 } else if (target && target->IsValid()) {
591 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
592 new ClangASTContextForExpressions(*target));
593 if (ast_sp) {
594 ast_sp->SetArchitecture(fixed_arch);
595 ast_sp->m_scratch_ast_source_ap.reset(
596 new ClangASTSource(target->shared_from_this()));
597 ast_sp->m_scratch_ast_source_ap->InstallASTContext(
598 ast_sp->getASTContext());
599 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
600 ast_sp->m_scratch_ast_source_ap->CreateProxy());
601 ast_sp->SetExternalSource(proxy_ast_source);
602 return ast_sp;
603 }
604 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000606 }
607 return lldb::TypeSystemSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608}
609
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610void ClangASTContext::EnumerateSupportedLanguages(
611 std::set<lldb::LanguageType> &languages_for_types,
612 std::set<lldb::LanguageType> &languages_for_expressions) {
613 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
614 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
615 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
616 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
617 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
618 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
619
620 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
621 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
622 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
623 lldb::eLanguageTypeC_plus_plus_14});
624
625 languages_for_types.insert(s_supported_languages_for_types.begin(),
626 s_supported_languages_for_types.end());
627 languages_for_expressions.insert(
628 s_supported_languages_for_expressions.begin(),
629 s_supported_languages_for_expressions.end());
Enrico Granata5d84a692014-08-19 21:46:37 +0000630}
631
Kate Stoneb9c1b512016-09-06 20:57:50 +0000632void ClangASTContext::Initialize() {
633 PluginManager::RegisterPlugin(GetPluginNameStatic(),
634 "clang base AST context plug-in",
635 CreateInstance, EnumerateSupportedLanguages);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000636}
637
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638void ClangASTContext::Terminate() {
639 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640}
641
Kate Stoneb9c1b512016-09-06 20:57:50 +0000642void ClangASTContext::Finalize() {
643 if (m_ast_ap.get()) {
644 GetASTMap().Erase(m_ast_ap.get());
645 if (!m_ast_owned)
646 m_ast_ap.release();
647 }
648
649 m_builtins_ap.reset();
650 m_selector_table_ap.reset();
651 m_identifier_table_ap.reset();
652 m_target_info_ap.reset();
653 m_target_options_rp.reset();
654 m_diagnostics_engine_ap.reset();
655 m_source_manager_ap.reset();
656 m_language_options_ap.reset();
657 m_ast_ap.reset();
658 m_scratch_ast_source_ap.reset();
659}
660
661void ClangASTContext::Clear() {
662 m_ast_ap.reset();
663 m_language_options_ap.reset();
664 m_source_manager_ap.reset();
665 m_diagnostics_engine_ap.reset();
666 m_target_options_rp.reset();
667 m_target_info_ap.reset();
668 m_identifier_table_ap.reset();
669 m_selector_table_ap.reset();
670 m_builtins_ap.reset();
671 m_pointer_byte_size = 0;
672}
673
674const char *ClangASTContext::GetTargetTriple() {
675 return m_target_triple.c_str();
676}
677
678void ClangASTContext::SetTargetTriple(const char *target_triple) {
679 Clear();
680 m_target_triple.assign(target_triple);
681}
682
683void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
684 SetTargetTriple(arch.GetTriple().str().c_str());
685}
686
687bool ClangASTContext::HasExternalSource() {
688 ASTContext *ast = getASTContext();
689 if (ast)
690 return ast->getExternalSource() != nullptr;
691 return false;
692}
693
694void ClangASTContext::SetExternalSource(
695 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) {
696 ASTContext *ast = getASTContext();
697 if (ast) {
698 ast->setExternalSource(ast_source_ap);
699 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
700 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
701 }
702}
703
704void ClangASTContext::RemoveExternalSource() {
705 ASTContext *ast = getASTContext();
706
707 if (ast) {
708 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
709 ast->setExternalSource(empty_ast_source_ap);
710 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
711 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
712 }
713}
714
715void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
716 if (!m_ast_owned) {
717 m_ast_ap.release();
718 }
719 m_ast_owned = false;
720 m_ast_ap.reset(ast_ctx);
721 GetASTMap().Insert(ast_ctx, this);
722}
723
724ASTContext *ClangASTContext::getASTContext() {
725 if (m_ast_ap.get() == nullptr) {
726 m_ast_owned = true;
727 m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
728 *getIdentifierTable(), *getSelectorTable(),
729 *getBuiltinContext()));
730
731 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
732
733 // This can be NULL if we don't know anything about the architecture or if
734 // the
735 // target for an architecture isn't enabled in the llvm/clang that we built
736 TargetInfo *target_info = getTargetInfo();
737 if (target_info)
738 m_ast_ap->InitBuiltinTypes(*target_info);
739
740 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
741 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
742 // m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000744
745 GetASTMap().Insert(m_ast_ap.get(), this);
746
747 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap(
748 new ClangExternalASTSourceCallbacks(
749 ClangASTContext::CompleteTagDecl,
750 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
751 ClangASTContext::LayoutRecordType, this));
752 SetExternalSource(ast_source_ap);
753 }
754 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000755}
756
Kate Stoneb9c1b512016-09-06 20:57:50 +0000757ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
758 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
759 return clang_ast;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000760}
761
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762Builtin::Context *ClangASTContext::getBuiltinContext() {
763 if (m_builtins_ap.get() == nullptr)
764 m_builtins_ap.reset(new Builtin::Context());
765 return m_builtins_ap.get();
Sean Callanan79439e82010-11-18 02:56:27 +0000766}
767
Kate Stoneb9c1b512016-09-06 20:57:50 +0000768IdentifierTable *ClangASTContext::getIdentifierTable() {
769 if (m_identifier_table_ap.get() == nullptr)
770 m_identifier_table_ap.reset(
771 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
772 return m_identifier_table_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773}
774
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775LangOptions *ClangASTContext::getLanguageOptions() {
776 if (m_language_options_ap.get() == nullptr) {
777 m_language_options_ap.reset(new LangOptions());
778 ParseLangArgs(*m_language_options_ap, IK_ObjCXX, GetTargetTriple());
779 // InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
780 }
781 return m_language_options_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000782}
783
Kate Stoneb9c1b512016-09-06 20:57:50 +0000784SelectorTable *ClangASTContext::getSelectorTable() {
785 if (m_selector_table_ap.get() == nullptr)
786 m_selector_table_ap.reset(new SelectorTable());
787 return m_selector_table_ap.get();
Greg Claytonfe689042015-11-10 17:47:04 +0000788}
789
Kate Stoneb9c1b512016-09-06 20:57:50 +0000790clang::FileManager *ClangASTContext::getFileManager() {
791 if (m_file_manager_ap.get() == nullptr) {
792 clang::FileSystemOptions file_system_options;
793 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
794 }
795 return m_file_manager_ap.get();
796}
797
798clang::SourceManager *ClangASTContext::getSourceManager() {
799 if (m_source_manager_ap.get() == nullptr)
800 m_source_manager_ap.reset(
801 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
802 return m_source_manager_ap.get();
803}
804
805clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
806 if (m_diagnostics_engine_ap.get() == nullptr) {
807 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
808 m_diagnostics_engine_ap.reset(
809 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
810 }
811 return m_diagnostics_engine_ap.get();
812}
813
814clang::MangleContext *ClangASTContext::getMangleContext() {
815 if (m_mangle_ctx_ap.get() == nullptr)
816 m_mangle_ctx_ap.reset(getASTContext()->createMangleContext());
817 return m_mangle_ctx_ap.get();
818}
819
820class NullDiagnosticConsumer : public DiagnosticConsumer {
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000821public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000822 NullDiagnosticConsumer() {
823 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
824 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000825
Kate Stoneb9c1b512016-09-06 20:57:50 +0000826 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
827 const clang::Diagnostic &info) {
828 if (m_log) {
829 llvm::SmallVector<char, 32> diag_str(10);
830 info.FormatDiagnostic(diag_str);
831 diag_str.push_back('\0');
832 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000833 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000834 }
835
836 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
837 return new NullDiagnosticConsumer();
838 }
839
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000840private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000841 Log *m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000842};
843
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
845 if (m_diagnostic_consumer_ap.get() == nullptr)
846 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
847
848 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000849}
850
Kate Stoneb9c1b512016-09-06 20:57:50 +0000851std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
852 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) {
853 m_target_options_rp = std::make_shared<clang::TargetOptions>();
854 if (m_target_options_rp.get() != nullptr)
855 m_target_options_rp->Triple = m_target_triple;
856 }
857 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000858}
859
Kate Stoneb9c1b512016-09-06 20:57:50 +0000860TargetInfo *ClangASTContext::getTargetInfo() {
861 // target_triple should be something like "x86_64-apple-macosx"
862 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
863 m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
864 getTargetOptions()));
865 return m_target_info_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000866}
867
868#pragma mark Basic Types
869
Kate Stoneb9c1b512016-09-06 20:57:50 +0000870static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
871 ASTContext *ast, QualType qual_type) {
872 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
873 if (qual_type_bit_size == bit_size)
874 return true;
875 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000876}
Greg Clayton56939cb2015-09-17 22:23:34 +0000877
Greg Claytona1e5dc82015-08-11 22:53:00 +0000878CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000879ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
880 size_t bit_size) {
881 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
882 getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000883}
884
Kate Stoneb9c1b512016-09-06 20:57:50 +0000885CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
886 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
887 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000888 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000889 switch (encoding) {
890 case eEncodingInvalid:
891 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
892 return CompilerType(ast, ast->VoidPtrTy);
893 break;
894
895 case eEncodingUint:
896 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
897 return CompilerType(ast, ast->UnsignedCharTy);
898 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
899 return CompilerType(ast, ast->UnsignedShortTy);
900 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
901 return CompilerType(ast, ast->UnsignedIntTy);
902 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
903 return CompilerType(ast, ast->UnsignedLongTy);
904 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
905 return CompilerType(ast, ast->UnsignedLongLongTy);
906 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
907 return CompilerType(ast, ast->UnsignedInt128Ty);
908 break;
909
910 case eEncodingSint:
911 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
912 return CompilerType(ast, ast->SignedCharTy);
913 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
914 return CompilerType(ast, ast->ShortTy);
915 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
916 return CompilerType(ast, ast->IntTy);
917 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
918 return CompilerType(ast, ast->LongTy);
919 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
920 return CompilerType(ast, ast->LongLongTy);
921 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
922 return CompilerType(ast, ast->Int128Ty);
923 break;
924
925 case eEncodingIEEE754:
926 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
927 return CompilerType(ast, ast->FloatTy);
928 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
929 return CompilerType(ast, ast->DoubleTy);
930 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
931 return CompilerType(ast, ast->LongDoubleTy);
932 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
933 return CompilerType(ast, ast->HalfTy);
934 break;
935
936 case eEncodingVector:
937 // Sanity check that bit_size is a multiple of 8's.
938 if (bit_size && !(bit_size & 0x7u))
939 return CompilerType(
940 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
941 break;
942 }
943
944 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000945}
946
Greg Clayton57ee3062013-07-11 22:46:58 +0000947lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000948ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) {
949 if (name) {
950 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
951 static TypeNameToBasicTypeMap g_type_map;
952 static std::once_flag g_once_flag;
953 std::call_once(g_once_flag, []() {
954 // "void"
955 g_type_map.Append(ConstString("void").GetCString(), eBasicTypeVoid);
956
957 // "char"
958 g_type_map.Append(ConstString("char").GetCString(), eBasicTypeChar);
959 g_type_map.Append(ConstString("signed char").GetCString(),
960 eBasicTypeSignedChar);
961 g_type_map.Append(ConstString("unsigned char").GetCString(),
962 eBasicTypeUnsignedChar);
963 g_type_map.Append(ConstString("wchar_t").GetCString(), eBasicTypeWChar);
964 g_type_map.Append(ConstString("signed wchar_t").GetCString(),
965 eBasicTypeSignedWChar);
966 g_type_map.Append(ConstString("unsigned wchar_t").GetCString(),
967 eBasicTypeUnsignedWChar);
968 // "short"
969 g_type_map.Append(ConstString("short").GetCString(), eBasicTypeShort);
970 g_type_map.Append(ConstString("short int").GetCString(), eBasicTypeShort);
971 g_type_map.Append(ConstString("unsigned short").GetCString(),
972 eBasicTypeUnsignedShort);
973 g_type_map.Append(ConstString("unsigned short int").GetCString(),
974 eBasicTypeUnsignedShort);
975
976 // "int"
977 g_type_map.Append(ConstString("int").GetCString(), eBasicTypeInt);
978 g_type_map.Append(ConstString("signed int").GetCString(), eBasicTypeInt);
979 g_type_map.Append(ConstString("unsigned int").GetCString(),
980 eBasicTypeUnsignedInt);
981 g_type_map.Append(ConstString("unsigned").GetCString(),
982 eBasicTypeUnsignedInt);
983
984 // "long"
985 g_type_map.Append(ConstString("long").GetCString(), eBasicTypeLong);
986 g_type_map.Append(ConstString("long int").GetCString(), eBasicTypeLong);
987 g_type_map.Append(ConstString("unsigned long").GetCString(),
988 eBasicTypeUnsignedLong);
989 g_type_map.Append(ConstString("unsigned long int").GetCString(),
990 eBasicTypeUnsignedLong);
991
992 // "long long"
993 g_type_map.Append(ConstString("long long").GetCString(),
994 eBasicTypeLongLong);
995 g_type_map.Append(ConstString("long long int").GetCString(),
996 eBasicTypeLongLong);
997 g_type_map.Append(ConstString("unsigned long long").GetCString(),
998 eBasicTypeUnsignedLongLong);
999 g_type_map.Append(ConstString("unsigned long long int").GetCString(),
1000 eBasicTypeUnsignedLongLong);
1001
1002 // "int128"
1003 g_type_map.Append(ConstString("__int128_t").GetCString(),
1004 eBasicTypeInt128);
1005 g_type_map.Append(ConstString("__uint128_t").GetCString(),
1006 eBasicTypeUnsignedInt128);
1007
1008 // Miscellaneous
1009 g_type_map.Append(ConstString("bool").GetCString(), eBasicTypeBool);
1010 g_type_map.Append(ConstString("float").GetCString(), eBasicTypeFloat);
1011 g_type_map.Append(ConstString("double").GetCString(), eBasicTypeDouble);
1012 g_type_map.Append(ConstString("long double").GetCString(),
1013 eBasicTypeLongDouble);
1014 g_type_map.Append(ConstString("id").GetCString(), eBasicTypeObjCID);
1015 g_type_map.Append(ConstString("SEL").GetCString(), eBasicTypeObjCSel);
1016 g_type_map.Append(ConstString("nullptr").GetCString(), eBasicTypeNullPtr);
1017 g_type_map.Sort();
1018 });
1019
1020 return g_type_map.Find(name.GetCString(), eBasicTypeInvalid);
1021 }
1022 return eBasicTypeInvalid;
Greg Clayton57ee3062013-07-11 22:46:58 +00001023}
1024
Kate Stoneb9c1b512016-09-06 20:57:50 +00001025CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1026 const ConstString &name) {
1027 if (ast) {
1028 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1029 return ClangASTContext::GetBasicType(ast, basic_type);
1030 }
1031 return CompilerType();
1032}
1033
1034uint32_t ClangASTContext::GetPointerByteSize() {
1035 if (m_pointer_byte_size == 0)
1036 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid)
1037 .GetPointerType()
1038 .GetByteSize(nullptr);
1039 return m_pointer_byte_size;
1040}
1041
1042CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1043 return GetBasicType(getASTContext(), basic_type);
1044}
1045
1046CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1047 lldb::BasicType basic_type) {
1048 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001049 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001050 lldb::opaque_compiler_type_t clang_type =
1051 GetOpaqueCompilerType(ast, basic_type);
1052
1053 if (clang_type)
1054 return CompilerType(GetASTContext(ast), clang_type);
1055 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001056}
1057
Kate Stoneb9c1b512016-09-06 20:57:50 +00001058CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1059 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1060 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001061
Kate Stoneb9c1b512016-09-06 20:57:50 +00001062#define streq(a, b) strcmp(a, b) == 0
1063 assert(ast != nullptr);
1064 if (ast) {
1065 switch (dw_ate) {
1066 default:
1067 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001068
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069 case DW_ATE_address:
1070 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1071 return CompilerType(ast, ast->VoidPtrTy);
1072 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001073
Kate Stoneb9c1b512016-09-06 20:57:50 +00001074 case DW_ATE_boolean:
1075 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
1076 return CompilerType(ast, ast->BoolTy);
1077 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1078 return CompilerType(ast, ast->UnsignedCharTy);
1079 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1080 return CompilerType(ast, ast->UnsignedShortTy);
1081 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1082 return CompilerType(ast, ast->UnsignedIntTy);
1083 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001084
Kate Stoneb9c1b512016-09-06 20:57:50 +00001085 case DW_ATE_lo_user:
1086 // This has been seen to mean DW_AT_complex_integer
1087 if (type_name) {
1088 if (::strstr(type_name, "complex")) {
1089 CompilerType complex_int_clang_type =
1090 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1091 bit_size / 2);
1092 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1093 complex_int_clang_type)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 }
1096 break;
1097
1098 case DW_ATE_complex_float:
1099 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1100 return CompilerType(ast, ast->FloatComplexTy);
1101 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1102 return CompilerType(ast, ast->DoubleComplexTy);
1103 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1104 return CompilerType(ast, ast->LongDoubleComplexTy);
1105 else {
1106 CompilerType complex_float_clang_type =
1107 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1108 bit_size / 2);
1109 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1110 complex_float_clang_type)));
1111 }
1112 break;
1113
1114 case DW_ATE_float:
1115 if (streq(type_name, "float") &&
1116 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1117 return CompilerType(ast, ast->FloatTy);
1118 if (streq(type_name, "double") &&
1119 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1120 return CompilerType(ast, ast->DoubleTy);
1121 if (streq(type_name, "long double") &&
1122 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1123 return CompilerType(ast, ast->LongDoubleTy);
1124 // Fall back to not requiring a name match
1125 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1126 return CompilerType(ast, ast->FloatTy);
1127 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1128 return CompilerType(ast, ast->DoubleTy);
1129 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1130 return CompilerType(ast, ast->LongDoubleTy);
1131 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1132 return CompilerType(ast, ast->HalfTy);
1133 break;
1134
1135 case DW_ATE_signed:
1136 if (type_name) {
1137 if (streq(type_name, "wchar_t") &&
1138 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1139 (getTargetInfo() &&
1140 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1141 return CompilerType(ast, ast->WCharTy);
1142 if (streq(type_name, "void") &&
1143 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1144 return CompilerType(ast, ast->VoidTy);
1145 if (strstr(type_name, "long long") &&
1146 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1147 return CompilerType(ast, ast->LongLongTy);
1148 if (strstr(type_name, "long") &&
1149 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1150 return CompilerType(ast, ast->LongTy);
1151 if (strstr(type_name, "short") &&
1152 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1153 return CompilerType(ast, ast->ShortTy);
1154 if (strstr(type_name, "char")) {
1155 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1156 return CompilerType(ast, ast->CharTy);
1157 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1158 return CompilerType(ast, ast->SignedCharTy);
1159 }
1160 if (strstr(type_name, "int")) {
1161 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1162 return CompilerType(ast, ast->IntTy);
1163 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1164 return CompilerType(ast, ast->Int128Ty);
1165 }
1166 }
1167 // We weren't able to match up a type name, just search by size
1168 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1169 return CompilerType(ast, ast->CharTy);
1170 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1171 return CompilerType(ast, ast->ShortTy);
1172 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1173 return CompilerType(ast, ast->IntTy);
1174 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1175 return CompilerType(ast, ast->LongTy);
1176 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1177 return CompilerType(ast, ast->LongLongTy);
1178 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1179 return CompilerType(ast, ast->Int128Ty);
1180 break;
1181
1182 case DW_ATE_signed_char:
1183 if (ast->getLangOpts().CharIsSigned && type_name &&
1184 streq(type_name, "char")) {
1185 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1186 return CompilerType(ast, ast->CharTy);
1187 }
1188 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1189 return CompilerType(ast, ast->SignedCharTy);
1190 break;
1191
1192 case DW_ATE_unsigned:
1193 if (type_name) {
1194 if (streq(type_name, "wchar_t")) {
1195 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1196 if (!(getTargetInfo() &&
1197 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1198 return CompilerType(ast, ast->WCharTy);
1199 }
1200 }
1201 if (strstr(type_name, "long long")) {
1202 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1203 return CompilerType(ast, ast->UnsignedLongLongTy);
1204 } else if (strstr(type_name, "long")) {
1205 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1206 return CompilerType(ast, ast->UnsignedLongTy);
1207 } else if (strstr(type_name, "short")) {
1208 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1209 return CompilerType(ast, ast->UnsignedShortTy);
1210 } else if (strstr(type_name, "char")) {
1211 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1212 return CompilerType(ast, ast->UnsignedCharTy);
1213 } else if (strstr(type_name, "int")) {
1214 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1215 return CompilerType(ast, ast->UnsignedIntTy);
1216 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1217 return CompilerType(ast, ast->UnsignedInt128Ty);
1218 }
1219 }
1220 // We weren't able to match up a type name, just search by size
1221 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1222 return CompilerType(ast, ast->UnsignedCharTy);
1223 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1224 return CompilerType(ast, ast->UnsignedShortTy);
1225 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1226 return CompilerType(ast, ast->UnsignedIntTy);
1227 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1228 return CompilerType(ast, ast->UnsignedLongTy);
1229 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1230 return CompilerType(ast, ast->UnsignedLongLongTy);
1231 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1232 return CompilerType(ast, ast->UnsignedInt128Ty);
1233 break;
1234
1235 case DW_ATE_unsigned_char:
1236 if (!ast->getLangOpts().CharIsSigned && type_name &&
1237 streq(type_name, "char")) {
1238 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1239 return CompilerType(ast, ast->CharTy);
1240 }
1241 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1242 return CompilerType(ast, ast->UnsignedCharTy);
1243 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1244 return CompilerType(ast, ast->UnsignedShortTy);
1245 break;
1246
1247 case DW_ATE_imaginary_float:
1248 break;
1249
1250 case DW_ATE_UTF:
1251 if (type_name) {
1252 if (streq(type_name, "char16_t")) {
1253 return CompilerType(ast, ast->Char16Ty);
1254 } else if (streq(type_name, "char32_t")) {
1255 return CompilerType(ast, ast->Char32Ty);
1256 }
1257 }
1258 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001260 }
1261 // This assert should fire for anything that we don't catch above so we know
1262 // to fix any issues we run into.
1263 if (type_name) {
1264 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1265 "DW_TAG_base_type '%s' encoded with "
1266 "DW_ATE = 0x%x, bit_size = %u\n",
1267 type_name, dw_ate, bit_size);
1268 } else {
1269 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1270 "DW_TAG_base_type encoded with "
1271 "DW_ATE = 0x%x, bit_size = %u\n",
1272 dw_ate, bit_size);
1273 }
1274 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001275}
1276
Kate Stoneb9c1b512016-09-06 20:57:50 +00001277CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1278 if (ast)
1279 return CompilerType(ast, ast->UnknownAnyTy);
1280 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001281}
1282
Kate Stoneb9c1b512016-09-06 20:57:50 +00001283CompilerType ClangASTContext::GetCStringType(bool is_const) {
1284 ASTContext *ast = getASTContext();
1285 QualType char_type(ast->CharTy);
1286
1287 if (is_const)
1288 char_type.addConst();
1289
1290 return CompilerType(ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001291}
1292
Sean Callanan09ab4b72011-11-30 22:11:59 +00001293clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001294ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1295 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001296}
1297
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1299 clang::Decl *source_decl) {
1300 FileSystemOptions file_system_options;
1301 FileManager file_manager(file_system_options);
1302 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1303
1304 return importer.Import(source_decl);
Greg Clayton526e5af2010-11-13 03:52:47 +00001305}
1306
Kate Stoneb9c1b512016-09-06 20:57:50 +00001307bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1308 bool ignore_qualifiers) {
1309 ClangASTContext *ast =
1310 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1311 if (!ast || ast != type2.GetTypeSystem())
1312 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001313
Kate Stoneb9c1b512016-09-06 20:57:50 +00001314 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1315 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001316
Kate Stoneb9c1b512016-09-06 20:57:50 +00001317 QualType type1_qual = ClangUtil::GetQualType(type1);
1318 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001319
Kate Stoneb9c1b512016-09-06 20:57:50 +00001320 if (ignore_qualifiers) {
1321 type1_qual = type1_qual.getUnqualifiedType();
1322 type2_qual = type2_qual.getUnqualifiedType();
1323 }
1324
1325 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001326}
1327
Kate Stoneb9c1b512016-09-06 20:57:50 +00001328CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1329 if (clang::ObjCInterfaceDecl *interface_decl =
1330 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1331 return GetTypeForDecl(interface_decl);
1332 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1333 return GetTypeForDecl(tag_decl);
1334 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001335}
1336
Kate Stoneb9c1b512016-09-06 20:57:50 +00001337CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
1338 // No need to call the getASTContext() accessor (which can create the AST
1339 // if it isn't created yet, because we can't have created a decl in this
1340 // AST if our AST didn't already exist...
1341 ASTContext *ast = &decl->getASTContext();
1342 if (ast)
1343 return CompilerType(ast, ast->getTagDeclType(decl));
1344 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001345}
1346
Kate Stoneb9c1b512016-09-06 20:57:50 +00001347CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
1348 // No need to call the getASTContext() accessor (which can create the AST
1349 // if it isn't created yet, because we can't have created a decl in this
1350 // AST if our AST didn't already exist...
1351 ASTContext *ast = &decl->getASTContext();
1352 if (ast)
1353 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1354 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001355}
1356
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001357#pragma mark Structure, Unions, Classes
1358
Kate Stoneb9c1b512016-09-06 20:57:50 +00001359CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1360 AccessType access_type,
1361 const char *name, int kind,
1362 LanguageType language,
1363 ClangASTMetadata *metadata) {
1364 ASTContext *ast = getASTContext();
1365 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001366
Kate Stoneb9c1b512016-09-06 20:57:50 +00001367 if (decl_ctx == nullptr)
1368 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001369
Kate Stoneb9c1b512016-09-06 20:57:50 +00001370 if (language == eLanguageTypeObjC ||
1371 language == eLanguageTypeObjC_plus_plus) {
1372 bool isForwardDecl = true;
1373 bool isInternal = false;
1374 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1375 }
Greg Clayton9e409562010-07-28 02:04:09 +00001376
Kate Stoneb9c1b512016-09-06 20:57:50 +00001377 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1378 // we will need to update this code. I was told to currently always use
1379 // the CXXRecordDecl class since we often don't know from debug information
1380 // if something is struct or a class, so we default to always use the more
1381 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001382
Kate Stoneb9c1b512016-09-06 20:57:50 +00001383 bool is_anonymous = (!name) || (!name[0]);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001384
Kate Stoneb9c1b512016-09-06 20:57:50 +00001385 CXXRecordDecl *decl = CXXRecordDecl::Create(
1386 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1387 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1388
1389 if (is_anonymous)
1390 decl->setAnonymousStructOrUnion(true);
1391
1392 if (decl) {
1393 if (metadata)
1394 SetMetadata(ast, decl, *metadata);
1395
1396 if (access_type != eAccessNone)
1397 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1398
1399 if (decl_ctx)
1400 decl_ctx->addDecl(decl);
1401
1402 return CompilerType(ast, ast->getTagDeclType(decl));
1403 }
1404 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001405}
1406
Kate Stoneb9c1b512016-09-06 20:57:50 +00001407static TemplateParameterList *CreateTemplateParameterList(
1408 ASTContext *ast,
1409 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1410 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1411 const bool parameter_pack = false;
1412 const bool is_typename = false;
1413 const unsigned depth = 0;
1414 const size_t num_template_params = template_param_infos.GetSize();
1415 for (size_t i = 0; i < num_template_params; ++i) {
1416 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001417
Kate Stoneb9c1b512016-09-06 20:57:50 +00001418 IdentifierInfo *identifier_info = nullptr;
1419 if (name && name[0])
1420 identifier_info = &ast->Idents.get(name);
1421 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral) {
1422 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1423 *ast,
1424 ast->getTranslationUnitDecl(), // Is this the right decl context?,
1425 // SourceLocation StartLoc,
1426 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1427 template_param_infos.args[i].getIntegralType(), parameter_pack,
1428 nullptr));
1429
1430 } else {
1431 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1432 *ast,
1433 ast->getTranslationUnitDecl(), // Is this the right decl context?
1434 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1435 is_typename, parameter_pack));
1436 }
1437 }
1438
1439 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1440 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1441 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1442 SourceLocation(), requires_clause);
1443 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001444}
1445
Kate Stoneb9c1b512016-09-06 20:57:50 +00001446clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1447 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1448 const char *name, const TemplateParameterInfos &template_param_infos) {
1449 // /// \brief Create a function template node.
1450 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001451
Kate Stoneb9c1b512016-09-06 20:57:50 +00001452 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001453
Kate Stoneb9c1b512016-09-06 20:57:50 +00001454 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1455 ast, template_param_infos, template_param_decls);
1456 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1457 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1458 template_param_list, func_decl);
1459
1460 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1461 i < template_param_decl_count; ++i) {
1462 // TODO: verify which decl context we should put template_param_decls into..
1463 template_param_decls[i]->setDeclContext(func_decl);
1464 }
1465
1466 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001467}
1468
Kate Stoneb9c1b512016-09-06 20:57:50 +00001469void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1470 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1471 const TemplateParameterInfos &infos) {
1472 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001473
Kate Stoneb9c1b512016-09-06 20:57:50 +00001474 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1475 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001476}
1477
Kate Stoneb9c1b512016-09-06 20:57:50 +00001478ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1479 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1480 int kind, const TemplateParameterInfos &template_param_infos) {
1481 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001482
Kate Stoneb9c1b512016-09-06 20:57:50 +00001483 ClassTemplateDecl *class_template_decl = nullptr;
1484 if (decl_ctx == nullptr)
1485 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001486
Kate Stoneb9c1b512016-09-06 20:57:50 +00001487 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1488 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001489
Kate Stoneb9c1b512016-09-06 20:57:50 +00001490 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001491
Kate Stoneb9c1b512016-09-06 20:57:50 +00001492 for (NamedDecl *decl : result) {
1493 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001494 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001495 return class_template_decl;
1496 }
1497
1498 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1499
1500 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1501 ast, template_param_infos, template_param_decls);
1502
1503 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1504 *ast, (TagDecl::TagKind)kind,
1505 decl_ctx, // What decl context do we use here? TU? The actual decl
1506 // context?
1507 SourceLocation(), SourceLocation(), &identifier_info);
1508
1509 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1510 i < template_param_decl_count; ++i) {
1511 template_param_decls[i]->setDeclContext(template_cxx_decl);
1512 }
1513
1514 // With templated classes, we say that a class is templated with
1515 // specializations, but that the bare class has no functions.
1516 // template_cxx_decl->startDefinition();
1517 // template_cxx_decl->completeDefinition();
1518
1519 class_template_decl = ClassTemplateDecl::Create(
1520 *ast,
1521 decl_ctx, // What decl context do we use here? TU? The actual decl
1522 // context?
1523 SourceLocation(), decl_name, template_param_list, template_cxx_decl,
1524 nullptr);
1525
1526 if (class_template_decl) {
1527 if (access_type != eAccessNone)
1528 class_template_decl->setAccess(
1529 ConvertAccessTypeToAccessSpecifier(access_type));
1530
1531 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1532 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1533
1534 decl_ctx->addDecl(class_template_decl);
1535
Sean Callanan5e9e1992011-10-26 01:06:27 +00001536#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001537 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001538#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001539 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001540
Kate Stoneb9c1b512016-09-06 20:57:50 +00001541 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001542}
1543
Greg Claytonf0705c82011-10-22 03:33:13 +00001544ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001545ClangASTContext::CreateClassTemplateSpecializationDecl(
1546 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1547 const TemplateParameterInfos &template_param_infos) {
1548 ASTContext *ast = getASTContext();
1549 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1550 ClassTemplateSpecializationDecl::Create(
1551 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1552 SourceLocation(), class_template_decl, template_param_infos.args,
1553 nullptr);
1554
1555 class_template_specialization_decl->setSpecializationKind(
1556 TSK_ExplicitSpecialization);
1557
1558 return class_template_specialization_decl;
1559}
1560
1561CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1562 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1563 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001564 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001565 if (ast)
1566 return CompilerType(
1567 ast, ast->getTagDeclType(class_template_specialization_decl));
1568 }
1569 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001570}
1571
Kate Stoneb9c1b512016-09-06 20:57:50 +00001572static inline bool check_op_param(bool is_method,
1573 clang::OverloadedOperatorKind op_kind,
1574 bool unary, bool binary,
1575 uint32_t num_params) {
1576 // Special-case call since it can take any number of operands
1577 if (op_kind == OO_Call)
1578 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001579
Kate Stoneb9c1b512016-09-06 20:57:50 +00001580 // The parameter count doesn't include "this"
1581 if (is_method)
1582 ++num_params;
1583 if (num_params == 1)
1584 return unary;
1585 if (num_params == 2)
1586 return binary;
1587 else
Greg Clayton090d0982011-06-19 03:43:27 +00001588 return false;
1589}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001590
Kate Stoneb9c1b512016-09-06 20:57:50 +00001591bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1592 bool is_method, clang::OverloadedOperatorKind op_kind,
1593 uint32_t num_params) {
1594 switch (op_kind) {
1595 default:
1596 break;
1597 // C++ standard allows any number of arguments to new/delete
1598 case OO_New:
1599 case OO_Array_New:
1600 case OO_Delete:
1601 case OO_Array_Delete:
1602 return true;
1603 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001604
Kate Stoneb9c1b512016-09-06 20:57:50 +00001605#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1606 case OO_##Name: \
1607 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1608 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001609#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001610 default:
1611 break;
1612 }
1613 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001614}
1615
Greg Clayton57ee3062013-07-11 22:46:58 +00001616clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001617ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1618 clang::AccessSpecifier rhs) {
1619 // Make the access equal to the stricter of the field and the nested field's
1620 // access
1621 if (lhs == AS_none || rhs == AS_none)
1622 return AS_none;
1623 if (lhs == AS_private || rhs == AS_private)
1624 return AS_private;
1625 if (lhs == AS_protected || rhs == AS_protected)
1626 return AS_protected;
1627 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001628}
1629
Kate Stoneb9c1b512016-09-06 20:57:50 +00001630bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1631 uint32_t &bitfield_bit_size) {
1632 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001633}
1634
Kate Stoneb9c1b512016-09-06 20:57:50 +00001635bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1636 uint32_t &bitfield_bit_size) {
1637 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001638 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001639
Kate Stoneb9c1b512016-09-06 20:57:50 +00001640 if (field->isBitField()) {
1641 Expr *bit_width_expr = field->getBitWidth();
1642 if (bit_width_expr) {
1643 llvm::APSInt bit_width_apsint;
1644 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1645 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001646 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001647 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001648 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001649 }
1650 return false;
1651}
1652
1653bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1654 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001655 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001656
1657 if (!record_decl->field_empty())
1658 return true;
1659
1660 // No fields, lets check this is a CXX record and check the base classes
1661 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1662 if (cxx_record_decl) {
1663 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1664 for (base_class = cxx_record_decl->bases_begin(),
1665 base_class_end = cxx_record_decl->bases_end();
1666 base_class != base_class_end; ++base_class) {
1667 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1668 base_class->getType()->getAs<RecordType>()->getDecl());
1669 if (RecordHasFields(base_class_decl))
1670 return true;
1671 }
1672 }
1673 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001674}
1675
Greg Clayton8cf05932010-07-22 18:30:50 +00001676#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001677
Kate Stoneb9c1b512016-09-06 20:57:50 +00001678CompilerType ClangASTContext::CreateObjCClass(const char *name,
1679 DeclContext *decl_ctx,
1680 bool isForwardDecl,
1681 bool isInternal,
1682 ClangASTMetadata *metadata) {
1683 ASTContext *ast = getASTContext();
1684 assert(ast != nullptr);
1685 assert(name && name[0]);
1686 if (decl_ctx == nullptr)
1687 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001688
Kate Stoneb9c1b512016-09-06 20:57:50 +00001689 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1690 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1691 nullptr, SourceLocation(),
1692 /*isForwardDecl,*/
1693 isInternal);
1694
1695 if (decl && metadata)
1696 SetMetadata(ast, decl, *metadata);
1697
1698 return CompilerType(ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001699}
1700
Kate Stoneb9c1b512016-09-06 20:57:50 +00001701static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1702 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) ==
1703 false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001704}
1705
Greg Clayton57ee3062013-07-11 22:46:58 +00001706uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001707ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1708 bool omit_empty_base_classes) {
1709 uint32_t num_bases = 0;
1710 if (cxx_record_decl) {
1711 if (omit_empty_base_classes) {
1712 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1713 for (base_class = cxx_record_decl->bases_begin(),
1714 base_class_end = cxx_record_decl->bases_end();
1715 base_class != base_class_end; ++base_class) {
1716 // Skip empty base classes
1717 if (omit_empty_base_classes) {
1718 if (BaseSpecifierIsEmpty(base_class))
1719 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001720 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001721 ++num_bases;
1722 }
1723 } else
1724 num_bases = cxx_record_decl->getNumBases();
1725 }
1726 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001727}
1728
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001729#pragma mark Namespace Declarations
1730
1731NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001732ClangASTContext::GetUniqueNamespaceDeclaration(const char *name,
1733 DeclContext *decl_ctx) {
1734 NamespaceDecl *namespace_decl = nullptr;
1735 ASTContext *ast = getASTContext();
1736 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1737 if (decl_ctx == nullptr)
1738 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001739
Kate Stoneb9c1b512016-09-06 20:57:50 +00001740 if (name) {
1741 IdentifierInfo &identifier_info = ast->Idents.get(name);
1742 DeclarationName decl_name(&identifier_info);
1743 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1744 for (NamedDecl *decl : result) {
1745 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1746 if (namespace_decl)
1747 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001748 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001749
1750 namespace_decl =
1751 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1752 SourceLocation(), &identifier_info, nullptr);
1753
1754 decl_ctx->addDecl(namespace_decl);
1755 } else {
1756 if (decl_ctx == translation_unit_decl) {
1757 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1758 if (namespace_decl)
1759 return namespace_decl;
1760
1761 namespace_decl =
1762 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1763 SourceLocation(), nullptr, nullptr);
1764 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1765 translation_unit_decl->addDecl(namespace_decl);
1766 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1767 } else {
1768 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1769 if (parent_namespace_decl) {
1770 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1771 if (namespace_decl)
1772 return namespace_decl;
1773 namespace_decl =
1774 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1775 SourceLocation(), nullptr, nullptr);
1776 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1777 parent_namespace_decl->addDecl(namespace_decl);
1778 assert(namespace_decl ==
1779 parent_namespace_decl->getAnonymousNamespace());
1780 } else {
1781 // BAD!!!
1782 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001783 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001784 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001785#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001786 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001787#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001788 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001789}
1790
Kate Stoneb9c1b512016-09-06 20:57:50 +00001791NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1792 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) {
1793 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1794 if (ast_ctx == nullptr)
1795 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +00001796
Kate Stoneb9c1b512016-09-06 20:57:50 +00001797 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
Siva Chandra03ff5c82016-02-05 19:10:04 +00001798}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001799
Paul Hermand628cbb2015-09-15 23:44:17 +00001800clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001801ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1802 if (ctx != nullptr) {
1803 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1804 clang::SourceLocation());
1805 ctx->addDecl(decl);
1806 return decl;
1807 }
1808 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001809}
1810
Kate Stoneb9c1b512016-09-06 20:57:50 +00001811clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1812 clang::DeclContext *right,
1813 clang::DeclContext *root) {
1814 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001815 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001816
1817 std::set<clang::DeclContext *> path_left;
1818 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1819 path_left.insert(d);
1820
1821 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1822 if (path_left.find(d) != path_left.end())
1823 return d;
1824
1825 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00001826}
1827
Kate Stoneb9c1b512016-09-06 20:57:50 +00001828clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1829 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1830 if (decl_ctx != nullptr && ns_decl != nullptr) {
1831 clang::TranslationUnitDecl *translation_unit =
1832 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1833 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1834 *getASTContext(), decl_ctx, clang::SourceLocation(),
1835 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1836 clang::SourceLocation(), ns_decl,
1837 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
1838 decl_ctx->addDecl(using_decl);
1839 return using_decl;
1840 }
1841 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001842}
1843
1844clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001845ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1846 clang::NamedDecl *target) {
1847 if (current_decl_ctx != nullptr && target != nullptr) {
1848 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1849 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
1850 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1851 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1852 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
1853 target);
1854 using_decl->addShadowDecl(shadow_decl);
1855 current_decl_ctx->addDecl(using_decl);
1856 return using_decl;
1857 }
1858 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001859}
1860
Kate Stoneb9c1b512016-09-06 20:57:50 +00001861clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
1862 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
1863 if (decl_context != nullptr) {
1864 clang::VarDecl *var_decl = clang::VarDecl::Create(
1865 *getASTContext(), decl_context, clang::SourceLocation(),
1866 clang::SourceLocation(),
1867 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
1868 nullptr, clang::SC_None);
1869 var_decl->setAccess(clang::AS_public);
1870 decl_context->addDecl(var_decl);
1871 return var_decl;
1872 }
1873 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001874}
1875
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001876lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001877ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
1878 lldb::BasicType basic_type) {
1879 switch (basic_type) {
1880 case eBasicTypeVoid:
1881 return ast->VoidTy.getAsOpaquePtr();
1882 case eBasicTypeChar:
1883 return ast->CharTy.getAsOpaquePtr();
1884 case eBasicTypeSignedChar:
1885 return ast->SignedCharTy.getAsOpaquePtr();
1886 case eBasicTypeUnsignedChar:
1887 return ast->UnsignedCharTy.getAsOpaquePtr();
1888 case eBasicTypeWChar:
1889 return ast->getWCharType().getAsOpaquePtr();
1890 case eBasicTypeSignedWChar:
1891 return ast->getSignedWCharType().getAsOpaquePtr();
1892 case eBasicTypeUnsignedWChar:
1893 return ast->getUnsignedWCharType().getAsOpaquePtr();
1894 case eBasicTypeChar16:
1895 return ast->Char16Ty.getAsOpaquePtr();
1896 case eBasicTypeChar32:
1897 return ast->Char32Ty.getAsOpaquePtr();
1898 case eBasicTypeShort:
1899 return ast->ShortTy.getAsOpaquePtr();
1900 case eBasicTypeUnsignedShort:
1901 return ast->UnsignedShortTy.getAsOpaquePtr();
1902 case eBasicTypeInt:
1903 return ast->IntTy.getAsOpaquePtr();
1904 case eBasicTypeUnsignedInt:
1905 return ast->UnsignedIntTy.getAsOpaquePtr();
1906 case eBasicTypeLong:
1907 return ast->LongTy.getAsOpaquePtr();
1908 case eBasicTypeUnsignedLong:
1909 return ast->UnsignedLongTy.getAsOpaquePtr();
1910 case eBasicTypeLongLong:
1911 return ast->LongLongTy.getAsOpaquePtr();
1912 case eBasicTypeUnsignedLongLong:
1913 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1914 case eBasicTypeInt128:
1915 return ast->Int128Ty.getAsOpaquePtr();
1916 case eBasicTypeUnsignedInt128:
1917 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1918 case eBasicTypeBool:
1919 return ast->BoolTy.getAsOpaquePtr();
1920 case eBasicTypeHalf:
1921 return ast->HalfTy.getAsOpaquePtr();
1922 case eBasicTypeFloat:
1923 return ast->FloatTy.getAsOpaquePtr();
1924 case eBasicTypeDouble:
1925 return ast->DoubleTy.getAsOpaquePtr();
1926 case eBasicTypeLongDouble:
1927 return ast->LongDoubleTy.getAsOpaquePtr();
1928 case eBasicTypeFloatComplex:
1929 return ast->FloatComplexTy.getAsOpaquePtr();
1930 case eBasicTypeDoubleComplex:
1931 return ast->DoubleComplexTy.getAsOpaquePtr();
1932 case eBasicTypeLongDoubleComplex:
1933 return ast->LongDoubleComplexTy.getAsOpaquePtr();
1934 case eBasicTypeObjCID:
1935 return ast->getObjCIdType().getAsOpaquePtr();
1936 case eBasicTypeObjCClass:
1937 return ast->getObjCClassType().getAsOpaquePtr();
1938 case eBasicTypeObjCSel:
1939 return ast->getObjCSelType().getAsOpaquePtr();
1940 case eBasicTypeNullPtr:
1941 return ast->NullPtrTy.getAsOpaquePtr();
1942 default:
1943 return nullptr;
1944 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001945}
1946
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001947#pragma mark Function Types
1948
Pavel Labath1ac2b202016-08-15 14:32:32 +00001949clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00001950ClangASTContext::GetDeclarationName(const char *name,
1951 const CompilerType &function_clang_type) {
1952 if (!name || !name[0])
1953 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00001954
Kate Stoneb9c1b512016-09-06 20:57:50 +00001955 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
1956 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
1957 return DeclarationName(&getASTContext()->Idents.get(
1958 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00001959
Kate Stoneb9c1b512016-09-06 20:57:50 +00001960 // Check the number of operator parameters. Sometimes we have
1961 // seen bad DWARF that doesn't correctly describe operators and
1962 // if we try to create a method and add it to the class, clang
1963 // will assert and crash, so we need to make sure things are
1964 // acceptable.
1965 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
1966 const clang::FunctionProtoType *function_type =
1967 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
1968 if (function_type == nullptr)
1969 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00001970
Kate Stoneb9c1b512016-09-06 20:57:50 +00001971 const bool is_method = false;
1972 const unsigned int num_params = function_type->getNumParams();
1973 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1974 is_method, op_kind, num_params))
1975 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00001976
Kate Stoneb9c1b512016-09-06 20:57:50 +00001977 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00001978}
1979
Kate Stoneb9c1b512016-09-06 20:57:50 +00001980FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
1981 DeclContext *decl_ctx, const char *name,
1982 const CompilerType &function_clang_type, int storage, bool is_inline) {
1983 FunctionDecl *func_decl = nullptr;
1984 ASTContext *ast = getASTContext();
1985 if (decl_ctx == nullptr)
1986 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001987
Kate Stoneb9c1b512016-09-06 20:57:50 +00001988 const bool hasWrittenPrototype = true;
1989 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00001990
Kate Stoneb9c1b512016-09-06 20:57:50 +00001991 clang::DeclarationName declarationName =
1992 GetDeclarationName(name, function_clang_type);
1993 func_decl = FunctionDecl::Create(
1994 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
1995 ClangUtil::GetQualType(function_clang_type), nullptr,
1996 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
1997 isConstexprSpecified);
1998 if (func_decl)
1999 decl_ctx->addDecl(func_decl);
2000
Sean Callanan5e9e1992011-10-26 01:06:27 +00002001#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00002002 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002003#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00002004
2005 return func_decl;
2006}
2007
2008CompilerType ClangASTContext::CreateFunctionType(
2009 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
2010 unsigned num_args, bool is_variadic, unsigned type_quals) {
2011 if (ast == nullptr)
2012 return CompilerType(); // invalid AST
2013
2014 if (!result_type || !ClangUtil::IsClangType(result_type))
2015 return CompilerType(); // invalid return type
2016
2017 std::vector<QualType> qual_type_args;
2018 if (num_args > 0 && args == nullptr)
2019 return CompilerType(); // invalid argument array passed in
2020
2021 // Verify that all arguments are valid and the right type
2022 for (unsigned i = 0; i < num_args; ++i) {
2023 if (args[i]) {
2024 // Make sure we have a clang type in args[i] and not a type from another
2025 // language whose name might match
2026 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2027 lldbassert(is_clang_type);
2028 if (is_clang_type)
2029 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2030 else
2031 return CompilerType(); // invalid argument type (must be a clang type)
2032 } else
2033 return CompilerType(); // invalid argument type (empty)
2034 }
2035
2036 // TODO: Detect calling convention in DWARF?
2037 FunctionProtoType::ExtProtoInfo proto_info;
2038 proto_info.Variadic = is_variadic;
2039 proto_info.ExceptionSpec = EST_None;
2040 proto_info.TypeQuals = type_quals;
2041 proto_info.RefQualifier = RQ_None;
2042
2043 return CompilerType(ast,
2044 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2045 qual_type_args, proto_info));
2046}
2047
2048ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
2049 const char *name, const CompilerType &param_type, int storage) {
2050 ASTContext *ast = getASTContext();
2051 assert(ast != nullptr);
2052 return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(),
2053 SourceLocation(), SourceLocation(),
2054 name && name[0] ? &ast->Idents.get(name) : nullptr,
2055 ClangUtil::GetQualType(param_type), nullptr,
2056 (clang::StorageClass)storage, nullptr);
2057}
2058
2059void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2060 ParmVarDecl **params,
2061 unsigned num_params) {
2062 if (function_decl)
2063 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002064}
2065
Greg Claytona1e5dc82015-08-11 22:53:00 +00002066CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002067ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
2068 QualType block_type = m_ast_ap->getBlockPointerType(
2069 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002070
Kate Stoneb9c1b512016-09-06 20:57:50 +00002071 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002072}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002073
2074#pragma mark Array Types
2075
Kate Stoneb9c1b512016-09-06 20:57:50 +00002076CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2077 size_t element_count,
2078 bool is_vector) {
2079 if (element_type.IsValid()) {
2080 ASTContext *ast = getASTContext();
2081 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002082
Kate Stoneb9c1b512016-09-06 20:57:50 +00002083 if (is_vector) {
2084 return CompilerType(
2085 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2086 element_count));
2087 } else {
2088
2089 llvm::APInt ap_element_count(64, element_count);
2090 if (element_count == 0) {
2091 return CompilerType(ast, ast->getIncompleteArrayType(
2092 ClangUtil::GetQualType(element_type),
2093 clang::ArrayType::Normal, 0));
2094 } else {
2095 return CompilerType(
2096 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2097 ap_element_count,
2098 clang::ArrayType::Normal, 0));
2099 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002100 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002101 }
2102 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002103}
2104
Kate Stoneb9c1b512016-09-06 20:57:50 +00002105CompilerType ClangASTContext::CreateStructForIdentifier(
2106 const ConstString &type_name,
2107 const std::initializer_list<std::pair<const char *, CompilerType>>
2108 &type_fields,
2109 bool packed) {
2110 CompilerType type;
2111 if (!type_name.IsEmpty() &&
2112 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2113 .IsValid()) {
2114 lldbassert("Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002115 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002116 }
2117
2118 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2119 clang::TTK_Struct, lldb::eLanguageTypeC);
2120 StartTagDeclarationDefinition(type);
2121 for (const auto &field : type_fields)
2122 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2123 0);
2124 if (packed)
2125 SetIsPacked(type);
2126 CompleteTagDeclarationDefinition(type);
2127 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002128}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002129
Kate Stoneb9c1b512016-09-06 20:57:50 +00002130CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
2131 const ConstString &type_name,
2132 const std::initializer_list<std::pair<const char *, CompilerType>>
2133 &type_fields,
2134 bool packed) {
2135 CompilerType type;
2136 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2137 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002138
Kate Stoneb9c1b512016-09-06 20:57:50 +00002139 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002140}
2141
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002142#pragma mark Enumeration Types
2143
Greg Claytona1e5dc82015-08-11 22:53:00 +00002144CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002145ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2146 const Declaration &decl,
2147 const CompilerType &integer_clang_type) {
2148 // TODO: Do something intelligent with the Declaration object passed in
2149 // like maybe filling in the SourceLocation with it...
2150 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002151
Kate Stoneb9c1b512016-09-06 20:57:50 +00002152 // TODO: ask about these...
2153 // const bool IsScoped = false;
2154 // const bool IsFixed = false;
2155
2156 EnumDecl *enum_decl = EnumDecl::Create(
2157 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2158 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
2159 false, // IsScoped
2160 false, // IsScopedUsingClassTag
2161 false); // IsFixed
2162
2163 if (enum_decl) {
2164 // TODO: check if we should be setting the promotion type too?
2165 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2166
2167 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2168
2169 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2170 }
2171 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002172}
2173
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002174// Disable this for now since I can't seem to get a nicely formatted float
2175// out of the APFloat class without just getting the float, double or quad
2176// and then using a formatted print on it which defeats the purpose. We ideally
2177// would like to get perfect string values for any kind of float semantics
2178// so we can support remote targets. The code below also requires a patch to
2179// llvm::APInt.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002180// bool
2181// ClangASTContext::ConvertFloatValueToString (ASTContext *ast,
2182// lldb::opaque_compiler_type_t clang_type, const uint8_t* bytes, size_t
2183// byte_size, int apint_byte_order, std::string &float_str)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002184//{
2185// uint32_t count = 0;
2186// bool is_complex = false;
2187// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
2188// {
2189// unsigned num_bytes_per_float = byte_size / count;
2190// unsigned num_bits_per_float = num_bytes_per_float * 8;
2191//
2192// float_str.clear();
2193// uint32_t i;
2194// for (i=0; i<count; i++)
2195// {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002196// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float,
2197// (APInt::ByteOrder)apint_byte_order);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002198// bool is_ieee = false;
2199// APFloat ap_float(ap_int, is_ieee);
2200// char s[1024];
2201// unsigned int hex_digits = 0;
2202// bool upper_case = false;
2203//
Kate Stoneb9c1b512016-09-06 20:57:50 +00002204// if (ap_float.convertToHexString(s, hex_digits, upper_case,
2205// APFloat::rmNearestTiesToEven) > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002206// {
2207// if (i > 0)
2208// float_str.append(", ");
2209// float_str.append(s);
2210// if (i == 1 && is_complex)
2211// float_str.append(1, 'i');
2212// }
2213// }
2214// return !float_str.empty();
2215// }
2216// return false;
2217//}
2218
Kate Stoneb9c1b512016-09-06 20:57:50 +00002219CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2220 size_t bit_size,
2221 bool is_signed) {
2222 if (ast) {
2223 if (is_signed) {
2224 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2225 return CompilerType(ast, ast->SignedCharTy);
2226
2227 if (bit_size == ast->getTypeSize(ast->ShortTy))
2228 return CompilerType(ast, ast->ShortTy);
2229
2230 if (bit_size == ast->getTypeSize(ast->IntTy))
2231 return CompilerType(ast, ast->IntTy);
2232
2233 if (bit_size == ast->getTypeSize(ast->LongTy))
2234 return CompilerType(ast, ast->LongTy);
2235
2236 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2237 return CompilerType(ast, ast->LongLongTy);
2238
2239 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2240 return CompilerType(ast, ast->Int128Ty);
2241 } else {
2242 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2243 return CompilerType(ast, ast->UnsignedCharTy);
2244
2245 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2246 return CompilerType(ast, ast->UnsignedShortTy);
2247
2248 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2249 return CompilerType(ast, ast->UnsignedIntTy);
2250
2251 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2252 return CompilerType(ast, ast->UnsignedLongTy);
2253
2254 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2255 return CompilerType(ast, ast->UnsignedLongLongTy);
2256
2257 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2258 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002259 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002260 }
2261 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002262}
2263
Kate Stoneb9c1b512016-09-06 20:57:50 +00002264CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2265 bool is_signed) {
2266 if (ast)
2267 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2268 is_signed);
2269 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002270}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002271
Kate Stoneb9c1b512016-09-06 20:57:50 +00002272void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2273 if (decl_ctx) {
2274 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002275
Kate Stoneb9c1b512016-09-06 20:57:50 +00002276 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2277 if (named_decl) {
2278 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2279 named_decl->getDeclName().getAsString().c_str());
2280 } else {
2281 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002282 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002283 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002284}
2285
Kate Stoneb9c1b512016-09-06 20:57:50 +00002286void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2287 if (decl == nullptr)
2288 return;
2289 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002290
Kate Stoneb9c1b512016-09-06 20:57:50 +00002291 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2292 if (record_decl) {
2293 printf("%20s: %s%s\n", decl->getDeclKindName(),
2294 record_decl->getDeclName().getAsString().c_str(),
2295 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002296
Kate Stoneb9c1b512016-09-06 20:57:50 +00002297 } else {
2298 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2299 if (named_decl) {
2300 printf("%20s: %s\n", decl->getDeclKindName(),
2301 named_decl->getDeclName().getAsString().c_str());
2302 } else {
2303 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002304 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002305 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002306}
2307
Kate Stoneb9c1b512016-09-06 20:57:50 +00002308bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2309 clang::Decl *rhs_decl) {
2310 if (lhs_decl && rhs_decl) {
2311 //----------------------------------------------------------------------
2312 // Make sure the decl kinds match first
2313 //----------------------------------------------------------------------
2314 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2315 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002316
Kate Stoneb9c1b512016-09-06 20:57:50 +00002317 if (lhs_decl_kind == rhs_decl_kind) {
2318 //------------------------------------------------------------------
2319 // Now check that the decl contexts kinds are all equivalent
2320 // before we have to check any names of the decl contexts...
2321 //------------------------------------------------------------------
2322 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2323 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2324 if (lhs_decl_ctx && rhs_decl_ctx) {
2325 while (1) {
2326 if (lhs_decl_ctx && rhs_decl_ctx) {
2327 const clang::Decl::Kind lhs_decl_ctx_kind =
2328 lhs_decl_ctx->getDeclKind();
2329 const clang::Decl::Kind rhs_decl_ctx_kind =
2330 rhs_decl_ctx->getDeclKind();
2331 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2332 lhs_decl_ctx = lhs_decl_ctx->getParent();
2333 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002334
Kate Stoneb9c1b512016-09-06 20:57:50 +00002335 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2336 break;
2337 } else
2338 return false;
2339 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002340 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002341 }
2342
2343 //--------------------------------------------------------------
2344 // Now make sure the name of the decls match
2345 //--------------------------------------------------------------
2346 clang::NamedDecl *lhs_named_decl =
2347 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2348 clang::NamedDecl *rhs_named_decl =
2349 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2350 if (lhs_named_decl && rhs_named_decl) {
2351 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2352 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2353 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2354 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2355 return false;
2356 } else
Greg Claytona2721472011-06-25 00:44:06 +00002357 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002358 } else
2359 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002360
Kate Stoneb9c1b512016-09-06 20:57:50 +00002361 //--------------------------------------------------------------
2362 // We know that the decl context kinds all match, so now we need
2363 // to make sure the names match as well
2364 //--------------------------------------------------------------
2365 lhs_decl_ctx = lhs_decl->getDeclContext();
2366 rhs_decl_ctx = rhs_decl->getDeclContext();
2367 while (1) {
2368 switch (lhs_decl_ctx->getDeclKind()) {
2369 case clang::Decl::TranslationUnit:
2370 // We don't care about the translation unit names
2371 return true;
2372 default: {
2373 clang::NamedDecl *lhs_named_decl =
2374 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2375 clang::NamedDecl *rhs_named_decl =
2376 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2377 if (lhs_named_decl && rhs_named_decl) {
2378 clang::DeclarationName lhs_decl_name =
2379 lhs_named_decl->getDeclName();
2380 clang::DeclarationName rhs_decl_name =
2381 rhs_named_decl->getDeclName();
2382 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2383 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2384 return false;
2385 } else
2386 return false;
2387 } else
2388 return false;
2389 } break;
2390 }
2391 lhs_decl_ctx = lhs_decl_ctx->getParent();
2392 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002393 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002394 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002395 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002396 }
2397 return false;
2398}
2399bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2400 clang::Decl *decl) {
2401 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002402 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002403
Kate Stoneb9c1b512016-09-06 20:57:50 +00002404 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002405
Kate Stoneb9c1b512016-09-06 20:57:50 +00002406 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002407 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002408
2409 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2410 if (tag_decl->isCompleteDefinition())
2411 return true;
2412
2413 if (!tag_decl->hasExternalLexicalStorage())
2414 return false;
2415
2416 ast_source->CompleteType(tag_decl);
2417
2418 return !tag_decl->getTypeForDecl()->isIncompleteType();
2419 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2420 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2421 if (objc_interface_decl->getDefinition())
2422 return true;
2423
2424 if (!objc_interface_decl->hasExternalLexicalStorage())
2425 return false;
2426
2427 ast_source->CompleteType(objc_interface_decl);
2428
2429 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2430 } else {
2431 return false;
2432 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002433}
2434
Kate Stoneb9c1b512016-09-06 20:57:50 +00002435void ClangASTContext::SetMetadataAsUserID(const void *object,
2436 user_id_t user_id) {
2437 ClangASTMetadata meta_data;
2438 meta_data.SetUserID(user_id);
2439 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002440}
2441
Kate Stoneb9c1b512016-09-06 20:57:50 +00002442void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2443 ClangASTMetadata &metadata) {
2444 ClangExternalASTSourceCommon *external_source =
2445 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2446
2447 if (external_source)
2448 external_source->SetMetadata(object, metadata);
2449}
2450
2451ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2452 const void *object) {
2453 ClangExternalASTSourceCommon *external_source =
2454 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2455
2456 if (external_source && external_source->HasMetadata(object))
2457 return external_source->GetMetadata(object);
2458 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002459 return nullptr;
2460}
2461
Kate Stoneb9c1b512016-09-06 20:57:50 +00002462clang::DeclContext *
2463ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2464 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2465}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002466
Kate Stoneb9c1b512016-09-06 20:57:50 +00002467clang::DeclContext *
2468ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2469 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2470}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002471
Kate Stoneb9c1b512016-09-06 20:57:50 +00002472bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2473 int kind) const {
2474 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2475 if (clang_type) {
2476 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2477 if (tag_type) {
2478 clang::TagDecl *tag_decl =
2479 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2480 if (tag_decl) {
2481 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2482 return true;
2483 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002484 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002485 }
2486 return false;
2487}
2488
2489bool ClangASTContext::SetDefaultAccessForRecordFields(
2490 clang::RecordDecl *record_decl, int default_accessibility,
2491 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2492 if (record_decl) {
2493 uint32_t field_idx;
2494 clang::RecordDecl::field_iterator field, field_end;
2495 for (field = record_decl->field_begin(),
2496 field_end = record_decl->field_end(), field_idx = 0;
2497 field != field_end; ++field, ++field_idx) {
2498 // If no accessibility was assigned, assign the correct one
2499 if (field_idx < num_assigned_accessibilities &&
2500 assigned_accessibilities[field_idx] == clang::AS_none)
2501 field->setAccess((clang::AccessSpecifier)default_accessibility);
2502 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002503 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002504 }
2505 return false;
2506}
2507
2508clang::DeclContext *
2509ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2510 return GetDeclContextForType(ClangUtil::GetQualType(type));
2511}
2512
2513clang::DeclContext *
2514ClangASTContext::GetDeclContextForType(clang::QualType type) {
2515 if (type.isNull())
2516 return nullptr;
2517
2518 clang::QualType qual_type = type.getCanonicalType();
2519 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2520 switch (type_class) {
2521 case clang::Type::ObjCInterface:
2522 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2523 ->getInterface();
2524 case clang::Type::ObjCObjectPointer:
2525 return GetDeclContextForType(
2526 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2527 ->getPointeeType());
2528 case clang::Type::Record:
2529 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2530 case clang::Type::Enum:
2531 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2532 case clang::Type::Typedef:
2533 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2534 ->getDecl()
2535 ->getUnderlyingType());
2536 case clang::Type::Auto:
2537 return GetDeclContextForType(
2538 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2539 case clang::Type::Elaborated:
2540 return GetDeclContextForType(
2541 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2542 case clang::Type::Paren:
2543 return GetDeclContextForType(
2544 llvm::cast<clang::ParenType>(qual_type)->desugar());
2545 default:
2546 break;
2547 }
2548 // No DeclContext in this type...
2549 return nullptr;
2550}
2551
2552static bool GetCompleteQualType(clang::ASTContext *ast,
2553 clang::QualType qual_type,
2554 bool allow_completion = true) {
2555 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2556 switch (type_class) {
2557 case clang::Type::ConstantArray:
2558 case clang::Type::IncompleteArray:
2559 case clang::Type::VariableArray: {
2560 const clang::ArrayType *array_type =
2561 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2562
2563 if (array_type)
2564 return GetCompleteQualType(ast, array_type->getElementType(),
2565 allow_completion);
2566 } break;
2567 case clang::Type::Record: {
2568 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2569 if (cxx_record_decl) {
2570 if (cxx_record_decl->hasExternalLexicalStorage()) {
2571 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2572 const bool fields_loaded =
2573 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2574 if (is_complete && fields_loaded)
2575 return true;
2576
2577 if (!allow_completion)
2578 return false;
2579
2580 // Call the field_begin() accessor to for it to use the external source
2581 // to load the fields...
2582 clang::ExternalASTSource *external_ast_source =
2583 ast->getExternalSource();
2584 if (external_ast_source) {
2585 external_ast_source->CompleteType(cxx_record_decl);
2586 if (cxx_record_decl->isCompleteDefinition()) {
2587 cxx_record_decl->field_begin();
2588 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2589 }
2590 }
2591 }
2592 }
2593 const clang::TagType *tag_type =
2594 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2595 return !tag_type->isIncompleteType();
2596 } break;
2597
2598 case clang::Type::Enum: {
2599 const clang::TagType *tag_type =
2600 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2601 if (tag_type) {
2602 clang::TagDecl *tag_decl = tag_type->getDecl();
2603 if (tag_decl) {
2604 if (tag_decl->getDefinition())
2605 return true;
2606
2607 if (!allow_completion)
2608 return false;
2609
2610 if (tag_decl->hasExternalLexicalStorage()) {
2611 if (ast) {
2612 clang::ExternalASTSource *external_ast_source =
2613 ast->getExternalSource();
2614 if (external_ast_source) {
2615 external_ast_source->CompleteType(tag_decl);
2616 return !tag_type->isIncompleteType();
2617 }
2618 }
2619 }
2620 return false;
2621 }
2622 }
2623
2624 } break;
2625 case clang::Type::ObjCObject:
2626 case clang::Type::ObjCInterface: {
2627 const clang::ObjCObjectType *objc_class_type =
2628 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2629 if (objc_class_type) {
2630 clang::ObjCInterfaceDecl *class_interface_decl =
2631 objc_class_type->getInterface();
2632 // We currently can't complete objective C types through the newly added
2633 // ASTContext
2634 // because it only supports TagDecl objects right now...
2635 if (class_interface_decl) {
2636 if (class_interface_decl->getDefinition())
2637 return true;
2638
2639 if (!allow_completion)
2640 return false;
2641
2642 if (class_interface_decl->hasExternalLexicalStorage()) {
2643 if (ast) {
2644 clang::ExternalASTSource *external_ast_source =
2645 ast->getExternalSource();
2646 if (external_ast_source) {
2647 external_ast_source->CompleteType(class_interface_decl);
2648 return !objc_class_type->isIncompleteType();
2649 }
2650 }
2651 }
2652 return false;
2653 }
2654 }
2655 } break;
2656
2657 case clang::Type::Typedef:
2658 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2659 ->getDecl()
2660 ->getUnderlyingType(),
2661 allow_completion);
2662
2663 case clang::Type::Auto:
2664 return GetCompleteQualType(
2665 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2666 allow_completion);
2667
2668 case clang::Type::Elaborated:
2669 return GetCompleteQualType(
2670 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2671 allow_completion);
2672
2673 case clang::Type::Paren:
2674 return GetCompleteQualType(
2675 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2676 allow_completion);
2677
2678 case clang::Type::Attributed:
2679 return GetCompleteQualType(
2680 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2681 allow_completion);
2682
2683 default:
2684 break;
2685 }
2686
2687 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002688}
2689
2690static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002691ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2692 switch (access) {
2693 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002694 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002695 case eAccessPublic:
2696 return clang::ObjCIvarDecl::Public;
2697 case eAccessPrivate:
2698 return clang::ObjCIvarDecl::Private;
2699 case eAccessProtected:
2700 return clang::ObjCIvarDecl::Protected;
2701 case eAccessPackage:
2702 return clang::ObjCIvarDecl::Package;
2703 }
2704 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002705}
2706
Greg Claytond8d4a572015-08-11 21:38:15 +00002707//----------------------------------------------------------------------
2708// Tests
2709//----------------------------------------------------------------------
2710
Kate Stoneb9c1b512016-09-06 20:57:50 +00002711bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2712 clang::QualType qual_type(GetCanonicalQualType(type));
2713
2714 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2715 switch (type_class) {
2716 case clang::Type::IncompleteArray:
2717 case clang::Type::VariableArray:
2718 case clang::Type::ConstantArray:
2719 case clang::Type::ExtVector:
2720 case clang::Type::Vector:
2721 case clang::Type::Record:
2722 case clang::Type::ObjCObject:
2723 case clang::Type::ObjCInterface:
2724 return true;
2725 case clang::Type::Auto:
2726 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2727 ->getDeducedType()
2728 .getAsOpaquePtr());
2729 case clang::Type::Elaborated:
2730 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2731 ->getNamedType()
2732 .getAsOpaquePtr());
2733 case clang::Type::Typedef:
2734 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2735 ->getDecl()
2736 ->getUnderlyingType()
2737 .getAsOpaquePtr());
2738 case clang::Type::Paren:
2739 return IsAggregateType(
2740 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2741 default:
2742 break;
2743 }
2744 // The clang type does have a value
2745 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002746}
2747
Kate Stoneb9c1b512016-09-06 20:57:50 +00002748bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2749 clang::QualType qual_type(GetCanonicalQualType(type));
2750
2751 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2752 switch (type_class) {
2753 case clang::Type::Record: {
2754 if (const clang::RecordType *record_type =
2755 llvm::dyn_cast_or_null<clang::RecordType>(
2756 qual_type.getTypePtrOrNull())) {
2757 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2758 return record_decl->isAnonymousStructOrUnion();
2759 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002760 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002761 break;
2762 }
2763 case clang::Type::Auto:
2764 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2765 ->getDeducedType()
2766 .getAsOpaquePtr());
2767 case clang::Type::Elaborated:
2768 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2769 ->getNamedType()
2770 .getAsOpaquePtr());
2771 case clang::Type::Typedef:
2772 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2773 ->getDecl()
2774 ->getUnderlyingType()
2775 .getAsOpaquePtr());
2776 case clang::Type::Paren:
2777 return IsAnonymousType(
2778 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2779 default:
2780 break;
2781 }
2782 // The clang type does have a value
2783 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002784}
2785
Kate Stoneb9c1b512016-09-06 20:57:50 +00002786bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2787 CompilerType *element_type_ptr,
2788 uint64_t *size, bool *is_incomplete) {
2789 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002790
Kate Stoneb9c1b512016-09-06 20:57:50 +00002791 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2792 switch (type_class) {
2793 default:
2794 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002795
Kate Stoneb9c1b512016-09-06 20:57:50 +00002796 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002797 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002798 element_type_ptr->SetCompilerType(
2799 getASTContext(),
2800 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002801 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002802 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2803 ->getSize()
2804 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002805 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002806 *is_incomplete = false;
2807 return true;
2808
2809 case clang::Type::IncompleteArray:
2810 if (element_type_ptr)
2811 element_type_ptr->SetCompilerType(
2812 getASTContext(),
2813 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2814 if (size)
2815 *size = 0;
2816 if (is_incomplete)
2817 *is_incomplete = true;
2818 return true;
2819
2820 case clang::Type::VariableArray:
2821 if (element_type_ptr)
2822 element_type_ptr->SetCompilerType(
2823 getASTContext(),
2824 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2825 if (size)
2826 *size = 0;
2827 if (is_incomplete)
2828 *is_incomplete = false;
2829 return true;
2830
2831 case clang::Type::DependentSizedArray:
2832 if (element_type_ptr)
2833 element_type_ptr->SetCompilerType(
2834 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2835 ->getElementType());
2836 if (size)
2837 *size = 0;
2838 if (is_incomplete)
2839 *is_incomplete = false;
2840 return true;
2841
2842 case clang::Type::Typedef:
2843 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2844 ->getDecl()
2845 ->getUnderlyingType()
2846 .getAsOpaquePtr(),
2847 element_type_ptr, size, is_incomplete);
2848 case clang::Type::Auto:
2849 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2850 ->getDeducedType()
2851 .getAsOpaquePtr(),
2852 element_type_ptr, size, is_incomplete);
2853 case clang::Type::Elaborated:
2854 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2855 ->getNamedType()
2856 .getAsOpaquePtr(),
2857 element_type_ptr, size, is_incomplete);
2858 case clang::Type::Paren:
2859 return IsArrayType(
2860 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2861 element_type_ptr, size, is_incomplete);
2862 }
2863 if (element_type_ptr)
2864 element_type_ptr->Clear();
2865 if (size)
2866 *size = 0;
2867 if (is_incomplete)
2868 *is_incomplete = false;
2869 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002870}
2871
Kate Stoneb9c1b512016-09-06 20:57:50 +00002872bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2873 CompilerType *element_type, uint64_t *size) {
2874 clang::QualType qual_type(GetCanonicalQualType(type));
2875
2876 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2877 switch (type_class) {
2878 case clang::Type::Vector: {
2879 const clang::VectorType *vector_type =
2880 qual_type->getAs<clang::VectorType>();
2881 if (vector_type) {
2882 if (size)
2883 *size = vector_type->getNumElements();
2884 if (element_type)
2885 *element_type =
2886 CompilerType(getASTContext(), vector_type->getElementType());
2887 }
2888 return true;
2889 } break;
2890 case clang::Type::ExtVector: {
2891 const clang::ExtVectorType *ext_vector_type =
2892 qual_type->getAs<clang::ExtVectorType>();
2893 if (ext_vector_type) {
2894 if (size)
2895 *size = ext_vector_type->getNumElements();
2896 if (element_type)
2897 *element_type =
2898 CompilerType(getASTContext(), ext_vector_type->getElementType());
2899 }
2900 return true;
2901 }
2902 default:
2903 break;
2904 }
2905 return false;
2906}
2907
2908bool ClangASTContext::IsRuntimeGeneratedType(
2909 lldb::opaque_compiler_type_t type) {
2910 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
2911 ->GetDeclContextForType(GetQualType(type));
2912 if (!decl_ctx)
2913 return false;
2914
2915 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2916 return false;
2917
2918 clang::ObjCInterfaceDecl *result_iface_decl =
2919 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2920
2921 ClangASTMetadata *ast_metadata =
2922 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2923 if (!ast_metadata)
2924 return false;
2925 return (ast_metadata->GetISAPtr() != 0);
2926}
2927
2928bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
2929 return GetQualType(type).getUnqualifiedType()->isCharType();
2930}
2931
2932bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
2933 const bool allow_completion = false;
2934 return GetCompleteQualType(getASTContext(), GetQualType(type),
2935 allow_completion);
2936}
2937
2938bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
2939 return GetQualType(type).isConstQualified();
2940}
2941
2942bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
2943 uint32_t &length) {
2944 CompilerType pointee_or_element_clang_type;
2945 length = 0;
2946 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2947
2948 if (!pointee_or_element_clang_type.IsValid())
2949 return false;
2950
2951 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2952 if (pointee_or_element_clang_type.IsCharType()) {
2953 if (type_flags.Test(eTypeIsArray)) {
2954 // We know the size of the array and it could be a C string
2955 // since it is an array of characters
2956 length = llvm::cast<clang::ConstantArrayType>(
2957 GetCanonicalQualType(type).getTypePtr())
2958 ->getSize()
2959 .getLimitedValue();
2960 }
2961 return true;
2962 }
2963 }
2964 return false;
2965}
2966
2967bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
2968 bool *is_variadic_ptr) {
2969 if (type) {
2970 clang::QualType qual_type(GetCanonicalQualType(type));
2971
2972 if (qual_type->isFunctionType()) {
2973 if (is_variadic_ptr) {
2974 const clang::FunctionProtoType *function_proto_type =
2975 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2976 if (function_proto_type)
2977 *is_variadic_ptr = function_proto_type->isVariadic();
2978 else
2979 *is_variadic_ptr = false;
2980 }
2981 return true;
2982 }
2983
Greg Claytond8d4a572015-08-11 21:38:15 +00002984 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002985 switch (type_class) {
2986 default:
2987 break;
2988 case clang::Type::Typedef:
2989 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
2990 ->getDecl()
2991 ->getUnderlyingType()
2992 .getAsOpaquePtr(),
2993 nullptr);
2994 case clang::Type::Auto:
2995 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
2996 ->getDeducedType()
2997 .getAsOpaquePtr(),
2998 nullptr);
2999 case clang::Type::Elaborated:
3000 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3001 ->getNamedType()
3002 .getAsOpaquePtr(),
3003 nullptr);
3004 case clang::Type::Paren:
3005 return IsFunctionType(
3006 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3007 nullptr);
3008 case clang::Type::LValueReference:
3009 case clang::Type::RValueReference: {
3010 const clang::ReferenceType *reference_type =
3011 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3012 if (reference_type)
3013 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3014 nullptr);
3015 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00003016 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003017 }
3018 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003019}
3020
3021// Used to detect "Homogeneous Floating-point Aggregates"
3022uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003023ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3024 CompilerType *base_type_ptr) {
3025 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003026 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003027
3028 clang::QualType qual_type(GetCanonicalQualType(type));
3029 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3030 switch (type_class) {
3031 case clang::Type::Record:
3032 if (GetCompleteType(type)) {
3033 const clang::CXXRecordDecl *cxx_record_decl =
3034 qual_type->getAsCXXRecordDecl();
3035 if (cxx_record_decl) {
3036 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3037 return 0;
3038 }
3039 const clang::RecordType *record_type =
3040 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3041 if (record_type) {
3042 const clang::RecordDecl *record_decl = record_type->getDecl();
3043 if (record_decl) {
3044 // We are looking for a structure that contains only floating point
3045 // types
3046 clang::RecordDecl::field_iterator field_pos,
3047 field_end = record_decl->field_end();
3048 uint32_t num_fields = 0;
3049 bool is_hva = false;
3050 bool is_hfa = false;
3051 clang::QualType base_qual_type;
3052 uint64_t base_bitwidth = 0;
3053 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3054 ++field_pos) {
3055 clang::QualType field_qual_type = field_pos->getType();
3056 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3057 if (field_qual_type->isFloatingType()) {
3058 if (field_qual_type->isComplexType())
3059 return 0;
3060 else {
3061 if (num_fields == 0)
3062 base_qual_type = field_qual_type;
3063 else {
3064 if (is_hva)
3065 return 0;
3066 is_hfa = true;
3067 if (field_qual_type.getTypePtr() !=
3068 base_qual_type.getTypePtr())
3069 return 0;
3070 }
3071 }
3072 } else if (field_qual_type->isVectorType() ||
3073 field_qual_type->isExtVectorType()) {
3074 if (num_fields == 0) {
3075 base_qual_type = field_qual_type;
3076 base_bitwidth = field_bitwidth;
3077 } else {
3078 if (is_hfa)
3079 return 0;
3080 is_hva = true;
3081 if (base_bitwidth != field_bitwidth)
3082 return 0;
3083 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3084 return 0;
3085 }
3086 } else
3087 return 0;
3088 ++num_fields;
3089 }
3090 if (base_type_ptr)
3091 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3092 return num_fields;
3093 }
3094 }
3095 }
3096 break;
3097
3098 case clang::Type::Typedef:
3099 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3100 ->getDecl()
3101 ->getUnderlyingType()
3102 .getAsOpaquePtr(),
3103 base_type_ptr);
3104
3105 case clang::Type::Auto:
3106 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3107 ->getDeducedType()
3108 .getAsOpaquePtr(),
3109 base_type_ptr);
3110
3111 case clang::Type::Elaborated:
3112 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3113 ->getNamedType()
3114 .getAsOpaquePtr(),
3115 base_type_ptr);
3116 default:
3117 break;
3118 }
3119 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003120}
3121
Kate Stoneb9c1b512016-09-06 20:57:50 +00003122size_t ClangASTContext::GetNumberOfFunctionArguments(
3123 lldb::opaque_compiler_type_t type) {
3124 if (type) {
3125 clang::QualType qual_type(GetCanonicalQualType(type));
3126 const clang::FunctionProtoType *func =
3127 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3128 if (func)
3129 return func->getNumParams();
3130 }
3131 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003132}
3133
Greg Claytona1e5dc82015-08-11 22:53:00 +00003134CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003135ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3136 const size_t index) {
3137 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003138 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003139 const clang::FunctionProtoType *func =
3140 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3141 if (func) {
3142 if (index < func->getNumParams())
3143 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003144 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003145 }
3146 return CompilerType();
3147}
3148
3149bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3150 if (type) {
3151 clang::QualType qual_type(GetCanonicalQualType(type));
3152
3153 if (qual_type->isFunctionPointerType())
3154 return true;
3155
3156 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3157 switch (type_class) {
3158 default:
3159 break;
3160 case clang::Type::Typedef:
3161 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3162 ->getDecl()
3163 ->getUnderlyingType()
3164 .getAsOpaquePtr());
3165 case clang::Type::Auto:
3166 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3167 ->getDeducedType()
3168 .getAsOpaquePtr());
3169 case clang::Type::Elaborated:
3170 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3171 ->getNamedType()
3172 .getAsOpaquePtr());
3173 case clang::Type::Paren:
3174 return IsFunctionPointerType(
3175 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3176
3177 case clang::Type::LValueReference:
3178 case clang::Type::RValueReference: {
3179 const clang::ReferenceType *reference_type =
3180 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3181 if (reference_type)
3182 return IsFunctionPointerType(
3183 reference_type->getPointeeType().getAsOpaquePtr());
3184 } break;
3185 }
3186 }
3187 return false;
3188}
3189
3190bool ClangASTContext::IsBlockPointerType(
3191 lldb::opaque_compiler_type_t type,
3192 CompilerType *function_pointer_type_ptr) {
3193 if (type) {
3194 clang::QualType qual_type(GetCanonicalQualType(type));
3195
3196 if (qual_type->isBlockPointerType()) {
3197 if (function_pointer_type_ptr) {
3198 const clang::BlockPointerType *block_pointer_type =
3199 qual_type->getAs<clang::BlockPointerType>();
3200 QualType pointee_type = block_pointer_type->getPointeeType();
3201 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3202 *function_pointer_type_ptr =
3203 CompilerType(getASTContext(), function_pointer_type);
3204 }
3205 return true;
3206 }
3207
3208 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3209 switch (type_class) {
3210 default:
3211 break;
3212 case clang::Type::Typedef:
3213 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3214 ->getDecl()
3215 ->getUnderlyingType()
3216 .getAsOpaquePtr(),
3217 function_pointer_type_ptr);
3218 case clang::Type::Auto:
3219 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3220 ->getDeducedType()
3221 .getAsOpaquePtr(),
3222 function_pointer_type_ptr);
3223 case clang::Type::Elaborated:
3224 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3225 ->getNamedType()
3226 .getAsOpaquePtr(),
3227 function_pointer_type_ptr);
3228 case clang::Type::Paren:
3229 return IsBlockPointerType(
3230 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3231 function_pointer_type_ptr);
3232
3233 case clang::Type::LValueReference:
3234 case clang::Type::RValueReference: {
3235 const clang::ReferenceType *reference_type =
3236 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3237 if (reference_type)
3238 return IsBlockPointerType(
3239 reference_type->getPointeeType().getAsOpaquePtr(),
3240 function_pointer_type_ptr);
3241 } break;
3242 }
3243 }
3244 return false;
3245}
3246
3247bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3248 bool &is_signed) {
3249 if (!type)
3250 return false;
3251
3252 clang::QualType qual_type(GetCanonicalQualType(type));
3253 const clang::BuiltinType *builtin_type =
3254 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3255
3256 if (builtin_type) {
3257 if (builtin_type->isInteger()) {
3258 is_signed = builtin_type->isSignedInteger();
3259 return true;
3260 }
3261 }
3262
3263 return false;
3264}
3265
3266bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3267 bool &is_signed) {
3268 if (type) {
3269 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3270 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3271
3272 if (enum_type) {
3273 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3274 is_signed);
3275 return true;
3276 }
3277 }
3278
3279 return false;
3280}
3281
3282bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3283 CompilerType *pointee_type) {
3284 if (type) {
3285 clang::QualType qual_type(GetCanonicalQualType(type));
3286 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3287 switch (type_class) {
3288 case clang::Type::Builtin:
3289 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3290 default:
3291 break;
3292 case clang::BuiltinType::ObjCId:
3293 case clang::BuiltinType::ObjCClass:
3294 return true;
3295 }
3296 return false;
3297 case clang::Type::ObjCObjectPointer:
3298 if (pointee_type)
3299 pointee_type->SetCompilerType(
3300 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3301 ->getPointeeType());
3302 return true;
3303 case clang::Type::BlockPointer:
3304 if (pointee_type)
3305 pointee_type->SetCompilerType(
3306 getASTContext(),
3307 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3308 return true;
3309 case clang::Type::Pointer:
3310 if (pointee_type)
3311 pointee_type->SetCompilerType(
3312 getASTContext(),
3313 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3314 return true;
3315 case clang::Type::MemberPointer:
3316 if (pointee_type)
3317 pointee_type->SetCompilerType(
3318 getASTContext(),
3319 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3320 return true;
3321 case clang::Type::Typedef:
3322 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3323 ->getDecl()
3324 ->getUnderlyingType()
3325 .getAsOpaquePtr(),
3326 pointee_type);
3327 case clang::Type::Auto:
3328 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3329 ->getDeducedType()
3330 .getAsOpaquePtr(),
3331 pointee_type);
3332 case clang::Type::Elaborated:
3333 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3334 ->getNamedType()
3335 .getAsOpaquePtr(),
3336 pointee_type);
3337 case clang::Type::Paren:
3338 return IsPointerType(
3339 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3340 pointee_type);
3341 default:
3342 break;
3343 }
3344 }
3345 if (pointee_type)
3346 pointee_type->Clear();
3347 return false;
3348}
3349
3350bool ClangASTContext::IsPointerOrReferenceType(
3351 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3352 if (type) {
3353 clang::QualType qual_type(GetCanonicalQualType(type));
3354 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3355 switch (type_class) {
3356 case clang::Type::Builtin:
3357 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3358 default:
3359 break;
3360 case clang::BuiltinType::ObjCId:
3361 case clang::BuiltinType::ObjCClass:
3362 return true;
3363 }
3364 return false;
3365 case clang::Type::ObjCObjectPointer:
3366 if (pointee_type)
3367 pointee_type->SetCompilerType(
3368 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3369 ->getPointeeType());
3370 return true;
3371 case clang::Type::BlockPointer:
3372 if (pointee_type)
3373 pointee_type->SetCompilerType(
3374 getASTContext(),
3375 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3376 return true;
3377 case clang::Type::Pointer:
3378 if (pointee_type)
3379 pointee_type->SetCompilerType(
3380 getASTContext(),
3381 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3382 return true;
3383 case clang::Type::MemberPointer:
3384 if (pointee_type)
3385 pointee_type->SetCompilerType(
3386 getASTContext(),
3387 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3388 return true;
3389 case clang::Type::LValueReference:
3390 if (pointee_type)
3391 pointee_type->SetCompilerType(
3392 getASTContext(),
3393 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3394 return true;
3395 case clang::Type::RValueReference:
3396 if (pointee_type)
3397 pointee_type->SetCompilerType(
3398 getASTContext(),
3399 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3400 return true;
3401 case clang::Type::Typedef:
3402 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3403 ->getDecl()
3404 ->getUnderlyingType()
3405 .getAsOpaquePtr(),
3406 pointee_type);
3407 case clang::Type::Auto:
3408 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3409 ->getDeducedType()
3410 .getAsOpaquePtr(),
3411 pointee_type);
3412 case clang::Type::Elaborated:
3413 return IsPointerOrReferenceType(
3414 llvm::cast<clang::ElaboratedType>(qual_type)
3415 ->getNamedType()
3416 .getAsOpaquePtr(),
3417 pointee_type);
3418 case clang::Type::Paren:
3419 return IsPointerOrReferenceType(
3420 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3421 pointee_type);
3422 default:
3423 break;
3424 }
3425 }
3426 if (pointee_type)
3427 pointee_type->Clear();
3428 return false;
3429}
3430
3431bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3432 CompilerType *pointee_type,
3433 bool *is_rvalue) {
3434 if (type) {
3435 clang::QualType qual_type(GetCanonicalQualType(type));
3436 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3437
3438 switch (type_class) {
3439 case clang::Type::LValueReference:
3440 if (pointee_type)
3441 pointee_type->SetCompilerType(
3442 getASTContext(),
3443 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3444 if (is_rvalue)
3445 *is_rvalue = false;
3446 return true;
3447 case clang::Type::RValueReference:
3448 if (pointee_type)
3449 pointee_type->SetCompilerType(
3450 getASTContext(),
3451 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3452 if (is_rvalue)
3453 *is_rvalue = true;
3454 return true;
3455 case clang::Type::Typedef:
3456 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3457 ->getDecl()
3458 ->getUnderlyingType()
3459 .getAsOpaquePtr(),
3460 pointee_type, is_rvalue);
3461 case clang::Type::Auto:
3462 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3463 ->getDeducedType()
3464 .getAsOpaquePtr(),
3465 pointee_type, is_rvalue);
3466 case clang::Type::Elaborated:
3467 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3468 ->getNamedType()
3469 .getAsOpaquePtr(),
3470 pointee_type, is_rvalue);
3471 case clang::Type::Paren:
3472 return IsReferenceType(
3473 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3474 pointee_type, is_rvalue);
3475
3476 default:
3477 break;
3478 }
3479 }
3480 if (pointee_type)
3481 pointee_type->Clear();
3482 return false;
3483}
3484
3485bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3486 uint32_t &count, bool &is_complex) {
3487 if (type) {
3488 clang::QualType qual_type(GetCanonicalQualType(type));
3489
3490 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3491 qual_type->getCanonicalTypeInternal())) {
3492 clang::BuiltinType::Kind kind = BT->getKind();
3493 if (kind >= clang::BuiltinType::Float &&
3494 kind <= clang::BuiltinType::LongDouble) {
3495 count = 1;
3496 is_complex = false;
3497 return true;
3498 }
3499 } else if (const clang::ComplexType *CT =
3500 llvm::dyn_cast<clang::ComplexType>(
3501 qual_type->getCanonicalTypeInternal())) {
3502 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3503 is_complex)) {
3504 count = 2;
3505 is_complex = true;
3506 return true;
3507 }
3508 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3509 qual_type->getCanonicalTypeInternal())) {
3510 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3511 is_complex)) {
3512 count = VT->getNumElements();
3513 is_complex = false;
3514 return true;
3515 }
3516 }
3517 }
3518 count = 0;
3519 is_complex = false;
3520 return false;
3521}
3522
3523bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3524 if (!type)
3525 return false;
3526
3527 clang::QualType qual_type(GetQualType(type));
3528 const clang::TagType *tag_type =
3529 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3530 if (tag_type) {
3531 clang::TagDecl *tag_decl = tag_type->getDecl();
3532 if (tag_decl)
3533 return tag_decl->isCompleteDefinition();
3534 return false;
3535 } else {
3536 const clang::ObjCObjectType *objc_class_type =
3537 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3538 if (objc_class_type) {
3539 clang::ObjCInterfaceDecl *class_interface_decl =
3540 objc_class_type->getInterface();
3541 if (class_interface_decl)
3542 return class_interface_decl->getDefinition() != nullptr;
3543 return false;
3544 }
3545 }
3546 return true;
3547}
3548
3549bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3550 if (type) {
3551 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3552
3553 const clang::ObjCObjectPointerType *obj_pointer_type =
3554 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3555
3556 if (obj_pointer_type)
3557 return obj_pointer_type->isObjCClassType();
3558 }
3559 return false;
3560}
3561
3562bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3563 if (ClangUtil::IsClangType(type))
3564 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3565 return false;
3566}
3567
3568bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3569 if (!type)
3570 return false;
3571 clang::QualType qual_type(GetCanonicalQualType(type));
3572 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3573 return (type_class == clang::Type::Record);
3574}
3575
3576bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3577 if (!type)
3578 return false;
3579 clang::QualType qual_type(GetCanonicalQualType(type));
3580 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3581 return (type_class == clang::Type::Enum);
3582}
3583
3584bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3585 if (type) {
3586 clang::QualType qual_type(GetCanonicalQualType(type));
3587 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3588 switch (type_class) {
3589 case clang::Type::Record:
3590 if (GetCompleteType(type)) {
3591 const clang::RecordType *record_type =
3592 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3593 const clang::RecordDecl *record_decl = record_type->getDecl();
3594 if (record_decl) {
3595 const clang::CXXRecordDecl *cxx_record_decl =
3596 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3597 if (cxx_record_decl)
3598 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003599 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003600 }
3601 break;
3602
3603 default:
3604 break;
3605 }
3606 }
3607 return false;
3608}
3609
3610bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3611 CompilerType *dynamic_pointee_type,
3612 bool check_cplusplus,
3613 bool check_objc) {
3614 clang::QualType pointee_qual_type;
3615 if (type) {
3616 clang::QualType qual_type(GetCanonicalQualType(type));
3617 bool success = false;
3618 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3619 switch (type_class) {
3620 case clang::Type::Builtin:
3621 if (check_objc &&
3622 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3623 clang::BuiltinType::ObjCId) {
3624 if (dynamic_pointee_type)
3625 dynamic_pointee_type->SetCompilerType(this, type);
3626 return true;
3627 }
3628 break;
3629
3630 case clang::Type::ObjCObjectPointer:
3631 if (check_objc) {
3632 if (auto objc_pointee_type =
3633 qual_type->getPointeeType().getTypePtrOrNull()) {
3634 if (auto objc_object_type =
3635 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3636 objc_pointee_type)) {
3637 if (objc_object_type->isObjCClass())
3638 return false;
3639 }
3640 }
3641 if (dynamic_pointee_type)
3642 dynamic_pointee_type->SetCompilerType(
3643 getASTContext(),
3644 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3645 ->getPointeeType());
3646 return true;
3647 }
3648 break;
3649
3650 case clang::Type::Pointer:
3651 pointee_qual_type =
3652 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3653 success = true;
3654 break;
3655
3656 case clang::Type::LValueReference:
3657 case clang::Type::RValueReference:
3658 pointee_qual_type =
3659 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3660 success = true;
3661 break;
3662
3663 case clang::Type::Typedef:
3664 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3665 ->getDecl()
3666 ->getUnderlyingType()
3667 .getAsOpaquePtr(),
3668 dynamic_pointee_type, check_cplusplus,
3669 check_objc);
3670
3671 case clang::Type::Auto:
3672 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3673 ->getDeducedType()
3674 .getAsOpaquePtr(),
3675 dynamic_pointee_type, check_cplusplus,
3676 check_objc);
3677
3678 case clang::Type::Elaborated:
3679 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3680 ->getNamedType()
3681 .getAsOpaquePtr(),
3682 dynamic_pointee_type, check_cplusplus,
3683 check_objc);
3684
3685 case clang::Type::Paren:
3686 return IsPossibleDynamicType(
3687 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3688 dynamic_pointee_type, check_cplusplus, check_objc);
3689 default:
3690 break;
3691 }
3692
3693 if (success) {
3694 // Check to make sure what we are pointing too is a possible dynamic C++
3695 // type
3696 // We currently accept any "void *" (in case we have a class that has been
3697 // watered down to an opaque pointer) and virtual C++ classes.
3698 const clang::Type::TypeClass pointee_type_class =
3699 pointee_qual_type.getCanonicalType()->getTypeClass();
3700 switch (pointee_type_class) {
3701 case clang::Type::Builtin:
3702 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3703 case clang::BuiltinType::UnknownAny:
3704 case clang::BuiltinType::Void:
3705 if (dynamic_pointee_type)
3706 dynamic_pointee_type->SetCompilerType(getASTContext(),
3707 pointee_qual_type);
3708 return true;
3709 default:
3710 break;
3711 }
3712 break;
3713
3714 case clang::Type::Record:
3715 if (check_cplusplus) {
3716 clang::CXXRecordDecl *cxx_record_decl =
3717 pointee_qual_type->getAsCXXRecordDecl();
3718 if (cxx_record_decl) {
3719 bool is_complete = cxx_record_decl->isCompleteDefinition();
3720
3721 if (is_complete)
3722 success = cxx_record_decl->isDynamicClass();
3723 else {
3724 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3725 getASTContext(), cxx_record_decl);
3726 if (metadata)
3727 success = metadata->GetIsDynamicCXXType();
3728 else {
3729 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3730 .GetCompleteType();
3731 if (is_complete)
3732 success = cxx_record_decl->isDynamicClass();
3733 else
3734 success = false;
3735 }
3736 }
3737
3738 if (success) {
3739 if (dynamic_pointee_type)
3740 dynamic_pointee_type->SetCompilerType(getASTContext(),
3741 pointee_qual_type);
3742 return true;
3743 }
3744 }
3745 }
3746 break;
3747
3748 case clang::Type::ObjCObject:
3749 case clang::Type::ObjCInterface:
3750 if (check_objc) {
3751 if (dynamic_pointee_type)
3752 dynamic_pointee_type->SetCompilerType(getASTContext(),
3753 pointee_qual_type);
3754 return true;
3755 }
3756 break;
3757
3758 default:
3759 break;
3760 }
3761 }
3762 }
3763 if (dynamic_pointee_type)
3764 dynamic_pointee_type->Clear();
3765 return false;
3766}
3767
3768bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3769 if (!type)
3770 return false;
3771
3772 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3773}
3774
3775bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3776 if (!type)
3777 return false;
3778 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3779}
3780
3781bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3782 if (!type)
3783 return false;
3784 return GetCanonicalQualType(type)->isVoidType();
3785}
3786
3787bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3788 return ClangASTContextSupportsLanguage(language);
3789}
3790
3791bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3792 std::string &class_name) {
3793 if (type) {
3794 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3795 if (!qual_type.isNull()) {
3796 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3797 if (cxx_record_decl) {
3798 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3799 return true;
3800 }
3801 }
3802 }
3803 class_name.clear();
3804 return false;
3805}
3806
3807bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3808 if (!type)
3809 return false;
3810
3811 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3812 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
3813 return true;
3814 return false;
3815}
3816
3817bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3818 if (!type)
3819 return false;
3820 clang::QualType qual_type(GetCanonicalQualType(type));
3821 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3822 if (tag_type)
3823 return tag_type->isBeingDefined();
3824 return false;
3825}
3826
3827bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3828 CompilerType *class_type_ptr) {
3829 if (!type)
3830 return false;
3831
3832 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3833
3834 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3835 if (class_type_ptr) {
3836 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3837 const clang::ObjCObjectPointerType *obj_pointer_type =
3838 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3839 if (obj_pointer_type == nullptr)
3840 class_type_ptr->Clear();
3841 else
3842 class_type_ptr->SetCompilerType(
3843 type.GetTypeSystem(),
3844 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3845 .getAsOpaquePtr());
3846 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003847 }
3848 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003849 }
3850 if (class_type_ptr)
3851 class_type_ptr->Clear();
3852 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003853}
3854
Kate Stoneb9c1b512016-09-06 20:57:50 +00003855bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3856 std::string &class_name) {
3857 if (!type)
3858 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003859
Kate Stoneb9c1b512016-09-06 20:57:50 +00003860 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3861
3862 const clang::ObjCObjectType *object_type =
3863 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3864 if (object_type) {
3865 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3866 if (interface) {
3867 class_name = interface->getNameAsString();
3868 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00003869 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003870 }
3871 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003872}
3873
Greg Claytond8d4a572015-08-11 21:38:15 +00003874//----------------------------------------------------------------------
3875// Type Completion
3876//----------------------------------------------------------------------
3877
Kate Stoneb9c1b512016-09-06 20:57:50 +00003878bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3879 if (!type)
3880 return false;
3881 const bool allow_completion = true;
3882 return GetCompleteQualType(getASTContext(), GetQualType(type),
3883 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00003884}
3885
Kate Stoneb9c1b512016-09-06 20:57:50 +00003886ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
3887 std::string type_name;
3888 if (type) {
3889 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
3890 clang::QualType qual_type(GetQualType(type));
3891 printing_policy.SuppressTagKeyword = true;
3892 const clang::TypedefType *typedef_type =
3893 qual_type->getAs<clang::TypedefType>();
3894 if (typedef_type) {
3895 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3896 type_name = typedef_decl->getQualifiedNameAsString();
3897 } else {
3898 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003899 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003900 }
3901 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00003902}
3903
3904uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003905ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
3906 CompilerType *pointee_or_element_clang_type) {
3907 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003908 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003909
3910 if (pointee_or_element_clang_type)
3911 pointee_or_element_clang_type->Clear();
3912
3913 clang::QualType qual_type(GetQualType(type));
3914
3915 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3916 switch (type_class) {
3917 case clang::Type::Builtin: {
3918 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3919 qual_type->getCanonicalTypeInternal());
3920
3921 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3922 switch (builtin_type->getKind()) {
3923 case clang::BuiltinType::ObjCId:
3924 case clang::BuiltinType::ObjCClass:
3925 if (pointee_or_element_clang_type)
3926 pointee_or_element_clang_type->SetCompilerType(
3927 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
3928 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3929 break;
3930
3931 case clang::BuiltinType::ObjCSel:
3932 if (pointee_or_element_clang_type)
3933 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
3934 getASTContext()->CharTy);
3935 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3936 break;
3937
3938 case clang::BuiltinType::Bool:
3939 case clang::BuiltinType::Char_U:
3940 case clang::BuiltinType::UChar:
3941 case clang::BuiltinType::WChar_U:
3942 case clang::BuiltinType::Char16:
3943 case clang::BuiltinType::Char32:
3944 case clang::BuiltinType::UShort:
3945 case clang::BuiltinType::UInt:
3946 case clang::BuiltinType::ULong:
3947 case clang::BuiltinType::ULongLong:
3948 case clang::BuiltinType::UInt128:
3949 case clang::BuiltinType::Char_S:
3950 case clang::BuiltinType::SChar:
3951 case clang::BuiltinType::WChar_S:
3952 case clang::BuiltinType::Short:
3953 case clang::BuiltinType::Int:
3954 case clang::BuiltinType::Long:
3955 case clang::BuiltinType::LongLong:
3956 case clang::BuiltinType::Int128:
3957 case clang::BuiltinType::Float:
3958 case clang::BuiltinType::Double:
3959 case clang::BuiltinType::LongDouble:
3960 builtin_type_flags |= eTypeIsScalar;
3961 if (builtin_type->isInteger()) {
3962 builtin_type_flags |= eTypeIsInteger;
3963 if (builtin_type->isSignedInteger())
3964 builtin_type_flags |= eTypeIsSigned;
3965 } else if (builtin_type->isFloatingPoint())
3966 builtin_type_flags |= eTypeIsFloat;
3967 break;
3968 default:
3969 break;
3970 }
3971 return builtin_type_flags;
3972 }
3973
3974 case clang::Type::BlockPointer:
3975 if (pointee_or_element_clang_type)
3976 pointee_or_element_clang_type->SetCompilerType(
3977 getASTContext(), qual_type->getPointeeType());
3978 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3979
3980 case clang::Type::Complex: {
3981 uint32_t complex_type_flags =
3982 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3983 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
3984 qual_type->getCanonicalTypeInternal());
3985 if (complex_type) {
3986 clang::QualType complex_element_type(complex_type->getElementType());
3987 if (complex_element_type->isIntegerType())
3988 complex_type_flags |= eTypeIsFloat;
3989 else if (complex_element_type->isFloatingType())
3990 complex_type_flags |= eTypeIsInteger;
3991 }
3992 return complex_type_flags;
3993 } break;
3994
3995 case clang::Type::ConstantArray:
3996 case clang::Type::DependentSizedArray:
3997 case clang::Type::IncompleteArray:
3998 case clang::Type::VariableArray:
3999 if (pointee_or_element_clang_type)
4000 pointee_or_element_clang_type->SetCompilerType(
4001 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4002 ->getElementType());
4003 return eTypeHasChildren | eTypeIsArray;
4004
4005 case clang::Type::DependentName:
4006 return 0;
4007 case clang::Type::DependentSizedExtVector:
4008 return eTypeHasChildren | eTypeIsVector;
4009 case clang::Type::DependentTemplateSpecialization:
4010 return eTypeIsTemplate;
4011 case clang::Type::Decltype:
4012 return 0;
4013
4014 case clang::Type::Enum:
4015 if (pointee_or_element_clang_type)
4016 pointee_or_element_clang_type->SetCompilerType(
4017 getASTContext(),
4018 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4019 return eTypeIsEnumeration | eTypeHasValue;
4020
4021 case clang::Type::Auto:
4022 return CompilerType(
4023 getASTContext(),
4024 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4025 .GetTypeInfo(pointee_or_element_clang_type);
4026 case clang::Type::Elaborated:
4027 return CompilerType(
4028 getASTContext(),
4029 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4030 .GetTypeInfo(pointee_or_element_clang_type);
4031 case clang::Type::Paren:
4032 return CompilerType(getASTContext(),
4033 llvm::cast<clang::ParenType>(qual_type)->desugar())
4034 .GetTypeInfo(pointee_or_element_clang_type);
4035
4036 case clang::Type::FunctionProto:
4037 return eTypeIsFuncPrototype | eTypeHasValue;
4038 case clang::Type::FunctionNoProto:
4039 return eTypeIsFuncPrototype | eTypeHasValue;
4040 case clang::Type::InjectedClassName:
4041 return 0;
4042
4043 case clang::Type::LValueReference:
4044 case clang::Type::RValueReference:
4045 if (pointee_or_element_clang_type)
4046 pointee_or_element_clang_type->SetCompilerType(
4047 getASTContext(),
4048 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4049 ->getPointeeType());
4050 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4051
4052 case clang::Type::MemberPointer:
4053 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4054
4055 case clang::Type::ObjCObjectPointer:
4056 if (pointee_or_element_clang_type)
4057 pointee_or_element_clang_type->SetCompilerType(
4058 getASTContext(), qual_type->getPointeeType());
4059 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4060 eTypeHasValue;
4061
4062 case clang::Type::ObjCObject:
4063 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4064 case clang::Type::ObjCInterface:
4065 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4066
4067 case clang::Type::Pointer:
4068 if (pointee_or_element_clang_type)
4069 pointee_or_element_clang_type->SetCompilerType(
4070 getASTContext(), qual_type->getPointeeType());
4071 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4072
4073 case clang::Type::Record:
4074 if (qual_type->getAsCXXRecordDecl())
4075 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4076 else
4077 return eTypeHasChildren | eTypeIsStructUnion;
4078 break;
4079 case clang::Type::SubstTemplateTypeParm:
4080 return eTypeIsTemplate;
4081 case clang::Type::TemplateTypeParm:
4082 return eTypeIsTemplate;
4083 case clang::Type::TemplateSpecialization:
4084 return eTypeIsTemplate;
4085
4086 case clang::Type::Typedef:
4087 return eTypeIsTypedef |
4088 CompilerType(getASTContext(),
4089 llvm::cast<clang::TypedefType>(qual_type)
4090 ->getDecl()
4091 ->getUnderlyingType())
4092 .GetTypeInfo(pointee_or_element_clang_type);
4093 case clang::Type::TypeOfExpr:
4094 return 0;
4095 case clang::Type::TypeOf:
4096 return 0;
4097 case clang::Type::UnresolvedUsing:
4098 return 0;
4099
4100 case clang::Type::ExtVector:
4101 case clang::Type::Vector: {
4102 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4103 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4104 qual_type->getCanonicalTypeInternal());
4105 if (vector_type) {
4106 if (vector_type->isIntegerType())
4107 vector_type_flags |= eTypeIsFloat;
4108 else if (vector_type->isFloatingType())
4109 vector_type_flags |= eTypeIsInteger;
4110 }
4111 return vector_type_flags;
4112 }
4113 default:
4114 return 0;
4115 }
4116 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004117}
4118
Greg Claytond8d4a572015-08-11 21:38:15 +00004119lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004120ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4121 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004122 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004123
4124 // If the type is a reference, then resolve it to what it refers to first:
4125 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4126 if (qual_type->isAnyPointerType()) {
4127 if (qual_type->isObjCObjectPointerType())
4128 return lldb::eLanguageTypeObjC;
4129
4130 clang::QualType pointee_type(qual_type->getPointeeType());
4131 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4132 return lldb::eLanguageTypeC_plus_plus;
4133 if (pointee_type->isObjCObjectOrInterfaceType())
4134 return lldb::eLanguageTypeObjC;
4135 if (pointee_type->isObjCClassType())
4136 return lldb::eLanguageTypeObjC;
4137 if (pointee_type.getTypePtr() ==
4138 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4139 return lldb::eLanguageTypeObjC;
4140 } else {
4141 if (qual_type->isObjCObjectOrInterfaceType())
4142 return lldb::eLanguageTypeObjC;
4143 if (qual_type->getAsCXXRecordDecl())
4144 return lldb::eLanguageTypeC_plus_plus;
4145 switch (qual_type->getTypeClass()) {
4146 default:
4147 break;
4148 case clang::Type::Builtin:
4149 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4150 default:
4151 case clang::BuiltinType::Void:
4152 case clang::BuiltinType::Bool:
4153 case clang::BuiltinType::Char_U:
4154 case clang::BuiltinType::UChar:
4155 case clang::BuiltinType::WChar_U:
4156 case clang::BuiltinType::Char16:
4157 case clang::BuiltinType::Char32:
4158 case clang::BuiltinType::UShort:
4159 case clang::BuiltinType::UInt:
4160 case clang::BuiltinType::ULong:
4161 case clang::BuiltinType::ULongLong:
4162 case clang::BuiltinType::UInt128:
4163 case clang::BuiltinType::Char_S:
4164 case clang::BuiltinType::SChar:
4165 case clang::BuiltinType::WChar_S:
4166 case clang::BuiltinType::Short:
4167 case clang::BuiltinType::Int:
4168 case clang::BuiltinType::Long:
4169 case clang::BuiltinType::LongLong:
4170 case clang::BuiltinType::Int128:
4171 case clang::BuiltinType::Float:
4172 case clang::BuiltinType::Double:
4173 case clang::BuiltinType::LongDouble:
4174 break;
4175
4176 case clang::BuiltinType::NullPtr:
4177 return eLanguageTypeC_plus_plus;
4178
4179 case clang::BuiltinType::ObjCId:
4180 case clang::BuiltinType::ObjCClass:
4181 case clang::BuiltinType::ObjCSel:
4182 return eLanguageTypeObjC;
4183
4184 case clang::BuiltinType::Dependent:
4185 case clang::BuiltinType::Overload:
4186 case clang::BuiltinType::BoundMember:
4187 case clang::BuiltinType::UnknownAny:
4188 break;
4189 }
4190 break;
4191 case clang::Type::Typedef:
4192 return CompilerType(getASTContext(),
4193 llvm::cast<clang::TypedefType>(qual_type)
4194 ->getDecl()
4195 ->getUnderlyingType())
4196 .GetMinimumLanguage();
4197 }
4198 }
4199 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004200}
4201
4202lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004203ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4204 if (!type)
4205 return lldb::eTypeClassInvalid;
4206
4207 clang::QualType qual_type(GetQualType(type));
4208
4209 switch (qual_type->getTypeClass()) {
4210 case clang::Type::UnaryTransform:
4211 break;
4212 case clang::Type::FunctionNoProto:
4213 return lldb::eTypeClassFunction;
4214 case clang::Type::FunctionProto:
4215 return lldb::eTypeClassFunction;
4216 case clang::Type::IncompleteArray:
4217 return lldb::eTypeClassArray;
4218 case clang::Type::VariableArray:
4219 return lldb::eTypeClassArray;
4220 case clang::Type::ConstantArray:
4221 return lldb::eTypeClassArray;
4222 case clang::Type::DependentSizedArray:
4223 return lldb::eTypeClassArray;
4224 case clang::Type::DependentSizedExtVector:
4225 return lldb::eTypeClassVector;
4226 case clang::Type::ExtVector:
4227 return lldb::eTypeClassVector;
4228 case clang::Type::Vector:
4229 return lldb::eTypeClassVector;
4230 case clang::Type::Builtin:
4231 return lldb::eTypeClassBuiltin;
4232 case clang::Type::ObjCObjectPointer:
4233 return lldb::eTypeClassObjCObjectPointer;
4234 case clang::Type::BlockPointer:
4235 return lldb::eTypeClassBlockPointer;
4236 case clang::Type::Pointer:
4237 return lldb::eTypeClassPointer;
4238 case clang::Type::LValueReference:
4239 return lldb::eTypeClassReference;
4240 case clang::Type::RValueReference:
4241 return lldb::eTypeClassReference;
4242 case clang::Type::MemberPointer:
4243 return lldb::eTypeClassMemberPointer;
4244 case clang::Type::Complex:
4245 if (qual_type->isComplexType())
4246 return lldb::eTypeClassComplexFloat;
4247 else
4248 return lldb::eTypeClassComplexInteger;
4249 case clang::Type::ObjCObject:
4250 return lldb::eTypeClassObjCObject;
4251 case clang::Type::ObjCInterface:
4252 return lldb::eTypeClassObjCInterface;
4253 case clang::Type::Record: {
4254 const clang::RecordType *record_type =
4255 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4256 const clang::RecordDecl *record_decl = record_type->getDecl();
4257 if (record_decl->isUnion())
4258 return lldb::eTypeClassUnion;
4259 else if (record_decl->isStruct())
4260 return lldb::eTypeClassStruct;
4261 else
4262 return lldb::eTypeClassClass;
4263 } break;
4264 case clang::Type::Enum:
4265 return lldb::eTypeClassEnumeration;
4266 case clang::Type::Typedef:
4267 return lldb::eTypeClassTypedef;
4268 case clang::Type::UnresolvedUsing:
4269 break;
4270 case clang::Type::Paren:
4271 return CompilerType(getASTContext(),
4272 llvm::cast<clang::ParenType>(qual_type)->desugar())
4273 .GetTypeClass();
4274 case clang::Type::Auto:
4275 return CompilerType(
4276 getASTContext(),
4277 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4278 .GetTypeClass();
4279 case clang::Type::Elaborated:
4280 return CompilerType(
4281 getASTContext(),
4282 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4283 .GetTypeClass();
4284
4285 case clang::Type::Attributed:
4286 break;
4287 case clang::Type::TemplateTypeParm:
4288 break;
4289 case clang::Type::SubstTemplateTypeParm:
4290 break;
4291 case clang::Type::SubstTemplateTypeParmPack:
4292 break;
4293 case clang::Type::InjectedClassName:
4294 break;
4295 case clang::Type::DependentName:
4296 break;
4297 case clang::Type::DependentTemplateSpecialization:
4298 break;
4299 case clang::Type::PackExpansion:
4300 break;
4301
4302 case clang::Type::TypeOfExpr:
4303 break;
4304 case clang::Type::TypeOf:
4305 break;
4306 case clang::Type::Decltype:
4307 break;
4308 case clang::Type::TemplateSpecialization:
4309 break;
4310 case clang::Type::Atomic:
4311 break;
4312 case clang::Type::Pipe:
4313 break;
4314
4315 // pointer type decayed from an array or function type.
4316 case clang::Type::Decayed:
4317 break;
4318 case clang::Type::Adjusted:
4319 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004320 case clang::Type::ObjCTypeParam:
4321 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004322 }
4323 // We don't know hot to display this type...
4324 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004325}
4326
Kate Stoneb9c1b512016-09-06 20:57:50 +00004327unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4328 if (type)
4329 return GetQualType(type).getQualifiers().getCVRQualifiers();
4330 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004331}
4332
4333//----------------------------------------------------------------------
4334// Creating related types
4335//----------------------------------------------------------------------
4336
Greg Claytona1e5dc82015-08-11 22:53:00 +00004337CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004338ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4339 uint64_t *stride) {
4340 if (type) {
4341 clang::QualType qual_type(GetCanonicalQualType(type));
4342
4343 const clang::Type *array_eletype =
4344 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4345
4346 if (!array_eletype)
4347 return CompilerType();
4348
4349 CompilerType element_type(getASTContext(),
4350 array_eletype->getCanonicalTypeUnqualified());
4351
4352 // TODO: the real stride will be >= this value.. find the real one!
4353 if (stride)
4354 *stride = element_type.GetByteSize(nullptr);
4355
4356 return element_type;
4357 }
4358 return CompilerType();
4359}
4360
4361CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4362 uint64_t size) {
4363 if (type) {
4364 clang::QualType qual_type(GetCanonicalQualType(type));
4365 if (clang::ASTContext *ast_ctx = getASTContext()) {
4366 if (size != 0)
4367 return CompilerType(
4368 ast_ctx, ast_ctx->getConstantArrayType(
4369 qual_type, llvm::APInt(64, size),
4370 clang::ArrayType::ArraySizeModifier::Normal, 0));
4371 else
4372 return CompilerType(
4373 ast_ctx,
4374 ast_ctx->getIncompleteArrayType(
4375 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
Greg Claytond8d4a572015-08-11 21:38:15 +00004376 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004377 }
4378
4379 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004380}
4381
Greg Claytona1e5dc82015-08-11 22:53:00 +00004382CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004383ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4384 if (type)
4385 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4386 return CompilerType();
4387}
4388
4389static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4390 clang::QualType qual_type) {
4391 if (qual_type->isPointerType())
4392 qual_type = ast->getPointerType(
4393 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4394 else
4395 qual_type = qual_type.getUnqualifiedType();
4396 qual_type.removeLocalConst();
4397 qual_type.removeLocalRestrict();
4398 qual_type.removeLocalVolatile();
4399 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004400}
4401
4402CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004403ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4404 if (type)
4405 return CompilerType(
4406 getASTContext(),
4407 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4408 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004409}
4410
Kate Stoneb9c1b512016-09-06 20:57:50 +00004411int ClangASTContext::GetFunctionArgumentCount(
4412 lldb::opaque_compiler_type_t type) {
4413 if (type) {
4414 const clang::FunctionProtoType *func =
4415 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4416 if (func)
4417 return func->getNumParams();
4418 }
4419 return -1;
4420}
4421
4422CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4423 lldb::opaque_compiler_type_t type, size_t idx) {
4424 if (type) {
4425 const clang::FunctionProtoType *func =
4426 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4427 if (func) {
4428 const uint32_t num_args = func->getNumParams();
4429 if (idx < num_args)
4430 return CompilerType(getASTContext(), func->getParamType(idx));
4431 }
4432 }
4433 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004434}
4435
Greg Claytona1e5dc82015-08-11 22:53:00 +00004436CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004437ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4438 if (type) {
4439 clang::QualType qual_type(GetQualType(type));
4440 const clang::FunctionProtoType *func =
4441 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4442 if (func)
4443 return CompilerType(getASTContext(), func->getReturnType());
4444 }
4445 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004446}
4447
4448size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004449ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4450 size_t num_functions = 0;
4451 if (type) {
4452 clang::QualType qual_type(GetCanonicalQualType(type));
4453 switch (qual_type->getTypeClass()) {
4454 case clang::Type::Record:
4455 if (GetCompleteQualType(getASTContext(), qual_type)) {
4456 const clang::RecordType *record_type =
4457 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4458 const clang::RecordDecl *record_decl = record_type->getDecl();
4459 assert(record_decl);
4460 const clang::CXXRecordDecl *cxx_record_decl =
4461 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4462 if (cxx_record_decl)
4463 num_functions = std::distance(cxx_record_decl->method_begin(),
4464 cxx_record_decl->method_end());
4465 }
4466 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004467
Sean Callananf9c622a2016-09-30 18:44:43 +00004468 case clang::Type::ObjCObjectPointer: {
4469 const clang::ObjCObjectPointerType *objc_class_type =
4470 qual_type->getAsObjCInterfacePointerType();
4471 const clang::ObjCInterfaceType *objc_interface_type =
4472 objc_class_type->getInterfaceType();
4473 if (objc_interface_type &&
4474 GetCompleteType((lldb::opaque_compiler_type_t)objc_interface_type)) {
4475 clang::ObjCInterfaceDecl *class_interface_decl =
4476 objc_interface_type->getDecl();
4477 if (class_interface_decl) {
4478 num_functions = std::distance(class_interface_decl->meth_begin(),
4479 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004480 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004481 }
4482 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004483 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004484
4485 case clang::Type::ObjCObject:
4486 case clang::Type::ObjCInterface:
4487 if (GetCompleteType(type)) {
4488 const clang::ObjCObjectType *objc_class_type =
4489 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4490 if (objc_class_type) {
4491 clang::ObjCInterfaceDecl *class_interface_decl =
4492 objc_class_type->getInterface();
4493 if (class_interface_decl)
4494 num_functions = std::distance(class_interface_decl->meth_begin(),
4495 class_interface_decl->meth_end());
4496 }
4497 }
4498 break;
4499
4500 case clang::Type::Typedef:
4501 return CompilerType(getASTContext(),
4502 llvm::cast<clang::TypedefType>(qual_type)
4503 ->getDecl()
4504 ->getUnderlyingType())
4505 .GetNumMemberFunctions();
4506
4507 case clang::Type::Auto:
4508 return CompilerType(
4509 getASTContext(),
4510 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4511 .GetNumMemberFunctions();
4512
4513 case clang::Type::Elaborated:
4514 return CompilerType(
4515 getASTContext(),
4516 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4517 .GetNumMemberFunctions();
4518
4519 case clang::Type::Paren:
4520 return CompilerType(getASTContext(),
4521 llvm::cast<clang::ParenType>(qual_type)->desugar())
4522 .GetNumMemberFunctions();
4523
4524 default:
4525 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004526 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004527 }
4528 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004529}
4530
4531TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004532ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4533 size_t idx) {
4534 std::string name;
4535 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4536 CompilerType clang_type;
4537 CompilerDecl clang_decl;
4538 if (type) {
4539 clang::QualType qual_type(GetCanonicalQualType(type));
4540 switch (qual_type->getTypeClass()) {
4541 case clang::Type::Record:
4542 if (GetCompleteQualType(getASTContext(), qual_type)) {
4543 const clang::RecordType *record_type =
4544 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4545 const clang::RecordDecl *record_decl = record_type->getDecl();
4546 assert(record_decl);
4547 const clang::CXXRecordDecl *cxx_record_decl =
4548 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4549 if (cxx_record_decl) {
4550 auto method_iter = cxx_record_decl->method_begin();
4551 auto method_end = cxx_record_decl->method_end();
4552 if (idx <
4553 static_cast<size_t>(std::distance(method_iter, method_end))) {
4554 std::advance(method_iter, idx);
4555 clang::CXXMethodDecl *cxx_method_decl =
4556 method_iter->getCanonicalDecl();
4557 if (cxx_method_decl) {
4558 name = cxx_method_decl->getDeclName().getAsString();
4559 if (cxx_method_decl->isStatic())
4560 kind = lldb::eMemberFunctionKindStaticMethod;
4561 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4562 kind = lldb::eMemberFunctionKindConstructor;
4563 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4564 kind = lldb::eMemberFunctionKindDestructor;
4565 else
4566 kind = lldb::eMemberFunctionKindInstanceMethod;
4567 clang_type = CompilerType(
4568 this, cxx_method_decl->getType().getAsOpaquePtr());
4569 clang_decl = CompilerDecl(this, cxx_method_decl);
4570 }
4571 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004572 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004573 }
4574 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004575
Sean Callananf9c622a2016-09-30 18:44:43 +00004576 case clang::Type::ObjCObjectPointer: {
4577 const clang::ObjCObjectPointerType *objc_class_type =
4578 qual_type->getAsObjCInterfacePointerType();
4579 const clang::ObjCInterfaceType *objc_interface_type =
4580 objc_class_type->getInterfaceType();
4581 if (objc_interface_type &&
4582 GetCompleteType((lldb::opaque_compiler_type_t)objc_interface_type)) {
4583 clang::ObjCInterfaceDecl *class_interface_decl =
4584 objc_interface_type->getDecl();
4585 if (class_interface_decl) {
4586 auto method_iter = class_interface_decl->meth_begin();
4587 auto method_end = class_interface_decl->meth_end();
4588 if (idx <
4589 static_cast<size_t>(std::distance(method_iter, method_end))) {
4590 std::advance(method_iter, idx);
4591 clang::ObjCMethodDecl *objc_method_decl =
4592 method_iter->getCanonicalDecl();
4593 if (objc_method_decl) {
4594 clang_decl = CompilerDecl(this, objc_method_decl);
4595 name = objc_method_decl->getSelector().getAsString();
4596 if (objc_method_decl->isClassMethod())
4597 kind = lldb::eMemberFunctionKindStaticMethod;
4598 else
4599 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004600 }
4601 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004602 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004603 }
4604 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004605 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004606
Kate Stoneb9c1b512016-09-06 20:57:50 +00004607 case clang::Type::ObjCObject:
4608 case clang::Type::ObjCInterface:
4609 if (GetCompleteType(type)) {
4610 const clang::ObjCObjectType *objc_class_type =
4611 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4612 if (objc_class_type) {
4613 clang::ObjCInterfaceDecl *class_interface_decl =
4614 objc_class_type->getInterface();
4615 if (class_interface_decl) {
4616 auto method_iter = class_interface_decl->meth_begin();
4617 auto method_end = class_interface_decl->meth_end();
4618 if (idx <
4619 static_cast<size_t>(std::distance(method_iter, method_end))) {
4620 std::advance(method_iter, idx);
4621 clang::ObjCMethodDecl *objc_method_decl =
4622 method_iter->getCanonicalDecl();
4623 if (objc_method_decl) {
4624 clang_decl = CompilerDecl(this, objc_method_decl);
4625 name = objc_method_decl->getSelector().getAsString();
4626 if (objc_method_decl->isClassMethod())
4627 kind = lldb::eMemberFunctionKindStaticMethod;
4628 else
4629 kind = lldb::eMemberFunctionKindInstanceMethod;
4630 }
4631 }
4632 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004633 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004634 }
4635 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004636
Kate Stoneb9c1b512016-09-06 20:57:50 +00004637 case clang::Type::Typedef:
4638 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4639 ->getDecl()
4640 ->getUnderlyingType()
4641 .getAsOpaquePtr(),
4642 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004643
Kate Stoneb9c1b512016-09-06 20:57:50 +00004644 case clang::Type::Auto:
4645 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4646 ->getDeducedType()
4647 .getAsOpaquePtr(),
4648 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004649
Kate Stoneb9c1b512016-09-06 20:57:50 +00004650 case clang::Type::Elaborated:
4651 return GetMemberFunctionAtIndex(
4652 llvm::cast<clang::ElaboratedType>(qual_type)
4653 ->getNamedType()
4654 .getAsOpaquePtr(),
4655 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004656
Kate Stoneb9c1b512016-09-06 20:57:50 +00004657 case clang::Type::Paren:
4658 return GetMemberFunctionAtIndex(
4659 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4660 idx);
4661
4662 default:
4663 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004664 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004665 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004666
Kate Stoneb9c1b512016-09-06 20:57:50 +00004667 if (kind == eMemberFunctionKindUnknown)
4668 return TypeMemberFunctionImpl();
4669 else
4670 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004671}
4672
Greg Claytona1e5dc82015-08-11 22:53:00 +00004673CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004674ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4675 if (type)
4676 return CompilerType(getASTContext(),
4677 GetQualType(type).getNonReferenceType());
4678 return CompilerType();
4679}
4680
4681CompilerType ClangASTContext::CreateTypedefType(
4682 const CompilerType &type, const char *typedef_name,
4683 const CompilerDeclContext &compiler_decl_ctx) {
4684 if (type && typedef_name && typedef_name[0]) {
4685 ClangASTContext *ast =
4686 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4687 if (!ast)
4688 return CompilerType();
4689 clang::ASTContext *clang_ast = ast->getASTContext();
4690 clang::QualType qual_type(ClangUtil::GetQualType(type));
4691
4692 clang::DeclContext *decl_ctx =
4693 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4694 if (decl_ctx == nullptr)
4695 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4696
4697 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4698 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4699 &clang_ast->Idents.get(typedef_name),
4700 clang_ast->getTrivialTypeSourceInfo(qual_type));
4701
4702 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4703
4704 // Get a uniqued clang::QualType for the typedef decl type
4705 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4706 }
4707 return CompilerType();
4708}
4709
4710CompilerType
4711ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4712 if (type) {
4713 clang::QualType qual_type(GetQualType(type));
4714 return CompilerType(getASTContext(),
4715 qual_type.getTypePtr()->getPointeeType());
4716 }
4717 return CompilerType();
4718}
4719
4720CompilerType
4721ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4722 if (type) {
4723 clang::QualType qual_type(GetQualType(type));
4724
4725 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4726 switch (type_class) {
4727 case clang::Type::ObjCObject:
4728 case clang::Type::ObjCInterface:
4729 return CompilerType(getASTContext(),
4730 getASTContext()->getObjCObjectPointerType(qual_type));
4731
4732 default:
4733 return CompilerType(getASTContext(),
4734 getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004735 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004736 }
4737 return CompilerType();
4738}
4739
4740CompilerType
4741ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4742 if (type)
4743 return CompilerType(this, getASTContext()
4744 ->getLValueReferenceType(GetQualType(type))
4745 .getAsOpaquePtr());
4746 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004747 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004748}
4749
Kate Stoneb9c1b512016-09-06 20:57:50 +00004750CompilerType
4751ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4752 if (type)
4753 return CompilerType(this, getASTContext()
4754 ->getRValueReferenceType(GetQualType(type))
4755 .getAsOpaquePtr());
4756 else
4757 return CompilerType();
4758}
4759
4760CompilerType
4761ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4762 if (type) {
4763 clang::QualType result(GetQualType(type));
4764 result.addConst();
4765 return CompilerType(this, result.getAsOpaquePtr());
4766 }
4767 return CompilerType();
4768}
4769
4770CompilerType
4771ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4772 if (type) {
4773 clang::QualType result(GetQualType(type));
4774 result.addVolatile();
4775 return CompilerType(this, result.getAsOpaquePtr());
4776 }
4777 return CompilerType();
4778}
4779
4780CompilerType
4781ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4782 if (type) {
4783 clang::QualType result(GetQualType(type));
4784 result.addRestrict();
4785 return CompilerType(this, result.getAsOpaquePtr());
4786 }
4787 return CompilerType();
4788}
4789
4790CompilerType
4791ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4792 const char *typedef_name,
4793 const CompilerDeclContext &compiler_decl_ctx) {
4794 if (type) {
4795 clang::ASTContext *clang_ast = getASTContext();
4796 clang::QualType qual_type(GetQualType(type));
4797
4798 clang::DeclContext *decl_ctx =
4799 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4800 if (decl_ctx == nullptr)
4801 decl_ctx = getASTContext()->getTranslationUnitDecl();
4802
4803 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4804 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4805 &clang_ast->Idents.get(typedef_name),
4806 clang_ast->getTrivialTypeSourceInfo(qual_type));
4807
4808 clang::TagDecl *tdecl = nullptr;
4809 if (!qual_type.isNull()) {
4810 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4811 tdecl = rt->getDecl();
4812 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4813 tdecl = et->getDecl();
4814 }
4815
4816 // Check whether this declaration is an anonymous struct, union, or enum,
4817 // hidden behind a typedef. If so, we
4818 // try to check whether we have a typedef tag to attach to the original
4819 // record declaration
4820 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4821 tdecl->setTypedefNameForAnonDecl(decl);
4822
4823 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4824
4825 // Get a uniqued clang::QualType for the typedef decl type
4826 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4827 }
4828 return CompilerType();
4829}
4830
4831CompilerType
4832ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4833 if (type) {
4834 const clang::TypedefType *typedef_type =
4835 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4836 if (typedef_type)
4837 return CompilerType(getASTContext(),
4838 typedef_type->getDecl()->getUnderlyingType());
4839 }
4840 return CompilerType();
4841}
Greg Claytond8d4a572015-08-11 21:38:15 +00004842
4843//----------------------------------------------------------------------
4844// Create related types using the current type's AST
4845//----------------------------------------------------------------------
4846
Kate Stoneb9c1b512016-09-06 20:57:50 +00004847CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4848 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004849}
4850//----------------------------------------------------------------------
4851// Exploring the type
4852//----------------------------------------------------------------------
4853
Kate Stoneb9c1b512016-09-06 20:57:50 +00004854uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4855 ExecutionContextScope *exe_scope) {
4856 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00004857 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004858 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004859 switch (type_class) {
4860 case clang::Type::Record:
4861 if (GetCompleteType(type))
4862 return getASTContext()->getTypeSize(qual_type);
4863 else
4864 return 0;
4865 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004866
Kate Stoneb9c1b512016-09-06 20:57:50 +00004867 case clang::Type::ObjCInterface:
4868 case clang::Type::ObjCObject: {
4869 ExecutionContext exe_ctx(exe_scope);
4870 Process *process = exe_ctx.GetProcessPtr();
4871 if (process) {
4872 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4873 if (objc_runtime) {
4874 uint64_t bit_size = 0;
4875 if (objc_runtime->GetTypeBitSize(
4876 CompilerType(getASTContext(), qual_type), bit_size))
4877 return bit_size;
4878 }
4879 } else {
4880 static bool g_printed = false;
4881 if (!g_printed) {
4882 StreamString s;
4883 DumpTypeDescription(type, &s);
4884
4885 llvm::outs() << "warning: trying to determine the size of type ";
4886 llvm::outs() << s.GetString() << "\n";
4887 llvm::outs() << "without a valid ExecutionContext. this is not "
4888 "reliable. please file a bug against LLDB.\n";
4889 llvm::outs() << "backtrace:\n";
4890 llvm::sys::PrintStackTrace(llvm::outs());
4891 llvm::outs() << "\n";
4892 g_printed = true;
4893 }
4894 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004895 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004896 LLVM_FALLTHROUGH;
4897 default:
4898 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
4899 if (bit_size == 0) {
4900 if (qual_type->isIncompleteArrayType())
4901 return getASTContext()->getTypeSize(
4902 qual_type->getArrayElementTypeNoTypeQual()
4903 ->getCanonicalTypeUnqualified());
4904 }
4905 if (qual_type->isObjCObjectOrInterfaceType())
4906 return bit_size +
4907 getASTContext()->getTypeSize(
4908 getASTContext()->ObjCBuiltinClassTy);
4909 return bit_size;
4910 }
4911 }
4912 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004913}
4914
Kate Stoneb9c1b512016-09-06 20:57:50 +00004915size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
4916 if (GetCompleteType(type))
4917 return getASTContext()->getTypeAlign(GetQualType(type));
4918 return 0;
4919}
4920
4921lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
4922 uint64_t &count) {
4923 if (!type)
4924 return lldb::eEncodingInvalid;
4925
4926 count = 1;
4927 clang::QualType qual_type(GetCanonicalQualType(type));
4928
4929 switch (qual_type->getTypeClass()) {
4930 case clang::Type::UnaryTransform:
4931 break;
4932
4933 case clang::Type::FunctionNoProto:
4934 case clang::Type::FunctionProto:
4935 break;
4936
4937 case clang::Type::IncompleteArray:
4938 case clang::Type::VariableArray:
4939 break;
4940
4941 case clang::Type::ConstantArray:
4942 break;
4943
4944 case clang::Type::ExtVector:
4945 case clang::Type::Vector:
4946 // TODO: Set this to more than one???
4947 break;
4948
4949 case clang::Type::Builtin:
4950 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4951 case clang::BuiltinType::Void:
4952 break;
4953
4954 case clang::BuiltinType::Bool:
4955 case clang::BuiltinType::Char_S:
4956 case clang::BuiltinType::SChar:
4957 case clang::BuiltinType::WChar_S:
4958 case clang::BuiltinType::Char16:
4959 case clang::BuiltinType::Char32:
4960 case clang::BuiltinType::Short:
4961 case clang::BuiltinType::Int:
4962 case clang::BuiltinType::Long:
4963 case clang::BuiltinType::LongLong:
4964 case clang::BuiltinType::Int128:
4965 return lldb::eEncodingSint;
4966
4967 case clang::BuiltinType::Char_U:
4968 case clang::BuiltinType::UChar:
4969 case clang::BuiltinType::WChar_U:
4970 case clang::BuiltinType::UShort:
4971 case clang::BuiltinType::UInt:
4972 case clang::BuiltinType::ULong:
4973 case clang::BuiltinType::ULongLong:
4974 case clang::BuiltinType::UInt128:
4975 return lldb::eEncodingUint;
4976
4977 case clang::BuiltinType::Half:
4978 case clang::BuiltinType::Float:
4979 case clang::BuiltinType::Float128:
4980 case clang::BuiltinType::Double:
4981 case clang::BuiltinType::LongDouble:
4982 return lldb::eEncodingIEEE754;
4983
4984 case clang::BuiltinType::ObjCClass:
4985 case clang::BuiltinType::ObjCId:
4986 case clang::BuiltinType::ObjCSel:
4987 return lldb::eEncodingUint;
4988
4989 case clang::BuiltinType::NullPtr:
4990 return lldb::eEncodingUint;
4991
4992 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4993 case clang::BuiltinType::Kind::BoundMember:
4994 case clang::BuiltinType::Kind::BuiltinFn:
4995 case clang::BuiltinType::Kind::Dependent:
4996 case clang::BuiltinType::Kind::OCLClkEvent:
4997 case clang::BuiltinType::Kind::OCLEvent:
4998 case clang::BuiltinType::Kind::OCLImage1dRO:
4999 case clang::BuiltinType::Kind::OCLImage1dWO:
5000 case clang::BuiltinType::Kind::OCLImage1dRW:
5001 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5002 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5003 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5004 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5005 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5006 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5007 case clang::BuiltinType::Kind::OCLImage2dRO:
5008 case clang::BuiltinType::Kind::OCLImage2dWO:
5009 case clang::BuiltinType::Kind::OCLImage2dRW:
5010 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5011 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5012 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5013 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5014 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5015 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5016 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5017 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5018 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5019 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5020 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5021 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5022 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5023 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5024 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5025 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5026 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5027 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5028 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5029 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5030 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5031 case clang::BuiltinType::Kind::OCLImage3dRO:
5032 case clang::BuiltinType::Kind::OCLImage3dWO:
5033 case clang::BuiltinType::Kind::OCLImage3dRW:
5034 case clang::BuiltinType::Kind::OCLQueue:
5035 case clang::BuiltinType::Kind::OCLNDRange:
5036 case clang::BuiltinType::Kind::OCLReserveID:
5037 case clang::BuiltinType::Kind::OCLSampler:
5038 case clang::BuiltinType::Kind::OMPArraySection:
5039 case clang::BuiltinType::Kind::Overload:
5040 case clang::BuiltinType::Kind::PseudoObject:
5041 case clang::BuiltinType::Kind::UnknownAny:
5042 break;
5043 }
5044 break;
5045 // All pointer types are represented as unsigned integer encodings.
5046 // We may nee to add a eEncodingPointer if we ever need to know the
5047 // difference
5048 case clang::Type::ObjCObjectPointer:
5049 case clang::Type::BlockPointer:
5050 case clang::Type::Pointer:
5051 case clang::Type::LValueReference:
5052 case clang::Type::RValueReference:
5053 case clang::Type::MemberPointer:
5054 return lldb::eEncodingUint;
5055 case clang::Type::Complex: {
5056 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5057 if (qual_type->isComplexType())
5058 encoding = lldb::eEncodingIEEE754;
5059 else {
5060 const clang::ComplexType *complex_type =
5061 qual_type->getAsComplexIntegerType();
5062 if (complex_type)
5063 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5064 .GetEncoding(count);
5065 else
5066 encoding = lldb::eEncodingSint;
5067 }
5068 count = 2;
5069 return encoding;
5070 }
5071
5072 case clang::Type::ObjCInterface:
5073 break;
5074 case clang::Type::Record:
5075 break;
5076 case clang::Type::Enum:
5077 return lldb::eEncodingSint;
5078 case clang::Type::Typedef:
5079 return CompilerType(getASTContext(),
5080 llvm::cast<clang::TypedefType>(qual_type)
5081 ->getDecl()
5082 ->getUnderlyingType())
5083 .GetEncoding(count);
5084
5085 case clang::Type::Auto:
5086 return CompilerType(
5087 getASTContext(),
5088 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5089 .GetEncoding(count);
5090
5091 case clang::Type::Elaborated:
5092 return CompilerType(
5093 getASTContext(),
5094 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5095 .GetEncoding(count);
5096
5097 case clang::Type::Paren:
5098 return CompilerType(getASTContext(),
5099 llvm::cast<clang::ParenType>(qual_type)->desugar())
5100 .GetEncoding(count);
5101
5102 case clang::Type::DependentSizedArray:
5103 case clang::Type::DependentSizedExtVector:
5104 case clang::Type::UnresolvedUsing:
5105 case clang::Type::Attributed:
5106 case clang::Type::TemplateTypeParm:
5107 case clang::Type::SubstTemplateTypeParm:
5108 case clang::Type::SubstTemplateTypeParmPack:
5109 case clang::Type::InjectedClassName:
5110 case clang::Type::DependentName:
5111 case clang::Type::DependentTemplateSpecialization:
5112 case clang::Type::PackExpansion:
5113 case clang::Type::ObjCObject:
5114
5115 case clang::Type::TypeOfExpr:
5116 case clang::Type::TypeOf:
5117 case clang::Type::Decltype:
5118 case clang::Type::TemplateSpecialization:
5119 case clang::Type::Atomic:
5120 case clang::Type::Adjusted:
5121 case clang::Type::Pipe:
5122 break;
5123
5124 // pointer type decayed from an array or function type.
5125 case clang::Type::Decayed:
5126 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005127 case clang::Type::ObjCTypeParam:
5128 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005129 }
5130 count = 0;
5131 return lldb::eEncodingInvalid;
5132}
5133
5134lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5135 if (!type)
5136 return lldb::eFormatDefault;
5137
5138 clang::QualType qual_type(GetCanonicalQualType(type));
5139
5140 switch (qual_type->getTypeClass()) {
5141 case clang::Type::UnaryTransform:
5142 break;
5143
5144 case clang::Type::FunctionNoProto:
5145 case clang::Type::FunctionProto:
5146 break;
5147
5148 case clang::Type::IncompleteArray:
5149 case clang::Type::VariableArray:
5150 break;
5151
5152 case clang::Type::ConstantArray:
5153 return lldb::eFormatVoid; // no value
5154
5155 case clang::Type::ExtVector:
5156 case clang::Type::Vector:
5157 break;
5158
5159 case clang::Type::Builtin:
5160 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5161 // default: assert(0 && "Unknown builtin type!");
5162 case clang::BuiltinType::UnknownAny:
5163 case clang::BuiltinType::Void:
5164 case clang::BuiltinType::BoundMember:
5165 break;
5166
5167 case clang::BuiltinType::Bool:
5168 return lldb::eFormatBoolean;
5169 case clang::BuiltinType::Char_S:
5170 case clang::BuiltinType::SChar:
5171 case clang::BuiltinType::WChar_S:
5172 case clang::BuiltinType::Char_U:
5173 case clang::BuiltinType::UChar:
5174 case clang::BuiltinType::WChar_U:
5175 return lldb::eFormatChar;
5176 case clang::BuiltinType::Char16:
5177 return lldb::eFormatUnicode16;
5178 case clang::BuiltinType::Char32:
5179 return lldb::eFormatUnicode32;
5180 case clang::BuiltinType::UShort:
5181 return lldb::eFormatUnsigned;
5182 case clang::BuiltinType::Short:
5183 return lldb::eFormatDecimal;
5184 case clang::BuiltinType::UInt:
5185 return lldb::eFormatUnsigned;
5186 case clang::BuiltinType::Int:
5187 return lldb::eFormatDecimal;
5188 case clang::BuiltinType::ULong:
5189 return lldb::eFormatUnsigned;
5190 case clang::BuiltinType::Long:
5191 return lldb::eFormatDecimal;
5192 case clang::BuiltinType::ULongLong:
5193 return lldb::eFormatUnsigned;
5194 case clang::BuiltinType::LongLong:
5195 return lldb::eFormatDecimal;
5196 case clang::BuiltinType::UInt128:
5197 return lldb::eFormatUnsigned;
5198 case clang::BuiltinType::Int128:
5199 return lldb::eFormatDecimal;
5200 case clang::BuiltinType::Half:
5201 case clang::BuiltinType::Float:
5202 case clang::BuiltinType::Double:
5203 case clang::BuiltinType::LongDouble:
5204 return lldb::eFormatFloat;
5205 default:
5206 return lldb::eFormatHex;
5207 }
5208 break;
5209 case clang::Type::ObjCObjectPointer:
5210 return lldb::eFormatHex;
5211 case clang::Type::BlockPointer:
5212 return lldb::eFormatHex;
5213 case clang::Type::Pointer:
5214 return lldb::eFormatHex;
5215 case clang::Type::LValueReference:
5216 case clang::Type::RValueReference:
5217 return lldb::eFormatHex;
5218 case clang::Type::MemberPointer:
5219 break;
5220 case clang::Type::Complex: {
5221 if (qual_type->isComplexType())
5222 return lldb::eFormatComplex;
5223 else
5224 return lldb::eFormatComplexInteger;
5225 }
5226 case clang::Type::ObjCInterface:
5227 break;
5228 case clang::Type::Record:
5229 break;
5230 case clang::Type::Enum:
5231 return lldb::eFormatEnum;
5232 case clang::Type::Typedef:
5233 return CompilerType(getASTContext(),
5234 llvm::cast<clang::TypedefType>(qual_type)
5235 ->getDecl()
5236 ->getUnderlyingType())
5237 .GetFormat();
5238 case clang::Type::Auto:
5239 return CompilerType(getASTContext(),
5240 llvm::cast<clang::AutoType>(qual_type)->desugar())
5241 .GetFormat();
5242 case clang::Type::Paren:
5243 return CompilerType(getASTContext(),
5244 llvm::cast<clang::ParenType>(qual_type)->desugar())
5245 .GetFormat();
5246 case clang::Type::Elaborated:
5247 return CompilerType(
5248 getASTContext(),
5249 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5250 .GetFormat();
5251 case clang::Type::DependentSizedArray:
5252 case clang::Type::DependentSizedExtVector:
5253 case clang::Type::UnresolvedUsing:
5254 case clang::Type::Attributed:
5255 case clang::Type::TemplateTypeParm:
5256 case clang::Type::SubstTemplateTypeParm:
5257 case clang::Type::SubstTemplateTypeParmPack:
5258 case clang::Type::InjectedClassName:
5259 case clang::Type::DependentName:
5260 case clang::Type::DependentTemplateSpecialization:
5261 case clang::Type::PackExpansion:
5262 case clang::Type::ObjCObject:
5263
5264 case clang::Type::TypeOfExpr:
5265 case clang::Type::TypeOf:
5266 case clang::Type::Decltype:
5267 case clang::Type::TemplateSpecialization:
5268 case clang::Type::Atomic:
5269 case clang::Type::Adjusted:
5270 case clang::Type::Pipe:
5271 break;
5272
5273 // pointer type decayed from an array or function type.
5274 case clang::Type::Decayed:
5275 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005276 case clang::Type::ObjCTypeParam:
5277 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005278 }
5279 // We don't know hot to display this type...
5280 return lldb::eFormatBytes;
5281}
5282
5283static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5284 bool check_superclass) {
5285 while (class_interface_decl) {
5286 if (class_interface_decl->ivar_size() > 0)
5287 return true;
5288
5289 if (check_superclass)
5290 class_interface_decl = class_interface_decl->getSuperClass();
5291 else
5292 break;
5293 }
5294 return false;
5295}
5296
5297uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
5298 bool omit_empty_base_classes) {
5299 if (!type)
5300 return 0;
5301
5302 uint32_t num_children = 0;
5303 clang::QualType qual_type(GetQualType(type));
5304 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5305 switch (type_class) {
5306 case clang::Type::Builtin:
5307 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5308 case clang::BuiltinType::ObjCId: // child is Class
5309 case clang::BuiltinType::ObjCClass: // child is Class
5310 num_children = 1;
5311 break;
5312
5313 default:
5314 break;
5315 }
5316 break;
5317
5318 case clang::Type::Complex:
5319 return 0;
5320
5321 case clang::Type::Record:
5322 if (GetCompleteQualType(getASTContext(), qual_type)) {
5323 const clang::RecordType *record_type =
5324 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5325 const clang::RecordDecl *record_decl = record_type->getDecl();
5326 assert(record_decl);
5327 const clang::CXXRecordDecl *cxx_record_decl =
5328 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5329 if (cxx_record_decl) {
5330 if (omit_empty_base_classes) {
5331 // Check each base classes to see if it or any of its
5332 // base classes contain any fields. This can help
5333 // limit the noise in variable views by not having to
5334 // show base classes that contain no members.
5335 clang::CXXRecordDecl::base_class_const_iterator base_class,
5336 base_class_end;
5337 for (base_class = cxx_record_decl->bases_begin(),
5338 base_class_end = cxx_record_decl->bases_end();
5339 base_class != base_class_end; ++base_class) {
5340 const clang::CXXRecordDecl *base_class_decl =
5341 llvm::cast<clang::CXXRecordDecl>(
5342 base_class->getType()
5343 ->getAs<clang::RecordType>()
5344 ->getDecl());
5345
5346 // Skip empty base classes
5347 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5348 continue;
5349
5350 num_children++;
5351 }
5352 } else {
5353 // Include all base classes
5354 num_children += cxx_record_decl->getNumBases();
5355 }
5356 }
5357 clang::RecordDecl::field_iterator field, field_end;
5358 for (field = record_decl->field_begin(),
5359 field_end = record_decl->field_end();
5360 field != field_end; ++field)
5361 ++num_children;
5362 }
5363 break;
5364
5365 case clang::Type::ObjCObject:
5366 case clang::Type::ObjCInterface:
5367 if (GetCompleteQualType(getASTContext(), qual_type)) {
5368 const clang::ObjCObjectType *objc_class_type =
5369 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5370 assert(objc_class_type);
5371 if (objc_class_type) {
5372 clang::ObjCInterfaceDecl *class_interface_decl =
5373 objc_class_type->getInterface();
5374
5375 if (class_interface_decl) {
5376
5377 clang::ObjCInterfaceDecl *superclass_interface_decl =
5378 class_interface_decl->getSuperClass();
5379 if (superclass_interface_decl) {
5380 if (omit_empty_base_classes) {
5381 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5382 ++num_children;
5383 } else
5384 ++num_children;
5385 }
5386
5387 num_children += class_interface_decl->ivar_size();
5388 }
5389 }
5390 }
5391 break;
5392
5393 case clang::Type::ObjCObjectPointer: {
5394 const clang::ObjCObjectPointerType *pointer_type =
5395 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5396 clang::QualType pointee_type = pointer_type->getPointeeType();
5397 uint32_t num_pointee_children =
5398 CompilerType(getASTContext(), pointee_type)
5399 .GetNumChildren(omit_empty_base_classes);
5400 // If this type points to a simple type, then it has 1 child
5401 if (num_pointee_children == 0)
5402 num_children = 1;
5403 else
5404 num_children = num_pointee_children;
5405 } break;
5406
5407 case clang::Type::Vector:
5408 case clang::Type::ExtVector:
5409 num_children =
5410 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5411 break;
5412
5413 case clang::Type::ConstantArray:
5414 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5415 ->getSize()
5416 .getLimitedValue();
5417 break;
5418
5419 case clang::Type::Pointer: {
5420 const clang::PointerType *pointer_type =
5421 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5422 clang::QualType pointee_type(pointer_type->getPointeeType());
5423 uint32_t num_pointee_children =
5424 CompilerType(getASTContext(), pointee_type)
5425 .GetNumChildren(omit_empty_base_classes);
5426 if (num_pointee_children == 0) {
5427 // We have a pointer to a pointee type that claims it has no children.
5428 // We will want to look at
5429 num_children = GetNumPointeeChildren(pointee_type);
5430 } else
5431 num_children = num_pointee_children;
5432 } break;
5433
5434 case clang::Type::LValueReference:
5435 case clang::Type::RValueReference: {
5436 const clang::ReferenceType *reference_type =
5437 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5438 clang::QualType pointee_type = reference_type->getPointeeType();
5439 uint32_t num_pointee_children =
5440 CompilerType(getASTContext(), pointee_type)
5441 .GetNumChildren(omit_empty_base_classes);
5442 // If this type points to a simple type, then it has 1 child
5443 if (num_pointee_children == 0)
5444 num_children = 1;
5445 else
5446 num_children = num_pointee_children;
5447 } break;
5448
5449 case clang::Type::Typedef:
5450 num_children =
5451 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5452 ->getDecl()
5453 ->getUnderlyingType())
5454 .GetNumChildren(omit_empty_base_classes);
5455 break;
5456
5457 case clang::Type::Auto:
5458 num_children =
5459 CompilerType(getASTContext(),
5460 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5461 .GetNumChildren(omit_empty_base_classes);
5462 break;
5463
5464 case clang::Type::Elaborated:
5465 num_children =
5466 CompilerType(
5467 getASTContext(),
5468 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5469 .GetNumChildren(omit_empty_base_classes);
5470 break;
5471
5472 case clang::Type::Paren:
5473 num_children =
5474 CompilerType(getASTContext(),
5475 llvm::cast<clang::ParenType>(qual_type)->desugar())
5476 .GetNumChildren(omit_empty_base_classes);
5477 break;
5478 default:
5479 break;
5480 }
5481 return num_children;
5482}
5483
5484CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5485 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005486}
5487
Greg Claytond8d4a572015-08-11 21:38:15 +00005488lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005489ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5490 if (type) {
5491 clang::QualType qual_type(GetQualType(type));
5492 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5493 if (type_class == clang::Type::Builtin) {
5494 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5495 case clang::BuiltinType::Void:
5496 return eBasicTypeVoid;
5497 case clang::BuiltinType::Bool:
5498 return eBasicTypeBool;
5499 case clang::BuiltinType::Char_S:
5500 return eBasicTypeSignedChar;
5501 case clang::BuiltinType::Char_U:
5502 return eBasicTypeUnsignedChar;
5503 case clang::BuiltinType::Char16:
5504 return eBasicTypeChar16;
5505 case clang::BuiltinType::Char32:
5506 return eBasicTypeChar32;
5507 case clang::BuiltinType::UChar:
5508 return eBasicTypeUnsignedChar;
5509 case clang::BuiltinType::SChar:
5510 return eBasicTypeSignedChar;
5511 case clang::BuiltinType::WChar_S:
5512 return eBasicTypeSignedWChar;
5513 case clang::BuiltinType::WChar_U:
5514 return eBasicTypeUnsignedWChar;
5515 case clang::BuiltinType::Short:
5516 return eBasicTypeShort;
5517 case clang::BuiltinType::UShort:
5518 return eBasicTypeUnsignedShort;
5519 case clang::BuiltinType::Int:
5520 return eBasicTypeInt;
5521 case clang::BuiltinType::UInt:
5522 return eBasicTypeUnsignedInt;
5523 case clang::BuiltinType::Long:
5524 return eBasicTypeLong;
5525 case clang::BuiltinType::ULong:
5526 return eBasicTypeUnsignedLong;
5527 case clang::BuiltinType::LongLong:
5528 return eBasicTypeLongLong;
5529 case clang::BuiltinType::ULongLong:
5530 return eBasicTypeUnsignedLongLong;
5531 case clang::BuiltinType::Int128:
5532 return eBasicTypeInt128;
5533 case clang::BuiltinType::UInt128:
5534 return eBasicTypeUnsignedInt128;
5535
5536 case clang::BuiltinType::Half:
5537 return eBasicTypeHalf;
5538 case clang::BuiltinType::Float:
5539 return eBasicTypeFloat;
5540 case clang::BuiltinType::Double:
5541 return eBasicTypeDouble;
5542 case clang::BuiltinType::LongDouble:
5543 return eBasicTypeLongDouble;
5544
5545 case clang::BuiltinType::NullPtr:
5546 return eBasicTypeNullPtr;
5547 case clang::BuiltinType::ObjCId:
5548 return eBasicTypeObjCID;
5549 case clang::BuiltinType::ObjCClass:
5550 return eBasicTypeObjCClass;
5551 case clang::BuiltinType::ObjCSel:
5552 return eBasicTypeObjCSel;
5553 default:
5554 return eBasicTypeOther;
5555 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005556 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005557 }
5558 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005559}
5560
Kate Stoneb9c1b512016-09-06 20:57:50 +00005561void ClangASTContext::ForEachEnumerator(
5562 lldb::opaque_compiler_type_t type,
5563 std::function<bool(const CompilerType &integer_type,
5564 const ConstString &name,
5565 const llvm::APSInt &value)> const &callback) {
5566 const clang::EnumType *enum_type =
5567 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5568 if (enum_type) {
5569 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5570 if (enum_decl) {
5571 CompilerType integer_type(this,
5572 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005573
Kate Stoneb9c1b512016-09-06 20:57:50 +00005574 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5575 for (enum_pos = enum_decl->enumerator_begin(),
5576 enum_end_pos = enum_decl->enumerator_end();
5577 enum_pos != enum_end_pos; ++enum_pos) {
5578 ConstString name(enum_pos->getNameAsString().c_str());
5579 if (!callback(integer_type, name, enum_pos->getInitVal()))
5580 break;
5581 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005582 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005583 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005584}
5585
Greg Claytond8d4a572015-08-11 21:38:15 +00005586#pragma mark Aggregate Types
5587
Kate Stoneb9c1b512016-09-06 20:57:50 +00005588uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5589 if (!type)
5590 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005591
Kate Stoneb9c1b512016-09-06 20:57:50 +00005592 uint32_t count = 0;
5593 clang::QualType qual_type(GetCanonicalQualType(type));
5594 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5595 switch (type_class) {
5596 case clang::Type::Record:
5597 if (GetCompleteType(type)) {
5598 const clang::RecordType *record_type =
5599 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5600 if (record_type) {
5601 clang::RecordDecl *record_decl = record_type->getDecl();
5602 if (record_decl) {
5603 uint32_t field_idx = 0;
5604 clang::RecordDecl::field_iterator field, field_end;
5605 for (field = record_decl->field_begin(),
5606 field_end = record_decl->field_end();
5607 field != field_end; ++field)
5608 ++field_idx;
5609 count = field_idx;
5610 }
5611 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005612 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005613 break;
5614
5615 case clang::Type::Typedef:
5616 count =
5617 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5618 ->getDecl()
5619 ->getUnderlyingType())
5620 .GetNumFields();
5621 break;
5622
5623 case clang::Type::Auto:
5624 count =
5625 CompilerType(getASTContext(),
5626 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5627 .GetNumFields();
5628 break;
5629
5630 case clang::Type::Elaborated:
5631 count = CompilerType(
5632 getASTContext(),
5633 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5634 .GetNumFields();
5635 break;
5636
5637 case clang::Type::Paren:
5638 count = CompilerType(getASTContext(),
5639 llvm::cast<clang::ParenType>(qual_type)->desugar())
5640 .GetNumFields();
5641 break;
5642
Sean Callananf9c622a2016-09-30 18:44:43 +00005643 case clang::Type::ObjCObjectPointer: {
5644 const clang::ObjCObjectPointerType *objc_class_type =
5645 qual_type->getAsObjCInterfacePointerType();
5646 const clang::ObjCInterfaceType *objc_interface_type =
5647 objc_class_type->getInterfaceType();
5648 if (objc_interface_type &&
5649 GetCompleteType((lldb::opaque_compiler_type_t)objc_interface_type)) {
5650 clang::ObjCInterfaceDecl *class_interface_decl =
5651 objc_interface_type->getDecl();
5652 if (class_interface_decl) {
5653 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005654 }
5655 }
5656 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005657 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005658
5659 case clang::Type::ObjCObject:
5660 case clang::Type::ObjCInterface:
5661 if (GetCompleteType(type)) {
5662 const clang::ObjCObjectType *objc_class_type =
5663 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5664 if (objc_class_type) {
5665 clang::ObjCInterfaceDecl *class_interface_decl =
5666 objc_class_type->getInterface();
5667
5668 if (class_interface_decl)
5669 count = class_interface_decl->ivar_size();
5670 }
5671 }
5672 break;
5673
5674 default:
5675 break;
5676 }
5677 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005678}
5679
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005680static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005681GetObjCFieldAtIndex(clang::ASTContext *ast,
5682 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5683 std::string &name, uint64_t *bit_offset_ptr,
5684 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5685 if (class_interface_decl) {
5686 if (idx < (class_interface_decl->ivar_size())) {
5687 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5688 ivar_end = class_interface_decl->ivar_end();
5689 uint32_t ivar_idx = 0;
5690
5691 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5692 ++ivar_pos, ++ivar_idx) {
5693 if (ivar_idx == idx) {
5694 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5695
5696 clang::QualType ivar_qual_type(ivar_decl->getType());
5697
5698 name.assign(ivar_decl->getNameAsString());
5699
5700 if (bit_offset_ptr) {
5701 const clang::ASTRecordLayout &interface_layout =
5702 ast->getASTObjCInterfaceLayout(class_interface_decl);
5703 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5704 }
5705
5706 const bool is_bitfield = ivar_pos->isBitField();
5707
5708 if (bitfield_bit_size_ptr) {
5709 *bitfield_bit_size_ptr = 0;
5710
5711 if (is_bitfield && ast) {
5712 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5713 llvm::APSInt bitfield_apsint;
5714 if (bitfield_bit_size_expr &&
5715 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5716 *ast)) {
5717 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5718 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005719 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005720 }
5721 if (is_bitfield_ptr)
5722 *is_bitfield_ptr = is_bitfield;
5723
5724 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005725 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005726 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005727 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005728 }
5729 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005730}
5731
Kate Stoneb9c1b512016-09-06 20:57:50 +00005732CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5733 size_t idx, std::string &name,
5734 uint64_t *bit_offset_ptr,
5735 uint32_t *bitfield_bit_size_ptr,
5736 bool *is_bitfield_ptr) {
5737 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005738 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005739
5740 clang::QualType qual_type(GetCanonicalQualType(type));
5741 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5742 switch (type_class) {
5743 case clang::Type::Record:
5744 if (GetCompleteType(type)) {
5745 const clang::RecordType *record_type =
5746 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5747 const clang::RecordDecl *record_decl = record_type->getDecl();
5748 uint32_t field_idx = 0;
5749 clang::RecordDecl::field_iterator field, field_end;
5750 for (field = record_decl->field_begin(),
5751 field_end = record_decl->field_end();
5752 field != field_end; ++field, ++field_idx) {
5753 if (idx == field_idx) {
5754 // Print the member type if requested
5755 // Print the member name and equal sign
5756 name.assign(field->getNameAsString());
5757
5758 // Figure out the type byte size (field_type_info.first) and
5759 // alignment (field_type_info.second) from the AST context.
5760 if (bit_offset_ptr) {
5761 const clang::ASTRecordLayout &record_layout =
5762 getASTContext()->getASTRecordLayout(record_decl);
5763 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5764 }
5765
5766 const bool is_bitfield = field->isBitField();
5767
5768 if (bitfield_bit_size_ptr) {
5769 *bitfield_bit_size_ptr = 0;
5770
5771 if (is_bitfield) {
5772 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5773 llvm::APSInt bitfield_apsint;
5774 if (bitfield_bit_size_expr &&
5775 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5776 *getASTContext())) {
5777 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5778 }
5779 }
5780 }
5781 if (is_bitfield_ptr)
5782 *is_bitfield_ptr = is_bitfield;
5783
5784 return CompilerType(getASTContext(), field->getType());
5785 }
5786 }
5787 }
5788 break;
5789
Sean Callananf9c622a2016-09-30 18:44:43 +00005790 case clang::Type::ObjCObjectPointer: {
5791 const clang::ObjCObjectPointerType *objc_class_type =
5792 qual_type->getAsObjCInterfacePointerType();
5793 const clang::ObjCInterfaceType *objc_interface_type =
5794 objc_class_type->getInterfaceType();
5795 if (objc_interface_type &&
5796 GetCompleteType((lldb::opaque_compiler_type_t)objc_interface_type)) {
5797 clang::ObjCInterfaceDecl *class_interface_decl =
5798 objc_interface_type->getDecl();
5799 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005800 return CompilerType(
5801 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5802 idx, name, bit_offset_ptr,
5803 bitfield_bit_size_ptr, is_bitfield_ptr));
5804 }
5805 }
5806 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005807 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005808
5809 case clang::Type::ObjCObject:
5810 case clang::Type::ObjCInterface:
5811 if (GetCompleteType(type)) {
5812 const clang::ObjCObjectType *objc_class_type =
5813 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5814 assert(objc_class_type);
5815 if (objc_class_type) {
5816 clang::ObjCInterfaceDecl *class_interface_decl =
5817 objc_class_type->getInterface();
5818 return CompilerType(
5819 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5820 idx, name, bit_offset_ptr,
5821 bitfield_bit_size_ptr, is_bitfield_ptr));
5822 }
5823 }
5824 break;
5825
5826 case clang::Type::Typedef:
5827 return CompilerType(getASTContext(),
5828 llvm::cast<clang::TypedefType>(qual_type)
5829 ->getDecl()
5830 ->getUnderlyingType())
5831 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5832 is_bitfield_ptr);
5833
5834 case clang::Type::Auto:
5835 return CompilerType(
5836 getASTContext(),
5837 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5838 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5839 is_bitfield_ptr);
5840
5841 case clang::Type::Elaborated:
5842 return CompilerType(
5843 getASTContext(),
5844 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5845 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5846 is_bitfield_ptr);
5847
5848 case clang::Type::Paren:
5849 return CompilerType(getASTContext(),
5850 llvm::cast<clang::ParenType>(qual_type)->desugar())
5851 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5852 is_bitfield_ptr);
5853
5854 default:
5855 break;
5856 }
5857 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005858}
5859
Greg Clayton99558cc42015-08-24 23:46:31 +00005860uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005861ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
5862 uint32_t count = 0;
5863 clang::QualType qual_type(GetCanonicalQualType(type));
5864 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5865 switch (type_class) {
5866 case clang::Type::Record:
5867 if (GetCompleteType(type)) {
5868 const clang::CXXRecordDecl *cxx_record_decl =
5869 qual_type->getAsCXXRecordDecl();
5870 if (cxx_record_decl)
5871 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00005872 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005873 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00005874
Kate Stoneb9c1b512016-09-06 20:57:50 +00005875 case clang::Type::ObjCObjectPointer:
5876 count = GetPointeeType(type).GetNumDirectBaseClasses();
5877 break;
5878
5879 case clang::Type::ObjCObject:
5880 if (GetCompleteType(type)) {
5881 const clang::ObjCObjectType *objc_class_type =
5882 qual_type->getAsObjCQualifiedInterfaceType();
5883 if (objc_class_type) {
5884 clang::ObjCInterfaceDecl *class_interface_decl =
5885 objc_class_type->getInterface();
5886
5887 if (class_interface_decl && class_interface_decl->getSuperClass())
5888 count = 1;
5889 }
5890 }
5891 break;
5892 case clang::Type::ObjCInterface:
5893 if (GetCompleteType(type)) {
5894 const clang::ObjCInterfaceType *objc_interface_type =
5895 qual_type->getAs<clang::ObjCInterfaceType>();
5896 if (objc_interface_type) {
5897 clang::ObjCInterfaceDecl *class_interface_decl =
5898 objc_interface_type->getInterface();
5899
5900 if (class_interface_decl && class_interface_decl->getSuperClass())
5901 count = 1;
5902 }
5903 }
5904 break;
5905
5906 case clang::Type::Typedef:
5907 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5908 ->getDecl()
5909 ->getUnderlyingType()
5910 .getAsOpaquePtr());
5911 break;
5912
5913 case clang::Type::Auto:
5914 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5915 ->getDeducedType()
5916 .getAsOpaquePtr());
5917 break;
5918
5919 case clang::Type::Elaborated:
5920 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5921 ->getNamedType()
5922 .getAsOpaquePtr());
5923 break;
5924
5925 case clang::Type::Paren:
5926 return GetNumDirectBaseClasses(
5927 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5928
5929 default:
5930 break;
5931 }
5932 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00005933}
5934
5935uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005936ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
5937 uint32_t count = 0;
5938 clang::QualType qual_type(GetCanonicalQualType(type));
5939 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5940 switch (type_class) {
5941 case clang::Type::Record:
5942 if (GetCompleteType(type)) {
5943 const clang::CXXRecordDecl *cxx_record_decl =
5944 qual_type->getAsCXXRecordDecl();
5945 if (cxx_record_decl)
5946 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00005947 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005948 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00005949
Kate Stoneb9c1b512016-09-06 20:57:50 +00005950 case clang::Type::Typedef:
5951 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5952 ->getDecl()
5953 ->getUnderlyingType()
5954 .getAsOpaquePtr());
5955 break;
5956
5957 case clang::Type::Auto:
5958 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5959 ->getDeducedType()
5960 .getAsOpaquePtr());
5961 break;
5962
5963 case clang::Type::Elaborated:
5964 count =
5965 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5966 ->getNamedType()
5967 .getAsOpaquePtr());
5968 break;
5969
5970 case clang::Type::Paren:
5971 count = GetNumVirtualBaseClasses(
5972 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5973 break;
5974
5975 default:
5976 break;
5977 }
5978 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00005979}
5980
Kate Stoneb9c1b512016-09-06 20:57:50 +00005981CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
5982 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
5983 clang::QualType qual_type(GetCanonicalQualType(type));
5984 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5985 switch (type_class) {
5986 case clang::Type::Record:
5987 if (GetCompleteType(type)) {
5988 const clang::CXXRecordDecl *cxx_record_decl =
5989 qual_type->getAsCXXRecordDecl();
5990 if (cxx_record_decl) {
5991 uint32_t curr_idx = 0;
5992 clang::CXXRecordDecl::base_class_const_iterator base_class,
5993 base_class_end;
5994 for (base_class = cxx_record_decl->bases_begin(),
5995 base_class_end = cxx_record_decl->bases_end();
5996 base_class != base_class_end; ++base_class, ++curr_idx) {
5997 if (curr_idx == idx) {
5998 if (bit_offset_ptr) {
5999 const clang::ASTRecordLayout &record_layout =
6000 getASTContext()->getASTRecordLayout(cxx_record_decl);
6001 const clang::CXXRecordDecl *base_class_decl =
6002 llvm::cast<clang::CXXRecordDecl>(
6003 base_class->getType()
6004 ->getAs<clang::RecordType>()
6005 ->getDecl());
6006 if (base_class->isVirtual())
6007 *bit_offset_ptr =
6008 record_layout.getVBaseClassOffset(base_class_decl)
6009 .getQuantity() *
6010 8;
6011 else
6012 *bit_offset_ptr =
6013 record_layout.getBaseClassOffset(base_class_decl)
6014 .getQuantity() *
6015 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006016 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006017 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6018 }
6019 }
6020 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006021 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006022 break;
6023
6024 case clang::Type::ObjCObjectPointer:
6025 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6026
6027 case clang::Type::ObjCObject:
6028 if (idx == 0 && GetCompleteType(type)) {
6029 const clang::ObjCObjectType *objc_class_type =
6030 qual_type->getAsObjCQualifiedInterfaceType();
6031 if (objc_class_type) {
6032 clang::ObjCInterfaceDecl *class_interface_decl =
6033 objc_class_type->getInterface();
6034
6035 if (class_interface_decl) {
6036 clang::ObjCInterfaceDecl *superclass_interface_decl =
6037 class_interface_decl->getSuperClass();
6038 if (superclass_interface_decl) {
6039 if (bit_offset_ptr)
6040 *bit_offset_ptr = 0;
6041 return CompilerType(getASTContext(),
6042 getASTContext()->getObjCInterfaceType(
6043 superclass_interface_decl));
6044 }
6045 }
6046 }
6047 }
6048 break;
6049 case clang::Type::ObjCInterface:
6050 if (idx == 0 && GetCompleteType(type)) {
6051 const clang::ObjCObjectType *objc_interface_type =
6052 qual_type->getAs<clang::ObjCInterfaceType>();
6053 if (objc_interface_type) {
6054 clang::ObjCInterfaceDecl *class_interface_decl =
6055 objc_interface_type->getInterface();
6056
6057 if (class_interface_decl) {
6058 clang::ObjCInterfaceDecl *superclass_interface_decl =
6059 class_interface_decl->getSuperClass();
6060 if (superclass_interface_decl) {
6061 if (bit_offset_ptr)
6062 *bit_offset_ptr = 0;
6063 return CompilerType(getASTContext(),
6064 getASTContext()->getObjCInterfaceType(
6065 superclass_interface_decl));
6066 }
6067 }
6068 }
6069 }
6070 break;
6071
6072 case clang::Type::Typedef:
6073 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6074 ->getDecl()
6075 ->getUnderlyingType()
6076 .getAsOpaquePtr(),
6077 idx, bit_offset_ptr);
6078
6079 case clang::Type::Auto:
6080 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6081 ->getDeducedType()
6082 .getAsOpaquePtr(),
6083 idx, bit_offset_ptr);
6084
6085 case clang::Type::Elaborated:
6086 return GetDirectBaseClassAtIndex(
6087 llvm::cast<clang::ElaboratedType>(qual_type)
6088 ->getNamedType()
6089 .getAsOpaquePtr(),
6090 idx, bit_offset_ptr);
6091
6092 case clang::Type::Paren:
6093 return GetDirectBaseClassAtIndex(
6094 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6095 idx, bit_offset_ptr);
6096
6097 default:
6098 break;
6099 }
6100 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006101}
6102
Kate Stoneb9c1b512016-09-06 20:57:50 +00006103CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6104 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6105 clang::QualType qual_type(GetCanonicalQualType(type));
6106 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6107 switch (type_class) {
6108 case clang::Type::Record:
6109 if (GetCompleteType(type)) {
6110 const clang::CXXRecordDecl *cxx_record_decl =
6111 qual_type->getAsCXXRecordDecl();
6112 if (cxx_record_decl) {
6113 uint32_t curr_idx = 0;
6114 clang::CXXRecordDecl::base_class_const_iterator base_class,
6115 base_class_end;
6116 for (base_class = cxx_record_decl->vbases_begin(),
6117 base_class_end = cxx_record_decl->vbases_end();
6118 base_class != base_class_end; ++base_class, ++curr_idx) {
6119 if (curr_idx == idx) {
6120 if (bit_offset_ptr) {
6121 const clang::ASTRecordLayout &record_layout =
6122 getASTContext()->getASTRecordLayout(cxx_record_decl);
6123 const clang::CXXRecordDecl *base_class_decl =
6124 llvm::cast<clang::CXXRecordDecl>(
6125 base_class->getType()
6126 ->getAs<clang::RecordType>()
6127 ->getDecl());
6128 *bit_offset_ptr =
6129 record_layout.getVBaseClassOffset(base_class_decl)
6130 .getQuantity() *
6131 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006132 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006133 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6134 }
6135 }
6136 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006137 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006138 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006139
Kate Stoneb9c1b512016-09-06 20:57:50 +00006140 case clang::Type::Typedef:
6141 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6142 ->getDecl()
6143 ->getUnderlyingType()
6144 .getAsOpaquePtr(),
6145 idx, bit_offset_ptr);
6146
6147 case clang::Type::Auto:
6148 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6149 ->getDeducedType()
6150 .getAsOpaquePtr(),
6151 idx, bit_offset_ptr);
6152
6153 case clang::Type::Elaborated:
6154 return GetVirtualBaseClassAtIndex(
6155 llvm::cast<clang::ElaboratedType>(qual_type)
6156 ->getNamedType()
6157 .getAsOpaquePtr(),
6158 idx, bit_offset_ptr);
6159
6160 case clang::Type::Paren:
6161 return GetVirtualBaseClassAtIndex(
6162 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6163 idx, bit_offset_ptr);
6164
6165 default:
6166 break;
6167 }
6168 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006169}
6170
Greg Claytond8d4a572015-08-11 21:38:15 +00006171// If a pointer to a pointee type (the clang_type arg) says that it has no
6172// children, then we either need to trust it, or override it and return a
6173// different result. For example, an "int *" has one child that is an integer,
6174// but a function pointer doesn't have any children. Likewise if a Record type
6175// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006176uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6177 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006178 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006179
6180 clang::QualType qual_type(type.getCanonicalType());
6181 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6182 switch (type_class) {
6183 case clang::Type::Builtin:
6184 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6185 case clang::BuiltinType::UnknownAny:
6186 case clang::BuiltinType::Void:
6187 case clang::BuiltinType::NullPtr:
6188 case clang::BuiltinType::OCLEvent:
6189 case clang::BuiltinType::OCLImage1dRO:
6190 case clang::BuiltinType::OCLImage1dWO:
6191 case clang::BuiltinType::OCLImage1dRW:
6192 case clang::BuiltinType::OCLImage1dArrayRO:
6193 case clang::BuiltinType::OCLImage1dArrayWO:
6194 case clang::BuiltinType::OCLImage1dArrayRW:
6195 case clang::BuiltinType::OCLImage1dBufferRO:
6196 case clang::BuiltinType::OCLImage1dBufferWO:
6197 case clang::BuiltinType::OCLImage1dBufferRW:
6198 case clang::BuiltinType::OCLImage2dRO:
6199 case clang::BuiltinType::OCLImage2dWO:
6200 case clang::BuiltinType::OCLImage2dRW:
6201 case clang::BuiltinType::OCLImage2dArrayRO:
6202 case clang::BuiltinType::OCLImage2dArrayWO:
6203 case clang::BuiltinType::OCLImage2dArrayRW:
6204 case clang::BuiltinType::OCLImage3dRO:
6205 case clang::BuiltinType::OCLImage3dWO:
6206 case clang::BuiltinType::OCLImage3dRW:
6207 case clang::BuiltinType::OCLSampler:
6208 return 0;
6209 case clang::BuiltinType::Bool:
6210 case clang::BuiltinType::Char_U:
6211 case clang::BuiltinType::UChar:
6212 case clang::BuiltinType::WChar_U:
6213 case clang::BuiltinType::Char16:
6214 case clang::BuiltinType::Char32:
6215 case clang::BuiltinType::UShort:
6216 case clang::BuiltinType::UInt:
6217 case clang::BuiltinType::ULong:
6218 case clang::BuiltinType::ULongLong:
6219 case clang::BuiltinType::UInt128:
6220 case clang::BuiltinType::Char_S:
6221 case clang::BuiltinType::SChar:
6222 case clang::BuiltinType::WChar_S:
6223 case clang::BuiltinType::Short:
6224 case clang::BuiltinType::Int:
6225 case clang::BuiltinType::Long:
6226 case clang::BuiltinType::LongLong:
6227 case clang::BuiltinType::Int128:
6228 case clang::BuiltinType::Float:
6229 case clang::BuiltinType::Double:
6230 case clang::BuiltinType::LongDouble:
6231 case clang::BuiltinType::Dependent:
6232 case clang::BuiltinType::Overload:
6233 case clang::BuiltinType::ObjCId:
6234 case clang::BuiltinType::ObjCClass:
6235 case clang::BuiltinType::ObjCSel:
6236 case clang::BuiltinType::BoundMember:
6237 case clang::BuiltinType::Half:
6238 case clang::BuiltinType::ARCUnbridgedCast:
6239 case clang::BuiltinType::PseudoObject:
6240 case clang::BuiltinType::BuiltinFn:
6241 case clang::BuiltinType::OMPArraySection:
6242 return 1;
6243 default:
6244 return 0;
6245 }
6246 break;
6247
6248 case clang::Type::Complex:
6249 return 1;
6250 case clang::Type::Pointer:
6251 return 1;
6252 case clang::Type::BlockPointer:
6253 return 0; // If block pointers don't have debug info, then no children for
6254 // them
6255 case clang::Type::LValueReference:
6256 return 1;
6257 case clang::Type::RValueReference:
6258 return 1;
6259 case clang::Type::MemberPointer:
6260 return 0;
6261 case clang::Type::ConstantArray:
6262 return 0;
6263 case clang::Type::IncompleteArray:
6264 return 0;
6265 case clang::Type::VariableArray:
6266 return 0;
6267 case clang::Type::DependentSizedArray:
6268 return 0;
6269 case clang::Type::DependentSizedExtVector:
6270 return 0;
6271 case clang::Type::Vector:
6272 return 0;
6273 case clang::Type::ExtVector:
6274 return 0;
6275 case clang::Type::FunctionProto:
6276 return 0; // When we function pointers, they have no children...
6277 case clang::Type::FunctionNoProto:
6278 return 0; // When we function pointers, they have no children...
6279 case clang::Type::UnresolvedUsing:
6280 return 0;
6281 case clang::Type::Paren:
6282 return GetNumPointeeChildren(
6283 llvm::cast<clang::ParenType>(qual_type)->desugar());
6284 case clang::Type::Typedef:
6285 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6286 ->getDecl()
6287 ->getUnderlyingType());
6288 case clang::Type::Auto:
6289 return GetNumPointeeChildren(
6290 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6291 case clang::Type::Elaborated:
6292 return GetNumPointeeChildren(
6293 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6294 case clang::Type::TypeOfExpr:
6295 return 0;
6296 case clang::Type::TypeOf:
6297 return 0;
6298 case clang::Type::Decltype:
6299 return 0;
6300 case clang::Type::Record:
6301 return 0;
6302 case clang::Type::Enum:
6303 return 1;
6304 case clang::Type::TemplateTypeParm:
6305 return 1;
6306 case clang::Type::SubstTemplateTypeParm:
6307 return 1;
6308 case clang::Type::TemplateSpecialization:
6309 return 1;
6310 case clang::Type::InjectedClassName:
6311 return 0;
6312 case clang::Type::DependentName:
6313 return 1;
6314 case clang::Type::DependentTemplateSpecialization:
6315 return 1;
6316 case clang::Type::ObjCObject:
6317 return 0;
6318 case clang::Type::ObjCInterface:
6319 return 0;
6320 case clang::Type::ObjCObjectPointer:
6321 return 1;
6322 default:
6323 break;
6324 }
6325 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006326}
6327
Kate Stoneb9c1b512016-09-06 20:57:50 +00006328CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6329 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6330 bool transparent_pointers, bool omit_empty_base_classes,
6331 bool ignore_array_bounds, std::string &child_name,
6332 uint32_t &child_byte_size, int32_t &child_byte_offset,
6333 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6334 bool &child_is_base_class, bool &child_is_deref_of_parent,
6335 ValueObject *valobj, uint64_t &language_flags) {
6336 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006337 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006338
Kate Stoneb9c1b512016-09-06 20:57:50 +00006339 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6340 const clang::Type::TypeClass parent_type_class =
6341 parent_qual_type->getTypeClass();
6342 child_bitfield_bit_size = 0;
6343 child_bitfield_bit_offset = 0;
6344 child_is_base_class = false;
6345 language_flags = 0;
6346
6347 const bool idx_is_valid = idx < GetNumChildren(type, omit_empty_base_classes);
6348 uint32_t bit_offset;
6349 switch (parent_type_class) {
6350 case clang::Type::Builtin:
6351 if (idx_is_valid) {
6352 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6353 case clang::BuiltinType::ObjCId:
6354 case clang::BuiltinType::ObjCClass:
6355 child_name = "isa";
6356 child_byte_size =
6357 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6358 CHAR_BIT;
6359 return CompilerType(getASTContext(),
6360 getASTContext()->ObjCBuiltinClassTy);
6361
6362 default:
6363 break;
6364 }
6365 }
6366 break;
6367
6368 case clang::Type::Record:
6369 if (idx_is_valid && GetCompleteType(type)) {
6370 const clang::RecordType *record_type =
6371 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6372 const clang::RecordDecl *record_decl = record_type->getDecl();
6373 assert(record_decl);
6374 const clang::ASTRecordLayout &record_layout =
6375 getASTContext()->getASTRecordLayout(record_decl);
6376 uint32_t child_idx = 0;
6377
6378 const clang::CXXRecordDecl *cxx_record_decl =
6379 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6380 if (cxx_record_decl) {
6381 // We might have base classes to print out first
6382 clang::CXXRecordDecl::base_class_const_iterator base_class,
6383 base_class_end;
6384 for (base_class = cxx_record_decl->bases_begin(),
6385 base_class_end = cxx_record_decl->bases_end();
6386 base_class != base_class_end; ++base_class) {
6387 const clang::CXXRecordDecl *base_class_decl = nullptr;
6388
6389 // Skip empty base classes
6390 if (omit_empty_base_classes) {
6391 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6392 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6393 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
6394 continue;
6395 }
6396
6397 if (idx == child_idx) {
6398 if (base_class_decl == nullptr)
6399 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6400 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6401
6402 if (base_class->isVirtual()) {
6403 bool handled = false;
6404 if (valobj) {
6405 Error err;
6406 AddressType addr_type = eAddressTypeInvalid;
6407 lldb::addr_t vtable_ptr_addr =
6408 valobj->GetCPPVTableAddress(addr_type);
6409
6410 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS &&
6411 addr_type == eAddressTypeLoad) {
6412
6413 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
6414 Process *process = exe_ctx.GetProcessPtr();
6415 if (process) {
6416 clang::VTableContextBase *vtable_ctx =
6417 getASTContext()->getVTableContext();
6418 if (vtable_ctx) {
6419 if (vtable_ctx->isMicrosoft()) {
6420 clang::MicrosoftVTableContext *msoft_vtable_ctx =
6421 static_cast<clang::MicrosoftVTableContext *>(
6422 vtable_ctx);
6423
6424 if (vtable_ptr_addr) {
6425 const lldb::addr_t vbtable_ptr_addr =
6426 vtable_ptr_addr +
6427 record_layout.getVBPtrOffset().getQuantity();
6428
6429 const lldb::addr_t vbtable_ptr =
6430 process->ReadPointerFromMemory(vbtable_ptr_addr,
6431 err);
6432 if (vbtable_ptr != LLDB_INVALID_ADDRESS) {
6433 // Get the index into the virtual base table. The
6434 // index is the index in uint32_t from vbtable_ptr
6435 const unsigned vbtable_index =
6436 msoft_vtable_ctx->getVBTableIndex(
6437 cxx_record_decl, base_class_decl);
6438 const lldb::addr_t base_offset_addr =
6439 vbtable_ptr + vbtable_index * 4;
6440 const uint32_t base_offset =
6441 process->ReadUnsignedIntegerFromMemory(
6442 base_offset_addr, 4, UINT32_MAX, err);
6443 if (base_offset != UINT32_MAX) {
6444 handled = true;
6445 bit_offset = base_offset * 8;
6446 }
6447 }
6448 }
6449 } else {
6450 clang::ItaniumVTableContext *itanium_vtable_ctx =
6451 static_cast<clang::ItaniumVTableContext *>(
6452 vtable_ctx);
6453 if (vtable_ptr_addr) {
6454 const lldb::addr_t vtable_ptr =
6455 process->ReadPointerFromMemory(vtable_ptr_addr,
6456 err);
6457 if (vtable_ptr != LLDB_INVALID_ADDRESS) {
6458 clang::CharUnits base_offset_offset =
6459 itanium_vtable_ctx->getVirtualBaseOffsetOffset(
6460 cxx_record_decl, base_class_decl);
6461 const lldb::addr_t base_offset_addr =
6462 vtable_ptr + base_offset_offset.getQuantity();
6463 const uint32_t base_offset_size =
6464 process->GetAddressByteSize();
6465 const uint64_t base_offset =
6466 process->ReadUnsignedIntegerFromMemory(
6467 base_offset_addr, base_offset_size,
6468 UINT32_MAX, err);
6469 if (base_offset < UINT32_MAX) {
6470 handled = true;
6471 bit_offset = base_offset * 8;
6472 }
6473 }
6474 }
6475 }
6476 }
6477 }
6478 }
6479 }
6480 if (!handled)
6481 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6482 .getQuantity() *
6483 8;
6484 } else
6485 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6486 .getQuantity() *
6487 8;
6488
6489 // Base classes should be a multiple of 8 bits in size
6490 child_byte_offset = bit_offset / 8;
6491 CompilerType base_class_clang_type(getASTContext(),
6492 base_class->getType());
6493 child_name = base_class_clang_type.GetTypeName().AsCString("");
6494 uint64_t base_class_clang_type_bit_size =
6495 base_class_clang_type.GetBitSize(
6496 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6497
6498 // Base classes bit sizes should be a multiple of 8 bits in size
6499 assert(base_class_clang_type_bit_size % 8 == 0);
6500 child_byte_size = base_class_clang_type_bit_size / 8;
6501 child_is_base_class = true;
6502 return base_class_clang_type;
6503 }
6504 // We don't increment the child index in the for loop since we might
6505 // be skipping empty base classes
6506 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006507 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006508 }
6509 // Make sure index is in range...
6510 uint32_t field_idx = 0;
6511 clang::RecordDecl::field_iterator field, field_end;
6512 for (field = record_decl->field_begin(),
6513 field_end = record_decl->field_end();
6514 field != field_end; ++field, ++field_idx, ++child_idx) {
6515 if (idx == child_idx) {
6516 // Print the member type if requested
6517 // Print the member name and equal sign
6518 child_name.assign(field->getNameAsString().c_str());
6519
6520 // Figure out the type byte size (field_type_info.first) and
6521 // alignment (field_type_info.second) from the AST context.
6522 CompilerType field_clang_type(getASTContext(), field->getType());
6523 assert(field_idx < record_layout.getFieldCount());
6524 child_byte_size = field_clang_type.GetByteSize(
6525 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6526 const uint32_t child_bit_size = child_byte_size * 8;
6527
6528 // Figure out the field offset within the current struct/union/class
6529 // type
6530 bit_offset = record_layout.getFieldOffset(field_idx);
6531 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6532 child_bitfield_bit_size)) {
6533 child_bitfield_bit_offset = bit_offset % child_bit_size;
6534 const uint32_t child_bit_offset =
6535 bit_offset - child_bitfield_bit_offset;
6536 child_byte_offset = child_bit_offset / 8;
6537 } else {
6538 child_byte_offset = bit_offset / 8;
6539 }
6540
6541 return field_clang_type;
6542 }
6543 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006544 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006545 break;
6546
6547 case clang::Type::ObjCObject:
6548 case clang::Type::ObjCInterface:
6549 if (idx_is_valid && GetCompleteType(type)) {
6550 const clang::ObjCObjectType *objc_class_type =
6551 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6552 assert(objc_class_type);
6553 if (objc_class_type) {
6554 uint32_t child_idx = 0;
6555 clang::ObjCInterfaceDecl *class_interface_decl =
6556 objc_class_type->getInterface();
6557
6558 if (class_interface_decl) {
6559
6560 const clang::ASTRecordLayout &interface_layout =
6561 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6562 clang::ObjCInterfaceDecl *superclass_interface_decl =
6563 class_interface_decl->getSuperClass();
6564 if (superclass_interface_decl) {
6565 if (omit_empty_base_classes) {
6566 CompilerType base_class_clang_type(
6567 getASTContext(), getASTContext()->getObjCInterfaceType(
6568 superclass_interface_decl));
6569 if (base_class_clang_type.GetNumChildren(
6570 omit_empty_base_classes) > 0) {
6571 if (idx == 0) {
6572 clang::QualType ivar_qual_type(
6573 getASTContext()->getObjCInterfaceType(
6574 superclass_interface_decl));
6575
6576 child_name.assign(
6577 superclass_interface_decl->getNameAsString().c_str());
6578
6579 clang::TypeInfo ivar_type_info =
6580 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6581
6582 child_byte_size = ivar_type_info.Width / 8;
6583 child_byte_offset = 0;
6584 child_is_base_class = true;
6585
6586 return CompilerType(getASTContext(), ivar_qual_type);
6587 }
6588
6589 ++child_idx;
6590 }
6591 } else
6592 ++child_idx;
6593 }
6594
6595 const uint32_t superclass_idx = child_idx;
6596
6597 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6598 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6599 ivar_end = class_interface_decl->ivar_end();
6600
6601 for (ivar_pos = class_interface_decl->ivar_begin();
6602 ivar_pos != ivar_end; ++ivar_pos) {
6603 if (child_idx == idx) {
6604 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6605
6606 clang::QualType ivar_qual_type(ivar_decl->getType());
6607
6608 child_name.assign(ivar_decl->getNameAsString().c_str());
6609
6610 clang::TypeInfo ivar_type_info =
6611 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6612
6613 child_byte_size = ivar_type_info.Width / 8;
6614
6615 // Figure out the field offset within the current
6616 // struct/union/class type
6617 // For ObjC objects, we can't trust the bit offset we get from
6618 // the Clang AST, since
6619 // that doesn't account for the space taken up by unbacked
6620 // properties, or from
6621 // the changing size of base classes that are newer than this
6622 // class.
6623 // So if we have a process around that we can ask about this
6624 // object, do so.
6625 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6626 Process *process = nullptr;
6627 if (exe_ctx)
6628 process = exe_ctx->GetProcessPtr();
6629 if (process) {
6630 ObjCLanguageRuntime *objc_runtime =
6631 process->GetObjCLanguageRuntime();
6632 if (objc_runtime != nullptr) {
6633 CompilerType parent_ast_type(getASTContext(),
6634 parent_qual_type);
6635 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6636 parent_ast_type, ivar_decl->getNameAsString().c_str());
6637 }
6638 }
6639
6640 // Setting this to UINT32_MAX to make sure we don't compute it
6641 // twice...
6642 bit_offset = UINT32_MAX;
6643
6644 if (child_byte_offset ==
6645 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6646 bit_offset = interface_layout.getFieldOffset(child_idx -
6647 superclass_idx);
6648 child_byte_offset = bit_offset / 8;
6649 }
6650
6651 // Note, the ObjC Ivar Byte offset is just that, it doesn't
6652 // account for the bit offset
6653 // of a bitfield within its containing object. So regardless of
6654 // where we get the byte
6655 // offset from, we still need to get the bit offset for
6656 // bitfields from the layout.
6657
6658 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6659 child_bitfield_bit_size)) {
6660 if (bit_offset == UINT32_MAX)
6661 bit_offset = interface_layout.getFieldOffset(
6662 child_idx - superclass_idx);
6663
6664 child_bitfield_bit_offset = bit_offset % 8;
6665 }
6666 return CompilerType(getASTContext(), ivar_qual_type);
6667 }
6668 ++child_idx;
6669 }
6670 }
6671 }
6672 }
6673 }
6674 break;
6675
6676 case clang::Type::ObjCObjectPointer:
6677 if (idx_is_valid) {
6678 CompilerType pointee_clang_type(GetPointeeType(type));
6679
6680 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6681 child_is_deref_of_parent = false;
6682 bool tmp_child_is_deref_of_parent = false;
6683 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6684 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6685 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6686 child_bitfield_bit_size, child_bitfield_bit_offset,
6687 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6688 language_flags);
6689 } else {
6690 child_is_deref_of_parent = true;
6691 const char *parent_name =
6692 valobj ? valobj->GetName().GetCString() : NULL;
6693 if (parent_name) {
6694 child_name.assign(1, '*');
6695 child_name += parent_name;
6696 }
6697
6698 // We have a pointer to an simple type
6699 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
6700 child_byte_size = pointee_clang_type.GetByteSize(
6701 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6702 child_byte_offset = 0;
6703 return pointee_clang_type;
6704 }
6705 }
6706 }
6707 break;
6708
6709 case clang::Type::Vector:
6710 case clang::Type::ExtVector:
6711 if (idx_is_valid) {
6712 const clang::VectorType *array =
6713 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6714 if (array) {
6715 CompilerType element_type(getASTContext(), array->getElementType());
6716 if (element_type.GetCompleteType()) {
6717 char element_name[64];
6718 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6719 static_cast<uint64_t>(idx));
6720 child_name.assign(element_name);
6721 child_byte_size = element_type.GetByteSize(
6722 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6723 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6724 return element_type;
6725 }
6726 }
6727 }
6728 break;
6729
6730 case clang::Type::ConstantArray:
6731 case clang::Type::IncompleteArray:
6732 if (ignore_array_bounds || idx_is_valid) {
6733 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6734 if (array) {
6735 CompilerType element_type(getASTContext(), array->getElementType());
6736 if (element_type.GetCompleteType()) {
6737 char element_name[64];
6738 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6739 static_cast<uint64_t>(idx));
6740 child_name.assign(element_name);
6741 child_byte_size = element_type.GetByteSize(
6742 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6743 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6744 return element_type;
6745 }
6746 }
6747 }
6748 break;
6749
6750 case clang::Type::Pointer:
6751 if (idx_is_valid) {
6752 CompilerType pointee_clang_type(GetPointeeType(type));
6753
6754 // Don't dereference "void *" pointers
6755 if (pointee_clang_type.IsVoidType())
6756 return CompilerType();
6757
6758 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6759 child_is_deref_of_parent = false;
6760 bool tmp_child_is_deref_of_parent = false;
6761 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6762 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6763 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6764 child_bitfield_bit_size, child_bitfield_bit_offset,
6765 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6766 language_flags);
6767 } else {
6768 child_is_deref_of_parent = true;
6769
6770 const char *parent_name =
6771 valobj ? valobj->GetName().GetCString() : NULL;
6772 if (parent_name) {
6773 child_name.assign(1, '*');
6774 child_name += parent_name;
6775 }
6776
6777 // We have a pointer to an simple type
6778 if (idx == 0) {
6779 child_byte_size = pointee_clang_type.GetByteSize(
6780 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6781 child_byte_offset = 0;
6782 return pointee_clang_type;
6783 }
6784 }
6785 }
6786 break;
6787
6788 case clang::Type::LValueReference:
6789 case clang::Type::RValueReference:
6790 if (idx_is_valid) {
6791 const clang::ReferenceType *reference_type =
6792 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6793 CompilerType pointee_clang_type(getASTContext(),
6794 reference_type->getPointeeType());
6795 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6796 child_is_deref_of_parent = false;
6797 bool tmp_child_is_deref_of_parent = false;
6798 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6799 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6800 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6801 child_bitfield_bit_size, child_bitfield_bit_offset,
6802 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6803 language_flags);
6804 } else {
6805 const char *parent_name =
6806 valobj ? valobj->GetName().GetCString() : NULL;
6807 if (parent_name) {
6808 child_name.assign(1, '&');
6809 child_name += parent_name;
6810 }
6811
6812 // We have a pointer to an simple type
6813 if (idx == 0) {
6814 child_byte_size = pointee_clang_type.GetByteSize(
6815 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6816 child_byte_offset = 0;
6817 return pointee_clang_type;
6818 }
6819 }
6820 }
6821 break;
6822
6823 case clang::Type::Typedef: {
6824 CompilerType typedefed_clang_type(
6825 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
6826 ->getDecl()
6827 ->getUnderlyingType());
6828 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
6829 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6830 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6831 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6832 child_is_deref_of_parent, valobj, language_flags);
6833 } break;
6834
6835 case clang::Type::Auto: {
6836 CompilerType elaborated_clang_type(
6837 getASTContext(),
6838 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
6839 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6840 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6841 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6842 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6843 child_is_deref_of_parent, valobj, language_flags);
6844 }
6845
6846 case clang::Type::Elaborated: {
6847 CompilerType elaborated_clang_type(
6848 getASTContext(),
6849 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
6850 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6851 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6852 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6853 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6854 child_is_deref_of_parent, valobj, language_flags);
6855 }
6856
6857 case clang::Type::Paren: {
6858 CompilerType paren_clang_type(
6859 getASTContext(),
6860 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
6861 return paren_clang_type.GetChildCompilerTypeAtIndex(
6862 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6863 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6864 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6865 child_is_deref_of_parent, valobj, language_flags);
6866 }
6867
6868 default:
6869 break;
6870 }
6871 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006872}
6873
Kate Stoneb9c1b512016-09-06 20:57:50 +00006874static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
6875 const clang::CXXBaseSpecifier *base_spec,
6876 bool omit_empty_base_classes) {
6877 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006878
Kate Stoneb9c1b512016-09-06 20:57:50 +00006879 const clang::CXXRecordDecl *cxx_record_decl =
6880 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6881
6882 // const char *super_name = record_decl->getNameAsCString();
6883 // const char *base_name =
6884 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6885 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6886 //
6887 if (cxx_record_decl) {
6888 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6889 for (base_class = cxx_record_decl->bases_begin(),
6890 base_class_end = cxx_record_decl->bases_end();
6891 base_class != base_class_end; ++base_class) {
6892 if (omit_empty_base_classes) {
6893 if (BaseSpecifierIsEmpty(base_class))
6894 continue;
6895 }
6896
6897 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
6898 // super_name, base_name,
6899 // child_idx,
6900 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6901 //
6902 //
6903 if (base_class == base_spec)
6904 return child_idx;
6905 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006906 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006907 }
6908
6909 return UINT32_MAX;
6910}
6911
6912static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
6913 clang::NamedDecl *canonical_decl,
6914 bool omit_empty_base_classes) {
6915 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
6916 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6917 omit_empty_base_classes);
6918
6919 clang::RecordDecl::field_iterator field, field_end;
6920 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6921 field != field_end; ++field, ++child_idx) {
6922 if (field->getCanonicalDecl() == canonical_decl)
6923 return child_idx;
6924 }
6925
6926 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00006927}
6928
6929// Look for a child member (doesn't include base classes, but it does include
6930// their members) in the type hierarchy. Returns an index path into "clang_type"
6931// on how to reach the appropriate member.
6932//
6933// class A
6934// {
6935// public:
6936// int m_a;
6937// int m_b;
6938// };
6939//
6940// class B
6941// {
6942// };
6943//
6944// class C :
6945// public B,
6946// public A
6947// {
6948// };
6949//
6950// If we have a clang type that describes "class C", and we wanted to looked
6951// "m_b" in it:
6952//
Kate Stoneb9c1b512016-09-06 20:57:50 +00006953// With omit_empty_base_classes == false we would get an integer array back
6954// with:
Greg Claytond8d4a572015-08-11 21:38:15 +00006955// { 1, 1 }
6956// The first index 1 is the child index for "class A" within class C
6957// The second index 1 is the child index for "m_b" within class A
6958//
6959// With omit_empty_base_classes == true we would get an integer array back with:
6960// { 0, 1 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006961// The first index 0 is the child index for "class A" within class C (since
6962// class B doesn't have any members it doesn't count)
Greg Claytond8d4a572015-08-11 21:38:15 +00006963// The second index 1 is the child index for "m_b" within class A
6964
Kate Stoneb9c1b512016-09-06 20:57:50 +00006965size_t ClangASTContext::GetIndexOfChildMemberWithName(
6966 lldb::opaque_compiler_type_t type, const char *name,
6967 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
6968 if (type && name && name[0]) {
6969 clang::QualType qual_type(GetCanonicalQualType(type));
6970 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6971 switch (type_class) {
6972 case clang::Type::Record:
6973 if (GetCompleteType(type)) {
6974 const clang::RecordType *record_type =
6975 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6976 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00006977
Kate Stoneb9c1b512016-09-06 20:57:50 +00006978 assert(record_decl);
6979 uint32_t child_idx = 0;
6980
6981 const clang::CXXRecordDecl *cxx_record_decl =
6982 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6983
6984 // Try and find a field that matches NAME
6985 clang::RecordDecl::field_iterator field, field_end;
6986 llvm::StringRef name_sref(name);
6987 for (field = record_decl->field_begin(),
6988 field_end = record_decl->field_end();
6989 field != field_end; ++field, ++child_idx) {
6990 llvm::StringRef field_name = field->getName();
6991 if (field_name.empty()) {
6992 CompilerType field_type(getASTContext(), field->getType());
6993 child_indexes.push_back(child_idx);
6994 if (field_type.GetIndexOfChildMemberWithName(
6995 name, omit_empty_base_classes, child_indexes))
6996 return child_indexes.size();
6997 child_indexes.pop_back();
6998
6999 } else if (field_name.equals(name_sref)) {
7000 // We have to add on the number of base classes to this index!
7001 child_indexes.push_back(
7002 child_idx + ClangASTContext::GetNumBaseClasses(
7003 cxx_record_decl, omit_empty_base_classes));
7004 return child_indexes.size();
7005 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007006 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007007
Kate Stoneb9c1b512016-09-06 20:57:50 +00007008 if (cxx_record_decl) {
7009 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7010
7011 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7012
7013 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7014 // Didn't find things easily, lets let clang do its thang...
7015 clang::IdentifierInfo &ident_ref =
7016 getASTContext()->Idents.get(name_sref);
7017 clang::DeclarationName decl_name(&ident_ref);
7018
7019 clang::CXXBasePaths paths;
7020 if (cxx_record_decl->lookupInBases(
7021 [decl_name](const clang::CXXBaseSpecifier *specifier,
7022 clang::CXXBasePath &path) {
7023 return clang::CXXRecordDecl::FindOrdinaryMember(
7024 specifier, path, decl_name);
7025 },
7026 paths)) {
7027 clang::CXXBasePaths::const_paths_iterator path,
7028 path_end = paths.end();
7029 for (path = paths.begin(); path != path_end; ++path) {
7030 const size_t num_path_elements = path->size();
7031 for (size_t e = 0; e < num_path_elements; ++e) {
7032 clang::CXXBasePathElement elem = (*path)[e];
7033
7034 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7035 omit_empty_base_classes);
7036 if (child_idx == UINT32_MAX) {
7037 child_indexes.clear();
7038 return 0;
7039 } else {
7040 child_indexes.push_back(child_idx);
7041 parent_record_decl = llvm::cast<clang::RecordDecl>(
7042 elem.Base->getType()
7043 ->getAs<clang::RecordType>()
7044 ->getDecl());
7045 }
7046 }
7047 for (clang::NamedDecl *path_decl : path->Decls) {
7048 child_idx = GetIndexForRecordChild(
7049 parent_record_decl, path_decl, omit_empty_base_classes);
7050 if (child_idx == UINT32_MAX) {
7051 child_indexes.clear();
7052 return 0;
7053 } else {
7054 child_indexes.push_back(child_idx);
7055 }
7056 }
7057 }
7058 return child_indexes.size();
7059 }
7060 }
7061 }
7062 break;
7063
7064 case clang::Type::ObjCObject:
7065 case clang::Type::ObjCInterface:
7066 if (GetCompleteType(type)) {
7067 llvm::StringRef name_sref(name);
7068 const clang::ObjCObjectType *objc_class_type =
7069 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7070 assert(objc_class_type);
7071 if (objc_class_type) {
7072 uint32_t child_idx = 0;
7073 clang::ObjCInterfaceDecl *class_interface_decl =
7074 objc_class_type->getInterface();
7075
7076 if (class_interface_decl) {
7077 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7078 ivar_end = class_interface_decl->ivar_end();
7079 clang::ObjCInterfaceDecl *superclass_interface_decl =
7080 class_interface_decl->getSuperClass();
7081
7082 for (ivar_pos = class_interface_decl->ivar_begin();
7083 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7084 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7085
7086 if (ivar_decl->getName().equals(name_sref)) {
7087 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7088 (omit_empty_base_classes &&
7089 ObjCDeclHasIVars(superclass_interface_decl, true)))
7090 ++child_idx;
7091
7092 child_indexes.push_back(child_idx);
7093 return child_indexes.size();
7094 }
7095 }
7096
7097 if (superclass_interface_decl) {
7098 // The super class index is always zero for ObjC classes,
7099 // so we push it onto the child indexes in case we find
7100 // an ivar in our superclass...
7101 child_indexes.push_back(0);
7102
7103 CompilerType superclass_clang_type(
7104 getASTContext(), getASTContext()->getObjCInterfaceType(
7105 superclass_interface_decl));
7106 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7107 name, omit_empty_base_classes, child_indexes)) {
7108 // We did find an ivar in a superclass so just
7109 // return the results!
7110 return child_indexes.size();
7111 }
7112
7113 // We didn't find an ivar matching "name" in our
7114 // superclass, pop the superclass zero index that
7115 // we pushed on above.
7116 child_indexes.pop_back();
7117 }
7118 }
7119 }
7120 }
7121 break;
7122
7123 case clang::Type::ObjCObjectPointer: {
7124 CompilerType objc_object_clang_type(
7125 getASTContext(),
7126 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7127 ->getPointeeType());
7128 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7129 name, omit_empty_base_classes, child_indexes);
7130 } break;
7131
7132 case clang::Type::ConstantArray: {
7133 // const clang::ConstantArrayType *array =
7134 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7135 // const uint64_t element_count =
7136 // array->getSize().getLimitedValue();
7137 //
7138 // if (idx < element_count)
7139 // {
7140 // std::pair<uint64_t, unsigned> field_type_info =
7141 // ast->getTypeInfo(array->getElementType());
7142 //
7143 // char element_name[32];
7144 // ::snprintf (element_name, sizeof (element_name),
7145 // "%s[%u]", parent_name ? parent_name : "", idx);
7146 //
7147 // child_name.assign(element_name);
7148 // assert(field_type_info.first % 8 == 0);
7149 // child_byte_size = field_type_info.first / 8;
7150 // child_byte_offset = idx * child_byte_size;
7151 // return array->getElementType().getAsOpaquePtr();
7152 // }
7153 } break;
7154
7155 // case clang::Type::MemberPointerType:
7156 // {
7157 // MemberPointerType *mem_ptr_type =
7158 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7159 // clang::QualType pointee_type =
7160 // mem_ptr_type->getPointeeType();
7161 //
7162 // if (ClangASTContext::IsAggregateType
7163 // (pointee_type.getAsOpaquePtr()))
7164 // {
7165 // return GetIndexOfChildWithName (ast,
7166 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7167 // name);
7168 // }
7169 // }
7170 // break;
7171 //
7172 case clang::Type::LValueReference:
7173 case clang::Type::RValueReference: {
7174 const clang::ReferenceType *reference_type =
7175 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7176 clang::QualType pointee_type(reference_type->getPointeeType());
7177 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7178
7179 if (pointee_clang_type.IsAggregateType()) {
7180 return pointee_clang_type.GetIndexOfChildMemberWithName(
7181 name, omit_empty_base_classes, child_indexes);
7182 }
7183 } break;
7184
7185 case clang::Type::Pointer: {
7186 CompilerType pointee_clang_type(GetPointeeType(type));
7187
7188 if (pointee_clang_type.IsAggregateType()) {
7189 return pointee_clang_type.GetIndexOfChildMemberWithName(
7190 name, omit_empty_base_classes, child_indexes);
7191 }
7192 } break;
7193
7194 case clang::Type::Typedef:
7195 return CompilerType(getASTContext(),
7196 llvm::cast<clang::TypedefType>(qual_type)
7197 ->getDecl()
7198 ->getUnderlyingType())
7199 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7200 child_indexes);
7201
7202 case clang::Type::Auto:
7203 return CompilerType(
7204 getASTContext(),
7205 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7206 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7207 child_indexes);
7208
7209 case clang::Type::Elaborated:
7210 return CompilerType(
7211 getASTContext(),
7212 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7213 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7214 child_indexes);
7215
7216 case clang::Type::Paren:
7217 return CompilerType(getASTContext(),
7218 llvm::cast<clang::ParenType>(qual_type)->desugar())
7219 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7220 child_indexes);
7221
7222 default:
7223 break;
7224 }
7225 }
7226 return 0;
7227}
Greg Claytond8d4a572015-08-11 21:38:15 +00007228
7229// Get the index of the child of "clang_type" whose name matches. This function
7230// doesn't descend into the children, but only looks one level deep and name
7231// matches can include base class names.
7232
7233uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007234ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7235 const char *name,
7236 bool omit_empty_base_classes) {
7237 if (type && name && name[0]) {
7238 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007239
Kate Stoneb9c1b512016-09-06 20:57:50 +00007240 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7241
7242 switch (type_class) {
7243 case clang::Type::Record:
7244 if (GetCompleteType(type)) {
7245 const clang::RecordType *record_type =
7246 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7247 const clang::RecordDecl *record_decl = record_type->getDecl();
7248
7249 assert(record_decl);
7250 uint32_t child_idx = 0;
7251
7252 const clang::CXXRecordDecl *cxx_record_decl =
7253 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7254
7255 if (cxx_record_decl) {
7256 clang::CXXRecordDecl::base_class_const_iterator base_class,
7257 base_class_end;
7258 for (base_class = cxx_record_decl->bases_begin(),
7259 base_class_end = cxx_record_decl->bases_end();
7260 base_class != base_class_end; ++base_class) {
7261 // Skip empty base classes
7262 clang::CXXRecordDecl *base_class_decl =
7263 llvm::cast<clang::CXXRecordDecl>(
7264 base_class->getType()
7265 ->getAs<clang::RecordType>()
7266 ->getDecl());
7267 if (omit_empty_base_classes &&
7268 ClangASTContext::RecordHasFields(base_class_decl) == false)
7269 continue;
7270
7271 CompilerType base_class_clang_type(getASTContext(),
7272 base_class->getType());
7273 std::string base_class_type_name(
7274 base_class_clang_type.GetTypeName().AsCString(""));
7275 if (base_class_type_name.compare(name) == 0)
7276 return child_idx;
7277 ++child_idx;
7278 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007279 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007280
Kate Stoneb9c1b512016-09-06 20:57:50 +00007281 // Try and find a field that matches NAME
7282 clang::RecordDecl::field_iterator field, field_end;
7283 llvm::StringRef name_sref(name);
7284 for (field = record_decl->field_begin(),
7285 field_end = record_decl->field_end();
7286 field != field_end; ++field, ++child_idx) {
7287 if (field->getName().equals(name_sref))
7288 return child_idx;
7289 }
7290 }
7291 break;
7292
7293 case clang::Type::ObjCObject:
7294 case clang::Type::ObjCInterface:
7295 if (GetCompleteType(type)) {
7296 llvm::StringRef name_sref(name);
7297 const clang::ObjCObjectType *objc_class_type =
7298 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7299 assert(objc_class_type);
7300 if (objc_class_type) {
7301 uint32_t child_idx = 0;
7302 clang::ObjCInterfaceDecl *class_interface_decl =
7303 objc_class_type->getInterface();
7304
7305 if (class_interface_decl) {
7306 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7307 ivar_end = class_interface_decl->ivar_end();
7308 clang::ObjCInterfaceDecl *superclass_interface_decl =
7309 class_interface_decl->getSuperClass();
7310
7311 for (ivar_pos = class_interface_decl->ivar_begin();
7312 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7313 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7314
7315 if (ivar_decl->getName().equals(name_sref)) {
7316 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7317 (omit_empty_base_classes &&
7318 ObjCDeclHasIVars(superclass_interface_decl, true)))
7319 ++child_idx;
7320
7321 return child_idx;
7322 }
7323 }
7324
7325 if (superclass_interface_decl) {
7326 if (superclass_interface_decl->getName().equals(name_sref))
7327 return 0;
7328 }
7329 }
7330 }
7331 }
7332 break;
7333
7334 case clang::Type::ObjCObjectPointer: {
7335 CompilerType pointee_clang_type(
7336 getASTContext(),
7337 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7338 ->getPointeeType());
7339 return pointee_clang_type.GetIndexOfChildWithName(
7340 name, omit_empty_base_classes);
7341 } break;
7342
7343 case clang::Type::ConstantArray: {
7344 // const clang::ConstantArrayType *array =
7345 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7346 // const uint64_t element_count =
7347 // array->getSize().getLimitedValue();
7348 //
7349 // if (idx < element_count)
7350 // {
7351 // std::pair<uint64_t, unsigned> field_type_info =
7352 // ast->getTypeInfo(array->getElementType());
7353 //
7354 // char element_name[32];
7355 // ::snprintf (element_name, sizeof (element_name),
7356 // "%s[%u]", parent_name ? parent_name : "", idx);
7357 //
7358 // child_name.assign(element_name);
7359 // assert(field_type_info.first % 8 == 0);
7360 // child_byte_size = field_type_info.first / 8;
7361 // child_byte_offset = idx * child_byte_size;
7362 // return array->getElementType().getAsOpaquePtr();
7363 // }
7364 } break;
7365
7366 // case clang::Type::MemberPointerType:
7367 // {
7368 // MemberPointerType *mem_ptr_type =
7369 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7370 // clang::QualType pointee_type =
7371 // mem_ptr_type->getPointeeType();
7372 //
7373 // if (ClangASTContext::IsAggregateType
7374 // (pointee_type.getAsOpaquePtr()))
7375 // {
7376 // return GetIndexOfChildWithName (ast,
7377 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7378 // name);
7379 // }
7380 // }
7381 // break;
7382 //
7383 case clang::Type::LValueReference:
7384 case clang::Type::RValueReference: {
7385 const clang::ReferenceType *reference_type =
7386 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7387 CompilerType pointee_type(getASTContext(),
7388 reference_type->getPointeeType());
7389
7390 if (pointee_type.IsAggregateType()) {
7391 return pointee_type.GetIndexOfChildWithName(name,
7392 omit_empty_base_classes);
7393 }
7394 } break;
7395
7396 case clang::Type::Pointer: {
7397 const clang::PointerType *pointer_type =
7398 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7399 CompilerType pointee_type(getASTContext(),
7400 pointer_type->getPointeeType());
7401
7402 if (pointee_type.IsAggregateType()) {
7403 return pointee_type.GetIndexOfChildWithName(name,
7404 omit_empty_base_classes);
7405 } else {
7406 // if (parent_name)
7407 // {
7408 // child_name.assign(1, '*');
7409 // child_name += parent_name;
7410 // }
7411 //
7412 // // We have a pointer to an simple type
7413 // if (idx == 0)
7414 // {
7415 // std::pair<uint64_t, unsigned> clang_type_info
7416 // = ast->getTypeInfo(pointee_type);
7417 // assert(clang_type_info.first % 8 == 0);
7418 // child_byte_size = clang_type_info.first / 8;
7419 // child_byte_offset = 0;
7420 // return pointee_type.getAsOpaquePtr();
7421 // }
7422 }
7423 } break;
7424
7425 case clang::Type::Auto:
7426 return CompilerType(
7427 getASTContext(),
7428 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7429 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7430
7431 case clang::Type::Elaborated:
7432 return CompilerType(
7433 getASTContext(),
7434 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7435 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7436
7437 case clang::Type::Paren:
7438 return CompilerType(getASTContext(),
7439 llvm::cast<clang::ParenType>(qual_type)->desugar())
7440 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7441
7442 case clang::Type::Typedef:
7443 return CompilerType(getASTContext(),
7444 llvm::cast<clang::TypedefType>(qual_type)
7445 ->getDecl()
7446 ->getUnderlyingType())
7447 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7448
7449 default:
7450 break;
7451 }
7452 }
7453 return UINT32_MAX;
7454}
Greg Claytond8d4a572015-08-11 21:38:15 +00007455
7456size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007457ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7458 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007459 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007460
Kate Stoneb9c1b512016-09-06 20:57:50 +00007461 clang::QualType qual_type(GetCanonicalQualType(type));
7462 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7463 switch (type_class) {
7464 case clang::Type::Record:
7465 if (GetCompleteType(type)) {
7466 const clang::CXXRecordDecl *cxx_record_decl =
7467 qual_type->getAsCXXRecordDecl();
7468 if (cxx_record_decl) {
7469 const clang::ClassTemplateSpecializationDecl *template_decl =
7470 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7471 cxx_record_decl);
7472 if (template_decl)
7473 return template_decl->getTemplateArgs().size();
7474 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007475 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007476 break;
7477
7478 case clang::Type::Typedef:
7479 return (CompilerType(getASTContext(),
7480 llvm::cast<clang::TypedefType>(qual_type)
7481 ->getDecl()
7482 ->getUnderlyingType()))
7483 .GetNumTemplateArguments();
7484
7485 case clang::Type::Auto:
7486 return (CompilerType(
7487 getASTContext(),
7488 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7489 .GetNumTemplateArguments();
7490
7491 case clang::Type::Elaborated:
7492 return (CompilerType(
7493 getASTContext(),
7494 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7495 .GetNumTemplateArguments();
7496
7497 case clang::Type::Paren:
7498 return (CompilerType(getASTContext(),
7499 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7500 .GetNumTemplateArguments();
7501
7502 default:
7503 break;
7504 }
7505
7506 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007507}
7508
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007509CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00007510ClangASTContext::GetTemplateArgument(lldb::opaque_compiler_type_t type,
7511 size_t arg_idx,
7512 lldb::TemplateArgumentKind &kind) {
7513 if (!type)
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007514 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00007515
7516 clang::QualType qual_type(GetCanonicalQualType(type));
7517 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7518 switch (type_class) {
7519 case clang::Type::Record:
7520 if (GetCompleteType(type)) {
7521 const clang::CXXRecordDecl *cxx_record_decl =
7522 qual_type->getAsCXXRecordDecl();
7523 if (cxx_record_decl) {
7524 const clang::ClassTemplateSpecializationDecl *template_decl =
7525 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7526 cxx_record_decl);
7527 if (template_decl &&
7528 arg_idx < template_decl->getTemplateArgs().size()) {
7529 const clang::TemplateArgument &template_arg =
7530 template_decl->getTemplateArgs()[arg_idx];
7531 switch (template_arg.getKind()) {
7532 case clang::TemplateArgument::Null:
7533 kind = eTemplateArgumentKindNull;
7534 return CompilerType();
7535
7536 case clang::TemplateArgument::Type:
7537 kind = eTemplateArgumentKindType;
7538 return CompilerType(getASTContext(), template_arg.getAsType());
7539
7540 case clang::TemplateArgument::Declaration:
7541 kind = eTemplateArgumentKindDeclaration;
7542 return CompilerType();
7543
7544 case clang::TemplateArgument::Integral:
7545 kind = eTemplateArgumentKindIntegral;
7546 return CompilerType(getASTContext(),
7547 template_arg.getIntegralType());
7548
7549 case clang::TemplateArgument::Template:
7550 kind = eTemplateArgumentKindTemplate;
7551 return CompilerType();
7552
7553 case clang::TemplateArgument::TemplateExpansion:
7554 kind = eTemplateArgumentKindTemplateExpansion;
7555 return CompilerType();
7556
7557 case clang::TemplateArgument::Expression:
7558 kind = eTemplateArgumentKindExpression;
7559 return CompilerType();
7560
7561 case clang::TemplateArgument::Pack:
7562 kind = eTemplateArgumentKindPack;
7563 return CompilerType();
7564
7565 default:
7566 assert(!"Unhandled clang::TemplateArgument::ArgKind");
7567 break;
7568 }
7569 }
7570 }
7571 }
7572 break;
7573
7574 case clang::Type::Typedef:
7575 return (CompilerType(getASTContext(),
7576 llvm::cast<clang::TypedefType>(qual_type)
7577 ->getDecl()
7578 ->getUnderlyingType()))
7579 .GetTemplateArgument(arg_idx, kind);
7580
7581 case clang::Type::Auto:
7582 return (CompilerType(
7583 getASTContext(),
7584 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7585 .GetTemplateArgument(arg_idx, kind);
7586
7587 case clang::Type::Elaborated:
7588 return (CompilerType(
7589 getASTContext(),
7590 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7591 .GetTemplateArgument(arg_idx, kind);
7592
7593 case clang::Type::Paren:
7594 return (CompilerType(getASTContext(),
7595 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7596 .GetTemplateArgument(arg_idx, kind);
7597
7598 default:
7599 break;
7600 }
7601 kind = eTemplateArgumentKindNull;
7602 return CompilerType();
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007603}
7604
Kate Stoneb9c1b512016-09-06 20:57:50 +00007605CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7606 if (type)
7607 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7608 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007609}
7610
Kate Stoneb9c1b512016-09-06 20:57:50 +00007611clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7612 const clang::EnumType *enutype =
7613 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7614 if (enutype)
7615 return enutype->getDecl();
7616 return NULL;
7617}
7618
7619clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7620 const clang::RecordType *record_type =
7621 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7622 if (record_type)
7623 return record_type->getDecl();
7624 return nullptr;
7625}
7626
7627clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
7628 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
7629 if (qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00007630 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007631 else
7632 return qual_type->getAsTagDecl();
Greg Claytone6b36cd2015-12-08 01:02:08 +00007633}
7634
Greg Claytond8d4a572015-08-11 21:38:15 +00007635clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007636ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7637 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007638}
7639
7640clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007641ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7642 const clang::ObjCObjectType *objc_class_type =
7643 llvm::dyn_cast<clang::ObjCObjectType>(
7644 ClangUtil::GetCanonicalQualType(type));
7645 if (objc_class_type)
7646 return objc_class_type->getInterface();
7647 return nullptr;
7648}
7649
7650clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
7651 const CompilerType &type, const char *name,
7652 const CompilerType &field_clang_type, AccessType access,
7653 uint32_t bitfield_bit_size) {
7654 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007655 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007656 ClangASTContext *ast =
7657 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7658 if (!ast)
7659 return nullptr;
7660 clang::ASTContext *clang_ast = ast->getASTContext();
7661
7662 clang::FieldDecl *field = nullptr;
7663
7664 clang::Expr *bit_width = nullptr;
7665 if (bitfield_bit_size != 0) {
7666 llvm::APInt bitfield_bit_size_apint(
7667 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7668 bit_width = new (*clang_ast)
7669 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7670 clang_ast->IntTy, clang::SourceLocation());
7671 }
7672
7673 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7674 if (record_decl) {
7675 field = clang::FieldDecl::Create(
7676 *clang_ast, record_decl, clang::SourceLocation(),
7677 clang::SourceLocation(),
7678 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7679 ClangUtil::GetQualType(field_clang_type), // Field type
7680 nullptr, // TInfo *
7681 bit_width, // BitWidth
7682 false, // Mutable
7683 clang::ICIS_NoInit); // HasInit
7684
7685 if (!name) {
7686 // Determine whether this field corresponds to an anonymous
7687 // struct or union.
7688 if (const clang::TagType *TagT =
7689 field->getType()->getAs<clang::TagType>()) {
7690 if (clang::RecordDecl *Rec =
7691 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7692 if (!Rec->getDeclName()) {
7693 Rec->setAnonymousStructOrUnion(true);
7694 field->setImplicit();
7695 }
7696 }
7697 }
7698
7699 if (field) {
7700 field->setAccess(
7701 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7702
7703 record_decl->addDecl(field);
7704
7705#ifdef LLDB_CONFIGURATION_DEBUG
7706 VerifyDecl(field);
7707#endif
7708 }
7709 } else {
7710 clang::ObjCInterfaceDecl *class_interface_decl =
7711 ast->GetAsObjCInterfaceDecl(type);
7712
7713 if (class_interface_decl) {
7714 const bool is_synthesized = false;
7715
7716 field_clang_type.GetCompleteType();
7717
7718 field = clang::ObjCIvarDecl::Create(
7719 *clang_ast, class_interface_decl, clang::SourceLocation(),
7720 clang::SourceLocation(),
7721 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7722 ClangUtil::GetQualType(field_clang_type), // Field type
7723 nullptr, // TypeSourceInfo *
7724 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7725 is_synthesized);
7726
7727 if (field) {
7728 class_interface_decl->addDecl(field);
7729
7730#ifdef LLDB_CONFIGURATION_DEBUG
7731 VerifyDecl(field);
7732#endif
7733 }
7734 }
7735 }
7736 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007737}
7738
Kate Stoneb9c1b512016-09-06 20:57:50 +00007739void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7740 if (!type)
7741 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007742
Kate Stoneb9c1b512016-09-06 20:57:50 +00007743 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7744 if (!ast)
7745 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007746
Kate Stoneb9c1b512016-09-06 20:57:50 +00007747 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007748
Kate Stoneb9c1b512016-09-06 20:57:50 +00007749 if (!record_decl)
7750 return;
7751
7752 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7753
7754 IndirectFieldVector indirect_fields;
7755 clang::RecordDecl::field_iterator field_pos;
7756 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7757 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7758 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7759 last_field_pos = field_pos++) {
7760 if (field_pos->isAnonymousStructOrUnion()) {
7761 clang::QualType field_qual_type = field_pos->getType();
7762
7763 const clang::RecordType *field_record_type =
7764 field_qual_type->getAs<clang::RecordType>();
7765
7766 if (!field_record_type)
7767 continue;
7768
7769 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7770
7771 if (!field_record_decl)
7772 continue;
7773
7774 for (clang::RecordDecl::decl_iterator
7775 di = field_record_decl->decls_begin(),
7776 de = field_record_decl->decls_end();
7777 di != de; ++di) {
7778 if (clang::FieldDecl *nested_field_decl =
7779 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7780 clang::NamedDecl **chain =
7781 new (*ast->getASTContext()) clang::NamedDecl *[2];
7782 chain[0] = *field_pos;
7783 chain[1] = nested_field_decl;
7784 clang::IndirectFieldDecl *indirect_field =
7785 clang::IndirectFieldDecl::Create(
7786 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7787 nested_field_decl->getIdentifier(),
7788 nested_field_decl->getType(), {chain, 2});
7789
7790 indirect_field->setImplicit();
7791
7792 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7793 field_pos->getAccess(), nested_field_decl->getAccess()));
7794
7795 indirect_fields.push_back(indirect_field);
7796 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7797 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7798 size_t nested_chain_size =
7799 nested_indirect_field_decl->getChainingSize();
7800 clang::NamedDecl **chain = new (*ast->getASTContext())
7801 clang::NamedDecl *[nested_chain_size + 1];
7802 chain[0] = *field_pos;
7803
7804 int chain_index = 1;
7805 for (clang::IndirectFieldDecl::chain_iterator
7806 nci = nested_indirect_field_decl->chain_begin(),
7807 nce = nested_indirect_field_decl->chain_end();
7808 nci < nce; ++nci) {
7809 chain[chain_index] = *nci;
7810 chain_index++;
7811 }
7812
7813 clang::IndirectFieldDecl *indirect_field =
7814 clang::IndirectFieldDecl::Create(
7815 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7816 nested_indirect_field_decl->getIdentifier(),
7817 nested_indirect_field_decl->getType(),
7818 {chain, nested_chain_size + 1});
7819
7820 indirect_field->setImplicit();
7821
7822 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7823 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
7824
7825 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00007826 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007827 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007828 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007829 }
7830
7831 // Check the last field to see if it has an incomplete array type as its
7832 // last member and if it does, the tell the record decl about it
7833 if (last_field_pos != field_end_pos) {
7834 if (last_field_pos->getType()->isIncompleteArrayType())
7835 record_decl->hasFlexibleArrayMember();
7836 }
7837
7838 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
7839 ife = indirect_fields.end();
7840 ifi < ife; ++ifi) {
7841 record_decl->addDecl(*ifi);
7842 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007843}
7844
Kate Stoneb9c1b512016-09-06 20:57:50 +00007845void ClangASTContext::SetIsPacked(const CompilerType &type) {
7846 if (type) {
7847 ClangASTContext *ast =
7848 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7849 if (ast) {
7850 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
7851
7852 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00007853 return;
7854
Kate Stoneb9c1b512016-09-06 20:57:50 +00007855 record_decl->addAttr(
7856 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00007857 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007858 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007859}
7860
Kate Stoneb9c1b512016-09-06 20:57:50 +00007861clang::VarDecl *ClangASTContext::AddVariableToRecordType(
7862 const CompilerType &type, const char *name, const CompilerType &var_type,
7863 AccessType access) {
7864 clang::VarDecl *var_decl = nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007865
Kate Stoneb9c1b512016-09-06 20:57:50 +00007866 if (!type.IsValid() || !var_type.IsValid())
7867 return nullptr;
7868 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7869 if (!ast)
7870 return nullptr;
7871
7872 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7873 if (record_decl) {
7874 var_decl = clang::VarDecl::Create(
7875 *ast->getASTContext(), // ASTContext &
7876 record_decl, // DeclContext *
7877 clang::SourceLocation(), // clang::SourceLocation StartLoc
7878 clang::SourceLocation(), // clang::SourceLocation IdLoc
7879 name ? &ast->getASTContext()->Idents.get(name)
7880 : nullptr, // clang::IdentifierInfo *
7881 ClangUtil::GetQualType(var_type), // Variable clang::QualType
7882 nullptr, // TypeSourceInfo *
7883 clang::SC_Static); // StorageClass
7884 if (var_decl) {
7885 var_decl->setAccess(
7886 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7887 record_decl->addDecl(var_decl);
7888
Greg Claytond8d4a572015-08-11 21:38:15 +00007889#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00007890 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00007891#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00007892 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007893 }
7894 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00007895}
7896
Kate Stoneb9c1b512016-09-06 20:57:50 +00007897clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
7898 lldb::opaque_compiler_type_t type, const char *name,
7899 const CompilerType &method_clang_type, lldb::AccessType access,
7900 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
7901 bool is_attr_used, bool is_artificial) {
7902 if (!type || !method_clang_type.IsValid() || name == nullptr ||
7903 name[0] == '\0')
7904 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007905
Kate Stoneb9c1b512016-09-06 20:57:50 +00007906 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007907
Kate Stoneb9c1b512016-09-06 20:57:50 +00007908 clang::CXXRecordDecl *cxx_record_decl =
7909 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007910
Kate Stoneb9c1b512016-09-06 20:57:50 +00007911 if (cxx_record_decl == nullptr)
7912 return nullptr;
7913
7914 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
7915
7916 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7917
7918 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
7919
7920 const clang::FunctionType *function_type =
7921 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7922
7923 if (function_type == nullptr)
7924 return nullptr;
7925
7926 const clang::FunctionProtoType *method_function_prototype(
7927 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7928
7929 if (!method_function_prototype)
7930 return nullptr;
7931
7932 unsigned int num_params = method_function_prototype->getNumParams();
7933
7934 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7935 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7936
7937 if (is_artificial)
7938 return nullptr; // skip everything artificial
7939
7940 if (name[0] == '~') {
7941 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
7942 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7943 clang::DeclarationNameInfo(
7944 getASTContext()->DeclarationNames.getCXXDestructorName(
7945 getASTContext()->getCanonicalType(record_qual_type)),
7946 clang::SourceLocation()),
7947 method_qual_type, nullptr, is_inline, is_artificial);
7948 cxx_method_decl = cxx_dtor_decl;
7949 } else if (decl_name == cxx_record_decl->getDeclName()) {
7950 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
7951 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7952 clang::DeclarationNameInfo(
7953 getASTContext()->DeclarationNames.getCXXConstructorName(
7954 getASTContext()->getCanonicalType(record_qual_type)),
7955 clang::SourceLocation()),
7956 method_qual_type,
7957 nullptr, // TypeSourceInfo *
7958 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
7959 cxx_method_decl = cxx_ctor_decl;
7960 } else {
7961 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7962 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7963
7964 if (IsOperator(name, op_kind)) {
7965 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
7966 // Check the number of operator parameters. Sometimes we have
7967 // seen bad DWARF that doesn't correctly describe operators and
7968 // if we try to create a method and add it to the class, clang
7969 // will assert and crash, so we need to make sure things are
7970 // acceptable.
7971 const bool is_method = true;
7972 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
7973 is_method, op_kind, num_params))
7974 return nullptr;
7975 cxx_method_decl = clang::CXXMethodDecl::Create(
7976 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7977 clang::DeclarationNameInfo(
7978 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
7979 clang::SourceLocation()),
7980 method_qual_type,
7981 nullptr, // TypeSourceInfo *
7982 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
7983 } else if (num_params == 0) {
7984 // Conversion operators don't take params...
7985 cxx_method_decl = clang::CXXConversionDecl::Create(
7986 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7987 clang::DeclarationNameInfo(
7988 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
7989 getASTContext()->getCanonicalType(
7990 function_type->getReturnType())),
7991 clang::SourceLocation()),
7992 method_qual_type,
7993 nullptr, // TypeSourceInfo *
7994 is_inline, is_explicit, false /*is_constexpr*/,
7995 clang::SourceLocation());
7996 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007997 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007998
7999 if (cxx_method_decl == nullptr) {
8000 cxx_method_decl = clang::CXXMethodDecl::Create(
8001 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8002 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8003 method_qual_type,
8004 nullptr, // TypeSourceInfo *
8005 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00008006 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008007 }
8008
8009 clang::AccessSpecifier access_specifier =
8010 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8011
8012 cxx_method_decl->setAccess(access_specifier);
8013 cxx_method_decl->setVirtualAsWritten(is_virtual);
8014
8015 if (is_attr_used)
8016 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8017
8018 // Populate the method decl with parameter decls
8019
8020 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8021
8022 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8023 params.push_back(clang::ParmVarDecl::Create(
8024 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8025 clang::SourceLocation(),
8026 nullptr, // anonymous
8027 method_function_prototype->getParamType(param_index), nullptr,
8028 clang::SC_None, nullptr));
8029 }
8030
8031 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8032
8033 cxx_record_decl->addDecl(cxx_method_decl);
8034
8035 // Sometimes the debug info will mention a constructor (default/copy/move),
8036 // destructor, or assignment operator (copy/move) but there won't be any
8037 // version of this in the code. So we check if the function was artificially
8038 // generated and if it is trivial and this lets the compiler/backend know
8039 // that it can inline the IR for these when it needs to and we can avoid a
8040 // "missing function" error when running expressions.
8041
8042 if (is_artificial) {
8043 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8044 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8045 (cxx_ctor_decl->isCopyConstructor() &&
8046 cxx_record_decl->hasTrivialCopyConstructor()) ||
8047 (cxx_ctor_decl->isMoveConstructor() &&
8048 cxx_record_decl->hasTrivialMoveConstructor()))) {
8049 cxx_ctor_decl->setDefaulted();
8050 cxx_ctor_decl->setTrivial(true);
8051 } else if (cxx_dtor_decl) {
8052 if (cxx_record_decl->hasTrivialDestructor()) {
8053 cxx_dtor_decl->setDefaulted();
8054 cxx_dtor_decl->setTrivial(true);
8055 }
8056 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8057 cxx_record_decl->hasTrivialCopyAssignment()) ||
8058 (cxx_method_decl->isMoveAssignmentOperator() &&
8059 cxx_record_decl->hasTrivialMoveAssignment())) {
8060 cxx_method_decl->setDefaulted();
8061 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008062 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008063 }
8064
Greg Claytond8d4a572015-08-11 21:38:15 +00008065#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008066 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008067#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008068
Kate Stoneb9c1b512016-09-06 20:57:50 +00008069 // printf ("decl->isPolymorphic() = %i\n",
8070 // cxx_record_decl->isPolymorphic());
8071 // printf ("decl->isAggregate() = %i\n",
8072 // cxx_record_decl->isAggregate());
8073 // printf ("decl->isPOD() = %i\n",
8074 // cxx_record_decl->isPOD());
8075 // printf ("decl->isEmpty() = %i\n",
8076 // cxx_record_decl->isEmpty());
8077 // printf ("decl->isAbstract() = %i\n",
8078 // cxx_record_decl->isAbstract());
8079 // printf ("decl->hasTrivialConstructor() = %i\n",
8080 // cxx_record_decl->hasTrivialConstructor());
8081 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8082 // cxx_record_decl->hasTrivialCopyConstructor());
8083 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8084 // cxx_record_decl->hasTrivialCopyAssignment());
8085 // printf ("decl->hasTrivialDestructor() = %i\n",
8086 // cxx_record_decl->hasTrivialDestructor());
8087 return cxx_method_decl;
8088}
Greg Claytond8d4a572015-08-11 21:38:15 +00008089
8090#pragma mark C++ Base Classes
8091
8092clang::CXXBaseSpecifier *
Kate Stoneb9c1b512016-09-06 20:57:50 +00008093ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8094 AccessType access, bool is_virtual,
8095 bool base_of_class) {
8096 if (type)
8097 return new clang::CXXBaseSpecifier(
8098 clang::SourceRange(), is_virtual, base_of_class,
8099 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8100 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8101 clang::SourceLocation());
8102 return nullptr;
8103}
8104
8105void ClangASTContext::DeleteBaseClassSpecifiers(
8106 clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes) {
8107 for (unsigned i = 0; i < num_base_classes; ++i) {
8108 delete base_classes[i];
8109 base_classes[i] = nullptr;
8110 }
8111}
8112
8113bool ClangASTContext::SetBaseClassesForClassType(
8114 lldb::opaque_compiler_type_t type,
8115 clang::CXXBaseSpecifier const *const *base_classes,
8116 unsigned num_base_classes) {
8117 if (type) {
8118 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8119 if (cxx_record_decl) {
8120 cxx_record_decl->setBases(base_classes, num_base_classes);
8121 return true;
8122 }
8123 }
8124 return false;
8125}
8126
8127bool ClangASTContext::SetObjCSuperClass(
8128 const CompilerType &type, const CompilerType &superclass_clang_type) {
8129 ClangASTContext *ast =
8130 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8131 if (!ast)
8132 return false;
8133 clang::ASTContext *clang_ast = ast->getASTContext();
8134
8135 if (type && superclass_clang_type.IsValid() &&
8136 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8137 clang::ObjCInterfaceDecl *class_interface_decl =
8138 GetAsObjCInterfaceDecl(type);
8139 clang::ObjCInterfaceDecl *super_interface_decl =
8140 GetAsObjCInterfaceDecl(superclass_clang_type);
8141 if (class_interface_decl && super_interface_decl) {
8142 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8143 clang_ast->getObjCInterfaceType(super_interface_decl)));
8144 return true;
8145 }
8146 }
8147 return false;
8148}
8149
8150bool ClangASTContext::AddObjCClassProperty(
8151 const CompilerType &type, const char *property_name,
8152 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8153 const char *property_setter_name, const char *property_getter_name,
8154 uint32_t property_attributes, ClangASTMetadata *metadata) {
8155 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8156 property_name[0] == '\0')
8157 return false;
8158 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8159 if (!ast)
8160 return false;
8161 clang::ASTContext *clang_ast = ast->getASTContext();
8162
8163 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8164
8165 if (class_interface_decl) {
8166 CompilerType property_clang_type_to_access;
8167
8168 if (property_clang_type.IsValid())
8169 property_clang_type_to_access = property_clang_type;
8170 else if (ivar_decl)
8171 property_clang_type_to_access =
8172 CompilerType(clang_ast, ivar_decl->getType());
8173
8174 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8175 clang::TypeSourceInfo *prop_type_source;
8176 if (ivar_decl)
8177 prop_type_source =
8178 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8179 else
8180 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8181 ClangUtil::GetQualType(property_clang_type));
8182
8183 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8184 *clang_ast, class_interface_decl,
8185 clang::SourceLocation(), // Source Location
8186 &clang_ast->Idents.get(property_name),
8187 clang::SourceLocation(), // Source Location for AT
8188 clang::SourceLocation(), // Source location for (
8189 ivar_decl ? ivar_decl->getType()
8190 : ClangUtil::GetQualType(property_clang_type),
8191 prop_type_source);
8192
8193 if (property_decl) {
8194 if (metadata)
8195 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8196
8197 class_interface_decl->addDecl(property_decl);
8198
8199 clang::Selector setter_sel, getter_sel;
8200
8201 if (property_setter_name != nullptr) {
8202 std::string property_setter_no_colon(
8203 property_setter_name, strlen(property_setter_name) - 1);
8204 clang::IdentifierInfo *setter_ident =
8205 &clang_ast->Idents.get(property_setter_no_colon.c_str());
8206 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8207 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8208 std::string setter_sel_string("set");
8209 setter_sel_string.push_back(::toupper(property_name[0]));
8210 setter_sel_string.append(&property_name[1]);
8211 clang::IdentifierInfo *setter_ident =
8212 &clang_ast->Idents.get(setter_sel_string.c_str());
8213 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8214 }
8215 property_decl->setSetterName(setter_sel);
8216 property_decl->setPropertyAttributes(
8217 clang::ObjCPropertyDecl::OBJC_PR_setter);
8218
8219 if (property_getter_name != nullptr) {
8220 clang::IdentifierInfo *getter_ident =
8221 &clang_ast->Idents.get(property_getter_name);
8222 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8223 } else {
8224 clang::IdentifierInfo *getter_ident =
8225 &clang_ast->Idents.get(property_name);
8226 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8227 }
8228 property_decl->setGetterName(getter_sel);
8229 property_decl->setPropertyAttributes(
8230 clang::ObjCPropertyDecl::OBJC_PR_getter);
8231
8232 if (ivar_decl)
8233 property_decl->setPropertyIvarDecl(ivar_decl);
8234
8235 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8236 property_decl->setPropertyAttributes(
8237 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8238 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8239 property_decl->setPropertyAttributes(
8240 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8241 if (property_attributes & DW_APPLE_PROPERTY_assign)
8242 property_decl->setPropertyAttributes(
8243 clang::ObjCPropertyDecl::OBJC_PR_assign);
8244 if (property_attributes & DW_APPLE_PROPERTY_retain)
8245 property_decl->setPropertyAttributes(
8246 clang::ObjCPropertyDecl::OBJC_PR_retain);
8247 if (property_attributes & DW_APPLE_PROPERTY_copy)
8248 property_decl->setPropertyAttributes(
8249 clang::ObjCPropertyDecl::OBJC_PR_copy);
8250 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8251 property_decl->setPropertyAttributes(
8252 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8253 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8254 property_decl->setPropertyAttributes(
8255 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8256 if (property_attributes &
8257 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8258 property_decl->setPropertyAttributes(
8259 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8260 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8261 property_decl->setPropertyAttributes(
8262 clang::ObjCPropertyDecl::OBJC_PR_class);
8263
8264 const bool isInstance =
8265 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8266
8267 if (!getter_sel.isNull() &&
8268 !(isInstance
8269 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8270 : class_interface_decl->lookupClassMethod(getter_sel))) {
8271 const bool isVariadic = false;
8272 const bool isSynthesized = false;
8273 const bool isImplicitlyDeclared = true;
8274 const bool isDefined = false;
8275 const clang::ObjCMethodDecl::ImplementationControl impControl =
8276 clang::ObjCMethodDecl::None;
8277 const bool HasRelatedResultType = false;
8278
8279 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8280 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8281 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8282 nullptr, class_interface_decl, isInstance, isVariadic,
8283 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8284 HasRelatedResultType);
8285
8286 if (getter && metadata)
8287 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8288
8289 if (getter) {
8290 getter->setMethodParams(*clang_ast,
8291 llvm::ArrayRef<clang::ParmVarDecl *>(),
8292 llvm::ArrayRef<clang::SourceLocation>());
8293
8294 class_interface_decl->addDecl(getter);
8295 }
8296 }
8297
8298 if (!setter_sel.isNull() &&
8299 !(isInstance
8300 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8301 : class_interface_decl->lookupClassMethod(setter_sel))) {
8302 clang::QualType result_type = clang_ast->VoidTy;
8303 const bool isVariadic = false;
8304 const bool isSynthesized = false;
8305 const bool isImplicitlyDeclared = true;
8306 const bool isDefined = false;
8307 const clang::ObjCMethodDecl::ImplementationControl impControl =
8308 clang::ObjCMethodDecl::None;
8309 const bool HasRelatedResultType = false;
8310
8311 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8312 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8313 setter_sel, result_type, nullptr, class_interface_decl,
8314 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8315 isDefined, impControl, HasRelatedResultType);
8316
8317 if (setter && metadata)
8318 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8319
8320 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8321
8322 params.push_back(clang::ParmVarDecl::Create(
8323 *clang_ast, setter, clang::SourceLocation(),
8324 clang::SourceLocation(),
8325 nullptr, // anonymous
8326 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8327 clang::SC_Auto, nullptr));
8328
8329 if (setter) {
8330 setter->setMethodParams(
8331 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8332 llvm::ArrayRef<clang::SourceLocation>());
8333
8334 class_interface_decl->addDecl(setter);
8335 }
8336 }
8337
8338 return true;
8339 }
8340 }
8341 }
8342 return false;
8343}
8344
8345bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8346 bool check_superclass) {
8347 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8348 if (class_interface_decl)
8349 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8350 return false;
8351}
8352
8353clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8354 const CompilerType &type,
8355 const char *name, // the full symbol name as seen in the symbol table
8356 // (lldb::opaque_compiler_type_t type, "-[NString
8357 // stringWithCString:]")
8358 const CompilerType &method_clang_type, lldb::AccessType access,
8359 bool is_artificial, bool is_variadic) {
8360 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008361 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008362
Kate Stoneb9c1b512016-09-06 20:57:50 +00008363 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8364
8365 if (class_interface_decl == nullptr)
8366 return nullptr;
8367 ClangASTContext *lldb_ast =
8368 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8369 if (lldb_ast == nullptr)
8370 return nullptr;
8371 clang::ASTContext *ast = lldb_ast->getASTContext();
8372
8373 const char *selector_start = ::strchr(name, ' ');
8374 if (selector_start == nullptr)
8375 return nullptr;
8376
8377 selector_start++;
8378 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8379
8380 size_t len = 0;
8381 const char *start;
8382 // printf ("name = '%s'\n", name);
8383
8384 unsigned num_selectors_with_args = 0;
8385 for (start = selector_start; start && *start != '\0' && *start != ']';
8386 start += len) {
8387 len = ::strcspn(start, ":]");
8388 bool has_arg = (start[len] == ':');
8389 if (has_arg)
8390 ++num_selectors_with_args;
8391 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8392 if (has_arg)
8393 len += 1;
8394 }
8395
8396 if (selector_idents.size() == 0)
8397 return nullptr;
8398
8399 clang::Selector method_selector = ast->Selectors.getSelector(
8400 num_selectors_with_args ? selector_idents.size() : 0,
8401 selector_idents.data());
8402
8403 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8404
8405 // Populate the method decl with parameter decls
8406 const clang::Type *method_type(method_qual_type.getTypePtr());
8407
8408 if (method_type == nullptr)
8409 return nullptr;
8410
8411 const clang::FunctionProtoType *method_function_prototype(
8412 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8413
8414 if (!method_function_prototype)
8415 return nullptr;
8416
8417 bool is_synthesized = false;
8418 bool is_defined = false;
8419 clang::ObjCMethodDecl::ImplementationControl imp_control =
8420 clang::ObjCMethodDecl::None;
8421
8422 const unsigned num_args = method_function_prototype->getNumParams();
8423
8424 if (num_args != num_selectors_with_args)
8425 return nullptr; // some debug information is corrupt. We are not going to
8426 // deal with it.
8427
8428 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8429 *ast,
8430 clang::SourceLocation(), // beginLoc,
8431 clang::SourceLocation(), // endLoc,
8432 method_selector, method_function_prototype->getReturnType(),
8433 nullptr, // TypeSourceInfo *ResultTInfo,
8434 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8435 ClangUtil::GetQualType(type)),
8436 name[0] == '-', is_variadic, is_synthesized,
8437 true, // is_implicitly_declared; we force this to true because we don't
8438 // have source locations
8439 is_defined, imp_control, false /*has_related_result_type*/);
8440
8441 if (objc_method_decl == nullptr)
8442 return nullptr;
8443
8444 if (num_args > 0) {
8445 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8446
8447 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8448 params.push_back(clang::ParmVarDecl::Create(
8449 *ast, objc_method_decl, clang::SourceLocation(),
8450 clang::SourceLocation(),
8451 nullptr, // anonymous
8452 method_function_prototype->getParamType(param_index), nullptr,
8453 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008454 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008455
Kate Stoneb9c1b512016-09-06 20:57:50 +00008456 objc_method_decl->setMethodParams(
8457 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8458 llvm::ArrayRef<clang::SourceLocation>());
8459 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008460
Kate Stoneb9c1b512016-09-06 20:57:50 +00008461 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008462
Greg Claytond8d4a572015-08-11 21:38:15 +00008463#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008464 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008465#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008466
8467 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008468}
8469
Kate Stoneb9c1b512016-09-06 20:57:50 +00008470bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8471 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008472 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008473
Kate Stoneb9c1b512016-09-06 20:57:50 +00008474 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008475
Kate Stoneb9c1b512016-09-06 20:57:50 +00008476 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8477 switch (type_class) {
8478 case clang::Type::Record: {
8479 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8480 if (cxx_record_decl)
8481 return cxx_record_decl->hasExternalLexicalStorage() ||
8482 cxx_record_decl->hasExternalVisibleStorage();
8483 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008484
Kate Stoneb9c1b512016-09-06 20:57:50 +00008485 case clang::Type::Enum: {
8486 clang::EnumDecl *enum_decl =
8487 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8488 if (enum_decl)
8489 return enum_decl->hasExternalLexicalStorage() ||
8490 enum_decl->hasExternalVisibleStorage();
8491 } break;
8492
8493 case clang::Type::ObjCObject:
8494 case clang::Type::ObjCInterface: {
8495 const clang::ObjCObjectType *objc_class_type =
8496 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8497 assert(objc_class_type);
8498 if (objc_class_type) {
8499 clang::ObjCInterfaceDecl *class_interface_decl =
8500 objc_class_type->getInterface();
8501
8502 if (class_interface_decl)
8503 return class_interface_decl->hasExternalLexicalStorage() ||
8504 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008505 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008506 } break;
8507
8508 case clang::Type::Typedef:
8509 return GetHasExternalStorage(CompilerType(
8510 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8511 ->getDecl()
8512 ->getUnderlyingType()
8513 .getAsOpaquePtr()));
8514
8515 case clang::Type::Auto:
8516 return GetHasExternalStorage(CompilerType(
8517 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8518 ->getDeducedType()
8519 .getAsOpaquePtr()));
8520
8521 case clang::Type::Elaborated:
8522 return GetHasExternalStorage(CompilerType(
8523 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8524 ->getNamedType()
8525 .getAsOpaquePtr()));
8526
8527 case clang::Type::Paren:
8528 return GetHasExternalStorage(CompilerType(
8529 type.GetTypeSystem(),
8530 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8531
8532 default:
8533 break;
8534 }
8535 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008536}
8537
Kate Stoneb9c1b512016-09-06 20:57:50 +00008538bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8539 bool has_extern) {
8540 if (!type)
8541 return false;
8542
8543 clang::QualType qual_type(GetCanonicalQualType(type));
8544
8545 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8546 switch (type_class) {
8547 case clang::Type::Record: {
8548 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8549 if (cxx_record_decl) {
8550 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8551 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8552 return true;
8553 }
8554 } break;
8555
8556 case clang::Type::Enum: {
8557 clang::EnumDecl *enum_decl =
8558 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8559 if (enum_decl) {
8560 enum_decl->setHasExternalLexicalStorage(has_extern);
8561 enum_decl->setHasExternalVisibleStorage(has_extern);
8562 return true;
8563 }
8564 } break;
8565
8566 case clang::Type::ObjCObject:
8567 case clang::Type::ObjCInterface: {
8568 const clang::ObjCObjectType *objc_class_type =
8569 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8570 assert(objc_class_type);
8571 if (objc_class_type) {
8572 clang::ObjCInterfaceDecl *class_interface_decl =
8573 objc_class_type->getInterface();
8574
8575 if (class_interface_decl) {
8576 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8577 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8578 return true;
8579 }
8580 }
8581 } break;
8582
8583 case clang::Type::Typedef:
8584 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8585 ->getDecl()
8586 ->getUnderlyingType()
8587 .getAsOpaquePtr(),
8588 has_extern);
8589
8590 case clang::Type::Auto:
8591 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8592 ->getDeducedType()
8593 .getAsOpaquePtr(),
8594 has_extern);
8595
8596 case clang::Type::Elaborated:
8597 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8598 ->getNamedType()
8599 .getAsOpaquePtr(),
8600 has_extern);
8601
8602 case clang::Type::Paren:
8603 return SetHasExternalStorage(
8604 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8605 has_extern);
8606
8607 default:
8608 break;
8609 }
8610 return false;
8611}
Greg Claytond8d4a572015-08-11 21:38:15 +00008612
8613#pragma mark TagDecl
8614
Kate Stoneb9c1b512016-09-06 20:57:50 +00008615bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8616 clang::QualType qual_type(ClangUtil::GetQualType(type));
8617 if (!qual_type.isNull()) {
8618 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8619 if (tag_type) {
8620 clang::TagDecl *tag_decl = tag_type->getDecl();
8621 if (tag_decl) {
8622 tag_decl->startDefinition();
8623 return true;
8624 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008625 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008626
8627 const clang::ObjCObjectType *object_type =
8628 qual_type->getAs<clang::ObjCObjectType>();
8629 if (object_type) {
8630 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8631 if (interface_decl) {
8632 interface_decl->startDefinition();
8633 return true;
8634 }
8635 }
8636 }
8637 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008638}
8639
Kate Stoneb9c1b512016-09-06 20:57:50 +00008640bool ClangASTContext::CompleteTagDeclarationDefinition(
8641 const CompilerType &type) {
8642 clang::QualType qual_type(ClangUtil::GetQualType(type));
8643 if (!qual_type.isNull()) {
8644 // Make sure we use the same methodology as
8645 // ClangASTContext::StartTagDeclarationDefinition()
8646 // as to how we start/end the definition. Previously we were calling
8647 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8648 if (tag_type) {
8649 clang::TagDecl *tag_decl = tag_type->getDecl();
8650 if (tag_decl) {
8651 clang::CXXRecordDecl *cxx_record_decl =
8652 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8653
8654 if (cxx_record_decl) {
8655 if (!cxx_record_decl->isCompleteDefinition())
8656 cxx_record_decl->completeDefinition();
8657 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8658 cxx_record_decl->setHasExternalLexicalStorage(false);
8659 cxx_record_decl->setHasExternalVisibleStorage(false);
8660 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008661 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008662 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008663 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008664
8665 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8666
8667 if (enutype) {
8668 clang::EnumDecl *enum_decl = enutype->getDecl();
8669
8670 if (enum_decl) {
8671 if (!enum_decl->isCompleteDefinition()) {
8672 ClangASTContext *lldb_ast =
8673 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8674 if (lldb_ast == nullptr)
8675 return false;
8676 clang::ASTContext *ast = lldb_ast->getASTContext();
8677
8678 /// TODO This really needs to be fixed.
8679
8680 QualType integer_type(enum_decl->getIntegerType());
8681 if (!integer_type.isNull()) {
8682 unsigned NumPositiveBits = 1;
8683 unsigned NumNegativeBits = 0;
8684
8685 clang::QualType promotion_qual_type;
8686 // If the enum integer type is less than an integer in bit width,
8687 // then we must promote it to an integer size.
8688 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8689 ast->getTypeSize(ast->IntTy)) {
8690 if (enum_decl->getIntegerType()->isSignedIntegerType())
8691 promotion_qual_type = ast->IntTy;
8692 else
8693 promotion_qual_type = ast->UnsignedIntTy;
8694 } else
8695 promotion_qual_type = enum_decl->getIntegerType();
8696
8697 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8698 promotion_qual_type, NumPositiveBits,
8699 NumNegativeBits);
8700 }
8701 }
8702 return true;
8703 }
8704 }
8705 }
8706 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008707}
8708
Kate Stoneb9c1b512016-09-06 20:57:50 +00008709bool ClangASTContext::AddEnumerationValueToEnumerationType(
8710 lldb::opaque_compiler_type_t type,
8711 const CompilerType &enumerator_clang_type, const Declaration &decl,
8712 const char *name, int64_t enum_value, uint32_t enum_value_bit_size) {
8713 if (type && enumerator_clang_type.IsValid() && name && name[0]) {
8714 clang::QualType enum_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008715
Kate Stoneb9c1b512016-09-06 20:57:50 +00008716 bool is_signed = false;
8717 enumerator_clang_type.IsIntegerType(is_signed);
Greg Claytond8d4a572015-08-11 21:38:15 +00008718 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Kate Stoneb9c1b512016-09-06 20:57:50 +00008719 if (clang_type) {
8720 const clang::EnumType *enutype =
8721 llvm::dyn_cast<clang::EnumType>(clang_type);
8722
8723 if (enutype) {
8724 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8725 enum_llvm_apsint = enum_value;
8726 clang::EnumConstantDecl *enumerator_decl =
8727 clang::EnumConstantDecl::Create(
8728 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8729 name ? &getASTContext()->Idents.get(name)
8730 : nullptr, // Identifier
8731 ClangUtil::GetQualType(enumerator_clang_type),
8732 nullptr, enum_llvm_apsint);
8733
8734 if (enumerator_decl) {
8735 enutype->getDecl()->addDecl(enumerator_decl);
8736
8737#ifdef LLDB_CONFIGURATION_DEBUG
8738 VerifyDecl(enumerator_decl);
8739#endif
8740
8741 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008742 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008743 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008744 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008745 }
8746 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008747}
8748
Greg Claytona1e5dc82015-08-11 22:53:00 +00008749CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00008750ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8751 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8752 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8753 if (clang_type) {
8754 const clang::EnumType *enutype =
8755 llvm::dyn_cast<clang::EnumType>(clang_type);
8756 if (enutype) {
8757 clang::EnumDecl *enum_decl = enutype->getDecl();
8758 if (enum_decl)
8759 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008760 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008761 }
8762 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008763}
8764
Kate Stoneb9c1b512016-09-06 20:57:50 +00008765CompilerType
8766ClangASTContext::CreateMemberPointerType(const CompilerType &type,
8767 const CompilerType &pointee_type) {
8768 if (type && pointee_type.IsValid() &&
8769 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8770 ClangASTContext *ast =
8771 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8772 if (!ast)
8773 return CompilerType();
8774 return CompilerType(ast->getASTContext(),
8775 ast->getASTContext()->getMemberPointerType(
8776 ClangUtil::GetQualType(pointee_type),
8777 ClangUtil::GetQualType(type).getTypePtr()));
8778 }
8779 return CompilerType();
8780}
Greg Claytond8d4a572015-08-11 21:38:15 +00008781
8782size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00008783ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
8784 const char *s, uint8_t *dst,
8785 size_t dst_size) {
8786 if (type) {
8787 clang::QualType qual_type(GetCanonicalQualType(type));
8788 uint32_t count = 0;
8789 bool is_complex = false;
8790 if (IsFloatingPointType(type, count, is_complex)) {
8791 // TODO: handle complex and vector types
8792 if (count != 1)
8793 return false;
8794
8795 llvm::StringRef s_sref(s);
8796 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
8797 s_sref);
8798
8799 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
8800 const uint64_t byte_size = bit_size / 8;
8801 if (dst_size >= byte_size) {
8802 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
8803 llvm::NextPowerOf2(byte_size) * 8);
8804 lldb_private::Error get_data_error;
8805 if (scalar.GetAsMemoryData(dst, byte_size,
8806 lldb_private::endian::InlHostByteOrder(),
8807 get_data_error))
8808 return byte_size;
8809 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008810 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008811 }
8812 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00008813}
8814
Greg Claytond8d4a572015-08-11 21:38:15 +00008815//----------------------------------------------------------------------
8816// Dumping types
8817//----------------------------------------------------------------------
8818#define DEPTH_INCREMENT 2
8819
Kate Stoneb9c1b512016-09-06 20:57:50 +00008820void ClangASTContext::DumpValue(
8821 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
8822 lldb::Format format, const lldb_private::DataExtractor &data,
8823 lldb::offset_t data_byte_offset, size_t data_byte_size,
8824 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
8825 bool show_summary, bool verbose, uint32_t depth) {
8826 if (!type)
8827 return;
8828
8829 clang::QualType qual_type(GetQualType(type));
8830 switch (qual_type->getTypeClass()) {
8831 case clang::Type::Record:
8832 if (GetCompleteType(type)) {
8833 const clang::RecordType *record_type =
8834 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8835 const clang::RecordDecl *record_decl = record_type->getDecl();
8836 assert(record_decl);
8837 uint32_t field_bit_offset = 0;
8838 uint32_t field_byte_offset = 0;
8839 const clang::ASTRecordLayout &record_layout =
8840 getASTContext()->getASTRecordLayout(record_decl);
8841 uint32_t child_idx = 0;
8842
8843 const clang::CXXRecordDecl *cxx_record_decl =
8844 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8845 if (cxx_record_decl) {
8846 // We might have base classes to print out first
8847 clang::CXXRecordDecl::base_class_const_iterator base_class,
8848 base_class_end;
8849 for (base_class = cxx_record_decl->bases_begin(),
8850 base_class_end = cxx_record_decl->bases_end();
8851 base_class != base_class_end; ++base_class) {
8852 const clang::CXXRecordDecl *base_class_decl =
8853 llvm::cast<clang::CXXRecordDecl>(
8854 base_class->getType()->getAs<clang::RecordType>()->getDecl());
8855
8856 // Skip empty base classes
8857 if (verbose == false &&
8858 ClangASTContext::RecordHasFields(base_class_decl) == false)
8859 continue;
8860
8861 if (base_class->isVirtual())
8862 field_bit_offset =
8863 record_layout.getVBaseClassOffset(base_class_decl)
8864 .getQuantity() *
8865 8;
8866 else
8867 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
8868 .getQuantity() *
8869 8;
8870 field_byte_offset = field_bit_offset / 8;
8871 assert(field_bit_offset % 8 == 0);
8872 if (child_idx == 0)
8873 s->PutChar('{');
8874 else
8875 s->PutChar(',');
8876
8877 clang::QualType base_class_qual_type = base_class->getType();
8878 std::string base_class_type_name(base_class_qual_type.getAsString());
8879
8880 // Indent and print the base class type name
8881 s->Printf("\n%*s%s ", depth + DEPTH_INCREMENT, "",
8882 base_class_type_name.c_str());
8883
8884 clang::TypeInfo base_class_type_info =
8885 getASTContext()->getTypeInfo(base_class_qual_type);
8886
8887 // Dump the value of the member
8888 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
8889 base_clang_type.DumpValue(
8890 exe_ctx,
8891 s, // Stream to dump to
8892 base_clang_type
8893 .GetFormat(), // The format with which to display the member
8894 data, // Data buffer containing all bytes for this type
8895 data_byte_offset + field_byte_offset, // Offset into "data" where
8896 // to grab value from
8897 base_class_type_info.Width / 8, // Size of this type in bytes
8898 0, // Bitfield bit size
8899 0, // Bitfield bit offset
8900 show_types, // Boolean indicating if we should show the variable
8901 // types
8902 show_summary, // Boolean indicating if we should show a summary
8903 // for the current type
8904 verbose, // Verbose output?
8905 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8906 // children
8907
8908 ++child_idx;
8909 }
8910 }
8911 uint32_t field_idx = 0;
8912 clang::RecordDecl::field_iterator field, field_end;
8913 for (field = record_decl->field_begin(),
8914 field_end = record_decl->field_end();
8915 field != field_end; ++field, ++field_idx, ++child_idx) {
8916 // Print the starting squiggly bracket (if this is the
8917 // first member) or comma (for member 2 and beyond) for
8918 // the struct/union/class member.
8919 if (child_idx == 0)
8920 s->PutChar('{');
8921 else
8922 s->PutChar(',');
8923
8924 // Indent
8925 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8926
8927 clang::QualType field_type = field->getType();
8928 // Print the member type if requested
8929 // Figure out the type byte size (field_type_info.first) and
8930 // alignment (field_type_info.second) from the AST context.
8931 clang::TypeInfo field_type_info =
8932 getASTContext()->getTypeInfo(field_type);
8933 assert(field_idx < record_layout.getFieldCount());
8934 // Figure out the field offset within the current struct/union/class
8935 // type
8936 field_bit_offset = record_layout.getFieldOffset(field_idx);
8937 field_byte_offset = field_bit_offset / 8;
8938 uint32_t field_bitfield_bit_size = 0;
8939 uint32_t field_bitfield_bit_offset = 0;
8940 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
8941 field_bitfield_bit_size))
8942 field_bitfield_bit_offset = field_bit_offset % 8;
8943
8944 if (show_types) {
8945 std::string field_type_name(field_type.getAsString());
8946 if (field_bitfield_bit_size > 0)
8947 s->Printf("(%s:%u) ", field_type_name.c_str(),
8948 field_bitfield_bit_size);
8949 else
8950 s->Printf("(%s) ", field_type_name.c_str());
8951 }
8952 // Print the member name and equal sign
8953 s->Printf("%s = ", field->getNameAsString().c_str());
8954
8955 // Dump the value of the member
8956 CompilerType field_clang_type(getASTContext(), field_type);
8957 field_clang_type.DumpValue(
8958 exe_ctx,
8959 s, // Stream to dump to
8960 field_clang_type
8961 .GetFormat(), // The format with which to display the member
8962 data, // Data buffer containing all bytes for this type
8963 data_byte_offset + field_byte_offset, // Offset into "data" where to
8964 // grab value from
8965 field_type_info.Width / 8, // Size of this type in bytes
8966 field_bitfield_bit_size, // Bitfield bit size
8967 field_bitfield_bit_offset, // Bitfield bit offset
8968 show_types, // Boolean indicating if we should show the variable
8969 // types
8970 show_summary, // Boolean indicating if we should show a summary for
8971 // the current type
8972 verbose, // Verbose output?
8973 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8974 // children
8975 }
8976
8977 // Indent the trailing squiggly bracket
8978 if (child_idx > 0)
8979 s->Printf("\n%*s}", depth, "");
8980 }
8981 return;
8982
8983 case clang::Type::Enum:
8984 if (GetCompleteType(type)) {
8985 const clang::EnumType *enutype =
8986 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8987 const clang::EnumDecl *enum_decl = enutype->getDecl();
8988 assert(enum_decl);
8989 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8990 lldb::offset_t offset = data_byte_offset;
8991 const int64_t enum_value = data.GetMaxU64Bitfield(
8992 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
8993 for (enum_pos = enum_decl->enumerator_begin(),
8994 enum_end_pos = enum_decl->enumerator_end();
8995 enum_pos != enum_end_pos; ++enum_pos) {
8996 if (enum_pos->getInitVal() == enum_value) {
8997 s->Printf("%s", enum_pos->getNameAsString().c_str());
8998 return;
8999 }
9000 }
9001 // If we have gotten here we didn't get find the enumerator in the
9002 // enum decl, so just print the integer.
9003 s->Printf("%" PRIi64, enum_value);
9004 }
9005 return;
9006
9007 case clang::Type::ConstantArray: {
9008 const clang::ConstantArrayType *array =
9009 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9010 bool is_array_of_characters = false;
9011 clang::QualType element_qual_type = array->getElementType();
9012
9013 const clang::Type *canonical_type =
9014 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9015 if (canonical_type)
9016 is_array_of_characters = canonical_type->isCharType();
9017
9018 const uint64_t element_count = array->getSize().getLimitedValue();
9019
9020 clang::TypeInfo field_type_info =
9021 getASTContext()->getTypeInfo(element_qual_type);
9022
9023 uint32_t element_idx = 0;
9024 uint32_t element_offset = 0;
9025 uint64_t element_byte_size = field_type_info.Width / 8;
9026 uint32_t element_stride = element_byte_size;
9027
9028 if (is_array_of_characters) {
9029 s->PutChar('"');
9030 data.Dump(s, data_byte_offset, lldb::eFormatChar, element_byte_size,
9031 element_count, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
9032 s->PutChar('"');
9033 return;
9034 } else {
9035 CompilerType element_clang_type(getASTContext(), element_qual_type);
9036 lldb::Format element_format = element_clang_type.GetFormat();
9037
9038 for (element_idx = 0; element_idx < element_count; ++element_idx) {
9039 // Print the starting squiggly bracket (if this is the
9040 // first member) or comman (for member 2 and beyong) for
9041 // the struct/union/class member.
9042 if (element_idx == 0)
9043 s->PutChar('{');
9044 else
9045 s->PutChar(',');
9046
9047 // Indent and print the index
9048 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9049
9050 // Figure out the field offset within the current struct/union/class
9051 // type
9052 element_offset = element_idx * element_stride;
9053
9054 // Dump the value of the member
9055 element_clang_type.DumpValue(
9056 exe_ctx,
9057 s, // Stream to dump to
9058 element_format, // The format with which to display the element
9059 data, // Data buffer containing all bytes for this type
9060 data_byte_offset +
9061 element_offset, // Offset into "data" where to grab value from
9062 element_byte_size, // Size of this type in bytes
9063 0, // Bitfield bit size
9064 0, // Bitfield bit offset
9065 show_types, // Boolean indicating if we should show the variable
9066 // types
9067 show_summary, // Boolean indicating if we should show a summary for
9068 // the current type
9069 verbose, // Verbose output?
9070 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9071 // children
9072 }
9073
9074 // Indent the trailing squiggly bracket
9075 if (element_idx > 0)
9076 s->Printf("\n%*s}", depth, "");
9077 }
9078 }
9079 return;
9080
9081 case clang::Type::Typedef: {
9082 clang::QualType typedef_qual_type =
9083 llvm::cast<clang::TypedefType>(qual_type)
9084 ->getDecl()
9085 ->getUnderlyingType();
9086
9087 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9088 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9089 clang::TypeInfo typedef_type_info =
9090 getASTContext()->getTypeInfo(typedef_qual_type);
9091 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9092
9093 return typedef_clang_type.DumpValue(
9094 exe_ctx,
9095 s, // Stream to dump to
9096 typedef_format, // The format with which to display the element
9097 data, // Data buffer containing all bytes for this type
9098 data_byte_offset, // Offset into "data" where to grab value from
9099 typedef_byte_size, // Size of this type in bytes
9100 bitfield_bit_size, // Bitfield bit size
9101 bitfield_bit_offset, // Bitfield bit offset
9102 show_types, // Boolean indicating if we should show the variable types
9103 show_summary, // Boolean indicating if we should show a summary for the
9104 // current type
9105 verbose, // Verbose output?
9106 depth); // Scope depth for any types that have children
9107 } break;
9108
9109 case clang::Type::Auto: {
9110 clang::QualType elaborated_qual_type =
9111 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9112 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9113 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9114 clang::TypeInfo elaborated_type_info =
9115 getASTContext()->getTypeInfo(elaborated_qual_type);
9116 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9117
9118 return elaborated_clang_type.DumpValue(
9119 exe_ctx,
9120 s, // Stream to dump to
9121 elaborated_format, // The format with which to display the element
9122 data, // Data buffer containing all bytes for this type
9123 data_byte_offset, // Offset into "data" where to grab value from
9124 elaborated_byte_size, // Size of this type in bytes
9125 bitfield_bit_size, // Bitfield bit size
9126 bitfield_bit_offset, // Bitfield bit offset
9127 show_types, // Boolean indicating if we should show the variable types
9128 show_summary, // Boolean indicating if we should show a summary for the
9129 // current type
9130 verbose, // Verbose output?
9131 depth); // Scope depth for any types that have children
9132 } break;
9133
9134 case clang::Type::Elaborated: {
9135 clang::QualType elaborated_qual_type =
9136 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9137 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9138 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9139 clang::TypeInfo elaborated_type_info =
9140 getASTContext()->getTypeInfo(elaborated_qual_type);
9141 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9142
9143 return elaborated_clang_type.DumpValue(
9144 exe_ctx,
9145 s, // Stream to dump to
9146 elaborated_format, // The format with which to display the element
9147 data, // Data buffer containing all bytes for this type
9148 data_byte_offset, // Offset into "data" where to grab value from
9149 elaborated_byte_size, // Size of this type in bytes
9150 bitfield_bit_size, // Bitfield bit size
9151 bitfield_bit_offset, // Bitfield bit offset
9152 show_types, // Boolean indicating if we should show the variable types
9153 show_summary, // Boolean indicating if we should show a summary for the
9154 // current type
9155 verbose, // Verbose output?
9156 depth); // Scope depth for any types that have children
9157 } break;
9158
9159 case clang::Type::Paren: {
9160 clang::QualType desugar_qual_type =
9161 llvm::cast<clang::ParenType>(qual_type)->desugar();
9162 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9163
9164 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9165 clang::TypeInfo desugar_type_info =
9166 getASTContext()->getTypeInfo(desugar_qual_type);
9167 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9168
9169 return desugar_clang_type.DumpValue(
9170 exe_ctx,
9171 s, // Stream to dump to
9172 desugar_format, // The format with which to display the element
9173 data, // Data buffer containing all bytes for this type
9174 data_byte_offset, // Offset into "data" where to grab value from
9175 desugar_byte_size, // Size of this type in bytes
9176 bitfield_bit_size, // Bitfield bit size
9177 bitfield_bit_offset, // Bitfield bit offset
9178 show_types, // Boolean indicating if we should show the variable types
9179 show_summary, // Boolean indicating if we should show a summary for the
9180 // current type
9181 verbose, // Verbose output?
9182 depth); // Scope depth for any types that have children
9183 } break;
9184
9185 default:
9186 // We are down to a scalar type that we just need to display.
9187 data.Dump(s, data_byte_offset, format, data_byte_size, 1, UINT32_MAX,
9188 LLDB_INVALID_ADDRESS, bitfield_bit_size, bitfield_bit_offset);
9189
9190 if (show_summary)
9191 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9192 break;
9193 }
9194}
9195
9196bool ClangASTContext::DumpTypeValue(
9197 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
9198 const lldb_private::DataExtractor &data, lldb::offset_t byte_offset,
9199 size_t byte_size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
9200 ExecutionContextScope *exe_scope) {
9201 if (!type)
9202 return false;
9203 if (IsAggregateType(type)) {
9204 return false;
9205 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009206 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009207
9208 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9209 switch (type_class) {
9210 case clang::Type::Typedef: {
9211 clang::QualType typedef_qual_type =
9212 llvm::cast<clang::TypedefType>(qual_type)
9213 ->getDecl()
9214 ->getUnderlyingType();
9215 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9216 if (format == eFormatDefault)
9217 format = typedef_clang_type.GetFormat();
9218 clang::TypeInfo typedef_type_info =
9219 getASTContext()->getTypeInfo(typedef_qual_type);
9220 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9221
9222 return typedef_clang_type.DumpTypeValue(
9223 s,
9224 format, // The format with which to display the element
9225 data, // Data buffer containing all bytes for this type
9226 byte_offset, // Offset into "data" where to grab value from
9227 typedef_byte_size, // Size of this type in bytes
9228 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9229 // treat as a bitfield
9230 bitfield_bit_offset, // Offset in bits of a bitfield value if
9231 // bitfield_bit_size != 0
9232 exe_scope);
9233 } break;
9234
9235 case clang::Type::Enum:
9236 // If our format is enum or default, show the enumeration value as
9237 // its enumeration string value, else just display it as requested.
9238 if ((format == eFormatEnum || format == eFormatDefault) &&
9239 GetCompleteType(type)) {
9240 const clang::EnumType *enutype =
9241 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9242 const clang::EnumDecl *enum_decl = enutype->getDecl();
9243 assert(enum_decl);
9244 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9245 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9246 lldb::offset_t offset = byte_offset;
9247 if (is_signed) {
9248 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9249 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9250 for (enum_pos = enum_decl->enumerator_begin(),
9251 enum_end_pos = enum_decl->enumerator_end();
9252 enum_pos != enum_end_pos; ++enum_pos) {
9253 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
9254 s->PutCString(enum_pos->getNameAsString().c_str());
9255 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009256 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009257 }
9258 // If we have gotten here we didn't get find the enumerator in the
9259 // enum decl, so just print the integer.
9260 s->Printf("%" PRIi64, enum_svalue);
9261 } else {
9262 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9263 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9264 for (enum_pos = enum_decl->enumerator_begin(),
9265 enum_end_pos = enum_decl->enumerator_end();
9266 enum_pos != enum_end_pos; ++enum_pos) {
9267 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
9268 s->PutCString(enum_pos->getNameAsString().c_str());
9269 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009270 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009271 }
9272 // If we have gotten here we didn't get find the enumerator in the
9273 // enum decl, so just print the integer.
9274 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009275 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009276 return true;
9277 }
9278 // format was not enum, just fall through and dump the value as
9279 // requested....
9280 LLVM_FALLTHROUGH;
9281
9282 default:
9283 // We are down to a scalar type that we just need to display.
9284 {
9285 uint32_t item_count = 1;
9286 // A few formats, we might need to modify our size and count for
9287 // depending
9288 // on how we are trying to display the value...
9289 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009290 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009291 case eFormatBoolean:
9292 case eFormatBinary:
9293 case eFormatComplex:
9294 case eFormatCString: // NULL terminated C strings
9295 case eFormatDecimal:
9296 case eFormatEnum:
9297 case eFormatHex:
9298 case eFormatHexUppercase:
9299 case eFormatFloat:
9300 case eFormatOctal:
9301 case eFormatOSType:
9302 case eFormatUnsigned:
9303 case eFormatPointer:
9304 case eFormatVectorOfChar:
9305 case eFormatVectorOfSInt8:
9306 case eFormatVectorOfUInt8:
9307 case eFormatVectorOfSInt16:
9308 case eFormatVectorOfUInt16:
9309 case eFormatVectorOfSInt32:
9310 case eFormatVectorOfUInt32:
9311 case eFormatVectorOfSInt64:
9312 case eFormatVectorOfUInt64:
9313 case eFormatVectorOfFloat32:
9314 case eFormatVectorOfFloat64:
9315 case eFormatVectorOfUInt128:
9316 break;
9317
9318 case eFormatChar:
9319 case eFormatCharPrintable:
9320 case eFormatCharArray:
9321 case eFormatBytes:
9322 case eFormatBytesWithASCII:
9323 item_count = byte_size;
9324 byte_size = 1;
9325 break;
9326
9327 case eFormatUnicode16:
9328 item_count = byte_size / 2;
9329 byte_size = 2;
9330 break;
9331
9332 case eFormatUnicode32:
9333 item_count = byte_size / 4;
9334 byte_size = 4;
9335 break;
9336 }
9337 return data.Dump(s, byte_offset, format, byte_size, item_count,
9338 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9339 bitfield_bit_offset, exe_scope);
9340 }
9341 break;
9342 }
9343 }
9344 return 0;
9345}
9346
9347void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9348 ExecutionContext *exe_ctx, Stream *s,
9349 const lldb_private::DataExtractor &data,
9350 lldb::offset_t data_byte_offset,
9351 size_t data_byte_size) {
9352 uint32_t length = 0;
9353 if (IsCStringType(type, length)) {
9354 if (exe_ctx) {
9355 Process *process = exe_ctx->GetProcessPtr();
9356 if (process) {
9357 lldb::offset_t offset = data_byte_offset;
9358 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9359 std::vector<uint8_t> buf;
9360 if (length > 0)
9361 buf.resize(length);
9362 else
9363 buf.resize(256);
9364
9365 lldb_private::DataExtractor cstr_data(&buf.front(), buf.size(),
9366 process->GetByteOrder(), 4);
9367 buf.back() = '\0';
9368 size_t bytes_read;
9369 size_t total_cstr_len = 0;
9370 Error error;
9371 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9372 buf.size(), error)) > 0) {
9373 const size_t len = strlen((const char *)&buf.front());
9374 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009375 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009376 if (total_cstr_len == 0)
9377 s->PutCString(" \"");
9378 cstr_data.Dump(s, 0, lldb::eFormatChar, 1, len, UINT32_MAX,
9379 LLDB_INVALID_ADDRESS, 0, 0);
9380 total_cstr_len += len;
9381 if (len < buf.size())
9382 break;
9383 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009384 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009385 if (total_cstr_len > 0)
9386 s->PutChar('"');
9387 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009388 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009389 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009390}
9391
Kate Stoneb9c1b512016-09-06 20:57:50 +00009392void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9393 StreamFile s(stdout, false);
9394 DumpTypeDescription(type, &s);
9395 ClangASTMetadata *metadata =
9396 ClangASTContext::GetMetadata(getASTContext(), type);
9397 if (metadata) {
9398 metadata->Dump(&s);
9399 }
9400}
Greg Claytond8d4a572015-08-11 21:38:15 +00009401
Kate Stoneb9c1b512016-09-06 20:57:50 +00009402void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9403 Stream *s) {
9404 if (type) {
9405 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009406
Kate Stoneb9c1b512016-09-06 20:57:50 +00009407 llvm::SmallVector<char, 1024> buf;
9408 llvm::raw_svector_ostream llvm_ostrm(buf);
9409
9410 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9411 switch (type_class) {
9412 case clang::Type::ObjCObject:
9413 case clang::Type::ObjCInterface: {
9414 GetCompleteType(type);
9415
9416 const clang::ObjCObjectType *objc_class_type =
9417 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9418 assert(objc_class_type);
9419 if (objc_class_type) {
9420 clang::ObjCInterfaceDecl *class_interface_decl =
9421 objc_class_type->getInterface();
9422 if (class_interface_decl) {
9423 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9424 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009425 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009426 }
9427 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009428
Kate Stoneb9c1b512016-09-06 20:57:50 +00009429 case clang::Type::Typedef: {
9430 const clang::TypedefType *typedef_type =
9431 qual_type->getAs<clang::TypedefType>();
9432 if (typedef_type) {
9433 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9434 std::string clang_typedef_name(
9435 typedef_decl->getQualifiedNameAsString());
9436 if (!clang_typedef_name.empty()) {
9437 s->PutCString("typedef ");
9438 s->PutCString(clang_typedef_name.c_str());
Greg Claytond8d4a572015-08-11 21:38:15 +00009439 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009440 }
9441 } break;
9442
9443 case clang::Type::Auto:
9444 CompilerType(getASTContext(),
9445 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9446 .DumpTypeDescription(s);
9447 return;
9448
9449 case clang::Type::Elaborated:
9450 CompilerType(getASTContext(),
9451 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9452 .DumpTypeDescription(s);
9453 return;
9454
9455 case clang::Type::Paren:
9456 CompilerType(getASTContext(),
9457 llvm::cast<clang::ParenType>(qual_type)->desugar())
9458 .DumpTypeDescription(s);
9459 return;
9460
9461 case clang::Type::Record: {
9462 GetCompleteType(type);
9463
9464 const clang::RecordType *record_type =
9465 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9466 const clang::RecordDecl *record_decl = record_type->getDecl();
9467 const clang::CXXRecordDecl *cxx_record_decl =
9468 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9469
9470 if (cxx_record_decl)
9471 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9472 s->GetIndentLevel());
9473 else
9474 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9475 s->GetIndentLevel());
9476 } break;
9477
9478 default: {
9479 const clang::TagType *tag_type =
9480 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9481 if (tag_type) {
9482 clang::TagDecl *tag_decl = tag_type->getDecl();
9483 if (tag_decl)
9484 tag_decl->print(llvm_ostrm, 0);
9485 } else {
9486 std::string clang_type_name(qual_type.getAsString());
9487 if (!clang_type_name.empty())
9488 s->PutCString(clang_type_name.c_str());
9489 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009490 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009491 }
9492
Kate Stoneb9c1b512016-09-06 20:57:50 +00009493 if (buf.size() > 0) {
9494 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009495 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009496 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009497}
9498
Kate Stoneb9c1b512016-09-06 20:57:50 +00009499void ClangASTContext::DumpTypeName(const CompilerType &type) {
9500 if (ClangUtil::IsClangType(type)) {
9501 clang::QualType qual_type(
9502 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9503
9504 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9505 switch (type_class) {
9506 case clang::Type::Record: {
9507 const clang::CXXRecordDecl *cxx_record_decl =
9508 qual_type->getAsCXXRecordDecl();
9509 if (cxx_record_decl)
9510 printf("class %s", cxx_record_decl->getName().str().c_str());
9511 } break;
9512
9513 case clang::Type::Enum: {
9514 clang::EnumDecl *enum_decl =
9515 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9516 if (enum_decl) {
9517 printf("enum %s", enum_decl->getName().str().c_str());
9518 }
9519 } break;
9520
9521 case clang::Type::ObjCObject:
9522 case clang::Type::ObjCInterface: {
9523 const clang::ObjCObjectType *objc_class_type =
9524 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9525 if (objc_class_type) {
9526 clang::ObjCInterfaceDecl *class_interface_decl =
9527 objc_class_type->getInterface();
9528 // We currently can't complete objective C types through the newly added
9529 // ASTContext
9530 // because it only supports TagDecl objects right now...
9531 if (class_interface_decl)
9532 printf("@class %s", class_interface_decl->getName().str().c_str());
9533 }
9534 } break;
9535
9536 case clang::Type::Typedef:
9537 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9538 ->getDecl()
9539 ->getName()
9540 .str()
9541 .c_str());
9542 break;
9543
9544 case clang::Type::Auto:
9545 printf("auto ");
9546 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9547 llvm::cast<clang::AutoType>(qual_type)
9548 ->getDeducedType()
9549 .getAsOpaquePtr()));
9550
9551 case clang::Type::Elaborated:
9552 printf("elaborated ");
9553 return DumpTypeName(CompilerType(
9554 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9555 ->getNamedType()
9556 .getAsOpaquePtr()));
9557
9558 case clang::Type::Paren:
9559 printf("paren ");
9560 return DumpTypeName(CompilerType(
9561 type.GetTypeSystem(),
9562 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9563
9564 default:
9565 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9566 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009567 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009568 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009569}
9570
Kate Stoneb9c1b512016-09-06 20:57:50 +00009571clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9572 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9573 const char *parent_name, int tag_decl_kind,
9574 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9575 if (template_param_infos.IsValid()) {
9576 std::string template_basename(parent_name);
9577 template_basename.erase(template_basename.find('<'));
9578
9579 return CreateClassTemplateDecl(decl_ctx, access_type,
9580 template_basename.c_str(), tag_decl_kind,
9581 template_param_infos);
9582 }
9583 return NULL;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009584}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009585
Kate Stoneb9c1b512016-09-06 20:57:50 +00009586void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9587 ClangASTContext *ast = (ClangASTContext *)baton;
9588 SymbolFile *sym_file = ast->GetSymbolFile();
9589 if (sym_file) {
9590 CompilerType clang_type = GetTypeForDecl(decl);
9591 if (clang_type)
9592 sym_file->CompleteType(clang_type);
9593 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009594}
9595
Kate Stoneb9c1b512016-09-06 20:57:50 +00009596void ClangASTContext::CompleteObjCInterfaceDecl(
9597 void *baton, clang::ObjCInterfaceDecl *decl) {
9598 ClangASTContext *ast = (ClangASTContext *)baton;
9599 SymbolFile *sym_file = ast->GetSymbolFile();
9600 if (sym_file) {
9601 CompilerType clang_type = GetTypeForDecl(decl);
9602 if (clang_type)
9603 sym_file->CompleteType(clang_type);
9604 }
Zachary Turner42dff792016-04-15 00:21:26 +00009605}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009606
Kate Stoneb9c1b512016-09-06 20:57:50 +00009607DWARFASTParser *ClangASTContext::GetDWARFParser() {
9608 if (!m_dwarf_ast_parser_ap)
9609 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9610 return m_dwarf_ast_parser_ap.get();
9611}
9612
9613PDBASTParser *ClangASTContext::GetPDBParser() {
9614 if (!m_pdb_ast_parser_ap)
9615 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9616 return m_pdb_ast_parser_ap.get();
9617}
9618
9619bool ClangASTContext::LayoutRecordType(
9620 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9621 uint64_t &alignment,
9622 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9623 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9624 &base_offsets,
9625 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9626 &vbase_offsets) {
9627 ClangASTContext *ast = (ClangASTContext *)baton;
9628 DWARFASTParserClang *dwarf_ast_parser =
9629 (DWARFASTParserClang *)ast->GetDWARFParser();
9630 return dwarf_ast_parser->GetClangASTImporter().LayoutRecordType(
9631 record_decl, bit_size, alignment, field_offsets, base_offsets,
9632 vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009633}
9634
Greg Clayton99558cc42015-08-24 23:46:31 +00009635//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009636// CompilerDecl override functions
9637//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009638
Kate Stoneb9c1b512016-09-06 20:57:50 +00009639ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9640 if (opaque_decl) {
9641 clang::NamedDecl *nd =
9642 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9643 if (nd != nullptr)
9644 return ConstString(nd->getDeclName().getAsString());
9645 }
9646 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009647}
9648
Kate Stoneb9c1b512016-09-06 20:57:50 +00009649ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9650 if (opaque_decl) {
9651 clang::NamedDecl *nd =
9652 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9653 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9654 clang::MangleContext *mc = getMangleContext();
9655 if (mc && mc->shouldMangleCXXName(nd)) {
9656 llvm::SmallVector<char, 1024> buf;
9657 llvm::raw_svector_ostream llvm_ostrm(buf);
9658 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9659 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9660 Ctor_Complete, llvm_ostrm);
9661 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9662 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9663 Dtor_Complete, llvm_ostrm);
9664 } else {
9665 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009666 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009667 if (buf.size() > 0)
9668 return ConstString(buf.data(), buf.size());
9669 }
Greg Claytonfe689042015-11-10 17:47:04 +00009670 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009671 }
9672 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009673}
9674
Kate Stoneb9c1b512016-09-06 20:57:50 +00009675CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9676 if (opaque_decl)
9677 return CompilerDeclContext(this,
9678 ((clang::Decl *)opaque_decl)->getDeclContext());
9679 else
9680 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009681}
9682
Kate Stoneb9c1b512016-09-06 20:57:50 +00009683CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9684 if (clang::FunctionDecl *func_decl =
9685 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9686 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9687 if (clang::ObjCMethodDecl *objc_method =
9688 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9689 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9690 else
Greg Claytonfe689042015-11-10 17:47:04 +00009691 return CompilerType();
9692}
9693
Kate Stoneb9c1b512016-09-06 20:57:50 +00009694size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9695 if (clang::FunctionDecl *func_decl =
9696 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9697 return func_decl->param_size();
9698 if (clang::ObjCMethodDecl *objc_method =
9699 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9700 return objc_method->param_size();
9701 else
9702 return 0;
9703}
9704
9705CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9706 size_t idx) {
9707 if (clang::FunctionDecl *func_decl =
9708 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9709 if (idx < func_decl->param_size()) {
9710 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9711 if (var_decl)
9712 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9713 }
9714 } else if (clang::ObjCMethodDecl *objc_method =
9715 llvm::dyn_cast<clang::ObjCMethodDecl>(
9716 (clang::Decl *)opaque_decl)) {
9717 if (idx < objc_method->param_size())
9718 return CompilerType(
9719 this,
9720 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9721 }
9722 return CompilerType();
9723}
9724
Paul Hermand628cbb2015-09-15 23:44:17 +00009725//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00009726// CompilerDeclContext functions
9727//----------------------------------------------------------------------
9728
Kate Stoneb9c1b512016-09-06 20:57:50 +00009729std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9730 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9731 std::vector<CompilerDecl> found_decls;
9732 if (opaque_decl_ctx) {
9733 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9734 std::set<DeclContext *> searched;
9735 std::multimap<DeclContext *, DeclContext *> search_queue;
9736 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009737
Kate Stoneb9c1b512016-09-06 20:57:50 +00009738 for (clang::DeclContext *decl_context = root_decl_ctx;
9739 decl_context != nullptr && found_decls.empty();
9740 decl_context = decl_context->getParent()) {
9741 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +00009742
Kate Stoneb9c1b512016-09-06 20:57:50 +00009743 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9744 it++) {
9745 if (!searched.insert(it->second).second)
9746 continue;
9747 symbol_file->ParseDeclsForContext(
9748 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +00009749
Kate Stoneb9c1b512016-09-06 20:57:50 +00009750 for (clang::Decl *child : it->second->decls()) {
9751 if (clang::UsingDirectiveDecl *ud =
9752 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9753 if (ignore_using_decls)
9754 continue;
9755 clang::DeclContext *from = ud->getCommonAncestor();
9756 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9757 search_queue.insert(
9758 std::make_pair(from, ud->getNominatedNamespace()));
9759 } else if (clang::UsingDecl *ud =
9760 llvm::dyn_cast<clang::UsingDecl>(child)) {
9761 if (ignore_using_decls)
9762 continue;
9763 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9764 clang::Decl *target = usd->getTargetDecl();
9765 if (clang::NamedDecl *nd =
9766 llvm::dyn_cast<clang::NamedDecl>(target)) {
9767 IdentifierInfo *ii = nd->getIdentifier();
9768 if (ii != nullptr &&
9769 ii->getName().equals(name.AsCString(nullptr)))
9770 found_decls.push_back(CompilerDecl(this, nd));
9771 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009772 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009773 } else if (clang::NamedDecl *nd =
9774 llvm::dyn_cast<clang::NamedDecl>(child)) {
9775 IdentifierInfo *ii = nd->getIdentifier();
9776 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9777 found_decls.push_back(CompilerDecl(this, nd));
9778 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009779 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009780 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009781 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009782 }
9783 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +00009784}
9785
Dawn Perchikb5925782015-12-12 19:31:41 +00009786// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009787// and return the number of levels it took to find it, or
9788// LLDB_INVALID_DECL_LEVEL
9789// if not found. If the decl was imported via a using declaration, its name
9790// and/or
9791// type, if set, will be used to check that the decl found in the scope is a
9792// match.
Dawn Perchikb5925782015-12-12 19:31:41 +00009793//
Kate Stoneb9c1b512016-09-06 20:57:50 +00009794// The optional name is required by languages (like C++) to handle using
9795// declarations
Dawn Perchikb5925782015-12-12 19:31:41 +00009796// like:
9797//
9798// void poo();
9799// namespace ns {
9800// void foo();
9801// void goo();
9802// }
9803// void bar() {
9804// using ns::foo;
9805// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
9806// // LLDB_INVALID_DECL_LEVEL for 'goo'.
9807// }
9808//
9809// The optional type is useful in the case that there's a specific overload
9810// that we're looking for that might otherwise be shadowed, like:
9811//
9812// void foo(int);
9813// namespace ns {
9814// void foo();
9815// }
9816// void bar() {
9817// using ns::foo;
9818// // CountDeclLevels returns 0 for { 'foo', void() },
9819// // 1 for { 'foo', void(int) }, and
9820// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
9821// }
9822//
9823// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +00009824// function at file scope will return the same level as a function at global
9825// scope.
9826// Ideally we'd like to treat the file scope as an additional scope just below
9827// the
9828// global scope. More work needs to be done to recognise that, if the decl
9829// we're
9830// trying to look up is static, we should compare its source file with that of
9831// the
Dawn Perchikb5925782015-12-12 19:31:41 +00009832// current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009833uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
9834 clang::DeclContext *child_decl_ctx,
9835 ConstString *child_name,
9836 CompilerType *child_type) {
9837 if (frame_decl_ctx) {
9838 std::set<DeclContext *> searched;
9839 std::multimap<DeclContext *, DeclContext *> search_queue;
9840 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +00009841
Kate Stoneb9c1b512016-09-06 20:57:50 +00009842 // Get the lookup scope for the decl we're trying to find.
9843 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +00009844
Kate Stoneb9c1b512016-09-06 20:57:50 +00009845 // Look for it in our scope's decl context and its parents.
9846 uint32_t level = 0;
9847 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
9848 decl_ctx = decl_ctx->getParent()) {
9849 if (!decl_ctx->isLookupContext())
9850 continue;
9851 if (decl_ctx == parent_decl_ctx)
9852 // Found it!
9853 return level;
9854 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
9855 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
9856 it++) {
9857 if (searched.find(it->second) != searched.end())
9858 continue;
9859
9860 // Currently DWARF has one shared translation unit for all Decls at top
9861 // level, so this
9862 // would erroneously find using statements anywhere. So don't look at
9863 // the top-level
9864 // translation unit.
9865 // TODO fix this and add a testcase that depends on it.
9866
9867 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
9868 continue;
9869
9870 searched.insert(it->second);
9871 symbol_file->ParseDeclsForContext(
9872 CompilerDeclContext(this, it->second));
9873
9874 for (clang::Decl *child : it->second->decls()) {
9875 if (clang::UsingDirectiveDecl *ud =
9876 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9877 clang::DeclContext *ns = ud->getNominatedNamespace();
9878 if (ns == parent_decl_ctx)
9879 // Found it!
9880 return level;
9881 clang::DeclContext *from = ud->getCommonAncestor();
9882 if (searched.find(ns) == searched.end())
9883 search_queue.insert(std::make_pair(from, ns));
9884 } else if (child_name) {
9885 if (clang::UsingDecl *ud =
9886 llvm::dyn_cast<clang::UsingDecl>(child)) {
9887 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9888 clang::Decl *target = usd->getTargetDecl();
9889 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
9890 if (!nd)
9891 continue;
9892 // Check names.
9893 IdentifierInfo *ii = nd->getIdentifier();
9894 if (ii == nullptr ||
9895 !ii->getName().equals(child_name->AsCString(nullptr)))
9896 continue;
9897 // Check types, if one was provided.
9898 if (child_type) {
9899 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
9900 if (!AreTypesSame(clang_type, *child_type,
9901 /*ignore_qualifiers=*/true))
9902 continue;
9903 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009904 // Found it!
9905 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009906 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009907 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009908 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009909 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009910 }
9911 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +00009912 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009913 }
9914 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +00009915}
9916
Kate Stoneb9c1b512016-09-06 20:57:50 +00009917bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
9918 if (opaque_decl_ctx)
9919 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
9920 else
Greg Clayton99558cc42015-08-24 23:46:31 +00009921 return false;
9922}
9923
Kate Stoneb9c1b512016-09-06 20:57:50 +00009924ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
9925 if (opaque_decl_ctx) {
9926 clang::NamedDecl *named_decl =
9927 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9928 if (named_decl)
9929 return ConstString(named_decl->getName());
9930 }
9931 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +00009932}
9933
Kate Stoneb9c1b512016-09-06 20:57:50 +00009934ConstString
9935ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
9936 if (opaque_decl_ctx) {
9937 clang::NamedDecl *named_decl =
9938 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9939 if (named_decl)
9940 return ConstString(
9941 llvm::StringRef(named_decl->getQualifiedNameAsString()));
9942 }
9943 return ConstString();
9944}
9945
9946bool ClangASTContext::DeclContextIsClassMethod(
9947 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
9948 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
9949 if (opaque_decl_ctx) {
9950 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9951 if (ObjCMethodDecl *objc_method =
9952 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
9953 if (is_instance_method_ptr)
9954 *is_instance_method_ptr = objc_method->isInstanceMethod();
9955 if (language_ptr)
9956 *language_ptr = eLanguageTypeObjC;
9957 if (language_object_name_ptr)
9958 language_object_name_ptr->SetCString("self");
9959 return true;
9960 } else if (CXXMethodDecl *cxx_method =
9961 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
9962 if (is_instance_method_ptr)
9963 *is_instance_method_ptr = cxx_method->isInstance();
9964 if (language_ptr)
9965 *language_ptr = eLanguageTypeC_plus_plus;
9966 if (language_object_name_ptr)
9967 language_object_name_ptr->SetCString("this");
9968 return true;
9969 } else if (clang::FunctionDecl *function_decl =
9970 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
9971 ClangASTMetadata *metadata =
9972 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
9973 if (metadata && metadata->HasObjectPtr()) {
9974 if (is_instance_method_ptr)
9975 *is_instance_method_ptr = true;
9976 if (language_ptr)
9977 *language_ptr = eLanguageTypeObjC;
9978 if (language_object_name_ptr)
9979 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
9980 return true;
9981 }
9982 }
9983 }
9984 return false;
9985}
9986
9987clang::DeclContext *
9988ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
9989 if (dc.IsClang())
9990 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
9991 return nullptr;
9992}
Greg Clayton99558cc42015-08-24 23:46:31 +00009993
9994ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00009995ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
9996 if (dc.IsClang())
9997 return llvm::dyn_cast<clang::ObjCMethodDecl>(
9998 (clang::DeclContext *)dc.GetOpaqueDeclContext());
9999 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010000}
10001
10002CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010003ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10004 if (dc.IsClang())
10005 return llvm::dyn_cast<clang::CXXMethodDecl>(
10006 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10007 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010008}
10009
10010clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010011ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10012 if (dc.IsClang())
10013 return llvm::dyn_cast<clang::FunctionDecl>(
10014 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10015 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010016}
10017
10018clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010019ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10020 if (dc.IsClang())
10021 return llvm::dyn_cast<clang::NamespaceDecl>(
10022 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10023 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010024}
10025
10026ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010027ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10028 const void *object) {
10029 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10030 if (ast)
10031 return ClangASTContext::GetMetadata(ast, object);
10032 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010033}
10034
10035clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010036ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10037 ClangASTContext *ast =
10038 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10039 if (ast)
10040 return ast->getASTContext();
10041 return nullptr;
10042}
10043
10044ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10045 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10046 m_target_wp(target.shared_from_this()),
10047 m_persistent_variables(new ClangPersistentVariables) {}
10048
10049UserExpression *ClangASTContextForExpressions::GetUserExpression(
10050 const char *expr, const char *expr_prefix, lldb::LanguageType language,
10051 Expression::ResultType desired_type,
10052 const EvaluateExpressionOptions &options) {
10053 TargetSP target_sp = m_target_wp.lock();
10054 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010055 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010056
10057 return new ClangUserExpression(*target_sp.get(), expr, expr_prefix, language,
10058 desired_type, options);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010059}
10060
Kate Stoneb9c1b512016-09-06 20:57:50 +000010061FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10062 const CompilerType &return_type, const Address &function_address,
10063 const ValueList &arg_value_list, const char *name) {
10064 TargetSP target_sp = m_target_wp.lock();
10065 if (!target_sp)
10066 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010067
Kate Stoneb9c1b512016-09-06 20:57:50 +000010068 Process *process = target_sp->GetProcessSP().get();
10069 if (!process)
10070 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010071
Kate Stoneb9c1b512016-09-06 20:57:50 +000010072 return new ClangFunctionCaller(*process, return_type, function_address,
10073 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010074}
10075
10076UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010077ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10078 const char *name) {
10079 TargetSP target_sp = m_target_wp.lock();
10080 if (!target_sp)
10081 return nullptr;
10082
10083 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010084}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010085
10086PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010087ClangASTContextForExpressions::GetPersistentExpressionState() {
10088 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010089}