blob: 66b129dfb4d7cc35e824d3db0652dcc4f10349ff [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
Sean Callanan246549c2010-07-08 18:16:16 +000017#define NDEBUG
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/ASTImporter.h"
20#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000021#include "clang/AST/DeclObjC.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "clang/AST/RecordLayout.h"
23#include "clang/AST/Type.h"
24#include "clang/Basic/Builtins.h"
25#include "clang/Basic/FileManager.h"
26#include "clang/Basic/SourceManager.h"
27#include "clang/Basic/TargetInfo.h"
28#include "clang/Basic/TargetOptions.h"
29#include "clang/Frontend/FrontendOptions.h"
30#include "clang/Frontend/LangStandard.h"
Sean Callanan246549c2010-07-08 18:16:16 +000031#undef NDEBUG
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Core/dwarf.h"
34
Eli Friedman932197d2010-06-13 19:06:42 +000035#include <stdio.h>
36
Greg Claytonc86103d2010-08-05 01:57:25 +000037using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038using namespace lldb_private;
39using namespace llvm;
40using namespace clang;
41
Greg Clayton8cf05932010-07-22 18:30:50 +000042static AccessSpecifier
Greg Claytonc86103d2010-08-05 01:57:25 +000043ConvertAccessTypeToAccessSpecifier (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +000044{
45 switch (access)
46 {
Greg Claytonc86103d2010-08-05 01:57:25 +000047 default: break;
48 case eAccessNone: return AS_none;
49 case eAccessPublic: return AS_public;
50 case eAccessPrivate: return AS_private;
51 case eAccessProtected: return AS_protected;
Greg Clayton8cf05932010-07-22 18:30:50 +000052 }
53 return AS_none;
54}
55
56static ObjCIvarDecl::AccessControl
Greg Claytonc86103d2010-08-05 01:57:25 +000057ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +000058{
59 switch (access)
60 {
Greg Claytonc86103d2010-08-05 01:57:25 +000061 default: break;
62 case eAccessNone: return ObjCIvarDecl::None;
63 case eAccessPublic: return ObjCIvarDecl::Public;
64 case eAccessPrivate: return ObjCIvarDecl::Private;
65 case eAccessProtected: return ObjCIvarDecl::Protected;
66 case eAccessPackage: return ObjCIvarDecl::Package;
Greg Clayton8cf05932010-07-22 18:30:50 +000067 }
68 return ObjCIvarDecl::None;
69}
70
71
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072static void
73ParseLangArgs
74(
75 LangOptions &Opts,
Greg Clayton94e5d782010-06-13 17:34:29 +000076 InputKind IK
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077)
78{
79 // FIXME: Cleanup per-file based stuff.
80
81 // Set some properties which depend soley on the input kind; it would be nice
82 // to move these to the language standard, and have the driver resolve the
83 // input kind + language standard.
Greg Clayton94e5d782010-06-13 17:34:29 +000084 if (IK == IK_Asm) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085 Opts.AsmPreprocessor = 1;
Greg Clayton94e5d782010-06-13 17:34:29 +000086 } else if (IK == IK_ObjC ||
87 IK == IK_ObjCXX ||
88 IK == IK_PreprocessedObjC ||
89 IK == IK_PreprocessedObjCXX) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000090 Opts.ObjC1 = Opts.ObjC2 = 1;
91 }
92
93 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
94
95 if (LangStd == LangStandard::lang_unspecified) {
96 // Based on the base language, pick one.
97 switch (IK) {
Greg Clayton94e5d782010-06-13 17:34:29 +000098 case IK_None:
99 case IK_AST:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100 assert(0 && "Invalid input kind!");
Greg Clayton94e5d782010-06-13 17:34:29 +0000101 case IK_OpenCL:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102 LangStd = LangStandard::lang_opencl;
103 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000104 case IK_Asm:
105 case IK_C:
106 case IK_PreprocessedC:
107 case IK_ObjC:
108 case IK_PreprocessedObjC:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109 LangStd = LangStandard::lang_gnu99;
110 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000111 case IK_CXX:
112 case IK_PreprocessedCXX:
113 case IK_ObjCXX:
114 case IK_PreprocessedObjCXX:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115 LangStd = LangStandard::lang_gnucxx98;
116 break;
117 }
118 }
119
120 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
121 Opts.BCPLComment = Std.hasBCPLComments();
122 Opts.C99 = Std.isC99();
123 Opts.CPlusPlus = Std.isCPlusPlus();
124 Opts.CPlusPlus0x = Std.isCPlusPlus0x();
125 Opts.Digraphs = Std.hasDigraphs();
126 Opts.GNUMode = Std.isGNUMode();
127 Opts.GNUInline = !Std.isC99();
128 Opts.HexFloats = Std.hasHexFloats();
129 Opts.ImplicitInt = Std.hasImplicitInt();
130
131 // OpenCL has some additional defaults.
132 if (LangStd == LangStandard::lang_opencl) {
133 Opts.OpenCL = 1;
134 Opts.AltiVec = 1;
135 Opts.CXXOperatorNames = 1;
136 Opts.LaxVectorConversions = 1;
137 }
138
139 // OpenCL and C++ both have bool, true, false keywords.
140 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
141
142// if (Opts.CPlusPlus)
143// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
144//
145// if (Args.hasArg(OPT_fobjc_gc_only))
146// Opts.setGCMode(LangOptions::GCOnly);
147// else if (Args.hasArg(OPT_fobjc_gc))
148// Opts.setGCMode(LangOptions::HybridGC);
149//
150// if (Args.hasArg(OPT_print_ivar_layout))
151// Opts.ObjCGCBitmapPrint = 1;
152//
153// if (Args.hasArg(OPT_faltivec))
154// Opts.AltiVec = 1;
155//
156// if (Args.hasArg(OPT_pthread))
157// Opts.POSIXThreads = 1;
158//
159// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
160// "default");
161// if (Vis == "default")
162 Opts.setVisibilityMode(LangOptions::Default);
163// else if (Vis == "hidden")
164// Opts.setVisibilityMode(LangOptions::Hidden);
165// else if (Vis == "protected")
166// Opts.setVisibilityMode(LangOptions::Protected);
167// else
168// Diags.Report(diag::err_drv_invalid_value)
169// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
170
171// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
172
173 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
174 // is specified, or -std is set to a conforming mode.
175 Opts.Trigraphs = !Opts.GNUMode;
176// if (Args.hasArg(OPT_trigraphs))
177// Opts.Trigraphs = 1;
178//
179// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
180// OPT_fno_dollars_in_identifiers,
181// !Opts.AsmPreprocessor);
182// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
183// Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
184// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
185// if (Args.hasArg(OPT_fno_lax_vector_conversions))
186// Opts.LaxVectorConversions = 0;
187// Opts.Exceptions = Args.hasArg(OPT_fexceptions);
188// Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
189// Opts.Blocks = Args.hasArg(OPT_fblocks);
190// Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char);
191// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
192// Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
193// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
194// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
195// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
196// Opts.AccessControl = Args.hasArg(OPT_faccess_control);
197// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
198// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
199// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
200// Diags);
201// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
202// Opts.ObjCConstantStringClass = getLastArgValue(Args,
203// OPT_fconstant_string_class);
204// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
205// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
206// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
207// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
208// Opts.Static = Args.hasArg(OPT_static_define);
209 Opts.OptimizeSize = 0;
210
211 // FIXME: Eliminate this dependency.
212// unsigned Opt =
213// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
214// Opts.Optimize = Opt != 0;
215 unsigned Opt = 0;
216
217 // This is the __NO_INLINE__ define, which just depends on things like the
218 // optimization level and -fno-inline, not actually whether the backend has
219 // inlining enabled.
220 //
221 // FIXME: This is affected by other options (-fno-inline).
222 Opts.NoInline = !Opt;
223
224// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
225// switch (SSP) {
226// default:
227// Diags.Report(diag::err_drv_invalid_value)
228// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
229// break;
230// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
231// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
232// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
233// }
234}
235
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237ClangASTContext::ClangASTContext(const char *target_triple) :
238 m_target_triple(),
239 m_ast_context_ap(),
240 m_language_options_ap(),
241 m_source_manager_ap(),
242 m_diagnostic_ap(),
243 m_target_options_ap(),
244 m_target_info_ap(),
245 m_identifier_table_ap(),
246 m_selector_table_ap(),
247 m_builtins_ap()
248{
249 if (target_triple && target_triple[0])
250 m_target_triple.assign (target_triple);
251}
252
253//----------------------------------------------------------------------
254// Destructor
255//----------------------------------------------------------------------
256ClangASTContext::~ClangASTContext()
257{
258 m_builtins_ap.reset();
259 m_selector_table_ap.reset();
260 m_identifier_table_ap.reset();
261 m_target_info_ap.reset();
262 m_target_options_ap.reset();
263 m_diagnostic_ap.reset();
264 m_source_manager_ap.reset();
265 m_language_options_ap.reset();
266 m_ast_context_ap.reset();
267}
268
269
270void
271ClangASTContext::Clear()
272{
273 m_ast_context_ap.reset();
274 m_language_options_ap.reset();
275 m_source_manager_ap.reset();
276 m_diagnostic_ap.reset();
277 m_target_options_ap.reset();
278 m_target_info_ap.reset();
279 m_identifier_table_ap.reset();
280 m_selector_table_ap.reset();
281 m_builtins_ap.reset();
282}
283
284const char *
285ClangASTContext::GetTargetTriple ()
286{
287 return m_target_triple.c_str();
288}
289
290void
291ClangASTContext::SetTargetTriple (const char *target_triple)
292{
293 Clear();
294 m_target_triple.assign(target_triple);
295}
296
297
298ASTContext *
299ClangASTContext::getASTContext()
300{
301 if (m_ast_context_ap.get() == NULL)
302 {
303 m_ast_context_ap.reset(
304 new ASTContext(
305 *getLanguageOptions(),
306 *getSourceManager(),
307 *getTargetInfo(),
308 *getIdentifierTable(),
309 *getSelectorTable(),
Greg Claytone6371122010-07-30 20:30:44 +0000310 *getBuiltinContext(),
311 0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312 }
313 return m_ast_context_ap.get();
314}
315
316Builtin::Context *
317ClangASTContext::getBuiltinContext()
318{
319 if (m_builtins_ap.get() == NULL)
320 m_builtins_ap.reset (new Builtin::Context(*getTargetInfo()));
321 return m_builtins_ap.get();
322}
323
324IdentifierTable *
325ClangASTContext::getIdentifierTable()
326{
327 if (m_identifier_table_ap.get() == NULL)
328 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), NULL));
329 return m_identifier_table_ap.get();
330}
331
332LangOptions *
333ClangASTContext::getLanguageOptions()
334{
335 if (m_language_options_ap.get() == NULL)
336 {
337 m_language_options_ap.reset(new LangOptions());
Greg Clayton94e5d782010-06-13 17:34:29 +0000338 ParseLangArgs(*m_language_options_ap, IK_ObjCXX);
339// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340 }
341 return m_language_options_ap.get();
342}
343
344SelectorTable *
345ClangASTContext::getSelectorTable()
346{
347 if (m_selector_table_ap.get() == NULL)
348 m_selector_table_ap.reset (new SelectorTable());
349 return m_selector_table_ap.get();
350}
351
Greg Claytone1a916a2010-07-21 22:12:05 +0000352clang::SourceManager *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353ClangASTContext::getSourceManager()
354{
355 if (m_source_manager_ap.get() == NULL)
Greg Claytone1a916a2010-07-21 22:12:05 +0000356 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnostic()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357 return m_source_manager_ap.get();
358}
359
360Diagnostic *
361ClangASTContext::getDiagnostic()
362{
363 if (m_diagnostic_ap.get() == NULL)
364 m_diagnostic_ap.reset(new Diagnostic());
365 return m_diagnostic_ap.get();
366}
367
368TargetOptions *
369ClangASTContext::getTargetOptions()
370{
371 if (m_target_options_ap.get() == NULL && !m_target_triple.empty())
372 {
373 m_target_options_ap.reset (new TargetOptions());
374 if (m_target_options_ap.get())
375 m_target_options_ap->Triple = m_target_triple;
376 }
377 return m_target_options_ap.get();
378}
379
380
381TargetInfo *
382ClangASTContext::getTargetInfo()
383{
384 // target_triple should be something like "x86_64-apple-darwin10"
385 if (m_target_info_ap.get() == NULL && !m_target_triple.empty())
386 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnostic(), *getTargetOptions()));
387 return m_target_info_ap.get();
388}
389
390#pragma mark Basic Types
391
392static inline bool
393QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast_context, QualType qual_type)
394{
395 uint64_t qual_type_bit_size = ast_context->getTypeSize(qual_type);
396 if (qual_type_bit_size == bit_size)
397 return true;
398 return false;
399}
400
401void *
Greg Claytonc86103d2010-08-05 01:57:25 +0000402ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403{
404 ASTContext *ast_context = getASTContext();
405
406 assert (ast_context != NULL);
407
408 return GetBuiltinTypeForEncodingAndBitSize (ast_context, encoding, bit_size);
409}
410
411void *
Greg Claytonc86103d2010-08-05 01:57:25 +0000412ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (clang::ASTContext *ast_context, Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413{
414 if (!ast_context)
415 return NULL;
416
417 switch (encoding)
418 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000419 case eEncodingInvalid:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->VoidPtrTy))
421 return ast_context->VoidPtrTy.getAsOpaquePtr();
422 break;
423
Greg Claytonc86103d2010-08-05 01:57:25 +0000424 case eEncodingUint:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy))
426 return ast_context->UnsignedCharTy.getAsOpaquePtr();
427 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedShortTy))
428 return ast_context->UnsignedShortTy.getAsOpaquePtr();
429 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedIntTy))
430 return ast_context->UnsignedIntTy.getAsOpaquePtr();
431 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongTy))
432 return ast_context->UnsignedLongTy.getAsOpaquePtr();
433 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongLongTy))
434 return ast_context->UnsignedLongLongTy.getAsOpaquePtr();
435 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedInt128Ty))
436 return ast_context->UnsignedInt128Ty.getAsOpaquePtr();
437 break;
438
Greg Claytonc86103d2010-08-05 01:57:25 +0000439 case eEncodingSint:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->CharTy))
441 return ast_context->CharTy.getAsOpaquePtr();
442 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->ShortTy))
443 return ast_context->ShortTy.getAsOpaquePtr();
444 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->IntTy))
445 return ast_context->IntTy.getAsOpaquePtr();
446 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongTy))
447 return ast_context->LongTy.getAsOpaquePtr();
448 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongLongTy))
449 return ast_context->LongLongTy.getAsOpaquePtr();
450 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->Int128Ty))
451 return ast_context->Int128Ty.getAsOpaquePtr();
452 break;
453
Greg Claytonc86103d2010-08-05 01:57:25 +0000454 case eEncodingIEEE754:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->FloatTy))
456 return ast_context->FloatTy.getAsOpaquePtr();
457 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->DoubleTy))
458 return ast_context->DoubleTy.getAsOpaquePtr();
459 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongDoubleTy))
460 return ast_context->LongDoubleTy.getAsOpaquePtr();
461 break;
462
Greg Claytonc86103d2010-08-05 01:57:25 +0000463 case eEncodingVector:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464 default:
465 break;
466 }
467
468 return NULL;
469}
470
471void *
472ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
473{
474 ASTContext *ast_context = getASTContext();
475
476 #define streq(a,b) strcmp(a,b) == 0
477 assert (ast_context != NULL);
478 if (ast_context)
479 {
480 switch (dw_ate)
481 {
482 default:
483 break;
484
485 case DW_ATE_address:
486 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->VoidPtrTy))
487 return ast_context->VoidPtrTy.getAsOpaquePtr();
488 break;
489
490 case DW_ATE_boolean:
491 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->BoolTy))
492 return ast_context->BoolTy.getAsOpaquePtr();
493 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy))
494 return ast_context->UnsignedCharTy.getAsOpaquePtr();
495 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedShortTy))
496 return ast_context->UnsignedShortTy.getAsOpaquePtr();
497 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedIntTy))
498 return ast_context->UnsignedIntTy.getAsOpaquePtr();
499 break;
500
501 case DW_ATE_complex_float:
502 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->FloatComplexTy))
503 return ast_context->FloatComplexTy.getAsOpaquePtr();
504 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->DoubleComplexTy))
505 return ast_context->DoubleComplexTy.getAsOpaquePtr();
506 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongDoubleComplexTy))
507 return ast_context->LongDoubleComplexTy.getAsOpaquePtr();
508 break;
509
510 case DW_ATE_float:
511 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->FloatTy))
512 return ast_context->FloatTy.getAsOpaquePtr();
513 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->DoubleTy))
514 return ast_context->DoubleTy.getAsOpaquePtr();
515 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongDoubleTy))
516 return ast_context->LongDoubleTy.getAsOpaquePtr();
517 break;
518
519 case DW_ATE_signed:
520 if (type_name)
521 {
522 if (streq(type_name, "int") ||
523 streq(type_name, "signed int"))
524 {
525 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->IntTy))
526 return ast_context->IntTy.getAsOpaquePtr();
527 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->Int128Ty))
528 return ast_context->Int128Ty.getAsOpaquePtr();
529 }
530
531 if (streq(type_name, "long int") ||
532 streq(type_name, "long long int") ||
533 streq(type_name, "signed long long"))
534 {
535 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongTy))
536 return ast_context->LongTy.getAsOpaquePtr();
537 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongLongTy))
538 return ast_context->LongLongTy.getAsOpaquePtr();
539 }
540
541 if (streq(type_name, "short") ||
542 streq(type_name, "short int") ||
543 streq(type_name, "signed short") ||
544 streq(type_name, "short signed int"))
545 {
546 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->ShortTy))
547 return ast_context->ShortTy.getAsOpaquePtr();
548 }
549
550 if (streq(type_name, "char") ||
551 streq(type_name, "signed char"))
552 {
553 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->CharTy))
554 return ast_context->CharTy.getAsOpaquePtr();
555 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->SignedCharTy))
556 return ast_context->SignedCharTy.getAsOpaquePtr();
557 }
558
559 if (streq(type_name, "wchar_t"))
560 {
561 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->WCharTy))
562 return ast_context->WCharTy.getAsOpaquePtr();
563 }
564
565 }
566 // We weren't able to match up a type name, just search by size
567 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->CharTy))
568 return ast_context->CharTy.getAsOpaquePtr();
569 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->ShortTy))
570 return ast_context->ShortTy.getAsOpaquePtr();
571 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->IntTy))
572 return ast_context->IntTy.getAsOpaquePtr();
573 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongTy))
574 return ast_context->LongTy.getAsOpaquePtr();
575 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongLongTy))
576 return ast_context->LongLongTy.getAsOpaquePtr();
577 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->Int128Ty))
578 return ast_context->Int128Ty.getAsOpaquePtr();
579 break;
580
581 case DW_ATE_signed_char:
582 if (type_name)
583 {
584 if (streq(type_name, "signed char"))
585 {
586 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->SignedCharTy))
587 return ast_context->SignedCharTy.getAsOpaquePtr();
588 }
589 }
590 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->CharTy))
591 return ast_context->CharTy.getAsOpaquePtr();
592 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->SignedCharTy))
593 return ast_context->SignedCharTy.getAsOpaquePtr();
594 break;
595
596 case DW_ATE_unsigned:
597 if (type_name)
598 {
599 if (streq(type_name, "unsigned int"))
600 {
601 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedIntTy))
602 return ast_context->UnsignedIntTy.getAsOpaquePtr();
603 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedInt128Ty))
604 return ast_context->UnsignedInt128Ty.getAsOpaquePtr();
605 }
606
607 if (streq(type_name, "unsigned int") ||
608 streq(type_name, "long unsigned int") ||
609 streq(type_name, "unsigned long long"))
610 {
611 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongTy))
612 return ast_context->UnsignedLongTy.getAsOpaquePtr();
613 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongLongTy))
614 return ast_context->UnsignedLongLongTy.getAsOpaquePtr();
615 }
616
617 if (streq(type_name, "unsigned short") ||
618 streq(type_name, "short unsigned int"))
619 {
620 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedShortTy))
621 return ast_context->UnsignedShortTy.getAsOpaquePtr();
622 }
623 if (streq(type_name, "unsigned char"))
624 {
625 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy))
626 return ast_context->UnsignedCharTy.getAsOpaquePtr();
627 }
628
629 }
630 // We weren't able to match up a type name, just search by size
631 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy))
632 return ast_context->UnsignedCharTy.getAsOpaquePtr();
633 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedShortTy))
634 return ast_context->UnsignedShortTy.getAsOpaquePtr();
635 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedIntTy))
636 return ast_context->UnsignedIntTy.getAsOpaquePtr();
637 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongTy))
638 return ast_context->UnsignedLongTy.getAsOpaquePtr();
639 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongLongTy))
640 return ast_context->UnsignedLongLongTy.getAsOpaquePtr();
641 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedInt128Ty))
642 return ast_context->UnsignedInt128Ty.getAsOpaquePtr();
643 break;
644
645 case DW_ATE_unsigned_char:
646 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy))
647 return ast_context->UnsignedCharTy.getAsOpaquePtr();
648 break;
649
650 case DW_ATE_imaginary_float:
651 break;
652 }
653 }
654 // This assert should fire for anything that we don't catch above so we know
655 // to fix any issues we run into.
656 assert (!"error: ClangASTContext::GetClangTypeForDWARFEncodingAndSize() contains an unhandled encoding. Fix this ASAP!");
657 return NULL;
658}
659
660void *
Sean Callanan6fe64b52010-09-17 02:24:29 +0000661ClangASTContext::GetBuiltInType_void(clang::ASTContext *ast_context)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662{
Sean Callanan6fe64b52010-09-17 02:24:29 +0000663 return ast_context->VoidTy.getAsOpaquePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664}
665
666void *
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000667ClangASTContext::GetBuiltInType_objc_id()
668{
669 return getASTContext()->getObjCIdType().getAsOpaquePtr();
670}
671
672void *
673ClangASTContext::GetBuiltInType_objc_Class()
674{
675 return getASTContext()->getObjCClassType().getAsOpaquePtr();
676}
677
678void *
679ClangASTContext::GetBuiltInType_objc_selector()
680{
681 return getASTContext()->getObjCSelType().getAsOpaquePtr();
682}
683
684void *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685ClangASTContext::GetCStringType (bool is_const)
686{
687 QualType char_type(getASTContext()->CharTy);
688
689 if (is_const)
690 char_type.addConst();
691
692 return getASTContext()->getPointerType(char_type).getAsOpaquePtr();
693}
694
695void *
696ClangASTContext::GetVoidPtrType (bool is_const)
697{
698 return GetVoidPtrType(getASTContext(), is_const);
699}
700
701void *
702ClangASTContext::GetVoidPtrType (clang::ASTContext *ast_context, bool is_const)
703{
704 QualType void_ptr_type(ast_context->VoidPtrTy);
705
706 if (is_const)
707 void_ptr_type.addConst();
708
709 return void_ptr_type.getAsOpaquePtr();
710}
711
712void *
713ClangASTContext::CopyType(clang::ASTContext *dest_context,
714 clang::ASTContext *source_context,
Greg Clayton8cf05932010-07-22 18:30:50 +0000715 void *clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716{
717 Diagnostic diagnostics;
718 FileManager file_manager;
719 ASTImporter importer(diagnostics,
720 *dest_context, file_manager,
721 *source_context, file_manager);
722 QualType ret = importer.Import(QualType::getFromOpaquePtr(clang_type));
723 return ret.getAsOpaquePtr();
724}
725
Sean Callanan23a30272010-07-16 00:00:27 +0000726bool
727ClangASTContext::AreTypesSame(clang::ASTContext *ast_context,
Sean Callanan4dcca2622010-07-15 22:30:52 +0000728 void *type1,
729 void *type2)
730{
731 return ast_context->hasSameType(QualType::getFromOpaquePtr(type1),
732 QualType::getFromOpaquePtr(type2));
733}
734
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735#pragma mark CVR modifiers
736
737void *
738ClangASTContext::AddConstModifier (void *clang_type)
739{
740 if (clang_type)
741 {
742 QualType result(QualType::getFromOpaquePtr(clang_type));
743 result.addConst();
744 return result.getAsOpaquePtr();
745 }
746 return NULL;
747}
748
749void *
750ClangASTContext::AddRestrictModifier (void *clang_type)
751{
752 if (clang_type)
753 {
754 QualType result(QualType::getFromOpaquePtr(clang_type));
755 result.getQualifiers().setRestrict (true);
756 return result.getAsOpaquePtr();
757 }
758 return NULL;
759}
760
761void *
762ClangASTContext::AddVolatileModifier (void *clang_type)
763{
764 if (clang_type)
765 {
766 QualType result(QualType::getFromOpaquePtr(clang_type));
767 result.getQualifiers().setVolatile (true);
768 return result.getAsOpaquePtr();
769 }
770 return NULL;
771}
772
773#pragma mark Structure, Unions, Classes
774
775void *
Greg Claytonc86103d2010-08-05 01:57:25 +0000776ClangASTContext::CreateRecordType (const char *name, int kind, DeclContext *decl_ctx, LanguageType language)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777{
778 ASTContext *ast_context = getASTContext();
779 assert (ast_context != NULL);
780
781 if (decl_ctx == NULL)
782 decl_ctx = ast_context->getTranslationUnitDecl();
783
Greg Clayton9e409562010-07-28 02:04:09 +0000784
Greg Claytonc86103d2010-08-05 01:57:25 +0000785 if (language == eLanguageTypeObjC)
Greg Clayton9e409562010-07-28 02:04:09 +0000786 {
787 bool isForwardDecl = false;
788 bool isInternal = false;
789 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal);
790 }
791
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000792 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
793 // we will need to update this code. I was told to currently always use
794 // the CXXRecordDecl class since we often don't know from debug information
795 // if something is struct or a class, so we default to always use the more
796 // complete definition just in case.
797 CXXRecordDecl *decl = CXXRecordDecl::Create(*ast_context,
798 (TagDecl::TagKind)kind,
799 decl_ctx,
800 SourceLocation(),
801 name && name[0] ? &ast_context->Idents.get(name) : NULL);
802
803 return ast_context->getTagDeclType(decl).getAsOpaquePtr();
804}
805
806bool
Sean Callanan61da09b2010-09-17 02:58:26 +0000807ClangASTContext::AddMethodToCXXRecordType
808(
809 clang::ASTContext *ast_context,
Sean Callananfc55f5d2010-09-21 00:44:12 +0000810 void *record_opaque_type,
Sean Callanan61da09b2010-09-17 02:58:26 +0000811 const char *name,
Sean Callananfc55f5d2010-09-21 00:44:12 +0000812 void *method_opaque_type
Sean Callanan61da09b2010-09-17 02:58:26 +0000813 )
814{
Sean Callananfc55f5d2010-09-21 00:44:12 +0000815 if (!record_opaque_type || !method_opaque_type || !name)
Sean Callanan61da09b2010-09-17 02:58:26 +0000816 return false;
817
818 assert(ast_context);
819
820 IdentifierTable *identifier_table = &ast_context->Idents;
821
822 assert(identifier_table);
823
Sean Callananfc55f5d2010-09-21 00:44:12 +0000824 QualType record_qual_type(QualType::getFromOpaquePtr(record_opaque_type));
Sean Callanan61da09b2010-09-17 02:58:26 +0000825 clang::Type *record_type(record_qual_type.getTypePtr());
826
827 if (!record_type)
828 return false;
829
830 RecordType *record_recty(dyn_cast<RecordType>(record_type));
831
832 if (!record_recty)
833 return false;
834
835 RecordDecl *record_decl = record_recty->getDecl();
836
837 if (!record_decl)
838 return false;
839
840 CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
841
842 if (!cxx_record_decl)
843 return false;
844
Sean Callananfc55f5d2010-09-21 00:44:12 +0000845 QualType method_qual_type(QualType::getFromOpaquePtr(method_opaque_type));
846
Sean Callanan61da09b2010-09-17 02:58:26 +0000847 CXXMethodDecl *cxx_method_decl = CXXMethodDecl::Create(*ast_context,
848 cxx_record_decl,
849 SourceLocation(),
850 DeclarationName(&identifier_table->get(name)),
Sean Callananfc55f5d2010-09-21 00:44:12 +0000851 method_qual_type,
Sean Callanan61da09b2010-09-17 02:58:26 +0000852 NULL);
853
Sean Callananfc55f5d2010-09-21 00:44:12 +0000854 // Populate the method decl with parameter decls
855
856 clang::Type *method_type(method_qual_type.getTypePtr());
857
858 if (!method_type)
859 return false;
860
861 FunctionProtoType *method_funprototy(dyn_cast<FunctionProtoType>(method_type));
862
863 if (!method_funprototy)
864 return false;
865
866 unsigned int num_params = method_funprototy->getNumArgs();
867
868 ParmVarDecl *params[num_params];
869
870 for (int param_index = 0;
871 param_index < num_params;
872 ++param_index)
873 {
874 params[param_index] = ParmVarDecl::Create(*ast_context,
875 cxx_method_decl,
876 SourceLocation(),
877 NULL, // anonymous
878 method_funprototy->getArgType(param_index),
879 NULL,
880 VarDecl::Auto,
881 VarDecl::Auto,
882 NULL);
883 }
884
885 cxx_method_decl->setParams(params, num_params);
886
Sean Callanan61da09b2010-09-17 02:58:26 +0000887 cxx_record_decl->addDecl(cxx_method_decl);
888
889 return true;
890}
891
892bool
Greg Clayton8cf05932010-07-22 18:30:50 +0000893ClangASTContext::AddFieldToRecordType
894(
Sean Callanan6e6a7c72010-09-16 20:01:08 +0000895 clang::ASTContext *ast_context,
Greg Clayton8cf05932010-07-22 18:30:50 +0000896 void *record_clang_type,
897 const char *name,
898 void *field_type,
899 AccessType access,
900 uint32_t bitfield_bit_size
901)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000902{
903 if (record_clang_type == NULL || field_type == NULL)
904 return false;
905
Sean Callanan6e6a7c72010-09-16 20:01:08 +0000906 IdentifierTable *identifier_table = &ast_context->Idents;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000907
908 assert (ast_context != NULL);
909 assert (identifier_table != NULL);
910
911 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
912
Greg Claytone1a916a2010-07-21 22:12:05 +0000913 clang::Type *clang_type = record_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000914 if (clang_type)
915 {
916 const RecordType *record_type = dyn_cast<RecordType>(clang_type);
917
918 if (record_type)
919 {
920 RecordDecl *record_decl = record_type->getDecl();
921
922 CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
923 if (cxx_record_decl)
Greg Clayton4f432e72010-09-22 00:40:49 +0000924 {
925 // NOTE: we currently have some fixes that should be placed
926 // into clang that will automatically set if a record is empty
927 // when each field is added (during the addDecl() method call
928 // below) so this code should be able to come out when those
929 // changes make it into llvm/clang, then we can remove this
930 // code...
931 // Currently SEMA is using the accessors manually to set
932 // whether a class is empty, is POD, is aggregate, and more.
933 // This code will be moved into CXXRecordDecl so everyone
934 // can benefit.
935 // This will currently work for everything except zero sized
936 // bitfields which we currently aren't detecting anyway from the
937 // DWARF so it should be ok for now.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000938 cxx_record_decl->setEmpty (false);
Greg Clayton4f432e72010-09-22 00:40:49 +0000939 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000940
941 clang::Expr *bit_width = NULL;
942 if (bitfield_bit_size != 0)
943 {
944 APInt bitfield_bit_size_apint(ast_context->getTypeSize(ast_context->IntTy), bitfield_bit_size);
945 bit_width = new (*ast_context)IntegerLiteral (bitfield_bit_size_apint, ast_context->IntTy, SourceLocation());
946 }
Greg Clayton8cf05932010-07-22 18:30:50 +0000947 FieldDecl *field = FieldDecl::Create (*ast_context,
948 record_decl,
949 SourceLocation(),
950 name ? &identifier_table->get(name) : NULL, // Identifier
951 QualType::getFromOpaquePtr(field_type), // Field type
952 NULL, // DeclaratorInfo *
953 bit_width, // BitWidth
954 false); // Mutable
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000955
Greg Clayton8cf05932010-07-22 18:30:50 +0000956 field->setAccess (ConvertAccessTypeToAccessSpecifier (access));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000957
958 if (field)
959 {
960 record_decl->addDecl(field);
Greg Clayton4f432e72010-09-22 00:40:49 +0000961
962 // NOTE: we currently have some fixes that should be placed
963 // into clang that will automatically set if a record is POD
964 // when each field is added (during the addDecl() method call
965 // above) so this code should be able to come out when those
966 // changes make it into llvm/clang, then we can remove this
967 // code...
968 // Currently SEMA is using the accessors manually to set
969 // whether a class is empty, is POD, is aggregate, and more.
970 // This code will be moved into CXXRecordDecl so everyone
971 // can benefit.
972
Greg Clayton60e1aa52010-09-22 00:24:45 +0000973 if (cxx_record_decl->isPOD())
974 {
975 if (!field->getType()->isPODType())
976 cxx_record_decl->setPOD (false);
977 return true;
978 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000979 }
980 }
Greg Clayton9e409562010-07-28 02:04:09 +0000981 else
982 {
983 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(clang_type);
984 if (objc_class_type)
985 {
986 bool isSynthesized = false;
Sean Callanan6e6a7c72010-09-16 20:01:08 +0000987 ClangASTContext::AddObjCClassIVar (ast_context,
988 record_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +0000989 name,
990 field_type,
991 access,
992 bitfield_bit_size,
993 isSynthesized);
994 }
995 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996 }
997 return false;
998}
999
1000bool
1001ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
1002{
1003 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1004}
1005
1006bool
1007ClangASTContext::FieldIsBitfield
1008(
1009 ASTContext *ast_context,
1010 FieldDecl* field,
1011 uint32_t& bitfield_bit_size
1012)
1013{
1014 if (ast_context == NULL || field == NULL)
1015 return false;
1016
1017 if (field->isBitField())
1018 {
1019 Expr* bit_width_expr = field->getBitWidth();
1020 if (bit_width_expr)
1021 {
1022 llvm::APSInt bit_width_apsint;
1023 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast_context))
1024 {
1025 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
1026 return true;
1027 }
1028 }
1029 }
1030 return false;
1031}
1032
1033bool
1034ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
1035{
1036 if (record_decl == NULL)
1037 return false;
1038
1039 if (!record_decl->field_empty())
1040 return true;
1041
1042 // No fields, lets check this is a CXX record and check the base classes
1043 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1044 if (cxx_record_decl)
1045 {
1046 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1047 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1048 base_class != base_class_end;
1049 ++base_class)
1050 {
1051 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1052 if (RecordHasFields(base_class_decl))
1053 return true;
1054 }
1055 }
1056 return false;
1057}
1058
1059void
1060ClangASTContext::SetDefaultAccessForRecordFields (void *clang_qual_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities)
1061{
1062 if (clang_qual_type)
1063 {
1064 QualType qual_type(QualType::getFromOpaquePtr(clang_qual_type));
Greg Claytone1a916a2010-07-21 22:12:05 +00001065 clang::Type *clang_type = qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001066 if (clang_type)
1067 {
1068 RecordType *record_type = dyn_cast<RecordType>(clang_type);
1069 if (record_type)
1070 {
1071 RecordDecl *record_decl = record_type->getDecl();
1072 if (record_decl)
1073 {
1074 uint32_t field_idx;
1075 RecordDecl::field_iterator field, field_end;
1076 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
1077 field != field_end;
1078 ++field, ++field_idx)
1079 {
1080 // If no accessibility was assigned, assign the correct one
1081 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
1082 field->setAccess ((AccessSpecifier)default_accessibility);
1083 }
1084 }
1085 }
1086 }
1087 }
1088}
1089
1090#pragma mark C++ Base Classes
1091
1092CXXBaseSpecifier *
Greg Clayton8cf05932010-07-22 18:30:50 +00001093ClangASTContext::CreateBaseClassSpecifier (void *base_class_type, AccessType access, bool is_virtual, bool base_of_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094{
1095 if (base_class_type)
Greg Claytone6371122010-07-30 20:30:44 +00001096 return new CXXBaseSpecifier (SourceRange(),
1097 is_virtual,
1098 base_of_class,
1099 ConvertAccessTypeToAccessSpecifier (access),
1100 getASTContext()->CreateTypeSourceInfo (QualType::getFromOpaquePtr(base_class_type)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001101 return NULL;
1102}
1103
Greg Clayton0b42ac32010-07-02 01:29:13 +00001104void
1105ClangASTContext::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes)
1106{
1107 for (unsigned i=0; i<num_base_classes; ++i)
1108 {
1109 delete base_classes[i];
1110 base_classes[i] = NULL;
1111 }
1112}
1113
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001114bool
1115ClangASTContext::SetBaseClassesForClassType (void *class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes)
1116{
1117 if (class_clang_type)
1118 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001119 clang::Type *clang_type = QualType::getFromOpaquePtr(class_clang_type).getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001120 if (clang_type)
1121 {
1122 RecordType *record_type = dyn_cast<RecordType>(clang_type);
1123 if (record_type)
1124 {
1125 CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_type->getDecl());
1126 if (cxx_record_decl)
1127 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001128 cxx_record_decl->setBases(base_classes, num_base_classes);
Greg Clayton9e826e32010-09-21 21:22:23 +00001129
Greg Clayton4f432e72010-09-22 00:40:49 +00001130 // NOTE: we currently have some fixes that should be placed
1131 // into clang that will automatically set these things when
1132 // they are added (during the setBases() method call above)
1133 // so this code should be able to come out when those changes
1134 // make it into llvm/clang, then we can remove this code...
1135 // Currently SEMA is using the accessors manually to set
1136 // whether a class is empty, is POD, is aggregate, and more.
1137 // This code will be moved into CXXRecordDecl so everyone
1138 // can benefit.
Greg Clayton60e1aa52010-09-22 00:24:45 +00001139 if (cxx_record_decl->isEmpty() || cxx_record_decl->isPOD())
Greg Clayton9e826e32010-09-21 21:22:23 +00001140 {
1141 // set empty to false if any bases are virtual, or not empty.
1142
1143 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1144 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1145 base_class != base_class_end;
1146 ++base_class)
1147 {
1148 if (base_class->isVirtual())
1149 {
1150 cxx_record_decl->setEmpty (false);
Greg Clayton60e1aa52010-09-22 00:24:45 +00001151 cxx_record_decl->setPOD (false);
Greg Clayton9e826e32010-09-21 21:22:23 +00001152 break;
1153 }
1154 else
1155 {
Greg Clayton60e1aa52010-09-22 00:24:45 +00001156 QualType base_type (base_class->getType());
1157
1158 if (!base_type->isPODType())
1159 cxx_record_decl->setPOD (false);
1160
1161 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_type->getAs<RecordType>()->getDecl());
1162 if (!base_class_decl->isEmpty())
1163 {
Greg Clayton9e826e32010-09-21 21:22:23 +00001164 cxx_record_decl->setEmpty (false);
1165 break;
Greg Clayton60e1aa52010-09-22 00:24:45 +00001166 }
Greg Clayton9e826e32010-09-21 21:22:23 +00001167 }
1168 }
1169 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001170 return true;
1171 }
1172 }
1173 }
1174 }
1175 return false;
1176}
Greg Clayton8cf05932010-07-22 18:30:50 +00001177#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001178
Greg Clayton8cf05932010-07-22 18:30:50 +00001179void *
1180ClangASTContext::CreateObjCClass
1181(
1182 const char *name,
1183 DeclContext *decl_ctx,
1184 bool isForwardDecl,
1185 bool isInternal
1186)
1187{
1188 ASTContext *ast_context = getASTContext();
1189 assert (ast_context != NULL);
1190 assert (name && name[0]);
1191 if (decl_ctx == NULL)
1192 decl_ctx = ast_context->getTranslationUnitDecl();
1193
1194 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1195 // we will need to update this code. I was told to currently always use
1196 // the CXXRecordDecl class since we often don't know from debug information
1197 // if something is struct or a class, so we default to always use the more
1198 // complete definition just in case.
1199 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast_context,
1200 decl_ctx,
1201 SourceLocation(),
1202 &ast_context->Idents.get(name),
1203 SourceLocation(),
1204 isForwardDecl,
1205 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00001206
1207 return ast_context->getObjCInterfaceType(decl).getAsOpaquePtr();
Greg Clayton8cf05932010-07-22 18:30:50 +00001208}
1209
1210bool
1211ClangASTContext::SetObjCSuperClass (void *class_opaque_type, void *super_opaque_type)
1212{
1213 if (class_opaque_type && super_opaque_type)
1214 {
1215 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
1216 QualType super_qual_type(QualType::getFromOpaquePtr(super_opaque_type));
1217 clang::Type *class_type = class_qual_type.getTypePtr();
1218 clang::Type *super_type = super_qual_type.getTypePtr();
1219 if (class_type && super_type)
1220 {
1221 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
1222 ObjCObjectType *objc_super_type = dyn_cast<ObjCObjectType>(super_type);
1223 if (objc_class_type && objc_super_type)
1224 {
1225 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1226 ObjCInterfaceDecl *super_interface_decl = objc_super_type->getInterface();
1227 if (class_interface_decl && super_interface_decl)
1228 {
1229 class_interface_decl->setSuperClass(super_interface_decl);
1230 return true;
1231 }
1232 }
1233 }
1234 }
1235 return false;
1236}
1237
1238
1239bool
1240ClangASTContext::AddObjCClassIVar
1241(
Sean Callanan6e6a7c72010-09-16 20:01:08 +00001242 clang::ASTContext *ast_context,
Greg Clayton8cf05932010-07-22 18:30:50 +00001243 void *class_opaque_type,
1244 const char *name,
1245 void *ivar_opaque_type,
1246 AccessType access,
1247 uint32_t bitfield_bit_size,
1248 bool isSynthesized
1249)
1250{
1251 if (class_opaque_type == NULL || ivar_opaque_type == NULL)
1252 return false;
1253
Sean Callanan6e6a7c72010-09-16 20:01:08 +00001254 IdentifierTable *identifier_table = &ast_context->Idents;
Greg Clayton8cf05932010-07-22 18:30:50 +00001255
1256 assert (ast_context != NULL);
1257 assert (identifier_table != NULL);
1258
1259 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
1260
1261 clang::Type *class_type = class_qual_type.getTypePtr();
1262 if (class_type)
1263 {
1264 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
1265
1266 if (objc_class_type)
1267 {
1268 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1269
1270 if (class_interface_decl)
1271 {
1272 clang::Expr *bit_width = NULL;
1273 if (bitfield_bit_size != 0)
1274 {
1275 APInt bitfield_bit_size_apint(ast_context->getTypeSize(ast_context->IntTy), bitfield_bit_size);
1276 bit_width = new (*ast_context)IntegerLiteral (bitfield_bit_size_apint, ast_context->IntTy, SourceLocation());
1277 }
1278
Greg Clayton9e409562010-07-28 02:04:09 +00001279 ObjCIvarDecl *field = ObjCIvarDecl::Create (*ast_context,
1280 class_interface_decl,
1281 SourceLocation(),
1282 &identifier_table->get(name), // Identifier
1283 QualType::getFromOpaquePtr(ivar_opaque_type), // Field type
1284 NULL, // TypeSourceInfo *
1285 ConvertAccessTypeToObjCIvarAccessControl (access),
1286 bit_width,
1287 isSynthesized);
1288
1289 if (field)
1290 {
1291 class_interface_decl->addDecl(field);
1292 return true;
1293 }
Greg Clayton8cf05932010-07-22 18:30:50 +00001294 }
1295 }
1296 }
1297 return false;
1298}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001299
Greg Clayton9e409562010-07-28 02:04:09 +00001300
1301bool
1302ClangASTContext::ObjCTypeHasIVars (void *class_opaque_type, bool check_superclass)
1303{
1304 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
1305
1306 clang::Type *class_type = class_qual_type.getTypePtr();
1307 if (class_type)
1308 {
1309 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
1310
1311 if (objc_class_type)
1312 return ObjCDeclHasIVars (objc_class_type->getInterface(), check_superclass);
1313 }
1314 return false;
1315}
1316
1317bool
1318ClangASTContext::ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
1319{
1320 while (class_interface_decl)
1321 {
1322 if (class_interface_decl->ivar_size() > 0)
1323 return true;
1324
1325 if (check_superclass)
1326 class_interface_decl = class_interface_decl->getSuperClass();
1327 else
1328 break;
1329 }
1330 return false;
1331}
1332
1333
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001334#pragma mark Aggregate Types
1335
1336bool
1337ClangASTContext::IsAggregateType (void *clang_type)
1338{
1339 if (clang_type == NULL)
1340 return false;
1341
1342 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
1343
1344 if (qual_type->isAggregateType ())
1345 return true;
1346
Greg Clayton737b9322010-09-13 03:32:57 +00001347 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
1348 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001349 {
Greg Claytone1a916a2010-07-21 22:12:05 +00001350 case clang::Type::IncompleteArray:
1351 case clang::Type::VariableArray:
1352 case clang::Type::ConstantArray:
1353 case clang::Type::ExtVector:
1354 case clang::Type::Vector:
1355 case clang::Type::Record:
Greg Clayton9e409562010-07-28 02:04:09 +00001356 case clang::Type::ObjCObject:
1357 case clang::Type::ObjCInterface:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358 return true;
1359
Greg Claytone1a916a2010-07-21 22:12:05 +00001360 case clang::Type::Typedef:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001361 return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr());
1362
1363 default:
1364 break;
1365 }
1366 // The clang type does have a value
1367 return false;
1368}
1369
1370uint32_t
1371ClangASTContext::GetNumChildren (void *clang_qual_type, bool omit_empty_base_classes)
1372{
1373 if (clang_qual_type == NULL)
1374 return 0;
1375
1376 uint32_t num_children = 0;
1377 QualType qual_type(QualType::getFromOpaquePtr(clang_qual_type));
Greg Clayton9e409562010-07-28 02:04:09 +00001378 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
1379 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001380 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001381 case clang::Type::Builtin:
1382 switch (cast<clang::BuiltinType>(qual_type)->getKind())
1383 {
1384 case clang::BuiltinType::ObjCId: // Child is Class
1385 case clang::BuiltinType::ObjCClass: // child is Class
1386 case clang::BuiltinType::ObjCSel: // child is const char *
1387 num_children = 1;
1388
1389 default:
1390 break;
1391 }
1392 break;
1393
Greg Claytone1a916a2010-07-21 22:12:05 +00001394 case clang::Type::Record:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001395 {
1396 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
1397 const RecordDecl *record_decl = record_type->getDecl();
1398 assert(record_decl);
1399 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1400 if (cxx_record_decl)
1401 {
1402 if (omit_empty_base_classes)
1403 {
1404 // Check each base classes to see if it or any of its
1405 // base classes contain any fields. This can help
1406 // limit the noise in variable views by not having to
1407 // show base classes that contain no members.
1408 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1409 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1410 base_class != base_class_end;
1411 ++base_class)
1412 {
1413 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1414
1415 // Skip empty base classes
1416 if (RecordHasFields(base_class_decl) == false)
1417 continue;
1418
1419 num_children++;
1420 }
1421 }
1422 else
1423 {
1424 // Include all base classes
1425 num_children += cxx_record_decl->getNumBases();
1426 }
1427
1428 }
1429 RecordDecl::field_iterator field, field_end;
1430 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
1431 ++num_children;
1432 }
1433 break;
1434
Greg Clayton9e409562010-07-28 02:04:09 +00001435 case clang::Type::ObjCObject:
1436 case clang::Type::ObjCInterface:
1437 {
1438 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
1439 assert (objc_class_type);
1440 if (objc_class_type)
1441 {
1442 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1443
1444 if (class_interface_decl)
1445 {
1446
1447 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
1448 if (superclass_interface_decl)
1449 {
1450 if (omit_empty_base_classes)
1451 {
1452 if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true))
1453 ++num_children;
1454 }
1455 else
1456 ++num_children;
1457 }
1458
1459 num_children += class_interface_decl->ivar_size();
1460 }
1461 }
1462 }
1463 break;
1464
1465 case clang::Type::ObjCObjectPointer:
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001466 {
1467 ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr());
1468 QualType pointee_type = pointer_type->getPointeeType();
1469 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (pointee_type.getAsOpaquePtr(),
1470 omit_empty_base_classes);
1471 // If this type points to a simple type, then it has 1 child
1472 if (num_pointee_children == 0)
1473 num_children = 1;
1474 else
1475 num_children = num_pointee_children;
1476 }
1477 break;
Greg Clayton9e409562010-07-28 02:04:09 +00001478
Greg Claytone1a916a2010-07-21 22:12:05 +00001479 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001480 num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
1481 break;
1482
Greg Claytone1a916a2010-07-21 22:12:05 +00001483 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001484 {
1485 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
1486 QualType pointee_type = pointer_type->getPointeeType();
Greg Clayton9e409562010-07-28 02:04:09 +00001487 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (pointee_type.getAsOpaquePtr(),
1488 omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001489 // If this type points to a simple type, then it has 1 child
1490 if (num_pointee_children == 0)
1491 num_children = 1;
1492 else
1493 num_children = num_pointee_children;
1494 }
1495 break;
1496
Greg Claytone1a916a2010-07-21 22:12:05 +00001497 case clang::Type::Typedef:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001498 num_children = ClangASTContext::GetNumChildren (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), omit_empty_base_classes);
1499 break;
1500
1501 default:
1502 break;
1503 }
1504 return num_children;
1505}
1506
1507
1508void *
1509ClangASTContext::GetChildClangTypeAtIndex
1510(
1511 const char *parent_name,
1512 void *parent_clang_type,
1513 uint32_t idx,
1514 bool transparent_pointers,
1515 bool omit_empty_base_classes,
1516 std::string& child_name,
1517 uint32_t &child_byte_size,
1518 int32_t &child_byte_offset,
1519 uint32_t &child_bitfield_bit_size,
1520 uint32_t &child_bitfield_bit_offset
1521)
1522{
1523 if (parent_clang_type)
1524
1525 return GetChildClangTypeAtIndex (getASTContext(),
1526 parent_name,
1527 parent_clang_type,
1528 idx,
1529 transparent_pointers,
1530 omit_empty_base_classes,
1531 child_name,
1532 child_byte_size,
1533 child_byte_offset,
1534 child_bitfield_bit_size,
1535 child_bitfield_bit_offset);
1536 return NULL;
1537}
1538
1539void *
1540ClangASTContext::GetChildClangTypeAtIndex
1541(
1542 ASTContext *ast_context,
1543 const char *parent_name,
1544 void *parent_clang_type,
1545 uint32_t idx,
1546 bool transparent_pointers,
1547 bool omit_empty_base_classes,
1548 std::string& child_name,
1549 uint32_t &child_byte_size,
1550 int32_t &child_byte_offset,
1551 uint32_t &child_bitfield_bit_size,
1552 uint32_t &child_bitfield_bit_offset
1553)
1554{
1555 if (parent_clang_type == NULL)
1556 return NULL;
1557
1558 if (idx < ClangASTContext::GetNumChildren (parent_clang_type, omit_empty_base_classes))
1559 {
1560 uint32_t bit_offset;
1561 child_bitfield_bit_size = 0;
1562 child_bitfield_bit_offset = 0;
1563 QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00001564 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
1565 switch (parent_type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001566 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001567 case clang::Type::Builtin:
1568 switch (cast<clang::BuiltinType>(parent_qual_type)->getKind())
1569 {
1570 case clang::BuiltinType::ObjCId:
1571 case clang::BuiltinType::ObjCClass:
1572 return ast_context->ObjCBuiltinClassTy.getAsOpaquePtr();
1573
1574 case clang::BuiltinType::ObjCSel:
1575 {
1576 QualType char_type(ast_context->CharTy);
1577 char_type.addConst();
1578 return ast_context->getPointerType(char_type).getAsOpaquePtr();
1579 }
1580 break;
1581
1582 default:
1583 break;
1584 }
1585 break;
1586
1587
Greg Claytone1a916a2010-07-21 22:12:05 +00001588 case clang::Type::Record:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001589 {
1590 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr());
1591 const RecordDecl *record_decl = record_type->getDecl();
1592 assert(record_decl);
1593 const ASTRecordLayout &record_layout = ast_context->getASTRecordLayout(record_decl);
1594 uint32_t child_idx = 0;
1595
1596 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1597 if (cxx_record_decl)
1598 {
1599 // We might have base classes to print out first
1600 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1601 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1602 base_class != base_class_end;
1603 ++base_class)
1604 {
1605 const CXXRecordDecl *base_class_decl = NULL;
1606
1607 // Skip empty base classes
1608 if (omit_empty_base_classes)
1609 {
1610 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1611 if (RecordHasFields(base_class_decl) == false)
1612 continue;
1613 }
1614
1615 if (idx == child_idx)
1616 {
1617 if (base_class_decl == NULL)
1618 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1619
1620
1621 if (base_class->isVirtual())
1622 bit_offset = record_layout.getVBaseClassOffset(base_class_decl);
1623 else
1624 bit_offset = record_layout.getBaseClassOffset(base_class_decl);
1625
1626 // Base classes should be a multiple of 8 bits in size
1627 assert (bit_offset % 8 == 0);
1628 child_byte_offset = bit_offset/8;
1629 std::string base_class_type_name(base_class->getType().getAsString());
1630
1631 child_name.assign(base_class_type_name.c_str());
1632
1633 uint64_t clang_type_info_bit_size = ast_context->getTypeSize(base_class->getType());
1634
1635 // Base classes biut sizes should be a multiple of 8 bits in size
1636 assert (clang_type_info_bit_size % 8 == 0);
1637 child_byte_size = clang_type_info_bit_size / 8;
1638 return base_class->getType().getAsOpaquePtr();
1639 }
1640 // We don't increment the child index in the for loop since we might
1641 // be skipping empty base classes
1642 ++child_idx;
1643 }
1644 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001645 // Make sure index is in range...
1646 uint32_t field_idx = 0;
1647 RecordDecl::field_iterator field, field_end;
1648 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
1649 {
1650 if (idx == child_idx)
1651 {
1652 // Print the member type if requested
1653 // Print the member name and equal sign
1654 child_name.assign(field->getNameAsString().c_str());
1655
1656 // Figure out the type byte size (field_type_info.first) and
1657 // alignment (field_type_info.second) from the AST context.
1658 std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(field->getType());
Greg Claytonc982c762010-07-09 20:39:50 +00001659 assert(field_idx < record_layout.getFieldCount());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001660
1661 child_byte_size = field_type_info.first / 8;
1662
1663 // Figure out the field offset within the current struct/union/class type
1664 bit_offset = record_layout.getFieldOffset (field_idx);
1665 child_byte_offset = bit_offset / 8;
1666 if (ClangASTContext::FieldIsBitfield (ast_context, *field, child_bitfield_bit_size))
1667 child_bitfield_bit_offset = bit_offset % 8;
1668
1669 return field->getType().getAsOpaquePtr();
1670 }
1671 }
1672 }
1673 break;
1674
Greg Clayton9e409562010-07-28 02:04:09 +00001675 case clang::Type::ObjCObject:
1676 case clang::Type::ObjCInterface:
1677 {
1678 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr());
1679 assert (objc_class_type);
1680 if (objc_class_type)
1681 {
1682 uint32_t child_idx = 0;
1683 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1684
1685 if (class_interface_decl)
1686 {
1687
1688 const ASTRecordLayout &interface_layout = ast_context->getASTObjCInterfaceLayout(class_interface_decl);
1689 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
1690 if (superclass_interface_decl)
1691 {
1692 if (omit_empty_base_classes)
1693 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001694 if (ClangASTContext::GetNumChildren(ast_context->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0)
Greg Clayton9e409562010-07-28 02:04:09 +00001695 {
1696 if (idx == 0)
1697 {
1698 QualType ivar_qual_type(ast_context->getObjCInterfaceType(superclass_interface_decl));
1699
1700
1701 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
1702
1703 std::pair<uint64_t, unsigned> ivar_type_info = ast_context->getTypeInfo(ivar_qual_type.getTypePtr());
1704
1705 child_byte_size = ivar_type_info.first / 8;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001706 child_byte_offset = 0;
Greg Clayton9e409562010-07-28 02:04:09 +00001707
1708 return ivar_qual_type.getAsOpaquePtr();
1709 }
1710
1711 ++child_idx;
1712 }
1713 }
1714 else
1715 ++child_idx;
1716 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001717
1718 const uint32_t superclass_idx = child_idx;
Greg Clayton9e409562010-07-28 02:04:09 +00001719
1720 if (idx < (child_idx + class_interface_decl->ivar_size()))
1721 {
1722 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
1723
1724 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
1725 {
1726 if (child_idx == idx)
1727 {
1728 const ObjCIvarDecl* ivar_decl = *ivar_pos;
1729
1730 QualType ivar_qual_type(ivar_decl->getType());
1731
1732 child_name.assign(ivar_decl->getNameAsString().c_str());
1733
1734 std::pair<uint64_t, unsigned> ivar_type_info = ast_context->getTypeInfo(ivar_qual_type.getTypePtr());
1735
1736 child_byte_size = ivar_type_info.first / 8;
1737
1738 // Figure out the field offset within the current struct/union/class type
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001739 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
Greg Clayton9e409562010-07-28 02:04:09 +00001740 child_byte_offset = bit_offset / 8;
1741
1742 return ivar_qual_type.getAsOpaquePtr();
1743 }
1744 ++child_idx;
1745 }
1746 }
1747 }
1748 }
1749 }
1750 break;
1751
1752 case clang::Type::ObjCObjectPointer:
1753 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001754 ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr());
1755 QualType pointee_type = pointer_type->getPointeeType();
1756
1757 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
1758 {
1759 return GetChildClangTypeAtIndex (ast_context,
1760 parent_name,
1761 pointer_type->getPointeeType().getAsOpaquePtr(),
1762 idx,
1763 transparent_pointers,
1764 omit_empty_base_classes,
1765 child_name,
1766 child_byte_size,
1767 child_byte_offset,
1768 child_bitfield_bit_size,
1769 child_bitfield_bit_offset);
1770 }
1771 else
1772 {
1773 if (parent_name)
1774 {
1775 child_name.assign(1, '*');
1776 child_name += parent_name;
1777 }
1778
1779 // We have a pointer to an simple type
1780 if (idx == 0)
1781 {
1782 std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type);
1783 assert(clang_type_info.first % 8 == 0);
1784 child_byte_size = clang_type_info.first / 8;
1785 child_byte_offset = 0;
1786 return pointee_type.getAsOpaquePtr();
1787 }
1788 }
Greg Clayton9e409562010-07-28 02:04:09 +00001789 }
1790 break;
1791
Greg Claytone1a916a2010-07-21 22:12:05 +00001792 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001793 {
1794 const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
1795 const uint64_t element_count = array->getSize().getLimitedValue();
1796
1797 if (idx < element_count)
1798 {
1799 std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(array->getElementType());
1800
1801 char element_name[32];
1802 ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
1803
1804 child_name.assign(element_name);
1805 assert(field_type_info.first % 8 == 0);
1806 child_byte_size = field_type_info.first / 8;
1807 child_byte_offset = idx * child_byte_size;
1808 return array->getElementType().getAsOpaquePtr();
1809 }
1810 }
1811 break;
1812
Greg Claytone1a916a2010-07-21 22:12:05 +00001813 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001814 {
1815 PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr());
1816 QualType pointee_type = pointer_type->getPointeeType();
1817
1818 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
1819 {
1820 return GetChildClangTypeAtIndex (ast_context,
1821 parent_name,
1822 pointer_type->getPointeeType().getAsOpaquePtr(),
1823 idx,
1824 transparent_pointers,
1825 omit_empty_base_classes,
1826 child_name,
1827 child_byte_size,
1828 child_byte_offset,
1829 child_bitfield_bit_size,
1830 child_bitfield_bit_offset);
1831 }
1832 else
1833 {
1834 if (parent_name)
1835 {
1836 child_name.assign(1, '*');
1837 child_name += parent_name;
1838 }
1839
1840 // We have a pointer to an simple type
1841 if (idx == 0)
1842 {
1843 std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type);
1844 assert(clang_type_info.first % 8 == 0);
1845 child_byte_size = clang_type_info.first / 8;
1846 child_byte_offset = 0;
1847 return pointee_type.getAsOpaquePtr();
1848 }
1849 }
1850 }
1851 break;
1852
Greg Claytone1a916a2010-07-21 22:12:05 +00001853 case clang::Type::Typedef:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001854 return GetChildClangTypeAtIndex (ast_context,
1855 parent_name,
1856 cast<TypedefType>(parent_qual_type)->LookThroughTypedefs().getAsOpaquePtr(),
1857 idx,
1858 transparent_pointers,
1859 omit_empty_base_classes,
1860 child_name,
1861 child_byte_size,
1862 child_byte_offset,
1863 child_bitfield_bit_size,
1864 child_bitfield_bit_offset);
1865 break;
1866
1867 default:
1868 break;
1869 }
1870 }
Greg Clayton19503a22010-07-23 15:37:46 +00001871 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001872}
1873
1874static inline bool
1875BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
1876{
1877 return ClangASTContext::RecordHasFields(cast<CXXRecordDecl>(b->getType()->getAs<RecordType>()->getDecl())) == false;
1878}
1879
1880static uint32_t
1881GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
1882{
1883 uint32_t num_bases = 0;
1884 if (cxx_record_decl)
1885 {
1886 if (omit_empty_base_classes)
1887 {
1888 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1889 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1890 base_class != base_class_end;
1891 ++base_class)
1892 {
1893 // Skip empty base classes
1894 if (omit_empty_base_classes)
1895 {
1896 if (BaseSpecifierIsEmpty (base_class))
1897 continue;
1898 }
1899 ++num_bases;
1900 }
1901 }
1902 else
1903 num_bases = cxx_record_decl->getNumBases();
1904 }
1905 return num_bases;
1906}
1907
1908
1909static uint32_t
1910GetIndexForRecordBase
1911(
1912 const RecordDecl *record_decl,
1913 const CXXBaseSpecifier *base_spec,
1914 bool omit_empty_base_classes
1915)
1916{
1917 uint32_t child_idx = 0;
1918
1919 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1920
1921// const char *super_name = record_decl->getNameAsCString();
1922// const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString();
1923// printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
1924//
1925 if (cxx_record_decl)
1926 {
1927 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1928 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1929 base_class != base_class_end;
1930 ++base_class)
1931 {
1932 if (omit_empty_base_classes)
1933 {
1934 if (BaseSpecifierIsEmpty (base_class))
1935 continue;
1936 }
1937
1938// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
1939// child_idx,
1940// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
1941//
1942//
1943 if (base_class == base_spec)
1944 return child_idx;
1945 ++child_idx;
1946 }
1947 }
1948
1949 return UINT32_MAX;
1950}
1951
1952
1953static uint32_t
1954GetIndexForRecordChild
1955(
1956 const RecordDecl *record_decl,
1957 NamedDecl *canonical_decl,
1958 bool omit_empty_base_classes
1959)
1960{
1961 uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes);
1962
1963// const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1964//
1965//// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString());
1966// if (cxx_record_decl)
1967// {
1968// CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1969// for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1970// base_class != base_class_end;
1971// ++base_class)
1972// {
1973// if (omit_empty_base_classes)
1974// {
1975// if (BaseSpecifierIsEmpty (base_class))
1976// continue;
1977// }
1978//
1979//// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
1980//// record_decl->getNameAsCString(),
1981//// canonical_decl->getNameAsCString(),
1982//// child_idx,
1983//// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
1984//
1985//
1986// CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1987// if (curr_base_class_decl == canonical_decl)
1988// {
1989// return child_idx;
1990// }
1991// ++child_idx;
1992// }
1993// }
1994//
1995// const uint32_t num_bases = child_idx;
1996 RecordDecl::field_iterator field, field_end;
1997 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
1998 field != field_end;
1999 ++field, ++child_idx)
2000 {
2001// printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n",
2002// record_decl->getNameAsCString(),
2003// canonical_decl->getNameAsCString(),
2004// child_idx - num_bases,
2005// field->getNameAsCString());
2006
2007 if (field->getCanonicalDecl() == canonical_decl)
2008 return child_idx;
2009 }
2010
2011 return UINT32_MAX;
2012}
2013
2014// Look for a child member (doesn't include base classes, but it does include
2015// their members) in the type hierarchy. Returns an index path into "clang_type"
2016// on how to reach the appropriate member.
2017//
2018// class A
2019// {
2020// public:
2021// int m_a;
2022// int m_b;
2023// };
2024//
2025// class B
2026// {
2027// };
2028//
2029// class C :
2030// public B,
2031// public A
2032// {
2033// };
2034//
2035// If we have a clang type that describes "class C", and we wanted to looked
2036// "m_b" in it:
2037//
2038// With omit_empty_base_classes == false we would get an integer array back with:
2039// { 1, 1 }
2040// The first index 1 is the child index for "class A" within class C
2041// The second index 1 is the child index for "m_b" within class A
2042//
2043// With omit_empty_base_classes == true we would get an integer array back with:
2044// { 0, 1 }
2045// 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)
2046// The second index 1 is the child index for "m_b" within class A
2047
2048size_t
2049ClangASTContext::GetIndexOfChildMemberWithName
2050(
2051 ASTContext *ast_context,
2052 void *clang_type,
2053 const char *name,
2054 bool omit_empty_base_classes,
2055 std::vector<uint32_t>& child_indexes
2056)
2057{
2058 if (clang_type && name && name[0])
2059 {
2060 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00002061 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2062 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002063 {
Greg Claytone1a916a2010-07-21 22:12:05 +00002064 case clang::Type::Record:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002065 {
2066 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
2067 const RecordDecl *record_decl = record_type->getDecl();
2068
2069 assert(record_decl);
2070 uint32_t child_idx = 0;
2071
2072 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2073
2074 // Try and find a field that matches NAME
2075 RecordDecl::field_iterator field, field_end;
2076 StringRef name_sref(name);
2077 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
2078 field != field_end;
2079 ++field, ++child_idx)
2080 {
2081 if (field->getName().equals (name_sref))
2082 {
2083 // We have to add on the number of base classes to this index!
2084 child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
2085 return child_indexes.size();
2086 }
2087 }
2088
2089 if (cxx_record_decl)
2090 {
2091 const RecordDecl *parent_record_decl = cxx_record_decl;
2092
2093 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
2094
2095 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
2096 // Didn't find things easily, lets let clang do its thang...
2097 IdentifierInfo & ident_ref = ast_context->Idents.get(name, name + strlen (name));
2098 DeclarationName decl_name(&ident_ref);
2099
2100 CXXBasePaths paths;
2101 if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember,
2102 decl_name.getAsOpaquePtr(),
2103 paths))
2104 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002105 CXXBasePaths::const_paths_iterator path, path_end = paths.end();
2106 for (path = paths.begin(); path != path_end; ++path)
2107 {
2108 const size_t num_path_elements = path->size();
2109 for (size_t e=0; e<num_path_elements; ++e)
2110 {
2111 CXXBasePathElement elem = (*path)[e];
2112
2113 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
2114 if (child_idx == UINT32_MAX)
2115 {
2116 child_indexes.clear();
2117 return 0;
2118 }
2119 else
2120 {
2121 child_indexes.push_back (child_idx);
2122 parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl());
2123 }
2124 }
2125 DeclContext::lookup_iterator named_decl_pos;
2126 for (named_decl_pos = path->Decls.first;
2127 named_decl_pos != path->Decls.second && parent_record_decl;
2128 ++named_decl_pos)
2129 {
2130 //printf ("path[%zu] = %s\n", child_indexes.size(), (*named_decl_pos)->getNameAsCString());
2131
2132 child_idx = GetIndexForRecordChild (parent_record_decl, *named_decl_pos, omit_empty_base_classes);
2133 if (child_idx == UINT32_MAX)
2134 {
2135 child_indexes.clear();
2136 return 0;
2137 }
2138 else
2139 {
2140 child_indexes.push_back (child_idx);
2141 }
2142 }
2143 }
2144 return child_indexes.size();
2145 }
2146 }
2147
2148 }
2149 break;
2150
Greg Clayton9e409562010-07-28 02:04:09 +00002151 case clang::Type::ObjCObject:
2152 case clang::Type::ObjCInterface:
2153 {
2154 StringRef name_sref(name);
2155 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
2156 assert (objc_class_type);
2157 if (objc_class_type)
2158 {
2159 uint32_t child_idx = 0;
2160 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2161
2162 if (class_interface_decl)
2163 {
2164 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
2165 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
2166
Greg Clayton6ba78152010-09-18 02:11:07 +00002167 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9e409562010-07-28 02:04:09 +00002168 {
2169 const ObjCIvarDecl* ivar_decl = *ivar_pos;
2170
2171 if (ivar_decl->getName().equals (name_sref))
2172 {
2173 if ((!omit_empty_base_classes && superclass_interface_decl) ||
2174 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
2175 ++child_idx;
2176
2177 child_indexes.push_back (child_idx);
2178 return child_indexes.size();
2179 }
2180 }
2181
2182 if (superclass_interface_decl)
2183 {
2184 // The super class index is always zero for ObjC classes,
2185 // so we push it onto the child indexes in case we find
2186 // an ivar in our superclass...
2187 child_indexes.push_back (0);
2188
2189 if (GetIndexOfChildMemberWithName (ast_context,
2190 ast_context->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(),
2191 name,
2192 omit_empty_base_classes,
2193 child_indexes))
2194 {
2195 // We did find an ivar in a superclass so just
2196 // return the results!
2197 return child_indexes.size();
2198 }
2199
2200 // We didn't find an ivar matching "name" in our
2201 // superclass, pop the superclass zero index that
2202 // we pushed on above.
2203 child_indexes.pop_back();
2204 }
2205 }
2206 }
2207 }
2208 break;
2209
2210 case clang::Type::ObjCObjectPointer:
2211 {
2212 return GetIndexOfChildMemberWithName (ast_context,
2213 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
2214 name,
2215 omit_empty_base_classes,
2216 child_indexes);
2217 }
2218 break;
2219
2220
Greg Claytone1a916a2010-07-21 22:12:05 +00002221 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002222 {
2223// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
2224// const uint64_t element_count = array->getSize().getLimitedValue();
2225//
2226// if (idx < element_count)
2227// {
2228// std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(array->getElementType());
2229//
2230// char element_name[32];
2231// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
2232//
2233// child_name.assign(element_name);
2234// assert(field_type_info.first % 8 == 0);
2235// child_byte_size = field_type_info.first / 8;
2236// child_byte_offset = idx * child_byte_size;
2237// return array->getElementType().getAsOpaquePtr();
2238// }
2239 }
2240 break;
2241
Greg Claytone1a916a2010-07-21 22:12:05 +00002242// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002243// {
2244// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
2245// QualType pointee_type = mem_ptr_type->getPointeeType();
2246//
2247// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
2248// {
2249// return GetIndexOfChildWithName (ast_context,
2250// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
2251// name);
2252// }
2253// }
2254// break;
2255//
Greg Claytone1a916a2010-07-21 22:12:05 +00002256 case clang::Type::LValueReference:
2257 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002258 {
2259 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
2260 QualType pointee_type = reference_type->getPointeeType();
2261
2262 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
2263 {
2264 return GetIndexOfChildMemberWithName (ast_context,
2265 reference_type->getPointeeType().getAsOpaquePtr(),
2266 name,
2267 omit_empty_base_classes,
2268 child_indexes);
2269 }
2270 }
2271 break;
2272
Greg Claytone1a916a2010-07-21 22:12:05 +00002273 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002274 {
2275 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
2276 QualType pointee_type = pointer_type->getPointeeType();
2277
2278 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
2279 {
2280 return GetIndexOfChildMemberWithName (ast_context,
2281 pointer_type->getPointeeType().getAsOpaquePtr(),
2282 name,
2283 omit_empty_base_classes,
2284 child_indexes);
2285 }
2286 else
2287 {
2288// if (parent_name)
2289// {
2290// child_name.assign(1, '*');
2291// child_name += parent_name;
2292// }
2293//
2294// // We have a pointer to an simple type
2295// if (idx == 0)
2296// {
2297// std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type);
2298// assert(clang_type_info.first % 8 == 0);
2299// child_byte_size = clang_type_info.first / 8;
2300// child_byte_offset = 0;
2301// return pointee_type.getAsOpaquePtr();
2302// }
2303 }
2304 }
2305 break;
2306
Greg Claytone1a916a2010-07-21 22:12:05 +00002307 case clang::Type::Typedef:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002308 return GetIndexOfChildMemberWithName (ast_context,
2309 cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(),
2310 name,
2311 omit_empty_base_classes,
2312 child_indexes);
2313
2314 default:
2315 break;
2316 }
2317 }
2318 return 0;
2319}
2320
2321
2322// Get the index of the child of "clang_type" whose name matches. This function
2323// doesn't descend into the children, but only looks one level deep and name
2324// matches can include base class names.
2325
2326uint32_t
2327ClangASTContext::GetIndexOfChildWithName
2328(
2329 ASTContext *ast_context,
2330 void *clang_type,
2331 const char *name,
2332 bool omit_empty_base_classes
2333)
2334{
2335 if (clang_type && name && name[0])
2336 {
2337 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9e409562010-07-28 02:04:09 +00002338
Greg Clayton737b9322010-09-13 03:32:57 +00002339 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Clayton9e409562010-07-28 02:04:09 +00002340
Greg Clayton737b9322010-09-13 03:32:57 +00002341 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002342 {
Greg Claytone1a916a2010-07-21 22:12:05 +00002343 case clang::Type::Record:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002344 {
2345 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
2346 const RecordDecl *record_decl = record_type->getDecl();
2347
2348 assert(record_decl);
2349 uint32_t child_idx = 0;
2350
2351 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2352
2353 if (cxx_record_decl)
2354 {
2355 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2356 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2357 base_class != base_class_end;
2358 ++base_class)
2359 {
2360 // Skip empty base classes
2361 CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2362 if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false)
2363 continue;
2364
2365 if (base_class->getType().getAsString().compare (name) == 0)
2366 return child_idx;
2367 ++child_idx;
2368 }
2369 }
2370
2371 // Try and find a field that matches NAME
2372 RecordDecl::field_iterator field, field_end;
2373 StringRef name_sref(name);
2374 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
2375 field != field_end;
2376 ++field, ++child_idx)
2377 {
2378 if (field->getName().equals (name_sref))
2379 return child_idx;
2380 }
2381
2382 }
2383 break;
2384
Greg Clayton9e409562010-07-28 02:04:09 +00002385 case clang::Type::ObjCObject:
2386 case clang::Type::ObjCInterface:
2387 {
2388 StringRef name_sref(name);
2389 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
2390 assert (objc_class_type);
2391 if (objc_class_type)
2392 {
2393 uint32_t child_idx = 0;
2394 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2395
2396 if (class_interface_decl)
2397 {
2398 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
2399 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
2400
2401 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
2402 {
2403 const ObjCIvarDecl* ivar_decl = *ivar_pos;
2404
2405 if (ivar_decl->getName().equals (name_sref))
2406 {
2407 if ((!omit_empty_base_classes && superclass_interface_decl) ||
2408 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
2409 ++child_idx;
2410
2411 return child_idx;
2412 }
2413 }
2414
2415 if (superclass_interface_decl)
2416 {
2417 if (superclass_interface_decl->getName().equals (name_sref))
2418 return 0;
2419 }
2420 }
2421 }
2422 }
2423 break;
2424
2425 case clang::Type::ObjCObjectPointer:
2426 {
2427 return GetIndexOfChildWithName (ast_context,
2428 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
2429 name,
2430 omit_empty_base_classes);
2431 }
2432 break;
2433
Greg Claytone1a916a2010-07-21 22:12:05 +00002434 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002435 {
2436// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
2437// const uint64_t element_count = array->getSize().getLimitedValue();
2438//
2439// if (idx < element_count)
2440// {
2441// std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(array->getElementType());
2442//
2443// char element_name[32];
2444// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
2445//
2446// child_name.assign(element_name);
2447// assert(field_type_info.first % 8 == 0);
2448// child_byte_size = field_type_info.first / 8;
2449// child_byte_offset = idx * child_byte_size;
2450// return array->getElementType().getAsOpaquePtr();
2451// }
2452 }
2453 break;
2454
Greg Claytone1a916a2010-07-21 22:12:05 +00002455// case clang::Type::MemberPointerType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002456// {
2457// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
2458// QualType pointee_type = mem_ptr_type->getPointeeType();
2459//
2460// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
2461// {
2462// return GetIndexOfChildWithName (ast_context,
2463// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
2464// name);
2465// }
2466// }
2467// break;
2468//
Greg Claytone1a916a2010-07-21 22:12:05 +00002469 case clang::Type::LValueReference:
2470 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002471 {
2472 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
2473 QualType pointee_type = reference_type->getPointeeType();
2474
2475 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
2476 {
2477 return GetIndexOfChildWithName (ast_context,
2478 reference_type->getPointeeType().getAsOpaquePtr(),
2479 name,
2480 omit_empty_base_classes);
2481 }
2482 }
2483 break;
2484
Greg Claytone1a916a2010-07-21 22:12:05 +00002485 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002486 {
2487 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
2488 QualType pointee_type = pointer_type->getPointeeType();
2489
2490 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
2491 {
2492 return GetIndexOfChildWithName (ast_context,
2493 pointer_type->getPointeeType().getAsOpaquePtr(),
2494 name,
2495 omit_empty_base_classes);
2496 }
2497 else
2498 {
2499// if (parent_name)
2500// {
2501// child_name.assign(1, '*');
2502// child_name += parent_name;
2503// }
2504//
2505// // We have a pointer to an simple type
2506// if (idx == 0)
2507// {
2508// std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type);
2509// assert(clang_type_info.first % 8 == 0);
2510// child_byte_size = clang_type_info.first / 8;
2511// child_byte_offset = 0;
2512// return pointee_type.getAsOpaquePtr();
2513// }
2514 }
2515 }
2516 break;
2517
Greg Claytone1a916a2010-07-21 22:12:05 +00002518 case clang::Type::Typedef:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002519 return GetIndexOfChildWithName (ast_context,
2520 cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(),
2521 name,
2522 omit_empty_base_classes);
2523
2524 default:
2525 break;
2526 }
2527 }
2528 return UINT32_MAX;
2529}
2530
2531#pragma mark TagType
2532
2533bool
2534ClangASTContext::SetTagTypeKind (void *tag_clang_type, int kind)
2535{
2536 if (tag_clang_type)
2537 {
2538 QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type));
Greg Claytone1a916a2010-07-21 22:12:05 +00002539 clang::Type *clang_type = tag_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002540 if (clang_type)
2541 {
2542 TagType *tag_type = dyn_cast<TagType>(clang_type);
2543 if (tag_type)
2544 {
2545 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl());
2546 if (tag_decl)
2547 {
2548 tag_decl->setTagKind ((TagDecl::TagKind)kind);
2549 return true;
2550 }
2551 }
2552 }
2553 }
2554 return false;
2555}
2556
2557
2558#pragma mark DeclContext Functions
2559
2560DeclContext *
2561ClangASTContext::GetDeclContextForType (void *clang_type)
2562{
2563 if (clang_type == NULL)
2564 return NULL;
2565
2566 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00002567 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2568 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002569 {
Greg Clayton9e409562010-07-28 02:04:09 +00002570 case clang::Type::FunctionNoProto: break;
2571 case clang::Type::FunctionProto: break;
2572 case clang::Type::IncompleteArray: break;
2573 case clang::Type::VariableArray: break;
2574 case clang::Type::ConstantArray: break;
2575 case clang::Type::ExtVector: break;
2576 case clang::Type::Vector: break;
2577 case clang::Type::Builtin: break;
2578 case clang::Type::BlockPointer: break;
2579 case clang::Type::Pointer: break;
2580 case clang::Type::LValueReference: break;
2581 case clang::Type::RValueReference: break;
2582 case clang::Type::MemberPointer: break;
2583 case clang::Type::Complex: break;
2584 case clang::Type::ObjCObject: break;
2585 case clang::Type::ObjCInterface: return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface();
2586 case clang::Type::ObjCObjectPointer: return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr());
2587 case clang::Type::Record: return cast<RecordType>(qual_type)->getDecl();
2588 case clang::Type::Enum: return cast<EnumType>(qual_type)->getDecl();
2589 case clang::Type::Typedef: return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002590
Greg Clayton9e409562010-07-28 02:04:09 +00002591 case clang::Type::TypeOfExpr: break;
2592 case clang::Type::TypeOf: break;
2593 case clang::Type::Decltype: break;
2594 //case clang::Type::QualifiedName: break;
2595 case clang::Type::TemplateSpecialization: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002596 }
2597 // No DeclContext in this type...
2598 return NULL;
2599}
2600
2601#pragma mark Namespace Declarations
2602
2603NamespaceDecl *
2604ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, const Declaration &decl, DeclContext *decl_ctx)
2605{
2606 // TODO: Do something intelligent with the Declaration object passed in
2607 // like maybe filling in the SourceLocation with it...
2608 if (name)
2609 {
2610 ASTContext *ast_context = getASTContext();
2611 if (decl_ctx == NULL)
2612 decl_ctx = ast_context->getTranslationUnitDecl();
2613 return NamespaceDecl::Create(*ast_context, decl_ctx, SourceLocation(), &ast_context->Idents.get(name));
2614 }
2615 return NULL;
2616}
2617
2618
2619#pragma mark Function Types
2620
2621FunctionDecl *
2622ClangASTContext::CreateFunctionDeclaration (const char *name, void *function_clang_type, int storage, bool is_inline)
2623{
2624 if (name)
2625 {
2626 ASTContext *ast_context = getASTContext();
2627 assert (ast_context != NULL);
2628
2629 if (name && name[0])
2630 {
2631 return FunctionDecl::Create(*ast_context,
2632 ast_context->getTranslationUnitDecl(),
2633 SourceLocation(),
2634 DeclarationName (&ast_context->Idents.get(name)),
2635 QualType::getFromOpaquePtr(function_clang_type),
2636 NULL,
2637 (FunctionDecl::StorageClass)storage,
2638 (FunctionDecl::StorageClass)storage,
2639 is_inline);
2640 }
2641 else
2642 {
2643 return FunctionDecl::Create(*ast_context,
2644 ast_context->getTranslationUnitDecl(),
2645 SourceLocation(),
2646 DeclarationName (),
2647 QualType::getFromOpaquePtr(function_clang_type),
2648 NULL,
2649 (FunctionDecl::StorageClass)storage,
2650 (FunctionDecl::StorageClass)storage,
2651 is_inline);
2652 }
2653 }
2654 return NULL;
2655}
2656
2657void *
Sean Callananc81256a2010-09-16 20:40:25 +00002658ClangASTContext::CreateFunctionType (clang::ASTContext *ast_context,
2659 void *result_type,
2660 void **args,
2661 unsigned num_args,
2662 bool is_variadic,
2663 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002664{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002665 assert (ast_context != NULL);
2666 std::vector<QualType> qual_type_args;
2667 for (unsigned i=0; i<num_args; ++i)
2668 qual_type_args.push_back (QualType::getFromOpaquePtr(args[i]));
2669
2670 // TODO: Detect calling convention in DWARF?
2671 return ast_context->getFunctionType(QualType::getFromOpaquePtr(result_type),
Greg Clayton471b31c2010-07-20 22:52:08 +00002672 qual_type_args.empty() ? NULL : &qual_type_args.front(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002673 qual_type_args.size(),
Sean Callananc81256a2010-09-16 20:40:25 +00002674 is_variadic,
2675 type_quals,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002676 false, // hasExceptionSpec
2677 false, // hasAnyExceptionSpec,
2678 0, // NumExs
2679 0, // const QualType *ExArray
2680 FunctionType::ExtInfo ()).getAsOpaquePtr(); // NoReturn);
2681}
2682
2683ParmVarDecl *
Sean Callananc81256a2010-09-16 20:40:25 +00002684ClangASTContext::CreateParameterDeclaration (const char *name, void *param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002685{
2686 ASTContext *ast_context = getASTContext();
2687 assert (ast_context != NULL);
2688 return ParmVarDecl::Create(*ast_context,
2689 ast_context->getTranslationUnitDecl(),
2690 SourceLocation(),
2691 name && name[0] ? &ast_context->Idents.get(name) : NULL,
Sean Callananc81256a2010-09-16 20:40:25 +00002692 QualType::getFromOpaquePtr(param_type),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002693 NULL,
2694 (VarDecl::StorageClass)storage,
2695 (VarDecl::StorageClass)storage,
2696 0);
2697}
2698
2699void
2700ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
2701{
2702 if (function_decl)
2703 function_decl->setParams (params, num_params);
2704}
2705
2706
2707#pragma mark Array Types
2708
2709void *
2710ClangASTContext::CreateArrayType (void *element_type, size_t element_count, uint32_t bit_stride)
2711{
2712 if (element_type)
2713 {
2714 ASTContext *ast_context = getASTContext();
2715 assert (ast_context != NULL);
2716 llvm::APInt ap_element_count (64, element_count);
2717 return ast_context->getConstantArrayType(QualType::getFromOpaquePtr(element_type),
2718 ap_element_count,
2719 ArrayType::Normal,
2720 0).getAsOpaquePtr(); // ElemQuals
2721 }
2722 return NULL;
2723}
2724
2725
2726#pragma mark TagDecl
2727
2728bool
2729ClangASTContext::StartTagDeclarationDefinition (void *clang_type)
2730{
2731 if (clang_type)
2732 {
2733 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Claytone1a916a2010-07-21 22:12:05 +00002734 clang::Type *t = qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002735 if (t)
2736 {
2737 TagType *tag_type = dyn_cast<TagType>(t);
2738 if (tag_type)
2739 {
2740 TagDecl *tag_decl = tag_type->getDecl();
2741 if (tag_decl)
2742 {
2743 tag_decl->startDefinition();
2744 return true;
2745 }
2746 }
2747 }
2748 }
2749 return false;
2750}
2751
2752bool
2753ClangASTContext::CompleteTagDeclarationDefinition (void *clang_type)
2754{
2755 if (clang_type)
2756 {
2757 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Claytone1a916a2010-07-21 22:12:05 +00002758 clang::Type *t = qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002759 if (t)
2760 {
2761 TagType *tag_type = dyn_cast<TagType>(t);
2762 if (tag_type)
2763 {
2764 TagDecl *tag_decl = tag_type->getDecl();
2765 if (tag_decl)
2766 {
2767 tag_decl->completeDefinition();
2768 return true;
2769 }
2770 }
2771 }
2772 }
2773 return false;
2774}
2775
2776
2777#pragma mark Enumeration Types
2778
2779void *
Greg Clayton83ff3892010-09-12 23:17:56 +00002780ClangASTContext::CreateEnumerationType (const Declaration &decl, const char *name, void *integer_qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002781{
2782 // TODO: Do something intelligent with the Declaration object passed in
2783 // like maybe filling in the SourceLocation with it...
2784 ASTContext *ast_context = getASTContext();
2785 assert (ast_context != NULL);
2786 EnumDecl *enum_decl = EnumDecl::Create(*ast_context,
2787 ast_context->getTranslationUnitDecl(),
2788 SourceLocation(),
2789 name && name[0] ? &ast_context->Idents.get(name) : NULL,
2790 SourceLocation(),
2791 NULL);
2792 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00002793 {
2794 // TODO: check if we should be setting the promotion type too?
2795 enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002796 return ast_context->getTagDeclType(enum_decl).getAsOpaquePtr();
Greg Clayton83ff3892010-09-12 23:17:56 +00002797 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002798 return NULL;
2799}
2800
2801bool
2802ClangASTContext::AddEnumerationValueToEnumerationType
2803(
2804 void *enum_clang_type,
2805 void *enumerator_clang_type,
2806 const Declaration &decl,
2807 const char *name,
2808 int64_t enum_value,
2809 uint32_t enum_value_bit_size
2810)
2811{
2812 if (enum_clang_type && enumerator_clang_type && name)
2813 {
2814 // TODO: Do something intelligent with the Declaration object passed in
2815 // like maybe filling in the SourceLocation with it...
2816 ASTContext *ast_context = getASTContext();
2817 IdentifierTable *identifier_table = getIdentifierTable();
2818
2819 assert (ast_context != NULL);
2820 assert (identifier_table != NULL);
2821 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
2822
Greg Claytone1a916a2010-07-21 22:12:05 +00002823 clang::Type *clang_type = enum_qual_type.getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002824 if (clang_type)
2825 {
2826 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
2827
2828 if (enum_type)
2829 {
2830 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, false);
2831 enum_llvm_apsint = enum_value;
2832 EnumConstantDecl *enumerator_decl =
2833 EnumConstantDecl::Create(*ast_context,
2834 enum_type->getDecl(),
2835 SourceLocation(),
2836 name ? &identifier_table->get(name) : NULL, // Identifier
2837 QualType::getFromOpaquePtr(enumerator_clang_type),
2838 NULL,
2839 enum_llvm_apsint);
2840
2841 if (enumerator_decl)
2842 {
2843 enum_type->getDecl()->addDecl(enumerator_decl);
2844 return true;
2845 }
2846 }
2847 }
2848 }
2849 return false;
2850}
2851
2852#pragma mark Pointers & References
2853
2854void *
2855ClangASTContext::CreatePointerType (void *clang_type)
2856{
2857 if (clang_type)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00002858 {
2859 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2860
Greg Clayton737b9322010-09-13 03:32:57 +00002861 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2862 switch (type_class)
Greg Clayton5fb47cd2010-07-29 20:06:32 +00002863 {
2864 case clang::Type::ObjCObject:
2865 case clang::Type::ObjCInterface:
Greg Clayton5fb47cd2010-07-29 20:06:32 +00002866 return getASTContext()->getObjCObjectPointerType(qual_type).getAsOpaquePtr();
2867
Greg Clayton5fb47cd2010-07-29 20:06:32 +00002868 default:
2869 return getASTContext()->getPointerType(qual_type).getAsOpaquePtr();
2870 }
2871 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002872 return NULL;
2873}
2874
2875void *
2876ClangASTContext::CreateLValueReferenceType (void *clang_type)
2877{
2878 if (clang_type)
2879 return getASTContext()->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
2880 return NULL;
2881}
2882
2883void *
2884ClangASTContext::CreateRValueReferenceType (void *clang_type)
2885{
2886 if (clang_type)
2887 return getASTContext()->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
2888 return NULL;
2889}
2890
Greg Clayton9b81a312010-06-12 01:20:30 +00002891void *
Greg Clayton8cf05932010-07-22 18:30:50 +00002892ClangASTContext::CreateMemberPointerType (void *clang_pointee_type, void *clang_class_type)
Greg Clayton9b81a312010-06-12 01:20:30 +00002893{
2894 if (clang_pointee_type && clang_pointee_type)
2895 return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type),
2896 QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr();
2897 return NULL;
2898}
2899
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002900size_t
2901ClangASTContext::GetPointerBitSize ()
2902{
2903 ASTContext *ast_context = getASTContext();
2904 return ast_context->getTypeSize(ast_context->VoidPtrTy);
2905}
2906
2907bool
2908ClangASTContext::IsPointerOrReferenceType (void *clang_type, void **target_type)
2909{
2910 if (clang_type == NULL)
2911 return false;
2912
2913 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00002914 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2915 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002916 {
Greg Claytone1a916a2010-07-21 22:12:05 +00002917 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002918 if (target_type)
2919 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
2920 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00002921 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002922 if (target_type)
2923 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
2924 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00002925 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002926 if (target_type)
2927 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
2928 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00002929 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002930 if (target_type)
2931 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
2932 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00002933 case clang::Type::LValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002934 if (target_type)
2935 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
2936 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00002937 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002938 if (target_type)
2939 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
2940 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00002941 case clang::Type::Typedef:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002942 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr());
2943 default:
2944 break;
2945 }
2946 return false;
2947}
2948
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002949bool
Greg Clayton8cf05932010-07-22 18:30:50 +00002950ClangASTContext::IsIntegerType (void *clang_type, bool &is_signed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002951{
2952 if (!clang_type)
2953 return false;
2954
2955 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2956 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal());
2957
2958 if (builtin_type)
2959 {
2960 if (builtin_type->isInteger())
2961 is_signed = builtin_type->isSignedInteger();
2962
2963 return true;
2964 }
2965
2966 return false;
2967}
2968
2969bool
2970ClangASTContext::IsPointerType (void *clang_type, void **target_type)
2971{
2972 if (clang_type)
2973 {
2974 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00002975 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2976 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002977 {
Greg Claytone1a916a2010-07-21 22:12:05 +00002978 case clang::Type::ObjCObjectPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002979 if (target_type)
2980 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
2981 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00002982 case clang::Type::BlockPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002983 if (target_type)
2984 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
2985 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00002986 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002987 if (target_type)
2988 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
2989 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00002990 case clang::Type::MemberPointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002991 if (target_type)
2992 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
2993 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00002994 case clang::Type::Typedef:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002995 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), target_type);
2996 default:
2997 break;
2998 }
2999 }
3000 return false;
3001}
3002
3003bool
3004ClangASTContext::IsFloatingPointType (void *clang_type, uint32_t &count, bool &is_complex)
3005{
3006 if (clang_type)
3007 {
3008 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3009
3010 if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()))
3011 {
3012 clang::BuiltinType::Kind kind = BT->getKind();
3013 if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble)
3014 {
3015 count = 1;
3016 is_complex = false;
3017 return true;
3018 }
3019 }
3020 else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal()))
3021 {
3022 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex))
3023 {
3024 count = 2;
3025 is_complex = true;
3026 return true;
3027 }
3028 }
3029 else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal()))
3030 {
3031 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex))
3032 {
3033 count = VT->getNumElements();
3034 is_complex = false;
3035 return true;
3036 }
3037 }
3038 }
3039 return false;
3040}
3041
3042
3043bool
3044ClangASTContext::IsCStringType (void *clang_type, uint32_t &length)
3045{
3046 if (clang_type)
3047 {
3048 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +00003049 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3050 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003051 {
Greg Claytone1a916a2010-07-21 22:12:05 +00003052 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003053 {
3054 ConstantArrayType *array = cast<ConstantArrayType>(qual_type.getTypePtr());
3055 QualType element_qual_type = array->getElementType();
Greg Claytone1a916a2010-07-21 22:12:05 +00003056 clang::Type *canonical_type = element_qual_type->getCanonicalTypeInternal().getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003057 if (canonical_type && canonical_type->isCharType())
3058 {
3059 // We know the size of the array and it could be a C string
3060 // since it is an array of characters
3061 length = array->getSize().getLimitedValue();
3062 return true;
3063 }
3064 }
3065 break;
3066
Greg Claytone1a916a2010-07-21 22:12:05 +00003067 case clang::Type::Pointer:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003068 {
3069 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Greg Claytone1a916a2010-07-21 22:12:05 +00003070 clang::Type *pointee_type_ptr = pointer_type->getPointeeType().getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003071 if (pointee_type_ptr)
3072 {
Greg Claytone1a916a2010-07-21 22:12:05 +00003073 clang::Type *canonical_type_ptr = pointee_type_ptr->getCanonicalTypeInternal().getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003074 length = 0; // No length info, read until a NULL terminator is received
3075 if (canonical_type_ptr)
3076 return canonical_type_ptr->isCharType();
3077 else
3078 return pointee_type_ptr->isCharType();
3079 }
3080 }
3081 break;
3082
Greg Claytone1a916a2010-07-21 22:12:05 +00003083 case clang::Type::Typedef:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003084 return ClangASTContext::IsCStringType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), length);
3085
Greg Claytone1a916a2010-07-21 22:12:05 +00003086 case clang::Type::LValueReference:
3087 case clang::Type::RValueReference:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003088 {
3089 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Claytone1a916a2010-07-21 22:12:05 +00003090 clang::Type *pointee_type_ptr = reference_type->getPointeeType().getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003091 if (pointee_type_ptr)
3092 {
Greg Claytone1a916a2010-07-21 22:12:05 +00003093 clang::Type *canonical_type_ptr = pointee_type_ptr->getCanonicalTypeInternal().getTypePtr();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003094 length = 0; // No length info, read until a NULL terminator is received
3095 if (canonical_type_ptr)
3096 return canonical_type_ptr->isCharType();
3097 else
3098 return pointee_type_ptr->isCharType();
3099 }
3100 }
3101 break;
3102 }
3103 }
3104 return false;
3105}
3106
3107bool
Greg Clayton737b9322010-09-13 03:32:57 +00003108ClangASTContext::IsFunctionPointerType (void *clang_type)
3109{
3110 if (clang_type)
3111 {
3112 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3113
3114 if (qual_type->isFunctionPointerType())
3115 return true;
3116
3117 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3118 switch (type_class)
3119 {
3120 case clang::Type::Typedef:
3121 return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr());
3122
3123 case clang::Type::LValueReference:
3124 case clang::Type::RValueReference:
3125 {
3126 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
3127 if (reference_type)
3128 return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr());
3129 }
3130 break;
3131 }
3132 }
3133 return false;
3134}
3135
3136
3137
3138
3139bool
Greg Clayton8cf05932010-07-22 18:30:50 +00003140ClangASTContext::IsArrayType (void *clang_type, void **member_type, uint64_t *size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003141{
3142 if (!clang_type)
3143 return false;
3144
3145 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3146
Greg Clayton737b9322010-09-13 03:32:57 +00003147 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3148 switch (type_class)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003149 {
Greg Claytone1a916a2010-07-21 22:12:05 +00003150 case clang::Type::ConstantArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003151 if (member_type)
3152 *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr();
3153 if (size)
3154 *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULONG_LONG_MAX);
3155 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00003156 case clang::Type::IncompleteArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003157 if (member_type)
3158 *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr();
3159 if (size)
3160 *size = 0;
3161 return true;
Greg Claytone1a916a2010-07-21 22:12:05 +00003162 case clang::Type::VariableArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003163 if (member_type)
3164 *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr();
3165 if (size)
3166 *size = 0;
Greg Claytone1a916a2010-07-21 22:12:05 +00003167 case clang::Type::DependentSizedArray:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003168 if (member_type)
3169 *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr();
3170 if (size)
3171 *size = 0;
3172 return true;
3173 }
3174 return false;
3175}
3176
3177
3178#pragma mark Typedefs
3179
3180void *
3181ClangASTContext::CreateTypedefType (const char *name, void *clang_type, DeclContext *decl_ctx)
3182{
3183 if (clang_type)
3184 {
3185 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3186 ASTContext *ast_context = getASTContext();
3187 IdentifierTable *identifier_table = getIdentifierTable();
3188 assert (ast_context != NULL);
3189 assert (identifier_table != NULL);
3190 if (decl_ctx == NULL)
3191 decl_ctx = ast_context->getTranslationUnitDecl();
3192 TypedefDecl *decl = TypedefDecl::Create(*ast_context,
3193 decl_ctx,
3194 SourceLocation(),
3195 name ? &identifier_table->get(name) : NULL, // Identifier
3196 ast_context->CreateTypeSourceInfo(qual_type));
3197
3198 // Get a uniqued QualType for the typedef decl type
3199 return ast_context->getTypedefType (decl).getAsOpaquePtr();
3200 }
3201 return NULL;
3202}
3203
3204
3205std::string
3206ClangASTContext::GetTypeName (void *opaque_qual_type)
3207{
3208 std::string return_name;
3209
3210 clang::QualType qual_type(clang::QualType::getFromOpaquePtr(opaque_qual_type));
3211
3212 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
3213 if (typedef_type)
3214 {
3215 const clang::TypedefDecl *typedef_decl = typedef_type->getDecl();
3216 return_name = typedef_decl->getQualifiedNameAsString();
3217 }
3218 else
3219 {
3220 return_name = qual_type.getAsString();
3221 }
3222
3223 return return_name;
3224}
3225
3226// Disable this for now since I can't seem to get a nicely formatted float
3227// out of the APFloat class without just getting the float, double or quad
3228// and then using a formatted print on it which defeats the purpose. We ideally
3229// would like to get perfect string values for any kind of float semantics
3230// so we can support remote targets. The code below also requires a patch to
3231// llvm::APInt.
3232//bool
3233//ClangASTContext::ConvertFloatValueToString (ASTContext *ast_context, void *clang_type, const uint8_t* bytes, size_t byte_size, int apint_byte_order, std::string &float_str)
3234//{
3235// uint32_t count = 0;
3236// bool is_complex = false;
3237// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
3238// {
3239// unsigned num_bytes_per_float = byte_size / count;
3240// unsigned num_bits_per_float = num_bytes_per_float * 8;
3241//
3242// float_str.clear();
3243// uint32_t i;
3244// for (i=0; i<count; i++)
3245// {
3246// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
3247// bool is_ieee = false;
3248// APFloat ap_float(ap_int, is_ieee);
3249// char s[1024];
3250// unsigned int hex_digits = 0;
3251// bool upper_case = false;
3252//
3253// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
3254// {
3255// if (i > 0)
3256// float_str.append(", ");
3257// float_str.append(s);
3258// if (i == 1 && is_complex)
3259// float_str.append(1, 'i');
3260// }
3261// }
3262// return !float_str.empty();
3263// }
3264// return false;
3265//}
3266
3267size_t
3268ClangASTContext::ConvertStringToFloatValue (ASTContext *ast_context, void *clang_type, const char *s, uint8_t *dst, size_t dst_size)
3269{
3270 if (clang_type)
3271 {
3272 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3273 uint32_t count = 0;
3274 bool is_complex = false;
3275 if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
3276 {
3277 // TODO: handle complex and vector types
3278 if (count != 1)
3279 return false;
3280
3281 StringRef s_sref(s);
3282 APFloat ap_float(ast_context->getFloatTypeSemantics(qual_type), s_sref);
3283
3284 const uint64_t bit_size = ast_context->getTypeSize (qual_type);
3285 const uint64_t byte_size = bit_size / 8;
3286 if (dst_size >= byte_size)
3287 {
3288 if (bit_size == sizeof(float)*8)
3289 {
3290 float float32 = ap_float.convertToFloat();
3291 ::memcpy (dst, &float32, byte_size);
3292 return byte_size;
3293 }
3294 else if (bit_size >= 64)
3295 {
3296 llvm::APInt ap_int(ap_float.bitcastToAPInt());
3297 ::memcpy (dst, ap_int.getRawData(), byte_size);
3298 return byte_size;
3299 }
3300 }
3301 }
3302 }
3303 return 0;
3304}
Sean Callanan6fe64b52010-09-17 02:24:29 +00003305
3306unsigned
3307ClangASTContext::GetTypeQualifiers(void *clang_type)
3308{
3309 assert (clang_type);
3310
3311 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3312
3313 return qual_type.getQualifiers().getCVRQualifiers();
3314}