blob: 132ab60b34a47819190d6d815feaacd7a0dba929 [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{
Greg Clayton70512312012-05-08 01:45:38 +0000625 // target_triple should be something like "x86_64-apple-macosx"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626 if (m_target_info_ap.get() == NULL && !m_target_triple.empty())
Sean Callanan880e6802011-10-07 23:18:13 +0000627 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), *getTargetOptions()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628 return m_target_info_ap.get();
629}
630
631#pragma mark Basic Types
632
633static inline bool
Greg Clayton6beaaa62011-01-17 03:46:26 +0000634QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000636 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637 if (qual_type_bit_size == bit_size)
638 return true;
639 return false;
640}
641
Greg Clayton1be10fc2010-09-29 01:12:09 +0000642clang_type_t
Greg Claytonc86103d2010-08-05 01:57:25 +0000643ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000644{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000645 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646
Greg Clayton6beaaa62011-01-17 03:46:26 +0000647 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648
Greg Clayton6beaaa62011-01-17 03:46:26 +0000649 return GetBuiltinTypeForEncodingAndBitSize (ast, encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650}
651
Greg Clayton1be10fc2010-09-29 01:12:09 +0000652clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000653ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000655 if (!ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000656 return NULL;
657
658 switch (encoding)
659 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000660 case eEncodingInvalid:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000661 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
662 return ast->VoidPtrTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663 break;
664
Greg Claytonc86103d2010-08-05 01:57:25 +0000665 case eEncodingUint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000666 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
667 return ast->UnsignedCharTy.getAsOpaquePtr();
668 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
669 return ast->UnsignedShortTy.getAsOpaquePtr();
670 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
671 return ast->UnsignedIntTy.getAsOpaquePtr();
672 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
673 return ast->UnsignedLongTy.getAsOpaquePtr();
674 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
675 return ast->UnsignedLongLongTy.getAsOpaquePtr();
676 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
677 return ast->UnsignedInt128Ty.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000678 break;
679
Greg Claytonc86103d2010-08-05 01:57:25 +0000680 case eEncodingSint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000681 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
682 return ast->CharTy.getAsOpaquePtr();
683 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
684 return ast->ShortTy.getAsOpaquePtr();
685 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
686 return ast->IntTy.getAsOpaquePtr();
687 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
688 return ast->LongTy.getAsOpaquePtr();
689 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
690 return ast->LongLongTy.getAsOpaquePtr();
691 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
692 return ast->Int128Ty.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000693 break;
694
Greg Claytonc86103d2010-08-05 01:57:25 +0000695 case eEncodingIEEE754:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000696 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
697 return ast->FloatTy.getAsOpaquePtr();
698 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
699 return ast->DoubleTy.getAsOpaquePtr();
700 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
701 return ast->LongDoubleTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702 break;
703
Greg Claytonc86103d2010-08-05 01:57:25 +0000704 case eEncodingVector:
Johnny Chenc79c93a2012-03-07 01:12:24 +0000705 // Sanity check that bit_size is a multiple of 8's.
706 if (bit_size && !(bit_size & 0x7u))
707 return ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8).getAsOpaquePtr();
708 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709 default:
710 break;
711 }
712
713 return NULL;
714}
715
Greg Clayton1be10fc2010-09-29 01:12:09 +0000716clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
718{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000719 ASTContext *ast = getASTContext();
Sean Callanan38d4df52012-04-03 01:10:10 +0000720
721#define streq(a,b) strcmp(a,b) == 0
Greg Clayton6beaaa62011-01-17 03:46:26 +0000722 assert (ast != NULL);
723 if (ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724 {
725 switch (dw_ate)
726 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000727 default:
728 break;
Greg Clayton605684e2011-10-28 23:06:08 +0000729
Sean Callanan38d4df52012-04-03 01:10:10 +0000730 case DW_ATE_address:
731 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
732 return ast->VoidPtrTy.getAsOpaquePtr();
733 break;
734
735 case DW_ATE_boolean:
736 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
737 return ast->BoolTy.getAsOpaquePtr();
738 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
739 return ast->UnsignedCharTy.getAsOpaquePtr();
740 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
741 return ast->UnsignedShortTy.getAsOpaquePtr();
742 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
743 return ast->UnsignedIntTy.getAsOpaquePtr();
744 break;
745
746 case DW_ATE_lo_user:
747 // This has been seen to mean DW_AT_complex_integer
748 if (type_name)
Greg Clayton605684e2011-10-28 23:06:08 +0000749 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000750 if (::strstr(type_name, "complex"))
751 {
752 clang_type_t complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2);
753 return ast->getComplexType (QualType::getFromOpaquePtr(complex_int_clang_type)).getAsOpaquePtr();
754 }
Greg Clayton605684e2011-10-28 23:06:08 +0000755 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000756 break;
757
758 case DW_ATE_complex_float:
759 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
760 return ast->FloatComplexTy.getAsOpaquePtr();
761 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
762 return ast->DoubleComplexTy.getAsOpaquePtr();
763 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
764 return ast->LongDoubleComplexTy.getAsOpaquePtr();
765 else
Greg Clayton605684e2011-10-28 23:06:08 +0000766 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000767 clang_type_t complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2);
768 return ast->getComplexType (QualType::getFromOpaquePtr(complex_float_clang_type)).getAsOpaquePtr();
Greg Clayton605684e2011-10-28 23:06:08 +0000769 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000770 break;
771
772 case DW_ATE_float:
773 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
774 return ast->FloatTy.getAsOpaquePtr();
775 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
776 return ast->DoubleTy.getAsOpaquePtr();
777 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
778 return ast->LongDoubleTy.getAsOpaquePtr();
779 break;
780
781 case DW_ATE_signed:
782 if (type_name)
783 {
784 if (streq(type_name, "wchar_t") &&
785 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
786 return ast->WCharTy.getAsOpaquePtr();
787 if (streq(type_name, "void") &&
788 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
789 return ast->VoidTy.getAsOpaquePtr();
790 if (strstr(type_name, "long long") &&
791 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
792 return ast->LongLongTy.getAsOpaquePtr();
793 if (strstr(type_name, "long") &&
794 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
795 return ast->LongTy.getAsOpaquePtr();
796 if (strstr(type_name, "short") &&
797 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
798 return ast->ShortTy.getAsOpaquePtr();
799 if (strstr(type_name, "char"))
800 {
801 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
802 return ast->CharTy.getAsOpaquePtr();
803 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
804 return ast->SignedCharTy.getAsOpaquePtr();
805 }
806 if (strstr(type_name, "int"))
807 {
808 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
809 return ast->IntTy.getAsOpaquePtr();
810 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
811 return ast->Int128Ty.getAsOpaquePtr();
812 }
813 }
814 // We weren't able to match up a type name, just search by size
815 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
816 return ast->CharTy.getAsOpaquePtr();
817 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
818 return ast->ShortTy.getAsOpaquePtr();
819 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
820 return ast->IntTy.getAsOpaquePtr();
821 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
822 return ast->LongTy.getAsOpaquePtr();
823 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
824 return ast->LongLongTy.getAsOpaquePtr();
825 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
826 return ast->Int128Ty.getAsOpaquePtr();
827 break;
828
829 case DW_ATE_signed_char:
830 if (type_name)
831 {
832 if (streq(type_name, "signed char"))
833 {
834 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
835 return ast->SignedCharTy.getAsOpaquePtr();
836 }
837 }
838 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
839 return ast->CharTy.getAsOpaquePtr();
840 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
841 return ast->SignedCharTy.getAsOpaquePtr();
842 break;
843
844 case DW_ATE_unsigned:
845 if (type_name)
846 {
847 if (strstr(type_name, "long long"))
848 {
849 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
850 return ast->UnsignedLongLongTy.getAsOpaquePtr();
851 }
852 else if (strstr(type_name, "long"))
853 {
854 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
855 return ast->UnsignedLongTy.getAsOpaquePtr();
856 }
857 else if (strstr(type_name, "short"))
858 {
859 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
860 return ast->UnsignedShortTy.getAsOpaquePtr();
861 }
862 else if (strstr(type_name, "char"))
863 {
864 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
865 return ast->UnsignedCharTy.getAsOpaquePtr();
866 }
867 else if (strstr(type_name, "int"))
868 {
869 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
870 return ast->UnsignedIntTy.getAsOpaquePtr();
871 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
872 return ast->UnsignedInt128Ty.getAsOpaquePtr();
873 }
874 }
875 // We weren't able to match up a type name, just search by size
876 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
877 return ast->UnsignedCharTy.getAsOpaquePtr();
878 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
879 return ast->UnsignedShortTy.getAsOpaquePtr();
880 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
881 return ast->UnsignedIntTy.getAsOpaquePtr();
882 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
883 return ast->UnsignedLongTy.getAsOpaquePtr();
884 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
885 return ast->UnsignedLongLongTy.getAsOpaquePtr();
886 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
887 return ast->UnsignedInt128Ty.getAsOpaquePtr();
888 break;
889
890 case DW_ATE_unsigned_char:
891 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
892 return ast->UnsignedCharTy.getAsOpaquePtr();
893 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
894 return ast->UnsignedShortTy.getAsOpaquePtr();
895 break;
896
897 case DW_ATE_imaginary_float:
898 break;
899
900 case DW_ATE_UTF:
901 if (type_name)
902 {
903 if (streq(type_name, "char16_t"))
904 {
905 return ast->Char16Ty.getAsOpaquePtr();
906 }
907 else if (streq(type_name, "char32_t"))
908 {
909 return ast->Char32Ty.getAsOpaquePtr();
910 }
911 }
912 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000913 }
914 }
915 // This assert should fire for anything that we don't catch above so we know
916 // to fix any issues we run into.
Greg Claytondc968d12011-05-17 18:15:05 +0000917 if (type_name)
918 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000919 Host::SystemLog (Host::eSystemLogError, "error: need to add support for DW_TAG_base_type '%s' encoded with DW_ATE = 0x%x, bit_size = %u\n", type_name, dw_ate, bit_size);
Greg Claytondc968d12011-05-17 18:15:05 +0000920 }
921 else
922 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000923 Host::SystemLog (Host::eSystemLogError, "error: need to add support for DW_TAG_base_type encoded with DW_ATE = 0x%x, bit_size = %u\n", dw_ate, bit_size);
Greg Claytondc968d12011-05-17 18:15:05 +0000924 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000925 return NULL;
926}
927
Greg Clayton1be10fc2010-09-29 01:12:09 +0000928clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000929ClangASTContext::GetBuiltInType_void(ASTContext *ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000930{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000931 return ast->VoidTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000932}
933
Greg Clayton1be10fc2010-09-29 01:12:09 +0000934clang_type_t
Sean Callananf7c3e272010-11-19 02:52:21 +0000935ClangASTContext::GetBuiltInType_bool()
936{
937 return getASTContext()->BoolTy.getAsOpaquePtr();
938}
939
940clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000941ClangASTContext::GetBuiltInType_objc_id()
942{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000943 return getASTContext()->getObjCIdType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000944}
945
Greg Clayton1be10fc2010-09-29 01:12:09 +0000946clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000947ClangASTContext::GetBuiltInType_objc_Class()
948{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000949 return getASTContext()->getObjCClassType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000950}
951
Greg Clayton1be10fc2010-09-29 01:12:09 +0000952clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000953ClangASTContext::GetBuiltInType_objc_selector()
954{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000955 return getASTContext()->getObjCSelType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000956}
957
Greg Clayton1be10fc2010-09-29 01:12:09 +0000958clang_type_t
Sean Callanan77502262011-05-12 23:54:16 +0000959ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast)
960{
961 return ast->UnknownAnyTy.getAsOpaquePtr();
962}
963
964clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000965ClangASTContext::GetCStringType (bool is_const)
966{
967 QualType char_type(getASTContext()->CharTy);
968
969 if (is_const)
970 char_type.addConst();
971
972 return getASTContext()->getPointerType(char_type).getAsOpaquePtr();
973}
974
Greg Clayton1be10fc2010-09-29 01:12:09 +0000975clang_type_t
Sean Callanana6582262012-04-05 00:12:52 +0000976ClangASTContext::GetVoidType()
977{
978 return GetVoidType(getASTContext());
979}
980
981clang_type_t
982ClangASTContext::GetVoidType(ASTContext *ast)
983{
984 return ast->VoidTy.getAsOpaquePtr();
985}
986
987clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988ClangASTContext::GetVoidPtrType (bool is_const)
989{
990 return GetVoidPtrType(getASTContext(), is_const);
991}
992
Greg Clayton1be10fc2010-09-29 01:12:09 +0000993clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000994ClangASTContext::GetVoidPtrType (ASTContext *ast, bool is_const)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000995{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000996 QualType void_ptr_type(ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000997
998 if (is_const)
999 void_ptr_type.addConst();
1000
1001 return void_ptr_type.getAsOpaquePtr();
1002}
1003
Sean Callanan09ab4b72011-11-30 22:11:59 +00001004clang::DeclContext *
1005ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast)
1006{
1007 return ast->getTranslationUnitDecl();
1008}
1009
Greg Clayton1be10fc2010-09-29 01:12:09 +00001010clang_type_t
Greg Clayton38a61402010-12-02 23:20:03 +00001011ClangASTContext::CopyType (ASTContext *dst_ast,
1012 ASTContext *src_ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001013 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001014{
Sean Callanan79439e82010-11-18 02:56:27 +00001015 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001016 FileManager file_manager (file_system_options);
1017 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001018 *src_ast, file_manager,
1019 false);
Sean Callanan0617fcb2010-11-09 22:37:10 +00001020
Greg Clayton38a61402010-12-02 23:20:03 +00001021 QualType src (QualType::getFromOpaquePtr(clang_type));
1022 QualType dst (importer.Import(src));
Sean Callanan0617fcb2010-11-09 22:37:10 +00001023
1024 return dst.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001025}
1026
Greg Clayton526e5af2010-11-13 03:52:47 +00001027
1028clang::Decl *
Greg Clayton38a61402010-12-02 23:20:03 +00001029ClangASTContext::CopyDecl (ASTContext *dst_ast,
1030 ASTContext *src_ast,
Greg Clayton526e5af2010-11-13 03:52:47 +00001031 clang::Decl *source_decl)
Sean Callanan7fddd4c2010-12-11 00:08:56 +00001032{
Sean Callanan79439e82010-11-18 02:56:27 +00001033 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001034 FileManager file_manager (file_system_options);
1035 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001036 *src_ast, file_manager,
1037 false);
Greg Clayton526e5af2010-11-13 03:52:47 +00001038
1039 return importer.Import(source_decl);
1040}
1041
Sean Callanan23a30272010-07-16 00:00:27 +00001042bool
Greg Clayton84db9102012-03-26 23:03:23 +00001043ClangASTContext::AreTypesSame (ASTContext *ast,
1044 clang_type_t type1,
1045 clang_type_t type2,
1046 bool ignore_qualifiers)
Sean Callanan4dcca2622010-07-15 22:30:52 +00001047{
Greg Clayton55995eb2012-04-06 17:38:55 +00001048 if (type1 == type2)
1049 return true;
1050
Sean Callanan5056ab02012-02-18 02:01:03 +00001051 QualType type1_qual = QualType::getFromOpaquePtr(type1);
1052 QualType type2_qual = QualType::getFromOpaquePtr(type2);
1053
1054 if (ignore_qualifiers)
1055 {
1056 type1_qual = type1_qual.getUnqualifiedType();
1057 type2_qual = type2_qual.getUnqualifiedType();
1058 }
1059
1060 return ast->hasSameType (type1_qual,
1061 type2_qual);
Sean Callanan4dcca2622010-07-15 22:30:52 +00001062}
1063
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001064#pragma mark CVR modifiers
1065
Greg Clayton1be10fc2010-09-29 01:12:09 +00001066clang_type_t
1067ClangASTContext::AddConstModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001068{
1069 if (clang_type)
1070 {
1071 QualType result(QualType::getFromOpaquePtr(clang_type));
1072 result.addConst();
1073 return result.getAsOpaquePtr();
1074 }
1075 return NULL;
1076}
1077
Greg Clayton1be10fc2010-09-29 01:12:09 +00001078clang_type_t
1079ClangASTContext::AddRestrictModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001080{
1081 if (clang_type)
1082 {
1083 QualType result(QualType::getFromOpaquePtr(clang_type));
1084 result.getQualifiers().setRestrict (true);
1085 return result.getAsOpaquePtr();
1086 }
1087 return NULL;
1088}
1089
Greg Clayton1be10fc2010-09-29 01:12:09 +00001090clang_type_t
1091ClangASTContext::AddVolatileModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001092{
1093 if (clang_type)
1094 {
1095 QualType result(QualType::getFromOpaquePtr(clang_type));
1096 result.getQualifiers().setVolatile (true);
1097 return result.getAsOpaquePtr();
1098 }
1099 return NULL;
1100}
1101
Greg Clayton6beaaa62011-01-17 03:46:26 +00001102
1103clang_type_t
1104ClangASTContext::GetTypeForDecl (TagDecl *decl)
1105{
1106 // No need to call the getASTContext() accessor (which can create the AST
1107 // if it isn't created yet, because we can't have created a decl in this
1108 // AST if our AST didn't already exist...
1109 if (m_ast_ap.get())
1110 return m_ast_ap->getTagDeclType(decl).getAsOpaquePtr();
1111 return NULL;
1112}
1113
1114clang_type_t
1115ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1116{
1117 // No need to call the getASTContext() accessor (which can create the AST
1118 // if it isn't created yet, because we can't have created a decl in this
1119 // AST if our AST didn't already exist...
1120 if (m_ast_ap.get())
1121 return m_ast_ap->getObjCInterfaceType(decl).getAsOpaquePtr();
1122 return NULL;
1123}
1124
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001125#pragma mark Structure, Unions, Classes
1126
Greg Clayton1be10fc2010-09-29 01:12:09 +00001127clang_type_t
Sean Callananad880762012-04-18 01:06:17 +00001128ClangASTContext::CreateRecordType (DeclContext *decl_ctx, AccessType access_type, const char *name, int kind, LanguageType language, uint64_t metadata)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001129{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001130 ASTContext *ast = getASTContext();
1131 assert (ast != NULL);
Sean Callananad880762012-04-18 01:06:17 +00001132
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001134 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001135
Greg Clayton9e409562010-07-28 02:04:09 +00001136
Greg Claytone1be9962011-08-24 23:50:00 +00001137 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
Greg Clayton9e409562010-07-28 02:04:09 +00001138 {
Greg Claytonaaf99e02010-10-11 02:25:34 +00001139 bool isForwardDecl = true;
Greg Clayton9e409562010-07-28 02:04:09 +00001140 bool isInternal = false;
Sean Callananad880762012-04-18 01:06:17 +00001141 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal, metadata);
Greg Clayton9e409562010-07-28 02:04:09 +00001142 }
1143
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1145 // we will need to update this code. I was told to currently always use
1146 // the CXXRecordDecl class since we often don't know from debug information
1147 // if something is struct or a class, so we default to always use the more
1148 // complete definition just in case.
Greg Claytonf0705c82011-10-22 03:33:13 +00001149 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1150 (TagDecl::TagKind)kind,
1151 decl_ctx,
1152 SourceLocation(),
1153 SourceLocation(),
1154 name && name[0] ? &ast->Idents.get(name) : NULL);
Sean Callanan7282e2a2012-01-13 22:10:18 +00001155
Sean Callananad880762012-04-18 01:06:17 +00001156 if (decl)
1157 SetMetadata(ast, (uintptr_t)decl, metadata);
Sean Callanan60217122012-04-13 00:10:03 +00001158
Greg Clayton55561e92011-10-26 03:31:36 +00001159 if (decl_ctx)
1160 {
1161 if (access_type != eAccessNone)
1162 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
1163 decl_ctx->addDecl (decl);
1164 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001165 return ast->getTagDeclType(decl).getAsOpaquePtr();
1166}
1167
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001168static TemplateParameterList *
1169CreateTemplateParameterList (ASTContext *ast,
1170 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1171 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1172{
1173 const bool parameter_pack = false;
1174 const bool is_typename = false;
1175 const unsigned depth = 0;
1176 const size_t num_template_params = template_param_infos.GetSize();
1177 for (size_t i=0; i<num_template_params; ++i)
1178 {
1179 const char *name = template_param_infos.names[i];
1180 if (template_param_infos.args[i].getAsIntegral())
1181 {
1182 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1183 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1184 SourceLocation(),
1185 SourceLocation(),
1186 depth,
1187 i,
1188 &ast->Idents.get(name),
1189 template_param_infos.args[i].getIntegralType(),
1190 parameter_pack,
1191 NULL));
1192
1193 }
1194 else
1195 {
1196 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1197 ast->getTranslationUnitDecl(), // Is this the right decl context?
1198 SourceLocation(),
1199 SourceLocation(),
1200 depth,
1201 i,
1202 &ast->Idents.get(name),
1203 is_typename,
1204 parameter_pack));
1205 }
1206 }
1207
1208 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1209 SourceLocation(),
1210 SourceLocation(),
1211 &template_param_decls.front(),
1212 template_param_decls.size(),
1213 SourceLocation());
1214 return template_param_list;
1215}
1216
1217clang::FunctionTemplateDecl *
1218ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1219 clang::FunctionDecl *func_decl,
1220 const char *name,
1221 const TemplateParameterInfos &template_param_infos)
1222{
1223// /// \brief Create a function template node.
1224 ASTContext *ast = getASTContext();
1225
1226 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1227
1228 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1229 template_param_infos,
1230 template_param_decls);
1231 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1232 decl_ctx,
1233 func_decl->getLocation(),
1234 func_decl->getDeclName(),
1235 template_param_list,
1236 func_decl);
1237
1238 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1239 i < template_param_decl_count;
1240 ++i)
1241 {
1242 // TODO: verify which decl context we should put template_param_decls into..
1243 template_param_decls[i]->setDeclContext (func_decl);
1244 }
1245
1246 return func_tmpl_decl;
1247}
1248
1249void
1250ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1251 clang::FunctionTemplateDecl *func_tmpl_decl,
1252 const TemplateParameterInfos &infos)
1253{
1254 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1255 infos.args.data(),
1256 infos.args.size());
1257
1258 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1259 &template_args,
1260 NULL);
1261}
1262
1263
Greg Claytonf0705c82011-10-22 03:33:13 +00001264ClassTemplateDecl *
1265ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001266 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001267 const char *class_name,
1268 int kind,
1269 const TemplateParameterInfos &template_param_infos)
1270{
1271 ASTContext *ast = getASTContext();
1272
1273 ClassTemplateDecl *class_template_decl = NULL;
1274 if (decl_ctx == NULL)
1275 decl_ctx = ast->getTranslationUnitDecl();
1276
1277 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1278 DeclarationName decl_name (&identifier_info);
1279
1280 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1281 for (clang::DeclContext::lookup_iterator pos = result.first, end = result.second; pos != end; ++pos)
1282 {
1283 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(*pos);
1284 if (class_template_decl)
1285 return class_template_decl;
1286 }
1287
1288 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Claytonf0705c82011-10-22 03:33:13 +00001289
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001290 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1291 template_param_infos,
1292 template_param_decls);
Greg Claytonf0705c82011-10-22 03:33:13 +00001293
1294 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1295 (TagDecl::TagKind)kind,
1296 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1297 SourceLocation(),
1298 SourceLocation(),
1299 &identifier_info);
Greg Claytone04741d2011-12-02 02:09:28 +00001300
1301 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1302 i < template_param_decl_count;
1303 ++i)
1304 {
1305 template_param_decls[i]->setDeclContext (template_cxx_decl);
1306 }
1307
Sean Callananb5c79622011-11-19 01:35:08 +00001308 // With templated classes, we say that a class is templated with
1309 // specializations, but that the bare class has no functions.
1310 template_cxx_decl->startDefinition();
1311 template_cxx_decl->completeDefinition();
1312
Greg Claytonf0705c82011-10-22 03:33:13 +00001313 class_template_decl = ClassTemplateDecl::Create (*ast,
1314 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1315 SourceLocation(),
1316 decl_name,
1317 template_param_list,
1318 template_cxx_decl,
1319 NULL);
1320
1321 if (class_template_decl)
Sean Callanan5e9e1992011-10-26 01:06:27 +00001322 {
Greg Clayton55561e92011-10-26 03:31:36 +00001323 if (access_type != eAccessNone)
1324 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Sean Callanan5b26f272012-02-04 08:49:35 +00001325
1326 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1327 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1328
Greg Claytonf0705c82011-10-22 03:33:13 +00001329 decl_ctx->addDecl (class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001330
1331#ifdef LLDB_CONFIGURATION_DEBUG
1332 VerifyDecl(class_template_decl);
1333#endif
1334 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001335
1336 return class_template_decl;
1337}
1338
1339
1340ClassTemplateSpecializationDecl *
1341ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1342 ClassTemplateDecl *class_template_decl,
1343 int kind,
1344 const TemplateParameterInfos &template_param_infos)
1345{
1346 ASTContext *ast = getASTContext();
1347 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1348 (TagDecl::TagKind)kind,
1349 decl_ctx,
1350 SourceLocation(),
1351 SourceLocation(),
1352 class_template_decl,
1353 &template_param_infos.args.front(),
1354 template_param_infos.args.size(),
1355 NULL);
1356
1357 return class_template_specialization_decl;
1358}
1359
1360lldb::clang_type_t
1361ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1362{
1363 if (class_template_specialization_decl)
1364 {
1365 ASTContext *ast = getASTContext();
1366 if (ast)
1367 return ast->getTagDeclType(class_template_specialization_decl).getAsOpaquePtr();
1368 }
1369 return NULL;
1370}
1371
Greg Clayton6beaaa62011-01-17 03:46:26 +00001372bool
1373ClangASTContext::SetHasExternalStorage (clang_type_t clang_type, bool has_extern)
1374{
1375 if (clang_type == NULL)
1376 return false;
1377
1378 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
1379
1380 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
1381 switch (type_class)
1382 {
1383 case clang::Type::Record:
1384 {
1385 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
1386 if (cxx_record_decl)
1387 {
1388 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001389 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001390 return true;
1391 }
1392 }
1393 break;
1394
1395 case clang::Type::Enum:
1396 {
1397 EnumDecl *enum_decl = cast<EnumType>(qual_type)->getDecl();
1398 if (enum_decl)
1399 {
1400 enum_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001401 enum_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001402 return true;
1403 }
1404 }
1405 break;
1406
1407 case clang::Type::ObjCObject:
1408 case clang::Type::ObjCInterface:
1409 {
Sean Callanan78e37602011-01-27 04:42:51 +00001410 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001411 assert (objc_class_type);
1412 if (objc_class_type)
1413 {
1414 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1415
1416 if (class_interface_decl)
1417 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001418 class_interface_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001419 class_interface_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001420 return true;
1421 }
1422 }
1423 }
1424 break;
1425
1426 case clang::Type::Typedef:
1427 return ClangASTContext::SetHasExternalStorage (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
Sean Callanan912855f2011-08-11 23:56:13 +00001428
1429 case clang::Type::Elaborated:
1430 return ClangASTContext::SetHasExternalStorage (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001431
1432 default:
1433 break;
1434 }
1435 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001436}
1437
Greg Claytona3c444a2010-10-01 23:13:49 +00001438static bool
1439IsOperator (const char *name, OverloadedOperatorKind &op_kind)
1440{
1441 if (name == NULL || name[0] == '\0')
1442 return false;
1443
Sean Callanana43f20d2010-12-10 19:51:54 +00001444#define OPERATOR_PREFIX "operator"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001445#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
Sean Callananbfeff8c2010-12-10 02:15:55 +00001446
1447 const char *post_op_name = NULL;
1448
Sean Callanana43f20d2010-12-10 19:51:54 +00001449 bool no_space = true;
Sean Callananbfeff8c2010-12-10 02:15:55 +00001450
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001451 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
Greg Claytona3c444a2010-10-01 23:13:49 +00001452 return false;
1453
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001454 post_op_name = name + OPERATOR_PREFIX_LENGTH;
1455
Sean Callanana43f20d2010-12-10 19:51:54 +00001456 if (post_op_name[0] == ' ')
1457 {
1458 post_op_name++;
1459 no_space = false;
1460 }
1461
1462#undef OPERATOR_PREFIX
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001463#undef OPERATOR_PREFIX_LENGTH
Sean Callanana43f20d2010-12-10 19:51:54 +00001464
Greg Claytona3c444a2010-10-01 23:13:49 +00001465 // This is an operator, set the overloaded operator kind to invalid
1466 // in case this is a conversion operator...
1467 op_kind = NUM_OVERLOADED_OPERATORS;
1468
1469 switch (post_op_name[0])
1470 {
Sean Callananbfeff8c2010-12-10 02:15:55 +00001471 default:
1472 if (no_space)
1473 return false;
1474 break;
Greg Claytona3c444a2010-10-01 23:13:49 +00001475 case 'n':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001476 if (no_space)
1477 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001478 if (strcmp (post_op_name, "new") == 0)
1479 op_kind = OO_New;
1480 else if (strcmp (post_op_name, "new[]") == 0)
1481 op_kind = OO_Array_New;
1482 break;
1483
1484 case 'd':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001485 if (no_space)
1486 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001487 if (strcmp (post_op_name, "delete") == 0)
1488 op_kind = OO_Delete;
1489 else if (strcmp (post_op_name, "delete[]") == 0)
1490 op_kind = OO_Array_Delete;
1491 break;
1492
1493 case '+':
1494 if (post_op_name[1] == '\0')
1495 op_kind = OO_Plus;
1496 else if (post_op_name[2] == '\0')
1497 {
1498 if (post_op_name[1] == '=')
1499 op_kind = OO_PlusEqual;
1500 else if (post_op_name[1] == '+')
1501 op_kind = OO_PlusPlus;
1502 }
1503 break;
1504
1505 case '-':
1506 if (post_op_name[1] == '\0')
1507 op_kind = OO_Minus;
1508 else if (post_op_name[2] == '\0')
1509 {
1510 switch (post_op_name[1])
1511 {
1512 case '=': op_kind = OO_MinusEqual; break;
1513 case '-': op_kind = OO_MinusMinus; break;
1514 case '>': op_kind = OO_Arrow; break;
1515 }
1516 }
1517 else if (post_op_name[3] == '\0')
1518 {
1519 if (post_op_name[2] == '*')
1520 op_kind = OO_ArrowStar; break;
1521 }
1522 break;
1523
1524 case '*':
1525 if (post_op_name[1] == '\0')
1526 op_kind = OO_Star;
1527 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1528 op_kind = OO_StarEqual;
1529 break;
1530
1531 case '/':
1532 if (post_op_name[1] == '\0')
1533 op_kind = OO_Slash;
1534 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1535 op_kind = OO_SlashEqual;
1536 break;
1537
1538 case '%':
1539 if (post_op_name[1] == '\0')
1540 op_kind = OO_Percent;
1541 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1542 op_kind = OO_PercentEqual;
1543 break;
1544
1545
1546 case '^':
1547 if (post_op_name[1] == '\0')
1548 op_kind = OO_Caret;
1549 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1550 op_kind = OO_CaretEqual;
1551 break;
1552
1553 case '&':
1554 if (post_op_name[1] == '\0')
1555 op_kind = OO_Amp;
1556 else if (post_op_name[2] == '\0')
1557 {
1558 switch (post_op_name[1])
1559 {
1560 case '=': op_kind = OO_AmpEqual; break;
1561 case '&': op_kind = OO_AmpAmp; break;
1562 }
1563 }
1564 break;
1565
1566 case '|':
1567 if (post_op_name[1] == '\0')
1568 op_kind = OO_Pipe;
1569 else if (post_op_name[2] == '\0')
1570 {
1571 switch (post_op_name[1])
1572 {
1573 case '=': op_kind = OO_PipeEqual; break;
1574 case '|': op_kind = OO_PipePipe; break;
1575 }
1576 }
1577 break;
1578
1579 case '~':
1580 if (post_op_name[1] == '\0')
1581 op_kind = OO_Tilde;
1582 break;
1583
1584 case '!':
1585 if (post_op_name[1] == '\0')
1586 op_kind = OO_Exclaim;
1587 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1588 op_kind = OO_ExclaimEqual;
1589 break;
1590
1591 case '=':
1592 if (post_op_name[1] == '\0')
1593 op_kind = OO_Equal;
1594 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1595 op_kind = OO_EqualEqual;
1596 break;
1597
1598 case '<':
1599 if (post_op_name[1] == '\0')
1600 op_kind = OO_Less;
1601 else if (post_op_name[2] == '\0')
1602 {
1603 switch (post_op_name[1])
1604 {
1605 case '<': op_kind = OO_LessLess; break;
1606 case '=': op_kind = OO_LessEqual; break;
1607 }
1608 }
1609 else if (post_op_name[3] == '\0')
1610 {
1611 if (post_op_name[2] == '=')
1612 op_kind = OO_LessLessEqual;
1613 }
1614 break;
1615
1616 case '>':
1617 if (post_op_name[1] == '\0')
1618 op_kind = OO_Greater;
1619 else if (post_op_name[2] == '\0')
1620 {
1621 switch (post_op_name[1])
1622 {
1623 case '>': op_kind = OO_GreaterGreater; break;
1624 case '=': op_kind = OO_GreaterEqual; break;
1625 }
1626 }
1627 else if (post_op_name[1] == '>' &&
1628 post_op_name[2] == '=' &&
1629 post_op_name[3] == '\0')
1630 {
1631 op_kind = OO_GreaterGreaterEqual;
1632 }
1633 break;
1634
1635 case ',':
1636 if (post_op_name[1] == '\0')
1637 op_kind = OO_Comma;
1638 break;
1639
1640 case '(':
1641 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
1642 op_kind = OO_Call;
1643 break;
1644
1645 case '[':
1646 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
1647 op_kind = OO_Subscript;
1648 break;
1649 }
1650
1651 return true;
1652}
Greg Clayton6beaaa62011-01-17 03:46:26 +00001653
Greg Clayton090d0982011-06-19 03:43:27 +00001654static inline bool
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001655check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
Greg Clayton090d0982011-06-19 03:43:27 +00001656{
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001657 // Special-case call since it can take any number of operands
1658 if(op_kind == OO_Call)
1659 return true;
1660
Greg Clayton090d0982011-06-19 03:43:27 +00001661 // The parameter count doens't include "this"
1662 if (num_params == 0)
1663 return unary;
1664 if (num_params == 1)
1665 return binary;
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001666 else
Greg Clayton090d0982011-06-19 03:43:27 +00001667 return false;
1668}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001669
Greg Clayton090d0982011-06-19 03:43:27 +00001670bool
1671ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1672{
Sean Callanan5b26f272012-02-04 08:49:35 +00001673 switch (op_kind)
1674 {
1675 default:
1676 break;
1677 // C++ standard allows any number of arguments to new/delete
1678 case OO_New:
1679 case OO_Array_New:
1680 case OO_Delete:
1681 case OO_Array_Delete:
1682 return true;
1683 }
1684
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001685#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) case OO_##Name: return check_op_param (op_kind, Unary, Binary, num_params);
Greg Clayton090d0982011-06-19 03:43:27 +00001686 switch (op_kind)
1687 {
1688#include "clang/Basic/OperatorKinds.def"
1689 default: break;
1690 }
1691 return false;
1692}
1693
Greg Claytona51ed9b2010-09-23 01:09:21 +00001694CXXMethodDecl *
Sean Callanan61da09b2010-09-17 02:58:26 +00001695ClangASTContext::AddMethodToCXXRecordType
1696(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001697 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001698 clang_type_t record_opaque_type,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001699 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001700 clang_type_t method_opaque_type,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001701 lldb::AccessType access,
Greg Clayton0fffff52010-09-24 05:15:53 +00001702 bool is_virtual,
1703 bool is_static,
Greg Claytonf51de672010-10-01 02:31:07 +00001704 bool is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00001705 bool is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00001706 bool is_attr_used,
1707 bool is_artificial
Greg Claytona51ed9b2010-09-23 01:09:21 +00001708)
Sean Callanan61da09b2010-09-17 02:58:26 +00001709{
Sean Callananfc55f5d2010-09-21 00:44:12 +00001710 if (!record_opaque_type || !method_opaque_type || !name)
Johnny Chend440bcc2010-09-28 16:10:54 +00001711 return NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001712
Greg Clayton6beaaa62011-01-17 03:46:26 +00001713 assert(ast);
Sean Callanan61da09b2010-09-17 02:58:26 +00001714
Greg Clayton6beaaa62011-01-17 03:46:26 +00001715 IdentifierTable *identifier_table = &ast->Idents;
Sean Callanan61da09b2010-09-17 02:58:26 +00001716
1717 assert(identifier_table);
1718
Sean Callananfc55f5d2010-09-21 00:44:12 +00001719 QualType record_qual_type(QualType::getFromOpaquePtr(record_opaque_type));
Greg Clayton0fffff52010-09-24 05:15:53 +00001720
Greg Clayton6beaaa62011-01-17 03:46:26 +00001721 CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
Sean Callanan61da09b2010-09-17 02:58:26 +00001722
Greg Clayton0fffff52010-09-24 05:15:53 +00001723 if (cxx_record_decl == NULL)
Greg Claytona51ed9b2010-09-23 01:09:21 +00001724 return NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001725
Greg Clayton0fffff52010-09-24 05:15:53 +00001726 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001727
Greg Claytonf51de672010-10-01 02:31:07 +00001728 CXXMethodDecl *cxx_method_decl = NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001729
Greg Claytonf51de672010-10-01 02:31:07 +00001730 DeclarationName decl_name (&identifier_table->get(name));
Greg Clayton878eaf12010-10-01 03:45:20 +00001731
Sean Callanan78e37602011-01-27 04:42:51 +00001732 const clang::FunctionType *function_Type = dyn_cast<FunctionType>(method_qual_type.getTypePtr());
Greg Clayton878eaf12010-10-01 03:45:20 +00001733
Greg Clayton90a2acd2010-10-02 01:40:05 +00001734 if (function_Type == NULL)
Greg Clayton878eaf12010-10-01 03:45:20 +00001735 return NULL;
1736
Sean Callanan78e37602011-01-27 04:42:51 +00001737 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(function_Type));
Greg Clayton878eaf12010-10-01 03:45:20 +00001738
1739 if (!method_function_prototype)
1740 return NULL;
1741
1742 unsigned int num_params = method_function_prototype->getNumArgs();
1743
Sean Callanandbb58392011-11-02 01:38:59 +00001744 CXXDestructorDecl *cxx_dtor_decl(NULL);
1745 CXXConstructorDecl *cxx_ctor_decl(NULL);
1746
Greg Clayton878eaf12010-10-01 03:45:20 +00001747 if (name[0] == '~')
Greg Claytonf51de672010-10-01 02:31:07 +00001748 {
Sean Callanandbb58392011-11-02 01:38:59 +00001749 cxx_dtor_decl = CXXDestructorDecl::Create (*ast,
1750 cxx_record_decl,
1751 SourceLocation(),
1752 DeclarationNameInfo (ast->DeclarationNames.getCXXDestructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1753 method_qual_type,
1754 NULL,
1755 is_inline,
1756 is_artificial);
1757 cxx_method_decl = cxx_dtor_decl;
Greg Clayton878eaf12010-10-01 03:45:20 +00001758 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001759 else if (decl_name == cxx_record_decl->getDeclName())
Greg Clayton878eaf12010-10-01 03:45:20 +00001760 {
Sean Callanandbb58392011-11-02 01:38:59 +00001761 cxx_ctor_decl = CXXConstructorDecl::Create (*ast,
1762 cxx_record_decl,
1763 SourceLocation(),
1764 DeclarationNameInfo (ast->DeclarationNames.getCXXConstructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1765 method_qual_type,
1766 NULL, // TypeSourceInfo *
1767 is_explicit,
1768 is_inline,
1769 is_artificial,
1770 false /*is_constexpr*/);
1771 cxx_method_decl = cxx_ctor_decl;
Greg Claytonf51de672010-10-01 02:31:07 +00001772 }
1773 else
Greg Clayton878eaf12010-10-01 03:45:20 +00001774 {
Greg Claytona3c444a2010-10-01 23:13:49 +00001775
1776 OverloadedOperatorKind op_kind = NUM_OVERLOADED_OPERATORS;
1777 if (IsOperator (name, op_kind))
Greg Clayton878eaf12010-10-01 03:45:20 +00001778 {
Greg Claytona3c444a2010-10-01 23:13:49 +00001779 if (op_kind != NUM_OVERLOADED_OPERATORS)
1780 {
Greg Clayton090d0982011-06-19 03:43:27 +00001781 // Check the number of operator parameters. Sometimes we have
1782 // seen bad DWARF that doesn't correctly describe operators and
1783 // if we try to create a methed and add it to the class, clang
1784 // will assert and crash, so we need to make sure things are
1785 // acceptable.
1786 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
1787 return NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001788 cxx_method_decl = CXXMethodDecl::Create (*ast,
Greg Clayton878eaf12010-10-01 03:45:20 +00001789 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001790 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001791 DeclarationNameInfo (ast->DeclarationNames.getCXXOperatorName (op_kind), SourceLocation()),
Greg Clayton878eaf12010-10-01 03:45:20 +00001792 method_qual_type,
1793 NULL, // TypeSourceInfo *
Greg Claytona3c444a2010-10-01 23:13:49 +00001794 is_static,
1795 SC_None,
Sean Callananfb0b7582011-03-15 00:17:19 +00001796 is_inline,
Sean Callanan880e6802011-10-07 23:18:13 +00001797 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001798 SourceLocation());
Greg Claytona3c444a2010-10-01 23:13:49 +00001799 }
1800 else if (num_params == 0)
1801 {
1802 // Conversion operators don't take params...
Greg Clayton6beaaa62011-01-17 03:46:26 +00001803 cxx_method_decl = CXXConversionDecl::Create (*ast,
Greg Claytona3c444a2010-10-01 23:13:49 +00001804 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001805 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001806 DeclarationNameInfo (ast->DeclarationNames.getCXXConversionFunctionName (ast->getCanonicalType (function_Type->getResultType())), SourceLocation()),
Greg Claytona3c444a2010-10-01 23:13:49 +00001807 method_qual_type,
1808 NULL, // TypeSourceInfo *
1809 is_inline,
Sean Callananfb0b7582011-03-15 00:17:19 +00001810 is_explicit,
Sean Callanan880e6802011-10-07 23:18:13 +00001811 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001812 SourceLocation());
Greg Claytona3c444a2010-10-01 23:13:49 +00001813 }
Greg Clayton878eaf12010-10-01 03:45:20 +00001814 }
Greg Claytona3c444a2010-10-01 23:13:49 +00001815
1816 if (cxx_method_decl == NULL)
Greg Clayton878eaf12010-10-01 03:45:20 +00001817 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001818 cxx_method_decl = CXXMethodDecl::Create (*ast,
Greg Clayton878eaf12010-10-01 03:45:20 +00001819 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001820 SourceLocation(),
Greg Claytona3c444a2010-10-01 23:13:49 +00001821 DeclarationNameInfo (decl_name, SourceLocation()),
Greg Clayton878eaf12010-10-01 03:45:20 +00001822 method_qual_type,
1823 NULL, // TypeSourceInfo *
1824 is_static,
1825 SC_None,
Sean Callananfb0b7582011-03-15 00:17:19 +00001826 is_inline,
Sean Callanan880e6802011-10-07 23:18:13 +00001827 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001828 SourceLocation());
Greg Clayton878eaf12010-10-01 03:45:20 +00001829 }
Greg Claytonf51de672010-10-01 02:31:07 +00001830 }
Greg Claytona3c444a2010-10-01 23:13:49 +00001831
Greg Clayton1be10fc2010-09-29 01:12:09 +00001832 AccessSpecifier access_specifier = ConvertAccessTypeToAccessSpecifier (access);
Greg Clayton0fffff52010-09-24 05:15:53 +00001833
1834 cxx_method_decl->setAccess (access_specifier);
1835 cxx_method_decl->setVirtualAsWritten (is_virtual);
Sean Callanane2ef6e32010-09-23 03:01:22 +00001836
Sean Callananc1b732d2011-11-01 18:07:13 +00001837 if (is_attr_used)
1838 cxx_method_decl->addAttr(::new (*ast) UsedAttr(SourceRange(), *ast));
1839
Sean Callananfc55f5d2010-09-21 00:44:12 +00001840 // Populate the method decl with parameter decls
Sean Callananfc55f5d2010-09-21 00:44:12 +00001841
Charles Davis8c444c42011-05-19 23:33:46 +00001842 llvm::SmallVector<ParmVarDecl *, 12> params;
Sean Callananfc55f5d2010-09-21 00:44:12 +00001843
1844 for (int param_index = 0;
1845 param_index < num_params;
1846 ++param_index)
1847 {
Charles Davis8c444c42011-05-19 23:33:46 +00001848 params.push_back (ParmVarDecl::Create (*ast,
1849 cxx_method_decl,
1850 SourceLocation(),
1851 SourceLocation(),
1852 NULL, // anonymous
1853 method_function_prototype->getArgType(param_index),
1854 NULL,
1855 SC_None,
1856 SC_None,
1857 NULL));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001858 }
1859
Sean Callanan880e6802011-10-07 23:18:13 +00001860 cxx_method_decl->setParams (ArrayRef<ParmVarDecl*>(params));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001861
Greg Clayton0fffff52010-09-24 05:15:53 +00001862 cxx_record_decl->addDecl (cxx_method_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001863
Greg Clayton8b867b42011-11-02 02:06:20 +00001864 // Sometimes the debug info will mention a constructor (default/copy/move),
1865 // destructor, or assignment operator (copy/move) but there won't be any
1866 // version of this in the code. So we check if the function was artificially
1867 // generated and if it is trivial and this lets the compiler/backend know
1868 // that it can inline the IR for these when it needs to and we can avoid a
1869 // "missing function" error when running expressions.
1870
Sean Callanandbb58392011-11-02 01:38:59 +00001871 if (is_artificial)
1872 {
Greg Clayton8b867b42011-11-02 02:06:20 +00001873 if (cxx_ctor_decl &&
1874 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
1875 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
1876 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
Sean Callanandbb58392011-11-02 01:38:59 +00001877 {
1878 cxx_ctor_decl->setDefaulted();
1879 cxx_ctor_decl->setTrivial(true);
1880 }
Greg Clayton8b867b42011-11-02 02:06:20 +00001881 else if (cxx_dtor_decl)
Sean Callanandbb58392011-11-02 01:38:59 +00001882 {
Greg Clayton8b867b42011-11-02 02:06:20 +00001883 if (cxx_record_decl->hasTrivialDestructor())
1884 {
1885 cxx_dtor_decl->setDefaulted();
1886 cxx_dtor_decl->setTrivial(true);
1887 }
1888 }
1889 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
1890 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
1891 {
1892 cxx_method_decl->setDefaulted();
1893 cxx_method_decl->setTrivial(true);
Sean Callanandbb58392011-11-02 01:38:59 +00001894 }
1895 }
1896
Sean Callanan5e9e1992011-10-26 01:06:27 +00001897#ifdef LLDB_CONFIGURATION_DEBUG
1898 VerifyDecl(cxx_method_decl);
1899#endif
Greg Claytonc432c192011-01-20 04:18:48 +00001900
1901// printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
1902// printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
1903// printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
1904// printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
1905// printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
1906// printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
1907// printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
1908// printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
1909// printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
Greg Claytona51ed9b2010-09-23 01:09:21 +00001910 return cxx_method_decl;
Sean Callanan61da09b2010-09-17 02:58:26 +00001911}
1912
Jim Inghame3ae82a2011-11-12 01:36:43 +00001913clang::FieldDecl *
Greg Clayton8cf05932010-07-22 18:30:50 +00001914ClangASTContext::AddFieldToRecordType
1915(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001916 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001917 clang_type_t record_clang_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00001918 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001919 clang_type_t field_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00001920 AccessType access,
1921 uint32_t bitfield_bit_size
1922)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001923{
1924 if (record_clang_type == NULL || field_type == NULL)
Jim Inghame3ae82a2011-11-12 01:36:43 +00001925 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001926
Jim Inghame3ae82a2011-11-12 01:36:43 +00001927 FieldDecl *field = NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001928 IdentifierTable *identifier_table = &ast->Idents;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001929
Greg Clayton6beaaa62011-01-17 03:46:26 +00001930 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001931 assert (identifier_table != NULL);
1932
1933 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
1934
Sean Callanan78e37602011-01-27 04:42:51 +00001935 const clang::Type *clang_type = record_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001936 if (clang_type)
1937 {
1938 const RecordType *record_type = dyn_cast<RecordType>(clang_type);
1939
1940 if (record_type)
1941 {
1942 RecordDecl *record_decl = record_type->getDecl();
1943
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001944 clang::Expr *bit_width = NULL;
1945 if (bitfield_bit_size != 0)
1946 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001947 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
1948 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001949 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001950 field = FieldDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00001951 record_decl,
1952 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00001953 SourceLocation(),
Greg Clayton8cf05932010-07-22 18:30:50 +00001954 name ? &identifier_table->get(name) : NULL, // Identifier
1955 QualType::getFromOpaquePtr(field_type), // Field type
Sean Callanancc427fa2011-07-30 02:42:06 +00001956 NULL, // TInfo *
Greg Clayton8cf05932010-07-22 18:30:50 +00001957 bit_width, // BitWidth
Sean Callanancc427fa2011-07-30 02:42:06 +00001958 false, // Mutable
1959 false); // HasInit
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001960
Sean Callanan5ed3ac12012-07-13 20:01:02 +00001961 if (!name) {
1962 // Determine whether this field corresponds to an anonymous
1963 // struct or union.
1964 if (const TagType *TagT = field->getType()->getAs<TagType>()) {
1965 if (RecordDecl *Rec = dyn_cast<RecordDecl>(TagT->getDecl()))
1966 if (!Rec->getDeclName()) {
1967 Rec->setAnonymousStructOrUnion(true);
1968 field->setImplicit();
1969
1970 }
1971 }
1972 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001973
Greg Clayton8cf05932010-07-22 18:30:50 +00001974 field->setAccess (ConvertAccessTypeToAccessSpecifier (access));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001975
1976 if (field)
1977 {
1978 record_decl->addDecl(field);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001979
1980#ifdef LLDB_CONFIGURATION_DEBUG
1981 VerifyDecl(field);
1982#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001983 }
1984 }
Greg Clayton9e409562010-07-28 02:04:09 +00001985 else
1986 {
Sean Callanan78e37602011-01-27 04:42:51 +00001987 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001988 if (objc_class_type)
1989 {
Greg Clayton0fffff52010-09-24 05:15:53 +00001990 bool is_synthesized = false;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001991 field = ClangASTContext::AddObjCClassIVar (ast,
Sean Callanan6e6a7c72010-09-16 20:01:08 +00001992 record_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001993 name,
1994 field_type,
1995 access,
1996 bitfield_bit_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00001997 is_synthesized);
Greg Clayton9e409562010-07-28 02:04:09 +00001998 }
1999 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002000 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002001 return field;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002002}
2003
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002004static clang::AccessSpecifier UnifyAccessSpecifiers (clang::AccessSpecifier lhs,
2005 clang::AccessSpecifier rhs)
2006{
2007 clang::AccessSpecifier ret = lhs;
2008
2009 // Make the access equal to the stricter of the field and the nested field's access
2010 switch (ret)
2011 {
2012 case clang::AS_none:
2013 break;
2014 case clang::AS_private:
2015 break;
2016 case clang::AS_protected:
2017 if (rhs == AS_private)
2018 ret = AS_private;
2019 break;
2020 case clang::AS_public:
2021 ret = rhs;
2022 break;
2023 }
2024
2025 return ret;
2026}
2027
2028void
2029ClangASTContext::BuildIndirectFields (clang::ASTContext *ast,
2030 lldb::clang_type_t record_clang_type)
2031{
2032 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
2033
2034 const RecordType *record_type = record_qual_type->getAs<RecordType>();
2035
2036 if (!record_type)
2037 return;
2038
2039 RecordDecl *record_decl = record_type->getDecl();
2040
2041 if (!record_decl)
2042 return;
2043
2044 typedef llvm::SmallVector <IndirectFieldDecl *, 1> IndirectFieldVector;
2045
2046 IndirectFieldVector indirect_fields;
2047
2048 for (RecordDecl::field_iterator fi = record_decl->field_begin(), fe = record_decl->field_end();
2049 fi != fe;
2050 ++fi)
2051 {
2052 if (fi->isAnonymousStructOrUnion())
2053 {
2054 QualType field_qual_type = fi->getType();
2055
2056 const RecordType *field_record_type = field_qual_type->getAs<RecordType>();
2057
2058 if (!field_record_type)
2059 continue;
2060
2061 RecordDecl *field_record_decl = field_record_type->getDecl();
2062
2063 if (!field_record_decl)
2064 continue;
2065
2066 for (RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
2067 di != de;
2068 ++di)
2069 {
2070 if (FieldDecl *nested_field_decl = dyn_cast<FieldDecl>(*di))
2071 {
2072 NamedDecl **chain = new (*ast) NamedDecl*[2];
2073 chain[0] = *fi;
2074 chain[1] = nested_field_decl;
2075 IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*ast,
2076 record_decl,
2077 SourceLocation(),
2078 nested_field_decl->getIdentifier(),
2079 nested_field_decl->getType(),
2080 chain,
2081 2);
2082
2083 indirect_field->setAccess(UnifyAccessSpecifiers(fi->getAccess(),
2084 nested_field_decl->getAccess()));
2085
2086 indirect_fields.push_back(indirect_field);
2087 }
2088 else if (IndirectFieldDecl *nested_indirect_field_decl = dyn_cast<IndirectFieldDecl>(*di))
2089 {
2090 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
2091 NamedDecl **chain = new (*ast) NamedDecl*[nested_chain_size + 1];
2092 chain[0] = *fi;
2093
2094 int chain_index = 1;
2095 for (IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
2096 nce = nested_indirect_field_decl->chain_end();
2097 nci < nce;
2098 ++nci)
2099 {
2100 chain[chain_index] = *nci;
2101 chain_index++;
2102 }
2103
2104 IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*ast,
2105 record_decl,
2106 SourceLocation(),
2107 nested_indirect_field_decl->getIdentifier(),
2108 nested_indirect_field_decl->getType(),
2109 chain,
2110 nested_chain_size + 1);
2111
2112 indirect_field->setAccess(UnifyAccessSpecifiers(fi->getAccess(),
2113 nested_indirect_field_decl->getAccess()));
2114
2115 indirect_fields.push_back(indirect_field);
2116 }
2117 }
2118 }
2119 }
2120
2121 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
2122 ifi < ife;
2123 ++ifi)
2124 {
2125 record_decl->addDecl(*ifi);
2126 }
2127}
2128
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002129bool
2130ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
2131{
2132 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
2133}
2134
2135bool
2136ClangASTContext::FieldIsBitfield
2137(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002138 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002139 FieldDecl* field,
2140 uint32_t& bitfield_bit_size
2141)
2142{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002143 if (ast == NULL || field == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002144 return false;
2145
2146 if (field->isBitField())
2147 {
2148 Expr* bit_width_expr = field->getBitWidth();
2149 if (bit_width_expr)
2150 {
2151 llvm::APSInt bit_width_apsint;
Greg Clayton6beaaa62011-01-17 03:46:26 +00002152 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002153 {
2154 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
2155 return true;
2156 }
2157 }
2158 }
2159 return false;
2160}
2161
2162bool
2163ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
2164{
2165 if (record_decl == NULL)
2166 return false;
2167
2168 if (!record_decl->field_empty())
2169 return true;
2170
2171 // No fields, lets check this is a CXX record and check the base classes
2172 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2173 if (cxx_record_decl)
2174 {
2175 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2176 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2177 base_class != base_class_end;
2178 ++base_class)
2179 {
2180 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2181 if (RecordHasFields(base_class_decl))
2182 return true;
2183 }
2184 }
2185 return false;
2186}
2187
2188void
Greg Clayton6beaaa62011-01-17 03:46:26 +00002189ClangASTContext::SetDefaultAccessForRecordFields (clang_type_t clang_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002190{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002191 if (clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002192 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002193 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
2194
Sean Callanan78e37602011-01-27 04:42:51 +00002195 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +00002196 if (record_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002197 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002198 RecordDecl *record_decl = record_type->getDecl();
2199 if (record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002200 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002201 uint32_t field_idx;
2202 RecordDecl::field_iterator field, field_end;
2203 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2204 field != field_end;
2205 ++field, ++field_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002206 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002207 // If no accessibility was assigned, assign the correct one
2208 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2209 field->setAccess ((AccessSpecifier)default_accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002210 }
2211 }
2212 }
2213 }
2214}
2215
2216#pragma mark C++ Base Classes
2217
2218CXXBaseSpecifier *
Greg Clayton1be10fc2010-09-29 01:12:09 +00002219ClangASTContext::CreateBaseClassSpecifier (clang_type_t base_class_type, AccessType access, bool is_virtual, bool base_of_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002220{
2221 if (base_class_type)
Greg Claytone6371122010-07-30 20:30:44 +00002222 return new CXXBaseSpecifier (SourceRange(),
2223 is_virtual,
2224 base_of_class,
2225 ConvertAccessTypeToAccessSpecifier (access),
Sean Callanan2c777c42011-01-18 23:32:05 +00002226 getASTContext()->CreateTypeSourceInfo (QualType::getFromOpaquePtr(base_class_type)),
2227 SourceLocation());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002228 return NULL;
2229}
2230
Greg Clayton0b42ac32010-07-02 01:29:13 +00002231void
2232ClangASTContext::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes)
2233{
2234 for (unsigned i=0; i<num_base_classes; ++i)
2235 {
2236 delete base_classes[i];
2237 base_classes[i] = NULL;
2238 }
2239}
2240
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002241bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002242ClangASTContext::SetBaseClassesForClassType (clang_type_t class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002243{
2244 if (class_clang_type)
2245 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002246 CXXRecordDecl *cxx_record_decl = QualType::getFromOpaquePtr(class_clang_type)->getAsCXXRecordDecl();
2247 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002248 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002249 cxx_record_decl->setBases(base_classes, num_base_classes);
2250 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002251 }
2252 }
2253 return false;
2254}
Greg Clayton8cf05932010-07-22 18:30:50 +00002255#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002256
Greg Clayton1be10fc2010-09-29 01:12:09 +00002257clang_type_t
Sean Callananad880762012-04-18 01:06:17 +00002258ClangASTContext::CreateObjCClass
Greg Clayton8cf05932010-07-22 18:30:50 +00002259(
2260 const char *name,
2261 DeclContext *decl_ctx,
2262 bool isForwardDecl,
Sean Callananad880762012-04-18 01:06:17 +00002263 bool isInternal,
2264 uint64_t metadata
Greg Clayton8cf05932010-07-22 18:30:50 +00002265)
2266{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002267 ASTContext *ast = getASTContext();
2268 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00002269 assert (name && name[0]);
2270 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00002271 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00002272
2273 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
2274 // we will need to update this code. I was told to currently always use
2275 // the CXXRecordDecl class since we often don't know from debug information
2276 // if something is struct or a class, so we default to always use the more
2277 // complete definition just in case.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002278 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00002279 decl_ctx,
2280 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00002281 &ast->Idents.get(name),
Sean Callanan5b26f272012-02-04 08:49:35 +00002282 NULL,
Greg Clayton8cf05932010-07-22 18:30:50 +00002283 SourceLocation(),
Sean Callanan5b26f272012-02-04 08:49:35 +00002284 /*isForwardDecl,*/
Greg Clayton8cf05932010-07-22 18:30:50 +00002285 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00002286
Sean Callananad880762012-04-18 01:06:17 +00002287 if (decl)
2288 SetMetadata(ast, (uintptr_t)decl, metadata);
2289
Greg Clayton6beaaa62011-01-17 03:46:26 +00002290 return ast->getObjCInterfaceType(decl).getAsOpaquePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002291}
2292
2293bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002294ClangASTContext::SetObjCSuperClass (clang_type_t class_opaque_type, clang_type_t super_opaque_type)
Greg Clayton8cf05932010-07-22 18:30:50 +00002295{
2296 if (class_opaque_type && super_opaque_type)
2297 {
2298 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2299 QualType super_qual_type(QualType::getFromOpaquePtr(super_opaque_type));
Sean Callanan78e37602011-01-27 04:42:51 +00002300 const clang::Type *class_type = class_qual_type.getTypePtr();
2301 const clang::Type *super_type = super_qual_type.getTypePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002302 if (class_type && super_type)
2303 {
Sean Callanan78e37602011-01-27 04:42:51 +00002304 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
2305 const ObjCObjectType *objc_super_type = dyn_cast<ObjCObjectType>(super_type);
Greg Clayton8cf05932010-07-22 18:30:50 +00002306 if (objc_class_type && objc_super_type)
2307 {
2308 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2309 ObjCInterfaceDecl *super_interface_decl = objc_super_type->getInterface();
2310 if (class_interface_decl && super_interface_decl)
2311 {
2312 class_interface_decl->setSuperClass(super_interface_decl);
2313 return true;
2314 }
2315 }
2316 }
2317 }
2318 return false;
2319}
2320
2321
Jim Inghame3ae82a2011-11-12 01:36:43 +00002322FieldDecl *
Greg Clayton8cf05932010-07-22 18:30:50 +00002323ClangASTContext::AddObjCClassIVar
2324(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002325 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002326 clang_type_t class_opaque_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00002327 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002328 clang_type_t ivar_opaque_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00002329 AccessType access,
2330 uint32_t bitfield_bit_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00002331 bool is_synthesized
Greg Clayton8cf05932010-07-22 18:30:50 +00002332)
2333{
2334 if (class_opaque_type == NULL || ivar_opaque_type == NULL)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002335 return NULL;
Greg Clayton8cf05932010-07-22 18:30:50 +00002336
Jim Inghame3ae82a2011-11-12 01:36:43 +00002337 ObjCIvarDecl *field = NULL;
2338
Greg Clayton6beaaa62011-01-17 03:46:26 +00002339 IdentifierTable *identifier_table = &ast->Idents;
Greg Clayton8cf05932010-07-22 18:30:50 +00002340
Greg Clayton6beaaa62011-01-17 03:46:26 +00002341 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00002342 assert (identifier_table != NULL);
2343
2344 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2345
Sean Callanan78e37602011-01-27 04:42:51 +00002346 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002347 if (class_type)
2348 {
Sean Callanan78e37602011-01-27 04:42:51 +00002349 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton8cf05932010-07-22 18:30:50 +00002350
2351 if (objc_class_type)
2352 {
2353 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2354
2355 if (class_interface_decl)
2356 {
2357 clang::Expr *bit_width = NULL;
2358 if (bitfield_bit_size != 0)
2359 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002360 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
2361 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
Greg Clayton8cf05932010-07-22 18:30:50 +00002362 }
2363
Jim Inghame3ae82a2011-11-12 01:36:43 +00002364 field = ObjCIvarDecl::Create (*ast,
2365 class_interface_decl,
2366 SourceLocation(),
2367 SourceLocation(),
2368 &identifier_table->get(name), // Identifier
2369 QualType::getFromOpaquePtr(ivar_opaque_type), // Field type
2370 NULL, // TypeSourceInfo *
2371 ConvertAccessTypeToObjCIvarAccessControl (access),
2372 bit_width,
2373 is_synthesized);
Greg Clayton9e409562010-07-28 02:04:09 +00002374
2375 if (field)
2376 {
2377 class_interface_decl->addDecl(field);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002378
2379#ifdef LLDB_CONFIGURATION_DEBUG
2380 VerifyDecl(field);
2381#endif
2382
Jim Inghame3ae82a2011-11-12 01:36:43 +00002383 return field;
Greg Clayton9e409562010-07-28 02:04:09 +00002384 }
Greg Clayton8cf05932010-07-22 18:30:50 +00002385 }
2386 }
2387 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002388 return NULL;
2389}
2390
2391bool
2392ClangASTContext::AddObjCClassProperty
2393(
2394 ASTContext *ast,
2395 clang_type_t class_opaque_type,
2396 const char *property_name,
2397 clang_type_t property_opaque_type,
2398 ObjCIvarDecl *ivar_decl,
2399 const char *property_setter_name,
2400 const char *property_getter_name,
Sean Callananad880762012-04-18 01:06:17 +00002401 uint32_t property_attributes,
2402 uint64_t metadata
Jim Inghame3ae82a2011-11-12 01:36:43 +00002403)
2404{
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002405 if (class_opaque_type == NULL || property_name == NULL || property_name[0] == '\0')
Jim Inghame3ae82a2011-11-12 01:36:43 +00002406 return false;
2407
2408 IdentifierTable *identifier_table = &ast->Idents;
2409
2410 assert (ast != NULL);
2411 assert (identifier_table != NULL);
2412
2413 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2414 const clang::Type *class_type = class_qual_type.getTypePtr();
2415 if (class_type)
2416 {
2417 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
2418
2419 if (objc_class_type)
2420 {
2421 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2422
Greg Clayton23f59502012-07-17 03:23:13 +00002423 clang_type_t property_opaque_type_to_access = NULL;
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002424
2425 if (property_opaque_type)
2426 property_opaque_type_to_access = property_opaque_type;
2427 else if (ivar_decl)
2428 property_opaque_type_to_access = ivar_decl->getType().getAsOpaquePtr();
2429
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002430 if (class_interface_decl && property_opaque_type_to_access)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002431 {
2432 clang::TypeSourceInfo *prop_type_source;
2433 if (ivar_decl)
2434 prop_type_source = ast->CreateTypeSourceInfo (ivar_decl->getType());
2435 else
2436 prop_type_source = ast->CreateTypeSourceInfo (QualType::getFromOpaquePtr(property_opaque_type));
2437
2438 ObjCPropertyDecl *property_decl = ObjCPropertyDecl::Create(*ast,
2439 class_interface_decl,
2440 SourceLocation(), // Source Location
2441 &identifier_table->get(property_name),
2442 SourceLocation(), //Source Location for AT
Sean Callanand5f33a82012-03-01 02:03:47 +00002443 SourceLocation(), //Source location for (
Jim Inghame3ae82a2011-11-12 01:36:43 +00002444 prop_type_source
2445 );
Sean Callananad880762012-04-18 01:06:17 +00002446
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002447 if (property_decl)
2448 {
Sean Callananad880762012-04-18 01:06:17 +00002449 SetMetadata(ast, (uintptr_t)property_decl, metadata);
2450
Jim Inghame3ae82a2011-11-12 01:36:43 +00002451 class_interface_decl->addDecl (property_decl);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002452
2453 Selector setter_sel, getter_sel;
2454
Jim Inghame3ae82a2011-11-12 01:36:43 +00002455 if (property_setter_name != NULL)
2456 {
2457 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
2458 clang::IdentifierInfo *setter_ident = &identifier_table->get(property_setter_no_colon.c_str());
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002459 setter_sel = ast->Selectors.getSelector(1, &setter_ident);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002460 }
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002461 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
2462 {
2463 std::string setter_sel_string("set");
2464 setter_sel_string.push_back(::toupper(property_name[0]));
2465 setter_sel_string.append(&property_name[1]);
2466 clang::IdentifierInfo *setter_ident = &identifier_table->get(setter_sel_string.c_str());
2467 setter_sel = ast->Selectors.getSelector(1, &setter_ident);
2468 }
Sean Callanana6582262012-04-05 00:12:52 +00002469 property_decl->setSetterName(setter_sel);
2470 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002471
2472 if (property_getter_name != NULL)
2473 {
2474 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_getter_name);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002475 getter_sel = ast->Selectors.getSelector(0, &getter_ident);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002476 }
2477 else
2478 {
2479 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_name);
2480 getter_sel = ast->Selectors.getSelector(0, &getter_ident);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002481 }
Sean Callanana6582262012-04-05 00:12:52 +00002482 property_decl->setGetterName(getter_sel);
2483 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002484
2485 if (ivar_decl)
2486 property_decl->setPropertyIvarDecl (ivar_decl);
2487
2488 if (property_attributes & DW_APPLE_PROPERTY_readonly)
2489 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
2490 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
2491 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
2492 if (property_attributes & DW_APPLE_PROPERTY_assign)
2493 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
2494 if (property_attributes & DW_APPLE_PROPERTY_retain)
2495 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
2496 if (property_attributes & DW_APPLE_PROPERTY_copy)
2497 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
2498 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
2499 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002500
2501 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
2502 {
2503 QualType result_type = QualType::getFromOpaquePtr(property_opaque_type_to_access);
2504
2505 const bool isInstance = true;
2506 const bool isVariadic = false;
2507 const bool isSynthesized = false;
2508 const bool isImplicitlyDeclared = true;
2509 const bool isDefined = false;
2510 const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
2511 const bool HasRelatedResultType = false;
2512
2513 ObjCMethodDecl *getter = ObjCMethodDecl::Create(*ast,
2514 SourceLocation(),
2515 SourceLocation(),
2516 getter_sel,
2517 result_type,
2518 NULL,
2519 class_interface_decl,
2520 isInstance,
2521 isVariadic,
2522 isSynthesized,
2523 isImplicitlyDeclared,
2524 isDefined,
2525 impControl,
2526 HasRelatedResultType);
Sean Callananad880762012-04-18 01:06:17 +00002527
2528 if (getter)
2529 SetMetadata(ast, (uintptr_t)getter, metadata);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002530
2531 getter->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(), ArrayRef<SourceLocation>());
2532
2533 class_interface_decl->addDecl(getter);
2534 }
2535
2536 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
2537 {
2538 QualType result_type = ast->VoidTy;
2539
2540 const bool isInstance = true;
2541 const bool isVariadic = false;
2542 const bool isSynthesized = false;
2543 const bool isImplicitlyDeclared = true;
2544 const bool isDefined = false;
2545 const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
2546 const bool HasRelatedResultType = false;
2547
2548 ObjCMethodDecl *setter = ObjCMethodDecl::Create(*ast,
2549 SourceLocation(),
2550 SourceLocation(),
2551 setter_sel,
2552 result_type,
2553 NULL,
2554 class_interface_decl,
2555 isInstance,
2556 isVariadic,
2557 isSynthesized,
2558 isImplicitlyDeclared,
2559 isDefined,
2560 impControl,
2561 HasRelatedResultType);
2562
Sean Callananad880762012-04-18 01:06:17 +00002563 if (setter)
2564 SetMetadata(ast, (uintptr_t)setter, metadata);
2565
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002566 llvm::SmallVector<ParmVarDecl *, 1> params;
2567
2568 params.push_back (ParmVarDecl::Create (*ast,
2569 setter,
2570 SourceLocation(),
2571 SourceLocation(),
2572 NULL, // anonymous
2573 QualType::getFromOpaquePtr(property_opaque_type_to_access),
2574 NULL,
2575 SC_Auto,
2576 SC_Auto,
2577 NULL));
2578
2579 setter->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
2580
2581 class_interface_decl->addDecl(setter);
2582 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002583
2584 return true;
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002585 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002586 }
2587 }
2588 }
Greg Clayton8cf05932010-07-22 18:30:50 +00002589 return false;
2590}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002591
Greg Clayton9e409562010-07-28 02:04:09 +00002592bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002593ClangASTContext::ObjCTypeHasIVars (clang_type_t class_opaque_type, bool check_superclass)
Greg Clayton9e409562010-07-28 02:04:09 +00002594{
2595 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2596
Sean Callanan78e37602011-01-27 04:42:51 +00002597 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton9e409562010-07-28 02:04:09 +00002598 if (class_type)
2599 {
Sean Callanan78e37602011-01-27 04:42:51 +00002600 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00002601
2602 if (objc_class_type)
2603 return ObjCDeclHasIVars (objc_class_type->getInterface(), check_superclass);
2604 }
2605 return false;
2606}
2607
2608bool
2609ClangASTContext::ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
2610{
2611 while (class_interface_decl)
2612 {
2613 if (class_interface_decl->ivar_size() > 0)
2614 return true;
2615
2616 if (check_superclass)
2617 class_interface_decl = class_interface_decl->getSuperClass();
2618 else
2619 break;
2620 }
2621 return false;
2622}
Greg Clayton0fffff52010-09-24 05:15:53 +00002623
Greg Clayton1be10fc2010-09-29 01:12:09 +00002624ObjCMethodDecl *
Greg Clayton0fffff52010-09-24 05:15:53 +00002625ClangASTContext::AddMethodToObjCObjectType
2626(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002627 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002628 clang_type_t class_opaque_type,
Greg Clayton0fffff52010-09-24 05:15:53 +00002629 const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
Greg Clayton1be10fc2010-09-29 01:12:09 +00002630 clang_type_t method_opaque_type,
Greg Clayton0fffff52010-09-24 05:15:53 +00002631 lldb::AccessType access
2632)
2633{
2634 if (class_opaque_type == NULL || method_opaque_type == NULL)
2635 return NULL;
2636
Greg Clayton6beaaa62011-01-17 03:46:26 +00002637 IdentifierTable *identifier_table = &ast->Idents;
Greg Clayton0fffff52010-09-24 05:15:53 +00002638
Greg Clayton6beaaa62011-01-17 03:46:26 +00002639 assert (ast != NULL);
Greg Clayton0fffff52010-09-24 05:15:53 +00002640 assert (identifier_table != NULL);
2641
2642 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2643
Sean Callanan78e37602011-01-27 04:42:51 +00002644 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton0fffff52010-09-24 05:15:53 +00002645 if (class_type == NULL)
2646 return NULL;
2647
Sean Callanan78e37602011-01-27 04:42:51 +00002648 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton0fffff52010-09-24 05:15:53 +00002649
2650 if (objc_class_type == NULL)
2651 return NULL;
2652
2653 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2654
2655 if (class_interface_decl == NULL)
2656 return NULL;
Greg Clayton9e409562010-07-28 02:04:09 +00002657
Greg Clayton0fffff52010-09-24 05:15:53 +00002658 const char *selector_start = ::strchr (name, ' ');
2659 if (selector_start == NULL)
2660 return NULL;
2661
2662 selector_start++;
2663 if (!(::isalpha (selector_start[0]) || selector_start[0] == '_'))
2664 return NULL;
2665 llvm::SmallVector<IdentifierInfo *, 12> selector_idents;
2666
Greg Clayton450e3f32010-10-12 02:24:53 +00002667 size_t len = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +00002668 const char *start;
Greg Clayton450e3f32010-10-12 02:24:53 +00002669 //printf ("name = '%s'\n", name);
2670
2671 unsigned num_selectors_with_args = 0;
2672 for (start = selector_start;
Greg Clayton0fffff52010-09-24 05:15:53 +00002673 start && *start != '\0' && *start != ']';
Greg Clayton450e3f32010-10-12 02:24:53 +00002674 start += len)
Greg Clayton0fffff52010-09-24 05:15:53 +00002675 {
Greg Clayton450e3f32010-10-12 02:24:53 +00002676 len = ::strcspn(start, ":]");
Greg Clayton90f90cd2010-10-27 04:01:14 +00002677 bool has_arg = (start[len] == ':');
2678 if (has_arg)
Greg Clayton450e3f32010-10-12 02:24:53 +00002679 ++num_selectors_with_args;
Greg Clayton0fffff52010-09-24 05:15:53 +00002680 selector_idents.push_back (&identifier_table->get (StringRef (start, len)));
Greg Clayton90f90cd2010-10-27 04:01:14 +00002681 if (has_arg)
2682 len += 1;
Greg Clayton0fffff52010-09-24 05:15:53 +00002683 }
2684
2685
2686 if (selector_idents.size() == 0)
2687 return 0;
2688
Greg Clayton6beaaa62011-01-17 03:46:26 +00002689 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
Greg Clayton0fffff52010-09-24 05:15:53 +00002690 selector_idents.data());
2691
2692 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
2693
2694 // Populate the method decl with parameter decls
Sean Callanan78e37602011-01-27 04:42:51 +00002695 const clang::Type *method_type(method_qual_type.getTypePtr());
Greg Clayton0fffff52010-09-24 05:15:53 +00002696
2697 if (method_type == NULL)
2698 return NULL;
2699
Sean Callanan78e37602011-01-27 04:42:51 +00002700 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type));
Greg Clayton0fffff52010-09-24 05:15:53 +00002701
2702 if (!method_function_prototype)
2703 return NULL;
2704
2705
2706 bool is_variadic = false;
2707 bool is_synthesized = false;
2708 bool is_defined = false;
2709 ObjCMethodDecl::ImplementationControl imp_control = ObjCMethodDecl::None;
2710
2711 const unsigned num_args = method_function_prototype->getNumArgs();
2712
Greg Clayton6beaaa62011-01-17 03:46:26 +00002713 ObjCMethodDecl *objc_method_decl = ObjCMethodDecl::Create (*ast,
Greg Clayton0fffff52010-09-24 05:15:53 +00002714 SourceLocation(), // beginLoc,
2715 SourceLocation(), // endLoc,
2716 method_selector,
2717 method_function_prototype->getResultType(),
2718 NULL, // TypeSourceInfo *ResultTInfo,
2719 GetDeclContextForType (class_opaque_type),
2720 name[0] == '-',
2721 is_variadic,
2722 is_synthesized,
Sean Callanan880e6802011-10-07 23:18:13 +00002723 true, // is_implicitly_declared
Greg Clayton0fffff52010-09-24 05:15:53 +00002724 is_defined,
2725 imp_control,
Sean Callanan880e6802011-10-07 23:18:13 +00002726 false /*has_related_result_type*/);
Greg Clayton0fffff52010-09-24 05:15:53 +00002727
2728
2729 if (objc_method_decl == NULL)
2730 return NULL;
2731
2732 if (num_args > 0)
2733 {
2734 llvm::SmallVector<ParmVarDecl *, 12> params;
2735
2736 for (int param_index = 0; param_index < num_args; ++param_index)
2737 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002738 params.push_back (ParmVarDecl::Create (*ast,
Greg Clayton0fffff52010-09-24 05:15:53 +00002739 objc_method_decl,
2740 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00002741 SourceLocation(),
Greg Clayton0fffff52010-09-24 05:15:53 +00002742 NULL, // anonymous
2743 method_function_prototype->getArgType(param_index),
2744 NULL,
2745 SC_Auto,
2746 SC_Auto,
2747 NULL));
2748 }
2749
Sean Callanan880e6802011-10-07 23:18:13 +00002750 objc_method_decl->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
Greg Clayton0fffff52010-09-24 05:15:53 +00002751 }
2752
2753 class_interface_decl->addDecl (objc_method_decl);
2754
Sean Callanan5e9e1992011-10-26 01:06:27 +00002755#ifdef LLDB_CONFIGURATION_DEBUG
2756 VerifyDecl(objc_method_decl);
2757#endif
Greg Clayton0fffff52010-09-24 05:15:53 +00002758
2759 return objc_method_decl;
2760}
2761
Greg Clayton402230e2012-02-03 01:30:30 +00002762size_t
2763ClangASTContext::GetNumTemplateArguments (clang::ASTContext *ast, clang_type_t clang_type)
2764{
2765 if (clang_type)
2766 {
2767 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2768
2769 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2770 switch (type_class)
2771 {
2772 case clang::Type::Record:
2773 if (GetCompleteQualType (ast, qual_type))
2774 {
2775 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2776 if (cxx_record_decl)
2777 {
2778 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2779 if (template_decl)
2780 return template_decl->getTemplateArgs().size();
2781 }
2782 }
2783 break;
2784
2785 case clang::Type::Typedef:
2786 return ClangASTContext::GetNumTemplateArguments (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2787 default:
2788 break;
2789 }
2790 }
2791 return 0;
2792}
2793
2794clang_type_t
2795ClangASTContext::GetTemplateArgument (clang::ASTContext *ast, clang_type_t clang_type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
2796{
2797 if (clang_type)
2798 {
2799 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2800
2801 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2802 switch (type_class)
2803 {
2804 case clang::Type::Record:
2805 if (GetCompleteQualType (ast, qual_type))
2806 {
2807 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2808 if (cxx_record_decl)
2809 {
2810 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2811 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
2812 {
2813 const TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
2814 switch (template_arg.getKind())
2815 {
2816 case clang::TemplateArgument::Null:
2817 kind = eTemplateArgumentKindNull;
2818 return NULL;
2819
2820 case clang::TemplateArgument::Type:
2821 kind = eTemplateArgumentKindType;
2822 return template_arg.getAsType().getAsOpaquePtr();
2823
2824 case clang::TemplateArgument::Declaration:
2825 kind = eTemplateArgumentKindDeclaration;
2826 return NULL;
2827
2828 case clang::TemplateArgument::Integral:
2829 kind = eTemplateArgumentKindIntegral;
2830 return template_arg.getIntegralType().getAsOpaquePtr();
2831
2832 case clang::TemplateArgument::Template:
2833 kind = eTemplateArgumentKindTemplate;
2834 return NULL;
2835
2836 case clang::TemplateArgument::TemplateExpansion:
2837 kind = eTemplateArgumentKindTemplateExpansion;
2838 return NULL;
2839
2840 case clang::TemplateArgument::Expression:
2841 kind = eTemplateArgumentKindExpression;
2842 return NULL;
2843
2844 case clang::TemplateArgument::Pack:
2845 kind = eTemplateArgumentKindPack;
2846 return NULL;
2847
2848 default:
2849 assert (!"Unhandled TemplateArgument::ArgKind");
2850 kind = eTemplateArgumentKindNull;
2851 return NULL;
2852 }
2853 }
2854 }
2855 }
2856 break;
2857
2858 case clang::Type::Typedef:
2859 return ClangASTContext::GetTemplateArgument (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), arg_idx, kind);
2860 default:
2861 break;
2862 }
2863 }
2864 kind = eTemplateArgumentKindNull;
2865 return NULL;
2866}
Greg Clayton0fffff52010-09-24 05:15:53 +00002867
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002868uint32_t
Greg Clayton73b472d2010-10-27 03:32:59 +00002869ClangASTContext::GetTypeInfo
2870(
2871 clang_type_t clang_type,
Greg Clayton6beaaa62011-01-17 03:46:26 +00002872 clang::ASTContext *ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00002873 clang_type_t *pointee_or_element_clang_type
2874)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002875{
2876 if (clang_type == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00002877 return 0;
2878
2879 if (pointee_or_element_clang_type)
2880 *pointee_or_element_clang_type = NULL;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002881
2882 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2883
2884 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2885 switch (type_class)
2886 {
Sean Callanana2424172010-10-25 00:29:48 +00002887 case clang::Type::Builtin:
2888 switch (cast<clang::BuiltinType>(qual_type)->getKind())
2889 {
Sean Callanana2424172010-10-25 00:29:48 +00002890 case clang::BuiltinType::ObjCId:
2891 case clang::BuiltinType::ObjCClass:
Greg Clayton6beaaa62011-01-17 03:46:26 +00002892 if (ast && pointee_or_element_clang_type)
2893 *pointee_or_element_clang_type = ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Sean Callanana2424172010-10-25 00:29:48 +00002894 return eTypeIsBuiltIn | eTypeIsPointer | eTypeHasValue;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002895 break;
2896 case clang::BuiltinType::Bool:
2897 case clang::BuiltinType::Char_U:
2898 case clang::BuiltinType::UChar:
2899 case clang::BuiltinType::WChar_U:
2900 case clang::BuiltinType::Char16:
2901 case clang::BuiltinType::Char32:
2902 case clang::BuiltinType::UShort:
2903 case clang::BuiltinType::UInt:
2904 case clang::BuiltinType::ULong:
2905 case clang::BuiltinType::ULongLong:
2906 case clang::BuiltinType::UInt128:
2907 case clang::BuiltinType::Char_S:
2908 case clang::BuiltinType::SChar:
2909 case clang::BuiltinType::WChar_S:
2910 case clang::BuiltinType::Short:
2911 case clang::BuiltinType::Int:
2912 case clang::BuiltinType::Long:
2913 case clang::BuiltinType::LongLong:
2914 case clang::BuiltinType::Int128:
2915 case clang::BuiltinType::Float:
2916 case clang::BuiltinType::Double:
2917 case clang::BuiltinType::LongDouble:
2918 return eTypeIsBuiltIn | eTypeHasValue | eTypeIsScalar;
Greg Clayton73b472d2010-10-27 03:32:59 +00002919 default:
2920 break;
Sean Callanana2424172010-10-25 00:29:48 +00002921 }
2922 return eTypeIsBuiltIn | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002923
2924 case clang::Type::BlockPointer:
2925 if (pointee_or_element_clang_type)
2926 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2927 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
2928
Greg Clayton49462ea2011-01-15 02:52:14 +00002929 case clang::Type::Complex: return eTypeIsBuiltIn | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002930
2931 case clang::Type::ConstantArray:
2932 case clang::Type::DependentSizedArray:
2933 case clang::Type::IncompleteArray:
2934 case clang::Type::VariableArray:
2935 if (pointee_or_element_clang_type)
2936 *pointee_or_element_clang_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
2937 return eTypeHasChildren | eTypeIsArray;
2938
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002939 case clang::Type::DependentName: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002940 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
2941 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
2942 case clang::Type::Decltype: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00002943
2944 case clang::Type::Enum:
2945 if (pointee_or_element_clang_type)
2946 *pointee_or_element_clang_type = cast<EnumType>(qual_type)->getDecl()->getIntegerType().getAsOpaquePtr();
2947 return eTypeIsEnumeration | eTypeHasValue;
2948
Sean Callanan912855f2011-08-11 23:56:13 +00002949 case clang::Type::Elaborated:
2950 return ClangASTContext::GetTypeInfo (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2951 ast,
2952 pointee_or_element_clang_type);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002953 case clang::Type::ExtVector: return eTypeHasChildren | eTypeIsVector;
2954 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
2955 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002956 case clang::Type::InjectedClassName: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00002957
2958 case clang::Type::LValueReference:
2959 case clang::Type::RValueReference:
2960 if (pointee_or_element_clang_type)
2961 *pointee_or_element_clang_type = cast<ReferenceType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr();
2962 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
2963
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002964 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002965
2966 case clang::Type::ObjCObjectPointer:
2967 if (pointee_or_element_clang_type)
2968 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2969 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
2970
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002971 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
2972 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
Greg Clayton73b472d2010-10-27 03:32:59 +00002973
2974 case clang::Type::Pointer:
2975 if (pointee_or_element_clang_type)
2976 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2977 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
2978
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002979 case clang::Type::Record:
2980 if (qual_type->getAsCXXRecordDecl())
2981 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
2982 else
2983 return eTypeHasChildren | eTypeIsStructUnion;
2984 break;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002985 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
2986 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
2987 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
Greg Clayton73b472d2010-10-27 03:32:59 +00002988
2989 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00002990 return eTypeIsTypedef | ClangASTContext::GetTypeInfo (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00002991 ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00002992 pointee_or_element_clang_type);
2993
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002994 case clang::Type::TypeOfExpr: return 0;
2995 case clang::Type::TypeOf: return 0;
2996 case clang::Type::UnresolvedUsing: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002997 case clang::Type::Vector: return eTypeHasChildren | eTypeIsVector;
2998 default: return 0;
2999 }
3000 return 0;
3001}
3002
Greg Clayton9e409562010-07-28 02:04:09 +00003003
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003004#pragma mark Aggregate Types
3005
3006bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00003007ClangASTContext::IsAggregateType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003008{
3009 if (clang_type == NULL)
3010 return false;
3011
3012 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3013
Greg Clayton737b9322010-09-13 03:32:57 +00003014 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3015 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003016 {
Greg Claytone1a916a2010-07-21 22:12:05 +00003017 case clang::Type::IncompleteArray:
3018 case clang::Type::VariableArray:
3019 case clang::Type::ConstantArray:
3020 case clang::Type::ExtVector:
3021 case clang::Type::Vector:
3022 case clang::Type::Record:
Greg Clayton9e409562010-07-28 02:04:09 +00003023 case clang::Type::ObjCObject:
3024 case clang::Type::ObjCInterface:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003025 return true;
Sean Callanan912855f2011-08-11 23:56:13 +00003026 case clang::Type::Elaborated:
3027 return ClangASTContext::IsAggregateType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Claytone1a916a2010-07-21 22:12:05 +00003028 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00003029 return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003030
3031 default:
3032 break;
3033 }
3034 // The clang type does have a value
3035 return false;
3036}
3037
3038uint32_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00003039ClangASTContext::GetNumChildren (clang::ASTContext *ast, clang_type_t clang_type, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003040{
Greg Clayton6beaaa62011-01-17 03:46:26 +00003041 if (clang_type == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003042 return 0;
3043
3044 uint32_t num_children = 0;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003045 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00003046 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3047 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003048 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003049 case clang::Type::Builtin:
3050 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3051 {
Greg Clayton73b472d2010-10-27 03:32:59 +00003052 case clang::BuiltinType::ObjCId: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003053 case clang::BuiltinType::ObjCClass: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003054 num_children = 1;
Greg Clayton73b472d2010-10-27 03:32:59 +00003055 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003056
3057 default:
3058 break;
3059 }
3060 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00003061
Greg Clayton49462ea2011-01-15 02:52:14 +00003062 case clang::Type::Complex: return 0;
Greg Clayton54979cd2010-12-15 05:08:08 +00003063
Greg Claytone1a916a2010-07-21 22:12:05 +00003064 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00003065 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003066 {
3067 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3068 const RecordDecl *record_decl = record_type->getDecl();
3069 assert(record_decl);
3070 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3071 if (cxx_record_decl)
3072 {
3073 if (omit_empty_base_classes)
3074 {
3075 // Check each base classes to see if it or any of its
3076 // base classes contain any fields. This can help
3077 // limit the noise in variable views by not having to
3078 // show base classes that contain no members.
3079 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3080 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3081 base_class != base_class_end;
3082 ++base_class)
3083 {
3084 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3085
3086 // Skip empty base classes
3087 if (RecordHasFields(base_class_decl) == false)
3088 continue;
3089
3090 num_children++;
3091 }
3092 }
3093 else
3094 {
3095 // Include all base classes
3096 num_children += cxx_record_decl->getNumBases();
3097 }
3098
3099 }
3100 RecordDecl::field_iterator field, field_end;
3101 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3102 ++num_children;
3103 }
3104 break;
3105
Greg Clayton9e409562010-07-28 02:04:09 +00003106 case clang::Type::ObjCObject:
3107 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00003108 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00003109 {
Sean Callanan78e37602011-01-27 04:42:51 +00003110 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003111 assert (objc_class_type);
3112 if (objc_class_type)
3113 {
3114 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3115
3116 if (class_interface_decl)
3117 {
3118
3119 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3120 if (superclass_interface_decl)
3121 {
3122 if (omit_empty_base_classes)
3123 {
3124 if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true))
3125 ++num_children;
3126 }
3127 else
3128 ++num_children;
3129 }
3130
3131 num_children += class_interface_decl->ivar_size();
3132 }
3133 }
3134 }
3135 break;
3136
3137 case clang::Type::ObjCObjectPointer:
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003138 {
Sean Callanan78e37602011-01-27 04:42:51 +00003139 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003140 QualType pointee_type = pointer_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003141 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3142 pointee_type.getAsOpaquePtr(),
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003143 omit_empty_base_classes);
3144 // If this type points to a simple type, then it has 1 child
3145 if (num_pointee_children == 0)
3146 num_children = 1;
3147 else
3148 num_children = num_pointee_children;
3149 }
3150 break;
Greg Clayton9e409562010-07-28 02:04:09 +00003151
Greg Claytone1a916a2010-07-21 22:12:05 +00003152 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003153 num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
3154 break;
3155
Greg Claytone1a916a2010-07-21 22:12:05 +00003156 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003157 {
Sean Callanan78e37602011-01-27 04:42:51 +00003158 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003159 QualType pointee_type (pointer_type->getPointeeType());
Greg Clayton6beaaa62011-01-17 03:46:26 +00003160 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3161 pointee_type.getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00003162 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003163 if (num_pointee_children == 0)
Greg Clayton54979cd2010-12-15 05:08:08 +00003164 {
3165 // We have a pointer to a pointee type that claims it has no children.
3166 // We will want to look at
3167 num_children = ClangASTContext::GetNumPointeeChildren (pointee_type.getAsOpaquePtr());
3168 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003169 else
3170 num_children = num_pointee_children;
3171 }
3172 break;
3173
Greg Clayton73b472d2010-10-27 03:32:59 +00003174 case clang::Type::LValueReference:
3175 case clang::Type::RValueReference:
3176 {
Sean Callanan78e37602011-01-27 04:42:51 +00003177 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton73b472d2010-10-27 03:32:59 +00003178 QualType pointee_type = reference_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003179 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3180 pointee_type.getAsOpaquePtr(),
Greg Clayton73b472d2010-10-27 03:32:59 +00003181 omit_empty_base_classes);
3182 // If this type points to a simple type, then it has 1 child
3183 if (num_pointee_children == 0)
3184 num_children = 1;
3185 else
3186 num_children = num_pointee_children;
3187 }
3188 break;
3189
3190
Greg Claytone1a916a2010-07-21 22:12:05 +00003191 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00003192 num_children = ClangASTContext::GetNumChildren (ast,
3193 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3194 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003195 break;
Sean Callanan912855f2011-08-11 23:56:13 +00003196
3197 case clang::Type::Elaborated:
3198 num_children = ClangASTContext::GetNumChildren (ast,
3199 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3200 omit_empty_base_classes);
3201 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003202
3203 default:
3204 break;
3205 }
3206 return num_children;
3207}
3208
Greg Claytonbf2331c2011-09-09 23:04:00 +00003209uint32_t
3210ClangASTContext::GetNumDirectBaseClasses (clang::ASTContext *ast, clang_type_t clang_type)
3211{
3212 if (clang_type == NULL)
3213 return 0;
3214
3215 uint32_t count = 0;
3216 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3217 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3218 switch (type_class)
3219 {
3220 case clang::Type::Record:
3221 if (GetCompleteQualType (ast, qual_type))
3222 {
3223 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3224 if (cxx_record_decl)
3225 count = cxx_record_decl->getNumBases();
3226 }
3227 break;
3228
3229 case clang::Type::ObjCObject:
3230 case clang::Type::ObjCInterface:
3231 if (GetCompleteQualType (ast, qual_type))
3232 {
3233 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3234 if (objc_class_type)
3235 {
3236 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3237
3238 if (class_interface_decl && class_interface_decl->getSuperClass())
3239 count = 1;
3240 }
3241 }
3242 break;
3243
3244
3245 case clang::Type::Typedef:
3246 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3247 break;
3248
3249 case clang::Type::Elaborated:
3250 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3251 break;
3252
3253 default:
3254 break;
3255 }
3256 return count;
3257}
3258
3259uint32_t
3260ClangASTContext::GetNumVirtualBaseClasses (clang::ASTContext *ast,
3261 clang_type_t clang_type)
3262{
3263 if (clang_type == NULL)
3264 return 0;
3265
3266 uint32_t count = 0;
3267 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3268 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3269 switch (type_class)
3270 {
3271 case clang::Type::Record:
3272 if (GetCompleteQualType (ast, qual_type))
3273 {
3274 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3275 if (cxx_record_decl)
3276 count = cxx_record_decl->getNumVBases();
3277 }
3278 break;
3279
3280 case clang::Type::Typedef:
3281 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3282 break;
3283
3284 case clang::Type::Elaborated:
3285 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3286 break;
3287
3288 default:
3289 break;
3290 }
3291 return count;
3292}
3293
3294uint32_t
3295ClangASTContext::GetNumFields (clang::ASTContext *ast, clang_type_t clang_type)
3296{
3297 if (clang_type == NULL)
3298 return 0;
3299
3300 uint32_t count = 0;
3301 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3302 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3303 switch (type_class)
3304 {
3305 case clang::Type::Record:
3306 if (GetCompleteQualType (ast, qual_type))
3307 {
3308 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
3309 if (record_type)
3310 {
3311 RecordDecl *record_decl = record_type->getDecl();
3312 if (record_decl)
3313 {
3314 uint32_t field_idx = 0;
3315 RecordDecl::field_iterator field, field_end;
3316 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3317 ++field_idx;
3318 count = field_idx;
3319 }
3320 }
3321 }
3322 break;
3323
3324 case clang::Type::Typedef:
3325 count = ClangASTContext::GetNumFields (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3326 break;
3327
3328 case clang::Type::Elaborated:
3329 count = ClangASTContext::GetNumFields (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3330 break;
3331
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003332 case clang::Type::ObjCObject:
3333 case clang::Type::ObjCInterface:
3334 if (GetCompleteQualType (ast, qual_type))
3335 {
3336 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3337 if (objc_class_type)
3338 {
3339 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3340
3341 if (class_interface_decl)
3342 count = class_interface_decl->ivar_size();
3343 }
3344 }
3345 break;
3346
Greg Claytonbf2331c2011-09-09 23:04:00 +00003347 default:
3348 break;
3349 }
3350 return count;
3351}
3352
3353clang_type_t
3354ClangASTContext::GetDirectBaseClassAtIndex (clang::ASTContext *ast,
3355 clang_type_t clang_type,
3356 uint32_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003357 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003358{
3359 if (clang_type == NULL)
3360 return 0;
3361
3362 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3363 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3364 switch (type_class)
3365 {
3366 case clang::Type::Record:
3367 if (GetCompleteQualType (ast, qual_type))
3368 {
3369 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3370 if (cxx_record_decl)
3371 {
3372 uint32_t curr_idx = 0;
3373 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3374 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3375 base_class != base_class_end;
3376 ++base_class, ++curr_idx)
3377 {
3378 if (curr_idx == idx)
3379 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003380 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003381 {
3382 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3383 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3384// if (base_class->isVirtual())
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003385// *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003386// else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003387 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003388 }
3389 return base_class->getType().getAsOpaquePtr();
3390 }
3391 }
3392 }
3393 }
3394 break;
3395
3396 case clang::Type::ObjCObject:
3397 case clang::Type::ObjCInterface:
3398 if (idx == 0 && GetCompleteQualType (ast, qual_type))
3399 {
3400 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3401 if (objc_class_type)
3402 {
3403 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3404
3405 if (class_interface_decl)
3406 {
3407 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3408 if (superclass_interface_decl)
3409 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003410 if (bit_offset_ptr)
3411 *bit_offset_ptr = 0;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003412 return ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr();
3413 }
3414 }
3415 }
3416 }
3417 break;
3418
3419
3420 case clang::Type::Typedef:
3421 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3422 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3423 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003424 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003425
3426 case clang::Type::Elaborated:
3427 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3428 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3429 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003430 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003431
3432 default:
3433 break;
3434 }
3435 return NULL;
3436}
3437
3438clang_type_t
3439ClangASTContext::GetVirtualBaseClassAtIndex (clang::ASTContext *ast,
3440 clang_type_t clang_type,
3441 uint32_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003442 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003443{
3444 if (clang_type == NULL)
3445 return 0;
3446
3447 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3448 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3449 switch (type_class)
3450 {
3451 case clang::Type::Record:
3452 if (GetCompleteQualType (ast, qual_type))
3453 {
3454 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3455 if (cxx_record_decl)
3456 {
3457 uint32_t curr_idx = 0;
3458 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3459 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
3460 base_class != base_class_end;
3461 ++base_class, ++curr_idx)
3462 {
3463 if (curr_idx == idx)
3464 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003465 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003466 {
3467 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3468 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003469 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003470
3471 }
3472 return base_class->getType().getAsOpaquePtr();
3473 }
3474 }
3475 }
3476 }
3477 break;
3478
3479 case clang::Type::Typedef:
3480 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3481 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3482 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003483 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003484
3485 case clang::Type::Elaborated:
3486 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3487 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3488 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003489 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003490
3491 default:
3492 break;
3493 }
3494 return NULL;
3495}
3496
3497clang_type_t
3498ClangASTContext::GetFieldAtIndex (clang::ASTContext *ast,
3499 clang_type_t clang_type,
3500 uint32_t idx,
3501 std::string& name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003502 uint64_t *bit_offset_ptr,
3503 uint32_t *bitfield_bit_size_ptr,
3504 bool *is_bitfield_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003505{
3506 if (clang_type == NULL)
3507 return 0;
3508
3509 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3510 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3511 switch (type_class)
3512 {
3513 case clang::Type::Record:
3514 if (GetCompleteQualType (ast, qual_type))
3515 {
3516 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3517 const RecordDecl *record_decl = record_type->getDecl();
3518 uint32_t field_idx = 0;
3519 RecordDecl::field_iterator field, field_end;
3520 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
3521 {
3522 if (idx == field_idx)
3523 {
3524 // Print the member type if requested
3525 // Print the member name and equal sign
3526 name.assign(field->getNameAsString());
3527
3528 // Figure out the type byte size (field_type_info.first) and
3529 // alignment (field_type_info.second) from the AST context.
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003530 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003531 {
3532 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003533 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003534 }
3535
Greg Clayton1811b4f2012-07-31 23:39:10 +00003536 const bool is_bitfield = field->isBitField();
3537
3538 if (bitfield_bit_size_ptr)
3539 {
3540 *bitfield_bit_size_ptr = 0;
3541
3542 if (is_bitfield && ast)
3543 {
3544 Expr *bitfield_bit_size_expr = field->getBitWidth();
3545 llvm::APSInt bitfield_apsint;
3546 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
3547 {
3548 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
3549 }
3550 }
3551 }
3552 if (is_bitfield_ptr)
3553 *is_bitfield_ptr = is_bitfield;
3554
Greg Claytonbf2331c2011-09-09 23:04:00 +00003555 return field->getType().getAsOpaquePtr();
3556 }
3557 }
3558 }
3559 break;
3560
3561 case clang::Type::ObjCObject:
3562 case clang::Type::ObjCInterface:
3563 if (GetCompleteQualType (ast, qual_type))
3564 {
3565 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3566 assert (objc_class_type);
3567 if (objc_class_type)
3568 {
3569 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3570
3571 if (class_interface_decl)
3572 {
3573 if (idx < (class_interface_decl->ivar_size()))
3574 {
3575 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
3576 uint32_t ivar_idx = 0;
3577
3578 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
3579 {
3580 if (ivar_idx == idx)
3581 {
3582 const ObjCIvarDecl* ivar_decl = *ivar_pos;
3583
3584 QualType ivar_qual_type(ivar_decl->getType());
3585
3586 name.assign(ivar_decl->getNameAsString());
3587
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003588 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003589 {
3590 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003591 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003592 }
3593
Greg Clayton1811b4f2012-07-31 23:39:10 +00003594 const bool is_bitfield = ivar_pos->isBitField();
3595
3596 if (bitfield_bit_size_ptr)
3597 {
3598 *bitfield_bit_size_ptr = 0;
3599
3600 if (is_bitfield && ast)
3601 {
3602 Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
3603 llvm::APSInt bitfield_apsint;
3604 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
3605 {
3606 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
3607 }
3608 }
3609 }
3610 if (is_bitfield_ptr)
3611 *is_bitfield_ptr = is_bitfield;
3612
Greg Claytonbf2331c2011-09-09 23:04:00 +00003613 return ivar_qual_type.getAsOpaquePtr();
3614 }
3615 }
3616 }
3617 }
3618 }
3619 }
3620 break;
3621
3622
3623 case clang::Type::Typedef:
3624 return ClangASTContext::GetFieldAtIndex (ast,
3625 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3626 idx,
3627 name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003628 bit_offset_ptr,
3629 bitfield_bit_size_ptr,
3630 is_bitfield_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003631
3632 case clang::Type::Elaborated:
3633 return ClangASTContext::GetFieldAtIndex (ast,
3634 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3635 idx,
3636 name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003637 bit_offset_ptr,
3638 bitfield_bit_size_ptr,
3639 is_bitfield_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003640
3641 default:
3642 break;
3643 }
3644 return NULL;
3645}
3646
3647
Greg Clayton54979cd2010-12-15 05:08:08 +00003648// If a pointer to a pointee type (the clang_type arg) says that it has no
3649// children, then we either need to trust it, or override it and return a
3650// different result. For example, an "int *" has one child that is an integer,
3651// but a function pointer doesn't have any children. Likewise if a Record type
3652// claims it has no children, then there really is nothing to show.
3653uint32_t
3654ClangASTContext::GetNumPointeeChildren (clang_type_t clang_type)
3655{
3656 if (clang_type == NULL)
3657 return 0;
3658
3659 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3660 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3661 switch (type_class)
3662 {
Greg Clayton97a43712011-01-08 22:26:47 +00003663 case clang::Type::Builtin:
3664 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3665 {
Greg Clayton7260f622011-04-18 08:33:37 +00003666 case clang::BuiltinType::UnknownAny:
Greg Clayton97a43712011-01-08 22:26:47 +00003667 case clang::BuiltinType::Void:
3668 case clang::BuiltinType::NullPtr:
3669 return 0;
3670 case clang::BuiltinType::Bool:
3671 case clang::BuiltinType::Char_U:
3672 case clang::BuiltinType::UChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003673 case clang::BuiltinType::WChar_U:
Greg Clayton97a43712011-01-08 22:26:47 +00003674 case clang::BuiltinType::Char16:
3675 case clang::BuiltinType::Char32:
3676 case clang::BuiltinType::UShort:
3677 case clang::BuiltinType::UInt:
3678 case clang::BuiltinType::ULong:
3679 case clang::BuiltinType::ULongLong:
3680 case clang::BuiltinType::UInt128:
3681 case clang::BuiltinType::Char_S:
3682 case clang::BuiltinType::SChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003683 case clang::BuiltinType::WChar_S:
Greg Clayton97a43712011-01-08 22:26:47 +00003684 case clang::BuiltinType::Short:
3685 case clang::BuiltinType::Int:
3686 case clang::BuiltinType::Long:
3687 case clang::BuiltinType::LongLong:
3688 case clang::BuiltinType::Int128:
3689 case clang::BuiltinType::Float:
3690 case clang::BuiltinType::Double:
3691 case clang::BuiltinType::LongDouble:
3692 case clang::BuiltinType::Dependent:
3693 case clang::BuiltinType::Overload:
Greg Clayton97a43712011-01-08 22:26:47 +00003694 case clang::BuiltinType::ObjCId:
3695 case clang::BuiltinType::ObjCClass:
3696 case clang::BuiltinType::ObjCSel:
Sean Callanand12cf8bb2011-05-15 22:34:38 +00003697 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00003698 case clang::BuiltinType::Half:
3699 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00003700 case clang::BuiltinType::PseudoObject:
Greg Clayton97a43712011-01-08 22:26:47 +00003701 return 1;
3702 }
3703 break;
3704
Greg Clayton49462ea2011-01-15 02:52:14 +00003705 case clang::Type::Complex: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003706 case clang::Type::Pointer: return 1;
3707 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
3708 case clang::Type::LValueReference: return 1;
3709 case clang::Type::RValueReference: return 1;
3710 case clang::Type::MemberPointer: return 0;
3711 case clang::Type::ConstantArray: return 0;
3712 case clang::Type::IncompleteArray: return 0;
3713 case clang::Type::VariableArray: return 0;
3714 case clang::Type::DependentSizedArray: return 0;
3715 case clang::Type::DependentSizedExtVector: return 0;
3716 case clang::Type::Vector: return 0;
3717 case clang::Type::ExtVector: return 0;
3718 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
3719 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
3720 case clang::Type::UnresolvedUsing: return 0;
3721 case clang::Type::Paren: return 0;
3722 case clang::Type::Typedef: return ClangASTContext::GetNumPointeeChildren (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00003723 case clang::Type::Elaborated: return ClangASTContext::GetNumPointeeChildren (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003724 case clang::Type::TypeOfExpr: return 0;
3725 case clang::Type::TypeOf: return 0;
3726 case clang::Type::Decltype: return 0;
3727 case clang::Type::Record: return 0;
3728 case clang::Type::Enum: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003729 case clang::Type::TemplateTypeParm: return 1;
3730 case clang::Type::SubstTemplateTypeParm: return 1;
3731 case clang::Type::TemplateSpecialization: return 1;
3732 case clang::Type::InjectedClassName: return 0;
3733 case clang::Type::DependentName: return 1;
3734 case clang::Type::DependentTemplateSpecialization: return 1;
3735 case clang::Type::ObjCObject: return 0;
3736 case clang::Type::ObjCInterface: return 0;
3737 case clang::Type::ObjCObjectPointer: return 1;
3738 default:
3739 break;
3740 }
3741 return 0;
3742}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003743
Greg Clayton1be10fc2010-09-29 01:12:09 +00003744clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003745ClangASTContext::GetChildClangTypeAtIndex
3746(
Jim Inghamd555bac2011-06-24 22:03:24 +00003747 ExecutionContext *exe_ctx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003748 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003749 clang_type_t parent_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003750 uint32_t idx,
3751 bool transparent_pointers,
3752 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003753 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003754 std::string& child_name,
3755 uint32_t &child_byte_size,
3756 int32_t &child_byte_offset,
3757 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003758 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003759 bool &child_is_base_class,
3760 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003761)
3762{
3763 if (parent_clang_type)
3764
Jim Inghamd555bac2011-06-24 22:03:24 +00003765 return GetChildClangTypeAtIndex (exe_ctx,
3766 getASTContext(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003767 parent_name,
3768 parent_clang_type,
3769 idx,
3770 transparent_pointers,
3771 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003772 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003773 child_name,
3774 child_byte_size,
3775 child_byte_offset,
3776 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003777 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003778 child_is_base_class,
3779 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003780 return NULL;
3781}
3782
Greg Clayton1be10fc2010-09-29 01:12:09 +00003783clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003784ClangASTContext::GetChildClangTypeAtIndex
3785(
Jim Inghamd555bac2011-06-24 22:03:24 +00003786 ExecutionContext *exe_ctx,
Greg Clayton6beaaa62011-01-17 03:46:26 +00003787 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003788 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003789 clang_type_t parent_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003790 uint32_t idx,
3791 bool transparent_pointers,
3792 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003793 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003794 std::string& child_name,
3795 uint32_t &child_byte_size,
3796 int32_t &child_byte_offset,
3797 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003798 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003799 bool &child_is_base_class,
3800 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003801)
3802{
3803 if (parent_clang_type == NULL)
3804 return NULL;
3805
Greg Clayton6beaaa62011-01-17 03:46:26 +00003806 if (idx < ClangASTContext::GetNumChildren (ast, parent_clang_type, omit_empty_base_classes))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003807 {
3808 uint32_t bit_offset;
3809 child_bitfield_bit_size = 0;
3810 child_bitfield_bit_offset = 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003811 child_is_base_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003812 QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00003813 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
3814 switch (parent_type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003815 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003816 case clang::Type::Builtin:
3817 switch (cast<clang::BuiltinType>(parent_qual_type)->getKind())
3818 {
3819 case clang::BuiltinType::ObjCId:
3820 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00003821 child_name = "isa";
Greg Clayton6beaaa62011-01-17 03:46:26 +00003822 child_byte_size = ast->getTypeSize(ast->ObjCBuiltinClassTy) / CHAR_BIT;
3823 return ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003824
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003825 default:
3826 break;
3827 }
3828 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003829
Greg Claytone1a916a2010-07-21 22:12:05 +00003830 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00003831 if (GetCompleteQualType (ast, parent_qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003832 {
3833 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr());
3834 const RecordDecl *record_decl = record_type->getDecl();
3835 assert(record_decl);
Greg Clayton6beaaa62011-01-17 03:46:26 +00003836 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003837 uint32_t child_idx = 0;
3838
3839 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3840 if (cxx_record_decl)
3841 {
3842 // We might have base classes to print out first
3843 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3844 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3845 base_class != base_class_end;
3846 ++base_class)
3847 {
3848 const CXXRecordDecl *base_class_decl = NULL;
3849
3850 // Skip empty base classes
3851 if (omit_empty_base_classes)
3852 {
3853 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3854 if (RecordHasFields(base_class_decl) == false)
3855 continue;
3856 }
3857
3858 if (idx == child_idx)
3859 {
3860 if (base_class_decl == NULL)
3861 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3862
3863
3864 if (base_class->isVirtual())
Greg Clayton6ed95942011-01-22 07:12:45 +00003865 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003866 else
Greg Clayton6ed95942011-01-22 07:12:45 +00003867 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003868
3869 // Base classes should be a multiple of 8 bits in size
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003870 child_byte_offset = bit_offset/8;
Greg Claytone3055942011-06-30 02:28:26 +00003871
Greg Clayton84db9102012-03-26 23:03:23 +00003872 child_name = ClangASTType::GetTypeNameForQualType(ast, base_class->getType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003873
Greg Clayton6beaaa62011-01-17 03:46:26 +00003874 uint64_t clang_type_info_bit_size = ast->getTypeSize(base_class->getType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003875
Jim Inghamf46b3382011-04-15 23:42:06 +00003876 // Base classes bit sizes should be a multiple of 8 bits in size
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003877 assert (clang_type_info_bit_size % 8 == 0);
3878 child_byte_size = clang_type_info_bit_size / 8;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003879 child_is_base_class = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003880 return base_class->getType().getAsOpaquePtr();
3881 }
3882 // We don't increment the child index in the for loop since we might
3883 // be skipping empty base classes
3884 ++child_idx;
3885 }
3886 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003887 // Make sure index is in range...
3888 uint32_t field_idx = 0;
3889 RecordDecl::field_iterator field, field_end;
3890 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
3891 {
3892 if (idx == child_idx)
3893 {
3894 // Print the member type if requested
3895 // Print the member name and equal sign
3896 child_name.assign(field->getNameAsString().c_str());
3897
3898 // Figure out the type byte size (field_type_info.first) and
3899 // alignment (field_type_info.second) from the AST context.
Greg Clayton6beaaa62011-01-17 03:46:26 +00003900 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(field->getType());
Greg Claytonc982c762010-07-09 20:39:50 +00003901 assert(field_idx < record_layout.getFieldCount());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003902
3903 child_byte_size = field_type_info.first / 8;
3904
3905 // Figure out the field offset within the current struct/union/class type
3906 bit_offset = record_layout.getFieldOffset (field_idx);
3907 child_byte_offset = bit_offset / 8;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003908 if (ClangASTContext::FieldIsBitfield (ast, *field, child_bitfield_bit_size))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003909 child_bitfield_bit_offset = bit_offset % 8;
3910
3911 return field->getType().getAsOpaquePtr();
3912 }
3913 }
3914 }
3915 break;
3916
Greg Clayton9e409562010-07-28 02:04:09 +00003917 case clang::Type::ObjCObject:
3918 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00003919 if (GetCompleteQualType (ast, parent_qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00003920 {
Sean Callanan78e37602011-01-27 04:42:51 +00003921 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003922 assert (objc_class_type);
3923 if (objc_class_type)
3924 {
3925 uint32_t child_idx = 0;
3926 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3927
3928 if (class_interface_decl)
3929 {
3930
Greg Clayton6beaaa62011-01-17 03:46:26 +00003931 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
Greg Clayton9e409562010-07-28 02:04:09 +00003932 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3933 if (superclass_interface_decl)
3934 {
3935 if (omit_empty_base_classes)
3936 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00003937 if (ClangASTContext::GetNumChildren(ast, ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0)
Greg Clayton9e409562010-07-28 02:04:09 +00003938 {
3939 if (idx == 0)
3940 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00003941 QualType ivar_qual_type(ast->getObjCInterfaceType(superclass_interface_decl));
Greg Clayton9e409562010-07-28 02:04:09 +00003942
3943
3944 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
3945
Greg Clayton6beaaa62011-01-17 03:46:26 +00003946 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003947
3948 child_byte_size = ivar_type_info.first / 8;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003949 child_byte_offset = 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003950 child_is_base_class = true;
Greg Clayton9e409562010-07-28 02:04:09 +00003951
3952 return ivar_qual_type.getAsOpaquePtr();
3953 }
3954
3955 ++child_idx;
3956 }
3957 }
3958 else
3959 ++child_idx;
3960 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003961
3962 const uint32_t superclass_idx = child_idx;
Greg Clayton9e409562010-07-28 02:04:09 +00003963
3964 if (idx < (child_idx + class_interface_decl->ivar_size()))
3965 {
3966 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
3967
3968 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
3969 {
3970 if (child_idx == idx)
3971 {
Jim Inghamf80bc3f2011-12-08 02:53:10 +00003972 ObjCIvarDecl* ivar_decl = *ivar_pos;
Greg Clayton9e409562010-07-28 02:04:09 +00003973
3974 QualType ivar_qual_type(ivar_decl->getType());
3975
3976 child_name.assign(ivar_decl->getNameAsString().c_str());
3977
Greg Clayton6beaaa62011-01-17 03:46:26 +00003978 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003979
3980 child_byte_size = ivar_type_info.first / 8;
3981
3982 // Figure out the field offset within the current struct/union/class type
Jim Inghamd555bac2011-06-24 22:03:24 +00003983 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
3984 // that doesn't account for the space taken up by unbacked properties, or from
3985 // the changing size of base classes that are newer than this class.
3986 // So if we have a process around that we can ask about this object, do so.
3987 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
Greg Claytonc14ee322011-09-22 04:58:26 +00003988 Process *process = NULL;
3989 if (exe_ctx)
3990 process = exe_ctx->GetProcessPtr();
3991 if (process)
Jim Inghamd555bac2011-06-24 22:03:24 +00003992 {
Greg Claytonc14ee322011-09-22 04:58:26 +00003993 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
Jim Inghamd555bac2011-06-24 22:03:24 +00003994 if (objc_runtime != NULL)
3995 {
Enrico Granata6f3533f2011-07-29 19:53:35 +00003996 ClangASTType parent_ast_type (ast, parent_qual_type.getAsOpaquePtr());
Jim Inghamd555bac2011-06-24 22:03:24 +00003997 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
3998 }
3999 }
4000
Jim Inghamf80bc3f2011-12-08 02:53:10 +00004001 // Setting this to UINT32_MAX to make sure we don't compute it twice...
4002 bit_offset = UINT32_MAX;
4003
Jim Inghamd555bac2011-06-24 22:03:24 +00004004 if (child_byte_offset == LLDB_INVALID_IVAR_OFFSET)
4005 {
4006 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
4007 child_byte_offset = bit_offset / 8;
4008 }
Jim Inghamf80bc3f2011-12-08 02:53:10 +00004009
4010 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
4011 // of a bitfield within its containing object. So regardless of where we get the byte
4012 // offset from, we still need to get the bit offset for bitfields from the layout.
4013
4014 if (ClangASTContext::FieldIsBitfield (ast, ivar_decl, child_bitfield_bit_size))
4015 {
4016 if (bit_offset == UINT32_MAX)
4017 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
4018
4019 child_bitfield_bit_offset = bit_offset % 8;
4020 }
Greg Clayton9e409562010-07-28 02:04:09 +00004021 return ivar_qual_type.getAsOpaquePtr();
4022 }
4023 ++child_idx;
4024 }
4025 }
4026 }
4027 }
4028 }
4029 break;
4030
4031 case clang::Type::ObjCObjectPointer:
4032 {
Sean Callanan78e37602011-01-27 04:42:51 +00004033 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004034 QualType pointee_type = pointer_type->getPointeeType();
4035
4036 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4037 {
Greg Claytone221f822011-01-21 01:59:00 +00004038 child_is_deref_of_parent = false;
4039 bool tmp_child_is_deref_of_parent = false;
Jim Inghamd555bac2011-06-24 22:03:24 +00004040 return GetChildClangTypeAtIndex (exe_ctx,
4041 ast,
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004042 parent_name,
4043 pointer_type->getPointeeType().getAsOpaquePtr(),
4044 idx,
4045 transparent_pointers,
4046 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004047 ignore_array_bounds,
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004048 child_name,
4049 child_byte_size,
4050 child_byte_offset,
4051 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00004052 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004053 child_is_base_class,
4054 tmp_child_is_deref_of_parent);
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004055 }
4056 else
4057 {
Greg Claytone221f822011-01-21 01:59:00 +00004058 child_is_deref_of_parent = true;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004059 if (parent_name)
4060 {
4061 child_name.assign(1, '*');
4062 child_name += parent_name;
4063 }
4064
4065 // We have a pointer to an simple type
Sean Callanan5b26f272012-02-04 08:49:35 +00004066 if (idx == 0 && GetCompleteQualType(ast, pointee_type))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004067 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004068 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004069 assert(clang_type_info.first % 8 == 0);
4070 child_byte_size = clang_type_info.first / 8;
4071 child_byte_offset = 0;
4072 return pointee_type.getAsOpaquePtr();
4073 }
4074 }
Greg Clayton9e409562010-07-28 02:04:09 +00004075 }
4076 break;
4077
Greg Claytone1a916a2010-07-21 22:12:05 +00004078 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004079 {
4080 const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4081 const uint64_t element_count = array->getSize().getLimitedValue();
4082
Greg Claytondaf515f2011-07-09 20:12:33 +00004083 if (ignore_array_bounds || idx < element_count)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004084 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004085 if (GetCompleteQualType (ast, array->getElementType()))
4086 {
4087 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004088
Greg Clayton6beaaa62011-01-17 03:46:26 +00004089 char element_name[64];
4090 ::snprintf (element_name, sizeof (element_name), "[%u]", idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004091
Greg Clayton6beaaa62011-01-17 03:46:26 +00004092 child_name.assign(element_name);
4093 assert(field_type_info.first % 8 == 0);
4094 child_byte_size = field_type_info.first / 8;
Greg Claytondaf515f2011-07-09 20:12:33 +00004095 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
Greg Clayton6beaaa62011-01-17 03:46:26 +00004096 return array->getElementType().getAsOpaquePtr();
4097 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004098 }
4099 }
4100 break;
4101
Greg Claytone1a916a2010-07-21 22:12:05 +00004102 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004103 {
Sean Callanan78e37602011-01-27 04:42:51 +00004104 const PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004105 QualType pointee_type = pointer_type->getPointeeType();
Greg Clayton97a43712011-01-08 22:26:47 +00004106
4107 // Don't dereference "void *" pointers
4108 if (pointee_type->isVoidType())
4109 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004110
4111 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4112 {
Greg Claytone221f822011-01-21 01:59:00 +00004113 child_is_deref_of_parent = false;
4114 bool tmp_child_is_deref_of_parent = false;
Jim Inghamd555bac2011-06-24 22:03:24 +00004115 return GetChildClangTypeAtIndex (exe_ctx,
4116 ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004117 parent_name,
4118 pointer_type->getPointeeType().getAsOpaquePtr(),
4119 idx,
4120 transparent_pointers,
4121 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004122 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004123 child_name,
4124 child_byte_size,
4125 child_byte_offset,
4126 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00004127 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004128 child_is_base_class,
4129 tmp_child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004130 }
4131 else
4132 {
Greg Claytone221f822011-01-21 01:59:00 +00004133 child_is_deref_of_parent = true;
4134
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004135 if (parent_name)
4136 {
4137 child_name.assign(1, '*');
4138 child_name += parent_name;
4139 }
4140
4141 // We have a pointer to an simple type
4142 if (idx == 0)
4143 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004144 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004145 assert(clang_type_info.first % 8 == 0);
4146 child_byte_size = clang_type_info.first / 8;
4147 child_byte_offset = 0;
4148 return pointee_type.getAsOpaquePtr();
4149 }
4150 }
4151 }
4152 break;
4153
Greg Clayton73b472d2010-10-27 03:32:59 +00004154 case clang::Type::LValueReference:
4155 case clang::Type::RValueReference:
4156 {
Sean Callanan78e37602011-01-27 04:42:51 +00004157 const ReferenceType *reference_type = cast<ReferenceType>(parent_qual_type.getTypePtr());
Greg Clayton73b472d2010-10-27 03:32:59 +00004158 QualType pointee_type(reference_type->getPointeeType());
4159 clang_type_t pointee_clang_type = pointee_type.getAsOpaquePtr();
4160 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_clang_type))
4161 {
Greg Claytone221f822011-01-21 01:59:00 +00004162 child_is_deref_of_parent = false;
4163 bool tmp_child_is_deref_of_parent = false;
Jim Inghamd555bac2011-06-24 22:03:24 +00004164 return GetChildClangTypeAtIndex (exe_ctx,
4165 ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00004166 parent_name,
4167 pointee_clang_type,
4168 idx,
4169 transparent_pointers,
4170 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004171 ignore_array_bounds,
Greg Clayton73b472d2010-10-27 03:32:59 +00004172 child_name,
4173 child_byte_size,
4174 child_byte_offset,
4175 child_bitfield_bit_size,
4176 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004177 child_is_base_class,
4178 tmp_child_is_deref_of_parent);
Greg Clayton73b472d2010-10-27 03:32:59 +00004179 }
4180 else
4181 {
4182 if (parent_name)
4183 {
4184 child_name.assign(1, '&');
4185 child_name += parent_name;
4186 }
4187
4188 // We have a pointer to an simple type
4189 if (idx == 0)
4190 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004191 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Greg Clayton73b472d2010-10-27 03:32:59 +00004192 assert(clang_type_info.first % 8 == 0);
4193 child_byte_size = clang_type_info.first / 8;
4194 child_byte_offset = 0;
4195 return pointee_type.getAsOpaquePtr();
4196 }
4197 }
4198 }
4199 break;
4200
Greg Claytone1a916a2010-07-21 22:12:05 +00004201 case clang::Type::Typedef:
Jim Inghamd555bac2011-06-24 22:03:24 +00004202 return GetChildClangTypeAtIndex (exe_ctx,
4203 ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004204 parent_name,
Sean Callanan48114472010-12-13 01:26:27 +00004205 cast<TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004206 idx,
4207 transparent_pointers,
4208 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004209 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004210 child_name,
4211 child_byte_size,
4212 child_byte_offset,
4213 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00004214 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004215 child_is_base_class,
4216 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004217 break;
Sean Callanan912855f2011-08-11 23:56:13 +00004218
4219 case clang::Type::Elaborated:
4220 return GetChildClangTypeAtIndex (exe_ctx,
4221 ast,
4222 parent_name,
4223 cast<ElaboratedType>(parent_qual_type)->getNamedType().getAsOpaquePtr(),
4224 idx,
4225 transparent_pointers,
4226 omit_empty_base_classes,
4227 ignore_array_bounds,
4228 child_name,
4229 child_byte_size,
4230 child_byte_offset,
4231 child_bitfield_bit_size,
4232 child_bitfield_bit_offset,
4233 child_is_base_class,
4234 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004235
4236 default:
4237 break;
4238 }
4239 }
Greg Clayton19503a22010-07-23 15:37:46 +00004240 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004241}
4242
4243static inline bool
4244BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
4245{
Greg Clayton6beaaa62011-01-17 03:46:26 +00004246 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004247}
4248
4249static uint32_t
4250GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
4251{
4252 uint32_t num_bases = 0;
4253 if (cxx_record_decl)
4254 {
4255 if (omit_empty_base_classes)
4256 {
4257 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4258 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4259 base_class != base_class_end;
4260 ++base_class)
4261 {
4262 // Skip empty base classes
4263 if (omit_empty_base_classes)
4264 {
4265 if (BaseSpecifierIsEmpty (base_class))
4266 continue;
4267 }
4268 ++num_bases;
4269 }
4270 }
4271 else
4272 num_bases = cxx_record_decl->getNumBases();
4273 }
4274 return num_bases;
4275}
4276
4277
4278static uint32_t
4279GetIndexForRecordBase
4280(
4281 const RecordDecl *record_decl,
4282 const CXXBaseSpecifier *base_spec,
4283 bool omit_empty_base_classes
4284)
4285{
4286 uint32_t child_idx = 0;
4287
4288 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4289
4290// const char *super_name = record_decl->getNameAsCString();
4291// const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString();
4292// printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
4293//
4294 if (cxx_record_decl)
4295 {
4296 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4297 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4298 base_class != base_class_end;
4299 ++base_class)
4300 {
4301 if (omit_empty_base_classes)
4302 {
4303 if (BaseSpecifierIsEmpty (base_class))
4304 continue;
4305 }
4306
4307// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
4308// child_idx,
4309// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4310//
4311//
4312 if (base_class == base_spec)
4313 return child_idx;
4314 ++child_idx;
4315 }
4316 }
4317
4318 return UINT32_MAX;
4319}
4320
4321
4322static uint32_t
4323GetIndexForRecordChild
4324(
4325 const RecordDecl *record_decl,
4326 NamedDecl *canonical_decl,
4327 bool omit_empty_base_classes
4328)
4329{
4330 uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes);
4331
4332// const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4333//
4334//// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString());
4335// if (cxx_record_decl)
4336// {
4337// CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4338// for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4339// base_class != base_class_end;
4340// ++base_class)
4341// {
4342// if (omit_empty_base_classes)
4343// {
4344// if (BaseSpecifierIsEmpty (base_class))
4345// continue;
4346// }
4347//
4348//// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
4349//// record_decl->getNameAsCString(),
4350//// canonical_decl->getNameAsCString(),
4351//// child_idx,
4352//// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4353//
4354//
4355// CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4356// if (curr_base_class_decl == canonical_decl)
4357// {
4358// return child_idx;
4359// }
4360// ++child_idx;
4361// }
4362// }
4363//
4364// const uint32_t num_bases = child_idx;
4365 RecordDecl::field_iterator field, field_end;
4366 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4367 field != field_end;
4368 ++field, ++child_idx)
4369 {
4370// printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n",
4371// record_decl->getNameAsCString(),
4372// canonical_decl->getNameAsCString(),
4373// child_idx - num_bases,
4374// field->getNameAsCString());
4375
4376 if (field->getCanonicalDecl() == canonical_decl)
4377 return child_idx;
4378 }
4379
4380 return UINT32_MAX;
4381}
4382
4383// Look for a child member (doesn't include base classes, but it does include
4384// their members) in the type hierarchy. Returns an index path into "clang_type"
4385// on how to reach the appropriate member.
4386//
4387// class A
4388// {
4389// public:
4390// int m_a;
4391// int m_b;
4392// };
4393//
4394// class B
4395// {
4396// };
4397//
4398// class C :
4399// public B,
4400// public A
4401// {
4402// };
4403//
4404// If we have a clang type that describes "class C", and we wanted to looked
4405// "m_b" in it:
4406//
4407// With omit_empty_base_classes == false we would get an integer array back with:
4408// { 1, 1 }
4409// The first index 1 is the child index for "class A" within class C
4410// The second index 1 is the child index for "m_b" within class A
4411//
4412// With omit_empty_base_classes == true we would get an integer array back with:
4413// { 0, 1 }
4414// 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)
4415// The second index 1 is the child index for "m_b" within class A
4416
4417size_t
4418ClangASTContext::GetIndexOfChildMemberWithName
4419(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004420 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004421 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004422 const char *name,
4423 bool omit_empty_base_classes,
4424 std::vector<uint32_t>& child_indexes
4425)
4426{
4427 if (clang_type && name && name[0])
4428 {
4429 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00004430 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4431 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004432 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004433 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004434 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004435 {
4436 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4437 const RecordDecl *record_decl = record_type->getDecl();
4438
4439 assert(record_decl);
4440 uint32_t child_idx = 0;
4441
4442 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4443
4444 // Try and find a field that matches NAME
4445 RecordDecl::field_iterator field, field_end;
4446 StringRef name_sref(name);
4447 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4448 field != field_end;
4449 ++field, ++child_idx)
4450 {
4451 if (field->getName().equals (name_sref))
4452 {
4453 // We have to add on the number of base classes to this index!
4454 child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
4455 return child_indexes.size();
4456 }
4457 }
4458
4459 if (cxx_record_decl)
4460 {
4461 const RecordDecl *parent_record_decl = cxx_record_decl;
4462
4463 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
4464
4465 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
4466 // Didn't find things easily, lets let clang do its thang...
Sean Callanancc427fa2011-07-30 02:42:06 +00004467 IdentifierInfo & ident_ref = ast->Idents.get(name_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004468 DeclarationName decl_name(&ident_ref);
4469
4470 CXXBasePaths paths;
4471 if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember,
4472 decl_name.getAsOpaquePtr(),
4473 paths))
4474 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004475 CXXBasePaths::const_paths_iterator path, path_end = paths.end();
4476 for (path = paths.begin(); path != path_end; ++path)
4477 {
4478 const size_t num_path_elements = path->size();
4479 for (size_t e=0; e<num_path_elements; ++e)
4480 {
4481 CXXBasePathElement elem = (*path)[e];
4482
4483 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
4484 if (child_idx == UINT32_MAX)
4485 {
4486 child_indexes.clear();
4487 return 0;
4488 }
4489 else
4490 {
4491 child_indexes.push_back (child_idx);
4492 parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl());
4493 }
4494 }
4495 DeclContext::lookup_iterator named_decl_pos;
4496 for (named_decl_pos = path->Decls.first;
4497 named_decl_pos != path->Decls.second && parent_record_decl;
4498 ++named_decl_pos)
4499 {
4500 //printf ("path[%zu] = %s\n", child_indexes.size(), (*named_decl_pos)->getNameAsCString());
4501
4502 child_idx = GetIndexForRecordChild (parent_record_decl, *named_decl_pos, omit_empty_base_classes);
4503 if (child_idx == UINT32_MAX)
4504 {
4505 child_indexes.clear();
4506 return 0;
4507 }
4508 else
4509 {
4510 child_indexes.push_back (child_idx);
4511 }
4512 }
4513 }
4514 return child_indexes.size();
4515 }
4516 }
4517
4518 }
4519 break;
4520
Greg Clayton9e409562010-07-28 02:04:09 +00004521 case clang::Type::ObjCObject:
4522 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004523 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004524 {
4525 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004526 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004527 assert (objc_class_type);
4528 if (objc_class_type)
4529 {
4530 uint32_t child_idx = 0;
4531 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4532
4533 if (class_interface_decl)
4534 {
4535 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4536 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4537
Greg Clayton6ba78152010-09-18 02:11:07 +00004538 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00004539 {
4540 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4541
4542 if (ivar_decl->getName().equals (name_sref))
4543 {
4544 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4545 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4546 ++child_idx;
4547
4548 child_indexes.push_back (child_idx);
4549 return child_indexes.size();
4550 }
4551 }
4552
4553 if (superclass_interface_decl)
4554 {
4555 // The super class index is always zero for ObjC classes,
4556 // so we push it onto the child indexes in case we find
4557 // an ivar in our superclass...
4558 child_indexes.push_back (0);
4559
Greg Clayton6beaaa62011-01-17 03:46:26 +00004560 if (GetIndexOfChildMemberWithName (ast,
4561 ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00004562 name,
4563 omit_empty_base_classes,
4564 child_indexes))
4565 {
4566 // We did find an ivar in a superclass so just
4567 // return the results!
4568 return child_indexes.size();
4569 }
4570
4571 // We didn't find an ivar matching "name" in our
4572 // superclass, pop the superclass zero index that
4573 // we pushed on above.
4574 child_indexes.pop_back();
4575 }
4576 }
4577 }
4578 }
4579 break;
4580
4581 case clang::Type::ObjCObjectPointer:
4582 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004583 return GetIndexOfChildMemberWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004584 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4585 name,
4586 omit_empty_base_classes,
4587 child_indexes);
4588 }
4589 break;
4590
4591
Greg Claytone1a916a2010-07-21 22:12:05 +00004592 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004593 {
4594// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4595// const uint64_t element_count = array->getSize().getLimitedValue();
4596//
4597// if (idx < element_count)
4598// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004599// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004600//
4601// char element_name[32];
4602// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4603//
4604// child_name.assign(element_name);
4605// assert(field_type_info.first % 8 == 0);
4606// child_byte_size = field_type_info.first / 8;
4607// child_byte_offset = idx * child_byte_size;
4608// return array->getElementType().getAsOpaquePtr();
4609// }
4610 }
4611 break;
4612
Greg Claytone1a916a2010-07-21 22:12:05 +00004613// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004614// {
4615// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4616// QualType pointee_type = mem_ptr_type->getPointeeType();
4617//
4618// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4619// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004620// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004621// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4622// name);
4623// }
4624// }
4625// break;
4626//
Greg Claytone1a916a2010-07-21 22:12:05 +00004627 case clang::Type::LValueReference:
4628 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004629 {
Sean Callanan78e37602011-01-27 04:42:51 +00004630 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004631 QualType pointee_type = reference_type->getPointeeType();
4632
4633 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4634 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004635 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004636 reference_type->getPointeeType().getAsOpaquePtr(),
4637 name,
4638 omit_empty_base_classes,
4639 child_indexes);
4640 }
4641 }
4642 break;
4643
Greg Claytone1a916a2010-07-21 22:12:05 +00004644 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004645 {
Sean Callanan78e37602011-01-27 04:42:51 +00004646 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004647 QualType pointee_type = pointer_type->getPointeeType();
4648
4649 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4650 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004651 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004652 pointer_type->getPointeeType().getAsOpaquePtr(),
4653 name,
4654 omit_empty_base_classes,
4655 child_indexes);
4656 }
4657 else
4658 {
4659// if (parent_name)
4660// {
4661// child_name.assign(1, '*');
4662// child_name += parent_name;
4663// }
4664//
4665// // We have a pointer to an simple type
4666// if (idx == 0)
4667// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004668// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004669// assert(clang_type_info.first % 8 == 0);
4670// child_byte_size = clang_type_info.first / 8;
4671// child_byte_offset = 0;
4672// return pointee_type.getAsOpaquePtr();
4673// }
4674 }
4675 }
4676 break;
4677
Greg Claytone1a916a2010-07-21 22:12:05 +00004678 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00004679 return GetIndexOfChildMemberWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00004680 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004681 name,
4682 omit_empty_base_classes,
4683 child_indexes);
4684
4685 default:
4686 break;
4687 }
4688 }
4689 return 0;
4690}
4691
4692
4693// Get the index of the child of "clang_type" whose name matches. This function
4694// doesn't descend into the children, but only looks one level deep and name
4695// matches can include base class names.
4696
4697uint32_t
4698ClangASTContext::GetIndexOfChildWithName
4699(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004700 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004701 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004702 const char *name,
4703 bool omit_empty_base_classes
4704)
4705{
4706 if (clang_type && name && name[0])
4707 {
4708 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00004709
Greg Clayton737b9322010-09-13 03:32:57 +00004710 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Clayton9e409562010-07-28 02:04:09 +00004711
Greg Clayton737b9322010-09-13 03:32:57 +00004712 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004713 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004714 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004715 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004716 {
4717 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4718 const RecordDecl *record_decl = record_type->getDecl();
4719
4720 assert(record_decl);
4721 uint32_t child_idx = 0;
4722
4723 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4724
4725 if (cxx_record_decl)
4726 {
4727 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4728 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4729 base_class != base_class_end;
4730 ++base_class)
4731 {
4732 // Skip empty base classes
4733 CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4734 if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false)
4735 continue;
4736
Greg Clayton84db9102012-03-26 23:03:23 +00004737 std::string base_class_type_name (ClangASTType::GetTypeNameForQualType(ast, base_class->getType()));
Greg Claytone3055942011-06-30 02:28:26 +00004738 if (base_class_type_name.compare (name) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004739 return child_idx;
4740 ++child_idx;
4741 }
4742 }
4743
4744 // Try and find a field that matches NAME
4745 RecordDecl::field_iterator field, field_end;
4746 StringRef name_sref(name);
4747 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4748 field != field_end;
4749 ++field, ++child_idx)
4750 {
4751 if (field->getName().equals (name_sref))
4752 return child_idx;
4753 }
4754
4755 }
4756 break;
4757
Greg Clayton9e409562010-07-28 02:04:09 +00004758 case clang::Type::ObjCObject:
4759 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004760 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004761 {
4762 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004763 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004764 assert (objc_class_type);
4765 if (objc_class_type)
4766 {
4767 uint32_t child_idx = 0;
4768 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4769
4770 if (class_interface_decl)
4771 {
4772 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4773 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4774
4775 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
4776 {
4777 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4778
4779 if (ivar_decl->getName().equals (name_sref))
4780 {
4781 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4782 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4783 ++child_idx;
4784
4785 return child_idx;
4786 }
4787 }
4788
4789 if (superclass_interface_decl)
4790 {
4791 if (superclass_interface_decl->getName().equals (name_sref))
4792 return 0;
4793 }
4794 }
4795 }
4796 }
4797 break;
4798
4799 case clang::Type::ObjCObjectPointer:
4800 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004801 return GetIndexOfChildWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004802 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4803 name,
4804 omit_empty_base_classes);
4805 }
4806 break;
4807
Greg Claytone1a916a2010-07-21 22:12:05 +00004808 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004809 {
4810// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4811// const uint64_t element_count = array->getSize().getLimitedValue();
4812//
4813// if (idx < element_count)
4814// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004815// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004816//
4817// char element_name[32];
4818// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4819//
4820// child_name.assign(element_name);
4821// assert(field_type_info.first % 8 == 0);
4822// child_byte_size = field_type_info.first / 8;
4823// child_byte_offset = idx * child_byte_size;
4824// return array->getElementType().getAsOpaquePtr();
4825// }
4826 }
4827 break;
4828
Greg Claytone1a916a2010-07-21 22:12:05 +00004829// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004830// {
4831// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4832// QualType pointee_type = mem_ptr_type->getPointeeType();
4833//
4834// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4835// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004836// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004837// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4838// name);
4839// }
4840// }
4841// break;
4842//
Greg Claytone1a916a2010-07-21 22:12:05 +00004843 case clang::Type::LValueReference:
4844 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004845 {
Sean Callanan78e37602011-01-27 04:42:51 +00004846 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004847 QualType pointee_type = reference_type->getPointeeType();
4848
4849 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4850 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004851 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004852 reference_type->getPointeeType().getAsOpaquePtr(),
4853 name,
4854 omit_empty_base_classes);
4855 }
4856 }
4857 break;
4858
Greg Claytone1a916a2010-07-21 22:12:05 +00004859 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004860 {
Sean Callanan78e37602011-01-27 04:42:51 +00004861 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004862 QualType pointee_type = pointer_type->getPointeeType();
4863
4864 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4865 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004866 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004867 pointer_type->getPointeeType().getAsOpaquePtr(),
4868 name,
4869 omit_empty_base_classes);
4870 }
4871 else
4872 {
4873// if (parent_name)
4874// {
4875// child_name.assign(1, '*');
4876// child_name += parent_name;
4877// }
4878//
4879// // We have a pointer to an simple type
4880// if (idx == 0)
4881// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004882// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004883// assert(clang_type_info.first % 8 == 0);
4884// child_byte_size = clang_type_info.first / 8;
4885// child_byte_offset = 0;
4886// return pointee_type.getAsOpaquePtr();
4887// }
4888 }
4889 }
4890 break;
4891
Greg Claytone1a916a2010-07-21 22:12:05 +00004892 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00004893 return GetIndexOfChildWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00004894 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004895 name,
4896 omit_empty_base_classes);
4897
4898 default:
4899 break;
4900 }
4901 }
4902 return UINT32_MAX;
4903}
4904
4905#pragma mark TagType
4906
4907bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00004908ClangASTContext::SetTagTypeKind (clang_type_t tag_clang_type, int kind)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004909{
4910 if (tag_clang_type)
4911 {
4912 QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00004913 const clang::Type *clang_type = tag_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004914 if (clang_type)
4915 {
Sean Callanan78e37602011-01-27 04:42:51 +00004916 const TagType *tag_type = dyn_cast<TagType>(clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004917 if (tag_type)
4918 {
4919 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl());
4920 if (tag_decl)
4921 {
4922 tag_decl->setTagKind ((TagDecl::TagKind)kind);
4923 return true;
4924 }
4925 }
4926 }
4927 }
4928 return false;
4929}
4930
4931
4932#pragma mark DeclContext Functions
4933
4934DeclContext *
Greg Clayton1be10fc2010-09-29 01:12:09 +00004935ClangASTContext::GetDeclContextForType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004936{
4937 if (clang_type == NULL)
4938 return NULL;
4939
4940 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00004941 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4942 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004943 {
Sean Callanancc427fa2011-07-30 02:42:06 +00004944 case clang::Type::UnaryTransform: break;
Greg Clayton9e409562010-07-28 02:04:09 +00004945 case clang::Type::FunctionNoProto: break;
4946 case clang::Type::FunctionProto: break;
4947 case clang::Type::IncompleteArray: break;
4948 case clang::Type::VariableArray: break;
4949 case clang::Type::ConstantArray: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00004950 case clang::Type::DependentSizedArray: break;
Greg Clayton9e409562010-07-28 02:04:09 +00004951 case clang::Type::ExtVector: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00004952 case clang::Type::DependentSizedExtVector: break;
Greg Clayton9e409562010-07-28 02:04:09 +00004953 case clang::Type::Vector: break;
4954 case clang::Type::Builtin: break;
4955 case clang::Type::BlockPointer: break;
4956 case clang::Type::Pointer: break;
4957 case clang::Type::LValueReference: break;
4958 case clang::Type::RValueReference: break;
4959 case clang::Type::MemberPointer: break;
4960 case clang::Type::Complex: break;
4961 case clang::Type::ObjCObject: break;
4962 case clang::Type::ObjCInterface: return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface();
4963 case clang::Type::ObjCObjectPointer: return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr());
4964 case clang::Type::Record: return cast<RecordType>(qual_type)->getDecl();
4965 case clang::Type::Enum: return cast<EnumType>(qual_type)->getDecl();
Sean Callanan48114472010-12-13 01:26:27 +00004966 case clang::Type::Typedef: return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00004967 case clang::Type::Elaborated: return ClangASTContext::GetDeclContextForType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004968 case clang::Type::TypeOfExpr: break;
4969 case clang::Type::TypeOf: break;
4970 case clang::Type::Decltype: break;
4971 //case clang::Type::QualifiedName: break;
4972 case clang::Type::TemplateSpecialization: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00004973 case clang::Type::DependentTemplateSpecialization: break;
4974 case clang::Type::TemplateTypeParm: break;
4975 case clang::Type::SubstTemplateTypeParm: break;
4976 case clang::Type::SubstTemplateTypeParmPack:break;
4977 case clang::Type::PackExpansion: break;
4978 case clang::Type::UnresolvedUsing: break;
4979 case clang::Type::Paren: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00004980 case clang::Type::Attributed: break;
4981 case clang::Type::Auto: break;
4982 case clang::Type::InjectedClassName: break;
4983 case clang::Type::DependentName: break;
Greg Claytonea3e7d52011-10-08 00:49:15 +00004984 case clang::Type::Atomic: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004985 }
4986 // No DeclContext in this type...
4987 return NULL;
4988}
4989
4990#pragma mark Namespace Declarations
4991
4992NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00004993ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004994{
Greg Clayton030a2042011-10-14 21:34:45 +00004995 NamespaceDecl *namespace_decl = NULL;
Greg Clayton9d3d6882011-10-31 23:51:19 +00004996 ASTContext *ast = getASTContext();
4997 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
4998 if (decl_ctx == NULL)
4999 decl_ctx = translation_unit_decl;
5000
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005001 if (name)
5002 {
Greg Clayton030a2042011-10-14 21:34:45 +00005003 IdentifierInfo &identifier_info = ast->Idents.get(name);
5004 DeclarationName decl_name (&identifier_info);
5005 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
5006 for (clang::DeclContext::lookup_iterator pos = result.first, end = result.second; pos != end; ++pos)
5007 {
5008 namespace_decl = dyn_cast<clang::NamespaceDecl>(*pos);
5009 if (namespace_decl)
5010 return namespace_decl;
5011 }
5012
Sean Callanan5b26f272012-02-04 08:49:35 +00005013 namespace_decl = NamespaceDecl::Create(*ast,
5014 decl_ctx,
5015 false,
5016 SourceLocation(),
5017 SourceLocation(),
5018 &identifier_info,
5019 NULL);
Greg Clayton030a2042011-10-14 21:34:45 +00005020
Greg Clayton9d3d6882011-10-31 23:51:19 +00005021 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005022 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00005023 else
5024 {
5025 if (decl_ctx == translation_unit_decl)
5026 {
5027 namespace_decl = translation_unit_decl->getAnonymousNamespace();
5028 if (namespace_decl)
5029 return namespace_decl;
5030
Sean Callanan5b26f272012-02-04 08:49:35 +00005031 namespace_decl = NamespaceDecl::Create(*ast,
5032 decl_ctx,
5033 false,
5034 SourceLocation(),
5035 SourceLocation(),
5036 NULL,
5037 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00005038 translation_unit_decl->setAnonymousNamespace (namespace_decl);
5039 translation_unit_decl->addDecl (namespace_decl);
5040 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
5041 }
5042 else
5043 {
5044 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
5045 if (parent_namespace_decl)
5046 {
5047 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
5048 if (namespace_decl)
5049 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00005050 namespace_decl = NamespaceDecl::Create(*ast,
5051 decl_ctx,
5052 false,
5053 SourceLocation(),
5054 SourceLocation(),
5055 NULL,
5056 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00005057 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
5058 parent_namespace_decl->addDecl (namespace_decl);
5059 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
5060 }
5061 else
5062 {
5063 // BAD!!!
5064 }
5065 }
5066
5067
5068 if (namespace_decl)
5069 {
5070 // If we make it here, we are creating the anonymous namespace decl
5071 // for the first time, so we need to do the using directive magic
5072 // like SEMA does
5073 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast,
5074 decl_ctx,
5075 SourceLocation(),
5076 SourceLocation(),
5077 NestedNameSpecifierLoc(),
5078 SourceLocation(),
5079 namespace_decl,
5080 decl_ctx);
5081 using_directive_decl->setImplicit();
5082 decl_ctx->addDecl(using_directive_decl);
5083 }
5084 }
5085#ifdef LLDB_CONFIGURATION_DEBUG
5086 VerifyDecl(namespace_decl);
5087#endif
Greg Clayton030a2042011-10-14 21:34:45 +00005088 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005089}
5090
5091
5092#pragma mark Function Types
5093
5094FunctionDecl *
Greg Clayton147e1fa2011-10-14 22:47:18 +00005095ClangASTContext::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 +00005096{
Greg Clayton147e1fa2011-10-14 22:47:18 +00005097 FunctionDecl *func_decl = NULL;
5098 ASTContext *ast = getASTContext();
5099 if (decl_ctx == NULL)
5100 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005101
Greg Clayton147e1fa2011-10-14 22:47:18 +00005102 if (name && name[0])
5103 {
5104 func_decl = FunctionDecl::Create (*ast,
5105 decl_ctx,
5106 SourceLocation(),
5107 SourceLocation(),
5108 DeclarationName (&ast->Idents.get(name)),
5109 QualType::getFromOpaquePtr(function_clang_type),
5110 NULL,
5111 (FunctionDecl::StorageClass)storage,
5112 (FunctionDecl::StorageClass)storage,
5113 is_inline);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005114 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00005115 else
5116 {
5117 func_decl = FunctionDecl::Create (*ast,
5118 decl_ctx,
5119 SourceLocation(),
5120 SourceLocation(),
5121 DeclarationName (),
5122 QualType::getFromOpaquePtr(function_clang_type),
5123 NULL,
5124 (FunctionDecl::StorageClass)storage,
5125 (FunctionDecl::StorageClass)storage,
5126 is_inline);
5127 }
5128 if (func_decl)
5129 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005130
5131#ifdef LLDB_CONFIGURATION_DEBUG
5132 VerifyDecl(func_decl);
5133#endif
5134
Greg Clayton147e1fa2011-10-14 22:47:18 +00005135 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005136}
5137
Greg Clayton1be10fc2010-09-29 01:12:09 +00005138clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00005139ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00005140 clang_type_t result_type,
5141 clang_type_t *args,
Sean Callananc81256a2010-09-16 20:40:25 +00005142 unsigned num_args,
5143 bool is_variadic,
5144 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005145{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005146 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005147 std::vector<QualType> qual_type_args;
5148 for (unsigned i=0; i<num_args; ++i)
5149 qual_type_args.push_back (QualType::getFromOpaquePtr(args[i]));
5150
5151 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00005152 FunctionProtoType::ExtProtoInfo proto_info;
5153 proto_info.Variadic = is_variadic;
Sean Callananfb0b7582011-03-15 00:17:19 +00005154 proto_info.ExceptionSpecType = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005155 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00005156 proto_info.RefQualifier = RQ_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005157 proto_info.NumExceptions = 0;
5158 proto_info.Exceptions = NULL;
5159
Greg Clayton147e1fa2011-10-14 22:47:18 +00005160 return ast->getFunctionType (QualType::getFromOpaquePtr(result_type),
5161 qual_type_args.empty() ? NULL : &qual_type_args.front(),
5162 qual_type_args.size(),
5163 proto_info).getAsOpaquePtr(); // NoReturn);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005164}
5165
5166ParmVarDecl *
Greg Clayton1be10fc2010-09-29 01:12:09 +00005167ClangASTContext::CreateParameterDeclaration (const char *name, clang_type_t param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005168{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005169 ASTContext *ast = getASTContext();
5170 assert (ast != NULL);
5171 return ParmVarDecl::Create(*ast,
5172 ast->getTranslationUnitDecl(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005173 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005174 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00005175 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callananc81256a2010-09-16 20:40:25 +00005176 QualType::getFromOpaquePtr(param_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005177 NULL,
5178 (VarDecl::StorageClass)storage,
5179 (VarDecl::StorageClass)storage,
5180 0);
5181}
5182
5183void
5184ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
5185{
5186 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00005187 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005188}
5189
5190
5191#pragma mark Array Types
5192
Greg Clayton1be10fc2010-09-29 01:12:09 +00005193clang_type_t
5194ClangASTContext::CreateArrayType (clang_type_t element_type, size_t element_count, uint32_t bit_stride)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005195{
5196 if (element_type)
5197 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00005198 ASTContext *ast = getASTContext();
5199 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005200 llvm::APInt ap_element_count (64, element_count);
Greg Clayton6beaaa62011-01-17 03:46:26 +00005201 return ast->getConstantArrayType(QualType::getFromOpaquePtr(element_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005202 ap_element_count,
5203 ArrayType::Normal,
5204 0).getAsOpaquePtr(); // ElemQuals
5205 }
5206 return NULL;
5207}
5208
5209
5210#pragma mark TagDecl
5211
5212bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005213ClangASTContext::StartTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005214{
5215 if (clang_type)
5216 {
5217 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00005218 const clang::Type *t = qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005219 if (t)
5220 {
Sean Callanan78e37602011-01-27 04:42:51 +00005221 const TagType *tag_type = dyn_cast<TagType>(t);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005222 if (tag_type)
5223 {
5224 TagDecl *tag_decl = tag_type->getDecl();
5225 if (tag_decl)
5226 {
5227 tag_decl->startDefinition();
5228 return true;
5229 }
5230 }
Sean Callanan5b26f272012-02-04 08:49:35 +00005231
5232 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(t);
5233 if (object_type)
5234 {
5235 ObjCInterfaceDecl *interface_decl = object_type->getInterface();
5236 if (interface_decl)
5237 {
5238 interface_decl->startDefinition();
5239 return true;
5240 }
5241 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005242 }
5243 }
5244 return false;
5245}
5246
5247bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005248ClangASTContext::CompleteTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005249{
5250 if (clang_type)
5251 {
5252 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton14372242010-09-29 03:44:17 +00005253
5254 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5255
5256 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005257 {
Greg Clayton14372242010-09-29 03:44:17 +00005258 cxx_record_decl->completeDefinition();
5259
5260 return true;
5261 }
5262
5263 const EnumType *enum_type = dyn_cast<EnumType>(qual_type.getTypePtr());
5264
5265 if (enum_type)
5266 {
5267 EnumDecl *enum_decl = enum_type->getDecl();
5268
5269 if (enum_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005270 {
Greg Clayton14372242010-09-29 03:44:17 +00005271 /// TODO This really needs to be fixed.
5272
5273 unsigned NumPositiveBits = 1;
5274 unsigned NumNegativeBits = 0;
5275
Greg Clayton6beaaa62011-01-17 03:46:26 +00005276 ASTContext *ast = getASTContext();
Greg Claytone02b8502010-10-12 04:29:14 +00005277
5278 QualType promotion_qual_type;
5279 // If the enum integer type is less than an integer in bit width,
5280 // then we must promote it to an integer size.
Greg Clayton6beaaa62011-01-17 03:46:26 +00005281 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
Greg Claytone02b8502010-10-12 04:29:14 +00005282 {
5283 if (enum_decl->getIntegerType()->isSignedIntegerType())
Greg Clayton6beaaa62011-01-17 03:46:26 +00005284 promotion_qual_type = ast->IntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005285 else
Greg Clayton6beaaa62011-01-17 03:46:26 +00005286 promotion_qual_type = ast->UnsignedIntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005287 }
5288 else
5289 promotion_qual_type = enum_decl->getIntegerType();
5290
5291 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
Greg Clayton14372242010-09-29 03:44:17 +00005292 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005293 }
5294 }
5295 }
5296 return false;
5297}
5298
5299
5300#pragma mark Enumeration Types
5301
Greg Clayton1be10fc2010-09-29 01:12:09 +00005302clang_type_t
Greg Claytonca512b32011-01-14 04:54:56 +00005303ClangASTContext::CreateEnumerationType
5304(
5305 const char *name,
5306 DeclContext *decl_ctx,
5307 const Declaration &decl,
5308 clang_type_t integer_qual_type
5309)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005310{
5311 // TODO: Do something intelligent with the Declaration object passed in
5312 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005313 ASTContext *ast = getASTContext();
5314 assert (ast != NULL);
Greg Claytone02b8502010-10-12 04:29:14 +00005315
5316 // TODO: ask about these...
5317// const bool IsScoped = false;
5318// const bool IsFixed = false;
5319
Greg Clayton6beaaa62011-01-17 03:46:26 +00005320 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00005321 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00005322 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00005323 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005324 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callanan48114472010-12-13 01:26:27 +00005325 NULL,
5326 false, // IsScoped
5327 false, // IsScopedUsingClassTag
5328 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00005329
5330
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005331 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00005332 {
5333 // TODO: check if we should be setting the promotion type too?
5334 enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00005335
5336 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
5337
Greg Clayton6beaaa62011-01-17 03:46:26 +00005338 return ast->getTagDeclType(enum_decl).getAsOpaquePtr();
Greg Clayton83ff3892010-09-12 23:17:56 +00005339 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005340 return NULL;
5341}
5342
Greg Clayton1be10fc2010-09-29 01:12:09 +00005343clang_type_t
5344ClangASTContext::GetEnumerationIntegerType (clang_type_t enum_clang_type)
5345{
5346 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5347
Sean Callanan78e37602011-01-27 04:42:51 +00005348 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Greg Clayton1be10fc2010-09-29 01:12:09 +00005349 if (clang_type)
5350 {
5351 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5352 if (enum_type)
5353 {
5354 EnumDecl *enum_decl = enum_type->getDecl();
5355 if (enum_decl)
5356 return enum_decl->getIntegerType().getAsOpaquePtr();
5357 }
5358 }
5359 return NULL;
5360}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005361bool
5362ClangASTContext::AddEnumerationValueToEnumerationType
5363(
Greg Clayton1be10fc2010-09-29 01:12:09 +00005364 clang_type_t enum_clang_type,
5365 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005366 const Declaration &decl,
5367 const char *name,
5368 int64_t enum_value,
5369 uint32_t enum_value_bit_size
5370)
5371{
5372 if (enum_clang_type && enumerator_clang_type && name)
5373 {
5374 // TODO: Do something intelligent with the Declaration object passed in
5375 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005376 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005377 IdentifierTable *identifier_table = getIdentifierTable();
5378
Greg Clayton6beaaa62011-01-17 03:46:26 +00005379 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005380 assert (identifier_table != NULL);
5381 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5382
Sean Callanan78e37602011-01-27 04:42:51 +00005383 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005384 if (clang_type)
5385 {
5386 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5387
5388 if (enum_type)
5389 {
5390 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, false);
5391 enum_llvm_apsint = enum_value;
5392 EnumConstantDecl *enumerator_decl =
Greg Clayton6beaaa62011-01-17 03:46:26 +00005393 EnumConstantDecl::Create (*ast,
5394 enum_type->getDecl(),
5395 SourceLocation(),
5396 name ? &identifier_table->get(name) : NULL, // Identifier
5397 QualType::getFromOpaquePtr(enumerator_clang_type),
5398 NULL,
5399 enum_llvm_apsint);
5400
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005401 if (enumerator_decl)
5402 {
5403 enum_type->getDecl()->addDecl(enumerator_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005404
5405#ifdef LLDB_CONFIGURATION_DEBUG
5406 VerifyDecl(enumerator_decl);
5407#endif
5408
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005409 return true;
5410 }
5411 }
5412 }
5413 }
5414 return false;
5415}
5416
5417#pragma mark Pointers & References
5418
Greg Clayton1be10fc2010-09-29 01:12:09 +00005419clang_type_t
5420ClangASTContext::CreatePointerType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005421{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005422 return CreatePointerType (getASTContext(), clang_type);
5423}
5424
5425clang_type_t
5426ClangASTContext::CreatePointerType (clang::ASTContext *ast, clang_type_t clang_type)
5427{
5428 if (ast && clang_type)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005429 {
5430 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5431
Greg Clayton737b9322010-09-13 03:32:57 +00005432 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5433 switch (type_class)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005434 {
5435 case clang::Type::ObjCObject:
5436 case clang::Type::ObjCInterface:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005437 return ast->getObjCObjectPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005438
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005439 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005440 return ast->getPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005441 }
5442 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005443 return NULL;
5444}
5445
Greg Clayton1be10fc2010-09-29 01:12:09 +00005446clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005447ClangASTContext::CreateLValueReferenceType (clang::ASTContext *ast,
5448 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005449{
5450 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005451 return ast->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005452 return NULL;
5453}
5454
Greg Clayton1be10fc2010-09-29 01:12:09 +00005455clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005456ClangASTContext::CreateRValueReferenceType (clang::ASTContext *ast,
5457 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005458{
5459 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005460 return ast->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005461 return NULL;
5462}
5463
Greg Clayton1be10fc2010-09-29 01:12:09 +00005464clang_type_t
5465ClangASTContext::CreateMemberPointerType (clang_type_t clang_pointee_type, clang_type_t clang_class_type)
Greg Clayton9b81a312010-06-12 01:20:30 +00005466{
5467 if (clang_pointee_type && clang_pointee_type)
5468 return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type),
5469 QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr();
5470 return NULL;
5471}
5472
Greg Clayton1a65ae12011-01-25 23:55:37 +00005473uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005474ClangASTContext::GetPointerBitSize ()
5475{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005476 ASTContext *ast = getASTContext();
5477 return ast->getTypeSize(ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005478}
5479
5480bool
Greg Clayton219cf312012-03-30 00:51:13 +00005481ClangASTContext::IsPossibleDynamicType (clang::ASTContext *ast,
5482 clang_type_t clang_type,
5483 clang_type_t *dynamic_pointee_type,
5484 bool check_cplusplus,
5485 bool check_objc)
Greg Claytondea8cb42011-06-29 22:09:02 +00005486{
5487 QualType pointee_qual_type;
5488 if (clang_type)
5489 {
5490 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5491 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5492 bool success = false;
5493 switch (type_class)
5494 {
5495 case clang::Type::Builtin:
Greg Clayton219cf312012-03-30 00:51:13 +00005496 if (check_objc && cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
Greg Claytondea8cb42011-06-29 22:09:02 +00005497 {
5498 if (dynamic_pointee_type)
5499 *dynamic_pointee_type = clang_type;
5500 return true;
5501 }
5502 break;
5503
5504 case clang::Type::ObjCObjectPointer:
Greg Clayton219cf312012-03-30 00:51:13 +00005505 if (check_objc)
5506 {
5507 if (dynamic_pointee_type)
5508 *dynamic_pointee_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5509 return true;
5510 }
5511 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005512
5513 case clang::Type::Pointer:
5514 pointee_qual_type = cast<PointerType>(qual_type)->getPointeeType();
5515 success = true;
5516 break;
5517
5518 case clang::Type::LValueReference:
5519 case clang::Type::RValueReference:
5520 pointee_qual_type = cast<ReferenceType>(qual_type)->getPointeeType();
5521 success = true;
5522 break;
5523
5524 case clang::Type::Typedef:
Greg Claytonaffb03b2011-07-08 18:27:39 +00005525 return ClangASTContext::IsPossibleDynamicType (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), dynamic_pointee_type);
Sean Callanan912855f2011-08-11 23:56:13 +00005526
5527 case clang::Type::Elaborated:
5528 return ClangASTContext::IsPossibleDynamicType (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), dynamic_pointee_type);
5529
Greg Claytondea8cb42011-06-29 22:09:02 +00005530 default:
5531 break;
5532 }
5533
5534 if (success)
5535 {
5536 // Check to make sure what we are pointing too is a possible dynamic C++ type
5537 // We currently accept any "void *" (in case we have a class that has been
5538 // watered down to an opaque pointer) and virtual C++ classes.
5539 const clang::Type::TypeClass pointee_type_class = pointee_qual_type->getTypeClass();
5540 switch (pointee_type_class)
5541 {
5542 case clang::Type::Builtin:
5543 switch (cast<clang::BuiltinType>(pointee_qual_type)->getKind())
5544 {
5545 case clang::BuiltinType::UnknownAny:
5546 case clang::BuiltinType::Void:
5547 if (dynamic_pointee_type)
5548 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5549 return true;
5550
5551 case clang::BuiltinType::NullPtr:
5552 case clang::BuiltinType::Bool:
5553 case clang::BuiltinType::Char_U:
5554 case clang::BuiltinType::UChar:
5555 case clang::BuiltinType::WChar_U:
5556 case clang::BuiltinType::Char16:
5557 case clang::BuiltinType::Char32:
5558 case clang::BuiltinType::UShort:
5559 case clang::BuiltinType::UInt:
5560 case clang::BuiltinType::ULong:
5561 case clang::BuiltinType::ULongLong:
5562 case clang::BuiltinType::UInt128:
5563 case clang::BuiltinType::Char_S:
5564 case clang::BuiltinType::SChar:
5565 case clang::BuiltinType::WChar_S:
5566 case clang::BuiltinType::Short:
5567 case clang::BuiltinType::Int:
5568 case clang::BuiltinType::Long:
5569 case clang::BuiltinType::LongLong:
5570 case clang::BuiltinType::Int128:
5571 case clang::BuiltinType::Float:
5572 case clang::BuiltinType::Double:
5573 case clang::BuiltinType::LongDouble:
5574 case clang::BuiltinType::Dependent:
5575 case clang::BuiltinType::Overload:
5576 case clang::BuiltinType::ObjCId:
5577 case clang::BuiltinType::ObjCClass:
5578 case clang::BuiltinType::ObjCSel:
5579 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00005580 case clang::BuiltinType::Half:
5581 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00005582 case clang::BuiltinType::PseudoObject:
Greg Claytondea8cb42011-06-29 22:09:02 +00005583 break;
5584 }
5585 break;
5586
5587 case clang::Type::Record:
Greg Clayton219cf312012-03-30 00:51:13 +00005588 if (check_cplusplus)
Greg Claytondea8cb42011-06-29 22:09:02 +00005589 {
5590 CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
5591 if (cxx_record_decl)
5592 {
Greg Claytone26928c2012-03-22 22:23:17 +00005593 // Do NOT complete the type here like we used to do
5594 // otherwise EVERY "class *" variable we have will try
5595 // to fully complete itself and this will take a lot of
5596 // time, memory and slow down debugging. If we have a complete
5597 // type, then answer the question definitively, else we
5598 // just say that a C++ class can possibly be dynamic...
Greg Clayton219cf312012-03-30 00:51:13 +00005599 if (cxx_record_decl->isCompleteDefinition())
Greg Claytondea8cb42011-06-29 22:09:02 +00005600 {
5601 success = cxx_record_decl->isDynamicClass();
5602 }
5603 else
5604 {
5605 // We failed to get the complete type, so we have to
5606 // treat this as a void * which we might possibly be
5607 // able to complete
5608 success = true;
5609 }
5610 if (success)
5611 {
5612 if (dynamic_pointee_type)
5613 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5614 return true;
5615 }
5616 }
5617 }
5618 break;
5619
5620 case clang::Type::ObjCObject:
5621 case clang::Type::ObjCInterface:
Greg Clayton219cf312012-03-30 00:51:13 +00005622 if (check_objc)
5623 {
5624 if (dynamic_pointee_type)
5625 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5626 return true;
5627 }
5628 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005629
5630 default:
5631 break;
5632 }
5633 }
5634 }
5635 if (dynamic_pointee_type)
5636 *dynamic_pointee_type = NULL;
5637 return false;
5638}
5639
5640
5641bool
Greg Clayton007d5be2011-05-30 00:49:24 +00005642ClangASTContext::IsPossibleCPlusPlusDynamicType (clang::ASTContext *ast, clang_type_t clang_type, clang_type_t *dynamic_pointee_type)
5643{
Greg Clayton219cf312012-03-30 00:51:13 +00005644 return IsPossibleDynamicType (ast,
5645 clang_type,
5646 dynamic_pointee_type,
5647 true, // Check for dynamic C++ types
5648 false); // Check for dynamic ObjC types
Greg Clayton007d5be2011-05-30 00:49:24 +00005649}
5650
Sean Callanan98298012011-10-27 19:41:13 +00005651bool
5652ClangASTContext::IsReferenceType (clang_type_t clang_type, clang_type_t *target_type)
5653{
5654 if (clang_type == NULL)
5655 return false;
5656
5657 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5658 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5659
5660 switch (type_class)
5661 {
5662 case clang::Type::LValueReference:
5663 if (target_type)
5664 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5665 return true;
5666 case clang::Type::RValueReference:
5667 if (target_type)
5668 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5669 return true;
5670 case clang::Type::Typedef:
5671 return ClangASTContext::IsReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5672 case clang::Type::Elaborated:
5673 return ClangASTContext::IsReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5674 default:
5675 break;
5676 }
5677
5678 return false;
5679}
Greg Clayton007d5be2011-05-30 00:49:24 +00005680
5681bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005682ClangASTContext::IsPointerOrReferenceType (clang_type_t clang_type, clang_type_t*target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005683{
5684 if (clang_type == NULL)
5685 return false;
5686
5687 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005688 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5689 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005690 {
Sean Callanana2424172010-10-25 00:29:48 +00005691 case clang::Type::Builtin:
5692 switch (cast<clang::BuiltinType>(qual_type)->getKind())
5693 {
5694 default:
5695 break;
5696 case clang::BuiltinType::ObjCId:
5697 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00005698 return true;
5699 }
5700 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00005701 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005702 if (target_type)
5703 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5704 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005705 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005706 if (target_type)
5707 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5708 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005709 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005710 if (target_type)
5711 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5712 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005713 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005714 if (target_type)
5715 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5716 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005717 case clang::Type::LValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005718 if (target_type)
5719 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5720 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005721 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005722 if (target_type)
5723 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5724 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005725 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00005726 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00005727 case clang::Type::Elaborated:
5728 return ClangASTContext::IsPointerOrReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005729 default:
5730 break;
5731 }
5732 return false;
5733}
5734
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005735bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005736ClangASTContext::IsIntegerType (clang_type_t clang_type, bool &is_signed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005737{
5738 if (!clang_type)
5739 return false;
5740
5741 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5742 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal());
5743
5744 if (builtin_type)
5745 {
5746 if (builtin_type->isInteger())
Jim Inghamef651602011-12-22 19:12:40 +00005747 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005748 is_signed = builtin_type->isSignedInteger();
Jim Inghamef651602011-12-22 19:12:40 +00005749 return true;
5750 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005751 }
5752
5753 return false;
5754}
5755
5756bool
Greg Claytonaffb03b2011-07-08 18:27:39 +00005757ClangASTContext::IsPointerType (clang_type_t clang_type, clang_type_t *target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005758{
Greg Claytonaffb03b2011-07-08 18:27:39 +00005759 if (target_type)
5760 *target_type = NULL;
5761
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005762 if (clang_type)
5763 {
5764 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005765 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5766 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005767 {
Sean Callanana2424172010-10-25 00:29:48 +00005768 case clang::Type::Builtin:
5769 switch (cast<clang::BuiltinType>(qual_type)->getKind())
5770 {
5771 default:
5772 break;
5773 case clang::BuiltinType::ObjCId:
5774 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00005775 return true;
5776 }
5777 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00005778 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005779 if (target_type)
5780 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5781 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005782 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005783 if (target_type)
5784 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5785 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005786 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005787 if (target_type)
5788 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5789 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005790 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005791 if (target_type)
5792 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5793 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005794 case clang::Type::Typedef:
Greg Claytonaffb03b2011-07-08 18:27:39 +00005795 return ClangASTContext::IsPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), target_type);
Sean Callanan912855f2011-08-11 23:56:13 +00005796 case clang::Type::Elaborated:
5797 return ClangASTContext::IsPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), target_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005798 default:
5799 break;
5800 }
5801 }
5802 return false;
5803}
5804
5805bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005806ClangASTContext::IsFloatingPointType (clang_type_t clang_type, uint32_t &count, bool &is_complex)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005807{
5808 if (clang_type)
5809 {
5810 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5811
5812 if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()))
5813 {
5814 clang::BuiltinType::Kind kind = BT->getKind();
5815 if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble)
5816 {
5817 count = 1;
5818 is_complex = false;
5819 return true;
5820 }
5821 }
5822 else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal()))
5823 {
5824 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex))
5825 {
5826 count = 2;
5827 is_complex = true;
5828 return true;
5829 }
5830 }
5831 else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal()))
5832 {
5833 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex))
5834 {
5835 count = VT->getNumElements();
5836 is_complex = false;
5837 return true;
5838 }
5839 }
5840 }
5841 return false;
5842}
5843
Enrico Granata9fc19442011-07-06 02:13:41 +00005844bool
5845ClangASTContext::IsScalarType (lldb::clang_type_t clang_type)
5846{
5847 bool is_signed;
5848 if (ClangASTContext::IsIntegerType(clang_type, is_signed))
5849 return true;
5850
5851 uint32_t count;
5852 bool is_complex;
5853 return ClangASTContext::IsFloatingPointType(clang_type, count, is_complex) && !is_complex;
5854}
5855
5856bool
5857ClangASTContext::IsPointerToScalarType (lldb::clang_type_t clang_type)
5858{
5859 if (!IsPointerType(clang_type))
5860 return false;
5861
5862 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5863 lldb::clang_type_t pointee_type = qual_type.getTypePtr()->getPointeeType().getAsOpaquePtr();
5864 return IsScalarType(pointee_type);
5865}
5866
5867bool
5868ClangASTContext::IsArrayOfScalarType (lldb::clang_type_t clang_type)
5869{
Sean Callanan0caa21c2012-01-19 23:54:24 +00005870 clang_type = GetAsArrayType(clang_type);
5871
5872 if (clang_type == 0)
Enrico Granata9fc19442011-07-06 02:13:41 +00005873 return false;
5874
5875 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5876 lldb::clang_type_t item_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
5877 return IsScalarType(item_type);
5878}
5879
Greg Clayton8f92f0a2010-10-14 22:52:14 +00005880
5881bool
5882ClangASTContext::GetCXXClassName (clang_type_t clang_type, std::string &class_name)
5883{
5884 if (clang_type)
5885 {
5886 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5887
5888 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5889 if (cxx_record_decl)
5890 {
5891 class_name.assign (cxx_record_decl->getIdentifier()->getNameStart());
5892 return true;
5893 }
5894 }
5895 class_name.clear();
5896 return false;
5897}
5898
5899
Greg Clayton0fffff52010-09-24 05:15:53 +00005900bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005901ClangASTContext::IsCXXClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00005902{
5903 if (clang_type)
5904 {
5905 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5906 if (qual_type->getAsCXXRecordDecl() != NULL)
5907 return true;
5908 }
5909 return false;
5910}
5911
Greg Clayton20568dd2011-10-13 23:13:20 +00005912bool
5913ClangASTContext::IsBeingDefined (lldb::clang_type_t clang_type)
5914{
5915 if (clang_type)
5916 {
5917 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5918 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type);
5919 if (tag_type)
5920 return tag_type->isBeingDefined();
5921 }
5922 return false;
5923}
5924
Greg Clayton0fffff52010-09-24 05:15:53 +00005925bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005926ClangASTContext::IsObjCClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00005927{
5928 if (clang_type)
5929 {
5930 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5931 if (qual_type->isObjCObjectOrInterfaceType())
5932 return true;
5933 }
5934 return false;
5935}
5936
Sean Callanan72772842012-02-22 23:57:45 +00005937bool
5938ClangASTContext::IsObjCObjectPointerType (lldb::clang_type_t clang_type, clang_type_t *class_type)
5939{
5940 if (clang_type)
5941 {
5942 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5943 if (qual_type->isObjCObjectPointerType())
5944 {
5945 if (class_type)
5946 {
5947 *class_type = NULL;
5948
5949 if (!qual_type->isObjCClassType() &&
5950 !qual_type->isObjCIdType())
5951 {
5952 const ObjCObjectPointerType *obj_pointer_type = dyn_cast<ObjCObjectPointerType>(qual_type);
Sean Callanand7dabe22012-03-10 01:59:11 +00005953 if (!obj_pointer_type)
5954 *class_type = NULL;
Sean Callanan1fc91ad2012-03-10 02:00:32 +00005955 else
5956 *class_type = QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr();
Sean Callanan72772842012-02-22 23:57:45 +00005957 }
5958 }
5959 return true;
5960 }
5961 }
5962 return false;
5963}
5964
5965bool
5966ClangASTContext::GetObjCClassName (lldb::clang_type_t clang_type,
5967 std::string &class_name)
5968{
5969 if (!clang_type)
5970 return false;
5971
5972 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(QualType::getFromOpaquePtr(clang_type));
5973 if (!object_type)
5974 return false;
5975
5976 const ObjCInterfaceDecl *interface = object_type->getInterface();
5977 if (!interface)
5978 return false;
5979
5980 class_name = interface->getNameAsString();
5981 return true;
5982}
Greg Clayton0fffff52010-09-24 05:15:53 +00005983
Greg Clayton73b472d2010-10-27 03:32:59 +00005984bool
5985ClangASTContext::IsCharType (clang_type_t clang_type)
5986{
5987 if (clang_type)
5988 return QualType::getFromOpaquePtr(clang_type)->isCharType();
5989 return false;
5990}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005991
5992bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005993ClangASTContext::IsCStringType (clang_type_t clang_type, uint32_t &length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005994{
Greg Clayton73b472d2010-10-27 03:32:59 +00005995 clang_type_t pointee_or_element_clang_type = NULL;
5996 Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, &pointee_or_element_clang_type));
5997
5998 if (pointee_or_element_clang_type == NULL)
5999 return false;
6000
6001 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006002 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006003 QualType pointee_or_element_qual_type (QualType::getFromOpaquePtr (pointee_or_element_clang_type));
6004
6005 if (pointee_or_element_qual_type.getUnqualifiedType()->isCharType())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006006 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006007 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6008 if (type_flags.Test (eTypeIsArray))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006009 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006010 // We know the size of the array and it could be a C string
6011 // since it is an array of characters
6012 length = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
6013 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006014 }
Greg Clayton73b472d2010-10-27 03:32:59 +00006015 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006016 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006017 length = 0;
6018 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006019 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006020
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006021 }
6022 }
6023 return false;
6024}
6025
6026bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006027ClangASTContext::IsFunctionPointerType (clang_type_t clang_type)
Greg Clayton737b9322010-09-13 03:32:57 +00006028{
6029 if (clang_type)
6030 {
6031 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6032
6033 if (qual_type->isFunctionPointerType())
6034 return true;
6035
6036 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6037 switch (type_class)
6038 {
Sean Callananfb0b7582011-03-15 00:17:19 +00006039 default:
6040 break;
Greg Clayton737b9322010-09-13 03:32:57 +00006041 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00006042 return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006043 case clang::Type::Elaborated:
6044 return ClangASTContext::IsFunctionPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00006045
6046 case clang::Type::LValueReference:
6047 case clang::Type::RValueReference:
6048 {
Sean Callanan78e37602011-01-27 04:42:51 +00006049 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00006050 if (reference_type)
6051 return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr());
6052 }
6053 break;
6054 }
6055 }
6056 return false;
6057}
6058
Greg Clayton73b472d2010-10-27 03:32:59 +00006059size_t
6060ClangASTContext::GetArraySize (clang_type_t clang_type)
6061{
6062 if (clang_type)
6063 {
Greg Claytonef37d68a2011-07-09 17:12:27 +00006064 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
6065 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6066 switch (type_class)
6067 {
6068 case clang::Type::ConstantArray:
6069 {
6070 const ConstantArrayType *array = cast<ConstantArrayType>(QualType::getFromOpaquePtr(clang_type).getTypePtr());
6071 if (array)
6072 return array->getSize().getLimitedValue();
6073 }
6074 break;
6075
6076 case clang::Type::Typedef:
6077 return ClangASTContext::GetArraySize(cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006078
6079 case clang::Type::Elaborated:
6080 return ClangASTContext::GetArraySize(cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00006081
6082 default:
6083 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006084 }
Greg Clayton73b472d2010-10-27 03:32:59 +00006085 }
6086 return 0;
6087}
Greg Clayton737b9322010-09-13 03:32:57 +00006088
Sean Callanan0caa21c2012-01-19 23:54:24 +00006089clang_type_t
6090ClangASTContext::GetAsArrayType (clang_type_t clang_type, clang_type_t*member_type, uint64_t *size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006091{
6092 if (!clang_type)
Sean Callanan0caa21c2012-01-19 23:54:24 +00006093 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006094
6095 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6096
Greg Clayton737b9322010-09-13 03:32:57 +00006097 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6098 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006099 {
Sean Callananfb0b7582011-03-15 00:17:19 +00006100 default:
6101 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006102
Greg Claytone1a916a2010-07-21 22:12:05 +00006103 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006104 if (member_type)
6105 *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6106 if (size)
Greg Claytonac4827f2011-04-01 18:14:08 +00006107 *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
Sean Callanan0caa21c2012-01-19 23:54:24 +00006108 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006109
Greg Claytone1a916a2010-07-21 22:12:05 +00006110 case clang::Type::IncompleteArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006111 if (member_type)
6112 *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6113 if (size)
6114 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006115 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006116
Greg Claytone1a916a2010-07-21 22:12:05 +00006117 case clang::Type::VariableArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006118 if (member_type)
6119 *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6120 if (size)
6121 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006122 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006123
Greg Claytone1a916a2010-07-21 22:12:05 +00006124 case clang::Type::DependentSizedArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006125 if (member_type)
6126 *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6127 if (size)
6128 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006129 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006130
6131 case clang::Type::Typedef:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006132 return ClangASTContext::GetAsArrayType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
6133 member_type,
6134 size);
Sean Callanan912855f2011-08-11 23:56:13 +00006135
6136 case clang::Type::Elaborated:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006137 return ClangASTContext::GetAsArrayType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
6138 member_type,
6139 size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006140 }
Sean Callanan0caa21c2012-01-19 23:54:24 +00006141 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006142}
6143
6144
6145#pragma mark Typedefs
6146
Greg Clayton1be10fc2010-09-29 01:12:09 +00006147clang_type_t
6148ClangASTContext::CreateTypedefType (const char *name, clang_type_t clang_type, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006149{
6150 if (clang_type)
6151 {
6152 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006153 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006154 IdentifierTable *identifier_table = getIdentifierTable();
Greg Clayton6beaaa62011-01-17 03:46:26 +00006155 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006156 assert (identifier_table != NULL);
6157 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00006158 decl_ctx = ast->getTranslationUnitDecl();
6159 TypedefDecl *decl = TypedefDecl::Create (*ast,
6160 decl_ctx,
6161 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00006162 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00006163 name ? &identifier_table->get(name) : NULL, // Identifier
6164 ast->CreateTypeSourceInfo(qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00006165
Greg Clayton147e1fa2011-10-14 22:47:18 +00006166 //decl_ctx->addDecl (decl);
6167
Sean Callanan2652ad22011-01-18 01:03:44 +00006168 decl->setAccess(AS_public); // TODO respect proper access specifier
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006169
6170 // Get a uniqued QualType for the typedef decl type
Greg Clayton6beaaa62011-01-17 03:46:26 +00006171 return ast->getTypedefType (decl).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006172 }
6173 return NULL;
6174}
6175
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006176// Disable this for now since I can't seem to get a nicely formatted float
6177// out of the APFloat class without just getting the float, double or quad
6178// and then using a formatted print on it which defeats the purpose. We ideally
6179// would like to get perfect string values for any kind of float semantics
6180// so we can support remote targets. The code below also requires a patch to
6181// llvm::APInt.
6182//bool
Greg Clayton6beaaa62011-01-17 03:46:26 +00006183//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 +00006184//{
6185// uint32_t count = 0;
6186// bool is_complex = false;
6187// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6188// {
6189// unsigned num_bytes_per_float = byte_size / count;
6190// unsigned num_bits_per_float = num_bytes_per_float * 8;
6191//
6192// float_str.clear();
6193// uint32_t i;
6194// for (i=0; i<count; i++)
6195// {
6196// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
6197// bool is_ieee = false;
6198// APFloat ap_float(ap_int, is_ieee);
6199// char s[1024];
6200// unsigned int hex_digits = 0;
6201// bool upper_case = false;
6202//
6203// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
6204// {
6205// if (i > 0)
6206// float_str.append(", ");
6207// float_str.append(s);
6208// if (i == 1 && is_complex)
6209// float_str.append(1, 'i');
6210// }
6211// }
6212// return !float_str.empty();
6213// }
6214// return false;
6215//}
6216
6217size_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00006218ClangASTContext::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 +00006219{
6220 if (clang_type)
6221 {
6222 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6223 uint32_t count = 0;
6224 bool is_complex = false;
6225 if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6226 {
6227 // TODO: handle complex and vector types
6228 if (count != 1)
6229 return false;
6230
6231 StringRef s_sref(s);
Greg Clayton6beaaa62011-01-17 03:46:26 +00006232 APFloat ap_float(ast->getFloatTypeSemantics(qual_type), s_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006233
Greg Clayton6beaaa62011-01-17 03:46:26 +00006234 const uint64_t bit_size = ast->getTypeSize (qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006235 const uint64_t byte_size = bit_size / 8;
6236 if (dst_size >= byte_size)
6237 {
6238 if (bit_size == sizeof(float)*8)
6239 {
6240 float float32 = ap_float.convertToFloat();
6241 ::memcpy (dst, &float32, byte_size);
6242 return byte_size;
6243 }
6244 else if (bit_size >= 64)
6245 {
6246 llvm::APInt ap_int(ap_float.bitcastToAPInt());
6247 ::memcpy (dst, ap_int.getRawData(), byte_size);
6248 return byte_size;
6249 }
6250 }
6251 }
6252 }
6253 return 0;
6254}
Sean Callanan6fe64b52010-09-17 02:24:29 +00006255
6256unsigned
Greg Clayton1be10fc2010-09-29 01:12:09 +00006257ClangASTContext::GetTypeQualifiers(clang_type_t clang_type)
Sean Callanan6fe64b52010-09-17 02:24:29 +00006258{
6259 assert (clang_type);
6260
6261 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6262
6263 return qual_type.getQualifiers().getCVRQualifiers();
6264}
Greg Clayton6beaaa62011-01-17 03:46:26 +00006265
6266bool
6267ClangASTContext::GetCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6268{
6269 if (clang_type == NULL)
6270 return false;
6271
Greg Claytonc432c192011-01-20 04:18:48 +00006272 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006273}
6274
6275
6276bool
6277ClangASTContext::GetCompleteType (clang_type_t clang_type)
6278{
6279 return ClangASTContext::GetCompleteType (getASTContext(), clang_type);
6280}
6281
Greg Claytona2721472011-06-25 00:44:06 +00006282bool
Enrico Granata86027e92012-03-24 01:11:14 +00006283ClangASTContext::IsCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6284{
6285 if (clang_type == NULL)
6286 return false;
6287
6288 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type), false); // just check but don't let it actually complete
6289}
6290
6291
6292bool
6293ClangASTContext::IsCompleteType (clang_type_t clang_type)
6294{
6295 return ClangASTContext::IsCompleteType (getASTContext(), clang_type);
6296}
6297
6298bool
Greg Claytona2721472011-06-25 00:44:06 +00006299ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
6300 clang::Decl *decl)
6301{
6302 if (!decl)
6303 return false;
6304
6305 ExternalASTSource *ast_source = ast->getExternalSource();
6306
6307 if (!ast_source)
6308 return false;
6309
6310 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
6311 {
Greg Clayton219cf312012-03-30 00:51:13 +00006312 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006313 return true;
6314
6315 if (!tag_decl->hasExternalLexicalStorage())
6316 return false;
6317
6318 ast_source->CompleteType(tag_decl);
6319
6320 return !tag_decl->getTypeForDecl()->isIncompleteType();
6321 }
6322 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
6323 {
Sean Callanan5b26f272012-02-04 08:49:35 +00006324 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006325 return true;
6326
6327 if (!objc_interface_decl->hasExternalLexicalStorage())
6328 return false;
6329
6330 ast_source->CompleteType(objc_interface_decl);
6331
Sean Callanan5b26f272012-02-04 08:49:35 +00006332 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00006333 }
6334 else
6335 {
6336 return false;
6337 }
6338}
6339
Sean Callanan60217122012-04-13 00:10:03 +00006340void
6341ClangASTContext::SetMetadata (clang::ASTContext *ast,
6342 uintptr_t object,
6343 uint64_t metadata)
6344{
6345 ClangExternalASTSourceCommon *external_source =
6346 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
6347
6348 if (external_source)
6349 external_source->SetMetadata(object, metadata);
6350}
6351
6352uint64_t
6353ClangASTContext::GetMetadata (clang::ASTContext *ast,
6354 uintptr_t object)
6355{
6356 ClangExternalASTSourceCommon *external_source =
6357 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
6358
6359 if (external_source && external_source->HasMetadata(object))
6360 return external_source->GetMetadata(object);
6361 else
6362 return 0;
6363}
6364
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006365clang::DeclContext *
6366ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
6367{
Sean Callanana87bee82011-08-19 06:19:25 +00006368 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006369}
6370
6371clang::DeclContext *
6372ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
6373{
Sean Callanana87bee82011-08-19 06:19:25 +00006374 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006375}
6376
Greg Clayton685c88c2012-07-14 00:53:55 +00006377
6378bool
6379ClangASTContext::GetClassMethodInfoForDeclContext (clang::DeclContext *decl_ctx,
6380 lldb::LanguageType &language,
6381 bool &is_instance_method,
6382 ConstString &language_object_name)
6383{
6384 language_object_name.Clear();
6385 language = eLanguageTypeUnknown;
6386 is_instance_method = false;
6387
6388 if (decl_ctx)
6389 {
6390 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
6391 {
6392 if (method_decl->isStatic())
6393 {
6394 is_instance_method = false;
6395 }
6396 else
6397 {
6398 language_object_name.SetCString("this");
6399 is_instance_method = true;
6400 }
6401 language = eLanguageTypeC_plus_plus;
6402 return true;
6403 }
6404 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
6405 {
6406 // Both static and instance methods have a "self" object in objective C
6407 language_object_name.SetCString("self");
6408 if (method_decl->isInstanceMethod())
6409 {
6410 is_instance_method = true;
6411 }
6412 else
6413 {
6414 is_instance_method = false;
6415 }
6416 language = eLanguageTypeObjC;
6417 return true;
6418 }
6419 }
6420 return false;
6421}
6422