blob: 3397b0edea83dcb568de9751905eb9b00b2c6ca5 [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 default: break;
198 case eAccessNone: return ObjCIvarDecl::None;
199 case eAccessPublic: return ObjCIvarDecl::Public;
200 case eAccessPrivate: return ObjCIvarDecl::Private;
201 case eAccessProtected: return ObjCIvarDecl::Protected;
202 case eAccessPackage: return ObjCIvarDecl::Package;
Greg Clayton8cf05932010-07-22 18:30:50 +0000203 }
204 return ObjCIvarDecl::None;
205}
206
207
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208static void
209ParseLangArgs
210(
211 LangOptions &Opts,
Greg Clayton94e5d782010-06-13 17:34:29 +0000212 InputKind IK
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213)
214{
215 // FIXME: Cleanup per-file based stuff.
216
217 // Set some properties which depend soley on the input kind; it would be nice
218 // to move these to the language standard, and have the driver resolve the
219 // input kind + language standard.
Greg Clayton94e5d782010-06-13 17:34:29 +0000220 if (IK == IK_Asm) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221 Opts.AsmPreprocessor = 1;
Greg Clayton94e5d782010-06-13 17:34:29 +0000222 } else if (IK == IK_ObjC ||
223 IK == IK_ObjCXX ||
224 IK == IK_PreprocessedObjC ||
225 IK == IK_PreprocessedObjCXX) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226 Opts.ObjC1 = Opts.ObjC2 = 1;
227 }
228
229 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
230
231 if (LangStd == LangStandard::lang_unspecified) {
232 // Based on the base language, pick one.
233 switch (IK) {
Greg Clayton94e5d782010-06-13 17:34:29 +0000234 case IK_None:
235 case IK_AST:
Sean Callananfb0b7582011-03-15 00:17:19 +0000236 case IK_LLVM_IR:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000237 assert (!"Invalid input kind!");
Greg Clayton94e5d782010-06-13 17:34:29 +0000238 case IK_OpenCL:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239 LangStd = LangStandard::lang_opencl;
240 break;
Sean Callananfb0b7582011-03-15 00:17:19 +0000241 case IK_CUDA:
242 LangStd = LangStandard::lang_cuda;
243 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000244 case IK_Asm:
245 case IK_C:
246 case IK_PreprocessedC:
247 case IK_ObjC:
248 case IK_PreprocessedObjC:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249 LangStd = LangStandard::lang_gnu99;
250 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000251 case IK_CXX:
252 case IK_PreprocessedCXX:
253 case IK_ObjCXX:
254 case IK_PreprocessedObjCXX:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255 LangStd = LangStandard::lang_gnucxx98;
256 break;
257 }
258 }
259
260 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
Filipe Cabecinhase818ca22012-11-12 21:26:32 +0000261 Opts.LineComment = Std.hasLineComments();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262 Opts.C99 = Std.isC99();
263 Opts.CPlusPlus = Std.isCPlusPlus();
264 Opts.CPlusPlus0x = Std.isCPlusPlus0x();
265 Opts.Digraphs = Std.hasDigraphs();
266 Opts.GNUMode = Std.isGNUMode();
267 Opts.GNUInline = !Std.isC99();
268 Opts.HexFloats = Std.hasHexFloats();
269 Opts.ImplicitInt = Std.hasImplicitInt();
270
271 // OpenCL has some additional defaults.
272 if (LangStd == LangStandard::lang_opencl) {
273 Opts.OpenCL = 1;
274 Opts.AltiVec = 1;
275 Opts.CXXOperatorNames = 1;
276 Opts.LaxVectorConversions = 1;
277 }
278
279 // OpenCL and C++ both have bool, true, false keywords.
280 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
281
282// if (Opts.CPlusPlus)
283// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
284//
285// if (Args.hasArg(OPT_fobjc_gc_only))
286// Opts.setGCMode(LangOptions::GCOnly);
287// else if (Args.hasArg(OPT_fobjc_gc))
288// Opts.setGCMode(LangOptions::HybridGC);
289//
290// if (Args.hasArg(OPT_print_ivar_layout))
291// Opts.ObjCGCBitmapPrint = 1;
292//
293// if (Args.hasArg(OPT_faltivec))
294// Opts.AltiVec = 1;
295//
296// if (Args.hasArg(OPT_pthread))
297// Opts.POSIXThreads = 1;
298//
299// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
300// "default");
301// if (Vis == "default")
Sean Callanan31e851c2010-10-29 18:38:40 +0000302 Opts.setVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303// else if (Vis == "hidden")
304// Opts.setVisibilityMode(LangOptions::Hidden);
305// else if (Vis == "protected")
306// Opts.setVisibilityMode(LangOptions::Protected);
307// else
308// Diags.Report(diag::err_drv_invalid_value)
309// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
310
311// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
312
313 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
314 // is specified, or -std is set to a conforming mode.
315 Opts.Trigraphs = !Opts.GNUMode;
316// if (Args.hasArg(OPT_trigraphs))
317// Opts.Trigraphs = 1;
318//
319// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
320// OPT_fno_dollars_in_identifiers,
321// !Opts.AsmPreprocessor);
322// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
323// Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
324// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
325// if (Args.hasArg(OPT_fno_lax_vector_conversions))
326// Opts.LaxVectorConversions = 0;
327// Opts.Exceptions = Args.hasArg(OPT_fexceptions);
328// Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
329// Opts.Blocks = Args.hasArg(OPT_fblocks);
330// Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char);
331// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
332// Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
333// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
334// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
335// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
336// Opts.AccessControl = Args.hasArg(OPT_faccess_control);
337// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
338// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
339// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
340// Diags);
341// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
342// Opts.ObjCConstantStringClass = getLastArgValue(Args,
343// OPT_fconstant_string_class);
344// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
345// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
346// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
347// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
348// Opts.Static = Args.hasArg(OPT_static_define);
349 Opts.OptimizeSize = 0;
350
351 // FIXME: Eliminate this dependency.
352// unsigned Opt =
353// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
354// Opts.Optimize = Opt != 0;
355 unsigned Opt = 0;
356
357 // This is the __NO_INLINE__ define, which just depends on things like the
358 // optimization level and -fno-inline, not actually whether the backend has
359 // inlining enabled.
360 //
361 // FIXME: This is affected by other options (-fno-inline).
Sean Callanan3d654b32012-09-24 22:25:51 +0000362 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363
364// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
365// switch (SSP) {
366// default:
367// Diags.Report(diag::err_drv_invalid_value)
368// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
369// break;
370// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
371// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
372// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
373// }
374}
375
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376
Greg Clayton6beaaa62011-01-17 03:46:26 +0000377ClangASTContext::ClangASTContext (const char *target_triple) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378 m_target_triple(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000379 m_ast_ap(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000380 m_language_options_ap(),
381 m_source_manager_ap(),
Sean Callanan880e6802011-10-07 23:18:13 +0000382 m_diagnostics_engine_ap(),
Sean Callananc5069ad2012-10-17 22:11:14 +0000383 m_target_options_rp(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 m_target_info_ap(),
385 m_identifier_table_ap(),
386 m_selector_table_ap(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000387 m_builtins_ap(),
388 m_callback_tag_decl (NULL),
389 m_callback_objc_decl (NULL),
390 m_callback_baton (NULL)
391
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392{
393 if (target_triple && target_triple[0])
Greg Clayton880cbb02011-07-30 01:26:02 +0000394 SetTargetTriple (target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395}
396
397//----------------------------------------------------------------------
398// Destructor
399//----------------------------------------------------------------------
400ClangASTContext::~ClangASTContext()
401{
402 m_builtins_ap.reset();
403 m_selector_table_ap.reset();
404 m_identifier_table_ap.reset();
405 m_target_info_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000406 m_target_options_rp.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000407 m_diagnostics_engine_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408 m_source_manager_ap.reset();
409 m_language_options_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000410 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411}
412
413
414void
415ClangASTContext::Clear()
416{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000417 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418 m_language_options_ap.reset();
419 m_source_manager_ap.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000420 m_diagnostics_engine_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000421 m_target_options_rp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422 m_target_info_ap.reset();
423 m_identifier_table_ap.reset();
424 m_selector_table_ap.reset();
425 m_builtins_ap.reset();
426}
427
428const char *
429ClangASTContext::GetTargetTriple ()
430{
431 return m_target_triple.c_str();
432}
433
434void
435ClangASTContext::SetTargetTriple (const char *target_triple)
436{
437 Clear();
438 m_target_triple.assign(target_triple);
439}
440
Greg Clayton514487e2011-02-15 21:59:32 +0000441void
442ClangASTContext::SetArchitecture (const ArchSpec &arch)
443{
Greg Clayton880cbb02011-07-30 01:26:02 +0000444 SetTargetTriple(arch.GetTriple().str().c_str());
Greg Clayton514487e2011-02-15 21:59:32 +0000445}
446
Greg Clayton6beaaa62011-01-17 03:46:26 +0000447bool
448ClangASTContext::HasExternalSource ()
449{
450 ASTContext *ast = getASTContext();
451 if (ast)
452 return ast->getExternalSource () != NULL;
453 return false;
454}
455
456void
457ClangASTContext::SetExternalSource (llvm::OwningPtr<ExternalASTSource> &ast_source_ap)
458{
459 ASTContext *ast = getASTContext();
460 if (ast)
461 {
462 ast->setExternalSource (ast_source_ap);
463 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
464 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
465 }
466}
467
468void
469ClangASTContext::RemoveExternalSource ()
470{
471 ASTContext *ast = getASTContext();
472
473 if (ast)
474 {
475 llvm::OwningPtr<ExternalASTSource> empty_ast_source_ap;
476 ast->setExternalSource (empty_ast_source_ap);
477 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
478 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
479 }
480}
481
482
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483
484ASTContext *
485ClangASTContext::getASTContext()
486{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000487 if (m_ast_ap.get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488 {
Greg Clayton6beaaa62011-01-17 03:46:26 +0000489 m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
490 *getSourceManager(),
Sean Callanan880e6802011-10-07 23:18:13 +0000491 getTargetInfo(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000492 *getIdentifierTable(),
493 *getSelectorTable(),
494 *getBuiltinContext(),
495 0));
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000496
Greg Clayton6beaaa62011-01-17 03:46:26 +0000497 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
498 {
499 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
500 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
501 }
502
Sean Callanan880e6802011-10-07 23:18:13 +0000503 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000504 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000505 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506}
507
508Builtin::Context *
509ClangASTContext::getBuiltinContext()
510{
511 if (m_builtins_ap.get() == NULL)
Sean Callanan880e6802011-10-07 23:18:13 +0000512 m_builtins_ap.reset (new Builtin::Context());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513 return m_builtins_ap.get();
514}
515
516IdentifierTable *
517ClangASTContext::getIdentifierTable()
518{
519 if (m_identifier_table_ap.get() == NULL)
520 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), NULL));
521 return m_identifier_table_ap.get();
522}
523
524LangOptions *
525ClangASTContext::getLanguageOptions()
526{
527 if (m_language_options_ap.get() == NULL)
528 {
529 m_language_options_ap.reset(new LangOptions());
Greg Clayton94e5d782010-06-13 17:34:29 +0000530 ParseLangArgs(*m_language_options_ap, IK_ObjCXX);
531// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000532 }
533 return m_language_options_ap.get();
534}
535
536SelectorTable *
537ClangASTContext::getSelectorTable()
538{
539 if (m_selector_table_ap.get() == NULL)
540 m_selector_table_ap.reset (new SelectorTable());
541 return m_selector_table_ap.get();
542}
543
Sean Callanan79439e82010-11-18 02:56:27 +0000544clang::FileManager *
545ClangASTContext::getFileManager()
546{
547 if (m_file_manager_ap.get() == NULL)
Greg Clayton38a61402010-12-02 23:20:03 +0000548 {
549 clang::FileSystemOptions file_system_options;
550 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
551 }
Sean Callanan79439e82010-11-18 02:56:27 +0000552 return m_file_manager_ap.get();
553}
554
Greg Claytone1a916a2010-07-21 22:12:05 +0000555clang::SourceManager *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556ClangASTContext::getSourceManager()
557{
558 if (m_source_manager_ap.get() == NULL)
Sean Callanan880e6802011-10-07 23:18:13 +0000559 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560 return m_source_manager_ap.get();
561}
562
Sean Callanan880e6802011-10-07 23:18:13 +0000563clang::DiagnosticsEngine *
564ClangASTContext::getDiagnosticsEngine()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565{
Sean Callanan880e6802011-10-07 23:18:13 +0000566 if (m_diagnostics_engine_ap.get() == NULL)
Greg Claytona651b532010-11-19 21:46:54 +0000567 {
568 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Sean Callananec8f1ef2012-10-25 01:00:25 +0000569 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
Greg Claytona651b532010-11-19 21:46:54 +0000570 }
Sean Callanan880e6802011-10-07 23:18:13 +0000571 return m_diagnostics_engine_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000572}
573
Sean Callanan880e6802011-10-07 23:18:13 +0000574class NullDiagnosticConsumer : public DiagnosticConsumer
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000575{
576public:
Sean Callanan880e6802011-10-07 23:18:13 +0000577 NullDiagnosticConsumer ()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000578 {
579 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
580 }
581
Sean Callanan880e6802011-10-07 23:18:13 +0000582 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const Diagnostic &info)
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000583 {
584 if (m_log)
585 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000586 llvm::SmallVector<char, 32> diag_str(10);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000587 info.FormatDiagnostic(diag_str);
588 diag_str.push_back('\0');
589 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
590 }
591 }
Sean Callanan880e6802011-10-07 23:18:13 +0000592
593 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
594 {
595 return new NullDiagnosticConsumer ();
596 }
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000597private:
598 LogSP m_log;
599};
600
Sean Callanan880e6802011-10-07 23:18:13 +0000601DiagnosticConsumer *
602ClangASTContext::getDiagnosticConsumer()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000603{
Sean Callanan880e6802011-10-07 23:18:13 +0000604 if (m_diagnostic_consumer_ap.get() == NULL)
605 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000606
Sean Callanan880e6802011-10-07 23:18:13 +0000607 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000608}
609
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610TargetOptions *
611ClangASTContext::getTargetOptions()
612{
Sean Callananc5069ad2012-10-17 22:11:14 +0000613 if (m_target_options_rp.getPtr() == NULL && !m_target_triple.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614 {
Sean Callananc5069ad2012-10-17 22:11:14 +0000615 m_target_options_rp.reset ();
616 m_target_options_rp = new TargetOptions();
617 if (m_target_options_rp.getPtr() != NULL)
618 m_target_options_rp->Triple = m_target_triple;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619 }
Sean Callananc5069ad2012-10-17 22:11:14 +0000620 return m_target_options_rp.getPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621}
622
623
624TargetInfo *
625ClangASTContext::getTargetInfo()
626{
Greg Clayton70512312012-05-08 01:45:38 +0000627 // target_triple should be something like "x86_64-apple-macosx"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628 if (m_target_info_ap.get() == NULL && !m_target_triple.empty())
Greg Clayton38d880a2012-11-16 21:35:22 +0000629 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), getTargetOptions()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630 return m_target_info_ap.get();
631}
632
633#pragma mark Basic Types
634
635static inline bool
Greg Clayton6beaaa62011-01-17 03:46:26 +0000636QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000638 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639 if (qual_type_bit_size == bit_size)
640 return true;
641 return false;
642}
643
Greg Clayton1be10fc2010-09-29 01:12:09 +0000644clang_type_t
Greg Claytonc86103d2010-08-05 01:57:25 +0000645ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000647 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648
Greg Clayton6beaaa62011-01-17 03:46:26 +0000649 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650
Greg Clayton6beaaa62011-01-17 03:46:26 +0000651 return GetBuiltinTypeForEncodingAndBitSize (ast, encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652}
653
Greg Clayton1be10fc2010-09-29 01:12:09 +0000654clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000655ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000656{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000657 if (!ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658 return NULL;
659
660 switch (encoding)
661 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000662 case eEncodingInvalid:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000663 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
664 return ast->VoidPtrTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665 break;
666
Greg Claytonc86103d2010-08-05 01:57:25 +0000667 case eEncodingUint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000668 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
669 return ast->UnsignedCharTy.getAsOpaquePtr();
670 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
671 return ast->UnsignedShortTy.getAsOpaquePtr();
672 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
673 return ast->UnsignedIntTy.getAsOpaquePtr();
674 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
675 return ast->UnsignedLongTy.getAsOpaquePtr();
676 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
677 return ast->UnsignedLongLongTy.getAsOpaquePtr();
678 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
679 return ast->UnsignedInt128Ty.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000680 break;
681
Greg Claytonc86103d2010-08-05 01:57:25 +0000682 case eEncodingSint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000683 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
684 return ast->CharTy.getAsOpaquePtr();
685 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
686 return ast->ShortTy.getAsOpaquePtr();
687 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
688 return ast->IntTy.getAsOpaquePtr();
689 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
690 return ast->LongTy.getAsOpaquePtr();
691 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
692 return ast->LongLongTy.getAsOpaquePtr();
693 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
694 return ast->Int128Ty.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695 break;
696
Greg Claytonc86103d2010-08-05 01:57:25 +0000697 case eEncodingIEEE754:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000698 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
699 return ast->FloatTy.getAsOpaquePtr();
700 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
701 return ast->DoubleTy.getAsOpaquePtr();
702 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
703 return ast->LongDoubleTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704 break;
705
Greg Claytonc86103d2010-08-05 01:57:25 +0000706 case eEncodingVector:
Johnny Chenc79c93a2012-03-07 01:12:24 +0000707 // Sanity check that bit_size is a multiple of 8's.
708 if (bit_size && !(bit_size & 0x7u))
709 return ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8).getAsOpaquePtr();
710 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711 default:
712 break;
713 }
714
715 return NULL;
716}
717
Greg Clayton1be10fc2010-09-29 01:12:09 +0000718clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
720{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000721 ASTContext *ast = getASTContext();
Sean Callanan38d4df52012-04-03 01:10:10 +0000722
723#define streq(a,b) strcmp(a,b) == 0
Greg Clayton6beaaa62011-01-17 03:46:26 +0000724 assert (ast != NULL);
725 if (ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000726 {
727 switch (dw_ate)
728 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000729 default:
730 break;
Greg Clayton605684e2011-10-28 23:06:08 +0000731
Sean Callanan38d4df52012-04-03 01:10:10 +0000732 case DW_ATE_address:
733 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
734 return ast->VoidPtrTy.getAsOpaquePtr();
735 break;
736
737 case DW_ATE_boolean:
738 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
739 return ast->BoolTy.getAsOpaquePtr();
740 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
741 return ast->UnsignedCharTy.getAsOpaquePtr();
742 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
743 return ast->UnsignedShortTy.getAsOpaquePtr();
744 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
745 return ast->UnsignedIntTy.getAsOpaquePtr();
746 break;
747
748 case DW_ATE_lo_user:
749 // This has been seen to mean DW_AT_complex_integer
750 if (type_name)
Greg Clayton605684e2011-10-28 23:06:08 +0000751 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000752 if (::strstr(type_name, "complex"))
753 {
754 clang_type_t complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2);
755 return ast->getComplexType (QualType::getFromOpaquePtr(complex_int_clang_type)).getAsOpaquePtr();
756 }
Greg Clayton605684e2011-10-28 23:06:08 +0000757 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000758 break;
759
760 case DW_ATE_complex_float:
761 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
762 return ast->FloatComplexTy.getAsOpaquePtr();
763 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
764 return ast->DoubleComplexTy.getAsOpaquePtr();
765 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
766 return ast->LongDoubleComplexTy.getAsOpaquePtr();
767 else
Greg Clayton605684e2011-10-28 23:06:08 +0000768 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000769 clang_type_t complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2);
770 return ast->getComplexType (QualType::getFromOpaquePtr(complex_float_clang_type)).getAsOpaquePtr();
Greg Clayton605684e2011-10-28 23:06:08 +0000771 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000772 break;
773
774 case DW_ATE_float:
775 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
776 return ast->FloatTy.getAsOpaquePtr();
777 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
778 return ast->DoubleTy.getAsOpaquePtr();
779 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
780 return ast->LongDoubleTy.getAsOpaquePtr();
781 break;
782
783 case DW_ATE_signed:
784 if (type_name)
785 {
786 if (streq(type_name, "wchar_t") &&
787 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
788 return ast->WCharTy.getAsOpaquePtr();
789 if (streq(type_name, "void") &&
790 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
791 return ast->VoidTy.getAsOpaquePtr();
792 if (strstr(type_name, "long long") &&
793 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
794 return ast->LongLongTy.getAsOpaquePtr();
795 if (strstr(type_name, "long") &&
796 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
797 return ast->LongTy.getAsOpaquePtr();
798 if (strstr(type_name, "short") &&
799 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
800 return ast->ShortTy.getAsOpaquePtr();
801 if (strstr(type_name, "char"))
802 {
803 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
804 return ast->CharTy.getAsOpaquePtr();
805 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
806 return ast->SignedCharTy.getAsOpaquePtr();
807 }
808 if (strstr(type_name, "int"))
809 {
810 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
811 return ast->IntTy.getAsOpaquePtr();
812 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
813 return ast->Int128Ty.getAsOpaquePtr();
814 }
815 }
816 // We weren't able to match up a type name, just search by size
817 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
818 return ast->CharTy.getAsOpaquePtr();
819 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
820 return ast->ShortTy.getAsOpaquePtr();
821 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
822 return ast->IntTy.getAsOpaquePtr();
823 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
824 return ast->LongTy.getAsOpaquePtr();
825 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
826 return ast->LongLongTy.getAsOpaquePtr();
827 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
828 return ast->Int128Ty.getAsOpaquePtr();
829 break;
830
831 case DW_ATE_signed_char:
832 if (type_name)
833 {
834 if (streq(type_name, "signed char"))
835 {
836 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
837 return ast->SignedCharTy.getAsOpaquePtr();
838 }
839 }
840 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
841 return ast->CharTy.getAsOpaquePtr();
842 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
843 return ast->SignedCharTy.getAsOpaquePtr();
844 break;
845
846 case DW_ATE_unsigned:
847 if (type_name)
848 {
849 if (strstr(type_name, "long long"))
850 {
851 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
852 return ast->UnsignedLongLongTy.getAsOpaquePtr();
853 }
854 else if (strstr(type_name, "long"))
855 {
856 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
857 return ast->UnsignedLongTy.getAsOpaquePtr();
858 }
859 else if (strstr(type_name, "short"))
860 {
861 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
862 return ast->UnsignedShortTy.getAsOpaquePtr();
863 }
864 else if (strstr(type_name, "char"))
865 {
866 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
867 return ast->UnsignedCharTy.getAsOpaquePtr();
868 }
869 else if (strstr(type_name, "int"))
870 {
871 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
872 return ast->UnsignedIntTy.getAsOpaquePtr();
873 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
874 return ast->UnsignedInt128Ty.getAsOpaquePtr();
875 }
876 }
877 // We weren't able to match up a type name, just search by size
878 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
879 return ast->UnsignedCharTy.getAsOpaquePtr();
880 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
881 return ast->UnsignedShortTy.getAsOpaquePtr();
882 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
883 return ast->UnsignedIntTy.getAsOpaquePtr();
884 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
885 return ast->UnsignedLongTy.getAsOpaquePtr();
886 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
887 return ast->UnsignedLongLongTy.getAsOpaquePtr();
888 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
889 return ast->UnsignedInt128Ty.getAsOpaquePtr();
890 break;
891
892 case DW_ATE_unsigned_char:
893 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
894 return ast->UnsignedCharTy.getAsOpaquePtr();
895 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
896 return ast->UnsignedShortTy.getAsOpaquePtr();
897 break;
898
899 case DW_ATE_imaginary_float:
900 break;
901
902 case DW_ATE_UTF:
903 if (type_name)
904 {
905 if (streq(type_name, "char16_t"))
906 {
907 return ast->Char16Ty.getAsOpaquePtr();
908 }
909 else if (streq(type_name, "char32_t"))
910 {
911 return ast->Char32Ty.getAsOpaquePtr();
912 }
913 }
914 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000915 }
916 }
917 // This assert should fire for anything that we don't catch above so we know
918 // to fix any issues we run into.
Greg Claytondc968d12011-05-17 18:15:05 +0000919 if (type_name)
920 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000921 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 +0000922 }
923 else
924 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000925 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 +0000926 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000927 return NULL;
928}
929
Greg Clayton1be10fc2010-09-29 01:12:09 +0000930clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000931ClangASTContext::GetBuiltInType_void(ASTContext *ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000932{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000933 return ast->VoidTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000934}
935
Greg Clayton1be10fc2010-09-29 01:12:09 +0000936clang_type_t
Sean Callananf7c3e272010-11-19 02:52:21 +0000937ClangASTContext::GetBuiltInType_bool()
938{
939 return getASTContext()->BoolTy.getAsOpaquePtr();
940}
941
942clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000943ClangASTContext::GetBuiltInType_objc_id()
944{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000945 return getASTContext()->getObjCIdType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000946}
947
Greg Clayton1be10fc2010-09-29 01:12:09 +0000948clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000949ClangASTContext::GetBuiltInType_objc_Class()
950{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000951 return getASTContext()->getObjCClassType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000952}
953
Greg Clayton1be10fc2010-09-29 01:12:09 +0000954clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000955ClangASTContext::GetBuiltInType_objc_selector()
956{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000957 return getASTContext()->getObjCSelType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000958}
959
Greg Clayton1be10fc2010-09-29 01:12:09 +0000960clang_type_t
Sean Callanan77502262011-05-12 23:54:16 +0000961ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast)
962{
963 return ast->UnknownAnyTy.getAsOpaquePtr();
964}
965
966clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000967ClangASTContext::GetCStringType (bool is_const)
968{
969 QualType char_type(getASTContext()->CharTy);
970
971 if (is_const)
972 char_type.addConst();
973
974 return getASTContext()->getPointerType(char_type).getAsOpaquePtr();
975}
976
Greg Clayton1be10fc2010-09-29 01:12:09 +0000977clang_type_t
Sean Callanana6582262012-04-05 00:12:52 +0000978ClangASTContext::GetVoidType()
979{
980 return GetVoidType(getASTContext());
981}
982
983clang_type_t
984ClangASTContext::GetVoidType(ASTContext *ast)
985{
986 return ast->VoidTy.getAsOpaquePtr();
987}
988
989clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000990ClangASTContext::GetVoidPtrType (bool is_const)
991{
992 return GetVoidPtrType(getASTContext(), is_const);
993}
994
Greg Clayton1be10fc2010-09-29 01:12:09 +0000995clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000996ClangASTContext::GetVoidPtrType (ASTContext *ast, bool is_const)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000997{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000998 QualType void_ptr_type(ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000999
1000 if (is_const)
1001 void_ptr_type.addConst();
1002
1003 return void_ptr_type.getAsOpaquePtr();
1004}
1005
Sean Callanan09ab4b72011-11-30 22:11:59 +00001006clang::DeclContext *
1007ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast)
1008{
1009 return ast->getTranslationUnitDecl();
1010}
1011
Greg Clayton1be10fc2010-09-29 01:12:09 +00001012clang_type_t
Greg Clayton38a61402010-12-02 23:20:03 +00001013ClangASTContext::CopyType (ASTContext *dst_ast,
1014 ASTContext *src_ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001015 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001016{
Sean Callanan79439e82010-11-18 02:56:27 +00001017 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001018 FileManager file_manager (file_system_options);
1019 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001020 *src_ast, file_manager,
1021 false);
Sean Callanan0617fcb2010-11-09 22:37:10 +00001022
Greg Clayton38a61402010-12-02 23:20:03 +00001023 QualType src (QualType::getFromOpaquePtr(clang_type));
1024 QualType dst (importer.Import(src));
Sean Callanan0617fcb2010-11-09 22:37:10 +00001025
1026 return dst.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001027}
1028
Greg Clayton526e5af2010-11-13 03:52:47 +00001029
1030clang::Decl *
Greg Clayton38a61402010-12-02 23:20:03 +00001031ClangASTContext::CopyDecl (ASTContext *dst_ast,
1032 ASTContext *src_ast,
Greg Clayton526e5af2010-11-13 03:52:47 +00001033 clang::Decl *source_decl)
Sean Callanan7fddd4c2010-12-11 00:08:56 +00001034{
Sean Callanan79439e82010-11-18 02:56:27 +00001035 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001036 FileManager file_manager (file_system_options);
1037 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001038 *src_ast, file_manager,
1039 false);
Greg Clayton526e5af2010-11-13 03:52:47 +00001040
1041 return importer.Import(source_decl);
1042}
1043
Sean Callanan23a30272010-07-16 00:00:27 +00001044bool
Greg Clayton84db9102012-03-26 23:03:23 +00001045ClangASTContext::AreTypesSame (ASTContext *ast,
1046 clang_type_t type1,
1047 clang_type_t type2,
1048 bool ignore_qualifiers)
Sean Callanan4dcca2622010-07-15 22:30:52 +00001049{
Greg Clayton55995eb2012-04-06 17:38:55 +00001050 if (type1 == type2)
1051 return true;
1052
Sean Callanan5056ab02012-02-18 02:01:03 +00001053 QualType type1_qual = QualType::getFromOpaquePtr(type1);
1054 QualType type2_qual = QualType::getFromOpaquePtr(type2);
1055
1056 if (ignore_qualifiers)
1057 {
1058 type1_qual = type1_qual.getUnqualifiedType();
1059 type2_qual = type2_qual.getUnqualifiedType();
1060 }
1061
1062 return ast->hasSameType (type1_qual,
1063 type2_qual);
Sean Callanan4dcca2622010-07-15 22:30:52 +00001064}
1065
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001066#pragma mark CVR modifiers
1067
Greg Clayton1be10fc2010-09-29 01:12:09 +00001068clang_type_t
1069ClangASTContext::AddConstModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070{
1071 if (clang_type)
1072 {
1073 QualType result(QualType::getFromOpaquePtr(clang_type));
1074 result.addConst();
1075 return result.getAsOpaquePtr();
1076 }
1077 return NULL;
1078}
1079
Greg Clayton1be10fc2010-09-29 01:12:09 +00001080clang_type_t
1081ClangASTContext::AddRestrictModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001082{
1083 if (clang_type)
1084 {
1085 QualType result(QualType::getFromOpaquePtr(clang_type));
1086 result.getQualifiers().setRestrict (true);
1087 return result.getAsOpaquePtr();
1088 }
1089 return NULL;
1090}
1091
Greg Clayton1be10fc2010-09-29 01:12:09 +00001092clang_type_t
1093ClangASTContext::AddVolatileModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094{
1095 if (clang_type)
1096 {
1097 QualType result(QualType::getFromOpaquePtr(clang_type));
1098 result.getQualifiers().setVolatile (true);
1099 return result.getAsOpaquePtr();
1100 }
1101 return NULL;
1102}
1103
Greg Clayton6beaaa62011-01-17 03:46:26 +00001104
1105clang_type_t
1106ClangASTContext::GetTypeForDecl (TagDecl *decl)
1107{
1108 // No need to call the getASTContext() accessor (which can create the AST
1109 // if it isn't created yet, because we can't have created a decl in this
1110 // AST if our AST didn't already exist...
1111 if (m_ast_ap.get())
1112 return m_ast_ap->getTagDeclType(decl).getAsOpaquePtr();
1113 return NULL;
1114}
1115
1116clang_type_t
1117ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1118{
1119 // No need to call the getASTContext() accessor (which can create the AST
1120 // if it isn't created yet, because we can't have created a decl in this
1121 // AST if our AST didn't already exist...
1122 if (m_ast_ap.get())
1123 return m_ast_ap->getObjCInterfaceType(decl).getAsOpaquePtr();
1124 return NULL;
1125}
1126
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001127#pragma mark Structure, Unions, Classes
1128
Greg Clayton1be10fc2010-09-29 01:12:09 +00001129clang_type_t
Jim Ingham379397632012-10-27 02:54:13 +00001130ClangASTContext::CreateRecordType (DeclContext *decl_ctx, AccessType access_type, const char *name, int kind, LanguageType language, ClangASTMetadata *metadata)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001131{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001132 ASTContext *ast = getASTContext();
1133 assert (ast != NULL);
Sean Callananad880762012-04-18 01:06:17 +00001134
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001135 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001136 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001137
Greg Clayton9e409562010-07-28 02:04:09 +00001138
Greg Claytone1be9962011-08-24 23:50:00 +00001139 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
Greg Clayton9e409562010-07-28 02:04:09 +00001140 {
Greg Claytonaaf99e02010-10-11 02:25:34 +00001141 bool isForwardDecl = true;
Greg Clayton9e409562010-07-28 02:04:09 +00001142 bool isInternal = false;
Sean Callananad880762012-04-18 01:06:17 +00001143 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal, metadata);
Greg Clayton9e409562010-07-28 02:04:09 +00001144 }
1145
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001146 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1147 // we will need to update this code. I was told to currently always use
1148 // the CXXRecordDecl class since we often don't know from debug information
1149 // if something is struct or a class, so we default to always use the more
1150 // complete definition just in case.
Greg Claytonf0705c82011-10-22 03:33:13 +00001151 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1152 (TagDecl::TagKind)kind,
1153 decl_ctx,
1154 SourceLocation(),
1155 SourceLocation(),
1156 name && name[0] ? &ast->Idents.get(name) : NULL);
Sean Callanan7282e2a2012-01-13 22:10:18 +00001157
Jim Ingham379397632012-10-27 02:54:13 +00001158 if (decl && metadata)
1159 SetMetadata(ast, (uintptr_t)decl, *metadata);
Sean Callanan60217122012-04-13 00:10:03 +00001160
Greg Clayton55561e92011-10-26 03:31:36 +00001161 if (decl_ctx)
1162 {
1163 if (access_type != eAccessNone)
1164 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
1165 decl_ctx->addDecl (decl);
1166 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001167 return ast->getTagDeclType(decl).getAsOpaquePtr();
1168}
1169
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001170static TemplateParameterList *
1171CreateTemplateParameterList (ASTContext *ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001172 const ClangASTContext::TemplateParameterInfos &template_param_infos,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001173 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1174{
1175 const bool parameter_pack = false;
1176 const bool is_typename = false;
1177 const unsigned depth = 0;
1178 const size_t num_template_params = template_param_infos.GetSize();
1179 for (size_t i=0; i<num_template_params; ++i)
1180 {
1181 const char *name = template_param_infos.names[i];
Sean Callanan3d654b32012-09-24 22:25:51 +00001182 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001183 {
1184 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1185 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1186 SourceLocation(),
1187 SourceLocation(),
1188 depth,
1189 i,
1190 &ast->Idents.get(name),
1191 template_param_infos.args[i].getIntegralType(),
1192 parameter_pack,
1193 NULL));
1194
1195 }
1196 else
1197 {
1198 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1199 ast->getTranslationUnitDecl(), // Is this the right decl context?
1200 SourceLocation(),
1201 SourceLocation(),
1202 depth,
1203 i,
1204 &ast->Idents.get(name),
1205 is_typename,
1206 parameter_pack));
1207 }
1208 }
1209
1210 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1211 SourceLocation(),
1212 SourceLocation(),
1213 &template_param_decls.front(),
1214 template_param_decls.size(),
1215 SourceLocation());
1216 return template_param_list;
1217}
1218
1219clang::FunctionTemplateDecl *
1220ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1221 clang::FunctionDecl *func_decl,
1222 const char *name,
1223 const TemplateParameterInfos &template_param_infos)
1224{
1225// /// \brief Create a function template node.
1226 ASTContext *ast = getASTContext();
1227
1228 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1229
1230 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1231 template_param_infos,
1232 template_param_decls);
1233 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1234 decl_ctx,
1235 func_decl->getLocation(),
1236 func_decl->getDeclName(),
1237 template_param_list,
1238 func_decl);
1239
1240 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1241 i < template_param_decl_count;
1242 ++i)
1243 {
1244 // TODO: verify which decl context we should put template_param_decls into..
1245 template_param_decls[i]->setDeclContext (func_decl);
1246 }
1247
1248 return func_tmpl_decl;
1249}
1250
1251void
1252ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1253 clang::FunctionTemplateDecl *func_tmpl_decl,
1254 const TemplateParameterInfos &infos)
1255{
1256 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1257 infos.args.data(),
1258 infos.args.size());
1259
1260 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1261 &template_args,
1262 NULL);
1263}
1264
1265
Greg Claytonf0705c82011-10-22 03:33:13 +00001266ClassTemplateDecl *
1267ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001268 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001269 const char *class_name,
1270 int kind,
1271 const TemplateParameterInfos &template_param_infos)
1272{
1273 ASTContext *ast = getASTContext();
1274
1275 ClassTemplateDecl *class_template_decl = NULL;
1276 if (decl_ctx == NULL)
1277 decl_ctx = ast->getTranslationUnitDecl();
1278
1279 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1280 DeclarationName decl_name (&identifier_info);
1281
1282 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1283 for (clang::DeclContext::lookup_iterator pos = result.first, end = result.second; pos != end; ++pos)
1284 {
1285 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(*pos);
1286 if (class_template_decl)
1287 return class_template_decl;
1288 }
1289
1290 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Claytonf0705c82011-10-22 03:33:13 +00001291
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001292 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1293 template_param_infos,
1294 template_param_decls);
Greg Claytonf0705c82011-10-22 03:33:13 +00001295
1296 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1297 (TagDecl::TagKind)kind,
1298 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1299 SourceLocation(),
1300 SourceLocation(),
1301 &identifier_info);
Greg Claytone04741d2011-12-02 02:09:28 +00001302
1303 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1304 i < template_param_decl_count;
1305 ++i)
1306 {
1307 template_param_decls[i]->setDeclContext (template_cxx_decl);
1308 }
1309
Sean Callananb5c79622011-11-19 01:35:08 +00001310 // With templated classes, we say that a class is templated with
1311 // specializations, but that the bare class has no functions.
1312 template_cxx_decl->startDefinition();
1313 template_cxx_decl->completeDefinition();
1314
Greg Claytonf0705c82011-10-22 03:33:13 +00001315 class_template_decl = ClassTemplateDecl::Create (*ast,
1316 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1317 SourceLocation(),
1318 decl_name,
1319 template_param_list,
1320 template_cxx_decl,
1321 NULL);
1322
1323 if (class_template_decl)
Sean Callanan5e9e1992011-10-26 01:06:27 +00001324 {
Greg Clayton55561e92011-10-26 03:31:36 +00001325 if (access_type != eAccessNone)
1326 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Sean Callanan5b26f272012-02-04 08:49:35 +00001327
1328 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1329 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1330
Greg Claytonf0705c82011-10-22 03:33:13 +00001331 decl_ctx->addDecl (class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001332
1333#ifdef LLDB_CONFIGURATION_DEBUG
1334 VerifyDecl(class_template_decl);
1335#endif
1336 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001337
1338 return class_template_decl;
1339}
1340
1341
1342ClassTemplateSpecializationDecl *
1343ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1344 ClassTemplateDecl *class_template_decl,
1345 int kind,
1346 const TemplateParameterInfos &template_param_infos)
1347{
1348 ASTContext *ast = getASTContext();
1349 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1350 (TagDecl::TagKind)kind,
1351 decl_ctx,
1352 SourceLocation(),
1353 SourceLocation(),
1354 class_template_decl,
1355 &template_param_infos.args.front(),
1356 template_param_infos.args.size(),
1357 NULL);
1358
1359 return class_template_specialization_decl;
1360}
1361
1362lldb::clang_type_t
1363ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1364{
1365 if (class_template_specialization_decl)
1366 {
1367 ASTContext *ast = getASTContext();
1368 if (ast)
1369 return ast->getTagDeclType(class_template_specialization_decl).getAsOpaquePtr();
1370 }
1371 return NULL;
1372}
1373
Greg Clayton6beaaa62011-01-17 03:46:26 +00001374bool
1375ClangASTContext::SetHasExternalStorage (clang_type_t clang_type, bool has_extern)
1376{
1377 if (clang_type == NULL)
1378 return false;
1379
1380 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
1381
1382 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
1383 switch (type_class)
1384 {
1385 case clang::Type::Record:
1386 {
1387 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
1388 if (cxx_record_decl)
1389 {
1390 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001391 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001392 return true;
1393 }
1394 }
1395 break;
1396
1397 case clang::Type::Enum:
1398 {
1399 EnumDecl *enum_decl = cast<EnumType>(qual_type)->getDecl();
1400 if (enum_decl)
1401 {
1402 enum_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001403 enum_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001404 return true;
1405 }
1406 }
1407 break;
1408
1409 case clang::Type::ObjCObject:
1410 case clang::Type::ObjCInterface:
1411 {
Sean Callanan78e37602011-01-27 04:42:51 +00001412 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001413 assert (objc_class_type);
1414 if (objc_class_type)
1415 {
1416 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1417
1418 if (class_interface_decl)
1419 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001420 class_interface_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001421 class_interface_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001422 return true;
1423 }
1424 }
1425 }
1426 break;
1427
1428 case clang::Type::Typedef:
1429 return ClangASTContext::SetHasExternalStorage (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
Sean Callanan912855f2011-08-11 23:56:13 +00001430
1431 case clang::Type::Elaborated:
1432 return ClangASTContext::SetHasExternalStorage (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001433
1434 default:
1435 break;
1436 }
1437 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001438}
1439
Greg Claytona3c444a2010-10-01 23:13:49 +00001440static bool
1441IsOperator (const char *name, OverloadedOperatorKind &op_kind)
1442{
1443 if (name == NULL || name[0] == '\0')
1444 return false;
1445
Sean Callanana43f20d2010-12-10 19:51:54 +00001446#define OPERATOR_PREFIX "operator"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001447#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
Sean Callananbfeff8c2010-12-10 02:15:55 +00001448
1449 const char *post_op_name = NULL;
1450
Sean Callanana43f20d2010-12-10 19:51:54 +00001451 bool no_space = true;
Sean Callananbfeff8c2010-12-10 02:15:55 +00001452
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001453 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
Greg Claytona3c444a2010-10-01 23:13:49 +00001454 return false;
1455
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001456 post_op_name = name + OPERATOR_PREFIX_LENGTH;
1457
Sean Callanana43f20d2010-12-10 19:51:54 +00001458 if (post_op_name[0] == ' ')
1459 {
1460 post_op_name++;
1461 no_space = false;
1462 }
1463
1464#undef OPERATOR_PREFIX
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001465#undef OPERATOR_PREFIX_LENGTH
Sean Callanana43f20d2010-12-10 19:51:54 +00001466
Greg Claytona3c444a2010-10-01 23:13:49 +00001467 // This is an operator, set the overloaded operator kind to invalid
1468 // in case this is a conversion operator...
1469 op_kind = NUM_OVERLOADED_OPERATORS;
1470
1471 switch (post_op_name[0])
1472 {
Sean Callananbfeff8c2010-12-10 02:15:55 +00001473 default:
1474 if (no_space)
1475 return false;
1476 break;
Greg Claytona3c444a2010-10-01 23:13:49 +00001477 case 'n':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001478 if (no_space)
1479 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001480 if (strcmp (post_op_name, "new") == 0)
1481 op_kind = OO_New;
1482 else if (strcmp (post_op_name, "new[]") == 0)
1483 op_kind = OO_Array_New;
1484 break;
1485
1486 case 'd':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001487 if (no_space)
1488 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001489 if (strcmp (post_op_name, "delete") == 0)
1490 op_kind = OO_Delete;
1491 else if (strcmp (post_op_name, "delete[]") == 0)
1492 op_kind = OO_Array_Delete;
1493 break;
1494
1495 case '+':
1496 if (post_op_name[1] == '\0')
1497 op_kind = OO_Plus;
1498 else if (post_op_name[2] == '\0')
1499 {
1500 if (post_op_name[1] == '=')
1501 op_kind = OO_PlusEqual;
1502 else if (post_op_name[1] == '+')
1503 op_kind = OO_PlusPlus;
1504 }
1505 break;
1506
1507 case '-':
1508 if (post_op_name[1] == '\0')
1509 op_kind = OO_Minus;
1510 else if (post_op_name[2] == '\0')
1511 {
1512 switch (post_op_name[1])
1513 {
1514 case '=': op_kind = OO_MinusEqual; break;
1515 case '-': op_kind = OO_MinusMinus; break;
1516 case '>': op_kind = OO_Arrow; break;
1517 }
1518 }
1519 else if (post_op_name[3] == '\0')
1520 {
1521 if (post_op_name[2] == '*')
1522 op_kind = OO_ArrowStar; break;
1523 }
1524 break;
1525
1526 case '*':
1527 if (post_op_name[1] == '\0')
1528 op_kind = OO_Star;
1529 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1530 op_kind = OO_StarEqual;
1531 break;
1532
1533 case '/':
1534 if (post_op_name[1] == '\0')
1535 op_kind = OO_Slash;
1536 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1537 op_kind = OO_SlashEqual;
1538 break;
1539
1540 case '%':
1541 if (post_op_name[1] == '\0')
1542 op_kind = OO_Percent;
1543 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1544 op_kind = OO_PercentEqual;
1545 break;
1546
1547
1548 case '^':
1549 if (post_op_name[1] == '\0')
1550 op_kind = OO_Caret;
1551 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1552 op_kind = OO_CaretEqual;
1553 break;
1554
1555 case '&':
1556 if (post_op_name[1] == '\0')
1557 op_kind = OO_Amp;
1558 else if (post_op_name[2] == '\0')
1559 {
1560 switch (post_op_name[1])
1561 {
1562 case '=': op_kind = OO_AmpEqual; break;
1563 case '&': op_kind = OO_AmpAmp; break;
1564 }
1565 }
1566 break;
1567
1568 case '|':
1569 if (post_op_name[1] == '\0')
1570 op_kind = OO_Pipe;
1571 else if (post_op_name[2] == '\0')
1572 {
1573 switch (post_op_name[1])
1574 {
1575 case '=': op_kind = OO_PipeEqual; break;
1576 case '|': op_kind = OO_PipePipe; break;
1577 }
1578 }
1579 break;
1580
1581 case '~':
1582 if (post_op_name[1] == '\0')
1583 op_kind = OO_Tilde;
1584 break;
1585
1586 case '!':
1587 if (post_op_name[1] == '\0')
1588 op_kind = OO_Exclaim;
1589 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1590 op_kind = OO_ExclaimEqual;
1591 break;
1592
1593 case '=':
1594 if (post_op_name[1] == '\0')
1595 op_kind = OO_Equal;
1596 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1597 op_kind = OO_EqualEqual;
1598 break;
1599
1600 case '<':
1601 if (post_op_name[1] == '\0')
1602 op_kind = OO_Less;
1603 else if (post_op_name[2] == '\0')
1604 {
1605 switch (post_op_name[1])
1606 {
1607 case '<': op_kind = OO_LessLess; break;
1608 case '=': op_kind = OO_LessEqual; break;
1609 }
1610 }
1611 else if (post_op_name[3] == '\0')
1612 {
1613 if (post_op_name[2] == '=')
1614 op_kind = OO_LessLessEqual;
1615 }
1616 break;
1617
1618 case '>':
1619 if (post_op_name[1] == '\0')
1620 op_kind = OO_Greater;
1621 else if (post_op_name[2] == '\0')
1622 {
1623 switch (post_op_name[1])
1624 {
1625 case '>': op_kind = OO_GreaterGreater; break;
1626 case '=': op_kind = OO_GreaterEqual; break;
1627 }
1628 }
1629 else if (post_op_name[1] == '>' &&
1630 post_op_name[2] == '=' &&
1631 post_op_name[3] == '\0')
1632 {
1633 op_kind = OO_GreaterGreaterEqual;
1634 }
1635 break;
1636
1637 case ',':
1638 if (post_op_name[1] == '\0')
1639 op_kind = OO_Comma;
1640 break;
1641
1642 case '(':
1643 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
1644 op_kind = OO_Call;
1645 break;
1646
1647 case '[':
1648 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
1649 op_kind = OO_Subscript;
1650 break;
1651 }
1652
1653 return true;
1654}
Greg Clayton6beaaa62011-01-17 03:46:26 +00001655
Greg Clayton090d0982011-06-19 03:43:27 +00001656static inline bool
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001657check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
Greg Clayton090d0982011-06-19 03:43:27 +00001658{
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001659 // Special-case call since it can take any number of operands
1660 if(op_kind == OO_Call)
1661 return true;
1662
Greg Clayton090d0982011-06-19 03:43:27 +00001663 // The parameter count doens't include "this"
1664 if (num_params == 0)
1665 return unary;
1666 if (num_params == 1)
1667 return binary;
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001668 else
Greg Clayton090d0982011-06-19 03:43:27 +00001669 return false;
1670}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001671
Greg Clayton090d0982011-06-19 03:43:27 +00001672bool
1673ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1674{
Sean Callanan5b26f272012-02-04 08:49:35 +00001675 switch (op_kind)
1676 {
1677 default:
1678 break;
1679 // C++ standard allows any number of arguments to new/delete
1680 case OO_New:
1681 case OO_Array_New:
1682 case OO_Delete:
1683 case OO_Array_Delete:
1684 return true;
1685 }
1686
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001687#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 +00001688 switch (op_kind)
1689 {
1690#include "clang/Basic/OperatorKinds.def"
1691 default: break;
1692 }
1693 return false;
1694}
1695
Greg Claytona51ed9b2010-09-23 01:09:21 +00001696CXXMethodDecl *
Sean Callanan61da09b2010-09-17 02:58:26 +00001697ClangASTContext::AddMethodToCXXRecordType
1698(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001699 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001700 clang_type_t record_opaque_type,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001701 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001702 clang_type_t method_opaque_type,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001703 lldb::AccessType access,
Greg Clayton0fffff52010-09-24 05:15:53 +00001704 bool is_virtual,
1705 bool is_static,
Greg Claytonf51de672010-10-01 02:31:07 +00001706 bool is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00001707 bool is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00001708 bool is_attr_used,
1709 bool is_artificial
Greg Claytona51ed9b2010-09-23 01:09:21 +00001710)
Sean Callanan61da09b2010-09-17 02:58:26 +00001711{
Sean Callananfc55f5d2010-09-21 00:44:12 +00001712 if (!record_opaque_type || !method_opaque_type || !name)
Johnny Chend440bcc2010-09-28 16:10:54 +00001713 return NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001714
Greg Clayton6beaaa62011-01-17 03:46:26 +00001715 assert(ast);
Sean Callanan61da09b2010-09-17 02:58:26 +00001716
Greg Clayton6beaaa62011-01-17 03:46:26 +00001717 IdentifierTable *identifier_table = &ast->Idents;
Sean Callanan61da09b2010-09-17 02:58:26 +00001718
1719 assert(identifier_table);
1720
Sean Callananfc55f5d2010-09-21 00:44:12 +00001721 QualType record_qual_type(QualType::getFromOpaquePtr(record_opaque_type));
Greg Clayton0fffff52010-09-24 05:15:53 +00001722
Greg Clayton6beaaa62011-01-17 03:46:26 +00001723 CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
Sean Callanan61da09b2010-09-17 02:58:26 +00001724
Greg Clayton0fffff52010-09-24 05:15:53 +00001725 if (cxx_record_decl == NULL)
Greg Claytona51ed9b2010-09-23 01:09:21 +00001726 return NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001727
Greg Clayton0fffff52010-09-24 05:15:53 +00001728 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001729
Greg Claytonf51de672010-10-01 02:31:07 +00001730 CXXMethodDecl *cxx_method_decl = NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001731
Greg Claytonf51de672010-10-01 02:31:07 +00001732 DeclarationName decl_name (&identifier_table->get(name));
Greg Clayton878eaf12010-10-01 03:45:20 +00001733
Sean Callanan78e37602011-01-27 04:42:51 +00001734 const clang::FunctionType *function_Type = dyn_cast<FunctionType>(method_qual_type.getTypePtr());
Greg Clayton878eaf12010-10-01 03:45:20 +00001735
Greg Clayton90a2acd2010-10-02 01:40:05 +00001736 if (function_Type == NULL)
Greg Clayton878eaf12010-10-01 03:45:20 +00001737 return NULL;
1738
Sean Callanan78e37602011-01-27 04:42:51 +00001739 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(function_Type));
Greg Clayton878eaf12010-10-01 03:45:20 +00001740
1741 if (!method_function_prototype)
1742 return NULL;
1743
1744 unsigned int num_params = method_function_prototype->getNumArgs();
1745
Sean Callanandbb58392011-11-02 01:38:59 +00001746 CXXDestructorDecl *cxx_dtor_decl(NULL);
1747 CXXConstructorDecl *cxx_ctor_decl(NULL);
1748
Greg Clayton878eaf12010-10-01 03:45:20 +00001749 if (name[0] == '~')
Greg Claytonf51de672010-10-01 02:31:07 +00001750 {
Sean Callanandbb58392011-11-02 01:38:59 +00001751 cxx_dtor_decl = CXXDestructorDecl::Create (*ast,
1752 cxx_record_decl,
1753 SourceLocation(),
1754 DeclarationNameInfo (ast->DeclarationNames.getCXXDestructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1755 method_qual_type,
1756 NULL,
1757 is_inline,
1758 is_artificial);
1759 cxx_method_decl = cxx_dtor_decl;
Greg Clayton878eaf12010-10-01 03:45:20 +00001760 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001761 else if (decl_name == cxx_record_decl->getDeclName())
Greg Clayton878eaf12010-10-01 03:45:20 +00001762 {
Sean Callanandbb58392011-11-02 01:38:59 +00001763 cxx_ctor_decl = CXXConstructorDecl::Create (*ast,
1764 cxx_record_decl,
1765 SourceLocation(),
1766 DeclarationNameInfo (ast->DeclarationNames.getCXXConstructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1767 method_qual_type,
1768 NULL, // TypeSourceInfo *
1769 is_explicit,
1770 is_inline,
1771 is_artificial,
1772 false /*is_constexpr*/);
1773 cxx_method_decl = cxx_ctor_decl;
Greg Claytonf51de672010-10-01 02:31:07 +00001774 }
1775 else
Greg Clayton878eaf12010-10-01 03:45:20 +00001776 {
Greg Claytona3c444a2010-10-01 23:13:49 +00001777
1778 OverloadedOperatorKind op_kind = NUM_OVERLOADED_OPERATORS;
1779 if (IsOperator (name, op_kind))
Greg Clayton878eaf12010-10-01 03:45:20 +00001780 {
Greg Claytona3c444a2010-10-01 23:13:49 +00001781 if (op_kind != NUM_OVERLOADED_OPERATORS)
1782 {
Greg Clayton090d0982011-06-19 03:43:27 +00001783 // Check the number of operator parameters. Sometimes we have
1784 // seen bad DWARF that doesn't correctly describe operators and
1785 // if we try to create a methed and add it to the class, clang
1786 // will assert and crash, so we need to make sure things are
1787 // acceptable.
1788 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
1789 return NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001790 cxx_method_decl = CXXMethodDecl::Create (*ast,
Greg Clayton878eaf12010-10-01 03:45:20 +00001791 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001792 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001793 DeclarationNameInfo (ast->DeclarationNames.getCXXOperatorName (op_kind), SourceLocation()),
Greg Clayton878eaf12010-10-01 03:45:20 +00001794 method_qual_type,
1795 NULL, // TypeSourceInfo *
Greg Claytona3c444a2010-10-01 23:13:49 +00001796 is_static,
1797 SC_None,
Sean Callananfb0b7582011-03-15 00:17:19 +00001798 is_inline,
Sean Callanan880e6802011-10-07 23:18:13 +00001799 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001800 SourceLocation());
Greg Claytona3c444a2010-10-01 23:13:49 +00001801 }
1802 else if (num_params == 0)
1803 {
1804 // Conversion operators don't take params...
Greg Clayton6beaaa62011-01-17 03:46:26 +00001805 cxx_method_decl = CXXConversionDecl::Create (*ast,
Greg Claytona3c444a2010-10-01 23:13:49 +00001806 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001807 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001808 DeclarationNameInfo (ast->DeclarationNames.getCXXConversionFunctionName (ast->getCanonicalType (function_Type->getResultType())), SourceLocation()),
Greg Claytona3c444a2010-10-01 23:13:49 +00001809 method_qual_type,
1810 NULL, // TypeSourceInfo *
1811 is_inline,
Sean Callananfb0b7582011-03-15 00:17:19 +00001812 is_explicit,
Sean Callanan880e6802011-10-07 23:18:13 +00001813 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001814 SourceLocation());
Greg Claytona3c444a2010-10-01 23:13:49 +00001815 }
Greg Clayton878eaf12010-10-01 03:45:20 +00001816 }
Greg Claytona3c444a2010-10-01 23:13:49 +00001817
1818 if (cxx_method_decl == NULL)
Greg Clayton878eaf12010-10-01 03:45:20 +00001819 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001820 cxx_method_decl = CXXMethodDecl::Create (*ast,
Greg Clayton878eaf12010-10-01 03:45:20 +00001821 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001822 SourceLocation(),
Greg Claytona3c444a2010-10-01 23:13:49 +00001823 DeclarationNameInfo (decl_name, SourceLocation()),
Greg Clayton878eaf12010-10-01 03:45:20 +00001824 method_qual_type,
1825 NULL, // TypeSourceInfo *
1826 is_static,
1827 SC_None,
Sean Callananfb0b7582011-03-15 00:17:19 +00001828 is_inline,
Sean Callanan880e6802011-10-07 23:18:13 +00001829 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001830 SourceLocation());
Greg Clayton878eaf12010-10-01 03:45:20 +00001831 }
Greg Claytonf51de672010-10-01 02:31:07 +00001832 }
Greg Claytona3c444a2010-10-01 23:13:49 +00001833
Greg Clayton1be10fc2010-09-29 01:12:09 +00001834 AccessSpecifier access_specifier = ConvertAccessTypeToAccessSpecifier (access);
Greg Clayton0fffff52010-09-24 05:15:53 +00001835
1836 cxx_method_decl->setAccess (access_specifier);
1837 cxx_method_decl->setVirtualAsWritten (is_virtual);
Sean Callanane2ef6e32010-09-23 03:01:22 +00001838
Sean Callananc1b732d2011-11-01 18:07:13 +00001839 if (is_attr_used)
1840 cxx_method_decl->addAttr(::new (*ast) UsedAttr(SourceRange(), *ast));
1841
Sean Callananfc55f5d2010-09-21 00:44:12 +00001842 // Populate the method decl with parameter decls
Sean Callananfc55f5d2010-09-21 00:44:12 +00001843
Charles Davis8c444c42011-05-19 23:33:46 +00001844 llvm::SmallVector<ParmVarDecl *, 12> params;
Sean Callananfc55f5d2010-09-21 00:44:12 +00001845
1846 for (int param_index = 0;
1847 param_index < num_params;
1848 ++param_index)
1849 {
Charles Davis8c444c42011-05-19 23:33:46 +00001850 params.push_back (ParmVarDecl::Create (*ast,
1851 cxx_method_decl,
1852 SourceLocation(),
1853 SourceLocation(),
1854 NULL, // anonymous
1855 method_function_prototype->getArgType(param_index),
1856 NULL,
1857 SC_None,
1858 SC_None,
1859 NULL));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001860 }
1861
Sean Callanan880e6802011-10-07 23:18:13 +00001862 cxx_method_decl->setParams (ArrayRef<ParmVarDecl*>(params));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001863
Greg Clayton0fffff52010-09-24 05:15:53 +00001864 cxx_record_decl->addDecl (cxx_method_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001865
Greg Clayton8b867b42011-11-02 02:06:20 +00001866 // Sometimes the debug info will mention a constructor (default/copy/move),
1867 // destructor, or assignment operator (copy/move) but there won't be any
1868 // version of this in the code. So we check if the function was artificially
1869 // generated and if it is trivial and this lets the compiler/backend know
1870 // that it can inline the IR for these when it needs to and we can avoid a
1871 // "missing function" error when running expressions.
1872
Sean Callanandbb58392011-11-02 01:38:59 +00001873 if (is_artificial)
1874 {
Greg Clayton8b867b42011-11-02 02:06:20 +00001875 if (cxx_ctor_decl &&
1876 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
1877 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
1878 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
Sean Callanandbb58392011-11-02 01:38:59 +00001879 {
1880 cxx_ctor_decl->setDefaulted();
1881 cxx_ctor_decl->setTrivial(true);
1882 }
Greg Clayton8b867b42011-11-02 02:06:20 +00001883 else if (cxx_dtor_decl)
Sean Callanandbb58392011-11-02 01:38:59 +00001884 {
Greg Clayton8b867b42011-11-02 02:06:20 +00001885 if (cxx_record_decl->hasTrivialDestructor())
1886 {
1887 cxx_dtor_decl->setDefaulted();
1888 cxx_dtor_decl->setTrivial(true);
1889 }
1890 }
1891 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
1892 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
1893 {
1894 cxx_method_decl->setDefaulted();
1895 cxx_method_decl->setTrivial(true);
Sean Callanandbb58392011-11-02 01:38:59 +00001896 }
1897 }
1898
Sean Callanan5e9e1992011-10-26 01:06:27 +00001899#ifdef LLDB_CONFIGURATION_DEBUG
1900 VerifyDecl(cxx_method_decl);
1901#endif
Greg Claytonc432c192011-01-20 04:18:48 +00001902
1903// printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
1904// printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
1905// printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
1906// printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
1907// printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
1908// printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
1909// printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
1910// printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
1911// printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
Greg Claytona51ed9b2010-09-23 01:09:21 +00001912 return cxx_method_decl;
Sean Callanan61da09b2010-09-17 02:58:26 +00001913}
1914
Jim Inghame3ae82a2011-11-12 01:36:43 +00001915clang::FieldDecl *
Greg Clayton8cf05932010-07-22 18:30:50 +00001916ClangASTContext::AddFieldToRecordType
1917(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001918 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001919 clang_type_t record_clang_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00001920 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001921 clang_type_t field_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00001922 AccessType access,
1923 uint32_t bitfield_bit_size
1924)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001925{
1926 if (record_clang_type == NULL || field_type == NULL)
Jim Inghame3ae82a2011-11-12 01:36:43 +00001927 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001928
Jim Inghame3ae82a2011-11-12 01:36:43 +00001929 FieldDecl *field = NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001930 IdentifierTable *identifier_table = &ast->Idents;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001931
Greg Clayton6beaaa62011-01-17 03:46:26 +00001932 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001933 assert (identifier_table != NULL);
1934
1935 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
1936
Sean Callanan78e37602011-01-27 04:42:51 +00001937 const clang::Type *clang_type = record_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001938 if (clang_type)
1939 {
1940 const RecordType *record_type = dyn_cast<RecordType>(clang_type);
1941
1942 if (record_type)
1943 {
1944 RecordDecl *record_decl = record_type->getDecl();
1945
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001946 clang::Expr *bit_width = NULL;
1947 if (bitfield_bit_size != 0)
1948 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001949 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
1950 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001951 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001952 field = FieldDecl::Create (*ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001953 record_decl,
1954 SourceLocation(),
1955 SourceLocation(),
1956 name ? &identifier_table->get(name) : NULL, // Identifier
1957 QualType::getFromOpaquePtr(field_type), // Field type
1958 NULL, // TInfo *
1959 bit_width, // BitWidth
1960 false, // Mutable
1961 ICIS_NoInit); // HasInit
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001962
Sean Callanan5ed3ac12012-07-13 20:01:02 +00001963 if (!name) {
1964 // Determine whether this field corresponds to an anonymous
1965 // struct or union.
1966 if (const TagType *TagT = field->getType()->getAs<TagType>()) {
1967 if (RecordDecl *Rec = dyn_cast<RecordDecl>(TagT->getDecl()))
1968 if (!Rec->getDeclName()) {
1969 Rec->setAnonymousStructOrUnion(true);
1970 field->setImplicit();
1971
1972 }
1973 }
1974 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001975
Greg Clayton8cf05932010-07-22 18:30:50 +00001976 field->setAccess (ConvertAccessTypeToAccessSpecifier (access));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001977
1978 if (field)
1979 {
1980 record_decl->addDecl(field);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001981
1982#ifdef LLDB_CONFIGURATION_DEBUG
1983 VerifyDecl(field);
1984#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001985 }
1986 }
Greg Clayton9e409562010-07-28 02:04:09 +00001987 else
1988 {
Sean Callanan78e37602011-01-27 04:42:51 +00001989 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001990 if (objc_class_type)
1991 {
Greg Clayton0fffff52010-09-24 05:15:53 +00001992 bool is_synthesized = false;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001993 field = ClangASTContext::AddObjCClassIVar (ast,
Sean Callanan6e6a7c72010-09-16 20:01:08 +00001994 record_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001995 name,
1996 field_type,
1997 access,
1998 bitfield_bit_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00001999 is_synthesized);
Greg Clayton9e409562010-07-28 02:04:09 +00002000 }
2001 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002002 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002003 return field;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002004}
2005
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002006static clang::AccessSpecifier UnifyAccessSpecifiers (clang::AccessSpecifier lhs,
2007 clang::AccessSpecifier rhs)
2008{
2009 clang::AccessSpecifier ret = lhs;
2010
2011 // Make the access equal to the stricter of the field and the nested field's access
2012 switch (ret)
2013 {
2014 case clang::AS_none:
2015 break;
2016 case clang::AS_private:
2017 break;
2018 case clang::AS_protected:
2019 if (rhs == AS_private)
2020 ret = AS_private;
2021 break;
2022 case clang::AS_public:
2023 ret = rhs;
2024 break;
2025 }
2026
2027 return ret;
2028}
2029
2030void
2031ClangASTContext::BuildIndirectFields (clang::ASTContext *ast,
2032 lldb::clang_type_t record_clang_type)
2033{
2034 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
2035
2036 const RecordType *record_type = record_qual_type->getAs<RecordType>();
2037
2038 if (!record_type)
2039 return;
2040
2041 RecordDecl *record_decl = record_type->getDecl();
2042
2043 if (!record_decl)
2044 return;
2045
2046 typedef llvm::SmallVector <IndirectFieldDecl *, 1> IndirectFieldVector;
2047
2048 IndirectFieldVector indirect_fields;
2049
2050 for (RecordDecl::field_iterator fi = record_decl->field_begin(), fe = record_decl->field_end();
2051 fi != fe;
2052 ++fi)
2053 {
2054 if (fi->isAnonymousStructOrUnion())
2055 {
2056 QualType field_qual_type = fi->getType();
2057
2058 const RecordType *field_record_type = field_qual_type->getAs<RecordType>();
2059
2060 if (!field_record_type)
2061 continue;
2062
2063 RecordDecl *field_record_decl = field_record_type->getDecl();
2064
2065 if (!field_record_decl)
2066 continue;
2067
2068 for (RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
2069 di != de;
2070 ++di)
2071 {
2072 if (FieldDecl *nested_field_decl = dyn_cast<FieldDecl>(*di))
2073 {
2074 NamedDecl **chain = new (*ast) NamedDecl*[2];
2075 chain[0] = *fi;
2076 chain[1] = nested_field_decl;
2077 IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*ast,
2078 record_decl,
2079 SourceLocation(),
2080 nested_field_decl->getIdentifier(),
2081 nested_field_decl->getType(),
2082 chain,
2083 2);
2084
2085 indirect_field->setAccess(UnifyAccessSpecifiers(fi->getAccess(),
2086 nested_field_decl->getAccess()));
2087
2088 indirect_fields.push_back(indirect_field);
2089 }
2090 else if (IndirectFieldDecl *nested_indirect_field_decl = dyn_cast<IndirectFieldDecl>(*di))
2091 {
2092 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
2093 NamedDecl **chain = new (*ast) NamedDecl*[nested_chain_size + 1];
2094 chain[0] = *fi;
2095
2096 int chain_index = 1;
2097 for (IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
2098 nce = nested_indirect_field_decl->chain_end();
2099 nci < nce;
2100 ++nci)
2101 {
2102 chain[chain_index] = *nci;
2103 chain_index++;
2104 }
2105
2106 IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*ast,
2107 record_decl,
2108 SourceLocation(),
2109 nested_indirect_field_decl->getIdentifier(),
2110 nested_indirect_field_decl->getType(),
2111 chain,
2112 nested_chain_size + 1);
2113
2114 indirect_field->setAccess(UnifyAccessSpecifiers(fi->getAccess(),
2115 nested_indirect_field_decl->getAccess()));
2116
2117 indirect_fields.push_back(indirect_field);
2118 }
2119 }
2120 }
2121 }
2122
2123 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
2124 ifi < ife;
2125 ++ifi)
2126 {
2127 record_decl->addDecl(*ifi);
2128 }
2129}
2130
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002131bool
2132ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
2133{
2134 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
2135}
2136
2137bool
2138ClangASTContext::FieldIsBitfield
2139(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002140 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002141 FieldDecl* field,
2142 uint32_t& bitfield_bit_size
2143)
2144{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002145 if (ast == NULL || field == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002146 return false;
2147
2148 if (field->isBitField())
2149 {
2150 Expr* bit_width_expr = field->getBitWidth();
2151 if (bit_width_expr)
2152 {
2153 llvm::APSInt bit_width_apsint;
Greg Clayton6beaaa62011-01-17 03:46:26 +00002154 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002155 {
2156 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
2157 return true;
2158 }
2159 }
2160 }
2161 return false;
2162}
2163
2164bool
2165ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
2166{
2167 if (record_decl == NULL)
2168 return false;
2169
2170 if (!record_decl->field_empty())
2171 return true;
2172
2173 // No fields, lets check this is a CXX record and check the base classes
2174 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2175 if (cxx_record_decl)
2176 {
2177 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2178 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2179 base_class != base_class_end;
2180 ++base_class)
2181 {
2182 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2183 if (RecordHasFields(base_class_decl))
2184 return true;
2185 }
2186 }
2187 return false;
2188}
2189
2190void
Greg Clayton6beaaa62011-01-17 03:46:26 +00002191ClangASTContext::SetDefaultAccessForRecordFields (clang_type_t clang_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002192{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002193 if (clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002194 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002195 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
2196
Sean Callanan78e37602011-01-27 04:42:51 +00002197 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +00002198 if (record_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002199 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002200 RecordDecl *record_decl = record_type->getDecl();
2201 if (record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002202 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002203 uint32_t field_idx;
2204 RecordDecl::field_iterator field, field_end;
2205 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2206 field != field_end;
2207 ++field, ++field_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002208 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002209 // If no accessibility was assigned, assign the correct one
2210 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2211 field->setAccess ((AccessSpecifier)default_accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002212 }
2213 }
2214 }
2215 }
2216}
2217
2218#pragma mark C++ Base Classes
2219
2220CXXBaseSpecifier *
Greg Clayton1be10fc2010-09-29 01:12:09 +00002221ClangASTContext::CreateBaseClassSpecifier (clang_type_t base_class_type, AccessType access, bool is_virtual, bool base_of_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002222{
2223 if (base_class_type)
Greg Claytone6371122010-07-30 20:30:44 +00002224 return new CXXBaseSpecifier (SourceRange(),
2225 is_virtual,
2226 base_of_class,
2227 ConvertAccessTypeToAccessSpecifier (access),
Sean Callanan2c777c42011-01-18 23:32:05 +00002228 getASTContext()->CreateTypeSourceInfo (QualType::getFromOpaquePtr(base_class_type)),
2229 SourceLocation());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002230 return NULL;
2231}
2232
Greg Clayton0b42ac32010-07-02 01:29:13 +00002233void
2234ClangASTContext::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes)
2235{
2236 for (unsigned i=0; i<num_base_classes; ++i)
2237 {
2238 delete base_classes[i];
2239 base_classes[i] = NULL;
2240 }
2241}
2242
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002243bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002244ClangASTContext::SetBaseClassesForClassType (clang_type_t class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002245{
2246 if (class_clang_type)
2247 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002248 CXXRecordDecl *cxx_record_decl = QualType::getFromOpaquePtr(class_clang_type)->getAsCXXRecordDecl();
2249 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002250 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002251 cxx_record_decl->setBases(base_classes, num_base_classes);
2252 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002253 }
2254 }
2255 return false;
2256}
Greg Clayton8cf05932010-07-22 18:30:50 +00002257#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002258
Greg Clayton1be10fc2010-09-29 01:12:09 +00002259clang_type_t
Sean Callananad880762012-04-18 01:06:17 +00002260ClangASTContext::CreateObjCClass
Greg Clayton8cf05932010-07-22 18:30:50 +00002261(
2262 const char *name,
2263 DeclContext *decl_ctx,
2264 bool isForwardDecl,
Sean Callananad880762012-04-18 01:06:17 +00002265 bool isInternal,
Jim Ingham379397632012-10-27 02:54:13 +00002266 ClangASTMetadata *metadata
Greg Clayton8cf05932010-07-22 18:30:50 +00002267)
2268{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002269 ASTContext *ast = getASTContext();
2270 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00002271 assert (name && name[0]);
2272 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00002273 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00002274
2275 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
2276 // we will need to update this code. I was told to currently always use
2277 // the CXXRecordDecl class since we often don't know from debug information
2278 // if something is struct or a class, so we default to always use the more
2279 // complete definition just in case.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002280 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00002281 decl_ctx,
2282 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00002283 &ast->Idents.get(name),
Sean Callanan5b26f272012-02-04 08:49:35 +00002284 NULL,
Greg Clayton8cf05932010-07-22 18:30:50 +00002285 SourceLocation(),
Sean Callanan5b26f272012-02-04 08:49:35 +00002286 /*isForwardDecl,*/
Greg Clayton8cf05932010-07-22 18:30:50 +00002287 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00002288
Jim Ingham379397632012-10-27 02:54:13 +00002289 if (decl && metadata)
2290 SetMetadata(ast, (uintptr_t)decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00002291
Greg Clayton6beaaa62011-01-17 03:46:26 +00002292 return ast->getObjCInterfaceType(decl).getAsOpaquePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002293}
2294
2295bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002296ClangASTContext::SetObjCSuperClass (clang_type_t class_opaque_type, clang_type_t super_opaque_type)
Greg Clayton8cf05932010-07-22 18:30:50 +00002297{
2298 if (class_opaque_type && super_opaque_type)
2299 {
2300 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2301 QualType super_qual_type(QualType::getFromOpaquePtr(super_opaque_type));
Sean Callanan78e37602011-01-27 04:42:51 +00002302 const clang::Type *class_type = class_qual_type.getTypePtr();
2303 const clang::Type *super_type = super_qual_type.getTypePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002304 if (class_type && super_type)
2305 {
Sean Callanan78e37602011-01-27 04:42:51 +00002306 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
2307 const ObjCObjectType *objc_super_type = dyn_cast<ObjCObjectType>(super_type);
Greg Clayton8cf05932010-07-22 18:30:50 +00002308 if (objc_class_type && objc_super_type)
2309 {
2310 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2311 ObjCInterfaceDecl *super_interface_decl = objc_super_type->getInterface();
2312 if (class_interface_decl && super_interface_decl)
2313 {
2314 class_interface_decl->setSuperClass(super_interface_decl);
2315 return true;
2316 }
2317 }
2318 }
2319 }
2320 return false;
2321}
2322
2323
Jim Inghame3ae82a2011-11-12 01:36:43 +00002324FieldDecl *
Greg Clayton8cf05932010-07-22 18:30:50 +00002325ClangASTContext::AddObjCClassIVar
2326(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002327 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002328 clang_type_t class_opaque_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00002329 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002330 clang_type_t ivar_opaque_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00002331 AccessType access,
2332 uint32_t bitfield_bit_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00002333 bool is_synthesized
Greg Clayton8cf05932010-07-22 18:30:50 +00002334)
2335{
2336 if (class_opaque_type == NULL || ivar_opaque_type == NULL)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002337 return NULL;
Greg Clayton8cf05932010-07-22 18:30:50 +00002338
Jim Inghame3ae82a2011-11-12 01:36:43 +00002339 ObjCIvarDecl *field = NULL;
2340
Greg Clayton6beaaa62011-01-17 03:46:26 +00002341 IdentifierTable *identifier_table = &ast->Idents;
Greg Clayton8cf05932010-07-22 18:30:50 +00002342
Greg Clayton6beaaa62011-01-17 03:46:26 +00002343 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00002344 assert (identifier_table != NULL);
2345
2346 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2347
Sean Callanan78e37602011-01-27 04:42:51 +00002348 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002349 if (class_type)
2350 {
Sean Callanan78e37602011-01-27 04:42:51 +00002351 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton8cf05932010-07-22 18:30:50 +00002352
2353 if (objc_class_type)
2354 {
2355 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2356
2357 if (class_interface_decl)
2358 {
2359 clang::Expr *bit_width = NULL;
2360 if (bitfield_bit_size != 0)
2361 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002362 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
2363 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
Greg Clayton8cf05932010-07-22 18:30:50 +00002364 }
2365
Jim Inghame3ae82a2011-11-12 01:36:43 +00002366 field = ObjCIvarDecl::Create (*ast,
2367 class_interface_decl,
2368 SourceLocation(),
2369 SourceLocation(),
Greg Clayton88bc7f32012-11-06 00:20:41 +00002370 name ? &identifier_table->get(name) : NULL, // Identifier
Jim Inghame3ae82a2011-11-12 01:36:43 +00002371 QualType::getFromOpaquePtr(ivar_opaque_type), // Field type
2372 NULL, // TypeSourceInfo *
2373 ConvertAccessTypeToObjCIvarAccessControl (access),
2374 bit_width,
2375 is_synthesized);
Greg Clayton9e409562010-07-28 02:04:09 +00002376
2377 if (field)
2378 {
2379 class_interface_decl->addDecl(field);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002380
2381#ifdef LLDB_CONFIGURATION_DEBUG
2382 VerifyDecl(field);
2383#endif
2384
Jim Inghame3ae82a2011-11-12 01:36:43 +00002385 return field;
Greg Clayton9e409562010-07-28 02:04:09 +00002386 }
Greg Clayton8cf05932010-07-22 18:30:50 +00002387 }
2388 }
2389 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002390 return NULL;
2391}
2392
2393bool
2394ClangASTContext::AddObjCClassProperty
2395(
2396 ASTContext *ast,
2397 clang_type_t class_opaque_type,
2398 const char *property_name,
2399 clang_type_t property_opaque_type,
2400 ObjCIvarDecl *ivar_decl,
2401 const char *property_setter_name,
2402 const char *property_getter_name,
Sean Callananad880762012-04-18 01:06:17 +00002403 uint32_t property_attributes,
Jim Ingham379397632012-10-27 02:54:13 +00002404 ClangASTMetadata *metadata
Jim Inghame3ae82a2011-11-12 01:36:43 +00002405)
2406{
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002407 if (class_opaque_type == NULL || property_name == NULL || property_name[0] == '\0')
Jim Inghame3ae82a2011-11-12 01:36:43 +00002408 return false;
2409
2410 IdentifierTable *identifier_table = &ast->Idents;
2411
2412 assert (ast != NULL);
2413 assert (identifier_table != NULL);
2414
2415 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2416 const clang::Type *class_type = class_qual_type.getTypePtr();
2417 if (class_type)
2418 {
2419 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
2420
2421 if (objc_class_type)
2422 {
2423 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2424
Greg Clayton23f59502012-07-17 03:23:13 +00002425 clang_type_t property_opaque_type_to_access = NULL;
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002426
2427 if (property_opaque_type)
2428 property_opaque_type_to_access = property_opaque_type;
2429 else if (ivar_decl)
2430 property_opaque_type_to_access = ivar_decl->getType().getAsOpaquePtr();
2431
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002432 if (class_interface_decl && property_opaque_type_to_access)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002433 {
2434 clang::TypeSourceInfo *prop_type_source;
2435 if (ivar_decl)
2436 prop_type_source = ast->CreateTypeSourceInfo (ivar_decl->getType());
2437 else
2438 prop_type_source = ast->CreateTypeSourceInfo (QualType::getFromOpaquePtr(property_opaque_type));
2439
2440 ObjCPropertyDecl *property_decl = ObjCPropertyDecl::Create(*ast,
2441 class_interface_decl,
2442 SourceLocation(), // Source Location
2443 &identifier_table->get(property_name),
2444 SourceLocation(), //Source Location for AT
Sean Callanand5f33a82012-03-01 02:03:47 +00002445 SourceLocation(), //Source location for (
Jim Inghame3ae82a2011-11-12 01:36:43 +00002446 prop_type_source
2447 );
Sean Callananad880762012-04-18 01:06:17 +00002448
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002449 if (property_decl)
2450 {
Jim Ingham379397632012-10-27 02:54:13 +00002451 if (metadata)
2452 SetMetadata(ast, (uintptr_t)property_decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00002453
Jim Inghame3ae82a2011-11-12 01:36:43 +00002454 class_interface_decl->addDecl (property_decl);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002455
2456 Selector setter_sel, getter_sel;
2457
Jim Inghame3ae82a2011-11-12 01:36:43 +00002458 if (property_setter_name != NULL)
2459 {
2460 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
2461 clang::IdentifierInfo *setter_ident = &identifier_table->get(property_setter_no_colon.c_str());
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002462 setter_sel = ast->Selectors.getSelector(1, &setter_ident);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002463 }
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002464 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
2465 {
2466 std::string setter_sel_string("set");
2467 setter_sel_string.push_back(::toupper(property_name[0]));
2468 setter_sel_string.append(&property_name[1]);
2469 clang::IdentifierInfo *setter_ident = &identifier_table->get(setter_sel_string.c_str());
2470 setter_sel = ast->Selectors.getSelector(1, &setter_ident);
2471 }
Sean Callanana6582262012-04-05 00:12:52 +00002472 property_decl->setSetterName(setter_sel);
2473 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002474
2475 if (property_getter_name != NULL)
2476 {
2477 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_getter_name);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002478 getter_sel = ast->Selectors.getSelector(0, &getter_ident);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002479 }
2480 else
2481 {
2482 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_name);
2483 getter_sel = ast->Selectors.getSelector(0, &getter_ident);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002484 }
Sean Callanana6582262012-04-05 00:12:52 +00002485 property_decl->setGetterName(getter_sel);
2486 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002487
2488 if (ivar_decl)
2489 property_decl->setPropertyIvarDecl (ivar_decl);
2490
2491 if (property_attributes & DW_APPLE_PROPERTY_readonly)
2492 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
2493 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
2494 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
2495 if (property_attributes & DW_APPLE_PROPERTY_assign)
2496 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
2497 if (property_attributes & DW_APPLE_PROPERTY_retain)
2498 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
2499 if (property_attributes & DW_APPLE_PROPERTY_copy)
2500 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
2501 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
2502 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002503
2504 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
2505 {
2506 QualType result_type = QualType::getFromOpaquePtr(property_opaque_type_to_access);
2507
2508 const bool isInstance = true;
2509 const bool isVariadic = false;
2510 const bool isSynthesized = false;
2511 const bool isImplicitlyDeclared = true;
2512 const bool isDefined = false;
2513 const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
2514 const bool HasRelatedResultType = false;
2515
2516 ObjCMethodDecl *getter = ObjCMethodDecl::Create(*ast,
2517 SourceLocation(),
2518 SourceLocation(),
2519 getter_sel,
2520 result_type,
2521 NULL,
2522 class_interface_decl,
2523 isInstance,
2524 isVariadic,
2525 isSynthesized,
2526 isImplicitlyDeclared,
2527 isDefined,
2528 impControl,
2529 HasRelatedResultType);
Sean Callananad880762012-04-18 01:06:17 +00002530
Jim Ingham379397632012-10-27 02:54:13 +00002531 if (getter && metadata)
2532 SetMetadata(ast, (uintptr_t)getter, *metadata);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002533
2534 getter->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(), ArrayRef<SourceLocation>());
2535
2536 class_interface_decl->addDecl(getter);
2537 }
2538
2539 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
2540 {
2541 QualType result_type = ast->VoidTy;
2542
2543 const bool isInstance = true;
2544 const bool isVariadic = false;
2545 const bool isSynthesized = false;
2546 const bool isImplicitlyDeclared = true;
2547 const bool isDefined = false;
2548 const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
2549 const bool HasRelatedResultType = false;
2550
2551 ObjCMethodDecl *setter = ObjCMethodDecl::Create(*ast,
2552 SourceLocation(),
2553 SourceLocation(),
2554 setter_sel,
2555 result_type,
2556 NULL,
2557 class_interface_decl,
2558 isInstance,
2559 isVariadic,
2560 isSynthesized,
2561 isImplicitlyDeclared,
2562 isDefined,
2563 impControl,
2564 HasRelatedResultType);
2565
Jim Ingham379397632012-10-27 02:54:13 +00002566 if (setter && metadata)
2567 SetMetadata(ast, (uintptr_t)setter, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00002568
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002569 llvm::SmallVector<ParmVarDecl *, 1> params;
2570
2571 params.push_back (ParmVarDecl::Create (*ast,
2572 setter,
2573 SourceLocation(),
2574 SourceLocation(),
2575 NULL, // anonymous
2576 QualType::getFromOpaquePtr(property_opaque_type_to_access),
2577 NULL,
2578 SC_Auto,
2579 SC_Auto,
2580 NULL));
2581
2582 setter->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
2583
2584 class_interface_decl->addDecl(setter);
2585 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002586
2587 return true;
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002588 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002589 }
2590 }
2591 }
Greg Clayton8cf05932010-07-22 18:30:50 +00002592 return false;
2593}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002594
Greg Clayton9e409562010-07-28 02:04:09 +00002595bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002596ClangASTContext::ObjCTypeHasIVars (clang_type_t class_opaque_type, bool check_superclass)
Greg Clayton9e409562010-07-28 02:04:09 +00002597{
2598 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2599
Sean Callanan78e37602011-01-27 04:42:51 +00002600 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton9e409562010-07-28 02:04:09 +00002601 if (class_type)
2602 {
Sean Callanan78e37602011-01-27 04:42:51 +00002603 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00002604
2605 if (objc_class_type)
2606 return ObjCDeclHasIVars (objc_class_type->getInterface(), check_superclass);
2607 }
2608 return false;
2609}
2610
2611bool
2612ClangASTContext::ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
2613{
2614 while (class_interface_decl)
2615 {
2616 if (class_interface_decl->ivar_size() > 0)
2617 return true;
2618
2619 if (check_superclass)
2620 class_interface_decl = class_interface_decl->getSuperClass();
2621 else
2622 break;
2623 }
2624 return false;
2625}
Greg Clayton0fffff52010-09-24 05:15:53 +00002626
Greg Clayton1be10fc2010-09-29 01:12:09 +00002627ObjCMethodDecl *
Greg Clayton0fffff52010-09-24 05:15:53 +00002628ClangASTContext::AddMethodToObjCObjectType
2629(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002630 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002631 clang_type_t class_opaque_type,
Greg Clayton0fffff52010-09-24 05:15:53 +00002632 const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
Greg Clayton1be10fc2010-09-29 01:12:09 +00002633 clang_type_t method_opaque_type,
Greg Clayton0fffff52010-09-24 05:15:53 +00002634 lldb::AccessType access
2635)
2636{
2637 if (class_opaque_type == NULL || method_opaque_type == NULL)
2638 return NULL;
2639
Greg Clayton6beaaa62011-01-17 03:46:26 +00002640 IdentifierTable *identifier_table = &ast->Idents;
Greg Clayton0fffff52010-09-24 05:15:53 +00002641
Greg Clayton6beaaa62011-01-17 03:46:26 +00002642 assert (ast != NULL);
Greg Clayton0fffff52010-09-24 05:15:53 +00002643 assert (identifier_table != NULL);
2644
2645 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2646
Sean Callanan78e37602011-01-27 04:42:51 +00002647 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton0fffff52010-09-24 05:15:53 +00002648 if (class_type == NULL)
2649 return NULL;
2650
Sean Callanan78e37602011-01-27 04:42:51 +00002651 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton0fffff52010-09-24 05:15:53 +00002652
2653 if (objc_class_type == NULL)
2654 return NULL;
2655
2656 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2657
2658 if (class_interface_decl == NULL)
2659 return NULL;
Greg Clayton9e409562010-07-28 02:04:09 +00002660
Greg Clayton0fffff52010-09-24 05:15:53 +00002661 const char *selector_start = ::strchr (name, ' ');
2662 if (selector_start == NULL)
2663 return NULL;
2664
2665 selector_start++;
2666 if (!(::isalpha (selector_start[0]) || selector_start[0] == '_'))
2667 return NULL;
2668 llvm::SmallVector<IdentifierInfo *, 12> selector_idents;
2669
Greg Clayton450e3f32010-10-12 02:24:53 +00002670 size_t len = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +00002671 const char *start;
Greg Clayton450e3f32010-10-12 02:24:53 +00002672 //printf ("name = '%s'\n", name);
2673
2674 unsigned num_selectors_with_args = 0;
2675 for (start = selector_start;
Greg Clayton0fffff52010-09-24 05:15:53 +00002676 start && *start != '\0' && *start != ']';
Greg Clayton450e3f32010-10-12 02:24:53 +00002677 start += len)
Greg Clayton0fffff52010-09-24 05:15:53 +00002678 {
Greg Clayton450e3f32010-10-12 02:24:53 +00002679 len = ::strcspn(start, ":]");
Greg Clayton90f90cd2010-10-27 04:01:14 +00002680 bool has_arg = (start[len] == ':');
2681 if (has_arg)
Greg Clayton450e3f32010-10-12 02:24:53 +00002682 ++num_selectors_with_args;
Greg Clayton0fffff52010-09-24 05:15:53 +00002683 selector_idents.push_back (&identifier_table->get (StringRef (start, len)));
Greg Clayton90f90cd2010-10-27 04:01:14 +00002684 if (has_arg)
2685 len += 1;
Greg Clayton0fffff52010-09-24 05:15:53 +00002686 }
2687
2688
2689 if (selector_idents.size() == 0)
2690 return 0;
2691
Greg Clayton6beaaa62011-01-17 03:46:26 +00002692 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
Greg Clayton0fffff52010-09-24 05:15:53 +00002693 selector_idents.data());
2694
2695 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
2696
2697 // Populate the method decl with parameter decls
Sean Callanan78e37602011-01-27 04:42:51 +00002698 const clang::Type *method_type(method_qual_type.getTypePtr());
Greg Clayton0fffff52010-09-24 05:15:53 +00002699
2700 if (method_type == NULL)
2701 return NULL;
2702
Sean Callanan78e37602011-01-27 04:42:51 +00002703 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type));
Greg Clayton0fffff52010-09-24 05:15:53 +00002704
2705 if (!method_function_prototype)
2706 return NULL;
2707
2708
2709 bool is_variadic = false;
2710 bool is_synthesized = false;
2711 bool is_defined = false;
2712 ObjCMethodDecl::ImplementationControl imp_control = ObjCMethodDecl::None;
2713
2714 const unsigned num_args = method_function_prototype->getNumArgs();
2715
Greg Clayton6beaaa62011-01-17 03:46:26 +00002716 ObjCMethodDecl *objc_method_decl = ObjCMethodDecl::Create (*ast,
Greg Clayton0fffff52010-09-24 05:15:53 +00002717 SourceLocation(), // beginLoc,
2718 SourceLocation(), // endLoc,
2719 method_selector,
2720 method_function_prototype->getResultType(),
2721 NULL, // TypeSourceInfo *ResultTInfo,
2722 GetDeclContextForType (class_opaque_type),
2723 name[0] == '-',
2724 is_variadic,
2725 is_synthesized,
Sean Callanan880e6802011-10-07 23:18:13 +00002726 true, // is_implicitly_declared
Greg Clayton0fffff52010-09-24 05:15:53 +00002727 is_defined,
2728 imp_control,
Sean Callanan880e6802011-10-07 23:18:13 +00002729 false /*has_related_result_type*/);
Greg Clayton0fffff52010-09-24 05:15:53 +00002730
2731
2732 if (objc_method_decl == NULL)
2733 return NULL;
2734
2735 if (num_args > 0)
2736 {
2737 llvm::SmallVector<ParmVarDecl *, 12> params;
2738
2739 for (int param_index = 0; param_index < num_args; ++param_index)
2740 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002741 params.push_back (ParmVarDecl::Create (*ast,
Greg Clayton0fffff52010-09-24 05:15:53 +00002742 objc_method_decl,
2743 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00002744 SourceLocation(),
Greg Clayton0fffff52010-09-24 05:15:53 +00002745 NULL, // anonymous
2746 method_function_prototype->getArgType(param_index),
2747 NULL,
2748 SC_Auto,
2749 SC_Auto,
2750 NULL));
2751 }
2752
Sean Callanan880e6802011-10-07 23:18:13 +00002753 objc_method_decl->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
Greg Clayton0fffff52010-09-24 05:15:53 +00002754 }
2755
2756 class_interface_decl->addDecl (objc_method_decl);
2757
Sean Callanan5e9e1992011-10-26 01:06:27 +00002758#ifdef LLDB_CONFIGURATION_DEBUG
2759 VerifyDecl(objc_method_decl);
2760#endif
Greg Clayton0fffff52010-09-24 05:15:53 +00002761
2762 return objc_method_decl;
2763}
2764
Greg Clayton402230e2012-02-03 01:30:30 +00002765size_t
2766ClangASTContext::GetNumTemplateArguments (clang::ASTContext *ast, clang_type_t clang_type)
2767{
2768 if (clang_type)
2769 {
2770 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2771
2772 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2773 switch (type_class)
2774 {
2775 case clang::Type::Record:
2776 if (GetCompleteQualType (ast, qual_type))
2777 {
2778 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2779 if (cxx_record_decl)
2780 {
2781 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2782 if (template_decl)
2783 return template_decl->getTemplateArgs().size();
2784 }
2785 }
2786 break;
2787
2788 case clang::Type::Typedef:
2789 return ClangASTContext::GetNumTemplateArguments (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2790 default:
2791 break;
2792 }
2793 }
2794 return 0;
2795}
2796
2797clang_type_t
2798ClangASTContext::GetTemplateArgument (clang::ASTContext *ast, clang_type_t clang_type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
2799{
2800 if (clang_type)
2801 {
2802 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2803
2804 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2805 switch (type_class)
2806 {
2807 case clang::Type::Record:
2808 if (GetCompleteQualType (ast, qual_type))
2809 {
2810 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2811 if (cxx_record_decl)
2812 {
2813 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2814 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
2815 {
2816 const TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
2817 switch (template_arg.getKind())
2818 {
2819 case clang::TemplateArgument::Null:
2820 kind = eTemplateArgumentKindNull;
2821 return NULL;
2822
2823 case clang::TemplateArgument::Type:
2824 kind = eTemplateArgumentKindType;
2825 return template_arg.getAsType().getAsOpaquePtr();
2826
2827 case clang::TemplateArgument::Declaration:
2828 kind = eTemplateArgumentKindDeclaration;
2829 return NULL;
2830
2831 case clang::TemplateArgument::Integral:
2832 kind = eTemplateArgumentKindIntegral;
2833 return template_arg.getIntegralType().getAsOpaquePtr();
2834
2835 case clang::TemplateArgument::Template:
2836 kind = eTemplateArgumentKindTemplate;
2837 return NULL;
2838
2839 case clang::TemplateArgument::TemplateExpansion:
2840 kind = eTemplateArgumentKindTemplateExpansion;
2841 return NULL;
2842
2843 case clang::TemplateArgument::Expression:
2844 kind = eTemplateArgumentKindExpression;
2845 return NULL;
2846
2847 case clang::TemplateArgument::Pack:
2848 kind = eTemplateArgumentKindPack;
2849 return NULL;
2850
2851 default:
2852 assert (!"Unhandled TemplateArgument::ArgKind");
2853 kind = eTemplateArgumentKindNull;
2854 return NULL;
2855 }
2856 }
2857 }
2858 }
2859 break;
2860
2861 case clang::Type::Typedef:
2862 return ClangASTContext::GetTemplateArgument (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), arg_idx, kind);
2863 default:
2864 break;
2865 }
2866 }
2867 kind = eTemplateArgumentKindNull;
2868 return NULL;
2869}
Greg Clayton0fffff52010-09-24 05:15:53 +00002870
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002871uint32_t
Greg Clayton73b472d2010-10-27 03:32:59 +00002872ClangASTContext::GetTypeInfo
2873(
2874 clang_type_t clang_type,
Greg Clayton6beaaa62011-01-17 03:46:26 +00002875 clang::ASTContext *ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00002876 clang_type_t *pointee_or_element_clang_type
2877)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002878{
2879 if (clang_type == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00002880 return 0;
2881
2882 if (pointee_or_element_clang_type)
2883 *pointee_or_element_clang_type = NULL;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002884
2885 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2886
2887 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2888 switch (type_class)
2889 {
Sean Callanana2424172010-10-25 00:29:48 +00002890 case clang::Type::Builtin:
2891 switch (cast<clang::BuiltinType>(qual_type)->getKind())
2892 {
Sean Callanana2424172010-10-25 00:29:48 +00002893 case clang::BuiltinType::ObjCId:
2894 case clang::BuiltinType::ObjCClass:
Greg Clayton6beaaa62011-01-17 03:46:26 +00002895 if (ast && pointee_or_element_clang_type)
2896 *pointee_or_element_clang_type = ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Sean Callanana2424172010-10-25 00:29:48 +00002897 return eTypeIsBuiltIn | eTypeIsPointer | eTypeHasValue;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002898 break;
2899 case clang::BuiltinType::Bool:
2900 case clang::BuiltinType::Char_U:
2901 case clang::BuiltinType::UChar:
2902 case clang::BuiltinType::WChar_U:
2903 case clang::BuiltinType::Char16:
2904 case clang::BuiltinType::Char32:
2905 case clang::BuiltinType::UShort:
2906 case clang::BuiltinType::UInt:
2907 case clang::BuiltinType::ULong:
2908 case clang::BuiltinType::ULongLong:
2909 case clang::BuiltinType::UInt128:
2910 case clang::BuiltinType::Char_S:
2911 case clang::BuiltinType::SChar:
2912 case clang::BuiltinType::WChar_S:
2913 case clang::BuiltinType::Short:
2914 case clang::BuiltinType::Int:
2915 case clang::BuiltinType::Long:
2916 case clang::BuiltinType::LongLong:
2917 case clang::BuiltinType::Int128:
2918 case clang::BuiltinType::Float:
2919 case clang::BuiltinType::Double:
2920 case clang::BuiltinType::LongDouble:
2921 return eTypeIsBuiltIn | eTypeHasValue | eTypeIsScalar;
Greg Clayton73b472d2010-10-27 03:32:59 +00002922 default:
2923 break;
Sean Callanana2424172010-10-25 00:29:48 +00002924 }
2925 return eTypeIsBuiltIn | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002926
2927 case clang::Type::BlockPointer:
2928 if (pointee_or_element_clang_type)
2929 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2930 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
2931
Greg Clayton49462ea2011-01-15 02:52:14 +00002932 case clang::Type::Complex: return eTypeIsBuiltIn | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002933
2934 case clang::Type::ConstantArray:
2935 case clang::Type::DependentSizedArray:
2936 case clang::Type::IncompleteArray:
2937 case clang::Type::VariableArray:
2938 if (pointee_or_element_clang_type)
2939 *pointee_or_element_clang_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
2940 return eTypeHasChildren | eTypeIsArray;
2941
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002942 case clang::Type::DependentName: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002943 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
2944 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
2945 case clang::Type::Decltype: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00002946
2947 case clang::Type::Enum:
2948 if (pointee_or_element_clang_type)
2949 *pointee_or_element_clang_type = cast<EnumType>(qual_type)->getDecl()->getIntegerType().getAsOpaquePtr();
2950 return eTypeIsEnumeration | eTypeHasValue;
2951
Sean Callanan912855f2011-08-11 23:56:13 +00002952 case clang::Type::Elaborated:
2953 return ClangASTContext::GetTypeInfo (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2954 ast,
2955 pointee_or_element_clang_type);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002956 case clang::Type::ExtVector: return eTypeHasChildren | eTypeIsVector;
2957 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
2958 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002959 case clang::Type::InjectedClassName: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00002960
2961 case clang::Type::LValueReference:
2962 case clang::Type::RValueReference:
2963 if (pointee_or_element_clang_type)
2964 *pointee_or_element_clang_type = cast<ReferenceType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr();
2965 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
2966
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002967 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002968
2969 case clang::Type::ObjCObjectPointer:
2970 if (pointee_or_element_clang_type)
2971 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2972 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
2973
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002974 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
2975 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
Greg Clayton73b472d2010-10-27 03:32:59 +00002976
2977 case clang::Type::Pointer:
2978 if (pointee_or_element_clang_type)
2979 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2980 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
2981
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002982 case clang::Type::Record:
2983 if (qual_type->getAsCXXRecordDecl())
2984 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
2985 else
2986 return eTypeHasChildren | eTypeIsStructUnion;
2987 break;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002988 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
2989 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
2990 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
Greg Clayton73b472d2010-10-27 03:32:59 +00002991
2992 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00002993 return eTypeIsTypedef | ClangASTContext::GetTypeInfo (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00002994 ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00002995 pointee_or_element_clang_type);
2996
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002997 case clang::Type::TypeOfExpr: return 0;
2998 case clang::Type::TypeOf: return 0;
2999 case clang::Type::UnresolvedUsing: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003000 case clang::Type::Vector: return eTypeHasChildren | eTypeIsVector;
3001 default: return 0;
3002 }
3003 return 0;
3004}
3005
Greg Clayton9e409562010-07-28 02:04:09 +00003006
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003007#pragma mark Aggregate Types
3008
3009bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00003010ClangASTContext::IsAggregateType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003011{
3012 if (clang_type == NULL)
3013 return false;
3014
3015 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3016
Greg Clayton737b9322010-09-13 03:32:57 +00003017 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3018 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003019 {
Greg Claytone1a916a2010-07-21 22:12:05 +00003020 case clang::Type::IncompleteArray:
3021 case clang::Type::VariableArray:
3022 case clang::Type::ConstantArray:
3023 case clang::Type::ExtVector:
3024 case clang::Type::Vector:
3025 case clang::Type::Record:
Greg Clayton9e409562010-07-28 02:04:09 +00003026 case clang::Type::ObjCObject:
3027 case clang::Type::ObjCInterface:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003028 return true;
Sean Callanan912855f2011-08-11 23:56:13 +00003029 case clang::Type::Elaborated:
3030 return ClangASTContext::IsAggregateType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Claytone1a916a2010-07-21 22:12:05 +00003031 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00003032 return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003033
3034 default:
3035 break;
3036 }
3037 // The clang type does have a value
3038 return false;
3039}
3040
3041uint32_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00003042ClangASTContext::GetNumChildren (clang::ASTContext *ast, clang_type_t clang_type, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003043{
Greg Clayton6beaaa62011-01-17 03:46:26 +00003044 if (clang_type == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003045 return 0;
3046
3047 uint32_t num_children = 0;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003048 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00003049 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3050 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003051 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003052 case clang::Type::Builtin:
3053 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3054 {
Greg Clayton73b472d2010-10-27 03:32:59 +00003055 case clang::BuiltinType::ObjCId: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003056 case clang::BuiltinType::ObjCClass: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003057 num_children = 1;
Greg Clayton73b472d2010-10-27 03:32:59 +00003058 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003059
3060 default:
3061 break;
3062 }
3063 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00003064
Greg Clayton49462ea2011-01-15 02:52:14 +00003065 case clang::Type::Complex: return 0;
Greg Clayton54979cd2010-12-15 05:08:08 +00003066
Greg Claytone1a916a2010-07-21 22:12:05 +00003067 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00003068 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003069 {
3070 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3071 const RecordDecl *record_decl = record_type->getDecl();
3072 assert(record_decl);
3073 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3074 if (cxx_record_decl)
3075 {
3076 if (omit_empty_base_classes)
3077 {
3078 // Check each base classes to see if it or any of its
3079 // base classes contain any fields. This can help
3080 // limit the noise in variable views by not having to
3081 // show base classes that contain no members.
3082 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3083 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3084 base_class != base_class_end;
3085 ++base_class)
3086 {
3087 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3088
3089 // Skip empty base classes
3090 if (RecordHasFields(base_class_decl) == false)
3091 continue;
3092
3093 num_children++;
3094 }
3095 }
3096 else
3097 {
3098 // Include all base classes
3099 num_children += cxx_record_decl->getNumBases();
3100 }
3101
3102 }
3103 RecordDecl::field_iterator field, field_end;
3104 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3105 ++num_children;
3106 }
3107 break;
3108
Greg Clayton9e409562010-07-28 02:04:09 +00003109 case clang::Type::ObjCObject:
3110 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00003111 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00003112 {
Sean Callanan78e37602011-01-27 04:42:51 +00003113 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003114 assert (objc_class_type);
3115 if (objc_class_type)
3116 {
3117 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3118
3119 if (class_interface_decl)
3120 {
3121
3122 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3123 if (superclass_interface_decl)
3124 {
3125 if (omit_empty_base_classes)
3126 {
3127 if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true))
3128 ++num_children;
3129 }
3130 else
3131 ++num_children;
3132 }
3133
3134 num_children += class_interface_decl->ivar_size();
3135 }
3136 }
3137 }
3138 break;
3139
3140 case clang::Type::ObjCObjectPointer:
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003141 {
Sean Callanan78e37602011-01-27 04:42:51 +00003142 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003143 QualType pointee_type = pointer_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003144 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3145 pointee_type.getAsOpaquePtr(),
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003146 omit_empty_base_classes);
3147 // If this type points to a simple type, then it has 1 child
3148 if (num_pointee_children == 0)
3149 num_children = 1;
3150 else
3151 num_children = num_pointee_children;
3152 }
3153 break;
Greg Clayton9e409562010-07-28 02:04:09 +00003154
Greg Claytone1a916a2010-07-21 22:12:05 +00003155 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003156 num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
3157 break;
3158
Greg Claytone1a916a2010-07-21 22:12:05 +00003159 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003160 {
Sean Callanan78e37602011-01-27 04:42:51 +00003161 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003162 QualType pointee_type (pointer_type->getPointeeType());
Greg Clayton6beaaa62011-01-17 03:46:26 +00003163 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3164 pointee_type.getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00003165 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003166 if (num_pointee_children == 0)
Greg Clayton54979cd2010-12-15 05:08:08 +00003167 {
3168 // We have a pointer to a pointee type that claims it has no children.
3169 // We will want to look at
3170 num_children = ClangASTContext::GetNumPointeeChildren (pointee_type.getAsOpaquePtr());
3171 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003172 else
3173 num_children = num_pointee_children;
3174 }
3175 break;
3176
Greg Clayton73b472d2010-10-27 03:32:59 +00003177 case clang::Type::LValueReference:
3178 case clang::Type::RValueReference:
3179 {
Sean Callanan78e37602011-01-27 04:42:51 +00003180 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton73b472d2010-10-27 03:32:59 +00003181 QualType pointee_type = reference_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003182 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3183 pointee_type.getAsOpaquePtr(),
Greg Clayton73b472d2010-10-27 03:32:59 +00003184 omit_empty_base_classes);
3185 // If this type points to a simple type, then it has 1 child
3186 if (num_pointee_children == 0)
3187 num_children = 1;
3188 else
3189 num_children = num_pointee_children;
3190 }
3191 break;
3192
3193
Greg Claytone1a916a2010-07-21 22:12:05 +00003194 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00003195 num_children = ClangASTContext::GetNumChildren (ast,
3196 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3197 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003198 break;
Sean Callanan912855f2011-08-11 23:56:13 +00003199
3200 case clang::Type::Elaborated:
3201 num_children = ClangASTContext::GetNumChildren (ast,
3202 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3203 omit_empty_base_classes);
3204 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003205
3206 default:
3207 break;
3208 }
3209 return num_children;
3210}
3211
Greg Claytonbf2331c2011-09-09 23:04:00 +00003212uint32_t
3213ClangASTContext::GetNumDirectBaseClasses (clang::ASTContext *ast, clang_type_t clang_type)
3214{
3215 if (clang_type == NULL)
3216 return 0;
3217
3218 uint32_t count = 0;
3219 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3220 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3221 switch (type_class)
3222 {
3223 case clang::Type::Record:
3224 if (GetCompleteQualType (ast, qual_type))
3225 {
3226 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3227 if (cxx_record_decl)
3228 count = cxx_record_decl->getNumBases();
3229 }
3230 break;
3231
3232 case clang::Type::ObjCObject:
3233 case clang::Type::ObjCInterface:
3234 if (GetCompleteQualType (ast, qual_type))
3235 {
3236 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3237 if (objc_class_type)
3238 {
3239 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3240
3241 if (class_interface_decl && class_interface_decl->getSuperClass())
3242 count = 1;
3243 }
3244 }
3245 break;
3246
3247
3248 case clang::Type::Typedef:
3249 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3250 break;
3251
3252 case clang::Type::Elaborated:
3253 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3254 break;
3255
3256 default:
3257 break;
3258 }
3259 return count;
3260}
3261
3262uint32_t
3263ClangASTContext::GetNumVirtualBaseClasses (clang::ASTContext *ast,
3264 clang_type_t clang_type)
3265{
3266 if (clang_type == NULL)
3267 return 0;
3268
3269 uint32_t count = 0;
3270 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3271 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3272 switch (type_class)
3273 {
3274 case clang::Type::Record:
3275 if (GetCompleteQualType (ast, qual_type))
3276 {
3277 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3278 if (cxx_record_decl)
3279 count = cxx_record_decl->getNumVBases();
3280 }
3281 break;
3282
3283 case clang::Type::Typedef:
3284 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3285 break;
3286
3287 case clang::Type::Elaborated:
3288 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3289 break;
3290
3291 default:
3292 break;
3293 }
3294 return count;
3295}
3296
3297uint32_t
3298ClangASTContext::GetNumFields (clang::ASTContext *ast, clang_type_t clang_type)
3299{
3300 if (clang_type == NULL)
3301 return 0;
3302
3303 uint32_t count = 0;
3304 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3305 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3306 switch (type_class)
3307 {
3308 case clang::Type::Record:
3309 if (GetCompleteQualType (ast, qual_type))
3310 {
3311 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
3312 if (record_type)
3313 {
3314 RecordDecl *record_decl = record_type->getDecl();
3315 if (record_decl)
3316 {
3317 uint32_t field_idx = 0;
3318 RecordDecl::field_iterator field, field_end;
3319 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3320 ++field_idx;
3321 count = field_idx;
3322 }
3323 }
3324 }
3325 break;
3326
3327 case clang::Type::Typedef:
3328 count = ClangASTContext::GetNumFields (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3329 break;
3330
3331 case clang::Type::Elaborated:
3332 count = ClangASTContext::GetNumFields (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3333 break;
3334
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003335 case clang::Type::ObjCObject:
3336 case clang::Type::ObjCInterface:
3337 if (GetCompleteQualType (ast, qual_type))
3338 {
3339 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3340 if (objc_class_type)
3341 {
3342 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3343
3344 if (class_interface_decl)
3345 count = class_interface_decl->ivar_size();
3346 }
3347 }
3348 break;
3349
Greg Claytonbf2331c2011-09-09 23:04:00 +00003350 default:
3351 break;
3352 }
3353 return count;
3354}
3355
3356clang_type_t
3357ClangASTContext::GetDirectBaseClassAtIndex (clang::ASTContext *ast,
3358 clang_type_t clang_type,
3359 uint32_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003360 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003361{
3362 if (clang_type == NULL)
3363 return 0;
3364
3365 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3366 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3367 switch (type_class)
3368 {
3369 case clang::Type::Record:
3370 if (GetCompleteQualType (ast, qual_type))
3371 {
3372 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3373 if (cxx_record_decl)
3374 {
3375 uint32_t curr_idx = 0;
3376 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3377 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3378 base_class != base_class_end;
3379 ++base_class, ++curr_idx)
3380 {
3381 if (curr_idx == idx)
3382 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003383 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003384 {
3385 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3386 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3387// if (base_class->isVirtual())
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003388// *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003389// else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003390 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003391 }
3392 return base_class->getType().getAsOpaquePtr();
3393 }
3394 }
3395 }
3396 }
3397 break;
3398
3399 case clang::Type::ObjCObject:
3400 case clang::Type::ObjCInterface:
3401 if (idx == 0 && GetCompleteQualType (ast, qual_type))
3402 {
3403 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3404 if (objc_class_type)
3405 {
3406 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3407
3408 if (class_interface_decl)
3409 {
3410 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3411 if (superclass_interface_decl)
3412 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003413 if (bit_offset_ptr)
3414 *bit_offset_ptr = 0;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003415 return ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr();
3416 }
3417 }
3418 }
3419 }
3420 break;
3421
3422
3423 case clang::Type::Typedef:
3424 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3425 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3426 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003427 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003428
3429 case clang::Type::Elaborated:
3430 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3431 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3432 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003433 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003434
3435 default:
3436 break;
3437 }
3438 return NULL;
3439}
3440
3441clang_type_t
3442ClangASTContext::GetVirtualBaseClassAtIndex (clang::ASTContext *ast,
3443 clang_type_t clang_type,
3444 uint32_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003445 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003446{
3447 if (clang_type == NULL)
3448 return 0;
3449
3450 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3451 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3452 switch (type_class)
3453 {
3454 case clang::Type::Record:
3455 if (GetCompleteQualType (ast, qual_type))
3456 {
3457 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3458 if (cxx_record_decl)
3459 {
3460 uint32_t curr_idx = 0;
3461 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3462 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
3463 base_class != base_class_end;
3464 ++base_class, ++curr_idx)
3465 {
3466 if (curr_idx == idx)
3467 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003468 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003469 {
3470 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3471 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003472 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003473
3474 }
3475 return base_class->getType().getAsOpaquePtr();
3476 }
3477 }
3478 }
3479 }
3480 break;
3481
3482 case clang::Type::Typedef:
3483 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3484 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3485 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003486 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003487
3488 case clang::Type::Elaborated:
3489 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3490 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3491 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003492 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003493
3494 default:
3495 break;
3496 }
3497 return NULL;
3498}
3499
3500clang_type_t
3501ClangASTContext::GetFieldAtIndex (clang::ASTContext *ast,
3502 clang_type_t clang_type,
3503 uint32_t idx,
3504 std::string& name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003505 uint64_t *bit_offset_ptr,
3506 uint32_t *bitfield_bit_size_ptr,
3507 bool *is_bitfield_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003508{
3509 if (clang_type == NULL)
3510 return 0;
3511
3512 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3513 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3514 switch (type_class)
3515 {
3516 case clang::Type::Record:
3517 if (GetCompleteQualType (ast, qual_type))
3518 {
3519 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3520 const RecordDecl *record_decl = record_type->getDecl();
3521 uint32_t field_idx = 0;
3522 RecordDecl::field_iterator field, field_end;
3523 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
3524 {
3525 if (idx == field_idx)
3526 {
3527 // Print the member type if requested
3528 // Print the member name and equal sign
3529 name.assign(field->getNameAsString());
3530
3531 // Figure out the type byte size (field_type_info.first) and
3532 // alignment (field_type_info.second) from the AST context.
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003533 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003534 {
3535 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003536 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003537 }
3538
Greg Clayton1811b4f2012-07-31 23:39:10 +00003539 const bool is_bitfield = field->isBitField();
3540
3541 if (bitfield_bit_size_ptr)
3542 {
3543 *bitfield_bit_size_ptr = 0;
3544
3545 if (is_bitfield && ast)
3546 {
3547 Expr *bitfield_bit_size_expr = field->getBitWidth();
3548 llvm::APSInt bitfield_apsint;
3549 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
3550 {
3551 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
3552 }
3553 }
3554 }
3555 if (is_bitfield_ptr)
3556 *is_bitfield_ptr = is_bitfield;
3557
Greg Claytonbf2331c2011-09-09 23:04:00 +00003558 return field->getType().getAsOpaquePtr();
3559 }
3560 }
3561 }
3562 break;
3563
3564 case clang::Type::ObjCObject:
3565 case clang::Type::ObjCInterface:
3566 if (GetCompleteQualType (ast, qual_type))
3567 {
3568 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3569 assert (objc_class_type);
3570 if (objc_class_type)
3571 {
3572 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3573
3574 if (class_interface_decl)
3575 {
3576 if (idx < (class_interface_decl->ivar_size()))
3577 {
3578 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
3579 uint32_t ivar_idx = 0;
3580
3581 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
3582 {
3583 if (ivar_idx == idx)
3584 {
3585 const ObjCIvarDecl* ivar_decl = *ivar_pos;
3586
3587 QualType ivar_qual_type(ivar_decl->getType());
3588
3589 name.assign(ivar_decl->getNameAsString());
3590
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003591 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003592 {
3593 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003594 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003595 }
3596
Greg Clayton1811b4f2012-07-31 23:39:10 +00003597 const bool is_bitfield = ivar_pos->isBitField();
3598
3599 if (bitfield_bit_size_ptr)
3600 {
3601 *bitfield_bit_size_ptr = 0;
3602
3603 if (is_bitfield && ast)
3604 {
3605 Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
3606 llvm::APSInt bitfield_apsint;
3607 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
3608 {
3609 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
3610 }
3611 }
3612 }
3613 if (is_bitfield_ptr)
3614 *is_bitfield_ptr = is_bitfield;
3615
Greg Claytonbf2331c2011-09-09 23:04:00 +00003616 return ivar_qual_type.getAsOpaquePtr();
3617 }
3618 }
3619 }
3620 }
3621 }
3622 }
3623 break;
3624
3625
3626 case clang::Type::Typedef:
3627 return ClangASTContext::GetFieldAtIndex (ast,
3628 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3629 idx,
3630 name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003631 bit_offset_ptr,
3632 bitfield_bit_size_ptr,
3633 is_bitfield_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003634
3635 case clang::Type::Elaborated:
3636 return ClangASTContext::GetFieldAtIndex (ast,
3637 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3638 idx,
3639 name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003640 bit_offset_ptr,
3641 bitfield_bit_size_ptr,
3642 is_bitfield_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003643
3644 default:
3645 break;
3646 }
3647 return NULL;
3648}
3649
Greg Claytoneaafa732012-10-13 00:20:27 +00003650lldb::BasicType
3651ClangASTContext::GetLLDBBasicTypeEnumeration (clang_type_t clang_type)
3652{
3653 if (clang_type)
3654 {
3655 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3656 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Claytonc4c5e892012-10-15 21:16:43 +00003657 if (type_class == clang::Type::Builtin)
Greg Claytoneaafa732012-10-13 00:20:27 +00003658 {
Greg Claytoneaafa732012-10-13 00:20:27 +00003659 switch (cast<clang::BuiltinType>(qual_type)->getKind())
Greg Claytonc4c5e892012-10-15 21:16:43 +00003660 {
Greg Claytoneaafa732012-10-13 00:20:27 +00003661 case clang::BuiltinType::Void: return eBasicTypeVoid;
3662 case clang::BuiltinType::Bool: return eBasicTypeBool;
3663 case clang::BuiltinType::Char_S: return eBasicTypeSignedChar;
3664 case clang::BuiltinType::Char_U: return eBasicTypeUnsignedChar;
3665 case clang::BuiltinType::Char16: return eBasicTypeChar16;
3666 case clang::BuiltinType::Char32: return eBasicTypeChar32;
3667 case clang::BuiltinType::UChar: return eBasicTypeUnsignedChar;
3668 case clang::BuiltinType::SChar: return eBasicTypeSignedChar;
3669 case clang::BuiltinType::WChar_S: return eBasicTypeSignedWChar;
3670 case clang::BuiltinType::WChar_U: return eBasicTypeUnsignedWChar;
3671 case clang::BuiltinType::Short: return eBasicTypeShort;
3672 case clang::BuiltinType::UShort: return eBasicTypeUnsignedShort;
3673 case clang::BuiltinType::Int: return eBasicTypeInt;
3674 case clang::BuiltinType::UInt: return eBasicTypeUnsignedInt;
3675 case clang::BuiltinType::Long: return eBasicTypeLong;
3676 case clang::BuiltinType::ULong: return eBasicTypeUnsignedLong;
3677 case clang::BuiltinType::LongLong: return eBasicTypeLongLong;
3678 case clang::BuiltinType::ULongLong: return eBasicTypeUnsignedLongLong;
3679 case clang::BuiltinType::Int128: return eBasicTypeInt128;
3680 case clang::BuiltinType::UInt128: return eBasicTypeUnsignedInt128;
3681
3682 case clang::BuiltinType::Half: return eBasicTypeHalf;
3683 case clang::BuiltinType::Float: return eBasicTypeFloat;
3684 case clang::BuiltinType::Double: return eBasicTypeDouble;
3685 case clang::BuiltinType::LongDouble:return eBasicTypeLongDouble;
3686
3687 case clang::BuiltinType::NullPtr: return eBasicTypeNullPtr;
3688 case clang::BuiltinType::ObjCId: return eBasicTypeObjCID;
3689 case clang::BuiltinType::ObjCClass: return eBasicTypeObjCClass;
3690 case clang::BuiltinType::ObjCSel: return eBasicTypeObjCSel;
3691 case clang::BuiltinType::Dependent:
3692 case clang::BuiltinType::Overload:
3693 case clang::BuiltinType::BoundMember:
3694 case clang::BuiltinType::PseudoObject:
3695 case clang::BuiltinType::UnknownAny:
3696 case clang::BuiltinType::BuiltinFn:
3697 case clang::BuiltinType::ARCUnbridgedCast:
3698 return eBasicTypeOther;
Greg Claytonc4c5e892012-10-15 21:16:43 +00003699 }
Greg Claytoneaafa732012-10-13 00:20:27 +00003700 }
3701 }
3702
3703 return eBasicTypeInvalid;
3704}
3705
3706
Greg Claytonbf2331c2011-09-09 23:04:00 +00003707
Greg Clayton54979cd2010-12-15 05:08:08 +00003708// If a pointer to a pointee type (the clang_type arg) says that it has no
3709// children, then we either need to trust it, or override it and return a
3710// different result. For example, an "int *" has one child that is an integer,
3711// but a function pointer doesn't have any children. Likewise if a Record type
3712// claims it has no children, then there really is nothing to show.
3713uint32_t
3714ClangASTContext::GetNumPointeeChildren (clang_type_t clang_type)
3715{
3716 if (clang_type == NULL)
3717 return 0;
3718
3719 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3720 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3721 switch (type_class)
3722 {
Greg Clayton97a43712011-01-08 22:26:47 +00003723 case clang::Type::Builtin:
3724 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3725 {
Greg Clayton7260f622011-04-18 08:33:37 +00003726 case clang::BuiltinType::UnknownAny:
Greg Clayton97a43712011-01-08 22:26:47 +00003727 case clang::BuiltinType::Void:
3728 case clang::BuiltinType::NullPtr:
3729 return 0;
3730 case clang::BuiltinType::Bool:
3731 case clang::BuiltinType::Char_U:
3732 case clang::BuiltinType::UChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003733 case clang::BuiltinType::WChar_U:
Greg Clayton97a43712011-01-08 22:26:47 +00003734 case clang::BuiltinType::Char16:
3735 case clang::BuiltinType::Char32:
3736 case clang::BuiltinType::UShort:
3737 case clang::BuiltinType::UInt:
3738 case clang::BuiltinType::ULong:
3739 case clang::BuiltinType::ULongLong:
3740 case clang::BuiltinType::UInt128:
3741 case clang::BuiltinType::Char_S:
3742 case clang::BuiltinType::SChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003743 case clang::BuiltinType::WChar_S:
Greg Clayton97a43712011-01-08 22:26:47 +00003744 case clang::BuiltinType::Short:
3745 case clang::BuiltinType::Int:
3746 case clang::BuiltinType::Long:
3747 case clang::BuiltinType::LongLong:
3748 case clang::BuiltinType::Int128:
3749 case clang::BuiltinType::Float:
3750 case clang::BuiltinType::Double:
3751 case clang::BuiltinType::LongDouble:
3752 case clang::BuiltinType::Dependent:
3753 case clang::BuiltinType::Overload:
Greg Clayton97a43712011-01-08 22:26:47 +00003754 case clang::BuiltinType::ObjCId:
3755 case clang::BuiltinType::ObjCClass:
3756 case clang::BuiltinType::ObjCSel:
Sean Callanand12cf8bb2011-05-15 22:34:38 +00003757 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00003758 case clang::BuiltinType::Half:
3759 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00003760 case clang::BuiltinType::PseudoObject:
Sean Callanan3d654b32012-09-24 22:25:51 +00003761 case clang::BuiltinType::BuiltinFn:
Greg Clayton97a43712011-01-08 22:26:47 +00003762 return 1;
3763 }
3764 break;
3765
Greg Clayton49462ea2011-01-15 02:52:14 +00003766 case clang::Type::Complex: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003767 case clang::Type::Pointer: return 1;
3768 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
3769 case clang::Type::LValueReference: return 1;
3770 case clang::Type::RValueReference: return 1;
3771 case clang::Type::MemberPointer: return 0;
3772 case clang::Type::ConstantArray: return 0;
3773 case clang::Type::IncompleteArray: return 0;
3774 case clang::Type::VariableArray: return 0;
3775 case clang::Type::DependentSizedArray: return 0;
3776 case clang::Type::DependentSizedExtVector: return 0;
3777 case clang::Type::Vector: return 0;
3778 case clang::Type::ExtVector: return 0;
3779 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
3780 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
3781 case clang::Type::UnresolvedUsing: return 0;
3782 case clang::Type::Paren: return 0;
3783 case clang::Type::Typedef: return ClangASTContext::GetNumPointeeChildren (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00003784 case clang::Type::Elaborated: return ClangASTContext::GetNumPointeeChildren (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003785 case clang::Type::TypeOfExpr: return 0;
3786 case clang::Type::TypeOf: return 0;
3787 case clang::Type::Decltype: return 0;
3788 case clang::Type::Record: return 0;
3789 case clang::Type::Enum: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003790 case clang::Type::TemplateTypeParm: return 1;
3791 case clang::Type::SubstTemplateTypeParm: return 1;
3792 case clang::Type::TemplateSpecialization: return 1;
3793 case clang::Type::InjectedClassName: return 0;
3794 case clang::Type::DependentName: return 1;
3795 case clang::Type::DependentTemplateSpecialization: return 1;
3796 case clang::Type::ObjCObject: return 0;
3797 case clang::Type::ObjCInterface: return 0;
3798 case clang::Type::ObjCObjectPointer: return 1;
3799 default:
3800 break;
3801 }
3802 return 0;
3803}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003804
Greg Clayton1be10fc2010-09-29 01:12:09 +00003805clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003806ClangASTContext::GetChildClangTypeAtIndex
3807(
Jim Inghamd555bac2011-06-24 22:03:24 +00003808 ExecutionContext *exe_ctx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003809 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003810 clang_type_t parent_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003811 uint32_t idx,
3812 bool transparent_pointers,
3813 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003814 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003815 std::string& child_name,
3816 uint32_t &child_byte_size,
3817 int32_t &child_byte_offset,
3818 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003819 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003820 bool &child_is_base_class,
3821 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003822)
3823{
3824 if (parent_clang_type)
3825
Jim Inghamd555bac2011-06-24 22:03:24 +00003826 return GetChildClangTypeAtIndex (exe_ctx,
3827 getASTContext(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003828 parent_name,
3829 parent_clang_type,
3830 idx,
3831 transparent_pointers,
3832 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003833 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003834 child_name,
3835 child_byte_size,
3836 child_byte_offset,
3837 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003838 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003839 child_is_base_class,
3840 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003841 return NULL;
3842}
3843
Greg Clayton1be10fc2010-09-29 01:12:09 +00003844clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003845ClangASTContext::GetChildClangTypeAtIndex
3846(
Jim Inghamd555bac2011-06-24 22:03:24 +00003847 ExecutionContext *exe_ctx,
Greg Clayton6beaaa62011-01-17 03:46:26 +00003848 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003849 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003850 clang_type_t parent_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003851 uint32_t idx,
3852 bool transparent_pointers,
3853 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003854 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003855 std::string& child_name,
3856 uint32_t &child_byte_size,
3857 int32_t &child_byte_offset,
3858 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003859 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003860 bool &child_is_base_class,
3861 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003862)
3863{
3864 if (parent_clang_type == NULL)
3865 return NULL;
3866
Greg Clayton6beaaa62011-01-17 03:46:26 +00003867 if (idx < ClangASTContext::GetNumChildren (ast, parent_clang_type, omit_empty_base_classes))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003868 {
3869 uint32_t bit_offset;
3870 child_bitfield_bit_size = 0;
3871 child_bitfield_bit_offset = 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003872 child_is_base_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003873 QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00003874 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
3875 switch (parent_type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003876 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003877 case clang::Type::Builtin:
3878 switch (cast<clang::BuiltinType>(parent_qual_type)->getKind())
3879 {
3880 case clang::BuiltinType::ObjCId:
3881 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00003882 child_name = "isa";
Greg Clayton6beaaa62011-01-17 03:46:26 +00003883 child_byte_size = ast->getTypeSize(ast->ObjCBuiltinClassTy) / CHAR_BIT;
3884 return ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003885
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003886 default:
3887 break;
3888 }
3889 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003890
Greg Claytone1a916a2010-07-21 22:12:05 +00003891 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00003892 if (GetCompleteQualType (ast, parent_qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003893 {
3894 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr());
3895 const RecordDecl *record_decl = record_type->getDecl();
3896 assert(record_decl);
Greg Clayton6beaaa62011-01-17 03:46:26 +00003897 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003898 uint32_t child_idx = 0;
3899
3900 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3901 if (cxx_record_decl)
3902 {
3903 // We might have base classes to print out first
3904 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3905 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3906 base_class != base_class_end;
3907 ++base_class)
3908 {
3909 const CXXRecordDecl *base_class_decl = NULL;
3910
3911 // Skip empty base classes
3912 if (omit_empty_base_classes)
3913 {
3914 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3915 if (RecordHasFields(base_class_decl) == false)
3916 continue;
3917 }
3918
3919 if (idx == child_idx)
3920 {
3921 if (base_class_decl == NULL)
3922 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3923
3924
3925 if (base_class->isVirtual())
Greg Clayton6ed95942011-01-22 07:12:45 +00003926 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003927 else
Greg Clayton6ed95942011-01-22 07:12:45 +00003928 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003929
3930 // Base classes should be a multiple of 8 bits in size
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003931 child_byte_offset = bit_offset/8;
Greg Claytone3055942011-06-30 02:28:26 +00003932
Greg Clayton84db9102012-03-26 23:03:23 +00003933 child_name = ClangASTType::GetTypeNameForQualType(ast, base_class->getType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003934
Greg Clayton6beaaa62011-01-17 03:46:26 +00003935 uint64_t clang_type_info_bit_size = ast->getTypeSize(base_class->getType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003936
Jim Inghamf46b3382011-04-15 23:42:06 +00003937 // Base classes bit sizes should be a multiple of 8 bits in size
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003938 assert (clang_type_info_bit_size % 8 == 0);
3939 child_byte_size = clang_type_info_bit_size / 8;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003940 child_is_base_class = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003941 return base_class->getType().getAsOpaquePtr();
3942 }
3943 // We don't increment the child index in the for loop since we might
3944 // be skipping empty base classes
3945 ++child_idx;
3946 }
3947 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003948 // Make sure index is in range...
3949 uint32_t field_idx = 0;
3950 RecordDecl::field_iterator field, field_end;
3951 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
3952 {
3953 if (idx == child_idx)
3954 {
3955 // Print the member type if requested
3956 // Print the member name and equal sign
3957 child_name.assign(field->getNameAsString().c_str());
3958
3959 // Figure out the type byte size (field_type_info.first) and
3960 // alignment (field_type_info.second) from the AST context.
Greg Clayton6beaaa62011-01-17 03:46:26 +00003961 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(field->getType());
Greg Claytonc982c762010-07-09 20:39:50 +00003962 assert(field_idx < record_layout.getFieldCount());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003963
3964 child_byte_size = field_type_info.first / 8;
3965
3966 // Figure out the field offset within the current struct/union/class type
3967 bit_offset = record_layout.getFieldOffset (field_idx);
3968 child_byte_offset = bit_offset / 8;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003969 if (ClangASTContext::FieldIsBitfield (ast, *field, child_bitfield_bit_size))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003970 child_bitfield_bit_offset = bit_offset % 8;
3971
3972 return field->getType().getAsOpaquePtr();
3973 }
3974 }
3975 }
3976 break;
3977
Greg Clayton9e409562010-07-28 02:04:09 +00003978 case clang::Type::ObjCObject:
3979 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00003980 if (GetCompleteQualType (ast, parent_qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00003981 {
Sean Callanan78e37602011-01-27 04:42:51 +00003982 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003983 assert (objc_class_type);
3984 if (objc_class_type)
3985 {
3986 uint32_t child_idx = 0;
3987 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3988
3989 if (class_interface_decl)
3990 {
3991
Greg Clayton6beaaa62011-01-17 03:46:26 +00003992 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
Greg Clayton9e409562010-07-28 02:04:09 +00003993 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3994 if (superclass_interface_decl)
3995 {
3996 if (omit_empty_base_classes)
3997 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00003998 if (ClangASTContext::GetNumChildren(ast, ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0)
Greg Clayton9e409562010-07-28 02:04:09 +00003999 {
4000 if (idx == 0)
4001 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004002 QualType ivar_qual_type(ast->getObjCInterfaceType(superclass_interface_decl));
Greg Clayton9e409562010-07-28 02:04:09 +00004003
4004
4005 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
4006
Greg Clayton6beaaa62011-01-17 03:46:26 +00004007 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004008
4009 child_byte_size = ivar_type_info.first / 8;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004010 child_byte_offset = 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00004011 child_is_base_class = true;
Greg Clayton9e409562010-07-28 02:04:09 +00004012
4013 return ivar_qual_type.getAsOpaquePtr();
4014 }
4015
4016 ++child_idx;
4017 }
4018 }
4019 else
4020 ++child_idx;
4021 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004022
4023 const uint32_t superclass_idx = child_idx;
Greg Clayton9e409562010-07-28 02:04:09 +00004024
4025 if (idx < (child_idx + class_interface_decl->ivar_size()))
4026 {
4027 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4028
4029 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
4030 {
4031 if (child_idx == idx)
4032 {
Jim Inghamf80bc3f2011-12-08 02:53:10 +00004033 ObjCIvarDecl* ivar_decl = *ivar_pos;
Greg Clayton9e409562010-07-28 02:04:09 +00004034
4035 QualType ivar_qual_type(ivar_decl->getType());
4036
4037 child_name.assign(ivar_decl->getNameAsString().c_str());
4038
Greg Clayton6beaaa62011-01-17 03:46:26 +00004039 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004040
4041 child_byte_size = ivar_type_info.first / 8;
4042
4043 // Figure out the field offset within the current struct/union/class type
Jim Inghamd555bac2011-06-24 22:03:24 +00004044 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
4045 // that doesn't account for the space taken up by unbacked properties, or from
4046 // the changing size of base classes that are newer than this class.
4047 // So if we have a process around that we can ask about this object, do so.
4048 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
Greg Claytonc14ee322011-09-22 04:58:26 +00004049 Process *process = NULL;
4050 if (exe_ctx)
4051 process = exe_ctx->GetProcessPtr();
4052 if (process)
Jim Inghamd555bac2011-06-24 22:03:24 +00004053 {
Greg Claytonc14ee322011-09-22 04:58:26 +00004054 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
Jim Inghamd555bac2011-06-24 22:03:24 +00004055 if (objc_runtime != NULL)
4056 {
Enrico Granata6f3533f2011-07-29 19:53:35 +00004057 ClangASTType parent_ast_type (ast, parent_qual_type.getAsOpaquePtr());
Jim Inghamd555bac2011-06-24 22:03:24 +00004058 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
4059 }
4060 }
4061
Jim Inghamf80bc3f2011-12-08 02:53:10 +00004062 // Setting this to UINT32_MAX to make sure we don't compute it twice...
4063 bit_offset = UINT32_MAX;
4064
Jim Inghamd555bac2011-06-24 22:03:24 +00004065 if (child_byte_offset == LLDB_INVALID_IVAR_OFFSET)
4066 {
4067 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
4068 child_byte_offset = bit_offset / 8;
4069 }
Jim Inghamf80bc3f2011-12-08 02:53:10 +00004070
4071 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
4072 // of a bitfield within its containing object. So regardless of where we get the byte
4073 // offset from, we still need to get the bit offset for bitfields from the layout.
4074
4075 if (ClangASTContext::FieldIsBitfield (ast, ivar_decl, child_bitfield_bit_size))
4076 {
4077 if (bit_offset == UINT32_MAX)
4078 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
4079
4080 child_bitfield_bit_offset = bit_offset % 8;
4081 }
Greg Clayton9e409562010-07-28 02:04:09 +00004082 return ivar_qual_type.getAsOpaquePtr();
4083 }
4084 ++child_idx;
4085 }
4086 }
4087 }
4088 }
4089 }
4090 break;
4091
4092 case clang::Type::ObjCObjectPointer:
4093 {
Sean Callanan78e37602011-01-27 04:42:51 +00004094 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004095 QualType pointee_type = pointer_type->getPointeeType();
4096
4097 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4098 {
Greg Claytone221f822011-01-21 01:59:00 +00004099 child_is_deref_of_parent = false;
4100 bool tmp_child_is_deref_of_parent = false;
Jim Inghamd555bac2011-06-24 22:03:24 +00004101 return GetChildClangTypeAtIndex (exe_ctx,
4102 ast,
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004103 parent_name,
4104 pointer_type->getPointeeType().getAsOpaquePtr(),
4105 idx,
4106 transparent_pointers,
4107 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004108 ignore_array_bounds,
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004109 child_name,
4110 child_byte_size,
4111 child_byte_offset,
4112 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00004113 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004114 child_is_base_class,
4115 tmp_child_is_deref_of_parent);
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004116 }
4117 else
4118 {
Greg Claytone221f822011-01-21 01:59:00 +00004119 child_is_deref_of_parent = true;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004120 if (parent_name)
4121 {
4122 child_name.assign(1, '*');
4123 child_name += parent_name;
4124 }
4125
4126 // We have a pointer to an simple type
Sean Callanan5b26f272012-02-04 08:49:35 +00004127 if (idx == 0 && GetCompleteQualType(ast, pointee_type))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004128 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004129 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004130 assert(clang_type_info.first % 8 == 0);
4131 child_byte_size = clang_type_info.first / 8;
4132 child_byte_offset = 0;
4133 return pointee_type.getAsOpaquePtr();
4134 }
4135 }
Greg Clayton9e409562010-07-28 02:04:09 +00004136 }
4137 break;
4138
Greg Claytone1a916a2010-07-21 22:12:05 +00004139 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004140 {
4141 const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4142 const uint64_t element_count = array->getSize().getLimitedValue();
4143
Greg Claytondaf515f2011-07-09 20:12:33 +00004144 if (ignore_array_bounds || idx < element_count)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004145 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004146 if (GetCompleteQualType (ast, array->getElementType()))
4147 {
4148 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004149
Greg Clayton6beaaa62011-01-17 03:46:26 +00004150 char element_name[64];
4151 ::snprintf (element_name, sizeof (element_name), "[%u]", idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004152
Greg Clayton6beaaa62011-01-17 03:46:26 +00004153 child_name.assign(element_name);
4154 assert(field_type_info.first % 8 == 0);
4155 child_byte_size = field_type_info.first / 8;
Greg Claytondaf515f2011-07-09 20:12:33 +00004156 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
Greg Clayton6beaaa62011-01-17 03:46:26 +00004157 return array->getElementType().getAsOpaquePtr();
4158 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004159 }
4160 }
4161 break;
4162
Greg Claytone1a916a2010-07-21 22:12:05 +00004163 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004164 {
Sean Callanan78e37602011-01-27 04:42:51 +00004165 const PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004166 QualType pointee_type = pointer_type->getPointeeType();
Greg Clayton97a43712011-01-08 22:26:47 +00004167
4168 // Don't dereference "void *" pointers
4169 if (pointee_type->isVoidType())
4170 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004171
4172 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4173 {
Greg Claytone221f822011-01-21 01:59:00 +00004174 child_is_deref_of_parent = false;
4175 bool tmp_child_is_deref_of_parent = false;
Jim Inghamd555bac2011-06-24 22:03:24 +00004176 return GetChildClangTypeAtIndex (exe_ctx,
4177 ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004178 parent_name,
4179 pointer_type->getPointeeType().getAsOpaquePtr(),
4180 idx,
4181 transparent_pointers,
4182 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004183 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004184 child_name,
4185 child_byte_size,
4186 child_byte_offset,
4187 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00004188 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004189 child_is_base_class,
4190 tmp_child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004191 }
4192 else
4193 {
Greg Claytone221f822011-01-21 01:59:00 +00004194 child_is_deref_of_parent = true;
4195
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004196 if (parent_name)
4197 {
4198 child_name.assign(1, '*');
4199 child_name += parent_name;
4200 }
4201
4202 // We have a pointer to an simple type
4203 if (idx == 0)
4204 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004205 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004206 assert(clang_type_info.first % 8 == 0);
4207 child_byte_size = clang_type_info.first / 8;
4208 child_byte_offset = 0;
4209 return pointee_type.getAsOpaquePtr();
4210 }
4211 }
4212 }
4213 break;
4214
Greg Clayton73b472d2010-10-27 03:32:59 +00004215 case clang::Type::LValueReference:
4216 case clang::Type::RValueReference:
4217 {
Sean Callanan78e37602011-01-27 04:42:51 +00004218 const ReferenceType *reference_type = cast<ReferenceType>(parent_qual_type.getTypePtr());
Greg Clayton73b472d2010-10-27 03:32:59 +00004219 QualType pointee_type(reference_type->getPointeeType());
4220 clang_type_t pointee_clang_type = pointee_type.getAsOpaquePtr();
4221 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_clang_type))
4222 {
Greg Claytone221f822011-01-21 01:59:00 +00004223 child_is_deref_of_parent = false;
4224 bool tmp_child_is_deref_of_parent = false;
Jim Inghamd555bac2011-06-24 22:03:24 +00004225 return GetChildClangTypeAtIndex (exe_ctx,
4226 ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00004227 parent_name,
4228 pointee_clang_type,
4229 idx,
4230 transparent_pointers,
4231 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004232 ignore_array_bounds,
Greg Clayton73b472d2010-10-27 03:32:59 +00004233 child_name,
4234 child_byte_size,
4235 child_byte_offset,
4236 child_bitfield_bit_size,
4237 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004238 child_is_base_class,
4239 tmp_child_is_deref_of_parent);
Greg Clayton73b472d2010-10-27 03:32:59 +00004240 }
4241 else
4242 {
4243 if (parent_name)
4244 {
4245 child_name.assign(1, '&');
4246 child_name += parent_name;
4247 }
4248
4249 // We have a pointer to an simple type
4250 if (idx == 0)
4251 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004252 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Greg Clayton73b472d2010-10-27 03:32:59 +00004253 assert(clang_type_info.first % 8 == 0);
4254 child_byte_size = clang_type_info.first / 8;
4255 child_byte_offset = 0;
4256 return pointee_type.getAsOpaquePtr();
4257 }
4258 }
4259 }
4260 break;
4261
Greg Claytone1a916a2010-07-21 22:12:05 +00004262 case clang::Type::Typedef:
Jim Inghamd555bac2011-06-24 22:03:24 +00004263 return GetChildClangTypeAtIndex (exe_ctx,
4264 ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004265 parent_name,
Sean Callanan48114472010-12-13 01:26:27 +00004266 cast<TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004267 idx,
4268 transparent_pointers,
4269 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004270 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004271 child_name,
4272 child_byte_size,
4273 child_byte_offset,
4274 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00004275 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004276 child_is_base_class,
4277 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004278 break;
Sean Callanan912855f2011-08-11 23:56:13 +00004279
4280 case clang::Type::Elaborated:
4281 return GetChildClangTypeAtIndex (exe_ctx,
4282 ast,
4283 parent_name,
4284 cast<ElaboratedType>(parent_qual_type)->getNamedType().getAsOpaquePtr(),
4285 idx,
4286 transparent_pointers,
4287 omit_empty_base_classes,
4288 ignore_array_bounds,
4289 child_name,
4290 child_byte_size,
4291 child_byte_offset,
4292 child_bitfield_bit_size,
4293 child_bitfield_bit_offset,
4294 child_is_base_class,
4295 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004296
4297 default:
4298 break;
4299 }
4300 }
Greg Clayton19503a22010-07-23 15:37:46 +00004301 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004302}
4303
4304static inline bool
4305BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
4306{
Greg Clayton6beaaa62011-01-17 03:46:26 +00004307 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004308}
4309
4310static uint32_t
4311GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
4312{
4313 uint32_t num_bases = 0;
4314 if (cxx_record_decl)
4315 {
4316 if (omit_empty_base_classes)
4317 {
4318 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4319 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4320 base_class != base_class_end;
4321 ++base_class)
4322 {
4323 // Skip empty base classes
4324 if (omit_empty_base_classes)
4325 {
4326 if (BaseSpecifierIsEmpty (base_class))
4327 continue;
4328 }
4329 ++num_bases;
4330 }
4331 }
4332 else
4333 num_bases = cxx_record_decl->getNumBases();
4334 }
4335 return num_bases;
4336}
4337
4338
4339static uint32_t
4340GetIndexForRecordBase
4341(
4342 const RecordDecl *record_decl,
4343 const CXXBaseSpecifier *base_spec,
4344 bool omit_empty_base_classes
4345)
4346{
4347 uint32_t child_idx = 0;
4348
4349 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4350
4351// const char *super_name = record_decl->getNameAsCString();
4352// const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString();
4353// printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
4354//
4355 if (cxx_record_decl)
4356 {
4357 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4358 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4359 base_class != base_class_end;
4360 ++base_class)
4361 {
4362 if (omit_empty_base_classes)
4363 {
4364 if (BaseSpecifierIsEmpty (base_class))
4365 continue;
4366 }
4367
4368// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
4369// child_idx,
4370// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4371//
4372//
4373 if (base_class == base_spec)
4374 return child_idx;
4375 ++child_idx;
4376 }
4377 }
4378
4379 return UINT32_MAX;
4380}
4381
4382
4383static uint32_t
4384GetIndexForRecordChild
4385(
4386 const RecordDecl *record_decl,
4387 NamedDecl *canonical_decl,
4388 bool omit_empty_base_classes
4389)
4390{
4391 uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes);
4392
4393// const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4394//
4395//// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString());
4396// if (cxx_record_decl)
4397// {
4398// CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4399// for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4400// base_class != base_class_end;
4401// ++base_class)
4402// {
4403// if (omit_empty_base_classes)
4404// {
4405// if (BaseSpecifierIsEmpty (base_class))
4406// continue;
4407// }
4408//
4409//// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
4410//// record_decl->getNameAsCString(),
4411//// canonical_decl->getNameAsCString(),
4412//// child_idx,
4413//// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4414//
4415//
4416// CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4417// if (curr_base_class_decl == canonical_decl)
4418// {
4419// return child_idx;
4420// }
4421// ++child_idx;
4422// }
4423// }
4424//
4425// const uint32_t num_bases = child_idx;
4426 RecordDecl::field_iterator field, field_end;
4427 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4428 field != field_end;
4429 ++field, ++child_idx)
4430 {
4431// printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n",
4432// record_decl->getNameAsCString(),
4433// canonical_decl->getNameAsCString(),
4434// child_idx - num_bases,
4435// field->getNameAsCString());
4436
4437 if (field->getCanonicalDecl() == canonical_decl)
4438 return child_idx;
4439 }
4440
4441 return UINT32_MAX;
4442}
4443
4444// Look for a child member (doesn't include base classes, but it does include
4445// their members) in the type hierarchy. Returns an index path into "clang_type"
4446// on how to reach the appropriate member.
4447//
4448// class A
4449// {
4450// public:
4451// int m_a;
4452// int m_b;
4453// };
4454//
4455// class B
4456// {
4457// };
4458//
4459// class C :
4460// public B,
4461// public A
4462// {
4463// };
4464//
4465// If we have a clang type that describes "class C", and we wanted to looked
4466// "m_b" in it:
4467//
4468// With omit_empty_base_classes == false we would get an integer array back with:
4469// { 1, 1 }
4470// The first index 1 is the child index for "class A" within class C
4471// The second index 1 is the child index for "m_b" within class A
4472//
4473// With omit_empty_base_classes == true we would get an integer array back with:
4474// { 0, 1 }
4475// 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)
4476// The second index 1 is the child index for "m_b" within class A
4477
4478size_t
4479ClangASTContext::GetIndexOfChildMemberWithName
4480(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004481 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004482 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004483 const char *name,
4484 bool omit_empty_base_classes,
4485 std::vector<uint32_t>& child_indexes
4486)
4487{
4488 if (clang_type && name && name[0])
4489 {
4490 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00004491 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4492 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004493 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004494 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004495 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004496 {
4497 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4498 const RecordDecl *record_decl = record_type->getDecl();
4499
4500 assert(record_decl);
4501 uint32_t child_idx = 0;
4502
4503 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4504
4505 // Try and find a field that matches NAME
4506 RecordDecl::field_iterator field, field_end;
4507 StringRef name_sref(name);
4508 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4509 field != field_end;
4510 ++field, ++child_idx)
4511 {
4512 if (field->getName().equals (name_sref))
4513 {
4514 // We have to add on the number of base classes to this index!
4515 child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
4516 return child_indexes.size();
4517 }
4518 }
4519
4520 if (cxx_record_decl)
4521 {
4522 const RecordDecl *parent_record_decl = cxx_record_decl;
4523
4524 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
4525
4526 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
4527 // Didn't find things easily, lets let clang do its thang...
Sean Callanancc427fa2011-07-30 02:42:06 +00004528 IdentifierInfo & ident_ref = ast->Idents.get(name_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004529 DeclarationName decl_name(&ident_ref);
4530
4531 CXXBasePaths paths;
4532 if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember,
4533 decl_name.getAsOpaquePtr(),
4534 paths))
4535 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004536 CXXBasePaths::const_paths_iterator path, path_end = paths.end();
4537 for (path = paths.begin(); path != path_end; ++path)
4538 {
4539 const size_t num_path_elements = path->size();
4540 for (size_t e=0; e<num_path_elements; ++e)
4541 {
4542 CXXBasePathElement elem = (*path)[e];
4543
4544 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
4545 if (child_idx == UINT32_MAX)
4546 {
4547 child_indexes.clear();
4548 return 0;
4549 }
4550 else
4551 {
4552 child_indexes.push_back (child_idx);
4553 parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl());
4554 }
4555 }
4556 DeclContext::lookup_iterator named_decl_pos;
4557 for (named_decl_pos = path->Decls.first;
4558 named_decl_pos != path->Decls.second && parent_record_decl;
4559 ++named_decl_pos)
4560 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004561 child_idx = GetIndexForRecordChild (parent_record_decl, *named_decl_pos, omit_empty_base_classes);
4562 if (child_idx == UINT32_MAX)
4563 {
4564 child_indexes.clear();
4565 return 0;
4566 }
4567 else
4568 {
4569 child_indexes.push_back (child_idx);
4570 }
4571 }
4572 }
4573 return child_indexes.size();
4574 }
4575 }
4576
4577 }
4578 break;
4579
Greg Clayton9e409562010-07-28 02:04:09 +00004580 case clang::Type::ObjCObject:
4581 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004582 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004583 {
4584 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004585 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004586 assert (objc_class_type);
4587 if (objc_class_type)
4588 {
4589 uint32_t child_idx = 0;
4590 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4591
4592 if (class_interface_decl)
4593 {
4594 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4595 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4596
Greg Clayton6ba78152010-09-18 02:11:07 +00004597 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00004598 {
4599 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4600
4601 if (ivar_decl->getName().equals (name_sref))
4602 {
4603 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4604 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4605 ++child_idx;
4606
4607 child_indexes.push_back (child_idx);
4608 return child_indexes.size();
4609 }
4610 }
4611
4612 if (superclass_interface_decl)
4613 {
4614 // The super class index is always zero for ObjC classes,
4615 // so we push it onto the child indexes in case we find
4616 // an ivar in our superclass...
4617 child_indexes.push_back (0);
4618
Greg Clayton6beaaa62011-01-17 03:46:26 +00004619 if (GetIndexOfChildMemberWithName (ast,
4620 ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00004621 name,
4622 omit_empty_base_classes,
4623 child_indexes))
4624 {
4625 // We did find an ivar in a superclass so just
4626 // return the results!
4627 return child_indexes.size();
4628 }
4629
4630 // We didn't find an ivar matching "name" in our
4631 // superclass, pop the superclass zero index that
4632 // we pushed on above.
4633 child_indexes.pop_back();
4634 }
4635 }
4636 }
4637 }
4638 break;
4639
4640 case clang::Type::ObjCObjectPointer:
4641 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004642 return GetIndexOfChildMemberWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004643 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4644 name,
4645 omit_empty_base_classes,
4646 child_indexes);
4647 }
4648 break;
4649
4650
Greg Claytone1a916a2010-07-21 22:12:05 +00004651 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004652 {
4653// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4654// const uint64_t element_count = array->getSize().getLimitedValue();
4655//
4656// if (idx < element_count)
4657// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004658// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004659//
4660// char element_name[32];
4661// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4662//
4663// child_name.assign(element_name);
4664// assert(field_type_info.first % 8 == 0);
4665// child_byte_size = field_type_info.first / 8;
4666// child_byte_offset = idx * child_byte_size;
4667// return array->getElementType().getAsOpaquePtr();
4668// }
4669 }
4670 break;
4671
Greg Claytone1a916a2010-07-21 22:12:05 +00004672// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004673// {
4674// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4675// QualType pointee_type = mem_ptr_type->getPointeeType();
4676//
4677// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4678// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004679// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004680// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4681// name);
4682// }
4683// }
4684// break;
4685//
Greg Claytone1a916a2010-07-21 22:12:05 +00004686 case clang::Type::LValueReference:
4687 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004688 {
Sean Callanan78e37602011-01-27 04:42:51 +00004689 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004690 QualType pointee_type = reference_type->getPointeeType();
4691
4692 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4693 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004694 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004695 reference_type->getPointeeType().getAsOpaquePtr(),
4696 name,
4697 omit_empty_base_classes,
4698 child_indexes);
4699 }
4700 }
4701 break;
4702
Greg Claytone1a916a2010-07-21 22:12:05 +00004703 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004704 {
Sean Callanan78e37602011-01-27 04:42:51 +00004705 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004706 QualType pointee_type = pointer_type->getPointeeType();
4707
4708 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4709 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004710 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004711 pointer_type->getPointeeType().getAsOpaquePtr(),
4712 name,
4713 omit_empty_base_classes,
4714 child_indexes);
4715 }
4716 else
4717 {
4718// if (parent_name)
4719// {
4720// child_name.assign(1, '*');
4721// child_name += parent_name;
4722// }
4723//
4724// // We have a pointer to an simple type
4725// if (idx == 0)
4726// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004727// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004728// assert(clang_type_info.first % 8 == 0);
4729// child_byte_size = clang_type_info.first / 8;
4730// child_byte_offset = 0;
4731// return pointee_type.getAsOpaquePtr();
4732// }
4733 }
4734 }
4735 break;
4736
Greg Claytone1a916a2010-07-21 22:12:05 +00004737 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00004738 return GetIndexOfChildMemberWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00004739 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004740 name,
4741 omit_empty_base_classes,
4742 child_indexes);
4743
4744 default:
4745 break;
4746 }
4747 }
4748 return 0;
4749}
4750
4751
4752// Get the index of the child of "clang_type" whose name matches. This function
4753// doesn't descend into the children, but only looks one level deep and name
4754// matches can include base class names.
4755
4756uint32_t
4757ClangASTContext::GetIndexOfChildWithName
4758(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004759 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004760 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004761 const char *name,
4762 bool omit_empty_base_classes
4763)
4764{
4765 if (clang_type && name && name[0])
4766 {
4767 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00004768
Greg Clayton737b9322010-09-13 03:32:57 +00004769 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Clayton9e409562010-07-28 02:04:09 +00004770
Greg Clayton737b9322010-09-13 03:32:57 +00004771 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004772 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004773 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004774 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004775 {
4776 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4777 const RecordDecl *record_decl = record_type->getDecl();
4778
4779 assert(record_decl);
4780 uint32_t child_idx = 0;
4781
4782 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4783
4784 if (cxx_record_decl)
4785 {
4786 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4787 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4788 base_class != base_class_end;
4789 ++base_class)
4790 {
4791 // Skip empty base classes
4792 CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4793 if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false)
4794 continue;
4795
Greg Clayton84db9102012-03-26 23:03:23 +00004796 std::string base_class_type_name (ClangASTType::GetTypeNameForQualType(ast, base_class->getType()));
Greg Claytone3055942011-06-30 02:28:26 +00004797 if (base_class_type_name.compare (name) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004798 return child_idx;
4799 ++child_idx;
4800 }
4801 }
4802
4803 // Try and find a field that matches NAME
4804 RecordDecl::field_iterator field, field_end;
4805 StringRef name_sref(name);
4806 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4807 field != field_end;
4808 ++field, ++child_idx)
4809 {
4810 if (field->getName().equals (name_sref))
4811 return child_idx;
4812 }
4813
4814 }
4815 break;
4816
Greg Clayton9e409562010-07-28 02:04:09 +00004817 case clang::Type::ObjCObject:
4818 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004819 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004820 {
4821 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004822 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004823 assert (objc_class_type);
4824 if (objc_class_type)
4825 {
4826 uint32_t child_idx = 0;
4827 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4828
4829 if (class_interface_decl)
4830 {
4831 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4832 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4833
Jim Ingham2f355a72012-10-04 22:22:16 +00004834 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00004835 {
4836 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4837
4838 if (ivar_decl->getName().equals (name_sref))
4839 {
4840 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4841 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4842 ++child_idx;
4843
4844 return child_idx;
4845 }
4846 }
4847
4848 if (superclass_interface_decl)
4849 {
4850 if (superclass_interface_decl->getName().equals (name_sref))
4851 return 0;
4852 }
4853 }
4854 }
4855 }
4856 break;
4857
4858 case clang::Type::ObjCObjectPointer:
4859 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004860 return GetIndexOfChildWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004861 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4862 name,
4863 omit_empty_base_classes);
4864 }
4865 break;
4866
Greg Claytone1a916a2010-07-21 22:12:05 +00004867 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004868 {
4869// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4870// const uint64_t element_count = array->getSize().getLimitedValue();
4871//
4872// if (idx < element_count)
4873// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004874// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004875//
4876// char element_name[32];
4877// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4878//
4879// child_name.assign(element_name);
4880// assert(field_type_info.first % 8 == 0);
4881// child_byte_size = field_type_info.first / 8;
4882// child_byte_offset = idx * child_byte_size;
4883// return array->getElementType().getAsOpaquePtr();
4884// }
4885 }
4886 break;
4887
Greg Claytone1a916a2010-07-21 22:12:05 +00004888// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004889// {
4890// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4891// QualType pointee_type = mem_ptr_type->getPointeeType();
4892//
4893// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4894// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004895// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004896// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4897// name);
4898// }
4899// }
4900// break;
4901//
Greg Claytone1a916a2010-07-21 22:12:05 +00004902 case clang::Type::LValueReference:
4903 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004904 {
Sean Callanan78e37602011-01-27 04:42:51 +00004905 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004906 QualType pointee_type = reference_type->getPointeeType();
4907
4908 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4909 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004910 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004911 reference_type->getPointeeType().getAsOpaquePtr(),
4912 name,
4913 omit_empty_base_classes);
4914 }
4915 }
4916 break;
4917
Greg Claytone1a916a2010-07-21 22:12:05 +00004918 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004919 {
Sean Callanan78e37602011-01-27 04:42:51 +00004920 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004921 QualType pointee_type = pointer_type->getPointeeType();
4922
4923 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4924 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004925 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004926 pointer_type->getPointeeType().getAsOpaquePtr(),
4927 name,
4928 omit_empty_base_classes);
4929 }
4930 else
4931 {
4932// if (parent_name)
4933// {
4934// child_name.assign(1, '*');
4935// child_name += parent_name;
4936// }
4937//
4938// // We have a pointer to an simple type
4939// if (idx == 0)
4940// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004941// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004942// assert(clang_type_info.first % 8 == 0);
4943// child_byte_size = clang_type_info.first / 8;
4944// child_byte_offset = 0;
4945// return pointee_type.getAsOpaquePtr();
4946// }
4947 }
4948 }
4949 break;
4950
Greg Claytone1a916a2010-07-21 22:12:05 +00004951 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00004952 return GetIndexOfChildWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00004953 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004954 name,
4955 omit_empty_base_classes);
4956
4957 default:
4958 break;
4959 }
4960 }
4961 return UINT32_MAX;
4962}
4963
4964#pragma mark TagType
4965
4966bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00004967ClangASTContext::SetTagTypeKind (clang_type_t tag_clang_type, int kind)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004968{
4969 if (tag_clang_type)
4970 {
4971 QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00004972 const clang::Type *clang_type = tag_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004973 if (clang_type)
4974 {
Sean Callanan78e37602011-01-27 04:42:51 +00004975 const TagType *tag_type = dyn_cast<TagType>(clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004976 if (tag_type)
4977 {
4978 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl());
4979 if (tag_decl)
4980 {
4981 tag_decl->setTagKind ((TagDecl::TagKind)kind);
4982 return true;
4983 }
4984 }
4985 }
4986 }
4987 return false;
4988}
4989
4990
4991#pragma mark DeclContext Functions
4992
4993DeclContext *
Greg Clayton1be10fc2010-09-29 01:12:09 +00004994ClangASTContext::GetDeclContextForType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004995{
4996 if (clang_type == NULL)
4997 return NULL;
4998
4999 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005000 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5001 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005002 {
Sean Callanancc427fa2011-07-30 02:42:06 +00005003 case clang::Type::UnaryTransform: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005004 case clang::Type::FunctionNoProto: break;
5005 case clang::Type::FunctionProto: break;
5006 case clang::Type::IncompleteArray: break;
5007 case clang::Type::VariableArray: break;
5008 case clang::Type::ConstantArray: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005009 case clang::Type::DependentSizedArray: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005010 case clang::Type::ExtVector: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005011 case clang::Type::DependentSizedExtVector: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005012 case clang::Type::Vector: break;
5013 case clang::Type::Builtin: break;
5014 case clang::Type::BlockPointer: break;
5015 case clang::Type::Pointer: break;
5016 case clang::Type::LValueReference: break;
5017 case clang::Type::RValueReference: break;
5018 case clang::Type::MemberPointer: break;
5019 case clang::Type::Complex: break;
5020 case clang::Type::ObjCObject: break;
5021 case clang::Type::ObjCInterface: return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface();
5022 case clang::Type::ObjCObjectPointer: return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr());
5023 case clang::Type::Record: return cast<RecordType>(qual_type)->getDecl();
5024 case clang::Type::Enum: return cast<EnumType>(qual_type)->getDecl();
Sean Callanan48114472010-12-13 01:26:27 +00005025 case clang::Type::Typedef: return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00005026 case clang::Type::Elaborated: return ClangASTContext::GetDeclContextForType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00005027 case clang::Type::TypeOfExpr: break;
5028 case clang::Type::TypeOf: break;
5029 case clang::Type::Decltype: break;
5030 //case clang::Type::QualifiedName: break;
5031 case clang::Type::TemplateSpecialization: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005032 case clang::Type::DependentTemplateSpecialization: break;
5033 case clang::Type::TemplateTypeParm: break;
5034 case clang::Type::SubstTemplateTypeParm: break;
5035 case clang::Type::SubstTemplateTypeParmPack:break;
5036 case clang::Type::PackExpansion: break;
5037 case clang::Type::UnresolvedUsing: break;
5038 case clang::Type::Paren: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005039 case clang::Type::Attributed: break;
5040 case clang::Type::Auto: break;
5041 case clang::Type::InjectedClassName: break;
5042 case clang::Type::DependentName: break;
Greg Claytonea3e7d52011-10-08 00:49:15 +00005043 case clang::Type::Atomic: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005044 }
5045 // No DeclContext in this type...
5046 return NULL;
5047}
5048
5049#pragma mark Namespace Declarations
5050
5051NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00005052ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005053{
Greg Clayton030a2042011-10-14 21:34:45 +00005054 NamespaceDecl *namespace_decl = NULL;
Greg Clayton9d3d6882011-10-31 23:51:19 +00005055 ASTContext *ast = getASTContext();
5056 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
5057 if (decl_ctx == NULL)
5058 decl_ctx = translation_unit_decl;
5059
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005060 if (name)
5061 {
Greg Clayton030a2042011-10-14 21:34:45 +00005062 IdentifierInfo &identifier_info = ast->Idents.get(name);
5063 DeclarationName decl_name (&identifier_info);
5064 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
5065 for (clang::DeclContext::lookup_iterator pos = result.first, end = result.second; pos != end; ++pos)
5066 {
5067 namespace_decl = dyn_cast<clang::NamespaceDecl>(*pos);
5068 if (namespace_decl)
5069 return namespace_decl;
5070 }
5071
Sean Callanan5b26f272012-02-04 08:49:35 +00005072 namespace_decl = NamespaceDecl::Create(*ast,
5073 decl_ctx,
5074 false,
5075 SourceLocation(),
5076 SourceLocation(),
5077 &identifier_info,
5078 NULL);
Greg Clayton030a2042011-10-14 21:34:45 +00005079
Greg Clayton9d3d6882011-10-31 23:51:19 +00005080 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005081 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00005082 else
5083 {
5084 if (decl_ctx == translation_unit_decl)
5085 {
5086 namespace_decl = translation_unit_decl->getAnonymousNamespace();
5087 if (namespace_decl)
5088 return namespace_decl;
5089
Sean Callanan5b26f272012-02-04 08:49:35 +00005090 namespace_decl = NamespaceDecl::Create(*ast,
5091 decl_ctx,
5092 false,
5093 SourceLocation(),
5094 SourceLocation(),
5095 NULL,
5096 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00005097 translation_unit_decl->setAnonymousNamespace (namespace_decl);
5098 translation_unit_decl->addDecl (namespace_decl);
5099 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
5100 }
5101 else
5102 {
5103 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
5104 if (parent_namespace_decl)
5105 {
5106 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
5107 if (namespace_decl)
5108 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00005109 namespace_decl = NamespaceDecl::Create(*ast,
5110 decl_ctx,
5111 false,
5112 SourceLocation(),
5113 SourceLocation(),
5114 NULL,
5115 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00005116 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
5117 parent_namespace_decl->addDecl (namespace_decl);
5118 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
5119 }
5120 else
5121 {
5122 // BAD!!!
5123 }
5124 }
5125
5126
5127 if (namespace_decl)
5128 {
5129 // If we make it here, we are creating the anonymous namespace decl
5130 // for the first time, so we need to do the using directive magic
5131 // like SEMA does
5132 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast,
5133 decl_ctx,
5134 SourceLocation(),
5135 SourceLocation(),
5136 NestedNameSpecifierLoc(),
5137 SourceLocation(),
5138 namespace_decl,
5139 decl_ctx);
5140 using_directive_decl->setImplicit();
5141 decl_ctx->addDecl(using_directive_decl);
5142 }
5143 }
5144#ifdef LLDB_CONFIGURATION_DEBUG
5145 VerifyDecl(namespace_decl);
5146#endif
Greg Clayton030a2042011-10-14 21:34:45 +00005147 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005148}
5149
5150
5151#pragma mark Function Types
5152
5153FunctionDecl *
Greg Clayton147e1fa2011-10-14 22:47:18 +00005154ClangASTContext::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 +00005155{
Greg Clayton147e1fa2011-10-14 22:47:18 +00005156 FunctionDecl *func_decl = NULL;
5157 ASTContext *ast = getASTContext();
5158 if (decl_ctx == NULL)
5159 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005160
Greg Clayton147e1fa2011-10-14 22:47:18 +00005161 if (name && name[0])
5162 {
5163 func_decl = FunctionDecl::Create (*ast,
5164 decl_ctx,
5165 SourceLocation(),
5166 SourceLocation(),
5167 DeclarationName (&ast->Idents.get(name)),
5168 QualType::getFromOpaquePtr(function_clang_type),
5169 NULL,
5170 (FunctionDecl::StorageClass)storage,
5171 (FunctionDecl::StorageClass)storage,
5172 is_inline);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005173 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00005174 else
5175 {
5176 func_decl = FunctionDecl::Create (*ast,
5177 decl_ctx,
5178 SourceLocation(),
5179 SourceLocation(),
5180 DeclarationName (),
5181 QualType::getFromOpaquePtr(function_clang_type),
5182 NULL,
5183 (FunctionDecl::StorageClass)storage,
5184 (FunctionDecl::StorageClass)storage,
5185 is_inline);
5186 }
5187 if (func_decl)
5188 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005189
5190#ifdef LLDB_CONFIGURATION_DEBUG
5191 VerifyDecl(func_decl);
5192#endif
5193
Greg Clayton147e1fa2011-10-14 22:47:18 +00005194 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005195}
5196
Greg Clayton1be10fc2010-09-29 01:12:09 +00005197clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00005198ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00005199 clang_type_t result_type,
5200 clang_type_t *args,
Sean Callananc81256a2010-09-16 20:40:25 +00005201 unsigned num_args,
5202 bool is_variadic,
5203 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005204{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005205 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005206 std::vector<QualType> qual_type_args;
5207 for (unsigned i=0; i<num_args; ++i)
5208 qual_type_args.push_back (QualType::getFromOpaquePtr(args[i]));
5209
5210 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00005211 FunctionProtoType::ExtProtoInfo proto_info;
5212 proto_info.Variadic = is_variadic;
Sean Callananfb0b7582011-03-15 00:17:19 +00005213 proto_info.ExceptionSpecType = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005214 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00005215 proto_info.RefQualifier = RQ_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005216 proto_info.NumExceptions = 0;
5217 proto_info.Exceptions = NULL;
5218
Greg Clayton147e1fa2011-10-14 22:47:18 +00005219 return ast->getFunctionType (QualType::getFromOpaquePtr(result_type),
5220 qual_type_args.empty() ? NULL : &qual_type_args.front(),
5221 qual_type_args.size(),
5222 proto_info).getAsOpaquePtr(); // NoReturn);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005223}
5224
5225ParmVarDecl *
Greg Clayton1be10fc2010-09-29 01:12:09 +00005226ClangASTContext::CreateParameterDeclaration (const char *name, clang_type_t param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005227{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005228 ASTContext *ast = getASTContext();
5229 assert (ast != NULL);
5230 return ParmVarDecl::Create(*ast,
5231 ast->getTranslationUnitDecl(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005232 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005233 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00005234 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callananc81256a2010-09-16 20:40:25 +00005235 QualType::getFromOpaquePtr(param_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005236 NULL,
5237 (VarDecl::StorageClass)storage,
5238 (VarDecl::StorageClass)storage,
5239 0);
5240}
5241
5242void
5243ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
5244{
5245 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00005246 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005247}
5248
5249
5250#pragma mark Array Types
5251
Greg Clayton1be10fc2010-09-29 01:12:09 +00005252clang_type_t
5253ClangASTContext::CreateArrayType (clang_type_t element_type, size_t element_count, uint32_t bit_stride)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005254{
5255 if (element_type)
5256 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00005257 ASTContext *ast = getASTContext();
5258 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005259 llvm::APInt ap_element_count (64, element_count);
Greg Clayton6beaaa62011-01-17 03:46:26 +00005260 return ast->getConstantArrayType(QualType::getFromOpaquePtr(element_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005261 ap_element_count,
5262 ArrayType::Normal,
5263 0).getAsOpaquePtr(); // ElemQuals
5264 }
5265 return NULL;
5266}
5267
5268
5269#pragma mark TagDecl
5270
5271bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005272ClangASTContext::StartTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005273{
5274 if (clang_type)
5275 {
5276 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00005277 const clang::Type *t = qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005278 if (t)
5279 {
Sean Callanan78e37602011-01-27 04:42:51 +00005280 const TagType *tag_type = dyn_cast<TagType>(t);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005281 if (tag_type)
5282 {
5283 TagDecl *tag_decl = tag_type->getDecl();
5284 if (tag_decl)
5285 {
5286 tag_decl->startDefinition();
5287 return true;
5288 }
5289 }
Sean Callanan5b26f272012-02-04 08:49:35 +00005290
5291 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(t);
5292 if (object_type)
5293 {
5294 ObjCInterfaceDecl *interface_decl = object_type->getInterface();
5295 if (interface_decl)
5296 {
5297 interface_decl->startDefinition();
5298 return true;
5299 }
5300 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005301 }
5302 }
5303 return false;
5304}
5305
5306bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005307ClangASTContext::CompleteTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005308{
5309 if (clang_type)
5310 {
5311 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton14372242010-09-29 03:44:17 +00005312
5313 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5314
5315 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005316 {
Greg Clayton14372242010-09-29 03:44:17 +00005317 cxx_record_decl->completeDefinition();
5318
5319 return true;
5320 }
5321
5322 const EnumType *enum_type = dyn_cast<EnumType>(qual_type.getTypePtr());
5323
5324 if (enum_type)
5325 {
5326 EnumDecl *enum_decl = enum_type->getDecl();
5327
5328 if (enum_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005329 {
Greg Clayton14372242010-09-29 03:44:17 +00005330 /// TODO This really needs to be fixed.
5331
5332 unsigned NumPositiveBits = 1;
5333 unsigned NumNegativeBits = 0;
5334
Greg Clayton6beaaa62011-01-17 03:46:26 +00005335 ASTContext *ast = getASTContext();
Greg Claytone02b8502010-10-12 04:29:14 +00005336
5337 QualType promotion_qual_type;
5338 // If the enum integer type is less than an integer in bit width,
5339 // then we must promote it to an integer size.
Greg Clayton6beaaa62011-01-17 03:46:26 +00005340 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
Greg Claytone02b8502010-10-12 04:29:14 +00005341 {
5342 if (enum_decl->getIntegerType()->isSignedIntegerType())
Greg Clayton6beaaa62011-01-17 03:46:26 +00005343 promotion_qual_type = ast->IntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005344 else
Greg Clayton6beaaa62011-01-17 03:46:26 +00005345 promotion_qual_type = ast->UnsignedIntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005346 }
5347 else
5348 promotion_qual_type = enum_decl->getIntegerType();
5349
5350 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
Greg Clayton14372242010-09-29 03:44:17 +00005351 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005352 }
5353 }
5354 }
5355 return false;
5356}
5357
5358
5359#pragma mark Enumeration Types
5360
Greg Clayton1be10fc2010-09-29 01:12:09 +00005361clang_type_t
Greg Claytonca512b32011-01-14 04:54:56 +00005362ClangASTContext::CreateEnumerationType
5363(
5364 const char *name,
5365 DeclContext *decl_ctx,
5366 const Declaration &decl,
5367 clang_type_t integer_qual_type
5368)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005369{
5370 // TODO: Do something intelligent with the Declaration object passed in
5371 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005372 ASTContext *ast = getASTContext();
5373 assert (ast != NULL);
Greg Claytone02b8502010-10-12 04:29:14 +00005374
5375 // TODO: ask about these...
5376// const bool IsScoped = false;
5377// const bool IsFixed = false;
5378
Greg Clayton6beaaa62011-01-17 03:46:26 +00005379 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00005380 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00005381 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00005382 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005383 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callanan48114472010-12-13 01:26:27 +00005384 NULL,
5385 false, // IsScoped
5386 false, // IsScopedUsingClassTag
5387 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00005388
5389
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005390 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00005391 {
5392 // TODO: check if we should be setting the promotion type too?
5393 enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00005394
5395 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
5396
Greg Clayton6beaaa62011-01-17 03:46:26 +00005397 return ast->getTagDeclType(enum_decl).getAsOpaquePtr();
Greg Clayton83ff3892010-09-12 23:17:56 +00005398 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005399 return NULL;
5400}
5401
Greg Clayton1be10fc2010-09-29 01:12:09 +00005402clang_type_t
5403ClangASTContext::GetEnumerationIntegerType (clang_type_t enum_clang_type)
5404{
5405 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5406
Sean Callanan78e37602011-01-27 04:42:51 +00005407 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Greg Clayton1be10fc2010-09-29 01:12:09 +00005408 if (clang_type)
5409 {
5410 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5411 if (enum_type)
5412 {
5413 EnumDecl *enum_decl = enum_type->getDecl();
5414 if (enum_decl)
5415 return enum_decl->getIntegerType().getAsOpaquePtr();
5416 }
5417 }
5418 return NULL;
5419}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005420bool
5421ClangASTContext::AddEnumerationValueToEnumerationType
5422(
Greg Clayton1be10fc2010-09-29 01:12:09 +00005423 clang_type_t enum_clang_type,
5424 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005425 const Declaration &decl,
5426 const char *name,
5427 int64_t enum_value,
5428 uint32_t enum_value_bit_size
5429)
5430{
5431 if (enum_clang_type && enumerator_clang_type && name)
5432 {
5433 // TODO: Do something intelligent with the Declaration object passed in
5434 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005435 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005436 IdentifierTable *identifier_table = getIdentifierTable();
5437
Greg Clayton6beaaa62011-01-17 03:46:26 +00005438 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005439 assert (identifier_table != NULL);
5440 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5441
Sean Callanan78e37602011-01-27 04:42:51 +00005442 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005443 if (clang_type)
5444 {
5445 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5446
5447 if (enum_type)
5448 {
5449 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, false);
5450 enum_llvm_apsint = enum_value;
5451 EnumConstantDecl *enumerator_decl =
Greg Clayton6beaaa62011-01-17 03:46:26 +00005452 EnumConstantDecl::Create (*ast,
5453 enum_type->getDecl(),
5454 SourceLocation(),
5455 name ? &identifier_table->get(name) : NULL, // Identifier
5456 QualType::getFromOpaquePtr(enumerator_clang_type),
5457 NULL,
5458 enum_llvm_apsint);
5459
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005460 if (enumerator_decl)
5461 {
5462 enum_type->getDecl()->addDecl(enumerator_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005463
5464#ifdef LLDB_CONFIGURATION_DEBUG
5465 VerifyDecl(enumerator_decl);
5466#endif
5467
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005468 return true;
5469 }
5470 }
5471 }
5472 }
5473 return false;
5474}
5475
5476#pragma mark Pointers & References
5477
Greg Clayton1be10fc2010-09-29 01:12:09 +00005478clang_type_t
5479ClangASTContext::CreatePointerType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005480{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005481 return CreatePointerType (getASTContext(), clang_type);
5482}
5483
5484clang_type_t
5485ClangASTContext::CreatePointerType (clang::ASTContext *ast, clang_type_t clang_type)
5486{
5487 if (ast && clang_type)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005488 {
5489 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5490
Greg Clayton737b9322010-09-13 03:32:57 +00005491 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5492 switch (type_class)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005493 {
5494 case clang::Type::ObjCObject:
5495 case clang::Type::ObjCInterface:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005496 return ast->getObjCObjectPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005497
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005498 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005499 return ast->getPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005500 }
5501 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005502 return NULL;
5503}
5504
Greg Clayton1be10fc2010-09-29 01:12:09 +00005505clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005506ClangASTContext::CreateLValueReferenceType (clang::ASTContext *ast,
5507 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005508{
5509 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005510 return ast->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005511 return NULL;
5512}
5513
Greg Clayton1be10fc2010-09-29 01:12:09 +00005514clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005515ClangASTContext::CreateRValueReferenceType (clang::ASTContext *ast,
5516 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005517{
5518 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005519 return ast->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005520 return NULL;
5521}
5522
Greg Clayton1be10fc2010-09-29 01:12:09 +00005523clang_type_t
5524ClangASTContext::CreateMemberPointerType (clang_type_t clang_pointee_type, clang_type_t clang_class_type)
Greg Clayton9b81a312010-06-12 01:20:30 +00005525{
5526 if (clang_pointee_type && clang_pointee_type)
5527 return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type),
5528 QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr();
5529 return NULL;
5530}
5531
Greg Clayton1a65ae12011-01-25 23:55:37 +00005532uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005533ClangASTContext::GetPointerBitSize ()
5534{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005535 ASTContext *ast = getASTContext();
5536 return ast->getTypeSize(ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005537}
5538
5539bool
Greg Clayton219cf312012-03-30 00:51:13 +00005540ClangASTContext::IsPossibleDynamicType (clang::ASTContext *ast,
5541 clang_type_t clang_type,
5542 clang_type_t *dynamic_pointee_type,
5543 bool check_cplusplus,
5544 bool check_objc)
Greg Claytondea8cb42011-06-29 22:09:02 +00005545{
5546 QualType pointee_qual_type;
5547 if (clang_type)
5548 {
5549 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5550 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5551 bool success = false;
5552 switch (type_class)
5553 {
5554 case clang::Type::Builtin:
Greg Clayton219cf312012-03-30 00:51:13 +00005555 if (check_objc && cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
Greg Claytondea8cb42011-06-29 22:09:02 +00005556 {
5557 if (dynamic_pointee_type)
5558 *dynamic_pointee_type = clang_type;
5559 return true;
5560 }
5561 break;
5562
5563 case clang::Type::ObjCObjectPointer:
Greg Clayton219cf312012-03-30 00:51:13 +00005564 if (check_objc)
5565 {
5566 if (dynamic_pointee_type)
5567 *dynamic_pointee_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5568 return true;
5569 }
5570 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005571
5572 case clang::Type::Pointer:
5573 pointee_qual_type = cast<PointerType>(qual_type)->getPointeeType();
5574 success = true;
5575 break;
5576
5577 case clang::Type::LValueReference:
5578 case clang::Type::RValueReference:
5579 pointee_qual_type = cast<ReferenceType>(qual_type)->getPointeeType();
5580 success = true;
5581 break;
5582
5583 case clang::Type::Typedef:
Greg Clayton70364252012-08-31 18:56:24 +00005584 return ClangASTContext::IsPossibleDynamicType (ast,
5585 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
5586 dynamic_pointee_type,
5587 check_cplusplus,
5588 check_objc);
Sean Callanan912855f2011-08-11 23:56:13 +00005589
5590 case clang::Type::Elaborated:
Greg Clayton70364252012-08-31 18:56:24 +00005591 return ClangASTContext::IsPossibleDynamicType (ast,
5592 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
5593 dynamic_pointee_type,
5594 check_cplusplus,
5595 check_objc);
Sean Callanan912855f2011-08-11 23:56:13 +00005596
Greg Claytondea8cb42011-06-29 22:09:02 +00005597 default:
5598 break;
5599 }
5600
5601 if (success)
5602 {
5603 // Check to make sure what we are pointing too is a possible dynamic C++ type
5604 // We currently accept any "void *" (in case we have a class that has been
5605 // watered down to an opaque pointer) and virtual C++ classes.
5606 const clang::Type::TypeClass pointee_type_class = pointee_qual_type->getTypeClass();
5607 switch (pointee_type_class)
5608 {
5609 case clang::Type::Builtin:
5610 switch (cast<clang::BuiltinType>(pointee_qual_type)->getKind())
5611 {
5612 case clang::BuiltinType::UnknownAny:
5613 case clang::BuiltinType::Void:
5614 if (dynamic_pointee_type)
5615 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5616 return true;
5617
5618 case clang::BuiltinType::NullPtr:
5619 case clang::BuiltinType::Bool:
5620 case clang::BuiltinType::Char_U:
5621 case clang::BuiltinType::UChar:
5622 case clang::BuiltinType::WChar_U:
5623 case clang::BuiltinType::Char16:
5624 case clang::BuiltinType::Char32:
5625 case clang::BuiltinType::UShort:
5626 case clang::BuiltinType::UInt:
5627 case clang::BuiltinType::ULong:
5628 case clang::BuiltinType::ULongLong:
5629 case clang::BuiltinType::UInt128:
5630 case clang::BuiltinType::Char_S:
5631 case clang::BuiltinType::SChar:
5632 case clang::BuiltinType::WChar_S:
5633 case clang::BuiltinType::Short:
5634 case clang::BuiltinType::Int:
5635 case clang::BuiltinType::Long:
5636 case clang::BuiltinType::LongLong:
5637 case clang::BuiltinType::Int128:
5638 case clang::BuiltinType::Float:
5639 case clang::BuiltinType::Double:
5640 case clang::BuiltinType::LongDouble:
5641 case clang::BuiltinType::Dependent:
5642 case clang::BuiltinType::Overload:
5643 case clang::BuiltinType::ObjCId:
5644 case clang::BuiltinType::ObjCClass:
5645 case clang::BuiltinType::ObjCSel:
5646 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00005647 case clang::BuiltinType::Half:
5648 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00005649 case clang::BuiltinType::PseudoObject:
Sean Callanan3d654b32012-09-24 22:25:51 +00005650 case clang::BuiltinType::BuiltinFn:
Greg Claytondea8cb42011-06-29 22:09:02 +00005651 break;
5652 }
5653 break;
5654
5655 case clang::Type::Record:
Greg Clayton219cf312012-03-30 00:51:13 +00005656 if (check_cplusplus)
Greg Claytondea8cb42011-06-29 22:09:02 +00005657 {
5658 CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
5659 if (cxx_record_decl)
5660 {
Greg Clayton70364252012-08-31 18:56:24 +00005661 bool is_complete = cxx_record_decl->isCompleteDefinition();
5662 if (!is_complete)
Sean Callanan6e4100ea2012-09-08 00:49:45 +00005663 is_complete = ClangASTContext::GetCompleteType (ast, pointee_qual_type.getAsOpaquePtr());
Greg Clayton70364252012-08-31 18:56:24 +00005664
5665 if (is_complete)
Greg Claytondea8cb42011-06-29 22:09:02 +00005666 {
5667 success = cxx_record_decl->isDynamicClass();
5668 }
5669 else
5670 {
Greg Clayton70364252012-08-31 18:56:24 +00005671 success = false;
Greg Claytondea8cb42011-06-29 22:09:02 +00005672 }
Greg Clayton70364252012-08-31 18:56:24 +00005673
Greg Claytondea8cb42011-06-29 22:09:02 +00005674 if (success)
5675 {
5676 if (dynamic_pointee_type)
5677 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5678 return true;
5679 }
5680 }
5681 }
5682 break;
5683
5684 case clang::Type::ObjCObject:
5685 case clang::Type::ObjCInterface:
Greg Clayton219cf312012-03-30 00:51:13 +00005686 if (check_objc)
5687 {
5688 if (dynamic_pointee_type)
5689 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5690 return true;
5691 }
5692 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005693
5694 default:
5695 break;
5696 }
5697 }
5698 }
5699 if (dynamic_pointee_type)
5700 *dynamic_pointee_type = NULL;
5701 return false;
5702}
5703
5704
5705bool
Greg Clayton007d5be2011-05-30 00:49:24 +00005706ClangASTContext::IsPossibleCPlusPlusDynamicType (clang::ASTContext *ast, clang_type_t clang_type, clang_type_t *dynamic_pointee_type)
5707{
Greg Clayton219cf312012-03-30 00:51:13 +00005708 return IsPossibleDynamicType (ast,
5709 clang_type,
5710 dynamic_pointee_type,
5711 true, // Check for dynamic C++ types
5712 false); // Check for dynamic ObjC types
Greg Clayton007d5be2011-05-30 00:49:24 +00005713}
5714
Sean Callanan98298012011-10-27 19:41:13 +00005715bool
5716ClangASTContext::IsReferenceType (clang_type_t clang_type, clang_type_t *target_type)
5717{
5718 if (clang_type == NULL)
5719 return false;
5720
5721 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5722 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5723
5724 switch (type_class)
5725 {
5726 case clang::Type::LValueReference:
5727 if (target_type)
5728 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5729 return true;
5730 case clang::Type::RValueReference:
5731 if (target_type)
5732 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5733 return true;
5734 case clang::Type::Typedef:
5735 return ClangASTContext::IsReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5736 case clang::Type::Elaborated:
5737 return ClangASTContext::IsReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5738 default:
5739 break;
5740 }
5741
5742 return false;
5743}
Greg Clayton007d5be2011-05-30 00:49:24 +00005744
5745bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005746ClangASTContext::IsPointerOrReferenceType (clang_type_t clang_type, clang_type_t*target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005747{
5748 if (clang_type == NULL)
5749 return false;
5750
5751 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005752 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5753 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005754 {
Sean Callanana2424172010-10-25 00:29:48 +00005755 case clang::Type::Builtin:
5756 switch (cast<clang::BuiltinType>(qual_type)->getKind())
5757 {
5758 default:
5759 break;
5760 case clang::BuiltinType::ObjCId:
5761 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00005762 return true;
5763 }
5764 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00005765 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005766 if (target_type)
5767 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5768 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005769 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005770 if (target_type)
5771 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5772 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005773 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005774 if (target_type)
5775 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5776 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005777 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005778 if (target_type)
5779 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5780 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005781 case clang::Type::LValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005782 if (target_type)
5783 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5784 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005785 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005786 if (target_type)
5787 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5788 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005789 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00005790 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00005791 case clang::Type::Elaborated:
5792 return ClangASTContext::IsPointerOrReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005793 default:
5794 break;
5795 }
5796 return false;
5797}
5798
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005799bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005800ClangASTContext::IsIntegerType (clang_type_t clang_type, bool &is_signed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005801{
5802 if (!clang_type)
5803 return false;
5804
5805 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5806 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal());
5807
5808 if (builtin_type)
5809 {
5810 if (builtin_type->isInteger())
Jim Inghamef651602011-12-22 19:12:40 +00005811 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005812 is_signed = builtin_type->isSignedInteger();
Jim Inghamef651602011-12-22 19:12:40 +00005813 return true;
5814 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005815 }
5816
5817 return false;
5818}
5819
5820bool
Greg Claytonaffb03b2011-07-08 18:27:39 +00005821ClangASTContext::IsPointerType (clang_type_t clang_type, clang_type_t *target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005822{
Greg Claytonaffb03b2011-07-08 18:27:39 +00005823 if (target_type)
5824 *target_type = NULL;
5825
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005826 if (clang_type)
5827 {
5828 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005829 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5830 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005831 {
Sean Callanana2424172010-10-25 00:29:48 +00005832 case clang::Type::Builtin:
5833 switch (cast<clang::BuiltinType>(qual_type)->getKind())
5834 {
5835 default:
5836 break;
5837 case clang::BuiltinType::ObjCId:
5838 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00005839 return true;
5840 }
5841 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00005842 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005843 if (target_type)
5844 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5845 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005846 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005847 if (target_type)
5848 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5849 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005850 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005851 if (target_type)
5852 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5853 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005854 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005855 if (target_type)
5856 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5857 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005858 case clang::Type::Typedef:
Greg Claytonaffb03b2011-07-08 18:27:39 +00005859 return ClangASTContext::IsPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), target_type);
Sean Callanan912855f2011-08-11 23:56:13 +00005860 case clang::Type::Elaborated:
5861 return ClangASTContext::IsPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), target_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005862 default:
5863 break;
5864 }
5865 }
5866 return false;
5867}
5868
5869bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005870ClangASTContext::IsFloatingPointType (clang_type_t clang_type, uint32_t &count, bool &is_complex)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005871{
5872 if (clang_type)
5873 {
5874 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5875
5876 if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()))
5877 {
5878 clang::BuiltinType::Kind kind = BT->getKind();
5879 if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble)
5880 {
5881 count = 1;
5882 is_complex = false;
5883 return true;
5884 }
5885 }
5886 else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal()))
5887 {
5888 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex))
5889 {
5890 count = 2;
5891 is_complex = true;
5892 return true;
5893 }
5894 }
5895 else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal()))
5896 {
5897 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex))
5898 {
5899 count = VT->getNumElements();
5900 is_complex = false;
5901 return true;
5902 }
5903 }
5904 }
5905 return false;
5906}
5907
Enrico Granata9fc19442011-07-06 02:13:41 +00005908bool
5909ClangASTContext::IsScalarType (lldb::clang_type_t clang_type)
5910{
5911 bool is_signed;
5912 if (ClangASTContext::IsIntegerType(clang_type, is_signed))
5913 return true;
5914
5915 uint32_t count;
5916 bool is_complex;
5917 return ClangASTContext::IsFloatingPointType(clang_type, count, is_complex) && !is_complex;
5918}
5919
5920bool
5921ClangASTContext::IsPointerToScalarType (lldb::clang_type_t clang_type)
5922{
5923 if (!IsPointerType(clang_type))
5924 return false;
5925
5926 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5927 lldb::clang_type_t pointee_type = qual_type.getTypePtr()->getPointeeType().getAsOpaquePtr();
5928 return IsScalarType(pointee_type);
5929}
5930
5931bool
5932ClangASTContext::IsArrayOfScalarType (lldb::clang_type_t clang_type)
5933{
Sean Callanan0caa21c2012-01-19 23:54:24 +00005934 clang_type = GetAsArrayType(clang_type);
5935
5936 if (clang_type == 0)
Enrico Granata9fc19442011-07-06 02:13:41 +00005937 return false;
5938
5939 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5940 lldb::clang_type_t item_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
5941 return IsScalarType(item_type);
5942}
5943
Greg Clayton8f92f0a2010-10-14 22:52:14 +00005944
5945bool
5946ClangASTContext::GetCXXClassName (clang_type_t clang_type, std::string &class_name)
5947{
5948 if (clang_type)
5949 {
5950 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5951
5952 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5953 if (cxx_record_decl)
5954 {
5955 class_name.assign (cxx_record_decl->getIdentifier()->getNameStart());
5956 return true;
5957 }
5958 }
5959 class_name.clear();
5960 return false;
5961}
5962
5963
Greg Clayton0fffff52010-09-24 05:15:53 +00005964bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005965ClangASTContext::IsCXXClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00005966{
5967 if (clang_type)
5968 {
5969 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5970 if (qual_type->getAsCXXRecordDecl() != NULL)
5971 return true;
5972 }
5973 return false;
5974}
5975
Greg Clayton20568dd2011-10-13 23:13:20 +00005976bool
5977ClangASTContext::IsBeingDefined (lldb::clang_type_t clang_type)
5978{
5979 if (clang_type)
5980 {
5981 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5982 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type);
5983 if (tag_type)
5984 return tag_type->isBeingDefined();
5985 }
5986 return false;
5987}
5988
Greg Clayton0fffff52010-09-24 05:15:53 +00005989bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005990ClangASTContext::IsObjCClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00005991{
5992 if (clang_type)
5993 {
5994 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5995 if (qual_type->isObjCObjectOrInterfaceType())
5996 return true;
5997 }
5998 return false;
5999}
6000
Sean Callanan72772842012-02-22 23:57:45 +00006001bool
6002ClangASTContext::IsObjCObjectPointerType (lldb::clang_type_t clang_type, clang_type_t *class_type)
6003{
6004 if (clang_type)
6005 {
6006 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6007 if (qual_type->isObjCObjectPointerType())
6008 {
6009 if (class_type)
6010 {
6011 *class_type = NULL;
6012
6013 if (!qual_type->isObjCClassType() &&
6014 !qual_type->isObjCIdType())
6015 {
6016 const ObjCObjectPointerType *obj_pointer_type = dyn_cast<ObjCObjectPointerType>(qual_type);
Sean Callanand7dabe22012-03-10 01:59:11 +00006017 if (!obj_pointer_type)
6018 *class_type = NULL;
Sean Callanan1fc91ad2012-03-10 02:00:32 +00006019 else
6020 *class_type = QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr();
Sean Callanan72772842012-02-22 23:57:45 +00006021 }
6022 }
6023 return true;
6024 }
6025 }
6026 return false;
6027}
6028
6029bool
6030ClangASTContext::GetObjCClassName (lldb::clang_type_t clang_type,
6031 std::string &class_name)
6032{
6033 if (!clang_type)
6034 return false;
6035
6036 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(QualType::getFromOpaquePtr(clang_type));
6037 if (!object_type)
6038 return false;
6039
6040 const ObjCInterfaceDecl *interface = object_type->getInterface();
6041 if (!interface)
6042 return false;
6043
6044 class_name = interface->getNameAsString();
6045 return true;
6046}
Greg Clayton0fffff52010-09-24 05:15:53 +00006047
Greg Clayton73b472d2010-10-27 03:32:59 +00006048bool
6049ClangASTContext::IsCharType (clang_type_t clang_type)
6050{
6051 if (clang_type)
6052 return QualType::getFromOpaquePtr(clang_type)->isCharType();
6053 return false;
6054}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006055
6056bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006057ClangASTContext::IsCStringType (clang_type_t clang_type, uint32_t &length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006058{
Greg Clayton73b472d2010-10-27 03:32:59 +00006059 clang_type_t pointee_or_element_clang_type = NULL;
6060 Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, &pointee_or_element_clang_type));
6061
6062 if (pointee_or_element_clang_type == NULL)
6063 return false;
6064
6065 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006066 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006067 QualType pointee_or_element_qual_type (QualType::getFromOpaquePtr (pointee_or_element_clang_type));
6068
6069 if (pointee_or_element_qual_type.getUnqualifiedType()->isCharType())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006070 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006071 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6072 if (type_flags.Test (eTypeIsArray))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006073 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006074 // We know the size of the array and it could be a C string
6075 // since it is an array of characters
6076 length = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
6077 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006078 }
Greg Clayton73b472d2010-10-27 03:32:59 +00006079 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006080 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006081 length = 0;
6082 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006083 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006084
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006085 }
6086 }
6087 return false;
6088}
6089
6090bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006091ClangASTContext::IsFunctionPointerType (clang_type_t clang_type)
Greg Clayton737b9322010-09-13 03:32:57 +00006092{
6093 if (clang_type)
6094 {
6095 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6096
6097 if (qual_type->isFunctionPointerType())
6098 return true;
6099
6100 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6101 switch (type_class)
6102 {
Sean Callananfb0b7582011-03-15 00:17:19 +00006103 default:
6104 break;
Greg Clayton737b9322010-09-13 03:32:57 +00006105 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00006106 return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006107 case clang::Type::Elaborated:
6108 return ClangASTContext::IsFunctionPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00006109
6110 case clang::Type::LValueReference:
6111 case clang::Type::RValueReference:
6112 {
Sean Callanan78e37602011-01-27 04:42:51 +00006113 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00006114 if (reference_type)
6115 return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr());
6116 }
6117 break;
6118 }
6119 }
6120 return false;
6121}
6122
Greg Clayton73b472d2010-10-27 03:32:59 +00006123size_t
6124ClangASTContext::GetArraySize (clang_type_t clang_type)
6125{
6126 if (clang_type)
6127 {
Greg Claytonef37d68a2011-07-09 17:12:27 +00006128 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
6129 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6130 switch (type_class)
6131 {
6132 case clang::Type::ConstantArray:
6133 {
6134 const ConstantArrayType *array = cast<ConstantArrayType>(QualType::getFromOpaquePtr(clang_type).getTypePtr());
6135 if (array)
6136 return array->getSize().getLimitedValue();
6137 }
6138 break;
6139
6140 case clang::Type::Typedef:
6141 return ClangASTContext::GetArraySize(cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006142
6143 case clang::Type::Elaborated:
6144 return ClangASTContext::GetArraySize(cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00006145
6146 default:
6147 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006148 }
Greg Clayton73b472d2010-10-27 03:32:59 +00006149 }
6150 return 0;
6151}
Greg Clayton737b9322010-09-13 03:32:57 +00006152
Sean Callanan0caa21c2012-01-19 23:54:24 +00006153clang_type_t
6154ClangASTContext::GetAsArrayType (clang_type_t clang_type, clang_type_t*member_type, uint64_t *size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006155{
6156 if (!clang_type)
Sean Callanan0caa21c2012-01-19 23:54:24 +00006157 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006158
6159 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6160
Greg Clayton737b9322010-09-13 03:32:57 +00006161 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6162 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006163 {
Sean Callananfb0b7582011-03-15 00:17:19 +00006164 default:
6165 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006166
Greg Claytone1a916a2010-07-21 22:12:05 +00006167 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006168 if (member_type)
6169 *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6170 if (size)
Greg Claytonac4827f2011-04-01 18:14:08 +00006171 *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
Sean Callanan0caa21c2012-01-19 23:54:24 +00006172 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006173
Greg Claytone1a916a2010-07-21 22:12:05 +00006174 case clang::Type::IncompleteArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006175 if (member_type)
6176 *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6177 if (size)
6178 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006179 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006180
Greg Claytone1a916a2010-07-21 22:12:05 +00006181 case clang::Type::VariableArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006182 if (member_type)
6183 *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6184 if (size)
6185 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006186 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006187
Greg Claytone1a916a2010-07-21 22:12:05 +00006188 case clang::Type::DependentSizedArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006189 if (member_type)
6190 *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6191 if (size)
6192 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006193 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006194
6195 case clang::Type::Typedef:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006196 return ClangASTContext::GetAsArrayType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
6197 member_type,
6198 size);
Sean Callanan912855f2011-08-11 23:56:13 +00006199
6200 case clang::Type::Elaborated:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006201 return ClangASTContext::GetAsArrayType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
6202 member_type,
6203 size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006204 }
Sean Callanan0caa21c2012-01-19 23:54:24 +00006205 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006206}
6207
6208
6209#pragma mark Typedefs
6210
Greg Clayton1be10fc2010-09-29 01:12:09 +00006211clang_type_t
6212ClangASTContext::CreateTypedefType (const char *name, clang_type_t clang_type, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006213{
6214 if (clang_type)
6215 {
6216 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006217 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006218 IdentifierTable *identifier_table = getIdentifierTable();
Greg Clayton6beaaa62011-01-17 03:46:26 +00006219 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006220 assert (identifier_table != NULL);
6221 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00006222 decl_ctx = ast->getTranslationUnitDecl();
6223 TypedefDecl *decl = TypedefDecl::Create (*ast,
6224 decl_ctx,
6225 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00006226 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00006227 name ? &identifier_table->get(name) : NULL, // Identifier
6228 ast->CreateTypeSourceInfo(qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00006229
Greg Clayton147e1fa2011-10-14 22:47:18 +00006230 //decl_ctx->addDecl (decl);
6231
Sean Callanan2652ad22011-01-18 01:03:44 +00006232 decl->setAccess(AS_public); // TODO respect proper access specifier
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006233
6234 // Get a uniqued QualType for the typedef decl type
Greg Clayton6beaaa62011-01-17 03:46:26 +00006235 return ast->getTypedefType (decl).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006236 }
6237 return NULL;
6238}
6239
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006240// Disable this for now since I can't seem to get a nicely formatted float
6241// out of the APFloat class without just getting the float, double or quad
6242// and then using a formatted print on it which defeats the purpose. We ideally
6243// would like to get perfect string values for any kind of float semantics
6244// so we can support remote targets. The code below also requires a patch to
6245// llvm::APInt.
6246//bool
Greg Clayton6beaaa62011-01-17 03:46:26 +00006247//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 +00006248//{
6249// uint32_t count = 0;
6250// bool is_complex = false;
6251// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6252// {
6253// unsigned num_bytes_per_float = byte_size / count;
6254// unsigned num_bits_per_float = num_bytes_per_float * 8;
6255//
6256// float_str.clear();
6257// uint32_t i;
6258// for (i=0; i<count; i++)
6259// {
6260// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
6261// bool is_ieee = false;
6262// APFloat ap_float(ap_int, is_ieee);
6263// char s[1024];
6264// unsigned int hex_digits = 0;
6265// bool upper_case = false;
6266//
6267// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
6268// {
6269// if (i > 0)
6270// float_str.append(", ");
6271// float_str.append(s);
6272// if (i == 1 && is_complex)
6273// float_str.append(1, 'i');
6274// }
6275// }
6276// return !float_str.empty();
6277// }
6278// return false;
6279//}
6280
6281size_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00006282ClangASTContext::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 +00006283{
6284 if (clang_type)
6285 {
6286 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6287 uint32_t count = 0;
6288 bool is_complex = false;
6289 if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6290 {
6291 // TODO: handle complex and vector types
6292 if (count != 1)
6293 return false;
6294
6295 StringRef s_sref(s);
Greg Clayton6beaaa62011-01-17 03:46:26 +00006296 APFloat ap_float(ast->getFloatTypeSemantics(qual_type), s_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006297
Greg Clayton6beaaa62011-01-17 03:46:26 +00006298 const uint64_t bit_size = ast->getTypeSize (qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006299 const uint64_t byte_size = bit_size / 8;
6300 if (dst_size >= byte_size)
6301 {
6302 if (bit_size == sizeof(float)*8)
6303 {
6304 float float32 = ap_float.convertToFloat();
6305 ::memcpy (dst, &float32, byte_size);
6306 return byte_size;
6307 }
6308 else if (bit_size >= 64)
6309 {
6310 llvm::APInt ap_int(ap_float.bitcastToAPInt());
6311 ::memcpy (dst, ap_int.getRawData(), byte_size);
6312 return byte_size;
6313 }
6314 }
6315 }
6316 }
6317 return 0;
6318}
Sean Callanan6fe64b52010-09-17 02:24:29 +00006319
6320unsigned
Greg Clayton1be10fc2010-09-29 01:12:09 +00006321ClangASTContext::GetTypeQualifiers(clang_type_t clang_type)
Sean Callanan6fe64b52010-09-17 02:24:29 +00006322{
6323 assert (clang_type);
6324
6325 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6326
6327 return qual_type.getQualifiers().getCVRQualifiers();
6328}
Greg Clayton6beaaa62011-01-17 03:46:26 +00006329
6330bool
6331ClangASTContext::GetCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6332{
6333 if (clang_type == NULL)
6334 return false;
6335
Greg Claytonc432c192011-01-20 04:18:48 +00006336 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006337}
6338
6339
6340bool
6341ClangASTContext::GetCompleteType (clang_type_t clang_type)
6342{
6343 return ClangASTContext::GetCompleteType (getASTContext(), clang_type);
6344}
6345
Greg Claytona2721472011-06-25 00:44:06 +00006346bool
Enrico Granata86027e92012-03-24 01:11:14 +00006347ClangASTContext::IsCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6348{
6349 if (clang_type == NULL)
6350 return false;
6351
6352 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type), false); // just check but don't let it actually complete
6353}
6354
6355
6356bool
6357ClangASTContext::IsCompleteType (clang_type_t clang_type)
6358{
6359 return ClangASTContext::IsCompleteType (getASTContext(), clang_type);
6360}
6361
6362bool
Greg Claytona2721472011-06-25 00:44:06 +00006363ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
6364 clang::Decl *decl)
6365{
6366 if (!decl)
6367 return false;
6368
6369 ExternalASTSource *ast_source = ast->getExternalSource();
6370
6371 if (!ast_source)
6372 return false;
6373
6374 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
6375 {
Greg Clayton219cf312012-03-30 00:51:13 +00006376 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006377 return true;
6378
6379 if (!tag_decl->hasExternalLexicalStorage())
6380 return false;
6381
6382 ast_source->CompleteType(tag_decl);
6383
6384 return !tag_decl->getTypeForDecl()->isIncompleteType();
6385 }
6386 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
6387 {
Sean Callanan5b26f272012-02-04 08:49:35 +00006388 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006389 return true;
6390
6391 if (!objc_interface_decl->hasExternalLexicalStorage())
6392 return false;
6393
6394 ast_source->CompleteType(objc_interface_decl);
6395
Sean Callanan5b26f272012-02-04 08:49:35 +00006396 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00006397 }
6398 else
6399 {
6400 return false;
6401 }
6402}
6403
Sean Callanan60217122012-04-13 00:10:03 +00006404void
Jim Ingham379397632012-10-27 02:54:13 +00006405ClangASTContext::SetMetadataAsUserID (uintptr_t object,
6406 user_id_t user_id)
6407{
6408 ClangASTMetadata meta_data;
6409 meta_data.SetUserID (user_id);
6410 SetMetadata (object, meta_data);
6411}
6412
6413void
Sean Callanan60217122012-04-13 00:10:03 +00006414ClangASTContext::SetMetadata (clang::ASTContext *ast,
6415 uintptr_t object,
Jim Ingham379397632012-10-27 02:54:13 +00006416 ClangASTMetadata &metadata)
Sean Callanan60217122012-04-13 00:10:03 +00006417{
6418 ClangExternalASTSourceCommon *external_source =
6419 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
6420
6421 if (external_source)
6422 external_source->SetMetadata(object, metadata);
6423}
6424
Jim Ingham379397632012-10-27 02:54:13 +00006425ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +00006426ClangASTContext::GetMetadata (clang::ASTContext *ast,
6427 uintptr_t object)
6428{
6429 ClangExternalASTSourceCommon *external_source =
6430 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
6431
6432 if (external_source && external_source->HasMetadata(object))
6433 return external_source->GetMetadata(object);
6434 else
Jim Ingham379397632012-10-27 02:54:13 +00006435 return NULL;
Sean Callanan60217122012-04-13 00:10:03 +00006436}
6437
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006438clang::DeclContext *
6439ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
6440{
Sean Callanana87bee82011-08-19 06:19:25 +00006441 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006442}
6443
6444clang::DeclContext *
6445ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
6446{
Sean Callanana87bee82011-08-19 06:19:25 +00006447 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006448}
6449
Greg Clayton685c88c2012-07-14 00:53:55 +00006450
6451bool
6452ClangASTContext::GetClassMethodInfoForDeclContext (clang::DeclContext *decl_ctx,
6453 lldb::LanguageType &language,
6454 bool &is_instance_method,
6455 ConstString &language_object_name)
6456{
6457 language_object_name.Clear();
6458 language = eLanguageTypeUnknown;
6459 is_instance_method = false;
6460
6461 if (decl_ctx)
6462 {
6463 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
6464 {
6465 if (method_decl->isStatic())
6466 {
6467 is_instance_method = false;
6468 }
6469 else
6470 {
6471 language_object_name.SetCString("this");
6472 is_instance_method = true;
6473 }
6474 language = eLanguageTypeC_plus_plus;
6475 return true;
6476 }
6477 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
6478 {
6479 // Both static and instance methods have a "self" object in objective C
6480 language_object_name.SetCString("self");
6481 if (method_decl->isInstanceMethod())
6482 {
6483 is_instance_method = true;
6484 }
6485 else
6486 {
6487 is_instance_method = false;
6488 }
6489 language = eLanguageTypeObjC;
6490 return true;
6491 }
Jim Ingham379397632012-10-27 02:54:13 +00006492 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
6493 {
6494 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), (uintptr_t) function_decl);
6495 if (metadata && metadata->HasObjectPtr())
6496 {
6497 language_object_name.SetCString (metadata->GetObjectPtrName());
6498 language = eLanguageTypeObjC;
6499 is_instance_method = true;
6500 }
6501 return true;
6502 }
Greg Clayton685c88c2012-07-14 00:53:55 +00006503 }
6504 return false;
6505}
6506