blob: 88e18a5db525b92c3b3ec92af1fea7a398052bfe [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{
Sean Callanan5056ab02012-02-18 02:01:03 +00001048 QualType type1_qual = QualType::getFromOpaquePtr(type1);
1049 QualType type2_qual = QualType::getFromOpaquePtr(type2);
1050
1051 if (ignore_qualifiers)
1052 {
1053 type1_qual = type1_qual.getUnqualifiedType();
1054 type2_qual = type2_qual.getUnqualifiedType();
1055 }
1056
1057 return ast->hasSameType (type1_qual,
1058 type2_qual);
Sean Callanan4dcca2622010-07-15 22:30:52 +00001059}
1060
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001061#pragma mark CVR modifiers
1062
Greg Clayton1be10fc2010-09-29 01:12:09 +00001063clang_type_t
1064ClangASTContext::AddConstModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001065{
1066 if (clang_type)
1067 {
1068 QualType result(QualType::getFromOpaquePtr(clang_type));
1069 result.addConst();
1070 return result.getAsOpaquePtr();
1071 }
1072 return NULL;
1073}
1074
Greg Clayton1be10fc2010-09-29 01:12:09 +00001075clang_type_t
1076ClangASTContext::AddRestrictModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001077{
1078 if (clang_type)
1079 {
1080 QualType result(QualType::getFromOpaquePtr(clang_type));
1081 result.getQualifiers().setRestrict (true);
1082 return result.getAsOpaquePtr();
1083 }
1084 return NULL;
1085}
1086
Greg Clayton1be10fc2010-09-29 01:12:09 +00001087clang_type_t
1088ClangASTContext::AddVolatileModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089{
1090 if (clang_type)
1091 {
1092 QualType result(QualType::getFromOpaquePtr(clang_type));
1093 result.getQualifiers().setVolatile (true);
1094 return result.getAsOpaquePtr();
1095 }
1096 return NULL;
1097}
1098
Greg Clayton6beaaa62011-01-17 03:46:26 +00001099
1100clang_type_t
1101ClangASTContext::GetTypeForDecl (TagDecl *decl)
1102{
1103 // No need to call the getASTContext() accessor (which can create the AST
1104 // if it isn't created yet, because we can't have created a decl in this
1105 // AST if our AST didn't already exist...
1106 if (m_ast_ap.get())
1107 return m_ast_ap->getTagDeclType(decl).getAsOpaquePtr();
1108 return NULL;
1109}
1110
1111clang_type_t
1112ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1113{
1114 // No need to call the getASTContext() accessor (which can create the AST
1115 // if it isn't created yet, because we can't have created a decl in this
1116 // AST if our AST didn't already exist...
1117 if (m_ast_ap.get())
1118 return m_ast_ap->getObjCInterfaceType(decl).getAsOpaquePtr();
1119 return NULL;
1120}
1121
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122#pragma mark Structure, Unions, Classes
1123
Greg Clayton1be10fc2010-09-29 01:12:09 +00001124clang_type_t
Greg Clayton55561e92011-10-26 03:31:36 +00001125ClangASTContext::CreateRecordType (DeclContext *decl_ctx, AccessType access_type, const char *name, int kind, LanguageType language)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001126{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001127 ASTContext *ast = getASTContext();
1128 assert (ast != NULL);
Sean Callanana2424172010-10-25 00:29:48 +00001129
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001130 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001131 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001132
Greg Clayton9e409562010-07-28 02:04:09 +00001133
Greg Claytone1be9962011-08-24 23:50:00 +00001134 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
Greg Clayton9e409562010-07-28 02:04:09 +00001135 {
Greg Claytonaaf99e02010-10-11 02:25:34 +00001136 bool isForwardDecl = true;
Greg Clayton9e409562010-07-28 02:04:09 +00001137 bool isInternal = false;
1138 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal);
1139 }
1140
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001141 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1142 // we will need to update this code. I was told to currently always use
1143 // the CXXRecordDecl class since we often don't know from debug information
1144 // if something is struct or a class, so we default to always use the more
1145 // complete definition just in case.
Greg Claytonf0705c82011-10-22 03:33:13 +00001146 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1147 (TagDecl::TagKind)kind,
1148 decl_ctx,
1149 SourceLocation(),
1150 SourceLocation(),
1151 name && name[0] ? &ast->Idents.get(name) : NULL);
Sean Callanan7282e2a2012-01-13 22:10:18 +00001152
1153 if (!name)
1154 decl->setAnonymousStructOrUnion(true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001155
Greg Clayton55561e92011-10-26 03:31:36 +00001156 if (decl_ctx)
1157 {
1158 if (access_type != eAccessNone)
1159 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
1160 decl_ctx->addDecl (decl);
1161 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001162 return ast->getTagDeclType(decl).getAsOpaquePtr();
1163}
1164
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001165static TemplateParameterList *
1166CreateTemplateParameterList (ASTContext *ast,
1167 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1168 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1169{
1170 const bool parameter_pack = false;
1171 const bool is_typename = false;
1172 const unsigned depth = 0;
1173 const size_t num_template_params = template_param_infos.GetSize();
1174 for (size_t i=0; i<num_template_params; ++i)
1175 {
1176 const char *name = template_param_infos.names[i];
1177 if (template_param_infos.args[i].getAsIntegral())
1178 {
1179 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1180 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1181 SourceLocation(),
1182 SourceLocation(),
1183 depth,
1184 i,
1185 &ast->Idents.get(name),
1186 template_param_infos.args[i].getIntegralType(),
1187 parameter_pack,
1188 NULL));
1189
1190 }
1191 else
1192 {
1193 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1194 ast->getTranslationUnitDecl(), // Is this the right decl context?
1195 SourceLocation(),
1196 SourceLocation(),
1197 depth,
1198 i,
1199 &ast->Idents.get(name),
1200 is_typename,
1201 parameter_pack));
1202 }
1203 }
1204
1205 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1206 SourceLocation(),
1207 SourceLocation(),
1208 &template_param_decls.front(),
1209 template_param_decls.size(),
1210 SourceLocation());
1211 return template_param_list;
1212}
1213
1214clang::FunctionTemplateDecl *
1215ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1216 clang::FunctionDecl *func_decl,
1217 const char *name,
1218 const TemplateParameterInfos &template_param_infos)
1219{
1220// /// \brief Create a function template node.
1221 ASTContext *ast = getASTContext();
1222
1223 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1224
1225 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1226 template_param_infos,
1227 template_param_decls);
1228 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1229 decl_ctx,
1230 func_decl->getLocation(),
1231 func_decl->getDeclName(),
1232 template_param_list,
1233 func_decl);
1234
1235 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1236 i < template_param_decl_count;
1237 ++i)
1238 {
1239 // TODO: verify which decl context we should put template_param_decls into..
1240 template_param_decls[i]->setDeclContext (func_decl);
1241 }
1242
1243 return func_tmpl_decl;
1244}
1245
1246void
1247ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1248 clang::FunctionTemplateDecl *func_tmpl_decl,
1249 const TemplateParameterInfos &infos)
1250{
1251 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1252 infos.args.data(),
1253 infos.args.size());
1254
1255 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1256 &template_args,
1257 NULL);
1258}
1259
1260
Greg Claytonf0705c82011-10-22 03:33:13 +00001261ClassTemplateDecl *
1262ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001263 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001264 const char *class_name,
1265 int kind,
1266 const TemplateParameterInfos &template_param_infos)
1267{
1268 ASTContext *ast = getASTContext();
1269
1270 ClassTemplateDecl *class_template_decl = NULL;
1271 if (decl_ctx == NULL)
1272 decl_ctx = ast->getTranslationUnitDecl();
1273
1274 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1275 DeclarationName decl_name (&identifier_info);
1276
1277 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1278 for (clang::DeclContext::lookup_iterator pos = result.first, end = result.second; pos != end; ++pos)
1279 {
1280 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(*pos);
1281 if (class_template_decl)
1282 return class_template_decl;
1283 }
1284
1285 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Claytonf0705c82011-10-22 03:33:13 +00001286
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001287 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1288 template_param_infos,
1289 template_param_decls);
Greg Claytonf0705c82011-10-22 03:33:13 +00001290
1291 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1292 (TagDecl::TagKind)kind,
1293 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1294 SourceLocation(),
1295 SourceLocation(),
1296 &identifier_info);
Greg Claytone04741d2011-12-02 02:09:28 +00001297
1298 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1299 i < template_param_decl_count;
1300 ++i)
1301 {
1302 template_param_decls[i]->setDeclContext (template_cxx_decl);
1303 }
1304
Sean Callananb5c79622011-11-19 01:35:08 +00001305 // With templated classes, we say that a class is templated with
1306 // specializations, but that the bare class has no functions.
1307 template_cxx_decl->startDefinition();
1308 template_cxx_decl->completeDefinition();
1309
Greg Claytonf0705c82011-10-22 03:33:13 +00001310 class_template_decl = ClassTemplateDecl::Create (*ast,
1311 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1312 SourceLocation(),
1313 decl_name,
1314 template_param_list,
1315 template_cxx_decl,
1316 NULL);
1317
1318 if (class_template_decl)
Sean Callanan5e9e1992011-10-26 01:06:27 +00001319 {
Greg Clayton55561e92011-10-26 03:31:36 +00001320 if (access_type != eAccessNone)
1321 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Sean Callanan5b26f272012-02-04 08:49:35 +00001322
1323 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1324 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1325
Greg Claytonf0705c82011-10-22 03:33:13 +00001326 decl_ctx->addDecl (class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001327
1328#ifdef LLDB_CONFIGURATION_DEBUG
1329 VerifyDecl(class_template_decl);
1330#endif
1331 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001332
1333 return class_template_decl;
1334}
1335
1336
1337ClassTemplateSpecializationDecl *
1338ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1339 ClassTemplateDecl *class_template_decl,
1340 int kind,
1341 const TemplateParameterInfos &template_param_infos)
1342{
1343 ASTContext *ast = getASTContext();
1344 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1345 (TagDecl::TagKind)kind,
1346 decl_ctx,
1347 SourceLocation(),
1348 SourceLocation(),
1349 class_template_decl,
1350 &template_param_infos.args.front(),
1351 template_param_infos.args.size(),
1352 NULL);
1353
1354 return class_template_specialization_decl;
1355}
1356
1357lldb::clang_type_t
1358ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1359{
1360 if (class_template_specialization_decl)
1361 {
1362 ASTContext *ast = getASTContext();
1363 if (ast)
1364 return ast->getTagDeclType(class_template_specialization_decl).getAsOpaquePtr();
1365 }
1366 return NULL;
1367}
1368
Greg Clayton6beaaa62011-01-17 03:46:26 +00001369bool
1370ClangASTContext::SetHasExternalStorage (clang_type_t clang_type, bool has_extern)
1371{
1372 if (clang_type == NULL)
1373 return false;
1374
1375 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
1376
1377 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
1378 switch (type_class)
1379 {
1380 case clang::Type::Record:
1381 {
1382 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
1383 if (cxx_record_decl)
1384 {
1385 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001386 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001387 return true;
1388 }
1389 }
1390 break;
1391
1392 case clang::Type::Enum:
1393 {
1394 EnumDecl *enum_decl = cast<EnumType>(qual_type)->getDecl();
1395 if (enum_decl)
1396 {
1397 enum_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001398 enum_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001399 return true;
1400 }
1401 }
1402 break;
1403
1404 case clang::Type::ObjCObject:
1405 case clang::Type::ObjCInterface:
1406 {
Sean Callanan78e37602011-01-27 04:42:51 +00001407 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001408 assert (objc_class_type);
1409 if (objc_class_type)
1410 {
1411 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1412
1413 if (class_interface_decl)
1414 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001415 class_interface_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001416 class_interface_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001417 return true;
1418 }
1419 }
1420 }
1421 break;
1422
1423 case clang::Type::Typedef:
1424 return ClangASTContext::SetHasExternalStorage (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
Sean Callanan912855f2011-08-11 23:56:13 +00001425
1426 case clang::Type::Elaborated:
1427 return ClangASTContext::SetHasExternalStorage (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001428
1429 default:
1430 break;
1431 }
1432 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001433}
1434
Greg Claytona3c444a2010-10-01 23:13:49 +00001435static bool
1436IsOperator (const char *name, OverloadedOperatorKind &op_kind)
1437{
1438 if (name == NULL || name[0] == '\0')
1439 return false;
1440
Sean Callanana43f20d2010-12-10 19:51:54 +00001441#define OPERATOR_PREFIX "operator"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001442#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
Sean Callananbfeff8c2010-12-10 02:15:55 +00001443
1444 const char *post_op_name = NULL;
1445
Sean Callanana43f20d2010-12-10 19:51:54 +00001446 bool no_space = true;
Sean Callananbfeff8c2010-12-10 02:15:55 +00001447
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001448 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
Greg Claytona3c444a2010-10-01 23:13:49 +00001449 return false;
1450
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001451 post_op_name = name + OPERATOR_PREFIX_LENGTH;
1452
Sean Callanana43f20d2010-12-10 19:51:54 +00001453 if (post_op_name[0] == ' ')
1454 {
1455 post_op_name++;
1456 no_space = false;
1457 }
1458
1459#undef OPERATOR_PREFIX
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001460#undef OPERATOR_PREFIX_LENGTH
Sean Callanana43f20d2010-12-10 19:51:54 +00001461
Greg Claytona3c444a2010-10-01 23:13:49 +00001462 // This is an operator, set the overloaded operator kind to invalid
1463 // in case this is a conversion operator...
1464 op_kind = NUM_OVERLOADED_OPERATORS;
1465
1466 switch (post_op_name[0])
1467 {
Sean Callananbfeff8c2010-12-10 02:15:55 +00001468 default:
1469 if (no_space)
1470 return false;
1471 break;
Greg Claytona3c444a2010-10-01 23:13:49 +00001472 case 'n':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001473 if (no_space)
1474 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001475 if (strcmp (post_op_name, "new") == 0)
1476 op_kind = OO_New;
1477 else if (strcmp (post_op_name, "new[]") == 0)
1478 op_kind = OO_Array_New;
1479 break;
1480
1481 case 'd':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001482 if (no_space)
1483 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001484 if (strcmp (post_op_name, "delete") == 0)
1485 op_kind = OO_Delete;
1486 else if (strcmp (post_op_name, "delete[]") == 0)
1487 op_kind = OO_Array_Delete;
1488 break;
1489
1490 case '+':
1491 if (post_op_name[1] == '\0')
1492 op_kind = OO_Plus;
1493 else if (post_op_name[2] == '\0')
1494 {
1495 if (post_op_name[1] == '=')
1496 op_kind = OO_PlusEqual;
1497 else if (post_op_name[1] == '+')
1498 op_kind = OO_PlusPlus;
1499 }
1500 break;
1501
1502 case '-':
1503 if (post_op_name[1] == '\0')
1504 op_kind = OO_Minus;
1505 else if (post_op_name[2] == '\0')
1506 {
1507 switch (post_op_name[1])
1508 {
1509 case '=': op_kind = OO_MinusEqual; break;
1510 case '-': op_kind = OO_MinusMinus; break;
1511 case '>': op_kind = OO_Arrow; break;
1512 }
1513 }
1514 else if (post_op_name[3] == '\0')
1515 {
1516 if (post_op_name[2] == '*')
1517 op_kind = OO_ArrowStar; break;
1518 }
1519 break;
1520
1521 case '*':
1522 if (post_op_name[1] == '\0')
1523 op_kind = OO_Star;
1524 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1525 op_kind = OO_StarEqual;
1526 break;
1527
1528 case '/':
1529 if (post_op_name[1] == '\0')
1530 op_kind = OO_Slash;
1531 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1532 op_kind = OO_SlashEqual;
1533 break;
1534
1535 case '%':
1536 if (post_op_name[1] == '\0')
1537 op_kind = OO_Percent;
1538 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1539 op_kind = OO_PercentEqual;
1540 break;
1541
1542
1543 case '^':
1544 if (post_op_name[1] == '\0')
1545 op_kind = OO_Caret;
1546 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1547 op_kind = OO_CaretEqual;
1548 break;
1549
1550 case '&':
1551 if (post_op_name[1] == '\0')
1552 op_kind = OO_Amp;
1553 else if (post_op_name[2] == '\0')
1554 {
1555 switch (post_op_name[1])
1556 {
1557 case '=': op_kind = OO_AmpEqual; break;
1558 case '&': op_kind = OO_AmpAmp; break;
1559 }
1560 }
1561 break;
1562
1563 case '|':
1564 if (post_op_name[1] == '\0')
1565 op_kind = OO_Pipe;
1566 else if (post_op_name[2] == '\0')
1567 {
1568 switch (post_op_name[1])
1569 {
1570 case '=': op_kind = OO_PipeEqual; break;
1571 case '|': op_kind = OO_PipePipe; break;
1572 }
1573 }
1574 break;
1575
1576 case '~':
1577 if (post_op_name[1] == '\0')
1578 op_kind = OO_Tilde;
1579 break;
1580
1581 case '!':
1582 if (post_op_name[1] == '\0')
1583 op_kind = OO_Exclaim;
1584 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1585 op_kind = OO_ExclaimEqual;
1586 break;
1587
1588 case '=':
1589 if (post_op_name[1] == '\0')
1590 op_kind = OO_Equal;
1591 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1592 op_kind = OO_EqualEqual;
1593 break;
1594
1595 case '<':
1596 if (post_op_name[1] == '\0')
1597 op_kind = OO_Less;
1598 else if (post_op_name[2] == '\0')
1599 {
1600 switch (post_op_name[1])
1601 {
1602 case '<': op_kind = OO_LessLess; break;
1603 case '=': op_kind = OO_LessEqual; break;
1604 }
1605 }
1606 else if (post_op_name[3] == '\0')
1607 {
1608 if (post_op_name[2] == '=')
1609 op_kind = OO_LessLessEqual;
1610 }
1611 break;
1612
1613 case '>':
1614 if (post_op_name[1] == '\0')
1615 op_kind = OO_Greater;
1616 else if (post_op_name[2] == '\0')
1617 {
1618 switch (post_op_name[1])
1619 {
1620 case '>': op_kind = OO_GreaterGreater; break;
1621 case '=': op_kind = OO_GreaterEqual; break;
1622 }
1623 }
1624 else if (post_op_name[1] == '>' &&
1625 post_op_name[2] == '=' &&
1626 post_op_name[3] == '\0')
1627 {
1628 op_kind = OO_GreaterGreaterEqual;
1629 }
1630 break;
1631
1632 case ',':
1633 if (post_op_name[1] == '\0')
1634 op_kind = OO_Comma;
1635 break;
1636
1637 case '(':
1638 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
1639 op_kind = OO_Call;
1640 break;
1641
1642 case '[':
1643 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
1644 op_kind = OO_Subscript;
1645 break;
1646 }
1647
1648 return true;
1649}
Greg Clayton6beaaa62011-01-17 03:46:26 +00001650
Greg Clayton090d0982011-06-19 03:43:27 +00001651static inline bool
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001652check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
Greg Clayton090d0982011-06-19 03:43:27 +00001653{
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001654 // Special-case call since it can take any number of operands
1655 if(op_kind == OO_Call)
1656 return true;
1657
Greg Clayton090d0982011-06-19 03:43:27 +00001658 // The parameter count doens't include "this"
1659 if (num_params == 0)
1660 return unary;
1661 if (num_params == 1)
1662 return binary;
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001663 else
Greg Clayton090d0982011-06-19 03:43:27 +00001664 return false;
1665}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001666
Greg Clayton090d0982011-06-19 03:43:27 +00001667bool
1668ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1669{
Sean Callanan5b26f272012-02-04 08:49:35 +00001670 switch (op_kind)
1671 {
1672 default:
1673 break;
1674 // C++ standard allows any number of arguments to new/delete
1675 case OO_New:
1676 case OO_Array_New:
1677 case OO_Delete:
1678 case OO_Array_Delete:
1679 return true;
1680 }
1681
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001682#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 +00001683 switch (op_kind)
1684 {
1685#include "clang/Basic/OperatorKinds.def"
1686 default: break;
1687 }
1688 return false;
1689}
1690
Greg Claytona51ed9b2010-09-23 01:09:21 +00001691CXXMethodDecl *
Sean Callanan61da09b2010-09-17 02:58:26 +00001692ClangASTContext::AddMethodToCXXRecordType
1693(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001694 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001695 clang_type_t record_opaque_type,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001696 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001697 clang_type_t method_opaque_type,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001698 lldb::AccessType access,
Greg Clayton0fffff52010-09-24 05:15:53 +00001699 bool is_virtual,
1700 bool is_static,
Greg Claytonf51de672010-10-01 02:31:07 +00001701 bool is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00001702 bool is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00001703 bool is_attr_used,
1704 bool is_artificial
Greg Claytona51ed9b2010-09-23 01:09:21 +00001705)
Sean Callanan61da09b2010-09-17 02:58:26 +00001706{
Sean Callananfc55f5d2010-09-21 00:44:12 +00001707 if (!record_opaque_type || !method_opaque_type || !name)
Johnny Chend440bcc2010-09-28 16:10:54 +00001708 return NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001709
Greg Clayton6beaaa62011-01-17 03:46:26 +00001710 assert(ast);
Sean Callanan61da09b2010-09-17 02:58:26 +00001711
Greg Clayton6beaaa62011-01-17 03:46:26 +00001712 IdentifierTable *identifier_table = &ast->Idents;
Sean Callanan61da09b2010-09-17 02:58:26 +00001713
1714 assert(identifier_table);
1715
Sean Callananfc55f5d2010-09-21 00:44:12 +00001716 QualType record_qual_type(QualType::getFromOpaquePtr(record_opaque_type));
Greg Clayton0fffff52010-09-24 05:15:53 +00001717
Greg Clayton6beaaa62011-01-17 03:46:26 +00001718 CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
Sean Callanan61da09b2010-09-17 02:58:26 +00001719
Greg Clayton0fffff52010-09-24 05:15:53 +00001720 if (cxx_record_decl == NULL)
Greg Claytona51ed9b2010-09-23 01:09:21 +00001721 return NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001722
Greg Clayton0fffff52010-09-24 05:15:53 +00001723 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001724
Greg Claytonf51de672010-10-01 02:31:07 +00001725 CXXMethodDecl *cxx_method_decl = NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001726
Greg Claytonf51de672010-10-01 02:31:07 +00001727 DeclarationName decl_name (&identifier_table->get(name));
Greg Clayton878eaf12010-10-01 03:45:20 +00001728
Sean Callanan78e37602011-01-27 04:42:51 +00001729 const clang::FunctionType *function_Type = dyn_cast<FunctionType>(method_qual_type.getTypePtr());
Greg Clayton878eaf12010-10-01 03:45:20 +00001730
Greg Clayton90a2acd2010-10-02 01:40:05 +00001731 if (function_Type == NULL)
Greg Clayton878eaf12010-10-01 03:45:20 +00001732 return NULL;
1733
Sean Callanan78e37602011-01-27 04:42:51 +00001734 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(function_Type));
Greg Clayton878eaf12010-10-01 03:45:20 +00001735
1736 if (!method_function_prototype)
1737 return NULL;
1738
1739 unsigned int num_params = method_function_prototype->getNumArgs();
1740
Sean Callanandbb58392011-11-02 01:38:59 +00001741 CXXDestructorDecl *cxx_dtor_decl(NULL);
1742 CXXConstructorDecl *cxx_ctor_decl(NULL);
1743
Greg Clayton878eaf12010-10-01 03:45:20 +00001744 if (name[0] == '~')
Greg Claytonf51de672010-10-01 02:31:07 +00001745 {
Sean Callanandbb58392011-11-02 01:38:59 +00001746 cxx_dtor_decl = CXXDestructorDecl::Create (*ast,
1747 cxx_record_decl,
1748 SourceLocation(),
1749 DeclarationNameInfo (ast->DeclarationNames.getCXXDestructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1750 method_qual_type,
1751 NULL,
1752 is_inline,
1753 is_artificial);
1754 cxx_method_decl = cxx_dtor_decl;
Greg Clayton878eaf12010-10-01 03:45:20 +00001755 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001756 else if (decl_name == cxx_record_decl->getDeclName())
Greg Clayton878eaf12010-10-01 03:45:20 +00001757 {
Sean Callanandbb58392011-11-02 01:38:59 +00001758 cxx_ctor_decl = CXXConstructorDecl::Create (*ast,
1759 cxx_record_decl,
1760 SourceLocation(),
1761 DeclarationNameInfo (ast->DeclarationNames.getCXXConstructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1762 method_qual_type,
1763 NULL, // TypeSourceInfo *
1764 is_explicit,
1765 is_inline,
1766 is_artificial,
1767 false /*is_constexpr*/);
1768 cxx_method_decl = cxx_ctor_decl;
Greg Claytonf51de672010-10-01 02:31:07 +00001769 }
1770 else
Greg Clayton878eaf12010-10-01 03:45:20 +00001771 {
Greg Claytona3c444a2010-10-01 23:13:49 +00001772
1773 OverloadedOperatorKind op_kind = NUM_OVERLOADED_OPERATORS;
1774 if (IsOperator (name, op_kind))
Greg Clayton878eaf12010-10-01 03:45:20 +00001775 {
Greg Claytona3c444a2010-10-01 23:13:49 +00001776 if (op_kind != NUM_OVERLOADED_OPERATORS)
1777 {
Greg Clayton090d0982011-06-19 03:43:27 +00001778 // Check the number of operator parameters. Sometimes we have
1779 // seen bad DWARF that doesn't correctly describe operators and
1780 // if we try to create a methed and add it to the class, clang
1781 // will assert and crash, so we need to make sure things are
1782 // acceptable.
1783 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
1784 return NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001785 cxx_method_decl = CXXMethodDecl::Create (*ast,
Greg Clayton878eaf12010-10-01 03:45:20 +00001786 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001787 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001788 DeclarationNameInfo (ast->DeclarationNames.getCXXOperatorName (op_kind), SourceLocation()),
Greg Clayton878eaf12010-10-01 03:45:20 +00001789 method_qual_type,
1790 NULL, // TypeSourceInfo *
Greg Claytona3c444a2010-10-01 23:13:49 +00001791 is_static,
1792 SC_None,
Sean Callananfb0b7582011-03-15 00:17:19 +00001793 is_inline,
Sean Callanan880e6802011-10-07 23:18:13 +00001794 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001795 SourceLocation());
Greg Claytona3c444a2010-10-01 23:13:49 +00001796 }
1797 else if (num_params == 0)
1798 {
1799 // Conversion operators don't take params...
Greg Clayton6beaaa62011-01-17 03:46:26 +00001800 cxx_method_decl = CXXConversionDecl::Create (*ast,
Greg Claytona3c444a2010-10-01 23:13:49 +00001801 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001802 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001803 DeclarationNameInfo (ast->DeclarationNames.getCXXConversionFunctionName (ast->getCanonicalType (function_Type->getResultType())), SourceLocation()),
Greg Claytona3c444a2010-10-01 23:13:49 +00001804 method_qual_type,
1805 NULL, // TypeSourceInfo *
1806 is_inline,
Sean Callananfb0b7582011-03-15 00:17:19 +00001807 is_explicit,
Sean Callanan880e6802011-10-07 23:18:13 +00001808 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001809 SourceLocation());
Greg Claytona3c444a2010-10-01 23:13:49 +00001810 }
Greg Clayton878eaf12010-10-01 03:45:20 +00001811 }
Greg Claytona3c444a2010-10-01 23:13:49 +00001812
1813 if (cxx_method_decl == NULL)
Greg Clayton878eaf12010-10-01 03:45:20 +00001814 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001815 cxx_method_decl = CXXMethodDecl::Create (*ast,
Greg Clayton878eaf12010-10-01 03:45:20 +00001816 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001817 SourceLocation(),
Greg Claytona3c444a2010-10-01 23:13:49 +00001818 DeclarationNameInfo (decl_name, SourceLocation()),
Greg Clayton878eaf12010-10-01 03:45:20 +00001819 method_qual_type,
1820 NULL, // TypeSourceInfo *
1821 is_static,
1822 SC_None,
Sean Callananfb0b7582011-03-15 00:17:19 +00001823 is_inline,
Sean Callanan880e6802011-10-07 23:18:13 +00001824 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001825 SourceLocation());
Greg Clayton878eaf12010-10-01 03:45:20 +00001826 }
Greg Claytonf51de672010-10-01 02:31:07 +00001827 }
Greg Claytona3c444a2010-10-01 23:13:49 +00001828
Greg Clayton1be10fc2010-09-29 01:12:09 +00001829 AccessSpecifier access_specifier = ConvertAccessTypeToAccessSpecifier (access);
Greg Clayton0fffff52010-09-24 05:15:53 +00001830
1831 cxx_method_decl->setAccess (access_specifier);
1832 cxx_method_decl->setVirtualAsWritten (is_virtual);
Sean Callanane2ef6e32010-09-23 03:01:22 +00001833
Sean Callananc1b732d2011-11-01 18:07:13 +00001834 if (is_attr_used)
1835 cxx_method_decl->addAttr(::new (*ast) UsedAttr(SourceRange(), *ast));
1836
Sean Callananfc55f5d2010-09-21 00:44:12 +00001837 // Populate the method decl with parameter decls
Sean Callananfc55f5d2010-09-21 00:44:12 +00001838
Charles Davis8c444c42011-05-19 23:33:46 +00001839 llvm::SmallVector<ParmVarDecl *, 12> params;
Sean Callananfc55f5d2010-09-21 00:44:12 +00001840
1841 for (int param_index = 0;
1842 param_index < num_params;
1843 ++param_index)
1844 {
Charles Davis8c444c42011-05-19 23:33:46 +00001845 params.push_back (ParmVarDecl::Create (*ast,
1846 cxx_method_decl,
1847 SourceLocation(),
1848 SourceLocation(),
1849 NULL, // anonymous
1850 method_function_prototype->getArgType(param_index),
1851 NULL,
1852 SC_None,
1853 SC_None,
1854 NULL));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001855 }
1856
Sean Callanan880e6802011-10-07 23:18:13 +00001857 cxx_method_decl->setParams (ArrayRef<ParmVarDecl*>(params));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001858
Greg Clayton0fffff52010-09-24 05:15:53 +00001859 cxx_record_decl->addDecl (cxx_method_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001860
Greg Clayton8b867b42011-11-02 02:06:20 +00001861 // Sometimes the debug info will mention a constructor (default/copy/move),
1862 // destructor, or assignment operator (copy/move) but there won't be any
1863 // version of this in the code. So we check if the function was artificially
1864 // generated and if it is trivial and this lets the compiler/backend know
1865 // that it can inline the IR for these when it needs to and we can avoid a
1866 // "missing function" error when running expressions.
1867
Sean Callanandbb58392011-11-02 01:38:59 +00001868 if (is_artificial)
1869 {
Greg Clayton8b867b42011-11-02 02:06:20 +00001870 if (cxx_ctor_decl &&
1871 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
1872 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
1873 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
Sean Callanandbb58392011-11-02 01:38:59 +00001874 {
1875 cxx_ctor_decl->setDefaulted();
1876 cxx_ctor_decl->setTrivial(true);
1877 }
Greg Clayton8b867b42011-11-02 02:06:20 +00001878 else if (cxx_dtor_decl)
Sean Callanandbb58392011-11-02 01:38:59 +00001879 {
Greg Clayton8b867b42011-11-02 02:06:20 +00001880 if (cxx_record_decl->hasTrivialDestructor())
1881 {
1882 cxx_dtor_decl->setDefaulted();
1883 cxx_dtor_decl->setTrivial(true);
1884 }
1885 }
1886 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
1887 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
1888 {
1889 cxx_method_decl->setDefaulted();
1890 cxx_method_decl->setTrivial(true);
Sean Callanandbb58392011-11-02 01:38:59 +00001891 }
1892 }
1893
Sean Callanan5e9e1992011-10-26 01:06:27 +00001894#ifdef LLDB_CONFIGURATION_DEBUG
1895 VerifyDecl(cxx_method_decl);
1896#endif
Greg Claytonc432c192011-01-20 04:18:48 +00001897
1898// printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
1899// printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
1900// printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
1901// printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
1902// printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
1903// printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
1904// printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
1905// printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
1906// printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
Greg Claytona51ed9b2010-09-23 01:09:21 +00001907 return cxx_method_decl;
Sean Callanan61da09b2010-09-17 02:58:26 +00001908}
1909
Jim Inghame3ae82a2011-11-12 01:36:43 +00001910clang::FieldDecl *
Greg Clayton8cf05932010-07-22 18:30:50 +00001911ClangASTContext::AddFieldToRecordType
1912(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001913 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001914 clang_type_t record_clang_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00001915 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001916 clang_type_t field_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00001917 AccessType access,
1918 uint32_t bitfield_bit_size
1919)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001920{
1921 if (record_clang_type == NULL || field_type == NULL)
Jim Inghame3ae82a2011-11-12 01:36:43 +00001922 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001923
Jim Inghame3ae82a2011-11-12 01:36:43 +00001924 FieldDecl *field = NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001925 IdentifierTable *identifier_table = &ast->Idents;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001926
Greg Clayton6beaaa62011-01-17 03:46:26 +00001927 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001928 assert (identifier_table != NULL);
1929
1930 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
1931
Sean Callanan78e37602011-01-27 04:42:51 +00001932 const clang::Type *clang_type = record_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001933 if (clang_type)
1934 {
1935 const RecordType *record_type = dyn_cast<RecordType>(clang_type);
1936
1937 if (record_type)
1938 {
1939 RecordDecl *record_decl = record_type->getDecl();
1940
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001941 clang::Expr *bit_width = NULL;
1942 if (bitfield_bit_size != 0)
1943 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001944 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
1945 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001946 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001947 field = FieldDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00001948 record_decl,
1949 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00001950 SourceLocation(),
Greg Clayton8cf05932010-07-22 18:30:50 +00001951 name ? &identifier_table->get(name) : NULL, // Identifier
1952 QualType::getFromOpaquePtr(field_type), // Field type
Sean Callanancc427fa2011-07-30 02:42:06 +00001953 NULL, // TInfo *
Greg Clayton8cf05932010-07-22 18:30:50 +00001954 bit_width, // BitWidth
Sean Callanancc427fa2011-07-30 02:42:06 +00001955 false, // Mutable
1956 false); // HasInit
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001957
1958 if (!name)
1959 field->setImplicit();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001960
Greg Clayton8cf05932010-07-22 18:30:50 +00001961 field->setAccess (ConvertAccessTypeToAccessSpecifier (access));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001962
1963 if (field)
1964 {
1965 record_decl->addDecl(field);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001966
1967#ifdef LLDB_CONFIGURATION_DEBUG
1968 VerifyDecl(field);
1969#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001970 }
1971 }
Greg Clayton9e409562010-07-28 02:04:09 +00001972 else
1973 {
Sean Callanan78e37602011-01-27 04:42:51 +00001974 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001975 if (objc_class_type)
1976 {
Greg Clayton0fffff52010-09-24 05:15:53 +00001977 bool is_synthesized = false;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001978 field = ClangASTContext::AddObjCClassIVar (ast,
Sean Callanan6e6a7c72010-09-16 20:01:08 +00001979 record_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001980 name,
1981 field_type,
1982 access,
1983 bitfield_bit_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00001984 is_synthesized);
Greg Clayton9e409562010-07-28 02:04:09 +00001985 }
1986 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001987 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001988 return field;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001989}
1990
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001991static clang::AccessSpecifier UnifyAccessSpecifiers (clang::AccessSpecifier lhs,
1992 clang::AccessSpecifier rhs)
1993{
1994 clang::AccessSpecifier ret = lhs;
1995
1996 // Make the access equal to the stricter of the field and the nested field's access
1997 switch (ret)
1998 {
1999 case clang::AS_none:
2000 break;
2001 case clang::AS_private:
2002 break;
2003 case clang::AS_protected:
2004 if (rhs == AS_private)
2005 ret = AS_private;
2006 break;
2007 case clang::AS_public:
2008 ret = rhs;
2009 break;
2010 }
2011
2012 return ret;
2013}
2014
2015void
2016ClangASTContext::BuildIndirectFields (clang::ASTContext *ast,
2017 lldb::clang_type_t record_clang_type)
2018{
2019 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
2020
2021 const RecordType *record_type = record_qual_type->getAs<RecordType>();
2022
2023 if (!record_type)
2024 return;
2025
2026 RecordDecl *record_decl = record_type->getDecl();
2027
2028 if (!record_decl)
2029 return;
2030
2031 typedef llvm::SmallVector <IndirectFieldDecl *, 1> IndirectFieldVector;
2032
2033 IndirectFieldVector indirect_fields;
2034
2035 for (RecordDecl::field_iterator fi = record_decl->field_begin(), fe = record_decl->field_end();
2036 fi != fe;
2037 ++fi)
2038 {
2039 if (fi->isAnonymousStructOrUnion())
2040 {
2041 QualType field_qual_type = fi->getType();
2042
2043 const RecordType *field_record_type = field_qual_type->getAs<RecordType>();
2044
2045 if (!field_record_type)
2046 continue;
2047
2048 RecordDecl *field_record_decl = field_record_type->getDecl();
2049
2050 if (!field_record_decl)
2051 continue;
2052
2053 for (RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
2054 di != de;
2055 ++di)
2056 {
2057 if (FieldDecl *nested_field_decl = dyn_cast<FieldDecl>(*di))
2058 {
2059 NamedDecl **chain = new (*ast) NamedDecl*[2];
2060 chain[0] = *fi;
2061 chain[1] = nested_field_decl;
2062 IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*ast,
2063 record_decl,
2064 SourceLocation(),
2065 nested_field_decl->getIdentifier(),
2066 nested_field_decl->getType(),
2067 chain,
2068 2);
2069
2070 indirect_field->setAccess(UnifyAccessSpecifiers(fi->getAccess(),
2071 nested_field_decl->getAccess()));
2072
2073 indirect_fields.push_back(indirect_field);
2074 }
2075 else if (IndirectFieldDecl *nested_indirect_field_decl = dyn_cast<IndirectFieldDecl>(*di))
2076 {
2077 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
2078 NamedDecl **chain = new (*ast) NamedDecl*[nested_chain_size + 1];
2079 chain[0] = *fi;
2080
2081 int chain_index = 1;
2082 for (IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
2083 nce = nested_indirect_field_decl->chain_end();
2084 nci < nce;
2085 ++nci)
2086 {
2087 chain[chain_index] = *nci;
2088 chain_index++;
2089 }
2090
2091 IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*ast,
2092 record_decl,
2093 SourceLocation(),
2094 nested_indirect_field_decl->getIdentifier(),
2095 nested_indirect_field_decl->getType(),
2096 chain,
2097 nested_chain_size + 1);
2098
2099 indirect_field->setAccess(UnifyAccessSpecifiers(fi->getAccess(),
2100 nested_indirect_field_decl->getAccess()));
2101
2102 indirect_fields.push_back(indirect_field);
2103 }
2104 }
2105 }
2106 }
2107
2108 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
2109 ifi < ife;
2110 ++ifi)
2111 {
2112 record_decl->addDecl(*ifi);
2113 }
2114}
2115
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002116bool
2117ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
2118{
2119 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
2120}
2121
2122bool
2123ClangASTContext::FieldIsBitfield
2124(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002125 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002126 FieldDecl* field,
2127 uint32_t& bitfield_bit_size
2128)
2129{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002130 if (ast == NULL || field == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002131 return false;
2132
2133 if (field->isBitField())
2134 {
2135 Expr* bit_width_expr = field->getBitWidth();
2136 if (bit_width_expr)
2137 {
2138 llvm::APSInt bit_width_apsint;
Greg Clayton6beaaa62011-01-17 03:46:26 +00002139 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002140 {
2141 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
2142 return true;
2143 }
2144 }
2145 }
2146 return false;
2147}
2148
2149bool
2150ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
2151{
2152 if (record_decl == NULL)
2153 return false;
2154
2155 if (!record_decl->field_empty())
2156 return true;
2157
2158 // No fields, lets check this is a CXX record and check the base classes
2159 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2160 if (cxx_record_decl)
2161 {
2162 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2163 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2164 base_class != base_class_end;
2165 ++base_class)
2166 {
2167 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2168 if (RecordHasFields(base_class_decl))
2169 return true;
2170 }
2171 }
2172 return false;
2173}
2174
2175void
Greg Clayton6beaaa62011-01-17 03:46:26 +00002176ClangASTContext::SetDefaultAccessForRecordFields (clang_type_t clang_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002177{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002178 if (clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002179 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002180 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
2181
Sean Callanan78e37602011-01-27 04:42:51 +00002182 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +00002183 if (record_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002184 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002185 RecordDecl *record_decl = record_type->getDecl();
2186 if (record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002187 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002188 uint32_t field_idx;
2189 RecordDecl::field_iterator field, field_end;
2190 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2191 field != field_end;
2192 ++field, ++field_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002193 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002194 // If no accessibility was assigned, assign the correct one
2195 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2196 field->setAccess ((AccessSpecifier)default_accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002197 }
2198 }
2199 }
2200 }
2201}
2202
2203#pragma mark C++ Base Classes
2204
2205CXXBaseSpecifier *
Greg Clayton1be10fc2010-09-29 01:12:09 +00002206ClangASTContext::CreateBaseClassSpecifier (clang_type_t base_class_type, AccessType access, bool is_virtual, bool base_of_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002207{
2208 if (base_class_type)
Greg Claytone6371122010-07-30 20:30:44 +00002209 return new CXXBaseSpecifier (SourceRange(),
2210 is_virtual,
2211 base_of_class,
2212 ConvertAccessTypeToAccessSpecifier (access),
Sean Callanan2c777c42011-01-18 23:32:05 +00002213 getASTContext()->CreateTypeSourceInfo (QualType::getFromOpaquePtr(base_class_type)),
2214 SourceLocation());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002215 return NULL;
2216}
2217
Greg Clayton0b42ac32010-07-02 01:29:13 +00002218void
2219ClangASTContext::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes)
2220{
2221 for (unsigned i=0; i<num_base_classes; ++i)
2222 {
2223 delete base_classes[i];
2224 base_classes[i] = NULL;
2225 }
2226}
2227
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002228bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002229ClangASTContext::SetBaseClassesForClassType (clang_type_t class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002230{
2231 if (class_clang_type)
2232 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002233 CXXRecordDecl *cxx_record_decl = QualType::getFromOpaquePtr(class_clang_type)->getAsCXXRecordDecl();
2234 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002235 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002236 cxx_record_decl->setBases(base_classes, num_base_classes);
2237 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002238 }
2239 }
2240 return false;
2241}
Greg Clayton8cf05932010-07-22 18:30:50 +00002242#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002243
Greg Clayton1be10fc2010-09-29 01:12:09 +00002244clang_type_t
Greg Clayton8cf05932010-07-22 18:30:50 +00002245ClangASTContext::CreateObjCClass
2246(
2247 const char *name,
2248 DeclContext *decl_ctx,
2249 bool isForwardDecl,
2250 bool isInternal
2251)
2252{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002253 ASTContext *ast = getASTContext();
2254 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00002255 assert (name && name[0]);
2256 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00002257 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00002258
2259 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
2260 // we will need to update this code. I was told to currently always use
2261 // the CXXRecordDecl class since we often don't know from debug information
2262 // if something is struct or a class, so we default to always use the more
2263 // complete definition just in case.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002264 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00002265 decl_ctx,
2266 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00002267 &ast->Idents.get(name),
Sean Callanan5b26f272012-02-04 08:49:35 +00002268 NULL,
Greg Clayton8cf05932010-07-22 18:30:50 +00002269 SourceLocation(),
Sean Callanan5b26f272012-02-04 08:49:35 +00002270 /*isForwardDecl,*/
Greg Clayton8cf05932010-07-22 18:30:50 +00002271 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00002272
Greg Clayton6beaaa62011-01-17 03:46:26 +00002273 return ast->getObjCInterfaceType(decl).getAsOpaquePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002274}
2275
2276bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002277ClangASTContext::SetObjCSuperClass (clang_type_t class_opaque_type, clang_type_t super_opaque_type)
Greg Clayton8cf05932010-07-22 18:30:50 +00002278{
2279 if (class_opaque_type && super_opaque_type)
2280 {
2281 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2282 QualType super_qual_type(QualType::getFromOpaquePtr(super_opaque_type));
Sean Callanan78e37602011-01-27 04:42:51 +00002283 const clang::Type *class_type = class_qual_type.getTypePtr();
2284 const clang::Type *super_type = super_qual_type.getTypePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002285 if (class_type && super_type)
2286 {
Sean Callanan78e37602011-01-27 04:42:51 +00002287 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
2288 const ObjCObjectType *objc_super_type = dyn_cast<ObjCObjectType>(super_type);
Greg Clayton8cf05932010-07-22 18:30:50 +00002289 if (objc_class_type && objc_super_type)
2290 {
2291 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2292 ObjCInterfaceDecl *super_interface_decl = objc_super_type->getInterface();
2293 if (class_interface_decl && super_interface_decl)
2294 {
2295 class_interface_decl->setSuperClass(super_interface_decl);
2296 return true;
2297 }
2298 }
2299 }
2300 }
2301 return false;
2302}
2303
2304
Jim Inghame3ae82a2011-11-12 01:36:43 +00002305FieldDecl *
Greg Clayton8cf05932010-07-22 18:30:50 +00002306ClangASTContext::AddObjCClassIVar
2307(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002308 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002309 clang_type_t class_opaque_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00002310 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002311 clang_type_t ivar_opaque_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00002312 AccessType access,
2313 uint32_t bitfield_bit_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00002314 bool is_synthesized
Greg Clayton8cf05932010-07-22 18:30:50 +00002315)
2316{
2317 if (class_opaque_type == NULL || ivar_opaque_type == NULL)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002318 return NULL;
Greg Clayton8cf05932010-07-22 18:30:50 +00002319
Jim Inghame3ae82a2011-11-12 01:36:43 +00002320 ObjCIvarDecl *field = NULL;
2321
Greg Clayton6beaaa62011-01-17 03:46:26 +00002322 IdentifierTable *identifier_table = &ast->Idents;
Greg Clayton8cf05932010-07-22 18:30:50 +00002323
Greg Clayton6beaaa62011-01-17 03:46:26 +00002324 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00002325 assert (identifier_table != NULL);
2326
2327 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2328
Sean Callanan78e37602011-01-27 04:42:51 +00002329 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002330 if (class_type)
2331 {
Sean Callanan78e37602011-01-27 04:42:51 +00002332 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton8cf05932010-07-22 18:30:50 +00002333
2334 if (objc_class_type)
2335 {
2336 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2337
2338 if (class_interface_decl)
2339 {
2340 clang::Expr *bit_width = NULL;
2341 if (bitfield_bit_size != 0)
2342 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002343 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
2344 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
Greg Clayton8cf05932010-07-22 18:30:50 +00002345 }
2346
Jim Inghame3ae82a2011-11-12 01:36:43 +00002347 field = ObjCIvarDecl::Create (*ast,
2348 class_interface_decl,
2349 SourceLocation(),
2350 SourceLocation(),
2351 &identifier_table->get(name), // Identifier
2352 QualType::getFromOpaquePtr(ivar_opaque_type), // Field type
2353 NULL, // TypeSourceInfo *
2354 ConvertAccessTypeToObjCIvarAccessControl (access),
2355 bit_width,
2356 is_synthesized);
Greg Clayton9e409562010-07-28 02:04:09 +00002357
2358 if (field)
2359 {
2360 class_interface_decl->addDecl(field);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002361
2362#ifdef LLDB_CONFIGURATION_DEBUG
2363 VerifyDecl(field);
2364#endif
2365
Jim Inghame3ae82a2011-11-12 01:36:43 +00002366 return field;
Greg Clayton9e409562010-07-28 02:04:09 +00002367 }
Greg Clayton8cf05932010-07-22 18:30:50 +00002368 }
2369 }
2370 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002371 return NULL;
2372}
2373
2374bool
2375ClangASTContext::AddObjCClassProperty
2376(
2377 ASTContext *ast,
2378 clang_type_t class_opaque_type,
2379 const char *property_name,
2380 clang_type_t property_opaque_type,
2381 ObjCIvarDecl *ivar_decl,
2382 const char *property_setter_name,
2383 const char *property_getter_name,
2384 uint32_t property_attributes
2385)
2386{
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002387 if (class_opaque_type == NULL || property_name == NULL || property_name[0] == '\0')
Jim Inghame3ae82a2011-11-12 01:36:43 +00002388 return false;
2389
2390 IdentifierTable *identifier_table = &ast->Idents;
2391
2392 assert (ast != NULL);
2393 assert (identifier_table != NULL);
2394
2395 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2396 const clang::Type *class_type = class_qual_type.getTypePtr();
2397 if (class_type)
2398 {
2399 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
2400
2401 if (objc_class_type)
2402 {
2403 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2404
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002405 clang_type_t property_opaque_type_to_access;
2406
2407 if (property_opaque_type)
2408 property_opaque_type_to_access = property_opaque_type;
2409 else if (ivar_decl)
2410 property_opaque_type_to_access = ivar_decl->getType().getAsOpaquePtr();
2411
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002412 if (class_interface_decl && property_opaque_type_to_access)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002413 {
2414 clang::TypeSourceInfo *prop_type_source;
2415 if (ivar_decl)
2416 prop_type_source = ast->CreateTypeSourceInfo (ivar_decl->getType());
2417 else
2418 prop_type_source = ast->CreateTypeSourceInfo (QualType::getFromOpaquePtr(property_opaque_type));
2419
2420 ObjCPropertyDecl *property_decl = ObjCPropertyDecl::Create(*ast,
2421 class_interface_decl,
2422 SourceLocation(), // Source Location
2423 &identifier_table->get(property_name),
2424 SourceLocation(), //Source Location for AT
Sean Callanand5f33a82012-03-01 02:03:47 +00002425 SourceLocation(), //Source location for (
Jim Inghame3ae82a2011-11-12 01:36:43 +00002426 prop_type_source
2427 );
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002428 if (property_decl)
2429 {
Jim Inghame3ae82a2011-11-12 01:36:43 +00002430 class_interface_decl->addDecl (property_decl);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002431
2432 Selector setter_sel, getter_sel;
2433
Jim Inghame3ae82a2011-11-12 01:36:43 +00002434 if (property_setter_name != NULL)
2435 {
2436 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
2437 clang::IdentifierInfo *setter_ident = &identifier_table->get(property_setter_no_colon.c_str());
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002438 setter_sel = ast->Selectors.getSelector(1, &setter_ident);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002439 }
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002440 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
2441 {
2442 std::string setter_sel_string("set");
2443 setter_sel_string.push_back(::toupper(property_name[0]));
2444 setter_sel_string.append(&property_name[1]);
2445 clang::IdentifierInfo *setter_ident = &identifier_table->get(setter_sel_string.c_str());
2446 setter_sel = ast->Selectors.getSelector(1, &setter_ident);
2447 }
Sean Callanana6582262012-04-05 00:12:52 +00002448 property_decl->setSetterName(setter_sel);
2449 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002450
2451 if (property_getter_name != NULL)
2452 {
2453 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_getter_name);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002454 getter_sel = ast->Selectors.getSelector(0, &getter_ident);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002455 }
2456 else
2457 {
2458 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_name);
2459 getter_sel = ast->Selectors.getSelector(0, &getter_ident);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002460 }
Sean Callanana6582262012-04-05 00:12:52 +00002461 property_decl->setGetterName(getter_sel);
2462 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002463
2464 if (ivar_decl)
2465 property_decl->setPropertyIvarDecl (ivar_decl);
2466
2467 if (property_attributes & DW_APPLE_PROPERTY_readonly)
2468 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
2469 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
2470 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
2471 if (property_attributes & DW_APPLE_PROPERTY_assign)
2472 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
2473 if (property_attributes & DW_APPLE_PROPERTY_retain)
2474 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
2475 if (property_attributes & DW_APPLE_PROPERTY_copy)
2476 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
2477 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
2478 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002479
2480 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
2481 {
2482 QualType result_type = QualType::getFromOpaquePtr(property_opaque_type_to_access);
2483
2484 const bool isInstance = true;
2485 const bool isVariadic = false;
2486 const bool isSynthesized = false;
2487 const bool isImplicitlyDeclared = true;
2488 const bool isDefined = false;
2489 const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
2490 const bool HasRelatedResultType = false;
2491
2492 ObjCMethodDecl *getter = ObjCMethodDecl::Create(*ast,
2493 SourceLocation(),
2494 SourceLocation(),
2495 getter_sel,
2496 result_type,
2497 NULL,
2498 class_interface_decl,
2499 isInstance,
2500 isVariadic,
2501 isSynthesized,
2502 isImplicitlyDeclared,
2503 isDefined,
2504 impControl,
2505 HasRelatedResultType);
2506
2507 getter->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(), ArrayRef<SourceLocation>());
2508
2509 class_interface_decl->addDecl(getter);
2510 }
2511
2512 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
2513 {
2514 QualType result_type = ast->VoidTy;
2515
2516 const bool isInstance = true;
2517 const bool isVariadic = false;
2518 const bool isSynthesized = false;
2519 const bool isImplicitlyDeclared = true;
2520 const bool isDefined = false;
2521 const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
2522 const bool HasRelatedResultType = false;
2523
2524 ObjCMethodDecl *setter = ObjCMethodDecl::Create(*ast,
2525 SourceLocation(),
2526 SourceLocation(),
2527 setter_sel,
2528 result_type,
2529 NULL,
2530 class_interface_decl,
2531 isInstance,
2532 isVariadic,
2533 isSynthesized,
2534 isImplicitlyDeclared,
2535 isDefined,
2536 impControl,
2537 HasRelatedResultType);
2538
2539 llvm::SmallVector<ParmVarDecl *, 1> params;
2540
2541 params.push_back (ParmVarDecl::Create (*ast,
2542 setter,
2543 SourceLocation(),
2544 SourceLocation(),
2545 NULL, // anonymous
2546 QualType::getFromOpaquePtr(property_opaque_type_to_access),
2547 NULL,
2548 SC_Auto,
2549 SC_Auto,
2550 NULL));
2551
2552 setter->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
2553
2554 class_interface_decl->addDecl(setter);
2555 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002556
2557 return true;
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002558 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002559 }
2560 }
2561 }
Greg Clayton8cf05932010-07-22 18:30:50 +00002562 return false;
2563}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002564
Greg Clayton9e409562010-07-28 02:04:09 +00002565bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002566ClangASTContext::ObjCTypeHasIVars (clang_type_t class_opaque_type, bool check_superclass)
Greg Clayton9e409562010-07-28 02:04:09 +00002567{
2568 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2569
Sean Callanan78e37602011-01-27 04:42:51 +00002570 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton9e409562010-07-28 02:04:09 +00002571 if (class_type)
2572 {
Sean Callanan78e37602011-01-27 04:42:51 +00002573 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00002574
2575 if (objc_class_type)
2576 return ObjCDeclHasIVars (objc_class_type->getInterface(), check_superclass);
2577 }
2578 return false;
2579}
2580
2581bool
2582ClangASTContext::ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
2583{
2584 while (class_interface_decl)
2585 {
2586 if (class_interface_decl->ivar_size() > 0)
2587 return true;
2588
2589 if (check_superclass)
2590 class_interface_decl = class_interface_decl->getSuperClass();
2591 else
2592 break;
2593 }
2594 return false;
2595}
Greg Clayton0fffff52010-09-24 05:15:53 +00002596
Greg Clayton1be10fc2010-09-29 01:12:09 +00002597ObjCMethodDecl *
Greg Clayton0fffff52010-09-24 05:15:53 +00002598ClangASTContext::AddMethodToObjCObjectType
2599(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002600 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002601 clang_type_t class_opaque_type,
Greg Clayton0fffff52010-09-24 05:15:53 +00002602 const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
Greg Clayton1be10fc2010-09-29 01:12:09 +00002603 clang_type_t method_opaque_type,
Greg Clayton0fffff52010-09-24 05:15:53 +00002604 lldb::AccessType access
2605)
2606{
2607 if (class_opaque_type == NULL || method_opaque_type == NULL)
2608 return NULL;
2609
Greg Clayton6beaaa62011-01-17 03:46:26 +00002610 IdentifierTable *identifier_table = &ast->Idents;
Greg Clayton0fffff52010-09-24 05:15:53 +00002611
Greg Clayton6beaaa62011-01-17 03:46:26 +00002612 assert (ast != NULL);
Greg Clayton0fffff52010-09-24 05:15:53 +00002613 assert (identifier_table != NULL);
2614
2615 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2616
Sean Callanan78e37602011-01-27 04:42:51 +00002617 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton0fffff52010-09-24 05:15:53 +00002618 if (class_type == NULL)
2619 return NULL;
2620
Sean Callanan78e37602011-01-27 04:42:51 +00002621 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton0fffff52010-09-24 05:15:53 +00002622
2623 if (objc_class_type == NULL)
2624 return NULL;
2625
2626 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2627
2628 if (class_interface_decl == NULL)
2629 return NULL;
Greg Clayton9e409562010-07-28 02:04:09 +00002630
Greg Clayton0fffff52010-09-24 05:15:53 +00002631 const char *selector_start = ::strchr (name, ' ');
2632 if (selector_start == NULL)
2633 return NULL;
2634
2635 selector_start++;
2636 if (!(::isalpha (selector_start[0]) || selector_start[0] == '_'))
2637 return NULL;
2638 llvm::SmallVector<IdentifierInfo *, 12> selector_idents;
2639
Greg Clayton450e3f32010-10-12 02:24:53 +00002640 size_t len = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +00002641 const char *start;
Greg Clayton450e3f32010-10-12 02:24:53 +00002642 //printf ("name = '%s'\n", name);
2643
2644 unsigned num_selectors_with_args = 0;
2645 for (start = selector_start;
Greg Clayton0fffff52010-09-24 05:15:53 +00002646 start && *start != '\0' && *start != ']';
Greg Clayton450e3f32010-10-12 02:24:53 +00002647 start += len)
Greg Clayton0fffff52010-09-24 05:15:53 +00002648 {
Greg Clayton450e3f32010-10-12 02:24:53 +00002649 len = ::strcspn(start, ":]");
Greg Clayton90f90cd2010-10-27 04:01:14 +00002650 bool has_arg = (start[len] == ':');
2651 if (has_arg)
Greg Clayton450e3f32010-10-12 02:24:53 +00002652 ++num_selectors_with_args;
Greg Clayton0fffff52010-09-24 05:15:53 +00002653 selector_idents.push_back (&identifier_table->get (StringRef (start, len)));
Greg Clayton90f90cd2010-10-27 04:01:14 +00002654 if (has_arg)
2655 len += 1;
Greg Clayton0fffff52010-09-24 05:15:53 +00002656 }
2657
2658
2659 if (selector_idents.size() == 0)
2660 return 0;
2661
Greg Clayton6beaaa62011-01-17 03:46:26 +00002662 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
Greg Clayton0fffff52010-09-24 05:15:53 +00002663 selector_idents.data());
2664
2665 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
2666
2667 // Populate the method decl with parameter decls
Sean Callanan78e37602011-01-27 04:42:51 +00002668 const clang::Type *method_type(method_qual_type.getTypePtr());
Greg Clayton0fffff52010-09-24 05:15:53 +00002669
2670 if (method_type == NULL)
2671 return NULL;
2672
Sean Callanan78e37602011-01-27 04:42:51 +00002673 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type));
Greg Clayton0fffff52010-09-24 05:15:53 +00002674
2675 if (!method_function_prototype)
2676 return NULL;
2677
2678
2679 bool is_variadic = false;
2680 bool is_synthesized = false;
2681 bool is_defined = false;
2682 ObjCMethodDecl::ImplementationControl imp_control = ObjCMethodDecl::None;
2683
2684 const unsigned num_args = method_function_prototype->getNumArgs();
2685
Greg Clayton6beaaa62011-01-17 03:46:26 +00002686 ObjCMethodDecl *objc_method_decl = ObjCMethodDecl::Create (*ast,
Greg Clayton0fffff52010-09-24 05:15:53 +00002687 SourceLocation(), // beginLoc,
2688 SourceLocation(), // endLoc,
2689 method_selector,
2690 method_function_prototype->getResultType(),
2691 NULL, // TypeSourceInfo *ResultTInfo,
2692 GetDeclContextForType (class_opaque_type),
2693 name[0] == '-',
2694 is_variadic,
2695 is_synthesized,
Sean Callanan880e6802011-10-07 23:18:13 +00002696 true, // is_implicitly_declared
Greg Clayton0fffff52010-09-24 05:15:53 +00002697 is_defined,
2698 imp_control,
Sean Callanan880e6802011-10-07 23:18:13 +00002699 false /*has_related_result_type*/);
Greg Clayton0fffff52010-09-24 05:15:53 +00002700
2701
2702 if (objc_method_decl == NULL)
2703 return NULL;
2704
2705 if (num_args > 0)
2706 {
2707 llvm::SmallVector<ParmVarDecl *, 12> params;
2708
2709 for (int param_index = 0; param_index < num_args; ++param_index)
2710 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002711 params.push_back (ParmVarDecl::Create (*ast,
Greg Clayton0fffff52010-09-24 05:15:53 +00002712 objc_method_decl,
2713 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00002714 SourceLocation(),
Greg Clayton0fffff52010-09-24 05:15:53 +00002715 NULL, // anonymous
2716 method_function_prototype->getArgType(param_index),
2717 NULL,
2718 SC_Auto,
2719 SC_Auto,
2720 NULL));
2721 }
2722
Sean Callanan880e6802011-10-07 23:18:13 +00002723 objc_method_decl->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
Greg Clayton0fffff52010-09-24 05:15:53 +00002724 }
2725
2726 class_interface_decl->addDecl (objc_method_decl);
2727
Sean Callanan5e9e1992011-10-26 01:06:27 +00002728#ifdef LLDB_CONFIGURATION_DEBUG
2729 VerifyDecl(objc_method_decl);
2730#endif
Greg Clayton0fffff52010-09-24 05:15:53 +00002731
2732 return objc_method_decl;
2733}
2734
Greg Clayton402230e2012-02-03 01:30:30 +00002735size_t
2736ClangASTContext::GetNumTemplateArguments (clang::ASTContext *ast, clang_type_t clang_type)
2737{
2738 if (clang_type)
2739 {
2740 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2741
2742 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2743 switch (type_class)
2744 {
2745 case clang::Type::Record:
2746 if (GetCompleteQualType (ast, qual_type))
2747 {
2748 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2749 if (cxx_record_decl)
2750 {
2751 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2752 if (template_decl)
2753 return template_decl->getTemplateArgs().size();
2754 }
2755 }
2756 break;
2757
2758 case clang::Type::Typedef:
2759 return ClangASTContext::GetNumTemplateArguments (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2760 default:
2761 break;
2762 }
2763 }
2764 return 0;
2765}
2766
2767clang_type_t
2768ClangASTContext::GetTemplateArgument (clang::ASTContext *ast, clang_type_t clang_type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
2769{
2770 if (clang_type)
2771 {
2772 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2773
2774 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2775 switch (type_class)
2776 {
2777 case clang::Type::Record:
2778 if (GetCompleteQualType (ast, qual_type))
2779 {
2780 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2781 if (cxx_record_decl)
2782 {
2783 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2784 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
2785 {
2786 const TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
2787 switch (template_arg.getKind())
2788 {
2789 case clang::TemplateArgument::Null:
2790 kind = eTemplateArgumentKindNull;
2791 return NULL;
2792
2793 case clang::TemplateArgument::Type:
2794 kind = eTemplateArgumentKindType;
2795 return template_arg.getAsType().getAsOpaquePtr();
2796
2797 case clang::TemplateArgument::Declaration:
2798 kind = eTemplateArgumentKindDeclaration;
2799 return NULL;
2800
2801 case clang::TemplateArgument::Integral:
2802 kind = eTemplateArgumentKindIntegral;
2803 return template_arg.getIntegralType().getAsOpaquePtr();
2804
2805 case clang::TemplateArgument::Template:
2806 kind = eTemplateArgumentKindTemplate;
2807 return NULL;
2808
2809 case clang::TemplateArgument::TemplateExpansion:
2810 kind = eTemplateArgumentKindTemplateExpansion;
2811 return NULL;
2812
2813 case clang::TemplateArgument::Expression:
2814 kind = eTemplateArgumentKindExpression;
2815 return NULL;
2816
2817 case clang::TemplateArgument::Pack:
2818 kind = eTemplateArgumentKindPack;
2819 return NULL;
2820
2821 default:
2822 assert (!"Unhandled TemplateArgument::ArgKind");
2823 kind = eTemplateArgumentKindNull;
2824 return NULL;
2825 }
2826 }
2827 }
2828 }
2829 break;
2830
2831 case clang::Type::Typedef:
2832 return ClangASTContext::GetTemplateArgument (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), arg_idx, kind);
2833 default:
2834 break;
2835 }
2836 }
2837 kind = eTemplateArgumentKindNull;
2838 return NULL;
2839}
Greg Clayton0fffff52010-09-24 05:15:53 +00002840
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002841uint32_t
Greg Clayton73b472d2010-10-27 03:32:59 +00002842ClangASTContext::GetTypeInfo
2843(
2844 clang_type_t clang_type,
Greg Clayton6beaaa62011-01-17 03:46:26 +00002845 clang::ASTContext *ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00002846 clang_type_t *pointee_or_element_clang_type
2847)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002848{
2849 if (clang_type == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00002850 return 0;
2851
2852 if (pointee_or_element_clang_type)
2853 *pointee_or_element_clang_type = NULL;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002854
2855 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2856
2857 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2858 switch (type_class)
2859 {
Sean Callanana2424172010-10-25 00:29:48 +00002860 case clang::Type::Builtin:
2861 switch (cast<clang::BuiltinType>(qual_type)->getKind())
2862 {
Sean Callanana2424172010-10-25 00:29:48 +00002863 case clang::BuiltinType::ObjCId:
2864 case clang::BuiltinType::ObjCClass:
Greg Clayton6beaaa62011-01-17 03:46:26 +00002865 if (ast && pointee_or_element_clang_type)
2866 *pointee_or_element_clang_type = ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Sean Callanana2424172010-10-25 00:29:48 +00002867 return eTypeIsBuiltIn | eTypeIsPointer | eTypeHasValue;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002868 break;
2869 case clang::BuiltinType::Bool:
2870 case clang::BuiltinType::Char_U:
2871 case clang::BuiltinType::UChar:
2872 case clang::BuiltinType::WChar_U:
2873 case clang::BuiltinType::Char16:
2874 case clang::BuiltinType::Char32:
2875 case clang::BuiltinType::UShort:
2876 case clang::BuiltinType::UInt:
2877 case clang::BuiltinType::ULong:
2878 case clang::BuiltinType::ULongLong:
2879 case clang::BuiltinType::UInt128:
2880 case clang::BuiltinType::Char_S:
2881 case clang::BuiltinType::SChar:
2882 case clang::BuiltinType::WChar_S:
2883 case clang::BuiltinType::Short:
2884 case clang::BuiltinType::Int:
2885 case clang::BuiltinType::Long:
2886 case clang::BuiltinType::LongLong:
2887 case clang::BuiltinType::Int128:
2888 case clang::BuiltinType::Float:
2889 case clang::BuiltinType::Double:
2890 case clang::BuiltinType::LongDouble:
2891 return eTypeIsBuiltIn | eTypeHasValue | eTypeIsScalar;
Greg Clayton73b472d2010-10-27 03:32:59 +00002892 default:
2893 break;
Sean Callanana2424172010-10-25 00:29:48 +00002894 }
2895 return eTypeIsBuiltIn | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002896
2897 case clang::Type::BlockPointer:
2898 if (pointee_or_element_clang_type)
2899 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2900 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
2901
Greg Clayton49462ea2011-01-15 02:52:14 +00002902 case clang::Type::Complex: return eTypeIsBuiltIn | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002903
2904 case clang::Type::ConstantArray:
2905 case clang::Type::DependentSizedArray:
2906 case clang::Type::IncompleteArray:
2907 case clang::Type::VariableArray:
2908 if (pointee_or_element_clang_type)
2909 *pointee_or_element_clang_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
2910 return eTypeHasChildren | eTypeIsArray;
2911
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002912 case clang::Type::DependentName: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002913 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
2914 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
2915 case clang::Type::Decltype: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00002916
2917 case clang::Type::Enum:
2918 if (pointee_or_element_clang_type)
2919 *pointee_or_element_clang_type = cast<EnumType>(qual_type)->getDecl()->getIntegerType().getAsOpaquePtr();
2920 return eTypeIsEnumeration | eTypeHasValue;
2921
Sean Callanan912855f2011-08-11 23:56:13 +00002922 case clang::Type::Elaborated:
2923 return ClangASTContext::GetTypeInfo (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2924 ast,
2925 pointee_or_element_clang_type);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002926 case clang::Type::ExtVector: return eTypeHasChildren | eTypeIsVector;
2927 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
2928 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002929 case clang::Type::InjectedClassName: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00002930
2931 case clang::Type::LValueReference:
2932 case clang::Type::RValueReference:
2933 if (pointee_or_element_clang_type)
2934 *pointee_or_element_clang_type = cast<ReferenceType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr();
2935 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
2936
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002937 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002938
2939 case clang::Type::ObjCObjectPointer:
2940 if (pointee_or_element_clang_type)
2941 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2942 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
2943
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002944 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
2945 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
Greg Clayton73b472d2010-10-27 03:32:59 +00002946
2947 case clang::Type::Pointer:
2948 if (pointee_or_element_clang_type)
2949 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2950 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
2951
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002952 case clang::Type::Record:
2953 if (qual_type->getAsCXXRecordDecl())
2954 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
2955 else
2956 return eTypeHasChildren | eTypeIsStructUnion;
2957 break;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002958 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
2959 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
2960 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
Greg Clayton73b472d2010-10-27 03:32:59 +00002961
2962 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00002963 return eTypeIsTypedef | ClangASTContext::GetTypeInfo (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00002964 ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00002965 pointee_or_element_clang_type);
2966
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002967 case clang::Type::TypeOfExpr: return 0;
2968 case clang::Type::TypeOf: return 0;
2969 case clang::Type::UnresolvedUsing: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002970 case clang::Type::Vector: return eTypeHasChildren | eTypeIsVector;
2971 default: return 0;
2972 }
2973 return 0;
2974}
2975
Greg Clayton9e409562010-07-28 02:04:09 +00002976
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002977#pragma mark Aggregate Types
2978
2979bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002980ClangASTContext::IsAggregateType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002981{
2982 if (clang_type == NULL)
2983 return false;
2984
2985 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2986
Greg Clayton737b9322010-09-13 03:32:57 +00002987 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2988 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002989 {
Greg Claytone1a916a2010-07-21 22:12:05 +00002990 case clang::Type::IncompleteArray:
2991 case clang::Type::VariableArray:
2992 case clang::Type::ConstantArray:
2993 case clang::Type::ExtVector:
2994 case clang::Type::Vector:
2995 case clang::Type::Record:
Greg Clayton9e409562010-07-28 02:04:09 +00002996 case clang::Type::ObjCObject:
2997 case clang::Type::ObjCInterface:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002998 return true;
Sean Callanan912855f2011-08-11 23:56:13 +00002999 case clang::Type::Elaborated:
3000 return ClangASTContext::IsAggregateType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Claytone1a916a2010-07-21 22:12:05 +00003001 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00003002 return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003003
3004 default:
3005 break;
3006 }
3007 // The clang type does have a value
3008 return false;
3009}
3010
3011uint32_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00003012ClangASTContext::GetNumChildren (clang::ASTContext *ast, clang_type_t clang_type, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003013{
Greg Clayton6beaaa62011-01-17 03:46:26 +00003014 if (clang_type == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003015 return 0;
3016
3017 uint32_t num_children = 0;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003018 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00003019 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3020 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003021 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003022 case clang::Type::Builtin:
3023 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3024 {
Greg Clayton73b472d2010-10-27 03:32:59 +00003025 case clang::BuiltinType::ObjCId: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003026 case clang::BuiltinType::ObjCClass: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003027 num_children = 1;
Greg Clayton73b472d2010-10-27 03:32:59 +00003028 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003029
3030 default:
3031 break;
3032 }
3033 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00003034
Greg Clayton49462ea2011-01-15 02:52:14 +00003035 case clang::Type::Complex: return 0;
Greg Clayton54979cd2010-12-15 05:08:08 +00003036
Greg Claytone1a916a2010-07-21 22:12:05 +00003037 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00003038 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003039 {
3040 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3041 const RecordDecl *record_decl = record_type->getDecl();
3042 assert(record_decl);
3043 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3044 if (cxx_record_decl)
3045 {
3046 if (omit_empty_base_classes)
3047 {
3048 // Check each base classes to see if it or any of its
3049 // base classes contain any fields. This can help
3050 // limit the noise in variable views by not having to
3051 // show base classes that contain no members.
3052 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3053 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3054 base_class != base_class_end;
3055 ++base_class)
3056 {
3057 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3058
3059 // Skip empty base classes
3060 if (RecordHasFields(base_class_decl) == false)
3061 continue;
3062
3063 num_children++;
3064 }
3065 }
3066 else
3067 {
3068 // Include all base classes
3069 num_children += cxx_record_decl->getNumBases();
3070 }
3071
3072 }
3073 RecordDecl::field_iterator field, field_end;
3074 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3075 ++num_children;
3076 }
3077 break;
3078
Greg Clayton9e409562010-07-28 02:04:09 +00003079 case clang::Type::ObjCObject:
3080 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00003081 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00003082 {
Sean Callanan78e37602011-01-27 04:42:51 +00003083 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003084 assert (objc_class_type);
3085 if (objc_class_type)
3086 {
3087 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3088
3089 if (class_interface_decl)
3090 {
3091
3092 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3093 if (superclass_interface_decl)
3094 {
3095 if (omit_empty_base_classes)
3096 {
3097 if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true))
3098 ++num_children;
3099 }
3100 else
3101 ++num_children;
3102 }
3103
3104 num_children += class_interface_decl->ivar_size();
3105 }
3106 }
3107 }
3108 break;
3109
3110 case clang::Type::ObjCObjectPointer:
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003111 {
Sean Callanan78e37602011-01-27 04:42:51 +00003112 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003113 QualType pointee_type = pointer_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003114 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3115 pointee_type.getAsOpaquePtr(),
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003116 omit_empty_base_classes);
3117 // If this type points to a simple type, then it has 1 child
3118 if (num_pointee_children == 0)
3119 num_children = 1;
3120 else
3121 num_children = num_pointee_children;
3122 }
3123 break;
Greg Clayton9e409562010-07-28 02:04:09 +00003124
Greg Claytone1a916a2010-07-21 22:12:05 +00003125 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003126 num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
3127 break;
3128
Greg Claytone1a916a2010-07-21 22:12:05 +00003129 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003130 {
Sean Callanan78e37602011-01-27 04:42:51 +00003131 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003132 QualType pointee_type (pointer_type->getPointeeType());
Greg Clayton6beaaa62011-01-17 03:46:26 +00003133 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3134 pointee_type.getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00003135 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003136 if (num_pointee_children == 0)
Greg Clayton54979cd2010-12-15 05:08:08 +00003137 {
3138 // We have a pointer to a pointee type that claims it has no children.
3139 // We will want to look at
3140 num_children = ClangASTContext::GetNumPointeeChildren (pointee_type.getAsOpaquePtr());
3141 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003142 else
3143 num_children = num_pointee_children;
3144 }
3145 break;
3146
Greg Clayton73b472d2010-10-27 03:32:59 +00003147 case clang::Type::LValueReference:
3148 case clang::Type::RValueReference:
3149 {
Sean Callanan78e37602011-01-27 04:42:51 +00003150 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton73b472d2010-10-27 03:32:59 +00003151 QualType pointee_type = reference_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003152 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3153 pointee_type.getAsOpaquePtr(),
Greg Clayton73b472d2010-10-27 03:32:59 +00003154 omit_empty_base_classes);
3155 // If this type points to a simple type, then it has 1 child
3156 if (num_pointee_children == 0)
3157 num_children = 1;
3158 else
3159 num_children = num_pointee_children;
3160 }
3161 break;
3162
3163
Greg Claytone1a916a2010-07-21 22:12:05 +00003164 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00003165 num_children = ClangASTContext::GetNumChildren (ast,
3166 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3167 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003168 break;
Sean Callanan912855f2011-08-11 23:56:13 +00003169
3170 case clang::Type::Elaborated:
3171 num_children = ClangASTContext::GetNumChildren (ast,
3172 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3173 omit_empty_base_classes);
3174 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003175
3176 default:
3177 break;
3178 }
3179 return num_children;
3180}
3181
Greg Claytonbf2331c2011-09-09 23:04:00 +00003182uint32_t
3183ClangASTContext::GetNumDirectBaseClasses (clang::ASTContext *ast, clang_type_t clang_type)
3184{
3185 if (clang_type == NULL)
3186 return 0;
3187
3188 uint32_t count = 0;
3189 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3190 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3191 switch (type_class)
3192 {
3193 case clang::Type::Record:
3194 if (GetCompleteQualType (ast, qual_type))
3195 {
3196 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3197 if (cxx_record_decl)
3198 count = cxx_record_decl->getNumBases();
3199 }
3200 break;
3201
3202 case clang::Type::ObjCObject:
3203 case clang::Type::ObjCInterface:
3204 if (GetCompleteQualType (ast, qual_type))
3205 {
3206 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3207 if (objc_class_type)
3208 {
3209 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3210
3211 if (class_interface_decl && class_interface_decl->getSuperClass())
3212 count = 1;
3213 }
3214 }
3215 break;
3216
3217
3218 case clang::Type::Typedef:
3219 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3220 break;
3221
3222 case clang::Type::Elaborated:
3223 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3224 break;
3225
3226 default:
3227 break;
3228 }
3229 return count;
3230}
3231
3232uint32_t
3233ClangASTContext::GetNumVirtualBaseClasses (clang::ASTContext *ast,
3234 clang_type_t clang_type)
3235{
3236 if (clang_type == NULL)
3237 return 0;
3238
3239 uint32_t count = 0;
3240 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3241 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3242 switch (type_class)
3243 {
3244 case clang::Type::Record:
3245 if (GetCompleteQualType (ast, qual_type))
3246 {
3247 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3248 if (cxx_record_decl)
3249 count = cxx_record_decl->getNumVBases();
3250 }
3251 break;
3252
3253 case clang::Type::Typedef:
3254 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3255 break;
3256
3257 case clang::Type::Elaborated:
3258 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3259 break;
3260
3261 default:
3262 break;
3263 }
3264 return count;
3265}
3266
3267uint32_t
3268ClangASTContext::GetNumFields (clang::ASTContext *ast, clang_type_t clang_type)
3269{
3270 if (clang_type == NULL)
3271 return 0;
3272
3273 uint32_t count = 0;
3274 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3275 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3276 switch (type_class)
3277 {
3278 case clang::Type::Record:
3279 if (GetCompleteQualType (ast, qual_type))
3280 {
3281 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
3282 if (record_type)
3283 {
3284 RecordDecl *record_decl = record_type->getDecl();
3285 if (record_decl)
3286 {
3287 uint32_t field_idx = 0;
3288 RecordDecl::field_iterator field, field_end;
3289 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3290 ++field_idx;
3291 count = field_idx;
3292 }
3293 }
3294 }
3295 break;
3296
3297 case clang::Type::Typedef:
3298 count = ClangASTContext::GetNumFields (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3299 break;
3300
3301 case clang::Type::Elaborated:
3302 count = ClangASTContext::GetNumFields (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3303 break;
3304
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003305 case clang::Type::ObjCObject:
3306 case clang::Type::ObjCInterface:
3307 if (GetCompleteQualType (ast, qual_type))
3308 {
3309 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3310 if (objc_class_type)
3311 {
3312 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3313
3314 if (class_interface_decl)
3315 count = class_interface_decl->ivar_size();
3316 }
3317 }
3318 break;
3319
Greg Claytonbf2331c2011-09-09 23:04:00 +00003320 default:
3321 break;
3322 }
3323 return count;
3324}
3325
3326clang_type_t
3327ClangASTContext::GetDirectBaseClassAtIndex (clang::ASTContext *ast,
3328 clang_type_t clang_type,
3329 uint32_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003330 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003331{
3332 if (clang_type == NULL)
3333 return 0;
3334
3335 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3336 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3337 switch (type_class)
3338 {
3339 case clang::Type::Record:
3340 if (GetCompleteQualType (ast, qual_type))
3341 {
3342 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3343 if (cxx_record_decl)
3344 {
3345 uint32_t curr_idx = 0;
3346 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3347 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3348 base_class != base_class_end;
3349 ++base_class, ++curr_idx)
3350 {
3351 if (curr_idx == idx)
3352 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003353 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003354 {
3355 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3356 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3357// if (base_class->isVirtual())
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003358// *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003359// else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003360 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003361 }
3362 return base_class->getType().getAsOpaquePtr();
3363 }
3364 }
3365 }
3366 }
3367 break;
3368
3369 case clang::Type::ObjCObject:
3370 case clang::Type::ObjCInterface:
3371 if (idx == 0 && GetCompleteQualType (ast, qual_type))
3372 {
3373 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3374 if (objc_class_type)
3375 {
3376 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3377
3378 if (class_interface_decl)
3379 {
3380 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3381 if (superclass_interface_decl)
3382 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003383 if (bit_offset_ptr)
3384 *bit_offset_ptr = 0;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003385 return ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr();
3386 }
3387 }
3388 }
3389 }
3390 break;
3391
3392
3393 case clang::Type::Typedef:
3394 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3395 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3396 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003397 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003398
3399 case clang::Type::Elaborated:
3400 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3401 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3402 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003403 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003404
3405 default:
3406 break;
3407 }
3408 return NULL;
3409}
3410
3411clang_type_t
3412ClangASTContext::GetVirtualBaseClassAtIndex (clang::ASTContext *ast,
3413 clang_type_t clang_type,
3414 uint32_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003415 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003416{
3417 if (clang_type == NULL)
3418 return 0;
3419
3420 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3421 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3422 switch (type_class)
3423 {
3424 case clang::Type::Record:
3425 if (GetCompleteQualType (ast, qual_type))
3426 {
3427 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3428 if (cxx_record_decl)
3429 {
3430 uint32_t curr_idx = 0;
3431 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3432 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
3433 base_class != base_class_end;
3434 ++base_class, ++curr_idx)
3435 {
3436 if (curr_idx == idx)
3437 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003438 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003439 {
3440 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3441 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003442 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003443
3444 }
3445 return base_class->getType().getAsOpaquePtr();
3446 }
3447 }
3448 }
3449 }
3450 break;
3451
3452 case clang::Type::Typedef:
3453 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3454 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3455 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003456 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003457
3458 case clang::Type::Elaborated:
3459 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3460 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3461 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003462 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003463
3464 default:
3465 break;
3466 }
3467 return NULL;
3468}
3469
3470clang_type_t
3471ClangASTContext::GetFieldAtIndex (clang::ASTContext *ast,
3472 clang_type_t clang_type,
3473 uint32_t idx,
3474 std::string& name,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003475 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003476{
3477 if (clang_type == NULL)
3478 return 0;
3479
3480 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3481 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3482 switch (type_class)
3483 {
3484 case clang::Type::Record:
3485 if (GetCompleteQualType (ast, qual_type))
3486 {
3487 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3488 const RecordDecl *record_decl = record_type->getDecl();
3489 uint32_t field_idx = 0;
3490 RecordDecl::field_iterator field, field_end;
3491 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
3492 {
3493 if (idx == field_idx)
3494 {
3495 // Print the member type if requested
3496 // Print the member name and equal sign
3497 name.assign(field->getNameAsString());
3498
3499 // Figure out the type byte size (field_type_info.first) and
3500 // alignment (field_type_info.second) from the AST context.
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003501 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003502 {
3503 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003504 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003505 }
3506
3507 return field->getType().getAsOpaquePtr();
3508 }
3509 }
3510 }
3511 break;
3512
3513 case clang::Type::ObjCObject:
3514 case clang::Type::ObjCInterface:
3515 if (GetCompleteQualType (ast, qual_type))
3516 {
3517 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3518 assert (objc_class_type);
3519 if (objc_class_type)
3520 {
3521 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3522
3523 if (class_interface_decl)
3524 {
3525 if (idx < (class_interface_decl->ivar_size()))
3526 {
3527 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
3528 uint32_t ivar_idx = 0;
3529
3530 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
3531 {
3532 if (ivar_idx == idx)
3533 {
3534 const ObjCIvarDecl* ivar_decl = *ivar_pos;
3535
3536 QualType ivar_qual_type(ivar_decl->getType());
3537
3538 name.assign(ivar_decl->getNameAsString());
3539
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003540 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003541 {
3542 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003543 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003544 }
3545
3546 return ivar_qual_type.getAsOpaquePtr();
3547 }
3548 }
3549 }
3550 }
3551 }
3552 }
3553 break;
3554
3555
3556 case clang::Type::Typedef:
3557 return ClangASTContext::GetFieldAtIndex (ast,
3558 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3559 idx,
3560 name,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003561 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003562
3563 case clang::Type::Elaborated:
3564 return ClangASTContext::GetFieldAtIndex (ast,
3565 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3566 idx,
3567 name,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003568 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003569
3570 default:
3571 break;
3572 }
3573 return NULL;
3574}
3575
3576
Greg Clayton54979cd2010-12-15 05:08:08 +00003577// If a pointer to a pointee type (the clang_type arg) says that it has no
3578// children, then we either need to trust it, or override it and return a
3579// different result. For example, an "int *" has one child that is an integer,
3580// but a function pointer doesn't have any children. Likewise if a Record type
3581// claims it has no children, then there really is nothing to show.
3582uint32_t
3583ClangASTContext::GetNumPointeeChildren (clang_type_t clang_type)
3584{
3585 if (clang_type == NULL)
3586 return 0;
3587
3588 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3589 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3590 switch (type_class)
3591 {
Greg Clayton97a43712011-01-08 22:26:47 +00003592 case clang::Type::Builtin:
3593 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3594 {
Greg Clayton7260f622011-04-18 08:33:37 +00003595 case clang::BuiltinType::UnknownAny:
Greg Clayton97a43712011-01-08 22:26:47 +00003596 case clang::BuiltinType::Void:
3597 case clang::BuiltinType::NullPtr:
3598 return 0;
3599 case clang::BuiltinType::Bool:
3600 case clang::BuiltinType::Char_U:
3601 case clang::BuiltinType::UChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003602 case clang::BuiltinType::WChar_U:
Greg Clayton97a43712011-01-08 22:26:47 +00003603 case clang::BuiltinType::Char16:
3604 case clang::BuiltinType::Char32:
3605 case clang::BuiltinType::UShort:
3606 case clang::BuiltinType::UInt:
3607 case clang::BuiltinType::ULong:
3608 case clang::BuiltinType::ULongLong:
3609 case clang::BuiltinType::UInt128:
3610 case clang::BuiltinType::Char_S:
3611 case clang::BuiltinType::SChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003612 case clang::BuiltinType::WChar_S:
Greg Clayton97a43712011-01-08 22:26:47 +00003613 case clang::BuiltinType::Short:
3614 case clang::BuiltinType::Int:
3615 case clang::BuiltinType::Long:
3616 case clang::BuiltinType::LongLong:
3617 case clang::BuiltinType::Int128:
3618 case clang::BuiltinType::Float:
3619 case clang::BuiltinType::Double:
3620 case clang::BuiltinType::LongDouble:
3621 case clang::BuiltinType::Dependent:
3622 case clang::BuiltinType::Overload:
Greg Clayton97a43712011-01-08 22:26:47 +00003623 case clang::BuiltinType::ObjCId:
3624 case clang::BuiltinType::ObjCClass:
3625 case clang::BuiltinType::ObjCSel:
Sean Callanand12cf8bb2011-05-15 22:34:38 +00003626 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00003627 case clang::BuiltinType::Half:
3628 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00003629 case clang::BuiltinType::PseudoObject:
Greg Clayton97a43712011-01-08 22:26:47 +00003630 return 1;
3631 }
3632 break;
3633
Greg Clayton49462ea2011-01-15 02:52:14 +00003634 case clang::Type::Complex: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003635 case clang::Type::Pointer: return 1;
3636 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
3637 case clang::Type::LValueReference: return 1;
3638 case clang::Type::RValueReference: return 1;
3639 case clang::Type::MemberPointer: return 0;
3640 case clang::Type::ConstantArray: return 0;
3641 case clang::Type::IncompleteArray: return 0;
3642 case clang::Type::VariableArray: return 0;
3643 case clang::Type::DependentSizedArray: return 0;
3644 case clang::Type::DependentSizedExtVector: return 0;
3645 case clang::Type::Vector: return 0;
3646 case clang::Type::ExtVector: return 0;
3647 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
3648 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
3649 case clang::Type::UnresolvedUsing: return 0;
3650 case clang::Type::Paren: return 0;
3651 case clang::Type::Typedef: return ClangASTContext::GetNumPointeeChildren (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00003652 case clang::Type::Elaborated: return ClangASTContext::GetNumPointeeChildren (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003653 case clang::Type::TypeOfExpr: return 0;
3654 case clang::Type::TypeOf: return 0;
3655 case clang::Type::Decltype: return 0;
3656 case clang::Type::Record: return 0;
3657 case clang::Type::Enum: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003658 case clang::Type::TemplateTypeParm: return 1;
3659 case clang::Type::SubstTemplateTypeParm: return 1;
3660 case clang::Type::TemplateSpecialization: return 1;
3661 case clang::Type::InjectedClassName: return 0;
3662 case clang::Type::DependentName: return 1;
3663 case clang::Type::DependentTemplateSpecialization: return 1;
3664 case clang::Type::ObjCObject: return 0;
3665 case clang::Type::ObjCInterface: return 0;
3666 case clang::Type::ObjCObjectPointer: return 1;
3667 default:
3668 break;
3669 }
3670 return 0;
3671}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003672
Greg Clayton1be10fc2010-09-29 01:12:09 +00003673clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003674ClangASTContext::GetChildClangTypeAtIndex
3675(
Jim Inghamd555bac2011-06-24 22:03:24 +00003676 ExecutionContext *exe_ctx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003677 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003678 clang_type_t parent_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003679 uint32_t idx,
3680 bool transparent_pointers,
3681 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003682 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003683 std::string& child_name,
3684 uint32_t &child_byte_size,
3685 int32_t &child_byte_offset,
3686 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003687 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003688 bool &child_is_base_class,
3689 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003690)
3691{
3692 if (parent_clang_type)
3693
Jim Inghamd555bac2011-06-24 22:03:24 +00003694 return GetChildClangTypeAtIndex (exe_ctx,
3695 getASTContext(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003696 parent_name,
3697 parent_clang_type,
3698 idx,
3699 transparent_pointers,
3700 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003701 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003702 child_name,
3703 child_byte_size,
3704 child_byte_offset,
3705 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003706 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003707 child_is_base_class,
3708 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003709 return NULL;
3710}
3711
Greg Clayton1be10fc2010-09-29 01:12:09 +00003712clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003713ClangASTContext::GetChildClangTypeAtIndex
3714(
Jim Inghamd555bac2011-06-24 22:03:24 +00003715 ExecutionContext *exe_ctx,
Greg Clayton6beaaa62011-01-17 03:46:26 +00003716 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003717 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003718 clang_type_t parent_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003719 uint32_t idx,
3720 bool transparent_pointers,
3721 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003722 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003723 std::string& child_name,
3724 uint32_t &child_byte_size,
3725 int32_t &child_byte_offset,
3726 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003727 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003728 bool &child_is_base_class,
3729 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003730)
3731{
3732 if (parent_clang_type == NULL)
3733 return NULL;
3734
Greg Clayton6beaaa62011-01-17 03:46:26 +00003735 if (idx < ClangASTContext::GetNumChildren (ast, parent_clang_type, omit_empty_base_classes))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003736 {
3737 uint32_t bit_offset;
3738 child_bitfield_bit_size = 0;
3739 child_bitfield_bit_offset = 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003740 child_is_base_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003741 QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00003742 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
3743 switch (parent_type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003744 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003745 case clang::Type::Builtin:
3746 switch (cast<clang::BuiltinType>(parent_qual_type)->getKind())
3747 {
3748 case clang::BuiltinType::ObjCId:
3749 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00003750 child_name = "isa";
Greg Clayton6beaaa62011-01-17 03:46:26 +00003751 child_byte_size = ast->getTypeSize(ast->ObjCBuiltinClassTy) / CHAR_BIT;
3752 return ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003753
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003754 default:
3755 break;
3756 }
3757 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003758
Greg Claytone1a916a2010-07-21 22:12:05 +00003759 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00003760 if (GetCompleteQualType (ast, parent_qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003761 {
3762 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr());
3763 const RecordDecl *record_decl = record_type->getDecl();
3764 assert(record_decl);
Greg Clayton6beaaa62011-01-17 03:46:26 +00003765 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003766 uint32_t child_idx = 0;
3767
3768 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3769 if (cxx_record_decl)
3770 {
3771 // We might have base classes to print out first
3772 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3773 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3774 base_class != base_class_end;
3775 ++base_class)
3776 {
3777 const CXXRecordDecl *base_class_decl = NULL;
3778
3779 // Skip empty base classes
3780 if (omit_empty_base_classes)
3781 {
3782 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3783 if (RecordHasFields(base_class_decl) == false)
3784 continue;
3785 }
3786
3787 if (idx == child_idx)
3788 {
3789 if (base_class_decl == NULL)
3790 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3791
3792
3793 if (base_class->isVirtual())
Greg Clayton6ed95942011-01-22 07:12:45 +00003794 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003795 else
Greg Clayton6ed95942011-01-22 07:12:45 +00003796 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003797
3798 // Base classes should be a multiple of 8 bits in size
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003799 child_byte_offset = bit_offset/8;
Greg Claytone3055942011-06-30 02:28:26 +00003800
Greg Clayton84db9102012-03-26 23:03:23 +00003801 child_name = ClangASTType::GetTypeNameForQualType(ast, base_class->getType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003802
Greg Clayton6beaaa62011-01-17 03:46:26 +00003803 uint64_t clang_type_info_bit_size = ast->getTypeSize(base_class->getType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003804
Jim Inghamf46b3382011-04-15 23:42:06 +00003805 // Base classes bit sizes should be a multiple of 8 bits in size
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003806 assert (clang_type_info_bit_size % 8 == 0);
3807 child_byte_size = clang_type_info_bit_size / 8;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003808 child_is_base_class = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003809 return base_class->getType().getAsOpaquePtr();
3810 }
3811 // We don't increment the child index in the for loop since we might
3812 // be skipping empty base classes
3813 ++child_idx;
3814 }
3815 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003816 // Make sure index is in range...
3817 uint32_t field_idx = 0;
3818 RecordDecl::field_iterator field, field_end;
3819 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
3820 {
3821 if (idx == child_idx)
3822 {
3823 // Print the member type if requested
3824 // Print the member name and equal sign
3825 child_name.assign(field->getNameAsString().c_str());
3826
3827 // Figure out the type byte size (field_type_info.first) and
3828 // alignment (field_type_info.second) from the AST context.
Greg Clayton6beaaa62011-01-17 03:46:26 +00003829 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(field->getType());
Greg Claytonc982c762010-07-09 20:39:50 +00003830 assert(field_idx < record_layout.getFieldCount());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003831
3832 child_byte_size = field_type_info.first / 8;
3833
3834 // Figure out the field offset within the current struct/union/class type
3835 bit_offset = record_layout.getFieldOffset (field_idx);
3836 child_byte_offset = bit_offset / 8;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003837 if (ClangASTContext::FieldIsBitfield (ast, *field, child_bitfield_bit_size))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003838 child_bitfield_bit_offset = bit_offset % 8;
3839
3840 return field->getType().getAsOpaquePtr();
3841 }
3842 }
3843 }
3844 break;
3845
Greg Clayton9e409562010-07-28 02:04:09 +00003846 case clang::Type::ObjCObject:
3847 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00003848 if (GetCompleteQualType (ast, parent_qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00003849 {
Sean Callanan78e37602011-01-27 04:42:51 +00003850 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003851 assert (objc_class_type);
3852 if (objc_class_type)
3853 {
3854 uint32_t child_idx = 0;
3855 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3856
3857 if (class_interface_decl)
3858 {
3859
Greg Clayton6beaaa62011-01-17 03:46:26 +00003860 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
Greg Clayton9e409562010-07-28 02:04:09 +00003861 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3862 if (superclass_interface_decl)
3863 {
3864 if (omit_empty_base_classes)
3865 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00003866 if (ClangASTContext::GetNumChildren(ast, ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0)
Greg Clayton9e409562010-07-28 02:04:09 +00003867 {
3868 if (idx == 0)
3869 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00003870 QualType ivar_qual_type(ast->getObjCInterfaceType(superclass_interface_decl));
Greg Clayton9e409562010-07-28 02:04:09 +00003871
3872
3873 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
3874
Greg Clayton6beaaa62011-01-17 03:46:26 +00003875 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003876
3877 child_byte_size = ivar_type_info.first / 8;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003878 child_byte_offset = 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003879 child_is_base_class = true;
Greg Clayton9e409562010-07-28 02:04:09 +00003880
3881 return ivar_qual_type.getAsOpaquePtr();
3882 }
3883
3884 ++child_idx;
3885 }
3886 }
3887 else
3888 ++child_idx;
3889 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003890
3891 const uint32_t superclass_idx = child_idx;
Greg Clayton9e409562010-07-28 02:04:09 +00003892
3893 if (idx < (child_idx + class_interface_decl->ivar_size()))
3894 {
3895 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
3896
3897 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
3898 {
3899 if (child_idx == idx)
3900 {
Jim Inghamf80bc3f2011-12-08 02:53:10 +00003901 ObjCIvarDecl* ivar_decl = *ivar_pos;
Greg Clayton9e409562010-07-28 02:04:09 +00003902
3903 QualType ivar_qual_type(ivar_decl->getType());
3904
3905 child_name.assign(ivar_decl->getNameAsString().c_str());
3906
Greg Clayton6beaaa62011-01-17 03:46:26 +00003907 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003908
3909 child_byte_size = ivar_type_info.first / 8;
3910
3911 // Figure out the field offset within the current struct/union/class type
Jim Inghamd555bac2011-06-24 22:03:24 +00003912 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
3913 // that doesn't account for the space taken up by unbacked properties, or from
3914 // the changing size of base classes that are newer than this class.
3915 // So if we have a process around that we can ask about this object, do so.
3916 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
Greg Claytonc14ee322011-09-22 04:58:26 +00003917 Process *process = NULL;
3918 if (exe_ctx)
3919 process = exe_ctx->GetProcessPtr();
3920 if (process)
Jim Inghamd555bac2011-06-24 22:03:24 +00003921 {
Greg Claytonc14ee322011-09-22 04:58:26 +00003922 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
Jim Inghamd555bac2011-06-24 22:03:24 +00003923 if (objc_runtime != NULL)
3924 {
Enrico Granata6f3533f2011-07-29 19:53:35 +00003925 ClangASTType parent_ast_type (ast, parent_qual_type.getAsOpaquePtr());
Jim Inghamd555bac2011-06-24 22:03:24 +00003926 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
3927 }
3928 }
3929
Jim Inghamf80bc3f2011-12-08 02:53:10 +00003930 // Setting this to UINT32_MAX to make sure we don't compute it twice...
3931 bit_offset = UINT32_MAX;
3932
Jim Inghamd555bac2011-06-24 22:03:24 +00003933 if (child_byte_offset == LLDB_INVALID_IVAR_OFFSET)
3934 {
3935 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
3936 child_byte_offset = bit_offset / 8;
3937 }
Jim Inghamf80bc3f2011-12-08 02:53:10 +00003938
3939 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
3940 // of a bitfield within its containing object. So regardless of where we get the byte
3941 // offset from, we still need to get the bit offset for bitfields from the layout.
3942
3943 if (ClangASTContext::FieldIsBitfield (ast, ivar_decl, child_bitfield_bit_size))
3944 {
3945 if (bit_offset == UINT32_MAX)
3946 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
3947
3948 child_bitfield_bit_offset = bit_offset % 8;
3949 }
Greg Clayton9e409562010-07-28 02:04:09 +00003950 return ivar_qual_type.getAsOpaquePtr();
3951 }
3952 ++child_idx;
3953 }
3954 }
3955 }
3956 }
3957 }
3958 break;
3959
3960 case clang::Type::ObjCObjectPointer:
3961 {
Sean Callanan78e37602011-01-27 04:42:51 +00003962 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003963 QualType pointee_type = pointer_type->getPointeeType();
3964
3965 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
3966 {
Greg Claytone221f822011-01-21 01:59:00 +00003967 child_is_deref_of_parent = false;
3968 bool tmp_child_is_deref_of_parent = false;
Jim Inghamd555bac2011-06-24 22:03:24 +00003969 return GetChildClangTypeAtIndex (exe_ctx,
3970 ast,
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003971 parent_name,
3972 pointer_type->getPointeeType().getAsOpaquePtr(),
3973 idx,
3974 transparent_pointers,
3975 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003976 ignore_array_bounds,
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003977 child_name,
3978 child_byte_size,
3979 child_byte_offset,
3980 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003981 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003982 child_is_base_class,
3983 tmp_child_is_deref_of_parent);
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003984 }
3985 else
3986 {
Greg Claytone221f822011-01-21 01:59:00 +00003987 child_is_deref_of_parent = true;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003988 if (parent_name)
3989 {
3990 child_name.assign(1, '*');
3991 child_name += parent_name;
3992 }
3993
3994 // We have a pointer to an simple type
Sean Callanan5b26f272012-02-04 08:49:35 +00003995 if (idx == 0 && GetCompleteQualType(ast, pointee_type))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003996 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00003997 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003998 assert(clang_type_info.first % 8 == 0);
3999 child_byte_size = clang_type_info.first / 8;
4000 child_byte_offset = 0;
4001 return pointee_type.getAsOpaquePtr();
4002 }
4003 }
Greg Clayton9e409562010-07-28 02:04:09 +00004004 }
4005 break;
4006
Greg Claytone1a916a2010-07-21 22:12:05 +00004007 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004008 {
4009 const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4010 const uint64_t element_count = array->getSize().getLimitedValue();
4011
Greg Claytondaf515f2011-07-09 20:12:33 +00004012 if (ignore_array_bounds || idx < element_count)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004013 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004014 if (GetCompleteQualType (ast, array->getElementType()))
4015 {
4016 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004017
Greg Clayton6beaaa62011-01-17 03:46:26 +00004018 char element_name[64];
4019 ::snprintf (element_name, sizeof (element_name), "[%u]", idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004020
Greg Clayton6beaaa62011-01-17 03:46:26 +00004021 child_name.assign(element_name);
4022 assert(field_type_info.first % 8 == 0);
4023 child_byte_size = field_type_info.first / 8;
Greg Claytondaf515f2011-07-09 20:12:33 +00004024 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
Greg Clayton6beaaa62011-01-17 03:46:26 +00004025 return array->getElementType().getAsOpaquePtr();
4026 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004027 }
4028 }
4029 break;
4030
Greg Claytone1a916a2010-07-21 22:12:05 +00004031 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004032 {
Sean Callanan78e37602011-01-27 04:42:51 +00004033 const PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004034 QualType pointee_type = pointer_type->getPointeeType();
Greg Clayton97a43712011-01-08 22:26:47 +00004035
4036 // Don't dereference "void *" pointers
4037 if (pointee_type->isVoidType())
4038 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004039
4040 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4041 {
Greg Claytone221f822011-01-21 01:59:00 +00004042 child_is_deref_of_parent = false;
4043 bool tmp_child_is_deref_of_parent = false;
Jim Inghamd555bac2011-06-24 22:03:24 +00004044 return GetChildClangTypeAtIndex (exe_ctx,
4045 ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004046 parent_name,
4047 pointer_type->getPointeeType().getAsOpaquePtr(),
4048 idx,
4049 transparent_pointers,
4050 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004051 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004052 child_name,
4053 child_byte_size,
4054 child_byte_offset,
4055 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00004056 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004057 child_is_base_class,
4058 tmp_child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004059 }
4060 else
4061 {
Greg Claytone221f822011-01-21 01:59:00 +00004062 child_is_deref_of_parent = true;
4063
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004064 if (parent_name)
4065 {
4066 child_name.assign(1, '*');
4067 child_name += parent_name;
4068 }
4069
4070 // We have a pointer to an simple type
4071 if (idx == 0)
4072 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004073 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004074 assert(clang_type_info.first % 8 == 0);
4075 child_byte_size = clang_type_info.first / 8;
4076 child_byte_offset = 0;
4077 return pointee_type.getAsOpaquePtr();
4078 }
4079 }
4080 }
4081 break;
4082
Greg Clayton73b472d2010-10-27 03:32:59 +00004083 case clang::Type::LValueReference:
4084 case clang::Type::RValueReference:
4085 {
Sean Callanan78e37602011-01-27 04:42:51 +00004086 const ReferenceType *reference_type = cast<ReferenceType>(parent_qual_type.getTypePtr());
Greg Clayton73b472d2010-10-27 03:32:59 +00004087 QualType pointee_type(reference_type->getPointeeType());
4088 clang_type_t pointee_clang_type = pointee_type.getAsOpaquePtr();
4089 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_clang_type))
4090 {
Greg Claytone221f822011-01-21 01:59:00 +00004091 child_is_deref_of_parent = false;
4092 bool tmp_child_is_deref_of_parent = false;
Jim Inghamd555bac2011-06-24 22:03:24 +00004093 return GetChildClangTypeAtIndex (exe_ctx,
4094 ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00004095 parent_name,
4096 pointee_clang_type,
4097 idx,
4098 transparent_pointers,
4099 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004100 ignore_array_bounds,
Greg Clayton73b472d2010-10-27 03:32:59 +00004101 child_name,
4102 child_byte_size,
4103 child_byte_offset,
4104 child_bitfield_bit_size,
4105 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004106 child_is_base_class,
4107 tmp_child_is_deref_of_parent);
Greg Clayton73b472d2010-10-27 03:32:59 +00004108 }
4109 else
4110 {
4111 if (parent_name)
4112 {
4113 child_name.assign(1, '&');
4114 child_name += parent_name;
4115 }
4116
4117 // We have a pointer to an simple type
4118 if (idx == 0)
4119 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004120 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Greg Clayton73b472d2010-10-27 03:32:59 +00004121 assert(clang_type_info.first % 8 == 0);
4122 child_byte_size = clang_type_info.first / 8;
4123 child_byte_offset = 0;
4124 return pointee_type.getAsOpaquePtr();
4125 }
4126 }
4127 }
4128 break;
4129
Greg Claytone1a916a2010-07-21 22:12:05 +00004130 case clang::Type::Typedef:
Jim Inghamd555bac2011-06-24 22:03:24 +00004131 return GetChildClangTypeAtIndex (exe_ctx,
4132 ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004133 parent_name,
Sean Callanan48114472010-12-13 01:26:27 +00004134 cast<TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004135 idx,
4136 transparent_pointers,
4137 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004138 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004139 child_name,
4140 child_byte_size,
4141 child_byte_offset,
4142 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00004143 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004144 child_is_base_class,
4145 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004146 break;
Sean Callanan912855f2011-08-11 23:56:13 +00004147
4148 case clang::Type::Elaborated:
4149 return GetChildClangTypeAtIndex (exe_ctx,
4150 ast,
4151 parent_name,
4152 cast<ElaboratedType>(parent_qual_type)->getNamedType().getAsOpaquePtr(),
4153 idx,
4154 transparent_pointers,
4155 omit_empty_base_classes,
4156 ignore_array_bounds,
4157 child_name,
4158 child_byte_size,
4159 child_byte_offset,
4160 child_bitfield_bit_size,
4161 child_bitfield_bit_offset,
4162 child_is_base_class,
4163 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004164
4165 default:
4166 break;
4167 }
4168 }
Greg Clayton19503a22010-07-23 15:37:46 +00004169 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004170}
4171
4172static inline bool
4173BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
4174{
Greg Clayton6beaaa62011-01-17 03:46:26 +00004175 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004176}
4177
4178static uint32_t
4179GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
4180{
4181 uint32_t num_bases = 0;
4182 if (cxx_record_decl)
4183 {
4184 if (omit_empty_base_classes)
4185 {
4186 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4187 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4188 base_class != base_class_end;
4189 ++base_class)
4190 {
4191 // Skip empty base classes
4192 if (omit_empty_base_classes)
4193 {
4194 if (BaseSpecifierIsEmpty (base_class))
4195 continue;
4196 }
4197 ++num_bases;
4198 }
4199 }
4200 else
4201 num_bases = cxx_record_decl->getNumBases();
4202 }
4203 return num_bases;
4204}
4205
4206
4207static uint32_t
4208GetIndexForRecordBase
4209(
4210 const RecordDecl *record_decl,
4211 const CXXBaseSpecifier *base_spec,
4212 bool omit_empty_base_classes
4213)
4214{
4215 uint32_t child_idx = 0;
4216
4217 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4218
4219// const char *super_name = record_decl->getNameAsCString();
4220// const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString();
4221// printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
4222//
4223 if (cxx_record_decl)
4224 {
4225 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4226 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4227 base_class != base_class_end;
4228 ++base_class)
4229 {
4230 if (omit_empty_base_classes)
4231 {
4232 if (BaseSpecifierIsEmpty (base_class))
4233 continue;
4234 }
4235
4236// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
4237// child_idx,
4238// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4239//
4240//
4241 if (base_class == base_spec)
4242 return child_idx;
4243 ++child_idx;
4244 }
4245 }
4246
4247 return UINT32_MAX;
4248}
4249
4250
4251static uint32_t
4252GetIndexForRecordChild
4253(
4254 const RecordDecl *record_decl,
4255 NamedDecl *canonical_decl,
4256 bool omit_empty_base_classes
4257)
4258{
4259 uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes);
4260
4261// const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4262//
4263//// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString());
4264// if (cxx_record_decl)
4265// {
4266// CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4267// for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4268// base_class != base_class_end;
4269// ++base_class)
4270// {
4271// if (omit_empty_base_classes)
4272// {
4273// if (BaseSpecifierIsEmpty (base_class))
4274// continue;
4275// }
4276//
4277//// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
4278//// record_decl->getNameAsCString(),
4279//// canonical_decl->getNameAsCString(),
4280//// child_idx,
4281//// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4282//
4283//
4284// CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4285// if (curr_base_class_decl == canonical_decl)
4286// {
4287// return child_idx;
4288// }
4289// ++child_idx;
4290// }
4291// }
4292//
4293// const uint32_t num_bases = child_idx;
4294 RecordDecl::field_iterator field, field_end;
4295 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4296 field != field_end;
4297 ++field, ++child_idx)
4298 {
4299// printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n",
4300// record_decl->getNameAsCString(),
4301// canonical_decl->getNameAsCString(),
4302// child_idx - num_bases,
4303// field->getNameAsCString());
4304
4305 if (field->getCanonicalDecl() == canonical_decl)
4306 return child_idx;
4307 }
4308
4309 return UINT32_MAX;
4310}
4311
4312// Look for a child member (doesn't include base classes, but it does include
4313// their members) in the type hierarchy. Returns an index path into "clang_type"
4314// on how to reach the appropriate member.
4315//
4316// class A
4317// {
4318// public:
4319// int m_a;
4320// int m_b;
4321// };
4322//
4323// class B
4324// {
4325// };
4326//
4327// class C :
4328// public B,
4329// public A
4330// {
4331// };
4332//
4333// If we have a clang type that describes "class C", and we wanted to looked
4334// "m_b" in it:
4335//
4336// With omit_empty_base_classes == false we would get an integer array back with:
4337// { 1, 1 }
4338// The first index 1 is the child index for "class A" within class C
4339// The second index 1 is the child index for "m_b" within class A
4340//
4341// With omit_empty_base_classes == true we would get an integer array back with:
4342// { 0, 1 }
4343// 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)
4344// The second index 1 is the child index for "m_b" within class A
4345
4346size_t
4347ClangASTContext::GetIndexOfChildMemberWithName
4348(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004349 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004350 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004351 const char *name,
4352 bool omit_empty_base_classes,
4353 std::vector<uint32_t>& child_indexes
4354)
4355{
4356 if (clang_type && name && name[0])
4357 {
4358 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00004359 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4360 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004361 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004362 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004363 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004364 {
4365 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4366 const RecordDecl *record_decl = record_type->getDecl();
4367
4368 assert(record_decl);
4369 uint32_t child_idx = 0;
4370
4371 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4372
4373 // Try and find a field that matches NAME
4374 RecordDecl::field_iterator field, field_end;
4375 StringRef name_sref(name);
4376 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4377 field != field_end;
4378 ++field, ++child_idx)
4379 {
4380 if (field->getName().equals (name_sref))
4381 {
4382 // We have to add on the number of base classes to this index!
4383 child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
4384 return child_indexes.size();
4385 }
4386 }
4387
4388 if (cxx_record_decl)
4389 {
4390 const RecordDecl *parent_record_decl = cxx_record_decl;
4391
4392 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
4393
4394 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
4395 // Didn't find things easily, lets let clang do its thang...
Sean Callanancc427fa2011-07-30 02:42:06 +00004396 IdentifierInfo & ident_ref = ast->Idents.get(name_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004397 DeclarationName decl_name(&ident_ref);
4398
4399 CXXBasePaths paths;
4400 if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember,
4401 decl_name.getAsOpaquePtr(),
4402 paths))
4403 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004404 CXXBasePaths::const_paths_iterator path, path_end = paths.end();
4405 for (path = paths.begin(); path != path_end; ++path)
4406 {
4407 const size_t num_path_elements = path->size();
4408 for (size_t e=0; e<num_path_elements; ++e)
4409 {
4410 CXXBasePathElement elem = (*path)[e];
4411
4412 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
4413 if (child_idx == UINT32_MAX)
4414 {
4415 child_indexes.clear();
4416 return 0;
4417 }
4418 else
4419 {
4420 child_indexes.push_back (child_idx);
4421 parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl());
4422 }
4423 }
4424 DeclContext::lookup_iterator named_decl_pos;
4425 for (named_decl_pos = path->Decls.first;
4426 named_decl_pos != path->Decls.second && parent_record_decl;
4427 ++named_decl_pos)
4428 {
4429 //printf ("path[%zu] = %s\n", child_indexes.size(), (*named_decl_pos)->getNameAsCString());
4430
4431 child_idx = GetIndexForRecordChild (parent_record_decl, *named_decl_pos, omit_empty_base_classes);
4432 if (child_idx == UINT32_MAX)
4433 {
4434 child_indexes.clear();
4435 return 0;
4436 }
4437 else
4438 {
4439 child_indexes.push_back (child_idx);
4440 }
4441 }
4442 }
4443 return child_indexes.size();
4444 }
4445 }
4446
4447 }
4448 break;
4449
Greg Clayton9e409562010-07-28 02:04:09 +00004450 case clang::Type::ObjCObject:
4451 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004452 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004453 {
4454 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004455 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004456 assert (objc_class_type);
4457 if (objc_class_type)
4458 {
4459 uint32_t child_idx = 0;
4460 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4461
4462 if (class_interface_decl)
4463 {
4464 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4465 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4466
Greg Clayton6ba78152010-09-18 02:11:07 +00004467 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00004468 {
4469 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4470
4471 if (ivar_decl->getName().equals (name_sref))
4472 {
4473 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4474 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4475 ++child_idx;
4476
4477 child_indexes.push_back (child_idx);
4478 return child_indexes.size();
4479 }
4480 }
4481
4482 if (superclass_interface_decl)
4483 {
4484 // The super class index is always zero for ObjC classes,
4485 // so we push it onto the child indexes in case we find
4486 // an ivar in our superclass...
4487 child_indexes.push_back (0);
4488
Greg Clayton6beaaa62011-01-17 03:46:26 +00004489 if (GetIndexOfChildMemberWithName (ast,
4490 ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00004491 name,
4492 omit_empty_base_classes,
4493 child_indexes))
4494 {
4495 // We did find an ivar in a superclass so just
4496 // return the results!
4497 return child_indexes.size();
4498 }
4499
4500 // We didn't find an ivar matching "name" in our
4501 // superclass, pop the superclass zero index that
4502 // we pushed on above.
4503 child_indexes.pop_back();
4504 }
4505 }
4506 }
4507 }
4508 break;
4509
4510 case clang::Type::ObjCObjectPointer:
4511 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004512 return GetIndexOfChildMemberWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004513 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4514 name,
4515 omit_empty_base_classes,
4516 child_indexes);
4517 }
4518 break;
4519
4520
Greg Claytone1a916a2010-07-21 22:12:05 +00004521 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004522 {
4523// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4524// const uint64_t element_count = array->getSize().getLimitedValue();
4525//
4526// if (idx < element_count)
4527// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004528// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004529//
4530// char element_name[32];
4531// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4532//
4533// child_name.assign(element_name);
4534// assert(field_type_info.first % 8 == 0);
4535// child_byte_size = field_type_info.first / 8;
4536// child_byte_offset = idx * child_byte_size;
4537// return array->getElementType().getAsOpaquePtr();
4538// }
4539 }
4540 break;
4541
Greg Claytone1a916a2010-07-21 22:12:05 +00004542// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004543// {
4544// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4545// QualType pointee_type = mem_ptr_type->getPointeeType();
4546//
4547// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4548// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004549// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004550// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4551// name);
4552// }
4553// }
4554// break;
4555//
Greg Claytone1a916a2010-07-21 22:12:05 +00004556 case clang::Type::LValueReference:
4557 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004558 {
Sean Callanan78e37602011-01-27 04:42:51 +00004559 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004560 QualType pointee_type = reference_type->getPointeeType();
4561
4562 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4563 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004564 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004565 reference_type->getPointeeType().getAsOpaquePtr(),
4566 name,
4567 omit_empty_base_classes,
4568 child_indexes);
4569 }
4570 }
4571 break;
4572
Greg Claytone1a916a2010-07-21 22:12:05 +00004573 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004574 {
Sean Callanan78e37602011-01-27 04:42:51 +00004575 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004576 QualType pointee_type = pointer_type->getPointeeType();
4577
4578 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4579 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004580 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004581 pointer_type->getPointeeType().getAsOpaquePtr(),
4582 name,
4583 omit_empty_base_classes,
4584 child_indexes);
4585 }
4586 else
4587 {
4588// if (parent_name)
4589// {
4590// child_name.assign(1, '*');
4591// child_name += parent_name;
4592// }
4593//
4594// // We have a pointer to an simple type
4595// if (idx == 0)
4596// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004597// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004598// assert(clang_type_info.first % 8 == 0);
4599// child_byte_size = clang_type_info.first / 8;
4600// child_byte_offset = 0;
4601// return pointee_type.getAsOpaquePtr();
4602// }
4603 }
4604 }
4605 break;
4606
Greg Claytone1a916a2010-07-21 22:12:05 +00004607 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00004608 return GetIndexOfChildMemberWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00004609 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004610 name,
4611 omit_empty_base_classes,
4612 child_indexes);
4613
4614 default:
4615 break;
4616 }
4617 }
4618 return 0;
4619}
4620
4621
4622// Get the index of the child of "clang_type" whose name matches. This function
4623// doesn't descend into the children, but only looks one level deep and name
4624// matches can include base class names.
4625
4626uint32_t
4627ClangASTContext::GetIndexOfChildWithName
4628(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004629 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004630 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004631 const char *name,
4632 bool omit_empty_base_classes
4633)
4634{
4635 if (clang_type && name && name[0])
4636 {
4637 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00004638
Greg Clayton737b9322010-09-13 03:32:57 +00004639 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Clayton9e409562010-07-28 02:04:09 +00004640
Greg Clayton737b9322010-09-13 03:32:57 +00004641 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004642 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004643 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004644 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004645 {
4646 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4647 const RecordDecl *record_decl = record_type->getDecl();
4648
4649 assert(record_decl);
4650 uint32_t child_idx = 0;
4651
4652 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4653
4654 if (cxx_record_decl)
4655 {
4656 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4657 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4658 base_class != base_class_end;
4659 ++base_class)
4660 {
4661 // Skip empty base classes
4662 CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4663 if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false)
4664 continue;
4665
Greg Clayton84db9102012-03-26 23:03:23 +00004666 std::string base_class_type_name (ClangASTType::GetTypeNameForQualType(ast, base_class->getType()));
Greg Claytone3055942011-06-30 02:28:26 +00004667 if (base_class_type_name.compare (name) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004668 return child_idx;
4669 ++child_idx;
4670 }
4671 }
4672
4673 // Try and find a field that matches NAME
4674 RecordDecl::field_iterator field, field_end;
4675 StringRef name_sref(name);
4676 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4677 field != field_end;
4678 ++field, ++child_idx)
4679 {
4680 if (field->getName().equals (name_sref))
4681 return child_idx;
4682 }
4683
4684 }
4685 break;
4686
Greg Clayton9e409562010-07-28 02:04:09 +00004687 case clang::Type::ObjCObject:
4688 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004689 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004690 {
4691 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004692 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004693 assert (objc_class_type);
4694 if (objc_class_type)
4695 {
4696 uint32_t child_idx = 0;
4697 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4698
4699 if (class_interface_decl)
4700 {
4701 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4702 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4703
4704 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
4705 {
4706 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4707
4708 if (ivar_decl->getName().equals (name_sref))
4709 {
4710 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4711 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4712 ++child_idx;
4713
4714 return child_idx;
4715 }
4716 }
4717
4718 if (superclass_interface_decl)
4719 {
4720 if (superclass_interface_decl->getName().equals (name_sref))
4721 return 0;
4722 }
4723 }
4724 }
4725 }
4726 break;
4727
4728 case clang::Type::ObjCObjectPointer:
4729 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004730 return GetIndexOfChildWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004731 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4732 name,
4733 omit_empty_base_classes);
4734 }
4735 break;
4736
Greg Claytone1a916a2010-07-21 22:12:05 +00004737 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004738 {
4739// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4740// const uint64_t element_count = array->getSize().getLimitedValue();
4741//
4742// if (idx < element_count)
4743// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004744// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004745//
4746// char element_name[32];
4747// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4748//
4749// child_name.assign(element_name);
4750// assert(field_type_info.first % 8 == 0);
4751// child_byte_size = field_type_info.first / 8;
4752// child_byte_offset = idx * child_byte_size;
4753// return array->getElementType().getAsOpaquePtr();
4754// }
4755 }
4756 break;
4757
Greg Claytone1a916a2010-07-21 22:12:05 +00004758// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004759// {
4760// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4761// QualType pointee_type = mem_ptr_type->getPointeeType();
4762//
4763// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4764// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004765// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004766// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4767// name);
4768// }
4769// }
4770// break;
4771//
Greg Claytone1a916a2010-07-21 22:12:05 +00004772 case clang::Type::LValueReference:
4773 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004774 {
Sean Callanan78e37602011-01-27 04:42:51 +00004775 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004776 QualType pointee_type = reference_type->getPointeeType();
4777
4778 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4779 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004780 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004781 reference_type->getPointeeType().getAsOpaquePtr(),
4782 name,
4783 omit_empty_base_classes);
4784 }
4785 }
4786 break;
4787
Greg Claytone1a916a2010-07-21 22:12:05 +00004788 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004789 {
Sean Callanan78e37602011-01-27 04:42:51 +00004790 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004791 QualType pointee_type = pointer_type->getPointeeType();
4792
4793 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4794 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004795 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004796 pointer_type->getPointeeType().getAsOpaquePtr(),
4797 name,
4798 omit_empty_base_classes);
4799 }
4800 else
4801 {
4802// if (parent_name)
4803// {
4804// child_name.assign(1, '*');
4805// child_name += parent_name;
4806// }
4807//
4808// // We have a pointer to an simple type
4809// if (idx == 0)
4810// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004811// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004812// assert(clang_type_info.first % 8 == 0);
4813// child_byte_size = clang_type_info.first / 8;
4814// child_byte_offset = 0;
4815// return pointee_type.getAsOpaquePtr();
4816// }
4817 }
4818 }
4819 break;
4820
Greg Claytone1a916a2010-07-21 22:12:05 +00004821 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00004822 return GetIndexOfChildWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00004823 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004824 name,
4825 omit_empty_base_classes);
4826
4827 default:
4828 break;
4829 }
4830 }
4831 return UINT32_MAX;
4832}
4833
4834#pragma mark TagType
4835
4836bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00004837ClangASTContext::SetTagTypeKind (clang_type_t tag_clang_type, int kind)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004838{
4839 if (tag_clang_type)
4840 {
4841 QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00004842 const clang::Type *clang_type = tag_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004843 if (clang_type)
4844 {
Sean Callanan78e37602011-01-27 04:42:51 +00004845 const TagType *tag_type = dyn_cast<TagType>(clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004846 if (tag_type)
4847 {
4848 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl());
4849 if (tag_decl)
4850 {
4851 tag_decl->setTagKind ((TagDecl::TagKind)kind);
4852 return true;
4853 }
4854 }
4855 }
4856 }
4857 return false;
4858}
4859
4860
4861#pragma mark DeclContext Functions
4862
4863DeclContext *
Greg Clayton1be10fc2010-09-29 01:12:09 +00004864ClangASTContext::GetDeclContextForType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004865{
4866 if (clang_type == NULL)
4867 return NULL;
4868
4869 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00004870 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4871 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004872 {
Sean Callanancc427fa2011-07-30 02:42:06 +00004873 case clang::Type::UnaryTransform: break;
Greg Clayton9e409562010-07-28 02:04:09 +00004874 case clang::Type::FunctionNoProto: break;
4875 case clang::Type::FunctionProto: break;
4876 case clang::Type::IncompleteArray: break;
4877 case clang::Type::VariableArray: break;
4878 case clang::Type::ConstantArray: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00004879 case clang::Type::DependentSizedArray: break;
Greg Clayton9e409562010-07-28 02:04:09 +00004880 case clang::Type::ExtVector: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00004881 case clang::Type::DependentSizedExtVector: break;
Greg Clayton9e409562010-07-28 02:04:09 +00004882 case clang::Type::Vector: break;
4883 case clang::Type::Builtin: break;
4884 case clang::Type::BlockPointer: break;
4885 case clang::Type::Pointer: break;
4886 case clang::Type::LValueReference: break;
4887 case clang::Type::RValueReference: break;
4888 case clang::Type::MemberPointer: break;
4889 case clang::Type::Complex: break;
4890 case clang::Type::ObjCObject: break;
4891 case clang::Type::ObjCInterface: return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface();
4892 case clang::Type::ObjCObjectPointer: return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr());
4893 case clang::Type::Record: return cast<RecordType>(qual_type)->getDecl();
4894 case clang::Type::Enum: return cast<EnumType>(qual_type)->getDecl();
Sean Callanan48114472010-12-13 01:26:27 +00004895 case clang::Type::Typedef: return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00004896 case clang::Type::Elaborated: return ClangASTContext::GetDeclContextForType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004897 case clang::Type::TypeOfExpr: break;
4898 case clang::Type::TypeOf: break;
4899 case clang::Type::Decltype: break;
4900 //case clang::Type::QualifiedName: break;
4901 case clang::Type::TemplateSpecialization: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00004902 case clang::Type::DependentTemplateSpecialization: break;
4903 case clang::Type::TemplateTypeParm: break;
4904 case clang::Type::SubstTemplateTypeParm: break;
4905 case clang::Type::SubstTemplateTypeParmPack:break;
4906 case clang::Type::PackExpansion: break;
4907 case clang::Type::UnresolvedUsing: break;
4908 case clang::Type::Paren: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00004909 case clang::Type::Attributed: break;
4910 case clang::Type::Auto: break;
4911 case clang::Type::InjectedClassName: break;
4912 case clang::Type::DependentName: break;
Greg Claytonea3e7d52011-10-08 00:49:15 +00004913 case clang::Type::Atomic: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004914 }
4915 // No DeclContext in this type...
4916 return NULL;
4917}
4918
4919#pragma mark Namespace Declarations
4920
4921NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00004922ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004923{
Greg Clayton030a2042011-10-14 21:34:45 +00004924 NamespaceDecl *namespace_decl = NULL;
Greg Clayton9d3d6882011-10-31 23:51:19 +00004925 ASTContext *ast = getASTContext();
4926 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
4927 if (decl_ctx == NULL)
4928 decl_ctx = translation_unit_decl;
4929
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004930 if (name)
4931 {
Greg Clayton030a2042011-10-14 21:34:45 +00004932 IdentifierInfo &identifier_info = ast->Idents.get(name);
4933 DeclarationName decl_name (&identifier_info);
4934 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
4935 for (clang::DeclContext::lookup_iterator pos = result.first, end = result.second; pos != end; ++pos)
4936 {
4937 namespace_decl = dyn_cast<clang::NamespaceDecl>(*pos);
4938 if (namespace_decl)
4939 return namespace_decl;
4940 }
4941
Sean Callanan5b26f272012-02-04 08:49:35 +00004942 namespace_decl = NamespaceDecl::Create(*ast,
4943 decl_ctx,
4944 false,
4945 SourceLocation(),
4946 SourceLocation(),
4947 &identifier_info,
4948 NULL);
Greg Clayton030a2042011-10-14 21:34:45 +00004949
Greg Clayton9d3d6882011-10-31 23:51:19 +00004950 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004951 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004952 else
4953 {
4954 if (decl_ctx == translation_unit_decl)
4955 {
4956 namespace_decl = translation_unit_decl->getAnonymousNamespace();
4957 if (namespace_decl)
4958 return namespace_decl;
4959
Sean Callanan5b26f272012-02-04 08:49:35 +00004960 namespace_decl = NamespaceDecl::Create(*ast,
4961 decl_ctx,
4962 false,
4963 SourceLocation(),
4964 SourceLocation(),
4965 NULL,
4966 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00004967 translation_unit_decl->setAnonymousNamespace (namespace_decl);
4968 translation_unit_decl->addDecl (namespace_decl);
4969 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
4970 }
4971 else
4972 {
4973 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
4974 if (parent_namespace_decl)
4975 {
4976 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
4977 if (namespace_decl)
4978 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00004979 namespace_decl = NamespaceDecl::Create(*ast,
4980 decl_ctx,
4981 false,
4982 SourceLocation(),
4983 SourceLocation(),
4984 NULL,
4985 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00004986 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
4987 parent_namespace_decl->addDecl (namespace_decl);
4988 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
4989 }
4990 else
4991 {
4992 // BAD!!!
4993 }
4994 }
4995
4996
4997 if (namespace_decl)
4998 {
4999 // If we make it here, we are creating the anonymous namespace decl
5000 // for the first time, so we need to do the using directive magic
5001 // like SEMA does
5002 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast,
5003 decl_ctx,
5004 SourceLocation(),
5005 SourceLocation(),
5006 NestedNameSpecifierLoc(),
5007 SourceLocation(),
5008 namespace_decl,
5009 decl_ctx);
5010 using_directive_decl->setImplicit();
5011 decl_ctx->addDecl(using_directive_decl);
5012 }
5013 }
5014#ifdef LLDB_CONFIGURATION_DEBUG
5015 VerifyDecl(namespace_decl);
5016#endif
Greg Clayton030a2042011-10-14 21:34:45 +00005017 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005018}
5019
5020
5021#pragma mark Function Types
5022
5023FunctionDecl *
Greg Clayton147e1fa2011-10-14 22:47:18 +00005024ClangASTContext::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 +00005025{
Greg Clayton147e1fa2011-10-14 22:47:18 +00005026 FunctionDecl *func_decl = NULL;
5027 ASTContext *ast = getASTContext();
5028 if (decl_ctx == NULL)
5029 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005030
Greg Clayton147e1fa2011-10-14 22:47:18 +00005031 if (name && name[0])
5032 {
5033 func_decl = FunctionDecl::Create (*ast,
5034 decl_ctx,
5035 SourceLocation(),
5036 SourceLocation(),
5037 DeclarationName (&ast->Idents.get(name)),
5038 QualType::getFromOpaquePtr(function_clang_type),
5039 NULL,
5040 (FunctionDecl::StorageClass)storage,
5041 (FunctionDecl::StorageClass)storage,
5042 is_inline);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005043 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00005044 else
5045 {
5046 func_decl = FunctionDecl::Create (*ast,
5047 decl_ctx,
5048 SourceLocation(),
5049 SourceLocation(),
5050 DeclarationName (),
5051 QualType::getFromOpaquePtr(function_clang_type),
5052 NULL,
5053 (FunctionDecl::StorageClass)storage,
5054 (FunctionDecl::StorageClass)storage,
5055 is_inline);
5056 }
5057 if (func_decl)
5058 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005059
5060#ifdef LLDB_CONFIGURATION_DEBUG
5061 VerifyDecl(func_decl);
5062#endif
5063
Greg Clayton147e1fa2011-10-14 22:47:18 +00005064 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005065}
5066
Greg Clayton1be10fc2010-09-29 01:12:09 +00005067clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00005068ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00005069 clang_type_t result_type,
5070 clang_type_t *args,
Sean Callananc81256a2010-09-16 20:40:25 +00005071 unsigned num_args,
5072 bool is_variadic,
5073 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005074{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005075 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005076 std::vector<QualType> qual_type_args;
5077 for (unsigned i=0; i<num_args; ++i)
5078 qual_type_args.push_back (QualType::getFromOpaquePtr(args[i]));
5079
5080 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00005081 FunctionProtoType::ExtProtoInfo proto_info;
5082 proto_info.Variadic = is_variadic;
Sean Callananfb0b7582011-03-15 00:17:19 +00005083 proto_info.ExceptionSpecType = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005084 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00005085 proto_info.RefQualifier = RQ_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005086 proto_info.NumExceptions = 0;
5087 proto_info.Exceptions = NULL;
5088
Greg Clayton147e1fa2011-10-14 22:47:18 +00005089 return ast->getFunctionType (QualType::getFromOpaquePtr(result_type),
5090 qual_type_args.empty() ? NULL : &qual_type_args.front(),
5091 qual_type_args.size(),
5092 proto_info).getAsOpaquePtr(); // NoReturn);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005093}
5094
5095ParmVarDecl *
Greg Clayton1be10fc2010-09-29 01:12:09 +00005096ClangASTContext::CreateParameterDeclaration (const char *name, clang_type_t param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005097{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005098 ASTContext *ast = getASTContext();
5099 assert (ast != NULL);
5100 return ParmVarDecl::Create(*ast,
5101 ast->getTranslationUnitDecl(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005102 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005103 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00005104 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callananc81256a2010-09-16 20:40:25 +00005105 QualType::getFromOpaquePtr(param_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005106 NULL,
5107 (VarDecl::StorageClass)storage,
5108 (VarDecl::StorageClass)storage,
5109 0);
5110}
5111
5112void
5113ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
5114{
5115 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00005116 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005117}
5118
5119
5120#pragma mark Array Types
5121
Greg Clayton1be10fc2010-09-29 01:12:09 +00005122clang_type_t
5123ClangASTContext::CreateArrayType (clang_type_t element_type, size_t element_count, uint32_t bit_stride)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005124{
5125 if (element_type)
5126 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00005127 ASTContext *ast = getASTContext();
5128 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005129 llvm::APInt ap_element_count (64, element_count);
Greg Clayton6beaaa62011-01-17 03:46:26 +00005130 return ast->getConstantArrayType(QualType::getFromOpaquePtr(element_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005131 ap_element_count,
5132 ArrayType::Normal,
5133 0).getAsOpaquePtr(); // ElemQuals
5134 }
5135 return NULL;
5136}
5137
5138
5139#pragma mark TagDecl
5140
5141bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005142ClangASTContext::StartTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005143{
5144 if (clang_type)
5145 {
5146 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00005147 const clang::Type *t = qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005148 if (t)
5149 {
Sean Callanan78e37602011-01-27 04:42:51 +00005150 const TagType *tag_type = dyn_cast<TagType>(t);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005151 if (tag_type)
5152 {
5153 TagDecl *tag_decl = tag_type->getDecl();
5154 if (tag_decl)
5155 {
5156 tag_decl->startDefinition();
5157 return true;
5158 }
5159 }
Sean Callanan5b26f272012-02-04 08:49:35 +00005160
5161 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(t);
5162 if (object_type)
5163 {
5164 ObjCInterfaceDecl *interface_decl = object_type->getInterface();
5165 if (interface_decl)
5166 {
5167 interface_decl->startDefinition();
5168 return true;
5169 }
5170 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005171 }
5172 }
5173 return false;
5174}
5175
5176bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005177ClangASTContext::CompleteTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005178{
5179 if (clang_type)
5180 {
5181 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton14372242010-09-29 03:44:17 +00005182
5183 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5184
5185 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005186 {
Greg Clayton14372242010-09-29 03:44:17 +00005187 cxx_record_decl->completeDefinition();
5188
5189 return true;
5190 }
5191
5192 const EnumType *enum_type = dyn_cast<EnumType>(qual_type.getTypePtr());
5193
5194 if (enum_type)
5195 {
5196 EnumDecl *enum_decl = enum_type->getDecl();
5197
5198 if (enum_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005199 {
Greg Clayton14372242010-09-29 03:44:17 +00005200 /// TODO This really needs to be fixed.
5201
5202 unsigned NumPositiveBits = 1;
5203 unsigned NumNegativeBits = 0;
5204
Greg Clayton6beaaa62011-01-17 03:46:26 +00005205 ASTContext *ast = getASTContext();
Greg Claytone02b8502010-10-12 04:29:14 +00005206
5207 QualType promotion_qual_type;
5208 // If the enum integer type is less than an integer in bit width,
5209 // then we must promote it to an integer size.
Greg Clayton6beaaa62011-01-17 03:46:26 +00005210 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
Greg Claytone02b8502010-10-12 04:29:14 +00005211 {
5212 if (enum_decl->getIntegerType()->isSignedIntegerType())
Greg Clayton6beaaa62011-01-17 03:46:26 +00005213 promotion_qual_type = ast->IntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005214 else
Greg Clayton6beaaa62011-01-17 03:46:26 +00005215 promotion_qual_type = ast->UnsignedIntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005216 }
5217 else
5218 promotion_qual_type = enum_decl->getIntegerType();
5219
5220 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
Greg Clayton14372242010-09-29 03:44:17 +00005221 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005222 }
5223 }
5224 }
5225 return false;
5226}
5227
5228
5229#pragma mark Enumeration Types
5230
Greg Clayton1be10fc2010-09-29 01:12:09 +00005231clang_type_t
Greg Claytonca512b32011-01-14 04:54:56 +00005232ClangASTContext::CreateEnumerationType
5233(
5234 const char *name,
5235 DeclContext *decl_ctx,
5236 const Declaration &decl,
5237 clang_type_t integer_qual_type
5238)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005239{
5240 // TODO: Do something intelligent with the Declaration object passed in
5241 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005242 ASTContext *ast = getASTContext();
5243 assert (ast != NULL);
Greg Claytone02b8502010-10-12 04:29:14 +00005244
5245 // TODO: ask about these...
5246// const bool IsScoped = false;
5247// const bool IsFixed = false;
5248
Greg Clayton6beaaa62011-01-17 03:46:26 +00005249 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00005250 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00005251 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00005252 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005253 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callanan48114472010-12-13 01:26:27 +00005254 NULL,
5255 false, // IsScoped
5256 false, // IsScopedUsingClassTag
5257 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00005258
5259
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005260 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00005261 {
5262 // TODO: check if we should be setting the promotion type too?
5263 enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00005264
5265 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
5266
Greg Clayton6beaaa62011-01-17 03:46:26 +00005267 return ast->getTagDeclType(enum_decl).getAsOpaquePtr();
Greg Clayton83ff3892010-09-12 23:17:56 +00005268 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005269 return NULL;
5270}
5271
Greg Clayton1be10fc2010-09-29 01:12:09 +00005272clang_type_t
5273ClangASTContext::GetEnumerationIntegerType (clang_type_t enum_clang_type)
5274{
5275 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5276
Sean Callanan78e37602011-01-27 04:42:51 +00005277 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Greg Clayton1be10fc2010-09-29 01:12:09 +00005278 if (clang_type)
5279 {
5280 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5281 if (enum_type)
5282 {
5283 EnumDecl *enum_decl = enum_type->getDecl();
5284 if (enum_decl)
5285 return enum_decl->getIntegerType().getAsOpaquePtr();
5286 }
5287 }
5288 return NULL;
5289}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005290bool
5291ClangASTContext::AddEnumerationValueToEnumerationType
5292(
Greg Clayton1be10fc2010-09-29 01:12:09 +00005293 clang_type_t enum_clang_type,
5294 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005295 const Declaration &decl,
5296 const char *name,
5297 int64_t enum_value,
5298 uint32_t enum_value_bit_size
5299)
5300{
5301 if (enum_clang_type && enumerator_clang_type && name)
5302 {
5303 // TODO: Do something intelligent with the Declaration object passed in
5304 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005305 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005306 IdentifierTable *identifier_table = getIdentifierTable();
5307
Greg Clayton6beaaa62011-01-17 03:46:26 +00005308 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005309 assert (identifier_table != NULL);
5310 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5311
Sean Callanan78e37602011-01-27 04:42:51 +00005312 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005313 if (clang_type)
5314 {
5315 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5316
5317 if (enum_type)
5318 {
5319 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, false);
5320 enum_llvm_apsint = enum_value;
5321 EnumConstantDecl *enumerator_decl =
Greg Clayton6beaaa62011-01-17 03:46:26 +00005322 EnumConstantDecl::Create (*ast,
5323 enum_type->getDecl(),
5324 SourceLocation(),
5325 name ? &identifier_table->get(name) : NULL, // Identifier
5326 QualType::getFromOpaquePtr(enumerator_clang_type),
5327 NULL,
5328 enum_llvm_apsint);
5329
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005330 if (enumerator_decl)
5331 {
5332 enum_type->getDecl()->addDecl(enumerator_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005333
5334#ifdef LLDB_CONFIGURATION_DEBUG
5335 VerifyDecl(enumerator_decl);
5336#endif
5337
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005338 return true;
5339 }
5340 }
5341 }
5342 }
5343 return false;
5344}
5345
5346#pragma mark Pointers & References
5347
Greg Clayton1be10fc2010-09-29 01:12:09 +00005348clang_type_t
5349ClangASTContext::CreatePointerType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005350{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005351 return CreatePointerType (getASTContext(), clang_type);
5352}
5353
5354clang_type_t
5355ClangASTContext::CreatePointerType (clang::ASTContext *ast, clang_type_t clang_type)
5356{
5357 if (ast && clang_type)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005358 {
5359 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5360
Greg Clayton737b9322010-09-13 03:32:57 +00005361 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5362 switch (type_class)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005363 {
5364 case clang::Type::ObjCObject:
5365 case clang::Type::ObjCInterface:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005366 return ast->getObjCObjectPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005367
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005368 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005369 return ast->getPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005370 }
5371 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005372 return NULL;
5373}
5374
Greg Clayton1be10fc2010-09-29 01:12:09 +00005375clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005376ClangASTContext::CreateLValueReferenceType (clang::ASTContext *ast,
5377 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005378{
5379 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005380 return ast->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005381 return NULL;
5382}
5383
Greg Clayton1be10fc2010-09-29 01:12:09 +00005384clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005385ClangASTContext::CreateRValueReferenceType (clang::ASTContext *ast,
5386 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005387{
5388 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005389 return ast->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005390 return NULL;
5391}
5392
Greg Clayton1be10fc2010-09-29 01:12:09 +00005393clang_type_t
5394ClangASTContext::CreateMemberPointerType (clang_type_t clang_pointee_type, clang_type_t clang_class_type)
Greg Clayton9b81a312010-06-12 01:20:30 +00005395{
5396 if (clang_pointee_type && clang_pointee_type)
5397 return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type),
5398 QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr();
5399 return NULL;
5400}
5401
Greg Clayton1a65ae12011-01-25 23:55:37 +00005402uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005403ClangASTContext::GetPointerBitSize ()
5404{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005405 ASTContext *ast = getASTContext();
5406 return ast->getTypeSize(ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005407}
5408
5409bool
Greg Clayton219cf312012-03-30 00:51:13 +00005410ClangASTContext::IsPossibleDynamicType (clang::ASTContext *ast,
5411 clang_type_t clang_type,
5412 clang_type_t *dynamic_pointee_type,
5413 bool check_cplusplus,
5414 bool check_objc)
Greg Claytondea8cb42011-06-29 22:09:02 +00005415{
5416 QualType pointee_qual_type;
5417 if (clang_type)
5418 {
5419 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5420 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5421 bool success = false;
5422 switch (type_class)
5423 {
5424 case clang::Type::Builtin:
Greg Clayton219cf312012-03-30 00:51:13 +00005425 if (check_objc && cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
Greg Claytondea8cb42011-06-29 22:09:02 +00005426 {
5427 if (dynamic_pointee_type)
5428 *dynamic_pointee_type = clang_type;
5429 return true;
5430 }
5431 break;
5432
5433 case clang::Type::ObjCObjectPointer:
Greg Clayton219cf312012-03-30 00:51:13 +00005434 if (check_objc)
5435 {
5436 if (dynamic_pointee_type)
5437 *dynamic_pointee_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5438 return true;
5439 }
5440 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005441
5442 case clang::Type::Pointer:
5443 pointee_qual_type = cast<PointerType>(qual_type)->getPointeeType();
5444 success = true;
5445 break;
5446
5447 case clang::Type::LValueReference:
5448 case clang::Type::RValueReference:
5449 pointee_qual_type = cast<ReferenceType>(qual_type)->getPointeeType();
5450 success = true;
5451 break;
5452
5453 case clang::Type::Typedef:
Greg Claytonaffb03b2011-07-08 18:27:39 +00005454 return ClangASTContext::IsPossibleDynamicType (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), dynamic_pointee_type);
Sean Callanan912855f2011-08-11 23:56:13 +00005455
5456 case clang::Type::Elaborated:
5457 return ClangASTContext::IsPossibleDynamicType (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), dynamic_pointee_type);
5458
Greg Claytondea8cb42011-06-29 22:09:02 +00005459 default:
5460 break;
5461 }
5462
5463 if (success)
5464 {
5465 // Check to make sure what we are pointing too is a possible dynamic C++ type
5466 // We currently accept any "void *" (in case we have a class that has been
5467 // watered down to an opaque pointer) and virtual C++ classes.
5468 const clang::Type::TypeClass pointee_type_class = pointee_qual_type->getTypeClass();
5469 switch (pointee_type_class)
5470 {
5471 case clang::Type::Builtin:
5472 switch (cast<clang::BuiltinType>(pointee_qual_type)->getKind())
5473 {
5474 case clang::BuiltinType::UnknownAny:
5475 case clang::BuiltinType::Void:
5476 if (dynamic_pointee_type)
5477 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5478 return true;
5479
5480 case clang::BuiltinType::NullPtr:
5481 case clang::BuiltinType::Bool:
5482 case clang::BuiltinType::Char_U:
5483 case clang::BuiltinType::UChar:
5484 case clang::BuiltinType::WChar_U:
5485 case clang::BuiltinType::Char16:
5486 case clang::BuiltinType::Char32:
5487 case clang::BuiltinType::UShort:
5488 case clang::BuiltinType::UInt:
5489 case clang::BuiltinType::ULong:
5490 case clang::BuiltinType::ULongLong:
5491 case clang::BuiltinType::UInt128:
5492 case clang::BuiltinType::Char_S:
5493 case clang::BuiltinType::SChar:
5494 case clang::BuiltinType::WChar_S:
5495 case clang::BuiltinType::Short:
5496 case clang::BuiltinType::Int:
5497 case clang::BuiltinType::Long:
5498 case clang::BuiltinType::LongLong:
5499 case clang::BuiltinType::Int128:
5500 case clang::BuiltinType::Float:
5501 case clang::BuiltinType::Double:
5502 case clang::BuiltinType::LongDouble:
5503 case clang::BuiltinType::Dependent:
5504 case clang::BuiltinType::Overload:
5505 case clang::BuiltinType::ObjCId:
5506 case clang::BuiltinType::ObjCClass:
5507 case clang::BuiltinType::ObjCSel:
5508 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00005509 case clang::BuiltinType::Half:
5510 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00005511 case clang::BuiltinType::PseudoObject:
Greg Claytondea8cb42011-06-29 22:09:02 +00005512 break;
5513 }
5514 break;
5515
5516 case clang::Type::Record:
Greg Clayton219cf312012-03-30 00:51:13 +00005517 if (check_cplusplus)
Greg Claytondea8cb42011-06-29 22:09:02 +00005518 {
5519 CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
5520 if (cxx_record_decl)
5521 {
Greg Claytone26928c2012-03-22 22:23:17 +00005522 // Do NOT complete the type here like we used to do
5523 // otherwise EVERY "class *" variable we have will try
5524 // to fully complete itself and this will take a lot of
5525 // time, memory and slow down debugging. If we have a complete
5526 // type, then answer the question definitively, else we
5527 // just say that a C++ class can possibly be dynamic...
Greg Clayton219cf312012-03-30 00:51:13 +00005528 if (cxx_record_decl->isCompleteDefinition())
Greg Claytondea8cb42011-06-29 22:09:02 +00005529 {
5530 success = cxx_record_decl->isDynamicClass();
5531 }
5532 else
5533 {
5534 // We failed to get the complete type, so we have to
5535 // treat this as a void * which we might possibly be
5536 // able to complete
5537 success = true;
5538 }
5539 if (success)
5540 {
5541 if (dynamic_pointee_type)
5542 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5543 return true;
5544 }
5545 }
5546 }
5547 break;
5548
5549 case clang::Type::ObjCObject:
5550 case clang::Type::ObjCInterface:
Greg Clayton219cf312012-03-30 00:51:13 +00005551 if (check_objc)
5552 {
5553 if (dynamic_pointee_type)
5554 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5555 return true;
5556 }
5557 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005558
5559 default:
5560 break;
5561 }
5562 }
5563 }
5564 if (dynamic_pointee_type)
5565 *dynamic_pointee_type = NULL;
5566 return false;
5567}
5568
5569
5570bool
Greg Clayton007d5be2011-05-30 00:49:24 +00005571ClangASTContext::IsPossibleCPlusPlusDynamicType (clang::ASTContext *ast, clang_type_t clang_type, clang_type_t *dynamic_pointee_type)
5572{
Greg Clayton219cf312012-03-30 00:51:13 +00005573 return IsPossibleDynamicType (ast,
5574 clang_type,
5575 dynamic_pointee_type,
5576 true, // Check for dynamic C++ types
5577 false); // Check for dynamic ObjC types
Greg Clayton007d5be2011-05-30 00:49:24 +00005578}
5579
Sean Callanan98298012011-10-27 19:41:13 +00005580bool
5581ClangASTContext::IsReferenceType (clang_type_t clang_type, clang_type_t *target_type)
5582{
5583 if (clang_type == NULL)
5584 return false;
5585
5586 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5587 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5588
5589 switch (type_class)
5590 {
5591 case clang::Type::LValueReference:
5592 if (target_type)
5593 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5594 return true;
5595 case clang::Type::RValueReference:
5596 if (target_type)
5597 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5598 return true;
5599 case clang::Type::Typedef:
5600 return ClangASTContext::IsReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5601 case clang::Type::Elaborated:
5602 return ClangASTContext::IsReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5603 default:
5604 break;
5605 }
5606
5607 return false;
5608}
Greg Clayton007d5be2011-05-30 00:49:24 +00005609
5610bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005611ClangASTContext::IsPointerOrReferenceType (clang_type_t clang_type, clang_type_t*target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005612{
5613 if (clang_type == NULL)
5614 return false;
5615
5616 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005617 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5618 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005619 {
Sean Callanana2424172010-10-25 00:29:48 +00005620 case clang::Type::Builtin:
5621 switch (cast<clang::BuiltinType>(qual_type)->getKind())
5622 {
5623 default:
5624 break;
5625 case clang::BuiltinType::ObjCId:
5626 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00005627 return true;
5628 }
5629 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00005630 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005631 if (target_type)
5632 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5633 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005634 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005635 if (target_type)
5636 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5637 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005638 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005639 if (target_type)
5640 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5641 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005642 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005643 if (target_type)
5644 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5645 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005646 case clang::Type::LValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005647 if (target_type)
5648 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5649 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005650 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005651 if (target_type)
5652 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5653 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005654 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00005655 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00005656 case clang::Type::Elaborated:
5657 return ClangASTContext::IsPointerOrReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005658 default:
5659 break;
5660 }
5661 return false;
5662}
5663
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005664bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005665ClangASTContext::IsIntegerType (clang_type_t clang_type, bool &is_signed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005666{
5667 if (!clang_type)
5668 return false;
5669
5670 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5671 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal());
5672
5673 if (builtin_type)
5674 {
5675 if (builtin_type->isInteger())
Jim Inghamef651602011-12-22 19:12:40 +00005676 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005677 is_signed = builtin_type->isSignedInteger();
Jim Inghamef651602011-12-22 19:12:40 +00005678 return true;
5679 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005680 }
5681
5682 return false;
5683}
5684
5685bool
Greg Claytonaffb03b2011-07-08 18:27:39 +00005686ClangASTContext::IsPointerType (clang_type_t clang_type, clang_type_t *target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005687{
Greg Claytonaffb03b2011-07-08 18:27:39 +00005688 if (target_type)
5689 *target_type = NULL;
5690
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005691 if (clang_type)
5692 {
5693 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005694 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5695 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005696 {
Sean Callanana2424172010-10-25 00:29:48 +00005697 case clang::Type::Builtin:
5698 switch (cast<clang::BuiltinType>(qual_type)->getKind())
5699 {
5700 default:
5701 break;
5702 case clang::BuiltinType::ObjCId:
5703 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00005704 return true;
5705 }
5706 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00005707 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005708 if (target_type)
5709 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5710 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005711 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005712 if (target_type)
5713 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5714 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005715 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005716 if (target_type)
5717 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5718 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005719 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005720 if (target_type)
5721 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5722 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005723 case clang::Type::Typedef:
Greg Claytonaffb03b2011-07-08 18:27:39 +00005724 return ClangASTContext::IsPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), target_type);
Sean Callanan912855f2011-08-11 23:56:13 +00005725 case clang::Type::Elaborated:
5726 return ClangASTContext::IsPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), target_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005727 default:
5728 break;
5729 }
5730 }
5731 return false;
5732}
5733
5734bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005735ClangASTContext::IsFloatingPointType (clang_type_t clang_type, uint32_t &count, bool &is_complex)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005736{
5737 if (clang_type)
5738 {
5739 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5740
5741 if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()))
5742 {
5743 clang::BuiltinType::Kind kind = BT->getKind();
5744 if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble)
5745 {
5746 count = 1;
5747 is_complex = false;
5748 return true;
5749 }
5750 }
5751 else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal()))
5752 {
5753 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex))
5754 {
5755 count = 2;
5756 is_complex = true;
5757 return true;
5758 }
5759 }
5760 else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal()))
5761 {
5762 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex))
5763 {
5764 count = VT->getNumElements();
5765 is_complex = false;
5766 return true;
5767 }
5768 }
5769 }
5770 return false;
5771}
5772
Enrico Granata9fc19442011-07-06 02:13:41 +00005773bool
5774ClangASTContext::IsScalarType (lldb::clang_type_t clang_type)
5775{
5776 bool is_signed;
5777 if (ClangASTContext::IsIntegerType(clang_type, is_signed))
5778 return true;
5779
5780 uint32_t count;
5781 bool is_complex;
5782 return ClangASTContext::IsFloatingPointType(clang_type, count, is_complex) && !is_complex;
5783}
5784
5785bool
5786ClangASTContext::IsPointerToScalarType (lldb::clang_type_t clang_type)
5787{
5788 if (!IsPointerType(clang_type))
5789 return false;
5790
5791 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5792 lldb::clang_type_t pointee_type = qual_type.getTypePtr()->getPointeeType().getAsOpaquePtr();
5793 return IsScalarType(pointee_type);
5794}
5795
5796bool
5797ClangASTContext::IsArrayOfScalarType (lldb::clang_type_t clang_type)
5798{
Sean Callanan0caa21c2012-01-19 23:54:24 +00005799 clang_type = GetAsArrayType(clang_type);
5800
5801 if (clang_type == 0)
Enrico Granata9fc19442011-07-06 02:13:41 +00005802 return false;
5803
5804 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5805 lldb::clang_type_t item_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
5806 return IsScalarType(item_type);
5807}
5808
Greg Clayton8f92f0a2010-10-14 22:52:14 +00005809
5810bool
5811ClangASTContext::GetCXXClassName (clang_type_t clang_type, std::string &class_name)
5812{
5813 if (clang_type)
5814 {
5815 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5816
5817 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5818 if (cxx_record_decl)
5819 {
5820 class_name.assign (cxx_record_decl->getIdentifier()->getNameStart());
5821 return true;
5822 }
5823 }
5824 class_name.clear();
5825 return false;
5826}
5827
5828
Greg Clayton0fffff52010-09-24 05:15:53 +00005829bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005830ClangASTContext::IsCXXClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00005831{
5832 if (clang_type)
5833 {
5834 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5835 if (qual_type->getAsCXXRecordDecl() != NULL)
5836 return true;
5837 }
5838 return false;
5839}
5840
Greg Clayton20568dd2011-10-13 23:13:20 +00005841bool
5842ClangASTContext::IsBeingDefined (lldb::clang_type_t clang_type)
5843{
5844 if (clang_type)
5845 {
5846 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5847 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type);
5848 if (tag_type)
5849 return tag_type->isBeingDefined();
5850 }
5851 return false;
5852}
5853
Greg Clayton0fffff52010-09-24 05:15:53 +00005854bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005855ClangASTContext::IsObjCClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00005856{
5857 if (clang_type)
5858 {
5859 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5860 if (qual_type->isObjCObjectOrInterfaceType())
5861 return true;
5862 }
5863 return false;
5864}
5865
Sean Callanan72772842012-02-22 23:57:45 +00005866bool
5867ClangASTContext::IsObjCObjectPointerType (lldb::clang_type_t clang_type, clang_type_t *class_type)
5868{
5869 if (clang_type)
5870 {
5871 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5872 if (qual_type->isObjCObjectPointerType())
5873 {
5874 if (class_type)
5875 {
5876 *class_type = NULL;
5877
5878 if (!qual_type->isObjCClassType() &&
5879 !qual_type->isObjCIdType())
5880 {
5881 const ObjCObjectPointerType *obj_pointer_type = dyn_cast<ObjCObjectPointerType>(qual_type);
Sean Callanand7dabe22012-03-10 01:59:11 +00005882 if (!obj_pointer_type)
5883 *class_type = NULL;
Sean Callanan1fc91ad2012-03-10 02:00:32 +00005884 else
5885 *class_type = QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr();
Sean Callanan72772842012-02-22 23:57:45 +00005886 }
5887 }
5888 return true;
5889 }
5890 }
5891 return false;
5892}
5893
5894bool
5895ClangASTContext::GetObjCClassName (lldb::clang_type_t clang_type,
5896 std::string &class_name)
5897{
5898 if (!clang_type)
5899 return false;
5900
5901 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(QualType::getFromOpaquePtr(clang_type));
5902 if (!object_type)
5903 return false;
5904
5905 const ObjCInterfaceDecl *interface = object_type->getInterface();
5906 if (!interface)
5907 return false;
5908
5909 class_name = interface->getNameAsString();
5910 return true;
5911}
Greg Clayton0fffff52010-09-24 05:15:53 +00005912
Greg Clayton73b472d2010-10-27 03:32:59 +00005913bool
5914ClangASTContext::IsCharType (clang_type_t clang_type)
5915{
5916 if (clang_type)
5917 return QualType::getFromOpaquePtr(clang_type)->isCharType();
5918 return false;
5919}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005920
5921bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005922ClangASTContext::IsCStringType (clang_type_t clang_type, uint32_t &length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005923{
Greg Clayton73b472d2010-10-27 03:32:59 +00005924 clang_type_t pointee_or_element_clang_type = NULL;
5925 Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, &pointee_or_element_clang_type));
5926
5927 if (pointee_or_element_clang_type == NULL)
5928 return false;
5929
5930 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005931 {
Greg Clayton73b472d2010-10-27 03:32:59 +00005932 QualType pointee_or_element_qual_type (QualType::getFromOpaquePtr (pointee_or_element_clang_type));
5933
5934 if (pointee_or_element_qual_type.getUnqualifiedType()->isCharType())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005935 {
Greg Clayton73b472d2010-10-27 03:32:59 +00005936 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5937 if (type_flags.Test (eTypeIsArray))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005938 {
Greg Clayton73b472d2010-10-27 03:32:59 +00005939 // We know the size of the array and it could be a C string
5940 // since it is an array of characters
5941 length = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
5942 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005943 }
Greg Clayton73b472d2010-10-27 03:32:59 +00005944 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005945 {
Greg Clayton73b472d2010-10-27 03:32:59 +00005946 length = 0;
5947 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005948 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005949
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005950 }
5951 }
5952 return false;
5953}
5954
5955bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005956ClangASTContext::IsFunctionPointerType (clang_type_t clang_type)
Greg Clayton737b9322010-09-13 03:32:57 +00005957{
5958 if (clang_type)
5959 {
5960 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5961
5962 if (qual_type->isFunctionPointerType())
5963 return true;
5964
5965 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5966 switch (type_class)
5967 {
Sean Callananfb0b7582011-03-15 00:17:19 +00005968 default:
5969 break;
Greg Clayton737b9322010-09-13 03:32:57 +00005970 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00005971 return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00005972 case clang::Type::Elaborated:
5973 return ClangASTContext::IsFunctionPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00005974
5975 case clang::Type::LValueReference:
5976 case clang::Type::RValueReference:
5977 {
Sean Callanan78e37602011-01-27 04:42:51 +00005978 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00005979 if (reference_type)
5980 return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr());
5981 }
5982 break;
5983 }
5984 }
5985 return false;
5986}
5987
Greg Clayton73b472d2010-10-27 03:32:59 +00005988size_t
5989ClangASTContext::GetArraySize (clang_type_t clang_type)
5990{
5991 if (clang_type)
5992 {
Greg Claytonef37d68a2011-07-09 17:12:27 +00005993 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
5994 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5995 switch (type_class)
5996 {
5997 case clang::Type::ConstantArray:
5998 {
5999 const ConstantArrayType *array = cast<ConstantArrayType>(QualType::getFromOpaquePtr(clang_type).getTypePtr());
6000 if (array)
6001 return array->getSize().getLimitedValue();
6002 }
6003 break;
6004
6005 case clang::Type::Typedef:
6006 return ClangASTContext::GetArraySize(cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006007
6008 case clang::Type::Elaborated:
6009 return ClangASTContext::GetArraySize(cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00006010
6011 default:
6012 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006013 }
Greg Clayton73b472d2010-10-27 03:32:59 +00006014 }
6015 return 0;
6016}
Greg Clayton737b9322010-09-13 03:32:57 +00006017
Sean Callanan0caa21c2012-01-19 23:54:24 +00006018clang_type_t
6019ClangASTContext::GetAsArrayType (clang_type_t clang_type, clang_type_t*member_type, uint64_t *size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006020{
6021 if (!clang_type)
Sean Callanan0caa21c2012-01-19 23:54:24 +00006022 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006023
6024 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6025
Greg Clayton737b9322010-09-13 03:32:57 +00006026 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6027 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006028 {
Sean Callananfb0b7582011-03-15 00:17:19 +00006029 default:
6030 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006031
Greg Claytone1a916a2010-07-21 22:12:05 +00006032 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006033 if (member_type)
6034 *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6035 if (size)
Greg Claytonac4827f2011-04-01 18:14:08 +00006036 *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
Sean Callanan0caa21c2012-01-19 23:54:24 +00006037 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006038
Greg Claytone1a916a2010-07-21 22:12:05 +00006039 case clang::Type::IncompleteArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006040 if (member_type)
6041 *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6042 if (size)
6043 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006044 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006045
Greg Claytone1a916a2010-07-21 22:12:05 +00006046 case clang::Type::VariableArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006047 if (member_type)
6048 *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6049 if (size)
6050 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006051 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006052
Greg Claytone1a916a2010-07-21 22:12:05 +00006053 case clang::Type::DependentSizedArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006054 if (member_type)
6055 *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6056 if (size)
6057 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006058 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006059
6060 case clang::Type::Typedef:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006061 return ClangASTContext::GetAsArrayType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
6062 member_type,
6063 size);
Sean Callanan912855f2011-08-11 23:56:13 +00006064
6065 case clang::Type::Elaborated:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006066 return ClangASTContext::GetAsArrayType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
6067 member_type,
6068 size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006069 }
Sean Callanan0caa21c2012-01-19 23:54:24 +00006070 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006071}
6072
6073
6074#pragma mark Typedefs
6075
Greg Clayton1be10fc2010-09-29 01:12:09 +00006076clang_type_t
6077ClangASTContext::CreateTypedefType (const char *name, clang_type_t clang_type, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006078{
6079 if (clang_type)
6080 {
6081 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006082 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006083 IdentifierTable *identifier_table = getIdentifierTable();
Greg Clayton6beaaa62011-01-17 03:46:26 +00006084 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006085 assert (identifier_table != NULL);
6086 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00006087 decl_ctx = ast->getTranslationUnitDecl();
6088 TypedefDecl *decl = TypedefDecl::Create (*ast,
6089 decl_ctx,
6090 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00006091 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00006092 name ? &identifier_table->get(name) : NULL, // Identifier
6093 ast->CreateTypeSourceInfo(qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00006094
Greg Clayton147e1fa2011-10-14 22:47:18 +00006095 //decl_ctx->addDecl (decl);
6096
Sean Callanan2652ad22011-01-18 01:03:44 +00006097 decl->setAccess(AS_public); // TODO respect proper access specifier
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006098
6099 // Get a uniqued QualType for the typedef decl type
Greg Clayton6beaaa62011-01-17 03:46:26 +00006100 return ast->getTypedefType (decl).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006101 }
6102 return NULL;
6103}
6104
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006105// Disable this for now since I can't seem to get a nicely formatted float
6106// out of the APFloat class without just getting the float, double or quad
6107// and then using a formatted print on it which defeats the purpose. We ideally
6108// would like to get perfect string values for any kind of float semantics
6109// so we can support remote targets. The code below also requires a patch to
6110// llvm::APInt.
6111//bool
Greg Clayton6beaaa62011-01-17 03:46:26 +00006112//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 +00006113//{
6114// uint32_t count = 0;
6115// bool is_complex = false;
6116// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6117// {
6118// unsigned num_bytes_per_float = byte_size / count;
6119// unsigned num_bits_per_float = num_bytes_per_float * 8;
6120//
6121// float_str.clear();
6122// uint32_t i;
6123// for (i=0; i<count; i++)
6124// {
6125// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
6126// bool is_ieee = false;
6127// APFloat ap_float(ap_int, is_ieee);
6128// char s[1024];
6129// unsigned int hex_digits = 0;
6130// bool upper_case = false;
6131//
6132// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
6133// {
6134// if (i > 0)
6135// float_str.append(", ");
6136// float_str.append(s);
6137// if (i == 1 && is_complex)
6138// float_str.append(1, 'i');
6139// }
6140// }
6141// return !float_str.empty();
6142// }
6143// return false;
6144//}
6145
6146size_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00006147ClangASTContext::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 +00006148{
6149 if (clang_type)
6150 {
6151 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6152 uint32_t count = 0;
6153 bool is_complex = false;
6154 if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6155 {
6156 // TODO: handle complex and vector types
6157 if (count != 1)
6158 return false;
6159
6160 StringRef s_sref(s);
Greg Clayton6beaaa62011-01-17 03:46:26 +00006161 APFloat ap_float(ast->getFloatTypeSemantics(qual_type), s_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006162
Greg Clayton6beaaa62011-01-17 03:46:26 +00006163 const uint64_t bit_size = ast->getTypeSize (qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006164 const uint64_t byte_size = bit_size / 8;
6165 if (dst_size >= byte_size)
6166 {
6167 if (bit_size == sizeof(float)*8)
6168 {
6169 float float32 = ap_float.convertToFloat();
6170 ::memcpy (dst, &float32, byte_size);
6171 return byte_size;
6172 }
6173 else if (bit_size >= 64)
6174 {
6175 llvm::APInt ap_int(ap_float.bitcastToAPInt());
6176 ::memcpy (dst, ap_int.getRawData(), byte_size);
6177 return byte_size;
6178 }
6179 }
6180 }
6181 }
6182 return 0;
6183}
Sean Callanan6fe64b52010-09-17 02:24:29 +00006184
6185unsigned
Greg Clayton1be10fc2010-09-29 01:12:09 +00006186ClangASTContext::GetTypeQualifiers(clang_type_t clang_type)
Sean Callanan6fe64b52010-09-17 02:24:29 +00006187{
6188 assert (clang_type);
6189
6190 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6191
6192 return qual_type.getQualifiers().getCVRQualifiers();
6193}
Greg Clayton6beaaa62011-01-17 03:46:26 +00006194
Sean Callanan3b107b12011-12-03 03:15:28 +00006195uint64_t
6196GetTypeFlags(clang::ASTContext *ast, lldb::clang_type_t clang_type)
6197{
6198 assert (clang_type);
6199
6200 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
6201
6202 if (!external_ast_source)
6203 return 0;
6204
6205 ClangExternalASTSourceCommon *common_ast_source = static_cast<ClangExternalASTSourceCommon*>(external_ast_source);
6206
6207 return common_ast_source->GetMetadata((uintptr_t)clang_type);
6208}
6209
6210void
6211SetTypeFlags(clang::ASTContext *ast, lldb::clang_type_t clang_type, uint64_t flags)
6212{
6213 assert (clang_type);
6214
6215 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
6216
6217 if (!external_ast_source)
6218 return;
6219
6220 ClangExternalASTSourceCommon *common_ast_source = static_cast<ClangExternalASTSourceCommon*>(external_ast_source);
6221
6222 return common_ast_source->SetMetadata((uintptr_t)clang_type, flags);
6223}
6224
Greg Clayton6beaaa62011-01-17 03:46:26 +00006225bool
6226ClangASTContext::GetCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6227{
6228 if (clang_type == NULL)
6229 return false;
6230
Greg Claytonc432c192011-01-20 04:18:48 +00006231 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006232}
6233
6234
6235bool
6236ClangASTContext::GetCompleteType (clang_type_t clang_type)
6237{
6238 return ClangASTContext::GetCompleteType (getASTContext(), clang_type);
6239}
6240
Greg Claytona2721472011-06-25 00:44:06 +00006241bool
Enrico Granata86027e92012-03-24 01:11:14 +00006242ClangASTContext::IsCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6243{
6244 if (clang_type == NULL)
6245 return false;
6246
6247 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type), false); // just check but don't let it actually complete
6248}
6249
6250
6251bool
6252ClangASTContext::IsCompleteType (clang_type_t clang_type)
6253{
6254 return ClangASTContext::IsCompleteType (getASTContext(), clang_type);
6255}
6256
6257bool
Greg Claytona2721472011-06-25 00:44:06 +00006258ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
6259 clang::Decl *decl)
6260{
6261 if (!decl)
6262 return false;
6263
6264 ExternalASTSource *ast_source = ast->getExternalSource();
6265
6266 if (!ast_source)
6267 return false;
6268
6269 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
6270 {
Greg Clayton219cf312012-03-30 00:51:13 +00006271 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006272 return true;
6273
6274 if (!tag_decl->hasExternalLexicalStorage())
6275 return false;
6276
6277 ast_source->CompleteType(tag_decl);
6278
6279 return !tag_decl->getTypeForDecl()->isIncompleteType();
6280 }
6281 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
6282 {
Sean Callanan5b26f272012-02-04 08:49:35 +00006283 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006284 return true;
6285
6286 if (!objc_interface_decl->hasExternalLexicalStorage())
6287 return false;
6288
6289 ast_source->CompleteType(objc_interface_decl);
6290
Sean Callanan5b26f272012-02-04 08:49:35 +00006291 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00006292 }
6293 else
6294 {
6295 return false;
6296 }
6297}
6298
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006299clang::DeclContext *
6300ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
6301{
Sean Callanana87bee82011-08-19 06:19:25 +00006302 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006303}
6304
6305clang::DeclContext *
6306ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
6307{
Sean Callanana87bee82011-08-19 06:19:25 +00006308 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006309}
6310