blob: 60be93fe9345918c9dc9070f7d4ca77509cdfbbe [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());
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002796
2797 case clang::Type::Elaborated:
2798 return ClangASTContext::GetNumTemplateArguments (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2799
Greg Clayton402230e2012-02-03 01:30:30 +00002800 default:
2801 break;
2802 }
2803 }
2804 return 0;
2805}
2806
2807clang_type_t
2808ClangASTContext::GetTemplateArgument (clang::ASTContext *ast, clang_type_t clang_type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
2809{
2810 if (clang_type)
2811 {
2812 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2813
2814 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2815 switch (type_class)
2816 {
2817 case clang::Type::Record:
2818 if (GetCompleteQualType (ast, qual_type))
2819 {
2820 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2821 if (cxx_record_decl)
2822 {
2823 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2824 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
2825 {
2826 const TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
2827 switch (template_arg.getKind())
2828 {
2829 case clang::TemplateArgument::Null:
2830 kind = eTemplateArgumentKindNull;
2831 return NULL;
2832
2833 case clang::TemplateArgument::Type:
2834 kind = eTemplateArgumentKindType;
2835 return template_arg.getAsType().getAsOpaquePtr();
2836
2837 case clang::TemplateArgument::Declaration:
2838 kind = eTemplateArgumentKindDeclaration;
2839 return NULL;
2840
2841 case clang::TemplateArgument::Integral:
2842 kind = eTemplateArgumentKindIntegral;
2843 return template_arg.getIntegralType().getAsOpaquePtr();
2844
2845 case clang::TemplateArgument::Template:
2846 kind = eTemplateArgumentKindTemplate;
2847 return NULL;
2848
2849 case clang::TemplateArgument::TemplateExpansion:
2850 kind = eTemplateArgumentKindTemplateExpansion;
2851 return NULL;
2852
2853 case clang::TemplateArgument::Expression:
2854 kind = eTemplateArgumentKindExpression;
2855 return NULL;
2856
2857 case clang::TemplateArgument::Pack:
2858 kind = eTemplateArgumentKindPack;
2859 return NULL;
2860
2861 default:
2862 assert (!"Unhandled TemplateArgument::ArgKind");
2863 kind = eTemplateArgumentKindNull;
2864 return NULL;
2865 }
2866 }
2867 }
2868 }
2869 break;
2870
2871 case clang::Type::Typedef:
2872 return ClangASTContext::GetTemplateArgument (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), arg_idx, kind);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002873
2874 case clang::Type::Elaborated:
2875 return ClangASTContext::GetTemplateArgument (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), arg_idx, kind);
2876
Greg Clayton402230e2012-02-03 01:30:30 +00002877 default:
2878 break;
2879 }
2880 }
2881 kind = eTemplateArgumentKindNull;
2882 return NULL;
2883}
Greg Clayton0fffff52010-09-24 05:15:53 +00002884
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002885uint32_t
Greg Clayton73b472d2010-10-27 03:32:59 +00002886ClangASTContext::GetTypeInfo
2887(
2888 clang_type_t clang_type,
Greg Clayton6beaaa62011-01-17 03:46:26 +00002889 clang::ASTContext *ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00002890 clang_type_t *pointee_or_element_clang_type
2891)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002892{
2893 if (clang_type == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00002894 return 0;
2895
2896 if (pointee_or_element_clang_type)
2897 *pointee_or_element_clang_type = NULL;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002898
2899 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2900
2901 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2902 switch (type_class)
2903 {
Sean Callanana2424172010-10-25 00:29:48 +00002904 case clang::Type::Builtin:
2905 switch (cast<clang::BuiltinType>(qual_type)->getKind())
2906 {
Sean Callanana2424172010-10-25 00:29:48 +00002907 case clang::BuiltinType::ObjCId:
2908 case clang::BuiltinType::ObjCClass:
Greg Clayton6beaaa62011-01-17 03:46:26 +00002909 if (ast && pointee_or_element_clang_type)
2910 *pointee_or_element_clang_type = ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Sean Callanana2424172010-10-25 00:29:48 +00002911 return eTypeIsBuiltIn | eTypeIsPointer | eTypeHasValue;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002912 break;
2913 case clang::BuiltinType::Bool:
2914 case clang::BuiltinType::Char_U:
2915 case clang::BuiltinType::UChar:
2916 case clang::BuiltinType::WChar_U:
2917 case clang::BuiltinType::Char16:
2918 case clang::BuiltinType::Char32:
2919 case clang::BuiltinType::UShort:
2920 case clang::BuiltinType::UInt:
2921 case clang::BuiltinType::ULong:
2922 case clang::BuiltinType::ULongLong:
2923 case clang::BuiltinType::UInt128:
2924 case clang::BuiltinType::Char_S:
2925 case clang::BuiltinType::SChar:
2926 case clang::BuiltinType::WChar_S:
2927 case clang::BuiltinType::Short:
2928 case clang::BuiltinType::Int:
2929 case clang::BuiltinType::Long:
2930 case clang::BuiltinType::LongLong:
2931 case clang::BuiltinType::Int128:
2932 case clang::BuiltinType::Float:
2933 case clang::BuiltinType::Double:
2934 case clang::BuiltinType::LongDouble:
2935 return eTypeIsBuiltIn | eTypeHasValue | eTypeIsScalar;
Greg Clayton73b472d2010-10-27 03:32:59 +00002936 default:
2937 break;
Sean Callanana2424172010-10-25 00:29:48 +00002938 }
2939 return eTypeIsBuiltIn | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002940
2941 case clang::Type::BlockPointer:
2942 if (pointee_or_element_clang_type)
2943 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2944 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
2945
Greg Clayton49462ea2011-01-15 02:52:14 +00002946 case clang::Type::Complex: return eTypeIsBuiltIn | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002947
2948 case clang::Type::ConstantArray:
2949 case clang::Type::DependentSizedArray:
2950 case clang::Type::IncompleteArray:
2951 case clang::Type::VariableArray:
2952 if (pointee_or_element_clang_type)
2953 *pointee_or_element_clang_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
2954 return eTypeHasChildren | eTypeIsArray;
2955
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002956 case clang::Type::DependentName: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002957 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
2958 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
2959 case clang::Type::Decltype: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00002960
2961 case clang::Type::Enum:
2962 if (pointee_or_element_clang_type)
2963 *pointee_or_element_clang_type = cast<EnumType>(qual_type)->getDecl()->getIntegerType().getAsOpaquePtr();
2964 return eTypeIsEnumeration | eTypeHasValue;
2965
Sean Callanan912855f2011-08-11 23:56:13 +00002966 case clang::Type::Elaborated:
2967 return ClangASTContext::GetTypeInfo (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2968 ast,
2969 pointee_or_element_clang_type);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002970 case clang::Type::ExtVector: return eTypeHasChildren | eTypeIsVector;
2971 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
2972 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002973 case clang::Type::InjectedClassName: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00002974
2975 case clang::Type::LValueReference:
2976 case clang::Type::RValueReference:
2977 if (pointee_or_element_clang_type)
2978 *pointee_or_element_clang_type = cast<ReferenceType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr();
2979 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
2980
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002981 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002982
2983 case clang::Type::ObjCObjectPointer:
2984 if (pointee_or_element_clang_type)
2985 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2986 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
2987
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002988 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
2989 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
Greg Clayton73b472d2010-10-27 03:32:59 +00002990
2991 case clang::Type::Pointer:
2992 if (pointee_or_element_clang_type)
2993 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2994 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
2995
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002996 case clang::Type::Record:
2997 if (qual_type->getAsCXXRecordDecl())
2998 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
2999 else
3000 return eTypeHasChildren | eTypeIsStructUnion;
3001 break;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003002 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
3003 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
3004 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
Greg Clayton73b472d2010-10-27 03:32:59 +00003005
3006 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00003007 return eTypeIsTypedef | ClangASTContext::GetTypeInfo (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00003008 ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00003009 pointee_or_element_clang_type);
3010
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003011 case clang::Type::TypeOfExpr: return 0;
3012 case clang::Type::TypeOf: return 0;
3013 case clang::Type::UnresolvedUsing: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003014 case clang::Type::Vector: return eTypeHasChildren | eTypeIsVector;
3015 default: return 0;
3016 }
3017 return 0;
3018}
3019
Greg Clayton9e409562010-07-28 02:04:09 +00003020
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003021#pragma mark Aggregate Types
3022
3023bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00003024ClangASTContext::IsAggregateType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003025{
3026 if (clang_type == NULL)
3027 return false;
3028
3029 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3030
Greg Clayton737b9322010-09-13 03:32:57 +00003031 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3032 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003033 {
Greg Claytone1a916a2010-07-21 22:12:05 +00003034 case clang::Type::IncompleteArray:
3035 case clang::Type::VariableArray:
3036 case clang::Type::ConstantArray:
3037 case clang::Type::ExtVector:
3038 case clang::Type::Vector:
3039 case clang::Type::Record:
Greg Clayton9e409562010-07-28 02:04:09 +00003040 case clang::Type::ObjCObject:
3041 case clang::Type::ObjCInterface:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003042 return true;
Sean Callanan912855f2011-08-11 23:56:13 +00003043 case clang::Type::Elaborated:
3044 return ClangASTContext::IsAggregateType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Claytone1a916a2010-07-21 22:12:05 +00003045 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00003046 return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003047
3048 default:
3049 break;
3050 }
3051 // The clang type does have a value
3052 return false;
3053}
3054
3055uint32_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00003056ClangASTContext::GetNumChildren (clang::ASTContext *ast, clang_type_t clang_type, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003057{
Greg Clayton6beaaa62011-01-17 03:46:26 +00003058 if (clang_type == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003059 return 0;
3060
3061 uint32_t num_children = 0;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003062 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00003063 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3064 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003065 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003066 case clang::Type::Builtin:
3067 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3068 {
Greg Clayton73b472d2010-10-27 03:32:59 +00003069 case clang::BuiltinType::ObjCId: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003070 case clang::BuiltinType::ObjCClass: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003071 num_children = 1;
Greg Clayton73b472d2010-10-27 03:32:59 +00003072 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003073
3074 default:
3075 break;
3076 }
3077 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00003078
Greg Clayton49462ea2011-01-15 02:52:14 +00003079 case clang::Type::Complex: return 0;
Greg Clayton54979cd2010-12-15 05:08:08 +00003080
Greg Claytone1a916a2010-07-21 22:12:05 +00003081 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00003082 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003083 {
3084 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3085 const RecordDecl *record_decl = record_type->getDecl();
3086 assert(record_decl);
3087 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3088 if (cxx_record_decl)
3089 {
3090 if (omit_empty_base_classes)
3091 {
3092 // Check each base classes to see if it or any of its
3093 // base classes contain any fields. This can help
3094 // limit the noise in variable views by not having to
3095 // show base classes that contain no members.
3096 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3097 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3098 base_class != base_class_end;
3099 ++base_class)
3100 {
3101 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3102
3103 // Skip empty base classes
3104 if (RecordHasFields(base_class_decl) == false)
3105 continue;
3106
3107 num_children++;
3108 }
3109 }
3110 else
3111 {
3112 // Include all base classes
3113 num_children += cxx_record_decl->getNumBases();
3114 }
3115
3116 }
3117 RecordDecl::field_iterator field, field_end;
3118 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3119 ++num_children;
3120 }
3121 break;
3122
Greg Clayton9e409562010-07-28 02:04:09 +00003123 case clang::Type::ObjCObject:
3124 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00003125 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00003126 {
Sean Callanan78e37602011-01-27 04:42:51 +00003127 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003128 assert (objc_class_type);
3129 if (objc_class_type)
3130 {
3131 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3132
3133 if (class_interface_decl)
3134 {
3135
3136 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3137 if (superclass_interface_decl)
3138 {
3139 if (omit_empty_base_classes)
3140 {
3141 if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true))
3142 ++num_children;
3143 }
3144 else
3145 ++num_children;
3146 }
3147
3148 num_children += class_interface_decl->ivar_size();
3149 }
3150 }
3151 }
3152 break;
3153
3154 case clang::Type::ObjCObjectPointer:
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003155 {
Sean Callanan78e37602011-01-27 04:42:51 +00003156 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003157 QualType pointee_type = pointer_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003158 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3159 pointee_type.getAsOpaquePtr(),
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003160 omit_empty_base_classes);
3161 // If this type points to a simple type, then it has 1 child
3162 if (num_pointee_children == 0)
3163 num_children = 1;
3164 else
3165 num_children = num_pointee_children;
3166 }
3167 break;
Greg Clayton9e409562010-07-28 02:04:09 +00003168
Greg Claytone1a916a2010-07-21 22:12:05 +00003169 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003170 num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
3171 break;
3172
Greg Claytone1a916a2010-07-21 22:12:05 +00003173 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003174 {
Sean Callanan78e37602011-01-27 04:42:51 +00003175 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003176 QualType pointee_type (pointer_type->getPointeeType());
Greg Clayton6beaaa62011-01-17 03:46:26 +00003177 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3178 pointee_type.getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00003179 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003180 if (num_pointee_children == 0)
Greg Clayton54979cd2010-12-15 05:08:08 +00003181 {
3182 // We have a pointer to a pointee type that claims it has no children.
3183 // We will want to look at
3184 num_children = ClangASTContext::GetNumPointeeChildren (pointee_type.getAsOpaquePtr());
3185 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003186 else
3187 num_children = num_pointee_children;
3188 }
3189 break;
3190
Greg Clayton73b472d2010-10-27 03:32:59 +00003191 case clang::Type::LValueReference:
3192 case clang::Type::RValueReference:
3193 {
Sean Callanan78e37602011-01-27 04:42:51 +00003194 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton73b472d2010-10-27 03:32:59 +00003195 QualType pointee_type = reference_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003196 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3197 pointee_type.getAsOpaquePtr(),
Greg Clayton73b472d2010-10-27 03:32:59 +00003198 omit_empty_base_classes);
3199 // If this type points to a simple type, then it has 1 child
3200 if (num_pointee_children == 0)
3201 num_children = 1;
3202 else
3203 num_children = num_pointee_children;
3204 }
3205 break;
3206
3207
Greg Claytone1a916a2010-07-21 22:12:05 +00003208 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00003209 num_children = ClangASTContext::GetNumChildren (ast,
3210 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3211 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003212 break;
Sean Callanan912855f2011-08-11 23:56:13 +00003213
3214 case clang::Type::Elaborated:
3215 num_children = ClangASTContext::GetNumChildren (ast,
3216 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3217 omit_empty_base_classes);
3218 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003219
3220 default:
3221 break;
3222 }
3223 return num_children;
3224}
3225
Greg Claytonbf2331c2011-09-09 23:04:00 +00003226uint32_t
3227ClangASTContext::GetNumDirectBaseClasses (clang::ASTContext *ast, clang_type_t clang_type)
3228{
3229 if (clang_type == NULL)
3230 return 0;
3231
3232 uint32_t count = 0;
3233 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3234 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3235 switch (type_class)
3236 {
3237 case clang::Type::Record:
3238 if (GetCompleteQualType (ast, qual_type))
3239 {
3240 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3241 if (cxx_record_decl)
3242 count = cxx_record_decl->getNumBases();
3243 }
3244 break;
3245
3246 case clang::Type::ObjCObject:
3247 case clang::Type::ObjCInterface:
3248 if (GetCompleteQualType (ast, qual_type))
3249 {
3250 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3251 if (objc_class_type)
3252 {
3253 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3254
3255 if (class_interface_decl && class_interface_decl->getSuperClass())
3256 count = 1;
3257 }
3258 }
3259 break;
3260
3261
3262 case clang::Type::Typedef:
3263 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3264 break;
3265
3266 case clang::Type::Elaborated:
3267 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3268 break;
3269
3270 default:
3271 break;
3272 }
3273 return count;
3274}
3275
3276uint32_t
3277ClangASTContext::GetNumVirtualBaseClasses (clang::ASTContext *ast,
3278 clang_type_t clang_type)
3279{
3280 if (clang_type == NULL)
3281 return 0;
3282
3283 uint32_t count = 0;
3284 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3285 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3286 switch (type_class)
3287 {
3288 case clang::Type::Record:
3289 if (GetCompleteQualType (ast, qual_type))
3290 {
3291 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3292 if (cxx_record_decl)
3293 count = cxx_record_decl->getNumVBases();
3294 }
3295 break;
3296
3297 case clang::Type::Typedef:
3298 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3299 break;
3300
3301 case clang::Type::Elaborated:
3302 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3303 break;
3304
3305 default:
3306 break;
3307 }
3308 return count;
3309}
3310
3311uint32_t
3312ClangASTContext::GetNumFields (clang::ASTContext *ast, clang_type_t clang_type)
3313{
3314 if (clang_type == NULL)
3315 return 0;
3316
3317 uint32_t count = 0;
3318 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3319 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3320 switch (type_class)
3321 {
3322 case clang::Type::Record:
3323 if (GetCompleteQualType (ast, qual_type))
3324 {
3325 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
3326 if (record_type)
3327 {
3328 RecordDecl *record_decl = record_type->getDecl();
3329 if (record_decl)
3330 {
3331 uint32_t field_idx = 0;
3332 RecordDecl::field_iterator field, field_end;
3333 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3334 ++field_idx;
3335 count = field_idx;
3336 }
3337 }
3338 }
3339 break;
3340
3341 case clang::Type::Typedef:
3342 count = ClangASTContext::GetNumFields (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3343 break;
3344
3345 case clang::Type::Elaborated:
3346 count = ClangASTContext::GetNumFields (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3347 break;
3348
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003349 case clang::Type::ObjCObject:
3350 case clang::Type::ObjCInterface:
3351 if (GetCompleteQualType (ast, qual_type))
3352 {
3353 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3354 if (objc_class_type)
3355 {
3356 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3357
3358 if (class_interface_decl)
3359 count = class_interface_decl->ivar_size();
3360 }
3361 }
3362 break;
3363
Greg Claytonbf2331c2011-09-09 23:04:00 +00003364 default:
3365 break;
3366 }
3367 return count;
3368}
3369
3370clang_type_t
3371ClangASTContext::GetDirectBaseClassAtIndex (clang::ASTContext *ast,
3372 clang_type_t clang_type,
3373 uint32_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003374 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003375{
3376 if (clang_type == NULL)
3377 return 0;
3378
3379 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3380 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3381 switch (type_class)
3382 {
3383 case clang::Type::Record:
3384 if (GetCompleteQualType (ast, qual_type))
3385 {
3386 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3387 if (cxx_record_decl)
3388 {
3389 uint32_t curr_idx = 0;
3390 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3391 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3392 base_class != base_class_end;
3393 ++base_class, ++curr_idx)
3394 {
3395 if (curr_idx == idx)
3396 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003397 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003398 {
3399 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3400 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3401// if (base_class->isVirtual())
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003402// *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003403// else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003404 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003405 }
3406 return base_class->getType().getAsOpaquePtr();
3407 }
3408 }
3409 }
3410 }
3411 break;
3412
3413 case clang::Type::ObjCObject:
3414 case clang::Type::ObjCInterface:
3415 if (idx == 0 && GetCompleteQualType (ast, qual_type))
3416 {
3417 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3418 if (objc_class_type)
3419 {
3420 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3421
3422 if (class_interface_decl)
3423 {
3424 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3425 if (superclass_interface_decl)
3426 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003427 if (bit_offset_ptr)
3428 *bit_offset_ptr = 0;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003429 return ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr();
3430 }
3431 }
3432 }
3433 }
3434 break;
3435
3436
3437 case clang::Type::Typedef:
3438 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3439 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3440 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003441 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003442
3443 case clang::Type::Elaborated:
3444 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3445 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3446 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003447 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003448
3449 default:
3450 break;
3451 }
3452 return NULL;
3453}
3454
3455clang_type_t
3456ClangASTContext::GetVirtualBaseClassAtIndex (clang::ASTContext *ast,
3457 clang_type_t clang_type,
3458 uint32_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003459 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003460{
3461 if (clang_type == NULL)
3462 return 0;
3463
3464 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3465 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3466 switch (type_class)
3467 {
3468 case clang::Type::Record:
3469 if (GetCompleteQualType (ast, qual_type))
3470 {
3471 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3472 if (cxx_record_decl)
3473 {
3474 uint32_t curr_idx = 0;
3475 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3476 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
3477 base_class != base_class_end;
3478 ++base_class, ++curr_idx)
3479 {
3480 if (curr_idx == idx)
3481 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003482 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003483 {
3484 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3485 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003486 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003487
3488 }
3489 return base_class->getType().getAsOpaquePtr();
3490 }
3491 }
3492 }
3493 }
3494 break;
3495
3496 case clang::Type::Typedef:
3497 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3498 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3499 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003500 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003501
3502 case clang::Type::Elaborated:
3503 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3504 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3505 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003506 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003507
3508 default:
3509 break;
3510 }
3511 return NULL;
3512}
3513
3514clang_type_t
3515ClangASTContext::GetFieldAtIndex (clang::ASTContext *ast,
3516 clang_type_t clang_type,
3517 uint32_t idx,
3518 std::string& name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003519 uint64_t *bit_offset_ptr,
3520 uint32_t *bitfield_bit_size_ptr,
3521 bool *is_bitfield_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003522{
3523 if (clang_type == NULL)
3524 return 0;
3525
3526 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3527 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3528 switch (type_class)
3529 {
3530 case clang::Type::Record:
3531 if (GetCompleteQualType (ast, qual_type))
3532 {
3533 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3534 const RecordDecl *record_decl = record_type->getDecl();
3535 uint32_t field_idx = 0;
3536 RecordDecl::field_iterator field, field_end;
3537 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
3538 {
3539 if (idx == field_idx)
3540 {
3541 // Print the member type if requested
3542 // Print the member name and equal sign
3543 name.assign(field->getNameAsString());
3544
3545 // Figure out the type byte size (field_type_info.first) and
3546 // alignment (field_type_info.second) from the AST context.
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003547 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003548 {
3549 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003550 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003551 }
3552
Greg Clayton1811b4f2012-07-31 23:39:10 +00003553 const bool is_bitfield = field->isBitField();
3554
3555 if (bitfield_bit_size_ptr)
3556 {
3557 *bitfield_bit_size_ptr = 0;
3558
3559 if (is_bitfield && ast)
3560 {
3561 Expr *bitfield_bit_size_expr = field->getBitWidth();
3562 llvm::APSInt bitfield_apsint;
3563 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
3564 {
3565 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
3566 }
3567 }
3568 }
3569 if (is_bitfield_ptr)
3570 *is_bitfield_ptr = is_bitfield;
3571
Greg Claytonbf2331c2011-09-09 23:04:00 +00003572 return field->getType().getAsOpaquePtr();
3573 }
3574 }
3575 }
3576 break;
3577
3578 case clang::Type::ObjCObject:
3579 case clang::Type::ObjCInterface:
3580 if (GetCompleteQualType (ast, qual_type))
3581 {
3582 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3583 assert (objc_class_type);
3584 if (objc_class_type)
3585 {
3586 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3587
3588 if (class_interface_decl)
3589 {
3590 if (idx < (class_interface_decl->ivar_size()))
3591 {
3592 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
3593 uint32_t ivar_idx = 0;
3594
3595 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
3596 {
3597 if (ivar_idx == idx)
3598 {
3599 const ObjCIvarDecl* ivar_decl = *ivar_pos;
3600
3601 QualType ivar_qual_type(ivar_decl->getType());
3602
3603 name.assign(ivar_decl->getNameAsString());
3604
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003605 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003606 {
3607 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003608 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003609 }
3610
Greg Clayton1811b4f2012-07-31 23:39:10 +00003611 const bool is_bitfield = ivar_pos->isBitField();
3612
3613 if (bitfield_bit_size_ptr)
3614 {
3615 *bitfield_bit_size_ptr = 0;
3616
3617 if (is_bitfield && ast)
3618 {
3619 Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
3620 llvm::APSInt bitfield_apsint;
3621 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
3622 {
3623 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
3624 }
3625 }
3626 }
3627 if (is_bitfield_ptr)
3628 *is_bitfield_ptr = is_bitfield;
3629
Greg Claytonbf2331c2011-09-09 23:04:00 +00003630 return ivar_qual_type.getAsOpaquePtr();
3631 }
3632 }
3633 }
3634 }
3635 }
3636 }
3637 break;
3638
3639
3640 case clang::Type::Typedef:
3641 return ClangASTContext::GetFieldAtIndex (ast,
3642 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3643 idx,
3644 name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003645 bit_offset_ptr,
3646 bitfield_bit_size_ptr,
3647 is_bitfield_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003648
3649 case clang::Type::Elaborated:
3650 return ClangASTContext::GetFieldAtIndex (ast,
3651 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3652 idx,
3653 name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003654 bit_offset_ptr,
3655 bitfield_bit_size_ptr,
3656 is_bitfield_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003657
3658 default:
3659 break;
3660 }
3661 return NULL;
3662}
3663
Greg Claytoneaafa732012-10-13 00:20:27 +00003664lldb::BasicType
3665ClangASTContext::GetLLDBBasicTypeEnumeration (clang_type_t clang_type)
3666{
3667 if (clang_type)
3668 {
3669 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3670 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Claytonc4c5e892012-10-15 21:16:43 +00003671 if (type_class == clang::Type::Builtin)
Greg Claytoneaafa732012-10-13 00:20:27 +00003672 {
Greg Claytoneaafa732012-10-13 00:20:27 +00003673 switch (cast<clang::BuiltinType>(qual_type)->getKind())
Greg Claytonc4c5e892012-10-15 21:16:43 +00003674 {
Greg Claytoneaafa732012-10-13 00:20:27 +00003675 case clang::BuiltinType::Void: return eBasicTypeVoid;
3676 case clang::BuiltinType::Bool: return eBasicTypeBool;
3677 case clang::BuiltinType::Char_S: return eBasicTypeSignedChar;
3678 case clang::BuiltinType::Char_U: return eBasicTypeUnsignedChar;
3679 case clang::BuiltinType::Char16: return eBasicTypeChar16;
3680 case clang::BuiltinType::Char32: return eBasicTypeChar32;
3681 case clang::BuiltinType::UChar: return eBasicTypeUnsignedChar;
3682 case clang::BuiltinType::SChar: return eBasicTypeSignedChar;
3683 case clang::BuiltinType::WChar_S: return eBasicTypeSignedWChar;
3684 case clang::BuiltinType::WChar_U: return eBasicTypeUnsignedWChar;
3685 case clang::BuiltinType::Short: return eBasicTypeShort;
3686 case clang::BuiltinType::UShort: return eBasicTypeUnsignedShort;
3687 case clang::BuiltinType::Int: return eBasicTypeInt;
3688 case clang::BuiltinType::UInt: return eBasicTypeUnsignedInt;
3689 case clang::BuiltinType::Long: return eBasicTypeLong;
3690 case clang::BuiltinType::ULong: return eBasicTypeUnsignedLong;
3691 case clang::BuiltinType::LongLong: return eBasicTypeLongLong;
3692 case clang::BuiltinType::ULongLong: return eBasicTypeUnsignedLongLong;
3693 case clang::BuiltinType::Int128: return eBasicTypeInt128;
3694 case clang::BuiltinType::UInt128: return eBasicTypeUnsignedInt128;
3695
3696 case clang::BuiltinType::Half: return eBasicTypeHalf;
3697 case clang::BuiltinType::Float: return eBasicTypeFloat;
3698 case clang::BuiltinType::Double: return eBasicTypeDouble;
3699 case clang::BuiltinType::LongDouble:return eBasicTypeLongDouble;
3700
3701 case clang::BuiltinType::NullPtr: return eBasicTypeNullPtr;
3702 case clang::BuiltinType::ObjCId: return eBasicTypeObjCID;
3703 case clang::BuiltinType::ObjCClass: return eBasicTypeObjCClass;
3704 case clang::BuiltinType::ObjCSel: return eBasicTypeObjCSel;
3705 case clang::BuiltinType::Dependent:
3706 case clang::BuiltinType::Overload:
3707 case clang::BuiltinType::BoundMember:
3708 case clang::BuiltinType::PseudoObject:
3709 case clang::BuiltinType::UnknownAny:
3710 case clang::BuiltinType::BuiltinFn:
3711 case clang::BuiltinType::ARCUnbridgedCast:
3712 return eBasicTypeOther;
Greg Claytonc4c5e892012-10-15 21:16:43 +00003713 }
Greg Claytoneaafa732012-10-13 00:20:27 +00003714 }
3715 }
3716
3717 return eBasicTypeInvalid;
3718}
3719
3720
Greg Claytonbf2331c2011-09-09 23:04:00 +00003721
Greg Clayton54979cd2010-12-15 05:08:08 +00003722// If a pointer to a pointee type (the clang_type arg) says that it has no
3723// children, then we either need to trust it, or override it and return a
3724// different result. For example, an "int *" has one child that is an integer,
3725// but a function pointer doesn't have any children. Likewise if a Record type
3726// claims it has no children, then there really is nothing to show.
3727uint32_t
3728ClangASTContext::GetNumPointeeChildren (clang_type_t clang_type)
3729{
3730 if (clang_type == NULL)
3731 return 0;
3732
3733 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3734 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3735 switch (type_class)
3736 {
Greg Clayton97a43712011-01-08 22:26:47 +00003737 case clang::Type::Builtin:
3738 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3739 {
Greg Clayton7260f622011-04-18 08:33:37 +00003740 case clang::BuiltinType::UnknownAny:
Greg Clayton97a43712011-01-08 22:26:47 +00003741 case clang::BuiltinType::Void:
3742 case clang::BuiltinType::NullPtr:
3743 return 0;
3744 case clang::BuiltinType::Bool:
3745 case clang::BuiltinType::Char_U:
3746 case clang::BuiltinType::UChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003747 case clang::BuiltinType::WChar_U:
Greg Clayton97a43712011-01-08 22:26:47 +00003748 case clang::BuiltinType::Char16:
3749 case clang::BuiltinType::Char32:
3750 case clang::BuiltinType::UShort:
3751 case clang::BuiltinType::UInt:
3752 case clang::BuiltinType::ULong:
3753 case clang::BuiltinType::ULongLong:
3754 case clang::BuiltinType::UInt128:
3755 case clang::BuiltinType::Char_S:
3756 case clang::BuiltinType::SChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003757 case clang::BuiltinType::WChar_S:
Greg Clayton97a43712011-01-08 22:26:47 +00003758 case clang::BuiltinType::Short:
3759 case clang::BuiltinType::Int:
3760 case clang::BuiltinType::Long:
3761 case clang::BuiltinType::LongLong:
3762 case clang::BuiltinType::Int128:
3763 case clang::BuiltinType::Float:
3764 case clang::BuiltinType::Double:
3765 case clang::BuiltinType::LongDouble:
3766 case clang::BuiltinType::Dependent:
3767 case clang::BuiltinType::Overload:
Greg Clayton97a43712011-01-08 22:26:47 +00003768 case clang::BuiltinType::ObjCId:
3769 case clang::BuiltinType::ObjCClass:
3770 case clang::BuiltinType::ObjCSel:
Sean Callanand12cf8bb2011-05-15 22:34:38 +00003771 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00003772 case clang::BuiltinType::Half:
3773 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00003774 case clang::BuiltinType::PseudoObject:
Sean Callanan3d654b32012-09-24 22:25:51 +00003775 case clang::BuiltinType::BuiltinFn:
Greg Clayton97a43712011-01-08 22:26:47 +00003776 return 1;
3777 }
3778 break;
3779
Greg Clayton49462ea2011-01-15 02:52:14 +00003780 case clang::Type::Complex: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003781 case clang::Type::Pointer: return 1;
3782 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
3783 case clang::Type::LValueReference: return 1;
3784 case clang::Type::RValueReference: return 1;
3785 case clang::Type::MemberPointer: return 0;
3786 case clang::Type::ConstantArray: return 0;
3787 case clang::Type::IncompleteArray: return 0;
3788 case clang::Type::VariableArray: return 0;
3789 case clang::Type::DependentSizedArray: return 0;
3790 case clang::Type::DependentSizedExtVector: return 0;
3791 case clang::Type::Vector: return 0;
3792 case clang::Type::ExtVector: return 0;
3793 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
3794 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
3795 case clang::Type::UnresolvedUsing: return 0;
3796 case clang::Type::Paren: return 0;
3797 case clang::Type::Typedef: return ClangASTContext::GetNumPointeeChildren (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00003798 case clang::Type::Elaborated: return ClangASTContext::GetNumPointeeChildren (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003799 case clang::Type::TypeOfExpr: return 0;
3800 case clang::Type::TypeOf: return 0;
3801 case clang::Type::Decltype: return 0;
3802 case clang::Type::Record: return 0;
3803 case clang::Type::Enum: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003804 case clang::Type::TemplateTypeParm: return 1;
3805 case clang::Type::SubstTemplateTypeParm: return 1;
3806 case clang::Type::TemplateSpecialization: return 1;
3807 case clang::Type::InjectedClassName: return 0;
3808 case clang::Type::DependentName: return 1;
3809 case clang::Type::DependentTemplateSpecialization: return 1;
3810 case clang::Type::ObjCObject: return 0;
3811 case clang::Type::ObjCInterface: return 0;
3812 case clang::Type::ObjCObjectPointer: return 1;
3813 default:
3814 break;
3815 }
3816 return 0;
3817}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003818
Greg Clayton1be10fc2010-09-29 01:12:09 +00003819clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003820ClangASTContext::GetChildClangTypeAtIndex
3821(
Jim Inghamd555bac2011-06-24 22:03:24 +00003822 ExecutionContext *exe_ctx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003823 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003824 clang_type_t parent_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003825 uint32_t idx,
3826 bool transparent_pointers,
3827 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003828 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003829 std::string& child_name,
3830 uint32_t &child_byte_size,
3831 int32_t &child_byte_offset,
3832 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003833 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003834 bool &child_is_base_class,
3835 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003836)
3837{
3838 if (parent_clang_type)
3839
Jim Inghamd555bac2011-06-24 22:03:24 +00003840 return GetChildClangTypeAtIndex (exe_ctx,
3841 getASTContext(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003842 parent_name,
3843 parent_clang_type,
3844 idx,
3845 transparent_pointers,
3846 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003847 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003848 child_name,
3849 child_byte_size,
3850 child_byte_offset,
3851 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003852 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003853 child_is_base_class,
3854 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003855 return NULL;
3856}
3857
Greg Clayton1be10fc2010-09-29 01:12:09 +00003858clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003859ClangASTContext::GetChildClangTypeAtIndex
3860(
Jim Inghamd555bac2011-06-24 22:03:24 +00003861 ExecutionContext *exe_ctx,
Greg Clayton6beaaa62011-01-17 03:46:26 +00003862 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003863 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003864 clang_type_t parent_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003865 uint32_t idx,
3866 bool transparent_pointers,
3867 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003868 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003869 std::string& child_name,
3870 uint32_t &child_byte_size,
3871 int32_t &child_byte_offset,
3872 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003873 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003874 bool &child_is_base_class,
3875 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003876)
3877{
3878 if (parent_clang_type == NULL)
3879 return NULL;
3880
Greg Clayton4ef877f2012-12-06 02:33:54 +00003881 QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type));
3882 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
3883 child_bitfield_bit_size = 0;
3884 child_bitfield_bit_offset = 0;
3885 child_is_base_class = false;
3886
3887 const bool idx_is_valid = idx < ClangASTContext::GetNumChildren (ast, parent_clang_type, omit_empty_base_classes);
3888 uint32_t bit_offset;
3889 switch (parent_type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003890 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003891 case clang::Type::Builtin:
3892 if (idx_is_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003893 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003894 switch (cast<clang::BuiltinType>(parent_qual_type)->getKind())
3895 {
3896 case clang::BuiltinType::ObjCId:
3897 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00003898 child_name = "isa";
Greg Clayton6beaaa62011-01-17 03:46:26 +00003899 child_byte_size = ast->getTypeSize(ast->ObjCBuiltinClassTy) / CHAR_BIT;
3900 return ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003901
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003902 default:
3903 break;
3904 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00003905 }
3906 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003907
Greg Clayton4ef877f2012-12-06 02:33:54 +00003908 case clang::Type::Record:
3909 if (idx_is_valid && GetCompleteQualType (ast, parent_qual_type))
3910 {
3911 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr());
3912 const RecordDecl *record_decl = record_type->getDecl();
3913 assert(record_decl);
3914 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
3915 uint32_t child_idx = 0;
3916
3917 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3918 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003919 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003920 // We might have base classes to print out first
3921 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3922 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3923 base_class != base_class_end;
3924 ++base_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003925 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003926 const CXXRecordDecl *base_class_decl = NULL;
3927
3928 // Skip empty base classes
3929 if (omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003930 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003931 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3932 if (RecordHasFields(base_class_decl) == false)
3933 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003934 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00003935
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003936 if (idx == child_idx)
3937 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003938 if (base_class_decl == NULL)
3939 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003940
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003941
Greg Clayton4ef877f2012-12-06 02:33:54 +00003942 if (base_class->isVirtual())
3943 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
3944 else
3945 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003946
Greg Clayton4ef877f2012-12-06 02:33:54 +00003947 // Base classes should be a multiple of 8 bits in size
3948 child_byte_offset = bit_offset/8;
3949
3950 child_name = ClangASTType::GetTypeNameForQualType(ast, base_class->getType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003951
Greg Clayton4ef877f2012-12-06 02:33:54 +00003952 uint64_t clang_type_info_bit_size = ast->getTypeSize(base_class->getType());
3953
3954 // Base classes bit sizes should be a multiple of 8 bits in size
3955 assert (clang_type_info_bit_size % 8 == 0);
3956 child_byte_size = clang_type_info_bit_size / 8;
3957 child_is_base_class = true;
3958 return base_class->getType().getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003959 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00003960 // We don't increment the child index in the for loop since we might
3961 // be skipping empty base classes
3962 ++child_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003963 }
3964 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00003965 // Make sure index is in range...
3966 uint32_t field_idx = 0;
3967 RecordDecl::field_iterator field, field_end;
3968 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 +00003969 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003970 if (idx == child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00003971 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003972 // Print the member type if requested
3973 // Print the member name and equal sign
3974 child_name.assign(field->getNameAsString().c_str());
3975
3976 // Figure out the type byte size (field_type_info.first) and
3977 // alignment (field_type_info.second) from the AST context.
3978 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(field->getType());
3979 assert(field_idx < record_layout.getFieldCount());
3980
3981 child_byte_size = field_type_info.first / 8;
3982
3983 // Figure out the field offset within the current struct/union/class type
3984 bit_offset = record_layout.getFieldOffset (field_idx);
3985 child_byte_offset = bit_offset / 8;
3986 if (ClangASTContext::FieldIsBitfield (ast, *field, child_bitfield_bit_size))
3987 child_bitfield_bit_offset = bit_offset % 8;
3988
3989 return field->getType().getAsOpaquePtr();
3990 }
3991 }
3992 }
3993 break;
3994
3995 case clang::Type::ObjCObject:
3996 case clang::Type::ObjCInterface:
3997 if (idx_is_valid && GetCompleteQualType (ast, parent_qual_type))
3998 {
3999 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr());
4000 assert (objc_class_type);
4001 if (objc_class_type)
4002 {
4003 uint32_t child_idx = 0;
4004 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4005
4006 if (class_interface_decl)
4007 {
4008
4009 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
4010 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4011 if (superclass_interface_decl)
Greg Clayton9e409562010-07-28 02:04:09 +00004012 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004013 if (omit_empty_base_classes)
Greg Clayton9e409562010-07-28 02:04:09 +00004014 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004015 if (ClangASTContext::GetNumChildren(ast, ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0)
Greg Clayton9e409562010-07-28 02:04:09 +00004016 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004017 if (idx == 0)
Greg Clayton9e409562010-07-28 02:04:09 +00004018 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004019 QualType ivar_qual_type(ast->getObjCInterfaceType(superclass_interface_decl));
Greg Clayton9e409562010-07-28 02:04:09 +00004020
Greg Clayton9e409562010-07-28 02:04:09 +00004021
Greg Clayton4ef877f2012-12-06 02:33:54 +00004022 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
Greg Clayton9e409562010-07-28 02:04:09 +00004023
Greg Clayton6beaaa62011-01-17 03:46:26 +00004024 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004025
4026 child_byte_size = ivar_type_info.first / 8;
Greg Clayton4ef877f2012-12-06 02:33:54 +00004027 child_byte_offset = 0;
4028 child_is_base_class = true;
Greg Clayton9e409562010-07-28 02:04:09 +00004029
Greg Clayton9e409562010-07-28 02:04:09 +00004030 return ivar_qual_type.getAsOpaquePtr();
4031 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004032
Greg Clayton9e409562010-07-28 02:04:09 +00004033 ++child_idx;
4034 }
4035 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004036 else
4037 ++child_idx;
4038 }
4039
4040 const uint32_t superclass_idx = child_idx;
4041
4042 if (idx < (child_idx + class_interface_decl->ivar_size()))
4043 {
4044 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4045
4046 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
4047 {
4048 if (child_idx == idx)
4049 {
4050 ObjCIvarDecl* ivar_decl = *ivar_pos;
4051
4052 QualType ivar_qual_type(ivar_decl->getType());
4053
4054 child_name.assign(ivar_decl->getNameAsString().c_str());
4055
4056 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
4057
4058 child_byte_size = ivar_type_info.first / 8;
4059
4060 // Figure out the field offset within the current struct/union/class type
4061 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
4062 // that doesn't account for the space taken up by unbacked properties, or from
4063 // the changing size of base classes that are newer than this class.
4064 // So if we have a process around that we can ask about this object, do so.
4065 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
4066 Process *process = NULL;
4067 if (exe_ctx)
4068 process = exe_ctx->GetProcessPtr();
4069 if (process)
4070 {
4071 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4072 if (objc_runtime != NULL)
4073 {
4074 ClangASTType parent_ast_type (ast, parent_qual_type.getAsOpaquePtr());
4075 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
4076 }
4077 }
4078
4079 // Setting this to UINT32_MAX to make sure we don't compute it twice...
4080 bit_offset = UINT32_MAX;
4081
4082 if (child_byte_offset == LLDB_INVALID_IVAR_OFFSET)
4083 {
4084 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
4085 child_byte_offset = bit_offset / 8;
4086 }
4087
4088 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
4089 // of a bitfield within its containing object. So regardless of where we get the byte
4090 // offset from, we still need to get the bit offset for bitfields from the layout.
4091
4092 if (ClangASTContext::FieldIsBitfield (ast, ivar_decl, child_bitfield_bit_size))
4093 {
4094 if (bit_offset == UINT32_MAX)
4095 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
4096
4097 child_bitfield_bit_offset = bit_offset % 8;
4098 }
4099 return ivar_qual_type.getAsOpaquePtr();
4100 }
4101 ++child_idx;
4102 }
Greg Clayton9e409562010-07-28 02:04:09 +00004103 }
4104 }
4105 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004106 }
4107 break;
4108
4109 case clang::Type::ObjCObjectPointer:
4110 if (idx_is_valid)
4111 {
4112 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr());
4113 QualType pointee_type = pointer_type->getPointeeType();
4114
4115 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
Greg Clayton9e409562010-07-28 02:04:09 +00004116 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004117 child_is_deref_of_parent = false;
4118 bool tmp_child_is_deref_of_parent = false;
4119 return GetChildClangTypeAtIndex (exe_ctx,
4120 ast,
4121 parent_name,
4122 pointer_type->getPointeeType().getAsOpaquePtr(),
4123 idx,
4124 transparent_pointers,
4125 omit_empty_base_classes,
4126 ignore_array_bounds,
4127 child_name,
4128 child_byte_size,
4129 child_byte_offset,
4130 child_bitfield_bit_size,
4131 child_bitfield_bit_offset,
4132 child_is_base_class,
4133 tmp_child_is_deref_of_parent);
4134 }
4135 else
4136 {
4137 child_is_deref_of_parent = true;
4138 if (parent_name)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004139 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004140 child_name.assign(1, '*');
4141 child_name += parent_name;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004142 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004143
Greg Clayton4ef877f2012-12-06 02:33:54 +00004144 // We have a pointer to an simple type
4145 if (idx == 0 && GetCompleteQualType(ast, pointee_type))
4146 {
4147 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
4148 assert(clang_type_info.first % 8 == 0);
4149 child_byte_size = clang_type_info.first / 8;
4150 child_byte_offset = 0;
4151 return pointee_type.getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004152 }
Greg Clayton9e409562010-07-28 02:04:09 +00004153 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004154 }
4155 break;
Greg Clayton9e409562010-07-28 02:04:09 +00004156
Greg Claytone1a916a2010-07-21 22:12:05 +00004157 case clang::Type::ConstantArray:
Greg Clayton4ef877f2012-12-06 02:33:54 +00004158 case clang::Type::IncompleteArray:
4159 if (ignore_array_bounds || idx_is_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004160 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004161 const ArrayType *array = cast<ArrayType>(parent_qual_type.getTypePtr());
4162 if (array)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004163 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004164 if (GetCompleteQualType (ast, array->getElementType()))
4165 {
4166 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Greg Clayton4ef877f2012-12-06 02:33:54 +00004167
Greg Clayton6beaaa62011-01-17 03:46:26 +00004168 char element_name[64];
4169 ::snprintf (element_name, sizeof (element_name), "[%u]", idx);
Greg Clayton4ef877f2012-12-06 02:33:54 +00004170
Greg Clayton6beaaa62011-01-17 03:46:26 +00004171 child_name.assign(element_name);
4172 assert(field_type_info.first % 8 == 0);
4173 child_byte_size = field_type_info.first / 8;
Greg Claytondaf515f2011-07-09 20:12:33 +00004174 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
Greg Clayton6beaaa62011-01-17 03:46:26 +00004175 return array->getElementType().getAsOpaquePtr();
4176 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004177 }
4178 }
4179 break;
Greg Clayton4ef877f2012-12-06 02:33:54 +00004180
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004181
Greg Clayton4ef877f2012-12-06 02:33:54 +00004182 case clang::Type::Pointer:
4183 if (idx_is_valid)
4184 {
4185 const PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr());
4186 QualType pointee_type = pointer_type->getPointeeType();
4187
4188 // Don't dereference "void *" pointers
4189 if (pointee_type->isVoidType())
4190 return NULL;
4191
4192 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004193 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004194 child_is_deref_of_parent = false;
4195 bool tmp_child_is_deref_of_parent = false;
4196 return GetChildClangTypeAtIndex (exe_ctx,
4197 ast,
4198 parent_name,
4199 pointer_type->getPointeeType().getAsOpaquePtr(),
4200 idx,
4201 transparent_pointers,
4202 omit_empty_base_classes,
4203 ignore_array_bounds,
4204 child_name,
4205 child_byte_size,
4206 child_byte_offset,
4207 child_bitfield_bit_size,
4208 child_bitfield_bit_offset,
4209 child_is_base_class,
4210 tmp_child_is_deref_of_parent);
4211 }
4212 else
4213 {
4214 child_is_deref_of_parent = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004215
Greg Clayton4ef877f2012-12-06 02:33:54 +00004216 if (parent_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004217 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004218 child_name.assign(1, '*');
4219 child_name += parent_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004220 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004221
4222 // We have a pointer to an simple type
4223 if (idx == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004224 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004225 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
4226 assert(clang_type_info.first % 8 == 0);
4227 child_byte_size = clang_type_info.first / 8;
4228 child_byte_offset = 0;
4229 return pointee_type.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004230 }
4231 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004232 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004233 break;
4234
4235 case clang::Type::LValueReference:
4236 case clang::Type::RValueReference:
4237 if (idx_is_valid)
4238 {
4239 const ReferenceType *reference_type = cast<ReferenceType>(parent_qual_type.getTypePtr());
4240 QualType pointee_type(reference_type->getPointeeType());
4241 clang_type_t pointee_clang_type = pointee_type.getAsOpaquePtr();
4242 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_clang_type))
4243 {
4244 child_is_deref_of_parent = false;
4245 bool tmp_child_is_deref_of_parent = false;
4246 return GetChildClangTypeAtIndex (exe_ctx,
4247 ast,
4248 parent_name,
4249 pointee_clang_type,
4250 idx,
4251 transparent_pointers,
4252 omit_empty_base_classes,
4253 ignore_array_bounds,
4254 child_name,
4255 child_byte_size,
4256 child_byte_offset,
4257 child_bitfield_bit_size,
4258 child_bitfield_bit_offset,
4259 child_is_base_class,
4260 tmp_child_is_deref_of_parent);
4261 }
4262 else
4263 {
4264 if (parent_name)
4265 {
4266 child_name.assign(1, '&');
4267 child_name += parent_name;
4268 }
4269
4270 // We have a pointer to an simple type
4271 if (idx == 0)
4272 {
4273 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
4274 assert(clang_type_info.first % 8 == 0);
4275 child_byte_size = clang_type_info.first / 8;
4276 child_byte_offset = 0;
4277 return pointee_type.getAsOpaquePtr();
4278 }
4279 }
4280 }
4281 break;
4282
4283 case clang::Type::Typedef:
4284 return GetChildClangTypeAtIndex (exe_ctx,
4285 ast,
4286 parent_name,
4287 cast<TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
4288 idx,
4289 transparent_pointers,
4290 omit_empty_base_classes,
4291 ignore_array_bounds,
4292 child_name,
4293 child_byte_size,
4294 child_byte_offset,
4295 child_bitfield_bit_size,
4296 child_bitfield_bit_offset,
4297 child_is_base_class,
4298 child_is_deref_of_parent);
4299 break;
4300
4301 case clang::Type::Elaborated:
4302 return GetChildClangTypeAtIndex (exe_ctx,
4303 ast,
4304 parent_name,
4305 cast<ElaboratedType>(parent_qual_type)->getNamedType().getAsOpaquePtr(),
4306 idx,
4307 transparent_pointers,
4308 omit_empty_base_classes,
4309 ignore_array_bounds,
4310 child_name,
4311 child_byte_size,
4312 child_byte_offset,
4313 child_bitfield_bit_size,
4314 child_bitfield_bit_offset,
4315 child_is_base_class,
4316 child_is_deref_of_parent);
4317
4318 default:
4319 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004320 }
Greg Clayton19503a22010-07-23 15:37:46 +00004321 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004322}
4323
4324static inline bool
4325BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
4326{
Greg Clayton6beaaa62011-01-17 03:46:26 +00004327 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004328}
4329
4330static uint32_t
4331GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
4332{
4333 uint32_t num_bases = 0;
4334 if (cxx_record_decl)
4335 {
4336 if (omit_empty_base_classes)
4337 {
4338 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4339 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4340 base_class != base_class_end;
4341 ++base_class)
4342 {
4343 // Skip empty base classes
4344 if (omit_empty_base_classes)
4345 {
4346 if (BaseSpecifierIsEmpty (base_class))
4347 continue;
4348 }
4349 ++num_bases;
4350 }
4351 }
4352 else
4353 num_bases = cxx_record_decl->getNumBases();
4354 }
4355 return num_bases;
4356}
4357
4358
4359static uint32_t
4360GetIndexForRecordBase
4361(
4362 const RecordDecl *record_decl,
4363 const CXXBaseSpecifier *base_spec,
4364 bool omit_empty_base_classes
4365)
4366{
4367 uint32_t child_idx = 0;
4368
4369 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4370
4371// const char *super_name = record_decl->getNameAsCString();
4372// const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString();
4373// printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
4374//
4375 if (cxx_record_decl)
4376 {
4377 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4378 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4379 base_class != base_class_end;
4380 ++base_class)
4381 {
4382 if (omit_empty_base_classes)
4383 {
4384 if (BaseSpecifierIsEmpty (base_class))
4385 continue;
4386 }
4387
4388// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
4389// child_idx,
4390// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4391//
4392//
4393 if (base_class == base_spec)
4394 return child_idx;
4395 ++child_idx;
4396 }
4397 }
4398
4399 return UINT32_MAX;
4400}
4401
4402
4403static uint32_t
4404GetIndexForRecordChild
4405(
4406 const RecordDecl *record_decl,
4407 NamedDecl *canonical_decl,
4408 bool omit_empty_base_classes
4409)
4410{
4411 uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes);
4412
4413// const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4414//
4415//// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString());
4416// if (cxx_record_decl)
4417// {
4418// CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4419// for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4420// base_class != base_class_end;
4421// ++base_class)
4422// {
4423// if (omit_empty_base_classes)
4424// {
4425// if (BaseSpecifierIsEmpty (base_class))
4426// continue;
4427// }
4428//
4429//// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
4430//// record_decl->getNameAsCString(),
4431//// canonical_decl->getNameAsCString(),
4432//// child_idx,
4433//// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4434//
4435//
4436// CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4437// if (curr_base_class_decl == canonical_decl)
4438// {
4439// return child_idx;
4440// }
4441// ++child_idx;
4442// }
4443// }
4444//
4445// const uint32_t num_bases = child_idx;
4446 RecordDecl::field_iterator field, field_end;
4447 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4448 field != field_end;
4449 ++field, ++child_idx)
4450 {
4451// printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n",
4452// record_decl->getNameAsCString(),
4453// canonical_decl->getNameAsCString(),
4454// child_idx - num_bases,
4455// field->getNameAsCString());
4456
4457 if (field->getCanonicalDecl() == canonical_decl)
4458 return child_idx;
4459 }
4460
4461 return UINT32_MAX;
4462}
4463
4464// Look for a child member (doesn't include base classes, but it does include
4465// their members) in the type hierarchy. Returns an index path into "clang_type"
4466// on how to reach the appropriate member.
4467//
4468// class A
4469// {
4470// public:
4471// int m_a;
4472// int m_b;
4473// };
4474//
4475// class B
4476// {
4477// };
4478//
4479// class C :
4480// public B,
4481// public A
4482// {
4483// };
4484//
4485// If we have a clang type that describes "class C", and we wanted to looked
4486// "m_b" in it:
4487//
4488// With omit_empty_base_classes == false we would get an integer array back with:
4489// { 1, 1 }
4490// The first index 1 is the child index for "class A" within class C
4491// The second index 1 is the child index for "m_b" within class A
4492//
4493// With omit_empty_base_classes == true we would get an integer array back with:
4494// { 0, 1 }
4495// 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)
4496// The second index 1 is the child index for "m_b" within class A
4497
4498size_t
4499ClangASTContext::GetIndexOfChildMemberWithName
4500(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004501 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004502 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004503 const char *name,
4504 bool omit_empty_base_classes,
4505 std::vector<uint32_t>& child_indexes
4506)
4507{
4508 if (clang_type && name && name[0])
4509 {
4510 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00004511 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4512 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004513 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004514 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004515 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004516 {
4517 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4518 const RecordDecl *record_decl = record_type->getDecl();
4519
4520 assert(record_decl);
4521 uint32_t child_idx = 0;
4522
4523 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4524
4525 // Try and find a field that matches NAME
4526 RecordDecl::field_iterator field, field_end;
4527 StringRef name_sref(name);
4528 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4529 field != field_end;
4530 ++field, ++child_idx)
4531 {
4532 if (field->getName().equals (name_sref))
4533 {
4534 // We have to add on the number of base classes to this index!
4535 child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
4536 return child_indexes.size();
4537 }
4538 }
4539
4540 if (cxx_record_decl)
4541 {
4542 const RecordDecl *parent_record_decl = cxx_record_decl;
4543
4544 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
4545
4546 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
4547 // Didn't find things easily, lets let clang do its thang...
Sean Callanancc427fa2011-07-30 02:42:06 +00004548 IdentifierInfo & ident_ref = ast->Idents.get(name_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004549 DeclarationName decl_name(&ident_ref);
4550
4551 CXXBasePaths paths;
4552 if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember,
4553 decl_name.getAsOpaquePtr(),
4554 paths))
4555 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004556 CXXBasePaths::const_paths_iterator path, path_end = paths.end();
4557 for (path = paths.begin(); path != path_end; ++path)
4558 {
4559 const size_t num_path_elements = path->size();
4560 for (size_t e=0; e<num_path_elements; ++e)
4561 {
4562 CXXBasePathElement elem = (*path)[e];
4563
4564 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
4565 if (child_idx == UINT32_MAX)
4566 {
4567 child_indexes.clear();
4568 return 0;
4569 }
4570 else
4571 {
4572 child_indexes.push_back (child_idx);
4573 parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl());
4574 }
4575 }
Sean Callanan5deaa4c2012-12-21 21:34:42 +00004576 for (NamedDecl *path_decl : path->Decls)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004577 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00004578 child_idx = GetIndexForRecordChild (parent_record_decl, path_decl, omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004579 if (child_idx == UINT32_MAX)
4580 {
4581 child_indexes.clear();
4582 return 0;
4583 }
4584 else
4585 {
4586 child_indexes.push_back (child_idx);
4587 }
4588 }
4589 }
4590 return child_indexes.size();
4591 }
4592 }
4593
4594 }
4595 break;
4596
Greg Clayton9e409562010-07-28 02:04:09 +00004597 case clang::Type::ObjCObject:
4598 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004599 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004600 {
4601 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004602 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004603 assert (objc_class_type);
4604 if (objc_class_type)
4605 {
4606 uint32_t child_idx = 0;
4607 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4608
4609 if (class_interface_decl)
4610 {
4611 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4612 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4613
Greg Clayton6ba78152010-09-18 02:11:07 +00004614 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00004615 {
4616 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4617
4618 if (ivar_decl->getName().equals (name_sref))
4619 {
4620 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4621 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4622 ++child_idx;
4623
4624 child_indexes.push_back (child_idx);
4625 return child_indexes.size();
4626 }
4627 }
4628
4629 if (superclass_interface_decl)
4630 {
4631 // The super class index is always zero for ObjC classes,
4632 // so we push it onto the child indexes in case we find
4633 // an ivar in our superclass...
4634 child_indexes.push_back (0);
4635
Greg Clayton6beaaa62011-01-17 03:46:26 +00004636 if (GetIndexOfChildMemberWithName (ast,
4637 ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00004638 name,
4639 omit_empty_base_classes,
4640 child_indexes))
4641 {
4642 // We did find an ivar in a superclass so just
4643 // return the results!
4644 return child_indexes.size();
4645 }
4646
4647 // We didn't find an ivar matching "name" in our
4648 // superclass, pop the superclass zero index that
4649 // we pushed on above.
4650 child_indexes.pop_back();
4651 }
4652 }
4653 }
4654 }
4655 break;
4656
4657 case clang::Type::ObjCObjectPointer:
4658 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004659 return GetIndexOfChildMemberWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004660 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4661 name,
4662 omit_empty_base_classes,
4663 child_indexes);
4664 }
4665 break;
4666
4667
Greg Claytone1a916a2010-07-21 22:12:05 +00004668 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004669 {
4670// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4671// const uint64_t element_count = array->getSize().getLimitedValue();
4672//
4673// if (idx < element_count)
4674// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004675// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004676//
4677// char element_name[32];
4678// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4679//
4680// child_name.assign(element_name);
4681// assert(field_type_info.first % 8 == 0);
4682// child_byte_size = field_type_info.first / 8;
4683// child_byte_offset = idx * child_byte_size;
4684// return array->getElementType().getAsOpaquePtr();
4685// }
4686 }
4687 break;
4688
Greg Claytone1a916a2010-07-21 22:12:05 +00004689// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004690// {
4691// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4692// QualType pointee_type = mem_ptr_type->getPointeeType();
4693//
4694// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4695// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004696// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004697// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4698// name);
4699// }
4700// }
4701// break;
4702//
Greg Claytone1a916a2010-07-21 22:12:05 +00004703 case clang::Type::LValueReference:
4704 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004705 {
Sean Callanan78e37602011-01-27 04:42:51 +00004706 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004707 QualType pointee_type = reference_type->getPointeeType();
4708
4709 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4710 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004711 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004712 reference_type->getPointeeType().getAsOpaquePtr(),
4713 name,
4714 omit_empty_base_classes,
4715 child_indexes);
4716 }
4717 }
4718 break;
4719
Greg Claytone1a916a2010-07-21 22:12:05 +00004720 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004721 {
Sean Callanan78e37602011-01-27 04:42:51 +00004722 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004723 QualType pointee_type = pointer_type->getPointeeType();
4724
4725 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4726 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004727 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004728 pointer_type->getPointeeType().getAsOpaquePtr(),
4729 name,
4730 omit_empty_base_classes,
4731 child_indexes);
4732 }
4733 else
4734 {
4735// if (parent_name)
4736// {
4737// child_name.assign(1, '*');
4738// child_name += parent_name;
4739// }
4740//
4741// // We have a pointer to an simple type
4742// if (idx == 0)
4743// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004744// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004745// assert(clang_type_info.first % 8 == 0);
4746// child_byte_size = clang_type_info.first / 8;
4747// child_byte_offset = 0;
4748// return pointee_type.getAsOpaquePtr();
4749// }
4750 }
4751 }
4752 break;
4753
Greg Claytone1a916a2010-07-21 22:12:05 +00004754 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00004755 return GetIndexOfChildMemberWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00004756 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004757 name,
4758 omit_empty_base_classes,
4759 child_indexes);
4760
Greg Clayton4b63a5c2013-01-04 18:10:18 +00004761 case clang::Type::Elaborated:
4762 return GetIndexOfChildMemberWithName (ast,
4763 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
4764 name,
4765 omit_empty_base_classes,
4766 child_indexes);
4767
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004768 default:
4769 break;
4770 }
4771 }
4772 return 0;
4773}
4774
4775
4776// Get the index of the child of "clang_type" whose name matches. This function
4777// doesn't descend into the children, but only looks one level deep and name
4778// matches can include base class names.
4779
4780uint32_t
4781ClangASTContext::GetIndexOfChildWithName
4782(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004783 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004784 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004785 const char *name,
4786 bool omit_empty_base_classes
4787)
4788{
4789 if (clang_type && name && name[0])
4790 {
4791 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00004792
Greg Clayton737b9322010-09-13 03:32:57 +00004793 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Clayton9e409562010-07-28 02:04:09 +00004794
Greg Clayton737b9322010-09-13 03:32:57 +00004795 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004796 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004797 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004798 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004799 {
4800 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4801 const RecordDecl *record_decl = record_type->getDecl();
4802
4803 assert(record_decl);
4804 uint32_t child_idx = 0;
4805
4806 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4807
4808 if (cxx_record_decl)
4809 {
4810 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4811 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4812 base_class != base_class_end;
4813 ++base_class)
4814 {
4815 // Skip empty base classes
4816 CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4817 if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false)
4818 continue;
4819
Greg Clayton84db9102012-03-26 23:03:23 +00004820 std::string base_class_type_name (ClangASTType::GetTypeNameForQualType(ast, base_class->getType()));
Greg Claytone3055942011-06-30 02:28:26 +00004821 if (base_class_type_name.compare (name) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004822 return child_idx;
4823 ++child_idx;
4824 }
4825 }
4826
4827 // Try and find a field that matches NAME
4828 RecordDecl::field_iterator field, field_end;
4829 StringRef name_sref(name);
4830 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4831 field != field_end;
4832 ++field, ++child_idx)
4833 {
4834 if (field->getName().equals (name_sref))
4835 return child_idx;
4836 }
4837
4838 }
4839 break;
4840
Greg Clayton9e409562010-07-28 02:04:09 +00004841 case clang::Type::ObjCObject:
4842 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004843 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004844 {
4845 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004846 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004847 assert (objc_class_type);
4848 if (objc_class_type)
4849 {
4850 uint32_t child_idx = 0;
4851 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4852
4853 if (class_interface_decl)
4854 {
4855 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4856 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4857
Jim Ingham2f355a72012-10-04 22:22:16 +00004858 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00004859 {
4860 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4861
4862 if (ivar_decl->getName().equals (name_sref))
4863 {
4864 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4865 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4866 ++child_idx;
4867
4868 return child_idx;
4869 }
4870 }
4871
4872 if (superclass_interface_decl)
4873 {
4874 if (superclass_interface_decl->getName().equals (name_sref))
4875 return 0;
4876 }
4877 }
4878 }
4879 }
4880 break;
4881
4882 case clang::Type::ObjCObjectPointer:
4883 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004884 return GetIndexOfChildWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004885 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4886 name,
4887 omit_empty_base_classes);
4888 }
4889 break;
4890
Greg Claytone1a916a2010-07-21 22:12:05 +00004891 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004892 {
4893// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4894// const uint64_t element_count = array->getSize().getLimitedValue();
4895//
4896// if (idx < element_count)
4897// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004898// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004899//
4900// char element_name[32];
4901// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4902//
4903// child_name.assign(element_name);
4904// assert(field_type_info.first % 8 == 0);
4905// child_byte_size = field_type_info.first / 8;
4906// child_byte_offset = idx * child_byte_size;
4907// return array->getElementType().getAsOpaquePtr();
4908// }
4909 }
4910 break;
4911
Greg Claytone1a916a2010-07-21 22:12:05 +00004912// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004913// {
4914// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4915// QualType pointee_type = mem_ptr_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// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4921// name);
4922// }
4923// }
4924// break;
4925//
Greg Claytone1a916a2010-07-21 22:12:05 +00004926 case clang::Type::LValueReference:
4927 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004928 {
Sean Callanan78e37602011-01-27 04:42:51 +00004929 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004930 QualType pointee_type = reference_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 reference_type->getPointeeType().getAsOpaquePtr(),
4936 name,
4937 omit_empty_base_classes);
4938 }
4939 }
4940 break;
4941
Greg Claytone1a916a2010-07-21 22:12:05 +00004942 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004943 {
Sean Callanan78e37602011-01-27 04:42:51 +00004944 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004945 QualType pointee_type = pointer_type->getPointeeType();
4946
4947 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4948 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004949 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004950 pointer_type->getPointeeType().getAsOpaquePtr(),
4951 name,
4952 omit_empty_base_classes);
4953 }
4954 else
4955 {
4956// if (parent_name)
4957// {
4958// child_name.assign(1, '*');
4959// child_name += parent_name;
4960// }
4961//
4962// // We have a pointer to an simple type
4963// if (idx == 0)
4964// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004965// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004966// assert(clang_type_info.first % 8 == 0);
4967// child_byte_size = clang_type_info.first / 8;
4968// child_byte_offset = 0;
4969// return pointee_type.getAsOpaquePtr();
4970// }
4971 }
4972 }
4973 break;
4974
Greg Clayton4b63a5c2013-01-04 18:10:18 +00004975 case clang::Type::Elaborated:
4976 return GetIndexOfChildWithName (ast,
4977 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
4978 name,
4979 omit_empty_base_classes);
4980
Greg Claytone1a916a2010-07-21 22:12:05 +00004981 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00004982 return GetIndexOfChildWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00004983 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004984 name,
4985 omit_empty_base_classes);
4986
4987 default:
4988 break;
4989 }
4990 }
4991 return UINT32_MAX;
4992}
4993
4994#pragma mark TagType
4995
4996bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00004997ClangASTContext::SetTagTypeKind (clang_type_t tag_clang_type, int kind)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004998{
4999 if (tag_clang_type)
5000 {
5001 QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00005002 const clang::Type *clang_type = tag_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005003 if (clang_type)
5004 {
Sean Callanan78e37602011-01-27 04:42:51 +00005005 const TagType *tag_type = dyn_cast<TagType>(clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005006 if (tag_type)
5007 {
5008 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl());
5009 if (tag_decl)
5010 {
5011 tag_decl->setTagKind ((TagDecl::TagKind)kind);
5012 return true;
5013 }
5014 }
5015 }
5016 }
5017 return false;
5018}
5019
5020
5021#pragma mark DeclContext Functions
5022
5023DeclContext *
Greg Clayton1be10fc2010-09-29 01:12:09 +00005024ClangASTContext::GetDeclContextForType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005025{
5026 if (clang_type == NULL)
5027 return NULL;
5028
5029 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005030 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5031 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005032 {
Sean Callanancc427fa2011-07-30 02:42:06 +00005033 case clang::Type::UnaryTransform: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005034 case clang::Type::FunctionNoProto: break;
5035 case clang::Type::FunctionProto: break;
5036 case clang::Type::IncompleteArray: break;
5037 case clang::Type::VariableArray: break;
5038 case clang::Type::ConstantArray: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005039 case clang::Type::DependentSizedArray: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005040 case clang::Type::ExtVector: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005041 case clang::Type::DependentSizedExtVector: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005042 case clang::Type::Vector: break;
5043 case clang::Type::Builtin: break;
5044 case clang::Type::BlockPointer: break;
5045 case clang::Type::Pointer: break;
5046 case clang::Type::LValueReference: break;
5047 case clang::Type::RValueReference: break;
5048 case clang::Type::MemberPointer: break;
5049 case clang::Type::Complex: break;
5050 case clang::Type::ObjCObject: break;
5051 case clang::Type::ObjCInterface: return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface();
5052 case clang::Type::ObjCObjectPointer: return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr());
5053 case clang::Type::Record: return cast<RecordType>(qual_type)->getDecl();
5054 case clang::Type::Enum: return cast<EnumType>(qual_type)->getDecl();
Sean Callanan48114472010-12-13 01:26:27 +00005055 case clang::Type::Typedef: return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00005056 case clang::Type::Elaborated: return ClangASTContext::GetDeclContextForType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00005057 case clang::Type::TypeOfExpr: break;
5058 case clang::Type::TypeOf: break;
5059 case clang::Type::Decltype: break;
5060 //case clang::Type::QualifiedName: break;
5061 case clang::Type::TemplateSpecialization: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005062 case clang::Type::DependentTemplateSpecialization: break;
5063 case clang::Type::TemplateTypeParm: break;
5064 case clang::Type::SubstTemplateTypeParm: break;
5065 case clang::Type::SubstTemplateTypeParmPack:break;
5066 case clang::Type::PackExpansion: break;
5067 case clang::Type::UnresolvedUsing: break;
5068 case clang::Type::Paren: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005069 case clang::Type::Attributed: break;
5070 case clang::Type::Auto: break;
5071 case clang::Type::InjectedClassName: break;
5072 case clang::Type::DependentName: break;
Greg Claytonea3e7d52011-10-08 00:49:15 +00005073 case clang::Type::Atomic: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005074 }
5075 // No DeclContext in this type...
5076 return NULL;
5077}
5078
5079#pragma mark Namespace Declarations
5080
5081NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00005082ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005083{
Greg Clayton030a2042011-10-14 21:34:45 +00005084 NamespaceDecl *namespace_decl = NULL;
Greg Clayton9d3d6882011-10-31 23:51:19 +00005085 ASTContext *ast = getASTContext();
5086 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
5087 if (decl_ctx == NULL)
5088 decl_ctx = translation_unit_decl;
5089
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005090 if (name)
5091 {
Greg Clayton030a2042011-10-14 21:34:45 +00005092 IdentifierInfo &identifier_info = ast->Idents.get(name);
5093 DeclarationName decl_name (&identifier_info);
5094 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00005095 for (NamedDecl *decl : result)
Greg Clayton030a2042011-10-14 21:34:45 +00005096 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00005097 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
Greg Clayton030a2042011-10-14 21:34:45 +00005098 if (namespace_decl)
5099 return namespace_decl;
5100 }
5101
Sean Callanan5b26f272012-02-04 08:49:35 +00005102 namespace_decl = NamespaceDecl::Create(*ast,
5103 decl_ctx,
5104 false,
5105 SourceLocation(),
5106 SourceLocation(),
5107 &identifier_info,
5108 NULL);
Greg Clayton030a2042011-10-14 21:34:45 +00005109
Greg Clayton9d3d6882011-10-31 23:51:19 +00005110 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005111 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00005112 else
5113 {
5114 if (decl_ctx == translation_unit_decl)
5115 {
5116 namespace_decl = translation_unit_decl->getAnonymousNamespace();
5117 if (namespace_decl)
5118 return namespace_decl;
5119
Sean Callanan5b26f272012-02-04 08:49:35 +00005120 namespace_decl = NamespaceDecl::Create(*ast,
5121 decl_ctx,
5122 false,
5123 SourceLocation(),
5124 SourceLocation(),
5125 NULL,
5126 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00005127 translation_unit_decl->setAnonymousNamespace (namespace_decl);
5128 translation_unit_decl->addDecl (namespace_decl);
5129 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
5130 }
5131 else
5132 {
5133 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
5134 if (parent_namespace_decl)
5135 {
5136 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
5137 if (namespace_decl)
5138 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00005139 namespace_decl = NamespaceDecl::Create(*ast,
5140 decl_ctx,
5141 false,
5142 SourceLocation(),
5143 SourceLocation(),
5144 NULL,
5145 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00005146 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
5147 parent_namespace_decl->addDecl (namespace_decl);
5148 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
5149 }
5150 else
5151 {
5152 // BAD!!!
5153 }
5154 }
5155
5156
5157 if (namespace_decl)
5158 {
5159 // If we make it here, we are creating the anonymous namespace decl
5160 // for the first time, so we need to do the using directive magic
5161 // like SEMA does
5162 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast,
5163 decl_ctx,
5164 SourceLocation(),
5165 SourceLocation(),
5166 NestedNameSpecifierLoc(),
5167 SourceLocation(),
5168 namespace_decl,
5169 decl_ctx);
5170 using_directive_decl->setImplicit();
5171 decl_ctx->addDecl(using_directive_decl);
5172 }
5173 }
5174#ifdef LLDB_CONFIGURATION_DEBUG
5175 VerifyDecl(namespace_decl);
5176#endif
Greg Clayton030a2042011-10-14 21:34:45 +00005177 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005178}
5179
5180
5181#pragma mark Function Types
5182
5183FunctionDecl *
Greg Clayton147e1fa2011-10-14 22:47:18 +00005184ClangASTContext::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 +00005185{
Greg Clayton147e1fa2011-10-14 22:47:18 +00005186 FunctionDecl *func_decl = NULL;
5187 ASTContext *ast = getASTContext();
5188 if (decl_ctx == NULL)
5189 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005190
Greg Clayton147e1fa2011-10-14 22:47:18 +00005191 if (name && name[0])
5192 {
5193 func_decl = FunctionDecl::Create (*ast,
5194 decl_ctx,
5195 SourceLocation(),
5196 SourceLocation(),
5197 DeclarationName (&ast->Idents.get(name)),
5198 QualType::getFromOpaquePtr(function_clang_type),
5199 NULL,
5200 (FunctionDecl::StorageClass)storage,
5201 (FunctionDecl::StorageClass)storage,
5202 is_inline);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005203 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00005204 else
5205 {
5206 func_decl = FunctionDecl::Create (*ast,
5207 decl_ctx,
5208 SourceLocation(),
5209 SourceLocation(),
5210 DeclarationName (),
5211 QualType::getFromOpaquePtr(function_clang_type),
5212 NULL,
5213 (FunctionDecl::StorageClass)storage,
5214 (FunctionDecl::StorageClass)storage,
5215 is_inline);
5216 }
5217 if (func_decl)
5218 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005219
5220#ifdef LLDB_CONFIGURATION_DEBUG
5221 VerifyDecl(func_decl);
5222#endif
5223
Greg Clayton147e1fa2011-10-14 22:47:18 +00005224 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005225}
5226
Greg Clayton1be10fc2010-09-29 01:12:09 +00005227clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00005228ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00005229 clang_type_t result_type,
5230 clang_type_t *args,
Sean Callananc81256a2010-09-16 20:40:25 +00005231 unsigned num_args,
5232 bool is_variadic,
5233 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005234{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005235 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005236 std::vector<QualType> qual_type_args;
5237 for (unsigned i=0; i<num_args; ++i)
5238 qual_type_args.push_back (QualType::getFromOpaquePtr(args[i]));
5239
5240 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00005241 FunctionProtoType::ExtProtoInfo proto_info;
5242 proto_info.Variadic = is_variadic;
Sean Callananfb0b7582011-03-15 00:17:19 +00005243 proto_info.ExceptionSpecType = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005244 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00005245 proto_info.RefQualifier = RQ_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005246 proto_info.NumExceptions = 0;
5247 proto_info.Exceptions = NULL;
5248
Greg Clayton147e1fa2011-10-14 22:47:18 +00005249 return ast->getFunctionType (QualType::getFromOpaquePtr(result_type),
5250 qual_type_args.empty() ? NULL : &qual_type_args.front(),
5251 qual_type_args.size(),
5252 proto_info).getAsOpaquePtr(); // NoReturn);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005253}
5254
5255ParmVarDecl *
Greg Clayton1be10fc2010-09-29 01:12:09 +00005256ClangASTContext::CreateParameterDeclaration (const char *name, clang_type_t param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005257{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005258 ASTContext *ast = getASTContext();
5259 assert (ast != NULL);
5260 return ParmVarDecl::Create(*ast,
5261 ast->getTranslationUnitDecl(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005262 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005263 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00005264 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callananc81256a2010-09-16 20:40:25 +00005265 QualType::getFromOpaquePtr(param_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005266 NULL,
5267 (VarDecl::StorageClass)storage,
5268 (VarDecl::StorageClass)storage,
5269 0);
5270}
5271
5272void
5273ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
5274{
5275 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00005276 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005277}
5278
5279
5280#pragma mark Array Types
5281
Greg Clayton1be10fc2010-09-29 01:12:09 +00005282clang_type_t
Greg Clayton4ef877f2012-12-06 02:33:54 +00005283ClangASTContext::CreateArrayType (clang_type_t element_type, size_t element_count)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005284{
5285 if (element_type)
5286 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00005287 ASTContext *ast = getASTContext();
5288 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005289 llvm::APInt ap_element_count (64, element_count);
Greg Clayton4ef877f2012-12-06 02:33:54 +00005290 if (element_count == 0)
5291 {
5292 return ast->getIncompleteArrayType(QualType::getFromOpaquePtr(element_type),
5293 ArrayType::Normal,
5294 0).getAsOpaquePtr();
5295
5296 }
5297 else
5298 {
5299 return ast->getConstantArrayType(QualType::getFromOpaquePtr(element_type),
5300 ap_element_count,
5301 ArrayType::Normal,
5302 0).getAsOpaquePtr(); // ElemQuals
5303 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005304 }
5305 return NULL;
5306}
5307
5308
5309#pragma mark TagDecl
5310
5311bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005312ClangASTContext::StartTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005313{
5314 if (clang_type)
5315 {
5316 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00005317 const clang::Type *t = qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005318 if (t)
5319 {
Sean Callanan78e37602011-01-27 04:42:51 +00005320 const TagType *tag_type = dyn_cast<TagType>(t);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005321 if (tag_type)
5322 {
5323 TagDecl *tag_decl = tag_type->getDecl();
5324 if (tag_decl)
5325 {
5326 tag_decl->startDefinition();
5327 return true;
5328 }
5329 }
Sean Callanan5b26f272012-02-04 08:49:35 +00005330
5331 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(t);
5332 if (object_type)
5333 {
5334 ObjCInterfaceDecl *interface_decl = object_type->getInterface();
5335 if (interface_decl)
5336 {
5337 interface_decl->startDefinition();
5338 return true;
5339 }
5340 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005341 }
5342 }
5343 return false;
5344}
5345
5346bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005347ClangASTContext::CompleteTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005348{
5349 if (clang_type)
5350 {
5351 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton14372242010-09-29 03:44:17 +00005352
5353 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5354
5355 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005356 {
Greg Clayton14372242010-09-29 03:44:17 +00005357 cxx_record_decl->completeDefinition();
5358
5359 return true;
5360 }
5361
5362 const EnumType *enum_type = dyn_cast<EnumType>(qual_type.getTypePtr());
5363
5364 if (enum_type)
5365 {
5366 EnumDecl *enum_decl = enum_type->getDecl();
5367
5368 if (enum_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005369 {
Greg Clayton14372242010-09-29 03:44:17 +00005370 /// TODO This really needs to be fixed.
5371
5372 unsigned NumPositiveBits = 1;
5373 unsigned NumNegativeBits = 0;
5374
Greg Clayton6beaaa62011-01-17 03:46:26 +00005375 ASTContext *ast = getASTContext();
Greg Claytone02b8502010-10-12 04:29:14 +00005376
5377 QualType promotion_qual_type;
5378 // If the enum integer type is less than an integer in bit width,
5379 // then we must promote it to an integer size.
Greg Clayton6beaaa62011-01-17 03:46:26 +00005380 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
Greg Claytone02b8502010-10-12 04:29:14 +00005381 {
5382 if (enum_decl->getIntegerType()->isSignedIntegerType())
Greg Clayton6beaaa62011-01-17 03:46:26 +00005383 promotion_qual_type = ast->IntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005384 else
Greg Clayton6beaaa62011-01-17 03:46:26 +00005385 promotion_qual_type = ast->UnsignedIntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005386 }
5387 else
5388 promotion_qual_type = enum_decl->getIntegerType();
5389
5390 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
Greg Clayton14372242010-09-29 03:44:17 +00005391 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005392 }
5393 }
5394 }
5395 return false;
5396}
5397
5398
5399#pragma mark Enumeration Types
5400
Greg Clayton1be10fc2010-09-29 01:12:09 +00005401clang_type_t
Greg Claytonca512b32011-01-14 04:54:56 +00005402ClangASTContext::CreateEnumerationType
5403(
5404 const char *name,
5405 DeclContext *decl_ctx,
5406 const Declaration &decl,
5407 clang_type_t integer_qual_type
5408)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005409{
5410 // TODO: Do something intelligent with the Declaration object passed in
5411 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005412 ASTContext *ast = getASTContext();
5413 assert (ast != NULL);
Greg Claytone02b8502010-10-12 04:29:14 +00005414
5415 // TODO: ask about these...
5416// const bool IsScoped = false;
5417// const bool IsFixed = false;
5418
Greg Clayton6beaaa62011-01-17 03:46:26 +00005419 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00005420 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00005421 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00005422 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005423 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callanan48114472010-12-13 01:26:27 +00005424 NULL,
5425 false, // IsScoped
5426 false, // IsScopedUsingClassTag
5427 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00005428
5429
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005430 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00005431 {
5432 // TODO: check if we should be setting the promotion type too?
5433 enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00005434
5435 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
5436
Greg Clayton6beaaa62011-01-17 03:46:26 +00005437 return ast->getTagDeclType(enum_decl).getAsOpaquePtr();
Greg Clayton83ff3892010-09-12 23:17:56 +00005438 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005439 return NULL;
5440}
5441
Greg Clayton1be10fc2010-09-29 01:12:09 +00005442clang_type_t
5443ClangASTContext::GetEnumerationIntegerType (clang_type_t enum_clang_type)
5444{
5445 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5446
Sean Callanan78e37602011-01-27 04:42:51 +00005447 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Greg Clayton1be10fc2010-09-29 01:12:09 +00005448 if (clang_type)
5449 {
5450 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5451 if (enum_type)
5452 {
5453 EnumDecl *enum_decl = enum_type->getDecl();
5454 if (enum_decl)
5455 return enum_decl->getIntegerType().getAsOpaquePtr();
5456 }
5457 }
5458 return NULL;
5459}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005460bool
5461ClangASTContext::AddEnumerationValueToEnumerationType
5462(
Greg Clayton1be10fc2010-09-29 01:12:09 +00005463 clang_type_t enum_clang_type,
5464 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005465 const Declaration &decl,
5466 const char *name,
5467 int64_t enum_value,
5468 uint32_t enum_value_bit_size
5469)
5470{
5471 if (enum_clang_type && enumerator_clang_type && name)
5472 {
5473 // TODO: Do something intelligent with the Declaration object passed in
5474 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005475 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005476 IdentifierTable *identifier_table = getIdentifierTable();
5477
Greg Clayton6beaaa62011-01-17 03:46:26 +00005478 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005479 assert (identifier_table != NULL);
5480 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5481
Sean Callanan78e37602011-01-27 04:42:51 +00005482 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005483 if (clang_type)
5484 {
5485 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5486
5487 if (enum_type)
5488 {
5489 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, false);
5490 enum_llvm_apsint = enum_value;
5491 EnumConstantDecl *enumerator_decl =
Greg Clayton6beaaa62011-01-17 03:46:26 +00005492 EnumConstantDecl::Create (*ast,
5493 enum_type->getDecl(),
5494 SourceLocation(),
5495 name ? &identifier_table->get(name) : NULL, // Identifier
5496 QualType::getFromOpaquePtr(enumerator_clang_type),
5497 NULL,
5498 enum_llvm_apsint);
5499
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005500 if (enumerator_decl)
5501 {
5502 enum_type->getDecl()->addDecl(enumerator_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005503
5504#ifdef LLDB_CONFIGURATION_DEBUG
5505 VerifyDecl(enumerator_decl);
5506#endif
5507
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005508 return true;
5509 }
5510 }
5511 }
5512 }
5513 return false;
5514}
5515
5516#pragma mark Pointers & References
5517
Greg Clayton1be10fc2010-09-29 01:12:09 +00005518clang_type_t
5519ClangASTContext::CreatePointerType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005520{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005521 return CreatePointerType (getASTContext(), clang_type);
5522}
5523
5524clang_type_t
5525ClangASTContext::CreatePointerType (clang::ASTContext *ast, clang_type_t clang_type)
5526{
5527 if (ast && clang_type)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005528 {
5529 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5530
Greg Clayton737b9322010-09-13 03:32:57 +00005531 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5532 switch (type_class)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005533 {
5534 case clang::Type::ObjCObject:
5535 case clang::Type::ObjCInterface:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005536 return ast->getObjCObjectPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005537
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005538 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005539 return ast->getPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005540 }
5541 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005542 return NULL;
5543}
5544
Greg Clayton1be10fc2010-09-29 01:12:09 +00005545clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005546ClangASTContext::CreateLValueReferenceType (clang::ASTContext *ast,
5547 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005548{
5549 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005550 return ast->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005551 return NULL;
5552}
5553
Greg Clayton1be10fc2010-09-29 01:12:09 +00005554clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005555ClangASTContext::CreateRValueReferenceType (clang::ASTContext *ast,
5556 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005557{
5558 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005559 return ast->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005560 return NULL;
5561}
5562
Greg Clayton1be10fc2010-09-29 01:12:09 +00005563clang_type_t
5564ClangASTContext::CreateMemberPointerType (clang_type_t clang_pointee_type, clang_type_t clang_class_type)
Greg Clayton9b81a312010-06-12 01:20:30 +00005565{
5566 if (clang_pointee_type && clang_pointee_type)
5567 return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type),
5568 QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr();
5569 return NULL;
5570}
5571
Greg Clayton1a65ae12011-01-25 23:55:37 +00005572uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005573ClangASTContext::GetPointerBitSize ()
5574{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005575 ASTContext *ast = getASTContext();
5576 return ast->getTypeSize(ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005577}
5578
5579bool
Greg Clayton219cf312012-03-30 00:51:13 +00005580ClangASTContext::IsPossibleDynamicType (clang::ASTContext *ast,
5581 clang_type_t clang_type,
5582 clang_type_t *dynamic_pointee_type,
5583 bool check_cplusplus,
5584 bool check_objc)
Greg Claytondea8cb42011-06-29 22:09:02 +00005585{
5586 QualType pointee_qual_type;
5587 if (clang_type)
5588 {
5589 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5590 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5591 bool success = false;
5592 switch (type_class)
5593 {
5594 case clang::Type::Builtin:
Greg Clayton219cf312012-03-30 00:51:13 +00005595 if (check_objc && cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
Greg Claytondea8cb42011-06-29 22:09:02 +00005596 {
5597 if (dynamic_pointee_type)
5598 *dynamic_pointee_type = clang_type;
5599 return true;
5600 }
5601 break;
5602
5603 case clang::Type::ObjCObjectPointer:
Greg Clayton219cf312012-03-30 00:51:13 +00005604 if (check_objc)
5605 {
5606 if (dynamic_pointee_type)
5607 *dynamic_pointee_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5608 return true;
5609 }
5610 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005611
5612 case clang::Type::Pointer:
5613 pointee_qual_type = cast<PointerType>(qual_type)->getPointeeType();
5614 success = true;
5615 break;
5616
5617 case clang::Type::LValueReference:
5618 case clang::Type::RValueReference:
5619 pointee_qual_type = cast<ReferenceType>(qual_type)->getPointeeType();
5620 success = true;
5621 break;
5622
5623 case clang::Type::Typedef:
Greg Clayton70364252012-08-31 18:56:24 +00005624 return ClangASTContext::IsPossibleDynamicType (ast,
5625 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
5626 dynamic_pointee_type,
5627 check_cplusplus,
5628 check_objc);
Sean Callanan912855f2011-08-11 23:56:13 +00005629
5630 case clang::Type::Elaborated:
Greg Clayton70364252012-08-31 18:56:24 +00005631 return ClangASTContext::IsPossibleDynamicType (ast,
5632 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
5633 dynamic_pointee_type,
5634 check_cplusplus,
5635 check_objc);
Sean Callanan912855f2011-08-11 23:56:13 +00005636
Greg Claytondea8cb42011-06-29 22:09:02 +00005637 default:
5638 break;
5639 }
5640
5641 if (success)
5642 {
5643 // Check to make sure what we are pointing too is a possible dynamic C++ type
5644 // We currently accept any "void *" (in case we have a class that has been
5645 // watered down to an opaque pointer) and virtual C++ classes.
5646 const clang::Type::TypeClass pointee_type_class = pointee_qual_type->getTypeClass();
5647 switch (pointee_type_class)
5648 {
5649 case clang::Type::Builtin:
5650 switch (cast<clang::BuiltinType>(pointee_qual_type)->getKind())
5651 {
5652 case clang::BuiltinType::UnknownAny:
5653 case clang::BuiltinType::Void:
5654 if (dynamic_pointee_type)
5655 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5656 return true;
5657
5658 case clang::BuiltinType::NullPtr:
5659 case clang::BuiltinType::Bool:
5660 case clang::BuiltinType::Char_U:
5661 case clang::BuiltinType::UChar:
5662 case clang::BuiltinType::WChar_U:
5663 case clang::BuiltinType::Char16:
5664 case clang::BuiltinType::Char32:
5665 case clang::BuiltinType::UShort:
5666 case clang::BuiltinType::UInt:
5667 case clang::BuiltinType::ULong:
5668 case clang::BuiltinType::ULongLong:
5669 case clang::BuiltinType::UInt128:
5670 case clang::BuiltinType::Char_S:
5671 case clang::BuiltinType::SChar:
5672 case clang::BuiltinType::WChar_S:
5673 case clang::BuiltinType::Short:
5674 case clang::BuiltinType::Int:
5675 case clang::BuiltinType::Long:
5676 case clang::BuiltinType::LongLong:
5677 case clang::BuiltinType::Int128:
5678 case clang::BuiltinType::Float:
5679 case clang::BuiltinType::Double:
5680 case clang::BuiltinType::LongDouble:
5681 case clang::BuiltinType::Dependent:
5682 case clang::BuiltinType::Overload:
5683 case clang::BuiltinType::ObjCId:
5684 case clang::BuiltinType::ObjCClass:
5685 case clang::BuiltinType::ObjCSel:
5686 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00005687 case clang::BuiltinType::Half:
5688 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00005689 case clang::BuiltinType::PseudoObject:
Sean Callanan3d654b32012-09-24 22:25:51 +00005690 case clang::BuiltinType::BuiltinFn:
Greg Claytondea8cb42011-06-29 22:09:02 +00005691 break;
5692 }
5693 break;
5694
5695 case clang::Type::Record:
Greg Clayton219cf312012-03-30 00:51:13 +00005696 if (check_cplusplus)
Greg Claytondea8cb42011-06-29 22:09:02 +00005697 {
5698 CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
5699 if (cxx_record_decl)
5700 {
Greg Clayton70364252012-08-31 18:56:24 +00005701 bool is_complete = cxx_record_decl->isCompleteDefinition();
5702 if (!is_complete)
Sean Callanan6e4100ea2012-09-08 00:49:45 +00005703 is_complete = ClangASTContext::GetCompleteType (ast, pointee_qual_type.getAsOpaquePtr());
Greg Clayton70364252012-08-31 18:56:24 +00005704
5705 if (is_complete)
Greg Claytondea8cb42011-06-29 22:09:02 +00005706 {
5707 success = cxx_record_decl->isDynamicClass();
5708 }
5709 else
5710 {
Greg Clayton70364252012-08-31 18:56:24 +00005711 success = false;
Greg Claytondea8cb42011-06-29 22:09:02 +00005712 }
Greg Clayton70364252012-08-31 18:56:24 +00005713
Greg Claytondea8cb42011-06-29 22:09:02 +00005714 if (success)
5715 {
5716 if (dynamic_pointee_type)
5717 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5718 return true;
5719 }
5720 }
5721 }
5722 break;
5723
5724 case clang::Type::ObjCObject:
5725 case clang::Type::ObjCInterface:
Greg Clayton219cf312012-03-30 00:51:13 +00005726 if (check_objc)
5727 {
5728 if (dynamic_pointee_type)
5729 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5730 return true;
5731 }
5732 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005733
5734 default:
5735 break;
5736 }
5737 }
5738 }
5739 if (dynamic_pointee_type)
5740 *dynamic_pointee_type = NULL;
5741 return false;
5742}
5743
5744
5745bool
Greg Clayton007d5be2011-05-30 00:49:24 +00005746ClangASTContext::IsPossibleCPlusPlusDynamicType (clang::ASTContext *ast, clang_type_t clang_type, clang_type_t *dynamic_pointee_type)
5747{
Greg Clayton219cf312012-03-30 00:51:13 +00005748 return IsPossibleDynamicType (ast,
5749 clang_type,
5750 dynamic_pointee_type,
5751 true, // Check for dynamic C++ types
5752 false); // Check for dynamic ObjC types
Greg Clayton007d5be2011-05-30 00:49:24 +00005753}
5754
Sean Callanan98298012011-10-27 19:41:13 +00005755bool
5756ClangASTContext::IsReferenceType (clang_type_t clang_type, clang_type_t *target_type)
5757{
5758 if (clang_type == NULL)
5759 return false;
5760
5761 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5762 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5763
5764 switch (type_class)
5765 {
5766 case clang::Type::LValueReference:
5767 if (target_type)
5768 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5769 return true;
5770 case clang::Type::RValueReference:
5771 if (target_type)
5772 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5773 return true;
5774 case clang::Type::Typedef:
5775 return ClangASTContext::IsReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5776 case clang::Type::Elaborated:
5777 return ClangASTContext::IsReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5778 default:
5779 break;
5780 }
5781
5782 return false;
5783}
Greg Clayton007d5be2011-05-30 00:49:24 +00005784
5785bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005786ClangASTContext::IsPointerOrReferenceType (clang_type_t clang_type, clang_type_t*target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005787{
5788 if (clang_type == NULL)
5789 return false;
5790
5791 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005792 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5793 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005794 {
Sean Callanana2424172010-10-25 00:29:48 +00005795 case clang::Type::Builtin:
5796 switch (cast<clang::BuiltinType>(qual_type)->getKind())
5797 {
5798 default:
5799 break;
5800 case clang::BuiltinType::ObjCId:
5801 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00005802 return true;
5803 }
5804 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00005805 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005806 if (target_type)
5807 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5808 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005809 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005810 if (target_type)
5811 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5812 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005813 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005814 if (target_type)
5815 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5816 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005817 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005818 if (target_type)
5819 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5820 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005821 case clang::Type::LValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005822 if (target_type)
5823 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5824 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005825 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005826 if (target_type)
5827 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5828 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005829 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00005830 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00005831 case clang::Type::Elaborated:
5832 return ClangASTContext::IsPointerOrReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005833 default:
5834 break;
5835 }
5836 return false;
5837}
5838
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005839bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005840ClangASTContext::IsIntegerType (clang_type_t clang_type, bool &is_signed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005841{
5842 if (!clang_type)
5843 return false;
5844
5845 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5846 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal());
5847
5848 if (builtin_type)
5849 {
5850 if (builtin_type->isInteger())
Jim Inghamef651602011-12-22 19:12:40 +00005851 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005852 is_signed = builtin_type->isSignedInteger();
Jim Inghamef651602011-12-22 19:12:40 +00005853 return true;
5854 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005855 }
5856
5857 return false;
5858}
5859
5860bool
Greg Claytonaffb03b2011-07-08 18:27:39 +00005861ClangASTContext::IsPointerType (clang_type_t clang_type, clang_type_t *target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005862{
Greg Claytonaffb03b2011-07-08 18:27:39 +00005863 if (target_type)
5864 *target_type = NULL;
5865
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005866 if (clang_type)
5867 {
5868 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005869 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5870 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005871 {
Sean Callanana2424172010-10-25 00:29:48 +00005872 case clang::Type::Builtin:
5873 switch (cast<clang::BuiltinType>(qual_type)->getKind())
5874 {
5875 default:
5876 break;
5877 case clang::BuiltinType::ObjCId:
5878 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00005879 return true;
5880 }
5881 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00005882 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005883 if (target_type)
5884 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5885 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005886 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005887 if (target_type)
5888 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5889 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005890 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005891 if (target_type)
5892 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5893 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005894 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005895 if (target_type)
5896 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5897 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005898 case clang::Type::Typedef:
Greg Claytonaffb03b2011-07-08 18:27:39 +00005899 return ClangASTContext::IsPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), target_type);
Sean Callanan912855f2011-08-11 23:56:13 +00005900 case clang::Type::Elaborated:
5901 return ClangASTContext::IsPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), target_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005902 default:
5903 break;
5904 }
5905 }
5906 return false;
5907}
5908
5909bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005910ClangASTContext::IsFloatingPointType (clang_type_t clang_type, uint32_t &count, bool &is_complex)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005911{
5912 if (clang_type)
5913 {
5914 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5915
5916 if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()))
5917 {
5918 clang::BuiltinType::Kind kind = BT->getKind();
5919 if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble)
5920 {
5921 count = 1;
5922 is_complex = false;
5923 return true;
5924 }
5925 }
5926 else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal()))
5927 {
5928 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex))
5929 {
5930 count = 2;
5931 is_complex = true;
5932 return true;
5933 }
5934 }
5935 else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal()))
5936 {
5937 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex))
5938 {
5939 count = VT->getNumElements();
5940 is_complex = false;
5941 return true;
5942 }
5943 }
5944 }
5945 return false;
5946}
5947
Enrico Granata9fc19442011-07-06 02:13:41 +00005948bool
5949ClangASTContext::IsScalarType (lldb::clang_type_t clang_type)
5950{
5951 bool is_signed;
5952 if (ClangASTContext::IsIntegerType(clang_type, is_signed))
5953 return true;
5954
5955 uint32_t count;
5956 bool is_complex;
5957 return ClangASTContext::IsFloatingPointType(clang_type, count, is_complex) && !is_complex;
5958}
5959
5960bool
5961ClangASTContext::IsPointerToScalarType (lldb::clang_type_t clang_type)
5962{
5963 if (!IsPointerType(clang_type))
5964 return false;
5965
5966 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5967 lldb::clang_type_t pointee_type = qual_type.getTypePtr()->getPointeeType().getAsOpaquePtr();
5968 return IsScalarType(pointee_type);
5969}
5970
5971bool
5972ClangASTContext::IsArrayOfScalarType (lldb::clang_type_t clang_type)
5973{
Greg Clayton4ef877f2012-12-06 02:33:54 +00005974 clang_type = GetAsArrayType(clang_type, NULL, NULL, NULL);
Sean Callanan0caa21c2012-01-19 23:54:24 +00005975
5976 if (clang_type == 0)
Enrico Granata9fc19442011-07-06 02:13:41 +00005977 return false;
5978
5979 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5980 lldb::clang_type_t item_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
5981 return IsScalarType(item_type);
5982}
5983
Greg Clayton8f92f0a2010-10-14 22:52:14 +00005984
5985bool
5986ClangASTContext::GetCXXClassName (clang_type_t clang_type, std::string &class_name)
5987{
5988 if (clang_type)
5989 {
5990 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5991
5992 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5993 if (cxx_record_decl)
5994 {
5995 class_name.assign (cxx_record_decl->getIdentifier()->getNameStart());
5996 return true;
5997 }
5998 }
5999 class_name.clear();
6000 return false;
6001}
6002
6003
Greg Clayton0fffff52010-09-24 05:15:53 +00006004bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006005ClangASTContext::IsCXXClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00006006{
6007 if (clang_type)
6008 {
6009 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6010 if (qual_type->getAsCXXRecordDecl() != NULL)
6011 return true;
6012 }
6013 return false;
6014}
6015
Greg Clayton20568dd2011-10-13 23:13:20 +00006016bool
6017ClangASTContext::IsBeingDefined (lldb::clang_type_t clang_type)
6018{
6019 if (clang_type)
6020 {
6021 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6022 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type);
6023 if (tag_type)
6024 return tag_type->isBeingDefined();
6025 }
6026 return false;
6027}
6028
Greg Clayton0fffff52010-09-24 05:15:53 +00006029bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006030ClangASTContext::IsObjCClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00006031{
6032 if (clang_type)
6033 {
6034 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6035 if (qual_type->isObjCObjectOrInterfaceType())
6036 return true;
6037 }
6038 return false;
6039}
6040
Sean Callanan72772842012-02-22 23:57:45 +00006041bool
6042ClangASTContext::IsObjCObjectPointerType (lldb::clang_type_t clang_type, clang_type_t *class_type)
6043{
6044 if (clang_type)
6045 {
6046 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6047 if (qual_type->isObjCObjectPointerType())
6048 {
6049 if (class_type)
6050 {
6051 *class_type = NULL;
6052
6053 if (!qual_type->isObjCClassType() &&
6054 !qual_type->isObjCIdType())
6055 {
6056 const ObjCObjectPointerType *obj_pointer_type = dyn_cast<ObjCObjectPointerType>(qual_type);
Sean Callanand7dabe22012-03-10 01:59:11 +00006057 if (!obj_pointer_type)
6058 *class_type = NULL;
Sean Callanan1fc91ad2012-03-10 02:00:32 +00006059 else
6060 *class_type = QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr();
Sean Callanan72772842012-02-22 23:57:45 +00006061 }
6062 }
6063 return true;
6064 }
6065 }
6066 return false;
6067}
6068
6069bool
6070ClangASTContext::GetObjCClassName (lldb::clang_type_t clang_type,
6071 std::string &class_name)
6072{
6073 if (!clang_type)
6074 return false;
6075
6076 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(QualType::getFromOpaquePtr(clang_type));
6077 if (!object_type)
6078 return false;
6079
6080 const ObjCInterfaceDecl *interface = object_type->getInterface();
6081 if (!interface)
6082 return false;
6083
6084 class_name = interface->getNameAsString();
6085 return true;
6086}
Greg Clayton0fffff52010-09-24 05:15:53 +00006087
Greg Clayton73b472d2010-10-27 03:32:59 +00006088bool
6089ClangASTContext::IsCharType (clang_type_t clang_type)
6090{
6091 if (clang_type)
6092 return QualType::getFromOpaquePtr(clang_type)->isCharType();
6093 return false;
6094}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006095
6096bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006097ClangASTContext::IsCStringType (clang_type_t clang_type, uint32_t &length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006098{
Greg Clayton73b472d2010-10-27 03:32:59 +00006099 clang_type_t pointee_or_element_clang_type = NULL;
6100 Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, &pointee_or_element_clang_type));
6101
6102 if (pointee_or_element_clang_type == NULL)
6103 return false;
6104
6105 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006106 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006107 QualType pointee_or_element_qual_type (QualType::getFromOpaquePtr (pointee_or_element_clang_type));
6108
6109 if (pointee_or_element_qual_type.getUnqualifiedType()->isCharType())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006110 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006111 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6112 if (type_flags.Test (eTypeIsArray))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006113 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006114 // We know the size of the array and it could be a C string
6115 // since it is an array of characters
6116 length = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
6117 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006118 }
Greg Clayton73b472d2010-10-27 03:32:59 +00006119 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006120 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006121 length = 0;
6122 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006123 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006124
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006125 }
6126 }
6127 return false;
6128}
6129
6130bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006131ClangASTContext::IsFunctionPointerType (clang_type_t clang_type)
Greg Clayton737b9322010-09-13 03:32:57 +00006132{
6133 if (clang_type)
6134 {
6135 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6136
6137 if (qual_type->isFunctionPointerType())
6138 return true;
6139
6140 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6141 switch (type_class)
6142 {
Sean Callananfb0b7582011-03-15 00:17:19 +00006143 default:
6144 break;
Greg Clayton737b9322010-09-13 03:32:57 +00006145 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00006146 return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006147 case clang::Type::Elaborated:
6148 return ClangASTContext::IsFunctionPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00006149
6150 case clang::Type::LValueReference:
6151 case clang::Type::RValueReference:
6152 {
Sean Callanan78e37602011-01-27 04:42:51 +00006153 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00006154 if (reference_type)
6155 return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr());
6156 }
6157 break;
6158 }
6159 }
6160 return false;
6161}
6162
Greg Clayton73b472d2010-10-27 03:32:59 +00006163size_t
6164ClangASTContext::GetArraySize (clang_type_t clang_type)
6165{
6166 if (clang_type)
6167 {
Greg Claytonef37d68a2011-07-09 17:12:27 +00006168 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
6169 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6170 switch (type_class)
6171 {
6172 case clang::Type::ConstantArray:
6173 {
6174 const ConstantArrayType *array = cast<ConstantArrayType>(QualType::getFromOpaquePtr(clang_type).getTypePtr());
6175 if (array)
6176 return array->getSize().getLimitedValue();
6177 }
6178 break;
6179
6180 case clang::Type::Typedef:
6181 return ClangASTContext::GetArraySize(cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006182
6183 case clang::Type::Elaborated:
6184 return ClangASTContext::GetArraySize(cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00006185
6186 default:
6187 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006188 }
Greg Clayton73b472d2010-10-27 03:32:59 +00006189 }
6190 return 0;
6191}
Greg Clayton737b9322010-09-13 03:32:57 +00006192
Sean Callanan0caa21c2012-01-19 23:54:24 +00006193clang_type_t
Greg Clayton4ef877f2012-12-06 02:33:54 +00006194ClangASTContext::GetAsArrayType (clang_type_t clang_type, clang_type_t*member_type, uint64_t *size, bool *is_incomplete)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006195{
Greg Clayton4ef877f2012-12-06 02:33:54 +00006196 if (is_incomplete)
6197 *is_incomplete = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006198 if (!clang_type)
Sean Callanan0caa21c2012-01-19 23:54:24 +00006199 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006200
6201 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6202
Greg Clayton737b9322010-09-13 03:32:57 +00006203 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6204 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006205 {
Sean Callananfb0b7582011-03-15 00:17:19 +00006206 default:
6207 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006208
Greg Claytone1a916a2010-07-21 22:12:05 +00006209 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006210 if (member_type)
6211 *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6212 if (size)
Greg Claytonac4827f2011-04-01 18:14:08 +00006213 *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
Sean Callanan0caa21c2012-01-19 23:54:24 +00006214 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006215
Greg Claytone1a916a2010-07-21 22:12:05 +00006216 case clang::Type::IncompleteArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006217 if (member_type)
6218 *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6219 if (size)
6220 *size = 0;
Greg Clayton4ef877f2012-12-06 02:33:54 +00006221 if (is_incomplete)
6222 *is_incomplete = true;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006223 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006224
Greg Claytone1a916a2010-07-21 22:12:05 +00006225 case clang::Type::VariableArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006226 if (member_type)
6227 *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6228 if (size)
6229 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006230 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006231
Greg Claytone1a916a2010-07-21 22:12:05 +00006232 case clang::Type::DependentSizedArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006233 if (member_type)
6234 *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6235 if (size)
6236 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006237 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006238
6239 case clang::Type::Typedef:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006240 return ClangASTContext::GetAsArrayType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
6241 member_type,
Greg Clayton4ef877f2012-12-06 02:33:54 +00006242 size,
6243 is_incomplete);
Sean Callanan912855f2011-08-11 23:56:13 +00006244
6245 case clang::Type::Elaborated:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006246 return ClangASTContext::GetAsArrayType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
6247 member_type,
Greg Clayton4ef877f2012-12-06 02:33:54 +00006248 size,
6249 is_incomplete);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006250 }
Sean Callanan0caa21c2012-01-19 23:54:24 +00006251 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006252}
6253
6254
6255#pragma mark Typedefs
6256
Greg Clayton1be10fc2010-09-29 01:12:09 +00006257clang_type_t
6258ClangASTContext::CreateTypedefType (const char *name, clang_type_t clang_type, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006259{
6260 if (clang_type)
6261 {
6262 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006263 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006264 IdentifierTable *identifier_table = getIdentifierTable();
Greg Clayton6beaaa62011-01-17 03:46:26 +00006265 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006266 assert (identifier_table != NULL);
6267 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00006268 decl_ctx = ast->getTranslationUnitDecl();
6269 TypedefDecl *decl = TypedefDecl::Create (*ast,
6270 decl_ctx,
6271 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00006272 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00006273 name ? &identifier_table->get(name) : NULL, // Identifier
6274 ast->CreateTypeSourceInfo(qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00006275
Greg Clayton147e1fa2011-10-14 22:47:18 +00006276 //decl_ctx->addDecl (decl);
6277
Sean Callanan2652ad22011-01-18 01:03:44 +00006278 decl->setAccess(AS_public); // TODO respect proper access specifier
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006279
6280 // Get a uniqued QualType for the typedef decl type
Greg Clayton6beaaa62011-01-17 03:46:26 +00006281 return ast->getTypedefType (decl).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006282 }
6283 return NULL;
6284}
6285
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006286// Disable this for now since I can't seem to get a nicely formatted float
6287// out of the APFloat class without just getting the float, double or quad
6288// and then using a formatted print on it which defeats the purpose. We ideally
6289// would like to get perfect string values for any kind of float semantics
6290// so we can support remote targets. The code below also requires a patch to
6291// llvm::APInt.
6292//bool
Greg Clayton6beaaa62011-01-17 03:46:26 +00006293//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 +00006294//{
6295// uint32_t count = 0;
6296// bool is_complex = false;
6297// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6298// {
6299// unsigned num_bytes_per_float = byte_size / count;
6300// unsigned num_bits_per_float = num_bytes_per_float * 8;
6301//
6302// float_str.clear();
6303// uint32_t i;
6304// for (i=0; i<count; i++)
6305// {
6306// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
6307// bool is_ieee = false;
6308// APFloat ap_float(ap_int, is_ieee);
6309// char s[1024];
6310// unsigned int hex_digits = 0;
6311// bool upper_case = false;
6312//
6313// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
6314// {
6315// if (i > 0)
6316// float_str.append(", ");
6317// float_str.append(s);
6318// if (i == 1 && is_complex)
6319// float_str.append(1, 'i');
6320// }
6321// }
6322// return !float_str.empty();
6323// }
6324// return false;
6325//}
6326
6327size_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00006328ClangASTContext::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 +00006329{
6330 if (clang_type)
6331 {
6332 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6333 uint32_t count = 0;
6334 bool is_complex = false;
6335 if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6336 {
6337 // TODO: handle complex and vector types
6338 if (count != 1)
6339 return false;
6340
6341 StringRef s_sref(s);
Greg Clayton6beaaa62011-01-17 03:46:26 +00006342 APFloat ap_float(ast->getFloatTypeSemantics(qual_type), s_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006343
Greg Clayton6beaaa62011-01-17 03:46:26 +00006344 const uint64_t bit_size = ast->getTypeSize (qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006345 const uint64_t byte_size = bit_size / 8;
6346 if (dst_size >= byte_size)
6347 {
6348 if (bit_size == sizeof(float)*8)
6349 {
6350 float float32 = ap_float.convertToFloat();
6351 ::memcpy (dst, &float32, byte_size);
6352 return byte_size;
6353 }
6354 else if (bit_size >= 64)
6355 {
6356 llvm::APInt ap_int(ap_float.bitcastToAPInt());
6357 ::memcpy (dst, ap_int.getRawData(), byte_size);
6358 return byte_size;
6359 }
6360 }
6361 }
6362 }
6363 return 0;
6364}
Sean Callanan6fe64b52010-09-17 02:24:29 +00006365
6366unsigned
Greg Clayton1be10fc2010-09-29 01:12:09 +00006367ClangASTContext::GetTypeQualifiers(clang_type_t clang_type)
Sean Callanan6fe64b52010-09-17 02:24:29 +00006368{
6369 assert (clang_type);
6370
6371 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6372
6373 return qual_type.getQualifiers().getCVRQualifiers();
6374}
Greg Clayton6beaaa62011-01-17 03:46:26 +00006375
6376bool
6377ClangASTContext::GetCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6378{
6379 if (clang_type == NULL)
6380 return false;
6381
Greg Claytonc432c192011-01-20 04:18:48 +00006382 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006383}
6384
6385
6386bool
6387ClangASTContext::GetCompleteType (clang_type_t clang_type)
6388{
6389 return ClangASTContext::GetCompleteType (getASTContext(), clang_type);
6390}
6391
Greg Claytona2721472011-06-25 00:44:06 +00006392bool
Enrico Granata86027e92012-03-24 01:11:14 +00006393ClangASTContext::IsCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6394{
6395 if (clang_type == NULL)
6396 return false;
6397
6398 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type), false); // just check but don't let it actually complete
6399}
6400
6401
6402bool
6403ClangASTContext::IsCompleteType (clang_type_t clang_type)
6404{
6405 return ClangASTContext::IsCompleteType (getASTContext(), clang_type);
6406}
6407
6408bool
Greg Claytona2721472011-06-25 00:44:06 +00006409ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
6410 clang::Decl *decl)
6411{
6412 if (!decl)
6413 return false;
6414
6415 ExternalASTSource *ast_source = ast->getExternalSource();
6416
6417 if (!ast_source)
6418 return false;
6419
6420 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
6421 {
Greg Clayton219cf312012-03-30 00:51:13 +00006422 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006423 return true;
6424
6425 if (!tag_decl->hasExternalLexicalStorage())
6426 return false;
6427
6428 ast_source->CompleteType(tag_decl);
6429
6430 return !tag_decl->getTypeForDecl()->isIncompleteType();
6431 }
6432 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
6433 {
Sean Callanan5b26f272012-02-04 08:49:35 +00006434 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006435 return true;
6436
6437 if (!objc_interface_decl->hasExternalLexicalStorage())
6438 return false;
6439
6440 ast_source->CompleteType(objc_interface_decl);
6441
Sean Callanan5b26f272012-02-04 08:49:35 +00006442 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00006443 }
6444 else
6445 {
6446 return false;
6447 }
6448}
6449
Sean Callanan60217122012-04-13 00:10:03 +00006450void
Jim Ingham379397632012-10-27 02:54:13 +00006451ClangASTContext::SetMetadataAsUserID (uintptr_t object,
6452 user_id_t user_id)
6453{
6454 ClangASTMetadata meta_data;
6455 meta_data.SetUserID (user_id);
6456 SetMetadata (object, meta_data);
6457}
6458
6459void
Sean Callanan60217122012-04-13 00:10:03 +00006460ClangASTContext::SetMetadata (clang::ASTContext *ast,
6461 uintptr_t object,
Jim Ingham379397632012-10-27 02:54:13 +00006462 ClangASTMetadata &metadata)
Sean Callanan60217122012-04-13 00:10:03 +00006463{
6464 ClangExternalASTSourceCommon *external_source =
6465 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
6466
6467 if (external_source)
6468 external_source->SetMetadata(object, metadata);
6469}
6470
Jim Ingham379397632012-10-27 02:54:13 +00006471ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +00006472ClangASTContext::GetMetadata (clang::ASTContext *ast,
6473 uintptr_t object)
6474{
6475 ClangExternalASTSourceCommon *external_source =
6476 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
6477
6478 if (external_source && external_source->HasMetadata(object))
6479 return external_source->GetMetadata(object);
6480 else
Jim Ingham379397632012-10-27 02:54:13 +00006481 return NULL;
Sean Callanan60217122012-04-13 00:10:03 +00006482}
6483
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006484clang::DeclContext *
6485ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
6486{
Sean Callanana87bee82011-08-19 06:19:25 +00006487 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006488}
6489
6490clang::DeclContext *
6491ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
6492{
Sean Callanana87bee82011-08-19 06:19:25 +00006493 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006494}
6495
Greg Clayton685c88c2012-07-14 00:53:55 +00006496
6497bool
6498ClangASTContext::GetClassMethodInfoForDeclContext (clang::DeclContext *decl_ctx,
6499 lldb::LanguageType &language,
6500 bool &is_instance_method,
6501 ConstString &language_object_name)
6502{
6503 language_object_name.Clear();
6504 language = eLanguageTypeUnknown;
6505 is_instance_method = false;
6506
6507 if (decl_ctx)
6508 {
6509 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
6510 {
6511 if (method_decl->isStatic())
6512 {
6513 is_instance_method = false;
6514 }
6515 else
6516 {
6517 language_object_name.SetCString("this");
6518 is_instance_method = true;
6519 }
6520 language = eLanguageTypeC_plus_plus;
6521 return true;
6522 }
6523 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
6524 {
6525 // Both static and instance methods have a "self" object in objective C
6526 language_object_name.SetCString("self");
6527 if (method_decl->isInstanceMethod())
6528 {
6529 is_instance_method = true;
6530 }
6531 else
6532 {
6533 is_instance_method = false;
6534 }
6535 language = eLanguageTypeObjC;
6536 return true;
6537 }
Jim Ingham379397632012-10-27 02:54:13 +00006538 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
6539 {
6540 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), (uintptr_t) function_decl);
6541 if (metadata && metadata->HasObjectPtr())
6542 {
6543 language_object_name.SetCString (metadata->GetObjectPtrName());
6544 language = eLanguageTypeObjC;
6545 is_instance_method = true;
6546 }
6547 return true;
6548 }
Greg Clayton685c88c2012-07-14 00:53:55 +00006549 }
6550 return false;
6551}
6552