blob: 418c266ff9f162fe82006486f94fac91f1684cf2 [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>
16
17// Other libraries and framework includes
Greg Clayton6beaaa62011-01-17 03:46:26 +000018
19// Clang headers like to use NDEBUG inside of them to enable/disable debug
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000020// related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
Greg Clayton6beaaa62011-01-17 03:46:26 +000021// or another. This is bad because it means that if clang was built in release
22// mode, it assumes that you are building in release mode which is not always
23// the case. You can end up with functions that are defined as empty in header
24// files when NDEBUG is not defined, and this can cause link errors with the
25// clang .a files that you have since you might be missing functions in the .a
26// file. So we have to define NDEBUG when including clang headers to avoid any
27// mismatches. This is covered by rdar://problem/8691220
28
Sean Callanan3b1d4f62011-10-26 17:46:51 +000029#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000030#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000031#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000032// Need to include assert.h so it is as clang would expect it to be (disabled)
33#include <assert.h>
34#endif
35
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "clang/AST/ASTContext.h"
37#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000038#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000040#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000041#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042#include "clang/AST/RecordLayout.h"
43#include "clang/AST/Type.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000044#include "clang/AST/VTableBuilder.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000046#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000048#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049#include "clang/Basic/SourceManager.h"
50#include "clang/Basic/TargetInfo.h"
51#include "clang/Basic/TargetOptions.h"
52#include "clang/Frontend/FrontendOptions.h"
53#include "clang/Frontend/LangStandard.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000054
55#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000056#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000057#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
58// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
59#include <assert.h>
60#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061
Greg Claytond8d4a572015-08-11 21:38:15 +000062#include "llvm/Support/Signals.h"
63
Greg Clayton514487e2011-02-15 21:59:32 +000064#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065#include "lldb/Core/dwarf.h"
Greg Clayton73b472d2010-10-27 03:32:59 +000066#include "lldb/Core/Flags.h"
Sean Callananfb8b7092010-10-28 18:19:36 +000067#include "lldb/Core/Log.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000068#include "lldb/Core/RegularExpression.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000069#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000070#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000071#include "lldb/Core/UniqueCStringMap.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000072#include "lldb/Expression/ASTDumper.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000073#include "lldb/Symbol/ClangASTContext.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000074#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000075#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000076#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000077#include "lldb/Target/ExecutionContext.h"
78#include "lldb/Target/Process.h"
79#include "lldb/Target/ObjCLanguageRuntime.h"
80
Eli Friedman932197d2010-06-13 19:06:42 +000081#include <stdio.h>
82
Greg Clayton1341baf2013-07-11 23:36:31 +000083#include <mutex>
84
Greg Clayton8b4edba2015-08-14 20:02:05 +000085
86//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
87
88#ifdef ENABLE_DEBUG_PRINTF
89#include <stdio.h>
90#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
91#else
92#define DEBUG_PRINTF(fmt, ...)
93#endif
94
Greg Claytonc86103d2010-08-05 01:57:25 +000095using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000096using namespace lldb_private;
97using namespace llvm;
98using namespace clang;
99
Enrico Granata2267ad42014-09-16 17:28:40 +0000100typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext*> ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000101
102static ClangASTMap &
103GetASTMap()
104{
Enrico Granata2267ad42014-09-16 17:28:40 +0000105 static ClangASTMap *g_map_ptr = nullptr;
106 static std::once_flag g_once_flag;
107 std::call_once(g_once_flag, []() {
108 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
109 });
110 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000111}
112
113
Greg Clayton57ee3062013-07-11 22:46:58 +0000114clang::AccessSpecifier
115ClangASTContext::ConvertAccessTypeToAccessSpecifier (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000116{
117 switch (access)
118 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000119 default: break;
120 case eAccessNone: return AS_none;
121 case eAccessPublic: return AS_public;
122 case eAccessPrivate: return AS_private;
123 case eAccessProtected: return AS_protected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000124 }
125 return AS_none;
126}
127
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128static void
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000129ParseLangArgs (LangOptions &Opts, InputKind IK, const char* triple)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130{
131 // FIXME: Cleanup per-file based stuff.
132
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000133 // Set some properties which depend solely on the input kind; it would be nice
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134 // to move these to the language standard, and have the driver resolve the
135 // input kind + language standard.
Greg Clayton94e5d782010-06-13 17:34:29 +0000136 if (IK == IK_Asm) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137 Opts.AsmPreprocessor = 1;
Greg Clayton94e5d782010-06-13 17:34:29 +0000138 } else if (IK == IK_ObjC ||
139 IK == IK_ObjCXX ||
140 IK == IK_PreprocessedObjC ||
141 IK == IK_PreprocessedObjCXX) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142 Opts.ObjC1 = Opts.ObjC2 = 1;
143 }
144
145 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
146
147 if (LangStd == LangStandard::lang_unspecified) {
148 // Based on the base language, pick one.
149 switch (IK) {
Greg Clayton94e5d782010-06-13 17:34:29 +0000150 case IK_None:
151 case IK_AST:
Sean Callananfb0b7582011-03-15 00:17:19 +0000152 case IK_LLVM_IR:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000153 assert (!"Invalid input kind!");
Greg Clayton94e5d782010-06-13 17:34:29 +0000154 case IK_OpenCL:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155 LangStd = LangStandard::lang_opencl;
156 break;
Sean Callananfb0b7582011-03-15 00:17:19 +0000157 case IK_CUDA:
Artem Belevich52210ae2015-03-19 18:12:26 +0000158 case IK_PreprocessedCuda:
Sean Callananfb0b7582011-03-15 00:17:19 +0000159 LangStd = LangStandard::lang_cuda;
160 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000161 case IK_Asm:
162 case IK_C:
163 case IK_PreprocessedC:
164 case IK_ObjC:
165 case IK_PreprocessedObjC:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166 LangStd = LangStandard::lang_gnu99;
167 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000168 case IK_CXX:
169 case IK_PreprocessedCXX:
170 case IK_ObjCXX:
171 case IK_PreprocessedObjCXX:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172 LangStd = LangStandard::lang_gnucxx98;
173 break;
174 }
175 }
176
177 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
Filipe Cabecinhase818ca22012-11-12 21:26:32 +0000178 Opts.LineComment = Std.hasLineComments();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 Opts.C99 = Std.isC99();
180 Opts.CPlusPlus = Std.isCPlusPlus();
Chandler Carruth38336a12013-01-02 12:55:00 +0000181 Opts.CPlusPlus11 = Std.isCPlusPlus11();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182 Opts.Digraphs = Std.hasDigraphs();
183 Opts.GNUMode = Std.isGNUMode();
184 Opts.GNUInline = !Std.isC99();
185 Opts.HexFloats = Std.hasHexFloats();
186 Opts.ImplicitInt = Std.hasImplicitInt();
Enrico Granatac921e342013-01-10 02:37:22 +0000187
188 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189
190 // OpenCL has some additional defaults.
191 if (LangStd == LangStandard::lang_opencl) {
192 Opts.OpenCL = 1;
193 Opts.AltiVec = 1;
194 Opts.CXXOperatorNames = 1;
195 Opts.LaxVectorConversions = 1;
196 }
197
198 // OpenCL and C++ both have bool, true, false keywords.
199 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
200
201// if (Opts.CPlusPlus)
202// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
203//
204// if (Args.hasArg(OPT_fobjc_gc_only))
205// Opts.setGCMode(LangOptions::GCOnly);
206// else if (Args.hasArg(OPT_fobjc_gc))
207// Opts.setGCMode(LangOptions::HybridGC);
208//
209// if (Args.hasArg(OPT_print_ivar_layout))
210// Opts.ObjCGCBitmapPrint = 1;
211//
212// if (Args.hasArg(OPT_faltivec))
213// Opts.AltiVec = 1;
214//
215// if (Args.hasArg(OPT_pthread))
216// Opts.POSIXThreads = 1;
217//
218// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
219// "default");
220// if (Vis == "default")
Sean Callanan37f76e52013-02-19 19:16:37 +0000221 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222// else if (Vis == "hidden")
223// Opts.setVisibilityMode(LangOptions::Hidden);
224// else if (Vis == "protected")
225// Opts.setVisibilityMode(LangOptions::Protected);
226// else
227// Diags.Report(diag::err_drv_invalid_value)
228// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
229
230// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
231
232 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
233 // is specified, or -std is set to a conforming mode.
234 Opts.Trigraphs = !Opts.GNUMode;
235// if (Args.hasArg(OPT_trigraphs))
236// Opts.Trigraphs = 1;
237//
238// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
239// OPT_fno_dollars_in_identifiers,
240// !Opts.AsmPreprocessor);
241// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
242// Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
243// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
244// if (Args.hasArg(OPT_fno_lax_vector_conversions))
245// Opts.LaxVectorConversions = 0;
246// Opts.Exceptions = Args.hasArg(OPT_fexceptions);
247// Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
248// Opts.Blocks = Args.hasArg(OPT_fblocks);
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000249 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
251// Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
252// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
253// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
254// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
255// Opts.AccessControl = Args.hasArg(OPT_faccess_control);
256// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
257// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
258// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
259// Diags);
260// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
261// Opts.ObjCConstantStringClass = getLastArgValue(Args,
262// OPT_fconstant_string_class);
263// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
264// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
265// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
266// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
267// Opts.Static = Args.hasArg(OPT_static_define);
268 Opts.OptimizeSize = 0;
269
270 // FIXME: Eliminate this dependency.
271// unsigned Opt =
272// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
273// Opts.Optimize = Opt != 0;
274 unsigned Opt = 0;
275
276 // This is the __NO_INLINE__ define, which just depends on things like the
277 // optimization level and -fno-inline, not actually whether the backend has
278 // inlining enabled.
279 //
280 // FIXME: This is affected by other options (-fno-inline).
Sean Callanan3d654b32012-09-24 22:25:51 +0000281 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282
283// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
284// switch (SSP) {
285// default:
286// Diags.Report(diag::err_drv_invalid_value)
287// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
288// break;
289// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
290// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
291// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
292// }
293}
294
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295
Greg Clayton6beaaa62011-01-17 03:46:26 +0000296ClangASTContext::ClangASTContext (const char *target_triple) :
Greg Clayton6dc8d582015-08-18 22:32:36 +0000297 m_target_triple (),
298 m_ast_ap (),
299 m_language_options_ap (),
300 m_source_manager_ap (),
301 m_diagnostics_engine_ap (),
302 m_target_options_rp (),
303 m_target_info_ap (),
304 m_identifier_table_ap (),
305 m_selector_table_ap (),
306 m_builtins_ap (),
Ed Masted4612ad2014-04-20 13:17:36 +0000307 m_callback_tag_decl (nullptr),
308 m_callback_objc_decl (nullptr),
309 m_callback_baton (nullptr),
Greg Claytond8d4a572015-08-11 21:38:15 +0000310 m_pointer_byte_size (0),
Greg Clayton6dc8d582015-08-18 22:32:36 +0000311 m_ast_owned (false),
312 m_record_decl_to_layout_map (),
313 m_die_to_decl_ctx (),
314 m_decl_ctx_to_die (),
315 m_clang_tu_decl (nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316{
317 if (target_triple && target_triple[0])
Greg Clayton880cbb02011-07-30 01:26:02 +0000318 SetTargetTriple (target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319}
320
321//----------------------------------------------------------------------
322// Destructor
323//----------------------------------------------------------------------
324ClangASTContext::~ClangASTContext()
325{
Enrico Granata5d84a692014-08-19 21:46:37 +0000326 if (m_ast_ap.get())
327 {
Enrico Granata2267ad42014-09-16 17:28:40 +0000328 GetASTMap().Erase(m_ast_ap.get());
Greg Claytond8d4a572015-08-11 21:38:15 +0000329 if (!m_ast_owned)
330 m_ast_ap.release();
Enrico Granata5d84a692014-08-19 21:46:37 +0000331 }
332
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333 m_builtins_ap.reset();
334 m_selector_table_ap.reset();
335 m_identifier_table_ap.reset();
336 m_target_info_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000337 m_target_options_rp.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000338 m_diagnostics_engine_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339 m_source_manager_ap.reset();
340 m_language_options_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000341 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342}
343
344
345void
346ClangASTContext::Clear()
347{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000348 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349 m_language_options_ap.reset();
350 m_source_manager_ap.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000351 m_diagnostics_engine_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000352 m_target_options_rp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353 m_target_info_ap.reset();
354 m_identifier_table_ap.reset();
355 m_selector_table_ap.reset();
356 m_builtins_ap.reset();
Greg Clayton57ee3062013-07-11 22:46:58 +0000357 m_pointer_byte_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358}
359
360const char *
361ClangASTContext::GetTargetTriple ()
362{
363 return m_target_triple.c_str();
364}
365
366void
367ClangASTContext::SetTargetTriple (const char *target_triple)
368{
369 Clear();
370 m_target_triple.assign(target_triple);
371}
372
Greg Clayton514487e2011-02-15 21:59:32 +0000373void
374ClangASTContext::SetArchitecture (const ArchSpec &arch)
375{
Greg Clayton880cbb02011-07-30 01:26:02 +0000376 SetTargetTriple(arch.GetTriple().str().c_str());
Greg Clayton514487e2011-02-15 21:59:32 +0000377}
378
Greg Clayton6beaaa62011-01-17 03:46:26 +0000379bool
380ClangASTContext::HasExternalSource ()
381{
382 ASTContext *ast = getASTContext();
383 if (ast)
Ed Masted4612ad2014-04-20 13:17:36 +0000384 return ast->getExternalSource () != nullptr;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000385 return false;
386}
387
388void
Todd Fiala955fe6f2014-02-27 17:18:23 +0000389ClangASTContext::SetExternalSource (llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000390{
391 ASTContext *ast = getASTContext();
392 if (ast)
393 {
394 ast->setExternalSource (ast_source_ap);
395 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
396 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
397 }
398}
399
400void
401ClangASTContext::RemoveExternalSource ()
402{
403 ASTContext *ast = getASTContext();
404
405 if (ast)
406 {
Todd Fiala955fe6f2014-02-27 17:18:23 +0000407 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000408 ast->setExternalSource (empty_ast_source_ap);
409 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
410 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
411 }
412}
413
Greg Claytond8d4a572015-08-11 21:38:15 +0000414void
415ClangASTContext::setASTContext(clang::ASTContext *ast_ctx)
416{
417 if (!m_ast_owned) {
418 m_ast_ap.release();
419 }
420 m_ast_owned = false;
421 m_ast_ap.reset(ast_ctx);
422 GetASTMap().Insert(ast_ctx, this);
423}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424
425ASTContext *
426ClangASTContext::getASTContext()
427{
Ed Masted4612ad2014-04-20 13:17:36 +0000428 if (m_ast_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429 {
Greg Claytond8d4a572015-08-11 21:38:15 +0000430 m_ast_owned = true;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000431 m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
432 *getSourceManager(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000433 *getIdentifierTable(),
434 *getSelectorTable(),
Alp Tokercf55e882014-05-03 15:05:45 +0000435 *getBuiltinContext()));
Sean Callanan6d61b632015-04-09 17:42:48 +0000436
437 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Greg Clayton28eb7bf2015-05-07 00:07:44 +0000438
439 // This can be NULL if we don't know anything about the architecture or if the
440 // target for an architecture isn't enabled in the llvm/clang that we built
441 TargetInfo *target_info = getTargetInfo();
442 if (target_info)
443 m_ast_ap->InitBuiltinTypes(*target_info);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000444
Greg Clayton6beaaa62011-01-17 03:46:26 +0000445 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
446 {
447 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
448 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
449 }
450
Enrico Granata2267ad42014-09-16 17:28:40 +0000451 GetASTMap().Insert(m_ast_ap.get(), this);
Greg Clayton6dc8d582015-08-18 22:32:36 +0000452
453 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap (new ClangExternalASTSourceCallbacks (ClangASTContext::CompleteTagDecl,
454 ClangASTContext::CompleteObjCInterfaceDecl,
455 nullptr,
456 ClangASTContext::LayoutRecordType,
457 this));
458 SetExternalSource (ast_source_ap);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000460 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461}
462
Enrico Granata5d84a692014-08-19 21:46:37 +0000463ClangASTContext*
464ClangASTContext::GetASTContext (clang::ASTContext* ast)
465{
Enrico Granata2267ad42014-09-16 17:28:40 +0000466 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
Enrico Granata5d84a692014-08-19 21:46:37 +0000467 return clang_ast;
468}
469
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470Builtin::Context *
471ClangASTContext::getBuiltinContext()
472{
Ed Masted4612ad2014-04-20 13:17:36 +0000473 if (m_builtins_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000474 m_builtins_ap.reset (new Builtin::Context());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475 return m_builtins_ap.get();
476}
477
478IdentifierTable *
479ClangASTContext::getIdentifierTable()
480{
Ed Masted4612ad2014-04-20 13:17:36 +0000481 if (m_identifier_table_ap.get() == nullptr)
482 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), nullptr));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483 return m_identifier_table_ap.get();
484}
485
486LangOptions *
487ClangASTContext::getLanguageOptions()
488{
Ed Masted4612ad2014-04-20 13:17:36 +0000489 if (m_language_options_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490 {
491 m_language_options_ap.reset(new LangOptions());
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000492 ParseLangArgs(*m_language_options_ap, IK_ObjCXX, GetTargetTriple());
Greg Clayton94e5d782010-06-13 17:34:29 +0000493// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494 }
495 return m_language_options_ap.get();
496}
497
498SelectorTable *
499ClangASTContext::getSelectorTable()
500{
Ed Masted4612ad2014-04-20 13:17:36 +0000501 if (m_selector_table_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502 m_selector_table_ap.reset (new SelectorTable());
503 return m_selector_table_ap.get();
504}
505
Sean Callanan79439e82010-11-18 02:56:27 +0000506clang::FileManager *
507ClangASTContext::getFileManager()
508{
Ed Masted4612ad2014-04-20 13:17:36 +0000509 if (m_file_manager_ap.get() == nullptr)
Greg Clayton38a61402010-12-02 23:20:03 +0000510 {
511 clang::FileSystemOptions file_system_options;
512 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
513 }
Sean Callanan79439e82010-11-18 02:56:27 +0000514 return m_file_manager_ap.get();
515}
516
Greg Claytone1a916a2010-07-21 22:12:05 +0000517clang::SourceManager *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000518ClangASTContext::getSourceManager()
519{
Ed Masted4612ad2014-04-20 13:17:36 +0000520 if (m_source_manager_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000521 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522 return m_source_manager_ap.get();
523}
524
Sean Callanan880e6802011-10-07 23:18:13 +0000525clang::DiagnosticsEngine *
526ClangASTContext::getDiagnosticsEngine()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527{
Ed Masted4612ad2014-04-20 13:17:36 +0000528 if (m_diagnostics_engine_ap.get() == nullptr)
Greg Claytona651b532010-11-19 21:46:54 +0000529 {
530 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Sean Callananec8f1ef2012-10-25 01:00:25 +0000531 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
Greg Claytona651b532010-11-19 21:46:54 +0000532 }
Sean Callanan880e6802011-10-07 23:18:13 +0000533 return m_diagnostics_engine_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000534}
535
Sean Callanan880e6802011-10-07 23:18:13 +0000536class NullDiagnosticConsumer : public DiagnosticConsumer
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000537{
538public:
Sean Callanan880e6802011-10-07 23:18:13 +0000539 NullDiagnosticConsumer ()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000540 {
541 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
542 }
543
Sean Callanan880e6802011-10-07 23:18:13 +0000544 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const Diagnostic &info)
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000545 {
546 if (m_log)
547 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000548 llvm::SmallVector<char, 32> diag_str(10);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000549 info.FormatDiagnostic(diag_str);
550 diag_str.push_back('\0');
551 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
552 }
553 }
Sean Callanan880e6802011-10-07 23:18:13 +0000554
555 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
556 {
557 return new NullDiagnosticConsumer ();
558 }
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000559private:
Greg Clayton5160ce52013-03-27 23:08:40 +0000560 Log * m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000561};
562
Sean Callanan880e6802011-10-07 23:18:13 +0000563DiagnosticConsumer *
564ClangASTContext::getDiagnosticConsumer()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000565{
Ed Masted4612ad2014-04-20 13:17:36 +0000566 if (m_diagnostic_consumer_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000567 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000568
Sean Callanan880e6802011-10-07 23:18:13 +0000569 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000570}
571
Jason Molenda45938b92014-07-08 23:46:39 +0000572std::shared_ptr<TargetOptions> &
573ClangASTContext::getTargetOptions() {
Alp Tokeredc902f2014-07-05 03:06:05 +0000574 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000575 {
Alp Toker5f838642014-07-06 05:36:57 +0000576 m_target_options_rp = std::make_shared<TargetOptions>();
Alp Tokeredc902f2014-07-05 03:06:05 +0000577 if (m_target_options_rp.get() != nullptr)
Sean Callananc5069ad2012-10-17 22:11:14 +0000578 m_target_options_rp->Triple = m_target_triple;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579 }
Alp Toker5f838642014-07-06 05:36:57 +0000580 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581}
582
583
584TargetInfo *
585ClangASTContext::getTargetInfo()
586{
Greg Clayton70512312012-05-08 01:45:38 +0000587 // target_triple should be something like "x86_64-apple-macosx"
Ed Masted4612ad2014-04-20 13:17:36 +0000588 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
Greg Clayton38d880a2012-11-16 21:35:22 +0000589 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), getTargetOptions()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590 return m_target_info_ap.get();
591}
592
593#pragma mark Basic Types
594
595static inline bool
Greg Clayton6beaaa62011-01-17 03:46:26 +0000596QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000598 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599 if (qual_type_bit_size == bit_size)
600 return true;
601 return false;
602}
Greg Claytona1e5dc82015-08-11 22:53:00 +0000603CompilerType
Greg Claytonc86103d2010-08-05 01:57:25 +0000604ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605{
Greg Clayton57ee3062013-07-11 22:46:58 +0000606 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607}
608
Greg Claytona1e5dc82015-08-11 22:53:00 +0000609CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +0000610ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000612 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000613 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614 switch (encoding)
615 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000616 case eEncodingInvalid:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000617 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000618 return CompilerType (ast, ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619 break;
620
Greg Claytonc86103d2010-08-05 01:57:25 +0000621 case eEncodingUint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000622 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000623 return CompilerType (ast, ast->UnsignedCharTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000624 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000625 return CompilerType (ast, ast->UnsignedShortTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000626 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000627 return CompilerType (ast, ast->UnsignedIntTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000628 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000629 return CompilerType (ast, ast->UnsignedLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000630 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000631 return CompilerType (ast, ast->UnsignedLongLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000632 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000633 return CompilerType (ast, ast->UnsignedInt128Ty);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634 break;
635
Greg Claytonc86103d2010-08-05 01:57:25 +0000636 case eEncodingSint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000637 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000638 return CompilerType (ast, ast->CharTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000639 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000640 return CompilerType (ast, ast->ShortTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000641 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000642 return CompilerType (ast, ast->IntTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000643 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000644 return CompilerType (ast, ast->LongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000645 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000646 return CompilerType (ast, ast->LongLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000647 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000648 return CompilerType (ast, ast->Int128Ty);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000649 break;
650
Greg Claytonc86103d2010-08-05 01:57:25 +0000651 case eEncodingIEEE754:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000652 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000653 return CompilerType (ast, ast->FloatTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000654 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000655 return CompilerType (ast, ast->DoubleTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000656 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000657 return CompilerType (ast, ast->LongDoubleTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658 break;
659
Greg Claytonc86103d2010-08-05 01:57:25 +0000660 case eEncodingVector:
Johnny Chenc79c93a2012-03-07 01:12:24 +0000661 // Sanity check that bit_size is a multiple of 8's.
662 if (bit_size && !(bit_size & 0x7u))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000663 return CompilerType (ast, ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8));
Johnny Chenc79c93a2012-03-07 01:12:24 +0000664 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665 }
666
Greg Claytona1e5dc82015-08-11 22:53:00 +0000667 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000668}
669
Greg Clayton57ee3062013-07-11 22:46:58 +0000670
671
672lldb::BasicType
673ClangASTContext::GetBasicTypeEnumeration (const ConstString &name)
674{
675 if (name)
676 {
677 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
678 static TypeNameToBasicTypeMap g_type_map;
679 static std::once_flag g_once_flag;
680 std::call_once(g_once_flag, [](){
681 // "void"
682 g_type_map.Append(ConstString("void").GetCString(), eBasicTypeVoid);
683
684 // "char"
685 g_type_map.Append(ConstString("char").GetCString(), eBasicTypeChar);
686 g_type_map.Append(ConstString("signed char").GetCString(), eBasicTypeSignedChar);
687 g_type_map.Append(ConstString("unsigned char").GetCString(), eBasicTypeUnsignedChar);
688 g_type_map.Append(ConstString("wchar_t").GetCString(), eBasicTypeWChar);
689 g_type_map.Append(ConstString("signed wchar_t").GetCString(), eBasicTypeSignedWChar);
690 g_type_map.Append(ConstString("unsigned wchar_t").GetCString(), eBasicTypeUnsignedWChar);
691 // "short"
692 g_type_map.Append(ConstString("short").GetCString(), eBasicTypeShort);
693 g_type_map.Append(ConstString("short int").GetCString(), eBasicTypeShort);
694 g_type_map.Append(ConstString("unsigned short").GetCString(), eBasicTypeUnsignedShort);
695 g_type_map.Append(ConstString("unsigned short int").GetCString(), eBasicTypeUnsignedShort);
696
697 // "int"
698 g_type_map.Append(ConstString("int").GetCString(), eBasicTypeInt);
699 g_type_map.Append(ConstString("signed int").GetCString(), eBasicTypeInt);
700 g_type_map.Append(ConstString("unsigned int").GetCString(), eBasicTypeUnsignedInt);
701 g_type_map.Append(ConstString("unsigned").GetCString(), eBasicTypeUnsignedInt);
702
703 // "long"
704 g_type_map.Append(ConstString("long").GetCString(), eBasicTypeLong);
705 g_type_map.Append(ConstString("long int").GetCString(), eBasicTypeLong);
706 g_type_map.Append(ConstString("unsigned long").GetCString(), eBasicTypeUnsignedLong);
707 g_type_map.Append(ConstString("unsigned long int").GetCString(), eBasicTypeUnsignedLong);
708
709 // "long long"
710 g_type_map.Append(ConstString("long long").GetCString(), eBasicTypeLongLong);
711 g_type_map.Append(ConstString("long long int").GetCString(), eBasicTypeLongLong);
712 g_type_map.Append(ConstString("unsigned long long").GetCString(), eBasicTypeUnsignedLongLong);
713 g_type_map.Append(ConstString("unsigned long long int").GetCString(), eBasicTypeUnsignedLongLong);
714
715 // "int128"
716 g_type_map.Append(ConstString("__int128_t").GetCString(), eBasicTypeInt128);
717 g_type_map.Append(ConstString("__uint128_t").GetCString(), eBasicTypeUnsignedInt128);
718
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000719 // Miscellaneous
Greg Clayton57ee3062013-07-11 22:46:58 +0000720 g_type_map.Append(ConstString("bool").GetCString(), eBasicTypeBool);
721 g_type_map.Append(ConstString("float").GetCString(), eBasicTypeFloat);
722 g_type_map.Append(ConstString("double").GetCString(), eBasicTypeDouble);
723 g_type_map.Append(ConstString("long double").GetCString(), eBasicTypeLongDouble);
724 g_type_map.Append(ConstString("id").GetCString(), eBasicTypeObjCID);
725 g_type_map.Append(ConstString("SEL").GetCString(), eBasicTypeObjCSel);
726 g_type_map.Append(ConstString("nullptr").GetCString(), eBasicTypeNullPtr);
727 g_type_map.Sort();
728 });
729
730 return g_type_map.Find(name.GetCString(), eBasicTypeInvalid);
731 }
732 return eBasicTypeInvalid;
733}
734
Greg Claytona1e5dc82015-08-11 22:53:00 +0000735CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000736ClangASTContext::GetBasicType (ASTContext *ast, const ConstString &name)
737{
738 if (ast)
739 {
740 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration (name);
741 return ClangASTContext::GetBasicType (ast, basic_type);
742 }
Greg Claytona1e5dc82015-08-11 22:53:00 +0000743 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +0000744}
745
746uint32_t
747ClangASTContext::GetPointerByteSize ()
748{
749 if (m_pointer_byte_size == 0)
Enrico Granata1cd5e922015-01-28 00:07:51 +0000750 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid).GetPointerType().GetByteSize(nullptr);
Greg Clayton57ee3062013-07-11 22:46:58 +0000751 return m_pointer_byte_size;
752}
753
Greg Claytona1e5dc82015-08-11 22:53:00 +0000754CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000755ClangASTContext::GetBasicType (lldb::BasicType basic_type)
756{
757 return GetBasicType (getASTContext(), basic_type);
758}
759
Greg Claytona1e5dc82015-08-11 22:53:00 +0000760CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000761ClangASTContext::GetBasicType (ASTContext *ast, lldb::BasicType basic_type)
762{
763 if (ast)
764 {
Ed Masted4612ad2014-04-20 13:17:36 +0000765 clang_type_t clang_type = nullptr;
Greg Clayton57ee3062013-07-11 22:46:58 +0000766
767 switch (basic_type)
768 {
769 case eBasicTypeInvalid:
770 case eBasicTypeOther:
771 break;
772 case eBasicTypeVoid:
773 clang_type = ast->VoidTy.getAsOpaquePtr();
774 break;
775 case eBasicTypeChar:
776 clang_type = ast->CharTy.getAsOpaquePtr();
777 break;
778 case eBasicTypeSignedChar:
779 clang_type = ast->SignedCharTy.getAsOpaquePtr();
780 break;
781 case eBasicTypeUnsignedChar:
782 clang_type = ast->UnsignedCharTy.getAsOpaquePtr();
783 break;
784 case eBasicTypeWChar:
785 clang_type = ast->getWCharType().getAsOpaquePtr();
786 break;
787 case eBasicTypeSignedWChar:
788 clang_type = ast->getSignedWCharType().getAsOpaquePtr();
789 break;
790 case eBasicTypeUnsignedWChar:
791 clang_type = ast->getUnsignedWCharType().getAsOpaquePtr();
792 break;
793 case eBasicTypeChar16:
794 clang_type = ast->Char16Ty.getAsOpaquePtr();
795 break;
796 case eBasicTypeChar32:
797 clang_type = ast->Char32Ty.getAsOpaquePtr();
798 break;
799 case eBasicTypeShort:
800 clang_type = ast->ShortTy.getAsOpaquePtr();
801 break;
802 case eBasicTypeUnsignedShort:
803 clang_type = ast->UnsignedShortTy.getAsOpaquePtr();
804 break;
805 case eBasicTypeInt:
806 clang_type = ast->IntTy.getAsOpaquePtr();
807 break;
808 case eBasicTypeUnsignedInt:
809 clang_type = ast->UnsignedIntTy.getAsOpaquePtr();
810 break;
811 case eBasicTypeLong:
812 clang_type = ast->LongTy.getAsOpaquePtr();
813 break;
814 case eBasicTypeUnsignedLong:
815 clang_type = ast->UnsignedLongTy.getAsOpaquePtr();
816 break;
817 case eBasicTypeLongLong:
818 clang_type = ast->LongLongTy.getAsOpaquePtr();
819 break;
820 case eBasicTypeUnsignedLongLong:
821 clang_type = ast->UnsignedLongLongTy.getAsOpaquePtr();
822 break;
823 case eBasicTypeInt128:
824 clang_type = ast->Int128Ty.getAsOpaquePtr();
825 break;
826 case eBasicTypeUnsignedInt128:
827 clang_type = ast->UnsignedInt128Ty.getAsOpaquePtr();
828 break;
829 case eBasicTypeBool:
830 clang_type = ast->BoolTy.getAsOpaquePtr();
831 break;
832 case eBasicTypeHalf:
833 clang_type = ast->HalfTy.getAsOpaquePtr();
834 break;
835 case eBasicTypeFloat:
836 clang_type = ast->FloatTy.getAsOpaquePtr();
837 break;
838 case eBasicTypeDouble:
839 clang_type = ast->DoubleTy.getAsOpaquePtr();
840 break;
841 case eBasicTypeLongDouble:
842 clang_type = ast->LongDoubleTy.getAsOpaquePtr();
843 break;
844 case eBasicTypeFloatComplex:
845 clang_type = ast->FloatComplexTy.getAsOpaquePtr();
846 break;
847 case eBasicTypeDoubleComplex:
848 clang_type = ast->DoubleComplexTy.getAsOpaquePtr();
849 break;
850 case eBasicTypeLongDoubleComplex:
851 clang_type = ast->LongDoubleComplexTy.getAsOpaquePtr();
852 break;
853 case eBasicTypeObjCID:
854 clang_type = ast->getObjCIdType().getAsOpaquePtr();
855 break;
856 case eBasicTypeObjCClass:
857 clang_type = ast->getObjCClassType().getAsOpaquePtr();
858 break;
859 case eBasicTypeObjCSel:
860 clang_type = ast->getObjCSelType().getAsOpaquePtr();
861 break;
862 case eBasicTypeNullPtr:
863 clang_type = ast->NullPtrTy.getAsOpaquePtr();
864 break;
865 }
866
867 if (clang_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000868 return CompilerType (GetASTContext(ast), clang_type);
Greg Clayton57ee3062013-07-11 22:46:58 +0000869 }
Greg Claytona1e5dc82015-08-11 22:53:00 +0000870 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +0000871}
872
873
Greg Claytona1e5dc82015-08-11 22:53:00 +0000874CompilerType
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000875ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
876{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000877 ASTContext *ast = getASTContext();
Sean Callanan38d4df52012-04-03 01:10:10 +0000878
879#define streq(a,b) strcmp(a,b) == 0
Ed Masted4612ad2014-04-20 13:17:36 +0000880 assert (ast != nullptr);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000881 if (ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000882 {
883 switch (dw_ate)
884 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000885 default:
886 break;
Greg Clayton605684e2011-10-28 23:06:08 +0000887
Sean Callanan38d4df52012-04-03 01:10:10 +0000888 case DW_ATE_address:
889 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000890 return CompilerType (ast, ast->VoidPtrTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000891 break;
892
893 case DW_ATE_boolean:
894 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000895 return CompilerType (ast, ast->BoolTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000896 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000897 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000898 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000899 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000900 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000901 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000902 break;
903
904 case DW_ATE_lo_user:
905 // This has been seen to mean DW_AT_complex_integer
906 if (type_name)
Greg Clayton605684e2011-10-28 23:06:08 +0000907 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000908 if (::strstr(type_name, "complex"))
909 {
Greg Claytona1e5dc82015-08-11 22:53:00 +0000910 CompilerType complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2);
911 return CompilerType (ast, ast->getComplexType (GetQualType(complex_int_clang_type)));
Sean Callanan38d4df52012-04-03 01:10:10 +0000912 }
Greg Clayton605684e2011-10-28 23:06:08 +0000913 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000914 break;
915
916 case DW_ATE_complex_float:
917 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000918 return CompilerType (ast, ast->FloatComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000919 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000920 return CompilerType (ast, ast->DoubleComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000921 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000922 return CompilerType (ast, ast->LongDoubleComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000923 else
Greg Clayton605684e2011-10-28 23:06:08 +0000924 {
Greg Claytona1e5dc82015-08-11 22:53:00 +0000925 CompilerType complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2);
926 return CompilerType (ast, ast->getComplexType (GetQualType(complex_float_clang_type)));
Greg Clayton605684e2011-10-28 23:06:08 +0000927 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000928 break;
929
930 case DW_ATE_float:
Greg Clayton8012cad2014-11-17 19:39:20 +0000931 if (streq(type_name, "float") && QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000932 return CompilerType (ast, ast->FloatTy);
Greg Clayton8012cad2014-11-17 19:39:20 +0000933 if (streq(type_name, "double") && QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000934 return CompilerType (ast, ast->DoubleTy);
Greg Clayton8012cad2014-11-17 19:39:20 +0000935 if (streq(type_name, "long double") && QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000936 return CompilerType (ast, ast->LongDoubleTy);
Bruce Mitchenere171da52015-07-22 00:16:02 +0000937 // Fall back to not requiring a name match
Sean Callanan38d4df52012-04-03 01:10:10 +0000938 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000939 return CompilerType (ast, ast->FloatTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000940 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000941 return CompilerType (ast, ast->DoubleTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000942 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000943 return CompilerType (ast, ast->LongDoubleTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000944 break;
945
946 case DW_ATE_signed:
947 if (type_name)
948 {
949 if (streq(type_name, "wchar_t") &&
Tamas Berghammer3c0d0052015-04-01 09:48:02 +0000950 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy) &&
Greg Clayton28eb7bf2015-05-07 00:07:44 +0000951 (getTargetInfo() && TargetInfo::isTypeSigned (getTargetInfo()->getWCharType())))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000952 return CompilerType (ast, ast->WCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000953 if (streq(type_name, "void") &&
954 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000955 return CompilerType (ast, ast->VoidTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000956 if (strstr(type_name, "long long") &&
957 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000958 return CompilerType (ast, ast->LongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000959 if (strstr(type_name, "long") &&
960 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000961 return CompilerType (ast, ast->LongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000962 if (strstr(type_name, "short") &&
963 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000964 return CompilerType (ast, ast->ShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000965 if (strstr(type_name, "char"))
966 {
967 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000968 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000969 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000970 return CompilerType (ast, ast->SignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000971 }
972 if (strstr(type_name, "int"))
973 {
974 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000975 return CompilerType (ast, ast->IntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000976 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000977 return CompilerType (ast, ast->Int128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +0000978 }
979 }
980 // We weren't able to match up a type name, just search by size
981 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000982 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000983 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000984 return CompilerType (ast, ast->ShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000985 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000986 return CompilerType (ast, ast->IntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000987 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000988 return CompilerType (ast, ast->LongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000989 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000990 return CompilerType (ast, ast->LongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000991 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000992 return CompilerType (ast, ast->Int128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +0000993 break;
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000994
Sean Callanan38d4df52012-04-03 01:10:10 +0000995 case DW_ATE_signed_char:
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000996 if (ast->getLangOpts().CharIsSigned && type_name && streq(type_name, "char"))
Sean Callanan38d4df52012-04-03 01:10:10 +0000997 {
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000998 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000999 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001000 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001001 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001002 return CompilerType (ast, ast->SignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001003 break;
1004
1005 case DW_ATE_unsigned:
1006 if (type_name)
1007 {
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001008 if (streq(type_name, "wchar_t"))
1009 {
1010 if (QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
1011 {
Greg Clayton28eb7bf2015-05-07 00:07:44 +00001012 if (!(getTargetInfo() && TargetInfo::isTypeSigned (getTargetInfo()->getWCharType())))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001013 return CompilerType (ast, ast->WCharTy);
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001014 }
1015 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001016 if (strstr(type_name, "long long"))
1017 {
1018 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001019 return CompilerType (ast, ast->UnsignedLongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001020 }
1021 else if (strstr(type_name, "long"))
1022 {
1023 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001024 return CompilerType (ast, ast->UnsignedLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001025 }
1026 else if (strstr(type_name, "short"))
1027 {
1028 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001029 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001030 }
1031 else if (strstr(type_name, "char"))
1032 {
1033 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001034 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001035 }
1036 else if (strstr(type_name, "int"))
1037 {
1038 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001039 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001040 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001041 return CompilerType (ast, ast->UnsignedInt128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001042 }
1043 }
1044 // We weren't able to match up a type name, just search by size
1045 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001046 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001047 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001048 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001049 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001050 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001051 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001052 return CompilerType (ast, ast->UnsignedLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001053 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001054 return CompilerType (ast, ast->UnsignedLongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001055 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001056 return CompilerType (ast, ast->UnsignedInt128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001057 break;
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001058
Sean Callanan38d4df52012-04-03 01:10:10 +00001059 case DW_ATE_unsigned_char:
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001060 if (!ast->getLangOpts().CharIsSigned && type_name && streq(type_name, "char"))
1061 {
1062 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001063 return CompilerType (ast, ast->CharTy);
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001064 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001065 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001066 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001067 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001068 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001069 break;
1070
1071 case DW_ATE_imaginary_float:
1072 break;
1073
1074 case DW_ATE_UTF:
1075 if (type_name)
1076 {
1077 if (streq(type_name, "char16_t"))
1078 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001079 return CompilerType (ast, ast->Char16Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001080 }
1081 else if (streq(type_name, "char32_t"))
1082 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001083 return CompilerType (ast, ast->Char32Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001084 }
1085 }
1086 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001087 }
1088 }
1089 // This assert should fire for anything that we don't catch above so we know
1090 // to fix any issues we run into.
Greg Claytondc968d12011-05-17 18:15:05 +00001091 if (type_name)
1092 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001093 Host::SystemLog (Host::eSystemLogError, "error: need to add support for DW_TAG_base_type '%s' encoded with DW_ATE = 0x%x, bit_size = %u\n", type_name, dw_ate, bit_size);
Greg Claytondc968d12011-05-17 18:15:05 +00001094 }
1095 else
1096 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001097 Host::SystemLog (Host::eSystemLogError, "error: need to add support for DW_TAG_base_type encoded with DW_ATE = 0x%x, bit_size = %u\n", dw_ate, bit_size);
Greg Claytondc968d12011-05-17 18:15:05 +00001098 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001099 return CompilerType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001100}
1101
Greg Claytona1e5dc82015-08-11 22:53:00 +00001102CompilerType
Sean Callanan77502262011-05-12 23:54:16 +00001103ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast)
1104{
Greg Clayton57ee3062013-07-11 22:46:58 +00001105 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001106 return CompilerType (ast, ast->UnknownAnyTy);
1107 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001108}
1109
Greg Claytona1e5dc82015-08-11 22:53:00 +00001110CompilerType
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001111ClangASTContext::GetCStringType (bool is_const)
1112{
Greg Clayton57ee3062013-07-11 22:46:58 +00001113 ASTContext *ast = getASTContext();
1114 QualType char_type(ast->CharTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001115
1116 if (is_const)
1117 char_type.addConst();
1118
Greg Claytona1e5dc82015-08-11 22:53:00 +00001119 return CompilerType (ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001120}
1121
Sean Callanan09ab4b72011-11-30 22:11:59 +00001122clang::DeclContext *
1123ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast)
1124{
1125 return ast->getTranslationUnitDecl();
1126}
1127
Greg Claytona1e5dc82015-08-11 22:53:00 +00001128CompilerType
Greg Clayton38a61402010-12-02 23:20:03 +00001129ClangASTContext::CopyType (ASTContext *dst_ast,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001130 CompilerType src)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001131{
Sean Callanan79439e82010-11-18 02:56:27 +00001132 FileSystemOptions file_system_options;
Greg Claytond8d4a572015-08-11 21:38:15 +00001133 ClangASTContext *src_ast = src.GetTypeSystem()->AsClangASTContext();
1134 if (src_ast == nullptr)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001135 return CompilerType();
Greg Clayton38a61402010-12-02 23:20:03 +00001136 FileManager file_manager (file_system_options);
1137 ASTImporter importer(*dst_ast, file_manager,
Greg Claytond8d4a572015-08-11 21:38:15 +00001138 *src_ast->getASTContext(), file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001139 false);
Sean Callanan0617fcb2010-11-09 22:37:10 +00001140
Greg Claytond8d4a572015-08-11 21:38:15 +00001141 QualType dst (importer.Import(GetQualType(src)));
Sean Callanan0617fcb2010-11-09 22:37:10 +00001142
Greg Claytona1e5dc82015-08-11 22:53:00 +00001143 return CompilerType (dst_ast, dst);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144}
1145
Greg Clayton526e5af2010-11-13 03:52:47 +00001146
1147clang::Decl *
Greg Clayton38a61402010-12-02 23:20:03 +00001148ClangASTContext::CopyDecl (ASTContext *dst_ast,
1149 ASTContext *src_ast,
Greg Clayton526e5af2010-11-13 03:52:47 +00001150 clang::Decl *source_decl)
Sean Callanan7fddd4c2010-12-11 00:08:56 +00001151{
Sean Callanan79439e82010-11-18 02:56:27 +00001152 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001153 FileManager file_manager (file_system_options);
1154 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001155 *src_ast, file_manager,
1156 false);
Greg Clayton526e5af2010-11-13 03:52:47 +00001157
1158 return importer.Import(source_decl);
1159}
1160
Sean Callanan23a30272010-07-16 00:00:27 +00001161bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00001162ClangASTContext::AreTypesSame (CompilerType type1,
1163 CompilerType type2,
Greg Clayton84db9102012-03-26 23:03:23 +00001164 bool ignore_qualifiers)
Sean Callanan4dcca2622010-07-15 22:30:52 +00001165{
Greg Claytond8d4a572015-08-11 21:38:15 +00001166 TypeSystem *ast = type1.GetTypeSystem();
1167 if (!ast->AsClangASTContext() || ast != type2.GetTypeSystem())
Greg Clayton57ee3062013-07-11 22:46:58 +00001168 return false;
1169
1170 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
Greg Clayton55995eb2012-04-06 17:38:55 +00001171 return true;
1172
Greg Claytond8d4a572015-08-11 21:38:15 +00001173 QualType type1_qual = GetQualType(type1);
1174 QualType type2_qual = GetQualType(type2);
Sean Callanan5056ab02012-02-18 02:01:03 +00001175
1176 if (ignore_qualifiers)
1177 {
1178 type1_qual = type1_qual.getUnqualifiedType();
1179 type2_qual = type2_qual.getUnqualifiedType();
1180 }
1181
Greg Claytond8d4a572015-08-11 21:38:15 +00001182 return ast->AsClangASTContext()->getASTContext()->hasSameType (type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001183}
1184
Greg Claytona1e5dc82015-08-11 22:53:00 +00001185CompilerType
Sean Callanan9998acd2014-12-05 01:21:59 +00001186ClangASTContext::GetTypeForDecl (clang::NamedDecl *decl)
1187{
1188 if (clang::ObjCInterfaceDecl *interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1189 return GetTypeForDecl(interface_decl);
1190 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1191 return GetTypeForDecl(tag_decl);
Greg Claytona1e5dc82015-08-11 22:53:00 +00001192 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001193}
1194
Greg Clayton6beaaa62011-01-17 03:46:26 +00001195
Greg Claytona1e5dc82015-08-11 22:53:00 +00001196CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001197ClangASTContext::GetTypeForDecl (TagDecl *decl)
1198{
1199 // No need to call the getASTContext() accessor (which can create the AST
1200 // if it isn't created yet, because we can't have created a decl in this
1201 // AST if our AST didn't already exist...
Sean Callanan9998acd2014-12-05 01:21:59 +00001202 ASTContext *ast = &decl->getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001203 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001204 return CompilerType (ast, ast->getTagDeclType(decl));
1205 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001206}
1207
Greg Claytona1e5dc82015-08-11 22:53:00 +00001208CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001209ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1210{
1211 // No need to call the getASTContext() accessor (which can create the AST
1212 // if it isn't created yet, because we can't have created a decl in this
1213 // AST if our AST didn't already exist...
Sean Callanan9998acd2014-12-05 01:21:59 +00001214 ASTContext *ast = &decl->getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001215 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001216 return CompilerType (ast, ast->getObjCInterfaceType(decl));
1217 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001218}
1219
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001220#pragma mark Structure, Unions, Classes
1221
Greg Claytona1e5dc82015-08-11 22:53:00 +00001222CompilerType
Greg Claytonc4ffd662013-03-08 01:37:30 +00001223ClangASTContext::CreateRecordType (DeclContext *decl_ctx,
1224 AccessType access_type,
1225 const char *name,
1226 int kind,
1227 LanguageType language,
1228 ClangASTMetadata *metadata)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001229{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001230 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001231 assert (ast != nullptr);
Sean Callananad880762012-04-18 01:06:17 +00001232
Ed Masted4612ad2014-04-20 13:17:36 +00001233 if (decl_ctx == nullptr)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001234 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001235
Greg Clayton9e409562010-07-28 02:04:09 +00001236
Greg Claytone1be9962011-08-24 23:50:00 +00001237 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
Greg Clayton9e409562010-07-28 02:04:09 +00001238 {
Greg Claytonaaf99e02010-10-11 02:25:34 +00001239 bool isForwardDecl = true;
Greg Clayton9e409562010-07-28 02:04:09 +00001240 bool isInternal = false;
Sean Callananad880762012-04-18 01:06:17 +00001241 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal, metadata);
Greg Clayton9e409562010-07-28 02:04:09 +00001242 }
1243
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001244 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1245 // we will need to update this code. I was told to currently always use
1246 // the CXXRecordDecl class since we often don't know from debug information
1247 // if something is struct or a class, so we default to always use the more
1248 // complete definition just in case.
Sean Callanan11e32d32014-02-18 00:31:38 +00001249
1250 bool is_anonymous = (!name) || (!name[0]);
1251
Greg Claytonf0705c82011-10-22 03:33:13 +00001252 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1253 (TagDecl::TagKind)kind,
1254 decl_ctx,
1255 SourceLocation(),
1256 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001257 is_anonymous ? nullptr : &ast->Idents.get(name));
Sean Callanan11e32d32014-02-18 00:31:38 +00001258
1259 if (is_anonymous)
1260 decl->setAnonymousStructOrUnion(true);
Sean Callanan7282e2a2012-01-13 22:10:18 +00001261
Greg Claytonc4ffd662013-03-08 01:37:30 +00001262 if (decl)
Greg Clayton55561e92011-10-26 03:31:36 +00001263 {
Greg Claytonc4ffd662013-03-08 01:37:30 +00001264 if (metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001265 SetMetadata(ast, decl, *metadata);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001266
Greg Clayton55561e92011-10-26 03:31:36 +00001267 if (access_type != eAccessNone)
1268 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Greg Claytonc4ffd662013-03-08 01:37:30 +00001269
1270 if (decl_ctx)
1271 decl_ctx->addDecl (decl);
1272
Greg Claytona1e5dc82015-08-11 22:53:00 +00001273 return CompilerType(ast, ast->getTagDeclType(decl));
Greg Clayton55561e92011-10-26 03:31:36 +00001274 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001275 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001276}
1277
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001278static TemplateParameterList *
1279CreateTemplateParameterList (ASTContext *ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001280 const ClangASTContext::TemplateParameterInfos &template_param_infos,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001281 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1282{
1283 const bool parameter_pack = false;
1284 const bool is_typename = false;
1285 const unsigned depth = 0;
1286 const size_t num_template_params = template_param_infos.GetSize();
1287 for (size_t i=0; i<num_template_params; ++i)
1288 {
1289 const char *name = template_param_infos.names[i];
Greg Clayton283b2652013-04-23 22:38:02 +00001290
Ed Masted4612ad2014-04-20 13:17:36 +00001291 IdentifierInfo *identifier_info = nullptr;
Greg Clayton283b2652013-04-23 22:38:02 +00001292 if (name && name[0])
1293 identifier_info = &ast->Idents.get(name);
Sean Callanan3d654b32012-09-24 22:25:51 +00001294 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001295 {
1296 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1297 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1298 SourceLocation(),
1299 SourceLocation(),
1300 depth,
1301 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001302 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001303 template_param_infos.args[i].getIntegralType(),
1304 parameter_pack,
Ed Masted4612ad2014-04-20 13:17:36 +00001305 nullptr));
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001306
1307 }
1308 else
1309 {
1310 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1311 ast->getTranslationUnitDecl(), // Is this the right decl context?
1312 SourceLocation(),
1313 SourceLocation(),
1314 depth,
1315 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001316 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001317 is_typename,
1318 parameter_pack));
1319 }
1320 }
1321
1322 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1323 SourceLocation(),
1324 SourceLocation(),
1325 &template_param_decls.front(),
1326 template_param_decls.size(),
1327 SourceLocation());
1328 return template_param_list;
1329}
1330
1331clang::FunctionTemplateDecl *
1332ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1333 clang::FunctionDecl *func_decl,
1334 const char *name,
1335 const TemplateParameterInfos &template_param_infos)
1336{
1337// /// \brief Create a function template node.
1338 ASTContext *ast = getASTContext();
1339
1340 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1341
1342 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1343 template_param_infos,
1344 template_param_decls);
1345 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1346 decl_ctx,
1347 func_decl->getLocation(),
1348 func_decl->getDeclName(),
1349 template_param_list,
1350 func_decl);
1351
1352 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1353 i < template_param_decl_count;
1354 ++i)
1355 {
1356 // TODO: verify which decl context we should put template_param_decls into..
1357 template_param_decls[i]->setDeclContext (func_decl);
1358 }
1359
1360 return func_tmpl_decl;
1361}
1362
1363void
1364ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1365 clang::FunctionTemplateDecl *func_tmpl_decl,
1366 const TemplateParameterInfos &infos)
1367{
1368 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1369 infos.args.data(),
1370 infos.args.size());
1371
1372 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1373 &template_args,
Ed Masted4612ad2014-04-20 13:17:36 +00001374 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001375}
1376
1377
Greg Claytonf0705c82011-10-22 03:33:13 +00001378ClassTemplateDecl *
1379ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001380 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001381 const char *class_name,
1382 int kind,
1383 const TemplateParameterInfos &template_param_infos)
1384{
1385 ASTContext *ast = getASTContext();
1386
Ed Masted4612ad2014-04-20 13:17:36 +00001387 ClassTemplateDecl *class_template_decl = nullptr;
1388 if (decl_ctx == nullptr)
Greg Claytonf0705c82011-10-22 03:33:13 +00001389 decl_ctx = ast->getTranslationUnitDecl();
1390
1391 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1392 DeclarationName decl_name (&identifier_info);
1393
1394 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001395
1396 for (NamedDecl *decl : result)
Greg Claytonf0705c82011-10-22 03:33:13 +00001397 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001398 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001399 if (class_template_decl)
1400 return class_template_decl;
1401 }
1402
1403 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Claytonf0705c82011-10-22 03:33:13 +00001404
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001405 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1406 template_param_infos,
1407 template_param_decls);
Greg Claytonf0705c82011-10-22 03:33:13 +00001408
1409 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1410 (TagDecl::TagKind)kind,
1411 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1412 SourceLocation(),
1413 SourceLocation(),
1414 &identifier_info);
Greg Claytone04741d2011-12-02 02:09:28 +00001415
1416 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1417 i < template_param_decl_count;
1418 ++i)
1419 {
1420 template_param_decls[i]->setDeclContext (template_cxx_decl);
1421 }
1422
Sean Callananb5c79622011-11-19 01:35:08 +00001423 // With templated classes, we say that a class is templated with
1424 // specializations, but that the bare class has no functions.
Sean Callananfa4fab72013-02-01 06:55:48 +00001425 //template_cxx_decl->startDefinition();
1426 //template_cxx_decl->completeDefinition();
Sean Callananb5c79622011-11-19 01:35:08 +00001427
Greg Claytonf0705c82011-10-22 03:33:13 +00001428 class_template_decl = ClassTemplateDecl::Create (*ast,
1429 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1430 SourceLocation(),
1431 decl_name,
1432 template_param_list,
1433 template_cxx_decl,
Ed Masted4612ad2014-04-20 13:17:36 +00001434 nullptr);
Greg Claytonf0705c82011-10-22 03:33:13 +00001435
1436 if (class_template_decl)
Sean Callanan5e9e1992011-10-26 01:06:27 +00001437 {
Greg Clayton55561e92011-10-26 03:31:36 +00001438 if (access_type != eAccessNone)
1439 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Sean Callanan5b26f272012-02-04 08:49:35 +00001440
1441 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1442 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1443
Greg Claytonf0705c82011-10-22 03:33:13 +00001444 decl_ctx->addDecl (class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001445
1446#ifdef LLDB_CONFIGURATION_DEBUG
1447 VerifyDecl(class_template_decl);
1448#endif
1449 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001450
1451 return class_template_decl;
1452}
1453
1454
1455ClassTemplateSpecializationDecl *
1456ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1457 ClassTemplateDecl *class_template_decl,
1458 int kind,
1459 const TemplateParameterInfos &template_param_infos)
1460{
1461 ASTContext *ast = getASTContext();
1462 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1463 (TagDecl::TagKind)kind,
1464 decl_ctx,
1465 SourceLocation(),
1466 SourceLocation(),
1467 class_template_decl,
1468 &template_param_infos.args.front(),
1469 template_param_infos.args.size(),
Ed Masted4612ad2014-04-20 13:17:36 +00001470 nullptr);
Greg Claytonf0705c82011-10-22 03:33:13 +00001471
Sean Callananfa4fab72013-02-01 06:55:48 +00001472 class_template_specialization_decl->setSpecializationKind(TSK_ExplicitSpecialization);
1473
Greg Claytonf0705c82011-10-22 03:33:13 +00001474 return class_template_specialization_decl;
1475}
1476
Greg Claytona1e5dc82015-08-11 22:53:00 +00001477CompilerType
Greg Claytonf0705c82011-10-22 03:33:13 +00001478ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1479{
1480 if (class_template_specialization_decl)
1481 {
1482 ASTContext *ast = getASTContext();
1483 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001484 return CompilerType(ast, ast->getTagDeclType(class_template_specialization_decl));
Greg Claytonf0705c82011-10-22 03:33:13 +00001485 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001486 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001487}
1488
Greg Clayton090d0982011-06-19 03:43:27 +00001489static inline bool
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001490check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
Greg Clayton090d0982011-06-19 03:43:27 +00001491{
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001492 // Special-case call since it can take any number of operands
1493 if(op_kind == OO_Call)
1494 return true;
1495
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001496 // The parameter count doesn't include "this"
Greg Clayton090d0982011-06-19 03:43:27 +00001497 if (num_params == 0)
1498 return unary;
1499 if (num_params == 1)
1500 return binary;
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001501 else
Greg Clayton090d0982011-06-19 03:43:27 +00001502 return false;
1503}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001504
Greg Clayton090d0982011-06-19 03:43:27 +00001505bool
1506ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1507{
Sean Callanan5b26f272012-02-04 08:49:35 +00001508 switch (op_kind)
1509 {
1510 default:
1511 break;
1512 // C++ standard allows any number of arguments to new/delete
1513 case OO_New:
1514 case OO_Array_New:
1515 case OO_Delete:
1516 case OO_Array_Delete:
1517 return true;
1518 }
1519
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001520#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) case OO_##Name: return check_op_param (op_kind, Unary, Binary, num_params);
Greg Clayton090d0982011-06-19 03:43:27 +00001521 switch (op_kind)
1522 {
1523#include "clang/Basic/OperatorKinds.def"
1524 default: break;
1525 }
1526 return false;
1527}
1528
Greg Clayton57ee3062013-07-11 22:46:58 +00001529clang::AccessSpecifier
1530ClangASTContext::UnifyAccessSpecifiers (clang::AccessSpecifier lhs, clang::AccessSpecifier rhs)
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001531{
1532 clang::AccessSpecifier ret = lhs;
1533
1534 // Make the access equal to the stricter of the field and the nested field's access
1535 switch (ret)
1536 {
1537 case clang::AS_none:
1538 break;
1539 case clang::AS_private:
1540 break;
1541 case clang::AS_protected:
1542 if (rhs == AS_private)
1543 ret = AS_private;
1544 break;
1545 case clang::AS_public:
1546 ret = rhs;
1547 break;
1548 }
1549
1550 return ret;
1551}
1552
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001553bool
1554ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
1555{
1556 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1557}
1558
1559bool
1560ClangASTContext::FieldIsBitfield
1561(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001562 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001563 FieldDecl* field,
1564 uint32_t& bitfield_bit_size
1565)
1566{
Ed Masted4612ad2014-04-20 13:17:36 +00001567 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001568 return false;
1569
1570 if (field->isBitField())
1571 {
1572 Expr* bit_width_expr = field->getBitWidth();
1573 if (bit_width_expr)
1574 {
1575 llvm::APSInt bit_width_apsint;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001576 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001577 {
1578 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
1579 return true;
1580 }
1581 }
1582 }
1583 return false;
1584}
1585
1586bool
1587ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
1588{
Ed Masted4612ad2014-04-20 13:17:36 +00001589 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001590 return false;
1591
1592 if (!record_decl->field_empty())
1593 return true;
1594
1595 // No fields, lets check this is a CXX record and check the base classes
1596 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1597 if (cxx_record_decl)
1598 {
1599 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1600 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1601 base_class != base_class_end;
1602 ++base_class)
1603 {
1604 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1605 if (RecordHasFields(base_class_decl))
1606 return true;
1607 }
1608 }
1609 return false;
1610}
1611
Greg Clayton8cf05932010-07-22 18:30:50 +00001612#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001613
Greg Claytona1e5dc82015-08-11 22:53:00 +00001614CompilerType
Sean Callananad880762012-04-18 01:06:17 +00001615ClangASTContext::CreateObjCClass
Greg Clayton8cf05932010-07-22 18:30:50 +00001616(
1617 const char *name,
1618 DeclContext *decl_ctx,
1619 bool isForwardDecl,
Sean Callananad880762012-04-18 01:06:17 +00001620 bool isInternal,
Jim Ingham379397632012-10-27 02:54:13 +00001621 ClangASTMetadata *metadata
Greg Clayton8cf05932010-07-22 18:30:50 +00001622)
1623{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001624 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001625 assert (ast != nullptr);
Greg Clayton8cf05932010-07-22 18:30:50 +00001626 assert (name && name[0]);
Ed Masted4612ad2014-04-20 13:17:36 +00001627 if (decl_ctx == nullptr)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001628 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001629
Greg Clayton6beaaa62011-01-17 03:46:26 +00001630 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00001631 decl_ctx,
1632 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001633 &ast->Idents.get(name),
Ed Masted4612ad2014-04-20 13:17:36 +00001634 nullptr,
Pavel Labath67add942015-07-07 10:11:16 +00001635 nullptr,
Greg Clayton8cf05932010-07-22 18:30:50 +00001636 SourceLocation(),
Sean Callanan5b26f272012-02-04 08:49:35 +00001637 /*isForwardDecl,*/
Greg Clayton8cf05932010-07-22 18:30:50 +00001638 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00001639
Jim Ingham379397632012-10-27 02:54:13 +00001640 if (decl && metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001641 SetMetadata(ast, decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00001642
Greg Claytona1e5dc82015-08-11 22:53:00 +00001643 return CompilerType (ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001644}
1645
1646static inline bool
1647BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
1648{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001649 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001650}
1651
Greg Clayton57ee3062013-07-11 22:46:58 +00001652uint32_t
1653ClangASTContext::GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001654{
1655 uint32_t num_bases = 0;
1656 if (cxx_record_decl)
1657 {
1658 if (omit_empty_base_classes)
1659 {
1660 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1661 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1662 base_class != base_class_end;
1663 ++base_class)
1664 {
1665 // Skip empty base classes
1666 if (omit_empty_base_classes)
1667 {
1668 if (BaseSpecifierIsEmpty (base_class))
1669 continue;
1670 }
1671 ++num_bases;
1672 }
1673 }
1674 else
1675 num_bases = cxx_record_decl->getNumBases();
1676 }
1677 return num_bases;
1678}
1679
1680
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001681#pragma mark Namespace Declarations
1682
1683NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00001684ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001685{
Ed Masted4612ad2014-04-20 13:17:36 +00001686 NamespaceDecl *namespace_decl = nullptr;
Greg Clayton9d3d6882011-10-31 23:51:19 +00001687 ASTContext *ast = getASTContext();
1688 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
Ed Masted4612ad2014-04-20 13:17:36 +00001689 if (decl_ctx == nullptr)
Greg Clayton9d3d6882011-10-31 23:51:19 +00001690 decl_ctx = translation_unit_decl;
1691
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001692 if (name)
1693 {
Greg Clayton030a2042011-10-14 21:34:45 +00001694 IdentifierInfo &identifier_info = ast->Idents.get(name);
1695 DeclarationName decl_name (&identifier_info);
1696 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001697 for (NamedDecl *decl : result)
Greg Clayton030a2042011-10-14 21:34:45 +00001698 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001699 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
Greg Clayton030a2042011-10-14 21:34:45 +00001700 if (namespace_decl)
1701 return namespace_decl;
1702 }
1703
Sean Callanan5b26f272012-02-04 08:49:35 +00001704 namespace_decl = NamespaceDecl::Create(*ast,
1705 decl_ctx,
1706 false,
1707 SourceLocation(),
1708 SourceLocation(),
1709 &identifier_info,
Ed Masted4612ad2014-04-20 13:17:36 +00001710 nullptr);
Greg Clayton030a2042011-10-14 21:34:45 +00001711
Greg Clayton9d3d6882011-10-31 23:51:19 +00001712 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001713 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001714 else
1715 {
1716 if (decl_ctx == translation_unit_decl)
1717 {
1718 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1719 if (namespace_decl)
1720 return namespace_decl;
1721
Sean Callanan5b26f272012-02-04 08:49:35 +00001722 namespace_decl = NamespaceDecl::Create(*ast,
1723 decl_ctx,
1724 false,
1725 SourceLocation(),
1726 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001727 nullptr,
1728 nullptr);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001729 translation_unit_decl->setAnonymousNamespace (namespace_decl);
1730 translation_unit_decl->addDecl (namespace_decl);
1731 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
1732 }
1733 else
1734 {
1735 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1736 if (parent_namespace_decl)
1737 {
1738 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1739 if (namespace_decl)
1740 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00001741 namespace_decl = NamespaceDecl::Create(*ast,
1742 decl_ctx,
1743 false,
1744 SourceLocation(),
1745 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001746 nullptr,
1747 nullptr);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001748 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
1749 parent_namespace_decl->addDecl (namespace_decl);
1750 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
1751 }
1752 else
1753 {
1754 // BAD!!!
1755 }
1756 }
1757
1758
1759 if (namespace_decl)
1760 {
1761 // If we make it here, we are creating the anonymous namespace decl
1762 // for the first time, so we need to do the using directive magic
1763 // like SEMA does
1764 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast,
1765 decl_ctx,
1766 SourceLocation(),
1767 SourceLocation(),
1768 NestedNameSpecifierLoc(),
1769 SourceLocation(),
1770 namespace_decl,
1771 decl_ctx);
1772 using_directive_decl->setImplicit();
1773 decl_ctx->addDecl(using_directive_decl);
1774 }
1775 }
1776#ifdef LLDB_CONFIGURATION_DEBUG
1777 VerifyDecl(namespace_decl);
1778#endif
Greg Clayton030a2042011-10-14 21:34:45 +00001779 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001780}
1781
1782
1783#pragma mark Function Types
1784
1785FunctionDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001786ClangASTContext::CreateFunctionDeclaration (DeclContext *decl_ctx,
1787 const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001788 const CompilerType &function_clang_type,
Greg Clayton57ee3062013-07-11 22:46:58 +00001789 int storage,
1790 bool is_inline)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001791{
Ed Masted4612ad2014-04-20 13:17:36 +00001792 FunctionDecl *func_decl = nullptr;
Greg Clayton147e1fa2011-10-14 22:47:18 +00001793 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001794 if (decl_ctx == nullptr)
Greg Clayton147e1fa2011-10-14 22:47:18 +00001795 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001796
Greg Clayton0d551042013-06-28 21:08:47 +00001797
1798 const bool hasWrittenPrototype = true;
1799 const bool isConstexprSpecified = false;
1800
Greg Clayton147e1fa2011-10-14 22:47:18 +00001801 if (name && name[0])
1802 {
1803 func_decl = FunctionDecl::Create (*ast,
1804 decl_ctx,
1805 SourceLocation(),
1806 SourceLocation(),
1807 DeclarationName (&ast->Idents.get(name)),
Greg Claytond8d4a572015-08-11 21:38:15 +00001808 GetQualType(function_clang_type),
Ed Masted4612ad2014-04-20 13:17:36 +00001809 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00001810 (clang::StorageClass)storage,
Greg Clayton0d551042013-06-28 21:08:47 +00001811 is_inline,
1812 hasWrittenPrototype,
1813 isConstexprSpecified);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001814 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00001815 else
1816 {
1817 func_decl = FunctionDecl::Create (*ast,
1818 decl_ctx,
1819 SourceLocation(),
1820 SourceLocation(),
1821 DeclarationName (),
Greg Claytond8d4a572015-08-11 21:38:15 +00001822 GetQualType(function_clang_type),
Ed Masted4612ad2014-04-20 13:17:36 +00001823 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00001824 (clang::StorageClass)storage,
Greg Clayton0d551042013-06-28 21:08:47 +00001825 is_inline,
1826 hasWrittenPrototype,
1827 isConstexprSpecified);
Greg Clayton147e1fa2011-10-14 22:47:18 +00001828 }
1829 if (func_decl)
1830 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001831
1832#ifdef LLDB_CONFIGURATION_DEBUG
1833 VerifyDecl(func_decl);
1834#endif
1835
Greg Clayton147e1fa2011-10-14 22:47:18 +00001836 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001837}
1838
Greg Claytona1e5dc82015-08-11 22:53:00 +00001839CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001840ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001841 const CompilerType& result_type,
1842 const CompilerType *args,
Sean Callananc81256a2010-09-16 20:40:25 +00001843 unsigned num_args,
1844 bool is_variadic,
1845 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001846{
Ed Masted4612ad2014-04-20 13:17:36 +00001847 assert (ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001848 std::vector<QualType> qual_type_args;
1849 for (unsigned i=0; i<num_args; ++i)
Greg Claytond8d4a572015-08-11 21:38:15 +00001850 qual_type_args.push_back (GetQualType(args[i]));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001851
1852 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00001853 FunctionProtoType::ExtProtoInfo proto_info;
1854 proto_info.Variadic = is_variadic;
Sylvestre Ledru186ca7d2014-08-01 12:19:15 +00001855 proto_info.ExceptionSpec = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00001856 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00001857 proto_info.RefQualifier = RQ_None;
Sylvestre Ledru186ca7d2014-08-01 12:19:15 +00001858
Greg Claytona1e5dc82015-08-11 22:53:00 +00001859 return CompilerType (ast, ast->getFunctionType (GetQualType(result_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00001860 qual_type_args,
Greg Claytond8d4a572015-08-11 21:38:15 +00001861 proto_info));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001862}
1863
1864ParmVarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00001865ClangASTContext::CreateParameterDeclaration (const char *name, const CompilerType &param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001866{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001867 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001868 assert (ast != nullptr);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001869 return ParmVarDecl::Create(*ast,
1870 ast->getTranslationUnitDecl(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001871 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00001872 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001873 name && name[0] ? &ast->Idents.get(name) : nullptr,
Greg Claytond8d4a572015-08-11 21:38:15 +00001874 GetQualType(param_type),
Ed Masted4612ad2014-04-20 13:17:36 +00001875 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00001876 (clang::StorageClass)storage,
Ed Masted4612ad2014-04-20 13:17:36 +00001877 nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001878}
1879
1880void
1881ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
1882{
1883 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00001884 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001885}
1886
1887
1888#pragma mark Array Types
1889
Greg Claytona1e5dc82015-08-11 22:53:00 +00001890CompilerType
1891ClangASTContext::CreateArrayType (const CompilerType &element_type,
Greg Clayton1c8ef472013-04-05 23:27:21 +00001892 size_t element_count,
1893 bool is_vector)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001894{
Greg Clayton57ee3062013-07-11 22:46:58 +00001895 if (element_type.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001896 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001897 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001898 assert (ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00001899
Greg Clayton1c8ef472013-04-05 23:27:21 +00001900 if (is_vector)
1901 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001902 return CompilerType (ast, ast->getExtVectorType(GetQualType(element_type), element_count));
Greg Clayton4ef877f2012-12-06 02:33:54 +00001903 }
1904 else
1905 {
Greg Clayton1c8ef472013-04-05 23:27:21 +00001906
1907 llvm::APInt ap_element_count (64, element_count);
1908 if (element_count == 0)
1909 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001910 return CompilerType (ast, ast->getIncompleteArrayType (GetQualType(element_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00001911 ArrayType::Normal,
Greg Claytond8d4a572015-08-11 21:38:15 +00001912 0));
Greg Clayton1c8ef472013-04-05 23:27:21 +00001913 }
1914 else
1915 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001916 return CompilerType (ast, ast->getConstantArrayType (GetQualType(element_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00001917 ap_element_count,
1918 ArrayType::Normal,
Greg Claytond8d4a572015-08-11 21:38:15 +00001919 0));
Greg Clayton1c8ef472013-04-05 23:27:21 +00001920 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00001921 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001922 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001923 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001924}
1925
Greg Claytona1e5dc82015-08-11 22:53:00 +00001926CompilerType
Enrico Granata76b08d52014-10-29 23:08:02 +00001927ClangASTContext::GetOrCreateStructForIdentifier (const ConstString &type_name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001928 const std::initializer_list< std::pair < const char *, CompilerType > >& type_fields,
Enrico Granataa449e862014-10-30 00:53:28 +00001929 bool packed)
Enrico Granata76b08d52014-10-29 23:08:02 +00001930{
Greg Claytona1e5dc82015-08-11 22:53:00 +00001931 CompilerType type;
Enrico Granata76b08d52014-10-29 23:08:02 +00001932 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
1933 return type;
1934 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(), clang::TTK_Struct, lldb::eLanguageTypeC);
Greg Claytond8d4a572015-08-11 21:38:15 +00001935 StartTagDeclarationDefinition(type);
Enrico Granata76b08d52014-10-29 23:08:02 +00001936 for (const auto& field : type_fields)
Greg Claytond8d4a572015-08-11 21:38:15 +00001937 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic, 0);
Enrico Granataa449e862014-10-30 00:53:28 +00001938 if (packed)
Greg Claytond8d4a572015-08-11 21:38:15 +00001939 SetIsPacked(type);
1940 CompleteTagDeclarationDefinition(type);
Enrico Granata76b08d52014-10-29 23:08:02 +00001941 return type;
1942}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001943
1944#pragma mark Enumeration Types
1945
Greg Claytona1e5dc82015-08-11 22:53:00 +00001946CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00001947ClangASTContext::CreateEnumerationType
Greg Claytonca512b32011-01-14 04:54:56 +00001948(
Greg Claytond8d4a572015-08-11 21:38:15 +00001949 const char *name,
1950 DeclContext *decl_ctx,
1951 const Declaration &decl,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001952 const CompilerType &integer_clang_type
Greg Claytond8d4a572015-08-11 21:38:15 +00001953 )
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001954{
1955 // TODO: Do something intelligent with the Declaration object passed in
1956 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00001957 ASTContext *ast = getASTContext();
Greg Claytond8d4a572015-08-11 21:38:15 +00001958
Greg Claytone02b8502010-10-12 04:29:14 +00001959 // TODO: ask about these...
Greg Claytond8d4a572015-08-11 21:38:15 +00001960 // const bool IsScoped = false;
1961 // const bool IsFixed = false;
1962
Greg Clayton6beaaa62011-01-17 03:46:26 +00001963 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00001964 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00001965 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00001966 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001967 name && name[0] ? &ast->Idents.get(name) : nullptr,
1968 nullptr,
Sean Callanan48114472010-12-13 01:26:27 +00001969 false, // IsScoped
1970 false, // IsScopedUsingClassTag
1971 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00001972
1973
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001974 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00001975 {
1976 // TODO: check if we should be setting the promotion type too?
Greg Claytond8d4a572015-08-11 21:38:15 +00001977 enum_decl->setIntegerType(GetQualType(integer_clang_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00001978
1979 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
1980
Greg Claytona1e5dc82015-08-11 22:53:00 +00001981 return CompilerType (ast, ast->getTagDeclType(enum_decl));
Greg Clayton83ff3892010-09-12 23:17:56 +00001982 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001983 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001984}
1985
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001986// Disable this for now since I can't seem to get a nicely formatted float
1987// out of the APFloat class without just getting the float, double or quad
1988// and then using a formatted print on it which defeats the purpose. We ideally
1989// would like to get perfect string values for any kind of float semantics
1990// so we can support remote targets. The code below also requires a patch to
1991// llvm::APInt.
1992//bool
Greg Clayton6beaaa62011-01-17 03:46:26 +00001993//ClangASTContext::ConvertFloatValueToString (ASTContext *ast, clang_type_t clang_type, const uint8_t* bytes, size_t byte_size, int apint_byte_order, std::string &float_str)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001994//{
1995// uint32_t count = 0;
1996// bool is_complex = false;
1997// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
1998// {
1999// unsigned num_bytes_per_float = byte_size / count;
2000// unsigned num_bits_per_float = num_bytes_per_float * 8;
2001//
2002// float_str.clear();
2003// uint32_t i;
2004// for (i=0; i<count; i++)
2005// {
2006// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
2007// bool is_ieee = false;
2008// APFloat ap_float(ap_int, is_ieee);
2009// char s[1024];
2010// unsigned int hex_digits = 0;
2011// bool upper_case = false;
2012//
2013// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
2014// {
2015// if (i > 0)
2016// float_str.append(", ");
2017// float_str.append(s);
2018// if (i == 1 && is_complex)
2019// float_str.append(1, 'i');
2020// }
2021// }
2022// return !float_str.empty();
2023// }
2024// return false;
2025//}
2026
Greg Claytona1e5dc82015-08-11 22:53:00 +00002027CompilerType
Enrico Granatae8bf7492014-08-15 23:00:02 +00002028ClangASTContext::GetIntTypeFromBitSize (clang::ASTContext *ast,
2029 size_t bit_size, bool is_signed)
2030{
2031 if (ast)
2032 {
2033 if (is_signed)
2034 {
2035 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002036 return CompilerType(ast, ast->SignedCharTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002037
2038 if (bit_size == ast->getTypeSize(ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002039 return CompilerType(ast, ast->ShortTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002040
2041 if (bit_size == ast->getTypeSize(ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002042 return CompilerType(ast, ast->IntTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002043
2044 if (bit_size == ast->getTypeSize(ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002045 return CompilerType(ast, ast->LongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002046
2047 if (bit_size == ast->getTypeSize(ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002048 return CompilerType(ast, ast->LongLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002049
2050 if (bit_size == ast->getTypeSize(ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002051 return CompilerType(ast, ast->Int128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002052 }
2053 else
2054 {
2055 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002056 return CompilerType(ast, ast->UnsignedCharTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002057
2058 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002059 return CompilerType(ast, ast->UnsignedShortTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002060
2061 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002062 return CompilerType(ast, ast->UnsignedIntTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002063
2064 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002065 return CompilerType(ast, ast->UnsignedLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002066
2067 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002068 return CompilerType(ast, ast->UnsignedLongLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002069
2070 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002071 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002072 }
2073 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002074 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002075}
2076
Greg Claytona1e5dc82015-08-11 22:53:00 +00002077CompilerType
Enrico Granatae8bf7492014-08-15 23:00:02 +00002078ClangASTContext::GetPointerSizedIntType (clang::ASTContext *ast, bool is_signed)
2079{
2080 if (ast)
2081 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy), is_signed);
Greg Claytona1e5dc82015-08-11 22:53:00 +00002082 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002083}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002084
Greg Claytona1e5dc82015-08-11 22:53:00 +00002085CompilerType
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002086ClangASTContext::GetFloatTypeFromBitSize (clang::ASTContext *ast,
2087 size_t bit_size)
2088{
2089 if (ast)
2090 {
2091 if (bit_size == ast->getTypeSize(ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002092 return CompilerType(ast, ast->FloatTy);
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002093 else if (bit_size == ast->getTypeSize(ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002094 return CompilerType(ast, ast->DoubleTy);
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002095 else if (bit_size == ast->getTypeSize(ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002096 return CompilerType(ast, ast->LongDoubleTy);
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002097 else if (bit_size == ast->getTypeSize(ast->HalfTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002098 return CompilerType(ast, ast->HalfTy);
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002099 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002100 return CompilerType();
Enrico Granata86027e92012-03-24 01:11:14 +00002101}
2102
2103bool
Greg Claytona2721472011-06-25 00:44:06 +00002104ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
2105 clang::Decl *decl)
2106{
2107 if (!decl)
2108 return false;
2109
2110 ExternalASTSource *ast_source = ast->getExternalSource();
2111
2112 if (!ast_source)
2113 return false;
2114
2115 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
2116 {
Greg Clayton219cf312012-03-30 00:51:13 +00002117 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002118 return true;
2119
2120 if (!tag_decl->hasExternalLexicalStorage())
2121 return false;
2122
2123 ast_source->CompleteType(tag_decl);
2124
2125 return !tag_decl->getTypeForDecl()->isIncompleteType();
2126 }
2127 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
2128 {
Sean Callanan5b26f272012-02-04 08:49:35 +00002129 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002130 return true;
2131
2132 if (!objc_interface_decl->hasExternalLexicalStorage())
2133 return false;
2134
2135 ast_source->CompleteType(objc_interface_decl);
2136
Sean Callanan5b26f272012-02-04 08:49:35 +00002137 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00002138 }
2139 else
2140 {
2141 return false;
2142 }
2143}
2144
Sean Callanan60217122012-04-13 00:10:03 +00002145void
Greg Claytond0029442013-03-27 01:48:02 +00002146ClangASTContext::SetMetadataAsUserID (const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002147 user_id_t user_id)
2148{
2149 ClangASTMetadata meta_data;
2150 meta_data.SetUserID (user_id);
2151 SetMetadata (object, meta_data);
2152}
2153
2154void
Sean Callanan60217122012-04-13 00:10:03 +00002155ClangASTContext::SetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002156 const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002157 ClangASTMetadata &metadata)
Sean Callanan60217122012-04-13 00:10:03 +00002158{
2159 ClangExternalASTSourceCommon *external_source =
Sean Callananceeb74e2014-12-06 01:03:30 +00002160 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
Sean Callanan60217122012-04-13 00:10:03 +00002161
2162 if (external_source)
2163 external_source->SetMetadata(object, metadata);
2164}
2165
Jim Ingham379397632012-10-27 02:54:13 +00002166ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +00002167ClangASTContext::GetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002168 const void *object)
Sean Callanan60217122012-04-13 00:10:03 +00002169{
2170 ClangExternalASTSourceCommon *external_source =
Sean Callananceeb74e2014-12-06 01:03:30 +00002171 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
Sean Callanan60217122012-04-13 00:10:03 +00002172
2173 if (external_source && external_source->HasMetadata(object))
2174 return external_source->GetMetadata(object);
2175 else
Ed Masted4612ad2014-04-20 13:17:36 +00002176 return nullptr;
Sean Callanan60217122012-04-13 00:10:03 +00002177}
2178
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002179clang::DeclContext *
2180ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
2181{
Sean Callanana87bee82011-08-19 06:19:25 +00002182 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002183}
2184
2185clang::DeclContext *
2186ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
2187{
Sean Callanana87bee82011-08-19 06:19:25 +00002188 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002189}
2190
Greg Clayton685c88c2012-07-14 00:53:55 +00002191
2192bool
2193ClangASTContext::GetClassMethodInfoForDeclContext (clang::DeclContext *decl_ctx,
2194 lldb::LanguageType &language,
2195 bool &is_instance_method,
2196 ConstString &language_object_name)
2197{
2198 language_object_name.Clear();
2199 language = eLanguageTypeUnknown;
2200 is_instance_method = false;
2201
2202 if (decl_ctx)
2203 {
2204 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
2205 {
2206 if (method_decl->isStatic())
2207 {
2208 is_instance_method = false;
2209 }
2210 else
2211 {
2212 language_object_name.SetCString("this");
2213 is_instance_method = true;
2214 }
2215 language = eLanguageTypeC_plus_plus;
2216 return true;
2217 }
2218 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
2219 {
2220 // Both static and instance methods have a "self" object in objective C
2221 language_object_name.SetCString("self");
2222 if (method_decl->isInstanceMethod())
2223 {
2224 is_instance_method = true;
2225 }
2226 else
2227 {
2228 is_instance_method = false;
2229 }
2230 language = eLanguageTypeObjC;
2231 return true;
2232 }
Jim Ingham379397632012-10-27 02:54:13 +00002233 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
2234 {
Greg Claytond0029442013-03-27 01:48:02 +00002235 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), function_decl);
Jim Ingham379397632012-10-27 02:54:13 +00002236 if (metadata && metadata->HasObjectPtr())
2237 {
2238 language_object_name.SetCString (metadata->GetObjectPtrName());
2239 language = eLanguageTypeObjC;
2240 is_instance_method = true;
2241 }
2242 return true;
2243 }
Greg Clayton685c88c2012-07-14 00:53:55 +00002244 }
2245 return false;
2246}
2247
Greg Claytond8d4a572015-08-11 21:38:15 +00002248
2249bool
2250ClangASTContext::SetTagTypeKind (clang::QualType tag_qual_type, int kind) const
2251{
2252 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2253 if (clang_type)
2254 {
2255 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2256 if (tag_type)
2257 {
2258 clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2259 if (tag_decl)
2260 {
2261 tag_decl->setTagKind ((clang::TagDecl::TagKind)kind);
2262 return true;
2263 }
2264 }
2265 }
2266 return false;
2267}
2268
2269
2270bool
2271ClangASTContext::SetDefaultAccessForRecordFields (clang::RecordDecl* record_decl,
2272 int default_accessibility,
2273 int *assigned_accessibilities,
2274 size_t num_assigned_accessibilities)
2275{
2276 if (record_decl)
2277 {
2278 uint32_t field_idx;
2279 clang::RecordDecl::field_iterator field, field_end;
2280 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2281 field != field_end;
2282 ++field, ++field_idx)
2283 {
2284 // If no accessibility was assigned, assign the correct one
2285 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2286 field->setAccess ((clang::AccessSpecifier)default_accessibility);
2287 }
2288 return true;
2289 }
2290 return false;
2291}
2292
2293clang::DeclContext *
2294ClangASTContext::GetDeclContextForType (clang::QualType type)
2295{
2296 if (type.isNull())
2297 return nullptr;
2298
2299 clang::QualType qual_type = type.getCanonicalType();
2300 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2301 switch (type_class)
2302 {
2303 case clang::Type::UnaryTransform: break;
2304 case clang::Type::FunctionNoProto: break;
2305 case clang::Type::FunctionProto: break;
2306 case clang::Type::IncompleteArray: break;
2307 case clang::Type::VariableArray: break;
2308 case clang::Type::ConstantArray: break;
2309 case clang::Type::DependentSizedArray: break;
2310 case clang::Type::ExtVector: break;
2311 case clang::Type::DependentSizedExtVector: break;
2312 case clang::Type::Vector: break;
2313 case clang::Type::Builtin: break;
2314 case clang::Type::BlockPointer: break;
2315 case clang::Type::Pointer: break;
2316 case clang::Type::LValueReference: break;
2317 case clang::Type::RValueReference: break;
2318 case clang::Type::MemberPointer: break;
2319 case clang::Type::Complex: break;
2320 case clang::Type::ObjCObject: break;
2321 case clang::Type::ObjCInterface: return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())->getInterface();
2322 case clang::Type::ObjCObjectPointer: return GetDeclContextForType (llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
2323 case clang::Type::Record: return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2324 case clang::Type::Enum: return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2325 case clang::Type::Typedef: return GetDeclContextForType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
2326 case clang::Type::Elaborated: return GetDeclContextForType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2327 case clang::Type::Paren: return GetDeclContextForType (llvm::cast<clang::ParenType>(qual_type)->desugar());
2328 case clang::Type::TypeOfExpr: break;
2329 case clang::Type::TypeOf: break;
2330 case clang::Type::Decltype: break;
2331 //case clang::Type::QualifiedName: break;
2332 case clang::Type::TemplateSpecialization: break;
2333 case clang::Type::DependentTemplateSpecialization: break;
2334 case clang::Type::TemplateTypeParm: break;
2335 case clang::Type::SubstTemplateTypeParm: break;
2336 case clang::Type::SubstTemplateTypeParmPack:break;
2337 case clang::Type::PackExpansion: break;
2338 case clang::Type::UnresolvedUsing: break;
2339 case clang::Type::Attributed: break;
2340 case clang::Type::Auto: break;
2341 case clang::Type::InjectedClassName: break;
2342 case clang::Type::DependentName: break;
2343 case clang::Type::Atomic: break;
2344 case clang::Type::Adjusted: break;
2345
2346 // pointer type decayed from an array or function type.
2347 case clang::Type::Decayed: break;
2348 }
2349 // No DeclContext in this type...
2350 return nullptr;
2351}
2352
2353static bool
2354GetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion = true)
2355{
2356 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2357 switch (type_class)
2358 {
2359 case clang::Type::ConstantArray:
2360 case clang::Type::IncompleteArray:
2361 case clang::Type::VariableArray:
2362 {
2363 const clang::ArrayType *array_type = llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2364
2365 if (array_type)
2366 return GetCompleteQualType (ast, array_type->getElementType(), allow_completion);
2367 }
2368 break;
2369
2370 case clang::Type::Record:
2371 case clang::Type::Enum:
2372 {
2373 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2374 if (tag_type)
2375 {
2376 clang::TagDecl *tag_decl = tag_type->getDecl();
2377 if (tag_decl)
2378 {
2379 if (tag_decl->isCompleteDefinition())
2380 return true;
2381
2382 if (!allow_completion)
2383 return false;
2384
2385 if (tag_decl->hasExternalLexicalStorage())
2386 {
2387 if (ast)
2388 {
2389 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2390 if (external_ast_source)
2391 {
2392 external_ast_source->CompleteType(tag_decl);
2393 return !tag_type->isIncompleteType();
2394 }
2395 }
2396 }
2397 return false;
2398 }
2399 }
2400
2401 }
2402 break;
2403
2404 case clang::Type::ObjCObject:
2405 case clang::Type::ObjCInterface:
2406 {
2407 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2408 if (objc_class_type)
2409 {
2410 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2411 // We currently can't complete objective C types through the newly added ASTContext
2412 // because it only supports TagDecl objects right now...
2413 if (class_interface_decl)
2414 {
2415 if (class_interface_decl->getDefinition())
2416 return true;
2417
2418 if (!allow_completion)
2419 return false;
2420
2421 if (class_interface_decl->hasExternalLexicalStorage())
2422 {
2423 if (ast)
2424 {
2425 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2426 if (external_ast_source)
2427 {
2428 external_ast_source->CompleteType (class_interface_decl);
2429 return !objc_class_type->isIncompleteType();
2430 }
2431 }
2432 }
2433 return false;
2434 }
2435 }
2436 }
2437 break;
2438
2439 case clang::Type::Typedef:
2440 return GetCompleteQualType (ast, llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType(), allow_completion);
2441
2442 case clang::Type::Elaborated:
2443 return GetCompleteQualType (ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(), allow_completion);
2444
2445 case clang::Type::Paren:
2446 return GetCompleteQualType (ast, llvm::cast<clang::ParenType>(qual_type)->desugar(), allow_completion);
2447
2448 default:
2449 break;
2450 }
2451
2452 return true;
2453}
2454
2455static clang::ObjCIvarDecl::AccessControl
2456ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
2457{
2458 switch (access)
2459 {
2460 case eAccessNone: return clang::ObjCIvarDecl::None;
2461 case eAccessPublic: return clang::ObjCIvarDecl::Public;
2462 case eAccessPrivate: return clang::ObjCIvarDecl::Private;
2463 case eAccessProtected: return clang::ObjCIvarDecl::Protected;
2464 case eAccessPackage: return clang::ObjCIvarDecl::Package;
2465 }
2466 return clang::ObjCIvarDecl::None;
2467}
2468
2469
2470//----------------------------------------------------------------------
2471// Tests
2472//----------------------------------------------------------------------
2473
2474bool
2475ClangASTContext::IsAggregateType (void* type)
2476{
2477 clang::QualType qual_type (GetCanonicalQualType(type));
2478
2479 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2480 switch (type_class)
2481 {
2482 case clang::Type::IncompleteArray:
2483 case clang::Type::VariableArray:
2484 case clang::Type::ConstantArray:
2485 case clang::Type::ExtVector:
2486 case clang::Type::Vector:
2487 case clang::Type::Record:
2488 case clang::Type::ObjCObject:
2489 case clang::Type::ObjCInterface:
2490 return true;
2491 case clang::Type::Elaborated:
2492 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2493 case clang::Type::Typedef:
2494 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2495 case clang::Type::Paren:
2496 return IsAggregateType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2497 default:
2498 break;
2499 }
2500 // The clang type does have a value
2501 return false;
2502}
2503
2504bool
2505ClangASTContext::IsArrayType (void* type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002506 CompilerType *element_type_ptr,
Greg Claytond8d4a572015-08-11 21:38:15 +00002507 uint64_t *size,
2508 bool *is_incomplete)
2509{
2510 clang::QualType qual_type (GetCanonicalQualType(type));
2511
2512 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2513 switch (type_class)
2514 {
2515 default:
2516 break;
2517
2518 case clang::Type::ConstantArray:
2519 if (element_type_ptr)
2520 element_type_ptr->SetClangType (getASTContext(), llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
2521 if (size)
2522 *size = llvm::cast<clang::ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
2523 return true;
2524
2525 case clang::Type::IncompleteArray:
2526 if (element_type_ptr)
2527 element_type_ptr->SetClangType (getASTContext(), llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2528 if (size)
2529 *size = 0;
2530 if (is_incomplete)
2531 *is_incomplete = true;
2532 return true;
2533
2534 case clang::Type::VariableArray:
2535 if (element_type_ptr)
2536 element_type_ptr->SetClangType (getASTContext(), llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2537 if (size)
2538 *size = 0;
2539 return true;
2540
2541 case clang::Type::DependentSizedArray:
2542 if (element_type_ptr)
2543 element_type_ptr->SetClangType (getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)->getElementType());
2544 if (size)
2545 *size = 0;
2546 return true;
2547
2548 case clang::Type::Typedef:
2549 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
2550 element_type_ptr,
2551 size,
2552 is_incomplete);
2553 case clang::Type::Elaborated:
2554 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2555 element_type_ptr,
2556 size,
2557 is_incomplete);
2558 case clang::Type::Paren:
2559 return IsArrayType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2560 element_type_ptr,
2561 size,
2562 is_incomplete);
2563 }
2564 if (element_type_ptr)
2565 element_type_ptr->Clear();
2566 if (size)
2567 *size = 0;
2568 if (is_incomplete)
2569 *is_incomplete = false;
2570 return 0;
2571}
2572
2573bool
2574ClangASTContext::IsVectorType (void* type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002575 CompilerType *element_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00002576 uint64_t *size)
2577{
2578 clang::QualType qual_type (GetCanonicalQualType(type));
2579
2580 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2581 switch (type_class)
2582 {
2583 case clang::Type::Vector:
2584 {
2585 const clang::VectorType *vector_type = qual_type->getAs<clang::VectorType>();
2586 if (vector_type)
2587 {
2588 if (size)
2589 *size = vector_type->getNumElements();
2590 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002591 *element_type = CompilerType(getASTContext(), vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002592 }
2593 return true;
2594 }
2595 break;
2596 case clang::Type::ExtVector:
2597 {
2598 const clang::ExtVectorType *ext_vector_type = qual_type->getAs<clang::ExtVectorType>();
2599 if (ext_vector_type)
2600 {
2601 if (size)
2602 *size = ext_vector_type->getNumElements();
2603 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002604 *element_type = CompilerType(getASTContext(), ext_vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002605 }
2606 return true;
2607 }
2608 default:
2609 break;
2610 }
2611 return false;
2612}
2613
2614bool
2615ClangASTContext::IsRuntimeGeneratedType (void* type)
2616{
2617 clang::DeclContext* decl_ctx = ClangASTContext::GetASTContext(getASTContext())->GetDeclContextForType(GetQualType(type));
2618 if (!decl_ctx)
2619 return false;
2620
2621 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2622 return false;
2623
2624 clang::ObjCInterfaceDecl *result_iface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2625
2626 ClangASTMetadata* ast_metadata = ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2627 if (!ast_metadata)
2628 return false;
2629 return (ast_metadata->GetISAPtr() != 0);
2630}
2631
2632bool
2633ClangASTContext::IsCharType (void* type)
2634{
2635 return GetQualType(type).getUnqualifiedType()->isCharType();
2636}
2637
2638
2639bool
2640ClangASTContext::IsCompleteType (void* type)
2641{
2642 const bool allow_completion = false;
2643 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
2644}
2645
2646bool
2647ClangASTContext::IsConst(void* type)
2648{
2649 return GetQualType(type).isConstQualified();
2650}
2651
2652bool
2653ClangASTContext::IsCStringType (void* type, uint32_t &length)
2654{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002655 CompilerType pointee_or_element_clang_type;
Greg Claytond8d4a572015-08-11 21:38:15 +00002656 length = 0;
2657 Flags type_flags (GetTypeInfo (type, &pointee_or_element_clang_type));
2658
2659 if (!pointee_or_element_clang_type.IsValid())
2660 return false;
2661
2662 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
2663 {
2664 if (pointee_or_element_clang_type.IsCharType())
2665 {
2666 if (type_flags.Test (eTypeIsArray))
2667 {
2668 // We know the size of the array and it could be a C string
2669 // since it is an array of characters
2670 length = llvm::cast<clang::ConstantArrayType>(GetCanonicalQualType(type).getTypePtr())->getSize().getLimitedValue();
2671 }
2672 return true;
2673
2674 }
2675 }
2676 return false;
2677}
2678
2679bool
2680ClangASTContext::IsFunctionType (void* type, bool *is_variadic_ptr)
2681{
2682 if (type)
2683 {
2684 clang::QualType qual_type (GetCanonicalQualType(type));
2685
2686 if (qual_type->isFunctionType())
2687 {
2688 if (is_variadic_ptr)
2689 {
2690 const clang::FunctionProtoType *function_proto_type = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2691 if (function_proto_type)
2692 *is_variadic_ptr = function_proto_type->isVariadic();
2693 else
2694 *is_variadic_ptr = false;
2695 }
2696 return true;
2697 }
2698
2699 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2700 switch (type_class)
2701 {
2702 default:
2703 break;
2704 case clang::Type::Typedef:
2705 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), nullptr);
2706 case clang::Type::Elaborated:
2707 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), nullptr);
2708 case clang::Type::Paren:
2709 return IsFunctionType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), nullptr);
2710 case clang::Type::LValueReference:
2711 case clang::Type::RValueReference:
2712 {
2713 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2714 if (reference_type)
2715 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(), nullptr);
2716 }
2717 break;
2718 }
2719 }
2720 return false;
2721}
2722
2723// Used to detect "Homogeneous Floating-point Aggregates"
2724uint32_t
Greg Claytona1e5dc82015-08-11 22:53:00 +00002725ClangASTContext::IsHomogeneousAggregate (void* type, CompilerType* base_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00002726{
2727 if (!type)
2728 return 0;
2729
2730 clang::QualType qual_type(GetCanonicalQualType(type));
2731 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2732 switch (type_class)
2733 {
2734 case clang::Type::Record:
2735 if (GetCompleteType (type))
2736 {
2737 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2738 if (cxx_record_decl)
2739 {
2740 if (cxx_record_decl->getNumBases() ||
2741 cxx_record_decl->isDynamicClass())
2742 return 0;
2743 }
2744 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
2745 if (record_type)
2746 {
2747 const clang::RecordDecl *record_decl = record_type->getDecl();
2748 if (record_decl)
2749 {
2750 // We are looking for a structure that contains only floating point types
2751 clang::RecordDecl::field_iterator field_pos, field_end = record_decl->field_end();
2752 uint32_t num_fields = 0;
2753 bool is_hva = false;
2754 bool is_hfa = false;
2755 clang::QualType base_qual_type;
2756 for (field_pos = record_decl->field_begin(); field_pos != field_end; ++field_pos)
2757 {
2758 clang::QualType field_qual_type = field_pos->getType();
2759 if (field_qual_type->isFloatingType())
2760 {
2761 if (field_qual_type->isComplexType())
2762 return 0;
2763 else
2764 {
2765 if (num_fields == 0)
2766 base_qual_type = field_qual_type;
2767 else
2768 {
2769 if (is_hva)
2770 return 0;
2771 is_hfa = true;
2772 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
2773 return 0;
2774 }
2775 }
2776 }
2777 else if (field_qual_type->isVectorType() || field_qual_type->isExtVectorType())
2778 {
2779 const clang::VectorType *array = field_qual_type.getTypePtr()->getAs<clang::VectorType>();
2780 if (array && array->getNumElements() <= 4)
2781 {
2782 if (num_fields == 0)
2783 base_qual_type = array->getElementType();
2784 else
2785 {
2786 if (is_hfa)
2787 return 0;
2788 is_hva = true;
2789 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
2790 return 0;
2791 }
2792 }
2793 else
2794 return 0;
2795 }
2796 else
2797 return 0;
2798 ++num_fields;
2799 }
2800 if (base_type_ptr)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002801 *base_type_ptr = CompilerType (getASTContext(), base_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00002802 return num_fields;
2803 }
2804 }
2805 }
2806 break;
2807
2808 case clang::Type::Typedef:
2809 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), base_type_ptr);
2810
2811 case clang::Type::Elaborated:
2812 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), base_type_ptr);
2813 default:
2814 break;
2815 }
2816 return 0;
2817}
2818
2819size_t
2820ClangASTContext::GetNumberOfFunctionArguments (void* type)
2821{
2822 if (type)
2823 {
2824 clang::QualType qual_type (GetCanonicalQualType(type));
2825 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2826 if (func)
2827 return func->getNumParams();
2828 }
2829 return 0;
2830}
2831
Greg Claytona1e5dc82015-08-11 22:53:00 +00002832CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00002833ClangASTContext::GetFunctionArgumentAtIndex (void* type, const size_t index)
2834{
2835 if (type)
2836 {
2837 clang::QualType qual_type (GetCanonicalQualType(type));
2838 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2839 if (func)
2840 {
2841 if (index < func->getNumParams())
Greg Claytona1e5dc82015-08-11 22:53:00 +00002842 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00002843 }
2844 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002845 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00002846}
2847
2848bool
2849ClangASTContext::IsFunctionPointerType (void* type)
2850{
2851 if (type)
2852 {
2853 clang::QualType qual_type (GetCanonicalQualType(type));
2854
2855 if (qual_type->isFunctionPointerType())
2856 return true;
2857
2858 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2859 switch (type_class)
2860 {
2861 default:
2862 break;
2863 case clang::Type::Typedef:
2864 return IsFunctionPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2865 case clang::Type::Elaborated:
2866 return IsFunctionPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2867 case clang::Type::Paren:
2868 return IsFunctionPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2869
2870 case clang::Type::LValueReference:
2871 case clang::Type::RValueReference:
2872 {
2873 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2874 if (reference_type)
2875 return IsFunctionPointerType(reference_type->getPointeeType().getAsOpaquePtr());
2876 }
2877 break;
2878 }
2879 }
2880 return false;
2881
2882}
2883
2884bool
2885ClangASTContext::IsIntegerType (void* type, bool &is_signed)
2886{
2887 if (!type)
2888 return false;
2889
2890 clang::QualType qual_type (GetCanonicalQualType(type));
2891 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
2892
2893 if (builtin_type)
2894 {
2895 if (builtin_type->isInteger())
2896 {
2897 is_signed = builtin_type->isSignedInteger();
2898 return true;
2899 }
2900 }
2901
2902 return false;
2903}
2904
2905bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00002906ClangASTContext::IsPointerType (void* type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002907{
2908 if (type)
2909 {
2910 clang::QualType qual_type (GetCanonicalQualType(type));
2911 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2912 switch (type_class)
2913 {
2914 case clang::Type::Builtin:
2915 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
2916 {
2917 default:
2918 break;
2919 case clang::BuiltinType::ObjCId:
2920 case clang::BuiltinType::ObjCClass:
2921 return true;
2922 }
2923 return false;
2924 case clang::Type::ObjCObjectPointer:
2925 if (pointee_type)
2926 pointee_type->SetClangType (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
2927 return true;
2928 case clang::Type::BlockPointer:
2929 if (pointee_type)
2930 pointee_type->SetClangType (getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
2931 return true;
2932 case clang::Type::Pointer:
2933 if (pointee_type)
2934 pointee_type->SetClangType (getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
2935 return true;
2936 case clang::Type::MemberPointer:
2937 if (pointee_type)
2938 pointee_type->SetClangType (getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
2939 return true;
2940 case clang::Type::Typedef:
2941 return IsPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
2942 case clang::Type::Elaborated:
2943 return IsPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
2944 case clang::Type::Paren:
2945 return IsPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
2946 default:
2947 break;
2948 }
2949 }
2950 if (pointee_type)
2951 pointee_type->Clear();
2952 return false;
2953}
2954
2955
2956bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00002957ClangASTContext::IsPointerOrReferenceType (void* type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002958{
2959 if (type)
2960 {
2961 clang::QualType qual_type (GetCanonicalQualType(type));
2962 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2963 switch (type_class)
2964 {
2965 case clang::Type::Builtin:
2966 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
2967 {
2968 default:
2969 break;
2970 case clang::BuiltinType::ObjCId:
2971 case clang::BuiltinType::ObjCClass:
2972 return true;
2973 }
2974 return false;
2975 case clang::Type::ObjCObjectPointer:
2976 if (pointee_type)
2977 pointee_type->SetClangType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
2978 return true;
2979 case clang::Type::BlockPointer:
2980 if (pointee_type)
2981 pointee_type->SetClangType(getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
2982 return true;
2983 case clang::Type::Pointer:
2984 if (pointee_type)
2985 pointee_type->SetClangType(getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
2986 return true;
2987 case clang::Type::MemberPointer:
2988 if (pointee_type)
2989 pointee_type->SetClangType(getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
2990 return true;
2991 case clang::Type::LValueReference:
2992 if (pointee_type)
2993 pointee_type->SetClangType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
2994 return true;
2995 case clang::Type::RValueReference:
2996 if (pointee_type)
2997 pointee_type->SetClangType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
2998 return true;
2999 case clang::Type::Typedef:
3000 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
3001 case clang::Type::Elaborated:
3002 return IsPointerOrReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
3003 case clang::Type::Paren:
3004 return IsPointerOrReferenceType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
3005 default:
3006 break;
3007 }
3008 }
3009 if (pointee_type)
3010 pointee_type->Clear();
3011 return false;
3012}
3013
3014
3015bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003016ClangASTContext::IsReferenceType (void* type, CompilerType *pointee_type, bool* is_rvalue)
Greg Claytond8d4a572015-08-11 21:38:15 +00003017{
3018 if (type)
3019 {
3020 clang::QualType qual_type (GetCanonicalQualType(type));
3021 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3022
3023 switch (type_class)
3024 {
3025 case clang::Type::LValueReference:
3026 if (pointee_type)
3027 pointee_type->SetClangType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3028 if (is_rvalue)
3029 *is_rvalue = false;
3030 return true;
3031 case clang::Type::RValueReference:
3032 if (pointee_type)
3033 pointee_type->SetClangType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3034 if (is_rvalue)
3035 *is_rvalue = true;
3036 return true;
3037 case clang::Type::Typedef:
3038 return IsReferenceType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type, is_rvalue);
3039 case clang::Type::Elaborated:
3040 return IsReferenceType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type, is_rvalue);
3041 case clang::Type::Paren:
3042 return IsReferenceType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type, is_rvalue);
3043
3044 default:
3045 break;
3046 }
3047 }
3048 if (pointee_type)
3049 pointee_type->Clear();
3050 return false;
3051}
3052
3053bool
3054ClangASTContext::IsFloatingPointType (void* type, uint32_t &count, bool &is_complex)
3055{
3056 if (type)
3057 {
3058 clang::QualType qual_type (GetCanonicalQualType(type));
3059
3060 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal()))
3061 {
3062 clang::BuiltinType::Kind kind = BT->getKind();
3063 if (kind >= clang::BuiltinType::Float && kind <= clang::BuiltinType::LongDouble)
3064 {
3065 count = 1;
3066 is_complex = false;
3067 return true;
3068 }
3069 }
3070 else if (const clang::ComplexType *CT = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal()))
3071 {
3072 if (IsFloatingPointType (CT->getElementType().getAsOpaquePtr(), count, is_complex))
3073 {
3074 count = 2;
3075 is_complex = true;
3076 return true;
3077 }
3078 }
3079 else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal()))
3080 {
3081 if (IsFloatingPointType (VT->getElementType().getAsOpaquePtr(), count, is_complex))
3082 {
3083 count = VT->getNumElements();
3084 is_complex = false;
3085 return true;
3086 }
3087 }
3088 }
3089 count = 0;
3090 is_complex = false;
3091 return false;
3092}
3093
3094
3095bool
3096ClangASTContext::IsDefined(void* type)
3097{
3098 if (!type)
3099 return false;
3100
3101 clang::QualType qual_type(GetQualType(type));
3102 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3103 if (tag_type)
3104 {
3105 clang::TagDecl *tag_decl = tag_type->getDecl();
3106 if (tag_decl)
3107 return tag_decl->isCompleteDefinition();
3108 return false;
3109 }
3110 else
3111 {
3112 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3113 if (objc_class_type)
3114 {
3115 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3116 if (class_interface_decl)
3117 return class_interface_decl->getDefinition() != nullptr;
3118 return false;
3119 }
3120 }
3121 return true;
3122}
3123
3124bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003125ClangASTContext::IsObjCClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003126{
3127 if (type)
3128 {
3129 clang::QualType qual_type (GetCanonicalQualType(type));
3130
3131 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3132
3133 if (obj_pointer_type)
3134 return obj_pointer_type->isObjCClassType();
3135 }
3136 return false;
3137}
3138
3139bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003140ClangASTContext::IsObjCObjectOrInterfaceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003141{
3142 if (type)
3143 return GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3144 return false;
3145}
3146
3147bool
3148ClangASTContext::IsPolymorphicClass (void* type)
3149{
3150 if (type)
3151 {
3152 clang::QualType qual_type(GetCanonicalQualType(type));
3153 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3154 switch (type_class)
3155 {
3156 case clang::Type::Record:
3157 if (GetCompleteType(type))
3158 {
3159 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3160 const clang::RecordDecl *record_decl = record_type->getDecl();
3161 if (record_decl)
3162 {
3163 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3164 if (cxx_record_decl)
3165 return cxx_record_decl->isPolymorphic();
3166 }
3167 }
3168 break;
3169
3170 default:
3171 break;
3172 }
3173 }
3174 return false;
3175}
3176
3177bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003178ClangASTContext::IsPossibleDynamicType (void* type, CompilerType *dynamic_pointee_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00003179 bool check_cplusplus,
3180 bool check_objc)
3181{
3182 clang::QualType pointee_qual_type;
3183 if (type)
3184 {
3185 clang::QualType qual_type (GetCanonicalQualType(type));
3186 bool success = false;
3187 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3188 switch (type_class)
3189 {
3190 case clang::Type::Builtin:
3191 if (check_objc && llvm::cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
3192 {
3193 if (dynamic_pointee_type)
3194 dynamic_pointee_type->SetClangType(this, type);
3195 return true;
3196 }
3197 break;
3198
3199 case clang::Type::ObjCObjectPointer:
3200 if (check_objc)
3201 {
3202 if (dynamic_pointee_type)
3203 dynamic_pointee_type->SetClangType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
3204 return true;
3205 }
3206 break;
3207
3208 case clang::Type::Pointer:
3209 pointee_qual_type = llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3210 success = true;
3211 break;
3212
3213 case clang::Type::LValueReference:
3214 case clang::Type::RValueReference:
3215 pointee_qual_type = llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3216 success = true;
3217 break;
3218
3219 case clang::Type::Typedef:
3220 return IsPossibleDynamicType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3221 dynamic_pointee_type,
3222 check_cplusplus,
3223 check_objc);
3224
3225 case clang::Type::Elaborated:
3226 return IsPossibleDynamicType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3227 dynamic_pointee_type,
3228 check_cplusplus,
3229 check_objc);
3230
3231 case clang::Type::Paren:
3232 return IsPossibleDynamicType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3233 dynamic_pointee_type,
3234 check_cplusplus,
3235 check_objc);
3236 default:
3237 break;
3238 }
3239
3240 if (success)
3241 {
3242 // Check to make sure what we are pointing too is a possible dynamic C++ type
3243 // We currently accept any "void *" (in case we have a class that has been
3244 // watered down to an opaque pointer) and virtual C++ classes.
3245 const clang::Type::TypeClass pointee_type_class = pointee_qual_type.getCanonicalType()->getTypeClass();
3246 switch (pointee_type_class)
3247 {
3248 case clang::Type::Builtin:
3249 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind())
3250 {
3251 case clang::BuiltinType::UnknownAny:
3252 case clang::BuiltinType::Void:
3253 if (dynamic_pointee_type)
3254 dynamic_pointee_type->SetClangType(getASTContext(), pointee_qual_type);
3255 return true;
3256
3257 case clang::BuiltinType::NullPtr:
3258 case clang::BuiltinType::Bool:
3259 case clang::BuiltinType::Char_U:
3260 case clang::BuiltinType::UChar:
3261 case clang::BuiltinType::WChar_U:
3262 case clang::BuiltinType::Char16:
3263 case clang::BuiltinType::Char32:
3264 case clang::BuiltinType::UShort:
3265 case clang::BuiltinType::UInt:
3266 case clang::BuiltinType::ULong:
3267 case clang::BuiltinType::ULongLong:
3268 case clang::BuiltinType::UInt128:
3269 case clang::BuiltinType::Char_S:
3270 case clang::BuiltinType::SChar:
3271 case clang::BuiltinType::WChar_S:
3272 case clang::BuiltinType::Short:
3273 case clang::BuiltinType::Int:
3274 case clang::BuiltinType::Long:
3275 case clang::BuiltinType::LongLong:
3276 case clang::BuiltinType::Int128:
3277 case clang::BuiltinType::Float:
3278 case clang::BuiltinType::Double:
3279 case clang::BuiltinType::LongDouble:
3280 case clang::BuiltinType::Dependent:
3281 case clang::BuiltinType::Overload:
3282 case clang::BuiltinType::ObjCId:
3283 case clang::BuiltinType::ObjCClass:
3284 case clang::BuiltinType::ObjCSel:
3285 case clang::BuiltinType::BoundMember:
3286 case clang::BuiltinType::Half:
3287 case clang::BuiltinType::ARCUnbridgedCast:
3288 case clang::BuiltinType::PseudoObject:
3289 case clang::BuiltinType::BuiltinFn:
3290 case clang::BuiltinType::OCLEvent:
3291 case clang::BuiltinType::OCLImage1d:
3292 case clang::BuiltinType::OCLImage1dArray:
3293 case clang::BuiltinType::OCLImage1dBuffer:
3294 case clang::BuiltinType::OCLImage2d:
3295 case clang::BuiltinType::OCLImage2dArray:
3296 case clang::BuiltinType::OCLImage3d:
3297 case clang::BuiltinType::OCLSampler:
3298 break;
3299 }
3300 break;
3301
3302 case clang::Type::Record:
3303 if (check_cplusplus)
3304 {
3305 clang::CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
3306 if (cxx_record_decl)
3307 {
3308 bool is_complete = cxx_record_decl->isCompleteDefinition();
3309
3310 if (is_complete)
3311 success = cxx_record_decl->isDynamicClass();
3312 else
3313 {
3314 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), cxx_record_decl);
3315 if (metadata)
3316 success = metadata->GetIsDynamicCXXType();
3317 else
3318 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00003319 is_complete = CompilerType(getASTContext(), pointee_qual_type).GetCompleteType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003320 if (is_complete)
3321 success = cxx_record_decl->isDynamicClass();
3322 else
3323 success = false;
3324 }
3325 }
3326
3327 if (success)
3328 {
3329 if (dynamic_pointee_type)
3330 dynamic_pointee_type->SetClangType(getASTContext(), pointee_qual_type);
3331 return true;
3332 }
3333 }
3334 }
3335 break;
3336
3337 case clang::Type::ObjCObject:
3338 case clang::Type::ObjCInterface:
3339 if (check_objc)
3340 {
3341 if (dynamic_pointee_type)
3342 dynamic_pointee_type->SetClangType(getASTContext(), pointee_qual_type);
3343 return true;
3344 }
3345 break;
3346
3347 default:
3348 break;
3349 }
3350 }
3351 }
3352 if (dynamic_pointee_type)
3353 dynamic_pointee_type->Clear();
3354 return false;
3355}
3356
3357
3358bool
3359ClangASTContext::IsScalarType (void* type)
3360{
3361 if (!type)
3362 return false;
3363
3364 return (GetTypeInfo (type, nullptr) & eTypeIsScalar) != 0;
3365}
3366
3367bool
3368ClangASTContext::IsTypedefType (void* type)
3369{
3370 if (!type)
3371 return false;
3372 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3373}
3374
3375bool
3376ClangASTContext::IsVoidType (void* type)
3377{
3378 if (!type)
3379 return false;
3380 return GetCanonicalQualType(type)->isVoidType();
3381}
3382
3383bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003384ClangASTContext::GetCXXClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003385{
3386 if (type)
3387 {
3388 clang::QualType qual_type (GetCanonicalQualType(type));
3389
3390 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3391 if (cxx_record_decl)
3392 {
3393 class_name.assign (cxx_record_decl->getIdentifier()->getNameStart());
3394 return true;
3395 }
3396 }
3397 class_name.clear();
3398 return false;
3399}
3400
3401
3402bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003403ClangASTContext::IsCXXClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003404{
3405 if (!type)
3406 return false;
3407
3408 clang::QualType qual_type (GetCanonicalQualType(type));
3409 if (qual_type->getAsCXXRecordDecl() != nullptr)
3410 return true;
3411 return false;
3412}
3413
3414bool
3415ClangASTContext::IsBeingDefined (void* type)
3416{
3417 if (!type)
3418 return false;
3419 clang::QualType qual_type (GetCanonicalQualType(type));
3420 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3421 if (tag_type)
3422 return tag_type->isBeingDefined();
3423 return false;
3424}
3425
3426bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003427ClangASTContext::IsObjCObjectPointerType (const CompilerType& type, CompilerType *class_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003428{
3429 if (!type)
3430 return false;
3431
3432 clang::QualType qual_type (GetCanonicalQualType(type));
3433
3434 if (qual_type->isObjCObjectPointerType())
3435 {
3436 if (class_type_ptr)
3437 {
3438 if (!qual_type->isObjCClassType() &&
3439 !qual_type->isObjCIdType())
3440 {
3441 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3442 if (obj_pointer_type == nullptr)
3443 class_type_ptr->Clear();
3444 else
3445 class_type_ptr->SetClangType (type.GetTypeSystem(), clang::QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr());
3446 }
3447 }
3448 return true;
3449 }
3450 if (class_type_ptr)
3451 class_type_ptr->Clear();
3452 return false;
3453}
3454
3455bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003456ClangASTContext::GetObjCClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003457{
3458 if (!type)
3459 return false;
3460
3461 clang::QualType qual_type (GetCanonicalQualType(type));
3462
3463 const clang::ObjCObjectType *object_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3464 if (object_type)
3465 {
3466 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3467 if (interface)
3468 {
3469 class_name = interface->getNameAsString();
3470 return true;
3471 }
3472 }
3473 return false;
3474}
3475
3476
3477//----------------------------------------------------------------------
3478// Type Completion
3479//----------------------------------------------------------------------
3480
3481bool
3482ClangASTContext::GetCompleteType (void* type)
3483{
3484 if (!type)
3485 return false;
3486 const bool allow_completion = true;
3487 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
3488}
3489
3490ConstString
3491ClangASTContext::GetTypeName (void* type)
3492{
3493 std::string type_name;
3494 if (type)
3495 {
3496 clang::PrintingPolicy printing_policy (getASTContext()->getPrintingPolicy());
3497 clang::QualType qual_type(GetQualType(type));
3498 printing_policy.SuppressTagKeyword = true;
3499 printing_policy.LangOpts.WChar = true;
3500 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
3501 if (typedef_type)
3502 {
3503 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3504 type_name = typedef_decl->getQualifiedNameAsString();
3505 }
3506 else
3507 {
3508 type_name = qual_type.getAsString(printing_policy);
3509 }
3510 }
3511 return ConstString(type_name);
3512}
3513
3514uint32_t
Greg Claytona1e5dc82015-08-11 22:53:00 +00003515ClangASTContext::GetTypeInfo (void* type, CompilerType *pointee_or_element_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003516{
3517 if (!type)
3518 return 0;
3519
3520 if (pointee_or_element_clang_type)
3521 pointee_or_element_clang_type->Clear();
3522
3523 clang::QualType qual_type (GetQualType(type));
3524
3525 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3526 switch (type_class)
3527 {
3528 case clang::Type::Builtin:
3529 {
3530 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3531
3532 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3533 switch (builtin_type->getKind())
3534 {
3535 case clang::BuiltinType::ObjCId:
3536 case clang::BuiltinType::ObjCClass:
3537 if (pointee_or_element_clang_type)
3538 pointee_or_element_clang_type->SetClangType(getASTContext(), getASTContext()->ObjCBuiltinClassTy);
3539 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3540 break;
3541
3542 case clang::BuiltinType::ObjCSel:
3543 if (pointee_or_element_clang_type)
3544 pointee_or_element_clang_type->SetClangType(getASTContext(), getASTContext()->CharTy);
3545 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3546 break;
3547
3548 case clang::BuiltinType::Bool:
3549 case clang::BuiltinType::Char_U:
3550 case clang::BuiltinType::UChar:
3551 case clang::BuiltinType::WChar_U:
3552 case clang::BuiltinType::Char16:
3553 case clang::BuiltinType::Char32:
3554 case clang::BuiltinType::UShort:
3555 case clang::BuiltinType::UInt:
3556 case clang::BuiltinType::ULong:
3557 case clang::BuiltinType::ULongLong:
3558 case clang::BuiltinType::UInt128:
3559 case clang::BuiltinType::Char_S:
3560 case clang::BuiltinType::SChar:
3561 case clang::BuiltinType::WChar_S:
3562 case clang::BuiltinType::Short:
3563 case clang::BuiltinType::Int:
3564 case clang::BuiltinType::Long:
3565 case clang::BuiltinType::LongLong:
3566 case clang::BuiltinType::Int128:
3567 case clang::BuiltinType::Float:
3568 case clang::BuiltinType::Double:
3569 case clang::BuiltinType::LongDouble:
3570 builtin_type_flags |= eTypeIsScalar;
3571 if (builtin_type->isInteger())
3572 {
3573 builtin_type_flags |= eTypeIsInteger;
3574 if (builtin_type->isSignedInteger())
3575 builtin_type_flags |= eTypeIsSigned;
3576 }
3577 else if (builtin_type->isFloatingPoint())
3578 builtin_type_flags |= eTypeIsFloat;
3579 break;
3580 default:
3581 break;
3582 }
3583 return builtin_type_flags;
3584 }
3585
3586 case clang::Type::BlockPointer:
3587 if (pointee_or_element_clang_type)
3588 pointee_or_element_clang_type->SetClangType(getASTContext(), qual_type->getPointeeType());
3589 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3590
3591 case clang::Type::Complex:
3592 {
3593 uint32_t complex_type_flags = eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3594 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal());
3595 if (complex_type)
3596 {
3597 clang::QualType complex_element_type (complex_type->getElementType());
3598 if (complex_element_type->isIntegerType())
3599 complex_type_flags |= eTypeIsFloat;
3600 else if (complex_element_type->isFloatingType())
3601 complex_type_flags |= eTypeIsInteger;
3602 }
3603 return complex_type_flags;
3604 }
3605 break;
3606
3607 case clang::Type::ConstantArray:
3608 case clang::Type::DependentSizedArray:
3609 case clang::Type::IncompleteArray:
3610 case clang::Type::VariableArray:
3611 if (pointee_or_element_clang_type)
3612 pointee_or_element_clang_type->SetClangType(getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())->getElementType());
3613 return eTypeHasChildren | eTypeIsArray;
3614
3615 case clang::Type::DependentName: return 0;
3616 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
3617 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
3618 case clang::Type::Decltype: return 0;
3619
3620 case clang::Type::Enum:
3621 if (pointee_or_element_clang_type)
3622 pointee_or_element_clang_type->SetClangType(getASTContext(), llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
3623 return eTypeIsEnumeration | eTypeHasValue;
3624
3625 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003626 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003627 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003628 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003629
3630 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
3631 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
3632 case clang::Type::InjectedClassName: return 0;
3633
3634 case clang::Type::LValueReference:
3635 case clang::Type::RValueReference:
3636 if (pointee_or_element_clang_type)
3637 pointee_or_element_clang_type->SetClangType(getASTContext(), llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())->getPointeeType());
3638 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
3639
3640 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
3641
3642 case clang::Type::ObjCObjectPointer:
3643 if (pointee_or_element_clang_type)
3644 pointee_or_element_clang_type->SetClangType(getASTContext(), qual_type->getPointeeType());
3645 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
3646
3647 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3648 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3649
3650 case clang::Type::Pointer:
3651 if (pointee_or_element_clang_type)
3652 pointee_or_element_clang_type->SetClangType(getASTContext(), qual_type->getPointeeType());
3653 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
3654
3655 case clang::Type::Record:
3656 if (qual_type->getAsCXXRecordDecl())
3657 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
3658 else
3659 return eTypeHasChildren | eTypeIsStructUnion;
3660 break;
3661 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
3662 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
3663 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
3664
3665 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003666 return eTypeIsTypedef | CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003667 case clang::Type::TypeOfExpr: return 0;
3668 case clang::Type::TypeOf: return 0;
3669 case clang::Type::UnresolvedUsing: return 0;
3670
3671 case clang::Type::ExtVector:
3672 case clang::Type::Vector:
3673 {
3674 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
3675 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal());
3676 if (vector_type)
3677 {
3678 if (vector_type->isIntegerType())
3679 vector_type_flags |= eTypeIsFloat;
3680 else if (vector_type->isFloatingType())
3681 vector_type_flags |= eTypeIsInteger;
3682 }
3683 return vector_type_flags;
3684 }
3685 default: return 0;
3686 }
3687 return 0;
3688}
3689
3690
3691
3692lldb::LanguageType
3693ClangASTContext::GetMinimumLanguage (void* type)
3694{
3695 if (!type)
3696 return lldb::eLanguageTypeC;
3697
3698 // If the type is a reference, then resolve it to what it refers to first:
3699 clang::QualType qual_type (GetCanonicalQualType(type).getNonReferenceType());
3700 if (qual_type->isAnyPointerType())
3701 {
3702 if (qual_type->isObjCObjectPointerType())
3703 return lldb::eLanguageTypeObjC;
3704
3705 clang::QualType pointee_type (qual_type->getPointeeType());
3706 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
3707 return lldb::eLanguageTypeC_plus_plus;
3708 if (pointee_type->isObjCObjectOrInterfaceType())
3709 return lldb::eLanguageTypeObjC;
3710 if (pointee_type->isObjCClassType())
3711 return lldb::eLanguageTypeObjC;
3712 if (pointee_type.getTypePtr() == getASTContext()->ObjCBuiltinIdTy.getTypePtr())
3713 return lldb::eLanguageTypeObjC;
3714 }
3715 else
3716 {
3717 if (qual_type->isObjCObjectOrInterfaceType())
3718 return lldb::eLanguageTypeObjC;
3719 if (qual_type->getAsCXXRecordDecl())
3720 return lldb::eLanguageTypeC_plus_plus;
3721 switch (qual_type->getTypeClass())
3722 {
3723 default:
3724 break;
3725 case clang::Type::Builtin:
3726 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
3727 {
3728 default:
3729 case clang::BuiltinType::Void:
3730 case clang::BuiltinType::Bool:
3731 case clang::BuiltinType::Char_U:
3732 case clang::BuiltinType::UChar:
3733 case clang::BuiltinType::WChar_U:
3734 case clang::BuiltinType::Char16:
3735 case clang::BuiltinType::Char32:
3736 case clang::BuiltinType::UShort:
3737 case clang::BuiltinType::UInt:
3738 case clang::BuiltinType::ULong:
3739 case clang::BuiltinType::ULongLong:
3740 case clang::BuiltinType::UInt128:
3741 case clang::BuiltinType::Char_S:
3742 case clang::BuiltinType::SChar:
3743 case clang::BuiltinType::WChar_S:
3744 case clang::BuiltinType::Short:
3745 case clang::BuiltinType::Int:
3746 case clang::BuiltinType::Long:
3747 case clang::BuiltinType::LongLong:
3748 case clang::BuiltinType::Int128:
3749 case clang::BuiltinType::Float:
3750 case clang::BuiltinType::Double:
3751 case clang::BuiltinType::LongDouble:
3752 break;
3753
3754 case clang::BuiltinType::NullPtr:
3755 return eLanguageTypeC_plus_plus;
3756
3757 case clang::BuiltinType::ObjCId:
3758 case clang::BuiltinType::ObjCClass:
3759 case clang::BuiltinType::ObjCSel:
3760 return eLanguageTypeObjC;
3761
3762 case clang::BuiltinType::Dependent:
3763 case clang::BuiltinType::Overload:
3764 case clang::BuiltinType::BoundMember:
3765 case clang::BuiltinType::UnknownAny:
3766 break;
3767 }
3768 break;
3769 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003770 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetMinimumLanguage();
Greg Claytond8d4a572015-08-11 21:38:15 +00003771 }
3772 }
3773 return lldb::eLanguageTypeC;
3774}
3775
3776lldb::TypeClass
3777ClangASTContext::GetTypeClass (void* type)
3778{
3779 if (!type)
3780 return lldb::eTypeClassInvalid;
3781
3782 clang::QualType qual_type(GetQualType(type));
3783
3784 switch (qual_type->getTypeClass())
3785 {
3786 case clang::Type::UnaryTransform: break;
3787 case clang::Type::FunctionNoProto: return lldb::eTypeClassFunction;
3788 case clang::Type::FunctionProto: return lldb::eTypeClassFunction;
3789 case clang::Type::IncompleteArray: return lldb::eTypeClassArray;
3790 case clang::Type::VariableArray: return lldb::eTypeClassArray;
3791 case clang::Type::ConstantArray: return lldb::eTypeClassArray;
3792 case clang::Type::DependentSizedArray: return lldb::eTypeClassArray;
3793 case clang::Type::DependentSizedExtVector: return lldb::eTypeClassVector;
3794 case clang::Type::ExtVector: return lldb::eTypeClassVector;
3795 case clang::Type::Vector: return lldb::eTypeClassVector;
3796 case clang::Type::Builtin: return lldb::eTypeClassBuiltin;
3797 case clang::Type::ObjCObjectPointer: return lldb::eTypeClassObjCObjectPointer;
3798 case clang::Type::BlockPointer: return lldb::eTypeClassBlockPointer;
3799 case clang::Type::Pointer: return lldb::eTypeClassPointer;
3800 case clang::Type::LValueReference: return lldb::eTypeClassReference;
3801 case clang::Type::RValueReference: return lldb::eTypeClassReference;
3802 case clang::Type::MemberPointer: return lldb::eTypeClassMemberPointer;
3803 case clang::Type::Complex:
3804 if (qual_type->isComplexType())
3805 return lldb::eTypeClassComplexFloat;
3806 else
3807 return lldb::eTypeClassComplexInteger;
3808 case clang::Type::ObjCObject: return lldb::eTypeClassObjCObject;
3809 case clang::Type::ObjCInterface: return lldb::eTypeClassObjCInterface;
3810 case clang::Type::Record:
3811 {
3812 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3813 const clang::RecordDecl *record_decl = record_type->getDecl();
3814 if (record_decl->isUnion())
3815 return lldb::eTypeClassUnion;
3816 else if (record_decl->isStruct())
3817 return lldb::eTypeClassStruct;
3818 else
3819 return lldb::eTypeClassClass;
3820 }
3821 break;
3822 case clang::Type::Enum: return lldb::eTypeClassEnumeration;
3823 case clang::Type::Typedef: return lldb::eTypeClassTypedef;
3824 case clang::Type::UnresolvedUsing: break;
3825 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003826 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00003827 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003828 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00003829
3830 case clang::Type::Attributed: break;
3831 case clang::Type::TemplateTypeParm: break;
3832 case clang::Type::SubstTemplateTypeParm: break;
3833 case clang::Type::SubstTemplateTypeParmPack:break;
3834 case clang::Type::Auto: break;
3835 case clang::Type::InjectedClassName: break;
3836 case clang::Type::DependentName: break;
3837 case clang::Type::DependentTemplateSpecialization: break;
3838 case clang::Type::PackExpansion: break;
3839
3840 case clang::Type::TypeOfExpr: break;
3841 case clang::Type::TypeOf: break;
3842 case clang::Type::Decltype: break;
3843 case clang::Type::TemplateSpecialization: break;
3844 case clang::Type::Atomic: break;
3845
3846 // pointer type decayed from an array or function type.
3847 case clang::Type::Decayed: break;
3848 case clang::Type::Adjusted: break;
3849 }
3850 // We don't know hot to display this type...
3851 return lldb::eTypeClassOther;
3852
3853}
3854
3855unsigned
3856ClangASTContext::GetTypeQualifiers(void* type)
3857{
3858 if (type)
3859 return GetQualType(type).getQualifiers().getCVRQualifiers();
3860 return 0;
3861}
3862
3863//----------------------------------------------------------------------
3864// Creating related types
3865//----------------------------------------------------------------------
3866
Greg Claytona1e5dc82015-08-11 22:53:00 +00003867CompilerType
3868ClangASTContext::AddConstModifier (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003869{
3870 if (type && type.GetTypeSystem()->AsClangASTContext())
3871 {
3872 clang::QualType result(GetQualType(type));
3873 result.addConst();
Greg Claytona1e5dc82015-08-11 22:53:00 +00003874 return CompilerType (type.GetTypeSystem(), result.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003875 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003876 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003877}
3878
Greg Claytona1e5dc82015-08-11 22:53:00 +00003879CompilerType
3880ClangASTContext::AddRestrictModifier (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003881{
3882 if (type && type.GetTypeSystem()->AsClangASTContext())
3883 {
3884 clang::QualType result(GetQualType(type));
3885 result.getQualifiers().setRestrict (true);
Greg Claytona1e5dc82015-08-11 22:53:00 +00003886 return CompilerType (type.GetTypeSystem(), result.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003887 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003888 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003889}
3890
Greg Claytona1e5dc82015-08-11 22:53:00 +00003891CompilerType
3892ClangASTContext::AddVolatileModifier (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003893{
3894 if (type && type.GetTypeSystem()->AsClangASTContext())
3895 {
3896 clang::QualType result(GetQualType(type));
3897 result.getQualifiers().setVolatile (true);
Greg Claytona1e5dc82015-08-11 22:53:00 +00003898 return CompilerType (type.GetTypeSystem(), result.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003899 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003900 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003901}
3902
Greg Claytona1e5dc82015-08-11 22:53:00 +00003903CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003904ClangASTContext::GetArrayElementType (void* type, uint64_t *stride)
3905{
3906 if (type)
3907 {
3908 clang::QualType qual_type(GetCanonicalQualType(type));
3909
3910 const clang::Type *array_eletype = qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
3911
3912 if (!array_eletype)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003913 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003914
Greg Claytona1e5dc82015-08-11 22:53:00 +00003915 CompilerType element_type (getASTContext(), array_eletype->getCanonicalTypeUnqualified());
Greg Claytond8d4a572015-08-11 21:38:15 +00003916
3917 // TODO: the real stride will be >= this value.. find the real one!
3918 if (stride)
3919 *stride = element_type.GetByteSize(nullptr);
3920
3921 return element_type;
3922
3923 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003924 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003925}
3926
Greg Claytona1e5dc82015-08-11 22:53:00 +00003927CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003928ClangASTContext::GetCanonicalType (void* type)
3929{
3930 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003931 return CompilerType (getASTContext(), GetCanonicalQualType(type));
3932 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003933}
3934
3935static clang::QualType
3936GetFullyUnqualifiedType_Impl (clang::ASTContext *ast, clang::QualType qual_type)
3937{
3938 if (qual_type->isPointerType())
3939 qual_type = ast->getPointerType(GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
3940 else
3941 qual_type = qual_type.getUnqualifiedType();
3942 qual_type.removeLocalConst();
3943 qual_type.removeLocalRestrict();
3944 qual_type.removeLocalVolatile();
3945 return qual_type;
3946}
3947
Greg Claytona1e5dc82015-08-11 22:53:00 +00003948CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003949ClangASTContext::GetFullyUnqualifiedType (void* type)
3950{
3951 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003952 return CompilerType(getASTContext(), GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
3953 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003954}
3955
3956
3957int
3958ClangASTContext::GetFunctionArgumentCount (void* type)
3959{
3960 if (type)
3961 {
3962 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
3963 if (func)
3964 return func->getNumParams();
3965 }
3966 return -1;
3967}
3968
Greg Claytona1e5dc82015-08-11 22:53:00 +00003969CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003970ClangASTContext::GetFunctionArgumentTypeAtIndex (void* type, size_t idx)
3971{
3972 if (type)
3973 {
3974 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
3975 if (func)
3976 {
3977 const uint32_t num_args = func->getNumParams();
3978 if (idx < num_args)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003979 return CompilerType(getASTContext(), func->getParamType(idx));
Greg Claytond8d4a572015-08-11 21:38:15 +00003980 }
3981 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003982 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003983}
3984
Greg Claytona1e5dc82015-08-11 22:53:00 +00003985CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003986ClangASTContext::GetFunctionReturnType (void* type)
3987{
3988 if (type)
3989 {
3990 clang::QualType qual_type(GetCanonicalQualType(type));
3991 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3992 if (func)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003993 return CompilerType(getASTContext(), func->getReturnType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003994 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003995 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003996}
3997
3998size_t
3999ClangASTContext::GetNumMemberFunctions (void* type)
4000{
4001 size_t num_functions = 0;
4002 if (type)
4003 {
4004 clang::QualType qual_type(GetCanonicalQualType(type));
4005 switch (qual_type->getTypeClass()) {
4006 case clang::Type::Record:
4007 if (GetCompleteQualType (getASTContext(), qual_type))
4008 {
4009 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4010 const clang::RecordDecl *record_decl = record_type->getDecl();
4011 assert(record_decl);
4012 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4013 if (cxx_record_decl)
4014 num_functions = std::distance(cxx_record_decl->method_begin(), cxx_record_decl->method_end());
4015 }
4016 break;
4017
4018 case clang::Type::ObjCObjectPointer:
4019 if (GetCompleteType(type))
4020 {
4021 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4022 if (objc_class_type)
4023 {
4024 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4025 if (class_interface_decl)
4026 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
4027 }
4028 }
4029 break;
4030
4031 case clang::Type::ObjCObject:
4032 case clang::Type::ObjCInterface:
4033 if (GetCompleteType(type))
4034 {
4035 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4036 if (objc_class_type)
4037 {
4038 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4039 if (class_interface_decl)
4040 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
4041 }
4042 }
4043 break;
4044
4045
4046 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004047 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004048
4049 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004050 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004051
4052 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004053 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004054
4055 default:
4056 break;
4057 }
4058 }
4059 return num_functions;
4060}
4061
4062TypeMemberFunctionImpl
4063ClangASTContext::GetMemberFunctionAtIndex (void* type, size_t idx)
4064{
4065 std::string name("");
4066 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
Greg Claytona1e5dc82015-08-11 22:53:00 +00004067 CompilerType clang_type{};
Greg Claytond8d4a572015-08-11 21:38:15 +00004068 clang::ObjCMethodDecl *method_decl(nullptr);
4069 if (type)
4070 {
4071 clang::QualType qual_type(GetCanonicalQualType(type));
4072 switch (qual_type->getTypeClass()) {
4073 case clang::Type::Record:
4074 if (GetCompleteQualType (getASTContext(), qual_type))
4075 {
4076 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4077 const clang::RecordDecl *record_decl = record_type->getDecl();
4078 assert(record_decl);
4079 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4080 if (cxx_record_decl)
4081 {
4082 auto method_iter = cxx_record_decl->method_begin();
4083 auto method_end = cxx_record_decl->method_end();
4084 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4085 {
4086 std::advance(method_iter, idx);
4087 auto method_decl = method_iter->getCanonicalDecl();
4088 if (method_decl)
4089 {
4090 if (!method_decl->getName().empty())
4091 name.assign(method_decl->getName().data());
4092 else
4093 name.clear();
4094 if (method_decl->isStatic())
4095 kind = lldb::eMemberFunctionKindStaticMethod;
4096 else if (llvm::isa<clang::CXXConstructorDecl>(method_decl))
4097 kind = lldb::eMemberFunctionKindConstructor;
4098 else if (llvm::isa<clang::CXXDestructorDecl>(method_decl))
4099 kind = lldb::eMemberFunctionKindDestructor;
4100 else
4101 kind = lldb::eMemberFunctionKindInstanceMethod;
Greg Claytona1e5dc82015-08-11 22:53:00 +00004102 clang_type = CompilerType(getASTContext(),method_decl->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004103 }
4104 }
4105 }
4106 }
4107 break;
4108
4109 case clang::Type::ObjCObjectPointer:
4110 if (GetCompleteType(type))
4111 {
4112 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4113 if (objc_class_type)
4114 {
4115 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4116 if (class_interface_decl)
4117 {
4118 auto method_iter = class_interface_decl->meth_begin();
4119 auto method_end = class_interface_decl->meth_end();
4120 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4121 {
4122 std::advance(method_iter, idx);
4123 method_decl = method_iter->getCanonicalDecl();
4124 if (method_decl)
4125 {
4126 name = method_decl->getSelector().getAsString();
4127 if (method_decl->isClassMethod())
4128 kind = lldb::eMemberFunctionKindStaticMethod;
4129 else
4130 kind = lldb::eMemberFunctionKindInstanceMethod;
4131 }
4132 }
4133 }
4134 }
4135 }
4136 break;
4137
4138 case clang::Type::ObjCObject:
4139 case clang::Type::ObjCInterface:
4140 if (GetCompleteType(type))
4141 {
4142 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4143 if (objc_class_type)
4144 {
4145 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4146 if (class_interface_decl)
4147 {
4148 auto method_iter = class_interface_decl->meth_begin();
4149 auto method_end = class_interface_decl->meth_end();
4150 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4151 {
4152 std::advance(method_iter, idx);
4153 method_decl = method_iter->getCanonicalDecl();
4154 if (method_decl)
4155 {
4156 name = method_decl->getSelector().getAsString();
4157 if (method_decl->isClassMethod())
4158 kind = lldb::eMemberFunctionKindStaticMethod;
4159 else
4160 kind = lldb::eMemberFunctionKindInstanceMethod;
4161 }
4162 }
4163 }
4164 }
4165 }
4166 break;
4167
4168 case clang::Type::Typedef:
4169 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx);
4170
4171 case clang::Type::Elaborated:
4172 return GetMemberFunctionAtIndex(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx);
4173
4174 case clang::Type::Paren:
4175 return GetMemberFunctionAtIndex(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx);
4176
4177 default:
4178 break;
4179 }
4180 }
4181
4182 if (kind == eMemberFunctionKindUnknown)
4183 return TypeMemberFunctionImpl();
4184 if (method_decl)
4185 return TypeMemberFunctionImpl(method_decl, name, kind);
4186 if (type)
4187 return TypeMemberFunctionImpl(clang_type, name, kind);
4188
4189 return TypeMemberFunctionImpl();
4190}
4191
Greg Claytona1e5dc82015-08-11 22:53:00 +00004192CompilerType
4193ClangASTContext::GetLValueReferenceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004194{
4195 if (type)
4196 {
4197 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
4198 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004199 return CompilerType(ast->getASTContext(), ast->getASTContext()->getLValueReferenceType(GetQualType(type)));
Greg Claytond8d4a572015-08-11 21:38:15 +00004200 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004201 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004202}
4203
Greg Claytona1e5dc82015-08-11 22:53:00 +00004204CompilerType
4205ClangASTContext::GetRValueReferenceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004206{
4207 if (type)
4208 {
4209 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
4210 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004211 return CompilerType(ast->getASTContext(), ast->getASTContext()->getRValueReferenceType(GetQualType(type)));
Greg Claytond8d4a572015-08-11 21:38:15 +00004212 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004213 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004214}
4215
Greg Claytona1e5dc82015-08-11 22:53:00 +00004216CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004217ClangASTContext::GetNonReferenceType (void* type)
4218{
4219 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004220 return CompilerType(getASTContext(), GetQualType(type).getNonReferenceType());
4221 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004222}
4223
Greg Claytona1e5dc82015-08-11 22:53:00 +00004224CompilerType
4225ClangASTContext::CreateTypedefType (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00004226 const char *typedef_name,
4227 clang::DeclContext *decl_ctx)
4228{
4229 if (type && typedef_name && typedef_name[0])
4230 {
4231 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
4232 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004233 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004234 clang::ASTContext* clang_ast = ast->getASTContext();
4235 clang::QualType qual_type (GetQualType(type));
4236 if (decl_ctx == nullptr)
4237 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4238 clang::TypedefDecl *decl = clang::TypedefDecl::Create (*clang_ast,
4239 decl_ctx,
4240 clang::SourceLocation(),
4241 clang::SourceLocation(),
4242 &clang_ast->Idents.get(typedef_name),
4243 clang_ast->getTrivialTypeSourceInfo(qual_type));
4244
4245 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4246
4247 // Get a uniqued clang::QualType for the typedef decl type
Greg Claytona1e5dc82015-08-11 22:53:00 +00004248 return CompilerType (clang_ast, clang_ast->getTypedefType (decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00004249 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004250 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004251
4252}
4253
Greg Claytona1e5dc82015-08-11 22:53:00 +00004254CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004255ClangASTContext::GetPointeeType (void* type)
4256{
4257 if (type)
4258 {
4259 clang::QualType qual_type(GetQualType(type));
Greg Claytona1e5dc82015-08-11 22:53:00 +00004260 return CompilerType (getASTContext(), qual_type.getTypePtr()->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004261 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004262 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004263}
4264
Greg Claytona1e5dc82015-08-11 22:53:00 +00004265CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004266ClangASTContext::GetPointerType (void* type)
4267{
4268 if (type)
4269 {
4270 clang::QualType qual_type (GetQualType(type));
4271
4272 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4273 switch (type_class)
4274 {
4275 case clang::Type::ObjCObject:
4276 case clang::Type::ObjCInterface:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004277 return CompilerType(getASTContext(), getASTContext()->getObjCObjectPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004278
4279 default:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004280 return CompilerType(getASTContext(), getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004281 }
4282 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004283 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004284}
4285
Greg Claytona1e5dc82015-08-11 22:53:00 +00004286CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004287ClangASTContext::GetTypedefedType (void* type)
4288{
4289 if (type)
4290 {
4291 const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4292 if (typedef_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004293 return CompilerType (getASTContext(), typedef_type->getDecl()->getUnderlyingType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004294 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004295 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004296}
4297
Greg Claytona1e5dc82015-08-11 22:53:00 +00004298CompilerType
4299ClangASTContext::RemoveFastQualifiers (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004300{
4301 if (type && type.GetTypeSystem()->AsClangASTContext())
4302 {
4303 clang::QualType qual_type(GetQualType(type));
4304 qual_type.getQualifiers().removeFastQualifiers();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004305 return CompilerType (type.GetTypeSystem(), qual_type.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00004306 }
4307 return type;
4308}
4309
4310
4311//----------------------------------------------------------------------
4312// Create related types using the current type's AST
4313//----------------------------------------------------------------------
4314
Greg Claytona1e5dc82015-08-11 22:53:00 +00004315CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004316ClangASTContext::GetBasicTypeFromAST (void* type, lldb::BasicType basic_type)
4317{
4318 if (type)
4319 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytona1e5dc82015-08-11 22:53:00 +00004320 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004321}
4322//----------------------------------------------------------------------
4323// Exploring the type
4324//----------------------------------------------------------------------
4325
4326uint64_t
4327ClangASTContext::GetBitSize (void* type, ExecutionContextScope *exe_scope)
4328{
4329 if (GetCompleteType (type))
4330 {
4331 clang::QualType qual_type(GetCanonicalQualType(type));
4332 switch (qual_type->getTypeClass())
4333 {
4334 case clang::Type::ObjCInterface:
4335 case clang::Type::ObjCObject:
4336 {
4337 ExecutionContext exe_ctx (exe_scope);
4338 Process *process = exe_ctx.GetProcessPtr();
4339 if (process)
4340 {
4341 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4342 if (objc_runtime)
4343 {
4344 uint64_t bit_size = 0;
Greg Claytona1e5dc82015-08-11 22:53:00 +00004345 if (objc_runtime->GetTypeBitSize(CompilerType(getASTContext(), qual_type), bit_size))
Greg Claytond8d4a572015-08-11 21:38:15 +00004346 return bit_size;
4347 }
4348 }
4349 else
4350 {
4351 static bool g_printed = false;
4352 if (!g_printed)
4353 {
4354 StreamString s;
4355 DumpTypeDescription(&s);
4356
4357 llvm::outs() << "warning: trying to determine the size of type ";
4358 llvm::outs() << s.GetString() << "\n";
4359 llvm::outs() << "without a valid ExecutionContext. this is not reliable. please file a bug against LLDB.\n";
4360 llvm::outs() << "backtrace:\n";
4361 llvm::sys::PrintStackTrace(llvm::outs());
4362 llvm::outs() << "\n";
4363 g_printed = true;
4364 }
4365 }
4366 }
4367 // fallthrough
4368 default:
4369 const uint32_t bit_size = getASTContext()->getTypeSize (qual_type);
4370 if (bit_size == 0)
4371 {
4372 if (qual_type->isIncompleteArrayType())
4373 return getASTContext()->getTypeSize (qual_type->getArrayElementTypeNoTypeQual()->getCanonicalTypeUnqualified());
4374 }
4375 if (qual_type->isObjCObjectOrInterfaceType())
4376 return bit_size + getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy);
4377 return bit_size;
4378 }
4379 }
4380 return 0;
4381}
4382
4383size_t
4384ClangASTContext::GetTypeBitAlign (void* type)
4385{
4386 if (GetCompleteType(type))
4387 return getASTContext()->getTypeAlign(GetQualType(type));
4388 return 0;
4389}
4390
4391
4392lldb::Encoding
4393ClangASTContext::GetEncoding (void* type, uint64_t &count)
4394{
4395 if (!type)
4396 return lldb::eEncodingInvalid;
4397
4398 count = 1;
4399 clang::QualType qual_type(GetCanonicalQualType(type));
4400
4401 switch (qual_type->getTypeClass())
4402 {
4403 case clang::Type::UnaryTransform:
4404 break;
4405
4406 case clang::Type::FunctionNoProto:
4407 case clang::Type::FunctionProto:
4408 break;
4409
4410 case clang::Type::IncompleteArray:
4411 case clang::Type::VariableArray:
4412 break;
4413
4414 case clang::Type::ConstantArray:
4415 break;
4416
4417 case clang::Type::ExtVector:
4418 case clang::Type::Vector:
4419 // TODO: Set this to more than one???
4420 break;
4421
4422 case clang::Type::Builtin:
4423 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4424 {
4425 default: assert(0 && "Unknown builtin type!");
4426 case clang::BuiltinType::Void:
4427 break;
4428
4429 case clang::BuiltinType::Bool:
4430 case clang::BuiltinType::Char_S:
4431 case clang::BuiltinType::SChar:
4432 case clang::BuiltinType::WChar_S:
4433 case clang::BuiltinType::Char16:
4434 case clang::BuiltinType::Char32:
4435 case clang::BuiltinType::Short:
4436 case clang::BuiltinType::Int:
4437 case clang::BuiltinType::Long:
4438 case clang::BuiltinType::LongLong:
4439 case clang::BuiltinType::Int128: return lldb::eEncodingSint;
4440
4441 case clang::BuiltinType::Char_U:
4442 case clang::BuiltinType::UChar:
4443 case clang::BuiltinType::WChar_U:
4444 case clang::BuiltinType::UShort:
4445 case clang::BuiltinType::UInt:
4446 case clang::BuiltinType::ULong:
4447 case clang::BuiltinType::ULongLong:
4448 case clang::BuiltinType::UInt128: return lldb::eEncodingUint;
4449
4450 case clang::BuiltinType::Float:
4451 case clang::BuiltinType::Double:
4452 case clang::BuiltinType::LongDouble: return lldb::eEncodingIEEE754;
4453
4454 case clang::BuiltinType::ObjCClass:
4455 case clang::BuiltinType::ObjCId:
4456 case clang::BuiltinType::ObjCSel: return lldb::eEncodingUint;
4457
4458 case clang::BuiltinType::NullPtr: return lldb::eEncodingUint;
4459
4460 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4461 case clang::BuiltinType::Kind::BoundMember:
4462 case clang::BuiltinType::Kind::BuiltinFn:
4463 case clang::BuiltinType::Kind::Dependent:
4464 case clang::BuiltinType::Kind::Half:
4465 case clang::BuiltinType::Kind::OCLEvent:
4466 case clang::BuiltinType::Kind::OCLImage1d:
4467 case clang::BuiltinType::Kind::OCLImage1dArray:
4468 case clang::BuiltinType::Kind::OCLImage1dBuffer:
4469 case clang::BuiltinType::Kind::OCLImage2d:
4470 case clang::BuiltinType::Kind::OCLImage2dArray:
4471 case clang::BuiltinType::Kind::OCLImage3d:
4472 case clang::BuiltinType::Kind::OCLSampler:
4473 case clang::BuiltinType::Kind::Overload:
4474 case clang::BuiltinType::Kind::PseudoObject:
4475 case clang::BuiltinType::Kind::UnknownAny:
4476 break;
4477 }
4478 break;
4479 // All pointer types are represented as unsigned integer encodings.
4480 // We may nee to add a eEncodingPointer if we ever need to know the
4481 // difference
4482 case clang::Type::ObjCObjectPointer:
4483 case clang::Type::BlockPointer:
4484 case clang::Type::Pointer:
4485 case clang::Type::LValueReference:
4486 case clang::Type::RValueReference:
4487 case clang::Type::MemberPointer: return lldb::eEncodingUint;
4488 case clang::Type::Complex:
4489 {
4490 lldb::Encoding encoding = lldb::eEncodingIEEE754;
4491 if (qual_type->isComplexType())
4492 encoding = lldb::eEncodingIEEE754;
4493 else
4494 {
4495 const clang::ComplexType *complex_type = qual_type->getAsComplexIntegerType();
4496 if (complex_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004497 encoding = CompilerType(getASTContext(), complex_type->getElementType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004498 else
4499 encoding = lldb::eEncodingSint;
4500 }
4501 count = 2;
4502 return encoding;
4503 }
4504
4505 case clang::Type::ObjCInterface: break;
4506 case clang::Type::Record: break;
4507 case clang::Type::Enum: return lldb::eEncodingSint;
4508 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004509 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004510
4511 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004512 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004513
4514 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004515 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004516
4517 case clang::Type::DependentSizedArray:
4518 case clang::Type::DependentSizedExtVector:
4519 case clang::Type::UnresolvedUsing:
4520 case clang::Type::Attributed:
4521 case clang::Type::TemplateTypeParm:
4522 case clang::Type::SubstTemplateTypeParm:
4523 case clang::Type::SubstTemplateTypeParmPack:
4524 case clang::Type::Auto:
4525 case clang::Type::InjectedClassName:
4526 case clang::Type::DependentName:
4527 case clang::Type::DependentTemplateSpecialization:
4528 case clang::Type::PackExpansion:
4529 case clang::Type::ObjCObject:
4530
4531 case clang::Type::TypeOfExpr:
4532 case clang::Type::TypeOf:
4533 case clang::Type::Decltype:
4534 case clang::Type::TemplateSpecialization:
4535 case clang::Type::Atomic:
4536 case clang::Type::Adjusted:
4537 break;
4538
4539 // pointer type decayed from an array or function type.
4540 case clang::Type::Decayed:
4541 break;
4542 }
4543 count = 0;
4544 return lldb::eEncodingInvalid;
4545}
4546
4547lldb::Format
4548ClangASTContext::GetFormat (void* type)
4549{
4550 if (!type)
4551 return lldb::eFormatDefault;
4552
4553 clang::QualType qual_type(GetCanonicalQualType(type));
4554
4555 switch (qual_type->getTypeClass())
4556 {
4557 case clang::Type::UnaryTransform:
4558 break;
4559
4560 case clang::Type::FunctionNoProto:
4561 case clang::Type::FunctionProto:
4562 break;
4563
4564 case clang::Type::IncompleteArray:
4565 case clang::Type::VariableArray:
4566 break;
4567
4568 case clang::Type::ConstantArray:
4569 return lldb::eFormatVoid; // no value
4570
4571 case clang::Type::ExtVector:
4572 case clang::Type::Vector:
4573 break;
4574
4575 case clang::Type::Builtin:
4576 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4577 {
4578 //default: assert(0 && "Unknown builtin type!");
4579 case clang::BuiltinType::UnknownAny:
4580 case clang::BuiltinType::Void:
4581 case clang::BuiltinType::BoundMember:
4582 break;
4583
4584 case clang::BuiltinType::Bool: return lldb::eFormatBoolean;
4585 case clang::BuiltinType::Char_S:
4586 case clang::BuiltinType::SChar:
4587 case clang::BuiltinType::WChar_S:
4588 case clang::BuiltinType::Char_U:
4589 case clang::BuiltinType::UChar:
4590 case clang::BuiltinType::WChar_U: return lldb::eFormatChar;
4591 case clang::BuiltinType::Char16: return lldb::eFormatUnicode16;
4592 case clang::BuiltinType::Char32: return lldb::eFormatUnicode32;
4593 case clang::BuiltinType::UShort: return lldb::eFormatUnsigned;
4594 case clang::BuiltinType::Short: return lldb::eFormatDecimal;
4595 case clang::BuiltinType::UInt: return lldb::eFormatUnsigned;
4596 case clang::BuiltinType::Int: return lldb::eFormatDecimal;
4597 case clang::BuiltinType::ULong: return lldb::eFormatUnsigned;
4598 case clang::BuiltinType::Long: return lldb::eFormatDecimal;
4599 case clang::BuiltinType::ULongLong: return lldb::eFormatUnsigned;
4600 case clang::BuiltinType::LongLong: return lldb::eFormatDecimal;
4601 case clang::BuiltinType::UInt128: return lldb::eFormatUnsigned;
4602 case clang::BuiltinType::Int128: return lldb::eFormatDecimal;
4603 case clang::BuiltinType::Float: return lldb::eFormatFloat;
4604 case clang::BuiltinType::Double: return lldb::eFormatFloat;
4605 case clang::BuiltinType::LongDouble: return lldb::eFormatFloat;
4606 case clang::BuiltinType::NullPtr:
4607 case clang::BuiltinType::Overload:
4608 case clang::BuiltinType::Dependent:
4609 case clang::BuiltinType::ObjCId:
4610 case clang::BuiltinType::ObjCClass:
4611 case clang::BuiltinType::ObjCSel:
4612 case clang::BuiltinType::Half:
4613 case clang::BuiltinType::ARCUnbridgedCast:
4614 case clang::BuiltinType::PseudoObject:
4615 case clang::BuiltinType::BuiltinFn:
4616 case clang::BuiltinType::OCLEvent:
4617 case clang::BuiltinType::OCLImage1d:
4618 case clang::BuiltinType::OCLImage1dArray:
4619 case clang::BuiltinType::OCLImage1dBuffer:
4620 case clang::BuiltinType::OCLImage2d:
4621 case clang::BuiltinType::OCLImage2dArray:
4622 case clang::BuiltinType::OCLImage3d:
4623 case clang::BuiltinType::OCLSampler:
4624 return lldb::eFormatHex;
4625 }
4626 break;
4627 case clang::Type::ObjCObjectPointer: return lldb::eFormatHex;
4628 case clang::Type::BlockPointer: return lldb::eFormatHex;
4629 case clang::Type::Pointer: return lldb::eFormatHex;
4630 case clang::Type::LValueReference:
4631 case clang::Type::RValueReference: return lldb::eFormatHex;
4632 case clang::Type::MemberPointer: break;
4633 case clang::Type::Complex:
4634 {
4635 if (qual_type->isComplexType())
4636 return lldb::eFormatComplex;
4637 else
4638 return lldb::eFormatComplexInteger;
4639 }
4640 case clang::Type::ObjCInterface: break;
4641 case clang::Type::Record: break;
4642 case clang::Type::Enum: return lldb::eFormatEnum;
4643 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004644 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004645 case clang::Type::Auto:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004646 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004647 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004648 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004649 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004650 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004651 case clang::Type::DependentSizedArray:
4652 case clang::Type::DependentSizedExtVector:
4653 case clang::Type::UnresolvedUsing:
4654 case clang::Type::Attributed:
4655 case clang::Type::TemplateTypeParm:
4656 case clang::Type::SubstTemplateTypeParm:
4657 case clang::Type::SubstTemplateTypeParmPack:
4658 case clang::Type::InjectedClassName:
4659 case clang::Type::DependentName:
4660 case clang::Type::DependentTemplateSpecialization:
4661 case clang::Type::PackExpansion:
4662 case clang::Type::ObjCObject:
4663
4664 case clang::Type::TypeOfExpr:
4665 case clang::Type::TypeOf:
4666 case clang::Type::Decltype:
4667 case clang::Type::TemplateSpecialization:
4668 case clang::Type::Atomic:
4669 case clang::Type::Adjusted:
4670 break;
4671
4672 // pointer type decayed from an array or function type.
4673 case clang::Type::Decayed:
4674 break;
4675 }
4676 // We don't know hot to display this type...
4677 return lldb::eFormatBytes;
4678}
4679
4680static bool
4681ObjCDeclHasIVars (clang::ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
4682{
4683 while (class_interface_decl)
4684 {
4685 if (class_interface_decl->ivar_size() > 0)
4686 return true;
4687
4688 if (check_superclass)
4689 class_interface_decl = class_interface_decl->getSuperClass();
4690 else
4691 break;
4692 }
4693 return false;
4694}
4695
4696uint32_t
4697ClangASTContext::GetNumChildren (void* type, bool omit_empty_base_classes)
4698{
4699 if (!type)
4700 return 0;
4701
4702 uint32_t num_children = 0;
4703 clang::QualType qual_type(GetQualType(type));
4704 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4705 switch (type_class)
4706 {
4707 case clang::Type::Builtin:
4708 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4709 {
4710 case clang::BuiltinType::ObjCId: // child is Class
4711 case clang::BuiltinType::ObjCClass: // child is Class
4712 num_children = 1;
4713 break;
4714
4715 default:
4716 break;
4717 }
4718 break;
4719
4720 case clang::Type::Complex: return 0;
4721
4722 case clang::Type::Record:
4723 if (GetCompleteQualType (getASTContext(), qual_type))
4724 {
4725 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4726 const clang::RecordDecl *record_decl = record_type->getDecl();
4727 assert(record_decl);
4728 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4729 if (cxx_record_decl)
4730 {
4731 if (omit_empty_base_classes)
4732 {
4733 // Check each base classes to see if it or any of its
4734 // base classes contain any fields. This can help
4735 // limit the noise in variable views by not having to
4736 // show base classes that contain no members.
4737 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4738 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4739 base_class != base_class_end;
4740 ++base_class)
4741 {
4742 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
4743
4744 // Skip empty base classes
4745 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
4746 continue;
4747
4748 num_children++;
4749 }
4750 }
4751 else
4752 {
4753 // Include all base classes
4754 num_children += cxx_record_decl->getNumBases();
4755 }
4756
4757 }
4758 clang::RecordDecl::field_iterator field, field_end;
4759 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
4760 ++num_children;
4761 }
4762 break;
4763
4764 case clang::Type::ObjCObject:
4765 case clang::Type::ObjCInterface:
4766 if (GetCompleteQualType (getASTContext(), qual_type))
4767 {
4768 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4769 assert (objc_class_type);
4770 if (objc_class_type)
4771 {
4772 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4773
4774 if (class_interface_decl)
4775 {
4776
4777 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4778 if (superclass_interface_decl)
4779 {
4780 if (omit_empty_base_classes)
4781 {
4782 if (ObjCDeclHasIVars (superclass_interface_decl, true))
4783 ++num_children;
4784 }
4785 else
4786 ++num_children;
4787 }
4788
4789 num_children += class_interface_decl->ivar_size();
4790 }
4791 }
4792 }
4793 break;
4794
4795 case clang::Type::ObjCObjectPointer:
4796 {
4797 const clang::ObjCObjectPointerType *pointer_type = llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
4798 clang::QualType pointee_type = pointer_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004799 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004800 // If this type points to a simple type, then it has 1 child
4801 if (num_pointee_children == 0)
4802 num_children = 1;
4803 else
4804 num_children = num_pointee_children;
4805 }
4806 break;
4807
4808 case clang::Type::Vector:
4809 case clang::Type::ExtVector:
4810 num_children = llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
4811 break;
4812
4813 case clang::Type::ConstantArray:
4814 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
4815 break;
4816
4817 case clang::Type::Pointer:
4818 {
4819 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
4820 clang::QualType pointee_type (pointer_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00004821 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004822 if (num_pointee_children == 0)
4823 {
4824 // We have a pointer to a pointee type that claims it has no children.
4825 // We will want to look at
4826 num_children = GetNumPointeeChildren (pointee_type);
4827 }
4828 else
4829 num_children = num_pointee_children;
4830 }
4831 break;
4832
4833 case clang::Type::LValueReference:
4834 case clang::Type::RValueReference:
4835 {
4836 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
4837 clang::QualType pointee_type = reference_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004838 uint32_t num_pointee_children = CompilerType (getASTContext(), pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004839 // If this type points to a simple type, then it has 1 child
4840 if (num_pointee_children == 0)
4841 num_children = 1;
4842 else
4843 num_children = num_pointee_children;
4844 }
4845 break;
4846
4847
4848 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004849 num_children = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004850 break;
4851
4852 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004853 num_children = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004854 break;
4855
4856 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004857 num_children = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004858 break;
4859 default:
4860 break;
4861 }
4862 return num_children;
4863}
4864
4865lldb::BasicType
4866ClangASTContext::GetBasicTypeEnumeration (void* type)
4867{
4868 if (type)
4869 {
4870 clang::QualType qual_type(GetQualType(type));
4871 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4872 if (type_class == clang::Type::Builtin)
4873 {
4874 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4875 {
4876 case clang::BuiltinType::Void: return eBasicTypeVoid;
4877 case clang::BuiltinType::Bool: return eBasicTypeBool;
4878 case clang::BuiltinType::Char_S: return eBasicTypeSignedChar;
4879 case clang::BuiltinType::Char_U: return eBasicTypeUnsignedChar;
4880 case clang::BuiltinType::Char16: return eBasicTypeChar16;
4881 case clang::BuiltinType::Char32: return eBasicTypeChar32;
4882 case clang::BuiltinType::UChar: return eBasicTypeUnsignedChar;
4883 case clang::BuiltinType::SChar: return eBasicTypeSignedChar;
4884 case clang::BuiltinType::WChar_S: return eBasicTypeSignedWChar;
4885 case clang::BuiltinType::WChar_U: return eBasicTypeUnsignedWChar;
4886 case clang::BuiltinType::Short: return eBasicTypeShort;
4887 case clang::BuiltinType::UShort: return eBasicTypeUnsignedShort;
4888 case clang::BuiltinType::Int: return eBasicTypeInt;
4889 case clang::BuiltinType::UInt: return eBasicTypeUnsignedInt;
4890 case clang::BuiltinType::Long: return eBasicTypeLong;
4891 case clang::BuiltinType::ULong: return eBasicTypeUnsignedLong;
4892 case clang::BuiltinType::LongLong: return eBasicTypeLongLong;
4893 case clang::BuiltinType::ULongLong: return eBasicTypeUnsignedLongLong;
4894 case clang::BuiltinType::Int128: return eBasicTypeInt128;
4895 case clang::BuiltinType::UInt128: return eBasicTypeUnsignedInt128;
4896
4897 case clang::BuiltinType::Half: return eBasicTypeHalf;
4898 case clang::BuiltinType::Float: return eBasicTypeFloat;
4899 case clang::BuiltinType::Double: return eBasicTypeDouble;
4900 case clang::BuiltinType::LongDouble:return eBasicTypeLongDouble;
4901
4902 case clang::BuiltinType::NullPtr: return eBasicTypeNullPtr;
4903 case clang::BuiltinType::ObjCId: return eBasicTypeObjCID;
4904 case clang::BuiltinType::ObjCClass: return eBasicTypeObjCClass;
4905 case clang::BuiltinType::ObjCSel: return eBasicTypeObjCSel;
4906 case clang::BuiltinType::Dependent:
4907 case clang::BuiltinType::Overload:
4908 case clang::BuiltinType::BoundMember:
4909 case clang::BuiltinType::PseudoObject:
4910 case clang::BuiltinType::UnknownAny:
4911 case clang::BuiltinType::BuiltinFn:
4912 case clang::BuiltinType::ARCUnbridgedCast:
4913 case clang::BuiltinType::OCLEvent:
4914 case clang::BuiltinType::OCLImage1d:
4915 case clang::BuiltinType::OCLImage1dArray:
4916 case clang::BuiltinType::OCLImage1dBuffer:
4917 case clang::BuiltinType::OCLImage2d:
4918 case clang::BuiltinType::OCLImage2dArray:
4919 case clang::BuiltinType::OCLImage3d:
4920 case clang::BuiltinType::OCLSampler:
4921 return eBasicTypeOther;
4922 }
4923 }
4924 }
4925 return eBasicTypeInvalid;
4926}
4927
4928
4929#pragma mark Aggregate Types
4930
4931uint32_t
Greg Claytona1e5dc82015-08-11 22:53:00 +00004932ClangASTContext::GetNumDirectBaseClasses (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004933{
4934 if (!type)
4935 return 0;
4936 ClangASTContext *ast = type.GetTypeSystem()->AsClangASTContext();
4937 if (!ast)
4938 return 0;
4939
4940 uint32_t count = 0;
4941 clang::QualType qual_type(GetCanonicalQualType(type));
4942 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4943 switch (type_class)
4944 {
4945 case clang::Type::Record:
4946 if (ast->GetCompleteType(type.GetOpaqueQualType()))
4947 {
4948 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
4949 if (cxx_record_decl)
4950 count = cxx_record_decl->getNumBases();
4951 }
4952 break;
4953
4954 case clang::Type::ObjCObjectPointer:
4955 count = GetNumDirectBaseClasses(ast->GetPointeeType(type.GetOpaqueQualType()));
4956 break;
4957
4958 case clang::Type::ObjCObject:
4959 if (ast->GetCompleteType(type.GetOpaqueQualType()))
4960 {
4961 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
4962 if (objc_class_type)
4963 {
4964 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4965
4966 if (class_interface_decl && class_interface_decl->getSuperClass())
4967 count = 1;
4968 }
4969 }
4970 break;
4971 case clang::Type::ObjCInterface:
4972 if (ast->GetCompleteType(type.GetOpaqueQualType()))
4973 {
4974 const clang::ObjCInterfaceType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
4975 if (objc_interface_type)
4976 {
4977 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
4978
4979 if (class_interface_decl && class_interface_decl->getSuperClass())
4980 count = 1;
4981 }
4982 }
4983 break;
4984
4985
4986 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004987 count = GetNumDirectBaseClasses(CompilerType (ast->getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()));
Greg Claytond8d4a572015-08-11 21:38:15 +00004988 break;
4989
4990 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004991 count = GetNumDirectBaseClasses(CompilerType (ast->getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()));
Greg Claytond8d4a572015-08-11 21:38:15 +00004992 break;
4993
4994 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004995 return GetNumDirectBaseClasses(CompilerType (ast->getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()));
Greg Claytond8d4a572015-08-11 21:38:15 +00004996
4997 default:
4998 break;
4999 }
5000 return count;
5001}
5002
5003uint32_t
Greg Claytona1e5dc82015-08-11 22:53:00 +00005004ClangASTContext::GetNumVirtualBaseClasses (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00005005{
5006 if (!type)
5007 return 0;
5008 ClangASTContext *ast = type.GetTypeSystem()->AsClangASTContext();
5009 if (!ast)
5010 return 0;
5011
5012 uint32_t count = 0;
5013 clang::QualType qual_type(GetCanonicalQualType(type));
5014 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5015 switch (type_class)
5016 {
5017 case clang::Type::Record:
5018 if (ast->GetCompleteType(type.GetOpaqueQualType()))
5019 {
5020 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5021 if (cxx_record_decl)
5022 count = cxx_record_decl->getNumVBases();
5023 }
5024 break;
5025
5026 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005027 count = GetNumVirtualBaseClasses(CompilerType (ast->getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()));
Greg Claytond8d4a572015-08-11 21:38:15 +00005028 break;
5029
5030 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005031 count = GetNumVirtualBaseClasses(CompilerType (ast->getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()));
Greg Claytond8d4a572015-08-11 21:38:15 +00005032 break;
5033
5034 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005035 count = GetNumVirtualBaseClasses(CompilerType (ast->getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()));
Greg Claytond8d4a572015-08-11 21:38:15 +00005036 break;
5037
5038 default:
5039 break;
5040 }
5041 return count;
5042}
5043
5044uint32_t
5045ClangASTContext::GetNumFields (void* type)
5046{
5047 if (!type)
5048 return 0;
5049
5050 uint32_t count = 0;
5051 clang::QualType qual_type(GetCanonicalQualType(type));
5052 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5053 switch (type_class)
5054 {
5055 case clang::Type::Record:
5056 if (GetCompleteType(type))
5057 {
5058 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5059 if (record_type)
5060 {
5061 clang::RecordDecl *record_decl = record_type->getDecl();
5062 if (record_decl)
5063 {
5064 uint32_t field_idx = 0;
5065 clang::RecordDecl::field_iterator field, field_end;
5066 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
5067 ++field_idx;
5068 count = field_idx;
5069 }
5070 }
5071 }
5072 break;
5073
5074 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005075 count = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00005076 break;
5077
5078 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005079 count = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00005080 break;
5081
5082 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005083 count = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00005084 break;
5085
5086 case clang::Type::ObjCObjectPointer:
5087 if (GetCompleteType(type))
5088 {
5089 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
5090 if (objc_class_type)
5091 {
5092 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
5093
5094 if (class_interface_decl)
5095 count = class_interface_decl->ivar_size();
5096 }
5097 }
5098 break;
5099
5100 case clang::Type::ObjCObject:
5101 case clang::Type::ObjCInterface:
5102 if (GetCompleteType(type))
5103 {
5104 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5105 if (objc_class_type)
5106 {
5107 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5108
5109 if (class_interface_decl)
5110 count = class_interface_decl->ivar_size();
5111 }
5112 }
5113 break;
5114
5115 default:
5116 break;
5117 }
5118 return count;
5119}
5120
Greg Claytona1e5dc82015-08-11 22:53:00 +00005121CompilerType
5122ClangASTContext::GetDirectBaseClassAtIndex (const CompilerType& type, size_t idx, uint32_t *bit_offset_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00005123{
5124 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005125 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005126 ClangASTContext *ast = type.GetTypeSystem()->AsClangASTContext();
5127 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005128 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005129
5130 clang::QualType qual_type(GetCanonicalQualType(type));
5131 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5132 switch (type_class)
5133 {
5134 case clang::Type::Record:
5135 if (ast->GetCompleteType(type.GetOpaqueQualType()))
5136 {
5137 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5138 if (cxx_record_decl)
5139 {
5140 uint32_t curr_idx = 0;
5141 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5142 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5143 base_class != base_class_end;
5144 ++base_class, ++curr_idx)
5145 {
5146 if (curr_idx == idx)
5147 {
5148 if (bit_offset_ptr)
5149 {
5150 const clang::ASTRecordLayout &record_layout = ast->getASTContext()->getASTRecordLayout(cxx_record_decl);
5151 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5152 if (base_class->isVirtual())
5153 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5154 else
5155 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5156 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005157 return CompilerType (ast, base_class->getType().getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00005158 }
5159 }
5160 }
5161 }
5162 break;
5163
5164 case clang::Type::ObjCObjectPointer:
5165 return GetDirectBaseClassAtIndex(ast->GetPointeeType(type.GetOpaqueQualType()), idx, bit_offset_ptr);
5166
5167 case clang::Type::ObjCObject:
5168 if (idx == 0 && ast->GetCompleteType(type.GetOpaqueQualType()))
5169 {
5170 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
5171 if (objc_class_type)
5172 {
5173 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5174
5175 if (class_interface_decl)
5176 {
5177 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5178 if (superclass_interface_decl)
5179 {
5180 if (bit_offset_ptr)
5181 *bit_offset_ptr = 0;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005182 return CompilerType (ast->getASTContext(), ast->getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00005183 }
5184 }
5185 }
5186 }
5187 break;
5188 case clang::Type::ObjCInterface:
5189 if (idx == 0 && ast->GetCompleteType(type.GetOpaqueQualType()))
5190 {
5191 const clang::ObjCObjectType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
5192 if (objc_interface_type)
5193 {
5194 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
5195
5196 if (class_interface_decl)
5197 {
5198 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5199 if (superclass_interface_decl)
5200 {
5201 if (bit_offset_ptr)
5202 *bit_offset_ptr = 0;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005203 return CompilerType (ast->getASTContext(), ast->getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00005204 }
5205 }
5206 }
5207 }
5208 break;
5209
5210
5211 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005212 return GetDirectBaseClassAtIndex (CompilerType (ast, llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()), idx, bit_offset_ptr);
Greg Claytond8d4a572015-08-11 21:38:15 +00005213
5214 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005215 return GetDirectBaseClassAtIndex (CompilerType (ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()), idx, bit_offset_ptr);
Greg Claytond8d4a572015-08-11 21:38:15 +00005216
5217 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005218 return GetDirectBaseClassAtIndex (CompilerType (ast, llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()), idx, bit_offset_ptr);
Greg Claytond8d4a572015-08-11 21:38:15 +00005219
5220 default:
5221 break;
5222 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005223 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005224}
5225
Greg Claytona1e5dc82015-08-11 22:53:00 +00005226CompilerType
5227ClangASTContext::GetVirtualBaseClassAtIndex (const CompilerType& type, size_t idx, uint32_t *bit_offset_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00005228{
5229 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005230 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005231 ClangASTContext *ast = type.GetTypeSystem()->AsClangASTContext();
5232 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005233 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005234
5235 clang::QualType qual_type(GetCanonicalQualType(type));
5236 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5237 switch (type_class)
5238 {
5239 case clang::Type::Record:
5240 if (ast->GetCompleteType(type.GetOpaqueQualType()))
5241 {
5242 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5243 if (cxx_record_decl)
5244 {
5245 uint32_t curr_idx = 0;
5246 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5247 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
5248 base_class != base_class_end;
5249 ++base_class, ++curr_idx)
5250 {
5251 if (curr_idx == idx)
5252 {
5253 if (bit_offset_ptr)
5254 {
5255 const clang::ASTRecordLayout &record_layout = ast->getASTContext()->getASTRecordLayout(cxx_record_decl);
5256 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5257 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5258
5259 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005260 return CompilerType (ast, base_class->getType().getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00005261 }
5262 }
5263 }
5264 }
5265 break;
5266
5267 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005268 return GetVirtualBaseClassAtIndex (CompilerType (ast, llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()), idx, bit_offset_ptr);
Greg Claytond8d4a572015-08-11 21:38:15 +00005269
5270 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005271 return GetVirtualBaseClassAtIndex (CompilerType (ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()), idx, bit_offset_ptr);
Greg Claytond8d4a572015-08-11 21:38:15 +00005272
5273 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005274 return GetVirtualBaseClassAtIndex (CompilerType (ast, llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()), idx, bit_offset_ptr);
Greg Claytond8d4a572015-08-11 21:38:15 +00005275
5276 default:
5277 break;
5278 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005279 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005280}
5281
5282static clang_type_t
5283GetObjCFieldAtIndex (clang::ASTContext *ast,
5284 clang::ObjCInterfaceDecl *class_interface_decl,
5285 size_t idx,
5286 std::string& name,
5287 uint64_t *bit_offset_ptr,
5288 uint32_t *bitfield_bit_size_ptr,
5289 bool *is_bitfield_ptr)
5290{
5291 if (class_interface_decl)
5292 {
5293 if (idx < (class_interface_decl->ivar_size()))
5294 {
5295 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
5296 uint32_t ivar_idx = 0;
5297
5298 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
5299 {
5300 if (ivar_idx == idx)
5301 {
5302 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
5303
5304 clang::QualType ivar_qual_type(ivar_decl->getType());
5305
5306 name.assign(ivar_decl->getNameAsString());
5307
5308 if (bit_offset_ptr)
5309 {
5310 const clang::ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
5311 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
5312 }
5313
5314 const bool is_bitfield = ivar_pos->isBitField();
5315
5316 if (bitfield_bit_size_ptr)
5317 {
5318 *bitfield_bit_size_ptr = 0;
5319
5320 if (is_bitfield && ast)
5321 {
5322 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5323 llvm::APSInt bitfield_apsint;
5324 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
5325 {
5326 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5327 }
5328 }
5329 }
5330 if (is_bitfield_ptr)
5331 *is_bitfield_ptr = is_bitfield;
5332
5333 return ivar_qual_type.getAsOpaquePtr();
5334 }
5335 }
5336 }
5337 }
5338 return nullptr;
5339}
5340
Greg Claytona1e5dc82015-08-11 22:53:00 +00005341CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00005342ClangASTContext::GetFieldAtIndex (void* type, size_t idx,
5343 std::string& name,
5344 uint64_t *bit_offset_ptr,
5345 uint32_t *bitfield_bit_size_ptr,
5346 bool *is_bitfield_ptr)
5347{
5348 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005349 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005350
5351 clang::QualType qual_type(GetCanonicalQualType(type));
5352 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5353 switch (type_class)
5354 {
5355 case clang::Type::Record:
5356 if (GetCompleteType(type))
5357 {
5358 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5359 const clang::RecordDecl *record_decl = record_type->getDecl();
5360 uint32_t field_idx = 0;
5361 clang::RecordDecl::field_iterator field, field_end;
5362 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
5363 {
5364 if (idx == field_idx)
5365 {
5366 // Print the member type if requested
5367 // Print the member name and equal sign
5368 name.assign(field->getNameAsString());
5369
5370 // Figure out the type byte size (field_type_info.first) and
5371 // alignment (field_type_info.second) from the AST context.
5372 if (bit_offset_ptr)
5373 {
5374 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5375 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
5376 }
5377
5378 const bool is_bitfield = field->isBitField();
5379
5380 if (bitfield_bit_size_ptr)
5381 {
5382 *bitfield_bit_size_ptr = 0;
5383
5384 if (is_bitfield)
5385 {
5386 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5387 llvm::APSInt bitfield_apsint;
5388 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *getASTContext()))
5389 {
5390 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5391 }
5392 }
5393 }
5394 if (is_bitfield_ptr)
5395 *is_bitfield_ptr = is_bitfield;
5396
Greg Claytona1e5dc82015-08-11 22:53:00 +00005397 return CompilerType (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005398 }
5399 }
5400 }
5401 break;
5402
5403 case clang::Type::ObjCObjectPointer:
5404 if (GetCompleteType(type))
5405 {
5406 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
5407 if (objc_class_type)
5408 {
5409 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005410 return CompilerType (this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl, idx, name, bit_offset_ptr, bitfield_bit_size_ptr, is_bitfield_ptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00005411 }
5412 }
5413 break;
5414
5415 case clang::Type::ObjCObject:
5416 case clang::Type::ObjCInterface:
5417 if (GetCompleteType(type))
5418 {
5419 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5420 assert (objc_class_type);
5421 if (objc_class_type)
5422 {
5423 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005424 return CompilerType (this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl, idx, name, bit_offset_ptr, bitfield_bit_size_ptr, is_bitfield_ptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00005425 }
5426 }
5427 break;
5428
5429
5430 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005431 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005432 GetFieldAtIndex (idx,
5433 name,
5434 bit_offset_ptr,
5435 bitfield_bit_size_ptr,
5436 is_bitfield_ptr);
5437
5438 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005439 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005440 GetFieldAtIndex (idx,
5441 name,
5442 bit_offset_ptr,
5443 bitfield_bit_size_ptr,
5444 is_bitfield_ptr);
5445
5446 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005447 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005448 GetFieldAtIndex (idx,
5449 name,
5450 bit_offset_ptr,
5451 bitfield_bit_size_ptr,
5452 is_bitfield_ptr);
5453
5454 default:
5455 break;
5456 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005457 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005458}
5459
5460// If a pointer to a pointee type (the clang_type arg) says that it has no
5461// children, then we either need to trust it, or override it and return a
5462// different result. For example, an "int *" has one child that is an integer,
5463// but a function pointer doesn't have any children. Likewise if a Record type
5464// claims it has no children, then there really is nothing to show.
5465uint32_t
5466ClangASTContext::GetNumPointeeChildren (clang::QualType type)
5467{
5468 if (type.isNull())
5469 return 0;
5470
5471 clang::QualType qual_type(type.getCanonicalType());
5472 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5473 switch (type_class)
5474 {
5475 case clang::Type::Builtin:
5476 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
5477 {
5478 case clang::BuiltinType::UnknownAny:
5479 case clang::BuiltinType::Void:
5480 case clang::BuiltinType::NullPtr:
5481 case clang::BuiltinType::OCLEvent:
5482 case clang::BuiltinType::OCLImage1d:
5483 case clang::BuiltinType::OCLImage1dArray:
5484 case clang::BuiltinType::OCLImage1dBuffer:
5485 case clang::BuiltinType::OCLImage2d:
5486 case clang::BuiltinType::OCLImage2dArray:
5487 case clang::BuiltinType::OCLImage3d:
5488 case clang::BuiltinType::OCLSampler:
5489 return 0;
5490 case clang::BuiltinType::Bool:
5491 case clang::BuiltinType::Char_U:
5492 case clang::BuiltinType::UChar:
5493 case clang::BuiltinType::WChar_U:
5494 case clang::BuiltinType::Char16:
5495 case clang::BuiltinType::Char32:
5496 case clang::BuiltinType::UShort:
5497 case clang::BuiltinType::UInt:
5498 case clang::BuiltinType::ULong:
5499 case clang::BuiltinType::ULongLong:
5500 case clang::BuiltinType::UInt128:
5501 case clang::BuiltinType::Char_S:
5502 case clang::BuiltinType::SChar:
5503 case clang::BuiltinType::WChar_S:
5504 case clang::BuiltinType::Short:
5505 case clang::BuiltinType::Int:
5506 case clang::BuiltinType::Long:
5507 case clang::BuiltinType::LongLong:
5508 case clang::BuiltinType::Int128:
5509 case clang::BuiltinType::Float:
5510 case clang::BuiltinType::Double:
5511 case clang::BuiltinType::LongDouble:
5512 case clang::BuiltinType::Dependent:
5513 case clang::BuiltinType::Overload:
5514 case clang::BuiltinType::ObjCId:
5515 case clang::BuiltinType::ObjCClass:
5516 case clang::BuiltinType::ObjCSel:
5517 case clang::BuiltinType::BoundMember:
5518 case clang::BuiltinType::Half:
5519 case clang::BuiltinType::ARCUnbridgedCast:
5520 case clang::BuiltinType::PseudoObject:
5521 case clang::BuiltinType::BuiltinFn:
5522 return 1;
5523 }
5524 break;
5525
5526 case clang::Type::Complex: return 1;
5527 case clang::Type::Pointer: return 1;
5528 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
5529 case clang::Type::LValueReference: return 1;
5530 case clang::Type::RValueReference: return 1;
5531 case clang::Type::MemberPointer: return 0;
5532 case clang::Type::ConstantArray: return 0;
5533 case clang::Type::IncompleteArray: return 0;
5534 case clang::Type::VariableArray: return 0;
5535 case clang::Type::DependentSizedArray: return 0;
5536 case clang::Type::DependentSizedExtVector: return 0;
5537 case clang::Type::Vector: return 0;
5538 case clang::Type::ExtVector: return 0;
5539 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
5540 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
5541 case clang::Type::UnresolvedUsing: return 0;
5542 case clang::Type::Paren: return GetNumPointeeChildren (llvm::cast<clang::ParenType>(qual_type)->desugar());
5543 case clang::Type::Typedef: return GetNumPointeeChildren (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
5544 case clang::Type::Elaborated: return GetNumPointeeChildren (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
5545 case clang::Type::TypeOfExpr: return 0;
5546 case clang::Type::TypeOf: return 0;
5547 case clang::Type::Decltype: return 0;
5548 case clang::Type::Record: return 0;
5549 case clang::Type::Enum: return 1;
5550 case clang::Type::TemplateTypeParm: return 1;
5551 case clang::Type::SubstTemplateTypeParm: return 1;
5552 case clang::Type::TemplateSpecialization: return 1;
5553 case clang::Type::InjectedClassName: return 0;
5554 case clang::Type::DependentName: return 1;
5555 case clang::Type::DependentTemplateSpecialization: return 1;
5556 case clang::Type::ObjCObject: return 0;
5557 case clang::Type::ObjCInterface: return 0;
5558 case clang::Type::ObjCObjectPointer: return 1;
5559 default:
5560 break;
5561 }
5562 return 0;
5563}
5564
5565
Greg Claytona1e5dc82015-08-11 22:53:00 +00005566CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00005567ClangASTContext::GetChildClangTypeAtIndex (void* type, ExecutionContext *exe_ctx,
5568 size_t idx,
5569 bool transparent_pointers,
5570 bool omit_empty_base_classes,
5571 bool ignore_array_bounds,
5572 std::string& child_name,
5573 uint32_t &child_byte_size,
5574 int32_t &child_byte_offset,
5575 uint32_t &child_bitfield_bit_size,
5576 uint32_t &child_bitfield_bit_offset,
5577 bool &child_is_base_class,
5578 bool &child_is_deref_of_parent,
5579 ValueObject *valobj)
5580{
5581 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005582 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005583
5584 clang::QualType parent_qual_type(GetCanonicalQualType(type));
5585 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
5586 child_bitfield_bit_size = 0;
5587 child_bitfield_bit_offset = 0;
5588 child_is_base_class = false;
5589
5590 const bool idx_is_valid = idx < GetNumChildren (type, omit_empty_base_classes);
5591 uint32_t bit_offset;
5592 switch (parent_type_class)
5593 {
5594 case clang::Type::Builtin:
5595 if (idx_is_valid)
5596 {
5597 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind())
5598 {
5599 case clang::BuiltinType::ObjCId:
5600 case clang::BuiltinType::ObjCClass:
5601 child_name = "isa";
5602 child_byte_size = getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) / CHAR_BIT;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005603 return CompilerType (getASTContext(), getASTContext()->ObjCBuiltinClassTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00005604
5605 default:
5606 break;
5607 }
5608 }
5609 break;
5610
5611 case clang::Type::Record:
5612 if (idx_is_valid && GetCompleteType(type))
5613 {
5614 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
5615 const clang::RecordDecl *record_decl = record_type->getDecl();
5616 assert(record_decl);
5617 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5618 uint32_t child_idx = 0;
5619
5620 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5621 if (cxx_record_decl)
5622 {
5623 // We might have base classes to print out first
5624 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5625 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5626 base_class != base_class_end;
5627 ++base_class)
5628 {
5629 const clang::CXXRecordDecl *base_class_decl = nullptr;
5630
5631 // Skip empty base classes
5632 if (omit_empty_base_classes)
5633 {
5634 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5635 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5636 continue;
5637 }
5638
5639 if (idx == child_idx)
5640 {
5641 if (base_class_decl == nullptr)
5642 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5643
5644
5645 if (base_class->isVirtual())
5646 {
5647 bool handled = false;
5648 if (valobj)
5649 {
5650 Error err;
5651 AddressType addr_type = eAddressTypeInvalid;
5652 lldb::addr_t vtable_ptr_addr = valobj->GetCPPVTableAddress(addr_type);
5653
5654 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS && addr_type == eAddressTypeLoad)
5655 {
5656
5657 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
5658 Process *process = exe_ctx.GetProcessPtr();
5659 if (process)
5660 {
5661 clang::VTableContextBase *vtable_ctx = getASTContext()->getVTableContext();
5662 if (vtable_ctx)
5663 {
5664 if (vtable_ctx->isMicrosoft())
5665 {
5666 clang::MicrosoftVTableContext *msoft_vtable_ctx = static_cast<clang::MicrosoftVTableContext *>(vtable_ctx);
5667
5668 if (vtable_ptr_addr)
5669 {
5670 const lldb::addr_t vbtable_ptr_addr = vtable_ptr_addr + record_layout.getVBPtrOffset().getQuantity();
5671
5672 const lldb::addr_t vbtable_ptr = process->ReadPointerFromMemory(vbtable_ptr_addr, err);
5673 if (vbtable_ptr != LLDB_INVALID_ADDRESS)
5674 {
5675 // Get the index into the virtual base table. The index is the index in uint32_t from vbtable_ptr
5676 const unsigned vbtable_index = msoft_vtable_ctx->getVBTableIndex(cxx_record_decl, base_class_decl);
5677 const lldb::addr_t base_offset_addr = vbtable_ptr + vbtable_index * 4;
5678 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
5679 if (base_offset != UINT32_MAX)
5680 {
5681 handled = true;
5682 bit_offset = base_offset * 8;
5683 }
5684 }
5685 }
5686 }
5687 else
5688 {
5689 clang::ItaniumVTableContext *itanium_vtable_ctx = static_cast<clang::ItaniumVTableContext *>(vtable_ctx);
5690 if (vtable_ptr_addr)
5691 {
5692 const lldb::addr_t vtable_ptr = process->ReadPointerFromMemory(vtable_ptr_addr, err);
5693 if (vtable_ptr != LLDB_INVALID_ADDRESS)
5694 {
5695 clang::CharUnits base_offset_offset = itanium_vtable_ctx->getVirtualBaseOffsetOffset(cxx_record_decl, base_class_decl);
5696 const lldb::addr_t base_offset_addr = vtable_ptr + base_offset_offset.getQuantity();
5697 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
5698 if (base_offset != UINT32_MAX)
5699 {
5700 handled = true;
5701 bit_offset = base_offset * 8;
5702 }
5703 }
5704 }
5705 }
5706 }
5707 }
5708 }
5709
5710 }
5711 if (!handled)
5712 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5713 }
5714 else
5715 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5716
5717 // Base classes should be a multiple of 8 bits in size
5718 child_byte_offset = bit_offset/8;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005719 CompilerType base_class_clang_type(getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005720 child_name = base_class_clang_type.GetTypeName().AsCString("");
5721 uint64_t base_class_clang_type_bit_size = base_class_clang_type.GetBitSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5722
5723 // Base classes bit sizes should be a multiple of 8 bits in size
5724 assert (base_class_clang_type_bit_size % 8 == 0);
5725 child_byte_size = base_class_clang_type_bit_size / 8;
5726 child_is_base_class = true;
5727 return base_class_clang_type;
5728 }
5729 // We don't increment the child index in the for loop since we might
5730 // be skipping empty base classes
5731 ++child_idx;
5732 }
5733 }
5734 // Make sure index is in range...
5735 uint32_t field_idx = 0;
5736 clang::RecordDecl::field_iterator field, field_end;
5737 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
5738 {
5739 if (idx == child_idx)
5740 {
5741 // Print the member type if requested
5742 // Print the member name and equal sign
5743 child_name.assign(field->getNameAsString().c_str());
5744
5745 // Figure out the type byte size (field_type_info.first) and
5746 // alignment (field_type_info.second) from the AST context.
Greg Claytona1e5dc82015-08-11 22:53:00 +00005747 CompilerType field_clang_type (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005748 assert(field_idx < record_layout.getFieldCount());
5749 child_byte_size = field_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5750
5751 // Figure out the field offset within the current struct/union/class type
5752 bit_offset = record_layout.getFieldOffset (field_idx);
5753 child_byte_offset = bit_offset / 8;
5754 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, child_bitfield_bit_size))
5755 child_bitfield_bit_offset = bit_offset % 8;
5756
5757 return field_clang_type;
5758 }
5759 }
5760 }
5761 break;
5762
5763 case clang::Type::ObjCObject:
5764 case clang::Type::ObjCInterface:
5765 if (idx_is_valid && GetCompleteType(type))
5766 {
5767 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
5768 assert (objc_class_type);
5769 if (objc_class_type)
5770 {
5771 uint32_t child_idx = 0;
5772 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5773
5774 if (class_interface_decl)
5775 {
5776
5777 const clang::ASTRecordLayout &interface_layout = getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
5778 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5779 if (superclass_interface_decl)
5780 {
5781 if (omit_empty_base_classes)
5782 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005783 CompilerType base_class_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00005784 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes) > 0)
5785 {
5786 if (idx == 0)
5787 {
5788 clang::QualType ivar_qual_type(getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5789
5790
5791 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
5792
5793 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
5794
5795 child_byte_size = ivar_type_info.Width / 8;
5796 child_byte_offset = 0;
5797 child_is_base_class = true;
5798
Greg Claytona1e5dc82015-08-11 22:53:00 +00005799 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005800 }
5801
5802 ++child_idx;
5803 }
5804 }
5805 else
5806 ++child_idx;
5807 }
5808
5809 const uint32_t superclass_idx = child_idx;
5810
5811 if (idx < (child_idx + class_interface_decl->ivar_size()))
5812 {
5813 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
5814
5815 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
5816 {
5817 if (child_idx == idx)
5818 {
5819 clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
5820
5821 clang::QualType ivar_qual_type(ivar_decl->getType());
5822
5823 child_name.assign(ivar_decl->getNameAsString().c_str());
5824
5825 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
5826
5827 child_byte_size = ivar_type_info.Width / 8;
5828
5829 // Figure out the field offset within the current struct/union/class type
5830 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
5831 // that doesn't account for the space taken up by unbacked properties, or from
5832 // the changing size of base classes that are newer than this class.
5833 // So if we have a process around that we can ask about this object, do so.
5834 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
5835 Process *process = nullptr;
5836 if (exe_ctx)
5837 process = exe_ctx->GetProcessPtr();
5838 if (process)
5839 {
5840 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
5841 if (objc_runtime != nullptr)
5842 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005843 CompilerType parent_ast_type (getASTContext(), parent_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005844 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
5845 }
5846 }
5847
5848 // Setting this to UINT32_MAX to make sure we don't compute it twice...
5849 bit_offset = UINT32_MAX;
5850
5851 if (child_byte_offset == static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET))
5852 {
5853 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
5854 child_byte_offset = bit_offset / 8;
5855 }
5856
5857 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
5858 // of a bitfield within its containing object. So regardless of where we get the byte
5859 // offset from, we still need to get the bit offset for bitfields from the layout.
5860
5861 if (ClangASTContext::FieldIsBitfield (getASTContext(), ivar_decl, child_bitfield_bit_size))
5862 {
5863 if (bit_offset == UINT32_MAX)
5864 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
5865
5866 child_bitfield_bit_offset = bit_offset % 8;
5867 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005868 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005869 }
5870 ++child_idx;
5871 }
5872 }
5873 }
5874 }
5875 }
5876 break;
5877
5878 case clang::Type::ObjCObjectPointer:
5879 if (idx_is_valid)
5880 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005881 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005882
5883 if (transparent_pointers && pointee_clang_type.IsAggregateType())
5884 {
5885 child_is_deref_of_parent = false;
5886 bool tmp_child_is_deref_of_parent = false;
5887 return pointee_clang_type.GetChildClangTypeAtIndex (exe_ctx,
5888 idx,
5889 transparent_pointers,
5890 omit_empty_base_classes,
5891 ignore_array_bounds,
5892 child_name,
5893 child_byte_size,
5894 child_byte_offset,
5895 child_bitfield_bit_size,
5896 child_bitfield_bit_offset,
5897 child_is_base_class,
5898 tmp_child_is_deref_of_parent,
5899 valobj);
5900 }
5901 else
5902 {
5903 child_is_deref_of_parent = true;
5904 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
5905 if (parent_name)
5906 {
5907 child_name.assign(1, '*');
5908 child_name += parent_name;
5909 }
5910
5911 // We have a pointer to an simple type
5912 if (idx == 0 && pointee_clang_type.GetCompleteType())
5913 {
5914 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5915 child_byte_offset = 0;
5916 return pointee_clang_type;
5917 }
5918 }
5919 }
5920 break;
5921
5922 case clang::Type::Vector:
5923 case clang::Type::ExtVector:
5924 if (idx_is_valid)
5925 {
5926 const clang::VectorType *array = llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
5927 if (array)
5928 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005929 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005930 if (element_type.GetCompleteType())
5931 {
5932 char element_name[64];
5933 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
5934 child_name.assign(element_name);
5935 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5936 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
5937 return element_type;
5938 }
5939 }
5940 }
5941 break;
5942
5943 case clang::Type::ConstantArray:
5944 case clang::Type::IncompleteArray:
5945 if (ignore_array_bounds || idx_is_valid)
5946 {
5947 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
5948 if (array)
5949 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005950 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005951 if (element_type.GetCompleteType())
5952 {
5953 char element_name[64];
5954 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
5955 child_name.assign(element_name);
5956 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5957 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
5958 return element_type;
5959 }
5960 }
5961 }
5962 break;
5963
5964
5965 case clang::Type::Pointer:
5966 if (idx_is_valid)
5967 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005968 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005969
5970 // Don't dereference "void *" pointers
5971 if (pointee_clang_type.IsVoidType())
Greg Claytona1e5dc82015-08-11 22:53:00 +00005972 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005973
5974 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
5975 {
5976 child_is_deref_of_parent = false;
5977 bool tmp_child_is_deref_of_parent = false;
5978 return pointee_clang_type.GetChildClangTypeAtIndex (exe_ctx,
5979 idx,
5980 transparent_pointers,
5981 omit_empty_base_classes,
5982 ignore_array_bounds,
5983 child_name,
5984 child_byte_size,
5985 child_byte_offset,
5986 child_bitfield_bit_size,
5987 child_bitfield_bit_offset,
5988 child_is_base_class,
5989 tmp_child_is_deref_of_parent,
5990 valobj);
5991 }
5992 else
5993 {
5994 child_is_deref_of_parent = true;
5995
5996 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
5997 if (parent_name)
5998 {
5999 child_name.assign(1, '*');
6000 child_name += parent_name;
6001 }
6002
6003 // We have a pointer to an simple type
6004 if (idx == 0)
6005 {
6006 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6007 child_byte_offset = 0;
6008 return pointee_clang_type;
6009 }
6010 }
6011 }
6012 break;
6013
6014 case clang::Type::LValueReference:
6015 case clang::Type::RValueReference:
6016 if (idx_is_valid)
6017 {
6018 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006019 CompilerType pointee_clang_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006020 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
6021 {
6022 child_is_deref_of_parent = false;
6023 bool tmp_child_is_deref_of_parent = false;
6024 return pointee_clang_type.GetChildClangTypeAtIndex (exe_ctx,
6025 idx,
6026 transparent_pointers,
6027 omit_empty_base_classes,
6028 ignore_array_bounds,
6029 child_name,
6030 child_byte_size,
6031 child_byte_offset,
6032 child_bitfield_bit_size,
6033 child_bitfield_bit_offset,
6034 child_is_base_class,
6035 tmp_child_is_deref_of_parent,
6036 valobj);
6037 }
6038 else
6039 {
6040 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
6041 if (parent_name)
6042 {
6043 child_name.assign(1, '&');
6044 child_name += parent_name;
6045 }
6046
6047 // We have a pointer to an simple type
6048 if (idx == 0)
6049 {
6050 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6051 child_byte_offset = 0;
6052 return pointee_clang_type;
6053 }
6054 }
6055 }
6056 break;
6057
6058 case clang::Type::Typedef:
6059 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006060 CompilerType typedefed_clang_type (getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006061 return typedefed_clang_type.GetChildClangTypeAtIndex (exe_ctx,
6062 idx,
6063 transparent_pointers,
6064 omit_empty_base_classes,
6065 ignore_array_bounds,
6066 child_name,
6067 child_byte_size,
6068 child_byte_offset,
6069 child_bitfield_bit_size,
6070 child_bitfield_bit_offset,
6071 child_is_base_class,
6072 child_is_deref_of_parent,
6073 valobj);
6074 }
6075 break;
6076
6077 case clang::Type::Elaborated:
6078 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006079 CompilerType elaborated_clang_type (getASTContext(), llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006080 return elaborated_clang_type.GetChildClangTypeAtIndex (exe_ctx,
6081 idx,
6082 transparent_pointers,
6083 omit_empty_base_classes,
6084 ignore_array_bounds,
6085 child_name,
6086 child_byte_size,
6087 child_byte_offset,
6088 child_bitfield_bit_size,
6089 child_bitfield_bit_offset,
6090 child_is_base_class,
6091 child_is_deref_of_parent,
6092 valobj);
6093 }
6094
6095 case clang::Type::Paren:
6096 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006097 CompilerType paren_clang_type (getASTContext(), llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00006098 return paren_clang_type.GetChildClangTypeAtIndex (exe_ctx,
6099 idx,
6100 transparent_pointers,
6101 omit_empty_base_classes,
6102 ignore_array_bounds,
6103 child_name,
6104 child_byte_size,
6105 child_byte_offset,
6106 child_bitfield_bit_size,
6107 child_bitfield_bit_offset,
6108 child_is_base_class,
6109 child_is_deref_of_parent,
6110 valobj);
6111 }
6112
6113
6114 default:
6115 break;
6116 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00006117 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006118}
6119
6120static uint32_t
6121GetIndexForRecordBase
6122(
6123 const clang::RecordDecl *record_decl,
6124 const clang::CXXBaseSpecifier *base_spec,
6125 bool omit_empty_base_classes
6126 )
6127{
6128 uint32_t child_idx = 0;
6129
6130 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6131
6132 // const char *super_name = record_decl->getNameAsCString();
6133 // const char *base_name = base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6134 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6135 //
6136 if (cxx_record_decl)
6137 {
6138 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6139 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6140 base_class != base_class_end;
6141 ++base_class)
6142 {
6143 if (omit_empty_base_classes)
6144 {
6145 if (BaseSpecifierIsEmpty (base_class))
6146 continue;
6147 }
6148
6149 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
6150 // child_idx,
6151 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6152 //
6153 //
6154 if (base_class == base_spec)
6155 return child_idx;
6156 ++child_idx;
6157 }
6158 }
6159
6160 return UINT32_MAX;
6161}
6162
6163
6164static uint32_t
6165GetIndexForRecordChild (const clang::RecordDecl *record_decl,
6166 clang::NamedDecl *canonical_decl,
6167 bool omit_empty_base_classes)
6168{
6169 uint32_t child_idx = ClangASTContext::GetNumBaseClasses (llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6170 omit_empty_base_classes);
6171
6172 clang::RecordDecl::field_iterator field, field_end;
6173 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6174 field != field_end;
6175 ++field, ++child_idx)
6176 {
6177 if (field->getCanonicalDecl() == canonical_decl)
6178 return child_idx;
6179 }
6180
6181 return UINT32_MAX;
6182}
6183
6184// Look for a child member (doesn't include base classes, but it does include
6185// their members) in the type hierarchy. Returns an index path into "clang_type"
6186// on how to reach the appropriate member.
6187//
6188// class A
6189// {
6190// public:
6191// int m_a;
6192// int m_b;
6193// };
6194//
6195// class B
6196// {
6197// };
6198//
6199// class C :
6200// public B,
6201// public A
6202// {
6203// };
6204//
6205// If we have a clang type that describes "class C", and we wanted to looked
6206// "m_b" in it:
6207//
6208// With omit_empty_base_classes == false we would get an integer array back with:
6209// { 1, 1 }
6210// The first index 1 is the child index for "class A" within class C
6211// The second index 1 is the child index for "m_b" within class A
6212//
6213// With omit_empty_base_classes == true we would get an integer array back with:
6214// { 0, 1 }
6215// The first index 0 is the child index for "class A" within class C (since class B doesn't have any members it doesn't count)
6216// The second index 1 is the child index for "m_b" within class A
6217
6218size_t
6219ClangASTContext::GetIndexOfChildMemberWithName (void* type, const char *name,
6220 bool omit_empty_base_classes,
6221 std::vector<uint32_t>& child_indexes)
6222{
6223 if (type && name && name[0])
6224 {
6225 clang::QualType qual_type(GetCanonicalQualType(type));
6226 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6227 switch (type_class)
6228 {
6229 case clang::Type::Record:
6230 if (GetCompleteType(type))
6231 {
6232 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6233 const clang::RecordDecl *record_decl = record_type->getDecl();
6234
6235 assert(record_decl);
6236 uint32_t child_idx = 0;
6237
6238 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6239
6240 // Try and find a field that matches NAME
6241 clang::RecordDecl::field_iterator field, field_end;
6242 llvm::StringRef name_sref(name);
6243 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6244 field != field_end;
6245 ++field, ++child_idx)
6246 {
6247 llvm::StringRef field_name = field->getName();
6248 if (field_name.empty())
6249 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006250 CompilerType field_type(getASTContext(),field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006251 child_indexes.push_back(child_idx);
6252 if (field_type.GetIndexOfChildMemberWithName(name, omit_empty_base_classes, child_indexes))
6253 return child_indexes.size();
6254 child_indexes.pop_back();
6255
6256 }
6257 else if (field_name.equals (name_sref))
6258 {
6259 // We have to add on the number of base classes to this index!
6260 child_indexes.push_back (child_idx + ClangASTContext::GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
6261 return child_indexes.size();
6262 }
6263 }
6264
6265 if (cxx_record_decl)
6266 {
6267 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
6268
6269 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
6270
6271 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
6272 // Didn't find things easily, lets let clang do its thang...
6273 clang::IdentifierInfo & ident_ref = getASTContext()->Idents.get(name_sref);
6274 clang::DeclarationName decl_name(&ident_ref);
6275
6276 clang::CXXBasePaths paths;
6277 if (cxx_record_decl->lookupInBases([decl_name](const clang::CXXBaseSpecifier *specifier, clang::CXXBasePath &path) {
6278 return clang::CXXRecordDecl::FindOrdinaryMember(specifier, path, decl_name);
6279 },
6280 paths))
6281 {
6282 clang::CXXBasePaths::const_paths_iterator path, path_end = paths.end();
6283 for (path = paths.begin(); path != path_end; ++path)
6284 {
6285 const size_t num_path_elements = path->size();
6286 for (size_t e=0; e<num_path_elements; ++e)
6287 {
6288 clang::CXXBasePathElement elem = (*path)[e];
6289
6290 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
6291 if (child_idx == UINT32_MAX)
6292 {
6293 child_indexes.clear();
6294 return 0;
6295 }
6296 else
6297 {
6298 child_indexes.push_back (child_idx);
6299 parent_record_decl = llvm::cast<clang::RecordDecl>(elem.Base->getType()->getAs<clang::RecordType>()->getDecl());
6300 }
6301 }
6302 for (clang::NamedDecl *path_decl : path->Decls)
6303 {
6304 child_idx = GetIndexForRecordChild (parent_record_decl, path_decl, omit_empty_base_classes);
6305 if (child_idx == UINT32_MAX)
6306 {
6307 child_indexes.clear();
6308 return 0;
6309 }
6310 else
6311 {
6312 child_indexes.push_back (child_idx);
6313 }
6314 }
6315 }
6316 return child_indexes.size();
6317 }
6318 }
6319
6320 }
6321 break;
6322
6323 case clang::Type::ObjCObject:
6324 case clang::Type::ObjCInterface:
6325 if (GetCompleteType(type))
6326 {
6327 llvm::StringRef name_sref(name);
6328 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6329 assert (objc_class_type);
6330 if (objc_class_type)
6331 {
6332 uint32_t child_idx = 0;
6333 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6334
6335 if (class_interface_decl)
6336 {
6337 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6338 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6339
6340 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6341 {
6342 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6343
6344 if (ivar_decl->getName().equals (name_sref))
6345 {
6346 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6347 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6348 ++child_idx;
6349
6350 child_indexes.push_back (child_idx);
6351 return child_indexes.size();
6352 }
6353 }
6354
6355 if (superclass_interface_decl)
6356 {
6357 // The super class index is always zero for ObjC classes,
6358 // so we push it onto the child indexes in case we find
6359 // an ivar in our superclass...
6360 child_indexes.push_back (0);
6361
Greg Claytona1e5dc82015-08-11 22:53:00 +00006362 CompilerType superclass_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00006363 if (superclass_clang_type.GetIndexOfChildMemberWithName (name,
6364 omit_empty_base_classes,
6365 child_indexes))
6366 {
6367 // We did find an ivar in a superclass so just
6368 // return the results!
6369 return child_indexes.size();
6370 }
6371
6372 // We didn't find an ivar matching "name" in our
6373 // superclass, pop the superclass zero index that
6374 // we pushed on above.
6375 child_indexes.pop_back();
6376 }
6377 }
6378 }
6379 }
6380 break;
6381
6382 case clang::Type::ObjCObjectPointer:
6383 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006384 CompilerType objc_object_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006385 return objc_object_clang_type.GetIndexOfChildMemberWithName (name,
6386 omit_empty_base_classes,
6387 child_indexes);
6388 }
6389 break;
6390
6391
6392 case clang::Type::ConstantArray:
6393 {
6394 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6395 // const uint64_t element_count = array->getSize().getLimitedValue();
6396 //
6397 // if (idx < element_count)
6398 // {
6399 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6400 //
6401 // char element_name[32];
6402 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6403 //
6404 // child_name.assign(element_name);
6405 // assert(field_type_info.first % 8 == 0);
6406 // child_byte_size = field_type_info.first / 8;
6407 // child_byte_offset = idx * child_byte_size;
6408 // return array->getElementType().getAsOpaquePtr();
6409 // }
6410 }
6411 break;
6412
6413 // case clang::Type::MemberPointerType:
6414 // {
6415 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6416 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6417 //
6418 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6419 // {
6420 // return GetIndexOfChildWithName (ast,
6421 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6422 // name);
6423 // }
6424 // }
6425 // break;
6426 //
6427 case clang::Type::LValueReference:
6428 case clang::Type::RValueReference:
6429 {
6430 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
6431 clang::QualType pointee_type(reference_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006432 CompilerType pointee_clang_type (getASTContext(), pointee_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00006433
6434 if (pointee_clang_type.IsAggregateType ())
6435 {
6436 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6437 omit_empty_base_classes,
6438 child_indexes);
6439 }
6440 }
6441 break;
6442
6443 case clang::Type::Pointer:
6444 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006445 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00006446
6447 if (pointee_clang_type.IsAggregateType ())
6448 {
6449 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6450 omit_empty_base_classes,
6451 child_indexes);
6452 }
6453 }
6454 break;
6455
6456 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006457 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006458 omit_empty_base_classes,
6459 child_indexes);
6460
6461 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006462 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006463 omit_empty_base_classes,
6464 child_indexes);
6465
6466 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006467 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006468 omit_empty_base_classes,
6469 child_indexes);
6470
6471 default:
6472 break;
6473 }
6474 }
6475 return 0;
6476}
6477
6478
6479// Get the index of the child of "clang_type" whose name matches. This function
6480// doesn't descend into the children, but only looks one level deep and name
6481// matches can include base class names.
6482
6483uint32_t
6484ClangASTContext::GetIndexOfChildWithName (void* type, const char *name, bool omit_empty_base_classes)
6485{
6486 if (type && name && name[0])
6487 {
6488 clang::QualType qual_type(GetCanonicalQualType(type));
6489
6490 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6491
6492 switch (type_class)
6493 {
6494 case clang::Type::Record:
6495 if (GetCompleteType(type))
6496 {
6497 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6498 const clang::RecordDecl *record_decl = record_type->getDecl();
6499
6500 assert(record_decl);
6501 uint32_t child_idx = 0;
6502
6503 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6504
6505 if (cxx_record_decl)
6506 {
6507 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6508 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6509 base_class != base_class_end;
6510 ++base_class)
6511 {
6512 // Skip empty base classes
6513 clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
6514 if (omit_empty_base_classes && ClangASTContext::RecordHasFields(base_class_decl) == false)
6515 continue;
6516
Greg Claytona1e5dc82015-08-11 22:53:00 +00006517 CompilerType base_class_clang_type (getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006518 std::string base_class_type_name (base_class_clang_type.GetTypeName().AsCString(""));
6519 if (base_class_type_name.compare (name) == 0)
6520 return child_idx;
6521 ++child_idx;
6522 }
6523 }
6524
6525 // Try and find a field that matches NAME
6526 clang::RecordDecl::field_iterator field, field_end;
6527 llvm::StringRef name_sref(name);
6528 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6529 field != field_end;
6530 ++field, ++child_idx)
6531 {
6532 if (field->getName().equals (name_sref))
6533 return child_idx;
6534 }
6535
6536 }
6537 break;
6538
6539 case clang::Type::ObjCObject:
6540 case clang::Type::ObjCInterface:
6541 if (GetCompleteType(type))
6542 {
6543 llvm::StringRef name_sref(name);
6544 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6545 assert (objc_class_type);
6546 if (objc_class_type)
6547 {
6548 uint32_t child_idx = 0;
6549 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6550
6551 if (class_interface_decl)
6552 {
6553 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6554 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6555
6556 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6557 {
6558 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6559
6560 if (ivar_decl->getName().equals (name_sref))
6561 {
6562 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6563 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6564 ++child_idx;
6565
6566 return child_idx;
6567 }
6568 }
6569
6570 if (superclass_interface_decl)
6571 {
6572 if (superclass_interface_decl->getName().equals (name_sref))
6573 return 0;
6574 }
6575 }
6576 }
6577 }
6578 break;
6579
6580 case clang::Type::ObjCObjectPointer:
6581 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006582 CompilerType pointee_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006583 return pointee_clang_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6584 }
6585 break;
6586
6587 case clang::Type::ConstantArray:
6588 {
6589 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6590 // const uint64_t element_count = array->getSize().getLimitedValue();
6591 //
6592 // if (idx < element_count)
6593 // {
6594 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6595 //
6596 // char element_name[32];
6597 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6598 //
6599 // child_name.assign(element_name);
6600 // assert(field_type_info.first % 8 == 0);
6601 // child_byte_size = field_type_info.first / 8;
6602 // child_byte_offset = idx * child_byte_size;
6603 // return array->getElementType().getAsOpaquePtr();
6604 // }
6605 }
6606 break;
6607
6608 // case clang::Type::MemberPointerType:
6609 // {
6610 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6611 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6612 //
6613 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6614 // {
6615 // return GetIndexOfChildWithName (ast,
6616 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6617 // name);
6618 // }
6619 // }
6620 // break;
6621 //
6622 case clang::Type::LValueReference:
6623 case clang::Type::RValueReference:
6624 {
6625 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006626 CompilerType pointee_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006627
6628 if (pointee_type.IsAggregateType ())
6629 {
6630 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6631 }
6632 }
6633 break;
6634
6635 case clang::Type::Pointer:
6636 {
6637 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006638 CompilerType pointee_type (getASTContext(), pointer_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006639
6640 if (pointee_type.IsAggregateType ())
6641 {
6642 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6643 }
6644 else
6645 {
6646 // if (parent_name)
6647 // {
6648 // child_name.assign(1, '*');
6649 // child_name += parent_name;
6650 // }
6651 //
6652 // // We have a pointer to an simple type
6653 // if (idx == 0)
6654 // {
6655 // std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
6656 // assert(clang_type_info.first % 8 == 0);
6657 // child_byte_size = clang_type_info.first / 8;
6658 // child_byte_offset = 0;
6659 // return pointee_type.getAsOpaquePtr();
6660 // }
6661 }
6662 }
6663 break;
6664
6665 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006666 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006667
6668 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006669 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006670
6671 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006672 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006673
6674 default:
6675 break;
6676 }
6677 }
6678 return UINT32_MAX;
6679}
6680
6681
6682size_t
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006683ClangASTContext::GetNumTemplateArguments (void* type)
Greg Claytond8d4a572015-08-11 21:38:15 +00006684{
6685 if (!type)
6686 return 0;
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006687
6688 clang::QualType qual_type (GetCanonicalQualType(type));
6689 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6690 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00006691 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006692 case clang::Type::Record:
6693 if (GetCompleteType(type))
6694 {
6695 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
6696 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00006697 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006698 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
6699 if (template_decl)
6700 return template_decl->getTemplateArgs().size();
Greg Claytond8d4a572015-08-11 21:38:15 +00006701 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006702 }
6703 break;
6704
6705 case clang::Type::Typedef:
6706 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetNumTemplateArguments();
6707
6708 case clang::Type::Elaborated:
6709 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetNumTemplateArguments();
6710
6711 case clang::Type::Paren:
6712 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetNumTemplateArguments();
6713
6714 default:
6715 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006716 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006717
Greg Claytond8d4a572015-08-11 21:38:15 +00006718 return 0;
6719}
6720
Greg Claytona1e5dc82015-08-11 22:53:00 +00006721CompilerType
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006722ClangASTContext::GetTemplateArgument (void* type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
Greg Claytond8d4a572015-08-11 21:38:15 +00006723{
6724 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006725 return CompilerType();
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006726
6727 clang::QualType qual_type (GetCanonicalQualType(type));
6728 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6729 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00006730 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006731 case clang::Type::Record:
6732 if (GetCompleteType(type))
6733 {
6734 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
6735 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00006736 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006737 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
6738 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
Greg Claytond8d4a572015-08-11 21:38:15 +00006739 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006740 const clang::TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
6741 switch (template_arg.getKind())
Greg Claytond8d4a572015-08-11 21:38:15 +00006742 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006743 case clang::TemplateArgument::Null:
6744 kind = eTemplateArgumentKindNull;
6745 return CompilerType();
6746
6747 case clang::TemplateArgument::Type:
6748 kind = eTemplateArgumentKindType;
6749 return CompilerType(getASTContext(), template_arg.getAsType());
6750
6751 case clang::TemplateArgument::Declaration:
6752 kind = eTemplateArgumentKindDeclaration;
6753 return CompilerType();
6754
6755 case clang::TemplateArgument::Integral:
6756 kind = eTemplateArgumentKindIntegral;
6757 return CompilerType(getASTContext(), template_arg.getIntegralType());
6758
6759 case clang::TemplateArgument::Template:
6760 kind = eTemplateArgumentKindTemplate;
6761 return CompilerType();
6762
6763 case clang::TemplateArgument::TemplateExpansion:
6764 kind = eTemplateArgumentKindTemplateExpansion;
6765 return CompilerType();
6766
6767 case clang::TemplateArgument::Expression:
6768 kind = eTemplateArgumentKindExpression;
6769 return CompilerType();
6770
6771 case clang::TemplateArgument::Pack:
6772 kind = eTemplateArgumentKindPack;
6773 return CompilerType();
6774
6775 default:
6776 assert (!"Unhandled clang::TemplateArgument::ArgKind");
6777 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006778 }
6779 }
6780 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006781 }
6782 break;
6783
6784 case clang::Type::Typedef:
6785 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetTemplateArgument(arg_idx, kind);
6786
6787 case clang::Type::Elaborated:
6788 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetTemplateArgument(arg_idx, kind);
6789
6790 case clang::Type::Paren:
6791 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetTemplateArgument(arg_idx, kind);
6792
6793 default:
6794 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006795 }
6796 kind = eTemplateArgumentKindNull;
Greg Claytona1e5dc82015-08-11 22:53:00 +00006797 return CompilerType ();
Greg Claytond8d4a572015-08-11 21:38:15 +00006798}
6799
6800static bool
6801IsOperator (const char *name, clang::OverloadedOperatorKind &op_kind)
6802{
6803 if (name == nullptr || name[0] == '\0')
6804 return false;
6805
6806#define OPERATOR_PREFIX "operator"
6807#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
6808
6809 const char *post_op_name = nullptr;
6810
6811 bool no_space = true;
6812
6813 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
6814 return false;
6815
6816 post_op_name = name + OPERATOR_PREFIX_LENGTH;
6817
6818 if (post_op_name[0] == ' ')
6819 {
6820 post_op_name++;
6821 no_space = false;
6822 }
6823
6824#undef OPERATOR_PREFIX
6825#undef OPERATOR_PREFIX_LENGTH
6826
6827 // This is an operator, set the overloaded operator kind to invalid
6828 // in case this is a conversion operator...
6829 op_kind = clang::NUM_OVERLOADED_OPERATORS;
6830
6831 switch (post_op_name[0])
6832 {
6833 default:
6834 if (no_space)
6835 return false;
6836 break;
6837 case 'n':
6838 if (no_space)
6839 return false;
6840 if (strcmp (post_op_name, "new") == 0)
6841 op_kind = clang::OO_New;
6842 else if (strcmp (post_op_name, "new[]") == 0)
6843 op_kind = clang::OO_Array_New;
6844 break;
6845
6846 case 'd':
6847 if (no_space)
6848 return false;
6849 if (strcmp (post_op_name, "delete") == 0)
6850 op_kind = clang::OO_Delete;
6851 else if (strcmp (post_op_name, "delete[]") == 0)
6852 op_kind = clang::OO_Array_Delete;
6853 break;
6854
6855 case '+':
6856 if (post_op_name[1] == '\0')
6857 op_kind = clang::OO_Plus;
6858 else if (post_op_name[2] == '\0')
6859 {
6860 if (post_op_name[1] == '=')
6861 op_kind = clang::OO_PlusEqual;
6862 else if (post_op_name[1] == '+')
6863 op_kind = clang::OO_PlusPlus;
6864 }
6865 break;
6866
6867 case '-':
6868 if (post_op_name[1] == '\0')
6869 op_kind = clang::OO_Minus;
6870 else if (post_op_name[2] == '\0')
6871 {
6872 switch (post_op_name[1])
6873 {
6874 case '=': op_kind = clang::OO_MinusEqual; break;
6875 case '-': op_kind = clang::OO_MinusMinus; break;
6876 case '>': op_kind = clang::OO_Arrow; break;
6877 }
6878 }
6879 else if (post_op_name[3] == '\0')
6880 {
6881 if (post_op_name[2] == '*')
6882 op_kind = clang::OO_ArrowStar; break;
6883 }
6884 break;
6885
6886 case '*':
6887 if (post_op_name[1] == '\0')
6888 op_kind = clang::OO_Star;
6889 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6890 op_kind = clang::OO_StarEqual;
6891 break;
6892
6893 case '/':
6894 if (post_op_name[1] == '\0')
6895 op_kind = clang::OO_Slash;
6896 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6897 op_kind = clang::OO_SlashEqual;
6898 break;
6899
6900 case '%':
6901 if (post_op_name[1] == '\0')
6902 op_kind = clang::OO_Percent;
6903 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6904 op_kind = clang::OO_PercentEqual;
6905 break;
6906
6907
6908 case '^':
6909 if (post_op_name[1] == '\0')
6910 op_kind = clang::OO_Caret;
6911 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6912 op_kind = clang::OO_CaretEqual;
6913 break;
6914
6915 case '&':
6916 if (post_op_name[1] == '\0')
6917 op_kind = clang::OO_Amp;
6918 else if (post_op_name[2] == '\0')
6919 {
6920 switch (post_op_name[1])
6921 {
6922 case '=': op_kind = clang::OO_AmpEqual; break;
6923 case '&': op_kind = clang::OO_AmpAmp; break;
6924 }
6925 }
6926 break;
6927
6928 case '|':
6929 if (post_op_name[1] == '\0')
6930 op_kind = clang::OO_Pipe;
6931 else if (post_op_name[2] == '\0')
6932 {
6933 switch (post_op_name[1])
6934 {
6935 case '=': op_kind = clang::OO_PipeEqual; break;
6936 case '|': op_kind = clang::OO_PipePipe; break;
6937 }
6938 }
6939 break;
6940
6941 case '~':
6942 if (post_op_name[1] == '\0')
6943 op_kind = clang::OO_Tilde;
6944 break;
6945
6946 case '!':
6947 if (post_op_name[1] == '\0')
6948 op_kind = clang::OO_Exclaim;
6949 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6950 op_kind = clang::OO_ExclaimEqual;
6951 break;
6952
6953 case '=':
6954 if (post_op_name[1] == '\0')
6955 op_kind = clang::OO_Equal;
6956 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6957 op_kind = clang::OO_EqualEqual;
6958 break;
6959
6960 case '<':
6961 if (post_op_name[1] == '\0')
6962 op_kind = clang::OO_Less;
6963 else if (post_op_name[2] == '\0')
6964 {
6965 switch (post_op_name[1])
6966 {
6967 case '<': op_kind = clang::OO_LessLess; break;
6968 case '=': op_kind = clang::OO_LessEqual; break;
6969 }
6970 }
6971 else if (post_op_name[3] == '\0')
6972 {
6973 if (post_op_name[2] == '=')
6974 op_kind = clang::OO_LessLessEqual;
6975 }
6976 break;
6977
6978 case '>':
6979 if (post_op_name[1] == '\0')
6980 op_kind = clang::OO_Greater;
6981 else if (post_op_name[2] == '\0')
6982 {
6983 switch (post_op_name[1])
6984 {
6985 case '>': op_kind = clang::OO_GreaterGreater; break;
6986 case '=': op_kind = clang::OO_GreaterEqual; break;
6987 }
6988 }
6989 else if (post_op_name[1] == '>' &&
6990 post_op_name[2] == '=' &&
6991 post_op_name[3] == '\0')
6992 {
6993 op_kind = clang::OO_GreaterGreaterEqual;
6994 }
6995 break;
6996
6997 case ',':
6998 if (post_op_name[1] == '\0')
6999 op_kind = clang::OO_Comma;
7000 break;
7001
7002 case '(':
7003 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
7004 op_kind = clang::OO_Call;
7005 break;
7006
7007 case '[':
7008 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
7009 op_kind = clang::OO_Subscript;
7010 break;
7011 }
7012
7013 return true;
7014}
7015
7016clang::EnumDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007017ClangASTContext::GetAsEnumDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007018{
7019 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
7020 if (enutype)
7021 return enutype->getDecl();
7022 return NULL;
7023}
7024
7025clang::RecordDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007026ClangASTContext::GetAsRecordDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007027{
7028 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(GetCanonicalQualType(type));
7029 if (record_type)
7030 return record_type->getDecl();
7031 return nullptr;
7032}
7033
7034clang::CXXRecordDecl *
7035ClangASTContext::GetAsCXXRecordDecl (void* type)
7036{
7037 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
7038}
7039
7040clang::ObjCInterfaceDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007041ClangASTContext::GetAsObjCInterfaceDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007042{
7043 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(GetCanonicalQualType(type));
7044 if (objc_class_type)
7045 return objc_class_type->getInterface();
7046 return nullptr;
7047}
7048
7049clang::FieldDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007050ClangASTContext::AddFieldToRecordType (const CompilerType& type, const char *name,
7051 const CompilerType &field_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007052 AccessType access,
7053 uint32_t bitfield_bit_size)
7054{
7055 if (!type.IsValid() || !field_clang_type.IsValid())
7056 return nullptr;
7057 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
7058 if (!ast)
7059 return nullptr;
7060 clang::ASTContext* clang_ast = ast->getASTContext();
7061
7062 clang::FieldDecl *field = nullptr;
7063
7064 clang::Expr *bit_width = nullptr;
7065 if (bitfield_bit_size != 0)
7066 {
7067 llvm::APInt bitfield_bit_size_apint(clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7068 bit_width = new (*clang_ast)clang::IntegerLiteral (*clang_ast, bitfield_bit_size_apint, clang_ast->IntTy, clang::SourceLocation());
7069 }
7070
7071 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
7072 if (record_decl)
7073 {
7074 field = clang::FieldDecl::Create (*clang_ast,
7075 record_decl,
7076 clang::SourceLocation(),
7077 clang::SourceLocation(),
7078 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7079 GetQualType(field_clang_type), // Field type
7080 nullptr, // TInfo *
7081 bit_width, // BitWidth
7082 false, // Mutable
7083 clang::ICIS_NoInit); // HasInit
7084
7085 if (!name)
7086 {
7087 // Determine whether this field corresponds to an anonymous
7088 // struct or union.
7089 if (const clang::TagType *TagT = field->getType()->getAs<clang::TagType>()) {
7090 if (clang::RecordDecl *Rec = llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7091 if (!Rec->getDeclName()) {
7092 Rec->setAnonymousStructOrUnion(true);
7093 field->setImplicit();
7094
7095 }
7096 }
7097 }
7098
7099 if (field)
7100 {
7101 field->setAccess (ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7102
7103 record_decl->addDecl(field);
7104
7105#ifdef LLDB_CONFIGURATION_DEBUG
7106 VerifyDecl(field);
7107#endif
7108 }
7109 }
7110 else
7111 {
7112 clang::ObjCInterfaceDecl *class_interface_decl = ast->GetAsObjCInterfaceDecl (type);
7113
7114 if (class_interface_decl)
7115 {
7116 const bool is_synthesized = false;
7117
7118 field_clang_type.GetCompleteType();
7119
7120 field = clang::ObjCIvarDecl::Create (*clang_ast,
7121 class_interface_decl,
7122 clang::SourceLocation(),
7123 clang::SourceLocation(),
7124 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7125 GetQualType(field_clang_type), // Field type
7126 nullptr, // TypeSourceInfo *
7127 ConvertAccessTypeToObjCIvarAccessControl (access),
7128 bit_width,
7129 is_synthesized);
7130
7131 if (field)
7132 {
7133 class_interface_decl->addDecl(field);
7134
7135#ifdef LLDB_CONFIGURATION_DEBUG
7136 VerifyDecl(field);
7137#endif
7138 }
7139 }
7140 }
7141 return field;
7142}
7143
7144void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007145ClangASTContext::BuildIndirectFields (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007146{
7147 ClangASTContext* ast = nullptr;
7148 if (type)
7149 ast = type.GetTypeSystem()->AsClangASTContext();
7150 if (!ast)
7151 return;
7152
7153 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7154
7155 if (!record_decl)
7156 return;
7157
7158 typedef llvm::SmallVector <clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7159
7160 IndirectFieldVector indirect_fields;
7161 clang::RecordDecl::field_iterator field_pos;
7162 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7163 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7164 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; last_field_pos = field_pos++)
7165 {
7166 if (field_pos->isAnonymousStructOrUnion())
7167 {
7168 clang::QualType field_qual_type = field_pos->getType();
7169
7170 const clang::RecordType *field_record_type = field_qual_type->getAs<clang::RecordType>();
7171
7172 if (!field_record_type)
7173 continue;
7174
7175 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7176
7177 if (!field_record_decl)
7178 continue;
7179
7180 for (clang::RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
7181 di != de;
7182 ++di)
7183 {
7184 if (clang::FieldDecl *nested_field_decl = llvm::dyn_cast<clang::FieldDecl>(*di))
7185 {
7186 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[2];
7187 chain[0] = *field_pos;
7188 chain[1] = nested_field_decl;
7189 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7190 record_decl,
7191 clang::SourceLocation(),
7192 nested_field_decl->getIdentifier(),
7193 nested_field_decl->getType(),
7194 chain,
7195 2);
7196
7197 indirect_field->setImplicit();
7198
7199 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7200 nested_field_decl->getAccess()));
7201
7202 indirect_fields.push_back(indirect_field);
7203 }
7204 else if (clang::IndirectFieldDecl *nested_indirect_field_decl = llvm::dyn_cast<clang::IndirectFieldDecl>(*di))
7205 {
7206 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
7207 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[nested_chain_size + 1];
7208 chain[0] = *field_pos;
7209
7210 int chain_index = 1;
7211 for (clang::IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
7212 nce = nested_indirect_field_decl->chain_end();
7213 nci < nce;
7214 ++nci)
7215 {
7216 chain[chain_index] = *nci;
7217 chain_index++;
7218 }
7219
7220 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7221 record_decl,
7222 clang::SourceLocation(),
7223 nested_indirect_field_decl->getIdentifier(),
7224 nested_indirect_field_decl->getType(),
7225 chain,
7226 nested_chain_size + 1);
7227
7228 indirect_field->setImplicit();
7229
7230 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7231 nested_indirect_field_decl->getAccess()));
7232
7233 indirect_fields.push_back(indirect_field);
7234 }
7235 }
7236 }
7237 }
7238
7239 // Check the last field to see if it has an incomplete array type as its
7240 // last member and if it does, the tell the record decl about it
7241 if (last_field_pos != field_end_pos)
7242 {
7243 if (last_field_pos->getType()->isIncompleteArrayType())
7244 record_decl->hasFlexibleArrayMember();
7245 }
7246
7247 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
7248 ifi < ife;
7249 ++ifi)
7250 {
7251 record_decl->addDecl(*ifi);
7252 }
7253}
7254
7255void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007256ClangASTContext::SetIsPacked (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007257{
7258 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
7259
7260 if (!record_decl)
7261 return;
7262
7263 record_decl->addAttr(clang::PackedAttr::CreateImplicit(*type.GetTypeSystem()->AsClangASTContext()->getASTContext()));
7264}
7265
7266clang::VarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007267ClangASTContext::AddVariableToRecordType (const CompilerType& type, const char *name,
7268 const CompilerType &var_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007269 AccessType access)
7270{
7271 clang::VarDecl *var_decl = nullptr;
7272
7273 if (!type.IsValid() || !var_type.IsValid())
7274 return nullptr;
7275 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
7276 if (!ast)
7277 return nullptr;
7278
7279 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
7280 if (record_decl)
7281 {
7282 var_decl = clang::VarDecl::Create (*ast->getASTContext(), // ASTContext &
7283 record_decl, // DeclContext *
7284 clang::SourceLocation(), // clang::SourceLocation StartLoc
7285 clang::SourceLocation(), // clang::SourceLocation IdLoc
7286 name ? &ast->getASTContext()->Idents.get(name) : nullptr, // clang::IdentifierInfo *
7287 GetQualType(var_type), // Variable clang::QualType
7288 nullptr, // TypeSourceInfo *
7289 clang::SC_Static); // StorageClass
7290 if (var_decl)
7291 {
7292 var_decl->setAccess(ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7293 record_decl->addDecl(var_decl);
7294
7295#ifdef LLDB_CONFIGURATION_DEBUG
7296 VerifyDecl(var_decl);
7297#endif
7298 }
7299 }
7300 return var_decl;
7301}
7302
7303
7304clang::CXXMethodDecl *
7305ClangASTContext::AddMethodToCXXRecordType (void* type, const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007306 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007307 lldb::AccessType access,
7308 bool is_virtual,
7309 bool is_static,
7310 bool is_inline,
7311 bool is_explicit,
7312 bool is_attr_used,
7313 bool is_artificial)
7314{
7315 if (!type || !method_clang_type.IsValid() || name == nullptr || name[0] == '\0')
7316 return nullptr;
7317
7318 clang::QualType record_qual_type(GetCanonicalQualType(type));
7319
7320 clang::CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
7321
7322 if (cxx_record_decl == nullptr)
7323 return nullptr;
7324
7325 clang::QualType method_qual_type (GetQualType(method_clang_type));
7326
7327 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7328
7329 clang::DeclarationName decl_name (&getASTContext()->Idents.get(name));
7330
7331 const clang::FunctionType *function_type = llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7332
7333 if (function_type == nullptr)
7334 return nullptr;
7335
7336 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7337
7338 if (!method_function_prototype)
7339 return nullptr;
7340
7341 unsigned int num_params = method_function_prototype->getNumParams();
7342
7343 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7344 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7345
7346 if (is_artificial)
7347 return nullptr; // skip everything artificial
7348
7349 if (name[0] == '~')
7350 {
7351 cxx_dtor_decl = clang::CXXDestructorDecl::Create (*getASTContext(),
7352 cxx_record_decl,
7353 clang::SourceLocation(),
7354 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXDestructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7355 method_qual_type,
7356 nullptr,
7357 is_inline,
7358 is_artificial);
7359 cxx_method_decl = cxx_dtor_decl;
7360 }
7361 else if (decl_name == cxx_record_decl->getDeclName())
7362 {
7363 cxx_ctor_decl = clang::CXXConstructorDecl::Create (*getASTContext(),
7364 cxx_record_decl,
7365 clang::SourceLocation(),
7366 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConstructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7367 method_qual_type,
7368 nullptr, // TypeSourceInfo *
7369 is_explicit,
7370 is_inline,
7371 is_artificial,
7372 false /*is_constexpr*/);
7373 cxx_method_decl = cxx_ctor_decl;
7374 }
7375 else
7376 {
7377 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7378 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7379
7380 if (IsOperator (name, op_kind))
7381 {
7382 if (op_kind != clang::NUM_OVERLOADED_OPERATORS)
7383 {
7384 // Check the number of operator parameters. Sometimes we have
7385 // seen bad DWARF that doesn't correctly describe operators and
7386 // if we try to create a method and add it to the class, clang
7387 // will assert and crash, so we need to make sure things are
7388 // acceptable.
7389 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
7390 return nullptr;
7391 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7392 cxx_record_decl,
7393 clang::SourceLocation(),
7394 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXOperatorName (op_kind), clang::SourceLocation()),
7395 method_qual_type,
7396 nullptr, // TypeSourceInfo *
7397 SC,
7398 is_inline,
7399 false /*is_constexpr*/,
7400 clang::SourceLocation());
7401 }
7402 else if (num_params == 0)
7403 {
7404 // Conversion operators don't take params...
7405 cxx_method_decl = clang::CXXConversionDecl::Create (*getASTContext(),
7406 cxx_record_decl,
7407 clang::SourceLocation(),
7408 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConversionFunctionName (getASTContext()->getCanonicalType (function_type->getReturnType())), clang::SourceLocation()),
7409 method_qual_type,
7410 nullptr, // TypeSourceInfo *
7411 is_inline,
7412 is_explicit,
7413 false /*is_constexpr*/,
7414 clang::SourceLocation());
7415 }
7416 }
7417
7418 if (cxx_method_decl == nullptr)
7419 {
7420 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7421 cxx_record_decl,
7422 clang::SourceLocation(),
7423 clang::DeclarationNameInfo (decl_name, clang::SourceLocation()),
7424 method_qual_type,
7425 nullptr, // TypeSourceInfo *
7426 SC,
7427 is_inline,
7428 false /*is_constexpr*/,
7429 clang::SourceLocation());
7430 }
7431 }
7432
7433 clang::AccessSpecifier access_specifier = ClangASTContext::ConvertAccessTypeToAccessSpecifier (access);
7434
7435 cxx_method_decl->setAccess (access_specifier);
7436 cxx_method_decl->setVirtualAsWritten (is_virtual);
7437
7438 if (is_attr_used)
7439 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
7440
7441 // Populate the method decl with parameter decls
7442
7443 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
7444
7445 for (unsigned param_index = 0;
7446 param_index < num_params;
7447 ++param_index)
7448 {
7449 params.push_back (clang::ParmVarDecl::Create (*getASTContext(),
7450 cxx_method_decl,
7451 clang::SourceLocation(),
7452 clang::SourceLocation(),
7453 nullptr, // anonymous
7454 method_function_prototype->getParamType(param_index),
7455 nullptr,
7456 clang::SC_None,
7457 nullptr));
7458 }
7459
7460 cxx_method_decl->setParams (llvm::ArrayRef<clang::ParmVarDecl*>(params));
7461
7462 cxx_record_decl->addDecl (cxx_method_decl);
7463
7464 // Sometimes the debug info will mention a constructor (default/copy/move),
7465 // destructor, or assignment operator (copy/move) but there won't be any
7466 // version of this in the code. So we check if the function was artificially
7467 // generated and if it is trivial and this lets the compiler/backend know
7468 // that it can inline the IR for these when it needs to and we can avoid a
7469 // "missing function" error when running expressions.
7470
7471 if (is_artificial)
7472 {
7473 if (cxx_ctor_decl &&
7474 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
7475 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
7476 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
7477 {
7478 cxx_ctor_decl->setDefaulted();
7479 cxx_ctor_decl->setTrivial(true);
7480 }
7481 else if (cxx_dtor_decl)
7482 {
7483 if (cxx_record_decl->hasTrivialDestructor())
7484 {
7485 cxx_dtor_decl->setDefaulted();
7486 cxx_dtor_decl->setTrivial(true);
7487 }
7488 }
7489 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
7490 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
7491 {
7492 cxx_method_decl->setDefaulted();
7493 cxx_method_decl->setTrivial(true);
7494 }
7495 }
7496
7497#ifdef LLDB_CONFIGURATION_DEBUG
7498 VerifyDecl(cxx_method_decl);
7499#endif
7500
7501 // printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
7502 // printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
7503 // printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
7504 // printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
7505 // printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
7506 // printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
7507 // printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
7508 // printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
7509 // printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
7510 return cxx_method_decl;
7511}
7512
7513
7514#pragma mark C++ Base Classes
7515
7516clang::CXXBaseSpecifier *
7517ClangASTContext::CreateBaseClassSpecifier (void* type, AccessType access, bool is_virtual, bool base_of_class)
7518{
7519 if (type)
7520 return new clang::CXXBaseSpecifier (clang::SourceRange(),
7521 is_virtual,
7522 base_of_class,
7523 ClangASTContext::ConvertAccessTypeToAccessSpecifier (access),
7524 getASTContext()->getTrivialTypeSourceInfo (GetQualType(type)),
7525 clang::SourceLocation());
7526 return nullptr;
7527}
7528
7529void
7530ClangASTContext::DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes)
7531{
7532 for (unsigned i=0; i<num_base_classes; ++i)
7533 {
7534 delete base_classes[i];
7535 base_classes[i] = nullptr;
7536 }
7537}
7538
7539bool
7540ClangASTContext::SetBaseClassesForClassType (void* type, clang::CXXBaseSpecifier const * const *base_classes,
7541 unsigned num_base_classes)
7542{
7543 if (type)
7544 {
7545 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
7546 if (cxx_record_decl)
7547 {
7548 cxx_record_decl->setBases(base_classes, num_base_classes);
7549 return true;
7550 }
7551 }
7552 return false;
7553}
7554
7555bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007556ClangASTContext::SetObjCSuperClass (const CompilerType& type, const CompilerType &superclass_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007557{
7558 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
7559 if (!ast)
7560 return false;
7561 clang::ASTContext* clang_ast = ast->getASTContext();
7562
7563 if (type && superclass_clang_type.IsValid() && superclass_clang_type.GetTypeSystem() == type.GetTypeSystem())
7564 {
7565 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7566 clang::ObjCInterfaceDecl *super_interface_decl = GetAsObjCInterfaceDecl (superclass_clang_type);
7567 if (class_interface_decl && super_interface_decl)
7568 {
7569 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(clang_ast->getObjCInterfaceType(super_interface_decl)));
7570 return true;
7571 }
7572 }
7573 return false;
7574}
7575
7576bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007577ClangASTContext::AddObjCClassProperty (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007578 const char *property_name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007579 const CompilerType &property_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007580 clang::ObjCIvarDecl *ivar_decl,
7581 const char *property_setter_name,
7582 const char *property_getter_name,
7583 uint32_t property_attributes,
7584 ClangASTMetadata *metadata)
7585{
7586 if (!type || !property_clang_type.IsValid() || property_name == nullptr || property_name[0] == '\0')
7587 return false;
7588 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
7589 if (!ast)
7590 return false;
7591 clang::ASTContext* clang_ast = ast->getASTContext();
7592
7593 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7594
7595 if (class_interface_decl)
7596 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00007597 CompilerType property_clang_type_to_access;
Greg Claytond8d4a572015-08-11 21:38:15 +00007598
7599 if (property_clang_type.IsValid())
7600 property_clang_type_to_access = property_clang_type;
7601 else if (ivar_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00007602 property_clang_type_to_access = CompilerType (clang_ast, ivar_decl->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00007603
7604 if (class_interface_decl && property_clang_type_to_access.IsValid())
7605 {
7606 clang::TypeSourceInfo *prop_type_source;
7607 if (ivar_decl)
7608 prop_type_source = clang_ast->getTrivialTypeSourceInfo (ivar_decl->getType());
7609 else
7610 prop_type_source = clang_ast->getTrivialTypeSourceInfo (GetQualType(property_clang_type));
7611
7612 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create (*clang_ast,
7613 class_interface_decl,
7614 clang::SourceLocation(), // Source Location
7615 &clang_ast->Idents.get(property_name),
7616 clang::SourceLocation(), //Source Location for AT
7617 clang::SourceLocation(), //Source location for (
7618 ivar_decl ? ivar_decl->getType() : ClangASTContext::GetQualType(property_clang_type),
7619 prop_type_source);
7620
7621 if (property_decl)
7622 {
7623 if (metadata)
7624 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
7625
7626 class_interface_decl->addDecl (property_decl);
7627
7628 clang::Selector setter_sel, getter_sel;
7629
7630 if (property_setter_name != nullptr)
7631 {
7632 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
7633 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(property_setter_no_colon.c_str());
7634 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
7635 }
7636 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
7637 {
7638 std::string setter_sel_string("set");
7639 setter_sel_string.push_back(::toupper(property_name[0]));
7640 setter_sel_string.append(&property_name[1]);
7641 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(setter_sel_string.c_str());
7642 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
7643 }
7644 property_decl->setSetterName(setter_sel);
7645 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
7646
7647 if (property_getter_name != nullptr)
7648 {
7649 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_getter_name);
7650 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
7651 }
7652 else
7653 {
7654 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_name);
7655 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
7656 }
7657 property_decl->setGetterName(getter_sel);
7658 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
7659
7660 if (ivar_decl)
7661 property_decl->setPropertyIvarDecl (ivar_decl);
7662
7663 if (property_attributes & DW_APPLE_PROPERTY_readonly)
7664 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
7665 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
7666 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
7667 if (property_attributes & DW_APPLE_PROPERTY_assign)
7668 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
7669 if (property_attributes & DW_APPLE_PROPERTY_retain)
7670 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
7671 if (property_attributes & DW_APPLE_PROPERTY_copy)
7672 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
7673 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
7674 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
7675
7676 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
7677 {
7678 const bool isInstance = true;
7679 const bool isVariadic = false;
7680 const bool isSynthesized = false;
7681 const bool isImplicitlyDeclared = true;
7682 const bool isDefined = false;
7683 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
7684 const bool HasRelatedResultType = false;
7685
7686 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create (*clang_ast,
7687 clang::SourceLocation(),
7688 clang::SourceLocation(),
7689 getter_sel,
7690 GetQualType(property_clang_type_to_access),
7691 nullptr,
7692 class_interface_decl,
7693 isInstance,
7694 isVariadic,
7695 isSynthesized,
7696 isImplicitlyDeclared,
7697 isDefined,
7698 impControl,
7699 HasRelatedResultType);
7700
7701 if (getter && metadata)
7702 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
7703
7704 if (getter)
7705 {
7706 getter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(), llvm::ArrayRef<clang::SourceLocation>());
7707
7708 class_interface_decl->addDecl(getter);
7709 }
7710 }
7711
7712 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
7713 {
7714 clang::QualType result_type = clang_ast->VoidTy;
7715
7716 const bool isInstance = true;
7717 const bool isVariadic = false;
7718 const bool isSynthesized = false;
7719 const bool isImplicitlyDeclared = true;
7720 const bool isDefined = false;
7721 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
7722 const bool HasRelatedResultType = false;
7723
7724 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create (*clang_ast,
7725 clang::SourceLocation(),
7726 clang::SourceLocation(),
7727 setter_sel,
7728 result_type,
7729 nullptr,
7730 class_interface_decl,
7731 isInstance,
7732 isVariadic,
7733 isSynthesized,
7734 isImplicitlyDeclared,
7735 isDefined,
7736 impControl,
7737 HasRelatedResultType);
7738
7739 if (setter && metadata)
7740 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
7741
7742 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
7743
7744 params.push_back (clang::ParmVarDecl::Create (*clang_ast,
7745 setter,
7746 clang::SourceLocation(),
7747 clang::SourceLocation(),
7748 nullptr, // anonymous
7749 GetQualType(property_clang_type_to_access),
7750 nullptr,
7751 clang::SC_Auto,
7752 nullptr));
7753
7754 if (setter)
7755 {
7756 setter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
7757
7758 class_interface_decl->addDecl(setter);
7759 }
7760 }
7761
7762 return true;
7763 }
7764 }
7765 }
7766 return false;
7767}
7768
7769bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007770ClangASTContext::IsObjCClassTypeAndHasIVars (const CompilerType& type, bool check_superclass)
Greg Claytond8d4a572015-08-11 21:38:15 +00007771{
7772 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7773 if (class_interface_decl)
7774 return ObjCDeclHasIVars (class_interface_decl, check_superclass);
7775 return false;
7776}
7777
7778
7779clang::ObjCMethodDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007780ClangASTContext::AddMethodToObjCObjectType (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007781 const char *name, // the full symbol name as seen in the symbol table (void* type, "-[NString stringWithCString:]")
Greg Claytona1e5dc82015-08-11 22:53:00 +00007782 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007783 lldb::AccessType access,
7784 bool is_artificial)
7785{
7786 if (!type || !method_clang_type.IsValid())
7787 return nullptr;
7788
7789 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
7790
7791 if (class_interface_decl == nullptr)
7792 return nullptr;
7793 clang::ASTContext* ast = type.GetTypeSystem()->AsClangASTContext()->getASTContext();
7794
7795 const char *selector_start = ::strchr (name, ' ');
7796 if (selector_start == nullptr)
7797 return nullptr;
7798
7799 selector_start++;
7800 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
7801
7802 size_t len = 0;
7803 const char *start;
7804 //printf ("name = '%s'\n", name);
7805
7806 unsigned num_selectors_with_args = 0;
7807 for (start = selector_start;
7808 start && *start != '\0' && *start != ']';
7809 start += len)
7810 {
7811 len = ::strcspn(start, ":]");
7812 bool has_arg = (start[len] == ':');
7813 if (has_arg)
7814 ++num_selectors_with_args;
7815 selector_idents.push_back (&ast->Idents.get (llvm::StringRef (start, len)));
7816 if (has_arg)
7817 len += 1;
7818 }
7819
7820
7821 if (selector_idents.size() == 0)
7822 return nullptr;
7823
7824 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
7825 selector_idents.data());
7826
7827 clang::QualType method_qual_type (GetQualType(method_clang_type));
7828
7829 // Populate the method decl with parameter decls
7830 const clang::Type *method_type(method_qual_type.getTypePtr());
7831
7832 if (method_type == nullptr)
7833 return nullptr;
7834
7835 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(method_type));
7836
7837 if (!method_function_prototype)
7838 return nullptr;
7839
7840
7841 bool is_variadic = false;
7842 bool is_synthesized = false;
7843 bool is_defined = false;
7844 clang::ObjCMethodDecl::ImplementationControl imp_control = clang::ObjCMethodDecl::None;
7845
7846 const unsigned num_args = method_function_prototype->getNumParams();
7847
7848 if (num_args != num_selectors_with_args)
7849 return nullptr; // some debug information is corrupt. We are not going to deal with it.
7850
7851 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create (*ast,
7852 clang::SourceLocation(), // beginLoc,
7853 clang::SourceLocation(), // endLoc,
7854 method_selector,
7855 method_function_prototype->getReturnType(),
7856 nullptr, // TypeSourceInfo *ResultTInfo,
7857 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(GetQualType(type)),
7858 name[0] == '-',
7859 is_variadic,
7860 is_synthesized,
7861 true, // is_implicitly_declared; we force this to true because we don't have source locations
7862 is_defined,
7863 imp_control,
7864 false /*has_related_result_type*/);
7865
7866
7867 if (objc_method_decl == nullptr)
7868 return nullptr;
7869
7870 if (num_args > 0)
7871 {
7872 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
7873
7874 for (unsigned param_index = 0; param_index < num_args; ++param_index)
7875 {
7876 params.push_back (clang::ParmVarDecl::Create (*ast,
7877 objc_method_decl,
7878 clang::SourceLocation(),
7879 clang::SourceLocation(),
7880 nullptr, // anonymous
7881 method_function_prototype->getParamType(param_index),
7882 nullptr,
7883 clang::SC_Auto,
7884 nullptr));
7885 }
7886
7887 objc_method_decl->setMethodParams(*ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
7888 }
7889
7890 class_interface_decl->addDecl (objc_method_decl);
7891
7892#ifdef LLDB_CONFIGURATION_DEBUG
7893 VerifyDecl(objc_method_decl);
7894#endif
7895
7896 return objc_method_decl;
7897}
7898
7899bool
7900ClangASTContext::SetHasExternalStorage (void* type, bool has_extern)
7901{
7902 if (!type)
7903 return false;
7904
7905 clang::QualType qual_type (GetCanonicalQualType(type));
7906
7907 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7908 switch (type_class)
7909 {
7910 case clang::Type::Record:
7911 {
7912 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
7913 if (cxx_record_decl)
7914 {
7915 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
7916 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
7917 return true;
7918 }
7919 }
7920 break;
7921
7922 case clang::Type::Enum:
7923 {
7924 clang::EnumDecl *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getDecl();
7925 if (enum_decl)
7926 {
7927 enum_decl->setHasExternalLexicalStorage (has_extern);
7928 enum_decl->setHasExternalVisibleStorage (has_extern);
7929 return true;
7930 }
7931 }
7932 break;
7933
7934 case clang::Type::ObjCObject:
7935 case clang::Type::ObjCInterface:
7936 {
7937 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7938 assert (objc_class_type);
7939 if (objc_class_type)
7940 {
7941 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
7942
7943 if (class_interface_decl)
7944 {
7945 class_interface_decl->setHasExternalLexicalStorage (has_extern);
7946 class_interface_decl->setHasExternalVisibleStorage (has_extern);
7947 return true;
7948 }
7949 }
7950 }
7951 break;
7952
7953 case clang::Type::Typedef:
7954 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
7955
7956 case clang::Type::Elaborated:
7957 return SetHasExternalStorage (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
7958
7959 case clang::Type::Paren:
7960 return SetHasExternalStorage (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), has_extern);
7961
7962 default:
7963 break;
7964 }
7965 return false;
7966}
7967
7968
7969#pragma mark TagDecl
7970
7971bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007972ClangASTContext::StartTagDeclarationDefinition (const CompilerType &type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007973{
7974 if (type)
7975 {
7976
7977 clang::QualType qual_type (GetQualType(type));
7978 const clang::Type *t = qual_type.getTypePtr();
7979 if (t)
7980 {
7981 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(t);
7982 if (tag_type)
7983 {
7984 clang::TagDecl *tag_decl = tag_type->getDecl();
7985 if (tag_decl)
7986 {
7987 tag_decl->startDefinition();
7988 return true;
7989 }
7990 }
7991
7992 const clang::ObjCObjectType *object_type = llvm::dyn_cast<clang::ObjCObjectType>(t);
7993 if (object_type)
7994 {
7995 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
7996 if (interface_decl)
7997 {
7998 interface_decl->startDefinition();
7999 return true;
8000 }
8001 }
8002 }
8003 }
8004 return false;
8005}
8006
8007bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00008008ClangASTContext::CompleteTagDeclarationDefinition (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008009{
8010 if (type)
8011 {
8012 clang::QualType qual_type (GetQualType(type));
8013 if (qual_type.isNull())
8014 return false;
8015 clang::ASTContext* ast = type.GetTypeSystem()->AsClangASTContext()->getASTContext();
8016
8017 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8018
8019 if (cxx_record_decl)
8020 {
8021 cxx_record_decl->completeDefinition();
8022
8023 return true;
8024 }
8025
8026 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(qual_type.getTypePtr());
8027
8028 if (enutype)
8029 {
8030 clang::EnumDecl *enum_decl = enutype->getDecl();
8031
8032 if (enum_decl)
8033 {
8034 /// TODO This really needs to be fixed.
8035
8036 unsigned NumPositiveBits = 1;
8037 unsigned NumNegativeBits = 0;
8038
8039 clang::QualType promotion_qual_type;
8040 // If the enum integer type is less than an integer in bit width,
8041 // then we must promote it to an integer size.
8042 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
8043 {
8044 if (enum_decl->getIntegerType()->isSignedIntegerType())
8045 promotion_qual_type = ast->IntTy;
8046 else
8047 promotion_qual_type = ast->UnsignedIntTy;
8048 }
8049 else
8050 promotion_qual_type = enum_decl->getIntegerType();
8051
8052 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
8053 return true;
8054 }
8055 }
8056 }
8057 return false;
8058}
8059
Greg Claytond8d4a572015-08-11 21:38:15 +00008060bool
Greg Clayton8b4edba2015-08-14 20:02:05 +00008061ClangASTContext::AddEnumerationValueToEnumerationType (void* type,
8062 const CompilerType &enumerator_clang_type,
8063 const Declaration &decl,
8064 const char *name,
8065 int64_t enum_value,
8066 uint32_t enum_value_bit_size)
Greg Claytond8d4a572015-08-11 21:38:15 +00008067{
8068 if (type && enumerator_clang_type.IsValid() && name && name[0])
8069 {
8070 clang::QualType enum_qual_type (GetCanonicalQualType(type));
8071
8072 bool is_signed = false;
8073 enumerator_clang_type.IsIntegerType (is_signed);
8074 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8075 if (clang_type)
8076 {
8077 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8078
8079 if (enutype)
8080 {
8081 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8082 enum_llvm_apsint = enum_value;
8083 clang::EnumConstantDecl *enumerator_decl =
8084 clang::EnumConstantDecl::Create (*getASTContext(),
8085 enutype->getDecl(),
8086 clang::SourceLocation(),
8087 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
8088 GetQualType(enumerator_clang_type),
8089 nullptr,
8090 enum_llvm_apsint);
8091
8092 if (enumerator_decl)
8093 {
8094 enutype->getDecl()->addDecl(enumerator_decl);
8095
8096#ifdef LLDB_CONFIGURATION_DEBUG
8097 VerifyDecl(enumerator_decl);
8098#endif
8099
8100 return true;
8101 }
8102 }
8103 }
8104 }
8105 return false;
8106}
8107
Greg Claytona1e5dc82015-08-11 22:53:00 +00008108CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00008109ClangASTContext::GetEnumerationIntegerType (void* type)
8110{
8111 clang::QualType enum_qual_type (GetCanonicalQualType(type));
8112 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8113 if (clang_type)
8114 {
8115 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8116 if (enutype)
8117 {
8118 clang::EnumDecl *enum_decl = enutype->getDecl();
8119 if (enum_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008120 return CompilerType (getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008121 }
8122 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008123 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008124}
8125
Greg Claytona1e5dc82015-08-11 22:53:00 +00008126CompilerType
8127ClangASTContext::CreateMemberPointerType (const CompilerType& type, const CompilerType &pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008128{
8129 if (type && pointee_type.IsValid() && type.GetTypeSystem() == pointee_type.GetTypeSystem())
8130 {
8131 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
8132 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008133 return CompilerType();
8134 return CompilerType (ast->getASTContext(),
Greg Claytond8d4a572015-08-11 21:38:15 +00008135 ast->getASTContext()->getMemberPointerType (GetQualType(pointee_type),
8136 GetQualType(type).getTypePtr()));
8137 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008138 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008139}
8140
8141
8142size_t
8143ClangASTContext::ConvertStringToFloatValue (void* type, const char *s, uint8_t *dst, size_t dst_size)
8144{
8145 if (type)
8146 {
8147 clang::QualType qual_type (GetCanonicalQualType(type));
8148 uint32_t count = 0;
8149 bool is_complex = false;
8150 if (IsFloatingPointType (type, count, is_complex))
8151 {
8152 // TODO: handle complex and vector types
8153 if (count != 1)
8154 return false;
8155
8156 llvm::StringRef s_sref(s);
8157 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type), s_sref);
8158
8159 const uint64_t bit_size = getASTContext()->getTypeSize (qual_type);
8160 const uint64_t byte_size = bit_size / 8;
8161 if (dst_size >= byte_size)
8162 {
8163 if (bit_size == sizeof(float)*8)
8164 {
8165 float float32 = ap_float.convertToFloat();
8166 ::memcpy (dst, &float32, byte_size);
8167 return byte_size;
8168 }
8169 else if (bit_size >= 64)
8170 {
8171 llvm::APInt ap_int(ap_float.bitcastToAPInt());
8172 ::memcpy (dst, ap_int.getRawData(), byte_size);
8173 return byte_size;
8174 }
8175 }
8176 }
8177 }
8178 return 0;
8179}
8180
8181
8182
8183//----------------------------------------------------------------------
8184// Dumping types
8185//----------------------------------------------------------------------
8186#define DEPTH_INCREMENT 2
8187
8188void
8189ClangASTContext::DumpValue (void* type, ExecutionContext *exe_ctx,
8190 Stream *s,
8191 lldb::Format format,
8192 const lldb_private::DataExtractor &data,
8193 lldb::offset_t data_byte_offset,
8194 size_t data_byte_size,
8195 uint32_t bitfield_bit_size,
8196 uint32_t bitfield_bit_offset,
8197 bool show_types,
8198 bool show_summary,
8199 bool verbose,
8200 uint32_t depth)
8201{
8202 if (!type)
8203 return;
8204
8205 clang::QualType qual_type(GetQualType(type));
8206 switch (qual_type->getTypeClass())
8207 {
8208 case clang::Type::Record:
8209 if (GetCompleteType(type))
8210 {
8211 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8212 const clang::RecordDecl *record_decl = record_type->getDecl();
8213 assert(record_decl);
8214 uint32_t field_bit_offset = 0;
8215 uint32_t field_byte_offset = 0;
8216 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
8217 uint32_t child_idx = 0;
8218
8219 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8220 if (cxx_record_decl)
8221 {
8222 // We might have base classes to print out first
8223 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
8224 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
8225 base_class != base_class_end;
8226 ++base_class)
8227 {
8228 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
8229
8230 // Skip empty base classes
8231 if (verbose == false && ClangASTContext::RecordHasFields(base_class_decl) == false)
8232 continue;
8233
8234 if (base_class->isVirtual())
8235 field_bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
8236 else
8237 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
8238 field_byte_offset = field_bit_offset / 8;
8239 assert (field_bit_offset % 8 == 0);
8240 if (child_idx == 0)
8241 s->PutChar('{');
8242 else
8243 s->PutChar(',');
8244
8245 clang::QualType base_class_qual_type = base_class->getType();
8246 std::string base_class_type_name(base_class_qual_type.getAsString());
8247
8248 // Indent and print the base class type name
8249 s->Printf("\n%*s%s ", depth + DEPTH_INCREMENT, "", base_class_type_name.c_str());
8250
8251 clang::TypeInfo base_class_type_info = getASTContext()->getTypeInfo(base_class_qual_type);
8252
8253 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008254 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008255 base_clang_type.DumpValue (exe_ctx,
8256 s, // Stream to dump to
8257 base_clang_type.GetFormat(), // The format with which to display the member
8258 data, // Data buffer containing all bytes for this type
8259 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8260 base_class_type_info.Width / 8, // Size of this type in bytes
8261 0, // Bitfield bit size
8262 0, // Bitfield bit offset
8263 show_types, // Boolean indicating if we should show the variable types
8264 show_summary, // Boolean indicating if we should show a summary for the current type
8265 verbose, // Verbose output?
8266 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8267
8268 ++child_idx;
8269 }
8270 }
8271 uint32_t field_idx = 0;
8272 clang::RecordDecl::field_iterator field, field_end;
8273 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
8274 {
8275 // Print the starting squiggly bracket (if this is the
8276 // first member) or comma (for member 2 and beyond) for
8277 // the struct/union/class member.
8278 if (child_idx == 0)
8279 s->PutChar('{');
8280 else
8281 s->PutChar(',');
8282
8283 // Indent
8284 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8285
8286 clang::QualType field_type = field->getType();
8287 // Print the member type if requested
8288 // Figure out the type byte size (field_type_info.first) and
8289 // alignment (field_type_info.second) from the AST context.
8290 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(field_type);
8291 assert(field_idx < record_layout.getFieldCount());
8292 // Figure out the field offset within the current struct/union/class type
8293 field_bit_offset = record_layout.getFieldOffset (field_idx);
8294 field_byte_offset = field_bit_offset / 8;
8295 uint32_t field_bitfield_bit_size = 0;
8296 uint32_t field_bitfield_bit_offset = 0;
8297 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, field_bitfield_bit_size))
8298 field_bitfield_bit_offset = field_bit_offset % 8;
8299
8300 if (show_types)
8301 {
8302 std::string field_type_name(field_type.getAsString());
8303 if (field_bitfield_bit_size > 0)
8304 s->Printf("(%s:%u) ", field_type_name.c_str(), field_bitfield_bit_size);
8305 else
8306 s->Printf("(%s) ", field_type_name.c_str());
8307 }
8308 // Print the member name and equal sign
8309 s->Printf("%s = ", field->getNameAsString().c_str());
8310
8311
8312 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008313 CompilerType field_clang_type (getASTContext(), field_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008314 field_clang_type.DumpValue (exe_ctx,
8315 s, // Stream to dump to
8316 field_clang_type.GetFormat(), // The format with which to display the member
8317 data, // Data buffer containing all bytes for this type
8318 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8319 field_type_info.Width / 8, // Size of this type in bytes
8320 field_bitfield_bit_size, // Bitfield bit size
8321 field_bitfield_bit_offset, // Bitfield bit offset
8322 show_types, // Boolean indicating if we should show the variable types
8323 show_summary, // Boolean indicating if we should show a summary for the current type
8324 verbose, // Verbose output?
8325 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8326 }
8327
8328 // Indent the trailing squiggly bracket
8329 if (child_idx > 0)
8330 s->Printf("\n%*s}", depth, "");
8331 }
8332 return;
8333
8334 case clang::Type::Enum:
8335 if (GetCompleteType(type))
8336 {
8337 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8338 const clang::EnumDecl *enum_decl = enutype->getDecl();
8339 assert(enum_decl);
8340 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8341 lldb::offset_t offset = data_byte_offset;
8342 const int64_t enum_value = data.GetMaxU64Bitfield(&offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
8343 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8344 {
8345 if (enum_pos->getInitVal() == enum_value)
8346 {
8347 s->Printf("%s", enum_pos->getNameAsString().c_str());
8348 return;
8349 }
8350 }
8351 // If we have gotten here we didn't get find the enumerator in the
8352 // enum decl, so just print the integer.
8353 s->Printf("%" PRIi64, enum_value);
8354 }
8355 return;
8356
8357 case clang::Type::ConstantArray:
8358 {
8359 const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
8360 bool is_array_of_characters = false;
8361 clang::QualType element_qual_type = array->getElementType();
8362
8363 const clang::Type *canonical_type = element_qual_type->getCanonicalTypeInternal().getTypePtr();
8364 if (canonical_type)
8365 is_array_of_characters = canonical_type->isCharType();
8366
8367 const uint64_t element_count = array->getSize().getLimitedValue();
8368
8369 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(element_qual_type);
8370
8371 uint32_t element_idx = 0;
8372 uint32_t element_offset = 0;
8373 uint64_t element_byte_size = field_type_info.Width / 8;
8374 uint32_t element_stride = element_byte_size;
8375
8376 if (is_array_of_characters)
8377 {
8378 s->PutChar('"');
8379 data.Dump(s, data_byte_offset, lldb::eFormatChar, element_byte_size, element_count, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
8380 s->PutChar('"');
8381 return;
8382 }
8383 else
8384 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00008385 CompilerType element_clang_type(getASTContext(), element_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008386 lldb::Format element_format = element_clang_type.GetFormat();
8387
8388 for (element_idx = 0; element_idx < element_count; ++element_idx)
8389 {
8390 // Print the starting squiggly bracket (if this is the
8391 // first member) or comman (for member 2 and beyong) for
8392 // the struct/union/class member.
8393 if (element_idx == 0)
8394 s->PutChar('{');
8395 else
8396 s->PutChar(',');
8397
8398 // Indent and print the index
8399 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
8400
8401 // Figure out the field offset within the current struct/union/class type
8402 element_offset = element_idx * element_stride;
8403
8404 // Dump the value of the member
8405 element_clang_type.DumpValue (exe_ctx,
8406 s, // Stream to dump to
8407 element_format, // The format with which to display the element
8408 data, // Data buffer containing all bytes for this type
8409 data_byte_offset + element_offset,// Offset into "data" where to grab value from
8410 element_byte_size, // Size of this type in bytes
8411 0, // Bitfield bit size
8412 0, // Bitfield bit offset
8413 show_types, // Boolean indicating if we should show the variable types
8414 show_summary, // Boolean indicating if we should show a summary for the current type
8415 verbose, // Verbose output?
8416 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8417 }
8418
8419 // Indent the trailing squiggly bracket
8420 if (element_idx > 0)
8421 s->Printf("\n%*s}", depth, "");
8422 }
8423 }
8424 return;
8425
8426 case clang::Type::Typedef:
8427 {
8428 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
8429
Greg Claytona1e5dc82015-08-11 22:53:00 +00008430 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008431 lldb::Format typedef_format = typedef_clang_type.GetFormat();
8432 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
8433 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
8434
8435 return typedef_clang_type.DumpValue (exe_ctx,
8436 s, // Stream to dump to
8437 typedef_format, // The format with which to display the element
8438 data, // Data buffer containing all bytes for this type
8439 data_byte_offset, // Offset into "data" where to grab value from
8440 typedef_byte_size, // Size of this type in bytes
8441 bitfield_bit_size, // Bitfield bit size
8442 bitfield_bit_offset,// Bitfield bit offset
8443 show_types, // Boolean indicating if we should show the variable types
8444 show_summary, // Boolean indicating if we should show a summary for the current type
8445 verbose, // Verbose output?
8446 depth); // Scope depth for any types that have children
8447 }
8448 break;
8449
8450 case clang::Type::Elaborated:
8451 {
8452 clang::QualType elaborated_qual_type = llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008453 CompilerType elaborated_clang_type (getASTContext(), elaborated_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008454 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
8455 clang::TypeInfo elaborated_type_info = getASTContext()->getTypeInfo(elaborated_qual_type);
8456 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
8457
8458 return elaborated_clang_type.DumpValue (exe_ctx,
8459 s, // Stream to dump to
8460 elaborated_format, // The format with which to display the element
8461 data, // Data buffer containing all bytes for this type
8462 data_byte_offset, // Offset into "data" where to grab value from
8463 elaborated_byte_size, // Size of this type in bytes
8464 bitfield_bit_size, // Bitfield bit size
8465 bitfield_bit_offset,// Bitfield bit offset
8466 show_types, // Boolean indicating if we should show the variable types
8467 show_summary, // Boolean indicating if we should show a summary for the current type
8468 verbose, // Verbose output?
8469 depth); // Scope depth for any types that have children
8470 }
8471 break;
8472
8473 case clang::Type::Paren:
8474 {
8475 clang::QualType desugar_qual_type = llvm::cast<clang::ParenType>(qual_type)->desugar();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008476 CompilerType desugar_clang_type (getASTContext(), desugar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008477
8478 lldb::Format desugar_format = desugar_clang_type.GetFormat();
8479 clang::TypeInfo desugar_type_info = getASTContext()->getTypeInfo(desugar_qual_type);
8480 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
8481
8482 return desugar_clang_type.DumpValue (exe_ctx,
8483 s, // Stream to dump to
8484 desugar_format, // The format with which to display the element
8485 data, // Data buffer containing all bytes for this type
8486 data_byte_offset, // Offset into "data" where to grab value from
8487 desugar_byte_size, // Size of this type in bytes
8488 bitfield_bit_size, // Bitfield bit size
8489 bitfield_bit_offset,// Bitfield bit offset
8490 show_types, // Boolean indicating if we should show the variable types
8491 show_summary, // Boolean indicating if we should show a summary for the current type
8492 verbose, // Verbose output?
8493 depth); // Scope depth for any types that have children
8494 }
8495 break;
8496
8497 default:
8498 // We are down to a scalar type that we just need to display.
8499 data.Dump(s,
8500 data_byte_offset,
8501 format,
8502 data_byte_size,
8503 1,
8504 UINT32_MAX,
8505 LLDB_INVALID_ADDRESS,
8506 bitfield_bit_size,
8507 bitfield_bit_offset);
8508
8509 if (show_summary)
8510 DumpSummary (type, exe_ctx, s, data, data_byte_offset, data_byte_size);
8511 break;
8512 }
8513}
8514
8515
8516
8517
8518bool
8519ClangASTContext::DumpTypeValue (void* type, Stream *s,
8520 lldb::Format format,
8521 const lldb_private::DataExtractor &data,
8522 lldb::offset_t byte_offset,
8523 size_t byte_size,
8524 uint32_t bitfield_bit_size,
8525 uint32_t bitfield_bit_offset,
8526 ExecutionContextScope *exe_scope)
8527{
8528 if (!type)
8529 return false;
8530 if (IsAggregateType(type))
8531 {
8532 return false;
8533 }
8534 else
8535 {
8536 clang::QualType qual_type(GetQualType(type));
8537
8538 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8539 switch (type_class)
8540 {
8541 case clang::Type::Typedef:
8542 {
8543 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008544 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008545 if (format == eFormatDefault)
8546 format = typedef_clang_type.GetFormat();
8547 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
8548 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
8549
8550 return typedef_clang_type.DumpTypeValue (s,
8551 format, // The format with which to display the element
8552 data, // Data buffer containing all bytes for this type
8553 byte_offset, // Offset into "data" where to grab value from
8554 typedef_byte_size, // Size of this type in bytes
8555 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't treat as a bitfield
8556 bitfield_bit_offset, // Offset in bits of a bitfield value if bitfield_bit_size != 0
8557 exe_scope);
8558 }
8559 break;
8560
8561 case clang::Type::Enum:
8562 // If our format is enum or default, show the enumeration value as
8563 // its enumeration string value, else just display it as requested.
8564 if ((format == eFormatEnum || format == eFormatDefault) && GetCompleteType(type))
8565 {
8566 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8567 const clang::EnumDecl *enum_decl = enutype->getDecl();
8568 assert(enum_decl);
8569 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8570 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
8571 lldb::offset_t offset = byte_offset;
8572 if (is_signed)
8573 {
8574 const int64_t enum_svalue = data.GetMaxS64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
8575 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8576 {
8577 if (enum_pos->getInitVal().getSExtValue() == enum_svalue)
8578 {
8579 s->PutCString (enum_pos->getNameAsString().c_str());
8580 return true;
8581 }
8582 }
8583 // If we have gotten here we didn't get find the enumerator in the
8584 // enum decl, so just print the integer.
8585 s->Printf("%" PRIi64, enum_svalue);
8586 }
8587 else
8588 {
8589 const uint64_t enum_uvalue = data.GetMaxU64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
8590 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8591 {
8592 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue)
8593 {
8594 s->PutCString (enum_pos->getNameAsString().c_str());
8595 return true;
8596 }
8597 }
8598 // If we have gotten here we didn't get find the enumerator in the
8599 // enum decl, so just print the integer.
8600 s->Printf("%" PRIu64, enum_uvalue);
8601 }
8602 return true;
8603 }
8604 // format was not enum, just fall through and dump the value as requested....
8605
8606 default:
8607 // We are down to a scalar type that we just need to display.
8608 {
8609 uint32_t item_count = 1;
8610 // A few formats, we might need to modify our size and count for depending
8611 // on how we are trying to display the value...
8612 switch (format)
8613 {
8614 default:
8615 case eFormatBoolean:
8616 case eFormatBinary:
8617 case eFormatComplex:
8618 case eFormatCString: // NULL terminated C strings
8619 case eFormatDecimal:
8620 case eFormatEnum:
8621 case eFormatHex:
8622 case eFormatHexUppercase:
8623 case eFormatFloat:
8624 case eFormatOctal:
8625 case eFormatOSType:
8626 case eFormatUnsigned:
8627 case eFormatPointer:
8628 case eFormatVectorOfChar:
8629 case eFormatVectorOfSInt8:
8630 case eFormatVectorOfUInt8:
8631 case eFormatVectorOfSInt16:
8632 case eFormatVectorOfUInt16:
8633 case eFormatVectorOfSInt32:
8634 case eFormatVectorOfUInt32:
8635 case eFormatVectorOfSInt64:
8636 case eFormatVectorOfUInt64:
8637 case eFormatVectorOfFloat32:
8638 case eFormatVectorOfFloat64:
8639 case eFormatVectorOfUInt128:
8640 break;
8641
8642 case eFormatChar:
8643 case eFormatCharPrintable:
8644 case eFormatCharArray:
8645 case eFormatBytes:
8646 case eFormatBytesWithASCII:
8647 item_count = byte_size;
8648 byte_size = 1;
8649 break;
8650
8651 case eFormatUnicode16:
8652 item_count = byte_size / 2;
8653 byte_size = 2;
8654 break;
8655
8656 case eFormatUnicode32:
8657 item_count = byte_size / 4;
8658 byte_size = 4;
8659 break;
8660 }
8661 return data.Dump (s,
8662 byte_offset,
8663 format,
8664 byte_size,
8665 item_count,
8666 UINT32_MAX,
8667 LLDB_INVALID_ADDRESS,
8668 bitfield_bit_size,
8669 bitfield_bit_offset,
8670 exe_scope);
8671 }
8672 break;
8673 }
8674 }
8675 return 0;
8676}
8677
8678
8679
8680void
8681ClangASTContext::DumpSummary (void* type, ExecutionContext *exe_ctx,
8682 Stream *s,
8683 const lldb_private::DataExtractor &data,
8684 lldb::offset_t data_byte_offset,
8685 size_t data_byte_size)
8686{
8687 uint32_t length = 0;
8688 if (IsCStringType (type, length))
8689 {
8690 if (exe_ctx)
8691 {
8692 Process *process = exe_ctx->GetProcessPtr();
8693 if (process)
8694 {
8695 lldb::offset_t offset = data_byte_offset;
8696 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
8697 std::vector<uint8_t> buf;
8698 if (length > 0)
8699 buf.resize (length);
8700 else
8701 buf.resize (256);
8702
8703 lldb_private::DataExtractor cstr_data(&buf.front(), buf.size(), process->GetByteOrder(), 4);
8704 buf.back() = '\0';
8705 size_t bytes_read;
8706 size_t total_cstr_len = 0;
8707 Error error;
8708 while ((bytes_read = process->ReadMemory (pointer_address, &buf.front(), buf.size(), error)) > 0)
8709 {
8710 const size_t len = strlen((const char *)&buf.front());
8711 if (len == 0)
8712 break;
8713 if (total_cstr_len == 0)
8714 s->PutCString (" \"");
8715 cstr_data.Dump(s, 0, lldb::eFormatChar, 1, len, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
8716 total_cstr_len += len;
8717 if (len < buf.size())
8718 break;
8719 pointer_address += total_cstr_len;
8720 }
8721 if (total_cstr_len > 0)
8722 s->PutChar ('"');
8723 }
8724 }
8725 }
8726}
8727
8728void
8729ClangASTContext::DumpTypeDescription (void* type)
8730{
8731 StreamFile s (stdout, false);
8732 DumpTypeDescription (&s);
8733 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), type);
8734 if (metadata)
8735 {
8736 metadata->Dump (&s);
8737 }
8738}
8739
8740void
8741ClangASTContext::DumpTypeDescription (void* type, Stream *s)
8742{
8743 if (type)
8744 {
8745 clang::QualType qual_type(GetQualType(type));
8746
8747 llvm::SmallVector<char, 1024> buf;
8748 llvm::raw_svector_ostream llvm_ostrm (buf);
8749
8750 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8751 switch (type_class)
8752 {
8753 case clang::Type::ObjCObject:
8754 case clang::Type::ObjCInterface:
8755 {
8756 GetCompleteType(type);
8757
8758 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8759 assert (objc_class_type);
8760 if (objc_class_type)
8761 {
8762 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
8763 if (class_interface_decl)
8764 {
8765 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
8766 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
8767 }
8768 }
8769 }
8770 break;
8771
8772 case clang::Type::Typedef:
8773 {
8774 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
8775 if (typedef_type)
8776 {
8777 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
8778 std::string clang_typedef_name (typedef_decl->getQualifiedNameAsString());
8779 if (!clang_typedef_name.empty())
8780 {
8781 s->PutCString ("typedef ");
8782 s->PutCString (clang_typedef_name.c_str());
8783 }
8784 }
8785 }
8786 break;
8787
8788 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00008789 CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00008790 return;
8791
8792 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00008793 CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00008794 return;
8795
8796 case clang::Type::Record:
8797 {
8798 GetCompleteType(type);
8799
8800 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8801 const clang::RecordDecl *record_decl = record_type->getDecl();
8802 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8803
8804 if (cxx_record_decl)
8805 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
8806 else
8807 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
8808 }
8809 break;
8810
8811 default:
8812 {
8813 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
8814 if (tag_type)
8815 {
8816 clang::TagDecl *tag_decl = tag_type->getDecl();
8817 if (tag_decl)
8818 tag_decl->print(llvm_ostrm, 0);
8819 }
8820 else
8821 {
8822 std::string clang_type_name(qual_type.getAsString());
8823 if (!clang_type_name.empty())
8824 s->PutCString (clang_type_name.c_str());
8825 }
8826 }
8827 }
8828
Greg Claytond8d4a572015-08-11 21:38:15 +00008829 if (buf.size() > 0)
8830 {
8831 s->Write (buf.data(), buf.size());
8832 }
8833 }
8834}
Greg Clayton8b4edba2015-08-14 20:02:05 +00008835
8836// DWARF parsing functions
8837#pragma mark DWARF Parsing
8838
8839#include "lldb/Core/Module.h"
8840#include "lldb/Symbol/CompileUnit.h"
8841#include "lldb/Symbol/Function.h"
8842#include "lldb/Symbol/ObjectFile.h"
8843#include "lldb/Symbol/TypeList.h"
8844
Oleksiy Vyalov52ae0232015-08-14 21:16:00 +00008845#include "Plugins/SymbolFile/DWARF/DWARFCompileUnit.h"
8846#include "Plugins/SymbolFile/DWARF/DWARFDebugInfo.h"
8847#include "Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.h"
8848#include "Plugins/SymbolFile/DWARF/DWARFDeclContext.h"
8849#include "Plugins/SymbolFile/DWARF/DWARFDefines.h"
8850#include "Plugins/SymbolFile/DWARF/DWARFDIECollection.h"
8851#include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
8852#include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h"
8853#include "Plugins/SymbolFile/DWARF/UniqueDWARFASTType.h"
Greg Clayton8b4edba2015-08-14 20:02:05 +00008854
8855
8856class ClangASTContext::DelayedAddObjCClassProperty
8857{
8858public:
8859 DelayedAddObjCClassProperty(const CompilerType &class_opaque_type,
8860 const char *property_name,
8861 const CompilerType &property_opaque_type, // The property type is only required if you don't have an ivar decl
8862 clang::ObjCIvarDecl *ivar_decl,
8863 const char *property_setter_name,
8864 const char *property_getter_name,
8865 uint32_t property_attributes,
8866 const ClangASTMetadata *metadata) :
8867 m_class_opaque_type (class_opaque_type),
8868 m_property_name (property_name),
8869 m_property_opaque_type (property_opaque_type),
8870 m_ivar_decl (ivar_decl),
8871 m_property_setter_name (property_setter_name),
8872 m_property_getter_name (property_getter_name),
8873 m_property_attributes (property_attributes)
8874 {
8875 if (metadata != NULL)
8876 {
8877 m_metadata_ap.reset(new ClangASTMetadata());
8878 *m_metadata_ap = *metadata;
8879 }
8880 }
8881
8882 DelayedAddObjCClassProperty (const DelayedAddObjCClassProperty &rhs)
8883 {
8884 *this = rhs;
8885 }
8886
8887 DelayedAddObjCClassProperty& operator= (const DelayedAddObjCClassProperty &rhs)
8888 {
8889 m_class_opaque_type = rhs.m_class_opaque_type;
8890 m_property_name = rhs.m_property_name;
8891 m_property_opaque_type = rhs.m_property_opaque_type;
8892 m_ivar_decl = rhs.m_ivar_decl;
8893 m_property_setter_name = rhs.m_property_setter_name;
8894 m_property_getter_name = rhs.m_property_getter_name;
8895 m_property_attributes = rhs.m_property_attributes;
8896
8897 if (rhs.m_metadata_ap.get())
8898 {
8899 m_metadata_ap.reset (new ClangASTMetadata());
8900 *m_metadata_ap = *rhs.m_metadata_ap;
8901 }
8902 return *this;
8903 }
8904
8905 bool
8906 Finalize()
8907 {
8908 ClangASTContext* ast = m_class_opaque_type.GetTypeSystem()->AsClangASTContext();
8909 assert(ast);
8910 return ast->AddObjCClassProperty (m_class_opaque_type,
8911 m_property_name,
8912 m_property_opaque_type,
8913 m_ivar_decl,
8914 m_property_setter_name,
8915 m_property_getter_name,
8916 m_property_attributes,
8917 m_metadata_ap.get());
8918 }
8919
8920private:
8921 CompilerType m_class_opaque_type;
8922 const char *m_property_name;
8923 CompilerType m_property_opaque_type;
8924 clang::ObjCIvarDecl *m_ivar_decl;
8925 const char *m_property_setter_name;
8926 const char *m_property_getter_name;
8927 uint32_t m_property_attributes;
8928 std::unique_ptr<ClangASTMetadata> m_metadata_ap;
8929};
8930
8931bool
8932ClangASTContext::ParseTemplateDIE (SymbolFileDWARF *dwarf,
8933 DWARFCompileUnit* dwarf_cu,
8934 const DWARFDebugInfoEntry *die,
8935 ClangASTContext::TemplateParameterInfos &template_param_infos)
8936{
8937 const dw_tag_t tag = die->Tag();
8938
8939 switch (tag)
8940 {
8941 case DW_TAG_template_type_parameter:
8942 case DW_TAG_template_value_parameter:
8943 {
8944 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
8945
8946 DWARFDebugInfoEntry::Attributes attributes;
8947 const size_t num_attributes = die->GetAttributes (dwarf,
8948 dwarf_cu,
8949 fixed_form_sizes,
8950 attributes);
8951 const char *name = NULL;
8952 Type *lldb_type = NULL;
8953 CompilerType clang_type;
8954 uint64_t uval64 = 0;
8955 bool uval64_valid = false;
8956 if (num_attributes > 0)
8957 {
8958 DWARFFormValue form_value;
8959 for (size_t i=0; i<num_attributes; ++i)
8960 {
8961 const dw_attr_t attr = attributes.AttributeAtIndex(i);
8962
8963 switch (attr)
8964 {
8965 case DW_AT_name:
8966 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
8967 name = form_value.AsCString(&dwarf->get_debug_str_data());
8968 break;
8969
8970 case DW_AT_type:
8971 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
8972 {
8973 const dw_offset_t type_die_offset = form_value.Reference();
8974 lldb_type = dwarf->ResolveTypeUID(type_die_offset);
8975 if (lldb_type)
8976 clang_type = lldb_type->GetClangForwardType();
8977 }
8978 break;
8979
8980 case DW_AT_const_value:
8981 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
8982 {
8983 uval64_valid = true;
8984 uval64 = form_value.Unsigned();
8985 }
8986 break;
8987 default:
8988 break;
8989 }
8990 }
8991
8992 clang::ASTContext *ast = getASTContext();
8993 if (!clang_type)
8994 clang_type = GetBasicType(eBasicTypeVoid);
8995
8996 if (clang_type)
8997 {
8998 bool is_signed = false;
8999 if (name && name[0])
9000 template_param_infos.names.push_back(name);
9001 else
9002 template_param_infos.names.push_back(NULL);
9003
9004 if (tag == DW_TAG_template_value_parameter &&
9005 lldb_type != NULL &&
9006 clang_type.IsIntegerType (is_signed) &&
9007 uval64_valid)
9008 {
9009 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
9010 template_param_infos.args.push_back (clang::TemplateArgument (*ast,
9011 llvm::APSInt(apint),
9012 ClangASTContext::GetQualType(clang_type)));
9013 }
9014 else
9015 {
9016 template_param_infos.args.push_back (clang::TemplateArgument (ClangASTContext::GetQualType(clang_type)));
9017 }
9018 }
9019 else
9020 {
9021 return false;
9022 }
9023
9024 }
9025 }
9026 return true;
9027
9028 default:
9029 break;
9030 }
9031 return false;
9032}
9033
9034bool
9035ClangASTContext::ParseTemplateParameterInfos (SymbolFileDWARF *dwarf,
9036 DWARFCompileUnit* dwarf_cu,
9037 const DWARFDebugInfoEntry *parent_die,
9038 ClangASTContext::TemplateParameterInfos &template_param_infos)
9039{
9040
9041 if (parent_die == NULL)
9042 return false;
9043
9044 Args template_parameter_names;
9045 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild();
9046 die != NULL;
9047 die = die->GetSibling())
9048 {
9049 const dw_tag_t tag = die->Tag();
9050
9051 switch (tag)
9052 {
9053 case DW_TAG_template_type_parameter:
9054 case DW_TAG_template_value_parameter:
9055 ParseTemplateDIE (dwarf, dwarf_cu, die, template_param_infos);
9056 break;
9057
9058 default:
9059 break;
9060 }
9061 }
9062 if (template_param_infos.args.empty())
9063 return false;
9064 return template_param_infos.args.size() == template_param_infos.names.size();
9065}
9066
9067clang::ClassTemplateDecl *
9068ClangASTContext::ParseClassTemplateDecl (SymbolFileDWARF *dwarf,
9069 clang::DeclContext *decl_ctx,
9070 lldb::AccessType access_type,
9071 const char *parent_name,
9072 int tag_decl_kind,
9073 const ClangASTContext::TemplateParameterInfos &template_param_infos)
9074{
9075 if (template_param_infos.IsValid())
9076 {
9077 std::string template_basename(parent_name);
9078 template_basename.erase (template_basename.find('<'));
9079
9080 return CreateClassTemplateDecl (decl_ctx,
9081 access_type,
9082 template_basename.c_str(),
9083 tag_decl_kind,
9084 template_param_infos);
9085 }
9086 return NULL;
9087}
9088
9089bool
Greg Clayton6dc8d582015-08-18 22:32:36 +00009090ClangASTContext::CompleteTypeFromDWARF (SymbolFileDWARF *dwarf,
9091 DWARFCompileUnit *dwarf_cu,
9092 const DWARFDebugInfoEntry* die,
9093 lldb_private::Type *type,
9094 CompilerType &clang_type)
Greg Clayton8b4edba2015-08-14 20:02:05 +00009095{
9096 // Disable external storage for this type so we don't get anymore
9097 // clang::ExternalASTSource queries for this type.
9098 SetHasExternalStorage (clang_type.GetOpaqueQualType(), false);
9099
9100 if (dwarf == nullptr || dwarf_cu == nullptr || die == nullptr)
9101 return false;
9102
9103 const dw_tag_t tag = die->Tag();
9104
9105 Log *log = nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
9106 if (log)
9107 dwarf->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log,
9108 "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
9109 dwarf->MakeUserID(die->GetOffset()),
9110 DW_TAG_value_to_name(tag),
9111 type->GetName().AsCString());
9112 assert (clang_type);
9113 DWARFDebugInfoEntry::Attributes attributes;
9114
9115 switch (tag)
9116 {
9117 case DW_TAG_structure_type:
9118 case DW_TAG_union_type:
9119 case DW_TAG_class_type:
9120 {
9121 LayoutInfo layout_info;
9122
9123 {
9124 if (die->HasChildren())
9125 {
9126 LanguageType class_language = eLanguageTypeUnknown;
9127 if (ClangASTContext::IsObjCObjectOrInterfaceType(clang_type))
9128 {
9129 class_language = eLanguageTypeObjC;
9130 // For objective C we don't start the definition when
9131 // the class is created.
9132 ClangASTContext::StartTagDeclarationDefinition (clang_type);
9133 }
9134
9135 int tag_decl_kind = -1;
9136 AccessType default_accessibility = eAccessNone;
9137 if (tag == DW_TAG_structure_type)
9138 {
9139 tag_decl_kind = clang::TTK_Struct;
9140 default_accessibility = eAccessPublic;
9141 }
9142 else if (tag == DW_TAG_union_type)
9143 {
9144 tag_decl_kind = clang::TTK_Union;
9145 default_accessibility = eAccessPublic;
9146 }
9147 else if (tag == DW_TAG_class_type)
9148 {
9149 tag_decl_kind = clang::TTK_Class;
9150 default_accessibility = eAccessPrivate;
9151 }
9152
9153 SymbolContext sc(dwarf->GetCompUnitForDWARFCompUnit(dwarf_cu));
9154 std::vector<clang::CXXBaseSpecifier *> base_classes;
9155 std::vector<int> member_accessibilities;
9156 bool is_a_class = false;
9157 // Parse members and base classes first
9158 DWARFDIECollection member_function_dies;
9159
9160 DelayedPropertyList delayed_properties;
9161 ParseChildMembers (sc,
9162 dwarf,
9163 dwarf_cu,
9164 die,
9165 clang_type,
9166 class_language,
9167 base_classes,
9168 member_accessibilities,
9169 member_function_dies,
9170 delayed_properties,
9171 default_accessibility,
9172 is_a_class,
9173 layout_info);
9174
9175 // Now parse any methods if there were any...
9176 size_t num_functions = member_function_dies.Size();
9177 if (num_functions > 0)
9178 {
9179 for (size_t i=0; i<num_functions; ++i)
9180 {
9181 dwarf->ResolveType(dwarf_cu, member_function_dies.GetDIEPtrAtIndex(i));
9182 }
9183 }
9184
9185 if (class_language == eLanguageTypeObjC)
9186 {
9187 ConstString class_name (clang_type.GetTypeName());
9188 if (class_name)
9189 {
9190 DIEArray method_die_offsets;
9191 dwarf->GetObjCMethodDIEOffsets(class_name, method_die_offsets);
9192
9193 if (!method_die_offsets.empty())
9194 {
9195 DWARFDebugInfo* debug_info = dwarf->DebugInfo();
9196
9197 DWARFCompileUnit* method_cu = NULL;
9198 const size_t num_matches = method_die_offsets.size();
9199 for (size_t i=0; i<num_matches; ++i)
9200 {
9201 const dw_offset_t die_offset = method_die_offsets[i];
9202 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
9203
9204 if (method_die)
9205 dwarf->ResolveType (method_cu, method_die);
9206 }
9207 }
9208
9209 for (DelayedPropertyList::iterator pi = delayed_properties.begin(), pe = delayed_properties.end();
9210 pi != pe;
9211 ++pi)
9212 pi->Finalize();
9213 }
9214 }
9215
9216 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
9217 // need to tell the clang type it is actually a class.
9218 if (class_language != eLanguageTypeObjC)
9219 {
9220 if (is_a_class && tag_decl_kind != clang::TTK_Class)
9221 SetTagTypeKind (ClangASTContext::GetQualType(clang_type), clang::TTK_Class);
9222 }
9223
9224 // Since DW_TAG_structure_type gets used for both classes
9225 // and structures, we may need to set any DW_TAG_member
9226 // fields to have a "private" access if none was specified.
9227 // When we parsed the child members we tracked that actual
9228 // accessibility value for each DW_TAG_member in the
9229 // "member_accessibilities" array. If the value for the
9230 // member is zero, then it was set to the "default_accessibility"
9231 // which for structs was "public". Below we correct this
9232 // by setting any fields to "private" that weren't correctly
9233 // set.
9234 if (is_a_class && !member_accessibilities.empty())
9235 {
9236 // This is a class and all members that didn't have
9237 // their access specified are private.
9238 SetDefaultAccessForRecordFields (GetAsRecordDecl(clang_type),
9239 eAccessPrivate,
9240 &member_accessibilities.front(),
9241 member_accessibilities.size());
9242 }
9243
9244 if (!base_classes.empty())
9245 {
9246 // Make sure all base classes refer to complete types and not
9247 // forward declarations. If we don't do this, clang will crash
9248 // with an assertion in the call to clang_type.SetBaseClassesForClassType()
9249 bool base_class_error = false;
9250 for (auto &base_class : base_classes)
9251 {
9252 clang::TypeSourceInfo *type_source_info = base_class->getTypeSourceInfo();
9253 if (type_source_info)
9254 {
9255 CompilerType base_class_type (this, type_source_info->getType().getAsOpaquePtr());
9256 if (base_class_type.GetCompleteType() == false)
9257 {
9258 if (!base_class_error)
9259 {
9260 dwarf->GetObjectFile()->GetModule()->ReportError ("DWARF DIE at 0x%8.8x for class '%s' has a base class '%s' that is a forward declaration, not a complete definition.\nPlease file a bug against the compiler and include the preprocessed output for %s",
9261 die->GetOffset(),
9262 die->GetName(dwarf, dwarf_cu),
9263 base_class_type.GetTypeName().GetCString(),
9264 sc.comp_unit ? sc.comp_unit->GetPath().c_str() : "the source file");
9265 }
9266 // We have no choice other than to pretend that the base class
9267 // is complete. If we don't do this, clang will crash when we
9268 // call setBases() inside of "clang_type.SetBaseClassesForClassType()"
9269 // below. Since we provide layout assistance, all ivars in this
9270 // class and other classes will be fine, this is the best we can do
9271 // short of crashing.
9272
9273 ClangASTContext::StartTagDeclarationDefinition (base_class_type);
9274 ClangASTContext::CompleteTagDeclarationDefinition (base_class_type);
9275 }
9276 }
9277 }
9278 SetBaseClassesForClassType (clang_type.GetOpaqueQualType(),
9279 &base_classes.front(),
9280 base_classes.size());
9281
9282 // Clang will copy each CXXBaseSpecifier in "base_classes"
9283 // so we have to free them all.
9284 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
9285 base_classes.size());
9286 }
9287 }
9288 }
9289
9290 ClangASTContext::BuildIndirectFields (clang_type);
9291 ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
9292
9293 if (!layout_info.field_offsets.empty() ||
9294 !layout_info.base_offsets.empty() ||
9295 !layout_info.vbase_offsets.empty() )
9296 {
9297 if (type)
9298 layout_info.bit_size = type->GetByteSize() * 8;
9299 if (layout_info.bit_size == 0)
9300 layout_info.bit_size = die->GetAttributeValueAsUnsigned(dwarf, dwarf_cu, DW_AT_byte_size, 0) * 8;
9301
9302 clang::CXXRecordDecl *record_decl = GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
9303 if (record_decl)
9304 {
9305 if (log)
9306 {
9307 ModuleSP module_sp = dwarf->GetObjectFile()->GetModule();
9308
9309 if (module_sp)
9310 {
9311 module_sp->LogMessage (log,
Greg Clayton6dc8d582015-08-18 22:32:36 +00009312 "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) caching layout info for record_decl = %p, bit_size = %" PRIu64 ", alignment = %" PRIu64 ", field_offsets[%u], base_offsets[%u], vbase_offsets[%u])",
Greg Clayton8b4edba2015-08-14 20:02:05 +00009313 static_cast<void*>(clang_type.GetOpaqueQualType()),
9314 static_cast<void*>(record_decl),
9315 layout_info.bit_size,
9316 layout_info.alignment,
9317 static_cast<uint32_t>(layout_info.field_offsets.size()),
9318 static_cast<uint32_t>(layout_info.base_offsets.size()),
9319 static_cast<uint32_t>(layout_info.vbase_offsets.size()));
9320
9321 uint32_t idx;
9322 {
9323 llvm::DenseMap<const clang::FieldDecl *, uint64_t>::const_iterator pos,
9324 end = layout_info.field_offsets.end();
9325 for (idx = 0, pos = layout_info.field_offsets.begin(); pos != end; ++pos, ++idx)
9326 {
9327 module_sp->LogMessage(log,
Greg Clayton6dc8d582015-08-18 22:32:36 +00009328 "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) field[%u] = { bit_offset=%u, name='%s' }",
Greg Clayton8b4edba2015-08-14 20:02:05 +00009329 static_cast<void *>(clang_type.GetOpaqueQualType()),
9330 idx,
9331 static_cast<uint32_t>(pos->second),
9332 pos->first->getNameAsString().c_str());
9333 }
9334 }
9335
9336 {
9337 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator base_pos,
9338 base_end = layout_info.base_offsets.end();
9339 for (idx = 0, base_pos = layout_info.base_offsets.begin(); base_pos != base_end; ++base_pos, ++idx)
9340 {
9341 module_sp->LogMessage(log,
Greg Clayton6dc8d582015-08-18 22:32:36 +00009342 "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) base[%u] = { byte_offset=%u, name='%s' }",
Greg Clayton8b4edba2015-08-14 20:02:05 +00009343 clang_type.GetOpaqueQualType(), idx, (uint32_t)base_pos->second.getQuantity(),
9344 base_pos->first->getNameAsString().c_str());
9345 }
9346 }
9347 {
9348 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator vbase_pos,
9349 vbase_end = layout_info.vbase_offsets.end();
9350 for (idx = 0, vbase_pos = layout_info.vbase_offsets.begin(); vbase_pos != vbase_end; ++vbase_pos, ++idx)
9351 {
9352 module_sp->LogMessage(log,
Greg Clayton6dc8d582015-08-18 22:32:36 +00009353 "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) vbase[%u] = { byte_offset=%u, name='%s' }",
Greg Clayton8b4edba2015-08-14 20:02:05 +00009354 static_cast<void *>(clang_type.GetOpaqueQualType()), idx,
9355 static_cast<uint32_t>(vbase_pos->second.getQuantity()),
9356 vbase_pos->first->getNameAsString().c_str());
9357 }
9358 }
9359
9360 }
9361 }
9362 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
9363 }
9364 }
9365 }
9366
9367 return (bool)clang_type;
9368
9369 case DW_TAG_enumeration_type:
9370 ClangASTContext::StartTagDeclarationDefinition (clang_type);
9371 if (die->HasChildren())
9372 {
9373 SymbolContext sc(dwarf->GetCompUnitForDWARFCompUnit(dwarf_cu));
9374 bool is_signed = false;
9375 clang_type.IsIntegerType(is_signed);
9376 ParseChildEnumerators(sc, clang_type, is_signed, type->GetByteSize(), dwarf, dwarf_cu, die);
9377 }
9378 ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
9379 return (bool)clang_type;
9380
9381 default:
9382 assert(false && "not a forward clang type decl!");
9383 break;
9384 }
9385
9386 return false;
9387}
9388
Greg Clayton6dc8d582015-08-18 22:32:36 +00009389void
9390ClangASTContext::CompleteTagDecl (void *baton, clang::TagDecl *decl)
9391{
9392 ClangASTContext *ast = (ClangASTContext *)baton;
9393 SymbolFile *sym_file = ast->GetSymbolFile();
9394 if (sym_file)
9395 {
9396 CompilerType clang_type = GetTypeForDecl (decl);
9397 if (clang_type)
9398 sym_file->CompleteType (clang_type);
9399 }
9400}
9401
9402void
9403ClangASTContext::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
9404{
9405 ClangASTContext *ast = (ClangASTContext *)baton;
9406 SymbolFile *sym_file = ast->GetSymbolFile();
9407 if (sym_file)
9408 {
9409 CompilerType clang_type = GetTypeForDecl (decl);
9410 if (clang_type)
9411 sym_file->CompleteType (clang_type);
9412 }
9413}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009414
9415bool
Greg Clayton6dc8d582015-08-18 22:32:36 +00009416ClangASTContext::LayoutRecordType(void *baton,
Greg Clayton8b4edba2015-08-14 20:02:05 +00009417 const clang::RecordDecl *record_decl,
9418 uint64_t &bit_size,
9419 uint64_t &alignment,
9420 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9421 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
9422 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
9423{
Greg Clayton6dc8d582015-08-18 22:32:36 +00009424 ClangASTContext *ast = (ClangASTContext *)baton;
9425 RecordDeclToLayoutMap::iterator pos = ast->m_record_decl_to_layout_map.find (record_decl);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009426 bool success = false;
9427 base_offsets.clear();
9428 vbase_offsets.clear();
Greg Clayton6dc8d582015-08-18 22:32:36 +00009429 if (pos != ast->m_record_decl_to_layout_map.end())
Greg Clayton8b4edba2015-08-14 20:02:05 +00009430 {
9431 bit_size = pos->second.bit_size;
9432 alignment = pos->second.alignment;
9433 field_offsets.swap(pos->second.field_offsets);
9434 base_offsets.swap (pos->second.base_offsets);
9435 vbase_offsets.swap (pos->second.vbase_offsets);
Greg Clayton6dc8d582015-08-18 22:32:36 +00009436 ast->m_record_decl_to_layout_map.erase(pos);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009437 success = true;
9438 }
9439 else
9440 {
9441 bit_size = 0;
9442 alignment = 0;
9443 field_offsets.clear();
9444 }
9445 return success;
9446}
9447
9448
9449size_t
9450ClangASTContext::ParseChildEnumerators (const SymbolContext& sc,
9451 lldb_private::CompilerType &clang_type,
9452 bool is_signed,
9453 uint32_t enumerator_byte_size,
9454 SymbolFileDWARF *dwarf,
9455 DWARFCompileUnit* dwarf_cu,
9456 const DWARFDebugInfoEntry *parent_die)
9457{
9458 if (parent_die == NULL)
9459 return 0;
9460
9461 size_t enumerators_added = 0;
9462 const DWARFDebugInfoEntry *die;
9463 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
9464
9465 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
9466 {
9467 const dw_tag_t tag = die->Tag();
9468 if (tag == DW_TAG_enumerator)
9469 {
9470 DWARFDebugInfoEntry::Attributes attributes;
9471 const size_t num_child_attributes = die->GetAttributes(dwarf, dwarf_cu, fixed_form_sizes, attributes);
9472 if (num_child_attributes > 0)
9473 {
9474 const char *name = NULL;
9475 bool got_value = false;
9476 int64_t enum_value = 0;
9477 Declaration decl;
9478
9479 uint32_t i;
9480 for (i=0; i<num_child_attributes; ++i)
9481 {
9482 const dw_attr_t attr = attributes.AttributeAtIndex(i);
9483 DWARFFormValue form_value;
9484 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
9485 {
9486 switch (attr)
9487 {
9488 case DW_AT_const_value:
9489 got_value = true;
9490 if (is_signed)
9491 enum_value = form_value.Signed();
9492 else
9493 enum_value = form_value.Unsigned();
9494 break;
9495
9496 case DW_AT_name:
9497 name = form_value.AsCString(&dwarf->get_debug_str_data());
9498 break;
9499
9500 case DW_AT_description:
9501 default:
9502 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
9503 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
9504 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
9505 case DW_AT_sibling:
9506 break;
9507 }
9508 }
9509 }
9510
9511 if (name && name[0] && got_value)
9512 {
9513 AddEnumerationValueToEnumerationType (clang_type.GetOpaqueQualType(),
9514 GetEnumerationIntegerType(clang_type.GetOpaqueQualType()),
9515 decl,
9516 name,
9517 enum_value,
9518 enumerator_byte_size * 8);
9519 ++enumerators_added;
9520 }
9521 }
9522 }
9523 }
9524 return enumerators_added;
9525}
9526
9527#if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
9528
9529class DIEStack
9530{
9531public:
9532
9533 void Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
9534 {
9535 m_dies.push_back (DIEInfo(cu, die));
9536 }
9537
9538
9539 void LogDIEs (Log *log, SymbolFileDWARF *dwarf)
9540 {
9541 StreamString log_strm;
9542 const size_t n = m_dies.size();
9543 log_strm.Printf("DIEStack[%" PRIu64 "]:\n", (uint64_t)n);
9544 for (size_t i=0; i<n; i++)
9545 {
9546 DWARFCompileUnit *cu = m_dies[i].cu;
9547 const DWARFDebugInfoEntry *die = m_dies[i].die;
9548 std::string qualified_name;
9549 die->GetQualifiedName(dwarf, cu, qualified_name);
9550 log_strm.Printf ("[%" PRIu64 "] 0x%8.8x: %s name='%s'\n",
9551 (uint64_t)i,
9552 die->GetOffset(),
9553 DW_TAG_value_to_name(die->Tag()),
9554 qualified_name.c_str());
9555 }
9556 log->PutCString(log_strm.GetData());
9557 }
9558 void Pop ()
9559 {
9560 m_dies.pop_back();
9561 }
9562
9563 class ScopedPopper
9564 {
9565 public:
9566 ScopedPopper (DIEStack &die_stack) :
9567 m_die_stack (die_stack),
9568 m_valid (false)
9569 {
9570 }
9571
9572 void
9573 Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
9574 {
9575 m_valid = true;
9576 m_die_stack.Push (cu, die);
9577 }
9578
9579 ~ScopedPopper ()
9580 {
9581 if (m_valid)
9582 m_die_stack.Pop();
9583 }
9584
9585
9586
9587 protected:
9588 DIEStack &m_die_stack;
9589 bool m_valid;
9590 };
9591
9592protected:
9593 struct DIEInfo {
9594 DIEInfo (DWARFCompileUnit *c, const DWARFDebugInfoEntry *d) :
9595 cu(c),
9596 die(d)
9597 {
9598 }
9599 DWARFCompileUnit *cu;
9600 const DWARFDebugInfoEntry *die;
9601 };
9602 typedef std::vector<DIEInfo> Stack;
9603 Stack m_dies;
9604};
9605#endif
9606
9607
9608
9609static AccessType
9610DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
9611{
9612 switch (dwarf_accessibility)
9613 {
9614 case DW_ACCESS_public: return eAccessPublic;
9615 case DW_ACCESS_private: return eAccessPrivate;
9616 case DW_ACCESS_protected: return eAccessProtected;
9617 default: break;
9618 }
9619 return eAccessNone;
9620}
9621
9622static bool
9623DeclKindIsCXXClass (clang::Decl::Kind decl_kind)
9624{
9625 switch (decl_kind)
9626 {
9627 case clang::Decl::CXXRecord:
9628 case clang::Decl::ClassTemplateSpecialization:
9629 return true;
9630 default:
9631 break;
9632 }
9633 return false;
9634}
9635
9636struct BitfieldInfo
9637{
9638 uint64_t bit_size;
9639 uint64_t bit_offset;
9640
9641 BitfieldInfo () :
9642 bit_size (LLDB_INVALID_ADDRESS),
9643 bit_offset (LLDB_INVALID_ADDRESS)
9644 {
9645 }
9646
9647 void
9648 Clear()
9649 {
9650 bit_size = LLDB_INVALID_ADDRESS;
9651 bit_offset = LLDB_INVALID_ADDRESS;
9652 }
9653
9654 bool IsValid ()
9655 {
9656 return (bit_size != LLDB_INVALID_ADDRESS) &&
9657 (bit_offset != LLDB_INVALID_ADDRESS);
9658 }
9659};
9660
9661Function *
9662ClangASTContext::ParseFunctionFromDWARF (const SymbolContext& sc,
9663 SymbolFileDWARF *dwarf,
9664 DWARFCompileUnit* dwarf_cu,
9665 const DWARFDebugInfoEntry *die)
9666{
9667 DWARFDebugRanges::RangeList func_ranges;
9668 const char *name = NULL;
9669 const char *mangled = NULL;
9670 int decl_file = 0;
9671 int decl_line = 0;
9672 int decl_column = 0;
9673 int call_file = 0;
9674 int call_line = 0;
9675 int call_column = 0;
9676 DWARFExpression frame_base;
9677
9678 assert (die->Tag() == DW_TAG_subprogram);
9679
9680 if (die->Tag() != DW_TAG_subprogram)
9681 return NULL;
9682
9683 if (die->GetDIENamesAndRanges (dwarf,
9684 dwarf_cu,
9685 name,
9686 mangled,
9687 func_ranges,
9688 decl_file,
9689 decl_line,
9690 decl_column,
9691 call_file,
9692 call_line,
9693 call_column,
9694 &frame_base))
9695 {
9696 // Union of all ranges in the function DIE (if the function is discontiguous)
9697 AddressRange func_range;
9698 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0);
9699 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0);
9700 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
9701 {
9702 ModuleSP module_sp (dwarf->GetObjectFile()->GetModule());
9703 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, module_sp->GetSectionList());
9704 if (func_range.GetBaseAddress().IsValid())
9705 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
9706 }
9707
9708 if (func_range.GetBaseAddress().IsValid())
9709 {
9710 Mangled func_name;
9711 if (mangled)
9712 func_name.SetValue(ConstString(mangled), true);
9713 else if (die->GetParent()->Tag() == DW_TAG_compile_unit &&
9714 LanguageRuntime::LanguageIsCPlusPlus(dwarf_cu->GetLanguageType()) &&
9715 name && strcmp(name, "main") != 0)
9716 {
9717 // If the mangled name is not present in the DWARF, generate the demangled name
9718 // using the decl context. We skip if the function is "main" as its name is
9719 // never mangled.
9720 bool is_static = false;
9721 bool is_variadic = false;
9722 unsigned type_quals = 0;
9723 std::vector<CompilerType> param_types;
9724 std::vector<clang::ParmVarDecl*> param_decls;
9725 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
9726 DWARFDeclContext decl_ctx;
9727 StreamString sstr;
9728
9729 die->GetDWARFDeclContext(dwarf, dwarf_cu, decl_ctx);
9730 sstr << decl_ctx.GetQualifiedName();
9731
9732 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE(dwarf,
9733 dwarf_cu,
9734 die,
9735 &decl_ctx_die);
9736 ParseChildParameters(sc,
9737 containing_decl_ctx,
9738 dwarf,
9739 dwarf_cu,
9740 die,
9741 true,
9742 is_static,
9743 is_variadic,
9744 param_types,
9745 param_decls,
9746 type_quals);
9747 sstr << "(";
9748 for (size_t i = 0; i < param_types.size(); i++)
9749 {
9750 if (i > 0)
9751 sstr << ", ";
9752 sstr << param_types[i].GetTypeName();
9753 }
9754 if (is_variadic)
9755 sstr << ", ...";
9756 sstr << ")";
9757 if (type_quals & clang::Qualifiers::Const)
9758 sstr << " const";
9759
9760 func_name.SetValue(ConstString(sstr.GetData()), false);
9761 }
9762 else
9763 func_name.SetValue(ConstString(name), false);
9764
9765 FunctionSP func_sp;
9766 std::unique_ptr<Declaration> decl_ap;
9767 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
9768 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
9769 decl_line,
9770 decl_column));
9771
9772 // Supply the type _only_ if it has already been parsed
9773 Type *func_type = dwarf->m_die_to_type.lookup (die);
9774
9775 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
9776
9777 if (dwarf->FixupAddress (func_range.GetBaseAddress()))
9778 {
9779 const user_id_t func_user_id = dwarf->MakeUserID(die->GetOffset());
9780 func_sp.reset(new Function (sc.comp_unit,
9781 dwarf->MakeUserID(func_user_id), // UserID is the DIE offset
9782 dwarf->MakeUserID(func_user_id),
9783 func_name,
9784 func_type,
9785 func_range)); // first address range
9786
9787 if (func_sp.get() != NULL)
9788 {
9789 if (frame_base.IsValid())
9790 func_sp->GetFrameBaseExpression() = frame_base;
9791 sc.comp_unit->AddFunction(func_sp);
9792 return func_sp.get();
9793 }
9794 }
9795 }
9796 }
9797 return NULL;
9798}
9799
9800
9801size_t
9802ClangASTContext::ParseChildMembers (const SymbolContext& sc,
9803 SymbolFileDWARF *dwarf,
9804 DWARFCompileUnit* dwarf_cu,
9805 const DWARFDebugInfoEntry *parent_die,
9806 CompilerType &class_clang_type,
9807 const LanguageType class_language,
9808 std::vector<clang::CXXBaseSpecifier *>& base_classes,
9809 std::vector<int>& member_accessibilities,
9810 DWARFDIECollection& member_function_dies,
9811 DelayedPropertyList& delayed_properties,
9812 AccessType& default_accessibility,
9813 bool &is_a_class,
9814 LayoutInfo &layout_info)
9815{
9816 if (parent_die == NULL)
9817 return 0;
9818
9819 size_t count = 0;
9820 const DWARFDebugInfoEntry *die;
9821 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
9822 uint32_t member_idx = 0;
9823 BitfieldInfo last_field_info;
9824 ModuleSP module_sp = dwarf->GetObjectFile()->GetModule();
9825 ClangASTContext* ast = class_clang_type.GetTypeSystem()->AsClangASTContext();
9826 if (ast == nullptr)
9827 return 0;
9828
9829 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
9830 {
9831 dw_tag_t tag = die->Tag();
9832
9833 switch (tag)
9834 {
9835 case DW_TAG_member:
9836 case DW_TAG_APPLE_property:
9837 {
9838 DWARFDebugInfoEntry::Attributes attributes;
9839 const size_t num_attributes = die->GetAttributes (dwarf,
9840 dwarf_cu,
9841 fixed_form_sizes,
9842 attributes);
9843 if (num_attributes > 0)
9844 {
9845 Declaration decl;
9846 //DWARFExpression location;
9847 const char *name = NULL;
9848 const char *prop_name = NULL;
9849 const char *prop_getter_name = NULL;
9850 const char *prop_setter_name = NULL;
9851 uint32_t prop_attributes = 0;
9852
9853
9854 bool is_artificial = false;
9855 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
9856 AccessType accessibility = eAccessNone;
9857 uint32_t member_byte_offset = UINT32_MAX;
9858 size_t byte_size = 0;
9859 size_t bit_offset = 0;
9860 size_t bit_size = 0;
9861 bool is_external = false; // On DW_TAG_members, this means the member is static
9862 uint32_t i;
9863 for (i=0; i<num_attributes && !is_artificial; ++i)
9864 {
9865 const dw_attr_t attr = attributes.AttributeAtIndex(i);
9866 DWARFFormValue form_value;
9867 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
9868 {
9869 switch (attr)
9870 {
9871 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
9872 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
9873 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
9874 case DW_AT_name: name = form_value.AsCString(&dwarf->get_debug_str_data()); break;
9875 case DW_AT_type: encoding_uid = form_value.Reference(); break;
9876 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
9877 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
9878 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
9879 case DW_AT_data_member_location:
9880 if (form_value.BlockData())
9881 {
9882 Value initialValue(0);
9883 Value memberOffset(0);
9884 const DWARFDataExtractor& debug_info_data = dwarf->get_debug_info_data();
9885 uint32_t block_length = form_value.Unsigned();
9886 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
9887 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
9888 NULL, // ClangExpressionVariableList *
9889 NULL, // ClangExpressionDeclMap *
9890 NULL, // RegisterContext *
9891 module_sp,
9892 debug_info_data,
9893 block_offset,
9894 block_length,
9895 eRegisterKindDWARF,
9896 &initialValue,
9897 memberOffset,
9898 NULL))
9899 {
9900 member_byte_offset = memberOffset.ResolveValue(NULL).UInt();
9901 }
9902 }
9903 else
9904 {
9905 // With DWARF 3 and later, if the value is an integer constant,
9906 // this form value is the offset in bytes from the beginning
9907 // of the containing entity.
9908 member_byte_offset = form_value.Unsigned();
9909 }
9910 break;
9911
9912 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
9913 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
9914 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&dwarf->get_debug_str_data()); break;
9915 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&dwarf->get_debug_str_data()); break;
9916 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&dwarf->get_debug_str_data()); break;
9917 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
9918 case DW_AT_external: is_external = form_value.Boolean(); break;
9919
9920 default:
9921 case DW_AT_declaration:
9922 case DW_AT_description:
9923 case DW_AT_mutable:
9924 case DW_AT_visibility:
9925 case DW_AT_sibling:
9926 break;
9927 }
9928 }
9929 }
9930
9931 if (prop_name)
9932 {
9933 ConstString fixed_getter;
9934 ConstString fixed_setter;
9935
9936 // Check if the property getter/setter were provided as full
9937 // names. We want basenames, so we extract them.
9938
9939 if (prop_getter_name && prop_getter_name[0] == '-')
9940 {
9941 ObjCLanguageRuntime::MethodName prop_getter_method(prop_getter_name, true);
9942 prop_getter_name = prop_getter_method.GetSelector().GetCString();
9943 }
9944
9945 if (prop_setter_name && prop_setter_name[0] == '-')
9946 {
9947 ObjCLanguageRuntime::MethodName prop_setter_method(prop_setter_name, true);
9948 prop_setter_name = prop_setter_method.GetSelector().GetCString();
9949 }
9950
9951 // If the names haven't been provided, they need to be
9952 // filled in.
9953
9954 if (!prop_getter_name)
9955 {
9956 prop_getter_name = prop_name;
9957 }
9958 if (!prop_setter_name && prop_name[0] && !(prop_attributes & DW_APPLE_PROPERTY_readonly))
9959 {
9960 StreamString ss;
9961
9962 ss.Printf("set%c%s:",
9963 toupper(prop_name[0]),
9964 &prop_name[1]);
9965
9966 fixed_setter.SetCString(ss.GetData());
9967 prop_setter_name = fixed_setter.GetCString();
9968 }
9969 }
9970
9971 // Clang has a DWARF generation bug where sometimes it
9972 // represents fields that are references with bad byte size
9973 // and bit size/offset information such as:
9974 //
9975 // DW_AT_byte_size( 0x00 )
9976 // DW_AT_bit_size( 0x40 )
9977 // DW_AT_bit_offset( 0xffffffffffffffc0 )
9978 //
9979 // So check the bit offset to make sure it is sane, and if
9980 // the values are not sane, remove them. If we don't do this
9981 // then we will end up with a crash if we try to use this
9982 // type in an expression when clang becomes unhappy with its
9983 // recycled debug info.
9984
9985 if (bit_offset > 128)
9986 {
9987 bit_size = 0;
9988 bit_offset = 0;
9989 }
9990
9991 // FIXME: Make Clang ignore Objective-C accessibility for expressions
9992 if (class_language == eLanguageTypeObjC ||
9993 class_language == eLanguageTypeObjC_plus_plus)
9994 accessibility = eAccessNone;
9995
9996 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
9997 {
9998 // Not all compilers will mark the vtable pointer
9999 // member as artificial (llvm-gcc). We can't have
10000 // the virtual members in our classes otherwise it
10001 // throws off all child offsets since we end up
10002 // having and extra pointer sized member in our
10003 // class layouts.
10004 is_artificial = true;
10005 }
10006
10007 // Handle static members
10008 if (is_external && member_byte_offset == UINT32_MAX)
10009 {
10010 Type *var_type = dwarf->ResolveTypeUID(encoding_uid);
10011
10012 if (var_type)
10013 {
10014 if (accessibility == eAccessNone)
10015 accessibility = eAccessPublic;
10016 ClangASTContext::AddVariableToRecordType (class_clang_type,
10017 name,
10018 var_type->GetClangLayoutType(),
10019 accessibility);
10020 }
10021 break;
10022 }
10023
10024 if (is_artificial == false)
10025 {
10026 Type *member_type = dwarf->ResolveTypeUID(encoding_uid);
10027
10028 clang::FieldDecl *field_decl = NULL;
10029 if (tag == DW_TAG_member)
10030 {
10031 if (member_type)
10032 {
10033 if (accessibility == eAccessNone)
10034 accessibility = default_accessibility;
10035 member_accessibilities.push_back(accessibility);
10036
10037 uint64_t field_bit_offset = (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
10038 if (bit_size > 0)
10039 {
10040
10041 BitfieldInfo this_field_info;
10042 this_field_info.bit_offset = field_bit_offset;
10043 this_field_info.bit_size = bit_size;
10044
10045 /////////////////////////////////////////////////////////////
10046 // How to locate a field given the DWARF debug information
10047 //
10048 // AT_byte_size indicates the size of the word in which the
10049 // bit offset must be interpreted.
10050 //
10051 // AT_data_member_location indicates the byte offset of the
10052 // word from the base address of the structure.
10053 //
10054 // AT_bit_offset indicates how many bits into the word
10055 // (according to the host endianness) the low-order bit of
10056 // the field starts. AT_bit_offset can be negative.
10057 //
10058 // AT_bit_size indicates the size of the field in bits.
10059 /////////////////////////////////////////////////////////////
10060
10061 if (byte_size == 0)
10062 byte_size = member_type->GetByteSize();
10063
10064 if (dwarf->GetObjectFile()->GetByteOrder() == eByteOrderLittle)
10065 {
10066 this_field_info.bit_offset += byte_size * 8;
10067 this_field_info.bit_offset -= (bit_offset + bit_size);
10068 }
10069 else
10070 {
10071 this_field_info.bit_offset += bit_offset;
10072 }
10073
10074 // Update the field bit offset we will report for layout
10075 field_bit_offset = this_field_info.bit_offset;
10076
10077 // If the member to be emitted did not start on a character boundary and there is
10078 // empty space between the last field and this one, then we need to emit an
10079 // anonymous member filling up the space up to its start. There are three cases
10080 // here:
10081 //
10082 // 1 If the previous member ended on a character boundary, then we can emit an
10083 // anonymous member starting at the most recent character boundary.
10084 //
10085 // 2 If the previous member did not end on a character boundary and the distance
10086 // from the end of the previous member to the current member is less than a
10087 // word width, then we can emit an anonymous member starting right after the
10088 // previous member and right before this member.
10089 //
10090 // 3 If the previous member did not end on a character boundary and the distance
10091 // from the end of the previous member to the current member is greater than
10092 // or equal a word width, then we act as in Case 1.
10093
10094 const uint64_t character_width = 8;
10095 const uint64_t word_width = 32;
10096
10097 // Objective-C has invalid DW_AT_bit_offset values in older versions
10098 // of clang, so we have to be careful and only insert unnamed bitfields
10099 // if we have a new enough clang.
10100 bool detect_unnamed_bitfields = true;
10101
10102 if (class_language == eLanguageTypeObjC || class_language == eLanguageTypeObjC_plus_plus)
10103 detect_unnamed_bitfields = dwarf_cu->Supports_unnamed_objc_bitfields ();
10104
10105 if (detect_unnamed_bitfields)
10106 {
10107 BitfieldInfo anon_field_info;
10108
10109 if ((this_field_info.bit_offset % character_width) != 0) // not char aligned
10110 {
10111 uint64_t last_field_end = 0;
10112
10113 if (last_field_info.IsValid())
10114 last_field_end = last_field_info.bit_offset + last_field_info.bit_size;
10115
10116 if (this_field_info.bit_offset != last_field_end)
10117 {
10118 if (((last_field_end % character_width) == 0) || // case 1
10119 (this_field_info.bit_offset - last_field_end >= word_width)) // case 3
10120 {
10121 anon_field_info.bit_size = this_field_info.bit_offset % character_width;
10122 anon_field_info.bit_offset = this_field_info.bit_offset - anon_field_info.bit_size;
10123 }
10124 else // case 2
10125 {
10126 anon_field_info.bit_size = this_field_info.bit_offset - last_field_end;
10127 anon_field_info.bit_offset = last_field_end;
10128 }
10129 }
10130 }
10131
10132 if (anon_field_info.IsValid())
10133 {
10134 clang::FieldDecl *unnamed_bitfield_decl =
10135 ClangASTContext::AddFieldToRecordType (class_clang_type,
10136 NULL,
10137 GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, word_width),
10138 accessibility,
10139 anon_field_info.bit_size);
10140
10141 layout_info.field_offsets.insert(
10142 std::make_pair(unnamed_bitfield_decl, anon_field_info.bit_offset));
10143 }
10144 }
10145 last_field_info = this_field_info;
10146 }
10147 else
10148 {
10149 last_field_info.Clear();
10150 }
10151
10152 CompilerType member_clang_type = member_type->GetClangLayoutType();
10153
10154 {
10155 // Older versions of clang emit array[0] and array[1] in the same way (<rdar://problem/12566646>).
10156 // If the current field is at the end of the structure, then there is definitely no room for extra
10157 // elements and we override the type to array[0].
10158
10159 CompilerType member_array_element_type;
10160 uint64_t member_array_size;
10161 bool member_array_is_incomplete;
10162
10163 if (member_clang_type.IsArrayType(&member_array_element_type,
10164 &member_array_size,
10165 &member_array_is_incomplete) &&
10166 !member_array_is_incomplete)
10167 {
10168 uint64_t parent_byte_size = parent_die->GetAttributeValueAsUnsigned(dwarf, dwarf_cu, DW_AT_byte_size, UINT64_MAX);
10169
10170 if (member_byte_offset >= parent_byte_size)
10171 {
10172 if (member_array_size != 1)
10173 {
10174 module_sp->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64 " which extends beyond the bounds of 0x%8.8" PRIx64,
10175 dwarf->MakeUserID(die->GetOffset()),
10176 name,
10177 encoding_uid,
10178 dwarf->MakeUserID(parent_die->GetOffset()));
10179 }
10180
10181 member_clang_type = CreateArrayType(member_array_element_type, 0, false);
10182 }
10183 }
10184 }
10185
10186 field_decl = ClangASTContext::AddFieldToRecordType (class_clang_type,
10187 name,
10188 member_clang_type,
10189 accessibility,
10190 bit_size);
10191
10192 SetMetadataAsUserID (field_decl, dwarf->MakeUserID(die->GetOffset()));
10193
10194 layout_info.field_offsets.insert(std::make_pair(field_decl, field_bit_offset));
10195 }
10196 else
10197 {
10198 if (name)
10199 module_sp->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64 " which was unable to be parsed",
10200 dwarf->MakeUserID(die->GetOffset()),
10201 name,
10202 encoding_uid);
10203 else
10204 module_sp->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member refers to type 0x%8.8" PRIx64 " which was unable to be parsed",
10205 dwarf->MakeUserID(die->GetOffset()),
10206 encoding_uid);
10207 }
10208 }
10209
10210 if (prop_name != NULL && member_type)
10211 {
10212 clang::ObjCIvarDecl *ivar_decl = NULL;
10213
10214 if (field_decl)
10215 {
10216 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
10217 assert (ivar_decl != NULL);
10218 }
10219
10220 ClangASTMetadata metadata;
10221 metadata.SetUserID (dwarf->MakeUserID(die->GetOffset()));
10222 delayed_properties.push_back(DelayedAddObjCClassProperty(class_clang_type,
10223 prop_name,
10224 member_type->GetClangLayoutType(),
10225 ivar_decl,
10226 prop_setter_name,
10227 prop_getter_name,
10228 prop_attributes,
10229 &metadata));
10230
10231 if (ivar_decl)
10232 SetMetadataAsUserID (ivar_decl, dwarf->MakeUserID(die->GetOffset()));
10233 }
10234 }
10235 }
10236 ++member_idx;
10237 }
10238 break;
10239
10240 case DW_TAG_subprogram:
10241 // Let the type parsing code handle this one for us.
10242 member_function_dies.Append (die);
10243 break;
10244
10245 case DW_TAG_inheritance:
10246 {
10247 is_a_class = true;
10248 if (default_accessibility == eAccessNone)
10249 default_accessibility = eAccessPrivate;
10250 // TODO: implement DW_TAG_inheritance type parsing
10251 DWARFDebugInfoEntry::Attributes attributes;
10252 const size_t num_attributes = die->GetAttributes (dwarf,
10253 dwarf_cu,
10254 fixed_form_sizes,
10255 attributes);
10256 if (num_attributes > 0)
10257 {
10258 Declaration decl;
10259 DWARFExpression location;
10260 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
10261 AccessType accessibility = default_accessibility;
10262 bool is_virtual = false;
10263 bool is_base_of_class = true;
10264 off_t member_byte_offset = 0;
10265 uint32_t i;
10266 for (i=0; i<num_attributes; ++i)
10267 {
10268 const dw_attr_t attr = attributes.AttributeAtIndex(i);
10269 DWARFFormValue form_value;
10270 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
10271 {
10272 switch (attr)
10273 {
10274 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
10275 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
10276 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
10277 case DW_AT_type: encoding_uid = form_value.Reference(); break;
10278 case DW_AT_data_member_location:
10279 if (form_value.BlockData())
10280 {
10281 Value initialValue(0);
10282 Value memberOffset(0);
10283 const DWARFDataExtractor& debug_info_data = dwarf->get_debug_info_data();
10284 uint32_t block_length = form_value.Unsigned();
10285 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
10286 if (DWARFExpression::Evaluate (NULL,
10287 NULL,
10288 NULL,
10289 NULL,
10290 module_sp,
10291 debug_info_data,
10292 block_offset,
10293 block_length,
10294 eRegisterKindDWARF,
10295 &initialValue,
10296 memberOffset,
10297 NULL))
10298 {
10299 member_byte_offset = memberOffset.ResolveValue(NULL).UInt();
10300 }
10301 }
10302 else
10303 {
10304 // With DWARF 3 and later, if the value is an integer constant,
10305 // this form value is the offset in bytes from the beginning
10306 // of the containing entity.
10307 member_byte_offset = form_value.Unsigned();
10308 }
10309 break;
10310
10311 case DW_AT_accessibility:
10312 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
10313 break;
10314
10315 case DW_AT_virtuality:
10316 is_virtual = form_value.Boolean();
10317 break;
10318
10319 case DW_AT_sibling:
10320 break;
10321
10322 default:
10323 break;
10324 }
10325 }
10326 }
10327
10328 Type *base_class_type = dwarf->ResolveTypeUID(encoding_uid);
10329 if (base_class_type == NULL)
10330 {
10331 module_sp->ReportError("0x%8.8x: DW_TAG_inheritance failed to resolve the base class at 0x%8.8" PRIx64 " from enclosing type 0x%8.8x. \nPlease file a bug and attach the file at the start of this error message",
10332 die->GetOffset(),
10333 encoding_uid,
10334 parent_die->GetOffset());
10335 break;
10336 }
10337
10338 CompilerType base_class_clang_type = base_class_type->GetClangFullType();
10339 assert (base_class_clang_type);
10340 if (class_language == eLanguageTypeObjC)
10341 {
10342 ast->SetObjCSuperClass(class_clang_type, base_class_clang_type);
10343 }
10344 else
10345 {
10346 base_classes.push_back (ast->CreateBaseClassSpecifier (base_class_clang_type.GetOpaqueQualType(),
10347 accessibility,
10348 is_virtual,
10349 is_base_of_class));
10350
10351 if (is_virtual)
10352 {
10353 // Do not specify any offset for virtual inheritance. The DWARF produced by clang doesn't
10354 // give us a constant offset, but gives us a DWARF expressions that requires an actual object
10355 // in memory. the DW_AT_data_member_location for a virtual base class looks like:
10356 // DW_AT_data_member_location( DW_OP_dup, DW_OP_deref, DW_OP_constu(0x00000018), DW_OP_minus, DW_OP_deref, DW_OP_plus )
10357 // Given this, there is really no valid response we can give to clang for virtual base
10358 // class offsets, and this should eventually be removed from LayoutRecordType() in the external
10359 // AST source in clang.
10360 }
10361 else
10362 {
10363 layout_info.base_offsets.insert(
10364 std::make_pair(ast->GetAsCXXRecordDecl(base_class_clang_type.GetOpaqueQualType()),
10365 clang::CharUnits::fromQuantity(member_byte_offset)));
10366 }
10367 }
10368 }
10369 }
10370 break;
10371
10372 default:
10373 break;
10374 }
10375 }
10376
10377 return count;
10378}
10379
10380
10381size_t
10382ClangASTContext::ParseChildParameters (const SymbolContext& sc,
10383 clang::DeclContext *containing_decl_ctx,
10384 SymbolFileDWARF *dwarf,
10385 DWARFCompileUnit* dwarf_cu,
10386 const DWARFDebugInfoEntry *parent_die,
10387 bool skip_artificial,
10388 bool &is_static,
10389 bool &is_variadic,
10390 std::vector<CompilerType>& function_param_types,
10391 std::vector<clang::ParmVarDecl*>& function_param_decls,
10392 unsigned &type_quals)
10393{
10394 if (parent_die == NULL)
10395 return 0;
10396
10397 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
10398
10399 size_t arg_idx = 0;
10400 const DWARFDebugInfoEntry *die;
10401 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
10402 {
10403 dw_tag_t tag = die->Tag();
10404 switch (tag)
10405 {
10406 case DW_TAG_formal_parameter:
10407 {
10408 DWARFDebugInfoEntry::Attributes attributes;
10409 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, fixed_form_sizes, attributes);
10410 if (num_attributes > 0)
10411 {
10412 const char *name = NULL;
10413 Declaration decl;
10414 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
10415 bool is_artificial = false;
10416 // one of None, Auto, Register, Extern, Static, PrivateExtern
10417
10418 clang::StorageClass storage = clang::SC_None;
10419 uint32_t i;
10420 for (i=0; i<num_attributes; ++i)
10421 {
10422 const dw_attr_t attr = attributes.AttributeAtIndex(i);
10423 DWARFFormValue form_value;
10424 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
10425 {
10426 switch (attr)
10427 {
10428 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
10429 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
10430 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
10431 case DW_AT_name: name = form_value.AsCString(&dwarf->get_debug_str_data()); break;
10432 case DW_AT_type: param_type_die_offset = form_value.Reference(); break;
10433 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
10434 case DW_AT_location:
10435 // if (form_value.BlockData())
10436 // {
10437 // const DWARFDataExtractor& debug_info_data = debug_info();
10438 // uint32_t block_length = form_value.Unsigned();
10439 // DWARFDataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
10440 // }
10441 // else
10442 // {
10443 // }
10444 // break;
10445 case DW_AT_const_value:
10446 case DW_AT_default_value:
10447 case DW_AT_description:
10448 case DW_AT_endianity:
10449 case DW_AT_is_optional:
10450 case DW_AT_segment:
10451 case DW_AT_variable_parameter:
10452 default:
10453 case DW_AT_abstract_origin:
10454 case DW_AT_sibling:
10455 break;
10456 }
10457 }
10458 }
10459
10460 bool skip = false;
10461 if (skip_artificial)
10462 {
10463 if (is_artificial)
10464 {
10465 // In order to determine if a C++ member function is
10466 // "const" we have to look at the const-ness of "this"...
10467 // Ugly, but that
10468 if (arg_idx == 0)
10469 {
10470 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
10471 {
10472 // Often times compilers omit the "this" name for the
10473 // specification DIEs, so we can't rely upon the name
10474 // being in the formal parameter DIE...
10475 if (name == NULL || ::strcmp(name, "this")==0)
10476 {
10477 Type *this_type = dwarf->ResolveTypeUID (param_type_die_offset);
10478 if (this_type)
10479 {
10480 uint32_t encoding_mask = this_type->GetEncodingMask();
10481 if (encoding_mask & Type::eEncodingIsPointerUID)
10482 {
10483 is_static = false;
10484
10485 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
10486 type_quals |= clang::Qualifiers::Const;
10487 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
10488 type_quals |= clang::Qualifiers::Volatile;
10489 }
10490 }
10491 }
10492 }
10493 }
10494 skip = true;
10495 }
10496 else
10497 {
10498
10499 // HACK: Objective C formal parameters "self" and "_cmd"
10500 // are not marked as artificial in the DWARF...
10501 CompileUnit *comp_unit = dwarf->GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
10502 if (comp_unit)
10503 {
10504 switch (comp_unit->GetLanguage())
10505 {
10506 case eLanguageTypeObjC:
10507 case eLanguageTypeObjC_plus_plus:
10508 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
10509 skip = true;
10510 break;
10511 default:
10512 break;
10513 }
10514 }
10515 }
10516 }
10517
10518 if (!skip)
10519 {
10520 Type *type = dwarf->ResolveTypeUID(param_type_die_offset);
10521 if (type)
10522 {
10523 function_param_types.push_back (type->GetClangForwardType());
10524
10525 clang::ParmVarDecl *param_var_decl = CreateParameterDeclaration (name,
10526 type->GetClangForwardType(),
10527 storage);
10528 assert(param_var_decl);
10529 function_param_decls.push_back(param_var_decl);
10530
10531 SetMetadataAsUserID (param_var_decl, dwarf->MakeUserID(die->GetOffset()));
10532 }
10533 }
10534 }
10535 arg_idx++;
10536 }
10537 break;
10538
10539 case DW_TAG_unspecified_parameters:
10540 is_variadic = true;
10541 break;
10542
10543 case DW_TAG_template_type_parameter:
10544 case DW_TAG_template_value_parameter:
10545 // The one caller of this was never using the template_param_infos,
10546 // and the local variable was taking up a large amount of stack space
10547 // in SymbolFileDWARF::ParseType() so this was removed. If we ever need
10548 // the template params back, we can add them back.
10549 // ParseTemplateDIE (dwarf_cu, die, template_param_infos);
10550 break;
10551
10552 default:
10553 break;
10554 }
10555 }
10556 return arg_idx;
10557}
10558
10559void
10560ClangASTContext::ParseChildArrayInfo (const SymbolContext& sc,
10561 SymbolFileDWARF *dwarf,
10562 DWARFCompileUnit* dwarf_cu,
10563 const DWARFDebugInfoEntry *parent_die,
10564 int64_t& first_index,
10565 std::vector<uint64_t>& element_orders,
10566 uint32_t& byte_stride,
10567 uint32_t& bit_stride)
10568{
10569 if (parent_die == NULL)
10570 return;
10571
10572 const DWARFDebugInfoEntry *die;
10573 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
10574 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
10575 {
10576 const dw_tag_t tag = die->Tag();
10577 switch (tag)
10578 {
10579 case DW_TAG_subrange_type:
10580 {
10581 DWARFDebugInfoEntry::Attributes attributes;
10582 const size_t num_child_attributes = die->GetAttributes(dwarf, dwarf_cu, fixed_form_sizes, attributes);
10583 if (num_child_attributes > 0)
10584 {
10585 uint64_t num_elements = 0;
10586 uint64_t lower_bound = 0;
10587 uint64_t upper_bound = 0;
10588 bool upper_bound_valid = false;
10589 uint32_t i;
10590 for (i=0; i<num_child_attributes; ++i)
10591 {
10592 const dw_attr_t attr = attributes.AttributeAtIndex(i);
10593 DWARFFormValue form_value;
10594 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
10595 {
10596 switch (attr)
10597 {
10598 case DW_AT_name:
10599 break;
10600
10601 case DW_AT_count:
10602 num_elements = form_value.Unsigned();
10603 break;
10604
10605 case DW_AT_bit_stride:
10606 bit_stride = form_value.Unsigned();
10607 break;
10608
10609 case DW_AT_byte_stride:
10610 byte_stride = form_value.Unsigned();
10611 break;
10612
10613 case DW_AT_lower_bound:
10614 lower_bound = form_value.Unsigned();
10615 break;
10616
10617 case DW_AT_upper_bound:
10618 upper_bound_valid = true;
10619 upper_bound = form_value.Unsigned();
10620 break;
10621
10622 default:
10623 case DW_AT_abstract_origin:
10624 case DW_AT_accessibility:
10625 case DW_AT_allocated:
10626 case DW_AT_associated:
10627 case DW_AT_data_location:
10628 case DW_AT_declaration:
10629 case DW_AT_description:
10630 case DW_AT_sibling:
10631 case DW_AT_threads_scaled:
10632 case DW_AT_type:
10633 case DW_AT_visibility:
10634 break;
10635 }
10636 }
10637 }
10638
10639 if (num_elements == 0)
10640 {
10641 if (upper_bound_valid && upper_bound >= lower_bound)
10642 num_elements = upper_bound - lower_bound + 1;
10643 }
10644
10645 element_orders.push_back (num_elements);
10646 }
10647 }
10648 break;
10649 }
10650 }
10651}
10652
10653clang::DeclContext*
10654ClangASTContext::GetClangDeclContextContainingTypeUID (SymbolFileDWARF *dwarf, lldb::user_id_t type_uid)
10655{
10656 DWARFDebugInfo* debug_info = dwarf->DebugInfo();
10657 if (debug_info && dwarf->UserIDMatches(type_uid))
10658 {
10659 DWARFCompileUnitSP cu_sp;
10660 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
10661 if (die)
10662 return GetClangDeclContextContainingDIE (dwarf, cu_sp.get(), die, NULL);
10663 }
10664 return NULL;
10665}
10666
10667clang::DeclContext*
10668ClangASTContext::GetClangDeclContextForTypeUID (SymbolFileDWARF *dwarf, const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
10669{
10670 if (dwarf->UserIDMatches(type_uid))
10671 return GetClangDeclContextForDIEOffset (dwarf, sc, type_uid);
10672 return NULL;
10673}
10674
10675
10676clang::DeclContext *
10677ClangASTContext::GetClangDeclContextForDIE (SymbolFileDWARF *dwarf,
10678 const SymbolContext &sc,
10679 DWARFCompileUnit *cu,
10680 const DWARFDebugInfoEntry *die)
10681{
Greg Clayton6dc8d582015-08-18 22:32:36 +000010682 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010683 if (clang_decl_ctx)
10684 return clang_decl_ctx;
10685 // If this DIE has a specification, or an abstract origin, then trace to those.
10686
10687 dw_offset_t die_offset = die->GetAttributeValueAsReference(dwarf, cu, DW_AT_specification, DW_INVALID_OFFSET);
10688 if (die_offset != DW_INVALID_OFFSET)
10689 return GetClangDeclContextForDIEOffset (dwarf, sc, die_offset);
10690
10691 die_offset = die->GetAttributeValueAsReference(dwarf, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
10692 if (die_offset != DW_INVALID_OFFSET)
10693 return GetClangDeclContextForDIEOffset (dwarf, sc, die_offset);
10694
10695 Log *log = nullptr; //(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
10696 if (log)
10697 dwarf->GetObjectFile()->GetModule()->LogMessage(log, "SymbolFileDWARF::GetClangDeclContextForDIE (die = 0x%8.8x) %s '%s'", die->GetOffset(), DW_TAG_value_to_name(die->Tag()), die->GetName(dwarf, cu));
10698 // This is the DIE we want. Parse it, then query our map.
10699 bool assert_not_being_parsed = true;
10700 dwarf->ResolveTypeUID (cu, die, assert_not_being_parsed);
10701
Greg Clayton6dc8d582015-08-18 22:32:36 +000010702 clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010703
10704 return clang_decl_ctx;
10705}
10706
10707
10708clang::DeclContext *
10709ClangASTContext::GetClangDeclContextContainingDIEOffset (SymbolFileDWARF *dwarf,
10710 dw_offset_t die_offset)
10711{
10712 if (die_offset != DW_INVALID_OFFSET)
10713 {
10714 DWARFCompileUnitSP cu_sp;
10715 const DWARFDebugInfoEntry* die = dwarf->DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
10716 return GetClangDeclContextContainingDIE (dwarf, cu_sp.get(), die, NULL);
10717 }
10718 return NULL;
10719}
10720
10721clang::DeclContext *
10722ClangASTContext::GetClangDeclContextForDIEOffset (SymbolFileDWARF *dwarf,
10723 const SymbolContext &sc,
10724 dw_offset_t die_offset)
10725{
10726 if (die_offset != DW_INVALID_OFFSET)
10727 {
10728 DWARFDebugInfo* debug_info = dwarf->DebugInfo();
10729 if (debug_info)
10730 {
10731 DWARFCompileUnitSP cu_sp;
10732 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
10733 if (die)
10734 return GetClangDeclContextForDIE (dwarf, sc, cu_sp.get(), die);
10735 }
10736 }
10737 return NULL;
10738}
10739
10740clang::NamespaceDecl *
10741ClangASTContext::ResolveNamespaceDIE (SymbolFileDWARF *dwarf,
10742 DWARFCompileUnit *dwarf_cu,
10743 const DWARFDebugInfoEntry *die)
10744{
10745 if (die && die->Tag() == DW_TAG_namespace)
10746 {
10747 // See if we already parsed this namespace DIE and associated it with a
10748 // uniqued namespace declaration
Greg Clayton6dc8d582015-08-18 22:32:36 +000010749 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010750 if (namespace_decl)
10751 return namespace_decl;
10752 else
10753 {
10754 const char *namespace_name = die->GetAttributeValueAsString(dwarf, dwarf_cu, DW_AT_name, NULL);
10755 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf, dwarf_cu, die, NULL);
10756 namespace_decl = GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
10757 Log *log = nullptr;// (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
10758 if (log)
10759 {
10760 if (namespace_name)
10761 {
10762 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
10763 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
10764 static_cast<void*>(getASTContext()),
10765 dwarf->MakeUserID(die->GetOffset()),
10766 namespace_name,
10767 static_cast<void*>(namespace_decl),
10768 static_cast<void*>(namespace_decl->getOriginalNamespace()));
10769 }
10770 else
10771 {
10772 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
10773 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
10774 static_cast<void*>(getASTContext()),
10775 dwarf->MakeUserID(die->GetOffset()),
10776 static_cast<void*>(namespace_decl),
10777 static_cast<void*>(namespace_decl->getOriginalNamespace()));
10778 }
10779 }
10780
10781 if (namespace_decl)
Greg Clayton6dc8d582015-08-18 22:32:36 +000010782 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010783 return namespace_decl;
10784 }
10785 }
10786 return NULL;
10787}
10788
10789clang::DeclContext *
10790ClangASTContext::GetClangDeclContextContainingDIE (SymbolFileDWARF *dwarf,
10791 DWARFCompileUnit *cu,
10792 const DWARFDebugInfoEntry *die,
10793 const DWARFDebugInfoEntry **decl_ctx_die_copy)
10794{
Greg Clayton6dc8d582015-08-18 22:32:36 +000010795 if (m_clang_tu_decl == NULL)
10796 m_clang_tu_decl = getASTContext()->getTranslationUnitDecl();
Greg Clayton8b4edba2015-08-14 20:02:05 +000010797
10798 const DWARFDebugInfoEntry *decl_ctx_die = dwarf->GetDeclContextDIEContainingDIE (cu, die);
10799
10800 if (decl_ctx_die_copy)
10801 *decl_ctx_die_copy = decl_ctx_die;
10802
10803 if (decl_ctx_die)
10804 {
10805
Greg Clayton6dc8d582015-08-18 22:32:36 +000010806 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
10807 if (pos != m_die_to_decl_ctx.end())
Greg Clayton8b4edba2015-08-14 20:02:05 +000010808 return pos->second;
10809
10810 switch (decl_ctx_die->Tag())
10811 {
10812 case DW_TAG_compile_unit:
Greg Clayton6dc8d582015-08-18 22:32:36 +000010813 return m_clang_tu_decl;
Greg Clayton8b4edba2015-08-14 20:02:05 +000010814
10815 case DW_TAG_namespace:
10816 return ResolveNamespaceDIE (dwarf, cu, decl_ctx_die);
10817
10818 case DW_TAG_structure_type:
10819 case DW_TAG_union_type:
10820 case DW_TAG_class_type:
10821 {
10822 Type* type = dwarf->ResolveType (cu, decl_ctx_die);
10823 if (type)
10824 {
10825 clang::DeclContext *decl_ctx = GetDeclContextForType(type->GetClangForwardType());
10826 if (decl_ctx)
10827 {
Greg Clayton6dc8d582015-08-18 22:32:36 +000010828 LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010829 if (decl_ctx)
10830 return decl_ctx;
10831 }
10832 }
10833 }
10834 break;
10835
10836 default:
10837 break;
10838 }
10839 }
Greg Clayton6dc8d582015-08-18 22:32:36 +000010840 return m_clang_tu_decl;
Greg Clayton8b4edba2015-08-14 20:02:05 +000010841}
10842
10843
10844
10845TypeSP
10846ClangASTContext::ParseTypeFromDWARF (const SymbolContext& sc,
10847 SymbolFileDWARF *dwarf,
10848 DWARFCompileUnit* dwarf_cu,
10849 const DWARFDebugInfoEntry *die,
10850 Log *log,
10851 bool *type_is_new_ptr)
10852{
10853 TypeSP type_sp;
10854
10855 if (type_is_new_ptr)
10856 *type_is_new_ptr = false;
10857
10858#if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
10859 static DIEStack g_die_stack;
10860 DIEStack::ScopedPopper scoped_die_logger(g_die_stack);
10861#endif
10862
10863 AccessType accessibility = eAccessNone;
10864 if (die != NULL)
10865 {
10866 if (log)
10867 {
10868 const DWARFDebugInfoEntry *context_die;
10869 clang::DeclContext *context = GetClangDeclContextContainingDIE (dwarf, dwarf_cu, die, &context_die);
10870
10871 dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x, decl_ctx = %p (die 0x%8.8x)) %s name = '%s')",
10872 die->GetOffset(),
10873 static_cast<void*>(context),
10874 context_die->GetOffset(),
10875 DW_TAG_value_to_name(die->Tag()),
10876 die->GetName(dwarf, dwarf_cu));
10877
10878#if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
10879 scoped_die_logger.Push (dwarf_cu, die);
10880 g_die_stack.LogDIEs(log, dwarf);
10881#endif
10882 }
10883 //
10884 // Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
10885 // if (log && dwarf_cu)
10886 // {
10887 // StreamString s;
10888 // die->DumpLocation (this, dwarf_cu, s);
10889 // dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
10890 //
10891 // }
10892
10893 Type *type_ptr = dwarf->m_die_to_type.lookup (die);
10894 TypeList* type_list = dwarf->GetTypeList();
10895 if (type_ptr == NULL)
10896 {
10897 if (type_is_new_ptr)
10898 *type_is_new_ptr = true;
10899
10900 const dw_tag_t tag = die->Tag();
10901
10902 bool is_forward_declaration = false;
10903 DWARFDebugInfoEntry::Attributes attributes;
10904 const char *type_name_cstr = NULL;
10905 ConstString type_name_const_str;
10906 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
10907 uint64_t byte_size = 0;
10908 Declaration decl;
10909
10910 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
10911 CompilerType clang_type;
10912 DWARFFormValue form_value;
10913
10914 dw_attr_t attr;
10915
10916 switch (tag)
10917 {
10918 case DW_TAG_base_type:
10919 case DW_TAG_pointer_type:
10920 case DW_TAG_reference_type:
10921 case DW_TAG_rvalue_reference_type:
10922 case DW_TAG_typedef:
10923 case DW_TAG_const_type:
10924 case DW_TAG_restrict_type:
10925 case DW_TAG_volatile_type:
10926 case DW_TAG_unspecified_type:
10927 {
10928 // Set a bit that lets us know that we are currently parsing this
10929 dwarf->m_die_to_type[die] = DIE_IS_BEING_PARSED;
10930
10931 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, NULL, attributes);
10932 uint32_t encoding = 0;
10933 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
10934
10935 if (num_attributes > 0)
10936 {
10937 uint32_t i;
10938 for (i=0; i<num_attributes; ++i)
10939 {
10940 attr = attributes.AttributeAtIndex(i);
10941 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
10942 {
10943 switch (attr)
10944 {
10945 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
10946 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
10947 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
10948 case DW_AT_name:
10949
10950 type_name_cstr = form_value.AsCString(&dwarf->get_debug_str_data());
10951 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
10952 // include the "&"...
10953 if (tag == DW_TAG_reference_type)
10954 {
10955 if (strchr (type_name_cstr, '&') == NULL)
10956 type_name_cstr = NULL;
10957 }
10958 if (type_name_cstr)
10959 type_name_const_str.SetCString(type_name_cstr);
10960 break;
10961 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
10962 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
10963 case DW_AT_type: encoding_uid = form_value.Reference(); break;
10964 default:
10965 case DW_AT_sibling:
10966 break;
10967 }
10968 }
10969 }
10970 }
10971
10972 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\") type => 0x%8.8lx\n", dwarf->MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr, encoding_uid);
10973
10974 switch (tag)
10975 {
10976 default:
10977 break;
10978
10979 case DW_TAG_unspecified_type:
10980 if (strcmp(type_name_cstr, "nullptr_t") == 0 ||
10981 strcmp(type_name_cstr, "decltype(nullptr)") == 0 )
10982 {
10983 resolve_state = Type::eResolveStateFull;
10984 clang_type = GetBasicType(eBasicTypeNullPtr);
10985 break;
10986 }
10987 // Fall through to base type below in case we can handle the type there...
10988
10989 case DW_TAG_base_type:
10990 resolve_state = Type::eResolveStateFull;
10991 clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
10992 encoding,
10993 byte_size * 8);
10994 break;
10995
10996 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
10997 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
10998 case DW_TAG_rvalue_reference_type: encoding_data_type = Type::eEncodingIsRValueReferenceUID; break;
10999 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
11000 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
11001 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
11002 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
11003 }
11004
11005 if (!clang_type && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID) && sc.comp_unit != NULL)
11006 {
11007 bool translation_unit_is_objc = (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus);
11008
11009 if (translation_unit_is_objc)
11010 {
11011 if (type_name_cstr != NULL)
11012 {
11013 static ConstString g_objc_type_name_id("id");
11014 static ConstString g_objc_type_name_Class("Class");
11015 static ConstString g_objc_type_name_selector("SEL");
11016
11017 if (type_name_const_str == g_objc_type_name_id)
11018 {
11019 if (log)
11020 dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.",
11021 die->GetOffset(),
11022 DW_TAG_value_to_name(die->Tag()),
11023 die->GetName(dwarf, dwarf_cu));
11024 clang_type = GetBasicType(eBasicTypeObjCID);
11025 encoding_data_type = Type::eEncodingIsUID;
11026 encoding_uid = LLDB_INVALID_UID;
11027 resolve_state = Type::eResolveStateFull;
11028
11029 }
11030 else if (type_name_const_str == g_objc_type_name_Class)
11031 {
11032 if (log)
11033 dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.",
11034 die->GetOffset(),
11035 DW_TAG_value_to_name(die->Tag()),
11036 die->GetName(dwarf, dwarf_cu));
11037 clang_type = GetBasicType(eBasicTypeObjCClass);
11038 encoding_data_type = Type::eEncodingIsUID;
11039 encoding_uid = LLDB_INVALID_UID;
11040 resolve_state = Type::eResolveStateFull;
11041 }
11042 else if (type_name_const_str == g_objc_type_name_selector)
11043 {
11044 if (log)
11045 dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.",
11046 die->GetOffset(),
11047 DW_TAG_value_to_name(die->Tag()),
11048 die->GetName(dwarf, dwarf_cu));
11049 clang_type = GetBasicType(eBasicTypeObjCSel);
11050 encoding_data_type = Type::eEncodingIsUID;
11051 encoding_uid = LLDB_INVALID_UID;
11052 resolve_state = Type::eResolveStateFull;
11053 }
11054 }
11055 else if (encoding_data_type == Type::eEncodingIsPointerUID && encoding_uid != LLDB_INVALID_UID)
11056 {
11057 // Clang sometimes erroneously emits id as objc_object*. In that case we fix up the type to "id".
11058
11059 DWARFDebugInfoEntry* encoding_die = dwarf_cu->GetDIEPtr(encoding_uid);
11060
11061 if (encoding_die && encoding_die->Tag() == DW_TAG_structure_type)
11062 {
11063 if (const char *struct_name = encoding_die->GetAttributeValueAsString(dwarf, dwarf_cu, DW_AT_name, NULL))
11064 {
11065 if (!strcmp(struct_name, "objc_object"))
11066 {
11067 if (log)
11068 dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is 'objc_object*', which we overrode to 'id'.",
11069 die->GetOffset(),
11070 DW_TAG_value_to_name(die->Tag()),
11071 die->GetName(dwarf, dwarf_cu));
11072 clang_type = GetBasicType(eBasicTypeObjCID);
11073 encoding_data_type = Type::eEncodingIsUID;
11074 encoding_uid = LLDB_INVALID_UID;
11075 resolve_state = Type::eResolveStateFull;
11076 }
11077 }
11078 }
11079 }
11080 }
11081 }
11082
11083 type_sp.reset( new Type (dwarf->MakeUserID(die->GetOffset()),
11084 dwarf,
11085 type_name_const_str,
11086 byte_size,
11087 NULL,
11088 encoding_uid,
11089 encoding_data_type,
11090 &decl,
11091 clang_type,
11092 resolve_state));
11093
11094 dwarf->m_die_to_type[die] = type_sp.get();
11095
11096 // Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
11097 // if (encoding_type != NULL)
11098 // {
11099 // if (encoding_type != DIE_IS_BEING_PARSED)
11100 // type_sp->SetEncodingType(encoding_type);
11101 // else
11102 // m_indirect_fixups.push_back(type_sp.get());
11103 // }
11104 }
11105 break;
11106
11107 case DW_TAG_structure_type:
11108 case DW_TAG_union_type:
11109 case DW_TAG_class_type:
11110 {
11111 // Set a bit that lets us know that we are currently parsing this
11112 dwarf->m_die_to_type[die] = DIE_IS_BEING_PARSED;
11113 bool byte_size_valid = false;
11114
11115 LanguageType class_language = eLanguageTypeUnknown;
11116 bool is_complete_objc_class = false;
11117 //bool struct_is_class = false;
11118 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, NULL, attributes);
11119 if (num_attributes > 0)
11120 {
11121 uint32_t i;
11122 for (i=0; i<num_attributes; ++i)
11123 {
11124 attr = attributes.AttributeAtIndex(i);
11125 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
11126 {
11127 switch (attr)
11128 {
11129 case DW_AT_decl_file:
11130 if (dwarf_cu->DW_AT_decl_file_attributes_are_invalid())
11131 {
11132 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
11133 // point to the compile unit file, so we clear this invalid value
11134 // so that we can still unique types efficiently.
11135 decl.SetFile(FileSpec ("<invalid>", false));
11136 }
11137 else
11138 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
11139 break;
11140
11141 case DW_AT_decl_line:
11142 decl.SetLine(form_value.Unsigned());
11143 break;
11144
11145 case DW_AT_decl_column:
11146 decl.SetColumn(form_value.Unsigned());
11147 break;
11148
11149 case DW_AT_name:
11150 type_name_cstr = form_value.AsCString(&dwarf->get_debug_str_data());
11151 type_name_const_str.SetCString(type_name_cstr);
11152 break;
11153
11154 case DW_AT_byte_size:
11155 byte_size = form_value.Unsigned();
11156 byte_size_valid = true;
11157 break;
11158
11159 case DW_AT_accessibility:
11160 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
11161 break;
11162
11163 case DW_AT_declaration:
11164 is_forward_declaration = form_value.Boolean();
11165 break;
11166
11167 case DW_AT_APPLE_runtime_class:
11168 class_language = (LanguageType)form_value.Signed();
11169 break;
11170
11171 case DW_AT_APPLE_objc_complete_type:
11172 is_complete_objc_class = form_value.Signed();
11173 break;
11174
11175 case DW_AT_allocated:
11176 case DW_AT_associated:
11177 case DW_AT_data_location:
11178 case DW_AT_description:
11179 case DW_AT_start_scope:
11180 case DW_AT_visibility:
11181 default:
11182 case DW_AT_sibling:
11183 break;
11184 }
11185 }
11186 }
11187 }
11188
11189 // UniqueDWARFASTType is large, so don't create a local variables on the
11190 // stack, put it on the heap. This function is often called recursively
11191 // and clang isn't good and sharing the stack space for variables in different blocks.
11192 std::unique_ptr<UniqueDWARFASTType> unique_ast_entry_ap(new UniqueDWARFASTType());
11193
11194 // Only try and unique the type if it has a name.
11195 if (type_name_const_str &&
11196 dwarf->GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
11197 dwarf,
11198 dwarf_cu,
11199 die,
11200 decl,
11201 byte_size_valid ? byte_size : -1,
11202 *unique_ast_entry_ap))
11203 {
11204 // We have already parsed this type or from another
11205 // compile unit. GCC loves to use the "one definition
11206 // rule" which can result in multiple definitions
11207 // of the same class over and over in each compile
11208 // unit.
11209 type_sp = unique_ast_entry_ap->m_type_sp;
11210 if (type_sp)
11211 {
11212 dwarf->m_die_to_type[die] = type_sp.get();
11213 return type_sp;
11214 }
11215 }
11216
11217 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", dwarf->MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
11218
11219 int tag_decl_kind = -1;
11220 AccessType default_accessibility = eAccessNone;
11221 if (tag == DW_TAG_structure_type)
11222 {
11223 tag_decl_kind = clang::TTK_Struct;
11224 default_accessibility = eAccessPublic;
11225 }
11226 else if (tag == DW_TAG_union_type)
11227 {
11228 tag_decl_kind = clang::TTK_Union;
11229 default_accessibility = eAccessPublic;
11230 }
11231 else if (tag == DW_TAG_class_type)
11232 {
11233 tag_decl_kind = clang::TTK_Class;
11234 default_accessibility = eAccessPrivate;
11235 }
11236
11237 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
11238 die->HasChildren() == false &&
11239 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
11240 {
11241 // Work around an issue with clang at the moment where
11242 // forward declarations for objective C classes are emitted
11243 // as:
11244 // DW_TAG_structure_type [2]
11245 // DW_AT_name( "ForwardObjcClass" )
11246 // DW_AT_byte_size( 0x00 )
11247 // DW_AT_decl_file( "..." )
11248 // DW_AT_decl_line( 1 )
11249 //
11250 // Note that there is no DW_AT_declaration and there are
11251 // no children, and the byte size is zero.
11252 is_forward_declaration = true;
11253 }
11254
11255 if (class_language == eLanguageTypeObjC ||
11256 class_language == eLanguageTypeObjC_plus_plus)
11257 {
11258 if (!is_complete_objc_class && dwarf->Supports_DW_AT_APPLE_objc_complete_type(dwarf_cu))
11259 {
11260 // We have a valid eSymbolTypeObjCClass class symbol whose
11261 // name matches the current objective C class that we
11262 // are trying to find and this DIE isn't the complete
11263 // definition (we checked is_complete_objc_class above and
11264 // know it is false), so the real definition is in here somewhere
11265 type_sp = dwarf->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
11266
11267 if (!type_sp)
11268 {
11269 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
11270 if (debug_map_symfile)
11271 {
11272 // We weren't able to find a full declaration in
11273 // this DWARF, see if we have a declaration anywhere
11274 // else...
11275 type_sp = debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
11276 }
11277 }
11278
11279 if (type_sp)
11280 {
11281 if (log)
11282 {
11283 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
11284 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8" PRIx64,
11285 static_cast<void*>(this),
11286 die->GetOffset(),
11287 DW_TAG_value_to_name(tag),
11288 type_name_cstr,
11289 type_sp->GetID());
11290 }
11291
11292 // We found a real definition for this type elsewhere
11293 // so lets use it and cache the fact that we found
11294 // a complete type for this die
11295 dwarf->m_die_to_type[die] = type_sp.get();
11296 return type_sp;
11297 }
11298 }
11299 }
11300
11301
11302 if (is_forward_declaration)
11303 {
11304 // We have a forward declaration to a type and we need
11305 // to try and find a full declaration. We look in the
11306 // current type index just in case we have a forward
11307 // declaration followed by an actual declarations in the
11308 // DWARF. If this fails, we need to look elsewhere...
11309 if (log)
11310 {
11311 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
11312 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
11313 static_cast<void*>(this),
11314 die->GetOffset(),
11315 DW_TAG_value_to_name(tag),
11316 type_name_cstr);
11317 }
11318
11319 DWARFDeclContext die_decl_ctx;
11320 die->GetDWARFDeclContext(dwarf, dwarf_cu, die_decl_ctx);
11321
11322 //type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
11323 type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
11324
11325 if (!type_sp)
11326 {
11327 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
11328 if (debug_map_symfile)
11329 {
11330 // We weren't able to find a full declaration in
11331 // this DWARF, see if we have a declaration anywhere
11332 // else...
11333 type_sp = debug_map_symfile->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
11334 }
11335 }
11336
11337 if (type_sp)
11338 {
11339 if (log)
11340 {
11341 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
11342 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8" PRIx64,
11343 static_cast<void*>(this),
11344 die->GetOffset(),
11345 DW_TAG_value_to_name(tag),
11346 type_name_cstr,
11347 type_sp->GetID());
11348 }
11349
11350 // We found a real definition for this type elsewhere
11351 // so lets use it and cache the fact that we found
11352 // a complete type for this die
11353 dwarf->m_die_to_type[die] = type_sp.get();
11354 return type_sp;
11355 }
11356 }
11357 assert (tag_decl_kind != -1);
11358 bool clang_type_was_created = false;
11359 clang_type.SetClangType(this, dwarf->m_forward_decl_die_to_clang_type.lookup (die));
11360 if (!clang_type)
11361 {
11362 const DWARFDebugInfoEntry *decl_ctx_die;
11363
11364 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf, dwarf_cu, die, &decl_ctx_die);
11365 if (accessibility == eAccessNone && decl_ctx)
11366 {
11367 // Check the decl context that contains this class/struct/union.
11368 // If it is a class we must give it an accessibility.
11369 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
11370 if (DeclKindIsCXXClass (containing_decl_kind))
11371 accessibility = default_accessibility;
11372 }
11373
11374 ClangASTMetadata metadata;
11375 metadata.SetUserID(dwarf->MakeUserID(die->GetOffset()));
11376 metadata.SetIsDynamicCXXType(dwarf->ClassOrStructIsVirtual (dwarf_cu, die));
11377
11378 if (type_name_cstr && strchr (type_name_cstr, '<'))
11379 {
11380 ClangASTContext::TemplateParameterInfos template_param_infos;
11381 if (ParseTemplateParameterInfos (dwarf, dwarf_cu, die, template_param_infos))
11382 {
11383 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (dwarf,
11384 decl_ctx,
11385 accessibility,
11386 type_name_cstr,
11387 tag_decl_kind,
11388 template_param_infos);
11389
11390 clang::ClassTemplateSpecializationDecl *class_specialization_decl = CreateClassTemplateSpecializationDecl (decl_ctx,
11391 class_template_decl,
11392 tag_decl_kind,
11393 template_param_infos);
11394 clang_type = CreateClassTemplateSpecializationType (class_specialization_decl);
11395 clang_type_was_created = true;
11396
11397 SetMetadata (class_template_decl, metadata);
11398 SetMetadata (class_specialization_decl, metadata);
11399 }
11400 }
11401
11402 if (!clang_type_was_created)
11403 {
11404 clang_type_was_created = true;
11405 clang_type = CreateRecordType (decl_ctx,
11406 accessibility,
11407 type_name_cstr,
11408 tag_decl_kind,
11409 class_language,
11410 &metadata);
11411 }
11412 }
11413
11414 // Store a forward declaration to this class type in case any
11415 // parameters in any class methods need it for the clang
11416 // types for function prototypes.
Greg Clayton6dc8d582015-08-18 22:32:36 +000011417 LinkDeclContextToDIE(GetDeclContextForType(clang_type), die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000011418 type_sp.reset (new Type (dwarf->MakeUserID(die->GetOffset()),
11419 dwarf,
11420 type_name_const_str,
11421 byte_size,
11422 NULL,
11423 LLDB_INVALID_UID,
11424 Type::eEncodingIsUID,
11425 &decl,
11426 clang_type,
11427 Type::eResolveStateForward));
11428
11429 type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
11430
11431
11432 // Add our type to the unique type map so we don't
11433 // end up creating many copies of the same type over
11434 // and over in the ASTContext for our module
11435 unique_ast_entry_ap->m_type_sp = type_sp;
11436 unique_ast_entry_ap->m_symfile = dwarf;
11437 unique_ast_entry_ap->m_cu = dwarf_cu;
11438 unique_ast_entry_ap->m_die = die;
11439 unique_ast_entry_ap->m_declaration = decl;
11440 unique_ast_entry_ap->m_byte_size = byte_size;
11441 dwarf->GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
11442 *unique_ast_entry_ap);
11443
11444 if (is_forward_declaration && die->HasChildren())
11445 {
11446 // Check to see if the DIE actually has a definition, some version of GCC will
11447 // emit DIEs with DW_AT_declaration set to true, but yet still have subprogram,
11448 // members, or inheritance, so we can't trust it
11449 const DWARFDebugInfoEntry *child_die = die->GetFirstChild();
11450 while (child_die)
11451 {
11452 switch (child_die->Tag())
11453 {
11454 case DW_TAG_inheritance:
11455 case DW_TAG_subprogram:
11456 case DW_TAG_member:
11457 case DW_TAG_APPLE_property:
11458 case DW_TAG_class_type:
11459 case DW_TAG_structure_type:
11460 case DW_TAG_enumeration_type:
11461 case DW_TAG_typedef:
11462 case DW_TAG_union_type:
11463 child_die = NULL;
11464 is_forward_declaration = false;
11465 break;
11466 default:
11467 child_die = child_die->GetSibling();
11468 break;
11469 }
11470 }
11471 }
11472
11473 if (!is_forward_declaration)
11474 {
11475 // Always start the definition for a class type so that
11476 // if the class has child classes or types that require
11477 // the class to be created for use as their decl contexts
11478 // the class will be ready to accept these child definitions.
11479 if (die->HasChildren() == false)
11480 {
11481 // No children for this struct/union/class, lets finish it
11482 ClangASTContext::StartTagDeclarationDefinition (clang_type);
11483 ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
11484
11485 if (tag == DW_TAG_structure_type) // this only applies in C
11486 {
11487 clang::RecordDecl *record_decl = ClangASTContext::GetAsRecordDecl(clang_type);
11488
11489 if (record_decl)
11490 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, LayoutInfo()));
11491 }
11492 }
11493 else if (clang_type_was_created)
11494 {
11495 // Start the definition if the class is not objective C since
11496 // the underlying decls respond to isCompleteDefinition(). Objective
11497 // C decls don't respond to isCompleteDefinition() so we can't
11498 // start the declaration definition right away. For C++ class/union/structs
11499 // we want to start the definition in case the class is needed as the
11500 // declaration context for a contained class or type without the need
11501 // to complete that type..
11502
11503 if (class_language != eLanguageTypeObjC &&
11504 class_language != eLanguageTypeObjC_plus_plus)
11505 ClangASTContext::StartTagDeclarationDefinition (clang_type);
11506
11507 // Leave this as a forward declaration until we need
11508 // to know the details of the type. lldb_private::Type
11509 // will automatically call the SymbolFile virtual function
Greg Clayton6dc8d582015-08-18 22:32:36 +000011510 // "SymbolFileDWARF::CompleteType(Type *)"
Greg Clayton8b4edba2015-08-14 20:02:05 +000011511 // When the definition needs to be defined.
11512 dwarf->m_forward_decl_die_to_clang_type[die] = clang_type.GetOpaqueQualType();
11513 dwarf->m_forward_decl_clang_type_to_die[ClangASTContext::RemoveFastQualifiers(clang_type).GetOpaqueQualType()] = die;
11514 SetHasExternalStorage (clang_type.GetOpaqueQualType(), true);
11515 }
11516 }
11517
11518 }
11519 break;
11520
11521 case DW_TAG_enumeration_type:
11522 {
11523 // Set a bit that lets us know that we are currently parsing this
11524 dwarf->m_die_to_type[die] = DIE_IS_BEING_PARSED;
11525
11526 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
11527
11528 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, NULL, attributes);
11529 if (num_attributes > 0)
11530 {
11531 uint32_t i;
11532
11533 for (i=0; i<num_attributes; ++i)
11534 {
11535 attr = attributes.AttributeAtIndex(i);
11536 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
11537 {
11538 switch (attr)
11539 {
11540 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
11541 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
11542 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
11543 case DW_AT_name:
11544 type_name_cstr = form_value.AsCString(&dwarf->get_debug_str_data());
11545 type_name_const_str.SetCString(type_name_cstr);
11546 break;
11547 case DW_AT_type: encoding_uid = form_value.Reference(); break;
11548 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
11549 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
11550 case DW_AT_declaration: break; //is_forward_declaration = form_value.Boolean(); break;
11551 case DW_AT_allocated:
11552 case DW_AT_associated:
11553 case DW_AT_bit_stride:
11554 case DW_AT_byte_stride:
11555 case DW_AT_data_location:
11556 case DW_AT_description:
11557 case DW_AT_start_scope:
11558 case DW_AT_visibility:
11559 case DW_AT_specification:
11560 case DW_AT_abstract_origin:
11561 case DW_AT_sibling:
11562 break;
11563 }
11564 }
11565 }
11566
11567 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", dwarf->MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
11568
11569 CompilerType enumerator_clang_type;
11570 clang_type.SetClangType (this, dwarf->m_forward_decl_die_to_clang_type.lookup (die));
11571 if (!clang_type)
11572 {
11573 if (encoding_uid != DW_INVALID_OFFSET)
11574 {
11575 Type *enumerator_type = dwarf->ResolveTypeUID(encoding_uid);
11576 if (enumerator_type)
11577 enumerator_clang_type = enumerator_type->GetClangFullType();
11578 }
11579
11580 if (!enumerator_clang_type)
11581 enumerator_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
11582 DW_ATE_signed,
11583 byte_size * 8);
11584
11585 clang_type = CreateEnumerationType (type_name_cstr,
11586 GetClangDeclContextContainingDIE (dwarf, dwarf_cu, die, NULL),
11587 decl,
11588 enumerator_clang_type);
11589 }
11590 else
11591 {
11592 enumerator_clang_type = GetEnumerationIntegerType (clang_type.GetOpaqueQualType());
11593 }
11594
Greg Clayton6dc8d582015-08-18 22:32:36 +000011595 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000011596
11597 type_sp.reset( new Type (dwarf->MakeUserID(die->GetOffset()),
11598 dwarf,
11599 type_name_const_str,
11600 byte_size,
11601 NULL,
11602 encoding_uid,
11603 Type::eEncodingIsUID,
11604 &decl,
11605 clang_type,
11606 Type::eResolveStateForward));
11607
11608 ClangASTContext::StartTagDeclarationDefinition (clang_type);
11609 if (die->HasChildren())
11610 {
11611 SymbolContext cu_sc(dwarf->GetCompUnitForDWARFCompUnit(dwarf_cu));
11612 bool is_signed = false;
11613 enumerator_clang_type.IsIntegerType(is_signed);
11614 ParseChildEnumerators(cu_sc, clang_type, is_signed, type_sp->GetByteSize(), dwarf, dwarf_cu, die);
11615 }
11616 ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
11617 }
11618 }
11619 break;
11620
11621 case DW_TAG_inlined_subroutine:
11622 case DW_TAG_subprogram:
11623 case DW_TAG_subroutine_type:
11624 {
11625 // Set a bit that lets us know that we are currently parsing this
11626 dwarf->m_die_to_type[die] = DIE_IS_BEING_PARSED;
11627
11628 //const char *mangled = NULL;
11629 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
11630 bool is_variadic = false;
11631 bool is_inline = false;
11632 bool is_static = false;
11633 bool is_virtual = false;
11634 bool is_explicit = false;
11635 bool is_artificial = false;
11636 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
11637 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
11638 dw_offset_t object_pointer_die_offset = DW_INVALID_OFFSET;
11639
11640 unsigned type_quals = 0;
11641 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
11642
11643
11644 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, NULL, attributes);
11645 if (num_attributes > 0)
11646 {
11647 uint32_t i;
11648 for (i=0; i<num_attributes; ++i)
11649 {
11650 attr = attributes.AttributeAtIndex(i);
11651 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
11652 {
11653 switch (attr)
11654 {
11655 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
11656 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
11657 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
11658 case DW_AT_name:
11659 type_name_cstr = form_value.AsCString(&dwarf->get_debug_str_data());
11660 type_name_const_str.SetCString(type_name_cstr);
11661 break;
11662
11663 case DW_AT_linkage_name:
11664 case DW_AT_MIPS_linkage_name: break; // mangled = form_value.AsCString(&dwarf->get_debug_str_data()); break;
11665 case DW_AT_type: type_die_offset = form_value.Reference(); break;
11666 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
11667 case DW_AT_declaration: break; // is_forward_declaration = form_value.Boolean(); break;
11668 case DW_AT_inline: is_inline = form_value.Boolean(); break;
11669 case DW_AT_virtuality: is_virtual = form_value.Boolean(); break;
11670 case DW_AT_explicit: is_explicit = form_value.Boolean(); break;
11671 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
11672
11673
11674 case DW_AT_external:
11675 if (form_value.Unsigned())
11676 {
11677 if (storage == clang::SC_None)
11678 storage = clang::SC_Extern;
11679 else
11680 storage = clang::SC_PrivateExtern;
11681 }
11682 break;
11683
11684 case DW_AT_specification:
11685 specification_die_offset = form_value.Reference();
11686 break;
11687
11688 case DW_AT_abstract_origin:
11689 abstract_origin_die_offset = form_value.Reference();
11690 break;
11691
11692 case DW_AT_object_pointer:
11693 object_pointer_die_offset = form_value.Reference();
11694 break;
11695
11696 case DW_AT_allocated:
11697 case DW_AT_associated:
11698 case DW_AT_address_class:
11699 case DW_AT_calling_convention:
11700 case DW_AT_data_location:
11701 case DW_AT_elemental:
11702 case DW_AT_entry_pc:
11703 case DW_AT_frame_base:
11704 case DW_AT_high_pc:
11705 case DW_AT_low_pc:
11706 case DW_AT_prototyped:
11707 case DW_AT_pure:
11708 case DW_AT_ranges:
11709 case DW_AT_recursive:
11710 case DW_AT_return_addr:
11711 case DW_AT_segment:
11712 case DW_AT_start_scope:
11713 case DW_AT_static_link:
11714 case DW_AT_trampoline:
11715 case DW_AT_visibility:
11716 case DW_AT_vtable_elem_location:
11717 case DW_AT_description:
11718 case DW_AT_sibling:
11719 break;
11720 }
11721 }
11722 }
11723 }
11724
11725 std::string object_pointer_name;
11726 if (object_pointer_die_offset != DW_INVALID_OFFSET)
11727 {
11728 // Get the name from the object pointer die
11729 StreamString s;
11730 if (DWARFDebugInfoEntry::GetName (dwarf, dwarf_cu, object_pointer_die_offset, s))
11731 {
11732 object_pointer_name.assign(s.GetData());
11733 }
11734 }
11735
11736 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", dwarf->MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
11737
11738 CompilerType return_clang_type;
11739 Type *func_type = NULL;
11740
11741 if (type_die_offset != DW_INVALID_OFFSET)
11742 func_type = dwarf->ResolveTypeUID(type_die_offset);
11743
11744 if (func_type)
11745 return_clang_type = func_type->GetClangForwardType();
11746 else
11747 return_clang_type = GetBasicType(eBasicTypeVoid);
11748
11749
11750 std::vector<CompilerType> function_param_types;
11751 std::vector<clang::ParmVarDecl*> function_param_decls;
11752
11753 // Parse the function children for the parameters
11754
11755 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
11756 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf, dwarf_cu, die, &decl_ctx_die);
11757 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
11758
11759 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
11760 // Start off static. This will be set to false in ParseChildParameters(...)
11761 // if we find a "this" parameters as the first parameter
11762 if (is_cxx_method)
11763 is_static = true;
11764
11765 if (die->HasChildren())
11766 {
11767 bool skip_artificial = true;
11768 ParseChildParameters (sc,
11769 containing_decl_ctx,
11770 dwarf,
11771 dwarf_cu,
11772 die,
11773 skip_artificial,
11774 is_static,
11775 is_variadic,
11776 function_param_types,
11777 function_param_decls,
11778 type_quals);
11779 }
11780
11781 // clang_type will get the function prototype clang type after this call
11782 clang_type = CreateFunctionType (return_clang_type,
11783 function_param_types.data(),
11784 function_param_types.size(),
11785 is_variadic,
11786 type_quals);
11787
11788 bool ignore_containing_context = false;
11789
11790 if (type_name_cstr)
11791 {
11792 bool type_handled = false;
11793 if (tag == DW_TAG_subprogram)
11794 {
11795 ObjCLanguageRuntime::MethodName objc_method (type_name_cstr, true);
11796 if (objc_method.IsValid(true))
11797 {
11798 CompilerType class_opaque_type;
11799 ConstString class_name(objc_method.GetClassName());
11800 if (class_name)
11801 {
11802 TypeSP complete_objc_class_type_sp (dwarf->FindCompleteObjCDefinitionTypeForDIE (NULL, class_name, false));
11803
11804 if (complete_objc_class_type_sp)
11805 {
11806 CompilerType type_clang_forward_type = complete_objc_class_type_sp->GetClangForwardType();
11807 if (ClangASTContext::IsObjCObjectOrInterfaceType(type_clang_forward_type))
11808 class_opaque_type = type_clang_forward_type;
11809 }
11810 }
11811
11812 if (class_opaque_type)
11813 {
11814 // If accessibility isn't set to anything valid, assume public for
11815 // now...
11816 if (accessibility == eAccessNone)
11817 accessibility = eAccessPublic;
11818
11819 clang::ObjCMethodDecl *objc_method_decl = AddMethodToObjCObjectType (class_opaque_type,
11820 type_name_cstr,
11821 clang_type,
11822 accessibility,
11823 is_artificial);
11824 type_handled = objc_method_decl != NULL;
11825 if (type_handled)
11826 {
Greg Clayton6dc8d582015-08-18 22:32:36 +000011827 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000011828 SetMetadataAsUserID (objc_method_decl, dwarf->MakeUserID(die->GetOffset()));
11829 }
11830 else
11831 {
11832 dwarf->GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: invalid Objective-C method 0x%4.4x (%s), please file a bug and attach the file at the start of this error message",
11833 die->GetOffset(),
11834 tag,
11835 DW_TAG_value_to_name(tag));
11836 }
11837 }
11838 }
11839 else if (is_cxx_method)
11840 {
11841 // Look at the parent of this DIE and see if is is
11842 // a class or struct and see if this is actually a
11843 // C++ method
11844 Type *class_type = dwarf->ResolveType (dwarf_cu, decl_ctx_die);
11845 if (class_type)
11846 {
11847 if (class_type->GetID() != dwarf->MakeUserID(decl_ctx_die->GetOffset()))
11848 {
11849 // We uniqued the parent class of this function to another class
11850 // so we now need to associate all dies under "decl_ctx_die" to
11851 // DIEs in the DIE for "class_type"...
11852 SymbolFileDWARF *class_symfile = NULL;
11853 DWARFCompileUnitSP class_type_cu_sp;
11854 const DWARFDebugInfoEntry *class_type_die = NULL;
11855
11856 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
11857 if (debug_map_symfile)
11858 {
11859 class_symfile = debug_map_symfile->GetSymbolFileByOSOIndex(SymbolFileDWARFDebugMap::GetOSOIndexFromUserID(class_type->GetID()));
11860 class_type_die = class_symfile->DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp);
11861 }
11862 else
11863 {
11864 class_symfile = dwarf;
11865 class_type_die = dwarf->DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp);
11866 }
11867 if (class_type_die)
11868 {
11869 DWARFDIECollection failures;
11870
11871 CopyUniqueClassMethodTypes (dwarf,
11872 class_symfile,
11873 class_type,
11874 class_type_cu_sp.get(),
11875 class_type_die,
11876 dwarf_cu,
11877 decl_ctx_die,
11878 failures);
11879
11880 // FIXME do something with these failures that's smarter than
11881 // just dropping them on the ground. Unfortunately classes don't
11882 // like having stuff added to them after their definitions are
11883 // complete...
11884
11885 type_ptr = dwarf->m_die_to_type[die];
11886 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
11887 {
11888 type_sp = type_ptr->shared_from_this();
11889 break;
11890 }
11891 }
11892 }
11893
11894 if (specification_die_offset != DW_INVALID_OFFSET)
11895 {
11896 // We have a specification which we are going to base our function
11897 // prototype off of, so we need this type to be completed so that the
11898 // m_die_to_decl_ctx for the method in the specification has a valid
11899 // clang decl context.
11900 class_type->GetClangForwardType();
11901 // If we have a specification, then the function type should have been
11902 // made with the specification and not with this die.
11903 DWARFCompileUnitSP spec_cu_sp;
11904 const DWARFDebugInfoEntry* spec_die = dwarf->DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
11905 clang::DeclContext *spec_clang_decl_ctx = GetClangDeclContextForDIE (dwarf, sc, dwarf_cu, spec_die);
11906 if (spec_clang_decl_ctx)
11907 {
Greg Clayton6dc8d582015-08-18 22:32:36 +000011908 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000011909 }
11910 else
11911 {
11912 dwarf->GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_specification(0x%8.8x) has no decl\n",
11913 dwarf->MakeUserID(die->GetOffset()),
11914 specification_die_offset);
11915 }
11916 type_handled = true;
11917 }
11918 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
11919 {
11920 // We have a specification which we are going to base our function
11921 // prototype off of, so we need this type to be completed so that the
11922 // m_die_to_decl_ctx for the method in the abstract origin has a valid
11923 // clang decl context.
11924 class_type->GetClangForwardType();
11925
11926 DWARFCompileUnitSP abs_cu_sp;
11927 const DWARFDebugInfoEntry* abs_die = dwarf->DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
11928 clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE (dwarf, sc, dwarf_cu, abs_die);
11929 if (abs_clang_decl_ctx)
11930 {
Greg Clayton6dc8d582015-08-18 22:32:36 +000011931 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000011932 }
11933 else
11934 {
11935 dwarf->GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_abstract_origin(0x%8.8x) has no decl\n",
11936 dwarf->MakeUserID(die->GetOffset()),
11937 abstract_origin_die_offset);
11938 }
11939 type_handled = true;
11940 }
11941 else
11942 {
11943 CompilerType class_opaque_type = class_type->GetClangForwardType();
11944 if (ClangASTContext::IsCXXClassType(class_opaque_type))
11945 {
11946 if (class_opaque_type.IsBeingDefined ())
11947 {
11948 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
11949 // in the DWARF for C++ methods... Default to public for now...
11950 if (accessibility == eAccessNone)
11951 accessibility = eAccessPublic;
11952
11953 if (!is_static && !die->HasChildren())
11954 {
11955 // We have a C++ member function with no children (this pointer!)
11956 // and clang will get mad if we try and make a function that isn't
11957 // well formed in the DWARF, so we will just skip it...
11958 type_handled = true;
11959 }
11960 else
11961 {
11962 clang::CXXMethodDecl *cxx_method_decl;
11963 // REMOVE THE CRASH DESCRIPTION BELOW
11964 Host::SetCrashDescriptionWithFormat ("SymbolFileDWARF::ParseType() is adding a method %s to class %s in DIE 0x%8.8" PRIx64 " from %s",
11965 type_name_cstr,
11966 class_type->GetName().GetCString(),
11967 dwarf->MakeUserID(die->GetOffset()),
11968 dwarf->GetObjectFile()->GetFileSpec().GetPath().c_str());
11969
11970 const bool is_attr_used = false;
11971
11972 cxx_method_decl = AddMethodToCXXRecordType (class_opaque_type.GetOpaqueQualType(),
11973 type_name_cstr,
11974 clang_type,
11975 accessibility,
11976 is_virtual,
11977 is_static,
11978 is_inline,
11979 is_explicit,
11980 is_attr_used,
11981 is_artificial);
11982
11983 type_handled = cxx_method_decl != NULL;
11984
11985 if (type_handled)
11986 {
Greg Clayton6dc8d582015-08-18 22:32:36 +000011987 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000011988
11989 Host::SetCrashDescription (NULL);
11990
11991
11992 ClangASTMetadata metadata;
11993 metadata.SetUserID(dwarf->MakeUserID(die->GetOffset()));
11994
11995 if (!object_pointer_name.empty())
11996 {
11997 metadata.SetObjectPtrName(object_pointer_name.c_str());
11998 if (log)
11999 log->Printf ("Setting object pointer name: %s on method object %p.\n",
12000 object_pointer_name.c_str(),
12001 static_cast<void*>(cxx_method_decl));
12002 }
12003 SetMetadata (cxx_method_decl, metadata);
12004 }
12005 else
12006 {
12007 ignore_containing_context = true;
12008 }
12009 }
12010 }
12011 else
12012 {
12013 // We were asked to parse the type for a method in a class, yet the
12014 // class hasn't been asked to complete itself through the
12015 // clang::ExternalASTSource protocol, so we need to just have the
12016 // class complete itself and do things the right way, then our
12017 // DIE should then have an entry in the dwarf->m_die_to_type map. First
12018 // we need to modify the dwarf->m_die_to_type so it doesn't think we are
12019 // trying to parse this DIE anymore...
12020 dwarf->m_die_to_type[die] = NULL;
12021
12022 // Now we get the full type to force our class type to complete itself
12023 // using the clang::ExternalASTSource protocol which will parse all
12024 // base classes and all methods (including the method for this DIE).
12025 class_type->GetClangFullType();
12026
12027 // The type for this DIE should have been filled in the function call above
12028 type_ptr = dwarf->m_die_to_type[die];
12029 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
12030 {
12031 type_sp = type_ptr->shared_from_this();
12032 break;
12033 }
12034
12035 // FIXME This is fixing some even uglier behavior but we really need to
12036 // uniq the methods of each class as well as the class itself.
12037 // <rdar://problem/11240464>
12038 type_handled = true;
12039 }
12040 }
12041 }
12042 }
12043 }
12044 }
12045
12046 if (!type_handled)
12047 {
12048 // We just have a function that isn't part of a class
12049 clang::FunctionDecl *function_decl = CreateFunctionDeclaration (ignore_containing_context ? GetTranslationUnitDecl() : containing_decl_ctx,
12050 type_name_cstr,
12051 clang_type,
12052 storage,
12053 is_inline);
12054
12055 // if (template_param_infos.GetSize() > 0)
12056 // {
12057 // clang::FunctionTemplateDecl *func_template_decl = CreateFunctionTemplateDecl (containing_decl_ctx,
12058 // function_decl,
12059 // type_name_cstr,
12060 // template_param_infos);
12061 //
12062 // CreateFunctionTemplateSpecializationInfo (function_decl,
12063 // func_template_decl,
12064 // template_param_infos);
12065 // }
12066 // Add the decl to our DIE to decl context map
12067 assert (function_decl);
Greg Clayton6dc8d582015-08-18 22:32:36 +000012068 LinkDeclContextToDIE(function_decl, die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000012069 if (!function_param_decls.empty())
12070 SetFunctionParameters (function_decl,
12071 &function_param_decls.front(),
12072 function_param_decls.size());
12073
12074 ClangASTMetadata metadata;
12075 metadata.SetUserID(dwarf->MakeUserID(die->GetOffset()));
12076
12077 if (!object_pointer_name.empty())
12078 {
12079 metadata.SetObjectPtrName(object_pointer_name.c_str());
12080 if (log)
12081 log->Printf ("Setting object pointer name: %s on function object %p.",
12082 object_pointer_name.c_str(),
12083 static_cast<void*>(function_decl));
12084 }
12085 SetMetadata (function_decl, metadata);
12086 }
12087 }
12088 type_sp.reset( new Type (dwarf->MakeUserID(die->GetOffset()),
12089 dwarf,
12090 type_name_const_str,
12091 0,
12092 NULL,
12093 LLDB_INVALID_UID,
12094 Type::eEncodingIsUID,
12095 &decl,
12096 clang_type,
12097 Type::eResolveStateFull));
12098 assert(type_sp.get());
12099 }
12100 break;
12101
12102 case DW_TAG_array_type:
12103 {
12104 // Set a bit that lets us know that we are currently parsing this
12105 dwarf->m_die_to_type[die] = DIE_IS_BEING_PARSED;
12106
12107 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
12108 int64_t first_index = 0;
12109 uint32_t byte_stride = 0;
12110 uint32_t bit_stride = 0;
12111 bool is_vector = false;
12112 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, NULL, attributes);
12113
12114 if (num_attributes > 0)
12115 {
12116 uint32_t i;
12117 for (i=0; i<num_attributes; ++i)
12118 {
12119 attr = attributes.AttributeAtIndex(i);
12120 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
12121 {
12122 switch (attr)
12123 {
12124 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
12125 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
12126 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
12127 case DW_AT_name:
12128 type_name_cstr = form_value.AsCString(&dwarf->get_debug_str_data());
12129 type_name_const_str.SetCString(type_name_cstr);
12130 break;
12131
12132 case DW_AT_type: type_die_offset = form_value.Reference(); break;
12133 case DW_AT_byte_size: break; // byte_size = form_value.Unsigned(); break;
12134 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
12135 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
12136 case DW_AT_GNU_vector: is_vector = form_value.Boolean(); break;
12137 case DW_AT_accessibility: break; // accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
12138 case DW_AT_declaration: break; // is_forward_declaration = form_value.Boolean(); break;
12139 case DW_AT_allocated:
12140 case DW_AT_associated:
12141 case DW_AT_data_location:
12142 case DW_AT_description:
12143 case DW_AT_ordering:
12144 case DW_AT_start_scope:
12145 case DW_AT_visibility:
12146 case DW_AT_specification:
12147 case DW_AT_abstract_origin:
12148 case DW_AT_sibling:
12149 break;
12150 }
12151 }
12152 }
12153
12154 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", dwarf->MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
12155
12156 Type *element_type = dwarf->ResolveTypeUID(type_die_offset);
12157
12158 if (element_type)
12159 {
12160 std::vector<uint64_t> element_orders;
12161 ParseChildArrayInfo(sc, dwarf, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
12162 if (byte_stride == 0 && bit_stride == 0)
12163 byte_stride = element_type->GetByteSize();
12164 CompilerType array_element_type = element_type->GetClangForwardType();
12165 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
12166 if (element_orders.size() > 0)
12167 {
12168 uint64_t num_elements = 0;
12169 std::vector<uint64_t>::const_reverse_iterator pos;
12170 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
12171 for (pos = element_orders.rbegin(); pos != end; ++pos)
12172 {
12173 num_elements = *pos;
12174 clang_type = CreateArrayType (array_element_type,
12175 num_elements,
12176 is_vector);
12177 array_element_type = clang_type;
12178 array_element_bit_stride = num_elements ?
12179 array_element_bit_stride * num_elements :
12180 array_element_bit_stride;
12181 }
12182 }
12183 else
12184 {
12185 clang_type = CreateArrayType (array_element_type, 0, is_vector);
12186 }
12187 ConstString empty_name;
12188 type_sp.reset( new Type (dwarf->MakeUserID(die->GetOffset()),
12189 dwarf,
12190 empty_name,
12191 array_element_bit_stride / 8,
12192 NULL,
12193 type_die_offset,
12194 Type::eEncodingIsUID,
12195 &decl,
12196 clang_type,
12197 Type::eResolveStateFull));
12198 type_sp->SetEncodingType (element_type);
12199 }
12200 }
12201 }
12202 break;
12203
12204 case DW_TAG_ptr_to_member_type:
12205 {
12206 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
12207 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
12208
12209 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, NULL, attributes);
12210
12211 if (num_attributes > 0) {
12212 uint32_t i;
12213 for (i=0; i<num_attributes; ++i)
12214 {
12215 attr = attributes.AttributeAtIndex(i);
12216 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
12217 {
12218 switch (attr)
12219 {
12220 case DW_AT_type:
12221 type_die_offset = form_value.Reference(); break;
12222 case DW_AT_containing_type:
12223 containing_type_die_offset = form_value.Reference(); break;
12224 }
12225 }
12226 }
12227
12228 Type *pointee_type = dwarf->ResolveTypeUID(type_die_offset);
12229 Type *class_type = dwarf->ResolveTypeUID(containing_type_die_offset);
12230
12231 CompilerType pointee_clang_type = pointee_type->GetClangForwardType();
12232 CompilerType class_clang_type = class_type->GetClangLayoutType();
12233
12234 clang_type = ClangASTContext::CreateMemberPointerType(pointee_clang_type, class_clang_type);
12235
12236 byte_size = clang_type.GetByteSize(nullptr);
12237
12238 type_sp.reset( new Type (dwarf->MakeUserID(die->GetOffset()),
12239 dwarf,
12240 type_name_const_str,
12241 byte_size,
12242 NULL,
12243 LLDB_INVALID_UID,
12244 Type::eEncodingIsUID,
12245 NULL,
12246 clang_type,
12247 Type::eResolveStateForward));
12248 }
12249
12250 break;
12251 }
12252 default:
12253 dwarf->GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: unhandled type tag 0x%4.4x (%s), please file a bug and attach the file at the start of this error message",
12254 die->GetOffset(),
12255 tag,
12256 DW_TAG_value_to_name(tag));
12257 break;
12258 }
12259
12260 if (type_sp.get())
12261 {
12262 const DWARFDebugInfoEntry *sc_parent_die = SymbolFileDWARF::GetParentSymbolContextDIE(die);
12263 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
12264
12265 SymbolContextScope * symbol_context_scope = NULL;
12266 if (sc_parent_tag == DW_TAG_compile_unit)
12267 {
12268 symbol_context_scope = sc.comp_unit;
12269 }
12270 else if (sc.function != NULL && sc_parent_die)
12271 {
12272 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(dwarf->MakeUserID(sc_parent_die->GetOffset()));
12273 if (symbol_context_scope == NULL)
12274 symbol_context_scope = sc.function;
12275 }
12276
12277 if (symbol_context_scope != NULL)
12278 {
12279 type_sp->SetSymbolContextScope(symbol_context_scope);
12280 }
12281
12282 // We are ready to put this type into the uniqued list up at the module level
12283 type_list->Insert (type_sp);
12284
12285 dwarf->m_die_to_type[die] = type_sp.get();
12286 }
12287 }
12288 else if (type_ptr != DIE_IS_BEING_PARSED)
12289 {
12290 type_sp = type_ptr->shared_from_this();
12291 }
12292 }
12293 return type_sp;
12294}
12295
12296
12297bool
12298ClangASTContext::CopyUniqueClassMethodTypes (SymbolFileDWARF *dst_symfile,
12299 SymbolFileDWARF *src_symfile,
Yaron Kerenfe16dea2015-08-16 19:40:40 +000012300 lldb_private::Type *class_type,
Greg Clayton8b4edba2015-08-14 20:02:05 +000012301 DWARFCompileUnit* src_cu,
12302 const DWARFDebugInfoEntry *src_class_die,
12303 DWARFCompileUnit* dst_cu,
12304 const DWARFDebugInfoEntry *dst_class_die,
12305 DWARFDIECollection &failures)
12306{
12307 if (!class_type || !src_cu || !src_class_die || !dst_cu || !dst_class_die)
12308 return false;
12309 if (src_class_die->Tag() != dst_class_die->Tag())
12310 return false;
12311
12312 // We need to complete the class type so we can get all of the method types
12313 // parsed so we can then unique those types to their equivalent counterparts
12314 // in "dst_cu" and "dst_class_die"
12315 class_type->GetClangFullType();
12316
12317 const DWARFDebugInfoEntry *src_die;
12318 const DWARFDebugInfoEntry *dst_die;
12319 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die;
12320 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die;
12321 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die_artificial;
12322 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die_artificial;
12323 for (src_die = src_class_die->GetFirstChild(); src_die != NULL; src_die = src_die->GetSibling())
12324 {
12325 if (src_die->Tag() == DW_TAG_subprogram)
12326 {
12327 // Make sure this is a declaration and not a concrete instance by looking
12328 // for DW_AT_declaration set to 1. Sometimes concrete function instances
12329 // are placed inside the class definitions and shouldn't be included in
12330 // the list of things are are tracking here.
12331 if (src_die->GetAttributeValueAsUnsigned(src_symfile, src_cu, DW_AT_declaration, 0) == 1)
12332 {
12333 const char *src_name = src_die->GetMangledName (src_symfile, src_cu);
12334 if (src_name)
12335 {
12336 ConstString src_const_name(src_name);
12337 if (src_die->GetAttributeValueAsUnsigned(src_symfile, src_cu, DW_AT_artificial, 0))
12338 src_name_to_die_artificial.Append(src_const_name.GetCString(), src_die);
12339 else
12340 src_name_to_die.Append(src_const_name.GetCString(), src_die);
12341 }
12342 }
12343 }
12344 }
12345 for (dst_die = dst_class_die->GetFirstChild(); dst_die != NULL; dst_die = dst_die->GetSibling())
12346 {
12347 if (dst_die->Tag() == DW_TAG_subprogram)
12348 {
12349 // Make sure this is a declaration and not a concrete instance by looking
12350 // for DW_AT_declaration set to 1. Sometimes concrete function instances
12351 // are placed inside the class definitions and shouldn't be included in
12352 // the list of things are are tracking here.
12353 if (dst_die->GetAttributeValueAsUnsigned(dst_symfile, dst_cu, DW_AT_declaration, 0) == 1)
12354 {
12355 const char *dst_name = dst_die->GetMangledName (dst_symfile, dst_cu);
12356 if (dst_name)
12357 {
12358 ConstString dst_const_name(dst_name);
12359 if (dst_die->GetAttributeValueAsUnsigned(dst_symfile, dst_cu, DW_AT_artificial, 0))
12360 dst_name_to_die_artificial.Append(dst_const_name.GetCString(), dst_die);
12361 else
12362 dst_name_to_die.Append(dst_const_name.GetCString(), dst_die);
12363 }
12364 }
12365 }
12366 }
12367 const uint32_t src_size = src_name_to_die.GetSize ();
12368 const uint32_t dst_size = dst_name_to_die.GetSize ();
12369 Log *log = nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_TYPE_COMPLETION));
12370
12371 // Is everything kosher so we can go through the members at top speed?
12372 bool fast_path = true;
12373
12374 if (src_size != dst_size)
12375 {
12376 if (src_size != 0 && dst_size != 0)
12377 {
12378 if (log)
12379 log->Printf("warning: trying to unique class DIE 0x%8.8x to 0x%8.8x, but they didn't have the same size (src=%d, dst=%d)",
12380 src_class_die->GetOffset(),
12381 dst_class_die->GetOffset(),
12382 src_size,
12383 dst_size);
12384 }
12385
12386 fast_path = false;
12387 }
12388
12389 uint32_t idx;
12390
12391 if (fast_path)
12392 {
12393 for (idx = 0; idx < src_size; ++idx)
12394 {
12395 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
12396 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
12397
12398 if (src_die->Tag() != dst_die->Tag())
12399 {
12400 if (log)
12401 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x (%s) tags didn't match 0x%8.8x (%s)",
12402 src_class_die->GetOffset(),
12403 dst_class_die->GetOffset(),
12404 src_die->GetOffset(),
12405 DW_TAG_value_to_name(src_die->Tag()),
12406 dst_die->GetOffset(),
12407 DW_TAG_value_to_name(src_die->Tag()));
12408 fast_path = false;
12409 }
12410
12411 const char *src_name = src_die->GetMangledName (src_symfile, src_cu);
12412 const char *dst_name = dst_die->GetMangledName (dst_symfile, dst_cu);
12413
12414 // Make sure the names match
12415 if (src_name == dst_name || (strcmp (src_name, dst_name) == 0))
12416 continue;
12417
12418 if (log)
12419 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x (%s) names didn't match 0x%8.8x (%s)",
12420 src_class_die->GetOffset(),
12421 dst_class_die->GetOffset(),
12422 src_die->GetOffset(),
12423 src_name,
12424 dst_die->GetOffset(),
12425 dst_name);
12426
12427 fast_path = false;
12428 }
12429 }
12430
12431 // Now do the work of linking the DeclContexts and Types.
12432 if (fast_path)
12433 {
12434 // We can do this quickly. Just run across the tables index-for-index since
12435 // we know each node has matching names and tags.
12436 for (idx = 0; idx < src_size; ++idx)
12437 {
12438 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
12439 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
12440
Greg Clayton6dc8d582015-08-18 22:32:36 +000012441 clang::DeclContext *src_decl_ctx = src_symfile->GetClangASTContext().m_die_to_decl_ctx[src_die];
Greg Clayton8b4edba2015-08-14 20:02:05 +000012442 if (src_decl_ctx)
12443 {
12444 if (log)
12445 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
12446 static_cast<void*>(src_decl_ctx),
12447 src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton6dc8d582015-08-18 22:32:36 +000012448 dst_symfile->GetClangASTContext().LinkDeclContextToDIE (src_decl_ctx, dst_die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000012449 }
12450 else
12451 {
12452 if (log)
12453 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found",
12454 src_die->GetOffset(), dst_die->GetOffset());
12455 }
12456
12457 Type *src_child_type = dst_symfile->m_die_to_type[src_die];
12458 if (src_child_type)
12459 {
12460 if (log)
12461 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
12462 static_cast<void*>(src_child_type),
12463 src_child_type->GetID(),
12464 src_die->GetOffset(), dst_die->GetOffset());
12465 dst_symfile->m_die_to_type[dst_die] = src_child_type;
12466 }
12467 else
12468 {
12469 if (log)
12470 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
12471 }
12472 }
12473 }
12474 else
12475 {
12476 // We must do this slowly. For each member of the destination, look
12477 // up a member in the source with the same name, check its tag, and
12478 // unique them if everything matches up. Report failures.
12479
12480 if (!src_name_to_die.IsEmpty() && !dst_name_to_die.IsEmpty())
12481 {
12482 src_name_to_die.Sort();
12483
12484 for (idx = 0; idx < dst_size; ++idx)
12485 {
12486 const char *dst_name = dst_name_to_die.GetCStringAtIndex(idx);
12487 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
12488 src_die = src_name_to_die.Find(dst_name, NULL);
12489
12490 if (src_die && (src_die->Tag() == dst_die->Tag()))
12491 {
Greg Clayton6dc8d582015-08-18 22:32:36 +000012492 clang::DeclContext *src_decl_ctx = src_symfile->GetClangASTContext().m_die_to_decl_ctx[src_die];
Greg Clayton8b4edba2015-08-14 20:02:05 +000012493 if (src_decl_ctx)
12494 {
12495 if (log)
12496 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
12497 static_cast<void*>(src_decl_ctx),
12498 src_die->GetOffset(),
12499 dst_die->GetOffset());
Greg Clayton6dc8d582015-08-18 22:32:36 +000012500 dst_symfile->GetClangASTContext().LinkDeclContextToDIE (src_decl_ctx, dst_die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000012501 }
12502 else
12503 {
12504 if (log)
12505 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
12506 }
12507
12508 Type *src_child_type = dst_symfile->m_die_to_type[src_die];
12509 if (src_child_type)
12510 {
12511 if (log)
12512 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
12513 static_cast<void*>(src_child_type),
12514 src_child_type->GetID(),
12515 src_die->GetOffset(),
12516 dst_die->GetOffset());
12517 dst_symfile->m_die_to_type[dst_die] = src_child_type;
12518 }
12519 else
12520 {
12521 if (log)
12522 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
12523 }
12524 }
12525 else
12526 {
12527 if (log)
12528 log->Printf ("warning: couldn't find a match for 0x%8.8x", dst_die->GetOffset());
12529
12530 failures.Append(dst_die);
12531 }
12532 }
12533 }
12534 }
12535
12536 const uint32_t src_size_artificial = src_name_to_die_artificial.GetSize ();
12537 const uint32_t dst_size_artificial = dst_name_to_die_artificial.GetSize ();
12538
12539 UniqueCStringMap<const DWARFDebugInfoEntry *> name_to_die_artificial_not_in_src;
12540
12541 if (src_size_artificial && dst_size_artificial)
12542 {
12543 dst_name_to_die_artificial.Sort();
12544
12545 for (idx = 0; idx < src_size_artificial; ++idx)
12546 {
12547 const char *src_name_artificial = src_name_to_die_artificial.GetCStringAtIndex(idx);
12548 src_die = src_name_to_die_artificial.GetValueAtIndexUnchecked (idx);
12549 dst_die = dst_name_to_die_artificial.Find(src_name_artificial, NULL);
12550
12551 if (dst_die)
12552 {
12553 // Both classes have the artificial types, link them
Greg Clayton6dc8d582015-08-18 22:32:36 +000012554 clang::DeclContext *src_decl_ctx = dst_symfile->GetClangASTContext().m_die_to_decl_ctx[src_die];
Greg Clayton8b4edba2015-08-14 20:02:05 +000012555 if (src_decl_ctx)
12556 {
12557 if (log)
12558 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
12559 static_cast<void*>(src_decl_ctx),
12560 src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton6dc8d582015-08-18 22:32:36 +000012561 dst_symfile->GetClangASTContext().LinkDeclContextToDIE (src_decl_ctx, dst_die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000012562 }
12563 else
12564 {
12565 if (log)
12566 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
12567 }
12568
12569 Type *src_child_type = dst_symfile->m_die_to_type[src_die];
12570 if (src_child_type)
12571 {
12572 if (log)
12573 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
12574 static_cast<void*>(src_child_type),
12575 src_child_type->GetID(),
12576 src_die->GetOffset(), dst_die->GetOffset());
12577 dst_symfile->m_die_to_type[dst_die] = src_child_type;
12578 }
12579 else
12580 {
12581 if (log)
12582 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
12583 }
12584 }
12585 }
12586 }
12587
12588 if (dst_size_artificial)
12589 {
12590 for (idx = 0; idx < dst_size_artificial; ++idx)
12591 {
12592 const char *dst_name_artificial = dst_name_to_die_artificial.GetCStringAtIndex(idx);
12593 dst_die = dst_name_to_die_artificial.GetValueAtIndexUnchecked (idx);
12594 if (log)
12595 log->Printf ("warning: need to create artificial method for 0x%8.8x for method '%s'", dst_die->GetOffset(), dst_name_artificial);
12596
12597 failures.Append(dst_die);
12598 }
12599 }
12600
12601 return (failures.Size() != 0);
12602}
12603
12604
12605bool
12606ClangASTContext::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
12607 SymbolFileDWARF *dwarf,
12608 DWARFCompileUnit *cu,
12609 const DWARFDebugInfoEntry *die)
12610{
12611 // No namespace specified, so the answer is
12612 if (namespace_decl == NULL)
12613 return true;
12614
12615 Log *log = nullptr; //(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
12616
12617 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
12618 clang::DeclContext *die_clang_decl_ctx = GetClangDeclContextContainingDIE (dwarf, cu, die, &decl_ctx_die);
12619 if (decl_ctx_die)
12620 {
12621 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
12622
12623 if (clang_namespace_decl)
12624 {
12625 if (decl_ctx_die->Tag() != DW_TAG_namespace)
12626 {
12627 if (log)
12628 dwarf->GetObjectFile()->GetModule()->LogMessage(log, "Found a match, but its parent is not a namespace");
12629 return false;
12630 }
12631
12632 if (clang_namespace_decl == die_clang_decl_ctx)
12633 return true;
12634 else
12635 return false;
12636 }
12637 else
12638 {
12639 // We have a namespace_decl that was not NULL but it contained
12640 // a NULL "clang::NamespaceDecl", so this means the global namespace
12641 // So as long the contained decl context DIE isn't a namespace
12642 // we should be ok.
12643 if (decl_ctx_die->Tag() != DW_TAG_namespace)
12644 return true;
12645 }
12646 }
12647
12648 if (log)
12649 dwarf->GetObjectFile()->GetModule()->LogMessage(log, "Found a match, but its parent doesn't exist");
12650
12651 return false;
12652}
12653