blob: 03424aba2bf45368289deb56f3a282def172cb96 [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"
37#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000038#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000039#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "clang/AST/RecordLayout.h"
41#include "clang/AST/Type.h"
42#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000043#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000045#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "clang/Basic/SourceManager.h"
47#include "clang/Basic/TargetInfo.h"
48#include "clang/Basic/TargetOptions.h"
49#include "clang/Frontend/FrontendOptions.h"
50#include "clang/Frontend/LangStandard.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000051
52#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000053#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000054#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
55// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
56#include <assert.h>
57#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058
Greg Clayton514487e2011-02-15 21:59:32 +000059#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060#include "lldb/Core/dwarf.h"
Greg Clayton73b472d2010-10-27 03:32:59 +000061#include "lldb/Core/Flags.h"
Sean Callananfb8b7092010-10-28 18:19:36 +000062#include "lldb/Core/Log.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000063#include "lldb/Core/RegularExpression.h"
64#include "lldb/Expression/ASTDumper.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000065#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000066#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000067#include "lldb/Target/ExecutionContext.h"
68#include "lldb/Target/Process.h"
69#include "lldb/Target/ObjCLanguageRuntime.h"
70
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071
Eli Friedman932197d2010-06-13 19:06:42 +000072#include <stdio.h>
73
Greg Claytonc86103d2010-08-05 01:57:25 +000074using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075using namespace lldb_private;
76using namespace llvm;
77using namespace clang;
78
Greg Clayton6beaaa62011-01-17 03:46:26 +000079
80static bool
Enrico Granata86027e92012-03-24 01:11:14 +000081GetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion = true)
Greg Clayton6beaaa62011-01-17 03:46:26 +000082{
83 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
84 switch (type_class)
85 {
Sean Callanan5b26f272012-02-04 08:49:35 +000086 case clang::Type::ConstantArray:
87 {
88 const clang::ArrayType *array_type = dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
89
90 if (array_type)
Enrico Granata86027e92012-03-24 01:11:14 +000091 return GetCompleteQualType (ast, array_type->getElementType(), allow_completion);
Sean Callanan5b26f272012-02-04 08:49:35 +000092 }
93 break;
94
Greg Clayton6beaaa62011-01-17 03:46:26 +000095 case clang::Type::Record:
96 case clang::Type::Enum:
97 {
Sean Callanan78e37602011-01-27 04:42:51 +000098 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +000099 if (tag_type)
100 {
101 clang::TagDecl *tag_decl = tag_type->getDecl();
102 if (tag_decl)
103 {
Greg Clayton219cf312012-03-30 00:51:13 +0000104 if (tag_decl->isCompleteDefinition())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000105 return true;
Enrico Granata86027e92012-03-24 01:11:14 +0000106
107 if (!allow_completion)
108 return false;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000109
110 if (tag_decl->hasExternalLexicalStorage())
111 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000112 if (ast)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000113 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000114 ExternalASTSource *external_ast_source = ast->getExternalSource();
115 if (external_ast_source)
116 {
117 external_ast_source->CompleteType(tag_decl);
118 return !tag_type->isIncompleteType();
119 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000120 }
121 }
122 return false;
123 }
124 }
125
126 }
127 break;
128
129 case clang::Type::ObjCObject:
130 case clang::Type::ObjCInterface:
131 {
Sean Callanan78e37602011-01-27 04:42:51 +0000132 const clang::ObjCObjectType *objc_class_type = dyn_cast<clang::ObjCObjectType>(qual_type);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000133 if (objc_class_type)
134 {
135 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
136 // We currently can't complete objective C types through the newly added ASTContext
137 // because it only supports TagDecl objects right now...
Enrico Granata9dd75c82011-07-15 23:30:15 +0000138 if (class_interface_decl)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000139 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000140 if (class_interface_decl->getDefinition())
141 return true;
142
Enrico Granata86027e92012-03-24 01:11:14 +0000143 if (!allow_completion)
144 return false;
Greg Clayton20533982012-03-30 23:48:28 +0000145
Sean Callanan5b26f272012-02-04 08:49:35 +0000146 if (class_interface_decl->hasExternalLexicalStorage())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000147 {
Enrico Granata0a3958e2011-07-02 00:25:22 +0000148 if (ast)
Greg Clayton007d5be2011-05-30 00:49:24 +0000149 {
Enrico Granata0a3958e2011-07-02 00:25:22 +0000150 ExternalASTSource *external_ast_source = ast->getExternalSource();
151 if (external_ast_source)
152 {
153 external_ast_source->CompleteType (class_interface_decl);
Sean Callanan5b26f272012-02-04 08:49:35 +0000154 return !objc_class_type->isIncompleteType();
Enrico Granata0a3958e2011-07-02 00:25:22 +0000155 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000156 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000157 }
Enrico Granata0a3958e2011-07-02 00:25:22 +0000158 return false;
Sean Callanan5b26f272012-02-04 08:49:35 +0000159 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000160 }
161 }
162 break;
163
164 case clang::Type::Typedef:
Enrico Granata86027e92012-03-24 01:11:14 +0000165 return GetCompleteQualType (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType(), allow_completion);
Sean Callanan912855f2011-08-11 23:56:13 +0000166
167 case clang::Type::Elaborated:
Enrico Granata86027e92012-03-24 01:11:14 +0000168 return GetCompleteQualType (ast, cast<ElaboratedType>(qual_type)->getNamedType(), allow_completion);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000169
170 default:
171 break;
172 }
173
174 return true;
175}
176
Greg Clayton8cf05932010-07-22 18:30:50 +0000177static AccessSpecifier
Greg Claytonc86103d2010-08-05 01:57:25 +0000178ConvertAccessTypeToAccessSpecifier (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000179{
180 switch (access)
181 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000182 default: break;
183 case eAccessNone: return AS_none;
184 case eAccessPublic: return AS_public;
185 case eAccessPrivate: return AS_private;
186 case eAccessProtected: return AS_protected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000187 }
188 return AS_none;
189}
190
191static ObjCIvarDecl::AccessControl
Greg Claytonc86103d2010-08-05 01:57:25 +0000192ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000193{
194 switch (access)
195 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000196 default: break;
197 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);
260 Opts.BCPLComment = Std.hasBCPLComments();
261 Opts.C99 = Std.isC99();
262 Opts.CPlusPlus = Std.isCPlusPlus();
263 Opts.CPlusPlus0x = Std.isCPlusPlus0x();
264 Opts.Digraphs = Std.hasDigraphs();
265 Opts.GNUMode = Std.isGNUMode();
266 Opts.GNUInline = !Std.isC99();
267 Opts.HexFloats = Std.hasHexFloats();
268 Opts.ImplicitInt = Std.hasImplicitInt();
269
270 // OpenCL has some additional defaults.
271 if (LangStd == LangStandard::lang_opencl) {
272 Opts.OpenCL = 1;
273 Opts.AltiVec = 1;
274 Opts.CXXOperatorNames = 1;
275 Opts.LaxVectorConversions = 1;
276 }
277
278 // OpenCL and C++ both have bool, true, false keywords.
279 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
280
281// if (Opts.CPlusPlus)
282// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
283//
284// if (Args.hasArg(OPT_fobjc_gc_only))
285// Opts.setGCMode(LangOptions::GCOnly);
286// else if (Args.hasArg(OPT_fobjc_gc))
287// Opts.setGCMode(LangOptions::HybridGC);
288//
289// if (Args.hasArg(OPT_print_ivar_layout))
290// Opts.ObjCGCBitmapPrint = 1;
291//
292// if (Args.hasArg(OPT_faltivec))
293// Opts.AltiVec = 1;
294//
295// if (Args.hasArg(OPT_pthread))
296// Opts.POSIXThreads = 1;
297//
298// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
299// "default");
300// if (Vis == "default")
Sean Callanan31e851c2010-10-29 18:38:40 +0000301 Opts.setVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302// else if (Vis == "hidden")
303// Opts.setVisibilityMode(LangOptions::Hidden);
304// else if (Vis == "protected")
305// Opts.setVisibilityMode(LangOptions::Protected);
306// else
307// Diags.Report(diag::err_drv_invalid_value)
308// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
309
310// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
311
312 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
313 // is specified, or -std is set to a conforming mode.
314 Opts.Trigraphs = !Opts.GNUMode;
315// if (Args.hasArg(OPT_trigraphs))
316// Opts.Trigraphs = 1;
317//
318// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
319// OPT_fno_dollars_in_identifiers,
320// !Opts.AsmPreprocessor);
321// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
322// Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
323// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
324// if (Args.hasArg(OPT_fno_lax_vector_conversions))
325// Opts.LaxVectorConversions = 0;
326// Opts.Exceptions = Args.hasArg(OPT_fexceptions);
327// Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
328// Opts.Blocks = Args.hasArg(OPT_fblocks);
329// Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char);
330// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
331// Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
332// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
333// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
334// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
335// Opts.AccessControl = Args.hasArg(OPT_faccess_control);
336// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
337// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
338// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
339// Diags);
340// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
341// Opts.ObjCConstantStringClass = getLastArgValue(Args,
342// OPT_fconstant_string_class);
343// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
344// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
345// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
346// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
347// Opts.Static = Args.hasArg(OPT_static_define);
348 Opts.OptimizeSize = 0;
349
350 // FIXME: Eliminate this dependency.
351// unsigned Opt =
352// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
353// Opts.Optimize = Opt != 0;
354 unsigned Opt = 0;
355
356 // This is the __NO_INLINE__ define, which just depends on things like the
357 // optimization level and -fno-inline, not actually whether the backend has
358 // inlining enabled.
359 //
360 // FIXME: This is affected by other options (-fno-inline).
Bill Wendlingc08a5e72012-04-03 08:41:55 +0000361 Opts.NoInline = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362
363// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
364// switch (SSP) {
365// default:
366// Diags.Report(diag::err_drv_invalid_value)
367// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
368// break;
369// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
370// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
371// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
372// }
373}
374
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375
Greg Clayton6beaaa62011-01-17 03:46:26 +0000376ClangASTContext::ClangASTContext (const char *target_triple) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377 m_target_triple(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000378 m_ast_ap(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379 m_language_options_ap(),
380 m_source_manager_ap(),
Sean Callanan880e6802011-10-07 23:18:13 +0000381 m_diagnostics_engine_ap(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382 m_target_options_ap(),
383 m_target_info_ap(),
384 m_identifier_table_ap(),
385 m_selector_table_ap(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000386 m_builtins_ap(),
387 m_callback_tag_decl (NULL),
388 m_callback_objc_decl (NULL),
389 m_callback_baton (NULL)
390
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391{
392 if (target_triple && target_triple[0])
Greg Clayton880cbb02011-07-30 01:26:02 +0000393 SetTargetTriple (target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394}
395
396//----------------------------------------------------------------------
397// Destructor
398//----------------------------------------------------------------------
399ClangASTContext::~ClangASTContext()
400{
401 m_builtins_ap.reset();
402 m_selector_table_ap.reset();
403 m_identifier_table_ap.reset();
404 m_target_info_ap.reset();
405 m_target_options_ap.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000406 m_diagnostics_engine_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407 m_source_manager_ap.reset();
408 m_language_options_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000409 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410}
411
412
413void
414ClangASTContext::Clear()
415{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000416 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417 m_language_options_ap.reset();
418 m_source_manager_ap.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000419 m_diagnostics_engine_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420 m_target_options_ap.reset();
421 m_target_info_ap.reset();
422 m_identifier_table_ap.reset();
423 m_selector_table_ap.reset();
424 m_builtins_ap.reset();
425}
426
427const char *
428ClangASTContext::GetTargetTriple ()
429{
430 return m_target_triple.c_str();
431}
432
433void
434ClangASTContext::SetTargetTriple (const char *target_triple)
435{
436 Clear();
437 m_target_triple.assign(target_triple);
438}
439
Greg Clayton514487e2011-02-15 21:59:32 +0000440void
441ClangASTContext::SetArchitecture (const ArchSpec &arch)
442{
Greg Clayton880cbb02011-07-30 01:26:02 +0000443 SetTargetTriple(arch.GetTriple().str().c_str());
Greg Clayton514487e2011-02-15 21:59:32 +0000444}
445
Greg Clayton6beaaa62011-01-17 03:46:26 +0000446bool
447ClangASTContext::HasExternalSource ()
448{
449 ASTContext *ast = getASTContext();
450 if (ast)
451 return ast->getExternalSource () != NULL;
452 return false;
453}
454
455void
456ClangASTContext::SetExternalSource (llvm::OwningPtr<ExternalASTSource> &ast_source_ap)
457{
458 ASTContext *ast = getASTContext();
459 if (ast)
460 {
461 ast->setExternalSource (ast_source_ap);
462 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
463 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
464 }
465}
466
467void
468ClangASTContext::RemoveExternalSource ()
469{
470 ASTContext *ast = getASTContext();
471
472 if (ast)
473 {
474 llvm::OwningPtr<ExternalASTSource> empty_ast_source_ap;
475 ast->setExternalSource (empty_ast_source_ap);
476 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
477 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
478 }
479}
480
481
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482
483ASTContext *
484ClangASTContext::getASTContext()
485{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000486 if (m_ast_ap.get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 {
Greg Clayton6beaaa62011-01-17 03:46:26 +0000488 m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
489 *getSourceManager(),
Sean Callanan880e6802011-10-07 23:18:13 +0000490 getTargetInfo(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000491 *getIdentifierTable(),
492 *getSelectorTable(),
493 *getBuiltinContext(),
494 0));
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000495
Greg Clayton6beaaa62011-01-17 03:46:26 +0000496 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
497 {
498 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
499 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
500 }
501
Sean Callanan880e6802011-10-07 23:18:13 +0000502 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000504 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505}
506
507Builtin::Context *
508ClangASTContext::getBuiltinContext()
509{
510 if (m_builtins_ap.get() == NULL)
Sean Callanan880e6802011-10-07 23:18:13 +0000511 m_builtins_ap.reset (new Builtin::Context());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512 return m_builtins_ap.get();
513}
514
515IdentifierTable *
516ClangASTContext::getIdentifierTable()
517{
518 if (m_identifier_table_ap.get() == NULL)
519 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), NULL));
520 return m_identifier_table_ap.get();
521}
522
523LangOptions *
524ClangASTContext::getLanguageOptions()
525{
526 if (m_language_options_ap.get() == NULL)
527 {
528 m_language_options_ap.reset(new LangOptions());
Greg Clayton94e5d782010-06-13 17:34:29 +0000529 ParseLangArgs(*m_language_options_ap, IK_ObjCXX);
530// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531 }
532 return m_language_options_ap.get();
533}
534
535SelectorTable *
536ClangASTContext::getSelectorTable()
537{
538 if (m_selector_table_ap.get() == NULL)
539 m_selector_table_ap.reset (new SelectorTable());
540 return m_selector_table_ap.get();
541}
542
Sean Callanan79439e82010-11-18 02:56:27 +0000543clang::FileManager *
544ClangASTContext::getFileManager()
545{
546 if (m_file_manager_ap.get() == NULL)
Greg Clayton38a61402010-12-02 23:20:03 +0000547 {
548 clang::FileSystemOptions file_system_options;
549 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
550 }
Sean Callanan79439e82010-11-18 02:56:27 +0000551 return m_file_manager_ap.get();
552}
553
Greg Claytone1a916a2010-07-21 22:12:05 +0000554clang::SourceManager *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555ClangASTContext::getSourceManager()
556{
557 if (m_source_manager_ap.get() == NULL)
Sean Callanan880e6802011-10-07 23:18:13 +0000558 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559 return m_source_manager_ap.get();
560}
561
Sean Callanan880e6802011-10-07 23:18:13 +0000562clang::DiagnosticsEngine *
563ClangASTContext::getDiagnosticsEngine()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564{
Sean Callanan880e6802011-10-07 23:18:13 +0000565 if (m_diagnostics_engine_ap.get() == NULL)
Greg Claytona651b532010-11-19 21:46:54 +0000566 {
567 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Sean Callanan880e6802011-10-07 23:18:13 +0000568 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp));
Greg Claytona651b532010-11-19 21:46:54 +0000569 }
Sean Callanan880e6802011-10-07 23:18:13 +0000570 return m_diagnostics_engine_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000571}
572
Sean Callanan880e6802011-10-07 23:18:13 +0000573class NullDiagnosticConsumer : public DiagnosticConsumer
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000574{
575public:
Sean Callanan880e6802011-10-07 23:18:13 +0000576 NullDiagnosticConsumer ()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000577 {
578 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
579 }
580
Sean Callanan880e6802011-10-07 23:18:13 +0000581 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const Diagnostic &info)
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000582 {
583 if (m_log)
584 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000585 llvm::SmallVector<char, 32> diag_str(10);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000586 info.FormatDiagnostic(diag_str);
587 diag_str.push_back('\0');
588 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
589 }
590 }
Sean Callanan880e6802011-10-07 23:18:13 +0000591
592 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
593 {
594 return new NullDiagnosticConsumer ();
595 }
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000596private:
597 LogSP m_log;
598};
599
Sean Callanan880e6802011-10-07 23:18:13 +0000600DiagnosticConsumer *
601ClangASTContext::getDiagnosticConsumer()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000602{
Sean Callanan880e6802011-10-07 23:18:13 +0000603 if (m_diagnostic_consumer_ap.get() == NULL)
604 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000605
Sean Callanan880e6802011-10-07 23:18:13 +0000606 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000607}
608
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609TargetOptions *
610ClangASTContext::getTargetOptions()
611{
612 if (m_target_options_ap.get() == NULL && !m_target_triple.empty())
613 {
614 m_target_options_ap.reset (new TargetOptions());
615 if (m_target_options_ap.get())
616 m_target_options_ap->Triple = m_target_triple;
617 }
618 return m_target_options_ap.get();
619}
620
621
622TargetInfo *
623ClangASTContext::getTargetInfo()
624{
625 // target_triple should be something like "x86_64-apple-darwin10"
626 if (m_target_info_ap.get() == NULL && !m_target_triple.empty())
Sean Callanan880e6802011-10-07 23:18:13 +0000627 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), *getTargetOptions()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628 return m_target_info_ap.get();
629}
630
631#pragma mark Basic Types
632
633static inline bool
Greg Clayton6beaaa62011-01-17 03:46:26 +0000634QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000636 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637 if (qual_type_bit_size == bit_size)
638 return true;
639 return false;
640}
641
Greg Clayton1be10fc2010-09-29 01:12:09 +0000642clang_type_t
Greg Claytonc86103d2010-08-05 01:57:25 +0000643ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000644{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000645 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646
Greg Clayton6beaaa62011-01-17 03:46:26 +0000647 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648
Greg Clayton6beaaa62011-01-17 03:46:26 +0000649 return GetBuiltinTypeForEncodingAndBitSize (ast, encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650}
651
Greg Clayton1be10fc2010-09-29 01:12:09 +0000652clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000653ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000655 if (!ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000656 return NULL;
657
658 switch (encoding)
659 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000660 case eEncodingInvalid:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000661 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
662 return ast->VoidPtrTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663 break;
664
Greg Claytonc86103d2010-08-05 01:57:25 +0000665 case eEncodingUint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000666 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
667 return ast->UnsignedCharTy.getAsOpaquePtr();
668 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
669 return ast->UnsignedShortTy.getAsOpaquePtr();
670 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
671 return ast->UnsignedIntTy.getAsOpaquePtr();
672 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
673 return ast->UnsignedLongTy.getAsOpaquePtr();
674 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
675 return ast->UnsignedLongLongTy.getAsOpaquePtr();
676 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
677 return ast->UnsignedInt128Ty.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000678 break;
679
Greg Claytonc86103d2010-08-05 01:57:25 +0000680 case eEncodingSint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000681 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
682 return ast->CharTy.getAsOpaquePtr();
683 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
684 return ast->ShortTy.getAsOpaquePtr();
685 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
686 return ast->IntTy.getAsOpaquePtr();
687 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
688 return ast->LongTy.getAsOpaquePtr();
689 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
690 return ast->LongLongTy.getAsOpaquePtr();
691 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
692 return ast->Int128Ty.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000693 break;
694
Greg Claytonc86103d2010-08-05 01:57:25 +0000695 case eEncodingIEEE754:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000696 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
697 return ast->FloatTy.getAsOpaquePtr();
698 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
699 return ast->DoubleTy.getAsOpaquePtr();
700 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
701 return ast->LongDoubleTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702 break;
703
Greg Claytonc86103d2010-08-05 01:57:25 +0000704 case eEncodingVector:
Johnny Chenc79c93a2012-03-07 01:12:24 +0000705 // Sanity check that bit_size is a multiple of 8's.
706 if (bit_size && !(bit_size & 0x7u))
707 return ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8).getAsOpaquePtr();
708 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709 default:
710 break;
711 }
712
713 return NULL;
714}
715
Greg Clayton1be10fc2010-09-29 01:12:09 +0000716clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
718{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000719 ASTContext *ast = getASTContext();
Sean Callanan38d4df52012-04-03 01:10:10 +0000720
721#define streq(a,b) strcmp(a,b) == 0
Greg Clayton6beaaa62011-01-17 03:46:26 +0000722 assert (ast != NULL);
723 if (ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724 {
725 switch (dw_ate)
726 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000727 default:
728 break;
Greg Clayton605684e2011-10-28 23:06:08 +0000729
Sean Callanan38d4df52012-04-03 01:10:10 +0000730 case DW_ATE_address:
731 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
732 return ast->VoidPtrTy.getAsOpaquePtr();
733 break;
734
735 case DW_ATE_boolean:
736 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
737 return ast->BoolTy.getAsOpaquePtr();
738 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
739 return ast->UnsignedCharTy.getAsOpaquePtr();
740 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
741 return ast->UnsignedShortTy.getAsOpaquePtr();
742 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
743 return ast->UnsignedIntTy.getAsOpaquePtr();
744 break;
745
746 case DW_ATE_lo_user:
747 // This has been seen to mean DW_AT_complex_integer
748 if (type_name)
Greg Clayton605684e2011-10-28 23:06:08 +0000749 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000750 if (::strstr(type_name, "complex"))
751 {
752 clang_type_t complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2);
753 return ast->getComplexType (QualType::getFromOpaquePtr(complex_int_clang_type)).getAsOpaquePtr();
754 }
Greg Clayton605684e2011-10-28 23:06:08 +0000755 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000756 break;
757
758 case DW_ATE_complex_float:
759 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
760 return ast->FloatComplexTy.getAsOpaquePtr();
761 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
762 return ast->DoubleComplexTy.getAsOpaquePtr();
763 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
764 return ast->LongDoubleComplexTy.getAsOpaquePtr();
765 else
Greg Clayton605684e2011-10-28 23:06:08 +0000766 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000767 clang_type_t complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2);
768 return ast->getComplexType (QualType::getFromOpaquePtr(complex_float_clang_type)).getAsOpaquePtr();
Greg Clayton605684e2011-10-28 23:06:08 +0000769 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000770 break;
771
772 case DW_ATE_float:
773 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
774 return ast->FloatTy.getAsOpaquePtr();
775 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
776 return ast->DoubleTy.getAsOpaquePtr();
777 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
778 return ast->LongDoubleTy.getAsOpaquePtr();
779 break;
780
781 case DW_ATE_signed:
782 if (type_name)
783 {
784 if (streq(type_name, "wchar_t") &&
785 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
786 return ast->WCharTy.getAsOpaquePtr();
787 if (streq(type_name, "void") &&
788 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
789 return ast->VoidTy.getAsOpaquePtr();
790 if (strstr(type_name, "long long") &&
791 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
792 return ast->LongLongTy.getAsOpaquePtr();
793 if (strstr(type_name, "long") &&
794 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
795 return ast->LongTy.getAsOpaquePtr();
796 if (strstr(type_name, "short") &&
797 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
798 return ast->ShortTy.getAsOpaquePtr();
799 if (strstr(type_name, "char"))
800 {
801 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
802 return ast->CharTy.getAsOpaquePtr();
803 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
804 return ast->SignedCharTy.getAsOpaquePtr();
805 }
806 if (strstr(type_name, "int"))
807 {
808 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
809 return ast->IntTy.getAsOpaquePtr();
810 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
811 return ast->Int128Ty.getAsOpaquePtr();
812 }
813 }
814 // We weren't able to match up a type name, just search by size
815 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
816 return ast->CharTy.getAsOpaquePtr();
817 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
818 return ast->ShortTy.getAsOpaquePtr();
819 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
820 return ast->IntTy.getAsOpaquePtr();
821 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
822 return ast->LongTy.getAsOpaquePtr();
823 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
824 return ast->LongLongTy.getAsOpaquePtr();
825 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
826 return ast->Int128Ty.getAsOpaquePtr();
827 break;
828
829 case DW_ATE_signed_char:
830 if (type_name)
831 {
832 if (streq(type_name, "signed char"))
833 {
834 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
835 return ast->SignedCharTy.getAsOpaquePtr();
836 }
837 }
838 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
839 return ast->CharTy.getAsOpaquePtr();
840 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
841 return ast->SignedCharTy.getAsOpaquePtr();
842 break;
843
844 case DW_ATE_unsigned:
845 if (type_name)
846 {
847 if (strstr(type_name, "long long"))
848 {
849 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
850 return ast->UnsignedLongLongTy.getAsOpaquePtr();
851 }
852 else if (strstr(type_name, "long"))
853 {
854 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
855 return ast->UnsignedLongTy.getAsOpaquePtr();
856 }
857 else if (strstr(type_name, "short"))
858 {
859 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
860 return ast->UnsignedShortTy.getAsOpaquePtr();
861 }
862 else if (strstr(type_name, "char"))
863 {
864 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
865 return ast->UnsignedCharTy.getAsOpaquePtr();
866 }
867 else if (strstr(type_name, "int"))
868 {
869 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
870 return ast->UnsignedIntTy.getAsOpaquePtr();
871 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
872 return ast->UnsignedInt128Ty.getAsOpaquePtr();
873 }
874 }
875 // We weren't able to match up a type name, just search by size
876 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
877 return ast->UnsignedCharTy.getAsOpaquePtr();
878 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
879 return ast->UnsignedShortTy.getAsOpaquePtr();
880 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
881 return ast->UnsignedIntTy.getAsOpaquePtr();
882 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
883 return ast->UnsignedLongTy.getAsOpaquePtr();
884 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
885 return ast->UnsignedLongLongTy.getAsOpaquePtr();
886 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
887 return ast->UnsignedInt128Ty.getAsOpaquePtr();
888 break;
889
890 case DW_ATE_unsigned_char:
891 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
892 return ast->UnsignedCharTy.getAsOpaquePtr();
893 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
894 return ast->UnsignedShortTy.getAsOpaquePtr();
895 break;
896
897 case DW_ATE_imaginary_float:
898 break;
899
900 case DW_ATE_UTF:
901 if (type_name)
902 {
903 if (streq(type_name, "char16_t"))
904 {
905 return ast->Char16Ty.getAsOpaquePtr();
906 }
907 else if (streq(type_name, "char32_t"))
908 {
909 return ast->Char32Ty.getAsOpaquePtr();
910 }
911 }
912 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000913 }
914 }
915 // This assert should fire for anything that we don't catch above so we know
916 // to fix any issues we run into.
Greg Claytondc968d12011-05-17 18:15:05 +0000917 if (type_name)
918 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000919 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 +0000920 }
921 else
922 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000923 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 +0000924 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000925 return NULL;
926}
927
Greg Clayton1be10fc2010-09-29 01:12:09 +0000928clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000929ClangASTContext::GetBuiltInType_void(ASTContext *ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000930{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000931 return ast->VoidTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000932}
933
Greg Clayton1be10fc2010-09-29 01:12:09 +0000934clang_type_t
Sean Callananf7c3e272010-11-19 02:52:21 +0000935ClangASTContext::GetBuiltInType_bool()
936{
937 return getASTContext()->BoolTy.getAsOpaquePtr();
938}
939
940clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000941ClangASTContext::GetBuiltInType_objc_id()
942{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000943 return getASTContext()->getObjCIdType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000944}
945
Greg Clayton1be10fc2010-09-29 01:12:09 +0000946clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000947ClangASTContext::GetBuiltInType_objc_Class()
948{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000949 return getASTContext()->getObjCClassType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000950}
951
Greg Clayton1be10fc2010-09-29 01:12:09 +0000952clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000953ClangASTContext::GetBuiltInType_objc_selector()
954{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000955 return getASTContext()->getObjCSelType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000956}
957
Greg Clayton1be10fc2010-09-29 01:12:09 +0000958clang_type_t
Sean Callanan77502262011-05-12 23:54:16 +0000959ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast)
960{
961 return ast->UnknownAnyTy.getAsOpaquePtr();
962}
963
964clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000965ClangASTContext::GetCStringType (bool is_const)
966{
967 QualType char_type(getASTContext()->CharTy);
968
969 if (is_const)
970 char_type.addConst();
971
972 return getASTContext()->getPointerType(char_type).getAsOpaquePtr();
973}
974
Greg Clayton1be10fc2010-09-29 01:12:09 +0000975clang_type_t
Sean Callanana6582262012-04-05 00:12:52 +0000976ClangASTContext::GetVoidType()
977{
978 return GetVoidType(getASTContext());
979}
980
981clang_type_t
982ClangASTContext::GetVoidType(ASTContext *ast)
983{
984 return ast->VoidTy.getAsOpaquePtr();
985}
986
987clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988ClangASTContext::GetVoidPtrType (bool is_const)
989{
990 return GetVoidPtrType(getASTContext(), is_const);
991}
992
Greg Clayton1be10fc2010-09-29 01:12:09 +0000993clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000994ClangASTContext::GetVoidPtrType (ASTContext *ast, bool is_const)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000995{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000996 QualType void_ptr_type(ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000997
998 if (is_const)
999 void_ptr_type.addConst();
1000
1001 return void_ptr_type.getAsOpaquePtr();
1002}
1003
Sean Callanan09ab4b72011-11-30 22:11:59 +00001004clang::DeclContext *
1005ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast)
1006{
1007 return ast->getTranslationUnitDecl();
1008}
1009
Greg Clayton1be10fc2010-09-29 01:12:09 +00001010clang_type_t
Greg Clayton38a61402010-12-02 23:20:03 +00001011ClangASTContext::CopyType (ASTContext *dst_ast,
1012 ASTContext *src_ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001013 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001014{
Sean Callanan79439e82010-11-18 02:56:27 +00001015 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001016 FileManager file_manager (file_system_options);
1017 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001018 *src_ast, file_manager,
1019 false);
Sean Callanan0617fcb2010-11-09 22:37:10 +00001020
Greg Clayton38a61402010-12-02 23:20:03 +00001021 QualType src (QualType::getFromOpaquePtr(clang_type));
1022 QualType dst (importer.Import(src));
Sean Callanan0617fcb2010-11-09 22:37:10 +00001023
1024 return dst.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001025}
1026
Greg Clayton526e5af2010-11-13 03:52:47 +00001027
1028clang::Decl *
Greg Clayton38a61402010-12-02 23:20:03 +00001029ClangASTContext::CopyDecl (ASTContext *dst_ast,
1030 ASTContext *src_ast,
Greg Clayton526e5af2010-11-13 03:52:47 +00001031 clang::Decl *source_decl)
Sean Callanan7fddd4c2010-12-11 00:08:56 +00001032{
Sean Callanan79439e82010-11-18 02:56:27 +00001033 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001034 FileManager file_manager (file_system_options);
1035 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001036 *src_ast, file_manager,
1037 false);
Greg Clayton526e5af2010-11-13 03:52:47 +00001038
1039 return importer.Import(source_decl);
1040}
1041
Sean Callanan23a30272010-07-16 00:00:27 +00001042bool
Greg Clayton84db9102012-03-26 23:03:23 +00001043ClangASTContext::AreTypesSame (ASTContext *ast,
1044 clang_type_t type1,
1045 clang_type_t type2,
1046 bool ignore_qualifiers)
Sean Callanan4dcca2622010-07-15 22:30:52 +00001047{
Greg Clayton55995eb2012-04-06 17:38:55 +00001048 if (type1 == type2)
1049 return true;
1050
Sean Callanan5056ab02012-02-18 02:01:03 +00001051 QualType type1_qual = QualType::getFromOpaquePtr(type1);
1052 QualType type2_qual = QualType::getFromOpaquePtr(type2);
1053
1054 if (ignore_qualifiers)
1055 {
1056 type1_qual = type1_qual.getUnqualifiedType();
1057 type2_qual = type2_qual.getUnqualifiedType();
1058 }
1059
1060 return ast->hasSameType (type1_qual,
1061 type2_qual);
Sean Callanan4dcca2622010-07-15 22:30:52 +00001062}
1063
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001064#pragma mark CVR modifiers
1065
Greg Clayton1be10fc2010-09-29 01:12:09 +00001066clang_type_t
1067ClangASTContext::AddConstModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001068{
1069 if (clang_type)
1070 {
1071 QualType result(QualType::getFromOpaquePtr(clang_type));
1072 result.addConst();
1073 return result.getAsOpaquePtr();
1074 }
1075 return NULL;
1076}
1077
Greg Clayton1be10fc2010-09-29 01:12:09 +00001078clang_type_t
1079ClangASTContext::AddRestrictModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001080{
1081 if (clang_type)
1082 {
1083 QualType result(QualType::getFromOpaquePtr(clang_type));
1084 result.getQualifiers().setRestrict (true);
1085 return result.getAsOpaquePtr();
1086 }
1087 return NULL;
1088}
1089
Greg Clayton1be10fc2010-09-29 01:12:09 +00001090clang_type_t
1091ClangASTContext::AddVolatileModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001092{
1093 if (clang_type)
1094 {
1095 QualType result(QualType::getFromOpaquePtr(clang_type));
1096 result.getQualifiers().setVolatile (true);
1097 return result.getAsOpaquePtr();
1098 }
1099 return NULL;
1100}
1101
Greg Clayton6beaaa62011-01-17 03:46:26 +00001102
1103clang_type_t
1104ClangASTContext::GetTypeForDecl (TagDecl *decl)
1105{
1106 // No need to call the getASTContext() accessor (which can create the AST
1107 // if it isn't created yet, because we can't have created a decl in this
1108 // AST if our AST didn't already exist...
1109 if (m_ast_ap.get())
1110 return m_ast_ap->getTagDeclType(decl).getAsOpaquePtr();
1111 return NULL;
1112}
1113
1114clang_type_t
1115ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1116{
1117 // No need to call the getASTContext() accessor (which can create the AST
1118 // if it isn't created yet, because we can't have created a decl in this
1119 // AST if our AST didn't already exist...
1120 if (m_ast_ap.get())
1121 return m_ast_ap->getObjCInterfaceType(decl).getAsOpaquePtr();
1122 return NULL;
1123}
1124
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001125#pragma mark Structure, Unions, Classes
1126
Greg Clayton1be10fc2010-09-29 01:12:09 +00001127clang_type_t
Greg Clayton55561e92011-10-26 03:31:36 +00001128ClangASTContext::CreateRecordType (DeclContext *decl_ctx, AccessType access_type, const char *name, int kind, LanguageType language)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001129{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001130 ASTContext *ast = getASTContext();
1131 assert (ast != NULL);
Sean Callanana2424172010-10-25 00:29:48 +00001132
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001134 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001135
Greg Clayton9e409562010-07-28 02:04:09 +00001136
Greg Claytone1be9962011-08-24 23:50:00 +00001137 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
Greg Clayton9e409562010-07-28 02:04:09 +00001138 {
Greg Claytonaaf99e02010-10-11 02:25:34 +00001139 bool isForwardDecl = true;
Greg Clayton9e409562010-07-28 02:04:09 +00001140 bool isInternal = false;
1141 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal);
1142 }
1143
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1145 // we will need to update this code. I was told to currently always use
1146 // the CXXRecordDecl class since we often don't know from debug information
1147 // if something is struct or a class, so we default to always use the more
1148 // complete definition just in case.
Greg Claytonf0705c82011-10-22 03:33:13 +00001149 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1150 (TagDecl::TagKind)kind,
1151 decl_ctx,
1152 SourceLocation(),
1153 SourceLocation(),
1154 name && name[0] ? &ast->Idents.get(name) : NULL);
Sean Callanan7282e2a2012-01-13 22:10:18 +00001155
1156 if (!name)
1157 decl->setAnonymousStructOrUnion(true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158
Greg Clayton55561e92011-10-26 03:31:36 +00001159 if (decl_ctx)
1160 {
1161 if (access_type != eAccessNone)
1162 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
1163 decl_ctx->addDecl (decl);
1164 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001165 return ast->getTagDeclType(decl).getAsOpaquePtr();
1166}
1167
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001168static TemplateParameterList *
1169CreateTemplateParameterList (ASTContext *ast,
1170 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1171 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1172{
1173 const bool parameter_pack = false;
1174 const bool is_typename = false;
1175 const unsigned depth = 0;
1176 const size_t num_template_params = template_param_infos.GetSize();
1177 for (size_t i=0; i<num_template_params; ++i)
1178 {
1179 const char *name = template_param_infos.names[i];
1180 if (template_param_infos.args[i].getAsIntegral())
1181 {
1182 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1183 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1184 SourceLocation(),
1185 SourceLocation(),
1186 depth,
1187 i,
1188 &ast->Idents.get(name),
1189 template_param_infos.args[i].getIntegralType(),
1190 parameter_pack,
1191 NULL));
1192
1193 }
1194 else
1195 {
1196 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1197 ast->getTranslationUnitDecl(), // Is this the right decl context?
1198 SourceLocation(),
1199 SourceLocation(),
1200 depth,
1201 i,
1202 &ast->Idents.get(name),
1203 is_typename,
1204 parameter_pack));
1205 }
1206 }
1207
1208 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1209 SourceLocation(),
1210 SourceLocation(),
1211 &template_param_decls.front(),
1212 template_param_decls.size(),
1213 SourceLocation());
1214 return template_param_list;
1215}
1216
1217clang::FunctionTemplateDecl *
1218ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1219 clang::FunctionDecl *func_decl,
1220 const char *name,
1221 const TemplateParameterInfos &template_param_infos)
1222{
1223// /// \brief Create a function template node.
1224 ASTContext *ast = getASTContext();
1225
1226 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1227
1228 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1229 template_param_infos,
1230 template_param_decls);
1231 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1232 decl_ctx,
1233 func_decl->getLocation(),
1234 func_decl->getDeclName(),
1235 template_param_list,
1236 func_decl);
1237
1238 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1239 i < template_param_decl_count;
1240 ++i)
1241 {
1242 // TODO: verify which decl context we should put template_param_decls into..
1243 template_param_decls[i]->setDeclContext (func_decl);
1244 }
1245
1246 return func_tmpl_decl;
1247}
1248
1249void
1250ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1251 clang::FunctionTemplateDecl *func_tmpl_decl,
1252 const TemplateParameterInfos &infos)
1253{
1254 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1255 infos.args.data(),
1256 infos.args.size());
1257
1258 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1259 &template_args,
1260 NULL);
1261}
1262
1263
Greg Claytonf0705c82011-10-22 03:33:13 +00001264ClassTemplateDecl *
1265ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001266 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001267 const char *class_name,
1268 int kind,
1269 const TemplateParameterInfos &template_param_infos)
1270{
1271 ASTContext *ast = getASTContext();
1272
1273 ClassTemplateDecl *class_template_decl = NULL;
1274 if (decl_ctx == NULL)
1275 decl_ctx = ast->getTranslationUnitDecl();
1276
1277 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1278 DeclarationName decl_name (&identifier_info);
1279
1280 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1281 for (clang::DeclContext::lookup_iterator pos = result.first, end = result.second; pos != end; ++pos)
1282 {
1283 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(*pos);
1284 if (class_template_decl)
1285 return class_template_decl;
1286 }
1287
1288 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Claytonf0705c82011-10-22 03:33:13 +00001289
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001290 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1291 template_param_infos,
1292 template_param_decls);
Greg Claytonf0705c82011-10-22 03:33:13 +00001293
1294 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1295 (TagDecl::TagKind)kind,
1296 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1297 SourceLocation(),
1298 SourceLocation(),
1299 &identifier_info);
Greg Claytone04741d2011-12-02 02:09:28 +00001300
1301 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1302 i < template_param_decl_count;
1303 ++i)
1304 {
1305 template_param_decls[i]->setDeclContext (template_cxx_decl);
1306 }
1307
Sean Callananb5c79622011-11-19 01:35:08 +00001308 // With templated classes, we say that a class is templated with
1309 // specializations, but that the bare class has no functions.
1310 template_cxx_decl->startDefinition();
1311 template_cxx_decl->completeDefinition();
1312
Greg Claytonf0705c82011-10-22 03:33:13 +00001313 class_template_decl = ClassTemplateDecl::Create (*ast,
1314 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1315 SourceLocation(),
1316 decl_name,
1317 template_param_list,
1318 template_cxx_decl,
1319 NULL);
1320
1321 if (class_template_decl)
Sean Callanan5e9e1992011-10-26 01:06:27 +00001322 {
Greg Clayton55561e92011-10-26 03:31:36 +00001323 if (access_type != eAccessNone)
1324 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Sean Callanan5b26f272012-02-04 08:49:35 +00001325
1326 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1327 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1328
Greg Claytonf0705c82011-10-22 03:33:13 +00001329 decl_ctx->addDecl (class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001330
1331#ifdef LLDB_CONFIGURATION_DEBUG
1332 VerifyDecl(class_template_decl);
1333#endif
1334 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001335
1336 return class_template_decl;
1337}
1338
1339
1340ClassTemplateSpecializationDecl *
1341ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1342 ClassTemplateDecl *class_template_decl,
1343 int kind,
1344 const TemplateParameterInfos &template_param_infos)
1345{
1346 ASTContext *ast = getASTContext();
1347 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1348 (TagDecl::TagKind)kind,
1349 decl_ctx,
1350 SourceLocation(),
1351 SourceLocation(),
1352 class_template_decl,
1353 &template_param_infos.args.front(),
1354 template_param_infos.args.size(),
1355 NULL);
1356
1357 return class_template_specialization_decl;
1358}
1359
1360lldb::clang_type_t
1361ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1362{
1363 if (class_template_specialization_decl)
1364 {
1365 ASTContext *ast = getASTContext();
1366 if (ast)
1367 return ast->getTagDeclType(class_template_specialization_decl).getAsOpaquePtr();
1368 }
1369 return NULL;
1370}
1371
Greg Clayton6beaaa62011-01-17 03:46:26 +00001372bool
1373ClangASTContext::SetHasExternalStorage (clang_type_t clang_type, bool has_extern)
1374{
1375 if (clang_type == NULL)
1376 return false;
1377
1378 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
1379
1380 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
1381 switch (type_class)
1382 {
1383 case clang::Type::Record:
1384 {
1385 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
1386 if (cxx_record_decl)
1387 {
1388 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001389 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001390 return true;
1391 }
1392 }
1393 break;
1394
1395 case clang::Type::Enum:
1396 {
1397 EnumDecl *enum_decl = cast<EnumType>(qual_type)->getDecl();
1398 if (enum_decl)
1399 {
1400 enum_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001401 enum_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001402 return true;
1403 }
1404 }
1405 break;
1406
1407 case clang::Type::ObjCObject:
1408 case clang::Type::ObjCInterface:
1409 {
Sean Callanan78e37602011-01-27 04:42:51 +00001410 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001411 assert (objc_class_type);
1412 if (objc_class_type)
1413 {
1414 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1415
1416 if (class_interface_decl)
1417 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001418 class_interface_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001419 class_interface_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001420 return true;
1421 }
1422 }
1423 }
1424 break;
1425
1426 case clang::Type::Typedef:
1427 return ClangASTContext::SetHasExternalStorage (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
Sean Callanan912855f2011-08-11 23:56:13 +00001428
1429 case clang::Type::Elaborated:
1430 return ClangASTContext::SetHasExternalStorage (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001431
1432 default:
1433 break;
1434 }
1435 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001436}
1437
Greg Claytona3c444a2010-10-01 23:13:49 +00001438static bool
1439IsOperator (const char *name, OverloadedOperatorKind &op_kind)
1440{
1441 if (name == NULL || name[0] == '\0')
1442 return false;
1443
Sean Callanana43f20d2010-12-10 19:51:54 +00001444#define OPERATOR_PREFIX "operator"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001445#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
Sean Callananbfeff8c2010-12-10 02:15:55 +00001446
1447 const char *post_op_name = NULL;
1448
Sean Callanana43f20d2010-12-10 19:51:54 +00001449 bool no_space = true;
Sean Callananbfeff8c2010-12-10 02:15:55 +00001450
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001451 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
Greg Claytona3c444a2010-10-01 23:13:49 +00001452 return false;
1453
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001454 post_op_name = name + OPERATOR_PREFIX_LENGTH;
1455
Sean Callanana43f20d2010-12-10 19:51:54 +00001456 if (post_op_name[0] == ' ')
1457 {
1458 post_op_name++;
1459 no_space = false;
1460 }
1461
1462#undef OPERATOR_PREFIX
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001463#undef OPERATOR_PREFIX_LENGTH
Sean Callanana43f20d2010-12-10 19:51:54 +00001464
Greg Claytona3c444a2010-10-01 23:13:49 +00001465 // This is an operator, set the overloaded operator kind to invalid
1466 // in case this is a conversion operator...
1467 op_kind = NUM_OVERLOADED_OPERATORS;
1468
1469 switch (post_op_name[0])
1470 {
Sean Callananbfeff8c2010-12-10 02:15:55 +00001471 default:
1472 if (no_space)
1473 return false;
1474 break;
Greg Claytona3c444a2010-10-01 23:13:49 +00001475 case 'n':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001476 if (no_space)
1477 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001478 if (strcmp (post_op_name, "new") == 0)
1479 op_kind = OO_New;
1480 else if (strcmp (post_op_name, "new[]") == 0)
1481 op_kind = OO_Array_New;
1482 break;
1483
1484 case 'd':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001485 if (no_space)
1486 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001487 if (strcmp (post_op_name, "delete") == 0)
1488 op_kind = OO_Delete;
1489 else if (strcmp (post_op_name, "delete[]") == 0)
1490 op_kind = OO_Array_Delete;
1491 break;
1492
1493 case '+':
1494 if (post_op_name[1] == '\0')
1495 op_kind = OO_Plus;
1496 else if (post_op_name[2] == '\0')
1497 {
1498 if (post_op_name[1] == '=')
1499 op_kind = OO_PlusEqual;
1500 else if (post_op_name[1] == '+')
1501 op_kind = OO_PlusPlus;
1502 }
1503 break;
1504
1505 case '-':
1506 if (post_op_name[1] == '\0')
1507 op_kind = OO_Minus;
1508 else if (post_op_name[2] == '\0')
1509 {
1510 switch (post_op_name[1])
1511 {
1512 case '=': op_kind = OO_MinusEqual; break;
1513 case '-': op_kind = OO_MinusMinus; break;
1514 case '>': op_kind = OO_Arrow; break;
1515 }
1516 }
1517 else if (post_op_name[3] == '\0')
1518 {
1519 if (post_op_name[2] == '*')
1520 op_kind = OO_ArrowStar; break;
1521 }
1522 break;
1523
1524 case '*':
1525 if (post_op_name[1] == '\0')
1526 op_kind = OO_Star;
1527 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1528 op_kind = OO_StarEqual;
1529 break;
1530
1531 case '/':
1532 if (post_op_name[1] == '\0')
1533 op_kind = OO_Slash;
1534 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1535 op_kind = OO_SlashEqual;
1536 break;
1537
1538 case '%':
1539 if (post_op_name[1] == '\0')
1540 op_kind = OO_Percent;
1541 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1542 op_kind = OO_PercentEqual;
1543 break;
1544
1545
1546 case '^':
1547 if (post_op_name[1] == '\0')
1548 op_kind = OO_Caret;
1549 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1550 op_kind = OO_CaretEqual;
1551 break;
1552
1553 case '&':
1554 if (post_op_name[1] == '\0')
1555 op_kind = OO_Amp;
1556 else if (post_op_name[2] == '\0')
1557 {
1558 switch (post_op_name[1])
1559 {
1560 case '=': op_kind = OO_AmpEqual; break;
1561 case '&': op_kind = OO_AmpAmp; break;
1562 }
1563 }
1564 break;
1565
1566 case '|':
1567 if (post_op_name[1] == '\0')
1568 op_kind = OO_Pipe;
1569 else if (post_op_name[2] == '\0')
1570 {
1571 switch (post_op_name[1])
1572 {
1573 case '=': op_kind = OO_PipeEqual; break;
1574 case '|': op_kind = OO_PipePipe; break;
1575 }
1576 }
1577 break;
1578
1579 case '~':
1580 if (post_op_name[1] == '\0')
1581 op_kind = OO_Tilde;
1582 break;
1583
1584 case '!':
1585 if (post_op_name[1] == '\0')
1586 op_kind = OO_Exclaim;
1587 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1588 op_kind = OO_ExclaimEqual;
1589 break;
1590
1591 case '=':
1592 if (post_op_name[1] == '\0')
1593 op_kind = OO_Equal;
1594 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1595 op_kind = OO_EqualEqual;
1596 break;
1597
1598 case '<':
1599 if (post_op_name[1] == '\0')
1600 op_kind = OO_Less;
1601 else if (post_op_name[2] == '\0')
1602 {
1603 switch (post_op_name[1])
1604 {
1605 case '<': op_kind = OO_LessLess; break;
1606 case '=': op_kind = OO_LessEqual; break;
1607 }
1608 }
1609 else if (post_op_name[3] == '\0')
1610 {
1611 if (post_op_name[2] == '=')
1612 op_kind = OO_LessLessEqual;
1613 }
1614 break;
1615
1616 case '>':
1617 if (post_op_name[1] == '\0')
1618 op_kind = OO_Greater;
1619 else if (post_op_name[2] == '\0')
1620 {
1621 switch (post_op_name[1])
1622 {
1623 case '>': op_kind = OO_GreaterGreater; break;
1624 case '=': op_kind = OO_GreaterEqual; break;
1625 }
1626 }
1627 else if (post_op_name[1] == '>' &&
1628 post_op_name[2] == '=' &&
1629 post_op_name[3] == '\0')
1630 {
1631 op_kind = OO_GreaterGreaterEqual;
1632 }
1633 break;
1634
1635 case ',':
1636 if (post_op_name[1] == '\0')
1637 op_kind = OO_Comma;
1638 break;
1639
1640 case '(':
1641 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
1642 op_kind = OO_Call;
1643 break;
1644
1645 case '[':
1646 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
1647 op_kind = OO_Subscript;
1648 break;
1649 }
1650
1651 return true;
1652}
Greg Clayton6beaaa62011-01-17 03:46:26 +00001653
Greg Clayton090d0982011-06-19 03:43:27 +00001654static inline bool
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001655check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
Greg Clayton090d0982011-06-19 03:43:27 +00001656{
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001657 // Special-case call since it can take any number of operands
1658 if(op_kind == OO_Call)
1659 return true;
1660
Greg Clayton090d0982011-06-19 03:43:27 +00001661 // The parameter count doens't include "this"
1662 if (num_params == 0)
1663 return unary;
1664 if (num_params == 1)
1665 return binary;
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001666 else
Greg Clayton090d0982011-06-19 03:43:27 +00001667 return false;
1668}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001669
Greg Clayton090d0982011-06-19 03:43:27 +00001670bool
1671ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1672{
Sean Callanan5b26f272012-02-04 08:49:35 +00001673 switch (op_kind)
1674 {
1675 default:
1676 break;
1677 // C++ standard allows any number of arguments to new/delete
1678 case OO_New:
1679 case OO_Array_New:
1680 case OO_Delete:
1681 case OO_Array_Delete:
1682 return true;
1683 }
1684
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001685#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) case OO_##Name: return check_op_param (op_kind, Unary, Binary, num_params);
Greg Clayton090d0982011-06-19 03:43:27 +00001686 switch (op_kind)
1687 {
1688#include "clang/Basic/OperatorKinds.def"
1689 default: break;
1690 }
1691 return false;
1692}
1693
Greg Claytona51ed9b2010-09-23 01:09:21 +00001694CXXMethodDecl *
Sean Callanan61da09b2010-09-17 02:58:26 +00001695ClangASTContext::AddMethodToCXXRecordType
1696(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001697 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001698 clang_type_t record_opaque_type,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001699 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001700 clang_type_t method_opaque_type,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001701 lldb::AccessType access,
Greg Clayton0fffff52010-09-24 05:15:53 +00001702 bool is_virtual,
1703 bool is_static,
Greg Claytonf51de672010-10-01 02:31:07 +00001704 bool is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00001705 bool is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00001706 bool is_attr_used,
1707 bool is_artificial
Greg Claytona51ed9b2010-09-23 01:09:21 +00001708)
Sean Callanan61da09b2010-09-17 02:58:26 +00001709{
Sean Callananfc55f5d2010-09-21 00:44:12 +00001710 if (!record_opaque_type || !method_opaque_type || !name)
Johnny Chend440bcc2010-09-28 16:10:54 +00001711 return NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001712
Greg Clayton6beaaa62011-01-17 03:46:26 +00001713 assert(ast);
Sean Callanan61da09b2010-09-17 02:58:26 +00001714
Greg Clayton6beaaa62011-01-17 03:46:26 +00001715 IdentifierTable *identifier_table = &ast->Idents;
Sean Callanan61da09b2010-09-17 02:58:26 +00001716
1717 assert(identifier_table);
1718
Sean Callananfc55f5d2010-09-21 00:44:12 +00001719 QualType record_qual_type(QualType::getFromOpaquePtr(record_opaque_type));
Greg Clayton0fffff52010-09-24 05:15:53 +00001720
Greg Clayton6beaaa62011-01-17 03:46:26 +00001721 CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
Sean Callanan61da09b2010-09-17 02:58:26 +00001722
Greg Clayton0fffff52010-09-24 05:15:53 +00001723 if (cxx_record_decl == NULL)
Greg Claytona51ed9b2010-09-23 01:09:21 +00001724 return NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001725
Greg Clayton0fffff52010-09-24 05:15:53 +00001726 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001727
Greg Claytonf51de672010-10-01 02:31:07 +00001728 CXXMethodDecl *cxx_method_decl = NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001729
Greg Claytonf51de672010-10-01 02:31:07 +00001730 DeclarationName decl_name (&identifier_table->get(name));
Greg Clayton878eaf12010-10-01 03:45:20 +00001731
Sean Callanan78e37602011-01-27 04:42:51 +00001732 const clang::FunctionType *function_Type = dyn_cast<FunctionType>(method_qual_type.getTypePtr());
Greg Clayton878eaf12010-10-01 03:45:20 +00001733
Greg Clayton90a2acd2010-10-02 01:40:05 +00001734 if (function_Type == NULL)
Greg Clayton878eaf12010-10-01 03:45:20 +00001735 return NULL;
1736
Sean Callanan78e37602011-01-27 04:42:51 +00001737 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(function_Type));
Greg Clayton878eaf12010-10-01 03:45:20 +00001738
1739 if (!method_function_prototype)
1740 return NULL;
1741
1742 unsigned int num_params = method_function_prototype->getNumArgs();
1743
Sean Callanandbb58392011-11-02 01:38:59 +00001744 CXXDestructorDecl *cxx_dtor_decl(NULL);
1745 CXXConstructorDecl *cxx_ctor_decl(NULL);
1746
Greg Clayton878eaf12010-10-01 03:45:20 +00001747 if (name[0] == '~')
Greg Claytonf51de672010-10-01 02:31:07 +00001748 {
Sean Callanandbb58392011-11-02 01:38:59 +00001749 cxx_dtor_decl = CXXDestructorDecl::Create (*ast,
1750 cxx_record_decl,
1751 SourceLocation(),
1752 DeclarationNameInfo (ast->DeclarationNames.getCXXDestructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1753 method_qual_type,
1754 NULL,
1755 is_inline,
1756 is_artificial);
1757 cxx_method_decl = cxx_dtor_decl;
Greg Clayton878eaf12010-10-01 03:45:20 +00001758 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001759 else if (decl_name == cxx_record_decl->getDeclName())
Greg Clayton878eaf12010-10-01 03:45:20 +00001760 {
Sean Callanandbb58392011-11-02 01:38:59 +00001761 cxx_ctor_decl = CXXConstructorDecl::Create (*ast,
1762 cxx_record_decl,
1763 SourceLocation(),
1764 DeclarationNameInfo (ast->DeclarationNames.getCXXConstructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1765 method_qual_type,
1766 NULL, // TypeSourceInfo *
1767 is_explicit,
1768 is_inline,
1769 is_artificial,
1770 false /*is_constexpr*/);
1771 cxx_method_decl = cxx_ctor_decl;
Greg Claytonf51de672010-10-01 02:31:07 +00001772 }
1773 else
Greg Clayton878eaf12010-10-01 03:45:20 +00001774 {
Greg Claytona3c444a2010-10-01 23:13:49 +00001775
1776 OverloadedOperatorKind op_kind = NUM_OVERLOADED_OPERATORS;
1777 if (IsOperator (name, op_kind))
Greg Clayton878eaf12010-10-01 03:45:20 +00001778 {
Greg Claytona3c444a2010-10-01 23:13:49 +00001779 if (op_kind != NUM_OVERLOADED_OPERATORS)
1780 {
Greg Clayton090d0982011-06-19 03:43:27 +00001781 // Check the number of operator parameters. Sometimes we have
1782 // seen bad DWARF that doesn't correctly describe operators and
1783 // if we try to create a methed and add it to the class, clang
1784 // will assert and crash, so we need to make sure things are
1785 // acceptable.
1786 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
1787 return NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001788 cxx_method_decl = CXXMethodDecl::Create (*ast,
Greg Clayton878eaf12010-10-01 03:45:20 +00001789 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001790 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001791 DeclarationNameInfo (ast->DeclarationNames.getCXXOperatorName (op_kind), SourceLocation()),
Greg Clayton878eaf12010-10-01 03:45:20 +00001792 method_qual_type,
1793 NULL, // TypeSourceInfo *
Greg Claytona3c444a2010-10-01 23:13:49 +00001794 is_static,
1795 SC_None,
Sean Callananfb0b7582011-03-15 00:17:19 +00001796 is_inline,
Sean Callanan880e6802011-10-07 23:18:13 +00001797 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001798 SourceLocation());
Greg Claytona3c444a2010-10-01 23:13:49 +00001799 }
1800 else if (num_params == 0)
1801 {
1802 // Conversion operators don't take params...
Greg Clayton6beaaa62011-01-17 03:46:26 +00001803 cxx_method_decl = CXXConversionDecl::Create (*ast,
Greg Claytona3c444a2010-10-01 23:13:49 +00001804 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001805 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001806 DeclarationNameInfo (ast->DeclarationNames.getCXXConversionFunctionName (ast->getCanonicalType (function_Type->getResultType())), SourceLocation()),
Greg Claytona3c444a2010-10-01 23:13:49 +00001807 method_qual_type,
1808 NULL, // TypeSourceInfo *
1809 is_inline,
Sean Callananfb0b7582011-03-15 00:17:19 +00001810 is_explicit,
Sean Callanan880e6802011-10-07 23:18:13 +00001811 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001812 SourceLocation());
Greg Claytona3c444a2010-10-01 23:13:49 +00001813 }
Greg Clayton878eaf12010-10-01 03:45:20 +00001814 }
Greg Claytona3c444a2010-10-01 23:13:49 +00001815
1816 if (cxx_method_decl == NULL)
Greg Clayton878eaf12010-10-01 03:45:20 +00001817 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001818 cxx_method_decl = CXXMethodDecl::Create (*ast,
Greg Clayton878eaf12010-10-01 03:45:20 +00001819 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001820 SourceLocation(),
Greg Claytona3c444a2010-10-01 23:13:49 +00001821 DeclarationNameInfo (decl_name, SourceLocation()),
Greg Clayton878eaf12010-10-01 03:45:20 +00001822 method_qual_type,
1823 NULL, // TypeSourceInfo *
1824 is_static,
1825 SC_None,
Sean Callananfb0b7582011-03-15 00:17:19 +00001826 is_inline,
Sean Callanan880e6802011-10-07 23:18:13 +00001827 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001828 SourceLocation());
Greg Clayton878eaf12010-10-01 03:45:20 +00001829 }
Greg Claytonf51de672010-10-01 02:31:07 +00001830 }
Greg Claytona3c444a2010-10-01 23:13:49 +00001831
Greg Clayton1be10fc2010-09-29 01:12:09 +00001832 AccessSpecifier access_specifier = ConvertAccessTypeToAccessSpecifier (access);
Greg Clayton0fffff52010-09-24 05:15:53 +00001833
1834 cxx_method_decl->setAccess (access_specifier);
1835 cxx_method_decl->setVirtualAsWritten (is_virtual);
Sean Callanane2ef6e32010-09-23 03:01:22 +00001836
Sean Callananc1b732d2011-11-01 18:07:13 +00001837 if (is_attr_used)
1838 cxx_method_decl->addAttr(::new (*ast) UsedAttr(SourceRange(), *ast));
1839
Sean Callananfc55f5d2010-09-21 00:44:12 +00001840 // Populate the method decl with parameter decls
Sean Callananfc55f5d2010-09-21 00:44:12 +00001841
Charles Davis8c444c42011-05-19 23:33:46 +00001842 llvm::SmallVector<ParmVarDecl *, 12> params;
Sean Callananfc55f5d2010-09-21 00:44:12 +00001843
1844 for (int param_index = 0;
1845 param_index < num_params;
1846 ++param_index)
1847 {
Charles Davis8c444c42011-05-19 23:33:46 +00001848 params.push_back (ParmVarDecl::Create (*ast,
1849 cxx_method_decl,
1850 SourceLocation(),
1851 SourceLocation(),
1852 NULL, // anonymous
1853 method_function_prototype->getArgType(param_index),
1854 NULL,
1855 SC_None,
1856 SC_None,
1857 NULL));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001858 }
1859
Sean Callanan880e6802011-10-07 23:18:13 +00001860 cxx_method_decl->setParams (ArrayRef<ParmVarDecl*>(params));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001861
Greg Clayton0fffff52010-09-24 05:15:53 +00001862 cxx_record_decl->addDecl (cxx_method_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001863
Greg Clayton8b867b42011-11-02 02:06:20 +00001864 // Sometimes the debug info will mention a constructor (default/copy/move),
1865 // destructor, or assignment operator (copy/move) but there won't be any
1866 // version of this in the code. So we check if the function was artificially
1867 // generated and if it is trivial and this lets the compiler/backend know
1868 // that it can inline the IR for these when it needs to and we can avoid a
1869 // "missing function" error when running expressions.
1870
Sean Callanandbb58392011-11-02 01:38:59 +00001871 if (is_artificial)
1872 {
Greg Clayton8b867b42011-11-02 02:06:20 +00001873 if (cxx_ctor_decl &&
1874 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
1875 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
1876 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
Sean Callanandbb58392011-11-02 01:38:59 +00001877 {
1878 cxx_ctor_decl->setDefaulted();
1879 cxx_ctor_decl->setTrivial(true);
1880 }
Greg Clayton8b867b42011-11-02 02:06:20 +00001881 else if (cxx_dtor_decl)
Sean Callanandbb58392011-11-02 01:38:59 +00001882 {
Greg Clayton8b867b42011-11-02 02:06:20 +00001883 if (cxx_record_decl->hasTrivialDestructor())
1884 {
1885 cxx_dtor_decl->setDefaulted();
1886 cxx_dtor_decl->setTrivial(true);
1887 }
1888 }
1889 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
1890 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
1891 {
1892 cxx_method_decl->setDefaulted();
1893 cxx_method_decl->setTrivial(true);
Sean Callanandbb58392011-11-02 01:38:59 +00001894 }
1895 }
1896
Sean Callanan5e9e1992011-10-26 01:06:27 +00001897#ifdef LLDB_CONFIGURATION_DEBUG
1898 VerifyDecl(cxx_method_decl);
1899#endif
Greg Claytonc432c192011-01-20 04:18:48 +00001900
1901// printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
1902// printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
1903// printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
1904// printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
1905// printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
1906// printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
1907// printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
1908// printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
1909// printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
Greg Claytona51ed9b2010-09-23 01:09:21 +00001910 return cxx_method_decl;
Sean Callanan61da09b2010-09-17 02:58:26 +00001911}
1912
Jim Inghame3ae82a2011-11-12 01:36:43 +00001913clang::FieldDecl *
Greg Clayton8cf05932010-07-22 18:30:50 +00001914ClangASTContext::AddFieldToRecordType
1915(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001916 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001917 clang_type_t record_clang_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00001918 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001919 clang_type_t field_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00001920 AccessType access,
1921 uint32_t bitfield_bit_size
1922)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001923{
1924 if (record_clang_type == NULL || field_type == NULL)
Jim Inghame3ae82a2011-11-12 01:36:43 +00001925 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001926
Jim Inghame3ae82a2011-11-12 01:36:43 +00001927 FieldDecl *field = NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001928 IdentifierTable *identifier_table = &ast->Idents;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001929
Greg Clayton6beaaa62011-01-17 03:46:26 +00001930 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001931 assert (identifier_table != NULL);
1932
1933 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
1934
Sean Callanan78e37602011-01-27 04:42:51 +00001935 const clang::Type *clang_type = record_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001936 if (clang_type)
1937 {
1938 const RecordType *record_type = dyn_cast<RecordType>(clang_type);
1939
1940 if (record_type)
1941 {
1942 RecordDecl *record_decl = record_type->getDecl();
1943
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001944 clang::Expr *bit_width = NULL;
1945 if (bitfield_bit_size != 0)
1946 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001947 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
1948 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001949 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001950 field = FieldDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00001951 record_decl,
1952 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00001953 SourceLocation(),
Greg Clayton8cf05932010-07-22 18:30:50 +00001954 name ? &identifier_table->get(name) : NULL, // Identifier
1955 QualType::getFromOpaquePtr(field_type), // Field type
Sean Callanancc427fa2011-07-30 02:42:06 +00001956 NULL, // TInfo *
Greg Clayton8cf05932010-07-22 18:30:50 +00001957 bit_width, // BitWidth
Sean Callanancc427fa2011-07-30 02:42:06 +00001958 false, // Mutable
1959 false); // HasInit
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001960
1961 if (!name)
1962 field->setImplicit();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001963
Greg Clayton8cf05932010-07-22 18:30:50 +00001964 field->setAccess (ConvertAccessTypeToAccessSpecifier (access));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001965
1966 if (field)
1967 {
1968 record_decl->addDecl(field);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001969
1970#ifdef LLDB_CONFIGURATION_DEBUG
1971 VerifyDecl(field);
1972#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001973 }
1974 }
Greg Clayton9e409562010-07-28 02:04:09 +00001975 else
1976 {
Sean Callanan78e37602011-01-27 04:42:51 +00001977 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001978 if (objc_class_type)
1979 {
Greg Clayton0fffff52010-09-24 05:15:53 +00001980 bool is_synthesized = false;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001981 field = ClangASTContext::AddObjCClassIVar (ast,
Sean Callanan6e6a7c72010-09-16 20:01:08 +00001982 record_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001983 name,
1984 field_type,
1985 access,
1986 bitfield_bit_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00001987 is_synthesized);
Greg Clayton9e409562010-07-28 02:04:09 +00001988 }
1989 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001990 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001991 return field;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001992}
1993
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001994static clang::AccessSpecifier UnifyAccessSpecifiers (clang::AccessSpecifier lhs,
1995 clang::AccessSpecifier rhs)
1996{
1997 clang::AccessSpecifier ret = lhs;
1998
1999 // Make the access equal to the stricter of the field and the nested field's access
2000 switch (ret)
2001 {
2002 case clang::AS_none:
2003 break;
2004 case clang::AS_private:
2005 break;
2006 case clang::AS_protected:
2007 if (rhs == AS_private)
2008 ret = AS_private;
2009 break;
2010 case clang::AS_public:
2011 ret = rhs;
2012 break;
2013 }
2014
2015 return ret;
2016}
2017
2018void
2019ClangASTContext::BuildIndirectFields (clang::ASTContext *ast,
2020 lldb::clang_type_t record_clang_type)
2021{
2022 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
2023
2024 const RecordType *record_type = record_qual_type->getAs<RecordType>();
2025
2026 if (!record_type)
2027 return;
2028
2029 RecordDecl *record_decl = record_type->getDecl();
2030
2031 if (!record_decl)
2032 return;
2033
2034 typedef llvm::SmallVector <IndirectFieldDecl *, 1> IndirectFieldVector;
2035
2036 IndirectFieldVector indirect_fields;
2037
2038 for (RecordDecl::field_iterator fi = record_decl->field_begin(), fe = record_decl->field_end();
2039 fi != fe;
2040 ++fi)
2041 {
2042 if (fi->isAnonymousStructOrUnion())
2043 {
2044 QualType field_qual_type = fi->getType();
2045
2046 const RecordType *field_record_type = field_qual_type->getAs<RecordType>();
2047
2048 if (!field_record_type)
2049 continue;
2050
2051 RecordDecl *field_record_decl = field_record_type->getDecl();
2052
2053 if (!field_record_decl)
2054 continue;
2055
2056 for (RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
2057 di != de;
2058 ++di)
2059 {
2060 if (FieldDecl *nested_field_decl = dyn_cast<FieldDecl>(*di))
2061 {
2062 NamedDecl **chain = new (*ast) NamedDecl*[2];
2063 chain[0] = *fi;
2064 chain[1] = nested_field_decl;
2065 IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*ast,
2066 record_decl,
2067 SourceLocation(),
2068 nested_field_decl->getIdentifier(),
2069 nested_field_decl->getType(),
2070 chain,
2071 2);
2072
2073 indirect_field->setAccess(UnifyAccessSpecifiers(fi->getAccess(),
2074 nested_field_decl->getAccess()));
2075
2076 indirect_fields.push_back(indirect_field);
2077 }
2078 else if (IndirectFieldDecl *nested_indirect_field_decl = dyn_cast<IndirectFieldDecl>(*di))
2079 {
2080 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
2081 NamedDecl **chain = new (*ast) NamedDecl*[nested_chain_size + 1];
2082 chain[0] = *fi;
2083
2084 int chain_index = 1;
2085 for (IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
2086 nce = nested_indirect_field_decl->chain_end();
2087 nci < nce;
2088 ++nci)
2089 {
2090 chain[chain_index] = *nci;
2091 chain_index++;
2092 }
2093
2094 IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*ast,
2095 record_decl,
2096 SourceLocation(),
2097 nested_indirect_field_decl->getIdentifier(),
2098 nested_indirect_field_decl->getType(),
2099 chain,
2100 nested_chain_size + 1);
2101
2102 indirect_field->setAccess(UnifyAccessSpecifiers(fi->getAccess(),
2103 nested_indirect_field_decl->getAccess()));
2104
2105 indirect_fields.push_back(indirect_field);
2106 }
2107 }
2108 }
2109 }
2110
2111 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
2112 ifi < ife;
2113 ++ifi)
2114 {
2115 record_decl->addDecl(*ifi);
2116 }
2117}
2118
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002119bool
2120ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
2121{
2122 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
2123}
2124
2125bool
2126ClangASTContext::FieldIsBitfield
2127(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002128 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002129 FieldDecl* field,
2130 uint32_t& bitfield_bit_size
2131)
2132{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002133 if (ast == NULL || field == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002134 return false;
2135
2136 if (field->isBitField())
2137 {
2138 Expr* bit_width_expr = field->getBitWidth();
2139 if (bit_width_expr)
2140 {
2141 llvm::APSInt bit_width_apsint;
Greg Clayton6beaaa62011-01-17 03:46:26 +00002142 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002143 {
2144 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
2145 return true;
2146 }
2147 }
2148 }
2149 return false;
2150}
2151
2152bool
2153ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
2154{
2155 if (record_decl == NULL)
2156 return false;
2157
2158 if (!record_decl->field_empty())
2159 return true;
2160
2161 // No fields, lets check this is a CXX record and check the base classes
2162 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2163 if (cxx_record_decl)
2164 {
2165 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2166 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2167 base_class != base_class_end;
2168 ++base_class)
2169 {
2170 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2171 if (RecordHasFields(base_class_decl))
2172 return true;
2173 }
2174 }
2175 return false;
2176}
2177
2178void
Greg Clayton6beaaa62011-01-17 03:46:26 +00002179ClangASTContext::SetDefaultAccessForRecordFields (clang_type_t clang_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002180{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002181 if (clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002182 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002183 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
2184
Sean Callanan78e37602011-01-27 04:42:51 +00002185 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +00002186 if (record_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002187 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002188 RecordDecl *record_decl = record_type->getDecl();
2189 if (record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002190 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002191 uint32_t field_idx;
2192 RecordDecl::field_iterator field, field_end;
2193 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2194 field != field_end;
2195 ++field, ++field_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002196 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002197 // If no accessibility was assigned, assign the correct one
2198 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2199 field->setAccess ((AccessSpecifier)default_accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002200 }
2201 }
2202 }
2203 }
2204}
2205
2206#pragma mark C++ Base Classes
2207
2208CXXBaseSpecifier *
Greg Clayton1be10fc2010-09-29 01:12:09 +00002209ClangASTContext::CreateBaseClassSpecifier (clang_type_t base_class_type, AccessType access, bool is_virtual, bool base_of_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002210{
2211 if (base_class_type)
Greg Claytone6371122010-07-30 20:30:44 +00002212 return new CXXBaseSpecifier (SourceRange(),
2213 is_virtual,
2214 base_of_class,
2215 ConvertAccessTypeToAccessSpecifier (access),
Sean Callanan2c777c42011-01-18 23:32:05 +00002216 getASTContext()->CreateTypeSourceInfo (QualType::getFromOpaquePtr(base_class_type)),
2217 SourceLocation());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002218 return NULL;
2219}
2220
Greg Clayton0b42ac32010-07-02 01:29:13 +00002221void
2222ClangASTContext::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes)
2223{
2224 for (unsigned i=0; i<num_base_classes; ++i)
2225 {
2226 delete base_classes[i];
2227 base_classes[i] = NULL;
2228 }
2229}
2230
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002231bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002232ClangASTContext::SetBaseClassesForClassType (clang_type_t class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002233{
2234 if (class_clang_type)
2235 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002236 CXXRecordDecl *cxx_record_decl = QualType::getFromOpaquePtr(class_clang_type)->getAsCXXRecordDecl();
2237 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002238 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002239 cxx_record_decl->setBases(base_classes, num_base_classes);
2240 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002241 }
2242 }
2243 return false;
2244}
Greg Clayton8cf05932010-07-22 18:30:50 +00002245#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002246
Greg Clayton1be10fc2010-09-29 01:12:09 +00002247clang_type_t
Greg Clayton8cf05932010-07-22 18:30:50 +00002248ClangASTContext::CreateObjCClass
2249(
2250 const char *name,
2251 DeclContext *decl_ctx,
2252 bool isForwardDecl,
2253 bool isInternal
2254)
2255{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002256 ASTContext *ast = getASTContext();
2257 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00002258 assert (name && name[0]);
2259 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00002260 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00002261
2262 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
2263 // we will need to update this code. I was told to currently always use
2264 // the CXXRecordDecl class since we often don't know from debug information
2265 // if something is struct or a class, so we default to always use the more
2266 // complete definition just in case.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002267 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00002268 decl_ctx,
2269 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00002270 &ast->Idents.get(name),
Sean Callanan5b26f272012-02-04 08:49:35 +00002271 NULL,
Greg Clayton8cf05932010-07-22 18:30:50 +00002272 SourceLocation(),
Sean Callanan5b26f272012-02-04 08:49:35 +00002273 /*isForwardDecl,*/
Greg Clayton8cf05932010-07-22 18:30:50 +00002274 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00002275
Greg Clayton6beaaa62011-01-17 03:46:26 +00002276 return ast->getObjCInterfaceType(decl).getAsOpaquePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002277}
2278
2279bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002280ClangASTContext::SetObjCSuperClass (clang_type_t class_opaque_type, clang_type_t super_opaque_type)
Greg Clayton8cf05932010-07-22 18:30:50 +00002281{
2282 if (class_opaque_type && super_opaque_type)
2283 {
2284 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2285 QualType super_qual_type(QualType::getFromOpaquePtr(super_opaque_type));
Sean Callanan78e37602011-01-27 04:42:51 +00002286 const clang::Type *class_type = class_qual_type.getTypePtr();
2287 const clang::Type *super_type = super_qual_type.getTypePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002288 if (class_type && super_type)
2289 {
Sean Callanan78e37602011-01-27 04:42:51 +00002290 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
2291 const ObjCObjectType *objc_super_type = dyn_cast<ObjCObjectType>(super_type);
Greg Clayton8cf05932010-07-22 18:30:50 +00002292 if (objc_class_type && objc_super_type)
2293 {
2294 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2295 ObjCInterfaceDecl *super_interface_decl = objc_super_type->getInterface();
2296 if (class_interface_decl && super_interface_decl)
2297 {
2298 class_interface_decl->setSuperClass(super_interface_decl);
2299 return true;
2300 }
2301 }
2302 }
2303 }
2304 return false;
2305}
2306
2307
Jim Inghame3ae82a2011-11-12 01:36:43 +00002308FieldDecl *
Greg Clayton8cf05932010-07-22 18:30:50 +00002309ClangASTContext::AddObjCClassIVar
2310(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002311 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002312 clang_type_t class_opaque_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00002313 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002314 clang_type_t ivar_opaque_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00002315 AccessType access,
2316 uint32_t bitfield_bit_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00002317 bool is_synthesized
Greg Clayton8cf05932010-07-22 18:30:50 +00002318)
2319{
2320 if (class_opaque_type == NULL || ivar_opaque_type == NULL)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002321 return NULL;
Greg Clayton8cf05932010-07-22 18:30:50 +00002322
Jim Inghame3ae82a2011-11-12 01:36:43 +00002323 ObjCIvarDecl *field = NULL;
2324
Greg Clayton6beaaa62011-01-17 03:46:26 +00002325 IdentifierTable *identifier_table = &ast->Idents;
Greg Clayton8cf05932010-07-22 18:30:50 +00002326
Greg Clayton6beaaa62011-01-17 03:46:26 +00002327 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00002328 assert (identifier_table != NULL);
2329
2330 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2331
Sean Callanan78e37602011-01-27 04:42:51 +00002332 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002333 if (class_type)
2334 {
Sean Callanan78e37602011-01-27 04:42:51 +00002335 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton8cf05932010-07-22 18:30:50 +00002336
2337 if (objc_class_type)
2338 {
2339 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2340
2341 if (class_interface_decl)
2342 {
2343 clang::Expr *bit_width = NULL;
2344 if (bitfield_bit_size != 0)
2345 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002346 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
2347 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
Greg Clayton8cf05932010-07-22 18:30:50 +00002348 }
2349
Jim Inghame3ae82a2011-11-12 01:36:43 +00002350 field = ObjCIvarDecl::Create (*ast,
2351 class_interface_decl,
2352 SourceLocation(),
2353 SourceLocation(),
2354 &identifier_table->get(name), // Identifier
2355 QualType::getFromOpaquePtr(ivar_opaque_type), // Field type
2356 NULL, // TypeSourceInfo *
2357 ConvertAccessTypeToObjCIvarAccessControl (access),
2358 bit_width,
2359 is_synthesized);
Greg Clayton9e409562010-07-28 02:04:09 +00002360
2361 if (field)
2362 {
2363 class_interface_decl->addDecl(field);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002364
2365#ifdef LLDB_CONFIGURATION_DEBUG
2366 VerifyDecl(field);
2367#endif
2368
Jim Inghame3ae82a2011-11-12 01:36:43 +00002369 return field;
Greg Clayton9e409562010-07-28 02:04:09 +00002370 }
Greg Clayton8cf05932010-07-22 18:30:50 +00002371 }
2372 }
2373 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002374 return NULL;
2375}
2376
2377bool
2378ClangASTContext::AddObjCClassProperty
2379(
2380 ASTContext *ast,
2381 clang_type_t class_opaque_type,
2382 const char *property_name,
2383 clang_type_t property_opaque_type,
2384 ObjCIvarDecl *ivar_decl,
2385 const char *property_setter_name,
2386 const char *property_getter_name,
2387 uint32_t property_attributes
2388)
2389{
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002390 if (class_opaque_type == NULL || property_name == NULL || property_name[0] == '\0')
Jim Inghame3ae82a2011-11-12 01:36:43 +00002391 return false;
2392
2393 IdentifierTable *identifier_table = &ast->Idents;
2394
2395 assert (ast != NULL);
2396 assert (identifier_table != NULL);
2397
2398 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2399 const clang::Type *class_type = class_qual_type.getTypePtr();
2400 if (class_type)
2401 {
2402 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
2403
2404 if (objc_class_type)
2405 {
2406 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2407
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002408 clang_type_t property_opaque_type_to_access;
2409
2410 if (property_opaque_type)
2411 property_opaque_type_to_access = property_opaque_type;
2412 else if (ivar_decl)
2413 property_opaque_type_to_access = ivar_decl->getType().getAsOpaquePtr();
2414
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002415 if (class_interface_decl && property_opaque_type_to_access)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002416 {
2417 clang::TypeSourceInfo *prop_type_source;
2418 if (ivar_decl)
2419 prop_type_source = ast->CreateTypeSourceInfo (ivar_decl->getType());
2420 else
2421 prop_type_source = ast->CreateTypeSourceInfo (QualType::getFromOpaquePtr(property_opaque_type));
2422
2423 ObjCPropertyDecl *property_decl = ObjCPropertyDecl::Create(*ast,
2424 class_interface_decl,
2425 SourceLocation(), // Source Location
2426 &identifier_table->get(property_name),
2427 SourceLocation(), //Source Location for AT
Sean Callanand5f33a82012-03-01 02:03:47 +00002428 SourceLocation(), //Source location for (
Jim Inghame3ae82a2011-11-12 01:36:43 +00002429 prop_type_source
2430 );
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002431 if (property_decl)
2432 {
Jim Inghame3ae82a2011-11-12 01:36:43 +00002433 class_interface_decl->addDecl (property_decl);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002434
2435 Selector setter_sel, getter_sel;
2436
Jim Inghame3ae82a2011-11-12 01:36:43 +00002437 if (property_setter_name != NULL)
2438 {
2439 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
2440 clang::IdentifierInfo *setter_ident = &identifier_table->get(property_setter_no_colon.c_str());
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002441 setter_sel = ast->Selectors.getSelector(1, &setter_ident);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002442 }
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002443 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
2444 {
2445 std::string setter_sel_string("set");
2446 setter_sel_string.push_back(::toupper(property_name[0]));
2447 setter_sel_string.append(&property_name[1]);
2448 clang::IdentifierInfo *setter_ident = &identifier_table->get(setter_sel_string.c_str());
2449 setter_sel = ast->Selectors.getSelector(1, &setter_ident);
2450 }
Sean Callanana6582262012-04-05 00:12:52 +00002451 property_decl->setSetterName(setter_sel);
2452 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002453
2454 if (property_getter_name != NULL)
2455 {
2456 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_getter_name);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002457 getter_sel = ast->Selectors.getSelector(0, &getter_ident);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002458 }
2459 else
2460 {
2461 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_name);
2462 getter_sel = ast->Selectors.getSelector(0, &getter_ident);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002463 }
Sean Callanana6582262012-04-05 00:12:52 +00002464 property_decl->setGetterName(getter_sel);
2465 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002466
2467 if (ivar_decl)
2468 property_decl->setPropertyIvarDecl (ivar_decl);
2469
2470 if (property_attributes & DW_APPLE_PROPERTY_readonly)
2471 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
2472 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
2473 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
2474 if (property_attributes & DW_APPLE_PROPERTY_assign)
2475 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
2476 if (property_attributes & DW_APPLE_PROPERTY_retain)
2477 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
2478 if (property_attributes & DW_APPLE_PROPERTY_copy)
2479 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
2480 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
2481 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002482
2483 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
2484 {
2485 QualType result_type = QualType::getFromOpaquePtr(property_opaque_type_to_access);
2486
2487 const bool isInstance = true;
2488 const bool isVariadic = false;
2489 const bool isSynthesized = false;
2490 const bool isImplicitlyDeclared = true;
2491 const bool isDefined = false;
2492 const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
2493 const bool HasRelatedResultType = false;
2494
2495 ObjCMethodDecl *getter = ObjCMethodDecl::Create(*ast,
2496 SourceLocation(),
2497 SourceLocation(),
2498 getter_sel,
2499 result_type,
2500 NULL,
2501 class_interface_decl,
2502 isInstance,
2503 isVariadic,
2504 isSynthesized,
2505 isImplicitlyDeclared,
2506 isDefined,
2507 impControl,
2508 HasRelatedResultType);
2509
2510 getter->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(), ArrayRef<SourceLocation>());
2511
2512 class_interface_decl->addDecl(getter);
2513 }
2514
2515 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
2516 {
2517 QualType result_type = ast->VoidTy;
2518
2519 const bool isInstance = true;
2520 const bool isVariadic = false;
2521 const bool isSynthesized = false;
2522 const bool isImplicitlyDeclared = true;
2523 const bool isDefined = false;
2524 const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
2525 const bool HasRelatedResultType = false;
2526
2527 ObjCMethodDecl *setter = ObjCMethodDecl::Create(*ast,
2528 SourceLocation(),
2529 SourceLocation(),
2530 setter_sel,
2531 result_type,
2532 NULL,
2533 class_interface_decl,
2534 isInstance,
2535 isVariadic,
2536 isSynthesized,
2537 isImplicitlyDeclared,
2538 isDefined,
2539 impControl,
2540 HasRelatedResultType);
2541
2542 llvm::SmallVector<ParmVarDecl *, 1> params;
2543
2544 params.push_back (ParmVarDecl::Create (*ast,
2545 setter,
2546 SourceLocation(),
2547 SourceLocation(),
2548 NULL, // anonymous
2549 QualType::getFromOpaquePtr(property_opaque_type_to_access),
2550 NULL,
2551 SC_Auto,
2552 SC_Auto,
2553 NULL));
2554
2555 setter->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
2556
2557 class_interface_decl->addDecl(setter);
2558 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002559
2560 return true;
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002561 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002562 }
2563 }
2564 }
Greg Clayton8cf05932010-07-22 18:30:50 +00002565 return false;
2566}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002567
Greg Clayton9e409562010-07-28 02:04:09 +00002568bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002569ClangASTContext::ObjCTypeHasIVars (clang_type_t class_opaque_type, bool check_superclass)
Greg Clayton9e409562010-07-28 02:04:09 +00002570{
2571 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2572
Sean Callanan78e37602011-01-27 04:42:51 +00002573 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton9e409562010-07-28 02:04:09 +00002574 if (class_type)
2575 {
Sean Callanan78e37602011-01-27 04:42:51 +00002576 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00002577
2578 if (objc_class_type)
2579 return ObjCDeclHasIVars (objc_class_type->getInterface(), check_superclass);
2580 }
2581 return false;
2582}
2583
2584bool
2585ClangASTContext::ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
2586{
2587 while (class_interface_decl)
2588 {
2589 if (class_interface_decl->ivar_size() > 0)
2590 return true;
2591
2592 if (check_superclass)
2593 class_interface_decl = class_interface_decl->getSuperClass();
2594 else
2595 break;
2596 }
2597 return false;
2598}
Greg Clayton0fffff52010-09-24 05:15:53 +00002599
Greg Clayton1be10fc2010-09-29 01:12:09 +00002600ObjCMethodDecl *
Greg Clayton0fffff52010-09-24 05:15:53 +00002601ClangASTContext::AddMethodToObjCObjectType
2602(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002603 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002604 clang_type_t class_opaque_type,
Greg Clayton0fffff52010-09-24 05:15:53 +00002605 const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
Greg Clayton1be10fc2010-09-29 01:12:09 +00002606 clang_type_t method_opaque_type,
Greg Clayton0fffff52010-09-24 05:15:53 +00002607 lldb::AccessType access
2608)
2609{
2610 if (class_opaque_type == NULL || method_opaque_type == NULL)
2611 return NULL;
2612
Greg Clayton6beaaa62011-01-17 03:46:26 +00002613 IdentifierTable *identifier_table = &ast->Idents;
Greg Clayton0fffff52010-09-24 05:15:53 +00002614
Greg Clayton6beaaa62011-01-17 03:46:26 +00002615 assert (ast != NULL);
Greg Clayton0fffff52010-09-24 05:15:53 +00002616 assert (identifier_table != NULL);
2617
2618 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2619
Sean Callanan78e37602011-01-27 04:42:51 +00002620 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton0fffff52010-09-24 05:15:53 +00002621 if (class_type == NULL)
2622 return NULL;
2623
Sean Callanan78e37602011-01-27 04:42:51 +00002624 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton0fffff52010-09-24 05:15:53 +00002625
2626 if (objc_class_type == NULL)
2627 return NULL;
2628
2629 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2630
2631 if (class_interface_decl == NULL)
2632 return NULL;
Greg Clayton9e409562010-07-28 02:04:09 +00002633
Greg Clayton0fffff52010-09-24 05:15:53 +00002634 const char *selector_start = ::strchr (name, ' ');
2635 if (selector_start == NULL)
2636 return NULL;
2637
2638 selector_start++;
2639 if (!(::isalpha (selector_start[0]) || selector_start[0] == '_'))
2640 return NULL;
2641 llvm::SmallVector<IdentifierInfo *, 12> selector_idents;
2642
Greg Clayton450e3f32010-10-12 02:24:53 +00002643 size_t len = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +00002644 const char *start;
Greg Clayton450e3f32010-10-12 02:24:53 +00002645 //printf ("name = '%s'\n", name);
2646
2647 unsigned num_selectors_with_args = 0;
2648 for (start = selector_start;
Greg Clayton0fffff52010-09-24 05:15:53 +00002649 start && *start != '\0' && *start != ']';
Greg Clayton450e3f32010-10-12 02:24:53 +00002650 start += len)
Greg Clayton0fffff52010-09-24 05:15:53 +00002651 {
Greg Clayton450e3f32010-10-12 02:24:53 +00002652 len = ::strcspn(start, ":]");
Greg Clayton90f90cd2010-10-27 04:01:14 +00002653 bool has_arg = (start[len] == ':');
2654 if (has_arg)
Greg Clayton450e3f32010-10-12 02:24:53 +00002655 ++num_selectors_with_args;
Greg Clayton0fffff52010-09-24 05:15:53 +00002656 selector_idents.push_back (&identifier_table->get (StringRef (start, len)));
Greg Clayton90f90cd2010-10-27 04:01:14 +00002657 if (has_arg)
2658 len += 1;
Greg Clayton0fffff52010-09-24 05:15:53 +00002659 }
2660
2661
2662 if (selector_idents.size() == 0)
2663 return 0;
2664
Greg Clayton6beaaa62011-01-17 03:46:26 +00002665 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
Greg Clayton0fffff52010-09-24 05:15:53 +00002666 selector_idents.data());
2667
2668 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
2669
2670 // Populate the method decl with parameter decls
Sean Callanan78e37602011-01-27 04:42:51 +00002671 const clang::Type *method_type(method_qual_type.getTypePtr());
Greg Clayton0fffff52010-09-24 05:15:53 +00002672
2673 if (method_type == NULL)
2674 return NULL;
2675
Sean Callanan78e37602011-01-27 04:42:51 +00002676 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type));
Greg Clayton0fffff52010-09-24 05:15:53 +00002677
2678 if (!method_function_prototype)
2679 return NULL;
2680
2681
2682 bool is_variadic = false;
2683 bool is_synthesized = false;
2684 bool is_defined = false;
2685 ObjCMethodDecl::ImplementationControl imp_control = ObjCMethodDecl::None;
2686
2687 const unsigned num_args = method_function_prototype->getNumArgs();
2688
Greg Clayton6beaaa62011-01-17 03:46:26 +00002689 ObjCMethodDecl *objc_method_decl = ObjCMethodDecl::Create (*ast,
Greg Clayton0fffff52010-09-24 05:15:53 +00002690 SourceLocation(), // beginLoc,
2691 SourceLocation(), // endLoc,
2692 method_selector,
2693 method_function_prototype->getResultType(),
2694 NULL, // TypeSourceInfo *ResultTInfo,
2695 GetDeclContextForType (class_opaque_type),
2696 name[0] == '-',
2697 is_variadic,
2698 is_synthesized,
Sean Callanan880e6802011-10-07 23:18:13 +00002699 true, // is_implicitly_declared
Greg Clayton0fffff52010-09-24 05:15:53 +00002700 is_defined,
2701 imp_control,
Sean Callanan880e6802011-10-07 23:18:13 +00002702 false /*has_related_result_type*/);
Greg Clayton0fffff52010-09-24 05:15:53 +00002703
2704
2705 if (objc_method_decl == NULL)
2706 return NULL;
2707
2708 if (num_args > 0)
2709 {
2710 llvm::SmallVector<ParmVarDecl *, 12> params;
2711
2712 for (int param_index = 0; param_index < num_args; ++param_index)
2713 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002714 params.push_back (ParmVarDecl::Create (*ast,
Greg Clayton0fffff52010-09-24 05:15:53 +00002715 objc_method_decl,
2716 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00002717 SourceLocation(),
Greg Clayton0fffff52010-09-24 05:15:53 +00002718 NULL, // anonymous
2719 method_function_prototype->getArgType(param_index),
2720 NULL,
2721 SC_Auto,
2722 SC_Auto,
2723 NULL));
2724 }
2725
Sean Callanan880e6802011-10-07 23:18:13 +00002726 objc_method_decl->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
Greg Clayton0fffff52010-09-24 05:15:53 +00002727 }
2728
2729 class_interface_decl->addDecl (objc_method_decl);
2730
Sean Callanan5e9e1992011-10-26 01:06:27 +00002731#ifdef LLDB_CONFIGURATION_DEBUG
2732 VerifyDecl(objc_method_decl);
2733#endif
Greg Clayton0fffff52010-09-24 05:15:53 +00002734
2735 return objc_method_decl;
2736}
2737
Greg Clayton402230e2012-02-03 01:30:30 +00002738size_t
2739ClangASTContext::GetNumTemplateArguments (clang::ASTContext *ast, clang_type_t clang_type)
2740{
2741 if (clang_type)
2742 {
2743 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2744
2745 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2746 switch (type_class)
2747 {
2748 case clang::Type::Record:
2749 if (GetCompleteQualType (ast, qual_type))
2750 {
2751 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2752 if (cxx_record_decl)
2753 {
2754 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2755 if (template_decl)
2756 return template_decl->getTemplateArgs().size();
2757 }
2758 }
2759 break;
2760
2761 case clang::Type::Typedef:
2762 return ClangASTContext::GetNumTemplateArguments (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2763 default:
2764 break;
2765 }
2766 }
2767 return 0;
2768}
2769
2770clang_type_t
2771ClangASTContext::GetTemplateArgument (clang::ASTContext *ast, clang_type_t clang_type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
2772{
2773 if (clang_type)
2774 {
2775 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2776
2777 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2778 switch (type_class)
2779 {
2780 case clang::Type::Record:
2781 if (GetCompleteQualType (ast, qual_type))
2782 {
2783 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2784 if (cxx_record_decl)
2785 {
2786 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2787 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
2788 {
2789 const TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
2790 switch (template_arg.getKind())
2791 {
2792 case clang::TemplateArgument::Null:
2793 kind = eTemplateArgumentKindNull;
2794 return NULL;
2795
2796 case clang::TemplateArgument::Type:
2797 kind = eTemplateArgumentKindType;
2798 return template_arg.getAsType().getAsOpaquePtr();
2799
2800 case clang::TemplateArgument::Declaration:
2801 kind = eTemplateArgumentKindDeclaration;
2802 return NULL;
2803
2804 case clang::TemplateArgument::Integral:
2805 kind = eTemplateArgumentKindIntegral;
2806 return template_arg.getIntegralType().getAsOpaquePtr();
2807
2808 case clang::TemplateArgument::Template:
2809 kind = eTemplateArgumentKindTemplate;
2810 return NULL;
2811
2812 case clang::TemplateArgument::TemplateExpansion:
2813 kind = eTemplateArgumentKindTemplateExpansion;
2814 return NULL;
2815
2816 case clang::TemplateArgument::Expression:
2817 kind = eTemplateArgumentKindExpression;
2818 return NULL;
2819
2820 case clang::TemplateArgument::Pack:
2821 kind = eTemplateArgumentKindPack;
2822 return NULL;
2823
2824 default:
2825 assert (!"Unhandled TemplateArgument::ArgKind");
2826 kind = eTemplateArgumentKindNull;
2827 return NULL;
2828 }
2829 }
2830 }
2831 }
2832 break;
2833
2834 case clang::Type::Typedef:
2835 return ClangASTContext::GetTemplateArgument (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), arg_idx, kind);
2836 default:
2837 break;
2838 }
2839 }
2840 kind = eTemplateArgumentKindNull;
2841 return NULL;
2842}
Greg Clayton0fffff52010-09-24 05:15:53 +00002843
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002844uint32_t
Greg Clayton73b472d2010-10-27 03:32:59 +00002845ClangASTContext::GetTypeInfo
2846(
2847 clang_type_t clang_type,
Greg Clayton6beaaa62011-01-17 03:46:26 +00002848 clang::ASTContext *ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00002849 clang_type_t *pointee_or_element_clang_type
2850)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002851{
2852 if (clang_type == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00002853 return 0;
2854
2855 if (pointee_or_element_clang_type)
2856 *pointee_or_element_clang_type = NULL;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002857
2858 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2859
2860 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2861 switch (type_class)
2862 {
Sean Callanana2424172010-10-25 00:29:48 +00002863 case clang::Type::Builtin:
2864 switch (cast<clang::BuiltinType>(qual_type)->getKind())
2865 {
Sean Callanana2424172010-10-25 00:29:48 +00002866 case clang::BuiltinType::ObjCId:
2867 case clang::BuiltinType::ObjCClass:
Greg Clayton6beaaa62011-01-17 03:46:26 +00002868 if (ast && pointee_or_element_clang_type)
2869 *pointee_or_element_clang_type = ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Sean Callanana2424172010-10-25 00:29:48 +00002870 return eTypeIsBuiltIn | eTypeIsPointer | eTypeHasValue;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002871 break;
2872 case clang::BuiltinType::Bool:
2873 case clang::BuiltinType::Char_U:
2874 case clang::BuiltinType::UChar:
2875 case clang::BuiltinType::WChar_U:
2876 case clang::BuiltinType::Char16:
2877 case clang::BuiltinType::Char32:
2878 case clang::BuiltinType::UShort:
2879 case clang::BuiltinType::UInt:
2880 case clang::BuiltinType::ULong:
2881 case clang::BuiltinType::ULongLong:
2882 case clang::BuiltinType::UInt128:
2883 case clang::BuiltinType::Char_S:
2884 case clang::BuiltinType::SChar:
2885 case clang::BuiltinType::WChar_S:
2886 case clang::BuiltinType::Short:
2887 case clang::BuiltinType::Int:
2888 case clang::BuiltinType::Long:
2889 case clang::BuiltinType::LongLong:
2890 case clang::BuiltinType::Int128:
2891 case clang::BuiltinType::Float:
2892 case clang::BuiltinType::Double:
2893 case clang::BuiltinType::LongDouble:
2894 return eTypeIsBuiltIn | eTypeHasValue | eTypeIsScalar;
Greg Clayton73b472d2010-10-27 03:32:59 +00002895 default:
2896 break;
Sean Callanana2424172010-10-25 00:29:48 +00002897 }
2898 return eTypeIsBuiltIn | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002899
2900 case clang::Type::BlockPointer:
2901 if (pointee_or_element_clang_type)
2902 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2903 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
2904
Greg Clayton49462ea2011-01-15 02:52:14 +00002905 case clang::Type::Complex: return eTypeIsBuiltIn | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002906
2907 case clang::Type::ConstantArray:
2908 case clang::Type::DependentSizedArray:
2909 case clang::Type::IncompleteArray:
2910 case clang::Type::VariableArray:
2911 if (pointee_or_element_clang_type)
2912 *pointee_or_element_clang_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
2913 return eTypeHasChildren | eTypeIsArray;
2914
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002915 case clang::Type::DependentName: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002916 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
2917 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
2918 case clang::Type::Decltype: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00002919
2920 case clang::Type::Enum:
2921 if (pointee_or_element_clang_type)
2922 *pointee_or_element_clang_type = cast<EnumType>(qual_type)->getDecl()->getIntegerType().getAsOpaquePtr();
2923 return eTypeIsEnumeration | eTypeHasValue;
2924
Sean Callanan912855f2011-08-11 23:56:13 +00002925 case clang::Type::Elaborated:
2926 return ClangASTContext::GetTypeInfo (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2927 ast,
2928 pointee_or_element_clang_type);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002929 case clang::Type::ExtVector: return eTypeHasChildren | eTypeIsVector;
2930 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
2931 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002932 case clang::Type::InjectedClassName: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00002933
2934 case clang::Type::LValueReference:
2935 case clang::Type::RValueReference:
2936 if (pointee_or_element_clang_type)
2937 *pointee_or_element_clang_type = cast<ReferenceType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr();
2938 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
2939
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002940 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002941
2942 case clang::Type::ObjCObjectPointer:
2943 if (pointee_or_element_clang_type)
2944 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2945 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
2946
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002947 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
2948 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
Greg Clayton73b472d2010-10-27 03:32:59 +00002949
2950 case clang::Type::Pointer:
2951 if (pointee_or_element_clang_type)
2952 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2953 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
2954
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002955 case clang::Type::Record:
2956 if (qual_type->getAsCXXRecordDecl())
2957 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
2958 else
2959 return eTypeHasChildren | eTypeIsStructUnion;
2960 break;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002961 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
2962 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
2963 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
Greg Clayton73b472d2010-10-27 03:32:59 +00002964
2965 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00002966 return eTypeIsTypedef | ClangASTContext::GetTypeInfo (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00002967 ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00002968 pointee_or_element_clang_type);
2969
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002970 case clang::Type::TypeOfExpr: return 0;
2971 case clang::Type::TypeOf: return 0;
2972 case clang::Type::UnresolvedUsing: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002973 case clang::Type::Vector: return eTypeHasChildren | eTypeIsVector;
2974 default: return 0;
2975 }
2976 return 0;
2977}
2978
Greg Clayton9e409562010-07-28 02:04:09 +00002979
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002980#pragma mark Aggregate Types
2981
2982bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002983ClangASTContext::IsAggregateType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002984{
2985 if (clang_type == NULL)
2986 return false;
2987
2988 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2989
Greg Clayton737b9322010-09-13 03:32:57 +00002990 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2991 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002992 {
Greg Claytone1a916a2010-07-21 22:12:05 +00002993 case clang::Type::IncompleteArray:
2994 case clang::Type::VariableArray:
2995 case clang::Type::ConstantArray:
2996 case clang::Type::ExtVector:
2997 case clang::Type::Vector:
2998 case clang::Type::Record:
Greg Clayton9e409562010-07-28 02:04:09 +00002999 case clang::Type::ObjCObject:
3000 case clang::Type::ObjCInterface:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003001 return true;
Sean Callanan912855f2011-08-11 23:56:13 +00003002 case clang::Type::Elaborated:
3003 return ClangASTContext::IsAggregateType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Claytone1a916a2010-07-21 22:12:05 +00003004 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00003005 return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003006
3007 default:
3008 break;
3009 }
3010 // The clang type does have a value
3011 return false;
3012}
3013
3014uint32_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00003015ClangASTContext::GetNumChildren (clang::ASTContext *ast, clang_type_t clang_type, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003016{
Greg Clayton6beaaa62011-01-17 03:46:26 +00003017 if (clang_type == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003018 return 0;
3019
3020 uint32_t num_children = 0;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003021 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00003022 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3023 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003024 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003025 case clang::Type::Builtin:
3026 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3027 {
Greg Clayton73b472d2010-10-27 03:32:59 +00003028 case clang::BuiltinType::ObjCId: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003029 case clang::BuiltinType::ObjCClass: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003030 num_children = 1;
Greg Clayton73b472d2010-10-27 03:32:59 +00003031 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003032
3033 default:
3034 break;
3035 }
3036 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00003037
Greg Clayton49462ea2011-01-15 02:52:14 +00003038 case clang::Type::Complex: return 0;
Greg Clayton54979cd2010-12-15 05:08:08 +00003039
Greg Claytone1a916a2010-07-21 22:12:05 +00003040 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00003041 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003042 {
3043 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3044 const RecordDecl *record_decl = record_type->getDecl();
3045 assert(record_decl);
3046 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3047 if (cxx_record_decl)
3048 {
3049 if (omit_empty_base_classes)
3050 {
3051 // Check each base classes to see if it or any of its
3052 // base classes contain any fields. This can help
3053 // limit the noise in variable views by not having to
3054 // show base classes that contain no members.
3055 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3056 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3057 base_class != base_class_end;
3058 ++base_class)
3059 {
3060 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3061
3062 // Skip empty base classes
3063 if (RecordHasFields(base_class_decl) == false)
3064 continue;
3065
3066 num_children++;
3067 }
3068 }
3069 else
3070 {
3071 // Include all base classes
3072 num_children += cxx_record_decl->getNumBases();
3073 }
3074
3075 }
3076 RecordDecl::field_iterator field, field_end;
3077 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3078 ++num_children;
3079 }
3080 break;
3081
Greg Clayton9e409562010-07-28 02:04:09 +00003082 case clang::Type::ObjCObject:
3083 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00003084 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00003085 {
Sean Callanan78e37602011-01-27 04:42:51 +00003086 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003087 assert (objc_class_type);
3088 if (objc_class_type)
3089 {
3090 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3091
3092 if (class_interface_decl)
3093 {
3094
3095 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3096 if (superclass_interface_decl)
3097 {
3098 if (omit_empty_base_classes)
3099 {
3100 if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true))
3101 ++num_children;
3102 }
3103 else
3104 ++num_children;
3105 }
3106
3107 num_children += class_interface_decl->ivar_size();
3108 }
3109 }
3110 }
3111 break;
3112
3113 case clang::Type::ObjCObjectPointer:
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003114 {
Sean Callanan78e37602011-01-27 04:42:51 +00003115 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003116 QualType pointee_type = pointer_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003117 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3118 pointee_type.getAsOpaquePtr(),
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003119 omit_empty_base_classes);
3120 // If this type points to a simple type, then it has 1 child
3121 if (num_pointee_children == 0)
3122 num_children = 1;
3123 else
3124 num_children = num_pointee_children;
3125 }
3126 break;
Greg Clayton9e409562010-07-28 02:04:09 +00003127
Greg Claytone1a916a2010-07-21 22:12:05 +00003128 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003129 num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
3130 break;
3131
Greg Claytone1a916a2010-07-21 22:12:05 +00003132 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003133 {
Sean Callanan78e37602011-01-27 04:42:51 +00003134 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003135 QualType pointee_type (pointer_type->getPointeeType());
Greg Clayton6beaaa62011-01-17 03:46:26 +00003136 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3137 pointee_type.getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00003138 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003139 if (num_pointee_children == 0)
Greg Clayton54979cd2010-12-15 05:08:08 +00003140 {
3141 // We have a pointer to a pointee type that claims it has no children.
3142 // We will want to look at
3143 num_children = ClangASTContext::GetNumPointeeChildren (pointee_type.getAsOpaquePtr());
3144 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003145 else
3146 num_children = num_pointee_children;
3147 }
3148 break;
3149
Greg Clayton73b472d2010-10-27 03:32:59 +00003150 case clang::Type::LValueReference:
3151 case clang::Type::RValueReference:
3152 {
Sean Callanan78e37602011-01-27 04:42:51 +00003153 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton73b472d2010-10-27 03:32:59 +00003154 QualType pointee_type = reference_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003155 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3156 pointee_type.getAsOpaquePtr(),
Greg Clayton73b472d2010-10-27 03:32:59 +00003157 omit_empty_base_classes);
3158 // If this type points to a simple type, then it has 1 child
3159 if (num_pointee_children == 0)
3160 num_children = 1;
3161 else
3162 num_children = num_pointee_children;
3163 }
3164 break;
3165
3166
Greg Claytone1a916a2010-07-21 22:12:05 +00003167 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00003168 num_children = ClangASTContext::GetNumChildren (ast,
3169 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3170 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003171 break;
Sean Callanan912855f2011-08-11 23:56:13 +00003172
3173 case clang::Type::Elaborated:
3174 num_children = ClangASTContext::GetNumChildren (ast,
3175 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3176 omit_empty_base_classes);
3177 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003178
3179 default:
3180 break;
3181 }
3182 return num_children;
3183}
3184
Greg Claytonbf2331c2011-09-09 23:04:00 +00003185uint32_t
3186ClangASTContext::GetNumDirectBaseClasses (clang::ASTContext *ast, clang_type_t clang_type)
3187{
3188 if (clang_type == NULL)
3189 return 0;
3190
3191 uint32_t count = 0;
3192 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3193 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3194 switch (type_class)
3195 {
3196 case clang::Type::Record:
3197 if (GetCompleteQualType (ast, qual_type))
3198 {
3199 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3200 if (cxx_record_decl)
3201 count = cxx_record_decl->getNumBases();
3202 }
3203 break;
3204
3205 case clang::Type::ObjCObject:
3206 case clang::Type::ObjCInterface:
3207 if (GetCompleteQualType (ast, qual_type))
3208 {
3209 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3210 if (objc_class_type)
3211 {
3212 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3213
3214 if (class_interface_decl && class_interface_decl->getSuperClass())
3215 count = 1;
3216 }
3217 }
3218 break;
3219
3220
3221 case clang::Type::Typedef:
3222 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3223 break;
3224
3225 case clang::Type::Elaborated:
3226 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3227 break;
3228
3229 default:
3230 break;
3231 }
3232 return count;
3233}
3234
3235uint32_t
3236ClangASTContext::GetNumVirtualBaseClasses (clang::ASTContext *ast,
3237 clang_type_t clang_type)
3238{
3239 if (clang_type == NULL)
3240 return 0;
3241
3242 uint32_t count = 0;
3243 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3244 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3245 switch (type_class)
3246 {
3247 case clang::Type::Record:
3248 if (GetCompleteQualType (ast, qual_type))
3249 {
3250 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3251 if (cxx_record_decl)
3252 count = cxx_record_decl->getNumVBases();
3253 }
3254 break;
3255
3256 case clang::Type::Typedef:
3257 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3258 break;
3259
3260 case clang::Type::Elaborated:
3261 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3262 break;
3263
3264 default:
3265 break;
3266 }
3267 return count;
3268}
3269
3270uint32_t
3271ClangASTContext::GetNumFields (clang::ASTContext *ast, clang_type_t clang_type)
3272{
3273 if (clang_type == NULL)
3274 return 0;
3275
3276 uint32_t count = 0;
3277 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3278 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3279 switch (type_class)
3280 {
3281 case clang::Type::Record:
3282 if (GetCompleteQualType (ast, qual_type))
3283 {
3284 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
3285 if (record_type)
3286 {
3287 RecordDecl *record_decl = record_type->getDecl();
3288 if (record_decl)
3289 {
3290 uint32_t field_idx = 0;
3291 RecordDecl::field_iterator field, field_end;
3292 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3293 ++field_idx;
3294 count = field_idx;
3295 }
3296 }
3297 }
3298 break;
3299
3300 case clang::Type::Typedef:
3301 count = ClangASTContext::GetNumFields (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3302 break;
3303
3304 case clang::Type::Elaborated:
3305 count = ClangASTContext::GetNumFields (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3306 break;
3307
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003308 case clang::Type::ObjCObject:
3309 case clang::Type::ObjCInterface:
3310 if (GetCompleteQualType (ast, qual_type))
3311 {
3312 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3313 if (objc_class_type)
3314 {
3315 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3316
3317 if (class_interface_decl)
3318 count = class_interface_decl->ivar_size();
3319 }
3320 }
3321 break;
3322
Greg Claytonbf2331c2011-09-09 23:04:00 +00003323 default:
3324 break;
3325 }
3326 return count;
3327}
3328
3329clang_type_t
3330ClangASTContext::GetDirectBaseClassAtIndex (clang::ASTContext *ast,
3331 clang_type_t clang_type,
3332 uint32_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003333 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003334{
3335 if (clang_type == NULL)
3336 return 0;
3337
3338 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3339 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3340 switch (type_class)
3341 {
3342 case clang::Type::Record:
3343 if (GetCompleteQualType (ast, qual_type))
3344 {
3345 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3346 if (cxx_record_decl)
3347 {
3348 uint32_t curr_idx = 0;
3349 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3350 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3351 base_class != base_class_end;
3352 ++base_class, ++curr_idx)
3353 {
3354 if (curr_idx == idx)
3355 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003356 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003357 {
3358 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3359 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3360// if (base_class->isVirtual())
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003361// *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003362// else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003363 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003364 }
3365 return base_class->getType().getAsOpaquePtr();
3366 }
3367 }
3368 }
3369 }
3370 break;
3371
3372 case clang::Type::ObjCObject:
3373 case clang::Type::ObjCInterface:
3374 if (idx == 0 && GetCompleteQualType (ast, qual_type))
3375 {
3376 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3377 if (objc_class_type)
3378 {
3379 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3380
3381 if (class_interface_decl)
3382 {
3383 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3384 if (superclass_interface_decl)
3385 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003386 if (bit_offset_ptr)
3387 *bit_offset_ptr = 0;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003388 return ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr();
3389 }
3390 }
3391 }
3392 }
3393 break;
3394
3395
3396 case clang::Type::Typedef:
3397 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3398 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3399 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003400 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003401
3402 case clang::Type::Elaborated:
3403 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3404 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3405 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003406 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003407
3408 default:
3409 break;
3410 }
3411 return NULL;
3412}
3413
3414clang_type_t
3415ClangASTContext::GetVirtualBaseClassAtIndex (clang::ASTContext *ast,
3416 clang_type_t clang_type,
3417 uint32_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003418 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003419{
3420 if (clang_type == NULL)
3421 return 0;
3422
3423 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3424 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3425 switch (type_class)
3426 {
3427 case clang::Type::Record:
3428 if (GetCompleteQualType (ast, qual_type))
3429 {
3430 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3431 if (cxx_record_decl)
3432 {
3433 uint32_t curr_idx = 0;
3434 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3435 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
3436 base_class != base_class_end;
3437 ++base_class, ++curr_idx)
3438 {
3439 if (curr_idx == idx)
3440 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003441 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003442 {
3443 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3444 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003445 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003446
3447 }
3448 return base_class->getType().getAsOpaquePtr();
3449 }
3450 }
3451 }
3452 }
3453 break;
3454
3455 case clang::Type::Typedef:
3456 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3457 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3458 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003459 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003460
3461 case clang::Type::Elaborated:
3462 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3463 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3464 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003465 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003466
3467 default:
3468 break;
3469 }
3470 return NULL;
3471}
3472
3473clang_type_t
3474ClangASTContext::GetFieldAtIndex (clang::ASTContext *ast,
3475 clang_type_t clang_type,
3476 uint32_t idx,
3477 std::string& name,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003478 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003479{
3480 if (clang_type == NULL)
3481 return 0;
3482
3483 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3484 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3485 switch (type_class)
3486 {
3487 case clang::Type::Record:
3488 if (GetCompleteQualType (ast, qual_type))
3489 {
3490 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3491 const RecordDecl *record_decl = record_type->getDecl();
3492 uint32_t field_idx = 0;
3493 RecordDecl::field_iterator field, field_end;
3494 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
3495 {
3496 if (idx == field_idx)
3497 {
3498 // Print the member type if requested
3499 // Print the member name and equal sign
3500 name.assign(field->getNameAsString());
3501
3502 // Figure out the type byte size (field_type_info.first) and
3503 // alignment (field_type_info.second) from the AST context.
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003504 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003505 {
3506 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003507 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003508 }
3509
3510 return field->getType().getAsOpaquePtr();
3511 }
3512 }
3513 }
3514 break;
3515
3516 case clang::Type::ObjCObject:
3517 case clang::Type::ObjCInterface:
3518 if (GetCompleteQualType (ast, qual_type))
3519 {
3520 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3521 assert (objc_class_type);
3522 if (objc_class_type)
3523 {
3524 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3525
3526 if (class_interface_decl)
3527 {
3528 if (idx < (class_interface_decl->ivar_size()))
3529 {
3530 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
3531 uint32_t ivar_idx = 0;
3532
3533 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
3534 {
3535 if (ivar_idx == idx)
3536 {
3537 const ObjCIvarDecl* ivar_decl = *ivar_pos;
3538
3539 QualType ivar_qual_type(ivar_decl->getType());
3540
3541 name.assign(ivar_decl->getNameAsString());
3542
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003543 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003544 {
3545 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003546 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003547 }
3548
3549 return ivar_qual_type.getAsOpaquePtr();
3550 }
3551 }
3552 }
3553 }
3554 }
3555 }
3556 break;
3557
3558
3559 case clang::Type::Typedef:
3560 return ClangASTContext::GetFieldAtIndex (ast,
3561 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3562 idx,
3563 name,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003564 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003565
3566 case clang::Type::Elaborated:
3567 return ClangASTContext::GetFieldAtIndex (ast,
3568 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3569 idx,
3570 name,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003571 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003572
3573 default:
3574 break;
3575 }
3576 return NULL;
3577}
3578
3579
Greg Clayton54979cd2010-12-15 05:08:08 +00003580// If a pointer to a pointee type (the clang_type arg) says that it has no
3581// children, then we either need to trust it, or override it and return a
3582// different result. For example, an "int *" has one child that is an integer,
3583// but a function pointer doesn't have any children. Likewise if a Record type
3584// claims it has no children, then there really is nothing to show.
3585uint32_t
3586ClangASTContext::GetNumPointeeChildren (clang_type_t clang_type)
3587{
3588 if (clang_type == NULL)
3589 return 0;
3590
3591 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3592 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3593 switch (type_class)
3594 {
Greg Clayton97a43712011-01-08 22:26:47 +00003595 case clang::Type::Builtin:
3596 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3597 {
Greg Clayton7260f622011-04-18 08:33:37 +00003598 case clang::BuiltinType::UnknownAny:
Greg Clayton97a43712011-01-08 22:26:47 +00003599 case clang::BuiltinType::Void:
3600 case clang::BuiltinType::NullPtr:
3601 return 0;
3602 case clang::BuiltinType::Bool:
3603 case clang::BuiltinType::Char_U:
3604 case clang::BuiltinType::UChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003605 case clang::BuiltinType::WChar_U:
Greg Clayton97a43712011-01-08 22:26:47 +00003606 case clang::BuiltinType::Char16:
3607 case clang::BuiltinType::Char32:
3608 case clang::BuiltinType::UShort:
3609 case clang::BuiltinType::UInt:
3610 case clang::BuiltinType::ULong:
3611 case clang::BuiltinType::ULongLong:
3612 case clang::BuiltinType::UInt128:
3613 case clang::BuiltinType::Char_S:
3614 case clang::BuiltinType::SChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003615 case clang::BuiltinType::WChar_S:
Greg Clayton97a43712011-01-08 22:26:47 +00003616 case clang::BuiltinType::Short:
3617 case clang::BuiltinType::Int:
3618 case clang::BuiltinType::Long:
3619 case clang::BuiltinType::LongLong:
3620 case clang::BuiltinType::Int128:
3621 case clang::BuiltinType::Float:
3622 case clang::BuiltinType::Double:
3623 case clang::BuiltinType::LongDouble:
3624 case clang::BuiltinType::Dependent:
3625 case clang::BuiltinType::Overload:
Greg Clayton97a43712011-01-08 22:26:47 +00003626 case clang::BuiltinType::ObjCId:
3627 case clang::BuiltinType::ObjCClass:
3628 case clang::BuiltinType::ObjCSel:
Sean Callanand12cf8bb2011-05-15 22:34:38 +00003629 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00003630 case clang::BuiltinType::Half:
3631 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00003632 case clang::BuiltinType::PseudoObject:
Greg Clayton97a43712011-01-08 22:26:47 +00003633 return 1;
3634 }
3635 break;
3636
Greg Clayton49462ea2011-01-15 02:52:14 +00003637 case clang::Type::Complex: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003638 case clang::Type::Pointer: return 1;
3639 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
3640 case clang::Type::LValueReference: return 1;
3641 case clang::Type::RValueReference: return 1;
3642 case clang::Type::MemberPointer: return 0;
3643 case clang::Type::ConstantArray: return 0;
3644 case clang::Type::IncompleteArray: return 0;
3645 case clang::Type::VariableArray: return 0;
3646 case clang::Type::DependentSizedArray: return 0;
3647 case clang::Type::DependentSizedExtVector: return 0;
3648 case clang::Type::Vector: return 0;
3649 case clang::Type::ExtVector: return 0;
3650 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
3651 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
3652 case clang::Type::UnresolvedUsing: return 0;
3653 case clang::Type::Paren: return 0;
3654 case clang::Type::Typedef: return ClangASTContext::GetNumPointeeChildren (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00003655 case clang::Type::Elaborated: return ClangASTContext::GetNumPointeeChildren (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003656 case clang::Type::TypeOfExpr: return 0;
3657 case clang::Type::TypeOf: return 0;
3658 case clang::Type::Decltype: return 0;
3659 case clang::Type::Record: return 0;
3660 case clang::Type::Enum: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003661 case clang::Type::TemplateTypeParm: return 1;
3662 case clang::Type::SubstTemplateTypeParm: return 1;
3663 case clang::Type::TemplateSpecialization: return 1;
3664 case clang::Type::InjectedClassName: return 0;
3665 case clang::Type::DependentName: return 1;
3666 case clang::Type::DependentTemplateSpecialization: return 1;
3667 case clang::Type::ObjCObject: return 0;
3668 case clang::Type::ObjCInterface: return 0;
3669 case clang::Type::ObjCObjectPointer: return 1;
3670 default:
3671 break;
3672 }
3673 return 0;
3674}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003675
Greg Clayton1be10fc2010-09-29 01:12:09 +00003676clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003677ClangASTContext::GetChildClangTypeAtIndex
3678(
Jim Inghamd555bac2011-06-24 22:03:24 +00003679 ExecutionContext *exe_ctx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003680 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003681 clang_type_t parent_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003682 uint32_t idx,
3683 bool transparent_pointers,
3684 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003685 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003686 std::string& child_name,
3687 uint32_t &child_byte_size,
3688 int32_t &child_byte_offset,
3689 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003690 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003691 bool &child_is_base_class,
3692 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003693)
3694{
3695 if (parent_clang_type)
3696
Jim Inghamd555bac2011-06-24 22:03:24 +00003697 return GetChildClangTypeAtIndex (exe_ctx,
3698 getASTContext(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003699 parent_name,
3700 parent_clang_type,
3701 idx,
3702 transparent_pointers,
3703 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003704 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003705 child_name,
3706 child_byte_size,
3707 child_byte_offset,
3708 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003709 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003710 child_is_base_class,
3711 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003712 return NULL;
3713}
3714
Greg Clayton1be10fc2010-09-29 01:12:09 +00003715clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003716ClangASTContext::GetChildClangTypeAtIndex
3717(
Jim Inghamd555bac2011-06-24 22:03:24 +00003718 ExecutionContext *exe_ctx,
Greg Clayton6beaaa62011-01-17 03:46:26 +00003719 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003720 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003721 clang_type_t parent_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003722 uint32_t idx,
3723 bool transparent_pointers,
3724 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003725 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003726 std::string& child_name,
3727 uint32_t &child_byte_size,
3728 int32_t &child_byte_offset,
3729 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003730 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003731 bool &child_is_base_class,
3732 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003733)
3734{
3735 if (parent_clang_type == NULL)
3736 return NULL;
3737
Greg Clayton6beaaa62011-01-17 03:46:26 +00003738 if (idx < ClangASTContext::GetNumChildren (ast, parent_clang_type, omit_empty_base_classes))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003739 {
3740 uint32_t bit_offset;
3741 child_bitfield_bit_size = 0;
3742 child_bitfield_bit_offset = 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003743 child_is_base_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003744 QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00003745 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
3746 switch (parent_type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003747 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003748 case clang::Type::Builtin:
3749 switch (cast<clang::BuiltinType>(parent_qual_type)->getKind())
3750 {
3751 case clang::BuiltinType::ObjCId:
3752 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00003753 child_name = "isa";
Greg Clayton6beaaa62011-01-17 03:46:26 +00003754 child_byte_size = ast->getTypeSize(ast->ObjCBuiltinClassTy) / CHAR_BIT;
3755 return ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003756
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003757 default:
3758 break;
3759 }
3760 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003761
Greg Claytone1a916a2010-07-21 22:12:05 +00003762 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00003763 if (GetCompleteQualType (ast, parent_qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003764 {
3765 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr());
3766 const RecordDecl *record_decl = record_type->getDecl();
3767 assert(record_decl);
Greg Clayton6beaaa62011-01-17 03:46:26 +00003768 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003769 uint32_t child_idx = 0;
3770
3771 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3772 if (cxx_record_decl)
3773 {
3774 // We might have base classes to print out first
3775 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3776 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3777 base_class != base_class_end;
3778 ++base_class)
3779 {
3780 const CXXRecordDecl *base_class_decl = NULL;
3781
3782 // Skip empty base classes
3783 if (omit_empty_base_classes)
3784 {
3785 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3786 if (RecordHasFields(base_class_decl) == false)
3787 continue;
3788 }
3789
3790 if (idx == child_idx)
3791 {
3792 if (base_class_decl == NULL)
3793 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3794
3795
3796 if (base_class->isVirtual())
Greg Clayton6ed95942011-01-22 07:12:45 +00003797 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003798 else
Greg Clayton6ed95942011-01-22 07:12:45 +00003799 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003800
3801 // Base classes should be a multiple of 8 bits in size
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003802 child_byte_offset = bit_offset/8;
Greg Claytone3055942011-06-30 02:28:26 +00003803
Greg Clayton84db9102012-03-26 23:03:23 +00003804 child_name = ClangASTType::GetTypeNameForQualType(ast, base_class->getType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003805
Greg Clayton6beaaa62011-01-17 03:46:26 +00003806 uint64_t clang_type_info_bit_size = ast->getTypeSize(base_class->getType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003807
Jim Inghamf46b3382011-04-15 23:42:06 +00003808 // Base classes bit sizes should be a multiple of 8 bits in size
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003809 assert (clang_type_info_bit_size % 8 == 0);
3810 child_byte_size = clang_type_info_bit_size / 8;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003811 child_is_base_class = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003812 return base_class->getType().getAsOpaquePtr();
3813 }
3814 // We don't increment the child index in the for loop since we might
3815 // be skipping empty base classes
3816 ++child_idx;
3817 }
3818 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003819 // Make sure index is in range...
3820 uint32_t field_idx = 0;
3821 RecordDecl::field_iterator field, field_end;
3822 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
3823 {
3824 if (idx == child_idx)
3825 {
3826 // Print the member type if requested
3827 // Print the member name and equal sign
3828 child_name.assign(field->getNameAsString().c_str());
3829
3830 // Figure out the type byte size (field_type_info.first) and
3831 // alignment (field_type_info.second) from the AST context.
Greg Clayton6beaaa62011-01-17 03:46:26 +00003832 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(field->getType());
Greg Claytonc982c762010-07-09 20:39:50 +00003833 assert(field_idx < record_layout.getFieldCount());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003834
3835 child_byte_size = field_type_info.first / 8;
3836
3837 // Figure out the field offset within the current struct/union/class type
3838 bit_offset = record_layout.getFieldOffset (field_idx);
3839 child_byte_offset = bit_offset / 8;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003840 if (ClangASTContext::FieldIsBitfield (ast, *field, child_bitfield_bit_size))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003841 child_bitfield_bit_offset = bit_offset % 8;
3842
3843 return field->getType().getAsOpaquePtr();
3844 }
3845 }
3846 }
3847 break;
3848
Greg Clayton9e409562010-07-28 02:04:09 +00003849 case clang::Type::ObjCObject:
3850 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00003851 if (GetCompleteQualType (ast, parent_qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00003852 {
Sean Callanan78e37602011-01-27 04:42:51 +00003853 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003854 assert (objc_class_type);
3855 if (objc_class_type)
3856 {
3857 uint32_t child_idx = 0;
3858 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3859
3860 if (class_interface_decl)
3861 {
3862
Greg Clayton6beaaa62011-01-17 03:46:26 +00003863 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
Greg Clayton9e409562010-07-28 02:04:09 +00003864 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3865 if (superclass_interface_decl)
3866 {
3867 if (omit_empty_base_classes)
3868 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00003869 if (ClangASTContext::GetNumChildren(ast, ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0)
Greg Clayton9e409562010-07-28 02:04:09 +00003870 {
3871 if (idx == 0)
3872 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00003873 QualType ivar_qual_type(ast->getObjCInterfaceType(superclass_interface_decl));
Greg Clayton9e409562010-07-28 02:04:09 +00003874
3875
3876 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
3877
Greg Clayton6beaaa62011-01-17 03:46:26 +00003878 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003879
3880 child_byte_size = ivar_type_info.first / 8;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003881 child_byte_offset = 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003882 child_is_base_class = true;
Greg Clayton9e409562010-07-28 02:04:09 +00003883
3884 return ivar_qual_type.getAsOpaquePtr();
3885 }
3886
3887 ++child_idx;
3888 }
3889 }
3890 else
3891 ++child_idx;
3892 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003893
3894 const uint32_t superclass_idx = child_idx;
Greg Clayton9e409562010-07-28 02:04:09 +00003895
3896 if (idx < (child_idx + class_interface_decl->ivar_size()))
3897 {
3898 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
3899
3900 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
3901 {
3902 if (child_idx == idx)
3903 {
Jim Inghamf80bc3f2011-12-08 02:53:10 +00003904 ObjCIvarDecl* ivar_decl = *ivar_pos;
Greg Clayton9e409562010-07-28 02:04:09 +00003905
3906 QualType ivar_qual_type(ivar_decl->getType());
3907
3908 child_name.assign(ivar_decl->getNameAsString().c_str());
3909
Greg Clayton6beaaa62011-01-17 03:46:26 +00003910 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003911
3912 child_byte_size = ivar_type_info.first / 8;
3913
3914 // Figure out the field offset within the current struct/union/class type
Jim Inghamd555bac2011-06-24 22:03:24 +00003915 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
3916 // that doesn't account for the space taken up by unbacked properties, or from
3917 // the changing size of base classes that are newer than this class.
3918 // So if we have a process around that we can ask about this object, do so.
3919 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
Greg Claytonc14ee322011-09-22 04:58:26 +00003920 Process *process = NULL;
3921 if (exe_ctx)
3922 process = exe_ctx->GetProcessPtr();
3923 if (process)
Jim Inghamd555bac2011-06-24 22:03:24 +00003924 {
Greg Claytonc14ee322011-09-22 04:58:26 +00003925 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
Jim Inghamd555bac2011-06-24 22:03:24 +00003926 if (objc_runtime != NULL)
3927 {
Enrico Granata6f3533f2011-07-29 19:53:35 +00003928 ClangASTType parent_ast_type (ast, parent_qual_type.getAsOpaquePtr());
Jim Inghamd555bac2011-06-24 22:03:24 +00003929 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
3930 }
3931 }
3932
Jim Inghamf80bc3f2011-12-08 02:53:10 +00003933 // Setting this to UINT32_MAX to make sure we don't compute it twice...
3934 bit_offset = UINT32_MAX;
3935
Jim Inghamd555bac2011-06-24 22:03:24 +00003936 if (child_byte_offset == LLDB_INVALID_IVAR_OFFSET)
3937 {
3938 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
3939 child_byte_offset = bit_offset / 8;
3940 }
Jim Inghamf80bc3f2011-12-08 02:53:10 +00003941
3942 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
3943 // of a bitfield within its containing object. So regardless of where we get the byte
3944 // offset from, we still need to get the bit offset for bitfields from the layout.
3945
3946 if (ClangASTContext::FieldIsBitfield (ast, ivar_decl, child_bitfield_bit_size))
3947 {
3948 if (bit_offset == UINT32_MAX)
3949 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
3950
3951 child_bitfield_bit_offset = bit_offset % 8;
3952 }
Greg Clayton9e409562010-07-28 02:04:09 +00003953 return ivar_qual_type.getAsOpaquePtr();
3954 }
3955 ++child_idx;
3956 }
3957 }
3958 }
3959 }
3960 }
3961 break;
3962
3963 case clang::Type::ObjCObjectPointer:
3964 {
Sean Callanan78e37602011-01-27 04:42:51 +00003965 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003966 QualType pointee_type = pointer_type->getPointeeType();
3967
3968 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
3969 {
Greg Claytone221f822011-01-21 01:59:00 +00003970 child_is_deref_of_parent = false;
3971 bool tmp_child_is_deref_of_parent = false;
Jim Inghamd555bac2011-06-24 22:03:24 +00003972 return GetChildClangTypeAtIndex (exe_ctx,
3973 ast,
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003974 parent_name,
3975 pointer_type->getPointeeType().getAsOpaquePtr(),
3976 idx,
3977 transparent_pointers,
3978 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003979 ignore_array_bounds,
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003980 child_name,
3981 child_byte_size,
3982 child_byte_offset,
3983 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003984 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003985 child_is_base_class,
3986 tmp_child_is_deref_of_parent);
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003987 }
3988 else
3989 {
Greg Claytone221f822011-01-21 01:59:00 +00003990 child_is_deref_of_parent = true;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003991 if (parent_name)
3992 {
3993 child_name.assign(1, '*');
3994 child_name += parent_name;
3995 }
3996
3997 // We have a pointer to an simple type
Sean Callanan5b26f272012-02-04 08:49:35 +00003998 if (idx == 0 && GetCompleteQualType(ast, pointee_type))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003999 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004000 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004001 assert(clang_type_info.first % 8 == 0);
4002 child_byte_size = clang_type_info.first / 8;
4003 child_byte_offset = 0;
4004 return pointee_type.getAsOpaquePtr();
4005 }
4006 }
Greg Clayton9e409562010-07-28 02:04:09 +00004007 }
4008 break;
4009
Greg Claytone1a916a2010-07-21 22:12:05 +00004010 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004011 {
4012 const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4013 const uint64_t element_count = array->getSize().getLimitedValue();
4014
Greg Claytondaf515f2011-07-09 20:12:33 +00004015 if (ignore_array_bounds || idx < element_count)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004016 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004017 if (GetCompleteQualType (ast, array->getElementType()))
4018 {
4019 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004020
Greg Clayton6beaaa62011-01-17 03:46:26 +00004021 char element_name[64];
4022 ::snprintf (element_name, sizeof (element_name), "[%u]", idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004023
Greg Clayton6beaaa62011-01-17 03:46:26 +00004024 child_name.assign(element_name);
4025 assert(field_type_info.first % 8 == 0);
4026 child_byte_size = field_type_info.first / 8;
Greg Claytondaf515f2011-07-09 20:12:33 +00004027 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
Greg Clayton6beaaa62011-01-17 03:46:26 +00004028 return array->getElementType().getAsOpaquePtr();
4029 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004030 }
4031 }
4032 break;
4033
Greg Claytone1a916a2010-07-21 22:12:05 +00004034 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004035 {
Sean Callanan78e37602011-01-27 04:42:51 +00004036 const PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004037 QualType pointee_type = pointer_type->getPointeeType();
Greg Clayton97a43712011-01-08 22:26:47 +00004038
4039 // Don't dereference "void *" pointers
4040 if (pointee_type->isVoidType())
4041 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004042
4043 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4044 {
Greg Claytone221f822011-01-21 01:59:00 +00004045 child_is_deref_of_parent = false;
4046 bool tmp_child_is_deref_of_parent = false;
Jim Inghamd555bac2011-06-24 22:03:24 +00004047 return GetChildClangTypeAtIndex (exe_ctx,
4048 ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004049 parent_name,
4050 pointer_type->getPointeeType().getAsOpaquePtr(),
4051 idx,
4052 transparent_pointers,
4053 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004054 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004055 child_name,
4056 child_byte_size,
4057 child_byte_offset,
4058 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00004059 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004060 child_is_base_class,
4061 tmp_child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004062 }
4063 else
4064 {
Greg Claytone221f822011-01-21 01:59:00 +00004065 child_is_deref_of_parent = true;
4066
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004067 if (parent_name)
4068 {
4069 child_name.assign(1, '*');
4070 child_name += parent_name;
4071 }
4072
4073 // We have a pointer to an simple type
4074 if (idx == 0)
4075 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004076 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004077 assert(clang_type_info.first % 8 == 0);
4078 child_byte_size = clang_type_info.first / 8;
4079 child_byte_offset = 0;
4080 return pointee_type.getAsOpaquePtr();
4081 }
4082 }
4083 }
4084 break;
4085
Greg Clayton73b472d2010-10-27 03:32:59 +00004086 case clang::Type::LValueReference:
4087 case clang::Type::RValueReference:
4088 {
Sean Callanan78e37602011-01-27 04:42:51 +00004089 const ReferenceType *reference_type = cast<ReferenceType>(parent_qual_type.getTypePtr());
Greg Clayton73b472d2010-10-27 03:32:59 +00004090 QualType pointee_type(reference_type->getPointeeType());
4091 clang_type_t pointee_clang_type = pointee_type.getAsOpaquePtr();
4092 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_clang_type))
4093 {
Greg Claytone221f822011-01-21 01:59:00 +00004094 child_is_deref_of_parent = false;
4095 bool tmp_child_is_deref_of_parent = false;
Jim Inghamd555bac2011-06-24 22:03:24 +00004096 return GetChildClangTypeAtIndex (exe_ctx,
4097 ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00004098 parent_name,
4099 pointee_clang_type,
4100 idx,
4101 transparent_pointers,
4102 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004103 ignore_array_bounds,
Greg Clayton73b472d2010-10-27 03:32:59 +00004104 child_name,
4105 child_byte_size,
4106 child_byte_offset,
4107 child_bitfield_bit_size,
4108 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004109 child_is_base_class,
4110 tmp_child_is_deref_of_parent);
Greg Clayton73b472d2010-10-27 03:32:59 +00004111 }
4112 else
4113 {
4114 if (parent_name)
4115 {
4116 child_name.assign(1, '&');
4117 child_name += parent_name;
4118 }
4119
4120 // We have a pointer to an simple type
4121 if (idx == 0)
4122 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004123 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Greg Clayton73b472d2010-10-27 03:32:59 +00004124 assert(clang_type_info.first % 8 == 0);
4125 child_byte_size = clang_type_info.first / 8;
4126 child_byte_offset = 0;
4127 return pointee_type.getAsOpaquePtr();
4128 }
4129 }
4130 }
4131 break;
4132
Greg Claytone1a916a2010-07-21 22:12:05 +00004133 case clang::Type::Typedef:
Jim Inghamd555bac2011-06-24 22:03:24 +00004134 return GetChildClangTypeAtIndex (exe_ctx,
4135 ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004136 parent_name,
Sean Callanan48114472010-12-13 01:26:27 +00004137 cast<TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004138 idx,
4139 transparent_pointers,
4140 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004141 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004142 child_name,
4143 child_byte_size,
4144 child_byte_offset,
4145 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00004146 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004147 child_is_base_class,
4148 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004149 break;
Sean Callanan912855f2011-08-11 23:56:13 +00004150
4151 case clang::Type::Elaborated:
4152 return GetChildClangTypeAtIndex (exe_ctx,
4153 ast,
4154 parent_name,
4155 cast<ElaboratedType>(parent_qual_type)->getNamedType().getAsOpaquePtr(),
4156 idx,
4157 transparent_pointers,
4158 omit_empty_base_classes,
4159 ignore_array_bounds,
4160 child_name,
4161 child_byte_size,
4162 child_byte_offset,
4163 child_bitfield_bit_size,
4164 child_bitfield_bit_offset,
4165 child_is_base_class,
4166 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004167
4168 default:
4169 break;
4170 }
4171 }
Greg Clayton19503a22010-07-23 15:37:46 +00004172 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004173}
4174
4175static inline bool
4176BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
4177{
Greg Clayton6beaaa62011-01-17 03:46:26 +00004178 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004179}
4180
4181static uint32_t
4182GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
4183{
4184 uint32_t num_bases = 0;
4185 if (cxx_record_decl)
4186 {
4187 if (omit_empty_base_classes)
4188 {
4189 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4190 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4191 base_class != base_class_end;
4192 ++base_class)
4193 {
4194 // Skip empty base classes
4195 if (omit_empty_base_classes)
4196 {
4197 if (BaseSpecifierIsEmpty (base_class))
4198 continue;
4199 }
4200 ++num_bases;
4201 }
4202 }
4203 else
4204 num_bases = cxx_record_decl->getNumBases();
4205 }
4206 return num_bases;
4207}
4208
4209
4210static uint32_t
4211GetIndexForRecordBase
4212(
4213 const RecordDecl *record_decl,
4214 const CXXBaseSpecifier *base_spec,
4215 bool omit_empty_base_classes
4216)
4217{
4218 uint32_t child_idx = 0;
4219
4220 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4221
4222// const char *super_name = record_decl->getNameAsCString();
4223// const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString();
4224// printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
4225//
4226 if (cxx_record_decl)
4227 {
4228 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4229 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4230 base_class != base_class_end;
4231 ++base_class)
4232 {
4233 if (omit_empty_base_classes)
4234 {
4235 if (BaseSpecifierIsEmpty (base_class))
4236 continue;
4237 }
4238
4239// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
4240// child_idx,
4241// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4242//
4243//
4244 if (base_class == base_spec)
4245 return child_idx;
4246 ++child_idx;
4247 }
4248 }
4249
4250 return UINT32_MAX;
4251}
4252
4253
4254static uint32_t
4255GetIndexForRecordChild
4256(
4257 const RecordDecl *record_decl,
4258 NamedDecl *canonical_decl,
4259 bool omit_empty_base_classes
4260)
4261{
4262 uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes);
4263
4264// const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4265//
4266//// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString());
4267// if (cxx_record_decl)
4268// {
4269// CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4270// for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4271// base_class != base_class_end;
4272// ++base_class)
4273// {
4274// if (omit_empty_base_classes)
4275// {
4276// if (BaseSpecifierIsEmpty (base_class))
4277// continue;
4278// }
4279//
4280//// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
4281//// record_decl->getNameAsCString(),
4282//// canonical_decl->getNameAsCString(),
4283//// child_idx,
4284//// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4285//
4286//
4287// CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4288// if (curr_base_class_decl == canonical_decl)
4289// {
4290// return child_idx;
4291// }
4292// ++child_idx;
4293// }
4294// }
4295//
4296// const uint32_t num_bases = child_idx;
4297 RecordDecl::field_iterator field, field_end;
4298 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4299 field != field_end;
4300 ++field, ++child_idx)
4301 {
4302// printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n",
4303// record_decl->getNameAsCString(),
4304// canonical_decl->getNameAsCString(),
4305// child_idx - num_bases,
4306// field->getNameAsCString());
4307
4308 if (field->getCanonicalDecl() == canonical_decl)
4309 return child_idx;
4310 }
4311
4312 return UINT32_MAX;
4313}
4314
4315// Look for a child member (doesn't include base classes, but it does include
4316// their members) in the type hierarchy. Returns an index path into "clang_type"
4317// on how to reach the appropriate member.
4318//
4319// class A
4320// {
4321// public:
4322// int m_a;
4323// int m_b;
4324// };
4325//
4326// class B
4327// {
4328// };
4329//
4330// class C :
4331// public B,
4332// public A
4333// {
4334// };
4335//
4336// If we have a clang type that describes "class C", and we wanted to looked
4337// "m_b" in it:
4338//
4339// With omit_empty_base_classes == false we would get an integer array back with:
4340// { 1, 1 }
4341// The first index 1 is the child index for "class A" within class C
4342// The second index 1 is the child index for "m_b" within class A
4343//
4344// With omit_empty_base_classes == true we would get an integer array back with:
4345// { 0, 1 }
4346// 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)
4347// The second index 1 is the child index for "m_b" within class A
4348
4349size_t
4350ClangASTContext::GetIndexOfChildMemberWithName
4351(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004352 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004353 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004354 const char *name,
4355 bool omit_empty_base_classes,
4356 std::vector<uint32_t>& child_indexes
4357)
4358{
4359 if (clang_type && name && name[0])
4360 {
4361 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00004362 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4363 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004364 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004365 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004366 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004367 {
4368 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4369 const RecordDecl *record_decl = record_type->getDecl();
4370
4371 assert(record_decl);
4372 uint32_t child_idx = 0;
4373
4374 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4375
4376 // Try and find a field that matches NAME
4377 RecordDecl::field_iterator field, field_end;
4378 StringRef name_sref(name);
4379 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4380 field != field_end;
4381 ++field, ++child_idx)
4382 {
4383 if (field->getName().equals (name_sref))
4384 {
4385 // We have to add on the number of base classes to this index!
4386 child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
4387 return child_indexes.size();
4388 }
4389 }
4390
4391 if (cxx_record_decl)
4392 {
4393 const RecordDecl *parent_record_decl = cxx_record_decl;
4394
4395 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
4396
4397 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
4398 // Didn't find things easily, lets let clang do its thang...
Sean Callanancc427fa2011-07-30 02:42:06 +00004399 IdentifierInfo & ident_ref = ast->Idents.get(name_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004400 DeclarationName decl_name(&ident_ref);
4401
4402 CXXBasePaths paths;
4403 if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember,
4404 decl_name.getAsOpaquePtr(),
4405 paths))
4406 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004407 CXXBasePaths::const_paths_iterator path, path_end = paths.end();
4408 for (path = paths.begin(); path != path_end; ++path)
4409 {
4410 const size_t num_path_elements = path->size();
4411 for (size_t e=0; e<num_path_elements; ++e)
4412 {
4413 CXXBasePathElement elem = (*path)[e];
4414
4415 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
4416 if (child_idx == UINT32_MAX)
4417 {
4418 child_indexes.clear();
4419 return 0;
4420 }
4421 else
4422 {
4423 child_indexes.push_back (child_idx);
4424 parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl());
4425 }
4426 }
4427 DeclContext::lookup_iterator named_decl_pos;
4428 for (named_decl_pos = path->Decls.first;
4429 named_decl_pos != path->Decls.second && parent_record_decl;
4430 ++named_decl_pos)
4431 {
4432 //printf ("path[%zu] = %s\n", child_indexes.size(), (*named_decl_pos)->getNameAsCString());
4433
4434 child_idx = GetIndexForRecordChild (parent_record_decl, *named_decl_pos, omit_empty_base_classes);
4435 if (child_idx == UINT32_MAX)
4436 {
4437 child_indexes.clear();
4438 return 0;
4439 }
4440 else
4441 {
4442 child_indexes.push_back (child_idx);
4443 }
4444 }
4445 }
4446 return child_indexes.size();
4447 }
4448 }
4449
4450 }
4451 break;
4452
Greg Clayton9e409562010-07-28 02:04:09 +00004453 case clang::Type::ObjCObject:
4454 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004455 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004456 {
4457 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004458 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004459 assert (objc_class_type);
4460 if (objc_class_type)
4461 {
4462 uint32_t child_idx = 0;
4463 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4464
4465 if (class_interface_decl)
4466 {
4467 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4468 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4469
Greg Clayton6ba78152010-09-18 02:11:07 +00004470 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00004471 {
4472 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4473
4474 if (ivar_decl->getName().equals (name_sref))
4475 {
4476 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4477 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4478 ++child_idx;
4479
4480 child_indexes.push_back (child_idx);
4481 return child_indexes.size();
4482 }
4483 }
4484
4485 if (superclass_interface_decl)
4486 {
4487 // The super class index is always zero for ObjC classes,
4488 // so we push it onto the child indexes in case we find
4489 // an ivar in our superclass...
4490 child_indexes.push_back (0);
4491
Greg Clayton6beaaa62011-01-17 03:46:26 +00004492 if (GetIndexOfChildMemberWithName (ast,
4493 ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00004494 name,
4495 omit_empty_base_classes,
4496 child_indexes))
4497 {
4498 // We did find an ivar in a superclass so just
4499 // return the results!
4500 return child_indexes.size();
4501 }
4502
4503 // We didn't find an ivar matching "name" in our
4504 // superclass, pop the superclass zero index that
4505 // we pushed on above.
4506 child_indexes.pop_back();
4507 }
4508 }
4509 }
4510 }
4511 break;
4512
4513 case clang::Type::ObjCObjectPointer:
4514 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004515 return GetIndexOfChildMemberWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004516 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4517 name,
4518 omit_empty_base_classes,
4519 child_indexes);
4520 }
4521 break;
4522
4523
Greg Claytone1a916a2010-07-21 22:12:05 +00004524 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004525 {
4526// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4527// const uint64_t element_count = array->getSize().getLimitedValue();
4528//
4529// if (idx < element_count)
4530// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004531// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004532//
4533// char element_name[32];
4534// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4535//
4536// child_name.assign(element_name);
4537// assert(field_type_info.first % 8 == 0);
4538// child_byte_size = field_type_info.first / 8;
4539// child_byte_offset = idx * child_byte_size;
4540// return array->getElementType().getAsOpaquePtr();
4541// }
4542 }
4543 break;
4544
Greg Claytone1a916a2010-07-21 22:12:05 +00004545// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004546// {
4547// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4548// QualType pointee_type = mem_ptr_type->getPointeeType();
4549//
4550// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4551// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004552// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004553// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4554// name);
4555// }
4556// }
4557// break;
4558//
Greg Claytone1a916a2010-07-21 22:12:05 +00004559 case clang::Type::LValueReference:
4560 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004561 {
Sean Callanan78e37602011-01-27 04:42:51 +00004562 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004563 QualType pointee_type = reference_type->getPointeeType();
4564
4565 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4566 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004567 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004568 reference_type->getPointeeType().getAsOpaquePtr(),
4569 name,
4570 omit_empty_base_classes,
4571 child_indexes);
4572 }
4573 }
4574 break;
4575
Greg Claytone1a916a2010-07-21 22:12:05 +00004576 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004577 {
Sean Callanan78e37602011-01-27 04:42:51 +00004578 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004579 QualType pointee_type = pointer_type->getPointeeType();
4580
4581 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4582 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004583 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004584 pointer_type->getPointeeType().getAsOpaquePtr(),
4585 name,
4586 omit_empty_base_classes,
4587 child_indexes);
4588 }
4589 else
4590 {
4591// if (parent_name)
4592// {
4593// child_name.assign(1, '*');
4594// child_name += parent_name;
4595// }
4596//
4597// // We have a pointer to an simple type
4598// if (idx == 0)
4599// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004600// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004601// assert(clang_type_info.first % 8 == 0);
4602// child_byte_size = clang_type_info.first / 8;
4603// child_byte_offset = 0;
4604// return pointee_type.getAsOpaquePtr();
4605// }
4606 }
4607 }
4608 break;
4609
Greg Claytone1a916a2010-07-21 22:12:05 +00004610 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00004611 return GetIndexOfChildMemberWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00004612 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004613 name,
4614 omit_empty_base_classes,
4615 child_indexes);
4616
4617 default:
4618 break;
4619 }
4620 }
4621 return 0;
4622}
4623
4624
4625// Get the index of the child of "clang_type" whose name matches. This function
4626// doesn't descend into the children, but only looks one level deep and name
4627// matches can include base class names.
4628
4629uint32_t
4630ClangASTContext::GetIndexOfChildWithName
4631(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004632 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004633 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004634 const char *name,
4635 bool omit_empty_base_classes
4636)
4637{
4638 if (clang_type && name && name[0])
4639 {
4640 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00004641
Greg Clayton737b9322010-09-13 03:32:57 +00004642 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Clayton9e409562010-07-28 02:04:09 +00004643
Greg Clayton737b9322010-09-13 03:32:57 +00004644 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004645 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004646 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004647 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004648 {
4649 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4650 const RecordDecl *record_decl = record_type->getDecl();
4651
4652 assert(record_decl);
4653 uint32_t child_idx = 0;
4654
4655 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4656
4657 if (cxx_record_decl)
4658 {
4659 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4660 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4661 base_class != base_class_end;
4662 ++base_class)
4663 {
4664 // Skip empty base classes
4665 CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4666 if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false)
4667 continue;
4668
Greg Clayton84db9102012-03-26 23:03:23 +00004669 std::string base_class_type_name (ClangASTType::GetTypeNameForQualType(ast, base_class->getType()));
Greg Claytone3055942011-06-30 02:28:26 +00004670 if (base_class_type_name.compare (name) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004671 return child_idx;
4672 ++child_idx;
4673 }
4674 }
4675
4676 // Try and find a field that matches NAME
4677 RecordDecl::field_iterator field, field_end;
4678 StringRef name_sref(name);
4679 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4680 field != field_end;
4681 ++field, ++child_idx)
4682 {
4683 if (field->getName().equals (name_sref))
4684 return child_idx;
4685 }
4686
4687 }
4688 break;
4689
Greg Clayton9e409562010-07-28 02:04:09 +00004690 case clang::Type::ObjCObject:
4691 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004692 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004693 {
4694 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004695 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004696 assert (objc_class_type);
4697 if (objc_class_type)
4698 {
4699 uint32_t child_idx = 0;
4700 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4701
4702 if (class_interface_decl)
4703 {
4704 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4705 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4706
4707 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
4708 {
4709 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4710
4711 if (ivar_decl->getName().equals (name_sref))
4712 {
4713 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4714 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4715 ++child_idx;
4716
4717 return child_idx;
4718 }
4719 }
4720
4721 if (superclass_interface_decl)
4722 {
4723 if (superclass_interface_decl->getName().equals (name_sref))
4724 return 0;
4725 }
4726 }
4727 }
4728 }
4729 break;
4730
4731 case clang::Type::ObjCObjectPointer:
4732 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004733 return GetIndexOfChildWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004734 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4735 name,
4736 omit_empty_base_classes);
4737 }
4738 break;
4739
Greg Claytone1a916a2010-07-21 22:12:05 +00004740 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004741 {
4742// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4743// const uint64_t element_count = array->getSize().getLimitedValue();
4744//
4745// if (idx < element_count)
4746// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004747// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004748//
4749// char element_name[32];
4750// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4751//
4752// child_name.assign(element_name);
4753// assert(field_type_info.first % 8 == 0);
4754// child_byte_size = field_type_info.first / 8;
4755// child_byte_offset = idx * child_byte_size;
4756// return array->getElementType().getAsOpaquePtr();
4757// }
4758 }
4759 break;
4760
Greg Claytone1a916a2010-07-21 22:12:05 +00004761// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004762// {
4763// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4764// QualType pointee_type = mem_ptr_type->getPointeeType();
4765//
4766// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4767// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004768// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004769// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4770// name);
4771// }
4772// }
4773// break;
4774//
Greg Claytone1a916a2010-07-21 22:12:05 +00004775 case clang::Type::LValueReference:
4776 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004777 {
Sean Callanan78e37602011-01-27 04:42:51 +00004778 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004779 QualType pointee_type = reference_type->getPointeeType();
4780
4781 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4782 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004783 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004784 reference_type->getPointeeType().getAsOpaquePtr(),
4785 name,
4786 omit_empty_base_classes);
4787 }
4788 }
4789 break;
4790
Greg Claytone1a916a2010-07-21 22:12:05 +00004791 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004792 {
Sean Callanan78e37602011-01-27 04:42:51 +00004793 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004794 QualType pointee_type = pointer_type->getPointeeType();
4795
4796 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4797 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004798 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004799 pointer_type->getPointeeType().getAsOpaquePtr(),
4800 name,
4801 omit_empty_base_classes);
4802 }
4803 else
4804 {
4805// if (parent_name)
4806// {
4807// child_name.assign(1, '*');
4808// child_name += parent_name;
4809// }
4810//
4811// // We have a pointer to an simple type
4812// if (idx == 0)
4813// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004814// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004815// assert(clang_type_info.first % 8 == 0);
4816// child_byte_size = clang_type_info.first / 8;
4817// child_byte_offset = 0;
4818// return pointee_type.getAsOpaquePtr();
4819// }
4820 }
4821 }
4822 break;
4823
Greg Claytone1a916a2010-07-21 22:12:05 +00004824 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00004825 return GetIndexOfChildWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00004826 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004827 name,
4828 omit_empty_base_classes);
4829
4830 default:
4831 break;
4832 }
4833 }
4834 return UINT32_MAX;
4835}
4836
4837#pragma mark TagType
4838
4839bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00004840ClangASTContext::SetTagTypeKind (clang_type_t tag_clang_type, int kind)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004841{
4842 if (tag_clang_type)
4843 {
4844 QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00004845 const clang::Type *clang_type = tag_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004846 if (clang_type)
4847 {
Sean Callanan78e37602011-01-27 04:42:51 +00004848 const TagType *tag_type = dyn_cast<TagType>(clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004849 if (tag_type)
4850 {
4851 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl());
4852 if (tag_decl)
4853 {
4854 tag_decl->setTagKind ((TagDecl::TagKind)kind);
4855 return true;
4856 }
4857 }
4858 }
4859 }
4860 return false;
4861}
4862
4863
4864#pragma mark DeclContext Functions
4865
4866DeclContext *
Greg Clayton1be10fc2010-09-29 01:12:09 +00004867ClangASTContext::GetDeclContextForType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004868{
4869 if (clang_type == NULL)
4870 return NULL;
4871
4872 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00004873 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4874 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004875 {
Sean Callanancc427fa2011-07-30 02:42:06 +00004876 case clang::Type::UnaryTransform: break;
Greg Clayton9e409562010-07-28 02:04:09 +00004877 case clang::Type::FunctionNoProto: break;
4878 case clang::Type::FunctionProto: break;
4879 case clang::Type::IncompleteArray: break;
4880 case clang::Type::VariableArray: break;
4881 case clang::Type::ConstantArray: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00004882 case clang::Type::DependentSizedArray: break;
Greg Clayton9e409562010-07-28 02:04:09 +00004883 case clang::Type::ExtVector: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00004884 case clang::Type::DependentSizedExtVector: break;
Greg Clayton9e409562010-07-28 02:04:09 +00004885 case clang::Type::Vector: break;
4886 case clang::Type::Builtin: break;
4887 case clang::Type::BlockPointer: break;
4888 case clang::Type::Pointer: break;
4889 case clang::Type::LValueReference: break;
4890 case clang::Type::RValueReference: break;
4891 case clang::Type::MemberPointer: break;
4892 case clang::Type::Complex: break;
4893 case clang::Type::ObjCObject: break;
4894 case clang::Type::ObjCInterface: return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface();
4895 case clang::Type::ObjCObjectPointer: return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr());
4896 case clang::Type::Record: return cast<RecordType>(qual_type)->getDecl();
4897 case clang::Type::Enum: return cast<EnumType>(qual_type)->getDecl();
Sean Callanan48114472010-12-13 01:26:27 +00004898 case clang::Type::Typedef: return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00004899 case clang::Type::Elaborated: return ClangASTContext::GetDeclContextForType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004900 case clang::Type::TypeOfExpr: break;
4901 case clang::Type::TypeOf: break;
4902 case clang::Type::Decltype: break;
4903 //case clang::Type::QualifiedName: break;
4904 case clang::Type::TemplateSpecialization: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00004905 case clang::Type::DependentTemplateSpecialization: break;
4906 case clang::Type::TemplateTypeParm: break;
4907 case clang::Type::SubstTemplateTypeParm: break;
4908 case clang::Type::SubstTemplateTypeParmPack:break;
4909 case clang::Type::PackExpansion: break;
4910 case clang::Type::UnresolvedUsing: break;
4911 case clang::Type::Paren: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00004912 case clang::Type::Attributed: break;
4913 case clang::Type::Auto: break;
4914 case clang::Type::InjectedClassName: break;
4915 case clang::Type::DependentName: break;
Greg Claytonea3e7d52011-10-08 00:49:15 +00004916 case clang::Type::Atomic: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004917 }
4918 // No DeclContext in this type...
4919 return NULL;
4920}
4921
4922#pragma mark Namespace Declarations
4923
4924NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00004925ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004926{
Greg Clayton030a2042011-10-14 21:34:45 +00004927 NamespaceDecl *namespace_decl = NULL;
Greg Clayton9d3d6882011-10-31 23:51:19 +00004928 ASTContext *ast = getASTContext();
4929 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
4930 if (decl_ctx == NULL)
4931 decl_ctx = translation_unit_decl;
4932
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004933 if (name)
4934 {
Greg Clayton030a2042011-10-14 21:34:45 +00004935 IdentifierInfo &identifier_info = ast->Idents.get(name);
4936 DeclarationName decl_name (&identifier_info);
4937 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
4938 for (clang::DeclContext::lookup_iterator pos = result.first, end = result.second; pos != end; ++pos)
4939 {
4940 namespace_decl = dyn_cast<clang::NamespaceDecl>(*pos);
4941 if (namespace_decl)
4942 return namespace_decl;
4943 }
4944
Sean Callanan5b26f272012-02-04 08:49:35 +00004945 namespace_decl = NamespaceDecl::Create(*ast,
4946 decl_ctx,
4947 false,
4948 SourceLocation(),
4949 SourceLocation(),
4950 &identifier_info,
4951 NULL);
Greg Clayton030a2042011-10-14 21:34:45 +00004952
Greg Clayton9d3d6882011-10-31 23:51:19 +00004953 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004954 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004955 else
4956 {
4957 if (decl_ctx == translation_unit_decl)
4958 {
4959 namespace_decl = translation_unit_decl->getAnonymousNamespace();
4960 if (namespace_decl)
4961 return namespace_decl;
4962
Sean Callanan5b26f272012-02-04 08:49:35 +00004963 namespace_decl = NamespaceDecl::Create(*ast,
4964 decl_ctx,
4965 false,
4966 SourceLocation(),
4967 SourceLocation(),
4968 NULL,
4969 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00004970 translation_unit_decl->setAnonymousNamespace (namespace_decl);
4971 translation_unit_decl->addDecl (namespace_decl);
4972 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
4973 }
4974 else
4975 {
4976 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
4977 if (parent_namespace_decl)
4978 {
4979 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
4980 if (namespace_decl)
4981 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00004982 namespace_decl = NamespaceDecl::Create(*ast,
4983 decl_ctx,
4984 false,
4985 SourceLocation(),
4986 SourceLocation(),
4987 NULL,
4988 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00004989 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
4990 parent_namespace_decl->addDecl (namespace_decl);
4991 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
4992 }
4993 else
4994 {
4995 // BAD!!!
4996 }
4997 }
4998
4999
5000 if (namespace_decl)
5001 {
5002 // If we make it here, we are creating the anonymous namespace decl
5003 // for the first time, so we need to do the using directive magic
5004 // like SEMA does
5005 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast,
5006 decl_ctx,
5007 SourceLocation(),
5008 SourceLocation(),
5009 NestedNameSpecifierLoc(),
5010 SourceLocation(),
5011 namespace_decl,
5012 decl_ctx);
5013 using_directive_decl->setImplicit();
5014 decl_ctx->addDecl(using_directive_decl);
5015 }
5016 }
5017#ifdef LLDB_CONFIGURATION_DEBUG
5018 VerifyDecl(namespace_decl);
5019#endif
Greg Clayton030a2042011-10-14 21:34:45 +00005020 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005021}
5022
5023
5024#pragma mark Function Types
5025
5026FunctionDecl *
Greg Clayton147e1fa2011-10-14 22:47:18 +00005027ClangASTContext::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 +00005028{
Greg Clayton147e1fa2011-10-14 22:47:18 +00005029 FunctionDecl *func_decl = NULL;
5030 ASTContext *ast = getASTContext();
5031 if (decl_ctx == NULL)
5032 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005033
Greg Clayton147e1fa2011-10-14 22:47:18 +00005034 if (name && name[0])
5035 {
5036 func_decl = FunctionDecl::Create (*ast,
5037 decl_ctx,
5038 SourceLocation(),
5039 SourceLocation(),
5040 DeclarationName (&ast->Idents.get(name)),
5041 QualType::getFromOpaquePtr(function_clang_type),
5042 NULL,
5043 (FunctionDecl::StorageClass)storage,
5044 (FunctionDecl::StorageClass)storage,
5045 is_inline);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005046 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00005047 else
5048 {
5049 func_decl = FunctionDecl::Create (*ast,
5050 decl_ctx,
5051 SourceLocation(),
5052 SourceLocation(),
5053 DeclarationName (),
5054 QualType::getFromOpaquePtr(function_clang_type),
5055 NULL,
5056 (FunctionDecl::StorageClass)storage,
5057 (FunctionDecl::StorageClass)storage,
5058 is_inline);
5059 }
5060 if (func_decl)
5061 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005062
5063#ifdef LLDB_CONFIGURATION_DEBUG
5064 VerifyDecl(func_decl);
5065#endif
5066
Greg Clayton147e1fa2011-10-14 22:47:18 +00005067 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005068}
5069
Greg Clayton1be10fc2010-09-29 01:12:09 +00005070clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00005071ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00005072 clang_type_t result_type,
5073 clang_type_t *args,
Sean Callananc81256a2010-09-16 20:40:25 +00005074 unsigned num_args,
5075 bool is_variadic,
5076 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005077{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005078 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005079 std::vector<QualType> qual_type_args;
5080 for (unsigned i=0; i<num_args; ++i)
5081 qual_type_args.push_back (QualType::getFromOpaquePtr(args[i]));
5082
5083 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00005084 FunctionProtoType::ExtProtoInfo proto_info;
5085 proto_info.Variadic = is_variadic;
Sean Callananfb0b7582011-03-15 00:17:19 +00005086 proto_info.ExceptionSpecType = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005087 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00005088 proto_info.RefQualifier = RQ_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005089 proto_info.NumExceptions = 0;
5090 proto_info.Exceptions = NULL;
5091
Greg Clayton147e1fa2011-10-14 22:47:18 +00005092 return ast->getFunctionType (QualType::getFromOpaquePtr(result_type),
5093 qual_type_args.empty() ? NULL : &qual_type_args.front(),
5094 qual_type_args.size(),
5095 proto_info).getAsOpaquePtr(); // NoReturn);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005096}
5097
5098ParmVarDecl *
Greg Clayton1be10fc2010-09-29 01:12:09 +00005099ClangASTContext::CreateParameterDeclaration (const char *name, clang_type_t param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005100{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005101 ASTContext *ast = getASTContext();
5102 assert (ast != NULL);
5103 return ParmVarDecl::Create(*ast,
5104 ast->getTranslationUnitDecl(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005105 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005106 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00005107 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callananc81256a2010-09-16 20:40:25 +00005108 QualType::getFromOpaquePtr(param_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005109 NULL,
5110 (VarDecl::StorageClass)storage,
5111 (VarDecl::StorageClass)storage,
5112 0);
5113}
5114
5115void
5116ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
5117{
5118 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00005119 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005120}
5121
5122
5123#pragma mark Array Types
5124
Greg Clayton1be10fc2010-09-29 01:12:09 +00005125clang_type_t
5126ClangASTContext::CreateArrayType (clang_type_t element_type, size_t element_count, uint32_t bit_stride)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005127{
5128 if (element_type)
5129 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00005130 ASTContext *ast = getASTContext();
5131 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005132 llvm::APInt ap_element_count (64, element_count);
Greg Clayton6beaaa62011-01-17 03:46:26 +00005133 return ast->getConstantArrayType(QualType::getFromOpaquePtr(element_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005134 ap_element_count,
5135 ArrayType::Normal,
5136 0).getAsOpaquePtr(); // ElemQuals
5137 }
5138 return NULL;
5139}
5140
5141
5142#pragma mark TagDecl
5143
5144bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005145ClangASTContext::StartTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005146{
5147 if (clang_type)
5148 {
5149 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00005150 const clang::Type *t = qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005151 if (t)
5152 {
Sean Callanan78e37602011-01-27 04:42:51 +00005153 const TagType *tag_type = dyn_cast<TagType>(t);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005154 if (tag_type)
5155 {
5156 TagDecl *tag_decl = tag_type->getDecl();
5157 if (tag_decl)
5158 {
5159 tag_decl->startDefinition();
5160 return true;
5161 }
5162 }
Sean Callanan5b26f272012-02-04 08:49:35 +00005163
5164 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(t);
5165 if (object_type)
5166 {
5167 ObjCInterfaceDecl *interface_decl = object_type->getInterface();
5168 if (interface_decl)
5169 {
5170 interface_decl->startDefinition();
5171 return true;
5172 }
5173 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005174 }
5175 }
5176 return false;
5177}
5178
5179bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005180ClangASTContext::CompleteTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005181{
5182 if (clang_type)
5183 {
5184 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton14372242010-09-29 03:44:17 +00005185
5186 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5187
5188 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005189 {
Greg Clayton14372242010-09-29 03:44:17 +00005190 cxx_record_decl->completeDefinition();
5191
5192 return true;
5193 }
5194
5195 const EnumType *enum_type = dyn_cast<EnumType>(qual_type.getTypePtr());
5196
5197 if (enum_type)
5198 {
5199 EnumDecl *enum_decl = enum_type->getDecl();
5200
5201 if (enum_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005202 {
Greg Clayton14372242010-09-29 03:44:17 +00005203 /// TODO This really needs to be fixed.
5204
5205 unsigned NumPositiveBits = 1;
5206 unsigned NumNegativeBits = 0;
5207
Greg Clayton6beaaa62011-01-17 03:46:26 +00005208 ASTContext *ast = getASTContext();
Greg Claytone02b8502010-10-12 04:29:14 +00005209
5210 QualType promotion_qual_type;
5211 // If the enum integer type is less than an integer in bit width,
5212 // then we must promote it to an integer size.
Greg Clayton6beaaa62011-01-17 03:46:26 +00005213 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
Greg Claytone02b8502010-10-12 04:29:14 +00005214 {
5215 if (enum_decl->getIntegerType()->isSignedIntegerType())
Greg Clayton6beaaa62011-01-17 03:46:26 +00005216 promotion_qual_type = ast->IntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005217 else
Greg Clayton6beaaa62011-01-17 03:46:26 +00005218 promotion_qual_type = ast->UnsignedIntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005219 }
5220 else
5221 promotion_qual_type = enum_decl->getIntegerType();
5222
5223 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
Greg Clayton14372242010-09-29 03:44:17 +00005224 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005225 }
5226 }
5227 }
5228 return false;
5229}
5230
5231
5232#pragma mark Enumeration Types
5233
Greg Clayton1be10fc2010-09-29 01:12:09 +00005234clang_type_t
Greg Claytonca512b32011-01-14 04:54:56 +00005235ClangASTContext::CreateEnumerationType
5236(
5237 const char *name,
5238 DeclContext *decl_ctx,
5239 const Declaration &decl,
5240 clang_type_t integer_qual_type
5241)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005242{
5243 // TODO: Do something intelligent with the Declaration object passed in
5244 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005245 ASTContext *ast = getASTContext();
5246 assert (ast != NULL);
Greg Claytone02b8502010-10-12 04:29:14 +00005247
5248 // TODO: ask about these...
5249// const bool IsScoped = false;
5250// const bool IsFixed = false;
5251
Greg Clayton6beaaa62011-01-17 03:46:26 +00005252 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00005253 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00005254 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00005255 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005256 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callanan48114472010-12-13 01:26:27 +00005257 NULL,
5258 false, // IsScoped
5259 false, // IsScopedUsingClassTag
5260 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00005261
5262
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005263 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00005264 {
5265 // TODO: check if we should be setting the promotion type too?
5266 enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00005267
5268 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
5269
Greg Clayton6beaaa62011-01-17 03:46:26 +00005270 return ast->getTagDeclType(enum_decl).getAsOpaquePtr();
Greg Clayton83ff3892010-09-12 23:17:56 +00005271 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005272 return NULL;
5273}
5274
Greg Clayton1be10fc2010-09-29 01:12:09 +00005275clang_type_t
5276ClangASTContext::GetEnumerationIntegerType (clang_type_t enum_clang_type)
5277{
5278 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5279
Sean Callanan78e37602011-01-27 04:42:51 +00005280 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Greg Clayton1be10fc2010-09-29 01:12:09 +00005281 if (clang_type)
5282 {
5283 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5284 if (enum_type)
5285 {
5286 EnumDecl *enum_decl = enum_type->getDecl();
5287 if (enum_decl)
5288 return enum_decl->getIntegerType().getAsOpaquePtr();
5289 }
5290 }
5291 return NULL;
5292}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005293bool
5294ClangASTContext::AddEnumerationValueToEnumerationType
5295(
Greg Clayton1be10fc2010-09-29 01:12:09 +00005296 clang_type_t enum_clang_type,
5297 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005298 const Declaration &decl,
5299 const char *name,
5300 int64_t enum_value,
5301 uint32_t enum_value_bit_size
5302)
5303{
5304 if (enum_clang_type && enumerator_clang_type && name)
5305 {
5306 // TODO: Do something intelligent with the Declaration object passed in
5307 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005308 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005309 IdentifierTable *identifier_table = getIdentifierTable();
5310
Greg Clayton6beaaa62011-01-17 03:46:26 +00005311 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005312 assert (identifier_table != NULL);
5313 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5314
Sean Callanan78e37602011-01-27 04:42:51 +00005315 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005316 if (clang_type)
5317 {
5318 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5319
5320 if (enum_type)
5321 {
5322 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, false);
5323 enum_llvm_apsint = enum_value;
5324 EnumConstantDecl *enumerator_decl =
Greg Clayton6beaaa62011-01-17 03:46:26 +00005325 EnumConstantDecl::Create (*ast,
5326 enum_type->getDecl(),
5327 SourceLocation(),
5328 name ? &identifier_table->get(name) : NULL, // Identifier
5329 QualType::getFromOpaquePtr(enumerator_clang_type),
5330 NULL,
5331 enum_llvm_apsint);
5332
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005333 if (enumerator_decl)
5334 {
5335 enum_type->getDecl()->addDecl(enumerator_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005336
5337#ifdef LLDB_CONFIGURATION_DEBUG
5338 VerifyDecl(enumerator_decl);
5339#endif
5340
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005341 return true;
5342 }
5343 }
5344 }
5345 }
5346 return false;
5347}
5348
5349#pragma mark Pointers & References
5350
Greg Clayton1be10fc2010-09-29 01:12:09 +00005351clang_type_t
5352ClangASTContext::CreatePointerType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005353{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005354 return CreatePointerType (getASTContext(), clang_type);
5355}
5356
5357clang_type_t
5358ClangASTContext::CreatePointerType (clang::ASTContext *ast, clang_type_t clang_type)
5359{
5360 if (ast && clang_type)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005361 {
5362 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5363
Greg Clayton737b9322010-09-13 03:32:57 +00005364 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5365 switch (type_class)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005366 {
5367 case clang::Type::ObjCObject:
5368 case clang::Type::ObjCInterface:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005369 return ast->getObjCObjectPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005370
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005371 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005372 return ast->getPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005373 }
5374 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005375 return NULL;
5376}
5377
Greg Clayton1be10fc2010-09-29 01:12:09 +00005378clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005379ClangASTContext::CreateLValueReferenceType (clang::ASTContext *ast,
5380 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005381{
5382 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005383 return ast->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005384 return NULL;
5385}
5386
Greg Clayton1be10fc2010-09-29 01:12:09 +00005387clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005388ClangASTContext::CreateRValueReferenceType (clang::ASTContext *ast,
5389 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005390{
5391 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005392 return ast->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005393 return NULL;
5394}
5395
Greg Clayton1be10fc2010-09-29 01:12:09 +00005396clang_type_t
5397ClangASTContext::CreateMemberPointerType (clang_type_t clang_pointee_type, clang_type_t clang_class_type)
Greg Clayton9b81a312010-06-12 01:20:30 +00005398{
5399 if (clang_pointee_type && clang_pointee_type)
5400 return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type),
5401 QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr();
5402 return NULL;
5403}
5404
Greg Clayton1a65ae12011-01-25 23:55:37 +00005405uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005406ClangASTContext::GetPointerBitSize ()
5407{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005408 ASTContext *ast = getASTContext();
5409 return ast->getTypeSize(ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005410}
5411
5412bool
Greg Clayton219cf312012-03-30 00:51:13 +00005413ClangASTContext::IsPossibleDynamicType (clang::ASTContext *ast,
5414 clang_type_t clang_type,
5415 clang_type_t *dynamic_pointee_type,
5416 bool check_cplusplus,
5417 bool check_objc)
Greg Claytondea8cb42011-06-29 22:09:02 +00005418{
5419 QualType pointee_qual_type;
5420 if (clang_type)
5421 {
5422 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5423 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5424 bool success = false;
5425 switch (type_class)
5426 {
5427 case clang::Type::Builtin:
Greg Clayton219cf312012-03-30 00:51:13 +00005428 if (check_objc && cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
Greg Claytondea8cb42011-06-29 22:09:02 +00005429 {
5430 if (dynamic_pointee_type)
5431 *dynamic_pointee_type = clang_type;
5432 return true;
5433 }
5434 break;
5435
5436 case clang::Type::ObjCObjectPointer:
Greg Clayton219cf312012-03-30 00:51:13 +00005437 if (check_objc)
5438 {
5439 if (dynamic_pointee_type)
5440 *dynamic_pointee_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5441 return true;
5442 }
5443 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005444
5445 case clang::Type::Pointer:
5446 pointee_qual_type = cast<PointerType>(qual_type)->getPointeeType();
5447 success = true;
5448 break;
5449
5450 case clang::Type::LValueReference:
5451 case clang::Type::RValueReference:
5452 pointee_qual_type = cast<ReferenceType>(qual_type)->getPointeeType();
5453 success = true;
5454 break;
5455
5456 case clang::Type::Typedef:
Greg Claytonaffb03b2011-07-08 18:27:39 +00005457 return ClangASTContext::IsPossibleDynamicType (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), dynamic_pointee_type);
Sean Callanan912855f2011-08-11 23:56:13 +00005458
5459 case clang::Type::Elaborated:
5460 return ClangASTContext::IsPossibleDynamicType (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), dynamic_pointee_type);
5461
Greg Claytondea8cb42011-06-29 22:09:02 +00005462 default:
5463 break;
5464 }
5465
5466 if (success)
5467 {
5468 // Check to make sure what we are pointing too is a possible dynamic C++ type
5469 // We currently accept any "void *" (in case we have a class that has been
5470 // watered down to an opaque pointer) and virtual C++ classes.
5471 const clang::Type::TypeClass pointee_type_class = pointee_qual_type->getTypeClass();
5472 switch (pointee_type_class)
5473 {
5474 case clang::Type::Builtin:
5475 switch (cast<clang::BuiltinType>(pointee_qual_type)->getKind())
5476 {
5477 case clang::BuiltinType::UnknownAny:
5478 case clang::BuiltinType::Void:
5479 if (dynamic_pointee_type)
5480 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5481 return true;
5482
5483 case clang::BuiltinType::NullPtr:
5484 case clang::BuiltinType::Bool:
5485 case clang::BuiltinType::Char_U:
5486 case clang::BuiltinType::UChar:
5487 case clang::BuiltinType::WChar_U:
5488 case clang::BuiltinType::Char16:
5489 case clang::BuiltinType::Char32:
5490 case clang::BuiltinType::UShort:
5491 case clang::BuiltinType::UInt:
5492 case clang::BuiltinType::ULong:
5493 case clang::BuiltinType::ULongLong:
5494 case clang::BuiltinType::UInt128:
5495 case clang::BuiltinType::Char_S:
5496 case clang::BuiltinType::SChar:
5497 case clang::BuiltinType::WChar_S:
5498 case clang::BuiltinType::Short:
5499 case clang::BuiltinType::Int:
5500 case clang::BuiltinType::Long:
5501 case clang::BuiltinType::LongLong:
5502 case clang::BuiltinType::Int128:
5503 case clang::BuiltinType::Float:
5504 case clang::BuiltinType::Double:
5505 case clang::BuiltinType::LongDouble:
5506 case clang::BuiltinType::Dependent:
5507 case clang::BuiltinType::Overload:
5508 case clang::BuiltinType::ObjCId:
5509 case clang::BuiltinType::ObjCClass:
5510 case clang::BuiltinType::ObjCSel:
5511 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00005512 case clang::BuiltinType::Half:
5513 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00005514 case clang::BuiltinType::PseudoObject:
Greg Claytondea8cb42011-06-29 22:09:02 +00005515 break;
5516 }
5517 break;
5518
5519 case clang::Type::Record:
Greg Clayton219cf312012-03-30 00:51:13 +00005520 if (check_cplusplus)
Greg Claytondea8cb42011-06-29 22:09:02 +00005521 {
5522 CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
5523 if (cxx_record_decl)
5524 {
Greg Claytone26928c2012-03-22 22:23:17 +00005525 // Do NOT complete the type here like we used to do
5526 // otherwise EVERY "class *" variable we have will try
5527 // to fully complete itself and this will take a lot of
5528 // time, memory and slow down debugging. If we have a complete
5529 // type, then answer the question definitively, else we
5530 // just say that a C++ class can possibly be dynamic...
Greg Clayton219cf312012-03-30 00:51:13 +00005531 if (cxx_record_decl->isCompleteDefinition())
Greg Claytondea8cb42011-06-29 22:09:02 +00005532 {
5533 success = cxx_record_decl->isDynamicClass();
5534 }
5535 else
5536 {
5537 // We failed to get the complete type, so we have to
5538 // treat this as a void * which we might possibly be
5539 // able to complete
5540 success = true;
5541 }
5542 if (success)
5543 {
5544 if (dynamic_pointee_type)
5545 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5546 return true;
5547 }
5548 }
5549 }
5550 break;
5551
5552 case clang::Type::ObjCObject:
5553 case clang::Type::ObjCInterface:
Greg Clayton219cf312012-03-30 00:51:13 +00005554 if (check_objc)
5555 {
5556 if (dynamic_pointee_type)
5557 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5558 return true;
5559 }
5560 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005561
5562 default:
5563 break;
5564 }
5565 }
5566 }
5567 if (dynamic_pointee_type)
5568 *dynamic_pointee_type = NULL;
5569 return false;
5570}
5571
5572
5573bool
Greg Clayton007d5be2011-05-30 00:49:24 +00005574ClangASTContext::IsPossibleCPlusPlusDynamicType (clang::ASTContext *ast, clang_type_t clang_type, clang_type_t *dynamic_pointee_type)
5575{
Greg Clayton219cf312012-03-30 00:51:13 +00005576 return IsPossibleDynamicType (ast,
5577 clang_type,
5578 dynamic_pointee_type,
5579 true, // Check for dynamic C++ types
5580 false); // Check for dynamic ObjC types
Greg Clayton007d5be2011-05-30 00:49:24 +00005581}
5582
Sean Callanan98298012011-10-27 19:41:13 +00005583bool
5584ClangASTContext::IsReferenceType (clang_type_t clang_type, clang_type_t *target_type)
5585{
5586 if (clang_type == NULL)
5587 return false;
5588
5589 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5590 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5591
5592 switch (type_class)
5593 {
5594 case clang::Type::LValueReference:
5595 if (target_type)
5596 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5597 return true;
5598 case clang::Type::RValueReference:
5599 if (target_type)
5600 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5601 return true;
5602 case clang::Type::Typedef:
5603 return ClangASTContext::IsReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5604 case clang::Type::Elaborated:
5605 return ClangASTContext::IsReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5606 default:
5607 break;
5608 }
5609
5610 return false;
5611}
Greg Clayton007d5be2011-05-30 00:49:24 +00005612
5613bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005614ClangASTContext::IsPointerOrReferenceType (clang_type_t clang_type, clang_type_t*target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005615{
5616 if (clang_type == NULL)
5617 return false;
5618
5619 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005620 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5621 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005622 {
Sean Callanana2424172010-10-25 00:29:48 +00005623 case clang::Type::Builtin:
5624 switch (cast<clang::BuiltinType>(qual_type)->getKind())
5625 {
5626 default:
5627 break;
5628 case clang::BuiltinType::ObjCId:
5629 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00005630 return true;
5631 }
5632 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00005633 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005634 if (target_type)
5635 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5636 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005637 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005638 if (target_type)
5639 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5640 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005641 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005642 if (target_type)
5643 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5644 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005645 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005646 if (target_type)
5647 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5648 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005649 case clang::Type::LValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005650 if (target_type)
5651 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5652 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005653 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005654 if (target_type)
5655 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5656 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005657 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00005658 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00005659 case clang::Type::Elaborated:
5660 return ClangASTContext::IsPointerOrReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005661 default:
5662 break;
5663 }
5664 return false;
5665}
5666
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005667bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005668ClangASTContext::IsIntegerType (clang_type_t clang_type, bool &is_signed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005669{
5670 if (!clang_type)
5671 return false;
5672
5673 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5674 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal());
5675
5676 if (builtin_type)
5677 {
5678 if (builtin_type->isInteger())
Jim Inghamef651602011-12-22 19:12:40 +00005679 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005680 is_signed = builtin_type->isSignedInteger();
Jim Inghamef651602011-12-22 19:12:40 +00005681 return true;
5682 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005683 }
5684
5685 return false;
5686}
5687
5688bool
Greg Claytonaffb03b2011-07-08 18:27:39 +00005689ClangASTContext::IsPointerType (clang_type_t clang_type, clang_type_t *target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005690{
Greg Claytonaffb03b2011-07-08 18:27:39 +00005691 if (target_type)
5692 *target_type = NULL;
5693
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005694 if (clang_type)
5695 {
5696 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005697 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5698 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005699 {
Sean Callanana2424172010-10-25 00:29:48 +00005700 case clang::Type::Builtin:
5701 switch (cast<clang::BuiltinType>(qual_type)->getKind())
5702 {
5703 default:
5704 break;
5705 case clang::BuiltinType::ObjCId:
5706 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00005707 return true;
5708 }
5709 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00005710 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005711 if (target_type)
5712 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5713 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005714 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005715 if (target_type)
5716 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5717 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005718 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005719 if (target_type)
5720 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5721 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005722 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005723 if (target_type)
5724 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5725 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005726 case clang::Type::Typedef:
Greg Claytonaffb03b2011-07-08 18:27:39 +00005727 return ClangASTContext::IsPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), target_type);
Sean Callanan912855f2011-08-11 23:56:13 +00005728 case clang::Type::Elaborated:
5729 return ClangASTContext::IsPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), target_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005730 default:
5731 break;
5732 }
5733 }
5734 return false;
5735}
5736
5737bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005738ClangASTContext::IsFloatingPointType (clang_type_t clang_type, uint32_t &count, bool &is_complex)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005739{
5740 if (clang_type)
5741 {
5742 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5743
5744 if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()))
5745 {
5746 clang::BuiltinType::Kind kind = BT->getKind();
5747 if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble)
5748 {
5749 count = 1;
5750 is_complex = false;
5751 return true;
5752 }
5753 }
5754 else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal()))
5755 {
5756 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex))
5757 {
5758 count = 2;
5759 is_complex = true;
5760 return true;
5761 }
5762 }
5763 else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal()))
5764 {
5765 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex))
5766 {
5767 count = VT->getNumElements();
5768 is_complex = false;
5769 return true;
5770 }
5771 }
5772 }
5773 return false;
5774}
5775
Enrico Granata9fc19442011-07-06 02:13:41 +00005776bool
5777ClangASTContext::IsScalarType (lldb::clang_type_t clang_type)
5778{
5779 bool is_signed;
5780 if (ClangASTContext::IsIntegerType(clang_type, is_signed))
5781 return true;
5782
5783 uint32_t count;
5784 bool is_complex;
5785 return ClangASTContext::IsFloatingPointType(clang_type, count, is_complex) && !is_complex;
5786}
5787
5788bool
5789ClangASTContext::IsPointerToScalarType (lldb::clang_type_t clang_type)
5790{
5791 if (!IsPointerType(clang_type))
5792 return false;
5793
5794 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5795 lldb::clang_type_t pointee_type = qual_type.getTypePtr()->getPointeeType().getAsOpaquePtr();
5796 return IsScalarType(pointee_type);
5797}
5798
5799bool
5800ClangASTContext::IsArrayOfScalarType (lldb::clang_type_t clang_type)
5801{
Sean Callanan0caa21c2012-01-19 23:54:24 +00005802 clang_type = GetAsArrayType(clang_type);
5803
5804 if (clang_type == 0)
Enrico Granata9fc19442011-07-06 02:13:41 +00005805 return false;
5806
5807 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5808 lldb::clang_type_t item_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
5809 return IsScalarType(item_type);
5810}
5811
Greg Clayton8f92f0a2010-10-14 22:52:14 +00005812
5813bool
5814ClangASTContext::GetCXXClassName (clang_type_t clang_type, std::string &class_name)
5815{
5816 if (clang_type)
5817 {
5818 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5819
5820 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5821 if (cxx_record_decl)
5822 {
5823 class_name.assign (cxx_record_decl->getIdentifier()->getNameStart());
5824 return true;
5825 }
5826 }
5827 class_name.clear();
5828 return false;
5829}
5830
5831
Greg Clayton0fffff52010-09-24 05:15:53 +00005832bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005833ClangASTContext::IsCXXClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00005834{
5835 if (clang_type)
5836 {
5837 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5838 if (qual_type->getAsCXXRecordDecl() != NULL)
5839 return true;
5840 }
5841 return false;
5842}
5843
Greg Clayton20568dd2011-10-13 23:13:20 +00005844bool
5845ClangASTContext::IsBeingDefined (lldb::clang_type_t clang_type)
5846{
5847 if (clang_type)
5848 {
5849 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5850 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type);
5851 if (tag_type)
5852 return tag_type->isBeingDefined();
5853 }
5854 return false;
5855}
5856
Greg Clayton0fffff52010-09-24 05:15:53 +00005857bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005858ClangASTContext::IsObjCClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00005859{
5860 if (clang_type)
5861 {
5862 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5863 if (qual_type->isObjCObjectOrInterfaceType())
5864 return true;
5865 }
5866 return false;
5867}
5868
Sean Callanan72772842012-02-22 23:57:45 +00005869bool
5870ClangASTContext::IsObjCObjectPointerType (lldb::clang_type_t clang_type, clang_type_t *class_type)
5871{
5872 if (clang_type)
5873 {
5874 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5875 if (qual_type->isObjCObjectPointerType())
5876 {
5877 if (class_type)
5878 {
5879 *class_type = NULL;
5880
5881 if (!qual_type->isObjCClassType() &&
5882 !qual_type->isObjCIdType())
5883 {
5884 const ObjCObjectPointerType *obj_pointer_type = dyn_cast<ObjCObjectPointerType>(qual_type);
Sean Callanand7dabe22012-03-10 01:59:11 +00005885 if (!obj_pointer_type)
5886 *class_type = NULL;
Sean Callanan1fc91ad2012-03-10 02:00:32 +00005887 else
5888 *class_type = QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr();
Sean Callanan72772842012-02-22 23:57:45 +00005889 }
5890 }
5891 return true;
5892 }
5893 }
5894 return false;
5895}
5896
5897bool
5898ClangASTContext::GetObjCClassName (lldb::clang_type_t clang_type,
5899 std::string &class_name)
5900{
5901 if (!clang_type)
5902 return false;
5903
5904 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(QualType::getFromOpaquePtr(clang_type));
5905 if (!object_type)
5906 return false;
5907
5908 const ObjCInterfaceDecl *interface = object_type->getInterface();
5909 if (!interface)
5910 return false;
5911
5912 class_name = interface->getNameAsString();
5913 return true;
5914}
Greg Clayton0fffff52010-09-24 05:15:53 +00005915
Greg Clayton73b472d2010-10-27 03:32:59 +00005916bool
5917ClangASTContext::IsCharType (clang_type_t clang_type)
5918{
5919 if (clang_type)
5920 return QualType::getFromOpaquePtr(clang_type)->isCharType();
5921 return false;
5922}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005923
5924bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005925ClangASTContext::IsCStringType (clang_type_t clang_type, uint32_t &length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005926{
Greg Clayton73b472d2010-10-27 03:32:59 +00005927 clang_type_t pointee_or_element_clang_type = NULL;
5928 Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, &pointee_or_element_clang_type));
5929
5930 if (pointee_or_element_clang_type == NULL)
5931 return false;
5932
5933 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005934 {
Greg Clayton73b472d2010-10-27 03:32:59 +00005935 QualType pointee_or_element_qual_type (QualType::getFromOpaquePtr (pointee_or_element_clang_type));
5936
5937 if (pointee_or_element_qual_type.getUnqualifiedType()->isCharType())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005938 {
Greg Clayton73b472d2010-10-27 03:32:59 +00005939 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5940 if (type_flags.Test (eTypeIsArray))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005941 {
Greg Clayton73b472d2010-10-27 03:32:59 +00005942 // We know the size of the array and it could be a C string
5943 // since it is an array of characters
5944 length = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
5945 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005946 }
Greg Clayton73b472d2010-10-27 03:32:59 +00005947 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005948 {
Greg Clayton73b472d2010-10-27 03:32:59 +00005949 length = 0;
5950 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005951 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005952
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005953 }
5954 }
5955 return false;
5956}
5957
5958bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005959ClangASTContext::IsFunctionPointerType (clang_type_t clang_type)
Greg Clayton737b9322010-09-13 03:32:57 +00005960{
5961 if (clang_type)
5962 {
5963 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5964
5965 if (qual_type->isFunctionPointerType())
5966 return true;
5967
5968 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5969 switch (type_class)
5970 {
Sean Callananfb0b7582011-03-15 00:17:19 +00005971 default:
5972 break;
Greg Clayton737b9322010-09-13 03:32:57 +00005973 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00005974 return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00005975 case clang::Type::Elaborated:
5976 return ClangASTContext::IsFunctionPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00005977
5978 case clang::Type::LValueReference:
5979 case clang::Type::RValueReference:
5980 {
Sean Callanan78e37602011-01-27 04:42:51 +00005981 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00005982 if (reference_type)
5983 return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr());
5984 }
5985 break;
5986 }
5987 }
5988 return false;
5989}
5990
Greg Clayton73b472d2010-10-27 03:32:59 +00005991size_t
5992ClangASTContext::GetArraySize (clang_type_t clang_type)
5993{
5994 if (clang_type)
5995 {
Greg Claytonef37d68a2011-07-09 17:12:27 +00005996 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
5997 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5998 switch (type_class)
5999 {
6000 case clang::Type::ConstantArray:
6001 {
6002 const ConstantArrayType *array = cast<ConstantArrayType>(QualType::getFromOpaquePtr(clang_type).getTypePtr());
6003 if (array)
6004 return array->getSize().getLimitedValue();
6005 }
6006 break;
6007
6008 case clang::Type::Typedef:
6009 return ClangASTContext::GetArraySize(cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006010
6011 case clang::Type::Elaborated:
6012 return ClangASTContext::GetArraySize(cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00006013
6014 default:
6015 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006016 }
Greg Clayton73b472d2010-10-27 03:32:59 +00006017 }
6018 return 0;
6019}
Greg Clayton737b9322010-09-13 03:32:57 +00006020
Sean Callanan0caa21c2012-01-19 23:54:24 +00006021clang_type_t
6022ClangASTContext::GetAsArrayType (clang_type_t clang_type, clang_type_t*member_type, uint64_t *size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006023{
6024 if (!clang_type)
Sean Callanan0caa21c2012-01-19 23:54:24 +00006025 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006026
6027 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6028
Greg Clayton737b9322010-09-13 03:32:57 +00006029 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6030 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006031 {
Sean Callananfb0b7582011-03-15 00:17:19 +00006032 default:
6033 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006034
Greg Claytone1a916a2010-07-21 22:12:05 +00006035 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006036 if (member_type)
6037 *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6038 if (size)
Greg Claytonac4827f2011-04-01 18:14:08 +00006039 *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
Sean Callanan0caa21c2012-01-19 23:54:24 +00006040 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006041
Greg Claytone1a916a2010-07-21 22:12:05 +00006042 case clang::Type::IncompleteArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006043 if (member_type)
6044 *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6045 if (size)
6046 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006047 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006048
Greg Claytone1a916a2010-07-21 22:12:05 +00006049 case clang::Type::VariableArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006050 if (member_type)
6051 *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6052 if (size)
6053 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006054 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006055
Greg Claytone1a916a2010-07-21 22:12:05 +00006056 case clang::Type::DependentSizedArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006057 if (member_type)
6058 *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6059 if (size)
6060 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006061 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006062
6063 case clang::Type::Typedef:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006064 return ClangASTContext::GetAsArrayType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
6065 member_type,
6066 size);
Sean Callanan912855f2011-08-11 23:56:13 +00006067
6068 case clang::Type::Elaborated:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006069 return ClangASTContext::GetAsArrayType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
6070 member_type,
6071 size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006072 }
Sean Callanan0caa21c2012-01-19 23:54:24 +00006073 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006074}
6075
6076
6077#pragma mark Typedefs
6078
Greg Clayton1be10fc2010-09-29 01:12:09 +00006079clang_type_t
6080ClangASTContext::CreateTypedefType (const char *name, clang_type_t clang_type, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006081{
6082 if (clang_type)
6083 {
6084 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006085 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006086 IdentifierTable *identifier_table = getIdentifierTable();
Greg Clayton6beaaa62011-01-17 03:46:26 +00006087 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006088 assert (identifier_table != NULL);
6089 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00006090 decl_ctx = ast->getTranslationUnitDecl();
6091 TypedefDecl *decl = TypedefDecl::Create (*ast,
6092 decl_ctx,
6093 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00006094 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00006095 name ? &identifier_table->get(name) : NULL, // Identifier
6096 ast->CreateTypeSourceInfo(qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00006097
Greg Clayton147e1fa2011-10-14 22:47:18 +00006098 //decl_ctx->addDecl (decl);
6099
Sean Callanan2652ad22011-01-18 01:03:44 +00006100 decl->setAccess(AS_public); // TODO respect proper access specifier
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006101
6102 // Get a uniqued QualType for the typedef decl type
Greg Clayton6beaaa62011-01-17 03:46:26 +00006103 return ast->getTypedefType (decl).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006104 }
6105 return NULL;
6106}
6107
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006108// Disable this for now since I can't seem to get a nicely formatted float
6109// out of the APFloat class without just getting the float, double or quad
6110// and then using a formatted print on it which defeats the purpose. We ideally
6111// would like to get perfect string values for any kind of float semantics
6112// so we can support remote targets. The code below also requires a patch to
6113// llvm::APInt.
6114//bool
Greg Clayton6beaaa62011-01-17 03:46:26 +00006115//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 +00006116//{
6117// uint32_t count = 0;
6118// bool is_complex = false;
6119// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6120// {
6121// unsigned num_bytes_per_float = byte_size / count;
6122// unsigned num_bits_per_float = num_bytes_per_float * 8;
6123//
6124// float_str.clear();
6125// uint32_t i;
6126// for (i=0; i<count; i++)
6127// {
6128// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
6129// bool is_ieee = false;
6130// APFloat ap_float(ap_int, is_ieee);
6131// char s[1024];
6132// unsigned int hex_digits = 0;
6133// bool upper_case = false;
6134//
6135// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
6136// {
6137// if (i > 0)
6138// float_str.append(", ");
6139// float_str.append(s);
6140// if (i == 1 && is_complex)
6141// float_str.append(1, 'i');
6142// }
6143// }
6144// return !float_str.empty();
6145// }
6146// return false;
6147//}
6148
6149size_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00006150ClangASTContext::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 +00006151{
6152 if (clang_type)
6153 {
6154 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6155 uint32_t count = 0;
6156 bool is_complex = false;
6157 if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6158 {
6159 // TODO: handle complex and vector types
6160 if (count != 1)
6161 return false;
6162
6163 StringRef s_sref(s);
Greg Clayton6beaaa62011-01-17 03:46:26 +00006164 APFloat ap_float(ast->getFloatTypeSemantics(qual_type), s_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006165
Greg Clayton6beaaa62011-01-17 03:46:26 +00006166 const uint64_t bit_size = ast->getTypeSize (qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006167 const uint64_t byte_size = bit_size / 8;
6168 if (dst_size >= byte_size)
6169 {
6170 if (bit_size == sizeof(float)*8)
6171 {
6172 float float32 = ap_float.convertToFloat();
6173 ::memcpy (dst, &float32, byte_size);
6174 return byte_size;
6175 }
6176 else if (bit_size >= 64)
6177 {
6178 llvm::APInt ap_int(ap_float.bitcastToAPInt());
6179 ::memcpy (dst, ap_int.getRawData(), byte_size);
6180 return byte_size;
6181 }
6182 }
6183 }
6184 }
6185 return 0;
6186}
Sean Callanan6fe64b52010-09-17 02:24:29 +00006187
6188unsigned
Greg Clayton1be10fc2010-09-29 01:12:09 +00006189ClangASTContext::GetTypeQualifiers(clang_type_t clang_type)
Sean Callanan6fe64b52010-09-17 02:24:29 +00006190{
6191 assert (clang_type);
6192
6193 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6194
6195 return qual_type.getQualifiers().getCVRQualifiers();
6196}
Greg Clayton6beaaa62011-01-17 03:46:26 +00006197
Sean Callanan3b107b12011-12-03 03:15:28 +00006198uint64_t
6199GetTypeFlags(clang::ASTContext *ast, lldb::clang_type_t clang_type)
6200{
6201 assert (clang_type);
6202
6203 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
6204
6205 if (!external_ast_source)
6206 return 0;
6207
6208 ClangExternalASTSourceCommon *common_ast_source = static_cast<ClangExternalASTSourceCommon*>(external_ast_source);
6209
6210 return common_ast_source->GetMetadata((uintptr_t)clang_type);
6211}
6212
6213void
6214SetTypeFlags(clang::ASTContext *ast, lldb::clang_type_t clang_type, uint64_t flags)
6215{
6216 assert (clang_type);
6217
6218 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
6219
6220 if (!external_ast_source)
6221 return;
6222
6223 ClangExternalASTSourceCommon *common_ast_source = static_cast<ClangExternalASTSourceCommon*>(external_ast_source);
6224
6225 return common_ast_source->SetMetadata((uintptr_t)clang_type, flags);
6226}
6227
Greg Clayton6beaaa62011-01-17 03:46:26 +00006228bool
6229ClangASTContext::GetCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6230{
6231 if (clang_type == NULL)
6232 return false;
6233
Greg Claytonc432c192011-01-20 04:18:48 +00006234 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006235}
6236
6237
6238bool
6239ClangASTContext::GetCompleteType (clang_type_t clang_type)
6240{
6241 return ClangASTContext::GetCompleteType (getASTContext(), clang_type);
6242}
6243
Greg Claytona2721472011-06-25 00:44:06 +00006244bool
Enrico Granata86027e92012-03-24 01:11:14 +00006245ClangASTContext::IsCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6246{
6247 if (clang_type == NULL)
6248 return false;
6249
6250 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type), false); // just check but don't let it actually complete
6251}
6252
6253
6254bool
6255ClangASTContext::IsCompleteType (clang_type_t clang_type)
6256{
6257 return ClangASTContext::IsCompleteType (getASTContext(), clang_type);
6258}
6259
6260bool
Greg Claytona2721472011-06-25 00:44:06 +00006261ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
6262 clang::Decl *decl)
6263{
6264 if (!decl)
6265 return false;
6266
6267 ExternalASTSource *ast_source = ast->getExternalSource();
6268
6269 if (!ast_source)
6270 return false;
6271
6272 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
6273 {
Greg Clayton219cf312012-03-30 00:51:13 +00006274 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006275 return true;
6276
6277 if (!tag_decl->hasExternalLexicalStorage())
6278 return false;
6279
6280 ast_source->CompleteType(tag_decl);
6281
6282 return !tag_decl->getTypeForDecl()->isIncompleteType();
6283 }
6284 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
6285 {
Sean Callanan5b26f272012-02-04 08:49:35 +00006286 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006287 return true;
6288
6289 if (!objc_interface_decl->hasExternalLexicalStorage())
6290 return false;
6291
6292 ast_source->CompleteType(objc_interface_decl);
6293
Sean Callanan5b26f272012-02-04 08:49:35 +00006294 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00006295 }
6296 else
6297 {
6298 return false;
6299 }
6300}
6301
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006302clang::DeclContext *
6303ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
6304{
Sean Callanana87bee82011-08-19 06:19:25 +00006305 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006306}
6307
6308clang::DeclContext *
6309ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
6310{
Sean Callanana87bee82011-08-19 06:19:25 +00006311 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006312}
6313