blob: 1853c30d7ffbe1f6d50e1a45fc768fadee2e564c [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eli Friedman932197d2010-06-13 19:06:42 +000010#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011
12// C Includes
13// C++ Includes
14#include <string>
15
16// Other libraries and framework includes
Greg Clayton6beaaa62011-01-17 03:46:26 +000017
18// Clang headers like to use NDEBUG inside of them to enable/disable debug
19// releated features using "#ifndef NDEBUG" preprocessor blocks to do one thing
20// or another. This is bad because it means that if clang was built in release
21// mode, it assumes that you are building in release mode which is not always
22// the case. You can end up with functions that are defined as empty in header
23// files when NDEBUG is not defined, and this can cause link errors with the
24// clang .a files that you have since you might be missing functions in the .a
25// file. So we have to define NDEBUG when including clang headers to avoid any
26// mismatches. This is covered by rdar://problem/8691220
27
Sean Callanan3b1d4f62011-10-26 17:46:51 +000028#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000029#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000030#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000031// Need to include assert.h so it is as clang would expect it to be (disabled)
32#include <assert.h>
33#endif
34
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "clang/AST/ASTContext.h"
36#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000037#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000039#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000040#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "clang/AST/RecordLayout.h"
42#include "clang/AST/Type.h"
43#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000044#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000046#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "clang/Basic/SourceManager.h"
48#include "clang/Basic/TargetInfo.h"
49#include "clang/Basic/TargetOptions.h"
50#include "clang/Frontend/FrontendOptions.h"
51#include "clang/Frontend/LangStandard.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000052
53#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000054#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000055#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
56// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
57#include <assert.h>
58#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059
Greg Clayton514487e2011-02-15 21:59:32 +000060#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061#include "lldb/Core/dwarf.h"
Greg Clayton73b472d2010-10-27 03:32:59 +000062#include "lldb/Core/Flags.h"
Sean Callananfb8b7092010-10-28 18:19:36 +000063#include "lldb/Core/Log.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000064#include "lldb/Core/RegularExpression.h"
65#include "lldb/Expression/ASTDumper.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000066#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000067#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000068#include "lldb/Target/ExecutionContext.h"
69#include "lldb/Target/Process.h"
70#include "lldb/Target/ObjCLanguageRuntime.h"
71
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072
Eli Friedman932197d2010-06-13 19:06:42 +000073#include <stdio.h>
74
Greg Claytonc86103d2010-08-05 01:57:25 +000075using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076using namespace lldb_private;
77using namespace llvm;
78using namespace clang;
79
Greg Clayton6beaaa62011-01-17 03:46:26 +000080
81static bool
Enrico Granata86027e92012-03-24 01:11:14 +000082GetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion = true)
Greg Clayton6beaaa62011-01-17 03:46:26 +000083{
84 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
85 switch (type_class)
86 {
Sean Callanan5b26f272012-02-04 08:49:35 +000087 case clang::Type::ConstantArray:
88 {
89 const clang::ArrayType *array_type = dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
90
91 if (array_type)
Enrico Granata86027e92012-03-24 01:11:14 +000092 return GetCompleteQualType (ast, array_type->getElementType(), allow_completion);
Sean Callanan5b26f272012-02-04 08:49:35 +000093 }
94 break;
95
Greg Clayton6beaaa62011-01-17 03:46:26 +000096 case clang::Type::Record:
97 case clang::Type::Enum:
98 {
Sean Callanan78e37602011-01-27 04:42:51 +000099 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +0000100 if (tag_type)
101 {
102 clang::TagDecl *tag_decl = tag_type->getDecl();
103 if (tag_decl)
104 {
Greg Clayton219cf312012-03-30 00:51:13 +0000105 if (tag_decl->isCompleteDefinition())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000106 return true;
Enrico Granata86027e92012-03-24 01:11:14 +0000107
108 if (!allow_completion)
109 return false;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000110
111 if (tag_decl->hasExternalLexicalStorage())
112 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000113 if (ast)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000114 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000115 ExternalASTSource *external_ast_source = ast->getExternalSource();
116 if (external_ast_source)
117 {
118 external_ast_source->CompleteType(tag_decl);
119 return !tag_type->isIncompleteType();
120 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000121 }
122 }
123 return false;
124 }
125 }
126
127 }
128 break;
129
130 case clang::Type::ObjCObject:
131 case clang::Type::ObjCInterface:
132 {
Sean Callanan78e37602011-01-27 04:42:51 +0000133 const clang::ObjCObjectType *objc_class_type = dyn_cast<clang::ObjCObjectType>(qual_type);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000134 if (objc_class_type)
135 {
136 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
137 // We currently can't complete objective C types through the newly added ASTContext
138 // because it only supports TagDecl objects right now...
Enrico Granata9dd75c82011-07-15 23:30:15 +0000139 if (class_interface_decl)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000140 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000141 if (class_interface_decl->getDefinition())
142 return true;
143
Enrico Granata86027e92012-03-24 01:11:14 +0000144 if (!allow_completion)
145 return false;
Greg Clayton20533982012-03-30 23:48:28 +0000146
Sean Callanan5b26f272012-02-04 08:49:35 +0000147 if (class_interface_decl->hasExternalLexicalStorage())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000148 {
Enrico Granata0a3958e2011-07-02 00:25:22 +0000149 if (ast)
Greg Clayton007d5be2011-05-30 00:49:24 +0000150 {
Enrico Granata0a3958e2011-07-02 00:25:22 +0000151 ExternalASTSource *external_ast_source = ast->getExternalSource();
152 if (external_ast_source)
153 {
154 external_ast_source->CompleteType (class_interface_decl);
Sean Callanan5b26f272012-02-04 08:49:35 +0000155 return !objc_class_type->isIncompleteType();
Enrico Granata0a3958e2011-07-02 00:25:22 +0000156 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000157 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000158 }
Enrico Granata0a3958e2011-07-02 00:25:22 +0000159 return false;
Sean Callanan5b26f272012-02-04 08:49:35 +0000160 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000161 }
162 }
163 break;
164
165 case clang::Type::Typedef:
Enrico Granata86027e92012-03-24 01:11:14 +0000166 return GetCompleteQualType (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType(), allow_completion);
Sean Callanan912855f2011-08-11 23:56:13 +0000167
168 case clang::Type::Elaborated:
Enrico Granata86027e92012-03-24 01:11:14 +0000169 return GetCompleteQualType (ast, cast<ElaboratedType>(qual_type)->getNamedType(), allow_completion);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000170
171 default:
172 break;
173 }
174
175 return true;
176}
177
Greg Clayton8cf05932010-07-22 18:30:50 +0000178static AccessSpecifier
Greg Claytonc86103d2010-08-05 01:57:25 +0000179ConvertAccessTypeToAccessSpecifier (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000180{
181 switch (access)
182 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000183 default: break;
184 case eAccessNone: return AS_none;
185 case eAccessPublic: return AS_public;
186 case eAccessPrivate: return AS_private;
187 case eAccessProtected: return AS_protected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000188 }
189 return AS_none;
190}
191
192static ObjCIvarDecl::AccessControl
Greg Claytonc86103d2010-08-05 01:57:25 +0000193ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000194{
195 switch (access)
196 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000197 case eAccessNone: return ObjCIvarDecl::None;
198 case eAccessPublic: return ObjCIvarDecl::Public;
199 case eAccessPrivate: return ObjCIvarDecl::Private;
200 case eAccessProtected: return ObjCIvarDecl::Protected;
201 case eAccessPackage: return ObjCIvarDecl::Package;
Greg Clayton8cf05932010-07-22 18:30:50 +0000202 }
203 return ObjCIvarDecl::None;
204}
205
206
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207static void
208ParseLangArgs
209(
210 LangOptions &Opts,
Greg Clayton94e5d782010-06-13 17:34:29 +0000211 InputKind IK
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212)
213{
214 // FIXME: Cleanup per-file based stuff.
215
216 // Set some properties which depend soley on the input kind; it would be nice
217 // to move these to the language standard, and have the driver resolve the
218 // input kind + language standard.
Greg Clayton94e5d782010-06-13 17:34:29 +0000219 if (IK == IK_Asm) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220 Opts.AsmPreprocessor = 1;
Greg Clayton94e5d782010-06-13 17:34:29 +0000221 } else if (IK == IK_ObjC ||
222 IK == IK_ObjCXX ||
223 IK == IK_PreprocessedObjC ||
224 IK == IK_PreprocessedObjCXX) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225 Opts.ObjC1 = Opts.ObjC2 = 1;
226 }
227
228 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
229
230 if (LangStd == LangStandard::lang_unspecified) {
231 // Based on the base language, pick one.
232 switch (IK) {
Greg Clayton94e5d782010-06-13 17:34:29 +0000233 case IK_None:
234 case IK_AST:
Sean Callananfb0b7582011-03-15 00:17:19 +0000235 case IK_LLVM_IR:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000236 assert (!"Invalid input kind!");
Greg Clayton94e5d782010-06-13 17:34:29 +0000237 case IK_OpenCL:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238 LangStd = LangStandard::lang_opencl;
239 break;
Sean Callananfb0b7582011-03-15 00:17:19 +0000240 case IK_CUDA:
241 LangStd = LangStandard::lang_cuda;
242 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000243 case IK_Asm:
244 case IK_C:
245 case IK_PreprocessedC:
246 case IK_ObjC:
247 case IK_PreprocessedObjC:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248 LangStd = LangStandard::lang_gnu99;
249 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000250 case IK_CXX:
251 case IK_PreprocessedCXX:
252 case IK_ObjCXX:
253 case IK_PreprocessedObjCXX:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254 LangStd = LangStandard::lang_gnucxx98;
255 break;
256 }
257 }
258
259 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
Filipe Cabecinhase818ca22012-11-12 21:26:32 +0000260 Opts.LineComment = Std.hasLineComments();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261 Opts.C99 = Std.isC99();
262 Opts.CPlusPlus = Std.isCPlusPlus();
Chandler Carruth38336a12013-01-02 12:55:00 +0000263 Opts.CPlusPlus11 = Std.isCPlusPlus11();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264 Opts.Digraphs = Std.hasDigraphs();
265 Opts.GNUMode = Std.isGNUMode();
266 Opts.GNUInline = !Std.isC99();
267 Opts.HexFloats = Std.hasHexFloats();
268 Opts.ImplicitInt = Std.hasImplicitInt();
Enrico Granatac921e342013-01-10 02:37:22 +0000269
270 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271
272 // OpenCL has some additional defaults.
273 if (LangStd == LangStandard::lang_opencl) {
274 Opts.OpenCL = 1;
275 Opts.AltiVec = 1;
276 Opts.CXXOperatorNames = 1;
277 Opts.LaxVectorConversions = 1;
278 }
279
280 // OpenCL and C++ both have bool, true, false keywords.
281 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
282
283// if (Opts.CPlusPlus)
284// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
285//
286// if (Args.hasArg(OPT_fobjc_gc_only))
287// Opts.setGCMode(LangOptions::GCOnly);
288// else if (Args.hasArg(OPT_fobjc_gc))
289// Opts.setGCMode(LangOptions::HybridGC);
290//
291// if (Args.hasArg(OPT_print_ivar_layout))
292// Opts.ObjCGCBitmapPrint = 1;
293//
294// if (Args.hasArg(OPT_faltivec))
295// Opts.AltiVec = 1;
296//
297// if (Args.hasArg(OPT_pthread))
298// Opts.POSIXThreads = 1;
299//
300// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
301// "default");
302// if (Vis == "default")
Sean Callanan37f76e52013-02-19 19:16:37 +0000303 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304// else if (Vis == "hidden")
305// Opts.setVisibilityMode(LangOptions::Hidden);
306// else if (Vis == "protected")
307// Opts.setVisibilityMode(LangOptions::Protected);
308// else
309// Diags.Report(diag::err_drv_invalid_value)
310// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
311
312// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
313
314 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
315 // is specified, or -std is set to a conforming mode.
316 Opts.Trigraphs = !Opts.GNUMode;
317// if (Args.hasArg(OPT_trigraphs))
318// Opts.Trigraphs = 1;
319//
320// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
321// OPT_fno_dollars_in_identifiers,
322// !Opts.AsmPreprocessor);
323// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
324// Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
325// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
326// if (Args.hasArg(OPT_fno_lax_vector_conversions))
327// Opts.LaxVectorConversions = 0;
328// Opts.Exceptions = Args.hasArg(OPT_fexceptions);
329// Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
330// Opts.Blocks = Args.hasArg(OPT_fblocks);
331// Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char);
332// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
333// Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
334// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
335// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
336// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
337// Opts.AccessControl = Args.hasArg(OPT_faccess_control);
338// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
339// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
340// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
341// Diags);
342// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
343// Opts.ObjCConstantStringClass = getLastArgValue(Args,
344// OPT_fconstant_string_class);
345// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
346// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
347// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
348// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
349// Opts.Static = Args.hasArg(OPT_static_define);
350 Opts.OptimizeSize = 0;
351
352 // FIXME: Eliminate this dependency.
353// unsigned Opt =
354// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
355// Opts.Optimize = Opt != 0;
356 unsigned Opt = 0;
357
358 // This is the __NO_INLINE__ define, which just depends on things like the
359 // optimization level and -fno-inline, not actually whether the backend has
360 // inlining enabled.
361 //
362 // FIXME: This is affected by other options (-fno-inline).
Sean Callanan3d654b32012-09-24 22:25:51 +0000363 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364
365// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
366// switch (SSP) {
367// default:
368// Diags.Report(diag::err_drv_invalid_value)
369// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
370// break;
371// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
372// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
373// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
374// }
375}
376
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377
Greg Clayton6beaaa62011-01-17 03:46:26 +0000378ClangASTContext::ClangASTContext (const char *target_triple) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379 m_target_triple(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000380 m_ast_ap(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381 m_language_options_ap(),
382 m_source_manager_ap(),
Sean Callanan880e6802011-10-07 23:18:13 +0000383 m_diagnostics_engine_ap(),
Sean Callananc5069ad2012-10-17 22:11:14 +0000384 m_target_options_rp(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385 m_target_info_ap(),
386 m_identifier_table_ap(),
387 m_selector_table_ap(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000388 m_builtins_ap(),
389 m_callback_tag_decl (NULL),
390 m_callback_objc_decl (NULL),
391 m_callback_baton (NULL)
392
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393{
394 if (target_triple && target_triple[0])
Greg Clayton880cbb02011-07-30 01:26:02 +0000395 SetTargetTriple (target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396}
397
398//----------------------------------------------------------------------
399// Destructor
400//----------------------------------------------------------------------
401ClangASTContext::~ClangASTContext()
402{
403 m_builtins_ap.reset();
404 m_selector_table_ap.reset();
405 m_identifier_table_ap.reset();
406 m_target_info_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000407 m_target_options_rp.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000408 m_diagnostics_engine_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409 m_source_manager_ap.reset();
410 m_language_options_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000411 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412}
413
414
415void
416ClangASTContext::Clear()
417{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000418 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419 m_language_options_ap.reset();
420 m_source_manager_ap.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000421 m_diagnostics_engine_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000422 m_target_options_rp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423 m_target_info_ap.reset();
424 m_identifier_table_ap.reset();
425 m_selector_table_ap.reset();
426 m_builtins_ap.reset();
427}
428
429const char *
430ClangASTContext::GetTargetTriple ()
431{
432 return m_target_triple.c_str();
433}
434
435void
436ClangASTContext::SetTargetTriple (const char *target_triple)
437{
438 Clear();
439 m_target_triple.assign(target_triple);
440}
441
Greg Clayton514487e2011-02-15 21:59:32 +0000442void
443ClangASTContext::SetArchitecture (const ArchSpec &arch)
444{
Greg Clayton880cbb02011-07-30 01:26:02 +0000445 SetTargetTriple(arch.GetTriple().str().c_str());
Greg Clayton514487e2011-02-15 21:59:32 +0000446}
447
Greg Clayton6beaaa62011-01-17 03:46:26 +0000448bool
449ClangASTContext::HasExternalSource ()
450{
451 ASTContext *ast = getASTContext();
452 if (ast)
453 return ast->getExternalSource () != NULL;
454 return false;
455}
456
457void
458ClangASTContext::SetExternalSource (llvm::OwningPtr<ExternalASTSource> &ast_source_ap)
459{
460 ASTContext *ast = getASTContext();
461 if (ast)
462 {
463 ast->setExternalSource (ast_source_ap);
464 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
465 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
466 }
467}
468
469void
470ClangASTContext::RemoveExternalSource ()
471{
472 ASTContext *ast = getASTContext();
473
474 if (ast)
475 {
476 llvm::OwningPtr<ExternalASTSource> empty_ast_source_ap;
477 ast->setExternalSource (empty_ast_source_ap);
478 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
479 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
480 }
481}
482
483
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484
485ASTContext *
486ClangASTContext::getASTContext()
487{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000488 if (m_ast_ap.get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 {
Greg Clayton6beaaa62011-01-17 03:46:26 +0000490 m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
491 *getSourceManager(),
Sean Callanan880e6802011-10-07 23:18:13 +0000492 getTargetInfo(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000493 *getIdentifierTable(),
494 *getSelectorTable(),
495 *getBuiltinContext(),
496 0));
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000497
Greg Clayton6beaaa62011-01-17 03:46:26 +0000498 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
499 {
500 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
501 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
502 }
503
Sean Callanan880e6802011-10-07 23:18:13 +0000504 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000506 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507}
508
509Builtin::Context *
510ClangASTContext::getBuiltinContext()
511{
512 if (m_builtins_ap.get() == NULL)
Sean Callanan880e6802011-10-07 23:18:13 +0000513 m_builtins_ap.reset (new Builtin::Context());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514 return m_builtins_ap.get();
515}
516
517IdentifierTable *
518ClangASTContext::getIdentifierTable()
519{
520 if (m_identifier_table_ap.get() == NULL)
521 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), NULL));
522 return m_identifier_table_ap.get();
523}
524
525LangOptions *
526ClangASTContext::getLanguageOptions()
527{
528 if (m_language_options_ap.get() == NULL)
529 {
530 m_language_options_ap.reset(new LangOptions());
Greg Clayton94e5d782010-06-13 17:34:29 +0000531 ParseLangArgs(*m_language_options_ap, IK_ObjCXX);
532// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000533 }
534 return m_language_options_ap.get();
535}
536
537SelectorTable *
538ClangASTContext::getSelectorTable()
539{
540 if (m_selector_table_ap.get() == NULL)
541 m_selector_table_ap.reset (new SelectorTable());
542 return m_selector_table_ap.get();
543}
544
Sean Callanan79439e82010-11-18 02:56:27 +0000545clang::FileManager *
546ClangASTContext::getFileManager()
547{
548 if (m_file_manager_ap.get() == NULL)
Greg Clayton38a61402010-12-02 23:20:03 +0000549 {
550 clang::FileSystemOptions file_system_options;
551 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
552 }
Sean Callanan79439e82010-11-18 02:56:27 +0000553 return m_file_manager_ap.get();
554}
555
Greg Claytone1a916a2010-07-21 22:12:05 +0000556clang::SourceManager *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557ClangASTContext::getSourceManager()
558{
559 if (m_source_manager_ap.get() == NULL)
Sean Callanan880e6802011-10-07 23:18:13 +0000560 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561 return m_source_manager_ap.get();
562}
563
Sean Callanan880e6802011-10-07 23:18:13 +0000564clang::DiagnosticsEngine *
565ClangASTContext::getDiagnosticsEngine()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000566{
Sean Callanan880e6802011-10-07 23:18:13 +0000567 if (m_diagnostics_engine_ap.get() == NULL)
Greg Claytona651b532010-11-19 21:46:54 +0000568 {
569 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Sean Callananec8f1ef2012-10-25 01:00:25 +0000570 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
Greg Claytona651b532010-11-19 21:46:54 +0000571 }
Sean Callanan880e6802011-10-07 23:18:13 +0000572 return m_diagnostics_engine_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573}
574
Sean Callanan880e6802011-10-07 23:18:13 +0000575class NullDiagnosticConsumer : public DiagnosticConsumer
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000576{
577public:
Sean Callanan880e6802011-10-07 23:18:13 +0000578 NullDiagnosticConsumer ()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000579 {
580 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
581 }
582
Sean Callanan880e6802011-10-07 23:18:13 +0000583 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const Diagnostic &info)
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000584 {
585 if (m_log)
586 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000587 llvm::SmallVector<char, 32> diag_str(10);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000588 info.FormatDiagnostic(diag_str);
589 diag_str.push_back('\0');
590 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
591 }
592 }
Sean Callanan880e6802011-10-07 23:18:13 +0000593
594 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
595 {
596 return new NullDiagnosticConsumer ();
597 }
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000598private:
599 LogSP m_log;
600};
601
Sean Callanan880e6802011-10-07 23:18:13 +0000602DiagnosticConsumer *
603ClangASTContext::getDiagnosticConsumer()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000604{
Sean Callanan880e6802011-10-07 23:18:13 +0000605 if (m_diagnostic_consumer_ap.get() == NULL)
606 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000607
Sean Callanan880e6802011-10-07 23:18:13 +0000608 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000609}
610
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611TargetOptions *
612ClangASTContext::getTargetOptions()
613{
Sean Callananc5069ad2012-10-17 22:11:14 +0000614 if (m_target_options_rp.getPtr() == NULL && !m_target_triple.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615 {
Sean Callananc5069ad2012-10-17 22:11:14 +0000616 m_target_options_rp.reset ();
617 m_target_options_rp = new TargetOptions();
618 if (m_target_options_rp.getPtr() != NULL)
619 m_target_options_rp->Triple = m_target_triple;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 }
Sean Callananc5069ad2012-10-17 22:11:14 +0000621 return m_target_options_rp.getPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622}
623
624
625TargetInfo *
626ClangASTContext::getTargetInfo()
627{
Greg Clayton70512312012-05-08 01:45:38 +0000628 // target_triple should be something like "x86_64-apple-macosx"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 if (m_target_info_ap.get() == NULL && !m_target_triple.empty())
Greg Clayton38d880a2012-11-16 21:35:22 +0000630 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), getTargetOptions()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631 return m_target_info_ap.get();
632}
633
634#pragma mark Basic Types
635
636static inline bool
Greg Clayton6beaaa62011-01-17 03:46:26 +0000637QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000639 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640 if (qual_type_bit_size == bit_size)
641 return true;
642 return false;
643}
644
Greg Clayton1be10fc2010-09-29 01:12:09 +0000645clang_type_t
Greg Claytonc86103d2010-08-05 01:57:25 +0000646ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000648 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000649
Greg Clayton6beaaa62011-01-17 03:46:26 +0000650 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651
Greg Clayton6beaaa62011-01-17 03:46:26 +0000652 return GetBuiltinTypeForEncodingAndBitSize (ast, encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653}
654
Greg Clayton1be10fc2010-09-29 01:12:09 +0000655clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000656ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000658 if (!ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659 return NULL;
660
661 switch (encoding)
662 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000663 case eEncodingInvalid:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000664 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
665 return ast->VoidPtrTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666 break;
667
Greg Claytonc86103d2010-08-05 01:57:25 +0000668 case eEncodingUint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000669 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
670 return ast->UnsignedCharTy.getAsOpaquePtr();
671 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
672 return ast->UnsignedShortTy.getAsOpaquePtr();
673 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
674 return ast->UnsignedIntTy.getAsOpaquePtr();
675 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
676 return ast->UnsignedLongTy.getAsOpaquePtr();
677 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
678 return ast->UnsignedLongLongTy.getAsOpaquePtr();
679 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
680 return ast->UnsignedInt128Ty.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000681 break;
682
Greg Claytonc86103d2010-08-05 01:57:25 +0000683 case eEncodingSint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000684 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
685 return ast->CharTy.getAsOpaquePtr();
686 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
687 return ast->ShortTy.getAsOpaquePtr();
688 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
689 return ast->IntTy.getAsOpaquePtr();
690 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
691 return ast->LongTy.getAsOpaquePtr();
692 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
693 return ast->LongLongTy.getAsOpaquePtr();
694 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
695 return ast->Int128Ty.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696 break;
697
Greg Claytonc86103d2010-08-05 01:57:25 +0000698 case eEncodingIEEE754:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000699 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
700 return ast->FloatTy.getAsOpaquePtr();
701 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
702 return ast->DoubleTy.getAsOpaquePtr();
703 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
704 return ast->LongDoubleTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000705 break;
706
Greg Claytonc86103d2010-08-05 01:57:25 +0000707 case eEncodingVector:
Johnny Chenc79c93a2012-03-07 01:12:24 +0000708 // Sanity check that bit_size is a multiple of 8's.
709 if (bit_size && !(bit_size & 0x7u))
710 return ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8).getAsOpaquePtr();
711 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712 }
713
714 return NULL;
715}
716
Greg Clayton1be10fc2010-09-29 01:12:09 +0000717clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
719{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000720 ASTContext *ast = getASTContext();
Sean Callanan38d4df52012-04-03 01:10:10 +0000721
722#define streq(a,b) strcmp(a,b) == 0
Greg Clayton6beaaa62011-01-17 03:46:26 +0000723 assert (ast != NULL);
724 if (ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725 {
726 switch (dw_ate)
727 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000728 default:
729 break;
Greg Clayton605684e2011-10-28 23:06:08 +0000730
Sean Callanan38d4df52012-04-03 01:10:10 +0000731 case DW_ATE_address:
732 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
733 return ast->VoidPtrTy.getAsOpaquePtr();
734 break;
735
736 case DW_ATE_boolean:
737 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
738 return ast->BoolTy.getAsOpaquePtr();
739 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
740 return ast->UnsignedCharTy.getAsOpaquePtr();
741 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
742 return ast->UnsignedShortTy.getAsOpaquePtr();
743 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
744 return ast->UnsignedIntTy.getAsOpaquePtr();
745 break;
746
747 case DW_ATE_lo_user:
748 // This has been seen to mean DW_AT_complex_integer
749 if (type_name)
Greg Clayton605684e2011-10-28 23:06:08 +0000750 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000751 if (::strstr(type_name, "complex"))
752 {
753 clang_type_t complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2);
754 return ast->getComplexType (QualType::getFromOpaquePtr(complex_int_clang_type)).getAsOpaquePtr();
755 }
Greg Clayton605684e2011-10-28 23:06:08 +0000756 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000757 break;
758
759 case DW_ATE_complex_float:
760 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
761 return ast->FloatComplexTy.getAsOpaquePtr();
762 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
763 return ast->DoubleComplexTy.getAsOpaquePtr();
764 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
765 return ast->LongDoubleComplexTy.getAsOpaquePtr();
766 else
Greg Clayton605684e2011-10-28 23:06:08 +0000767 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000768 clang_type_t complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2);
769 return ast->getComplexType (QualType::getFromOpaquePtr(complex_float_clang_type)).getAsOpaquePtr();
Greg Clayton605684e2011-10-28 23:06:08 +0000770 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000771 break;
772
773 case DW_ATE_float:
774 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
775 return ast->FloatTy.getAsOpaquePtr();
776 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
777 return ast->DoubleTy.getAsOpaquePtr();
778 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
779 return ast->LongDoubleTy.getAsOpaquePtr();
780 break;
781
782 case DW_ATE_signed:
783 if (type_name)
784 {
785 if (streq(type_name, "wchar_t") &&
786 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
787 return ast->WCharTy.getAsOpaquePtr();
788 if (streq(type_name, "void") &&
789 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
790 return ast->VoidTy.getAsOpaquePtr();
791 if (strstr(type_name, "long long") &&
792 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
793 return ast->LongLongTy.getAsOpaquePtr();
794 if (strstr(type_name, "long") &&
795 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
796 return ast->LongTy.getAsOpaquePtr();
797 if (strstr(type_name, "short") &&
798 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
799 return ast->ShortTy.getAsOpaquePtr();
800 if (strstr(type_name, "char"))
801 {
802 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
803 return ast->CharTy.getAsOpaquePtr();
804 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
805 return ast->SignedCharTy.getAsOpaquePtr();
806 }
807 if (strstr(type_name, "int"))
808 {
809 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
810 return ast->IntTy.getAsOpaquePtr();
811 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
812 return ast->Int128Ty.getAsOpaquePtr();
813 }
814 }
815 // We weren't able to match up a type name, just search by size
816 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
817 return ast->CharTy.getAsOpaquePtr();
818 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
819 return ast->ShortTy.getAsOpaquePtr();
820 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
821 return ast->IntTy.getAsOpaquePtr();
822 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
823 return ast->LongTy.getAsOpaquePtr();
824 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
825 return ast->LongLongTy.getAsOpaquePtr();
826 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
827 return ast->Int128Ty.getAsOpaquePtr();
828 break;
829
830 case DW_ATE_signed_char:
831 if (type_name)
832 {
833 if (streq(type_name, "signed char"))
834 {
835 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
836 return ast->SignedCharTy.getAsOpaquePtr();
837 }
838 }
839 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
840 return ast->CharTy.getAsOpaquePtr();
841 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
842 return ast->SignedCharTy.getAsOpaquePtr();
843 break;
844
845 case DW_ATE_unsigned:
846 if (type_name)
847 {
848 if (strstr(type_name, "long long"))
849 {
850 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
851 return ast->UnsignedLongLongTy.getAsOpaquePtr();
852 }
853 else if (strstr(type_name, "long"))
854 {
855 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
856 return ast->UnsignedLongTy.getAsOpaquePtr();
857 }
858 else if (strstr(type_name, "short"))
859 {
860 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
861 return ast->UnsignedShortTy.getAsOpaquePtr();
862 }
863 else if (strstr(type_name, "char"))
864 {
865 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
866 return ast->UnsignedCharTy.getAsOpaquePtr();
867 }
868 else if (strstr(type_name, "int"))
869 {
870 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
871 return ast->UnsignedIntTy.getAsOpaquePtr();
872 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
873 return ast->UnsignedInt128Ty.getAsOpaquePtr();
874 }
875 }
876 // We weren't able to match up a type name, just search by size
877 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
878 return ast->UnsignedCharTy.getAsOpaquePtr();
879 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
880 return ast->UnsignedShortTy.getAsOpaquePtr();
881 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
882 return ast->UnsignedIntTy.getAsOpaquePtr();
883 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
884 return ast->UnsignedLongTy.getAsOpaquePtr();
885 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
886 return ast->UnsignedLongLongTy.getAsOpaquePtr();
887 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
888 return ast->UnsignedInt128Ty.getAsOpaquePtr();
889 break;
890
891 case DW_ATE_unsigned_char:
892 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
893 return ast->UnsignedCharTy.getAsOpaquePtr();
894 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
895 return ast->UnsignedShortTy.getAsOpaquePtr();
896 break;
897
898 case DW_ATE_imaginary_float:
899 break;
900
901 case DW_ATE_UTF:
902 if (type_name)
903 {
904 if (streq(type_name, "char16_t"))
905 {
906 return ast->Char16Ty.getAsOpaquePtr();
907 }
908 else if (streq(type_name, "char32_t"))
909 {
910 return ast->Char32Ty.getAsOpaquePtr();
911 }
912 }
913 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000914 }
915 }
916 // This assert should fire for anything that we don't catch above so we know
917 // to fix any issues we run into.
Greg Claytondc968d12011-05-17 18:15:05 +0000918 if (type_name)
919 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000920 Host::SystemLog (Host::eSystemLogError, "error: need to add support for DW_TAG_base_type '%s' encoded with DW_ATE = 0x%x, bit_size = %u\n", type_name, dw_ate, bit_size);
Greg Claytondc968d12011-05-17 18:15:05 +0000921 }
922 else
923 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000924 Host::SystemLog (Host::eSystemLogError, "error: need to add support for DW_TAG_base_type encoded with DW_ATE = 0x%x, bit_size = %u\n", dw_ate, bit_size);
Greg Claytondc968d12011-05-17 18:15:05 +0000925 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000926 return NULL;
927}
928
Greg Clayton1be10fc2010-09-29 01:12:09 +0000929clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000930ClangASTContext::GetBuiltInType_void(ASTContext *ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000931{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000932 return ast->VoidTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000933}
934
Greg Clayton1be10fc2010-09-29 01:12:09 +0000935clang_type_t
Sean Callananf7c3e272010-11-19 02:52:21 +0000936ClangASTContext::GetBuiltInType_bool()
937{
938 return getASTContext()->BoolTy.getAsOpaquePtr();
939}
940
941clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000942ClangASTContext::GetBuiltInType_objc_id()
943{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000944 return getASTContext()->getObjCIdType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000945}
946
Greg Clayton1be10fc2010-09-29 01:12:09 +0000947clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000948ClangASTContext::GetBuiltInType_objc_Class()
949{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000950 return getASTContext()->getObjCClassType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000951}
952
Greg Clayton1be10fc2010-09-29 01:12:09 +0000953clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000954ClangASTContext::GetBuiltInType_objc_selector()
955{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000956 return getASTContext()->getObjCSelType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000957}
958
Greg Clayton1be10fc2010-09-29 01:12:09 +0000959clang_type_t
Sean Callanan77502262011-05-12 23:54:16 +0000960ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast)
961{
962 return ast->UnknownAnyTy.getAsOpaquePtr();
963}
964
965clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000966ClangASTContext::GetCStringType (bool is_const)
967{
968 QualType char_type(getASTContext()->CharTy);
969
970 if (is_const)
971 char_type.addConst();
972
973 return getASTContext()->getPointerType(char_type).getAsOpaquePtr();
974}
975
Greg Clayton1be10fc2010-09-29 01:12:09 +0000976clang_type_t
Sean Callanana6582262012-04-05 00:12:52 +0000977ClangASTContext::GetVoidType()
978{
979 return GetVoidType(getASTContext());
980}
981
982clang_type_t
983ClangASTContext::GetVoidType(ASTContext *ast)
984{
985 return ast->VoidTy.getAsOpaquePtr();
986}
987
988clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000989ClangASTContext::GetVoidPtrType (bool is_const)
990{
991 return GetVoidPtrType(getASTContext(), is_const);
992}
993
Greg Clayton1be10fc2010-09-29 01:12:09 +0000994clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000995ClangASTContext::GetVoidPtrType (ASTContext *ast, bool is_const)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000997 QualType void_ptr_type(ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000998
999 if (is_const)
1000 void_ptr_type.addConst();
1001
1002 return void_ptr_type.getAsOpaquePtr();
1003}
1004
Sean Callanan09ab4b72011-11-30 22:11:59 +00001005clang::DeclContext *
1006ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast)
1007{
1008 return ast->getTranslationUnitDecl();
1009}
1010
Greg Clayton1be10fc2010-09-29 01:12:09 +00001011clang_type_t
Greg Clayton38a61402010-12-02 23:20:03 +00001012ClangASTContext::CopyType (ASTContext *dst_ast,
1013 ASTContext *src_ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001014 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015{
Sean Callanan79439e82010-11-18 02:56:27 +00001016 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001017 FileManager file_manager (file_system_options);
1018 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001019 *src_ast, file_manager,
1020 false);
Sean Callanan0617fcb2010-11-09 22:37:10 +00001021
Greg Clayton38a61402010-12-02 23:20:03 +00001022 QualType src (QualType::getFromOpaquePtr(clang_type));
1023 QualType dst (importer.Import(src));
Sean Callanan0617fcb2010-11-09 22:37:10 +00001024
1025 return dst.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026}
1027
Greg Clayton526e5af2010-11-13 03:52:47 +00001028
1029clang::Decl *
Greg Clayton38a61402010-12-02 23:20:03 +00001030ClangASTContext::CopyDecl (ASTContext *dst_ast,
1031 ASTContext *src_ast,
Greg Clayton526e5af2010-11-13 03:52:47 +00001032 clang::Decl *source_decl)
Sean Callanan7fddd4c2010-12-11 00:08:56 +00001033{
Sean Callanan79439e82010-11-18 02:56:27 +00001034 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001035 FileManager file_manager (file_system_options);
1036 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001037 *src_ast, file_manager,
1038 false);
Greg Clayton526e5af2010-11-13 03:52:47 +00001039
1040 return importer.Import(source_decl);
1041}
1042
Sean Callanan23a30272010-07-16 00:00:27 +00001043bool
Greg Clayton84db9102012-03-26 23:03:23 +00001044ClangASTContext::AreTypesSame (ASTContext *ast,
1045 clang_type_t type1,
1046 clang_type_t type2,
1047 bool ignore_qualifiers)
Sean Callanan4dcca2622010-07-15 22:30:52 +00001048{
Greg Clayton55995eb2012-04-06 17:38:55 +00001049 if (type1 == type2)
1050 return true;
1051
Sean Callanan5056ab02012-02-18 02:01:03 +00001052 QualType type1_qual = QualType::getFromOpaquePtr(type1);
1053 QualType type2_qual = QualType::getFromOpaquePtr(type2);
1054
1055 if (ignore_qualifiers)
1056 {
1057 type1_qual = type1_qual.getUnqualifiedType();
1058 type2_qual = type2_qual.getUnqualifiedType();
1059 }
1060
1061 return ast->hasSameType (type1_qual,
1062 type2_qual);
Sean Callanan4dcca2622010-07-15 22:30:52 +00001063}
1064
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001065#pragma mark CVR modifiers
1066
Greg Clayton1be10fc2010-09-29 01:12:09 +00001067clang_type_t
1068ClangASTContext::AddConstModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001069{
1070 if (clang_type)
1071 {
1072 QualType result(QualType::getFromOpaquePtr(clang_type));
1073 result.addConst();
1074 return result.getAsOpaquePtr();
1075 }
1076 return NULL;
1077}
1078
Greg Clayton1be10fc2010-09-29 01:12:09 +00001079clang_type_t
1080ClangASTContext::AddRestrictModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081{
1082 if (clang_type)
1083 {
1084 QualType result(QualType::getFromOpaquePtr(clang_type));
1085 result.getQualifiers().setRestrict (true);
1086 return result.getAsOpaquePtr();
1087 }
1088 return NULL;
1089}
1090
Greg Clayton1be10fc2010-09-29 01:12:09 +00001091clang_type_t
1092ClangASTContext::AddVolatileModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001093{
1094 if (clang_type)
1095 {
1096 QualType result(QualType::getFromOpaquePtr(clang_type));
1097 result.getQualifiers().setVolatile (true);
1098 return result.getAsOpaquePtr();
1099 }
1100 return NULL;
1101}
1102
Greg Clayton6beaaa62011-01-17 03:46:26 +00001103
1104clang_type_t
1105ClangASTContext::GetTypeForDecl (TagDecl *decl)
1106{
1107 // No need to call the getASTContext() accessor (which can create the AST
1108 // if it isn't created yet, because we can't have created a decl in this
1109 // AST if our AST didn't already exist...
1110 if (m_ast_ap.get())
1111 return m_ast_ap->getTagDeclType(decl).getAsOpaquePtr();
1112 return NULL;
1113}
1114
1115clang_type_t
1116ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1117{
1118 // No need to call the getASTContext() accessor (which can create the AST
1119 // if it isn't created yet, because we can't have created a decl in this
1120 // AST if our AST didn't already exist...
1121 if (m_ast_ap.get())
1122 return m_ast_ap->getObjCInterfaceType(decl).getAsOpaquePtr();
1123 return NULL;
1124}
1125
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001126#pragma mark Structure, Unions, Classes
1127
Greg Clayton1be10fc2010-09-29 01:12:09 +00001128clang_type_t
Jim Ingham379397632012-10-27 02:54:13 +00001129ClangASTContext::CreateRecordType (DeclContext *decl_ctx, AccessType access_type, const char *name, int kind, LanguageType language, ClangASTMetadata *metadata)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001130{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001131 ASTContext *ast = getASTContext();
1132 assert (ast != NULL);
Sean Callananad880762012-04-18 01:06:17 +00001133
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001135 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001136
Greg Clayton9e409562010-07-28 02:04:09 +00001137
Greg Claytone1be9962011-08-24 23:50:00 +00001138 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
Greg Clayton9e409562010-07-28 02:04:09 +00001139 {
Greg Claytonaaf99e02010-10-11 02:25:34 +00001140 bool isForwardDecl = true;
Greg Clayton9e409562010-07-28 02:04:09 +00001141 bool isInternal = false;
Sean Callananad880762012-04-18 01:06:17 +00001142 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal, metadata);
Greg Clayton9e409562010-07-28 02:04:09 +00001143 }
1144
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001145 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1146 // we will need to update this code. I was told to currently always use
1147 // the CXXRecordDecl class since we often don't know from debug information
1148 // if something is struct or a class, so we default to always use the more
1149 // complete definition just in case.
Greg Claytonf0705c82011-10-22 03:33:13 +00001150 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1151 (TagDecl::TagKind)kind,
1152 decl_ctx,
1153 SourceLocation(),
1154 SourceLocation(),
1155 name && name[0] ? &ast->Idents.get(name) : NULL);
Sean Callanan7282e2a2012-01-13 22:10:18 +00001156
Jim Ingham379397632012-10-27 02:54:13 +00001157 if (decl && metadata)
1158 SetMetadata(ast, (uintptr_t)decl, *metadata);
Sean Callanan60217122012-04-13 00:10:03 +00001159
Greg Clayton55561e92011-10-26 03:31:36 +00001160 if (decl_ctx)
1161 {
1162 if (access_type != eAccessNone)
1163 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
1164 decl_ctx->addDecl (decl);
1165 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001166 return ast->getTagDeclType(decl).getAsOpaquePtr();
1167}
1168
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001169static TemplateParameterList *
1170CreateTemplateParameterList (ASTContext *ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001171 const ClangASTContext::TemplateParameterInfos &template_param_infos,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001172 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1173{
1174 const bool parameter_pack = false;
1175 const bool is_typename = false;
1176 const unsigned depth = 0;
1177 const size_t num_template_params = template_param_infos.GetSize();
1178 for (size_t i=0; i<num_template_params; ++i)
1179 {
1180 const char *name = template_param_infos.names[i];
Sean Callanan3d654b32012-09-24 22:25:51 +00001181 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001182 {
1183 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1184 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1185 SourceLocation(),
1186 SourceLocation(),
1187 depth,
1188 i,
1189 &ast->Idents.get(name),
1190 template_param_infos.args[i].getIntegralType(),
1191 parameter_pack,
1192 NULL));
1193
1194 }
1195 else
1196 {
1197 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1198 ast->getTranslationUnitDecl(), // Is this the right decl context?
1199 SourceLocation(),
1200 SourceLocation(),
1201 depth,
1202 i,
1203 &ast->Idents.get(name),
1204 is_typename,
1205 parameter_pack));
1206 }
1207 }
1208
1209 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1210 SourceLocation(),
1211 SourceLocation(),
1212 &template_param_decls.front(),
1213 template_param_decls.size(),
1214 SourceLocation());
1215 return template_param_list;
1216}
1217
1218clang::FunctionTemplateDecl *
1219ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1220 clang::FunctionDecl *func_decl,
1221 const char *name,
1222 const TemplateParameterInfos &template_param_infos)
1223{
1224// /// \brief Create a function template node.
1225 ASTContext *ast = getASTContext();
1226
1227 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1228
1229 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1230 template_param_infos,
1231 template_param_decls);
1232 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1233 decl_ctx,
1234 func_decl->getLocation(),
1235 func_decl->getDeclName(),
1236 template_param_list,
1237 func_decl);
1238
1239 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1240 i < template_param_decl_count;
1241 ++i)
1242 {
1243 // TODO: verify which decl context we should put template_param_decls into..
1244 template_param_decls[i]->setDeclContext (func_decl);
1245 }
1246
1247 return func_tmpl_decl;
1248}
1249
1250void
1251ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1252 clang::FunctionTemplateDecl *func_tmpl_decl,
1253 const TemplateParameterInfos &infos)
1254{
1255 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1256 infos.args.data(),
1257 infos.args.size());
1258
1259 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1260 &template_args,
1261 NULL);
1262}
1263
1264
Greg Claytonf0705c82011-10-22 03:33:13 +00001265ClassTemplateDecl *
1266ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001267 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001268 const char *class_name,
1269 int kind,
1270 const TemplateParameterInfos &template_param_infos)
1271{
1272 ASTContext *ast = getASTContext();
1273
1274 ClassTemplateDecl *class_template_decl = NULL;
1275 if (decl_ctx == NULL)
1276 decl_ctx = ast->getTranslationUnitDecl();
1277
1278 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1279 DeclarationName decl_name (&identifier_info);
1280
1281 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001282
1283 for (NamedDecl *decl : result)
Greg Claytonf0705c82011-10-22 03:33:13 +00001284 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001285 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001286 if (class_template_decl)
1287 return class_template_decl;
1288 }
1289
1290 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Claytonf0705c82011-10-22 03:33:13 +00001291
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001292 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1293 template_param_infos,
1294 template_param_decls);
Greg Claytonf0705c82011-10-22 03:33:13 +00001295
1296 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1297 (TagDecl::TagKind)kind,
1298 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1299 SourceLocation(),
1300 SourceLocation(),
1301 &identifier_info);
Greg Claytone04741d2011-12-02 02:09:28 +00001302
1303 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1304 i < template_param_decl_count;
1305 ++i)
1306 {
1307 template_param_decls[i]->setDeclContext (template_cxx_decl);
1308 }
1309
Sean Callananb5c79622011-11-19 01:35:08 +00001310 // With templated classes, we say that a class is templated with
1311 // specializations, but that the bare class has no functions.
Sean Callananfa4fab72013-02-01 06:55:48 +00001312 //template_cxx_decl->startDefinition();
1313 //template_cxx_decl->completeDefinition();
Sean Callananb5c79622011-11-19 01:35:08 +00001314
Greg Claytonf0705c82011-10-22 03:33:13 +00001315 class_template_decl = ClassTemplateDecl::Create (*ast,
1316 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1317 SourceLocation(),
1318 decl_name,
1319 template_param_list,
1320 template_cxx_decl,
1321 NULL);
1322
1323 if (class_template_decl)
Sean Callanan5e9e1992011-10-26 01:06:27 +00001324 {
Greg Clayton55561e92011-10-26 03:31:36 +00001325 if (access_type != eAccessNone)
1326 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Sean Callanan5b26f272012-02-04 08:49:35 +00001327
1328 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1329 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1330
Greg Claytonf0705c82011-10-22 03:33:13 +00001331 decl_ctx->addDecl (class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001332
1333#ifdef LLDB_CONFIGURATION_DEBUG
1334 VerifyDecl(class_template_decl);
1335#endif
1336 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001337
1338 return class_template_decl;
1339}
1340
1341
1342ClassTemplateSpecializationDecl *
1343ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1344 ClassTemplateDecl *class_template_decl,
1345 int kind,
1346 const TemplateParameterInfos &template_param_infos)
1347{
1348 ASTContext *ast = getASTContext();
1349 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1350 (TagDecl::TagKind)kind,
1351 decl_ctx,
1352 SourceLocation(),
1353 SourceLocation(),
1354 class_template_decl,
1355 &template_param_infos.args.front(),
1356 template_param_infos.args.size(),
1357 NULL);
1358
Sean Callananfa4fab72013-02-01 06:55:48 +00001359 class_template_specialization_decl->setSpecializationKind(TSK_ExplicitSpecialization);
1360
Greg Claytonf0705c82011-10-22 03:33:13 +00001361 return class_template_specialization_decl;
1362}
1363
1364lldb::clang_type_t
1365ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1366{
1367 if (class_template_specialization_decl)
1368 {
1369 ASTContext *ast = getASTContext();
1370 if (ast)
1371 return ast->getTagDeclType(class_template_specialization_decl).getAsOpaquePtr();
1372 }
1373 return NULL;
1374}
1375
Greg Clayton6beaaa62011-01-17 03:46:26 +00001376bool
1377ClangASTContext::SetHasExternalStorage (clang_type_t clang_type, bool has_extern)
1378{
1379 if (clang_type == NULL)
1380 return false;
1381
1382 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
1383
1384 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
1385 switch (type_class)
1386 {
1387 case clang::Type::Record:
1388 {
1389 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
1390 if (cxx_record_decl)
1391 {
1392 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001393 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001394 return true;
1395 }
1396 }
1397 break;
1398
1399 case clang::Type::Enum:
1400 {
1401 EnumDecl *enum_decl = cast<EnumType>(qual_type)->getDecl();
1402 if (enum_decl)
1403 {
1404 enum_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001405 enum_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001406 return true;
1407 }
1408 }
1409 break;
1410
1411 case clang::Type::ObjCObject:
1412 case clang::Type::ObjCInterface:
1413 {
Sean Callanan78e37602011-01-27 04:42:51 +00001414 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001415 assert (objc_class_type);
1416 if (objc_class_type)
1417 {
1418 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1419
1420 if (class_interface_decl)
1421 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001422 class_interface_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001423 class_interface_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001424 return true;
1425 }
1426 }
1427 }
1428 break;
1429
1430 case clang::Type::Typedef:
1431 return ClangASTContext::SetHasExternalStorage (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
Sean Callanan912855f2011-08-11 23:56:13 +00001432
1433 case clang::Type::Elaborated:
1434 return ClangASTContext::SetHasExternalStorage (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001435
1436 default:
1437 break;
1438 }
1439 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001440}
1441
Greg Claytona3c444a2010-10-01 23:13:49 +00001442static bool
1443IsOperator (const char *name, OverloadedOperatorKind &op_kind)
1444{
1445 if (name == NULL || name[0] == '\0')
1446 return false;
1447
Sean Callanana43f20d2010-12-10 19:51:54 +00001448#define OPERATOR_PREFIX "operator"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001449#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
Sean Callananbfeff8c2010-12-10 02:15:55 +00001450
1451 const char *post_op_name = NULL;
1452
Sean Callanana43f20d2010-12-10 19:51:54 +00001453 bool no_space = true;
Sean Callananbfeff8c2010-12-10 02:15:55 +00001454
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001455 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
Greg Claytona3c444a2010-10-01 23:13:49 +00001456 return false;
1457
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001458 post_op_name = name + OPERATOR_PREFIX_LENGTH;
1459
Sean Callanana43f20d2010-12-10 19:51:54 +00001460 if (post_op_name[0] == ' ')
1461 {
1462 post_op_name++;
1463 no_space = false;
1464 }
1465
1466#undef OPERATOR_PREFIX
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001467#undef OPERATOR_PREFIX_LENGTH
Sean Callanana43f20d2010-12-10 19:51:54 +00001468
Greg Claytona3c444a2010-10-01 23:13:49 +00001469 // This is an operator, set the overloaded operator kind to invalid
1470 // in case this is a conversion operator...
1471 op_kind = NUM_OVERLOADED_OPERATORS;
1472
1473 switch (post_op_name[0])
1474 {
Sean Callananbfeff8c2010-12-10 02:15:55 +00001475 default:
1476 if (no_space)
1477 return false;
1478 break;
Greg Claytona3c444a2010-10-01 23:13:49 +00001479 case 'n':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001480 if (no_space)
1481 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001482 if (strcmp (post_op_name, "new") == 0)
1483 op_kind = OO_New;
1484 else if (strcmp (post_op_name, "new[]") == 0)
1485 op_kind = OO_Array_New;
1486 break;
1487
1488 case 'd':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001489 if (no_space)
1490 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001491 if (strcmp (post_op_name, "delete") == 0)
1492 op_kind = OO_Delete;
1493 else if (strcmp (post_op_name, "delete[]") == 0)
1494 op_kind = OO_Array_Delete;
1495 break;
1496
1497 case '+':
1498 if (post_op_name[1] == '\0')
1499 op_kind = OO_Plus;
1500 else if (post_op_name[2] == '\0')
1501 {
1502 if (post_op_name[1] == '=')
1503 op_kind = OO_PlusEqual;
1504 else if (post_op_name[1] == '+')
1505 op_kind = OO_PlusPlus;
1506 }
1507 break;
1508
1509 case '-':
1510 if (post_op_name[1] == '\0')
1511 op_kind = OO_Minus;
1512 else if (post_op_name[2] == '\0')
1513 {
1514 switch (post_op_name[1])
1515 {
1516 case '=': op_kind = OO_MinusEqual; break;
1517 case '-': op_kind = OO_MinusMinus; break;
1518 case '>': op_kind = OO_Arrow; break;
1519 }
1520 }
1521 else if (post_op_name[3] == '\0')
1522 {
1523 if (post_op_name[2] == '*')
1524 op_kind = OO_ArrowStar; break;
1525 }
1526 break;
1527
1528 case '*':
1529 if (post_op_name[1] == '\0')
1530 op_kind = OO_Star;
1531 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1532 op_kind = OO_StarEqual;
1533 break;
1534
1535 case '/':
1536 if (post_op_name[1] == '\0')
1537 op_kind = OO_Slash;
1538 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1539 op_kind = OO_SlashEqual;
1540 break;
1541
1542 case '%':
1543 if (post_op_name[1] == '\0')
1544 op_kind = OO_Percent;
1545 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1546 op_kind = OO_PercentEqual;
1547 break;
1548
1549
1550 case '^':
1551 if (post_op_name[1] == '\0')
1552 op_kind = OO_Caret;
1553 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1554 op_kind = OO_CaretEqual;
1555 break;
1556
1557 case '&':
1558 if (post_op_name[1] == '\0')
1559 op_kind = OO_Amp;
1560 else if (post_op_name[2] == '\0')
1561 {
1562 switch (post_op_name[1])
1563 {
1564 case '=': op_kind = OO_AmpEqual; break;
1565 case '&': op_kind = OO_AmpAmp; break;
1566 }
1567 }
1568 break;
1569
1570 case '|':
1571 if (post_op_name[1] == '\0')
1572 op_kind = OO_Pipe;
1573 else if (post_op_name[2] == '\0')
1574 {
1575 switch (post_op_name[1])
1576 {
1577 case '=': op_kind = OO_PipeEqual; break;
1578 case '|': op_kind = OO_PipePipe; break;
1579 }
1580 }
1581 break;
1582
1583 case '~':
1584 if (post_op_name[1] == '\0')
1585 op_kind = OO_Tilde;
1586 break;
1587
1588 case '!':
1589 if (post_op_name[1] == '\0')
1590 op_kind = OO_Exclaim;
1591 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1592 op_kind = OO_ExclaimEqual;
1593 break;
1594
1595 case '=':
1596 if (post_op_name[1] == '\0')
1597 op_kind = OO_Equal;
1598 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1599 op_kind = OO_EqualEqual;
1600 break;
1601
1602 case '<':
1603 if (post_op_name[1] == '\0')
1604 op_kind = OO_Less;
1605 else if (post_op_name[2] == '\0')
1606 {
1607 switch (post_op_name[1])
1608 {
1609 case '<': op_kind = OO_LessLess; break;
1610 case '=': op_kind = OO_LessEqual; break;
1611 }
1612 }
1613 else if (post_op_name[3] == '\0')
1614 {
1615 if (post_op_name[2] == '=')
1616 op_kind = OO_LessLessEqual;
1617 }
1618 break;
1619
1620 case '>':
1621 if (post_op_name[1] == '\0')
1622 op_kind = OO_Greater;
1623 else if (post_op_name[2] == '\0')
1624 {
1625 switch (post_op_name[1])
1626 {
1627 case '>': op_kind = OO_GreaterGreater; break;
1628 case '=': op_kind = OO_GreaterEqual; break;
1629 }
1630 }
1631 else if (post_op_name[1] == '>' &&
1632 post_op_name[2] == '=' &&
1633 post_op_name[3] == '\0')
1634 {
1635 op_kind = OO_GreaterGreaterEqual;
1636 }
1637 break;
1638
1639 case ',':
1640 if (post_op_name[1] == '\0')
1641 op_kind = OO_Comma;
1642 break;
1643
1644 case '(':
1645 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
1646 op_kind = OO_Call;
1647 break;
1648
1649 case '[':
1650 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
1651 op_kind = OO_Subscript;
1652 break;
1653 }
1654
1655 return true;
1656}
Greg Clayton6beaaa62011-01-17 03:46:26 +00001657
Greg Clayton090d0982011-06-19 03:43:27 +00001658static inline bool
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001659check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
Greg Clayton090d0982011-06-19 03:43:27 +00001660{
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001661 // Special-case call since it can take any number of operands
1662 if(op_kind == OO_Call)
1663 return true;
1664
Greg Clayton090d0982011-06-19 03:43:27 +00001665 // The parameter count doens't include "this"
1666 if (num_params == 0)
1667 return unary;
1668 if (num_params == 1)
1669 return binary;
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001670 else
Greg Clayton090d0982011-06-19 03:43:27 +00001671 return false;
1672}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001673
Greg Clayton090d0982011-06-19 03:43:27 +00001674bool
1675ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1676{
Sean Callanan5b26f272012-02-04 08:49:35 +00001677 switch (op_kind)
1678 {
1679 default:
1680 break;
1681 // C++ standard allows any number of arguments to new/delete
1682 case OO_New:
1683 case OO_Array_New:
1684 case OO_Delete:
1685 case OO_Array_Delete:
1686 return true;
1687 }
1688
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001689#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 +00001690 switch (op_kind)
1691 {
1692#include "clang/Basic/OperatorKinds.def"
1693 default: break;
1694 }
1695 return false;
1696}
1697
Greg Claytona51ed9b2010-09-23 01:09:21 +00001698CXXMethodDecl *
Sean Callanan61da09b2010-09-17 02:58:26 +00001699ClangASTContext::AddMethodToCXXRecordType
1700(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001701 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001702 clang_type_t record_opaque_type,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001703 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001704 clang_type_t method_opaque_type,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001705 lldb::AccessType access,
Greg Clayton0fffff52010-09-24 05:15:53 +00001706 bool is_virtual,
1707 bool is_static,
Greg Claytonf51de672010-10-01 02:31:07 +00001708 bool is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00001709 bool is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00001710 bool is_attr_used,
1711 bool is_artificial
Greg Claytona51ed9b2010-09-23 01:09:21 +00001712)
Sean Callanan61da09b2010-09-17 02:58:26 +00001713{
Sean Callananfc55f5d2010-09-21 00:44:12 +00001714 if (!record_opaque_type || !method_opaque_type || !name)
Johnny Chend440bcc2010-09-28 16:10:54 +00001715 return NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001716
Greg Clayton6beaaa62011-01-17 03:46:26 +00001717 assert(ast);
Sean Callanan61da09b2010-09-17 02:58:26 +00001718
Greg Clayton6beaaa62011-01-17 03:46:26 +00001719 IdentifierTable *identifier_table = &ast->Idents;
Sean Callanan61da09b2010-09-17 02:58:26 +00001720
1721 assert(identifier_table);
1722
Sean Callananfc55f5d2010-09-21 00:44:12 +00001723 QualType record_qual_type(QualType::getFromOpaquePtr(record_opaque_type));
Greg Clayton0fffff52010-09-24 05:15:53 +00001724
Greg Clayton6beaaa62011-01-17 03:46:26 +00001725 CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
Sean Callanan61da09b2010-09-17 02:58:26 +00001726
Greg Clayton0fffff52010-09-24 05:15:53 +00001727 if (cxx_record_decl == NULL)
Greg Claytona51ed9b2010-09-23 01:09:21 +00001728 return NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001729
Greg Clayton0fffff52010-09-24 05:15:53 +00001730 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001731
Greg Claytonf51de672010-10-01 02:31:07 +00001732 CXXMethodDecl *cxx_method_decl = NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001733
Greg Claytonf51de672010-10-01 02:31:07 +00001734 DeclarationName decl_name (&identifier_table->get(name));
Greg Clayton878eaf12010-10-01 03:45:20 +00001735
Sean Callanan78e37602011-01-27 04:42:51 +00001736 const clang::FunctionType *function_Type = dyn_cast<FunctionType>(method_qual_type.getTypePtr());
Greg Clayton878eaf12010-10-01 03:45:20 +00001737
Greg Clayton90a2acd2010-10-02 01:40:05 +00001738 if (function_Type == NULL)
Greg Clayton878eaf12010-10-01 03:45:20 +00001739 return NULL;
1740
Sean Callanan78e37602011-01-27 04:42:51 +00001741 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(function_Type));
Greg Clayton878eaf12010-10-01 03:45:20 +00001742
1743 if (!method_function_prototype)
1744 return NULL;
1745
1746 unsigned int num_params = method_function_prototype->getNumArgs();
1747
Sean Callanandbb58392011-11-02 01:38:59 +00001748 CXXDestructorDecl *cxx_dtor_decl(NULL);
1749 CXXConstructorDecl *cxx_ctor_decl(NULL);
1750
Greg Clayton878eaf12010-10-01 03:45:20 +00001751 if (name[0] == '~')
Greg Claytonf51de672010-10-01 02:31:07 +00001752 {
Sean Callanandbb58392011-11-02 01:38:59 +00001753 cxx_dtor_decl = CXXDestructorDecl::Create (*ast,
1754 cxx_record_decl,
1755 SourceLocation(),
1756 DeclarationNameInfo (ast->DeclarationNames.getCXXDestructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1757 method_qual_type,
1758 NULL,
1759 is_inline,
1760 is_artificial);
1761 cxx_method_decl = cxx_dtor_decl;
Greg Clayton878eaf12010-10-01 03:45:20 +00001762 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001763 else if (decl_name == cxx_record_decl->getDeclName())
Greg Clayton878eaf12010-10-01 03:45:20 +00001764 {
Sean Callanandbb58392011-11-02 01:38:59 +00001765 cxx_ctor_decl = CXXConstructorDecl::Create (*ast,
1766 cxx_record_decl,
1767 SourceLocation(),
1768 DeclarationNameInfo (ast->DeclarationNames.getCXXConstructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1769 method_qual_type,
1770 NULL, // TypeSourceInfo *
1771 is_explicit,
1772 is_inline,
1773 is_artificial,
1774 false /*is_constexpr*/);
1775 cxx_method_decl = cxx_ctor_decl;
Greg Claytonf51de672010-10-01 02:31:07 +00001776 }
1777 else
Greg Clayton878eaf12010-10-01 03:45:20 +00001778 {
Greg Claytona3c444a2010-10-01 23:13:49 +00001779
1780 OverloadedOperatorKind op_kind = NUM_OVERLOADED_OPERATORS;
1781 if (IsOperator (name, op_kind))
Greg Clayton878eaf12010-10-01 03:45:20 +00001782 {
Greg Claytona3c444a2010-10-01 23:13:49 +00001783 if (op_kind != NUM_OVERLOADED_OPERATORS)
1784 {
Greg Clayton090d0982011-06-19 03:43:27 +00001785 // Check the number of operator parameters. Sometimes we have
1786 // seen bad DWARF that doesn't correctly describe operators and
1787 // if we try to create a methed and add it to the class, clang
1788 // will assert and crash, so we need to make sure things are
1789 // acceptable.
1790 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
1791 return NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001792 cxx_method_decl = CXXMethodDecl::Create (*ast,
Greg Clayton878eaf12010-10-01 03:45:20 +00001793 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001794 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001795 DeclarationNameInfo (ast->DeclarationNames.getCXXOperatorName (op_kind), SourceLocation()),
Greg Clayton878eaf12010-10-01 03:45:20 +00001796 method_qual_type,
1797 NULL, // TypeSourceInfo *
Greg Claytona3c444a2010-10-01 23:13:49 +00001798 is_static,
1799 SC_None,
Sean Callananfb0b7582011-03-15 00:17:19 +00001800 is_inline,
Sean Callanan880e6802011-10-07 23:18:13 +00001801 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001802 SourceLocation());
Greg Claytona3c444a2010-10-01 23:13:49 +00001803 }
1804 else if (num_params == 0)
1805 {
1806 // Conversion operators don't take params...
Greg Clayton6beaaa62011-01-17 03:46:26 +00001807 cxx_method_decl = CXXConversionDecl::Create (*ast,
Greg Claytona3c444a2010-10-01 23:13:49 +00001808 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001809 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001810 DeclarationNameInfo (ast->DeclarationNames.getCXXConversionFunctionName (ast->getCanonicalType (function_Type->getResultType())), SourceLocation()),
Greg Claytona3c444a2010-10-01 23:13:49 +00001811 method_qual_type,
1812 NULL, // TypeSourceInfo *
1813 is_inline,
Sean Callananfb0b7582011-03-15 00:17:19 +00001814 is_explicit,
Sean Callanan880e6802011-10-07 23:18:13 +00001815 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001816 SourceLocation());
Greg Claytona3c444a2010-10-01 23:13:49 +00001817 }
Greg Clayton878eaf12010-10-01 03:45:20 +00001818 }
Greg Claytona3c444a2010-10-01 23:13:49 +00001819
1820 if (cxx_method_decl == NULL)
Greg Clayton878eaf12010-10-01 03:45:20 +00001821 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001822 cxx_method_decl = CXXMethodDecl::Create (*ast,
Greg Clayton878eaf12010-10-01 03:45:20 +00001823 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001824 SourceLocation(),
Greg Claytona3c444a2010-10-01 23:13:49 +00001825 DeclarationNameInfo (decl_name, SourceLocation()),
Greg Clayton878eaf12010-10-01 03:45:20 +00001826 method_qual_type,
1827 NULL, // TypeSourceInfo *
1828 is_static,
1829 SC_None,
Sean Callananfb0b7582011-03-15 00:17:19 +00001830 is_inline,
Sean Callanan880e6802011-10-07 23:18:13 +00001831 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001832 SourceLocation());
Greg Clayton878eaf12010-10-01 03:45:20 +00001833 }
Greg Claytonf51de672010-10-01 02:31:07 +00001834 }
Greg Claytona3c444a2010-10-01 23:13:49 +00001835
Greg Clayton1be10fc2010-09-29 01:12:09 +00001836 AccessSpecifier access_specifier = ConvertAccessTypeToAccessSpecifier (access);
Greg Clayton0fffff52010-09-24 05:15:53 +00001837
1838 cxx_method_decl->setAccess (access_specifier);
1839 cxx_method_decl->setVirtualAsWritten (is_virtual);
Sean Callanane2ef6e32010-09-23 03:01:22 +00001840
Sean Callananc1b732d2011-11-01 18:07:13 +00001841 if (is_attr_used)
1842 cxx_method_decl->addAttr(::new (*ast) UsedAttr(SourceRange(), *ast));
1843
Sean Callananfc55f5d2010-09-21 00:44:12 +00001844 // Populate the method decl with parameter decls
Sean Callananfc55f5d2010-09-21 00:44:12 +00001845
Charles Davis8c444c42011-05-19 23:33:46 +00001846 llvm::SmallVector<ParmVarDecl *, 12> params;
Sean Callananfc55f5d2010-09-21 00:44:12 +00001847
1848 for (int param_index = 0;
1849 param_index < num_params;
1850 ++param_index)
1851 {
Charles Davis8c444c42011-05-19 23:33:46 +00001852 params.push_back (ParmVarDecl::Create (*ast,
1853 cxx_method_decl,
1854 SourceLocation(),
1855 SourceLocation(),
1856 NULL, // anonymous
1857 method_function_prototype->getArgType(param_index),
1858 NULL,
1859 SC_None,
1860 SC_None,
1861 NULL));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001862 }
1863
Sean Callanan880e6802011-10-07 23:18:13 +00001864 cxx_method_decl->setParams (ArrayRef<ParmVarDecl*>(params));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001865
Greg Clayton0fffff52010-09-24 05:15:53 +00001866 cxx_record_decl->addDecl (cxx_method_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001867
Greg Clayton8b867b42011-11-02 02:06:20 +00001868 // Sometimes the debug info will mention a constructor (default/copy/move),
1869 // destructor, or assignment operator (copy/move) but there won't be any
1870 // version of this in the code. So we check if the function was artificially
1871 // generated and if it is trivial and this lets the compiler/backend know
1872 // that it can inline the IR for these when it needs to and we can avoid a
1873 // "missing function" error when running expressions.
1874
Sean Callanandbb58392011-11-02 01:38:59 +00001875 if (is_artificial)
1876 {
Greg Clayton8b867b42011-11-02 02:06:20 +00001877 if (cxx_ctor_decl &&
1878 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
1879 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
1880 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
Sean Callanandbb58392011-11-02 01:38:59 +00001881 {
1882 cxx_ctor_decl->setDefaulted();
1883 cxx_ctor_decl->setTrivial(true);
1884 }
Greg Clayton8b867b42011-11-02 02:06:20 +00001885 else if (cxx_dtor_decl)
Sean Callanandbb58392011-11-02 01:38:59 +00001886 {
Greg Clayton8b867b42011-11-02 02:06:20 +00001887 if (cxx_record_decl->hasTrivialDestructor())
1888 {
1889 cxx_dtor_decl->setDefaulted();
1890 cxx_dtor_decl->setTrivial(true);
1891 }
1892 }
1893 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
1894 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
1895 {
1896 cxx_method_decl->setDefaulted();
1897 cxx_method_decl->setTrivial(true);
Sean Callanandbb58392011-11-02 01:38:59 +00001898 }
1899 }
1900
Sean Callanan5e9e1992011-10-26 01:06:27 +00001901#ifdef LLDB_CONFIGURATION_DEBUG
1902 VerifyDecl(cxx_method_decl);
1903#endif
Greg Claytonc432c192011-01-20 04:18:48 +00001904
1905// printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
1906// printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
1907// printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
1908// printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
1909// printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
1910// printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
1911// printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
1912// printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
1913// printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
Greg Claytona51ed9b2010-09-23 01:09:21 +00001914 return cxx_method_decl;
Sean Callanan61da09b2010-09-17 02:58:26 +00001915}
1916
Jim Inghame3ae82a2011-11-12 01:36:43 +00001917clang::FieldDecl *
Greg Clayton8cf05932010-07-22 18:30:50 +00001918ClangASTContext::AddFieldToRecordType
1919(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001920 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001921 clang_type_t record_clang_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00001922 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001923 clang_type_t field_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00001924 AccessType access,
1925 uint32_t bitfield_bit_size
1926)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001927{
1928 if (record_clang_type == NULL || field_type == NULL)
Jim Inghame3ae82a2011-11-12 01:36:43 +00001929 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001930
Jim Inghame3ae82a2011-11-12 01:36:43 +00001931 FieldDecl *field = NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001932 IdentifierTable *identifier_table = &ast->Idents;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001933
Greg Clayton6beaaa62011-01-17 03:46:26 +00001934 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001935 assert (identifier_table != NULL);
1936
1937 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
1938
Sean Callanan78e37602011-01-27 04:42:51 +00001939 const clang::Type *clang_type = record_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001940 if (clang_type)
1941 {
1942 const RecordType *record_type = dyn_cast<RecordType>(clang_type);
1943
1944 if (record_type)
1945 {
1946 RecordDecl *record_decl = record_type->getDecl();
1947
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001948 clang::Expr *bit_width = NULL;
1949 if (bitfield_bit_size != 0)
1950 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001951 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
1952 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001953 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001954 field = FieldDecl::Create (*ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001955 record_decl,
1956 SourceLocation(),
1957 SourceLocation(),
1958 name ? &identifier_table->get(name) : NULL, // Identifier
1959 QualType::getFromOpaquePtr(field_type), // Field type
1960 NULL, // TInfo *
1961 bit_width, // BitWidth
1962 false, // Mutable
1963 ICIS_NoInit); // HasInit
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001964
Sean Callanan5ed3ac12012-07-13 20:01:02 +00001965 if (!name) {
1966 // Determine whether this field corresponds to an anonymous
1967 // struct or union.
1968 if (const TagType *TagT = field->getType()->getAs<TagType>()) {
1969 if (RecordDecl *Rec = dyn_cast<RecordDecl>(TagT->getDecl()))
1970 if (!Rec->getDeclName()) {
1971 Rec->setAnonymousStructOrUnion(true);
1972 field->setImplicit();
1973
1974 }
1975 }
1976 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001977
Greg Clayton8cf05932010-07-22 18:30:50 +00001978 field->setAccess (ConvertAccessTypeToAccessSpecifier (access));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001979
1980 if (field)
1981 {
1982 record_decl->addDecl(field);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001983
1984#ifdef LLDB_CONFIGURATION_DEBUG
1985 VerifyDecl(field);
1986#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001987 }
1988 }
Greg Clayton9e409562010-07-28 02:04:09 +00001989 else
1990 {
Sean Callanan78e37602011-01-27 04:42:51 +00001991 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001992 if (objc_class_type)
1993 {
Greg Clayton0fffff52010-09-24 05:15:53 +00001994 bool is_synthesized = false;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001995 field = ClangASTContext::AddObjCClassIVar (ast,
Sean Callanan6e6a7c72010-09-16 20:01:08 +00001996 record_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001997 name,
1998 field_type,
1999 access,
2000 bitfield_bit_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00002001 is_synthesized);
Greg Clayton9e409562010-07-28 02:04:09 +00002002 }
2003 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002004 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002005 return field;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002006}
2007
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002008static clang::AccessSpecifier UnifyAccessSpecifiers (clang::AccessSpecifier lhs,
2009 clang::AccessSpecifier rhs)
2010{
2011 clang::AccessSpecifier ret = lhs;
2012
2013 // Make the access equal to the stricter of the field and the nested field's access
2014 switch (ret)
2015 {
2016 case clang::AS_none:
2017 break;
2018 case clang::AS_private:
2019 break;
2020 case clang::AS_protected:
2021 if (rhs == AS_private)
2022 ret = AS_private;
2023 break;
2024 case clang::AS_public:
2025 ret = rhs;
2026 break;
2027 }
2028
2029 return ret;
2030}
2031
2032void
2033ClangASTContext::BuildIndirectFields (clang::ASTContext *ast,
2034 lldb::clang_type_t record_clang_type)
2035{
2036 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
2037
2038 const RecordType *record_type = record_qual_type->getAs<RecordType>();
2039
2040 if (!record_type)
2041 return;
2042
2043 RecordDecl *record_decl = record_type->getDecl();
2044
2045 if (!record_decl)
2046 return;
2047
2048 typedef llvm::SmallVector <IndirectFieldDecl *, 1> IndirectFieldVector;
2049
2050 IndirectFieldVector indirect_fields;
Greg Clayton4ef877f2012-12-06 02:33:54 +00002051 RecordDecl::field_iterator field_pos;
2052 RecordDecl::field_iterator field_end_pos = record_decl->field_end();
2053 RecordDecl::field_iterator last_field_pos = field_end_pos;
2054 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; last_field_pos = field_pos++)
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002055 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00002056 if (field_pos->isAnonymousStructOrUnion())
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002057 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00002058 QualType field_qual_type = field_pos->getType();
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002059
2060 const RecordType *field_record_type = field_qual_type->getAs<RecordType>();
2061
2062 if (!field_record_type)
2063 continue;
2064
2065 RecordDecl *field_record_decl = field_record_type->getDecl();
2066
2067 if (!field_record_decl)
2068 continue;
2069
2070 for (RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
2071 di != de;
2072 ++di)
2073 {
2074 if (FieldDecl *nested_field_decl = dyn_cast<FieldDecl>(*di))
2075 {
2076 NamedDecl **chain = new (*ast) NamedDecl*[2];
Greg Clayton4ef877f2012-12-06 02:33:54 +00002077 chain[0] = *field_pos;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002078 chain[1] = nested_field_decl;
2079 IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*ast,
2080 record_decl,
2081 SourceLocation(),
2082 nested_field_decl->getIdentifier(),
2083 nested_field_decl->getType(),
2084 chain,
2085 2);
2086
Greg Clayton4ef877f2012-12-06 02:33:54 +00002087 indirect_field->setAccess(UnifyAccessSpecifiers(field_pos->getAccess(),
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002088 nested_field_decl->getAccess()));
2089
2090 indirect_fields.push_back(indirect_field);
2091 }
2092 else if (IndirectFieldDecl *nested_indirect_field_decl = dyn_cast<IndirectFieldDecl>(*di))
2093 {
2094 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
2095 NamedDecl **chain = new (*ast) NamedDecl*[nested_chain_size + 1];
Greg Clayton4ef877f2012-12-06 02:33:54 +00002096 chain[0] = *field_pos;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002097
2098 int chain_index = 1;
2099 for (IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
2100 nce = nested_indirect_field_decl->chain_end();
2101 nci < nce;
2102 ++nci)
2103 {
2104 chain[chain_index] = *nci;
2105 chain_index++;
2106 }
2107
2108 IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*ast,
2109 record_decl,
2110 SourceLocation(),
2111 nested_indirect_field_decl->getIdentifier(),
2112 nested_indirect_field_decl->getType(),
2113 chain,
2114 nested_chain_size + 1);
2115
Greg Clayton4ef877f2012-12-06 02:33:54 +00002116 indirect_field->setAccess(UnifyAccessSpecifiers(field_pos->getAccess(),
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002117 nested_indirect_field_decl->getAccess()));
2118
2119 indirect_fields.push_back(indirect_field);
2120 }
2121 }
2122 }
2123 }
2124
Greg Clayton4ef877f2012-12-06 02:33:54 +00002125 // Check the last field to see if it has an incomplete array type as its
2126 // last member and if it does, the tell the record decl about it
2127 if (last_field_pos != field_end_pos)
2128 {
2129 if (last_field_pos->getType()->isIncompleteArrayType())
2130 record_decl->hasFlexibleArrayMember();
2131 }
2132
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002133 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
2134 ifi < ife;
2135 ++ifi)
2136 {
2137 record_decl->addDecl(*ifi);
2138 }
2139}
2140
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002141bool
2142ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
2143{
2144 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
2145}
2146
2147bool
2148ClangASTContext::FieldIsBitfield
2149(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002150 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002151 FieldDecl* field,
2152 uint32_t& bitfield_bit_size
2153)
2154{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002155 if (ast == NULL || field == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002156 return false;
2157
2158 if (field->isBitField())
2159 {
2160 Expr* bit_width_expr = field->getBitWidth();
2161 if (bit_width_expr)
2162 {
2163 llvm::APSInt bit_width_apsint;
Greg Clayton6beaaa62011-01-17 03:46:26 +00002164 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002165 {
2166 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
2167 return true;
2168 }
2169 }
2170 }
2171 return false;
2172}
2173
2174bool
2175ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
2176{
2177 if (record_decl == NULL)
2178 return false;
2179
2180 if (!record_decl->field_empty())
2181 return true;
2182
2183 // No fields, lets check this is a CXX record and check the base classes
2184 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2185 if (cxx_record_decl)
2186 {
2187 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2188 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2189 base_class != base_class_end;
2190 ++base_class)
2191 {
2192 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2193 if (RecordHasFields(base_class_decl))
2194 return true;
2195 }
2196 }
2197 return false;
2198}
2199
2200void
Greg Clayton6beaaa62011-01-17 03:46:26 +00002201ClangASTContext::SetDefaultAccessForRecordFields (clang_type_t clang_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002202{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002203 if (clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002204 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002205 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
2206
Sean Callanan78e37602011-01-27 04:42:51 +00002207 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +00002208 if (record_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002209 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002210 RecordDecl *record_decl = record_type->getDecl();
2211 if (record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002212 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002213 uint32_t field_idx;
2214 RecordDecl::field_iterator field, field_end;
2215 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2216 field != field_end;
2217 ++field, ++field_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002218 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002219 // If no accessibility was assigned, assign the correct one
2220 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2221 field->setAccess ((AccessSpecifier)default_accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002222 }
2223 }
2224 }
2225 }
2226}
2227
2228#pragma mark C++ Base Classes
2229
2230CXXBaseSpecifier *
Greg Clayton1be10fc2010-09-29 01:12:09 +00002231ClangASTContext::CreateBaseClassSpecifier (clang_type_t base_class_type, AccessType access, bool is_virtual, bool base_of_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002232{
2233 if (base_class_type)
Greg Claytone6371122010-07-30 20:30:44 +00002234 return new CXXBaseSpecifier (SourceRange(),
2235 is_virtual,
2236 base_of_class,
2237 ConvertAccessTypeToAccessSpecifier (access),
Sean Callanan2c777c42011-01-18 23:32:05 +00002238 getASTContext()->CreateTypeSourceInfo (QualType::getFromOpaquePtr(base_class_type)),
2239 SourceLocation());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002240 return NULL;
2241}
2242
Greg Clayton0b42ac32010-07-02 01:29:13 +00002243void
2244ClangASTContext::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes)
2245{
2246 for (unsigned i=0; i<num_base_classes; ++i)
2247 {
2248 delete base_classes[i];
2249 base_classes[i] = NULL;
2250 }
2251}
2252
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002253bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002254ClangASTContext::SetBaseClassesForClassType (clang_type_t class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002255{
2256 if (class_clang_type)
2257 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002258 CXXRecordDecl *cxx_record_decl = QualType::getFromOpaquePtr(class_clang_type)->getAsCXXRecordDecl();
2259 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002260 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002261 cxx_record_decl->setBases(base_classes, num_base_classes);
2262 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002263 }
2264 }
2265 return false;
2266}
Greg Clayton8cf05932010-07-22 18:30:50 +00002267#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002268
Greg Clayton1be10fc2010-09-29 01:12:09 +00002269clang_type_t
Sean Callananad880762012-04-18 01:06:17 +00002270ClangASTContext::CreateObjCClass
Greg Clayton8cf05932010-07-22 18:30:50 +00002271(
2272 const char *name,
2273 DeclContext *decl_ctx,
2274 bool isForwardDecl,
Sean Callananad880762012-04-18 01:06:17 +00002275 bool isInternal,
Jim Ingham379397632012-10-27 02:54:13 +00002276 ClangASTMetadata *metadata
Greg Clayton8cf05932010-07-22 18:30:50 +00002277)
2278{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002279 ASTContext *ast = getASTContext();
2280 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00002281 assert (name && name[0]);
2282 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00002283 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00002284
2285 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
2286 // we will need to update this code. I was told to currently always use
2287 // the CXXRecordDecl class since we often don't know from debug information
2288 // if something is struct or a class, so we default to always use the more
2289 // complete definition just in case.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002290 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00002291 decl_ctx,
2292 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00002293 &ast->Idents.get(name),
Sean Callanan5b26f272012-02-04 08:49:35 +00002294 NULL,
Greg Clayton8cf05932010-07-22 18:30:50 +00002295 SourceLocation(),
Sean Callanan5b26f272012-02-04 08:49:35 +00002296 /*isForwardDecl,*/
Greg Clayton8cf05932010-07-22 18:30:50 +00002297 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00002298
Jim Ingham379397632012-10-27 02:54:13 +00002299 if (decl && metadata)
2300 SetMetadata(ast, (uintptr_t)decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00002301
Greg Clayton6beaaa62011-01-17 03:46:26 +00002302 return ast->getObjCInterfaceType(decl).getAsOpaquePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002303}
2304
2305bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002306ClangASTContext::SetObjCSuperClass (clang_type_t class_opaque_type, clang_type_t super_opaque_type)
Greg Clayton8cf05932010-07-22 18:30:50 +00002307{
2308 if (class_opaque_type && super_opaque_type)
2309 {
2310 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2311 QualType super_qual_type(QualType::getFromOpaquePtr(super_opaque_type));
Sean Callanan78e37602011-01-27 04:42:51 +00002312 const clang::Type *class_type = class_qual_type.getTypePtr();
2313 const clang::Type *super_type = super_qual_type.getTypePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002314 if (class_type && super_type)
2315 {
Sean Callanan78e37602011-01-27 04:42:51 +00002316 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
2317 const ObjCObjectType *objc_super_type = dyn_cast<ObjCObjectType>(super_type);
Greg Clayton8cf05932010-07-22 18:30:50 +00002318 if (objc_class_type && objc_super_type)
2319 {
2320 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2321 ObjCInterfaceDecl *super_interface_decl = objc_super_type->getInterface();
2322 if (class_interface_decl && super_interface_decl)
2323 {
2324 class_interface_decl->setSuperClass(super_interface_decl);
2325 return true;
2326 }
2327 }
2328 }
2329 }
2330 return false;
2331}
2332
2333
Jim Inghame3ae82a2011-11-12 01:36:43 +00002334FieldDecl *
Greg Clayton8cf05932010-07-22 18:30:50 +00002335ClangASTContext::AddObjCClassIVar
2336(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002337 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002338 clang_type_t class_opaque_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00002339 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002340 clang_type_t ivar_opaque_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00002341 AccessType access,
2342 uint32_t bitfield_bit_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00002343 bool is_synthesized
Greg Clayton8cf05932010-07-22 18:30:50 +00002344)
2345{
2346 if (class_opaque_type == NULL || ivar_opaque_type == NULL)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002347 return NULL;
Greg Clayton8cf05932010-07-22 18:30:50 +00002348
Jim Inghame3ae82a2011-11-12 01:36:43 +00002349 ObjCIvarDecl *field = NULL;
2350
Greg Clayton6beaaa62011-01-17 03:46:26 +00002351 IdentifierTable *identifier_table = &ast->Idents;
Greg Clayton8cf05932010-07-22 18:30:50 +00002352
Greg Clayton6beaaa62011-01-17 03:46:26 +00002353 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00002354 assert (identifier_table != NULL);
2355
2356 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2357
Sean Callanan78e37602011-01-27 04:42:51 +00002358 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002359 if (class_type)
2360 {
Sean Callanan78e37602011-01-27 04:42:51 +00002361 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton8cf05932010-07-22 18:30:50 +00002362
2363 if (objc_class_type)
2364 {
2365 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2366
2367 if (class_interface_decl)
2368 {
2369 clang::Expr *bit_width = NULL;
2370 if (bitfield_bit_size != 0)
2371 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002372 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
2373 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
Greg Clayton8cf05932010-07-22 18:30:50 +00002374 }
2375
Jim Inghame3ae82a2011-11-12 01:36:43 +00002376 field = ObjCIvarDecl::Create (*ast,
2377 class_interface_decl,
2378 SourceLocation(),
2379 SourceLocation(),
Greg Clayton88bc7f32012-11-06 00:20:41 +00002380 name ? &identifier_table->get(name) : NULL, // Identifier
Jim Inghame3ae82a2011-11-12 01:36:43 +00002381 QualType::getFromOpaquePtr(ivar_opaque_type), // Field type
2382 NULL, // TypeSourceInfo *
2383 ConvertAccessTypeToObjCIvarAccessControl (access),
2384 bit_width,
2385 is_synthesized);
Greg Clayton9e409562010-07-28 02:04:09 +00002386
2387 if (field)
2388 {
2389 class_interface_decl->addDecl(field);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002390
2391#ifdef LLDB_CONFIGURATION_DEBUG
2392 VerifyDecl(field);
2393#endif
2394
Jim Inghame3ae82a2011-11-12 01:36:43 +00002395 return field;
Greg Clayton9e409562010-07-28 02:04:09 +00002396 }
Greg Clayton8cf05932010-07-22 18:30:50 +00002397 }
2398 }
2399 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002400 return NULL;
2401}
2402
2403bool
2404ClangASTContext::AddObjCClassProperty
2405(
2406 ASTContext *ast,
2407 clang_type_t class_opaque_type,
2408 const char *property_name,
2409 clang_type_t property_opaque_type,
2410 ObjCIvarDecl *ivar_decl,
2411 const char *property_setter_name,
2412 const char *property_getter_name,
Sean Callananad880762012-04-18 01:06:17 +00002413 uint32_t property_attributes,
Jim Ingham379397632012-10-27 02:54:13 +00002414 ClangASTMetadata *metadata
Jim Inghame3ae82a2011-11-12 01:36:43 +00002415)
2416{
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002417 if (class_opaque_type == NULL || property_name == NULL || property_name[0] == '\0')
Jim Inghame3ae82a2011-11-12 01:36:43 +00002418 return false;
2419
2420 IdentifierTable *identifier_table = &ast->Idents;
2421
2422 assert (ast != NULL);
2423 assert (identifier_table != NULL);
2424
2425 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2426 const clang::Type *class_type = class_qual_type.getTypePtr();
2427 if (class_type)
2428 {
2429 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
2430
2431 if (objc_class_type)
2432 {
2433 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2434
Greg Clayton23f59502012-07-17 03:23:13 +00002435 clang_type_t property_opaque_type_to_access = NULL;
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002436
2437 if (property_opaque_type)
2438 property_opaque_type_to_access = property_opaque_type;
2439 else if (ivar_decl)
2440 property_opaque_type_to_access = ivar_decl->getType().getAsOpaquePtr();
2441
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002442 if (class_interface_decl && property_opaque_type_to_access)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002443 {
2444 clang::TypeSourceInfo *prop_type_source;
2445 if (ivar_decl)
2446 prop_type_source = ast->CreateTypeSourceInfo (ivar_decl->getType());
2447 else
2448 prop_type_source = ast->CreateTypeSourceInfo (QualType::getFromOpaquePtr(property_opaque_type));
2449
2450 ObjCPropertyDecl *property_decl = ObjCPropertyDecl::Create(*ast,
2451 class_interface_decl,
2452 SourceLocation(), // Source Location
2453 &identifier_table->get(property_name),
2454 SourceLocation(), //Source Location for AT
Sean Callanand5f33a82012-03-01 02:03:47 +00002455 SourceLocation(), //Source location for (
Jim Inghame3ae82a2011-11-12 01:36:43 +00002456 prop_type_source
2457 );
Sean Callananad880762012-04-18 01:06:17 +00002458
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002459 if (property_decl)
2460 {
Jim Ingham379397632012-10-27 02:54:13 +00002461 if (metadata)
2462 SetMetadata(ast, (uintptr_t)property_decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00002463
Jim Inghame3ae82a2011-11-12 01:36:43 +00002464 class_interface_decl->addDecl (property_decl);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002465
2466 Selector setter_sel, getter_sel;
2467
Jim Inghame3ae82a2011-11-12 01:36:43 +00002468 if (property_setter_name != NULL)
2469 {
2470 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
2471 clang::IdentifierInfo *setter_ident = &identifier_table->get(property_setter_no_colon.c_str());
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002472 setter_sel = ast->Selectors.getSelector(1, &setter_ident);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002473 }
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002474 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
2475 {
2476 std::string setter_sel_string("set");
2477 setter_sel_string.push_back(::toupper(property_name[0]));
2478 setter_sel_string.append(&property_name[1]);
2479 clang::IdentifierInfo *setter_ident = &identifier_table->get(setter_sel_string.c_str());
2480 setter_sel = ast->Selectors.getSelector(1, &setter_ident);
2481 }
Sean Callanana6582262012-04-05 00:12:52 +00002482 property_decl->setSetterName(setter_sel);
2483 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002484
2485 if (property_getter_name != NULL)
2486 {
2487 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_getter_name);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002488 getter_sel = ast->Selectors.getSelector(0, &getter_ident);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002489 }
2490 else
2491 {
2492 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_name);
2493 getter_sel = ast->Selectors.getSelector(0, &getter_ident);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002494 }
Sean Callanana6582262012-04-05 00:12:52 +00002495 property_decl->setGetterName(getter_sel);
2496 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002497
2498 if (ivar_decl)
2499 property_decl->setPropertyIvarDecl (ivar_decl);
2500
2501 if (property_attributes & DW_APPLE_PROPERTY_readonly)
2502 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
2503 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
2504 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
2505 if (property_attributes & DW_APPLE_PROPERTY_assign)
2506 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
2507 if (property_attributes & DW_APPLE_PROPERTY_retain)
2508 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
2509 if (property_attributes & DW_APPLE_PROPERTY_copy)
2510 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
2511 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
2512 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002513
2514 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
2515 {
2516 QualType result_type = QualType::getFromOpaquePtr(property_opaque_type_to_access);
2517
2518 const bool isInstance = true;
2519 const bool isVariadic = false;
2520 const bool isSynthesized = false;
2521 const bool isImplicitlyDeclared = true;
2522 const bool isDefined = false;
2523 const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
2524 const bool HasRelatedResultType = false;
2525
2526 ObjCMethodDecl *getter = ObjCMethodDecl::Create(*ast,
2527 SourceLocation(),
2528 SourceLocation(),
2529 getter_sel,
2530 result_type,
2531 NULL,
2532 class_interface_decl,
2533 isInstance,
2534 isVariadic,
2535 isSynthesized,
2536 isImplicitlyDeclared,
2537 isDefined,
2538 impControl,
2539 HasRelatedResultType);
Sean Callananad880762012-04-18 01:06:17 +00002540
Jim Ingham379397632012-10-27 02:54:13 +00002541 if (getter && metadata)
2542 SetMetadata(ast, (uintptr_t)getter, *metadata);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002543
2544 getter->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(), ArrayRef<SourceLocation>());
2545
2546 class_interface_decl->addDecl(getter);
2547 }
2548
2549 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
2550 {
2551 QualType result_type = ast->VoidTy;
2552
2553 const bool isInstance = true;
2554 const bool isVariadic = false;
2555 const bool isSynthesized = false;
2556 const bool isImplicitlyDeclared = true;
2557 const bool isDefined = false;
2558 const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
2559 const bool HasRelatedResultType = false;
2560
2561 ObjCMethodDecl *setter = ObjCMethodDecl::Create(*ast,
2562 SourceLocation(),
2563 SourceLocation(),
2564 setter_sel,
2565 result_type,
2566 NULL,
2567 class_interface_decl,
2568 isInstance,
2569 isVariadic,
2570 isSynthesized,
2571 isImplicitlyDeclared,
2572 isDefined,
2573 impControl,
2574 HasRelatedResultType);
2575
Jim Ingham379397632012-10-27 02:54:13 +00002576 if (setter && metadata)
2577 SetMetadata(ast, (uintptr_t)setter, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00002578
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002579 llvm::SmallVector<ParmVarDecl *, 1> params;
2580
2581 params.push_back (ParmVarDecl::Create (*ast,
2582 setter,
2583 SourceLocation(),
2584 SourceLocation(),
2585 NULL, // anonymous
2586 QualType::getFromOpaquePtr(property_opaque_type_to_access),
2587 NULL,
2588 SC_Auto,
2589 SC_Auto,
2590 NULL));
2591
2592 setter->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
2593
2594 class_interface_decl->addDecl(setter);
2595 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002596
2597 return true;
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002598 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002599 }
2600 }
2601 }
Greg Clayton8cf05932010-07-22 18:30:50 +00002602 return false;
2603}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002604
Greg Clayton9e409562010-07-28 02:04:09 +00002605bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002606ClangASTContext::ObjCTypeHasIVars (clang_type_t class_opaque_type, bool check_superclass)
Greg Clayton9e409562010-07-28 02:04:09 +00002607{
2608 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2609
Sean Callanan78e37602011-01-27 04:42:51 +00002610 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton9e409562010-07-28 02:04:09 +00002611 if (class_type)
2612 {
Sean Callanan78e37602011-01-27 04:42:51 +00002613 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00002614
2615 if (objc_class_type)
2616 return ObjCDeclHasIVars (objc_class_type->getInterface(), check_superclass);
2617 }
2618 return false;
2619}
2620
2621bool
2622ClangASTContext::ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
2623{
2624 while (class_interface_decl)
2625 {
2626 if (class_interface_decl->ivar_size() > 0)
2627 return true;
2628
2629 if (check_superclass)
2630 class_interface_decl = class_interface_decl->getSuperClass();
2631 else
2632 break;
2633 }
2634 return false;
2635}
Greg Clayton0fffff52010-09-24 05:15:53 +00002636
Greg Clayton1be10fc2010-09-29 01:12:09 +00002637ObjCMethodDecl *
Greg Clayton0fffff52010-09-24 05:15:53 +00002638ClangASTContext::AddMethodToObjCObjectType
2639(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002640 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002641 clang_type_t class_opaque_type,
Greg Clayton0fffff52010-09-24 05:15:53 +00002642 const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
Greg Clayton1be10fc2010-09-29 01:12:09 +00002643 clang_type_t method_opaque_type,
Greg Clayton0fffff52010-09-24 05:15:53 +00002644 lldb::AccessType access
2645)
2646{
2647 if (class_opaque_type == NULL || method_opaque_type == NULL)
2648 return NULL;
2649
Greg Clayton6beaaa62011-01-17 03:46:26 +00002650 IdentifierTable *identifier_table = &ast->Idents;
Greg Clayton0fffff52010-09-24 05:15:53 +00002651
Greg Clayton6beaaa62011-01-17 03:46:26 +00002652 assert (ast != NULL);
Greg Clayton0fffff52010-09-24 05:15:53 +00002653 assert (identifier_table != NULL);
2654
2655 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2656
Sean Callanan78e37602011-01-27 04:42:51 +00002657 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton0fffff52010-09-24 05:15:53 +00002658 if (class_type == NULL)
2659 return NULL;
2660
Sean Callanan78e37602011-01-27 04:42:51 +00002661 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton0fffff52010-09-24 05:15:53 +00002662
2663 if (objc_class_type == NULL)
2664 return NULL;
2665
2666 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2667
2668 if (class_interface_decl == NULL)
2669 return NULL;
Greg Clayton9e409562010-07-28 02:04:09 +00002670
Greg Clayton0fffff52010-09-24 05:15:53 +00002671 const char *selector_start = ::strchr (name, ' ');
2672 if (selector_start == NULL)
2673 return NULL;
2674
2675 selector_start++;
2676 if (!(::isalpha (selector_start[0]) || selector_start[0] == '_'))
2677 return NULL;
2678 llvm::SmallVector<IdentifierInfo *, 12> selector_idents;
2679
Greg Clayton450e3f32010-10-12 02:24:53 +00002680 size_t len = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +00002681 const char *start;
Greg Clayton450e3f32010-10-12 02:24:53 +00002682 //printf ("name = '%s'\n", name);
2683
2684 unsigned num_selectors_with_args = 0;
2685 for (start = selector_start;
Greg Clayton0fffff52010-09-24 05:15:53 +00002686 start && *start != '\0' && *start != ']';
Greg Clayton450e3f32010-10-12 02:24:53 +00002687 start += len)
Greg Clayton0fffff52010-09-24 05:15:53 +00002688 {
Greg Clayton450e3f32010-10-12 02:24:53 +00002689 len = ::strcspn(start, ":]");
Greg Clayton90f90cd2010-10-27 04:01:14 +00002690 bool has_arg = (start[len] == ':');
2691 if (has_arg)
Greg Clayton450e3f32010-10-12 02:24:53 +00002692 ++num_selectors_with_args;
Greg Clayton0fffff52010-09-24 05:15:53 +00002693 selector_idents.push_back (&identifier_table->get (StringRef (start, len)));
Greg Clayton90f90cd2010-10-27 04:01:14 +00002694 if (has_arg)
2695 len += 1;
Greg Clayton0fffff52010-09-24 05:15:53 +00002696 }
2697
2698
2699 if (selector_idents.size() == 0)
2700 return 0;
2701
Greg Clayton6beaaa62011-01-17 03:46:26 +00002702 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
Greg Clayton0fffff52010-09-24 05:15:53 +00002703 selector_idents.data());
2704
2705 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
2706
2707 // Populate the method decl with parameter decls
Sean Callanan78e37602011-01-27 04:42:51 +00002708 const clang::Type *method_type(method_qual_type.getTypePtr());
Greg Clayton0fffff52010-09-24 05:15:53 +00002709
2710 if (method_type == NULL)
2711 return NULL;
2712
Sean Callanan78e37602011-01-27 04:42:51 +00002713 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type));
Greg Clayton0fffff52010-09-24 05:15:53 +00002714
2715 if (!method_function_prototype)
2716 return NULL;
2717
2718
2719 bool is_variadic = false;
2720 bool is_synthesized = false;
2721 bool is_defined = false;
2722 ObjCMethodDecl::ImplementationControl imp_control = ObjCMethodDecl::None;
2723
2724 const unsigned num_args = method_function_prototype->getNumArgs();
Sean Callananc3a1d562013-02-06 23:21:59 +00002725
2726 if (num_args != num_selectors_with_args)
2727 return NULL; // some debug information is corrupt. We are not going to deal with it.
Greg Clayton0fffff52010-09-24 05:15:53 +00002728
Greg Clayton6beaaa62011-01-17 03:46:26 +00002729 ObjCMethodDecl *objc_method_decl = ObjCMethodDecl::Create (*ast,
Greg Clayton0fffff52010-09-24 05:15:53 +00002730 SourceLocation(), // beginLoc,
2731 SourceLocation(), // endLoc,
2732 method_selector,
2733 method_function_prototype->getResultType(),
2734 NULL, // TypeSourceInfo *ResultTInfo,
2735 GetDeclContextForType (class_opaque_type),
2736 name[0] == '-',
2737 is_variadic,
2738 is_synthesized,
Sean Callanan880e6802011-10-07 23:18:13 +00002739 true, // is_implicitly_declared
Greg Clayton0fffff52010-09-24 05:15:53 +00002740 is_defined,
2741 imp_control,
Sean Callanan880e6802011-10-07 23:18:13 +00002742 false /*has_related_result_type*/);
Greg Clayton0fffff52010-09-24 05:15:53 +00002743
2744
2745 if (objc_method_decl == NULL)
2746 return NULL;
2747
2748 if (num_args > 0)
2749 {
2750 llvm::SmallVector<ParmVarDecl *, 12> params;
2751
2752 for (int param_index = 0; param_index < num_args; ++param_index)
2753 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002754 params.push_back (ParmVarDecl::Create (*ast,
Greg Clayton0fffff52010-09-24 05:15:53 +00002755 objc_method_decl,
2756 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00002757 SourceLocation(),
Greg Clayton0fffff52010-09-24 05:15:53 +00002758 NULL, // anonymous
2759 method_function_prototype->getArgType(param_index),
2760 NULL,
2761 SC_Auto,
2762 SC_Auto,
2763 NULL));
2764 }
2765
Sean Callanan880e6802011-10-07 23:18:13 +00002766 objc_method_decl->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
Greg Clayton0fffff52010-09-24 05:15:53 +00002767 }
2768
2769 class_interface_decl->addDecl (objc_method_decl);
2770
Sean Callanan5e9e1992011-10-26 01:06:27 +00002771#ifdef LLDB_CONFIGURATION_DEBUG
2772 VerifyDecl(objc_method_decl);
2773#endif
Greg Clayton0fffff52010-09-24 05:15:53 +00002774
2775 return objc_method_decl;
2776}
2777
Greg Clayton402230e2012-02-03 01:30:30 +00002778size_t
2779ClangASTContext::GetNumTemplateArguments (clang::ASTContext *ast, clang_type_t clang_type)
2780{
2781 if (clang_type)
2782 {
2783 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2784
2785 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2786 switch (type_class)
2787 {
2788 case clang::Type::Record:
2789 if (GetCompleteQualType (ast, qual_type))
2790 {
2791 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2792 if (cxx_record_decl)
2793 {
2794 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2795 if (template_decl)
2796 return template_decl->getTemplateArgs().size();
2797 }
2798 }
2799 break;
2800
2801 case clang::Type::Typedef:
2802 return ClangASTContext::GetNumTemplateArguments (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002803
2804 case clang::Type::Elaborated:
2805 return ClangASTContext::GetNumTemplateArguments (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2806
Greg Clayton402230e2012-02-03 01:30:30 +00002807 default:
2808 break;
2809 }
2810 }
2811 return 0;
2812}
2813
2814clang_type_t
2815ClangASTContext::GetTemplateArgument (clang::ASTContext *ast, clang_type_t clang_type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
2816{
2817 if (clang_type)
2818 {
2819 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2820
2821 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2822 switch (type_class)
2823 {
2824 case clang::Type::Record:
2825 if (GetCompleteQualType (ast, qual_type))
2826 {
2827 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2828 if (cxx_record_decl)
2829 {
2830 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2831 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
2832 {
2833 const TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
2834 switch (template_arg.getKind())
2835 {
2836 case clang::TemplateArgument::Null:
2837 kind = eTemplateArgumentKindNull;
2838 return NULL;
2839
2840 case clang::TemplateArgument::Type:
2841 kind = eTemplateArgumentKindType;
2842 return template_arg.getAsType().getAsOpaquePtr();
2843
2844 case clang::TemplateArgument::Declaration:
2845 kind = eTemplateArgumentKindDeclaration;
2846 return NULL;
2847
2848 case clang::TemplateArgument::Integral:
2849 kind = eTemplateArgumentKindIntegral;
2850 return template_arg.getIntegralType().getAsOpaquePtr();
2851
2852 case clang::TemplateArgument::Template:
2853 kind = eTemplateArgumentKindTemplate;
2854 return NULL;
2855
2856 case clang::TemplateArgument::TemplateExpansion:
2857 kind = eTemplateArgumentKindTemplateExpansion;
2858 return NULL;
2859
2860 case clang::TemplateArgument::Expression:
2861 kind = eTemplateArgumentKindExpression;
2862 return NULL;
2863
2864 case clang::TemplateArgument::Pack:
2865 kind = eTemplateArgumentKindPack;
2866 return NULL;
2867
2868 default:
2869 assert (!"Unhandled TemplateArgument::ArgKind");
2870 kind = eTemplateArgumentKindNull;
2871 return NULL;
2872 }
2873 }
2874 }
2875 }
2876 break;
2877
2878 case clang::Type::Typedef:
2879 return ClangASTContext::GetTemplateArgument (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), arg_idx, kind);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002880
2881 case clang::Type::Elaborated:
2882 return ClangASTContext::GetTemplateArgument (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), arg_idx, kind);
2883
Greg Clayton402230e2012-02-03 01:30:30 +00002884 default:
2885 break;
2886 }
2887 }
2888 kind = eTemplateArgumentKindNull;
2889 return NULL;
2890}
Greg Clayton0fffff52010-09-24 05:15:53 +00002891
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002892uint32_t
Greg Clayton73b472d2010-10-27 03:32:59 +00002893ClangASTContext::GetTypeInfo
2894(
2895 clang_type_t clang_type,
Greg Clayton6beaaa62011-01-17 03:46:26 +00002896 clang::ASTContext *ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00002897 clang_type_t *pointee_or_element_clang_type
2898)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002899{
2900 if (clang_type == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00002901 return 0;
2902
2903 if (pointee_or_element_clang_type)
2904 *pointee_or_element_clang_type = NULL;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002905
2906 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2907
2908 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2909 switch (type_class)
2910 {
Sean Callanana2424172010-10-25 00:29:48 +00002911 case clang::Type::Builtin:
2912 switch (cast<clang::BuiltinType>(qual_type)->getKind())
2913 {
Sean Callanana2424172010-10-25 00:29:48 +00002914 case clang::BuiltinType::ObjCId:
2915 case clang::BuiltinType::ObjCClass:
Greg Clayton6beaaa62011-01-17 03:46:26 +00002916 if (ast && pointee_or_element_clang_type)
2917 *pointee_or_element_clang_type = ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Sean Callanana2424172010-10-25 00:29:48 +00002918 return eTypeIsBuiltIn | eTypeIsPointer | eTypeHasValue;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002919 break;
2920 case clang::BuiltinType::Bool:
2921 case clang::BuiltinType::Char_U:
2922 case clang::BuiltinType::UChar:
2923 case clang::BuiltinType::WChar_U:
2924 case clang::BuiltinType::Char16:
2925 case clang::BuiltinType::Char32:
2926 case clang::BuiltinType::UShort:
2927 case clang::BuiltinType::UInt:
2928 case clang::BuiltinType::ULong:
2929 case clang::BuiltinType::ULongLong:
2930 case clang::BuiltinType::UInt128:
2931 case clang::BuiltinType::Char_S:
2932 case clang::BuiltinType::SChar:
2933 case clang::BuiltinType::WChar_S:
2934 case clang::BuiltinType::Short:
2935 case clang::BuiltinType::Int:
2936 case clang::BuiltinType::Long:
2937 case clang::BuiltinType::LongLong:
2938 case clang::BuiltinType::Int128:
2939 case clang::BuiltinType::Float:
2940 case clang::BuiltinType::Double:
2941 case clang::BuiltinType::LongDouble:
2942 return eTypeIsBuiltIn | eTypeHasValue | eTypeIsScalar;
Greg Clayton73b472d2010-10-27 03:32:59 +00002943 default:
2944 break;
Sean Callanana2424172010-10-25 00:29:48 +00002945 }
2946 return eTypeIsBuiltIn | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002947
2948 case clang::Type::BlockPointer:
2949 if (pointee_or_element_clang_type)
2950 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2951 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
2952
Greg Clayton49462ea2011-01-15 02:52:14 +00002953 case clang::Type::Complex: return eTypeIsBuiltIn | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002954
2955 case clang::Type::ConstantArray:
2956 case clang::Type::DependentSizedArray:
2957 case clang::Type::IncompleteArray:
2958 case clang::Type::VariableArray:
2959 if (pointee_or_element_clang_type)
2960 *pointee_or_element_clang_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
2961 return eTypeHasChildren | eTypeIsArray;
2962
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002963 case clang::Type::DependentName: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002964 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
2965 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
2966 case clang::Type::Decltype: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00002967
2968 case clang::Type::Enum:
2969 if (pointee_or_element_clang_type)
2970 *pointee_or_element_clang_type = cast<EnumType>(qual_type)->getDecl()->getIntegerType().getAsOpaquePtr();
2971 return eTypeIsEnumeration | eTypeHasValue;
2972
Sean Callanan912855f2011-08-11 23:56:13 +00002973 case clang::Type::Elaborated:
2974 return ClangASTContext::GetTypeInfo (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2975 ast,
2976 pointee_or_element_clang_type);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002977 case clang::Type::ExtVector: return eTypeHasChildren | eTypeIsVector;
2978 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
2979 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002980 case clang::Type::InjectedClassName: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00002981
2982 case clang::Type::LValueReference:
2983 case clang::Type::RValueReference:
2984 if (pointee_or_element_clang_type)
2985 *pointee_or_element_clang_type = cast<ReferenceType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr();
2986 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
2987
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002988 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002989
2990 case clang::Type::ObjCObjectPointer:
2991 if (pointee_or_element_clang_type)
2992 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2993 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
2994
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002995 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
2996 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
Greg Clayton73b472d2010-10-27 03:32:59 +00002997
2998 case clang::Type::Pointer:
2999 if (pointee_or_element_clang_type)
3000 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
3001 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
3002
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003003 case clang::Type::Record:
3004 if (qual_type->getAsCXXRecordDecl())
3005 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
3006 else
3007 return eTypeHasChildren | eTypeIsStructUnion;
3008 break;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003009 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
3010 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
3011 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
Greg Clayton73b472d2010-10-27 03:32:59 +00003012
3013 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00003014 return eTypeIsTypedef | ClangASTContext::GetTypeInfo (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00003015 ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00003016 pointee_or_element_clang_type);
3017
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003018 case clang::Type::TypeOfExpr: return 0;
3019 case clang::Type::TypeOf: return 0;
3020 case clang::Type::UnresolvedUsing: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003021 case clang::Type::Vector: return eTypeHasChildren | eTypeIsVector;
3022 default: return 0;
3023 }
3024 return 0;
3025}
3026
Greg Clayton9e409562010-07-28 02:04:09 +00003027
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003028#pragma mark Aggregate Types
3029
3030bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00003031ClangASTContext::IsAggregateType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003032{
3033 if (clang_type == NULL)
3034 return false;
3035
3036 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3037
Greg Clayton737b9322010-09-13 03:32:57 +00003038 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3039 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003040 {
Greg Claytone1a916a2010-07-21 22:12:05 +00003041 case clang::Type::IncompleteArray:
3042 case clang::Type::VariableArray:
3043 case clang::Type::ConstantArray:
3044 case clang::Type::ExtVector:
3045 case clang::Type::Vector:
3046 case clang::Type::Record:
Greg Clayton9e409562010-07-28 02:04:09 +00003047 case clang::Type::ObjCObject:
3048 case clang::Type::ObjCInterface:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003049 return true;
Sean Callanan912855f2011-08-11 23:56:13 +00003050 case clang::Type::Elaborated:
3051 return ClangASTContext::IsAggregateType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Claytone1a916a2010-07-21 22:12:05 +00003052 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00003053 return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003054
3055 default:
3056 break;
3057 }
3058 // The clang type does have a value
3059 return false;
3060}
3061
3062uint32_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00003063ClangASTContext::GetNumChildren (clang::ASTContext *ast, clang_type_t clang_type, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003064{
Greg Clayton6beaaa62011-01-17 03:46:26 +00003065 if (clang_type == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003066 return 0;
3067
3068 uint32_t num_children = 0;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003069 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00003070 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3071 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003072 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003073 case clang::Type::Builtin:
3074 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3075 {
Greg Clayton73b472d2010-10-27 03:32:59 +00003076 case clang::BuiltinType::ObjCId: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003077 case clang::BuiltinType::ObjCClass: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003078 num_children = 1;
Greg Clayton73b472d2010-10-27 03:32:59 +00003079 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003080
3081 default:
3082 break;
3083 }
3084 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00003085
Greg Clayton49462ea2011-01-15 02:52:14 +00003086 case clang::Type::Complex: return 0;
Greg Clayton54979cd2010-12-15 05:08:08 +00003087
Greg Claytone1a916a2010-07-21 22:12:05 +00003088 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00003089 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003090 {
3091 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3092 const RecordDecl *record_decl = record_type->getDecl();
3093 assert(record_decl);
3094 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3095 if (cxx_record_decl)
3096 {
3097 if (omit_empty_base_classes)
3098 {
3099 // Check each base classes to see if it or any of its
3100 // base classes contain any fields. This can help
3101 // limit the noise in variable views by not having to
3102 // show base classes that contain no members.
3103 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3104 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3105 base_class != base_class_end;
3106 ++base_class)
3107 {
3108 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3109
3110 // Skip empty base classes
3111 if (RecordHasFields(base_class_decl) == false)
3112 continue;
3113
3114 num_children++;
3115 }
3116 }
3117 else
3118 {
3119 // Include all base classes
3120 num_children += cxx_record_decl->getNumBases();
3121 }
3122
3123 }
3124 RecordDecl::field_iterator field, field_end;
3125 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3126 ++num_children;
3127 }
3128 break;
3129
Greg Clayton9e409562010-07-28 02:04:09 +00003130 case clang::Type::ObjCObject:
3131 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00003132 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00003133 {
Sean Callanan78e37602011-01-27 04:42:51 +00003134 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003135 assert (objc_class_type);
3136 if (objc_class_type)
3137 {
3138 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3139
3140 if (class_interface_decl)
3141 {
3142
3143 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3144 if (superclass_interface_decl)
3145 {
3146 if (omit_empty_base_classes)
3147 {
3148 if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true))
3149 ++num_children;
3150 }
3151 else
3152 ++num_children;
3153 }
3154
3155 num_children += class_interface_decl->ivar_size();
3156 }
3157 }
3158 }
3159 break;
3160
3161 case clang::Type::ObjCObjectPointer:
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003162 {
Sean Callanan78e37602011-01-27 04:42:51 +00003163 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003164 QualType pointee_type = pointer_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003165 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3166 pointee_type.getAsOpaquePtr(),
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003167 omit_empty_base_classes);
3168 // If this type points to a simple type, then it has 1 child
3169 if (num_pointee_children == 0)
3170 num_children = 1;
3171 else
3172 num_children = num_pointee_children;
3173 }
3174 break;
Greg Clayton9e409562010-07-28 02:04:09 +00003175
Greg Claytone1a916a2010-07-21 22:12:05 +00003176 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003177 num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
3178 break;
3179
Greg Claytone1a916a2010-07-21 22:12:05 +00003180 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003181 {
Sean Callanan78e37602011-01-27 04:42:51 +00003182 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003183 QualType pointee_type (pointer_type->getPointeeType());
Greg Clayton6beaaa62011-01-17 03:46:26 +00003184 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3185 pointee_type.getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00003186 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003187 if (num_pointee_children == 0)
Greg Clayton54979cd2010-12-15 05:08:08 +00003188 {
3189 // We have a pointer to a pointee type that claims it has no children.
3190 // We will want to look at
3191 num_children = ClangASTContext::GetNumPointeeChildren (pointee_type.getAsOpaquePtr());
3192 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003193 else
3194 num_children = num_pointee_children;
3195 }
3196 break;
3197
Greg Clayton73b472d2010-10-27 03:32:59 +00003198 case clang::Type::LValueReference:
3199 case clang::Type::RValueReference:
3200 {
Sean Callanan78e37602011-01-27 04:42:51 +00003201 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton73b472d2010-10-27 03:32:59 +00003202 QualType pointee_type = reference_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003203 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3204 pointee_type.getAsOpaquePtr(),
Greg Clayton73b472d2010-10-27 03:32:59 +00003205 omit_empty_base_classes);
3206 // If this type points to a simple type, then it has 1 child
3207 if (num_pointee_children == 0)
3208 num_children = 1;
3209 else
3210 num_children = num_pointee_children;
3211 }
3212 break;
3213
3214
Greg Claytone1a916a2010-07-21 22:12:05 +00003215 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00003216 num_children = ClangASTContext::GetNumChildren (ast,
3217 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3218 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003219 break;
Sean Callanan912855f2011-08-11 23:56:13 +00003220
3221 case clang::Type::Elaborated:
3222 num_children = ClangASTContext::GetNumChildren (ast,
3223 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3224 omit_empty_base_classes);
3225 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003226
3227 default:
3228 break;
3229 }
3230 return num_children;
3231}
3232
Greg Claytonbf2331c2011-09-09 23:04:00 +00003233uint32_t
3234ClangASTContext::GetNumDirectBaseClasses (clang::ASTContext *ast, clang_type_t clang_type)
3235{
3236 if (clang_type == NULL)
3237 return 0;
3238
3239 uint32_t count = 0;
3240 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3241 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3242 switch (type_class)
3243 {
3244 case clang::Type::Record:
3245 if (GetCompleteQualType (ast, qual_type))
3246 {
3247 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3248 if (cxx_record_decl)
3249 count = cxx_record_decl->getNumBases();
3250 }
3251 break;
3252
3253 case clang::Type::ObjCObject:
3254 case clang::Type::ObjCInterface:
3255 if (GetCompleteQualType (ast, qual_type))
3256 {
3257 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3258 if (objc_class_type)
3259 {
3260 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3261
3262 if (class_interface_decl && class_interface_decl->getSuperClass())
3263 count = 1;
3264 }
3265 }
3266 break;
3267
3268
3269 case clang::Type::Typedef:
3270 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3271 break;
3272
3273 case clang::Type::Elaborated:
3274 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3275 break;
3276
3277 default:
3278 break;
3279 }
3280 return count;
3281}
3282
3283uint32_t
3284ClangASTContext::GetNumVirtualBaseClasses (clang::ASTContext *ast,
3285 clang_type_t clang_type)
3286{
3287 if (clang_type == NULL)
3288 return 0;
3289
3290 uint32_t count = 0;
3291 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3292 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3293 switch (type_class)
3294 {
3295 case clang::Type::Record:
3296 if (GetCompleteQualType (ast, qual_type))
3297 {
3298 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3299 if (cxx_record_decl)
3300 count = cxx_record_decl->getNumVBases();
3301 }
3302 break;
3303
3304 case clang::Type::Typedef:
3305 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3306 break;
3307
3308 case clang::Type::Elaborated:
3309 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3310 break;
3311
3312 default:
3313 break;
3314 }
3315 return count;
3316}
3317
3318uint32_t
3319ClangASTContext::GetNumFields (clang::ASTContext *ast, clang_type_t clang_type)
3320{
3321 if (clang_type == NULL)
3322 return 0;
3323
3324 uint32_t count = 0;
3325 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3326 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3327 switch (type_class)
3328 {
3329 case clang::Type::Record:
3330 if (GetCompleteQualType (ast, qual_type))
3331 {
3332 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
3333 if (record_type)
3334 {
3335 RecordDecl *record_decl = record_type->getDecl();
3336 if (record_decl)
3337 {
3338 uint32_t field_idx = 0;
3339 RecordDecl::field_iterator field, field_end;
3340 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3341 ++field_idx;
3342 count = field_idx;
3343 }
3344 }
3345 }
3346 break;
3347
3348 case clang::Type::Typedef:
3349 count = ClangASTContext::GetNumFields (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3350 break;
3351
3352 case clang::Type::Elaborated:
3353 count = ClangASTContext::GetNumFields (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3354 break;
3355
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003356 case clang::Type::ObjCObject:
3357 case clang::Type::ObjCInterface:
3358 if (GetCompleteQualType (ast, qual_type))
3359 {
3360 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3361 if (objc_class_type)
3362 {
3363 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3364
3365 if (class_interface_decl)
3366 count = class_interface_decl->ivar_size();
3367 }
3368 }
3369 break;
3370
Greg Claytonbf2331c2011-09-09 23:04:00 +00003371 default:
3372 break;
3373 }
3374 return count;
3375}
3376
3377clang_type_t
3378ClangASTContext::GetDirectBaseClassAtIndex (clang::ASTContext *ast,
3379 clang_type_t clang_type,
Greg Claytonc7bece562013-01-25 18:06:21 +00003380 size_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003381 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003382{
3383 if (clang_type == NULL)
3384 return 0;
3385
3386 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3387 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3388 switch (type_class)
3389 {
3390 case clang::Type::Record:
3391 if (GetCompleteQualType (ast, qual_type))
3392 {
3393 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3394 if (cxx_record_decl)
3395 {
3396 uint32_t curr_idx = 0;
3397 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3398 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3399 base_class != base_class_end;
3400 ++base_class, ++curr_idx)
3401 {
3402 if (curr_idx == idx)
3403 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003404 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003405 {
3406 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3407 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3408// if (base_class->isVirtual())
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003409// *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003410// else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003411 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003412 }
3413 return base_class->getType().getAsOpaquePtr();
3414 }
3415 }
3416 }
3417 }
3418 break;
3419
3420 case clang::Type::ObjCObject:
3421 case clang::Type::ObjCInterface:
3422 if (idx == 0 && GetCompleteQualType (ast, qual_type))
3423 {
3424 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3425 if (objc_class_type)
3426 {
3427 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3428
3429 if (class_interface_decl)
3430 {
3431 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3432 if (superclass_interface_decl)
3433 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003434 if (bit_offset_ptr)
3435 *bit_offset_ptr = 0;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003436 return ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr();
3437 }
3438 }
3439 }
3440 }
3441 break;
3442
3443
3444 case clang::Type::Typedef:
3445 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3446 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3447 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003448 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003449
3450 case clang::Type::Elaborated:
3451 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3452 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3453 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003454 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003455
3456 default:
3457 break;
3458 }
3459 return NULL;
3460}
3461
3462clang_type_t
3463ClangASTContext::GetVirtualBaseClassAtIndex (clang::ASTContext *ast,
3464 clang_type_t clang_type,
Greg Claytonc7bece562013-01-25 18:06:21 +00003465 size_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003466 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003467{
3468 if (clang_type == NULL)
3469 return 0;
3470
3471 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3472 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3473 switch (type_class)
3474 {
3475 case clang::Type::Record:
3476 if (GetCompleteQualType (ast, qual_type))
3477 {
3478 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3479 if (cxx_record_decl)
3480 {
3481 uint32_t curr_idx = 0;
3482 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3483 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
3484 base_class != base_class_end;
3485 ++base_class, ++curr_idx)
3486 {
3487 if (curr_idx == idx)
3488 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003489 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003490 {
3491 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3492 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003493 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003494
3495 }
3496 return base_class->getType().getAsOpaquePtr();
3497 }
3498 }
3499 }
3500 }
3501 break;
3502
3503 case clang::Type::Typedef:
3504 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3505 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3506 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003507 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003508
3509 case clang::Type::Elaborated:
3510 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3511 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3512 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003513 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003514
3515 default:
3516 break;
3517 }
3518 return NULL;
3519}
3520
3521clang_type_t
3522ClangASTContext::GetFieldAtIndex (clang::ASTContext *ast,
3523 clang_type_t clang_type,
Greg Claytonc7bece562013-01-25 18:06:21 +00003524 size_t idx,
Greg Claytonbf2331c2011-09-09 23:04:00 +00003525 std::string& name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003526 uint64_t *bit_offset_ptr,
3527 uint32_t *bitfield_bit_size_ptr,
3528 bool *is_bitfield_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003529{
3530 if (clang_type == NULL)
3531 return 0;
3532
3533 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3534 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3535 switch (type_class)
3536 {
3537 case clang::Type::Record:
3538 if (GetCompleteQualType (ast, qual_type))
3539 {
3540 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3541 const RecordDecl *record_decl = record_type->getDecl();
3542 uint32_t field_idx = 0;
3543 RecordDecl::field_iterator field, field_end;
3544 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
3545 {
3546 if (idx == field_idx)
3547 {
3548 // Print the member type if requested
3549 // Print the member name and equal sign
3550 name.assign(field->getNameAsString());
3551
3552 // Figure out the type byte size (field_type_info.first) and
3553 // alignment (field_type_info.second) from the AST context.
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003554 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003555 {
3556 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003557 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003558 }
3559
Greg Clayton1811b4f2012-07-31 23:39:10 +00003560 const bool is_bitfield = field->isBitField();
3561
3562 if (bitfield_bit_size_ptr)
3563 {
3564 *bitfield_bit_size_ptr = 0;
3565
3566 if (is_bitfield && ast)
3567 {
3568 Expr *bitfield_bit_size_expr = field->getBitWidth();
3569 llvm::APSInt bitfield_apsint;
3570 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
3571 {
3572 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
3573 }
3574 }
3575 }
3576 if (is_bitfield_ptr)
3577 *is_bitfield_ptr = is_bitfield;
3578
Greg Claytonbf2331c2011-09-09 23:04:00 +00003579 return field->getType().getAsOpaquePtr();
3580 }
3581 }
3582 }
3583 break;
3584
3585 case clang::Type::ObjCObject:
3586 case clang::Type::ObjCInterface:
3587 if (GetCompleteQualType (ast, qual_type))
3588 {
3589 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3590 assert (objc_class_type);
3591 if (objc_class_type)
3592 {
3593 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3594
3595 if (class_interface_decl)
3596 {
3597 if (idx < (class_interface_decl->ivar_size()))
3598 {
3599 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
3600 uint32_t ivar_idx = 0;
3601
3602 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
3603 {
3604 if (ivar_idx == idx)
3605 {
3606 const ObjCIvarDecl* ivar_decl = *ivar_pos;
3607
3608 QualType ivar_qual_type(ivar_decl->getType());
3609
3610 name.assign(ivar_decl->getNameAsString());
3611
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003612 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003613 {
3614 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003615 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003616 }
3617
Greg Clayton1811b4f2012-07-31 23:39:10 +00003618 const bool is_bitfield = ivar_pos->isBitField();
3619
3620 if (bitfield_bit_size_ptr)
3621 {
3622 *bitfield_bit_size_ptr = 0;
3623
3624 if (is_bitfield && ast)
3625 {
3626 Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
3627 llvm::APSInt bitfield_apsint;
3628 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
3629 {
3630 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
3631 }
3632 }
3633 }
3634 if (is_bitfield_ptr)
3635 *is_bitfield_ptr = is_bitfield;
3636
Greg Claytonbf2331c2011-09-09 23:04:00 +00003637 return ivar_qual_type.getAsOpaquePtr();
3638 }
3639 }
3640 }
3641 }
3642 }
3643 }
3644 break;
3645
3646
3647 case clang::Type::Typedef:
3648 return ClangASTContext::GetFieldAtIndex (ast,
3649 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3650 idx,
3651 name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003652 bit_offset_ptr,
3653 bitfield_bit_size_ptr,
3654 is_bitfield_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003655
3656 case clang::Type::Elaborated:
3657 return ClangASTContext::GetFieldAtIndex (ast,
3658 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3659 idx,
3660 name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003661 bit_offset_ptr,
3662 bitfield_bit_size_ptr,
3663 is_bitfield_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003664
3665 default:
3666 break;
3667 }
3668 return NULL;
3669}
3670
Greg Claytoneaafa732012-10-13 00:20:27 +00003671lldb::BasicType
3672ClangASTContext::GetLLDBBasicTypeEnumeration (clang_type_t clang_type)
3673{
3674 if (clang_type)
3675 {
3676 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3677 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Claytonc4c5e892012-10-15 21:16:43 +00003678 if (type_class == clang::Type::Builtin)
Greg Claytoneaafa732012-10-13 00:20:27 +00003679 {
Greg Claytoneaafa732012-10-13 00:20:27 +00003680 switch (cast<clang::BuiltinType>(qual_type)->getKind())
Greg Claytonc4c5e892012-10-15 21:16:43 +00003681 {
Greg Claytoneaafa732012-10-13 00:20:27 +00003682 case clang::BuiltinType::Void: return eBasicTypeVoid;
3683 case clang::BuiltinType::Bool: return eBasicTypeBool;
3684 case clang::BuiltinType::Char_S: return eBasicTypeSignedChar;
3685 case clang::BuiltinType::Char_U: return eBasicTypeUnsignedChar;
3686 case clang::BuiltinType::Char16: return eBasicTypeChar16;
3687 case clang::BuiltinType::Char32: return eBasicTypeChar32;
3688 case clang::BuiltinType::UChar: return eBasicTypeUnsignedChar;
3689 case clang::BuiltinType::SChar: return eBasicTypeSignedChar;
3690 case clang::BuiltinType::WChar_S: return eBasicTypeSignedWChar;
3691 case clang::BuiltinType::WChar_U: return eBasicTypeUnsignedWChar;
3692 case clang::BuiltinType::Short: return eBasicTypeShort;
3693 case clang::BuiltinType::UShort: return eBasicTypeUnsignedShort;
3694 case clang::BuiltinType::Int: return eBasicTypeInt;
3695 case clang::BuiltinType::UInt: return eBasicTypeUnsignedInt;
3696 case clang::BuiltinType::Long: return eBasicTypeLong;
3697 case clang::BuiltinType::ULong: return eBasicTypeUnsignedLong;
3698 case clang::BuiltinType::LongLong: return eBasicTypeLongLong;
3699 case clang::BuiltinType::ULongLong: return eBasicTypeUnsignedLongLong;
3700 case clang::BuiltinType::Int128: return eBasicTypeInt128;
3701 case clang::BuiltinType::UInt128: return eBasicTypeUnsignedInt128;
3702
3703 case clang::BuiltinType::Half: return eBasicTypeHalf;
3704 case clang::BuiltinType::Float: return eBasicTypeFloat;
3705 case clang::BuiltinType::Double: return eBasicTypeDouble;
3706 case clang::BuiltinType::LongDouble:return eBasicTypeLongDouble;
3707
3708 case clang::BuiltinType::NullPtr: return eBasicTypeNullPtr;
3709 case clang::BuiltinType::ObjCId: return eBasicTypeObjCID;
3710 case clang::BuiltinType::ObjCClass: return eBasicTypeObjCClass;
3711 case clang::BuiltinType::ObjCSel: return eBasicTypeObjCSel;
3712 case clang::BuiltinType::Dependent:
3713 case clang::BuiltinType::Overload:
3714 case clang::BuiltinType::BoundMember:
3715 case clang::BuiltinType::PseudoObject:
3716 case clang::BuiltinType::UnknownAny:
3717 case clang::BuiltinType::BuiltinFn:
3718 case clang::BuiltinType::ARCUnbridgedCast:
Greg Clayton3dcaa2c2013-02-01 00:46:49 +00003719 case clang::BuiltinType::OCLEvent:
3720 case clang::BuiltinType::OCLImage1d:
3721 case clang::BuiltinType::OCLImage1dArray:
3722 case clang::BuiltinType::OCLImage1dBuffer:
3723 case clang::BuiltinType::OCLImage2d:
3724 case clang::BuiltinType::OCLImage2dArray:
3725 case clang::BuiltinType::OCLImage3d:
Greg Clayton142c5a42013-02-13 18:15:56 +00003726 case clang::BuiltinType::OCLSampler:
Greg Claytoneaafa732012-10-13 00:20:27 +00003727 return eBasicTypeOther;
Greg Claytonc4c5e892012-10-15 21:16:43 +00003728 }
Greg Claytoneaafa732012-10-13 00:20:27 +00003729 }
3730 }
3731
3732 return eBasicTypeInvalid;
3733}
3734
3735
Greg Claytonbf2331c2011-09-09 23:04:00 +00003736
Greg Clayton54979cd2010-12-15 05:08:08 +00003737// If a pointer to a pointee type (the clang_type arg) says that it has no
3738// children, then we either need to trust it, or override it and return a
3739// different result. For example, an "int *" has one child that is an integer,
3740// but a function pointer doesn't have any children. Likewise if a Record type
3741// claims it has no children, then there really is nothing to show.
3742uint32_t
3743ClangASTContext::GetNumPointeeChildren (clang_type_t clang_type)
3744{
3745 if (clang_type == NULL)
3746 return 0;
3747
3748 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3749 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3750 switch (type_class)
3751 {
Greg Clayton97a43712011-01-08 22:26:47 +00003752 case clang::Type::Builtin:
3753 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3754 {
Greg Clayton7260f622011-04-18 08:33:37 +00003755 case clang::BuiltinType::UnknownAny:
Greg Clayton97a43712011-01-08 22:26:47 +00003756 case clang::BuiltinType::Void:
3757 case clang::BuiltinType::NullPtr:
Greg Clayton3dcaa2c2013-02-01 00:46:49 +00003758 case clang::BuiltinType::OCLEvent:
3759 case clang::BuiltinType::OCLImage1d:
3760 case clang::BuiltinType::OCLImage1dArray:
3761 case clang::BuiltinType::OCLImage1dBuffer:
3762 case clang::BuiltinType::OCLImage2d:
3763 case clang::BuiltinType::OCLImage2dArray:
3764 case clang::BuiltinType::OCLImage3d:
Greg Clayton142c5a42013-02-13 18:15:56 +00003765 case clang::BuiltinType::OCLSampler:
Greg Clayton97a43712011-01-08 22:26:47 +00003766 return 0;
3767 case clang::BuiltinType::Bool:
3768 case clang::BuiltinType::Char_U:
3769 case clang::BuiltinType::UChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003770 case clang::BuiltinType::WChar_U:
Greg Clayton97a43712011-01-08 22:26:47 +00003771 case clang::BuiltinType::Char16:
3772 case clang::BuiltinType::Char32:
3773 case clang::BuiltinType::UShort:
3774 case clang::BuiltinType::UInt:
3775 case clang::BuiltinType::ULong:
3776 case clang::BuiltinType::ULongLong:
3777 case clang::BuiltinType::UInt128:
3778 case clang::BuiltinType::Char_S:
3779 case clang::BuiltinType::SChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003780 case clang::BuiltinType::WChar_S:
Greg Clayton97a43712011-01-08 22:26:47 +00003781 case clang::BuiltinType::Short:
3782 case clang::BuiltinType::Int:
3783 case clang::BuiltinType::Long:
3784 case clang::BuiltinType::LongLong:
3785 case clang::BuiltinType::Int128:
3786 case clang::BuiltinType::Float:
3787 case clang::BuiltinType::Double:
3788 case clang::BuiltinType::LongDouble:
3789 case clang::BuiltinType::Dependent:
3790 case clang::BuiltinType::Overload:
Greg Clayton97a43712011-01-08 22:26:47 +00003791 case clang::BuiltinType::ObjCId:
3792 case clang::BuiltinType::ObjCClass:
3793 case clang::BuiltinType::ObjCSel:
Sean Callanand12cf8bb2011-05-15 22:34:38 +00003794 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00003795 case clang::BuiltinType::Half:
3796 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00003797 case clang::BuiltinType::PseudoObject:
Sean Callanan3d654b32012-09-24 22:25:51 +00003798 case clang::BuiltinType::BuiltinFn:
Greg Clayton97a43712011-01-08 22:26:47 +00003799 return 1;
3800 }
3801 break;
3802
Greg Clayton49462ea2011-01-15 02:52:14 +00003803 case clang::Type::Complex: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003804 case clang::Type::Pointer: return 1;
3805 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
3806 case clang::Type::LValueReference: return 1;
3807 case clang::Type::RValueReference: return 1;
3808 case clang::Type::MemberPointer: return 0;
3809 case clang::Type::ConstantArray: return 0;
3810 case clang::Type::IncompleteArray: return 0;
3811 case clang::Type::VariableArray: return 0;
3812 case clang::Type::DependentSizedArray: return 0;
3813 case clang::Type::DependentSizedExtVector: return 0;
3814 case clang::Type::Vector: return 0;
3815 case clang::Type::ExtVector: return 0;
3816 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
3817 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
3818 case clang::Type::UnresolvedUsing: return 0;
3819 case clang::Type::Paren: return 0;
3820 case clang::Type::Typedef: return ClangASTContext::GetNumPointeeChildren (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00003821 case clang::Type::Elaborated: return ClangASTContext::GetNumPointeeChildren (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003822 case clang::Type::TypeOfExpr: return 0;
3823 case clang::Type::TypeOf: return 0;
3824 case clang::Type::Decltype: return 0;
3825 case clang::Type::Record: return 0;
3826 case clang::Type::Enum: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003827 case clang::Type::TemplateTypeParm: return 1;
3828 case clang::Type::SubstTemplateTypeParm: return 1;
3829 case clang::Type::TemplateSpecialization: return 1;
3830 case clang::Type::InjectedClassName: return 0;
3831 case clang::Type::DependentName: return 1;
3832 case clang::Type::DependentTemplateSpecialization: return 1;
3833 case clang::Type::ObjCObject: return 0;
3834 case clang::Type::ObjCInterface: return 0;
3835 case clang::Type::ObjCObjectPointer: return 1;
3836 default:
3837 break;
3838 }
3839 return 0;
3840}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003841
Greg Clayton1be10fc2010-09-29 01:12:09 +00003842clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003843ClangASTContext::GetChildClangTypeAtIndex
3844(
Jim Inghamd555bac2011-06-24 22:03:24 +00003845 ExecutionContext *exe_ctx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003846 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003847 clang_type_t parent_clang_type,
Greg Claytonc7bece562013-01-25 18:06:21 +00003848 size_t idx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003849 bool transparent_pointers,
3850 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003851 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003852 std::string& child_name,
3853 uint32_t &child_byte_size,
3854 int32_t &child_byte_offset,
3855 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003856 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003857 bool &child_is_base_class,
3858 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003859)
3860{
3861 if (parent_clang_type)
3862
Jim Inghamd555bac2011-06-24 22:03:24 +00003863 return GetChildClangTypeAtIndex (exe_ctx,
3864 getASTContext(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003865 parent_name,
3866 parent_clang_type,
3867 idx,
3868 transparent_pointers,
3869 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003870 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003871 child_name,
3872 child_byte_size,
3873 child_byte_offset,
3874 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003875 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003876 child_is_base_class,
3877 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003878 return NULL;
3879}
3880
Greg Clayton1be10fc2010-09-29 01:12:09 +00003881clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003882ClangASTContext::GetChildClangTypeAtIndex
3883(
Jim Inghamd555bac2011-06-24 22:03:24 +00003884 ExecutionContext *exe_ctx,
Greg Clayton6beaaa62011-01-17 03:46:26 +00003885 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003886 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003887 clang_type_t parent_clang_type,
Greg Claytonc7bece562013-01-25 18:06:21 +00003888 size_t idx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003889 bool transparent_pointers,
3890 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003891 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003892 std::string& child_name,
3893 uint32_t &child_byte_size,
3894 int32_t &child_byte_offset,
3895 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003896 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003897 bool &child_is_base_class,
3898 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003899)
3900{
3901 if (parent_clang_type == NULL)
3902 return NULL;
3903
Greg Clayton4ef877f2012-12-06 02:33:54 +00003904 QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type));
3905 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
3906 child_bitfield_bit_size = 0;
3907 child_bitfield_bit_offset = 0;
3908 child_is_base_class = false;
3909
3910 const bool idx_is_valid = idx < ClangASTContext::GetNumChildren (ast, parent_clang_type, omit_empty_base_classes);
3911 uint32_t bit_offset;
3912 switch (parent_type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003913 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003914 case clang::Type::Builtin:
3915 if (idx_is_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003916 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003917 switch (cast<clang::BuiltinType>(parent_qual_type)->getKind())
3918 {
3919 case clang::BuiltinType::ObjCId:
3920 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00003921 child_name = "isa";
Greg Clayton6beaaa62011-01-17 03:46:26 +00003922 child_byte_size = ast->getTypeSize(ast->ObjCBuiltinClassTy) / CHAR_BIT;
3923 return ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003924
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003925 default:
3926 break;
3927 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00003928 }
3929 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003930
Greg Clayton4ef877f2012-12-06 02:33:54 +00003931 case clang::Type::Record:
3932 if (idx_is_valid && GetCompleteQualType (ast, parent_qual_type))
3933 {
3934 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr());
3935 const RecordDecl *record_decl = record_type->getDecl();
3936 assert(record_decl);
3937 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
3938 uint32_t child_idx = 0;
3939
3940 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3941 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003942 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003943 // We might have base classes to print out first
3944 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3945 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3946 base_class != base_class_end;
3947 ++base_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003948 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003949 const CXXRecordDecl *base_class_decl = NULL;
3950
3951 // Skip empty base classes
3952 if (omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003953 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003954 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3955 if (RecordHasFields(base_class_decl) == false)
3956 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003957 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00003958
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003959 if (idx == child_idx)
3960 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003961 if (base_class_decl == NULL)
3962 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003963
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003964
Greg Clayton4ef877f2012-12-06 02:33:54 +00003965 if (base_class->isVirtual())
3966 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
3967 else
3968 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003969
Greg Clayton4ef877f2012-12-06 02:33:54 +00003970 // Base classes should be a multiple of 8 bits in size
3971 child_byte_offset = bit_offset/8;
3972
3973 child_name = ClangASTType::GetTypeNameForQualType(ast, base_class->getType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003974
Greg Clayton4ef877f2012-12-06 02:33:54 +00003975 uint64_t clang_type_info_bit_size = ast->getTypeSize(base_class->getType());
3976
3977 // Base classes bit sizes should be a multiple of 8 bits in size
3978 assert (clang_type_info_bit_size % 8 == 0);
3979 child_byte_size = clang_type_info_bit_size / 8;
3980 child_is_base_class = true;
3981 return base_class->getType().getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003982 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00003983 // We don't increment the child index in the for loop since we might
3984 // be skipping empty base classes
3985 ++child_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003986 }
3987 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00003988 // Make sure index is in range...
3989 uint32_t field_idx = 0;
3990 RecordDecl::field_iterator field, field_end;
3991 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 +00003992 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003993 if (idx == child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00003994 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003995 // Print the member type if requested
3996 // Print the member name and equal sign
3997 child_name.assign(field->getNameAsString().c_str());
3998
3999 // Figure out the type byte size (field_type_info.first) and
4000 // alignment (field_type_info.second) from the AST context.
4001 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(field->getType());
4002 assert(field_idx < record_layout.getFieldCount());
4003
4004 child_byte_size = field_type_info.first / 8;
4005
4006 // Figure out the field offset within the current struct/union/class type
4007 bit_offset = record_layout.getFieldOffset (field_idx);
4008 child_byte_offset = bit_offset / 8;
4009 if (ClangASTContext::FieldIsBitfield (ast, *field, child_bitfield_bit_size))
4010 child_bitfield_bit_offset = bit_offset % 8;
4011
4012 return field->getType().getAsOpaquePtr();
4013 }
4014 }
4015 }
4016 break;
4017
4018 case clang::Type::ObjCObject:
4019 case clang::Type::ObjCInterface:
4020 if (idx_is_valid && GetCompleteQualType (ast, parent_qual_type))
4021 {
4022 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr());
4023 assert (objc_class_type);
4024 if (objc_class_type)
4025 {
4026 uint32_t child_idx = 0;
4027 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4028
4029 if (class_interface_decl)
4030 {
4031
4032 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
4033 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4034 if (superclass_interface_decl)
Greg Clayton9e409562010-07-28 02:04:09 +00004035 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004036 if (omit_empty_base_classes)
Greg Clayton9e409562010-07-28 02:04:09 +00004037 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004038 if (ClangASTContext::GetNumChildren(ast, ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0)
Greg Clayton9e409562010-07-28 02:04:09 +00004039 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004040 if (idx == 0)
Greg Clayton9e409562010-07-28 02:04:09 +00004041 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004042 QualType ivar_qual_type(ast->getObjCInterfaceType(superclass_interface_decl));
Greg Clayton9e409562010-07-28 02:04:09 +00004043
Greg Clayton9e409562010-07-28 02:04:09 +00004044
Greg Clayton4ef877f2012-12-06 02:33:54 +00004045 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
Greg Clayton9e409562010-07-28 02:04:09 +00004046
Greg Clayton6beaaa62011-01-17 03:46:26 +00004047 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004048
4049 child_byte_size = ivar_type_info.first / 8;
Greg Clayton4ef877f2012-12-06 02:33:54 +00004050 child_byte_offset = 0;
4051 child_is_base_class = true;
Greg Clayton9e409562010-07-28 02:04:09 +00004052
Greg Clayton9e409562010-07-28 02:04:09 +00004053 return ivar_qual_type.getAsOpaquePtr();
4054 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004055
Greg Clayton9e409562010-07-28 02:04:09 +00004056 ++child_idx;
4057 }
4058 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004059 else
4060 ++child_idx;
4061 }
4062
4063 const uint32_t superclass_idx = child_idx;
4064
4065 if (idx < (child_idx + class_interface_decl->ivar_size()))
4066 {
4067 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4068
4069 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
4070 {
4071 if (child_idx == idx)
4072 {
4073 ObjCIvarDecl* ivar_decl = *ivar_pos;
4074
4075 QualType ivar_qual_type(ivar_decl->getType());
4076
4077 child_name.assign(ivar_decl->getNameAsString().c_str());
4078
4079 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
4080
4081 child_byte_size = ivar_type_info.first / 8;
4082
4083 // Figure out the field offset within the current struct/union/class type
4084 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
4085 // that doesn't account for the space taken up by unbacked properties, or from
4086 // the changing size of base classes that are newer than this class.
4087 // So if we have a process around that we can ask about this object, do so.
4088 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
4089 Process *process = NULL;
4090 if (exe_ctx)
4091 process = exe_ctx->GetProcessPtr();
4092 if (process)
4093 {
4094 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4095 if (objc_runtime != NULL)
4096 {
4097 ClangASTType parent_ast_type (ast, parent_qual_type.getAsOpaquePtr());
4098 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
4099 }
4100 }
4101
4102 // Setting this to UINT32_MAX to make sure we don't compute it twice...
4103 bit_offset = UINT32_MAX;
4104
4105 if (child_byte_offset == LLDB_INVALID_IVAR_OFFSET)
4106 {
4107 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
4108 child_byte_offset = bit_offset / 8;
4109 }
4110
4111 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
4112 // of a bitfield within its containing object. So regardless of where we get the byte
4113 // offset from, we still need to get the bit offset for bitfields from the layout.
4114
4115 if (ClangASTContext::FieldIsBitfield (ast, ivar_decl, child_bitfield_bit_size))
4116 {
4117 if (bit_offset == UINT32_MAX)
4118 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
4119
4120 child_bitfield_bit_offset = bit_offset % 8;
4121 }
4122 return ivar_qual_type.getAsOpaquePtr();
4123 }
4124 ++child_idx;
4125 }
Greg Clayton9e409562010-07-28 02:04:09 +00004126 }
4127 }
4128 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004129 }
4130 break;
4131
4132 case clang::Type::ObjCObjectPointer:
4133 if (idx_is_valid)
4134 {
4135 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr());
4136 QualType pointee_type = pointer_type->getPointeeType();
4137
4138 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
Greg Clayton9e409562010-07-28 02:04:09 +00004139 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004140 child_is_deref_of_parent = false;
4141 bool tmp_child_is_deref_of_parent = false;
4142 return GetChildClangTypeAtIndex (exe_ctx,
4143 ast,
4144 parent_name,
4145 pointer_type->getPointeeType().getAsOpaquePtr(),
4146 idx,
4147 transparent_pointers,
4148 omit_empty_base_classes,
4149 ignore_array_bounds,
4150 child_name,
4151 child_byte_size,
4152 child_byte_offset,
4153 child_bitfield_bit_size,
4154 child_bitfield_bit_offset,
4155 child_is_base_class,
4156 tmp_child_is_deref_of_parent);
4157 }
4158 else
4159 {
4160 child_is_deref_of_parent = true;
4161 if (parent_name)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004162 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004163 child_name.assign(1, '*');
4164 child_name += parent_name;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004165 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004166
Greg Clayton4ef877f2012-12-06 02:33:54 +00004167 // We have a pointer to an simple type
4168 if (idx == 0 && GetCompleteQualType(ast, pointee_type))
4169 {
4170 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
4171 assert(clang_type_info.first % 8 == 0);
4172 child_byte_size = clang_type_info.first / 8;
4173 child_byte_offset = 0;
4174 return pointee_type.getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004175 }
Greg Clayton9e409562010-07-28 02:04:09 +00004176 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004177 }
4178 break;
Greg Clayton9e409562010-07-28 02:04:09 +00004179
Greg Claytone1a916a2010-07-21 22:12:05 +00004180 case clang::Type::ConstantArray:
Greg Clayton4ef877f2012-12-06 02:33:54 +00004181 case clang::Type::IncompleteArray:
4182 if (ignore_array_bounds || idx_is_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004183 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004184 const ArrayType *array = cast<ArrayType>(parent_qual_type.getTypePtr());
4185 if (array)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004186 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004187 if (GetCompleteQualType (ast, array->getElementType()))
4188 {
4189 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Greg Clayton4ef877f2012-12-06 02:33:54 +00004190
Greg Clayton6beaaa62011-01-17 03:46:26 +00004191 char element_name[64];
Greg Claytonc7bece562013-01-25 18:06:21 +00004192 ::snprintf (element_name, sizeof (element_name), "[%zu]", idx);
Greg Clayton4ef877f2012-12-06 02:33:54 +00004193
Greg Clayton6beaaa62011-01-17 03:46:26 +00004194 child_name.assign(element_name);
4195 assert(field_type_info.first % 8 == 0);
4196 child_byte_size = field_type_info.first / 8;
Greg Claytondaf515f2011-07-09 20:12:33 +00004197 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
Greg Clayton6beaaa62011-01-17 03:46:26 +00004198 return array->getElementType().getAsOpaquePtr();
4199 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004200 }
4201 }
4202 break;
Greg Clayton4ef877f2012-12-06 02:33:54 +00004203
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004204
Greg Clayton4ef877f2012-12-06 02:33:54 +00004205 case clang::Type::Pointer:
4206 if (idx_is_valid)
4207 {
4208 const PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr());
4209 QualType pointee_type = pointer_type->getPointeeType();
4210
4211 // Don't dereference "void *" pointers
4212 if (pointee_type->isVoidType())
4213 return NULL;
4214
4215 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004216 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004217 child_is_deref_of_parent = false;
4218 bool tmp_child_is_deref_of_parent = false;
4219 return GetChildClangTypeAtIndex (exe_ctx,
4220 ast,
4221 parent_name,
4222 pointer_type->getPointeeType().getAsOpaquePtr(),
4223 idx,
4224 transparent_pointers,
4225 omit_empty_base_classes,
4226 ignore_array_bounds,
4227 child_name,
4228 child_byte_size,
4229 child_byte_offset,
4230 child_bitfield_bit_size,
4231 child_bitfield_bit_offset,
4232 child_is_base_class,
4233 tmp_child_is_deref_of_parent);
4234 }
4235 else
4236 {
4237 child_is_deref_of_parent = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004238
Greg Clayton4ef877f2012-12-06 02:33:54 +00004239 if (parent_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004240 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004241 child_name.assign(1, '*');
4242 child_name += parent_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004243 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004244
4245 // We have a pointer to an simple type
4246 if (idx == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004247 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004248 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
4249 assert(clang_type_info.first % 8 == 0);
4250 child_byte_size = clang_type_info.first / 8;
4251 child_byte_offset = 0;
4252 return pointee_type.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004253 }
4254 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004255 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004256 break;
4257
4258 case clang::Type::LValueReference:
4259 case clang::Type::RValueReference:
4260 if (idx_is_valid)
4261 {
4262 const ReferenceType *reference_type = cast<ReferenceType>(parent_qual_type.getTypePtr());
4263 QualType pointee_type(reference_type->getPointeeType());
4264 clang_type_t pointee_clang_type = pointee_type.getAsOpaquePtr();
4265 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_clang_type))
4266 {
4267 child_is_deref_of_parent = false;
4268 bool tmp_child_is_deref_of_parent = false;
4269 return GetChildClangTypeAtIndex (exe_ctx,
4270 ast,
4271 parent_name,
4272 pointee_clang_type,
4273 idx,
4274 transparent_pointers,
4275 omit_empty_base_classes,
4276 ignore_array_bounds,
4277 child_name,
4278 child_byte_size,
4279 child_byte_offset,
4280 child_bitfield_bit_size,
4281 child_bitfield_bit_offset,
4282 child_is_base_class,
4283 tmp_child_is_deref_of_parent);
4284 }
4285 else
4286 {
4287 if (parent_name)
4288 {
4289 child_name.assign(1, '&');
4290 child_name += parent_name;
4291 }
4292
4293 // We have a pointer to an simple type
4294 if (idx == 0)
4295 {
4296 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
4297 assert(clang_type_info.first % 8 == 0);
4298 child_byte_size = clang_type_info.first / 8;
4299 child_byte_offset = 0;
4300 return pointee_type.getAsOpaquePtr();
4301 }
4302 }
4303 }
4304 break;
4305
4306 case clang::Type::Typedef:
4307 return GetChildClangTypeAtIndex (exe_ctx,
4308 ast,
4309 parent_name,
4310 cast<TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
4311 idx,
4312 transparent_pointers,
4313 omit_empty_base_classes,
4314 ignore_array_bounds,
4315 child_name,
4316 child_byte_size,
4317 child_byte_offset,
4318 child_bitfield_bit_size,
4319 child_bitfield_bit_offset,
4320 child_is_base_class,
4321 child_is_deref_of_parent);
4322 break;
4323
4324 case clang::Type::Elaborated:
4325 return GetChildClangTypeAtIndex (exe_ctx,
4326 ast,
4327 parent_name,
4328 cast<ElaboratedType>(parent_qual_type)->getNamedType().getAsOpaquePtr(),
4329 idx,
4330 transparent_pointers,
4331 omit_empty_base_classes,
4332 ignore_array_bounds,
4333 child_name,
4334 child_byte_size,
4335 child_byte_offset,
4336 child_bitfield_bit_size,
4337 child_bitfield_bit_offset,
4338 child_is_base_class,
4339 child_is_deref_of_parent);
4340
4341 default:
4342 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004343 }
Greg Clayton19503a22010-07-23 15:37:46 +00004344 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004345}
4346
4347static inline bool
4348BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
4349{
Greg Clayton6beaaa62011-01-17 03:46:26 +00004350 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004351}
4352
4353static uint32_t
4354GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
4355{
4356 uint32_t num_bases = 0;
4357 if (cxx_record_decl)
4358 {
4359 if (omit_empty_base_classes)
4360 {
4361 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4362 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4363 base_class != base_class_end;
4364 ++base_class)
4365 {
4366 // Skip empty base classes
4367 if (omit_empty_base_classes)
4368 {
4369 if (BaseSpecifierIsEmpty (base_class))
4370 continue;
4371 }
4372 ++num_bases;
4373 }
4374 }
4375 else
4376 num_bases = cxx_record_decl->getNumBases();
4377 }
4378 return num_bases;
4379}
4380
4381
4382static uint32_t
4383GetIndexForRecordBase
4384(
4385 const RecordDecl *record_decl,
4386 const CXXBaseSpecifier *base_spec,
4387 bool omit_empty_base_classes
4388)
4389{
4390 uint32_t child_idx = 0;
4391
4392 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4393
4394// const char *super_name = record_decl->getNameAsCString();
4395// const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString();
4396// printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
4397//
4398 if (cxx_record_decl)
4399 {
4400 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4401 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4402 base_class != base_class_end;
4403 ++base_class)
4404 {
4405 if (omit_empty_base_classes)
4406 {
4407 if (BaseSpecifierIsEmpty (base_class))
4408 continue;
4409 }
4410
4411// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
4412// child_idx,
4413// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4414//
4415//
4416 if (base_class == base_spec)
4417 return child_idx;
4418 ++child_idx;
4419 }
4420 }
4421
4422 return UINT32_MAX;
4423}
4424
4425
4426static uint32_t
4427GetIndexForRecordChild
4428(
4429 const RecordDecl *record_decl,
4430 NamedDecl *canonical_decl,
4431 bool omit_empty_base_classes
4432)
4433{
4434 uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes);
4435
4436// const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4437//
4438//// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString());
4439// if (cxx_record_decl)
4440// {
4441// CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4442// for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4443// base_class != base_class_end;
4444// ++base_class)
4445// {
4446// if (omit_empty_base_classes)
4447// {
4448// if (BaseSpecifierIsEmpty (base_class))
4449// continue;
4450// }
4451//
4452//// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
4453//// record_decl->getNameAsCString(),
4454//// canonical_decl->getNameAsCString(),
4455//// child_idx,
4456//// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4457//
4458//
4459// CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4460// if (curr_base_class_decl == canonical_decl)
4461// {
4462// return child_idx;
4463// }
4464// ++child_idx;
4465// }
4466// }
4467//
4468// const uint32_t num_bases = child_idx;
4469 RecordDecl::field_iterator field, field_end;
4470 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4471 field != field_end;
4472 ++field, ++child_idx)
4473 {
4474// printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n",
4475// record_decl->getNameAsCString(),
4476// canonical_decl->getNameAsCString(),
4477// child_idx - num_bases,
4478// field->getNameAsCString());
4479
4480 if (field->getCanonicalDecl() == canonical_decl)
4481 return child_idx;
4482 }
4483
4484 return UINT32_MAX;
4485}
4486
4487// Look for a child member (doesn't include base classes, but it does include
4488// their members) in the type hierarchy. Returns an index path into "clang_type"
4489// on how to reach the appropriate member.
4490//
4491// class A
4492// {
4493// public:
4494// int m_a;
4495// int m_b;
4496// };
4497//
4498// class B
4499// {
4500// };
4501//
4502// class C :
4503// public B,
4504// public A
4505// {
4506// };
4507//
4508// If we have a clang type that describes "class C", and we wanted to looked
4509// "m_b" in it:
4510//
4511// With omit_empty_base_classes == false we would get an integer array back with:
4512// { 1, 1 }
4513// The first index 1 is the child index for "class A" within class C
4514// The second index 1 is the child index for "m_b" within class A
4515//
4516// With omit_empty_base_classes == true we would get an integer array back with:
4517// { 0, 1 }
4518// 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)
4519// The second index 1 is the child index for "m_b" within class A
4520
4521size_t
4522ClangASTContext::GetIndexOfChildMemberWithName
4523(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004524 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004525 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004526 const char *name,
4527 bool omit_empty_base_classes,
4528 std::vector<uint32_t>& child_indexes
4529)
4530{
4531 if (clang_type && name && name[0])
4532 {
4533 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00004534 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4535 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004536 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004537 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004538 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004539 {
4540 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4541 const RecordDecl *record_decl = record_type->getDecl();
4542
4543 assert(record_decl);
4544 uint32_t child_idx = 0;
4545
4546 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4547
4548 // Try and find a field that matches NAME
4549 RecordDecl::field_iterator field, field_end;
4550 StringRef name_sref(name);
4551 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4552 field != field_end;
4553 ++field, ++child_idx)
4554 {
4555 if (field->getName().equals (name_sref))
4556 {
4557 // We have to add on the number of base classes to this index!
4558 child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
4559 return child_indexes.size();
4560 }
4561 }
4562
4563 if (cxx_record_decl)
4564 {
4565 const RecordDecl *parent_record_decl = cxx_record_decl;
4566
4567 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
4568
4569 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
4570 // Didn't find things easily, lets let clang do its thang...
Sean Callanancc427fa2011-07-30 02:42:06 +00004571 IdentifierInfo & ident_ref = ast->Idents.get(name_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004572 DeclarationName decl_name(&ident_ref);
4573
4574 CXXBasePaths paths;
4575 if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember,
4576 decl_name.getAsOpaquePtr(),
4577 paths))
4578 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004579 CXXBasePaths::const_paths_iterator path, path_end = paths.end();
4580 for (path = paths.begin(); path != path_end; ++path)
4581 {
4582 const size_t num_path_elements = path->size();
4583 for (size_t e=0; e<num_path_elements; ++e)
4584 {
4585 CXXBasePathElement elem = (*path)[e];
4586
4587 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
4588 if (child_idx == UINT32_MAX)
4589 {
4590 child_indexes.clear();
4591 return 0;
4592 }
4593 else
4594 {
4595 child_indexes.push_back (child_idx);
4596 parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl());
4597 }
4598 }
Sean Callanan5deaa4c2012-12-21 21:34:42 +00004599 for (NamedDecl *path_decl : path->Decls)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004600 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00004601 child_idx = GetIndexForRecordChild (parent_record_decl, path_decl, omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004602 if (child_idx == UINT32_MAX)
4603 {
4604 child_indexes.clear();
4605 return 0;
4606 }
4607 else
4608 {
4609 child_indexes.push_back (child_idx);
4610 }
4611 }
4612 }
4613 return child_indexes.size();
4614 }
4615 }
4616
4617 }
4618 break;
4619
Greg Clayton9e409562010-07-28 02:04:09 +00004620 case clang::Type::ObjCObject:
4621 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004622 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004623 {
4624 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004625 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004626 assert (objc_class_type);
4627 if (objc_class_type)
4628 {
4629 uint32_t child_idx = 0;
4630 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4631
4632 if (class_interface_decl)
4633 {
4634 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4635 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4636
Greg Clayton6ba78152010-09-18 02:11:07 +00004637 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00004638 {
4639 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4640
4641 if (ivar_decl->getName().equals (name_sref))
4642 {
4643 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4644 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4645 ++child_idx;
4646
4647 child_indexes.push_back (child_idx);
4648 return child_indexes.size();
4649 }
4650 }
4651
4652 if (superclass_interface_decl)
4653 {
4654 // The super class index is always zero for ObjC classes,
4655 // so we push it onto the child indexes in case we find
4656 // an ivar in our superclass...
4657 child_indexes.push_back (0);
4658
Greg Clayton6beaaa62011-01-17 03:46:26 +00004659 if (GetIndexOfChildMemberWithName (ast,
4660 ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00004661 name,
4662 omit_empty_base_classes,
4663 child_indexes))
4664 {
4665 // We did find an ivar in a superclass so just
4666 // return the results!
4667 return child_indexes.size();
4668 }
4669
4670 // We didn't find an ivar matching "name" in our
4671 // superclass, pop the superclass zero index that
4672 // we pushed on above.
4673 child_indexes.pop_back();
4674 }
4675 }
4676 }
4677 }
4678 break;
4679
4680 case clang::Type::ObjCObjectPointer:
4681 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004682 return GetIndexOfChildMemberWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004683 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4684 name,
4685 omit_empty_base_classes,
4686 child_indexes);
4687 }
4688 break;
4689
4690
Greg Claytone1a916a2010-07-21 22:12:05 +00004691 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004692 {
4693// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4694// const uint64_t element_count = array->getSize().getLimitedValue();
4695//
4696// if (idx < element_count)
4697// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004698// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004699//
4700// char element_name[32];
4701// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4702//
4703// child_name.assign(element_name);
4704// assert(field_type_info.first % 8 == 0);
4705// child_byte_size = field_type_info.first / 8;
4706// child_byte_offset = idx * child_byte_size;
4707// return array->getElementType().getAsOpaquePtr();
4708// }
4709 }
4710 break;
4711
Greg Claytone1a916a2010-07-21 22:12:05 +00004712// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004713// {
4714// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4715// QualType pointee_type = mem_ptr_type->getPointeeType();
4716//
4717// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4718// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004719// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004720// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4721// name);
4722// }
4723// }
4724// break;
4725//
Greg Claytone1a916a2010-07-21 22:12:05 +00004726 case clang::Type::LValueReference:
4727 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004728 {
Sean Callanan78e37602011-01-27 04:42:51 +00004729 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004730 QualType pointee_type = reference_type->getPointeeType();
4731
4732 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4733 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004734 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004735 reference_type->getPointeeType().getAsOpaquePtr(),
4736 name,
4737 omit_empty_base_classes,
4738 child_indexes);
4739 }
4740 }
4741 break;
4742
Greg Claytone1a916a2010-07-21 22:12:05 +00004743 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004744 {
Sean Callanan78e37602011-01-27 04:42:51 +00004745 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004746 QualType pointee_type = pointer_type->getPointeeType();
4747
4748 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4749 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004750 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004751 pointer_type->getPointeeType().getAsOpaquePtr(),
4752 name,
4753 omit_empty_base_classes,
4754 child_indexes);
4755 }
4756 else
4757 {
4758// if (parent_name)
4759// {
4760// child_name.assign(1, '*');
4761// child_name += parent_name;
4762// }
4763//
4764// // We have a pointer to an simple type
4765// if (idx == 0)
4766// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004767// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004768// assert(clang_type_info.first % 8 == 0);
4769// child_byte_size = clang_type_info.first / 8;
4770// child_byte_offset = 0;
4771// return pointee_type.getAsOpaquePtr();
4772// }
4773 }
4774 }
4775 break;
4776
Greg Claytone1a916a2010-07-21 22:12:05 +00004777 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00004778 return GetIndexOfChildMemberWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00004779 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004780 name,
4781 omit_empty_base_classes,
4782 child_indexes);
4783
Greg Clayton4b63a5c2013-01-04 18:10:18 +00004784 case clang::Type::Elaborated:
4785 return GetIndexOfChildMemberWithName (ast,
4786 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
4787 name,
4788 omit_empty_base_classes,
4789 child_indexes);
4790
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004791 default:
4792 break;
4793 }
4794 }
4795 return 0;
4796}
4797
4798
4799// Get the index of the child of "clang_type" whose name matches. This function
4800// doesn't descend into the children, but only looks one level deep and name
4801// matches can include base class names.
4802
4803uint32_t
4804ClangASTContext::GetIndexOfChildWithName
4805(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004806 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004807 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004808 const char *name,
4809 bool omit_empty_base_classes
4810)
4811{
4812 if (clang_type && name && name[0])
4813 {
4814 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00004815
Greg Clayton737b9322010-09-13 03:32:57 +00004816 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Clayton9e409562010-07-28 02:04:09 +00004817
Greg Clayton737b9322010-09-13 03:32:57 +00004818 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004819 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004820 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004821 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004822 {
4823 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4824 const RecordDecl *record_decl = record_type->getDecl();
4825
4826 assert(record_decl);
4827 uint32_t child_idx = 0;
4828
4829 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4830
4831 if (cxx_record_decl)
4832 {
4833 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4834 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4835 base_class != base_class_end;
4836 ++base_class)
4837 {
4838 // Skip empty base classes
4839 CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4840 if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false)
4841 continue;
4842
Greg Clayton84db9102012-03-26 23:03:23 +00004843 std::string base_class_type_name (ClangASTType::GetTypeNameForQualType(ast, base_class->getType()));
Greg Claytone3055942011-06-30 02:28:26 +00004844 if (base_class_type_name.compare (name) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004845 return child_idx;
4846 ++child_idx;
4847 }
4848 }
4849
4850 // Try and find a field that matches NAME
4851 RecordDecl::field_iterator field, field_end;
4852 StringRef name_sref(name);
4853 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4854 field != field_end;
4855 ++field, ++child_idx)
4856 {
4857 if (field->getName().equals (name_sref))
4858 return child_idx;
4859 }
4860
4861 }
4862 break;
4863
Greg Clayton9e409562010-07-28 02:04:09 +00004864 case clang::Type::ObjCObject:
4865 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004866 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004867 {
4868 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004869 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004870 assert (objc_class_type);
4871 if (objc_class_type)
4872 {
4873 uint32_t child_idx = 0;
4874 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4875
4876 if (class_interface_decl)
4877 {
4878 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4879 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4880
Jim Ingham2f355a72012-10-04 22:22:16 +00004881 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00004882 {
4883 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4884
4885 if (ivar_decl->getName().equals (name_sref))
4886 {
4887 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4888 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4889 ++child_idx;
4890
4891 return child_idx;
4892 }
4893 }
4894
4895 if (superclass_interface_decl)
4896 {
4897 if (superclass_interface_decl->getName().equals (name_sref))
4898 return 0;
4899 }
4900 }
4901 }
4902 }
4903 break;
4904
4905 case clang::Type::ObjCObjectPointer:
4906 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004907 return GetIndexOfChildWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004908 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4909 name,
4910 omit_empty_base_classes);
4911 }
4912 break;
4913
Greg Claytone1a916a2010-07-21 22:12:05 +00004914 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004915 {
4916// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4917// const uint64_t element_count = array->getSize().getLimitedValue();
4918//
4919// if (idx < element_count)
4920// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004921// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004922//
4923// char element_name[32];
4924// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4925//
4926// child_name.assign(element_name);
4927// assert(field_type_info.first % 8 == 0);
4928// child_byte_size = field_type_info.first / 8;
4929// child_byte_offset = idx * child_byte_size;
4930// return array->getElementType().getAsOpaquePtr();
4931// }
4932 }
4933 break;
4934
Greg Claytone1a916a2010-07-21 22:12:05 +00004935// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004936// {
4937// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4938// QualType pointee_type = mem_ptr_type->getPointeeType();
4939//
4940// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4941// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004942// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004943// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4944// name);
4945// }
4946// }
4947// break;
4948//
Greg Claytone1a916a2010-07-21 22:12:05 +00004949 case clang::Type::LValueReference:
4950 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004951 {
Sean Callanan78e37602011-01-27 04:42:51 +00004952 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004953 QualType pointee_type = reference_type->getPointeeType();
4954
4955 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4956 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004957 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004958 reference_type->getPointeeType().getAsOpaquePtr(),
4959 name,
4960 omit_empty_base_classes);
4961 }
4962 }
4963 break;
4964
Greg Claytone1a916a2010-07-21 22:12:05 +00004965 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004966 {
Sean Callanan78e37602011-01-27 04:42:51 +00004967 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004968 QualType pointee_type = pointer_type->getPointeeType();
4969
4970 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4971 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004972 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004973 pointer_type->getPointeeType().getAsOpaquePtr(),
4974 name,
4975 omit_empty_base_classes);
4976 }
4977 else
4978 {
4979// if (parent_name)
4980// {
4981// child_name.assign(1, '*');
4982// child_name += parent_name;
4983// }
4984//
4985// // We have a pointer to an simple type
4986// if (idx == 0)
4987// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004988// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004989// assert(clang_type_info.first % 8 == 0);
4990// child_byte_size = clang_type_info.first / 8;
4991// child_byte_offset = 0;
4992// return pointee_type.getAsOpaquePtr();
4993// }
4994 }
4995 }
4996 break;
4997
Greg Clayton4b63a5c2013-01-04 18:10:18 +00004998 case clang::Type::Elaborated:
4999 return GetIndexOfChildWithName (ast,
5000 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
5001 name,
5002 omit_empty_base_classes);
5003
Greg Claytone1a916a2010-07-21 22:12:05 +00005004 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00005005 return GetIndexOfChildWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00005006 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005007 name,
5008 omit_empty_base_classes);
5009
5010 default:
5011 break;
5012 }
5013 }
5014 return UINT32_MAX;
5015}
5016
5017#pragma mark TagType
5018
5019bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005020ClangASTContext::SetTagTypeKind (clang_type_t tag_clang_type, int kind)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005021{
5022 if (tag_clang_type)
5023 {
5024 QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00005025 const clang::Type *clang_type = tag_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005026 if (clang_type)
5027 {
Sean Callanan78e37602011-01-27 04:42:51 +00005028 const TagType *tag_type = dyn_cast<TagType>(clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005029 if (tag_type)
5030 {
5031 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl());
5032 if (tag_decl)
5033 {
5034 tag_decl->setTagKind ((TagDecl::TagKind)kind);
5035 return true;
5036 }
5037 }
5038 }
5039 }
5040 return false;
5041}
5042
5043
5044#pragma mark DeclContext Functions
5045
5046DeclContext *
Greg Clayton1be10fc2010-09-29 01:12:09 +00005047ClangASTContext::GetDeclContextForType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005048{
5049 if (clang_type == NULL)
5050 return NULL;
5051
5052 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005053 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5054 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005055 {
Sean Callanancc427fa2011-07-30 02:42:06 +00005056 case clang::Type::UnaryTransform: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005057 case clang::Type::FunctionNoProto: break;
5058 case clang::Type::FunctionProto: break;
5059 case clang::Type::IncompleteArray: break;
5060 case clang::Type::VariableArray: break;
5061 case clang::Type::ConstantArray: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005062 case clang::Type::DependentSizedArray: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005063 case clang::Type::ExtVector: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005064 case clang::Type::DependentSizedExtVector: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005065 case clang::Type::Vector: break;
5066 case clang::Type::Builtin: break;
5067 case clang::Type::BlockPointer: break;
5068 case clang::Type::Pointer: break;
5069 case clang::Type::LValueReference: break;
5070 case clang::Type::RValueReference: break;
5071 case clang::Type::MemberPointer: break;
5072 case clang::Type::Complex: break;
5073 case clang::Type::ObjCObject: break;
5074 case clang::Type::ObjCInterface: return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface();
5075 case clang::Type::ObjCObjectPointer: return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr());
5076 case clang::Type::Record: return cast<RecordType>(qual_type)->getDecl();
5077 case clang::Type::Enum: return cast<EnumType>(qual_type)->getDecl();
Sean Callanan48114472010-12-13 01:26:27 +00005078 case clang::Type::Typedef: return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00005079 case clang::Type::Elaborated: return ClangASTContext::GetDeclContextForType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00005080 case clang::Type::TypeOfExpr: break;
5081 case clang::Type::TypeOf: break;
5082 case clang::Type::Decltype: break;
5083 //case clang::Type::QualifiedName: break;
5084 case clang::Type::TemplateSpecialization: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005085 case clang::Type::DependentTemplateSpecialization: break;
5086 case clang::Type::TemplateTypeParm: break;
5087 case clang::Type::SubstTemplateTypeParm: break;
5088 case clang::Type::SubstTemplateTypeParmPack:break;
5089 case clang::Type::PackExpansion: break;
5090 case clang::Type::UnresolvedUsing: break;
5091 case clang::Type::Paren: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005092 case clang::Type::Attributed: break;
5093 case clang::Type::Auto: break;
5094 case clang::Type::InjectedClassName: break;
5095 case clang::Type::DependentName: break;
Greg Claytonea3e7d52011-10-08 00:49:15 +00005096 case clang::Type::Atomic: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005097 }
5098 // No DeclContext in this type...
5099 return NULL;
5100}
5101
5102#pragma mark Namespace Declarations
5103
5104NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00005105ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005106{
Greg Clayton030a2042011-10-14 21:34:45 +00005107 NamespaceDecl *namespace_decl = NULL;
Greg Clayton9d3d6882011-10-31 23:51:19 +00005108 ASTContext *ast = getASTContext();
5109 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
5110 if (decl_ctx == NULL)
5111 decl_ctx = translation_unit_decl;
5112
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005113 if (name)
5114 {
Greg Clayton030a2042011-10-14 21:34:45 +00005115 IdentifierInfo &identifier_info = ast->Idents.get(name);
5116 DeclarationName decl_name (&identifier_info);
5117 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00005118 for (NamedDecl *decl : result)
Greg Clayton030a2042011-10-14 21:34:45 +00005119 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00005120 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
Greg Clayton030a2042011-10-14 21:34:45 +00005121 if (namespace_decl)
5122 return namespace_decl;
5123 }
5124
Sean Callanan5b26f272012-02-04 08:49:35 +00005125 namespace_decl = NamespaceDecl::Create(*ast,
5126 decl_ctx,
5127 false,
5128 SourceLocation(),
5129 SourceLocation(),
5130 &identifier_info,
5131 NULL);
Greg Clayton030a2042011-10-14 21:34:45 +00005132
Greg Clayton9d3d6882011-10-31 23:51:19 +00005133 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005134 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00005135 else
5136 {
5137 if (decl_ctx == translation_unit_decl)
5138 {
5139 namespace_decl = translation_unit_decl->getAnonymousNamespace();
5140 if (namespace_decl)
5141 return namespace_decl;
5142
Sean Callanan5b26f272012-02-04 08:49:35 +00005143 namespace_decl = NamespaceDecl::Create(*ast,
5144 decl_ctx,
5145 false,
5146 SourceLocation(),
5147 SourceLocation(),
5148 NULL,
5149 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00005150 translation_unit_decl->setAnonymousNamespace (namespace_decl);
5151 translation_unit_decl->addDecl (namespace_decl);
5152 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
5153 }
5154 else
5155 {
5156 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
5157 if (parent_namespace_decl)
5158 {
5159 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
5160 if (namespace_decl)
5161 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00005162 namespace_decl = NamespaceDecl::Create(*ast,
5163 decl_ctx,
5164 false,
5165 SourceLocation(),
5166 SourceLocation(),
5167 NULL,
5168 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00005169 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
5170 parent_namespace_decl->addDecl (namespace_decl);
5171 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
5172 }
5173 else
5174 {
5175 // BAD!!!
5176 }
5177 }
5178
5179
5180 if (namespace_decl)
5181 {
5182 // If we make it here, we are creating the anonymous namespace decl
5183 // for the first time, so we need to do the using directive magic
5184 // like SEMA does
5185 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast,
5186 decl_ctx,
5187 SourceLocation(),
5188 SourceLocation(),
5189 NestedNameSpecifierLoc(),
5190 SourceLocation(),
5191 namespace_decl,
5192 decl_ctx);
5193 using_directive_decl->setImplicit();
5194 decl_ctx->addDecl(using_directive_decl);
5195 }
5196 }
5197#ifdef LLDB_CONFIGURATION_DEBUG
5198 VerifyDecl(namespace_decl);
5199#endif
Greg Clayton030a2042011-10-14 21:34:45 +00005200 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005201}
5202
5203
5204#pragma mark Function Types
5205
5206FunctionDecl *
Greg Clayton147e1fa2011-10-14 22:47:18 +00005207ClangASTContext::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 +00005208{
Greg Clayton147e1fa2011-10-14 22:47:18 +00005209 FunctionDecl *func_decl = NULL;
5210 ASTContext *ast = getASTContext();
5211 if (decl_ctx == NULL)
5212 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005213
Greg Clayton147e1fa2011-10-14 22:47:18 +00005214 if (name && name[0])
5215 {
5216 func_decl = FunctionDecl::Create (*ast,
5217 decl_ctx,
5218 SourceLocation(),
5219 SourceLocation(),
5220 DeclarationName (&ast->Idents.get(name)),
5221 QualType::getFromOpaquePtr(function_clang_type),
5222 NULL,
5223 (FunctionDecl::StorageClass)storage,
5224 (FunctionDecl::StorageClass)storage,
5225 is_inline);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005226 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00005227 else
5228 {
5229 func_decl = FunctionDecl::Create (*ast,
5230 decl_ctx,
5231 SourceLocation(),
5232 SourceLocation(),
5233 DeclarationName (),
5234 QualType::getFromOpaquePtr(function_clang_type),
5235 NULL,
5236 (FunctionDecl::StorageClass)storage,
5237 (FunctionDecl::StorageClass)storage,
5238 is_inline);
5239 }
5240 if (func_decl)
5241 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005242
5243#ifdef LLDB_CONFIGURATION_DEBUG
5244 VerifyDecl(func_decl);
5245#endif
5246
Greg Clayton147e1fa2011-10-14 22:47:18 +00005247 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005248}
5249
Greg Clayton1be10fc2010-09-29 01:12:09 +00005250clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00005251ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00005252 clang_type_t result_type,
5253 clang_type_t *args,
Sean Callananc81256a2010-09-16 20:40:25 +00005254 unsigned num_args,
5255 bool is_variadic,
5256 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005257{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005258 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005259 std::vector<QualType> qual_type_args;
5260 for (unsigned i=0; i<num_args; ++i)
5261 qual_type_args.push_back (QualType::getFromOpaquePtr(args[i]));
5262
5263 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00005264 FunctionProtoType::ExtProtoInfo proto_info;
5265 proto_info.Variadic = is_variadic;
Sean Callananfb0b7582011-03-15 00:17:19 +00005266 proto_info.ExceptionSpecType = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005267 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00005268 proto_info.RefQualifier = RQ_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005269 proto_info.NumExceptions = 0;
5270 proto_info.Exceptions = NULL;
5271
Greg Clayton147e1fa2011-10-14 22:47:18 +00005272 return ast->getFunctionType (QualType::getFromOpaquePtr(result_type),
5273 qual_type_args.empty() ? NULL : &qual_type_args.front(),
5274 qual_type_args.size(),
5275 proto_info).getAsOpaquePtr(); // NoReturn);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005276}
5277
5278ParmVarDecl *
Greg Clayton1be10fc2010-09-29 01:12:09 +00005279ClangASTContext::CreateParameterDeclaration (const char *name, clang_type_t param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005280{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005281 ASTContext *ast = getASTContext();
5282 assert (ast != NULL);
5283 return ParmVarDecl::Create(*ast,
5284 ast->getTranslationUnitDecl(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005285 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005286 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00005287 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callananc81256a2010-09-16 20:40:25 +00005288 QualType::getFromOpaquePtr(param_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005289 NULL,
5290 (VarDecl::StorageClass)storage,
5291 (VarDecl::StorageClass)storage,
5292 0);
5293}
5294
5295void
5296ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
5297{
5298 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00005299 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005300}
5301
5302
5303#pragma mark Array Types
5304
Greg Clayton1be10fc2010-09-29 01:12:09 +00005305clang_type_t
Greg Clayton4ef877f2012-12-06 02:33:54 +00005306ClangASTContext::CreateArrayType (clang_type_t element_type, size_t element_count)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005307{
5308 if (element_type)
5309 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00005310 ASTContext *ast = getASTContext();
5311 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005312 llvm::APInt ap_element_count (64, element_count);
Greg Clayton4ef877f2012-12-06 02:33:54 +00005313 if (element_count == 0)
5314 {
5315 return ast->getIncompleteArrayType(QualType::getFromOpaquePtr(element_type),
5316 ArrayType::Normal,
5317 0).getAsOpaquePtr();
5318
5319 }
5320 else
5321 {
5322 return ast->getConstantArrayType(QualType::getFromOpaquePtr(element_type),
5323 ap_element_count,
5324 ArrayType::Normal,
5325 0).getAsOpaquePtr(); // ElemQuals
5326 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005327 }
5328 return NULL;
5329}
5330
5331
5332#pragma mark TagDecl
5333
5334bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005335ClangASTContext::StartTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005336{
5337 if (clang_type)
5338 {
5339 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00005340 const clang::Type *t = qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005341 if (t)
5342 {
Sean Callanan78e37602011-01-27 04:42:51 +00005343 const TagType *tag_type = dyn_cast<TagType>(t);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005344 if (tag_type)
5345 {
5346 TagDecl *tag_decl = tag_type->getDecl();
5347 if (tag_decl)
5348 {
5349 tag_decl->startDefinition();
5350 return true;
5351 }
5352 }
Sean Callanan5b26f272012-02-04 08:49:35 +00005353
5354 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(t);
5355 if (object_type)
5356 {
5357 ObjCInterfaceDecl *interface_decl = object_type->getInterface();
5358 if (interface_decl)
5359 {
5360 interface_decl->startDefinition();
5361 return true;
5362 }
5363 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005364 }
5365 }
5366 return false;
5367}
5368
5369bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005370ClangASTContext::CompleteTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005371{
5372 if (clang_type)
5373 {
5374 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton14372242010-09-29 03:44:17 +00005375
5376 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5377
5378 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005379 {
Greg Clayton14372242010-09-29 03:44:17 +00005380 cxx_record_decl->completeDefinition();
5381
5382 return true;
5383 }
5384
5385 const EnumType *enum_type = dyn_cast<EnumType>(qual_type.getTypePtr());
5386
5387 if (enum_type)
5388 {
5389 EnumDecl *enum_decl = enum_type->getDecl();
5390
5391 if (enum_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005392 {
Greg Clayton14372242010-09-29 03:44:17 +00005393 /// TODO This really needs to be fixed.
5394
5395 unsigned NumPositiveBits = 1;
5396 unsigned NumNegativeBits = 0;
5397
Greg Clayton6beaaa62011-01-17 03:46:26 +00005398 ASTContext *ast = getASTContext();
Greg Claytone02b8502010-10-12 04:29:14 +00005399
5400 QualType promotion_qual_type;
5401 // If the enum integer type is less than an integer in bit width,
5402 // then we must promote it to an integer size.
Greg Clayton6beaaa62011-01-17 03:46:26 +00005403 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
Greg Claytone02b8502010-10-12 04:29:14 +00005404 {
5405 if (enum_decl->getIntegerType()->isSignedIntegerType())
Greg Clayton6beaaa62011-01-17 03:46:26 +00005406 promotion_qual_type = ast->IntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005407 else
Greg Clayton6beaaa62011-01-17 03:46:26 +00005408 promotion_qual_type = ast->UnsignedIntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005409 }
5410 else
5411 promotion_qual_type = enum_decl->getIntegerType();
5412
5413 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
Greg Clayton14372242010-09-29 03:44:17 +00005414 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005415 }
5416 }
5417 }
5418 return false;
5419}
5420
5421
5422#pragma mark Enumeration Types
5423
Greg Clayton1be10fc2010-09-29 01:12:09 +00005424clang_type_t
Greg Claytonca512b32011-01-14 04:54:56 +00005425ClangASTContext::CreateEnumerationType
5426(
5427 const char *name,
5428 DeclContext *decl_ctx,
5429 const Declaration &decl,
5430 clang_type_t integer_qual_type
5431)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005432{
5433 // TODO: Do something intelligent with the Declaration object passed in
5434 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005435 ASTContext *ast = getASTContext();
5436 assert (ast != NULL);
Greg Claytone02b8502010-10-12 04:29:14 +00005437
5438 // TODO: ask about these...
5439// const bool IsScoped = false;
5440// const bool IsFixed = false;
5441
Greg Clayton6beaaa62011-01-17 03:46:26 +00005442 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00005443 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00005444 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00005445 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005446 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callanan48114472010-12-13 01:26:27 +00005447 NULL,
5448 false, // IsScoped
5449 false, // IsScopedUsingClassTag
5450 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00005451
5452
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005453 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00005454 {
5455 // TODO: check if we should be setting the promotion type too?
5456 enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00005457
5458 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
5459
Greg Clayton6beaaa62011-01-17 03:46:26 +00005460 return ast->getTagDeclType(enum_decl).getAsOpaquePtr();
Greg Clayton83ff3892010-09-12 23:17:56 +00005461 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005462 return NULL;
5463}
5464
Greg Clayton1be10fc2010-09-29 01:12:09 +00005465clang_type_t
5466ClangASTContext::GetEnumerationIntegerType (clang_type_t enum_clang_type)
5467{
5468 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5469
Sean Callanan78e37602011-01-27 04:42:51 +00005470 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Greg Clayton1be10fc2010-09-29 01:12:09 +00005471 if (clang_type)
5472 {
5473 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5474 if (enum_type)
5475 {
5476 EnumDecl *enum_decl = enum_type->getDecl();
5477 if (enum_decl)
5478 return enum_decl->getIntegerType().getAsOpaquePtr();
5479 }
5480 }
5481 return NULL;
5482}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005483bool
5484ClangASTContext::AddEnumerationValueToEnumerationType
5485(
Greg Clayton1be10fc2010-09-29 01:12:09 +00005486 clang_type_t enum_clang_type,
5487 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005488 const Declaration &decl,
5489 const char *name,
5490 int64_t enum_value,
5491 uint32_t enum_value_bit_size
5492)
5493{
5494 if (enum_clang_type && enumerator_clang_type && name)
5495 {
5496 // TODO: Do something intelligent with the Declaration object passed in
5497 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005498 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005499 IdentifierTable *identifier_table = getIdentifierTable();
5500
Greg Clayton6beaaa62011-01-17 03:46:26 +00005501 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005502 assert (identifier_table != NULL);
5503 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5504
Greg Clayton3e067532013-03-05 23:54:39 +00005505 bool is_signed = false;
5506 IsIntegerType (enumerator_clang_type, is_signed);
Sean Callanan78e37602011-01-27 04:42:51 +00005507 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005508 if (clang_type)
5509 {
5510 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5511
5512 if (enum_type)
5513 {
Greg Clayton3e067532013-03-05 23:54:39 +00005514 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005515 enum_llvm_apsint = enum_value;
5516 EnumConstantDecl *enumerator_decl =
Greg Clayton6beaaa62011-01-17 03:46:26 +00005517 EnumConstantDecl::Create (*ast,
5518 enum_type->getDecl(),
5519 SourceLocation(),
5520 name ? &identifier_table->get(name) : NULL, // Identifier
5521 QualType::getFromOpaquePtr(enumerator_clang_type),
5522 NULL,
5523 enum_llvm_apsint);
5524
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005525 if (enumerator_decl)
5526 {
5527 enum_type->getDecl()->addDecl(enumerator_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005528
5529#ifdef LLDB_CONFIGURATION_DEBUG
5530 VerifyDecl(enumerator_decl);
5531#endif
5532
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005533 return true;
5534 }
5535 }
5536 }
5537 }
5538 return false;
5539}
5540
5541#pragma mark Pointers & References
5542
Greg Clayton1be10fc2010-09-29 01:12:09 +00005543clang_type_t
5544ClangASTContext::CreatePointerType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005545{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005546 return CreatePointerType (getASTContext(), clang_type);
5547}
5548
5549clang_type_t
5550ClangASTContext::CreatePointerType (clang::ASTContext *ast, clang_type_t clang_type)
5551{
5552 if (ast && clang_type)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005553 {
5554 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5555
Greg Clayton737b9322010-09-13 03:32:57 +00005556 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5557 switch (type_class)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005558 {
5559 case clang::Type::ObjCObject:
5560 case clang::Type::ObjCInterface:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005561 return ast->getObjCObjectPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005562
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005563 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005564 return ast->getPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005565 }
5566 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005567 return NULL;
5568}
5569
Greg Clayton1be10fc2010-09-29 01:12:09 +00005570clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005571ClangASTContext::CreateLValueReferenceType (clang::ASTContext *ast,
5572 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005573{
5574 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005575 return ast->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005576 return NULL;
5577}
5578
Greg Clayton1be10fc2010-09-29 01:12:09 +00005579clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005580ClangASTContext::CreateRValueReferenceType (clang::ASTContext *ast,
5581 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005582{
5583 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005584 return ast->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005585 return NULL;
5586}
5587
Greg Clayton1be10fc2010-09-29 01:12:09 +00005588clang_type_t
5589ClangASTContext::CreateMemberPointerType (clang_type_t clang_pointee_type, clang_type_t clang_class_type)
Greg Clayton9b81a312010-06-12 01:20:30 +00005590{
5591 if (clang_pointee_type && clang_pointee_type)
5592 return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type),
5593 QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr();
5594 return NULL;
5595}
5596
Greg Clayton1a65ae12011-01-25 23:55:37 +00005597uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005598ClangASTContext::GetPointerBitSize ()
5599{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005600 ASTContext *ast = getASTContext();
5601 return ast->getTypeSize(ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005602}
5603
5604bool
Greg Clayton219cf312012-03-30 00:51:13 +00005605ClangASTContext::IsPossibleDynamicType (clang::ASTContext *ast,
5606 clang_type_t clang_type,
5607 clang_type_t *dynamic_pointee_type,
5608 bool check_cplusplus,
5609 bool check_objc)
Greg Claytondea8cb42011-06-29 22:09:02 +00005610{
5611 QualType pointee_qual_type;
5612 if (clang_type)
5613 {
5614 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5615 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5616 bool success = false;
5617 switch (type_class)
5618 {
5619 case clang::Type::Builtin:
Greg Clayton219cf312012-03-30 00:51:13 +00005620 if (check_objc && cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
Greg Claytondea8cb42011-06-29 22:09:02 +00005621 {
5622 if (dynamic_pointee_type)
5623 *dynamic_pointee_type = clang_type;
5624 return true;
5625 }
5626 break;
5627
5628 case clang::Type::ObjCObjectPointer:
Greg Clayton219cf312012-03-30 00:51:13 +00005629 if (check_objc)
5630 {
5631 if (dynamic_pointee_type)
5632 *dynamic_pointee_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5633 return true;
5634 }
5635 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005636
5637 case clang::Type::Pointer:
5638 pointee_qual_type = cast<PointerType>(qual_type)->getPointeeType();
5639 success = true;
5640 break;
5641
5642 case clang::Type::LValueReference:
5643 case clang::Type::RValueReference:
5644 pointee_qual_type = cast<ReferenceType>(qual_type)->getPointeeType();
5645 success = true;
5646 break;
5647
5648 case clang::Type::Typedef:
Greg Clayton70364252012-08-31 18:56:24 +00005649 return ClangASTContext::IsPossibleDynamicType (ast,
5650 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
5651 dynamic_pointee_type,
5652 check_cplusplus,
5653 check_objc);
Sean Callanan912855f2011-08-11 23:56:13 +00005654
5655 case clang::Type::Elaborated:
Greg Clayton70364252012-08-31 18:56:24 +00005656 return ClangASTContext::IsPossibleDynamicType (ast,
5657 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
5658 dynamic_pointee_type,
5659 check_cplusplus,
5660 check_objc);
Sean Callanan912855f2011-08-11 23:56:13 +00005661
Greg Claytondea8cb42011-06-29 22:09:02 +00005662 default:
5663 break;
5664 }
5665
5666 if (success)
5667 {
5668 // Check to make sure what we are pointing too is a possible dynamic C++ type
5669 // We currently accept any "void *" (in case we have a class that has been
5670 // watered down to an opaque pointer) and virtual C++ classes.
5671 const clang::Type::TypeClass pointee_type_class = pointee_qual_type->getTypeClass();
5672 switch (pointee_type_class)
5673 {
5674 case clang::Type::Builtin:
5675 switch (cast<clang::BuiltinType>(pointee_qual_type)->getKind())
5676 {
5677 case clang::BuiltinType::UnknownAny:
5678 case clang::BuiltinType::Void:
5679 if (dynamic_pointee_type)
5680 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5681 return true;
5682
5683 case clang::BuiltinType::NullPtr:
5684 case clang::BuiltinType::Bool:
5685 case clang::BuiltinType::Char_U:
5686 case clang::BuiltinType::UChar:
5687 case clang::BuiltinType::WChar_U:
5688 case clang::BuiltinType::Char16:
5689 case clang::BuiltinType::Char32:
5690 case clang::BuiltinType::UShort:
5691 case clang::BuiltinType::UInt:
5692 case clang::BuiltinType::ULong:
5693 case clang::BuiltinType::ULongLong:
5694 case clang::BuiltinType::UInt128:
5695 case clang::BuiltinType::Char_S:
5696 case clang::BuiltinType::SChar:
5697 case clang::BuiltinType::WChar_S:
5698 case clang::BuiltinType::Short:
5699 case clang::BuiltinType::Int:
5700 case clang::BuiltinType::Long:
5701 case clang::BuiltinType::LongLong:
5702 case clang::BuiltinType::Int128:
5703 case clang::BuiltinType::Float:
5704 case clang::BuiltinType::Double:
5705 case clang::BuiltinType::LongDouble:
5706 case clang::BuiltinType::Dependent:
5707 case clang::BuiltinType::Overload:
5708 case clang::BuiltinType::ObjCId:
5709 case clang::BuiltinType::ObjCClass:
5710 case clang::BuiltinType::ObjCSel:
5711 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00005712 case clang::BuiltinType::Half:
5713 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00005714 case clang::BuiltinType::PseudoObject:
Sean Callanan3d654b32012-09-24 22:25:51 +00005715 case clang::BuiltinType::BuiltinFn:
Greg Clayton3dcaa2c2013-02-01 00:46:49 +00005716 case clang::BuiltinType::OCLEvent:
5717 case clang::BuiltinType::OCLImage1d:
5718 case clang::BuiltinType::OCLImage1dArray:
5719 case clang::BuiltinType::OCLImage1dBuffer:
5720 case clang::BuiltinType::OCLImage2d:
5721 case clang::BuiltinType::OCLImage2dArray:
5722 case clang::BuiltinType::OCLImage3d:
Greg Clayton142c5a42013-02-13 18:15:56 +00005723 case clang::BuiltinType::OCLSampler:
Greg Claytondea8cb42011-06-29 22:09:02 +00005724 break;
5725 }
5726 break;
5727
5728 case clang::Type::Record:
Greg Clayton219cf312012-03-30 00:51:13 +00005729 if (check_cplusplus)
Greg Claytondea8cb42011-06-29 22:09:02 +00005730 {
5731 CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
5732 if (cxx_record_decl)
5733 {
Greg Clayton70364252012-08-31 18:56:24 +00005734 bool is_complete = cxx_record_decl->isCompleteDefinition();
5735 if (!is_complete)
Sean Callanan6e4100ea2012-09-08 00:49:45 +00005736 is_complete = ClangASTContext::GetCompleteType (ast, pointee_qual_type.getAsOpaquePtr());
Greg Clayton70364252012-08-31 18:56:24 +00005737
5738 if (is_complete)
Greg Claytondea8cb42011-06-29 22:09:02 +00005739 {
5740 success = cxx_record_decl->isDynamicClass();
5741 }
5742 else
5743 {
Greg Clayton70364252012-08-31 18:56:24 +00005744 success = false;
Greg Claytondea8cb42011-06-29 22:09:02 +00005745 }
Greg Clayton70364252012-08-31 18:56:24 +00005746
Greg Claytondea8cb42011-06-29 22:09:02 +00005747 if (success)
5748 {
5749 if (dynamic_pointee_type)
5750 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5751 return true;
5752 }
5753 }
5754 }
5755 break;
5756
5757 case clang::Type::ObjCObject:
5758 case clang::Type::ObjCInterface:
Greg Clayton219cf312012-03-30 00:51:13 +00005759 if (check_objc)
5760 {
5761 if (dynamic_pointee_type)
5762 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5763 return true;
5764 }
5765 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005766
5767 default:
5768 break;
5769 }
5770 }
5771 }
5772 if (dynamic_pointee_type)
5773 *dynamic_pointee_type = NULL;
5774 return false;
5775}
5776
5777
5778bool
Greg Clayton007d5be2011-05-30 00:49:24 +00005779ClangASTContext::IsPossibleCPlusPlusDynamicType (clang::ASTContext *ast, clang_type_t clang_type, clang_type_t *dynamic_pointee_type)
5780{
Greg Clayton219cf312012-03-30 00:51:13 +00005781 return IsPossibleDynamicType (ast,
5782 clang_type,
5783 dynamic_pointee_type,
5784 true, // Check for dynamic C++ types
5785 false); // Check for dynamic ObjC types
Greg Clayton007d5be2011-05-30 00:49:24 +00005786}
5787
Sean Callanan98298012011-10-27 19:41:13 +00005788bool
5789ClangASTContext::IsReferenceType (clang_type_t clang_type, clang_type_t *target_type)
5790{
5791 if (clang_type == NULL)
5792 return false;
5793
5794 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5795 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5796
5797 switch (type_class)
5798 {
5799 case clang::Type::LValueReference:
5800 if (target_type)
5801 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5802 return true;
5803 case clang::Type::RValueReference:
5804 if (target_type)
5805 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5806 return true;
5807 case clang::Type::Typedef:
5808 return ClangASTContext::IsReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5809 case clang::Type::Elaborated:
5810 return ClangASTContext::IsReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5811 default:
5812 break;
5813 }
5814
5815 return false;
5816}
Greg Clayton007d5be2011-05-30 00:49:24 +00005817
5818bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005819ClangASTContext::IsPointerOrReferenceType (clang_type_t clang_type, clang_type_t*target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005820{
5821 if (clang_type == NULL)
5822 return false;
5823
5824 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005825 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5826 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005827 {
Sean Callanana2424172010-10-25 00:29:48 +00005828 case clang::Type::Builtin:
5829 switch (cast<clang::BuiltinType>(qual_type)->getKind())
5830 {
5831 default:
5832 break;
5833 case clang::BuiltinType::ObjCId:
5834 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00005835 return true;
5836 }
5837 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00005838 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005839 if (target_type)
5840 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5841 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005842 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005843 if (target_type)
5844 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5845 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005846 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005847 if (target_type)
5848 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5849 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005850 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005851 if (target_type)
5852 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5853 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005854 case clang::Type::LValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005855 if (target_type)
5856 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5857 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005858 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005859 if (target_type)
5860 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5861 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005862 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00005863 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00005864 case clang::Type::Elaborated:
5865 return ClangASTContext::IsPointerOrReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005866 default:
5867 break;
5868 }
5869 return false;
5870}
5871
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005872bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005873ClangASTContext::IsIntegerType (clang_type_t clang_type, bool &is_signed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005874{
5875 if (!clang_type)
5876 return false;
5877
5878 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5879 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal());
5880
5881 if (builtin_type)
5882 {
5883 if (builtin_type->isInteger())
Jim Inghamef651602011-12-22 19:12:40 +00005884 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005885 is_signed = builtin_type->isSignedInteger();
Jim Inghamef651602011-12-22 19:12:40 +00005886 return true;
5887 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005888 }
5889
5890 return false;
5891}
5892
5893bool
Greg Claytonaffb03b2011-07-08 18:27:39 +00005894ClangASTContext::IsPointerType (clang_type_t clang_type, clang_type_t *target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005895{
Greg Claytonaffb03b2011-07-08 18:27:39 +00005896 if (target_type)
5897 *target_type = NULL;
5898
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005899 if (clang_type)
5900 {
5901 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005902 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5903 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005904 {
Sean Callanana2424172010-10-25 00:29:48 +00005905 case clang::Type::Builtin:
5906 switch (cast<clang::BuiltinType>(qual_type)->getKind())
5907 {
5908 default:
5909 break;
5910 case clang::BuiltinType::ObjCId:
5911 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00005912 return true;
5913 }
5914 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00005915 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005916 if (target_type)
5917 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5918 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005919 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005920 if (target_type)
5921 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5922 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005923 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005924 if (target_type)
5925 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5926 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005927 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005928 if (target_type)
5929 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5930 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005931 case clang::Type::Typedef:
Greg Claytonaffb03b2011-07-08 18:27:39 +00005932 return ClangASTContext::IsPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), target_type);
Sean Callanan912855f2011-08-11 23:56:13 +00005933 case clang::Type::Elaborated:
5934 return ClangASTContext::IsPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), target_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005935 default:
5936 break;
5937 }
5938 }
5939 return false;
5940}
5941
5942bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005943ClangASTContext::IsFloatingPointType (clang_type_t clang_type, uint32_t &count, bool &is_complex)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005944{
5945 if (clang_type)
5946 {
5947 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5948
5949 if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()))
5950 {
5951 clang::BuiltinType::Kind kind = BT->getKind();
5952 if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble)
5953 {
5954 count = 1;
5955 is_complex = false;
5956 return true;
5957 }
5958 }
5959 else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal()))
5960 {
5961 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex))
5962 {
5963 count = 2;
5964 is_complex = true;
5965 return true;
5966 }
5967 }
5968 else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal()))
5969 {
5970 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex))
5971 {
5972 count = VT->getNumElements();
5973 is_complex = false;
5974 return true;
5975 }
5976 }
5977 }
5978 return false;
5979}
5980
Enrico Granata9fc19442011-07-06 02:13:41 +00005981bool
5982ClangASTContext::IsScalarType (lldb::clang_type_t clang_type)
5983{
5984 bool is_signed;
5985 if (ClangASTContext::IsIntegerType(clang_type, is_signed))
5986 return true;
5987
5988 uint32_t count;
5989 bool is_complex;
5990 return ClangASTContext::IsFloatingPointType(clang_type, count, is_complex) && !is_complex;
5991}
5992
5993bool
5994ClangASTContext::IsPointerToScalarType (lldb::clang_type_t clang_type)
5995{
5996 if (!IsPointerType(clang_type))
5997 return false;
5998
5999 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6000 lldb::clang_type_t pointee_type = qual_type.getTypePtr()->getPointeeType().getAsOpaquePtr();
6001 return IsScalarType(pointee_type);
6002}
6003
6004bool
6005ClangASTContext::IsArrayOfScalarType (lldb::clang_type_t clang_type)
6006{
Greg Clayton4ef877f2012-12-06 02:33:54 +00006007 clang_type = GetAsArrayType(clang_type, NULL, NULL, NULL);
Sean Callanan0caa21c2012-01-19 23:54:24 +00006008
6009 if (clang_type == 0)
Enrico Granata9fc19442011-07-06 02:13:41 +00006010 return false;
6011
6012 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6013 lldb::clang_type_t item_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
6014 return IsScalarType(item_type);
6015}
6016
Greg Clayton8f92f0a2010-10-14 22:52:14 +00006017
6018bool
6019ClangASTContext::GetCXXClassName (clang_type_t clang_type, std::string &class_name)
6020{
6021 if (clang_type)
6022 {
6023 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6024
6025 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
6026 if (cxx_record_decl)
6027 {
6028 class_name.assign (cxx_record_decl->getIdentifier()->getNameStart());
6029 return true;
6030 }
6031 }
6032 class_name.clear();
6033 return false;
6034}
6035
6036
Greg Clayton0fffff52010-09-24 05:15:53 +00006037bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006038ClangASTContext::IsCXXClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00006039{
6040 if (clang_type)
6041 {
6042 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6043 if (qual_type->getAsCXXRecordDecl() != NULL)
6044 return true;
6045 }
6046 return false;
6047}
6048
Greg Clayton20568dd2011-10-13 23:13:20 +00006049bool
6050ClangASTContext::IsBeingDefined (lldb::clang_type_t clang_type)
6051{
6052 if (clang_type)
6053 {
6054 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6055 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type);
6056 if (tag_type)
6057 return tag_type->isBeingDefined();
6058 }
6059 return false;
6060}
6061
Greg Clayton0fffff52010-09-24 05:15:53 +00006062bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006063ClangASTContext::IsObjCClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00006064{
6065 if (clang_type)
6066 {
6067 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6068 if (qual_type->isObjCObjectOrInterfaceType())
6069 return true;
6070 }
6071 return false;
6072}
6073
Sean Callanan72772842012-02-22 23:57:45 +00006074bool
6075ClangASTContext::IsObjCObjectPointerType (lldb::clang_type_t clang_type, clang_type_t *class_type)
6076{
6077 if (clang_type)
6078 {
6079 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6080 if (qual_type->isObjCObjectPointerType())
6081 {
6082 if (class_type)
6083 {
6084 *class_type = NULL;
6085
6086 if (!qual_type->isObjCClassType() &&
6087 !qual_type->isObjCIdType())
6088 {
6089 const ObjCObjectPointerType *obj_pointer_type = dyn_cast<ObjCObjectPointerType>(qual_type);
Sean Callanand7dabe22012-03-10 01:59:11 +00006090 if (!obj_pointer_type)
6091 *class_type = NULL;
Sean Callanan1fc91ad2012-03-10 02:00:32 +00006092 else
6093 *class_type = QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr();
Sean Callanan72772842012-02-22 23:57:45 +00006094 }
6095 }
6096 return true;
6097 }
6098 }
6099 return false;
6100}
6101
6102bool
6103ClangASTContext::GetObjCClassName (lldb::clang_type_t clang_type,
6104 std::string &class_name)
6105{
6106 if (!clang_type)
6107 return false;
6108
6109 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(QualType::getFromOpaquePtr(clang_type));
6110 if (!object_type)
6111 return false;
6112
6113 const ObjCInterfaceDecl *interface = object_type->getInterface();
6114 if (!interface)
6115 return false;
6116
6117 class_name = interface->getNameAsString();
6118 return true;
6119}
Greg Clayton0fffff52010-09-24 05:15:53 +00006120
Greg Clayton73b472d2010-10-27 03:32:59 +00006121bool
6122ClangASTContext::IsCharType (clang_type_t clang_type)
6123{
6124 if (clang_type)
6125 return QualType::getFromOpaquePtr(clang_type)->isCharType();
6126 return false;
6127}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006128
6129bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006130ClangASTContext::IsCStringType (clang_type_t clang_type, uint32_t &length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006131{
Greg Clayton73b472d2010-10-27 03:32:59 +00006132 clang_type_t pointee_or_element_clang_type = NULL;
6133 Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, &pointee_or_element_clang_type));
6134
6135 if (pointee_or_element_clang_type == NULL)
6136 return false;
6137
6138 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006139 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006140 QualType pointee_or_element_qual_type (QualType::getFromOpaquePtr (pointee_or_element_clang_type));
6141
6142 if (pointee_or_element_qual_type.getUnqualifiedType()->isCharType())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006143 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006144 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6145 if (type_flags.Test (eTypeIsArray))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006146 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006147 // We know the size of the array and it could be a C string
6148 // since it is an array of characters
6149 length = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
6150 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006151 }
Greg Clayton73b472d2010-10-27 03:32:59 +00006152 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006153 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006154 length = 0;
6155 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006156 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006157
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006158 }
6159 }
6160 return false;
6161}
6162
6163bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006164ClangASTContext::IsFunctionPointerType (clang_type_t clang_type)
Greg Clayton737b9322010-09-13 03:32:57 +00006165{
6166 if (clang_type)
6167 {
6168 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6169
6170 if (qual_type->isFunctionPointerType())
6171 return true;
6172
6173 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6174 switch (type_class)
6175 {
Sean Callananfb0b7582011-03-15 00:17:19 +00006176 default:
6177 break;
Greg Clayton737b9322010-09-13 03:32:57 +00006178 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00006179 return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006180 case clang::Type::Elaborated:
6181 return ClangASTContext::IsFunctionPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00006182
6183 case clang::Type::LValueReference:
6184 case clang::Type::RValueReference:
6185 {
Sean Callanan78e37602011-01-27 04:42:51 +00006186 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00006187 if (reference_type)
6188 return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr());
6189 }
6190 break;
6191 }
6192 }
6193 return false;
6194}
6195
Greg Clayton73b472d2010-10-27 03:32:59 +00006196size_t
6197ClangASTContext::GetArraySize (clang_type_t clang_type)
6198{
6199 if (clang_type)
6200 {
Greg Claytonef37d68a2011-07-09 17:12:27 +00006201 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
6202 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6203 switch (type_class)
6204 {
6205 case clang::Type::ConstantArray:
6206 {
6207 const ConstantArrayType *array = cast<ConstantArrayType>(QualType::getFromOpaquePtr(clang_type).getTypePtr());
6208 if (array)
6209 return array->getSize().getLimitedValue();
6210 }
6211 break;
6212
6213 case clang::Type::Typedef:
6214 return ClangASTContext::GetArraySize(cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006215
6216 case clang::Type::Elaborated:
6217 return ClangASTContext::GetArraySize(cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00006218
6219 default:
6220 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006221 }
Greg Clayton73b472d2010-10-27 03:32:59 +00006222 }
6223 return 0;
6224}
Greg Clayton737b9322010-09-13 03:32:57 +00006225
Sean Callanan0caa21c2012-01-19 23:54:24 +00006226clang_type_t
Greg Clayton4ef877f2012-12-06 02:33:54 +00006227ClangASTContext::GetAsArrayType (clang_type_t clang_type, clang_type_t*member_type, uint64_t *size, bool *is_incomplete)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006228{
Greg Clayton4ef877f2012-12-06 02:33:54 +00006229 if (is_incomplete)
6230 *is_incomplete = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006231 if (!clang_type)
Sean Callanan0caa21c2012-01-19 23:54:24 +00006232 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006233
6234 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6235
Greg Clayton737b9322010-09-13 03:32:57 +00006236 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6237 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006238 {
Sean Callananfb0b7582011-03-15 00:17:19 +00006239 default:
6240 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006241
Greg Claytone1a916a2010-07-21 22:12:05 +00006242 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006243 if (member_type)
6244 *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6245 if (size)
Greg Claytonac4827f2011-04-01 18:14:08 +00006246 *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
Sean Callanan0caa21c2012-01-19 23:54:24 +00006247 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006248
Greg Claytone1a916a2010-07-21 22:12:05 +00006249 case clang::Type::IncompleteArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006250 if (member_type)
6251 *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6252 if (size)
6253 *size = 0;
Greg Clayton4ef877f2012-12-06 02:33:54 +00006254 if (is_incomplete)
6255 *is_incomplete = true;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006256 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006257
Greg Claytone1a916a2010-07-21 22:12:05 +00006258 case clang::Type::VariableArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006259 if (member_type)
6260 *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6261 if (size)
6262 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006263 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006264
Greg Claytone1a916a2010-07-21 22:12:05 +00006265 case clang::Type::DependentSizedArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006266 if (member_type)
6267 *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6268 if (size)
6269 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006270 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006271
6272 case clang::Type::Typedef:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006273 return ClangASTContext::GetAsArrayType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
6274 member_type,
Greg Clayton4ef877f2012-12-06 02:33:54 +00006275 size,
6276 is_incomplete);
Sean Callanan912855f2011-08-11 23:56:13 +00006277
6278 case clang::Type::Elaborated:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006279 return ClangASTContext::GetAsArrayType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
6280 member_type,
Greg Clayton4ef877f2012-12-06 02:33:54 +00006281 size,
6282 is_incomplete);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006283 }
Sean Callanan0caa21c2012-01-19 23:54:24 +00006284 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006285}
6286
6287
6288#pragma mark Typedefs
6289
Greg Clayton1be10fc2010-09-29 01:12:09 +00006290clang_type_t
6291ClangASTContext::CreateTypedefType (const char *name, clang_type_t clang_type, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006292{
6293 if (clang_type)
6294 {
6295 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006296 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006297 IdentifierTable *identifier_table = getIdentifierTable();
Greg Clayton6beaaa62011-01-17 03:46:26 +00006298 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006299 assert (identifier_table != NULL);
6300 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00006301 decl_ctx = ast->getTranslationUnitDecl();
6302 TypedefDecl *decl = TypedefDecl::Create (*ast,
6303 decl_ctx,
6304 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00006305 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00006306 name ? &identifier_table->get(name) : NULL, // Identifier
6307 ast->CreateTypeSourceInfo(qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00006308
Greg Clayton147e1fa2011-10-14 22:47:18 +00006309 //decl_ctx->addDecl (decl);
6310
Sean Callanan2652ad22011-01-18 01:03:44 +00006311 decl->setAccess(AS_public); // TODO respect proper access specifier
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006312
6313 // Get a uniqued QualType for the typedef decl type
Greg Clayton6beaaa62011-01-17 03:46:26 +00006314 return ast->getTypedefType (decl).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006315 }
6316 return NULL;
6317}
6318
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006319// Disable this for now since I can't seem to get a nicely formatted float
6320// out of the APFloat class without just getting the float, double or quad
6321// and then using a formatted print on it which defeats the purpose. We ideally
6322// would like to get perfect string values for any kind of float semantics
6323// so we can support remote targets. The code below also requires a patch to
6324// llvm::APInt.
6325//bool
Greg Clayton6beaaa62011-01-17 03:46:26 +00006326//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 +00006327//{
6328// uint32_t count = 0;
6329// bool is_complex = false;
6330// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6331// {
6332// unsigned num_bytes_per_float = byte_size / count;
6333// unsigned num_bits_per_float = num_bytes_per_float * 8;
6334//
6335// float_str.clear();
6336// uint32_t i;
6337// for (i=0; i<count; i++)
6338// {
6339// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
6340// bool is_ieee = false;
6341// APFloat ap_float(ap_int, is_ieee);
6342// char s[1024];
6343// unsigned int hex_digits = 0;
6344// bool upper_case = false;
6345//
6346// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
6347// {
6348// if (i > 0)
6349// float_str.append(", ");
6350// float_str.append(s);
6351// if (i == 1 && is_complex)
6352// float_str.append(1, 'i');
6353// }
6354// }
6355// return !float_str.empty();
6356// }
6357// return false;
6358//}
6359
6360size_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00006361ClangASTContext::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 +00006362{
6363 if (clang_type)
6364 {
6365 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6366 uint32_t count = 0;
6367 bool is_complex = false;
6368 if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6369 {
6370 // TODO: handle complex and vector types
6371 if (count != 1)
6372 return false;
6373
6374 StringRef s_sref(s);
Greg Clayton6beaaa62011-01-17 03:46:26 +00006375 APFloat ap_float(ast->getFloatTypeSemantics(qual_type), s_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006376
Greg Clayton6beaaa62011-01-17 03:46:26 +00006377 const uint64_t bit_size = ast->getTypeSize (qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006378 const uint64_t byte_size = bit_size / 8;
6379 if (dst_size >= byte_size)
6380 {
6381 if (bit_size == sizeof(float)*8)
6382 {
6383 float float32 = ap_float.convertToFloat();
6384 ::memcpy (dst, &float32, byte_size);
6385 return byte_size;
6386 }
6387 else if (bit_size >= 64)
6388 {
6389 llvm::APInt ap_int(ap_float.bitcastToAPInt());
6390 ::memcpy (dst, ap_int.getRawData(), byte_size);
6391 return byte_size;
6392 }
6393 }
6394 }
6395 }
6396 return 0;
6397}
Sean Callanan6fe64b52010-09-17 02:24:29 +00006398
6399unsigned
Greg Clayton1be10fc2010-09-29 01:12:09 +00006400ClangASTContext::GetTypeQualifiers(clang_type_t clang_type)
Sean Callanan6fe64b52010-09-17 02:24:29 +00006401{
6402 assert (clang_type);
6403
6404 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6405
6406 return qual_type.getQualifiers().getCVRQualifiers();
6407}
Greg Clayton6beaaa62011-01-17 03:46:26 +00006408
6409bool
6410ClangASTContext::GetCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6411{
6412 if (clang_type == NULL)
6413 return false;
6414
Greg Claytonc432c192011-01-20 04:18:48 +00006415 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006416}
6417
6418
6419bool
6420ClangASTContext::GetCompleteType (clang_type_t clang_type)
6421{
6422 return ClangASTContext::GetCompleteType (getASTContext(), clang_type);
6423}
6424
Greg Claytona2721472011-06-25 00:44:06 +00006425bool
Enrico Granata86027e92012-03-24 01:11:14 +00006426ClangASTContext::IsCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6427{
6428 if (clang_type == NULL)
6429 return false;
6430
6431 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type), false); // just check but don't let it actually complete
6432}
6433
6434
6435bool
6436ClangASTContext::IsCompleteType (clang_type_t clang_type)
6437{
6438 return ClangASTContext::IsCompleteType (getASTContext(), clang_type);
6439}
6440
6441bool
Greg Claytona2721472011-06-25 00:44:06 +00006442ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
6443 clang::Decl *decl)
6444{
6445 if (!decl)
6446 return false;
6447
6448 ExternalASTSource *ast_source = ast->getExternalSource();
6449
6450 if (!ast_source)
6451 return false;
6452
6453 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
6454 {
Greg Clayton219cf312012-03-30 00:51:13 +00006455 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006456 return true;
6457
6458 if (!tag_decl->hasExternalLexicalStorage())
6459 return false;
6460
6461 ast_source->CompleteType(tag_decl);
6462
6463 return !tag_decl->getTypeForDecl()->isIncompleteType();
6464 }
6465 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
6466 {
Sean Callanan5b26f272012-02-04 08:49:35 +00006467 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006468 return true;
6469
6470 if (!objc_interface_decl->hasExternalLexicalStorage())
6471 return false;
6472
6473 ast_source->CompleteType(objc_interface_decl);
6474
Sean Callanan5b26f272012-02-04 08:49:35 +00006475 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00006476 }
6477 else
6478 {
6479 return false;
6480 }
6481}
6482
Sean Callanan60217122012-04-13 00:10:03 +00006483void
Jim Ingham379397632012-10-27 02:54:13 +00006484ClangASTContext::SetMetadataAsUserID (uintptr_t object,
6485 user_id_t user_id)
6486{
6487 ClangASTMetadata meta_data;
6488 meta_data.SetUserID (user_id);
6489 SetMetadata (object, meta_data);
6490}
6491
6492void
Sean Callanan60217122012-04-13 00:10:03 +00006493ClangASTContext::SetMetadata (clang::ASTContext *ast,
6494 uintptr_t object,
Jim Ingham379397632012-10-27 02:54:13 +00006495 ClangASTMetadata &metadata)
Sean Callanan60217122012-04-13 00:10:03 +00006496{
6497 ClangExternalASTSourceCommon *external_source =
6498 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
6499
6500 if (external_source)
6501 external_source->SetMetadata(object, metadata);
6502}
6503
Jim Ingham379397632012-10-27 02:54:13 +00006504ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +00006505ClangASTContext::GetMetadata (clang::ASTContext *ast,
6506 uintptr_t object)
6507{
6508 ClangExternalASTSourceCommon *external_source =
6509 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
6510
6511 if (external_source && external_source->HasMetadata(object))
6512 return external_source->GetMetadata(object);
6513 else
Jim Ingham379397632012-10-27 02:54:13 +00006514 return NULL;
Sean Callanan60217122012-04-13 00:10:03 +00006515}
6516
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006517clang::DeclContext *
6518ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
6519{
Sean Callanana87bee82011-08-19 06:19:25 +00006520 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006521}
6522
6523clang::DeclContext *
6524ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
6525{
Sean Callanana87bee82011-08-19 06:19:25 +00006526 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006527}
6528
Greg Clayton685c88c2012-07-14 00:53:55 +00006529
6530bool
6531ClangASTContext::GetClassMethodInfoForDeclContext (clang::DeclContext *decl_ctx,
6532 lldb::LanguageType &language,
6533 bool &is_instance_method,
6534 ConstString &language_object_name)
6535{
6536 language_object_name.Clear();
6537 language = eLanguageTypeUnknown;
6538 is_instance_method = false;
6539
6540 if (decl_ctx)
6541 {
6542 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
6543 {
6544 if (method_decl->isStatic())
6545 {
6546 is_instance_method = false;
6547 }
6548 else
6549 {
6550 language_object_name.SetCString("this");
6551 is_instance_method = true;
6552 }
6553 language = eLanguageTypeC_plus_plus;
6554 return true;
6555 }
6556 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
6557 {
6558 // Both static and instance methods have a "self" object in objective C
6559 language_object_name.SetCString("self");
6560 if (method_decl->isInstanceMethod())
6561 {
6562 is_instance_method = true;
6563 }
6564 else
6565 {
6566 is_instance_method = false;
6567 }
6568 language = eLanguageTypeObjC;
6569 return true;
6570 }
Jim Ingham379397632012-10-27 02:54:13 +00006571 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
6572 {
6573 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), (uintptr_t) function_decl);
6574 if (metadata && metadata->HasObjectPtr())
6575 {
6576 language_object_name.SetCString (metadata->GetObjectPtrName());
6577 language = eLanguageTypeObjC;
6578 is_instance_method = true;
6579 }
6580 return true;
6581 }
Greg Clayton685c88c2012-07-14 00:53:55 +00006582 }
6583 return false;
6584}
6585