blob: 1de5cb6cec062610726e1a5c7154363433f8130e [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 Claytond8d4a572015-08-11 21:38:15 +00002191bool
2192ClangASTContext::SetTagTypeKind (clang::QualType tag_qual_type, int kind) const
2193{
2194 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2195 if (clang_type)
2196 {
2197 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2198 if (tag_type)
2199 {
2200 clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2201 if (tag_decl)
2202 {
2203 tag_decl->setTagKind ((clang::TagDecl::TagKind)kind);
2204 return true;
2205 }
2206 }
2207 }
2208 return false;
2209}
2210
2211
2212bool
2213ClangASTContext::SetDefaultAccessForRecordFields (clang::RecordDecl* record_decl,
2214 int default_accessibility,
2215 int *assigned_accessibilities,
2216 size_t num_assigned_accessibilities)
2217{
2218 if (record_decl)
2219 {
2220 uint32_t field_idx;
2221 clang::RecordDecl::field_iterator field, field_end;
2222 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2223 field != field_end;
2224 ++field, ++field_idx)
2225 {
2226 // If no accessibility was assigned, assign the correct one
2227 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2228 field->setAccess ((clang::AccessSpecifier)default_accessibility);
2229 }
2230 return true;
2231 }
2232 return false;
2233}
2234
2235clang::DeclContext *
Greg Clayton99558cc42015-08-24 23:46:31 +00002236ClangASTContext::GetDeclContextForType (const CompilerType& type)
2237{
2238 return GetDeclContextForType(GetQualType(type));
2239}
2240
2241clang::DeclContext *
Greg Claytond8d4a572015-08-11 21:38:15 +00002242ClangASTContext::GetDeclContextForType (clang::QualType type)
2243{
2244 if (type.isNull())
2245 return nullptr;
2246
2247 clang::QualType qual_type = type.getCanonicalType();
2248 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2249 switch (type_class)
2250 {
Greg Claytond8d4a572015-08-11 21:38:15 +00002251 case clang::Type::ObjCInterface: return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())->getInterface();
2252 case clang::Type::ObjCObjectPointer: return GetDeclContextForType (llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
2253 case clang::Type::Record: return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2254 case clang::Type::Enum: return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2255 case clang::Type::Typedef: return GetDeclContextForType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
2256 case clang::Type::Elaborated: return GetDeclContextForType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2257 case clang::Type::Paren: return GetDeclContextForType (llvm::cast<clang::ParenType>(qual_type)->desugar());
Greg Clayton99558cc42015-08-24 23:46:31 +00002258 default:
2259 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00002260 }
2261 // No DeclContext in this type...
2262 return nullptr;
2263}
2264
2265static bool
2266GetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion = true)
2267{
2268 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2269 switch (type_class)
2270 {
2271 case clang::Type::ConstantArray:
2272 case clang::Type::IncompleteArray:
2273 case clang::Type::VariableArray:
2274 {
2275 const clang::ArrayType *array_type = llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2276
2277 if (array_type)
2278 return GetCompleteQualType (ast, array_type->getElementType(), allow_completion);
2279 }
2280 break;
2281
2282 case clang::Type::Record:
2283 case clang::Type::Enum:
2284 {
2285 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2286 if (tag_type)
2287 {
2288 clang::TagDecl *tag_decl = tag_type->getDecl();
2289 if (tag_decl)
2290 {
2291 if (tag_decl->isCompleteDefinition())
2292 return true;
2293
2294 if (!allow_completion)
2295 return false;
2296
2297 if (tag_decl->hasExternalLexicalStorage())
2298 {
2299 if (ast)
2300 {
2301 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2302 if (external_ast_source)
2303 {
2304 external_ast_source->CompleteType(tag_decl);
2305 return !tag_type->isIncompleteType();
2306 }
2307 }
2308 }
2309 return false;
2310 }
2311 }
2312
2313 }
2314 break;
2315
2316 case clang::Type::ObjCObject:
2317 case clang::Type::ObjCInterface:
2318 {
2319 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2320 if (objc_class_type)
2321 {
2322 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2323 // We currently can't complete objective C types through the newly added ASTContext
2324 // because it only supports TagDecl objects right now...
2325 if (class_interface_decl)
2326 {
2327 if (class_interface_decl->getDefinition())
2328 return true;
2329
2330 if (!allow_completion)
2331 return false;
2332
2333 if (class_interface_decl->hasExternalLexicalStorage())
2334 {
2335 if (ast)
2336 {
2337 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2338 if (external_ast_source)
2339 {
2340 external_ast_source->CompleteType (class_interface_decl);
2341 return !objc_class_type->isIncompleteType();
2342 }
2343 }
2344 }
2345 return false;
2346 }
2347 }
2348 }
2349 break;
2350
2351 case clang::Type::Typedef:
2352 return GetCompleteQualType (ast, llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType(), allow_completion);
2353
2354 case clang::Type::Elaborated:
2355 return GetCompleteQualType (ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(), allow_completion);
2356
2357 case clang::Type::Paren:
2358 return GetCompleteQualType (ast, llvm::cast<clang::ParenType>(qual_type)->desugar(), allow_completion);
2359
2360 default:
2361 break;
2362 }
2363
2364 return true;
2365}
2366
2367static clang::ObjCIvarDecl::AccessControl
2368ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
2369{
2370 switch (access)
2371 {
2372 case eAccessNone: return clang::ObjCIvarDecl::None;
2373 case eAccessPublic: return clang::ObjCIvarDecl::Public;
2374 case eAccessPrivate: return clang::ObjCIvarDecl::Private;
2375 case eAccessProtected: return clang::ObjCIvarDecl::Protected;
2376 case eAccessPackage: return clang::ObjCIvarDecl::Package;
2377 }
2378 return clang::ObjCIvarDecl::None;
2379}
2380
2381
2382//----------------------------------------------------------------------
2383// Tests
2384//----------------------------------------------------------------------
2385
2386bool
2387ClangASTContext::IsAggregateType (void* type)
2388{
2389 clang::QualType qual_type (GetCanonicalQualType(type));
2390
2391 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2392 switch (type_class)
2393 {
2394 case clang::Type::IncompleteArray:
2395 case clang::Type::VariableArray:
2396 case clang::Type::ConstantArray:
2397 case clang::Type::ExtVector:
2398 case clang::Type::Vector:
2399 case clang::Type::Record:
2400 case clang::Type::ObjCObject:
2401 case clang::Type::ObjCInterface:
2402 return true;
2403 case clang::Type::Elaborated:
2404 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2405 case clang::Type::Typedef:
2406 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2407 case clang::Type::Paren:
2408 return IsAggregateType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2409 default:
2410 break;
2411 }
2412 // The clang type does have a value
2413 return false;
2414}
2415
2416bool
2417ClangASTContext::IsArrayType (void* type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002418 CompilerType *element_type_ptr,
Greg Claytond8d4a572015-08-11 21:38:15 +00002419 uint64_t *size,
2420 bool *is_incomplete)
2421{
2422 clang::QualType qual_type (GetCanonicalQualType(type));
2423
2424 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2425 switch (type_class)
2426 {
2427 default:
2428 break;
2429
2430 case clang::Type::ConstantArray:
2431 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002432 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002433 if (size)
2434 *size = llvm::cast<clang::ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
2435 return true;
2436
2437 case clang::Type::IncompleteArray:
2438 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002439 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002440 if (size)
2441 *size = 0;
2442 if (is_incomplete)
2443 *is_incomplete = true;
2444 return true;
2445
2446 case clang::Type::VariableArray:
2447 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002448 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002449 if (size)
2450 *size = 0;
2451 return true;
2452
2453 case clang::Type::DependentSizedArray:
2454 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002455 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002456 if (size)
2457 *size = 0;
2458 return true;
2459
2460 case clang::Type::Typedef:
2461 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
2462 element_type_ptr,
2463 size,
2464 is_incomplete);
2465 case clang::Type::Elaborated:
2466 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2467 element_type_ptr,
2468 size,
2469 is_incomplete);
2470 case clang::Type::Paren:
2471 return IsArrayType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2472 element_type_ptr,
2473 size,
2474 is_incomplete);
2475 }
2476 if (element_type_ptr)
2477 element_type_ptr->Clear();
2478 if (size)
2479 *size = 0;
2480 if (is_incomplete)
2481 *is_incomplete = false;
2482 return 0;
2483}
2484
2485bool
2486ClangASTContext::IsVectorType (void* type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002487 CompilerType *element_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00002488 uint64_t *size)
2489{
2490 clang::QualType qual_type (GetCanonicalQualType(type));
2491
2492 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2493 switch (type_class)
2494 {
2495 case clang::Type::Vector:
2496 {
2497 const clang::VectorType *vector_type = qual_type->getAs<clang::VectorType>();
2498 if (vector_type)
2499 {
2500 if (size)
2501 *size = vector_type->getNumElements();
2502 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002503 *element_type = CompilerType(getASTContext(), vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002504 }
2505 return true;
2506 }
2507 break;
2508 case clang::Type::ExtVector:
2509 {
2510 const clang::ExtVectorType *ext_vector_type = qual_type->getAs<clang::ExtVectorType>();
2511 if (ext_vector_type)
2512 {
2513 if (size)
2514 *size = ext_vector_type->getNumElements();
2515 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002516 *element_type = CompilerType(getASTContext(), ext_vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002517 }
2518 return true;
2519 }
2520 default:
2521 break;
2522 }
2523 return false;
2524}
2525
2526bool
2527ClangASTContext::IsRuntimeGeneratedType (void* type)
2528{
2529 clang::DeclContext* decl_ctx = ClangASTContext::GetASTContext(getASTContext())->GetDeclContextForType(GetQualType(type));
2530 if (!decl_ctx)
2531 return false;
2532
2533 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2534 return false;
2535
2536 clang::ObjCInterfaceDecl *result_iface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2537
2538 ClangASTMetadata* ast_metadata = ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2539 if (!ast_metadata)
2540 return false;
2541 return (ast_metadata->GetISAPtr() != 0);
2542}
2543
2544bool
2545ClangASTContext::IsCharType (void* type)
2546{
2547 return GetQualType(type).getUnqualifiedType()->isCharType();
2548}
2549
2550
2551bool
2552ClangASTContext::IsCompleteType (void* type)
2553{
2554 const bool allow_completion = false;
2555 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
2556}
2557
2558bool
2559ClangASTContext::IsConst(void* type)
2560{
2561 return GetQualType(type).isConstQualified();
2562}
2563
2564bool
2565ClangASTContext::IsCStringType (void* type, uint32_t &length)
2566{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002567 CompilerType pointee_or_element_clang_type;
Greg Claytond8d4a572015-08-11 21:38:15 +00002568 length = 0;
2569 Flags type_flags (GetTypeInfo (type, &pointee_or_element_clang_type));
2570
2571 if (!pointee_or_element_clang_type.IsValid())
2572 return false;
2573
2574 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
2575 {
2576 if (pointee_or_element_clang_type.IsCharType())
2577 {
2578 if (type_flags.Test (eTypeIsArray))
2579 {
2580 // We know the size of the array and it could be a C string
2581 // since it is an array of characters
2582 length = llvm::cast<clang::ConstantArrayType>(GetCanonicalQualType(type).getTypePtr())->getSize().getLimitedValue();
2583 }
2584 return true;
2585
2586 }
2587 }
2588 return false;
2589}
2590
2591bool
2592ClangASTContext::IsFunctionType (void* type, bool *is_variadic_ptr)
2593{
2594 if (type)
2595 {
2596 clang::QualType qual_type (GetCanonicalQualType(type));
2597
2598 if (qual_type->isFunctionType())
2599 {
2600 if (is_variadic_ptr)
2601 {
2602 const clang::FunctionProtoType *function_proto_type = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2603 if (function_proto_type)
2604 *is_variadic_ptr = function_proto_type->isVariadic();
2605 else
2606 *is_variadic_ptr = false;
2607 }
2608 return true;
2609 }
2610
2611 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2612 switch (type_class)
2613 {
2614 default:
2615 break;
2616 case clang::Type::Typedef:
2617 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), nullptr);
2618 case clang::Type::Elaborated:
2619 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), nullptr);
2620 case clang::Type::Paren:
2621 return IsFunctionType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), nullptr);
2622 case clang::Type::LValueReference:
2623 case clang::Type::RValueReference:
2624 {
2625 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2626 if (reference_type)
2627 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(), nullptr);
2628 }
2629 break;
2630 }
2631 }
2632 return false;
2633}
2634
2635// Used to detect "Homogeneous Floating-point Aggregates"
2636uint32_t
Greg Claytona1e5dc82015-08-11 22:53:00 +00002637ClangASTContext::IsHomogeneousAggregate (void* type, CompilerType* base_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00002638{
2639 if (!type)
2640 return 0;
2641
2642 clang::QualType qual_type(GetCanonicalQualType(type));
2643 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2644 switch (type_class)
2645 {
2646 case clang::Type::Record:
2647 if (GetCompleteType (type))
2648 {
2649 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2650 if (cxx_record_decl)
2651 {
2652 if (cxx_record_decl->getNumBases() ||
2653 cxx_record_decl->isDynamicClass())
2654 return 0;
2655 }
2656 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
2657 if (record_type)
2658 {
2659 const clang::RecordDecl *record_decl = record_type->getDecl();
2660 if (record_decl)
2661 {
2662 // We are looking for a structure that contains only floating point types
2663 clang::RecordDecl::field_iterator field_pos, field_end = record_decl->field_end();
2664 uint32_t num_fields = 0;
2665 bool is_hva = false;
2666 bool is_hfa = false;
2667 clang::QualType base_qual_type;
2668 for (field_pos = record_decl->field_begin(); field_pos != field_end; ++field_pos)
2669 {
2670 clang::QualType field_qual_type = field_pos->getType();
2671 if (field_qual_type->isFloatingType())
2672 {
2673 if (field_qual_type->isComplexType())
2674 return 0;
2675 else
2676 {
2677 if (num_fields == 0)
2678 base_qual_type = field_qual_type;
2679 else
2680 {
2681 if (is_hva)
2682 return 0;
2683 is_hfa = true;
2684 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
2685 return 0;
2686 }
2687 }
2688 }
2689 else if (field_qual_type->isVectorType() || field_qual_type->isExtVectorType())
2690 {
2691 const clang::VectorType *array = field_qual_type.getTypePtr()->getAs<clang::VectorType>();
2692 if (array && array->getNumElements() <= 4)
2693 {
2694 if (num_fields == 0)
2695 base_qual_type = array->getElementType();
2696 else
2697 {
2698 if (is_hfa)
2699 return 0;
2700 is_hva = true;
2701 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
2702 return 0;
2703 }
2704 }
2705 else
2706 return 0;
2707 }
2708 else
2709 return 0;
2710 ++num_fields;
2711 }
2712 if (base_type_ptr)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002713 *base_type_ptr = CompilerType (getASTContext(), base_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00002714 return num_fields;
2715 }
2716 }
2717 }
2718 break;
2719
2720 case clang::Type::Typedef:
2721 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), base_type_ptr);
2722
2723 case clang::Type::Elaborated:
2724 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), base_type_ptr);
2725 default:
2726 break;
2727 }
2728 return 0;
2729}
2730
2731size_t
2732ClangASTContext::GetNumberOfFunctionArguments (void* type)
2733{
2734 if (type)
2735 {
2736 clang::QualType qual_type (GetCanonicalQualType(type));
2737 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2738 if (func)
2739 return func->getNumParams();
2740 }
2741 return 0;
2742}
2743
Greg Claytona1e5dc82015-08-11 22:53:00 +00002744CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00002745ClangASTContext::GetFunctionArgumentAtIndex (void* type, const size_t index)
2746{
2747 if (type)
2748 {
2749 clang::QualType qual_type (GetCanonicalQualType(type));
2750 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2751 if (func)
2752 {
2753 if (index < func->getNumParams())
Greg Claytona1e5dc82015-08-11 22:53:00 +00002754 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00002755 }
2756 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002757 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00002758}
2759
2760bool
2761ClangASTContext::IsFunctionPointerType (void* type)
2762{
2763 if (type)
2764 {
2765 clang::QualType qual_type (GetCanonicalQualType(type));
2766
2767 if (qual_type->isFunctionPointerType())
2768 return true;
2769
2770 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2771 switch (type_class)
2772 {
2773 default:
2774 break;
2775 case clang::Type::Typedef:
2776 return IsFunctionPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2777 case clang::Type::Elaborated:
2778 return IsFunctionPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2779 case clang::Type::Paren:
2780 return IsFunctionPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2781
2782 case clang::Type::LValueReference:
2783 case clang::Type::RValueReference:
2784 {
2785 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2786 if (reference_type)
2787 return IsFunctionPointerType(reference_type->getPointeeType().getAsOpaquePtr());
2788 }
2789 break;
2790 }
2791 }
2792 return false;
2793
2794}
2795
2796bool
2797ClangASTContext::IsIntegerType (void* type, bool &is_signed)
2798{
2799 if (!type)
2800 return false;
2801
2802 clang::QualType qual_type (GetCanonicalQualType(type));
2803 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
2804
2805 if (builtin_type)
2806 {
2807 if (builtin_type->isInteger())
2808 {
2809 is_signed = builtin_type->isSignedInteger();
2810 return true;
2811 }
2812 }
2813
2814 return false;
2815}
2816
2817bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00002818ClangASTContext::IsPointerType (void* type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002819{
2820 if (type)
2821 {
2822 clang::QualType qual_type (GetCanonicalQualType(type));
2823 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2824 switch (type_class)
2825 {
2826 case clang::Type::Builtin:
2827 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
2828 {
2829 default:
2830 break;
2831 case clang::BuiltinType::ObjCId:
2832 case clang::BuiltinType::ObjCClass:
2833 return true;
2834 }
2835 return false;
2836 case clang::Type::ObjCObjectPointer:
2837 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002838 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002839 return true;
2840 case clang::Type::BlockPointer:
2841 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002842 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002843 return true;
2844 case clang::Type::Pointer:
2845 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002846 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002847 return true;
2848 case clang::Type::MemberPointer:
2849 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002850 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002851 return true;
2852 case clang::Type::Typedef:
2853 return IsPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
2854 case clang::Type::Elaborated:
2855 return IsPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
2856 case clang::Type::Paren:
2857 return IsPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
2858 default:
2859 break;
2860 }
2861 }
2862 if (pointee_type)
2863 pointee_type->Clear();
2864 return false;
2865}
2866
2867
2868bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00002869ClangASTContext::IsPointerOrReferenceType (void* type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002870{
2871 if (type)
2872 {
2873 clang::QualType qual_type (GetCanonicalQualType(type));
2874 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2875 switch (type_class)
2876 {
2877 case clang::Type::Builtin:
2878 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
2879 {
2880 default:
2881 break;
2882 case clang::BuiltinType::ObjCId:
2883 case clang::BuiltinType::ObjCClass:
2884 return true;
2885 }
2886 return false;
2887 case clang::Type::ObjCObjectPointer:
2888 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002889 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002890 return true;
2891 case clang::Type::BlockPointer:
2892 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002893 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002894 return true;
2895 case clang::Type::Pointer:
2896 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002897 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002898 return true;
2899 case clang::Type::MemberPointer:
2900 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002901 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002902 return true;
2903 case clang::Type::LValueReference:
2904 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002905 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00002906 return true;
2907 case clang::Type::RValueReference:
2908 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002909 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00002910 return true;
2911 case clang::Type::Typedef:
2912 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
2913 case clang::Type::Elaborated:
2914 return IsPointerOrReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
2915 case clang::Type::Paren:
2916 return IsPointerOrReferenceType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
2917 default:
2918 break;
2919 }
2920 }
2921 if (pointee_type)
2922 pointee_type->Clear();
2923 return false;
2924}
2925
2926
2927bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00002928ClangASTContext::IsReferenceType (void* type, CompilerType *pointee_type, bool* is_rvalue)
Greg Claytond8d4a572015-08-11 21:38:15 +00002929{
2930 if (type)
2931 {
2932 clang::QualType qual_type (GetCanonicalQualType(type));
2933 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2934
2935 switch (type_class)
2936 {
2937 case clang::Type::LValueReference:
2938 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002939 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00002940 if (is_rvalue)
2941 *is_rvalue = false;
2942 return true;
2943 case clang::Type::RValueReference:
2944 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002945 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00002946 if (is_rvalue)
2947 *is_rvalue = true;
2948 return true;
2949 case clang::Type::Typedef:
2950 return IsReferenceType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type, is_rvalue);
2951 case clang::Type::Elaborated:
2952 return IsReferenceType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type, is_rvalue);
2953 case clang::Type::Paren:
2954 return IsReferenceType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type, is_rvalue);
2955
2956 default:
2957 break;
2958 }
2959 }
2960 if (pointee_type)
2961 pointee_type->Clear();
2962 return false;
2963}
2964
2965bool
2966ClangASTContext::IsFloatingPointType (void* type, uint32_t &count, bool &is_complex)
2967{
2968 if (type)
2969 {
2970 clang::QualType qual_type (GetCanonicalQualType(type));
2971
2972 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal()))
2973 {
2974 clang::BuiltinType::Kind kind = BT->getKind();
2975 if (kind >= clang::BuiltinType::Float && kind <= clang::BuiltinType::LongDouble)
2976 {
2977 count = 1;
2978 is_complex = false;
2979 return true;
2980 }
2981 }
2982 else if (const clang::ComplexType *CT = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal()))
2983 {
2984 if (IsFloatingPointType (CT->getElementType().getAsOpaquePtr(), count, is_complex))
2985 {
2986 count = 2;
2987 is_complex = true;
2988 return true;
2989 }
2990 }
2991 else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal()))
2992 {
2993 if (IsFloatingPointType (VT->getElementType().getAsOpaquePtr(), count, is_complex))
2994 {
2995 count = VT->getNumElements();
2996 is_complex = false;
2997 return true;
2998 }
2999 }
3000 }
3001 count = 0;
3002 is_complex = false;
3003 return false;
3004}
3005
3006
3007bool
3008ClangASTContext::IsDefined(void* type)
3009{
3010 if (!type)
3011 return false;
3012
3013 clang::QualType qual_type(GetQualType(type));
3014 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3015 if (tag_type)
3016 {
3017 clang::TagDecl *tag_decl = tag_type->getDecl();
3018 if (tag_decl)
3019 return tag_decl->isCompleteDefinition();
3020 return false;
3021 }
3022 else
3023 {
3024 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3025 if (objc_class_type)
3026 {
3027 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3028 if (class_interface_decl)
3029 return class_interface_decl->getDefinition() != nullptr;
3030 return false;
3031 }
3032 }
3033 return true;
3034}
3035
3036bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003037ClangASTContext::IsObjCClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003038{
3039 if (type)
3040 {
3041 clang::QualType qual_type (GetCanonicalQualType(type));
3042
3043 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3044
3045 if (obj_pointer_type)
3046 return obj_pointer_type->isObjCClassType();
3047 }
3048 return false;
3049}
3050
3051bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003052ClangASTContext::IsObjCObjectOrInterfaceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003053{
3054 if (type)
3055 return GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3056 return false;
3057}
3058
3059bool
3060ClangASTContext::IsPolymorphicClass (void* type)
3061{
3062 if (type)
3063 {
3064 clang::QualType qual_type(GetCanonicalQualType(type));
3065 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3066 switch (type_class)
3067 {
3068 case clang::Type::Record:
3069 if (GetCompleteType(type))
3070 {
3071 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3072 const clang::RecordDecl *record_decl = record_type->getDecl();
3073 if (record_decl)
3074 {
3075 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3076 if (cxx_record_decl)
3077 return cxx_record_decl->isPolymorphic();
3078 }
3079 }
3080 break;
3081
3082 default:
3083 break;
3084 }
3085 }
3086 return false;
3087}
3088
3089bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003090ClangASTContext::IsPossibleDynamicType (void* type, CompilerType *dynamic_pointee_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00003091 bool check_cplusplus,
3092 bool check_objc)
3093{
3094 clang::QualType pointee_qual_type;
3095 if (type)
3096 {
3097 clang::QualType qual_type (GetCanonicalQualType(type));
3098 bool success = false;
3099 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3100 switch (type_class)
3101 {
3102 case clang::Type::Builtin:
3103 if (check_objc && llvm::cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
3104 {
3105 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003106 dynamic_pointee_type->SetCompilerType(this, type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003107 return true;
3108 }
3109 break;
3110
3111 case clang::Type::ObjCObjectPointer:
3112 if (check_objc)
3113 {
3114 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003115 dynamic_pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003116 return true;
3117 }
3118 break;
3119
3120 case clang::Type::Pointer:
3121 pointee_qual_type = llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3122 success = true;
3123 break;
3124
3125 case clang::Type::LValueReference:
3126 case clang::Type::RValueReference:
3127 pointee_qual_type = llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3128 success = true;
3129 break;
3130
3131 case clang::Type::Typedef:
3132 return IsPossibleDynamicType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3133 dynamic_pointee_type,
3134 check_cplusplus,
3135 check_objc);
3136
3137 case clang::Type::Elaborated:
3138 return IsPossibleDynamicType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3139 dynamic_pointee_type,
3140 check_cplusplus,
3141 check_objc);
3142
3143 case clang::Type::Paren:
3144 return IsPossibleDynamicType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3145 dynamic_pointee_type,
3146 check_cplusplus,
3147 check_objc);
3148 default:
3149 break;
3150 }
3151
3152 if (success)
3153 {
3154 // Check to make sure what we are pointing too is a possible dynamic C++ type
3155 // We currently accept any "void *" (in case we have a class that has been
3156 // watered down to an opaque pointer) and virtual C++ classes.
3157 const clang::Type::TypeClass pointee_type_class = pointee_qual_type.getCanonicalType()->getTypeClass();
3158 switch (pointee_type_class)
3159 {
3160 case clang::Type::Builtin:
3161 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind())
3162 {
3163 case clang::BuiltinType::UnknownAny:
3164 case clang::BuiltinType::Void:
3165 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003166 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003167 return true;
3168
3169 case clang::BuiltinType::NullPtr:
3170 case clang::BuiltinType::Bool:
3171 case clang::BuiltinType::Char_U:
3172 case clang::BuiltinType::UChar:
3173 case clang::BuiltinType::WChar_U:
3174 case clang::BuiltinType::Char16:
3175 case clang::BuiltinType::Char32:
3176 case clang::BuiltinType::UShort:
3177 case clang::BuiltinType::UInt:
3178 case clang::BuiltinType::ULong:
3179 case clang::BuiltinType::ULongLong:
3180 case clang::BuiltinType::UInt128:
3181 case clang::BuiltinType::Char_S:
3182 case clang::BuiltinType::SChar:
3183 case clang::BuiltinType::WChar_S:
3184 case clang::BuiltinType::Short:
3185 case clang::BuiltinType::Int:
3186 case clang::BuiltinType::Long:
3187 case clang::BuiltinType::LongLong:
3188 case clang::BuiltinType::Int128:
3189 case clang::BuiltinType::Float:
3190 case clang::BuiltinType::Double:
3191 case clang::BuiltinType::LongDouble:
3192 case clang::BuiltinType::Dependent:
3193 case clang::BuiltinType::Overload:
3194 case clang::BuiltinType::ObjCId:
3195 case clang::BuiltinType::ObjCClass:
3196 case clang::BuiltinType::ObjCSel:
3197 case clang::BuiltinType::BoundMember:
3198 case clang::BuiltinType::Half:
3199 case clang::BuiltinType::ARCUnbridgedCast:
3200 case clang::BuiltinType::PseudoObject:
3201 case clang::BuiltinType::BuiltinFn:
3202 case clang::BuiltinType::OCLEvent:
3203 case clang::BuiltinType::OCLImage1d:
3204 case clang::BuiltinType::OCLImage1dArray:
3205 case clang::BuiltinType::OCLImage1dBuffer:
3206 case clang::BuiltinType::OCLImage2d:
3207 case clang::BuiltinType::OCLImage2dArray:
3208 case clang::BuiltinType::OCLImage3d:
3209 case clang::BuiltinType::OCLSampler:
3210 break;
3211 }
3212 break;
3213
3214 case clang::Type::Record:
3215 if (check_cplusplus)
3216 {
3217 clang::CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
3218 if (cxx_record_decl)
3219 {
3220 bool is_complete = cxx_record_decl->isCompleteDefinition();
3221
3222 if (is_complete)
3223 success = cxx_record_decl->isDynamicClass();
3224 else
3225 {
3226 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), cxx_record_decl);
3227 if (metadata)
3228 success = metadata->GetIsDynamicCXXType();
3229 else
3230 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00003231 is_complete = CompilerType(getASTContext(), pointee_qual_type).GetCompleteType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003232 if (is_complete)
3233 success = cxx_record_decl->isDynamicClass();
3234 else
3235 success = false;
3236 }
3237 }
3238
3239 if (success)
3240 {
3241 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003242 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003243 return true;
3244 }
3245 }
3246 }
3247 break;
3248
3249 case clang::Type::ObjCObject:
3250 case clang::Type::ObjCInterface:
3251 if (check_objc)
3252 {
3253 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003254 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003255 return true;
3256 }
3257 break;
3258
3259 default:
3260 break;
3261 }
3262 }
3263 }
3264 if (dynamic_pointee_type)
3265 dynamic_pointee_type->Clear();
3266 return false;
3267}
3268
3269
3270bool
3271ClangASTContext::IsScalarType (void* type)
3272{
3273 if (!type)
3274 return false;
3275
3276 return (GetTypeInfo (type, nullptr) & eTypeIsScalar) != 0;
3277}
3278
3279bool
3280ClangASTContext::IsTypedefType (void* type)
3281{
3282 if (!type)
3283 return false;
3284 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3285}
3286
3287bool
3288ClangASTContext::IsVoidType (void* type)
3289{
3290 if (!type)
3291 return false;
3292 return GetCanonicalQualType(type)->isVoidType();
3293}
3294
3295bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003296ClangASTContext::GetCXXClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003297{
3298 if (type)
3299 {
3300 clang::QualType qual_type (GetCanonicalQualType(type));
3301
3302 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3303 if (cxx_record_decl)
3304 {
3305 class_name.assign (cxx_record_decl->getIdentifier()->getNameStart());
3306 return true;
3307 }
3308 }
3309 class_name.clear();
3310 return false;
3311}
3312
3313
3314bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003315ClangASTContext::IsCXXClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003316{
3317 if (!type)
3318 return false;
3319
3320 clang::QualType qual_type (GetCanonicalQualType(type));
3321 if (qual_type->getAsCXXRecordDecl() != nullptr)
3322 return true;
3323 return false;
3324}
3325
3326bool
3327ClangASTContext::IsBeingDefined (void* type)
3328{
3329 if (!type)
3330 return false;
3331 clang::QualType qual_type (GetCanonicalQualType(type));
3332 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3333 if (tag_type)
3334 return tag_type->isBeingDefined();
3335 return false;
3336}
3337
3338bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003339ClangASTContext::IsObjCObjectPointerType (const CompilerType& type, CompilerType *class_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003340{
3341 if (!type)
3342 return false;
3343
3344 clang::QualType qual_type (GetCanonicalQualType(type));
3345
3346 if (qual_type->isObjCObjectPointerType())
3347 {
3348 if (class_type_ptr)
3349 {
3350 if (!qual_type->isObjCClassType() &&
3351 !qual_type->isObjCIdType())
3352 {
3353 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3354 if (obj_pointer_type == nullptr)
3355 class_type_ptr->Clear();
3356 else
Greg Clayton99558cc42015-08-24 23:46:31 +00003357 class_type_ptr->SetCompilerType (type.GetTypeSystem(), clang::QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003358 }
3359 }
3360 return true;
3361 }
3362 if (class_type_ptr)
3363 class_type_ptr->Clear();
3364 return false;
3365}
3366
3367bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003368ClangASTContext::GetObjCClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003369{
3370 if (!type)
3371 return false;
3372
3373 clang::QualType qual_type (GetCanonicalQualType(type));
3374
3375 const clang::ObjCObjectType *object_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3376 if (object_type)
3377 {
3378 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3379 if (interface)
3380 {
3381 class_name = interface->getNameAsString();
3382 return true;
3383 }
3384 }
3385 return false;
3386}
3387
3388
3389//----------------------------------------------------------------------
3390// Type Completion
3391//----------------------------------------------------------------------
3392
3393bool
3394ClangASTContext::GetCompleteType (void* type)
3395{
3396 if (!type)
3397 return false;
3398 const bool allow_completion = true;
3399 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
3400}
3401
3402ConstString
3403ClangASTContext::GetTypeName (void* type)
3404{
3405 std::string type_name;
3406 if (type)
3407 {
3408 clang::PrintingPolicy printing_policy (getASTContext()->getPrintingPolicy());
3409 clang::QualType qual_type(GetQualType(type));
3410 printing_policy.SuppressTagKeyword = true;
3411 printing_policy.LangOpts.WChar = true;
3412 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
3413 if (typedef_type)
3414 {
3415 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3416 type_name = typedef_decl->getQualifiedNameAsString();
3417 }
3418 else
3419 {
3420 type_name = qual_type.getAsString(printing_policy);
3421 }
3422 }
3423 return ConstString(type_name);
3424}
3425
3426uint32_t
Greg Claytona1e5dc82015-08-11 22:53:00 +00003427ClangASTContext::GetTypeInfo (void* type, CompilerType *pointee_or_element_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003428{
3429 if (!type)
3430 return 0;
3431
3432 if (pointee_or_element_clang_type)
3433 pointee_or_element_clang_type->Clear();
3434
3435 clang::QualType qual_type (GetQualType(type));
3436
3437 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3438 switch (type_class)
3439 {
3440 case clang::Type::Builtin:
3441 {
3442 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3443
3444 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3445 switch (builtin_type->getKind())
3446 {
3447 case clang::BuiltinType::ObjCId:
3448 case clang::BuiltinType::ObjCClass:
3449 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003450 pointee_or_element_clang_type->SetCompilerType(getASTContext(), getASTContext()->ObjCBuiltinClassTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003451 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3452 break;
3453
3454 case clang::BuiltinType::ObjCSel:
3455 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003456 pointee_or_element_clang_type->SetCompilerType(getASTContext(), getASTContext()->CharTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003457 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3458 break;
3459
3460 case clang::BuiltinType::Bool:
3461 case clang::BuiltinType::Char_U:
3462 case clang::BuiltinType::UChar:
3463 case clang::BuiltinType::WChar_U:
3464 case clang::BuiltinType::Char16:
3465 case clang::BuiltinType::Char32:
3466 case clang::BuiltinType::UShort:
3467 case clang::BuiltinType::UInt:
3468 case clang::BuiltinType::ULong:
3469 case clang::BuiltinType::ULongLong:
3470 case clang::BuiltinType::UInt128:
3471 case clang::BuiltinType::Char_S:
3472 case clang::BuiltinType::SChar:
3473 case clang::BuiltinType::WChar_S:
3474 case clang::BuiltinType::Short:
3475 case clang::BuiltinType::Int:
3476 case clang::BuiltinType::Long:
3477 case clang::BuiltinType::LongLong:
3478 case clang::BuiltinType::Int128:
3479 case clang::BuiltinType::Float:
3480 case clang::BuiltinType::Double:
3481 case clang::BuiltinType::LongDouble:
3482 builtin_type_flags |= eTypeIsScalar;
3483 if (builtin_type->isInteger())
3484 {
3485 builtin_type_flags |= eTypeIsInteger;
3486 if (builtin_type->isSignedInteger())
3487 builtin_type_flags |= eTypeIsSigned;
3488 }
3489 else if (builtin_type->isFloatingPoint())
3490 builtin_type_flags |= eTypeIsFloat;
3491 break;
3492 default:
3493 break;
3494 }
3495 return builtin_type_flags;
3496 }
3497
3498 case clang::Type::BlockPointer:
3499 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003500 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003501 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3502
3503 case clang::Type::Complex:
3504 {
3505 uint32_t complex_type_flags = eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3506 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal());
3507 if (complex_type)
3508 {
3509 clang::QualType complex_element_type (complex_type->getElementType());
3510 if (complex_element_type->isIntegerType())
3511 complex_type_flags |= eTypeIsFloat;
3512 else if (complex_element_type->isFloatingType())
3513 complex_type_flags |= eTypeIsInteger;
3514 }
3515 return complex_type_flags;
3516 }
3517 break;
3518
3519 case clang::Type::ConstantArray:
3520 case clang::Type::DependentSizedArray:
3521 case clang::Type::IncompleteArray:
3522 case clang::Type::VariableArray:
3523 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003524 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003525 return eTypeHasChildren | eTypeIsArray;
3526
3527 case clang::Type::DependentName: return 0;
3528 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
3529 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
3530 case clang::Type::Decltype: return 0;
3531
3532 case clang::Type::Enum:
3533 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003534 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003535 return eTypeIsEnumeration | eTypeHasValue;
3536
3537 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003538 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003539 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003540 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003541
3542 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
3543 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
3544 case clang::Type::InjectedClassName: return 0;
3545
3546 case clang::Type::LValueReference:
3547 case clang::Type::RValueReference:
3548 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003549 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003550 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
3551
3552 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
3553
3554 case clang::Type::ObjCObjectPointer:
3555 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003556 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003557 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
3558
3559 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3560 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3561
3562 case clang::Type::Pointer:
3563 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003564 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003565 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
3566
3567 case clang::Type::Record:
3568 if (qual_type->getAsCXXRecordDecl())
3569 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
3570 else
3571 return eTypeHasChildren | eTypeIsStructUnion;
3572 break;
3573 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
3574 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
3575 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
3576
3577 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003578 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 +00003579 case clang::Type::TypeOfExpr: return 0;
3580 case clang::Type::TypeOf: return 0;
3581 case clang::Type::UnresolvedUsing: return 0;
3582
3583 case clang::Type::ExtVector:
3584 case clang::Type::Vector:
3585 {
3586 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
3587 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal());
3588 if (vector_type)
3589 {
3590 if (vector_type->isIntegerType())
3591 vector_type_flags |= eTypeIsFloat;
3592 else if (vector_type->isFloatingType())
3593 vector_type_flags |= eTypeIsInteger;
3594 }
3595 return vector_type_flags;
3596 }
3597 default: return 0;
3598 }
3599 return 0;
3600}
3601
3602
3603
3604lldb::LanguageType
3605ClangASTContext::GetMinimumLanguage (void* type)
3606{
3607 if (!type)
3608 return lldb::eLanguageTypeC;
3609
3610 // If the type is a reference, then resolve it to what it refers to first:
3611 clang::QualType qual_type (GetCanonicalQualType(type).getNonReferenceType());
3612 if (qual_type->isAnyPointerType())
3613 {
3614 if (qual_type->isObjCObjectPointerType())
3615 return lldb::eLanguageTypeObjC;
3616
3617 clang::QualType pointee_type (qual_type->getPointeeType());
3618 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
3619 return lldb::eLanguageTypeC_plus_plus;
3620 if (pointee_type->isObjCObjectOrInterfaceType())
3621 return lldb::eLanguageTypeObjC;
3622 if (pointee_type->isObjCClassType())
3623 return lldb::eLanguageTypeObjC;
3624 if (pointee_type.getTypePtr() == getASTContext()->ObjCBuiltinIdTy.getTypePtr())
3625 return lldb::eLanguageTypeObjC;
3626 }
3627 else
3628 {
3629 if (qual_type->isObjCObjectOrInterfaceType())
3630 return lldb::eLanguageTypeObjC;
3631 if (qual_type->getAsCXXRecordDecl())
3632 return lldb::eLanguageTypeC_plus_plus;
3633 switch (qual_type->getTypeClass())
3634 {
3635 default:
3636 break;
3637 case clang::Type::Builtin:
3638 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
3639 {
3640 default:
3641 case clang::BuiltinType::Void:
3642 case clang::BuiltinType::Bool:
3643 case clang::BuiltinType::Char_U:
3644 case clang::BuiltinType::UChar:
3645 case clang::BuiltinType::WChar_U:
3646 case clang::BuiltinType::Char16:
3647 case clang::BuiltinType::Char32:
3648 case clang::BuiltinType::UShort:
3649 case clang::BuiltinType::UInt:
3650 case clang::BuiltinType::ULong:
3651 case clang::BuiltinType::ULongLong:
3652 case clang::BuiltinType::UInt128:
3653 case clang::BuiltinType::Char_S:
3654 case clang::BuiltinType::SChar:
3655 case clang::BuiltinType::WChar_S:
3656 case clang::BuiltinType::Short:
3657 case clang::BuiltinType::Int:
3658 case clang::BuiltinType::Long:
3659 case clang::BuiltinType::LongLong:
3660 case clang::BuiltinType::Int128:
3661 case clang::BuiltinType::Float:
3662 case clang::BuiltinType::Double:
3663 case clang::BuiltinType::LongDouble:
3664 break;
3665
3666 case clang::BuiltinType::NullPtr:
3667 return eLanguageTypeC_plus_plus;
3668
3669 case clang::BuiltinType::ObjCId:
3670 case clang::BuiltinType::ObjCClass:
3671 case clang::BuiltinType::ObjCSel:
3672 return eLanguageTypeObjC;
3673
3674 case clang::BuiltinType::Dependent:
3675 case clang::BuiltinType::Overload:
3676 case clang::BuiltinType::BoundMember:
3677 case clang::BuiltinType::UnknownAny:
3678 break;
3679 }
3680 break;
3681 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003682 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetMinimumLanguage();
Greg Claytond8d4a572015-08-11 21:38:15 +00003683 }
3684 }
3685 return lldb::eLanguageTypeC;
3686}
3687
3688lldb::TypeClass
3689ClangASTContext::GetTypeClass (void* type)
3690{
3691 if (!type)
3692 return lldb::eTypeClassInvalid;
3693
3694 clang::QualType qual_type(GetQualType(type));
3695
3696 switch (qual_type->getTypeClass())
3697 {
3698 case clang::Type::UnaryTransform: break;
3699 case clang::Type::FunctionNoProto: return lldb::eTypeClassFunction;
3700 case clang::Type::FunctionProto: return lldb::eTypeClassFunction;
3701 case clang::Type::IncompleteArray: return lldb::eTypeClassArray;
3702 case clang::Type::VariableArray: return lldb::eTypeClassArray;
3703 case clang::Type::ConstantArray: return lldb::eTypeClassArray;
3704 case clang::Type::DependentSizedArray: return lldb::eTypeClassArray;
3705 case clang::Type::DependentSizedExtVector: return lldb::eTypeClassVector;
3706 case clang::Type::ExtVector: return lldb::eTypeClassVector;
3707 case clang::Type::Vector: return lldb::eTypeClassVector;
3708 case clang::Type::Builtin: return lldb::eTypeClassBuiltin;
3709 case clang::Type::ObjCObjectPointer: return lldb::eTypeClassObjCObjectPointer;
3710 case clang::Type::BlockPointer: return lldb::eTypeClassBlockPointer;
3711 case clang::Type::Pointer: return lldb::eTypeClassPointer;
3712 case clang::Type::LValueReference: return lldb::eTypeClassReference;
3713 case clang::Type::RValueReference: return lldb::eTypeClassReference;
3714 case clang::Type::MemberPointer: return lldb::eTypeClassMemberPointer;
3715 case clang::Type::Complex:
3716 if (qual_type->isComplexType())
3717 return lldb::eTypeClassComplexFloat;
3718 else
3719 return lldb::eTypeClassComplexInteger;
3720 case clang::Type::ObjCObject: return lldb::eTypeClassObjCObject;
3721 case clang::Type::ObjCInterface: return lldb::eTypeClassObjCInterface;
3722 case clang::Type::Record:
3723 {
3724 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3725 const clang::RecordDecl *record_decl = record_type->getDecl();
3726 if (record_decl->isUnion())
3727 return lldb::eTypeClassUnion;
3728 else if (record_decl->isStruct())
3729 return lldb::eTypeClassStruct;
3730 else
3731 return lldb::eTypeClassClass;
3732 }
3733 break;
3734 case clang::Type::Enum: return lldb::eTypeClassEnumeration;
3735 case clang::Type::Typedef: return lldb::eTypeClassTypedef;
3736 case clang::Type::UnresolvedUsing: break;
3737 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003738 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00003739 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003740 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00003741
3742 case clang::Type::Attributed: break;
3743 case clang::Type::TemplateTypeParm: break;
3744 case clang::Type::SubstTemplateTypeParm: break;
3745 case clang::Type::SubstTemplateTypeParmPack:break;
3746 case clang::Type::Auto: break;
3747 case clang::Type::InjectedClassName: break;
3748 case clang::Type::DependentName: break;
3749 case clang::Type::DependentTemplateSpecialization: break;
3750 case clang::Type::PackExpansion: break;
3751
3752 case clang::Type::TypeOfExpr: break;
3753 case clang::Type::TypeOf: break;
3754 case clang::Type::Decltype: break;
3755 case clang::Type::TemplateSpecialization: break;
3756 case clang::Type::Atomic: break;
3757
3758 // pointer type decayed from an array or function type.
3759 case clang::Type::Decayed: break;
3760 case clang::Type::Adjusted: break;
3761 }
3762 // We don't know hot to display this type...
3763 return lldb::eTypeClassOther;
3764
3765}
3766
3767unsigned
3768ClangASTContext::GetTypeQualifiers(void* type)
3769{
3770 if (type)
3771 return GetQualType(type).getQualifiers().getCVRQualifiers();
3772 return 0;
3773}
3774
3775//----------------------------------------------------------------------
3776// Creating related types
3777//----------------------------------------------------------------------
3778
Greg Claytona1e5dc82015-08-11 22:53:00 +00003779CompilerType
3780ClangASTContext::AddConstModifier (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003781{
3782 if (type && type.GetTypeSystem()->AsClangASTContext())
3783 {
3784 clang::QualType result(GetQualType(type));
3785 result.addConst();
Greg Claytona1e5dc82015-08-11 22:53:00 +00003786 return CompilerType (type.GetTypeSystem(), result.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003787 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003788 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003789}
3790
Greg Claytona1e5dc82015-08-11 22:53:00 +00003791CompilerType
3792ClangASTContext::AddRestrictModifier (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003793{
3794 if (type && type.GetTypeSystem()->AsClangASTContext())
3795 {
3796 clang::QualType result(GetQualType(type));
3797 result.getQualifiers().setRestrict (true);
Greg Claytona1e5dc82015-08-11 22:53:00 +00003798 return CompilerType (type.GetTypeSystem(), result.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003799 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003800 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003801}
3802
Greg Claytona1e5dc82015-08-11 22:53:00 +00003803CompilerType
3804ClangASTContext::AddVolatileModifier (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003805{
3806 if (type && type.GetTypeSystem()->AsClangASTContext())
3807 {
3808 clang::QualType result(GetQualType(type));
3809 result.getQualifiers().setVolatile (true);
Greg Claytona1e5dc82015-08-11 22:53:00 +00003810 return CompilerType (type.GetTypeSystem(), result.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003811 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003812 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003813}
3814
Greg Claytona1e5dc82015-08-11 22:53:00 +00003815CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003816ClangASTContext::GetArrayElementType (void* type, uint64_t *stride)
3817{
3818 if (type)
3819 {
3820 clang::QualType qual_type(GetCanonicalQualType(type));
3821
3822 const clang::Type *array_eletype = qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
3823
3824 if (!array_eletype)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003825 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003826
Greg Claytona1e5dc82015-08-11 22:53:00 +00003827 CompilerType element_type (getASTContext(), array_eletype->getCanonicalTypeUnqualified());
Greg Claytond8d4a572015-08-11 21:38:15 +00003828
3829 // TODO: the real stride will be >= this value.. find the real one!
3830 if (stride)
3831 *stride = element_type.GetByteSize(nullptr);
3832
3833 return element_type;
3834
3835 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003836 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003837}
3838
Greg Claytona1e5dc82015-08-11 22:53:00 +00003839CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003840ClangASTContext::GetCanonicalType (void* type)
3841{
3842 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003843 return CompilerType (getASTContext(), GetCanonicalQualType(type));
3844 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003845}
3846
3847static clang::QualType
3848GetFullyUnqualifiedType_Impl (clang::ASTContext *ast, clang::QualType qual_type)
3849{
3850 if (qual_type->isPointerType())
3851 qual_type = ast->getPointerType(GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
3852 else
3853 qual_type = qual_type.getUnqualifiedType();
3854 qual_type.removeLocalConst();
3855 qual_type.removeLocalRestrict();
3856 qual_type.removeLocalVolatile();
3857 return qual_type;
3858}
3859
Greg Claytona1e5dc82015-08-11 22:53:00 +00003860CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003861ClangASTContext::GetFullyUnqualifiedType (void* type)
3862{
3863 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003864 return CompilerType(getASTContext(), GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
3865 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003866}
3867
3868
3869int
3870ClangASTContext::GetFunctionArgumentCount (void* type)
3871{
3872 if (type)
3873 {
3874 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
3875 if (func)
3876 return func->getNumParams();
3877 }
3878 return -1;
3879}
3880
Greg Claytona1e5dc82015-08-11 22:53:00 +00003881CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003882ClangASTContext::GetFunctionArgumentTypeAtIndex (void* type, size_t idx)
3883{
3884 if (type)
3885 {
3886 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
3887 if (func)
3888 {
3889 const uint32_t num_args = func->getNumParams();
3890 if (idx < num_args)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003891 return CompilerType(getASTContext(), func->getParamType(idx));
Greg Claytond8d4a572015-08-11 21:38:15 +00003892 }
3893 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003894 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003895}
3896
Greg Claytona1e5dc82015-08-11 22:53:00 +00003897CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003898ClangASTContext::GetFunctionReturnType (void* type)
3899{
3900 if (type)
3901 {
3902 clang::QualType qual_type(GetCanonicalQualType(type));
3903 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3904 if (func)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003905 return CompilerType(getASTContext(), func->getReturnType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003906 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003907 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003908}
3909
3910size_t
3911ClangASTContext::GetNumMemberFunctions (void* type)
3912{
3913 size_t num_functions = 0;
3914 if (type)
3915 {
3916 clang::QualType qual_type(GetCanonicalQualType(type));
3917 switch (qual_type->getTypeClass()) {
3918 case clang::Type::Record:
3919 if (GetCompleteQualType (getASTContext(), qual_type))
3920 {
3921 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3922 const clang::RecordDecl *record_decl = record_type->getDecl();
3923 assert(record_decl);
3924 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3925 if (cxx_record_decl)
3926 num_functions = std::distance(cxx_record_decl->method_begin(), cxx_record_decl->method_end());
3927 }
3928 break;
3929
3930 case clang::Type::ObjCObjectPointer:
3931 if (GetCompleteType(type))
3932 {
3933 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
3934 if (objc_class_type)
3935 {
3936 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
3937 if (class_interface_decl)
3938 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
3939 }
3940 }
3941 break;
3942
3943 case clang::Type::ObjCObject:
3944 case clang::Type::ObjCInterface:
3945 if (GetCompleteType(type))
3946 {
3947 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
3948 if (objc_class_type)
3949 {
3950 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3951 if (class_interface_decl)
3952 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
3953 }
3954 }
3955 break;
3956
3957
3958 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003959 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00003960
3961 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003962 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00003963
3964 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003965 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00003966
3967 default:
3968 break;
3969 }
3970 }
3971 return num_functions;
3972}
3973
3974TypeMemberFunctionImpl
3975ClangASTContext::GetMemberFunctionAtIndex (void* type, size_t idx)
3976{
3977 std::string name("");
3978 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
Greg Claytona1e5dc82015-08-11 22:53:00 +00003979 CompilerType clang_type{};
Greg Claytond8d4a572015-08-11 21:38:15 +00003980 clang::ObjCMethodDecl *method_decl(nullptr);
3981 if (type)
3982 {
3983 clang::QualType qual_type(GetCanonicalQualType(type));
3984 switch (qual_type->getTypeClass()) {
3985 case clang::Type::Record:
3986 if (GetCompleteQualType (getASTContext(), qual_type))
3987 {
3988 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3989 const clang::RecordDecl *record_decl = record_type->getDecl();
3990 assert(record_decl);
3991 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3992 if (cxx_record_decl)
3993 {
3994 auto method_iter = cxx_record_decl->method_begin();
3995 auto method_end = cxx_record_decl->method_end();
3996 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
3997 {
3998 std::advance(method_iter, idx);
3999 auto method_decl = method_iter->getCanonicalDecl();
4000 if (method_decl)
4001 {
4002 if (!method_decl->getName().empty())
4003 name.assign(method_decl->getName().data());
4004 else
4005 name.clear();
4006 if (method_decl->isStatic())
4007 kind = lldb::eMemberFunctionKindStaticMethod;
4008 else if (llvm::isa<clang::CXXConstructorDecl>(method_decl))
4009 kind = lldb::eMemberFunctionKindConstructor;
4010 else if (llvm::isa<clang::CXXDestructorDecl>(method_decl))
4011 kind = lldb::eMemberFunctionKindDestructor;
4012 else
4013 kind = lldb::eMemberFunctionKindInstanceMethod;
Greg Claytona1e5dc82015-08-11 22:53:00 +00004014 clang_type = CompilerType(getASTContext(),method_decl->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004015 }
4016 }
4017 }
4018 }
4019 break;
4020
4021 case clang::Type::ObjCObjectPointer:
4022 if (GetCompleteType(type))
4023 {
4024 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4025 if (objc_class_type)
4026 {
4027 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4028 if (class_interface_decl)
4029 {
4030 auto method_iter = class_interface_decl->meth_begin();
4031 auto method_end = class_interface_decl->meth_end();
4032 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4033 {
4034 std::advance(method_iter, idx);
4035 method_decl = method_iter->getCanonicalDecl();
4036 if (method_decl)
4037 {
4038 name = method_decl->getSelector().getAsString();
4039 if (method_decl->isClassMethod())
4040 kind = lldb::eMemberFunctionKindStaticMethod;
4041 else
4042 kind = lldb::eMemberFunctionKindInstanceMethod;
4043 }
4044 }
4045 }
4046 }
4047 }
4048 break;
4049
4050 case clang::Type::ObjCObject:
4051 case clang::Type::ObjCInterface:
4052 if (GetCompleteType(type))
4053 {
4054 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4055 if (objc_class_type)
4056 {
4057 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4058 if (class_interface_decl)
4059 {
4060 auto method_iter = class_interface_decl->meth_begin();
4061 auto method_end = class_interface_decl->meth_end();
4062 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4063 {
4064 std::advance(method_iter, idx);
4065 method_decl = method_iter->getCanonicalDecl();
4066 if (method_decl)
4067 {
4068 name = method_decl->getSelector().getAsString();
4069 if (method_decl->isClassMethod())
4070 kind = lldb::eMemberFunctionKindStaticMethod;
4071 else
4072 kind = lldb::eMemberFunctionKindInstanceMethod;
4073 }
4074 }
4075 }
4076 }
4077 }
4078 break;
4079
4080 case clang::Type::Typedef:
4081 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx);
4082
4083 case clang::Type::Elaborated:
4084 return GetMemberFunctionAtIndex(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx);
4085
4086 case clang::Type::Paren:
4087 return GetMemberFunctionAtIndex(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx);
4088
4089 default:
4090 break;
4091 }
4092 }
4093
4094 if (kind == eMemberFunctionKindUnknown)
4095 return TypeMemberFunctionImpl();
4096 if (method_decl)
4097 return TypeMemberFunctionImpl(method_decl, name, kind);
4098 if (type)
4099 return TypeMemberFunctionImpl(clang_type, name, kind);
4100
4101 return TypeMemberFunctionImpl();
4102}
4103
Greg Claytona1e5dc82015-08-11 22:53:00 +00004104CompilerType
4105ClangASTContext::GetLValueReferenceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004106{
4107 if (type)
4108 {
4109 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
4110 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004111 return CompilerType(ast->getASTContext(), ast->getASTContext()->getLValueReferenceType(GetQualType(type)));
Greg Claytond8d4a572015-08-11 21:38:15 +00004112 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004113 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004114}
4115
Greg Claytona1e5dc82015-08-11 22:53:00 +00004116CompilerType
4117ClangASTContext::GetRValueReferenceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004118{
4119 if (type)
4120 {
4121 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
4122 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004123 return CompilerType(ast->getASTContext(), ast->getASTContext()->getRValueReferenceType(GetQualType(type)));
Greg Claytond8d4a572015-08-11 21:38:15 +00004124 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004125 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004126}
4127
Greg Claytona1e5dc82015-08-11 22:53:00 +00004128CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004129ClangASTContext::GetNonReferenceType (void* type)
4130{
4131 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004132 return CompilerType(getASTContext(), GetQualType(type).getNonReferenceType());
4133 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004134}
4135
Greg Claytona1e5dc82015-08-11 22:53:00 +00004136CompilerType
4137ClangASTContext::CreateTypedefType (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00004138 const char *typedef_name,
Greg Clayton99558cc42015-08-24 23:46:31 +00004139 const CompilerDeclContext &compiler_decl_ctx)
Greg Claytond8d4a572015-08-11 21:38:15 +00004140{
4141 if (type && typedef_name && typedef_name[0])
4142 {
4143 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
4144 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004145 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004146 clang::ASTContext* clang_ast = ast->getASTContext();
4147 clang::QualType qual_type (GetQualType(type));
Greg Clayton99558cc42015-08-24 23:46:31 +00004148
4149 clang::DeclContext *decl_ctx = ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
Greg Claytond8d4a572015-08-11 21:38:15 +00004150 if (decl_ctx == nullptr)
4151 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
Greg Clayton99558cc42015-08-24 23:46:31 +00004152
Greg Claytond8d4a572015-08-11 21:38:15 +00004153 clang::TypedefDecl *decl = clang::TypedefDecl::Create (*clang_ast,
4154 decl_ctx,
4155 clang::SourceLocation(),
4156 clang::SourceLocation(),
4157 &clang_ast->Idents.get(typedef_name),
4158 clang_ast->getTrivialTypeSourceInfo(qual_type));
4159
4160 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4161
4162 // Get a uniqued clang::QualType for the typedef decl type
Greg Claytona1e5dc82015-08-11 22:53:00 +00004163 return CompilerType (clang_ast, clang_ast->getTypedefType (decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00004164 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004165 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004166
4167}
4168
Greg Claytona1e5dc82015-08-11 22:53:00 +00004169CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004170ClangASTContext::GetPointeeType (void* type)
4171{
4172 if (type)
4173 {
4174 clang::QualType qual_type(GetQualType(type));
Greg Claytona1e5dc82015-08-11 22:53:00 +00004175 return CompilerType (getASTContext(), qual_type.getTypePtr()->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004176 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004177 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004178}
4179
Greg Claytona1e5dc82015-08-11 22:53:00 +00004180CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004181ClangASTContext::GetPointerType (void* type)
4182{
4183 if (type)
4184 {
4185 clang::QualType qual_type (GetQualType(type));
4186
4187 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4188 switch (type_class)
4189 {
4190 case clang::Type::ObjCObject:
4191 case clang::Type::ObjCInterface:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004192 return CompilerType(getASTContext(), getASTContext()->getObjCObjectPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004193
4194 default:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004195 return CompilerType(getASTContext(), getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004196 }
4197 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004198 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004199}
4200
Greg Claytona1e5dc82015-08-11 22:53:00 +00004201CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004202ClangASTContext::GetTypedefedType (void* type)
4203{
4204 if (type)
4205 {
4206 const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4207 if (typedef_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004208 return CompilerType (getASTContext(), typedef_type->getDecl()->getUnderlyingType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004209 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004210 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004211}
4212
Greg Claytona1e5dc82015-08-11 22:53:00 +00004213CompilerType
4214ClangASTContext::RemoveFastQualifiers (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004215{
4216 if (type && type.GetTypeSystem()->AsClangASTContext())
4217 {
4218 clang::QualType qual_type(GetQualType(type));
4219 qual_type.getQualifiers().removeFastQualifiers();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004220 return CompilerType (type.GetTypeSystem(), qual_type.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00004221 }
4222 return type;
4223}
4224
4225
4226//----------------------------------------------------------------------
4227// Create related types using the current type's AST
4228//----------------------------------------------------------------------
4229
Greg Claytona1e5dc82015-08-11 22:53:00 +00004230CompilerType
Greg Clayton99558cc42015-08-24 23:46:31 +00004231ClangASTContext::GetBasicTypeFromAST (lldb::BasicType basic_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004232{
Greg Clayton99558cc42015-08-24 23:46:31 +00004233 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004234}
4235//----------------------------------------------------------------------
4236// Exploring the type
4237//----------------------------------------------------------------------
4238
4239uint64_t
4240ClangASTContext::GetBitSize (void* type, ExecutionContextScope *exe_scope)
4241{
4242 if (GetCompleteType (type))
4243 {
4244 clang::QualType qual_type(GetCanonicalQualType(type));
4245 switch (qual_type->getTypeClass())
4246 {
4247 case clang::Type::ObjCInterface:
4248 case clang::Type::ObjCObject:
4249 {
4250 ExecutionContext exe_ctx (exe_scope);
4251 Process *process = exe_ctx.GetProcessPtr();
4252 if (process)
4253 {
4254 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4255 if (objc_runtime)
4256 {
4257 uint64_t bit_size = 0;
Greg Claytona1e5dc82015-08-11 22:53:00 +00004258 if (objc_runtime->GetTypeBitSize(CompilerType(getASTContext(), qual_type), bit_size))
Greg Claytond8d4a572015-08-11 21:38:15 +00004259 return bit_size;
4260 }
4261 }
4262 else
4263 {
4264 static bool g_printed = false;
4265 if (!g_printed)
4266 {
4267 StreamString s;
4268 DumpTypeDescription(&s);
4269
4270 llvm::outs() << "warning: trying to determine the size of type ";
4271 llvm::outs() << s.GetString() << "\n";
4272 llvm::outs() << "without a valid ExecutionContext. this is not reliable. please file a bug against LLDB.\n";
4273 llvm::outs() << "backtrace:\n";
4274 llvm::sys::PrintStackTrace(llvm::outs());
4275 llvm::outs() << "\n";
4276 g_printed = true;
4277 }
4278 }
4279 }
4280 // fallthrough
4281 default:
4282 const uint32_t bit_size = getASTContext()->getTypeSize (qual_type);
4283 if (bit_size == 0)
4284 {
4285 if (qual_type->isIncompleteArrayType())
4286 return getASTContext()->getTypeSize (qual_type->getArrayElementTypeNoTypeQual()->getCanonicalTypeUnqualified());
4287 }
4288 if (qual_type->isObjCObjectOrInterfaceType())
4289 return bit_size + getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy);
4290 return bit_size;
4291 }
4292 }
4293 return 0;
4294}
4295
4296size_t
4297ClangASTContext::GetTypeBitAlign (void* type)
4298{
4299 if (GetCompleteType(type))
4300 return getASTContext()->getTypeAlign(GetQualType(type));
4301 return 0;
4302}
4303
4304
4305lldb::Encoding
4306ClangASTContext::GetEncoding (void* type, uint64_t &count)
4307{
4308 if (!type)
4309 return lldb::eEncodingInvalid;
4310
4311 count = 1;
4312 clang::QualType qual_type(GetCanonicalQualType(type));
4313
4314 switch (qual_type->getTypeClass())
4315 {
4316 case clang::Type::UnaryTransform:
4317 break;
4318
4319 case clang::Type::FunctionNoProto:
4320 case clang::Type::FunctionProto:
4321 break;
4322
4323 case clang::Type::IncompleteArray:
4324 case clang::Type::VariableArray:
4325 break;
4326
4327 case clang::Type::ConstantArray:
4328 break;
4329
4330 case clang::Type::ExtVector:
4331 case clang::Type::Vector:
4332 // TODO: Set this to more than one???
4333 break;
4334
4335 case clang::Type::Builtin:
4336 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4337 {
4338 default: assert(0 && "Unknown builtin type!");
4339 case clang::BuiltinType::Void:
4340 break;
4341
4342 case clang::BuiltinType::Bool:
4343 case clang::BuiltinType::Char_S:
4344 case clang::BuiltinType::SChar:
4345 case clang::BuiltinType::WChar_S:
4346 case clang::BuiltinType::Char16:
4347 case clang::BuiltinType::Char32:
4348 case clang::BuiltinType::Short:
4349 case clang::BuiltinType::Int:
4350 case clang::BuiltinType::Long:
4351 case clang::BuiltinType::LongLong:
4352 case clang::BuiltinType::Int128: return lldb::eEncodingSint;
4353
4354 case clang::BuiltinType::Char_U:
4355 case clang::BuiltinType::UChar:
4356 case clang::BuiltinType::WChar_U:
4357 case clang::BuiltinType::UShort:
4358 case clang::BuiltinType::UInt:
4359 case clang::BuiltinType::ULong:
4360 case clang::BuiltinType::ULongLong:
4361 case clang::BuiltinType::UInt128: return lldb::eEncodingUint;
4362
4363 case clang::BuiltinType::Float:
4364 case clang::BuiltinType::Double:
4365 case clang::BuiltinType::LongDouble: return lldb::eEncodingIEEE754;
4366
4367 case clang::BuiltinType::ObjCClass:
4368 case clang::BuiltinType::ObjCId:
4369 case clang::BuiltinType::ObjCSel: return lldb::eEncodingUint;
4370
4371 case clang::BuiltinType::NullPtr: return lldb::eEncodingUint;
4372
4373 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4374 case clang::BuiltinType::Kind::BoundMember:
4375 case clang::BuiltinType::Kind::BuiltinFn:
4376 case clang::BuiltinType::Kind::Dependent:
4377 case clang::BuiltinType::Kind::Half:
4378 case clang::BuiltinType::Kind::OCLEvent:
4379 case clang::BuiltinType::Kind::OCLImage1d:
4380 case clang::BuiltinType::Kind::OCLImage1dArray:
4381 case clang::BuiltinType::Kind::OCLImage1dBuffer:
4382 case clang::BuiltinType::Kind::OCLImage2d:
4383 case clang::BuiltinType::Kind::OCLImage2dArray:
4384 case clang::BuiltinType::Kind::OCLImage3d:
4385 case clang::BuiltinType::Kind::OCLSampler:
4386 case clang::BuiltinType::Kind::Overload:
4387 case clang::BuiltinType::Kind::PseudoObject:
4388 case clang::BuiltinType::Kind::UnknownAny:
4389 break;
4390 }
4391 break;
4392 // All pointer types are represented as unsigned integer encodings.
4393 // We may nee to add a eEncodingPointer if we ever need to know the
4394 // difference
4395 case clang::Type::ObjCObjectPointer:
4396 case clang::Type::BlockPointer:
4397 case clang::Type::Pointer:
4398 case clang::Type::LValueReference:
4399 case clang::Type::RValueReference:
4400 case clang::Type::MemberPointer: return lldb::eEncodingUint;
4401 case clang::Type::Complex:
4402 {
4403 lldb::Encoding encoding = lldb::eEncodingIEEE754;
4404 if (qual_type->isComplexType())
4405 encoding = lldb::eEncodingIEEE754;
4406 else
4407 {
4408 const clang::ComplexType *complex_type = qual_type->getAsComplexIntegerType();
4409 if (complex_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004410 encoding = CompilerType(getASTContext(), complex_type->getElementType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004411 else
4412 encoding = lldb::eEncodingSint;
4413 }
4414 count = 2;
4415 return encoding;
4416 }
4417
4418 case clang::Type::ObjCInterface: break;
4419 case clang::Type::Record: break;
4420 case clang::Type::Enum: return lldb::eEncodingSint;
4421 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004422 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004423
4424 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004425 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004426
4427 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004428 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004429
4430 case clang::Type::DependentSizedArray:
4431 case clang::Type::DependentSizedExtVector:
4432 case clang::Type::UnresolvedUsing:
4433 case clang::Type::Attributed:
4434 case clang::Type::TemplateTypeParm:
4435 case clang::Type::SubstTemplateTypeParm:
4436 case clang::Type::SubstTemplateTypeParmPack:
4437 case clang::Type::Auto:
4438 case clang::Type::InjectedClassName:
4439 case clang::Type::DependentName:
4440 case clang::Type::DependentTemplateSpecialization:
4441 case clang::Type::PackExpansion:
4442 case clang::Type::ObjCObject:
4443
4444 case clang::Type::TypeOfExpr:
4445 case clang::Type::TypeOf:
4446 case clang::Type::Decltype:
4447 case clang::Type::TemplateSpecialization:
4448 case clang::Type::Atomic:
4449 case clang::Type::Adjusted:
4450 break;
4451
4452 // pointer type decayed from an array or function type.
4453 case clang::Type::Decayed:
4454 break;
4455 }
4456 count = 0;
4457 return lldb::eEncodingInvalid;
4458}
4459
4460lldb::Format
4461ClangASTContext::GetFormat (void* type)
4462{
4463 if (!type)
4464 return lldb::eFormatDefault;
4465
4466 clang::QualType qual_type(GetCanonicalQualType(type));
4467
4468 switch (qual_type->getTypeClass())
4469 {
4470 case clang::Type::UnaryTransform:
4471 break;
4472
4473 case clang::Type::FunctionNoProto:
4474 case clang::Type::FunctionProto:
4475 break;
4476
4477 case clang::Type::IncompleteArray:
4478 case clang::Type::VariableArray:
4479 break;
4480
4481 case clang::Type::ConstantArray:
4482 return lldb::eFormatVoid; // no value
4483
4484 case clang::Type::ExtVector:
4485 case clang::Type::Vector:
4486 break;
4487
4488 case clang::Type::Builtin:
4489 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4490 {
4491 //default: assert(0 && "Unknown builtin type!");
4492 case clang::BuiltinType::UnknownAny:
4493 case clang::BuiltinType::Void:
4494 case clang::BuiltinType::BoundMember:
4495 break;
4496
4497 case clang::BuiltinType::Bool: return lldb::eFormatBoolean;
4498 case clang::BuiltinType::Char_S:
4499 case clang::BuiltinType::SChar:
4500 case clang::BuiltinType::WChar_S:
4501 case clang::BuiltinType::Char_U:
4502 case clang::BuiltinType::UChar:
4503 case clang::BuiltinType::WChar_U: return lldb::eFormatChar;
4504 case clang::BuiltinType::Char16: return lldb::eFormatUnicode16;
4505 case clang::BuiltinType::Char32: return lldb::eFormatUnicode32;
4506 case clang::BuiltinType::UShort: return lldb::eFormatUnsigned;
4507 case clang::BuiltinType::Short: return lldb::eFormatDecimal;
4508 case clang::BuiltinType::UInt: return lldb::eFormatUnsigned;
4509 case clang::BuiltinType::Int: return lldb::eFormatDecimal;
4510 case clang::BuiltinType::ULong: return lldb::eFormatUnsigned;
4511 case clang::BuiltinType::Long: return lldb::eFormatDecimal;
4512 case clang::BuiltinType::ULongLong: return lldb::eFormatUnsigned;
4513 case clang::BuiltinType::LongLong: return lldb::eFormatDecimal;
4514 case clang::BuiltinType::UInt128: return lldb::eFormatUnsigned;
4515 case clang::BuiltinType::Int128: return lldb::eFormatDecimal;
4516 case clang::BuiltinType::Float: return lldb::eFormatFloat;
4517 case clang::BuiltinType::Double: return lldb::eFormatFloat;
4518 case clang::BuiltinType::LongDouble: return lldb::eFormatFloat;
4519 case clang::BuiltinType::NullPtr:
4520 case clang::BuiltinType::Overload:
4521 case clang::BuiltinType::Dependent:
4522 case clang::BuiltinType::ObjCId:
4523 case clang::BuiltinType::ObjCClass:
4524 case clang::BuiltinType::ObjCSel:
4525 case clang::BuiltinType::Half:
4526 case clang::BuiltinType::ARCUnbridgedCast:
4527 case clang::BuiltinType::PseudoObject:
4528 case clang::BuiltinType::BuiltinFn:
4529 case clang::BuiltinType::OCLEvent:
4530 case clang::BuiltinType::OCLImage1d:
4531 case clang::BuiltinType::OCLImage1dArray:
4532 case clang::BuiltinType::OCLImage1dBuffer:
4533 case clang::BuiltinType::OCLImage2d:
4534 case clang::BuiltinType::OCLImage2dArray:
4535 case clang::BuiltinType::OCLImage3d:
4536 case clang::BuiltinType::OCLSampler:
4537 return lldb::eFormatHex;
4538 }
4539 break;
4540 case clang::Type::ObjCObjectPointer: return lldb::eFormatHex;
4541 case clang::Type::BlockPointer: return lldb::eFormatHex;
4542 case clang::Type::Pointer: return lldb::eFormatHex;
4543 case clang::Type::LValueReference:
4544 case clang::Type::RValueReference: return lldb::eFormatHex;
4545 case clang::Type::MemberPointer: break;
4546 case clang::Type::Complex:
4547 {
4548 if (qual_type->isComplexType())
4549 return lldb::eFormatComplex;
4550 else
4551 return lldb::eFormatComplexInteger;
4552 }
4553 case clang::Type::ObjCInterface: break;
4554 case clang::Type::Record: break;
4555 case clang::Type::Enum: return lldb::eFormatEnum;
4556 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004557 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004558 case clang::Type::Auto:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004559 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004560 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004561 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004562 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004563 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004564 case clang::Type::DependentSizedArray:
4565 case clang::Type::DependentSizedExtVector:
4566 case clang::Type::UnresolvedUsing:
4567 case clang::Type::Attributed:
4568 case clang::Type::TemplateTypeParm:
4569 case clang::Type::SubstTemplateTypeParm:
4570 case clang::Type::SubstTemplateTypeParmPack:
4571 case clang::Type::InjectedClassName:
4572 case clang::Type::DependentName:
4573 case clang::Type::DependentTemplateSpecialization:
4574 case clang::Type::PackExpansion:
4575 case clang::Type::ObjCObject:
4576
4577 case clang::Type::TypeOfExpr:
4578 case clang::Type::TypeOf:
4579 case clang::Type::Decltype:
4580 case clang::Type::TemplateSpecialization:
4581 case clang::Type::Atomic:
4582 case clang::Type::Adjusted:
4583 break;
4584
4585 // pointer type decayed from an array or function type.
4586 case clang::Type::Decayed:
4587 break;
4588 }
4589 // We don't know hot to display this type...
4590 return lldb::eFormatBytes;
4591}
4592
4593static bool
4594ObjCDeclHasIVars (clang::ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
4595{
4596 while (class_interface_decl)
4597 {
4598 if (class_interface_decl->ivar_size() > 0)
4599 return true;
4600
4601 if (check_superclass)
4602 class_interface_decl = class_interface_decl->getSuperClass();
4603 else
4604 break;
4605 }
4606 return false;
4607}
4608
4609uint32_t
4610ClangASTContext::GetNumChildren (void* type, bool omit_empty_base_classes)
4611{
4612 if (!type)
4613 return 0;
4614
4615 uint32_t num_children = 0;
4616 clang::QualType qual_type(GetQualType(type));
4617 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4618 switch (type_class)
4619 {
4620 case clang::Type::Builtin:
4621 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4622 {
4623 case clang::BuiltinType::ObjCId: // child is Class
4624 case clang::BuiltinType::ObjCClass: // child is Class
4625 num_children = 1;
4626 break;
4627
4628 default:
4629 break;
4630 }
4631 break;
4632
4633 case clang::Type::Complex: return 0;
4634
4635 case clang::Type::Record:
4636 if (GetCompleteQualType (getASTContext(), qual_type))
4637 {
4638 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4639 const clang::RecordDecl *record_decl = record_type->getDecl();
4640 assert(record_decl);
4641 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4642 if (cxx_record_decl)
4643 {
4644 if (omit_empty_base_classes)
4645 {
4646 // Check each base classes to see if it or any of its
4647 // base classes contain any fields. This can help
4648 // limit the noise in variable views by not having to
4649 // show base classes that contain no members.
4650 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4651 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4652 base_class != base_class_end;
4653 ++base_class)
4654 {
4655 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
4656
4657 // Skip empty base classes
4658 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
4659 continue;
4660
4661 num_children++;
4662 }
4663 }
4664 else
4665 {
4666 // Include all base classes
4667 num_children += cxx_record_decl->getNumBases();
4668 }
4669
4670 }
4671 clang::RecordDecl::field_iterator field, field_end;
4672 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
4673 ++num_children;
4674 }
4675 break;
4676
4677 case clang::Type::ObjCObject:
4678 case clang::Type::ObjCInterface:
4679 if (GetCompleteQualType (getASTContext(), qual_type))
4680 {
4681 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4682 assert (objc_class_type);
4683 if (objc_class_type)
4684 {
4685 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4686
4687 if (class_interface_decl)
4688 {
4689
4690 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4691 if (superclass_interface_decl)
4692 {
4693 if (omit_empty_base_classes)
4694 {
4695 if (ObjCDeclHasIVars (superclass_interface_decl, true))
4696 ++num_children;
4697 }
4698 else
4699 ++num_children;
4700 }
4701
4702 num_children += class_interface_decl->ivar_size();
4703 }
4704 }
4705 }
4706 break;
4707
4708 case clang::Type::ObjCObjectPointer:
4709 {
4710 const clang::ObjCObjectPointerType *pointer_type = llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
4711 clang::QualType pointee_type = pointer_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004712 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004713 // If this type points to a simple type, then it has 1 child
4714 if (num_pointee_children == 0)
4715 num_children = 1;
4716 else
4717 num_children = num_pointee_children;
4718 }
4719 break;
4720
4721 case clang::Type::Vector:
4722 case clang::Type::ExtVector:
4723 num_children = llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
4724 break;
4725
4726 case clang::Type::ConstantArray:
4727 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
4728 break;
4729
4730 case clang::Type::Pointer:
4731 {
4732 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
4733 clang::QualType pointee_type (pointer_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00004734 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004735 if (num_pointee_children == 0)
4736 {
4737 // We have a pointer to a pointee type that claims it has no children.
4738 // We will want to look at
4739 num_children = GetNumPointeeChildren (pointee_type);
4740 }
4741 else
4742 num_children = num_pointee_children;
4743 }
4744 break;
4745
4746 case clang::Type::LValueReference:
4747 case clang::Type::RValueReference:
4748 {
4749 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
4750 clang::QualType pointee_type = reference_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004751 uint32_t num_pointee_children = CompilerType (getASTContext(), pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004752 // If this type points to a simple type, then it has 1 child
4753 if (num_pointee_children == 0)
4754 num_children = 1;
4755 else
4756 num_children = num_pointee_children;
4757 }
4758 break;
4759
4760
4761 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004762 num_children = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004763 break;
4764
4765 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004766 num_children = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004767 break;
4768
4769 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004770 num_children = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004771 break;
4772 default:
4773 break;
4774 }
4775 return num_children;
4776}
4777
4778lldb::BasicType
4779ClangASTContext::GetBasicTypeEnumeration (void* type)
4780{
4781 if (type)
4782 {
4783 clang::QualType qual_type(GetQualType(type));
4784 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4785 if (type_class == clang::Type::Builtin)
4786 {
4787 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4788 {
4789 case clang::BuiltinType::Void: return eBasicTypeVoid;
4790 case clang::BuiltinType::Bool: return eBasicTypeBool;
4791 case clang::BuiltinType::Char_S: return eBasicTypeSignedChar;
4792 case clang::BuiltinType::Char_U: return eBasicTypeUnsignedChar;
4793 case clang::BuiltinType::Char16: return eBasicTypeChar16;
4794 case clang::BuiltinType::Char32: return eBasicTypeChar32;
4795 case clang::BuiltinType::UChar: return eBasicTypeUnsignedChar;
4796 case clang::BuiltinType::SChar: return eBasicTypeSignedChar;
4797 case clang::BuiltinType::WChar_S: return eBasicTypeSignedWChar;
4798 case clang::BuiltinType::WChar_U: return eBasicTypeUnsignedWChar;
4799 case clang::BuiltinType::Short: return eBasicTypeShort;
4800 case clang::BuiltinType::UShort: return eBasicTypeUnsignedShort;
4801 case clang::BuiltinType::Int: return eBasicTypeInt;
4802 case clang::BuiltinType::UInt: return eBasicTypeUnsignedInt;
4803 case clang::BuiltinType::Long: return eBasicTypeLong;
4804 case clang::BuiltinType::ULong: return eBasicTypeUnsignedLong;
4805 case clang::BuiltinType::LongLong: return eBasicTypeLongLong;
4806 case clang::BuiltinType::ULongLong: return eBasicTypeUnsignedLongLong;
4807 case clang::BuiltinType::Int128: return eBasicTypeInt128;
4808 case clang::BuiltinType::UInt128: return eBasicTypeUnsignedInt128;
4809
4810 case clang::BuiltinType::Half: return eBasicTypeHalf;
4811 case clang::BuiltinType::Float: return eBasicTypeFloat;
4812 case clang::BuiltinType::Double: return eBasicTypeDouble;
4813 case clang::BuiltinType::LongDouble:return eBasicTypeLongDouble;
4814
4815 case clang::BuiltinType::NullPtr: return eBasicTypeNullPtr;
4816 case clang::BuiltinType::ObjCId: return eBasicTypeObjCID;
4817 case clang::BuiltinType::ObjCClass: return eBasicTypeObjCClass;
4818 case clang::BuiltinType::ObjCSel: return eBasicTypeObjCSel;
4819 case clang::BuiltinType::Dependent:
4820 case clang::BuiltinType::Overload:
4821 case clang::BuiltinType::BoundMember:
4822 case clang::BuiltinType::PseudoObject:
4823 case clang::BuiltinType::UnknownAny:
4824 case clang::BuiltinType::BuiltinFn:
4825 case clang::BuiltinType::ARCUnbridgedCast:
4826 case clang::BuiltinType::OCLEvent:
4827 case clang::BuiltinType::OCLImage1d:
4828 case clang::BuiltinType::OCLImage1dArray:
4829 case clang::BuiltinType::OCLImage1dBuffer:
4830 case clang::BuiltinType::OCLImage2d:
4831 case clang::BuiltinType::OCLImage2dArray:
4832 case clang::BuiltinType::OCLImage3d:
4833 case clang::BuiltinType::OCLSampler:
4834 return eBasicTypeOther;
4835 }
4836 }
4837 }
4838 return eBasicTypeInvalid;
4839}
4840
Greg Clayton99558cc42015-08-24 23:46:31 +00004841void
4842ClangASTContext::ForEachEnumerator (void* type, std::function <bool (const CompilerType &integer_type, const ConstString &name, const llvm::APSInt &value)> const &callback)
4843{
4844 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
4845 if (enum_type)
4846 {
4847 const clang::EnumDecl *enum_decl = enum_type->getDecl();
4848 if (enum_decl)
4849 {
4850 CompilerType integer_type(this, enum_decl->getIntegerType().getAsOpaquePtr());
4851
4852 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
4853 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
4854 {
4855 ConstString name(enum_pos->getNameAsString().c_str());
4856 if (!callback (integer_type, name, enum_pos->getInitVal()))
4857 break;
4858 }
4859 }
4860 }
4861}
4862
Greg Claytond8d4a572015-08-11 21:38:15 +00004863
4864#pragma mark Aggregate Types
4865
4866uint32_t
Greg Claytond8d4a572015-08-11 21:38:15 +00004867ClangASTContext::GetNumFields (void* type)
4868{
4869 if (!type)
4870 return 0;
4871
4872 uint32_t count = 0;
4873 clang::QualType qual_type(GetCanonicalQualType(type));
4874 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4875 switch (type_class)
4876 {
4877 case clang::Type::Record:
4878 if (GetCompleteType(type))
4879 {
4880 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
4881 if (record_type)
4882 {
4883 clang::RecordDecl *record_decl = record_type->getDecl();
4884 if (record_decl)
4885 {
4886 uint32_t field_idx = 0;
4887 clang::RecordDecl::field_iterator field, field_end;
4888 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
4889 ++field_idx;
4890 count = field_idx;
4891 }
4892 }
4893 }
4894 break;
4895
4896 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004897 count = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00004898 break;
4899
4900 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004901 count = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00004902 break;
4903
4904 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004905 count = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00004906 break;
4907
4908 case clang::Type::ObjCObjectPointer:
4909 if (GetCompleteType(type))
4910 {
4911 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4912 if (objc_class_type)
4913 {
4914 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4915
4916 if (class_interface_decl)
4917 count = class_interface_decl->ivar_size();
4918 }
4919 }
4920 break;
4921
4922 case clang::Type::ObjCObject:
4923 case clang::Type::ObjCInterface:
4924 if (GetCompleteType(type))
4925 {
4926 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4927 if (objc_class_type)
4928 {
4929 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4930
4931 if (class_interface_decl)
4932 count = class_interface_decl->ivar_size();
4933 }
4934 }
4935 break;
4936
4937 default:
4938 break;
4939 }
4940 return count;
4941}
4942
Greg Claytond8d4a572015-08-11 21:38:15 +00004943static clang_type_t
4944GetObjCFieldAtIndex (clang::ASTContext *ast,
4945 clang::ObjCInterfaceDecl *class_interface_decl,
4946 size_t idx,
4947 std::string& name,
4948 uint64_t *bit_offset_ptr,
4949 uint32_t *bitfield_bit_size_ptr,
4950 bool *is_bitfield_ptr)
4951{
4952 if (class_interface_decl)
4953 {
4954 if (idx < (class_interface_decl->ivar_size()))
4955 {
4956 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4957 uint32_t ivar_idx = 0;
4958
4959 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
4960 {
4961 if (ivar_idx == idx)
4962 {
4963 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
4964
4965 clang::QualType ivar_qual_type(ivar_decl->getType());
4966
4967 name.assign(ivar_decl->getNameAsString());
4968
4969 if (bit_offset_ptr)
4970 {
4971 const clang::ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
4972 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
4973 }
4974
4975 const bool is_bitfield = ivar_pos->isBitField();
4976
4977 if (bitfield_bit_size_ptr)
4978 {
4979 *bitfield_bit_size_ptr = 0;
4980
4981 if (is_bitfield && ast)
4982 {
4983 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
4984 llvm::APSInt bitfield_apsint;
4985 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
4986 {
4987 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
4988 }
4989 }
4990 }
4991 if (is_bitfield_ptr)
4992 *is_bitfield_ptr = is_bitfield;
4993
4994 return ivar_qual_type.getAsOpaquePtr();
4995 }
4996 }
4997 }
4998 }
4999 return nullptr;
5000}
5001
Greg Claytona1e5dc82015-08-11 22:53:00 +00005002CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00005003ClangASTContext::GetFieldAtIndex (void* type, size_t idx,
5004 std::string& name,
5005 uint64_t *bit_offset_ptr,
5006 uint32_t *bitfield_bit_size_ptr,
5007 bool *is_bitfield_ptr)
5008{
5009 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005010 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005011
5012 clang::QualType qual_type(GetCanonicalQualType(type));
5013 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5014 switch (type_class)
5015 {
5016 case clang::Type::Record:
5017 if (GetCompleteType(type))
5018 {
5019 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5020 const clang::RecordDecl *record_decl = record_type->getDecl();
5021 uint32_t field_idx = 0;
5022 clang::RecordDecl::field_iterator field, field_end;
5023 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
5024 {
5025 if (idx == field_idx)
5026 {
5027 // Print the member type if requested
5028 // Print the member name and equal sign
5029 name.assign(field->getNameAsString());
5030
5031 // Figure out the type byte size (field_type_info.first) and
5032 // alignment (field_type_info.second) from the AST context.
5033 if (bit_offset_ptr)
5034 {
5035 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5036 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
5037 }
5038
5039 const bool is_bitfield = field->isBitField();
5040
5041 if (bitfield_bit_size_ptr)
5042 {
5043 *bitfield_bit_size_ptr = 0;
5044
5045 if (is_bitfield)
5046 {
5047 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5048 llvm::APSInt bitfield_apsint;
5049 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *getASTContext()))
5050 {
5051 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5052 }
5053 }
5054 }
5055 if (is_bitfield_ptr)
5056 *is_bitfield_ptr = is_bitfield;
5057
Greg Claytona1e5dc82015-08-11 22:53:00 +00005058 return CompilerType (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005059 }
5060 }
5061 }
5062 break;
5063
5064 case clang::Type::ObjCObjectPointer:
5065 if (GetCompleteType(type))
5066 {
5067 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
5068 if (objc_class_type)
5069 {
5070 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005071 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 +00005072 }
5073 }
5074 break;
5075
5076 case clang::Type::ObjCObject:
5077 case clang::Type::ObjCInterface:
5078 if (GetCompleteType(type))
5079 {
5080 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5081 assert (objc_class_type);
5082 if (objc_class_type)
5083 {
5084 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005085 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 +00005086 }
5087 }
5088 break;
5089
5090
5091 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005092 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005093 GetFieldAtIndex (idx,
5094 name,
5095 bit_offset_ptr,
5096 bitfield_bit_size_ptr,
5097 is_bitfield_ptr);
5098
5099 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005100 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005101 GetFieldAtIndex (idx,
5102 name,
5103 bit_offset_ptr,
5104 bitfield_bit_size_ptr,
5105 is_bitfield_ptr);
5106
5107 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005108 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005109 GetFieldAtIndex (idx,
5110 name,
5111 bit_offset_ptr,
5112 bitfield_bit_size_ptr,
5113 is_bitfield_ptr);
5114
5115 default:
5116 break;
5117 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005118 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005119}
5120
Greg Clayton99558cc42015-08-24 23:46:31 +00005121uint32_t
5122ClangASTContext::GetNumDirectBaseClasses (void *type)
5123{
5124 uint32_t count = 0;
5125 clang::QualType qual_type(GetCanonicalQualType(type));
5126 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5127 switch (type_class)
5128 {
5129 case clang::Type::Record:
5130 if (GetCompleteType(type))
5131 {
5132 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5133 if (cxx_record_decl)
5134 count = cxx_record_decl->getNumBases();
5135 }
5136 break;
5137
5138 case clang::Type::ObjCObjectPointer:
5139 count = GetPointeeType(type).GetNumDirectBaseClasses();
5140 break;
5141
5142 case clang::Type::ObjCObject:
5143 if (GetCompleteType(type))
5144 {
5145 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
5146 if (objc_class_type)
5147 {
5148 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5149
5150 if (class_interface_decl && class_interface_decl->getSuperClass())
5151 count = 1;
5152 }
5153 }
5154 break;
5155 case clang::Type::ObjCInterface:
5156 if (GetCompleteType(type))
5157 {
5158 const clang::ObjCInterfaceType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
5159 if (objc_interface_type)
5160 {
5161 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
5162
5163 if (class_interface_decl && class_interface_decl->getSuperClass())
5164 count = 1;
5165 }
5166 }
5167 break;
5168
5169
5170 case clang::Type::Typedef:
5171 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5172 break;
5173
5174 case clang::Type::Elaborated:
5175 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5176 break;
5177
5178 case clang::Type::Paren:
5179 return GetNumDirectBaseClasses(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5180
5181 default:
5182 break;
5183 }
5184 return count;
5185
5186}
5187
5188uint32_t
5189ClangASTContext::GetNumVirtualBaseClasses (void *type)
5190{
5191 uint32_t count = 0;
5192 clang::QualType qual_type(GetCanonicalQualType(type));
5193 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5194 switch (type_class)
5195 {
5196 case clang::Type::Record:
5197 if (GetCompleteType(type))
5198 {
5199 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5200 if (cxx_record_decl)
5201 count = cxx_record_decl->getNumVBases();
5202 }
5203 break;
5204
5205 case clang::Type::Typedef:
5206 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5207 break;
5208
5209 case clang::Type::Elaborated:
5210 count = GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5211 break;
5212
5213 case clang::Type::Paren:
5214 count = GetNumVirtualBaseClasses(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5215 break;
5216
5217 default:
5218 break;
5219 }
5220 return count;
5221
5222}
5223
5224CompilerType
5225ClangASTContext::GetDirectBaseClassAtIndex (void *type, size_t idx, uint32_t *bit_offset_ptr)
5226{
5227 clang::QualType qual_type(GetCanonicalQualType(type));
5228 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5229 switch (type_class)
5230 {
5231 case clang::Type::Record:
5232 if (GetCompleteType(type))
5233 {
5234 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5235 if (cxx_record_decl)
5236 {
5237 uint32_t curr_idx = 0;
5238 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5239 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5240 base_class != base_class_end;
5241 ++base_class, ++curr_idx)
5242 {
5243 if (curr_idx == idx)
5244 {
5245 if (bit_offset_ptr)
5246 {
5247 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(cxx_record_decl);
5248 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5249 if (base_class->isVirtual())
5250 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5251 else
5252 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5253 }
5254 return CompilerType (this, base_class->getType().getAsOpaquePtr());
5255 }
5256 }
5257 }
5258 }
5259 break;
5260
5261 case clang::Type::ObjCObjectPointer:
5262 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
5263
5264 case clang::Type::ObjCObject:
5265 if (idx == 0 && GetCompleteType(type))
5266 {
5267 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
5268 if (objc_class_type)
5269 {
5270 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5271
5272 if (class_interface_decl)
5273 {
5274 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5275 if (superclass_interface_decl)
5276 {
5277 if (bit_offset_ptr)
5278 *bit_offset_ptr = 0;
5279 return CompilerType (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5280 }
5281 }
5282 }
5283 }
5284 break;
5285 case clang::Type::ObjCInterface:
5286 if (idx == 0 && GetCompleteType(type))
5287 {
5288 const clang::ObjCObjectType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
5289 if (objc_interface_type)
5290 {
5291 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
5292
5293 if (class_interface_decl)
5294 {
5295 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5296 if (superclass_interface_decl)
5297 {
5298 if (bit_offset_ptr)
5299 *bit_offset_ptr = 0;
5300 return CompilerType (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5301 }
5302 }
5303 }
5304 }
5305 break;
5306
5307
5308 case clang::Type::Typedef:
5309 return GetDirectBaseClassAtIndex (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx, bit_offset_ptr);
5310
5311 case clang::Type::Elaborated:
5312 return GetDirectBaseClassAtIndex (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5313
5314 case clang::Type::Paren:
5315 return GetDirectBaseClassAtIndex (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx, bit_offset_ptr);
5316
5317 default:
5318 break;
5319 }
5320 return CompilerType();
5321}
5322
5323CompilerType
5324ClangASTContext::GetVirtualBaseClassAtIndex (void *type,
5325 size_t idx,
5326 uint32_t *bit_offset_ptr)
5327{
5328 clang::QualType qual_type(GetCanonicalQualType(type));
5329 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5330 switch (type_class)
5331 {
5332 case clang::Type::Record:
5333 if (GetCompleteType(type))
5334 {
5335 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5336 if (cxx_record_decl)
5337 {
5338 uint32_t curr_idx = 0;
5339 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5340 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
5341 base_class != base_class_end;
5342 ++base_class, ++curr_idx)
5343 {
5344 if (curr_idx == idx)
5345 {
5346 if (bit_offset_ptr)
5347 {
5348 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(cxx_record_decl);
5349 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5350 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5351
5352 }
5353 return CompilerType (this, base_class->getType().getAsOpaquePtr());
5354 }
5355 }
5356 }
5357 }
5358 break;
5359
5360 case clang::Type::Typedef:
5361 return GetVirtualBaseClassAtIndex (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx, bit_offset_ptr);
5362
5363 case clang::Type::Elaborated:
5364 return GetVirtualBaseClassAtIndex (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5365
5366 case clang::Type::Paren:
5367 return GetVirtualBaseClassAtIndex (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx, bit_offset_ptr);
5368
5369 default:
5370 break;
5371 }
5372 return CompilerType();
5373
5374}
5375
Greg Claytond8d4a572015-08-11 21:38:15 +00005376// If a pointer to a pointee type (the clang_type arg) says that it has no
5377// children, then we either need to trust it, or override it and return a
5378// different result. For example, an "int *" has one child that is an integer,
5379// but a function pointer doesn't have any children. Likewise if a Record type
5380// claims it has no children, then there really is nothing to show.
5381uint32_t
5382ClangASTContext::GetNumPointeeChildren (clang::QualType type)
5383{
5384 if (type.isNull())
5385 return 0;
5386
5387 clang::QualType qual_type(type.getCanonicalType());
5388 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5389 switch (type_class)
5390 {
5391 case clang::Type::Builtin:
5392 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
5393 {
5394 case clang::BuiltinType::UnknownAny:
5395 case clang::BuiltinType::Void:
5396 case clang::BuiltinType::NullPtr:
5397 case clang::BuiltinType::OCLEvent:
5398 case clang::BuiltinType::OCLImage1d:
5399 case clang::BuiltinType::OCLImage1dArray:
5400 case clang::BuiltinType::OCLImage1dBuffer:
5401 case clang::BuiltinType::OCLImage2d:
5402 case clang::BuiltinType::OCLImage2dArray:
5403 case clang::BuiltinType::OCLImage3d:
5404 case clang::BuiltinType::OCLSampler:
5405 return 0;
5406 case clang::BuiltinType::Bool:
5407 case clang::BuiltinType::Char_U:
5408 case clang::BuiltinType::UChar:
5409 case clang::BuiltinType::WChar_U:
5410 case clang::BuiltinType::Char16:
5411 case clang::BuiltinType::Char32:
5412 case clang::BuiltinType::UShort:
5413 case clang::BuiltinType::UInt:
5414 case clang::BuiltinType::ULong:
5415 case clang::BuiltinType::ULongLong:
5416 case clang::BuiltinType::UInt128:
5417 case clang::BuiltinType::Char_S:
5418 case clang::BuiltinType::SChar:
5419 case clang::BuiltinType::WChar_S:
5420 case clang::BuiltinType::Short:
5421 case clang::BuiltinType::Int:
5422 case clang::BuiltinType::Long:
5423 case clang::BuiltinType::LongLong:
5424 case clang::BuiltinType::Int128:
5425 case clang::BuiltinType::Float:
5426 case clang::BuiltinType::Double:
5427 case clang::BuiltinType::LongDouble:
5428 case clang::BuiltinType::Dependent:
5429 case clang::BuiltinType::Overload:
5430 case clang::BuiltinType::ObjCId:
5431 case clang::BuiltinType::ObjCClass:
5432 case clang::BuiltinType::ObjCSel:
5433 case clang::BuiltinType::BoundMember:
5434 case clang::BuiltinType::Half:
5435 case clang::BuiltinType::ARCUnbridgedCast:
5436 case clang::BuiltinType::PseudoObject:
5437 case clang::BuiltinType::BuiltinFn:
5438 return 1;
5439 }
5440 break;
5441
5442 case clang::Type::Complex: return 1;
5443 case clang::Type::Pointer: return 1;
5444 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
5445 case clang::Type::LValueReference: return 1;
5446 case clang::Type::RValueReference: return 1;
5447 case clang::Type::MemberPointer: return 0;
5448 case clang::Type::ConstantArray: return 0;
5449 case clang::Type::IncompleteArray: return 0;
5450 case clang::Type::VariableArray: return 0;
5451 case clang::Type::DependentSizedArray: return 0;
5452 case clang::Type::DependentSizedExtVector: return 0;
5453 case clang::Type::Vector: return 0;
5454 case clang::Type::ExtVector: return 0;
5455 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
5456 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
5457 case clang::Type::UnresolvedUsing: return 0;
5458 case clang::Type::Paren: return GetNumPointeeChildren (llvm::cast<clang::ParenType>(qual_type)->desugar());
5459 case clang::Type::Typedef: return GetNumPointeeChildren (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
5460 case clang::Type::Elaborated: return GetNumPointeeChildren (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
5461 case clang::Type::TypeOfExpr: return 0;
5462 case clang::Type::TypeOf: return 0;
5463 case clang::Type::Decltype: return 0;
5464 case clang::Type::Record: return 0;
5465 case clang::Type::Enum: return 1;
5466 case clang::Type::TemplateTypeParm: return 1;
5467 case clang::Type::SubstTemplateTypeParm: return 1;
5468 case clang::Type::TemplateSpecialization: return 1;
5469 case clang::Type::InjectedClassName: return 0;
5470 case clang::Type::DependentName: return 1;
5471 case clang::Type::DependentTemplateSpecialization: return 1;
5472 case clang::Type::ObjCObject: return 0;
5473 case clang::Type::ObjCInterface: return 0;
5474 case clang::Type::ObjCObjectPointer: return 1;
5475 default:
5476 break;
5477 }
5478 return 0;
5479}
5480
5481
Greg Claytona1e5dc82015-08-11 22:53:00 +00005482CompilerType
Greg Clayton99558cc42015-08-24 23:46:31 +00005483ClangASTContext::GetChildClangTypeAtIndex (void* type,
5484 ExecutionContext *exe_ctx,
5485 size_t idx,
5486 bool transparent_pointers,
5487 bool omit_empty_base_classes,
5488 bool ignore_array_bounds,
5489 std::string& child_name,
5490 uint32_t &child_byte_size,
5491 int32_t &child_byte_offset,
5492 uint32_t &child_bitfield_bit_size,
5493 uint32_t &child_bitfield_bit_offset,
5494 bool &child_is_base_class,
5495 bool &child_is_deref_of_parent,
5496 ValueObject *valobj)
Greg Claytond8d4a572015-08-11 21:38:15 +00005497{
5498 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005499 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005500
5501 clang::QualType parent_qual_type(GetCanonicalQualType(type));
5502 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
5503 child_bitfield_bit_size = 0;
5504 child_bitfield_bit_offset = 0;
5505 child_is_base_class = false;
5506
5507 const bool idx_is_valid = idx < GetNumChildren (type, omit_empty_base_classes);
5508 uint32_t bit_offset;
5509 switch (parent_type_class)
5510 {
5511 case clang::Type::Builtin:
5512 if (idx_is_valid)
5513 {
5514 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind())
5515 {
5516 case clang::BuiltinType::ObjCId:
5517 case clang::BuiltinType::ObjCClass:
5518 child_name = "isa";
5519 child_byte_size = getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) / CHAR_BIT;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005520 return CompilerType (getASTContext(), getASTContext()->ObjCBuiltinClassTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00005521
5522 default:
5523 break;
5524 }
5525 }
5526 break;
5527
5528 case clang::Type::Record:
5529 if (idx_is_valid && GetCompleteType(type))
5530 {
5531 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
5532 const clang::RecordDecl *record_decl = record_type->getDecl();
5533 assert(record_decl);
5534 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5535 uint32_t child_idx = 0;
5536
5537 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5538 if (cxx_record_decl)
5539 {
5540 // We might have base classes to print out first
5541 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5542 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5543 base_class != base_class_end;
5544 ++base_class)
5545 {
5546 const clang::CXXRecordDecl *base_class_decl = nullptr;
5547
5548 // Skip empty base classes
5549 if (omit_empty_base_classes)
5550 {
5551 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5552 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5553 continue;
5554 }
5555
5556 if (idx == child_idx)
5557 {
5558 if (base_class_decl == nullptr)
5559 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5560
5561
5562 if (base_class->isVirtual())
5563 {
5564 bool handled = false;
5565 if (valobj)
5566 {
5567 Error err;
5568 AddressType addr_type = eAddressTypeInvalid;
5569 lldb::addr_t vtable_ptr_addr = valobj->GetCPPVTableAddress(addr_type);
5570
5571 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS && addr_type == eAddressTypeLoad)
5572 {
5573
5574 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
5575 Process *process = exe_ctx.GetProcessPtr();
5576 if (process)
5577 {
5578 clang::VTableContextBase *vtable_ctx = getASTContext()->getVTableContext();
5579 if (vtable_ctx)
5580 {
5581 if (vtable_ctx->isMicrosoft())
5582 {
5583 clang::MicrosoftVTableContext *msoft_vtable_ctx = static_cast<clang::MicrosoftVTableContext *>(vtable_ctx);
5584
5585 if (vtable_ptr_addr)
5586 {
5587 const lldb::addr_t vbtable_ptr_addr = vtable_ptr_addr + record_layout.getVBPtrOffset().getQuantity();
5588
5589 const lldb::addr_t vbtable_ptr = process->ReadPointerFromMemory(vbtable_ptr_addr, err);
5590 if (vbtable_ptr != LLDB_INVALID_ADDRESS)
5591 {
5592 // Get the index into the virtual base table. The index is the index in uint32_t from vbtable_ptr
5593 const unsigned vbtable_index = msoft_vtable_ctx->getVBTableIndex(cxx_record_decl, base_class_decl);
5594 const lldb::addr_t base_offset_addr = vbtable_ptr + vbtable_index * 4;
5595 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
5596 if (base_offset != UINT32_MAX)
5597 {
5598 handled = true;
5599 bit_offset = base_offset * 8;
5600 }
5601 }
5602 }
5603 }
5604 else
5605 {
5606 clang::ItaniumVTableContext *itanium_vtable_ctx = static_cast<clang::ItaniumVTableContext *>(vtable_ctx);
5607 if (vtable_ptr_addr)
5608 {
5609 const lldb::addr_t vtable_ptr = process->ReadPointerFromMemory(vtable_ptr_addr, err);
5610 if (vtable_ptr != LLDB_INVALID_ADDRESS)
5611 {
5612 clang::CharUnits base_offset_offset = itanium_vtable_ctx->getVirtualBaseOffsetOffset(cxx_record_decl, base_class_decl);
5613 const lldb::addr_t base_offset_addr = vtable_ptr + base_offset_offset.getQuantity();
5614 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
5615 if (base_offset != UINT32_MAX)
5616 {
5617 handled = true;
5618 bit_offset = base_offset * 8;
5619 }
5620 }
5621 }
5622 }
5623 }
5624 }
5625 }
5626
5627 }
5628 if (!handled)
5629 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5630 }
5631 else
5632 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5633
5634 // Base classes should be a multiple of 8 bits in size
5635 child_byte_offset = bit_offset/8;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005636 CompilerType base_class_clang_type(getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005637 child_name = base_class_clang_type.GetTypeName().AsCString("");
5638 uint64_t base_class_clang_type_bit_size = base_class_clang_type.GetBitSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5639
5640 // Base classes bit sizes should be a multiple of 8 bits in size
5641 assert (base_class_clang_type_bit_size % 8 == 0);
5642 child_byte_size = base_class_clang_type_bit_size / 8;
5643 child_is_base_class = true;
5644 return base_class_clang_type;
5645 }
5646 // We don't increment the child index in the for loop since we might
5647 // be skipping empty base classes
5648 ++child_idx;
5649 }
5650 }
5651 // Make sure index is in range...
5652 uint32_t field_idx = 0;
5653 clang::RecordDecl::field_iterator field, field_end;
5654 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
5655 {
5656 if (idx == child_idx)
5657 {
5658 // Print the member type if requested
5659 // Print the member name and equal sign
5660 child_name.assign(field->getNameAsString().c_str());
5661
5662 // Figure out the type byte size (field_type_info.first) and
5663 // alignment (field_type_info.second) from the AST context.
Greg Claytona1e5dc82015-08-11 22:53:00 +00005664 CompilerType field_clang_type (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005665 assert(field_idx < record_layout.getFieldCount());
5666 child_byte_size = field_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5667
5668 // Figure out the field offset within the current struct/union/class type
5669 bit_offset = record_layout.getFieldOffset (field_idx);
5670 child_byte_offset = bit_offset / 8;
5671 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, child_bitfield_bit_size))
5672 child_bitfield_bit_offset = bit_offset % 8;
5673
5674 return field_clang_type;
5675 }
5676 }
5677 }
5678 break;
5679
5680 case clang::Type::ObjCObject:
5681 case clang::Type::ObjCInterface:
5682 if (idx_is_valid && GetCompleteType(type))
5683 {
5684 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
5685 assert (objc_class_type);
5686 if (objc_class_type)
5687 {
5688 uint32_t child_idx = 0;
5689 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5690
5691 if (class_interface_decl)
5692 {
5693
5694 const clang::ASTRecordLayout &interface_layout = getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
5695 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5696 if (superclass_interface_decl)
5697 {
5698 if (omit_empty_base_classes)
5699 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005700 CompilerType base_class_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00005701 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes) > 0)
5702 {
5703 if (idx == 0)
5704 {
5705 clang::QualType ivar_qual_type(getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5706
5707
5708 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
5709
5710 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
5711
5712 child_byte_size = ivar_type_info.Width / 8;
5713 child_byte_offset = 0;
5714 child_is_base_class = true;
5715
Greg Claytona1e5dc82015-08-11 22:53:00 +00005716 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005717 }
5718
5719 ++child_idx;
5720 }
5721 }
5722 else
5723 ++child_idx;
5724 }
5725
5726 const uint32_t superclass_idx = child_idx;
5727
5728 if (idx < (child_idx + class_interface_decl->ivar_size()))
5729 {
5730 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
5731
5732 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
5733 {
5734 if (child_idx == idx)
5735 {
5736 clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
5737
5738 clang::QualType ivar_qual_type(ivar_decl->getType());
5739
5740 child_name.assign(ivar_decl->getNameAsString().c_str());
5741
5742 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
5743
5744 child_byte_size = ivar_type_info.Width / 8;
5745
5746 // Figure out the field offset within the current struct/union/class type
5747 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
5748 // that doesn't account for the space taken up by unbacked properties, or from
5749 // the changing size of base classes that are newer than this class.
5750 // So if we have a process around that we can ask about this object, do so.
5751 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
5752 Process *process = nullptr;
5753 if (exe_ctx)
5754 process = exe_ctx->GetProcessPtr();
5755 if (process)
5756 {
5757 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
5758 if (objc_runtime != nullptr)
5759 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005760 CompilerType parent_ast_type (getASTContext(), parent_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005761 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
5762 }
5763 }
5764
5765 // Setting this to UINT32_MAX to make sure we don't compute it twice...
5766 bit_offset = UINT32_MAX;
5767
5768 if (child_byte_offset == static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET))
5769 {
5770 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
5771 child_byte_offset = bit_offset / 8;
5772 }
5773
5774 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
5775 // of a bitfield within its containing object. So regardless of where we get the byte
5776 // offset from, we still need to get the bit offset for bitfields from the layout.
5777
5778 if (ClangASTContext::FieldIsBitfield (getASTContext(), ivar_decl, child_bitfield_bit_size))
5779 {
5780 if (bit_offset == UINT32_MAX)
5781 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
5782
5783 child_bitfield_bit_offset = bit_offset % 8;
5784 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005785 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005786 }
5787 ++child_idx;
5788 }
5789 }
5790 }
5791 }
5792 }
5793 break;
5794
5795 case clang::Type::ObjCObjectPointer:
5796 if (idx_is_valid)
5797 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005798 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005799
5800 if (transparent_pointers && pointee_clang_type.IsAggregateType())
5801 {
5802 child_is_deref_of_parent = false;
5803 bool tmp_child_is_deref_of_parent = false;
5804 return pointee_clang_type.GetChildClangTypeAtIndex (exe_ctx,
5805 idx,
5806 transparent_pointers,
5807 omit_empty_base_classes,
5808 ignore_array_bounds,
5809 child_name,
5810 child_byte_size,
5811 child_byte_offset,
5812 child_bitfield_bit_size,
5813 child_bitfield_bit_offset,
5814 child_is_base_class,
5815 tmp_child_is_deref_of_parent,
5816 valobj);
5817 }
5818 else
5819 {
5820 child_is_deref_of_parent = true;
5821 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
5822 if (parent_name)
5823 {
5824 child_name.assign(1, '*');
5825 child_name += parent_name;
5826 }
5827
5828 // We have a pointer to an simple type
5829 if (idx == 0 && pointee_clang_type.GetCompleteType())
5830 {
5831 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5832 child_byte_offset = 0;
5833 return pointee_clang_type;
5834 }
5835 }
5836 }
5837 break;
5838
5839 case clang::Type::Vector:
5840 case clang::Type::ExtVector:
5841 if (idx_is_valid)
5842 {
5843 const clang::VectorType *array = llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
5844 if (array)
5845 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005846 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005847 if (element_type.GetCompleteType())
5848 {
5849 char element_name[64];
5850 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
5851 child_name.assign(element_name);
5852 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5853 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
5854 return element_type;
5855 }
5856 }
5857 }
5858 break;
5859
5860 case clang::Type::ConstantArray:
5861 case clang::Type::IncompleteArray:
5862 if (ignore_array_bounds || idx_is_valid)
5863 {
5864 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
5865 if (array)
5866 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005867 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005868 if (element_type.GetCompleteType())
5869 {
5870 char element_name[64];
5871 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
5872 child_name.assign(element_name);
5873 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5874 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
5875 return element_type;
5876 }
5877 }
5878 }
5879 break;
5880
5881
5882 case clang::Type::Pointer:
5883 if (idx_is_valid)
5884 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005885 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005886
5887 // Don't dereference "void *" pointers
5888 if (pointee_clang_type.IsVoidType())
Greg Claytona1e5dc82015-08-11 22:53:00 +00005889 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005890
5891 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
5892 {
5893 child_is_deref_of_parent = false;
5894 bool tmp_child_is_deref_of_parent = false;
5895 return pointee_clang_type.GetChildClangTypeAtIndex (exe_ctx,
5896 idx,
5897 transparent_pointers,
5898 omit_empty_base_classes,
5899 ignore_array_bounds,
5900 child_name,
5901 child_byte_size,
5902 child_byte_offset,
5903 child_bitfield_bit_size,
5904 child_bitfield_bit_offset,
5905 child_is_base_class,
5906 tmp_child_is_deref_of_parent,
5907 valobj);
5908 }
5909 else
5910 {
5911 child_is_deref_of_parent = true;
5912
5913 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
5914 if (parent_name)
5915 {
5916 child_name.assign(1, '*');
5917 child_name += parent_name;
5918 }
5919
5920 // We have a pointer to an simple type
5921 if (idx == 0)
5922 {
5923 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5924 child_byte_offset = 0;
5925 return pointee_clang_type;
5926 }
5927 }
5928 }
5929 break;
5930
5931 case clang::Type::LValueReference:
5932 case clang::Type::RValueReference:
5933 if (idx_is_valid)
5934 {
5935 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00005936 CompilerType pointee_clang_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005937 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
5938 {
5939 child_is_deref_of_parent = false;
5940 bool tmp_child_is_deref_of_parent = false;
5941 return pointee_clang_type.GetChildClangTypeAtIndex (exe_ctx,
5942 idx,
5943 transparent_pointers,
5944 omit_empty_base_classes,
5945 ignore_array_bounds,
5946 child_name,
5947 child_byte_size,
5948 child_byte_offset,
5949 child_bitfield_bit_size,
5950 child_bitfield_bit_offset,
5951 child_is_base_class,
5952 tmp_child_is_deref_of_parent,
5953 valobj);
5954 }
5955 else
5956 {
5957 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
5958 if (parent_name)
5959 {
5960 child_name.assign(1, '&');
5961 child_name += parent_name;
5962 }
5963
5964 // We have a pointer to an simple type
5965 if (idx == 0)
5966 {
5967 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5968 child_byte_offset = 0;
5969 return pointee_clang_type;
5970 }
5971 }
5972 }
5973 break;
5974
5975 case clang::Type::Typedef:
5976 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005977 CompilerType typedefed_clang_type (getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005978 return typedefed_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 child_is_deref_of_parent,
5990 valobj);
5991 }
5992 break;
5993
5994 case clang::Type::Elaborated:
5995 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005996 CompilerType elaborated_clang_type (getASTContext(), llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005997 return elaborated_clang_type.GetChildClangTypeAtIndex (exe_ctx,
5998 idx,
5999 transparent_pointers,
6000 omit_empty_base_classes,
6001 ignore_array_bounds,
6002 child_name,
6003 child_byte_size,
6004 child_byte_offset,
6005 child_bitfield_bit_size,
6006 child_bitfield_bit_offset,
6007 child_is_base_class,
6008 child_is_deref_of_parent,
6009 valobj);
6010 }
6011
6012 case clang::Type::Paren:
6013 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006014 CompilerType paren_clang_type (getASTContext(), llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00006015 return paren_clang_type.GetChildClangTypeAtIndex (exe_ctx,
6016 idx,
6017 transparent_pointers,
6018 omit_empty_base_classes,
6019 ignore_array_bounds,
6020 child_name,
6021 child_byte_size,
6022 child_byte_offset,
6023 child_bitfield_bit_size,
6024 child_bitfield_bit_offset,
6025 child_is_base_class,
6026 child_is_deref_of_parent,
6027 valobj);
6028 }
6029
6030
6031 default:
6032 break;
6033 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00006034 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006035}
6036
6037static uint32_t
6038GetIndexForRecordBase
6039(
6040 const clang::RecordDecl *record_decl,
6041 const clang::CXXBaseSpecifier *base_spec,
6042 bool omit_empty_base_classes
6043 )
6044{
6045 uint32_t child_idx = 0;
6046
6047 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6048
6049 // const char *super_name = record_decl->getNameAsCString();
6050 // const char *base_name = base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6051 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6052 //
6053 if (cxx_record_decl)
6054 {
6055 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6056 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6057 base_class != base_class_end;
6058 ++base_class)
6059 {
6060 if (omit_empty_base_classes)
6061 {
6062 if (BaseSpecifierIsEmpty (base_class))
6063 continue;
6064 }
6065
6066 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
6067 // child_idx,
6068 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6069 //
6070 //
6071 if (base_class == base_spec)
6072 return child_idx;
6073 ++child_idx;
6074 }
6075 }
6076
6077 return UINT32_MAX;
6078}
6079
6080
6081static uint32_t
6082GetIndexForRecordChild (const clang::RecordDecl *record_decl,
6083 clang::NamedDecl *canonical_decl,
6084 bool omit_empty_base_classes)
6085{
6086 uint32_t child_idx = ClangASTContext::GetNumBaseClasses (llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6087 omit_empty_base_classes);
6088
6089 clang::RecordDecl::field_iterator field, field_end;
6090 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6091 field != field_end;
6092 ++field, ++child_idx)
6093 {
6094 if (field->getCanonicalDecl() == canonical_decl)
6095 return child_idx;
6096 }
6097
6098 return UINT32_MAX;
6099}
6100
6101// Look for a child member (doesn't include base classes, but it does include
6102// their members) in the type hierarchy. Returns an index path into "clang_type"
6103// on how to reach the appropriate member.
6104//
6105// class A
6106// {
6107// public:
6108// int m_a;
6109// int m_b;
6110// };
6111//
6112// class B
6113// {
6114// };
6115//
6116// class C :
6117// public B,
6118// public A
6119// {
6120// };
6121//
6122// If we have a clang type that describes "class C", and we wanted to looked
6123// "m_b" in it:
6124//
6125// With omit_empty_base_classes == false we would get an integer array back with:
6126// { 1, 1 }
6127// The first index 1 is the child index for "class A" within class C
6128// The second index 1 is the child index for "m_b" within class A
6129//
6130// With omit_empty_base_classes == true we would get an integer array back with:
6131// { 0, 1 }
6132// 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)
6133// The second index 1 is the child index for "m_b" within class A
6134
6135size_t
6136ClangASTContext::GetIndexOfChildMemberWithName (void* type, const char *name,
6137 bool omit_empty_base_classes,
6138 std::vector<uint32_t>& child_indexes)
6139{
6140 if (type && name && name[0])
6141 {
6142 clang::QualType qual_type(GetCanonicalQualType(type));
6143 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6144 switch (type_class)
6145 {
6146 case clang::Type::Record:
6147 if (GetCompleteType(type))
6148 {
6149 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6150 const clang::RecordDecl *record_decl = record_type->getDecl();
6151
6152 assert(record_decl);
6153 uint32_t child_idx = 0;
6154
6155 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6156
6157 // Try and find a field that matches NAME
6158 clang::RecordDecl::field_iterator field, field_end;
6159 llvm::StringRef name_sref(name);
6160 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6161 field != field_end;
6162 ++field, ++child_idx)
6163 {
6164 llvm::StringRef field_name = field->getName();
6165 if (field_name.empty())
6166 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006167 CompilerType field_type(getASTContext(),field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006168 child_indexes.push_back(child_idx);
6169 if (field_type.GetIndexOfChildMemberWithName(name, omit_empty_base_classes, child_indexes))
6170 return child_indexes.size();
6171 child_indexes.pop_back();
6172
6173 }
6174 else if (field_name.equals (name_sref))
6175 {
6176 // We have to add on the number of base classes to this index!
6177 child_indexes.push_back (child_idx + ClangASTContext::GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
6178 return child_indexes.size();
6179 }
6180 }
6181
6182 if (cxx_record_decl)
6183 {
6184 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
6185
6186 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
6187
6188 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
6189 // Didn't find things easily, lets let clang do its thang...
6190 clang::IdentifierInfo & ident_ref = getASTContext()->Idents.get(name_sref);
6191 clang::DeclarationName decl_name(&ident_ref);
6192
6193 clang::CXXBasePaths paths;
6194 if (cxx_record_decl->lookupInBases([decl_name](const clang::CXXBaseSpecifier *specifier, clang::CXXBasePath &path) {
6195 return clang::CXXRecordDecl::FindOrdinaryMember(specifier, path, decl_name);
6196 },
6197 paths))
6198 {
6199 clang::CXXBasePaths::const_paths_iterator path, path_end = paths.end();
6200 for (path = paths.begin(); path != path_end; ++path)
6201 {
6202 const size_t num_path_elements = path->size();
6203 for (size_t e=0; e<num_path_elements; ++e)
6204 {
6205 clang::CXXBasePathElement elem = (*path)[e];
6206
6207 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
6208 if (child_idx == UINT32_MAX)
6209 {
6210 child_indexes.clear();
6211 return 0;
6212 }
6213 else
6214 {
6215 child_indexes.push_back (child_idx);
6216 parent_record_decl = llvm::cast<clang::RecordDecl>(elem.Base->getType()->getAs<clang::RecordType>()->getDecl());
6217 }
6218 }
6219 for (clang::NamedDecl *path_decl : path->Decls)
6220 {
6221 child_idx = GetIndexForRecordChild (parent_record_decl, path_decl, omit_empty_base_classes);
6222 if (child_idx == UINT32_MAX)
6223 {
6224 child_indexes.clear();
6225 return 0;
6226 }
6227 else
6228 {
6229 child_indexes.push_back (child_idx);
6230 }
6231 }
6232 }
6233 return child_indexes.size();
6234 }
6235 }
6236
6237 }
6238 break;
6239
6240 case clang::Type::ObjCObject:
6241 case clang::Type::ObjCInterface:
6242 if (GetCompleteType(type))
6243 {
6244 llvm::StringRef name_sref(name);
6245 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6246 assert (objc_class_type);
6247 if (objc_class_type)
6248 {
6249 uint32_t child_idx = 0;
6250 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6251
6252 if (class_interface_decl)
6253 {
6254 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6255 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6256
6257 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6258 {
6259 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6260
6261 if (ivar_decl->getName().equals (name_sref))
6262 {
6263 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6264 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6265 ++child_idx;
6266
6267 child_indexes.push_back (child_idx);
6268 return child_indexes.size();
6269 }
6270 }
6271
6272 if (superclass_interface_decl)
6273 {
6274 // The super class index is always zero for ObjC classes,
6275 // so we push it onto the child indexes in case we find
6276 // an ivar in our superclass...
6277 child_indexes.push_back (0);
6278
Greg Claytona1e5dc82015-08-11 22:53:00 +00006279 CompilerType superclass_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00006280 if (superclass_clang_type.GetIndexOfChildMemberWithName (name,
6281 omit_empty_base_classes,
6282 child_indexes))
6283 {
6284 // We did find an ivar in a superclass so just
6285 // return the results!
6286 return child_indexes.size();
6287 }
6288
6289 // We didn't find an ivar matching "name" in our
6290 // superclass, pop the superclass zero index that
6291 // we pushed on above.
6292 child_indexes.pop_back();
6293 }
6294 }
6295 }
6296 }
6297 break;
6298
6299 case clang::Type::ObjCObjectPointer:
6300 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006301 CompilerType objc_object_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006302 return objc_object_clang_type.GetIndexOfChildMemberWithName (name,
6303 omit_empty_base_classes,
6304 child_indexes);
6305 }
6306 break;
6307
6308
6309 case clang::Type::ConstantArray:
6310 {
6311 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6312 // const uint64_t element_count = array->getSize().getLimitedValue();
6313 //
6314 // if (idx < element_count)
6315 // {
6316 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6317 //
6318 // char element_name[32];
6319 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6320 //
6321 // child_name.assign(element_name);
6322 // assert(field_type_info.first % 8 == 0);
6323 // child_byte_size = field_type_info.first / 8;
6324 // child_byte_offset = idx * child_byte_size;
6325 // return array->getElementType().getAsOpaquePtr();
6326 // }
6327 }
6328 break;
6329
6330 // case clang::Type::MemberPointerType:
6331 // {
6332 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6333 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6334 //
6335 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6336 // {
6337 // return GetIndexOfChildWithName (ast,
6338 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6339 // name);
6340 // }
6341 // }
6342 // break;
6343 //
6344 case clang::Type::LValueReference:
6345 case clang::Type::RValueReference:
6346 {
6347 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
6348 clang::QualType pointee_type(reference_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006349 CompilerType pointee_clang_type (getASTContext(), pointee_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00006350
6351 if (pointee_clang_type.IsAggregateType ())
6352 {
6353 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6354 omit_empty_base_classes,
6355 child_indexes);
6356 }
6357 }
6358 break;
6359
6360 case clang::Type::Pointer:
6361 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006362 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00006363
6364 if (pointee_clang_type.IsAggregateType ())
6365 {
6366 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6367 omit_empty_base_classes,
6368 child_indexes);
6369 }
6370 }
6371 break;
6372
6373 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006374 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006375 omit_empty_base_classes,
6376 child_indexes);
6377
6378 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006379 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006380 omit_empty_base_classes,
6381 child_indexes);
6382
6383 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006384 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006385 omit_empty_base_classes,
6386 child_indexes);
6387
6388 default:
6389 break;
6390 }
6391 }
6392 return 0;
6393}
6394
6395
6396// Get the index of the child of "clang_type" whose name matches. This function
6397// doesn't descend into the children, but only looks one level deep and name
6398// matches can include base class names.
6399
6400uint32_t
6401ClangASTContext::GetIndexOfChildWithName (void* type, const char *name, bool omit_empty_base_classes)
6402{
6403 if (type && name && name[0])
6404 {
6405 clang::QualType qual_type(GetCanonicalQualType(type));
6406
6407 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6408
6409 switch (type_class)
6410 {
6411 case clang::Type::Record:
6412 if (GetCompleteType(type))
6413 {
6414 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6415 const clang::RecordDecl *record_decl = record_type->getDecl();
6416
6417 assert(record_decl);
6418 uint32_t child_idx = 0;
6419
6420 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6421
6422 if (cxx_record_decl)
6423 {
6424 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6425 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6426 base_class != base_class_end;
6427 ++base_class)
6428 {
6429 // Skip empty base classes
6430 clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
6431 if (omit_empty_base_classes && ClangASTContext::RecordHasFields(base_class_decl) == false)
6432 continue;
6433
Greg Claytona1e5dc82015-08-11 22:53:00 +00006434 CompilerType base_class_clang_type (getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006435 std::string base_class_type_name (base_class_clang_type.GetTypeName().AsCString(""));
6436 if (base_class_type_name.compare (name) == 0)
6437 return child_idx;
6438 ++child_idx;
6439 }
6440 }
6441
6442 // Try and find a field that matches NAME
6443 clang::RecordDecl::field_iterator field, field_end;
6444 llvm::StringRef name_sref(name);
6445 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6446 field != field_end;
6447 ++field, ++child_idx)
6448 {
6449 if (field->getName().equals (name_sref))
6450 return child_idx;
6451 }
6452
6453 }
6454 break;
6455
6456 case clang::Type::ObjCObject:
6457 case clang::Type::ObjCInterface:
6458 if (GetCompleteType(type))
6459 {
6460 llvm::StringRef name_sref(name);
6461 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6462 assert (objc_class_type);
6463 if (objc_class_type)
6464 {
6465 uint32_t child_idx = 0;
6466 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6467
6468 if (class_interface_decl)
6469 {
6470 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6471 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6472
6473 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6474 {
6475 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6476
6477 if (ivar_decl->getName().equals (name_sref))
6478 {
6479 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6480 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6481 ++child_idx;
6482
6483 return child_idx;
6484 }
6485 }
6486
6487 if (superclass_interface_decl)
6488 {
6489 if (superclass_interface_decl->getName().equals (name_sref))
6490 return 0;
6491 }
6492 }
6493 }
6494 }
6495 break;
6496
6497 case clang::Type::ObjCObjectPointer:
6498 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006499 CompilerType pointee_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006500 return pointee_clang_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6501 }
6502 break;
6503
6504 case clang::Type::ConstantArray:
6505 {
6506 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6507 // const uint64_t element_count = array->getSize().getLimitedValue();
6508 //
6509 // if (idx < element_count)
6510 // {
6511 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6512 //
6513 // char element_name[32];
6514 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6515 //
6516 // child_name.assign(element_name);
6517 // assert(field_type_info.first % 8 == 0);
6518 // child_byte_size = field_type_info.first / 8;
6519 // child_byte_offset = idx * child_byte_size;
6520 // return array->getElementType().getAsOpaquePtr();
6521 // }
6522 }
6523 break;
6524
6525 // case clang::Type::MemberPointerType:
6526 // {
6527 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6528 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6529 //
6530 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6531 // {
6532 // return GetIndexOfChildWithName (ast,
6533 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6534 // name);
6535 // }
6536 // }
6537 // break;
6538 //
6539 case clang::Type::LValueReference:
6540 case clang::Type::RValueReference:
6541 {
6542 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006543 CompilerType pointee_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006544
6545 if (pointee_type.IsAggregateType ())
6546 {
6547 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6548 }
6549 }
6550 break;
6551
6552 case clang::Type::Pointer:
6553 {
6554 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006555 CompilerType pointee_type (getASTContext(), pointer_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006556
6557 if (pointee_type.IsAggregateType ())
6558 {
6559 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6560 }
6561 else
6562 {
6563 // if (parent_name)
6564 // {
6565 // child_name.assign(1, '*');
6566 // child_name += parent_name;
6567 // }
6568 //
6569 // // We have a pointer to an simple type
6570 // if (idx == 0)
6571 // {
6572 // std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
6573 // assert(clang_type_info.first % 8 == 0);
6574 // child_byte_size = clang_type_info.first / 8;
6575 // child_byte_offset = 0;
6576 // return pointee_type.getAsOpaquePtr();
6577 // }
6578 }
6579 }
6580 break;
6581
6582 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006583 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006584
6585 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006586 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006587
6588 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006589 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006590
6591 default:
6592 break;
6593 }
6594 }
6595 return UINT32_MAX;
6596}
6597
6598
6599size_t
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006600ClangASTContext::GetNumTemplateArguments (void* type)
Greg Claytond8d4a572015-08-11 21:38:15 +00006601{
6602 if (!type)
6603 return 0;
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006604
6605 clang::QualType qual_type (GetCanonicalQualType(type));
6606 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6607 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00006608 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006609 case clang::Type::Record:
6610 if (GetCompleteType(type))
6611 {
6612 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
6613 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00006614 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006615 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
6616 if (template_decl)
6617 return template_decl->getTemplateArgs().size();
Greg Claytond8d4a572015-08-11 21:38:15 +00006618 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006619 }
6620 break;
6621
6622 case clang::Type::Typedef:
6623 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetNumTemplateArguments();
6624
6625 case clang::Type::Elaborated:
6626 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetNumTemplateArguments();
6627
6628 case clang::Type::Paren:
6629 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetNumTemplateArguments();
6630
6631 default:
6632 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006633 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006634
Greg Claytond8d4a572015-08-11 21:38:15 +00006635 return 0;
6636}
6637
Greg Claytona1e5dc82015-08-11 22:53:00 +00006638CompilerType
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006639ClangASTContext::GetTemplateArgument (void* type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
Greg Claytond8d4a572015-08-11 21:38:15 +00006640{
6641 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006642 return CompilerType();
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006643
6644 clang::QualType qual_type (GetCanonicalQualType(type));
6645 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6646 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00006647 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006648 case clang::Type::Record:
6649 if (GetCompleteType(type))
6650 {
6651 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
6652 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00006653 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006654 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
6655 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
Greg Claytond8d4a572015-08-11 21:38:15 +00006656 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006657 const clang::TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
6658 switch (template_arg.getKind())
Greg Claytond8d4a572015-08-11 21:38:15 +00006659 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006660 case clang::TemplateArgument::Null:
6661 kind = eTemplateArgumentKindNull;
6662 return CompilerType();
6663
6664 case clang::TemplateArgument::Type:
6665 kind = eTemplateArgumentKindType;
6666 return CompilerType(getASTContext(), template_arg.getAsType());
6667
6668 case clang::TemplateArgument::Declaration:
6669 kind = eTemplateArgumentKindDeclaration;
6670 return CompilerType();
6671
6672 case clang::TemplateArgument::Integral:
6673 kind = eTemplateArgumentKindIntegral;
6674 return CompilerType(getASTContext(), template_arg.getIntegralType());
6675
6676 case clang::TemplateArgument::Template:
6677 kind = eTemplateArgumentKindTemplate;
6678 return CompilerType();
6679
6680 case clang::TemplateArgument::TemplateExpansion:
6681 kind = eTemplateArgumentKindTemplateExpansion;
6682 return CompilerType();
6683
6684 case clang::TemplateArgument::Expression:
6685 kind = eTemplateArgumentKindExpression;
6686 return CompilerType();
6687
6688 case clang::TemplateArgument::Pack:
6689 kind = eTemplateArgumentKindPack;
6690 return CompilerType();
6691
6692 default:
6693 assert (!"Unhandled clang::TemplateArgument::ArgKind");
6694 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006695 }
6696 }
6697 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006698 }
6699 break;
6700
6701 case clang::Type::Typedef:
6702 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetTemplateArgument(arg_idx, kind);
6703
6704 case clang::Type::Elaborated:
6705 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetTemplateArgument(arg_idx, kind);
6706
6707 case clang::Type::Paren:
6708 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetTemplateArgument(arg_idx, kind);
6709
6710 default:
6711 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006712 }
6713 kind = eTemplateArgumentKindNull;
Greg Claytona1e5dc82015-08-11 22:53:00 +00006714 return CompilerType ();
Greg Claytond8d4a572015-08-11 21:38:15 +00006715}
6716
6717static bool
6718IsOperator (const char *name, clang::OverloadedOperatorKind &op_kind)
6719{
6720 if (name == nullptr || name[0] == '\0')
6721 return false;
6722
6723#define OPERATOR_PREFIX "operator"
6724#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
6725
6726 const char *post_op_name = nullptr;
6727
6728 bool no_space = true;
6729
6730 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
6731 return false;
6732
6733 post_op_name = name + OPERATOR_PREFIX_LENGTH;
6734
6735 if (post_op_name[0] == ' ')
6736 {
6737 post_op_name++;
6738 no_space = false;
6739 }
6740
6741#undef OPERATOR_PREFIX
6742#undef OPERATOR_PREFIX_LENGTH
6743
6744 // This is an operator, set the overloaded operator kind to invalid
6745 // in case this is a conversion operator...
6746 op_kind = clang::NUM_OVERLOADED_OPERATORS;
6747
6748 switch (post_op_name[0])
6749 {
6750 default:
6751 if (no_space)
6752 return false;
6753 break;
6754 case 'n':
6755 if (no_space)
6756 return false;
6757 if (strcmp (post_op_name, "new") == 0)
6758 op_kind = clang::OO_New;
6759 else if (strcmp (post_op_name, "new[]") == 0)
6760 op_kind = clang::OO_Array_New;
6761 break;
6762
6763 case 'd':
6764 if (no_space)
6765 return false;
6766 if (strcmp (post_op_name, "delete") == 0)
6767 op_kind = clang::OO_Delete;
6768 else if (strcmp (post_op_name, "delete[]") == 0)
6769 op_kind = clang::OO_Array_Delete;
6770 break;
6771
6772 case '+':
6773 if (post_op_name[1] == '\0')
6774 op_kind = clang::OO_Plus;
6775 else if (post_op_name[2] == '\0')
6776 {
6777 if (post_op_name[1] == '=')
6778 op_kind = clang::OO_PlusEqual;
6779 else if (post_op_name[1] == '+')
6780 op_kind = clang::OO_PlusPlus;
6781 }
6782 break;
6783
6784 case '-':
6785 if (post_op_name[1] == '\0')
6786 op_kind = clang::OO_Minus;
6787 else if (post_op_name[2] == '\0')
6788 {
6789 switch (post_op_name[1])
6790 {
6791 case '=': op_kind = clang::OO_MinusEqual; break;
6792 case '-': op_kind = clang::OO_MinusMinus; break;
6793 case '>': op_kind = clang::OO_Arrow; break;
6794 }
6795 }
6796 else if (post_op_name[3] == '\0')
6797 {
6798 if (post_op_name[2] == '*')
6799 op_kind = clang::OO_ArrowStar; break;
6800 }
6801 break;
6802
6803 case '*':
6804 if (post_op_name[1] == '\0')
6805 op_kind = clang::OO_Star;
6806 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6807 op_kind = clang::OO_StarEqual;
6808 break;
6809
6810 case '/':
6811 if (post_op_name[1] == '\0')
6812 op_kind = clang::OO_Slash;
6813 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6814 op_kind = clang::OO_SlashEqual;
6815 break;
6816
6817 case '%':
6818 if (post_op_name[1] == '\0')
6819 op_kind = clang::OO_Percent;
6820 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6821 op_kind = clang::OO_PercentEqual;
6822 break;
6823
6824
6825 case '^':
6826 if (post_op_name[1] == '\0')
6827 op_kind = clang::OO_Caret;
6828 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6829 op_kind = clang::OO_CaretEqual;
6830 break;
6831
6832 case '&':
6833 if (post_op_name[1] == '\0')
6834 op_kind = clang::OO_Amp;
6835 else if (post_op_name[2] == '\0')
6836 {
6837 switch (post_op_name[1])
6838 {
6839 case '=': op_kind = clang::OO_AmpEqual; break;
6840 case '&': op_kind = clang::OO_AmpAmp; break;
6841 }
6842 }
6843 break;
6844
6845 case '|':
6846 if (post_op_name[1] == '\0')
6847 op_kind = clang::OO_Pipe;
6848 else if (post_op_name[2] == '\0')
6849 {
6850 switch (post_op_name[1])
6851 {
6852 case '=': op_kind = clang::OO_PipeEqual; break;
6853 case '|': op_kind = clang::OO_PipePipe; break;
6854 }
6855 }
6856 break;
6857
6858 case '~':
6859 if (post_op_name[1] == '\0')
6860 op_kind = clang::OO_Tilde;
6861 break;
6862
6863 case '!':
6864 if (post_op_name[1] == '\0')
6865 op_kind = clang::OO_Exclaim;
6866 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6867 op_kind = clang::OO_ExclaimEqual;
6868 break;
6869
6870 case '=':
6871 if (post_op_name[1] == '\0')
6872 op_kind = clang::OO_Equal;
6873 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6874 op_kind = clang::OO_EqualEqual;
6875 break;
6876
6877 case '<':
6878 if (post_op_name[1] == '\0')
6879 op_kind = clang::OO_Less;
6880 else if (post_op_name[2] == '\0')
6881 {
6882 switch (post_op_name[1])
6883 {
6884 case '<': op_kind = clang::OO_LessLess; break;
6885 case '=': op_kind = clang::OO_LessEqual; break;
6886 }
6887 }
6888 else if (post_op_name[3] == '\0')
6889 {
6890 if (post_op_name[2] == '=')
6891 op_kind = clang::OO_LessLessEqual;
6892 }
6893 break;
6894
6895 case '>':
6896 if (post_op_name[1] == '\0')
6897 op_kind = clang::OO_Greater;
6898 else if (post_op_name[2] == '\0')
6899 {
6900 switch (post_op_name[1])
6901 {
6902 case '>': op_kind = clang::OO_GreaterGreater; break;
6903 case '=': op_kind = clang::OO_GreaterEqual; break;
6904 }
6905 }
6906 else if (post_op_name[1] == '>' &&
6907 post_op_name[2] == '=' &&
6908 post_op_name[3] == '\0')
6909 {
6910 op_kind = clang::OO_GreaterGreaterEqual;
6911 }
6912 break;
6913
6914 case ',':
6915 if (post_op_name[1] == '\0')
6916 op_kind = clang::OO_Comma;
6917 break;
6918
6919 case '(':
6920 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
6921 op_kind = clang::OO_Call;
6922 break;
6923
6924 case '[':
6925 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
6926 op_kind = clang::OO_Subscript;
6927 break;
6928 }
6929
6930 return true;
6931}
6932
6933clang::EnumDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00006934ClangASTContext::GetAsEnumDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00006935{
6936 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
6937 if (enutype)
6938 return enutype->getDecl();
6939 return NULL;
6940}
6941
6942clang::RecordDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00006943ClangASTContext::GetAsRecordDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00006944{
6945 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(GetCanonicalQualType(type));
6946 if (record_type)
6947 return record_type->getDecl();
6948 return nullptr;
6949}
6950
6951clang::CXXRecordDecl *
6952ClangASTContext::GetAsCXXRecordDecl (void* type)
6953{
6954 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
6955}
6956
6957clang::ObjCInterfaceDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00006958ClangASTContext::GetAsObjCInterfaceDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00006959{
6960 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(GetCanonicalQualType(type));
6961 if (objc_class_type)
6962 return objc_class_type->getInterface();
6963 return nullptr;
6964}
6965
6966clang::FieldDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00006967ClangASTContext::AddFieldToRecordType (const CompilerType& type, const char *name,
6968 const CompilerType &field_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00006969 AccessType access,
6970 uint32_t bitfield_bit_size)
6971{
6972 if (!type.IsValid() || !field_clang_type.IsValid())
6973 return nullptr;
6974 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
6975 if (!ast)
6976 return nullptr;
6977 clang::ASTContext* clang_ast = ast->getASTContext();
6978
6979 clang::FieldDecl *field = nullptr;
6980
6981 clang::Expr *bit_width = nullptr;
6982 if (bitfield_bit_size != 0)
6983 {
6984 llvm::APInt bitfield_bit_size_apint(clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
6985 bit_width = new (*clang_ast)clang::IntegerLiteral (*clang_ast, bitfield_bit_size_apint, clang_ast->IntTy, clang::SourceLocation());
6986 }
6987
6988 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
6989 if (record_decl)
6990 {
6991 field = clang::FieldDecl::Create (*clang_ast,
6992 record_decl,
6993 clang::SourceLocation(),
6994 clang::SourceLocation(),
6995 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
6996 GetQualType(field_clang_type), // Field type
6997 nullptr, // TInfo *
6998 bit_width, // BitWidth
6999 false, // Mutable
7000 clang::ICIS_NoInit); // HasInit
7001
7002 if (!name)
7003 {
7004 // Determine whether this field corresponds to an anonymous
7005 // struct or union.
7006 if (const clang::TagType *TagT = field->getType()->getAs<clang::TagType>()) {
7007 if (clang::RecordDecl *Rec = llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7008 if (!Rec->getDeclName()) {
7009 Rec->setAnonymousStructOrUnion(true);
7010 field->setImplicit();
7011
7012 }
7013 }
7014 }
7015
7016 if (field)
7017 {
7018 field->setAccess (ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7019
7020 record_decl->addDecl(field);
7021
7022#ifdef LLDB_CONFIGURATION_DEBUG
7023 VerifyDecl(field);
7024#endif
7025 }
7026 }
7027 else
7028 {
7029 clang::ObjCInterfaceDecl *class_interface_decl = ast->GetAsObjCInterfaceDecl (type);
7030
7031 if (class_interface_decl)
7032 {
7033 const bool is_synthesized = false;
7034
7035 field_clang_type.GetCompleteType();
7036
7037 field = clang::ObjCIvarDecl::Create (*clang_ast,
7038 class_interface_decl,
7039 clang::SourceLocation(),
7040 clang::SourceLocation(),
7041 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7042 GetQualType(field_clang_type), // Field type
7043 nullptr, // TypeSourceInfo *
7044 ConvertAccessTypeToObjCIvarAccessControl (access),
7045 bit_width,
7046 is_synthesized);
7047
7048 if (field)
7049 {
7050 class_interface_decl->addDecl(field);
7051
7052#ifdef LLDB_CONFIGURATION_DEBUG
7053 VerifyDecl(field);
7054#endif
7055 }
7056 }
7057 }
7058 return field;
7059}
7060
7061void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007062ClangASTContext::BuildIndirectFields (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007063{
7064 ClangASTContext* ast = nullptr;
7065 if (type)
7066 ast = type.GetTypeSystem()->AsClangASTContext();
7067 if (!ast)
7068 return;
7069
7070 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7071
7072 if (!record_decl)
7073 return;
7074
7075 typedef llvm::SmallVector <clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7076
7077 IndirectFieldVector indirect_fields;
7078 clang::RecordDecl::field_iterator field_pos;
7079 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7080 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7081 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; last_field_pos = field_pos++)
7082 {
7083 if (field_pos->isAnonymousStructOrUnion())
7084 {
7085 clang::QualType field_qual_type = field_pos->getType();
7086
7087 const clang::RecordType *field_record_type = field_qual_type->getAs<clang::RecordType>();
7088
7089 if (!field_record_type)
7090 continue;
7091
7092 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7093
7094 if (!field_record_decl)
7095 continue;
7096
7097 for (clang::RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
7098 di != de;
7099 ++di)
7100 {
7101 if (clang::FieldDecl *nested_field_decl = llvm::dyn_cast<clang::FieldDecl>(*di))
7102 {
7103 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[2];
7104 chain[0] = *field_pos;
7105 chain[1] = nested_field_decl;
7106 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7107 record_decl,
7108 clang::SourceLocation(),
7109 nested_field_decl->getIdentifier(),
7110 nested_field_decl->getType(),
7111 chain,
7112 2);
7113
7114 indirect_field->setImplicit();
7115
7116 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7117 nested_field_decl->getAccess()));
7118
7119 indirect_fields.push_back(indirect_field);
7120 }
7121 else if (clang::IndirectFieldDecl *nested_indirect_field_decl = llvm::dyn_cast<clang::IndirectFieldDecl>(*di))
7122 {
7123 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
7124 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[nested_chain_size + 1];
7125 chain[0] = *field_pos;
7126
7127 int chain_index = 1;
7128 for (clang::IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
7129 nce = nested_indirect_field_decl->chain_end();
7130 nci < nce;
7131 ++nci)
7132 {
7133 chain[chain_index] = *nci;
7134 chain_index++;
7135 }
7136
7137 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7138 record_decl,
7139 clang::SourceLocation(),
7140 nested_indirect_field_decl->getIdentifier(),
7141 nested_indirect_field_decl->getType(),
7142 chain,
7143 nested_chain_size + 1);
7144
7145 indirect_field->setImplicit();
7146
7147 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7148 nested_indirect_field_decl->getAccess()));
7149
7150 indirect_fields.push_back(indirect_field);
7151 }
7152 }
7153 }
7154 }
7155
7156 // Check the last field to see if it has an incomplete array type as its
7157 // last member and if it does, the tell the record decl about it
7158 if (last_field_pos != field_end_pos)
7159 {
7160 if (last_field_pos->getType()->isIncompleteArrayType())
7161 record_decl->hasFlexibleArrayMember();
7162 }
7163
7164 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
7165 ifi < ife;
7166 ++ifi)
7167 {
7168 record_decl->addDecl(*ifi);
7169 }
7170}
7171
7172void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007173ClangASTContext::SetIsPacked (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007174{
7175 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
7176
7177 if (!record_decl)
7178 return;
7179
7180 record_decl->addAttr(clang::PackedAttr::CreateImplicit(*type.GetTypeSystem()->AsClangASTContext()->getASTContext()));
7181}
7182
7183clang::VarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007184ClangASTContext::AddVariableToRecordType (const CompilerType& type, const char *name,
7185 const CompilerType &var_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007186 AccessType access)
7187{
7188 clang::VarDecl *var_decl = nullptr;
7189
7190 if (!type.IsValid() || !var_type.IsValid())
7191 return nullptr;
7192 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
7193 if (!ast)
7194 return nullptr;
7195
7196 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
7197 if (record_decl)
7198 {
7199 var_decl = clang::VarDecl::Create (*ast->getASTContext(), // ASTContext &
7200 record_decl, // DeclContext *
7201 clang::SourceLocation(), // clang::SourceLocation StartLoc
7202 clang::SourceLocation(), // clang::SourceLocation IdLoc
7203 name ? &ast->getASTContext()->Idents.get(name) : nullptr, // clang::IdentifierInfo *
7204 GetQualType(var_type), // Variable clang::QualType
7205 nullptr, // TypeSourceInfo *
7206 clang::SC_Static); // StorageClass
7207 if (var_decl)
7208 {
7209 var_decl->setAccess(ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7210 record_decl->addDecl(var_decl);
7211
7212#ifdef LLDB_CONFIGURATION_DEBUG
7213 VerifyDecl(var_decl);
7214#endif
7215 }
7216 }
7217 return var_decl;
7218}
7219
7220
7221clang::CXXMethodDecl *
7222ClangASTContext::AddMethodToCXXRecordType (void* type, const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007223 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007224 lldb::AccessType access,
7225 bool is_virtual,
7226 bool is_static,
7227 bool is_inline,
7228 bool is_explicit,
7229 bool is_attr_used,
7230 bool is_artificial)
7231{
7232 if (!type || !method_clang_type.IsValid() || name == nullptr || name[0] == '\0')
7233 return nullptr;
7234
7235 clang::QualType record_qual_type(GetCanonicalQualType(type));
7236
7237 clang::CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
7238
7239 if (cxx_record_decl == nullptr)
7240 return nullptr;
7241
7242 clang::QualType method_qual_type (GetQualType(method_clang_type));
7243
7244 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7245
7246 clang::DeclarationName decl_name (&getASTContext()->Idents.get(name));
7247
7248 const clang::FunctionType *function_type = llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7249
7250 if (function_type == nullptr)
7251 return nullptr;
7252
7253 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7254
7255 if (!method_function_prototype)
7256 return nullptr;
7257
7258 unsigned int num_params = method_function_prototype->getNumParams();
7259
7260 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7261 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7262
7263 if (is_artificial)
7264 return nullptr; // skip everything artificial
7265
7266 if (name[0] == '~')
7267 {
7268 cxx_dtor_decl = clang::CXXDestructorDecl::Create (*getASTContext(),
7269 cxx_record_decl,
7270 clang::SourceLocation(),
7271 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXDestructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7272 method_qual_type,
7273 nullptr,
7274 is_inline,
7275 is_artificial);
7276 cxx_method_decl = cxx_dtor_decl;
7277 }
7278 else if (decl_name == cxx_record_decl->getDeclName())
7279 {
7280 cxx_ctor_decl = clang::CXXConstructorDecl::Create (*getASTContext(),
7281 cxx_record_decl,
7282 clang::SourceLocation(),
7283 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConstructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7284 method_qual_type,
7285 nullptr, // TypeSourceInfo *
7286 is_explicit,
7287 is_inline,
7288 is_artificial,
7289 false /*is_constexpr*/);
7290 cxx_method_decl = cxx_ctor_decl;
7291 }
7292 else
7293 {
7294 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7295 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7296
7297 if (IsOperator (name, op_kind))
7298 {
7299 if (op_kind != clang::NUM_OVERLOADED_OPERATORS)
7300 {
7301 // Check the number of operator parameters. Sometimes we have
7302 // seen bad DWARF that doesn't correctly describe operators and
7303 // if we try to create a method and add it to the class, clang
7304 // will assert and crash, so we need to make sure things are
7305 // acceptable.
7306 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
7307 return nullptr;
7308 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7309 cxx_record_decl,
7310 clang::SourceLocation(),
7311 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXOperatorName (op_kind), clang::SourceLocation()),
7312 method_qual_type,
7313 nullptr, // TypeSourceInfo *
7314 SC,
7315 is_inline,
7316 false /*is_constexpr*/,
7317 clang::SourceLocation());
7318 }
7319 else if (num_params == 0)
7320 {
7321 // Conversion operators don't take params...
7322 cxx_method_decl = clang::CXXConversionDecl::Create (*getASTContext(),
7323 cxx_record_decl,
7324 clang::SourceLocation(),
7325 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConversionFunctionName (getASTContext()->getCanonicalType (function_type->getReturnType())), clang::SourceLocation()),
7326 method_qual_type,
7327 nullptr, // TypeSourceInfo *
7328 is_inline,
7329 is_explicit,
7330 false /*is_constexpr*/,
7331 clang::SourceLocation());
7332 }
7333 }
7334
7335 if (cxx_method_decl == nullptr)
7336 {
7337 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7338 cxx_record_decl,
7339 clang::SourceLocation(),
7340 clang::DeclarationNameInfo (decl_name, clang::SourceLocation()),
7341 method_qual_type,
7342 nullptr, // TypeSourceInfo *
7343 SC,
7344 is_inline,
7345 false /*is_constexpr*/,
7346 clang::SourceLocation());
7347 }
7348 }
7349
7350 clang::AccessSpecifier access_specifier = ClangASTContext::ConvertAccessTypeToAccessSpecifier (access);
7351
7352 cxx_method_decl->setAccess (access_specifier);
7353 cxx_method_decl->setVirtualAsWritten (is_virtual);
7354
7355 if (is_attr_used)
7356 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
7357
7358 // Populate the method decl with parameter decls
7359
7360 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
7361
7362 for (unsigned param_index = 0;
7363 param_index < num_params;
7364 ++param_index)
7365 {
7366 params.push_back (clang::ParmVarDecl::Create (*getASTContext(),
7367 cxx_method_decl,
7368 clang::SourceLocation(),
7369 clang::SourceLocation(),
7370 nullptr, // anonymous
7371 method_function_prototype->getParamType(param_index),
7372 nullptr,
7373 clang::SC_None,
7374 nullptr));
7375 }
7376
7377 cxx_method_decl->setParams (llvm::ArrayRef<clang::ParmVarDecl*>(params));
7378
7379 cxx_record_decl->addDecl (cxx_method_decl);
7380
7381 // Sometimes the debug info will mention a constructor (default/copy/move),
7382 // destructor, or assignment operator (copy/move) but there won't be any
7383 // version of this in the code. So we check if the function was artificially
7384 // generated and if it is trivial and this lets the compiler/backend know
7385 // that it can inline the IR for these when it needs to and we can avoid a
7386 // "missing function" error when running expressions.
7387
7388 if (is_artificial)
7389 {
7390 if (cxx_ctor_decl &&
7391 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
7392 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
7393 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
7394 {
7395 cxx_ctor_decl->setDefaulted();
7396 cxx_ctor_decl->setTrivial(true);
7397 }
7398 else if (cxx_dtor_decl)
7399 {
7400 if (cxx_record_decl->hasTrivialDestructor())
7401 {
7402 cxx_dtor_decl->setDefaulted();
7403 cxx_dtor_decl->setTrivial(true);
7404 }
7405 }
7406 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
7407 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
7408 {
7409 cxx_method_decl->setDefaulted();
7410 cxx_method_decl->setTrivial(true);
7411 }
7412 }
7413
7414#ifdef LLDB_CONFIGURATION_DEBUG
7415 VerifyDecl(cxx_method_decl);
7416#endif
7417
7418 // printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
7419 // printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
7420 // printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
7421 // printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
7422 // printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
7423 // printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
7424 // printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
7425 // printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
7426 // printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
7427 return cxx_method_decl;
7428}
7429
7430
7431#pragma mark C++ Base Classes
7432
7433clang::CXXBaseSpecifier *
7434ClangASTContext::CreateBaseClassSpecifier (void* type, AccessType access, bool is_virtual, bool base_of_class)
7435{
7436 if (type)
7437 return new clang::CXXBaseSpecifier (clang::SourceRange(),
7438 is_virtual,
7439 base_of_class,
7440 ClangASTContext::ConvertAccessTypeToAccessSpecifier (access),
7441 getASTContext()->getTrivialTypeSourceInfo (GetQualType(type)),
7442 clang::SourceLocation());
7443 return nullptr;
7444}
7445
7446void
7447ClangASTContext::DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes)
7448{
7449 for (unsigned i=0; i<num_base_classes; ++i)
7450 {
7451 delete base_classes[i];
7452 base_classes[i] = nullptr;
7453 }
7454}
7455
7456bool
7457ClangASTContext::SetBaseClassesForClassType (void* type, clang::CXXBaseSpecifier const * const *base_classes,
7458 unsigned num_base_classes)
7459{
7460 if (type)
7461 {
7462 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
7463 if (cxx_record_decl)
7464 {
7465 cxx_record_decl->setBases(base_classes, num_base_classes);
7466 return true;
7467 }
7468 }
7469 return false;
7470}
7471
7472bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007473ClangASTContext::SetObjCSuperClass (const CompilerType& type, const CompilerType &superclass_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007474{
7475 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
7476 if (!ast)
7477 return false;
7478 clang::ASTContext* clang_ast = ast->getASTContext();
7479
7480 if (type && superclass_clang_type.IsValid() && superclass_clang_type.GetTypeSystem() == type.GetTypeSystem())
7481 {
7482 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7483 clang::ObjCInterfaceDecl *super_interface_decl = GetAsObjCInterfaceDecl (superclass_clang_type);
7484 if (class_interface_decl && super_interface_decl)
7485 {
7486 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(clang_ast->getObjCInterfaceType(super_interface_decl)));
7487 return true;
7488 }
7489 }
7490 return false;
7491}
7492
7493bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007494ClangASTContext::AddObjCClassProperty (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007495 const char *property_name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007496 const CompilerType &property_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007497 clang::ObjCIvarDecl *ivar_decl,
7498 const char *property_setter_name,
7499 const char *property_getter_name,
7500 uint32_t property_attributes,
7501 ClangASTMetadata *metadata)
7502{
7503 if (!type || !property_clang_type.IsValid() || property_name == nullptr || property_name[0] == '\0')
7504 return false;
7505 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
7506 if (!ast)
7507 return false;
7508 clang::ASTContext* clang_ast = ast->getASTContext();
7509
7510 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7511
7512 if (class_interface_decl)
7513 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00007514 CompilerType property_clang_type_to_access;
Greg Claytond8d4a572015-08-11 21:38:15 +00007515
7516 if (property_clang_type.IsValid())
7517 property_clang_type_to_access = property_clang_type;
7518 else if (ivar_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00007519 property_clang_type_to_access = CompilerType (clang_ast, ivar_decl->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00007520
7521 if (class_interface_decl && property_clang_type_to_access.IsValid())
7522 {
7523 clang::TypeSourceInfo *prop_type_source;
7524 if (ivar_decl)
7525 prop_type_source = clang_ast->getTrivialTypeSourceInfo (ivar_decl->getType());
7526 else
7527 prop_type_source = clang_ast->getTrivialTypeSourceInfo (GetQualType(property_clang_type));
7528
7529 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create (*clang_ast,
7530 class_interface_decl,
7531 clang::SourceLocation(), // Source Location
7532 &clang_ast->Idents.get(property_name),
7533 clang::SourceLocation(), //Source Location for AT
7534 clang::SourceLocation(), //Source location for (
7535 ivar_decl ? ivar_decl->getType() : ClangASTContext::GetQualType(property_clang_type),
7536 prop_type_source);
7537
7538 if (property_decl)
7539 {
7540 if (metadata)
7541 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
7542
7543 class_interface_decl->addDecl (property_decl);
7544
7545 clang::Selector setter_sel, getter_sel;
7546
7547 if (property_setter_name != nullptr)
7548 {
7549 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
7550 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(property_setter_no_colon.c_str());
7551 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
7552 }
7553 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
7554 {
7555 std::string setter_sel_string("set");
7556 setter_sel_string.push_back(::toupper(property_name[0]));
7557 setter_sel_string.append(&property_name[1]);
7558 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(setter_sel_string.c_str());
7559 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
7560 }
7561 property_decl->setSetterName(setter_sel);
7562 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
7563
7564 if (property_getter_name != nullptr)
7565 {
7566 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_getter_name);
7567 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
7568 }
7569 else
7570 {
7571 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_name);
7572 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
7573 }
7574 property_decl->setGetterName(getter_sel);
7575 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
7576
7577 if (ivar_decl)
7578 property_decl->setPropertyIvarDecl (ivar_decl);
7579
7580 if (property_attributes & DW_APPLE_PROPERTY_readonly)
7581 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
7582 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
7583 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
7584 if (property_attributes & DW_APPLE_PROPERTY_assign)
7585 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
7586 if (property_attributes & DW_APPLE_PROPERTY_retain)
7587 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
7588 if (property_attributes & DW_APPLE_PROPERTY_copy)
7589 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
7590 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
7591 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
7592
7593 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
7594 {
7595 const bool isInstance = true;
7596 const bool isVariadic = false;
7597 const bool isSynthesized = false;
7598 const bool isImplicitlyDeclared = true;
7599 const bool isDefined = false;
7600 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
7601 const bool HasRelatedResultType = false;
7602
7603 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create (*clang_ast,
7604 clang::SourceLocation(),
7605 clang::SourceLocation(),
7606 getter_sel,
7607 GetQualType(property_clang_type_to_access),
7608 nullptr,
7609 class_interface_decl,
7610 isInstance,
7611 isVariadic,
7612 isSynthesized,
7613 isImplicitlyDeclared,
7614 isDefined,
7615 impControl,
7616 HasRelatedResultType);
7617
7618 if (getter && metadata)
7619 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
7620
7621 if (getter)
7622 {
7623 getter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(), llvm::ArrayRef<clang::SourceLocation>());
7624
7625 class_interface_decl->addDecl(getter);
7626 }
7627 }
7628
7629 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
7630 {
7631 clang::QualType result_type = clang_ast->VoidTy;
7632
7633 const bool isInstance = true;
7634 const bool isVariadic = false;
7635 const bool isSynthesized = false;
7636 const bool isImplicitlyDeclared = true;
7637 const bool isDefined = false;
7638 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
7639 const bool HasRelatedResultType = false;
7640
7641 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create (*clang_ast,
7642 clang::SourceLocation(),
7643 clang::SourceLocation(),
7644 setter_sel,
7645 result_type,
7646 nullptr,
7647 class_interface_decl,
7648 isInstance,
7649 isVariadic,
7650 isSynthesized,
7651 isImplicitlyDeclared,
7652 isDefined,
7653 impControl,
7654 HasRelatedResultType);
7655
7656 if (setter && metadata)
7657 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
7658
7659 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
7660
7661 params.push_back (clang::ParmVarDecl::Create (*clang_ast,
7662 setter,
7663 clang::SourceLocation(),
7664 clang::SourceLocation(),
7665 nullptr, // anonymous
7666 GetQualType(property_clang_type_to_access),
7667 nullptr,
7668 clang::SC_Auto,
7669 nullptr));
7670
7671 if (setter)
7672 {
7673 setter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
7674
7675 class_interface_decl->addDecl(setter);
7676 }
7677 }
7678
7679 return true;
7680 }
7681 }
7682 }
7683 return false;
7684}
7685
7686bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007687ClangASTContext::IsObjCClassTypeAndHasIVars (const CompilerType& type, bool check_superclass)
Greg Claytond8d4a572015-08-11 21:38:15 +00007688{
7689 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7690 if (class_interface_decl)
7691 return ObjCDeclHasIVars (class_interface_decl, check_superclass);
7692 return false;
7693}
7694
7695
7696clang::ObjCMethodDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007697ClangASTContext::AddMethodToObjCObjectType (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007698 const char *name, // the full symbol name as seen in the symbol table (void* type, "-[NString stringWithCString:]")
Greg Claytona1e5dc82015-08-11 22:53:00 +00007699 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007700 lldb::AccessType access,
7701 bool is_artificial)
7702{
7703 if (!type || !method_clang_type.IsValid())
7704 return nullptr;
7705
7706 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
7707
7708 if (class_interface_decl == nullptr)
7709 return nullptr;
7710 clang::ASTContext* ast = type.GetTypeSystem()->AsClangASTContext()->getASTContext();
7711
7712 const char *selector_start = ::strchr (name, ' ');
7713 if (selector_start == nullptr)
7714 return nullptr;
7715
7716 selector_start++;
7717 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
7718
7719 size_t len = 0;
7720 const char *start;
7721 //printf ("name = '%s'\n", name);
7722
7723 unsigned num_selectors_with_args = 0;
7724 for (start = selector_start;
7725 start && *start != '\0' && *start != ']';
7726 start += len)
7727 {
7728 len = ::strcspn(start, ":]");
7729 bool has_arg = (start[len] == ':');
7730 if (has_arg)
7731 ++num_selectors_with_args;
7732 selector_idents.push_back (&ast->Idents.get (llvm::StringRef (start, len)));
7733 if (has_arg)
7734 len += 1;
7735 }
7736
7737
7738 if (selector_idents.size() == 0)
7739 return nullptr;
7740
7741 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
7742 selector_idents.data());
7743
7744 clang::QualType method_qual_type (GetQualType(method_clang_type));
7745
7746 // Populate the method decl with parameter decls
7747 const clang::Type *method_type(method_qual_type.getTypePtr());
7748
7749 if (method_type == nullptr)
7750 return nullptr;
7751
7752 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(method_type));
7753
7754 if (!method_function_prototype)
7755 return nullptr;
7756
7757
7758 bool is_variadic = false;
7759 bool is_synthesized = false;
7760 bool is_defined = false;
7761 clang::ObjCMethodDecl::ImplementationControl imp_control = clang::ObjCMethodDecl::None;
7762
7763 const unsigned num_args = method_function_prototype->getNumParams();
7764
7765 if (num_args != num_selectors_with_args)
7766 return nullptr; // some debug information is corrupt. We are not going to deal with it.
7767
7768 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create (*ast,
7769 clang::SourceLocation(), // beginLoc,
7770 clang::SourceLocation(), // endLoc,
7771 method_selector,
7772 method_function_prototype->getReturnType(),
7773 nullptr, // TypeSourceInfo *ResultTInfo,
7774 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(GetQualType(type)),
7775 name[0] == '-',
7776 is_variadic,
7777 is_synthesized,
7778 true, // is_implicitly_declared; we force this to true because we don't have source locations
7779 is_defined,
7780 imp_control,
7781 false /*has_related_result_type*/);
7782
7783
7784 if (objc_method_decl == nullptr)
7785 return nullptr;
7786
7787 if (num_args > 0)
7788 {
7789 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
7790
7791 for (unsigned param_index = 0; param_index < num_args; ++param_index)
7792 {
7793 params.push_back (clang::ParmVarDecl::Create (*ast,
7794 objc_method_decl,
7795 clang::SourceLocation(),
7796 clang::SourceLocation(),
7797 nullptr, // anonymous
7798 method_function_prototype->getParamType(param_index),
7799 nullptr,
7800 clang::SC_Auto,
7801 nullptr));
7802 }
7803
7804 objc_method_decl->setMethodParams(*ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
7805 }
7806
7807 class_interface_decl->addDecl (objc_method_decl);
7808
7809#ifdef LLDB_CONFIGURATION_DEBUG
7810 VerifyDecl(objc_method_decl);
7811#endif
7812
7813 return objc_method_decl;
7814}
7815
7816bool
7817ClangASTContext::SetHasExternalStorage (void* type, bool has_extern)
7818{
7819 if (!type)
7820 return false;
7821
7822 clang::QualType qual_type (GetCanonicalQualType(type));
7823
7824 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7825 switch (type_class)
7826 {
7827 case clang::Type::Record:
7828 {
7829 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
7830 if (cxx_record_decl)
7831 {
7832 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
7833 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
7834 return true;
7835 }
7836 }
7837 break;
7838
7839 case clang::Type::Enum:
7840 {
7841 clang::EnumDecl *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getDecl();
7842 if (enum_decl)
7843 {
7844 enum_decl->setHasExternalLexicalStorage (has_extern);
7845 enum_decl->setHasExternalVisibleStorage (has_extern);
7846 return true;
7847 }
7848 }
7849 break;
7850
7851 case clang::Type::ObjCObject:
7852 case clang::Type::ObjCInterface:
7853 {
7854 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7855 assert (objc_class_type);
7856 if (objc_class_type)
7857 {
7858 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
7859
7860 if (class_interface_decl)
7861 {
7862 class_interface_decl->setHasExternalLexicalStorage (has_extern);
7863 class_interface_decl->setHasExternalVisibleStorage (has_extern);
7864 return true;
7865 }
7866 }
7867 }
7868 break;
7869
7870 case clang::Type::Typedef:
7871 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
7872
7873 case clang::Type::Elaborated:
7874 return SetHasExternalStorage (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
7875
7876 case clang::Type::Paren:
7877 return SetHasExternalStorage (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), has_extern);
7878
7879 default:
7880 break;
7881 }
7882 return false;
7883}
7884
7885
7886#pragma mark TagDecl
7887
7888bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007889ClangASTContext::StartTagDeclarationDefinition (const CompilerType &type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007890{
7891 if (type)
7892 {
7893
7894 clang::QualType qual_type (GetQualType(type));
7895 const clang::Type *t = qual_type.getTypePtr();
7896 if (t)
7897 {
7898 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(t);
7899 if (tag_type)
7900 {
7901 clang::TagDecl *tag_decl = tag_type->getDecl();
7902 if (tag_decl)
7903 {
7904 tag_decl->startDefinition();
7905 return true;
7906 }
7907 }
7908
7909 const clang::ObjCObjectType *object_type = llvm::dyn_cast<clang::ObjCObjectType>(t);
7910 if (object_type)
7911 {
7912 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
7913 if (interface_decl)
7914 {
7915 interface_decl->startDefinition();
7916 return true;
7917 }
7918 }
7919 }
7920 }
7921 return false;
7922}
7923
7924bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007925ClangASTContext::CompleteTagDeclarationDefinition (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007926{
7927 if (type)
7928 {
7929 clang::QualType qual_type (GetQualType(type));
7930 if (qual_type.isNull())
7931 return false;
7932 clang::ASTContext* ast = type.GetTypeSystem()->AsClangASTContext()->getASTContext();
7933
7934 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
7935
7936 if (cxx_record_decl)
7937 {
7938 cxx_record_decl->completeDefinition();
7939
7940 return true;
7941 }
7942
7943 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(qual_type.getTypePtr());
7944
7945 if (enutype)
7946 {
7947 clang::EnumDecl *enum_decl = enutype->getDecl();
7948
7949 if (enum_decl)
7950 {
7951 /// TODO This really needs to be fixed.
7952
7953 unsigned NumPositiveBits = 1;
7954 unsigned NumNegativeBits = 0;
7955
7956 clang::QualType promotion_qual_type;
7957 // If the enum integer type is less than an integer in bit width,
7958 // then we must promote it to an integer size.
7959 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
7960 {
7961 if (enum_decl->getIntegerType()->isSignedIntegerType())
7962 promotion_qual_type = ast->IntTy;
7963 else
7964 promotion_qual_type = ast->UnsignedIntTy;
7965 }
7966 else
7967 promotion_qual_type = enum_decl->getIntegerType();
7968
7969 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
7970 return true;
7971 }
7972 }
7973 }
7974 return false;
7975}
7976
Greg Claytond8d4a572015-08-11 21:38:15 +00007977bool
Greg Clayton8b4edba2015-08-14 20:02:05 +00007978ClangASTContext::AddEnumerationValueToEnumerationType (void* type,
7979 const CompilerType &enumerator_clang_type,
7980 const Declaration &decl,
7981 const char *name,
7982 int64_t enum_value,
7983 uint32_t enum_value_bit_size)
Greg Claytond8d4a572015-08-11 21:38:15 +00007984{
7985 if (type && enumerator_clang_type.IsValid() && name && name[0])
7986 {
7987 clang::QualType enum_qual_type (GetCanonicalQualType(type));
7988
7989 bool is_signed = false;
7990 enumerator_clang_type.IsIntegerType (is_signed);
7991 const clang::Type *clang_type = enum_qual_type.getTypePtr();
7992 if (clang_type)
7993 {
7994 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
7995
7996 if (enutype)
7997 {
7998 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
7999 enum_llvm_apsint = enum_value;
8000 clang::EnumConstantDecl *enumerator_decl =
8001 clang::EnumConstantDecl::Create (*getASTContext(),
8002 enutype->getDecl(),
8003 clang::SourceLocation(),
8004 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
8005 GetQualType(enumerator_clang_type),
8006 nullptr,
8007 enum_llvm_apsint);
8008
8009 if (enumerator_decl)
8010 {
8011 enutype->getDecl()->addDecl(enumerator_decl);
8012
8013#ifdef LLDB_CONFIGURATION_DEBUG
8014 VerifyDecl(enumerator_decl);
8015#endif
8016
8017 return true;
8018 }
8019 }
8020 }
8021 }
8022 return false;
8023}
8024
Greg Claytona1e5dc82015-08-11 22:53:00 +00008025CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00008026ClangASTContext::GetEnumerationIntegerType (void* type)
8027{
8028 clang::QualType enum_qual_type (GetCanonicalQualType(type));
8029 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8030 if (clang_type)
8031 {
8032 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8033 if (enutype)
8034 {
8035 clang::EnumDecl *enum_decl = enutype->getDecl();
8036 if (enum_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008037 return CompilerType (getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008038 }
8039 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008040 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008041}
8042
Greg Claytona1e5dc82015-08-11 22:53:00 +00008043CompilerType
8044ClangASTContext::CreateMemberPointerType (const CompilerType& type, const CompilerType &pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008045{
8046 if (type && pointee_type.IsValid() && type.GetTypeSystem() == pointee_type.GetTypeSystem())
8047 {
8048 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
8049 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008050 return CompilerType();
8051 return CompilerType (ast->getASTContext(),
Greg Claytond8d4a572015-08-11 21:38:15 +00008052 ast->getASTContext()->getMemberPointerType (GetQualType(pointee_type),
8053 GetQualType(type).getTypePtr()));
8054 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008055 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008056}
8057
8058
8059size_t
8060ClangASTContext::ConvertStringToFloatValue (void* type, const char *s, uint8_t *dst, size_t dst_size)
8061{
8062 if (type)
8063 {
8064 clang::QualType qual_type (GetCanonicalQualType(type));
8065 uint32_t count = 0;
8066 bool is_complex = false;
8067 if (IsFloatingPointType (type, count, is_complex))
8068 {
8069 // TODO: handle complex and vector types
8070 if (count != 1)
8071 return false;
8072
8073 llvm::StringRef s_sref(s);
8074 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type), s_sref);
8075
8076 const uint64_t bit_size = getASTContext()->getTypeSize (qual_type);
8077 const uint64_t byte_size = bit_size / 8;
8078 if (dst_size >= byte_size)
8079 {
8080 if (bit_size == sizeof(float)*8)
8081 {
8082 float float32 = ap_float.convertToFloat();
8083 ::memcpy (dst, &float32, byte_size);
8084 return byte_size;
8085 }
8086 else if (bit_size >= 64)
8087 {
8088 llvm::APInt ap_int(ap_float.bitcastToAPInt());
8089 ::memcpy (dst, ap_int.getRawData(), byte_size);
8090 return byte_size;
8091 }
8092 }
8093 }
8094 }
8095 return 0;
8096}
8097
8098
8099
8100//----------------------------------------------------------------------
8101// Dumping types
8102//----------------------------------------------------------------------
8103#define DEPTH_INCREMENT 2
8104
8105void
8106ClangASTContext::DumpValue (void* type, ExecutionContext *exe_ctx,
8107 Stream *s,
8108 lldb::Format format,
8109 const lldb_private::DataExtractor &data,
8110 lldb::offset_t data_byte_offset,
8111 size_t data_byte_size,
8112 uint32_t bitfield_bit_size,
8113 uint32_t bitfield_bit_offset,
8114 bool show_types,
8115 bool show_summary,
8116 bool verbose,
8117 uint32_t depth)
8118{
8119 if (!type)
8120 return;
8121
8122 clang::QualType qual_type(GetQualType(type));
8123 switch (qual_type->getTypeClass())
8124 {
8125 case clang::Type::Record:
8126 if (GetCompleteType(type))
8127 {
8128 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8129 const clang::RecordDecl *record_decl = record_type->getDecl();
8130 assert(record_decl);
8131 uint32_t field_bit_offset = 0;
8132 uint32_t field_byte_offset = 0;
8133 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
8134 uint32_t child_idx = 0;
8135
8136 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8137 if (cxx_record_decl)
8138 {
8139 // We might have base classes to print out first
8140 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
8141 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
8142 base_class != base_class_end;
8143 ++base_class)
8144 {
8145 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
8146
8147 // Skip empty base classes
8148 if (verbose == false && ClangASTContext::RecordHasFields(base_class_decl) == false)
8149 continue;
8150
8151 if (base_class->isVirtual())
8152 field_bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
8153 else
8154 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
8155 field_byte_offset = field_bit_offset / 8;
8156 assert (field_bit_offset % 8 == 0);
8157 if (child_idx == 0)
8158 s->PutChar('{');
8159 else
8160 s->PutChar(',');
8161
8162 clang::QualType base_class_qual_type = base_class->getType();
8163 std::string base_class_type_name(base_class_qual_type.getAsString());
8164
8165 // Indent and print the base class type name
8166 s->Printf("\n%*s%s ", depth + DEPTH_INCREMENT, "", base_class_type_name.c_str());
8167
8168 clang::TypeInfo base_class_type_info = getASTContext()->getTypeInfo(base_class_qual_type);
8169
8170 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008171 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008172 base_clang_type.DumpValue (exe_ctx,
8173 s, // Stream to dump to
8174 base_clang_type.GetFormat(), // The format with which to display the member
8175 data, // Data buffer containing all bytes for this type
8176 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8177 base_class_type_info.Width / 8, // Size of this type in bytes
8178 0, // Bitfield bit size
8179 0, // Bitfield bit offset
8180 show_types, // Boolean indicating if we should show the variable types
8181 show_summary, // Boolean indicating if we should show a summary for the current type
8182 verbose, // Verbose output?
8183 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8184
8185 ++child_idx;
8186 }
8187 }
8188 uint32_t field_idx = 0;
8189 clang::RecordDecl::field_iterator field, field_end;
8190 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
8191 {
8192 // Print the starting squiggly bracket (if this is the
8193 // first member) or comma (for member 2 and beyond) for
8194 // the struct/union/class member.
8195 if (child_idx == 0)
8196 s->PutChar('{');
8197 else
8198 s->PutChar(',');
8199
8200 // Indent
8201 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8202
8203 clang::QualType field_type = field->getType();
8204 // Print the member type if requested
8205 // Figure out the type byte size (field_type_info.first) and
8206 // alignment (field_type_info.second) from the AST context.
8207 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(field_type);
8208 assert(field_idx < record_layout.getFieldCount());
8209 // Figure out the field offset within the current struct/union/class type
8210 field_bit_offset = record_layout.getFieldOffset (field_idx);
8211 field_byte_offset = field_bit_offset / 8;
8212 uint32_t field_bitfield_bit_size = 0;
8213 uint32_t field_bitfield_bit_offset = 0;
8214 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, field_bitfield_bit_size))
8215 field_bitfield_bit_offset = field_bit_offset % 8;
8216
8217 if (show_types)
8218 {
8219 std::string field_type_name(field_type.getAsString());
8220 if (field_bitfield_bit_size > 0)
8221 s->Printf("(%s:%u) ", field_type_name.c_str(), field_bitfield_bit_size);
8222 else
8223 s->Printf("(%s) ", field_type_name.c_str());
8224 }
8225 // Print the member name and equal sign
8226 s->Printf("%s = ", field->getNameAsString().c_str());
8227
8228
8229 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008230 CompilerType field_clang_type (getASTContext(), field_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008231 field_clang_type.DumpValue (exe_ctx,
8232 s, // Stream to dump to
8233 field_clang_type.GetFormat(), // The format with which to display the member
8234 data, // Data buffer containing all bytes for this type
8235 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8236 field_type_info.Width / 8, // Size of this type in bytes
8237 field_bitfield_bit_size, // Bitfield bit size
8238 field_bitfield_bit_offset, // Bitfield bit offset
8239 show_types, // Boolean indicating if we should show the variable types
8240 show_summary, // Boolean indicating if we should show a summary for the current type
8241 verbose, // Verbose output?
8242 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8243 }
8244
8245 // Indent the trailing squiggly bracket
8246 if (child_idx > 0)
8247 s->Printf("\n%*s}", depth, "");
8248 }
8249 return;
8250
8251 case clang::Type::Enum:
8252 if (GetCompleteType(type))
8253 {
8254 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8255 const clang::EnumDecl *enum_decl = enutype->getDecl();
8256 assert(enum_decl);
8257 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8258 lldb::offset_t offset = data_byte_offset;
8259 const int64_t enum_value = data.GetMaxU64Bitfield(&offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
8260 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8261 {
8262 if (enum_pos->getInitVal() == enum_value)
8263 {
8264 s->Printf("%s", enum_pos->getNameAsString().c_str());
8265 return;
8266 }
8267 }
8268 // If we have gotten here we didn't get find the enumerator in the
8269 // enum decl, so just print the integer.
8270 s->Printf("%" PRIi64, enum_value);
8271 }
8272 return;
8273
8274 case clang::Type::ConstantArray:
8275 {
8276 const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
8277 bool is_array_of_characters = false;
8278 clang::QualType element_qual_type = array->getElementType();
8279
8280 const clang::Type *canonical_type = element_qual_type->getCanonicalTypeInternal().getTypePtr();
8281 if (canonical_type)
8282 is_array_of_characters = canonical_type->isCharType();
8283
8284 const uint64_t element_count = array->getSize().getLimitedValue();
8285
8286 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(element_qual_type);
8287
8288 uint32_t element_idx = 0;
8289 uint32_t element_offset = 0;
8290 uint64_t element_byte_size = field_type_info.Width / 8;
8291 uint32_t element_stride = element_byte_size;
8292
8293 if (is_array_of_characters)
8294 {
8295 s->PutChar('"');
8296 data.Dump(s, data_byte_offset, lldb::eFormatChar, element_byte_size, element_count, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
8297 s->PutChar('"');
8298 return;
8299 }
8300 else
8301 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00008302 CompilerType element_clang_type(getASTContext(), element_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008303 lldb::Format element_format = element_clang_type.GetFormat();
8304
8305 for (element_idx = 0; element_idx < element_count; ++element_idx)
8306 {
8307 // Print the starting squiggly bracket (if this is the
8308 // first member) or comman (for member 2 and beyong) for
8309 // the struct/union/class member.
8310 if (element_idx == 0)
8311 s->PutChar('{');
8312 else
8313 s->PutChar(',');
8314
8315 // Indent and print the index
8316 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
8317
8318 // Figure out the field offset within the current struct/union/class type
8319 element_offset = element_idx * element_stride;
8320
8321 // Dump the value of the member
8322 element_clang_type.DumpValue (exe_ctx,
8323 s, // Stream to dump to
8324 element_format, // The format with which to display the element
8325 data, // Data buffer containing all bytes for this type
8326 data_byte_offset + element_offset,// Offset into "data" where to grab value from
8327 element_byte_size, // Size of this type in bytes
8328 0, // Bitfield bit size
8329 0, // Bitfield bit offset
8330 show_types, // Boolean indicating if we should show the variable types
8331 show_summary, // Boolean indicating if we should show a summary for the current type
8332 verbose, // Verbose output?
8333 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8334 }
8335
8336 // Indent the trailing squiggly bracket
8337 if (element_idx > 0)
8338 s->Printf("\n%*s}", depth, "");
8339 }
8340 }
8341 return;
8342
8343 case clang::Type::Typedef:
8344 {
8345 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
8346
Greg Claytona1e5dc82015-08-11 22:53:00 +00008347 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008348 lldb::Format typedef_format = typedef_clang_type.GetFormat();
8349 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
8350 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
8351
8352 return typedef_clang_type.DumpValue (exe_ctx,
8353 s, // Stream to dump to
8354 typedef_format, // The format with which to display the element
8355 data, // Data buffer containing all bytes for this type
8356 data_byte_offset, // Offset into "data" where to grab value from
8357 typedef_byte_size, // Size of this type in bytes
8358 bitfield_bit_size, // Bitfield bit size
8359 bitfield_bit_offset,// Bitfield bit offset
8360 show_types, // Boolean indicating if we should show the variable types
8361 show_summary, // Boolean indicating if we should show a summary for the current type
8362 verbose, // Verbose output?
8363 depth); // Scope depth for any types that have children
8364 }
8365 break;
8366
8367 case clang::Type::Elaborated:
8368 {
8369 clang::QualType elaborated_qual_type = llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008370 CompilerType elaborated_clang_type (getASTContext(), elaborated_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008371 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
8372 clang::TypeInfo elaborated_type_info = getASTContext()->getTypeInfo(elaborated_qual_type);
8373 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
8374
8375 return elaborated_clang_type.DumpValue (exe_ctx,
8376 s, // Stream to dump to
8377 elaborated_format, // The format with which to display the element
8378 data, // Data buffer containing all bytes for this type
8379 data_byte_offset, // Offset into "data" where to grab value from
8380 elaborated_byte_size, // Size of this type in bytes
8381 bitfield_bit_size, // Bitfield bit size
8382 bitfield_bit_offset,// Bitfield bit offset
8383 show_types, // Boolean indicating if we should show the variable types
8384 show_summary, // Boolean indicating if we should show a summary for the current type
8385 verbose, // Verbose output?
8386 depth); // Scope depth for any types that have children
8387 }
8388 break;
8389
8390 case clang::Type::Paren:
8391 {
8392 clang::QualType desugar_qual_type = llvm::cast<clang::ParenType>(qual_type)->desugar();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008393 CompilerType desugar_clang_type (getASTContext(), desugar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008394
8395 lldb::Format desugar_format = desugar_clang_type.GetFormat();
8396 clang::TypeInfo desugar_type_info = getASTContext()->getTypeInfo(desugar_qual_type);
8397 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
8398
8399 return desugar_clang_type.DumpValue (exe_ctx,
8400 s, // Stream to dump to
8401 desugar_format, // The format with which to display the element
8402 data, // Data buffer containing all bytes for this type
8403 data_byte_offset, // Offset into "data" where to grab value from
8404 desugar_byte_size, // Size of this type in bytes
8405 bitfield_bit_size, // Bitfield bit size
8406 bitfield_bit_offset,// Bitfield bit offset
8407 show_types, // Boolean indicating if we should show the variable types
8408 show_summary, // Boolean indicating if we should show a summary for the current type
8409 verbose, // Verbose output?
8410 depth); // Scope depth for any types that have children
8411 }
8412 break;
8413
8414 default:
8415 // We are down to a scalar type that we just need to display.
8416 data.Dump(s,
8417 data_byte_offset,
8418 format,
8419 data_byte_size,
8420 1,
8421 UINT32_MAX,
8422 LLDB_INVALID_ADDRESS,
8423 bitfield_bit_size,
8424 bitfield_bit_offset);
8425
8426 if (show_summary)
8427 DumpSummary (type, exe_ctx, s, data, data_byte_offset, data_byte_size);
8428 break;
8429 }
8430}
8431
8432
8433
8434
8435bool
8436ClangASTContext::DumpTypeValue (void* type, Stream *s,
8437 lldb::Format format,
8438 const lldb_private::DataExtractor &data,
8439 lldb::offset_t byte_offset,
8440 size_t byte_size,
8441 uint32_t bitfield_bit_size,
8442 uint32_t bitfield_bit_offset,
8443 ExecutionContextScope *exe_scope)
8444{
8445 if (!type)
8446 return false;
8447 if (IsAggregateType(type))
8448 {
8449 return false;
8450 }
8451 else
8452 {
8453 clang::QualType qual_type(GetQualType(type));
8454
8455 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8456 switch (type_class)
8457 {
8458 case clang::Type::Typedef:
8459 {
8460 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008461 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008462 if (format == eFormatDefault)
8463 format = typedef_clang_type.GetFormat();
8464 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
8465 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
8466
8467 return typedef_clang_type.DumpTypeValue (s,
8468 format, // The format with which to display the element
8469 data, // Data buffer containing all bytes for this type
8470 byte_offset, // Offset into "data" where to grab value from
8471 typedef_byte_size, // Size of this type in bytes
8472 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't treat as a bitfield
8473 bitfield_bit_offset, // Offset in bits of a bitfield value if bitfield_bit_size != 0
8474 exe_scope);
8475 }
8476 break;
8477
8478 case clang::Type::Enum:
8479 // If our format is enum or default, show the enumeration value as
8480 // its enumeration string value, else just display it as requested.
8481 if ((format == eFormatEnum || format == eFormatDefault) && GetCompleteType(type))
8482 {
8483 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8484 const clang::EnumDecl *enum_decl = enutype->getDecl();
8485 assert(enum_decl);
8486 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8487 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
8488 lldb::offset_t offset = byte_offset;
8489 if (is_signed)
8490 {
8491 const int64_t enum_svalue = data.GetMaxS64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
8492 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8493 {
8494 if (enum_pos->getInitVal().getSExtValue() == enum_svalue)
8495 {
8496 s->PutCString (enum_pos->getNameAsString().c_str());
8497 return true;
8498 }
8499 }
8500 // If we have gotten here we didn't get find the enumerator in the
8501 // enum decl, so just print the integer.
8502 s->Printf("%" PRIi64, enum_svalue);
8503 }
8504 else
8505 {
8506 const uint64_t enum_uvalue = data.GetMaxU64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
8507 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8508 {
8509 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue)
8510 {
8511 s->PutCString (enum_pos->getNameAsString().c_str());
8512 return true;
8513 }
8514 }
8515 // If we have gotten here we didn't get find the enumerator in the
8516 // enum decl, so just print the integer.
8517 s->Printf("%" PRIu64, enum_uvalue);
8518 }
8519 return true;
8520 }
8521 // format was not enum, just fall through and dump the value as requested....
8522
8523 default:
8524 // We are down to a scalar type that we just need to display.
8525 {
8526 uint32_t item_count = 1;
8527 // A few formats, we might need to modify our size and count for depending
8528 // on how we are trying to display the value...
8529 switch (format)
8530 {
8531 default:
8532 case eFormatBoolean:
8533 case eFormatBinary:
8534 case eFormatComplex:
8535 case eFormatCString: // NULL terminated C strings
8536 case eFormatDecimal:
8537 case eFormatEnum:
8538 case eFormatHex:
8539 case eFormatHexUppercase:
8540 case eFormatFloat:
8541 case eFormatOctal:
8542 case eFormatOSType:
8543 case eFormatUnsigned:
8544 case eFormatPointer:
8545 case eFormatVectorOfChar:
8546 case eFormatVectorOfSInt8:
8547 case eFormatVectorOfUInt8:
8548 case eFormatVectorOfSInt16:
8549 case eFormatVectorOfUInt16:
8550 case eFormatVectorOfSInt32:
8551 case eFormatVectorOfUInt32:
8552 case eFormatVectorOfSInt64:
8553 case eFormatVectorOfUInt64:
8554 case eFormatVectorOfFloat32:
8555 case eFormatVectorOfFloat64:
8556 case eFormatVectorOfUInt128:
8557 break;
8558
8559 case eFormatChar:
8560 case eFormatCharPrintable:
8561 case eFormatCharArray:
8562 case eFormatBytes:
8563 case eFormatBytesWithASCII:
8564 item_count = byte_size;
8565 byte_size = 1;
8566 break;
8567
8568 case eFormatUnicode16:
8569 item_count = byte_size / 2;
8570 byte_size = 2;
8571 break;
8572
8573 case eFormatUnicode32:
8574 item_count = byte_size / 4;
8575 byte_size = 4;
8576 break;
8577 }
8578 return data.Dump (s,
8579 byte_offset,
8580 format,
8581 byte_size,
8582 item_count,
8583 UINT32_MAX,
8584 LLDB_INVALID_ADDRESS,
8585 bitfield_bit_size,
8586 bitfield_bit_offset,
8587 exe_scope);
8588 }
8589 break;
8590 }
8591 }
8592 return 0;
8593}
8594
8595
8596
8597void
8598ClangASTContext::DumpSummary (void* type, ExecutionContext *exe_ctx,
8599 Stream *s,
8600 const lldb_private::DataExtractor &data,
8601 lldb::offset_t data_byte_offset,
8602 size_t data_byte_size)
8603{
8604 uint32_t length = 0;
8605 if (IsCStringType (type, length))
8606 {
8607 if (exe_ctx)
8608 {
8609 Process *process = exe_ctx->GetProcessPtr();
8610 if (process)
8611 {
8612 lldb::offset_t offset = data_byte_offset;
8613 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
8614 std::vector<uint8_t> buf;
8615 if (length > 0)
8616 buf.resize (length);
8617 else
8618 buf.resize (256);
8619
8620 lldb_private::DataExtractor cstr_data(&buf.front(), buf.size(), process->GetByteOrder(), 4);
8621 buf.back() = '\0';
8622 size_t bytes_read;
8623 size_t total_cstr_len = 0;
8624 Error error;
8625 while ((bytes_read = process->ReadMemory (pointer_address, &buf.front(), buf.size(), error)) > 0)
8626 {
8627 const size_t len = strlen((const char *)&buf.front());
8628 if (len == 0)
8629 break;
8630 if (total_cstr_len == 0)
8631 s->PutCString (" \"");
8632 cstr_data.Dump(s, 0, lldb::eFormatChar, 1, len, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
8633 total_cstr_len += len;
8634 if (len < buf.size())
8635 break;
8636 pointer_address += total_cstr_len;
8637 }
8638 if (total_cstr_len > 0)
8639 s->PutChar ('"');
8640 }
8641 }
8642 }
8643}
8644
8645void
8646ClangASTContext::DumpTypeDescription (void* type)
8647{
8648 StreamFile s (stdout, false);
8649 DumpTypeDescription (&s);
8650 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), type);
8651 if (metadata)
8652 {
8653 metadata->Dump (&s);
8654 }
8655}
8656
8657void
8658ClangASTContext::DumpTypeDescription (void* type, Stream *s)
8659{
8660 if (type)
8661 {
8662 clang::QualType qual_type(GetQualType(type));
8663
8664 llvm::SmallVector<char, 1024> buf;
8665 llvm::raw_svector_ostream llvm_ostrm (buf);
8666
8667 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8668 switch (type_class)
8669 {
8670 case clang::Type::ObjCObject:
8671 case clang::Type::ObjCInterface:
8672 {
8673 GetCompleteType(type);
8674
8675 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8676 assert (objc_class_type);
8677 if (objc_class_type)
8678 {
8679 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
8680 if (class_interface_decl)
8681 {
8682 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
8683 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
8684 }
8685 }
8686 }
8687 break;
8688
8689 case clang::Type::Typedef:
8690 {
8691 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
8692 if (typedef_type)
8693 {
8694 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
8695 std::string clang_typedef_name (typedef_decl->getQualifiedNameAsString());
8696 if (!clang_typedef_name.empty())
8697 {
8698 s->PutCString ("typedef ");
8699 s->PutCString (clang_typedef_name.c_str());
8700 }
8701 }
8702 }
8703 break;
8704
8705 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00008706 CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00008707 return;
8708
8709 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00008710 CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00008711 return;
8712
8713 case clang::Type::Record:
8714 {
8715 GetCompleteType(type);
8716
8717 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8718 const clang::RecordDecl *record_decl = record_type->getDecl();
8719 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8720
8721 if (cxx_record_decl)
8722 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
8723 else
8724 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
8725 }
8726 break;
8727
8728 default:
8729 {
8730 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
8731 if (tag_type)
8732 {
8733 clang::TagDecl *tag_decl = tag_type->getDecl();
8734 if (tag_decl)
8735 tag_decl->print(llvm_ostrm, 0);
8736 }
8737 else
8738 {
8739 std::string clang_type_name(qual_type.getAsString());
8740 if (!clang_type_name.empty())
8741 s->PutCString (clang_type_name.c_str());
8742 }
8743 }
8744 }
8745
Greg Claytond8d4a572015-08-11 21:38:15 +00008746 if (buf.size() > 0)
8747 {
8748 s->Write (buf.data(), buf.size());
8749 }
8750 }
8751}
Greg Clayton8b4edba2015-08-14 20:02:05 +00008752
8753// DWARF parsing functions
8754#pragma mark DWARF Parsing
8755
8756#include "lldb/Core/Module.h"
8757#include "lldb/Symbol/CompileUnit.h"
8758#include "lldb/Symbol/Function.h"
8759#include "lldb/Symbol/ObjectFile.h"
8760#include "lldb/Symbol/TypeList.h"
8761
Oleksiy Vyalov52ae0232015-08-14 21:16:00 +00008762#include "Plugins/SymbolFile/DWARF/DWARFCompileUnit.h"
8763#include "Plugins/SymbolFile/DWARF/DWARFDebugInfo.h"
8764#include "Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.h"
8765#include "Plugins/SymbolFile/DWARF/DWARFDeclContext.h"
8766#include "Plugins/SymbolFile/DWARF/DWARFDefines.h"
8767#include "Plugins/SymbolFile/DWARF/DWARFDIECollection.h"
8768#include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
8769#include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h"
8770#include "Plugins/SymbolFile/DWARF/UniqueDWARFASTType.h"
Greg Clayton8b4edba2015-08-14 20:02:05 +00008771
8772
8773class ClangASTContext::DelayedAddObjCClassProperty
8774{
8775public:
8776 DelayedAddObjCClassProperty(const CompilerType &class_opaque_type,
8777 const char *property_name,
8778 const CompilerType &property_opaque_type, // The property type is only required if you don't have an ivar decl
8779 clang::ObjCIvarDecl *ivar_decl,
8780 const char *property_setter_name,
8781 const char *property_getter_name,
8782 uint32_t property_attributes,
8783 const ClangASTMetadata *metadata) :
8784 m_class_opaque_type (class_opaque_type),
8785 m_property_name (property_name),
8786 m_property_opaque_type (property_opaque_type),
8787 m_ivar_decl (ivar_decl),
8788 m_property_setter_name (property_setter_name),
8789 m_property_getter_name (property_getter_name),
8790 m_property_attributes (property_attributes)
8791 {
8792 if (metadata != NULL)
8793 {
8794 m_metadata_ap.reset(new ClangASTMetadata());
8795 *m_metadata_ap = *metadata;
8796 }
8797 }
8798
8799 DelayedAddObjCClassProperty (const DelayedAddObjCClassProperty &rhs)
8800 {
8801 *this = rhs;
8802 }
8803
8804 DelayedAddObjCClassProperty& operator= (const DelayedAddObjCClassProperty &rhs)
8805 {
8806 m_class_opaque_type = rhs.m_class_opaque_type;
8807 m_property_name = rhs.m_property_name;
8808 m_property_opaque_type = rhs.m_property_opaque_type;
8809 m_ivar_decl = rhs.m_ivar_decl;
8810 m_property_setter_name = rhs.m_property_setter_name;
8811 m_property_getter_name = rhs.m_property_getter_name;
8812 m_property_attributes = rhs.m_property_attributes;
8813
8814 if (rhs.m_metadata_ap.get())
8815 {
8816 m_metadata_ap.reset (new ClangASTMetadata());
8817 *m_metadata_ap = *rhs.m_metadata_ap;
8818 }
8819 return *this;
8820 }
8821
8822 bool
8823 Finalize()
8824 {
8825 ClangASTContext* ast = m_class_opaque_type.GetTypeSystem()->AsClangASTContext();
8826 assert(ast);
8827 return ast->AddObjCClassProperty (m_class_opaque_type,
8828 m_property_name,
8829 m_property_opaque_type,
8830 m_ivar_decl,
8831 m_property_setter_name,
8832 m_property_getter_name,
8833 m_property_attributes,
8834 m_metadata_ap.get());
8835 }
8836
8837private:
8838 CompilerType m_class_opaque_type;
8839 const char *m_property_name;
8840 CompilerType m_property_opaque_type;
8841 clang::ObjCIvarDecl *m_ivar_decl;
8842 const char *m_property_setter_name;
8843 const char *m_property_getter_name;
8844 uint32_t m_property_attributes;
8845 std::unique_ptr<ClangASTMetadata> m_metadata_ap;
8846};
8847
8848bool
8849ClangASTContext::ParseTemplateDIE (SymbolFileDWARF *dwarf,
8850 DWARFCompileUnit* dwarf_cu,
8851 const DWARFDebugInfoEntry *die,
8852 ClangASTContext::TemplateParameterInfos &template_param_infos)
8853{
8854 const dw_tag_t tag = die->Tag();
8855
8856 switch (tag)
8857 {
8858 case DW_TAG_template_type_parameter:
8859 case DW_TAG_template_value_parameter:
8860 {
8861 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
8862
8863 DWARFDebugInfoEntry::Attributes attributes;
8864 const size_t num_attributes = die->GetAttributes (dwarf,
8865 dwarf_cu,
8866 fixed_form_sizes,
8867 attributes);
8868 const char *name = NULL;
8869 Type *lldb_type = NULL;
8870 CompilerType clang_type;
8871 uint64_t uval64 = 0;
8872 bool uval64_valid = false;
8873 if (num_attributes > 0)
8874 {
8875 DWARFFormValue form_value;
8876 for (size_t i=0; i<num_attributes; ++i)
8877 {
8878 const dw_attr_t attr = attributes.AttributeAtIndex(i);
8879
8880 switch (attr)
8881 {
8882 case DW_AT_name:
8883 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
8884 name = form_value.AsCString(&dwarf->get_debug_str_data());
8885 break;
8886
8887 case DW_AT_type:
8888 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
8889 {
8890 const dw_offset_t type_die_offset = form_value.Reference();
8891 lldb_type = dwarf->ResolveTypeUID(type_die_offset);
8892 if (lldb_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00008893 clang_type = lldb_type->GetForwardCompilerType ();
Greg Clayton8b4edba2015-08-14 20:02:05 +00008894 }
8895 break;
8896
8897 case DW_AT_const_value:
8898 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
8899 {
8900 uval64_valid = true;
8901 uval64 = form_value.Unsigned();
8902 }
8903 break;
8904 default:
8905 break;
8906 }
8907 }
8908
8909 clang::ASTContext *ast = getASTContext();
8910 if (!clang_type)
8911 clang_type = GetBasicType(eBasicTypeVoid);
8912
8913 if (clang_type)
8914 {
8915 bool is_signed = false;
8916 if (name && name[0])
8917 template_param_infos.names.push_back(name);
8918 else
8919 template_param_infos.names.push_back(NULL);
8920
8921 if (tag == DW_TAG_template_value_parameter &&
8922 lldb_type != NULL &&
8923 clang_type.IsIntegerType (is_signed) &&
8924 uval64_valid)
8925 {
8926 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
8927 template_param_infos.args.push_back (clang::TemplateArgument (*ast,
8928 llvm::APSInt(apint),
8929 ClangASTContext::GetQualType(clang_type)));
8930 }
8931 else
8932 {
8933 template_param_infos.args.push_back (clang::TemplateArgument (ClangASTContext::GetQualType(clang_type)));
8934 }
8935 }
8936 else
8937 {
8938 return false;
8939 }
8940
8941 }
8942 }
8943 return true;
8944
8945 default:
8946 break;
8947 }
8948 return false;
8949}
8950
8951bool
8952ClangASTContext::ParseTemplateParameterInfos (SymbolFileDWARF *dwarf,
8953 DWARFCompileUnit* dwarf_cu,
8954 const DWARFDebugInfoEntry *parent_die,
8955 ClangASTContext::TemplateParameterInfos &template_param_infos)
8956{
8957
8958 if (parent_die == NULL)
8959 return false;
8960
8961 Args template_parameter_names;
8962 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild();
8963 die != NULL;
8964 die = die->GetSibling())
8965 {
8966 const dw_tag_t tag = die->Tag();
8967
8968 switch (tag)
8969 {
8970 case DW_TAG_template_type_parameter:
8971 case DW_TAG_template_value_parameter:
8972 ParseTemplateDIE (dwarf, dwarf_cu, die, template_param_infos);
8973 break;
8974
8975 default:
8976 break;
8977 }
8978 }
8979 if (template_param_infos.args.empty())
8980 return false;
8981 return template_param_infos.args.size() == template_param_infos.names.size();
8982}
8983
8984clang::ClassTemplateDecl *
8985ClangASTContext::ParseClassTemplateDecl (SymbolFileDWARF *dwarf,
8986 clang::DeclContext *decl_ctx,
8987 lldb::AccessType access_type,
8988 const char *parent_name,
8989 int tag_decl_kind,
8990 const ClangASTContext::TemplateParameterInfos &template_param_infos)
8991{
8992 if (template_param_infos.IsValid())
8993 {
8994 std::string template_basename(parent_name);
8995 template_basename.erase (template_basename.find('<'));
8996
8997 return CreateClassTemplateDecl (decl_ctx,
8998 access_type,
8999 template_basename.c_str(),
9000 tag_decl_kind,
9001 template_param_infos);
9002 }
9003 return NULL;
9004}
9005
9006bool
Greg Clayton6dc8d582015-08-18 22:32:36 +00009007ClangASTContext::CompleteTypeFromDWARF (SymbolFileDWARF *dwarf,
9008 DWARFCompileUnit *dwarf_cu,
9009 const DWARFDebugInfoEntry* die,
9010 lldb_private::Type *type,
9011 CompilerType &clang_type)
Greg Clayton8b4edba2015-08-14 20:02:05 +00009012{
9013 // Disable external storage for this type so we don't get anymore
9014 // clang::ExternalASTSource queries for this type.
9015 SetHasExternalStorage (clang_type.GetOpaqueQualType(), false);
9016
9017 if (dwarf == nullptr || dwarf_cu == nullptr || die == nullptr)
9018 return false;
9019
9020 const dw_tag_t tag = die->Tag();
9021
9022 Log *log = nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
9023 if (log)
9024 dwarf->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log,
9025 "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
9026 dwarf->MakeUserID(die->GetOffset()),
9027 DW_TAG_value_to_name(tag),
9028 type->GetName().AsCString());
9029 assert (clang_type);
9030 DWARFDebugInfoEntry::Attributes attributes;
9031
9032 switch (tag)
9033 {
9034 case DW_TAG_structure_type:
9035 case DW_TAG_union_type:
9036 case DW_TAG_class_type:
9037 {
9038 LayoutInfo layout_info;
9039
9040 {
9041 if (die->HasChildren())
9042 {
9043 LanguageType class_language = eLanguageTypeUnknown;
9044 if (ClangASTContext::IsObjCObjectOrInterfaceType(clang_type))
9045 {
9046 class_language = eLanguageTypeObjC;
9047 // For objective C we don't start the definition when
9048 // the class is created.
9049 ClangASTContext::StartTagDeclarationDefinition (clang_type);
9050 }
9051
9052 int tag_decl_kind = -1;
9053 AccessType default_accessibility = eAccessNone;
9054 if (tag == DW_TAG_structure_type)
9055 {
9056 tag_decl_kind = clang::TTK_Struct;
9057 default_accessibility = eAccessPublic;
9058 }
9059 else if (tag == DW_TAG_union_type)
9060 {
9061 tag_decl_kind = clang::TTK_Union;
9062 default_accessibility = eAccessPublic;
9063 }
9064 else if (tag == DW_TAG_class_type)
9065 {
9066 tag_decl_kind = clang::TTK_Class;
9067 default_accessibility = eAccessPrivate;
9068 }
9069
9070 SymbolContext sc(dwarf->GetCompUnitForDWARFCompUnit(dwarf_cu));
9071 std::vector<clang::CXXBaseSpecifier *> base_classes;
9072 std::vector<int> member_accessibilities;
9073 bool is_a_class = false;
9074 // Parse members and base classes first
9075 DWARFDIECollection member_function_dies;
9076
9077 DelayedPropertyList delayed_properties;
9078 ParseChildMembers (sc,
9079 dwarf,
9080 dwarf_cu,
9081 die,
9082 clang_type,
9083 class_language,
9084 base_classes,
9085 member_accessibilities,
9086 member_function_dies,
9087 delayed_properties,
9088 default_accessibility,
9089 is_a_class,
9090 layout_info);
9091
9092 // Now parse any methods if there were any...
9093 size_t num_functions = member_function_dies.Size();
9094 if (num_functions > 0)
9095 {
9096 for (size_t i=0; i<num_functions; ++i)
9097 {
9098 dwarf->ResolveType(dwarf_cu, member_function_dies.GetDIEPtrAtIndex(i));
9099 }
9100 }
9101
9102 if (class_language == eLanguageTypeObjC)
9103 {
9104 ConstString class_name (clang_type.GetTypeName());
9105 if (class_name)
9106 {
9107 DIEArray method_die_offsets;
9108 dwarf->GetObjCMethodDIEOffsets(class_name, method_die_offsets);
9109
9110 if (!method_die_offsets.empty())
9111 {
9112 DWARFDebugInfo* debug_info = dwarf->DebugInfo();
9113
9114 DWARFCompileUnit* method_cu = NULL;
9115 const size_t num_matches = method_die_offsets.size();
9116 for (size_t i=0; i<num_matches; ++i)
9117 {
9118 const dw_offset_t die_offset = method_die_offsets[i];
9119 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
9120
9121 if (method_die)
9122 dwarf->ResolveType (method_cu, method_die);
9123 }
9124 }
9125
9126 for (DelayedPropertyList::iterator pi = delayed_properties.begin(), pe = delayed_properties.end();
9127 pi != pe;
9128 ++pi)
9129 pi->Finalize();
9130 }
9131 }
9132
9133 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
9134 // need to tell the clang type it is actually a class.
9135 if (class_language != eLanguageTypeObjC)
9136 {
9137 if (is_a_class && tag_decl_kind != clang::TTK_Class)
9138 SetTagTypeKind (ClangASTContext::GetQualType(clang_type), clang::TTK_Class);
9139 }
9140
9141 // Since DW_TAG_structure_type gets used for both classes
9142 // and structures, we may need to set any DW_TAG_member
9143 // fields to have a "private" access if none was specified.
9144 // When we parsed the child members we tracked that actual
9145 // accessibility value for each DW_TAG_member in the
9146 // "member_accessibilities" array. If the value for the
9147 // member is zero, then it was set to the "default_accessibility"
9148 // which for structs was "public". Below we correct this
9149 // by setting any fields to "private" that weren't correctly
9150 // set.
9151 if (is_a_class && !member_accessibilities.empty())
9152 {
9153 // This is a class and all members that didn't have
9154 // their access specified are private.
9155 SetDefaultAccessForRecordFields (GetAsRecordDecl(clang_type),
9156 eAccessPrivate,
9157 &member_accessibilities.front(),
9158 member_accessibilities.size());
9159 }
9160
9161 if (!base_classes.empty())
9162 {
9163 // Make sure all base classes refer to complete types and not
9164 // forward declarations. If we don't do this, clang will crash
9165 // with an assertion in the call to clang_type.SetBaseClassesForClassType()
9166 bool base_class_error = false;
9167 for (auto &base_class : base_classes)
9168 {
9169 clang::TypeSourceInfo *type_source_info = base_class->getTypeSourceInfo();
9170 if (type_source_info)
9171 {
9172 CompilerType base_class_type (this, type_source_info->getType().getAsOpaquePtr());
9173 if (base_class_type.GetCompleteType() == false)
9174 {
9175 if (!base_class_error)
9176 {
9177 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",
9178 die->GetOffset(),
9179 die->GetName(dwarf, dwarf_cu),
9180 base_class_type.GetTypeName().GetCString(),
9181 sc.comp_unit ? sc.comp_unit->GetPath().c_str() : "the source file");
9182 }
9183 // We have no choice other than to pretend that the base class
9184 // is complete. If we don't do this, clang will crash when we
9185 // call setBases() inside of "clang_type.SetBaseClassesForClassType()"
9186 // below. Since we provide layout assistance, all ivars in this
9187 // class and other classes will be fine, this is the best we can do
9188 // short of crashing.
9189
9190 ClangASTContext::StartTagDeclarationDefinition (base_class_type);
9191 ClangASTContext::CompleteTagDeclarationDefinition (base_class_type);
9192 }
9193 }
9194 }
9195 SetBaseClassesForClassType (clang_type.GetOpaqueQualType(),
9196 &base_classes.front(),
9197 base_classes.size());
9198
9199 // Clang will copy each CXXBaseSpecifier in "base_classes"
9200 // so we have to free them all.
9201 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
9202 base_classes.size());
9203 }
9204 }
9205 }
9206
9207 ClangASTContext::BuildIndirectFields (clang_type);
9208 ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
9209
9210 if (!layout_info.field_offsets.empty() ||
9211 !layout_info.base_offsets.empty() ||
9212 !layout_info.vbase_offsets.empty() )
9213 {
9214 if (type)
9215 layout_info.bit_size = type->GetByteSize() * 8;
9216 if (layout_info.bit_size == 0)
9217 layout_info.bit_size = die->GetAttributeValueAsUnsigned(dwarf, dwarf_cu, DW_AT_byte_size, 0) * 8;
9218
9219 clang::CXXRecordDecl *record_decl = GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
9220 if (record_decl)
9221 {
9222 if (log)
9223 {
9224 ModuleSP module_sp = dwarf->GetObjectFile()->GetModule();
9225
9226 if (module_sp)
9227 {
9228 module_sp->LogMessage (log,
Greg Clayton6dc8d582015-08-18 22:32:36 +00009229 "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 +00009230 static_cast<void*>(clang_type.GetOpaqueQualType()),
9231 static_cast<void*>(record_decl),
9232 layout_info.bit_size,
9233 layout_info.alignment,
9234 static_cast<uint32_t>(layout_info.field_offsets.size()),
9235 static_cast<uint32_t>(layout_info.base_offsets.size()),
9236 static_cast<uint32_t>(layout_info.vbase_offsets.size()));
9237
9238 uint32_t idx;
9239 {
9240 llvm::DenseMap<const clang::FieldDecl *, uint64_t>::const_iterator pos,
9241 end = layout_info.field_offsets.end();
9242 for (idx = 0, pos = layout_info.field_offsets.begin(); pos != end; ++pos, ++idx)
9243 {
9244 module_sp->LogMessage(log,
Greg Clayton6dc8d582015-08-18 22:32:36 +00009245 "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) field[%u] = { bit_offset=%u, name='%s' }",
Greg Clayton8b4edba2015-08-14 20:02:05 +00009246 static_cast<void *>(clang_type.GetOpaqueQualType()),
9247 idx,
9248 static_cast<uint32_t>(pos->second),
9249 pos->first->getNameAsString().c_str());
9250 }
9251 }
9252
9253 {
9254 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator base_pos,
9255 base_end = layout_info.base_offsets.end();
9256 for (idx = 0, base_pos = layout_info.base_offsets.begin(); base_pos != base_end; ++base_pos, ++idx)
9257 {
9258 module_sp->LogMessage(log,
Greg Clayton6dc8d582015-08-18 22:32:36 +00009259 "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) base[%u] = { byte_offset=%u, name='%s' }",
Greg Clayton8b4edba2015-08-14 20:02:05 +00009260 clang_type.GetOpaqueQualType(), idx, (uint32_t)base_pos->second.getQuantity(),
9261 base_pos->first->getNameAsString().c_str());
9262 }
9263 }
9264 {
9265 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator vbase_pos,
9266 vbase_end = layout_info.vbase_offsets.end();
9267 for (idx = 0, vbase_pos = layout_info.vbase_offsets.begin(); vbase_pos != vbase_end; ++vbase_pos, ++idx)
9268 {
9269 module_sp->LogMessage(log,
Greg Clayton6dc8d582015-08-18 22:32:36 +00009270 "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) vbase[%u] = { byte_offset=%u, name='%s' }",
Greg Clayton8b4edba2015-08-14 20:02:05 +00009271 static_cast<void *>(clang_type.GetOpaqueQualType()), idx,
9272 static_cast<uint32_t>(vbase_pos->second.getQuantity()),
9273 vbase_pos->first->getNameAsString().c_str());
9274 }
9275 }
9276
9277 }
9278 }
9279 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
9280 }
9281 }
9282 }
9283
9284 return (bool)clang_type;
9285
9286 case DW_TAG_enumeration_type:
9287 ClangASTContext::StartTagDeclarationDefinition (clang_type);
9288 if (die->HasChildren())
9289 {
9290 SymbolContext sc(dwarf->GetCompUnitForDWARFCompUnit(dwarf_cu));
9291 bool is_signed = false;
9292 clang_type.IsIntegerType(is_signed);
9293 ParseChildEnumerators(sc, clang_type, is_signed, type->GetByteSize(), dwarf, dwarf_cu, die);
9294 }
9295 ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
9296 return (bool)clang_type;
9297
9298 default:
9299 assert(false && "not a forward clang type decl!");
9300 break;
9301 }
9302
9303 return false;
9304}
9305
Greg Clayton99558cc42015-08-24 23:46:31 +00009306CompilerDeclContext
9307ClangASTContext::GetDeclContextForUIDFromDWARF (SymbolFileDWARF *dwarf,
9308 DWARFCompileUnit *cu,
9309 const DWARFDebugInfoEntry* die)
9310{
9311 clang::DeclContext *clang_decl_ctx = GetClangDeclContextForDIE (dwarf, cu, die);
9312 if (clang_decl_ctx)
9313 return CompilerDeclContext(this, clang_decl_ctx);
9314 return CompilerDeclContext();
9315}
9316
9317CompilerDeclContext
9318ClangASTContext::GetDeclContextContainingUIDFromDWARF (SymbolFileDWARF *dwarf,
9319 DWARFCompileUnit *cu,
9320 const DWARFDebugInfoEntry* die)
9321{
9322 clang::DeclContext *clang_decl_ctx = GetClangDeclContextContainingDIE (dwarf, cu, die, nullptr);
9323 if (clang_decl_ctx)
9324 return CompilerDeclContext(this, clang_decl_ctx);
9325 return CompilerDeclContext();
9326}
9327
Greg Clayton6dc8d582015-08-18 22:32:36 +00009328void
9329ClangASTContext::CompleteTagDecl (void *baton, clang::TagDecl *decl)
9330{
9331 ClangASTContext *ast = (ClangASTContext *)baton;
9332 SymbolFile *sym_file = ast->GetSymbolFile();
9333 if (sym_file)
9334 {
9335 CompilerType clang_type = GetTypeForDecl (decl);
9336 if (clang_type)
9337 sym_file->CompleteType (clang_type);
9338 }
9339}
9340
9341void
9342ClangASTContext::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
9343{
9344 ClangASTContext *ast = (ClangASTContext *)baton;
9345 SymbolFile *sym_file = ast->GetSymbolFile();
9346 if (sym_file)
9347 {
9348 CompilerType clang_type = GetTypeForDecl (decl);
9349 if (clang_type)
9350 sym_file->CompleteType (clang_type);
9351 }
9352}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009353
9354bool
Greg Clayton6dc8d582015-08-18 22:32:36 +00009355ClangASTContext::LayoutRecordType(void *baton,
Greg Clayton8b4edba2015-08-14 20:02:05 +00009356 const clang::RecordDecl *record_decl,
9357 uint64_t &bit_size,
9358 uint64_t &alignment,
9359 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9360 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
9361 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
9362{
Greg Clayton6dc8d582015-08-18 22:32:36 +00009363 ClangASTContext *ast = (ClangASTContext *)baton;
9364 RecordDeclToLayoutMap::iterator pos = ast->m_record_decl_to_layout_map.find (record_decl);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009365 bool success = false;
9366 base_offsets.clear();
9367 vbase_offsets.clear();
Greg Clayton6dc8d582015-08-18 22:32:36 +00009368 if (pos != ast->m_record_decl_to_layout_map.end())
Greg Clayton8b4edba2015-08-14 20:02:05 +00009369 {
9370 bit_size = pos->second.bit_size;
9371 alignment = pos->second.alignment;
9372 field_offsets.swap(pos->second.field_offsets);
9373 base_offsets.swap (pos->second.base_offsets);
9374 vbase_offsets.swap (pos->second.vbase_offsets);
Greg Clayton6dc8d582015-08-18 22:32:36 +00009375 ast->m_record_decl_to_layout_map.erase(pos);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009376 success = true;
9377 }
9378 else
9379 {
9380 bit_size = 0;
9381 alignment = 0;
9382 field_offsets.clear();
9383 }
9384 return success;
9385}
9386
9387
9388size_t
9389ClangASTContext::ParseChildEnumerators (const SymbolContext& sc,
9390 lldb_private::CompilerType &clang_type,
9391 bool is_signed,
9392 uint32_t enumerator_byte_size,
9393 SymbolFileDWARF *dwarf,
9394 DWARFCompileUnit* dwarf_cu,
9395 const DWARFDebugInfoEntry *parent_die)
9396{
9397 if (parent_die == NULL)
9398 return 0;
9399
9400 size_t enumerators_added = 0;
9401 const DWARFDebugInfoEntry *die;
9402 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
9403
9404 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
9405 {
9406 const dw_tag_t tag = die->Tag();
9407 if (tag == DW_TAG_enumerator)
9408 {
9409 DWARFDebugInfoEntry::Attributes attributes;
9410 const size_t num_child_attributes = die->GetAttributes(dwarf, dwarf_cu, fixed_form_sizes, attributes);
9411 if (num_child_attributes > 0)
9412 {
9413 const char *name = NULL;
9414 bool got_value = false;
9415 int64_t enum_value = 0;
9416 Declaration decl;
9417
9418 uint32_t i;
9419 for (i=0; i<num_child_attributes; ++i)
9420 {
9421 const dw_attr_t attr = attributes.AttributeAtIndex(i);
9422 DWARFFormValue form_value;
9423 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
9424 {
9425 switch (attr)
9426 {
9427 case DW_AT_const_value:
9428 got_value = true;
9429 if (is_signed)
9430 enum_value = form_value.Signed();
9431 else
9432 enum_value = form_value.Unsigned();
9433 break;
9434
9435 case DW_AT_name:
9436 name = form_value.AsCString(&dwarf->get_debug_str_data());
9437 break;
9438
9439 case DW_AT_description:
9440 default:
9441 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
9442 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
9443 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
9444 case DW_AT_sibling:
9445 break;
9446 }
9447 }
9448 }
9449
9450 if (name && name[0] && got_value)
9451 {
9452 AddEnumerationValueToEnumerationType (clang_type.GetOpaqueQualType(),
9453 GetEnumerationIntegerType(clang_type.GetOpaqueQualType()),
9454 decl,
9455 name,
9456 enum_value,
9457 enumerator_byte_size * 8);
9458 ++enumerators_added;
9459 }
9460 }
9461 }
9462 }
9463 return enumerators_added;
9464}
9465
9466#if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
9467
9468class DIEStack
9469{
9470public:
9471
9472 void Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
9473 {
9474 m_dies.push_back (DIEInfo(cu, die));
9475 }
9476
9477
9478 void LogDIEs (Log *log, SymbolFileDWARF *dwarf)
9479 {
9480 StreamString log_strm;
9481 const size_t n = m_dies.size();
9482 log_strm.Printf("DIEStack[%" PRIu64 "]:\n", (uint64_t)n);
9483 for (size_t i=0; i<n; i++)
9484 {
9485 DWARFCompileUnit *cu = m_dies[i].cu;
9486 const DWARFDebugInfoEntry *die = m_dies[i].die;
9487 std::string qualified_name;
9488 die->GetQualifiedName(dwarf, cu, qualified_name);
9489 log_strm.Printf ("[%" PRIu64 "] 0x%8.8x: %s name='%s'\n",
9490 (uint64_t)i,
9491 die->GetOffset(),
9492 DW_TAG_value_to_name(die->Tag()),
9493 qualified_name.c_str());
9494 }
9495 log->PutCString(log_strm.GetData());
9496 }
9497 void Pop ()
9498 {
9499 m_dies.pop_back();
9500 }
9501
9502 class ScopedPopper
9503 {
9504 public:
9505 ScopedPopper (DIEStack &die_stack) :
9506 m_die_stack (die_stack),
9507 m_valid (false)
9508 {
9509 }
9510
9511 void
9512 Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
9513 {
9514 m_valid = true;
9515 m_die_stack.Push (cu, die);
9516 }
9517
9518 ~ScopedPopper ()
9519 {
9520 if (m_valid)
9521 m_die_stack.Pop();
9522 }
9523
9524
9525
9526 protected:
9527 DIEStack &m_die_stack;
9528 bool m_valid;
9529 };
9530
9531protected:
9532 struct DIEInfo {
9533 DIEInfo (DWARFCompileUnit *c, const DWARFDebugInfoEntry *d) :
9534 cu(c),
9535 die(d)
9536 {
9537 }
9538 DWARFCompileUnit *cu;
9539 const DWARFDebugInfoEntry *die;
9540 };
9541 typedef std::vector<DIEInfo> Stack;
9542 Stack m_dies;
9543};
9544#endif
9545
9546
9547
9548static AccessType
9549DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
9550{
9551 switch (dwarf_accessibility)
9552 {
9553 case DW_ACCESS_public: return eAccessPublic;
9554 case DW_ACCESS_private: return eAccessPrivate;
9555 case DW_ACCESS_protected: return eAccessProtected;
9556 default: break;
9557 }
9558 return eAccessNone;
9559}
9560
9561static bool
9562DeclKindIsCXXClass (clang::Decl::Kind decl_kind)
9563{
9564 switch (decl_kind)
9565 {
9566 case clang::Decl::CXXRecord:
9567 case clang::Decl::ClassTemplateSpecialization:
9568 return true;
9569 default:
9570 break;
9571 }
9572 return false;
9573}
9574
9575struct BitfieldInfo
9576{
9577 uint64_t bit_size;
9578 uint64_t bit_offset;
9579
9580 BitfieldInfo () :
9581 bit_size (LLDB_INVALID_ADDRESS),
9582 bit_offset (LLDB_INVALID_ADDRESS)
9583 {
9584 }
9585
9586 void
9587 Clear()
9588 {
9589 bit_size = LLDB_INVALID_ADDRESS;
9590 bit_offset = LLDB_INVALID_ADDRESS;
9591 }
9592
9593 bool IsValid ()
9594 {
9595 return (bit_size != LLDB_INVALID_ADDRESS) &&
9596 (bit_offset != LLDB_INVALID_ADDRESS);
9597 }
9598};
9599
9600Function *
9601ClangASTContext::ParseFunctionFromDWARF (const SymbolContext& sc,
9602 SymbolFileDWARF *dwarf,
9603 DWARFCompileUnit* dwarf_cu,
9604 const DWARFDebugInfoEntry *die)
9605{
9606 DWARFDebugRanges::RangeList func_ranges;
9607 const char *name = NULL;
9608 const char *mangled = NULL;
9609 int decl_file = 0;
9610 int decl_line = 0;
9611 int decl_column = 0;
9612 int call_file = 0;
9613 int call_line = 0;
9614 int call_column = 0;
9615 DWARFExpression frame_base;
9616
9617 assert (die->Tag() == DW_TAG_subprogram);
9618
9619 if (die->Tag() != DW_TAG_subprogram)
9620 return NULL;
9621
9622 if (die->GetDIENamesAndRanges (dwarf,
9623 dwarf_cu,
9624 name,
9625 mangled,
9626 func_ranges,
9627 decl_file,
9628 decl_line,
9629 decl_column,
9630 call_file,
9631 call_line,
9632 call_column,
9633 &frame_base))
9634 {
9635 // Union of all ranges in the function DIE (if the function is discontiguous)
9636 AddressRange func_range;
9637 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0);
9638 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0);
9639 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
9640 {
9641 ModuleSP module_sp (dwarf->GetObjectFile()->GetModule());
9642 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, module_sp->GetSectionList());
9643 if (func_range.GetBaseAddress().IsValid())
9644 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
9645 }
9646
9647 if (func_range.GetBaseAddress().IsValid())
9648 {
9649 Mangled func_name;
9650 if (mangled)
9651 func_name.SetValue(ConstString(mangled), true);
9652 else if (die->GetParent()->Tag() == DW_TAG_compile_unit &&
9653 LanguageRuntime::LanguageIsCPlusPlus(dwarf_cu->GetLanguageType()) &&
9654 name && strcmp(name, "main") != 0)
9655 {
9656 // If the mangled name is not present in the DWARF, generate the demangled name
9657 // using the decl context. We skip if the function is "main" as its name is
9658 // never mangled.
9659 bool is_static = false;
9660 bool is_variadic = false;
9661 unsigned type_quals = 0;
9662 std::vector<CompilerType> param_types;
9663 std::vector<clang::ParmVarDecl*> param_decls;
9664 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
9665 DWARFDeclContext decl_ctx;
9666 StreamString sstr;
9667
9668 die->GetDWARFDeclContext(dwarf, dwarf_cu, decl_ctx);
9669 sstr << decl_ctx.GetQualifiedName();
9670
9671 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE(dwarf,
9672 dwarf_cu,
9673 die,
9674 &decl_ctx_die);
9675 ParseChildParameters(sc,
9676 containing_decl_ctx,
9677 dwarf,
9678 dwarf_cu,
9679 die,
9680 true,
9681 is_static,
9682 is_variadic,
9683 param_types,
9684 param_decls,
9685 type_quals);
9686 sstr << "(";
9687 for (size_t i = 0; i < param_types.size(); i++)
9688 {
9689 if (i > 0)
9690 sstr << ", ";
9691 sstr << param_types[i].GetTypeName();
9692 }
9693 if (is_variadic)
9694 sstr << ", ...";
9695 sstr << ")";
9696 if (type_quals & clang::Qualifiers::Const)
9697 sstr << " const";
9698
9699 func_name.SetValue(ConstString(sstr.GetData()), false);
9700 }
9701 else
9702 func_name.SetValue(ConstString(name), false);
9703
9704 FunctionSP func_sp;
9705 std::unique_ptr<Declaration> decl_ap;
9706 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
9707 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
9708 decl_line,
9709 decl_column));
9710
9711 // Supply the type _only_ if it has already been parsed
9712 Type *func_type = dwarf->m_die_to_type.lookup (die);
9713
9714 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
9715
9716 if (dwarf->FixupAddress (func_range.GetBaseAddress()))
9717 {
9718 const user_id_t func_user_id = dwarf->MakeUserID(die->GetOffset());
9719 func_sp.reset(new Function (sc.comp_unit,
9720 dwarf->MakeUserID(func_user_id), // UserID is the DIE offset
9721 dwarf->MakeUserID(func_user_id),
9722 func_name,
9723 func_type,
9724 func_range)); // first address range
9725
9726 if (func_sp.get() != NULL)
9727 {
9728 if (frame_base.IsValid())
9729 func_sp->GetFrameBaseExpression() = frame_base;
9730 sc.comp_unit->AddFunction(func_sp);
9731 return func_sp.get();
9732 }
9733 }
9734 }
9735 }
9736 return NULL;
9737}
9738
9739
9740size_t
9741ClangASTContext::ParseChildMembers (const SymbolContext& sc,
9742 SymbolFileDWARF *dwarf,
9743 DWARFCompileUnit* dwarf_cu,
9744 const DWARFDebugInfoEntry *parent_die,
9745 CompilerType &class_clang_type,
9746 const LanguageType class_language,
9747 std::vector<clang::CXXBaseSpecifier *>& base_classes,
9748 std::vector<int>& member_accessibilities,
9749 DWARFDIECollection& member_function_dies,
9750 DelayedPropertyList& delayed_properties,
9751 AccessType& default_accessibility,
9752 bool &is_a_class,
9753 LayoutInfo &layout_info)
9754{
9755 if (parent_die == NULL)
9756 return 0;
9757
9758 size_t count = 0;
9759 const DWARFDebugInfoEntry *die;
9760 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
9761 uint32_t member_idx = 0;
9762 BitfieldInfo last_field_info;
9763 ModuleSP module_sp = dwarf->GetObjectFile()->GetModule();
9764 ClangASTContext* ast = class_clang_type.GetTypeSystem()->AsClangASTContext();
9765 if (ast == nullptr)
9766 return 0;
9767
9768 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
9769 {
9770 dw_tag_t tag = die->Tag();
9771
9772 switch (tag)
9773 {
9774 case DW_TAG_member:
9775 case DW_TAG_APPLE_property:
9776 {
9777 DWARFDebugInfoEntry::Attributes attributes;
9778 const size_t num_attributes = die->GetAttributes (dwarf,
9779 dwarf_cu,
9780 fixed_form_sizes,
9781 attributes);
9782 if (num_attributes > 0)
9783 {
9784 Declaration decl;
9785 //DWARFExpression location;
9786 const char *name = NULL;
9787 const char *prop_name = NULL;
9788 const char *prop_getter_name = NULL;
9789 const char *prop_setter_name = NULL;
9790 uint32_t prop_attributes = 0;
9791
9792
9793 bool is_artificial = false;
9794 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
9795 AccessType accessibility = eAccessNone;
9796 uint32_t member_byte_offset = UINT32_MAX;
9797 size_t byte_size = 0;
9798 size_t bit_offset = 0;
9799 size_t bit_size = 0;
9800 bool is_external = false; // On DW_TAG_members, this means the member is static
9801 uint32_t i;
9802 for (i=0; i<num_attributes && !is_artificial; ++i)
9803 {
9804 const dw_attr_t attr = attributes.AttributeAtIndex(i);
9805 DWARFFormValue form_value;
9806 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
9807 {
9808 switch (attr)
9809 {
9810 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
9811 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
9812 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
9813 case DW_AT_name: name = form_value.AsCString(&dwarf->get_debug_str_data()); break;
9814 case DW_AT_type: encoding_uid = form_value.Reference(); break;
9815 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
9816 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
9817 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
9818 case DW_AT_data_member_location:
9819 if (form_value.BlockData())
9820 {
9821 Value initialValue(0);
9822 Value memberOffset(0);
9823 const DWARFDataExtractor& debug_info_data = dwarf->get_debug_info_data();
9824 uint32_t block_length = form_value.Unsigned();
9825 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
9826 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
9827 NULL, // ClangExpressionVariableList *
9828 NULL, // ClangExpressionDeclMap *
9829 NULL, // RegisterContext *
9830 module_sp,
9831 debug_info_data,
9832 block_offset,
9833 block_length,
9834 eRegisterKindDWARF,
9835 &initialValue,
9836 memberOffset,
9837 NULL))
9838 {
9839 member_byte_offset = memberOffset.ResolveValue(NULL).UInt();
9840 }
9841 }
9842 else
9843 {
9844 // With DWARF 3 and later, if the value is an integer constant,
9845 // this form value is the offset in bytes from the beginning
9846 // of the containing entity.
9847 member_byte_offset = form_value.Unsigned();
9848 }
9849 break;
9850
9851 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
9852 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
9853 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&dwarf->get_debug_str_data()); break;
9854 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&dwarf->get_debug_str_data()); break;
9855 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&dwarf->get_debug_str_data()); break;
9856 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
9857 case DW_AT_external: is_external = form_value.Boolean(); break;
9858
9859 default:
9860 case DW_AT_declaration:
9861 case DW_AT_description:
9862 case DW_AT_mutable:
9863 case DW_AT_visibility:
9864 case DW_AT_sibling:
9865 break;
9866 }
9867 }
9868 }
9869
9870 if (prop_name)
9871 {
9872 ConstString fixed_getter;
9873 ConstString fixed_setter;
9874
9875 // Check if the property getter/setter were provided as full
9876 // names. We want basenames, so we extract them.
9877
9878 if (prop_getter_name && prop_getter_name[0] == '-')
9879 {
9880 ObjCLanguageRuntime::MethodName prop_getter_method(prop_getter_name, true);
9881 prop_getter_name = prop_getter_method.GetSelector().GetCString();
9882 }
9883
9884 if (prop_setter_name && prop_setter_name[0] == '-')
9885 {
9886 ObjCLanguageRuntime::MethodName prop_setter_method(prop_setter_name, true);
9887 prop_setter_name = prop_setter_method.GetSelector().GetCString();
9888 }
9889
9890 // If the names haven't been provided, they need to be
9891 // filled in.
9892
9893 if (!prop_getter_name)
9894 {
9895 prop_getter_name = prop_name;
9896 }
9897 if (!prop_setter_name && prop_name[0] && !(prop_attributes & DW_APPLE_PROPERTY_readonly))
9898 {
9899 StreamString ss;
9900
9901 ss.Printf("set%c%s:",
9902 toupper(prop_name[0]),
9903 &prop_name[1]);
9904
9905 fixed_setter.SetCString(ss.GetData());
9906 prop_setter_name = fixed_setter.GetCString();
9907 }
9908 }
9909
9910 // Clang has a DWARF generation bug where sometimes it
9911 // represents fields that are references with bad byte size
9912 // and bit size/offset information such as:
9913 //
9914 // DW_AT_byte_size( 0x00 )
9915 // DW_AT_bit_size( 0x40 )
9916 // DW_AT_bit_offset( 0xffffffffffffffc0 )
9917 //
9918 // So check the bit offset to make sure it is sane, and if
9919 // the values are not sane, remove them. If we don't do this
9920 // then we will end up with a crash if we try to use this
9921 // type in an expression when clang becomes unhappy with its
9922 // recycled debug info.
9923
9924 if (bit_offset > 128)
9925 {
9926 bit_size = 0;
9927 bit_offset = 0;
9928 }
9929
9930 // FIXME: Make Clang ignore Objective-C accessibility for expressions
9931 if (class_language == eLanguageTypeObjC ||
9932 class_language == eLanguageTypeObjC_plus_plus)
9933 accessibility = eAccessNone;
9934
9935 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
9936 {
9937 // Not all compilers will mark the vtable pointer
9938 // member as artificial (llvm-gcc). We can't have
9939 // the virtual members in our classes otherwise it
9940 // throws off all child offsets since we end up
9941 // having and extra pointer sized member in our
9942 // class layouts.
9943 is_artificial = true;
9944 }
9945
9946 // Handle static members
9947 if (is_external && member_byte_offset == UINT32_MAX)
9948 {
9949 Type *var_type = dwarf->ResolveTypeUID(encoding_uid);
9950
9951 if (var_type)
9952 {
9953 if (accessibility == eAccessNone)
9954 accessibility = eAccessPublic;
9955 ClangASTContext::AddVariableToRecordType (class_clang_type,
9956 name,
Greg Clayton99558cc42015-08-24 23:46:31 +00009957 var_type->GetLayoutCompilerType (),
Greg Clayton8b4edba2015-08-14 20:02:05 +00009958 accessibility);
9959 }
9960 break;
9961 }
9962
9963 if (is_artificial == false)
9964 {
9965 Type *member_type = dwarf->ResolveTypeUID(encoding_uid);
9966
9967 clang::FieldDecl *field_decl = NULL;
9968 if (tag == DW_TAG_member)
9969 {
9970 if (member_type)
9971 {
9972 if (accessibility == eAccessNone)
9973 accessibility = default_accessibility;
9974 member_accessibilities.push_back(accessibility);
9975
9976 uint64_t field_bit_offset = (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
9977 if (bit_size > 0)
9978 {
9979
9980 BitfieldInfo this_field_info;
9981 this_field_info.bit_offset = field_bit_offset;
9982 this_field_info.bit_size = bit_size;
9983
9984 /////////////////////////////////////////////////////////////
9985 // How to locate a field given the DWARF debug information
9986 //
9987 // AT_byte_size indicates the size of the word in which the
9988 // bit offset must be interpreted.
9989 //
9990 // AT_data_member_location indicates the byte offset of the
9991 // word from the base address of the structure.
9992 //
9993 // AT_bit_offset indicates how many bits into the word
9994 // (according to the host endianness) the low-order bit of
9995 // the field starts. AT_bit_offset can be negative.
9996 //
9997 // AT_bit_size indicates the size of the field in bits.
9998 /////////////////////////////////////////////////////////////
9999
10000 if (byte_size == 0)
10001 byte_size = member_type->GetByteSize();
10002
10003 if (dwarf->GetObjectFile()->GetByteOrder() == eByteOrderLittle)
10004 {
10005 this_field_info.bit_offset += byte_size * 8;
10006 this_field_info.bit_offset -= (bit_offset + bit_size);
10007 }
10008 else
10009 {
10010 this_field_info.bit_offset += bit_offset;
10011 }
10012
10013 // Update the field bit offset we will report for layout
10014 field_bit_offset = this_field_info.bit_offset;
10015
10016 // If the member to be emitted did not start on a character boundary and there is
10017 // empty space between the last field and this one, then we need to emit an
10018 // anonymous member filling up the space up to its start. There are three cases
10019 // here:
10020 //
10021 // 1 If the previous member ended on a character boundary, then we can emit an
10022 // anonymous member starting at the most recent character boundary.
10023 //
10024 // 2 If the previous member did not end on a character boundary and the distance
10025 // from the end of the previous member to the current member is less than a
10026 // word width, then we can emit an anonymous member starting right after the
10027 // previous member and right before this member.
10028 //
10029 // 3 If the previous member did not end on a character boundary and the distance
10030 // from the end of the previous member to the current member is greater than
10031 // or equal a word width, then we act as in Case 1.
10032
10033 const uint64_t character_width = 8;
10034 const uint64_t word_width = 32;
10035
10036 // Objective-C has invalid DW_AT_bit_offset values in older versions
10037 // of clang, so we have to be careful and only insert unnamed bitfields
10038 // if we have a new enough clang.
10039 bool detect_unnamed_bitfields = true;
10040
10041 if (class_language == eLanguageTypeObjC || class_language == eLanguageTypeObjC_plus_plus)
10042 detect_unnamed_bitfields = dwarf_cu->Supports_unnamed_objc_bitfields ();
10043
10044 if (detect_unnamed_bitfields)
10045 {
10046 BitfieldInfo anon_field_info;
10047
10048 if ((this_field_info.bit_offset % character_width) != 0) // not char aligned
10049 {
10050 uint64_t last_field_end = 0;
10051
10052 if (last_field_info.IsValid())
10053 last_field_end = last_field_info.bit_offset + last_field_info.bit_size;
10054
10055 if (this_field_info.bit_offset != last_field_end)
10056 {
10057 if (((last_field_end % character_width) == 0) || // case 1
10058 (this_field_info.bit_offset - last_field_end >= word_width)) // case 3
10059 {
10060 anon_field_info.bit_size = this_field_info.bit_offset % character_width;
10061 anon_field_info.bit_offset = this_field_info.bit_offset - anon_field_info.bit_size;
10062 }
10063 else // case 2
10064 {
10065 anon_field_info.bit_size = this_field_info.bit_offset - last_field_end;
10066 anon_field_info.bit_offset = last_field_end;
10067 }
10068 }
10069 }
10070
10071 if (anon_field_info.IsValid())
10072 {
10073 clang::FieldDecl *unnamed_bitfield_decl =
10074 ClangASTContext::AddFieldToRecordType (class_clang_type,
10075 NULL,
10076 GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, word_width),
10077 accessibility,
10078 anon_field_info.bit_size);
10079
10080 layout_info.field_offsets.insert(
10081 std::make_pair(unnamed_bitfield_decl, anon_field_info.bit_offset));
10082 }
10083 }
10084 last_field_info = this_field_info;
10085 }
10086 else
10087 {
10088 last_field_info.Clear();
10089 }
10090
Greg Clayton99558cc42015-08-24 23:46:31 +000010091 CompilerType member_clang_type = member_type->GetLayoutCompilerType ();
Greg Clayton8b4edba2015-08-14 20:02:05 +000010092
10093 {
10094 // Older versions of clang emit array[0] and array[1] in the same way (<rdar://problem/12566646>).
10095 // If the current field is at the end of the structure, then there is definitely no room for extra
10096 // elements and we override the type to array[0].
10097
10098 CompilerType member_array_element_type;
10099 uint64_t member_array_size;
10100 bool member_array_is_incomplete;
10101
10102 if (member_clang_type.IsArrayType(&member_array_element_type,
10103 &member_array_size,
10104 &member_array_is_incomplete) &&
10105 !member_array_is_incomplete)
10106 {
10107 uint64_t parent_byte_size = parent_die->GetAttributeValueAsUnsigned(dwarf, dwarf_cu, DW_AT_byte_size, UINT64_MAX);
10108
10109 if (member_byte_offset >= parent_byte_size)
10110 {
10111 if (member_array_size != 1)
10112 {
10113 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,
10114 dwarf->MakeUserID(die->GetOffset()),
10115 name,
10116 encoding_uid,
10117 dwarf->MakeUserID(parent_die->GetOffset()));
10118 }
10119
10120 member_clang_type = CreateArrayType(member_array_element_type, 0, false);
10121 }
10122 }
10123 }
10124
10125 field_decl = ClangASTContext::AddFieldToRecordType (class_clang_type,
10126 name,
10127 member_clang_type,
10128 accessibility,
10129 bit_size);
10130
10131 SetMetadataAsUserID (field_decl, dwarf->MakeUserID(die->GetOffset()));
10132
10133 layout_info.field_offsets.insert(std::make_pair(field_decl, field_bit_offset));
10134 }
10135 else
10136 {
10137 if (name)
10138 module_sp->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64 " which was unable to be parsed",
10139 dwarf->MakeUserID(die->GetOffset()),
10140 name,
10141 encoding_uid);
10142 else
10143 module_sp->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member refers to type 0x%8.8" PRIx64 " which was unable to be parsed",
10144 dwarf->MakeUserID(die->GetOffset()),
10145 encoding_uid);
10146 }
10147 }
10148
10149 if (prop_name != NULL && member_type)
10150 {
10151 clang::ObjCIvarDecl *ivar_decl = NULL;
10152
10153 if (field_decl)
10154 {
10155 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
10156 assert (ivar_decl != NULL);
10157 }
10158
10159 ClangASTMetadata metadata;
10160 metadata.SetUserID (dwarf->MakeUserID(die->GetOffset()));
10161 delayed_properties.push_back(DelayedAddObjCClassProperty(class_clang_type,
10162 prop_name,
Greg Clayton99558cc42015-08-24 23:46:31 +000010163 member_type->GetLayoutCompilerType (),
Greg Clayton8b4edba2015-08-14 20:02:05 +000010164 ivar_decl,
10165 prop_setter_name,
10166 prop_getter_name,
10167 prop_attributes,
10168 &metadata));
10169
10170 if (ivar_decl)
10171 SetMetadataAsUserID (ivar_decl, dwarf->MakeUserID(die->GetOffset()));
10172 }
10173 }
10174 }
10175 ++member_idx;
10176 }
10177 break;
10178
10179 case DW_TAG_subprogram:
10180 // Let the type parsing code handle this one for us.
10181 member_function_dies.Append (die);
10182 break;
10183
10184 case DW_TAG_inheritance:
10185 {
10186 is_a_class = true;
10187 if (default_accessibility == eAccessNone)
10188 default_accessibility = eAccessPrivate;
10189 // TODO: implement DW_TAG_inheritance type parsing
10190 DWARFDebugInfoEntry::Attributes attributes;
10191 const size_t num_attributes = die->GetAttributes (dwarf,
10192 dwarf_cu,
10193 fixed_form_sizes,
10194 attributes);
10195 if (num_attributes > 0)
10196 {
10197 Declaration decl;
10198 DWARFExpression location;
10199 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
10200 AccessType accessibility = default_accessibility;
10201 bool is_virtual = false;
10202 bool is_base_of_class = true;
10203 off_t member_byte_offset = 0;
10204 uint32_t i;
10205 for (i=0; i<num_attributes; ++i)
10206 {
10207 const dw_attr_t attr = attributes.AttributeAtIndex(i);
10208 DWARFFormValue form_value;
10209 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
10210 {
10211 switch (attr)
10212 {
10213 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
10214 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
10215 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
10216 case DW_AT_type: encoding_uid = form_value.Reference(); break;
10217 case DW_AT_data_member_location:
10218 if (form_value.BlockData())
10219 {
10220 Value initialValue(0);
10221 Value memberOffset(0);
10222 const DWARFDataExtractor& debug_info_data = dwarf->get_debug_info_data();
10223 uint32_t block_length = form_value.Unsigned();
10224 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
10225 if (DWARFExpression::Evaluate (NULL,
10226 NULL,
10227 NULL,
10228 NULL,
10229 module_sp,
10230 debug_info_data,
10231 block_offset,
10232 block_length,
10233 eRegisterKindDWARF,
10234 &initialValue,
10235 memberOffset,
10236 NULL))
10237 {
10238 member_byte_offset = memberOffset.ResolveValue(NULL).UInt();
10239 }
10240 }
10241 else
10242 {
10243 // With DWARF 3 and later, if the value is an integer constant,
10244 // this form value is the offset in bytes from the beginning
10245 // of the containing entity.
10246 member_byte_offset = form_value.Unsigned();
10247 }
10248 break;
10249
10250 case DW_AT_accessibility:
10251 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
10252 break;
10253
10254 case DW_AT_virtuality:
10255 is_virtual = form_value.Boolean();
10256 break;
10257
10258 case DW_AT_sibling:
10259 break;
10260
10261 default:
10262 break;
10263 }
10264 }
10265 }
10266
10267 Type *base_class_type = dwarf->ResolveTypeUID(encoding_uid);
10268 if (base_class_type == NULL)
10269 {
10270 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",
10271 die->GetOffset(),
10272 encoding_uid,
10273 parent_die->GetOffset());
10274 break;
10275 }
10276
Greg Clayton99558cc42015-08-24 23:46:31 +000010277 CompilerType base_class_clang_type = base_class_type->GetFullCompilerType ();
Greg Clayton8b4edba2015-08-14 20:02:05 +000010278 assert (base_class_clang_type);
10279 if (class_language == eLanguageTypeObjC)
10280 {
10281 ast->SetObjCSuperClass(class_clang_type, base_class_clang_type);
10282 }
10283 else
10284 {
10285 base_classes.push_back (ast->CreateBaseClassSpecifier (base_class_clang_type.GetOpaqueQualType(),
10286 accessibility,
10287 is_virtual,
10288 is_base_of_class));
10289
10290 if (is_virtual)
10291 {
10292 // Do not specify any offset for virtual inheritance. The DWARF produced by clang doesn't
10293 // give us a constant offset, but gives us a DWARF expressions that requires an actual object
10294 // in memory. the DW_AT_data_member_location for a virtual base class looks like:
10295 // DW_AT_data_member_location( DW_OP_dup, DW_OP_deref, DW_OP_constu(0x00000018), DW_OP_minus, DW_OP_deref, DW_OP_plus )
10296 // Given this, there is really no valid response we can give to clang for virtual base
10297 // class offsets, and this should eventually be removed from LayoutRecordType() in the external
10298 // AST source in clang.
10299 }
10300 else
10301 {
10302 layout_info.base_offsets.insert(
10303 std::make_pair(ast->GetAsCXXRecordDecl(base_class_clang_type.GetOpaqueQualType()),
10304 clang::CharUnits::fromQuantity(member_byte_offset)));
10305 }
10306 }
10307 }
10308 }
10309 break;
10310
10311 default:
10312 break;
10313 }
10314 }
10315
10316 return count;
10317}
10318
10319
10320size_t
10321ClangASTContext::ParseChildParameters (const SymbolContext& sc,
10322 clang::DeclContext *containing_decl_ctx,
10323 SymbolFileDWARF *dwarf,
10324 DWARFCompileUnit* dwarf_cu,
10325 const DWARFDebugInfoEntry *parent_die,
10326 bool skip_artificial,
10327 bool &is_static,
10328 bool &is_variadic,
10329 std::vector<CompilerType>& function_param_types,
10330 std::vector<clang::ParmVarDecl*>& function_param_decls,
10331 unsigned &type_quals)
10332{
10333 if (parent_die == NULL)
10334 return 0;
10335
10336 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
10337
10338 size_t arg_idx = 0;
10339 const DWARFDebugInfoEntry *die;
10340 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
10341 {
10342 dw_tag_t tag = die->Tag();
10343 switch (tag)
10344 {
10345 case DW_TAG_formal_parameter:
10346 {
10347 DWARFDebugInfoEntry::Attributes attributes;
10348 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, fixed_form_sizes, attributes);
10349 if (num_attributes > 0)
10350 {
10351 const char *name = NULL;
10352 Declaration decl;
10353 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
10354 bool is_artificial = false;
10355 // one of None, Auto, Register, Extern, Static, PrivateExtern
10356
10357 clang::StorageClass storage = clang::SC_None;
10358 uint32_t i;
10359 for (i=0; i<num_attributes; ++i)
10360 {
10361 const dw_attr_t attr = attributes.AttributeAtIndex(i);
10362 DWARFFormValue form_value;
10363 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
10364 {
10365 switch (attr)
10366 {
10367 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
10368 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
10369 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
10370 case DW_AT_name: name = form_value.AsCString(&dwarf->get_debug_str_data()); break;
10371 case DW_AT_type: param_type_die_offset = form_value.Reference(); break;
10372 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
10373 case DW_AT_location:
10374 // if (form_value.BlockData())
10375 // {
10376 // const DWARFDataExtractor& debug_info_data = debug_info();
10377 // uint32_t block_length = form_value.Unsigned();
10378 // DWARFDataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
10379 // }
10380 // else
10381 // {
10382 // }
10383 // break;
10384 case DW_AT_const_value:
10385 case DW_AT_default_value:
10386 case DW_AT_description:
10387 case DW_AT_endianity:
10388 case DW_AT_is_optional:
10389 case DW_AT_segment:
10390 case DW_AT_variable_parameter:
10391 default:
10392 case DW_AT_abstract_origin:
10393 case DW_AT_sibling:
10394 break;
10395 }
10396 }
10397 }
10398
10399 bool skip = false;
10400 if (skip_artificial)
10401 {
10402 if (is_artificial)
10403 {
10404 // In order to determine if a C++ member function is
10405 // "const" we have to look at the const-ness of "this"...
10406 // Ugly, but that
10407 if (arg_idx == 0)
10408 {
10409 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
10410 {
10411 // Often times compilers omit the "this" name for the
10412 // specification DIEs, so we can't rely upon the name
10413 // being in the formal parameter DIE...
10414 if (name == NULL || ::strcmp(name, "this")==0)
10415 {
10416 Type *this_type = dwarf->ResolveTypeUID (param_type_die_offset);
10417 if (this_type)
10418 {
10419 uint32_t encoding_mask = this_type->GetEncodingMask();
10420 if (encoding_mask & Type::eEncodingIsPointerUID)
10421 {
10422 is_static = false;
10423
10424 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
10425 type_quals |= clang::Qualifiers::Const;
10426 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
10427 type_quals |= clang::Qualifiers::Volatile;
10428 }
10429 }
10430 }
10431 }
10432 }
10433 skip = true;
10434 }
10435 else
10436 {
10437
10438 // HACK: Objective C formal parameters "self" and "_cmd"
10439 // are not marked as artificial in the DWARF...
10440 CompileUnit *comp_unit = dwarf->GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
10441 if (comp_unit)
10442 {
10443 switch (comp_unit->GetLanguage())
10444 {
10445 case eLanguageTypeObjC:
10446 case eLanguageTypeObjC_plus_plus:
10447 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
10448 skip = true;
10449 break;
10450 default:
10451 break;
10452 }
10453 }
10454 }
10455 }
10456
10457 if (!skip)
10458 {
10459 Type *type = dwarf->ResolveTypeUID(param_type_die_offset);
10460 if (type)
10461 {
Greg Clayton99558cc42015-08-24 23:46:31 +000010462 function_param_types.push_back (type->GetForwardCompilerType ());
Greg Clayton8b4edba2015-08-14 20:02:05 +000010463
10464 clang::ParmVarDecl *param_var_decl = CreateParameterDeclaration (name,
Greg Clayton99558cc42015-08-24 23:46:31 +000010465 type->GetForwardCompilerType (),
Greg Clayton8b4edba2015-08-14 20:02:05 +000010466 storage);
10467 assert(param_var_decl);
10468 function_param_decls.push_back(param_var_decl);
10469
10470 SetMetadataAsUserID (param_var_decl, dwarf->MakeUserID(die->GetOffset()));
10471 }
10472 }
10473 }
10474 arg_idx++;
10475 }
10476 break;
10477
10478 case DW_TAG_unspecified_parameters:
10479 is_variadic = true;
10480 break;
10481
10482 case DW_TAG_template_type_parameter:
10483 case DW_TAG_template_value_parameter:
10484 // The one caller of this was never using the template_param_infos,
10485 // and the local variable was taking up a large amount of stack space
10486 // in SymbolFileDWARF::ParseType() so this was removed. If we ever need
10487 // the template params back, we can add them back.
10488 // ParseTemplateDIE (dwarf_cu, die, template_param_infos);
10489 break;
10490
10491 default:
10492 break;
10493 }
10494 }
10495 return arg_idx;
10496}
10497
10498void
10499ClangASTContext::ParseChildArrayInfo (const SymbolContext& sc,
10500 SymbolFileDWARF *dwarf,
10501 DWARFCompileUnit* dwarf_cu,
10502 const DWARFDebugInfoEntry *parent_die,
10503 int64_t& first_index,
10504 std::vector<uint64_t>& element_orders,
10505 uint32_t& byte_stride,
10506 uint32_t& bit_stride)
10507{
10508 if (parent_die == NULL)
10509 return;
10510
10511 const DWARFDebugInfoEntry *die;
10512 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
10513 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
10514 {
10515 const dw_tag_t tag = die->Tag();
10516 switch (tag)
10517 {
10518 case DW_TAG_subrange_type:
10519 {
10520 DWARFDebugInfoEntry::Attributes attributes;
10521 const size_t num_child_attributes = die->GetAttributes(dwarf, dwarf_cu, fixed_form_sizes, attributes);
10522 if (num_child_attributes > 0)
10523 {
10524 uint64_t num_elements = 0;
10525 uint64_t lower_bound = 0;
10526 uint64_t upper_bound = 0;
10527 bool upper_bound_valid = false;
10528 uint32_t i;
10529 for (i=0; i<num_child_attributes; ++i)
10530 {
10531 const dw_attr_t attr = attributes.AttributeAtIndex(i);
10532 DWARFFormValue form_value;
10533 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
10534 {
10535 switch (attr)
10536 {
10537 case DW_AT_name:
10538 break;
10539
10540 case DW_AT_count:
10541 num_elements = form_value.Unsigned();
10542 break;
10543
10544 case DW_AT_bit_stride:
10545 bit_stride = form_value.Unsigned();
10546 break;
10547
10548 case DW_AT_byte_stride:
10549 byte_stride = form_value.Unsigned();
10550 break;
10551
10552 case DW_AT_lower_bound:
10553 lower_bound = form_value.Unsigned();
10554 break;
10555
10556 case DW_AT_upper_bound:
10557 upper_bound_valid = true;
10558 upper_bound = form_value.Unsigned();
10559 break;
10560
10561 default:
10562 case DW_AT_abstract_origin:
10563 case DW_AT_accessibility:
10564 case DW_AT_allocated:
10565 case DW_AT_associated:
10566 case DW_AT_data_location:
10567 case DW_AT_declaration:
10568 case DW_AT_description:
10569 case DW_AT_sibling:
10570 case DW_AT_threads_scaled:
10571 case DW_AT_type:
10572 case DW_AT_visibility:
10573 break;
10574 }
10575 }
10576 }
10577
10578 if (num_elements == 0)
10579 {
10580 if (upper_bound_valid && upper_bound >= lower_bound)
10581 num_elements = upper_bound - lower_bound + 1;
10582 }
10583
10584 element_orders.push_back (num_elements);
10585 }
10586 }
10587 break;
10588 }
10589 }
10590}
10591
Greg Clayton99558cc42015-08-24 23:46:31 +000010592//clang::DeclContext*
10593//ClangASTContext::GetClangDeclContextContainingTypeUID (SymbolFileDWARF *dwarf, lldb::user_id_t type_uid)
10594//{
10595// DWARFDebugInfo* debug_info = dwarf->DebugInfo();
10596// if (debug_info && dwarf->UserIDMatches(type_uid))
10597// {
10598// DWARFCompileUnitSP cu_sp;
10599// const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
10600// if (die)
10601// return GetClangDeclContextContainingDIE (dwarf, cu_sp.get(), die, NULL);
10602// }
10603// return NULL;
10604//}
10605//
10606//----------------------------------------------------------------------
10607// CompilerDeclContext functions
10608//----------------------------------------------------------------------
10609
10610bool
10611ClangASTContext::DeclContextIsStructUnionOrClass (void *opaque_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +000010612{
Greg Clayton99558cc42015-08-24 23:46:31 +000010613 if (opaque_decl_ctx)
10614 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
10615 else
10616 return false;
Greg Clayton8b4edba2015-08-14 20:02:05 +000010617}
10618
Greg Clayton99558cc42015-08-24 23:46:31 +000010619ConstString
10620ClangASTContext::DeclContextGetName (void *opaque_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +000010621{
Greg Clayton99558cc42015-08-24 23:46:31 +000010622 if (opaque_decl_ctx)
10623 {
10624 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10625 if (named_decl)
10626 return ConstString(named_decl->getName());
10627 }
10628 return ConstString();
10629}
10630
10631bool
10632ClangASTContext::DeclContextIsClassMethod (void *opaque_decl_ctx,
10633 lldb::LanguageType *language_ptr,
10634 bool *is_instance_method_ptr,
10635 ConstString *language_object_name_ptr)
10636{
10637 if (opaque_decl_ctx)
10638 {
10639 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10640 if (ObjCMethodDecl *objc_method = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
10641 {
10642 if (is_instance_method_ptr)
10643 *is_instance_method_ptr = objc_method->isInstanceMethod();
10644 if (language_ptr)
10645 *language_ptr = eLanguageTypeObjC;
10646 if (language_object_name_ptr)
10647 language_object_name_ptr->SetCString("self");
10648 return true;
10649 }
10650 else if (CXXMethodDecl *cxx_method = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
10651 {
10652 if (is_instance_method_ptr)
10653 *is_instance_method_ptr = cxx_method->isInstance();
10654 if (language_ptr)
10655 *language_ptr = eLanguageTypeC_plus_plus;
10656 if (language_object_name_ptr)
10657 language_object_name_ptr->SetCString("this");
10658 return true;
10659 }
10660 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
10661 {
10662 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), function_decl);
10663 if (metadata && metadata->HasObjectPtr())
10664 {
10665 if (is_instance_method_ptr)
10666 *is_instance_method_ptr = true;
10667 if (language_ptr)
10668 *language_ptr = eLanguageTypeObjC;
10669 if (language_object_name_ptr)
10670 language_object_name_ptr->SetCString (metadata->GetObjectPtrName());
10671 return true;
10672 }
10673 }
10674 }
10675 return false;
10676}
10677
10678clang::DeclContext *
10679ClangASTContext::DeclContextGetAsDeclContext (const CompilerDeclContext &dc)
10680{
10681 if (dc.IsClang())
10682 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10683 return nullptr;
10684}
10685
10686
10687ObjCMethodDecl *
10688ClangASTContext::DeclContextGetAsObjCMethodDecl (const CompilerDeclContext &dc)
10689{
10690 if (dc.IsClang())
10691 return llvm::dyn_cast<clang::ObjCMethodDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
10692 return nullptr;
10693}
10694
10695CXXMethodDecl *
10696ClangASTContext::DeclContextGetAsCXXMethodDecl (const CompilerDeclContext &dc)
10697{
10698 if (dc.IsClang())
10699 return llvm::dyn_cast<clang::CXXMethodDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
10700 return nullptr;
10701}
10702
10703clang::FunctionDecl *
10704ClangASTContext::DeclContextGetAsFunctionDecl (const CompilerDeclContext &dc)
10705{
10706 if (dc.IsClang())
10707 return llvm::dyn_cast<clang::FunctionDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
10708 return nullptr;
10709}
10710
10711clang::NamespaceDecl *
10712ClangASTContext::DeclContextGetAsNamespaceDecl (const CompilerDeclContext &dc)
10713{
10714 if (dc.IsClang())
10715 return llvm::dyn_cast<clang::NamespaceDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
10716 return nullptr;
10717}
10718
10719ClangASTMetadata *
10720ClangASTContext::DeclContextGetMetaData (const CompilerDeclContext &dc, const void *object)
10721{
10722 clang::ASTContext *ast = DeclContextGetClangASTContext (dc);
10723 if (ast)
10724 return ClangASTContext::GetMetadata (ast, object);
10725 return nullptr;
10726}
10727
10728clang::ASTContext *
10729ClangASTContext::DeclContextGetClangASTContext (const CompilerDeclContext &dc)
10730{
10731 TypeSystem *type_system = dc.GetTypeSystem();
10732 if (type_system)
10733 {
10734 ClangASTContext *ast = type_system->AsClangASTContext();
10735 if (ast)
10736 return ast->getASTContext();
10737 }
10738 return nullptr;
Greg Clayton8b4edba2015-08-14 20:02:05 +000010739}
10740
10741
10742clang::DeclContext *
10743ClangASTContext::GetClangDeclContextForDIE (SymbolFileDWARF *dwarf,
Greg Clayton8b4edba2015-08-14 20:02:05 +000010744 DWARFCompileUnit *cu,
10745 const DWARFDebugInfoEntry *die)
10746{
Greg Clayton99558cc42015-08-24 23:46:31 +000010747 if (die)
Greg Clayton8b4edba2015-08-14 20:02:05 +000010748 {
Greg Clayton99558cc42015-08-24 23:46:31 +000010749 clang::DeclContext *decl_ctx = GetCachedClangDeclContextForDIE (die);
10750 if (decl_ctx)
10751 return decl_ctx;
Greg Clayton8b4edba2015-08-14 20:02:05 +000010752
Greg Clayton99558cc42015-08-24 23:46:31 +000010753 bool try_parsing_type = true;
10754 switch (die->Tag())
Greg Clayton8b4edba2015-08-14 20:02:05 +000010755 {
Greg Clayton99558cc42015-08-24 23:46:31 +000010756 case DW_TAG_compile_unit:
10757 decl_ctx = m_clang_tu_decl;
10758 try_parsing_type = false;
10759 break;
10760
10761 case DW_TAG_namespace:
10762 decl_ctx = ResolveNamespaceDIE (dwarf, cu, die);
10763 try_parsing_type = false;
10764 break;
10765
10766 default:
10767 break;
10768 }
10769
10770 if (decl_ctx == nullptr && try_parsing_type)
10771 {
10772 Type* type = dwarf->ResolveType (cu, die);
10773 if (type)
10774 decl_ctx = GetCachedClangDeclContextForDIE (die);
10775 }
10776
10777 if (decl_ctx)
10778 {
10779 LinkDeclContextToDIE (decl_ctx, die);
10780 return decl_ctx;
Greg Clayton8b4edba2015-08-14 20:02:05 +000010781 }
10782 }
Greg Clayton99558cc42015-08-24 23:46:31 +000010783 return nullptr;
Greg Clayton8b4edba2015-08-14 20:02:05 +000010784}
10785
10786clang::NamespaceDecl *
10787ClangASTContext::ResolveNamespaceDIE (SymbolFileDWARF *dwarf,
10788 DWARFCompileUnit *dwarf_cu,
10789 const DWARFDebugInfoEntry *die)
10790{
10791 if (die && die->Tag() == DW_TAG_namespace)
10792 {
10793 // See if we already parsed this namespace DIE and associated it with a
10794 // uniqued namespace declaration
Greg Clayton6dc8d582015-08-18 22:32:36 +000010795 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010796 if (namespace_decl)
10797 return namespace_decl;
10798 else
10799 {
10800 const char *namespace_name = die->GetAttributeValueAsString(dwarf, dwarf_cu, DW_AT_name, NULL);
10801 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf, dwarf_cu, die, NULL);
10802 namespace_decl = GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
10803 Log *log = nullptr;// (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
10804 if (log)
10805 {
10806 if (namespace_name)
10807 {
10808 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
10809 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
10810 static_cast<void*>(getASTContext()),
10811 dwarf->MakeUserID(die->GetOffset()),
10812 namespace_name,
10813 static_cast<void*>(namespace_decl),
10814 static_cast<void*>(namespace_decl->getOriginalNamespace()));
10815 }
10816 else
10817 {
10818 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
10819 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
10820 static_cast<void*>(getASTContext()),
10821 dwarf->MakeUserID(die->GetOffset()),
10822 static_cast<void*>(namespace_decl),
10823 static_cast<void*>(namespace_decl->getOriginalNamespace()));
10824 }
10825 }
10826
10827 if (namespace_decl)
Greg Clayton6dc8d582015-08-18 22:32:36 +000010828 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010829 return namespace_decl;
10830 }
10831 }
Greg Clayton99558cc42015-08-24 23:46:31 +000010832 return nullptr;
Greg Clayton8b4edba2015-08-14 20:02:05 +000010833}
10834
10835clang::DeclContext *
10836ClangASTContext::GetClangDeclContextContainingDIE (SymbolFileDWARF *dwarf,
10837 DWARFCompileUnit *cu,
10838 const DWARFDebugInfoEntry *die,
10839 const DWARFDebugInfoEntry **decl_ctx_die_copy)
10840{
Greg Clayton6dc8d582015-08-18 22:32:36 +000010841 if (m_clang_tu_decl == NULL)
10842 m_clang_tu_decl = getASTContext()->getTranslationUnitDecl();
Greg Clayton8b4edba2015-08-14 20:02:05 +000010843
10844 const DWARFDebugInfoEntry *decl_ctx_die = dwarf->GetDeclContextDIEContainingDIE (cu, die);
10845
10846 if (decl_ctx_die_copy)
10847 *decl_ctx_die_copy = decl_ctx_die;
10848
10849 if (decl_ctx_die)
10850 {
Greg Clayton99558cc42015-08-24 23:46:31 +000010851 clang::DeclContext *clang_decl_ctx = GetClangDeclContextForDIE (dwarf, cu, decl_ctx_die);
10852 if (clang_decl_ctx)
10853 return clang_decl_ctx;
Greg Clayton8b4edba2015-08-14 20:02:05 +000010854 }
Greg Clayton6dc8d582015-08-18 22:32:36 +000010855 return m_clang_tu_decl;
Greg Clayton8b4edba2015-08-14 20:02:05 +000010856}
10857
10858
10859
10860TypeSP
10861ClangASTContext::ParseTypeFromDWARF (const SymbolContext& sc,
10862 SymbolFileDWARF *dwarf,
10863 DWARFCompileUnit* dwarf_cu,
10864 const DWARFDebugInfoEntry *die,
10865 Log *log,
10866 bool *type_is_new_ptr)
10867{
10868 TypeSP type_sp;
10869
10870 if (type_is_new_ptr)
10871 *type_is_new_ptr = false;
10872
10873#if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
10874 static DIEStack g_die_stack;
10875 DIEStack::ScopedPopper scoped_die_logger(g_die_stack);
10876#endif
10877
10878 AccessType accessibility = eAccessNone;
10879 if (die != NULL)
10880 {
10881 if (log)
10882 {
10883 const DWARFDebugInfoEntry *context_die;
10884 clang::DeclContext *context = GetClangDeclContextContainingDIE (dwarf, dwarf_cu, die, &context_die);
10885
10886 dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x, decl_ctx = %p (die 0x%8.8x)) %s name = '%s')",
10887 die->GetOffset(),
10888 static_cast<void*>(context),
10889 context_die->GetOffset(),
10890 DW_TAG_value_to_name(die->Tag()),
10891 die->GetName(dwarf, dwarf_cu));
10892
10893#if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
10894 scoped_die_logger.Push (dwarf_cu, die);
10895 g_die_stack.LogDIEs(log, dwarf);
10896#endif
10897 }
10898 //
10899 // Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
10900 // if (log && dwarf_cu)
10901 // {
10902 // StreamString s;
10903 // die->DumpLocation (this, dwarf_cu, s);
10904 // dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
10905 //
10906 // }
10907
10908 Type *type_ptr = dwarf->m_die_to_type.lookup (die);
10909 TypeList* type_list = dwarf->GetTypeList();
10910 if (type_ptr == NULL)
10911 {
10912 if (type_is_new_ptr)
10913 *type_is_new_ptr = true;
10914
10915 const dw_tag_t tag = die->Tag();
10916
10917 bool is_forward_declaration = false;
10918 DWARFDebugInfoEntry::Attributes attributes;
10919 const char *type_name_cstr = NULL;
10920 ConstString type_name_const_str;
10921 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
10922 uint64_t byte_size = 0;
10923 Declaration decl;
10924
10925 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
10926 CompilerType clang_type;
10927 DWARFFormValue form_value;
10928
10929 dw_attr_t attr;
10930
10931 switch (tag)
10932 {
10933 case DW_TAG_base_type:
10934 case DW_TAG_pointer_type:
10935 case DW_TAG_reference_type:
10936 case DW_TAG_rvalue_reference_type:
10937 case DW_TAG_typedef:
10938 case DW_TAG_const_type:
10939 case DW_TAG_restrict_type:
10940 case DW_TAG_volatile_type:
10941 case DW_TAG_unspecified_type:
10942 {
10943 // Set a bit that lets us know that we are currently parsing this
10944 dwarf->m_die_to_type[die] = DIE_IS_BEING_PARSED;
10945
10946 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, NULL, attributes);
10947 uint32_t encoding = 0;
10948 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
10949
10950 if (num_attributes > 0)
10951 {
10952 uint32_t i;
10953 for (i=0; i<num_attributes; ++i)
10954 {
10955 attr = attributes.AttributeAtIndex(i);
10956 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
10957 {
10958 switch (attr)
10959 {
10960 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
10961 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
10962 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
10963 case DW_AT_name:
10964
10965 type_name_cstr = form_value.AsCString(&dwarf->get_debug_str_data());
10966 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
10967 // include the "&"...
10968 if (tag == DW_TAG_reference_type)
10969 {
10970 if (strchr (type_name_cstr, '&') == NULL)
10971 type_name_cstr = NULL;
10972 }
10973 if (type_name_cstr)
10974 type_name_const_str.SetCString(type_name_cstr);
10975 break;
10976 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
10977 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
10978 case DW_AT_type: encoding_uid = form_value.Reference(); break;
10979 default:
10980 case DW_AT_sibling:
10981 break;
10982 }
10983 }
10984 }
10985 }
10986
10987 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);
10988
10989 switch (tag)
10990 {
10991 default:
10992 break;
10993
10994 case DW_TAG_unspecified_type:
10995 if (strcmp(type_name_cstr, "nullptr_t") == 0 ||
10996 strcmp(type_name_cstr, "decltype(nullptr)") == 0 )
10997 {
10998 resolve_state = Type::eResolveStateFull;
10999 clang_type = GetBasicType(eBasicTypeNullPtr);
11000 break;
11001 }
11002 // Fall through to base type below in case we can handle the type there...
11003
11004 case DW_TAG_base_type:
11005 resolve_state = Type::eResolveStateFull;
11006 clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
11007 encoding,
11008 byte_size * 8);
11009 break;
11010
11011 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
11012 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
11013 case DW_TAG_rvalue_reference_type: encoding_data_type = Type::eEncodingIsRValueReferenceUID; break;
11014 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
11015 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
11016 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
11017 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
11018 }
11019
11020 if (!clang_type && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID) && sc.comp_unit != NULL)
11021 {
11022 bool translation_unit_is_objc = (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus);
11023
11024 if (translation_unit_is_objc)
11025 {
11026 if (type_name_cstr != NULL)
11027 {
11028 static ConstString g_objc_type_name_id("id");
11029 static ConstString g_objc_type_name_Class("Class");
11030 static ConstString g_objc_type_name_selector("SEL");
11031
11032 if (type_name_const_str == g_objc_type_name_id)
11033 {
11034 if (log)
11035 dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.",
11036 die->GetOffset(),
11037 DW_TAG_value_to_name(die->Tag()),
11038 die->GetName(dwarf, dwarf_cu));
11039 clang_type = GetBasicType(eBasicTypeObjCID);
11040 encoding_data_type = Type::eEncodingIsUID;
11041 encoding_uid = LLDB_INVALID_UID;
11042 resolve_state = Type::eResolveStateFull;
11043
11044 }
11045 else if (type_name_const_str == g_objc_type_name_Class)
11046 {
11047 if (log)
11048 dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.",
11049 die->GetOffset(),
11050 DW_TAG_value_to_name(die->Tag()),
11051 die->GetName(dwarf, dwarf_cu));
11052 clang_type = GetBasicType(eBasicTypeObjCClass);
11053 encoding_data_type = Type::eEncodingIsUID;
11054 encoding_uid = LLDB_INVALID_UID;
11055 resolve_state = Type::eResolveStateFull;
11056 }
11057 else if (type_name_const_str == g_objc_type_name_selector)
11058 {
11059 if (log)
11060 dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.",
11061 die->GetOffset(),
11062 DW_TAG_value_to_name(die->Tag()),
11063 die->GetName(dwarf, dwarf_cu));
11064 clang_type = GetBasicType(eBasicTypeObjCSel);
11065 encoding_data_type = Type::eEncodingIsUID;
11066 encoding_uid = LLDB_INVALID_UID;
11067 resolve_state = Type::eResolveStateFull;
11068 }
11069 }
11070 else if (encoding_data_type == Type::eEncodingIsPointerUID && encoding_uid != LLDB_INVALID_UID)
11071 {
11072 // Clang sometimes erroneously emits id as objc_object*. In that case we fix up the type to "id".
11073
11074 DWARFDebugInfoEntry* encoding_die = dwarf_cu->GetDIEPtr(encoding_uid);
11075
11076 if (encoding_die && encoding_die->Tag() == DW_TAG_structure_type)
11077 {
11078 if (const char *struct_name = encoding_die->GetAttributeValueAsString(dwarf, dwarf_cu, DW_AT_name, NULL))
11079 {
11080 if (!strcmp(struct_name, "objc_object"))
11081 {
11082 if (log)
11083 dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is 'objc_object*', which we overrode to 'id'.",
11084 die->GetOffset(),
11085 DW_TAG_value_to_name(die->Tag()),
11086 die->GetName(dwarf, dwarf_cu));
11087 clang_type = GetBasicType(eBasicTypeObjCID);
11088 encoding_data_type = Type::eEncodingIsUID;
11089 encoding_uid = LLDB_INVALID_UID;
11090 resolve_state = Type::eResolveStateFull;
11091 }
11092 }
11093 }
11094 }
11095 }
11096 }
11097
11098 type_sp.reset( new Type (dwarf->MakeUserID(die->GetOffset()),
11099 dwarf,
11100 type_name_const_str,
11101 byte_size,
11102 NULL,
11103 encoding_uid,
11104 encoding_data_type,
11105 &decl,
11106 clang_type,
11107 resolve_state));
11108
11109 dwarf->m_die_to_type[die] = type_sp.get();
11110
11111 // Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
11112 // if (encoding_type != NULL)
11113 // {
11114 // if (encoding_type != DIE_IS_BEING_PARSED)
11115 // type_sp->SetEncodingType(encoding_type);
11116 // else
11117 // m_indirect_fixups.push_back(type_sp.get());
11118 // }
11119 }
11120 break;
11121
11122 case DW_TAG_structure_type:
11123 case DW_TAG_union_type:
11124 case DW_TAG_class_type:
11125 {
11126 // Set a bit that lets us know that we are currently parsing this
11127 dwarf->m_die_to_type[die] = DIE_IS_BEING_PARSED;
11128 bool byte_size_valid = false;
11129
11130 LanguageType class_language = eLanguageTypeUnknown;
11131 bool is_complete_objc_class = false;
11132 //bool struct_is_class = false;
11133 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, NULL, attributes);
11134 if (num_attributes > 0)
11135 {
11136 uint32_t i;
11137 for (i=0; i<num_attributes; ++i)
11138 {
11139 attr = attributes.AttributeAtIndex(i);
11140 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
11141 {
11142 switch (attr)
11143 {
11144 case DW_AT_decl_file:
11145 if (dwarf_cu->DW_AT_decl_file_attributes_are_invalid())
11146 {
11147 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
11148 // point to the compile unit file, so we clear this invalid value
11149 // so that we can still unique types efficiently.
11150 decl.SetFile(FileSpec ("<invalid>", false));
11151 }
11152 else
11153 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
11154 break;
11155
11156 case DW_AT_decl_line:
11157 decl.SetLine(form_value.Unsigned());
11158 break;
11159
11160 case DW_AT_decl_column:
11161 decl.SetColumn(form_value.Unsigned());
11162 break;
11163
11164 case DW_AT_name:
11165 type_name_cstr = form_value.AsCString(&dwarf->get_debug_str_data());
11166 type_name_const_str.SetCString(type_name_cstr);
11167 break;
11168
11169 case DW_AT_byte_size:
11170 byte_size = form_value.Unsigned();
11171 byte_size_valid = true;
11172 break;
11173
11174 case DW_AT_accessibility:
11175 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
11176 break;
11177
11178 case DW_AT_declaration:
11179 is_forward_declaration = form_value.Boolean();
11180 break;
11181
11182 case DW_AT_APPLE_runtime_class:
11183 class_language = (LanguageType)form_value.Signed();
11184 break;
11185
11186 case DW_AT_APPLE_objc_complete_type:
11187 is_complete_objc_class = form_value.Signed();
11188 break;
11189
11190 case DW_AT_allocated:
11191 case DW_AT_associated:
11192 case DW_AT_data_location:
11193 case DW_AT_description:
11194 case DW_AT_start_scope:
11195 case DW_AT_visibility:
11196 default:
11197 case DW_AT_sibling:
11198 break;
11199 }
11200 }
11201 }
11202 }
11203
11204 // UniqueDWARFASTType is large, so don't create a local variables on the
11205 // stack, put it on the heap. This function is often called recursively
11206 // and clang isn't good and sharing the stack space for variables in different blocks.
11207 std::unique_ptr<UniqueDWARFASTType> unique_ast_entry_ap(new UniqueDWARFASTType());
11208
11209 // Only try and unique the type if it has a name.
11210 if (type_name_const_str &&
11211 dwarf->GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
11212 dwarf,
11213 dwarf_cu,
11214 die,
11215 decl,
11216 byte_size_valid ? byte_size : -1,
11217 *unique_ast_entry_ap))
11218 {
11219 // We have already parsed this type or from another
11220 // compile unit. GCC loves to use the "one definition
11221 // rule" which can result in multiple definitions
11222 // of the same class over and over in each compile
11223 // unit.
11224 type_sp = unique_ast_entry_ap->m_type_sp;
11225 if (type_sp)
11226 {
11227 dwarf->m_die_to_type[die] = type_sp.get();
11228 return type_sp;
11229 }
11230 }
11231
11232 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", dwarf->MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
11233
11234 int tag_decl_kind = -1;
11235 AccessType default_accessibility = eAccessNone;
11236 if (tag == DW_TAG_structure_type)
11237 {
11238 tag_decl_kind = clang::TTK_Struct;
11239 default_accessibility = eAccessPublic;
11240 }
11241 else if (tag == DW_TAG_union_type)
11242 {
11243 tag_decl_kind = clang::TTK_Union;
11244 default_accessibility = eAccessPublic;
11245 }
11246 else if (tag == DW_TAG_class_type)
11247 {
11248 tag_decl_kind = clang::TTK_Class;
11249 default_accessibility = eAccessPrivate;
11250 }
11251
11252 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
11253 die->HasChildren() == false &&
11254 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
11255 {
11256 // Work around an issue with clang at the moment where
11257 // forward declarations for objective C classes are emitted
11258 // as:
11259 // DW_TAG_structure_type [2]
11260 // DW_AT_name( "ForwardObjcClass" )
11261 // DW_AT_byte_size( 0x00 )
11262 // DW_AT_decl_file( "..." )
11263 // DW_AT_decl_line( 1 )
11264 //
11265 // Note that there is no DW_AT_declaration and there are
11266 // no children, and the byte size is zero.
11267 is_forward_declaration = true;
11268 }
11269
11270 if (class_language == eLanguageTypeObjC ||
11271 class_language == eLanguageTypeObjC_plus_plus)
11272 {
11273 if (!is_complete_objc_class && dwarf->Supports_DW_AT_APPLE_objc_complete_type(dwarf_cu))
11274 {
11275 // We have a valid eSymbolTypeObjCClass class symbol whose
11276 // name matches the current objective C class that we
11277 // are trying to find and this DIE isn't the complete
11278 // definition (we checked is_complete_objc_class above and
11279 // know it is false), so the real definition is in here somewhere
11280 type_sp = dwarf->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
11281
11282 if (!type_sp)
11283 {
11284 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
11285 if (debug_map_symfile)
11286 {
11287 // We weren't able to find a full declaration in
11288 // this DWARF, see if we have a declaration anywhere
11289 // else...
11290 type_sp = debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
11291 }
11292 }
11293
11294 if (type_sp)
11295 {
11296 if (log)
11297 {
11298 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
11299 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8" PRIx64,
11300 static_cast<void*>(this),
11301 die->GetOffset(),
11302 DW_TAG_value_to_name(tag),
11303 type_name_cstr,
11304 type_sp->GetID());
11305 }
11306
11307 // We found a real definition for this type elsewhere
11308 // so lets use it and cache the fact that we found
11309 // a complete type for this die
11310 dwarf->m_die_to_type[die] = type_sp.get();
11311 return type_sp;
11312 }
11313 }
11314 }
11315
11316
11317 if (is_forward_declaration)
11318 {
11319 // We have a forward declaration to a type and we need
11320 // to try and find a full declaration. We look in the
11321 // current type index just in case we have a forward
11322 // declaration followed by an actual declarations in the
11323 // DWARF. If this fails, we need to look elsewhere...
11324 if (log)
11325 {
11326 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
11327 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
11328 static_cast<void*>(this),
11329 die->GetOffset(),
11330 DW_TAG_value_to_name(tag),
11331 type_name_cstr);
11332 }
11333
11334 DWARFDeclContext die_decl_ctx;
11335 die->GetDWARFDeclContext(dwarf, dwarf_cu, die_decl_ctx);
11336
11337 //type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
11338 type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
11339
11340 if (!type_sp)
11341 {
11342 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
11343 if (debug_map_symfile)
11344 {
11345 // We weren't able to find a full declaration in
11346 // this DWARF, see if we have a declaration anywhere
11347 // else...
11348 type_sp = debug_map_symfile->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
11349 }
11350 }
11351
11352 if (type_sp)
11353 {
11354 if (log)
11355 {
11356 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
11357 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8" PRIx64,
11358 static_cast<void*>(this),
11359 die->GetOffset(),
11360 DW_TAG_value_to_name(tag),
11361 type_name_cstr,
11362 type_sp->GetID());
11363 }
11364
11365 // We found a real definition for this type elsewhere
11366 // so lets use it and cache the fact that we found
11367 // a complete type for this die
11368 dwarf->m_die_to_type[die] = type_sp.get();
11369 return type_sp;
11370 }
11371 }
11372 assert (tag_decl_kind != -1);
11373 bool clang_type_was_created = false;
Greg Clayton99558cc42015-08-24 23:46:31 +000011374 clang_type.SetCompilerType(this, dwarf->m_forward_decl_die_to_clang_type.lookup (die));
Greg Clayton8b4edba2015-08-14 20:02:05 +000011375 if (!clang_type)
11376 {
11377 const DWARFDebugInfoEntry *decl_ctx_die;
11378
11379 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf, dwarf_cu, die, &decl_ctx_die);
11380 if (accessibility == eAccessNone && decl_ctx)
11381 {
11382 // Check the decl context that contains this class/struct/union.
11383 // If it is a class we must give it an accessibility.
11384 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
11385 if (DeclKindIsCXXClass (containing_decl_kind))
11386 accessibility = default_accessibility;
11387 }
11388
11389 ClangASTMetadata metadata;
11390 metadata.SetUserID(dwarf->MakeUserID(die->GetOffset()));
11391 metadata.SetIsDynamicCXXType(dwarf->ClassOrStructIsVirtual (dwarf_cu, die));
11392
11393 if (type_name_cstr && strchr (type_name_cstr, '<'))
11394 {
11395 ClangASTContext::TemplateParameterInfos template_param_infos;
11396 if (ParseTemplateParameterInfos (dwarf, dwarf_cu, die, template_param_infos))
11397 {
11398 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (dwarf,
11399 decl_ctx,
11400 accessibility,
11401 type_name_cstr,
11402 tag_decl_kind,
11403 template_param_infos);
11404
11405 clang::ClassTemplateSpecializationDecl *class_specialization_decl = CreateClassTemplateSpecializationDecl (decl_ctx,
11406 class_template_decl,
11407 tag_decl_kind,
11408 template_param_infos);
11409 clang_type = CreateClassTemplateSpecializationType (class_specialization_decl);
11410 clang_type_was_created = true;
11411
11412 SetMetadata (class_template_decl, metadata);
11413 SetMetadata (class_specialization_decl, metadata);
11414 }
11415 }
11416
11417 if (!clang_type_was_created)
11418 {
11419 clang_type_was_created = true;
11420 clang_type = CreateRecordType (decl_ctx,
11421 accessibility,
11422 type_name_cstr,
11423 tag_decl_kind,
11424 class_language,
11425 &metadata);
11426 }
11427 }
11428
11429 // Store a forward declaration to this class type in case any
11430 // parameters in any class methods need it for the clang
11431 // types for function prototypes.
Greg Clayton6dc8d582015-08-18 22:32:36 +000011432 LinkDeclContextToDIE(GetDeclContextForType(clang_type), die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000011433 type_sp.reset (new Type (dwarf->MakeUserID(die->GetOffset()),
11434 dwarf,
11435 type_name_const_str,
11436 byte_size,
11437 NULL,
11438 LLDB_INVALID_UID,
11439 Type::eEncodingIsUID,
11440 &decl,
11441 clang_type,
11442 Type::eResolveStateForward));
11443
11444 type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
11445
11446
11447 // Add our type to the unique type map so we don't
11448 // end up creating many copies of the same type over
11449 // and over in the ASTContext for our module
11450 unique_ast_entry_ap->m_type_sp = type_sp;
11451 unique_ast_entry_ap->m_symfile = dwarf;
11452 unique_ast_entry_ap->m_cu = dwarf_cu;
11453 unique_ast_entry_ap->m_die = die;
11454 unique_ast_entry_ap->m_declaration = decl;
11455 unique_ast_entry_ap->m_byte_size = byte_size;
11456 dwarf->GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
11457 *unique_ast_entry_ap);
11458
11459 if (is_forward_declaration && die->HasChildren())
11460 {
11461 // Check to see if the DIE actually has a definition, some version of GCC will
11462 // emit DIEs with DW_AT_declaration set to true, but yet still have subprogram,
11463 // members, or inheritance, so we can't trust it
11464 const DWARFDebugInfoEntry *child_die = die->GetFirstChild();
11465 while (child_die)
11466 {
11467 switch (child_die->Tag())
11468 {
11469 case DW_TAG_inheritance:
11470 case DW_TAG_subprogram:
11471 case DW_TAG_member:
11472 case DW_TAG_APPLE_property:
11473 case DW_TAG_class_type:
11474 case DW_TAG_structure_type:
11475 case DW_TAG_enumeration_type:
11476 case DW_TAG_typedef:
11477 case DW_TAG_union_type:
11478 child_die = NULL;
11479 is_forward_declaration = false;
11480 break;
11481 default:
11482 child_die = child_die->GetSibling();
11483 break;
11484 }
11485 }
11486 }
11487
11488 if (!is_forward_declaration)
11489 {
11490 // Always start the definition for a class type so that
11491 // if the class has child classes or types that require
11492 // the class to be created for use as their decl contexts
11493 // the class will be ready to accept these child definitions.
11494 if (die->HasChildren() == false)
11495 {
11496 // No children for this struct/union/class, lets finish it
11497 ClangASTContext::StartTagDeclarationDefinition (clang_type);
11498 ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
11499
11500 if (tag == DW_TAG_structure_type) // this only applies in C
11501 {
11502 clang::RecordDecl *record_decl = ClangASTContext::GetAsRecordDecl(clang_type);
11503
11504 if (record_decl)
11505 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, LayoutInfo()));
11506 }
11507 }
11508 else if (clang_type_was_created)
11509 {
11510 // Start the definition if the class is not objective C since
11511 // the underlying decls respond to isCompleteDefinition(). Objective
11512 // C decls don't respond to isCompleteDefinition() so we can't
11513 // start the declaration definition right away. For C++ class/union/structs
11514 // we want to start the definition in case the class is needed as the
11515 // declaration context for a contained class or type without the need
11516 // to complete that type..
11517
11518 if (class_language != eLanguageTypeObjC &&
11519 class_language != eLanguageTypeObjC_plus_plus)
11520 ClangASTContext::StartTagDeclarationDefinition (clang_type);
11521
11522 // Leave this as a forward declaration until we need
11523 // to know the details of the type. lldb_private::Type
11524 // will automatically call the SymbolFile virtual function
Greg Clayton6dc8d582015-08-18 22:32:36 +000011525 // "SymbolFileDWARF::CompleteType(Type *)"
Greg Clayton8b4edba2015-08-14 20:02:05 +000011526 // When the definition needs to be defined.
11527 dwarf->m_forward_decl_die_to_clang_type[die] = clang_type.GetOpaqueQualType();
11528 dwarf->m_forward_decl_clang_type_to_die[ClangASTContext::RemoveFastQualifiers(clang_type).GetOpaqueQualType()] = die;
11529 SetHasExternalStorage (clang_type.GetOpaqueQualType(), true);
11530 }
11531 }
11532
11533 }
11534 break;
11535
11536 case DW_TAG_enumeration_type:
11537 {
11538 // Set a bit that lets us know that we are currently parsing this
11539 dwarf->m_die_to_type[die] = DIE_IS_BEING_PARSED;
11540
11541 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
11542
11543 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, NULL, attributes);
11544 if (num_attributes > 0)
11545 {
11546 uint32_t i;
11547
11548 for (i=0; i<num_attributes; ++i)
11549 {
11550 attr = attributes.AttributeAtIndex(i);
11551 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
11552 {
11553 switch (attr)
11554 {
11555 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
11556 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
11557 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
11558 case DW_AT_name:
11559 type_name_cstr = form_value.AsCString(&dwarf->get_debug_str_data());
11560 type_name_const_str.SetCString(type_name_cstr);
11561 break;
11562 case DW_AT_type: encoding_uid = form_value.Reference(); break;
11563 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
11564 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
11565 case DW_AT_declaration: break; //is_forward_declaration = form_value.Boolean(); break;
11566 case DW_AT_allocated:
11567 case DW_AT_associated:
11568 case DW_AT_bit_stride:
11569 case DW_AT_byte_stride:
11570 case DW_AT_data_location:
11571 case DW_AT_description:
11572 case DW_AT_start_scope:
11573 case DW_AT_visibility:
11574 case DW_AT_specification:
11575 case DW_AT_abstract_origin:
11576 case DW_AT_sibling:
11577 break;
11578 }
11579 }
11580 }
11581
11582 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", dwarf->MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
11583
11584 CompilerType enumerator_clang_type;
Greg Clayton99558cc42015-08-24 23:46:31 +000011585 clang_type.SetCompilerType (this, dwarf->m_forward_decl_die_to_clang_type.lookup (die));
Greg Clayton8b4edba2015-08-14 20:02:05 +000011586 if (!clang_type)
11587 {
11588 if (encoding_uid != DW_INVALID_OFFSET)
11589 {
11590 Type *enumerator_type = dwarf->ResolveTypeUID(encoding_uid);
11591 if (enumerator_type)
Greg Clayton99558cc42015-08-24 23:46:31 +000011592 enumerator_clang_type = enumerator_type->GetFullCompilerType ();
Greg Clayton8b4edba2015-08-14 20:02:05 +000011593 }
11594
11595 if (!enumerator_clang_type)
11596 enumerator_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
11597 DW_ATE_signed,
11598 byte_size * 8);
11599
11600 clang_type = CreateEnumerationType (type_name_cstr,
11601 GetClangDeclContextContainingDIE (dwarf, dwarf_cu, die, NULL),
11602 decl,
11603 enumerator_clang_type);
11604 }
11605 else
11606 {
11607 enumerator_clang_type = GetEnumerationIntegerType (clang_type.GetOpaqueQualType());
11608 }
11609
Greg Clayton6dc8d582015-08-18 22:32:36 +000011610 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000011611
11612 type_sp.reset( new Type (dwarf->MakeUserID(die->GetOffset()),
11613 dwarf,
11614 type_name_const_str,
11615 byte_size,
11616 NULL,
11617 encoding_uid,
11618 Type::eEncodingIsUID,
11619 &decl,
11620 clang_type,
11621 Type::eResolveStateForward));
11622
11623 ClangASTContext::StartTagDeclarationDefinition (clang_type);
11624 if (die->HasChildren())
11625 {
11626 SymbolContext cu_sc(dwarf->GetCompUnitForDWARFCompUnit(dwarf_cu));
11627 bool is_signed = false;
11628 enumerator_clang_type.IsIntegerType(is_signed);
11629 ParseChildEnumerators(cu_sc, clang_type, is_signed, type_sp->GetByteSize(), dwarf, dwarf_cu, die);
11630 }
11631 ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
11632 }
11633 }
11634 break;
11635
11636 case DW_TAG_inlined_subroutine:
11637 case DW_TAG_subprogram:
11638 case DW_TAG_subroutine_type:
11639 {
11640 // Set a bit that lets us know that we are currently parsing this
11641 dwarf->m_die_to_type[die] = DIE_IS_BEING_PARSED;
11642
11643 //const char *mangled = NULL;
11644 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
11645 bool is_variadic = false;
11646 bool is_inline = false;
11647 bool is_static = false;
11648 bool is_virtual = false;
11649 bool is_explicit = false;
11650 bool is_artificial = false;
11651 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
11652 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
11653 dw_offset_t object_pointer_die_offset = DW_INVALID_OFFSET;
11654
11655 unsigned type_quals = 0;
11656 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
11657
11658
11659 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, NULL, attributes);
11660 if (num_attributes > 0)
11661 {
11662 uint32_t i;
11663 for (i=0; i<num_attributes; ++i)
11664 {
11665 attr = attributes.AttributeAtIndex(i);
11666 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
11667 {
11668 switch (attr)
11669 {
11670 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
11671 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
11672 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
11673 case DW_AT_name:
11674 type_name_cstr = form_value.AsCString(&dwarf->get_debug_str_data());
11675 type_name_const_str.SetCString(type_name_cstr);
11676 break;
11677
11678 case DW_AT_linkage_name:
11679 case DW_AT_MIPS_linkage_name: break; // mangled = form_value.AsCString(&dwarf->get_debug_str_data()); break;
11680 case DW_AT_type: type_die_offset = form_value.Reference(); break;
11681 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
11682 case DW_AT_declaration: break; // is_forward_declaration = form_value.Boolean(); break;
11683 case DW_AT_inline: is_inline = form_value.Boolean(); break;
11684 case DW_AT_virtuality: is_virtual = form_value.Boolean(); break;
11685 case DW_AT_explicit: is_explicit = form_value.Boolean(); break;
11686 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
11687
11688
11689 case DW_AT_external:
11690 if (form_value.Unsigned())
11691 {
11692 if (storage == clang::SC_None)
11693 storage = clang::SC_Extern;
11694 else
11695 storage = clang::SC_PrivateExtern;
11696 }
11697 break;
11698
11699 case DW_AT_specification:
11700 specification_die_offset = form_value.Reference();
11701 break;
11702
11703 case DW_AT_abstract_origin:
11704 abstract_origin_die_offset = form_value.Reference();
11705 break;
11706
11707 case DW_AT_object_pointer:
11708 object_pointer_die_offset = form_value.Reference();
11709 break;
11710
11711 case DW_AT_allocated:
11712 case DW_AT_associated:
11713 case DW_AT_address_class:
11714 case DW_AT_calling_convention:
11715 case DW_AT_data_location:
11716 case DW_AT_elemental:
11717 case DW_AT_entry_pc:
11718 case DW_AT_frame_base:
11719 case DW_AT_high_pc:
11720 case DW_AT_low_pc:
11721 case DW_AT_prototyped:
11722 case DW_AT_pure:
11723 case DW_AT_ranges:
11724 case DW_AT_recursive:
11725 case DW_AT_return_addr:
11726 case DW_AT_segment:
11727 case DW_AT_start_scope:
11728 case DW_AT_static_link:
11729 case DW_AT_trampoline:
11730 case DW_AT_visibility:
11731 case DW_AT_vtable_elem_location:
11732 case DW_AT_description:
11733 case DW_AT_sibling:
11734 break;
11735 }
11736 }
11737 }
11738 }
11739
11740 std::string object_pointer_name;
11741 if (object_pointer_die_offset != DW_INVALID_OFFSET)
11742 {
11743 // Get the name from the object pointer die
11744 StreamString s;
11745 if (DWARFDebugInfoEntry::GetName (dwarf, dwarf_cu, object_pointer_die_offset, s))
11746 {
11747 object_pointer_name.assign(s.GetData());
11748 }
11749 }
11750
11751 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", dwarf->MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
11752
11753 CompilerType return_clang_type;
11754 Type *func_type = NULL;
11755
11756 if (type_die_offset != DW_INVALID_OFFSET)
11757 func_type = dwarf->ResolveTypeUID(type_die_offset);
11758
11759 if (func_type)
Greg Clayton99558cc42015-08-24 23:46:31 +000011760 return_clang_type = func_type->GetForwardCompilerType ();
Greg Clayton8b4edba2015-08-14 20:02:05 +000011761 else
11762 return_clang_type = GetBasicType(eBasicTypeVoid);
11763
11764
11765 std::vector<CompilerType> function_param_types;
11766 std::vector<clang::ParmVarDecl*> function_param_decls;
11767
11768 // Parse the function children for the parameters
11769
11770 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
11771 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf, dwarf_cu, die, &decl_ctx_die);
11772 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
11773
11774 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
11775 // Start off static. This will be set to false in ParseChildParameters(...)
11776 // if we find a "this" parameters as the first parameter
11777 if (is_cxx_method)
11778 is_static = true;
11779
11780 if (die->HasChildren())
11781 {
11782 bool skip_artificial = true;
11783 ParseChildParameters (sc,
11784 containing_decl_ctx,
11785 dwarf,
11786 dwarf_cu,
11787 die,
11788 skip_artificial,
11789 is_static,
11790 is_variadic,
11791 function_param_types,
11792 function_param_decls,
11793 type_quals);
11794 }
11795
11796 // clang_type will get the function prototype clang type after this call
11797 clang_type = CreateFunctionType (return_clang_type,
11798 function_param_types.data(),
11799 function_param_types.size(),
11800 is_variadic,
11801 type_quals);
11802
11803 bool ignore_containing_context = false;
11804
11805 if (type_name_cstr)
11806 {
11807 bool type_handled = false;
Greg Clayton99558cc42015-08-24 23:46:31 +000011808 if (tag == DW_TAG_subprogram ||
11809 tag == DW_TAG_inlined_subroutine)
Greg Clayton8b4edba2015-08-14 20:02:05 +000011810 {
11811 ObjCLanguageRuntime::MethodName objc_method (type_name_cstr, true);
11812 if (objc_method.IsValid(true))
11813 {
11814 CompilerType class_opaque_type;
11815 ConstString class_name(objc_method.GetClassName());
11816 if (class_name)
11817 {
11818 TypeSP complete_objc_class_type_sp (dwarf->FindCompleteObjCDefinitionTypeForDIE (NULL, class_name, false));
11819
11820 if (complete_objc_class_type_sp)
11821 {
Greg Clayton99558cc42015-08-24 23:46:31 +000011822 CompilerType type_clang_forward_type = complete_objc_class_type_sp->GetForwardCompilerType ();
Greg Clayton8b4edba2015-08-14 20:02:05 +000011823 if (ClangASTContext::IsObjCObjectOrInterfaceType(type_clang_forward_type))
11824 class_opaque_type = type_clang_forward_type;
11825 }
11826 }
11827
11828 if (class_opaque_type)
11829 {
11830 // If accessibility isn't set to anything valid, assume public for
11831 // now...
11832 if (accessibility == eAccessNone)
11833 accessibility = eAccessPublic;
11834
11835 clang::ObjCMethodDecl *objc_method_decl = AddMethodToObjCObjectType (class_opaque_type,
11836 type_name_cstr,
11837 clang_type,
11838 accessibility,
11839 is_artificial);
11840 type_handled = objc_method_decl != NULL;
11841 if (type_handled)
11842 {
Greg Clayton6dc8d582015-08-18 22:32:36 +000011843 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000011844 SetMetadataAsUserID (objc_method_decl, dwarf->MakeUserID(die->GetOffset()));
11845 }
11846 else
11847 {
11848 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",
11849 die->GetOffset(),
11850 tag,
11851 DW_TAG_value_to_name(tag));
11852 }
11853 }
11854 }
11855 else if (is_cxx_method)
11856 {
11857 // Look at the parent of this DIE and see if is is
11858 // a class or struct and see if this is actually a
11859 // C++ method
11860 Type *class_type = dwarf->ResolveType (dwarf_cu, decl_ctx_die);
11861 if (class_type)
11862 {
11863 if (class_type->GetID() != dwarf->MakeUserID(decl_ctx_die->GetOffset()))
11864 {
11865 // We uniqued the parent class of this function to another class
11866 // so we now need to associate all dies under "decl_ctx_die" to
11867 // DIEs in the DIE for "class_type"...
11868 SymbolFileDWARF *class_symfile = NULL;
11869 DWARFCompileUnitSP class_type_cu_sp;
11870 const DWARFDebugInfoEntry *class_type_die = NULL;
11871
11872 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
11873 if (debug_map_symfile)
11874 {
11875 class_symfile = debug_map_symfile->GetSymbolFileByOSOIndex(SymbolFileDWARFDebugMap::GetOSOIndexFromUserID(class_type->GetID()));
11876 class_type_die = class_symfile->DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp);
11877 }
11878 else
11879 {
11880 class_symfile = dwarf;
11881 class_type_die = dwarf->DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp);
11882 }
11883 if (class_type_die)
11884 {
11885 DWARFDIECollection failures;
11886
11887 CopyUniqueClassMethodTypes (dwarf,
11888 class_symfile,
11889 class_type,
11890 class_type_cu_sp.get(),
11891 class_type_die,
11892 dwarf_cu,
11893 decl_ctx_die,
11894 failures);
11895
11896 // FIXME do something with these failures that's smarter than
11897 // just dropping them on the ground. Unfortunately classes don't
11898 // like having stuff added to them after their definitions are
11899 // complete...
11900
11901 type_ptr = dwarf->m_die_to_type[die];
11902 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
11903 {
11904 type_sp = type_ptr->shared_from_this();
11905 break;
11906 }
11907 }
11908 }
11909
11910 if (specification_die_offset != DW_INVALID_OFFSET)
11911 {
11912 // We have a specification which we are going to base our function
11913 // prototype off of, so we need this type to be completed so that the
11914 // m_die_to_decl_ctx for the method in the specification has a valid
11915 // clang decl context.
Greg Clayton99558cc42015-08-24 23:46:31 +000011916 class_type->GetForwardCompilerType ();
Greg Clayton8b4edba2015-08-14 20:02:05 +000011917 // If we have a specification, then the function type should have been
11918 // made with the specification and not with this die.
11919 DWARFCompileUnitSP spec_cu_sp;
11920 const DWARFDebugInfoEntry* spec_die = dwarf->DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
Greg Clayton99558cc42015-08-24 23:46:31 +000011921 clang::DeclContext *spec_clang_decl_ctx = GetClangDeclContextForDIE (dwarf, dwarf_cu, spec_die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000011922 if (spec_clang_decl_ctx)
11923 {
Greg Clayton6dc8d582015-08-18 22:32:36 +000011924 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000011925 }
11926 else
11927 {
11928 dwarf->GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_specification(0x%8.8x) has no decl\n",
11929 dwarf->MakeUserID(die->GetOffset()),
11930 specification_die_offset);
11931 }
11932 type_handled = true;
11933 }
11934 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
11935 {
11936 // We have a specification which we are going to base our function
11937 // prototype off of, so we need this type to be completed so that the
11938 // m_die_to_decl_ctx for the method in the abstract origin has a valid
11939 // clang decl context.
Greg Clayton99558cc42015-08-24 23:46:31 +000011940 class_type->GetForwardCompilerType ();
Greg Clayton8b4edba2015-08-14 20:02:05 +000011941
11942 DWARFCompileUnitSP abs_cu_sp;
11943 const DWARFDebugInfoEntry* abs_die = dwarf->DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
Greg Clayton99558cc42015-08-24 23:46:31 +000011944 clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE (dwarf, dwarf_cu, abs_die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000011945 if (abs_clang_decl_ctx)
11946 {
Greg Clayton6dc8d582015-08-18 22:32:36 +000011947 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000011948 }
11949 else
11950 {
11951 dwarf->GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_abstract_origin(0x%8.8x) has no decl\n",
11952 dwarf->MakeUserID(die->GetOffset()),
11953 abstract_origin_die_offset);
11954 }
11955 type_handled = true;
11956 }
11957 else
11958 {
Greg Clayton99558cc42015-08-24 23:46:31 +000011959 CompilerType class_opaque_type = class_type->GetForwardCompilerType ();
Greg Clayton8b4edba2015-08-14 20:02:05 +000011960 if (ClangASTContext::IsCXXClassType(class_opaque_type))
11961 {
11962 if (class_opaque_type.IsBeingDefined ())
11963 {
11964 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
11965 // in the DWARF for C++ methods... Default to public for now...
11966 if (accessibility == eAccessNone)
11967 accessibility = eAccessPublic;
11968
11969 if (!is_static && !die->HasChildren())
11970 {
11971 // We have a C++ member function with no children (this pointer!)
11972 // and clang will get mad if we try and make a function that isn't
11973 // well formed in the DWARF, so we will just skip it...
11974 type_handled = true;
11975 }
11976 else
11977 {
11978 clang::CXXMethodDecl *cxx_method_decl;
11979 // REMOVE THE CRASH DESCRIPTION BELOW
11980 Host::SetCrashDescriptionWithFormat ("SymbolFileDWARF::ParseType() is adding a method %s to class %s in DIE 0x%8.8" PRIx64 " from %s",
11981 type_name_cstr,
11982 class_type->GetName().GetCString(),
11983 dwarf->MakeUserID(die->GetOffset()),
11984 dwarf->GetObjectFile()->GetFileSpec().GetPath().c_str());
11985
11986 const bool is_attr_used = false;
11987
11988 cxx_method_decl = AddMethodToCXXRecordType (class_opaque_type.GetOpaqueQualType(),
11989 type_name_cstr,
11990 clang_type,
11991 accessibility,
11992 is_virtual,
11993 is_static,
11994 is_inline,
11995 is_explicit,
11996 is_attr_used,
11997 is_artificial);
11998
11999 type_handled = cxx_method_decl != NULL;
12000
12001 if (type_handled)
12002 {
Greg Clayton6dc8d582015-08-18 22:32:36 +000012003 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000012004
12005 Host::SetCrashDescription (NULL);
12006
12007
12008 ClangASTMetadata metadata;
12009 metadata.SetUserID(dwarf->MakeUserID(die->GetOffset()));
12010
12011 if (!object_pointer_name.empty())
12012 {
12013 metadata.SetObjectPtrName(object_pointer_name.c_str());
12014 if (log)
12015 log->Printf ("Setting object pointer name: %s on method object %p.\n",
12016 object_pointer_name.c_str(),
12017 static_cast<void*>(cxx_method_decl));
12018 }
12019 SetMetadata (cxx_method_decl, metadata);
12020 }
12021 else
12022 {
12023 ignore_containing_context = true;
12024 }
12025 }
12026 }
12027 else
12028 {
12029 // We were asked to parse the type for a method in a class, yet the
12030 // class hasn't been asked to complete itself through the
12031 // clang::ExternalASTSource protocol, so we need to just have the
12032 // class complete itself and do things the right way, then our
12033 // DIE should then have an entry in the dwarf->m_die_to_type map. First
12034 // we need to modify the dwarf->m_die_to_type so it doesn't think we are
12035 // trying to parse this DIE anymore...
12036 dwarf->m_die_to_type[die] = NULL;
12037
12038 // Now we get the full type to force our class type to complete itself
12039 // using the clang::ExternalASTSource protocol which will parse all
12040 // base classes and all methods (including the method for this DIE).
Greg Clayton99558cc42015-08-24 23:46:31 +000012041 class_type->GetFullCompilerType ();
Greg Clayton8b4edba2015-08-14 20:02:05 +000012042
12043 // The type for this DIE should have been filled in the function call above
12044 type_ptr = dwarf->m_die_to_type[die];
12045 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
12046 {
12047 type_sp = type_ptr->shared_from_this();
12048 break;
12049 }
12050
12051 // FIXME This is fixing some even uglier behavior but we really need to
12052 // uniq the methods of each class as well as the class itself.
12053 // <rdar://problem/11240464>
12054 type_handled = true;
12055 }
12056 }
12057 }
12058 }
12059 }
12060 }
12061
12062 if (!type_handled)
12063 {
12064 // We just have a function that isn't part of a class
12065 clang::FunctionDecl *function_decl = CreateFunctionDeclaration (ignore_containing_context ? GetTranslationUnitDecl() : containing_decl_ctx,
12066 type_name_cstr,
12067 clang_type,
12068 storage,
12069 is_inline);
12070
12071 // if (template_param_infos.GetSize() > 0)
12072 // {
12073 // clang::FunctionTemplateDecl *func_template_decl = CreateFunctionTemplateDecl (containing_decl_ctx,
12074 // function_decl,
12075 // type_name_cstr,
12076 // template_param_infos);
12077 //
12078 // CreateFunctionTemplateSpecializationInfo (function_decl,
12079 // func_template_decl,
12080 // template_param_infos);
12081 // }
12082 // Add the decl to our DIE to decl context map
12083 assert (function_decl);
Greg Clayton6dc8d582015-08-18 22:32:36 +000012084 LinkDeclContextToDIE(function_decl, die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000012085 if (!function_param_decls.empty())
12086 SetFunctionParameters (function_decl,
12087 &function_param_decls.front(),
12088 function_param_decls.size());
12089
12090 ClangASTMetadata metadata;
12091 metadata.SetUserID(dwarf->MakeUserID(die->GetOffset()));
12092
12093 if (!object_pointer_name.empty())
12094 {
12095 metadata.SetObjectPtrName(object_pointer_name.c_str());
12096 if (log)
12097 log->Printf ("Setting object pointer name: %s on function object %p.",
12098 object_pointer_name.c_str(),
12099 static_cast<void*>(function_decl));
12100 }
12101 SetMetadata (function_decl, metadata);
12102 }
12103 }
12104 type_sp.reset( new Type (dwarf->MakeUserID(die->GetOffset()),
12105 dwarf,
12106 type_name_const_str,
12107 0,
12108 NULL,
12109 LLDB_INVALID_UID,
12110 Type::eEncodingIsUID,
12111 &decl,
12112 clang_type,
12113 Type::eResolveStateFull));
12114 assert(type_sp.get());
12115 }
12116 break;
12117
12118 case DW_TAG_array_type:
12119 {
12120 // Set a bit that lets us know that we are currently parsing this
12121 dwarf->m_die_to_type[die] = DIE_IS_BEING_PARSED;
12122
12123 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
12124 int64_t first_index = 0;
12125 uint32_t byte_stride = 0;
12126 uint32_t bit_stride = 0;
12127 bool is_vector = false;
12128 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, NULL, attributes);
12129
12130 if (num_attributes > 0)
12131 {
12132 uint32_t i;
12133 for (i=0; i<num_attributes; ++i)
12134 {
12135 attr = attributes.AttributeAtIndex(i);
12136 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
12137 {
12138 switch (attr)
12139 {
12140 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
12141 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
12142 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
12143 case DW_AT_name:
12144 type_name_cstr = form_value.AsCString(&dwarf->get_debug_str_data());
12145 type_name_const_str.SetCString(type_name_cstr);
12146 break;
12147
12148 case DW_AT_type: type_die_offset = form_value.Reference(); break;
12149 case DW_AT_byte_size: break; // byte_size = form_value.Unsigned(); break;
12150 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
12151 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
12152 case DW_AT_GNU_vector: is_vector = form_value.Boolean(); break;
12153 case DW_AT_accessibility: break; // accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
12154 case DW_AT_declaration: break; // is_forward_declaration = form_value.Boolean(); break;
12155 case DW_AT_allocated:
12156 case DW_AT_associated:
12157 case DW_AT_data_location:
12158 case DW_AT_description:
12159 case DW_AT_ordering:
12160 case DW_AT_start_scope:
12161 case DW_AT_visibility:
12162 case DW_AT_specification:
12163 case DW_AT_abstract_origin:
12164 case DW_AT_sibling:
12165 break;
12166 }
12167 }
12168 }
12169
12170 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", dwarf->MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
12171
12172 Type *element_type = dwarf->ResolveTypeUID(type_die_offset);
12173
12174 if (element_type)
12175 {
12176 std::vector<uint64_t> element_orders;
12177 ParseChildArrayInfo(sc, dwarf, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
12178 if (byte_stride == 0 && bit_stride == 0)
12179 byte_stride = element_type->GetByteSize();
Greg Clayton99558cc42015-08-24 23:46:31 +000012180 CompilerType array_element_type = element_type->GetForwardCompilerType ();
Greg Clayton8b4edba2015-08-14 20:02:05 +000012181 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
12182 if (element_orders.size() > 0)
12183 {
12184 uint64_t num_elements = 0;
12185 std::vector<uint64_t>::const_reverse_iterator pos;
12186 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
12187 for (pos = element_orders.rbegin(); pos != end; ++pos)
12188 {
12189 num_elements = *pos;
12190 clang_type = CreateArrayType (array_element_type,
12191 num_elements,
12192 is_vector);
12193 array_element_type = clang_type;
12194 array_element_bit_stride = num_elements ?
12195 array_element_bit_stride * num_elements :
12196 array_element_bit_stride;
12197 }
12198 }
12199 else
12200 {
12201 clang_type = CreateArrayType (array_element_type, 0, is_vector);
12202 }
12203 ConstString empty_name;
12204 type_sp.reset( new Type (dwarf->MakeUserID(die->GetOffset()),
12205 dwarf,
12206 empty_name,
12207 array_element_bit_stride / 8,
12208 NULL,
12209 type_die_offset,
12210 Type::eEncodingIsUID,
12211 &decl,
12212 clang_type,
12213 Type::eResolveStateFull));
12214 type_sp->SetEncodingType (element_type);
12215 }
12216 }
12217 }
12218 break;
12219
12220 case DW_TAG_ptr_to_member_type:
12221 {
12222 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
12223 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
12224
12225 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, NULL, attributes);
12226
12227 if (num_attributes > 0) {
12228 uint32_t i;
12229 for (i=0; i<num_attributes; ++i)
12230 {
12231 attr = attributes.AttributeAtIndex(i);
12232 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
12233 {
12234 switch (attr)
12235 {
12236 case DW_AT_type:
12237 type_die_offset = form_value.Reference(); break;
12238 case DW_AT_containing_type:
12239 containing_type_die_offset = form_value.Reference(); break;
12240 }
12241 }
12242 }
12243
12244 Type *pointee_type = dwarf->ResolveTypeUID(type_die_offset);
12245 Type *class_type = dwarf->ResolveTypeUID(containing_type_die_offset);
12246
Greg Clayton99558cc42015-08-24 23:46:31 +000012247 CompilerType pointee_clang_type = pointee_type->GetForwardCompilerType ();
12248 CompilerType class_clang_type = class_type->GetLayoutCompilerType ();
Greg Clayton8b4edba2015-08-14 20:02:05 +000012249
12250 clang_type = ClangASTContext::CreateMemberPointerType(pointee_clang_type, class_clang_type);
12251
12252 byte_size = clang_type.GetByteSize(nullptr);
12253
12254 type_sp.reset( new Type (dwarf->MakeUserID(die->GetOffset()),
12255 dwarf,
12256 type_name_const_str,
12257 byte_size,
12258 NULL,
12259 LLDB_INVALID_UID,
12260 Type::eEncodingIsUID,
12261 NULL,
12262 clang_type,
12263 Type::eResolveStateForward));
12264 }
12265
12266 break;
12267 }
12268 default:
12269 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",
12270 die->GetOffset(),
12271 tag,
12272 DW_TAG_value_to_name(tag));
12273 break;
12274 }
12275
12276 if (type_sp.get())
12277 {
12278 const DWARFDebugInfoEntry *sc_parent_die = SymbolFileDWARF::GetParentSymbolContextDIE(die);
12279 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
12280
12281 SymbolContextScope * symbol_context_scope = NULL;
12282 if (sc_parent_tag == DW_TAG_compile_unit)
12283 {
12284 symbol_context_scope = sc.comp_unit;
12285 }
12286 else if (sc.function != NULL && sc_parent_die)
12287 {
12288 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(dwarf->MakeUserID(sc_parent_die->GetOffset()));
12289 if (symbol_context_scope == NULL)
12290 symbol_context_scope = sc.function;
12291 }
12292
12293 if (symbol_context_scope != NULL)
12294 {
12295 type_sp->SetSymbolContextScope(symbol_context_scope);
12296 }
12297
12298 // We are ready to put this type into the uniqued list up at the module level
12299 type_list->Insert (type_sp);
12300
12301 dwarf->m_die_to_type[die] = type_sp.get();
12302 }
12303 }
12304 else if (type_ptr != DIE_IS_BEING_PARSED)
12305 {
12306 type_sp = type_ptr->shared_from_this();
12307 }
12308 }
12309 return type_sp;
12310}
12311
12312
12313bool
12314ClangASTContext::CopyUniqueClassMethodTypes (SymbolFileDWARF *dst_symfile,
12315 SymbolFileDWARF *src_symfile,
Yaron Kerenfe16dea2015-08-16 19:40:40 +000012316 lldb_private::Type *class_type,
Greg Clayton8b4edba2015-08-14 20:02:05 +000012317 DWARFCompileUnit* src_cu,
12318 const DWARFDebugInfoEntry *src_class_die,
12319 DWARFCompileUnit* dst_cu,
12320 const DWARFDebugInfoEntry *dst_class_die,
12321 DWARFDIECollection &failures)
12322{
12323 if (!class_type || !src_cu || !src_class_die || !dst_cu || !dst_class_die)
12324 return false;
12325 if (src_class_die->Tag() != dst_class_die->Tag())
12326 return false;
12327
12328 // We need to complete the class type so we can get all of the method types
12329 // parsed so we can then unique those types to their equivalent counterparts
12330 // in "dst_cu" and "dst_class_die"
Greg Clayton99558cc42015-08-24 23:46:31 +000012331 class_type->GetFullCompilerType ();
Greg Clayton8b4edba2015-08-14 20:02:05 +000012332
12333 const DWARFDebugInfoEntry *src_die;
12334 const DWARFDebugInfoEntry *dst_die;
12335 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die;
12336 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die;
12337 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die_artificial;
12338 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die_artificial;
12339 for (src_die = src_class_die->GetFirstChild(); src_die != NULL; src_die = src_die->GetSibling())
12340 {
12341 if (src_die->Tag() == DW_TAG_subprogram)
12342 {
12343 // Make sure this is a declaration and not a concrete instance by looking
12344 // for DW_AT_declaration set to 1. Sometimes concrete function instances
12345 // are placed inside the class definitions and shouldn't be included in
12346 // the list of things are are tracking here.
12347 if (src_die->GetAttributeValueAsUnsigned(src_symfile, src_cu, DW_AT_declaration, 0) == 1)
12348 {
12349 const char *src_name = src_die->GetMangledName (src_symfile, src_cu);
12350 if (src_name)
12351 {
12352 ConstString src_const_name(src_name);
12353 if (src_die->GetAttributeValueAsUnsigned(src_symfile, src_cu, DW_AT_artificial, 0))
12354 src_name_to_die_artificial.Append(src_const_name.GetCString(), src_die);
12355 else
12356 src_name_to_die.Append(src_const_name.GetCString(), src_die);
12357 }
12358 }
12359 }
12360 }
12361 for (dst_die = dst_class_die->GetFirstChild(); dst_die != NULL; dst_die = dst_die->GetSibling())
12362 {
12363 if (dst_die->Tag() == DW_TAG_subprogram)
12364 {
12365 // Make sure this is a declaration and not a concrete instance by looking
12366 // for DW_AT_declaration set to 1. Sometimes concrete function instances
12367 // are placed inside the class definitions and shouldn't be included in
12368 // the list of things are are tracking here.
12369 if (dst_die->GetAttributeValueAsUnsigned(dst_symfile, dst_cu, DW_AT_declaration, 0) == 1)
12370 {
12371 const char *dst_name = dst_die->GetMangledName (dst_symfile, dst_cu);
12372 if (dst_name)
12373 {
12374 ConstString dst_const_name(dst_name);
12375 if (dst_die->GetAttributeValueAsUnsigned(dst_symfile, dst_cu, DW_AT_artificial, 0))
12376 dst_name_to_die_artificial.Append(dst_const_name.GetCString(), dst_die);
12377 else
12378 dst_name_to_die.Append(dst_const_name.GetCString(), dst_die);
12379 }
12380 }
12381 }
12382 }
12383 const uint32_t src_size = src_name_to_die.GetSize ();
12384 const uint32_t dst_size = dst_name_to_die.GetSize ();
12385 Log *log = nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_TYPE_COMPLETION));
12386
12387 // Is everything kosher so we can go through the members at top speed?
12388 bool fast_path = true;
12389
12390 if (src_size != dst_size)
12391 {
12392 if (src_size != 0 && dst_size != 0)
12393 {
12394 if (log)
12395 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)",
12396 src_class_die->GetOffset(),
12397 dst_class_die->GetOffset(),
12398 src_size,
12399 dst_size);
12400 }
12401
12402 fast_path = false;
12403 }
12404
12405 uint32_t idx;
12406
12407 if (fast_path)
12408 {
12409 for (idx = 0; idx < src_size; ++idx)
12410 {
12411 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
12412 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
12413
12414 if (src_die->Tag() != dst_die->Tag())
12415 {
12416 if (log)
12417 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)",
12418 src_class_die->GetOffset(),
12419 dst_class_die->GetOffset(),
12420 src_die->GetOffset(),
12421 DW_TAG_value_to_name(src_die->Tag()),
12422 dst_die->GetOffset(),
12423 DW_TAG_value_to_name(src_die->Tag()));
12424 fast_path = false;
12425 }
12426
12427 const char *src_name = src_die->GetMangledName (src_symfile, src_cu);
12428 const char *dst_name = dst_die->GetMangledName (dst_symfile, dst_cu);
12429
12430 // Make sure the names match
12431 if (src_name == dst_name || (strcmp (src_name, dst_name) == 0))
12432 continue;
12433
12434 if (log)
12435 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)",
12436 src_class_die->GetOffset(),
12437 dst_class_die->GetOffset(),
12438 src_die->GetOffset(),
12439 src_name,
12440 dst_die->GetOffset(),
12441 dst_name);
12442
12443 fast_path = false;
12444 }
12445 }
12446
12447 // Now do the work of linking the DeclContexts and Types.
12448 if (fast_path)
12449 {
12450 // We can do this quickly. Just run across the tables index-for-index since
12451 // we know each node has matching names and tags.
12452 for (idx = 0; idx < src_size; ++idx)
12453 {
12454 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
12455 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
12456
Greg Clayton6dc8d582015-08-18 22:32:36 +000012457 clang::DeclContext *src_decl_ctx = src_symfile->GetClangASTContext().m_die_to_decl_ctx[src_die];
Greg Clayton8b4edba2015-08-14 20:02:05 +000012458 if (src_decl_ctx)
12459 {
12460 if (log)
12461 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
12462 static_cast<void*>(src_decl_ctx),
12463 src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton6dc8d582015-08-18 22:32:36 +000012464 dst_symfile->GetClangASTContext().LinkDeclContextToDIE (src_decl_ctx, dst_die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000012465 }
12466 else
12467 {
12468 if (log)
12469 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found",
12470 src_die->GetOffset(), dst_die->GetOffset());
12471 }
12472
12473 Type *src_child_type = dst_symfile->m_die_to_type[src_die];
12474 if (src_child_type)
12475 {
12476 if (log)
12477 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
12478 static_cast<void*>(src_child_type),
12479 src_child_type->GetID(),
12480 src_die->GetOffset(), dst_die->GetOffset());
12481 dst_symfile->m_die_to_type[dst_die] = src_child_type;
12482 }
12483 else
12484 {
12485 if (log)
12486 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());
12487 }
12488 }
12489 }
12490 else
12491 {
12492 // We must do this slowly. For each member of the destination, look
12493 // up a member in the source with the same name, check its tag, and
12494 // unique them if everything matches up. Report failures.
12495
12496 if (!src_name_to_die.IsEmpty() && !dst_name_to_die.IsEmpty())
12497 {
12498 src_name_to_die.Sort();
12499
12500 for (idx = 0; idx < dst_size; ++idx)
12501 {
12502 const char *dst_name = dst_name_to_die.GetCStringAtIndex(idx);
12503 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
12504 src_die = src_name_to_die.Find(dst_name, NULL);
12505
12506 if (src_die && (src_die->Tag() == dst_die->Tag()))
12507 {
Greg Clayton6dc8d582015-08-18 22:32:36 +000012508 clang::DeclContext *src_decl_ctx = src_symfile->GetClangASTContext().m_die_to_decl_ctx[src_die];
Greg Clayton8b4edba2015-08-14 20:02:05 +000012509 if (src_decl_ctx)
12510 {
12511 if (log)
12512 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
12513 static_cast<void*>(src_decl_ctx),
12514 src_die->GetOffset(),
12515 dst_die->GetOffset());
Greg Clayton6dc8d582015-08-18 22:32:36 +000012516 dst_symfile->GetClangASTContext().LinkDeclContextToDIE (src_decl_ctx, dst_die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000012517 }
12518 else
12519 {
12520 if (log)
12521 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());
12522 }
12523
12524 Type *src_child_type = dst_symfile->m_die_to_type[src_die];
12525 if (src_child_type)
12526 {
12527 if (log)
12528 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
12529 static_cast<void*>(src_child_type),
12530 src_child_type->GetID(),
12531 src_die->GetOffset(),
12532 dst_die->GetOffset());
12533 dst_symfile->m_die_to_type[dst_die] = src_child_type;
12534 }
12535 else
12536 {
12537 if (log)
12538 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());
12539 }
12540 }
12541 else
12542 {
12543 if (log)
12544 log->Printf ("warning: couldn't find a match for 0x%8.8x", dst_die->GetOffset());
12545
12546 failures.Append(dst_die);
12547 }
12548 }
12549 }
12550 }
12551
12552 const uint32_t src_size_artificial = src_name_to_die_artificial.GetSize ();
12553 const uint32_t dst_size_artificial = dst_name_to_die_artificial.GetSize ();
12554
12555 UniqueCStringMap<const DWARFDebugInfoEntry *> name_to_die_artificial_not_in_src;
12556
12557 if (src_size_artificial && dst_size_artificial)
12558 {
12559 dst_name_to_die_artificial.Sort();
12560
12561 for (idx = 0; idx < src_size_artificial; ++idx)
12562 {
12563 const char *src_name_artificial = src_name_to_die_artificial.GetCStringAtIndex(idx);
12564 src_die = src_name_to_die_artificial.GetValueAtIndexUnchecked (idx);
12565 dst_die = dst_name_to_die_artificial.Find(src_name_artificial, NULL);
12566
12567 if (dst_die)
12568 {
12569 // Both classes have the artificial types, link them
Greg Clayton6dc8d582015-08-18 22:32:36 +000012570 clang::DeclContext *src_decl_ctx = dst_symfile->GetClangASTContext().m_die_to_decl_ctx[src_die];
Greg Clayton8b4edba2015-08-14 20:02:05 +000012571 if (src_decl_ctx)
12572 {
12573 if (log)
12574 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
12575 static_cast<void*>(src_decl_ctx),
12576 src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton6dc8d582015-08-18 22:32:36 +000012577 dst_symfile->GetClangASTContext().LinkDeclContextToDIE (src_decl_ctx, dst_die);
Greg Clayton8b4edba2015-08-14 20:02:05 +000012578 }
12579 else
12580 {
12581 if (log)
12582 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());
12583 }
12584
12585 Type *src_child_type = dst_symfile->m_die_to_type[src_die];
12586 if (src_child_type)
12587 {
12588 if (log)
12589 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
12590 static_cast<void*>(src_child_type),
12591 src_child_type->GetID(),
12592 src_die->GetOffset(), dst_die->GetOffset());
12593 dst_symfile->m_die_to_type[dst_die] = src_child_type;
12594 }
12595 else
12596 {
12597 if (log)
12598 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());
12599 }
12600 }
12601 }
12602 }
12603
12604 if (dst_size_artificial)
12605 {
12606 for (idx = 0; idx < dst_size_artificial; ++idx)
12607 {
12608 const char *dst_name_artificial = dst_name_to_die_artificial.GetCStringAtIndex(idx);
12609 dst_die = dst_name_to_die_artificial.GetValueAtIndexUnchecked (idx);
12610 if (log)
12611 log->Printf ("warning: need to create artificial method for 0x%8.8x for method '%s'", dst_die->GetOffset(), dst_name_artificial);
12612
12613 failures.Append(dst_die);
12614 }
12615 }
12616
12617 return (failures.Size() != 0);
12618}
12619