blob: 76ec6bc14d0398de046be417fe7c93d3ad0c6c02 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eli Friedman932197d2010-06-13 19:06:42 +000010#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011
12// C Includes
13// C++ Includes
14#include <string>
15
16// Other libraries and framework includes
Greg Clayton6beaaa62011-01-17 03:46:26 +000017
18// Clang headers like to use NDEBUG inside of them to enable/disable debug
19// releated features using "#ifndef NDEBUG" preprocessor blocks to do one thing
20// or another. This is bad because it means that if clang was built in release
21// mode, it assumes that you are building in release mode which is not always
22// the case. You can end up with functions that are defined as empty in header
23// files when NDEBUG is not defined, and this can cause link errors with the
24// clang .a files that you have since you might be missing functions in the .a
25// file. So we have to define NDEBUG when including clang headers to avoid any
26// mismatches. This is covered by rdar://problem/8691220
27
Sean Callanan3b1d4f62011-10-26 17:46:51 +000028#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000029#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000030#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000031// Need to include assert.h so it is as clang would expect it to be (disabled)
32#include <assert.h>
33#endif
34
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "clang/AST/ASTContext.h"
36#include "clang/AST/ASTImporter.h"
37#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000038#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000039#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "clang/AST/RecordLayout.h"
41#include "clang/AST/Type.h"
42#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000043#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000045#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "clang/Basic/SourceManager.h"
47#include "clang/Basic/TargetInfo.h"
48#include "clang/Basic/TargetOptions.h"
49#include "clang/Frontend/FrontendOptions.h"
50#include "clang/Frontend/LangStandard.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000051
52#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000053#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000054#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
55// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
56#include <assert.h>
57#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058
Greg Clayton514487e2011-02-15 21:59:32 +000059#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060#include "lldb/Core/dwarf.h"
Greg Clayton73b472d2010-10-27 03:32:59 +000061#include "lldb/Core/Flags.h"
Sean Callananfb8b7092010-10-28 18:19:36 +000062#include "lldb/Core/Log.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000063#include "lldb/Core/RegularExpression.h"
64#include "lldb/Expression/ASTDumper.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000065#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000066#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000067#include "lldb/Target/ExecutionContext.h"
68#include "lldb/Target/Process.h"
69#include "lldb/Target/ObjCLanguageRuntime.h"
70
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071
Eli Friedman932197d2010-06-13 19:06:42 +000072#include <stdio.h>
73
Greg Claytonc86103d2010-08-05 01:57:25 +000074using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075using namespace lldb_private;
76using namespace llvm;
77using namespace clang;
78
Greg Clayton6beaaa62011-01-17 03:46:26 +000079
80static bool
Enrico Granata86027e92012-03-24 01:11:14 +000081GetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion = true)
Greg Clayton6beaaa62011-01-17 03:46:26 +000082{
83 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
84 switch (type_class)
85 {
Sean Callanan5b26f272012-02-04 08:49:35 +000086 case clang::Type::ConstantArray:
87 {
88 const clang::ArrayType *array_type = dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
89
90 if (array_type)
Enrico Granata86027e92012-03-24 01:11:14 +000091 return GetCompleteQualType (ast, array_type->getElementType(), allow_completion);
Sean Callanan5b26f272012-02-04 08:49:35 +000092 }
93 break;
94
Greg Clayton6beaaa62011-01-17 03:46:26 +000095 case clang::Type::Record:
96 case clang::Type::Enum:
97 {
Sean Callanan78e37602011-01-27 04:42:51 +000098 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +000099 if (tag_type)
100 {
101 clang::TagDecl *tag_decl = tag_type->getDecl();
102 if (tag_decl)
103 {
Greg Clayton219cf312012-03-30 00:51:13 +0000104 if (tag_decl->isCompleteDefinition())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000105 return true;
Enrico Granata86027e92012-03-24 01:11:14 +0000106
107 if (!allow_completion)
108 return false;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000109
110 if (tag_decl->hasExternalLexicalStorage())
111 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000112 if (ast)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000113 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000114 ExternalASTSource *external_ast_source = ast->getExternalSource();
115 if (external_ast_source)
116 {
117 external_ast_source->CompleteType(tag_decl);
118 return !tag_type->isIncompleteType();
119 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000120 }
121 }
122 return false;
123 }
124 }
125
126 }
127 break;
128
129 case clang::Type::ObjCObject:
130 case clang::Type::ObjCInterface:
131 {
Sean Callanan78e37602011-01-27 04:42:51 +0000132 const clang::ObjCObjectType *objc_class_type = dyn_cast<clang::ObjCObjectType>(qual_type);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000133 if (objc_class_type)
134 {
135 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
136 // We currently can't complete objective C types through the newly added ASTContext
137 // because it only supports TagDecl objects right now...
Enrico Granata9dd75c82011-07-15 23:30:15 +0000138 if (class_interface_decl)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000139 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000140 if (class_interface_decl->getDefinition())
141 return true;
142
Enrico Granata86027e92012-03-24 01:11:14 +0000143 if (!allow_completion)
144 return false;
Greg Clayton20533982012-03-30 23:48:28 +0000145
Sean Callanan5b26f272012-02-04 08:49:35 +0000146 if (class_interface_decl->hasExternalLexicalStorage())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000147 {
Enrico Granata0a3958e2011-07-02 00:25:22 +0000148 if (ast)
Greg Clayton007d5be2011-05-30 00:49:24 +0000149 {
Enrico Granata0a3958e2011-07-02 00:25:22 +0000150 ExternalASTSource *external_ast_source = ast->getExternalSource();
151 if (external_ast_source)
152 {
153 external_ast_source->CompleteType (class_interface_decl);
Sean Callanan5b26f272012-02-04 08:49:35 +0000154 return !objc_class_type->isIncompleteType();
Enrico Granata0a3958e2011-07-02 00:25:22 +0000155 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000156 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000157 }
Enrico Granata0a3958e2011-07-02 00:25:22 +0000158 return false;
Sean Callanan5b26f272012-02-04 08:49:35 +0000159 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000160 }
161 }
162 break;
163
164 case clang::Type::Typedef:
Enrico Granata86027e92012-03-24 01:11:14 +0000165 return GetCompleteQualType (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType(), allow_completion);
Sean Callanan912855f2011-08-11 23:56:13 +0000166
167 case clang::Type::Elaborated:
Enrico Granata86027e92012-03-24 01:11:14 +0000168 return GetCompleteQualType (ast, cast<ElaboratedType>(qual_type)->getNamedType(), allow_completion);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000169
170 default:
171 break;
172 }
173
174 return true;
175}
176
Greg Clayton8cf05932010-07-22 18:30:50 +0000177static AccessSpecifier
Greg Claytonc86103d2010-08-05 01:57:25 +0000178ConvertAccessTypeToAccessSpecifier (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000179{
180 switch (access)
181 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000182 default: break;
183 case eAccessNone: return AS_none;
184 case eAccessPublic: return AS_public;
185 case eAccessPrivate: return AS_private;
186 case eAccessProtected: return AS_protected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000187 }
188 return AS_none;
189}
190
191static ObjCIvarDecl::AccessControl
Greg Claytonc86103d2010-08-05 01:57:25 +0000192ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000193{
194 switch (access)
195 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000196 default: break;
197 case eAccessNone: return ObjCIvarDecl::None;
198 case eAccessPublic: return ObjCIvarDecl::Public;
199 case eAccessPrivate: return ObjCIvarDecl::Private;
200 case eAccessProtected: return ObjCIvarDecl::Protected;
201 case eAccessPackage: return ObjCIvarDecl::Package;
Greg Clayton8cf05932010-07-22 18:30:50 +0000202 }
203 return ObjCIvarDecl::None;
204}
205
206
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207static void
208ParseLangArgs
209(
210 LangOptions &Opts,
Greg Clayton94e5d782010-06-13 17:34:29 +0000211 InputKind IK
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212)
213{
214 // FIXME: Cleanup per-file based stuff.
215
216 // Set some properties which depend soley on the input kind; it would be nice
217 // to move these to the language standard, and have the driver resolve the
218 // input kind + language standard.
Greg Clayton94e5d782010-06-13 17:34:29 +0000219 if (IK == IK_Asm) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220 Opts.AsmPreprocessor = 1;
Greg Clayton94e5d782010-06-13 17:34:29 +0000221 } else if (IK == IK_ObjC ||
222 IK == IK_ObjCXX ||
223 IK == IK_PreprocessedObjC ||
224 IK == IK_PreprocessedObjCXX) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225 Opts.ObjC1 = Opts.ObjC2 = 1;
226 }
227
228 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
229
230 if (LangStd == LangStandard::lang_unspecified) {
231 // Based on the base language, pick one.
232 switch (IK) {
Greg Clayton94e5d782010-06-13 17:34:29 +0000233 case IK_None:
234 case IK_AST:
Sean Callananfb0b7582011-03-15 00:17:19 +0000235 case IK_LLVM_IR:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000236 assert (!"Invalid input kind!");
Greg Clayton94e5d782010-06-13 17:34:29 +0000237 case IK_OpenCL:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238 LangStd = LangStandard::lang_opencl;
239 break;
Sean Callananfb0b7582011-03-15 00:17:19 +0000240 case IK_CUDA:
241 LangStd = LangStandard::lang_cuda;
242 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000243 case IK_Asm:
244 case IK_C:
245 case IK_PreprocessedC:
246 case IK_ObjC:
247 case IK_PreprocessedObjC:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248 LangStd = LangStandard::lang_gnu99;
249 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000250 case IK_CXX:
251 case IK_PreprocessedCXX:
252 case IK_ObjCXX:
253 case IK_PreprocessedObjCXX:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254 LangStd = LangStandard::lang_gnucxx98;
255 break;
256 }
257 }
258
259 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
260 Opts.BCPLComment = Std.hasBCPLComments();
261 Opts.C99 = Std.isC99();
262 Opts.CPlusPlus = Std.isCPlusPlus();
263 Opts.CPlusPlus0x = Std.isCPlusPlus0x();
264 Opts.Digraphs = Std.hasDigraphs();
265 Opts.GNUMode = Std.isGNUMode();
266 Opts.GNUInline = !Std.isC99();
267 Opts.HexFloats = Std.hasHexFloats();
268 Opts.ImplicitInt = Std.hasImplicitInt();
269
270 // OpenCL has some additional defaults.
271 if (LangStd == LangStandard::lang_opencl) {
272 Opts.OpenCL = 1;
273 Opts.AltiVec = 1;
274 Opts.CXXOperatorNames = 1;
275 Opts.LaxVectorConversions = 1;
276 }
277
278 // OpenCL and C++ both have bool, true, false keywords.
279 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
280
281// if (Opts.CPlusPlus)
282// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
283//
284// if (Args.hasArg(OPT_fobjc_gc_only))
285// Opts.setGCMode(LangOptions::GCOnly);
286// else if (Args.hasArg(OPT_fobjc_gc))
287// Opts.setGCMode(LangOptions::HybridGC);
288//
289// if (Args.hasArg(OPT_print_ivar_layout))
290// Opts.ObjCGCBitmapPrint = 1;
291//
292// if (Args.hasArg(OPT_faltivec))
293// Opts.AltiVec = 1;
294//
295// if (Args.hasArg(OPT_pthread))
296// Opts.POSIXThreads = 1;
297//
298// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
299// "default");
300// if (Vis == "default")
Sean Callanan31e851c2010-10-29 18:38:40 +0000301 Opts.setVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302// else if (Vis == "hidden")
303// Opts.setVisibilityMode(LangOptions::Hidden);
304// else if (Vis == "protected")
305// Opts.setVisibilityMode(LangOptions::Protected);
306// else
307// Diags.Report(diag::err_drv_invalid_value)
308// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
309
310// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
311
312 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
313 // is specified, or -std is set to a conforming mode.
314 Opts.Trigraphs = !Opts.GNUMode;
315// if (Args.hasArg(OPT_trigraphs))
316// Opts.Trigraphs = 1;
317//
318// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
319// OPT_fno_dollars_in_identifiers,
320// !Opts.AsmPreprocessor);
321// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
322// Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
323// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
324// if (Args.hasArg(OPT_fno_lax_vector_conversions))
325// Opts.LaxVectorConversions = 0;
326// Opts.Exceptions = Args.hasArg(OPT_fexceptions);
327// Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
328// Opts.Blocks = Args.hasArg(OPT_fblocks);
329// Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char);
330// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
331// Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
332// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
333// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
334// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
335// Opts.AccessControl = Args.hasArg(OPT_faccess_control);
336// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
337// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
338// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
339// Diags);
340// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
341// Opts.ObjCConstantStringClass = getLastArgValue(Args,
342// OPT_fconstant_string_class);
343// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
344// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
345// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
346// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
347// Opts.Static = Args.hasArg(OPT_static_define);
348 Opts.OptimizeSize = 0;
349
350 // FIXME: Eliminate this dependency.
351// unsigned Opt =
352// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
353// Opts.Optimize = Opt != 0;
354 unsigned Opt = 0;
355
356 // This is the __NO_INLINE__ define, which just depends on things like the
357 // optimization level and -fno-inline, not actually whether the backend has
358 // inlining enabled.
359 //
360 // FIXME: This is affected by other options (-fno-inline).
Sean Callanan3d654b32012-09-24 22:25:51 +0000361 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362
363// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
364// switch (SSP) {
365// default:
366// Diags.Report(diag::err_drv_invalid_value)
367// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
368// break;
369// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
370// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
371// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
372// }
373}
374
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375
Greg Clayton6beaaa62011-01-17 03:46:26 +0000376ClangASTContext::ClangASTContext (const char *target_triple) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377 m_target_triple(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000378 m_ast_ap(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379 m_language_options_ap(),
380 m_source_manager_ap(),
Sean Callanan880e6802011-10-07 23:18:13 +0000381 m_diagnostics_engine_ap(),
Sean Callananc5069ad2012-10-17 22:11:14 +0000382 m_target_options_rp(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383 m_target_info_ap(),
384 m_identifier_table_ap(),
385 m_selector_table_ap(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000386 m_builtins_ap(),
387 m_callback_tag_decl (NULL),
388 m_callback_objc_decl (NULL),
389 m_callback_baton (NULL)
390
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391{
392 if (target_triple && target_triple[0])
Greg Clayton880cbb02011-07-30 01:26:02 +0000393 SetTargetTriple (target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394}
395
396//----------------------------------------------------------------------
397// Destructor
398//----------------------------------------------------------------------
399ClangASTContext::~ClangASTContext()
400{
401 m_builtins_ap.reset();
402 m_selector_table_ap.reset();
403 m_identifier_table_ap.reset();
404 m_target_info_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000405 m_target_options_rp.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000406 m_diagnostics_engine_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407 m_source_manager_ap.reset();
408 m_language_options_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000409 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410}
411
412
413void
414ClangASTContext::Clear()
415{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000416 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417 m_language_options_ap.reset();
418 m_source_manager_ap.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000419 m_diagnostics_engine_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000420 m_target_options_rp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421 m_target_info_ap.reset();
422 m_identifier_table_ap.reset();
423 m_selector_table_ap.reset();
424 m_builtins_ap.reset();
425}
426
427const char *
428ClangASTContext::GetTargetTriple ()
429{
430 return m_target_triple.c_str();
431}
432
433void
434ClangASTContext::SetTargetTriple (const char *target_triple)
435{
436 Clear();
437 m_target_triple.assign(target_triple);
438}
439
Greg Clayton514487e2011-02-15 21:59:32 +0000440void
441ClangASTContext::SetArchitecture (const ArchSpec &arch)
442{
Greg Clayton880cbb02011-07-30 01:26:02 +0000443 SetTargetTriple(arch.GetTriple().str().c_str());
Greg Clayton514487e2011-02-15 21:59:32 +0000444}
445
Greg Clayton6beaaa62011-01-17 03:46:26 +0000446bool
447ClangASTContext::HasExternalSource ()
448{
449 ASTContext *ast = getASTContext();
450 if (ast)
451 return ast->getExternalSource () != NULL;
452 return false;
453}
454
455void
456ClangASTContext::SetExternalSource (llvm::OwningPtr<ExternalASTSource> &ast_source_ap)
457{
458 ASTContext *ast = getASTContext();
459 if (ast)
460 {
461 ast->setExternalSource (ast_source_ap);
462 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
463 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
464 }
465}
466
467void
468ClangASTContext::RemoveExternalSource ()
469{
470 ASTContext *ast = getASTContext();
471
472 if (ast)
473 {
474 llvm::OwningPtr<ExternalASTSource> empty_ast_source_ap;
475 ast->setExternalSource (empty_ast_source_ap);
476 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
477 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
478 }
479}
480
481
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482
483ASTContext *
484ClangASTContext::getASTContext()
485{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000486 if (m_ast_ap.get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 {
Greg Clayton6beaaa62011-01-17 03:46:26 +0000488 m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
489 *getSourceManager(),
Sean Callanan880e6802011-10-07 23:18:13 +0000490 getTargetInfo(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000491 *getIdentifierTable(),
492 *getSelectorTable(),
493 *getBuiltinContext(),
494 0));
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000495
Greg Clayton6beaaa62011-01-17 03:46:26 +0000496 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
497 {
498 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
499 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
500 }
501
Sean Callanan880e6802011-10-07 23:18:13 +0000502 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000504 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505}
506
507Builtin::Context *
508ClangASTContext::getBuiltinContext()
509{
510 if (m_builtins_ap.get() == NULL)
Sean Callanan880e6802011-10-07 23:18:13 +0000511 m_builtins_ap.reset (new Builtin::Context());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512 return m_builtins_ap.get();
513}
514
515IdentifierTable *
516ClangASTContext::getIdentifierTable()
517{
518 if (m_identifier_table_ap.get() == NULL)
519 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), NULL));
520 return m_identifier_table_ap.get();
521}
522
523LangOptions *
524ClangASTContext::getLanguageOptions()
525{
526 if (m_language_options_ap.get() == NULL)
527 {
528 m_language_options_ap.reset(new LangOptions());
Greg Clayton94e5d782010-06-13 17:34:29 +0000529 ParseLangArgs(*m_language_options_ap, IK_ObjCXX);
530// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531 }
532 return m_language_options_ap.get();
533}
534
535SelectorTable *
536ClangASTContext::getSelectorTable()
537{
538 if (m_selector_table_ap.get() == NULL)
539 m_selector_table_ap.reset (new SelectorTable());
540 return m_selector_table_ap.get();
541}
542
Sean Callanan79439e82010-11-18 02:56:27 +0000543clang::FileManager *
544ClangASTContext::getFileManager()
545{
546 if (m_file_manager_ap.get() == NULL)
Greg Clayton38a61402010-12-02 23:20:03 +0000547 {
548 clang::FileSystemOptions file_system_options;
549 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
550 }
Sean Callanan79439e82010-11-18 02:56:27 +0000551 return m_file_manager_ap.get();
552}
553
Greg Claytone1a916a2010-07-21 22:12:05 +0000554clang::SourceManager *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555ClangASTContext::getSourceManager()
556{
557 if (m_source_manager_ap.get() == NULL)
Sean Callanan880e6802011-10-07 23:18:13 +0000558 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559 return m_source_manager_ap.get();
560}
561
Sean Callanan880e6802011-10-07 23:18:13 +0000562clang::DiagnosticsEngine *
563ClangASTContext::getDiagnosticsEngine()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564{
Sean Callanan880e6802011-10-07 23:18:13 +0000565 if (m_diagnostics_engine_ap.get() == NULL)
Greg Claytona651b532010-11-19 21:46:54 +0000566 {
567 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Sean Callananec8f1ef2012-10-25 01:00:25 +0000568 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
Greg Claytona651b532010-11-19 21:46:54 +0000569 }
Sean Callanan880e6802011-10-07 23:18:13 +0000570 return m_diagnostics_engine_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000571}
572
Sean Callanan880e6802011-10-07 23:18:13 +0000573class NullDiagnosticConsumer : public DiagnosticConsumer
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000574{
575public:
Sean Callanan880e6802011-10-07 23:18:13 +0000576 NullDiagnosticConsumer ()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000577 {
578 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
579 }
580
Sean Callanan880e6802011-10-07 23:18:13 +0000581 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const Diagnostic &info)
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000582 {
583 if (m_log)
584 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000585 llvm::SmallVector<char, 32> diag_str(10);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000586 info.FormatDiagnostic(diag_str);
587 diag_str.push_back('\0');
588 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
589 }
590 }
Sean Callanan880e6802011-10-07 23:18:13 +0000591
592 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
593 {
594 return new NullDiagnosticConsumer ();
595 }
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000596private:
597 LogSP m_log;
598};
599
Sean Callanan880e6802011-10-07 23:18:13 +0000600DiagnosticConsumer *
601ClangASTContext::getDiagnosticConsumer()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000602{
Sean Callanan880e6802011-10-07 23:18:13 +0000603 if (m_diagnostic_consumer_ap.get() == NULL)
604 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000605
Sean Callanan880e6802011-10-07 23:18:13 +0000606 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000607}
608
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609TargetOptions *
610ClangASTContext::getTargetOptions()
611{
Sean Callananc5069ad2012-10-17 22:11:14 +0000612 if (m_target_options_rp.getPtr() == NULL && !m_target_triple.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000613 {
Sean Callananc5069ad2012-10-17 22:11:14 +0000614 m_target_options_rp.reset ();
615 m_target_options_rp = new TargetOptions();
616 if (m_target_options_rp.getPtr() != NULL)
617 m_target_options_rp->Triple = m_target_triple;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000618 }
Sean Callananc5069ad2012-10-17 22:11:14 +0000619 return m_target_options_rp.getPtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620}
621
622
623TargetInfo *
624ClangASTContext::getTargetInfo()
625{
Greg Clayton70512312012-05-08 01:45:38 +0000626 // target_triple should be something like "x86_64-apple-macosx"
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627 if (m_target_info_ap.get() == NULL && !m_target_triple.empty())
Sean Callanan880e6802011-10-07 23:18:13 +0000628 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), *getTargetOptions()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 return m_target_info_ap.get();
630}
631
632#pragma mark Basic Types
633
634static inline bool
Greg Clayton6beaaa62011-01-17 03:46:26 +0000635QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000636{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000637 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638 if (qual_type_bit_size == bit_size)
639 return true;
640 return false;
641}
642
Greg Clayton1be10fc2010-09-29 01:12:09 +0000643clang_type_t
Greg Claytonc86103d2010-08-05 01:57:25 +0000644ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000645{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000646 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647
Greg Clayton6beaaa62011-01-17 03:46:26 +0000648 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000649
Greg Clayton6beaaa62011-01-17 03:46:26 +0000650 return GetBuiltinTypeForEncodingAndBitSize (ast, encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651}
652
Greg Clayton1be10fc2010-09-29 01:12:09 +0000653clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +0000654ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000655{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000656 if (!ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657 return NULL;
658
659 switch (encoding)
660 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000661 case eEncodingInvalid:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000662 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
663 return ast->VoidPtrTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664 break;
665
Greg Claytonc86103d2010-08-05 01:57:25 +0000666 case eEncodingUint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000667 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
668 return ast->UnsignedCharTy.getAsOpaquePtr();
669 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
670 return ast->UnsignedShortTy.getAsOpaquePtr();
671 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
672 return ast->UnsignedIntTy.getAsOpaquePtr();
673 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
674 return ast->UnsignedLongTy.getAsOpaquePtr();
675 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
676 return ast->UnsignedLongLongTy.getAsOpaquePtr();
677 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
678 return ast->UnsignedInt128Ty.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000679 break;
680
Greg Claytonc86103d2010-08-05 01:57:25 +0000681 case eEncodingSint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000682 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
683 return ast->CharTy.getAsOpaquePtr();
684 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
685 return ast->ShortTy.getAsOpaquePtr();
686 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
687 return ast->IntTy.getAsOpaquePtr();
688 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
689 return ast->LongTy.getAsOpaquePtr();
690 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
691 return ast->LongLongTy.getAsOpaquePtr();
692 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
693 return ast->Int128Ty.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694 break;
695
Greg Claytonc86103d2010-08-05 01:57:25 +0000696 case eEncodingIEEE754:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000697 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
698 return ast->FloatTy.getAsOpaquePtr();
699 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
700 return ast->DoubleTy.getAsOpaquePtr();
701 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
702 return ast->LongDoubleTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703 break;
704
Greg Claytonc86103d2010-08-05 01:57:25 +0000705 case eEncodingVector:
Johnny Chenc79c93a2012-03-07 01:12:24 +0000706 // Sanity check that bit_size is a multiple of 8's.
707 if (bit_size && !(bit_size & 0x7u))
708 return ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8).getAsOpaquePtr();
709 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000710 default:
711 break;
712 }
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);
1282 for (clang::DeclContext::lookup_iterator pos = result.first, end = result.second; pos != end; ++pos)
1283 {
1284 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(*pos);
1285 if (class_template_decl)
1286 return class_template_decl;
1287 }
1288
1289 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Claytonf0705c82011-10-22 03:33:13 +00001290
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001291 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1292 template_param_infos,
1293 template_param_decls);
Greg Claytonf0705c82011-10-22 03:33:13 +00001294
1295 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1296 (TagDecl::TagKind)kind,
1297 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1298 SourceLocation(),
1299 SourceLocation(),
1300 &identifier_info);
Greg Claytone04741d2011-12-02 02:09:28 +00001301
1302 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1303 i < template_param_decl_count;
1304 ++i)
1305 {
1306 template_param_decls[i]->setDeclContext (template_cxx_decl);
1307 }
1308
Sean Callananb5c79622011-11-19 01:35:08 +00001309 // With templated classes, we say that a class is templated with
1310 // specializations, but that the bare class has no functions.
1311 template_cxx_decl->startDefinition();
1312 template_cxx_decl->completeDefinition();
1313
Greg Claytonf0705c82011-10-22 03:33:13 +00001314 class_template_decl = ClassTemplateDecl::Create (*ast,
1315 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1316 SourceLocation(),
1317 decl_name,
1318 template_param_list,
1319 template_cxx_decl,
1320 NULL);
1321
1322 if (class_template_decl)
Sean Callanan5e9e1992011-10-26 01:06:27 +00001323 {
Greg Clayton55561e92011-10-26 03:31:36 +00001324 if (access_type != eAccessNone)
1325 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Sean Callanan5b26f272012-02-04 08:49:35 +00001326
1327 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1328 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1329
Greg Claytonf0705c82011-10-22 03:33:13 +00001330 decl_ctx->addDecl (class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001331
1332#ifdef LLDB_CONFIGURATION_DEBUG
1333 VerifyDecl(class_template_decl);
1334#endif
1335 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001336
1337 return class_template_decl;
1338}
1339
1340
1341ClassTemplateSpecializationDecl *
1342ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1343 ClassTemplateDecl *class_template_decl,
1344 int kind,
1345 const TemplateParameterInfos &template_param_infos)
1346{
1347 ASTContext *ast = getASTContext();
1348 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1349 (TagDecl::TagKind)kind,
1350 decl_ctx,
1351 SourceLocation(),
1352 SourceLocation(),
1353 class_template_decl,
1354 &template_param_infos.args.front(),
1355 template_param_infos.args.size(),
1356 NULL);
1357
1358 return class_template_specialization_decl;
1359}
1360
1361lldb::clang_type_t
1362ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1363{
1364 if (class_template_specialization_decl)
1365 {
1366 ASTContext *ast = getASTContext();
1367 if (ast)
1368 return ast->getTagDeclType(class_template_specialization_decl).getAsOpaquePtr();
1369 }
1370 return NULL;
1371}
1372
Greg Clayton6beaaa62011-01-17 03:46:26 +00001373bool
1374ClangASTContext::SetHasExternalStorage (clang_type_t clang_type, bool has_extern)
1375{
1376 if (clang_type == NULL)
1377 return false;
1378
1379 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
1380
1381 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
1382 switch (type_class)
1383 {
1384 case clang::Type::Record:
1385 {
1386 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
1387 if (cxx_record_decl)
1388 {
1389 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001390 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001391 return true;
1392 }
1393 }
1394 break;
1395
1396 case clang::Type::Enum:
1397 {
1398 EnumDecl *enum_decl = cast<EnumType>(qual_type)->getDecl();
1399 if (enum_decl)
1400 {
1401 enum_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001402 enum_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001403 return true;
1404 }
1405 }
1406 break;
1407
1408 case clang::Type::ObjCObject:
1409 case clang::Type::ObjCInterface:
1410 {
Sean Callanan78e37602011-01-27 04:42:51 +00001411 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001412 assert (objc_class_type);
1413 if (objc_class_type)
1414 {
1415 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1416
1417 if (class_interface_decl)
1418 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001419 class_interface_decl->setHasExternalLexicalStorage (has_extern);
Greg Claytonc432c192011-01-20 04:18:48 +00001420 class_interface_decl->setHasExternalVisibleStorage (has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001421 return true;
1422 }
1423 }
1424 }
1425 break;
1426
1427 case clang::Type::Typedef:
1428 return ClangASTContext::SetHasExternalStorage (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
Sean Callanan912855f2011-08-11 23:56:13 +00001429
1430 case clang::Type::Elaborated:
1431 return ClangASTContext::SetHasExternalStorage (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001432
1433 default:
1434 break;
1435 }
1436 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001437}
1438
Greg Claytona3c444a2010-10-01 23:13:49 +00001439static bool
1440IsOperator (const char *name, OverloadedOperatorKind &op_kind)
1441{
1442 if (name == NULL || name[0] == '\0')
1443 return false;
1444
Sean Callanana43f20d2010-12-10 19:51:54 +00001445#define OPERATOR_PREFIX "operator"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001446#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
Sean Callananbfeff8c2010-12-10 02:15:55 +00001447
1448 const char *post_op_name = NULL;
1449
Sean Callanana43f20d2010-12-10 19:51:54 +00001450 bool no_space = true;
Sean Callananbfeff8c2010-12-10 02:15:55 +00001451
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001452 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
Greg Claytona3c444a2010-10-01 23:13:49 +00001453 return false;
1454
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001455 post_op_name = name + OPERATOR_PREFIX_LENGTH;
1456
Sean Callanana43f20d2010-12-10 19:51:54 +00001457 if (post_op_name[0] == ' ')
1458 {
1459 post_op_name++;
1460 no_space = false;
1461 }
1462
1463#undef OPERATOR_PREFIX
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001464#undef OPERATOR_PREFIX_LENGTH
Sean Callanana43f20d2010-12-10 19:51:54 +00001465
Greg Claytona3c444a2010-10-01 23:13:49 +00001466 // This is an operator, set the overloaded operator kind to invalid
1467 // in case this is a conversion operator...
1468 op_kind = NUM_OVERLOADED_OPERATORS;
1469
1470 switch (post_op_name[0])
1471 {
Sean Callananbfeff8c2010-12-10 02:15:55 +00001472 default:
1473 if (no_space)
1474 return false;
1475 break;
Greg Claytona3c444a2010-10-01 23:13:49 +00001476 case 'n':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001477 if (no_space)
1478 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001479 if (strcmp (post_op_name, "new") == 0)
1480 op_kind = OO_New;
1481 else if (strcmp (post_op_name, "new[]") == 0)
1482 op_kind = OO_Array_New;
1483 break;
1484
1485 case 'd':
Sean Callananbfeff8c2010-12-10 02:15:55 +00001486 if (no_space)
1487 return false;
Greg Claytona3c444a2010-10-01 23:13:49 +00001488 if (strcmp (post_op_name, "delete") == 0)
1489 op_kind = OO_Delete;
1490 else if (strcmp (post_op_name, "delete[]") == 0)
1491 op_kind = OO_Array_Delete;
1492 break;
1493
1494 case '+':
1495 if (post_op_name[1] == '\0')
1496 op_kind = OO_Plus;
1497 else if (post_op_name[2] == '\0')
1498 {
1499 if (post_op_name[1] == '=')
1500 op_kind = OO_PlusEqual;
1501 else if (post_op_name[1] == '+')
1502 op_kind = OO_PlusPlus;
1503 }
1504 break;
1505
1506 case '-':
1507 if (post_op_name[1] == '\0')
1508 op_kind = OO_Minus;
1509 else if (post_op_name[2] == '\0')
1510 {
1511 switch (post_op_name[1])
1512 {
1513 case '=': op_kind = OO_MinusEqual; break;
1514 case '-': op_kind = OO_MinusMinus; break;
1515 case '>': op_kind = OO_Arrow; break;
1516 }
1517 }
1518 else if (post_op_name[3] == '\0')
1519 {
1520 if (post_op_name[2] == '*')
1521 op_kind = OO_ArrowStar; break;
1522 }
1523 break;
1524
1525 case '*':
1526 if (post_op_name[1] == '\0')
1527 op_kind = OO_Star;
1528 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1529 op_kind = OO_StarEqual;
1530 break;
1531
1532 case '/':
1533 if (post_op_name[1] == '\0')
1534 op_kind = OO_Slash;
1535 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1536 op_kind = OO_SlashEqual;
1537 break;
1538
1539 case '%':
1540 if (post_op_name[1] == '\0')
1541 op_kind = OO_Percent;
1542 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1543 op_kind = OO_PercentEqual;
1544 break;
1545
1546
1547 case '^':
1548 if (post_op_name[1] == '\0')
1549 op_kind = OO_Caret;
1550 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1551 op_kind = OO_CaretEqual;
1552 break;
1553
1554 case '&':
1555 if (post_op_name[1] == '\0')
1556 op_kind = OO_Amp;
1557 else if (post_op_name[2] == '\0')
1558 {
1559 switch (post_op_name[1])
1560 {
1561 case '=': op_kind = OO_AmpEqual; break;
1562 case '&': op_kind = OO_AmpAmp; break;
1563 }
1564 }
1565 break;
1566
1567 case '|':
1568 if (post_op_name[1] == '\0')
1569 op_kind = OO_Pipe;
1570 else if (post_op_name[2] == '\0')
1571 {
1572 switch (post_op_name[1])
1573 {
1574 case '=': op_kind = OO_PipeEqual; break;
1575 case '|': op_kind = OO_PipePipe; break;
1576 }
1577 }
1578 break;
1579
1580 case '~':
1581 if (post_op_name[1] == '\0')
1582 op_kind = OO_Tilde;
1583 break;
1584
1585 case '!':
1586 if (post_op_name[1] == '\0')
1587 op_kind = OO_Exclaim;
1588 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1589 op_kind = OO_ExclaimEqual;
1590 break;
1591
1592 case '=':
1593 if (post_op_name[1] == '\0')
1594 op_kind = OO_Equal;
1595 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
1596 op_kind = OO_EqualEqual;
1597 break;
1598
1599 case '<':
1600 if (post_op_name[1] == '\0')
1601 op_kind = OO_Less;
1602 else if (post_op_name[2] == '\0')
1603 {
1604 switch (post_op_name[1])
1605 {
1606 case '<': op_kind = OO_LessLess; break;
1607 case '=': op_kind = OO_LessEqual; break;
1608 }
1609 }
1610 else if (post_op_name[3] == '\0')
1611 {
1612 if (post_op_name[2] == '=')
1613 op_kind = OO_LessLessEqual;
1614 }
1615 break;
1616
1617 case '>':
1618 if (post_op_name[1] == '\0')
1619 op_kind = OO_Greater;
1620 else if (post_op_name[2] == '\0')
1621 {
1622 switch (post_op_name[1])
1623 {
1624 case '>': op_kind = OO_GreaterGreater; break;
1625 case '=': op_kind = OO_GreaterEqual; break;
1626 }
1627 }
1628 else if (post_op_name[1] == '>' &&
1629 post_op_name[2] == '=' &&
1630 post_op_name[3] == '\0')
1631 {
1632 op_kind = OO_GreaterGreaterEqual;
1633 }
1634 break;
1635
1636 case ',':
1637 if (post_op_name[1] == '\0')
1638 op_kind = OO_Comma;
1639 break;
1640
1641 case '(':
1642 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
1643 op_kind = OO_Call;
1644 break;
1645
1646 case '[':
1647 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
1648 op_kind = OO_Subscript;
1649 break;
1650 }
1651
1652 return true;
1653}
Greg Clayton6beaaa62011-01-17 03:46:26 +00001654
Greg Clayton090d0982011-06-19 03:43:27 +00001655static inline bool
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001656check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
Greg Clayton090d0982011-06-19 03:43:27 +00001657{
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001658 // Special-case call since it can take any number of operands
1659 if(op_kind == OO_Call)
1660 return true;
1661
Greg Clayton090d0982011-06-19 03:43:27 +00001662 // The parameter count doens't include "this"
1663 if (num_params == 0)
1664 return unary;
1665 if (num_params == 1)
1666 return binary;
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001667 else
Greg Clayton090d0982011-06-19 03:43:27 +00001668 return false;
1669}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001670
Greg Clayton090d0982011-06-19 03:43:27 +00001671bool
1672ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1673{
Sean Callanan5b26f272012-02-04 08:49:35 +00001674 switch (op_kind)
1675 {
1676 default:
1677 break;
1678 // C++ standard allows any number of arguments to new/delete
1679 case OO_New:
1680 case OO_Array_New:
1681 case OO_Delete:
1682 case OO_Array_Delete:
1683 return true;
1684 }
1685
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001686#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 +00001687 switch (op_kind)
1688 {
1689#include "clang/Basic/OperatorKinds.def"
1690 default: break;
1691 }
1692 return false;
1693}
1694
Greg Claytona51ed9b2010-09-23 01:09:21 +00001695CXXMethodDecl *
Sean Callanan61da09b2010-09-17 02:58:26 +00001696ClangASTContext::AddMethodToCXXRecordType
1697(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001698 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001699 clang_type_t record_opaque_type,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001700 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001701 clang_type_t method_opaque_type,
Greg Claytona51ed9b2010-09-23 01:09:21 +00001702 lldb::AccessType access,
Greg Clayton0fffff52010-09-24 05:15:53 +00001703 bool is_virtual,
1704 bool is_static,
Greg Claytonf51de672010-10-01 02:31:07 +00001705 bool is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00001706 bool is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00001707 bool is_attr_used,
1708 bool is_artificial
Greg Claytona51ed9b2010-09-23 01:09:21 +00001709)
Sean Callanan61da09b2010-09-17 02:58:26 +00001710{
Sean Callananfc55f5d2010-09-21 00:44:12 +00001711 if (!record_opaque_type || !method_opaque_type || !name)
Johnny Chend440bcc2010-09-28 16:10:54 +00001712 return NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001713
Greg Clayton6beaaa62011-01-17 03:46:26 +00001714 assert(ast);
Sean Callanan61da09b2010-09-17 02:58:26 +00001715
Greg Clayton6beaaa62011-01-17 03:46:26 +00001716 IdentifierTable *identifier_table = &ast->Idents;
Sean Callanan61da09b2010-09-17 02:58:26 +00001717
1718 assert(identifier_table);
1719
Sean Callananfc55f5d2010-09-21 00:44:12 +00001720 QualType record_qual_type(QualType::getFromOpaquePtr(record_opaque_type));
Greg Clayton0fffff52010-09-24 05:15:53 +00001721
Greg Clayton6beaaa62011-01-17 03:46:26 +00001722 CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
Sean Callanan61da09b2010-09-17 02:58:26 +00001723
Greg Clayton0fffff52010-09-24 05:15:53 +00001724 if (cxx_record_decl == NULL)
Greg Claytona51ed9b2010-09-23 01:09:21 +00001725 return NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001726
Greg Clayton0fffff52010-09-24 05:15:53 +00001727 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001728
Greg Claytonf51de672010-10-01 02:31:07 +00001729 CXXMethodDecl *cxx_method_decl = NULL;
Sean Callanan61da09b2010-09-17 02:58:26 +00001730
Greg Claytonf51de672010-10-01 02:31:07 +00001731 DeclarationName decl_name (&identifier_table->get(name));
Greg Clayton878eaf12010-10-01 03:45:20 +00001732
Sean Callanan78e37602011-01-27 04:42:51 +00001733 const clang::FunctionType *function_Type = dyn_cast<FunctionType>(method_qual_type.getTypePtr());
Greg Clayton878eaf12010-10-01 03:45:20 +00001734
Greg Clayton90a2acd2010-10-02 01:40:05 +00001735 if (function_Type == NULL)
Greg Clayton878eaf12010-10-01 03:45:20 +00001736 return NULL;
1737
Sean Callanan78e37602011-01-27 04:42:51 +00001738 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(function_Type));
Greg Clayton878eaf12010-10-01 03:45:20 +00001739
1740 if (!method_function_prototype)
1741 return NULL;
1742
1743 unsigned int num_params = method_function_prototype->getNumArgs();
1744
Sean Callanandbb58392011-11-02 01:38:59 +00001745 CXXDestructorDecl *cxx_dtor_decl(NULL);
1746 CXXConstructorDecl *cxx_ctor_decl(NULL);
1747
Greg Clayton878eaf12010-10-01 03:45:20 +00001748 if (name[0] == '~')
Greg Claytonf51de672010-10-01 02:31:07 +00001749 {
Sean Callanandbb58392011-11-02 01:38:59 +00001750 cxx_dtor_decl = CXXDestructorDecl::Create (*ast,
1751 cxx_record_decl,
1752 SourceLocation(),
1753 DeclarationNameInfo (ast->DeclarationNames.getCXXDestructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1754 method_qual_type,
1755 NULL,
1756 is_inline,
1757 is_artificial);
1758 cxx_method_decl = cxx_dtor_decl;
Greg Clayton878eaf12010-10-01 03:45:20 +00001759 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001760 else if (decl_name == cxx_record_decl->getDeclName())
Greg Clayton878eaf12010-10-01 03:45:20 +00001761 {
Sean Callanandbb58392011-11-02 01:38:59 +00001762 cxx_ctor_decl = CXXConstructorDecl::Create (*ast,
1763 cxx_record_decl,
1764 SourceLocation(),
1765 DeclarationNameInfo (ast->DeclarationNames.getCXXConstructorName (ast->getCanonicalType (record_qual_type)), SourceLocation()),
1766 method_qual_type,
1767 NULL, // TypeSourceInfo *
1768 is_explicit,
1769 is_inline,
1770 is_artificial,
1771 false /*is_constexpr*/);
1772 cxx_method_decl = cxx_ctor_decl;
Greg Claytonf51de672010-10-01 02:31:07 +00001773 }
1774 else
Greg Clayton878eaf12010-10-01 03:45:20 +00001775 {
Greg Claytona3c444a2010-10-01 23:13:49 +00001776
1777 OverloadedOperatorKind op_kind = NUM_OVERLOADED_OPERATORS;
1778 if (IsOperator (name, op_kind))
Greg Clayton878eaf12010-10-01 03:45:20 +00001779 {
Greg Claytona3c444a2010-10-01 23:13:49 +00001780 if (op_kind != NUM_OVERLOADED_OPERATORS)
1781 {
Greg Clayton090d0982011-06-19 03:43:27 +00001782 // Check the number of operator parameters. Sometimes we have
1783 // seen bad DWARF that doesn't correctly describe operators and
1784 // if we try to create a methed and add it to the class, clang
1785 // will assert and crash, so we need to make sure things are
1786 // acceptable.
1787 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
1788 return NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001789 cxx_method_decl = CXXMethodDecl::Create (*ast,
Greg Clayton878eaf12010-10-01 03:45:20 +00001790 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001791 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001792 DeclarationNameInfo (ast->DeclarationNames.getCXXOperatorName (op_kind), SourceLocation()),
Greg Clayton878eaf12010-10-01 03:45:20 +00001793 method_qual_type,
1794 NULL, // TypeSourceInfo *
Greg Claytona3c444a2010-10-01 23:13:49 +00001795 is_static,
1796 SC_None,
Sean Callananfb0b7582011-03-15 00:17:19 +00001797 is_inline,
Sean Callanan880e6802011-10-07 23:18:13 +00001798 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001799 SourceLocation());
Greg Claytona3c444a2010-10-01 23:13:49 +00001800 }
1801 else if (num_params == 0)
1802 {
1803 // Conversion operators don't take params...
Greg Clayton6beaaa62011-01-17 03:46:26 +00001804 cxx_method_decl = CXXConversionDecl::Create (*ast,
Greg Claytona3c444a2010-10-01 23:13:49 +00001805 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001806 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001807 DeclarationNameInfo (ast->DeclarationNames.getCXXConversionFunctionName (ast->getCanonicalType (function_Type->getResultType())), SourceLocation()),
Greg Claytona3c444a2010-10-01 23:13:49 +00001808 method_qual_type,
1809 NULL, // TypeSourceInfo *
1810 is_inline,
Sean Callananfb0b7582011-03-15 00:17:19 +00001811 is_explicit,
Sean Callanan880e6802011-10-07 23:18:13 +00001812 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001813 SourceLocation());
Greg Claytona3c444a2010-10-01 23:13:49 +00001814 }
Greg Clayton878eaf12010-10-01 03:45:20 +00001815 }
Greg Claytona3c444a2010-10-01 23:13:49 +00001816
1817 if (cxx_method_decl == NULL)
Greg Clayton878eaf12010-10-01 03:45:20 +00001818 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001819 cxx_method_decl = CXXMethodDecl::Create (*ast,
Greg Clayton878eaf12010-10-01 03:45:20 +00001820 cxx_record_decl,
Sean Callananfb0b7582011-03-15 00:17:19 +00001821 SourceLocation(),
Greg Claytona3c444a2010-10-01 23:13:49 +00001822 DeclarationNameInfo (decl_name, SourceLocation()),
Greg Clayton878eaf12010-10-01 03:45:20 +00001823 method_qual_type,
1824 NULL, // TypeSourceInfo *
1825 is_static,
1826 SC_None,
Sean Callananfb0b7582011-03-15 00:17:19 +00001827 is_inline,
Sean Callanan880e6802011-10-07 23:18:13 +00001828 false /*is_constexpr*/,
Sean Callananfb0b7582011-03-15 00:17:19 +00001829 SourceLocation());
Greg Clayton878eaf12010-10-01 03:45:20 +00001830 }
Greg Claytonf51de672010-10-01 02:31:07 +00001831 }
Greg Claytona3c444a2010-10-01 23:13:49 +00001832
Greg Clayton1be10fc2010-09-29 01:12:09 +00001833 AccessSpecifier access_specifier = ConvertAccessTypeToAccessSpecifier (access);
Greg Clayton0fffff52010-09-24 05:15:53 +00001834
1835 cxx_method_decl->setAccess (access_specifier);
1836 cxx_method_decl->setVirtualAsWritten (is_virtual);
Sean Callanane2ef6e32010-09-23 03:01:22 +00001837
Sean Callananc1b732d2011-11-01 18:07:13 +00001838 if (is_attr_used)
1839 cxx_method_decl->addAttr(::new (*ast) UsedAttr(SourceRange(), *ast));
1840
Sean Callananfc55f5d2010-09-21 00:44:12 +00001841 // Populate the method decl with parameter decls
Sean Callananfc55f5d2010-09-21 00:44:12 +00001842
Charles Davis8c444c42011-05-19 23:33:46 +00001843 llvm::SmallVector<ParmVarDecl *, 12> params;
Sean Callananfc55f5d2010-09-21 00:44:12 +00001844
1845 for (int param_index = 0;
1846 param_index < num_params;
1847 ++param_index)
1848 {
Charles Davis8c444c42011-05-19 23:33:46 +00001849 params.push_back (ParmVarDecl::Create (*ast,
1850 cxx_method_decl,
1851 SourceLocation(),
1852 SourceLocation(),
1853 NULL, // anonymous
1854 method_function_prototype->getArgType(param_index),
1855 NULL,
1856 SC_None,
1857 SC_None,
1858 NULL));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001859 }
1860
Sean Callanan880e6802011-10-07 23:18:13 +00001861 cxx_method_decl->setParams (ArrayRef<ParmVarDecl*>(params));
Sean Callananfc55f5d2010-09-21 00:44:12 +00001862
Greg Clayton0fffff52010-09-24 05:15:53 +00001863 cxx_record_decl->addDecl (cxx_method_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001864
Greg Clayton8b867b42011-11-02 02:06:20 +00001865 // Sometimes the debug info will mention a constructor (default/copy/move),
1866 // destructor, or assignment operator (copy/move) but there won't be any
1867 // version of this in the code. So we check if the function was artificially
1868 // generated and if it is trivial and this lets the compiler/backend know
1869 // that it can inline the IR for these when it needs to and we can avoid a
1870 // "missing function" error when running expressions.
1871
Sean Callanandbb58392011-11-02 01:38:59 +00001872 if (is_artificial)
1873 {
Greg Clayton8b867b42011-11-02 02:06:20 +00001874 if (cxx_ctor_decl &&
1875 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
1876 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
1877 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
Sean Callanandbb58392011-11-02 01:38:59 +00001878 {
1879 cxx_ctor_decl->setDefaulted();
1880 cxx_ctor_decl->setTrivial(true);
1881 }
Greg Clayton8b867b42011-11-02 02:06:20 +00001882 else if (cxx_dtor_decl)
Sean Callanandbb58392011-11-02 01:38:59 +00001883 {
Greg Clayton8b867b42011-11-02 02:06:20 +00001884 if (cxx_record_decl->hasTrivialDestructor())
1885 {
1886 cxx_dtor_decl->setDefaulted();
1887 cxx_dtor_decl->setTrivial(true);
1888 }
1889 }
1890 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
1891 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
1892 {
1893 cxx_method_decl->setDefaulted();
1894 cxx_method_decl->setTrivial(true);
Sean Callanandbb58392011-11-02 01:38:59 +00001895 }
1896 }
1897
Sean Callanan5e9e1992011-10-26 01:06:27 +00001898#ifdef LLDB_CONFIGURATION_DEBUG
1899 VerifyDecl(cxx_method_decl);
1900#endif
Greg Claytonc432c192011-01-20 04:18:48 +00001901
1902// printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
1903// printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
1904// printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
1905// printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
1906// printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
1907// printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
1908// printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
1909// printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
1910// printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
Greg Claytona51ed9b2010-09-23 01:09:21 +00001911 return cxx_method_decl;
Sean Callanan61da09b2010-09-17 02:58:26 +00001912}
1913
Jim Inghame3ae82a2011-11-12 01:36:43 +00001914clang::FieldDecl *
Greg Clayton8cf05932010-07-22 18:30:50 +00001915ClangASTContext::AddFieldToRecordType
1916(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001917 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001918 clang_type_t record_clang_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00001919 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001920 clang_type_t field_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00001921 AccessType access,
1922 uint32_t bitfield_bit_size
1923)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001924{
1925 if (record_clang_type == NULL || field_type == NULL)
Jim Inghame3ae82a2011-11-12 01:36:43 +00001926 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001927
Jim Inghame3ae82a2011-11-12 01:36:43 +00001928 FieldDecl *field = NULL;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001929 IdentifierTable *identifier_table = &ast->Idents;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001930
Greg Clayton6beaaa62011-01-17 03:46:26 +00001931 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001932 assert (identifier_table != NULL);
1933
1934 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
1935
Sean Callanan78e37602011-01-27 04:42:51 +00001936 const clang::Type *clang_type = record_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001937 if (clang_type)
1938 {
1939 const RecordType *record_type = dyn_cast<RecordType>(clang_type);
1940
1941 if (record_type)
1942 {
1943 RecordDecl *record_decl = record_type->getDecl();
1944
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001945 clang::Expr *bit_width = NULL;
1946 if (bitfield_bit_size != 0)
1947 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001948 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
1949 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001950 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001951 field = FieldDecl::Create (*ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001952 record_decl,
1953 SourceLocation(),
1954 SourceLocation(),
1955 name ? &identifier_table->get(name) : NULL, // Identifier
1956 QualType::getFromOpaquePtr(field_type), // Field type
1957 NULL, // TInfo *
1958 bit_width, // BitWidth
1959 false, // Mutable
1960 ICIS_NoInit); // HasInit
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001961
Sean Callanan5ed3ac12012-07-13 20:01:02 +00001962 if (!name) {
1963 // Determine whether this field corresponds to an anonymous
1964 // struct or union.
1965 if (const TagType *TagT = field->getType()->getAs<TagType>()) {
1966 if (RecordDecl *Rec = dyn_cast<RecordDecl>(TagT->getDecl()))
1967 if (!Rec->getDeclName()) {
1968 Rec->setAnonymousStructOrUnion(true);
1969 field->setImplicit();
1970
1971 }
1972 }
1973 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001974
Greg Clayton8cf05932010-07-22 18:30:50 +00001975 field->setAccess (ConvertAccessTypeToAccessSpecifier (access));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001976
1977 if (field)
1978 {
1979 record_decl->addDecl(field);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001980
1981#ifdef LLDB_CONFIGURATION_DEBUG
1982 VerifyDecl(field);
1983#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001984 }
1985 }
Greg Clayton9e409562010-07-28 02:04:09 +00001986 else
1987 {
Sean Callanan78e37602011-01-27 04:42:51 +00001988 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001989 if (objc_class_type)
1990 {
Greg Clayton0fffff52010-09-24 05:15:53 +00001991 bool is_synthesized = false;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001992 field = ClangASTContext::AddObjCClassIVar (ast,
Sean Callanan6e6a7c72010-09-16 20:01:08 +00001993 record_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001994 name,
1995 field_type,
1996 access,
1997 bitfield_bit_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00001998 is_synthesized);
Greg Clayton9e409562010-07-28 02:04:09 +00001999 }
2000 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002001 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002002 return field;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002003}
2004
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002005static clang::AccessSpecifier UnifyAccessSpecifiers (clang::AccessSpecifier lhs,
2006 clang::AccessSpecifier rhs)
2007{
2008 clang::AccessSpecifier ret = lhs;
2009
2010 // Make the access equal to the stricter of the field and the nested field's access
2011 switch (ret)
2012 {
2013 case clang::AS_none:
2014 break;
2015 case clang::AS_private:
2016 break;
2017 case clang::AS_protected:
2018 if (rhs == AS_private)
2019 ret = AS_private;
2020 break;
2021 case clang::AS_public:
2022 ret = rhs;
2023 break;
2024 }
2025
2026 return ret;
2027}
2028
2029void
2030ClangASTContext::BuildIndirectFields (clang::ASTContext *ast,
2031 lldb::clang_type_t record_clang_type)
2032{
2033 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
2034
2035 const RecordType *record_type = record_qual_type->getAs<RecordType>();
2036
2037 if (!record_type)
2038 return;
2039
2040 RecordDecl *record_decl = record_type->getDecl();
2041
2042 if (!record_decl)
2043 return;
2044
2045 typedef llvm::SmallVector <IndirectFieldDecl *, 1> IndirectFieldVector;
2046
2047 IndirectFieldVector indirect_fields;
2048
2049 for (RecordDecl::field_iterator fi = record_decl->field_begin(), fe = record_decl->field_end();
2050 fi != fe;
2051 ++fi)
2052 {
2053 if (fi->isAnonymousStructOrUnion())
2054 {
2055 QualType field_qual_type = fi->getType();
2056
2057 const RecordType *field_record_type = field_qual_type->getAs<RecordType>();
2058
2059 if (!field_record_type)
2060 continue;
2061
2062 RecordDecl *field_record_decl = field_record_type->getDecl();
2063
2064 if (!field_record_decl)
2065 continue;
2066
2067 for (RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
2068 di != de;
2069 ++di)
2070 {
2071 if (FieldDecl *nested_field_decl = dyn_cast<FieldDecl>(*di))
2072 {
2073 NamedDecl **chain = new (*ast) NamedDecl*[2];
2074 chain[0] = *fi;
2075 chain[1] = nested_field_decl;
2076 IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*ast,
2077 record_decl,
2078 SourceLocation(),
2079 nested_field_decl->getIdentifier(),
2080 nested_field_decl->getType(),
2081 chain,
2082 2);
2083
2084 indirect_field->setAccess(UnifyAccessSpecifiers(fi->getAccess(),
2085 nested_field_decl->getAccess()));
2086
2087 indirect_fields.push_back(indirect_field);
2088 }
2089 else if (IndirectFieldDecl *nested_indirect_field_decl = dyn_cast<IndirectFieldDecl>(*di))
2090 {
2091 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
2092 NamedDecl **chain = new (*ast) NamedDecl*[nested_chain_size + 1];
2093 chain[0] = *fi;
2094
2095 int chain_index = 1;
2096 for (IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
2097 nce = nested_indirect_field_decl->chain_end();
2098 nci < nce;
2099 ++nci)
2100 {
2101 chain[chain_index] = *nci;
2102 chain_index++;
2103 }
2104
2105 IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*ast,
2106 record_decl,
2107 SourceLocation(),
2108 nested_indirect_field_decl->getIdentifier(),
2109 nested_indirect_field_decl->getType(),
2110 chain,
2111 nested_chain_size + 1);
2112
2113 indirect_field->setAccess(UnifyAccessSpecifiers(fi->getAccess(),
2114 nested_indirect_field_decl->getAccess()));
2115
2116 indirect_fields.push_back(indirect_field);
2117 }
2118 }
2119 }
2120 }
2121
2122 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
2123 ifi < ife;
2124 ++ifi)
2125 {
2126 record_decl->addDecl(*ifi);
2127 }
2128}
2129
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002130bool
2131ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
2132{
2133 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
2134}
2135
2136bool
2137ClangASTContext::FieldIsBitfield
2138(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002139 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002140 FieldDecl* field,
2141 uint32_t& bitfield_bit_size
2142)
2143{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002144 if (ast == NULL || field == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002145 return false;
2146
2147 if (field->isBitField())
2148 {
2149 Expr* bit_width_expr = field->getBitWidth();
2150 if (bit_width_expr)
2151 {
2152 llvm::APSInt bit_width_apsint;
Greg Clayton6beaaa62011-01-17 03:46:26 +00002153 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002154 {
2155 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
2156 return true;
2157 }
2158 }
2159 }
2160 return false;
2161}
2162
2163bool
2164ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
2165{
2166 if (record_decl == NULL)
2167 return false;
2168
2169 if (!record_decl->field_empty())
2170 return true;
2171
2172 // No fields, lets check this is a CXX record and check the base classes
2173 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2174 if (cxx_record_decl)
2175 {
2176 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2177 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2178 base_class != base_class_end;
2179 ++base_class)
2180 {
2181 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2182 if (RecordHasFields(base_class_decl))
2183 return true;
2184 }
2185 }
2186 return false;
2187}
2188
2189void
Greg Clayton6beaaa62011-01-17 03:46:26 +00002190ClangASTContext::SetDefaultAccessForRecordFields (clang_type_t clang_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002191{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002192 if (clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002193 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002194 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
2195
Sean Callanan78e37602011-01-27 04:42:51 +00002196 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
Greg Clayton6beaaa62011-01-17 03:46:26 +00002197 if (record_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002198 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002199 RecordDecl *record_decl = record_type->getDecl();
2200 if (record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002201 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002202 uint32_t field_idx;
2203 RecordDecl::field_iterator field, field_end;
2204 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2205 field != field_end;
2206 ++field, ++field_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002207 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002208 // If no accessibility was assigned, assign the correct one
2209 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2210 field->setAccess ((AccessSpecifier)default_accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002211 }
2212 }
2213 }
2214 }
2215}
2216
2217#pragma mark C++ Base Classes
2218
2219CXXBaseSpecifier *
Greg Clayton1be10fc2010-09-29 01:12:09 +00002220ClangASTContext::CreateBaseClassSpecifier (clang_type_t base_class_type, AccessType access, bool is_virtual, bool base_of_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002221{
2222 if (base_class_type)
Greg Claytone6371122010-07-30 20:30:44 +00002223 return new CXXBaseSpecifier (SourceRange(),
2224 is_virtual,
2225 base_of_class,
2226 ConvertAccessTypeToAccessSpecifier (access),
Sean Callanan2c777c42011-01-18 23:32:05 +00002227 getASTContext()->CreateTypeSourceInfo (QualType::getFromOpaquePtr(base_class_type)),
2228 SourceLocation());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002229 return NULL;
2230}
2231
Greg Clayton0b42ac32010-07-02 01:29:13 +00002232void
2233ClangASTContext::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes)
2234{
2235 for (unsigned i=0; i<num_base_classes; ++i)
2236 {
2237 delete base_classes[i];
2238 base_classes[i] = NULL;
2239 }
2240}
2241
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002242bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002243ClangASTContext::SetBaseClassesForClassType (clang_type_t class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002244{
2245 if (class_clang_type)
2246 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002247 CXXRecordDecl *cxx_record_decl = QualType::getFromOpaquePtr(class_clang_type)->getAsCXXRecordDecl();
2248 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002249 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002250 cxx_record_decl->setBases(base_classes, num_base_classes);
2251 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002252 }
2253 }
2254 return false;
2255}
Greg Clayton8cf05932010-07-22 18:30:50 +00002256#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002257
Greg Clayton1be10fc2010-09-29 01:12:09 +00002258clang_type_t
Sean Callananad880762012-04-18 01:06:17 +00002259ClangASTContext::CreateObjCClass
Greg Clayton8cf05932010-07-22 18:30:50 +00002260(
2261 const char *name,
2262 DeclContext *decl_ctx,
2263 bool isForwardDecl,
Sean Callananad880762012-04-18 01:06:17 +00002264 bool isInternal,
Jim Ingham379397632012-10-27 02:54:13 +00002265 ClangASTMetadata *metadata
Greg Clayton8cf05932010-07-22 18:30:50 +00002266)
2267{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002268 ASTContext *ast = getASTContext();
2269 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00002270 assert (name && name[0]);
2271 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00002272 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00002273
2274 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
2275 // we will need to update this code. I was told to currently always use
2276 // the CXXRecordDecl class since we often don't know from debug information
2277 // if something is struct or a class, so we default to always use the more
2278 // complete definition just in case.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002279 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00002280 decl_ctx,
2281 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00002282 &ast->Idents.get(name),
Sean Callanan5b26f272012-02-04 08:49:35 +00002283 NULL,
Greg Clayton8cf05932010-07-22 18:30:50 +00002284 SourceLocation(),
Sean Callanan5b26f272012-02-04 08:49:35 +00002285 /*isForwardDecl,*/
Greg Clayton8cf05932010-07-22 18:30:50 +00002286 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00002287
Jim Ingham379397632012-10-27 02:54:13 +00002288 if (decl && metadata)
2289 SetMetadata(ast, (uintptr_t)decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00002290
Greg Clayton6beaaa62011-01-17 03:46:26 +00002291 return ast->getObjCInterfaceType(decl).getAsOpaquePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002292}
2293
2294bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002295ClangASTContext::SetObjCSuperClass (clang_type_t class_opaque_type, clang_type_t super_opaque_type)
Greg Clayton8cf05932010-07-22 18:30:50 +00002296{
2297 if (class_opaque_type && super_opaque_type)
2298 {
2299 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2300 QualType super_qual_type(QualType::getFromOpaquePtr(super_opaque_type));
Sean Callanan78e37602011-01-27 04:42:51 +00002301 const clang::Type *class_type = class_qual_type.getTypePtr();
2302 const clang::Type *super_type = super_qual_type.getTypePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002303 if (class_type && super_type)
2304 {
Sean Callanan78e37602011-01-27 04:42:51 +00002305 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
2306 const ObjCObjectType *objc_super_type = dyn_cast<ObjCObjectType>(super_type);
Greg Clayton8cf05932010-07-22 18:30:50 +00002307 if (objc_class_type && objc_super_type)
2308 {
2309 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2310 ObjCInterfaceDecl *super_interface_decl = objc_super_type->getInterface();
2311 if (class_interface_decl && super_interface_decl)
2312 {
2313 class_interface_decl->setSuperClass(super_interface_decl);
2314 return true;
2315 }
2316 }
2317 }
2318 }
2319 return false;
2320}
2321
2322
Jim Inghame3ae82a2011-11-12 01:36:43 +00002323FieldDecl *
Greg Clayton8cf05932010-07-22 18:30:50 +00002324ClangASTContext::AddObjCClassIVar
2325(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002326 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002327 clang_type_t class_opaque_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00002328 const char *name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002329 clang_type_t ivar_opaque_type,
Greg Clayton8cf05932010-07-22 18:30:50 +00002330 AccessType access,
2331 uint32_t bitfield_bit_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00002332 bool is_synthesized
Greg Clayton8cf05932010-07-22 18:30:50 +00002333)
2334{
2335 if (class_opaque_type == NULL || ivar_opaque_type == NULL)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002336 return NULL;
Greg Clayton8cf05932010-07-22 18:30:50 +00002337
Jim Inghame3ae82a2011-11-12 01:36:43 +00002338 ObjCIvarDecl *field = NULL;
2339
Greg Clayton6beaaa62011-01-17 03:46:26 +00002340 IdentifierTable *identifier_table = &ast->Idents;
Greg Clayton8cf05932010-07-22 18:30:50 +00002341
Greg Clayton6beaaa62011-01-17 03:46:26 +00002342 assert (ast != NULL);
Greg Clayton8cf05932010-07-22 18:30:50 +00002343 assert (identifier_table != NULL);
2344
2345 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2346
Sean Callanan78e37602011-01-27 04:42:51 +00002347 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00002348 if (class_type)
2349 {
Sean Callanan78e37602011-01-27 04:42:51 +00002350 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton8cf05932010-07-22 18:30:50 +00002351
2352 if (objc_class_type)
2353 {
2354 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2355
2356 if (class_interface_decl)
2357 {
2358 clang::Expr *bit_width = NULL;
2359 if (bitfield_bit_size != 0)
2360 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002361 APInt bitfield_bit_size_apint(ast->getTypeSize(ast->IntTy), bitfield_bit_size);
2362 bit_width = new (*ast)IntegerLiteral (*ast, bitfield_bit_size_apint, ast->IntTy, SourceLocation());
Greg Clayton8cf05932010-07-22 18:30:50 +00002363 }
2364
Jim Inghame3ae82a2011-11-12 01:36:43 +00002365 field = ObjCIvarDecl::Create (*ast,
2366 class_interface_decl,
2367 SourceLocation(),
2368 SourceLocation(),
2369 &identifier_table->get(name), // Identifier
2370 QualType::getFromOpaquePtr(ivar_opaque_type), // Field type
2371 NULL, // TypeSourceInfo *
2372 ConvertAccessTypeToObjCIvarAccessControl (access),
2373 bit_width,
2374 is_synthesized);
Greg Clayton9e409562010-07-28 02:04:09 +00002375
2376 if (field)
2377 {
2378 class_interface_decl->addDecl(field);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002379
2380#ifdef LLDB_CONFIGURATION_DEBUG
2381 VerifyDecl(field);
2382#endif
2383
Jim Inghame3ae82a2011-11-12 01:36:43 +00002384 return field;
Greg Clayton9e409562010-07-28 02:04:09 +00002385 }
Greg Clayton8cf05932010-07-22 18:30:50 +00002386 }
2387 }
2388 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002389 return NULL;
2390}
2391
2392bool
2393ClangASTContext::AddObjCClassProperty
2394(
2395 ASTContext *ast,
2396 clang_type_t class_opaque_type,
2397 const char *property_name,
2398 clang_type_t property_opaque_type,
2399 ObjCIvarDecl *ivar_decl,
2400 const char *property_setter_name,
2401 const char *property_getter_name,
Sean Callananad880762012-04-18 01:06:17 +00002402 uint32_t property_attributes,
Jim Ingham379397632012-10-27 02:54:13 +00002403 ClangASTMetadata *metadata
Jim Inghame3ae82a2011-11-12 01:36:43 +00002404)
2405{
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002406 if (class_opaque_type == NULL || property_name == NULL || property_name[0] == '\0')
Jim Inghame3ae82a2011-11-12 01:36:43 +00002407 return false;
2408
2409 IdentifierTable *identifier_table = &ast->Idents;
2410
2411 assert (ast != NULL);
2412 assert (identifier_table != NULL);
2413
2414 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2415 const clang::Type *class_type = class_qual_type.getTypePtr();
2416 if (class_type)
2417 {
2418 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
2419
2420 if (objc_class_type)
2421 {
2422 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2423
Greg Clayton23f59502012-07-17 03:23:13 +00002424 clang_type_t property_opaque_type_to_access = NULL;
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002425
2426 if (property_opaque_type)
2427 property_opaque_type_to_access = property_opaque_type;
2428 else if (ivar_decl)
2429 property_opaque_type_to_access = ivar_decl->getType().getAsOpaquePtr();
2430
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002431 if (class_interface_decl && property_opaque_type_to_access)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002432 {
2433 clang::TypeSourceInfo *prop_type_source;
2434 if (ivar_decl)
2435 prop_type_source = ast->CreateTypeSourceInfo (ivar_decl->getType());
2436 else
2437 prop_type_source = ast->CreateTypeSourceInfo (QualType::getFromOpaquePtr(property_opaque_type));
2438
2439 ObjCPropertyDecl *property_decl = ObjCPropertyDecl::Create(*ast,
2440 class_interface_decl,
2441 SourceLocation(), // Source Location
2442 &identifier_table->get(property_name),
2443 SourceLocation(), //Source Location for AT
Sean Callanand5f33a82012-03-01 02:03:47 +00002444 SourceLocation(), //Source location for (
Jim Inghame3ae82a2011-11-12 01:36:43 +00002445 prop_type_source
2446 );
Sean Callananad880762012-04-18 01:06:17 +00002447
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002448 if (property_decl)
2449 {
Jim Ingham379397632012-10-27 02:54:13 +00002450 if (metadata)
2451 SetMetadata(ast, (uintptr_t)property_decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00002452
Jim Inghame3ae82a2011-11-12 01:36:43 +00002453 class_interface_decl->addDecl (property_decl);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002454
2455 Selector setter_sel, getter_sel;
2456
Jim Inghame3ae82a2011-11-12 01:36:43 +00002457 if (property_setter_name != NULL)
2458 {
2459 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
2460 clang::IdentifierInfo *setter_ident = &identifier_table->get(property_setter_no_colon.c_str());
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002461 setter_sel = ast->Selectors.getSelector(1, &setter_ident);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002462 }
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002463 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
2464 {
2465 std::string setter_sel_string("set");
2466 setter_sel_string.push_back(::toupper(property_name[0]));
2467 setter_sel_string.append(&property_name[1]);
2468 clang::IdentifierInfo *setter_ident = &identifier_table->get(setter_sel_string.c_str());
2469 setter_sel = ast->Selectors.getSelector(1, &setter_ident);
2470 }
Sean Callanana6582262012-04-05 00:12:52 +00002471 property_decl->setSetterName(setter_sel);
2472 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002473
2474 if (property_getter_name != NULL)
2475 {
2476 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_getter_name);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002477 getter_sel = ast->Selectors.getSelector(0, &getter_ident);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002478 }
2479 else
2480 {
2481 clang::IdentifierInfo *getter_ident = &identifier_table->get(property_name);
2482 getter_sel = ast->Selectors.getSelector(0, &getter_ident);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002483 }
Sean Callanana6582262012-04-05 00:12:52 +00002484 property_decl->setGetterName(getter_sel);
2485 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
Jim Inghame3ae82a2011-11-12 01:36:43 +00002486
2487 if (ivar_decl)
2488 property_decl->setPropertyIvarDecl (ivar_decl);
2489
2490 if (property_attributes & DW_APPLE_PROPERTY_readonly)
2491 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
2492 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
2493 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
2494 if (property_attributes & DW_APPLE_PROPERTY_assign)
2495 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
2496 if (property_attributes & DW_APPLE_PROPERTY_retain)
2497 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
2498 if (property_attributes & DW_APPLE_PROPERTY_copy)
2499 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
2500 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
2501 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002502
2503 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
2504 {
2505 QualType result_type = QualType::getFromOpaquePtr(property_opaque_type_to_access);
2506
2507 const bool isInstance = true;
2508 const bool isVariadic = false;
2509 const bool isSynthesized = false;
2510 const bool isImplicitlyDeclared = true;
2511 const bool isDefined = false;
2512 const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
2513 const bool HasRelatedResultType = false;
2514
2515 ObjCMethodDecl *getter = ObjCMethodDecl::Create(*ast,
2516 SourceLocation(),
2517 SourceLocation(),
2518 getter_sel,
2519 result_type,
2520 NULL,
2521 class_interface_decl,
2522 isInstance,
2523 isVariadic,
2524 isSynthesized,
2525 isImplicitlyDeclared,
2526 isDefined,
2527 impControl,
2528 HasRelatedResultType);
Sean Callananad880762012-04-18 01:06:17 +00002529
Jim Ingham379397632012-10-27 02:54:13 +00002530 if (getter && metadata)
2531 SetMetadata(ast, (uintptr_t)getter, *metadata);
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002532
2533 getter->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(), ArrayRef<SourceLocation>());
2534
2535 class_interface_decl->addDecl(getter);
2536 }
2537
2538 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
2539 {
2540 QualType result_type = ast->VoidTy;
2541
2542 const bool isInstance = true;
2543 const bool isVariadic = false;
2544 const bool isSynthesized = false;
2545 const bool isImplicitlyDeclared = true;
2546 const bool isDefined = false;
2547 const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
2548 const bool HasRelatedResultType = false;
2549
2550 ObjCMethodDecl *setter = ObjCMethodDecl::Create(*ast,
2551 SourceLocation(),
2552 SourceLocation(),
2553 setter_sel,
2554 result_type,
2555 NULL,
2556 class_interface_decl,
2557 isInstance,
2558 isVariadic,
2559 isSynthesized,
2560 isImplicitlyDeclared,
2561 isDefined,
2562 impControl,
2563 HasRelatedResultType);
2564
Jim Ingham379397632012-10-27 02:54:13 +00002565 if (setter && metadata)
2566 SetMetadata(ast, (uintptr_t)setter, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00002567
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002568 llvm::SmallVector<ParmVarDecl *, 1> params;
2569
2570 params.push_back (ParmVarDecl::Create (*ast,
2571 setter,
2572 SourceLocation(),
2573 SourceLocation(),
2574 NULL, // anonymous
2575 QualType::getFromOpaquePtr(property_opaque_type_to_access),
2576 NULL,
2577 SC_Auto,
2578 SC_Auto,
2579 NULL));
2580
2581 setter->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
2582
2583 class_interface_decl->addDecl(setter);
2584 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002585
2586 return true;
Sean Callanan8a6a6ac2011-12-09 23:24:26 +00002587 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002588 }
2589 }
2590 }
Greg Clayton8cf05932010-07-22 18:30:50 +00002591 return false;
2592}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002593
Greg Clayton9e409562010-07-28 02:04:09 +00002594bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00002595ClangASTContext::ObjCTypeHasIVars (clang_type_t class_opaque_type, bool check_superclass)
Greg Clayton9e409562010-07-28 02:04:09 +00002596{
2597 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2598
Sean Callanan78e37602011-01-27 04:42:51 +00002599 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton9e409562010-07-28 02:04:09 +00002600 if (class_type)
2601 {
Sean Callanan78e37602011-01-27 04:42:51 +00002602 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00002603
2604 if (objc_class_type)
2605 return ObjCDeclHasIVars (objc_class_type->getInterface(), check_superclass);
2606 }
2607 return false;
2608}
2609
2610bool
2611ClangASTContext::ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
2612{
2613 while (class_interface_decl)
2614 {
2615 if (class_interface_decl->ivar_size() > 0)
2616 return true;
2617
2618 if (check_superclass)
2619 class_interface_decl = class_interface_decl->getSuperClass();
2620 else
2621 break;
2622 }
2623 return false;
2624}
Greg Clayton0fffff52010-09-24 05:15:53 +00002625
Greg Clayton1be10fc2010-09-29 01:12:09 +00002626ObjCMethodDecl *
Greg Clayton0fffff52010-09-24 05:15:53 +00002627ClangASTContext::AddMethodToObjCObjectType
2628(
Greg Clayton6beaaa62011-01-17 03:46:26 +00002629 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002630 clang_type_t class_opaque_type,
Greg Clayton0fffff52010-09-24 05:15:53 +00002631 const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
Greg Clayton1be10fc2010-09-29 01:12:09 +00002632 clang_type_t method_opaque_type,
Greg Clayton0fffff52010-09-24 05:15:53 +00002633 lldb::AccessType access
2634)
2635{
2636 if (class_opaque_type == NULL || method_opaque_type == NULL)
2637 return NULL;
2638
Greg Clayton6beaaa62011-01-17 03:46:26 +00002639 IdentifierTable *identifier_table = &ast->Idents;
Greg Clayton0fffff52010-09-24 05:15:53 +00002640
Greg Clayton6beaaa62011-01-17 03:46:26 +00002641 assert (ast != NULL);
Greg Clayton0fffff52010-09-24 05:15:53 +00002642 assert (identifier_table != NULL);
2643
2644 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
2645
Sean Callanan78e37602011-01-27 04:42:51 +00002646 const clang::Type *class_type = class_qual_type.getTypePtr();
Greg Clayton0fffff52010-09-24 05:15:53 +00002647 if (class_type == NULL)
2648 return NULL;
2649
Sean Callanan78e37602011-01-27 04:42:51 +00002650 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
Greg Clayton0fffff52010-09-24 05:15:53 +00002651
2652 if (objc_class_type == NULL)
2653 return NULL;
2654
2655 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2656
2657 if (class_interface_decl == NULL)
2658 return NULL;
Greg Clayton9e409562010-07-28 02:04:09 +00002659
Greg Clayton0fffff52010-09-24 05:15:53 +00002660 const char *selector_start = ::strchr (name, ' ');
2661 if (selector_start == NULL)
2662 return NULL;
2663
2664 selector_start++;
2665 if (!(::isalpha (selector_start[0]) || selector_start[0] == '_'))
2666 return NULL;
2667 llvm::SmallVector<IdentifierInfo *, 12> selector_idents;
2668
Greg Clayton450e3f32010-10-12 02:24:53 +00002669 size_t len = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +00002670 const char *start;
Greg Clayton450e3f32010-10-12 02:24:53 +00002671 //printf ("name = '%s'\n", name);
2672
2673 unsigned num_selectors_with_args = 0;
2674 for (start = selector_start;
Greg Clayton0fffff52010-09-24 05:15:53 +00002675 start && *start != '\0' && *start != ']';
Greg Clayton450e3f32010-10-12 02:24:53 +00002676 start += len)
Greg Clayton0fffff52010-09-24 05:15:53 +00002677 {
Greg Clayton450e3f32010-10-12 02:24:53 +00002678 len = ::strcspn(start, ":]");
Greg Clayton90f90cd2010-10-27 04:01:14 +00002679 bool has_arg = (start[len] == ':');
2680 if (has_arg)
Greg Clayton450e3f32010-10-12 02:24:53 +00002681 ++num_selectors_with_args;
Greg Clayton0fffff52010-09-24 05:15:53 +00002682 selector_idents.push_back (&identifier_table->get (StringRef (start, len)));
Greg Clayton90f90cd2010-10-27 04:01:14 +00002683 if (has_arg)
2684 len += 1;
Greg Clayton0fffff52010-09-24 05:15:53 +00002685 }
2686
2687
2688 if (selector_idents.size() == 0)
2689 return 0;
2690
Greg Clayton6beaaa62011-01-17 03:46:26 +00002691 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
Greg Clayton0fffff52010-09-24 05:15:53 +00002692 selector_idents.data());
2693
2694 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
2695
2696 // Populate the method decl with parameter decls
Sean Callanan78e37602011-01-27 04:42:51 +00002697 const clang::Type *method_type(method_qual_type.getTypePtr());
Greg Clayton0fffff52010-09-24 05:15:53 +00002698
2699 if (method_type == NULL)
2700 return NULL;
2701
Sean Callanan78e37602011-01-27 04:42:51 +00002702 const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type));
Greg Clayton0fffff52010-09-24 05:15:53 +00002703
2704 if (!method_function_prototype)
2705 return NULL;
2706
2707
2708 bool is_variadic = false;
2709 bool is_synthesized = false;
2710 bool is_defined = false;
2711 ObjCMethodDecl::ImplementationControl imp_control = ObjCMethodDecl::None;
2712
2713 const unsigned num_args = method_function_prototype->getNumArgs();
2714
Greg Clayton6beaaa62011-01-17 03:46:26 +00002715 ObjCMethodDecl *objc_method_decl = ObjCMethodDecl::Create (*ast,
Greg Clayton0fffff52010-09-24 05:15:53 +00002716 SourceLocation(), // beginLoc,
2717 SourceLocation(), // endLoc,
2718 method_selector,
2719 method_function_prototype->getResultType(),
2720 NULL, // TypeSourceInfo *ResultTInfo,
2721 GetDeclContextForType (class_opaque_type),
2722 name[0] == '-',
2723 is_variadic,
2724 is_synthesized,
Sean Callanan880e6802011-10-07 23:18:13 +00002725 true, // is_implicitly_declared
Greg Clayton0fffff52010-09-24 05:15:53 +00002726 is_defined,
2727 imp_control,
Sean Callanan880e6802011-10-07 23:18:13 +00002728 false /*has_related_result_type*/);
Greg Clayton0fffff52010-09-24 05:15:53 +00002729
2730
2731 if (objc_method_decl == NULL)
2732 return NULL;
2733
2734 if (num_args > 0)
2735 {
2736 llvm::SmallVector<ParmVarDecl *, 12> params;
2737
2738 for (int param_index = 0; param_index < num_args; ++param_index)
2739 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002740 params.push_back (ParmVarDecl::Create (*ast,
Greg Clayton0fffff52010-09-24 05:15:53 +00002741 objc_method_decl,
2742 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00002743 SourceLocation(),
Greg Clayton0fffff52010-09-24 05:15:53 +00002744 NULL, // anonymous
2745 method_function_prototype->getArgType(param_index),
2746 NULL,
2747 SC_Auto,
2748 SC_Auto,
2749 NULL));
2750 }
2751
Sean Callanan880e6802011-10-07 23:18:13 +00002752 objc_method_decl->setMethodParams(*ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
Greg Clayton0fffff52010-09-24 05:15:53 +00002753 }
2754
2755 class_interface_decl->addDecl (objc_method_decl);
2756
Sean Callanan5e9e1992011-10-26 01:06:27 +00002757#ifdef LLDB_CONFIGURATION_DEBUG
2758 VerifyDecl(objc_method_decl);
2759#endif
Greg Clayton0fffff52010-09-24 05:15:53 +00002760
2761 return objc_method_decl;
2762}
2763
Greg Clayton402230e2012-02-03 01:30:30 +00002764size_t
2765ClangASTContext::GetNumTemplateArguments (clang::ASTContext *ast, clang_type_t clang_type)
2766{
2767 if (clang_type)
2768 {
2769 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2770
2771 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2772 switch (type_class)
2773 {
2774 case clang::Type::Record:
2775 if (GetCompleteQualType (ast, qual_type))
2776 {
2777 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2778 if (cxx_record_decl)
2779 {
2780 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2781 if (template_decl)
2782 return template_decl->getTemplateArgs().size();
2783 }
2784 }
2785 break;
2786
2787 case clang::Type::Typedef:
2788 return ClangASTContext::GetNumTemplateArguments (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2789 default:
2790 break;
2791 }
2792 }
2793 return 0;
2794}
2795
2796clang_type_t
2797ClangASTContext::GetTemplateArgument (clang::ASTContext *ast, clang_type_t clang_type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
2798{
2799 if (clang_type)
2800 {
2801 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2802
2803 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2804 switch (type_class)
2805 {
2806 case clang::Type::Record:
2807 if (GetCompleteQualType (ast, qual_type))
2808 {
2809 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2810 if (cxx_record_decl)
2811 {
2812 const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
2813 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
2814 {
2815 const TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
2816 switch (template_arg.getKind())
2817 {
2818 case clang::TemplateArgument::Null:
2819 kind = eTemplateArgumentKindNull;
2820 return NULL;
2821
2822 case clang::TemplateArgument::Type:
2823 kind = eTemplateArgumentKindType;
2824 return template_arg.getAsType().getAsOpaquePtr();
2825
2826 case clang::TemplateArgument::Declaration:
2827 kind = eTemplateArgumentKindDeclaration;
2828 return NULL;
2829
2830 case clang::TemplateArgument::Integral:
2831 kind = eTemplateArgumentKindIntegral;
2832 return template_arg.getIntegralType().getAsOpaquePtr();
2833
2834 case clang::TemplateArgument::Template:
2835 kind = eTemplateArgumentKindTemplate;
2836 return NULL;
2837
2838 case clang::TemplateArgument::TemplateExpansion:
2839 kind = eTemplateArgumentKindTemplateExpansion;
2840 return NULL;
2841
2842 case clang::TemplateArgument::Expression:
2843 kind = eTemplateArgumentKindExpression;
2844 return NULL;
2845
2846 case clang::TemplateArgument::Pack:
2847 kind = eTemplateArgumentKindPack;
2848 return NULL;
2849
2850 default:
2851 assert (!"Unhandled TemplateArgument::ArgKind");
2852 kind = eTemplateArgumentKindNull;
2853 return NULL;
2854 }
2855 }
2856 }
2857 }
2858 break;
2859
2860 case clang::Type::Typedef:
2861 return ClangASTContext::GetTemplateArgument (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), arg_idx, kind);
2862 default:
2863 break;
2864 }
2865 }
2866 kind = eTemplateArgumentKindNull;
2867 return NULL;
2868}
Greg Clayton0fffff52010-09-24 05:15:53 +00002869
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002870uint32_t
Greg Clayton73b472d2010-10-27 03:32:59 +00002871ClangASTContext::GetTypeInfo
2872(
2873 clang_type_t clang_type,
Greg Clayton6beaaa62011-01-17 03:46:26 +00002874 clang::ASTContext *ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00002875 clang_type_t *pointee_or_element_clang_type
2876)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002877{
2878 if (clang_type == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00002879 return 0;
2880
2881 if (pointee_or_element_clang_type)
2882 *pointee_or_element_clang_type = NULL;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002883
2884 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2885
2886 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2887 switch (type_class)
2888 {
Sean Callanana2424172010-10-25 00:29:48 +00002889 case clang::Type::Builtin:
2890 switch (cast<clang::BuiltinType>(qual_type)->getKind())
2891 {
Sean Callanana2424172010-10-25 00:29:48 +00002892 case clang::BuiltinType::ObjCId:
2893 case clang::BuiltinType::ObjCClass:
Greg Clayton6beaaa62011-01-17 03:46:26 +00002894 if (ast && pointee_or_element_clang_type)
2895 *pointee_or_element_clang_type = ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Sean Callanana2424172010-10-25 00:29:48 +00002896 return eTypeIsBuiltIn | eTypeIsPointer | eTypeHasValue;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002897 break;
2898 case clang::BuiltinType::Bool:
2899 case clang::BuiltinType::Char_U:
2900 case clang::BuiltinType::UChar:
2901 case clang::BuiltinType::WChar_U:
2902 case clang::BuiltinType::Char16:
2903 case clang::BuiltinType::Char32:
2904 case clang::BuiltinType::UShort:
2905 case clang::BuiltinType::UInt:
2906 case clang::BuiltinType::ULong:
2907 case clang::BuiltinType::ULongLong:
2908 case clang::BuiltinType::UInt128:
2909 case clang::BuiltinType::Char_S:
2910 case clang::BuiltinType::SChar:
2911 case clang::BuiltinType::WChar_S:
2912 case clang::BuiltinType::Short:
2913 case clang::BuiltinType::Int:
2914 case clang::BuiltinType::Long:
2915 case clang::BuiltinType::LongLong:
2916 case clang::BuiltinType::Int128:
2917 case clang::BuiltinType::Float:
2918 case clang::BuiltinType::Double:
2919 case clang::BuiltinType::LongDouble:
2920 return eTypeIsBuiltIn | eTypeHasValue | eTypeIsScalar;
Greg Clayton73b472d2010-10-27 03:32:59 +00002921 default:
2922 break;
Sean Callanana2424172010-10-25 00:29:48 +00002923 }
2924 return eTypeIsBuiltIn | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002925
2926 case clang::Type::BlockPointer:
2927 if (pointee_or_element_clang_type)
2928 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2929 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
2930
Greg Clayton49462ea2011-01-15 02:52:14 +00002931 case clang::Type::Complex: return eTypeIsBuiltIn | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002932
2933 case clang::Type::ConstantArray:
2934 case clang::Type::DependentSizedArray:
2935 case clang::Type::IncompleteArray:
2936 case clang::Type::VariableArray:
2937 if (pointee_or_element_clang_type)
2938 *pointee_or_element_clang_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
2939 return eTypeHasChildren | eTypeIsArray;
2940
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002941 case clang::Type::DependentName: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002942 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
2943 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
2944 case clang::Type::Decltype: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00002945
2946 case clang::Type::Enum:
2947 if (pointee_or_element_clang_type)
2948 *pointee_or_element_clang_type = cast<EnumType>(qual_type)->getDecl()->getIntegerType().getAsOpaquePtr();
2949 return eTypeIsEnumeration | eTypeHasValue;
2950
Sean Callanan912855f2011-08-11 23:56:13 +00002951 case clang::Type::Elaborated:
2952 return ClangASTContext::GetTypeInfo (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2953 ast,
2954 pointee_or_element_clang_type);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002955 case clang::Type::ExtVector: return eTypeHasChildren | eTypeIsVector;
2956 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
2957 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002958 case clang::Type::InjectedClassName: return 0;
Greg Clayton73b472d2010-10-27 03:32:59 +00002959
2960 case clang::Type::LValueReference:
2961 case clang::Type::RValueReference:
2962 if (pointee_or_element_clang_type)
2963 *pointee_or_element_clang_type = cast<ReferenceType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr();
2964 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
2965
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002966 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
Greg Clayton73b472d2010-10-27 03:32:59 +00002967
2968 case clang::Type::ObjCObjectPointer:
2969 if (pointee_or_element_clang_type)
2970 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2971 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
2972
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002973 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
2974 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
Greg Clayton73b472d2010-10-27 03:32:59 +00002975
2976 case clang::Type::Pointer:
2977 if (pointee_or_element_clang_type)
2978 *pointee_or_element_clang_type = qual_type->getPointeeType().getAsOpaquePtr();
2979 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
2980
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002981 case clang::Type::Record:
2982 if (qual_type->getAsCXXRecordDecl())
2983 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
2984 else
2985 return eTypeHasChildren | eTypeIsStructUnion;
2986 break;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002987 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
2988 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
2989 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
Greg Clayton73b472d2010-10-27 03:32:59 +00002990
2991 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00002992 return eTypeIsTypedef | ClangASTContext::GetTypeInfo (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00002993 ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00002994 pointee_or_element_clang_type);
2995
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002996 case clang::Type::TypeOfExpr: return 0;
2997 case clang::Type::TypeOf: return 0;
2998 case clang::Type::UnresolvedUsing: return 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002999 case clang::Type::Vector: return eTypeHasChildren | eTypeIsVector;
3000 default: return 0;
3001 }
3002 return 0;
3003}
3004
Greg Clayton9e409562010-07-28 02:04:09 +00003005
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003006#pragma mark Aggregate Types
3007
3008bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00003009ClangASTContext::IsAggregateType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003010{
3011 if (clang_type == NULL)
3012 return false;
3013
3014 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3015
Greg Clayton737b9322010-09-13 03:32:57 +00003016 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3017 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003018 {
Greg Claytone1a916a2010-07-21 22:12:05 +00003019 case clang::Type::IncompleteArray:
3020 case clang::Type::VariableArray:
3021 case clang::Type::ConstantArray:
3022 case clang::Type::ExtVector:
3023 case clang::Type::Vector:
3024 case clang::Type::Record:
Greg Clayton9e409562010-07-28 02:04:09 +00003025 case clang::Type::ObjCObject:
3026 case clang::Type::ObjCInterface:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003027 return true;
Sean Callanan912855f2011-08-11 23:56:13 +00003028 case clang::Type::Elaborated:
3029 return ClangASTContext::IsAggregateType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Claytone1a916a2010-07-21 22:12:05 +00003030 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00003031 return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003032
3033 default:
3034 break;
3035 }
3036 // The clang type does have a value
3037 return false;
3038}
3039
3040uint32_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00003041ClangASTContext::GetNumChildren (clang::ASTContext *ast, clang_type_t clang_type, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003042{
Greg Clayton6beaaa62011-01-17 03:46:26 +00003043 if (clang_type == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003044 return 0;
3045
3046 uint32_t num_children = 0;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003047 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00003048 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3049 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003050 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003051 case clang::Type::Builtin:
3052 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3053 {
Greg Clayton73b472d2010-10-27 03:32:59 +00003054 case clang::BuiltinType::ObjCId: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003055 case clang::BuiltinType::ObjCClass: // child is Class
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003056 num_children = 1;
Greg Clayton73b472d2010-10-27 03:32:59 +00003057 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003058
3059 default:
3060 break;
3061 }
3062 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00003063
Greg Clayton49462ea2011-01-15 02:52:14 +00003064 case clang::Type::Complex: return 0;
Greg Clayton54979cd2010-12-15 05:08:08 +00003065
Greg Claytone1a916a2010-07-21 22:12:05 +00003066 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00003067 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003068 {
3069 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3070 const RecordDecl *record_decl = record_type->getDecl();
3071 assert(record_decl);
3072 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3073 if (cxx_record_decl)
3074 {
3075 if (omit_empty_base_classes)
3076 {
3077 // Check each base classes to see if it or any of its
3078 // base classes contain any fields. This can help
3079 // limit the noise in variable views by not having to
3080 // show base classes that contain no members.
3081 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3082 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3083 base_class != base_class_end;
3084 ++base_class)
3085 {
3086 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3087
3088 // Skip empty base classes
3089 if (RecordHasFields(base_class_decl) == false)
3090 continue;
3091
3092 num_children++;
3093 }
3094 }
3095 else
3096 {
3097 // Include all base classes
3098 num_children += cxx_record_decl->getNumBases();
3099 }
3100
3101 }
3102 RecordDecl::field_iterator field, field_end;
3103 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3104 ++num_children;
3105 }
3106 break;
3107
Greg Clayton9e409562010-07-28 02:04:09 +00003108 case clang::Type::ObjCObject:
3109 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00003110 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00003111 {
Sean Callanan78e37602011-01-27 04:42:51 +00003112 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003113 assert (objc_class_type);
3114 if (objc_class_type)
3115 {
3116 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3117
3118 if (class_interface_decl)
3119 {
3120
3121 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3122 if (superclass_interface_decl)
3123 {
3124 if (omit_empty_base_classes)
3125 {
3126 if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true))
3127 ++num_children;
3128 }
3129 else
3130 ++num_children;
3131 }
3132
3133 num_children += class_interface_decl->ivar_size();
3134 }
3135 }
3136 }
3137 break;
3138
3139 case clang::Type::ObjCObjectPointer:
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003140 {
Sean Callanan78e37602011-01-27 04:42:51 +00003141 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003142 QualType pointee_type = pointer_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003143 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3144 pointee_type.getAsOpaquePtr(),
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003145 omit_empty_base_classes);
3146 // If this type points to a simple type, then it has 1 child
3147 if (num_pointee_children == 0)
3148 num_children = 1;
3149 else
3150 num_children = num_pointee_children;
3151 }
3152 break;
Greg Clayton9e409562010-07-28 02:04:09 +00003153
Greg Claytone1a916a2010-07-21 22:12:05 +00003154 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003155 num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
3156 break;
3157
Greg Claytone1a916a2010-07-21 22:12:05 +00003158 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003159 {
Sean Callanan78e37602011-01-27 04:42:51 +00003160 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003161 QualType pointee_type (pointer_type->getPointeeType());
Greg Clayton6beaaa62011-01-17 03:46:26 +00003162 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3163 pointee_type.getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00003164 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003165 if (num_pointee_children == 0)
Greg Clayton54979cd2010-12-15 05:08:08 +00003166 {
3167 // We have a pointer to a pointee type that claims it has no children.
3168 // We will want to look at
3169 num_children = ClangASTContext::GetNumPointeeChildren (pointee_type.getAsOpaquePtr());
3170 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003171 else
3172 num_children = num_pointee_children;
3173 }
3174 break;
3175
Greg Clayton73b472d2010-10-27 03:32:59 +00003176 case clang::Type::LValueReference:
3177 case clang::Type::RValueReference:
3178 {
Sean Callanan78e37602011-01-27 04:42:51 +00003179 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton73b472d2010-10-27 03:32:59 +00003180 QualType pointee_type = reference_type->getPointeeType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00003181 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (ast,
3182 pointee_type.getAsOpaquePtr(),
Greg Clayton73b472d2010-10-27 03:32:59 +00003183 omit_empty_base_classes);
3184 // If this type points to a simple type, then it has 1 child
3185 if (num_pointee_children == 0)
3186 num_children = 1;
3187 else
3188 num_children = num_pointee_children;
3189 }
3190 break;
3191
3192
Greg Claytone1a916a2010-07-21 22:12:05 +00003193 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00003194 num_children = ClangASTContext::GetNumChildren (ast,
3195 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3196 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003197 break;
Sean Callanan912855f2011-08-11 23:56:13 +00003198
3199 case clang::Type::Elaborated:
3200 num_children = ClangASTContext::GetNumChildren (ast,
3201 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3202 omit_empty_base_classes);
3203 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003204
3205 default:
3206 break;
3207 }
3208 return num_children;
3209}
3210
Greg Claytonbf2331c2011-09-09 23:04:00 +00003211uint32_t
3212ClangASTContext::GetNumDirectBaseClasses (clang::ASTContext *ast, clang_type_t clang_type)
3213{
3214 if (clang_type == NULL)
3215 return 0;
3216
3217 uint32_t count = 0;
3218 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3219 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3220 switch (type_class)
3221 {
3222 case clang::Type::Record:
3223 if (GetCompleteQualType (ast, qual_type))
3224 {
3225 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3226 if (cxx_record_decl)
3227 count = cxx_record_decl->getNumBases();
3228 }
3229 break;
3230
3231 case clang::Type::ObjCObject:
3232 case clang::Type::ObjCInterface:
3233 if (GetCompleteQualType (ast, qual_type))
3234 {
3235 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3236 if (objc_class_type)
3237 {
3238 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3239
3240 if (class_interface_decl && class_interface_decl->getSuperClass())
3241 count = 1;
3242 }
3243 }
3244 break;
3245
3246
3247 case clang::Type::Typedef:
3248 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3249 break;
3250
3251 case clang::Type::Elaborated:
3252 count = ClangASTContext::GetNumDirectBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3253 break;
3254
3255 default:
3256 break;
3257 }
3258 return count;
3259}
3260
3261uint32_t
3262ClangASTContext::GetNumVirtualBaseClasses (clang::ASTContext *ast,
3263 clang_type_t clang_type)
3264{
3265 if (clang_type == NULL)
3266 return 0;
3267
3268 uint32_t count = 0;
3269 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3270 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3271 switch (type_class)
3272 {
3273 case clang::Type::Record:
3274 if (GetCompleteQualType (ast, qual_type))
3275 {
3276 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3277 if (cxx_record_decl)
3278 count = cxx_record_decl->getNumVBases();
3279 }
3280 break;
3281
3282 case clang::Type::Typedef:
3283 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3284 break;
3285
3286 case clang::Type::Elaborated:
3287 count = ClangASTContext::GetNumVirtualBaseClasses (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3288 break;
3289
3290 default:
3291 break;
3292 }
3293 return count;
3294}
3295
3296uint32_t
3297ClangASTContext::GetNumFields (clang::ASTContext *ast, clang_type_t clang_type)
3298{
3299 if (clang_type == NULL)
3300 return 0;
3301
3302 uint32_t count = 0;
3303 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3304 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3305 switch (type_class)
3306 {
3307 case clang::Type::Record:
3308 if (GetCompleteQualType (ast, qual_type))
3309 {
3310 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
3311 if (record_type)
3312 {
3313 RecordDecl *record_decl = record_type->getDecl();
3314 if (record_decl)
3315 {
3316 uint32_t field_idx = 0;
3317 RecordDecl::field_iterator field, field_end;
3318 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
3319 ++field_idx;
3320 count = field_idx;
3321 }
3322 }
3323 }
3324 break;
3325
3326 case clang::Type::Typedef:
3327 count = ClangASTContext::GetNumFields (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3328 break;
3329
3330 case clang::Type::Elaborated:
3331 count = ClangASTContext::GetNumFields (ast, cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3332 break;
3333
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003334 case clang::Type::ObjCObject:
3335 case clang::Type::ObjCInterface:
3336 if (GetCompleteQualType (ast, qual_type))
3337 {
3338 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3339 if (objc_class_type)
3340 {
3341 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3342
3343 if (class_interface_decl)
3344 count = class_interface_decl->ivar_size();
3345 }
3346 }
3347 break;
3348
Greg Claytonbf2331c2011-09-09 23:04:00 +00003349 default:
3350 break;
3351 }
3352 return count;
3353}
3354
3355clang_type_t
3356ClangASTContext::GetDirectBaseClassAtIndex (clang::ASTContext *ast,
3357 clang_type_t clang_type,
3358 uint32_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003359 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003360{
3361 if (clang_type == NULL)
3362 return 0;
3363
3364 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3365 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3366 switch (type_class)
3367 {
3368 case clang::Type::Record:
3369 if (GetCompleteQualType (ast, qual_type))
3370 {
3371 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3372 if (cxx_record_decl)
3373 {
3374 uint32_t curr_idx = 0;
3375 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3376 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3377 base_class != base_class_end;
3378 ++base_class, ++curr_idx)
3379 {
3380 if (curr_idx == idx)
3381 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003382 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003383 {
3384 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3385 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3386// if (base_class->isVirtual())
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003387// *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003388// else
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003389 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003390 }
3391 return base_class->getType().getAsOpaquePtr();
3392 }
3393 }
3394 }
3395 }
3396 break;
3397
3398 case clang::Type::ObjCObject:
3399 case clang::Type::ObjCInterface:
3400 if (idx == 0 && GetCompleteQualType (ast, qual_type))
3401 {
3402 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
3403 if (objc_class_type)
3404 {
3405 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3406
3407 if (class_interface_decl)
3408 {
3409 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3410 if (superclass_interface_decl)
3411 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003412 if (bit_offset_ptr)
3413 *bit_offset_ptr = 0;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003414 return ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr();
3415 }
3416 }
3417 }
3418 }
3419 break;
3420
3421
3422 case clang::Type::Typedef:
3423 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3424 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3425 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003426 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003427
3428 case clang::Type::Elaborated:
3429 return ClangASTContext::GetDirectBaseClassAtIndex (ast,
3430 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3431 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003432 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003433
3434 default:
3435 break;
3436 }
3437 return NULL;
3438}
3439
3440clang_type_t
3441ClangASTContext::GetVirtualBaseClassAtIndex (clang::ASTContext *ast,
3442 clang_type_t clang_type,
3443 uint32_t idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003444 uint32_t *bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003445{
3446 if (clang_type == NULL)
3447 return 0;
3448
3449 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3450 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3451 switch (type_class)
3452 {
3453 case clang::Type::Record:
3454 if (GetCompleteQualType (ast, qual_type))
3455 {
3456 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3457 if (cxx_record_decl)
3458 {
3459 uint32_t curr_idx = 0;
3460 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3461 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
3462 base_class != base_class_end;
3463 ++base_class, ++curr_idx)
3464 {
3465 if (curr_idx == idx)
3466 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003467 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003468 {
3469 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(cxx_record_decl);
3470 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003471 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Greg Claytonbf2331c2011-09-09 23:04:00 +00003472
3473 }
3474 return base_class->getType().getAsOpaquePtr();
3475 }
3476 }
3477 }
3478 }
3479 break;
3480
3481 case clang::Type::Typedef:
3482 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3483 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3484 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003485 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003486
3487 case clang::Type::Elaborated:
3488 return ClangASTContext::GetVirtualBaseClassAtIndex (ast,
3489 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3490 idx,
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003491 bit_offset_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003492
3493 default:
3494 break;
3495 }
3496 return NULL;
3497}
3498
3499clang_type_t
3500ClangASTContext::GetFieldAtIndex (clang::ASTContext *ast,
3501 clang_type_t clang_type,
3502 uint32_t idx,
3503 std::string& name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003504 uint64_t *bit_offset_ptr,
3505 uint32_t *bitfield_bit_size_ptr,
3506 bool *is_bitfield_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003507{
3508 if (clang_type == NULL)
3509 return 0;
3510
3511 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3512 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3513 switch (type_class)
3514 {
3515 case clang::Type::Record:
3516 if (GetCompleteQualType (ast, qual_type))
3517 {
3518 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3519 const RecordDecl *record_decl = record_type->getDecl();
3520 uint32_t field_idx = 0;
3521 RecordDecl::field_iterator field, field_end;
3522 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
3523 {
3524 if (idx == field_idx)
3525 {
3526 // Print the member type if requested
3527 // Print the member name and equal sign
3528 name.assign(field->getNameAsString());
3529
3530 // Figure out the type byte size (field_type_info.first) and
3531 // alignment (field_type_info.second) from the AST context.
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003532 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003533 {
3534 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003535 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003536 }
3537
Greg Clayton1811b4f2012-07-31 23:39:10 +00003538 const bool is_bitfield = field->isBitField();
3539
3540 if (bitfield_bit_size_ptr)
3541 {
3542 *bitfield_bit_size_ptr = 0;
3543
3544 if (is_bitfield && ast)
3545 {
3546 Expr *bitfield_bit_size_expr = field->getBitWidth();
3547 llvm::APSInt bitfield_apsint;
3548 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
3549 {
3550 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
3551 }
3552 }
3553 }
3554 if (is_bitfield_ptr)
3555 *is_bitfield_ptr = is_bitfield;
3556
Greg Claytonbf2331c2011-09-09 23:04:00 +00003557 return field->getType().getAsOpaquePtr();
3558 }
3559 }
3560 }
3561 break;
3562
3563 case clang::Type::ObjCObject:
3564 case clang::Type::ObjCInterface:
3565 if (GetCompleteQualType (ast, qual_type))
3566 {
3567 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3568 assert (objc_class_type);
3569 if (objc_class_type)
3570 {
3571 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3572
3573 if (class_interface_decl)
3574 {
3575 if (idx < (class_interface_decl->ivar_size()))
3576 {
3577 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
3578 uint32_t ivar_idx = 0;
3579
3580 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
3581 {
3582 if (ivar_idx == idx)
3583 {
3584 const ObjCIvarDecl* ivar_decl = *ivar_pos;
3585
3586 QualType ivar_qual_type(ivar_decl->getType());
3587
3588 name.assign(ivar_decl->getNameAsString());
3589
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003590 if (bit_offset_ptr)
Greg Claytonbf2331c2011-09-09 23:04:00 +00003591 {
3592 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
Greg Claytonda7bc7d2011-11-13 06:57:31 +00003593 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003594 }
3595
Greg Clayton1811b4f2012-07-31 23:39:10 +00003596 const bool is_bitfield = ivar_pos->isBitField();
3597
3598 if (bitfield_bit_size_ptr)
3599 {
3600 *bitfield_bit_size_ptr = 0;
3601
3602 if (is_bitfield && ast)
3603 {
3604 Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
3605 llvm::APSInt bitfield_apsint;
3606 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
3607 {
3608 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
3609 }
3610 }
3611 }
3612 if (is_bitfield_ptr)
3613 *is_bitfield_ptr = is_bitfield;
3614
Greg Claytonbf2331c2011-09-09 23:04:00 +00003615 return ivar_qual_type.getAsOpaquePtr();
3616 }
3617 }
3618 }
3619 }
3620 }
3621 }
3622 break;
3623
3624
3625 case clang::Type::Typedef:
3626 return ClangASTContext::GetFieldAtIndex (ast,
3627 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3628 idx,
3629 name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003630 bit_offset_ptr,
3631 bitfield_bit_size_ptr,
3632 is_bitfield_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003633
3634 case clang::Type::Elaborated:
3635 return ClangASTContext::GetFieldAtIndex (ast,
3636 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3637 idx,
3638 name,
Greg Clayton1811b4f2012-07-31 23:39:10 +00003639 bit_offset_ptr,
3640 bitfield_bit_size_ptr,
3641 is_bitfield_ptr);
Greg Claytonbf2331c2011-09-09 23:04:00 +00003642
3643 default:
3644 break;
3645 }
3646 return NULL;
3647}
3648
Greg Claytoneaafa732012-10-13 00:20:27 +00003649lldb::BasicType
3650ClangASTContext::GetLLDBBasicTypeEnumeration (clang_type_t clang_type)
3651{
3652 if (clang_type)
3653 {
3654 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3655 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Claytonc4c5e892012-10-15 21:16:43 +00003656 if (type_class == clang::Type::Builtin)
Greg Claytoneaafa732012-10-13 00:20:27 +00003657 {
Greg Claytoneaafa732012-10-13 00:20:27 +00003658 switch (cast<clang::BuiltinType>(qual_type)->getKind())
Greg Claytonc4c5e892012-10-15 21:16:43 +00003659 {
Greg Claytoneaafa732012-10-13 00:20:27 +00003660 case clang::BuiltinType::Void: return eBasicTypeVoid;
3661 case clang::BuiltinType::Bool: return eBasicTypeBool;
3662 case clang::BuiltinType::Char_S: return eBasicTypeSignedChar;
3663 case clang::BuiltinType::Char_U: return eBasicTypeUnsignedChar;
3664 case clang::BuiltinType::Char16: return eBasicTypeChar16;
3665 case clang::BuiltinType::Char32: return eBasicTypeChar32;
3666 case clang::BuiltinType::UChar: return eBasicTypeUnsignedChar;
3667 case clang::BuiltinType::SChar: return eBasicTypeSignedChar;
3668 case clang::BuiltinType::WChar_S: return eBasicTypeSignedWChar;
3669 case clang::BuiltinType::WChar_U: return eBasicTypeUnsignedWChar;
3670 case clang::BuiltinType::Short: return eBasicTypeShort;
3671 case clang::BuiltinType::UShort: return eBasicTypeUnsignedShort;
3672 case clang::BuiltinType::Int: return eBasicTypeInt;
3673 case clang::BuiltinType::UInt: return eBasicTypeUnsignedInt;
3674 case clang::BuiltinType::Long: return eBasicTypeLong;
3675 case clang::BuiltinType::ULong: return eBasicTypeUnsignedLong;
3676 case clang::BuiltinType::LongLong: return eBasicTypeLongLong;
3677 case clang::BuiltinType::ULongLong: return eBasicTypeUnsignedLongLong;
3678 case clang::BuiltinType::Int128: return eBasicTypeInt128;
3679 case clang::BuiltinType::UInt128: return eBasicTypeUnsignedInt128;
3680
3681 case clang::BuiltinType::Half: return eBasicTypeHalf;
3682 case clang::BuiltinType::Float: return eBasicTypeFloat;
3683 case clang::BuiltinType::Double: return eBasicTypeDouble;
3684 case clang::BuiltinType::LongDouble:return eBasicTypeLongDouble;
3685
3686 case clang::BuiltinType::NullPtr: return eBasicTypeNullPtr;
3687 case clang::BuiltinType::ObjCId: return eBasicTypeObjCID;
3688 case clang::BuiltinType::ObjCClass: return eBasicTypeObjCClass;
3689 case clang::BuiltinType::ObjCSel: return eBasicTypeObjCSel;
3690 case clang::BuiltinType::Dependent:
3691 case clang::BuiltinType::Overload:
3692 case clang::BuiltinType::BoundMember:
3693 case clang::BuiltinType::PseudoObject:
3694 case clang::BuiltinType::UnknownAny:
3695 case clang::BuiltinType::BuiltinFn:
3696 case clang::BuiltinType::ARCUnbridgedCast:
3697 return eBasicTypeOther;
Greg Claytonc4c5e892012-10-15 21:16:43 +00003698 }
Greg Claytoneaafa732012-10-13 00:20:27 +00003699 }
3700 }
3701
3702 return eBasicTypeInvalid;
3703}
3704
3705
Greg Claytonbf2331c2011-09-09 23:04:00 +00003706
Greg Clayton54979cd2010-12-15 05:08:08 +00003707// If a pointer to a pointee type (the clang_type arg) says that it has no
3708// children, then we either need to trust it, or override it and return a
3709// different result. For example, an "int *" has one child that is an integer,
3710// but a function pointer doesn't have any children. Likewise if a Record type
3711// claims it has no children, then there really is nothing to show.
3712uint32_t
3713ClangASTContext::GetNumPointeeChildren (clang_type_t clang_type)
3714{
3715 if (clang_type == NULL)
3716 return 0;
3717
3718 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
3719 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3720 switch (type_class)
3721 {
Greg Clayton97a43712011-01-08 22:26:47 +00003722 case clang::Type::Builtin:
3723 switch (cast<clang::BuiltinType>(qual_type)->getKind())
3724 {
Greg Clayton7260f622011-04-18 08:33:37 +00003725 case clang::BuiltinType::UnknownAny:
Greg Clayton97a43712011-01-08 22:26:47 +00003726 case clang::BuiltinType::Void:
3727 case clang::BuiltinType::NullPtr:
3728 return 0;
3729 case clang::BuiltinType::Bool:
3730 case clang::BuiltinType::Char_U:
3731 case clang::BuiltinType::UChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003732 case clang::BuiltinType::WChar_U:
Greg Clayton97a43712011-01-08 22:26:47 +00003733 case clang::BuiltinType::Char16:
3734 case clang::BuiltinType::Char32:
3735 case clang::BuiltinType::UShort:
3736 case clang::BuiltinType::UInt:
3737 case clang::BuiltinType::ULong:
3738 case clang::BuiltinType::ULongLong:
3739 case clang::BuiltinType::UInt128:
3740 case clang::BuiltinType::Char_S:
3741 case clang::BuiltinType::SChar:
Sean Callanan2c777c42011-01-18 23:32:05 +00003742 case clang::BuiltinType::WChar_S:
Greg Clayton97a43712011-01-08 22:26:47 +00003743 case clang::BuiltinType::Short:
3744 case clang::BuiltinType::Int:
3745 case clang::BuiltinType::Long:
3746 case clang::BuiltinType::LongLong:
3747 case clang::BuiltinType::Int128:
3748 case clang::BuiltinType::Float:
3749 case clang::BuiltinType::Double:
3750 case clang::BuiltinType::LongDouble:
3751 case clang::BuiltinType::Dependent:
3752 case clang::BuiltinType::Overload:
Greg Clayton97a43712011-01-08 22:26:47 +00003753 case clang::BuiltinType::ObjCId:
3754 case clang::BuiltinType::ObjCClass:
3755 case clang::BuiltinType::ObjCSel:
Sean Callanand12cf8bb2011-05-15 22:34:38 +00003756 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00003757 case clang::BuiltinType::Half:
3758 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00003759 case clang::BuiltinType::PseudoObject:
Sean Callanan3d654b32012-09-24 22:25:51 +00003760 case clang::BuiltinType::BuiltinFn:
Greg Clayton97a43712011-01-08 22:26:47 +00003761 return 1;
3762 }
3763 break;
3764
Greg Clayton49462ea2011-01-15 02:52:14 +00003765 case clang::Type::Complex: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003766 case clang::Type::Pointer: return 1;
3767 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
3768 case clang::Type::LValueReference: return 1;
3769 case clang::Type::RValueReference: return 1;
3770 case clang::Type::MemberPointer: return 0;
3771 case clang::Type::ConstantArray: return 0;
3772 case clang::Type::IncompleteArray: return 0;
3773 case clang::Type::VariableArray: return 0;
3774 case clang::Type::DependentSizedArray: return 0;
3775 case clang::Type::DependentSizedExtVector: return 0;
3776 case clang::Type::Vector: return 0;
3777 case clang::Type::ExtVector: return 0;
3778 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
3779 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
3780 case clang::Type::UnresolvedUsing: return 0;
3781 case clang::Type::Paren: return 0;
3782 case clang::Type::Typedef: return ClangASTContext::GetNumPointeeChildren (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00003783 case clang::Type::Elaborated: return ClangASTContext::GetNumPointeeChildren (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton54979cd2010-12-15 05:08:08 +00003784 case clang::Type::TypeOfExpr: return 0;
3785 case clang::Type::TypeOf: return 0;
3786 case clang::Type::Decltype: return 0;
3787 case clang::Type::Record: return 0;
3788 case clang::Type::Enum: return 1;
Greg Clayton54979cd2010-12-15 05:08:08 +00003789 case clang::Type::TemplateTypeParm: return 1;
3790 case clang::Type::SubstTemplateTypeParm: return 1;
3791 case clang::Type::TemplateSpecialization: return 1;
3792 case clang::Type::InjectedClassName: return 0;
3793 case clang::Type::DependentName: return 1;
3794 case clang::Type::DependentTemplateSpecialization: return 1;
3795 case clang::Type::ObjCObject: return 0;
3796 case clang::Type::ObjCInterface: return 0;
3797 case clang::Type::ObjCObjectPointer: return 1;
3798 default:
3799 break;
3800 }
3801 return 0;
3802}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003803
Greg Clayton1be10fc2010-09-29 01:12:09 +00003804clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003805ClangASTContext::GetChildClangTypeAtIndex
3806(
Jim Inghamd555bac2011-06-24 22:03:24 +00003807 ExecutionContext *exe_ctx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003808 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003809 clang_type_t parent_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003810 uint32_t idx,
3811 bool transparent_pointers,
3812 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003813 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003814 std::string& child_name,
3815 uint32_t &child_byte_size,
3816 int32_t &child_byte_offset,
3817 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003818 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003819 bool &child_is_base_class,
3820 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003821)
3822{
3823 if (parent_clang_type)
3824
Jim Inghamd555bac2011-06-24 22:03:24 +00003825 return GetChildClangTypeAtIndex (exe_ctx,
3826 getASTContext(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003827 parent_name,
3828 parent_clang_type,
3829 idx,
3830 transparent_pointers,
3831 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003832 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003833 child_name,
3834 child_byte_size,
3835 child_byte_offset,
3836 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003837 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003838 child_is_base_class,
3839 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003840 return NULL;
3841}
3842
Greg Clayton1be10fc2010-09-29 01:12:09 +00003843clang_type_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003844ClangASTContext::GetChildClangTypeAtIndex
3845(
Jim Inghamd555bac2011-06-24 22:03:24 +00003846 ExecutionContext *exe_ctx,
Greg Clayton6beaaa62011-01-17 03:46:26 +00003847 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003848 const char *parent_name,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003849 clang_type_t parent_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003850 uint32_t idx,
3851 bool transparent_pointers,
3852 bool omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003853 bool ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003854 std::string& child_name,
3855 uint32_t &child_byte_size,
3856 int32_t &child_byte_offset,
3857 uint32_t &child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003858 uint32_t &child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003859 bool &child_is_base_class,
3860 bool &child_is_deref_of_parent
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003861)
3862{
3863 if (parent_clang_type == NULL)
3864 return NULL;
3865
Greg Clayton6beaaa62011-01-17 03:46:26 +00003866 if (idx < ClangASTContext::GetNumChildren (ast, parent_clang_type, omit_empty_base_classes))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003867 {
3868 uint32_t bit_offset;
3869 child_bitfield_bit_size = 0;
3870 child_bitfield_bit_offset = 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003871 child_is_base_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003872 QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00003873 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
3874 switch (parent_type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003875 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003876 case clang::Type::Builtin:
3877 switch (cast<clang::BuiltinType>(parent_qual_type)->getKind())
3878 {
3879 case clang::BuiltinType::ObjCId:
3880 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00003881 child_name = "isa";
Greg Clayton6beaaa62011-01-17 03:46:26 +00003882 child_byte_size = ast->getTypeSize(ast->ObjCBuiltinClassTy) / CHAR_BIT;
3883 return ast->ObjCBuiltinClassTy.getAsOpaquePtr();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003884
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003885 default:
3886 break;
3887 }
3888 break;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003889
Greg Claytone1a916a2010-07-21 22:12:05 +00003890 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00003891 if (GetCompleteQualType (ast, parent_qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003892 {
3893 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr());
3894 const RecordDecl *record_decl = record_type->getDecl();
3895 assert(record_decl);
Greg Clayton6beaaa62011-01-17 03:46:26 +00003896 const ASTRecordLayout &record_layout = ast->getASTRecordLayout(record_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003897 uint32_t child_idx = 0;
3898
3899 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3900 if (cxx_record_decl)
3901 {
3902 // We might have base classes to print out first
3903 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3904 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3905 base_class != base_class_end;
3906 ++base_class)
3907 {
3908 const CXXRecordDecl *base_class_decl = NULL;
3909
3910 // Skip empty base classes
3911 if (omit_empty_base_classes)
3912 {
3913 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3914 if (RecordHasFields(base_class_decl) == false)
3915 continue;
3916 }
3917
3918 if (idx == child_idx)
3919 {
3920 if (base_class_decl == NULL)
3921 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3922
3923
3924 if (base_class->isVirtual())
Greg Clayton6ed95942011-01-22 07:12:45 +00003925 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003926 else
Greg Clayton6ed95942011-01-22 07:12:45 +00003927 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003928
3929 // Base classes should be a multiple of 8 bits in size
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003930 child_byte_offset = bit_offset/8;
Greg Claytone3055942011-06-30 02:28:26 +00003931
Greg Clayton84db9102012-03-26 23:03:23 +00003932 child_name = ClangASTType::GetTypeNameForQualType(ast, base_class->getType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003933
Greg Clayton6beaaa62011-01-17 03:46:26 +00003934 uint64_t clang_type_info_bit_size = ast->getTypeSize(base_class->getType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003935
Jim Inghamf46b3382011-04-15 23:42:06 +00003936 // Base classes bit sizes should be a multiple of 8 bits in size
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003937 assert (clang_type_info_bit_size % 8 == 0);
3938 child_byte_size = clang_type_info_bit_size / 8;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003939 child_is_base_class = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003940 return base_class->getType().getAsOpaquePtr();
3941 }
3942 // We don't increment the child index in the for loop since we might
3943 // be skipping empty base classes
3944 ++child_idx;
3945 }
3946 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003947 // Make sure index is in range...
3948 uint32_t field_idx = 0;
3949 RecordDecl::field_iterator field, field_end;
3950 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
3951 {
3952 if (idx == child_idx)
3953 {
3954 // Print the member type if requested
3955 // Print the member name and equal sign
3956 child_name.assign(field->getNameAsString().c_str());
3957
3958 // Figure out the type byte size (field_type_info.first) and
3959 // alignment (field_type_info.second) from the AST context.
Greg Clayton6beaaa62011-01-17 03:46:26 +00003960 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(field->getType());
Greg Claytonc982c762010-07-09 20:39:50 +00003961 assert(field_idx < record_layout.getFieldCount());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003962
3963 child_byte_size = field_type_info.first / 8;
3964
3965 // Figure out the field offset within the current struct/union/class type
3966 bit_offset = record_layout.getFieldOffset (field_idx);
3967 child_byte_offset = bit_offset / 8;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003968 if (ClangASTContext::FieldIsBitfield (ast, *field, child_bitfield_bit_size))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003969 child_bitfield_bit_offset = bit_offset % 8;
3970
3971 return field->getType().getAsOpaquePtr();
3972 }
3973 }
3974 }
3975 break;
3976
Greg Clayton9e409562010-07-28 02:04:09 +00003977 case clang::Type::ObjCObject:
3978 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00003979 if (GetCompleteQualType (ast, parent_qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00003980 {
Sean Callanan78e37602011-01-27 04:42:51 +00003981 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00003982 assert (objc_class_type);
3983 if (objc_class_type)
3984 {
3985 uint32_t child_idx = 0;
3986 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3987
3988 if (class_interface_decl)
3989 {
3990
Greg Clayton6beaaa62011-01-17 03:46:26 +00003991 const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
Greg Clayton9e409562010-07-28 02:04:09 +00003992 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3993 if (superclass_interface_decl)
3994 {
3995 if (omit_empty_base_classes)
3996 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00003997 if (ClangASTContext::GetNumChildren(ast, ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0)
Greg Clayton9e409562010-07-28 02:04:09 +00003998 {
3999 if (idx == 0)
4000 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004001 QualType ivar_qual_type(ast->getObjCInterfaceType(superclass_interface_decl));
Greg Clayton9e409562010-07-28 02:04:09 +00004002
4003
4004 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
4005
Greg Clayton6beaaa62011-01-17 03:46:26 +00004006 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004007
4008 child_byte_size = ivar_type_info.first / 8;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004009 child_byte_offset = 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00004010 child_is_base_class = true;
Greg Clayton9e409562010-07-28 02:04:09 +00004011
4012 return ivar_qual_type.getAsOpaquePtr();
4013 }
4014
4015 ++child_idx;
4016 }
4017 }
4018 else
4019 ++child_idx;
4020 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004021
4022 const uint32_t superclass_idx = child_idx;
Greg Clayton9e409562010-07-28 02:04:09 +00004023
4024 if (idx < (child_idx + class_interface_decl->ivar_size()))
4025 {
4026 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4027
4028 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
4029 {
4030 if (child_idx == idx)
4031 {
Jim Inghamf80bc3f2011-12-08 02:53:10 +00004032 ObjCIvarDecl* ivar_decl = *ivar_pos;
Greg Clayton9e409562010-07-28 02:04:09 +00004033
4034 QualType ivar_qual_type(ivar_decl->getType());
4035
4036 child_name.assign(ivar_decl->getNameAsString().c_str());
4037
Greg Clayton6beaaa62011-01-17 03:46:26 +00004038 std::pair<uint64_t, unsigned> ivar_type_info = ast->getTypeInfo(ivar_qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004039
4040 child_byte_size = ivar_type_info.first / 8;
4041
4042 // Figure out the field offset within the current struct/union/class type
Jim Inghamd555bac2011-06-24 22:03:24 +00004043 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
4044 // that doesn't account for the space taken up by unbacked properties, or from
4045 // the changing size of base classes that are newer than this class.
4046 // So if we have a process around that we can ask about this object, do so.
4047 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
Greg Claytonc14ee322011-09-22 04:58:26 +00004048 Process *process = NULL;
4049 if (exe_ctx)
4050 process = exe_ctx->GetProcessPtr();
4051 if (process)
Jim Inghamd555bac2011-06-24 22:03:24 +00004052 {
Greg Claytonc14ee322011-09-22 04:58:26 +00004053 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
Jim Inghamd555bac2011-06-24 22:03:24 +00004054 if (objc_runtime != NULL)
4055 {
Enrico Granata6f3533f2011-07-29 19:53:35 +00004056 ClangASTType parent_ast_type (ast, parent_qual_type.getAsOpaquePtr());
Jim Inghamd555bac2011-06-24 22:03:24 +00004057 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
4058 }
4059 }
4060
Jim Inghamf80bc3f2011-12-08 02:53:10 +00004061 // Setting this to UINT32_MAX to make sure we don't compute it twice...
4062 bit_offset = UINT32_MAX;
4063
Jim Inghamd555bac2011-06-24 22:03:24 +00004064 if (child_byte_offset == LLDB_INVALID_IVAR_OFFSET)
4065 {
4066 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
4067 child_byte_offset = bit_offset / 8;
4068 }
Jim Inghamf80bc3f2011-12-08 02:53:10 +00004069
4070 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
4071 // of a bitfield within its containing object. So regardless of where we get the byte
4072 // offset from, we still need to get the bit offset for bitfields from the layout.
4073
4074 if (ClangASTContext::FieldIsBitfield (ast, ivar_decl, child_bitfield_bit_size))
4075 {
4076 if (bit_offset == UINT32_MAX)
4077 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
4078
4079 child_bitfield_bit_offset = bit_offset % 8;
4080 }
Greg Clayton9e409562010-07-28 02:04:09 +00004081 return ivar_qual_type.getAsOpaquePtr();
4082 }
4083 ++child_idx;
4084 }
4085 }
4086 }
4087 }
4088 }
4089 break;
4090
4091 case clang::Type::ObjCObjectPointer:
4092 {
Sean Callanan78e37602011-01-27 04:42:51 +00004093 const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004094 QualType pointee_type = pointer_type->getPointeeType();
4095
4096 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4097 {
Greg Claytone221f822011-01-21 01:59:00 +00004098 child_is_deref_of_parent = false;
4099 bool tmp_child_is_deref_of_parent = false;
Jim Inghamd555bac2011-06-24 22:03:24 +00004100 return GetChildClangTypeAtIndex (exe_ctx,
4101 ast,
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004102 parent_name,
4103 pointer_type->getPointeeType().getAsOpaquePtr(),
4104 idx,
4105 transparent_pointers,
4106 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004107 ignore_array_bounds,
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004108 child_name,
4109 child_byte_size,
4110 child_byte_offset,
4111 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00004112 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004113 child_is_base_class,
4114 tmp_child_is_deref_of_parent);
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004115 }
4116 else
4117 {
Greg Claytone221f822011-01-21 01:59:00 +00004118 child_is_deref_of_parent = true;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004119 if (parent_name)
4120 {
4121 child_name.assign(1, '*');
4122 child_name += parent_name;
4123 }
4124
4125 // We have a pointer to an simple type
Sean Callanan5b26f272012-02-04 08:49:35 +00004126 if (idx == 0 && GetCompleteQualType(ast, pointee_type))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004127 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004128 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004129 assert(clang_type_info.first % 8 == 0);
4130 child_byte_size = clang_type_info.first / 8;
4131 child_byte_offset = 0;
4132 return pointee_type.getAsOpaquePtr();
4133 }
4134 }
Greg Clayton9e409562010-07-28 02:04:09 +00004135 }
4136 break;
4137
Greg Claytone1a916a2010-07-21 22:12:05 +00004138 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004139 {
4140 const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4141 const uint64_t element_count = array->getSize().getLimitedValue();
4142
Greg Claytondaf515f2011-07-09 20:12:33 +00004143 if (ignore_array_bounds || idx < element_count)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004144 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004145 if (GetCompleteQualType (ast, array->getElementType()))
4146 {
4147 std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004148
Greg Clayton6beaaa62011-01-17 03:46:26 +00004149 char element_name[64];
4150 ::snprintf (element_name, sizeof (element_name), "[%u]", idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004151
Greg Clayton6beaaa62011-01-17 03:46:26 +00004152 child_name.assign(element_name);
4153 assert(field_type_info.first % 8 == 0);
4154 child_byte_size = field_type_info.first / 8;
Greg Claytondaf515f2011-07-09 20:12:33 +00004155 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
Greg Clayton6beaaa62011-01-17 03:46:26 +00004156 return array->getElementType().getAsOpaquePtr();
4157 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004158 }
4159 }
4160 break;
4161
Greg Claytone1a916a2010-07-21 22:12:05 +00004162 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004163 {
Sean Callanan78e37602011-01-27 04:42:51 +00004164 const PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004165 QualType pointee_type = pointer_type->getPointeeType();
Greg Clayton97a43712011-01-08 22:26:47 +00004166
4167 // Don't dereference "void *" pointers
4168 if (pointee_type->isVoidType())
4169 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004170
4171 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4172 {
Greg Claytone221f822011-01-21 01:59:00 +00004173 child_is_deref_of_parent = false;
4174 bool tmp_child_is_deref_of_parent = false;
Jim Inghamd555bac2011-06-24 22:03:24 +00004175 return GetChildClangTypeAtIndex (exe_ctx,
4176 ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004177 parent_name,
4178 pointer_type->getPointeeType().getAsOpaquePtr(),
4179 idx,
4180 transparent_pointers,
4181 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004182 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004183 child_name,
4184 child_byte_size,
4185 child_byte_offset,
4186 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00004187 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004188 child_is_base_class,
4189 tmp_child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004190 }
4191 else
4192 {
Greg Claytone221f822011-01-21 01:59:00 +00004193 child_is_deref_of_parent = true;
4194
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004195 if (parent_name)
4196 {
4197 child_name.assign(1, '*');
4198 child_name += parent_name;
4199 }
4200
4201 // We have a pointer to an simple type
4202 if (idx == 0)
4203 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004204 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004205 assert(clang_type_info.first % 8 == 0);
4206 child_byte_size = clang_type_info.first / 8;
4207 child_byte_offset = 0;
4208 return pointee_type.getAsOpaquePtr();
4209 }
4210 }
4211 }
4212 break;
4213
Greg Clayton73b472d2010-10-27 03:32:59 +00004214 case clang::Type::LValueReference:
4215 case clang::Type::RValueReference:
4216 {
Sean Callanan78e37602011-01-27 04:42:51 +00004217 const ReferenceType *reference_type = cast<ReferenceType>(parent_qual_type.getTypePtr());
Greg Clayton73b472d2010-10-27 03:32:59 +00004218 QualType pointee_type(reference_type->getPointeeType());
4219 clang_type_t pointee_clang_type = pointee_type.getAsOpaquePtr();
4220 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_clang_type))
4221 {
Greg Claytone221f822011-01-21 01:59:00 +00004222 child_is_deref_of_parent = false;
4223 bool tmp_child_is_deref_of_parent = false;
Jim Inghamd555bac2011-06-24 22:03:24 +00004224 return GetChildClangTypeAtIndex (exe_ctx,
4225 ast,
Greg Clayton73b472d2010-10-27 03:32:59 +00004226 parent_name,
4227 pointee_clang_type,
4228 idx,
4229 transparent_pointers,
4230 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004231 ignore_array_bounds,
Greg Clayton73b472d2010-10-27 03:32:59 +00004232 child_name,
4233 child_byte_size,
4234 child_byte_offset,
4235 child_bitfield_bit_size,
4236 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004237 child_is_base_class,
4238 tmp_child_is_deref_of_parent);
Greg Clayton73b472d2010-10-27 03:32:59 +00004239 }
4240 else
4241 {
4242 if (parent_name)
4243 {
4244 child_name.assign(1, '&');
4245 child_name += parent_name;
4246 }
4247
4248 // We have a pointer to an simple type
4249 if (idx == 0)
4250 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004251 std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Greg Clayton73b472d2010-10-27 03:32:59 +00004252 assert(clang_type_info.first % 8 == 0);
4253 child_byte_size = clang_type_info.first / 8;
4254 child_byte_offset = 0;
4255 return pointee_type.getAsOpaquePtr();
4256 }
4257 }
4258 }
4259 break;
4260
Greg Claytone1a916a2010-07-21 22:12:05 +00004261 case clang::Type::Typedef:
Jim Inghamd555bac2011-06-24 22:03:24 +00004262 return GetChildClangTypeAtIndex (exe_ctx,
4263 ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004264 parent_name,
Sean Callanan48114472010-12-13 01:26:27 +00004265 cast<TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004266 idx,
4267 transparent_pointers,
4268 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00004269 ignore_array_bounds,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004270 child_name,
4271 child_byte_size,
4272 child_byte_offset,
4273 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00004274 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00004275 child_is_base_class,
4276 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004277 break;
Sean Callanan912855f2011-08-11 23:56:13 +00004278
4279 case clang::Type::Elaborated:
4280 return GetChildClangTypeAtIndex (exe_ctx,
4281 ast,
4282 parent_name,
4283 cast<ElaboratedType>(parent_qual_type)->getNamedType().getAsOpaquePtr(),
4284 idx,
4285 transparent_pointers,
4286 omit_empty_base_classes,
4287 ignore_array_bounds,
4288 child_name,
4289 child_byte_size,
4290 child_byte_offset,
4291 child_bitfield_bit_size,
4292 child_bitfield_bit_offset,
4293 child_is_base_class,
4294 child_is_deref_of_parent);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004295
4296 default:
4297 break;
4298 }
4299 }
Greg Clayton19503a22010-07-23 15:37:46 +00004300 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004301}
4302
4303static inline bool
4304BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
4305{
Greg Clayton6beaaa62011-01-17 03:46:26 +00004306 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004307}
4308
4309static uint32_t
4310GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
4311{
4312 uint32_t num_bases = 0;
4313 if (cxx_record_decl)
4314 {
4315 if (omit_empty_base_classes)
4316 {
4317 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4318 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4319 base_class != base_class_end;
4320 ++base_class)
4321 {
4322 // Skip empty base classes
4323 if (omit_empty_base_classes)
4324 {
4325 if (BaseSpecifierIsEmpty (base_class))
4326 continue;
4327 }
4328 ++num_bases;
4329 }
4330 }
4331 else
4332 num_bases = cxx_record_decl->getNumBases();
4333 }
4334 return num_bases;
4335}
4336
4337
4338static uint32_t
4339GetIndexForRecordBase
4340(
4341 const RecordDecl *record_decl,
4342 const CXXBaseSpecifier *base_spec,
4343 bool omit_empty_base_classes
4344)
4345{
4346 uint32_t child_idx = 0;
4347
4348 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4349
4350// const char *super_name = record_decl->getNameAsCString();
4351// const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString();
4352// printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
4353//
4354 if (cxx_record_decl)
4355 {
4356 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4357 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4358 base_class != base_class_end;
4359 ++base_class)
4360 {
4361 if (omit_empty_base_classes)
4362 {
4363 if (BaseSpecifierIsEmpty (base_class))
4364 continue;
4365 }
4366
4367// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
4368// child_idx,
4369// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4370//
4371//
4372 if (base_class == base_spec)
4373 return child_idx;
4374 ++child_idx;
4375 }
4376 }
4377
4378 return UINT32_MAX;
4379}
4380
4381
4382static uint32_t
4383GetIndexForRecordChild
4384(
4385 const RecordDecl *record_decl,
4386 NamedDecl *canonical_decl,
4387 bool omit_empty_base_classes
4388)
4389{
4390 uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes);
4391
4392// const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4393//
4394//// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString());
4395// if (cxx_record_decl)
4396// {
4397// CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4398// for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4399// base_class != base_class_end;
4400// ++base_class)
4401// {
4402// if (omit_empty_base_classes)
4403// {
4404// if (BaseSpecifierIsEmpty (base_class))
4405// continue;
4406// }
4407//
4408//// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
4409//// record_decl->getNameAsCString(),
4410//// canonical_decl->getNameAsCString(),
4411//// child_idx,
4412//// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
4413//
4414//
4415// CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4416// if (curr_base_class_decl == canonical_decl)
4417// {
4418// return child_idx;
4419// }
4420// ++child_idx;
4421// }
4422// }
4423//
4424// const uint32_t num_bases = child_idx;
4425 RecordDecl::field_iterator field, field_end;
4426 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4427 field != field_end;
4428 ++field, ++child_idx)
4429 {
4430// printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n",
4431// record_decl->getNameAsCString(),
4432// canonical_decl->getNameAsCString(),
4433// child_idx - num_bases,
4434// field->getNameAsCString());
4435
4436 if (field->getCanonicalDecl() == canonical_decl)
4437 return child_idx;
4438 }
4439
4440 return UINT32_MAX;
4441}
4442
4443// Look for a child member (doesn't include base classes, but it does include
4444// their members) in the type hierarchy. Returns an index path into "clang_type"
4445// on how to reach the appropriate member.
4446//
4447// class A
4448// {
4449// public:
4450// int m_a;
4451// int m_b;
4452// };
4453//
4454// class B
4455// {
4456// };
4457//
4458// class C :
4459// public B,
4460// public A
4461// {
4462// };
4463//
4464// If we have a clang type that describes "class C", and we wanted to looked
4465// "m_b" in it:
4466//
4467// With omit_empty_base_classes == false we would get an integer array back with:
4468// { 1, 1 }
4469// The first index 1 is the child index for "class A" within class C
4470// The second index 1 is the child index for "m_b" within class A
4471//
4472// With omit_empty_base_classes == true we would get an integer array back with:
4473// { 0, 1 }
4474// 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)
4475// The second index 1 is the child index for "m_b" within class A
4476
4477size_t
4478ClangASTContext::GetIndexOfChildMemberWithName
4479(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004480 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004481 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004482 const char *name,
4483 bool omit_empty_base_classes,
4484 std::vector<uint32_t>& child_indexes
4485)
4486{
4487 if (clang_type && name && name[0])
4488 {
4489 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00004490 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4491 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004492 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004493 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004494 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004495 {
4496 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4497 const RecordDecl *record_decl = record_type->getDecl();
4498
4499 assert(record_decl);
4500 uint32_t child_idx = 0;
4501
4502 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4503
4504 // Try and find a field that matches NAME
4505 RecordDecl::field_iterator field, field_end;
4506 StringRef name_sref(name);
4507 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4508 field != field_end;
4509 ++field, ++child_idx)
4510 {
4511 if (field->getName().equals (name_sref))
4512 {
4513 // We have to add on the number of base classes to this index!
4514 child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
4515 return child_indexes.size();
4516 }
4517 }
4518
4519 if (cxx_record_decl)
4520 {
4521 const RecordDecl *parent_record_decl = cxx_record_decl;
4522
4523 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
4524
4525 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
4526 // Didn't find things easily, lets let clang do its thang...
Sean Callanancc427fa2011-07-30 02:42:06 +00004527 IdentifierInfo & ident_ref = ast->Idents.get(name_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004528 DeclarationName decl_name(&ident_ref);
4529
4530 CXXBasePaths paths;
4531 if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember,
4532 decl_name.getAsOpaquePtr(),
4533 paths))
4534 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004535 CXXBasePaths::const_paths_iterator path, path_end = paths.end();
4536 for (path = paths.begin(); path != path_end; ++path)
4537 {
4538 const size_t num_path_elements = path->size();
4539 for (size_t e=0; e<num_path_elements; ++e)
4540 {
4541 CXXBasePathElement elem = (*path)[e];
4542
4543 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
4544 if (child_idx == UINT32_MAX)
4545 {
4546 child_indexes.clear();
4547 return 0;
4548 }
4549 else
4550 {
4551 child_indexes.push_back (child_idx);
4552 parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl());
4553 }
4554 }
4555 DeclContext::lookup_iterator named_decl_pos;
4556 for (named_decl_pos = path->Decls.first;
4557 named_decl_pos != path->Decls.second && parent_record_decl;
4558 ++named_decl_pos)
4559 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004560 child_idx = GetIndexForRecordChild (parent_record_decl, *named_decl_pos, omit_empty_base_classes);
4561 if (child_idx == UINT32_MAX)
4562 {
4563 child_indexes.clear();
4564 return 0;
4565 }
4566 else
4567 {
4568 child_indexes.push_back (child_idx);
4569 }
4570 }
4571 }
4572 return child_indexes.size();
4573 }
4574 }
4575
4576 }
4577 break;
4578
Greg Clayton9e409562010-07-28 02:04:09 +00004579 case clang::Type::ObjCObject:
4580 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004581 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004582 {
4583 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004584 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004585 assert (objc_class_type);
4586 if (objc_class_type)
4587 {
4588 uint32_t child_idx = 0;
4589 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4590
4591 if (class_interface_decl)
4592 {
4593 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4594 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4595
Greg Clayton6ba78152010-09-18 02:11:07 +00004596 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00004597 {
4598 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4599
4600 if (ivar_decl->getName().equals (name_sref))
4601 {
4602 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4603 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4604 ++child_idx;
4605
4606 child_indexes.push_back (child_idx);
4607 return child_indexes.size();
4608 }
4609 }
4610
4611 if (superclass_interface_decl)
4612 {
4613 // The super class index is always zero for ObjC classes,
4614 // so we push it onto the child indexes in case we find
4615 // an ivar in our superclass...
4616 child_indexes.push_back (0);
4617
Greg Clayton6beaaa62011-01-17 03:46:26 +00004618 if (GetIndexOfChildMemberWithName (ast,
4619 ast->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(),
Greg Clayton9e409562010-07-28 02:04:09 +00004620 name,
4621 omit_empty_base_classes,
4622 child_indexes))
4623 {
4624 // We did find an ivar in a superclass so just
4625 // return the results!
4626 return child_indexes.size();
4627 }
4628
4629 // We didn't find an ivar matching "name" in our
4630 // superclass, pop the superclass zero index that
4631 // we pushed on above.
4632 child_indexes.pop_back();
4633 }
4634 }
4635 }
4636 }
4637 break;
4638
4639 case clang::Type::ObjCObjectPointer:
4640 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004641 return GetIndexOfChildMemberWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004642 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4643 name,
4644 omit_empty_base_classes,
4645 child_indexes);
4646 }
4647 break;
4648
4649
Greg Claytone1a916a2010-07-21 22:12:05 +00004650 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004651 {
4652// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4653// const uint64_t element_count = array->getSize().getLimitedValue();
4654//
4655// if (idx < element_count)
4656// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004657// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004658//
4659// char element_name[32];
4660// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4661//
4662// child_name.assign(element_name);
4663// assert(field_type_info.first % 8 == 0);
4664// child_byte_size = field_type_info.first / 8;
4665// child_byte_offset = idx * child_byte_size;
4666// return array->getElementType().getAsOpaquePtr();
4667// }
4668 }
4669 break;
4670
Greg Claytone1a916a2010-07-21 22:12:05 +00004671// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004672// {
4673// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4674// QualType pointee_type = mem_ptr_type->getPointeeType();
4675//
4676// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4677// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004678// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004679// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4680// name);
4681// }
4682// }
4683// break;
4684//
Greg Claytone1a916a2010-07-21 22:12:05 +00004685 case clang::Type::LValueReference:
4686 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004687 {
Sean Callanan78e37602011-01-27 04:42:51 +00004688 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004689 QualType pointee_type = reference_type->getPointeeType();
4690
4691 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4692 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004693 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004694 reference_type->getPointeeType().getAsOpaquePtr(),
4695 name,
4696 omit_empty_base_classes,
4697 child_indexes);
4698 }
4699 }
4700 break;
4701
Greg Claytone1a916a2010-07-21 22:12:05 +00004702 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004703 {
Sean Callanan78e37602011-01-27 04:42:51 +00004704 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004705 QualType pointee_type = pointer_type->getPointeeType();
4706
4707 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4708 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004709 return GetIndexOfChildMemberWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004710 pointer_type->getPointeeType().getAsOpaquePtr(),
4711 name,
4712 omit_empty_base_classes,
4713 child_indexes);
4714 }
4715 else
4716 {
4717// if (parent_name)
4718// {
4719// child_name.assign(1, '*');
4720// child_name += parent_name;
4721// }
4722//
4723// // We have a pointer to an simple type
4724// if (idx == 0)
4725// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004726// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004727// assert(clang_type_info.first % 8 == 0);
4728// child_byte_size = clang_type_info.first / 8;
4729// child_byte_offset = 0;
4730// return pointee_type.getAsOpaquePtr();
4731// }
4732 }
4733 }
4734 break;
4735
Greg Claytone1a916a2010-07-21 22:12:05 +00004736 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00004737 return GetIndexOfChildMemberWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00004738 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004739 name,
4740 omit_empty_base_classes,
4741 child_indexes);
4742
4743 default:
4744 break;
4745 }
4746 }
4747 return 0;
4748}
4749
4750
4751// Get the index of the child of "clang_type" whose name matches. This function
4752// doesn't descend into the children, but only looks one level deep and name
4753// matches can include base class names.
4754
4755uint32_t
4756ClangASTContext::GetIndexOfChildWithName
4757(
Greg Clayton6beaaa62011-01-17 03:46:26 +00004758 ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00004759 clang_type_t clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004760 const char *name,
4761 bool omit_empty_base_classes
4762)
4763{
4764 if (clang_type && name && name[0])
4765 {
4766 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00004767
Greg Clayton737b9322010-09-13 03:32:57 +00004768 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Clayton9e409562010-07-28 02:04:09 +00004769
Greg Clayton737b9322010-09-13 03:32:57 +00004770 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004771 {
Greg Claytone1a916a2010-07-21 22:12:05 +00004772 case clang::Type::Record:
Greg Claytonc432c192011-01-20 04:18:48 +00004773 if (GetCompleteQualType (ast, qual_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004774 {
4775 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
4776 const RecordDecl *record_decl = record_type->getDecl();
4777
4778 assert(record_decl);
4779 uint32_t child_idx = 0;
4780
4781 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
4782
4783 if (cxx_record_decl)
4784 {
4785 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4786 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4787 base_class != base_class_end;
4788 ++base_class)
4789 {
4790 // Skip empty base classes
4791 CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
4792 if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false)
4793 continue;
4794
Greg Clayton84db9102012-03-26 23:03:23 +00004795 std::string base_class_type_name (ClangASTType::GetTypeNameForQualType(ast, base_class->getType()));
Greg Claytone3055942011-06-30 02:28:26 +00004796 if (base_class_type_name.compare (name) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004797 return child_idx;
4798 ++child_idx;
4799 }
4800 }
4801
4802 // Try and find a field that matches NAME
4803 RecordDecl::field_iterator field, field_end;
4804 StringRef name_sref(name);
4805 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
4806 field != field_end;
4807 ++field, ++child_idx)
4808 {
4809 if (field->getName().equals (name_sref))
4810 return child_idx;
4811 }
4812
4813 }
4814 break;
4815
Greg Clayton9e409562010-07-28 02:04:09 +00004816 case clang::Type::ObjCObject:
4817 case clang::Type::ObjCInterface:
Greg Claytonc432c192011-01-20 04:18:48 +00004818 if (GetCompleteQualType (ast, qual_type))
Greg Clayton9e409562010-07-28 02:04:09 +00004819 {
4820 StringRef name_sref(name);
Sean Callanan78e37602011-01-27 04:42:51 +00004821 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00004822 assert (objc_class_type);
4823 if (objc_class_type)
4824 {
4825 uint32_t child_idx = 0;
4826 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4827
4828 if (class_interface_decl)
4829 {
4830 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4831 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4832
Jim Ingham2f355a72012-10-04 22:22:16 +00004833 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00004834 {
4835 const ObjCIvarDecl* ivar_decl = *ivar_pos;
4836
4837 if (ivar_decl->getName().equals (name_sref))
4838 {
4839 if ((!omit_empty_base_classes && superclass_interface_decl) ||
4840 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
4841 ++child_idx;
4842
4843 return child_idx;
4844 }
4845 }
4846
4847 if (superclass_interface_decl)
4848 {
4849 if (superclass_interface_decl->getName().equals (name_sref))
4850 return 0;
4851 }
4852 }
4853 }
4854 }
4855 break;
4856
4857 case clang::Type::ObjCObjectPointer:
4858 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004859 return GetIndexOfChildWithName (ast,
Greg Clayton9e409562010-07-28 02:04:09 +00004860 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
4861 name,
4862 omit_empty_base_classes);
4863 }
4864 break;
4865
Greg Claytone1a916a2010-07-21 22:12:05 +00004866 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004867 {
4868// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
4869// const uint64_t element_count = array->getSize().getLimitedValue();
4870//
4871// if (idx < element_count)
4872// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004873// std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004874//
4875// char element_name[32];
4876// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
4877//
4878// child_name.assign(element_name);
4879// assert(field_type_info.first % 8 == 0);
4880// child_byte_size = field_type_info.first / 8;
4881// child_byte_offset = idx * child_byte_size;
4882// return array->getElementType().getAsOpaquePtr();
4883// }
4884 }
4885 break;
4886
Greg Claytone1a916a2010-07-21 22:12:05 +00004887// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004888// {
4889// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
4890// QualType pointee_type = mem_ptr_type->getPointeeType();
4891//
4892// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4893// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004894// return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004895// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
4896// name);
4897// }
4898// }
4899// break;
4900//
Greg Claytone1a916a2010-07-21 22:12:05 +00004901 case clang::Type::LValueReference:
4902 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004903 {
Sean Callanan78e37602011-01-27 04:42:51 +00004904 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004905 QualType pointee_type = reference_type->getPointeeType();
4906
4907 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4908 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004909 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004910 reference_type->getPointeeType().getAsOpaquePtr(),
4911 name,
4912 omit_empty_base_classes);
4913 }
4914 }
4915 break;
4916
Greg Claytone1a916a2010-07-21 22:12:05 +00004917 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004918 {
Sean Callanan78e37602011-01-27 04:42:51 +00004919 const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004920 QualType pointee_type = pointer_type->getPointeeType();
4921
4922 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
4923 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004924 return GetIndexOfChildWithName (ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004925 pointer_type->getPointeeType().getAsOpaquePtr(),
4926 name,
4927 omit_empty_base_classes);
4928 }
4929 else
4930 {
4931// if (parent_name)
4932// {
4933// child_name.assign(1, '*');
4934// child_name += parent_name;
4935// }
4936//
4937// // We have a pointer to an simple type
4938// if (idx == 0)
4939// {
Greg Clayton6beaaa62011-01-17 03:46:26 +00004940// std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004941// assert(clang_type_info.first % 8 == 0);
4942// child_byte_size = clang_type_info.first / 8;
4943// child_byte_offset = 0;
4944// return pointee_type.getAsOpaquePtr();
4945// }
4946 }
4947 }
4948 break;
4949
Greg Claytone1a916a2010-07-21 22:12:05 +00004950 case clang::Type::Typedef:
Greg Clayton6beaaa62011-01-17 03:46:26 +00004951 return GetIndexOfChildWithName (ast,
Sean Callanan48114472010-12-13 01:26:27 +00004952 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004953 name,
4954 omit_empty_base_classes);
4955
4956 default:
4957 break;
4958 }
4959 }
4960 return UINT32_MAX;
4961}
4962
4963#pragma mark TagType
4964
4965bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00004966ClangASTContext::SetTagTypeKind (clang_type_t tag_clang_type, int kind)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004967{
4968 if (tag_clang_type)
4969 {
4970 QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00004971 const clang::Type *clang_type = tag_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004972 if (clang_type)
4973 {
Sean Callanan78e37602011-01-27 04:42:51 +00004974 const TagType *tag_type = dyn_cast<TagType>(clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004975 if (tag_type)
4976 {
4977 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl());
4978 if (tag_decl)
4979 {
4980 tag_decl->setTagKind ((TagDecl::TagKind)kind);
4981 return true;
4982 }
4983 }
4984 }
4985 }
4986 return false;
4987}
4988
4989
4990#pragma mark DeclContext Functions
4991
4992DeclContext *
Greg Clayton1be10fc2010-09-29 01:12:09 +00004993ClangASTContext::GetDeclContextForType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004994{
4995 if (clang_type == NULL)
4996 return NULL;
4997
4998 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00004999 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5000 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005001 {
Sean Callanancc427fa2011-07-30 02:42:06 +00005002 case clang::Type::UnaryTransform: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005003 case clang::Type::FunctionNoProto: break;
5004 case clang::Type::FunctionProto: break;
5005 case clang::Type::IncompleteArray: break;
5006 case clang::Type::VariableArray: break;
5007 case clang::Type::ConstantArray: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005008 case clang::Type::DependentSizedArray: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005009 case clang::Type::ExtVector: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005010 case clang::Type::DependentSizedExtVector: break;
Greg Clayton9e409562010-07-28 02:04:09 +00005011 case clang::Type::Vector: break;
5012 case clang::Type::Builtin: break;
5013 case clang::Type::BlockPointer: break;
5014 case clang::Type::Pointer: break;
5015 case clang::Type::LValueReference: break;
5016 case clang::Type::RValueReference: break;
5017 case clang::Type::MemberPointer: break;
5018 case clang::Type::Complex: break;
5019 case clang::Type::ObjCObject: break;
5020 case clang::Type::ObjCInterface: return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface();
5021 case clang::Type::ObjCObjectPointer: return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr());
5022 case clang::Type::Record: return cast<RecordType>(qual_type)->getDecl();
5023 case clang::Type::Enum: return cast<EnumType>(qual_type)->getDecl();
Sean Callanan48114472010-12-13 01:26:27 +00005024 case clang::Type::Typedef: return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00005025 case clang::Type::Elaborated: return ClangASTContext::GetDeclContextForType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton9e409562010-07-28 02:04:09 +00005026 case clang::Type::TypeOfExpr: break;
5027 case clang::Type::TypeOf: break;
5028 case clang::Type::Decltype: break;
5029 //case clang::Type::QualifiedName: break;
5030 case clang::Type::TemplateSpecialization: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005031 case clang::Type::DependentTemplateSpecialization: break;
5032 case clang::Type::TemplateTypeParm: break;
5033 case clang::Type::SubstTemplateTypeParm: break;
5034 case clang::Type::SubstTemplateTypeParmPack:break;
5035 case clang::Type::PackExpansion: break;
5036 case clang::Type::UnresolvedUsing: break;
5037 case clang::Type::Paren: break;
Sean Callananfb0b7582011-03-15 00:17:19 +00005038 case clang::Type::Attributed: break;
5039 case clang::Type::Auto: break;
5040 case clang::Type::InjectedClassName: break;
5041 case clang::Type::DependentName: break;
Greg Claytonea3e7d52011-10-08 00:49:15 +00005042 case clang::Type::Atomic: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005043 }
5044 // No DeclContext in this type...
5045 return NULL;
5046}
5047
5048#pragma mark Namespace Declarations
5049
5050NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00005051ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005052{
Greg Clayton030a2042011-10-14 21:34:45 +00005053 NamespaceDecl *namespace_decl = NULL;
Greg Clayton9d3d6882011-10-31 23:51:19 +00005054 ASTContext *ast = getASTContext();
5055 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
5056 if (decl_ctx == NULL)
5057 decl_ctx = translation_unit_decl;
5058
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005059 if (name)
5060 {
Greg Clayton030a2042011-10-14 21:34:45 +00005061 IdentifierInfo &identifier_info = ast->Idents.get(name);
5062 DeclarationName decl_name (&identifier_info);
5063 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
5064 for (clang::DeclContext::lookup_iterator pos = result.first, end = result.second; pos != end; ++pos)
5065 {
5066 namespace_decl = dyn_cast<clang::NamespaceDecl>(*pos);
5067 if (namespace_decl)
5068 return namespace_decl;
5069 }
5070
Sean Callanan5b26f272012-02-04 08:49:35 +00005071 namespace_decl = NamespaceDecl::Create(*ast,
5072 decl_ctx,
5073 false,
5074 SourceLocation(),
5075 SourceLocation(),
5076 &identifier_info,
5077 NULL);
Greg Clayton030a2042011-10-14 21:34:45 +00005078
Greg Clayton9d3d6882011-10-31 23:51:19 +00005079 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005080 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00005081 else
5082 {
5083 if (decl_ctx == translation_unit_decl)
5084 {
5085 namespace_decl = translation_unit_decl->getAnonymousNamespace();
5086 if (namespace_decl)
5087 return namespace_decl;
5088
Sean Callanan5b26f272012-02-04 08:49:35 +00005089 namespace_decl = NamespaceDecl::Create(*ast,
5090 decl_ctx,
5091 false,
5092 SourceLocation(),
5093 SourceLocation(),
5094 NULL,
5095 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00005096 translation_unit_decl->setAnonymousNamespace (namespace_decl);
5097 translation_unit_decl->addDecl (namespace_decl);
5098 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
5099 }
5100 else
5101 {
5102 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
5103 if (parent_namespace_decl)
5104 {
5105 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
5106 if (namespace_decl)
5107 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00005108 namespace_decl = NamespaceDecl::Create(*ast,
5109 decl_ctx,
5110 false,
5111 SourceLocation(),
5112 SourceLocation(),
5113 NULL,
5114 NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00005115 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
5116 parent_namespace_decl->addDecl (namespace_decl);
5117 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
5118 }
5119 else
5120 {
5121 // BAD!!!
5122 }
5123 }
5124
5125
5126 if (namespace_decl)
5127 {
5128 // If we make it here, we are creating the anonymous namespace decl
5129 // for the first time, so we need to do the using directive magic
5130 // like SEMA does
5131 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast,
5132 decl_ctx,
5133 SourceLocation(),
5134 SourceLocation(),
5135 NestedNameSpecifierLoc(),
5136 SourceLocation(),
5137 namespace_decl,
5138 decl_ctx);
5139 using_directive_decl->setImplicit();
5140 decl_ctx->addDecl(using_directive_decl);
5141 }
5142 }
5143#ifdef LLDB_CONFIGURATION_DEBUG
5144 VerifyDecl(namespace_decl);
5145#endif
Greg Clayton030a2042011-10-14 21:34:45 +00005146 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005147}
5148
5149
5150#pragma mark Function Types
5151
5152FunctionDecl *
Greg Clayton147e1fa2011-10-14 22:47:18 +00005153ClangASTContext::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 +00005154{
Greg Clayton147e1fa2011-10-14 22:47:18 +00005155 FunctionDecl *func_decl = NULL;
5156 ASTContext *ast = getASTContext();
5157 if (decl_ctx == NULL)
5158 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005159
Greg Clayton147e1fa2011-10-14 22:47:18 +00005160 if (name && name[0])
5161 {
5162 func_decl = FunctionDecl::Create (*ast,
5163 decl_ctx,
5164 SourceLocation(),
5165 SourceLocation(),
5166 DeclarationName (&ast->Idents.get(name)),
5167 QualType::getFromOpaquePtr(function_clang_type),
5168 NULL,
5169 (FunctionDecl::StorageClass)storage,
5170 (FunctionDecl::StorageClass)storage,
5171 is_inline);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005172 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00005173 else
5174 {
5175 func_decl = FunctionDecl::Create (*ast,
5176 decl_ctx,
5177 SourceLocation(),
5178 SourceLocation(),
5179 DeclarationName (),
5180 QualType::getFromOpaquePtr(function_clang_type),
5181 NULL,
5182 (FunctionDecl::StorageClass)storage,
5183 (FunctionDecl::StorageClass)storage,
5184 is_inline);
5185 }
5186 if (func_decl)
5187 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005188
5189#ifdef LLDB_CONFIGURATION_DEBUG
5190 VerifyDecl(func_decl);
5191#endif
5192
Greg Clayton147e1fa2011-10-14 22:47:18 +00005193 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005194}
5195
Greg Clayton1be10fc2010-09-29 01:12:09 +00005196clang_type_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00005197ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Clayton1be10fc2010-09-29 01:12:09 +00005198 clang_type_t result_type,
5199 clang_type_t *args,
Sean Callananc81256a2010-09-16 20:40:25 +00005200 unsigned num_args,
5201 bool is_variadic,
5202 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005203{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005204 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005205 std::vector<QualType> qual_type_args;
5206 for (unsigned i=0; i<num_args; ++i)
5207 qual_type_args.push_back (QualType::getFromOpaquePtr(args[i]));
5208
5209 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00005210 FunctionProtoType::ExtProtoInfo proto_info;
5211 proto_info.Variadic = is_variadic;
Sean Callananfb0b7582011-03-15 00:17:19 +00005212 proto_info.ExceptionSpecType = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005213 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00005214 proto_info.RefQualifier = RQ_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00005215 proto_info.NumExceptions = 0;
5216 proto_info.Exceptions = NULL;
5217
Greg Clayton147e1fa2011-10-14 22:47:18 +00005218 return ast->getFunctionType (QualType::getFromOpaquePtr(result_type),
5219 qual_type_args.empty() ? NULL : &qual_type_args.front(),
5220 qual_type_args.size(),
5221 proto_info).getAsOpaquePtr(); // NoReturn);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005222}
5223
5224ParmVarDecl *
Greg Clayton1be10fc2010-09-29 01:12:09 +00005225ClangASTContext::CreateParameterDeclaration (const char *name, clang_type_t param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005226{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005227 ASTContext *ast = getASTContext();
5228 assert (ast != NULL);
5229 return ParmVarDecl::Create(*ast,
5230 ast->getTranslationUnitDecl(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005231 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005232 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00005233 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callananc81256a2010-09-16 20:40:25 +00005234 QualType::getFromOpaquePtr(param_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005235 NULL,
5236 (VarDecl::StorageClass)storage,
5237 (VarDecl::StorageClass)storage,
5238 0);
5239}
5240
5241void
5242ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
5243{
5244 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00005245 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005246}
5247
5248
5249#pragma mark Array Types
5250
Greg Clayton1be10fc2010-09-29 01:12:09 +00005251clang_type_t
5252ClangASTContext::CreateArrayType (clang_type_t element_type, size_t element_count, uint32_t bit_stride)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005253{
5254 if (element_type)
5255 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00005256 ASTContext *ast = getASTContext();
5257 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005258 llvm::APInt ap_element_count (64, element_count);
Greg Clayton6beaaa62011-01-17 03:46:26 +00005259 return ast->getConstantArrayType(QualType::getFromOpaquePtr(element_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005260 ap_element_count,
5261 ArrayType::Normal,
5262 0).getAsOpaquePtr(); // ElemQuals
5263 }
5264 return NULL;
5265}
5266
5267
5268#pragma mark TagDecl
5269
5270bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005271ClangASTContext::StartTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005272{
5273 if (clang_type)
5274 {
5275 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Sean Callanan78e37602011-01-27 04:42:51 +00005276 const clang::Type *t = qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005277 if (t)
5278 {
Sean Callanan78e37602011-01-27 04:42:51 +00005279 const TagType *tag_type = dyn_cast<TagType>(t);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005280 if (tag_type)
5281 {
5282 TagDecl *tag_decl = tag_type->getDecl();
5283 if (tag_decl)
5284 {
5285 tag_decl->startDefinition();
5286 return true;
5287 }
5288 }
Sean Callanan5b26f272012-02-04 08:49:35 +00005289
5290 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(t);
5291 if (object_type)
5292 {
5293 ObjCInterfaceDecl *interface_decl = object_type->getInterface();
5294 if (interface_decl)
5295 {
5296 interface_decl->startDefinition();
5297 return true;
5298 }
5299 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005300 }
5301 }
5302 return false;
5303}
5304
5305bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005306ClangASTContext::CompleteTagDeclarationDefinition (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005307{
5308 if (clang_type)
5309 {
5310 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton14372242010-09-29 03:44:17 +00005311
5312 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5313
5314 if (cxx_record_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005315 {
Greg Clayton14372242010-09-29 03:44:17 +00005316 cxx_record_decl->completeDefinition();
5317
5318 return true;
5319 }
5320
5321 const EnumType *enum_type = dyn_cast<EnumType>(qual_type.getTypePtr());
5322
5323 if (enum_type)
5324 {
5325 EnumDecl *enum_decl = enum_type->getDecl();
5326
5327 if (enum_decl)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005328 {
Greg Clayton14372242010-09-29 03:44:17 +00005329 /// TODO This really needs to be fixed.
5330
5331 unsigned NumPositiveBits = 1;
5332 unsigned NumNegativeBits = 0;
5333
Greg Clayton6beaaa62011-01-17 03:46:26 +00005334 ASTContext *ast = getASTContext();
Greg Claytone02b8502010-10-12 04:29:14 +00005335
5336 QualType promotion_qual_type;
5337 // If the enum integer type is less than an integer in bit width,
5338 // then we must promote it to an integer size.
Greg Clayton6beaaa62011-01-17 03:46:26 +00005339 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
Greg Claytone02b8502010-10-12 04:29:14 +00005340 {
5341 if (enum_decl->getIntegerType()->isSignedIntegerType())
Greg Clayton6beaaa62011-01-17 03:46:26 +00005342 promotion_qual_type = ast->IntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005343 else
Greg Clayton6beaaa62011-01-17 03:46:26 +00005344 promotion_qual_type = ast->UnsignedIntTy;
Greg Claytone02b8502010-10-12 04:29:14 +00005345 }
5346 else
5347 promotion_qual_type = enum_decl->getIntegerType();
5348
5349 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
Greg Clayton14372242010-09-29 03:44:17 +00005350 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005351 }
5352 }
5353 }
5354 return false;
5355}
5356
5357
5358#pragma mark Enumeration Types
5359
Greg Clayton1be10fc2010-09-29 01:12:09 +00005360clang_type_t
Greg Claytonca512b32011-01-14 04:54:56 +00005361ClangASTContext::CreateEnumerationType
5362(
5363 const char *name,
5364 DeclContext *decl_ctx,
5365 const Declaration &decl,
5366 clang_type_t integer_qual_type
5367)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005368{
5369 // TODO: Do something intelligent with the Declaration object passed in
5370 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005371 ASTContext *ast = getASTContext();
5372 assert (ast != NULL);
Greg Claytone02b8502010-10-12 04:29:14 +00005373
5374 // TODO: ask about these...
5375// const bool IsScoped = false;
5376// const bool IsFixed = false;
5377
Greg Clayton6beaaa62011-01-17 03:46:26 +00005378 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00005379 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00005380 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00005381 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00005382 name && name[0] ? &ast->Idents.get(name) : NULL,
Sean Callanan48114472010-12-13 01:26:27 +00005383 NULL,
5384 false, // IsScoped
5385 false, // IsScopedUsingClassTag
5386 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00005387
5388
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005389 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00005390 {
5391 // TODO: check if we should be setting the promotion type too?
5392 enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00005393
5394 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
5395
Greg Clayton6beaaa62011-01-17 03:46:26 +00005396 return ast->getTagDeclType(enum_decl).getAsOpaquePtr();
Greg Clayton83ff3892010-09-12 23:17:56 +00005397 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005398 return NULL;
5399}
5400
Greg Clayton1be10fc2010-09-29 01:12:09 +00005401clang_type_t
5402ClangASTContext::GetEnumerationIntegerType (clang_type_t enum_clang_type)
5403{
5404 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5405
Sean Callanan78e37602011-01-27 04:42:51 +00005406 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Greg Clayton1be10fc2010-09-29 01:12:09 +00005407 if (clang_type)
5408 {
5409 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5410 if (enum_type)
5411 {
5412 EnumDecl *enum_decl = enum_type->getDecl();
5413 if (enum_decl)
5414 return enum_decl->getIntegerType().getAsOpaquePtr();
5415 }
5416 }
5417 return NULL;
5418}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005419bool
5420ClangASTContext::AddEnumerationValueToEnumerationType
5421(
Greg Clayton1be10fc2010-09-29 01:12:09 +00005422 clang_type_t enum_clang_type,
5423 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005424 const Declaration &decl,
5425 const char *name,
5426 int64_t enum_value,
5427 uint32_t enum_value_bit_size
5428)
5429{
5430 if (enum_clang_type && enumerator_clang_type && name)
5431 {
5432 // TODO: Do something intelligent with the Declaration object passed in
5433 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00005434 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005435 IdentifierTable *identifier_table = getIdentifierTable();
5436
Greg Clayton6beaaa62011-01-17 03:46:26 +00005437 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005438 assert (identifier_table != NULL);
5439 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
5440
Sean Callanan78e37602011-01-27 04:42:51 +00005441 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005442 if (clang_type)
5443 {
5444 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5445
5446 if (enum_type)
5447 {
5448 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, false);
5449 enum_llvm_apsint = enum_value;
5450 EnumConstantDecl *enumerator_decl =
Greg Clayton6beaaa62011-01-17 03:46:26 +00005451 EnumConstantDecl::Create (*ast,
5452 enum_type->getDecl(),
5453 SourceLocation(),
5454 name ? &identifier_table->get(name) : NULL, // Identifier
5455 QualType::getFromOpaquePtr(enumerator_clang_type),
5456 NULL,
5457 enum_llvm_apsint);
5458
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005459 if (enumerator_decl)
5460 {
5461 enum_type->getDecl()->addDecl(enumerator_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00005462
5463#ifdef LLDB_CONFIGURATION_DEBUG
5464 VerifyDecl(enumerator_decl);
5465#endif
5466
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005467 return true;
5468 }
5469 }
5470 }
5471 }
5472 return false;
5473}
5474
5475#pragma mark Pointers & References
5476
Greg Clayton1be10fc2010-09-29 01:12:09 +00005477clang_type_t
5478ClangASTContext::CreatePointerType (clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005479{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005480 return CreatePointerType (getASTContext(), clang_type);
5481}
5482
5483clang_type_t
5484ClangASTContext::CreatePointerType (clang::ASTContext *ast, clang_type_t clang_type)
5485{
5486 if (ast && clang_type)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005487 {
5488 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5489
Greg Clayton737b9322010-09-13 03:32:57 +00005490 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5491 switch (type_class)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005492 {
5493 case clang::Type::ObjCObject:
5494 case clang::Type::ObjCInterface:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005495 return ast->getObjCObjectPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005496
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005497 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00005498 return ast->getPointerType(qual_type).getAsOpaquePtr();
Greg Clayton5fb47cd2010-07-29 20:06:32 +00005499 }
5500 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005501 return NULL;
5502}
5503
Greg Clayton1be10fc2010-09-29 01:12:09 +00005504clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005505ClangASTContext::CreateLValueReferenceType (clang::ASTContext *ast,
5506 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005507{
5508 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005509 return ast->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005510 return NULL;
5511}
5512
Greg Clayton1be10fc2010-09-29 01:12:09 +00005513clang_type_t
Sean Callanan92adcac2011-01-13 08:53:35 +00005514ClangASTContext::CreateRValueReferenceType (clang::ASTContext *ast,
5515 clang_type_t clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005516{
5517 if (clang_type)
Sean Callanan92adcac2011-01-13 08:53:35 +00005518 return ast->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005519 return NULL;
5520}
5521
Greg Clayton1be10fc2010-09-29 01:12:09 +00005522clang_type_t
5523ClangASTContext::CreateMemberPointerType (clang_type_t clang_pointee_type, clang_type_t clang_class_type)
Greg Clayton9b81a312010-06-12 01:20:30 +00005524{
5525 if (clang_pointee_type && clang_pointee_type)
5526 return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type),
5527 QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr();
5528 return NULL;
5529}
5530
Greg Clayton1a65ae12011-01-25 23:55:37 +00005531uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005532ClangASTContext::GetPointerBitSize ()
5533{
Greg Clayton6beaaa62011-01-17 03:46:26 +00005534 ASTContext *ast = getASTContext();
5535 return ast->getTypeSize(ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005536}
5537
5538bool
Greg Clayton219cf312012-03-30 00:51:13 +00005539ClangASTContext::IsPossibleDynamicType (clang::ASTContext *ast,
5540 clang_type_t clang_type,
5541 clang_type_t *dynamic_pointee_type,
5542 bool check_cplusplus,
5543 bool check_objc)
Greg Claytondea8cb42011-06-29 22:09:02 +00005544{
5545 QualType pointee_qual_type;
5546 if (clang_type)
5547 {
5548 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5549 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5550 bool success = false;
5551 switch (type_class)
5552 {
5553 case clang::Type::Builtin:
Greg Clayton219cf312012-03-30 00:51:13 +00005554 if (check_objc && cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
Greg Claytondea8cb42011-06-29 22:09:02 +00005555 {
5556 if (dynamic_pointee_type)
5557 *dynamic_pointee_type = clang_type;
5558 return true;
5559 }
5560 break;
5561
5562 case clang::Type::ObjCObjectPointer:
Greg Clayton219cf312012-03-30 00:51:13 +00005563 if (check_objc)
5564 {
5565 if (dynamic_pointee_type)
5566 *dynamic_pointee_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5567 return true;
5568 }
5569 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005570
5571 case clang::Type::Pointer:
5572 pointee_qual_type = cast<PointerType>(qual_type)->getPointeeType();
5573 success = true;
5574 break;
5575
5576 case clang::Type::LValueReference:
5577 case clang::Type::RValueReference:
5578 pointee_qual_type = cast<ReferenceType>(qual_type)->getPointeeType();
5579 success = true;
5580 break;
5581
5582 case clang::Type::Typedef:
Greg Clayton70364252012-08-31 18:56:24 +00005583 return ClangASTContext::IsPossibleDynamicType (ast,
5584 cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
5585 dynamic_pointee_type,
5586 check_cplusplus,
5587 check_objc);
Sean Callanan912855f2011-08-11 23:56:13 +00005588
5589 case clang::Type::Elaborated:
Greg Clayton70364252012-08-31 18:56:24 +00005590 return ClangASTContext::IsPossibleDynamicType (ast,
5591 cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
5592 dynamic_pointee_type,
5593 check_cplusplus,
5594 check_objc);
Sean Callanan912855f2011-08-11 23:56:13 +00005595
Greg Claytondea8cb42011-06-29 22:09:02 +00005596 default:
5597 break;
5598 }
5599
5600 if (success)
5601 {
5602 // Check to make sure what we are pointing too is a possible dynamic C++ type
5603 // We currently accept any "void *" (in case we have a class that has been
5604 // watered down to an opaque pointer) and virtual C++ classes.
5605 const clang::Type::TypeClass pointee_type_class = pointee_qual_type->getTypeClass();
5606 switch (pointee_type_class)
5607 {
5608 case clang::Type::Builtin:
5609 switch (cast<clang::BuiltinType>(pointee_qual_type)->getKind())
5610 {
5611 case clang::BuiltinType::UnknownAny:
5612 case clang::BuiltinType::Void:
5613 if (dynamic_pointee_type)
5614 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5615 return true;
5616
5617 case clang::BuiltinType::NullPtr:
5618 case clang::BuiltinType::Bool:
5619 case clang::BuiltinType::Char_U:
5620 case clang::BuiltinType::UChar:
5621 case clang::BuiltinType::WChar_U:
5622 case clang::BuiltinType::Char16:
5623 case clang::BuiltinType::Char32:
5624 case clang::BuiltinType::UShort:
5625 case clang::BuiltinType::UInt:
5626 case clang::BuiltinType::ULong:
5627 case clang::BuiltinType::ULongLong:
5628 case clang::BuiltinType::UInt128:
5629 case clang::BuiltinType::Char_S:
5630 case clang::BuiltinType::SChar:
5631 case clang::BuiltinType::WChar_S:
5632 case clang::BuiltinType::Short:
5633 case clang::BuiltinType::Int:
5634 case clang::BuiltinType::Long:
5635 case clang::BuiltinType::LongLong:
5636 case clang::BuiltinType::Int128:
5637 case clang::BuiltinType::Float:
5638 case clang::BuiltinType::Double:
5639 case clang::BuiltinType::LongDouble:
5640 case clang::BuiltinType::Dependent:
5641 case clang::BuiltinType::Overload:
5642 case clang::BuiltinType::ObjCId:
5643 case clang::BuiltinType::ObjCClass:
5644 case clang::BuiltinType::ObjCSel:
5645 case clang::BuiltinType::BoundMember:
Greg Claytonf0705c82011-10-22 03:33:13 +00005646 case clang::BuiltinType::Half:
5647 case clang::BuiltinType::ARCUnbridgedCast:
Greg Claytoned3ae702011-11-09 19:04:58 +00005648 case clang::BuiltinType::PseudoObject:
Sean Callanan3d654b32012-09-24 22:25:51 +00005649 case clang::BuiltinType::BuiltinFn:
Greg Claytondea8cb42011-06-29 22:09:02 +00005650 break;
5651 }
5652 break;
5653
5654 case clang::Type::Record:
Greg Clayton219cf312012-03-30 00:51:13 +00005655 if (check_cplusplus)
Greg Claytondea8cb42011-06-29 22:09:02 +00005656 {
5657 CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
5658 if (cxx_record_decl)
5659 {
Greg Clayton70364252012-08-31 18:56:24 +00005660 bool is_complete = cxx_record_decl->isCompleteDefinition();
5661 if (!is_complete)
Sean Callanan6e4100ea2012-09-08 00:49:45 +00005662 is_complete = ClangASTContext::GetCompleteType (ast, pointee_qual_type.getAsOpaquePtr());
Greg Clayton70364252012-08-31 18:56:24 +00005663
5664 if (is_complete)
Greg Claytondea8cb42011-06-29 22:09:02 +00005665 {
5666 success = cxx_record_decl->isDynamicClass();
5667 }
5668 else
5669 {
Greg Clayton70364252012-08-31 18:56:24 +00005670 success = false;
Greg Claytondea8cb42011-06-29 22:09:02 +00005671 }
Greg Clayton70364252012-08-31 18:56:24 +00005672
Greg Claytondea8cb42011-06-29 22:09:02 +00005673 if (success)
5674 {
5675 if (dynamic_pointee_type)
5676 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5677 return true;
5678 }
5679 }
5680 }
5681 break;
5682
5683 case clang::Type::ObjCObject:
5684 case clang::Type::ObjCInterface:
Greg Clayton219cf312012-03-30 00:51:13 +00005685 if (check_objc)
5686 {
5687 if (dynamic_pointee_type)
5688 *dynamic_pointee_type = pointee_qual_type.getAsOpaquePtr();
5689 return true;
5690 }
5691 break;
Greg Claytondea8cb42011-06-29 22:09:02 +00005692
5693 default:
5694 break;
5695 }
5696 }
5697 }
5698 if (dynamic_pointee_type)
5699 *dynamic_pointee_type = NULL;
5700 return false;
5701}
5702
5703
5704bool
Greg Clayton007d5be2011-05-30 00:49:24 +00005705ClangASTContext::IsPossibleCPlusPlusDynamicType (clang::ASTContext *ast, clang_type_t clang_type, clang_type_t *dynamic_pointee_type)
5706{
Greg Clayton219cf312012-03-30 00:51:13 +00005707 return IsPossibleDynamicType (ast,
5708 clang_type,
5709 dynamic_pointee_type,
5710 true, // Check for dynamic C++ types
5711 false); // Check for dynamic ObjC types
Greg Clayton007d5be2011-05-30 00:49:24 +00005712}
5713
Sean Callanan98298012011-10-27 19:41:13 +00005714bool
5715ClangASTContext::IsReferenceType (clang_type_t clang_type, clang_type_t *target_type)
5716{
5717 if (clang_type == NULL)
5718 return false;
5719
5720 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5721 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5722
5723 switch (type_class)
5724 {
5725 case clang::Type::LValueReference:
5726 if (target_type)
5727 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5728 return true;
5729 case clang::Type::RValueReference:
5730 if (target_type)
5731 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5732 return true;
5733 case clang::Type::Typedef:
5734 return ClangASTContext::IsReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5735 case clang::Type::Elaborated:
5736 return ClangASTContext::IsReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5737 default:
5738 break;
5739 }
5740
5741 return false;
5742}
Greg Clayton007d5be2011-05-30 00:49:24 +00005743
5744bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005745ClangASTContext::IsPointerOrReferenceType (clang_type_t clang_type, clang_type_t*target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005746{
5747 if (clang_type == NULL)
5748 return false;
5749
5750 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005751 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5752 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005753 {
Sean Callanana2424172010-10-25 00:29:48 +00005754 case clang::Type::Builtin:
5755 switch (cast<clang::BuiltinType>(qual_type)->getKind())
5756 {
5757 default:
5758 break;
5759 case clang::BuiltinType::ObjCId:
5760 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00005761 return true;
5762 }
5763 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00005764 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005765 if (target_type)
5766 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5767 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005768 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005769 if (target_type)
5770 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5771 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005772 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005773 if (target_type)
5774 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5775 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005776 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005777 if (target_type)
5778 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5779 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005780 case clang::Type::LValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005781 if (target_type)
5782 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5783 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005784 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005785 if (target_type)
5786 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
5787 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005788 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00005789 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00005790 case clang::Type::Elaborated:
5791 return ClangASTContext::IsPointerOrReferenceType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005792 default:
5793 break;
5794 }
5795 return false;
5796}
5797
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005798bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005799ClangASTContext::IsIntegerType (clang_type_t clang_type, bool &is_signed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005800{
5801 if (!clang_type)
5802 return false;
5803
5804 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5805 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal());
5806
5807 if (builtin_type)
5808 {
5809 if (builtin_type->isInteger())
Jim Inghamef651602011-12-22 19:12:40 +00005810 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005811 is_signed = builtin_type->isSignedInteger();
Jim Inghamef651602011-12-22 19:12:40 +00005812 return true;
5813 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005814 }
5815
5816 return false;
5817}
5818
5819bool
Greg Claytonaffb03b2011-07-08 18:27:39 +00005820ClangASTContext::IsPointerType (clang_type_t clang_type, clang_type_t *target_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005821{
Greg Claytonaffb03b2011-07-08 18:27:39 +00005822 if (target_type)
5823 *target_type = NULL;
5824
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005825 if (clang_type)
5826 {
5827 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00005828 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5829 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005830 {
Sean Callanana2424172010-10-25 00:29:48 +00005831 case clang::Type::Builtin:
5832 switch (cast<clang::BuiltinType>(qual_type)->getKind())
5833 {
5834 default:
5835 break;
5836 case clang::BuiltinType::ObjCId:
5837 case clang::BuiltinType::ObjCClass:
Sean Callanana2424172010-10-25 00:29:48 +00005838 return true;
5839 }
5840 return false;
Greg Claytone1a916a2010-07-21 22:12:05 +00005841 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005842 if (target_type)
5843 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5844 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005845 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005846 if (target_type)
5847 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5848 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005849 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005850 if (target_type)
5851 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5852 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005853 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005854 if (target_type)
5855 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
5856 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00005857 case clang::Type::Typedef:
Greg Claytonaffb03b2011-07-08 18:27:39 +00005858 return ClangASTContext::IsPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), target_type);
Sean Callanan912855f2011-08-11 23:56:13 +00005859 case clang::Type::Elaborated:
5860 return ClangASTContext::IsPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), target_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005861 default:
5862 break;
5863 }
5864 }
5865 return false;
5866}
5867
5868bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005869ClangASTContext::IsFloatingPointType (clang_type_t clang_type, uint32_t &count, bool &is_complex)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005870{
5871 if (clang_type)
5872 {
5873 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5874
5875 if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()))
5876 {
5877 clang::BuiltinType::Kind kind = BT->getKind();
5878 if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble)
5879 {
5880 count = 1;
5881 is_complex = false;
5882 return true;
5883 }
5884 }
5885 else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal()))
5886 {
5887 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex))
5888 {
5889 count = 2;
5890 is_complex = true;
5891 return true;
5892 }
5893 }
5894 else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal()))
5895 {
5896 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex))
5897 {
5898 count = VT->getNumElements();
5899 is_complex = false;
5900 return true;
5901 }
5902 }
5903 }
5904 return false;
5905}
5906
Enrico Granata9fc19442011-07-06 02:13:41 +00005907bool
5908ClangASTContext::IsScalarType (lldb::clang_type_t clang_type)
5909{
5910 bool is_signed;
5911 if (ClangASTContext::IsIntegerType(clang_type, is_signed))
5912 return true;
5913
5914 uint32_t count;
5915 bool is_complex;
5916 return ClangASTContext::IsFloatingPointType(clang_type, count, is_complex) && !is_complex;
5917}
5918
5919bool
5920ClangASTContext::IsPointerToScalarType (lldb::clang_type_t clang_type)
5921{
5922 if (!IsPointerType(clang_type))
5923 return false;
5924
5925 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5926 lldb::clang_type_t pointee_type = qual_type.getTypePtr()->getPointeeType().getAsOpaquePtr();
5927 return IsScalarType(pointee_type);
5928}
5929
5930bool
5931ClangASTContext::IsArrayOfScalarType (lldb::clang_type_t clang_type)
5932{
Sean Callanan0caa21c2012-01-19 23:54:24 +00005933 clang_type = GetAsArrayType(clang_type);
5934
5935 if (clang_type == 0)
Enrico Granata9fc19442011-07-06 02:13:41 +00005936 return false;
5937
5938 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5939 lldb::clang_type_t item_type = cast<ArrayType>(qual_type.getTypePtr())->getElementType().getAsOpaquePtr();
5940 return IsScalarType(item_type);
5941}
5942
Greg Clayton8f92f0a2010-10-14 22:52:14 +00005943
5944bool
5945ClangASTContext::GetCXXClassName (clang_type_t clang_type, std::string &class_name)
5946{
5947 if (clang_type)
5948 {
5949 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5950
5951 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5952 if (cxx_record_decl)
5953 {
5954 class_name.assign (cxx_record_decl->getIdentifier()->getNameStart());
5955 return true;
5956 }
5957 }
5958 class_name.clear();
5959 return false;
5960}
5961
5962
Greg Clayton0fffff52010-09-24 05:15:53 +00005963bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005964ClangASTContext::IsCXXClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00005965{
5966 if (clang_type)
5967 {
5968 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5969 if (qual_type->getAsCXXRecordDecl() != NULL)
5970 return true;
5971 }
5972 return false;
5973}
5974
Greg Clayton20568dd2011-10-13 23:13:20 +00005975bool
5976ClangASTContext::IsBeingDefined (lldb::clang_type_t clang_type)
5977{
5978 if (clang_type)
5979 {
5980 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5981 const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type);
5982 if (tag_type)
5983 return tag_type->isBeingDefined();
5984 }
5985 return false;
5986}
5987
Greg Clayton0fffff52010-09-24 05:15:53 +00005988bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00005989ClangASTContext::IsObjCClassType (clang_type_t clang_type)
Greg Clayton0fffff52010-09-24 05:15:53 +00005990{
5991 if (clang_type)
5992 {
5993 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
5994 if (qual_type->isObjCObjectOrInterfaceType())
5995 return true;
5996 }
5997 return false;
5998}
5999
Sean Callanan72772842012-02-22 23:57:45 +00006000bool
6001ClangASTContext::IsObjCObjectPointerType (lldb::clang_type_t clang_type, clang_type_t *class_type)
6002{
6003 if (clang_type)
6004 {
6005 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6006 if (qual_type->isObjCObjectPointerType())
6007 {
6008 if (class_type)
6009 {
6010 *class_type = NULL;
6011
6012 if (!qual_type->isObjCClassType() &&
6013 !qual_type->isObjCIdType())
6014 {
6015 const ObjCObjectPointerType *obj_pointer_type = dyn_cast<ObjCObjectPointerType>(qual_type);
Sean Callanand7dabe22012-03-10 01:59:11 +00006016 if (!obj_pointer_type)
6017 *class_type = NULL;
Sean Callanan1fc91ad2012-03-10 02:00:32 +00006018 else
6019 *class_type = QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr();
Sean Callanan72772842012-02-22 23:57:45 +00006020 }
6021 }
6022 return true;
6023 }
6024 }
6025 return false;
6026}
6027
6028bool
6029ClangASTContext::GetObjCClassName (lldb::clang_type_t clang_type,
6030 std::string &class_name)
6031{
6032 if (!clang_type)
6033 return false;
6034
6035 const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(QualType::getFromOpaquePtr(clang_type));
6036 if (!object_type)
6037 return false;
6038
6039 const ObjCInterfaceDecl *interface = object_type->getInterface();
6040 if (!interface)
6041 return false;
6042
6043 class_name = interface->getNameAsString();
6044 return true;
6045}
Greg Clayton0fffff52010-09-24 05:15:53 +00006046
Greg Clayton73b472d2010-10-27 03:32:59 +00006047bool
6048ClangASTContext::IsCharType (clang_type_t clang_type)
6049{
6050 if (clang_type)
6051 return QualType::getFromOpaquePtr(clang_type)->isCharType();
6052 return false;
6053}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006054
6055bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006056ClangASTContext::IsCStringType (clang_type_t clang_type, uint32_t &length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006057{
Greg Clayton73b472d2010-10-27 03:32:59 +00006058 clang_type_t pointee_or_element_clang_type = NULL;
6059 Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, &pointee_or_element_clang_type));
6060
6061 if (pointee_or_element_clang_type == NULL)
6062 return false;
6063
6064 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006065 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006066 QualType pointee_or_element_qual_type (QualType::getFromOpaquePtr (pointee_or_element_clang_type));
6067
6068 if (pointee_or_element_qual_type.getUnqualifiedType()->isCharType())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006069 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006070 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6071 if (type_flags.Test (eTypeIsArray))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006072 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006073 // We know the size of the array and it could be a C string
6074 // since it is an array of characters
6075 length = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
6076 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006077 }
Greg Clayton73b472d2010-10-27 03:32:59 +00006078 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006079 {
Greg Clayton73b472d2010-10-27 03:32:59 +00006080 length = 0;
6081 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006082 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006083
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006084 }
6085 }
6086 return false;
6087}
6088
6089bool
Greg Clayton1be10fc2010-09-29 01:12:09 +00006090ClangASTContext::IsFunctionPointerType (clang_type_t clang_type)
Greg Clayton737b9322010-09-13 03:32:57 +00006091{
6092 if (clang_type)
6093 {
6094 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6095
6096 if (qual_type->isFunctionPointerType())
6097 return true;
6098
6099 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6100 switch (type_class)
6101 {
Sean Callananfb0b7582011-03-15 00:17:19 +00006102 default:
6103 break;
Greg Clayton737b9322010-09-13 03:32:57 +00006104 case clang::Type::Typedef:
Sean Callanan48114472010-12-13 01:26:27 +00006105 return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006106 case clang::Type::Elaborated:
6107 return ClangASTContext::IsFunctionPointerType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00006108
6109 case clang::Type::LValueReference:
6110 case clang::Type::RValueReference:
6111 {
Sean Callanan78e37602011-01-27 04:42:51 +00006112 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton737b9322010-09-13 03:32:57 +00006113 if (reference_type)
6114 return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr());
6115 }
6116 break;
6117 }
6118 }
6119 return false;
6120}
6121
Greg Clayton73b472d2010-10-27 03:32:59 +00006122size_t
6123ClangASTContext::GetArraySize (clang_type_t clang_type)
6124{
6125 if (clang_type)
6126 {
Greg Claytonef37d68a2011-07-09 17:12:27 +00006127 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
6128 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6129 switch (type_class)
6130 {
6131 case clang::Type::ConstantArray:
6132 {
6133 const ConstantArrayType *array = cast<ConstantArrayType>(QualType::getFromOpaquePtr(clang_type).getTypePtr());
6134 if (array)
6135 return array->getSize().getLimitedValue();
6136 }
6137 break;
6138
6139 case clang::Type::Typedef:
6140 return ClangASTContext::GetArraySize(cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Sean Callanan912855f2011-08-11 23:56:13 +00006141
6142 case clang::Type::Elaborated:
6143 return ClangASTContext::GetArraySize(cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00006144
6145 default:
6146 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006147 }
Greg Clayton73b472d2010-10-27 03:32:59 +00006148 }
6149 return 0;
6150}
Greg Clayton737b9322010-09-13 03:32:57 +00006151
Sean Callanan0caa21c2012-01-19 23:54:24 +00006152clang_type_t
6153ClangASTContext::GetAsArrayType (clang_type_t clang_type, clang_type_t*member_type, uint64_t *size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006154{
6155 if (!clang_type)
Sean Callanan0caa21c2012-01-19 23:54:24 +00006156 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006157
6158 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6159
Greg Clayton737b9322010-09-13 03:32:57 +00006160 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6161 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006162 {
Sean Callananfb0b7582011-03-15 00:17:19 +00006163 default:
6164 break;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006165
Greg Claytone1a916a2010-07-21 22:12:05 +00006166 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006167 if (member_type)
6168 *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6169 if (size)
Greg Claytonac4827f2011-04-01 18:14:08 +00006170 *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
Sean Callanan0caa21c2012-01-19 23:54:24 +00006171 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006172
Greg Claytone1a916a2010-07-21 22:12:05 +00006173 case clang::Type::IncompleteArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006174 if (member_type)
6175 *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6176 if (size)
6177 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006178 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006179
Greg Claytone1a916a2010-07-21 22:12:05 +00006180 case clang::Type::VariableArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006181 if (member_type)
6182 *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6183 if (size)
6184 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006185 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006186
Greg Claytone1a916a2010-07-21 22:12:05 +00006187 case clang::Type::DependentSizedArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006188 if (member_type)
6189 *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr();
6190 if (size)
6191 *size = 0;
Sean Callanan0caa21c2012-01-19 23:54:24 +00006192 return clang_type;
Greg Claytonef37d68a2011-07-09 17:12:27 +00006193
6194 case clang::Type::Typedef:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006195 return ClangASTContext::GetAsArrayType (cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
6196 member_type,
6197 size);
Sean Callanan912855f2011-08-11 23:56:13 +00006198
6199 case clang::Type::Elaborated:
Sean Callanan0caa21c2012-01-19 23:54:24 +00006200 return ClangASTContext::GetAsArrayType (cast<ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
6201 member_type,
6202 size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006203 }
Sean Callanan0caa21c2012-01-19 23:54:24 +00006204 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006205}
6206
6207
6208#pragma mark Typedefs
6209
Greg Clayton1be10fc2010-09-29 01:12:09 +00006210clang_type_t
6211ClangASTContext::CreateTypedefType (const char *name, clang_type_t clang_type, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006212{
6213 if (clang_type)
6214 {
6215 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006216 ASTContext *ast = getASTContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006217 IdentifierTable *identifier_table = getIdentifierTable();
Greg Clayton6beaaa62011-01-17 03:46:26 +00006218 assert (ast != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006219 assert (identifier_table != NULL);
6220 if (decl_ctx == NULL)
Greg Clayton6beaaa62011-01-17 03:46:26 +00006221 decl_ctx = ast->getTranslationUnitDecl();
6222 TypedefDecl *decl = TypedefDecl::Create (*ast,
6223 decl_ctx,
6224 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00006225 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00006226 name ? &identifier_table->get(name) : NULL, // Identifier
6227 ast->CreateTypeSourceInfo(qual_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00006228
Greg Clayton147e1fa2011-10-14 22:47:18 +00006229 //decl_ctx->addDecl (decl);
6230
Sean Callanan2652ad22011-01-18 01:03:44 +00006231 decl->setAccess(AS_public); // TODO respect proper access specifier
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006232
6233 // Get a uniqued QualType for the typedef decl type
Greg Clayton6beaaa62011-01-17 03:46:26 +00006234 return ast->getTypedefType (decl).getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006235 }
6236 return NULL;
6237}
6238
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006239// Disable this for now since I can't seem to get a nicely formatted float
6240// out of the APFloat class without just getting the float, double or quad
6241// and then using a formatted print on it which defeats the purpose. We ideally
6242// would like to get perfect string values for any kind of float semantics
6243// so we can support remote targets. The code below also requires a patch to
6244// llvm::APInt.
6245//bool
Greg Clayton6beaaa62011-01-17 03:46:26 +00006246//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 +00006247//{
6248// uint32_t count = 0;
6249// bool is_complex = false;
6250// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6251// {
6252// unsigned num_bytes_per_float = byte_size / count;
6253// unsigned num_bits_per_float = num_bytes_per_float * 8;
6254//
6255// float_str.clear();
6256// uint32_t i;
6257// for (i=0; i<count; i++)
6258// {
6259// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
6260// bool is_ieee = false;
6261// APFloat ap_float(ap_int, is_ieee);
6262// char s[1024];
6263// unsigned int hex_digits = 0;
6264// bool upper_case = false;
6265//
6266// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
6267// {
6268// if (i > 0)
6269// float_str.append(", ");
6270// float_str.append(s);
6271// if (i == 1 && is_complex)
6272// float_str.append(1, 'i');
6273// }
6274// }
6275// return !float_str.empty();
6276// }
6277// return false;
6278//}
6279
6280size_t
Greg Clayton6beaaa62011-01-17 03:46:26 +00006281ClangASTContext::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 +00006282{
6283 if (clang_type)
6284 {
6285 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6286 uint32_t count = 0;
6287 bool is_complex = false;
6288 if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
6289 {
6290 // TODO: handle complex and vector types
6291 if (count != 1)
6292 return false;
6293
6294 StringRef s_sref(s);
Greg Clayton6beaaa62011-01-17 03:46:26 +00006295 APFloat ap_float(ast->getFloatTypeSemantics(qual_type), s_sref);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006296
Greg Clayton6beaaa62011-01-17 03:46:26 +00006297 const uint64_t bit_size = ast->getTypeSize (qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006298 const uint64_t byte_size = bit_size / 8;
6299 if (dst_size >= byte_size)
6300 {
6301 if (bit_size == sizeof(float)*8)
6302 {
6303 float float32 = ap_float.convertToFloat();
6304 ::memcpy (dst, &float32, byte_size);
6305 return byte_size;
6306 }
6307 else if (bit_size >= 64)
6308 {
6309 llvm::APInt ap_int(ap_float.bitcastToAPInt());
6310 ::memcpy (dst, ap_int.getRawData(), byte_size);
6311 return byte_size;
6312 }
6313 }
6314 }
6315 }
6316 return 0;
6317}
Sean Callanan6fe64b52010-09-17 02:24:29 +00006318
6319unsigned
Greg Clayton1be10fc2010-09-29 01:12:09 +00006320ClangASTContext::GetTypeQualifiers(clang_type_t clang_type)
Sean Callanan6fe64b52010-09-17 02:24:29 +00006321{
6322 assert (clang_type);
6323
6324 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
6325
6326 return qual_type.getQualifiers().getCVRQualifiers();
6327}
Greg Clayton6beaaa62011-01-17 03:46:26 +00006328
6329bool
6330ClangASTContext::GetCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6331{
6332 if (clang_type == NULL)
6333 return false;
6334
Greg Claytonc432c192011-01-20 04:18:48 +00006335 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type));
Greg Clayton6beaaa62011-01-17 03:46:26 +00006336}
6337
6338
6339bool
6340ClangASTContext::GetCompleteType (clang_type_t clang_type)
6341{
6342 return ClangASTContext::GetCompleteType (getASTContext(), clang_type);
6343}
6344
Greg Claytona2721472011-06-25 00:44:06 +00006345bool
Enrico Granata86027e92012-03-24 01:11:14 +00006346ClangASTContext::IsCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
6347{
6348 if (clang_type == NULL)
6349 return false;
6350
6351 return GetCompleteQualType (ast, clang::QualType::getFromOpaquePtr(clang_type), false); // just check but don't let it actually complete
6352}
6353
6354
6355bool
6356ClangASTContext::IsCompleteType (clang_type_t clang_type)
6357{
6358 return ClangASTContext::IsCompleteType (getASTContext(), clang_type);
6359}
6360
6361bool
Greg Claytona2721472011-06-25 00:44:06 +00006362ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
6363 clang::Decl *decl)
6364{
6365 if (!decl)
6366 return false;
6367
6368 ExternalASTSource *ast_source = ast->getExternalSource();
6369
6370 if (!ast_source)
6371 return false;
6372
6373 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
6374 {
Greg Clayton219cf312012-03-30 00:51:13 +00006375 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006376 return true;
6377
6378 if (!tag_decl->hasExternalLexicalStorage())
6379 return false;
6380
6381 ast_source->CompleteType(tag_decl);
6382
6383 return !tag_decl->getTypeForDecl()->isIncompleteType();
6384 }
6385 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
6386 {
Sean Callanan5b26f272012-02-04 08:49:35 +00006387 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00006388 return true;
6389
6390 if (!objc_interface_decl->hasExternalLexicalStorage())
6391 return false;
6392
6393 ast_source->CompleteType(objc_interface_decl);
6394
Sean Callanan5b26f272012-02-04 08:49:35 +00006395 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00006396 }
6397 else
6398 {
6399 return false;
6400 }
6401}
6402
Sean Callanan60217122012-04-13 00:10:03 +00006403void
Jim Ingham379397632012-10-27 02:54:13 +00006404ClangASTContext::SetMetadataAsUserID (uintptr_t object,
6405 user_id_t user_id)
6406{
6407 ClangASTMetadata meta_data;
6408 meta_data.SetUserID (user_id);
6409 SetMetadata (object, meta_data);
6410}
6411
6412void
Sean Callanan60217122012-04-13 00:10:03 +00006413ClangASTContext::SetMetadata (clang::ASTContext *ast,
6414 uintptr_t object,
Jim Ingham379397632012-10-27 02:54:13 +00006415 ClangASTMetadata &metadata)
Sean Callanan60217122012-04-13 00:10:03 +00006416{
6417 ClangExternalASTSourceCommon *external_source =
6418 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
6419
6420 if (external_source)
6421 external_source->SetMetadata(object, metadata);
6422}
6423
Jim Ingham379397632012-10-27 02:54:13 +00006424ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +00006425ClangASTContext::GetMetadata (clang::ASTContext *ast,
6426 uintptr_t object)
6427{
6428 ClangExternalASTSourceCommon *external_source =
6429 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
6430
6431 if (external_source && external_source->HasMetadata(object))
6432 return external_source->GetMetadata(object);
6433 else
Jim Ingham379397632012-10-27 02:54:13 +00006434 return NULL;
Sean Callanan60217122012-04-13 00:10:03 +00006435}
6436
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006437clang::DeclContext *
6438ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
6439{
Sean Callanana87bee82011-08-19 06:19:25 +00006440 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006441}
6442
6443clang::DeclContext *
6444ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
6445{
Sean Callanana87bee82011-08-19 06:19:25 +00006446 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006447}
6448
Greg Clayton685c88c2012-07-14 00:53:55 +00006449
6450bool
6451ClangASTContext::GetClassMethodInfoForDeclContext (clang::DeclContext *decl_ctx,
6452 lldb::LanguageType &language,
6453 bool &is_instance_method,
6454 ConstString &language_object_name)
6455{
6456 language_object_name.Clear();
6457 language = eLanguageTypeUnknown;
6458 is_instance_method = false;
6459
6460 if (decl_ctx)
6461 {
6462 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
6463 {
6464 if (method_decl->isStatic())
6465 {
6466 is_instance_method = false;
6467 }
6468 else
6469 {
6470 language_object_name.SetCString("this");
6471 is_instance_method = true;
6472 }
6473 language = eLanguageTypeC_plus_plus;
6474 return true;
6475 }
6476 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
6477 {
6478 // Both static and instance methods have a "self" object in objective C
6479 language_object_name.SetCString("self");
6480 if (method_decl->isInstanceMethod())
6481 {
6482 is_instance_method = true;
6483 }
6484 else
6485 {
6486 is_instance_method = false;
6487 }
6488 language = eLanguageTypeObjC;
6489 return true;
6490 }
Jim Ingham379397632012-10-27 02:54:13 +00006491 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
6492 {
6493 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), (uintptr_t) function_decl);
6494 if (metadata && metadata->HasObjectPtr())
6495 {
6496 language_object_name.SetCString (metadata->GetObjectPtrName());
6497 language = eLanguageTypeObjC;
6498 is_instance_method = true;
6499 }
6500 return true;
6501 }
Greg Clayton685c88c2012-07-14 00:53:55 +00006502 }
6503 return false;
6504}
6505