blob: 4e50793f39f4fe5ee44d7aba2e87547fbd9c27c0 [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
14#include <string>
15
16// Other libraries and framework includes
Greg Clayton6beaaa62011-01-17 03:46:26 +000017
18// Clang headers like to use NDEBUG inside of them to enable/disable debug
19// releated features using "#ifndef NDEBUG" preprocessor blocks to do one thing
20// or another. This is bad because it means that if clang was built in release
21// mode, it assumes that you are building in release mode which is not always
22// the case. You can end up with functions that are defined as empty in header
23// files when NDEBUG is not defined, and this can cause link errors with the
24// clang .a files that you have since you might be missing functions in the .a
25// file. So we have to define NDEBUG when including clang headers to avoid any
26// mismatches. This is covered by rdar://problem/8691220
27
Sean Callanan3b1d4f62011-10-26 17:46:51 +000028#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000029#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000030#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000031// Need to include assert.h so it is as clang would expect it to be (disabled)
32#include <assert.h>
33#endif
34
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "clang/AST/ASTContext.h"
36#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000037#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000039#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000040#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "clang/AST/RecordLayout.h"
42#include "clang/AST/Type.h"
43#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000044#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000046#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "clang/Basic/SourceManager.h"
48#include "clang/Basic/TargetInfo.h"
49#include "clang/Basic/TargetOptions.h"
50#include "clang/Frontend/FrontendOptions.h"
51#include "clang/Frontend/LangStandard.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000052
53#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000054#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000055#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
56// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
57#include <assert.h>
58#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059
Greg Clayton514487e2011-02-15 21:59:32 +000060#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061#include "lldb/Core/dwarf.h"
Greg Clayton73b472d2010-10-27 03:32:59 +000062#include "lldb/Core/Flags.h"
Sean Callananfb8b7092010-10-28 18:19:36 +000063#include "lldb/Core/Log.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000064#include "lldb/Core/RegularExpression.h"
65#include "lldb/Expression/ASTDumper.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000066#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000067#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000068#include "lldb/Target/ExecutionContext.h"
69#include "lldb/Target/Process.h"
70#include "lldb/Target/ObjCLanguageRuntime.h"
71
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072
Eli Friedman932197d2010-06-13 19:06:42 +000073#include <stdio.h>
74
Greg Claytonc86103d2010-08-05 01:57:25 +000075using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076using namespace lldb_private;
77using namespace llvm;
78using namespace clang;
79
Greg Clayton6beaaa62011-01-17 03:46:26 +000080
81static bool
Enrico Granata86027e92012-03-24 01:11:14 +000082GetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion = true)
Greg Clayton6beaaa62011-01-17 03:46:26 +000083{
84 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
85 switch (type_class)
86 {
Sean Callanan5b26f272012-02-04 08:49:35 +000087 case clang::Type::ConstantArray:
88 {
89 const clang::ArrayType *array_type = dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
90
91 if (array_type)
Enrico Granata86027e92012-03-24 01:11:14 +000092 return GetCompleteQualType (ast, array_type->getElementType(), allow_completion);
Sean Callanan5b26f272012-02-04 08:49:35 +000093 }
94 break;
95
Greg Clayton6beaaa62011-01-17 03:46:26 +000096 case clang::Type::Record:
97 case clang::Type::Enum:
98 {
Sean Callanan78e37602011-01-27 04:42:51 +000099 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +0000100 if (tag_type)
101 {
102 clang::TagDecl *tag_decl = tag_type->getDecl();
103 if (tag_decl)
104 {
Greg Clayton219cf312012-03-30 00:51:13 +0000105 if (tag_decl->isCompleteDefinition())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000106 return true;
Enrico Granata86027e92012-03-24 01:11:14 +0000107
108 if (!allow_completion)
109 return false;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000110
111 if (tag_decl->hasExternalLexicalStorage())
112 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000113 if (ast)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000114 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000115 ExternalASTSource *external_ast_source = ast->getExternalSource();
116 if (external_ast_source)
117 {
118 external_ast_source->CompleteType(tag_decl);
119 return !tag_type->isIncompleteType();
120 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000121 }
122 }
123 return false;
124 }
125 }
126
127 }
128 break;
129
130 case clang::Type::ObjCObject:
131 case clang::Type::ObjCInterface:
132 {
Sean Callanan78e37602011-01-27 04:42:51 +0000133 const clang::ObjCObjectType *objc_class_type = dyn_cast<clang::ObjCObjectType>(qual_type);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000134 if (objc_class_type)
135 {
136 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
137 // We currently can't complete objective C types through the newly added ASTContext
138 // because it only supports TagDecl objects right now...
Enrico Granata9dd75c82011-07-15 23:30:15 +0000139 if (class_interface_decl)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000140 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000141 if (class_interface_decl->getDefinition())
142 return true;
143
Enrico Granata86027e92012-03-24 01:11:14 +0000144 if (!allow_completion)
145 return false;
Greg Clayton20533982012-03-30 23:48:28 +0000146
Sean Callanan5b26f272012-02-04 08:49:35 +0000147 if (class_interface_decl->hasExternalLexicalStorage())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000148 {
Enrico Granata0a3958e2011-07-02 00:25:22 +0000149 if (ast)
Greg Clayton007d5be2011-05-30 00:49:24 +0000150 {
Enrico Granata0a3958e2011-07-02 00:25:22 +0000151 ExternalASTSource *external_ast_source = ast->getExternalSource();
152 if (external_ast_source)
153 {
154 external_ast_source->CompleteType (class_interface_decl);
Sean Callanan5b26f272012-02-04 08:49:35 +0000155 return !objc_class_type->isIncompleteType();
Enrico Granata0a3958e2011-07-02 00:25:22 +0000156 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000157 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000158 }
Enrico Granata0a3958e2011-07-02 00:25:22 +0000159 return false;
Sean Callanan5b26f272012-02-04 08:49:35 +0000160 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000161 }
162 }
163 break;
164
165 case clang::Type::Typedef:
Enrico Granata86027e92012-03-24 01:11:14 +0000166 return GetCompleteQualType (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType(), allow_completion);
Sean Callanan912855f2011-08-11 23:56:13 +0000167
168 case clang::Type::Elaborated:
Enrico Granata86027e92012-03-24 01:11:14 +0000169 return GetCompleteQualType (ast, cast<ElaboratedType>(qual_type)->getNamedType(), allow_completion);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000170
171 default:
172 break;
173 }
174
175 return true;
176}
177
Greg Clayton8cf05932010-07-22 18:30:50 +0000178static AccessSpecifier
Greg Claytonc86103d2010-08-05 01:57:25 +0000179ConvertAccessTypeToAccessSpecifier (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000180{
181 switch (access)
182 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000183 default: break;
184 case eAccessNone: return AS_none;
185 case eAccessPublic: return AS_public;
186 case eAccessPrivate: return AS_private;
187 case eAccessProtected: return AS_protected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000188 }
189 return AS_none;
190}
191
192static ObjCIvarDecl::AccessControl
Greg Claytonc86103d2010-08-05 01:57:25 +0000193ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000194{
195 switch (access)
196 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000197 case eAccessNone: return ObjCIvarDecl::None;
198 case eAccessPublic: return ObjCIvarDecl::Public;
199 case eAccessPrivate: return ObjCIvarDecl::Private;
200 case eAccessProtected: return ObjCIvarDecl::Protected;
201 case eAccessPackage: return ObjCIvarDecl::Package;
Greg Clayton8cf05932010-07-22 18:30:50 +0000202 }
203 return ObjCIvarDecl::None;
204}
205
206
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207static void
208ParseLangArgs
209(
210 LangOptions &Opts,
Greg Clayton94e5d782010-06-13 17:34:29 +0000211 InputKind IK
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212)
213{
214 // FIXME: Cleanup per-file based stuff.
215
216 // Set some properties which depend soley on the input kind; it would be nice
217 // to move these to the language standard, and have the driver resolve the
218 // input kind + language standard.
Greg Clayton94e5d782010-06-13 17:34:29 +0000219 if (IK == IK_Asm) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220 Opts.AsmPreprocessor = 1;
Greg Clayton94e5d782010-06-13 17:34:29 +0000221 } else if (IK == IK_ObjC ||
222 IK == IK_ObjCXX ||
223 IK == IK_PreprocessedObjC ||
224 IK == IK_PreprocessedObjCXX) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225 Opts.ObjC1 = Opts.ObjC2 = 1;
226 }
227
228 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
229
230 if (LangStd == LangStandard::lang_unspecified) {
231 // Based on the base language, pick one.
232 switch (IK) {
Greg Clayton94e5d782010-06-13 17:34:29 +0000233 case IK_None:
234 case IK_AST:
Sean Callananfb0b7582011-03-15 00:17:19 +0000235 case IK_LLVM_IR:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000236 assert (!"Invalid input kind!");
Greg Clayton94e5d782010-06-13 17:34:29 +0000237 case IK_OpenCL:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238 LangStd = LangStandard::lang_opencl;
239 break;
Sean Callananfb0b7582011-03-15 00:17:19 +0000240 case IK_CUDA:
241 LangStd = LangStandard::lang_cuda;
242 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000243 case IK_Asm:
244 case IK_C:
245 case IK_PreprocessedC:
246 case IK_ObjC:
247 case IK_PreprocessedObjC:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248 LangStd = LangStandard::lang_gnu99;
249 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000250 case IK_CXX:
251 case IK_PreprocessedCXX:
252 case IK_ObjCXX:
253 case IK_PreprocessedObjCXX:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254 LangStd = LangStandard::lang_gnucxx98;
255 break;
256 }
257 }
258
259 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
Filipe Cabecinhase818ca22012-11-12 21:26:32 +0000260 Opts.LineComment = Std.hasLineComments();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261 Opts.C99 = Std.isC99();
262 Opts.CPlusPlus = Std.isCPlusPlus();
Chandler Carruth38336a12013-01-02 12:55:00 +0000263 Opts.CPlusPlus11 = Std.isCPlusPlus11();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264 Opts.Digraphs = Std.hasDigraphs();
265 Opts.GNUMode = Std.isGNUMode();
266 Opts.GNUInline = !Std.isC99();
267 Opts.HexFloats = Std.hasHexFloats();
268 Opts.ImplicitInt = Std.hasImplicitInt();
Enrico Granatac921e342013-01-10 02:37:22 +0000269
270 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271
272 // OpenCL has some additional defaults.
273 if (LangStd == LangStandard::lang_opencl) {
274 Opts.OpenCL = 1;
275 Opts.AltiVec = 1;
276 Opts.CXXOperatorNames = 1;
277 Opts.LaxVectorConversions = 1;
278 }
279
280 // OpenCL and C++ both have bool, true, false keywords.
281 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
282
283// if (Opts.CPlusPlus)
284// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
285//
286// if (Args.hasArg(OPT_fobjc_gc_only))
287// Opts.setGCMode(LangOptions::GCOnly);
288// else if (Args.hasArg(OPT_fobjc_gc))
289// Opts.setGCMode(LangOptions::HybridGC);
290//
291// if (Args.hasArg(OPT_print_ivar_layout))
292// Opts.ObjCGCBitmapPrint = 1;
293//
294// if (Args.hasArg(OPT_faltivec))
295// Opts.AltiVec = 1;
296//
297// if (Args.hasArg(OPT_pthread))
298// Opts.POSIXThreads = 1;
299//
300// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
301// "default");
302// if (Vis == "default")
Sean Callanan37f76e52013-02-19 19:16:37 +0000303 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304// else if (Vis == "hidden")
305// Opts.setVisibilityMode(LangOptions::Hidden);
306// else if (Vis == "protected")
307// Opts.setVisibilityMode(LangOptions::Protected);
308// else
309// Diags.Report(diag::err_drv_invalid_value)
310// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
311
312// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
313
314 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
315 // is specified, or -std is set to a conforming mode.
316 Opts.Trigraphs = !Opts.GNUMode;
317// if (Args.hasArg(OPT_trigraphs))
318// Opts.Trigraphs = 1;
319//
320// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
321// OPT_fno_dollars_in_identifiers,
322// !Opts.AsmPreprocessor);
323// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
324// Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
325// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
326// if (Args.hasArg(OPT_fno_lax_vector_conversions))
327// Opts.LaxVectorConversions = 0;
328// Opts.Exceptions = Args.hasArg(OPT_fexceptions);
329// Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
330// Opts.Blocks = Args.hasArg(OPT_fblocks);
331// Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char);
332// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
333// Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
334// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
335// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
336// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
337// Opts.AccessControl = Args.hasArg(OPT_faccess_control);
338// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
339// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
340// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
341// Diags);
342// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
343// Opts.ObjCConstantStringClass = getLastArgValue(Args,
344// OPT_fconstant_string_class);
345// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
346// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
347// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
348// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
349// Opts.Static = Args.hasArg(OPT_static_define);
350 Opts.OptimizeSize = 0;
351
352 // FIXME: Eliminate this dependency.
353// unsigned Opt =
354// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
355// Opts.Optimize = Opt != 0;
356 unsigned Opt = 0;
357
358 // This is the __NO_INLINE__ define, which just depends on things like the
359 // optimization level and -fno-inline, not actually whether the backend has
360 // inlining enabled.
361 //
362 // FIXME: This is affected by other options (-fno-inline).
Sean Callanan3d654b32012-09-24 22:25:51 +0000363 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364
365// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
366// switch (SSP) {
367// default:
368// Diags.Report(diag::err_drv_invalid_value)
369// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
370// break;
371// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
372// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
373// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
374// }
375}
376
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377
Greg Clayton6beaaa62011-01-17 03:46:26 +0000378ClangASTContext::ClangASTContext (const char *target_triple) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379 m_target_triple(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000380 m_ast_ap(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381 m_language_options_ap(),
382 m_source_manager_ap(),
Sean Callanan880e6802011-10-07 23:18:13 +0000383 m_diagnostics_engine_ap(),
Sean Callananc5069ad2012-10-17 22:11:14 +0000384 m_target_options_rp(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385 m_target_info_ap(),
386 m_identifier_table_ap(),
387 m_selector_table_ap(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000388 m_builtins_ap(),
389 m_callback_tag_decl (NULL),
390 m_callback_objc_decl (NULL),
391 m_callback_baton (NULL)
392
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393{
394 if (target_triple && target_triple[0])
Greg Clayton880cbb02011-07-30 01:26:02 +0000395 SetTargetTriple (target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396}
397
398//----------------------------------------------------------------------
399// Destructor
400//----------------------------------------------------------------------
401ClangASTContext::~ClangASTContext()
402{
403 m_builtins_ap.reset();
404 m_selector_table_ap.reset();
405 m_identifier_table_ap.reset();
406 m_target_info_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000407 m_target_options_rp.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000408 m_diagnostics_engine_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409 m_source_manager_ap.reset();
410 m_language_options_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000411 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412}
413
414
415void
416ClangASTContext::Clear()
417{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000418 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419 m_language_options_ap.reset();
420 m_source_manager_ap.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000421 m_diagnostics_engine_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000422 m_target_options_rp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423 m_target_info_ap.reset();
424 m_identifier_table_ap.reset();
425 m_selector_table_ap.reset();
426 m_builtins_ap.reset();
427}
428
429const char *
430ClangASTContext::GetTargetTriple ()
431{
432 return m_target_triple.c_str();
433}
434
435void
436ClangASTContext::SetTargetTriple (const char *target_triple)
437{
438 Clear();
439 m_target_triple.assign(target_triple);
440}
441
Greg Clayton514487e2011-02-15 21:59:32 +0000442void
443ClangASTContext::SetArchitecture (const ArchSpec &arch)
444{
Greg Clayton880cbb02011-07-30 01:26:02 +0000445 SetTargetTriple(arch.GetTriple().str().c_str());
Greg Clayton514487e2011-02-15 21:59:32 +0000446}
447
Greg Clayton6beaaa62011-01-17 03:46:26 +0000448bool
449ClangASTContext::HasExternalSource ()
450{
451 ASTContext *ast = getASTContext();
452 if (ast)
453 return ast->getExternalSource () != NULL;
454 return false;
455}
456
457void
458ClangASTContext::SetExternalSource (llvm::OwningPtr<ExternalASTSource> &ast_source_ap)
459{
460 ASTContext *ast = getASTContext();
461 if (ast)
462 {
463 ast->setExternalSource (ast_source_ap);
464 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
465 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
466 }
467}
468
469void
470ClangASTContext::RemoveExternalSource ()
471{
472 ASTContext *ast = getASTContext();
473
474 if (ast)
475 {
476 llvm::OwningPtr<ExternalASTSource> empty_ast_source_ap;
477 ast->setExternalSource (empty_ast_source_ap);
478 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
479 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
480 }
481}
482
483
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484
485ASTContext *
486ClangASTContext::getASTContext()
487{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000488 if (m_ast_ap.get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 {
Greg Clayton6beaaa62011-01-17 03:46:26 +0000490 m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
491 *getSourceManager(),
Sean Callanan880e6802011-10-07 23:18:13 +0000492 getTargetInfo(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000493 *getIdentifierTable(),
494 *getSelectorTable(),
495 *getBuiltinContext(),
496 0));
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000497
Greg Clayton6beaaa62011-01-17 03:46:26 +0000498 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
499 {
500 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
501 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
502 }
503
Sean Callanan880e6802011-10-07 23:18:13 +0000504 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000506 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507}
508
509Builtin::Context *
510ClangASTContext::getBuiltinContext()
511{
512 if (m_builtins_ap.get() == NULL)
Sean Callanan880e6802011-10-07 23:18:13 +0000513 m_builtins_ap.reset (new Builtin::Context());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514 return m_builtins_ap.get();
515}
516
517IdentifierTable *
518ClangASTContext::getIdentifierTable()
519{
520 if (m_identifier_table_ap.get() == NULL)
521 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), NULL));
522 return m_identifier_table_ap.get();
523}
524
525LangOptions *
526ClangASTContext::getLanguageOptions()
527{
528 if (m_language_options_ap.get() == NULL)
529 {
530 m_language_options_ap.reset(new LangOptions());
Greg Clayton94e5d782010-06-13 17:34:29 +0000531 ParseLangArgs(*m_language_options_ap, IK_ObjCXX);
532// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000533 }
534 return m_language_options_ap.get();
535}
536
537SelectorTable *
538ClangASTContext::getSelectorTable()
539{
540 if (m_selector_table_ap.get() == NULL)
541 m_selector_table_ap.reset (new SelectorTable());
542 return m_selector_table_ap.get();
543}
544
Sean Callanan79439e82010-11-18 02:56:27 +0000545clang::FileManager *
546ClangASTContext::getFileManager()
547{
548 if (m_file_manager_ap.get() == NULL)
Greg Clayton38a61402010-12-02 23:20:03 +0000549 {
550 clang::FileSystemOptions file_system_options;
551 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
552 }
Sean Callanan79439e82010-11-18 02:56:27 +0000553 return m_file_manager_ap.get();
554}
555
Greg Claytone1a916a2010-07-21 22:12:05 +0000556clang::SourceManager *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557ClangASTContext::getSourceManager()
558{
559 if (m_source_manager_ap.get() == NULL)
Sean Callanan880e6802011-10-07 23:18:13 +0000560 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561 return m_source_manager_ap.get();
562}
563
Sean Callanan880e6802011-10-07 23:18:13 +0000564clang::DiagnosticsEngine *
565ClangASTContext::getDiagnosticsEngine()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000566{
Sean Callanan880e6802011-10-07 23:18:13 +0000567 if (m_diagnostics_engine_ap.get() == NULL)
Greg Claytona651b532010-11-19 21:46:54 +0000568 {
569 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Sean Callananec8f1ef2012-10-25 01:00:25 +0000570 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
Greg Claytona651b532010-11-19 21:46:54 +0000571 }
Sean Callanan880e6802011-10-07 23:18:13 +0000572 return m_diagnostics_engine_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573}
574
Sean Callanan880e6802011-10-07 23:18:13 +0000575class NullDiagnosticConsumer : public DiagnosticConsumer
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000576{
577public:
Sean Callanan880e6802011-10-07 23:18:13 +0000578 NullDiagnosticConsumer ()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000579 {
580 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
581 }
582
Sean Callanan880e6802011-10-07 23:18:13 +0000583 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const Diagnostic &info)
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000584 {
585 if (m_log)
586 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000587 llvm::SmallVector<char, 32> diag_str(10);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000588 info.FormatDiagnostic(diag_str);
589 diag_str.push_back('\0');
590 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
591 }
592 }
Sean Callanan880e6802011-10-07 23:18:13 +0000593
594 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
595 {
596 return new NullDiagnosticConsumer ();
597 }
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000598private:
Greg Clayton5160ce52013-03-27 23:08:40 +0000599 Log * m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000600};
601
Sean Callanan880e6802011-10-07 23:18:13 +0000602DiagnosticConsumer *
603ClangASTContext::getDiagnosticConsumer()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000604{
Sean Callanan880e6802011-10-07 23:18:13 +0000605 if (m_diagnostic_consumer_ap.get() == NULL)
606 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000607
Sean Callanan880e6802011-10-07 23:18:13 +0000608 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000609}
610
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611TargetOptions *
612ClangASTContext::getTargetOptions()
613{
Sean Callananc5069ad2012-10-17 22:11:14 +0000614 if (m_target_options_rp.getPtr() == NULL && !m_target_triple.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615 {
Sean Callananc5069ad2012-10-17 22:11:14 +0000616 m_target_options_rp.reset ();
617 m_target_options_rp = new TargetOptions();
618 if (m_target_options_rp.getPtr() != NULL)
619 m_target_options_rp->Triple = m_target_triple;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 }
Sean Callananc5069ad2012-10-17 22:11:14 +0000621 return m_target_options_rp.getPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622}
623
624
625TargetInfo *
626ClangASTContext::getTargetInfo()
627{
Greg Clayton70512312012-05-08 01:45:38 +0000628 // target_triple should be something like "x86_64-apple-macosx"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 if (m_target_info_ap.get() == NULL && !m_target_triple.empty())
Greg Clayton38d880a2012-11-16 21:35:22 +0000630 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), getTargetOptions()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631 return m_target_info_ap.get();
632}
633
634#pragma mark Basic Types
635
636static inline bool
Greg Clayton6beaaa62011-01-17 03:46:26 +0000637QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000639 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640 if (qual_type_bit_size == bit_size)
641 return true;
642 return false;
643}
644
Greg Clayton1be10fc2010-09-29 01:12:09 +0000645clang_type_t
Greg Claytonc86103d2010-08-05 01:57:25 +0000646ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000648 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000649
Greg Clayton6beaaa62011-01-17 03:46:26 +0000650 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651
Greg Clayton6beaaa62011-01-17 03:46:26 +0000652 return GetBuiltinTypeForEncodingAndBitSize (ast, encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653}
654
Greg Clayton1be10fc2010-09-29 01:12:09 +0000655clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000656ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000658 if (!ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659 return NULL;
660
661 switch (encoding)
662 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000663 case eEncodingInvalid:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000664 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
665 return ast->VoidPtrTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666 break;
667
Greg Claytonc86103d2010-08-05 01:57:25 +0000668 case eEncodingUint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000669 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
670 return ast->UnsignedCharTy.getAsOpaquePtr();
671 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
672 return ast->UnsignedShortTy.getAsOpaquePtr();
673 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
674 return ast->UnsignedIntTy.getAsOpaquePtr();
675 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
676 return ast->UnsignedLongTy.getAsOpaquePtr();
677 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
678 return ast->UnsignedLongLongTy.getAsOpaquePtr();
679 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
680 return ast->UnsignedInt128Ty.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000681 break;
682
Greg Claytonc86103d2010-08-05 01:57:25 +0000683 case eEncodingSint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000684 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
685 return ast->CharTy.getAsOpaquePtr();
686 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
687 return ast->ShortTy.getAsOpaquePtr();
688 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
689 return ast->IntTy.getAsOpaquePtr();
690 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
691 return ast->LongTy.getAsOpaquePtr();
692 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
693 return ast->LongLongTy.getAsOpaquePtr();
694 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
695 return ast->Int128Ty.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696 break;
697
Greg Claytonc86103d2010-08-05 01:57:25 +0000698 case eEncodingIEEE754:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000699 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
700 return ast->FloatTy.getAsOpaquePtr();
701 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
702 return ast->DoubleTy.getAsOpaquePtr();
703 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
704 return ast->LongDoubleTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000705 break;
706
Greg Claytonc86103d2010-08-05 01:57:25 +0000707 case eEncodingVector:
Johnny Chenc79c93a2012-03-07 01:12:24 +0000708 // Sanity check that bit_size is a multiple of 8's.
709 if (bit_size && !(bit_size & 0x7u))
710 return ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8).getAsOpaquePtr();
711 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712 }
713
714 return NULL;
715}
716
Greg Clayton1be10fc2010-09-29 01:12:09 +0000717clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
719{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000720 ASTContext *ast = getASTContext();
Sean Callanan38d4df52012-04-03 01:10:10 +0000721
722#define streq(a,b) strcmp(a,b) == 0
Greg Clayton6beaaa62011-01-17 03:46:26 +0000723 assert (ast != NULL);
724 if (ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725 {
726 switch (dw_ate)
727 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000728 default:
729 break;
Greg Clayton605684e2011-10-28 23:06:08 +0000730
Sean Callanan38d4df52012-04-03 01:10:10 +0000731 case DW_ATE_address:
732 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
733 return ast->VoidPtrTy.getAsOpaquePtr();
734 break;
735
736 case DW_ATE_boolean:
737 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
738 return ast->BoolTy.getAsOpaquePtr();
739 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
740 return ast->UnsignedCharTy.getAsOpaquePtr();
741 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
742 return ast->UnsignedShortTy.getAsOpaquePtr();
743 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
744 return ast->UnsignedIntTy.getAsOpaquePtr();
745 break;
746
747 case DW_ATE_lo_user:
748 // This has been seen to mean DW_AT_complex_integer
749 if (type_name)
Greg Clayton605684e2011-10-28 23:06:08 +0000750 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000751 if (::strstr(type_name, "complex"))
752 {
753 clang_type_t complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2);
754 return ast->getComplexType (QualType::getFromOpaquePtr(complex_int_clang_type)).getAsOpaquePtr();
755 }
Greg Clayton605684e2011-10-28 23:06:08 +0000756 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000757 break;
758
759 case DW_ATE_complex_float:
760 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
761 return ast->FloatComplexTy.getAsOpaquePtr();
762 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
763 return ast->DoubleComplexTy.getAsOpaquePtr();
764 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
765 return ast->LongDoubleComplexTy.getAsOpaquePtr();
766 else
Greg Clayton605684e2011-10-28 23:06:08 +0000767 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000768 clang_type_t complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2);
769 return ast->getComplexType (QualType::getFromOpaquePtr(complex_float_clang_type)).getAsOpaquePtr();
Greg Clayton605684e2011-10-28 23:06:08 +0000770 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000771 break;
772
773 case DW_ATE_float:
774 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
775 return ast->FloatTy.getAsOpaquePtr();
776 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
777 return ast->DoubleTy.getAsOpaquePtr();
778 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
779 return ast->LongDoubleTy.getAsOpaquePtr();
780 break;
781
782 case DW_ATE_signed:
783 if (type_name)
784 {
785 if (streq(type_name, "wchar_t") &&
786 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
787 return ast->WCharTy.getAsOpaquePtr();
788 if (streq(type_name, "void") &&
789 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
790 return ast->VoidTy.getAsOpaquePtr();
791 if (strstr(type_name, "long long") &&
792 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
793 return ast->LongLongTy.getAsOpaquePtr();
794 if (strstr(type_name, "long") &&
795 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
796 return ast->LongTy.getAsOpaquePtr();
797 if (strstr(type_name, "short") &&
798 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
799 return ast->ShortTy.getAsOpaquePtr();
800 if (strstr(type_name, "char"))
801 {
802 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
803 return ast->CharTy.getAsOpaquePtr();
804 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
805 return ast->SignedCharTy.getAsOpaquePtr();
806 }
807 if (strstr(type_name, "int"))
808 {
809 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
810 return ast->IntTy.getAsOpaquePtr();
811 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
812 return ast->Int128Ty.getAsOpaquePtr();
813 }
814 }
815 // We weren't able to match up a type name, just search by size
816 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
817 return ast->CharTy.getAsOpaquePtr();
818 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
819 return ast->ShortTy.getAsOpaquePtr();
820 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
821 return ast->IntTy.getAsOpaquePtr();
822 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
823 return ast->LongTy.getAsOpaquePtr();
824 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
825 return ast->LongLongTy.getAsOpaquePtr();
826 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
827 return ast->Int128Ty.getAsOpaquePtr();
828 break;
829
830 case DW_ATE_signed_char:
831 if (type_name)
832 {
833 if (streq(type_name, "signed char"))
834 {
835 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
836 return ast->SignedCharTy.getAsOpaquePtr();
837 }
838 }
839 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
840 return ast->CharTy.getAsOpaquePtr();
841 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
842 return ast->SignedCharTy.getAsOpaquePtr();
843 break;
844
845 case DW_ATE_unsigned:
846 if (type_name)
847 {
848 if (strstr(type_name, "long long"))
849 {
850 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
851 return ast->UnsignedLongLongTy.getAsOpaquePtr();
852 }
853 else if (strstr(type_name, "long"))
854 {
855 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
856 return ast->UnsignedLongTy.getAsOpaquePtr();
857 }
858 else if (strstr(type_name, "short"))
859 {
860 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
861 return ast->UnsignedShortTy.getAsOpaquePtr();
862 }
863 else if (strstr(type_name, "char"))
864 {
865 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
866 return ast->UnsignedCharTy.getAsOpaquePtr();
867 }
868 else if (strstr(type_name, "int"))
869 {
870 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
871 return ast->UnsignedIntTy.getAsOpaquePtr();
872 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
873 return ast->UnsignedInt128Ty.getAsOpaquePtr();
874 }
875 }
876 // We weren't able to match up a type name, just search by size
877 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
878 return ast->UnsignedCharTy.getAsOpaquePtr();
879 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
880 return ast->UnsignedShortTy.getAsOpaquePtr();
881 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
882 return ast->UnsignedIntTy.getAsOpaquePtr();
883 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
884 return ast->UnsignedLongTy.getAsOpaquePtr();
885 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
886 return ast->UnsignedLongLongTy.getAsOpaquePtr();
887 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
888 return ast->UnsignedInt128Ty.getAsOpaquePtr();
889 break;
890
891 case DW_ATE_unsigned_char:
892 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
893 return ast->UnsignedCharTy.getAsOpaquePtr();
894 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
895 return ast->UnsignedShortTy.getAsOpaquePtr();
896 break;
897
898 case DW_ATE_imaginary_float:
899 break;
900
901 case DW_ATE_UTF:
902 if (type_name)
903 {
904 if (streq(type_name, "char16_t"))
905 {
906 return ast->Char16Ty.getAsOpaquePtr();
907 }
908 else if (streq(type_name, "char32_t"))
909 {
910 return ast->Char32Ty.getAsOpaquePtr();
911 }
912 }
913 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000914 }
915 }
916 // This assert should fire for anything that we don't catch above so we know
917 // to fix any issues we run into.
Greg Claytondc968d12011-05-17 18:15:05 +0000918 if (type_name)
919 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000920 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 +0000921 }
922 else
923 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000924 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 +0000925 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000926 return NULL;
927}
928
Greg Clayton1be10fc2010-09-29 01:12:09 +0000929clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000930ClangASTContext::GetBuiltInType_void(ASTContext *ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000931{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000932 return ast->VoidTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000933}
934
Greg Clayton1be10fc2010-09-29 01:12:09 +0000935clang_type_t
Sean Callananf7c3e272010-11-19 02:52:21 +0000936ClangASTContext::GetBuiltInType_bool()
937{
938 return getASTContext()->BoolTy.getAsOpaquePtr();
939}
940
941clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000942ClangASTContext::GetBuiltInType_objc_id()
943{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000944 return getASTContext()->getObjCIdType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000945}
946
Enrico Granatabdbda932013-03-20 19:04:28 +0000947lldb::clang_type_t
948ClangASTContext::GetBuiltInType_objc_id(clang::ASTContext *ast)
949{
950 return ast->getObjCIdType().getAsOpaquePtr();
951}
952
Greg Clayton1be10fc2010-09-29 01:12:09 +0000953clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000954ClangASTContext::GetBuiltInType_objc_Class()
955{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000956 return getASTContext()->getObjCClassType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000957}
958
Greg Clayton1be10fc2010-09-29 01:12:09 +0000959clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000960ClangASTContext::GetBuiltInType_objc_selector()
961{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000962 return getASTContext()->getObjCSelType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000963}
964
Greg Clayton1be10fc2010-09-29 01:12:09 +0000965clang_type_t
Sean Callanan77502262011-05-12 23:54:16 +0000966ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast)
967{
968 return ast->UnknownAnyTy.getAsOpaquePtr();
969}
970
971clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000972ClangASTContext::GetCStringType (bool is_const)
973{
974 QualType char_type(getASTContext()->CharTy);
975
976 if (is_const)
977 char_type.addConst();
978
979 return getASTContext()->getPointerType(char_type).getAsOpaquePtr();
980}
981
Greg Clayton1be10fc2010-09-29 01:12:09 +0000982clang_type_t
Sean Callanana6582262012-04-05 00:12:52 +0000983ClangASTContext::GetVoidType()
984{
985 return GetVoidType(getASTContext());
986}
987
988clang_type_t
989ClangASTContext::GetVoidType(ASTContext *ast)
990{
991 return ast->VoidTy.getAsOpaquePtr();
992}
993
994clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000995ClangASTContext::GetVoidPtrType (bool is_const)
996{
997 return GetVoidPtrType(getASTContext(), is_const);
998}
999
Greg Clayton1be10fc2010-09-29 01:12:09 +00001000clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00001001ClangASTContext::GetVoidPtrType (ASTContext *ast, bool is_const)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001002{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001003 QualType void_ptr_type(ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004
1005 if (is_const)
1006 void_ptr_type.addConst();
1007
1008 return void_ptr_type.getAsOpaquePtr();
1009}
1010
Sean Callanan09ab4b72011-11-30 22:11:59 +00001011clang::DeclContext *
1012ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast)
1013{
1014 return ast->getTranslationUnitDecl();
1015}
1016
Greg Clayton1be10fc2010-09-29 01:12:09 +00001017clang_type_t
Greg Clayton38a61402010-12-02 23:20:03 +00001018ClangASTContext::CopyType (ASTContext *dst_ast,
1019 ASTContext *src_ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001020 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001021{
Sean Callanan79439e82010-11-18 02:56:27 +00001022 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001023 FileManager file_manager (file_system_options);
1024 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001025 *src_ast, file_manager,
1026 false);
Sean Callanan0617fcb2010-11-09 22:37:10 +00001027
Greg Clayton38a61402010-12-02 23:20:03 +00001028 QualType src (QualType::getFromOpaquePtr(clang_type));
1029 QualType dst (importer.Import(src));
Sean Callanan0617fcb2010-11-09 22:37:10 +00001030
1031 return dst.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001032}
1033
Greg Clayton526e5af2010-11-13 03:52:47 +00001034
1035clang::Decl *
Greg Clayton38a61402010-12-02 23:20:03 +00001036ClangASTContext::CopyDecl (ASTContext *dst_ast,
1037 ASTContext *src_ast,
Greg Clayton526e5af2010-11-13 03:52:47 +00001038 clang::Decl *source_decl)
Sean Callanan7fddd4c2010-12-11 00:08:56 +00001039{
Sean Callanan79439e82010-11-18 02:56:27 +00001040 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001041 FileManager file_manager (file_system_options);
1042 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001043 *src_ast, file_manager,
1044 false);
Greg Clayton526e5af2010-11-13 03:52:47 +00001045
1046 return importer.Import(source_decl);
1047}
1048
Sean Callanan23a30272010-07-16 00:00:27 +00001049bool
Greg Clayton84db9102012-03-26 23:03:23 +00001050ClangASTContext::AreTypesSame (ASTContext *ast,
1051 clang_type_t type1,
1052 clang_type_t type2,
1053 bool ignore_qualifiers)
Sean Callanan4dcca2622010-07-15 22:30:52 +00001054{
Greg Clayton55995eb2012-04-06 17:38:55 +00001055 if (type1 == type2)
1056 return true;
1057
Sean Callanan5056ab02012-02-18 02:01:03 +00001058 QualType type1_qual = QualType::getFromOpaquePtr(type1);
1059 QualType type2_qual = QualType::getFromOpaquePtr(type2);
1060
1061 if (ignore_qualifiers)
1062 {
1063 type1_qual = type1_qual.getUnqualifiedType();
1064 type2_qual = type2_qual.getUnqualifiedType();
1065 }
1066
1067 return ast->hasSameType (type1_qual,
1068 type2_qual);
Sean Callanan4dcca2622010-07-15 22:30:52 +00001069}
1070
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001071#pragma mark CVR modifiers
1072
Greg Clayton1be10fc2010-09-29 01:12:09 +00001073clang_type_t
1074ClangASTContext::AddConstModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001075{
1076 if (clang_type)
1077 {
1078 QualType result(QualType::getFromOpaquePtr(clang_type));
1079 result.addConst();
1080 return result.getAsOpaquePtr();
1081 }
1082 return NULL;
1083}
1084
Greg Clayton1be10fc2010-09-29 01:12:09 +00001085clang_type_t
1086ClangASTContext::AddRestrictModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001087{
1088 if (clang_type)
1089 {
1090 QualType result(QualType::getFromOpaquePtr(clang_type));
1091 result.getQualifiers().setRestrict (true);
1092 return result.getAsOpaquePtr();
1093 }
1094 return NULL;
1095}
1096
Greg Clayton1be10fc2010-09-29 01:12:09 +00001097clang_type_t
1098ClangASTContext::AddVolatileModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001099{
1100 if (clang_type)
1101 {
1102 QualType result(QualType::getFromOpaquePtr(clang_type));
1103 result.getQualifiers().setVolatile (true);
1104 return result.getAsOpaquePtr();
1105 }
1106 return NULL;
1107}
1108
Greg Clayton6beaaa62011-01-17 03:46:26 +00001109
1110clang_type_t
1111ClangASTContext::GetTypeForDecl (TagDecl *decl)
1112{
1113 // No need to call the getASTContext() accessor (which can create the AST
1114 // if it isn't created yet, because we can't have created a decl in this
1115 // AST if our AST didn't already exist...
1116 if (m_ast_ap.get())
1117 return m_ast_ap->getTagDeclType(decl).getAsOpaquePtr();
1118 return NULL;
1119}
1120
1121clang_type_t
1122ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1123{
1124 // No need to call the getASTContext() accessor (which can create the AST
1125 // if it isn't created yet, because we can't have created a decl in this
1126 // AST if our AST didn't already exist...
1127 if (m_ast_ap.get())
1128 return m_ast_ap->getObjCInterfaceType(decl).getAsOpaquePtr();
1129 return NULL;
1130}
1131
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001132#pragma mark Structure, Unions, Classes
1133
Greg Clayton1be10fc2010-09-29 01:12:09 +00001134clang_type_t
Greg Claytonc4ffd662013-03-08 01:37:30 +00001135ClangASTContext::CreateRecordType (DeclContext *decl_ctx,
1136 AccessType access_type,
1137 const char *name,
1138 int kind,
1139 LanguageType language,
1140 ClangASTMetadata *metadata)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001141{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001142 ASTContext *ast = getASTContext();
1143 assert (ast != NULL);
Sean Callananad880762012-04-18 01:06:17 +00001144
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001145 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001146 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001147
Greg Clayton9e409562010-07-28 02:04:09 +00001148
Greg Claytone1be9962011-08-24 23:50:00 +00001149 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
Greg Clayton9e409562010-07-28 02:04:09 +00001150 {
Greg Claytonaaf99e02010-10-11 02:25:34 +00001151 bool isForwardDecl = true;
Greg Clayton9e409562010-07-28 02:04:09 +00001152 bool isInternal = false;
Sean Callananad880762012-04-18 01:06:17 +00001153 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal, metadata);
Greg Clayton9e409562010-07-28 02:04:09 +00001154 }
1155
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1157 // we will need to update this code. I was told to currently always use
1158 // the CXXRecordDecl class since we often don't know from debug information
1159 // if something is struct or a class, so we default to always use the more
1160 // complete definition just in case.
Greg Claytonf0705c82011-10-22 03:33:13 +00001161 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1162 (TagDecl::TagKind)kind,
1163 decl_ctx,
1164 SourceLocation(),
1165 SourceLocation(),
1166 name && name[0] ? &ast->Idents.get(name) : NULL);
Sean Callanan7282e2a2012-01-13 22:10:18 +00001167
Greg Claytonc4ffd662013-03-08 01:37:30 +00001168 if (decl)
Greg Clayton55561e92011-10-26 03:31:36 +00001169 {
Greg Claytonc4ffd662013-03-08 01:37:30 +00001170 if (metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001171 SetMetadata(ast, decl, *metadata);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001172
Greg Clayton55561e92011-10-26 03:31:36 +00001173 if (access_type != eAccessNone)
1174 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Greg Claytonc4ffd662013-03-08 01:37:30 +00001175
1176 if (decl_ctx)
1177 decl_ctx->addDecl (decl);
1178
1179 return ast->getTagDeclType(decl).getAsOpaquePtr();
Greg Clayton55561e92011-10-26 03:31:36 +00001180 }
Greg Claytonc4ffd662013-03-08 01:37:30 +00001181 return NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001182}
1183
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001184static TemplateParameterList *
1185CreateTemplateParameterList (ASTContext *ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001186 const ClangASTContext::TemplateParameterInfos &template_param_infos,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001187 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1188{
1189 const bool parameter_pack = false;
1190 const bool is_typename = false;
1191 const unsigned depth = 0;
1192 const size_t num_template_params = template_param_infos.GetSize();
1193 for (size_t i=0; i<num_template_params; ++i)
1194 {
1195 const char *name = template_param_infos.names[i];
Greg Clayton283b2652013-04-23 22:38:02 +00001196
1197 IdentifierInfo *identifier_info = NULL;
1198 if (name && name[0])
1199 identifier_info = &ast->Idents.get(name);
Sean Callanan3d654b32012-09-24 22:25:51 +00001200 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001201 {
1202 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1203 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1204 SourceLocation(),
1205 SourceLocation(),
1206 depth,
1207 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001208 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001209 template_param_infos.args[i].getIntegralType(),
1210 parameter_pack,
1211 NULL));
1212
1213 }
1214 else
1215 {
1216 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1217 ast->getTranslationUnitDecl(), // Is this the right decl context?
1218 SourceLocation(),
1219 SourceLocation(),
1220 depth,
1221 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001222 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001223 is_typename,
1224 parameter_pack));
1225 }
1226 }
1227
1228 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1229 SourceLocation(),
1230 SourceLocation(),
1231 &template_param_decls.front(),
1232 template_param_decls.size(),
1233 SourceLocation());
1234 return template_param_list;
1235}
1236
1237clang::FunctionTemplateDecl *
1238ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1239 clang::FunctionDecl *func_decl,
1240 const char *name,
1241 const TemplateParameterInfos &template_param_infos)
1242{
1243// /// \brief Create a function template node.
1244 ASTContext *ast = getASTContext();
1245
1246 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1247
1248 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1249 template_param_infos,
1250 template_param_decls);
1251 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1252 decl_ctx,
1253 func_decl->getLocation(),
1254 func_decl->getDeclName(),
1255 template_param_list,
1256 func_decl);
1257
1258 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1259 i < template_param_decl_count;
1260 ++i)
1261 {
1262 // TODO: verify which decl context we should put template_param_decls into..
1263 template_param_decls[i]->setDeclContext (func_decl);
1264 }
1265
1266 return func_tmpl_decl;
1267}
1268
1269void
1270ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1271 clang::FunctionTemplateDecl *func_tmpl_decl,
1272 const TemplateParameterInfos &infos)
1273{
1274 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1275 infos.args.data(),
1276 infos.args.size());
1277
1278 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1279 &template_args,
1280 NULL);
1281}
1282
1283
Greg Claytonf0705c82011-10-22 03:33:13 +00001284ClassTemplateDecl *
1285ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001286 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001287 const char *class_name,
1288 int kind,
1289 const TemplateParameterInfos &template_param_infos)
1290{
1291 ASTContext *ast = getASTContext();
1292
1293 ClassTemplateDecl *class_template_decl = NULL;
1294 if (decl_ctx == NULL)
1295 decl_ctx = ast->getTranslationUnitDecl();
1296
1297 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1298 DeclarationName decl_name (&identifier_info);
1299
1300 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001301
1302 for (NamedDecl *decl : result)
Greg Claytonf0705c82011-10-22 03:33:13 +00001303 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001304 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001305 if (class_template_decl)
1306 return class_template_decl;
1307 }
1308
1309 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Claytonf0705c82011-10-22 03:33:13 +00001310
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001311 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1312 template_param_infos,
1313 template_param_decls);
Greg Claytonf0705c82011-10-22 03:33:13 +00001314
1315 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1316 (TagDecl::TagKind)kind,
1317 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1318 SourceLocation(),
1319 SourceLocation(),
1320 &identifier_info);
Greg Claytone04741d2011-12-02 02:09:28 +00001321
1322 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1323 i < template_param_decl_count;
1324 ++i)
1325 {
1326 template_param_decls[i]->setDeclContext (template_cxx_decl);
1327 }
1328
Sean Callananb5c79622011-11-19 01:35:08 +00001329 // With templated classes, we say that a class is templated with
1330 // specializations, but that the bare class has no functions.
Sean Callananfa4fab72013-02-01 06:55:48 +00001331 //template_cxx_decl->startDefinition();
1332 //template_cxx_decl->completeDefinition();
Sean Callananb5c79622011-11-19 01:35:08 +00001333
Greg Claytonf0705c82011-10-22 03:33:13 +00001334 class_template_decl = ClassTemplateDecl::Create (*ast,
1335 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1336 SourceLocation(),
1337 decl_name,
1338 template_param_list,
1339 template_cxx_decl,
1340 NULL);
1341
1342 if (class_template_decl)
Sean Callanan5e9e1992011-10-26 01:06:27 +00001343 {
Greg Clayton55561e92011-10-26 03:31:36 +00001344 if (access_type != eAccessNone)
1345 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Sean Callanan5b26f272012-02-04 08:49:35 +00001346
1347 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1348 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1349
Greg Claytonf0705c82011-10-22 03:33:13 +00001350 decl_ctx->addDecl (class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001351
1352#ifdef LLDB_CONFIGURATION_DEBUG
1353 VerifyDecl(class_template_decl);
1354#endif
1355 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001356
1357 return class_template_decl;
1358}
1359
1360
1361ClassTemplateSpecializationDecl *
1362ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1363 ClassTemplateDecl *class_template_decl,
1364 int kind,
1365 const TemplateParameterInfos &template_param_infos)
1366{
1367 ASTContext *ast = getASTContext();
1368 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1369 (TagDecl::TagKind)kind,
1370 decl_ctx,
1371 SourceLocation(),
1372 SourceLocation(),
1373 class_template_decl,
1374 &template_param_infos.args.front(),
1375 template_param_infos.args.size(),
1376 NULL);
1377
Sean Callananfa4fab72013-02-01 06:55:48 +00001378 class_template_specialization_decl->setSpecializationKind(TSK_ExplicitSpecialization);
1379
Greg Claytonf0705c82011-10-22 03:33:13 +00001380 return class_template_specialization_decl;
1381}
1382
1383lldb::clang_type_t
1384ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1385{
1386 if (class_template_specialization_decl)
1387 {
1388 ASTContext *ast = getASTContext();
1389 if (ast)
1390 return ast->getTagDeclType(class_template_specialization_decl).getAsOpaquePtr();
1391 }
1392 return NULL;
1393}
1394
Greg Clayton6beaaa62011-01-17 03:46:26 +00001395bool
1396ClangASTContext::SetHasExternalStorage (clang_type_t clang_type, bool has_extern)
1397{
1398 if (clang_type == NULL)
1399 return false;
1400
1401 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
1402
1403 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
1404 switch (type_class)
1405 {
1406 case clang::Type::Record:
1407 {
1408 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
1409 if (cxx_record_decl)
1410 {
1411 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001412 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001413 return true;
1414 }
1415 }
1416 break;
1417
1418 case clang::Type::Enum:
1419 {
1420 EnumDecl *enum_decl = cast<EnumType>(qual_type)->getDecl();
1421 if (enum_decl)
1422 {
1423 enum_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001424 enum_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001425 return true;
1426 }
1427 }
1428 break;
1429
1430 case clang::Type::ObjCObject:
1431 case clang::Type::ObjCInterface:
1432 {
Sean Callanan78e37602011-01-27 04:42:51 +00001433 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001434 assert (objc_class_type);
1435 if (objc_class_type)
1436 {
1437 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1438
1439 if (class_interface_decl)
1440 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001441 class_interface_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001442 class_interface_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001443 return true;
1444 }
1445 }
1446 }
1447 break;
1448
1449 case clang::Type::Typedef:
1450 return ClangASTContext::SetHasExternalStorage (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
Sean Callanan912855f2011-08-11 23:56:13 +00001451
1452 case clang::Type::Elaborated:
1453 return ClangASTContext::SetHasExternalStorage (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001454
1455 default:
1456 break;
1457 }
1458 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001459}
1460
Greg Claytona3c444a2010-10-01 23:13:49 +00001461static bool
1462IsOperator (const char *name, OverloadedOperatorKind &op_kind)
1463{
1464 if (name == NULL || name[0] == '\0')
1465 return false;
1466
Sean Callanana43f20d2010-12-10 19:51:54 +00001467#define OPERATOR_PREFIX "operator"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001468#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
Sean Callananbfeff8c2010-12-10 02:15:55 +00001469
1470 const char *post_op_name = NULL;
1471
Sean Callanana43f20d2010-12-10 19:51:54 +00001472 bool no_space = true;
Sean Callananbfeff8c2010-12-10 02:15:55 +00001473
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001474 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
Greg Claytona3c444a2010-10-01 23:13:49 +00001475 return false;
1476
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001477 post_op_name = name + OPERATOR_PREFIX_LENGTH;
1478
Sean Callanana43f20d2010-12-10 19:51:54 +00001479 if (post_op_name[0] == ' ')
1480 {
1481 post_op_name++;
1482 no_space = false;
1483 }
1484
1485#undef OPERATOR_PREFIX
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001486#undef OPERATOR_PREFIX_LENGTH
Sean Callanana43f20d2010-12-10 19:51:54 +00001487
Greg Claytona3c444a2010-10-01 23:13:49 +00001488 // This is an operator, set the overloaded operator kind to invalid
1489 // in case this is a conversion operator...
1490 op_kind = NUM_OVERLOADED_OPERATORS;
1491
1492 switch (post_op_name[0])
1493 {
Sean Callananbfeff8c2010-12-10 02:15:55 +00001494 default:
1495 if (no_space)
1496 return false;
1497 break;
Greg Claytona3c444a2010-10-01 23:13:49 +00001498 case 'n':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001499 if (no_space)
1500 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001501 if (strcmp (post_op_name, "new") == 0)
1502 op_kind = OO_New;
1503 else if (strcmp (post_op_name, "new[]") == 0)
1504 op_kind = OO_Array_New;
1505 break;
1506
1507 case 'd':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001508 if (no_space)
1509 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001510 if (strcmp (post_op_name, "delete") == 0)
1511 op_kind = OO_Delete;
1512 else if (strcmp (post_op_name, "delete[]") == 0)
1513 op_kind = OO_Array_Delete;
1514 break;
1515
1516 case '+':
1517 if (post_op_name[1] == '\0')
1518 op_kind = OO_Plus;
1519 else if (post_op_name[2] == '\0')
1520 {
1521 if (post_op_name[1] == '=')
1522 op_kind = OO_PlusEqual;
1523 else if (post_op_name[1] == '+')
1524 op_kind = OO_PlusPlus;
1525 }
1526 break;
1527
1528 case '-':
1529 if (post_op_name[1] == '\0')
1530 op_kind = OO_Minus;
1531 else if (post_op_name[2] == '\0')
1532 {
1533 switch (post_op_name[1])
1534 {
1535 case '=': op_kind = OO_MinusEqual; break;
1536 case '-': op_kind = OO_MinusMinus; break;
1537 case '>': op_kind = OO_Arrow; break;
1538 }
1539 }
1540 else if (post_op_name[3] == '\0')
1541 {
1542 if (post_op_name[2] == '*')
1543 op_kind = OO_ArrowStar; break;
1544 }
1545 break;
1546
1547 case '*':
1548 if (post_op_name[1] == '\0')
1549 op_kind = OO_Star;
1550 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1551 op_kind = OO_StarEqual;
1552 break;
1553
1554 case '/':
1555 if (post_op_name[1] == '\0')
1556 op_kind = OO_Slash;
1557 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1558 op_kind = OO_SlashEqual;
1559 break;
1560
1561 case '%':
1562 if (post_op_name[1] == '\0')
1563 op_kind = OO_Percent;
1564 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1565 op_kind = OO_PercentEqual;
1566 break;
1567
1568
1569 case '^':
1570 if (post_op_name[1] == '\0')
1571 op_kind = OO_Caret;
1572 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1573 op_kind = OO_CaretEqual;
1574 break;
1575
1576 case '&':
1577 if (post_op_name[1] == '\0')
1578 op_kind = OO_Amp;
1579 else if (post_op_name[2] == '\0')
1580 {
1581 switch (post_op_name[1])
1582 {
1583 case '=': op_kind = OO_AmpEqual; break;
1584 case '&': op_kind = OO_AmpAmp; break;
1585 }
1586 }
1587 break;
1588
1589 case '|':
1590 if (post_op_name[1] == '\0')
1591 op_kind = OO_Pipe;
1592 else if (post_op_name[2] == '\0')
1593 {
1594 switch (post_op_name[1])
1595 {
1596 case '=': op_kind = OO_PipeEqual; break;
1597 case '|': op_kind = OO_PipePipe; break;
1598 }
1599 }
1600 break;
1601
1602 case '~':
1603 if (post_op_name[1] == '\0')
1604 op_kind = OO_Tilde;
1605 break;
1606
1607 case '!':
1608 if (post_op_name[1] == '\0')
1609 op_kind = OO_Exclaim;
1610 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1611 op_kind = OO_ExclaimEqual;
1612 break;
1613
1614 case '=':
1615 if (post_op_name[1] == '\0')
1616 op_kind = OO_Equal;
1617 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1618 op_kind = OO_EqualEqual;
1619 break;
1620
1621 case '<':
1622 if (post_op_name[1] == '\0')
1623 op_kind = OO_Less;
1624 else if (post_op_name[2] == '\0')
1625 {
1626 switch (post_op_name[1])
1627 {
1628 case '<': op_kind = OO_LessLess; break;
1629 case '=': op_kind = OO_LessEqual; break;
1630 }
1631 }
1632 else if (post_op_name[3] == '\0')
1633 {
1634 if (post_op_name[2] == '=')
1635 op_kind = OO_LessLessEqual;
1636 }
1637 break;
1638
1639 case '>':
1640 if (post_op_name[1] == '\0')
1641 op_kind = OO_Greater;
1642 else if (post_op_name[2] == '\0')
1643 {
1644 switch (post_op_name[1])
1645 {
1646 case '>': op_kind = OO_GreaterGreater; break;
1647 case '=': op_kind = OO_GreaterEqual; break;
1648 }
1649 }
1650 else if (post_op_name[1] == '>' &&
1651 post_op_name[2] == '=' &&
1652 post_op_name[3] == '\0')
1653 {
1654 op_kind = OO_GreaterGreaterEqual;
1655 }
1656 break;
1657
1658 case ',':
1659 if (post_op_name[1] == '\0')
1660 op_kind = OO_Comma;
1661 break;
1662
1663 case '(':
1664 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
1665 op_kind = OO_Call;
1666 break;
1667
1668 case '[':
1669 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
1670 op_kind = OO_Subscript;
1671 break;
1672 }
1673
1674 return true;
1675}
Greg Clayton6beaaa62011-01-17 03:46:26 +00001676
Greg Clayton090d0982011-06-19 03:43:27 +00001677static inline bool
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001678check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
Greg Clayton090d0982011-06-19 03:43:27 +00001679{
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001680 // Special-case call since it can take any number of operands
1681 if(op_kind == OO_Call)
1682 return true;
1683
Greg Clayton090d0982011-06-19 03:43:27 +00001684 // The parameter count doens't include "this"
1685 if (num_params == 0)
1686 return unary;
1687 if (num_params == 1)
1688 return binary;
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001689 else
Greg Clayton090d0982011-06-19 03:43:27 +00001690 return false;
1691}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001692
Greg Clayton090d0982011-06-19 03:43:27 +00001693bool
1694ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1695{
Sean Callanan5b26f272012-02-04 08:49:35 +00001696 switch (op_kind)
1697 {
1698 default:
1699 break;
1700 // C++ standard allows any number of arguments to new/delete
1701 case OO_New:
1702 case OO_Array_New:
1703 case OO_Delete:
1704 case OO_Array_Delete:
1705 return true;
1706 }
1707
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001708#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 +00001709 switch (op_kind)
1710 {
1711#include "clang/Basic/OperatorKinds.def"
1712 default: break;
1713 }
1714 return false;
1715}
1716
Greg Claytona51ed9b2010-09-23 01:09:21 +00001717CXXMethodDecl *
Sean Callanan61da09b2010-09-17 02:58:26 +00001718ClangASTContext::AddMethodToCXXRecordType
1719(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001720 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001721 clang_type_t record_opaque_type,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001722 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001723 clang_type_t method_opaque_type,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001724 lldb::AccessType access,
Greg Clayton0fffff52010-09-24 05:15:53 +00001725 bool is_virtual,
1726 bool is_static,
Greg Claytonf51de672010-10-01 02:31:07 +00001727 bool is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00001728 bool is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00001729 bool is_attr_used,
1730 bool is_artificial
Greg Claytona51ed9b2010-09-23 01:09:21 +00001731)
Sean Callanan61da09b2010-09-17 02:58:26 +00001732{
Sean Callananfc55f5d2010-09-21 00:44:12 +00001733 if (!record_opaque_type || !method_opaque_type || !name)
Johnny Chend440bcc2010-09-28 16:10:54 +00001734 return NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001735
Greg Clayton6beaaa62011-01-17 03:46:26 +00001736 assert(ast);
Sean Callanan61da09b2010-09-17 02:58:26 +00001737
Greg Clayton6beaaa62011-01-17 03:46:26 +00001738 IdentifierTable *identifier_table = &ast->Idents;
Sean Callanan61da09b2010-09-17 02:58:26 +00001739
1740 assert(identifier_table);
1741
Sean Callananfc55f5d2010-09-21 00:44:12 +00001742 QualType record_qual_type(QualType::getFromOpaquePtr(record_opaque_type));
Greg Clayton0fffff52010-09-24 05:15:53 +00001743
Greg Clayton6beaaa62011-01-17 03:46:26 +00001744 CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
Sean Callanan61da09b2010-09-17 02:58:26 +00001745
Greg Clayton0fffff52010-09-24 05:15:53 +00001746 if (cxx_record_decl == NULL)
Greg Claytona51ed9b2010-09-23 01:09:21 +00001747 return NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001748
Greg Clayton0fffff52010-09-24 05:15:53 +00001749 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001750
Greg Claytonf51de672010-10-01 02:31:07 +00001751 CXXMethodDecl *cxx_method_decl = NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001752
Greg Claytonf51de672010-10-01 02:31:07 +00001753 DeclarationName decl_name (&identifier_table->get(name));
Greg Clayton878eaf12010-10-01 03:45:20 +00001754
Sean Callanan78e37602011-01-27 04:42:51 +00001755 const clang::FunctionType *function_Type = dyn_cast<FunctionType>(method_qual_type.getTypePtr());
Greg Clayton878eaf12010-10-01 03:45:20 +00001756
Greg Clayton90a2acd2010-10-02 01:40:05 +00001757 if (function_Type == NULL)
Greg Clayton878eaf12010-10-01 03:45:20 +00001758 return NULL;
1759
Sean Callanan78e37602011-01-27 04:42:51 +00001760 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(function_Type));
Greg Clayton878eaf12010-10-01 03:45:20 +00001761
1762 if (!method_function_prototype)
1763 return NULL;
1764
1765 unsigned int num_params = method_function_prototype->getNumArgs();
1766
Sean Callanandbb58392011-11-02 01:38:59 +00001767 CXXDestructorDecl *cxx_dtor_decl(NULL);
1768 CXXConstructorDecl *cxx_ctor_decl(NULL);
1769
Greg Clayton878eaf12010-10-01 03:45:20 +00001770 if (name[0] == '~')
Greg Claytonf51de672010-10-01 02:31:07 +00001771 {
Sean Callanandbb58392011-11-02 01:38:59 +00001772 cxx_dtor_decl = CXXDestructorDecl::Create (*ast,
1773 cxx_record_decl,
1774 SourceLocation(),
1775 DeclarationNameInfo (ast->DeclarationNames.getCXXDestructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1776 method_qual_type,
1777 NULL,
1778 is_inline,
1779 is_artificial);
1780 cxx_method_decl = cxx_dtor_decl;
Greg Clayton878eaf12010-10-01 03:45:20 +00001781 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001782 else if (decl_name == cxx_record_decl->getDeclName())
Greg Clayton878eaf12010-10-01 03:45:20 +00001783 {
Sean Callanan6447c472013-03-30 03:06:45 +00001784 if (is_artificial && method_function_prototype->getNumArgs() == 1)
1785 return NULL; // skip artificial copy constructors
1786
Sean Callanandbb58392011-11-02 01:38:59 +00001787 cxx_ctor_decl = CXXConstructorDecl::Create (*ast,
1788 cxx_record_decl,
1789 SourceLocation(),
1790 DeclarationNameInfo (ast->DeclarationNames.getCXXConstructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1791 method_qual_type,
1792 NULL, // TypeSourceInfo *
1793 is_explicit,
1794 is_inline,
1795 is_artificial,
1796 false /*is_constexpr*/);
1797 cxx_method_decl = cxx_ctor_decl;
Greg Claytonf51de672010-10-01 02:31:07 +00001798 }
1799 else
Greg Clayton878eaf12010-10-01 03:45:20 +00001800 {
Ashok Thirumurthib32f6bf2013-04-17 21:36:33 +00001801 clang::StorageClass SC = is_static ? SC_Static : SC_None;
Greg Claytona3c444a2010-10-01 23:13:49 +00001802 OverloadedOperatorKind op_kind = NUM_OVERLOADED_OPERATORS;
Ashok Thirumurthib32f6bf2013-04-17 21:36:33 +00001803
Greg Claytona3c444a2010-10-01 23:13:49 +00001804 if (IsOperator (name, op_kind))
Greg Clayton878eaf12010-10-01 03:45:20 +00001805 {
Greg Claytona3c444a2010-10-01 23:13:49 +00001806 if (op_kind != NUM_OVERLOADED_OPERATORS)
1807 {
Greg Clayton090d0982011-06-19 03:43:27 +00001808 // Check the number of operator parameters. Sometimes we have
1809 // seen bad DWARF that doesn't correctly describe operators and
1810 // if we try to create a methed and add it to the class, clang
1811 // will assert and crash, so we need to make sure things are
1812 // acceptable.
1813 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
1814 return NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001815 cxx_method_decl = CXXMethodDecl::Create (*ast,
Greg Clayton878eaf12010-10-01 03:45:20 +00001816 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001817 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001818 DeclarationNameInfo (ast->DeclarationNames.getCXXOperatorName (op_kind), SourceLocation()),
Greg Clayton878eaf12010-10-01 03:45:20 +00001819 method_qual_type,
1820 NULL, // TypeSourceInfo *
Ashok Thirumurthib32f6bf2013-04-17 21:36:33 +00001821 SC,
Sean Callananfb0b7582011-03-15 00:17:19 +00001822 is_inline,
Sean Callanan880e6802011-10-07 23:18:13 +00001823 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001824 SourceLocation());
Greg Claytona3c444a2010-10-01 23:13:49 +00001825 }
1826 else if (num_params == 0)
1827 {
1828 // Conversion operators don't take params...
Greg Clayton6beaaa62011-01-17 03:46:26 +00001829 cxx_method_decl = CXXConversionDecl::Create (*ast,
Greg Claytona3c444a2010-10-01 23:13:49 +00001830 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001831 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001832 DeclarationNameInfo (ast->DeclarationNames.getCXXConversionFunctionName (ast->getCanonicalType (function_Type->getResultType())), SourceLocation()),
Greg Claytona3c444a2010-10-01 23:13:49 +00001833 method_qual_type,
1834 NULL, // TypeSourceInfo *
1835 is_inline,
Sean Callananfb0b7582011-03-15 00:17:19 +00001836 is_explicit,
Sean Callanan880e6802011-10-07 23:18:13 +00001837 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001838 SourceLocation());
Greg Claytona3c444a2010-10-01 23:13:49 +00001839 }
Greg Clayton878eaf12010-10-01 03:45:20 +00001840 }
Greg Claytona3c444a2010-10-01 23:13:49 +00001841
1842 if (cxx_method_decl == NULL)
Greg Clayton878eaf12010-10-01 03:45:20 +00001843 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001844 cxx_method_decl = CXXMethodDecl::Create (*ast,
Greg Clayton878eaf12010-10-01 03:45:20 +00001845 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001846 SourceLocation(),
Greg Claytona3c444a2010-10-01 23:13:49 +00001847 DeclarationNameInfo (decl_name, SourceLocation()),
Greg Clayton878eaf12010-10-01 03:45:20 +00001848 method_qual_type,
1849 NULL, // TypeSourceInfo *
Ashok Thirumurthib32f6bf2013-04-17 21:36:33 +00001850 SC,
Sean Callananfb0b7582011-03-15 00:17:19 +00001851 is_inline,
Sean Callanan880e6802011-10-07 23:18:13 +00001852 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001853 SourceLocation());
Greg Clayton878eaf12010-10-01 03:45:20 +00001854 }
Greg Claytonf51de672010-10-01 02:31:07 +00001855 }
Greg Claytona3c444a2010-10-01 23:13:49 +00001856
Greg Clayton1be10fc2010-09-29 01:12:09 +00001857 AccessSpecifier access_specifier = ConvertAccessTypeToAccessSpecifier (access);
Greg Clayton0fffff52010-09-24 05:15:53 +00001858
1859 cxx_method_decl->setAccess (access_specifier);
1860 cxx_method_decl->setVirtualAsWritten (is_virtual);
Sean Callanane2ef6e32010-09-23 03:01:22 +00001861
Sean Callananc1b732d2011-11-01 18:07:13 +00001862 if (is_attr_used)
1863 cxx_method_decl->addAttr(::new (*ast) UsedAttr(SourceRange(), *ast));
1864
Sean Callananfc55f5d2010-09-21 00:44:12 +00001865 // Populate the method decl with parameter decls
Sean Callananfc55f5d2010-09-21 00:44:12 +00001866
Charles Davis8c444c42011-05-19 23:33:46 +00001867 llvm::SmallVector<ParmVarDecl *, 12> params;
Sean Callananfc55f5d2010-09-21 00:44:12 +00001868
1869 for (int param_index = 0;
1870 param_index < num_params;
1871 ++param_index)
1872 {
Charles Davis8c444c42011-05-19 23:33:46 +00001873 params.push_back (ParmVarDecl::Create (*ast,
1874 cxx_method_decl,
1875 SourceLocation(),
1876 SourceLocation(),
1877 NULL, // anonymous
1878 method_function_prototype->getArgType(param_index),
1879 NULL,
1880 SC_None,
Charles Davis8c444c42011-05-19 23:33:46 +00001881 NULL));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001882 }
1883
Sean Callanan880e6802011-10-07 23:18:13 +00001884 cxx_method_decl->setParams (ArrayRef<ParmVarDecl*>(params));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001885
Greg Clayton0fffff52010-09-24 05:15:53 +00001886 cxx_record_decl->addDecl (cxx_method_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001887
Greg Clayton8b867b42011-11-02 02:06:20 +00001888 // Sometimes the debug info will mention a constructor (default/copy/move),
1889 // destructor, or assignment operator (copy/move) but there won't be any
1890 // version of this in the code. So we check if the function was artificially
1891 // generated and if it is trivial and this lets the compiler/backend know
1892 // that it can inline the IR for these when it needs to and we can avoid a
1893 // "missing function" error when running expressions.
1894
Sean Callanandbb58392011-11-02 01:38:59 +00001895 if (is_artificial)
1896 {
Greg Clayton8b867b42011-11-02 02:06:20 +00001897 if (cxx_ctor_decl &&
1898 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
1899 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
1900 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
Sean Callanandbb58392011-11-02 01:38:59 +00001901 {
1902 cxx_ctor_decl->setDefaulted();
1903 cxx_ctor_decl->setTrivial(true);
1904 }
Greg Clayton8b867b42011-11-02 02:06:20 +00001905 else if (cxx_dtor_decl)
Sean Callanandbb58392011-11-02 01:38:59 +00001906 {
Greg Clayton8b867b42011-11-02 02:06:20 +00001907 if (cxx_record_decl->hasTrivialDestructor())
1908 {
1909 cxx_dtor_decl->setDefaulted();
1910 cxx_dtor_decl->setTrivial(true);
1911 }
1912 }
1913 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
1914 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
1915 {
1916 cxx_method_decl->setDefaulted();
1917 cxx_method_decl->setTrivial(true);
Sean Callanandbb58392011-11-02 01:38:59 +00001918 }
1919 }
1920
Sean Callanan5e9e1992011-10-26 01:06:27 +00001921#ifdef LLDB_CONFIGURATION_DEBUG
1922 VerifyDecl(cxx_method_decl);
1923#endif
Greg Claytonc432c192011-01-20 04:18:48 +00001924
1925// printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
1926// printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
1927// printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
1928// printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
1929// printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
1930// printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
1931// printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
1932// printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
1933// printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
Greg Claytona51ed9b2010-09-23 01:09:21 +00001934 return cxx_method_decl;
Sean Callanan61da09b2010-09-17 02:58:26 +00001935}
1936
Jim Inghame3ae82a2011-11-12 01:36:43 +00001937clang::FieldDecl *
Greg Clayton8cf05932010-07-22 18:30:50 +00001938ClangASTContext::AddFieldToRecordType
1939(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001940 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001941 clang_type_t record_clang_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00001942 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001943 clang_type_t field_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00001944 AccessType access,
1945 uint32_t bitfield_bit_size
1946)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001947{
1948 if (record_clang_type == NULL || field_type == NULL)
Jim Inghame3ae82a2011-11-12 01:36:43 +00001949 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001950
Jim Inghame3ae82a2011-11-12 01:36:43 +00001951 FieldDecl *field = NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001952 IdentifierTable *identifier_table = &ast->Idents;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001953
Greg Clayton6beaaa62011-01-17 03:46:26 +00001954 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001955 assert (identifier_table != NULL);
1956
1957 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
1958
Sean Callanan78e37602011-01-27 04:42:51 +00001959 const clang::Type *clang_type = record_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001960 if (clang_type)
1961 {
1962 const RecordType *record_type = dyn_cast<RecordType>(clang_type);
1963
1964 if (record_type)
1965 {
1966 RecordDecl *record_decl = record_type->getDecl();
1967
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001968 clang::Expr *bit_width = NULL;
1969 if (bitfield_bit_size != 0)
1970 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001971 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
1972 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001973 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001974 field = FieldDecl::Create (*ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001975 record_decl,
1976 SourceLocation(),
1977 SourceLocation(),
1978 name ? &identifier_table->get(name) : NULL, // Identifier
1979 QualType::getFromOpaquePtr(field_type), // Field type
1980 NULL, // TInfo *
1981 bit_width, // BitWidth
1982 false, // Mutable
1983 ICIS_NoInit); // HasInit
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001984
Sean Callanan5ed3ac12012-07-13 20:01:02 +00001985 if (!name) {
1986 // Determine whether this field corresponds to an anonymous
1987 // struct or union.
1988 if (const TagType *TagT = field->getType()->getAs<TagType>()) {
1989 if (RecordDecl *Rec = dyn_cast<RecordDecl>(TagT->getDecl()))
1990 if (!Rec->getDeclName()) {
1991 Rec->setAnonymousStructOrUnion(true);
1992 field->setImplicit();
1993
1994 }
1995 }
1996 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001997
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001998 if (field)
1999 {
Greg Clayton973b6c92013-04-11 16:57:51 +00002000 field->setAccess (ConvertAccessTypeToAccessSpecifier (access));
2001
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002002 record_decl->addDecl(field);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002003
2004#ifdef LLDB_CONFIGURATION_DEBUG
2005 VerifyDecl(field);
2006#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002007 }
2008 }
Greg Clayton9e409562010-07-28 02:04:09 +00002009 else
2010 {
Sean Callanan78e37602011-01-27 04:42:51 +00002011 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00002012 if (objc_class_type)
2013 {
Greg Clayton0fffff52010-09-24 05:15:53 +00002014 bool is_synthesized = false;
Jim Inghame3ae82a2011-11-12 01:36:43 +00002015 field = ClangASTContext::AddObjCClassIVar (ast,
Greg Clayton973b6c92013-04-11 16:57:51 +00002016 record_clang_type,
2017 name,
2018 field_type,
2019 access,
2020 bitfield_bit_size,
2021 is_synthesized);
Greg Clayton9e409562010-07-28 02:04:09 +00002022 }
2023 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002024 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002025 return field;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002026}
2027
Greg Clayton973b6c92013-04-11 16:57:51 +00002028clang::VarDecl *
2029ClangASTContext::AddVariableToRecordType (clang::ASTContext *ast,
2030 lldb::clang_type_t record_opaque_type,
2031 const char *name,
2032 lldb::clang_type_t var_type,
2033 AccessType access)
2034{
2035 clang::VarDecl *var_decl = NULL;
2036
2037 if (record_opaque_type == NULL || var_type == NULL)
2038 return NULL;
2039
2040 IdentifierTable *identifier_table = &ast->Idents;
2041
2042 assert (ast != NULL);
2043 assert (identifier_table != NULL);
2044
2045 const RecordType *record_type = dyn_cast<RecordType>(QualType::getFromOpaquePtr(record_opaque_type).getTypePtr());
2046
2047 if (record_type)
2048 {
2049 RecordDecl *record_decl = record_type->getDecl();
2050
2051 var_decl = VarDecl::Create (*ast, // ASTContext &
2052 record_decl, // DeclContext *
2053 SourceLocation(), // SourceLocation StartLoc
2054 SourceLocation(), // SourceLocation IdLoc
2055 name ? &identifier_table->get(name) : NULL, // IdentifierInfo *
2056 QualType::getFromOpaquePtr(var_type), // Variable QualType
2057 NULL, // TypeSourceInfo *
2058 SC_Static); // StorageClass
2059 if (var_decl)
2060 {
2061 var_decl->setAccess(ConvertAccessTypeToAccessSpecifier (access));
2062 record_decl->addDecl(var_decl);
2063
2064#ifdef LLDB_CONFIGURATION_DEBUG
2065 VerifyDecl(var_decl);
2066#endif
2067 }
2068 }
2069 return var_decl;
2070}
2071
2072
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002073static clang::AccessSpecifier UnifyAccessSpecifiers (clang::AccessSpecifier lhs,
2074 clang::AccessSpecifier rhs)
2075{
2076 clang::AccessSpecifier ret = lhs;
2077
2078 // Make the access equal to the stricter of the field and the nested field's access
2079 switch (ret)
2080 {
2081 case clang::AS_none:
2082 break;
2083 case clang::AS_private:
2084 break;
2085 case clang::AS_protected:
2086 if (rhs == AS_private)
2087 ret = AS_private;
2088 break;
2089 case clang::AS_public:
2090 ret = rhs;
2091 break;
2092 }
2093
2094 return ret;
2095}
2096
2097void
2098ClangASTContext::BuildIndirectFields (clang::ASTContext *ast,
2099 lldb::clang_type_t record_clang_type)
2100{
2101 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
2102
2103 const RecordType *record_type = record_qual_type->getAs<RecordType>();
2104
2105 if (!record_type)
2106 return;
2107
2108 RecordDecl *record_decl = record_type->getDecl();
2109
2110 if (!record_decl)
2111 return;
2112
2113 typedef llvm::SmallVector <IndirectFieldDecl *, 1> IndirectFieldVector;
2114
2115 IndirectFieldVector indirect_fields;
Greg Clayton4ef877f2012-12-06 02:33:54 +00002116 RecordDecl::field_iterator field_pos;
2117 RecordDecl::field_iterator field_end_pos = record_decl->field_end();
2118 RecordDecl::field_iterator last_field_pos = field_end_pos;
2119 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; last_field_pos = field_pos++)
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002120 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00002121 if (field_pos->isAnonymousStructOrUnion())
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002122 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00002123 QualType field_qual_type = field_pos->getType();
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002124
2125 const RecordType *field_record_type = field_qual_type->getAs<RecordType>();
2126
2127 if (!field_record_type)
2128 continue;
2129
2130 RecordDecl *field_record_decl = field_record_type->getDecl();
2131
2132 if (!field_record_decl)
2133 continue;
2134
2135 for (RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
2136 di != de;
2137 ++di)
2138 {
2139 if (FieldDecl *nested_field_decl = dyn_cast<FieldDecl>(*di))
2140 {
2141 NamedDecl **chain = new (*ast) NamedDecl*[2];
Greg Clayton4ef877f2012-12-06 02:33:54 +00002142 chain[0] = *field_pos;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002143 chain[1] = nested_field_decl;
2144 IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*ast,
2145 record_decl,
2146 SourceLocation(),
2147 nested_field_decl->getIdentifier(),
2148 nested_field_decl->getType(),
2149 chain,
2150 2);
2151
Greg Clayton4ef877f2012-12-06 02:33:54 +00002152 indirect_field->setAccess(UnifyAccessSpecifiers(field_pos->getAccess(),
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002153 nested_field_decl->getAccess()));
2154
2155 indirect_fields.push_back(indirect_field);
2156 }
2157 else if (IndirectFieldDecl *nested_indirect_field_decl = dyn_cast<IndirectFieldDecl>(*di))
2158 {
2159 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
2160 NamedDecl **chain = new (*ast) NamedDecl*[nested_chain_size + 1];
Greg Clayton4ef877f2012-12-06 02:33:54 +00002161 chain[0] = *field_pos;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002162
2163 int chain_index = 1;
2164 for (IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
2165 nce = nested_indirect_field_decl->chain_end();
2166 nci < nce;
2167 ++nci)
2168 {
2169 chain[chain_index] = *nci;
2170 chain_index++;
2171 }
2172
2173 IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*ast,
2174 record_decl,
2175 SourceLocation(),
2176 nested_indirect_field_decl->getIdentifier(),
2177 nested_indirect_field_decl->getType(),
2178 chain,
2179 nested_chain_size + 1);
2180
Greg Clayton4ef877f2012-12-06 02:33:54 +00002181 indirect_field->setAccess(UnifyAccessSpecifiers(field_pos->getAccess(),
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002182 nested_indirect_field_decl->getAccess()));
2183
2184 indirect_fields.push_back(indirect_field);
2185 }
2186 }
2187 }
2188 }
2189
Greg Clayton4ef877f2012-12-06 02:33:54 +00002190 // Check the last field to see if it has an incomplete array type as its
2191 // last member and if it does, the tell the record decl about it
2192 if (last_field_pos != field_end_pos)
2193 {
2194 if (last_field_pos->getType()->isIncompleteArrayType())
2195 record_decl->hasFlexibleArrayMember();
2196 }
2197
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002198 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
2199 ifi < ife;
2200 ++ifi)
2201 {
2202 record_decl->addDecl(*ifi);
2203 }
2204}
2205
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002206bool
2207ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
2208{
2209 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
2210}
2211
2212bool
2213ClangASTContext::FieldIsBitfield
2214(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002215 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002216 FieldDecl* field,
2217 uint32_t& bitfield_bit_size
2218)
2219{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002220 if (ast == NULL || field == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002221 return false;
2222
2223 if (field->isBitField())
2224 {
2225 Expr* bit_width_expr = field->getBitWidth();
2226 if (bit_width_expr)
2227 {
2228 llvm::APSInt bit_width_apsint;
Greg Clayton6beaaa62011-01-17 03:46:26 +00002229 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002230 {
2231 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
2232 return true;
2233 }
2234 }
2235 }
2236 return false;
2237}
2238
2239bool
2240ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
2241{
2242 if (record_decl == NULL)
2243 return false;
2244
2245 if (!record_decl->field_empty())
2246 return true;
2247
2248 // No fields, lets check this is a CXX record and check the base classes
2249 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2250 if (cxx_record_decl)
2251 {
2252 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2253 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2254 base_class != base_class_end;
2255 ++base_class)
2256 {
2257 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2258 if (RecordHasFields(base_class_decl))
2259 return true;
2260 }
2261 }
2262 return false;
2263}
2264
2265void
Greg Clayton6beaaa62011-01-17 03:46:26 +00002266ClangASTContext::SetDefaultAccessForRecordFields (clang_type_t clang_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002267{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002268 if (clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002269 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002270 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
2271
Sean Callanan78e37602011-01-27 04:42:51 +00002272 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +00002273 if (record_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002274 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002275 RecordDecl *record_decl = record_type->getDecl();
2276 if (record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002277 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002278 uint32_t field_idx;
2279 RecordDecl::field_iterator field, field_end;
2280 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2281 field != field_end;
2282 ++field, ++field_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002283 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002284 // If no accessibility was assigned, assign the correct one
2285 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2286 field->setAccess ((AccessSpecifier)default_accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002287 }
2288 }
2289 }
2290 }
2291}
2292
2293#pragma mark C++ Base Classes
2294
2295CXXBaseSpecifier *
Greg Clayton1be10fc2010-09-29 01:12:09 +00002296ClangASTContext::CreateBaseClassSpecifier (clang_type_t base_class_type, AccessType access, bool is_virtual, bool base_of_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002297{
2298 if (base_class_type)
Greg Claytone6371122010-07-30 20:30:44 +00002299 return new CXXBaseSpecifier (SourceRange(),
2300 is_virtual,
2301 base_of_class,
2302 ConvertAccessTypeToAccessSpecifier (access),
Sean Callanan34cf8202013-03-12 21:22:00 +00002303 getASTContext()->getTrivialTypeSourceInfo (QualType::getFromOpaquePtr(base_class_type)),
Sean Callanan2c777c42011-01-18 23:32:05 +00002304 SourceLocation());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002305 return NULL;
2306}
2307
Greg Clayton0b42ac32010-07-02 01:29:13 +00002308void
2309ClangASTContext::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes)
2310{
2311 for (unsigned i=0; i<num_base_classes; ++i)
2312 {
2313 delete base_classes[i];
2314 base_classes[i] = NULL;
2315 }
2316}
2317
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002318bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002319ClangASTContext::SetBaseClassesForClassType (clang_type_t class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002320{
2321 if (class_clang_type)
2322 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002323 CXXRecordDecl *cxx_record_decl = QualType::getFromOpaquePtr(class_clang_type)->getAsCXXRecordDecl();
2324 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002325 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002326 cxx_record_decl->setBases(base_classes, num_base_classes);
2327 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002328 }
2329 }
2330 return false;
2331}
Greg Clayton8cf05932010-07-22 18:30:50 +00002332#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002333
Greg Clayton1be10fc2010-09-29 01:12:09 +00002334clang_type_t
Sean Callananad880762012-04-18 01:06:17 +00002335ClangASTContext::CreateObjCClass
Greg Clayton8cf05932010-07-22 18:30:50 +00002336(
2337 const char *name,
2338 DeclContext *decl_ctx,
2339 bool isForwardDecl,
Sean Callananad880762012-04-18 01:06:17 +00002340 bool isInternal,
Jim Ingham379397632012-10-27 02:54:13 +00002341 ClangASTMetadata *metadata
Greg Clayton8cf05932010-07-22 18:30:50 +00002342)
2343{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002344 ASTContext *ast = getASTContext();
2345 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00002346 assert (name && name[0]);
2347 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00002348 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00002349
2350 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
2351 // we will need to update this code. I was told to currently always use
2352 // the CXXRecordDecl class since we often don't know from debug information
2353 // if something is struct or a class, so we default to always use the more
2354 // complete definition just in case.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002355 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00002356 decl_ctx,
2357 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00002358 &ast->Idents.get(name),
Sean Callanan5b26f272012-02-04 08:49:35 +00002359 NULL,
Greg Clayton8cf05932010-07-22 18:30:50 +00002360 SourceLocation(),
Sean Callanan5b26f272012-02-04 08:49:35 +00002361 /*isForwardDecl,*/
Greg Clayton8cf05932010-07-22 18:30:50 +00002362 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00002363
Jim Ingham379397632012-10-27 02:54:13 +00002364 if (decl && metadata)
Greg Claytond0029442013-03-27 01:48:02 +00002365 SetMetadata(ast, decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00002366
Greg Clayton6beaaa62011-01-17 03:46:26 +00002367 return ast->getObjCInterfaceType(decl).getAsOpaquePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002368}
2369
2370bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002371ClangASTContext::SetObjCSuperClass (clang_type_t class_opaque_type, clang_type_t super_opaque_type)
Greg Clayton8cf05932010-07-22 18:30:50 +00002372{
2373 if (class_opaque_type && super_opaque_type)
2374 {
2375 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2376 QualType super_qual_type(QualType::getFromOpaquePtr(super_opaque_type));
Sean Callanan78e37602011-01-27 04:42:51 +00002377 const clang::Type *class_type = class_qual_type.getTypePtr();
2378 const clang::Type *super_type = super_qual_type.getTypePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002379 if (class_type && super_type)
2380 {
Sean Callanan78e37602011-01-27 04:42:51 +00002381 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
2382 const ObjCObjectType *objc_super_type = dyn_cast<ObjCObjectType>(super_type);
Greg Clayton8cf05932010-07-22 18:30:50 +00002383 if (objc_class_type && objc_super_type)
2384 {
2385 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2386 ObjCInterfaceDecl *super_interface_decl = objc_super_type->getInterface();
2387 if (class_interface_decl && super_interface_decl)
2388 {
2389 class_interface_decl->setSuperClass(super_interface_decl);
2390 return true;
2391 }
2392 }
2393 }
2394 }
2395 return false;
2396}
2397
2398
Jim Inghame3ae82a2011-11-12 01:36:43 +00002399FieldDecl *
Greg Clayton8cf05932010-07-22 18:30:50 +00002400ClangASTContext::AddObjCClassIVar
2401(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002402 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002403 clang_type_t class_opaque_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00002404 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002405 clang_type_t ivar_opaque_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00002406 AccessType access,
2407 uint32_t bitfield_bit_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00002408 bool is_synthesized
Greg Clayton8cf05932010-07-22 18:30:50 +00002409)
2410{
2411 if (class_opaque_type == NULL || ivar_opaque_type == NULL)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002412 return NULL;
Greg Clayton8cf05932010-07-22 18:30:50 +00002413
Jim Inghame3ae82a2011-11-12 01:36:43 +00002414 ObjCIvarDecl *field = NULL;
2415
Greg Clayton6beaaa62011-01-17 03:46:26 +00002416 IdentifierTable *identifier_table = &ast->Idents;
Greg Clayton8cf05932010-07-22 18:30:50 +00002417
Greg Clayton6beaaa62011-01-17 03:46:26 +00002418 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00002419 assert (identifier_table != NULL);
2420
2421 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2422
Sean Callanan78e37602011-01-27 04:42:51 +00002423 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002424 if (class_type)
2425 {
Sean Callanan78e37602011-01-27 04:42:51 +00002426 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton8cf05932010-07-22 18:30:50 +00002427
2428 if (objc_class_type)
2429 {
2430 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2431
2432 if (class_interface_decl)
2433 {
2434 clang::Expr *bit_width = NULL;
2435 if (bitfield_bit_size != 0)
2436 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002437 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
2438 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
Greg Clayton8cf05932010-07-22 18:30:50 +00002439 }
2440
Jim Inghame3ae82a2011-11-12 01:36:43 +00002441 field = ObjCIvarDecl::Create (*ast,
2442 class_interface_decl,
2443 SourceLocation(),
2444 SourceLocation(),
Greg Clayton88bc7f32012-11-06 00:20:41 +00002445 name ? &identifier_table->get(name) : NULL, // Identifier
Jim Inghame3ae82a2011-11-12 01:36:43 +00002446 QualType::getFromOpaquePtr(ivar_opaque_type), // Field type
2447 NULL, // TypeSourceInfo *
2448 ConvertAccessTypeToObjCIvarAccessControl (access),
2449 bit_width,
2450 is_synthesized);
Greg Clayton9e409562010-07-28 02:04:09 +00002451
2452 if (field)
2453 {
2454 class_interface_decl->addDecl(field);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002455
2456#ifdef LLDB_CONFIGURATION_DEBUG
2457 VerifyDecl(field);
2458#endif
2459
Jim Inghame3ae82a2011-11-12 01:36:43 +00002460 return field;
Greg Clayton9e409562010-07-28 02:04:09 +00002461 }
Greg Clayton8cf05932010-07-22 18:30:50 +00002462 }
2463 }
2464 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002465 return NULL;
2466}
2467
2468bool
2469ClangASTContext::AddObjCClassProperty
2470(
2471 ASTContext *ast,
2472 clang_type_t class_opaque_type,
2473 const char *property_name,
2474 clang_type_t property_opaque_type,
2475 ObjCIvarDecl *ivar_decl,
2476 const char *property_setter_name,
2477 const char *property_getter_name,
Sean Callananad880762012-04-18 01:06:17 +00002478 uint32_t property_attributes,
Jim Ingham379397632012-10-27 02:54:13 +00002479 ClangASTMetadata *metadata
Jim Inghame3ae82a2011-11-12 01:36:43 +00002480)
2481{
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002482 if (class_opaque_type == NULL || property_name == NULL || property_name[0] == '\0')
Jim Inghame3ae82a2011-11-12 01:36:43 +00002483 return false;
2484
2485 IdentifierTable *identifier_table = &ast->Idents;
2486
2487 assert (ast != NULL);
2488 assert (identifier_table != NULL);
2489
2490 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2491 const clang::Type *class_type = class_qual_type.getTypePtr();
2492 if (class_type)
2493 {
2494 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
2495
2496 if (objc_class_type)
2497 {
2498 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2499
Greg Clayton23f59502012-07-17 03:23:13 +00002500 clang_type_t property_opaque_type_to_access = NULL;
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002501
2502 if (property_opaque_type)
2503 property_opaque_type_to_access = property_opaque_type;
2504 else if (ivar_decl)
2505 property_opaque_type_to_access = ivar_decl->getType().getAsOpaquePtr();
2506
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002507 if (class_interface_decl && property_opaque_type_to_access)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002508 {
2509 clang::TypeSourceInfo *prop_type_source;
2510 if (ivar_decl)
Sean Callanan34cf8202013-03-12 21:22:00 +00002511 prop_type_source = ast->getTrivialTypeSourceInfo (ivar_decl->getType());
Jim Inghame3ae82a2011-11-12 01:36:43 +00002512 else
Sean Callanan34cf8202013-03-12 21:22:00 +00002513 prop_type_source = ast->getTrivialTypeSourceInfo (QualType::getFromOpaquePtr(property_opaque_type));
Jim Inghame3ae82a2011-11-12 01:36:43 +00002514
2515 ObjCPropertyDecl *property_decl = ObjCPropertyDecl::Create(*ast,
2516 class_interface_decl,
2517 SourceLocation(), // Source Location
2518 &identifier_table->get(property_name),
2519 SourceLocation(), //Source Location for AT
Sean Callanand5f33a82012-03-01 02:03:47 +00002520 SourceLocation(), //Source location for (
Jim Inghame3ae82a2011-11-12 01:36:43 +00002521 prop_type_source
2522 );
Sean Callananad880762012-04-18 01:06:17 +00002523
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002524 if (property_decl)
2525 {
Jim Ingham379397632012-10-27 02:54:13 +00002526 if (metadata)
Greg Claytond0029442013-03-27 01:48:02 +00002527 SetMetadata(ast, property_decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00002528
Jim Inghame3ae82a2011-11-12 01:36:43 +00002529 class_interface_decl->addDecl (property_decl);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002530
2531 Selector setter_sel, getter_sel;
2532
Jim Inghame3ae82a2011-11-12 01:36:43 +00002533 if (property_setter_name != NULL)
2534 {
2535 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
2536 clang::IdentifierInfo *setter_ident = &identifier_table->get(property_setter_no_colon.c_str());
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002537 setter_sel = ast->Selectors.getSelector(1, &setter_ident);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002538 }
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002539 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
2540 {
2541 std::string setter_sel_string("set");
2542 setter_sel_string.push_back(::toupper(property_name[0]));
2543 setter_sel_string.append(&property_name[1]);
2544 clang::IdentifierInfo *setter_ident = &identifier_table->get(setter_sel_string.c_str());
2545 setter_sel = ast->Selectors.getSelector(1, &setter_ident);
2546 }
Sean Callanana6582262012-04-05 00:12:52 +00002547 property_decl->setSetterName(setter_sel);
2548 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002549
2550 if (property_getter_name != NULL)
2551 {
2552 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_getter_name);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002553 getter_sel = ast->Selectors.getSelector(0, &getter_ident);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002554 }
2555 else
2556 {
2557 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_name);
2558 getter_sel = ast->Selectors.getSelector(0, &getter_ident);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002559 }
Sean Callanana6582262012-04-05 00:12:52 +00002560 property_decl->setGetterName(getter_sel);
2561 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002562
2563 if (ivar_decl)
2564 property_decl->setPropertyIvarDecl (ivar_decl);
2565
2566 if (property_attributes & DW_APPLE_PROPERTY_readonly)
2567 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
2568 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
2569 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
2570 if (property_attributes & DW_APPLE_PROPERTY_assign)
2571 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
2572 if (property_attributes & DW_APPLE_PROPERTY_retain)
2573 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
2574 if (property_attributes & DW_APPLE_PROPERTY_copy)
2575 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
2576 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
2577 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002578
2579 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
2580 {
2581 QualType result_type = QualType::getFromOpaquePtr(property_opaque_type_to_access);
2582
2583 const bool isInstance = true;
2584 const bool isVariadic = false;
2585 const bool isSynthesized = false;
2586 const bool isImplicitlyDeclared = true;
2587 const bool isDefined = false;
2588 const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
2589 const bool HasRelatedResultType = false;
2590
2591 ObjCMethodDecl *getter = ObjCMethodDecl::Create(*ast,
2592 SourceLocation(),
2593 SourceLocation(),
2594 getter_sel,
2595 result_type,
2596 NULL,
2597 class_interface_decl,
2598 isInstance,
2599 isVariadic,
2600 isSynthesized,
2601 isImplicitlyDeclared,
2602 isDefined,
2603 impControl,
2604 HasRelatedResultType);
Sean Callananad880762012-04-18 01:06:17 +00002605
Jim Ingham379397632012-10-27 02:54:13 +00002606 if (getter && metadata)
Greg Claytond0029442013-03-27 01:48:02 +00002607 SetMetadata(ast, getter, *metadata);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002608
2609 getter->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(), ArrayRef<SourceLocation>());
2610
2611 class_interface_decl->addDecl(getter);
2612 }
2613
2614 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
2615 {
2616 QualType result_type = ast->VoidTy;
2617
2618 const bool isInstance = true;
2619 const bool isVariadic = false;
2620 const bool isSynthesized = false;
2621 const bool isImplicitlyDeclared = true;
2622 const bool isDefined = false;
2623 const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
2624 const bool HasRelatedResultType = false;
2625
2626 ObjCMethodDecl *setter = ObjCMethodDecl::Create(*ast,
2627 SourceLocation(),
2628 SourceLocation(),
2629 setter_sel,
2630 result_type,
2631 NULL,
2632 class_interface_decl,
2633 isInstance,
2634 isVariadic,
2635 isSynthesized,
2636 isImplicitlyDeclared,
2637 isDefined,
2638 impControl,
2639 HasRelatedResultType);
2640
Jim Ingham379397632012-10-27 02:54:13 +00002641 if (setter && metadata)
Greg Claytond0029442013-03-27 01:48:02 +00002642 SetMetadata(ast, setter, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00002643
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002644 llvm::SmallVector<ParmVarDecl *, 1> params;
2645
2646 params.push_back (ParmVarDecl::Create (*ast,
2647 setter,
2648 SourceLocation(),
2649 SourceLocation(),
2650 NULL, // anonymous
2651 QualType::getFromOpaquePtr(property_opaque_type_to_access),
2652 NULL,
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002653 SC_Auto,
2654 NULL));
2655
2656 setter->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
2657
2658 class_interface_decl->addDecl(setter);
2659 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002660
2661 return true;
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002662 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002663 }
2664 }
2665 }
Greg Clayton8cf05932010-07-22 18:30:50 +00002666 return false;
2667}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002668
Greg Clayton9e409562010-07-28 02:04:09 +00002669bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002670ClangASTContext::ObjCTypeHasIVars (clang_type_t class_opaque_type, bool check_superclass)
Greg Clayton9e409562010-07-28 02:04:09 +00002671{
2672 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2673
Sean Callanan78e37602011-01-27 04:42:51 +00002674 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton9e409562010-07-28 02:04:09 +00002675 if (class_type)
2676 {
Sean Callanan78e37602011-01-27 04:42:51 +00002677 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00002678
2679 if (objc_class_type)
2680 return ObjCDeclHasIVars (objc_class_type->getInterface(), check_superclass);
2681 }
2682 return false;
2683}
2684
2685bool
2686ClangASTContext::ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
2687{
2688 while (class_interface_decl)
2689 {
2690 if (class_interface_decl->ivar_size() > 0)
2691 return true;
2692
2693 if (check_superclass)
2694 class_interface_decl = class_interface_decl->getSuperClass();
2695 else
2696 break;
2697 }
2698 return false;
2699}
Greg Clayton0fffff52010-09-24 05:15:53 +00002700
Greg Clayton1be10fc2010-09-29 01:12:09 +00002701ObjCMethodDecl *
Greg Clayton1bbcc032013-03-08 02:42:06 +00002702ClangASTContext::AddMethodToObjCObjectType (ASTContext *ast,
2703 clang_type_t class_opaque_type,
2704 const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
2705 clang_type_t method_opaque_type,
2706 lldb::AccessType access,
2707 bool is_artificial)
Greg Clayton0fffff52010-09-24 05:15:53 +00002708{
2709 if (class_opaque_type == NULL || method_opaque_type == NULL)
2710 return NULL;
2711
Greg Clayton6beaaa62011-01-17 03:46:26 +00002712 IdentifierTable *identifier_table = &ast->Idents;
Greg Clayton0fffff52010-09-24 05:15:53 +00002713
Greg Clayton6beaaa62011-01-17 03:46:26 +00002714 assert (ast != NULL);
Greg Clayton0fffff52010-09-24 05:15:53 +00002715 assert (identifier_table != NULL);
2716
2717 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2718
Sean Callanan78e37602011-01-27 04:42:51 +00002719 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton0fffff52010-09-24 05:15:53 +00002720 if (class_type == NULL)
2721 return NULL;
2722
Sean Callanan78e37602011-01-27 04:42:51 +00002723 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton0fffff52010-09-24 05:15:53 +00002724
2725 if (objc_class_type == NULL)
2726 return NULL;
2727
2728 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2729
2730 if (class_interface_decl == NULL)
2731 return NULL;
Greg Clayton9e409562010-07-28 02:04:09 +00002732
Greg Clayton0fffff52010-09-24 05:15:53 +00002733 const char *selector_start = ::strchr (name, ' ');
2734 if (selector_start == NULL)
2735 return NULL;
2736
2737 selector_start++;
Greg Clayton0fffff52010-09-24 05:15:53 +00002738 llvm::SmallVector<IdentifierInfo *, 12> selector_idents;
2739
Greg Clayton450e3f32010-10-12 02:24:53 +00002740 size_t len = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +00002741 const char *start;
Greg Clayton450e3f32010-10-12 02:24:53 +00002742 //printf ("name = '%s'\n", name);
2743
2744 unsigned num_selectors_with_args = 0;
2745 for (start = selector_start;
Greg Clayton0fffff52010-09-24 05:15:53 +00002746 start && *start != '\0' && *start != ']';
Greg Clayton450e3f32010-10-12 02:24:53 +00002747 start += len)
Greg Clayton0fffff52010-09-24 05:15:53 +00002748 {
Greg Clayton450e3f32010-10-12 02:24:53 +00002749 len = ::strcspn(start, ":]");
Greg Clayton90f90cd2010-10-27 04:01:14 +00002750 bool has_arg = (start[len] == ':');
2751 if (has_arg)
Greg Clayton450e3f32010-10-12 02:24:53 +00002752 ++num_selectors_with_args;
Greg Clayton0fffff52010-09-24 05:15:53 +00002753 selector_idents.push_back (&identifier_table->get (StringRef (start, len)));
Greg Clayton90f90cd2010-10-27 04:01:14 +00002754 if (has_arg)
2755 len += 1;
Greg Clayton0fffff52010-09-24 05:15:53 +00002756 }
2757
2758
2759 if (selector_idents.size() == 0)
2760 return 0;
2761
Greg Clayton6beaaa62011-01-17 03:46:26 +00002762 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
Greg Clayton0fffff52010-09-24 05:15:53 +00002763 selector_idents.data());
2764
2765 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
2766
2767 // Populate the method decl with parameter decls
Sean Callanan78e37602011-01-27 04:42:51 +00002768 const clang::Type *method_type(method_qual_type.getTypePtr());
Greg Clayton0fffff52010-09-24 05:15:53 +00002769
2770 if (method_type == NULL)
2771 return NULL;
2772
Sean Callanan78e37602011-01-27 04:42:51 +00002773 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type));
Greg Clayton0fffff52010-09-24 05:15:53 +00002774
2775 if (!method_function_prototype)
2776 return NULL;
2777
2778
2779 bool is_variadic = false;
2780 bool is_synthesized = false;
2781 bool is_defined = false;
2782 ObjCMethodDecl::ImplementationControl imp_control = ObjCMethodDecl::None;
2783
2784 const unsigned num_args = method_function_prototype->getNumArgs();
Sean Callananc3a1d562013-02-06 23:21:59 +00002785
2786 if (num_args != num_selectors_with_args)
2787 return NULL; // some debug information is corrupt. We are not going to deal with it.
Greg Clayton0fffff52010-09-24 05:15:53 +00002788
Greg Clayton6beaaa62011-01-17 03:46:26 +00002789 ObjCMethodDecl *objc_method_decl = ObjCMethodDecl::Create (*ast,
Greg Clayton0fffff52010-09-24 05:15:53 +00002790 SourceLocation(), // beginLoc,
2791 SourceLocation(), // endLoc,
2792 method_selector,
2793 method_function_prototype->getResultType(),
2794 NULL, // TypeSourceInfo *ResultTInfo,
2795 GetDeclContextForType (class_opaque_type),
2796 name[0] == '-',
2797 is_variadic,
2798 is_synthesized,
Sean Callanan6c9265b2013-03-09 01:59:31 +00002799 true, // is_implicitly_declared; we force this to true because we don't have source locations
Greg Clayton0fffff52010-09-24 05:15:53 +00002800 is_defined,
2801 imp_control,
Sean Callanan880e6802011-10-07 23:18:13 +00002802 false /*has_related_result_type*/);
Greg Clayton0fffff52010-09-24 05:15:53 +00002803
2804
2805 if (objc_method_decl == NULL)
2806 return NULL;
2807
2808 if (num_args > 0)
2809 {
2810 llvm::SmallVector<ParmVarDecl *, 12> params;
2811
2812 for (int param_index = 0; param_index < num_args; ++param_index)
2813 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002814 params.push_back (ParmVarDecl::Create (*ast,
Greg Clayton0fffff52010-09-24 05:15:53 +00002815 objc_method_decl,
2816 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00002817 SourceLocation(),
Greg Clayton0fffff52010-09-24 05:15:53 +00002818 NULL, // anonymous
2819 method_function_prototype->getArgType(param_index),
2820 NULL,
Greg Clayton0fffff52010-09-24 05:15:53 +00002821 SC_Auto,
2822 NULL));
2823 }
2824
Sean Callanan880e6802011-10-07 23:18:13 +00002825 objc_method_decl->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
Greg Clayton0fffff52010-09-24 05:15:53 +00002826 }
2827
2828 class_interface_decl->addDecl (objc_method_decl);
2829
Sean Callanan5e9e1992011-10-26 01:06:27 +00002830#ifdef LLDB_CONFIGURATION_DEBUG
2831 VerifyDecl(objc_method_decl);
2832#endif
Greg Clayton0fffff52010-09-24 05:15:53 +00002833
2834 return objc_method_decl;
2835}
2836
Greg Clayton402230e2012-02-03 01:30:30 +00002837size_t
2838ClangASTContext::GetNumTemplateArguments (clang::ASTContext *ast, clang_type_t clang_type)
2839{
2840 if (clang_type)
2841 {
2842 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2843
2844 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2845 switch (type_class)
2846 {
2847 case clang::Type::Record:
2848 if (GetCompleteQualType (ast, qual_type))
2849 {
2850 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2851 if (cxx_record_decl)
2852 {
2853 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2854 if (template_decl)
2855 return template_decl->getTemplateArgs().size();
2856 }
2857 }
2858 break;
2859
2860 case clang::Type::Typedef:
2861 return ClangASTContext::GetNumTemplateArguments (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002862
2863 case clang::Type::Elaborated:
2864 return ClangASTContext::GetNumTemplateArguments (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2865
Greg Clayton402230e2012-02-03 01:30:30 +00002866 default:
2867 break;
2868 }
2869 }
2870 return 0;
2871}
2872
2873clang_type_t
2874ClangASTContext::GetTemplateArgument (clang::ASTContext *ast, clang_type_t clang_type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
2875{
2876 if (clang_type)
2877 {
2878 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2879
2880 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2881 switch (type_class)
2882 {
2883 case clang::Type::Record:
2884 if (GetCompleteQualType (ast, qual_type))
2885 {
2886 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2887 if (cxx_record_decl)
2888 {
2889 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2890 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
2891 {
2892 const TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
2893 switch (template_arg.getKind())
2894 {
2895 case clang::TemplateArgument::Null:
2896 kind = eTemplateArgumentKindNull;
2897 return NULL;
2898
2899 case clang::TemplateArgument::Type:
2900 kind = eTemplateArgumentKindType;
2901 return template_arg.getAsType().getAsOpaquePtr();
2902
2903 case clang::TemplateArgument::Declaration:
2904 kind = eTemplateArgumentKindDeclaration;
2905 return NULL;
2906
2907 case clang::TemplateArgument::Integral:
2908 kind = eTemplateArgumentKindIntegral;
2909 return template_arg.getIntegralType().getAsOpaquePtr();
2910
2911 case clang::TemplateArgument::Template:
2912 kind = eTemplateArgumentKindTemplate;
2913 return NULL;
2914
2915 case clang::TemplateArgument::TemplateExpansion:
2916 kind = eTemplateArgumentKindTemplateExpansion;
2917 return NULL;
2918
2919 case clang::TemplateArgument::Expression:
2920 kind = eTemplateArgumentKindExpression;
2921 return NULL;
2922
2923 case clang::TemplateArgument::Pack:
2924 kind = eTemplateArgumentKindPack;
2925 return NULL;
2926
2927 default:
2928 assert (!"Unhandled TemplateArgument::ArgKind");
2929 kind = eTemplateArgumentKindNull;
2930 return NULL;
2931 }
2932 }
2933 }
2934 }
2935 break;
2936
2937 case clang::Type::Typedef:
2938 return ClangASTContext::GetTemplateArgument (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), arg_idx, kind);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002939
2940 case clang::Type::Elaborated:
2941 return ClangASTContext::GetTemplateArgument (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), arg_idx, kind);
2942
Greg Clayton402230e2012-02-03 01:30:30 +00002943 default:
2944 break;
2945 }
2946 }
2947 kind = eTemplateArgumentKindNull;
2948 return NULL;
2949}
Greg Clayton0fffff52010-09-24 05:15:53 +00002950
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002951uint32_t
Greg Clayton73b472d2010-10-27 03:32:59 +00002952ClangASTContext::GetTypeInfo
2953(
2954 clang_type_t clang_type,
Greg Clayton6beaaa62011-01-17 03:46:26 +00002955 clang::ASTContext *ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00002956 clang_type_t *pointee_or_element_clang_type
2957)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002958{
2959 if (clang_type == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00002960 return 0;
2961
2962 if (pointee_or_element_clang_type)
2963 *pointee_or_element_clang_type = NULL;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002964
2965 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2966
2967 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2968 switch (type_class)
2969 {
Sean Callanana2424172010-10-25 00:29:48 +00002970 case clang::Type::Builtin:
Sean Callanana2424172010-10-25 00:29:48 +00002971 {
Greg Clayton1c8ef472013-04-05 23:27:21 +00002972 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal());
Greg Clayton73b472d2010-10-27 03:32:59 +00002973
Greg Clayton1c8ef472013-04-05 23:27:21 +00002974 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
2975 switch (builtin_type->getKind())
2976 {
2977 case clang::BuiltinType::ObjCId:
2978 case clang::BuiltinType::ObjCClass:
2979 if (ast && pointee_or_element_clang_type)
2980 *pointee_or_element_clang_type = ast->ObjCBuiltinClassTy.getAsOpaquePtr();
2981 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
2982 break;
2983
2984 case clang::BuiltinType::ObjCSel:
2985 case clang::BuiltinType::Bool:
2986 case clang::BuiltinType::Char_U:
2987 case clang::BuiltinType::UChar:
2988 case clang::BuiltinType::WChar_U:
2989 case clang::BuiltinType::Char16:
2990 case clang::BuiltinType::Char32:
2991 case clang::BuiltinType::UShort:
2992 case clang::BuiltinType::UInt:
2993 case clang::BuiltinType::ULong:
2994 case clang::BuiltinType::ULongLong:
2995 case clang::BuiltinType::UInt128:
2996 case clang::BuiltinType::Char_S:
2997 case clang::BuiltinType::SChar:
2998 case clang::BuiltinType::WChar_S:
2999 case clang::BuiltinType::Short:
3000 case clang::BuiltinType::Int:
3001 case clang::BuiltinType::Long:
3002 case clang::BuiltinType::LongLong:
3003 case clang::BuiltinType::Int128:
3004 case clang::BuiltinType::Float:
3005 case clang::BuiltinType::Double:
3006 case clang::BuiltinType::LongDouble:
3007 builtin_type_flags |= eTypeIsScalar;
3008 if (builtin_type->isInteger())
3009 {
3010 builtin_type_flags |= eTypeIsInteger;
3011 if (builtin_type->isSignedInteger())
3012 builtin_type_flags |= eTypeIsSigned;
3013 }
3014 else if (builtin_type->isFloatingPoint())
3015 builtin_type_flags |= eTypeIsFloat;
3016 break;
3017 default:
3018 break;
3019 }
3020 return builtin_type_flags;
3021 }
3022
3023 case clang::Type::BlockPointer:
Greg Clayton73b472d2010-10-27 03:32:59 +00003024 if (pointee_or_element_clang_type)
3025 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
3026 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3027
Greg Clayton1c8ef472013-04-05 23:27:21 +00003028 case clang::Type::Complex:
3029 {
3030 uint32_t complex_type_flags = eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3031 const ComplexType *complex_type = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal());
3032 if (complex_type)
3033 {
3034 QualType complex_element_type (complex_type->getElementType());
3035 if (complex_element_type->isIntegerType())
3036 complex_type_flags |= eTypeIsFloat;
3037 else if (complex_element_type->isFloatingType())
3038 complex_type_flags |= eTypeIsInteger;
3039 }
3040 return complex_type_flags;
3041 }
3042 break;
Greg Clayton73b472d2010-10-27 03:32:59 +00003043
3044 case clang::Type::ConstantArray:
3045 case clang::Type::DependentSizedArray:
3046 case clang::Type::IncompleteArray:
3047 case clang::Type::VariableArray:
3048 if (pointee_or_element_clang_type)
3049 *pointee_or_element_clang_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
3050 return eTypeHasChildren | eTypeIsArray;
3051
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003052 case clang::Type::DependentName: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003053 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
3054 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
3055 case clang::Type::Decltype: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00003056
3057 case clang::Type::Enum:
3058 if (pointee_or_element_clang_type)
3059 *pointee_or_element_clang_type = cast<EnumType>(qual_type)->getDecl()->getIntegerType().getAsOpaquePtr();
3060 return eTypeIsEnumeration | eTypeHasValue;
3061
Sean Callanan912855f2011-08-11 23:56:13 +00003062 case clang::Type::Elaborated:
3063 return ClangASTContext::GetTypeInfo (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3064 ast,
3065 pointee_or_element_clang_type);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003066 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
3067 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003068 case clang::Type::InjectedClassName: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00003069
3070 case clang::Type::LValueReference:
3071 case clang::Type::RValueReference:
3072 if (pointee_or_element_clang_type)
3073 *pointee_or_element_clang_type = cast<ReferenceType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr();
3074 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
3075
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003076 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00003077
3078 case clang::Type::ObjCObjectPointer:
3079 if (pointee_or_element_clang_type)
3080 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
3081 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
3082
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003083 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3084 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
Greg Clayton73b472d2010-10-27 03:32:59 +00003085
3086 case clang::Type::Pointer:
3087 if (pointee_or_element_clang_type)
3088 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
3089 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
3090
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003091 case clang::Type::Record:
3092 if (qual_type->getAsCXXRecordDecl())
3093 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
3094 else
3095 return eTypeHasChildren | eTypeIsStructUnion;
3096 break;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003097 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
3098 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
3099 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
Greg Clayton73b472d2010-10-27 03:32:59 +00003100
3101 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00003102 return eTypeIsTypedef | ClangASTContext::GetTypeInfo (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00003103 ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00003104 pointee_or_element_clang_type);
3105
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003106 case clang::Type::TypeOfExpr: return 0;
3107 case clang::Type::TypeOf: return 0;
3108 case clang::Type::UnresolvedUsing: return 0;
Greg Clayton1c8ef472013-04-05 23:27:21 +00003109
3110 case clang::Type::ExtVector:
3111 case clang::Type::Vector:
3112 {
3113 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
3114 const VectorType *vector_type = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal());
3115 if (vector_type)
3116 {
3117 if (vector_type->isIntegerType())
3118 vector_type_flags |= eTypeIsFloat;
3119 else if (vector_type->isFloatingType())
3120 vector_type_flags |= eTypeIsInteger;
3121 }
3122 return vector_type_flags;
3123 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003124 default: return 0;
3125 }
3126 return 0;
3127}
3128
Greg Clayton9e409562010-07-28 02:04:09 +00003129
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003130#pragma mark Aggregate Types
3131
3132bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00003133ClangASTContext::IsAggregateType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003134{
3135 if (clang_type == NULL)
3136 return false;
3137
3138 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3139
Greg Clayton737b9322010-09-13 03:32:57 +00003140 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3141 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003142 {
Greg Claytone1a916a2010-07-21 22:12:05 +00003143 case clang::Type::IncompleteArray:
3144 case clang::Type::VariableArray:
3145 case clang::Type::ConstantArray:
3146 case clang::Type::ExtVector:
3147 case clang::Type::Vector:
3148 case clang::Type::Record:
Greg Clayton9e409562010-07-28 02:04:09 +00003149 case clang::Type::ObjCObject:
3150 case clang::Type::ObjCInterface:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003151 return true;
Sean Callanan912855f2011-08-11 23:56:13 +00003152 case clang::Type::Elaborated:
3153 return ClangASTContext::IsAggregateType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Claytone1a916a2010-07-21 22:12:05 +00003154 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00003155 return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003156
3157 default:
3158 break;
3159 }
3160 // The clang type does have a value
3161 return false;
3162}
3163
3164uint32_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00003165ClangASTContext::GetNumChildren (clang::ASTContext *ast, clang_type_t clang_type, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003166{
Greg Clayton6beaaa62011-01-17 03:46:26 +00003167 if (clang_type == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003168 return 0;
3169
3170 uint32_t num_children = 0;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003171 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00003172 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3173 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003174 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003175 case clang::Type::Builtin:
3176 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3177 {
Greg Clayton73b472d2010-10-27 03:32:59 +00003178 case clang::BuiltinType::ObjCId: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003179 case clang::BuiltinType::ObjCClass: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003180 num_children = 1;
Greg Clayton73b472d2010-10-27 03:32:59 +00003181 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003182
3183 default:
3184 break;
3185 }
3186 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00003187
Greg Clayton49462ea2011-01-15 02:52:14 +00003188 case clang::Type::Complex: return 0;
Greg Clayton54979cd2010-12-15 05:08:08 +00003189
Greg Claytone1a916a2010-07-21 22:12:05 +00003190 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00003191 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003192 {
3193 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3194 const RecordDecl *record_decl = record_type->getDecl();
3195 assert(record_decl);
3196 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3197 if (cxx_record_decl)
3198 {
3199 if (omit_empty_base_classes)
3200 {
3201 // Check each base classes to see if it or any of its
3202 // base classes contain any fields. This can help
3203 // limit the noise in variable views by not having to
3204 // show base classes that contain no members.
3205 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3206 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3207 base_class != base_class_end;
3208 ++base_class)
3209 {
3210 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3211
3212 // Skip empty base classes
3213 if (RecordHasFields(base_class_decl) == false)
3214 continue;
3215
3216 num_children++;
3217 }
3218 }
3219 else
3220 {
3221 // Include all base classes
3222 num_children += cxx_record_decl->getNumBases();
3223 }
3224
3225 }
3226 RecordDecl::field_iterator field, field_end;
3227 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3228 ++num_children;
3229 }
3230 break;
3231
Greg Clayton9e409562010-07-28 02:04:09 +00003232 case clang::Type::ObjCObject:
3233 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00003234 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00003235 {
Sean Callanan78e37602011-01-27 04:42:51 +00003236 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003237 assert (objc_class_type);
3238 if (objc_class_type)
3239 {
3240 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3241
3242 if (class_interface_decl)
3243 {
3244
3245 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3246 if (superclass_interface_decl)
3247 {
3248 if (omit_empty_base_classes)
3249 {
3250 if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true))
3251 ++num_children;
3252 }
3253 else
3254 ++num_children;
3255 }
3256
3257 num_children += class_interface_decl->ivar_size();
3258 }
3259 }
3260 }
3261 break;
3262
3263 case clang::Type::ObjCObjectPointer:
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003264 {
Sean Callanan78e37602011-01-27 04:42:51 +00003265 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003266 QualType pointee_type = pointer_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003267 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3268 pointee_type.getAsOpaquePtr(),
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003269 omit_empty_base_classes);
3270 // If this type points to a simple type, then it has 1 child
3271 if (num_pointee_children == 0)
3272 num_children = 1;
3273 else
3274 num_children = num_pointee_children;
3275 }
3276 break;
Greg Clayton9e409562010-07-28 02:04:09 +00003277
Greg Clayton1c8ef472013-04-05 23:27:21 +00003278 case clang::Type::Vector:
3279 case clang::Type::ExtVector:
3280 num_children = cast<VectorType>(qual_type.getTypePtr())->getNumElements();
3281 break;
3282
Greg Claytone1a916a2010-07-21 22:12:05 +00003283 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003284 num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
3285 break;
3286
Greg Claytone1a916a2010-07-21 22:12:05 +00003287 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003288 {
Sean Callanan78e37602011-01-27 04:42:51 +00003289 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003290 QualType pointee_type (pointer_type->getPointeeType());
Greg Clayton6beaaa62011-01-17 03:46:26 +00003291 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3292 pointee_type.getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00003293 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003294 if (num_pointee_children == 0)
Greg Clayton54979cd2010-12-15 05:08:08 +00003295 {
3296 // We have a pointer to a pointee type that claims it has no children.
3297 // We will want to look at
3298 num_children = ClangASTContext::GetNumPointeeChildren (pointee_type.getAsOpaquePtr());
3299 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003300 else
3301 num_children = num_pointee_children;
3302 }
3303 break;
3304
Greg Clayton73b472d2010-10-27 03:32:59 +00003305 case clang::Type::LValueReference:
3306 case clang::Type::RValueReference:
3307 {
Sean Callanan78e37602011-01-27 04:42:51 +00003308 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton73b472d2010-10-27 03:32:59 +00003309 QualType pointee_type = reference_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003310 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3311 pointee_type.getAsOpaquePtr(),
Greg Clayton73b472d2010-10-27 03:32:59 +00003312 omit_empty_base_classes);
3313 // If this type points to a simple type, then it has 1 child
3314 if (num_pointee_children == 0)
3315 num_children = 1;
3316 else
3317 num_children = num_pointee_children;
3318 }
3319 break;
3320
3321
Greg Claytone1a916a2010-07-21 22:12:05 +00003322 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00003323 num_children = ClangASTContext::GetNumChildren (ast,
3324 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3325 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003326 break;
Sean Callanan912855f2011-08-11 23:56:13 +00003327
3328 case clang::Type::Elaborated:
3329 num_children = ClangASTContext::GetNumChildren (ast,
3330 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3331 omit_empty_base_classes);
3332 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003333
3334 default:
3335 break;
3336 }
3337 return num_children;
3338}
3339
Greg Claytonbf2331c2011-09-09 23:04:00 +00003340uint32_t
3341ClangASTContext::GetNumDirectBaseClasses (clang::ASTContext *ast, clang_type_t clang_type)
3342{
3343 if (clang_type == NULL)
3344 return 0;
3345
3346 uint32_t count = 0;
3347 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3348 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3349 switch (type_class)
3350 {
3351 case clang::Type::Record:
3352 if (GetCompleteQualType (ast, qual_type))
3353 {
3354 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3355 if (cxx_record_decl)
3356 count = cxx_record_decl->getNumBases();
3357 }
3358 break;
3359
3360 case clang::Type::ObjCObject:
3361 case clang::Type::ObjCInterface:
3362 if (GetCompleteQualType (ast, qual_type))
3363 {
3364 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3365 if (objc_class_type)
3366 {
3367 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3368
3369 if (class_interface_decl && class_interface_decl->getSuperClass())
3370 count = 1;
3371 }
3372 }
3373 break;
3374
3375
3376 case clang::Type::Typedef:
3377 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3378 break;
3379
3380 case clang::Type::Elaborated:
3381 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3382 break;
3383
3384 default:
3385 break;
3386 }
3387 return count;
3388}
3389
3390uint32_t
3391ClangASTContext::GetNumVirtualBaseClasses (clang::ASTContext *ast,
3392 clang_type_t clang_type)
3393{
3394 if (clang_type == NULL)
3395 return 0;
3396
3397 uint32_t count = 0;
3398 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3399 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3400 switch (type_class)
3401 {
3402 case clang::Type::Record:
3403 if (GetCompleteQualType (ast, qual_type))
3404 {
3405 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3406 if (cxx_record_decl)
3407 count = cxx_record_decl->getNumVBases();
3408 }
3409 break;
3410
3411 case clang::Type::Typedef:
3412 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3413 break;
3414
3415 case clang::Type::Elaborated:
3416 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3417 break;
3418
3419 default:
3420 break;
3421 }
3422 return count;
3423}
3424
3425uint32_t
3426ClangASTContext::GetNumFields (clang::ASTContext *ast, clang_type_t clang_type)
3427{
3428 if (clang_type == NULL)
3429 return 0;
3430
3431 uint32_t count = 0;
3432 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3433 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3434 switch (type_class)
3435 {
3436 case clang::Type::Record:
3437 if (GetCompleteQualType (ast, qual_type))
3438 {
3439 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
3440 if (record_type)
3441 {
3442 RecordDecl *record_decl = record_type->getDecl();
3443 if (record_decl)
3444 {
3445 uint32_t field_idx = 0;
3446 RecordDecl::field_iterator field, field_end;
3447 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3448 ++field_idx;
3449 count = field_idx;
3450 }
3451 }
3452 }
3453 break;
3454
3455 case clang::Type::Typedef:
3456 count = ClangASTContext::GetNumFields (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3457 break;
3458
3459 case clang::Type::Elaborated:
3460 count = ClangASTContext::GetNumFields (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3461 break;
3462
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003463 case clang::Type::ObjCObject:
3464 case clang::Type::ObjCInterface:
3465 if (GetCompleteQualType (ast, qual_type))
3466 {
3467 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3468 if (objc_class_type)
3469 {
3470 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3471
3472 if (class_interface_decl)
3473 count = class_interface_decl->ivar_size();
3474 }
3475 }
3476 break;
3477
Greg Claytonbf2331c2011-09-09 23:04:00 +00003478 default:
3479 break;
3480 }
3481 return count;
3482}
3483
3484clang_type_t
3485ClangASTContext::GetDirectBaseClassAtIndex (clang::ASTContext *ast,
3486 clang_type_t clang_type,
Greg Claytonc7bece562013-01-25 18:06:21 +00003487 size_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003488 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003489{
3490 if (clang_type == NULL)
3491 return 0;
3492
3493 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3494 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3495 switch (type_class)
3496 {
3497 case clang::Type::Record:
3498 if (GetCompleteQualType (ast, qual_type))
3499 {
3500 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3501 if (cxx_record_decl)
3502 {
3503 uint32_t curr_idx = 0;
3504 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3505 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3506 base_class != base_class_end;
3507 ++base_class, ++curr_idx)
3508 {
3509 if (curr_idx == idx)
3510 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003511 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003512 {
3513 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3514 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3515// if (base_class->isVirtual())
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003516// *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003517// else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003518 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003519 }
3520 return base_class->getType().getAsOpaquePtr();
3521 }
3522 }
3523 }
3524 }
3525 break;
3526
3527 case clang::Type::ObjCObject:
3528 case clang::Type::ObjCInterface:
3529 if (idx == 0 && GetCompleteQualType (ast, qual_type))
3530 {
3531 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3532 if (objc_class_type)
3533 {
3534 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3535
3536 if (class_interface_decl)
3537 {
3538 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3539 if (superclass_interface_decl)
3540 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003541 if (bit_offset_ptr)
3542 *bit_offset_ptr = 0;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003543 return ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr();
3544 }
3545 }
3546 }
3547 }
3548 break;
3549
3550
3551 case clang::Type::Typedef:
3552 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3553 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3554 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003555 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003556
3557 case clang::Type::Elaborated:
3558 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3559 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3560 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003561 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003562
3563 default:
3564 break;
3565 }
3566 return NULL;
3567}
3568
3569clang_type_t
3570ClangASTContext::GetVirtualBaseClassAtIndex (clang::ASTContext *ast,
3571 clang_type_t clang_type,
Greg Claytonc7bece562013-01-25 18:06:21 +00003572 size_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003573 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003574{
3575 if (clang_type == NULL)
3576 return 0;
3577
3578 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3579 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3580 switch (type_class)
3581 {
3582 case clang::Type::Record:
3583 if (GetCompleteQualType (ast, qual_type))
3584 {
3585 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3586 if (cxx_record_decl)
3587 {
3588 uint32_t curr_idx = 0;
3589 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3590 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
3591 base_class != base_class_end;
3592 ++base_class, ++curr_idx)
3593 {
3594 if (curr_idx == idx)
3595 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003596 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003597 {
3598 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3599 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003600 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003601
3602 }
3603 return base_class->getType().getAsOpaquePtr();
3604 }
3605 }
3606 }
3607 }
3608 break;
3609
3610 case clang::Type::Typedef:
3611 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3612 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3613 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003614 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003615
3616 case clang::Type::Elaborated:
3617 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3618 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3619 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003620 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003621
3622 default:
3623 break;
3624 }
3625 return NULL;
3626}
3627
3628clang_type_t
3629ClangASTContext::GetFieldAtIndex (clang::ASTContext *ast,
3630 clang_type_t clang_type,
Greg Claytonc7bece562013-01-25 18:06:21 +00003631 size_t idx,
Greg Claytonbf2331c2011-09-09 23:04:00 +00003632 std::string& name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003633 uint64_t *bit_offset_ptr,
3634 uint32_t *bitfield_bit_size_ptr,
3635 bool *is_bitfield_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003636{
3637 if (clang_type == NULL)
3638 return 0;
3639
3640 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3641 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3642 switch (type_class)
3643 {
3644 case clang::Type::Record:
3645 if (GetCompleteQualType (ast, qual_type))
3646 {
3647 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3648 const RecordDecl *record_decl = record_type->getDecl();
3649 uint32_t field_idx = 0;
3650 RecordDecl::field_iterator field, field_end;
3651 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
3652 {
3653 if (idx == field_idx)
3654 {
3655 // Print the member type if requested
3656 // Print the member name and equal sign
3657 name.assign(field->getNameAsString());
3658
3659 // Figure out the type byte size (field_type_info.first) and
3660 // alignment (field_type_info.second) from the AST context.
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003661 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003662 {
3663 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003664 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003665 }
3666
Greg Clayton1811b4f2012-07-31 23:39:10 +00003667 const bool is_bitfield = field->isBitField();
3668
3669 if (bitfield_bit_size_ptr)
3670 {
3671 *bitfield_bit_size_ptr = 0;
3672
3673 if (is_bitfield && ast)
3674 {
3675 Expr *bitfield_bit_size_expr = field->getBitWidth();
3676 llvm::APSInt bitfield_apsint;
3677 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
3678 {
3679 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
3680 }
3681 }
3682 }
3683 if (is_bitfield_ptr)
3684 *is_bitfield_ptr = is_bitfield;
3685
Greg Claytonbf2331c2011-09-09 23:04:00 +00003686 return field->getType().getAsOpaquePtr();
3687 }
3688 }
3689 }
3690 break;
3691
3692 case clang::Type::ObjCObject:
3693 case clang::Type::ObjCInterface:
3694 if (GetCompleteQualType (ast, qual_type))
3695 {
3696 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3697 assert (objc_class_type);
3698 if (objc_class_type)
3699 {
3700 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3701
3702 if (class_interface_decl)
3703 {
3704 if (idx < (class_interface_decl->ivar_size()))
3705 {
3706 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
3707 uint32_t ivar_idx = 0;
3708
3709 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
3710 {
3711 if (ivar_idx == idx)
3712 {
3713 const ObjCIvarDecl* ivar_decl = *ivar_pos;
3714
3715 QualType ivar_qual_type(ivar_decl->getType());
3716
3717 name.assign(ivar_decl->getNameAsString());
3718
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003719 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003720 {
3721 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003722 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003723 }
3724
Greg Clayton1811b4f2012-07-31 23:39:10 +00003725 const bool is_bitfield = ivar_pos->isBitField();
3726
3727 if (bitfield_bit_size_ptr)
3728 {
3729 *bitfield_bit_size_ptr = 0;
3730
3731 if (is_bitfield && ast)
3732 {
3733 Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
3734 llvm::APSInt bitfield_apsint;
3735 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
3736 {
3737 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
3738 }
3739 }
3740 }
3741 if (is_bitfield_ptr)
3742 *is_bitfield_ptr = is_bitfield;
3743
Greg Claytonbf2331c2011-09-09 23:04:00 +00003744 return ivar_qual_type.getAsOpaquePtr();
3745 }
3746 }
3747 }
3748 }
3749 }
3750 }
3751 break;
3752
3753
3754 case clang::Type::Typedef:
3755 return ClangASTContext::GetFieldAtIndex (ast,
3756 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3757 idx,
3758 name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003759 bit_offset_ptr,
3760 bitfield_bit_size_ptr,
3761 is_bitfield_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003762
3763 case clang::Type::Elaborated:
3764 return ClangASTContext::GetFieldAtIndex (ast,
3765 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3766 idx,
3767 name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003768 bit_offset_ptr,
3769 bitfield_bit_size_ptr,
3770 is_bitfield_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003771
3772 default:
3773 break;
3774 }
3775 return NULL;
3776}
3777
Enrico Granata92373532013-03-19 22:58:48 +00003778size_t
3779ClangASTContext::GetIndexOfFieldWithName (clang::ASTContext *ast,
3780 lldb::clang_type_t clang_type,
3781 const char* name,
3782 lldb::clang_type_t* field_clang_type,
3783 uint64_t *bit_offset_ptr,
3784 uint32_t *bitfield_bit_size_ptr,
3785 bool *is_bitfield_ptr)
3786{
3787 auto count = ClangASTContext::GetNumFields(ast, clang_type);
3788 lldb::clang_type_t field_clang_type_internal;
3789 std::string field_name;
3790 for (auto index = 0; index < count; index++)
3791 {
3792 field_clang_type_internal = ClangASTContext::GetFieldAtIndex(ast, clang_type, index, field_name, bit_offset_ptr, bitfield_bit_size_ptr, is_bitfield_ptr);
3793 if ( strcmp(field_name.c_str(), name) == 0 )
3794 {
3795 if (field_clang_type)
3796 *field_clang_type = field_clang_type_internal;
3797 return index;
3798 }
3799 }
3800 return UINT32_MAX;
3801}
3802
Greg Claytoneaafa732012-10-13 00:20:27 +00003803lldb::BasicType
3804ClangASTContext::GetLLDBBasicTypeEnumeration (clang_type_t clang_type)
3805{
3806 if (clang_type)
3807 {
3808 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3809 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Claytonc4c5e892012-10-15 21:16:43 +00003810 if (type_class == clang::Type::Builtin)
Greg Claytoneaafa732012-10-13 00:20:27 +00003811 {
Greg Claytoneaafa732012-10-13 00:20:27 +00003812 switch (cast<clang::BuiltinType>(qual_type)->getKind())
Greg Claytonc4c5e892012-10-15 21:16:43 +00003813 {
Greg Claytoneaafa732012-10-13 00:20:27 +00003814 case clang::BuiltinType::Void: return eBasicTypeVoid;
3815 case clang::BuiltinType::Bool: return eBasicTypeBool;
3816 case clang::BuiltinType::Char_S: return eBasicTypeSignedChar;
3817 case clang::BuiltinType::Char_U: return eBasicTypeUnsignedChar;
3818 case clang::BuiltinType::Char16: return eBasicTypeChar16;
3819 case clang::BuiltinType::Char32: return eBasicTypeChar32;
3820 case clang::BuiltinType::UChar: return eBasicTypeUnsignedChar;
3821 case clang::BuiltinType::SChar: return eBasicTypeSignedChar;
3822 case clang::BuiltinType::WChar_S: return eBasicTypeSignedWChar;
3823 case clang::BuiltinType::WChar_U: return eBasicTypeUnsignedWChar;
3824 case clang::BuiltinType::Short: return eBasicTypeShort;
3825 case clang::BuiltinType::UShort: return eBasicTypeUnsignedShort;
3826 case clang::BuiltinType::Int: return eBasicTypeInt;
3827 case clang::BuiltinType::UInt: return eBasicTypeUnsignedInt;
3828 case clang::BuiltinType::Long: return eBasicTypeLong;
3829 case clang::BuiltinType::ULong: return eBasicTypeUnsignedLong;
3830 case clang::BuiltinType::LongLong: return eBasicTypeLongLong;
3831 case clang::BuiltinType::ULongLong: return eBasicTypeUnsignedLongLong;
3832 case clang::BuiltinType::Int128: return eBasicTypeInt128;
3833 case clang::BuiltinType::UInt128: return eBasicTypeUnsignedInt128;
3834
3835 case clang::BuiltinType::Half: return eBasicTypeHalf;
3836 case clang::BuiltinType::Float: return eBasicTypeFloat;
3837 case clang::BuiltinType::Double: return eBasicTypeDouble;
3838 case clang::BuiltinType::LongDouble:return eBasicTypeLongDouble;
3839
3840 case clang::BuiltinType::NullPtr: return eBasicTypeNullPtr;
3841 case clang::BuiltinType::ObjCId: return eBasicTypeObjCID;
3842 case clang::BuiltinType::ObjCClass: return eBasicTypeObjCClass;
3843 case clang::BuiltinType::ObjCSel: return eBasicTypeObjCSel;
3844 case clang::BuiltinType::Dependent:
3845 case clang::BuiltinType::Overload:
3846 case clang::BuiltinType::BoundMember:
3847 case clang::BuiltinType::PseudoObject:
3848 case clang::BuiltinType::UnknownAny:
3849 case clang::BuiltinType::BuiltinFn:
3850 case clang::BuiltinType::ARCUnbridgedCast:
Greg Clayton3dcaa2c2013-02-01 00:46:49 +00003851 case clang::BuiltinType::OCLEvent:
3852 case clang::BuiltinType::OCLImage1d:
3853 case clang::BuiltinType::OCLImage1dArray:
3854 case clang::BuiltinType::OCLImage1dBuffer:
3855 case clang::BuiltinType::OCLImage2d:
3856 case clang::BuiltinType::OCLImage2dArray:
3857 case clang::BuiltinType::OCLImage3d:
Greg Clayton142c5a42013-02-13 18:15:56 +00003858 case clang::BuiltinType::OCLSampler:
Greg Claytoneaafa732012-10-13 00:20:27 +00003859 return eBasicTypeOther;
Greg Claytonc4c5e892012-10-15 21:16:43 +00003860 }
Greg Claytoneaafa732012-10-13 00:20:27 +00003861 }
3862 }
3863
3864 return eBasicTypeInvalid;
3865}
3866
3867
Greg Claytonbf2331c2011-09-09 23:04:00 +00003868
Greg Clayton54979cd2010-12-15 05:08:08 +00003869// If a pointer to a pointee type (the clang_type arg) says that it has no
3870// children, then we either need to trust it, or override it and return a
3871// different result. For example, an "int *" has one child that is an integer,
3872// but a function pointer doesn't have any children. Likewise if a Record type
3873// claims it has no children, then there really is nothing to show.
3874uint32_t
3875ClangASTContext::GetNumPointeeChildren (clang_type_t clang_type)
3876{
3877 if (clang_type == NULL)
3878 return 0;
3879
3880 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3881 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3882 switch (type_class)
3883 {
Greg Clayton97a43712011-01-08 22:26:47 +00003884 case clang::Type::Builtin:
3885 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3886 {
Greg Clayton7260f622011-04-18 08:33:37 +00003887 case clang::BuiltinType::UnknownAny:
Greg Clayton97a43712011-01-08 22:26:47 +00003888 case clang::BuiltinType::Void:
3889 case clang::BuiltinType::NullPtr:
Greg Clayton3dcaa2c2013-02-01 00:46:49 +00003890 case clang::BuiltinType::OCLEvent:
3891 case clang::BuiltinType::OCLImage1d:
3892 case clang::BuiltinType::OCLImage1dArray:
3893 case clang::BuiltinType::OCLImage1dBuffer:
3894 case clang::BuiltinType::OCLImage2d:
3895 case clang::BuiltinType::OCLImage2dArray:
3896 case clang::BuiltinType::OCLImage3d:
Greg Clayton142c5a42013-02-13 18:15:56 +00003897 case clang::BuiltinType::OCLSampler:
Greg Clayton97a43712011-01-08 22:26:47 +00003898 return 0;
3899 case clang::BuiltinType::Bool:
3900 case clang::BuiltinType::Char_U:
3901 case clang::BuiltinType::UChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003902 case clang::BuiltinType::WChar_U:
Greg Clayton97a43712011-01-08 22:26:47 +00003903 case clang::BuiltinType::Char16:
3904 case clang::BuiltinType::Char32:
3905 case clang::BuiltinType::UShort:
3906 case clang::BuiltinType::UInt:
3907 case clang::BuiltinType::ULong:
3908 case clang::BuiltinType::ULongLong:
3909 case clang::BuiltinType::UInt128:
3910 case clang::BuiltinType::Char_S:
3911 case clang::BuiltinType::SChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003912 case clang::BuiltinType::WChar_S:
Greg Clayton97a43712011-01-08 22:26:47 +00003913 case clang::BuiltinType::Short:
3914 case clang::BuiltinType::Int:
3915 case clang::BuiltinType::Long:
3916 case clang::BuiltinType::LongLong:
3917 case clang::BuiltinType::Int128:
3918 case clang::BuiltinType::Float:
3919 case clang::BuiltinType::Double:
3920 case clang::BuiltinType::LongDouble:
3921 case clang::BuiltinType::Dependent:
3922 case clang::BuiltinType::Overload:
Greg Clayton97a43712011-01-08 22:26:47 +00003923 case clang::BuiltinType::ObjCId:
3924 case clang::BuiltinType::ObjCClass:
3925 case clang::BuiltinType::ObjCSel:
Sean Callanand12cf8bb2011-05-15 22:34:38 +00003926 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00003927 case clang::BuiltinType::Half:
3928 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00003929 case clang::BuiltinType::PseudoObject:
Sean Callanan3d654b32012-09-24 22:25:51 +00003930 case clang::BuiltinType::BuiltinFn:
Greg Clayton97a43712011-01-08 22:26:47 +00003931 return 1;
3932 }
3933 break;
3934
Greg Clayton49462ea2011-01-15 02:52:14 +00003935 case clang::Type::Complex: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003936 case clang::Type::Pointer: return 1;
3937 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
3938 case clang::Type::LValueReference: return 1;
3939 case clang::Type::RValueReference: return 1;
3940 case clang::Type::MemberPointer: return 0;
3941 case clang::Type::ConstantArray: return 0;
3942 case clang::Type::IncompleteArray: return 0;
3943 case clang::Type::VariableArray: return 0;
3944 case clang::Type::DependentSizedArray: return 0;
3945 case clang::Type::DependentSizedExtVector: return 0;
3946 case clang::Type::Vector: return 0;
3947 case clang::Type::ExtVector: return 0;
3948 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
3949 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
3950 case clang::Type::UnresolvedUsing: return 0;
3951 case clang::Type::Paren: return 0;
3952 case clang::Type::Typedef: return ClangASTContext::GetNumPointeeChildren (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00003953 case clang::Type::Elaborated: return ClangASTContext::GetNumPointeeChildren (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003954 case clang::Type::TypeOfExpr: return 0;
3955 case clang::Type::TypeOf: return 0;
3956 case clang::Type::Decltype: return 0;
3957 case clang::Type::Record: return 0;
3958 case clang::Type::Enum: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003959 case clang::Type::TemplateTypeParm: return 1;
3960 case clang::Type::SubstTemplateTypeParm: return 1;
3961 case clang::Type::TemplateSpecialization: return 1;
3962 case clang::Type::InjectedClassName: return 0;
3963 case clang::Type::DependentName: return 1;
3964 case clang::Type::DependentTemplateSpecialization: return 1;
3965 case clang::Type::ObjCObject: return 0;
3966 case clang::Type::ObjCInterface: return 0;
3967 case clang::Type::ObjCObjectPointer: return 1;
3968 default:
3969 break;
3970 }
3971 return 0;
3972}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003973
Greg Clayton1be10fc2010-09-29 01:12:09 +00003974clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003975ClangASTContext::GetChildClangTypeAtIndex
3976(
Jim Inghamd555bac2011-06-24 22:03:24 +00003977 ExecutionContext *exe_ctx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003978 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003979 clang_type_t parent_clang_type,
Greg Claytonc7bece562013-01-25 18:06:21 +00003980 size_t idx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003981 bool transparent_pointers,
3982 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003983 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003984 std::string& child_name,
3985 uint32_t &child_byte_size,
3986 int32_t &child_byte_offset,
3987 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003988 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003989 bool &child_is_base_class,
3990 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003991)
3992{
3993 if (parent_clang_type)
3994
Jim Inghamd555bac2011-06-24 22:03:24 +00003995 return GetChildClangTypeAtIndex (exe_ctx,
3996 getASTContext(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003997 parent_name,
3998 parent_clang_type,
3999 idx,
4000 transparent_pointers,
4001 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004002 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004003 child_name,
4004 child_byte_size,
4005 child_byte_offset,
4006 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00004007 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004008 child_is_base_class,
4009 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004010 return NULL;
4011}
4012
Greg Clayton1be10fc2010-09-29 01:12:09 +00004013clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004014ClangASTContext::GetChildClangTypeAtIndex
4015(
Jim Inghamd555bac2011-06-24 22:03:24 +00004016 ExecutionContext *exe_ctx,
Greg Clayton6beaaa62011-01-17 03:46:26 +00004017 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004018 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004019 clang_type_t parent_clang_type,
Greg Claytonc7bece562013-01-25 18:06:21 +00004020 size_t idx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004021 bool transparent_pointers,
4022 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004023 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004024 std::string& child_name,
4025 uint32_t &child_byte_size,
4026 int32_t &child_byte_offset,
4027 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00004028 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004029 bool &child_is_base_class,
4030 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004031)
4032{
4033 if (parent_clang_type == NULL)
4034 return NULL;
4035
Greg Clayton4ef877f2012-12-06 02:33:54 +00004036 QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type));
4037 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
4038 child_bitfield_bit_size = 0;
4039 child_bitfield_bit_offset = 0;
4040 child_is_base_class = false;
4041
4042 const bool idx_is_valid = idx < ClangASTContext::GetNumChildren (ast, parent_clang_type, omit_empty_base_classes);
4043 uint32_t bit_offset;
4044 switch (parent_type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004045 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004046 case clang::Type::Builtin:
4047 if (idx_is_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004048 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004049 switch (cast<clang::BuiltinType>(parent_qual_type)->getKind())
4050 {
4051 case clang::BuiltinType::ObjCId:
4052 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00004053 child_name = "isa";
Greg Clayton6beaaa62011-01-17 03:46:26 +00004054 child_byte_size = ast->getTypeSize(ast->ObjCBuiltinClassTy) / CHAR_BIT;
4055 return ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004056
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004057 default:
4058 break;
4059 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004060 }
4061 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004062
Greg Clayton4ef877f2012-12-06 02:33:54 +00004063 case clang::Type::Record:
4064 if (idx_is_valid && GetCompleteQualType (ast, parent_qual_type))
4065 {
4066 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr());
4067 const RecordDecl *record_decl = record_type->getDecl();
4068 assert(record_decl);
4069 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
4070 uint32_t child_idx = 0;
4071
4072 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4073 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004074 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004075 // We might have base classes to print out first
4076 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4077 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4078 base_class != base_class_end;
4079 ++base_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004080 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004081 const CXXRecordDecl *base_class_decl = NULL;
4082
4083 // Skip empty base classes
4084 if (omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004085 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004086 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4087 if (RecordHasFields(base_class_decl) == false)
4088 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004089 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004090
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004091 if (idx == child_idx)
4092 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004093 if (base_class_decl == NULL)
4094 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004095
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004096
Greg Clayton4ef877f2012-12-06 02:33:54 +00004097 if (base_class->isVirtual())
4098 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
4099 else
4100 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004101
Greg Clayton4ef877f2012-12-06 02:33:54 +00004102 // Base classes should be a multiple of 8 bits in size
4103 child_byte_offset = bit_offset/8;
4104
4105 child_name = ClangASTType::GetTypeNameForQualType(ast, base_class->getType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004106
Greg Clayton4ef877f2012-12-06 02:33:54 +00004107 uint64_t clang_type_info_bit_size = ast->getTypeSize(base_class->getType());
4108
4109 // Base classes bit sizes should be a multiple of 8 bits in size
4110 assert (clang_type_info_bit_size % 8 == 0);
4111 child_byte_size = clang_type_info_bit_size / 8;
4112 child_is_base_class = true;
4113 return base_class->getType().getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004114 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004115 // We don't increment the child index in the for loop since we might
4116 // be skipping empty base classes
4117 ++child_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004118 }
4119 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004120 // Make sure index is in range...
4121 uint32_t field_idx = 0;
4122 RecordDecl::field_iterator field, field_end;
4123 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00004124 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004125 if (idx == child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00004126 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004127 // Print the member type if requested
4128 // Print the member name and equal sign
4129 child_name.assign(field->getNameAsString().c_str());
4130
4131 // Figure out the type byte size (field_type_info.first) and
4132 // alignment (field_type_info.second) from the AST context.
4133 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(field->getType());
4134 assert(field_idx < record_layout.getFieldCount());
4135
4136 child_byte_size = field_type_info.first / 8;
4137
4138 // Figure out the field offset within the current struct/union/class type
4139 bit_offset = record_layout.getFieldOffset (field_idx);
4140 child_byte_offset = bit_offset / 8;
4141 if (ClangASTContext::FieldIsBitfield (ast, *field, child_bitfield_bit_size))
4142 child_bitfield_bit_offset = bit_offset % 8;
4143
4144 return field->getType().getAsOpaquePtr();
4145 }
4146 }
4147 }
4148 break;
4149
4150 case clang::Type::ObjCObject:
4151 case clang::Type::ObjCInterface:
4152 if (idx_is_valid && GetCompleteQualType (ast, parent_qual_type))
4153 {
4154 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr());
4155 assert (objc_class_type);
4156 if (objc_class_type)
4157 {
4158 uint32_t child_idx = 0;
4159 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4160
4161 if (class_interface_decl)
4162 {
4163
4164 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
4165 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4166 if (superclass_interface_decl)
Greg Clayton9e409562010-07-28 02:04:09 +00004167 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004168 if (omit_empty_base_classes)
Greg Clayton9e409562010-07-28 02:04:09 +00004169 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004170 if (ClangASTContext::GetNumChildren(ast, ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0)
Greg Clayton9e409562010-07-28 02:04:09 +00004171 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004172 if (idx == 0)
Greg Clayton9e409562010-07-28 02:04:09 +00004173 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004174 QualType ivar_qual_type(ast->getObjCInterfaceType(superclass_interface_decl));
Greg Clayton9e409562010-07-28 02:04:09 +00004175
Greg Clayton9e409562010-07-28 02:04:09 +00004176
Greg Clayton4ef877f2012-12-06 02:33:54 +00004177 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
Greg Clayton9e409562010-07-28 02:04:09 +00004178
Greg Clayton6beaaa62011-01-17 03:46:26 +00004179 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004180
4181 child_byte_size = ivar_type_info.first / 8;
Greg Clayton4ef877f2012-12-06 02:33:54 +00004182 child_byte_offset = 0;
4183 child_is_base_class = true;
Greg Clayton9e409562010-07-28 02:04:09 +00004184
Greg Clayton9e409562010-07-28 02:04:09 +00004185 return ivar_qual_type.getAsOpaquePtr();
4186 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004187
Greg Clayton9e409562010-07-28 02:04:09 +00004188 ++child_idx;
4189 }
4190 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004191 else
4192 ++child_idx;
4193 }
4194
4195 const uint32_t superclass_idx = child_idx;
4196
4197 if (idx < (child_idx + class_interface_decl->ivar_size()))
4198 {
4199 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4200
4201 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
4202 {
4203 if (child_idx == idx)
4204 {
4205 ObjCIvarDecl* ivar_decl = *ivar_pos;
4206
4207 QualType ivar_qual_type(ivar_decl->getType());
4208
4209 child_name.assign(ivar_decl->getNameAsString().c_str());
4210
4211 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
4212
4213 child_byte_size = ivar_type_info.first / 8;
4214
4215 // Figure out the field offset within the current struct/union/class type
4216 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
4217 // that doesn't account for the space taken up by unbacked properties, or from
4218 // the changing size of base classes that are newer than this class.
4219 // So if we have a process around that we can ask about this object, do so.
4220 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
4221 Process *process = NULL;
4222 if (exe_ctx)
4223 process = exe_ctx->GetProcessPtr();
4224 if (process)
4225 {
4226 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4227 if (objc_runtime != NULL)
4228 {
4229 ClangASTType parent_ast_type (ast, parent_qual_type.getAsOpaquePtr());
4230 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
4231 }
4232 }
4233
4234 // Setting this to UINT32_MAX to make sure we don't compute it twice...
4235 bit_offset = UINT32_MAX;
4236
4237 if (child_byte_offset == LLDB_INVALID_IVAR_OFFSET)
4238 {
4239 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
4240 child_byte_offset = bit_offset / 8;
4241 }
4242
4243 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
4244 // of a bitfield within its containing object. So regardless of where we get the byte
4245 // offset from, we still need to get the bit offset for bitfields from the layout.
4246
4247 if (ClangASTContext::FieldIsBitfield (ast, ivar_decl, child_bitfield_bit_size))
4248 {
4249 if (bit_offset == UINT32_MAX)
4250 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
4251
4252 child_bitfield_bit_offset = bit_offset % 8;
4253 }
4254 return ivar_qual_type.getAsOpaquePtr();
4255 }
4256 ++child_idx;
4257 }
Greg Clayton9e409562010-07-28 02:04:09 +00004258 }
4259 }
4260 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004261 }
4262 break;
4263
4264 case clang::Type::ObjCObjectPointer:
4265 if (idx_is_valid)
4266 {
4267 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr());
4268 QualType pointee_type = pointer_type->getPointeeType();
4269
4270 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
Greg Clayton9e409562010-07-28 02:04:09 +00004271 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004272 child_is_deref_of_parent = false;
4273 bool tmp_child_is_deref_of_parent = false;
4274 return GetChildClangTypeAtIndex (exe_ctx,
4275 ast,
4276 parent_name,
4277 pointer_type->getPointeeType().getAsOpaquePtr(),
4278 idx,
4279 transparent_pointers,
4280 omit_empty_base_classes,
4281 ignore_array_bounds,
4282 child_name,
4283 child_byte_size,
4284 child_byte_offset,
4285 child_bitfield_bit_size,
4286 child_bitfield_bit_offset,
4287 child_is_base_class,
4288 tmp_child_is_deref_of_parent);
4289 }
4290 else
4291 {
4292 child_is_deref_of_parent = true;
4293 if (parent_name)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004294 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004295 child_name.assign(1, '*');
4296 child_name += parent_name;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004297 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004298
Greg Clayton4ef877f2012-12-06 02:33:54 +00004299 // We have a pointer to an simple type
4300 if (idx == 0 && GetCompleteQualType(ast, pointee_type))
4301 {
4302 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
4303 assert(clang_type_info.first % 8 == 0);
4304 child_byte_size = clang_type_info.first / 8;
4305 child_byte_offset = 0;
4306 return pointee_type.getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004307 }
Greg Clayton9e409562010-07-28 02:04:09 +00004308 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004309 }
4310 break;
Greg Clayton9e409562010-07-28 02:04:09 +00004311
Greg Clayton1c8ef472013-04-05 23:27:21 +00004312 case clang::Type::Vector:
4313 case clang::Type::ExtVector:
4314 if (idx_is_valid)
4315 {
4316 const VectorType *array = cast<VectorType>(parent_qual_type.getTypePtr());
4317 if (array)
4318 {
4319 if (GetCompleteQualType (ast, array->getElementType()))
4320 {
4321 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
4322
4323 char element_name[64];
4324 ::snprintf (element_name, sizeof (element_name), "[%zu]", idx);
4325
4326 child_name.assign(element_name);
4327 assert(field_type_info.first % 8 == 0);
4328 child_byte_size = field_type_info.first / 8;
4329 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
4330 return array->getElementType().getAsOpaquePtr();
4331 }
4332 }
4333 }
4334 break;
4335
Greg Claytone1a916a2010-07-21 22:12:05 +00004336 case clang::Type::ConstantArray:
Greg Clayton4ef877f2012-12-06 02:33:54 +00004337 case clang::Type::IncompleteArray:
4338 if (ignore_array_bounds || idx_is_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004339 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004340 const ArrayType *array = cast<ArrayType>(parent_qual_type.getTypePtr());
4341 if (array)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004342 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004343 if (GetCompleteQualType (ast, array->getElementType()))
4344 {
4345 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Greg Clayton4ef877f2012-12-06 02:33:54 +00004346
Greg Clayton6beaaa62011-01-17 03:46:26 +00004347 char element_name[64];
Greg Claytonc7bece562013-01-25 18:06:21 +00004348 ::snprintf (element_name, sizeof (element_name), "[%zu]", idx);
Greg Clayton4ef877f2012-12-06 02:33:54 +00004349
Greg Clayton6beaaa62011-01-17 03:46:26 +00004350 child_name.assign(element_name);
4351 assert(field_type_info.first % 8 == 0);
4352 child_byte_size = field_type_info.first / 8;
Greg Claytondaf515f2011-07-09 20:12:33 +00004353 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
Greg Clayton6beaaa62011-01-17 03:46:26 +00004354 return array->getElementType().getAsOpaquePtr();
4355 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004356 }
4357 }
4358 break;
Greg Clayton4ef877f2012-12-06 02:33:54 +00004359
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004360
Greg Clayton4ef877f2012-12-06 02:33:54 +00004361 case clang::Type::Pointer:
4362 if (idx_is_valid)
4363 {
4364 const PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr());
4365 QualType pointee_type = pointer_type->getPointeeType();
4366
4367 // Don't dereference "void *" pointers
4368 if (pointee_type->isVoidType())
4369 return NULL;
4370
4371 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004372 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004373 child_is_deref_of_parent = false;
4374 bool tmp_child_is_deref_of_parent = false;
4375 return GetChildClangTypeAtIndex (exe_ctx,
4376 ast,
4377 parent_name,
4378 pointer_type->getPointeeType().getAsOpaquePtr(),
4379 idx,
4380 transparent_pointers,
4381 omit_empty_base_classes,
4382 ignore_array_bounds,
4383 child_name,
4384 child_byte_size,
4385 child_byte_offset,
4386 child_bitfield_bit_size,
4387 child_bitfield_bit_offset,
4388 child_is_base_class,
4389 tmp_child_is_deref_of_parent);
4390 }
4391 else
4392 {
4393 child_is_deref_of_parent = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004394
Greg Clayton4ef877f2012-12-06 02:33:54 +00004395 if (parent_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004396 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004397 child_name.assign(1, '*');
4398 child_name += parent_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004399 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004400
4401 // We have a pointer to an simple type
4402 if (idx == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004403 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004404 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
4405 assert(clang_type_info.first % 8 == 0);
4406 child_byte_size = clang_type_info.first / 8;
4407 child_byte_offset = 0;
4408 return pointee_type.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004409 }
4410 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004411 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004412 break;
4413
4414 case clang::Type::LValueReference:
4415 case clang::Type::RValueReference:
4416 if (idx_is_valid)
4417 {
4418 const ReferenceType *reference_type = cast<ReferenceType>(parent_qual_type.getTypePtr());
4419 QualType pointee_type(reference_type->getPointeeType());
4420 clang_type_t pointee_clang_type = pointee_type.getAsOpaquePtr();
4421 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_clang_type))
4422 {
4423 child_is_deref_of_parent = false;
4424 bool tmp_child_is_deref_of_parent = false;
4425 return GetChildClangTypeAtIndex (exe_ctx,
4426 ast,
4427 parent_name,
4428 pointee_clang_type,
4429 idx,
4430 transparent_pointers,
4431 omit_empty_base_classes,
4432 ignore_array_bounds,
4433 child_name,
4434 child_byte_size,
4435 child_byte_offset,
4436 child_bitfield_bit_size,
4437 child_bitfield_bit_offset,
4438 child_is_base_class,
4439 tmp_child_is_deref_of_parent);
4440 }
4441 else
4442 {
4443 if (parent_name)
4444 {
4445 child_name.assign(1, '&');
4446 child_name += parent_name;
4447 }
4448
4449 // We have a pointer to an simple type
4450 if (idx == 0)
4451 {
4452 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
4453 assert(clang_type_info.first % 8 == 0);
4454 child_byte_size = clang_type_info.first / 8;
4455 child_byte_offset = 0;
4456 return pointee_type.getAsOpaquePtr();
4457 }
4458 }
4459 }
4460 break;
4461
4462 case clang::Type::Typedef:
4463 return GetChildClangTypeAtIndex (exe_ctx,
4464 ast,
4465 parent_name,
4466 cast<TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
4467 idx,
4468 transparent_pointers,
4469 omit_empty_base_classes,
4470 ignore_array_bounds,
4471 child_name,
4472 child_byte_size,
4473 child_byte_offset,
4474 child_bitfield_bit_size,
4475 child_bitfield_bit_offset,
4476 child_is_base_class,
4477 child_is_deref_of_parent);
4478 break;
4479
4480 case clang::Type::Elaborated:
4481 return GetChildClangTypeAtIndex (exe_ctx,
4482 ast,
4483 parent_name,
4484 cast<ElaboratedType>(parent_qual_type)->getNamedType().getAsOpaquePtr(),
4485 idx,
4486 transparent_pointers,
4487 omit_empty_base_classes,
4488 ignore_array_bounds,
4489 child_name,
4490 child_byte_size,
4491 child_byte_offset,
4492 child_bitfield_bit_size,
4493 child_bitfield_bit_offset,
4494 child_is_base_class,
4495 child_is_deref_of_parent);
4496
4497 default:
4498 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004499 }
Greg Clayton19503a22010-07-23 15:37:46 +00004500 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004501}
4502
4503static inline bool
4504BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
4505{
Greg Clayton6beaaa62011-01-17 03:46:26 +00004506 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004507}
4508
4509static uint32_t
4510GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
4511{
4512 uint32_t num_bases = 0;
4513 if (cxx_record_decl)
4514 {
4515 if (omit_empty_base_classes)
4516 {
4517 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4518 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4519 base_class != base_class_end;
4520 ++base_class)
4521 {
4522 // Skip empty base classes
4523 if (omit_empty_base_classes)
4524 {
4525 if (BaseSpecifierIsEmpty (base_class))
4526 continue;
4527 }
4528 ++num_bases;
4529 }
4530 }
4531 else
4532 num_bases = cxx_record_decl->getNumBases();
4533 }
4534 return num_bases;
4535}
4536
4537
4538static uint32_t
4539GetIndexForRecordBase
4540(
4541 const RecordDecl *record_decl,
4542 const CXXBaseSpecifier *base_spec,
4543 bool omit_empty_base_classes
4544)
4545{
4546 uint32_t child_idx = 0;
4547
4548 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4549
4550// const char *super_name = record_decl->getNameAsCString();
4551// const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString();
4552// printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
4553//
4554 if (cxx_record_decl)
4555 {
4556 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4557 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4558 base_class != base_class_end;
4559 ++base_class)
4560 {
4561 if (omit_empty_base_classes)
4562 {
4563 if (BaseSpecifierIsEmpty (base_class))
4564 continue;
4565 }
4566
4567// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
4568// child_idx,
4569// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4570//
4571//
4572 if (base_class == base_spec)
4573 return child_idx;
4574 ++child_idx;
4575 }
4576 }
4577
4578 return UINT32_MAX;
4579}
4580
4581
4582static uint32_t
4583GetIndexForRecordChild
4584(
4585 const RecordDecl *record_decl,
4586 NamedDecl *canonical_decl,
4587 bool omit_empty_base_classes
4588)
4589{
4590 uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes);
4591
4592// const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4593//
4594//// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString());
4595// if (cxx_record_decl)
4596// {
4597// CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4598// for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4599// base_class != base_class_end;
4600// ++base_class)
4601// {
4602// if (omit_empty_base_classes)
4603// {
4604// if (BaseSpecifierIsEmpty (base_class))
4605// continue;
4606// }
4607//
4608//// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
4609//// record_decl->getNameAsCString(),
4610//// canonical_decl->getNameAsCString(),
4611//// child_idx,
4612//// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4613//
4614//
4615// CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4616// if (curr_base_class_decl == canonical_decl)
4617// {
4618// return child_idx;
4619// }
4620// ++child_idx;
4621// }
4622// }
4623//
4624// const uint32_t num_bases = child_idx;
4625 RecordDecl::field_iterator field, field_end;
4626 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4627 field != field_end;
4628 ++field, ++child_idx)
4629 {
4630// printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n",
4631// record_decl->getNameAsCString(),
4632// canonical_decl->getNameAsCString(),
4633// child_idx - num_bases,
4634// field->getNameAsCString());
4635
4636 if (field->getCanonicalDecl() == canonical_decl)
4637 return child_idx;
4638 }
4639
4640 return UINT32_MAX;
4641}
4642
4643// Look for a child member (doesn't include base classes, but it does include
4644// their members) in the type hierarchy. Returns an index path into "clang_type"
4645// on how to reach the appropriate member.
4646//
4647// class A
4648// {
4649// public:
4650// int m_a;
4651// int m_b;
4652// };
4653//
4654// class B
4655// {
4656// };
4657//
4658// class C :
4659// public B,
4660// public A
4661// {
4662// };
4663//
4664// If we have a clang type that describes "class C", and we wanted to looked
4665// "m_b" in it:
4666//
4667// With omit_empty_base_classes == false we would get an integer array back with:
4668// { 1, 1 }
4669// The first index 1 is the child index for "class A" within class C
4670// The second index 1 is the child index for "m_b" within class A
4671//
4672// With omit_empty_base_classes == true we would get an integer array back with:
4673// { 0, 1 }
4674// 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)
4675// The second index 1 is the child index for "m_b" within class A
4676
4677size_t
4678ClangASTContext::GetIndexOfChildMemberWithName
4679(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004680 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004681 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004682 const char *name,
4683 bool omit_empty_base_classes,
4684 std::vector<uint32_t>& child_indexes
4685)
4686{
4687 if (clang_type && name && name[0])
4688 {
4689 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00004690 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4691 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004692 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004693 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004694 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004695 {
4696 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4697 const RecordDecl *record_decl = record_type->getDecl();
4698
4699 assert(record_decl);
4700 uint32_t child_idx = 0;
4701
4702 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4703
4704 // Try and find a field that matches NAME
4705 RecordDecl::field_iterator field, field_end;
4706 StringRef name_sref(name);
4707 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4708 field != field_end;
4709 ++field, ++child_idx)
4710 {
4711 if (field->getName().equals (name_sref))
4712 {
4713 // We have to add on the number of base classes to this index!
4714 child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
4715 return child_indexes.size();
4716 }
4717 }
4718
4719 if (cxx_record_decl)
4720 {
4721 const RecordDecl *parent_record_decl = cxx_record_decl;
4722
4723 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
4724
4725 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
4726 // Didn't find things easily, lets let clang do its thang...
Sean Callanancc427fa2011-07-30 02:42:06 +00004727 IdentifierInfo & ident_ref = ast->Idents.get(name_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004728 DeclarationName decl_name(&ident_ref);
4729
4730 CXXBasePaths paths;
4731 if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember,
4732 decl_name.getAsOpaquePtr(),
4733 paths))
4734 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004735 CXXBasePaths::const_paths_iterator path, path_end = paths.end();
4736 for (path = paths.begin(); path != path_end; ++path)
4737 {
4738 const size_t num_path_elements = path->size();
4739 for (size_t e=0; e<num_path_elements; ++e)
4740 {
4741 CXXBasePathElement elem = (*path)[e];
4742
4743 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
4744 if (child_idx == UINT32_MAX)
4745 {
4746 child_indexes.clear();
4747 return 0;
4748 }
4749 else
4750 {
4751 child_indexes.push_back (child_idx);
4752 parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl());
4753 }
4754 }
Sean Callanan5deaa4c2012-12-21 21:34:42 +00004755 for (NamedDecl *path_decl : path->Decls)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004756 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00004757 child_idx = GetIndexForRecordChild (parent_record_decl, path_decl, omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004758 if (child_idx == UINT32_MAX)
4759 {
4760 child_indexes.clear();
4761 return 0;
4762 }
4763 else
4764 {
4765 child_indexes.push_back (child_idx);
4766 }
4767 }
4768 }
4769 return child_indexes.size();
4770 }
4771 }
4772
4773 }
4774 break;
4775
Greg Clayton9e409562010-07-28 02:04:09 +00004776 case clang::Type::ObjCObject:
4777 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004778 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004779 {
4780 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004781 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004782 assert (objc_class_type);
4783 if (objc_class_type)
4784 {
4785 uint32_t child_idx = 0;
4786 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4787
4788 if (class_interface_decl)
4789 {
4790 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4791 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4792
Greg Clayton6ba78152010-09-18 02:11:07 +00004793 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00004794 {
4795 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4796
4797 if (ivar_decl->getName().equals (name_sref))
4798 {
4799 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4800 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4801 ++child_idx;
4802
4803 child_indexes.push_back (child_idx);
4804 return child_indexes.size();
4805 }
4806 }
4807
4808 if (superclass_interface_decl)
4809 {
4810 // The super class index is always zero for ObjC classes,
4811 // so we push it onto the child indexes in case we find
4812 // an ivar in our superclass...
4813 child_indexes.push_back (0);
4814
Greg Clayton6beaaa62011-01-17 03:46:26 +00004815 if (GetIndexOfChildMemberWithName (ast,
4816 ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00004817 name,
4818 omit_empty_base_classes,
4819 child_indexes))
4820 {
4821 // We did find an ivar in a superclass so just
4822 // return the results!
4823 return child_indexes.size();
4824 }
4825
4826 // We didn't find an ivar matching "name" in our
4827 // superclass, pop the superclass zero index that
4828 // we pushed on above.
4829 child_indexes.pop_back();
4830 }
4831 }
4832 }
4833 }
4834 break;
4835
4836 case clang::Type::ObjCObjectPointer:
4837 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004838 return GetIndexOfChildMemberWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004839 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4840 name,
4841 omit_empty_base_classes,
4842 child_indexes);
4843 }
4844 break;
4845
4846
Greg Claytone1a916a2010-07-21 22:12:05 +00004847 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004848 {
4849// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4850// const uint64_t element_count = array->getSize().getLimitedValue();
4851//
4852// if (idx < element_count)
4853// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004854// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004855//
4856// char element_name[32];
4857// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4858//
4859// child_name.assign(element_name);
4860// assert(field_type_info.first % 8 == 0);
4861// child_byte_size = field_type_info.first / 8;
4862// child_byte_offset = idx * child_byte_size;
4863// return array->getElementType().getAsOpaquePtr();
4864// }
4865 }
4866 break;
4867
Greg Claytone1a916a2010-07-21 22:12:05 +00004868// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004869// {
4870// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4871// QualType pointee_type = mem_ptr_type->getPointeeType();
4872//
4873// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4874// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004875// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004876// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4877// name);
4878// }
4879// }
4880// break;
4881//
Greg Claytone1a916a2010-07-21 22:12:05 +00004882 case clang::Type::LValueReference:
4883 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004884 {
Sean Callanan78e37602011-01-27 04:42:51 +00004885 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004886 QualType pointee_type = reference_type->getPointeeType();
4887
4888 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4889 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004890 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004891 reference_type->getPointeeType().getAsOpaquePtr(),
4892 name,
4893 omit_empty_base_classes,
4894 child_indexes);
4895 }
4896 }
4897 break;
4898
Greg Claytone1a916a2010-07-21 22:12:05 +00004899 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004900 {
Sean Callanan78e37602011-01-27 04:42:51 +00004901 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004902 QualType pointee_type = pointer_type->getPointeeType();
4903
4904 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4905 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004906 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004907 pointer_type->getPointeeType().getAsOpaquePtr(),
4908 name,
4909 omit_empty_base_classes,
4910 child_indexes);
4911 }
4912 else
4913 {
4914// if (parent_name)
4915// {
4916// child_name.assign(1, '*');
4917// child_name += parent_name;
4918// }
4919//
4920// // We have a pointer to an simple type
4921// if (idx == 0)
4922// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004923// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004924// assert(clang_type_info.first % 8 == 0);
4925// child_byte_size = clang_type_info.first / 8;
4926// child_byte_offset = 0;
4927// return pointee_type.getAsOpaquePtr();
4928// }
4929 }
4930 }
4931 break;
4932
Greg Claytone1a916a2010-07-21 22:12:05 +00004933 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00004934 return GetIndexOfChildMemberWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00004935 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004936 name,
4937 omit_empty_base_classes,
4938 child_indexes);
4939
Greg Clayton4b63a5c2013-01-04 18:10:18 +00004940 case clang::Type::Elaborated:
4941 return GetIndexOfChildMemberWithName (ast,
4942 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
4943 name,
4944 omit_empty_base_classes,
4945 child_indexes);
4946
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004947 default:
4948 break;
4949 }
4950 }
4951 return 0;
4952}
4953
4954
4955// Get the index of the child of "clang_type" whose name matches. This function
4956// doesn't descend into the children, but only looks one level deep and name
4957// matches can include base class names.
4958
4959uint32_t
4960ClangASTContext::GetIndexOfChildWithName
4961(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004962 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004963 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004964 const char *name,
4965 bool omit_empty_base_classes
4966)
4967{
4968 if (clang_type && name && name[0])
4969 {
4970 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00004971
Greg Clayton737b9322010-09-13 03:32:57 +00004972 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Clayton9e409562010-07-28 02:04:09 +00004973
Greg Clayton737b9322010-09-13 03:32:57 +00004974 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004975 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004976 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004977 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004978 {
4979 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4980 const RecordDecl *record_decl = record_type->getDecl();
4981
4982 assert(record_decl);
4983 uint32_t child_idx = 0;
4984
4985 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4986
4987 if (cxx_record_decl)
4988 {
4989 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4990 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4991 base_class != base_class_end;
4992 ++base_class)
4993 {
4994 // Skip empty base classes
4995 CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4996 if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false)
4997 continue;
4998
Greg Clayton84db9102012-03-26 23:03:23 +00004999 std::string base_class_type_name (ClangASTType::GetTypeNameForQualType(ast, base_class->getType()));
Greg Claytone3055942011-06-30 02:28:26 +00005000 if (base_class_type_name.compare (name) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005001 return child_idx;
5002 ++child_idx;
5003 }
5004 }
5005
5006 // Try and find a field that matches NAME
5007 RecordDecl::field_iterator field, field_end;
5008 StringRef name_sref(name);
5009 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
5010 field != field_end;
5011 ++field, ++child_idx)
5012 {
5013 if (field->getName().equals (name_sref))
5014 return child_idx;
5015 }
5016
5017 }
5018 break;
5019
Greg Clayton9e409562010-07-28 02:04:09 +00005020 case clang::Type::ObjCObject:
5021 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00005022 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00005023 {
5024 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00005025 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00005026 assert (objc_class_type);
5027 if (objc_class_type)
5028 {
5029 uint32_t child_idx = 0;
5030 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5031
5032 if (class_interface_decl)
5033 {
5034 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
5035 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5036
Jim Ingham2f355a72012-10-04 22:22:16 +00005037 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00005038 {
5039 const ObjCIvarDecl* ivar_decl = *ivar_pos;
5040
5041 if (ivar_decl->getName().equals (name_sref))
5042 {
5043 if ((!omit_empty_base_classes && superclass_interface_decl) ||
5044 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
5045 ++child_idx;
5046
5047 return child_idx;
5048 }
5049 }
5050
5051 if (superclass_interface_decl)
5052 {
5053 if (superclass_interface_decl->getName().equals (name_sref))
5054 return 0;
5055 }
5056 }
5057 }
5058 }
5059 break;
5060
5061 case clang::Type::ObjCObjectPointer:
5062 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00005063 return GetIndexOfChildWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00005064 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
5065 name,
5066 omit_empty_base_classes);
5067 }
5068 break;
5069
Greg Claytone1a916a2010-07-21 22:12:05 +00005070 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005071 {
5072// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
5073// const uint64_t element_count = array->getSize().getLimitedValue();
5074//
5075// if (idx < element_count)
5076// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00005077// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005078//
5079// char element_name[32];
5080// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
5081//
5082// child_name.assign(element_name);
5083// assert(field_type_info.first % 8 == 0);
5084// child_byte_size = field_type_info.first / 8;
5085// child_byte_offset = idx * child_byte_size;
5086// return array->getElementType().getAsOpaquePtr();
5087// }
5088 }
5089 break;
5090
Greg Claytone1a916a2010-07-21 22:12:05 +00005091// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005092// {
5093// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
5094// QualType pointee_type = mem_ptr_type->getPointeeType();
5095//
5096// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
5097// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00005098// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005099// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
5100// name);
5101// }
5102// }
5103// break;
5104//
Greg Claytone1a916a2010-07-21 22:12:05 +00005105 case clang::Type::LValueReference:
5106 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005107 {
Sean Callanan78e37602011-01-27 04:42:51 +00005108 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005109 QualType pointee_type = reference_type->getPointeeType();
5110
5111 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
5112 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00005113 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005114 reference_type->getPointeeType().getAsOpaquePtr(),
5115 name,
5116 omit_empty_base_classes);
5117 }
5118 }
5119 break;
5120
Greg Claytone1a916a2010-07-21 22:12:05 +00005121 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005122 {
Sean Callanan78e37602011-01-27 04:42:51 +00005123 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005124 QualType pointee_type = pointer_type->getPointeeType();
5125
5126 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
5127 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00005128 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005129 pointer_type->getPointeeType().getAsOpaquePtr(),
5130 name,
5131 omit_empty_base_classes);
5132 }
5133 else
5134 {
5135// if (parent_name)
5136// {
5137// child_name.assign(1, '*');
5138// child_name += parent_name;
5139// }
5140//
5141// // We have a pointer to an simple type
5142// if (idx == 0)
5143// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00005144// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005145// assert(clang_type_info.first % 8 == 0);
5146// child_byte_size = clang_type_info.first / 8;
5147// child_byte_offset = 0;
5148// return pointee_type.getAsOpaquePtr();
5149// }
5150 }
5151 }
5152 break;
5153
Greg Clayton4b63a5c2013-01-04 18:10:18 +00005154 case clang::Type::Elaborated:
5155 return GetIndexOfChildWithName (ast,
5156 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
5157 name,
5158 omit_empty_base_classes);
5159
Greg Claytone1a916a2010-07-21 22:12:05 +00005160 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00005161 return GetIndexOfChildWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00005162 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005163 name,
5164 omit_empty_base_classes);
5165
5166 default:
5167 break;
5168 }
5169 }
5170 return UINT32_MAX;
5171}
5172
5173#pragma mark TagType
5174
5175bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005176ClangASTContext::SetTagTypeKind (clang_type_t tag_clang_type, int kind)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005177{
5178 if (tag_clang_type)
5179 {
5180 QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00005181 const clang::Type *clang_type = tag_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005182 if (clang_type)
5183 {
Sean Callanan78e37602011-01-27 04:42:51 +00005184 const TagType *tag_type = dyn_cast<TagType>(clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005185 if (tag_type)
5186 {
5187 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl());
5188 if (tag_decl)
5189 {
5190 tag_decl->setTagKind ((TagDecl::TagKind)kind);
5191 return true;
5192 }
5193 }
5194 }
5195 }
5196 return false;
5197}
5198
5199
5200#pragma mark DeclContext Functions
5201
5202DeclContext *
Greg Clayton1be10fc2010-09-29 01:12:09 +00005203ClangASTContext::GetDeclContextForType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005204{
5205 if (clang_type == NULL)
5206 return NULL;
5207
5208 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005209 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5210 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005211 {
Sean Callanancc427fa2011-07-30 02:42:06 +00005212 case clang::Type::UnaryTransform: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005213 case clang::Type::FunctionNoProto: break;
5214 case clang::Type::FunctionProto: break;
5215 case clang::Type::IncompleteArray: break;
5216 case clang::Type::VariableArray: break;
5217 case clang::Type::ConstantArray: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005218 case clang::Type::DependentSizedArray: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005219 case clang::Type::ExtVector: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005220 case clang::Type::DependentSizedExtVector: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005221 case clang::Type::Vector: break;
5222 case clang::Type::Builtin: break;
5223 case clang::Type::BlockPointer: break;
5224 case clang::Type::Pointer: break;
5225 case clang::Type::LValueReference: break;
5226 case clang::Type::RValueReference: break;
5227 case clang::Type::MemberPointer: break;
5228 case clang::Type::Complex: break;
5229 case clang::Type::ObjCObject: break;
5230 case clang::Type::ObjCInterface: return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface();
5231 case clang::Type::ObjCObjectPointer: return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr());
5232 case clang::Type::Record: return cast<RecordType>(qual_type)->getDecl();
5233 case clang::Type::Enum: return cast<EnumType>(qual_type)->getDecl();
Sean Callanan48114472010-12-13 01:26:27 +00005234 case clang::Type::Typedef: return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00005235 case clang::Type::Elaborated: return ClangASTContext::GetDeclContextForType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00005236 case clang::Type::TypeOfExpr: break;
5237 case clang::Type::TypeOf: break;
5238 case clang::Type::Decltype: break;
5239 //case clang::Type::QualifiedName: break;
5240 case clang::Type::TemplateSpecialization: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005241 case clang::Type::DependentTemplateSpecialization: break;
5242 case clang::Type::TemplateTypeParm: break;
5243 case clang::Type::SubstTemplateTypeParm: break;
5244 case clang::Type::SubstTemplateTypeParmPack:break;
5245 case clang::Type::PackExpansion: break;
5246 case clang::Type::UnresolvedUsing: break;
5247 case clang::Type::Paren: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005248 case clang::Type::Attributed: break;
5249 case clang::Type::Auto: break;
5250 case clang::Type::InjectedClassName: break;
5251 case clang::Type::DependentName: break;
Greg Claytonea3e7d52011-10-08 00:49:15 +00005252 case clang::Type::Atomic: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005253 }
5254 // No DeclContext in this type...
5255 return NULL;
5256}
5257
5258#pragma mark Namespace Declarations
5259
5260NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00005261ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005262{
Greg Clayton030a2042011-10-14 21:34:45 +00005263 NamespaceDecl *namespace_decl = NULL;
Greg Clayton9d3d6882011-10-31 23:51:19 +00005264 ASTContext *ast = getASTContext();
5265 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
5266 if (decl_ctx == NULL)
5267 decl_ctx = translation_unit_decl;
5268
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005269 if (name)
5270 {
Greg Clayton030a2042011-10-14 21:34:45 +00005271 IdentifierInfo &identifier_info = ast->Idents.get(name);
5272 DeclarationName decl_name (&identifier_info);
5273 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00005274 for (NamedDecl *decl : result)
Greg Clayton030a2042011-10-14 21:34:45 +00005275 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00005276 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
Greg Clayton030a2042011-10-14 21:34:45 +00005277 if (namespace_decl)
5278 return namespace_decl;
5279 }
5280
Sean Callanan5b26f272012-02-04 08:49:35 +00005281 namespace_decl = NamespaceDecl::Create(*ast,
5282 decl_ctx,
5283 false,
5284 SourceLocation(),
5285 SourceLocation(),
5286 &identifier_info,
5287 NULL);
Greg Clayton030a2042011-10-14 21:34:45 +00005288
Greg Clayton9d3d6882011-10-31 23:51:19 +00005289 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005290 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00005291 else
5292 {
5293 if (decl_ctx == translation_unit_decl)
5294 {
5295 namespace_decl = translation_unit_decl->getAnonymousNamespace();
5296 if (namespace_decl)
5297 return namespace_decl;
5298
Sean Callanan5b26f272012-02-04 08:49:35 +00005299 namespace_decl = NamespaceDecl::Create(*ast,
5300 decl_ctx,
5301 false,
5302 SourceLocation(),
5303 SourceLocation(),
5304 NULL,
5305 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00005306 translation_unit_decl->setAnonymousNamespace (namespace_decl);
5307 translation_unit_decl->addDecl (namespace_decl);
5308 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
5309 }
5310 else
5311 {
5312 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
5313 if (parent_namespace_decl)
5314 {
5315 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
5316 if (namespace_decl)
5317 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00005318 namespace_decl = NamespaceDecl::Create(*ast,
5319 decl_ctx,
5320 false,
5321 SourceLocation(),
5322 SourceLocation(),
5323 NULL,
5324 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00005325 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
5326 parent_namespace_decl->addDecl (namespace_decl);
5327 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
5328 }
5329 else
5330 {
5331 // BAD!!!
5332 }
5333 }
5334
5335
5336 if (namespace_decl)
5337 {
5338 // If we make it here, we are creating the anonymous namespace decl
5339 // for the first time, so we need to do the using directive magic
5340 // like SEMA does
5341 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast,
5342 decl_ctx,
5343 SourceLocation(),
5344 SourceLocation(),
5345 NestedNameSpecifierLoc(),
5346 SourceLocation(),
5347 namespace_decl,
5348 decl_ctx);
5349 using_directive_decl->setImplicit();
5350 decl_ctx->addDecl(using_directive_decl);
5351 }
5352 }
5353#ifdef LLDB_CONFIGURATION_DEBUG
5354 VerifyDecl(namespace_decl);
5355#endif
Greg Clayton030a2042011-10-14 21:34:45 +00005356 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005357}
5358
5359
5360#pragma mark Function Types
5361
5362FunctionDecl *
Greg Clayton147e1fa2011-10-14 22:47:18 +00005363ClangASTContext::CreateFunctionDeclaration (DeclContext *decl_ctx, const char *name, clang_type_t function_clang_type, int storage, bool is_inline)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005364{
Greg Clayton147e1fa2011-10-14 22:47:18 +00005365 FunctionDecl *func_decl = NULL;
5366 ASTContext *ast = getASTContext();
5367 if (decl_ctx == NULL)
5368 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005369
Greg Clayton147e1fa2011-10-14 22:47:18 +00005370 if (name && name[0])
5371 {
5372 func_decl = FunctionDecl::Create (*ast,
5373 decl_ctx,
5374 SourceLocation(),
5375 SourceLocation(),
5376 DeclarationName (&ast->Idents.get(name)),
5377 QualType::getFromOpaquePtr(function_clang_type),
5378 NULL,
5379 (FunctionDecl::StorageClass)storage,
5380 (FunctionDecl::StorageClass)storage,
5381 is_inline);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005382 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00005383 else
5384 {
5385 func_decl = FunctionDecl::Create (*ast,
5386 decl_ctx,
5387 SourceLocation(),
5388 SourceLocation(),
5389 DeclarationName (),
5390 QualType::getFromOpaquePtr(function_clang_type),
5391 NULL,
5392 (FunctionDecl::StorageClass)storage,
5393 (FunctionDecl::StorageClass)storage,
5394 is_inline);
5395 }
5396 if (func_decl)
5397 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005398
5399#ifdef LLDB_CONFIGURATION_DEBUG
5400 VerifyDecl(func_decl);
5401#endif
5402
Greg Clayton147e1fa2011-10-14 22:47:18 +00005403 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005404}
5405
Greg Clayton1be10fc2010-09-29 01:12:09 +00005406clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00005407ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00005408 clang_type_t result_type,
5409 clang_type_t *args,
Sean Callananc81256a2010-09-16 20:40:25 +00005410 unsigned num_args,
5411 bool is_variadic,
5412 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005413{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005414 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005415 std::vector<QualType> qual_type_args;
5416 for (unsigned i=0; i<num_args; ++i)
5417 qual_type_args.push_back (QualType::getFromOpaquePtr(args[i]));
5418
5419 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00005420 FunctionProtoType::ExtProtoInfo proto_info;
5421 proto_info.Variadic = is_variadic;
Sean Callananfb0b7582011-03-15 00:17:19 +00005422 proto_info.ExceptionSpecType = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005423 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00005424 proto_info.RefQualifier = RQ_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005425 proto_info.NumExceptions = 0;
5426 proto_info.Exceptions = NULL;
5427
Greg Clayton147e1fa2011-10-14 22:47:18 +00005428 return ast->getFunctionType (QualType::getFromOpaquePtr(result_type),
Sean Callanan6c9265b2013-03-09 01:59:31 +00005429 qual_type_args,
5430 proto_info).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005431}
5432
5433ParmVarDecl *
Greg Clayton1be10fc2010-09-29 01:12:09 +00005434ClangASTContext::CreateParameterDeclaration (const char *name, clang_type_t param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005435{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005436 ASTContext *ast = getASTContext();
5437 assert (ast != NULL);
5438 return ParmVarDecl::Create(*ast,
5439 ast->getTranslationUnitDecl(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005440 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005441 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00005442 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callananc81256a2010-09-16 20:40:25 +00005443 QualType::getFromOpaquePtr(param_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005444 NULL,
5445 (VarDecl::StorageClass)storage,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005446 0);
5447}
5448
5449void
5450ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
5451{
5452 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00005453 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005454}
5455
5456
5457#pragma mark Array Types
5458
Greg Clayton1be10fc2010-09-29 01:12:09 +00005459clang_type_t
Greg Clayton1c8ef472013-04-05 23:27:21 +00005460ClangASTContext::CreateArrayType (clang_type_t element_type,
5461 size_t element_count,
5462 bool is_vector)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005463{
5464 if (element_type)
5465 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00005466 ASTContext *ast = getASTContext();
5467 assert (ast != NULL);
Greg Clayton4ef877f2012-12-06 02:33:54 +00005468
Greg Clayton1c8ef472013-04-05 23:27:21 +00005469 QualType element_qual_type(QualType::getFromOpaquePtr(element_type));
5470
5471 if (is_vector)
5472 {
5473 return ast->getExtVectorType(element_qual_type, element_count).getAsOpaquePtr();
Greg Clayton4ef877f2012-12-06 02:33:54 +00005474 }
5475 else
5476 {
Greg Clayton1c8ef472013-04-05 23:27:21 +00005477
5478 llvm::APInt ap_element_count (64, element_count);
5479 if (element_count == 0)
5480 {
5481 return ast->getIncompleteArrayType(element_qual_type,
5482 ArrayType::Normal,
5483 0).getAsOpaquePtr();
5484
5485 }
5486 else
5487 {
5488 return ast->getConstantArrayType(element_qual_type,
5489 ap_element_count,
5490 ArrayType::Normal,
5491 0).getAsOpaquePtr(); // ElemQuals
5492 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00005493 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005494 }
5495 return NULL;
5496}
5497
5498
5499#pragma mark TagDecl
5500
5501bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005502ClangASTContext::StartTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005503{
5504 if (clang_type)
5505 {
5506 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00005507 const clang::Type *t = qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005508 if (t)
5509 {
Sean Callanan78e37602011-01-27 04:42:51 +00005510 const TagType *tag_type = dyn_cast<TagType>(t);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005511 if (tag_type)
5512 {
5513 TagDecl *tag_decl = tag_type->getDecl();
5514 if (tag_decl)
5515 {
5516 tag_decl->startDefinition();
5517 return true;
5518 }
5519 }
Sean Callanan5b26f272012-02-04 08:49:35 +00005520
5521 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(t);
5522 if (object_type)
5523 {
5524 ObjCInterfaceDecl *interface_decl = object_type->getInterface();
5525 if (interface_decl)
5526 {
5527 interface_decl->startDefinition();
5528 return true;
5529 }
5530 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005531 }
5532 }
5533 return false;
5534}
5535
5536bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005537ClangASTContext::CompleteTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005538{
5539 if (clang_type)
5540 {
5541 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton14372242010-09-29 03:44:17 +00005542
5543 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5544
5545 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005546 {
Greg Clayton14372242010-09-29 03:44:17 +00005547 cxx_record_decl->completeDefinition();
5548
5549 return true;
5550 }
5551
5552 const EnumType *enum_type = dyn_cast<EnumType>(qual_type.getTypePtr());
5553
5554 if (enum_type)
5555 {
5556 EnumDecl *enum_decl = enum_type->getDecl();
5557
5558 if (enum_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005559 {
Greg Clayton14372242010-09-29 03:44:17 +00005560 /// TODO This really needs to be fixed.
5561
5562 unsigned NumPositiveBits = 1;
5563 unsigned NumNegativeBits = 0;
5564
Greg Clayton6beaaa62011-01-17 03:46:26 +00005565 ASTContext *ast = getASTContext();
Greg Claytone02b8502010-10-12 04:29:14 +00005566
5567 QualType promotion_qual_type;
5568 // If the enum integer type is less than an integer in bit width,
5569 // then we must promote it to an integer size.
Greg Clayton6beaaa62011-01-17 03:46:26 +00005570 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
Greg Claytone02b8502010-10-12 04:29:14 +00005571 {
5572 if (enum_decl->getIntegerType()->isSignedIntegerType())
Greg Clayton6beaaa62011-01-17 03:46:26 +00005573 promotion_qual_type = ast->IntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005574 else
Greg Clayton6beaaa62011-01-17 03:46:26 +00005575 promotion_qual_type = ast->UnsignedIntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005576 }
5577 else
5578 promotion_qual_type = enum_decl->getIntegerType();
5579
5580 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
Greg Clayton14372242010-09-29 03:44:17 +00005581 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005582 }
5583 }
5584 }
5585 return false;
5586}
5587
5588
5589#pragma mark Enumeration Types
5590
Greg Clayton1be10fc2010-09-29 01:12:09 +00005591clang_type_t
Greg Claytonca512b32011-01-14 04:54:56 +00005592ClangASTContext::CreateEnumerationType
5593(
5594 const char *name,
5595 DeclContext *decl_ctx,
5596 const Declaration &decl,
5597 clang_type_t integer_qual_type
5598)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005599{
5600 // TODO: Do something intelligent with the Declaration object passed in
5601 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005602 ASTContext *ast = getASTContext();
5603 assert (ast != NULL);
Greg Claytone02b8502010-10-12 04:29:14 +00005604
5605 // TODO: ask about these...
5606// const bool IsScoped = false;
5607// const bool IsFixed = false;
5608
Greg Clayton6beaaa62011-01-17 03:46:26 +00005609 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00005610 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00005611 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00005612 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005613 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callanan48114472010-12-13 01:26:27 +00005614 NULL,
5615 false, // IsScoped
5616 false, // IsScopedUsingClassTag
5617 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00005618
5619
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005620 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00005621 {
5622 // TODO: check if we should be setting the promotion type too?
5623 enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00005624
5625 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
5626
Greg Clayton6beaaa62011-01-17 03:46:26 +00005627 return ast->getTagDeclType(enum_decl).getAsOpaquePtr();
Greg Clayton83ff3892010-09-12 23:17:56 +00005628 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005629 return NULL;
5630}
5631
Greg Clayton1be10fc2010-09-29 01:12:09 +00005632clang_type_t
5633ClangASTContext::GetEnumerationIntegerType (clang_type_t enum_clang_type)
5634{
5635 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5636
Sean Callanan78e37602011-01-27 04:42:51 +00005637 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Greg Clayton1be10fc2010-09-29 01:12:09 +00005638 if (clang_type)
5639 {
5640 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5641 if (enum_type)
5642 {
5643 EnumDecl *enum_decl = enum_type->getDecl();
5644 if (enum_decl)
5645 return enum_decl->getIntegerType().getAsOpaquePtr();
5646 }
5647 }
5648 return NULL;
5649}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005650bool
5651ClangASTContext::AddEnumerationValueToEnumerationType
5652(
Greg Clayton1be10fc2010-09-29 01:12:09 +00005653 clang_type_t enum_clang_type,
5654 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005655 const Declaration &decl,
5656 const char *name,
5657 int64_t enum_value,
5658 uint32_t enum_value_bit_size
5659)
5660{
5661 if (enum_clang_type && enumerator_clang_type && name)
5662 {
5663 // TODO: Do something intelligent with the Declaration object passed in
5664 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005665 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005666 IdentifierTable *identifier_table = getIdentifierTable();
5667
Greg Clayton6beaaa62011-01-17 03:46:26 +00005668 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005669 assert (identifier_table != NULL);
5670 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5671
Greg Clayton3e067532013-03-05 23:54:39 +00005672 bool is_signed = false;
5673 IsIntegerType (enumerator_clang_type, is_signed);
Sean Callanan78e37602011-01-27 04:42:51 +00005674 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005675 if (clang_type)
5676 {
5677 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5678
5679 if (enum_type)
5680 {
Greg Clayton3e067532013-03-05 23:54:39 +00005681 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005682 enum_llvm_apsint = enum_value;
5683 EnumConstantDecl *enumerator_decl =
Greg Clayton6beaaa62011-01-17 03:46:26 +00005684 EnumConstantDecl::Create (*ast,
5685 enum_type->getDecl(),
5686 SourceLocation(),
5687 name ? &identifier_table->get(name) : NULL, // Identifier
5688 QualType::getFromOpaquePtr(enumerator_clang_type),
5689 NULL,
5690 enum_llvm_apsint);
5691
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005692 if (enumerator_decl)
5693 {
5694 enum_type->getDecl()->addDecl(enumerator_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005695
5696#ifdef LLDB_CONFIGURATION_DEBUG
5697 VerifyDecl(enumerator_decl);
5698#endif
5699
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005700 return true;
5701 }
5702 }
5703 }
5704 }
5705 return false;
5706}
5707
5708#pragma mark Pointers & References
5709
Greg Clayton1be10fc2010-09-29 01:12:09 +00005710clang_type_t
5711ClangASTContext::CreatePointerType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005712{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005713 return CreatePointerType (getASTContext(), clang_type);
5714}
5715
5716clang_type_t
5717ClangASTContext::CreatePointerType (clang::ASTContext *ast, clang_type_t clang_type)
5718{
5719 if (ast && clang_type)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005720 {
5721 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5722
Greg Clayton737b9322010-09-13 03:32:57 +00005723 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5724 switch (type_class)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005725 {
5726 case clang::Type::ObjCObject:
5727 case clang::Type::ObjCInterface:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005728 return ast->getObjCObjectPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005729
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005730 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005731 return ast->getPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005732 }
5733 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005734 return NULL;
5735}
5736
Greg Clayton1be10fc2010-09-29 01:12:09 +00005737clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005738ClangASTContext::CreateLValueReferenceType (clang::ASTContext *ast,
5739 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005740{
5741 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005742 return ast->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005743 return NULL;
5744}
5745
Greg Clayton1be10fc2010-09-29 01:12:09 +00005746clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005747ClangASTContext::CreateRValueReferenceType (clang::ASTContext *ast,
5748 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005749{
5750 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005751 return ast->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005752 return NULL;
5753}
5754
Greg Clayton1be10fc2010-09-29 01:12:09 +00005755clang_type_t
5756ClangASTContext::CreateMemberPointerType (clang_type_t clang_pointee_type, clang_type_t clang_class_type)
Greg Clayton9b81a312010-06-12 01:20:30 +00005757{
5758 if (clang_pointee_type && clang_pointee_type)
5759 return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type),
5760 QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr();
5761 return NULL;
5762}
5763
Greg Claytonfaac1112013-03-14 18:31:44 +00005764uint64_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005765ClangASTContext::GetPointerBitSize ()
5766{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005767 ASTContext *ast = getASTContext();
5768 return ast->getTypeSize(ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005769}
5770
5771bool
Greg Clayton219cf312012-03-30 00:51:13 +00005772ClangASTContext::IsPossibleDynamicType (clang::ASTContext *ast,
5773 clang_type_t clang_type,
5774 clang_type_t *dynamic_pointee_type,
5775 bool check_cplusplus,
5776 bool check_objc)
Greg Claytondea8cb42011-06-29 22:09:02 +00005777{
5778 QualType pointee_qual_type;
5779 if (clang_type)
5780 {
5781 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5782 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5783 bool success = false;
5784 switch (type_class)
5785 {
5786 case clang::Type::Builtin:
Greg Clayton219cf312012-03-30 00:51:13 +00005787 if (check_objc && cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
Greg Claytondea8cb42011-06-29 22:09:02 +00005788 {
5789 if (dynamic_pointee_type)
5790 *dynamic_pointee_type = clang_type;
5791 return true;
5792 }
5793 break;
5794
5795 case clang::Type::ObjCObjectPointer:
Greg Clayton219cf312012-03-30 00:51:13 +00005796 if (check_objc)
5797 {
5798 if (dynamic_pointee_type)
5799 *dynamic_pointee_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5800 return true;
5801 }
5802 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005803
5804 case clang::Type::Pointer:
5805 pointee_qual_type = cast<PointerType>(qual_type)->getPointeeType();
5806 success = true;
5807 break;
5808
5809 case clang::Type::LValueReference:
5810 case clang::Type::RValueReference:
5811 pointee_qual_type = cast<ReferenceType>(qual_type)->getPointeeType();
5812 success = true;
5813 break;
5814
5815 case clang::Type::Typedef:
Greg Clayton70364252012-08-31 18:56:24 +00005816 return ClangASTContext::IsPossibleDynamicType (ast,
5817 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
5818 dynamic_pointee_type,
5819 check_cplusplus,
5820 check_objc);
Sean Callanan912855f2011-08-11 23:56:13 +00005821
5822 case clang::Type::Elaborated:
Greg Clayton70364252012-08-31 18:56:24 +00005823 return ClangASTContext::IsPossibleDynamicType (ast,
5824 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
5825 dynamic_pointee_type,
5826 check_cplusplus,
5827 check_objc);
Sean Callanan912855f2011-08-11 23:56:13 +00005828
Greg Claytondea8cb42011-06-29 22:09:02 +00005829 default:
5830 break;
5831 }
5832
5833 if (success)
5834 {
5835 // Check to make sure what we are pointing too is a possible dynamic C++ type
5836 // We currently accept any "void *" (in case we have a class that has been
5837 // watered down to an opaque pointer) and virtual C++ classes.
5838 const clang::Type::TypeClass pointee_type_class = pointee_qual_type->getTypeClass();
5839 switch (pointee_type_class)
5840 {
5841 case clang::Type::Builtin:
5842 switch (cast<clang::BuiltinType>(pointee_qual_type)->getKind())
5843 {
5844 case clang::BuiltinType::UnknownAny:
5845 case clang::BuiltinType::Void:
5846 if (dynamic_pointee_type)
5847 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5848 return true;
5849
5850 case clang::BuiltinType::NullPtr:
5851 case clang::BuiltinType::Bool:
5852 case clang::BuiltinType::Char_U:
5853 case clang::BuiltinType::UChar:
5854 case clang::BuiltinType::WChar_U:
5855 case clang::BuiltinType::Char16:
5856 case clang::BuiltinType::Char32:
5857 case clang::BuiltinType::UShort:
5858 case clang::BuiltinType::UInt:
5859 case clang::BuiltinType::ULong:
5860 case clang::BuiltinType::ULongLong:
5861 case clang::BuiltinType::UInt128:
5862 case clang::BuiltinType::Char_S:
5863 case clang::BuiltinType::SChar:
5864 case clang::BuiltinType::WChar_S:
5865 case clang::BuiltinType::Short:
5866 case clang::BuiltinType::Int:
5867 case clang::BuiltinType::Long:
5868 case clang::BuiltinType::LongLong:
5869 case clang::BuiltinType::Int128:
5870 case clang::BuiltinType::Float:
5871 case clang::BuiltinType::Double:
5872 case clang::BuiltinType::LongDouble:
5873 case clang::BuiltinType::Dependent:
5874 case clang::BuiltinType::Overload:
5875 case clang::BuiltinType::ObjCId:
5876 case clang::BuiltinType::ObjCClass:
5877 case clang::BuiltinType::ObjCSel:
5878 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00005879 case clang::BuiltinType::Half:
5880 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00005881 case clang::BuiltinType::PseudoObject:
Sean Callanan3d654b32012-09-24 22:25:51 +00005882 case clang::BuiltinType::BuiltinFn:
Greg Clayton3dcaa2c2013-02-01 00:46:49 +00005883 case clang::BuiltinType::OCLEvent:
5884 case clang::BuiltinType::OCLImage1d:
5885 case clang::BuiltinType::OCLImage1dArray:
5886 case clang::BuiltinType::OCLImage1dBuffer:
5887 case clang::BuiltinType::OCLImage2d:
5888 case clang::BuiltinType::OCLImage2dArray:
5889 case clang::BuiltinType::OCLImage3d:
Greg Clayton142c5a42013-02-13 18:15:56 +00005890 case clang::BuiltinType::OCLSampler:
Greg Claytondea8cb42011-06-29 22:09:02 +00005891 break;
5892 }
5893 break;
5894
5895 case clang::Type::Record:
Greg Clayton219cf312012-03-30 00:51:13 +00005896 if (check_cplusplus)
Greg Claytondea8cb42011-06-29 22:09:02 +00005897 {
5898 CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
5899 if (cxx_record_decl)
5900 {
Greg Clayton70364252012-08-31 18:56:24 +00005901 bool is_complete = cxx_record_decl->isCompleteDefinition();
Greg Claytonc4ffd662013-03-08 01:37:30 +00005902
Greg Clayton70364252012-08-31 18:56:24 +00005903 if (is_complete)
Greg Claytondea8cb42011-06-29 22:09:02 +00005904 success = cxx_record_decl->isDynamicClass();
Greg Claytondea8cb42011-06-29 22:09:02 +00005905 else
5906 {
Greg Claytond0029442013-03-27 01:48:02 +00005907 ClangASTMetadata *metadata = GetMetadata (ast, cxx_record_decl);
Greg Claytonc4ffd662013-03-08 01:37:30 +00005908 if (metadata)
5909 success = metadata->GetIsDynamicCXXType();
5910 else
5911 {
5912 is_complete = ClangASTContext::GetCompleteType (ast, pointee_qual_type.getAsOpaquePtr());
5913 if (is_complete)
5914 success = cxx_record_decl->isDynamicClass();
5915 else
5916 success = false;
5917 }
Greg Claytondea8cb42011-06-29 22:09:02 +00005918 }
Greg Clayton70364252012-08-31 18:56:24 +00005919
Greg Claytondea8cb42011-06-29 22:09:02 +00005920 if (success)
5921 {
5922 if (dynamic_pointee_type)
5923 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5924 return true;
5925 }
5926 }
5927 }
5928 break;
5929
5930 case clang::Type::ObjCObject:
5931 case clang::Type::ObjCInterface:
Greg Clayton219cf312012-03-30 00:51:13 +00005932 if (check_objc)
5933 {
5934 if (dynamic_pointee_type)
5935 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5936 return true;
5937 }
5938 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005939
5940 default:
5941 break;
5942 }
5943 }
5944 }
5945 if (dynamic_pointee_type)
5946 *dynamic_pointee_type = NULL;
5947 return false;
5948}
5949
5950
5951bool
Greg Clayton007d5be2011-05-30 00:49:24 +00005952ClangASTContext::IsPossibleCPlusPlusDynamicType (clang::ASTContext *ast, clang_type_t clang_type, clang_type_t *dynamic_pointee_type)
5953{
Greg Clayton219cf312012-03-30 00:51:13 +00005954 return IsPossibleDynamicType (ast,
5955 clang_type,
5956 dynamic_pointee_type,
5957 true, // Check for dynamic C++ types
5958 false); // Check for dynamic ObjC types
Greg Clayton007d5be2011-05-30 00:49:24 +00005959}
5960
Sean Callanan98298012011-10-27 19:41:13 +00005961bool
5962ClangASTContext::IsReferenceType (clang_type_t clang_type, clang_type_t *target_type)
5963{
5964 if (clang_type == NULL)
5965 return false;
5966
5967 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5968 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5969
5970 switch (type_class)
5971 {
5972 case clang::Type::LValueReference:
5973 if (target_type)
5974 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5975 return true;
5976 case clang::Type::RValueReference:
5977 if (target_type)
5978 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5979 return true;
5980 case clang::Type::Typedef:
5981 return ClangASTContext::IsReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5982 case clang::Type::Elaborated:
5983 return ClangASTContext::IsReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5984 default:
5985 break;
5986 }
5987
5988 return false;
5989}
Greg Clayton007d5be2011-05-30 00:49:24 +00005990
5991bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005992ClangASTContext::IsPointerOrReferenceType (clang_type_t clang_type, clang_type_t*target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005993{
5994 if (clang_type == NULL)
5995 return false;
5996
5997 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005998 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5999 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006000 {
Sean Callanana2424172010-10-25 00:29:48 +00006001 case clang::Type::Builtin:
6002 switch (cast<clang::BuiltinType>(qual_type)->getKind())
6003 {
6004 default:
6005 break;
6006 case clang::BuiltinType::ObjCId:
6007 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00006008 return true;
6009 }
6010 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00006011 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006012 if (target_type)
6013 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
6014 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00006015 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006016 if (target_type)
6017 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
6018 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00006019 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006020 if (target_type)
6021 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
6022 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00006023 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006024 if (target_type)
6025 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
6026 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00006027 case clang::Type::LValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006028 if (target_type)
6029 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
6030 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00006031 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006032 if (target_type)
6033 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
6034 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00006035 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00006036 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006037 case clang::Type::Elaborated:
6038 return ClangASTContext::IsPointerOrReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006039 default:
6040 break;
6041 }
6042 return false;
6043}
6044
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006045bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006046ClangASTContext::IsIntegerType (clang_type_t clang_type, bool &is_signed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006047{
6048 if (!clang_type)
6049 return false;
6050
6051 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6052 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal());
6053
6054 if (builtin_type)
6055 {
6056 if (builtin_type->isInteger())
Jim Inghamef651602011-12-22 19:12:40 +00006057 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006058 is_signed = builtin_type->isSignedInteger();
Jim Inghamef651602011-12-22 19:12:40 +00006059 return true;
6060 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006061 }
6062
6063 return false;
6064}
6065
6066bool
Greg Claytonaffb03b2011-07-08 18:27:39 +00006067ClangASTContext::IsPointerType (clang_type_t clang_type, clang_type_t *target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006068{
Greg Claytonaffb03b2011-07-08 18:27:39 +00006069 if (target_type)
6070 *target_type = NULL;
6071
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006072 if (clang_type)
6073 {
6074 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00006075 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6076 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006077 {
Sean Callanana2424172010-10-25 00:29:48 +00006078 case clang::Type::Builtin:
6079 switch (cast<clang::BuiltinType>(qual_type)->getKind())
6080 {
6081 default:
6082 break;
6083 case clang::BuiltinType::ObjCId:
6084 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00006085 return true;
6086 }
6087 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00006088 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006089 if (target_type)
6090 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
6091 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00006092 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006093 if (target_type)
6094 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
6095 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00006096 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006097 if (target_type)
6098 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
6099 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00006100 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006101 if (target_type)
6102 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
6103 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00006104 case clang::Type::Typedef:
Greg Claytonaffb03b2011-07-08 18:27:39 +00006105 return ClangASTContext::IsPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), target_type);
Sean Callanan912855f2011-08-11 23:56:13 +00006106 case clang::Type::Elaborated:
6107 return ClangASTContext::IsPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), target_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006108 default:
6109 break;
6110 }
6111 }
6112 return false;
6113}
6114
6115bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006116ClangASTContext::IsFloatingPointType (clang_type_t clang_type, uint32_t &count, bool &is_complex)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006117{
6118 if (clang_type)
6119 {
6120 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6121
6122 if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()))
6123 {
6124 clang::BuiltinType::Kind kind = BT->getKind();
6125 if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble)
6126 {
6127 count = 1;
6128 is_complex = false;
6129 return true;
6130 }
6131 }
6132 else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal()))
6133 {
6134 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex))
6135 {
6136 count = 2;
6137 is_complex = true;
6138 return true;
6139 }
6140 }
6141 else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal()))
6142 {
6143 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex))
6144 {
6145 count = VT->getNumElements();
6146 is_complex = false;
6147 return true;
6148 }
6149 }
6150 }
6151 return false;
6152}
6153
Enrico Granata9fc19442011-07-06 02:13:41 +00006154bool
6155ClangASTContext::IsScalarType (lldb::clang_type_t clang_type)
6156{
6157 bool is_signed;
6158 if (ClangASTContext::IsIntegerType(clang_type, is_signed))
6159 return true;
6160
6161 uint32_t count;
6162 bool is_complex;
6163 return ClangASTContext::IsFloatingPointType(clang_type, count, is_complex) && !is_complex;
6164}
6165
6166bool
6167ClangASTContext::IsPointerToScalarType (lldb::clang_type_t clang_type)
6168{
6169 if (!IsPointerType(clang_type))
6170 return false;
6171
6172 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6173 lldb::clang_type_t pointee_type = qual_type.getTypePtr()->getPointeeType().getAsOpaquePtr();
6174 return IsScalarType(pointee_type);
6175}
6176
6177bool
6178ClangASTContext::IsArrayOfScalarType (lldb::clang_type_t clang_type)
6179{
Greg Clayton4ef877f2012-12-06 02:33:54 +00006180 clang_type = GetAsArrayType(clang_type, NULL, NULL, NULL);
Sean Callanan0caa21c2012-01-19 23:54:24 +00006181
6182 if (clang_type == 0)
Enrico Granata9fc19442011-07-06 02:13:41 +00006183 return false;
6184
6185 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6186 lldb::clang_type_t item_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
6187 return IsScalarType(item_type);
6188}
6189
Greg Clayton8f92f0a2010-10-14 22:52:14 +00006190
6191bool
6192ClangASTContext::GetCXXClassName (clang_type_t clang_type, std::string &class_name)
6193{
6194 if (clang_type)
6195 {
6196 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6197
6198 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
6199 if (cxx_record_decl)
6200 {
6201 class_name.assign (cxx_record_decl->getIdentifier()->getNameStart());
6202 return true;
6203 }
6204 }
6205 class_name.clear();
6206 return false;
6207}
6208
6209
Greg Clayton0fffff52010-09-24 05:15:53 +00006210bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006211ClangASTContext::IsCXXClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00006212{
6213 if (clang_type)
6214 {
6215 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6216 if (qual_type->getAsCXXRecordDecl() != NULL)
6217 return true;
6218 }
6219 return false;
6220}
6221
Greg Clayton20568dd2011-10-13 23:13:20 +00006222bool
6223ClangASTContext::IsBeingDefined (lldb::clang_type_t clang_type)
6224{
6225 if (clang_type)
6226 {
6227 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6228 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type);
6229 if (tag_type)
6230 return tag_type->isBeingDefined();
6231 }
6232 return false;
6233}
6234
Greg Clayton0fffff52010-09-24 05:15:53 +00006235bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006236ClangASTContext::IsObjCClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00006237{
6238 if (clang_type)
6239 {
6240 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6241 if (qual_type->isObjCObjectOrInterfaceType())
6242 return true;
6243 }
6244 return false;
6245}
6246
Sean Callanan72772842012-02-22 23:57:45 +00006247bool
6248ClangASTContext::IsObjCObjectPointerType (lldb::clang_type_t clang_type, clang_type_t *class_type)
6249{
6250 if (clang_type)
6251 {
6252 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6253 if (qual_type->isObjCObjectPointerType())
6254 {
6255 if (class_type)
6256 {
6257 *class_type = NULL;
6258
6259 if (!qual_type->isObjCClassType() &&
6260 !qual_type->isObjCIdType())
6261 {
6262 const ObjCObjectPointerType *obj_pointer_type = dyn_cast<ObjCObjectPointerType>(qual_type);
Sean Callanand7dabe22012-03-10 01:59:11 +00006263 if (!obj_pointer_type)
6264 *class_type = NULL;
Sean Callanan1fc91ad2012-03-10 02:00:32 +00006265 else
6266 *class_type = QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr();
Sean Callanan72772842012-02-22 23:57:45 +00006267 }
6268 }
6269 return true;
6270 }
6271 }
6272 return false;
6273}
6274
6275bool
6276ClangASTContext::GetObjCClassName (lldb::clang_type_t clang_type,
6277 std::string &class_name)
6278{
6279 if (!clang_type)
6280 return false;
6281
6282 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(QualType::getFromOpaquePtr(clang_type));
6283 if (!object_type)
6284 return false;
6285
6286 const ObjCInterfaceDecl *interface = object_type->getInterface();
6287 if (!interface)
6288 return false;
6289
6290 class_name = interface->getNameAsString();
6291 return true;
6292}
Greg Clayton0fffff52010-09-24 05:15:53 +00006293
Greg Clayton73b472d2010-10-27 03:32:59 +00006294bool
6295ClangASTContext::IsCharType (clang_type_t clang_type)
6296{
6297 if (clang_type)
6298 return QualType::getFromOpaquePtr(clang_type)->isCharType();
6299 return false;
6300}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006301
6302bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006303ClangASTContext::IsCStringType (clang_type_t clang_type, uint32_t &length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006304{
Greg Clayton73b472d2010-10-27 03:32:59 +00006305 clang_type_t pointee_or_element_clang_type = NULL;
6306 Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, &pointee_or_element_clang_type));
6307
6308 if (pointee_or_element_clang_type == NULL)
6309 return false;
6310
6311 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006312 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006313 QualType pointee_or_element_qual_type (QualType::getFromOpaquePtr (pointee_or_element_clang_type));
6314
6315 if (pointee_or_element_qual_type.getUnqualifiedType()->isCharType())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006316 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006317 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6318 if (type_flags.Test (eTypeIsArray))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006319 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006320 // We know the size of the array and it could be a C string
6321 // since it is an array of characters
6322 length = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
6323 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006324 }
Greg Clayton73b472d2010-10-27 03:32:59 +00006325 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006326 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006327 length = 0;
6328 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006329 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006330
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006331 }
6332 }
6333 return false;
6334}
6335
6336bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006337ClangASTContext::IsFunctionPointerType (clang_type_t clang_type)
Greg Clayton737b9322010-09-13 03:32:57 +00006338{
6339 if (clang_type)
6340 {
6341 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6342
6343 if (qual_type->isFunctionPointerType())
6344 return true;
6345
6346 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6347 switch (type_class)
6348 {
Sean Callananfb0b7582011-03-15 00:17:19 +00006349 default:
6350 break;
Greg Clayton737b9322010-09-13 03:32:57 +00006351 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00006352 return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006353 case clang::Type::Elaborated:
6354 return ClangASTContext::IsFunctionPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00006355
6356 case clang::Type::LValueReference:
6357 case clang::Type::RValueReference:
6358 {
Sean Callanan78e37602011-01-27 04:42:51 +00006359 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00006360 if (reference_type)
6361 return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr());
6362 }
6363 break;
6364 }
6365 }
6366 return false;
6367}
6368
Greg Clayton73b472d2010-10-27 03:32:59 +00006369size_t
6370ClangASTContext::GetArraySize (clang_type_t clang_type)
6371{
6372 if (clang_type)
6373 {
Greg Claytonef37d68a2011-07-09 17:12:27 +00006374 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
6375 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6376 switch (type_class)
6377 {
6378 case clang::Type::ConstantArray:
6379 {
6380 const ConstantArrayType *array = cast<ConstantArrayType>(QualType::getFromOpaquePtr(clang_type).getTypePtr());
6381 if (array)
6382 return array->getSize().getLimitedValue();
6383 }
6384 break;
6385
6386 case clang::Type::Typedef:
6387 return ClangASTContext::GetArraySize(cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006388
6389 case clang::Type::Elaborated:
6390 return ClangASTContext::GetArraySize(cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00006391
6392 default:
6393 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006394 }
Greg Clayton73b472d2010-10-27 03:32:59 +00006395 }
6396 return 0;
6397}
Greg Clayton737b9322010-09-13 03:32:57 +00006398
Sean Callanan0caa21c2012-01-19 23:54:24 +00006399clang_type_t
Greg Clayton4ef877f2012-12-06 02:33:54 +00006400ClangASTContext::GetAsArrayType (clang_type_t clang_type, clang_type_t*member_type, uint64_t *size, bool *is_incomplete)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006401{
Greg Clayton4ef877f2012-12-06 02:33:54 +00006402 if (is_incomplete)
6403 *is_incomplete = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006404 if (!clang_type)
Sean Callanan0caa21c2012-01-19 23:54:24 +00006405 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006406
6407 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6408
Greg Clayton737b9322010-09-13 03:32:57 +00006409 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6410 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006411 {
Sean Callananfb0b7582011-03-15 00:17:19 +00006412 default:
6413 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006414
Greg Claytone1a916a2010-07-21 22:12:05 +00006415 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006416 if (member_type)
6417 *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6418 if (size)
Greg Claytonac4827f2011-04-01 18:14:08 +00006419 *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
Sean Callanan0caa21c2012-01-19 23:54:24 +00006420 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006421
Greg Claytone1a916a2010-07-21 22:12:05 +00006422 case clang::Type::IncompleteArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006423 if (member_type)
6424 *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6425 if (size)
6426 *size = 0;
Greg Clayton4ef877f2012-12-06 02:33:54 +00006427 if (is_incomplete)
6428 *is_incomplete = true;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006429 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006430
Greg Claytone1a916a2010-07-21 22:12:05 +00006431 case clang::Type::VariableArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006432 if (member_type)
6433 *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6434 if (size)
6435 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006436 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006437
Greg Claytone1a916a2010-07-21 22:12:05 +00006438 case clang::Type::DependentSizedArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006439 if (member_type)
6440 *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6441 if (size)
6442 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006443 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006444
6445 case clang::Type::Typedef:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006446 return ClangASTContext::GetAsArrayType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
6447 member_type,
Greg Clayton4ef877f2012-12-06 02:33:54 +00006448 size,
6449 is_incomplete);
Sean Callanan912855f2011-08-11 23:56:13 +00006450
6451 case clang::Type::Elaborated:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006452 return ClangASTContext::GetAsArrayType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
6453 member_type,
Greg Clayton4ef877f2012-12-06 02:33:54 +00006454 size,
6455 is_incomplete);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006456 }
Sean Callanan0caa21c2012-01-19 23:54:24 +00006457 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006458}
6459
6460
6461#pragma mark Typedefs
6462
Greg Clayton1be10fc2010-09-29 01:12:09 +00006463clang_type_t
6464ClangASTContext::CreateTypedefType (const char *name, clang_type_t clang_type, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006465{
6466 if (clang_type)
6467 {
6468 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006469 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006470 IdentifierTable *identifier_table = getIdentifierTable();
Greg Clayton6beaaa62011-01-17 03:46:26 +00006471 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006472 assert (identifier_table != NULL);
6473 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00006474 decl_ctx = ast->getTranslationUnitDecl();
6475 TypedefDecl *decl = TypedefDecl::Create (*ast,
6476 decl_ctx,
6477 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00006478 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00006479 name ? &identifier_table->get(name) : NULL, // Identifier
Sean Callanan34cf8202013-03-12 21:22:00 +00006480 ast->getTrivialTypeSourceInfo(qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00006481
Greg Clayton147e1fa2011-10-14 22:47:18 +00006482 //decl_ctx->addDecl (decl);
6483
Sean Callanan2652ad22011-01-18 01:03:44 +00006484 decl->setAccess(AS_public); // TODO respect proper access specifier
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006485
6486 // Get a uniqued QualType for the typedef decl type
Greg Clayton6beaaa62011-01-17 03:46:26 +00006487 return ast->getTypedefType (decl).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006488 }
6489 return NULL;
6490}
6491
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006492// Disable this for now since I can't seem to get a nicely formatted float
6493// out of the APFloat class without just getting the float, double or quad
6494// and then using a formatted print on it which defeats the purpose. We ideally
6495// would like to get perfect string values for any kind of float semantics
6496// so we can support remote targets. The code below also requires a patch to
6497// llvm::APInt.
6498//bool
Greg Clayton6beaaa62011-01-17 03:46:26 +00006499//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 +00006500//{
6501// uint32_t count = 0;
6502// bool is_complex = false;
6503// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6504// {
6505// unsigned num_bytes_per_float = byte_size / count;
6506// unsigned num_bits_per_float = num_bytes_per_float * 8;
6507//
6508// float_str.clear();
6509// uint32_t i;
6510// for (i=0; i<count; i++)
6511// {
6512// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
6513// bool is_ieee = false;
6514// APFloat ap_float(ap_int, is_ieee);
6515// char s[1024];
6516// unsigned int hex_digits = 0;
6517// bool upper_case = false;
6518//
6519// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
6520// {
6521// if (i > 0)
6522// float_str.append(", ");
6523// float_str.append(s);
6524// if (i == 1 && is_complex)
6525// float_str.append(1, 'i');
6526// }
6527// }
6528// return !float_str.empty();
6529// }
6530// return false;
6531//}
6532
6533size_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00006534ClangASTContext::ConvertStringToFloatValue (ASTContext *ast, clang_type_t clang_type, const char *s, uint8_t *dst, size_t dst_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006535{
6536 if (clang_type)
6537 {
6538 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6539 uint32_t count = 0;
6540 bool is_complex = false;
6541 if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6542 {
6543 // TODO: handle complex and vector types
6544 if (count != 1)
6545 return false;
6546
6547 StringRef s_sref(s);
Greg Clayton6beaaa62011-01-17 03:46:26 +00006548 APFloat ap_float(ast->getFloatTypeSemantics(qual_type), s_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006549
Greg Clayton6beaaa62011-01-17 03:46:26 +00006550 const uint64_t bit_size = ast->getTypeSize (qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006551 const uint64_t byte_size = bit_size / 8;
6552 if (dst_size >= byte_size)
6553 {
6554 if (bit_size == sizeof(float)*8)
6555 {
6556 float float32 = ap_float.convertToFloat();
6557 ::memcpy (dst, &float32, byte_size);
6558 return byte_size;
6559 }
6560 else if (bit_size >= 64)
6561 {
6562 llvm::APInt ap_int(ap_float.bitcastToAPInt());
6563 ::memcpy (dst, ap_int.getRawData(), byte_size);
6564 return byte_size;
6565 }
6566 }
6567 }
6568 }
6569 return 0;
6570}
Sean Callanan6fe64b52010-09-17 02:24:29 +00006571
6572unsigned
Greg Clayton1be10fc2010-09-29 01:12:09 +00006573ClangASTContext::GetTypeQualifiers(clang_type_t clang_type)
Sean Callanan6fe64b52010-09-17 02:24:29 +00006574{
6575 assert (clang_type);
6576
6577 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6578
6579 return qual_type.getQualifiers().getCVRQualifiers();
6580}
Greg Clayton6beaaa62011-01-17 03:46:26 +00006581
6582bool
6583ClangASTContext::GetCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6584{
6585 if (clang_type == NULL)
6586 return false;
6587
Greg Claytonc432c192011-01-20 04:18:48 +00006588 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006589}
6590
6591
6592bool
6593ClangASTContext::GetCompleteType (clang_type_t clang_type)
6594{
6595 return ClangASTContext::GetCompleteType (getASTContext(), clang_type);
6596}
6597
Greg Claytona2721472011-06-25 00:44:06 +00006598bool
Enrico Granata86027e92012-03-24 01:11:14 +00006599ClangASTContext::IsCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6600{
6601 if (clang_type == NULL)
6602 return false;
6603
6604 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type), false); // just check but don't let it actually complete
6605}
6606
6607
6608bool
6609ClangASTContext::IsCompleteType (clang_type_t clang_type)
6610{
6611 return ClangASTContext::IsCompleteType (getASTContext(), clang_type);
6612}
6613
6614bool
Greg Claytona2721472011-06-25 00:44:06 +00006615ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
6616 clang::Decl *decl)
6617{
6618 if (!decl)
6619 return false;
6620
6621 ExternalASTSource *ast_source = ast->getExternalSource();
6622
6623 if (!ast_source)
6624 return false;
6625
6626 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
6627 {
Greg Clayton219cf312012-03-30 00:51:13 +00006628 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006629 return true;
6630
6631 if (!tag_decl->hasExternalLexicalStorage())
6632 return false;
6633
6634 ast_source->CompleteType(tag_decl);
6635
6636 return !tag_decl->getTypeForDecl()->isIncompleteType();
6637 }
6638 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
6639 {
Sean Callanan5b26f272012-02-04 08:49:35 +00006640 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006641 return true;
6642
6643 if (!objc_interface_decl->hasExternalLexicalStorage())
6644 return false;
6645
6646 ast_source->CompleteType(objc_interface_decl);
6647
Sean Callanan5b26f272012-02-04 08:49:35 +00006648 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00006649 }
6650 else
6651 {
6652 return false;
6653 }
6654}
6655
Sean Callanan60217122012-04-13 00:10:03 +00006656void
Greg Claytond0029442013-03-27 01:48:02 +00006657ClangASTContext::SetMetadataAsUserID (const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00006658 user_id_t user_id)
6659{
6660 ClangASTMetadata meta_data;
6661 meta_data.SetUserID (user_id);
6662 SetMetadata (object, meta_data);
6663}
6664
6665void
Sean Callanan60217122012-04-13 00:10:03 +00006666ClangASTContext::SetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00006667 const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00006668 ClangASTMetadata &metadata)
Sean Callanan60217122012-04-13 00:10:03 +00006669{
6670 ClangExternalASTSourceCommon *external_source =
6671 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
6672
6673 if (external_source)
6674 external_source->SetMetadata(object, metadata);
6675}
6676
Jim Ingham379397632012-10-27 02:54:13 +00006677ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +00006678ClangASTContext::GetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00006679 const void *object)
Sean Callanan60217122012-04-13 00:10:03 +00006680{
6681 ClangExternalASTSourceCommon *external_source =
6682 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
6683
6684 if (external_source && external_source->HasMetadata(object))
6685 return external_source->GetMetadata(object);
6686 else
Jim Ingham379397632012-10-27 02:54:13 +00006687 return NULL;
Sean Callanan60217122012-04-13 00:10:03 +00006688}
6689
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006690clang::DeclContext *
6691ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
6692{
Sean Callanana87bee82011-08-19 06:19:25 +00006693 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006694}
6695
6696clang::DeclContext *
6697ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
6698{
Sean Callanana87bee82011-08-19 06:19:25 +00006699 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006700}
6701
Greg Clayton685c88c2012-07-14 00:53:55 +00006702
6703bool
6704ClangASTContext::GetClassMethodInfoForDeclContext (clang::DeclContext *decl_ctx,
6705 lldb::LanguageType &language,
6706 bool &is_instance_method,
6707 ConstString &language_object_name)
6708{
6709 language_object_name.Clear();
6710 language = eLanguageTypeUnknown;
6711 is_instance_method = false;
6712
6713 if (decl_ctx)
6714 {
6715 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
6716 {
6717 if (method_decl->isStatic())
6718 {
6719 is_instance_method = false;
6720 }
6721 else
6722 {
6723 language_object_name.SetCString("this");
6724 is_instance_method = true;
6725 }
6726 language = eLanguageTypeC_plus_plus;
6727 return true;
6728 }
6729 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
6730 {
6731 // Both static and instance methods have a "self" object in objective C
6732 language_object_name.SetCString("self");
6733 if (method_decl->isInstanceMethod())
6734 {
6735 is_instance_method = true;
6736 }
6737 else
6738 {
6739 is_instance_method = false;
6740 }
6741 language = eLanguageTypeObjC;
6742 return true;
6743 }
Jim Ingham379397632012-10-27 02:54:13 +00006744 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
6745 {
Greg Claytond0029442013-03-27 01:48:02 +00006746 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), function_decl);
Jim Ingham379397632012-10-27 02:54:13 +00006747 if (metadata && metadata->HasObjectPtr())
6748 {
6749 language_object_name.SetCString (metadata->GetObjectPtrName());
6750 language = eLanguageTypeObjC;
6751 is_instance_method = true;
6752 }
6753 return true;
6754 }
Greg Clayton685c88c2012-07-14 00:53:55 +00006755 }
6756 return false;
6757}
6758