blob: ec25600b8ec6da28465f843a0db4bd8b038be7bb [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eli Friedman932197d2010-06-13 19:06:42 +000010#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011
12// C Includes
13// C++ Includes
14#include <string>
15
16// Other libraries and framework includes
Greg Clayton6beaaa62011-01-17 03:46:26 +000017
18// Clang headers like to use NDEBUG inside of them to enable/disable debug
19// releated features using "#ifndef NDEBUG" preprocessor blocks to do one thing
20// or another. This is bad because it means that if clang was built in release
21// mode, it assumes that you are building in release mode which is not always
22// the case. You can end up with functions that are defined as empty in header
23// files when NDEBUG is not defined, and this can cause link errors with the
24// clang .a files that you have since you might be missing functions in the .a
25// file. So we have to define NDEBUG when including clang headers to avoid any
26// mismatches. This is covered by rdar://problem/8691220
27
Sean Callanan3b1d4f62011-10-26 17:46:51 +000028#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000029#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000030#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000031// Need to include assert.h so it is as clang would expect it to be (disabled)
32#include <assert.h>
33#endif
34
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "clang/AST/ASTContext.h"
36#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000037#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000039#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000040#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "clang/AST/RecordLayout.h"
42#include "clang/AST/Type.h"
43#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000044#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000046#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "clang/Basic/SourceManager.h"
48#include "clang/Basic/TargetInfo.h"
49#include "clang/Basic/TargetOptions.h"
50#include "clang/Frontend/FrontendOptions.h"
51#include "clang/Frontend/LangStandard.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000052
53#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000054#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000055#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
56// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
57#include <assert.h>
58#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059
Greg Clayton514487e2011-02-15 21:59:32 +000060#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061#include "lldb/Core/dwarf.h"
Greg Clayton73b472d2010-10-27 03:32:59 +000062#include "lldb/Core/Flags.h"
Sean Callananfb8b7092010-10-28 18:19:36 +000063#include "lldb/Core/Log.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000064#include "lldb/Core/RegularExpression.h"
65#include "lldb/Expression/ASTDumper.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000066#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000067#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000068#include "lldb/Target/ExecutionContext.h"
69#include "lldb/Target/Process.h"
70#include "lldb/Target/ObjCLanguageRuntime.h"
71
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072
Eli Friedman932197d2010-06-13 19:06:42 +000073#include <stdio.h>
74
Greg Claytonc86103d2010-08-05 01:57:25 +000075using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076using namespace lldb_private;
77using namespace llvm;
78using namespace clang;
79
Greg Clayton6beaaa62011-01-17 03:46:26 +000080
81static bool
Enrico Granata86027e92012-03-24 01:11:14 +000082GetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion = true)
Greg Clayton6beaaa62011-01-17 03:46:26 +000083{
84 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
85 switch (type_class)
86 {
Sean Callanan5b26f272012-02-04 08:49:35 +000087 case clang::Type::ConstantArray:
88 {
89 const clang::ArrayType *array_type = dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
90
91 if (array_type)
Enrico Granata86027e92012-03-24 01:11:14 +000092 return GetCompleteQualType (ast, array_type->getElementType(), allow_completion);
Sean Callanan5b26f272012-02-04 08:49:35 +000093 }
94 break;
95
Greg Clayton6beaaa62011-01-17 03:46:26 +000096 case clang::Type::Record:
97 case clang::Type::Enum:
98 {
Sean Callanan78e37602011-01-27 04:42:51 +000099 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +0000100 if (tag_type)
101 {
102 clang::TagDecl *tag_decl = tag_type->getDecl();
103 if (tag_decl)
104 {
Greg Clayton219cf312012-03-30 00:51:13 +0000105 if (tag_decl->isCompleteDefinition())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000106 return true;
Enrico Granata86027e92012-03-24 01:11:14 +0000107
108 if (!allow_completion)
109 return false;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000110
111 if (tag_decl->hasExternalLexicalStorage())
112 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000113 if (ast)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000114 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000115 ExternalASTSource *external_ast_source = ast->getExternalSource();
116 if (external_ast_source)
117 {
118 external_ast_source->CompleteType(tag_decl);
119 return !tag_type->isIncompleteType();
120 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000121 }
122 }
123 return false;
124 }
125 }
126
127 }
128 break;
129
130 case clang::Type::ObjCObject:
131 case clang::Type::ObjCInterface:
132 {
Sean Callanan78e37602011-01-27 04:42:51 +0000133 const clang::ObjCObjectType *objc_class_type = dyn_cast<clang::ObjCObjectType>(qual_type);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000134 if (objc_class_type)
135 {
136 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
137 // We currently can't complete objective C types through the newly added ASTContext
138 // because it only supports TagDecl objects right now...
Enrico Granata9dd75c82011-07-15 23:30:15 +0000139 if (class_interface_decl)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000140 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000141 if (class_interface_decl->getDefinition())
142 return true;
143
Enrico Granata86027e92012-03-24 01:11:14 +0000144 if (!allow_completion)
145 return false;
Greg Clayton20533982012-03-30 23:48:28 +0000146
Sean Callanan5b26f272012-02-04 08:49:35 +0000147 if (class_interface_decl->hasExternalLexicalStorage())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000148 {
Enrico Granata0a3958e2011-07-02 00:25:22 +0000149 if (ast)
Greg Clayton007d5be2011-05-30 00:49:24 +0000150 {
Enrico Granata0a3958e2011-07-02 00:25:22 +0000151 ExternalASTSource *external_ast_source = ast->getExternalSource();
152 if (external_ast_source)
153 {
154 external_ast_source->CompleteType (class_interface_decl);
Sean Callanan5b26f272012-02-04 08:49:35 +0000155 return !objc_class_type->isIncompleteType();
Enrico Granata0a3958e2011-07-02 00:25:22 +0000156 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000157 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000158 }
Enrico Granata0a3958e2011-07-02 00:25:22 +0000159 return false;
Sean Callanan5b26f272012-02-04 08:49:35 +0000160 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000161 }
162 }
163 break;
164
165 case clang::Type::Typedef:
Enrico Granata86027e92012-03-24 01:11:14 +0000166 return GetCompleteQualType (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType(), allow_completion);
Sean Callanan912855f2011-08-11 23:56:13 +0000167
168 case clang::Type::Elaborated:
Enrico Granata86027e92012-03-24 01:11:14 +0000169 return GetCompleteQualType (ast, cast<ElaboratedType>(qual_type)->getNamedType(), allow_completion);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000170
171 default:
172 break;
173 }
174
175 return true;
176}
177
Greg Clayton8cf05932010-07-22 18:30:50 +0000178static AccessSpecifier
Greg Claytonc86103d2010-08-05 01:57:25 +0000179ConvertAccessTypeToAccessSpecifier (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000180{
181 switch (access)
182 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000183 default: break;
184 case eAccessNone: return AS_none;
185 case eAccessPublic: return AS_public;
186 case eAccessPrivate: return AS_private;
187 case eAccessProtected: return AS_protected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000188 }
189 return AS_none;
190}
191
192static ObjCIvarDecl::AccessControl
Greg Claytonc86103d2010-08-05 01:57:25 +0000193ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000194{
195 switch (access)
196 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000197 case eAccessNone: return ObjCIvarDecl::None;
198 case eAccessPublic: return ObjCIvarDecl::Public;
199 case eAccessPrivate: return ObjCIvarDecl::Private;
200 case eAccessProtected: return ObjCIvarDecl::Protected;
201 case eAccessPackage: return ObjCIvarDecl::Package;
Greg Clayton8cf05932010-07-22 18:30:50 +0000202 }
203 return ObjCIvarDecl::None;
204}
205
206
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207static void
208ParseLangArgs
209(
210 LangOptions &Opts,
Greg Clayton94e5d782010-06-13 17:34:29 +0000211 InputKind IK
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212)
213{
214 // FIXME: Cleanup per-file based stuff.
215
216 // Set some properties which depend soley on the input kind; it would be nice
217 // to move these to the language standard, and have the driver resolve the
218 // input kind + language standard.
Greg Clayton94e5d782010-06-13 17:34:29 +0000219 if (IK == IK_Asm) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220 Opts.AsmPreprocessor = 1;
Greg Clayton94e5d782010-06-13 17:34:29 +0000221 } else if (IK == IK_ObjC ||
222 IK == IK_ObjCXX ||
223 IK == IK_PreprocessedObjC ||
224 IK == IK_PreprocessedObjCXX) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225 Opts.ObjC1 = Opts.ObjC2 = 1;
226 }
227
228 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
229
230 if (LangStd == LangStandard::lang_unspecified) {
231 // Based on the base language, pick one.
232 switch (IK) {
Greg Clayton94e5d782010-06-13 17:34:29 +0000233 case IK_None:
234 case IK_AST:
Sean Callananfb0b7582011-03-15 00:17:19 +0000235 case IK_LLVM_IR:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000236 assert (!"Invalid input kind!");
Greg Clayton94e5d782010-06-13 17:34:29 +0000237 case IK_OpenCL:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238 LangStd = LangStandard::lang_opencl;
239 break;
Sean Callananfb0b7582011-03-15 00:17:19 +0000240 case IK_CUDA:
241 LangStd = LangStandard::lang_cuda;
242 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000243 case IK_Asm:
244 case IK_C:
245 case IK_PreprocessedC:
246 case IK_ObjC:
247 case IK_PreprocessedObjC:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248 LangStd = LangStandard::lang_gnu99;
249 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000250 case IK_CXX:
251 case IK_PreprocessedCXX:
252 case IK_ObjCXX:
253 case IK_PreprocessedObjCXX:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254 LangStd = LangStandard::lang_gnucxx98;
255 break;
256 }
257 }
258
259 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
Filipe Cabecinhase818ca22012-11-12 21:26:32 +0000260 Opts.LineComment = Std.hasLineComments();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261 Opts.C99 = Std.isC99();
262 Opts.CPlusPlus = Std.isCPlusPlus();
Chandler Carruth38336a12013-01-02 12:55:00 +0000263 Opts.CPlusPlus11 = Std.isCPlusPlus11();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264 Opts.Digraphs = Std.hasDigraphs();
265 Opts.GNUMode = Std.isGNUMode();
266 Opts.GNUInline = !Std.isC99();
267 Opts.HexFloats = Std.hasHexFloats();
268 Opts.ImplicitInt = Std.hasImplicitInt();
Enrico Granatac921e342013-01-10 02:37:22 +0000269
270 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271
272 // OpenCL has some additional defaults.
273 if (LangStd == LangStandard::lang_opencl) {
274 Opts.OpenCL = 1;
275 Opts.AltiVec = 1;
276 Opts.CXXOperatorNames = 1;
277 Opts.LaxVectorConversions = 1;
278 }
279
280 // OpenCL and C++ both have bool, true, false keywords.
281 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
282
283// if (Opts.CPlusPlus)
284// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
285//
286// if (Args.hasArg(OPT_fobjc_gc_only))
287// Opts.setGCMode(LangOptions::GCOnly);
288// else if (Args.hasArg(OPT_fobjc_gc))
289// Opts.setGCMode(LangOptions::HybridGC);
290//
291// if (Args.hasArg(OPT_print_ivar_layout))
292// Opts.ObjCGCBitmapPrint = 1;
293//
294// if (Args.hasArg(OPT_faltivec))
295// Opts.AltiVec = 1;
296//
297// if (Args.hasArg(OPT_pthread))
298// Opts.POSIXThreads = 1;
299//
300// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
301// "default");
302// if (Vis == "default")
Sean Callanan31e851c2010-10-29 18:38:40 +0000303 Opts.setVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304// else if (Vis == "hidden")
305// Opts.setVisibilityMode(LangOptions::Hidden);
306// else if (Vis == "protected")
307// Opts.setVisibilityMode(LangOptions::Protected);
308// else
309// Diags.Report(diag::err_drv_invalid_value)
310// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
311
312// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
313
314 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
315 // is specified, or -std is set to a conforming mode.
316 Opts.Trigraphs = !Opts.GNUMode;
317// if (Args.hasArg(OPT_trigraphs))
318// Opts.Trigraphs = 1;
319//
320// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
321// OPT_fno_dollars_in_identifiers,
322// !Opts.AsmPreprocessor);
323// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
324// Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
325// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
326// if (Args.hasArg(OPT_fno_lax_vector_conversions))
327// Opts.LaxVectorConversions = 0;
328// Opts.Exceptions = Args.hasArg(OPT_fexceptions);
329// Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
330// Opts.Blocks = Args.hasArg(OPT_fblocks);
331// Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char);
332// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
333// Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
334// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
335// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
336// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
337// Opts.AccessControl = Args.hasArg(OPT_faccess_control);
338// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
339// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
340// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
341// Diags);
342// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
343// Opts.ObjCConstantStringClass = getLastArgValue(Args,
344// OPT_fconstant_string_class);
345// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
346// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
347// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
348// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
349// Opts.Static = Args.hasArg(OPT_static_define);
350 Opts.OptimizeSize = 0;
351
352 // FIXME: Eliminate this dependency.
353// unsigned Opt =
354// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
355// Opts.Optimize = Opt != 0;
356 unsigned Opt = 0;
357
358 // This is the __NO_INLINE__ define, which just depends on things like the
359 // optimization level and -fno-inline, not actually whether the backend has
360 // inlining enabled.
361 //
362 // FIXME: This is affected by other options (-fno-inline).
Sean Callanan3d654b32012-09-24 22:25:51 +0000363 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364
365// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
366// switch (SSP) {
367// default:
368// Diags.Report(diag::err_drv_invalid_value)
369// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
370// break;
371// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
372// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
373// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
374// }
375}
376
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377
Greg Clayton6beaaa62011-01-17 03:46:26 +0000378ClangASTContext::ClangASTContext (const char *target_triple) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379 m_target_triple(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000380 m_ast_ap(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381 m_language_options_ap(),
382 m_source_manager_ap(),
Sean Callanan880e6802011-10-07 23:18:13 +0000383 m_diagnostics_engine_ap(),
Sean Callananc5069ad2012-10-17 22:11:14 +0000384 m_target_options_rp(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385 m_target_info_ap(),
386 m_identifier_table_ap(),
387 m_selector_table_ap(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000388 m_builtins_ap(),
389 m_callback_tag_decl (NULL),
390 m_callback_objc_decl (NULL),
391 m_callback_baton (NULL)
392
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393{
394 if (target_triple && target_triple[0])
Greg Clayton880cbb02011-07-30 01:26:02 +0000395 SetTargetTriple (target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396}
397
398//----------------------------------------------------------------------
399// Destructor
400//----------------------------------------------------------------------
401ClangASTContext::~ClangASTContext()
402{
403 m_builtins_ap.reset();
404 m_selector_table_ap.reset();
405 m_identifier_table_ap.reset();
406 m_target_info_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000407 m_target_options_rp.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000408 m_diagnostics_engine_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409 m_source_manager_ap.reset();
410 m_language_options_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000411 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412}
413
414
415void
416ClangASTContext::Clear()
417{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000418 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419 m_language_options_ap.reset();
420 m_source_manager_ap.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000421 m_diagnostics_engine_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000422 m_target_options_rp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423 m_target_info_ap.reset();
424 m_identifier_table_ap.reset();
425 m_selector_table_ap.reset();
426 m_builtins_ap.reset();
427}
428
429const char *
430ClangASTContext::GetTargetTriple ()
431{
432 return m_target_triple.c_str();
433}
434
435void
436ClangASTContext::SetTargetTriple (const char *target_triple)
437{
438 Clear();
439 m_target_triple.assign(target_triple);
440}
441
Greg Clayton514487e2011-02-15 21:59:32 +0000442void
443ClangASTContext::SetArchitecture (const ArchSpec &arch)
444{
Greg Clayton880cbb02011-07-30 01:26:02 +0000445 SetTargetTriple(arch.GetTriple().str().c_str());
Greg Clayton514487e2011-02-15 21:59:32 +0000446}
447
Greg Clayton6beaaa62011-01-17 03:46:26 +0000448bool
449ClangASTContext::HasExternalSource ()
450{
451 ASTContext *ast = getASTContext();
452 if (ast)
453 return ast->getExternalSource () != NULL;
454 return false;
455}
456
457void
458ClangASTContext::SetExternalSource (llvm::OwningPtr<ExternalASTSource> &ast_source_ap)
459{
460 ASTContext *ast = getASTContext();
461 if (ast)
462 {
463 ast->setExternalSource (ast_source_ap);
464 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
465 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
466 }
467}
468
469void
470ClangASTContext::RemoveExternalSource ()
471{
472 ASTContext *ast = getASTContext();
473
474 if (ast)
475 {
476 llvm::OwningPtr<ExternalASTSource> empty_ast_source_ap;
477 ast->setExternalSource (empty_ast_source_ap);
478 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
479 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
480 }
481}
482
483
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484
485ASTContext *
486ClangASTContext::getASTContext()
487{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000488 if (m_ast_ap.get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 {
Greg Clayton6beaaa62011-01-17 03:46:26 +0000490 m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
491 *getSourceManager(),
Sean Callanan880e6802011-10-07 23:18:13 +0000492 getTargetInfo(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000493 *getIdentifierTable(),
494 *getSelectorTable(),
495 *getBuiltinContext(),
496 0));
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000497
Greg Clayton6beaaa62011-01-17 03:46:26 +0000498 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
499 {
500 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
501 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
502 }
503
Sean Callanan880e6802011-10-07 23:18:13 +0000504 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000506 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507}
508
509Builtin::Context *
510ClangASTContext::getBuiltinContext()
511{
512 if (m_builtins_ap.get() == NULL)
Sean Callanan880e6802011-10-07 23:18:13 +0000513 m_builtins_ap.reset (new Builtin::Context());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514 return m_builtins_ap.get();
515}
516
517IdentifierTable *
518ClangASTContext::getIdentifierTable()
519{
520 if (m_identifier_table_ap.get() == NULL)
521 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), NULL));
522 return m_identifier_table_ap.get();
523}
524
525LangOptions *
526ClangASTContext::getLanguageOptions()
527{
528 if (m_language_options_ap.get() == NULL)
529 {
530 m_language_options_ap.reset(new LangOptions());
Greg Clayton94e5d782010-06-13 17:34:29 +0000531 ParseLangArgs(*m_language_options_ap, IK_ObjCXX);
532// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000533 }
534 return m_language_options_ap.get();
535}
536
537SelectorTable *
538ClangASTContext::getSelectorTable()
539{
540 if (m_selector_table_ap.get() == NULL)
541 m_selector_table_ap.reset (new SelectorTable());
542 return m_selector_table_ap.get();
543}
544
Sean Callanan79439e82010-11-18 02:56:27 +0000545clang::FileManager *
546ClangASTContext::getFileManager()
547{
548 if (m_file_manager_ap.get() == NULL)
Greg Clayton38a61402010-12-02 23:20:03 +0000549 {
550 clang::FileSystemOptions file_system_options;
551 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
552 }
Sean Callanan79439e82010-11-18 02:56:27 +0000553 return m_file_manager_ap.get();
554}
555
Greg Claytone1a916a2010-07-21 22:12:05 +0000556clang::SourceManager *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557ClangASTContext::getSourceManager()
558{
559 if (m_source_manager_ap.get() == NULL)
Sean Callanan880e6802011-10-07 23:18:13 +0000560 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561 return m_source_manager_ap.get();
562}
563
Sean Callanan880e6802011-10-07 23:18:13 +0000564clang::DiagnosticsEngine *
565ClangASTContext::getDiagnosticsEngine()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000566{
Sean Callanan880e6802011-10-07 23:18:13 +0000567 if (m_diagnostics_engine_ap.get() == NULL)
Greg Claytona651b532010-11-19 21:46:54 +0000568 {
569 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Sean Callananec8f1ef2012-10-25 01:00:25 +0000570 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
Greg Claytona651b532010-11-19 21:46:54 +0000571 }
Sean Callanan880e6802011-10-07 23:18:13 +0000572 return m_diagnostics_engine_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573}
574
Sean Callanan880e6802011-10-07 23:18:13 +0000575class NullDiagnosticConsumer : public DiagnosticConsumer
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000576{
577public:
Sean Callanan880e6802011-10-07 23:18:13 +0000578 NullDiagnosticConsumer ()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000579 {
580 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
581 }
582
Sean Callanan880e6802011-10-07 23:18:13 +0000583 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const Diagnostic &info)
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000584 {
585 if (m_log)
586 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000587 llvm::SmallVector<char, 32> diag_str(10);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000588 info.FormatDiagnostic(diag_str);
589 diag_str.push_back('\0');
590 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
591 }
592 }
Sean Callanan880e6802011-10-07 23:18:13 +0000593
594 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
595 {
596 return new NullDiagnosticConsumer ();
597 }
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000598private:
599 LogSP m_log;
600};
601
Sean Callanan880e6802011-10-07 23:18:13 +0000602DiagnosticConsumer *
603ClangASTContext::getDiagnosticConsumer()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000604{
Sean Callanan880e6802011-10-07 23:18:13 +0000605 if (m_diagnostic_consumer_ap.get() == NULL)
606 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000607
Sean Callanan880e6802011-10-07 23:18:13 +0000608 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000609}
610
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611TargetOptions *
612ClangASTContext::getTargetOptions()
613{
Sean Callananc5069ad2012-10-17 22:11:14 +0000614 if (m_target_options_rp.getPtr() == NULL && !m_target_triple.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615 {
Sean Callananc5069ad2012-10-17 22:11:14 +0000616 m_target_options_rp.reset ();
617 m_target_options_rp = new TargetOptions();
618 if (m_target_options_rp.getPtr() != NULL)
619 m_target_options_rp->Triple = m_target_triple;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 }
Sean Callananc5069ad2012-10-17 22:11:14 +0000621 return m_target_options_rp.getPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622}
623
624
625TargetInfo *
626ClangASTContext::getTargetInfo()
627{
Greg Clayton70512312012-05-08 01:45:38 +0000628 // target_triple should be something like "x86_64-apple-macosx"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 if (m_target_info_ap.get() == NULL && !m_target_triple.empty())
Greg Clayton38d880a2012-11-16 21:35:22 +0000630 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), getTargetOptions()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631 return m_target_info_ap.get();
632}
633
634#pragma mark Basic Types
635
636static inline bool
Greg Clayton6beaaa62011-01-17 03:46:26 +0000637QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000639 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640 if (qual_type_bit_size == bit_size)
641 return true;
642 return false;
643}
644
Greg Clayton1be10fc2010-09-29 01:12:09 +0000645clang_type_t
Greg Claytonc86103d2010-08-05 01:57:25 +0000646ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000648 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000649
Greg Clayton6beaaa62011-01-17 03:46:26 +0000650 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651
Greg Clayton6beaaa62011-01-17 03:46:26 +0000652 return GetBuiltinTypeForEncodingAndBitSize (ast, encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653}
654
Greg Clayton1be10fc2010-09-29 01:12:09 +0000655clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000656ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000658 if (!ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659 return NULL;
660
661 switch (encoding)
662 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000663 case eEncodingInvalid:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000664 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
665 return ast->VoidPtrTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666 break;
667
Greg Claytonc86103d2010-08-05 01:57:25 +0000668 case eEncodingUint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000669 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
670 return ast->UnsignedCharTy.getAsOpaquePtr();
671 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
672 return ast->UnsignedShortTy.getAsOpaquePtr();
673 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
674 return ast->UnsignedIntTy.getAsOpaquePtr();
675 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
676 return ast->UnsignedLongTy.getAsOpaquePtr();
677 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
678 return ast->UnsignedLongLongTy.getAsOpaquePtr();
679 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
680 return ast->UnsignedInt128Ty.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000681 break;
682
Greg Claytonc86103d2010-08-05 01:57:25 +0000683 case eEncodingSint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000684 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
685 return ast->CharTy.getAsOpaquePtr();
686 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
687 return ast->ShortTy.getAsOpaquePtr();
688 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
689 return ast->IntTy.getAsOpaquePtr();
690 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
691 return ast->LongTy.getAsOpaquePtr();
692 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
693 return ast->LongLongTy.getAsOpaquePtr();
694 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
695 return ast->Int128Ty.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696 break;
697
Greg Claytonc86103d2010-08-05 01:57:25 +0000698 case eEncodingIEEE754:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000699 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
700 return ast->FloatTy.getAsOpaquePtr();
701 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
702 return ast->DoubleTy.getAsOpaquePtr();
703 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
704 return ast->LongDoubleTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000705 break;
706
Greg Claytonc86103d2010-08-05 01:57:25 +0000707 case eEncodingVector:
Johnny Chenc79c93a2012-03-07 01:12:24 +0000708 // Sanity check that bit_size is a multiple of 8's.
709 if (bit_size && !(bit_size & 0x7u))
710 return ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8).getAsOpaquePtr();
711 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712 }
713
714 return NULL;
715}
716
Greg Clayton1be10fc2010-09-29 01:12:09 +0000717clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
719{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000720 ASTContext *ast = getASTContext();
Sean Callanan38d4df52012-04-03 01:10:10 +0000721
722#define streq(a,b) strcmp(a,b) == 0
Greg Clayton6beaaa62011-01-17 03:46:26 +0000723 assert (ast != NULL);
724 if (ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725 {
726 switch (dw_ate)
727 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000728 default:
729 break;
Greg Clayton605684e2011-10-28 23:06:08 +0000730
Sean Callanan38d4df52012-04-03 01:10:10 +0000731 case DW_ATE_address:
732 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
733 return ast->VoidPtrTy.getAsOpaquePtr();
734 break;
735
736 case DW_ATE_boolean:
737 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
738 return ast->BoolTy.getAsOpaquePtr();
739 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
740 return ast->UnsignedCharTy.getAsOpaquePtr();
741 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
742 return ast->UnsignedShortTy.getAsOpaquePtr();
743 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
744 return ast->UnsignedIntTy.getAsOpaquePtr();
745 break;
746
747 case DW_ATE_lo_user:
748 // This has been seen to mean DW_AT_complex_integer
749 if (type_name)
Greg Clayton605684e2011-10-28 23:06:08 +0000750 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000751 if (::strstr(type_name, "complex"))
752 {
753 clang_type_t complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2);
754 return ast->getComplexType (QualType::getFromOpaquePtr(complex_int_clang_type)).getAsOpaquePtr();
755 }
Greg Clayton605684e2011-10-28 23:06:08 +0000756 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000757 break;
758
759 case DW_ATE_complex_float:
760 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
761 return ast->FloatComplexTy.getAsOpaquePtr();
762 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
763 return ast->DoubleComplexTy.getAsOpaquePtr();
764 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
765 return ast->LongDoubleComplexTy.getAsOpaquePtr();
766 else
Greg Clayton605684e2011-10-28 23:06:08 +0000767 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000768 clang_type_t complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2);
769 return ast->getComplexType (QualType::getFromOpaquePtr(complex_float_clang_type)).getAsOpaquePtr();
Greg Clayton605684e2011-10-28 23:06:08 +0000770 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000771 break;
772
773 case DW_ATE_float:
774 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
775 return ast->FloatTy.getAsOpaquePtr();
776 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
777 return ast->DoubleTy.getAsOpaquePtr();
778 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
779 return ast->LongDoubleTy.getAsOpaquePtr();
780 break;
781
782 case DW_ATE_signed:
783 if (type_name)
784 {
785 if (streq(type_name, "wchar_t") &&
786 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
787 return ast->WCharTy.getAsOpaquePtr();
788 if (streq(type_name, "void") &&
789 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
790 return ast->VoidTy.getAsOpaquePtr();
791 if (strstr(type_name, "long long") &&
792 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
793 return ast->LongLongTy.getAsOpaquePtr();
794 if (strstr(type_name, "long") &&
795 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
796 return ast->LongTy.getAsOpaquePtr();
797 if (strstr(type_name, "short") &&
798 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
799 return ast->ShortTy.getAsOpaquePtr();
800 if (strstr(type_name, "char"))
801 {
802 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
803 return ast->CharTy.getAsOpaquePtr();
804 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
805 return ast->SignedCharTy.getAsOpaquePtr();
806 }
807 if (strstr(type_name, "int"))
808 {
809 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
810 return ast->IntTy.getAsOpaquePtr();
811 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
812 return ast->Int128Ty.getAsOpaquePtr();
813 }
814 }
815 // We weren't able to match up a type name, just search by size
816 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
817 return ast->CharTy.getAsOpaquePtr();
818 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
819 return ast->ShortTy.getAsOpaquePtr();
820 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
821 return ast->IntTy.getAsOpaquePtr();
822 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
823 return ast->LongTy.getAsOpaquePtr();
824 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
825 return ast->LongLongTy.getAsOpaquePtr();
826 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
827 return ast->Int128Ty.getAsOpaquePtr();
828 break;
829
830 case DW_ATE_signed_char:
831 if (type_name)
832 {
833 if (streq(type_name, "signed char"))
834 {
835 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
836 return ast->SignedCharTy.getAsOpaquePtr();
837 }
838 }
839 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
840 return ast->CharTy.getAsOpaquePtr();
841 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
842 return ast->SignedCharTy.getAsOpaquePtr();
843 break;
844
845 case DW_ATE_unsigned:
846 if (type_name)
847 {
848 if (strstr(type_name, "long long"))
849 {
850 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
851 return ast->UnsignedLongLongTy.getAsOpaquePtr();
852 }
853 else if (strstr(type_name, "long"))
854 {
855 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
856 return ast->UnsignedLongTy.getAsOpaquePtr();
857 }
858 else if (strstr(type_name, "short"))
859 {
860 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
861 return ast->UnsignedShortTy.getAsOpaquePtr();
862 }
863 else if (strstr(type_name, "char"))
864 {
865 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
866 return ast->UnsignedCharTy.getAsOpaquePtr();
867 }
868 else if (strstr(type_name, "int"))
869 {
870 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
871 return ast->UnsignedIntTy.getAsOpaquePtr();
872 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
873 return ast->UnsignedInt128Ty.getAsOpaquePtr();
874 }
875 }
876 // We weren't able to match up a type name, just search by size
877 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
878 return ast->UnsignedCharTy.getAsOpaquePtr();
879 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
880 return ast->UnsignedShortTy.getAsOpaquePtr();
881 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
882 return ast->UnsignedIntTy.getAsOpaquePtr();
883 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
884 return ast->UnsignedLongTy.getAsOpaquePtr();
885 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
886 return ast->UnsignedLongLongTy.getAsOpaquePtr();
887 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
888 return ast->UnsignedInt128Ty.getAsOpaquePtr();
889 break;
890
891 case DW_ATE_unsigned_char:
892 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
893 return ast->UnsignedCharTy.getAsOpaquePtr();
894 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
895 return ast->UnsignedShortTy.getAsOpaquePtr();
896 break;
897
898 case DW_ATE_imaginary_float:
899 break;
900
901 case DW_ATE_UTF:
902 if (type_name)
903 {
904 if (streq(type_name, "char16_t"))
905 {
906 return ast->Char16Ty.getAsOpaquePtr();
907 }
908 else if (streq(type_name, "char32_t"))
909 {
910 return ast->Char32Ty.getAsOpaquePtr();
911 }
912 }
913 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000914 }
915 }
916 // This assert should fire for anything that we don't catch above so we know
917 // to fix any issues we run into.
Greg Claytondc968d12011-05-17 18:15:05 +0000918 if (type_name)
919 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000920 Host::SystemLog (Host::eSystemLogError, "error: need to add support for DW_TAG_base_type '%s' encoded with DW_ATE = 0x%x, bit_size = %u\n", type_name, dw_ate, bit_size);
Greg Claytondc968d12011-05-17 18:15:05 +0000921 }
922 else
923 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000924 Host::SystemLog (Host::eSystemLogError, "error: need to add support for DW_TAG_base_type encoded with DW_ATE = 0x%x, bit_size = %u\n", dw_ate, bit_size);
Greg Claytondc968d12011-05-17 18:15:05 +0000925 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000926 return NULL;
927}
928
Greg Clayton1be10fc2010-09-29 01:12:09 +0000929clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000930ClangASTContext::GetBuiltInType_void(ASTContext *ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000931{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000932 return ast->VoidTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000933}
934
Greg Clayton1be10fc2010-09-29 01:12:09 +0000935clang_type_t
Sean Callananf7c3e272010-11-19 02:52:21 +0000936ClangASTContext::GetBuiltInType_bool()
937{
938 return getASTContext()->BoolTy.getAsOpaquePtr();
939}
940
941clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000942ClangASTContext::GetBuiltInType_objc_id()
943{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000944 return getASTContext()->getObjCIdType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000945}
946
Greg Clayton1be10fc2010-09-29 01:12:09 +0000947clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000948ClangASTContext::GetBuiltInType_objc_Class()
949{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000950 return getASTContext()->getObjCClassType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000951}
952
Greg Clayton1be10fc2010-09-29 01:12:09 +0000953clang_type_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000954ClangASTContext::GetBuiltInType_objc_selector()
955{
Sean Callanand5c17ed2011-11-15 02:11:17 +0000956 return getASTContext()->getObjCSelType().getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000957}
958
Greg Clayton1be10fc2010-09-29 01:12:09 +0000959clang_type_t
Sean Callanan77502262011-05-12 23:54:16 +0000960ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast)
961{
962 return ast->UnknownAnyTy.getAsOpaquePtr();
963}
964
965clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000966ClangASTContext::GetCStringType (bool is_const)
967{
968 QualType char_type(getASTContext()->CharTy);
969
970 if (is_const)
971 char_type.addConst();
972
973 return getASTContext()->getPointerType(char_type).getAsOpaquePtr();
974}
975
Greg Clayton1be10fc2010-09-29 01:12:09 +0000976clang_type_t
Sean Callanana6582262012-04-05 00:12:52 +0000977ClangASTContext::GetVoidType()
978{
979 return GetVoidType(getASTContext());
980}
981
982clang_type_t
983ClangASTContext::GetVoidType(ASTContext *ast)
984{
985 return ast->VoidTy.getAsOpaquePtr();
986}
987
988clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000989ClangASTContext::GetVoidPtrType (bool is_const)
990{
991 return GetVoidPtrType(getASTContext(), is_const);
992}
993
Greg Clayton1be10fc2010-09-29 01:12:09 +0000994clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000995ClangASTContext::GetVoidPtrType (ASTContext *ast, bool is_const)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000997 QualType void_ptr_type(ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000998
999 if (is_const)
1000 void_ptr_type.addConst();
1001
1002 return void_ptr_type.getAsOpaquePtr();
1003}
1004
Sean Callanan09ab4b72011-11-30 22:11:59 +00001005clang::DeclContext *
1006ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast)
1007{
1008 return ast->getTranslationUnitDecl();
1009}
1010
Greg Clayton1be10fc2010-09-29 01:12:09 +00001011clang_type_t
Greg Clayton38a61402010-12-02 23:20:03 +00001012ClangASTContext::CopyType (ASTContext *dst_ast,
1013 ASTContext *src_ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001014 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015{
Sean Callanan79439e82010-11-18 02:56:27 +00001016 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001017 FileManager file_manager (file_system_options);
1018 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001019 *src_ast, file_manager,
1020 false);
Sean Callanan0617fcb2010-11-09 22:37:10 +00001021
Greg Clayton38a61402010-12-02 23:20:03 +00001022 QualType src (QualType::getFromOpaquePtr(clang_type));
1023 QualType dst (importer.Import(src));
Sean Callanan0617fcb2010-11-09 22:37:10 +00001024
1025 return dst.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026}
1027
Greg Clayton526e5af2010-11-13 03:52:47 +00001028
1029clang::Decl *
Greg Clayton38a61402010-12-02 23:20:03 +00001030ClangASTContext::CopyDecl (ASTContext *dst_ast,
1031 ASTContext *src_ast,
Greg Clayton526e5af2010-11-13 03:52:47 +00001032 clang::Decl *source_decl)
Sean Callanan7fddd4c2010-12-11 00:08:56 +00001033{
Sean Callanan79439e82010-11-18 02:56:27 +00001034 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001035 FileManager file_manager (file_system_options);
1036 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001037 *src_ast, file_manager,
1038 false);
Greg Clayton526e5af2010-11-13 03:52:47 +00001039
1040 return importer.Import(source_decl);
1041}
1042
Sean Callanan23a30272010-07-16 00:00:27 +00001043bool
Greg Clayton84db9102012-03-26 23:03:23 +00001044ClangASTContext::AreTypesSame (ASTContext *ast,
1045 clang_type_t type1,
1046 clang_type_t type2,
1047 bool ignore_qualifiers)
Sean Callanan4dcca2622010-07-15 22:30:52 +00001048{
Greg Clayton55995eb2012-04-06 17:38:55 +00001049 if (type1 == type2)
1050 return true;
1051
Sean Callanan5056ab02012-02-18 02:01:03 +00001052 QualType type1_qual = QualType::getFromOpaquePtr(type1);
1053 QualType type2_qual = QualType::getFromOpaquePtr(type2);
1054
1055 if (ignore_qualifiers)
1056 {
1057 type1_qual = type1_qual.getUnqualifiedType();
1058 type2_qual = type2_qual.getUnqualifiedType();
1059 }
1060
1061 return ast->hasSameType (type1_qual,
1062 type2_qual);
Sean Callanan4dcca2622010-07-15 22:30:52 +00001063}
1064
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001065#pragma mark CVR modifiers
1066
Greg Clayton1be10fc2010-09-29 01:12:09 +00001067clang_type_t
1068ClangASTContext::AddConstModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001069{
1070 if (clang_type)
1071 {
1072 QualType result(QualType::getFromOpaquePtr(clang_type));
1073 result.addConst();
1074 return result.getAsOpaquePtr();
1075 }
1076 return NULL;
1077}
1078
Greg Clayton1be10fc2010-09-29 01:12:09 +00001079clang_type_t
1080ClangASTContext::AddRestrictModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081{
1082 if (clang_type)
1083 {
1084 QualType result(QualType::getFromOpaquePtr(clang_type));
1085 result.getQualifiers().setRestrict (true);
1086 return result.getAsOpaquePtr();
1087 }
1088 return NULL;
1089}
1090
Greg Clayton1be10fc2010-09-29 01:12:09 +00001091clang_type_t
1092ClangASTContext::AddVolatileModifier (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001093{
1094 if (clang_type)
1095 {
1096 QualType result(QualType::getFromOpaquePtr(clang_type));
1097 result.getQualifiers().setVolatile (true);
1098 return result.getAsOpaquePtr();
1099 }
1100 return NULL;
1101}
1102
Greg Clayton6beaaa62011-01-17 03:46:26 +00001103
1104clang_type_t
1105ClangASTContext::GetTypeForDecl (TagDecl *decl)
1106{
1107 // No need to call the getASTContext() accessor (which can create the AST
1108 // if it isn't created yet, because we can't have created a decl in this
1109 // AST if our AST didn't already exist...
1110 if (m_ast_ap.get())
1111 return m_ast_ap->getTagDeclType(decl).getAsOpaquePtr();
1112 return NULL;
1113}
1114
1115clang_type_t
1116ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1117{
1118 // No need to call the getASTContext() accessor (which can create the AST
1119 // if it isn't created yet, because we can't have created a decl in this
1120 // AST if our AST didn't already exist...
1121 if (m_ast_ap.get())
1122 return m_ast_ap->getObjCInterfaceType(decl).getAsOpaquePtr();
1123 return NULL;
1124}
1125
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001126#pragma mark Structure, Unions, Classes
1127
Greg Clayton1be10fc2010-09-29 01:12:09 +00001128clang_type_t
Jim Ingham379397632012-10-27 02:54:13 +00001129ClangASTContext::CreateRecordType (DeclContext *decl_ctx, AccessType access_type, const char *name, int kind, LanguageType language, ClangASTMetadata *metadata)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001130{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001131 ASTContext *ast = getASTContext();
1132 assert (ast != NULL);
Sean Callananad880762012-04-18 01:06:17 +00001133
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001135 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001136
Greg Clayton9e409562010-07-28 02:04:09 +00001137
Greg Claytone1be9962011-08-24 23:50:00 +00001138 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
Greg Clayton9e409562010-07-28 02:04:09 +00001139 {
Greg Claytonaaf99e02010-10-11 02:25:34 +00001140 bool isForwardDecl = true;
Greg Clayton9e409562010-07-28 02:04:09 +00001141 bool isInternal = false;
Sean Callananad880762012-04-18 01:06:17 +00001142 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal, metadata);
Greg Clayton9e409562010-07-28 02:04:09 +00001143 }
1144
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001145 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1146 // we will need to update this code. I was told to currently always use
1147 // the CXXRecordDecl class since we often don't know from debug information
1148 // if something is struct or a class, so we default to always use the more
1149 // complete definition just in case.
Greg Claytonf0705c82011-10-22 03:33:13 +00001150 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1151 (TagDecl::TagKind)kind,
1152 decl_ctx,
1153 SourceLocation(),
1154 SourceLocation(),
1155 name && name[0] ? &ast->Idents.get(name) : NULL);
Sean Callanan7282e2a2012-01-13 22:10:18 +00001156
Jim Ingham379397632012-10-27 02:54:13 +00001157 if (decl && metadata)
1158 SetMetadata(ast, (uintptr_t)decl, *metadata);
Sean Callanan60217122012-04-13 00:10:03 +00001159
Greg Clayton55561e92011-10-26 03:31:36 +00001160 if (decl_ctx)
1161 {
1162 if (access_type != eAccessNone)
1163 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
1164 decl_ctx->addDecl (decl);
1165 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001166 return ast->getTagDeclType(decl).getAsOpaquePtr();
1167}
1168
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001169static TemplateParameterList *
1170CreateTemplateParameterList (ASTContext *ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001171 const ClangASTContext::TemplateParameterInfos &template_param_infos,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001172 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1173{
1174 const bool parameter_pack = false;
1175 const bool is_typename = false;
1176 const unsigned depth = 0;
1177 const size_t num_template_params = template_param_infos.GetSize();
1178 for (size_t i=0; i<num_template_params; ++i)
1179 {
1180 const char *name = template_param_infos.names[i];
Sean Callanan3d654b32012-09-24 22:25:51 +00001181 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001182 {
1183 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1184 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1185 SourceLocation(),
1186 SourceLocation(),
1187 depth,
1188 i,
1189 &ast->Idents.get(name),
1190 template_param_infos.args[i].getIntegralType(),
1191 parameter_pack,
1192 NULL));
1193
1194 }
1195 else
1196 {
1197 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1198 ast->getTranslationUnitDecl(), // Is this the right decl context?
1199 SourceLocation(),
1200 SourceLocation(),
1201 depth,
1202 i,
1203 &ast->Idents.get(name),
1204 is_typename,
1205 parameter_pack));
1206 }
1207 }
1208
1209 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1210 SourceLocation(),
1211 SourceLocation(),
1212 &template_param_decls.front(),
1213 template_param_decls.size(),
1214 SourceLocation());
1215 return template_param_list;
1216}
1217
1218clang::FunctionTemplateDecl *
1219ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1220 clang::FunctionDecl *func_decl,
1221 const char *name,
1222 const TemplateParameterInfos &template_param_infos)
1223{
1224// /// \brief Create a function template node.
1225 ASTContext *ast = getASTContext();
1226
1227 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1228
1229 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1230 template_param_infos,
1231 template_param_decls);
1232 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1233 decl_ctx,
1234 func_decl->getLocation(),
1235 func_decl->getDeclName(),
1236 template_param_list,
1237 func_decl);
1238
1239 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1240 i < template_param_decl_count;
1241 ++i)
1242 {
1243 // TODO: verify which decl context we should put template_param_decls into..
1244 template_param_decls[i]->setDeclContext (func_decl);
1245 }
1246
1247 return func_tmpl_decl;
1248}
1249
1250void
1251ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1252 clang::FunctionTemplateDecl *func_tmpl_decl,
1253 const TemplateParameterInfos &infos)
1254{
1255 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1256 infos.args.data(),
1257 infos.args.size());
1258
1259 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1260 &template_args,
1261 NULL);
1262}
1263
1264
Greg Claytonf0705c82011-10-22 03:33:13 +00001265ClassTemplateDecl *
1266ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001267 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001268 const char *class_name,
1269 int kind,
1270 const TemplateParameterInfos &template_param_infos)
1271{
1272 ASTContext *ast = getASTContext();
1273
1274 ClassTemplateDecl *class_template_decl = NULL;
1275 if (decl_ctx == NULL)
1276 decl_ctx = ast->getTranslationUnitDecl();
1277
1278 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1279 DeclarationName decl_name (&identifier_info);
1280
1281 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001282
1283 for (NamedDecl *decl : result)
Greg Claytonf0705c82011-10-22 03:33:13 +00001284 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001285 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001286 if (class_template_decl)
1287 return class_template_decl;
1288 }
1289
1290 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Claytonf0705c82011-10-22 03:33:13 +00001291
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001292 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1293 template_param_infos,
1294 template_param_decls);
Greg Claytonf0705c82011-10-22 03:33:13 +00001295
1296 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1297 (TagDecl::TagKind)kind,
1298 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1299 SourceLocation(),
1300 SourceLocation(),
1301 &identifier_info);
Greg Claytone04741d2011-12-02 02:09:28 +00001302
1303 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1304 i < template_param_decl_count;
1305 ++i)
1306 {
1307 template_param_decls[i]->setDeclContext (template_cxx_decl);
1308 }
1309
Sean Callananb5c79622011-11-19 01:35:08 +00001310 // With templated classes, we say that a class is templated with
1311 // specializations, but that the bare class has no functions.
1312 template_cxx_decl->startDefinition();
1313 template_cxx_decl->completeDefinition();
1314
Greg Claytonf0705c82011-10-22 03:33:13 +00001315 class_template_decl = ClassTemplateDecl::Create (*ast,
1316 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1317 SourceLocation(),
1318 decl_name,
1319 template_param_list,
1320 template_cxx_decl,
1321 NULL);
1322
1323 if (class_template_decl)
Sean Callanan5e9e1992011-10-26 01:06:27 +00001324 {
Greg Clayton55561e92011-10-26 03:31:36 +00001325 if (access_type != eAccessNone)
1326 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Sean Callanan5b26f272012-02-04 08:49:35 +00001327
1328 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1329 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1330
Greg Claytonf0705c82011-10-22 03:33:13 +00001331 decl_ctx->addDecl (class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001332
1333#ifdef LLDB_CONFIGURATION_DEBUG
1334 VerifyDecl(class_template_decl);
1335#endif
1336 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001337
1338 return class_template_decl;
1339}
1340
1341
1342ClassTemplateSpecializationDecl *
1343ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1344 ClassTemplateDecl *class_template_decl,
1345 int kind,
1346 const TemplateParameterInfos &template_param_infos)
1347{
1348 ASTContext *ast = getASTContext();
1349 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1350 (TagDecl::TagKind)kind,
1351 decl_ctx,
1352 SourceLocation(),
1353 SourceLocation(),
1354 class_template_decl,
1355 &template_param_infos.args.front(),
1356 template_param_infos.args.size(),
1357 NULL);
1358
1359 return class_template_specialization_decl;
1360}
1361
1362lldb::clang_type_t
1363ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1364{
1365 if (class_template_specialization_decl)
1366 {
1367 ASTContext *ast = getASTContext();
1368 if (ast)
1369 return ast->getTagDeclType(class_template_specialization_decl).getAsOpaquePtr();
1370 }
1371 return NULL;
1372}
1373
Greg Clayton6beaaa62011-01-17 03:46:26 +00001374bool
1375ClangASTContext::SetHasExternalStorage (clang_type_t clang_type, bool has_extern)
1376{
1377 if (clang_type == NULL)
1378 return false;
1379
1380 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
1381
1382 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
1383 switch (type_class)
1384 {
1385 case clang::Type::Record:
1386 {
1387 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
1388 if (cxx_record_decl)
1389 {
1390 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001391 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001392 return true;
1393 }
1394 }
1395 break;
1396
1397 case clang::Type::Enum:
1398 {
1399 EnumDecl *enum_decl = cast<EnumType>(qual_type)->getDecl();
1400 if (enum_decl)
1401 {
1402 enum_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001403 enum_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001404 return true;
1405 }
1406 }
1407 break;
1408
1409 case clang::Type::ObjCObject:
1410 case clang::Type::ObjCInterface:
1411 {
Sean Callanan78e37602011-01-27 04:42:51 +00001412 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001413 assert (objc_class_type);
1414 if (objc_class_type)
1415 {
1416 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1417
1418 if (class_interface_decl)
1419 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001420 class_interface_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001421 class_interface_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001422 return true;
1423 }
1424 }
1425 }
1426 break;
1427
1428 case clang::Type::Typedef:
1429 return ClangASTContext::SetHasExternalStorage (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
Sean Callanan912855f2011-08-11 23:56:13 +00001430
1431 case clang::Type::Elaborated:
1432 return ClangASTContext::SetHasExternalStorage (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001433
1434 default:
1435 break;
1436 }
1437 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001438}
1439
Greg Claytona3c444a2010-10-01 23:13:49 +00001440static bool
1441IsOperator (const char *name, OverloadedOperatorKind &op_kind)
1442{
1443 if (name == NULL || name[0] == '\0')
1444 return false;
1445
Sean Callanana43f20d2010-12-10 19:51:54 +00001446#define OPERATOR_PREFIX "operator"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001447#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
Sean Callananbfeff8c2010-12-10 02:15:55 +00001448
1449 const char *post_op_name = NULL;
1450
Sean Callanana43f20d2010-12-10 19:51:54 +00001451 bool no_space = true;
Sean Callananbfeff8c2010-12-10 02:15:55 +00001452
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001453 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
Greg Claytona3c444a2010-10-01 23:13:49 +00001454 return false;
1455
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001456 post_op_name = name + OPERATOR_PREFIX_LENGTH;
1457
Sean Callanana43f20d2010-12-10 19:51:54 +00001458 if (post_op_name[0] == ' ')
1459 {
1460 post_op_name++;
1461 no_space = false;
1462 }
1463
1464#undef OPERATOR_PREFIX
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001465#undef OPERATOR_PREFIX_LENGTH
Sean Callanana43f20d2010-12-10 19:51:54 +00001466
Greg Claytona3c444a2010-10-01 23:13:49 +00001467 // This is an operator, set the overloaded operator kind to invalid
1468 // in case this is a conversion operator...
1469 op_kind = NUM_OVERLOADED_OPERATORS;
1470
1471 switch (post_op_name[0])
1472 {
Sean Callananbfeff8c2010-12-10 02:15:55 +00001473 default:
1474 if (no_space)
1475 return false;
1476 break;
Greg Claytona3c444a2010-10-01 23:13:49 +00001477 case 'n':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001478 if (no_space)
1479 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001480 if (strcmp (post_op_name, "new") == 0)
1481 op_kind = OO_New;
1482 else if (strcmp (post_op_name, "new[]") == 0)
1483 op_kind = OO_Array_New;
1484 break;
1485
1486 case 'd':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001487 if (no_space)
1488 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001489 if (strcmp (post_op_name, "delete") == 0)
1490 op_kind = OO_Delete;
1491 else if (strcmp (post_op_name, "delete[]") == 0)
1492 op_kind = OO_Array_Delete;
1493 break;
1494
1495 case '+':
1496 if (post_op_name[1] == '\0')
1497 op_kind = OO_Plus;
1498 else if (post_op_name[2] == '\0')
1499 {
1500 if (post_op_name[1] == '=')
1501 op_kind = OO_PlusEqual;
1502 else if (post_op_name[1] == '+')
1503 op_kind = OO_PlusPlus;
1504 }
1505 break;
1506
1507 case '-':
1508 if (post_op_name[1] == '\0')
1509 op_kind = OO_Minus;
1510 else if (post_op_name[2] == '\0')
1511 {
1512 switch (post_op_name[1])
1513 {
1514 case '=': op_kind = OO_MinusEqual; break;
1515 case '-': op_kind = OO_MinusMinus; break;
1516 case '>': op_kind = OO_Arrow; break;
1517 }
1518 }
1519 else if (post_op_name[3] == '\0')
1520 {
1521 if (post_op_name[2] == '*')
1522 op_kind = OO_ArrowStar; break;
1523 }
1524 break;
1525
1526 case '*':
1527 if (post_op_name[1] == '\0')
1528 op_kind = OO_Star;
1529 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1530 op_kind = OO_StarEqual;
1531 break;
1532
1533 case '/':
1534 if (post_op_name[1] == '\0')
1535 op_kind = OO_Slash;
1536 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1537 op_kind = OO_SlashEqual;
1538 break;
1539
1540 case '%':
1541 if (post_op_name[1] == '\0')
1542 op_kind = OO_Percent;
1543 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1544 op_kind = OO_PercentEqual;
1545 break;
1546
1547
1548 case '^':
1549 if (post_op_name[1] == '\0')
1550 op_kind = OO_Caret;
1551 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1552 op_kind = OO_CaretEqual;
1553 break;
1554
1555 case '&':
1556 if (post_op_name[1] == '\0')
1557 op_kind = OO_Amp;
1558 else if (post_op_name[2] == '\0')
1559 {
1560 switch (post_op_name[1])
1561 {
1562 case '=': op_kind = OO_AmpEqual; break;
1563 case '&': op_kind = OO_AmpAmp; break;
1564 }
1565 }
1566 break;
1567
1568 case '|':
1569 if (post_op_name[1] == '\0')
1570 op_kind = OO_Pipe;
1571 else if (post_op_name[2] == '\0')
1572 {
1573 switch (post_op_name[1])
1574 {
1575 case '=': op_kind = OO_PipeEqual; break;
1576 case '|': op_kind = OO_PipePipe; break;
1577 }
1578 }
1579 break;
1580
1581 case '~':
1582 if (post_op_name[1] == '\0')
1583 op_kind = OO_Tilde;
1584 break;
1585
1586 case '!':
1587 if (post_op_name[1] == '\0')
1588 op_kind = OO_Exclaim;
1589 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1590 op_kind = OO_ExclaimEqual;
1591 break;
1592
1593 case '=':
1594 if (post_op_name[1] == '\0')
1595 op_kind = OO_Equal;
1596 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1597 op_kind = OO_EqualEqual;
1598 break;
1599
1600 case '<':
1601 if (post_op_name[1] == '\0')
1602 op_kind = OO_Less;
1603 else if (post_op_name[2] == '\0')
1604 {
1605 switch (post_op_name[1])
1606 {
1607 case '<': op_kind = OO_LessLess; break;
1608 case '=': op_kind = OO_LessEqual; break;
1609 }
1610 }
1611 else if (post_op_name[3] == '\0')
1612 {
1613 if (post_op_name[2] == '=')
1614 op_kind = OO_LessLessEqual;
1615 }
1616 break;
1617
1618 case '>':
1619 if (post_op_name[1] == '\0')
1620 op_kind = OO_Greater;
1621 else if (post_op_name[2] == '\0')
1622 {
1623 switch (post_op_name[1])
1624 {
1625 case '>': op_kind = OO_GreaterGreater; break;
1626 case '=': op_kind = OO_GreaterEqual; break;
1627 }
1628 }
1629 else if (post_op_name[1] == '>' &&
1630 post_op_name[2] == '=' &&
1631 post_op_name[3] == '\0')
1632 {
1633 op_kind = OO_GreaterGreaterEqual;
1634 }
1635 break;
1636
1637 case ',':
1638 if (post_op_name[1] == '\0')
1639 op_kind = OO_Comma;
1640 break;
1641
1642 case '(':
1643 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
1644 op_kind = OO_Call;
1645 break;
1646
1647 case '[':
1648 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
1649 op_kind = OO_Subscript;
1650 break;
1651 }
1652
1653 return true;
1654}
Greg Clayton6beaaa62011-01-17 03:46:26 +00001655
Greg Clayton090d0982011-06-19 03:43:27 +00001656static inline bool
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001657check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
Greg Clayton090d0982011-06-19 03:43:27 +00001658{
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001659 // Special-case call since it can take any number of operands
1660 if(op_kind == OO_Call)
1661 return true;
1662
Greg Clayton090d0982011-06-19 03:43:27 +00001663 // The parameter count doens't include "this"
1664 if (num_params == 0)
1665 return unary;
1666 if (num_params == 1)
1667 return binary;
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001668 else
Greg Clayton090d0982011-06-19 03:43:27 +00001669 return false;
1670}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001671
Greg Clayton090d0982011-06-19 03:43:27 +00001672bool
1673ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1674{
Sean Callanan5b26f272012-02-04 08:49:35 +00001675 switch (op_kind)
1676 {
1677 default:
1678 break;
1679 // C++ standard allows any number of arguments to new/delete
1680 case OO_New:
1681 case OO_Array_New:
1682 case OO_Delete:
1683 case OO_Array_Delete:
1684 return true;
1685 }
1686
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001687#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 +00001688 switch (op_kind)
1689 {
1690#include "clang/Basic/OperatorKinds.def"
1691 default: break;
1692 }
1693 return false;
1694}
1695
Greg Claytona51ed9b2010-09-23 01:09:21 +00001696CXXMethodDecl *
Sean Callanan61da09b2010-09-17 02:58:26 +00001697ClangASTContext::AddMethodToCXXRecordType
1698(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001699 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001700 clang_type_t record_opaque_type,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001701 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001702 clang_type_t method_opaque_type,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001703 lldb::AccessType access,
Greg Clayton0fffff52010-09-24 05:15:53 +00001704 bool is_virtual,
1705 bool is_static,
Greg Claytonf51de672010-10-01 02:31:07 +00001706 bool is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00001707 bool is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00001708 bool is_attr_used,
1709 bool is_artificial
Greg Claytona51ed9b2010-09-23 01:09:21 +00001710)
Sean Callanan61da09b2010-09-17 02:58:26 +00001711{
Sean Callananfc55f5d2010-09-21 00:44:12 +00001712 if (!record_opaque_type || !method_opaque_type || !name)
Johnny Chend440bcc2010-09-28 16:10:54 +00001713 return NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001714
Greg Clayton6beaaa62011-01-17 03:46:26 +00001715 assert(ast);
Sean Callanan61da09b2010-09-17 02:58:26 +00001716
Greg Clayton6beaaa62011-01-17 03:46:26 +00001717 IdentifierTable *identifier_table = &ast->Idents;
Sean Callanan61da09b2010-09-17 02:58:26 +00001718
1719 assert(identifier_table);
1720
Sean Callananfc55f5d2010-09-21 00:44:12 +00001721 QualType record_qual_type(QualType::getFromOpaquePtr(record_opaque_type));
Greg Clayton0fffff52010-09-24 05:15:53 +00001722
Greg Clayton6beaaa62011-01-17 03:46:26 +00001723 CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
Sean Callanan61da09b2010-09-17 02:58:26 +00001724
Greg Clayton0fffff52010-09-24 05:15:53 +00001725 if (cxx_record_decl == NULL)
Greg Claytona51ed9b2010-09-23 01:09:21 +00001726 return NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001727
Greg Clayton0fffff52010-09-24 05:15:53 +00001728 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001729
Greg Claytonf51de672010-10-01 02:31:07 +00001730 CXXMethodDecl *cxx_method_decl = NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001731
Greg Claytonf51de672010-10-01 02:31:07 +00001732 DeclarationName decl_name (&identifier_table->get(name));
Greg Clayton878eaf12010-10-01 03:45:20 +00001733
Sean Callanan78e37602011-01-27 04:42:51 +00001734 const clang::FunctionType *function_Type = dyn_cast<FunctionType>(method_qual_type.getTypePtr());
Greg Clayton878eaf12010-10-01 03:45:20 +00001735
Greg Clayton90a2acd2010-10-02 01:40:05 +00001736 if (function_Type == NULL)
Greg Clayton878eaf12010-10-01 03:45:20 +00001737 return NULL;
1738
Sean Callanan78e37602011-01-27 04:42:51 +00001739 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(function_Type));
Greg Clayton878eaf12010-10-01 03:45:20 +00001740
1741 if (!method_function_prototype)
1742 return NULL;
1743
1744 unsigned int num_params = method_function_prototype->getNumArgs();
1745
Sean Callanandbb58392011-11-02 01:38:59 +00001746 CXXDestructorDecl *cxx_dtor_decl(NULL);
1747 CXXConstructorDecl *cxx_ctor_decl(NULL);
1748
Greg Clayton878eaf12010-10-01 03:45:20 +00001749 if (name[0] == '~')
Greg Claytonf51de672010-10-01 02:31:07 +00001750 {
Sean Callanandbb58392011-11-02 01:38:59 +00001751 cxx_dtor_decl = CXXDestructorDecl::Create (*ast,
1752 cxx_record_decl,
1753 SourceLocation(),
1754 DeclarationNameInfo (ast->DeclarationNames.getCXXDestructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1755 method_qual_type,
1756 NULL,
1757 is_inline,
1758 is_artificial);
1759 cxx_method_decl = cxx_dtor_decl;
Greg Clayton878eaf12010-10-01 03:45:20 +00001760 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001761 else if (decl_name == cxx_record_decl->getDeclName())
Greg Clayton878eaf12010-10-01 03:45:20 +00001762 {
Sean Callanandbb58392011-11-02 01:38:59 +00001763 cxx_ctor_decl = CXXConstructorDecl::Create (*ast,
1764 cxx_record_decl,
1765 SourceLocation(),
1766 DeclarationNameInfo (ast->DeclarationNames.getCXXConstructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1767 method_qual_type,
1768 NULL, // TypeSourceInfo *
1769 is_explicit,
1770 is_inline,
1771 is_artificial,
1772 false /*is_constexpr*/);
1773 cxx_method_decl = cxx_ctor_decl;
Greg Claytonf51de672010-10-01 02:31:07 +00001774 }
1775 else
Greg Clayton878eaf12010-10-01 03:45:20 +00001776 {
Greg Claytona3c444a2010-10-01 23:13:49 +00001777
1778 OverloadedOperatorKind op_kind = NUM_OVERLOADED_OPERATORS;
1779 if (IsOperator (name, op_kind))
Greg Clayton878eaf12010-10-01 03:45:20 +00001780 {
Greg Claytona3c444a2010-10-01 23:13:49 +00001781 if (op_kind != NUM_OVERLOADED_OPERATORS)
1782 {
Greg Clayton090d0982011-06-19 03:43:27 +00001783 // Check the number of operator parameters. Sometimes we have
1784 // seen bad DWARF that doesn't correctly describe operators and
1785 // if we try to create a methed and add it to the class, clang
1786 // will assert and crash, so we need to make sure things are
1787 // acceptable.
1788 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
1789 return NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001790 cxx_method_decl = CXXMethodDecl::Create (*ast,
Greg Clayton878eaf12010-10-01 03:45:20 +00001791 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001792 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001793 DeclarationNameInfo (ast->DeclarationNames.getCXXOperatorName (op_kind), SourceLocation()),
Greg Clayton878eaf12010-10-01 03:45:20 +00001794 method_qual_type,
1795 NULL, // TypeSourceInfo *
Greg Claytona3c444a2010-10-01 23:13:49 +00001796 is_static,
1797 SC_None,
Sean Callananfb0b7582011-03-15 00:17:19 +00001798 is_inline,
Sean Callanan880e6802011-10-07 23:18:13 +00001799 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001800 SourceLocation());
Greg Claytona3c444a2010-10-01 23:13:49 +00001801 }
1802 else if (num_params == 0)
1803 {
1804 // Conversion operators don't take params...
Greg Clayton6beaaa62011-01-17 03:46:26 +00001805 cxx_method_decl = CXXConversionDecl::Create (*ast,
Greg Claytona3c444a2010-10-01 23:13:49 +00001806 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001807 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001808 DeclarationNameInfo (ast->DeclarationNames.getCXXConversionFunctionName (ast->getCanonicalType (function_Type->getResultType())), SourceLocation()),
Greg Claytona3c444a2010-10-01 23:13:49 +00001809 method_qual_type,
1810 NULL, // TypeSourceInfo *
1811 is_inline,
Sean Callananfb0b7582011-03-15 00:17:19 +00001812 is_explicit,
Sean Callanan880e6802011-10-07 23:18:13 +00001813 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001814 SourceLocation());
Greg Claytona3c444a2010-10-01 23:13:49 +00001815 }
Greg Clayton878eaf12010-10-01 03:45:20 +00001816 }
Greg Claytona3c444a2010-10-01 23:13:49 +00001817
1818 if (cxx_method_decl == NULL)
Greg Clayton878eaf12010-10-01 03:45:20 +00001819 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001820 cxx_method_decl = CXXMethodDecl::Create (*ast,
Greg Clayton878eaf12010-10-01 03:45:20 +00001821 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001822 SourceLocation(),
Greg Claytona3c444a2010-10-01 23:13:49 +00001823 DeclarationNameInfo (decl_name, SourceLocation()),
Greg Clayton878eaf12010-10-01 03:45:20 +00001824 method_qual_type,
1825 NULL, // TypeSourceInfo *
1826 is_static,
1827 SC_None,
Sean Callananfb0b7582011-03-15 00:17:19 +00001828 is_inline,
Sean Callanan880e6802011-10-07 23:18:13 +00001829 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001830 SourceLocation());
Greg Clayton878eaf12010-10-01 03:45:20 +00001831 }
Greg Claytonf51de672010-10-01 02:31:07 +00001832 }
Greg Claytona3c444a2010-10-01 23:13:49 +00001833
Greg Clayton1be10fc2010-09-29 01:12:09 +00001834 AccessSpecifier access_specifier = ConvertAccessTypeToAccessSpecifier (access);
Greg Clayton0fffff52010-09-24 05:15:53 +00001835
1836 cxx_method_decl->setAccess (access_specifier);
1837 cxx_method_decl->setVirtualAsWritten (is_virtual);
Sean Callanane2ef6e32010-09-23 03:01:22 +00001838
Sean Callananc1b732d2011-11-01 18:07:13 +00001839 if (is_attr_used)
1840 cxx_method_decl->addAttr(::new (*ast) UsedAttr(SourceRange(), *ast));
1841
Sean Callananfc55f5d2010-09-21 00:44:12 +00001842 // Populate the method decl with parameter decls
Sean Callananfc55f5d2010-09-21 00:44:12 +00001843
Charles Davis8c444c42011-05-19 23:33:46 +00001844 llvm::SmallVector<ParmVarDecl *, 12> params;
Sean Callananfc55f5d2010-09-21 00:44:12 +00001845
1846 for (int param_index = 0;
1847 param_index < num_params;
1848 ++param_index)
1849 {
Charles Davis8c444c42011-05-19 23:33:46 +00001850 params.push_back (ParmVarDecl::Create (*ast,
1851 cxx_method_decl,
1852 SourceLocation(),
1853 SourceLocation(),
1854 NULL, // anonymous
1855 method_function_prototype->getArgType(param_index),
1856 NULL,
1857 SC_None,
1858 SC_None,
1859 NULL));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001860 }
1861
Sean Callanan880e6802011-10-07 23:18:13 +00001862 cxx_method_decl->setParams (ArrayRef<ParmVarDecl*>(params));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001863
Greg Clayton0fffff52010-09-24 05:15:53 +00001864 cxx_record_decl->addDecl (cxx_method_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001865
Greg Clayton8b867b42011-11-02 02:06:20 +00001866 // Sometimes the debug info will mention a constructor (default/copy/move),
1867 // destructor, or assignment operator (copy/move) but there won't be any
1868 // version of this in the code. So we check if the function was artificially
1869 // generated and if it is trivial and this lets the compiler/backend know
1870 // that it can inline the IR for these when it needs to and we can avoid a
1871 // "missing function" error when running expressions.
1872
Sean Callanandbb58392011-11-02 01:38:59 +00001873 if (is_artificial)
1874 {
Greg Clayton8b867b42011-11-02 02:06:20 +00001875 if (cxx_ctor_decl &&
1876 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
1877 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
1878 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
Sean Callanandbb58392011-11-02 01:38:59 +00001879 {
1880 cxx_ctor_decl->setDefaulted();
1881 cxx_ctor_decl->setTrivial(true);
1882 }
Greg Clayton8b867b42011-11-02 02:06:20 +00001883 else if (cxx_dtor_decl)
Sean Callanandbb58392011-11-02 01:38:59 +00001884 {
Greg Clayton8b867b42011-11-02 02:06:20 +00001885 if (cxx_record_decl->hasTrivialDestructor())
1886 {
1887 cxx_dtor_decl->setDefaulted();
1888 cxx_dtor_decl->setTrivial(true);
1889 }
1890 }
1891 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
1892 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
1893 {
1894 cxx_method_decl->setDefaulted();
1895 cxx_method_decl->setTrivial(true);
Sean Callanandbb58392011-11-02 01:38:59 +00001896 }
1897 }
1898
Sean Callanan5e9e1992011-10-26 01:06:27 +00001899#ifdef LLDB_CONFIGURATION_DEBUG
1900 VerifyDecl(cxx_method_decl);
1901#endif
Greg Claytonc432c192011-01-20 04:18:48 +00001902
1903// printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
1904// printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
1905// printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
1906// printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
1907// printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
1908// printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
1909// printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
1910// printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
1911// printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
Greg Claytona51ed9b2010-09-23 01:09:21 +00001912 return cxx_method_decl;
Sean Callanan61da09b2010-09-17 02:58:26 +00001913}
1914
Jim Inghame3ae82a2011-11-12 01:36:43 +00001915clang::FieldDecl *
Greg Clayton8cf05932010-07-22 18:30:50 +00001916ClangASTContext::AddFieldToRecordType
1917(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001918 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001919 clang_type_t record_clang_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00001920 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001921 clang_type_t field_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00001922 AccessType access,
1923 uint32_t bitfield_bit_size
1924)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001925{
1926 if (record_clang_type == NULL || field_type == NULL)
Jim Inghame3ae82a2011-11-12 01:36:43 +00001927 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001928
Jim Inghame3ae82a2011-11-12 01:36:43 +00001929 FieldDecl *field = NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001930 IdentifierTable *identifier_table = &ast->Idents;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001931
Greg Clayton6beaaa62011-01-17 03:46:26 +00001932 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001933 assert (identifier_table != NULL);
1934
1935 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
1936
Sean Callanan78e37602011-01-27 04:42:51 +00001937 const clang::Type *clang_type = record_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001938 if (clang_type)
1939 {
1940 const RecordType *record_type = dyn_cast<RecordType>(clang_type);
1941
1942 if (record_type)
1943 {
1944 RecordDecl *record_decl = record_type->getDecl();
1945
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001946 clang::Expr *bit_width = NULL;
1947 if (bitfield_bit_size != 0)
1948 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001949 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
1950 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001951 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001952 field = FieldDecl::Create (*ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001953 record_decl,
1954 SourceLocation(),
1955 SourceLocation(),
1956 name ? &identifier_table->get(name) : NULL, // Identifier
1957 QualType::getFromOpaquePtr(field_type), // Field type
1958 NULL, // TInfo *
1959 bit_width, // BitWidth
1960 false, // Mutable
1961 ICIS_NoInit); // HasInit
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001962
Sean Callanan5ed3ac12012-07-13 20:01:02 +00001963 if (!name) {
1964 // Determine whether this field corresponds to an anonymous
1965 // struct or union.
1966 if (const TagType *TagT = field->getType()->getAs<TagType>()) {
1967 if (RecordDecl *Rec = dyn_cast<RecordDecl>(TagT->getDecl()))
1968 if (!Rec->getDeclName()) {
1969 Rec->setAnonymousStructOrUnion(true);
1970 field->setImplicit();
1971
1972 }
1973 }
1974 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001975
Greg Clayton8cf05932010-07-22 18:30:50 +00001976 field->setAccess (ConvertAccessTypeToAccessSpecifier (access));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001977
1978 if (field)
1979 {
1980 record_decl->addDecl(field);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001981
1982#ifdef LLDB_CONFIGURATION_DEBUG
1983 VerifyDecl(field);
1984#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001985 }
1986 }
Greg Clayton9e409562010-07-28 02:04:09 +00001987 else
1988 {
Sean Callanan78e37602011-01-27 04:42:51 +00001989 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001990 if (objc_class_type)
1991 {
Greg Clayton0fffff52010-09-24 05:15:53 +00001992 bool is_synthesized = false;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001993 field = ClangASTContext::AddObjCClassIVar (ast,
Sean Callanan6e6a7c72010-09-16 20:01:08 +00001994 record_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001995 name,
1996 field_type,
1997 access,
1998 bitfield_bit_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00001999 is_synthesized);
Greg Clayton9e409562010-07-28 02:04:09 +00002000 }
2001 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002002 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002003 return field;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002004}
2005
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002006static clang::AccessSpecifier UnifyAccessSpecifiers (clang::AccessSpecifier lhs,
2007 clang::AccessSpecifier rhs)
2008{
2009 clang::AccessSpecifier ret = lhs;
2010
2011 // Make the access equal to the stricter of the field and the nested field's access
2012 switch (ret)
2013 {
2014 case clang::AS_none:
2015 break;
2016 case clang::AS_private:
2017 break;
2018 case clang::AS_protected:
2019 if (rhs == AS_private)
2020 ret = AS_private;
2021 break;
2022 case clang::AS_public:
2023 ret = rhs;
2024 break;
2025 }
2026
2027 return ret;
2028}
2029
2030void
2031ClangASTContext::BuildIndirectFields (clang::ASTContext *ast,
2032 lldb::clang_type_t record_clang_type)
2033{
2034 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
2035
2036 const RecordType *record_type = record_qual_type->getAs<RecordType>();
2037
2038 if (!record_type)
2039 return;
2040
2041 RecordDecl *record_decl = record_type->getDecl();
2042
2043 if (!record_decl)
2044 return;
2045
2046 typedef llvm::SmallVector <IndirectFieldDecl *, 1> IndirectFieldVector;
2047
2048 IndirectFieldVector indirect_fields;
Greg Clayton4ef877f2012-12-06 02:33:54 +00002049 RecordDecl::field_iterator field_pos;
2050 RecordDecl::field_iterator field_end_pos = record_decl->field_end();
2051 RecordDecl::field_iterator last_field_pos = field_end_pos;
2052 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; last_field_pos = field_pos++)
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002053 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00002054 if (field_pos->isAnonymousStructOrUnion())
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002055 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00002056 QualType field_qual_type = field_pos->getType();
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002057
2058 const RecordType *field_record_type = field_qual_type->getAs<RecordType>();
2059
2060 if (!field_record_type)
2061 continue;
2062
2063 RecordDecl *field_record_decl = field_record_type->getDecl();
2064
2065 if (!field_record_decl)
2066 continue;
2067
2068 for (RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
2069 di != de;
2070 ++di)
2071 {
2072 if (FieldDecl *nested_field_decl = dyn_cast<FieldDecl>(*di))
2073 {
2074 NamedDecl **chain = new (*ast) NamedDecl*[2];
Greg Clayton4ef877f2012-12-06 02:33:54 +00002075 chain[0] = *field_pos;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002076 chain[1] = nested_field_decl;
2077 IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*ast,
2078 record_decl,
2079 SourceLocation(),
2080 nested_field_decl->getIdentifier(),
2081 nested_field_decl->getType(),
2082 chain,
2083 2);
2084
Greg Clayton4ef877f2012-12-06 02:33:54 +00002085 indirect_field->setAccess(UnifyAccessSpecifiers(field_pos->getAccess(),
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002086 nested_field_decl->getAccess()));
2087
2088 indirect_fields.push_back(indirect_field);
2089 }
2090 else if (IndirectFieldDecl *nested_indirect_field_decl = dyn_cast<IndirectFieldDecl>(*di))
2091 {
2092 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
2093 NamedDecl **chain = new (*ast) NamedDecl*[nested_chain_size + 1];
Greg Clayton4ef877f2012-12-06 02:33:54 +00002094 chain[0] = *field_pos;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002095
2096 int chain_index = 1;
2097 for (IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
2098 nce = nested_indirect_field_decl->chain_end();
2099 nci < nce;
2100 ++nci)
2101 {
2102 chain[chain_index] = *nci;
2103 chain_index++;
2104 }
2105
2106 IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*ast,
2107 record_decl,
2108 SourceLocation(),
2109 nested_indirect_field_decl->getIdentifier(),
2110 nested_indirect_field_decl->getType(),
2111 chain,
2112 nested_chain_size + 1);
2113
Greg Clayton4ef877f2012-12-06 02:33:54 +00002114 indirect_field->setAccess(UnifyAccessSpecifiers(field_pos->getAccess(),
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002115 nested_indirect_field_decl->getAccess()));
2116
2117 indirect_fields.push_back(indirect_field);
2118 }
2119 }
2120 }
2121 }
2122
Greg Clayton4ef877f2012-12-06 02:33:54 +00002123 // Check the last field to see if it has an incomplete array type as its
2124 // last member and if it does, the tell the record decl about it
2125 if (last_field_pos != field_end_pos)
2126 {
2127 if (last_field_pos->getType()->isIncompleteArrayType())
2128 record_decl->hasFlexibleArrayMember();
2129 }
2130
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002131 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
2132 ifi < ife;
2133 ++ifi)
2134 {
2135 record_decl->addDecl(*ifi);
2136 }
2137}
2138
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002139bool
2140ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
2141{
2142 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
2143}
2144
2145bool
2146ClangASTContext::FieldIsBitfield
2147(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002148 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002149 FieldDecl* field,
2150 uint32_t& bitfield_bit_size
2151)
2152{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002153 if (ast == NULL || field == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002154 return false;
2155
2156 if (field->isBitField())
2157 {
2158 Expr* bit_width_expr = field->getBitWidth();
2159 if (bit_width_expr)
2160 {
2161 llvm::APSInt bit_width_apsint;
Greg Clayton6beaaa62011-01-17 03:46:26 +00002162 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002163 {
2164 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
2165 return true;
2166 }
2167 }
2168 }
2169 return false;
2170}
2171
2172bool
2173ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
2174{
2175 if (record_decl == NULL)
2176 return false;
2177
2178 if (!record_decl->field_empty())
2179 return true;
2180
2181 // No fields, lets check this is a CXX record and check the base classes
2182 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2183 if (cxx_record_decl)
2184 {
2185 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2186 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2187 base_class != base_class_end;
2188 ++base_class)
2189 {
2190 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2191 if (RecordHasFields(base_class_decl))
2192 return true;
2193 }
2194 }
2195 return false;
2196}
2197
2198void
Greg Clayton6beaaa62011-01-17 03:46:26 +00002199ClangASTContext::SetDefaultAccessForRecordFields (clang_type_t clang_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002200{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002201 if (clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002202 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002203 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
2204
Sean Callanan78e37602011-01-27 04:42:51 +00002205 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +00002206 if (record_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002207 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002208 RecordDecl *record_decl = record_type->getDecl();
2209 if (record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002210 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002211 uint32_t field_idx;
2212 RecordDecl::field_iterator field, field_end;
2213 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2214 field != field_end;
2215 ++field, ++field_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002216 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002217 // If no accessibility was assigned, assign the correct one
2218 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2219 field->setAccess ((AccessSpecifier)default_accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002220 }
2221 }
2222 }
2223 }
2224}
2225
2226#pragma mark C++ Base Classes
2227
2228CXXBaseSpecifier *
Greg Clayton1be10fc2010-09-29 01:12:09 +00002229ClangASTContext::CreateBaseClassSpecifier (clang_type_t base_class_type, AccessType access, bool is_virtual, bool base_of_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002230{
2231 if (base_class_type)
Greg Claytone6371122010-07-30 20:30:44 +00002232 return new CXXBaseSpecifier (SourceRange(),
2233 is_virtual,
2234 base_of_class,
2235 ConvertAccessTypeToAccessSpecifier (access),
Sean Callanan2c777c42011-01-18 23:32:05 +00002236 getASTContext()->CreateTypeSourceInfo (QualType::getFromOpaquePtr(base_class_type)),
2237 SourceLocation());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002238 return NULL;
2239}
2240
Greg Clayton0b42ac32010-07-02 01:29:13 +00002241void
2242ClangASTContext::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes)
2243{
2244 for (unsigned i=0; i<num_base_classes; ++i)
2245 {
2246 delete base_classes[i];
2247 base_classes[i] = NULL;
2248 }
2249}
2250
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002251bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002252ClangASTContext::SetBaseClassesForClassType (clang_type_t class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002253{
2254 if (class_clang_type)
2255 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002256 CXXRecordDecl *cxx_record_decl = QualType::getFromOpaquePtr(class_clang_type)->getAsCXXRecordDecl();
2257 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002258 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002259 cxx_record_decl->setBases(base_classes, num_base_classes);
2260 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002261 }
2262 }
2263 return false;
2264}
Greg Clayton8cf05932010-07-22 18:30:50 +00002265#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002266
Greg Clayton1be10fc2010-09-29 01:12:09 +00002267clang_type_t
Sean Callananad880762012-04-18 01:06:17 +00002268ClangASTContext::CreateObjCClass
Greg Clayton8cf05932010-07-22 18:30:50 +00002269(
2270 const char *name,
2271 DeclContext *decl_ctx,
2272 bool isForwardDecl,
Sean Callananad880762012-04-18 01:06:17 +00002273 bool isInternal,
Jim Ingham379397632012-10-27 02:54:13 +00002274 ClangASTMetadata *metadata
Greg Clayton8cf05932010-07-22 18:30:50 +00002275)
2276{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002277 ASTContext *ast = getASTContext();
2278 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00002279 assert (name && name[0]);
2280 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00002281 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00002282
2283 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
2284 // we will need to update this code. I was told to currently always use
2285 // the CXXRecordDecl class since we often don't know from debug information
2286 // if something is struct or a class, so we default to always use the more
2287 // complete definition just in case.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002288 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00002289 decl_ctx,
2290 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00002291 &ast->Idents.get(name),
Sean Callanan5b26f272012-02-04 08:49:35 +00002292 NULL,
Greg Clayton8cf05932010-07-22 18:30:50 +00002293 SourceLocation(),
Sean Callanan5b26f272012-02-04 08:49:35 +00002294 /*isForwardDecl,*/
Greg Clayton8cf05932010-07-22 18:30:50 +00002295 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00002296
Jim Ingham379397632012-10-27 02:54:13 +00002297 if (decl && metadata)
2298 SetMetadata(ast, (uintptr_t)decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00002299
Greg Clayton6beaaa62011-01-17 03:46:26 +00002300 return ast->getObjCInterfaceType(decl).getAsOpaquePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002301}
2302
2303bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002304ClangASTContext::SetObjCSuperClass (clang_type_t class_opaque_type, clang_type_t super_opaque_type)
Greg Clayton8cf05932010-07-22 18:30:50 +00002305{
2306 if (class_opaque_type && super_opaque_type)
2307 {
2308 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2309 QualType super_qual_type(QualType::getFromOpaquePtr(super_opaque_type));
Sean Callanan78e37602011-01-27 04:42:51 +00002310 const clang::Type *class_type = class_qual_type.getTypePtr();
2311 const clang::Type *super_type = super_qual_type.getTypePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002312 if (class_type && super_type)
2313 {
Sean Callanan78e37602011-01-27 04:42:51 +00002314 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
2315 const ObjCObjectType *objc_super_type = dyn_cast<ObjCObjectType>(super_type);
Greg Clayton8cf05932010-07-22 18:30:50 +00002316 if (objc_class_type && objc_super_type)
2317 {
2318 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2319 ObjCInterfaceDecl *super_interface_decl = objc_super_type->getInterface();
2320 if (class_interface_decl && super_interface_decl)
2321 {
2322 class_interface_decl->setSuperClass(super_interface_decl);
2323 return true;
2324 }
2325 }
2326 }
2327 }
2328 return false;
2329}
2330
2331
Jim Inghame3ae82a2011-11-12 01:36:43 +00002332FieldDecl *
Greg Clayton8cf05932010-07-22 18:30:50 +00002333ClangASTContext::AddObjCClassIVar
2334(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002335 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002336 clang_type_t class_opaque_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00002337 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002338 clang_type_t ivar_opaque_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00002339 AccessType access,
2340 uint32_t bitfield_bit_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00002341 bool is_synthesized
Greg Clayton8cf05932010-07-22 18:30:50 +00002342)
2343{
2344 if (class_opaque_type == NULL || ivar_opaque_type == NULL)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002345 return NULL;
Greg Clayton8cf05932010-07-22 18:30:50 +00002346
Jim Inghame3ae82a2011-11-12 01:36:43 +00002347 ObjCIvarDecl *field = NULL;
2348
Greg Clayton6beaaa62011-01-17 03:46:26 +00002349 IdentifierTable *identifier_table = &ast->Idents;
Greg Clayton8cf05932010-07-22 18:30:50 +00002350
Greg Clayton6beaaa62011-01-17 03:46:26 +00002351 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00002352 assert (identifier_table != NULL);
2353
2354 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2355
Sean Callanan78e37602011-01-27 04:42:51 +00002356 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002357 if (class_type)
2358 {
Sean Callanan78e37602011-01-27 04:42:51 +00002359 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton8cf05932010-07-22 18:30:50 +00002360
2361 if (objc_class_type)
2362 {
2363 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2364
2365 if (class_interface_decl)
2366 {
2367 clang::Expr *bit_width = NULL;
2368 if (bitfield_bit_size != 0)
2369 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002370 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
2371 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
Greg Clayton8cf05932010-07-22 18:30:50 +00002372 }
2373
Jim Inghame3ae82a2011-11-12 01:36:43 +00002374 field = ObjCIvarDecl::Create (*ast,
2375 class_interface_decl,
2376 SourceLocation(),
2377 SourceLocation(),
Greg Clayton88bc7f32012-11-06 00:20:41 +00002378 name ? &identifier_table->get(name) : NULL, // Identifier
Jim Inghame3ae82a2011-11-12 01:36:43 +00002379 QualType::getFromOpaquePtr(ivar_opaque_type), // Field type
2380 NULL, // TypeSourceInfo *
2381 ConvertAccessTypeToObjCIvarAccessControl (access),
2382 bit_width,
2383 is_synthesized);
Greg Clayton9e409562010-07-28 02:04:09 +00002384
2385 if (field)
2386 {
2387 class_interface_decl->addDecl(field);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002388
2389#ifdef LLDB_CONFIGURATION_DEBUG
2390 VerifyDecl(field);
2391#endif
2392
Jim Inghame3ae82a2011-11-12 01:36:43 +00002393 return field;
Greg Clayton9e409562010-07-28 02:04:09 +00002394 }
Greg Clayton8cf05932010-07-22 18:30:50 +00002395 }
2396 }
2397 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002398 return NULL;
2399}
2400
2401bool
2402ClangASTContext::AddObjCClassProperty
2403(
2404 ASTContext *ast,
2405 clang_type_t class_opaque_type,
2406 const char *property_name,
2407 clang_type_t property_opaque_type,
2408 ObjCIvarDecl *ivar_decl,
2409 const char *property_setter_name,
2410 const char *property_getter_name,
Sean Callananad880762012-04-18 01:06:17 +00002411 uint32_t property_attributes,
Jim Ingham379397632012-10-27 02:54:13 +00002412 ClangASTMetadata *metadata
Jim Inghame3ae82a2011-11-12 01:36:43 +00002413)
2414{
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002415 if (class_opaque_type == NULL || property_name == NULL || property_name[0] == '\0')
Jim Inghame3ae82a2011-11-12 01:36:43 +00002416 return false;
2417
2418 IdentifierTable *identifier_table = &ast->Idents;
2419
2420 assert (ast != NULL);
2421 assert (identifier_table != NULL);
2422
2423 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2424 const clang::Type *class_type = class_qual_type.getTypePtr();
2425 if (class_type)
2426 {
2427 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
2428
2429 if (objc_class_type)
2430 {
2431 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2432
Greg Clayton23f59502012-07-17 03:23:13 +00002433 clang_type_t property_opaque_type_to_access = NULL;
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002434
2435 if (property_opaque_type)
2436 property_opaque_type_to_access = property_opaque_type;
2437 else if (ivar_decl)
2438 property_opaque_type_to_access = ivar_decl->getType().getAsOpaquePtr();
2439
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002440 if (class_interface_decl && property_opaque_type_to_access)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002441 {
2442 clang::TypeSourceInfo *prop_type_source;
2443 if (ivar_decl)
2444 prop_type_source = ast->CreateTypeSourceInfo (ivar_decl->getType());
2445 else
2446 prop_type_source = ast->CreateTypeSourceInfo (QualType::getFromOpaquePtr(property_opaque_type));
2447
2448 ObjCPropertyDecl *property_decl = ObjCPropertyDecl::Create(*ast,
2449 class_interface_decl,
2450 SourceLocation(), // Source Location
2451 &identifier_table->get(property_name),
2452 SourceLocation(), //Source Location for AT
Sean Callanand5f33a82012-03-01 02:03:47 +00002453 SourceLocation(), //Source location for (
Jim Inghame3ae82a2011-11-12 01:36:43 +00002454 prop_type_source
2455 );
Sean Callananad880762012-04-18 01:06:17 +00002456
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002457 if (property_decl)
2458 {
Jim Ingham379397632012-10-27 02:54:13 +00002459 if (metadata)
2460 SetMetadata(ast, (uintptr_t)property_decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00002461
Jim Inghame3ae82a2011-11-12 01:36:43 +00002462 class_interface_decl->addDecl (property_decl);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002463
2464 Selector setter_sel, getter_sel;
2465
Jim Inghame3ae82a2011-11-12 01:36:43 +00002466 if (property_setter_name != NULL)
2467 {
2468 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
2469 clang::IdentifierInfo *setter_ident = &identifier_table->get(property_setter_no_colon.c_str());
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002470 setter_sel = ast->Selectors.getSelector(1, &setter_ident);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002471 }
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002472 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
2473 {
2474 std::string setter_sel_string("set");
2475 setter_sel_string.push_back(::toupper(property_name[0]));
2476 setter_sel_string.append(&property_name[1]);
2477 clang::IdentifierInfo *setter_ident = &identifier_table->get(setter_sel_string.c_str());
2478 setter_sel = ast->Selectors.getSelector(1, &setter_ident);
2479 }
Sean Callanana6582262012-04-05 00:12:52 +00002480 property_decl->setSetterName(setter_sel);
2481 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002482
2483 if (property_getter_name != NULL)
2484 {
2485 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_getter_name);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002486 getter_sel = ast->Selectors.getSelector(0, &getter_ident);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002487 }
2488 else
2489 {
2490 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_name);
2491 getter_sel = ast->Selectors.getSelector(0, &getter_ident);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002492 }
Sean Callanana6582262012-04-05 00:12:52 +00002493 property_decl->setGetterName(getter_sel);
2494 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002495
2496 if (ivar_decl)
2497 property_decl->setPropertyIvarDecl (ivar_decl);
2498
2499 if (property_attributes & DW_APPLE_PROPERTY_readonly)
2500 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
2501 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
2502 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
2503 if (property_attributes & DW_APPLE_PROPERTY_assign)
2504 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
2505 if (property_attributes & DW_APPLE_PROPERTY_retain)
2506 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
2507 if (property_attributes & DW_APPLE_PROPERTY_copy)
2508 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
2509 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
2510 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002511
2512 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
2513 {
2514 QualType result_type = QualType::getFromOpaquePtr(property_opaque_type_to_access);
2515
2516 const bool isInstance = true;
2517 const bool isVariadic = false;
2518 const bool isSynthesized = false;
2519 const bool isImplicitlyDeclared = true;
2520 const bool isDefined = false;
2521 const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
2522 const bool HasRelatedResultType = false;
2523
2524 ObjCMethodDecl *getter = ObjCMethodDecl::Create(*ast,
2525 SourceLocation(),
2526 SourceLocation(),
2527 getter_sel,
2528 result_type,
2529 NULL,
2530 class_interface_decl,
2531 isInstance,
2532 isVariadic,
2533 isSynthesized,
2534 isImplicitlyDeclared,
2535 isDefined,
2536 impControl,
2537 HasRelatedResultType);
Sean Callananad880762012-04-18 01:06:17 +00002538
Jim Ingham379397632012-10-27 02:54:13 +00002539 if (getter && metadata)
2540 SetMetadata(ast, (uintptr_t)getter, *metadata);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002541
2542 getter->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(), ArrayRef<SourceLocation>());
2543
2544 class_interface_decl->addDecl(getter);
2545 }
2546
2547 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
2548 {
2549 QualType result_type = ast->VoidTy;
2550
2551 const bool isInstance = true;
2552 const bool isVariadic = false;
2553 const bool isSynthesized = false;
2554 const bool isImplicitlyDeclared = true;
2555 const bool isDefined = false;
2556 const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
2557 const bool HasRelatedResultType = false;
2558
2559 ObjCMethodDecl *setter = ObjCMethodDecl::Create(*ast,
2560 SourceLocation(),
2561 SourceLocation(),
2562 setter_sel,
2563 result_type,
2564 NULL,
2565 class_interface_decl,
2566 isInstance,
2567 isVariadic,
2568 isSynthesized,
2569 isImplicitlyDeclared,
2570 isDefined,
2571 impControl,
2572 HasRelatedResultType);
2573
Jim Ingham379397632012-10-27 02:54:13 +00002574 if (setter && metadata)
2575 SetMetadata(ast, (uintptr_t)setter, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00002576
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002577 llvm::SmallVector<ParmVarDecl *, 1> params;
2578
2579 params.push_back (ParmVarDecl::Create (*ast,
2580 setter,
2581 SourceLocation(),
2582 SourceLocation(),
2583 NULL, // anonymous
2584 QualType::getFromOpaquePtr(property_opaque_type_to_access),
2585 NULL,
2586 SC_Auto,
2587 SC_Auto,
2588 NULL));
2589
2590 setter->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
2591
2592 class_interface_decl->addDecl(setter);
2593 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002594
2595 return true;
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002596 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002597 }
2598 }
2599 }
Greg Clayton8cf05932010-07-22 18:30:50 +00002600 return false;
2601}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002602
Greg Clayton9e409562010-07-28 02:04:09 +00002603bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002604ClangASTContext::ObjCTypeHasIVars (clang_type_t class_opaque_type, bool check_superclass)
Greg Clayton9e409562010-07-28 02:04:09 +00002605{
2606 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2607
Sean Callanan78e37602011-01-27 04:42:51 +00002608 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton9e409562010-07-28 02:04:09 +00002609 if (class_type)
2610 {
Sean Callanan78e37602011-01-27 04:42:51 +00002611 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00002612
2613 if (objc_class_type)
2614 return ObjCDeclHasIVars (objc_class_type->getInterface(), check_superclass);
2615 }
2616 return false;
2617}
2618
2619bool
2620ClangASTContext::ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
2621{
2622 while (class_interface_decl)
2623 {
2624 if (class_interface_decl->ivar_size() > 0)
2625 return true;
2626
2627 if (check_superclass)
2628 class_interface_decl = class_interface_decl->getSuperClass();
2629 else
2630 break;
2631 }
2632 return false;
2633}
Greg Clayton0fffff52010-09-24 05:15:53 +00002634
Greg Clayton1be10fc2010-09-29 01:12:09 +00002635ObjCMethodDecl *
Greg Clayton0fffff52010-09-24 05:15:53 +00002636ClangASTContext::AddMethodToObjCObjectType
2637(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002638 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002639 clang_type_t class_opaque_type,
Greg Clayton0fffff52010-09-24 05:15:53 +00002640 const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
Greg Clayton1be10fc2010-09-29 01:12:09 +00002641 clang_type_t method_opaque_type,
Greg Clayton0fffff52010-09-24 05:15:53 +00002642 lldb::AccessType access
2643)
2644{
2645 if (class_opaque_type == NULL || method_opaque_type == NULL)
2646 return NULL;
2647
Greg Clayton6beaaa62011-01-17 03:46:26 +00002648 IdentifierTable *identifier_table = &ast->Idents;
Greg Clayton0fffff52010-09-24 05:15:53 +00002649
Greg Clayton6beaaa62011-01-17 03:46:26 +00002650 assert (ast != NULL);
Greg Clayton0fffff52010-09-24 05:15:53 +00002651 assert (identifier_table != NULL);
2652
2653 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2654
Sean Callanan78e37602011-01-27 04:42:51 +00002655 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton0fffff52010-09-24 05:15:53 +00002656 if (class_type == NULL)
2657 return NULL;
2658
Sean Callanan78e37602011-01-27 04:42:51 +00002659 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton0fffff52010-09-24 05:15:53 +00002660
2661 if (objc_class_type == NULL)
2662 return NULL;
2663
2664 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2665
2666 if (class_interface_decl == NULL)
2667 return NULL;
Greg Clayton9e409562010-07-28 02:04:09 +00002668
Greg Clayton0fffff52010-09-24 05:15:53 +00002669 const char *selector_start = ::strchr (name, ' ');
2670 if (selector_start == NULL)
2671 return NULL;
2672
2673 selector_start++;
2674 if (!(::isalpha (selector_start[0]) || selector_start[0] == '_'))
2675 return NULL;
2676 llvm::SmallVector<IdentifierInfo *, 12> selector_idents;
2677
Greg Clayton450e3f32010-10-12 02:24:53 +00002678 size_t len = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +00002679 const char *start;
Greg Clayton450e3f32010-10-12 02:24:53 +00002680 //printf ("name = '%s'\n", name);
2681
2682 unsigned num_selectors_with_args = 0;
2683 for (start = selector_start;
Greg Clayton0fffff52010-09-24 05:15:53 +00002684 start && *start != '\0' && *start != ']';
Greg Clayton450e3f32010-10-12 02:24:53 +00002685 start += len)
Greg Clayton0fffff52010-09-24 05:15:53 +00002686 {
Greg Clayton450e3f32010-10-12 02:24:53 +00002687 len = ::strcspn(start, ":]");
Greg Clayton90f90cd2010-10-27 04:01:14 +00002688 bool has_arg = (start[len] == ':');
2689 if (has_arg)
Greg Clayton450e3f32010-10-12 02:24:53 +00002690 ++num_selectors_with_args;
Greg Clayton0fffff52010-09-24 05:15:53 +00002691 selector_idents.push_back (&identifier_table->get (StringRef (start, len)));
Greg Clayton90f90cd2010-10-27 04:01:14 +00002692 if (has_arg)
2693 len += 1;
Greg Clayton0fffff52010-09-24 05:15:53 +00002694 }
2695
2696
2697 if (selector_idents.size() == 0)
2698 return 0;
2699
Greg Clayton6beaaa62011-01-17 03:46:26 +00002700 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
Greg Clayton0fffff52010-09-24 05:15:53 +00002701 selector_idents.data());
2702
2703 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
2704
2705 // Populate the method decl with parameter decls
Sean Callanan78e37602011-01-27 04:42:51 +00002706 const clang::Type *method_type(method_qual_type.getTypePtr());
Greg Clayton0fffff52010-09-24 05:15:53 +00002707
2708 if (method_type == NULL)
2709 return NULL;
2710
Sean Callanan78e37602011-01-27 04:42:51 +00002711 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type));
Greg Clayton0fffff52010-09-24 05:15:53 +00002712
2713 if (!method_function_prototype)
2714 return NULL;
2715
2716
2717 bool is_variadic = false;
2718 bool is_synthesized = false;
2719 bool is_defined = false;
2720 ObjCMethodDecl::ImplementationControl imp_control = ObjCMethodDecl::None;
2721
2722 const unsigned num_args = method_function_prototype->getNumArgs();
2723
Greg Clayton6beaaa62011-01-17 03:46:26 +00002724 ObjCMethodDecl *objc_method_decl = ObjCMethodDecl::Create (*ast,
Greg Clayton0fffff52010-09-24 05:15:53 +00002725 SourceLocation(), // beginLoc,
2726 SourceLocation(), // endLoc,
2727 method_selector,
2728 method_function_prototype->getResultType(),
2729 NULL, // TypeSourceInfo *ResultTInfo,
2730 GetDeclContextForType (class_opaque_type),
2731 name[0] == '-',
2732 is_variadic,
2733 is_synthesized,
Sean Callanan880e6802011-10-07 23:18:13 +00002734 true, // is_implicitly_declared
Greg Clayton0fffff52010-09-24 05:15:53 +00002735 is_defined,
2736 imp_control,
Sean Callanan880e6802011-10-07 23:18:13 +00002737 false /*has_related_result_type*/);
Greg Clayton0fffff52010-09-24 05:15:53 +00002738
2739
2740 if (objc_method_decl == NULL)
2741 return NULL;
2742
2743 if (num_args > 0)
2744 {
2745 llvm::SmallVector<ParmVarDecl *, 12> params;
2746
2747 for (int param_index = 0; param_index < num_args; ++param_index)
2748 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002749 params.push_back (ParmVarDecl::Create (*ast,
Greg Clayton0fffff52010-09-24 05:15:53 +00002750 objc_method_decl,
2751 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00002752 SourceLocation(),
Greg Clayton0fffff52010-09-24 05:15:53 +00002753 NULL, // anonymous
2754 method_function_prototype->getArgType(param_index),
2755 NULL,
2756 SC_Auto,
2757 SC_Auto,
2758 NULL));
2759 }
2760
Sean Callanan880e6802011-10-07 23:18:13 +00002761 objc_method_decl->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
Greg Clayton0fffff52010-09-24 05:15:53 +00002762 }
2763
2764 class_interface_decl->addDecl (objc_method_decl);
2765
Sean Callanan5e9e1992011-10-26 01:06:27 +00002766#ifdef LLDB_CONFIGURATION_DEBUG
2767 VerifyDecl(objc_method_decl);
2768#endif
Greg Clayton0fffff52010-09-24 05:15:53 +00002769
2770 return objc_method_decl;
2771}
2772
Greg Clayton402230e2012-02-03 01:30:30 +00002773size_t
2774ClangASTContext::GetNumTemplateArguments (clang::ASTContext *ast, clang_type_t clang_type)
2775{
2776 if (clang_type)
2777 {
2778 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2779
2780 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2781 switch (type_class)
2782 {
2783 case clang::Type::Record:
2784 if (GetCompleteQualType (ast, qual_type))
2785 {
2786 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2787 if (cxx_record_decl)
2788 {
2789 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2790 if (template_decl)
2791 return template_decl->getTemplateArgs().size();
2792 }
2793 }
2794 break;
2795
2796 case clang::Type::Typedef:
2797 return ClangASTContext::GetNumTemplateArguments (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002798
2799 case clang::Type::Elaborated:
2800 return ClangASTContext::GetNumTemplateArguments (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2801
Greg Clayton402230e2012-02-03 01:30:30 +00002802 default:
2803 break;
2804 }
2805 }
2806 return 0;
2807}
2808
2809clang_type_t
2810ClangASTContext::GetTemplateArgument (clang::ASTContext *ast, clang_type_t clang_type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
2811{
2812 if (clang_type)
2813 {
2814 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2815
2816 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2817 switch (type_class)
2818 {
2819 case clang::Type::Record:
2820 if (GetCompleteQualType (ast, qual_type))
2821 {
2822 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2823 if (cxx_record_decl)
2824 {
2825 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2826 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
2827 {
2828 const TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
2829 switch (template_arg.getKind())
2830 {
2831 case clang::TemplateArgument::Null:
2832 kind = eTemplateArgumentKindNull;
2833 return NULL;
2834
2835 case clang::TemplateArgument::Type:
2836 kind = eTemplateArgumentKindType;
2837 return template_arg.getAsType().getAsOpaquePtr();
2838
2839 case clang::TemplateArgument::Declaration:
2840 kind = eTemplateArgumentKindDeclaration;
2841 return NULL;
2842
2843 case clang::TemplateArgument::Integral:
2844 kind = eTemplateArgumentKindIntegral;
2845 return template_arg.getIntegralType().getAsOpaquePtr();
2846
2847 case clang::TemplateArgument::Template:
2848 kind = eTemplateArgumentKindTemplate;
2849 return NULL;
2850
2851 case clang::TemplateArgument::TemplateExpansion:
2852 kind = eTemplateArgumentKindTemplateExpansion;
2853 return NULL;
2854
2855 case clang::TemplateArgument::Expression:
2856 kind = eTemplateArgumentKindExpression;
2857 return NULL;
2858
2859 case clang::TemplateArgument::Pack:
2860 kind = eTemplateArgumentKindPack;
2861 return NULL;
2862
2863 default:
2864 assert (!"Unhandled TemplateArgument::ArgKind");
2865 kind = eTemplateArgumentKindNull;
2866 return NULL;
2867 }
2868 }
2869 }
2870 }
2871 break;
2872
2873 case clang::Type::Typedef:
2874 return ClangASTContext::GetTemplateArgument (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), arg_idx, kind);
Greg Clayton4b63a5c2013-01-04 18:10:18 +00002875
2876 case clang::Type::Elaborated:
2877 return ClangASTContext::GetTemplateArgument (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), arg_idx, kind);
2878
Greg Clayton402230e2012-02-03 01:30:30 +00002879 default:
2880 break;
2881 }
2882 }
2883 kind = eTemplateArgumentKindNull;
2884 return NULL;
2885}
Greg Clayton0fffff52010-09-24 05:15:53 +00002886
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002887uint32_t
Greg Clayton73b472d2010-10-27 03:32:59 +00002888ClangASTContext::GetTypeInfo
2889(
2890 clang_type_t clang_type,
Greg Clayton6beaaa62011-01-17 03:46:26 +00002891 clang::ASTContext *ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00002892 clang_type_t *pointee_or_element_clang_type
2893)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002894{
2895 if (clang_type == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00002896 return 0;
2897
2898 if (pointee_or_element_clang_type)
2899 *pointee_or_element_clang_type = NULL;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002900
2901 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2902
2903 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2904 switch (type_class)
2905 {
Sean Callanana2424172010-10-25 00:29:48 +00002906 case clang::Type::Builtin:
2907 switch (cast<clang::BuiltinType>(qual_type)->getKind())
2908 {
Sean Callanana2424172010-10-25 00:29:48 +00002909 case clang::BuiltinType::ObjCId:
2910 case clang::BuiltinType::ObjCClass:
Greg Clayton6beaaa62011-01-17 03:46:26 +00002911 if (ast && pointee_or_element_clang_type)
2912 *pointee_or_element_clang_type = ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Sean Callanana2424172010-10-25 00:29:48 +00002913 return eTypeIsBuiltIn | eTypeIsPointer | eTypeHasValue;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002914 break;
2915 case clang::BuiltinType::Bool:
2916 case clang::BuiltinType::Char_U:
2917 case clang::BuiltinType::UChar:
2918 case clang::BuiltinType::WChar_U:
2919 case clang::BuiltinType::Char16:
2920 case clang::BuiltinType::Char32:
2921 case clang::BuiltinType::UShort:
2922 case clang::BuiltinType::UInt:
2923 case clang::BuiltinType::ULong:
2924 case clang::BuiltinType::ULongLong:
2925 case clang::BuiltinType::UInt128:
2926 case clang::BuiltinType::Char_S:
2927 case clang::BuiltinType::SChar:
2928 case clang::BuiltinType::WChar_S:
2929 case clang::BuiltinType::Short:
2930 case clang::BuiltinType::Int:
2931 case clang::BuiltinType::Long:
2932 case clang::BuiltinType::LongLong:
2933 case clang::BuiltinType::Int128:
2934 case clang::BuiltinType::Float:
2935 case clang::BuiltinType::Double:
2936 case clang::BuiltinType::LongDouble:
2937 return eTypeIsBuiltIn | eTypeHasValue | eTypeIsScalar;
Greg Clayton73b472d2010-10-27 03:32:59 +00002938 default:
2939 break;
Sean Callanana2424172010-10-25 00:29:48 +00002940 }
2941 return eTypeIsBuiltIn | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002942
2943 case clang::Type::BlockPointer:
2944 if (pointee_or_element_clang_type)
2945 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2946 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
2947
Greg Clayton49462ea2011-01-15 02:52:14 +00002948 case clang::Type::Complex: return eTypeIsBuiltIn | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002949
2950 case clang::Type::ConstantArray:
2951 case clang::Type::DependentSizedArray:
2952 case clang::Type::IncompleteArray:
2953 case clang::Type::VariableArray:
2954 if (pointee_or_element_clang_type)
2955 *pointee_or_element_clang_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
2956 return eTypeHasChildren | eTypeIsArray;
2957
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002958 case clang::Type::DependentName: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002959 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
2960 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
2961 case clang::Type::Decltype: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00002962
2963 case clang::Type::Enum:
2964 if (pointee_or_element_clang_type)
2965 *pointee_or_element_clang_type = cast<EnumType>(qual_type)->getDecl()->getIntegerType().getAsOpaquePtr();
2966 return eTypeIsEnumeration | eTypeHasValue;
2967
Sean Callanan912855f2011-08-11 23:56:13 +00002968 case clang::Type::Elaborated:
2969 return ClangASTContext::GetTypeInfo (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2970 ast,
2971 pointee_or_element_clang_type);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002972 case clang::Type::ExtVector: return eTypeHasChildren | eTypeIsVector;
2973 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
2974 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002975 case clang::Type::InjectedClassName: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00002976
2977 case clang::Type::LValueReference:
2978 case clang::Type::RValueReference:
2979 if (pointee_or_element_clang_type)
2980 *pointee_or_element_clang_type = cast<ReferenceType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr();
2981 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
2982
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002983 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002984
2985 case clang::Type::ObjCObjectPointer:
2986 if (pointee_or_element_clang_type)
2987 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2988 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
2989
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002990 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
2991 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
Greg Clayton73b472d2010-10-27 03:32:59 +00002992
2993 case clang::Type::Pointer:
2994 if (pointee_or_element_clang_type)
2995 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2996 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
2997
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002998 case clang::Type::Record:
2999 if (qual_type->getAsCXXRecordDecl())
3000 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
3001 else
3002 return eTypeHasChildren | eTypeIsStructUnion;
3003 break;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003004 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
3005 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
3006 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
Greg Clayton73b472d2010-10-27 03:32:59 +00003007
3008 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00003009 return eTypeIsTypedef | ClangASTContext::GetTypeInfo (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00003010 ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00003011 pointee_or_element_clang_type);
3012
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003013 case clang::Type::TypeOfExpr: return 0;
3014 case clang::Type::TypeOf: return 0;
3015 case clang::Type::UnresolvedUsing: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003016 case clang::Type::Vector: return eTypeHasChildren | eTypeIsVector;
3017 default: return 0;
3018 }
3019 return 0;
3020}
3021
Greg Clayton9e409562010-07-28 02:04:09 +00003022
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003023#pragma mark Aggregate Types
3024
3025bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00003026ClangASTContext::IsAggregateType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003027{
3028 if (clang_type == NULL)
3029 return false;
3030
3031 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3032
Greg Clayton737b9322010-09-13 03:32:57 +00003033 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3034 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003035 {
Greg Claytone1a916a2010-07-21 22:12:05 +00003036 case clang::Type::IncompleteArray:
3037 case clang::Type::VariableArray:
3038 case clang::Type::ConstantArray:
3039 case clang::Type::ExtVector:
3040 case clang::Type::Vector:
3041 case clang::Type::Record:
Greg Clayton9e409562010-07-28 02:04:09 +00003042 case clang::Type::ObjCObject:
3043 case clang::Type::ObjCInterface:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003044 return true;
Sean Callanan912855f2011-08-11 23:56:13 +00003045 case clang::Type::Elaborated:
3046 return ClangASTContext::IsAggregateType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Claytone1a916a2010-07-21 22:12:05 +00003047 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00003048 return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003049
3050 default:
3051 break;
3052 }
3053 // The clang type does have a value
3054 return false;
3055}
3056
3057uint32_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00003058ClangASTContext::GetNumChildren (clang::ASTContext *ast, clang_type_t clang_type, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003059{
Greg Clayton6beaaa62011-01-17 03:46:26 +00003060 if (clang_type == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003061 return 0;
3062
3063 uint32_t num_children = 0;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003064 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00003065 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3066 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003067 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003068 case clang::Type::Builtin:
3069 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3070 {
Greg Clayton73b472d2010-10-27 03:32:59 +00003071 case clang::BuiltinType::ObjCId: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003072 case clang::BuiltinType::ObjCClass: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003073 num_children = 1;
Greg Clayton73b472d2010-10-27 03:32:59 +00003074 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003075
3076 default:
3077 break;
3078 }
3079 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00003080
Greg Clayton49462ea2011-01-15 02:52:14 +00003081 case clang::Type::Complex: return 0;
Greg Clayton54979cd2010-12-15 05:08:08 +00003082
Greg Claytone1a916a2010-07-21 22:12:05 +00003083 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00003084 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003085 {
3086 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3087 const RecordDecl *record_decl = record_type->getDecl();
3088 assert(record_decl);
3089 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3090 if (cxx_record_decl)
3091 {
3092 if (omit_empty_base_classes)
3093 {
3094 // Check each base classes to see if it or any of its
3095 // base classes contain any fields. This can help
3096 // limit the noise in variable views by not having to
3097 // show base classes that contain no members.
3098 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3099 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3100 base_class != base_class_end;
3101 ++base_class)
3102 {
3103 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3104
3105 // Skip empty base classes
3106 if (RecordHasFields(base_class_decl) == false)
3107 continue;
3108
3109 num_children++;
3110 }
3111 }
3112 else
3113 {
3114 // Include all base classes
3115 num_children += cxx_record_decl->getNumBases();
3116 }
3117
3118 }
3119 RecordDecl::field_iterator field, field_end;
3120 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3121 ++num_children;
3122 }
3123 break;
3124
Greg Clayton9e409562010-07-28 02:04:09 +00003125 case clang::Type::ObjCObject:
3126 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00003127 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00003128 {
Sean Callanan78e37602011-01-27 04:42:51 +00003129 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003130 assert (objc_class_type);
3131 if (objc_class_type)
3132 {
3133 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3134
3135 if (class_interface_decl)
3136 {
3137
3138 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3139 if (superclass_interface_decl)
3140 {
3141 if (omit_empty_base_classes)
3142 {
3143 if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true))
3144 ++num_children;
3145 }
3146 else
3147 ++num_children;
3148 }
3149
3150 num_children += class_interface_decl->ivar_size();
3151 }
3152 }
3153 }
3154 break;
3155
3156 case clang::Type::ObjCObjectPointer:
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003157 {
Sean Callanan78e37602011-01-27 04:42:51 +00003158 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr());
Greg Claytonb0b9fe62010-08-03 00:35:52 +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 Claytonb0b9fe62010-08-03 00:35:52 +00003162 omit_empty_base_classes);
3163 // If this type points to a simple type, then it has 1 child
3164 if (num_pointee_children == 0)
3165 num_children = 1;
3166 else
3167 num_children = num_pointee_children;
3168 }
3169 break;
Greg Clayton9e409562010-07-28 02:04:09 +00003170
Greg Claytone1a916a2010-07-21 22:12:05 +00003171 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003172 num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
3173 break;
3174
Greg Claytone1a916a2010-07-21 22:12:05 +00003175 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003176 {
Sean Callanan78e37602011-01-27 04:42:51 +00003177 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003178 QualType pointee_type (pointer_type->getPointeeType());
Greg Clayton6beaaa62011-01-17 03:46:26 +00003179 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3180 pointee_type.getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00003181 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003182 if (num_pointee_children == 0)
Greg Clayton54979cd2010-12-15 05:08:08 +00003183 {
3184 // We have a pointer to a pointee type that claims it has no children.
3185 // We will want to look at
3186 num_children = ClangASTContext::GetNumPointeeChildren (pointee_type.getAsOpaquePtr());
3187 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003188 else
3189 num_children = num_pointee_children;
3190 }
3191 break;
3192
Greg Clayton73b472d2010-10-27 03:32:59 +00003193 case clang::Type::LValueReference:
3194 case clang::Type::RValueReference:
3195 {
Sean Callanan78e37602011-01-27 04:42:51 +00003196 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton73b472d2010-10-27 03:32:59 +00003197 QualType pointee_type = reference_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003198 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3199 pointee_type.getAsOpaquePtr(),
Greg Clayton73b472d2010-10-27 03:32:59 +00003200 omit_empty_base_classes);
3201 // If this type points to a simple type, then it has 1 child
3202 if (num_pointee_children == 0)
3203 num_children = 1;
3204 else
3205 num_children = num_pointee_children;
3206 }
3207 break;
3208
3209
Greg Claytone1a916a2010-07-21 22:12:05 +00003210 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00003211 num_children = ClangASTContext::GetNumChildren (ast,
3212 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3213 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003214 break;
Sean Callanan912855f2011-08-11 23:56:13 +00003215
3216 case clang::Type::Elaborated:
3217 num_children = ClangASTContext::GetNumChildren (ast,
3218 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3219 omit_empty_base_classes);
3220 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003221
3222 default:
3223 break;
3224 }
3225 return num_children;
3226}
3227
Greg Claytonbf2331c2011-09-09 23:04:00 +00003228uint32_t
3229ClangASTContext::GetNumDirectBaseClasses (clang::ASTContext *ast, clang_type_t clang_type)
3230{
3231 if (clang_type == NULL)
3232 return 0;
3233
3234 uint32_t count = 0;
3235 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3236 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3237 switch (type_class)
3238 {
3239 case clang::Type::Record:
3240 if (GetCompleteQualType (ast, qual_type))
3241 {
3242 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3243 if (cxx_record_decl)
3244 count = cxx_record_decl->getNumBases();
3245 }
3246 break;
3247
3248 case clang::Type::ObjCObject:
3249 case clang::Type::ObjCInterface:
3250 if (GetCompleteQualType (ast, qual_type))
3251 {
3252 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3253 if (objc_class_type)
3254 {
3255 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3256
3257 if (class_interface_decl && class_interface_decl->getSuperClass())
3258 count = 1;
3259 }
3260 }
3261 break;
3262
3263
3264 case clang::Type::Typedef:
3265 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3266 break;
3267
3268 case clang::Type::Elaborated:
3269 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3270 break;
3271
3272 default:
3273 break;
3274 }
3275 return count;
3276}
3277
3278uint32_t
3279ClangASTContext::GetNumVirtualBaseClasses (clang::ASTContext *ast,
3280 clang_type_t clang_type)
3281{
3282 if (clang_type == NULL)
3283 return 0;
3284
3285 uint32_t count = 0;
3286 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3287 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3288 switch (type_class)
3289 {
3290 case clang::Type::Record:
3291 if (GetCompleteQualType (ast, qual_type))
3292 {
3293 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3294 if (cxx_record_decl)
3295 count = cxx_record_decl->getNumVBases();
3296 }
3297 break;
3298
3299 case clang::Type::Typedef:
3300 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3301 break;
3302
3303 case clang::Type::Elaborated:
3304 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3305 break;
3306
3307 default:
3308 break;
3309 }
3310 return count;
3311}
3312
3313uint32_t
3314ClangASTContext::GetNumFields (clang::ASTContext *ast, clang_type_t clang_type)
3315{
3316 if (clang_type == NULL)
3317 return 0;
3318
3319 uint32_t count = 0;
3320 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3321 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3322 switch (type_class)
3323 {
3324 case clang::Type::Record:
3325 if (GetCompleteQualType (ast, qual_type))
3326 {
3327 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
3328 if (record_type)
3329 {
3330 RecordDecl *record_decl = record_type->getDecl();
3331 if (record_decl)
3332 {
3333 uint32_t field_idx = 0;
3334 RecordDecl::field_iterator field, field_end;
3335 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3336 ++field_idx;
3337 count = field_idx;
3338 }
3339 }
3340 }
3341 break;
3342
3343 case clang::Type::Typedef:
3344 count = ClangASTContext::GetNumFields (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3345 break;
3346
3347 case clang::Type::Elaborated:
3348 count = ClangASTContext::GetNumFields (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3349 break;
3350
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003351 case clang::Type::ObjCObject:
3352 case clang::Type::ObjCInterface:
3353 if (GetCompleteQualType (ast, qual_type))
3354 {
3355 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3356 if (objc_class_type)
3357 {
3358 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3359
3360 if (class_interface_decl)
3361 count = class_interface_decl->ivar_size();
3362 }
3363 }
3364 break;
3365
Greg Claytonbf2331c2011-09-09 23:04:00 +00003366 default:
3367 break;
3368 }
3369 return count;
3370}
3371
3372clang_type_t
3373ClangASTContext::GetDirectBaseClassAtIndex (clang::ASTContext *ast,
3374 clang_type_t clang_type,
3375 uint32_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003376 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003377{
3378 if (clang_type == NULL)
3379 return 0;
3380
3381 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3382 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3383 switch (type_class)
3384 {
3385 case clang::Type::Record:
3386 if (GetCompleteQualType (ast, qual_type))
3387 {
3388 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3389 if (cxx_record_decl)
3390 {
3391 uint32_t curr_idx = 0;
3392 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3393 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3394 base_class != base_class_end;
3395 ++base_class, ++curr_idx)
3396 {
3397 if (curr_idx == idx)
3398 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003399 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003400 {
3401 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3402 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3403// if (base_class->isVirtual())
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003404// *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003405// else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003406 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003407 }
3408 return base_class->getType().getAsOpaquePtr();
3409 }
3410 }
3411 }
3412 }
3413 break;
3414
3415 case clang::Type::ObjCObject:
3416 case clang::Type::ObjCInterface:
3417 if (idx == 0 && GetCompleteQualType (ast, qual_type))
3418 {
3419 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3420 if (objc_class_type)
3421 {
3422 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3423
3424 if (class_interface_decl)
3425 {
3426 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3427 if (superclass_interface_decl)
3428 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003429 if (bit_offset_ptr)
3430 *bit_offset_ptr = 0;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003431 return ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr();
3432 }
3433 }
3434 }
3435 }
3436 break;
3437
3438
3439 case clang::Type::Typedef:
3440 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3441 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3442 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003443 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003444
3445 case clang::Type::Elaborated:
3446 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3447 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3448 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003449 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003450
3451 default:
3452 break;
3453 }
3454 return NULL;
3455}
3456
3457clang_type_t
3458ClangASTContext::GetVirtualBaseClassAtIndex (clang::ASTContext *ast,
3459 clang_type_t clang_type,
3460 uint32_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003461 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003462{
3463 if (clang_type == NULL)
3464 return 0;
3465
3466 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3467 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3468 switch (type_class)
3469 {
3470 case clang::Type::Record:
3471 if (GetCompleteQualType (ast, qual_type))
3472 {
3473 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3474 if (cxx_record_decl)
3475 {
3476 uint32_t curr_idx = 0;
3477 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3478 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
3479 base_class != base_class_end;
3480 ++base_class, ++curr_idx)
3481 {
3482 if (curr_idx == idx)
3483 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003484 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003485 {
3486 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3487 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003488 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003489
3490 }
3491 return base_class->getType().getAsOpaquePtr();
3492 }
3493 }
3494 }
3495 }
3496 break;
3497
3498 case clang::Type::Typedef:
3499 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3500 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3501 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003502 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003503
3504 case clang::Type::Elaborated:
3505 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3506 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3507 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003508 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003509
3510 default:
3511 break;
3512 }
3513 return NULL;
3514}
3515
3516clang_type_t
3517ClangASTContext::GetFieldAtIndex (clang::ASTContext *ast,
3518 clang_type_t clang_type,
3519 uint32_t idx,
3520 std::string& name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003521 uint64_t *bit_offset_ptr,
3522 uint32_t *bitfield_bit_size_ptr,
3523 bool *is_bitfield_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003524{
3525 if (clang_type == NULL)
3526 return 0;
3527
3528 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3529 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3530 switch (type_class)
3531 {
3532 case clang::Type::Record:
3533 if (GetCompleteQualType (ast, qual_type))
3534 {
3535 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3536 const RecordDecl *record_decl = record_type->getDecl();
3537 uint32_t field_idx = 0;
3538 RecordDecl::field_iterator field, field_end;
3539 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
3540 {
3541 if (idx == field_idx)
3542 {
3543 // Print the member type if requested
3544 // Print the member name and equal sign
3545 name.assign(field->getNameAsString());
3546
3547 // Figure out the type byte size (field_type_info.first) and
3548 // alignment (field_type_info.second) from the AST context.
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003549 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003550 {
3551 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003552 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003553 }
3554
Greg Clayton1811b4f2012-07-31 23:39:10 +00003555 const bool is_bitfield = field->isBitField();
3556
3557 if (bitfield_bit_size_ptr)
3558 {
3559 *bitfield_bit_size_ptr = 0;
3560
3561 if (is_bitfield && ast)
3562 {
3563 Expr *bitfield_bit_size_expr = field->getBitWidth();
3564 llvm::APSInt bitfield_apsint;
3565 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
3566 {
3567 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
3568 }
3569 }
3570 }
3571 if (is_bitfield_ptr)
3572 *is_bitfield_ptr = is_bitfield;
3573
Greg Claytonbf2331c2011-09-09 23:04:00 +00003574 return field->getType().getAsOpaquePtr();
3575 }
3576 }
3577 }
3578 break;
3579
3580 case clang::Type::ObjCObject:
3581 case clang::Type::ObjCInterface:
3582 if (GetCompleteQualType (ast, qual_type))
3583 {
3584 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3585 assert (objc_class_type);
3586 if (objc_class_type)
3587 {
3588 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3589
3590 if (class_interface_decl)
3591 {
3592 if (idx < (class_interface_decl->ivar_size()))
3593 {
3594 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
3595 uint32_t ivar_idx = 0;
3596
3597 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
3598 {
3599 if (ivar_idx == idx)
3600 {
3601 const ObjCIvarDecl* ivar_decl = *ivar_pos;
3602
3603 QualType ivar_qual_type(ivar_decl->getType());
3604
3605 name.assign(ivar_decl->getNameAsString());
3606
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003607 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003608 {
3609 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003610 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003611 }
3612
Greg Clayton1811b4f2012-07-31 23:39:10 +00003613 const bool is_bitfield = ivar_pos->isBitField();
3614
3615 if (bitfield_bit_size_ptr)
3616 {
3617 *bitfield_bit_size_ptr = 0;
3618
3619 if (is_bitfield && ast)
3620 {
3621 Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
3622 llvm::APSInt bitfield_apsint;
3623 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
3624 {
3625 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
3626 }
3627 }
3628 }
3629 if (is_bitfield_ptr)
3630 *is_bitfield_ptr = is_bitfield;
3631
Greg Claytonbf2331c2011-09-09 23:04:00 +00003632 return ivar_qual_type.getAsOpaquePtr();
3633 }
3634 }
3635 }
3636 }
3637 }
3638 }
3639 break;
3640
3641
3642 case clang::Type::Typedef:
3643 return ClangASTContext::GetFieldAtIndex (ast,
3644 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3645 idx,
3646 name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003647 bit_offset_ptr,
3648 bitfield_bit_size_ptr,
3649 is_bitfield_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003650
3651 case clang::Type::Elaborated:
3652 return ClangASTContext::GetFieldAtIndex (ast,
3653 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3654 idx,
3655 name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003656 bit_offset_ptr,
3657 bitfield_bit_size_ptr,
3658 is_bitfield_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003659
3660 default:
3661 break;
3662 }
3663 return NULL;
3664}
3665
Greg Claytoneaafa732012-10-13 00:20:27 +00003666lldb::BasicType
3667ClangASTContext::GetLLDBBasicTypeEnumeration (clang_type_t clang_type)
3668{
3669 if (clang_type)
3670 {
3671 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3672 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Claytonc4c5e892012-10-15 21:16:43 +00003673 if (type_class == clang::Type::Builtin)
Greg Claytoneaafa732012-10-13 00:20:27 +00003674 {
Greg Claytoneaafa732012-10-13 00:20:27 +00003675 switch (cast<clang::BuiltinType>(qual_type)->getKind())
Greg Claytonc4c5e892012-10-15 21:16:43 +00003676 {
Greg Claytoneaafa732012-10-13 00:20:27 +00003677 case clang::BuiltinType::Void: return eBasicTypeVoid;
3678 case clang::BuiltinType::Bool: return eBasicTypeBool;
3679 case clang::BuiltinType::Char_S: return eBasicTypeSignedChar;
3680 case clang::BuiltinType::Char_U: return eBasicTypeUnsignedChar;
3681 case clang::BuiltinType::Char16: return eBasicTypeChar16;
3682 case clang::BuiltinType::Char32: return eBasicTypeChar32;
3683 case clang::BuiltinType::UChar: return eBasicTypeUnsignedChar;
3684 case clang::BuiltinType::SChar: return eBasicTypeSignedChar;
3685 case clang::BuiltinType::WChar_S: return eBasicTypeSignedWChar;
3686 case clang::BuiltinType::WChar_U: return eBasicTypeUnsignedWChar;
3687 case clang::BuiltinType::Short: return eBasicTypeShort;
3688 case clang::BuiltinType::UShort: return eBasicTypeUnsignedShort;
3689 case clang::BuiltinType::Int: return eBasicTypeInt;
3690 case clang::BuiltinType::UInt: return eBasicTypeUnsignedInt;
3691 case clang::BuiltinType::Long: return eBasicTypeLong;
3692 case clang::BuiltinType::ULong: return eBasicTypeUnsignedLong;
3693 case clang::BuiltinType::LongLong: return eBasicTypeLongLong;
3694 case clang::BuiltinType::ULongLong: return eBasicTypeUnsignedLongLong;
3695 case clang::BuiltinType::Int128: return eBasicTypeInt128;
3696 case clang::BuiltinType::UInt128: return eBasicTypeUnsignedInt128;
3697
3698 case clang::BuiltinType::Half: return eBasicTypeHalf;
3699 case clang::BuiltinType::Float: return eBasicTypeFloat;
3700 case clang::BuiltinType::Double: return eBasicTypeDouble;
3701 case clang::BuiltinType::LongDouble:return eBasicTypeLongDouble;
3702
3703 case clang::BuiltinType::NullPtr: return eBasicTypeNullPtr;
3704 case clang::BuiltinType::ObjCId: return eBasicTypeObjCID;
3705 case clang::BuiltinType::ObjCClass: return eBasicTypeObjCClass;
3706 case clang::BuiltinType::ObjCSel: return eBasicTypeObjCSel;
3707 case clang::BuiltinType::Dependent:
3708 case clang::BuiltinType::Overload:
3709 case clang::BuiltinType::BoundMember:
3710 case clang::BuiltinType::PseudoObject:
3711 case clang::BuiltinType::UnknownAny:
3712 case clang::BuiltinType::BuiltinFn:
3713 case clang::BuiltinType::ARCUnbridgedCast:
3714 return eBasicTypeOther;
Greg Claytonc4c5e892012-10-15 21:16:43 +00003715 }
Greg Claytoneaafa732012-10-13 00:20:27 +00003716 }
3717 }
3718
3719 return eBasicTypeInvalid;
3720}
3721
3722
Greg Claytonbf2331c2011-09-09 23:04:00 +00003723
Greg Clayton54979cd2010-12-15 05:08:08 +00003724// If a pointer to a pointee type (the clang_type arg) says that it has no
3725// children, then we either need to trust it, or override it and return a
3726// different result. For example, an "int *" has one child that is an integer,
3727// but a function pointer doesn't have any children. Likewise if a Record type
3728// claims it has no children, then there really is nothing to show.
3729uint32_t
3730ClangASTContext::GetNumPointeeChildren (clang_type_t clang_type)
3731{
3732 if (clang_type == NULL)
3733 return 0;
3734
3735 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3736 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3737 switch (type_class)
3738 {
Greg Clayton97a43712011-01-08 22:26:47 +00003739 case clang::Type::Builtin:
3740 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3741 {
Greg Clayton7260f622011-04-18 08:33:37 +00003742 case clang::BuiltinType::UnknownAny:
Greg Clayton97a43712011-01-08 22:26:47 +00003743 case clang::BuiltinType::Void:
3744 case clang::BuiltinType::NullPtr:
3745 return 0;
3746 case clang::BuiltinType::Bool:
3747 case clang::BuiltinType::Char_U:
3748 case clang::BuiltinType::UChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003749 case clang::BuiltinType::WChar_U:
Greg Clayton97a43712011-01-08 22:26:47 +00003750 case clang::BuiltinType::Char16:
3751 case clang::BuiltinType::Char32:
3752 case clang::BuiltinType::UShort:
3753 case clang::BuiltinType::UInt:
3754 case clang::BuiltinType::ULong:
3755 case clang::BuiltinType::ULongLong:
3756 case clang::BuiltinType::UInt128:
3757 case clang::BuiltinType::Char_S:
3758 case clang::BuiltinType::SChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003759 case clang::BuiltinType::WChar_S:
Greg Clayton97a43712011-01-08 22:26:47 +00003760 case clang::BuiltinType::Short:
3761 case clang::BuiltinType::Int:
3762 case clang::BuiltinType::Long:
3763 case clang::BuiltinType::LongLong:
3764 case clang::BuiltinType::Int128:
3765 case clang::BuiltinType::Float:
3766 case clang::BuiltinType::Double:
3767 case clang::BuiltinType::LongDouble:
3768 case clang::BuiltinType::Dependent:
3769 case clang::BuiltinType::Overload:
Greg Clayton97a43712011-01-08 22:26:47 +00003770 case clang::BuiltinType::ObjCId:
3771 case clang::BuiltinType::ObjCClass:
3772 case clang::BuiltinType::ObjCSel:
Sean Callanand12cf8bb2011-05-15 22:34:38 +00003773 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00003774 case clang::BuiltinType::Half:
3775 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00003776 case clang::BuiltinType::PseudoObject:
Sean Callanan3d654b32012-09-24 22:25:51 +00003777 case clang::BuiltinType::BuiltinFn:
Greg Clayton97a43712011-01-08 22:26:47 +00003778 return 1;
3779 }
3780 break;
3781
Greg Clayton49462ea2011-01-15 02:52:14 +00003782 case clang::Type::Complex: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003783 case clang::Type::Pointer: return 1;
3784 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
3785 case clang::Type::LValueReference: return 1;
3786 case clang::Type::RValueReference: return 1;
3787 case clang::Type::MemberPointer: return 0;
3788 case clang::Type::ConstantArray: return 0;
3789 case clang::Type::IncompleteArray: return 0;
3790 case clang::Type::VariableArray: return 0;
3791 case clang::Type::DependentSizedArray: return 0;
3792 case clang::Type::DependentSizedExtVector: return 0;
3793 case clang::Type::Vector: return 0;
3794 case clang::Type::ExtVector: return 0;
3795 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
3796 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
3797 case clang::Type::UnresolvedUsing: return 0;
3798 case clang::Type::Paren: return 0;
3799 case clang::Type::Typedef: return ClangASTContext::GetNumPointeeChildren (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00003800 case clang::Type::Elaborated: return ClangASTContext::GetNumPointeeChildren (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003801 case clang::Type::TypeOfExpr: return 0;
3802 case clang::Type::TypeOf: return 0;
3803 case clang::Type::Decltype: return 0;
3804 case clang::Type::Record: return 0;
3805 case clang::Type::Enum: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003806 case clang::Type::TemplateTypeParm: return 1;
3807 case clang::Type::SubstTemplateTypeParm: return 1;
3808 case clang::Type::TemplateSpecialization: return 1;
3809 case clang::Type::InjectedClassName: return 0;
3810 case clang::Type::DependentName: return 1;
3811 case clang::Type::DependentTemplateSpecialization: return 1;
3812 case clang::Type::ObjCObject: return 0;
3813 case clang::Type::ObjCInterface: return 0;
3814 case clang::Type::ObjCObjectPointer: return 1;
3815 default:
3816 break;
3817 }
3818 return 0;
3819}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003820
Greg Clayton1be10fc2010-09-29 01:12:09 +00003821clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003822ClangASTContext::GetChildClangTypeAtIndex
3823(
Jim Inghamd555bac2011-06-24 22:03:24 +00003824 ExecutionContext *exe_ctx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003825 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003826 clang_type_t parent_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003827 uint32_t idx,
3828 bool transparent_pointers,
3829 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003830 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003831 std::string& child_name,
3832 uint32_t &child_byte_size,
3833 int32_t &child_byte_offset,
3834 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003835 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003836 bool &child_is_base_class,
3837 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003838)
3839{
3840 if (parent_clang_type)
3841
Jim Inghamd555bac2011-06-24 22:03:24 +00003842 return GetChildClangTypeAtIndex (exe_ctx,
3843 getASTContext(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003844 parent_name,
3845 parent_clang_type,
3846 idx,
3847 transparent_pointers,
3848 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003849 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003850 child_name,
3851 child_byte_size,
3852 child_byte_offset,
3853 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003854 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003855 child_is_base_class,
3856 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003857 return NULL;
3858}
3859
Greg Clayton1be10fc2010-09-29 01:12:09 +00003860clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003861ClangASTContext::GetChildClangTypeAtIndex
3862(
Jim Inghamd555bac2011-06-24 22:03:24 +00003863 ExecutionContext *exe_ctx,
Greg Clayton6beaaa62011-01-17 03:46:26 +00003864 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003865 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003866 clang_type_t parent_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003867 uint32_t idx,
3868 bool transparent_pointers,
3869 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003870 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003871 std::string& child_name,
3872 uint32_t &child_byte_size,
3873 int32_t &child_byte_offset,
3874 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003875 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003876 bool &child_is_base_class,
3877 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003878)
3879{
3880 if (parent_clang_type == NULL)
3881 return NULL;
3882
Greg Clayton4ef877f2012-12-06 02:33:54 +00003883 QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type));
3884 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
3885 child_bitfield_bit_size = 0;
3886 child_bitfield_bit_offset = 0;
3887 child_is_base_class = false;
3888
3889 const bool idx_is_valid = idx < ClangASTContext::GetNumChildren (ast, parent_clang_type, omit_empty_base_classes);
3890 uint32_t bit_offset;
3891 switch (parent_type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003892 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003893 case clang::Type::Builtin:
3894 if (idx_is_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003895 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003896 switch (cast<clang::BuiltinType>(parent_qual_type)->getKind())
3897 {
3898 case clang::BuiltinType::ObjCId:
3899 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00003900 child_name = "isa";
Greg Clayton6beaaa62011-01-17 03:46:26 +00003901 child_byte_size = ast->getTypeSize(ast->ObjCBuiltinClassTy) / CHAR_BIT;
3902 return ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003903
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003904 default:
3905 break;
3906 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00003907 }
3908 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003909
Greg Clayton4ef877f2012-12-06 02:33:54 +00003910 case clang::Type::Record:
3911 if (idx_is_valid && GetCompleteQualType (ast, parent_qual_type))
3912 {
3913 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr());
3914 const RecordDecl *record_decl = record_type->getDecl();
3915 assert(record_decl);
3916 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
3917 uint32_t child_idx = 0;
3918
3919 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3920 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003921 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003922 // We might have base classes to print out first
3923 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3924 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3925 base_class != base_class_end;
3926 ++base_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003927 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003928 const CXXRecordDecl *base_class_decl = NULL;
3929
3930 // Skip empty base classes
3931 if (omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003932 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003933 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3934 if (RecordHasFields(base_class_decl) == false)
3935 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003936 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00003937
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003938 if (idx == child_idx)
3939 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003940 if (base_class_decl == NULL)
3941 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003942
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003943
Greg Clayton4ef877f2012-12-06 02:33:54 +00003944 if (base_class->isVirtual())
3945 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
3946 else
3947 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003948
Greg Clayton4ef877f2012-12-06 02:33:54 +00003949 // Base classes should be a multiple of 8 bits in size
3950 child_byte_offset = bit_offset/8;
3951
3952 child_name = ClangASTType::GetTypeNameForQualType(ast, base_class->getType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003953
Greg Clayton4ef877f2012-12-06 02:33:54 +00003954 uint64_t clang_type_info_bit_size = ast->getTypeSize(base_class->getType());
3955
3956 // Base classes bit sizes should be a multiple of 8 bits in size
3957 assert (clang_type_info_bit_size % 8 == 0);
3958 child_byte_size = clang_type_info_bit_size / 8;
3959 child_is_base_class = true;
3960 return base_class->getType().getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003961 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00003962 // We don't increment the child index in the for loop since we might
3963 // be skipping empty base classes
3964 ++child_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003965 }
3966 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00003967 // Make sure index is in range...
3968 uint32_t field_idx = 0;
3969 RecordDecl::field_iterator field, field_end;
3970 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00003971 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003972 if (idx == child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00003973 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00003974 // Print the member type if requested
3975 // Print the member name and equal sign
3976 child_name.assign(field->getNameAsString().c_str());
3977
3978 // Figure out the type byte size (field_type_info.first) and
3979 // alignment (field_type_info.second) from the AST context.
3980 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(field->getType());
3981 assert(field_idx < record_layout.getFieldCount());
3982
3983 child_byte_size = field_type_info.first / 8;
3984
3985 // Figure out the field offset within the current struct/union/class type
3986 bit_offset = record_layout.getFieldOffset (field_idx);
3987 child_byte_offset = bit_offset / 8;
3988 if (ClangASTContext::FieldIsBitfield (ast, *field, child_bitfield_bit_size))
3989 child_bitfield_bit_offset = bit_offset % 8;
3990
3991 return field->getType().getAsOpaquePtr();
3992 }
3993 }
3994 }
3995 break;
3996
3997 case clang::Type::ObjCObject:
3998 case clang::Type::ObjCInterface:
3999 if (idx_is_valid && GetCompleteQualType (ast, parent_qual_type))
4000 {
4001 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr());
4002 assert (objc_class_type);
4003 if (objc_class_type)
4004 {
4005 uint32_t child_idx = 0;
4006 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4007
4008 if (class_interface_decl)
4009 {
4010
4011 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
4012 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4013 if (superclass_interface_decl)
Greg Clayton9e409562010-07-28 02:04:09 +00004014 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004015 if (omit_empty_base_classes)
Greg Clayton9e409562010-07-28 02:04:09 +00004016 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004017 if (ClangASTContext::GetNumChildren(ast, ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0)
Greg Clayton9e409562010-07-28 02:04:09 +00004018 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004019 if (idx == 0)
Greg Clayton9e409562010-07-28 02:04:09 +00004020 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004021 QualType ivar_qual_type(ast->getObjCInterfaceType(superclass_interface_decl));
Greg Clayton9e409562010-07-28 02:04:09 +00004022
Greg Clayton9e409562010-07-28 02:04:09 +00004023
Greg Clayton4ef877f2012-12-06 02:33:54 +00004024 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
Greg Clayton9e409562010-07-28 02:04:09 +00004025
Greg Clayton6beaaa62011-01-17 03:46:26 +00004026 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004027
4028 child_byte_size = ivar_type_info.first / 8;
Greg Clayton4ef877f2012-12-06 02:33:54 +00004029 child_byte_offset = 0;
4030 child_is_base_class = true;
Greg Clayton9e409562010-07-28 02:04:09 +00004031
Greg Clayton9e409562010-07-28 02:04:09 +00004032 return ivar_qual_type.getAsOpaquePtr();
4033 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004034
Greg Clayton9e409562010-07-28 02:04:09 +00004035 ++child_idx;
4036 }
4037 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004038 else
4039 ++child_idx;
4040 }
4041
4042 const uint32_t superclass_idx = child_idx;
4043
4044 if (idx < (child_idx + class_interface_decl->ivar_size()))
4045 {
4046 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4047
4048 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
4049 {
4050 if (child_idx == idx)
4051 {
4052 ObjCIvarDecl* ivar_decl = *ivar_pos;
4053
4054 QualType ivar_qual_type(ivar_decl->getType());
4055
4056 child_name.assign(ivar_decl->getNameAsString().c_str());
4057
4058 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
4059
4060 child_byte_size = ivar_type_info.first / 8;
4061
4062 // Figure out the field offset within the current struct/union/class type
4063 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
4064 // that doesn't account for the space taken up by unbacked properties, or from
4065 // the changing size of base classes that are newer than this class.
4066 // So if we have a process around that we can ask about this object, do so.
4067 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
4068 Process *process = NULL;
4069 if (exe_ctx)
4070 process = exe_ctx->GetProcessPtr();
4071 if (process)
4072 {
4073 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4074 if (objc_runtime != NULL)
4075 {
4076 ClangASTType parent_ast_type (ast, parent_qual_type.getAsOpaquePtr());
4077 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
4078 }
4079 }
4080
4081 // Setting this to UINT32_MAX to make sure we don't compute it twice...
4082 bit_offset = UINT32_MAX;
4083
4084 if (child_byte_offset == LLDB_INVALID_IVAR_OFFSET)
4085 {
4086 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
4087 child_byte_offset = bit_offset / 8;
4088 }
4089
4090 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
4091 // of a bitfield within its containing object. So regardless of where we get the byte
4092 // offset from, we still need to get the bit offset for bitfields from the layout.
4093
4094 if (ClangASTContext::FieldIsBitfield (ast, ivar_decl, child_bitfield_bit_size))
4095 {
4096 if (bit_offset == UINT32_MAX)
4097 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
4098
4099 child_bitfield_bit_offset = bit_offset % 8;
4100 }
4101 return ivar_qual_type.getAsOpaquePtr();
4102 }
4103 ++child_idx;
4104 }
Greg Clayton9e409562010-07-28 02:04:09 +00004105 }
4106 }
4107 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004108 }
4109 break;
4110
4111 case clang::Type::ObjCObjectPointer:
4112 if (idx_is_valid)
4113 {
4114 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr());
4115 QualType pointee_type = pointer_type->getPointeeType();
4116
4117 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
Greg Clayton9e409562010-07-28 02:04:09 +00004118 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004119 child_is_deref_of_parent = false;
4120 bool tmp_child_is_deref_of_parent = false;
4121 return GetChildClangTypeAtIndex (exe_ctx,
4122 ast,
4123 parent_name,
4124 pointer_type->getPointeeType().getAsOpaquePtr(),
4125 idx,
4126 transparent_pointers,
4127 omit_empty_base_classes,
4128 ignore_array_bounds,
4129 child_name,
4130 child_byte_size,
4131 child_byte_offset,
4132 child_bitfield_bit_size,
4133 child_bitfield_bit_offset,
4134 child_is_base_class,
4135 tmp_child_is_deref_of_parent);
4136 }
4137 else
4138 {
4139 child_is_deref_of_parent = true;
4140 if (parent_name)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004141 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004142 child_name.assign(1, '*');
4143 child_name += parent_name;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004144 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004145
Greg Clayton4ef877f2012-12-06 02:33:54 +00004146 // We have a pointer to an simple type
4147 if (idx == 0 && GetCompleteQualType(ast, pointee_type))
4148 {
4149 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
4150 assert(clang_type_info.first % 8 == 0);
4151 child_byte_size = clang_type_info.first / 8;
4152 child_byte_offset = 0;
4153 return pointee_type.getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004154 }
Greg Clayton9e409562010-07-28 02:04:09 +00004155 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004156 }
4157 break;
Greg Clayton9e409562010-07-28 02:04:09 +00004158
Greg Claytone1a916a2010-07-21 22:12:05 +00004159 case clang::Type::ConstantArray:
Greg Clayton4ef877f2012-12-06 02:33:54 +00004160 case clang::Type::IncompleteArray:
4161 if (ignore_array_bounds || idx_is_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004162 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004163 const ArrayType *array = cast<ArrayType>(parent_qual_type.getTypePtr());
4164 if (array)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004165 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004166 if (GetCompleteQualType (ast, array->getElementType()))
4167 {
4168 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Greg Clayton4ef877f2012-12-06 02:33:54 +00004169
Greg Clayton6beaaa62011-01-17 03:46:26 +00004170 char element_name[64];
4171 ::snprintf (element_name, sizeof (element_name), "[%u]", idx);
Greg Clayton4ef877f2012-12-06 02:33:54 +00004172
Greg Clayton6beaaa62011-01-17 03:46:26 +00004173 child_name.assign(element_name);
4174 assert(field_type_info.first % 8 == 0);
4175 child_byte_size = field_type_info.first / 8;
Greg Claytondaf515f2011-07-09 20:12:33 +00004176 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
Greg Clayton6beaaa62011-01-17 03:46:26 +00004177 return array->getElementType().getAsOpaquePtr();
4178 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004179 }
4180 }
4181 break;
Greg Clayton4ef877f2012-12-06 02:33:54 +00004182
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004183
Greg Clayton4ef877f2012-12-06 02:33:54 +00004184 case clang::Type::Pointer:
4185 if (idx_is_valid)
4186 {
4187 const PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr());
4188 QualType pointee_type = pointer_type->getPointeeType();
4189
4190 // Don't dereference "void *" pointers
4191 if (pointee_type->isVoidType())
4192 return NULL;
4193
4194 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004195 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004196 child_is_deref_of_parent = false;
4197 bool tmp_child_is_deref_of_parent = false;
4198 return GetChildClangTypeAtIndex (exe_ctx,
4199 ast,
4200 parent_name,
4201 pointer_type->getPointeeType().getAsOpaquePtr(),
4202 idx,
4203 transparent_pointers,
4204 omit_empty_base_classes,
4205 ignore_array_bounds,
4206 child_name,
4207 child_byte_size,
4208 child_byte_offset,
4209 child_bitfield_bit_size,
4210 child_bitfield_bit_offset,
4211 child_is_base_class,
4212 tmp_child_is_deref_of_parent);
4213 }
4214 else
4215 {
4216 child_is_deref_of_parent = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004217
Greg Clayton4ef877f2012-12-06 02:33:54 +00004218 if (parent_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004219 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004220 child_name.assign(1, '*');
4221 child_name += parent_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004222 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004223
4224 // We have a pointer to an simple type
4225 if (idx == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004226 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004227 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
4228 assert(clang_type_info.first % 8 == 0);
4229 child_byte_size = clang_type_info.first / 8;
4230 child_byte_offset = 0;
4231 return pointee_type.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004232 }
4233 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004234 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00004235 break;
4236
4237 case clang::Type::LValueReference:
4238 case clang::Type::RValueReference:
4239 if (idx_is_valid)
4240 {
4241 const ReferenceType *reference_type = cast<ReferenceType>(parent_qual_type.getTypePtr());
4242 QualType pointee_type(reference_type->getPointeeType());
4243 clang_type_t pointee_clang_type = pointee_type.getAsOpaquePtr();
4244 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_clang_type))
4245 {
4246 child_is_deref_of_parent = false;
4247 bool tmp_child_is_deref_of_parent = false;
4248 return GetChildClangTypeAtIndex (exe_ctx,
4249 ast,
4250 parent_name,
4251 pointee_clang_type,
4252 idx,
4253 transparent_pointers,
4254 omit_empty_base_classes,
4255 ignore_array_bounds,
4256 child_name,
4257 child_byte_size,
4258 child_byte_offset,
4259 child_bitfield_bit_size,
4260 child_bitfield_bit_offset,
4261 child_is_base_class,
4262 tmp_child_is_deref_of_parent);
4263 }
4264 else
4265 {
4266 if (parent_name)
4267 {
4268 child_name.assign(1, '&');
4269 child_name += parent_name;
4270 }
4271
4272 // We have a pointer to an simple type
4273 if (idx == 0)
4274 {
4275 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
4276 assert(clang_type_info.first % 8 == 0);
4277 child_byte_size = clang_type_info.first / 8;
4278 child_byte_offset = 0;
4279 return pointee_type.getAsOpaquePtr();
4280 }
4281 }
4282 }
4283 break;
4284
4285 case clang::Type::Typedef:
4286 return GetChildClangTypeAtIndex (exe_ctx,
4287 ast,
4288 parent_name,
4289 cast<TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
4290 idx,
4291 transparent_pointers,
4292 omit_empty_base_classes,
4293 ignore_array_bounds,
4294 child_name,
4295 child_byte_size,
4296 child_byte_offset,
4297 child_bitfield_bit_size,
4298 child_bitfield_bit_offset,
4299 child_is_base_class,
4300 child_is_deref_of_parent);
4301 break;
4302
4303 case clang::Type::Elaborated:
4304 return GetChildClangTypeAtIndex (exe_ctx,
4305 ast,
4306 parent_name,
4307 cast<ElaboratedType>(parent_qual_type)->getNamedType().getAsOpaquePtr(),
4308 idx,
4309 transparent_pointers,
4310 omit_empty_base_classes,
4311 ignore_array_bounds,
4312 child_name,
4313 child_byte_size,
4314 child_byte_offset,
4315 child_bitfield_bit_size,
4316 child_bitfield_bit_offset,
4317 child_is_base_class,
4318 child_is_deref_of_parent);
4319
4320 default:
4321 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004322 }
Greg Clayton19503a22010-07-23 15:37:46 +00004323 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004324}
4325
4326static inline bool
4327BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
4328{
Greg Clayton6beaaa62011-01-17 03:46:26 +00004329 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004330}
4331
4332static uint32_t
4333GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
4334{
4335 uint32_t num_bases = 0;
4336 if (cxx_record_decl)
4337 {
4338 if (omit_empty_base_classes)
4339 {
4340 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4341 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4342 base_class != base_class_end;
4343 ++base_class)
4344 {
4345 // Skip empty base classes
4346 if (omit_empty_base_classes)
4347 {
4348 if (BaseSpecifierIsEmpty (base_class))
4349 continue;
4350 }
4351 ++num_bases;
4352 }
4353 }
4354 else
4355 num_bases = cxx_record_decl->getNumBases();
4356 }
4357 return num_bases;
4358}
4359
4360
4361static uint32_t
4362GetIndexForRecordBase
4363(
4364 const RecordDecl *record_decl,
4365 const CXXBaseSpecifier *base_spec,
4366 bool omit_empty_base_classes
4367)
4368{
4369 uint32_t child_idx = 0;
4370
4371 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4372
4373// const char *super_name = record_decl->getNameAsCString();
4374// const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString();
4375// printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
4376//
4377 if (cxx_record_decl)
4378 {
4379 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4380 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4381 base_class != base_class_end;
4382 ++base_class)
4383 {
4384 if (omit_empty_base_classes)
4385 {
4386 if (BaseSpecifierIsEmpty (base_class))
4387 continue;
4388 }
4389
4390// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
4391// child_idx,
4392// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4393//
4394//
4395 if (base_class == base_spec)
4396 return child_idx;
4397 ++child_idx;
4398 }
4399 }
4400
4401 return UINT32_MAX;
4402}
4403
4404
4405static uint32_t
4406GetIndexForRecordChild
4407(
4408 const RecordDecl *record_decl,
4409 NamedDecl *canonical_decl,
4410 bool omit_empty_base_classes
4411)
4412{
4413 uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes);
4414
4415// const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4416//
4417//// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString());
4418// if (cxx_record_decl)
4419// {
4420// CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4421// for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4422// base_class != base_class_end;
4423// ++base_class)
4424// {
4425// if (omit_empty_base_classes)
4426// {
4427// if (BaseSpecifierIsEmpty (base_class))
4428// continue;
4429// }
4430//
4431//// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
4432//// record_decl->getNameAsCString(),
4433//// canonical_decl->getNameAsCString(),
4434//// child_idx,
4435//// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4436//
4437//
4438// CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4439// if (curr_base_class_decl == canonical_decl)
4440// {
4441// return child_idx;
4442// }
4443// ++child_idx;
4444// }
4445// }
4446//
4447// const uint32_t num_bases = child_idx;
4448 RecordDecl::field_iterator field, field_end;
4449 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4450 field != field_end;
4451 ++field, ++child_idx)
4452 {
4453// printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n",
4454// record_decl->getNameAsCString(),
4455// canonical_decl->getNameAsCString(),
4456// child_idx - num_bases,
4457// field->getNameAsCString());
4458
4459 if (field->getCanonicalDecl() == canonical_decl)
4460 return child_idx;
4461 }
4462
4463 return UINT32_MAX;
4464}
4465
4466// Look for a child member (doesn't include base classes, but it does include
4467// their members) in the type hierarchy. Returns an index path into "clang_type"
4468// on how to reach the appropriate member.
4469//
4470// class A
4471// {
4472// public:
4473// int m_a;
4474// int m_b;
4475// };
4476//
4477// class B
4478// {
4479// };
4480//
4481// class C :
4482// public B,
4483// public A
4484// {
4485// };
4486//
4487// If we have a clang type that describes "class C", and we wanted to looked
4488// "m_b" in it:
4489//
4490// With omit_empty_base_classes == false we would get an integer array back with:
4491// { 1, 1 }
4492// The first index 1 is the child index for "class A" within class C
4493// The second index 1 is the child index for "m_b" within class A
4494//
4495// With omit_empty_base_classes == true we would get an integer array back with:
4496// { 0, 1 }
4497// 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)
4498// The second index 1 is the child index for "m_b" within class A
4499
4500size_t
4501ClangASTContext::GetIndexOfChildMemberWithName
4502(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004503 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004504 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004505 const char *name,
4506 bool omit_empty_base_classes,
4507 std::vector<uint32_t>& child_indexes
4508)
4509{
4510 if (clang_type && name && name[0])
4511 {
4512 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00004513 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4514 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004515 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004516 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004517 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004518 {
4519 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4520 const RecordDecl *record_decl = record_type->getDecl();
4521
4522 assert(record_decl);
4523 uint32_t child_idx = 0;
4524
4525 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4526
4527 // Try and find a field that matches NAME
4528 RecordDecl::field_iterator field, field_end;
4529 StringRef name_sref(name);
4530 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4531 field != field_end;
4532 ++field, ++child_idx)
4533 {
4534 if (field->getName().equals (name_sref))
4535 {
4536 // We have to add on the number of base classes to this index!
4537 child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
4538 return child_indexes.size();
4539 }
4540 }
4541
4542 if (cxx_record_decl)
4543 {
4544 const RecordDecl *parent_record_decl = cxx_record_decl;
4545
4546 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
4547
4548 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
4549 // Didn't find things easily, lets let clang do its thang...
Sean Callanancc427fa2011-07-30 02:42:06 +00004550 IdentifierInfo & ident_ref = ast->Idents.get(name_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004551 DeclarationName decl_name(&ident_ref);
4552
4553 CXXBasePaths paths;
4554 if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember,
4555 decl_name.getAsOpaquePtr(),
4556 paths))
4557 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004558 CXXBasePaths::const_paths_iterator path, path_end = paths.end();
4559 for (path = paths.begin(); path != path_end; ++path)
4560 {
4561 const size_t num_path_elements = path->size();
4562 for (size_t e=0; e<num_path_elements; ++e)
4563 {
4564 CXXBasePathElement elem = (*path)[e];
4565
4566 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
4567 if (child_idx == UINT32_MAX)
4568 {
4569 child_indexes.clear();
4570 return 0;
4571 }
4572 else
4573 {
4574 child_indexes.push_back (child_idx);
4575 parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl());
4576 }
4577 }
Sean Callanan5deaa4c2012-12-21 21:34:42 +00004578 for (NamedDecl *path_decl : path->Decls)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004579 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00004580 child_idx = GetIndexForRecordChild (parent_record_decl, path_decl, omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004581 if (child_idx == UINT32_MAX)
4582 {
4583 child_indexes.clear();
4584 return 0;
4585 }
4586 else
4587 {
4588 child_indexes.push_back (child_idx);
4589 }
4590 }
4591 }
4592 return child_indexes.size();
4593 }
4594 }
4595
4596 }
4597 break;
4598
Greg Clayton9e409562010-07-28 02:04:09 +00004599 case clang::Type::ObjCObject:
4600 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004601 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004602 {
4603 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004604 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004605 assert (objc_class_type);
4606 if (objc_class_type)
4607 {
4608 uint32_t child_idx = 0;
4609 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4610
4611 if (class_interface_decl)
4612 {
4613 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4614 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4615
Greg Clayton6ba78152010-09-18 02:11:07 +00004616 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00004617 {
4618 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4619
4620 if (ivar_decl->getName().equals (name_sref))
4621 {
4622 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4623 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4624 ++child_idx;
4625
4626 child_indexes.push_back (child_idx);
4627 return child_indexes.size();
4628 }
4629 }
4630
4631 if (superclass_interface_decl)
4632 {
4633 // The super class index is always zero for ObjC classes,
4634 // so we push it onto the child indexes in case we find
4635 // an ivar in our superclass...
4636 child_indexes.push_back (0);
4637
Greg Clayton6beaaa62011-01-17 03:46:26 +00004638 if (GetIndexOfChildMemberWithName (ast,
4639 ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00004640 name,
4641 omit_empty_base_classes,
4642 child_indexes))
4643 {
4644 // We did find an ivar in a superclass so just
4645 // return the results!
4646 return child_indexes.size();
4647 }
4648
4649 // We didn't find an ivar matching "name" in our
4650 // superclass, pop the superclass zero index that
4651 // we pushed on above.
4652 child_indexes.pop_back();
4653 }
4654 }
4655 }
4656 }
4657 break;
4658
4659 case clang::Type::ObjCObjectPointer:
4660 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004661 return GetIndexOfChildMemberWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004662 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4663 name,
4664 omit_empty_base_classes,
4665 child_indexes);
4666 }
4667 break;
4668
4669
Greg Claytone1a916a2010-07-21 22:12:05 +00004670 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004671 {
4672// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4673// const uint64_t element_count = array->getSize().getLimitedValue();
4674//
4675// if (idx < element_count)
4676// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004677// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004678//
4679// char element_name[32];
4680// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4681//
4682// child_name.assign(element_name);
4683// assert(field_type_info.first % 8 == 0);
4684// child_byte_size = field_type_info.first / 8;
4685// child_byte_offset = idx * child_byte_size;
4686// return array->getElementType().getAsOpaquePtr();
4687// }
4688 }
4689 break;
4690
Greg Claytone1a916a2010-07-21 22:12:05 +00004691// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004692// {
4693// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4694// QualType pointee_type = mem_ptr_type->getPointeeType();
4695//
4696// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4697// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004698// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004699// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4700// name);
4701// }
4702// }
4703// break;
4704//
Greg Claytone1a916a2010-07-21 22:12:05 +00004705 case clang::Type::LValueReference:
4706 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004707 {
Sean Callanan78e37602011-01-27 04:42:51 +00004708 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004709 QualType pointee_type = reference_type->getPointeeType();
4710
4711 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4712 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004713 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004714 reference_type->getPointeeType().getAsOpaquePtr(),
4715 name,
4716 omit_empty_base_classes,
4717 child_indexes);
4718 }
4719 }
4720 break;
4721
Greg Claytone1a916a2010-07-21 22:12:05 +00004722 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004723 {
Sean Callanan78e37602011-01-27 04:42:51 +00004724 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004725 QualType pointee_type = pointer_type->getPointeeType();
4726
4727 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4728 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004729 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004730 pointer_type->getPointeeType().getAsOpaquePtr(),
4731 name,
4732 omit_empty_base_classes,
4733 child_indexes);
4734 }
4735 else
4736 {
4737// if (parent_name)
4738// {
4739// child_name.assign(1, '*');
4740// child_name += parent_name;
4741// }
4742//
4743// // We have a pointer to an simple type
4744// if (idx == 0)
4745// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004746// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004747// assert(clang_type_info.first % 8 == 0);
4748// child_byte_size = clang_type_info.first / 8;
4749// child_byte_offset = 0;
4750// return pointee_type.getAsOpaquePtr();
4751// }
4752 }
4753 }
4754 break;
4755
Greg Claytone1a916a2010-07-21 22:12:05 +00004756 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00004757 return GetIndexOfChildMemberWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00004758 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004759 name,
4760 omit_empty_base_classes,
4761 child_indexes);
4762
Greg Clayton4b63a5c2013-01-04 18:10:18 +00004763 case clang::Type::Elaborated:
4764 return GetIndexOfChildMemberWithName (ast,
4765 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
4766 name,
4767 omit_empty_base_classes,
4768 child_indexes);
4769
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004770 default:
4771 break;
4772 }
4773 }
4774 return 0;
4775}
4776
4777
4778// Get the index of the child of "clang_type" whose name matches. This function
4779// doesn't descend into the children, but only looks one level deep and name
4780// matches can include base class names.
4781
4782uint32_t
4783ClangASTContext::GetIndexOfChildWithName
4784(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004785 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004786 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004787 const char *name,
4788 bool omit_empty_base_classes
4789)
4790{
4791 if (clang_type && name && name[0])
4792 {
4793 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00004794
Greg Clayton737b9322010-09-13 03:32:57 +00004795 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Clayton9e409562010-07-28 02:04:09 +00004796
Greg Clayton737b9322010-09-13 03:32:57 +00004797 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004798 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004799 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004800 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004801 {
4802 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4803 const RecordDecl *record_decl = record_type->getDecl();
4804
4805 assert(record_decl);
4806 uint32_t child_idx = 0;
4807
4808 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4809
4810 if (cxx_record_decl)
4811 {
4812 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4813 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4814 base_class != base_class_end;
4815 ++base_class)
4816 {
4817 // Skip empty base classes
4818 CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4819 if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false)
4820 continue;
4821
Greg Clayton84db9102012-03-26 23:03:23 +00004822 std::string base_class_type_name (ClangASTType::GetTypeNameForQualType(ast, base_class->getType()));
Greg Claytone3055942011-06-30 02:28:26 +00004823 if (base_class_type_name.compare (name) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004824 return child_idx;
4825 ++child_idx;
4826 }
4827 }
4828
4829 // Try and find a field that matches NAME
4830 RecordDecl::field_iterator field, field_end;
4831 StringRef name_sref(name);
4832 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4833 field != field_end;
4834 ++field, ++child_idx)
4835 {
4836 if (field->getName().equals (name_sref))
4837 return child_idx;
4838 }
4839
4840 }
4841 break;
4842
Greg Clayton9e409562010-07-28 02:04:09 +00004843 case clang::Type::ObjCObject:
4844 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004845 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004846 {
4847 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004848 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004849 assert (objc_class_type);
4850 if (objc_class_type)
4851 {
4852 uint32_t child_idx = 0;
4853 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4854
4855 if (class_interface_decl)
4856 {
4857 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4858 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4859
Jim Ingham2f355a72012-10-04 22:22:16 +00004860 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00004861 {
4862 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4863
4864 if (ivar_decl->getName().equals (name_sref))
4865 {
4866 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4867 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4868 ++child_idx;
4869
4870 return child_idx;
4871 }
4872 }
4873
4874 if (superclass_interface_decl)
4875 {
4876 if (superclass_interface_decl->getName().equals (name_sref))
4877 return 0;
4878 }
4879 }
4880 }
4881 }
4882 break;
4883
4884 case clang::Type::ObjCObjectPointer:
4885 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004886 return GetIndexOfChildWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004887 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4888 name,
4889 omit_empty_base_classes);
4890 }
4891 break;
4892
Greg Claytone1a916a2010-07-21 22:12:05 +00004893 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004894 {
4895// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4896// const uint64_t element_count = array->getSize().getLimitedValue();
4897//
4898// if (idx < element_count)
4899// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004900// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004901//
4902// char element_name[32];
4903// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4904//
4905// child_name.assign(element_name);
4906// assert(field_type_info.first % 8 == 0);
4907// child_byte_size = field_type_info.first / 8;
4908// child_byte_offset = idx * child_byte_size;
4909// return array->getElementType().getAsOpaquePtr();
4910// }
4911 }
4912 break;
4913
Greg Claytone1a916a2010-07-21 22:12:05 +00004914// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004915// {
4916// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4917// QualType pointee_type = mem_ptr_type->getPointeeType();
4918//
4919// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4920// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004921// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004922// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4923// name);
4924// }
4925// }
4926// break;
4927//
Greg Claytone1a916a2010-07-21 22:12:05 +00004928 case clang::Type::LValueReference:
4929 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004930 {
Sean Callanan78e37602011-01-27 04:42:51 +00004931 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004932 QualType pointee_type = reference_type->getPointeeType();
4933
4934 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4935 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004936 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004937 reference_type->getPointeeType().getAsOpaquePtr(),
4938 name,
4939 omit_empty_base_classes);
4940 }
4941 }
4942 break;
4943
Greg Claytone1a916a2010-07-21 22:12:05 +00004944 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004945 {
Sean Callanan78e37602011-01-27 04:42:51 +00004946 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004947 QualType pointee_type = pointer_type->getPointeeType();
4948
4949 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4950 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004951 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004952 pointer_type->getPointeeType().getAsOpaquePtr(),
4953 name,
4954 omit_empty_base_classes);
4955 }
4956 else
4957 {
4958// if (parent_name)
4959// {
4960// child_name.assign(1, '*');
4961// child_name += parent_name;
4962// }
4963//
4964// // We have a pointer to an simple type
4965// if (idx == 0)
4966// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004967// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004968// assert(clang_type_info.first % 8 == 0);
4969// child_byte_size = clang_type_info.first / 8;
4970// child_byte_offset = 0;
4971// return pointee_type.getAsOpaquePtr();
4972// }
4973 }
4974 }
4975 break;
4976
Greg Clayton4b63a5c2013-01-04 18:10:18 +00004977 case clang::Type::Elaborated:
4978 return GetIndexOfChildWithName (ast,
4979 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
4980 name,
4981 omit_empty_base_classes);
4982
Greg Claytone1a916a2010-07-21 22:12:05 +00004983 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00004984 return GetIndexOfChildWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00004985 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004986 name,
4987 omit_empty_base_classes);
4988
4989 default:
4990 break;
4991 }
4992 }
4993 return UINT32_MAX;
4994}
4995
4996#pragma mark TagType
4997
4998bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00004999ClangASTContext::SetTagTypeKind (clang_type_t tag_clang_type, int kind)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005000{
5001 if (tag_clang_type)
5002 {
5003 QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00005004 const clang::Type *clang_type = tag_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005005 if (clang_type)
5006 {
Sean Callanan78e37602011-01-27 04:42:51 +00005007 const TagType *tag_type = dyn_cast<TagType>(clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005008 if (tag_type)
5009 {
5010 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl());
5011 if (tag_decl)
5012 {
5013 tag_decl->setTagKind ((TagDecl::TagKind)kind);
5014 return true;
5015 }
5016 }
5017 }
5018 }
5019 return false;
5020}
5021
5022
5023#pragma mark DeclContext Functions
5024
5025DeclContext *
Greg Clayton1be10fc2010-09-29 01:12:09 +00005026ClangASTContext::GetDeclContextForType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005027{
5028 if (clang_type == NULL)
5029 return NULL;
5030
5031 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005032 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5033 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005034 {
Sean Callanancc427fa2011-07-30 02:42:06 +00005035 case clang::Type::UnaryTransform: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005036 case clang::Type::FunctionNoProto: break;
5037 case clang::Type::FunctionProto: break;
5038 case clang::Type::IncompleteArray: break;
5039 case clang::Type::VariableArray: break;
5040 case clang::Type::ConstantArray: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005041 case clang::Type::DependentSizedArray: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005042 case clang::Type::ExtVector: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005043 case clang::Type::DependentSizedExtVector: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005044 case clang::Type::Vector: break;
5045 case clang::Type::Builtin: break;
5046 case clang::Type::BlockPointer: break;
5047 case clang::Type::Pointer: break;
5048 case clang::Type::LValueReference: break;
5049 case clang::Type::RValueReference: break;
5050 case clang::Type::MemberPointer: break;
5051 case clang::Type::Complex: break;
5052 case clang::Type::ObjCObject: break;
5053 case clang::Type::ObjCInterface: return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface();
5054 case clang::Type::ObjCObjectPointer: return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr());
5055 case clang::Type::Record: return cast<RecordType>(qual_type)->getDecl();
5056 case clang::Type::Enum: return cast<EnumType>(qual_type)->getDecl();
Sean Callanan48114472010-12-13 01:26:27 +00005057 case clang::Type::Typedef: return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00005058 case clang::Type::Elaborated: return ClangASTContext::GetDeclContextForType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00005059 case clang::Type::TypeOfExpr: break;
5060 case clang::Type::TypeOf: break;
5061 case clang::Type::Decltype: break;
5062 //case clang::Type::QualifiedName: break;
5063 case clang::Type::TemplateSpecialization: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005064 case clang::Type::DependentTemplateSpecialization: break;
5065 case clang::Type::TemplateTypeParm: break;
5066 case clang::Type::SubstTemplateTypeParm: break;
5067 case clang::Type::SubstTemplateTypeParmPack:break;
5068 case clang::Type::PackExpansion: break;
5069 case clang::Type::UnresolvedUsing: break;
5070 case clang::Type::Paren: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005071 case clang::Type::Attributed: break;
5072 case clang::Type::Auto: break;
5073 case clang::Type::InjectedClassName: break;
5074 case clang::Type::DependentName: break;
Greg Claytonea3e7d52011-10-08 00:49:15 +00005075 case clang::Type::Atomic: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005076 }
5077 // No DeclContext in this type...
5078 return NULL;
5079}
5080
5081#pragma mark Namespace Declarations
5082
5083NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00005084ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005085{
Greg Clayton030a2042011-10-14 21:34:45 +00005086 NamespaceDecl *namespace_decl = NULL;
Greg Clayton9d3d6882011-10-31 23:51:19 +00005087 ASTContext *ast = getASTContext();
5088 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
5089 if (decl_ctx == NULL)
5090 decl_ctx = translation_unit_decl;
5091
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005092 if (name)
5093 {
Greg Clayton030a2042011-10-14 21:34:45 +00005094 IdentifierInfo &identifier_info = ast->Idents.get(name);
5095 DeclarationName decl_name (&identifier_info);
5096 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00005097 for (NamedDecl *decl : result)
Greg Clayton030a2042011-10-14 21:34:45 +00005098 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00005099 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
Greg Clayton030a2042011-10-14 21:34:45 +00005100 if (namespace_decl)
5101 return namespace_decl;
5102 }
5103
Sean Callanan5b26f272012-02-04 08:49:35 +00005104 namespace_decl = NamespaceDecl::Create(*ast,
5105 decl_ctx,
5106 false,
5107 SourceLocation(),
5108 SourceLocation(),
5109 &identifier_info,
5110 NULL);
Greg Clayton030a2042011-10-14 21:34:45 +00005111
Greg Clayton9d3d6882011-10-31 23:51:19 +00005112 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005113 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00005114 else
5115 {
5116 if (decl_ctx == translation_unit_decl)
5117 {
5118 namespace_decl = translation_unit_decl->getAnonymousNamespace();
5119 if (namespace_decl)
5120 return namespace_decl;
5121
Sean Callanan5b26f272012-02-04 08:49:35 +00005122 namespace_decl = NamespaceDecl::Create(*ast,
5123 decl_ctx,
5124 false,
5125 SourceLocation(),
5126 SourceLocation(),
5127 NULL,
5128 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00005129 translation_unit_decl->setAnonymousNamespace (namespace_decl);
5130 translation_unit_decl->addDecl (namespace_decl);
5131 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
5132 }
5133 else
5134 {
5135 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
5136 if (parent_namespace_decl)
5137 {
5138 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
5139 if (namespace_decl)
5140 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00005141 namespace_decl = NamespaceDecl::Create(*ast,
5142 decl_ctx,
5143 false,
5144 SourceLocation(),
5145 SourceLocation(),
5146 NULL,
5147 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00005148 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
5149 parent_namespace_decl->addDecl (namespace_decl);
5150 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
5151 }
5152 else
5153 {
5154 // BAD!!!
5155 }
5156 }
5157
5158
5159 if (namespace_decl)
5160 {
5161 // If we make it here, we are creating the anonymous namespace decl
5162 // for the first time, so we need to do the using directive magic
5163 // like SEMA does
5164 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast,
5165 decl_ctx,
5166 SourceLocation(),
5167 SourceLocation(),
5168 NestedNameSpecifierLoc(),
5169 SourceLocation(),
5170 namespace_decl,
5171 decl_ctx);
5172 using_directive_decl->setImplicit();
5173 decl_ctx->addDecl(using_directive_decl);
5174 }
5175 }
5176#ifdef LLDB_CONFIGURATION_DEBUG
5177 VerifyDecl(namespace_decl);
5178#endif
Greg Clayton030a2042011-10-14 21:34:45 +00005179 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005180}
5181
5182
5183#pragma mark Function Types
5184
5185FunctionDecl *
Greg Clayton147e1fa2011-10-14 22:47:18 +00005186ClangASTContext::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 +00005187{
Greg Clayton147e1fa2011-10-14 22:47:18 +00005188 FunctionDecl *func_decl = NULL;
5189 ASTContext *ast = getASTContext();
5190 if (decl_ctx == NULL)
5191 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005192
Greg Clayton147e1fa2011-10-14 22:47:18 +00005193 if (name && name[0])
5194 {
5195 func_decl = FunctionDecl::Create (*ast,
5196 decl_ctx,
5197 SourceLocation(),
5198 SourceLocation(),
5199 DeclarationName (&ast->Idents.get(name)),
5200 QualType::getFromOpaquePtr(function_clang_type),
5201 NULL,
5202 (FunctionDecl::StorageClass)storage,
5203 (FunctionDecl::StorageClass)storage,
5204 is_inline);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005205 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00005206 else
5207 {
5208 func_decl = FunctionDecl::Create (*ast,
5209 decl_ctx,
5210 SourceLocation(),
5211 SourceLocation(),
5212 DeclarationName (),
5213 QualType::getFromOpaquePtr(function_clang_type),
5214 NULL,
5215 (FunctionDecl::StorageClass)storage,
5216 (FunctionDecl::StorageClass)storage,
5217 is_inline);
5218 }
5219 if (func_decl)
5220 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005221
5222#ifdef LLDB_CONFIGURATION_DEBUG
5223 VerifyDecl(func_decl);
5224#endif
5225
Greg Clayton147e1fa2011-10-14 22:47:18 +00005226 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005227}
5228
Greg Clayton1be10fc2010-09-29 01:12:09 +00005229clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00005230ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00005231 clang_type_t result_type,
5232 clang_type_t *args,
Sean Callananc81256a2010-09-16 20:40:25 +00005233 unsigned num_args,
5234 bool is_variadic,
5235 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005236{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005237 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005238 std::vector<QualType> qual_type_args;
5239 for (unsigned i=0; i<num_args; ++i)
5240 qual_type_args.push_back (QualType::getFromOpaquePtr(args[i]));
5241
5242 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00005243 FunctionProtoType::ExtProtoInfo proto_info;
5244 proto_info.Variadic = is_variadic;
Sean Callananfb0b7582011-03-15 00:17:19 +00005245 proto_info.ExceptionSpecType = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005246 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00005247 proto_info.RefQualifier = RQ_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005248 proto_info.NumExceptions = 0;
5249 proto_info.Exceptions = NULL;
5250
Greg Clayton147e1fa2011-10-14 22:47:18 +00005251 return ast->getFunctionType (QualType::getFromOpaquePtr(result_type),
5252 qual_type_args.empty() ? NULL : &qual_type_args.front(),
5253 qual_type_args.size(),
5254 proto_info).getAsOpaquePtr(); // NoReturn);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005255}
5256
5257ParmVarDecl *
Greg Clayton1be10fc2010-09-29 01:12:09 +00005258ClangASTContext::CreateParameterDeclaration (const char *name, clang_type_t param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005259{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005260 ASTContext *ast = getASTContext();
5261 assert (ast != NULL);
5262 return ParmVarDecl::Create(*ast,
5263 ast->getTranslationUnitDecl(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005264 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005265 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00005266 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callananc81256a2010-09-16 20:40:25 +00005267 QualType::getFromOpaquePtr(param_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005268 NULL,
5269 (VarDecl::StorageClass)storage,
5270 (VarDecl::StorageClass)storage,
5271 0);
5272}
5273
5274void
5275ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
5276{
5277 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00005278 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005279}
5280
5281
5282#pragma mark Array Types
5283
Greg Clayton1be10fc2010-09-29 01:12:09 +00005284clang_type_t
Greg Clayton4ef877f2012-12-06 02:33:54 +00005285ClangASTContext::CreateArrayType (clang_type_t element_type, size_t element_count)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005286{
5287 if (element_type)
5288 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00005289 ASTContext *ast = getASTContext();
5290 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005291 llvm::APInt ap_element_count (64, element_count);
Greg Clayton4ef877f2012-12-06 02:33:54 +00005292 if (element_count == 0)
5293 {
5294 return ast->getIncompleteArrayType(QualType::getFromOpaquePtr(element_type),
5295 ArrayType::Normal,
5296 0).getAsOpaquePtr();
5297
5298 }
5299 else
5300 {
5301 return ast->getConstantArrayType(QualType::getFromOpaquePtr(element_type),
5302 ap_element_count,
5303 ArrayType::Normal,
5304 0).getAsOpaquePtr(); // ElemQuals
5305 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005306 }
5307 return NULL;
5308}
5309
5310
5311#pragma mark TagDecl
5312
5313bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005314ClangASTContext::StartTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005315{
5316 if (clang_type)
5317 {
5318 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00005319 const clang::Type *t = qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005320 if (t)
5321 {
Sean Callanan78e37602011-01-27 04:42:51 +00005322 const TagType *tag_type = dyn_cast<TagType>(t);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005323 if (tag_type)
5324 {
5325 TagDecl *tag_decl = tag_type->getDecl();
5326 if (tag_decl)
5327 {
5328 tag_decl->startDefinition();
5329 return true;
5330 }
5331 }
Sean Callanan5b26f272012-02-04 08:49:35 +00005332
5333 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(t);
5334 if (object_type)
5335 {
5336 ObjCInterfaceDecl *interface_decl = object_type->getInterface();
5337 if (interface_decl)
5338 {
5339 interface_decl->startDefinition();
5340 return true;
5341 }
5342 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005343 }
5344 }
5345 return false;
5346}
5347
5348bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005349ClangASTContext::CompleteTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005350{
5351 if (clang_type)
5352 {
5353 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton14372242010-09-29 03:44:17 +00005354
5355 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5356
5357 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005358 {
Greg Clayton14372242010-09-29 03:44:17 +00005359 cxx_record_decl->completeDefinition();
5360
5361 return true;
5362 }
5363
5364 const EnumType *enum_type = dyn_cast<EnumType>(qual_type.getTypePtr());
5365
5366 if (enum_type)
5367 {
5368 EnumDecl *enum_decl = enum_type->getDecl();
5369
5370 if (enum_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005371 {
Greg Clayton14372242010-09-29 03:44:17 +00005372 /// TODO This really needs to be fixed.
5373
5374 unsigned NumPositiveBits = 1;
5375 unsigned NumNegativeBits = 0;
5376
Greg Clayton6beaaa62011-01-17 03:46:26 +00005377 ASTContext *ast = getASTContext();
Greg Claytone02b8502010-10-12 04:29:14 +00005378
5379 QualType promotion_qual_type;
5380 // If the enum integer type is less than an integer in bit width,
5381 // then we must promote it to an integer size.
Greg Clayton6beaaa62011-01-17 03:46:26 +00005382 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
Greg Claytone02b8502010-10-12 04:29:14 +00005383 {
5384 if (enum_decl->getIntegerType()->isSignedIntegerType())
Greg Clayton6beaaa62011-01-17 03:46:26 +00005385 promotion_qual_type = ast->IntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005386 else
Greg Clayton6beaaa62011-01-17 03:46:26 +00005387 promotion_qual_type = ast->UnsignedIntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005388 }
5389 else
5390 promotion_qual_type = enum_decl->getIntegerType();
5391
5392 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
Greg Clayton14372242010-09-29 03:44:17 +00005393 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005394 }
5395 }
5396 }
5397 return false;
5398}
5399
5400
5401#pragma mark Enumeration Types
5402
Greg Clayton1be10fc2010-09-29 01:12:09 +00005403clang_type_t
Greg Claytonca512b32011-01-14 04:54:56 +00005404ClangASTContext::CreateEnumerationType
5405(
5406 const char *name,
5407 DeclContext *decl_ctx,
5408 const Declaration &decl,
5409 clang_type_t integer_qual_type
5410)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005411{
5412 // TODO: Do something intelligent with the Declaration object passed in
5413 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005414 ASTContext *ast = getASTContext();
5415 assert (ast != NULL);
Greg Claytone02b8502010-10-12 04:29:14 +00005416
5417 // TODO: ask about these...
5418// const bool IsScoped = false;
5419// const bool IsFixed = false;
5420
Greg Clayton6beaaa62011-01-17 03:46:26 +00005421 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00005422 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00005423 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00005424 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005425 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callanan48114472010-12-13 01:26:27 +00005426 NULL,
5427 false, // IsScoped
5428 false, // IsScopedUsingClassTag
5429 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00005430
5431
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005432 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00005433 {
5434 // TODO: check if we should be setting the promotion type too?
5435 enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00005436
5437 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
5438
Greg Clayton6beaaa62011-01-17 03:46:26 +00005439 return ast->getTagDeclType(enum_decl).getAsOpaquePtr();
Greg Clayton83ff3892010-09-12 23:17:56 +00005440 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005441 return NULL;
5442}
5443
Greg Clayton1be10fc2010-09-29 01:12:09 +00005444clang_type_t
5445ClangASTContext::GetEnumerationIntegerType (clang_type_t enum_clang_type)
5446{
5447 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5448
Sean Callanan78e37602011-01-27 04:42:51 +00005449 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Greg Clayton1be10fc2010-09-29 01:12:09 +00005450 if (clang_type)
5451 {
5452 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5453 if (enum_type)
5454 {
5455 EnumDecl *enum_decl = enum_type->getDecl();
5456 if (enum_decl)
5457 return enum_decl->getIntegerType().getAsOpaquePtr();
5458 }
5459 }
5460 return NULL;
5461}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005462bool
5463ClangASTContext::AddEnumerationValueToEnumerationType
5464(
Greg Clayton1be10fc2010-09-29 01:12:09 +00005465 clang_type_t enum_clang_type,
5466 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005467 const Declaration &decl,
5468 const char *name,
5469 int64_t enum_value,
5470 uint32_t enum_value_bit_size
5471)
5472{
5473 if (enum_clang_type && enumerator_clang_type && name)
5474 {
5475 // TODO: Do something intelligent with the Declaration object passed in
5476 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005477 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005478 IdentifierTable *identifier_table = getIdentifierTable();
5479
Greg Clayton6beaaa62011-01-17 03:46:26 +00005480 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005481 assert (identifier_table != NULL);
5482 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5483
Sean Callanan78e37602011-01-27 04:42:51 +00005484 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005485 if (clang_type)
5486 {
5487 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5488
5489 if (enum_type)
5490 {
5491 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, false);
5492 enum_llvm_apsint = enum_value;
5493 EnumConstantDecl *enumerator_decl =
Greg Clayton6beaaa62011-01-17 03:46:26 +00005494 EnumConstantDecl::Create (*ast,
5495 enum_type->getDecl(),
5496 SourceLocation(),
5497 name ? &identifier_table->get(name) : NULL, // Identifier
5498 QualType::getFromOpaquePtr(enumerator_clang_type),
5499 NULL,
5500 enum_llvm_apsint);
5501
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005502 if (enumerator_decl)
5503 {
5504 enum_type->getDecl()->addDecl(enumerator_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005505
5506#ifdef LLDB_CONFIGURATION_DEBUG
5507 VerifyDecl(enumerator_decl);
5508#endif
5509
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005510 return true;
5511 }
5512 }
5513 }
5514 }
5515 return false;
5516}
5517
5518#pragma mark Pointers & References
5519
Greg Clayton1be10fc2010-09-29 01:12:09 +00005520clang_type_t
5521ClangASTContext::CreatePointerType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005522{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005523 return CreatePointerType (getASTContext(), clang_type);
5524}
5525
5526clang_type_t
5527ClangASTContext::CreatePointerType (clang::ASTContext *ast, clang_type_t clang_type)
5528{
5529 if (ast && clang_type)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005530 {
5531 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5532
Greg Clayton737b9322010-09-13 03:32:57 +00005533 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5534 switch (type_class)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005535 {
5536 case clang::Type::ObjCObject:
5537 case clang::Type::ObjCInterface:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005538 return ast->getObjCObjectPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005539
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005540 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005541 return ast->getPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005542 }
5543 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005544 return NULL;
5545}
5546
Greg Clayton1be10fc2010-09-29 01:12:09 +00005547clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005548ClangASTContext::CreateLValueReferenceType (clang::ASTContext *ast,
5549 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005550{
5551 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005552 return ast->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005553 return NULL;
5554}
5555
Greg Clayton1be10fc2010-09-29 01:12:09 +00005556clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005557ClangASTContext::CreateRValueReferenceType (clang::ASTContext *ast,
5558 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005559{
5560 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005561 return ast->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005562 return NULL;
5563}
5564
Greg Clayton1be10fc2010-09-29 01:12:09 +00005565clang_type_t
5566ClangASTContext::CreateMemberPointerType (clang_type_t clang_pointee_type, clang_type_t clang_class_type)
Greg Clayton9b81a312010-06-12 01:20:30 +00005567{
5568 if (clang_pointee_type && clang_pointee_type)
5569 return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type),
5570 QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr();
5571 return NULL;
5572}
5573
Greg Clayton1a65ae12011-01-25 23:55:37 +00005574uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005575ClangASTContext::GetPointerBitSize ()
5576{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005577 ASTContext *ast = getASTContext();
5578 return ast->getTypeSize(ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005579}
5580
5581bool
Greg Clayton219cf312012-03-30 00:51:13 +00005582ClangASTContext::IsPossibleDynamicType (clang::ASTContext *ast,
5583 clang_type_t clang_type,
5584 clang_type_t *dynamic_pointee_type,
5585 bool check_cplusplus,
5586 bool check_objc)
Greg Claytondea8cb42011-06-29 22:09:02 +00005587{
5588 QualType pointee_qual_type;
5589 if (clang_type)
5590 {
5591 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5592 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5593 bool success = false;
5594 switch (type_class)
5595 {
5596 case clang::Type::Builtin:
Greg Clayton219cf312012-03-30 00:51:13 +00005597 if (check_objc && cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
Greg Claytondea8cb42011-06-29 22:09:02 +00005598 {
5599 if (dynamic_pointee_type)
5600 *dynamic_pointee_type = clang_type;
5601 return true;
5602 }
5603 break;
5604
5605 case clang::Type::ObjCObjectPointer:
Greg Clayton219cf312012-03-30 00:51:13 +00005606 if (check_objc)
5607 {
5608 if (dynamic_pointee_type)
5609 *dynamic_pointee_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5610 return true;
5611 }
5612 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005613
5614 case clang::Type::Pointer:
5615 pointee_qual_type = cast<PointerType>(qual_type)->getPointeeType();
5616 success = true;
5617 break;
5618
5619 case clang::Type::LValueReference:
5620 case clang::Type::RValueReference:
5621 pointee_qual_type = cast<ReferenceType>(qual_type)->getPointeeType();
5622 success = true;
5623 break;
5624
5625 case clang::Type::Typedef:
Greg Clayton70364252012-08-31 18:56:24 +00005626 return ClangASTContext::IsPossibleDynamicType (ast,
5627 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
5628 dynamic_pointee_type,
5629 check_cplusplus,
5630 check_objc);
Sean Callanan912855f2011-08-11 23:56:13 +00005631
5632 case clang::Type::Elaborated:
Greg Clayton70364252012-08-31 18:56:24 +00005633 return ClangASTContext::IsPossibleDynamicType (ast,
5634 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
5635 dynamic_pointee_type,
5636 check_cplusplus,
5637 check_objc);
Sean Callanan912855f2011-08-11 23:56:13 +00005638
Greg Claytondea8cb42011-06-29 22:09:02 +00005639 default:
5640 break;
5641 }
5642
5643 if (success)
5644 {
5645 // Check to make sure what we are pointing too is a possible dynamic C++ type
5646 // We currently accept any "void *" (in case we have a class that has been
5647 // watered down to an opaque pointer) and virtual C++ classes.
5648 const clang::Type::TypeClass pointee_type_class = pointee_qual_type->getTypeClass();
5649 switch (pointee_type_class)
5650 {
5651 case clang::Type::Builtin:
5652 switch (cast<clang::BuiltinType>(pointee_qual_type)->getKind())
5653 {
5654 case clang::BuiltinType::UnknownAny:
5655 case clang::BuiltinType::Void:
5656 if (dynamic_pointee_type)
5657 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5658 return true;
5659
5660 case clang::BuiltinType::NullPtr:
5661 case clang::BuiltinType::Bool:
5662 case clang::BuiltinType::Char_U:
5663 case clang::BuiltinType::UChar:
5664 case clang::BuiltinType::WChar_U:
5665 case clang::BuiltinType::Char16:
5666 case clang::BuiltinType::Char32:
5667 case clang::BuiltinType::UShort:
5668 case clang::BuiltinType::UInt:
5669 case clang::BuiltinType::ULong:
5670 case clang::BuiltinType::ULongLong:
5671 case clang::BuiltinType::UInt128:
5672 case clang::BuiltinType::Char_S:
5673 case clang::BuiltinType::SChar:
5674 case clang::BuiltinType::WChar_S:
5675 case clang::BuiltinType::Short:
5676 case clang::BuiltinType::Int:
5677 case clang::BuiltinType::Long:
5678 case clang::BuiltinType::LongLong:
5679 case clang::BuiltinType::Int128:
5680 case clang::BuiltinType::Float:
5681 case clang::BuiltinType::Double:
5682 case clang::BuiltinType::LongDouble:
5683 case clang::BuiltinType::Dependent:
5684 case clang::BuiltinType::Overload:
5685 case clang::BuiltinType::ObjCId:
5686 case clang::BuiltinType::ObjCClass:
5687 case clang::BuiltinType::ObjCSel:
5688 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00005689 case clang::BuiltinType::Half:
5690 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00005691 case clang::BuiltinType::PseudoObject:
Sean Callanan3d654b32012-09-24 22:25:51 +00005692 case clang::BuiltinType::BuiltinFn:
Greg Claytondea8cb42011-06-29 22:09:02 +00005693 break;
5694 }
5695 break;
5696
5697 case clang::Type::Record:
Greg Clayton219cf312012-03-30 00:51:13 +00005698 if (check_cplusplus)
Greg Claytondea8cb42011-06-29 22:09:02 +00005699 {
5700 CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
5701 if (cxx_record_decl)
5702 {
Greg Clayton70364252012-08-31 18:56:24 +00005703 bool is_complete = cxx_record_decl->isCompleteDefinition();
5704 if (!is_complete)
Sean Callanan6e4100ea2012-09-08 00:49:45 +00005705 is_complete = ClangASTContext::GetCompleteType (ast, pointee_qual_type.getAsOpaquePtr());
Greg Clayton70364252012-08-31 18:56:24 +00005706
5707 if (is_complete)
Greg Claytondea8cb42011-06-29 22:09:02 +00005708 {
5709 success = cxx_record_decl->isDynamicClass();
5710 }
5711 else
5712 {
Greg Clayton70364252012-08-31 18:56:24 +00005713 success = false;
Greg Claytondea8cb42011-06-29 22:09:02 +00005714 }
Greg Clayton70364252012-08-31 18:56:24 +00005715
Greg Claytondea8cb42011-06-29 22:09:02 +00005716 if (success)
5717 {
5718 if (dynamic_pointee_type)
5719 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5720 return true;
5721 }
5722 }
5723 }
5724 break;
5725
5726 case clang::Type::ObjCObject:
5727 case clang::Type::ObjCInterface:
Greg Clayton219cf312012-03-30 00:51:13 +00005728 if (check_objc)
5729 {
5730 if (dynamic_pointee_type)
5731 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5732 return true;
5733 }
5734 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005735
5736 default:
5737 break;
5738 }
5739 }
5740 }
5741 if (dynamic_pointee_type)
5742 *dynamic_pointee_type = NULL;
5743 return false;
5744}
5745
5746
5747bool
Greg Clayton007d5be2011-05-30 00:49:24 +00005748ClangASTContext::IsPossibleCPlusPlusDynamicType (clang::ASTContext *ast, clang_type_t clang_type, clang_type_t *dynamic_pointee_type)
5749{
Greg Clayton219cf312012-03-30 00:51:13 +00005750 return IsPossibleDynamicType (ast,
5751 clang_type,
5752 dynamic_pointee_type,
5753 true, // Check for dynamic C++ types
5754 false); // Check for dynamic ObjC types
Greg Clayton007d5be2011-05-30 00:49:24 +00005755}
5756
Sean Callanan98298012011-10-27 19:41:13 +00005757bool
5758ClangASTContext::IsReferenceType (clang_type_t clang_type, clang_type_t *target_type)
5759{
5760 if (clang_type == NULL)
5761 return false;
5762
5763 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5764 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5765
5766 switch (type_class)
5767 {
5768 case clang::Type::LValueReference:
5769 if (target_type)
5770 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5771 return true;
5772 case clang::Type::RValueReference:
5773 if (target_type)
5774 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5775 return true;
5776 case clang::Type::Typedef:
5777 return ClangASTContext::IsReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5778 case clang::Type::Elaborated:
5779 return ClangASTContext::IsReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5780 default:
5781 break;
5782 }
5783
5784 return false;
5785}
Greg Clayton007d5be2011-05-30 00:49:24 +00005786
5787bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005788ClangASTContext::IsPointerOrReferenceType (clang_type_t clang_type, clang_type_t*target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005789{
5790 if (clang_type == NULL)
5791 return false;
5792
5793 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005794 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5795 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005796 {
Sean Callanana2424172010-10-25 00:29:48 +00005797 case clang::Type::Builtin:
5798 switch (cast<clang::BuiltinType>(qual_type)->getKind())
5799 {
5800 default:
5801 break;
5802 case clang::BuiltinType::ObjCId:
5803 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00005804 return true;
5805 }
5806 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00005807 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005808 if (target_type)
5809 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5810 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005811 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005812 if (target_type)
5813 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5814 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005815 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005816 if (target_type)
5817 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5818 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005819 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005820 if (target_type)
5821 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5822 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005823 case clang::Type::LValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005824 if (target_type)
5825 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5826 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005827 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005828 if (target_type)
5829 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5830 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005831 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00005832 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00005833 case clang::Type::Elaborated:
5834 return ClangASTContext::IsPointerOrReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005835 default:
5836 break;
5837 }
5838 return false;
5839}
5840
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005841bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005842ClangASTContext::IsIntegerType (clang_type_t clang_type, bool &is_signed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005843{
5844 if (!clang_type)
5845 return false;
5846
5847 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5848 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal());
5849
5850 if (builtin_type)
5851 {
5852 if (builtin_type->isInteger())
Jim Inghamef651602011-12-22 19:12:40 +00005853 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005854 is_signed = builtin_type->isSignedInteger();
Jim Inghamef651602011-12-22 19:12:40 +00005855 return true;
5856 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005857 }
5858
5859 return false;
5860}
5861
5862bool
Greg Claytonaffb03b2011-07-08 18:27:39 +00005863ClangASTContext::IsPointerType (clang_type_t clang_type, clang_type_t *target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005864{
Greg Claytonaffb03b2011-07-08 18:27:39 +00005865 if (target_type)
5866 *target_type = NULL;
5867
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005868 if (clang_type)
5869 {
5870 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005871 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5872 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005873 {
Sean Callanana2424172010-10-25 00:29:48 +00005874 case clang::Type::Builtin:
5875 switch (cast<clang::BuiltinType>(qual_type)->getKind())
5876 {
5877 default:
5878 break;
5879 case clang::BuiltinType::ObjCId:
5880 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00005881 return true;
5882 }
5883 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00005884 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005885 if (target_type)
5886 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5887 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005888 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005889 if (target_type)
5890 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5891 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005892 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005893 if (target_type)
5894 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5895 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005896 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005897 if (target_type)
5898 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5899 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005900 case clang::Type::Typedef:
Greg Claytonaffb03b2011-07-08 18:27:39 +00005901 return ClangASTContext::IsPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), target_type);
Sean Callanan912855f2011-08-11 23:56:13 +00005902 case clang::Type::Elaborated:
5903 return ClangASTContext::IsPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), target_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005904 default:
5905 break;
5906 }
5907 }
5908 return false;
5909}
5910
5911bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005912ClangASTContext::IsFloatingPointType (clang_type_t clang_type, uint32_t &count, bool &is_complex)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005913{
5914 if (clang_type)
5915 {
5916 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5917
5918 if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()))
5919 {
5920 clang::BuiltinType::Kind kind = BT->getKind();
5921 if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble)
5922 {
5923 count = 1;
5924 is_complex = false;
5925 return true;
5926 }
5927 }
5928 else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal()))
5929 {
5930 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex))
5931 {
5932 count = 2;
5933 is_complex = true;
5934 return true;
5935 }
5936 }
5937 else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal()))
5938 {
5939 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex))
5940 {
5941 count = VT->getNumElements();
5942 is_complex = false;
5943 return true;
5944 }
5945 }
5946 }
5947 return false;
5948}
5949
Enrico Granata9fc19442011-07-06 02:13:41 +00005950bool
5951ClangASTContext::IsScalarType (lldb::clang_type_t clang_type)
5952{
5953 bool is_signed;
5954 if (ClangASTContext::IsIntegerType(clang_type, is_signed))
5955 return true;
5956
5957 uint32_t count;
5958 bool is_complex;
5959 return ClangASTContext::IsFloatingPointType(clang_type, count, is_complex) && !is_complex;
5960}
5961
5962bool
5963ClangASTContext::IsPointerToScalarType (lldb::clang_type_t clang_type)
5964{
5965 if (!IsPointerType(clang_type))
5966 return false;
5967
5968 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5969 lldb::clang_type_t pointee_type = qual_type.getTypePtr()->getPointeeType().getAsOpaquePtr();
5970 return IsScalarType(pointee_type);
5971}
5972
5973bool
5974ClangASTContext::IsArrayOfScalarType (lldb::clang_type_t clang_type)
5975{
Greg Clayton4ef877f2012-12-06 02:33:54 +00005976 clang_type = GetAsArrayType(clang_type, NULL, NULL, NULL);
Sean Callanan0caa21c2012-01-19 23:54:24 +00005977
5978 if (clang_type == 0)
Enrico Granata9fc19442011-07-06 02:13:41 +00005979 return false;
5980
5981 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5982 lldb::clang_type_t item_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
5983 return IsScalarType(item_type);
5984}
5985
Greg Clayton8f92f0a2010-10-14 22:52:14 +00005986
5987bool
5988ClangASTContext::GetCXXClassName (clang_type_t clang_type, std::string &class_name)
5989{
5990 if (clang_type)
5991 {
5992 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5993
5994 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5995 if (cxx_record_decl)
5996 {
5997 class_name.assign (cxx_record_decl->getIdentifier()->getNameStart());
5998 return true;
5999 }
6000 }
6001 class_name.clear();
6002 return false;
6003}
6004
6005
Greg Clayton0fffff52010-09-24 05:15:53 +00006006bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006007ClangASTContext::IsCXXClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00006008{
6009 if (clang_type)
6010 {
6011 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6012 if (qual_type->getAsCXXRecordDecl() != NULL)
6013 return true;
6014 }
6015 return false;
6016}
6017
Greg Clayton20568dd2011-10-13 23:13:20 +00006018bool
6019ClangASTContext::IsBeingDefined (lldb::clang_type_t clang_type)
6020{
6021 if (clang_type)
6022 {
6023 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6024 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type);
6025 if (tag_type)
6026 return tag_type->isBeingDefined();
6027 }
6028 return false;
6029}
6030
Greg Clayton0fffff52010-09-24 05:15:53 +00006031bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006032ClangASTContext::IsObjCClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00006033{
6034 if (clang_type)
6035 {
6036 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6037 if (qual_type->isObjCObjectOrInterfaceType())
6038 return true;
6039 }
6040 return false;
6041}
6042
Sean Callanan72772842012-02-22 23:57:45 +00006043bool
6044ClangASTContext::IsObjCObjectPointerType (lldb::clang_type_t clang_type, clang_type_t *class_type)
6045{
6046 if (clang_type)
6047 {
6048 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6049 if (qual_type->isObjCObjectPointerType())
6050 {
6051 if (class_type)
6052 {
6053 *class_type = NULL;
6054
6055 if (!qual_type->isObjCClassType() &&
6056 !qual_type->isObjCIdType())
6057 {
6058 const ObjCObjectPointerType *obj_pointer_type = dyn_cast<ObjCObjectPointerType>(qual_type);
Sean Callanand7dabe22012-03-10 01:59:11 +00006059 if (!obj_pointer_type)
6060 *class_type = NULL;
Sean Callanan1fc91ad2012-03-10 02:00:32 +00006061 else
6062 *class_type = QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr();
Sean Callanan72772842012-02-22 23:57:45 +00006063 }
6064 }
6065 return true;
6066 }
6067 }
6068 return false;
6069}
6070
6071bool
6072ClangASTContext::GetObjCClassName (lldb::clang_type_t clang_type,
6073 std::string &class_name)
6074{
6075 if (!clang_type)
6076 return false;
6077
6078 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(QualType::getFromOpaquePtr(clang_type));
6079 if (!object_type)
6080 return false;
6081
6082 const ObjCInterfaceDecl *interface = object_type->getInterface();
6083 if (!interface)
6084 return false;
6085
6086 class_name = interface->getNameAsString();
6087 return true;
6088}
Greg Clayton0fffff52010-09-24 05:15:53 +00006089
Greg Clayton73b472d2010-10-27 03:32:59 +00006090bool
6091ClangASTContext::IsCharType (clang_type_t clang_type)
6092{
6093 if (clang_type)
6094 return QualType::getFromOpaquePtr(clang_type)->isCharType();
6095 return false;
6096}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006097
6098bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006099ClangASTContext::IsCStringType (clang_type_t clang_type, uint32_t &length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006100{
Greg Clayton73b472d2010-10-27 03:32:59 +00006101 clang_type_t pointee_or_element_clang_type = NULL;
6102 Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, &pointee_or_element_clang_type));
6103
6104 if (pointee_or_element_clang_type == NULL)
6105 return false;
6106
6107 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006108 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006109 QualType pointee_or_element_qual_type (QualType::getFromOpaquePtr (pointee_or_element_clang_type));
6110
6111 if (pointee_or_element_qual_type.getUnqualifiedType()->isCharType())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006112 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006113 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6114 if (type_flags.Test (eTypeIsArray))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006115 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006116 // We know the size of the array and it could be a C string
6117 // since it is an array of characters
6118 length = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
6119 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006120 }
Greg Clayton73b472d2010-10-27 03:32:59 +00006121 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006122 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006123 length = 0;
6124 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006125 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006126
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006127 }
6128 }
6129 return false;
6130}
6131
6132bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006133ClangASTContext::IsFunctionPointerType (clang_type_t clang_type)
Greg Clayton737b9322010-09-13 03:32:57 +00006134{
6135 if (clang_type)
6136 {
6137 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6138
6139 if (qual_type->isFunctionPointerType())
6140 return true;
6141
6142 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6143 switch (type_class)
6144 {
Sean Callananfb0b7582011-03-15 00:17:19 +00006145 default:
6146 break;
Greg Clayton737b9322010-09-13 03:32:57 +00006147 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00006148 return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006149 case clang::Type::Elaborated:
6150 return ClangASTContext::IsFunctionPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00006151
6152 case clang::Type::LValueReference:
6153 case clang::Type::RValueReference:
6154 {
Sean Callanan78e37602011-01-27 04:42:51 +00006155 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00006156 if (reference_type)
6157 return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr());
6158 }
6159 break;
6160 }
6161 }
6162 return false;
6163}
6164
Greg Clayton73b472d2010-10-27 03:32:59 +00006165size_t
6166ClangASTContext::GetArraySize (clang_type_t clang_type)
6167{
6168 if (clang_type)
6169 {
Greg Claytonef37d68a2011-07-09 17:12:27 +00006170 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
6171 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6172 switch (type_class)
6173 {
6174 case clang::Type::ConstantArray:
6175 {
6176 const ConstantArrayType *array = cast<ConstantArrayType>(QualType::getFromOpaquePtr(clang_type).getTypePtr());
6177 if (array)
6178 return array->getSize().getLimitedValue();
6179 }
6180 break;
6181
6182 case clang::Type::Typedef:
6183 return ClangASTContext::GetArraySize(cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006184
6185 case clang::Type::Elaborated:
6186 return ClangASTContext::GetArraySize(cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00006187
6188 default:
6189 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006190 }
Greg Clayton73b472d2010-10-27 03:32:59 +00006191 }
6192 return 0;
6193}
Greg Clayton737b9322010-09-13 03:32:57 +00006194
Sean Callanan0caa21c2012-01-19 23:54:24 +00006195clang_type_t
Greg Clayton4ef877f2012-12-06 02:33:54 +00006196ClangASTContext::GetAsArrayType (clang_type_t clang_type, clang_type_t*member_type, uint64_t *size, bool *is_incomplete)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006197{
Greg Clayton4ef877f2012-12-06 02:33:54 +00006198 if (is_incomplete)
6199 *is_incomplete = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006200 if (!clang_type)
Sean Callanan0caa21c2012-01-19 23:54:24 +00006201 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006202
6203 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6204
Greg Clayton737b9322010-09-13 03:32:57 +00006205 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6206 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006207 {
Sean Callananfb0b7582011-03-15 00:17:19 +00006208 default:
6209 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006210
Greg Claytone1a916a2010-07-21 22:12:05 +00006211 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006212 if (member_type)
6213 *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6214 if (size)
Greg Claytonac4827f2011-04-01 18:14:08 +00006215 *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
Sean Callanan0caa21c2012-01-19 23:54:24 +00006216 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006217
Greg Claytone1a916a2010-07-21 22:12:05 +00006218 case clang::Type::IncompleteArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006219 if (member_type)
6220 *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6221 if (size)
6222 *size = 0;
Greg Clayton4ef877f2012-12-06 02:33:54 +00006223 if (is_incomplete)
6224 *is_incomplete = true;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006225 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006226
Greg Claytone1a916a2010-07-21 22:12:05 +00006227 case clang::Type::VariableArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006228 if (member_type)
6229 *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6230 if (size)
6231 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006232 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006233
Greg Claytone1a916a2010-07-21 22:12:05 +00006234 case clang::Type::DependentSizedArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006235 if (member_type)
6236 *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6237 if (size)
6238 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006239 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006240
6241 case clang::Type::Typedef:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006242 return ClangASTContext::GetAsArrayType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
6243 member_type,
Greg Clayton4ef877f2012-12-06 02:33:54 +00006244 size,
6245 is_incomplete);
Sean Callanan912855f2011-08-11 23:56:13 +00006246
6247 case clang::Type::Elaborated:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006248 return ClangASTContext::GetAsArrayType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
6249 member_type,
Greg Clayton4ef877f2012-12-06 02:33:54 +00006250 size,
6251 is_incomplete);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006252 }
Sean Callanan0caa21c2012-01-19 23:54:24 +00006253 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006254}
6255
6256
6257#pragma mark Typedefs
6258
Greg Clayton1be10fc2010-09-29 01:12:09 +00006259clang_type_t
6260ClangASTContext::CreateTypedefType (const char *name, clang_type_t clang_type, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006261{
6262 if (clang_type)
6263 {
6264 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006265 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006266 IdentifierTable *identifier_table = getIdentifierTable();
Greg Clayton6beaaa62011-01-17 03:46:26 +00006267 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006268 assert (identifier_table != NULL);
6269 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00006270 decl_ctx = ast->getTranslationUnitDecl();
6271 TypedefDecl *decl = TypedefDecl::Create (*ast,
6272 decl_ctx,
6273 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00006274 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00006275 name ? &identifier_table->get(name) : NULL, // Identifier
6276 ast->CreateTypeSourceInfo(qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00006277
Greg Clayton147e1fa2011-10-14 22:47:18 +00006278 //decl_ctx->addDecl (decl);
6279
Sean Callanan2652ad22011-01-18 01:03:44 +00006280 decl->setAccess(AS_public); // TODO respect proper access specifier
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006281
6282 // Get a uniqued QualType for the typedef decl type
Greg Clayton6beaaa62011-01-17 03:46:26 +00006283 return ast->getTypedefType (decl).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006284 }
6285 return NULL;
6286}
6287
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006288// Disable this for now since I can't seem to get a nicely formatted float
6289// out of the APFloat class without just getting the float, double or quad
6290// and then using a formatted print on it which defeats the purpose. We ideally
6291// would like to get perfect string values for any kind of float semantics
6292// so we can support remote targets. The code below also requires a patch to
6293// llvm::APInt.
6294//bool
Greg Clayton6beaaa62011-01-17 03:46:26 +00006295//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 +00006296//{
6297// uint32_t count = 0;
6298// bool is_complex = false;
6299// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6300// {
6301// unsigned num_bytes_per_float = byte_size / count;
6302// unsigned num_bits_per_float = num_bytes_per_float * 8;
6303//
6304// float_str.clear();
6305// uint32_t i;
6306// for (i=0; i<count; i++)
6307// {
6308// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
6309// bool is_ieee = false;
6310// APFloat ap_float(ap_int, is_ieee);
6311// char s[1024];
6312// unsigned int hex_digits = 0;
6313// bool upper_case = false;
6314//
6315// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
6316// {
6317// if (i > 0)
6318// float_str.append(", ");
6319// float_str.append(s);
6320// if (i == 1 && is_complex)
6321// float_str.append(1, 'i');
6322// }
6323// }
6324// return !float_str.empty();
6325// }
6326// return false;
6327//}
6328
6329size_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00006330ClangASTContext::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 +00006331{
6332 if (clang_type)
6333 {
6334 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6335 uint32_t count = 0;
6336 bool is_complex = false;
6337 if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6338 {
6339 // TODO: handle complex and vector types
6340 if (count != 1)
6341 return false;
6342
6343 StringRef s_sref(s);
Greg Clayton6beaaa62011-01-17 03:46:26 +00006344 APFloat ap_float(ast->getFloatTypeSemantics(qual_type), s_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006345
Greg Clayton6beaaa62011-01-17 03:46:26 +00006346 const uint64_t bit_size = ast->getTypeSize (qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006347 const uint64_t byte_size = bit_size / 8;
6348 if (dst_size >= byte_size)
6349 {
6350 if (bit_size == sizeof(float)*8)
6351 {
6352 float float32 = ap_float.convertToFloat();
6353 ::memcpy (dst, &float32, byte_size);
6354 return byte_size;
6355 }
6356 else if (bit_size >= 64)
6357 {
6358 llvm::APInt ap_int(ap_float.bitcastToAPInt());
6359 ::memcpy (dst, ap_int.getRawData(), byte_size);
6360 return byte_size;
6361 }
6362 }
6363 }
6364 }
6365 return 0;
6366}
Sean Callanan6fe64b52010-09-17 02:24:29 +00006367
6368unsigned
Greg Clayton1be10fc2010-09-29 01:12:09 +00006369ClangASTContext::GetTypeQualifiers(clang_type_t clang_type)
Sean Callanan6fe64b52010-09-17 02:24:29 +00006370{
6371 assert (clang_type);
6372
6373 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6374
6375 return qual_type.getQualifiers().getCVRQualifiers();
6376}
Greg Clayton6beaaa62011-01-17 03:46:26 +00006377
6378bool
6379ClangASTContext::GetCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6380{
6381 if (clang_type == NULL)
6382 return false;
6383
Greg Claytonc432c192011-01-20 04:18:48 +00006384 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006385}
6386
6387
6388bool
6389ClangASTContext::GetCompleteType (clang_type_t clang_type)
6390{
6391 return ClangASTContext::GetCompleteType (getASTContext(), clang_type);
6392}
6393
Greg Claytona2721472011-06-25 00:44:06 +00006394bool
Enrico Granata86027e92012-03-24 01:11:14 +00006395ClangASTContext::IsCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6396{
6397 if (clang_type == NULL)
6398 return false;
6399
6400 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type), false); // just check but don't let it actually complete
6401}
6402
6403
6404bool
6405ClangASTContext::IsCompleteType (clang_type_t clang_type)
6406{
6407 return ClangASTContext::IsCompleteType (getASTContext(), clang_type);
6408}
6409
6410bool
Greg Claytona2721472011-06-25 00:44:06 +00006411ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
6412 clang::Decl *decl)
6413{
6414 if (!decl)
6415 return false;
6416
6417 ExternalASTSource *ast_source = ast->getExternalSource();
6418
6419 if (!ast_source)
6420 return false;
6421
6422 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
6423 {
Greg Clayton219cf312012-03-30 00:51:13 +00006424 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006425 return true;
6426
6427 if (!tag_decl->hasExternalLexicalStorage())
6428 return false;
6429
6430 ast_source->CompleteType(tag_decl);
6431
6432 return !tag_decl->getTypeForDecl()->isIncompleteType();
6433 }
6434 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
6435 {
Sean Callanan5b26f272012-02-04 08:49:35 +00006436 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006437 return true;
6438
6439 if (!objc_interface_decl->hasExternalLexicalStorage())
6440 return false;
6441
6442 ast_source->CompleteType(objc_interface_decl);
6443
Sean Callanan5b26f272012-02-04 08:49:35 +00006444 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00006445 }
6446 else
6447 {
6448 return false;
6449 }
6450}
6451
Sean Callanan60217122012-04-13 00:10:03 +00006452void
Jim Ingham379397632012-10-27 02:54:13 +00006453ClangASTContext::SetMetadataAsUserID (uintptr_t object,
6454 user_id_t user_id)
6455{
6456 ClangASTMetadata meta_data;
6457 meta_data.SetUserID (user_id);
6458 SetMetadata (object, meta_data);
6459}
6460
6461void
Sean Callanan60217122012-04-13 00:10:03 +00006462ClangASTContext::SetMetadata (clang::ASTContext *ast,
6463 uintptr_t object,
Jim Ingham379397632012-10-27 02:54:13 +00006464 ClangASTMetadata &metadata)
Sean Callanan60217122012-04-13 00:10:03 +00006465{
6466 ClangExternalASTSourceCommon *external_source =
6467 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
6468
6469 if (external_source)
6470 external_source->SetMetadata(object, metadata);
6471}
6472
Jim Ingham379397632012-10-27 02:54:13 +00006473ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +00006474ClangASTContext::GetMetadata (clang::ASTContext *ast,
6475 uintptr_t object)
6476{
6477 ClangExternalASTSourceCommon *external_source =
6478 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
6479
6480 if (external_source && external_source->HasMetadata(object))
6481 return external_source->GetMetadata(object);
6482 else
Jim Ingham379397632012-10-27 02:54:13 +00006483 return NULL;
Sean Callanan60217122012-04-13 00:10:03 +00006484}
6485
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006486clang::DeclContext *
6487ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
6488{
Sean Callanana87bee82011-08-19 06:19:25 +00006489 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006490}
6491
6492clang::DeclContext *
6493ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
6494{
Sean Callanana87bee82011-08-19 06:19:25 +00006495 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006496}
6497
Greg Clayton685c88c2012-07-14 00:53:55 +00006498
6499bool
6500ClangASTContext::GetClassMethodInfoForDeclContext (clang::DeclContext *decl_ctx,
6501 lldb::LanguageType &language,
6502 bool &is_instance_method,
6503 ConstString &language_object_name)
6504{
6505 language_object_name.Clear();
6506 language = eLanguageTypeUnknown;
6507 is_instance_method = false;
6508
6509 if (decl_ctx)
6510 {
6511 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
6512 {
6513 if (method_decl->isStatic())
6514 {
6515 is_instance_method = false;
6516 }
6517 else
6518 {
6519 language_object_name.SetCString("this");
6520 is_instance_method = true;
6521 }
6522 language = eLanguageTypeC_plus_plus;
6523 return true;
6524 }
6525 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
6526 {
6527 // Both static and instance methods have a "self" object in objective C
6528 language_object_name.SetCString("self");
6529 if (method_decl->isInstanceMethod())
6530 {
6531 is_instance_method = true;
6532 }
6533 else
6534 {
6535 is_instance_method = false;
6536 }
6537 language = eLanguageTypeObjC;
6538 return true;
6539 }
Jim Ingham379397632012-10-27 02:54:13 +00006540 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
6541 {
6542 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), (uintptr_t) function_decl);
6543 if (metadata && metadata->HasObjectPtr())
6544 {
6545 language_object_name.SetCString (metadata->GetObjectPtrName());
6546 language = eLanguageTypeObjC;
6547 is_instance_method = true;
6548 }
6549 return true;
6550 }
Greg Clayton685c88c2012-07-14 00:53:55 +00006551 }
6552 return false;
6553}
6554