blob: e7fda6d4e8ade998e1fe8589c9503025abba7889 [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;
4320 }
4321 // We don't know hot to display this type...
4322 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004323}
4324
Kate Stoneb9c1b512016-09-06 20:57:50 +00004325unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4326 if (type)
4327 return GetQualType(type).getQualifiers().getCVRQualifiers();
4328 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004329}
4330
4331//----------------------------------------------------------------------
4332// Creating related types
4333//----------------------------------------------------------------------
4334
Greg Claytona1e5dc82015-08-11 22:53:00 +00004335CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004336ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4337 uint64_t *stride) {
4338 if (type) {
4339 clang::QualType qual_type(GetCanonicalQualType(type));
4340
4341 const clang::Type *array_eletype =
4342 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4343
4344 if (!array_eletype)
4345 return CompilerType();
4346
4347 CompilerType element_type(getASTContext(),
4348 array_eletype->getCanonicalTypeUnqualified());
4349
4350 // TODO: the real stride will be >= this value.. find the real one!
4351 if (stride)
4352 *stride = element_type.GetByteSize(nullptr);
4353
4354 return element_type;
4355 }
4356 return CompilerType();
4357}
4358
4359CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4360 uint64_t size) {
4361 if (type) {
4362 clang::QualType qual_type(GetCanonicalQualType(type));
4363 if (clang::ASTContext *ast_ctx = getASTContext()) {
4364 if (size != 0)
4365 return CompilerType(
4366 ast_ctx, ast_ctx->getConstantArrayType(
4367 qual_type, llvm::APInt(64, size),
4368 clang::ArrayType::ArraySizeModifier::Normal, 0));
4369 else
4370 return CompilerType(
4371 ast_ctx,
4372 ast_ctx->getIncompleteArrayType(
4373 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
Greg Claytond8d4a572015-08-11 21:38:15 +00004374 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004375 }
4376
4377 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004378}
4379
Greg Claytona1e5dc82015-08-11 22:53:00 +00004380CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004381ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4382 if (type)
4383 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4384 return CompilerType();
4385}
4386
4387static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4388 clang::QualType qual_type) {
4389 if (qual_type->isPointerType())
4390 qual_type = ast->getPointerType(
4391 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4392 else
4393 qual_type = qual_type.getUnqualifiedType();
4394 qual_type.removeLocalConst();
4395 qual_type.removeLocalRestrict();
4396 qual_type.removeLocalVolatile();
4397 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004398}
4399
4400CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004401ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4402 if (type)
4403 return CompilerType(
4404 getASTContext(),
4405 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4406 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004407}
4408
Kate Stoneb9c1b512016-09-06 20:57:50 +00004409int ClangASTContext::GetFunctionArgumentCount(
4410 lldb::opaque_compiler_type_t type) {
4411 if (type) {
4412 const clang::FunctionProtoType *func =
4413 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4414 if (func)
4415 return func->getNumParams();
4416 }
4417 return -1;
4418}
4419
4420CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4421 lldb::opaque_compiler_type_t type, size_t idx) {
4422 if (type) {
4423 const clang::FunctionProtoType *func =
4424 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4425 if (func) {
4426 const uint32_t num_args = func->getNumParams();
4427 if (idx < num_args)
4428 return CompilerType(getASTContext(), func->getParamType(idx));
4429 }
4430 }
4431 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004432}
4433
Greg Claytona1e5dc82015-08-11 22:53:00 +00004434CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004435ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4436 if (type) {
4437 clang::QualType qual_type(GetQualType(type));
4438 const clang::FunctionProtoType *func =
4439 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4440 if (func)
4441 return CompilerType(getASTContext(), func->getReturnType());
4442 }
4443 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004444}
4445
4446size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004447ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4448 size_t num_functions = 0;
4449 if (type) {
4450 clang::QualType qual_type(GetCanonicalQualType(type));
4451 switch (qual_type->getTypeClass()) {
4452 case clang::Type::Record:
4453 if (GetCompleteQualType(getASTContext(), qual_type)) {
4454 const clang::RecordType *record_type =
4455 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4456 const clang::RecordDecl *record_decl = record_type->getDecl();
4457 assert(record_decl);
4458 const clang::CXXRecordDecl *cxx_record_decl =
4459 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4460 if (cxx_record_decl)
4461 num_functions = std::distance(cxx_record_decl->method_begin(),
4462 cxx_record_decl->method_end());
4463 }
4464 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004465
Kate Stoneb9c1b512016-09-06 20:57:50 +00004466 case clang::Type::ObjCObjectPointer:
4467 if (GetCompleteType(type)) {
4468 const clang::ObjCObjectPointerType *objc_class_type =
4469 qual_type->getAsObjCInterfacePointerType();
4470 if (objc_class_type) {
4471 clang::ObjCInterfaceDecl *class_interface_decl =
4472 objc_class_type->getInterfaceDecl();
4473 if (class_interface_decl)
4474 num_functions = std::distance(class_interface_decl->meth_begin(),
4475 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004476 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004477 }
4478 break;
4479
4480 case clang::Type::ObjCObject:
4481 case clang::Type::ObjCInterface:
4482 if (GetCompleteType(type)) {
4483 const clang::ObjCObjectType *objc_class_type =
4484 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4485 if (objc_class_type) {
4486 clang::ObjCInterfaceDecl *class_interface_decl =
4487 objc_class_type->getInterface();
4488 if (class_interface_decl)
4489 num_functions = std::distance(class_interface_decl->meth_begin(),
4490 class_interface_decl->meth_end());
4491 }
4492 }
4493 break;
4494
4495 case clang::Type::Typedef:
4496 return CompilerType(getASTContext(),
4497 llvm::cast<clang::TypedefType>(qual_type)
4498 ->getDecl()
4499 ->getUnderlyingType())
4500 .GetNumMemberFunctions();
4501
4502 case clang::Type::Auto:
4503 return CompilerType(
4504 getASTContext(),
4505 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4506 .GetNumMemberFunctions();
4507
4508 case clang::Type::Elaborated:
4509 return CompilerType(
4510 getASTContext(),
4511 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4512 .GetNumMemberFunctions();
4513
4514 case clang::Type::Paren:
4515 return CompilerType(getASTContext(),
4516 llvm::cast<clang::ParenType>(qual_type)->desugar())
4517 .GetNumMemberFunctions();
4518
4519 default:
4520 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004521 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004522 }
4523 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004524}
4525
4526TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004527ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4528 size_t idx) {
4529 std::string name;
4530 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4531 CompilerType clang_type;
4532 CompilerDecl clang_decl;
4533 if (type) {
4534 clang::QualType qual_type(GetCanonicalQualType(type));
4535 switch (qual_type->getTypeClass()) {
4536 case clang::Type::Record:
4537 if (GetCompleteQualType(getASTContext(), qual_type)) {
4538 const clang::RecordType *record_type =
4539 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4540 const clang::RecordDecl *record_decl = record_type->getDecl();
4541 assert(record_decl);
4542 const clang::CXXRecordDecl *cxx_record_decl =
4543 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4544 if (cxx_record_decl) {
4545 auto method_iter = cxx_record_decl->method_begin();
4546 auto method_end = cxx_record_decl->method_end();
4547 if (idx <
4548 static_cast<size_t>(std::distance(method_iter, method_end))) {
4549 std::advance(method_iter, idx);
4550 clang::CXXMethodDecl *cxx_method_decl =
4551 method_iter->getCanonicalDecl();
4552 if (cxx_method_decl) {
4553 name = cxx_method_decl->getDeclName().getAsString();
4554 if (cxx_method_decl->isStatic())
4555 kind = lldb::eMemberFunctionKindStaticMethod;
4556 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4557 kind = lldb::eMemberFunctionKindConstructor;
4558 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4559 kind = lldb::eMemberFunctionKindDestructor;
4560 else
4561 kind = lldb::eMemberFunctionKindInstanceMethod;
4562 clang_type = CompilerType(
4563 this, cxx_method_decl->getType().getAsOpaquePtr());
4564 clang_decl = CompilerDecl(this, cxx_method_decl);
4565 }
4566 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004567 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004568 }
4569 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004570
Kate Stoneb9c1b512016-09-06 20:57:50 +00004571 case clang::Type::ObjCObjectPointer:
4572 if (GetCompleteType(type)) {
4573 const clang::ObjCObjectPointerType *objc_class_type =
4574 qual_type->getAsObjCInterfacePointerType();
4575 if (objc_class_type) {
4576 clang::ObjCInterfaceDecl *class_interface_decl =
4577 objc_class_type->getInterfaceDecl();
4578 if (class_interface_decl) {
4579 auto method_iter = class_interface_decl->meth_begin();
4580 auto method_end = class_interface_decl->meth_end();
4581 if (idx <
4582 static_cast<size_t>(std::distance(method_iter, method_end))) {
4583 std::advance(method_iter, idx);
4584 clang::ObjCMethodDecl *objc_method_decl =
4585 method_iter->getCanonicalDecl();
4586 if (objc_method_decl) {
4587 clang_decl = CompilerDecl(this, objc_method_decl);
4588 name = objc_method_decl->getSelector().getAsString();
4589 if (objc_method_decl->isClassMethod())
4590 kind = lldb::eMemberFunctionKindStaticMethod;
4591 else
4592 kind = lldb::eMemberFunctionKindInstanceMethod;
4593 }
4594 }
4595 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004596 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004597 }
4598 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004599
Kate Stoneb9c1b512016-09-06 20:57:50 +00004600 case clang::Type::ObjCObject:
4601 case clang::Type::ObjCInterface:
4602 if (GetCompleteType(type)) {
4603 const clang::ObjCObjectType *objc_class_type =
4604 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4605 if (objc_class_type) {
4606 clang::ObjCInterfaceDecl *class_interface_decl =
4607 objc_class_type->getInterface();
4608 if (class_interface_decl) {
4609 auto method_iter = class_interface_decl->meth_begin();
4610 auto method_end = class_interface_decl->meth_end();
4611 if (idx <
4612 static_cast<size_t>(std::distance(method_iter, method_end))) {
4613 std::advance(method_iter, idx);
4614 clang::ObjCMethodDecl *objc_method_decl =
4615 method_iter->getCanonicalDecl();
4616 if (objc_method_decl) {
4617 clang_decl = CompilerDecl(this, objc_method_decl);
4618 name = objc_method_decl->getSelector().getAsString();
4619 if (objc_method_decl->isClassMethod())
4620 kind = lldb::eMemberFunctionKindStaticMethod;
4621 else
4622 kind = lldb::eMemberFunctionKindInstanceMethod;
4623 }
4624 }
4625 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004626 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004627 }
4628 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004629
Kate Stoneb9c1b512016-09-06 20:57:50 +00004630 case clang::Type::Typedef:
4631 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4632 ->getDecl()
4633 ->getUnderlyingType()
4634 .getAsOpaquePtr(),
4635 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004636
Kate Stoneb9c1b512016-09-06 20:57:50 +00004637 case clang::Type::Auto:
4638 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4639 ->getDeducedType()
4640 .getAsOpaquePtr(),
4641 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004642
Kate Stoneb9c1b512016-09-06 20:57:50 +00004643 case clang::Type::Elaborated:
4644 return GetMemberFunctionAtIndex(
4645 llvm::cast<clang::ElaboratedType>(qual_type)
4646 ->getNamedType()
4647 .getAsOpaquePtr(),
4648 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004649
Kate Stoneb9c1b512016-09-06 20:57:50 +00004650 case clang::Type::Paren:
4651 return GetMemberFunctionAtIndex(
4652 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4653 idx);
4654
4655 default:
4656 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004657 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004658 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004659
Kate Stoneb9c1b512016-09-06 20:57:50 +00004660 if (kind == eMemberFunctionKindUnknown)
4661 return TypeMemberFunctionImpl();
4662 else
4663 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004664}
4665
Greg Claytona1e5dc82015-08-11 22:53:00 +00004666CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004667ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4668 if (type)
4669 return CompilerType(getASTContext(),
4670 GetQualType(type).getNonReferenceType());
4671 return CompilerType();
4672}
4673
4674CompilerType ClangASTContext::CreateTypedefType(
4675 const CompilerType &type, const char *typedef_name,
4676 const CompilerDeclContext &compiler_decl_ctx) {
4677 if (type && typedef_name && typedef_name[0]) {
4678 ClangASTContext *ast =
4679 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4680 if (!ast)
4681 return CompilerType();
4682 clang::ASTContext *clang_ast = ast->getASTContext();
4683 clang::QualType qual_type(ClangUtil::GetQualType(type));
4684
4685 clang::DeclContext *decl_ctx =
4686 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4687 if (decl_ctx == nullptr)
4688 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4689
4690 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4691 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4692 &clang_ast->Idents.get(typedef_name),
4693 clang_ast->getTrivialTypeSourceInfo(qual_type));
4694
4695 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4696
4697 // Get a uniqued clang::QualType for the typedef decl type
4698 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4699 }
4700 return CompilerType();
4701}
4702
4703CompilerType
4704ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4705 if (type) {
4706 clang::QualType qual_type(GetQualType(type));
4707 return CompilerType(getASTContext(),
4708 qual_type.getTypePtr()->getPointeeType());
4709 }
4710 return CompilerType();
4711}
4712
4713CompilerType
4714ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4715 if (type) {
4716 clang::QualType qual_type(GetQualType(type));
4717
4718 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4719 switch (type_class) {
4720 case clang::Type::ObjCObject:
4721 case clang::Type::ObjCInterface:
4722 return CompilerType(getASTContext(),
4723 getASTContext()->getObjCObjectPointerType(qual_type));
4724
4725 default:
4726 return CompilerType(getASTContext(),
4727 getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004728 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004729 }
4730 return CompilerType();
4731}
4732
4733CompilerType
4734ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4735 if (type)
4736 return CompilerType(this, getASTContext()
4737 ->getLValueReferenceType(GetQualType(type))
4738 .getAsOpaquePtr());
4739 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004740 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004741}
4742
Kate Stoneb9c1b512016-09-06 20:57:50 +00004743CompilerType
4744ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4745 if (type)
4746 return CompilerType(this, getASTContext()
4747 ->getRValueReferenceType(GetQualType(type))
4748 .getAsOpaquePtr());
4749 else
4750 return CompilerType();
4751}
4752
4753CompilerType
4754ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4755 if (type) {
4756 clang::QualType result(GetQualType(type));
4757 result.addConst();
4758 return CompilerType(this, result.getAsOpaquePtr());
4759 }
4760 return CompilerType();
4761}
4762
4763CompilerType
4764ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4765 if (type) {
4766 clang::QualType result(GetQualType(type));
4767 result.addVolatile();
4768 return CompilerType(this, result.getAsOpaquePtr());
4769 }
4770 return CompilerType();
4771}
4772
4773CompilerType
4774ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4775 if (type) {
4776 clang::QualType result(GetQualType(type));
4777 result.addRestrict();
4778 return CompilerType(this, result.getAsOpaquePtr());
4779 }
4780 return CompilerType();
4781}
4782
4783CompilerType
4784ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4785 const char *typedef_name,
4786 const CompilerDeclContext &compiler_decl_ctx) {
4787 if (type) {
4788 clang::ASTContext *clang_ast = getASTContext();
4789 clang::QualType qual_type(GetQualType(type));
4790
4791 clang::DeclContext *decl_ctx =
4792 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4793 if (decl_ctx == nullptr)
4794 decl_ctx = getASTContext()->getTranslationUnitDecl();
4795
4796 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4797 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4798 &clang_ast->Idents.get(typedef_name),
4799 clang_ast->getTrivialTypeSourceInfo(qual_type));
4800
4801 clang::TagDecl *tdecl = nullptr;
4802 if (!qual_type.isNull()) {
4803 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4804 tdecl = rt->getDecl();
4805 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4806 tdecl = et->getDecl();
4807 }
4808
4809 // Check whether this declaration is an anonymous struct, union, or enum,
4810 // hidden behind a typedef. If so, we
4811 // try to check whether we have a typedef tag to attach to the original
4812 // record declaration
4813 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4814 tdecl->setTypedefNameForAnonDecl(decl);
4815
4816 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4817
4818 // Get a uniqued clang::QualType for the typedef decl type
4819 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4820 }
4821 return CompilerType();
4822}
4823
4824CompilerType
4825ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4826 if (type) {
4827 const clang::TypedefType *typedef_type =
4828 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4829 if (typedef_type)
4830 return CompilerType(getASTContext(),
4831 typedef_type->getDecl()->getUnderlyingType());
4832 }
4833 return CompilerType();
4834}
Greg Claytond8d4a572015-08-11 21:38:15 +00004835
4836//----------------------------------------------------------------------
4837// Create related types using the current type's AST
4838//----------------------------------------------------------------------
4839
Kate Stoneb9c1b512016-09-06 20:57:50 +00004840CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4841 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004842}
4843//----------------------------------------------------------------------
4844// Exploring the type
4845//----------------------------------------------------------------------
4846
Kate Stoneb9c1b512016-09-06 20:57:50 +00004847uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4848 ExecutionContextScope *exe_scope) {
4849 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00004850 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004851 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004852 switch (type_class) {
4853 case clang::Type::Record:
4854 if (GetCompleteType(type))
4855 return getASTContext()->getTypeSize(qual_type);
4856 else
4857 return 0;
4858 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004859
Kate Stoneb9c1b512016-09-06 20:57:50 +00004860 case clang::Type::ObjCInterface:
4861 case clang::Type::ObjCObject: {
4862 ExecutionContext exe_ctx(exe_scope);
4863 Process *process = exe_ctx.GetProcessPtr();
4864 if (process) {
4865 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4866 if (objc_runtime) {
4867 uint64_t bit_size = 0;
4868 if (objc_runtime->GetTypeBitSize(
4869 CompilerType(getASTContext(), qual_type), bit_size))
4870 return bit_size;
4871 }
4872 } else {
4873 static bool g_printed = false;
4874 if (!g_printed) {
4875 StreamString s;
4876 DumpTypeDescription(type, &s);
4877
4878 llvm::outs() << "warning: trying to determine the size of type ";
4879 llvm::outs() << s.GetString() << "\n";
4880 llvm::outs() << "without a valid ExecutionContext. this is not "
4881 "reliable. please file a bug against LLDB.\n";
4882 llvm::outs() << "backtrace:\n";
4883 llvm::sys::PrintStackTrace(llvm::outs());
4884 llvm::outs() << "\n";
4885 g_printed = true;
4886 }
4887 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004888 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004889 LLVM_FALLTHROUGH;
4890 default:
4891 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
4892 if (bit_size == 0) {
4893 if (qual_type->isIncompleteArrayType())
4894 return getASTContext()->getTypeSize(
4895 qual_type->getArrayElementTypeNoTypeQual()
4896 ->getCanonicalTypeUnqualified());
4897 }
4898 if (qual_type->isObjCObjectOrInterfaceType())
4899 return bit_size +
4900 getASTContext()->getTypeSize(
4901 getASTContext()->ObjCBuiltinClassTy);
4902 return bit_size;
4903 }
4904 }
4905 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004906}
4907
Kate Stoneb9c1b512016-09-06 20:57:50 +00004908size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
4909 if (GetCompleteType(type))
4910 return getASTContext()->getTypeAlign(GetQualType(type));
4911 return 0;
4912}
4913
4914lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
4915 uint64_t &count) {
4916 if (!type)
4917 return lldb::eEncodingInvalid;
4918
4919 count = 1;
4920 clang::QualType qual_type(GetCanonicalQualType(type));
4921
4922 switch (qual_type->getTypeClass()) {
4923 case clang::Type::UnaryTransform:
4924 break;
4925
4926 case clang::Type::FunctionNoProto:
4927 case clang::Type::FunctionProto:
4928 break;
4929
4930 case clang::Type::IncompleteArray:
4931 case clang::Type::VariableArray:
4932 break;
4933
4934 case clang::Type::ConstantArray:
4935 break;
4936
4937 case clang::Type::ExtVector:
4938 case clang::Type::Vector:
4939 // TODO: Set this to more than one???
4940 break;
4941
4942 case clang::Type::Builtin:
4943 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4944 case clang::BuiltinType::Void:
4945 break;
4946
4947 case clang::BuiltinType::Bool:
4948 case clang::BuiltinType::Char_S:
4949 case clang::BuiltinType::SChar:
4950 case clang::BuiltinType::WChar_S:
4951 case clang::BuiltinType::Char16:
4952 case clang::BuiltinType::Char32:
4953 case clang::BuiltinType::Short:
4954 case clang::BuiltinType::Int:
4955 case clang::BuiltinType::Long:
4956 case clang::BuiltinType::LongLong:
4957 case clang::BuiltinType::Int128:
4958 return lldb::eEncodingSint;
4959
4960 case clang::BuiltinType::Char_U:
4961 case clang::BuiltinType::UChar:
4962 case clang::BuiltinType::WChar_U:
4963 case clang::BuiltinType::UShort:
4964 case clang::BuiltinType::UInt:
4965 case clang::BuiltinType::ULong:
4966 case clang::BuiltinType::ULongLong:
4967 case clang::BuiltinType::UInt128:
4968 return lldb::eEncodingUint;
4969
4970 case clang::BuiltinType::Half:
4971 case clang::BuiltinType::Float:
4972 case clang::BuiltinType::Float128:
4973 case clang::BuiltinType::Double:
4974 case clang::BuiltinType::LongDouble:
4975 return lldb::eEncodingIEEE754;
4976
4977 case clang::BuiltinType::ObjCClass:
4978 case clang::BuiltinType::ObjCId:
4979 case clang::BuiltinType::ObjCSel:
4980 return lldb::eEncodingUint;
4981
4982 case clang::BuiltinType::NullPtr:
4983 return lldb::eEncodingUint;
4984
4985 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4986 case clang::BuiltinType::Kind::BoundMember:
4987 case clang::BuiltinType::Kind::BuiltinFn:
4988 case clang::BuiltinType::Kind::Dependent:
4989 case clang::BuiltinType::Kind::OCLClkEvent:
4990 case clang::BuiltinType::Kind::OCLEvent:
4991 case clang::BuiltinType::Kind::OCLImage1dRO:
4992 case clang::BuiltinType::Kind::OCLImage1dWO:
4993 case clang::BuiltinType::Kind::OCLImage1dRW:
4994 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
4995 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
4996 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
4997 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
4998 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
4999 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5000 case clang::BuiltinType::Kind::OCLImage2dRO:
5001 case clang::BuiltinType::Kind::OCLImage2dWO:
5002 case clang::BuiltinType::Kind::OCLImage2dRW:
5003 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5004 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5005 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5006 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5007 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5008 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5009 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5010 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5011 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5012 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5013 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5014 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5015 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5016 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5017 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5018 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5019 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5020 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5021 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5022 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5023 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5024 case clang::BuiltinType::Kind::OCLImage3dRO:
5025 case clang::BuiltinType::Kind::OCLImage3dWO:
5026 case clang::BuiltinType::Kind::OCLImage3dRW:
5027 case clang::BuiltinType::Kind::OCLQueue:
5028 case clang::BuiltinType::Kind::OCLNDRange:
5029 case clang::BuiltinType::Kind::OCLReserveID:
5030 case clang::BuiltinType::Kind::OCLSampler:
5031 case clang::BuiltinType::Kind::OMPArraySection:
5032 case clang::BuiltinType::Kind::Overload:
5033 case clang::BuiltinType::Kind::PseudoObject:
5034 case clang::BuiltinType::Kind::UnknownAny:
5035 break;
5036 }
5037 break;
5038 // All pointer types are represented as unsigned integer encodings.
5039 // We may nee to add a eEncodingPointer if we ever need to know the
5040 // difference
5041 case clang::Type::ObjCObjectPointer:
5042 case clang::Type::BlockPointer:
5043 case clang::Type::Pointer:
5044 case clang::Type::LValueReference:
5045 case clang::Type::RValueReference:
5046 case clang::Type::MemberPointer:
5047 return lldb::eEncodingUint;
5048 case clang::Type::Complex: {
5049 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5050 if (qual_type->isComplexType())
5051 encoding = lldb::eEncodingIEEE754;
5052 else {
5053 const clang::ComplexType *complex_type =
5054 qual_type->getAsComplexIntegerType();
5055 if (complex_type)
5056 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5057 .GetEncoding(count);
5058 else
5059 encoding = lldb::eEncodingSint;
5060 }
5061 count = 2;
5062 return encoding;
5063 }
5064
5065 case clang::Type::ObjCInterface:
5066 break;
5067 case clang::Type::Record:
5068 break;
5069 case clang::Type::Enum:
5070 return lldb::eEncodingSint;
5071 case clang::Type::Typedef:
5072 return CompilerType(getASTContext(),
5073 llvm::cast<clang::TypedefType>(qual_type)
5074 ->getDecl()
5075 ->getUnderlyingType())
5076 .GetEncoding(count);
5077
5078 case clang::Type::Auto:
5079 return CompilerType(
5080 getASTContext(),
5081 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5082 .GetEncoding(count);
5083
5084 case clang::Type::Elaborated:
5085 return CompilerType(
5086 getASTContext(),
5087 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5088 .GetEncoding(count);
5089
5090 case clang::Type::Paren:
5091 return CompilerType(getASTContext(),
5092 llvm::cast<clang::ParenType>(qual_type)->desugar())
5093 .GetEncoding(count);
5094
5095 case clang::Type::DependentSizedArray:
5096 case clang::Type::DependentSizedExtVector:
5097 case clang::Type::UnresolvedUsing:
5098 case clang::Type::Attributed:
5099 case clang::Type::TemplateTypeParm:
5100 case clang::Type::SubstTemplateTypeParm:
5101 case clang::Type::SubstTemplateTypeParmPack:
5102 case clang::Type::InjectedClassName:
5103 case clang::Type::DependentName:
5104 case clang::Type::DependentTemplateSpecialization:
5105 case clang::Type::PackExpansion:
5106 case clang::Type::ObjCObject:
5107
5108 case clang::Type::TypeOfExpr:
5109 case clang::Type::TypeOf:
5110 case clang::Type::Decltype:
5111 case clang::Type::TemplateSpecialization:
5112 case clang::Type::Atomic:
5113 case clang::Type::Adjusted:
5114 case clang::Type::Pipe:
5115 break;
5116
5117 // pointer type decayed from an array or function type.
5118 case clang::Type::Decayed:
5119 break;
5120 }
5121 count = 0;
5122 return lldb::eEncodingInvalid;
5123}
5124
5125lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5126 if (!type)
5127 return lldb::eFormatDefault;
5128
5129 clang::QualType qual_type(GetCanonicalQualType(type));
5130
5131 switch (qual_type->getTypeClass()) {
5132 case clang::Type::UnaryTransform:
5133 break;
5134
5135 case clang::Type::FunctionNoProto:
5136 case clang::Type::FunctionProto:
5137 break;
5138
5139 case clang::Type::IncompleteArray:
5140 case clang::Type::VariableArray:
5141 break;
5142
5143 case clang::Type::ConstantArray:
5144 return lldb::eFormatVoid; // no value
5145
5146 case clang::Type::ExtVector:
5147 case clang::Type::Vector:
5148 break;
5149
5150 case clang::Type::Builtin:
5151 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5152 // default: assert(0 && "Unknown builtin type!");
5153 case clang::BuiltinType::UnknownAny:
5154 case clang::BuiltinType::Void:
5155 case clang::BuiltinType::BoundMember:
5156 break;
5157
5158 case clang::BuiltinType::Bool:
5159 return lldb::eFormatBoolean;
5160 case clang::BuiltinType::Char_S:
5161 case clang::BuiltinType::SChar:
5162 case clang::BuiltinType::WChar_S:
5163 case clang::BuiltinType::Char_U:
5164 case clang::BuiltinType::UChar:
5165 case clang::BuiltinType::WChar_U:
5166 return lldb::eFormatChar;
5167 case clang::BuiltinType::Char16:
5168 return lldb::eFormatUnicode16;
5169 case clang::BuiltinType::Char32:
5170 return lldb::eFormatUnicode32;
5171 case clang::BuiltinType::UShort:
5172 return lldb::eFormatUnsigned;
5173 case clang::BuiltinType::Short:
5174 return lldb::eFormatDecimal;
5175 case clang::BuiltinType::UInt:
5176 return lldb::eFormatUnsigned;
5177 case clang::BuiltinType::Int:
5178 return lldb::eFormatDecimal;
5179 case clang::BuiltinType::ULong:
5180 return lldb::eFormatUnsigned;
5181 case clang::BuiltinType::Long:
5182 return lldb::eFormatDecimal;
5183 case clang::BuiltinType::ULongLong:
5184 return lldb::eFormatUnsigned;
5185 case clang::BuiltinType::LongLong:
5186 return lldb::eFormatDecimal;
5187 case clang::BuiltinType::UInt128:
5188 return lldb::eFormatUnsigned;
5189 case clang::BuiltinType::Int128:
5190 return lldb::eFormatDecimal;
5191 case clang::BuiltinType::Half:
5192 case clang::BuiltinType::Float:
5193 case clang::BuiltinType::Double:
5194 case clang::BuiltinType::LongDouble:
5195 return lldb::eFormatFloat;
5196 default:
5197 return lldb::eFormatHex;
5198 }
5199 break;
5200 case clang::Type::ObjCObjectPointer:
5201 return lldb::eFormatHex;
5202 case clang::Type::BlockPointer:
5203 return lldb::eFormatHex;
5204 case clang::Type::Pointer:
5205 return lldb::eFormatHex;
5206 case clang::Type::LValueReference:
5207 case clang::Type::RValueReference:
5208 return lldb::eFormatHex;
5209 case clang::Type::MemberPointer:
5210 break;
5211 case clang::Type::Complex: {
5212 if (qual_type->isComplexType())
5213 return lldb::eFormatComplex;
5214 else
5215 return lldb::eFormatComplexInteger;
5216 }
5217 case clang::Type::ObjCInterface:
5218 break;
5219 case clang::Type::Record:
5220 break;
5221 case clang::Type::Enum:
5222 return lldb::eFormatEnum;
5223 case clang::Type::Typedef:
5224 return CompilerType(getASTContext(),
5225 llvm::cast<clang::TypedefType>(qual_type)
5226 ->getDecl()
5227 ->getUnderlyingType())
5228 .GetFormat();
5229 case clang::Type::Auto:
5230 return CompilerType(getASTContext(),
5231 llvm::cast<clang::AutoType>(qual_type)->desugar())
5232 .GetFormat();
5233 case clang::Type::Paren:
5234 return CompilerType(getASTContext(),
5235 llvm::cast<clang::ParenType>(qual_type)->desugar())
5236 .GetFormat();
5237 case clang::Type::Elaborated:
5238 return CompilerType(
5239 getASTContext(),
5240 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5241 .GetFormat();
5242 case clang::Type::DependentSizedArray:
5243 case clang::Type::DependentSizedExtVector:
5244 case clang::Type::UnresolvedUsing:
5245 case clang::Type::Attributed:
5246 case clang::Type::TemplateTypeParm:
5247 case clang::Type::SubstTemplateTypeParm:
5248 case clang::Type::SubstTemplateTypeParmPack:
5249 case clang::Type::InjectedClassName:
5250 case clang::Type::DependentName:
5251 case clang::Type::DependentTemplateSpecialization:
5252 case clang::Type::PackExpansion:
5253 case clang::Type::ObjCObject:
5254
5255 case clang::Type::TypeOfExpr:
5256 case clang::Type::TypeOf:
5257 case clang::Type::Decltype:
5258 case clang::Type::TemplateSpecialization:
5259 case clang::Type::Atomic:
5260 case clang::Type::Adjusted:
5261 case clang::Type::Pipe:
5262 break;
5263
5264 // pointer type decayed from an array or function type.
5265 case clang::Type::Decayed:
5266 break;
5267 }
5268 // We don't know hot to display this type...
5269 return lldb::eFormatBytes;
5270}
5271
5272static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5273 bool check_superclass) {
5274 while (class_interface_decl) {
5275 if (class_interface_decl->ivar_size() > 0)
5276 return true;
5277
5278 if (check_superclass)
5279 class_interface_decl = class_interface_decl->getSuperClass();
5280 else
5281 break;
5282 }
5283 return false;
5284}
5285
5286uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
5287 bool omit_empty_base_classes) {
5288 if (!type)
5289 return 0;
5290
5291 uint32_t num_children = 0;
5292 clang::QualType qual_type(GetQualType(type));
5293 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5294 switch (type_class) {
5295 case clang::Type::Builtin:
5296 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5297 case clang::BuiltinType::ObjCId: // child is Class
5298 case clang::BuiltinType::ObjCClass: // child is Class
5299 num_children = 1;
5300 break;
5301
5302 default:
5303 break;
5304 }
5305 break;
5306
5307 case clang::Type::Complex:
5308 return 0;
5309
5310 case clang::Type::Record:
5311 if (GetCompleteQualType(getASTContext(), qual_type)) {
5312 const clang::RecordType *record_type =
5313 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5314 const clang::RecordDecl *record_decl = record_type->getDecl();
5315 assert(record_decl);
5316 const clang::CXXRecordDecl *cxx_record_decl =
5317 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5318 if (cxx_record_decl) {
5319 if (omit_empty_base_classes) {
5320 // Check each base classes to see if it or any of its
5321 // base classes contain any fields. This can help
5322 // limit the noise in variable views by not having to
5323 // show base classes that contain no members.
5324 clang::CXXRecordDecl::base_class_const_iterator base_class,
5325 base_class_end;
5326 for (base_class = cxx_record_decl->bases_begin(),
5327 base_class_end = cxx_record_decl->bases_end();
5328 base_class != base_class_end; ++base_class) {
5329 const clang::CXXRecordDecl *base_class_decl =
5330 llvm::cast<clang::CXXRecordDecl>(
5331 base_class->getType()
5332 ->getAs<clang::RecordType>()
5333 ->getDecl());
5334
5335 // Skip empty base classes
5336 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5337 continue;
5338
5339 num_children++;
5340 }
5341 } else {
5342 // Include all base classes
5343 num_children += cxx_record_decl->getNumBases();
5344 }
5345 }
5346 clang::RecordDecl::field_iterator field, field_end;
5347 for (field = record_decl->field_begin(),
5348 field_end = record_decl->field_end();
5349 field != field_end; ++field)
5350 ++num_children;
5351 }
5352 break;
5353
5354 case clang::Type::ObjCObject:
5355 case clang::Type::ObjCInterface:
5356 if (GetCompleteQualType(getASTContext(), qual_type)) {
5357 const clang::ObjCObjectType *objc_class_type =
5358 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5359 assert(objc_class_type);
5360 if (objc_class_type) {
5361 clang::ObjCInterfaceDecl *class_interface_decl =
5362 objc_class_type->getInterface();
5363
5364 if (class_interface_decl) {
5365
5366 clang::ObjCInterfaceDecl *superclass_interface_decl =
5367 class_interface_decl->getSuperClass();
5368 if (superclass_interface_decl) {
5369 if (omit_empty_base_classes) {
5370 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5371 ++num_children;
5372 } else
5373 ++num_children;
5374 }
5375
5376 num_children += class_interface_decl->ivar_size();
5377 }
5378 }
5379 }
5380 break;
5381
5382 case clang::Type::ObjCObjectPointer: {
5383 const clang::ObjCObjectPointerType *pointer_type =
5384 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5385 clang::QualType pointee_type = pointer_type->getPointeeType();
5386 uint32_t num_pointee_children =
5387 CompilerType(getASTContext(), pointee_type)
5388 .GetNumChildren(omit_empty_base_classes);
5389 // If this type points to a simple type, then it has 1 child
5390 if (num_pointee_children == 0)
5391 num_children = 1;
5392 else
5393 num_children = num_pointee_children;
5394 } break;
5395
5396 case clang::Type::Vector:
5397 case clang::Type::ExtVector:
5398 num_children =
5399 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5400 break;
5401
5402 case clang::Type::ConstantArray:
5403 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5404 ->getSize()
5405 .getLimitedValue();
5406 break;
5407
5408 case clang::Type::Pointer: {
5409 const clang::PointerType *pointer_type =
5410 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5411 clang::QualType pointee_type(pointer_type->getPointeeType());
5412 uint32_t num_pointee_children =
5413 CompilerType(getASTContext(), pointee_type)
5414 .GetNumChildren(omit_empty_base_classes);
5415 if (num_pointee_children == 0) {
5416 // We have a pointer to a pointee type that claims it has no children.
5417 // We will want to look at
5418 num_children = GetNumPointeeChildren(pointee_type);
5419 } else
5420 num_children = num_pointee_children;
5421 } break;
5422
5423 case clang::Type::LValueReference:
5424 case clang::Type::RValueReference: {
5425 const clang::ReferenceType *reference_type =
5426 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5427 clang::QualType pointee_type = reference_type->getPointeeType();
5428 uint32_t num_pointee_children =
5429 CompilerType(getASTContext(), pointee_type)
5430 .GetNumChildren(omit_empty_base_classes);
5431 // If this type points to a simple type, then it has 1 child
5432 if (num_pointee_children == 0)
5433 num_children = 1;
5434 else
5435 num_children = num_pointee_children;
5436 } break;
5437
5438 case clang::Type::Typedef:
5439 num_children =
5440 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5441 ->getDecl()
5442 ->getUnderlyingType())
5443 .GetNumChildren(omit_empty_base_classes);
5444 break;
5445
5446 case clang::Type::Auto:
5447 num_children =
5448 CompilerType(getASTContext(),
5449 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5450 .GetNumChildren(omit_empty_base_classes);
5451 break;
5452
5453 case clang::Type::Elaborated:
5454 num_children =
5455 CompilerType(
5456 getASTContext(),
5457 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5458 .GetNumChildren(omit_empty_base_classes);
5459 break;
5460
5461 case clang::Type::Paren:
5462 num_children =
5463 CompilerType(getASTContext(),
5464 llvm::cast<clang::ParenType>(qual_type)->desugar())
5465 .GetNumChildren(omit_empty_base_classes);
5466 break;
5467 default:
5468 break;
5469 }
5470 return num_children;
5471}
5472
5473CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5474 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005475}
5476
Greg Claytond8d4a572015-08-11 21:38:15 +00005477lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005478ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5479 if (type) {
5480 clang::QualType qual_type(GetQualType(type));
5481 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5482 if (type_class == clang::Type::Builtin) {
5483 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5484 case clang::BuiltinType::Void:
5485 return eBasicTypeVoid;
5486 case clang::BuiltinType::Bool:
5487 return eBasicTypeBool;
5488 case clang::BuiltinType::Char_S:
5489 return eBasicTypeSignedChar;
5490 case clang::BuiltinType::Char_U:
5491 return eBasicTypeUnsignedChar;
5492 case clang::BuiltinType::Char16:
5493 return eBasicTypeChar16;
5494 case clang::BuiltinType::Char32:
5495 return eBasicTypeChar32;
5496 case clang::BuiltinType::UChar:
5497 return eBasicTypeUnsignedChar;
5498 case clang::BuiltinType::SChar:
5499 return eBasicTypeSignedChar;
5500 case clang::BuiltinType::WChar_S:
5501 return eBasicTypeSignedWChar;
5502 case clang::BuiltinType::WChar_U:
5503 return eBasicTypeUnsignedWChar;
5504 case clang::BuiltinType::Short:
5505 return eBasicTypeShort;
5506 case clang::BuiltinType::UShort:
5507 return eBasicTypeUnsignedShort;
5508 case clang::BuiltinType::Int:
5509 return eBasicTypeInt;
5510 case clang::BuiltinType::UInt:
5511 return eBasicTypeUnsignedInt;
5512 case clang::BuiltinType::Long:
5513 return eBasicTypeLong;
5514 case clang::BuiltinType::ULong:
5515 return eBasicTypeUnsignedLong;
5516 case clang::BuiltinType::LongLong:
5517 return eBasicTypeLongLong;
5518 case clang::BuiltinType::ULongLong:
5519 return eBasicTypeUnsignedLongLong;
5520 case clang::BuiltinType::Int128:
5521 return eBasicTypeInt128;
5522 case clang::BuiltinType::UInt128:
5523 return eBasicTypeUnsignedInt128;
5524
5525 case clang::BuiltinType::Half:
5526 return eBasicTypeHalf;
5527 case clang::BuiltinType::Float:
5528 return eBasicTypeFloat;
5529 case clang::BuiltinType::Double:
5530 return eBasicTypeDouble;
5531 case clang::BuiltinType::LongDouble:
5532 return eBasicTypeLongDouble;
5533
5534 case clang::BuiltinType::NullPtr:
5535 return eBasicTypeNullPtr;
5536 case clang::BuiltinType::ObjCId:
5537 return eBasicTypeObjCID;
5538 case clang::BuiltinType::ObjCClass:
5539 return eBasicTypeObjCClass;
5540 case clang::BuiltinType::ObjCSel:
5541 return eBasicTypeObjCSel;
5542 default:
5543 return eBasicTypeOther;
5544 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005545 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005546 }
5547 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005548}
5549
Kate Stoneb9c1b512016-09-06 20:57:50 +00005550void ClangASTContext::ForEachEnumerator(
5551 lldb::opaque_compiler_type_t type,
5552 std::function<bool(const CompilerType &integer_type,
5553 const ConstString &name,
5554 const llvm::APSInt &value)> const &callback) {
5555 const clang::EnumType *enum_type =
5556 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5557 if (enum_type) {
5558 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5559 if (enum_decl) {
5560 CompilerType integer_type(this,
5561 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005562
Kate Stoneb9c1b512016-09-06 20:57:50 +00005563 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5564 for (enum_pos = enum_decl->enumerator_begin(),
5565 enum_end_pos = enum_decl->enumerator_end();
5566 enum_pos != enum_end_pos; ++enum_pos) {
5567 ConstString name(enum_pos->getNameAsString().c_str());
5568 if (!callback(integer_type, name, enum_pos->getInitVal()))
5569 break;
5570 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005571 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005572 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005573}
5574
Greg Claytond8d4a572015-08-11 21:38:15 +00005575#pragma mark Aggregate Types
5576
Kate Stoneb9c1b512016-09-06 20:57:50 +00005577uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5578 if (!type)
5579 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005580
Kate Stoneb9c1b512016-09-06 20:57:50 +00005581 uint32_t count = 0;
5582 clang::QualType qual_type(GetCanonicalQualType(type));
5583 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5584 switch (type_class) {
5585 case clang::Type::Record:
5586 if (GetCompleteType(type)) {
5587 const clang::RecordType *record_type =
5588 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5589 if (record_type) {
5590 clang::RecordDecl *record_decl = record_type->getDecl();
5591 if (record_decl) {
5592 uint32_t field_idx = 0;
5593 clang::RecordDecl::field_iterator field, field_end;
5594 for (field = record_decl->field_begin(),
5595 field_end = record_decl->field_end();
5596 field != field_end; ++field)
5597 ++field_idx;
5598 count = field_idx;
5599 }
5600 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005601 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005602 break;
5603
5604 case clang::Type::Typedef:
5605 count =
5606 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5607 ->getDecl()
5608 ->getUnderlyingType())
5609 .GetNumFields();
5610 break;
5611
5612 case clang::Type::Auto:
5613 count =
5614 CompilerType(getASTContext(),
5615 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5616 .GetNumFields();
5617 break;
5618
5619 case clang::Type::Elaborated:
5620 count = CompilerType(
5621 getASTContext(),
5622 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5623 .GetNumFields();
5624 break;
5625
5626 case clang::Type::Paren:
5627 count = CompilerType(getASTContext(),
5628 llvm::cast<clang::ParenType>(qual_type)->desugar())
5629 .GetNumFields();
5630 break;
5631
5632 case clang::Type::ObjCObjectPointer:
5633 if (GetCompleteType(type)) {
5634 const clang::ObjCObjectPointerType *objc_class_type =
5635 qual_type->getAsObjCInterfacePointerType();
5636 if (objc_class_type) {
5637 clang::ObjCInterfaceDecl *class_interface_decl =
5638 objc_class_type->getInterfaceDecl();
5639
5640 if (class_interface_decl)
5641 count = class_interface_decl->ivar_size();
5642 }
5643 }
5644 break;
5645
5646 case clang::Type::ObjCObject:
5647 case clang::Type::ObjCInterface:
5648 if (GetCompleteType(type)) {
5649 const clang::ObjCObjectType *objc_class_type =
5650 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5651 if (objc_class_type) {
5652 clang::ObjCInterfaceDecl *class_interface_decl =
5653 objc_class_type->getInterface();
5654
5655 if (class_interface_decl)
5656 count = class_interface_decl->ivar_size();
5657 }
5658 }
5659 break;
5660
5661 default:
5662 break;
5663 }
5664 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005665}
5666
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005667static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005668GetObjCFieldAtIndex(clang::ASTContext *ast,
5669 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5670 std::string &name, uint64_t *bit_offset_ptr,
5671 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5672 if (class_interface_decl) {
5673 if (idx < (class_interface_decl->ivar_size())) {
5674 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5675 ivar_end = class_interface_decl->ivar_end();
5676 uint32_t ivar_idx = 0;
5677
5678 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5679 ++ivar_pos, ++ivar_idx) {
5680 if (ivar_idx == idx) {
5681 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5682
5683 clang::QualType ivar_qual_type(ivar_decl->getType());
5684
5685 name.assign(ivar_decl->getNameAsString());
5686
5687 if (bit_offset_ptr) {
5688 const clang::ASTRecordLayout &interface_layout =
5689 ast->getASTObjCInterfaceLayout(class_interface_decl);
5690 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5691 }
5692
5693 const bool is_bitfield = ivar_pos->isBitField();
5694
5695 if (bitfield_bit_size_ptr) {
5696 *bitfield_bit_size_ptr = 0;
5697
5698 if (is_bitfield && ast) {
5699 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5700 llvm::APSInt bitfield_apsint;
5701 if (bitfield_bit_size_expr &&
5702 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5703 *ast)) {
5704 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5705 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005706 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005707 }
5708 if (is_bitfield_ptr)
5709 *is_bitfield_ptr = is_bitfield;
5710
5711 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005712 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005713 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005714 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005715 }
5716 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005717}
5718
Kate Stoneb9c1b512016-09-06 20:57:50 +00005719CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5720 size_t idx, std::string &name,
5721 uint64_t *bit_offset_ptr,
5722 uint32_t *bitfield_bit_size_ptr,
5723 bool *is_bitfield_ptr) {
5724 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005725 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005726
5727 clang::QualType qual_type(GetCanonicalQualType(type));
5728 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5729 switch (type_class) {
5730 case clang::Type::Record:
5731 if (GetCompleteType(type)) {
5732 const clang::RecordType *record_type =
5733 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5734 const clang::RecordDecl *record_decl = record_type->getDecl();
5735 uint32_t field_idx = 0;
5736 clang::RecordDecl::field_iterator field, field_end;
5737 for (field = record_decl->field_begin(),
5738 field_end = record_decl->field_end();
5739 field != field_end; ++field, ++field_idx) {
5740 if (idx == field_idx) {
5741 // Print the member type if requested
5742 // Print the member name and equal sign
5743 name.assign(field->getNameAsString());
5744
5745 // Figure out the type byte size (field_type_info.first) and
5746 // alignment (field_type_info.second) from the AST context.
5747 if (bit_offset_ptr) {
5748 const clang::ASTRecordLayout &record_layout =
5749 getASTContext()->getASTRecordLayout(record_decl);
5750 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5751 }
5752
5753 const bool is_bitfield = field->isBitField();
5754
5755 if (bitfield_bit_size_ptr) {
5756 *bitfield_bit_size_ptr = 0;
5757
5758 if (is_bitfield) {
5759 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5760 llvm::APSInt bitfield_apsint;
5761 if (bitfield_bit_size_expr &&
5762 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5763 *getASTContext())) {
5764 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5765 }
5766 }
5767 }
5768 if (is_bitfield_ptr)
5769 *is_bitfield_ptr = is_bitfield;
5770
5771 return CompilerType(getASTContext(), field->getType());
5772 }
5773 }
5774 }
5775 break;
5776
5777 case clang::Type::ObjCObjectPointer:
5778 if (GetCompleteType(type)) {
5779 const clang::ObjCObjectPointerType *objc_class_type =
5780 qual_type->getAsObjCInterfacePointerType();
5781 if (objc_class_type) {
5782 clang::ObjCInterfaceDecl *class_interface_decl =
5783 objc_class_type->getInterfaceDecl();
5784 return CompilerType(
5785 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5786 idx, name, bit_offset_ptr,
5787 bitfield_bit_size_ptr, is_bitfield_ptr));
5788 }
5789 }
5790 break;
5791
5792 case clang::Type::ObjCObject:
5793 case clang::Type::ObjCInterface:
5794 if (GetCompleteType(type)) {
5795 const clang::ObjCObjectType *objc_class_type =
5796 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5797 assert(objc_class_type);
5798 if (objc_class_type) {
5799 clang::ObjCInterfaceDecl *class_interface_decl =
5800 objc_class_type->getInterface();
5801 return CompilerType(
5802 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5803 idx, name, bit_offset_ptr,
5804 bitfield_bit_size_ptr, is_bitfield_ptr));
5805 }
5806 }
5807 break;
5808
5809 case clang::Type::Typedef:
5810 return CompilerType(getASTContext(),
5811 llvm::cast<clang::TypedefType>(qual_type)
5812 ->getDecl()
5813 ->getUnderlyingType())
5814 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5815 is_bitfield_ptr);
5816
5817 case clang::Type::Auto:
5818 return CompilerType(
5819 getASTContext(),
5820 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5821 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5822 is_bitfield_ptr);
5823
5824 case clang::Type::Elaborated:
5825 return CompilerType(
5826 getASTContext(),
5827 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5828 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5829 is_bitfield_ptr);
5830
5831 case clang::Type::Paren:
5832 return CompilerType(getASTContext(),
5833 llvm::cast<clang::ParenType>(qual_type)->desugar())
5834 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5835 is_bitfield_ptr);
5836
5837 default:
5838 break;
5839 }
5840 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005841}
5842
Greg Clayton99558cc42015-08-24 23:46:31 +00005843uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005844ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
5845 uint32_t count = 0;
5846 clang::QualType qual_type(GetCanonicalQualType(type));
5847 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5848 switch (type_class) {
5849 case clang::Type::Record:
5850 if (GetCompleteType(type)) {
5851 const clang::CXXRecordDecl *cxx_record_decl =
5852 qual_type->getAsCXXRecordDecl();
5853 if (cxx_record_decl)
5854 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00005855 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005856 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00005857
Kate Stoneb9c1b512016-09-06 20:57:50 +00005858 case clang::Type::ObjCObjectPointer:
5859 count = GetPointeeType(type).GetNumDirectBaseClasses();
5860 break;
5861
5862 case clang::Type::ObjCObject:
5863 if (GetCompleteType(type)) {
5864 const clang::ObjCObjectType *objc_class_type =
5865 qual_type->getAsObjCQualifiedInterfaceType();
5866 if (objc_class_type) {
5867 clang::ObjCInterfaceDecl *class_interface_decl =
5868 objc_class_type->getInterface();
5869
5870 if (class_interface_decl && class_interface_decl->getSuperClass())
5871 count = 1;
5872 }
5873 }
5874 break;
5875 case clang::Type::ObjCInterface:
5876 if (GetCompleteType(type)) {
5877 const clang::ObjCInterfaceType *objc_interface_type =
5878 qual_type->getAs<clang::ObjCInterfaceType>();
5879 if (objc_interface_type) {
5880 clang::ObjCInterfaceDecl *class_interface_decl =
5881 objc_interface_type->getInterface();
5882
5883 if (class_interface_decl && class_interface_decl->getSuperClass())
5884 count = 1;
5885 }
5886 }
5887 break;
5888
5889 case clang::Type::Typedef:
5890 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5891 ->getDecl()
5892 ->getUnderlyingType()
5893 .getAsOpaquePtr());
5894 break;
5895
5896 case clang::Type::Auto:
5897 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5898 ->getDeducedType()
5899 .getAsOpaquePtr());
5900 break;
5901
5902 case clang::Type::Elaborated:
5903 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5904 ->getNamedType()
5905 .getAsOpaquePtr());
5906 break;
5907
5908 case clang::Type::Paren:
5909 return GetNumDirectBaseClasses(
5910 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5911
5912 default:
5913 break;
5914 }
5915 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00005916}
5917
5918uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005919ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
5920 uint32_t count = 0;
5921 clang::QualType qual_type(GetCanonicalQualType(type));
5922 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5923 switch (type_class) {
5924 case clang::Type::Record:
5925 if (GetCompleteType(type)) {
5926 const clang::CXXRecordDecl *cxx_record_decl =
5927 qual_type->getAsCXXRecordDecl();
5928 if (cxx_record_decl)
5929 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00005930 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005931 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00005932
Kate Stoneb9c1b512016-09-06 20:57:50 +00005933 case clang::Type::Typedef:
5934 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5935 ->getDecl()
5936 ->getUnderlyingType()
5937 .getAsOpaquePtr());
5938 break;
5939
5940 case clang::Type::Auto:
5941 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5942 ->getDeducedType()
5943 .getAsOpaquePtr());
5944 break;
5945
5946 case clang::Type::Elaborated:
5947 count =
5948 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5949 ->getNamedType()
5950 .getAsOpaquePtr());
5951 break;
5952
5953 case clang::Type::Paren:
5954 count = GetNumVirtualBaseClasses(
5955 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5956 break;
5957
5958 default:
5959 break;
5960 }
5961 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00005962}
5963
Kate Stoneb9c1b512016-09-06 20:57:50 +00005964CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
5965 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
5966 clang::QualType qual_type(GetCanonicalQualType(type));
5967 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5968 switch (type_class) {
5969 case clang::Type::Record:
5970 if (GetCompleteType(type)) {
5971 const clang::CXXRecordDecl *cxx_record_decl =
5972 qual_type->getAsCXXRecordDecl();
5973 if (cxx_record_decl) {
5974 uint32_t curr_idx = 0;
5975 clang::CXXRecordDecl::base_class_const_iterator base_class,
5976 base_class_end;
5977 for (base_class = cxx_record_decl->bases_begin(),
5978 base_class_end = cxx_record_decl->bases_end();
5979 base_class != base_class_end; ++base_class, ++curr_idx) {
5980 if (curr_idx == idx) {
5981 if (bit_offset_ptr) {
5982 const clang::ASTRecordLayout &record_layout =
5983 getASTContext()->getASTRecordLayout(cxx_record_decl);
5984 const clang::CXXRecordDecl *base_class_decl =
5985 llvm::cast<clang::CXXRecordDecl>(
5986 base_class->getType()
5987 ->getAs<clang::RecordType>()
5988 ->getDecl());
5989 if (base_class->isVirtual())
5990 *bit_offset_ptr =
5991 record_layout.getVBaseClassOffset(base_class_decl)
5992 .getQuantity() *
5993 8;
5994 else
5995 *bit_offset_ptr =
5996 record_layout.getBaseClassOffset(base_class_decl)
5997 .getQuantity() *
5998 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00005999 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006000 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6001 }
6002 }
6003 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006004 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006005 break;
6006
6007 case clang::Type::ObjCObjectPointer:
6008 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6009
6010 case clang::Type::ObjCObject:
6011 if (idx == 0 && GetCompleteType(type)) {
6012 const clang::ObjCObjectType *objc_class_type =
6013 qual_type->getAsObjCQualifiedInterfaceType();
6014 if (objc_class_type) {
6015 clang::ObjCInterfaceDecl *class_interface_decl =
6016 objc_class_type->getInterface();
6017
6018 if (class_interface_decl) {
6019 clang::ObjCInterfaceDecl *superclass_interface_decl =
6020 class_interface_decl->getSuperClass();
6021 if (superclass_interface_decl) {
6022 if (bit_offset_ptr)
6023 *bit_offset_ptr = 0;
6024 return CompilerType(getASTContext(),
6025 getASTContext()->getObjCInterfaceType(
6026 superclass_interface_decl));
6027 }
6028 }
6029 }
6030 }
6031 break;
6032 case clang::Type::ObjCInterface:
6033 if (idx == 0 && GetCompleteType(type)) {
6034 const clang::ObjCObjectType *objc_interface_type =
6035 qual_type->getAs<clang::ObjCInterfaceType>();
6036 if (objc_interface_type) {
6037 clang::ObjCInterfaceDecl *class_interface_decl =
6038 objc_interface_type->getInterface();
6039
6040 if (class_interface_decl) {
6041 clang::ObjCInterfaceDecl *superclass_interface_decl =
6042 class_interface_decl->getSuperClass();
6043 if (superclass_interface_decl) {
6044 if (bit_offset_ptr)
6045 *bit_offset_ptr = 0;
6046 return CompilerType(getASTContext(),
6047 getASTContext()->getObjCInterfaceType(
6048 superclass_interface_decl));
6049 }
6050 }
6051 }
6052 }
6053 break;
6054
6055 case clang::Type::Typedef:
6056 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6057 ->getDecl()
6058 ->getUnderlyingType()
6059 .getAsOpaquePtr(),
6060 idx, bit_offset_ptr);
6061
6062 case clang::Type::Auto:
6063 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6064 ->getDeducedType()
6065 .getAsOpaquePtr(),
6066 idx, bit_offset_ptr);
6067
6068 case clang::Type::Elaborated:
6069 return GetDirectBaseClassAtIndex(
6070 llvm::cast<clang::ElaboratedType>(qual_type)
6071 ->getNamedType()
6072 .getAsOpaquePtr(),
6073 idx, bit_offset_ptr);
6074
6075 case clang::Type::Paren:
6076 return GetDirectBaseClassAtIndex(
6077 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6078 idx, bit_offset_ptr);
6079
6080 default:
6081 break;
6082 }
6083 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006084}
6085
Kate Stoneb9c1b512016-09-06 20:57:50 +00006086CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6087 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6088 clang::QualType qual_type(GetCanonicalQualType(type));
6089 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6090 switch (type_class) {
6091 case clang::Type::Record:
6092 if (GetCompleteType(type)) {
6093 const clang::CXXRecordDecl *cxx_record_decl =
6094 qual_type->getAsCXXRecordDecl();
6095 if (cxx_record_decl) {
6096 uint32_t curr_idx = 0;
6097 clang::CXXRecordDecl::base_class_const_iterator base_class,
6098 base_class_end;
6099 for (base_class = cxx_record_decl->vbases_begin(),
6100 base_class_end = cxx_record_decl->vbases_end();
6101 base_class != base_class_end; ++base_class, ++curr_idx) {
6102 if (curr_idx == idx) {
6103 if (bit_offset_ptr) {
6104 const clang::ASTRecordLayout &record_layout =
6105 getASTContext()->getASTRecordLayout(cxx_record_decl);
6106 const clang::CXXRecordDecl *base_class_decl =
6107 llvm::cast<clang::CXXRecordDecl>(
6108 base_class->getType()
6109 ->getAs<clang::RecordType>()
6110 ->getDecl());
6111 *bit_offset_ptr =
6112 record_layout.getVBaseClassOffset(base_class_decl)
6113 .getQuantity() *
6114 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006115 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006116 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6117 }
6118 }
6119 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006120 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006121 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006122
Kate Stoneb9c1b512016-09-06 20:57:50 +00006123 case clang::Type::Typedef:
6124 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6125 ->getDecl()
6126 ->getUnderlyingType()
6127 .getAsOpaquePtr(),
6128 idx, bit_offset_ptr);
6129
6130 case clang::Type::Auto:
6131 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6132 ->getDeducedType()
6133 .getAsOpaquePtr(),
6134 idx, bit_offset_ptr);
6135
6136 case clang::Type::Elaborated:
6137 return GetVirtualBaseClassAtIndex(
6138 llvm::cast<clang::ElaboratedType>(qual_type)
6139 ->getNamedType()
6140 .getAsOpaquePtr(),
6141 idx, bit_offset_ptr);
6142
6143 case clang::Type::Paren:
6144 return GetVirtualBaseClassAtIndex(
6145 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6146 idx, bit_offset_ptr);
6147
6148 default:
6149 break;
6150 }
6151 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006152}
6153
Greg Claytond8d4a572015-08-11 21:38:15 +00006154// If a pointer to a pointee type (the clang_type arg) says that it has no
6155// children, then we either need to trust it, or override it and return a
6156// different result. For example, an "int *" has one child that is an integer,
6157// but a function pointer doesn't have any children. Likewise if a Record type
6158// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006159uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6160 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006161 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006162
6163 clang::QualType qual_type(type.getCanonicalType());
6164 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6165 switch (type_class) {
6166 case clang::Type::Builtin:
6167 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6168 case clang::BuiltinType::UnknownAny:
6169 case clang::BuiltinType::Void:
6170 case clang::BuiltinType::NullPtr:
6171 case clang::BuiltinType::OCLEvent:
6172 case clang::BuiltinType::OCLImage1dRO:
6173 case clang::BuiltinType::OCLImage1dWO:
6174 case clang::BuiltinType::OCLImage1dRW:
6175 case clang::BuiltinType::OCLImage1dArrayRO:
6176 case clang::BuiltinType::OCLImage1dArrayWO:
6177 case clang::BuiltinType::OCLImage1dArrayRW:
6178 case clang::BuiltinType::OCLImage1dBufferRO:
6179 case clang::BuiltinType::OCLImage1dBufferWO:
6180 case clang::BuiltinType::OCLImage1dBufferRW:
6181 case clang::BuiltinType::OCLImage2dRO:
6182 case clang::BuiltinType::OCLImage2dWO:
6183 case clang::BuiltinType::OCLImage2dRW:
6184 case clang::BuiltinType::OCLImage2dArrayRO:
6185 case clang::BuiltinType::OCLImage2dArrayWO:
6186 case clang::BuiltinType::OCLImage2dArrayRW:
6187 case clang::BuiltinType::OCLImage3dRO:
6188 case clang::BuiltinType::OCLImage3dWO:
6189 case clang::BuiltinType::OCLImage3dRW:
6190 case clang::BuiltinType::OCLSampler:
6191 return 0;
6192 case clang::BuiltinType::Bool:
6193 case clang::BuiltinType::Char_U:
6194 case clang::BuiltinType::UChar:
6195 case clang::BuiltinType::WChar_U:
6196 case clang::BuiltinType::Char16:
6197 case clang::BuiltinType::Char32:
6198 case clang::BuiltinType::UShort:
6199 case clang::BuiltinType::UInt:
6200 case clang::BuiltinType::ULong:
6201 case clang::BuiltinType::ULongLong:
6202 case clang::BuiltinType::UInt128:
6203 case clang::BuiltinType::Char_S:
6204 case clang::BuiltinType::SChar:
6205 case clang::BuiltinType::WChar_S:
6206 case clang::BuiltinType::Short:
6207 case clang::BuiltinType::Int:
6208 case clang::BuiltinType::Long:
6209 case clang::BuiltinType::LongLong:
6210 case clang::BuiltinType::Int128:
6211 case clang::BuiltinType::Float:
6212 case clang::BuiltinType::Double:
6213 case clang::BuiltinType::LongDouble:
6214 case clang::BuiltinType::Dependent:
6215 case clang::BuiltinType::Overload:
6216 case clang::BuiltinType::ObjCId:
6217 case clang::BuiltinType::ObjCClass:
6218 case clang::BuiltinType::ObjCSel:
6219 case clang::BuiltinType::BoundMember:
6220 case clang::BuiltinType::Half:
6221 case clang::BuiltinType::ARCUnbridgedCast:
6222 case clang::BuiltinType::PseudoObject:
6223 case clang::BuiltinType::BuiltinFn:
6224 case clang::BuiltinType::OMPArraySection:
6225 return 1;
6226 default:
6227 return 0;
6228 }
6229 break;
6230
6231 case clang::Type::Complex:
6232 return 1;
6233 case clang::Type::Pointer:
6234 return 1;
6235 case clang::Type::BlockPointer:
6236 return 0; // If block pointers don't have debug info, then no children for
6237 // them
6238 case clang::Type::LValueReference:
6239 return 1;
6240 case clang::Type::RValueReference:
6241 return 1;
6242 case clang::Type::MemberPointer:
6243 return 0;
6244 case clang::Type::ConstantArray:
6245 return 0;
6246 case clang::Type::IncompleteArray:
6247 return 0;
6248 case clang::Type::VariableArray:
6249 return 0;
6250 case clang::Type::DependentSizedArray:
6251 return 0;
6252 case clang::Type::DependentSizedExtVector:
6253 return 0;
6254 case clang::Type::Vector:
6255 return 0;
6256 case clang::Type::ExtVector:
6257 return 0;
6258 case clang::Type::FunctionProto:
6259 return 0; // When we function pointers, they have no children...
6260 case clang::Type::FunctionNoProto:
6261 return 0; // When we function pointers, they have no children...
6262 case clang::Type::UnresolvedUsing:
6263 return 0;
6264 case clang::Type::Paren:
6265 return GetNumPointeeChildren(
6266 llvm::cast<clang::ParenType>(qual_type)->desugar());
6267 case clang::Type::Typedef:
6268 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6269 ->getDecl()
6270 ->getUnderlyingType());
6271 case clang::Type::Auto:
6272 return GetNumPointeeChildren(
6273 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6274 case clang::Type::Elaborated:
6275 return GetNumPointeeChildren(
6276 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6277 case clang::Type::TypeOfExpr:
6278 return 0;
6279 case clang::Type::TypeOf:
6280 return 0;
6281 case clang::Type::Decltype:
6282 return 0;
6283 case clang::Type::Record:
6284 return 0;
6285 case clang::Type::Enum:
6286 return 1;
6287 case clang::Type::TemplateTypeParm:
6288 return 1;
6289 case clang::Type::SubstTemplateTypeParm:
6290 return 1;
6291 case clang::Type::TemplateSpecialization:
6292 return 1;
6293 case clang::Type::InjectedClassName:
6294 return 0;
6295 case clang::Type::DependentName:
6296 return 1;
6297 case clang::Type::DependentTemplateSpecialization:
6298 return 1;
6299 case clang::Type::ObjCObject:
6300 return 0;
6301 case clang::Type::ObjCInterface:
6302 return 0;
6303 case clang::Type::ObjCObjectPointer:
6304 return 1;
6305 default:
6306 break;
6307 }
6308 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006309}
6310
Kate Stoneb9c1b512016-09-06 20:57:50 +00006311CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6312 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6313 bool transparent_pointers, bool omit_empty_base_classes,
6314 bool ignore_array_bounds, std::string &child_name,
6315 uint32_t &child_byte_size, int32_t &child_byte_offset,
6316 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6317 bool &child_is_base_class, bool &child_is_deref_of_parent,
6318 ValueObject *valobj, uint64_t &language_flags) {
6319 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006320 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006321
Kate Stoneb9c1b512016-09-06 20:57:50 +00006322 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6323 const clang::Type::TypeClass parent_type_class =
6324 parent_qual_type->getTypeClass();
6325 child_bitfield_bit_size = 0;
6326 child_bitfield_bit_offset = 0;
6327 child_is_base_class = false;
6328 language_flags = 0;
6329
6330 const bool idx_is_valid = idx < GetNumChildren(type, omit_empty_base_classes);
6331 uint32_t bit_offset;
6332 switch (parent_type_class) {
6333 case clang::Type::Builtin:
6334 if (idx_is_valid) {
6335 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6336 case clang::BuiltinType::ObjCId:
6337 case clang::BuiltinType::ObjCClass:
6338 child_name = "isa";
6339 child_byte_size =
6340 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6341 CHAR_BIT;
6342 return CompilerType(getASTContext(),
6343 getASTContext()->ObjCBuiltinClassTy);
6344
6345 default:
6346 break;
6347 }
6348 }
6349 break;
6350
6351 case clang::Type::Record:
6352 if (idx_is_valid && GetCompleteType(type)) {
6353 const clang::RecordType *record_type =
6354 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6355 const clang::RecordDecl *record_decl = record_type->getDecl();
6356 assert(record_decl);
6357 const clang::ASTRecordLayout &record_layout =
6358 getASTContext()->getASTRecordLayout(record_decl);
6359 uint32_t child_idx = 0;
6360
6361 const clang::CXXRecordDecl *cxx_record_decl =
6362 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6363 if (cxx_record_decl) {
6364 // We might have base classes to print out first
6365 clang::CXXRecordDecl::base_class_const_iterator base_class,
6366 base_class_end;
6367 for (base_class = cxx_record_decl->bases_begin(),
6368 base_class_end = cxx_record_decl->bases_end();
6369 base_class != base_class_end; ++base_class) {
6370 const clang::CXXRecordDecl *base_class_decl = nullptr;
6371
6372 // Skip empty base classes
6373 if (omit_empty_base_classes) {
6374 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6375 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6376 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
6377 continue;
6378 }
6379
6380 if (idx == child_idx) {
6381 if (base_class_decl == nullptr)
6382 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6383 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6384
6385 if (base_class->isVirtual()) {
6386 bool handled = false;
6387 if (valobj) {
6388 Error err;
6389 AddressType addr_type = eAddressTypeInvalid;
6390 lldb::addr_t vtable_ptr_addr =
6391 valobj->GetCPPVTableAddress(addr_type);
6392
6393 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS &&
6394 addr_type == eAddressTypeLoad) {
6395
6396 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
6397 Process *process = exe_ctx.GetProcessPtr();
6398 if (process) {
6399 clang::VTableContextBase *vtable_ctx =
6400 getASTContext()->getVTableContext();
6401 if (vtable_ctx) {
6402 if (vtable_ctx->isMicrosoft()) {
6403 clang::MicrosoftVTableContext *msoft_vtable_ctx =
6404 static_cast<clang::MicrosoftVTableContext *>(
6405 vtable_ctx);
6406
6407 if (vtable_ptr_addr) {
6408 const lldb::addr_t vbtable_ptr_addr =
6409 vtable_ptr_addr +
6410 record_layout.getVBPtrOffset().getQuantity();
6411
6412 const lldb::addr_t vbtable_ptr =
6413 process->ReadPointerFromMemory(vbtable_ptr_addr,
6414 err);
6415 if (vbtable_ptr != LLDB_INVALID_ADDRESS) {
6416 // Get the index into the virtual base table. The
6417 // index is the index in uint32_t from vbtable_ptr
6418 const unsigned vbtable_index =
6419 msoft_vtable_ctx->getVBTableIndex(
6420 cxx_record_decl, base_class_decl);
6421 const lldb::addr_t base_offset_addr =
6422 vbtable_ptr + vbtable_index * 4;
6423 const uint32_t base_offset =
6424 process->ReadUnsignedIntegerFromMemory(
6425 base_offset_addr, 4, UINT32_MAX, err);
6426 if (base_offset != UINT32_MAX) {
6427 handled = true;
6428 bit_offset = base_offset * 8;
6429 }
6430 }
6431 }
6432 } else {
6433 clang::ItaniumVTableContext *itanium_vtable_ctx =
6434 static_cast<clang::ItaniumVTableContext *>(
6435 vtable_ctx);
6436 if (vtable_ptr_addr) {
6437 const lldb::addr_t vtable_ptr =
6438 process->ReadPointerFromMemory(vtable_ptr_addr,
6439 err);
6440 if (vtable_ptr != LLDB_INVALID_ADDRESS) {
6441 clang::CharUnits base_offset_offset =
6442 itanium_vtable_ctx->getVirtualBaseOffsetOffset(
6443 cxx_record_decl, base_class_decl);
6444 const lldb::addr_t base_offset_addr =
6445 vtable_ptr + base_offset_offset.getQuantity();
6446 const uint32_t base_offset_size =
6447 process->GetAddressByteSize();
6448 const uint64_t base_offset =
6449 process->ReadUnsignedIntegerFromMemory(
6450 base_offset_addr, base_offset_size,
6451 UINT32_MAX, err);
6452 if (base_offset < UINT32_MAX) {
6453 handled = true;
6454 bit_offset = base_offset * 8;
6455 }
6456 }
6457 }
6458 }
6459 }
6460 }
6461 }
6462 }
6463 if (!handled)
6464 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6465 .getQuantity() *
6466 8;
6467 } else
6468 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6469 .getQuantity() *
6470 8;
6471
6472 // Base classes should be a multiple of 8 bits in size
6473 child_byte_offset = bit_offset / 8;
6474 CompilerType base_class_clang_type(getASTContext(),
6475 base_class->getType());
6476 child_name = base_class_clang_type.GetTypeName().AsCString("");
6477 uint64_t base_class_clang_type_bit_size =
6478 base_class_clang_type.GetBitSize(
6479 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6480
6481 // Base classes bit sizes should be a multiple of 8 bits in size
6482 assert(base_class_clang_type_bit_size % 8 == 0);
6483 child_byte_size = base_class_clang_type_bit_size / 8;
6484 child_is_base_class = true;
6485 return base_class_clang_type;
6486 }
6487 // We don't increment the child index in the for loop since we might
6488 // be skipping empty base classes
6489 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006490 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006491 }
6492 // Make sure index is in range...
6493 uint32_t field_idx = 0;
6494 clang::RecordDecl::field_iterator field, field_end;
6495 for (field = record_decl->field_begin(),
6496 field_end = record_decl->field_end();
6497 field != field_end; ++field, ++field_idx, ++child_idx) {
6498 if (idx == child_idx) {
6499 // Print the member type if requested
6500 // Print the member name and equal sign
6501 child_name.assign(field->getNameAsString().c_str());
6502
6503 // Figure out the type byte size (field_type_info.first) and
6504 // alignment (field_type_info.second) from the AST context.
6505 CompilerType field_clang_type(getASTContext(), field->getType());
6506 assert(field_idx < record_layout.getFieldCount());
6507 child_byte_size = field_clang_type.GetByteSize(
6508 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6509 const uint32_t child_bit_size = child_byte_size * 8;
6510
6511 // Figure out the field offset within the current struct/union/class
6512 // type
6513 bit_offset = record_layout.getFieldOffset(field_idx);
6514 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6515 child_bitfield_bit_size)) {
6516 child_bitfield_bit_offset = bit_offset % child_bit_size;
6517 const uint32_t child_bit_offset =
6518 bit_offset - child_bitfield_bit_offset;
6519 child_byte_offset = child_bit_offset / 8;
6520 } else {
6521 child_byte_offset = bit_offset / 8;
6522 }
6523
6524 return field_clang_type;
6525 }
6526 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006527 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006528 break;
6529
6530 case clang::Type::ObjCObject:
6531 case clang::Type::ObjCInterface:
6532 if (idx_is_valid && GetCompleteType(type)) {
6533 const clang::ObjCObjectType *objc_class_type =
6534 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6535 assert(objc_class_type);
6536 if (objc_class_type) {
6537 uint32_t child_idx = 0;
6538 clang::ObjCInterfaceDecl *class_interface_decl =
6539 objc_class_type->getInterface();
6540
6541 if (class_interface_decl) {
6542
6543 const clang::ASTRecordLayout &interface_layout =
6544 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6545 clang::ObjCInterfaceDecl *superclass_interface_decl =
6546 class_interface_decl->getSuperClass();
6547 if (superclass_interface_decl) {
6548 if (omit_empty_base_classes) {
6549 CompilerType base_class_clang_type(
6550 getASTContext(), getASTContext()->getObjCInterfaceType(
6551 superclass_interface_decl));
6552 if (base_class_clang_type.GetNumChildren(
6553 omit_empty_base_classes) > 0) {
6554 if (idx == 0) {
6555 clang::QualType ivar_qual_type(
6556 getASTContext()->getObjCInterfaceType(
6557 superclass_interface_decl));
6558
6559 child_name.assign(
6560 superclass_interface_decl->getNameAsString().c_str());
6561
6562 clang::TypeInfo ivar_type_info =
6563 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6564
6565 child_byte_size = ivar_type_info.Width / 8;
6566 child_byte_offset = 0;
6567 child_is_base_class = true;
6568
6569 return CompilerType(getASTContext(), ivar_qual_type);
6570 }
6571
6572 ++child_idx;
6573 }
6574 } else
6575 ++child_idx;
6576 }
6577
6578 const uint32_t superclass_idx = child_idx;
6579
6580 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6581 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6582 ivar_end = class_interface_decl->ivar_end();
6583
6584 for (ivar_pos = class_interface_decl->ivar_begin();
6585 ivar_pos != ivar_end; ++ivar_pos) {
6586 if (child_idx == idx) {
6587 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6588
6589 clang::QualType ivar_qual_type(ivar_decl->getType());
6590
6591 child_name.assign(ivar_decl->getNameAsString().c_str());
6592
6593 clang::TypeInfo ivar_type_info =
6594 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6595
6596 child_byte_size = ivar_type_info.Width / 8;
6597
6598 // Figure out the field offset within the current
6599 // struct/union/class type
6600 // For ObjC objects, we can't trust the bit offset we get from
6601 // the Clang AST, since
6602 // that doesn't account for the space taken up by unbacked
6603 // properties, or from
6604 // the changing size of base classes that are newer than this
6605 // class.
6606 // So if we have a process around that we can ask about this
6607 // object, do so.
6608 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6609 Process *process = nullptr;
6610 if (exe_ctx)
6611 process = exe_ctx->GetProcessPtr();
6612 if (process) {
6613 ObjCLanguageRuntime *objc_runtime =
6614 process->GetObjCLanguageRuntime();
6615 if (objc_runtime != nullptr) {
6616 CompilerType parent_ast_type(getASTContext(),
6617 parent_qual_type);
6618 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6619 parent_ast_type, ivar_decl->getNameAsString().c_str());
6620 }
6621 }
6622
6623 // Setting this to UINT32_MAX to make sure we don't compute it
6624 // twice...
6625 bit_offset = UINT32_MAX;
6626
6627 if (child_byte_offset ==
6628 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6629 bit_offset = interface_layout.getFieldOffset(child_idx -
6630 superclass_idx);
6631 child_byte_offset = bit_offset / 8;
6632 }
6633
6634 // Note, the ObjC Ivar Byte offset is just that, it doesn't
6635 // account for the bit offset
6636 // of a bitfield within its containing object. So regardless of
6637 // where we get the byte
6638 // offset from, we still need to get the bit offset for
6639 // bitfields from the layout.
6640
6641 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6642 child_bitfield_bit_size)) {
6643 if (bit_offset == UINT32_MAX)
6644 bit_offset = interface_layout.getFieldOffset(
6645 child_idx - superclass_idx);
6646
6647 child_bitfield_bit_offset = bit_offset % 8;
6648 }
6649 return CompilerType(getASTContext(), ivar_qual_type);
6650 }
6651 ++child_idx;
6652 }
6653 }
6654 }
6655 }
6656 }
6657 break;
6658
6659 case clang::Type::ObjCObjectPointer:
6660 if (idx_is_valid) {
6661 CompilerType pointee_clang_type(GetPointeeType(type));
6662
6663 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6664 child_is_deref_of_parent = false;
6665 bool tmp_child_is_deref_of_parent = false;
6666 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6667 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6668 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6669 child_bitfield_bit_size, child_bitfield_bit_offset,
6670 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6671 language_flags);
6672 } else {
6673 child_is_deref_of_parent = true;
6674 const char *parent_name =
6675 valobj ? valobj->GetName().GetCString() : NULL;
6676 if (parent_name) {
6677 child_name.assign(1, '*');
6678 child_name += parent_name;
6679 }
6680
6681 // We have a pointer to an simple type
6682 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
6683 child_byte_size = pointee_clang_type.GetByteSize(
6684 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6685 child_byte_offset = 0;
6686 return pointee_clang_type;
6687 }
6688 }
6689 }
6690 break;
6691
6692 case clang::Type::Vector:
6693 case clang::Type::ExtVector:
6694 if (idx_is_valid) {
6695 const clang::VectorType *array =
6696 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6697 if (array) {
6698 CompilerType element_type(getASTContext(), array->getElementType());
6699 if (element_type.GetCompleteType()) {
6700 char element_name[64];
6701 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6702 static_cast<uint64_t>(idx));
6703 child_name.assign(element_name);
6704 child_byte_size = element_type.GetByteSize(
6705 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6706 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6707 return element_type;
6708 }
6709 }
6710 }
6711 break;
6712
6713 case clang::Type::ConstantArray:
6714 case clang::Type::IncompleteArray:
6715 if (ignore_array_bounds || idx_is_valid) {
6716 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6717 if (array) {
6718 CompilerType element_type(getASTContext(), array->getElementType());
6719 if (element_type.GetCompleteType()) {
6720 char element_name[64];
6721 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6722 static_cast<uint64_t>(idx));
6723 child_name.assign(element_name);
6724 child_byte_size = element_type.GetByteSize(
6725 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6726 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6727 return element_type;
6728 }
6729 }
6730 }
6731 break;
6732
6733 case clang::Type::Pointer:
6734 if (idx_is_valid) {
6735 CompilerType pointee_clang_type(GetPointeeType(type));
6736
6737 // Don't dereference "void *" pointers
6738 if (pointee_clang_type.IsVoidType())
6739 return CompilerType();
6740
6741 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6742 child_is_deref_of_parent = false;
6743 bool tmp_child_is_deref_of_parent = false;
6744 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6745 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6746 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6747 child_bitfield_bit_size, child_bitfield_bit_offset,
6748 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6749 language_flags);
6750 } else {
6751 child_is_deref_of_parent = true;
6752
6753 const char *parent_name =
6754 valobj ? valobj->GetName().GetCString() : NULL;
6755 if (parent_name) {
6756 child_name.assign(1, '*');
6757 child_name += parent_name;
6758 }
6759
6760 // We have a pointer to an simple type
6761 if (idx == 0) {
6762 child_byte_size = pointee_clang_type.GetByteSize(
6763 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6764 child_byte_offset = 0;
6765 return pointee_clang_type;
6766 }
6767 }
6768 }
6769 break;
6770
6771 case clang::Type::LValueReference:
6772 case clang::Type::RValueReference:
6773 if (idx_is_valid) {
6774 const clang::ReferenceType *reference_type =
6775 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6776 CompilerType pointee_clang_type(getASTContext(),
6777 reference_type->getPointeeType());
6778 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6779 child_is_deref_of_parent = false;
6780 bool tmp_child_is_deref_of_parent = false;
6781 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6782 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6783 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6784 child_bitfield_bit_size, child_bitfield_bit_offset,
6785 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6786 language_flags);
6787 } else {
6788 const char *parent_name =
6789 valobj ? valobj->GetName().GetCString() : NULL;
6790 if (parent_name) {
6791 child_name.assign(1, '&');
6792 child_name += parent_name;
6793 }
6794
6795 // We have a pointer to an simple type
6796 if (idx == 0) {
6797 child_byte_size = pointee_clang_type.GetByteSize(
6798 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6799 child_byte_offset = 0;
6800 return pointee_clang_type;
6801 }
6802 }
6803 }
6804 break;
6805
6806 case clang::Type::Typedef: {
6807 CompilerType typedefed_clang_type(
6808 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
6809 ->getDecl()
6810 ->getUnderlyingType());
6811 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
6812 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6813 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6814 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6815 child_is_deref_of_parent, valobj, language_flags);
6816 } break;
6817
6818 case clang::Type::Auto: {
6819 CompilerType elaborated_clang_type(
6820 getASTContext(),
6821 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
6822 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6823 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6824 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6825 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6826 child_is_deref_of_parent, valobj, language_flags);
6827 }
6828
6829 case clang::Type::Elaborated: {
6830 CompilerType elaborated_clang_type(
6831 getASTContext(),
6832 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
6833 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6834 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6835 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6836 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6837 child_is_deref_of_parent, valobj, language_flags);
6838 }
6839
6840 case clang::Type::Paren: {
6841 CompilerType paren_clang_type(
6842 getASTContext(),
6843 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
6844 return paren_clang_type.GetChildCompilerTypeAtIndex(
6845 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6846 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6847 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6848 child_is_deref_of_parent, valobj, language_flags);
6849 }
6850
6851 default:
6852 break;
6853 }
6854 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006855}
6856
Kate Stoneb9c1b512016-09-06 20:57:50 +00006857static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
6858 const clang::CXXBaseSpecifier *base_spec,
6859 bool omit_empty_base_classes) {
6860 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006861
Kate Stoneb9c1b512016-09-06 20:57:50 +00006862 const clang::CXXRecordDecl *cxx_record_decl =
6863 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6864
6865 // const char *super_name = record_decl->getNameAsCString();
6866 // const char *base_name =
6867 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6868 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6869 //
6870 if (cxx_record_decl) {
6871 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6872 for (base_class = cxx_record_decl->bases_begin(),
6873 base_class_end = cxx_record_decl->bases_end();
6874 base_class != base_class_end; ++base_class) {
6875 if (omit_empty_base_classes) {
6876 if (BaseSpecifierIsEmpty(base_class))
6877 continue;
6878 }
6879
6880 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
6881 // super_name, base_name,
6882 // child_idx,
6883 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6884 //
6885 //
6886 if (base_class == base_spec)
6887 return child_idx;
6888 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006889 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006890 }
6891
6892 return UINT32_MAX;
6893}
6894
6895static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
6896 clang::NamedDecl *canonical_decl,
6897 bool omit_empty_base_classes) {
6898 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
6899 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6900 omit_empty_base_classes);
6901
6902 clang::RecordDecl::field_iterator field, field_end;
6903 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6904 field != field_end; ++field, ++child_idx) {
6905 if (field->getCanonicalDecl() == canonical_decl)
6906 return child_idx;
6907 }
6908
6909 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00006910}
6911
6912// Look for a child member (doesn't include base classes, but it does include
6913// their members) in the type hierarchy. Returns an index path into "clang_type"
6914// on how to reach the appropriate member.
6915//
6916// class A
6917// {
6918// public:
6919// int m_a;
6920// int m_b;
6921// };
6922//
6923// class B
6924// {
6925// };
6926//
6927// class C :
6928// public B,
6929// public A
6930// {
6931// };
6932//
6933// If we have a clang type that describes "class C", and we wanted to looked
6934// "m_b" in it:
6935//
Kate Stoneb9c1b512016-09-06 20:57:50 +00006936// With omit_empty_base_classes == false we would get an integer array back
6937// with:
Greg Claytond8d4a572015-08-11 21:38:15 +00006938// { 1, 1 }
6939// The first index 1 is the child index for "class A" within class C
6940// The second index 1 is the child index for "m_b" within class A
6941//
6942// With omit_empty_base_classes == true we would get an integer array back with:
6943// { 0, 1 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006944// The first index 0 is the child index for "class A" within class C (since
6945// class B doesn't have any members it doesn't count)
Greg Claytond8d4a572015-08-11 21:38:15 +00006946// The second index 1 is the child index for "m_b" within class A
6947
Kate Stoneb9c1b512016-09-06 20:57:50 +00006948size_t ClangASTContext::GetIndexOfChildMemberWithName(
6949 lldb::opaque_compiler_type_t type, const char *name,
6950 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
6951 if (type && name && name[0]) {
6952 clang::QualType qual_type(GetCanonicalQualType(type));
6953 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6954 switch (type_class) {
6955 case clang::Type::Record:
6956 if (GetCompleteType(type)) {
6957 const clang::RecordType *record_type =
6958 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6959 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00006960
Kate Stoneb9c1b512016-09-06 20:57:50 +00006961 assert(record_decl);
6962 uint32_t child_idx = 0;
6963
6964 const clang::CXXRecordDecl *cxx_record_decl =
6965 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6966
6967 // Try and find a field that matches NAME
6968 clang::RecordDecl::field_iterator field, field_end;
6969 llvm::StringRef name_sref(name);
6970 for (field = record_decl->field_begin(),
6971 field_end = record_decl->field_end();
6972 field != field_end; ++field, ++child_idx) {
6973 llvm::StringRef field_name = field->getName();
6974 if (field_name.empty()) {
6975 CompilerType field_type(getASTContext(), field->getType());
6976 child_indexes.push_back(child_idx);
6977 if (field_type.GetIndexOfChildMemberWithName(
6978 name, omit_empty_base_classes, child_indexes))
6979 return child_indexes.size();
6980 child_indexes.pop_back();
6981
6982 } else if (field_name.equals(name_sref)) {
6983 // We have to add on the number of base classes to this index!
6984 child_indexes.push_back(
6985 child_idx + ClangASTContext::GetNumBaseClasses(
6986 cxx_record_decl, omit_empty_base_classes));
6987 return child_indexes.size();
6988 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006989 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006990
Kate Stoneb9c1b512016-09-06 20:57:50 +00006991 if (cxx_record_decl) {
6992 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
6993
6994 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
6995
6996 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
6997 // Didn't find things easily, lets let clang do its thang...
6998 clang::IdentifierInfo &ident_ref =
6999 getASTContext()->Idents.get(name_sref);
7000 clang::DeclarationName decl_name(&ident_ref);
7001
7002 clang::CXXBasePaths paths;
7003 if (cxx_record_decl->lookupInBases(
7004 [decl_name](const clang::CXXBaseSpecifier *specifier,
7005 clang::CXXBasePath &path) {
7006 return clang::CXXRecordDecl::FindOrdinaryMember(
7007 specifier, path, decl_name);
7008 },
7009 paths)) {
7010 clang::CXXBasePaths::const_paths_iterator path,
7011 path_end = paths.end();
7012 for (path = paths.begin(); path != path_end; ++path) {
7013 const size_t num_path_elements = path->size();
7014 for (size_t e = 0; e < num_path_elements; ++e) {
7015 clang::CXXBasePathElement elem = (*path)[e];
7016
7017 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7018 omit_empty_base_classes);
7019 if (child_idx == UINT32_MAX) {
7020 child_indexes.clear();
7021 return 0;
7022 } else {
7023 child_indexes.push_back(child_idx);
7024 parent_record_decl = llvm::cast<clang::RecordDecl>(
7025 elem.Base->getType()
7026 ->getAs<clang::RecordType>()
7027 ->getDecl());
7028 }
7029 }
7030 for (clang::NamedDecl *path_decl : path->Decls) {
7031 child_idx = GetIndexForRecordChild(
7032 parent_record_decl, path_decl, omit_empty_base_classes);
7033 if (child_idx == UINT32_MAX) {
7034 child_indexes.clear();
7035 return 0;
7036 } else {
7037 child_indexes.push_back(child_idx);
7038 }
7039 }
7040 }
7041 return child_indexes.size();
7042 }
7043 }
7044 }
7045 break;
7046
7047 case clang::Type::ObjCObject:
7048 case clang::Type::ObjCInterface:
7049 if (GetCompleteType(type)) {
7050 llvm::StringRef name_sref(name);
7051 const clang::ObjCObjectType *objc_class_type =
7052 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7053 assert(objc_class_type);
7054 if (objc_class_type) {
7055 uint32_t child_idx = 0;
7056 clang::ObjCInterfaceDecl *class_interface_decl =
7057 objc_class_type->getInterface();
7058
7059 if (class_interface_decl) {
7060 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7061 ivar_end = class_interface_decl->ivar_end();
7062 clang::ObjCInterfaceDecl *superclass_interface_decl =
7063 class_interface_decl->getSuperClass();
7064
7065 for (ivar_pos = class_interface_decl->ivar_begin();
7066 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7067 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7068
7069 if (ivar_decl->getName().equals(name_sref)) {
7070 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7071 (omit_empty_base_classes &&
7072 ObjCDeclHasIVars(superclass_interface_decl, true)))
7073 ++child_idx;
7074
7075 child_indexes.push_back(child_idx);
7076 return child_indexes.size();
7077 }
7078 }
7079
7080 if (superclass_interface_decl) {
7081 // The super class index is always zero for ObjC classes,
7082 // so we push it onto the child indexes in case we find
7083 // an ivar in our superclass...
7084 child_indexes.push_back(0);
7085
7086 CompilerType superclass_clang_type(
7087 getASTContext(), getASTContext()->getObjCInterfaceType(
7088 superclass_interface_decl));
7089 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7090 name, omit_empty_base_classes, child_indexes)) {
7091 // We did find an ivar in a superclass so just
7092 // return the results!
7093 return child_indexes.size();
7094 }
7095
7096 // We didn't find an ivar matching "name" in our
7097 // superclass, pop the superclass zero index that
7098 // we pushed on above.
7099 child_indexes.pop_back();
7100 }
7101 }
7102 }
7103 }
7104 break;
7105
7106 case clang::Type::ObjCObjectPointer: {
7107 CompilerType objc_object_clang_type(
7108 getASTContext(),
7109 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7110 ->getPointeeType());
7111 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7112 name, omit_empty_base_classes, child_indexes);
7113 } break;
7114
7115 case clang::Type::ConstantArray: {
7116 // const clang::ConstantArrayType *array =
7117 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7118 // const uint64_t element_count =
7119 // array->getSize().getLimitedValue();
7120 //
7121 // if (idx < element_count)
7122 // {
7123 // std::pair<uint64_t, unsigned> field_type_info =
7124 // ast->getTypeInfo(array->getElementType());
7125 //
7126 // char element_name[32];
7127 // ::snprintf (element_name, sizeof (element_name),
7128 // "%s[%u]", parent_name ? parent_name : "", idx);
7129 //
7130 // child_name.assign(element_name);
7131 // assert(field_type_info.first % 8 == 0);
7132 // child_byte_size = field_type_info.first / 8;
7133 // child_byte_offset = idx * child_byte_size;
7134 // return array->getElementType().getAsOpaquePtr();
7135 // }
7136 } break;
7137
7138 // case clang::Type::MemberPointerType:
7139 // {
7140 // MemberPointerType *mem_ptr_type =
7141 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7142 // clang::QualType pointee_type =
7143 // mem_ptr_type->getPointeeType();
7144 //
7145 // if (ClangASTContext::IsAggregateType
7146 // (pointee_type.getAsOpaquePtr()))
7147 // {
7148 // return GetIndexOfChildWithName (ast,
7149 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7150 // name);
7151 // }
7152 // }
7153 // break;
7154 //
7155 case clang::Type::LValueReference:
7156 case clang::Type::RValueReference: {
7157 const clang::ReferenceType *reference_type =
7158 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7159 clang::QualType pointee_type(reference_type->getPointeeType());
7160 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7161
7162 if (pointee_clang_type.IsAggregateType()) {
7163 return pointee_clang_type.GetIndexOfChildMemberWithName(
7164 name, omit_empty_base_classes, child_indexes);
7165 }
7166 } break;
7167
7168 case clang::Type::Pointer: {
7169 CompilerType pointee_clang_type(GetPointeeType(type));
7170
7171 if (pointee_clang_type.IsAggregateType()) {
7172 return pointee_clang_type.GetIndexOfChildMemberWithName(
7173 name, omit_empty_base_classes, child_indexes);
7174 }
7175 } break;
7176
7177 case clang::Type::Typedef:
7178 return CompilerType(getASTContext(),
7179 llvm::cast<clang::TypedefType>(qual_type)
7180 ->getDecl()
7181 ->getUnderlyingType())
7182 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7183 child_indexes);
7184
7185 case clang::Type::Auto:
7186 return CompilerType(
7187 getASTContext(),
7188 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7189 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7190 child_indexes);
7191
7192 case clang::Type::Elaborated:
7193 return CompilerType(
7194 getASTContext(),
7195 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7196 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7197 child_indexes);
7198
7199 case clang::Type::Paren:
7200 return CompilerType(getASTContext(),
7201 llvm::cast<clang::ParenType>(qual_type)->desugar())
7202 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7203 child_indexes);
7204
7205 default:
7206 break;
7207 }
7208 }
7209 return 0;
7210}
Greg Claytond8d4a572015-08-11 21:38:15 +00007211
7212// Get the index of the child of "clang_type" whose name matches. This function
7213// doesn't descend into the children, but only looks one level deep and name
7214// matches can include base class names.
7215
7216uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007217ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7218 const char *name,
7219 bool omit_empty_base_classes) {
7220 if (type && name && name[0]) {
7221 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007222
Kate Stoneb9c1b512016-09-06 20:57:50 +00007223 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7224
7225 switch (type_class) {
7226 case clang::Type::Record:
7227 if (GetCompleteType(type)) {
7228 const clang::RecordType *record_type =
7229 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7230 const clang::RecordDecl *record_decl = record_type->getDecl();
7231
7232 assert(record_decl);
7233 uint32_t child_idx = 0;
7234
7235 const clang::CXXRecordDecl *cxx_record_decl =
7236 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7237
7238 if (cxx_record_decl) {
7239 clang::CXXRecordDecl::base_class_const_iterator base_class,
7240 base_class_end;
7241 for (base_class = cxx_record_decl->bases_begin(),
7242 base_class_end = cxx_record_decl->bases_end();
7243 base_class != base_class_end; ++base_class) {
7244 // Skip empty base classes
7245 clang::CXXRecordDecl *base_class_decl =
7246 llvm::cast<clang::CXXRecordDecl>(
7247 base_class->getType()
7248 ->getAs<clang::RecordType>()
7249 ->getDecl());
7250 if (omit_empty_base_classes &&
7251 ClangASTContext::RecordHasFields(base_class_decl) == false)
7252 continue;
7253
7254 CompilerType base_class_clang_type(getASTContext(),
7255 base_class->getType());
7256 std::string base_class_type_name(
7257 base_class_clang_type.GetTypeName().AsCString(""));
7258 if (base_class_type_name.compare(name) == 0)
7259 return child_idx;
7260 ++child_idx;
7261 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007262 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007263
Kate Stoneb9c1b512016-09-06 20:57:50 +00007264 // Try and find a field that matches NAME
7265 clang::RecordDecl::field_iterator field, field_end;
7266 llvm::StringRef name_sref(name);
7267 for (field = record_decl->field_begin(),
7268 field_end = record_decl->field_end();
7269 field != field_end; ++field, ++child_idx) {
7270 if (field->getName().equals(name_sref))
7271 return child_idx;
7272 }
7273 }
7274 break;
7275
7276 case clang::Type::ObjCObject:
7277 case clang::Type::ObjCInterface:
7278 if (GetCompleteType(type)) {
7279 llvm::StringRef name_sref(name);
7280 const clang::ObjCObjectType *objc_class_type =
7281 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7282 assert(objc_class_type);
7283 if (objc_class_type) {
7284 uint32_t child_idx = 0;
7285 clang::ObjCInterfaceDecl *class_interface_decl =
7286 objc_class_type->getInterface();
7287
7288 if (class_interface_decl) {
7289 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7290 ivar_end = class_interface_decl->ivar_end();
7291 clang::ObjCInterfaceDecl *superclass_interface_decl =
7292 class_interface_decl->getSuperClass();
7293
7294 for (ivar_pos = class_interface_decl->ivar_begin();
7295 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7296 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7297
7298 if (ivar_decl->getName().equals(name_sref)) {
7299 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7300 (omit_empty_base_classes &&
7301 ObjCDeclHasIVars(superclass_interface_decl, true)))
7302 ++child_idx;
7303
7304 return child_idx;
7305 }
7306 }
7307
7308 if (superclass_interface_decl) {
7309 if (superclass_interface_decl->getName().equals(name_sref))
7310 return 0;
7311 }
7312 }
7313 }
7314 }
7315 break;
7316
7317 case clang::Type::ObjCObjectPointer: {
7318 CompilerType pointee_clang_type(
7319 getASTContext(),
7320 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7321 ->getPointeeType());
7322 return pointee_clang_type.GetIndexOfChildWithName(
7323 name, omit_empty_base_classes);
7324 } break;
7325
7326 case clang::Type::ConstantArray: {
7327 // const clang::ConstantArrayType *array =
7328 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7329 // const uint64_t element_count =
7330 // array->getSize().getLimitedValue();
7331 //
7332 // if (idx < element_count)
7333 // {
7334 // std::pair<uint64_t, unsigned> field_type_info =
7335 // ast->getTypeInfo(array->getElementType());
7336 //
7337 // char element_name[32];
7338 // ::snprintf (element_name, sizeof (element_name),
7339 // "%s[%u]", parent_name ? parent_name : "", idx);
7340 //
7341 // child_name.assign(element_name);
7342 // assert(field_type_info.first % 8 == 0);
7343 // child_byte_size = field_type_info.first / 8;
7344 // child_byte_offset = idx * child_byte_size;
7345 // return array->getElementType().getAsOpaquePtr();
7346 // }
7347 } break;
7348
7349 // case clang::Type::MemberPointerType:
7350 // {
7351 // MemberPointerType *mem_ptr_type =
7352 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7353 // clang::QualType pointee_type =
7354 // mem_ptr_type->getPointeeType();
7355 //
7356 // if (ClangASTContext::IsAggregateType
7357 // (pointee_type.getAsOpaquePtr()))
7358 // {
7359 // return GetIndexOfChildWithName (ast,
7360 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7361 // name);
7362 // }
7363 // }
7364 // break;
7365 //
7366 case clang::Type::LValueReference:
7367 case clang::Type::RValueReference: {
7368 const clang::ReferenceType *reference_type =
7369 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7370 CompilerType pointee_type(getASTContext(),
7371 reference_type->getPointeeType());
7372
7373 if (pointee_type.IsAggregateType()) {
7374 return pointee_type.GetIndexOfChildWithName(name,
7375 omit_empty_base_classes);
7376 }
7377 } break;
7378
7379 case clang::Type::Pointer: {
7380 const clang::PointerType *pointer_type =
7381 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7382 CompilerType pointee_type(getASTContext(),
7383 pointer_type->getPointeeType());
7384
7385 if (pointee_type.IsAggregateType()) {
7386 return pointee_type.GetIndexOfChildWithName(name,
7387 omit_empty_base_classes);
7388 } else {
7389 // if (parent_name)
7390 // {
7391 // child_name.assign(1, '*');
7392 // child_name += parent_name;
7393 // }
7394 //
7395 // // We have a pointer to an simple type
7396 // if (idx == 0)
7397 // {
7398 // std::pair<uint64_t, unsigned> clang_type_info
7399 // = ast->getTypeInfo(pointee_type);
7400 // assert(clang_type_info.first % 8 == 0);
7401 // child_byte_size = clang_type_info.first / 8;
7402 // child_byte_offset = 0;
7403 // return pointee_type.getAsOpaquePtr();
7404 // }
7405 }
7406 } break;
7407
7408 case clang::Type::Auto:
7409 return CompilerType(
7410 getASTContext(),
7411 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7412 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7413
7414 case clang::Type::Elaborated:
7415 return CompilerType(
7416 getASTContext(),
7417 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7418 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7419
7420 case clang::Type::Paren:
7421 return CompilerType(getASTContext(),
7422 llvm::cast<clang::ParenType>(qual_type)->desugar())
7423 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7424
7425 case clang::Type::Typedef:
7426 return CompilerType(getASTContext(),
7427 llvm::cast<clang::TypedefType>(qual_type)
7428 ->getDecl()
7429 ->getUnderlyingType())
7430 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7431
7432 default:
7433 break;
7434 }
7435 }
7436 return UINT32_MAX;
7437}
Greg Claytond8d4a572015-08-11 21:38:15 +00007438
7439size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007440ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7441 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007442 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007443
Kate Stoneb9c1b512016-09-06 20:57:50 +00007444 clang::QualType qual_type(GetCanonicalQualType(type));
7445 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7446 switch (type_class) {
7447 case clang::Type::Record:
7448 if (GetCompleteType(type)) {
7449 const clang::CXXRecordDecl *cxx_record_decl =
7450 qual_type->getAsCXXRecordDecl();
7451 if (cxx_record_decl) {
7452 const clang::ClassTemplateSpecializationDecl *template_decl =
7453 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7454 cxx_record_decl);
7455 if (template_decl)
7456 return template_decl->getTemplateArgs().size();
7457 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007458 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007459 break;
7460
7461 case clang::Type::Typedef:
7462 return (CompilerType(getASTContext(),
7463 llvm::cast<clang::TypedefType>(qual_type)
7464 ->getDecl()
7465 ->getUnderlyingType()))
7466 .GetNumTemplateArguments();
7467
7468 case clang::Type::Auto:
7469 return (CompilerType(
7470 getASTContext(),
7471 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7472 .GetNumTemplateArguments();
7473
7474 case clang::Type::Elaborated:
7475 return (CompilerType(
7476 getASTContext(),
7477 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7478 .GetNumTemplateArguments();
7479
7480 case clang::Type::Paren:
7481 return (CompilerType(getASTContext(),
7482 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7483 .GetNumTemplateArguments();
7484
7485 default:
7486 break;
7487 }
7488
7489 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007490}
7491
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007492CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00007493ClangASTContext::GetTemplateArgument(lldb::opaque_compiler_type_t type,
7494 size_t arg_idx,
7495 lldb::TemplateArgumentKind &kind) {
7496 if (!type)
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007497 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00007498
7499 clang::QualType qual_type(GetCanonicalQualType(type));
7500 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7501 switch (type_class) {
7502 case clang::Type::Record:
7503 if (GetCompleteType(type)) {
7504 const clang::CXXRecordDecl *cxx_record_decl =
7505 qual_type->getAsCXXRecordDecl();
7506 if (cxx_record_decl) {
7507 const clang::ClassTemplateSpecializationDecl *template_decl =
7508 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7509 cxx_record_decl);
7510 if (template_decl &&
7511 arg_idx < template_decl->getTemplateArgs().size()) {
7512 const clang::TemplateArgument &template_arg =
7513 template_decl->getTemplateArgs()[arg_idx];
7514 switch (template_arg.getKind()) {
7515 case clang::TemplateArgument::Null:
7516 kind = eTemplateArgumentKindNull;
7517 return CompilerType();
7518
7519 case clang::TemplateArgument::Type:
7520 kind = eTemplateArgumentKindType;
7521 return CompilerType(getASTContext(), template_arg.getAsType());
7522
7523 case clang::TemplateArgument::Declaration:
7524 kind = eTemplateArgumentKindDeclaration;
7525 return CompilerType();
7526
7527 case clang::TemplateArgument::Integral:
7528 kind = eTemplateArgumentKindIntegral;
7529 return CompilerType(getASTContext(),
7530 template_arg.getIntegralType());
7531
7532 case clang::TemplateArgument::Template:
7533 kind = eTemplateArgumentKindTemplate;
7534 return CompilerType();
7535
7536 case clang::TemplateArgument::TemplateExpansion:
7537 kind = eTemplateArgumentKindTemplateExpansion;
7538 return CompilerType();
7539
7540 case clang::TemplateArgument::Expression:
7541 kind = eTemplateArgumentKindExpression;
7542 return CompilerType();
7543
7544 case clang::TemplateArgument::Pack:
7545 kind = eTemplateArgumentKindPack;
7546 return CompilerType();
7547
7548 default:
7549 assert(!"Unhandled clang::TemplateArgument::ArgKind");
7550 break;
7551 }
7552 }
7553 }
7554 }
7555 break;
7556
7557 case clang::Type::Typedef:
7558 return (CompilerType(getASTContext(),
7559 llvm::cast<clang::TypedefType>(qual_type)
7560 ->getDecl()
7561 ->getUnderlyingType()))
7562 .GetTemplateArgument(arg_idx, kind);
7563
7564 case clang::Type::Auto:
7565 return (CompilerType(
7566 getASTContext(),
7567 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7568 .GetTemplateArgument(arg_idx, kind);
7569
7570 case clang::Type::Elaborated:
7571 return (CompilerType(
7572 getASTContext(),
7573 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7574 .GetTemplateArgument(arg_idx, kind);
7575
7576 case clang::Type::Paren:
7577 return (CompilerType(getASTContext(),
7578 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7579 .GetTemplateArgument(arg_idx, kind);
7580
7581 default:
7582 break;
7583 }
7584 kind = eTemplateArgumentKindNull;
7585 return CompilerType();
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007586}
7587
Kate Stoneb9c1b512016-09-06 20:57:50 +00007588CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7589 if (type)
7590 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7591 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007592}
7593
Kate Stoneb9c1b512016-09-06 20:57:50 +00007594clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7595 const clang::EnumType *enutype =
7596 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7597 if (enutype)
7598 return enutype->getDecl();
7599 return NULL;
7600}
7601
7602clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7603 const clang::RecordType *record_type =
7604 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7605 if (record_type)
7606 return record_type->getDecl();
7607 return nullptr;
7608}
7609
7610clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
7611 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
7612 if (qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00007613 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007614 else
7615 return qual_type->getAsTagDecl();
Greg Claytone6b36cd2015-12-08 01:02:08 +00007616}
7617
Greg Claytond8d4a572015-08-11 21:38:15 +00007618clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007619ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7620 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007621}
7622
7623clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007624ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7625 const clang::ObjCObjectType *objc_class_type =
7626 llvm::dyn_cast<clang::ObjCObjectType>(
7627 ClangUtil::GetCanonicalQualType(type));
7628 if (objc_class_type)
7629 return objc_class_type->getInterface();
7630 return nullptr;
7631}
7632
7633clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
7634 const CompilerType &type, const char *name,
7635 const CompilerType &field_clang_type, AccessType access,
7636 uint32_t bitfield_bit_size) {
7637 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007638 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007639 ClangASTContext *ast =
7640 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7641 if (!ast)
7642 return nullptr;
7643 clang::ASTContext *clang_ast = ast->getASTContext();
7644
7645 clang::FieldDecl *field = nullptr;
7646
7647 clang::Expr *bit_width = nullptr;
7648 if (bitfield_bit_size != 0) {
7649 llvm::APInt bitfield_bit_size_apint(
7650 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7651 bit_width = new (*clang_ast)
7652 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7653 clang_ast->IntTy, clang::SourceLocation());
7654 }
7655
7656 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7657 if (record_decl) {
7658 field = clang::FieldDecl::Create(
7659 *clang_ast, record_decl, clang::SourceLocation(),
7660 clang::SourceLocation(),
7661 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7662 ClangUtil::GetQualType(field_clang_type), // Field type
7663 nullptr, // TInfo *
7664 bit_width, // BitWidth
7665 false, // Mutable
7666 clang::ICIS_NoInit); // HasInit
7667
7668 if (!name) {
7669 // Determine whether this field corresponds to an anonymous
7670 // struct or union.
7671 if (const clang::TagType *TagT =
7672 field->getType()->getAs<clang::TagType>()) {
7673 if (clang::RecordDecl *Rec =
7674 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7675 if (!Rec->getDeclName()) {
7676 Rec->setAnonymousStructOrUnion(true);
7677 field->setImplicit();
7678 }
7679 }
7680 }
7681
7682 if (field) {
7683 field->setAccess(
7684 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7685
7686 record_decl->addDecl(field);
7687
7688#ifdef LLDB_CONFIGURATION_DEBUG
7689 VerifyDecl(field);
7690#endif
7691 }
7692 } else {
7693 clang::ObjCInterfaceDecl *class_interface_decl =
7694 ast->GetAsObjCInterfaceDecl(type);
7695
7696 if (class_interface_decl) {
7697 const bool is_synthesized = false;
7698
7699 field_clang_type.GetCompleteType();
7700
7701 field = clang::ObjCIvarDecl::Create(
7702 *clang_ast, class_interface_decl, clang::SourceLocation(),
7703 clang::SourceLocation(),
7704 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7705 ClangUtil::GetQualType(field_clang_type), // Field type
7706 nullptr, // TypeSourceInfo *
7707 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7708 is_synthesized);
7709
7710 if (field) {
7711 class_interface_decl->addDecl(field);
7712
7713#ifdef LLDB_CONFIGURATION_DEBUG
7714 VerifyDecl(field);
7715#endif
7716 }
7717 }
7718 }
7719 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007720}
7721
Kate Stoneb9c1b512016-09-06 20:57:50 +00007722void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7723 if (!type)
7724 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007725
Kate Stoneb9c1b512016-09-06 20:57:50 +00007726 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7727 if (!ast)
7728 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007729
Kate Stoneb9c1b512016-09-06 20:57:50 +00007730 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007731
Kate Stoneb9c1b512016-09-06 20:57:50 +00007732 if (!record_decl)
7733 return;
7734
7735 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7736
7737 IndirectFieldVector indirect_fields;
7738 clang::RecordDecl::field_iterator field_pos;
7739 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7740 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7741 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7742 last_field_pos = field_pos++) {
7743 if (field_pos->isAnonymousStructOrUnion()) {
7744 clang::QualType field_qual_type = field_pos->getType();
7745
7746 const clang::RecordType *field_record_type =
7747 field_qual_type->getAs<clang::RecordType>();
7748
7749 if (!field_record_type)
7750 continue;
7751
7752 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7753
7754 if (!field_record_decl)
7755 continue;
7756
7757 for (clang::RecordDecl::decl_iterator
7758 di = field_record_decl->decls_begin(),
7759 de = field_record_decl->decls_end();
7760 di != de; ++di) {
7761 if (clang::FieldDecl *nested_field_decl =
7762 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7763 clang::NamedDecl **chain =
7764 new (*ast->getASTContext()) clang::NamedDecl *[2];
7765 chain[0] = *field_pos;
7766 chain[1] = nested_field_decl;
7767 clang::IndirectFieldDecl *indirect_field =
7768 clang::IndirectFieldDecl::Create(
7769 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7770 nested_field_decl->getIdentifier(),
7771 nested_field_decl->getType(), {chain, 2});
7772
7773 indirect_field->setImplicit();
7774
7775 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7776 field_pos->getAccess(), nested_field_decl->getAccess()));
7777
7778 indirect_fields.push_back(indirect_field);
7779 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7780 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7781 size_t nested_chain_size =
7782 nested_indirect_field_decl->getChainingSize();
7783 clang::NamedDecl **chain = new (*ast->getASTContext())
7784 clang::NamedDecl *[nested_chain_size + 1];
7785 chain[0] = *field_pos;
7786
7787 int chain_index = 1;
7788 for (clang::IndirectFieldDecl::chain_iterator
7789 nci = nested_indirect_field_decl->chain_begin(),
7790 nce = nested_indirect_field_decl->chain_end();
7791 nci < nce; ++nci) {
7792 chain[chain_index] = *nci;
7793 chain_index++;
7794 }
7795
7796 clang::IndirectFieldDecl *indirect_field =
7797 clang::IndirectFieldDecl::Create(
7798 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7799 nested_indirect_field_decl->getIdentifier(),
7800 nested_indirect_field_decl->getType(),
7801 {chain, nested_chain_size + 1});
7802
7803 indirect_field->setImplicit();
7804
7805 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7806 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
7807
7808 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00007809 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007810 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007811 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007812 }
7813
7814 // Check the last field to see if it has an incomplete array type as its
7815 // last member and if it does, the tell the record decl about it
7816 if (last_field_pos != field_end_pos) {
7817 if (last_field_pos->getType()->isIncompleteArrayType())
7818 record_decl->hasFlexibleArrayMember();
7819 }
7820
7821 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
7822 ife = indirect_fields.end();
7823 ifi < ife; ++ifi) {
7824 record_decl->addDecl(*ifi);
7825 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007826}
7827
Kate Stoneb9c1b512016-09-06 20:57:50 +00007828void ClangASTContext::SetIsPacked(const CompilerType &type) {
7829 if (type) {
7830 ClangASTContext *ast =
7831 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7832 if (ast) {
7833 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
7834
7835 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00007836 return;
7837
Kate Stoneb9c1b512016-09-06 20:57:50 +00007838 record_decl->addAttr(
7839 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00007840 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007841 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007842}
7843
Kate Stoneb9c1b512016-09-06 20:57:50 +00007844clang::VarDecl *ClangASTContext::AddVariableToRecordType(
7845 const CompilerType &type, const char *name, const CompilerType &var_type,
7846 AccessType access) {
7847 clang::VarDecl *var_decl = nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007848
Kate Stoneb9c1b512016-09-06 20:57:50 +00007849 if (!type.IsValid() || !var_type.IsValid())
7850 return nullptr;
7851 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7852 if (!ast)
7853 return nullptr;
7854
7855 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7856 if (record_decl) {
7857 var_decl = clang::VarDecl::Create(
7858 *ast->getASTContext(), // ASTContext &
7859 record_decl, // DeclContext *
7860 clang::SourceLocation(), // clang::SourceLocation StartLoc
7861 clang::SourceLocation(), // clang::SourceLocation IdLoc
7862 name ? &ast->getASTContext()->Idents.get(name)
7863 : nullptr, // clang::IdentifierInfo *
7864 ClangUtil::GetQualType(var_type), // Variable clang::QualType
7865 nullptr, // TypeSourceInfo *
7866 clang::SC_Static); // StorageClass
7867 if (var_decl) {
7868 var_decl->setAccess(
7869 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7870 record_decl->addDecl(var_decl);
7871
Greg Claytond8d4a572015-08-11 21:38:15 +00007872#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00007873 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00007874#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00007875 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007876 }
7877 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00007878}
7879
Kate Stoneb9c1b512016-09-06 20:57:50 +00007880clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
7881 lldb::opaque_compiler_type_t type, const char *name,
7882 const CompilerType &method_clang_type, lldb::AccessType access,
7883 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
7884 bool is_attr_used, bool is_artificial) {
7885 if (!type || !method_clang_type.IsValid() || name == nullptr ||
7886 name[0] == '\0')
7887 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007888
Kate Stoneb9c1b512016-09-06 20:57:50 +00007889 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007890
Kate Stoneb9c1b512016-09-06 20:57:50 +00007891 clang::CXXRecordDecl *cxx_record_decl =
7892 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007893
Kate Stoneb9c1b512016-09-06 20:57:50 +00007894 if (cxx_record_decl == nullptr)
7895 return nullptr;
7896
7897 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
7898
7899 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7900
7901 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
7902
7903 const clang::FunctionType *function_type =
7904 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7905
7906 if (function_type == nullptr)
7907 return nullptr;
7908
7909 const clang::FunctionProtoType *method_function_prototype(
7910 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7911
7912 if (!method_function_prototype)
7913 return nullptr;
7914
7915 unsigned int num_params = method_function_prototype->getNumParams();
7916
7917 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7918 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7919
7920 if (is_artificial)
7921 return nullptr; // skip everything artificial
7922
7923 if (name[0] == '~') {
7924 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
7925 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7926 clang::DeclarationNameInfo(
7927 getASTContext()->DeclarationNames.getCXXDestructorName(
7928 getASTContext()->getCanonicalType(record_qual_type)),
7929 clang::SourceLocation()),
7930 method_qual_type, nullptr, is_inline, is_artificial);
7931 cxx_method_decl = cxx_dtor_decl;
7932 } else if (decl_name == cxx_record_decl->getDeclName()) {
7933 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
7934 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7935 clang::DeclarationNameInfo(
7936 getASTContext()->DeclarationNames.getCXXConstructorName(
7937 getASTContext()->getCanonicalType(record_qual_type)),
7938 clang::SourceLocation()),
7939 method_qual_type,
7940 nullptr, // TypeSourceInfo *
7941 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
7942 cxx_method_decl = cxx_ctor_decl;
7943 } else {
7944 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7945 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7946
7947 if (IsOperator(name, op_kind)) {
7948 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
7949 // Check the number of operator parameters. Sometimes we have
7950 // seen bad DWARF that doesn't correctly describe operators and
7951 // if we try to create a method and add it to the class, clang
7952 // will assert and crash, so we need to make sure things are
7953 // acceptable.
7954 const bool is_method = true;
7955 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
7956 is_method, op_kind, num_params))
7957 return nullptr;
7958 cxx_method_decl = clang::CXXMethodDecl::Create(
7959 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7960 clang::DeclarationNameInfo(
7961 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
7962 clang::SourceLocation()),
7963 method_qual_type,
7964 nullptr, // TypeSourceInfo *
7965 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
7966 } else if (num_params == 0) {
7967 // Conversion operators don't take params...
7968 cxx_method_decl = clang::CXXConversionDecl::Create(
7969 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7970 clang::DeclarationNameInfo(
7971 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
7972 getASTContext()->getCanonicalType(
7973 function_type->getReturnType())),
7974 clang::SourceLocation()),
7975 method_qual_type,
7976 nullptr, // TypeSourceInfo *
7977 is_inline, is_explicit, false /*is_constexpr*/,
7978 clang::SourceLocation());
7979 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007980 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007981
7982 if (cxx_method_decl == nullptr) {
7983 cxx_method_decl = clang::CXXMethodDecl::Create(
7984 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7985 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
7986 method_qual_type,
7987 nullptr, // TypeSourceInfo *
7988 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00007989 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007990 }
7991
7992 clang::AccessSpecifier access_specifier =
7993 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
7994
7995 cxx_method_decl->setAccess(access_specifier);
7996 cxx_method_decl->setVirtualAsWritten(is_virtual);
7997
7998 if (is_attr_used)
7999 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8000
8001 // Populate the method decl with parameter decls
8002
8003 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8004
8005 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8006 params.push_back(clang::ParmVarDecl::Create(
8007 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8008 clang::SourceLocation(),
8009 nullptr, // anonymous
8010 method_function_prototype->getParamType(param_index), nullptr,
8011 clang::SC_None, nullptr));
8012 }
8013
8014 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8015
8016 cxx_record_decl->addDecl(cxx_method_decl);
8017
8018 // Sometimes the debug info will mention a constructor (default/copy/move),
8019 // destructor, or assignment operator (copy/move) but there won't be any
8020 // version of this in the code. So we check if the function was artificially
8021 // generated and if it is trivial and this lets the compiler/backend know
8022 // that it can inline the IR for these when it needs to and we can avoid a
8023 // "missing function" error when running expressions.
8024
8025 if (is_artificial) {
8026 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8027 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8028 (cxx_ctor_decl->isCopyConstructor() &&
8029 cxx_record_decl->hasTrivialCopyConstructor()) ||
8030 (cxx_ctor_decl->isMoveConstructor() &&
8031 cxx_record_decl->hasTrivialMoveConstructor()))) {
8032 cxx_ctor_decl->setDefaulted();
8033 cxx_ctor_decl->setTrivial(true);
8034 } else if (cxx_dtor_decl) {
8035 if (cxx_record_decl->hasTrivialDestructor()) {
8036 cxx_dtor_decl->setDefaulted();
8037 cxx_dtor_decl->setTrivial(true);
8038 }
8039 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8040 cxx_record_decl->hasTrivialCopyAssignment()) ||
8041 (cxx_method_decl->isMoveAssignmentOperator() &&
8042 cxx_record_decl->hasTrivialMoveAssignment())) {
8043 cxx_method_decl->setDefaulted();
8044 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008045 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008046 }
8047
Greg Claytond8d4a572015-08-11 21:38:15 +00008048#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008049 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008050#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008051
Kate Stoneb9c1b512016-09-06 20:57:50 +00008052 // printf ("decl->isPolymorphic() = %i\n",
8053 // cxx_record_decl->isPolymorphic());
8054 // printf ("decl->isAggregate() = %i\n",
8055 // cxx_record_decl->isAggregate());
8056 // printf ("decl->isPOD() = %i\n",
8057 // cxx_record_decl->isPOD());
8058 // printf ("decl->isEmpty() = %i\n",
8059 // cxx_record_decl->isEmpty());
8060 // printf ("decl->isAbstract() = %i\n",
8061 // cxx_record_decl->isAbstract());
8062 // printf ("decl->hasTrivialConstructor() = %i\n",
8063 // cxx_record_decl->hasTrivialConstructor());
8064 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8065 // cxx_record_decl->hasTrivialCopyConstructor());
8066 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8067 // cxx_record_decl->hasTrivialCopyAssignment());
8068 // printf ("decl->hasTrivialDestructor() = %i\n",
8069 // cxx_record_decl->hasTrivialDestructor());
8070 return cxx_method_decl;
8071}
Greg Claytond8d4a572015-08-11 21:38:15 +00008072
8073#pragma mark C++ Base Classes
8074
8075clang::CXXBaseSpecifier *
Kate Stoneb9c1b512016-09-06 20:57:50 +00008076ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8077 AccessType access, bool is_virtual,
8078 bool base_of_class) {
8079 if (type)
8080 return new clang::CXXBaseSpecifier(
8081 clang::SourceRange(), is_virtual, base_of_class,
8082 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8083 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8084 clang::SourceLocation());
8085 return nullptr;
8086}
8087
8088void ClangASTContext::DeleteBaseClassSpecifiers(
8089 clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes) {
8090 for (unsigned i = 0; i < num_base_classes; ++i) {
8091 delete base_classes[i];
8092 base_classes[i] = nullptr;
8093 }
8094}
8095
8096bool ClangASTContext::SetBaseClassesForClassType(
8097 lldb::opaque_compiler_type_t type,
8098 clang::CXXBaseSpecifier const *const *base_classes,
8099 unsigned num_base_classes) {
8100 if (type) {
8101 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8102 if (cxx_record_decl) {
8103 cxx_record_decl->setBases(base_classes, num_base_classes);
8104 return true;
8105 }
8106 }
8107 return false;
8108}
8109
8110bool ClangASTContext::SetObjCSuperClass(
8111 const CompilerType &type, const CompilerType &superclass_clang_type) {
8112 ClangASTContext *ast =
8113 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8114 if (!ast)
8115 return false;
8116 clang::ASTContext *clang_ast = ast->getASTContext();
8117
8118 if (type && superclass_clang_type.IsValid() &&
8119 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8120 clang::ObjCInterfaceDecl *class_interface_decl =
8121 GetAsObjCInterfaceDecl(type);
8122 clang::ObjCInterfaceDecl *super_interface_decl =
8123 GetAsObjCInterfaceDecl(superclass_clang_type);
8124 if (class_interface_decl && super_interface_decl) {
8125 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8126 clang_ast->getObjCInterfaceType(super_interface_decl)));
8127 return true;
8128 }
8129 }
8130 return false;
8131}
8132
8133bool ClangASTContext::AddObjCClassProperty(
8134 const CompilerType &type, const char *property_name,
8135 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8136 const char *property_setter_name, const char *property_getter_name,
8137 uint32_t property_attributes, ClangASTMetadata *metadata) {
8138 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8139 property_name[0] == '\0')
8140 return false;
8141 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8142 if (!ast)
8143 return false;
8144 clang::ASTContext *clang_ast = ast->getASTContext();
8145
8146 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8147
8148 if (class_interface_decl) {
8149 CompilerType property_clang_type_to_access;
8150
8151 if (property_clang_type.IsValid())
8152 property_clang_type_to_access = property_clang_type;
8153 else if (ivar_decl)
8154 property_clang_type_to_access =
8155 CompilerType(clang_ast, ivar_decl->getType());
8156
8157 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8158 clang::TypeSourceInfo *prop_type_source;
8159 if (ivar_decl)
8160 prop_type_source =
8161 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8162 else
8163 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8164 ClangUtil::GetQualType(property_clang_type));
8165
8166 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8167 *clang_ast, class_interface_decl,
8168 clang::SourceLocation(), // Source Location
8169 &clang_ast->Idents.get(property_name),
8170 clang::SourceLocation(), // Source Location for AT
8171 clang::SourceLocation(), // Source location for (
8172 ivar_decl ? ivar_decl->getType()
8173 : ClangUtil::GetQualType(property_clang_type),
8174 prop_type_source);
8175
8176 if (property_decl) {
8177 if (metadata)
8178 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8179
8180 class_interface_decl->addDecl(property_decl);
8181
8182 clang::Selector setter_sel, getter_sel;
8183
8184 if (property_setter_name != nullptr) {
8185 std::string property_setter_no_colon(
8186 property_setter_name, strlen(property_setter_name) - 1);
8187 clang::IdentifierInfo *setter_ident =
8188 &clang_ast->Idents.get(property_setter_no_colon.c_str());
8189 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8190 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8191 std::string setter_sel_string("set");
8192 setter_sel_string.push_back(::toupper(property_name[0]));
8193 setter_sel_string.append(&property_name[1]);
8194 clang::IdentifierInfo *setter_ident =
8195 &clang_ast->Idents.get(setter_sel_string.c_str());
8196 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8197 }
8198 property_decl->setSetterName(setter_sel);
8199 property_decl->setPropertyAttributes(
8200 clang::ObjCPropertyDecl::OBJC_PR_setter);
8201
8202 if (property_getter_name != nullptr) {
8203 clang::IdentifierInfo *getter_ident =
8204 &clang_ast->Idents.get(property_getter_name);
8205 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8206 } else {
8207 clang::IdentifierInfo *getter_ident =
8208 &clang_ast->Idents.get(property_name);
8209 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8210 }
8211 property_decl->setGetterName(getter_sel);
8212 property_decl->setPropertyAttributes(
8213 clang::ObjCPropertyDecl::OBJC_PR_getter);
8214
8215 if (ivar_decl)
8216 property_decl->setPropertyIvarDecl(ivar_decl);
8217
8218 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8219 property_decl->setPropertyAttributes(
8220 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8221 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8222 property_decl->setPropertyAttributes(
8223 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8224 if (property_attributes & DW_APPLE_PROPERTY_assign)
8225 property_decl->setPropertyAttributes(
8226 clang::ObjCPropertyDecl::OBJC_PR_assign);
8227 if (property_attributes & DW_APPLE_PROPERTY_retain)
8228 property_decl->setPropertyAttributes(
8229 clang::ObjCPropertyDecl::OBJC_PR_retain);
8230 if (property_attributes & DW_APPLE_PROPERTY_copy)
8231 property_decl->setPropertyAttributes(
8232 clang::ObjCPropertyDecl::OBJC_PR_copy);
8233 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8234 property_decl->setPropertyAttributes(
8235 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8236 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8237 property_decl->setPropertyAttributes(
8238 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8239 if (property_attributes &
8240 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8241 property_decl->setPropertyAttributes(
8242 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8243 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8244 property_decl->setPropertyAttributes(
8245 clang::ObjCPropertyDecl::OBJC_PR_class);
8246
8247 const bool isInstance =
8248 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8249
8250 if (!getter_sel.isNull() &&
8251 !(isInstance
8252 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8253 : class_interface_decl->lookupClassMethod(getter_sel))) {
8254 const bool isVariadic = false;
8255 const bool isSynthesized = false;
8256 const bool isImplicitlyDeclared = true;
8257 const bool isDefined = false;
8258 const clang::ObjCMethodDecl::ImplementationControl impControl =
8259 clang::ObjCMethodDecl::None;
8260 const bool HasRelatedResultType = false;
8261
8262 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8263 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8264 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8265 nullptr, class_interface_decl, isInstance, isVariadic,
8266 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8267 HasRelatedResultType);
8268
8269 if (getter && metadata)
8270 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8271
8272 if (getter) {
8273 getter->setMethodParams(*clang_ast,
8274 llvm::ArrayRef<clang::ParmVarDecl *>(),
8275 llvm::ArrayRef<clang::SourceLocation>());
8276
8277 class_interface_decl->addDecl(getter);
8278 }
8279 }
8280
8281 if (!setter_sel.isNull() &&
8282 !(isInstance
8283 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8284 : class_interface_decl->lookupClassMethod(setter_sel))) {
8285 clang::QualType result_type = clang_ast->VoidTy;
8286 const bool isVariadic = false;
8287 const bool isSynthesized = false;
8288 const bool isImplicitlyDeclared = true;
8289 const bool isDefined = false;
8290 const clang::ObjCMethodDecl::ImplementationControl impControl =
8291 clang::ObjCMethodDecl::None;
8292 const bool HasRelatedResultType = false;
8293
8294 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8295 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8296 setter_sel, result_type, nullptr, class_interface_decl,
8297 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8298 isDefined, impControl, HasRelatedResultType);
8299
8300 if (setter && metadata)
8301 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8302
8303 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8304
8305 params.push_back(clang::ParmVarDecl::Create(
8306 *clang_ast, setter, clang::SourceLocation(),
8307 clang::SourceLocation(),
8308 nullptr, // anonymous
8309 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8310 clang::SC_Auto, nullptr));
8311
8312 if (setter) {
8313 setter->setMethodParams(
8314 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8315 llvm::ArrayRef<clang::SourceLocation>());
8316
8317 class_interface_decl->addDecl(setter);
8318 }
8319 }
8320
8321 return true;
8322 }
8323 }
8324 }
8325 return false;
8326}
8327
8328bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8329 bool check_superclass) {
8330 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8331 if (class_interface_decl)
8332 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8333 return false;
8334}
8335
8336clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8337 const CompilerType &type,
8338 const char *name, // the full symbol name as seen in the symbol table
8339 // (lldb::opaque_compiler_type_t type, "-[NString
8340 // stringWithCString:]")
8341 const CompilerType &method_clang_type, lldb::AccessType access,
8342 bool is_artificial, bool is_variadic) {
8343 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008344 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008345
Kate Stoneb9c1b512016-09-06 20:57:50 +00008346 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8347
8348 if (class_interface_decl == nullptr)
8349 return nullptr;
8350 ClangASTContext *lldb_ast =
8351 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8352 if (lldb_ast == nullptr)
8353 return nullptr;
8354 clang::ASTContext *ast = lldb_ast->getASTContext();
8355
8356 const char *selector_start = ::strchr(name, ' ');
8357 if (selector_start == nullptr)
8358 return nullptr;
8359
8360 selector_start++;
8361 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8362
8363 size_t len = 0;
8364 const char *start;
8365 // printf ("name = '%s'\n", name);
8366
8367 unsigned num_selectors_with_args = 0;
8368 for (start = selector_start; start && *start != '\0' && *start != ']';
8369 start += len) {
8370 len = ::strcspn(start, ":]");
8371 bool has_arg = (start[len] == ':');
8372 if (has_arg)
8373 ++num_selectors_with_args;
8374 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8375 if (has_arg)
8376 len += 1;
8377 }
8378
8379 if (selector_idents.size() == 0)
8380 return nullptr;
8381
8382 clang::Selector method_selector = ast->Selectors.getSelector(
8383 num_selectors_with_args ? selector_idents.size() : 0,
8384 selector_idents.data());
8385
8386 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8387
8388 // Populate the method decl with parameter decls
8389 const clang::Type *method_type(method_qual_type.getTypePtr());
8390
8391 if (method_type == nullptr)
8392 return nullptr;
8393
8394 const clang::FunctionProtoType *method_function_prototype(
8395 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8396
8397 if (!method_function_prototype)
8398 return nullptr;
8399
8400 bool is_synthesized = false;
8401 bool is_defined = false;
8402 clang::ObjCMethodDecl::ImplementationControl imp_control =
8403 clang::ObjCMethodDecl::None;
8404
8405 const unsigned num_args = method_function_prototype->getNumParams();
8406
8407 if (num_args != num_selectors_with_args)
8408 return nullptr; // some debug information is corrupt. We are not going to
8409 // deal with it.
8410
8411 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8412 *ast,
8413 clang::SourceLocation(), // beginLoc,
8414 clang::SourceLocation(), // endLoc,
8415 method_selector, method_function_prototype->getReturnType(),
8416 nullptr, // TypeSourceInfo *ResultTInfo,
8417 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8418 ClangUtil::GetQualType(type)),
8419 name[0] == '-', is_variadic, is_synthesized,
8420 true, // is_implicitly_declared; we force this to true because we don't
8421 // have source locations
8422 is_defined, imp_control, false /*has_related_result_type*/);
8423
8424 if (objc_method_decl == nullptr)
8425 return nullptr;
8426
8427 if (num_args > 0) {
8428 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8429
8430 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8431 params.push_back(clang::ParmVarDecl::Create(
8432 *ast, objc_method_decl, clang::SourceLocation(),
8433 clang::SourceLocation(),
8434 nullptr, // anonymous
8435 method_function_prototype->getParamType(param_index), nullptr,
8436 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008437 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008438
Kate Stoneb9c1b512016-09-06 20:57:50 +00008439 objc_method_decl->setMethodParams(
8440 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8441 llvm::ArrayRef<clang::SourceLocation>());
8442 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008443
Kate Stoneb9c1b512016-09-06 20:57:50 +00008444 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008445
Greg Claytond8d4a572015-08-11 21:38:15 +00008446#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008447 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008448#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008449
8450 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008451}
8452
Kate Stoneb9c1b512016-09-06 20:57:50 +00008453bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8454 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008455 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008456
Kate Stoneb9c1b512016-09-06 20:57:50 +00008457 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008458
Kate Stoneb9c1b512016-09-06 20:57:50 +00008459 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8460 switch (type_class) {
8461 case clang::Type::Record: {
8462 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8463 if (cxx_record_decl)
8464 return cxx_record_decl->hasExternalLexicalStorage() ||
8465 cxx_record_decl->hasExternalVisibleStorage();
8466 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008467
Kate Stoneb9c1b512016-09-06 20:57:50 +00008468 case clang::Type::Enum: {
8469 clang::EnumDecl *enum_decl =
8470 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8471 if (enum_decl)
8472 return enum_decl->hasExternalLexicalStorage() ||
8473 enum_decl->hasExternalVisibleStorage();
8474 } break;
8475
8476 case clang::Type::ObjCObject:
8477 case clang::Type::ObjCInterface: {
8478 const clang::ObjCObjectType *objc_class_type =
8479 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8480 assert(objc_class_type);
8481 if (objc_class_type) {
8482 clang::ObjCInterfaceDecl *class_interface_decl =
8483 objc_class_type->getInterface();
8484
8485 if (class_interface_decl)
8486 return class_interface_decl->hasExternalLexicalStorage() ||
8487 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008488 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008489 } break;
8490
8491 case clang::Type::Typedef:
8492 return GetHasExternalStorage(CompilerType(
8493 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8494 ->getDecl()
8495 ->getUnderlyingType()
8496 .getAsOpaquePtr()));
8497
8498 case clang::Type::Auto:
8499 return GetHasExternalStorage(CompilerType(
8500 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8501 ->getDeducedType()
8502 .getAsOpaquePtr()));
8503
8504 case clang::Type::Elaborated:
8505 return GetHasExternalStorage(CompilerType(
8506 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8507 ->getNamedType()
8508 .getAsOpaquePtr()));
8509
8510 case clang::Type::Paren:
8511 return GetHasExternalStorage(CompilerType(
8512 type.GetTypeSystem(),
8513 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8514
8515 default:
8516 break;
8517 }
8518 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008519}
8520
Kate Stoneb9c1b512016-09-06 20:57:50 +00008521bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8522 bool has_extern) {
8523 if (!type)
8524 return false;
8525
8526 clang::QualType qual_type(GetCanonicalQualType(type));
8527
8528 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8529 switch (type_class) {
8530 case clang::Type::Record: {
8531 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8532 if (cxx_record_decl) {
8533 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8534 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8535 return true;
8536 }
8537 } break;
8538
8539 case clang::Type::Enum: {
8540 clang::EnumDecl *enum_decl =
8541 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8542 if (enum_decl) {
8543 enum_decl->setHasExternalLexicalStorage(has_extern);
8544 enum_decl->setHasExternalVisibleStorage(has_extern);
8545 return true;
8546 }
8547 } break;
8548
8549 case clang::Type::ObjCObject:
8550 case clang::Type::ObjCInterface: {
8551 const clang::ObjCObjectType *objc_class_type =
8552 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8553 assert(objc_class_type);
8554 if (objc_class_type) {
8555 clang::ObjCInterfaceDecl *class_interface_decl =
8556 objc_class_type->getInterface();
8557
8558 if (class_interface_decl) {
8559 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8560 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8561 return true;
8562 }
8563 }
8564 } break;
8565
8566 case clang::Type::Typedef:
8567 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8568 ->getDecl()
8569 ->getUnderlyingType()
8570 .getAsOpaquePtr(),
8571 has_extern);
8572
8573 case clang::Type::Auto:
8574 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8575 ->getDeducedType()
8576 .getAsOpaquePtr(),
8577 has_extern);
8578
8579 case clang::Type::Elaborated:
8580 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8581 ->getNamedType()
8582 .getAsOpaquePtr(),
8583 has_extern);
8584
8585 case clang::Type::Paren:
8586 return SetHasExternalStorage(
8587 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8588 has_extern);
8589
8590 default:
8591 break;
8592 }
8593 return false;
8594}
Greg Claytond8d4a572015-08-11 21:38:15 +00008595
8596#pragma mark TagDecl
8597
Kate Stoneb9c1b512016-09-06 20:57:50 +00008598bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8599 clang::QualType qual_type(ClangUtil::GetQualType(type));
8600 if (!qual_type.isNull()) {
8601 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8602 if (tag_type) {
8603 clang::TagDecl *tag_decl = tag_type->getDecl();
8604 if (tag_decl) {
8605 tag_decl->startDefinition();
8606 return true;
8607 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008608 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008609
8610 const clang::ObjCObjectType *object_type =
8611 qual_type->getAs<clang::ObjCObjectType>();
8612 if (object_type) {
8613 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8614 if (interface_decl) {
8615 interface_decl->startDefinition();
8616 return true;
8617 }
8618 }
8619 }
8620 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008621}
8622
Kate Stoneb9c1b512016-09-06 20:57:50 +00008623bool ClangASTContext::CompleteTagDeclarationDefinition(
8624 const CompilerType &type) {
8625 clang::QualType qual_type(ClangUtil::GetQualType(type));
8626 if (!qual_type.isNull()) {
8627 // Make sure we use the same methodology as
8628 // ClangASTContext::StartTagDeclarationDefinition()
8629 // as to how we start/end the definition. Previously we were calling
8630 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8631 if (tag_type) {
8632 clang::TagDecl *tag_decl = tag_type->getDecl();
8633 if (tag_decl) {
8634 clang::CXXRecordDecl *cxx_record_decl =
8635 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8636
8637 if (cxx_record_decl) {
8638 if (!cxx_record_decl->isCompleteDefinition())
8639 cxx_record_decl->completeDefinition();
8640 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8641 cxx_record_decl->setHasExternalLexicalStorage(false);
8642 cxx_record_decl->setHasExternalVisibleStorage(false);
8643 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008644 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008645 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008646 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008647
8648 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8649
8650 if (enutype) {
8651 clang::EnumDecl *enum_decl = enutype->getDecl();
8652
8653 if (enum_decl) {
8654 if (!enum_decl->isCompleteDefinition()) {
8655 ClangASTContext *lldb_ast =
8656 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8657 if (lldb_ast == nullptr)
8658 return false;
8659 clang::ASTContext *ast = lldb_ast->getASTContext();
8660
8661 /// TODO This really needs to be fixed.
8662
8663 QualType integer_type(enum_decl->getIntegerType());
8664 if (!integer_type.isNull()) {
8665 unsigned NumPositiveBits = 1;
8666 unsigned NumNegativeBits = 0;
8667
8668 clang::QualType promotion_qual_type;
8669 // If the enum integer type is less than an integer in bit width,
8670 // then we must promote it to an integer size.
8671 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8672 ast->getTypeSize(ast->IntTy)) {
8673 if (enum_decl->getIntegerType()->isSignedIntegerType())
8674 promotion_qual_type = ast->IntTy;
8675 else
8676 promotion_qual_type = ast->UnsignedIntTy;
8677 } else
8678 promotion_qual_type = enum_decl->getIntegerType();
8679
8680 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8681 promotion_qual_type, NumPositiveBits,
8682 NumNegativeBits);
8683 }
8684 }
8685 return true;
8686 }
8687 }
8688 }
8689 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008690}
8691
Kate Stoneb9c1b512016-09-06 20:57:50 +00008692bool ClangASTContext::AddEnumerationValueToEnumerationType(
8693 lldb::opaque_compiler_type_t type,
8694 const CompilerType &enumerator_clang_type, const Declaration &decl,
8695 const char *name, int64_t enum_value, uint32_t enum_value_bit_size) {
8696 if (type && enumerator_clang_type.IsValid() && name && name[0]) {
8697 clang::QualType enum_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008698
Kate Stoneb9c1b512016-09-06 20:57:50 +00008699 bool is_signed = false;
8700 enumerator_clang_type.IsIntegerType(is_signed);
Greg Claytond8d4a572015-08-11 21:38:15 +00008701 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Kate Stoneb9c1b512016-09-06 20:57:50 +00008702 if (clang_type) {
8703 const clang::EnumType *enutype =
8704 llvm::dyn_cast<clang::EnumType>(clang_type);
8705
8706 if (enutype) {
8707 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8708 enum_llvm_apsint = enum_value;
8709 clang::EnumConstantDecl *enumerator_decl =
8710 clang::EnumConstantDecl::Create(
8711 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8712 name ? &getASTContext()->Idents.get(name)
8713 : nullptr, // Identifier
8714 ClangUtil::GetQualType(enumerator_clang_type),
8715 nullptr, enum_llvm_apsint);
8716
8717 if (enumerator_decl) {
8718 enutype->getDecl()->addDecl(enumerator_decl);
8719
8720#ifdef LLDB_CONFIGURATION_DEBUG
8721 VerifyDecl(enumerator_decl);
8722#endif
8723
8724 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008725 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008726 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008727 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008728 }
8729 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008730}
8731
Greg Claytona1e5dc82015-08-11 22:53:00 +00008732CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00008733ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8734 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8735 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8736 if (clang_type) {
8737 const clang::EnumType *enutype =
8738 llvm::dyn_cast<clang::EnumType>(clang_type);
8739 if (enutype) {
8740 clang::EnumDecl *enum_decl = enutype->getDecl();
8741 if (enum_decl)
8742 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008743 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008744 }
8745 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008746}
8747
Kate Stoneb9c1b512016-09-06 20:57:50 +00008748CompilerType
8749ClangASTContext::CreateMemberPointerType(const CompilerType &type,
8750 const CompilerType &pointee_type) {
8751 if (type && pointee_type.IsValid() &&
8752 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8753 ClangASTContext *ast =
8754 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8755 if (!ast)
8756 return CompilerType();
8757 return CompilerType(ast->getASTContext(),
8758 ast->getASTContext()->getMemberPointerType(
8759 ClangUtil::GetQualType(pointee_type),
8760 ClangUtil::GetQualType(type).getTypePtr()));
8761 }
8762 return CompilerType();
8763}
Greg Claytond8d4a572015-08-11 21:38:15 +00008764
8765size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00008766ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
8767 const char *s, uint8_t *dst,
8768 size_t dst_size) {
8769 if (type) {
8770 clang::QualType qual_type(GetCanonicalQualType(type));
8771 uint32_t count = 0;
8772 bool is_complex = false;
8773 if (IsFloatingPointType(type, count, is_complex)) {
8774 // TODO: handle complex and vector types
8775 if (count != 1)
8776 return false;
8777
8778 llvm::StringRef s_sref(s);
8779 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
8780 s_sref);
8781
8782 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
8783 const uint64_t byte_size = bit_size / 8;
8784 if (dst_size >= byte_size) {
8785 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
8786 llvm::NextPowerOf2(byte_size) * 8);
8787 lldb_private::Error get_data_error;
8788 if (scalar.GetAsMemoryData(dst, byte_size,
8789 lldb_private::endian::InlHostByteOrder(),
8790 get_data_error))
8791 return byte_size;
8792 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008793 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008794 }
8795 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00008796}
8797
Greg Claytond8d4a572015-08-11 21:38:15 +00008798//----------------------------------------------------------------------
8799// Dumping types
8800//----------------------------------------------------------------------
8801#define DEPTH_INCREMENT 2
8802
Kate Stoneb9c1b512016-09-06 20:57:50 +00008803void ClangASTContext::DumpValue(
8804 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
8805 lldb::Format format, const lldb_private::DataExtractor &data,
8806 lldb::offset_t data_byte_offset, size_t data_byte_size,
8807 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
8808 bool show_summary, bool verbose, uint32_t depth) {
8809 if (!type)
8810 return;
8811
8812 clang::QualType qual_type(GetQualType(type));
8813 switch (qual_type->getTypeClass()) {
8814 case clang::Type::Record:
8815 if (GetCompleteType(type)) {
8816 const clang::RecordType *record_type =
8817 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8818 const clang::RecordDecl *record_decl = record_type->getDecl();
8819 assert(record_decl);
8820 uint32_t field_bit_offset = 0;
8821 uint32_t field_byte_offset = 0;
8822 const clang::ASTRecordLayout &record_layout =
8823 getASTContext()->getASTRecordLayout(record_decl);
8824 uint32_t child_idx = 0;
8825
8826 const clang::CXXRecordDecl *cxx_record_decl =
8827 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8828 if (cxx_record_decl) {
8829 // We might have base classes to print out first
8830 clang::CXXRecordDecl::base_class_const_iterator base_class,
8831 base_class_end;
8832 for (base_class = cxx_record_decl->bases_begin(),
8833 base_class_end = cxx_record_decl->bases_end();
8834 base_class != base_class_end; ++base_class) {
8835 const clang::CXXRecordDecl *base_class_decl =
8836 llvm::cast<clang::CXXRecordDecl>(
8837 base_class->getType()->getAs<clang::RecordType>()->getDecl());
8838
8839 // Skip empty base classes
8840 if (verbose == false &&
8841 ClangASTContext::RecordHasFields(base_class_decl) == false)
8842 continue;
8843
8844 if (base_class->isVirtual())
8845 field_bit_offset =
8846 record_layout.getVBaseClassOffset(base_class_decl)
8847 .getQuantity() *
8848 8;
8849 else
8850 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
8851 .getQuantity() *
8852 8;
8853 field_byte_offset = field_bit_offset / 8;
8854 assert(field_bit_offset % 8 == 0);
8855 if (child_idx == 0)
8856 s->PutChar('{');
8857 else
8858 s->PutChar(',');
8859
8860 clang::QualType base_class_qual_type = base_class->getType();
8861 std::string base_class_type_name(base_class_qual_type.getAsString());
8862
8863 // Indent and print the base class type name
8864 s->Printf("\n%*s%s ", depth + DEPTH_INCREMENT, "",
8865 base_class_type_name.c_str());
8866
8867 clang::TypeInfo base_class_type_info =
8868 getASTContext()->getTypeInfo(base_class_qual_type);
8869
8870 // Dump the value of the member
8871 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
8872 base_clang_type.DumpValue(
8873 exe_ctx,
8874 s, // Stream to dump to
8875 base_clang_type
8876 .GetFormat(), // The format with which to display the member
8877 data, // Data buffer containing all bytes for this type
8878 data_byte_offset + field_byte_offset, // Offset into "data" where
8879 // to grab value from
8880 base_class_type_info.Width / 8, // Size of this type in bytes
8881 0, // Bitfield bit size
8882 0, // Bitfield bit offset
8883 show_types, // Boolean indicating if we should show the variable
8884 // types
8885 show_summary, // Boolean indicating if we should show a summary
8886 // for the current type
8887 verbose, // Verbose output?
8888 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8889 // children
8890
8891 ++child_idx;
8892 }
8893 }
8894 uint32_t field_idx = 0;
8895 clang::RecordDecl::field_iterator field, field_end;
8896 for (field = record_decl->field_begin(),
8897 field_end = record_decl->field_end();
8898 field != field_end; ++field, ++field_idx, ++child_idx) {
8899 // Print the starting squiggly bracket (if this is the
8900 // first member) or comma (for member 2 and beyond) for
8901 // the struct/union/class member.
8902 if (child_idx == 0)
8903 s->PutChar('{');
8904 else
8905 s->PutChar(',');
8906
8907 // Indent
8908 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8909
8910 clang::QualType field_type = field->getType();
8911 // Print the member type if requested
8912 // Figure out the type byte size (field_type_info.first) and
8913 // alignment (field_type_info.second) from the AST context.
8914 clang::TypeInfo field_type_info =
8915 getASTContext()->getTypeInfo(field_type);
8916 assert(field_idx < record_layout.getFieldCount());
8917 // Figure out the field offset within the current struct/union/class
8918 // type
8919 field_bit_offset = record_layout.getFieldOffset(field_idx);
8920 field_byte_offset = field_bit_offset / 8;
8921 uint32_t field_bitfield_bit_size = 0;
8922 uint32_t field_bitfield_bit_offset = 0;
8923 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
8924 field_bitfield_bit_size))
8925 field_bitfield_bit_offset = field_bit_offset % 8;
8926
8927 if (show_types) {
8928 std::string field_type_name(field_type.getAsString());
8929 if (field_bitfield_bit_size > 0)
8930 s->Printf("(%s:%u) ", field_type_name.c_str(),
8931 field_bitfield_bit_size);
8932 else
8933 s->Printf("(%s) ", field_type_name.c_str());
8934 }
8935 // Print the member name and equal sign
8936 s->Printf("%s = ", field->getNameAsString().c_str());
8937
8938 // Dump the value of the member
8939 CompilerType field_clang_type(getASTContext(), field_type);
8940 field_clang_type.DumpValue(
8941 exe_ctx,
8942 s, // Stream to dump to
8943 field_clang_type
8944 .GetFormat(), // The format with which to display the member
8945 data, // Data buffer containing all bytes for this type
8946 data_byte_offset + field_byte_offset, // Offset into "data" where to
8947 // grab value from
8948 field_type_info.Width / 8, // Size of this type in bytes
8949 field_bitfield_bit_size, // Bitfield bit size
8950 field_bitfield_bit_offset, // Bitfield bit offset
8951 show_types, // Boolean indicating if we should show the variable
8952 // types
8953 show_summary, // Boolean indicating if we should show a summary for
8954 // the current type
8955 verbose, // Verbose output?
8956 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8957 // children
8958 }
8959
8960 // Indent the trailing squiggly bracket
8961 if (child_idx > 0)
8962 s->Printf("\n%*s}", depth, "");
8963 }
8964 return;
8965
8966 case clang::Type::Enum:
8967 if (GetCompleteType(type)) {
8968 const clang::EnumType *enutype =
8969 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8970 const clang::EnumDecl *enum_decl = enutype->getDecl();
8971 assert(enum_decl);
8972 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8973 lldb::offset_t offset = data_byte_offset;
8974 const int64_t enum_value = data.GetMaxU64Bitfield(
8975 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
8976 for (enum_pos = enum_decl->enumerator_begin(),
8977 enum_end_pos = enum_decl->enumerator_end();
8978 enum_pos != enum_end_pos; ++enum_pos) {
8979 if (enum_pos->getInitVal() == enum_value) {
8980 s->Printf("%s", enum_pos->getNameAsString().c_str());
8981 return;
8982 }
8983 }
8984 // If we have gotten here we didn't get find the enumerator in the
8985 // enum decl, so just print the integer.
8986 s->Printf("%" PRIi64, enum_value);
8987 }
8988 return;
8989
8990 case clang::Type::ConstantArray: {
8991 const clang::ConstantArrayType *array =
8992 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
8993 bool is_array_of_characters = false;
8994 clang::QualType element_qual_type = array->getElementType();
8995
8996 const clang::Type *canonical_type =
8997 element_qual_type->getCanonicalTypeInternal().getTypePtr();
8998 if (canonical_type)
8999 is_array_of_characters = canonical_type->isCharType();
9000
9001 const uint64_t element_count = array->getSize().getLimitedValue();
9002
9003 clang::TypeInfo field_type_info =
9004 getASTContext()->getTypeInfo(element_qual_type);
9005
9006 uint32_t element_idx = 0;
9007 uint32_t element_offset = 0;
9008 uint64_t element_byte_size = field_type_info.Width / 8;
9009 uint32_t element_stride = element_byte_size;
9010
9011 if (is_array_of_characters) {
9012 s->PutChar('"');
9013 data.Dump(s, data_byte_offset, lldb::eFormatChar, element_byte_size,
9014 element_count, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
9015 s->PutChar('"');
9016 return;
9017 } else {
9018 CompilerType element_clang_type(getASTContext(), element_qual_type);
9019 lldb::Format element_format = element_clang_type.GetFormat();
9020
9021 for (element_idx = 0; element_idx < element_count; ++element_idx) {
9022 // Print the starting squiggly bracket (if this is the
9023 // first member) or comman (for member 2 and beyong) for
9024 // the struct/union/class member.
9025 if (element_idx == 0)
9026 s->PutChar('{');
9027 else
9028 s->PutChar(',');
9029
9030 // Indent and print the index
9031 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9032
9033 // Figure out the field offset within the current struct/union/class
9034 // type
9035 element_offset = element_idx * element_stride;
9036
9037 // Dump the value of the member
9038 element_clang_type.DumpValue(
9039 exe_ctx,
9040 s, // Stream to dump to
9041 element_format, // The format with which to display the element
9042 data, // Data buffer containing all bytes for this type
9043 data_byte_offset +
9044 element_offset, // Offset into "data" where to grab value from
9045 element_byte_size, // Size of this type in bytes
9046 0, // Bitfield bit size
9047 0, // Bitfield bit offset
9048 show_types, // Boolean indicating if we should show the variable
9049 // types
9050 show_summary, // Boolean indicating if we should show a summary for
9051 // the current type
9052 verbose, // Verbose output?
9053 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9054 // children
9055 }
9056
9057 // Indent the trailing squiggly bracket
9058 if (element_idx > 0)
9059 s->Printf("\n%*s}", depth, "");
9060 }
9061 }
9062 return;
9063
9064 case clang::Type::Typedef: {
9065 clang::QualType typedef_qual_type =
9066 llvm::cast<clang::TypedefType>(qual_type)
9067 ->getDecl()
9068 ->getUnderlyingType();
9069
9070 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9071 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9072 clang::TypeInfo typedef_type_info =
9073 getASTContext()->getTypeInfo(typedef_qual_type);
9074 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9075
9076 return typedef_clang_type.DumpValue(
9077 exe_ctx,
9078 s, // Stream to dump to
9079 typedef_format, // The format with which to display the element
9080 data, // Data buffer containing all bytes for this type
9081 data_byte_offset, // Offset into "data" where to grab value from
9082 typedef_byte_size, // Size of this type in bytes
9083 bitfield_bit_size, // Bitfield bit size
9084 bitfield_bit_offset, // Bitfield bit offset
9085 show_types, // Boolean indicating if we should show the variable types
9086 show_summary, // Boolean indicating if we should show a summary for the
9087 // current type
9088 verbose, // Verbose output?
9089 depth); // Scope depth for any types that have children
9090 } break;
9091
9092 case clang::Type::Auto: {
9093 clang::QualType elaborated_qual_type =
9094 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9095 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9096 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9097 clang::TypeInfo elaborated_type_info =
9098 getASTContext()->getTypeInfo(elaborated_qual_type);
9099 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9100
9101 return elaborated_clang_type.DumpValue(
9102 exe_ctx,
9103 s, // Stream to dump to
9104 elaborated_format, // The format with which to display the element
9105 data, // Data buffer containing all bytes for this type
9106 data_byte_offset, // Offset into "data" where to grab value from
9107 elaborated_byte_size, // Size of this type in bytes
9108 bitfield_bit_size, // Bitfield bit size
9109 bitfield_bit_offset, // Bitfield bit offset
9110 show_types, // Boolean indicating if we should show the variable types
9111 show_summary, // Boolean indicating if we should show a summary for the
9112 // current type
9113 verbose, // Verbose output?
9114 depth); // Scope depth for any types that have children
9115 } break;
9116
9117 case clang::Type::Elaborated: {
9118 clang::QualType elaborated_qual_type =
9119 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9120 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9121 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9122 clang::TypeInfo elaborated_type_info =
9123 getASTContext()->getTypeInfo(elaborated_qual_type);
9124 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9125
9126 return elaborated_clang_type.DumpValue(
9127 exe_ctx,
9128 s, // Stream to dump to
9129 elaborated_format, // The format with which to display the element
9130 data, // Data buffer containing all bytes for this type
9131 data_byte_offset, // Offset into "data" where to grab value from
9132 elaborated_byte_size, // Size of this type in bytes
9133 bitfield_bit_size, // Bitfield bit size
9134 bitfield_bit_offset, // Bitfield bit offset
9135 show_types, // Boolean indicating if we should show the variable types
9136 show_summary, // Boolean indicating if we should show a summary for the
9137 // current type
9138 verbose, // Verbose output?
9139 depth); // Scope depth for any types that have children
9140 } break;
9141
9142 case clang::Type::Paren: {
9143 clang::QualType desugar_qual_type =
9144 llvm::cast<clang::ParenType>(qual_type)->desugar();
9145 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9146
9147 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9148 clang::TypeInfo desugar_type_info =
9149 getASTContext()->getTypeInfo(desugar_qual_type);
9150 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9151
9152 return desugar_clang_type.DumpValue(
9153 exe_ctx,
9154 s, // Stream to dump to
9155 desugar_format, // The format with which to display the element
9156 data, // Data buffer containing all bytes for this type
9157 data_byte_offset, // Offset into "data" where to grab value from
9158 desugar_byte_size, // Size of this type in bytes
9159 bitfield_bit_size, // Bitfield bit size
9160 bitfield_bit_offset, // Bitfield bit offset
9161 show_types, // Boolean indicating if we should show the variable types
9162 show_summary, // Boolean indicating if we should show a summary for the
9163 // current type
9164 verbose, // Verbose output?
9165 depth); // Scope depth for any types that have children
9166 } break;
9167
9168 default:
9169 // We are down to a scalar type that we just need to display.
9170 data.Dump(s, data_byte_offset, format, data_byte_size, 1, UINT32_MAX,
9171 LLDB_INVALID_ADDRESS, bitfield_bit_size, bitfield_bit_offset);
9172
9173 if (show_summary)
9174 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9175 break;
9176 }
9177}
9178
9179bool ClangASTContext::DumpTypeValue(
9180 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
9181 const lldb_private::DataExtractor &data, lldb::offset_t byte_offset,
9182 size_t byte_size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
9183 ExecutionContextScope *exe_scope) {
9184 if (!type)
9185 return false;
9186 if (IsAggregateType(type)) {
9187 return false;
9188 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009189 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009190
9191 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9192 switch (type_class) {
9193 case clang::Type::Typedef: {
9194 clang::QualType typedef_qual_type =
9195 llvm::cast<clang::TypedefType>(qual_type)
9196 ->getDecl()
9197 ->getUnderlyingType();
9198 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9199 if (format == eFormatDefault)
9200 format = typedef_clang_type.GetFormat();
9201 clang::TypeInfo typedef_type_info =
9202 getASTContext()->getTypeInfo(typedef_qual_type);
9203 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9204
9205 return typedef_clang_type.DumpTypeValue(
9206 s,
9207 format, // The format with which to display the element
9208 data, // Data buffer containing all bytes for this type
9209 byte_offset, // Offset into "data" where to grab value from
9210 typedef_byte_size, // Size of this type in bytes
9211 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9212 // treat as a bitfield
9213 bitfield_bit_offset, // Offset in bits of a bitfield value if
9214 // bitfield_bit_size != 0
9215 exe_scope);
9216 } break;
9217
9218 case clang::Type::Enum:
9219 // If our format is enum or default, show the enumeration value as
9220 // its enumeration string value, else just display it as requested.
9221 if ((format == eFormatEnum || format == eFormatDefault) &&
9222 GetCompleteType(type)) {
9223 const clang::EnumType *enutype =
9224 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9225 const clang::EnumDecl *enum_decl = enutype->getDecl();
9226 assert(enum_decl);
9227 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9228 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9229 lldb::offset_t offset = byte_offset;
9230 if (is_signed) {
9231 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9232 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9233 for (enum_pos = enum_decl->enumerator_begin(),
9234 enum_end_pos = enum_decl->enumerator_end();
9235 enum_pos != enum_end_pos; ++enum_pos) {
9236 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
9237 s->PutCString(enum_pos->getNameAsString().c_str());
9238 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009239 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009240 }
9241 // If we have gotten here we didn't get find the enumerator in the
9242 // enum decl, so just print the integer.
9243 s->Printf("%" PRIi64, enum_svalue);
9244 } else {
9245 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9246 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9247 for (enum_pos = enum_decl->enumerator_begin(),
9248 enum_end_pos = enum_decl->enumerator_end();
9249 enum_pos != enum_end_pos; ++enum_pos) {
9250 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
9251 s->PutCString(enum_pos->getNameAsString().c_str());
9252 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009253 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009254 }
9255 // If we have gotten here we didn't get find the enumerator in the
9256 // enum decl, so just print the integer.
9257 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009258 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009259 return true;
9260 }
9261 // format was not enum, just fall through and dump the value as
9262 // requested....
9263 LLVM_FALLTHROUGH;
9264
9265 default:
9266 // We are down to a scalar type that we just need to display.
9267 {
9268 uint32_t item_count = 1;
9269 // A few formats, we might need to modify our size and count for
9270 // depending
9271 // on how we are trying to display the value...
9272 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009273 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009274 case eFormatBoolean:
9275 case eFormatBinary:
9276 case eFormatComplex:
9277 case eFormatCString: // NULL terminated C strings
9278 case eFormatDecimal:
9279 case eFormatEnum:
9280 case eFormatHex:
9281 case eFormatHexUppercase:
9282 case eFormatFloat:
9283 case eFormatOctal:
9284 case eFormatOSType:
9285 case eFormatUnsigned:
9286 case eFormatPointer:
9287 case eFormatVectorOfChar:
9288 case eFormatVectorOfSInt8:
9289 case eFormatVectorOfUInt8:
9290 case eFormatVectorOfSInt16:
9291 case eFormatVectorOfUInt16:
9292 case eFormatVectorOfSInt32:
9293 case eFormatVectorOfUInt32:
9294 case eFormatVectorOfSInt64:
9295 case eFormatVectorOfUInt64:
9296 case eFormatVectorOfFloat32:
9297 case eFormatVectorOfFloat64:
9298 case eFormatVectorOfUInt128:
9299 break;
9300
9301 case eFormatChar:
9302 case eFormatCharPrintable:
9303 case eFormatCharArray:
9304 case eFormatBytes:
9305 case eFormatBytesWithASCII:
9306 item_count = byte_size;
9307 byte_size = 1;
9308 break;
9309
9310 case eFormatUnicode16:
9311 item_count = byte_size / 2;
9312 byte_size = 2;
9313 break;
9314
9315 case eFormatUnicode32:
9316 item_count = byte_size / 4;
9317 byte_size = 4;
9318 break;
9319 }
9320 return data.Dump(s, byte_offset, format, byte_size, item_count,
9321 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9322 bitfield_bit_offset, exe_scope);
9323 }
9324 break;
9325 }
9326 }
9327 return 0;
9328}
9329
9330void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9331 ExecutionContext *exe_ctx, Stream *s,
9332 const lldb_private::DataExtractor &data,
9333 lldb::offset_t data_byte_offset,
9334 size_t data_byte_size) {
9335 uint32_t length = 0;
9336 if (IsCStringType(type, length)) {
9337 if (exe_ctx) {
9338 Process *process = exe_ctx->GetProcessPtr();
9339 if (process) {
9340 lldb::offset_t offset = data_byte_offset;
9341 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9342 std::vector<uint8_t> buf;
9343 if (length > 0)
9344 buf.resize(length);
9345 else
9346 buf.resize(256);
9347
9348 lldb_private::DataExtractor cstr_data(&buf.front(), buf.size(),
9349 process->GetByteOrder(), 4);
9350 buf.back() = '\0';
9351 size_t bytes_read;
9352 size_t total_cstr_len = 0;
9353 Error error;
9354 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9355 buf.size(), error)) > 0) {
9356 const size_t len = strlen((const char *)&buf.front());
9357 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009358 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009359 if (total_cstr_len == 0)
9360 s->PutCString(" \"");
9361 cstr_data.Dump(s, 0, lldb::eFormatChar, 1, len, UINT32_MAX,
9362 LLDB_INVALID_ADDRESS, 0, 0);
9363 total_cstr_len += len;
9364 if (len < buf.size())
9365 break;
9366 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009367 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009368 if (total_cstr_len > 0)
9369 s->PutChar('"');
9370 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009371 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009372 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009373}
9374
Kate Stoneb9c1b512016-09-06 20:57:50 +00009375void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9376 StreamFile s(stdout, false);
9377 DumpTypeDescription(type, &s);
9378 ClangASTMetadata *metadata =
9379 ClangASTContext::GetMetadata(getASTContext(), type);
9380 if (metadata) {
9381 metadata->Dump(&s);
9382 }
9383}
Greg Claytond8d4a572015-08-11 21:38:15 +00009384
Kate Stoneb9c1b512016-09-06 20:57:50 +00009385void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9386 Stream *s) {
9387 if (type) {
9388 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009389
Kate Stoneb9c1b512016-09-06 20:57:50 +00009390 llvm::SmallVector<char, 1024> buf;
9391 llvm::raw_svector_ostream llvm_ostrm(buf);
9392
9393 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9394 switch (type_class) {
9395 case clang::Type::ObjCObject:
9396 case clang::Type::ObjCInterface: {
9397 GetCompleteType(type);
9398
9399 const clang::ObjCObjectType *objc_class_type =
9400 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9401 assert(objc_class_type);
9402 if (objc_class_type) {
9403 clang::ObjCInterfaceDecl *class_interface_decl =
9404 objc_class_type->getInterface();
9405 if (class_interface_decl) {
9406 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9407 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009408 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009409 }
9410 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009411
Kate Stoneb9c1b512016-09-06 20:57:50 +00009412 case clang::Type::Typedef: {
9413 const clang::TypedefType *typedef_type =
9414 qual_type->getAs<clang::TypedefType>();
9415 if (typedef_type) {
9416 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9417 std::string clang_typedef_name(
9418 typedef_decl->getQualifiedNameAsString());
9419 if (!clang_typedef_name.empty()) {
9420 s->PutCString("typedef ");
9421 s->PutCString(clang_typedef_name.c_str());
Greg Claytond8d4a572015-08-11 21:38:15 +00009422 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009423 }
9424 } break;
9425
9426 case clang::Type::Auto:
9427 CompilerType(getASTContext(),
9428 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9429 .DumpTypeDescription(s);
9430 return;
9431
9432 case clang::Type::Elaborated:
9433 CompilerType(getASTContext(),
9434 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9435 .DumpTypeDescription(s);
9436 return;
9437
9438 case clang::Type::Paren:
9439 CompilerType(getASTContext(),
9440 llvm::cast<clang::ParenType>(qual_type)->desugar())
9441 .DumpTypeDescription(s);
9442 return;
9443
9444 case clang::Type::Record: {
9445 GetCompleteType(type);
9446
9447 const clang::RecordType *record_type =
9448 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9449 const clang::RecordDecl *record_decl = record_type->getDecl();
9450 const clang::CXXRecordDecl *cxx_record_decl =
9451 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9452
9453 if (cxx_record_decl)
9454 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9455 s->GetIndentLevel());
9456 else
9457 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9458 s->GetIndentLevel());
9459 } break;
9460
9461 default: {
9462 const clang::TagType *tag_type =
9463 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9464 if (tag_type) {
9465 clang::TagDecl *tag_decl = tag_type->getDecl();
9466 if (tag_decl)
9467 tag_decl->print(llvm_ostrm, 0);
9468 } else {
9469 std::string clang_type_name(qual_type.getAsString());
9470 if (!clang_type_name.empty())
9471 s->PutCString(clang_type_name.c_str());
9472 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009473 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009474 }
9475
Kate Stoneb9c1b512016-09-06 20:57:50 +00009476 if (buf.size() > 0) {
9477 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009478 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009479 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009480}
9481
Kate Stoneb9c1b512016-09-06 20:57:50 +00009482void ClangASTContext::DumpTypeName(const CompilerType &type) {
9483 if (ClangUtil::IsClangType(type)) {
9484 clang::QualType qual_type(
9485 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9486
9487 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9488 switch (type_class) {
9489 case clang::Type::Record: {
9490 const clang::CXXRecordDecl *cxx_record_decl =
9491 qual_type->getAsCXXRecordDecl();
9492 if (cxx_record_decl)
9493 printf("class %s", cxx_record_decl->getName().str().c_str());
9494 } break;
9495
9496 case clang::Type::Enum: {
9497 clang::EnumDecl *enum_decl =
9498 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9499 if (enum_decl) {
9500 printf("enum %s", enum_decl->getName().str().c_str());
9501 }
9502 } break;
9503
9504 case clang::Type::ObjCObject:
9505 case clang::Type::ObjCInterface: {
9506 const clang::ObjCObjectType *objc_class_type =
9507 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9508 if (objc_class_type) {
9509 clang::ObjCInterfaceDecl *class_interface_decl =
9510 objc_class_type->getInterface();
9511 // We currently can't complete objective C types through the newly added
9512 // ASTContext
9513 // because it only supports TagDecl objects right now...
9514 if (class_interface_decl)
9515 printf("@class %s", class_interface_decl->getName().str().c_str());
9516 }
9517 } break;
9518
9519 case clang::Type::Typedef:
9520 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9521 ->getDecl()
9522 ->getName()
9523 .str()
9524 .c_str());
9525 break;
9526
9527 case clang::Type::Auto:
9528 printf("auto ");
9529 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9530 llvm::cast<clang::AutoType>(qual_type)
9531 ->getDeducedType()
9532 .getAsOpaquePtr()));
9533
9534 case clang::Type::Elaborated:
9535 printf("elaborated ");
9536 return DumpTypeName(CompilerType(
9537 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9538 ->getNamedType()
9539 .getAsOpaquePtr()));
9540
9541 case clang::Type::Paren:
9542 printf("paren ");
9543 return DumpTypeName(CompilerType(
9544 type.GetTypeSystem(),
9545 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9546
9547 default:
9548 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9549 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009550 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009551 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009552}
9553
Kate Stoneb9c1b512016-09-06 20:57:50 +00009554clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9555 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9556 const char *parent_name, int tag_decl_kind,
9557 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9558 if (template_param_infos.IsValid()) {
9559 std::string template_basename(parent_name);
9560 template_basename.erase(template_basename.find('<'));
9561
9562 return CreateClassTemplateDecl(decl_ctx, access_type,
9563 template_basename.c_str(), tag_decl_kind,
9564 template_param_infos);
9565 }
9566 return NULL;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009567}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009568
Kate Stoneb9c1b512016-09-06 20:57:50 +00009569void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9570 ClangASTContext *ast = (ClangASTContext *)baton;
9571 SymbolFile *sym_file = ast->GetSymbolFile();
9572 if (sym_file) {
9573 CompilerType clang_type = GetTypeForDecl(decl);
9574 if (clang_type)
9575 sym_file->CompleteType(clang_type);
9576 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009577}
9578
Kate Stoneb9c1b512016-09-06 20:57:50 +00009579void ClangASTContext::CompleteObjCInterfaceDecl(
9580 void *baton, clang::ObjCInterfaceDecl *decl) {
9581 ClangASTContext *ast = (ClangASTContext *)baton;
9582 SymbolFile *sym_file = ast->GetSymbolFile();
9583 if (sym_file) {
9584 CompilerType clang_type = GetTypeForDecl(decl);
9585 if (clang_type)
9586 sym_file->CompleteType(clang_type);
9587 }
Zachary Turner42dff792016-04-15 00:21:26 +00009588}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009589
Kate Stoneb9c1b512016-09-06 20:57:50 +00009590DWARFASTParser *ClangASTContext::GetDWARFParser() {
9591 if (!m_dwarf_ast_parser_ap)
9592 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9593 return m_dwarf_ast_parser_ap.get();
9594}
9595
9596PDBASTParser *ClangASTContext::GetPDBParser() {
9597 if (!m_pdb_ast_parser_ap)
9598 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9599 return m_pdb_ast_parser_ap.get();
9600}
9601
9602bool ClangASTContext::LayoutRecordType(
9603 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9604 uint64_t &alignment,
9605 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9606 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9607 &base_offsets,
9608 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9609 &vbase_offsets) {
9610 ClangASTContext *ast = (ClangASTContext *)baton;
9611 DWARFASTParserClang *dwarf_ast_parser =
9612 (DWARFASTParserClang *)ast->GetDWARFParser();
9613 return dwarf_ast_parser->GetClangASTImporter().LayoutRecordType(
9614 record_decl, bit_size, alignment, field_offsets, base_offsets,
9615 vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009616}
9617
Greg Clayton99558cc42015-08-24 23:46:31 +00009618//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009619// CompilerDecl override functions
9620//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009621
Kate Stoneb9c1b512016-09-06 20:57:50 +00009622ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9623 if (opaque_decl) {
9624 clang::NamedDecl *nd =
9625 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9626 if (nd != nullptr)
9627 return ConstString(nd->getDeclName().getAsString());
9628 }
9629 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009630}
9631
Kate Stoneb9c1b512016-09-06 20:57:50 +00009632ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9633 if (opaque_decl) {
9634 clang::NamedDecl *nd =
9635 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9636 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9637 clang::MangleContext *mc = getMangleContext();
9638 if (mc && mc->shouldMangleCXXName(nd)) {
9639 llvm::SmallVector<char, 1024> buf;
9640 llvm::raw_svector_ostream llvm_ostrm(buf);
9641 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9642 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9643 Ctor_Complete, llvm_ostrm);
9644 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9645 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9646 Dtor_Complete, llvm_ostrm);
9647 } else {
9648 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009649 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009650 if (buf.size() > 0)
9651 return ConstString(buf.data(), buf.size());
9652 }
Greg Claytonfe689042015-11-10 17:47:04 +00009653 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009654 }
9655 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009656}
9657
Kate Stoneb9c1b512016-09-06 20:57:50 +00009658CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9659 if (opaque_decl)
9660 return CompilerDeclContext(this,
9661 ((clang::Decl *)opaque_decl)->getDeclContext());
9662 else
9663 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009664}
9665
Kate Stoneb9c1b512016-09-06 20:57:50 +00009666CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9667 if (clang::FunctionDecl *func_decl =
9668 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9669 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9670 if (clang::ObjCMethodDecl *objc_method =
9671 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9672 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9673 else
Greg Claytonfe689042015-11-10 17:47:04 +00009674 return CompilerType();
9675}
9676
Kate Stoneb9c1b512016-09-06 20:57:50 +00009677size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9678 if (clang::FunctionDecl *func_decl =
9679 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9680 return func_decl->param_size();
9681 if (clang::ObjCMethodDecl *objc_method =
9682 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9683 return objc_method->param_size();
9684 else
9685 return 0;
9686}
9687
9688CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9689 size_t idx) {
9690 if (clang::FunctionDecl *func_decl =
9691 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9692 if (idx < func_decl->param_size()) {
9693 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9694 if (var_decl)
9695 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9696 }
9697 } else if (clang::ObjCMethodDecl *objc_method =
9698 llvm::dyn_cast<clang::ObjCMethodDecl>(
9699 (clang::Decl *)opaque_decl)) {
9700 if (idx < objc_method->param_size())
9701 return CompilerType(
9702 this,
9703 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9704 }
9705 return CompilerType();
9706}
9707
Paul Hermand628cbb2015-09-15 23:44:17 +00009708//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00009709// CompilerDeclContext functions
9710//----------------------------------------------------------------------
9711
Kate Stoneb9c1b512016-09-06 20:57:50 +00009712std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9713 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9714 std::vector<CompilerDecl> found_decls;
9715 if (opaque_decl_ctx) {
9716 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9717 std::set<DeclContext *> searched;
9718 std::multimap<DeclContext *, DeclContext *> search_queue;
9719 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009720
Kate Stoneb9c1b512016-09-06 20:57:50 +00009721 for (clang::DeclContext *decl_context = root_decl_ctx;
9722 decl_context != nullptr && found_decls.empty();
9723 decl_context = decl_context->getParent()) {
9724 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +00009725
Kate Stoneb9c1b512016-09-06 20:57:50 +00009726 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9727 it++) {
9728 if (!searched.insert(it->second).second)
9729 continue;
9730 symbol_file->ParseDeclsForContext(
9731 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +00009732
Kate Stoneb9c1b512016-09-06 20:57:50 +00009733 for (clang::Decl *child : it->second->decls()) {
9734 if (clang::UsingDirectiveDecl *ud =
9735 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9736 if (ignore_using_decls)
9737 continue;
9738 clang::DeclContext *from = ud->getCommonAncestor();
9739 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9740 search_queue.insert(
9741 std::make_pair(from, ud->getNominatedNamespace()));
9742 } else if (clang::UsingDecl *ud =
9743 llvm::dyn_cast<clang::UsingDecl>(child)) {
9744 if (ignore_using_decls)
9745 continue;
9746 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9747 clang::Decl *target = usd->getTargetDecl();
9748 if (clang::NamedDecl *nd =
9749 llvm::dyn_cast<clang::NamedDecl>(target)) {
9750 IdentifierInfo *ii = nd->getIdentifier();
9751 if (ii != nullptr &&
9752 ii->getName().equals(name.AsCString(nullptr)))
9753 found_decls.push_back(CompilerDecl(this, nd));
9754 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009755 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009756 } else if (clang::NamedDecl *nd =
9757 llvm::dyn_cast<clang::NamedDecl>(child)) {
9758 IdentifierInfo *ii = nd->getIdentifier();
9759 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9760 found_decls.push_back(CompilerDecl(this, nd));
9761 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009762 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009763 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009764 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009765 }
9766 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +00009767}
9768
Dawn Perchikb5925782015-12-12 19:31:41 +00009769// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009770// and return the number of levels it took to find it, or
9771// LLDB_INVALID_DECL_LEVEL
9772// if not found. If the decl was imported via a using declaration, its name
9773// and/or
9774// type, if set, will be used to check that the decl found in the scope is a
9775// match.
Dawn Perchikb5925782015-12-12 19:31:41 +00009776//
Kate Stoneb9c1b512016-09-06 20:57:50 +00009777// The optional name is required by languages (like C++) to handle using
9778// declarations
Dawn Perchikb5925782015-12-12 19:31:41 +00009779// like:
9780//
9781// void poo();
9782// namespace ns {
9783// void foo();
9784// void goo();
9785// }
9786// void bar() {
9787// using ns::foo;
9788// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
9789// // LLDB_INVALID_DECL_LEVEL for 'goo'.
9790// }
9791//
9792// The optional type is useful in the case that there's a specific overload
9793// that we're looking for that might otherwise be shadowed, like:
9794//
9795// void foo(int);
9796// namespace ns {
9797// void foo();
9798// }
9799// void bar() {
9800// using ns::foo;
9801// // CountDeclLevels returns 0 for { 'foo', void() },
9802// // 1 for { 'foo', void(int) }, and
9803// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
9804// }
9805//
9806// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +00009807// function at file scope will return the same level as a function at global
9808// scope.
9809// Ideally we'd like to treat the file scope as an additional scope just below
9810// the
9811// global scope. More work needs to be done to recognise that, if the decl
9812// we're
9813// trying to look up is static, we should compare its source file with that of
9814// the
Dawn Perchikb5925782015-12-12 19:31:41 +00009815// current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009816uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
9817 clang::DeclContext *child_decl_ctx,
9818 ConstString *child_name,
9819 CompilerType *child_type) {
9820 if (frame_decl_ctx) {
9821 std::set<DeclContext *> searched;
9822 std::multimap<DeclContext *, DeclContext *> search_queue;
9823 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +00009824
Kate Stoneb9c1b512016-09-06 20:57:50 +00009825 // Get the lookup scope for the decl we're trying to find.
9826 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +00009827
Kate Stoneb9c1b512016-09-06 20:57:50 +00009828 // Look for it in our scope's decl context and its parents.
9829 uint32_t level = 0;
9830 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
9831 decl_ctx = decl_ctx->getParent()) {
9832 if (!decl_ctx->isLookupContext())
9833 continue;
9834 if (decl_ctx == parent_decl_ctx)
9835 // Found it!
9836 return level;
9837 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
9838 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
9839 it++) {
9840 if (searched.find(it->second) != searched.end())
9841 continue;
9842
9843 // Currently DWARF has one shared translation unit for all Decls at top
9844 // level, so this
9845 // would erroneously find using statements anywhere. So don't look at
9846 // the top-level
9847 // translation unit.
9848 // TODO fix this and add a testcase that depends on it.
9849
9850 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
9851 continue;
9852
9853 searched.insert(it->second);
9854 symbol_file->ParseDeclsForContext(
9855 CompilerDeclContext(this, it->second));
9856
9857 for (clang::Decl *child : it->second->decls()) {
9858 if (clang::UsingDirectiveDecl *ud =
9859 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9860 clang::DeclContext *ns = ud->getNominatedNamespace();
9861 if (ns == parent_decl_ctx)
9862 // Found it!
9863 return level;
9864 clang::DeclContext *from = ud->getCommonAncestor();
9865 if (searched.find(ns) == searched.end())
9866 search_queue.insert(std::make_pair(from, ns));
9867 } else if (child_name) {
9868 if (clang::UsingDecl *ud =
9869 llvm::dyn_cast<clang::UsingDecl>(child)) {
9870 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9871 clang::Decl *target = usd->getTargetDecl();
9872 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
9873 if (!nd)
9874 continue;
9875 // Check names.
9876 IdentifierInfo *ii = nd->getIdentifier();
9877 if (ii == nullptr ||
9878 !ii->getName().equals(child_name->AsCString(nullptr)))
9879 continue;
9880 // Check types, if one was provided.
9881 if (child_type) {
9882 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
9883 if (!AreTypesSame(clang_type, *child_type,
9884 /*ignore_qualifiers=*/true))
9885 continue;
9886 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009887 // Found it!
9888 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009889 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009890 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009891 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009892 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009893 }
9894 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +00009895 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009896 }
9897 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +00009898}
9899
Kate Stoneb9c1b512016-09-06 20:57:50 +00009900bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
9901 if (opaque_decl_ctx)
9902 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
9903 else
Greg Clayton99558cc42015-08-24 23:46:31 +00009904 return false;
9905}
9906
Kate Stoneb9c1b512016-09-06 20:57:50 +00009907ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
9908 if (opaque_decl_ctx) {
9909 clang::NamedDecl *named_decl =
9910 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9911 if (named_decl)
9912 return ConstString(named_decl->getName());
9913 }
9914 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +00009915}
9916
Kate Stoneb9c1b512016-09-06 20:57:50 +00009917ConstString
9918ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
9919 if (opaque_decl_ctx) {
9920 clang::NamedDecl *named_decl =
9921 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9922 if (named_decl)
9923 return ConstString(
9924 llvm::StringRef(named_decl->getQualifiedNameAsString()));
9925 }
9926 return ConstString();
9927}
9928
9929bool ClangASTContext::DeclContextIsClassMethod(
9930 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
9931 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
9932 if (opaque_decl_ctx) {
9933 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9934 if (ObjCMethodDecl *objc_method =
9935 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
9936 if (is_instance_method_ptr)
9937 *is_instance_method_ptr = objc_method->isInstanceMethod();
9938 if (language_ptr)
9939 *language_ptr = eLanguageTypeObjC;
9940 if (language_object_name_ptr)
9941 language_object_name_ptr->SetCString("self");
9942 return true;
9943 } else if (CXXMethodDecl *cxx_method =
9944 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
9945 if (is_instance_method_ptr)
9946 *is_instance_method_ptr = cxx_method->isInstance();
9947 if (language_ptr)
9948 *language_ptr = eLanguageTypeC_plus_plus;
9949 if (language_object_name_ptr)
9950 language_object_name_ptr->SetCString("this");
9951 return true;
9952 } else if (clang::FunctionDecl *function_decl =
9953 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
9954 ClangASTMetadata *metadata =
9955 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
9956 if (metadata && metadata->HasObjectPtr()) {
9957 if (is_instance_method_ptr)
9958 *is_instance_method_ptr = true;
9959 if (language_ptr)
9960 *language_ptr = eLanguageTypeObjC;
9961 if (language_object_name_ptr)
9962 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
9963 return true;
9964 }
9965 }
9966 }
9967 return false;
9968}
9969
9970clang::DeclContext *
9971ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
9972 if (dc.IsClang())
9973 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
9974 return nullptr;
9975}
Greg Clayton99558cc42015-08-24 23:46:31 +00009976
9977ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00009978ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
9979 if (dc.IsClang())
9980 return llvm::dyn_cast<clang::ObjCMethodDecl>(
9981 (clang::DeclContext *)dc.GetOpaqueDeclContext());
9982 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +00009983}
9984
9985CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00009986ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
9987 if (dc.IsClang())
9988 return llvm::dyn_cast<clang::CXXMethodDecl>(
9989 (clang::DeclContext *)dc.GetOpaqueDeclContext());
9990 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +00009991}
9992
9993clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00009994ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
9995 if (dc.IsClang())
9996 return llvm::dyn_cast<clang::FunctionDecl>(
9997 (clang::DeclContext *)dc.GetOpaqueDeclContext());
9998 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +00009999}
10000
10001clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010002ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10003 if (dc.IsClang())
10004 return llvm::dyn_cast<clang::NamespaceDecl>(
10005 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10006 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010007}
10008
10009ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010010ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10011 const void *object) {
10012 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10013 if (ast)
10014 return ClangASTContext::GetMetadata(ast, object);
10015 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010016}
10017
10018clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010019ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10020 ClangASTContext *ast =
10021 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10022 if (ast)
10023 return ast->getASTContext();
10024 return nullptr;
10025}
10026
10027ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10028 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10029 m_target_wp(target.shared_from_this()),
10030 m_persistent_variables(new ClangPersistentVariables) {}
10031
10032UserExpression *ClangASTContextForExpressions::GetUserExpression(
10033 const char *expr, const char *expr_prefix, lldb::LanguageType language,
10034 Expression::ResultType desired_type,
10035 const EvaluateExpressionOptions &options) {
10036 TargetSP target_sp = m_target_wp.lock();
10037 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010038 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010039
10040 return new ClangUserExpression(*target_sp.get(), expr, expr_prefix, language,
10041 desired_type, options);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010042}
10043
Kate Stoneb9c1b512016-09-06 20:57:50 +000010044FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10045 const CompilerType &return_type, const Address &function_address,
10046 const ValueList &arg_value_list, const char *name) {
10047 TargetSP target_sp = m_target_wp.lock();
10048 if (!target_sp)
10049 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010050
Kate Stoneb9c1b512016-09-06 20:57:50 +000010051 Process *process = target_sp->GetProcessSP().get();
10052 if (!process)
10053 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010054
Kate Stoneb9c1b512016-09-06 20:57:50 +000010055 return new ClangFunctionCaller(*process, return_type, function_address,
10056 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010057}
10058
10059UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010060ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10061 const char *name) {
10062 TargetSP target_sp = m_target_wp.lock();
10063 if (!target_sp)
10064 return nullptr;
10065
10066 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010067}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010068
10069PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010070ClangASTContextForExpressions::GetPersistentExpressionState() {
10071 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010072}