blob: 59c97c4941943e0e49c2f85f3d6294c8e83c82e9 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eli Friedman932197d2010-06-13 19:06:42 +000010#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011
12// C Includes
13// C++ Includes
14#include <string>
15
16// Other libraries and framework includes
Greg Clayton6beaaa62011-01-17 03:46:26 +000017
18// Clang headers like to use NDEBUG inside of them to enable/disable debug
19// releated features using "#ifndef NDEBUG" preprocessor blocks to do one thing
20// or another. This is bad because it means that if clang was built in release
21// mode, it assumes that you are building in release mode which is not always
22// the case. You can end up with functions that are defined as empty in header
23// files when NDEBUG is not defined, and this can cause link errors with the
24// clang .a files that you have since you might be missing functions in the .a
25// file. So we have to define NDEBUG when including clang headers to avoid any
26// mismatches. This is covered by rdar://problem/8691220
27
Sean Callanan3b1d4f62011-10-26 17:46:51 +000028#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000029#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000030#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000031// Need to include assert.h so it is as clang would expect it to be (disabled)
32#include <assert.h>
33#endif
34
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "clang/AST/ASTContext.h"
36#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000037#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000039#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000040#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "clang/AST/RecordLayout.h"
42#include "clang/AST/Type.h"
43#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000044#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000046#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "clang/Basic/SourceManager.h"
48#include "clang/Basic/TargetInfo.h"
49#include "clang/Basic/TargetOptions.h"
50#include "clang/Frontend/FrontendOptions.h"
51#include "clang/Frontend/LangStandard.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000052
53#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000054#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000055#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
56// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
57#include <assert.h>
58#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059
Greg Clayton514487e2011-02-15 21:59:32 +000060#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061#include "lldb/Core/dwarf.h"
Greg Clayton73b472d2010-10-27 03:32:59 +000062#include "lldb/Core/Flags.h"
Sean Callananfb8b7092010-10-28 18:19:36 +000063#include "lldb/Core/Log.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000064#include "lldb/Core/RegularExpression.h"
65#include "lldb/Expression/ASTDumper.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000066#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000067#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000068#include "lldb/Target/ExecutionContext.h"
69#include "lldb/Target/Process.h"
70#include "lldb/Target/ObjCLanguageRuntime.h"
71
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072
Eli Friedman932197d2010-06-13 19:06:42 +000073#include <stdio.h>
74
Greg Claytonc86103d2010-08-05 01:57:25 +000075using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076using namespace lldb_private;
77using namespace llvm;
78using namespace clang;
79
Greg Clayton6beaaa62011-01-17 03:46:26 +000080
81static bool
Enrico Granata86027e92012-03-24 01:11:14 +000082GetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion = true)
Greg Clayton6beaaa62011-01-17 03:46:26 +000083{
84 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
85 switch (type_class)
86 {
Sean Callanan5b26f272012-02-04 08:49:35 +000087 case clang::Type::ConstantArray:
88 {
89 const clang::ArrayType *array_type = dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
90
91 if (array_type)
Enrico Granata86027e92012-03-24 01:11:14 +000092 return GetCompleteQualType (ast, array_type->getElementType(), allow_completion);
Sean Callanan5b26f272012-02-04 08:49:35 +000093 }
94 break;
95
Greg Clayton6beaaa62011-01-17 03:46:26 +000096 case clang::Type::Record:
97 case clang::Type::Enum:
98 {
Sean Callanan78e37602011-01-27 04:42:51 +000099 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +0000100 if (tag_type)
101 {
102 clang::TagDecl *tag_decl = tag_type->getDecl();
103 if (tag_decl)
104 {
Greg Clayton219cf312012-03-30 00:51:13 +0000105 if (tag_decl->isCompleteDefinition())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000106 return true;
Enrico Granata86027e92012-03-24 01:11:14 +0000107
108 if (!allow_completion)
109 return false;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000110
111 if (tag_decl->hasExternalLexicalStorage())
112 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000113 if (ast)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000114 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000115 ExternalASTSource *external_ast_source = ast->getExternalSource();
116 if (external_ast_source)
117 {
118 external_ast_source->CompleteType(tag_decl);
119 return !tag_type->isIncompleteType();
120 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000121 }
122 }
123 return false;
124 }
125 }
126
127 }
128 break;
129
130 case clang::Type::ObjCObject:
131 case clang::Type::ObjCInterface:
132 {
Sean Callanan78e37602011-01-27 04:42:51 +0000133 const clang::ObjCObjectType *objc_class_type = dyn_cast<clang::ObjCObjectType>(qual_type);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000134 if (objc_class_type)
135 {
136 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
137 // We currently can't complete objective C types through the newly added ASTContext
138 // because it only supports TagDecl objects right now...
Enrico Granata9dd75c82011-07-15 23:30:15 +0000139 if (class_interface_decl)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000140 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000141 if (class_interface_decl->getDefinition())
142 return true;
143
Enrico Granata86027e92012-03-24 01:11:14 +0000144 if (!allow_completion)
145 return false;
Greg Clayton20533982012-03-30 23:48:28 +0000146
Sean Callanan5b26f272012-02-04 08:49:35 +0000147 if (class_interface_decl->hasExternalLexicalStorage())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000148 {
Enrico Granata0a3958e2011-07-02 00:25:22 +0000149 if (ast)
Greg Clayton007d5be2011-05-30 00:49:24 +0000150 {
Enrico Granata0a3958e2011-07-02 00:25:22 +0000151 ExternalASTSource *external_ast_source = ast->getExternalSource();
152 if (external_ast_source)
153 {
154 external_ast_source->CompleteType (class_interface_decl);
Sean Callanan5b26f272012-02-04 08:49:35 +0000155 return !objc_class_type->isIncompleteType();
Enrico Granata0a3958e2011-07-02 00:25:22 +0000156 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000157 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000158 }
Enrico Granata0a3958e2011-07-02 00:25:22 +0000159 return false;
Sean Callanan5b26f272012-02-04 08:49:35 +0000160 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000161 }
162 }
163 break;
164
165 case clang::Type::Typedef:
Enrico Granata86027e92012-03-24 01:11:14 +0000166 return GetCompleteQualType (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType(), allow_completion);
Sean Callanan912855f2011-08-11 23:56:13 +0000167
168 case clang::Type::Elaborated:
Enrico Granata86027e92012-03-24 01:11:14 +0000169 return GetCompleteQualType (ast, cast<ElaboratedType>(qual_type)->getNamedType(), allow_completion);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000170
171 default:
172 break;
173 }
174
175 return true;
176}
177
Greg Clayton8cf05932010-07-22 18:30:50 +0000178static AccessSpecifier
Greg Claytonc86103d2010-08-05 01:57:25 +0000179ConvertAccessTypeToAccessSpecifier (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000180{
181 switch (access)
182 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000183 default: break;
184 case eAccessNone: return AS_none;
185 case eAccessPublic: return AS_public;
186 case eAccessPrivate: return AS_private;
187 case eAccessProtected: return AS_protected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000188 }
189 return AS_none;
190}
191
192static ObjCIvarDecl::AccessControl
Greg Claytonc86103d2010-08-05 01:57:25 +0000193ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000194{
195 switch (access)
196 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000197 case eAccessNone: return ObjCIvarDecl::None;
198 case eAccessPublic: return ObjCIvarDecl::Public;
199 case eAccessPrivate: return ObjCIvarDecl::Private;
200 case eAccessProtected: return ObjCIvarDecl::Protected;
201 case eAccessPackage: return ObjCIvarDecl::Package;
Greg Clayton8cf05932010-07-22 18:30:50 +0000202 }
203 return ObjCIvarDecl::None;
204}
205
206
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207static void
208ParseLangArgs
209(
210 LangOptions &Opts,
Greg Clayton94e5d782010-06-13 17:34:29 +0000211 InputKind IK
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212)
213{
214 // FIXME: Cleanup per-file based stuff.
215
216 // Set some properties which depend soley on the input kind; it would be nice
217 // to move these to the language standard, and have the driver resolve the
218 // input kind + language standard.
Greg Clayton94e5d782010-06-13 17:34:29 +0000219 if (IK == IK_Asm) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220 Opts.AsmPreprocessor = 1;
Greg Clayton94e5d782010-06-13 17:34:29 +0000221 } else if (IK == IK_ObjC ||
222 IK == IK_ObjCXX ||
223 IK == IK_PreprocessedObjC ||
224 IK == IK_PreprocessedObjCXX) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225 Opts.ObjC1 = Opts.ObjC2 = 1;
226 }
227
228 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
229
230 if (LangStd == LangStandard::lang_unspecified) {
231 // Based on the base language, pick one.
232 switch (IK) {
Greg Clayton94e5d782010-06-13 17:34:29 +0000233 case IK_None:
234 case IK_AST:
Sean Callananfb0b7582011-03-15 00:17:19 +0000235 case IK_LLVM_IR:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000236 assert (!"Invalid input kind!");
Greg Clayton94e5d782010-06-13 17:34:29 +0000237 case IK_OpenCL:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238 LangStd = LangStandard::lang_opencl;
239 break;
Sean Callananfb0b7582011-03-15 00:17:19 +0000240 case IK_CUDA:
241 LangStd = LangStandard::lang_cuda;
242 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000243 case IK_Asm:
244 case IK_C:
245 case IK_PreprocessedC:
246 case IK_ObjC:
247 case IK_PreprocessedObjC:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248 LangStd = LangStandard::lang_gnu99;
249 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000250 case IK_CXX:
251 case IK_PreprocessedCXX:
252 case IK_ObjCXX:
253 case IK_PreprocessedObjCXX:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254 LangStd = LangStandard::lang_gnucxx98;
255 break;
256 }
257 }
258
259 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
Filipe Cabecinhase818ca22012-11-12 21:26:32 +0000260 Opts.LineComment = Std.hasLineComments();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261 Opts.C99 = Std.isC99();
262 Opts.CPlusPlus = Std.isCPlusPlus();
Chandler Carruth38336a12013-01-02 12:55:00 +0000263 Opts.CPlusPlus11 = Std.isCPlusPlus11();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264 Opts.Digraphs = Std.hasDigraphs();
265 Opts.GNUMode = Std.isGNUMode();
266 Opts.GNUInline = !Std.isC99();
267 Opts.HexFloats = Std.hasHexFloats();
268 Opts.ImplicitInt = Std.hasImplicitInt();
269
270 // OpenCL has some additional defaults.
271 if (LangStd == LangStandard::lang_opencl) {
272 Opts.OpenCL = 1;
273 Opts.AltiVec = 1;
274 Opts.CXXOperatorNames = 1;
275 Opts.LaxVectorConversions = 1;
276 }
277
278 // OpenCL and C++ both have bool, true, false keywords.
279 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
280
281// if (Opts.CPlusPlus)
282// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
283//
284// if (Args.hasArg(OPT_fobjc_gc_only))
285// Opts.setGCMode(LangOptions::GCOnly);
286// else if (Args.hasArg(OPT_fobjc_gc))
287// Opts.setGCMode(LangOptions::HybridGC);
288//
289// if (Args.hasArg(OPT_print_ivar_layout))
290// Opts.ObjCGCBitmapPrint = 1;
291//
292// if (Args.hasArg(OPT_faltivec))
293// Opts.AltiVec = 1;
294//
295// if (Args.hasArg(OPT_pthread))
296// Opts.POSIXThreads = 1;
297//
298// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
299// "default");
300// if (Vis == "default")
Sean Callanan31e851c2010-10-29 18:38:40 +0000301 Opts.setVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302// else if (Vis == "hidden")
303// Opts.setVisibilityMode(LangOptions::Hidden);
304// else if (Vis == "protected")
305// Opts.setVisibilityMode(LangOptions::Protected);
306// else
307// Diags.Report(diag::err_drv_invalid_value)
308// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
309
310// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
311
312 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
313 // is specified, or -std is set to a conforming mode.
314 Opts.Trigraphs = !Opts.GNUMode;
315// if (Args.hasArg(OPT_trigraphs))
316// Opts.Trigraphs = 1;
317//
318// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
319// OPT_fno_dollars_in_identifiers,
320// !Opts.AsmPreprocessor);
321// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
322// Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
323// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
324// if (Args.hasArg(OPT_fno_lax_vector_conversions))
325// Opts.LaxVectorConversions = 0;
326// Opts.Exceptions = Args.hasArg(OPT_fexceptions);
327// Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
328// Opts.Blocks = Args.hasArg(OPT_fblocks);
329// Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char);
330// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
331// Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
332// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
333// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
334// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
335// Opts.AccessControl = Args.hasArg(OPT_faccess_control);
336// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
337// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
338// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
339// Diags);
340// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
341// Opts.ObjCConstantStringClass = getLastArgValue(Args,
342// OPT_fconstant_string_class);
343// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
344// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
345// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
346// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
347// Opts.Static = Args.hasArg(OPT_static_define);
348 Opts.OptimizeSize = 0;
349
350 // FIXME: Eliminate this dependency.
351// unsigned Opt =
352// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
353// Opts.Optimize = Opt != 0;
354 unsigned Opt = 0;
355
356 // This is the __NO_INLINE__ define, which just depends on things like the
357 // optimization level and -fno-inline, not actually whether the backend has
358 // inlining enabled.
359 //
360 // FIXME: This is affected by other options (-fno-inline).
Sean Callanan3d654b32012-09-24 22:25:51 +0000361 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362
363// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
364// switch (SSP) {
365// default:
366// Diags.Report(diag::err_drv_invalid_value)
367// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
368// break;
369// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
370// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
371// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
372// }
373}
374
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375
Greg Clayton6beaaa62011-01-17 03:46:26 +0000376ClangASTContext::ClangASTContext (const char *target_triple) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377 m_target_triple(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000378 m_ast_ap(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379 m_language_options_ap(),
380 m_source_manager_ap(),
Sean Callanan880e6802011-10-07 23:18:13 +0000381 m_diagnostics_engine_ap(),
Sean Callananc5069ad2012-10-17 22:11:14 +0000382 m_target_options_rp(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383 m_target_info_ap(),
384 m_identifier_table_ap(),
385 m_selector_table_ap(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000386 m_builtins_ap(),
387 m_callback_tag_decl (NULL),
388 m_callback_objc_decl (NULL),
389 m_callback_baton (NULL)
390
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391{
392 if (target_triple && target_triple[0])
Greg Clayton880cbb02011-07-30 01:26:02 +0000393 SetTargetTriple (target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394}
395
396//----------------------------------------------------------------------
397// Destructor
398//----------------------------------------------------------------------
399ClangASTContext::~ClangASTContext()
400{
401 m_builtins_ap.reset();
402 m_selector_table_ap.reset();
403 m_identifier_table_ap.reset();
404 m_target_info_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000405 m_target_options_rp.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000406 m_diagnostics_engine_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407 m_source_manager_ap.reset();
408 m_language_options_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000409 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410}
411
412
413void
414ClangASTContext::Clear()
415{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000416 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417 m_language_options_ap.reset();
418 m_source_manager_ap.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000419 m_diagnostics_engine_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000420 m_target_options_rp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421 m_target_info_ap.reset();
422 m_identifier_table_ap.reset();
423 m_selector_table_ap.reset();
424 m_builtins_ap.reset();
425}
426
427const char *
428ClangASTContext::GetTargetTriple ()
429{
430 return m_target_triple.c_str();
431}
432
433void
434ClangASTContext::SetTargetTriple (const char *target_triple)
435{
436 Clear();
437 m_target_triple.assign(target_triple);
438}
439
Greg Clayton514487e2011-02-15 21:59:32 +0000440void
441ClangASTContext::SetArchitecture (const ArchSpec &arch)
442{
Greg Clayton880cbb02011-07-30 01:26:02 +0000443 SetTargetTriple(arch.GetTriple().str().c_str());
Greg Clayton514487e2011-02-15 21:59:32 +0000444}
445
Greg Clayton6beaaa62011-01-17 03:46:26 +0000446bool
447ClangASTContext::HasExternalSource ()
448{
449 ASTContext *ast = getASTContext();
450 if (ast)
451 return ast->getExternalSource () != NULL;
452 return false;
453}
454
455void
456ClangASTContext::SetExternalSource (llvm::OwningPtr<ExternalASTSource> &ast_source_ap)
457{
458 ASTContext *ast = getASTContext();
459 if (ast)
460 {
461 ast->setExternalSource (ast_source_ap);
462 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
463 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
464 }
465}
466
467void
468ClangASTContext::RemoveExternalSource ()
469{
470 ASTContext *ast = getASTContext();
471
472 if (ast)
473 {
474 llvm::OwningPtr<ExternalASTSource> empty_ast_source_ap;
475 ast->setExternalSource (empty_ast_source_ap);
476 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
477 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
478 }
479}
480
481
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482
483ASTContext *
484ClangASTContext::getASTContext()
485{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000486 if (m_ast_ap.get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 {
Greg Clayton6beaaa62011-01-17 03:46:26 +0000488 m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
489 *getSourceManager(),
Sean Callanan880e6802011-10-07 23:18:13 +0000490 getTargetInfo(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000491 *getIdentifierTable(),
492 *getSelectorTable(),
493 *getBuiltinContext(),
494 0));
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000495
Greg Clayton6beaaa62011-01-17 03:46:26 +0000496 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
497 {
498 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
499 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
500 }
501
Sean Callanan880e6802011-10-07 23:18:13 +0000502 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000504 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505}
506
507Builtin::Context *
508ClangASTContext::getBuiltinContext()
509{
510 if (m_builtins_ap.get() == NULL)
Sean Callanan880e6802011-10-07 23:18:13 +0000511 m_builtins_ap.reset (new Builtin::Context());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512 return m_builtins_ap.get();
513}
514
515IdentifierTable *
516ClangASTContext::getIdentifierTable()
517{
518 if (m_identifier_table_ap.get() == NULL)
519 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), NULL));
520 return m_identifier_table_ap.get();
521}
522
523LangOptions *
524ClangASTContext::getLanguageOptions()
525{
526 if (m_language_options_ap.get() == NULL)
527 {
528 m_language_options_ap.reset(new LangOptions());
Greg Clayton94e5d782010-06-13 17:34:29 +0000529 ParseLangArgs(*m_language_options_ap, IK_ObjCXX);
530// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531 }
532 return m_language_options_ap.get();
533}
534
535SelectorTable *
536ClangASTContext::getSelectorTable()
537{
538 if (m_selector_table_ap.get() == NULL)
539 m_selector_table_ap.reset (new SelectorTable());
540 return m_selector_table_ap.get();
541}
542
Sean Callanan79439e82010-11-18 02:56:27 +0000543clang::FileManager *
544ClangASTContext::getFileManager()
545{
546 if (m_file_manager_ap.get() == NULL)
Greg Clayton38a61402010-12-02 23:20:03 +0000547 {
548 clang::FileSystemOptions file_system_options;
549 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
550 }
Sean Callanan79439e82010-11-18 02:56:27 +0000551 return m_file_manager_ap.get();
552}
553
Greg Claytone1a916a2010-07-21 22:12:05 +0000554clang::SourceManager *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555ClangASTContext::getSourceManager()
556{
557 if (m_source_manager_ap.get() == NULL)
Sean Callanan880e6802011-10-07 23:18:13 +0000558 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559 return m_source_manager_ap.get();
560}
561
Sean Callanan880e6802011-10-07 23:18:13 +0000562clang::DiagnosticsEngine *
563ClangASTContext::getDiagnosticsEngine()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564{
Sean Callanan880e6802011-10-07 23:18:13 +0000565 if (m_diagnostics_engine_ap.get() == NULL)
Greg Claytona651b532010-11-19 21:46:54 +0000566 {
567 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Sean Callananec8f1ef2012-10-25 01:00:25 +0000568 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
Greg Claytona651b532010-11-19 21:46:54 +0000569 }
Sean Callanan880e6802011-10-07 23:18:13 +0000570 return m_diagnostics_engine_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000571}
572
Sean Callanan880e6802011-10-07 23:18:13 +0000573class NullDiagnosticConsumer : public DiagnosticConsumer
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000574{
575public:
Sean Callanan880e6802011-10-07 23:18:13 +0000576 NullDiagnosticConsumer ()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000577 {
578 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
579 }
580
Sean Callanan880e6802011-10-07 23:18:13 +0000581 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const Diagnostic &info)
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000582 {
583 if (m_log)
584 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000585 llvm::SmallVector<char, 32> diag_str(10);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000586 info.FormatDiagnostic(diag_str);
587 diag_str.push_back('\0');
588 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
589 }
590 }
Sean Callanan880e6802011-10-07 23:18:13 +0000591
592 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
593 {
594 return new NullDiagnosticConsumer ();
595 }
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000596private:
597 LogSP m_log;
598};
599
Sean Callanan880e6802011-10-07 23:18:13 +0000600DiagnosticConsumer *
601ClangASTContext::getDiagnosticConsumer()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000602{
Sean Callanan880e6802011-10-07 23:18:13 +0000603 if (m_diagnostic_consumer_ap.get() == NULL)
604 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000605
Sean Callanan880e6802011-10-07 23:18:13 +0000606 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000607}
608
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609TargetOptions *
610ClangASTContext::getTargetOptions()
611{
Sean Callananc5069ad2012-10-17 22:11:14 +0000612 if (m_target_options_rp.getPtr() == NULL && !m_target_triple.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000613 {
Sean Callananc5069ad2012-10-17 22:11:14 +0000614 m_target_options_rp.reset ();
615 m_target_options_rp = new TargetOptions();
616 if (m_target_options_rp.getPtr() != NULL)
617 m_target_options_rp->Triple = m_target_triple;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000618 }
Sean Callananc5069ad2012-10-17 22:11:14 +0000619 return m_target_options_rp.getPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620}
621
622
623TargetInfo *
624ClangASTContext::getTargetInfo()
625{
Greg Clayton70512312012-05-08 01:45:38 +0000626 // target_triple should be something like "x86_64-apple-macosx"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627 if (m_target_info_ap.get() == NULL && !m_target_triple.empty())
Greg Clayton38d880a2012-11-16 21:35:22 +0000628 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), getTargetOptions()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 return m_target_info_ap.get();
630}
631
632#pragma mark Basic Types
633
634static inline bool
Greg Clayton6beaaa62011-01-17 03:46:26 +0000635QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000636{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000637 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638 if (qual_type_bit_size == bit_size)
639 return true;
640 return false;
641}
642
Greg Clayton1be10fc2010-09-29 01:12:09 +0000643clang_type_t
Greg Claytonc86103d2010-08-05 01:57:25 +0000644ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000645{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000646 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647
Greg Clayton6beaaa62011-01-17 03:46:26 +0000648 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000649
Greg Clayton6beaaa62011-01-17 03:46:26 +0000650 return GetBuiltinTypeForEncodingAndBitSize (ast, encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651}
652
Greg Clayton1be10fc2010-09-29 01:12:09 +0000653clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000654ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000655{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000656 if (!ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657 return NULL;
658
659 switch (encoding)
660 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000661 case eEncodingInvalid:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000662 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
663 return ast->VoidPtrTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664 break;
665
Greg Claytonc86103d2010-08-05 01:57:25 +0000666 case eEncodingUint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000667 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
668 return ast->UnsignedCharTy.getAsOpaquePtr();
669 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
670 return ast->UnsignedShortTy.getAsOpaquePtr();
671 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
672 return ast->UnsignedIntTy.getAsOpaquePtr();
673 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
674 return ast->UnsignedLongTy.getAsOpaquePtr();
675 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
676 return ast->UnsignedLongLongTy.getAsOpaquePtr();
677 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
678 return ast->UnsignedInt128Ty.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000679 break;
680
Greg Claytonc86103d2010-08-05 01:57:25 +0000681 case eEncodingSint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000682 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
683 return ast->CharTy.getAsOpaquePtr();
684 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
685 return ast->ShortTy.getAsOpaquePtr();
686 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
687 return ast->IntTy.getAsOpaquePtr();
688 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
689 return ast->LongTy.getAsOpaquePtr();
690 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
691 return ast->LongLongTy.getAsOpaquePtr();
692 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
693 return ast->Int128Ty.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694 break;
695
Greg Claytonc86103d2010-08-05 01:57:25 +0000696 case eEncodingIEEE754:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000697 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
698 return ast->FloatTy.getAsOpaquePtr();
699 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
700 return ast->DoubleTy.getAsOpaquePtr();
701 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
702 return ast->LongDoubleTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703 break;
704
Greg Claytonc86103d2010-08-05 01:57:25 +0000705 case eEncodingVector:
Johnny Chenc79c93a2012-03-07 01:12:24 +0000706 // Sanity check that bit_size is a multiple of 8's.
707 if (bit_size && !(bit_size & 0x7u))
708 return ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8).getAsOpaquePtr();
709 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000710 }
711
712 return NULL;
713}
714
Greg Clayton1be10fc2010-09-29 01:12:09 +0000715clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
717{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000718 ASTContext *ast = getASTContext();
Sean Callanan38d4df52012-04-03 01:10:10 +0000719
720#define streq(a,b) strcmp(a,b) == 0
Greg Clayton6beaaa62011-01-17 03:46:26 +0000721 assert (ast != NULL);
722 if (ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000723 {
724 switch (dw_ate)
725 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000726 default:
727 break;
Greg Clayton605684e2011-10-28 23:06:08 +0000728
Sean Callanan38d4df52012-04-03 01:10:10 +0000729 case DW_ATE_address:
730 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
731 return ast->VoidPtrTy.getAsOpaquePtr();
732 break;
733
734 case DW_ATE_boolean:
735 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
736 return ast->BoolTy.getAsOpaquePtr();
737 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
738 return ast->UnsignedCharTy.getAsOpaquePtr();
739 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
740 return ast->UnsignedShortTy.getAsOpaquePtr();
741 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
742 return ast->UnsignedIntTy.getAsOpaquePtr();
743 break;
744
745 case DW_ATE_lo_user:
746 // This has been seen to mean DW_AT_complex_integer
747 if (type_name)
Greg Clayton605684e2011-10-28 23:06:08 +0000748 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000749 if (::strstr(type_name, "complex"))
750 {
751 clang_type_t complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2);
752 return ast->getComplexType (QualType::getFromOpaquePtr(complex_int_clang_type)).getAsOpaquePtr();
753 }
Greg Clayton605684e2011-10-28 23:06:08 +0000754 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000755 break;
756
757 case DW_ATE_complex_float:
758 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
759 return ast->FloatComplexTy.getAsOpaquePtr();
760 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
761 return ast->DoubleComplexTy.getAsOpaquePtr();
762 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
763 return ast->LongDoubleComplexTy.getAsOpaquePtr();
764 else
Greg Clayton605684e2011-10-28 23:06:08 +0000765 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000766 clang_type_t complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2);
767 return ast->getComplexType (QualType::getFromOpaquePtr(complex_float_clang_type)).getAsOpaquePtr();
Greg Clayton605684e2011-10-28 23:06:08 +0000768 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000769 break;
770
771 case DW_ATE_float:
772 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
773 return ast->FloatTy.getAsOpaquePtr();
774 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
775 return ast->DoubleTy.getAsOpaquePtr();
776 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
777 return ast->LongDoubleTy.getAsOpaquePtr();
778 break;
779
780 case DW_ATE_signed:
781 if (type_name)
782 {
783 if (streq(type_name, "wchar_t") &&
784 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
785 return ast->WCharTy.getAsOpaquePtr();
786 if (streq(type_name, "void") &&
787 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
788 return ast->VoidTy.getAsOpaquePtr();
789 if (strstr(type_name, "long long") &&
790 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
791 return ast->LongLongTy.getAsOpaquePtr();
792 if (strstr(type_name, "long") &&
793 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
794 return ast->LongTy.getAsOpaquePtr();
795 if (strstr(type_name, "short") &&
796 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
797 return ast->ShortTy.getAsOpaquePtr();
798 if (strstr(type_name, "char"))
799 {
800 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
801 return ast->CharTy.getAsOpaquePtr();
802 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
803 return ast->SignedCharTy.getAsOpaquePtr();
804 }
805 if (strstr(type_name, "int"))
806 {
807 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
808 return ast->IntTy.getAsOpaquePtr();
809 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
810 return ast->Int128Ty.getAsOpaquePtr();
811 }
812 }
813 // We weren't able to match up a type name, just search by size
814 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
815 return ast->CharTy.getAsOpaquePtr();
816 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
817 return ast->ShortTy.getAsOpaquePtr();
818 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
819 return ast->IntTy.getAsOpaquePtr();
820 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
821 return ast->LongTy.getAsOpaquePtr();
822 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
823 return ast->LongLongTy.getAsOpaquePtr();
824 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
825 return ast->Int128Ty.getAsOpaquePtr();
826 break;
827
828 case DW_ATE_signed_char:
829 if (type_name)
830 {
831 if (streq(type_name, "signed char"))
832 {
833 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
834 return ast->SignedCharTy.getAsOpaquePtr();
835 }
836 }
837 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
838 return ast->CharTy.getAsOpaquePtr();
839 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
840 return ast->SignedCharTy.getAsOpaquePtr();
841 break;
842
843 case DW_ATE_unsigned:
844 if (type_name)
845 {
846 if (strstr(type_name, "long long"))
847 {
848 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
849 return ast->UnsignedLongLongTy.getAsOpaquePtr();
850 }
851 else if (strstr(type_name, "long"))
852 {
853 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
854 return ast->UnsignedLongTy.getAsOpaquePtr();
855 }
856 else if (strstr(type_name, "short"))
857 {
858 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
859 return ast->UnsignedShortTy.getAsOpaquePtr();
860 }
861 else if (strstr(type_name, "char"))
862 {
863 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
864 return ast->UnsignedCharTy.getAsOpaquePtr();
865 }
866 else if (strstr(type_name, "int"))
867 {
868 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
869 return ast->UnsignedIntTy.getAsOpaquePtr();
870 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
871 return ast->UnsignedInt128Ty.getAsOpaquePtr();
872 }
873 }
874 // We weren't able to match up a type name, just search by size
875 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
876 return ast->UnsignedCharTy.getAsOpaquePtr();
877 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
878 return ast->UnsignedShortTy.getAsOpaquePtr();
879 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
880 return ast->UnsignedIntTy.getAsOpaquePtr();
881 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
882 return ast->UnsignedLongTy.getAsOpaquePtr();
883 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
884 return ast->UnsignedLongLongTy.getAsOpaquePtr();
885 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
886 return ast->UnsignedInt128Ty.getAsOpaquePtr();
887 break;
888
889 case DW_ATE_unsigned_char:
890 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
891 return ast->UnsignedCharTy.getAsOpaquePtr();
892 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
893 return ast->UnsignedShortTy.getAsOpaquePtr();
894 break;
895
896 case DW_ATE_imaginary_float:
897 break;
898
899 case DW_ATE_UTF:
900 if (type_name)
901 {
902 if (streq(type_name, "char16_t"))
903 {
904 return ast->Char16Ty.getAsOpaquePtr();
905 }
906 else if (streq(type_name, "char32_t"))
907 {
908 return ast->Char32Ty.getAsOpaquePtr();
909 }
910 }
911 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000912 }
913 }
914 // This assert should fire for anything that we don't catch above so we know
915 // to fix any issues we run into.
Greg Claytondc968d12011-05-17 18:15:05 +0000916 if (type_name)
917 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000918 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 +0000919 }
920 else
921 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000922 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 +0000923 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000924 return NULL;
925}
926
Greg Clayton1be10fc2010-09-29 01:12:09 +0000927clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000928ClangASTContext::GetBuiltInType_void(ASTContext *ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000929{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000930 return ast->VoidTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000931}
932
Greg Clayton1be10fc2010-09-29 01:12:09 +0000933clang_type_t
Sean Callananf7c3e272010-11-19 02:52:21 +0000934ClangASTContext::GetBuiltInType_bool()
935{
936 return getASTContext()->BoolTy.getAsOpaquePtr();
937}
938
939clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000940ClangASTContext::GetBuiltInType_objc_id()
941{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000942 return getASTContext()->getObjCIdType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000943}
944
Greg Clayton1be10fc2010-09-29 01:12:09 +0000945clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000946ClangASTContext::GetBuiltInType_objc_Class()
947{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000948 return getASTContext()->getObjCClassType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000949}
950
Greg Clayton1be10fc2010-09-29 01:12:09 +0000951clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000952ClangASTContext::GetBuiltInType_objc_selector()
953{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000954 return getASTContext()->getObjCSelType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000955}
956
Greg Clayton1be10fc2010-09-29 01:12:09 +0000957clang_type_t
Sean Callanan77502262011-05-12 23:54:16 +0000958ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast)
959{
960 return ast->UnknownAnyTy.getAsOpaquePtr();
961}
962
963clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000964ClangASTContext::GetCStringType (bool is_const)
965{
966 QualType char_type(getASTContext()->CharTy);
967
968 if (is_const)
969 char_type.addConst();
970
971 return getASTContext()->getPointerType(char_type).getAsOpaquePtr();
972}
973
Greg Clayton1be10fc2010-09-29 01:12:09 +0000974clang_type_t
Sean Callanana6582262012-04-05 00:12:52 +0000975ClangASTContext::GetVoidType()
976{
977 return GetVoidType(getASTContext());
978}
979
980clang_type_t
981ClangASTContext::GetVoidType(ASTContext *ast)
982{
983 return ast->VoidTy.getAsOpaquePtr();
984}
985
986clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000987ClangASTContext::GetVoidPtrType (bool is_const)
988{
989 return GetVoidPtrType(getASTContext(), is_const);
990}
991
Greg Clayton1be10fc2010-09-29 01:12:09 +0000992clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000993ClangASTContext::GetVoidPtrType (ASTContext *ast, bool is_const)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000994{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000995 QualType void_ptr_type(ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996
997 if (is_const)
998 void_ptr_type.addConst();
999
1000 return void_ptr_type.getAsOpaquePtr();
1001}
1002
Sean Callanan09ab4b72011-11-30 22:11:59 +00001003clang::DeclContext *
1004ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast)
1005{
1006 return ast->getTranslationUnitDecl();
1007}
1008
Greg Clayton1be10fc2010-09-29 01:12:09 +00001009clang_type_t
Greg Clayton38a61402010-12-02 23:20:03 +00001010ClangASTContext::CopyType (ASTContext *dst_ast,
1011 ASTContext *src_ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001012 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001013{
Sean Callanan79439e82010-11-18 02:56:27 +00001014 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001015 FileManager file_manager (file_system_options);
1016 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001017 *src_ast, file_manager,
1018 false);
Sean Callanan0617fcb2010-11-09 22:37:10 +00001019
Greg Clayton38a61402010-12-02 23:20:03 +00001020 QualType src (QualType::getFromOpaquePtr(clang_type));
1021 QualType dst (importer.Import(src));
Sean Callanan0617fcb2010-11-09 22:37:10 +00001022
1023 return dst.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001024}
1025
Greg Clayton526e5af2010-11-13 03:52:47 +00001026
1027clang::Decl *
Greg Clayton38a61402010-12-02 23:20:03 +00001028ClangASTContext::CopyDecl (ASTContext *dst_ast,
1029 ASTContext *src_ast,
Greg Clayton526e5af2010-11-13 03:52:47 +00001030 clang::Decl *source_decl)
Sean Callanan7fddd4c2010-12-11 00:08:56 +00001031{
Sean Callanan79439e82010-11-18 02:56:27 +00001032 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001033 FileManager file_manager (file_system_options);
1034 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001035 *src_ast, file_manager,
1036 false);
Greg Clayton526e5af2010-11-13 03:52:47 +00001037
1038 return importer.Import(source_decl);
1039}
1040
Sean Callanan23a30272010-07-16 00:00:27 +00001041bool
Greg Clayton84db9102012-03-26 23:03:23 +00001042ClangASTContext::AreTypesSame (ASTContext *ast,
1043 clang_type_t type1,
1044 clang_type_t type2,
1045 bool ignore_qualifiers)
Sean Callanan4dcca2622010-07-15 22:30:52 +00001046{
Greg Clayton55995eb2012-04-06 17:38:55 +00001047 if (type1 == type2)
1048 return true;
1049
Sean Callanan5056ab02012-02-18 02:01:03 +00001050 QualType type1_qual = QualType::getFromOpaquePtr(type1);
1051 QualType type2_qual = QualType::getFromOpaquePtr(type2);
1052
1053 if (ignore_qualifiers)
1054 {
1055 type1_qual = type1_qual.getUnqualifiedType();
1056 type2_qual = type2_qual.getUnqualifiedType();
1057 }
1058
1059 return ast->hasSameType (type1_qual,
1060 type2_qual);
Sean Callanan4dcca2622010-07-15 22:30:52 +00001061}
1062
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001063#pragma mark CVR modifiers
1064
Greg Clayton1be10fc2010-09-29 01:12:09 +00001065clang_type_t
1066ClangASTContext::AddConstModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001067{
1068 if (clang_type)
1069 {
1070 QualType result(QualType::getFromOpaquePtr(clang_type));
1071 result.addConst();
1072 return result.getAsOpaquePtr();
1073 }
1074 return NULL;
1075}
1076
Greg Clayton1be10fc2010-09-29 01:12:09 +00001077clang_type_t
1078ClangASTContext::AddRestrictModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001079{
1080 if (clang_type)
1081 {
1082 QualType result(QualType::getFromOpaquePtr(clang_type));
1083 result.getQualifiers().setRestrict (true);
1084 return result.getAsOpaquePtr();
1085 }
1086 return NULL;
1087}
1088
Greg Clayton1be10fc2010-09-29 01:12:09 +00001089clang_type_t
1090ClangASTContext::AddVolatileModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001091{
1092 if (clang_type)
1093 {
1094 QualType result(QualType::getFromOpaquePtr(clang_type));
1095 result.getQualifiers().setVolatile (true);
1096 return result.getAsOpaquePtr();
1097 }
1098 return NULL;
1099}
1100
Greg Clayton6beaaa62011-01-17 03:46:26 +00001101
1102clang_type_t
1103ClangASTContext::GetTypeForDecl (TagDecl *decl)
1104{
1105 // No need to call the getASTContext() accessor (which can create the AST
1106 // if it isn't created yet, because we can't have created a decl in this
1107 // AST if our AST didn't already exist...
1108 if (m_ast_ap.get())
1109 return m_ast_ap->getTagDeclType(decl).getAsOpaquePtr();
1110 return NULL;
1111}
1112
1113clang_type_t
1114ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1115{
1116 // No need to call the getASTContext() accessor (which can create the AST
1117 // if it isn't created yet, because we can't have created a decl in this
1118 // AST if our AST didn't already exist...
1119 if (m_ast_ap.get())
1120 return m_ast_ap->getObjCInterfaceType(decl).getAsOpaquePtr();
1121 return NULL;
1122}
1123
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001124#pragma mark Structure, Unions, Classes
1125
Greg Clayton1be10fc2010-09-29 01:12:09 +00001126clang_type_t
Jim Ingham379397632012-10-27 02:54:13 +00001127ClangASTContext::CreateRecordType (DeclContext *decl_ctx, AccessType access_type, const char *name, int kind, LanguageType language, ClangASTMetadata *metadata)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001128{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001129 ASTContext *ast = getASTContext();
1130 assert (ast != NULL);
Sean Callananad880762012-04-18 01:06:17 +00001131
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001132 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001133 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134
Greg Clayton9e409562010-07-28 02:04:09 +00001135
Greg Claytone1be9962011-08-24 23:50:00 +00001136 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
Greg Clayton9e409562010-07-28 02:04:09 +00001137 {
Greg Claytonaaf99e02010-10-11 02:25:34 +00001138 bool isForwardDecl = true;
Greg Clayton9e409562010-07-28 02:04:09 +00001139 bool isInternal = false;
Sean Callananad880762012-04-18 01:06:17 +00001140 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal, metadata);
Greg Clayton9e409562010-07-28 02:04:09 +00001141 }
1142
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001143 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1144 // we will need to update this code. I was told to currently always use
1145 // the CXXRecordDecl class since we often don't know from debug information
1146 // if something is struct or a class, so we default to always use the more
1147 // complete definition just in case.
Greg Claytonf0705c82011-10-22 03:33:13 +00001148 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1149 (TagDecl::TagKind)kind,
1150 decl_ctx,
1151 SourceLocation(),
1152 SourceLocation(),
1153 name && name[0] ? &ast->Idents.get(name) : NULL);
Sean Callanan7282e2a2012-01-13 22:10:18 +00001154
Jim Ingham379397632012-10-27 02:54:13 +00001155 if (decl && metadata)
1156 SetMetadata(ast, (uintptr_t)decl, *metadata);
Sean Callanan60217122012-04-13 00:10:03 +00001157
Greg Clayton55561e92011-10-26 03:31:36 +00001158 if (decl_ctx)
1159 {
1160 if (access_type != eAccessNone)
1161 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
1162 decl_ctx->addDecl (decl);
1163 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001164 return ast->getTagDeclType(decl).getAsOpaquePtr();
1165}
1166
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001167static TemplateParameterList *
1168CreateTemplateParameterList (ASTContext *ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001169 const ClangASTContext::TemplateParameterInfos &template_param_infos,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001170 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1171{
1172 const bool parameter_pack = false;
1173 const bool is_typename = false;
1174 const unsigned depth = 0;
1175 const size_t num_template_params = template_param_infos.GetSize();
1176 for (size_t i=0; i<num_template_params; ++i)
1177 {
1178 const char *name = template_param_infos.names[i];
Sean Callanan3d654b32012-09-24 22:25:51 +00001179 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001180 {
1181 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1182 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1183 SourceLocation(),
1184 SourceLocation(),
1185 depth,
1186 i,
1187 &ast->Idents.get(name),
1188 template_param_infos.args[i].getIntegralType(),
1189 parameter_pack,
1190 NULL));
1191
1192 }
1193 else
1194 {
1195 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1196 ast->getTranslationUnitDecl(), // Is this the right decl context?
1197 SourceLocation(),
1198 SourceLocation(),
1199 depth,
1200 i,
1201 &ast->Idents.get(name),
1202 is_typename,
1203 parameter_pack));
1204 }
1205 }
1206
1207 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1208 SourceLocation(),
1209 SourceLocation(),
1210 &template_param_decls.front(),
1211 template_param_decls.size(),
1212 SourceLocation());
1213 return template_param_list;
1214}
1215
1216clang::FunctionTemplateDecl *
1217ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1218 clang::FunctionDecl *func_decl,
1219 const char *name,
1220 const TemplateParameterInfos &template_param_infos)
1221{
1222// /// \brief Create a function template node.
1223 ASTContext *ast = getASTContext();
1224
1225 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1226
1227 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1228 template_param_infos,
1229 template_param_decls);
1230 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1231 decl_ctx,
1232 func_decl->getLocation(),
1233 func_decl->getDeclName(),
1234 template_param_list,
1235 func_decl);
1236
1237 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1238 i < template_param_decl_count;
1239 ++i)
1240 {
1241 // TODO: verify which decl context we should put template_param_decls into..
1242 template_param_decls[i]->setDeclContext (func_decl);
1243 }
1244
1245 return func_tmpl_decl;
1246}
1247
1248void
1249ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1250 clang::FunctionTemplateDecl *func_tmpl_decl,
1251 const TemplateParameterInfos &infos)
1252{
1253 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1254 infos.args.data(),
1255 infos.args.size());
1256
1257 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1258 &template_args,
1259 NULL);
1260}
1261
1262
Greg Claytonf0705c82011-10-22 03:33:13 +00001263ClassTemplateDecl *
1264ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001265 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001266 const char *class_name,
1267 int kind,
1268 const TemplateParameterInfos &template_param_infos)
1269{
1270 ASTContext *ast = getASTContext();
1271
1272 ClassTemplateDecl *class_template_decl = NULL;
1273 if (decl_ctx == NULL)
1274 decl_ctx = ast->getTranslationUnitDecl();
1275
1276 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1277 DeclarationName decl_name (&identifier_info);
1278
1279 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001280
1281 for (NamedDecl *decl : result)
Greg Claytonf0705c82011-10-22 03:33:13 +00001282 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001283 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001284 if (class_template_decl)
1285 return class_template_decl;
1286 }
1287
1288 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Claytonf0705c82011-10-22 03:33:13 +00001289
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001290 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1291 template_param_infos,
1292 template_param_decls);
Greg Claytonf0705c82011-10-22 03:33:13 +00001293
1294 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1295 (TagDecl::TagKind)kind,
1296 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1297 SourceLocation(),
1298 SourceLocation(),
1299 &identifier_info);
Greg Claytone04741d2011-12-02 02:09:28 +00001300
1301 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1302 i < template_param_decl_count;
1303 ++i)
1304 {
1305 template_param_decls[i]->setDeclContext (template_cxx_decl);
1306 }
1307
Sean Callananb5c79622011-11-19 01:35:08 +00001308 // With templated classes, we say that a class is templated with
1309 // specializations, but that the bare class has no functions.
1310 template_cxx_decl->startDefinition();
1311 template_cxx_decl->completeDefinition();
1312
Greg Claytonf0705c82011-10-22 03:33:13 +00001313 class_template_decl = ClassTemplateDecl::Create (*ast,
1314 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1315 SourceLocation(),
1316 decl_name,
1317 template_param_list,
1318 template_cxx_decl,
1319 NULL);
1320
1321 if (class_template_decl)
Sean Callanan5e9e1992011-10-26 01:06:27 +00001322 {
Greg Clayton55561e92011-10-26 03:31:36 +00001323 if (access_type != eAccessNone)
1324 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Sean Callanan5b26f272012-02-04 08:49:35 +00001325
1326 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1327 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1328
Greg Claytonf0705c82011-10-22 03:33:13 +00001329 decl_ctx->addDecl (class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001330
1331#ifdef LLDB_CONFIGURATION_DEBUG
1332 VerifyDecl(class_template_decl);
1333#endif
1334 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001335
1336 return class_template_decl;
1337}
1338
1339
1340ClassTemplateSpecializationDecl *
1341ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1342 ClassTemplateDecl *class_template_decl,
1343 int kind,
1344 const TemplateParameterInfos &template_param_infos)
1345{
1346 ASTContext *ast = getASTContext();
1347 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1348 (TagDecl::TagKind)kind,
1349 decl_ctx,
1350 SourceLocation(),
1351 SourceLocation(),
1352 class_template_decl,
1353 &template_param_infos.args.front(),
1354 template_param_infos.args.size(),
1355 NULL);
1356
1357 return class_template_specialization_decl;
1358}
1359
1360lldb::clang_type_t
1361ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1362{
1363 if (class_template_specialization_decl)
1364 {
1365 ASTContext *ast = getASTContext();
1366 if (ast)
1367 return ast->getTagDeclType(class_template_specialization_decl).getAsOpaquePtr();
1368 }
1369 return NULL;
1370}
1371
Greg Clayton6beaaa62011-01-17 03:46:26 +00001372bool
1373ClangASTContext::SetHasExternalStorage (clang_type_t clang_type, bool has_extern)
1374{
1375 if (clang_type == NULL)
1376 return false;
1377
1378 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
1379
1380 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
1381 switch (type_class)
1382 {
1383 case clang::Type::Record:
1384 {
1385 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
1386 if (cxx_record_decl)
1387 {
1388 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001389 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001390 return true;
1391 }
1392 }
1393 break;
1394
1395 case clang::Type::Enum:
1396 {
1397 EnumDecl *enum_decl = cast<EnumType>(qual_type)->getDecl();
1398 if (enum_decl)
1399 {
1400 enum_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001401 enum_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001402 return true;
1403 }
1404 }
1405 break;
1406
1407 case clang::Type::ObjCObject:
1408 case clang::Type::ObjCInterface:
1409 {
Sean Callanan78e37602011-01-27 04:42:51 +00001410 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001411 assert (objc_class_type);
1412 if (objc_class_type)
1413 {
1414 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1415
1416 if (class_interface_decl)
1417 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001418 class_interface_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001419 class_interface_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001420 return true;
1421 }
1422 }
1423 }
1424 break;
1425
1426 case clang::Type::Typedef:
1427 return ClangASTContext::SetHasExternalStorage (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
Sean Callanan912855f2011-08-11 23:56:13 +00001428
1429 case clang::Type::Elaborated:
1430 return ClangASTContext::SetHasExternalStorage (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001431
1432 default:
1433 break;
1434 }
1435 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001436}
1437
Greg Claytona3c444a2010-10-01 23:13:49 +00001438static bool
1439IsOperator (const char *name, OverloadedOperatorKind &op_kind)
1440{
1441 if (name == NULL || name[0] == '\0')
1442 return false;
1443
Sean Callanana43f20d2010-12-10 19:51:54 +00001444#define OPERATOR_PREFIX "operator"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001445#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
Sean Callananbfeff8c2010-12-10 02:15:55 +00001446
1447 const char *post_op_name = NULL;
1448
Sean Callanana43f20d2010-12-10 19:51:54 +00001449 bool no_space = true;
Sean Callananbfeff8c2010-12-10 02:15:55 +00001450
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001451 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
Greg Claytona3c444a2010-10-01 23:13:49 +00001452 return false;
1453
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001454 post_op_name = name + OPERATOR_PREFIX_LENGTH;
1455
Sean Callanana43f20d2010-12-10 19:51:54 +00001456 if (post_op_name[0] == ' ')
1457 {
1458 post_op_name++;
1459 no_space = false;
1460 }
1461
1462#undef OPERATOR_PREFIX
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001463#undef OPERATOR_PREFIX_LENGTH
Sean Callanana43f20d2010-12-10 19:51:54 +00001464
Greg Claytona3c444a2010-10-01 23:13:49 +00001465 // This is an operator, set the overloaded operator kind to invalid
1466 // in case this is a conversion operator...
1467 op_kind = NUM_OVERLOADED_OPERATORS;
1468
1469 switch (post_op_name[0])
1470 {
Sean Callananbfeff8c2010-12-10 02:15:55 +00001471 default:
1472 if (no_space)
1473 return false;
1474 break;
Greg Claytona3c444a2010-10-01 23:13:49 +00001475 case 'n':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001476 if (no_space)
1477 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001478 if (strcmp (post_op_name, "new") == 0)
1479 op_kind = OO_New;
1480 else if (strcmp (post_op_name, "new[]") == 0)
1481 op_kind = OO_Array_New;
1482 break;
1483
1484 case 'd':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001485 if (no_space)
1486 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001487 if (strcmp (post_op_name, "delete") == 0)
1488 op_kind = OO_Delete;
1489 else if (strcmp (post_op_name, "delete[]") == 0)
1490 op_kind = OO_Array_Delete;
1491 break;
1492
1493 case '+':
1494 if (post_op_name[1] == '\0')
1495 op_kind = OO_Plus;
1496 else if (post_op_name[2] == '\0')
1497 {
1498 if (post_op_name[1] == '=')
1499 op_kind = OO_PlusEqual;
1500 else if (post_op_name[1] == '+')
1501 op_kind = OO_PlusPlus;
1502 }
1503 break;
1504
1505 case '-':
1506 if (post_op_name[1] == '\0')
1507 op_kind = OO_Minus;
1508 else if (post_op_name[2] == '\0')
1509 {
1510 switch (post_op_name[1])
1511 {
1512 case '=': op_kind = OO_MinusEqual; break;
1513 case '-': op_kind = OO_MinusMinus; break;
1514 case '>': op_kind = OO_Arrow; break;
1515 }
1516 }
1517 else if (post_op_name[3] == '\0')
1518 {
1519 if (post_op_name[2] == '*')
1520 op_kind = OO_ArrowStar; break;
1521 }
1522 break;
1523
1524 case '*':
1525 if (post_op_name[1] == '\0')
1526 op_kind = OO_Star;
1527 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1528 op_kind = OO_StarEqual;
1529 break;
1530
1531 case '/':
1532 if (post_op_name[1] == '\0')
1533 op_kind = OO_Slash;
1534 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1535 op_kind = OO_SlashEqual;
1536 break;
1537
1538 case '%':
1539 if (post_op_name[1] == '\0')
1540 op_kind = OO_Percent;
1541 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1542 op_kind = OO_PercentEqual;
1543 break;
1544
1545
1546 case '^':
1547 if (post_op_name[1] == '\0')
1548 op_kind = OO_Caret;
1549 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1550 op_kind = OO_CaretEqual;
1551 break;
1552
1553 case '&':
1554 if (post_op_name[1] == '\0')
1555 op_kind = OO_Amp;
1556 else if (post_op_name[2] == '\0')
1557 {
1558 switch (post_op_name[1])
1559 {
1560 case '=': op_kind = OO_AmpEqual; break;
1561 case '&': op_kind = OO_AmpAmp; break;
1562 }
1563 }
1564 break;
1565
1566 case '|':
1567 if (post_op_name[1] == '\0')
1568 op_kind = OO_Pipe;
1569 else if (post_op_name[2] == '\0')
1570 {
1571 switch (post_op_name[1])
1572 {
1573 case '=': op_kind = OO_PipeEqual; break;
1574 case '|': op_kind = OO_PipePipe; break;
1575 }
1576 }
1577 break;
1578
1579 case '~':
1580 if (post_op_name[1] == '\0')
1581 op_kind = OO_Tilde;
1582 break;
1583
1584 case '!':
1585 if (post_op_name[1] == '\0')
1586 op_kind = OO_Exclaim;
1587 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1588 op_kind = OO_ExclaimEqual;
1589 break;
1590
1591 case '=':
1592 if (post_op_name[1] == '\0')
1593 op_kind = OO_Equal;
1594 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1595 op_kind = OO_EqualEqual;
1596 break;
1597
1598 case '<':
1599 if (post_op_name[1] == '\0')
1600 op_kind = OO_Less;
1601 else if (post_op_name[2] == '\0')
1602 {
1603 switch (post_op_name[1])
1604 {
1605 case '<': op_kind = OO_LessLess; break;
1606 case '=': op_kind = OO_LessEqual; break;
1607 }
1608 }
1609 else if (post_op_name[3] == '\0')
1610 {
1611 if (post_op_name[2] == '=')
1612 op_kind = OO_LessLessEqual;
1613 }
1614 break;
1615
1616 case '>':
1617 if (post_op_name[1] == '\0')
1618 op_kind = OO_Greater;
1619 else if (post_op_name[2] == '\0')
1620 {
1621 switch (post_op_name[1])
1622 {
1623 case '>': op_kind = OO_GreaterGreater; break;
1624 case '=': op_kind = OO_GreaterEqual; break;
1625 }
1626 }
1627 else if (post_op_name[1] == '>' &&
1628 post_op_name[2] == '=' &&
1629 post_op_name[3] == '\0')
1630 {
1631 op_kind = OO_GreaterGreaterEqual;
1632 }
1633 break;
1634
1635 case ',':
1636 if (post_op_name[1] == '\0')
1637 op_kind = OO_Comma;
1638 break;
1639
1640 case '(':
1641 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
1642 op_kind = OO_Call;
1643 break;
1644
1645 case '[':
1646 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
1647 op_kind = OO_Subscript;
1648 break;
1649 }
1650
1651 return true;
1652}
Greg Clayton6beaaa62011-01-17 03:46:26 +00001653
Greg Clayton090d0982011-06-19 03:43:27 +00001654static inline bool
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001655check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
Greg Clayton090d0982011-06-19 03:43:27 +00001656{
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001657 // Special-case call since it can take any number of operands
1658 if(op_kind == OO_Call)
1659 return true;
1660
Greg Clayton090d0982011-06-19 03:43:27 +00001661 // The parameter count doens't include "this"
1662 if (num_params == 0)
1663 return unary;
1664 if (num_params == 1)
1665 return binary;
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001666 else
Greg Clayton090d0982011-06-19 03:43:27 +00001667 return false;
1668}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001669
Greg Clayton090d0982011-06-19 03:43:27 +00001670bool
1671ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1672{
Sean Callanan5b26f272012-02-04 08:49:35 +00001673 switch (op_kind)
1674 {
1675 default:
1676 break;
1677 // C++ standard allows any number of arguments to new/delete
1678 case OO_New:
1679 case OO_Array_New:
1680 case OO_Delete:
1681 case OO_Array_Delete:
1682 return true;
1683 }
1684
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001685#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 +00001686 switch (op_kind)
1687 {
1688#include "clang/Basic/OperatorKinds.def"
1689 default: break;
1690 }
1691 return false;
1692}
1693
Greg Claytona51ed9b2010-09-23 01:09:21 +00001694CXXMethodDecl *
Sean Callanan61da09b2010-09-17 02:58:26 +00001695ClangASTContext::AddMethodToCXXRecordType
1696(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001697 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001698 clang_type_t record_opaque_type,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001699 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001700 clang_type_t method_opaque_type,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001701 lldb::AccessType access,
Greg Clayton0fffff52010-09-24 05:15:53 +00001702 bool is_virtual,
1703 bool is_static,
Greg Claytonf51de672010-10-01 02:31:07 +00001704 bool is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00001705 bool is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00001706 bool is_attr_used,
1707 bool is_artificial
Greg Claytona51ed9b2010-09-23 01:09:21 +00001708)
Sean Callanan61da09b2010-09-17 02:58:26 +00001709{
Sean Callananfc55f5d2010-09-21 00:44:12 +00001710 if (!record_opaque_type || !method_opaque_type || !name)
Johnny Chend440bcc2010-09-28 16:10:54 +00001711 return NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001712
Greg Clayton6beaaa62011-01-17 03:46:26 +00001713 assert(ast);
Sean Callanan61da09b2010-09-17 02:58:26 +00001714
Greg Clayton6beaaa62011-01-17 03:46:26 +00001715 IdentifierTable *identifier_table = &ast->Idents;
Sean Callanan61da09b2010-09-17 02:58:26 +00001716
1717 assert(identifier_table);
1718
Sean Callananfc55f5d2010-09-21 00:44:12 +00001719 QualType record_qual_type(QualType::getFromOpaquePtr(record_opaque_type));
Greg Clayton0fffff52010-09-24 05:15:53 +00001720
Greg Clayton6beaaa62011-01-17 03:46:26 +00001721 CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
Sean Callanan61da09b2010-09-17 02:58:26 +00001722
Greg Clayton0fffff52010-09-24 05:15:53 +00001723 if (cxx_record_decl == NULL)
Greg Claytona51ed9b2010-09-23 01:09:21 +00001724 return NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001725
Greg Clayton0fffff52010-09-24 05:15:53 +00001726 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001727
Greg Claytonf51de672010-10-01 02:31:07 +00001728 CXXMethodDecl *cxx_method_decl = NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001729
Greg Claytonf51de672010-10-01 02:31:07 +00001730 DeclarationName decl_name (&identifier_table->get(name));
Greg Clayton878eaf12010-10-01 03:45:20 +00001731
Sean Callanan78e37602011-01-27 04:42:51 +00001732 const clang::FunctionType *function_Type = dyn_cast<FunctionType>(method_qual_type.getTypePtr());
Greg Clayton878eaf12010-10-01 03:45:20 +00001733
Greg Clayton90a2acd2010-10-02 01:40:05 +00001734 if (function_Type == NULL)
Greg Clayton878eaf12010-10-01 03:45:20 +00001735 return NULL;
1736
Sean Callanan78e37602011-01-27 04:42:51 +00001737 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(function_Type));
Greg Clayton878eaf12010-10-01 03:45:20 +00001738
1739 if (!method_function_prototype)
1740 return NULL;
1741
1742 unsigned int num_params = method_function_prototype->getNumArgs();
1743
Sean Callanandbb58392011-11-02 01:38:59 +00001744 CXXDestructorDecl *cxx_dtor_decl(NULL);
1745 CXXConstructorDecl *cxx_ctor_decl(NULL);
1746
Greg Clayton878eaf12010-10-01 03:45:20 +00001747 if (name[0] == '~')
Greg Claytonf51de672010-10-01 02:31:07 +00001748 {
Sean Callanandbb58392011-11-02 01:38:59 +00001749 cxx_dtor_decl = CXXDestructorDecl::Create (*ast,
1750 cxx_record_decl,
1751 SourceLocation(),
1752 DeclarationNameInfo (ast->DeclarationNames.getCXXDestructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1753 method_qual_type,
1754 NULL,
1755 is_inline,
1756 is_artificial);
1757 cxx_method_decl = cxx_dtor_decl;
Greg Clayton878eaf12010-10-01 03:45:20 +00001758 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001759 else if (decl_name == cxx_record_decl->getDeclName())
Greg Clayton878eaf12010-10-01 03:45:20 +00001760 {
Sean Callanandbb58392011-11-02 01:38:59 +00001761 cxx_ctor_decl = CXXConstructorDecl::Create (*ast,
1762 cxx_record_decl,
1763 SourceLocation(),
1764 DeclarationNameInfo (ast->DeclarationNames.getCXXConstructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1765 method_qual_type,
1766 NULL, // TypeSourceInfo *
1767 is_explicit,
1768 is_inline,
1769 is_artificial,
1770 false /*is_constexpr*/);
1771 cxx_method_decl = cxx_ctor_decl;
Greg Claytonf51de672010-10-01 02:31:07 +00001772 }
1773 else
Greg Clayton878eaf12010-10-01 03:45:20 +00001774 {
Greg Claytona3c444a2010-10-01 23:13:49 +00001775
1776 OverloadedOperatorKind op_kind = NUM_OVERLOADED_OPERATORS;
1777 if (IsOperator (name, op_kind))
Greg Clayton878eaf12010-10-01 03:45:20 +00001778 {
Greg Claytona3c444a2010-10-01 23:13:49 +00001779 if (op_kind != NUM_OVERLOADED_OPERATORS)
1780 {
Greg Clayton090d0982011-06-19 03:43:27 +00001781 // Check the number of operator parameters. Sometimes we have
1782 // seen bad DWARF that doesn't correctly describe operators and
1783 // if we try to create a methed and add it to the class, clang
1784 // will assert and crash, so we need to make sure things are
1785 // acceptable.
1786 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
1787 return NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001788 cxx_method_decl = CXXMethodDecl::Create (*ast,
Greg Clayton878eaf12010-10-01 03:45:20 +00001789 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001790 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001791 DeclarationNameInfo (ast->DeclarationNames.getCXXOperatorName (op_kind), SourceLocation()),
Greg Clayton878eaf12010-10-01 03:45:20 +00001792 method_qual_type,
1793 NULL, // TypeSourceInfo *
Greg Claytona3c444a2010-10-01 23:13:49 +00001794 is_static,
1795 SC_None,
Sean Callananfb0b7582011-03-15 00:17:19 +00001796 is_inline,
Sean Callanan880e6802011-10-07 23:18:13 +00001797 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001798 SourceLocation());
Greg Claytona3c444a2010-10-01 23:13:49 +00001799 }
1800 else if (num_params == 0)
1801 {
1802 // Conversion operators don't take params...
Greg Clayton6beaaa62011-01-17 03:46:26 +00001803 cxx_method_decl = CXXConversionDecl::Create (*ast,
Greg Claytona3c444a2010-10-01 23:13:49 +00001804 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001805 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001806 DeclarationNameInfo (ast->DeclarationNames.getCXXConversionFunctionName (ast->getCanonicalType (function_Type->getResultType())), SourceLocation()),
Greg Claytona3c444a2010-10-01 23:13:49 +00001807 method_qual_type,
1808 NULL, // TypeSourceInfo *
1809 is_inline,
Sean Callananfb0b7582011-03-15 00:17:19 +00001810 is_explicit,
Sean Callanan880e6802011-10-07 23:18:13 +00001811 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001812 SourceLocation());
Greg Claytona3c444a2010-10-01 23:13:49 +00001813 }
Greg Clayton878eaf12010-10-01 03:45:20 +00001814 }
Greg Claytona3c444a2010-10-01 23:13:49 +00001815
1816 if (cxx_method_decl == NULL)
Greg Clayton878eaf12010-10-01 03:45:20 +00001817 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001818 cxx_method_decl = CXXMethodDecl::Create (*ast,
Greg Clayton878eaf12010-10-01 03:45:20 +00001819 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001820 SourceLocation(),
Greg Claytona3c444a2010-10-01 23:13:49 +00001821 DeclarationNameInfo (decl_name, SourceLocation()),
Greg Clayton878eaf12010-10-01 03:45:20 +00001822 method_qual_type,
1823 NULL, // TypeSourceInfo *
1824 is_static,
1825 SC_None,
Sean Callananfb0b7582011-03-15 00:17:19 +00001826 is_inline,
Sean Callanan880e6802011-10-07 23:18:13 +00001827 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001828 SourceLocation());
Greg Clayton878eaf12010-10-01 03:45:20 +00001829 }
Greg Claytonf51de672010-10-01 02:31:07 +00001830 }
Greg Claytona3c444a2010-10-01 23:13:49 +00001831
Greg Clayton1be10fc2010-09-29 01:12:09 +00001832 AccessSpecifier access_specifier = ConvertAccessTypeToAccessSpecifier (access);
Greg Clayton0fffff52010-09-24 05:15:53 +00001833
1834 cxx_method_decl->setAccess (access_specifier);
1835 cxx_method_decl->setVirtualAsWritten (is_virtual);
Sean Callanane2ef6e32010-09-23 03:01:22 +00001836
Sean Callananc1b732d2011-11-01 18:07:13 +00001837 if (is_attr_used)
1838 cxx_method_decl->addAttr(::new (*ast) UsedAttr(SourceRange(), *ast));
1839
Sean Callananfc55f5d2010-09-21 00:44:12 +00001840 // Populate the method decl with parameter decls
Sean Callananfc55f5d2010-09-21 00:44:12 +00001841
Charles Davis8c444c42011-05-19 23:33:46 +00001842 llvm::SmallVector<ParmVarDecl *, 12> params;
Sean Callananfc55f5d2010-09-21 00:44:12 +00001843
1844 for (int param_index = 0;
1845 param_index < num_params;
1846 ++param_index)
1847 {
Charles Davis8c444c42011-05-19 23:33:46 +00001848 params.push_back (ParmVarDecl::Create (*ast,
1849 cxx_method_decl,
1850 SourceLocation(),
1851 SourceLocation(),
1852 NULL, // anonymous
1853 method_function_prototype->getArgType(param_index),
1854 NULL,
1855 SC_None,
1856 SC_None,
1857 NULL));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001858 }
1859
Sean Callanan880e6802011-10-07 23:18:13 +00001860 cxx_method_decl->setParams (ArrayRef<ParmVarDecl*>(params));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001861
Greg Clayton0fffff52010-09-24 05:15:53 +00001862 cxx_record_decl->addDecl (cxx_method_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001863
Greg Clayton8b867b42011-11-02 02:06:20 +00001864 // Sometimes the debug info will mention a constructor (default/copy/move),
1865 // destructor, or assignment operator (copy/move) but there won't be any
1866 // version of this in the code. So we check if the function was artificially
1867 // generated and if it is trivial and this lets the compiler/backend know
1868 // that it can inline the IR for these when it needs to and we can avoid a
1869 // "missing function" error when running expressions.
1870
Sean Callanandbb58392011-11-02 01:38:59 +00001871 if (is_artificial)
1872 {
Greg Clayton8b867b42011-11-02 02:06:20 +00001873 if (cxx_ctor_decl &&
1874 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
1875 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
1876 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
Sean Callanandbb58392011-11-02 01:38:59 +00001877 {
1878 cxx_ctor_decl->setDefaulted();
1879 cxx_ctor_decl->setTrivial(true);
1880 }
Greg Clayton8b867b42011-11-02 02:06:20 +00001881 else if (cxx_dtor_decl)
Sean Callanandbb58392011-11-02 01:38:59 +00001882 {
Greg Clayton8b867b42011-11-02 02:06:20 +00001883 if (cxx_record_decl->hasTrivialDestructor())
1884 {
1885 cxx_dtor_decl->setDefaulted();
1886 cxx_dtor_decl->setTrivial(true);
1887 }
1888 }
1889 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
1890 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
1891 {
1892 cxx_method_decl->setDefaulted();
1893 cxx_method_decl->setTrivial(true);
Sean Callanandbb58392011-11-02 01:38:59 +00001894 }
1895 }
1896
Sean Callanan5e9e1992011-10-26 01:06:27 +00001897#ifdef LLDB_CONFIGURATION_DEBUG
1898 VerifyDecl(cxx_method_decl);
1899#endif
Greg Claytonc432c192011-01-20 04:18:48 +00001900
1901// printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
1902// printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
1903// printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
1904// printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
1905// printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
1906// printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
1907// printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
1908// printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
1909// printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
Greg Claytona51ed9b2010-09-23 01:09:21 +00001910 return cxx_method_decl;
Sean Callanan61da09b2010-09-17 02:58:26 +00001911}
1912
Jim Inghame3ae82a2011-11-12 01:36:43 +00001913clang::FieldDecl *
Greg Clayton8cf05932010-07-22 18:30:50 +00001914ClangASTContext::AddFieldToRecordType
1915(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001916 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001917 clang_type_t record_clang_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00001918 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001919 clang_type_t field_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00001920 AccessType access,
1921 uint32_t bitfield_bit_size
1922)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001923{
1924 if (record_clang_type == NULL || field_type == NULL)
Jim Inghame3ae82a2011-11-12 01:36:43 +00001925 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001926
Jim Inghame3ae82a2011-11-12 01:36:43 +00001927 FieldDecl *field = NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001928 IdentifierTable *identifier_table = &ast->Idents;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001929
Greg Clayton6beaaa62011-01-17 03:46:26 +00001930 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001931 assert (identifier_table != NULL);
1932
1933 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
1934
Sean Callanan78e37602011-01-27 04:42:51 +00001935 const clang::Type *clang_type = record_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001936 if (clang_type)
1937 {
1938 const RecordType *record_type = dyn_cast<RecordType>(clang_type);
1939
1940 if (record_type)
1941 {
1942 RecordDecl *record_decl = record_type->getDecl();
1943
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001944 clang::Expr *bit_width = NULL;
1945 if (bitfield_bit_size != 0)
1946 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001947 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
1948 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001949 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001950 field = FieldDecl::Create (*ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001951 record_decl,
1952 SourceLocation(),
1953 SourceLocation(),
1954 name ? &identifier_table->get(name) : NULL, // Identifier
1955 QualType::getFromOpaquePtr(field_type), // Field type
1956 NULL, // TInfo *
1957 bit_width, // BitWidth
1958 false, // Mutable
1959 ICIS_NoInit); // HasInit
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001960
Sean Callanan5ed3ac12012-07-13 20:01:02 +00001961 if (!name) {
1962 // Determine whether this field corresponds to an anonymous
1963 // struct or union.
1964 if (const TagType *TagT = field->getType()->getAs<TagType>()) {
1965 if (RecordDecl *Rec = dyn_cast<RecordDecl>(TagT->getDecl()))
1966 if (!Rec->getDeclName()) {
1967 Rec->setAnonymousStructOrUnion(true);
1968 field->setImplicit();
1969
1970 }
1971 }
1972 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001973
Greg Clayton8cf05932010-07-22 18:30:50 +00001974 field->setAccess (ConvertAccessTypeToAccessSpecifier (access));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001975
1976 if (field)
1977 {
1978 record_decl->addDecl(field);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001979
1980#ifdef LLDB_CONFIGURATION_DEBUG
1981 VerifyDecl(field);
1982#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001983 }
1984 }
Greg Clayton9e409562010-07-28 02:04:09 +00001985 else
1986 {
Sean Callanan78e37602011-01-27 04:42:51 +00001987 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001988 if (objc_class_type)
1989 {
Greg Clayton0fffff52010-09-24 05:15:53 +00001990 bool is_synthesized = false;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001991 field = ClangASTContext::AddObjCClassIVar (ast,
Sean Callanan6e6a7c72010-09-16 20:01:08 +00001992 record_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001993 name,
1994 field_type,
1995 access,
1996 bitfield_bit_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00001997 is_synthesized);
Greg Clayton9e409562010-07-28 02:04:09 +00001998 }
1999 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002000 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002001 return field;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002002}
2003
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002004static clang::AccessSpecifier UnifyAccessSpecifiers (clang::AccessSpecifier lhs,
2005 clang::AccessSpecifier rhs)
2006{
2007 clang::AccessSpecifier ret = lhs;
2008
2009 // Make the access equal to the stricter of the field and the nested field's access
2010 switch (ret)
2011 {
2012 case clang::AS_none:
2013 break;
2014 case clang::AS_private:
2015 break;
2016 case clang::AS_protected:
2017 if (rhs == AS_private)
2018 ret = AS_private;
2019 break;
2020 case clang::AS_public:
2021 ret = rhs;
2022 break;
2023 }
2024
2025 return ret;
2026}
2027
2028void
2029ClangASTContext::BuildIndirectFields (clang::ASTContext *ast,
2030 lldb::clang_type_t record_clang_type)
2031{
2032 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
2033
2034 const RecordType *record_type = record_qual_type->getAs<RecordType>();
2035
2036 if (!record_type)
2037 return;
2038
2039 RecordDecl *record_decl = record_type->getDecl();
2040
2041 if (!record_decl)
2042 return;
2043
2044 typedef llvm::SmallVector <IndirectFieldDecl *, 1> IndirectFieldVector;
2045
2046 IndirectFieldVector indirect_fields;
Greg Clayton4ef877f2012-12-06 02:33:54 +00002047 RecordDecl::field_iterator field_pos;
2048 RecordDecl::field_iterator field_end_pos = record_decl->field_end();
2049 RecordDecl::field_iterator last_field_pos = field_end_pos;
2050 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; last_field_pos = field_pos++)
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002051 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00002052 if (field_pos->isAnonymousStructOrUnion())
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002053 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00002054 QualType field_qual_type = field_pos->getType();
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002055
2056 const RecordType *field_record_type = field_qual_type->getAs<RecordType>();
2057
2058 if (!field_record_type)
2059 continue;
2060
2061 RecordDecl *field_record_decl = field_record_type->getDecl();
2062
2063 if (!field_record_decl)
2064 continue;
2065
2066 for (RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
2067 di != de;
2068 ++di)
2069 {
2070 if (FieldDecl *nested_field_decl = dyn_cast<FieldDecl>(*di))
2071 {
2072 NamedDecl **chain = new (*ast) NamedDecl*[2];
Greg Clayton4ef877f2012-12-06 02:33:54 +00002073 chain[0] = *field_pos;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002074 chain[1] = nested_field_decl;
2075 IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*ast,
2076 record_decl,
2077 SourceLocation(),
2078 nested_field_decl->getIdentifier(),
2079 nested_field_decl->getType(),
2080 chain,
2081 2);
2082
Greg Clayton4ef877f2012-12-06 02:33:54 +00002083 indirect_field->setAccess(UnifyAccessSpecifiers(field_pos->getAccess(),
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002084 nested_field_decl->getAccess()));
2085
2086 indirect_fields.push_back(indirect_field);
2087 }
2088 else if (IndirectFieldDecl *nested_indirect_field_decl = dyn_cast<IndirectFieldDecl>(*di))
2089 {
2090 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
2091 NamedDecl **chain = new (*ast) NamedDecl*[nested_chain_size + 1];
Greg Clayton4ef877f2012-12-06 02:33:54 +00002092 chain[0] = *field_pos;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002093
2094 int chain_index = 1;
2095 for (IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
2096 nce = nested_indirect_field_decl->chain_end();
2097 nci < nce;
2098 ++nci)
2099 {
2100 chain[chain_index] = *nci;
2101 chain_index++;
2102 }
2103
2104 IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*ast,
2105 record_decl,
2106 SourceLocation(),
2107 nested_indirect_field_decl->getIdentifier(),
2108 nested_indirect_field_decl->getType(),
2109 chain,
2110 nested_chain_size + 1);
2111
Greg Clayton4ef877f2012-12-06 02:33:54 +00002112 indirect_field->setAccess(UnifyAccessSpecifiers(field_pos->getAccess(),
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002113 nested_indirect_field_decl->getAccess()));
2114
2115 indirect_fields.push_back(indirect_field);
2116 }
2117 }
2118 }
2119 }
2120
Greg Clayton4ef877f2012-12-06 02:33:54 +00002121 // Check the last field to see if it has an incomplete array type as its
2122 // last member and if it does, the tell the record decl about it
2123 if (last_field_pos != field_end_pos)
2124 {
2125 if (last_field_pos->getType()->isIncompleteArrayType())
2126 record_decl->hasFlexibleArrayMember();
2127 }
2128
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002129 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
2130 ifi < ife;
2131 ++ifi)
2132 {
2133 record_decl->addDecl(*ifi);
2134 }
2135}
2136
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002137bool
2138ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
2139{
2140 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
2141}
2142
2143bool
2144ClangASTContext::FieldIsBitfield
2145(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002146 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002147 FieldDecl* field,
2148 uint32_t& bitfield_bit_size
2149)
2150{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002151 if (ast == NULL || field == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002152 return false;
2153
2154 if (field->isBitField())
2155 {
2156 Expr* bit_width_expr = field->getBitWidth();
2157 if (bit_width_expr)
2158 {
2159 llvm::APSInt bit_width_apsint;
Greg Clayton6beaaa62011-01-17 03:46:26 +00002160 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002161 {
2162 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
2163 return true;
2164 }
2165 }
2166 }
2167 return false;
2168}
2169
2170bool
2171ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
2172{
2173 if (record_decl == NULL)
2174 return false;
2175
2176 if (!record_decl->field_empty())
2177 return true;
2178
2179 // No fields, lets check this is a CXX record and check the base classes
2180 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2181 if (cxx_record_decl)
2182 {
2183 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2184 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2185 base_class != base_class_end;
2186 ++base_class)
2187 {
2188 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2189 if (RecordHasFields(base_class_decl))
2190 return true;
2191 }
2192 }
2193 return false;
2194}
2195
2196void
Greg Clayton6beaaa62011-01-17 03:46:26 +00002197ClangASTContext::SetDefaultAccessForRecordFields (clang_type_t clang_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002198{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002199 if (clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002200 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002201 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
2202
Sean Callanan78e37602011-01-27 04:42:51 +00002203 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +00002204 if (record_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002205 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002206 RecordDecl *record_decl = record_type->getDecl();
2207 if (record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002208 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002209 uint32_t field_idx;
2210 RecordDecl::field_iterator field, field_end;
2211 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2212 field != field_end;
2213 ++field, ++field_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002214 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002215 // If no accessibility was assigned, assign the correct one
2216 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2217 field->setAccess ((AccessSpecifier)default_accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002218 }
2219 }
2220 }
2221 }
2222}
2223
2224#pragma mark C++ Base Classes
2225
2226CXXBaseSpecifier *
Greg Clayton1be10fc2010-09-29 01:12:09 +00002227ClangASTContext::CreateBaseClassSpecifier (clang_type_t base_class_type, AccessType access, bool is_virtual, bool base_of_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002228{
2229 if (base_class_type)
Greg Claytone6371122010-07-30 20:30:44 +00002230 return new CXXBaseSpecifier (SourceRange(),
2231 is_virtual,
2232 base_of_class,
2233 ConvertAccessTypeToAccessSpecifier (access),
Sean Callanan2c777c42011-01-18 23:32:05 +00002234 getASTContext()->CreateTypeSourceInfo (QualType::getFromOpaquePtr(base_class_type)),
2235 SourceLocation());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002236 return NULL;
2237}
2238
Greg Clayton0b42ac32010-07-02 01:29:13 +00002239void
2240ClangASTContext::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes)
2241{
2242 for (unsigned i=0; i<num_base_classes; ++i)
2243 {
2244 delete base_classes[i];
2245 base_classes[i] = NULL;
2246 }
2247}
2248
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002249bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002250ClangASTContext::SetBaseClassesForClassType (clang_type_t class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002251{
2252 if (class_clang_type)
2253 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002254 CXXRecordDecl *cxx_record_decl = QualType::getFromOpaquePtr(class_clang_type)->getAsCXXRecordDecl();
2255 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002256 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002257 cxx_record_decl->setBases(base_classes, num_base_classes);
2258 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002259 }
2260 }
2261 return false;
2262}
Greg Clayton8cf05932010-07-22 18:30:50 +00002263#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002264
Greg Clayton1be10fc2010-09-29 01:12:09 +00002265clang_type_t
Sean Callananad880762012-04-18 01:06:17 +00002266ClangASTContext::CreateObjCClass
Greg Clayton8cf05932010-07-22 18:30:50 +00002267(
2268 const char *name,
2269 DeclContext *decl_ctx,
2270 bool isForwardDecl,
Sean Callananad880762012-04-18 01:06:17 +00002271 bool isInternal,
Jim Ingham379397632012-10-27 02:54:13 +00002272 ClangASTMetadata *metadata
Greg Clayton8cf05932010-07-22 18:30:50 +00002273)
2274{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002275 ASTContext *ast = getASTContext();
2276 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00002277 assert (name && name[0]);
2278 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00002279 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00002280
2281 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
2282 // we will need to update this code. I was told to currently always use
2283 // the CXXRecordDecl class since we often don't know from debug information
2284 // if something is struct or a class, so we default to always use the more
2285 // complete definition just in case.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002286 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00002287 decl_ctx,
2288 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00002289 &ast->Idents.get(name),
Sean Callanan5b26f272012-02-04 08:49:35 +00002290 NULL,
Greg Clayton8cf05932010-07-22 18:30:50 +00002291 SourceLocation(),
Sean Callanan5b26f272012-02-04 08:49:35 +00002292 /*isForwardDecl,*/
Greg Clayton8cf05932010-07-22 18:30:50 +00002293 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00002294
Jim Ingham379397632012-10-27 02:54:13 +00002295 if (decl && metadata)
2296 SetMetadata(ast, (uintptr_t)decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00002297
Greg Clayton6beaaa62011-01-17 03:46:26 +00002298 return ast->getObjCInterfaceType(decl).getAsOpaquePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002299}
2300
2301bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002302ClangASTContext::SetObjCSuperClass (clang_type_t class_opaque_type, clang_type_t super_opaque_type)
Greg Clayton8cf05932010-07-22 18:30:50 +00002303{
2304 if (class_opaque_type && super_opaque_type)
2305 {
2306 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2307 QualType super_qual_type(QualType::getFromOpaquePtr(super_opaque_type));
Sean Callanan78e37602011-01-27 04:42:51 +00002308 const clang::Type *class_type = class_qual_type.getTypePtr();
2309 const clang::Type *super_type = super_qual_type.getTypePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002310 if (class_type && super_type)
2311 {
Sean Callanan78e37602011-01-27 04:42:51 +00002312 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
2313 const ObjCObjectType *objc_super_type = dyn_cast<ObjCObjectType>(super_type);
Greg Clayton8cf05932010-07-22 18:30:50 +00002314 if (objc_class_type && objc_super_type)
2315 {
2316 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2317 ObjCInterfaceDecl *super_interface_decl = objc_super_type->getInterface();
2318 if (class_interface_decl && super_interface_decl)
2319 {
2320 class_interface_decl->setSuperClass(super_interface_decl);
2321 return true;
2322 }
2323 }
2324 }
2325 }
2326 return false;
2327}
2328
2329
Jim Inghame3ae82a2011-11-12 01:36:43 +00002330FieldDecl *
Greg Clayton8cf05932010-07-22 18:30:50 +00002331ClangASTContext::AddObjCClassIVar
2332(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002333 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002334 clang_type_t class_opaque_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00002335 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002336 clang_type_t ivar_opaque_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00002337 AccessType access,
2338 uint32_t bitfield_bit_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00002339 bool is_synthesized
Greg Clayton8cf05932010-07-22 18:30:50 +00002340)
2341{
2342 if (class_opaque_type == NULL || ivar_opaque_type == NULL)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002343 return NULL;
Greg Clayton8cf05932010-07-22 18:30:50 +00002344
Jim Inghame3ae82a2011-11-12 01:36:43 +00002345 ObjCIvarDecl *field = NULL;
2346
Greg Clayton6beaaa62011-01-17 03:46:26 +00002347 IdentifierTable *identifier_table = &ast->Idents;
Greg Clayton8cf05932010-07-22 18:30:50 +00002348
Greg Clayton6beaaa62011-01-17 03:46:26 +00002349 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00002350 assert (identifier_table != NULL);
2351
2352 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2353
Sean Callanan78e37602011-01-27 04:42:51 +00002354 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002355 if (class_type)
2356 {
Sean Callanan78e37602011-01-27 04:42:51 +00002357 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton8cf05932010-07-22 18:30:50 +00002358
2359 if (objc_class_type)
2360 {
2361 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2362
2363 if (class_interface_decl)
2364 {
2365 clang::Expr *bit_width = NULL;
2366 if (bitfield_bit_size != 0)
2367 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002368 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
2369 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
Greg Clayton8cf05932010-07-22 18:30:50 +00002370 }
2371
Jim Inghame3ae82a2011-11-12 01:36:43 +00002372 field = ObjCIvarDecl::Create (*ast,
2373 class_interface_decl,
2374 SourceLocation(),
2375 SourceLocation(),
Greg Clayton88bc7f32012-11-06 00:20:41 +00002376 name ? &identifier_table->get(name) : NULL, // Identifier
Jim Inghame3ae82a2011-11-12 01:36:43 +00002377 QualType::getFromOpaquePtr(ivar_opaque_type), // Field type
2378 NULL, // TypeSourceInfo *
2379 ConvertAccessTypeToObjCIvarAccessControl (access),
2380 bit_width,
2381 is_synthesized);
Greg Clayton9e409562010-07-28 02:04:09 +00002382
2383 if (field)
2384 {
2385 class_interface_decl->addDecl(field);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002386
2387#ifdef LLDB_CONFIGURATION_DEBUG
2388 VerifyDecl(field);
2389#endif
2390
Jim Inghame3ae82a2011-11-12 01:36:43 +00002391 return field;
Greg Clayton9e409562010-07-28 02:04:09 +00002392 }
Greg Clayton8cf05932010-07-22 18:30:50 +00002393 }
2394 }
2395 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002396 return NULL;
2397}
2398
2399bool
2400ClangASTContext::AddObjCClassProperty
2401(
2402 ASTContext *ast,
2403 clang_type_t class_opaque_type,
2404 const char *property_name,
2405 clang_type_t property_opaque_type,
2406 ObjCIvarDecl *ivar_decl,
2407 const char *property_setter_name,
2408 const char *property_getter_name,
Sean Callananad880762012-04-18 01:06:17 +00002409 uint32_t property_attributes,
Jim Ingham379397632012-10-27 02:54:13 +00002410 ClangASTMetadata *metadata
Jim Inghame3ae82a2011-11-12 01:36:43 +00002411)
2412{
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002413 if (class_opaque_type == NULL || property_name == NULL || property_name[0] == '\0')
Jim Inghame3ae82a2011-11-12 01:36:43 +00002414 return false;
2415
2416 IdentifierTable *identifier_table = &ast->Idents;
2417
2418 assert (ast != NULL);
2419 assert (identifier_table != NULL);
2420
2421 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2422 const clang::Type *class_type = class_qual_type.getTypePtr();
2423 if (class_type)
2424 {
2425 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
2426
2427 if (objc_class_type)
2428 {
2429 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2430
Greg Clayton23f59502012-07-17 03:23:13 +00002431 clang_type_t property_opaque_type_to_access = NULL;
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002432
2433 if (property_opaque_type)
2434 property_opaque_type_to_access = property_opaque_type;
2435 else if (ivar_decl)
2436 property_opaque_type_to_access = ivar_decl->getType().getAsOpaquePtr();
2437
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002438 if (class_interface_decl && property_opaque_type_to_access)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002439 {
2440 clang::TypeSourceInfo *prop_type_source;
2441 if (ivar_decl)
2442 prop_type_source = ast->CreateTypeSourceInfo (ivar_decl->getType());
2443 else
2444 prop_type_source = ast->CreateTypeSourceInfo (QualType::getFromOpaquePtr(property_opaque_type));
2445
2446 ObjCPropertyDecl *property_decl = ObjCPropertyDecl::Create(*ast,
2447 class_interface_decl,
2448 SourceLocation(), // Source Location
2449 &identifier_table->get(property_name),
2450 SourceLocation(), //Source Location for AT
Sean Callanand5f33a82012-03-01 02:03:47 +00002451 SourceLocation(), //Source location for (
Jim Inghame3ae82a2011-11-12 01:36:43 +00002452 prop_type_source
2453 );
Sean Callananad880762012-04-18 01:06:17 +00002454
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002455 if (property_decl)
2456 {
Jim Ingham379397632012-10-27 02:54:13 +00002457 if (metadata)
2458 SetMetadata(ast, (uintptr_t)property_decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00002459
Jim Inghame3ae82a2011-11-12 01:36:43 +00002460 class_interface_decl->addDecl (property_decl);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002461
2462 Selector setter_sel, getter_sel;
2463
Jim Inghame3ae82a2011-11-12 01:36:43 +00002464 if (property_setter_name != NULL)
2465 {
2466 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
2467 clang::IdentifierInfo *setter_ident = &identifier_table->get(property_setter_no_colon.c_str());
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002468 setter_sel = ast->Selectors.getSelector(1, &setter_ident);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002469 }
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002470 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
2471 {
2472 std::string setter_sel_string("set");
2473 setter_sel_string.push_back(::toupper(property_name[0]));
2474 setter_sel_string.append(&property_name[1]);
2475 clang::IdentifierInfo *setter_ident = &identifier_table->get(setter_sel_string.c_str());
2476 setter_sel = ast->Selectors.getSelector(1, &setter_ident);
2477 }
Sean Callanana6582262012-04-05 00:12:52 +00002478 property_decl->setSetterName(setter_sel);
2479 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002480
2481 if (property_getter_name != NULL)
2482 {
2483 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_getter_name);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002484 getter_sel = ast->Selectors.getSelector(0, &getter_ident);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002485 }
2486 else
2487 {
2488 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_name);
2489 getter_sel = ast->Selectors.getSelector(0, &getter_ident);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002490 }
Sean Callanana6582262012-04-05 00:12:52 +00002491 property_decl->setGetterName(getter_sel);
2492 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002493
2494 if (ivar_decl)
2495 property_decl->setPropertyIvarDecl (ivar_decl);
2496
2497 if (property_attributes & DW_APPLE_PROPERTY_readonly)
2498 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
2499 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
2500 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
2501 if (property_attributes & DW_APPLE_PROPERTY_assign)
2502 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
2503 if (property_attributes & DW_APPLE_PROPERTY_retain)
2504 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
2505 if (property_attributes & DW_APPLE_PROPERTY_copy)
2506 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
2507 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
2508 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002509
2510 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
2511 {
2512 QualType result_type = QualType::getFromOpaquePtr(property_opaque_type_to_access);
2513
2514 const bool isInstance = true;
2515 const bool isVariadic = false;
2516 const bool isSynthesized = false;
2517 const bool isImplicitlyDeclared = true;
2518 const bool isDefined = false;
2519 const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
2520 const bool HasRelatedResultType = false;
2521
2522 ObjCMethodDecl *getter = ObjCMethodDecl::Create(*ast,
2523 SourceLocation(),
2524 SourceLocation(),
2525 getter_sel,
2526 result_type,
2527 NULL,
2528 class_interface_decl,
2529 isInstance,
2530 isVariadic,
2531 isSynthesized,
2532 isImplicitlyDeclared,
2533 isDefined,
2534 impControl,
2535 HasRelatedResultType);
Sean Callananad880762012-04-18 01:06:17 +00002536
Jim Ingham379397632012-10-27 02:54:13 +00002537 if (getter && metadata)
2538 SetMetadata(ast, (uintptr_t)getter, *metadata);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002539
2540 getter->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(), ArrayRef<SourceLocation>());
2541
2542 class_interface_decl->addDecl(getter);
2543 }
2544
2545 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
2546 {
2547 QualType result_type = ast->VoidTy;
2548
2549 const bool isInstance = true;
2550 const bool isVariadic = false;
2551 const bool isSynthesized = false;
2552 const bool isImplicitlyDeclared = true;
2553 const bool isDefined = false;
2554 const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
2555 const bool HasRelatedResultType = false;
2556
2557 ObjCMethodDecl *setter = ObjCMethodDecl::Create(*ast,
2558 SourceLocation(),
2559 SourceLocation(),
2560 setter_sel,
2561 result_type,
2562 NULL,
2563 class_interface_decl,
2564 isInstance,
2565 isVariadic,
2566 isSynthesized,
2567 isImplicitlyDeclared,
2568 isDefined,
2569 impControl,
2570 HasRelatedResultType);
2571
Jim Ingham379397632012-10-27 02:54:13 +00002572 if (setter && metadata)
2573 SetMetadata(ast, (uintptr_t)setter, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00002574
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002575 llvm::SmallVector<ParmVarDecl *, 1> params;
2576
2577 params.push_back (ParmVarDecl::Create (*ast,
2578 setter,
2579 SourceLocation(),
2580 SourceLocation(),
2581 NULL, // anonymous
2582 QualType::getFromOpaquePtr(property_opaque_type_to_access),
2583 NULL,
2584 SC_Auto,
2585 SC_Auto,
2586 NULL));
2587
2588 setter->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
2589
2590 class_interface_decl->addDecl(setter);
2591 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002592
2593 return true;
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002594 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002595 }
2596 }
2597 }
Greg Clayton8cf05932010-07-22 18:30:50 +00002598 return false;
2599}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002600
Greg Clayton9e409562010-07-28 02:04:09 +00002601bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002602ClangASTContext::ObjCTypeHasIVars (clang_type_t class_opaque_type, bool check_superclass)
Greg Clayton9e409562010-07-28 02:04:09 +00002603{
2604 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2605
Sean Callanan78e37602011-01-27 04:42:51 +00002606 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton9e409562010-07-28 02:04:09 +00002607 if (class_type)
2608 {
Sean Callanan78e37602011-01-27 04:42:51 +00002609 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00002610
2611 if (objc_class_type)
2612 return ObjCDeclHasIVars (objc_class_type->getInterface(), check_superclass);
2613 }
2614 return false;
2615}
2616
2617bool
2618ClangASTContext::ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
2619{
2620 while (class_interface_decl)
2621 {
2622 if (class_interface_decl->ivar_size() > 0)
2623 return true;
2624
2625 if (check_superclass)
2626 class_interface_decl = class_interface_decl->getSuperClass();
2627 else
2628 break;
2629 }
2630 return false;
2631}
Greg Clayton0fffff52010-09-24 05:15:53 +00002632
Greg Clayton1be10fc2010-09-29 01:12:09 +00002633ObjCMethodDecl *
Greg Clayton0fffff52010-09-24 05:15:53 +00002634ClangASTContext::AddMethodToObjCObjectType
2635(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002636 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002637 clang_type_t class_opaque_type,
Greg Clayton0fffff52010-09-24 05:15:53 +00002638 const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
Greg Clayton1be10fc2010-09-29 01:12:09 +00002639 clang_type_t method_opaque_type,
Greg Clayton0fffff52010-09-24 05:15:53 +00002640 lldb::AccessType access
2641)
2642{
2643 if (class_opaque_type == NULL || method_opaque_type == NULL)
2644 return NULL;
2645
Greg Clayton6beaaa62011-01-17 03:46:26 +00002646 IdentifierTable *identifier_table = &ast->Idents;
Greg Clayton0fffff52010-09-24 05:15:53 +00002647
Greg Clayton6beaaa62011-01-17 03:46:26 +00002648 assert (ast != NULL);
Greg Clayton0fffff52010-09-24 05:15:53 +00002649 assert (identifier_table != NULL);
2650
2651 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2652
Sean Callanan78e37602011-01-27 04:42:51 +00002653 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton0fffff52010-09-24 05:15:53 +00002654 if (class_type == NULL)
2655 return NULL;
2656
Sean Callanan78e37602011-01-27 04:42:51 +00002657 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton0fffff52010-09-24 05:15:53 +00002658
2659 if (objc_class_type == NULL)
2660 return NULL;
2661
2662 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2663
2664 if (class_interface_decl == NULL)
2665 return NULL;
Greg Clayton9e409562010-07-28 02:04:09 +00002666
Greg Clayton0fffff52010-09-24 05:15:53 +00002667 const char *selector_start = ::strchr (name, ' ');
2668 if (selector_start == NULL)
2669 return NULL;
2670
2671 selector_start++;
2672 if (!(::isalpha (selector_start[0]) || selector_start[0] == '_'))
2673 return NULL;
2674 llvm::SmallVector<IdentifierInfo *, 12> selector_idents;
2675
Greg Clayton450e3f32010-10-12 02:24:53 +00002676 size_t len = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +00002677 const char *start;
Greg Clayton450e3f32010-10-12 02:24:53 +00002678 //printf ("name = '%s'\n", name);
2679
2680 unsigned num_selectors_with_args = 0;
2681 for (start = selector_start;
Greg Clayton0fffff52010-09-24 05:15:53 +00002682 start && *start != '\0' && *start != ']';
Greg Clayton450e3f32010-10-12 02:24:53 +00002683 start += len)
Greg Clayton0fffff52010-09-24 05:15:53 +00002684 {
Greg Clayton450e3f32010-10-12 02:24:53 +00002685 len = ::strcspn(start, ":]");
Greg Clayton90f90cd2010-10-27 04:01:14 +00002686 bool has_arg = (start[len] == ':');
2687 if (has_arg)
Greg Clayton450e3f32010-10-12 02:24:53 +00002688 ++num_selectors_with_args;
Greg Clayton0fffff52010-09-24 05:15:53 +00002689 selector_idents.push_back (&identifier_table->get (StringRef (start, len)));
Greg Clayton90f90cd2010-10-27 04:01:14 +00002690 if (has_arg)
2691 len += 1;
Greg Clayton0fffff52010-09-24 05:15:53 +00002692 }
2693
2694
2695 if (selector_idents.size() == 0)
2696 return 0;
2697
Greg Clayton6beaaa62011-01-17 03:46:26 +00002698 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
Greg Clayton0fffff52010-09-24 05:15:53 +00002699 selector_idents.data());
2700
2701 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
2702
2703 // Populate the method decl with parameter decls
Sean Callanan78e37602011-01-27 04:42:51 +00002704 const clang::Type *method_type(method_qual_type.getTypePtr());
Greg Clayton0fffff52010-09-24 05:15:53 +00002705
2706 if (method_type == NULL)
2707 return NULL;
2708
Sean Callanan78e37602011-01-27 04:42:51 +00002709 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type));
Greg Clayton0fffff52010-09-24 05:15:53 +00002710
2711 if (!method_function_prototype)
2712 return NULL;
2713
2714
2715 bool is_variadic = false;
2716 bool is_synthesized = false;
2717 bool is_defined = false;
2718 ObjCMethodDecl::ImplementationControl imp_control = ObjCMethodDecl::None;
2719
2720 const unsigned num_args = method_function_prototype->getNumArgs();
2721
Greg Clayton6beaaa62011-01-17 03:46:26 +00002722 ObjCMethodDecl *objc_method_decl = ObjCMethodDecl::Create (*ast,
Greg Clayton0fffff52010-09-24 05:15:53 +00002723 SourceLocation(), // beginLoc,
2724 SourceLocation(), // endLoc,
2725 method_selector,
2726 method_function_prototype->getResultType(),
2727 NULL, // TypeSourceInfo *ResultTInfo,
2728 GetDeclContextForType (class_opaque_type),
2729 name[0] == '-',
2730 is_variadic,
2731 is_synthesized,
Sean Callanan880e6802011-10-07 23:18:13 +00002732 true, // is_implicitly_declared
Greg Clayton0fffff52010-09-24 05:15:53 +00002733 is_defined,
2734 imp_control,
Sean Callanan880e6802011-10-07 23:18:13 +00002735 false /*has_related_result_type*/);
Greg Clayton0fffff52010-09-24 05:15:53 +00002736
2737
2738 if (objc_method_decl == NULL)
2739 return NULL;
2740
2741 if (num_args > 0)
2742 {
2743 llvm::SmallVector<ParmVarDecl *, 12> params;
2744
2745 for (int param_index = 0; param_index < num_args; ++param_index)
2746 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002747 params.push_back (ParmVarDecl::Create (*ast,
Greg Clayton0fffff52010-09-24 05:15:53 +00002748 objc_method_decl,
2749 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00002750 SourceLocation(),
Greg Clayton0fffff52010-09-24 05:15:53 +00002751 NULL, // anonymous
2752 method_function_prototype->getArgType(param_index),
2753 NULL,
2754 SC_Auto,
2755 SC_Auto,
2756 NULL));
2757 }
2758
Sean Callanan880e6802011-10-07 23:18:13 +00002759 objc_method_decl->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
Greg Clayton0fffff52010-09-24 05:15:53 +00002760 }
2761
2762 class_interface_decl->addDecl (objc_method_decl);
2763
Sean Callanan5e9e1992011-10-26 01:06:27 +00002764#ifdef LLDB_CONFIGURATION_DEBUG
2765 VerifyDecl(objc_method_decl);
2766#endif
Greg Clayton0fffff52010-09-24 05:15:53 +00002767
2768 return objc_method_decl;
2769}
2770
Greg Clayton402230e2012-02-03 01:30:30 +00002771size_t
2772ClangASTContext::GetNumTemplateArguments (clang::ASTContext *ast, clang_type_t clang_type)
2773{
2774 if (clang_type)
2775 {
2776 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2777
2778 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2779 switch (type_class)
2780 {
2781 case clang::Type::Record:
2782 if (GetCompleteQualType (ast, qual_type))
2783 {
2784 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2785 if (cxx_record_decl)
2786 {
2787 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2788 if (template_decl)
2789 return template_decl->getTemplateArgs().size();
2790 }
2791 }
2792 break;
2793
2794 case clang::Type::Typedef:
2795 return ClangASTContext::GetNumTemplateArguments (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2796 default:
2797 break;
2798 }
2799 }
2800 return 0;
2801}
2802
2803clang_type_t
2804ClangASTContext::GetTemplateArgument (clang::ASTContext *ast, clang_type_t clang_type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
2805{
2806 if (clang_type)
2807 {
2808 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2809
2810 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2811 switch (type_class)
2812 {
2813 case clang::Type::Record:
2814 if (GetCompleteQualType (ast, qual_type))
2815 {
2816 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2817 if (cxx_record_decl)
2818 {
2819 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2820 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
2821 {
2822 const TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
2823 switch (template_arg.getKind())
2824 {
2825 case clang::TemplateArgument::Null:
2826 kind = eTemplateArgumentKindNull;
2827 return NULL;
2828
2829 case clang::TemplateArgument::Type:
2830 kind = eTemplateArgumentKindType;
2831 return template_arg.getAsType().getAsOpaquePtr();
2832
2833 case clang::TemplateArgument::Declaration:
2834 kind = eTemplateArgumentKindDeclaration;
2835 return NULL;
2836
2837 case clang::TemplateArgument::Integral:
2838 kind = eTemplateArgumentKindIntegral;
2839 return template_arg.getIntegralType().getAsOpaquePtr();
2840
2841 case clang::TemplateArgument::Template:
2842 kind = eTemplateArgumentKindTemplate;
2843 return NULL;
2844
2845 case clang::TemplateArgument::TemplateExpansion:
2846 kind = eTemplateArgumentKindTemplateExpansion;
2847 return NULL;
2848
2849 case clang::TemplateArgument::Expression:
2850 kind = eTemplateArgumentKindExpression;
2851 return NULL;
2852
2853 case clang::TemplateArgument::Pack:
2854 kind = eTemplateArgumentKindPack;
2855 return NULL;
2856
2857 default:
2858 assert (!"Unhandled TemplateArgument::ArgKind");
2859 kind = eTemplateArgumentKindNull;
2860 return NULL;
2861 }
2862 }
2863 }
2864 }
2865 break;
2866
2867 case clang::Type::Typedef:
2868 return ClangASTContext::GetTemplateArgument (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), arg_idx, kind);
2869 default:
2870 break;
2871 }
2872 }
2873 kind = eTemplateArgumentKindNull;
2874 return NULL;
2875}
Greg Clayton0fffff52010-09-24 05:15:53 +00002876
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002877uint32_t
Greg Clayton73b472d2010-10-27 03:32:59 +00002878ClangASTContext::GetTypeInfo
2879(
2880 clang_type_t clang_type,
Greg Clayton6beaaa62011-01-17 03:46:26 +00002881 clang::ASTContext *ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00002882 clang_type_t *pointee_or_element_clang_type
2883)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002884{
2885 if (clang_type == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00002886 return 0;
2887
2888 if (pointee_or_element_clang_type)
2889 *pointee_or_element_clang_type = NULL;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002890
2891 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2892
2893 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2894 switch (type_class)
2895 {
Sean Callanana2424172010-10-25 00:29:48 +00002896 case clang::Type::Builtin:
2897 switch (cast<clang::BuiltinType>(qual_type)->getKind())
2898 {
Sean Callanana2424172010-10-25 00:29:48 +00002899 case clang::BuiltinType::ObjCId:
2900 case clang::BuiltinType::ObjCClass:
Greg Clayton6beaaa62011-01-17 03:46:26 +00002901 if (ast && pointee_or_element_clang_type)
2902 *pointee_or_element_clang_type = ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Sean Callanana2424172010-10-25 00:29:48 +00002903 return eTypeIsBuiltIn | eTypeIsPointer | eTypeHasValue;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002904 break;
2905 case clang::BuiltinType::Bool:
2906 case clang::BuiltinType::Char_U:
2907 case clang::BuiltinType::UChar:
2908 case clang::BuiltinType::WChar_U:
2909 case clang::BuiltinType::Char16:
2910 case clang::BuiltinType::Char32:
2911 case clang::BuiltinType::UShort:
2912 case clang::BuiltinType::UInt:
2913 case clang::BuiltinType::ULong:
2914 case clang::BuiltinType::ULongLong:
2915 case clang::BuiltinType::UInt128:
2916 case clang::BuiltinType::Char_S:
2917 case clang::BuiltinType::SChar:
2918 case clang::BuiltinType::WChar_S:
2919 case clang::BuiltinType::Short:
2920 case clang::BuiltinType::Int:
2921 case clang::BuiltinType::Long:
2922 case clang::BuiltinType::LongLong:
2923 case clang::BuiltinType::Int128:
2924 case clang::BuiltinType::Float:
2925 case clang::BuiltinType::Double:
2926 case clang::BuiltinType::LongDouble:
2927 return eTypeIsBuiltIn | eTypeHasValue | eTypeIsScalar;
Greg Clayton73b472d2010-10-27 03:32:59 +00002928 default:
2929 break;
Sean Callanana2424172010-10-25 00:29:48 +00002930 }
2931 return eTypeIsBuiltIn | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002932
2933 case clang::Type::BlockPointer:
2934 if (pointee_or_element_clang_type)
2935 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2936 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
2937
Greg Clayton49462ea2011-01-15 02:52:14 +00002938 case clang::Type::Complex: return eTypeIsBuiltIn | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002939
2940 case clang::Type::ConstantArray:
2941 case clang::Type::DependentSizedArray:
2942 case clang::Type::IncompleteArray:
2943 case clang::Type::VariableArray:
2944 if (pointee_or_element_clang_type)
2945 *pointee_or_element_clang_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
2946 return eTypeHasChildren | eTypeIsArray;
2947
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002948 case clang::Type::DependentName: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002949 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
2950 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
2951 case clang::Type::Decltype: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00002952
2953 case clang::Type::Enum:
2954 if (pointee_or_element_clang_type)
2955 *pointee_or_element_clang_type = cast<EnumType>(qual_type)->getDecl()->getIntegerType().getAsOpaquePtr();
2956 return eTypeIsEnumeration | eTypeHasValue;
2957
Sean Callanan912855f2011-08-11 23:56:13 +00002958 case clang::Type::Elaborated:
2959 return ClangASTContext::GetTypeInfo (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2960 ast,
2961 pointee_or_element_clang_type);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002962 case clang::Type::ExtVector: return eTypeHasChildren | eTypeIsVector;
2963 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
2964 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002965 case clang::Type::InjectedClassName: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00002966
2967 case clang::Type::LValueReference:
2968 case clang::Type::RValueReference:
2969 if (pointee_or_element_clang_type)
2970 *pointee_or_element_clang_type = cast<ReferenceType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr();
2971 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
2972
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002973 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002974
2975 case clang::Type::ObjCObjectPointer:
2976 if (pointee_or_element_clang_type)
2977 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2978 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
2979
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002980 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
2981 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
Greg Clayton73b472d2010-10-27 03:32:59 +00002982
2983 case clang::Type::Pointer:
2984 if (pointee_or_element_clang_type)
2985 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2986 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
2987
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002988 case clang::Type::Record:
2989 if (qual_type->getAsCXXRecordDecl())
2990 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
2991 else
2992 return eTypeHasChildren | eTypeIsStructUnion;
2993 break;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002994 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
2995 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
2996 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
Greg Clayton73b472d2010-10-27 03:32:59 +00002997
2998 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00002999 return eTypeIsTypedef | ClangASTContext::GetTypeInfo (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00003000 ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00003001 pointee_or_element_clang_type);
3002
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003003 case clang::Type::TypeOfExpr: return 0;
3004 case clang::Type::TypeOf: return 0;
3005 case clang::Type::UnresolvedUsing: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003006 case clang::Type::Vector: return eTypeHasChildren | eTypeIsVector;
3007 default: return 0;
3008 }
3009 return 0;
3010}
3011
Greg Clayton9e409562010-07-28 02:04:09 +00003012
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003013#pragma mark Aggregate Types
3014
3015bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00003016ClangASTContext::IsAggregateType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003017{
3018 if (clang_type == NULL)
3019 return false;
3020
3021 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3022
Greg Clayton737b9322010-09-13 03:32:57 +00003023 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3024 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003025 {
Greg Claytone1a916a2010-07-21 22:12:05 +00003026 case clang::Type::IncompleteArray:
3027 case clang::Type::VariableArray:
3028 case clang::Type::ConstantArray:
3029 case clang::Type::ExtVector:
3030 case clang::Type::Vector:
3031 case clang::Type::Record:
Greg Clayton9e409562010-07-28 02:04:09 +00003032 case clang::Type::ObjCObject:
3033 case clang::Type::ObjCInterface:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003034 return true;
Sean Callanan912855f2011-08-11 23:56:13 +00003035 case clang::Type::Elaborated:
3036 return ClangASTContext::IsAggregateType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Claytone1a916a2010-07-21 22:12:05 +00003037 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00003038 return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003039
3040 default:
3041 break;
3042 }
3043 // The clang type does have a value
3044 return false;
3045}
3046
3047uint32_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00003048ClangASTContext::GetNumChildren (clang::ASTContext *ast, clang_type_t clang_type, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003049{
Greg Clayton6beaaa62011-01-17 03:46:26 +00003050 if (clang_type == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003051 return 0;
3052
3053 uint32_t num_children = 0;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003054 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00003055 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3056 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003057 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003058 case clang::Type::Builtin:
3059 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3060 {
Greg Clayton73b472d2010-10-27 03:32:59 +00003061 case clang::BuiltinType::ObjCId: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003062 case clang::BuiltinType::ObjCClass: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003063 num_children = 1;
Greg Clayton73b472d2010-10-27 03:32:59 +00003064 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003065
3066 default:
3067 break;
3068 }
3069 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00003070
Greg Clayton49462ea2011-01-15 02:52:14 +00003071 case clang::Type::Complex: return 0;
Greg Clayton54979cd2010-12-15 05:08:08 +00003072
Greg Claytone1a916a2010-07-21 22:12:05 +00003073 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00003074 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003075 {
3076 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3077 const RecordDecl *record_decl = record_type->getDecl();
3078 assert(record_decl);
3079 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3080 if (cxx_record_decl)
3081 {
3082 if (omit_empty_base_classes)
3083 {
3084 // Check each base classes to see if it or any of its
3085 // base classes contain any fields. This can help
3086 // limit the noise in variable views by not having to
3087 // show base classes that contain no members.
3088 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3089 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3090 base_class != base_class_end;
3091 ++base_class)
3092 {
3093 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3094
3095 // Skip empty base classes
3096 if (RecordHasFields(base_class_decl) == false)
3097 continue;
3098
3099 num_children++;
3100 }
3101 }
3102 else
3103 {
3104 // Include all base classes
3105 num_children += cxx_record_decl->getNumBases();
3106 }
3107
3108 }
3109 RecordDecl::field_iterator field, field_end;
3110 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3111 ++num_children;
3112 }
3113 break;
3114
Greg Clayton9e409562010-07-28 02:04:09 +00003115 case clang::Type::ObjCObject:
3116 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00003117 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00003118 {
Sean Callanan78e37602011-01-27 04:42:51 +00003119 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003120 assert (objc_class_type);
3121 if (objc_class_type)
3122 {
3123 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3124
3125 if (class_interface_decl)
3126 {
3127
3128 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3129 if (superclass_interface_decl)
3130 {
3131 if (omit_empty_base_classes)
3132 {
3133 if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true))
3134 ++num_children;
3135 }
3136 else
3137 ++num_children;
3138 }
3139
3140 num_children += class_interface_decl->ivar_size();
3141 }
3142 }
3143 }
3144 break;
3145
3146 case clang::Type::ObjCObjectPointer:
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003147 {
Sean Callanan78e37602011-01-27 04:42:51 +00003148 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003149 QualType pointee_type = pointer_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003150 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3151 pointee_type.getAsOpaquePtr(),
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003152 omit_empty_base_classes);
3153 // If this type points to a simple type, then it has 1 child
3154 if (num_pointee_children == 0)
3155 num_children = 1;
3156 else
3157 num_children = num_pointee_children;
3158 }
3159 break;
Greg Clayton9e409562010-07-28 02:04:09 +00003160
Greg Claytone1a916a2010-07-21 22:12:05 +00003161 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003162 num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
3163 break;
3164
Greg Claytone1a916a2010-07-21 22:12:05 +00003165 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003166 {
Sean Callanan78e37602011-01-27 04:42:51 +00003167 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003168 QualType pointee_type (pointer_type->getPointeeType());
Greg Clayton6beaaa62011-01-17 03:46:26 +00003169 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3170 pointee_type.getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00003171 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003172 if (num_pointee_children == 0)
Greg Clayton54979cd2010-12-15 05:08:08 +00003173 {
3174 // We have a pointer to a pointee type that claims it has no children.
3175 // We will want to look at
3176 num_children = ClangASTContext::GetNumPointeeChildren (pointee_type.getAsOpaquePtr());
3177 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003178 else
3179 num_children = num_pointee_children;
3180 }
3181 break;
3182
Greg Clayton73b472d2010-10-27 03:32:59 +00003183 case clang::Type::LValueReference:
3184 case clang::Type::RValueReference:
3185 {
Sean Callanan78e37602011-01-27 04:42:51 +00003186 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton73b472d2010-10-27 03:32:59 +00003187 QualType pointee_type = reference_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003188 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3189 pointee_type.getAsOpaquePtr(),
Greg Clayton73b472d2010-10-27 03:32:59 +00003190 omit_empty_base_classes);
3191 // If this type points to a simple type, then it has 1 child
3192 if (num_pointee_children == 0)
3193 num_children = 1;
3194 else
3195 num_children = num_pointee_children;
3196 }
3197 break;
3198
3199
Greg Claytone1a916a2010-07-21 22:12:05 +00003200 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00003201 num_children = ClangASTContext::GetNumChildren (ast,
3202 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3203 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003204 break;
Sean Callanan912855f2011-08-11 23:56:13 +00003205
3206 case clang::Type::Elaborated:
3207 num_children = ClangASTContext::GetNumChildren (ast,
3208 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3209 omit_empty_base_classes);
3210 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003211
3212 default:
3213 break;
3214 }
3215 return num_children;
3216}
3217
Greg Claytonbf2331c2011-09-09 23:04:00 +00003218uint32_t
3219ClangASTContext::GetNumDirectBaseClasses (clang::ASTContext *ast, clang_type_t clang_type)
3220{
3221 if (clang_type == NULL)
3222 return 0;
3223
3224 uint32_t count = 0;
3225 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3226 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3227 switch (type_class)
3228 {
3229 case clang::Type::Record:
3230 if (GetCompleteQualType (ast, qual_type))
3231 {
3232 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3233 if (cxx_record_decl)
3234 count = cxx_record_decl->getNumBases();
3235 }
3236 break;
3237
3238 case clang::Type::ObjCObject:
3239 case clang::Type::ObjCInterface:
3240 if (GetCompleteQualType (ast, qual_type))
3241 {
3242 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3243 if (objc_class_type)
3244 {
3245 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3246
3247 if (class_interface_decl && class_interface_decl->getSuperClass())
3248 count = 1;
3249 }
3250 }
3251 break;
3252
3253
3254 case clang::Type::Typedef:
3255 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3256 break;
3257
3258 case clang::Type::Elaborated:
3259 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3260 break;
3261
3262 default:
3263 break;
3264 }
3265 return count;
3266}
3267
3268uint32_t
3269ClangASTContext::GetNumVirtualBaseClasses (clang::ASTContext *ast,
3270 clang_type_t clang_type)
3271{
3272 if (clang_type == NULL)
3273 return 0;
3274
3275 uint32_t count = 0;
3276 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3277 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3278 switch (type_class)
3279 {
3280 case clang::Type::Record:
3281 if (GetCompleteQualType (ast, qual_type))
3282 {
3283 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3284 if (cxx_record_decl)
3285 count = cxx_record_decl->getNumVBases();
3286 }
3287 break;
3288
3289 case clang::Type::Typedef:
3290 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3291 break;
3292
3293 case clang::Type::Elaborated:
3294 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3295 break;
3296
3297 default:
3298 break;
3299 }
3300 return count;
3301}
3302
3303uint32_t
3304ClangASTContext::GetNumFields (clang::ASTContext *ast, clang_type_t clang_type)
3305{
3306 if (clang_type == NULL)
3307 return 0;
3308
3309 uint32_t count = 0;
3310 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3311 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3312 switch (type_class)
3313 {
3314 case clang::Type::Record:
3315 if (GetCompleteQualType (ast, qual_type))
3316 {
3317 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
3318 if (record_type)
3319 {
3320 RecordDecl *record_decl = record_type->getDecl();
3321 if (record_decl)
3322 {
3323 uint32_t field_idx = 0;
3324 RecordDecl::field_iterator field, field_end;
3325 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3326 ++field_idx;
3327 count = field_idx;
3328 }
3329 }
3330 }
3331 break;
3332
3333 case clang::Type::Typedef:
3334 count = ClangASTContext::GetNumFields (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3335 break;
3336
3337 case clang::Type::Elaborated:
3338 count = ClangASTContext::GetNumFields (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3339 break;
3340
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003341 case clang::Type::ObjCObject:
3342 case clang::Type::ObjCInterface:
3343 if (GetCompleteQualType (ast, qual_type))
3344 {
3345 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3346 if (objc_class_type)
3347 {
3348 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3349
3350 if (class_interface_decl)
3351 count = class_interface_decl->ivar_size();
3352 }
3353 }
3354 break;
3355
Greg Claytonbf2331c2011-09-09 23:04:00 +00003356 default:
3357 break;
3358 }
3359 return count;
3360}
3361
3362clang_type_t
3363ClangASTContext::GetDirectBaseClassAtIndex (clang::ASTContext *ast,
3364 clang_type_t clang_type,
3365 uint32_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003366 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003367{
3368 if (clang_type == NULL)
3369 return 0;
3370
3371 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3372 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3373 switch (type_class)
3374 {
3375 case clang::Type::Record:
3376 if (GetCompleteQualType (ast, qual_type))
3377 {
3378 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3379 if (cxx_record_decl)
3380 {
3381 uint32_t curr_idx = 0;
3382 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3383 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3384 base_class != base_class_end;
3385 ++base_class, ++curr_idx)
3386 {
3387 if (curr_idx == idx)
3388 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003389 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003390 {
3391 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3392 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3393// if (base_class->isVirtual())
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003394// *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003395// else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003396 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003397 }
3398 return base_class->getType().getAsOpaquePtr();
3399 }
3400 }
3401 }
3402 }
3403 break;
3404
3405 case clang::Type::ObjCObject:
3406 case clang::Type::ObjCInterface:
3407 if (idx == 0 && GetCompleteQualType (ast, qual_type))
3408 {
3409 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3410 if (objc_class_type)
3411 {
3412 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3413
3414 if (class_interface_decl)
3415 {
3416 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3417 if (superclass_interface_decl)
3418 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003419 if (bit_offset_ptr)
3420 *bit_offset_ptr = 0;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003421 return ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr();
3422 }
3423 }
3424 }
3425 }
3426 break;
3427
3428
3429 case clang::Type::Typedef:
3430 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3431 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3432 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003433 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003434
3435 case clang::Type::Elaborated:
3436 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3437 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3438 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003439 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003440
3441 default:
3442 break;
3443 }
3444 return NULL;
3445}
3446
3447clang_type_t
3448ClangASTContext::GetVirtualBaseClassAtIndex (clang::ASTContext *ast,
3449 clang_type_t clang_type,
3450 uint32_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003451 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003452{
3453 if (clang_type == NULL)
3454 return 0;
3455
3456 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3457 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3458 switch (type_class)
3459 {
3460 case clang::Type::Record:
3461 if (GetCompleteQualType (ast, qual_type))
3462 {
3463 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3464 if (cxx_record_decl)
3465 {
3466 uint32_t curr_idx = 0;
3467 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3468 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
3469 base_class != base_class_end;
3470 ++base_class, ++curr_idx)
3471 {
3472 if (curr_idx == idx)
3473 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003474 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003475 {
3476 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3477 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003478 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003479
3480 }
3481 return base_class->getType().getAsOpaquePtr();
3482 }
3483 }
3484 }
3485 }
3486 break;
3487
3488 case clang::Type::Typedef:
3489 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3490 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3491 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003492 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003493
3494 case clang::Type::Elaborated:
3495 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3496 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3497 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003498 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003499
3500 default:
3501 break;
3502 }
3503 return NULL;
3504}
3505
3506clang_type_t
3507ClangASTContext::GetFieldAtIndex (clang::ASTContext *ast,
3508 clang_type_t clang_type,
3509 uint32_t idx,
3510 std::string& name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003511 uint64_t *bit_offset_ptr,
3512 uint32_t *bitfield_bit_size_ptr,
3513 bool *is_bitfield_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003514{
3515 if (clang_type == NULL)
3516 return 0;
3517
3518 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3519 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3520 switch (type_class)
3521 {
3522 case clang::Type::Record:
3523 if (GetCompleteQualType (ast, qual_type))
3524 {
3525 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3526 const RecordDecl *record_decl = record_type->getDecl();
3527 uint32_t field_idx = 0;
3528 RecordDecl::field_iterator field, field_end;
3529 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
3530 {
3531 if (idx == field_idx)
3532 {
3533 // Print the member type if requested
3534 // Print the member name and equal sign
3535 name.assign(field->getNameAsString());
3536
3537 // Figure out the type byte size (field_type_info.first) and
3538 // alignment (field_type_info.second) from the AST context.
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003539 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003540 {
3541 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003542 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003543 }
3544
Greg Clayton1811b4f2012-07-31 23:39:10 +00003545 const bool is_bitfield = field->isBitField();
3546
3547 if (bitfield_bit_size_ptr)
3548 {
3549 *bitfield_bit_size_ptr = 0;
3550
3551 if (is_bitfield && ast)
3552 {
3553 Expr *bitfield_bit_size_expr = field->getBitWidth();
3554 llvm::APSInt bitfield_apsint;
3555 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
3556 {
3557 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
3558 }
3559 }
3560 }
3561 if (is_bitfield_ptr)
3562 *is_bitfield_ptr = is_bitfield;
3563
Greg Claytonbf2331c2011-09-09 23:04:00 +00003564 return field->getType().getAsOpaquePtr();
3565 }
3566 }
3567 }
3568 break;
3569
3570 case clang::Type::ObjCObject:
3571 case clang::Type::ObjCInterface:
3572 if (GetCompleteQualType (ast, qual_type))
3573 {
3574 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3575 assert (objc_class_type);
3576 if (objc_class_type)
3577 {
3578 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3579
3580 if (class_interface_decl)
3581 {
3582 if (idx < (class_interface_decl->ivar_size()))
3583 {
3584 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
3585 uint32_t ivar_idx = 0;
3586
3587 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
3588 {
3589 if (ivar_idx == idx)
3590 {
3591 const ObjCIvarDecl* ivar_decl = *ivar_pos;
3592
3593 QualType ivar_qual_type(ivar_decl->getType());
3594
3595 name.assign(ivar_decl->getNameAsString());
3596
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003597 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003598 {
3599 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003600 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003601 }
3602
Greg Clayton1811b4f2012-07-31 23:39:10 +00003603 const bool is_bitfield = ivar_pos->isBitField();
3604
3605 if (bitfield_bit_size_ptr)
3606 {
3607 *bitfield_bit_size_ptr = 0;
3608
3609 if (is_bitfield && ast)
3610 {
3611 Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
3612 llvm::APSInt bitfield_apsint;
3613 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
3614 {
3615 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
3616 }
3617 }
3618 }
3619 if (is_bitfield_ptr)
3620 *is_bitfield_ptr = is_bitfield;
3621
Greg Claytonbf2331c2011-09-09 23:04:00 +00003622 return ivar_qual_type.getAsOpaquePtr();
3623 }
3624 }
3625 }
3626 }
3627 }
3628 }
3629 break;
3630
3631
3632 case clang::Type::Typedef:
3633 return ClangASTContext::GetFieldAtIndex (ast,
3634 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3635 idx,
3636 name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003637 bit_offset_ptr,
3638 bitfield_bit_size_ptr,
3639 is_bitfield_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003640
3641 case clang::Type::Elaborated:
3642 return ClangASTContext::GetFieldAtIndex (ast,
3643 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3644 idx,
3645 name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003646 bit_offset_ptr,
3647 bitfield_bit_size_ptr,
3648 is_bitfield_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003649
3650 default:
3651 break;
3652 }
3653 return NULL;
3654}
3655
Greg Claytoneaafa732012-10-13 00:20:27 +00003656lldb::BasicType
3657ClangASTContext::GetLLDBBasicTypeEnumeration (clang_type_t clang_type)
3658{
3659 if (clang_type)
3660 {
3661 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3662 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Claytonc4c5e892012-10-15 21:16:43 +00003663 if (type_class == clang::Type::Builtin)
Greg Claytoneaafa732012-10-13 00:20:27 +00003664 {
Greg Claytoneaafa732012-10-13 00:20:27 +00003665 switch (cast<clang::BuiltinType>(qual_type)->getKind())
Greg Claytonc4c5e892012-10-15 21:16:43 +00003666 {
Greg Claytoneaafa732012-10-13 00:20:27 +00003667 case clang::BuiltinType::Void: return eBasicTypeVoid;
3668 case clang::BuiltinType::Bool: return eBasicTypeBool;
3669 case clang::BuiltinType::Char_S: return eBasicTypeSignedChar;
3670 case clang::BuiltinType::Char_U: return eBasicTypeUnsignedChar;
3671 case clang::BuiltinType::Char16: return eBasicTypeChar16;
3672 case clang::BuiltinType::Char32: return eBasicTypeChar32;
3673 case clang::BuiltinType::UChar: return eBasicTypeUnsignedChar;
3674 case clang::BuiltinType::SChar: return eBasicTypeSignedChar;
3675 case clang::BuiltinType::WChar_S: return eBasicTypeSignedWChar;
3676 case clang::BuiltinType::WChar_U: return eBasicTypeUnsignedWChar;
3677 case clang::BuiltinType::Short: return eBasicTypeShort;
3678 case clang::BuiltinType::UShort: return eBasicTypeUnsignedShort;
3679 case clang::BuiltinType::Int: return eBasicTypeInt;
3680 case clang::BuiltinType::UInt: return eBasicTypeUnsignedInt;
3681 case clang::BuiltinType::Long: return eBasicTypeLong;
3682 case clang::BuiltinType::ULong: return eBasicTypeUnsignedLong;
3683 case clang::BuiltinType::LongLong: return eBasicTypeLongLong;
3684 case clang::BuiltinType::ULongLong: return eBasicTypeUnsignedLongLong;
3685 case clang::BuiltinType::Int128: return eBasicTypeInt128;
3686 case clang::BuiltinType::UInt128: return eBasicTypeUnsignedInt128;
3687
3688 case clang::BuiltinType::Half: return eBasicTypeHalf;
3689 case clang::BuiltinType::Float: return eBasicTypeFloat;
3690 case clang::BuiltinType::Double: return eBasicTypeDouble;
3691 case clang::BuiltinType::LongDouble:return eBasicTypeLongDouble;
3692
3693 case clang::BuiltinType::NullPtr: return eBasicTypeNullPtr;
3694 case clang::BuiltinType::ObjCId: return eBasicTypeObjCID;
3695 case clang::BuiltinType::ObjCClass: return eBasicTypeObjCClass;
3696 case clang::BuiltinType::ObjCSel: return eBasicTypeObjCSel;
3697 case clang::BuiltinType::Dependent:
3698 case clang::BuiltinType::Overload:
3699 case clang::BuiltinType::BoundMember:
3700 case clang::BuiltinType::PseudoObject:
3701 case clang::BuiltinType::UnknownAny:
3702 case clang::BuiltinType::BuiltinFn:
3703 case clang::BuiltinType::ARCUnbridgedCast:
3704 return eBasicTypeOther;
Greg Claytonc4c5e892012-10-15 21:16:43 +00003705 }
Greg Claytoneaafa732012-10-13 00:20:27 +00003706 }
3707 }
3708
3709 return eBasicTypeInvalid;
3710}
3711
3712
Greg Claytonbf2331c2011-09-09 23:04:00 +00003713
Greg Clayton54979cd2010-12-15 05:08:08 +00003714// If a pointer to a pointee type (the clang_type arg) says that it has no
3715// children, then we either need to trust it, or override it and return a
3716// different result. For example, an "int *" has one child that is an integer,
3717// but a function pointer doesn't have any children. Likewise if a Record type
3718// claims it has no children, then there really is nothing to show.
3719uint32_t
3720ClangASTContext::GetNumPointeeChildren (clang_type_t clang_type)
3721{
3722 if (clang_type == NULL)
3723 return 0;
3724
3725 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3726 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3727 switch (type_class)
3728 {
Greg Clayton97a43712011-01-08 22:26:47 +00003729 case clang::Type::Builtin:
3730 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3731 {
Greg Clayton7260f622011-04-18 08:33:37 +00003732 case clang::BuiltinType::UnknownAny:
Greg Clayton97a43712011-01-08 22:26:47 +00003733 case clang::BuiltinType::Void:
3734 case clang::BuiltinType::NullPtr:
3735 return 0;
3736 case clang::BuiltinType::Bool:
3737 case clang::BuiltinType::Char_U:
3738 case clang::BuiltinType::UChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003739 case clang::BuiltinType::WChar_U:
Greg Clayton97a43712011-01-08 22:26:47 +00003740 case clang::BuiltinType::Char16:
3741 case clang::BuiltinType::Char32:
3742 case clang::BuiltinType::UShort:
3743 case clang::BuiltinType::UInt:
3744 case clang::BuiltinType::ULong:
3745 case clang::BuiltinType::ULongLong:
3746 case clang::BuiltinType::UInt128:
3747 case clang::BuiltinType::Char_S:
3748 case clang::BuiltinType::SChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003749 case clang::BuiltinType::WChar_S:
Greg Clayton97a43712011-01-08 22:26:47 +00003750 case clang::BuiltinType::Short:
3751 case clang::BuiltinType::Int:
3752 case clang::BuiltinType::Long:
3753 case clang::BuiltinType::LongLong:
3754 case clang::BuiltinType::Int128:
3755 case clang::BuiltinType::Float:
3756 case clang::BuiltinType::Double:
3757 case clang::BuiltinType::LongDouble:
3758 case clang::BuiltinType::Dependent:
3759 case clang::BuiltinType::Overload:
Greg Clayton97a43712011-01-08 22:26:47 +00003760 case clang::BuiltinType::ObjCId:
3761 case clang::BuiltinType::ObjCClass:
3762 case clang::BuiltinType::ObjCSel:
Sean Callanand12cf8bb2011-05-15 22:34:38 +00003763 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00003764 case clang::BuiltinType::Half:
3765 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00003766 case clang::BuiltinType::PseudoObject:
Sean Callanan3d654b32012-09-24 22:25:51 +00003767 case clang::BuiltinType::BuiltinFn:
Greg Clayton97a43712011-01-08 22:26:47 +00003768 return 1;
3769 }
3770 break;
3771
Greg Clayton49462ea2011-01-15 02:52:14 +00003772 case clang::Type::Complex: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003773 case clang::Type::Pointer: return 1;
3774 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
3775 case clang::Type::LValueReference: return 1;
3776 case clang::Type::RValueReference: return 1;
3777 case clang::Type::MemberPointer: return 0;
3778 case clang::Type::ConstantArray: return 0;
3779 case clang::Type::IncompleteArray: return 0;
3780 case clang::Type::VariableArray: return 0;
3781 case clang::Type::DependentSizedArray: return 0;
3782 case clang::Type::DependentSizedExtVector: return 0;
3783 case clang::Type::Vector: return 0;
3784 case clang::Type::ExtVector: return 0;
3785 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
3786 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
3787 case clang::Type::UnresolvedUsing: return 0;
3788 case clang::Type::Paren: return 0;
3789 case clang::Type::Typedef: return ClangASTContext::GetNumPointeeChildren (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00003790 case clang::Type::Elaborated: return ClangASTContext::GetNumPointeeChildren (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003791 case clang::Type::TypeOfExpr: return 0;
3792 case clang::Type::TypeOf: return 0;
3793 case clang::Type::Decltype: return 0;
3794 case clang::Type::Record: return 0;
3795 case clang::Type::Enum: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003796 case clang::Type::TemplateTypeParm: return 1;
3797 case clang::Type::SubstTemplateTypeParm: return 1;
3798 case clang::Type::TemplateSpecialization: return 1;
3799 case clang::Type::InjectedClassName: return 0;
3800 case clang::Type::DependentName: return 1;
3801 case clang::Type::DependentTemplateSpecialization: return 1;
3802 case clang::Type::ObjCObject: return 0;
3803 case clang::Type::ObjCInterface: return 0;
3804 case clang::Type::ObjCObjectPointer: return 1;
3805 default:
3806 break;
3807 }
3808 return 0;
3809}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003810
Greg Clayton1be10fc2010-09-29 01:12:09 +00003811clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003812ClangASTContext::GetChildClangTypeAtIndex
3813(
Jim Inghamd555bac2011-06-24 22:03:24 +00003814 ExecutionContext *exe_ctx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003815 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003816 clang_type_t parent_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003817 uint32_t idx,
3818 bool transparent_pointers,
3819 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003820 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003821 std::string& child_name,
3822 uint32_t &child_byte_size,
3823 int32_t &child_byte_offset,
3824 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003825 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003826 bool &child_is_base_class,
3827 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003828)
3829{
3830 if (parent_clang_type)
3831
Jim Inghamd555bac2011-06-24 22:03:24 +00003832 return GetChildClangTypeAtIndex (exe_ctx,
3833 getASTContext(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003834 parent_name,
3835 parent_clang_type,
3836 idx,
3837 transparent_pointers,
3838 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003839 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003840 child_name,
3841 child_byte_size,
3842 child_byte_offset,
3843 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003844 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003845 child_is_base_class,
3846 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003847 return NULL;
3848}
3849
Greg Clayton1be10fc2010-09-29 01:12:09 +00003850clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003851ClangASTContext::GetChildClangTypeAtIndex
3852(
Jim Inghamd555bac2011-06-24 22:03:24 +00003853 ExecutionContext *exe_ctx,
Greg Clayton6beaaa62011-01-17 03:46:26 +00003854 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003855 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003856 clang_type_t parent_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003857 uint32_t idx,
3858 bool transparent_pointers,
3859 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003860 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003861 std::string& child_name,
3862 uint32_t &child_byte_size,
3863 int32_t &child_byte_offset,
3864 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003865 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003866 bool &child_is_base_class,
3867 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003868)
3869{
3870 if (parent_clang_type == NULL)
3871 return NULL;
3872
Greg Clayton4ef877f2012-12-06 02:33:54 +00003873 QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type));
3874 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
3875 child_bitfield_bit_size = 0;
3876 child_bitfield_bit_offset = 0;
3877 child_is_base_class = false;
3878
3879 const bool idx_is_valid = idx < ClangASTContext::GetNumChildren (ast, parent_clang_type, omit_empty_base_classes);
3880 uint32_t bit_offset;
3881 switch (parent_type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003882 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003883 case clang::Type::Builtin:
3884 if (idx_is_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003885 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003886 switch (cast<clang::BuiltinType>(parent_qual_type)->getKind())
3887 {
3888 case clang::BuiltinType::ObjCId:
3889 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00003890 child_name = "isa";
Greg Clayton6beaaa62011-01-17 03:46:26 +00003891 child_byte_size = ast->getTypeSize(ast->ObjCBuiltinClassTy) / CHAR_BIT;
3892 return ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003893
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003894 default:
3895 break;
3896 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00003897 }
3898 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003899
Greg Clayton4ef877f2012-12-06 02:33:54 +00003900 case clang::Type::Record:
3901 if (idx_is_valid && GetCompleteQualType (ast, parent_qual_type))
3902 {
3903 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr());
3904 const RecordDecl *record_decl = record_type->getDecl();
3905 assert(record_decl);
3906 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
3907 uint32_t child_idx = 0;
3908
3909 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3910 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003911 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003912 // We might have base classes to print out first
3913 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3914 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3915 base_class != base_class_end;
3916 ++base_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003917 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003918 const CXXRecordDecl *base_class_decl = NULL;
3919
3920 // Skip empty base classes
3921 if (omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003922 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003923 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3924 if (RecordHasFields(base_class_decl) == false)
3925 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003926 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00003927
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003928 if (idx == child_idx)
3929 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003930 if (base_class_decl == NULL)
3931 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003932
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003933
Greg Clayton4ef877f2012-12-06 02:33:54 +00003934 if (base_class->isVirtual())
3935 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
3936 else
3937 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003938
Greg Clayton4ef877f2012-12-06 02:33:54 +00003939 // Base classes should be a multiple of 8 bits in size
3940 child_byte_offset = bit_offset/8;
3941
3942 child_name = ClangASTType::GetTypeNameForQualType(ast, base_class->getType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003943
Greg Clayton4ef877f2012-12-06 02:33:54 +00003944 uint64_t clang_type_info_bit_size = ast->getTypeSize(base_class->getType());
3945
3946 // Base classes bit sizes should be a multiple of 8 bits in size
3947 assert (clang_type_info_bit_size % 8 == 0);
3948 child_byte_size = clang_type_info_bit_size / 8;
3949 child_is_base_class = true;
3950 return base_class->getType().getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003951 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00003952 // We don't increment the child index in the for loop since we might
3953 // be skipping empty base classes
3954 ++child_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003955 }
3956 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00003957 // Make sure index is in range...
3958 uint32_t field_idx = 0;
3959 RecordDecl::field_iterator field, field_end;
3960 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00003961 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003962 if (idx == child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00003963 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003964 // Print the member type if requested
3965 // Print the member name and equal sign
3966 child_name.assign(field->getNameAsString().c_str());
3967
3968 // Figure out the type byte size (field_type_info.first) and
3969 // alignment (field_type_info.second) from the AST context.
3970 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(field->getType());
3971 assert(field_idx < record_layout.getFieldCount());
3972
3973 child_byte_size = field_type_info.first / 8;
3974
3975 // Figure out the field offset within the current struct/union/class type
3976 bit_offset = record_layout.getFieldOffset (field_idx);
3977 child_byte_offset = bit_offset / 8;
3978 if (ClangASTContext::FieldIsBitfield (ast, *field, child_bitfield_bit_size))
3979 child_bitfield_bit_offset = bit_offset % 8;
3980
3981 return field->getType().getAsOpaquePtr();
3982 }
3983 }
3984 }
3985 break;
3986
3987 case clang::Type::ObjCObject:
3988 case clang::Type::ObjCInterface:
3989 if (idx_is_valid && GetCompleteQualType (ast, parent_qual_type))
3990 {
3991 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr());
3992 assert (objc_class_type);
3993 if (objc_class_type)
3994 {
3995 uint32_t child_idx = 0;
3996 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3997
3998 if (class_interface_decl)
3999 {
4000
4001 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
4002 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4003 if (superclass_interface_decl)
Greg Clayton9e409562010-07-28 02:04:09 +00004004 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004005 if (omit_empty_base_classes)
Greg Clayton9e409562010-07-28 02:04:09 +00004006 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004007 if (ClangASTContext::GetNumChildren(ast, ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0)
Greg Clayton9e409562010-07-28 02:04:09 +00004008 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004009 if (idx == 0)
Greg Clayton9e409562010-07-28 02:04:09 +00004010 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004011 QualType ivar_qual_type(ast->getObjCInterfaceType(superclass_interface_decl));
Greg Clayton9e409562010-07-28 02:04:09 +00004012
Greg Clayton9e409562010-07-28 02:04:09 +00004013
Greg Clayton4ef877f2012-12-06 02:33:54 +00004014 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
Greg Clayton9e409562010-07-28 02:04:09 +00004015
Greg Clayton6beaaa62011-01-17 03:46:26 +00004016 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004017
4018 child_byte_size = ivar_type_info.first / 8;
Greg Clayton4ef877f2012-12-06 02:33:54 +00004019 child_byte_offset = 0;
4020 child_is_base_class = true;
Greg Clayton9e409562010-07-28 02:04:09 +00004021
Greg Clayton9e409562010-07-28 02:04:09 +00004022 return ivar_qual_type.getAsOpaquePtr();
4023 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004024
Greg Clayton9e409562010-07-28 02:04:09 +00004025 ++child_idx;
4026 }
4027 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004028 else
4029 ++child_idx;
4030 }
4031
4032 const uint32_t superclass_idx = child_idx;
4033
4034 if (idx < (child_idx + class_interface_decl->ivar_size()))
4035 {
4036 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4037
4038 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
4039 {
4040 if (child_idx == idx)
4041 {
4042 ObjCIvarDecl* ivar_decl = *ivar_pos;
4043
4044 QualType ivar_qual_type(ivar_decl->getType());
4045
4046 child_name.assign(ivar_decl->getNameAsString().c_str());
4047
4048 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
4049
4050 child_byte_size = ivar_type_info.first / 8;
4051
4052 // Figure out the field offset within the current struct/union/class type
4053 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
4054 // that doesn't account for the space taken up by unbacked properties, or from
4055 // the changing size of base classes that are newer than this class.
4056 // So if we have a process around that we can ask about this object, do so.
4057 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
4058 Process *process = NULL;
4059 if (exe_ctx)
4060 process = exe_ctx->GetProcessPtr();
4061 if (process)
4062 {
4063 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4064 if (objc_runtime != NULL)
4065 {
4066 ClangASTType parent_ast_type (ast, parent_qual_type.getAsOpaquePtr());
4067 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
4068 }
4069 }
4070
4071 // Setting this to UINT32_MAX to make sure we don't compute it twice...
4072 bit_offset = UINT32_MAX;
4073
4074 if (child_byte_offset == LLDB_INVALID_IVAR_OFFSET)
4075 {
4076 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
4077 child_byte_offset = bit_offset / 8;
4078 }
4079
4080 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
4081 // of a bitfield within its containing object. So regardless of where we get the byte
4082 // offset from, we still need to get the bit offset for bitfields from the layout.
4083
4084 if (ClangASTContext::FieldIsBitfield (ast, ivar_decl, child_bitfield_bit_size))
4085 {
4086 if (bit_offset == UINT32_MAX)
4087 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
4088
4089 child_bitfield_bit_offset = bit_offset % 8;
4090 }
4091 return ivar_qual_type.getAsOpaquePtr();
4092 }
4093 ++child_idx;
4094 }
Greg Clayton9e409562010-07-28 02:04:09 +00004095 }
4096 }
4097 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004098 }
4099 break;
4100
4101 case clang::Type::ObjCObjectPointer:
4102 if (idx_is_valid)
4103 {
4104 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr());
4105 QualType pointee_type = pointer_type->getPointeeType();
4106
4107 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
Greg Clayton9e409562010-07-28 02:04:09 +00004108 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004109 child_is_deref_of_parent = false;
4110 bool tmp_child_is_deref_of_parent = false;
4111 return GetChildClangTypeAtIndex (exe_ctx,
4112 ast,
4113 parent_name,
4114 pointer_type->getPointeeType().getAsOpaquePtr(),
4115 idx,
4116 transparent_pointers,
4117 omit_empty_base_classes,
4118 ignore_array_bounds,
4119 child_name,
4120 child_byte_size,
4121 child_byte_offset,
4122 child_bitfield_bit_size,
4123 child_bitfield_bit_offset,
4124 child_is_base_class,
4125 tmp_child_is_deref_of_parent);
4126 }
4127 else
4128 {
4129 child_is_deref_of_parent = true;
4130 if (parent_name)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004131 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004132 child_name.assign(1, '*');
4133 child_name += parent_name;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004134 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004135
Greg Clayton4ef877f2012-12-06 02:33:54 +00004136 // We have a pointer to an simple type
4137 if (idx == 0 && GetCompleteQualType(ast, pointee_type))
4138 {
4139 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
4140 assert(clang_type_info.first % 8 == 0);
4141 child_byte_size = clang_type_info.first / 8;
4142 child_byte_offset = 0;
4143 return pointee_type.getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004144 }
Greg Clayton9e409562010-07-28 02:04:09 +00004145 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004146 }
4147 break;
Greg Clayton9e409562010-07-28 02:04:09 +00004148
Greg Claytone1a916a2010-07-21 22:12:05 +00004149 case clang::Type::ConstantArray:
Greg Clayton4ef877f2012-12-06 02:33:54 +00004150 case clang::Type::IncompleteArray:
4151 if (ignore_array_bounds || idx_is_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004152 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004153 const ArrayType *array = cast<ArrayType>(parent_qual_type.getTypePtr());
4154 if (array)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004155 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004156 if (GetCompleteQualType (ast, array->getElementType()))
4157 {
4158 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Greg Clayton4ef877f2012-12-06 02:33:54 +00004159
Greg Clayton6beaaa62011-01-17 03:46:26 +00004160 char element_name[64];
4161 ::snprintf (element_name, sizeof (element_name), "[%u]", idx);
Greg Clayton4ef877f2012-12-06 02:33:54 +00004162
Greg Clayton6beaaa62011-01-17 03:46:26 +00004163 child_name.assign(element_name);
4164 assert(field_type_info.first % 8 == 0);
4165 child_byte_size = field_type_info.first / 8;
Greg Claytondaf515f2011-07-09 20:12:33 +00004166 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
Greg Clayton6beaaa62011-01-17 03:46:26 +00004167 return array->getElementType().getAsOpaquePtr();
4168 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004169 }
4170 }
4171 break;
Greg Clayton4ef877f2012-12-06 02:33:54 +00004172
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004173
Greg Clayton4ef877f2012-12-06 02:33:54 +00004174 case clang::Type::Pointer:
4175 if (idx_is_valid)
4176 {
4177 const PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr());
4178 QualType pointee_type = pointer_type->getPointeeType();
4179
4180 // Don't dereference "void *" pointers
4181 if (pointee_type->isVoidType())
4182 return NULL;
4183
4184 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004185 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004186 child_is_deref_of_parent = false;
4187 bool tmp_child_is_deref_of_parent = false;
4188 return GetChildClangTypeAtIndex (exe_ctx,
4189 ast,
4190 parent_name,
4191 pointer_type->getPointeeType().getAsOpaquePtr(),
4192 idx,
4193 transparent_pointers,
4194 omit_empty_base_classes,
4195 ignore_array_bounds,
4196 child_name,
4197 child_byte_size,
4198 child_byte_offset,
4199 child_bitfield_bit_size,
4200 child_bitfield_bit_offset,
4201 child_is_base_class,
4202 tmp_child_is_deref_of_parent);
4203 }
4204 else
4205 {
4206 child_is_deref_of_parent = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004207
Greg Clayton4ef877f2012-12-06 02:33:54 +00004208 if (parent_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004209 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004210 child_name.assign(1, '*');
4211 child_name += parent_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004212 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004213
4214 // We have a pointer to an simple type
4215 if (idx == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004216 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004217 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
4218 assert(clang_type_info.first % 8 == 0);
4219 child_byte_size = clang_type_info.first / 8;
4220 child_byte_offset = 0;
4221 return pointee_type.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004222 }
4223 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004224 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004225 break;
4226
4227 case clang::Type::LValueReference:
4228 case clang::Type::RValueReference:
4229 if (idx_is_valid)
4230 {
4231 const ReferenceType *reference_type = cast<ReferenceType>(parent_qual_type.getTypePtr());
4232 QualType pointee_type(reference_type->getPointeeType());
4233 clang_type_t pointee_clang_type = pointee_type.getAsOpaquePtr();
4234 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_clang_type))
4235 {
4236 child_is_deref_of_parent = false;
4237 bool tmp_child_is_deref_of_parent = false;
4238 return GetChildClangTypeAtIndex (exe_ctx,
4239 ast,
4240 parent_name,
4241 pointee_clang_type,
4242 idx,
4243 transparent_pointers,
4244 omit_empty_base_classes,
4245 ignore_array_bounds,
4246 child_name,
4247 child_byte_size,
4248 child_byte_offset,
4249 child_bitfield_bit_size,
4250 child_bitfield_bit_offset,
4251 child_is_base_class,
4252 tmp_child_is_deref_of_parent);
4253 }
4254 else
4255 {
4256 if (parent_name)
4257 {
4258 child_name.assign(1, '&');
4259 child_name += parent_name;
4260 }
4261
4262 // We have a pointer to an simple type
4263 if (idx == 0)
4264 {
4265 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
4266 assert(clang_type_info.first % 8 == 0);
4267 child_byte_size = clang_type_info.first / 8;
4268 child_byte_offset = 0;
4269 return pointee_type.getAsOpaquePtr();
4270 }
4271 }
4272 }
4273 break;
4274
4275 case clang::Type::Typedef:
4276 return GetChildClangTypeAtIndex (exe_ctx,
4277 ast,
4278 parent_name,
4279 cast<TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
4280 idx,
4281 transparent_pointers,
4282 omit_empty_base_classes,
4283 ignore_array_bounds,
4284 child_name,
4285 child_byte_size,
4286 child_byte_offset,
4287 child_bitfield_bit_size,
4288 child_bitfield_bit_offset,
4289 child_is_base_class,
4290 child_is_deref_of_parent);
4291 break;
4292
4293 case clang::Type::Elaborated:
4294 return GetChildClangTypeAtIndex (exe_ctx,
4295 ast,
4296 parent_name,
4297 cast<ElaboratedType>(parent_qual_type)->getNamedType().getAsOpaquePtr(),
4298 idx,
4299 transparent_pointers,
4300 omit_empty_base_classes,
4301 ignore_array_bounds,
4302 child_name,
4303 child_byte_size,
4304 child_byte_offset,
4305 child_bitfield_bit_size,
4306 child_bitfield_bit_offset,
4307 child_is_base_class,
4308 child_is_deref_of_parent);
4309
4310 default:
4311 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004312 }
Greg Clayton19503a22010-07-23 15:37:46 +00004313 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004314}
4315
4316static inline bool
4317BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
4318{
Greg Clayton6beaaa62011-01-17 03:46:26 +00004319 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004320}
4321
4322static uint32_t
4323GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
4324{
4325 uint32_t num_bases = 0;
4326 if (cxx_record_decl)
4327 {
4328 if (omit_empty_base_classes)
4329 {
4330 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4331 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4332 base_class != base_class_end;
4333 ++base_class)
4334 {
4335 // Skip empty base classes
4336 if (omit_empty_base_classes)
4337 {
4338 if (BaseSpecifierIsEmpty (base_class))
4339 continue;
4340 }
4341 ++num_bases;
4342 }
4343 }
4344 else
4345 num_bases = cxx_record_decl->getNumBases();
4346 }
4347 return num_bases;
4348}
4349
4350
4351static uint32_t
4352GetIndexForRecordBase
4353(
4354 const RecordDecl *record_decl,
4355 const CXXBaseSpecifier *base_spec,
4356 bool omit_empty_base_classes
4357)
4358{
4359 uint32_t child_idx = 0;
4360
4361 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4362
4363// const char *super_name = record_decl->getNameAsCString();
4364// const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString();
4365// printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
4366//
4367 if (cxx_record_decl)
4368 {
4369 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4370 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4371 base_class != base_class_end;
4372 ++base_class)
4373 {
4374 if (omit_empty_base_classes)
4375 {
4376 if (BaseSpecifierIsEmpty (base_class))
4377 continue;
4378 }
4379
4380// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
4381// child_idx,
4382// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4383//
4384//
4385 if (base_class == base_spec)
4386 return child_idx;
4387 ++child_idx;
4388 }
4389 }
4390
4391 return UINT32_MAX;
4392}
4393
4394
4395static uint32_t
4396GetIndexForRecordChild
4397(
4398 const RecordDecl *record_decl,
4399 NamedDecl *canonical_decl,
4400 bool omit_empty_base_classes
4401)
4402{
4403 uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes);
4404
4405// const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4406//
4407//// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString());
4408// if (cxx_record_decl)
4409// {
4410// CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4411// for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4412// base_class != base_class_end;
4413// ++base_class)
4414// {
4415// if (omit_empty_base_classes)
4416// {
4417// if (BaseSpecifierIsEmpty (base_class))
4418// continue;
4419// }
4420//
4421//// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
4422//// record_decl->getNameAsCString(),
4423//// canonical_decl->getNameAsCString(),
4424//// child_idx,
4425//// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4426//
4427//
4428// CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4429// if (curr_base_class_decl == canonical_decl)
4430// {
4431// return child_idx;
4432// }
4433// ++child_idx;
4434// }
4435// }
4436//
4437// const uint32_t num_bases = child_idx;
4438 RecordDecl::field_iterator field, field_end;
4439 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4440 field != field_end;
4441 ++field, ++child_idx)
4442 {
4443// printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n",
4444// record_decl->getNameAsCString(),
4445// canonical_decl->getNameAsCString(),
4446// child_idx - num_bases,
4447// field->getNameAsCString());
4448
4449 if (field->getCanonicalDecl() == canonical_decl)
4450 return child_idx;
4451 }
4452
4453 return UINT32_MAX;
4454}
4455
4456// Look for a child member (doesn't include base classes, but it does include
4457// their members) in the type hierarchy. Returns an index path into "clang_type"
4458// on how to reach the appropriate member.
4459//
4460// class A
4461// {
4462// public:
4463// int m_a;
4464// int m_b;
4465// };
4466//
4467// class B
4468// {
4469// };
4470//
4471// class C :
4472// public B,
4473// public A
4474// {
4475// };
4476//
4477// If we have a clang type that describes "class C", and we wanted to looked
4478// "m_b" in it:
4479//
4480// With omit_empty_base_classes == false we would get an integer array back with:
4481// { 1, 1 }
4482// The first index 1 is the child index for "class A" within class C
4483// The second index 1 is the child index for "m_b" within class A
4484//
4485// With omit_empty_base_classes == true we would get an integer array back with:
4486// { 0, 1 }
4487// 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)
4488// The second index 1 is the child index for "m_b" within class A
4489
4490size_t
4491ClangASTContext::GetIndexOfChildMemberWithName
4492(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004493 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004494 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004495 const char *name,
4496 bool omit_empty_base_classes,
4497 std::vector<uint32_t>& child_indexes
4498)
4499{
4500 if (clang_type && name && name[0])
4501 {
4502 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00004503 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4504 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004505 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004506 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004507 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004508 {
4509 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4510 const RecordDecl *record_decl = record_type->getDecl();
4511
4512 assert(record_decl);
4513 uint32_t child_idx = 0;
4514
4515 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4516
4517 // Try and find a field that matches NAME
4518 RecordDecl::field_iterator field, field_end;
4519 StringRef name_sref(name);
4520 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4521 field != field_end;
4522 ++field, ++child_idx)
4523 {
4524 if (field->getName().equals (name_sref))
4525 {
4526 // We have to add on the number of base classes to this index!
4527 child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
4528 return child_indexes.size();
4529 }
4530 }
4531
4532 if (cxx_record_decl)
4533 {
4534 const RecordDecl *parent_record_decl = cxx_record_decl;
4535
4536 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
4537
4538 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
4539 // Didn't find things easily, lets let clang do its thang...
Sean Callanancc427fa2011-07-30 02:42:06 +00004540 IdentifierInfo & ident_ref = ast->Idents.get(name_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004541 DeclarationName decl_name(&ident_ref);
4542
4543 CXXBasePaths paths;
4544 if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember,
4545 decl_name.getAsOpaquePtr(),
4546 paths))
4547 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004548 CXXBasePaths::const_paths_iterator path, path_end = paths.end();
4549 for (path = paths.begin(); path != path_end; ++path)
4550 {
4551 const size_t num_path_elements = path->size();
4552 for (size_t e=0; e<num_path_elements; ++e)
4553 {
4554 CXXBasePathElement elem = (*path)[e];
4555
4556 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
4557 if (child_idx == UINT32_MAX)
4558 {
4559 child_indexes.clear();
4560 return 0;
4561 }
4562 else
4563 {
4564 child_indexes.push_back (child_idx);
4565 parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl());
4566 }
4567 }
Sean Callanan5deaa4c2012-12-21 21:34:42 +00004568 for (NamedDecl *path_decl : path->Decls)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004569 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00004570 child_idx = GetIndexForRecordChild (parent_record_decl, path_decl, omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004571 if (child_idx == UINT32_MAX)
4572 {
4573 child_indexes.clear();
4574 return 0;
4575 }
4576 else
4577 {
4578 child_indexes.push_back (child_idx);
4579 }
4580 }
4581 }
4582 return child_indexes.size();
4583 }
4584 }
4585
4586 }
4587 break;
4588
Greg Clayton9e409562010-07-28 02:04:09 +00004589 case clang::Type::ObjCObject:
4590 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004591 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004592 {
4593 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004594 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004595 assert (objc_class_type);
4596 if (objc_class_type)
4597 {
4598 uint32_t child_idx = 0;
4599 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4600
4601 if (class_interface_decl)
4602 {
4603 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4604 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4605
Greg Clayton6ba78152010-09-18 02:11:07 +00004606 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00004607 {
4608 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4609
4610 if (ivar_decl->getName().equals (name_sref))
4611 {
4612 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4613 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4614 ++child_idx;
4615
4616 child_indexes.push_back (child_idx);
4617 return child_indexes.size();
4618 }
4619 }
4620
4621 if (superclass_interface_decl)
4622 {
4623 // The super class index is always zero for ObjC classes,
4624 // so we push it onto the child indexes in case we find
4625 // an ivar in our superclass...
4626 child_indexes.push_back (0);
4627
Greg Clayton6beaaa62011-01-17 03:46:26 +00004628 if (GetIndexOfChildMemberWithName (ast,
4629 ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00004630 name,
4631 omit_empty_base_classes,
4632 child_indexes))
4633 {
4634 // We did find an ivar in a superclass so just
4635 // return the results!
4636 return child_indexes.size();
4637 }
4638
4639 // We didn't find an ivar matching "name" in our
4640 // superclass, pop the superclass zero index that
4641 // we pushed on above.
4642 child_indexes.pop_back();
4643 }
4644 }
4645 }
4646 }
4647 break;
4648
4649 case clang::Type::ObjCObjectPointer:
4650 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004651 return GetIndexOfChildMemberWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004652 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4653 name,
4654 omit_empty_base_classes,
4655 child_indexes);
4656 }
4657 break;
4658
4659
Greg Claytone1a916a2010-07-21 22:12:05 +00004660 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004661 {
4662// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4663// const uint64_t element_count = array->getSize().getLimitedValue();
4664//
4665// if (idx < element_count)
4666// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004667// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004668//
4669// char element_name[32];
4670// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4671//
4672// child_name.assign(element_name);
4673// assert(field_type_info.first % 8 == 0);
4674// child_byte_size = field_type_info.first / 8;
4675// child_byte_offset = idx * child_byte_size;
4676// return array->getElementType().getAsOpaquePtr();
4677// }
4678 }
4679 break;
4680
Greg Claytone1a916a2010-07-21 22:12:05 +00004681// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004682// {
4683// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4684// QualType pointee_type = mem_ptr_type->getPointeeType();
4685//
4686// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4687// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004688// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004689// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4690// name);
4691// }
4692// }
4693// break;
4694//
Greg Claytone1a916a2010-07-21 22:12:05 +00004695 case clang::Type::LValueReference:
4696 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004697 {
Sean Callanan78e37602011-01-27 04:42:51 +00004698 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004699 QualType pointee_type = reference_type->getPointeeType();
4700
4701 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4702 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004703 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004704 reference_type->getPointeeType().getAsOpaquePtr(),
4705 name,
4706 omit_empty_base_classes,
4707 child_indexes);
4708 }
4709 }
4710 break;
4711
Greg Claytone1a916a2010-07-21 22:12:05 +00004712 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004713 {
Sean Callanan78e37602011-01-27 04:42:51 +00004714 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004715 QualType pointee_type = pointer_type->getPointeeType();
4716
4717 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4718 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004719 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004720 pointer_type->getPointeeType().getAsOpaquePtr(),
4721 name,
4722 omit_empty_base_classes,
4723 child_indexes);
4724 }
4725 else
4726 {
4727// if (parent_name)
4728// {
4729// child_name.assign(1, '*');
4730// child_name += parent_name;
4731// }
4732//
4733// // We have a pointer to an simple type
4734// if (idx == 0)
4735// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004736// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004737// assert(clang_type_info.first % 8 == 0);
4738// child_byte_size = clang_type_info.first / 8;
4739// child_byte_offset = 0;
4740// return pointee_type.getAsOpaquePtr();
4741// }
4742 }
4743 }
4744 break;
4745
Greg Claytone1a916a2010-07-21 22:12:05 +00004746 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00004747 return GetIndexOfChildMemberWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00004748 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004749 name,
4750 omit_empty_base_classes,
4751 child_indexes);
4752
4753 default:
4754 break;
4755 }
4756 }
4757 return 0;
4758}
4759
4760
4761// Get the index of the child of "clang_type" whose name matches. This function
4762// doesn't descend into the children, but only looks one level deep and name
4763// matches can include base class names.
4764
4765uint32_t
4766ClangASTContext::GetIndexOfChildWithName
4767(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004768 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004769 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004770 const char *name,
4771 bool omit_empty_base_classes
4772)
4773{
4774 if (clang_type && name && name[0])
4775 {
4776 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00004777
Greg Clayton737b9322010-09-13 03:32:57 +00004778 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Clayton9e409562010-07-28 02:04:09 +00004779
Greg Clayton737b9322010-09-13 03:32:57 +00004780 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004781 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004782 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004783 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004784 {
4785 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4786 const RecordDecl *record_decl = record_type->getDecl();
4787
4788 assert(record_decl);
4789 uint32_t child_idx = 0;
4790
4791 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4792
4793 if (cxx_record_decl)
4794 {
4795 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4796 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4797 base_class != base_class_end;
4798 ++base_class)
4799 {
4800 // Skip empty base classes
4801 CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4802 if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false)
4803 continue;
4804
Greg Clayton84db9102012-03-26 23:03:23 +00004805 std::string base_class_type_name (ClangASTType::GetTypeNameForQualType(ast, base_class->getType()));
Greg Claytone3055942011-06-30 02:28:26 +00004806 if (base_class_type_name.compare (name) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004807 return child_idx;
4808 ++child_idx;
4809 }
4810 }
4811
4812 // Try and find a field that matches NAME
4813 RecordDecl::field_iterator field, field_end;
4814 StringRef name_sref(name);
4815 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4816 field != field_end;
4817 ++field, ++child_idx)
4818 {
4819 if (field->getName().equals (name_sref))
4820 return child_idx;
4821 }
4822
4823 }
4824 break;
4825
Greg Clayton9e409562010-07-28 02:04:09 +00004826 case clang::Type::ObjCObject:
4827 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004828 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004829 {
4830 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004831 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004832 assert (objc_class_type);
4833 if (objc_class_type)
4834 {
4835 uint32_t child_idx = 0;
4836 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4837
4838 if (class_interface_decl)
4839 {
4840 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4841 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4842
Jim Ingham2f355a72012-10-04 22:22:16 +00004843 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00004844 {
4845 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4846
4847 if (ivar_decl->getName().equals (name_sref))
4848 {
4849 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4850 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4851 ++child_idx;
4852
4853 return child_idx;
4854 }
4855 }
4856
4857 if (superclass_interface_decl)
4858 {
4859 if (superclass_interface_decl->getName().equals (name_sref))
4860 return 0;
4861 }
4862 }
4863 }
4864 }
4865 break;
4866
4867 case clang::Type::ObjCObjectPointer:
4868 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004869 return GetIndexOfChildWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004870 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4871 name,
4872 omit_empty_base_classes);
4873 }
4874 break;
4875
Greg Claytone1a916a2010-07-21 22:12:05 +00004876 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004877 {
4878// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4879// const uint64_t element_count = array->getSize().getLimitedValue();
4880//
4881// if (idx < element_count)
4882// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004883// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004884//
4885// char element_name[32];
4886// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4887//
4888// child_name.assign(element_name);
4889// assert(field_type_info.first % 8 == 0);
4890// child_byte_size = field_type_info.first / 8;
4891// child_byte_offset = idx * child_byte_size;
4892// return array->getElementType().getAsOpaquePtr();
4893// }
4894 }
4895 break;
4896
Greg Claytone1a916a2010-07-21 22:12:05 +00004897// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004898// {
4899// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4900// QualType pointee_type = mem_ptr_type->getPointeeType();
4901//
4902// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4903// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004904// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004905// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4906// name);
4907// }
4908// }
4909// break;
4910//
Greg Claytone1a916a2010-07-21 22:12:05 +00004911 case clang::Type::LValueReference:
4912 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004913 {
Sean Callanan78e37602011-01-27 04:42:51 +00004914 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004915 QualType pointee_type = reference_type->getPointeeType();
4916
4917 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4918 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004919 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004920 reference_type->getPointeeType().getAsOpaquePtr(),
4921 name,
4922 omit_empty_base_classes);
4923 }
4924 }
4925 break;
4926
Greg Claytone1a916a2010-07-21 22:12:05 +00004927 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004928 {
Sean Callanan78e37602011-01-27 04:42:51 +00004929 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004930 QualType pointee_type = pointer_type->getPointeeType();
4931
4932 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4933 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004934 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004935 pointer_type->getPointeeType().getAsOpaquePtr(),
4936 name,
4937 omit_empty_base_classes);
4938 }
4939 else
4940 {
4941// if (parent_name)
4942// {
4943// child_name.assign(1, '*');
4944// child_name += parent_name;
4945// }
4946//
4947// // We have a pointer to an simple type
4948// if (idx == 0)
4949// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004950// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004951// assert(clang_type_info.first % 8 == 0);
4952// child_byte_size = clang_type_info.first / 8;
4953// child_byte_offset = 0;
4954// return pointee_type.getAsOpaquePtr();
4955// }
4956 }
4957 }
4958 break;
4959
Greg Claytone1a916a2010-07-21 22:12:05 +00004960 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00004961 return GetIndexOfChildWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00004962 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004963 name,
4964 omit_empty_base_classes);
4965
4966 default:
4967 break;
4968 }
4969 }
4970 return UINT32_MAX;
4971}
4972
4973#pragma mark TagType
4974
4975bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00004976ClangASTContext::SetTagTypeKind (clang_type_t tag_clang_type, int kind)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004977{
4978 if (tag_clang_type)
4979 {
4980 QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00004981 const clang::Type *clang_type = tag_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004982 if (clang_type)
4983 {
Sean Callanan78e37602011-01-27 04:42:51 +00004984 const TagType *tag_type = dyn_cast<TagType>(clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004985 if (tag_type)
4986 {
4987 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl());
4988 if (tag_decl)
4989 {
4990 tag_decl->setTagKind ((TagDecl::TagKind)kind);
4991 return true;
4992 }
4993 }
4994 }
4995 }
4996 return false;
4997}
4998
4999
5000#pragma mark DeclContext Functions
5001
5002DeclContext *
Greg Clayton1be10fc2010-09-29 01:12:09 +00005003ClangASTContext::GetDeclContextForType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005004{
5005 if (clang_type == NULL)
5006 return NULL;
5007
5008 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005009 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5010 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005011 {
Sean Callanancc427fa2011-07-30 02:42:06 +00005012 case clang::Type::UnaryTransform: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005013 case clang::Type::FunctionNoProto: break;
5014 case clang::Type::FunctionProto: break;
5015 case clang::Type::IncompleteArray: break;
5016 case clang::Type::VariableArray: break;
5017 case clang::Type::ConstantArray: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005018 case clang::Type::DependentSizedArray: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005019 case clang::Type::ExtVector: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005020 case clang::Type::DependentSizedExtVector: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005021 case clang::Type::Vector: break;
5022 case clang::Type::Builtin: break;
5023 case clang::Type::BlockPointer: break;
5024 case clang::Type::Pointer: break;
5025 case clang::Type::LValueReference: break;
5026 case clang::Type::RValueReference: break;
5027 case clang::Type::MemberPointer: break;
5028 case clang::Type::Complex: break;
5029 case clang::Type::ObjCObject: break;
5030 case clang::Type::ObjCInterface: return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface();
5031 case clang::Type::ObjCObjectPointer: return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr());
5032 case clang::Type::Record: return cast<RecordType>(qual_type)->getDecl();
5033 case clang::Type::Enum: return cast<EnumType>(qual_type)->getDecl();
Sean Callanan48114472010-12-13 01:26:27 +00005034 case clang::Type::Typedef: return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00005035 case clang::Type::Elaborated: return ClangASTContext::GetDeclContextForType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00005036 case clang::Type::TypeOfExpr: break;
5037 case clang::Type::TypeOf: break;
5038 case clang::Type::Decltype: break;
5039 //case clang::Type::QualifiedName: break;
5040 case clang::Type::TemplateSpecialization: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005041 case clang::Type::DependentTemplateSpecialization: break;
5042 case clang::Type::TemplateTypeParm: break;
5043 case clang::Type::SubstTemplateTypeParm: break;
5044 case clang::Type::SubstTemplateTypeParmPack:break;
5045 case clang::Type::PackExpansion: break;
5046 case clang::Type::UnresolvedUsing: break;
5047 case clang::Type::Paren: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005048 case clang::Type::Attributed: break;
5049 case clang::Type::Auto: break;
5050 case clang::Type::InjectedClassName: break;
5051 case clang::Type::DependentName: break;
Greg Claytonea3e7d52011-10-08 00:49:15 +00005052 case clang::Type::Atomic: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005053 }
5054 // No DeclContext in this type...
5055 return NULL;
5056}
5057
5058#pragma mark Namespace Declarations
5059
5060NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00005061ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005062{
Greg Clayton030a2042011-10-14 21:34:45 +00005063 NamespaceDecl *namespace_decl = NULL;
Greg Clayton9d3d6882011-10-31 23:51:19 +00005064 ASTContext *ast = getASTContext();
5065 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
5066 if (decl_ctx == NULL)
5067 decl_ctx = translation_unit_decl;
5068
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005069 if (name)
5070 {
Greg Clayton030a2042011-10-14 21:34:45 +00005071 IdentifierInfo &identifier_info = ast->Idents.get(name);
5072 DeclarationName decl_name (&identifier_info);
5073 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00005074 for (NamedDecl *decl : result)
Greg Clayton030a2042011-10-14 21:34:45 +00005075 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00005076 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
Greg Clayton030a2042011-10-14 21:34:45 +00005077 if (namespace_decl)
5078 return namespace_decl;
5079 }
5080
Sean Callanan5b26f272012-02-04 08:49:35 +00005081 namespace_decl = NamespaceDecl::Create(*ast,
5082 decl_ctx,
5083 false,
5084 SourceLocation(),
5085 SourceLocation(),
5086 &identifier_info,
5087 NULL);
Greg Clayton030a2042011-10-14 21:34:45 +00005088
Greg Clayton9d3d6882011-10-31 23:51:19 +00005089 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005090 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00005091 else
5092 {
5093 if (decl_ctx == translation_unit_decl)
5094 {
5095 namespace_decl = translation_unit_decl->getAnonymousNamespace();
5096 if (namespace_decl)
5097 return namespace_decl;
5098
Sean Callanan5b26f272012-02-04 08:49:35 +00005099 namespace_decl = NamespaceDecl::Create(*ast,
5100 decl_ctx,
5101 false,
5102 SourceLocation(),
5103 SourceLocation(),
5104 NULL,
5105 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00005106 translation_unit_decl->setAnonymousNamespace (namespace_decl);
5107 translation_unit_decl->addDecl (namespace_decl);
5108 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
5109 }
5110 else
5111 {
5112 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
5113 if (parent_namespace_decl)
5114 {
5115 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
5116 if (namespace_decl)
5117 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00005118 namespace_decl = NamespaceDecl::Create(*ast,
5119 decl_ctx,
5120 false,
5121 SourceLocation(),
5122 SourceLocation(),
5123 NULL,
5124 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00005125 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
5126 parent_namespace_decl->addDecl (namespace_decl);
5127 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
5128 }
5129 else
5130 {
5131 // BAD!!!
5132 }
5133 }
5134
5135
5136 if (namespace_decl)
5137 {
5138 // If we make it here, we are creating the anonymous namespace decl
5139 // for the first time, so we need to do the using directive magic
5140 // like SEMA does
5141 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast,
5142 decl_ctx,
5143 SourceLocation(),
5144 SourceLocation(),
5145 NestedNameSpecifierLoc(),
5146 SourceLocation(),
5147 namespace_decl,
5148 decl_ctx);
5149 using_directive_decl->setImplicit();
5150 decl_ctx->addDecl(using_directive_decl);
5151 }
5152 }
5153#ifdef LLDB_CONFIGURATION_DEBUG
5154 VerifyDecl(namespace_decl);
5155#endif
Greg Clayton030a2042011-10-14 21:34:45 +00005156 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005157}
5158
5159
5160#pragma mark Function Types
5161
5162FunctionDecl *
Greg Clayton147e1fa2011-10-14 22:47:18 +00005163ClangASTContext::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 +00005164{
Greg Clayton147e1fa2011-10-14 22:47:18 +00005165 FunctionDecl *func_decl = NULL;
5166 ASTContext *ast = getASTContext();
5167 if (decl_ctx == NULL)
5168 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005169
Greg Clayton147e1fa2011-10-14 22:47:18 +00005170 if (name && name[0])
5171 {
5172 func_decl = FunctionDecl::Create (*ast,
5173 decl_ctx,
5174 SourceLocation(),
5175 SourceLocation(),
5176 DeclarationName (&ast->Idents.get(name)),
5177 QualType::getFromOpaquePtr(function_clang_type),
5178 NULL,
5179 (FunctionDecl::StorageClass)storage,
5180 (FunctionDecl::StorageClass)storage,
5181 is_inline);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005182 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00005183 else
5184 {
5185 func_decl = FunctionDecl::Create (*ast,
5186 decl_ctx,
5187 SourceLocation(),
5188 SourceLocation(),
5189 DeclarationName (),
5190 QualType::getFromOpaquePtr(function_clang_type),
5191 NULL,
5192 (FunctionDecl::StorageClass)storage,
5193 (FunctionDecl::StorageClass)storage,
5194 is_inline);
5195 }
5196 if (func_decl)
5197 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005198
5199#ifdef LLDB_CONFIGURATION_DEBUG
5200 VerifyDecl(func_decl);
5201#endif
5202
Greg Clayton147e1fa2011-10-14 22:47:18 +00005203 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005204}
5205
Greg Clayton1be10fc2010-09-29 01:12:09 +00005206clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00005207ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00005208 clang_type_t result_type,
5209 clang_type_t *args,
Sean Callananc81256a2010-09-16 20:40:25 +00005210 unsigned num_args,
5211 bool is_variadic,
5212 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005213{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005214 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005215 std::vector<QualType> qual_type_args;
5216 for (unsigned i=0; i<num_args; ++i)
5217 qual_type_args.push_back (QualType::getFromOpaquePtr(args[i]));
5218
5219 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00005220 FunctionProtoType::ExtProtoInfo proto_info;
5221 proto_info.Variadic = is_variadic;
Sean Callananfb0b7582011-03-15 00:17:19 +00005222 proto_info.ExceptionSpecType = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005223 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00005224 proto_info.RefQualifier = RQ_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005225 proto_info.NumExceptions = 0;
5226 proto_info.Exceptions = NULL;
5227
Greg Clayton147e1fa2011-10-14 22:47:18 +00005228 return ast->getFunctionType (QualType::getFromOpaquePtr(result_type),
5229 qual_type_args.empty() ? NULL : &qual_type_args.front(),
5230 qual_type_args.size(),
5231 proto_info).getAsOpaquePtr(); // NoReturn);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005232}
5233
5234ParmVarDecl *
Greg Clayton1be10fc2010-09-29 01:12:09 +00005235ClangASTContext::CreateParameterDeclaration (const char *name, clang_type_t param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005236{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005237 ASTContext *ast = getASTContext();
5238 assert (ast != NULL);
5239 return ParmVarDecl::Create(*ast,
5240 ast->getTranslationUnitDecl(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005241 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005242 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00005243 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callananc81256a2010-09-16 20:40:25 +00005244 QualType::getFromOpaquePtr(param_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005245 NULL,
5246 (VarDecl::StorageClass)storage,
5247 (VarDecl::StorageClass)storage,
5248 0);
5249}
5250
5251void
5252ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
5253{
5254 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00005255 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005256}
5257
5258
5259#pragma mark Array Types
5260
Greg Clayton1be10fc2010-09-29 01:12:09 +00005261clang_type_t
Greg Clayton4ef877f2012-12-06 02:33:54 +00005262ClangASTContext::CreateArrayType (clang_type_t element_type, size_t element_count)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005263{
5264 if (element_type)
5265 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00005266 ASTContext *ast = getASTContext();
5267 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005268 llvm::APInt ap_element_count (64, element_count);
Greg Clayton4ef877f2012-12-06 02:33:54 +00005269 if (element_count == 0)
5270 {
5271 return ast->getIncompleteArrayType(QualType::getFromOpaquePtr(element_type),
5272 ArrayType::Normal,
5273 0).getAsOpaquePtr();
5274
5275 }
5276 else
5277 {
5278 return ast->getConstantArrayType(QualType::getFromOpaquePtr(element_type),
5279 ap_element_count,
5280 ArrayType::Normal,
5281 0).getAsOpaquePtr(); // ElemQuals
5282 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005283 }
5284 return NULL;
5285}
5286
5287
5288#pragma mark TagDecl
5289
5290bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005291ClangASTContext::StartTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005292{
5293 if (clang_type)
5294 {
5295 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00005296 const clang::Type *t = qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005297 if (t)
5298 {
Sean Callanan78e37602011-01-27 04:42:51 +00005299 const TagType *tag_type = dyn_cast<TagType>(t);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005300 if (tag_type)
5301 {
5302 TagDecl *tag_decl = tag_type->getDecl();
5303 if (tag_decl)
5304 {
5305 tag_decl->startDefinition();
5306 return true;
5307 }
5308 }
Sean Callanan5b26f272012-02-04 08:49:35 +00005309
5310 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(t);
5311 if (object_type)
5312 {
5313 ObjCInterfaceDecl *interface_decl = object_type->getInterface();
5314 if (interface_decl)
5315 {
5316 interface_decl->startDefinition();
5317 return true;
5318 }
5319 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005320 }
5321 }
5322 return false;
5323}
5324
5325bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005326ClangASTContext::CompleteTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005327{
5328 if (clang_type)
5329 {
5330 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton14372242010-09-29 03:44:17 +00005331
5332 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5333
5334 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005335 {
Greg Clayton14372242010-09-29 03:44:17 +00005336 cxx_record_decl->completeDefinition();
5337
5338 return true;
5339 }
5340
5341 const EnumType *enum_type = dyn_cast<EnumType>(qual_type.getTypePtr());
5342
5343 if (enum_type)
5344 {
5345 EnumDecl *enum_decl = enum_type->getDecl();
5346
5347 if (enum_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005348 {
Greg Clayton14372242010-09-29 03:44:17 +00005349 /// TODO This really needs to be fixed.
5350
5351 unsigned NumPositiveBits = 1;
5352 unsigned NumNegativeBits = 0;
5353
Greg Clayton6beaaa62011-01-17 03:46:26 +00005354 ASTContext *ast = getASTContext();
Greg Claytone02b8502010-10-12 04:29:14 +00005355
5356 QualType promotion_qual_type;
5357 // If the enum integer type is less than an integer in bit width,
5358 // then we must promote it to an integer size.
Greg Clayton6beaaa62011-01-17 03:46:26 +00005359 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
Greg Claytone02b8502010-10-12 04:29:14 +00005360 {
5361 if (enum_decl->getIntegerType()->isSignedIntegerType())
Greg Clayton6beaaa62011-01-17 03:46:26 +00005362 promotion_qual_type = ast->IntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005363 else
Greg Clayton6beaaa62011-01-17 03:46:26 +00005364 promotion_qual_type = ast->UnsignedIntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005365 }
5366 else
5367 promotion_qual_type = enum_decl->getIntegerType();
5368
5369 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
Greg Clayton14372242010-09-29 03:44:17 +00005370 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005371 }
5372 }
5373 }
5374 return false;
5375}
5376
5377
5378#pragma mark Enumeration Types
5379
Greg Clayton1be10fc2010-09-29 01:12:09 +00005380clang_type_t
Greg Claytonca512b32011-01-14 04:54:56 +00005381ClangASTContext::CreateEnumerationType
5382(
5383 const char *name,
5384 DeclContext *decl_ctx,
5385 const Declaration &decl,
5386 clang_type_t integer_qual_type
5387)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005388{
5389 // TODO: Do something intelligent with the Declaration object passed in
5390 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005391 ASTContext *ast = getASTContext();
5392 assert (ast != NULL);
Greg Claytone02b8502010-10-12 04:29:14 +00005393
5394 // TODO: ask about these...
5395// const bool IsScoped = false;
5396// const bool IsFixed = false;
5397
Greg Clayton6beaaa62011-01-17 03:46:26 +00005398 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00005399 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00005400 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00005401 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005402 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callanan48114472010-12-13 01:26:27 +00005403 NULL,
5404 false, // IsScoped
5405 false, // IsScopedUsingClassTag
5406 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00005407
5408
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005409 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00005410 {
5411 // TODO: check if we should be setting the promotion type too?
5412 enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00005413
5414 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
5415
Greg Clayton6beaaa62011-01-17 03:46:26 +00005416 return ast->getTagDeclType(enum_decl).getAsOpaquePtr();
Greg Clayton83ff3892010-09-12 23:17:56 +00005417 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005418 return NULL;
5419}
5420
Greg Clayton1be10fc2010-09-29 01:12:09 +00005421clang_type_t
5422ClangASTContext::GetEnumerationIntegerType (clang_type_t enum_clang_type)
5423{
5424 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5425
Sean Callanan78e37602011-01-27 04:42:51 +00005426 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Greg Clayton1be10fc2010-09-29 01:12:09 +00005427 if (clang_type)
5428 {
5429 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5430 if (enum_type)
5431 {
5432 EnumDecl *enum_decl = enum_type->getDecl();
5433 if (enum_decl)
5434 return enum_decl->getIntegerType().getAsOpaquePtr();
5435 }
5436 }
5437 return NULL;
5438}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005439bool
5440ClangASTContext::AddEnumerationValueToEnumerationType
5441(
Greg Clayton1be10fc2010-09-29 01:12:09 +00005442 clang_type_t enum_clang_type,
5443 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005444 const Declaration &decl,
5445 const char *name,
5446 int64_t enum_value,
5447 uint32_t enum_value_bit_size
5448)
5449{
5450 if (enum_clang_type && enumerator_clang_type && name)
5451 {
5452 // TODO: Do something intelligent with the Declaration object passed in
5453 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005454 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005455 IdentifierTable *identifier_table = getIdentifierTable();
5456
Greg Clayton6beaaa62011-01-17 03:46:26 +00005457 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005458 assert (identifier_table != NULL);
5459 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5460
Sean Callanan78e37602011-01-27 04:42:51 +00005461 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005462 if (clang_type)
5463 {
5464 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5465
5466 if (enum_type)
5467 {
5468 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, false);
5469 enum_llvm_apsint = enum_value;
5470 EnumConstantDecl *enumerator_decl =
Greg Clayton6beaaa62011-01-17 03:46:26 +00005471 EnumConstantDecl::Create (*ast,
5472 enum_type->getDecl(),
5473 SourceLocation(),
5474 name ? &identifier_table->get(name) : NULL, // Identifier
5475 QualType::getFromOpaquePtr(enumerator_clang_type),
5476 NULL,
5477 enum_llvm_apsint);
5478
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005479 if (enumerator_decl)
5480 {
5481 enum_type->getDecl()->addDecl(enumerator_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005482
5483#ifdef LLDB_CONFIGURATION_DEBUG
5484 VerifyDecl(enumerator_decl);
5485#endif
5486
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005487 return true;
5488 }
5489 }
5490 }
5491 }
5492 return false;
5493}
5494
5495#pragma mark Pointers & References
5496
Greg Clayton1be10fc2010-09-29 01:12:09 +00005497clang_type_t
5498ClangASTContext::CreatePointerType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005499{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005500 return CreatePointerType (getASTContext(), clang_type);
5501}
5502
5503clang_type_t
5504ClangASTContext::CreatePointerType (clang::ASTContext *ast, clang_type_t clang_type)
5505{
5506 if (ast && clang_type)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005507 {
5508 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5509
Greg Clayton737b9322010-09-13 03:32:57 +00005510 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5511 switch (type_class)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005512 {
5513 case clang::Type::ObjCObject:
5514 case clang::Type::ObjCInterface:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005515 return ast->getObjCObjectPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005516
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005517 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005518 return ast->getPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005519 }
5520 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005521 return NULL;
5522}
5523
Greg Clayton1be10fc2010-09-29 01:12:09 +00005524clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005525ClangASTContext::CreateLValueReferenceType (clang::ASTContext *ast,
5526 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005527{
5528 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005529 return ast->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005530 return NULL;
5531}
5532
Greg Clayton1be10fc2010-09-29 01:12:09 +00005533clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005534ClangASTContext::CreateRValueReferenceType (clang::ASTContext *ast,
5535 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005536{
5537 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005538 return ast->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005539 return NULL;
5540}
5541
Greg Clayton1be10fc2010-09-29 01:12:09 +00005542clang_type_t
5543ClangASTContext::CreateMemberPointerType (clang_type_t clang_pointee_type, clang_type_t clang_class_type)
Greg Clayton9b81a312010-06-12 01:20:30 +00005544{
5545 if (clang_pointee_type && clang_pointee_type)
5546 return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type),
5547 QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr();
5548 return NULL;
5549}
5550
Greg Clayton1a65ae12011-01-25 23:55:37 +00005551uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005552ClangASTContext::GetPointerBitSize ()
5553{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005554 ASTContext *ast = getASTContext();
5555 return ast->getTypeSize(ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005556}
5557
5558bool
Greg Clayton219cf312012-03-30 00:51:13 +00005559ClangASTContext::IsPossibleDynamicType (clang::ASTContext *ast,
5560 clang_type_t clang_type,
5561 clang_type_t *dynamic_pointee_type,
5562 bool check_cplusplus,
5563 bool check_objc)
Greg Claytondea8cb42011-06-29 22:09:02 +00005564{
5565 QualType pointee_qual_type;
5566 if (clang_type)
5567 {
5568 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5569 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5570 bool success = false;
5571 switch (type_class)
5572 {
5573 case clang::Type::Builtin:
Greg Clayton219cf312012-03-30 00:51:13 +00005574 if (check_objc && cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
Greg Claytondea8cb42011-06-29 22:09:02 +00005575 {
5576 if (dynamic_pointee_type)
5577 *dynamic_pointee_type = clang_type;
5578 return true;
5579 }
5580 break;
5581
5582 case clang::Type::ObjCObjectPointer:
Greg Clayton219cf312012-03-30 00:51:13 +00005583 if (check_objc)
5584 {
5585 if (dynamic_pointee_type)
5586 *dynamic_pointee_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5587 return true;
5588 }
5589 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005590
5591 case clang::Type::Pointer:
5592 pointee_qual_type = cast<PointerType>(qual_type)->getPointeeType();
5593 success = true;
5594 break;
5595
5596 case clang::Type::LValueReference:
5597 case clang::Type::RValueReference:
5598 pointee_qual_type = cast<ReferenceType>(qual_type)->getPointeeType();
5599 success = true;
5600 break;
5601
5602 case clang::Type::Typedef:
Greg Clayton70364252012-08-31 18:56:24 +00005603 return ClangASTContext::IsPossibleDynamicType (ast,
5604 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
5605 dynamic_pointee_type,
5606 check_cplusplus,
5607 check_objc);
Sean Callanan912855f2011-08-11 23:56:13 +00005608
5609 case clang::Type::Elaborated:
Greg Clayton70364252012-08-31 18:56:24 +00005610 return ClangASTContext::IsPossibleDynamicType (ast,
5611 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
5612 dynamic_pointee_type,
5613 check_cplusplus,
5614 check_objc);
Sean Callanan912855f2011-08-11 23:56:13 +00005615
Greg Claytondea8cb42011-06-29 22:09:02 +00005616 default:
5617 break;
5618 }
5619
5620 if (success)
5621 {
5622 // Check to make sure what we are pointing too is a possible dynamic C++ type
5623 // We currently accept any "void *" (in case we have a class that has been
5624 // watered down to an opaque pointer) and virtual C++ classes.
5625 const clang::Type::TypeClass pointee_type_class = pointee_qual_type->getTypeClass();
5626 switch (pointee_type_class)
5627 {
5628 case clang::Type::Builtin:
5629 switch (cast<clang::BuiltinType>(pointee_qual_type)->getKind())
5630 {
5631 case clang::BuiltinType::UnknownAny:
5632 case clang::BuiltinType::Void:
5633 if (dynamic_pointee_type)
5634 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5635 return true;
5636
5637 case clang::BuiltinType::NullPtr:
5638 case clang::BuiltinType::Bool:
5639 case clang::BuiltinType::Char_U:
5640 case clang::BuiltinType::UChar:
5641 case clang::BuiltinType::WChar_U:
5642 case clang::BuiltinType::Char16:
5643 case clang::BuiltinType::Char32:
5644 case clang::BuiltinType::UShort:
5645 case clang::BuiltinType::UInt:
5646 case clang::BuiltinType::ULong:
5647 case clang::BuiltinType::ULongLong:
5648 case clang::BuiltinType::UInt128:
5649 case clang::BuiltinType::Char_S:
5650 case clang::BuiltinType::SChar:
5651 case clang::BuiltinType::WChar_S:
5652 case clang::BuiltinType::Short:
5653 case clang::BuiltinType::Int:
5654 case clang::BuiltinType::Long:
5655 case clang::BuiltinType::LongLong:
5656 case clang::BuiltinType::Int128:
5657 case clang::BuiltinType::Float:
5658 case clang::BuiltinType::Double:
5659 case clang::BuiltinType::LongDouble:
5660 case clang::BuiltinType::Dependent:
5661 case clang::BuiltinType::Overload:
5662 case clang::BuiltinType::ObjCId:
5663 case clang::BuiltinType::ObjCClass:
5664 case clang::BuiltinType::ObjCSel:
5665 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00005666 case clang::BuiltinType::Half:
5667 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00005668 case clang::BuiltinType::PseudoObject:
Sean Callanan3d654b32012-09-24 22:25:51 +00005669 case clang::BuiltinType::BuiltinFn:
Greg Claytondea8cb42011-06-29 22:09:02 +00005670 break;
5671 }
5672 break;
5673
5674 case clang::Type::Record:
Greg Clayton219cf312012-03-30 00:51:13 +00005675 if (check_cplusplus)
Greg Claytondea8cb42011-06-29 22:09:02 +00005676 {
5677 CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
5678 if (cxx_record_decl)
5679 {
Greg Clayton70364252012-08-31 18:56:24 +00005680 bool is_complete = cxx_record_decl->isCompleteDefinition();
5681 if (!is_complete)
Sean Callanan6e4100ea2012-09-08 00:49:45 +00005682 is_complete = ClangASTContext::GetCompleteType (ast, pointee_qual_type.getAsOpaquePtr());
Greg Clayton70364252012-08-31 18:56:24 +00005683
5684 if (is_complete)
Greg Claytondea8cb42011-06-29 22:09:02 +00005685 {
5686 success = cxx_record_decl->isDynamicClass();
5687 }
5688 else
5689 {
Greg Clayton70364252012-08-31 18:56:24 +00005690 success = false;
Greg Claytondea8cb42011-06-29 22:09:02 +00005691 }
Greg Clayton70364252012-08-31 18:56:24 +00005692
Greg Claytondea8cb42011-06-29 22:09:02 +00005693 if (success)
5694 {
5695 if (dynamic_pointee_type)
5696 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5697 return true;
5698 }
5699 }
5700 }
5701 break;
5702
5703 case clang::Type::ObjCObject:
5704 case clang::Type::ObjCInterface:
Greg Clayton219cf312012-03-30 00:51:13 +00005705 if (check_objc)
5706 {
5707 if (dynamic_pointee_type)
5708 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5709 return true;
5710 }
5711 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005712
5713 default:
5714 break;
5715 }
5716 }
5717 }
5718 if (dynamic_pointee_type)
5719 *dynamic_pointee_type = NULL;
5720 return false;
5721}
5722
5723
5724bool
Greg Clayton007d5be2011-05-30 00:49:24 +00005725ClangASTContext::IsPossibleCPlusPlusDynamicType (clang::ASTContext *ast, clang_type_t clang_type, clang_type_t *dynamic_pointee_type)
5726{
Greg Clayton219cf312012-03-30 00:51:13 +00005727 return IsPossibleDynamicType (ast,
5728 clang_type,
5729 dynamic_pointee_type,
5730 true, // Check for dynamic C++ types
5731 false); // Check for dynamic ObjC types
Greg Clayton007d5be2011-05-30 00:49:24 +00005732}
5733
Sean Callanan98298012011-10-27 19:41:13 +00005734bool
5735ClangASTContext::IsReferenceType (clang_type_t clang_type, clang_type_t *target_type)
5736{
5737 if (clang_type == NULL)
5738 return false;
5739
5740 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5741 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5742
5743 switch (type_class)
5744 {
5745 case clang::Type::LValueReference:
5746 if (target_type)
5747 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5748 return true;
5749 case clang::Type::RValueReference:
5750 if (target_type)
5751 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5752 return true;
5753 case clang::Type::Typedef:
5754 return ClangASTContext::IsReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5755 case clang::Type::Elaborated:
5756 return ClangASTContext::IsReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5757 default:
5758 break;
5759 }
5760
5761 return false;
5762}
Greg Clayton007d5be2011-05-30 00:49:24 +00005763
5764bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005765ClangASTContext::IsPointerOrReferenceType (clang_type_t clang_type, clang_type_t*target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005766{
5767 if (clang_type == NULL)
5768 return false;
5769
5770 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005771 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5772 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005773 {
Sean Callanana2424172010-10-25 00:29:48 +00005774 case clang::Type::Builtin:
5775 switch (cast<clang::BuiltinType>(qual_type)->getKind())
5776 {
5777 default:
5778 break;
5779 case clang::BuiltinType::ObjCId:
5780 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00005781 return true;
5782 }
5783 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00005784 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005785 if (target_type)
5786 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5787 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005788 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005789 if (target_type)
5790 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5791 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005792 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005793 if (target_type)
5794 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5795 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005796 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005797 if (target_type)
5798 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5799 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005800 case clang::Type::LValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005801 if (target_type)
5802 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5803 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005804 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005805 if (target_type)
5806 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5807 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005808 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00005809 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00005810 case clang::Type::Elaborated:
5811 return ClangASTContext::IsPointerOrReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005812 default:
5813 break;
5814 }
5815 return false;
5816}
5817
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005818bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005819ClangASTContext::IsIntegerType (clang_type_t clang_type, bool &is_signed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005820{
5821 if (!clang_type)
5822 return false;
5823
5824 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5825 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal());
5826
5827 if (builtin_type)
5828 {
5829 if (builtin_type->isInteger())
Jim Inghamef651602011-12-22 19:12:40 +00005830 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005831 is_signed = builtin_type->isSignedInteger();
Jim Inghamef651602011-12-22 19:12:40 +00005832 return true;
5833 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005834 }
5835
5836 return false;
5837}
5838
5839bool
Greg Claytonaffb03b2011-07-08 18:27:39 +00005840ClangASTContext::IsPointerType (clang_type_t clang_type, clang_type_t *target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005841{
Greg Claytonaffb03b2011-07-08 18:27:39 +00005842 if (target_type)
5843 *target_type = NULL;
5844
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005845 if (clang_type)
5846 {
5847 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005848 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5849 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005850 {
Sean Callanana2424172010-10-25 00:29:48 +00005851 case clang::Type::Builtin:
5852 switch (cast<clang::BuiltinType>(qual_type)->getKind())
5853 {
5854 default:
5855 break;
5856 case clang::BuiltinType::ObjCId:
5857 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00005858 return true;
5859 }
5860 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00005861 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005862 if (target_type)
5863 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5864 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005865 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005866 if (target_type)
5867 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5868 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005869 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005870 if (target_type)
5871 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5872 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005873 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005874 if (target_type)
5875 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5876 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005877 case clang::Type::Typedef:
Greg Claytonaffb03b2011-07-08 18:27:39 +00005878 return ClangASTContext::IsPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), target_type);
Sean Callanan912855f2011-08-11 23:56:13 +00005879 case clang::Type::Elaborated:
5880 return ClangASTContext::IsPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), target_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005881 default:
5882 break;
5883 }
5884 }
5885 return false;
5886}
5887
5888bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005889ClangASTContext::IsFloatingPointType (clang_type_t clang_type, uint32_t &count, bool &is_complex)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005890{
5891 if (clang_type)
5892 {
5893 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5894
5895 if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()))
5896 {
5897 clang::BuiltinType::Kind kind = BT->getKind();
5898 if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble)
5899 {
5900 count = 1;
5901 is_complex = false;
5902 return true;
5903 }
5904 }
5905 else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal()))
5906 {
5907 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex))
5908 {
5909 count = 2;
5910 is_complex = true;
5911 return true;
5912 }
5913 }
5914 else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal()))
5915 {
5916 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex))
5917 {
5918 count = VT->getNumElements();
5919 is_complex = false;
5920 return true;
5921 }
5922 }
5923 }
5924 return false;
5925}
5926
Enrico Granata9fc19442011-07-06 02:13:41 +00005927bool
5928ClangASTContext::IsScalarType (lldb::clang_type_t clang_type)
5929{
5930 bool is_signed;
5931 if (ClangASTContext::IsIntegerType(clang_type, is_signed))
5932 return true;
5933
5934 uint32_t count;
5935 bool is_complex;
5936 return ClangASTContext::IsFloatingPointType(clang_type, count, is_complex) && !is_complex;
5937}
5938
5939bool
5940ClangASTContext::IsPointerToScalarType (lldb::clang_type_t clang_type)
5941{
5942 if (!IsPointerType(clang_type))
5943 return false;
5944
5945 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5946 lldb::clang_type_t pointee_type = qual_type.getTypePtr()->getPointeeType().getAsOpaquePtr();
5947 return IsScalarType(pointee_type);
5948}
5949
5950bool
5951ClangASTContext::IsArrayOfScalarType (lldb::clang_type_t clang_type)
5952{
Greg Clayton4ef877f2012-12-06 02:33:54 +00005953 clang_type = GetAsArrayType(clang_type, NULL, NULL, NULL);
Sean Callanan0caa21c2012-01-19 23:54:24 +00005954
5955 if (clang_type == 0)
Enrico Granata9fc19442011-07-06 02:13:41 +00005956 return false;
5957
5958 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5959 lldb::clang_type_t item_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
5960 return IsScalarType(item_type);
5961}
5962
Greg Clayton8f92f0a2010-10-14 22:52:14 +00005963
5964bool
5965ClangASTContext::GetCXXClassName (clang_type_t clang_type, std::string &class_name)
5966{
5967 if (clang_type)
5968 {
5969 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5970
5971 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5972 if (cxx_record_decl)
5973 {
5974 class_name.assign (cxx_record_decl->getIdentifier()->getNameStart());
5975 return true;
5976 }
5977 }
5978 class_name.clear();
5979 return false;
5980}
5981
5982
Greg Clayton0fffff52010-09-24 05:15:53 +00005983bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005984ClangASTContext::IsCXXClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00005985{
5986 if (clang_type)
5987 {
5988 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5989 if (qual_type->getAsCXXRecordDecl() != NULL)
5990 return true;
5991 }
5992 return false;
5993}
5994
Greg Clayton20568dd2011-10-13 23:13:20 +00005995bool
5996ClangASTContext::IsBeingDefined (lldb::clang_type_t clang_type)
5997{
5998 if (clang_type)
5999 {
6000 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6001 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type);
6002 if (tag_type)
6003 return tag_type->isBeingDefined();
6004 }
6005 return false;
6006}
6007
Greg Clayton0fffff52010-09-24 05:15:53 +00006008bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006009ClangASTContext::IsObjCClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00006010{
6011 if (clang_type)
6012 {
6013 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6014 if (qual_type->isObjCObjectOrInterfaceType())
6015 return true;
6016 }
6017 return false;
6018}
6019
Sean Callanan72772842012-02-22 23:57:45 +00006020bool
6021ClangASTContext::IsObjCObjectPointerType (lldb::clang_type_t clang_type, clang_type_t *class_type)
6022{
6023 if (clang_type)
6024 {
6025 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6026 if (qual_type->isObjCObjectPointerType())
6027 {
6028 if (class_type)
6029 {
6030 *class_type = NULL;
6031
6032 if (!qual_type->isObjCClassType() &&
6033 !qual_type->isObjCIdType())
6034 {
6035 const ObjCObjectPointerType *obj_pointer_type = dyn_cast<ObjCObjectPointerType>(qual_type);
Sean Callanand7dabe22012-03-10 01:59:11 +00006036 if (!obj_pointer_type)
6037 *class_type = NULL;
Sean Callanan1fc91ad2012-03-10 02:00:32 +00006038 else
6039 *class_type = QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr();
Sean Callanan72772842012-02-22 23:57:45 +00006040 }
6041 }
6042 return true;
6043 }
6044 }
6045 return false;
6046}
6047
6048bool
6049ClangASTContext::GetObjCClassName (lldb::clang_type_t clang_type,
6050 std::string &class_name)
6051{
6052 if (!clang_type)
6053 return false;
6054
6055 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(QualType::getFromOpaquePtr(clang_type));
6056 if (!object_type)
6057 return false;
6058
6059 const ObjCInterfaceDecl *interface = object_type->getInterface();
6060 if (!interface)
6061 return false;
6062
6063 class_name = interface->getNameAsString();
6064 return true;
6065}
Greg Clayton0fffff52010-09-24 05:15:53 +00006066
Greg Clayton73b472d2010-10-27 03:32:59 +00006067bool
6068ClangASTContext::IsCharType (clang_type_t clang_type)
6069{
6070 if (clang_type)
6071 return QualType::getFromOpaquePtr(clang_type)->isCharType();
6072 return false;
6073}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006074
6075bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006076ClangASTContext::IsCStringType (clang_type_t clang_type, uint32_t &length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006077{
Greg Clayton73b472d2010-10-27 03:32:59 +00006078 clang_type_t pointee_or_element_clang_type = NULL;
6079 Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, &pointee_or_element_clang_type));
6080
6081 if (pointee_or_element_clang_type == NULL)
6082 return false;
6083
6084 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006085 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006086 QualType pointee_or_element_qual_type (QualType::getFromOpaquePtr (pointee_or_element_clang_type));
6087
6088 if (pointee_or_element_qual_type.getUnqualifiedType()->isCharType())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006089 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006090 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6091 if (type_flags.Test (eTypeIsArray))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006092 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006093 // We know the size of the array and it could be a C string
6094 // since it is an array of characters
6095 length = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
6096 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006097 }
Greg Clayton73b472d2010-10-27 03:32:59 +00006098 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006099 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006100 length = 0;
6101 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006102 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006103
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006104 }
6105 }
6106 return false;
6107}
6108
6109bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006110ClangASTContext::IsFunctionPointerType (clang_type_t clang_type)
Greg Clayton737b9322010-09-13 03:32:57 +00006111{
6112 if (clang_type)
6113 {
6114 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6115
6116 if (qual_type->isFunctionPointerType())
6117 return true;
6118
6119 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6120 switch (type_class)
6121 {
Sean Callananfb0b7582011-03-15 00:17:19 +00006122 default:
6123 break;
Greg Clayton737b9322010-09-13 03:32:57 +00006124 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00006125 return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006126 case clang::Type::Elaborated:
6127 return ClangASTContext::IsFunctionPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00006128
6129 case clang::Type::LValueReference:
6130 case clang::Type::RValueReference:
6131 {
Sean Callanan78e37602011-01-27 04:42:51 +00006132 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00006133 if (reference_type)
6134 return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr());
6135 }
6136 break;
6137 }
6138 }
6139 return false;
6140}
6141
Greg Clayton73b472d2010-10-27 03:32:59 +00006142size_t
6143ClangASTContext::GetArraySize (clang_type_t clang_type)
6144{
6145 if (clang_type)
6146 {
Greg Claytonef37d68a2011-07-09 17:12:27 +00006147 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
6148 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6149 switch (type_class)
6150 {
6151 case clang::Type::ConstantArray:
6152 {
6153 const ConstantArrayType *array = cast<ConstantArrayType>(QualType::getFromOpaquePtr(clang_type).getTypePtr());
6154 if (array)
6155 return array->getSize().getLimitedValue();
6156 }
6157 break;
6158
6159 case clang::Type::Typedef:
6160 return ClangASTContext::GetArraySize(cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006161
6162 case clang::Type::Elaborated:
6163 return ClangASTContext::GetArraySize(cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00006164
6165 default:
6166 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006167 }
Greg Clayton73b472d2010-10-27 03:32:59 +00006168 }
6169 return 0;
6170}
Greg Clayton737b9322010-09-13 03:32:57 +00006171
Sean Callanan0caa21c2012-01-19 23:54:24 +00006172clang_type_t
Greg Clayton4ef877f2012-12-06 02:33:54 +00006173ClangASTContext::GetAsArrayType (clang_type_t clang_type, clang_type_t*member_type, uint64_t *size, bool *is_incomplete)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006174{
Greg Clayton4ef877f2012-12-06 02:33:54 +00006175 if (is_incomplete)
6176 *is_incomplete = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006177 if (!clang_type)
Sean Callanan0caa21c2012-01-19 23:54:24 +00006178 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006179
6180 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6181
Greg Clayton737b9322010-09-13 03:32:57 +00006182 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6183 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006184 {
Sean Callananfb0b7582011-03-15 00:17:19 +00006185 default:
6186 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006187
Greg Claytone1a916a2010-07-21 22:12:05 +00006188 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006189 if (member_type)
6190 *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6191 if (size)
Greg Claytonac4827f2011-04-01 18:14:08 +00006192 *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
Sean Callanan0caa21c2012-01-19 23:54:24 +00006193 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006194
Greg Claytone1a916a2010-07-21 22:12:05 +00006195 case clang::Type::IncompleteArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006196 if (member_type)
6197 *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6198 if (size)
6199 *size = 0;
Greg Clayton4ef877f2012-12-06 02:33:54 +00006200 if (is_incomplete)
6201 *is_incomplete = true;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006202 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006203
Greg Claytone1a916a2010-07-21 22:12:05 +00006204 case clang::Type::VariableArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006205 if (member_type)
6206 *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6207 if (size)
6208 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006209 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006210
Greg Claytone1a916a2010-07-21 22:12:05 +00006211 case clang::Type::DependentSizedArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006212 if (member_type)
6213 *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6214 if (size)
6215 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006216 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006217
6218 case clang::Type::Typedef:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006219 return ClangASTContext::GetAsArrayType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
6220 member_type,
Greg Clayton4ef877f2012-12-06 02:33:54 +00006221 size,
6222 is_incomplete);
Sean Callanan912855f2011-08-11 23:56:13 +00006223
6224 case clang::Type::Elaborated:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006225 return ClangASTContext::GetAsArrayType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
6226 member_type,
Greg Clayton4ef877f2012-12-06 02:33:54 +00006227 size,
6228 is_incomplete);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006229 }
Sean Callanan0caa21c2012-01-19 23:54:24 +00006230 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006231}
6232
6233
6234#pragma mark Typedefs
6235
Greg Clayton1be10fc2010-09-29 01:12:09 +00006236clang_type_t
6237ClangASTContext::CreateTypedefType (const char *name, clang_type_t clang_type, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006238{
6239 if (clang_type)
6240 {
6241 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006242 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006243 IdentifierTable *identifier_table = getIdentifierTable();
Greg Clayton6beaaa62011-01-17 03:46:26 +00006244 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006245 assert (identifier_table != NULL);
6246 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00006247 decl_ctx = ast->getTranslationUnitDecl();
6248 TypedefDecl *decl = TypedefDecl::Create (*ast,
6249 decl_ctx,
6250 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00006251 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00006252 name ? &identifier_table->get(name) : NULL, // Identifier
6253 ast->CreateTypeSourceInfo(qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00006254
Greg Clayton147e1fa2011-10-14 22:47:18 +00006255 //decl_ctx->addDecl (decl);
6256
Sean Callanan2652ad22011-01-18 01:03:44 +00006257 decl->setAccess(AS_public); // TODO respect proper access specifier
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006258
6259 // Get a uniqued QualType for the typedef decl type
Greg Clayton6beaaa62011-01-17 03:46:26 +00006260 return ast->getTypedefType (decl).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006261 }
6262 return NULL;
6263}
6264
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006265// Disable this for now since I can't seem to get a nicely formatted float
6266// out of the APFloat class without just getting the float, double or quad
6267// and then using a formatted print on it which defeats the purpose. We ideally
6268// would like to get perfect string values for any kind of float semantics
6269// so we can support remote targets. The code below also requires a patch to
6270// llvm::APInt.
6271//bool
Greg Clayton6beaaa62011-01-17 03:46:26 +00006272//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 +00006273//{
6274// uint32_t count = 0;
6275// bool is_complex = false;
6276// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6277// {
6278// unsigned num_bytes_per_float = byte_size / count;
6279// unsigned num_bits_per_float = num_bytes_per_float * 8;
6280//
6281// float_str.clear();
6282// uint32_t i;
6283// for (i=0; i<count; i++)
6284// {
6285// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
6286// bool is_ieee = false;
6287// APFloat ap_float(ap_int, is_ieee);
6288// char s[1024];
6289// unsigned int hex_digits = 0;
6290// bool upper_case = false;
6291//
6292// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
6293// {
6294// if (i > 0)
6295// float_str.append(", ");
6296// float_str.append(s);
6297// if (i == 1 && is_complex)
6298// float_str.append(1, 'i');
6299// }
6300// }
6301// return !float_str.empty();
6302// }
6303// return false;
6304//}
6305
6306size_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00006307ClangASTContext::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 +00006308{
6309 if (clang_type)
6310 {
6311 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6312 uint32_t count = 0;
6313 bool is_complex = false;
6314 if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6315 {
6316 // TODO: handle complex and vector types
6317 if (count != 1)
6318 return false;
6319
6320 StringRef s_sref(s);
Greg Clayton6beaaa62011-01-17 03:46:26 +00006321 APFloat ap_float(ast->getFloatTypeSemantics(qual_type), s_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006322
Greg Clayton6beaaa62011-01-17 03:46:26 +00006323 const uint64_t bit_size = ast->getTypeSize (qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006324 const uint64_t byte_size = bit_size / 8;
6325 if (dst_size >= byte_size)
6326 {
6327 if (bit_size == sizeof(float)*8)
6328 {
6329 float float32 = ap_float.convertToFloat();
6330 ::memcpy (dst, &float32, byte_size);
6331 return byte_size;
6332 }
6333 else if (bit_size >= 64)
6334 {
6335 llvm::APInt ap_int(ap_float.bitcastToAPInt());
6336 ::memcpy (dst, ap_int.getRawData(), byte_size);
6337 return byte_size;
6338 }
6339 }
6340 }
6341 }
6342 return 0;
6343}
Sean Callanan6fe64b52010-09-17 02:24:29 +00006344
6345unsigned
Greg Clayton1be10fc2010-09-29 01:12:09 +00006346ClangASTContext::GetTypeQualifiers(clang_type_t clang_type)
Sean Callanan6fe64b52010-09-17 02:24:29 +00006347{
6348 assert (clang_type);
6349
6350 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6351
6352 return qual_type.getQualifiers().getCVRQualifiers();
6353}
Greg Clayton6beaaa62011-01-17 03:46:26 +00006354
6355bool
6356ClangASTContext::GetCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6357{
6358 if (clang_type == NULL)
6359 return false;
6360
Greg Claytonc432c192011-01-20 04:18:48 +00006361 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006362}
6363
6364
6365bool
6366ClangASTContext::GetCompleteType (clang_type_t clang_type)
6367{
6368 return ClangASTContext::GetCompleteType (getASTContext(), clang_type);
6369}
6370
Greg Claytona2721472011-06-25 00:44:06 +00006371bool
Enrico Granata86027e92012-03-24 01:11:14 +00006372ClangASTContext::IsCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6373{
6374 if (clang_type == NULL)
6375 return false;
6376
6377 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type), false); // just check but don't let it actually complete
6378}
6379
6380
6381bool
6382ClangASTContext::IsCompleteType (clang_type_t clang_type)
6383{
6384 return ClangASTContext::IsCompleteType (getASTContext(), clang_type);
6385}
6386
6387bool
Greg Claytona2721472011-06-25 00:44:06 +00006388ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
6389 clang::Decl *decl)
6390{
6391 if (!decl)
6392 return false;
6393
6394 ExternalASTSource *ast_source = ast->getExternalSource();
6395
6396 if (!ast_source)
6397 return false;
6398
6399 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
6400 {
Greg Clayton219cf312012-03-30 00:51:13 +00006401 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006402 return true;
6403
6404 if (!tag_decl->hasExternalLexicalStorage())
6405 return false;
6406
6407 ast_source->CompleteType(tag_decl);
6408
6409 return !tag_decl->getTypeForDecl()->isIncompleteType();
6410 }
6411 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
6412 {
Sean Callanan5b26f272012-02-04 08:49:35 +00006413 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006414 return true;
6415
6416 if (!objc_interface_decl->hasExternalLexicalStorage())
6417 return false;
6418
6419 ast_source->CompleteType(objc_interface_decl);
6420
Sean Callanan5b26f272012-02-04 08:49:35 +00006421 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00006422 }
6423 else
6424 {
6425 return false;
6426 }
6427}
6428
Sean Callanan60217122012-04-13 00:10:03 +00006429void
Jim Ingham379397632012-10-27 02:54:13 +00006430ClangASTContext::SetMetadataAsUserID (uintptr_t object,
6431 user_id_t user_id)
6432{
6433 ClangASTMetadata meta_data;
6434 meta_data.SetUserID (user_id);
6435 SetMetadata (object, meta_data);
6436}
6437
6438void
Sean Callanan60217122012-04-13 00:10:03 +00006439ClangASTContext::SetMetadata (clang::ASTContext *ast,
6440 uintptr_t object,
Jim Ingham379397632012-10-27 02:54:13 +00006441 ClangASTMetadata &metadata)
Sean Callanan60217122012-04-13 00:10:03 +00006442{
6443 ClangExternalASTSourceCommon *external_source =
6444 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
6445
6446 if (external_source)
6447 external_source->SetMetadata(object, metadata);
6448}
6449
Jim Ingham379397632012-10-27 02:54:13 +00006450ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +00006451ClangASTContext::GetMetadata (clang::ASTContext *ast,
6452 uintptr_t object)
6453{
6454 ClangExternalASTSourceCommon *external_source =
6455 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
6456
6457 if (external_source && external_source->HasMetadata(object))
6458 return external_source->GetMetadata(object);
6459 else
Jim Ingham379397632012-10-27 02:54:13 +00006460 return NULL;
Sean Callanan60217122012-04-13 00:10:03 +00006461}
6462
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006463clang::DeclContext *
6464ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
6465{
Sean Callanana87bee82011-08-19 06:19:25 +00006466 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006467}
6468
6469clang::DeclContext *
6470ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
6471{
Sean Callanana87bee82011-08-19 06:19:25 +00006472 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006473}
6474
Greg Clayton685c88c2012-07-14 00:53:55 +00006475
6476bool
6477ClangASTContext::GetClassMethodInfoForDeclContext (clang::DeclContext *decl_ctx,
6478 lldb::LanguageType &language,
6479 bool &is_instance_method,
6480 ConstString &language_object_name)
6481{
6482 language_object_name.Clear();
6483 language = eLanguageTypeUnknown;
6484 is_instance_method = false;
6485
6486 if (decl_ctx)
6487 {
6488 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
6489 {
6490 if (method_decl->isStatic())
6491 {
6492 is_instance_method = false;
6493 }
6494 else
6495 {
6496 language_object_name.SetCString("this");
6497 is_instance_method = true;
6498 }
6499 language = eLanguageTypeC_plus_plus;
6500 return true;
6501 }
6502 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
6503 {
6504 // Both static and instance methods have a "self" object in objective C
6505 language_object_name.SetCString("self");
6506 if (method_decl->isInstanceMethod())
6507 {
6508 is_instance_method = true;
6509 }
6510 else
6511 {
6512 is_instance_method = false;
6513 }
6514 language = eLanguageTypeObjC;
6515 return true;
6516 }
Jim Ingham379397632012-10-27 02:54:13 +00006517 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
6518 {
6519 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), (uintptr_t) function_decl);
6520 if (metadata && metadata->HasObjectPtr())
6521 {
6522 language_object_name.SetCString (metadata->GetObjectPtrName());
6523 language = eLanguageTypeObjC;
6524 is_instance_method = true;
6525 }
6526 return true;
6527 }
Greg Clayton685c88c2012-07-14 00:53:55 +00006528 }
6529 return false;
6530}
6531