blob: cf223b4cfb0cd344890b0d9d6e5ada79cc37cf76 [file] [log] [blame]
Chris Lattner24943d22010-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 Friedmanf05633b2010-06-13 19:06:42 +000010#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner24943d22010-06-08 16:52:24 +000011
12// C Includes
13// C++ Includes
14#include <string>
15
16// Other libraries and framework includes
Sean Callananbc4f0f52010-07-08 18:16:16 +000017#define NDEBUG
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/ASTImporter.h"
20#include "clang/AST/CXXInheritance.h"
Greg Clayton84f80752010-07-22 18:30:50 +000021#include "clang/AST/DeclObjC.h"
Chris Lattner24943d22010-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 Callananbc4f0f52010-07-08 18:16:16 +000031#undef NDEBUG
Chris Lattner24943d22010-06-08 16:52:24 +000032
Chris Lattner24943d22010-06-08 16:52:24 +000033#include "lldb/Core/dwarf.h"
34
Eli Friedmanf05633b2010-06-13 19:06:42 +000035#include <stdio.h>
36
Greg Clayton585660c2010-08-05 01:57:25 +000037using namespace lldb;
Chris Lattner24943d22010-06-08 16:52:24 +000038using namespace lldb_private;
39using namespace llvm;
40using namespace clang;
41
Greg Clayton84f80752010-07-22 18:30:50 +000042static AccessSpecifier
Greg Clayton585660c2010-08-05 01:57:25 +000043ConvertAccessTypeToAccessSpecifier (AccessType access)
Greg Clayton84f80752010-07-22 18:30:50 +000044{
45 switch (access)
46 {
Greg Clayton585660c2010-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 Clayton84f80752010-07-22 18:30:50 +000052 }
53 return AS_none;
54}
55
56static ObjCIvarDecl::AccessControl
Greg Clayton585660c2010-08-05 01:57:25 +000057ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
Greg Clayton84f80752010-07-22 18:30:50 +000058{
59 switch (access)
60 {
Greg Clayton585660c2010-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 Clayton84f80752010-07-22 18:30:50 +000067 }
68 return ObjCIvarDecl::None;
69}
70
71
Chris Lattner24943d22010-06-08 16:52:24 +000072static void
73ParseLangArgs
74(
75 LangOptions &Opts,
Greg Claytone41c4b22010-06-13 17:34:29 +000076 InputKind IK
Chris Lattner24943d22010-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 Claytone41c4b22010-06-13 17:34:29 +000084 if (IK == IK_Asm) {
Chris Lattner24943d22010-06-08 16:52:24 +000085 Opts.AsmPreprocessor = 1;
Greg Claytone41c4b22010-06-13 17:34:29 +000086 } else if (IK == IK_ObjC ||
87 IK == IK_ObjCXX ||
88 IK == IK_PreprocessedObjC ||
89 IK == IK_PreprocessedObjCXX) {
Chris Lattner24943d22010-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 Claytone41c4b22010-06-13 17:34:29 +000098 case IK_None:
99 case IK_AST:
Chris Lattner24943d22010-06-08 16:52:24 +0000100 assert(0 && "Invalid input kind!");
Greg Claytone41c4b22010-06-13 17:34:29 +0000101 case IK_OpenCL:
Chris Lattner24943d22010-06-08 16:52:24 +0000102 LangStd = LangStandard::lang_opencl;
103 break;
Greg Claytone41c4b22010-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 Lattner24943d22010-06-08 16:52:24 +0000109 LangStd = LangStandard::lang_gnu99;
110 break;
Greg Claytone41c4b22010-06-13 17:34:29 +0000111 case IK_CXX:
112 case IK_PreprocessedCXX:
113 case IK_ObjCXX:
114 case IK_PreprocessedObjCXX:
Chris Lattner24943d22010-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 Lattner24943d22010-06-08 16:52:24 +0000236
Chris Lattner24943d22010-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 Clayton6e713402010-07-30 20:30:44 +0000310 *getBuiltinContext(),
311 0));
Chris Lattner24943d22010-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 Claytone41c4b22010-06-13 17:34:29 +0000338 ParseLangArgs(*m_language_options_ap, IK_ObjCXX);
339// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
Chris Lattner24943d22010-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 Clayton1674b122010-07-21 22:12:05 +0000352clang::SourceManager *
Chris Lattner24943d22010-06-08 16:52:24 +0000353ClangASTContext::getSourceManager()
354{
355 if (m_source_manager_ap.get() == NULL)
Greg Clayton1674b122010-07-21 22:12:05 +0000356 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnostic()));
Chris Lattner24943d22010-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 Clayton585660c2010-08-05 01:57:25 +0000402ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size)
Chris Lattner24943d22010-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 Clayton585660c2010-08-05 01:57:25 +0000412ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (clang::ASTContext *ast_context, Encoding encoding, uint32_t bit_size)
Chris Lattner24943d22010-06-08 16:52:24 +0000413{
414 if (!ast_context)
415 return NULL;
416
417 switch (encoding)
418 {
Greg Clayton585660c2010-08-05 01:57:25 +0000419 case eEncodingInvalid:
Chris Lattner24943d22010-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 Clayton585660c2010-08-05 01:57:25 +0000424 case eEncodingUint:
Chris Lattner24943d22010-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 Clayton585660c2010-08-05 01:57:25 +0000439 case eEncodingSint:
Chris Lattner24943d22010-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 Clayton585660c2010-08-05 01:57:25 +0000454 case eEncodingIEEE754:
Chris Lattner24943d22010-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 Clayton585660c2010-08-05 01:57:25 +0000463 case eEncodingVector:
Chris Lattner24943d22010-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 Callanana751f7b2010-09-17 02:24:29 +0000661ClangASTContext::GetBuiltInType_void(clang::ASTContext *ast_context)
Chris Lattner24943d22010-06-08 16:52:24 +0000662{
Sean Callanana751f7b2010-09-17 02:24:29 +0000663 return ast_context->VoidTy.getAsOpaquePtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000664}
665
666void *
Greg Clayton960d6a42010-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 Lattner24943d22010-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 Clayton84f80752010-07-22 18:30:50 +0000715 void *clang_type)
Chris Lattner24943d22010-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 Callanan8d825062010-07-16 00:00:27 +0000726bool
727ClangASTContext::AreTypesSame(clang::ASTContext *ast_context,
Sean Callanan5510ddd2010-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 Lattner24943d22010-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 Clayton585660c2010-08-05 01:57:25 +0000776ClangASTContext::CreateRecordType (const char *name, int kind, DeclContext *decl_ctx, LanguageType language)
Chris Lattner24943d22010-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 Clayton9488b742010-07-28 02:04:09 +0000784
Greg Clayton585660c2010-08-05 01:57:25 +0000785 if (language == eLanguageTypeObjC)
Greg Clayton9488b742010-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 Lattner24943d22010-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
Greg Clayton412440a2010-09-23 01:09:21 +0000806CXXMethodDecl *
Sean Callanan79523002010-09-17 02:58:26 +0000807ClangASTContext::AddMethodToCXXRecordType
808(
Greg Clayton412440a2010-09-23 01:09:21 +0000809 clang::ASTContext *ast_context,
810 void *record_opaque_type,
811 const char *name,
812 void *method_opaque_type,
813 lldb::AccessType access,
Greg Clayton1d8173f2010-09-24 05:15:53 +0000814 bool is_virtual,
815 bool is_static,
816 bool is_inline
Greg Clayton412440a2010-09-23 01:09:21 +0000817)
Sean Callanan79523002010-09-17 02:58:26 +0000818{
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000819 if (!record_opaque_type || !method_opaque_type || !name)
Johnny Chen974fddb2010-09-28 16:10:54 +0000820 return NULL;
Sean Callanan79523002010-09-17 02:58:26 +0000821
822 assert(ast_context);
823
824 IdentifierTable *identifier_table = &ast_context->Idents;
825
826 assert(identifier_table);
827
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000828 QualType record_qual_type(QualType::getFromOpaquePtr(record_opaque_type));
Greg Clayton1d8173f2010-09-24 05:15:53 +0000829
830 clang::Type *clang_type(record_qual_type.getTypePtr());
Sean Callanan79523002010-09-17 02:58:26 +0000831
Greg Clayton1d8173f2010-09-24 05:15:53 +0000832 if (clang_type == NULL)
Greg Clayton412440a2010-09-23 01:09:21 +0000833 return NULL;
Sean Callanan79523002010-09-17 02:58:26 +0000834
Greg Clayton1d8173f2010-09-24 05:15:53 +0000835 RecordType *record_clang_type(dyn_cast<RecordType>(clang_type));
Sean Callanan79523002010-09-17 02:58:26 +0000836
Greg Clayton1d8173f2010-09-24 05:15:53 +0000837 if (record_clang_type == NULL)
Greg Clayton412440a2010-09-23 01:09:21 +0000838 return NULL;
Sean Callanan79523002010-09-17 02:58:26 +0000839
Greg Clayton1d8173f2010-09-24 05:15:53 +0000840 RecordDecl *record_decl = record_clang_type->getDecl();
Sean Callanan79523002010-09-17 02:58:26 +0000841
Greg Clayton1d8173f2010-09-24 05:15:53 +0000842 if (record_decl == NULL)
Greg Clayton412440a2010-09-23 01:09:21 +0000843 return NULL;
Sean Callanan79523002010-09-17 02:58:26 +0000844
845 CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
846
Greg Clayton1d8173f2010-09-24 05:15:53 +0000847 if (cxx_record_decl == NULL)
Greg Clayton412440a2010-09-23 01:09:21 +0000848 return NULL;
Sean Callanan79523002010-09-17 02:58:26 +0000849
Greg Clayton1d8173f2010-09-24 05:15:53 +0000850 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000851
Greg Clayton1d8173f2010-09-24 05:15:53 +0000852 CXXMethodDecl *cxx_method_decl = CXXMethodDecl::Create (*ast_context,
853 cxx_record_decl,
854 DeclarationNameInfo (DeclarationName (&identifier_table->get(name)), SourceLocation()),
855 method_qual_type,
856 NULL, // TypeSourceInfo *
857 is_static,
858 SC_None,
859 is_inline);
Sean Callanan79523002010-09-17 02:58:26 +0000860
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000861
Greg Clayton1d8173f2010-09-24 05:15:53 +0000862 clang::AccessSpecifier access_specifier = ConvertAccessTypeToAccessSpecifier (access);
863
864 cxx_method_decl->setAccess (access_specifier);
865 cxx_method_decl->setVirtualAsWritten (is_virtual);
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000866
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000867 // Populate the method decl with parameter decls
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000868 clang::Type *method_type(method_qual_type.getTypePtr());
869
Greg Clayton1d8173f2010-09-24 05:15:53 +0000870 if (method_type == NULL)
Greg Clayton412440a2010-09-23 01:09:21 +0000871 return NULL;
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000872
Greg Clayton1d8173f2010-09-24 05:15:53 +0000873 FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type));
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000874
Greg Clayton1d8173f2010-09-24 05:15:53 +0000875 if (!method_function_prototype)
Greg Clayton412440a2010-09-23 01:09:21 +0000876 return NULL;
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000877
Greg Clayton1d8173f2010-09-24 05:15:53 +0000878 unsigned int num_params = method_function_prototype->getNumArgs();
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000879
880 ParmVarDecl *params[num_params];
881
882 for (int param_index = 0;
883 param_index < num_params;
884 ++param_index)
885 {
Greg Clayton1d8173f2010-09-24 05:15:53 +0000886 params[param_index] = ParmVarDecl::Create (*ast_context,
887 cxx_method_decl,
888 SourceLocation(),
889 NULL, // anonymous
890 method_function_prototype->getArgType(param_index),
891 NULL,
892 SC_None,
893 SC_None,
894 NULL);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000895 }
896
Greg Clayton1d8173f2010-09-24 05:15:53 +0000897 cxx_method_decl->setParams (params, num_params);
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000898
Greg Clayton1d8173f2010-09-24 05:15:53 +0000899 cxx_record_decl->addDecl (cxx_method_decl);
Sean Callanan79523002010-09-17 02:58:26 +0000900
Greg Clayton412440a2010-09-23 01:09:21 +0000901 return cxx_method_decl;
Sean Callanan79523002010-09-17 02:58:26 +0000902}
903
904bool
Greg Clayton84f80752010-07-22 18:30:50 +0000905ClangASTContext::AddFieldToRecordType
906(
Sean Callanan60a0ced2010-09-16 20:01:08 +0000907 clang::ASTContext *ast_context,
Greg Clayton84f80752010-07-22 18:30:50 +0000908 void *record_clang_type,
909 const char *name,
910 void *field_type,
911 AccessType access,
912 uint32_t bitfield_bit_size
913)
Chris Lattner24943d22010-06-08 16:52:24 +0000914{
915 if (record_clang_type == NULL || field_type == NULL)
916 return false;
917
Sean Callanan60a0ced2010-09-16 20:01:08 +0000918 IdentifierTable *identifier_table = &ast_context->Idents;
Chris Lattner24943d22010-06-08 16:52:24 +0000919
920 assert (ast_context != NULL);
921 assert (identifier_table != NULL);
922
923 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type));
924
Greg Clayton1674b122010-07-21 22:12:05 +0000925 clang::Type *clang_type = record_qual_type.getTypePtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000926 if (clang_type)
927 {
928 const RecordType *record_type = dyn_cast<RecordType>(clang_type);
929
930 if (record_type)
931 {
932 RecordDecl *record_decl = record_type->getDecl();
933
934 CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
935 if (cxx_record_decl)
Greg Claytona4e27af2010-09-22 00:40:49 +0000936 {
937 // NOTE: we currently have some fixes that should be placed
938 // into clang that will automatically set if a record is empty
939 // when each field is added (during the addDecl() method call
940 // below) so this code should be able to come out when those
941 // changes make it into llvm/clang, then we can remove this
942 // code...
943 // Currently SEMA is using the accessors manually to set
944 // whether a class is empty, is POD, is aggregate, and more.
945 // This code will be moved into CXXRecordDecl so everyone
946 // can benefit.
947 // This will currently work for everything except zero sized
948 // bitfields which we currently aren't detecting anyway from the
949 // DWARF so it should be ok for now.
Chris Lattner24943d22010-06-08 16:52:24 +0000950 cxx_record_decl->setEmpty (false);
Greg Claytona4e27af2010-09-22 00:40:49 +0000951 }
Chris Lattner24943d22010-06-08 16:52:24 +0000952
953 clang::Expr *bit_width = NULL;
954 if (bitfield_bit_size != 0)
955 {
956 APInt bitfield_bit_size_apint(ast_context->getTypeSize(ast_context->IntTy), bitfield_bit_size);
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000957 bit_width = new (*ast_context)IntegerLiteral (*ast_context, bitfield_bit_size_apint, ast_context->IntTy, SourceLocation());
Chris Lattner24943d22010-06-08 16:52:24 +0000958 }
Greg Clayton84f80752010-07-22 18:30:50 +0000959 FieldDecl *field = FieldDecl::Create (*ast_context,
960 record_decl,
961 SourceLocation(),
962 name ? &identifier_table->get(name) : NULL, // Identifier
963 QualType::getFromOpaquePtr(field_type), // Field type
964 NULL, // DeclaratorInfo *
965 bit_width, // BitWidth
966 false); // Mutable
Chris Lattner24943d22010-06-08 16:52:24 +0000967
Greg Clayton84f80752010-07-22 18:30:50 +0000968 field->setAccess (ConvertAccessTypeToAccessSpecifier (access));
Chris Lattner24943d22010-06-08 16:52:24 +0000969
970 if (field)
971 {
972 record_decl->addDecl(field);
Greg Claytona4e27af2010-09-22 00:40:49 +0000973
974 // NOTE: we currently have some fixes that should be placed
975 // into clang that will automatically set if a record is POD
976 // when each field is added (during the addDecl() method call
977 // above) so this code should be able to come out when those
978 // changes make it into llvm/clang, then we can remove this
979 // code...
980 // Currently SEMA is using the accessors manually to set
981 // whether a class is empty, is POD, is aggregate, and more.
982 // This code will be moved into CXXRecordDecl so everyone
983 // can benefit.
984
Greg Clayton22d5fe32010-09-22 00:24:45 +0000985 if (cxx_record_decl->isPOD())
986 {
987 if (!field->getType()->isPODType())
988 cxx_record_decl->setPOD (false);
989 return true;
990 }
Chris Lattner24943d22010-06-08 16:52:24 +0000991 }
992 }
Greg Clayton9488b742010-07-28 02:04:09 +0000993 else
994 {
995 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(clang_type);
996 if (objc_class_type)
997 {
Greg Clayton1d8173f2010-09-24 05:15:53 +0000998 bool is_synthesized = false;
Sean Callanan60a0ced2010-09-16 20:01:08 +0000999 ClangASTContext::AddObjCClassIVar (ast_context,
1000 record_clang_type,
Greg Clayton9488b742010-07-28 02:04:09 +00001001 name,
1002 field_type,
1003 access,
1004 bitfield_bit_size,
Greg Clayton1d8173f2010-09-24 05:15:53 +00001005 is_synthesized);
Greg Clayton9488b742010-07-28 02:04:09 +00001006 }
1007 }
Chris Lattner24943d22010-06-08 16:52:24 +00001008 }
1009 return false;
1010}
1011
1012bool
1013ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
1014{
1015 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1016}
1017
1018bool
1019ClangASTContext::FieldIsBitfield
1020(
1021 ASTContext *ast_context,
1022 FieldDecl* field,
1023 uint32_t& bitfield_bit_size
1024)
1025{
1026 if (ast_context == NULL || field == NULL)
1027 return false;
1028
1029 if (field->isBitField())
1030 {
1031 Expr* bit_width_expr = field->getBitWidth();
1032 if (bit_width_expr)
1033 {
1034 llvm::APSInt bit_width_apsint;
1035 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast_context))
1036 {
1037 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
1038 return true;
1039 }
1040 }
1041 }
1042 return false;
1043}
1044
1045bool
1046ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
1047{
1048 if (record_decl == NULL)
1049 return false;
1050
1051 if (!record_decl->field_empty())
1052 return true;
1053
1054 // No fields, lets check this is a CXX record and check the base classes
1055 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1056 if (cxx_record_decl)
1057 {
1058 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1059 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1060 base_class != base_class_end;
1061 ++base_class)
1062 {
1063 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1064 if (RecordHasFields(base_class_decl))
1065 return true;
1066 }
1067 }
1068 return false;
1069}
1070
1071void
1072ClangASTContext::SetDefaultAccessForRecordFields (void *clang_qual_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities)
1073{
1074 if (clang_qual_type)
1075 {
1076 QualType qual_type(QualType::getFromOpaquePtr(clang_qual_type));
Greg Clayton1674b122010-07-21 22:12:05 +00001077 clang::Type *clang_type = qual_type.getTypePtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001078 if (clang_type)
1079 {
1080 RecordType *record_type = dyn_cast<RecordType>(clang_type);
1081 if (record_type)
1082 {
1083 RecordDecl *record_decl = record_type->getDecl();
1084 if (record_decl)
1085 {
1086 uint32_t field_idx;
1087 RecordDecl::field_iterator field, field_end;
1088 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
1089 field != field_end;
1090 ++field, ++field_idx)
1091 {
1092 // If no accessibility was assigned, assign the correct one
1093 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
1094 field->setAccess ((AccessSpecifier)default_accessibility);
1095 }
1096 }
1097 }
1098 }
1099 }
1100}
1101
1102#pragma mark C++ Base Classes
1103
1104CXXBaseSpecifier *
Greg Clayton84f80752010-07-22 18:30:50 +00001105ClangASTContext::CreateBaseClassSpecifier (void *base_class_type, AccessType access, bool is_virtual, bool base_of_class)
Chris Lattner24943d22010-06-08 16:52:24 +00001106{
1107 if (base_class_type)
Greg Clayton6e713402010-07-30 20:30:44 +00001108 return new CXXBaseSpecifier (SourceRange(),
1109 is_virtual,
1110 base_of_class,
1111 ConvertAccessTypeToAccessSpecifier (access),
1112 getASTContext()->CreateTypeSourceInfo (QualType::getFromOpaquePtr(base_class_type)));
Chris Lattner24943d22010-06-08 16:52:24 +00001113 return NULL;
1114}
1115
Greg Claytone9d0df42010-07-02 01:29:13 +00001116void
1117ClangASTContext::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes)
1118{
1119 for (unsigned i=0; i<num_base_classes; ++i)
1120 {
1121 delete base_classes[i];
1122 base_classes[i] = NULL;
1123 }
1124}
1125
Chris Lattner24943d22010-06-08 16:52:24 +00001126bool
1127ClangASTContext::SetBaseClassesForClassType (void *class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes)
1128{
1129 if (class_clang_type)
1130 {
Greg Clayton1674b122010-07-21 22:12:05 +00001131 clang::Type *clang_type = QualType::getFromOpaquePtr(class_clang_type).getTypePtr();
Chris Lattner24943d22010-06-08 16:52:24 +00001132 if (clang_type)
1133 {
1134 RecordType *record_type = dyn_cast<RecordType>(clang_type);
1135 if (record_type)
1136 {
1137 CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_type->getDecl());
1138 if (cxx_record_decl)
1139 {
Chris Lattner24943d22010-06-08 16:52:24 +00001140 cxx_record_decl->setBases(base_classes, num_base_classes);
Greg Claytonbba60fb2010-09-21 21:22:23 +00001141
Greg Claytona4e27af2010-09-22 00:40:49 +00001142 // NOTE: we currently have some fixes that should be placed
1143 // into clang that will automatically set these things when
1144 // they are added (during the setBases() method call above)
1145 // so this code should be able to come out when those changes
1146 // make it into llvm/clang, then we can remove this code...
1147 // Currently SEMA is using the accessors manually to set
1148 // whether a class is empty, is POD, is aggregate, and more.
1149 // This code will be moved into CXXRecordDecl so everyone
1150 // can benefit.
Greg Clayton22d5fe32010-09-22 00:24:45 +00001151 if (cxx_record_decl->isEmpty() || cxx_record_decl->isPOD())
Greg Claytonbba60fb2010-09-21 21:22:23 +00001152 {
1153 // set empty to false if any bases are virtual, or not empty.
1154
1155 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1156 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1157 base_class != base_class_end;
1158 ++base_class)
1159 {
1160 if (base_class->isVirtual())
1161 {
1162 cxx_record_decl->setEmpty (false);
Greg Clayton22d5fe32010-09-22 00:24:45 +00001163 cxx_record_decl->setPOD (false);
Greg Claytonbba60fb2010-09-21 21:22:23 +00001164 break;
1165 }
1166 else
1167 {
Greg Clayton22d5fe32010-09-22 00:24:45 +00001168 QualType base_type (base_class->getType());
1169
1170 if (!base_type->isPODType())
1171 cxx_record_decl->setPOD (false);
1172
1173 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_type->getAs<RecordType>()->getDecl());
1174 if (!base_class_decl->isEmpty())
1175 {
Greg Claytonbba60fb2010-09-21 21:22:23 +00001176 cxx_record_decl->setEmpty (false);
1177 break;
Greg Clayton22d5fe32010-09-22 00:24:45 +00001178 }
Greg Claytonbba60fb2010-09-21 21:22:23 +00001179 }
1180 }
1181 }
Chris Lattner24943d22010-06-08 16:52:24 +00001182 return true;
1183 }
1184 }
1185 }
1186 }
1187 return false;
1188}
Greg Clayton84f80752010-07-22 18:30:50 +00001189#pragma mark Objective C Classes
Chris Lattner24943d22010-06-08 16:52:24 +00001190
Greg Clayton84f80752010-07-22 18:30:50 +00001191void *
1192ClangASTContext::CreateObjCClass
1193(
1194 const char *name,
1195 DeclContext *decl_ctx,
1196 bool isForwardDecl,
1197 bool isInternal
1198)
1199{
1200 ASTContext *ast_context = getASTContext();
1201 assert (ast_context != NULL);
1202 assert (name && name[0]);
1203 if (decl_ctx == NULL)
1204 decl_ctx = ast_context->getTranslationUnitDecl();
1205
1206 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1207 // we will need to update this code. I was told to currently always use
1208 // the CXXRecordDecl class since we often don't know from debug information
1209 // if something is struct or a class, so we default to always use the more
1210 // complete definition just in case.
1211 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast_context,
1212 decl_ctx,
1213 SourceLocation(),
1214 &ast_context->Idents.get(name),
1215 SourceLocation(),
1216 isForwardDecl,
1217 isInternal);
Greg Clayton9488b742010-07-28 02:04:09 +00001218
1219 return ast_context->getObjCInterfaceType(decl).getAsOpaquePtr();
Greg Clayton84f80752010-07-22 18:30:50 +00001220}
1221
1222bool
1223ClangASTContext::SetObjCSuperClass (void *class_opaque_type, void *super_opaque_type)
1224{
1225 if (class_opaque_type && super_opaque_type)
1226 {
1227 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
1228 QualType super_qual_type(QualType::getFromOpaquePtr(super_opaque_type));
1229 clang::Type *class_type = class_qual_type.getTypePtr();
1230 clang::Type *super_type = super_qual_type.getTypePtr();
1231 if (class_type && super_type)
1232 {
1233 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
1234 ObjCObjectType *objc_super_type = dyn_cast<ObjCObjectType>(super_type);
1235 if (objc_class_type && objc_super_type)
1236 {
1237 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1238 ObjCInterfaceDecl *super_interface_decl = objc_super_type->getInterface();
1239 if (class_interface_decl && super_interface_decl)
1240 {
1241 class_interface_decl->setSuperClass(super_interface_decl);
1242 return true;
1243 }
1244 }
1245 }
1246 }
1247 return false;
1248}
1249
1250
1251bool
1252ClangASTContext::AddObjCClassIVar
1253(
Sean Callanan60a0ced2010-09-16 20:01:08 +00001254 clang::ASTContext *ast_context,
Greg Clayton84f80752010-07-22 18:30:50 +00001255 void *class_opaque_type,
1256 const char *name,
1257 void *ivar_opaque_type,
1258 AccessType access,
1259 uint32_t bitfield_bit_size,
Greg Clayton1d8173f2010-09-24 05:15:53 +00001260 bool is_synthesized
Greg Clayton84f80752010-07-22 18:30:50 +00001261)
1262{
1263 if (class_opaque_type == NULL || ivar_opaque_type == NULL)
1264 return false;
1265
Sean Callanan60a0ced2010-09-16 20:01:08 +00001266 IdentifierTable *identifier_table = &ast_context->Idents;
Greg Clayton84f80752010-07-22 18:30:50 +00001267
1268 assert (ast_context != NULL);
1269 assert (identifier_table != NULL);
1270
1271 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
1272
1273 clang::Type *class_type = class_qual_type.getTypePtr();
1274 if (class_type)
1275 {
1276 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
1277
1278 if (objc_class_type)
1279 {
1280 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1281
1282 if (class_interface_decl)
1283 {
1284 clang::Expr *bit_width = NULL;
1285 if (bitfield_bit_size != 0)
1286 {
1287 APInt bitfield_bit_size_apint(ast_context->getTypeSize(ast_context->IntTy), bitfield_bit_size);
Sean Callanan47a5c4c2010-09-23 03:01:22 +00001288 bit_width = new (*ast_context)IntegerLiteral (*ast_context, bitfield_bit_size_apint, ast_context->IntTy, SourceLocation());
Greg Clayton84f80752010-07-22 18:30:50 +00001289 }
1290
Greg Clayton9488b742010-07-28 02:04:09 +00001291 ObjCIvarDecl *field = ObjCIvarDecl::Create (*ast_context,
1292 class_interface_decl,
1293 SourceLocation(),
1294 &identifier_table->get(name), // Identifier
1295 QualType::getFromOpaquePtr(ivar_opaque_type), // Field type
1296 NULL, // TypeSourceInfo *
1297 ConvertAccessTypeToObjCIvarAccessControl (access),
1298 bit_width,
Greg Clayton1d8173f2010-09-24 05:15:53 +00001299 is_synthesized);
Greg Clayton9488b742010-07-28 02:04:09 +00001300
1301 if (field)
1302 {
1303 class_interface_decl->addDecl(field);
1304 return true;
1305 }
Greg Clayton84f80752010-07-22 18:30:50 +00001306 }
1307 }
1308 }
1309 return false;
1310}
Chris Lattner24943d22010-06-08 16:52:24 +00001311
Greg Clayton9488b742010-07-28 02:04:09 +00001312
1313bool
1314ClangASTContext::ObjCTypeHasIVars (void *class_opaque_type, bool check_superclass)
1315{
1316 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
1317
1318 clang::Type *class_type = class_qual_type.getTypePtr();
1319 if (class_type)
1320 {
1321 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
1322
1323 if (objc_class_type)
1324 return ObjCDeclHasIVars (objc_class_type->getInterface(), check_superclass);
1325 }
1326 return false;
1327}
1328
1329bool
1330ClangASTContext::ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
1331{
1332 while (class_interface_decl)
1333 {
1334 if (class_interface_decl->ivar_size() > 0)
1335 return true;
1336
1337 if (check_superclass)
1338 class_interface_decl = class_interface_decl->getSuperClass();
1339 else
1340 break;
1341 }
1342 return false;
1343}
Greg Clayton1d8173f2010-09-24 05:15:53 +00001344
1345clang::ObjCMethodDecl *
1346ClangASTContext::AddMethodToObjCObjectType
1347(
1348 clang::ASTContext *ast_context,
1349 void *class_opaque_type,
1350 const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
1351 void *method_opaque_type,
1352 lldb::AccessType access
1353)
1354{
1355 if (class_opaque_type == NULL || method_opaque_type == NULL)
1356 return NULL;
1357
1358 IdentifierTable *identifier_table = &ast_context->Idents;
1359
1360 assert (ast_context != NULL);
1361 assert (identifier_table != NULL);
1362
1363 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type));
1364
1365 clang::Type *class_type = class_qual_type.getTypePtr();
1366 if (class_type == NULL)
1367 return NULL;
1368
1369 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type);
1370
1371 if (objc_class_type == NULL)
1372 return NULL;
1373
1374 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1375
1376 if (class_interface_decl == NULL)
1377 return NULL;
Greg Clayton9488b742010-07-28 02:04:09 +00001378
Greg Clayton1d8173f2010-09-24 05:15:53 +00001379 const char *selector_start = ::strchr (name, ' ');
1380 if (selector_start == NULL)
1381 return NULL;
1382
1383 selector_start++;
1384 if (!(::isalpha (selector_start[0]) || selector_start[0] == '_'))
1385 return NULL;
1386 llvm::SmallVector<IdentifierInfo *, 12> selector_idents;
1387
1388 size_t len;
1389 const char *start;
1390 for (start = selector_start, len = ::strcspn(start, ":]");
1391 start && *start != '\0' && *start != ']';
1392 start += len + 1)
1393 {
1394 selector_idents.push_back (&identifier_table->get (StringRef (start, len)));
1395 }
1396
1397
1398 if (selector_idents.size() == 0)
1399 return 0;
1400
1401 clang::Selector method_selector = ast_context->Selectors.getSelector (selector_idents.size(),
1402 selector_idents.data());
1403
1404 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type));
1405
1406 // Populate the method decl with parameter decls
1407 clang::Type *method_type(method_qual_type.getTypePtr());
1408
1409 if (method_type == NULL)
1410 return NULL;
1411
1412 FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type));
1413
1414 if (!method_function_prototype)
1415 return NULL;
1416
1417
1418 bool is_variadic = false;
1419 bool is_synthesized = false;
1420 bool is_defined = false;
1421 ObjCMethodDecl::ImplementationControl imp_control = ObjCMethodDecl::None;
1422
1423 const unsigned num_args = method_function_prototype->getNumArgs();
1424
1425 ObjCMethodDecl *objc_method_decl = ObjCMethodDecl::Create (*ast_context,
1426 SourceLocation(), // beginLoc,
1427 SourceLocation(), // endLoc,
1428 method_selector,
1429 method_function_prototype->getResultType(),
1430 NULL, // TypeSourceInfo *ResultTInfo,
1431 GetDeclContextForType (class_opaque_type),
1432 name[0] == '-',
1433 is_variadic,
1434 is_synthesized,
1435 is_defined,
1436 imp_control,
1437 num_args);
1438
1439
1440 if (objc_method_decl == NULL)
1441 return NULL;
1442
1443 if (num_args > 0)
1444 {
1445 llvm::SmallVector<ParmVarDecl *, 12> params;
1446
1447 for (int param_index = 0; param_index < num_args; ++param_index)
1448 {
1449 params.push_back (ParmVarDecl::Create (*ast_context,
1450 objc_method_decl,
1451 SourceLocation(),
1452 NULL, // anonymous
1453 method_function_prototype->getArgType(param_index),
1454 NULL,
1455 SC_Auto,
1456 SC_Auto,
1457 NULL));
1458 }
1459
1460 objc_method_decl->setMethodParams(*ast_context, params.data(), params.size(), num_args);
1461 }
1462
1463 class_interface_decl->addDecl (objc_method_decl);
1464
1465
1466 return objc_method_decl;
1467}
1468
1469
Greg Clayton9488b742010-07-28 02:04:09 +00001470
Chris Lattner24943d22010-06-08 16:52:24 +00001471#pragma mark Aggregate Types
1472
1473bool
1474ClangASTContext::IsAggregateType (void *clang_type)
1475{
1476 if (clang_type == NULL)
1477 return false;
1478
1479 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
1480
1481 if (qual_type->isAggregateType ())
1482 return true;
1483
Greg Clayton03e0f972010-09-13 03:32:57 +00001484 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
1485 switch (type_class)
Chris Lattner24943d22010-06-08 16:52:24 +00001486 {
Greg Clayton1674b122010-07-21 22:12:05 +00001487 case clang::Type::IncompleteArray:
1488 case clang::Type::VariableArray:
1489 case clang::Type::ConstantArray:
1490 case clang::Type::ExtVector:
1491 case clang::Type::Vector:
1492 case clang::Type::Record:
Greg Clayton9488b742010-07-28 02:04:09 +00001493 case clang::Type::ObjCObject:
1494 case clang::Type::ObjCInterface:
Chris Lattner24943d22010-06-08 16:52:24 +00001495 return true;
1496
Greg Clayton1674b122010-07-21 22:12:05 +00001497 case clang::Type::Typedef:
Chris Lattner24943d22010-06-08 16:52:24 +00001498 return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr());
1499
1500 default:
1501 break;
1502 }
1503 // The clang type does have a value
1504 return false;
1505}
1506
1507uint32_t
1508ClangASTContext::GetNumChildren (void *clang_qual_type, bool omit_empty_base_classes)
1509{
1510 if (clang_qual_type == NULL)
1511 return 0;
1512
1513 uint32_t num_children = 0;
1514 QualType qual_type(QualType::getFromOpaquePtr(clang_qual_type));
Greg Clayton9488b742010-07-28 02:04:09 +00001515 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
1516 switch (type_class)
Chris Lattner24943d22010-06-08 16:52:24 +00001517 {
Greg Clayton960d6a42010-08-03 00:35:52 +00001518 case clang::Type::Builtin:
1519 switch (cast<clang::BuiltinType>(qual_type)->getKind())
1520 {
1521 case clang::BuiltinType::ObjCId: // Child is Class
1522 case clang::BuiltinType::ObjCClass: // child is Class
1523 case clang::BuiltinType::ObjCSel: // child is const char *
1524 num_children = 1;
1525
1526 default:
1527 break;
1528 }
1529 break;
1530
Greg Clayton1674b122010-07-21 22:12:05 +00001531 case clang::Type::Record:
Chris Lattner24943d22010-06-08 16:52:24 +00001532 {
1533 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
1534 const RecordDecl *record_decl = record_type->getDecl();
1535 assert(record_decl);
1536 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1537 if (cxx_record_decl)
1538 {
1539 if (omit_empty_base_classes)
1540 {
1541 // Check each base classes to see if it or any of its
1542 // base classes contain any fields. This can help
1543 // limit the noise in variable views by not having to
1544 // show base classes that contain no members.
1545 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1546 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1547 base_class != base_class_end;
1548 ++base_class)
1549 {
1550 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1551
1552 // Skip empty base classes
1553 if (RecordHasFields(base_class_decl) == false)
1554 continue;
1555
1556 num_children++;
1557 }
1558 }
1559 else
1560 {
1561 // Include all base classes
1562 num_children += cxx_record_decl->getNumBases();
1563 }
1564
1565 }
1566 RecordDecl::field_iterator field, field_end;
1567 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
1568 ++num_children;
1569 }
1570 break;
1571
Greg Clayton9488b742010-07-28 02:04:09 +00001572 case clang::Type::ObjCObject:
1573 case clang::Type::ObjCInterface:
1574 {
1575 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
1576 assert (objc_class_type);
1577 if (objc_class_type)
1578 {
1579 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1580
1581 if (class_interface_decl)
1582 {
1583
1584 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
1585 if (superclass_interface_decl)
1586 {
1587 if (omit_empty_base_classes)
1588 {
1589 if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true))
1590 ++num_children;
1591 }
1592 else
1593 ++num_children;
1594 }
1595
1596 num_children += class_interface_decl->ivar_size();
1597 }
1598 }
1599 }
1600 break;
1601
1602 case clang::Type::ObjCObjectPointer:
Greg Clayton960d6a42010-08-03 00:35:52 +00001603 {
1604 ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr());
1605 QualType pointee_type = pointer_type->getPointeeType();
1606 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (pointee_type.getAsOpaquePtr(),
1607 omit_empty_base_classes);
1608 // If this type points to a simple type, then it has 1 child
1609 if (num_pointee_children == 0)
1610 num_children = 1;
1611 else
1612 num_children = num_pointee_children;
1613 }
1614 break;
Greg Clayton9488b742010-07-28 02:04:09 +00001615
Greg Clayton1674b122010-07-21 22:12:05 +00001616 case clang::Type::ConstantArray:
Chris Lattner24943d22010-06-08 16:52:24 +00001617 num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
1618 break;
1619
Greg Clayton1674b122010-07-21 22:12:05 +00001620 case clang::Type::Pointer:
Chris Lattner24943d22010-06-08 16:52:24 +00001621 {
1622 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
1623 QualType pointee_type = pointer_type->getPointeeType();
Greg Clayton9488b742010-07-28 02:04:09 +00001624 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (pointee_type.getAsOpaquePtr(),
1625 omit_empty_base_classes);
Chris Lattner24943d22010-06-08 16:52:24 +00001626 // If this type points to a simple type, then it has 1 child
1627 if (num_pointee_children == 0)
1628 num_children = 1;
1629 else
1630 num_children = num_pointee_children;
1631 }
1632 break;
1633
Greg Clayton1674b122010-07-21 22:12:05 +00001634 case clang::Type::Typedef:
Chris Lattner24943d22010-06-08 16:52:24 +00001635 num_children = ClangASTContext::GetNumChildren (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), omit_empty_base_classes);
1636 break;
1637
1638 default:
1639 break;
1640 }
1641 return num_children;
1642}
1643
1644
1645void *
1646ClangASTContext::GetChildClangTypeAtIndex
1647(
1648 const char *parent_name,
1649 void *parent_clang_type,
1650 uint32_t idx,
1651 bool transparent_pointers,
1652 bool omit_empty_base_classes,
1653 std::string& child_name,
1654 uint32_t &child_byte_size,
1655 int32_t &child_byte_offset,
1656 uint32_t &child_bitfield_bit_size,
1657 uint32_t &child_bitfield_bit_offset
1658)
1659{
1660 if (parent_clang_type)
1661
1662 return GetChildClangTypeAtIndex (getASTContext(),
1663 parent_name,
1664 parent_clang_type,
1665 idx,
1666 transparent_pointers,
1667 omit_empty_base_classes,
1668 child_name,
1669 child_byte_size,
1670 child_byte_offset,
1671 child_bitfield_bit_size,
1672 child_bitfield_bit_offset);
1673 return NULL;
1674}
1675
1676void *
1677ClangASTContext::GetChildClangTypeAtIndex
1678(
1679 ASTContext *ast_context,
1680 const char *parent_name,
1681 void *parent_clang_type,
1682 uint32_t idx,
1683 bool transparent_pointers,
1684 bool omit_empty_base_classes,
1685 std::string& child_name,
1686 uint32_t &child_byte_size,
1687 int32_t &child_byte_offset,
1688 uint32_t &child_bitfield_bit_size,
1689 uint32_t &child_bitfield_bit_offset
1690)
1691{
1692 if (parent_clang_type == NULL)
1693 return NULL;
1694
1695 if (idx < ClangASTContext::GetNumChildren (parent_clang_type, omit_empty_base_classes))
1696 {
1697 uint32_t bit_offset;
1698 child_bitfield_bit_size = 0;
1699 child_bitfield_bit_offset = 0;
1700 QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type));
Greg Clayton03e0f972010-09-13 03:32:57 +00001701 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
1702 switch (parent_type_class)
Chris Lattner24943d22010-06-08 16:52:24 +00001703 {
Greg Clayton960d6a42010-08-03 00:35:52 +00001704 case clang::Type::Builtin:
1705 switch (cast<clang::BuiltinType>(parent_qual_type)->getKind())
1706 {
1707 case clang::BuiltinType::ObjCId:
1708 case clang::BuiltinType::ObjCClass:
1709 return ast_context->ObjCBuiltinClassTy.getAsOpaquePtr();
1710
1711 case clang::BuiltinType::ObjCSel:
1712 {
1713 QualType char_type(ast_context->CharTy);
1714 char_type.addConst();
1715 return ast_context->getPointerType(char_type).getAsOpaquePtr();
1716 }
1717 break;
1718
1719 default:
1720 break;
1721 }
1722 break;
1723
1724
Greg Clayton1674b122010-07-21 22:12:05 +00001725 case clang::Type::Record:
Chris Lattner24943d22010-06-08 16:52:24 +00001726 {
1727 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr());
1728 const RecordDecl *record_decl = record_type->getDecl();
1729 assert(record_decl);
1730 const ASTRecordLayout &record_layout = ast_context->getASTRecordLayout(record_decl);
1731 uint32_t child_idx = 0;
1732
1733 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1734 if (cxx_record_decl)
1735 {
1736 // We might have base classes to print out first
1737 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1738 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1739 base_class != base_class_end;
1740 ++base_class)
1741 {
1742 const CXXRecordDecl *base_class_decl = NULL;
1743
1744 // Skip empty base classes
1745 if (omit_empty_base_classes)
1746 {
1747 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1748 if (RecordHasFields(base_class_decl) == false)
1749 continue;
1750 }
1751
1752 if (idx == child_idx)
1753 {
1754 if (base_class_decl == NULL)
1755 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1756
1757
1758 if (base_class->isVirtual())
1759 bit_offset = record_layout.getVBaseClassOffset(base_class_decl);
1760 else
1761 bit_offset = record_layout.getBaseClassOffset(base_class_decl);
1762
1763 // Base classes should be a multiple of 8 bits in size
1764 assert (bit_offset % 8 == 0);
1765 child_byte_offset = bit_offset/8;
1766 std::string base_class_type_name(base_class->getType().getAsString());
1767
1768 child_name.assign(base_class_type_name.c_str());
1769
1770 uint64_t clang_type_info_bit_size = ast_context->getTypeSize(base_class->getType());
1771
1772 // Base classes biut sizes should be a multiple of 8 bits in size
1773 assert (clang_type_info_bit_size % 8 == 0);
1774 child_byte_size = clang_type_info_bit_size / 8;
1775 return base_class->getType().getAsOpaquePtr();
1776 }
1777 // We don't increment the child index in the for loop since we might
1778 // be skipping empty base classes
1779 ++child_idx;
1780 }
1781 }
Chris Lattner24943d22010-06-08 16:52:24 +00001782 // Make sure index is in range...
1783 uint32_t field_idx = 0;
1784 RecordDecl::field_iterator field, field_end;
1785 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
1786 {
1787 if (idx == child_idx)
1788 {
1789 // Print the member type if requested
1790 // Print the member name and equal sign
1791 child_name.assign(field->getNameAsString().c_str());
1792
1793 // Figure out the type byte size (field_type_info.first) and
1794 // alignment (field_type_info.second) from the AST context.
1795 std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(field->getType());
Greg Clayton54e7afa2010-07-09 20:39:50 +00001796 assert(field_idx < record_layout.getFieldCount());
Chris Lattner24943d22010-06-08 16:52:24 +00001797
1798 child_byte_size = field_type_info.first / 8;
1799
1800 // Figure out the field offset within the current struct/union/class type
1801 bit_offset = record_layout.getFieldOffset (field_idx);
1802 child_byte_offset = bit_offset / 8;
1803 if (ClangASTContext::FieldIsBitfield (ast_context, *field, child_bitfield_bit_size))
1804 child_bitfield_bit_offset = bit_offset % 8;
1805
1806 return field->getType().getAsOpaquePtr();
1807 }
1808 }
1809 }
1810 break;
1811
Greg Clayton9488b742010-07-28 02:04:09 +00001812 case clang::Type::ObjCObject:
1813 case clang::Type::ObjCInterface:
1814 {
1815 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr());
1816 assert (objc_class_type);
1817 if (objc_class_type)
1818 {
1819 uint32_t child_idx = 0;
1820 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
1821
1822 if (class_interface_decl)
1823 {
1824
1825 const ASTRecordLayout &interface_layout = ast_context->getASTObjCInterfaceLayout(class_interface_decl);
1826 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
1827 if (superclass_interface_decl)
1828 {
1829 if (omit_empty_base_classes)
1830 {
Greg Clayton960d6a42010-08-03 00:35:52 +00001831 if (ClangASTContext::GetNumChildren(ast_context->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0)
Greg Clayton9488b742010-07-28 02:04:09 +00001832 {
1833 if (idx == 0)
1834 {
1835 QualType ivar_qual_type(ast_context->getObjCInterfaceType(superclass_interface_decl));
1836
1837
1838 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
1839
1840 std::pair<uint64_t, unsigned> ivar_type_info = ast_context->getTypeInfo(ivar_qual_type.getTypePtr());
1841
1842 child_byte_size = ivar_type_info.first / 8;
Greg Clayton960d6a42010-08-03 00:35:52 +00001843 child_byte_offset = 0;
Greg Clayton9488b742010-07-28 02:04:09 +00001844
1845 return ivar_qual_type.getAsOpaquePtr();
1846 }
1847
1848 ++child_idx;
1849 }
1850 }
1851 else
1852 ++child_idx;
1853 }
Greg Clayton960d6a42010-08-03 00:35:52 +00001854
1855 const uint32_t superclass_idx = child_idx;
Greg Clayton9488b742010-07-28 02:04:09 +00001856
1857 if (idx < (child_idx + class_interface_decl->ivar_size()))
1858 {
1859 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
1860
1861 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
1862 {
1863 if (child_idx == idx)
1864 {
1865 const ObjCIvarDecl* ivar_decl = *ivar_pos;
1866
1867 QualType ivar_qual_type(ivar_decl->getType());
1868
1869 child_name.assign(ivar_decl->getNameAsString().c_str());
1870
1871 std::pair<uint64_t, unsigned> ivar_type_info = ast_context->getTypeInfo(ivar_qual_type.getTypePtr());
1872
1873 child_byte_size = ivar_type_info.first / 8;
1874
1875 // Figure out the field offset within the current struct/union/class type
Greg Clayton960d6a42010-08-03 00:35:52 +00001876 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
Greg Clayton9488b742010-07-28 02:04:09 +00001877 child_byte_offset = bit_offset / 8;
1878
1879 return ivar_qual_type.getAsOpaquePtr();
1880 }
1881 ++child_idx;
1882 }
1883 }
1884 }
1885 }
1886 }
1887 break;
1888
1889 case clang::Type::ObjCObjectPointer:
1890 {
Greg Clayton960d6a42010-08-03 00:35:52 +00001891 ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr());
1892 QualType pointee_type = pointer_type->getPointeeType();
1893
1894 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
1895 {
1896 return GetChildClangTypeAtIndex (ast_context,
1897 parent_name,
1898 pointer_type->getPointeeType().getAsOpaquePtr(),
1899 idx,
1900 transparent_pointers,
1901 omit_empty_base_classes,
1902 child_name,
1903 child_byte_size,
1904 child_byte_offset,
1905 child_bitfield_bit_size,
1906 child_bitfield_bit_offset);
1907 }
1908 else
1909 {
1910 if (parent_name)
1911 {
1912 child_name.assign(1, '*');
1913 child_name += parent_name;
1914 }
1915
1916 // We have a pointer to an simple type
1917 if (idx == 0)
1918 {
1919 std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type);
1920 assert(clang_type_info.first % 8 == 0);
1921 child_byte_size = clang_type_info.first / 8;
1922 child_byte_offset = 0;
1923 return pointee_type.getAsOpaquePtr();
1924 }
1925 }
Greg Clayton9488b742010-07-28 02:04:09 +00001926 }
1927 break;
1928
Greg Clayton1674b122010-07-21 22:12:05 +00001929 case clang::Type::ConstantArray:
Chris Lattner24943d22010-06-08 16:52:24 +00001930 {
1931 const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
1932 const uint64_t element_count = array->getSize().getLimitedValue();
1933
1934 if (idx < element_count)
1935 {
1936 std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(array->getElementType());
1937
1938 char element_name[32];
1939 ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
1940
1941 child_name.assign(element_name);
1942 assert(field_type_info.first % 8 == 0);
1943 child_byte_size = field_type_info.first / 8;
1944 child_byte_offset = idx * child_byte_size;
1945 return array->getElementType().getAsOpaquePtr();
1946 }
1947 }
1948 break;
1949
Greg Clayton1674b122010-07-21 22:12:05 +00001950 case clang::Type::Pointer:
Chris Lattner24943d22010-06-08 16:52:24 +00001951 {
1952 PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr());
1953 QualType pointee_type = pointer_type->getPointeeType();
1954
1955 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
1956 {
1957 return GetChildClangTypeAtIndex (ast_context,
1958 parent_name,
1959 pointer_type->getPointeeType().getAsOpaquePtr(),
1960 idx,
1961 transparent_pointers,
1962 omit_empty_base_classes,
1963 child_name,
1964 child_byte_size,
1965 child_byte_offset,
1966 child_bitfield_bit_size,
1967 child_bitfield_bit_offset);
1968 }
1969 else
1970 {
1971 if (parent_name)
1972 {
1973 child_name.assign(1, '*');
1974 child_name += parent_name;
1975 }
1976
1977 // We have a pointer to an simple type
1978 if (idx == 0)
1979 {
1980 std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type);
1981 assert(clang_type_info.first % 8 == 0);
1982 child_byte_size = clang_type_info.first / 8;
1983 child_byte_offset = 0;
1984 return pointee_type.getAsOpaquePtr();
1985 }
1986 }
1987 }
1988 break;
1989
Greg Clayton1674b122010-07-21 22:12:05 +00001990 case clang::Type::Typedef:
Chris Lattner24943d22010-06-08 16:52:24 +00001991 return GetChildClangTypeAtIndex (ast_context,
1992 parent_name,
1993 cast<TypedefType>(parent_qual_type)->LookThroughTypedefs().getAsOpaquePtr(),
1994 idx,
1995 transparent_pointers,
1996 omit_empty_base_classes,
1997 child_name,
1998 child_byte_size,
1999 child_byte_offset,
2000 child_bitfield_bit_size,
2001 child_bitfield_bit_offset);
2002 break;
2003
2004 default:
2005 break;
2006 }
2007 }
Greg Claytonf8e98a62010-07-23 15:37:46 +00002008 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +00002009}
2010
2011static inline bool
2012BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
2013{
2014 return ClangASTContext::RecordHasFields(cast<CXXRecordDecl>(b->getType()->getAs<RecordType>()->getDecl())) == false;
2015}
2016
2017static uint32_t
2018GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
2019{
2020 uint32_t num_bases = 0;
2021 if (cxx_record_decl)
2022 {
2023 if (omit_empty_base_classes)
2024 {
2025 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2026 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2027 base_class != base_class_end;
2028 ++base_class)
2029 {
2030 // Skip empty base classes
2031 if (omit_empty_base_classes)
2032 {
2033 if (BaseSpecifierIsEmpty (base_class))
2034 continue;
2035 }
2036 ++num_bases;
2037 }
2038 }
2039 else
2040 num_bases = cxx_record_decl->getNumBases();
2041 }
2042 return num_bases;
2043}
2044
2045
2046static uint32_t
2047GetIndexForRecordBase
2048(
2049 const RecordDecl *record_decl,
2050 const CXXBaseSpecifier *base_spec,
2051 bool omit_empty_base_classes
2052)
2053{
2054 uint32_t child_idx = 0;
2055
2056 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2057
2058// const char *super_name = record_decl->getNameAsCString();
2059// const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString();
2060// printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
2061//
2062 if (cxx_record_decl)
2063 {
2064 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2065 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2066 base_class != base_class_end;
2067 ++base_class)
2068 {
2069 if (omit_empty_base_classes)
2070 {
2071 if (BaseSpecifierIsEmpty (base_class))
2072 continue;
2073 }
2074
2075// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
2076// child_idx,
2077// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
2078//
2079//
2080 if (base_class == base_spec)
2081 return child_idx;
2082 ++child_idx;
2083 }
2084 }
2085
2086 return UINT32_MAX;
2087}
2088
2089
2090static uint32_t
2091GetIndexForRecordChild
2092(
2093 const RecordDecl *record_decl,
2094 NamedDecl *canonical_decl,
2095 bool omit_empty_base_classes
2096)
2097{
2098 uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes);
2099
2100// const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2101//
2102//// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString());
2103// if (cxx_record_decl)
2104// {
2105// CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2106// for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2107// base_class != base_class_end;
2108// ++base_class)
2109// {
2110// if (omit_empty_base_classes)
2111// {
2112// if (BaseSpecifierIsEmpty (base_class))
2113// continue;
2114// }
2115//
2116//// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
2117//// record_decl->getNameAsCString(),
2118//// canonical_decl->getNameAsCString(),
2119//// child_idx,
2120//// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
2121//
2122//
2123// CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2124// if (curr_base_class_decl == canonical_decl)
2125// {
2126// return child_idx;
2127// }
2128// ++child_idx;
2129// }
2130// }
2131//
2132// const uint32_t num_bases = child_idx;
2133 RecordDecl::field_iterator field, field_end;
2134 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
2135 field != field_end;
2136 ++field, ++child_idx)
2137 {
2138// printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n",
2139// record_decl->getNameAsCString(),
2140// canonical_decl->getNameAsCString(),
2141// child_idx - num_bases,
2142// field->getNameAsCString());
2143
2144 if (field->getCanonicalDecl() == canonical_decl)
2145 return child_idx;
2146 }
2147
2148 return UINT32_MAX;
2149}
2150
2151// Look for a child member (doesn't include base classes, but it does include
2152// their members) in the type hierarchy. Returns an index path into "clang_type"
2153// on how to reach the appropriate member.
2154//
2155// class A
2156// {
2157// public:
2158// int m_a;
2159// int m_b;
2160// };
2161//
2162// class B
2163// {
2164// };
2165//
2166// class C :
2167// public B,
2168// public A
2169// {
2170// };
2171//
2172// If we have a clang type that describes "class C", and we wanted to looked
2173// "m_b" in it:
2174//
2175// With omit_empty_base_classes == false we would get an integer array back with:
2176// { 1, 1 }
2177// The first index 1 is the child index for "class A" within class C
2178// The second index 1 is the child index for "m_b" within class A
2179//
2180// With omit_empty_base_classes == true we would get an integer array back with:
2181// { 0, 1 }
2182// 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)
2183// The second index 1 is the child index for "m_b" within class A
2184
2185size_t
2186ClangASTContext::GetIndexOfChildMemberWithName
2187(
2188 ASTContext *ast_context,
2189 void *clang_type,
2190 const char *name,
2191 bool omit_empty_base_classes,
2192 std::vector<uint32_t>& child_indexes
2193)
2194{
2195 if (clang_type && name && name[0])
2196 {
2197 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton03e0f972010-09-13 03:32:57 +00002198 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2199 switch (type_class)
Chris Lattner24943d22010-06-08 16:52:24 +00002200 {
Greg Clayton1674b122010-07-21 22:12:05 +00002201 case clang::Type::Record:
Chris Lattner24943d22010-06-08 16:52:24 +00002202 {
2203 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
2204 const RecordDecl *record_decl = record_type->getDecl();
2205
2206 assert(record_decl);
2207 uint32_t child_idx = 0;
2208
2209 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2210
2211 // Try and find a field that matches NAME
2212 RecordDecl::field_iterator field, field_end;
2213 StringRef name_sref(name);
2214 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
2215 field != field_end;
2216 ++field, ++child_idx)
2217 {
2218 if (field->getName().equals (name_sref))
2219 {
2220 // We have to add on the number of base classes to this index!
2221 child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
2222 return child_indexes.size();
2223 }
2224 }
2225
2226 if (cxx_record_decl)
2227 {
2228 const RecordDecl *parent_record_decl = cxx_record_decl;
2229
2230 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
2231
2232 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
2233 // Didn't find things easily, lets let clang do its thang...
2234 IdentifierInfo & ident_ref = ast_context->Idents.get(name, name + strlen (name));
2235 DeclarationName decl_name(&ident_ref);
2236
2237 CXXBasePaths paths;
2238 if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember,
2239 decl_name.getAsOpaquePtr(),
2240 paths))
2241 {
Chris Lattner24943d22010-06-08 16:52:24 +00002242 CXXBasePaths::const_paths_iterator path, path_end = paths.end();
2243 for (path = paths.begin(); path != path_end; ++path)
2244 {
2245 const size_t num_path_elements = path->size();
2246 for (size_t e=0; e<num_path_elements; ++e)
2247 {
2248 CXXBasePathElement elem = (*path)[e];
2249
2250 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
2251 if (child_idx == UINT32_MAX)
2252 {
2253 child_indexes.clear();
2254 return 0;
2255 }
2256 else
2257 {
2258 child_indexes.push_back (child_idx);
2259 parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl());
2260 }
2261 }
2262 DeclContext::lookup_iterator named_decl_pos;
2263 for (named_decl_pos = path->Decls.first;
2264 named_decl_pos != path->Decls.second && parent_record_decl;
2265 ++named_decl_pos)
2266 {
2267 //printf ("path[%zu] = %s\n", child_indexes.size(), (*named_decl_pos)->getNameAsCString());
2268
2269 child_idx = GetIndexForRecordChild (parent_record_decl, *named_decl_pos, omit_empty_base_classes);
2270 if (child_idx == UINT32_MAX)
2271 {
2272 child_indexes.clear();
2273 return 0;
2274 }
2275 else
2276 {
2277 child_indexes.push_back (child_idx);
2278 }
2279 }
2280 }
2281 return child_indexes.size();
2282 }
2283 }
2284
2285 }
2286 break;
2287
Greg Clayton9488b742010-07-28 02:04:09 +00002288 case clang::Type::ObjCObject:
2289 case clang::Type::ObjCInterface:
2290 {
2291 StringRef name_sref(name);
2292 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
2293 assert (objc_class_type);
2294 if (objc_class_type)
2295 {
2296 uint32_t child_idx = 0;
2297 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2298
2299 if (class_interface_decl)
2300 {
2301 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
2302 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
2303
Greg Clayton823533e2010-09-18 02:11:07 +00002304 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
Greg Clayton9488b742010-07-28 02:04:09 +00002305 {
2306 const ObjCIvarDecl* ivar_decl = *ivar_pos;
2307
2308 if (ivar_decl->getName().equals (name_sref))
2309 {
2310 if ((!omit_empty_base_classes && superclass_interface_decl) ||
2311 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
2312 ++child_idx;
2313
2314 child_indexes.push_back (child_idx);
2315 return child_indexes.size();
2316 }
2317 }
2318
2319 if (superclass_interface_decl)
2320 {
2321 // The super class index is always zero for ObjC classes,
2322 // so we push it onto the child indexes in case we find
2323 // an ivar in our superclass...
2324 child_indexes.push_back (0);
2325
2326 if (GetIndexOfChildMemberWithName (ast_context,
2327 ast_context->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(),
2328 name,
2329 omit_empty_base_classes,
2330 child_indexes))
2331 {
2332 // We did find an ivar in a superclass so just
2333 // return the results!
2334 return child_indexes.size();
2335 }
2336
2337 // We didn't find an ivar matching "name" in our
2338 // superclass, pop the superclass zero index that
2339 // we pushed on above.
2340 child_indexes.pop_back();
2341 }
2342 }
2343 }
2344 }
2345 break;
2346
2347 case clang::Type::ObjCObjectPointer:
2348 {
2349 return GetIndexOfChildMemberWithName (ast_context,
2350 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
2351 name,
2352 omit_empty_base_classes,
2353 child_indexes);
2354 }
2355 break;
2356
2357
Greg Clayton1674b122010-07-21 22:12:05 +00002358 case clang::Type::ConstantArray:
Chris Lattner24943d22010-06-08 16:52:24 +00002359 {
2360// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
2361// const uint64_t element_count = array->getSize().getLimitedValue();
2362//
2363// if (idx < element_count)
2364// {
2365// std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(array->getElementType());
2366//
2367// char element_name[32];
2368// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
2369//
2370// child_name.assign(element_name);
2371// assert(field_type_info.first % 8 == 0);
2372// child_byte_size = field_type_info.first / 8;
2373// child_byte_offset = idx * child_byte_size;
2374// return array->getElementType().getAsOpaquePtr();
2375// }
2376 }
2377 break;
2378
Greg Clayton1674b122010-07-21 22:12:05 +00002379// case clang::Type::MemberPointerType:
Chris Lattner24943d22010-06-08 16:52:24 +00002380// {
2381// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
2382// QualType pointee_type = mem_ptr_type->getPointeeType();
2383//
2384// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
2385// {
2386// return GetIndexOfChildWithName (ast_context,
2387// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
2388// name);
2389// }
2390// }
2391// break;
2392//
Greg Clayton1674b122010-07-21 22:12:05 +00002393 case clang::Type::LValueReference:
2394 case clang::Type::RValueReference:
Chris Lattner24943d22010-06-08 16:52:24 +00002395 {
2396 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
2397 QualType pointee_type = reference_type->getPointeeType();
2398
2399 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
2400 {
2401 return GetIndexOfChildMemberWithName (ast_context,
2402 reference_type->getPointeeType().getAsOpaquePtr(),
2403 name,
2404 omit_empty_base_classes,
2405 child_indexes);
2406 }
2407 }
2408 break;
2409
Greg Clayton1674b122010-07-21 22:12:05 +00002410 case clang::Type::Pointer:
Chris Lattner24943d22010-06-08 16:52:24 +00002411 {
2412 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
2413 QualType pointee_type = pointer_type->getPointeeType();
2414
2415 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
2416 {
2417 return GetIndexOfChildMemberWithName (ast_context,
2418 pointer_type->getPointeeType().getAsOpaquePtr(),
2419 name,
2420 omit_empty_base_classes,
2421 child_indexes);
2422 }
2423 else
2424 {
2425// if (parent_name)
2426// {
2427// child_name.assign(1, '*');
2428// child_name += parent_name;
2429// }
2430//
2431// // We have a pointer to an simple type
2432// if (idx == 0)
2433// {
2434// std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type);
2435// assert(clang_type_info.first % 8 == 0);
2436// child_byte_size = clang_type_info.first / 8;
2437// child_byte_offset = 0;
2438// return pointee_type.getAsOpaquePtr();
2439// }
2440 }
2441 }
2442 break;
2443
Greg Clayton1674b122010-07-21 22:12:05 +00002444 case clang::Type::Typedef:
Chris Lattner24943d22010-06-08 16:52:24 +00002445 return GetIndexOfChildMemberWithName (ast_context,
2446 cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(),
2447 name,
2448 omit_empty_base_classes,
2449 child_indexes);
2450
2451 default:
2452 break;
2453 }
2454 }
2455 return 0;
2456}
2457
2458
2459// Get the index of the child of "clang_type" whose name matches. This function
2460// doesn't descend into the children, but only looks one level deep and name
2461// matches can include base class names.
2462
2463uint32_t
2464ClangASTContext::GetIndexOfChildWithName
2465(
2466 ASTContext *ast_context,
2467 void *clang_type,
2468 const char *name,
2469 bool omit_empty_base_classes
2470)
2471{
2472 if (clang_type && name && name[0])
2473 {
2474 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton9488b742010-07-28 02:04:09 +00002475
Greg Clayton03e0f972010-09-13 03:32:57 +00002476 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Greg Clayton9488b742010-07-28 02:04:09 +00002477
Greg Clayton03e0f972010-09-13 03:32:57 +00002478 switch (type_class)
Chris Lattner24943d22010-06-08 16:52:24 +00002479 {
Greg Clayton1674b122010-07-21 22:12:05 +00002480 case clang::Type::Record:
Chris Lattner24943d22010-06-08 16:52:24 +00002481 {
2482 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
2483 const RecordDecl *record_decl = record_type->getDecl();
2484
2485 assert(record_decl);
2486 uint32_t child_idx = 0;
2487
2488 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2489
2490 if (cxx_record_decl)
2491 {
2492 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2493 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2494 base_class != base_class_end;
2495 ++base_class)
2496 {
2497 // Skip empty base classes
2498 CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2499 if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false)
2500 continue;
2501
2502 if (base_class->getType().getAsString().compare (name) == 0)
2503 return child_idx;
2504 ++child_idx;
2505 }
2506 }
2507
2508 // Try and find a field that matches NAME
2509 RecordDecl::field_iterator field, field_end;
2510 StringRef name_sref(name);
2511 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
2512 field != field_end;
2513 ++field, ++child_idx)
2514 {
2515 if (field->getName().equals (name_sref))
2516 return child_idx;
2517 }
2518
2519 }
2520 break;
2521
Greg Clayton9488b742010-07-28 02:04:09 +00002522 case clang::Type::ObjCObject:
2523 case clang::Type::ObjCInterface:
2524 {
2525 StringRef name_sref(name);
2526 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
2527 assert (objc_class_type);
2528 if (objc_class_type)
2529 {
2530 uint32_t child_idx = 0;
2531 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2532
2533 if (class_interface_decl)
2534 {
2535 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
2536 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
2537
2538 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
2539 {
2540 const ObjCIvarDecl* ivar_decl = *ivar_pos;
2541
2542 if (ivar_decl->getName().equals (name_sref))
2543 {
2544 if ((!omit_empty_base_classes && superclass_interface_decl) ||
2545 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
2546 ++child_idx;
2547
2548 return child_idx;
2549 }
2550 }
2551
2552 if (superclass_interface_decl)
2553 {
2554 if (superclass_interface_decl->getName().equals (name_sref))
2555 return 0;
2556 }
2557 }
2558 }
2559 }
2560 break;
2561
2562 case clang::Type::ObjCObjectPointer:
2563 {
2564 return GetIndexOfChildWithName (ast_context,
2565 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(),
2566 name,
2567 omit_empty_base_classes);
2568 }
2569 break;
2570
Greg Clayton1674b122010-07-21 22:12:05 +00002571 case clang::Type::ConstantArray:
Chris Lattner24943d22010-06-08 16:52:24 +00002572 {
2573// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
2574// const uint64_t element_count = array->getSize().getLimitedValue();
2575//
2576// if (idx < element_count)
2577// {
2578// std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(array->getElementType());
2579//
2580// char element_name[32];
2581// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
2582//
2583// child_name.assign(element_name);
2584// assert(field_type_info.first % 8 == 0);
2585// child_byte_size = field_type_info.first / 8;
2586// child_byte_offset = idx * child_byte_size;
2587// return array->getElementType().getAsOpaquePtr();
2588// }
2589 }
2590 break;
2591
Greg Clayton1674b122010-07-21 22:12:05 +00002592// case clang::Type::MemberPointerType:
Chris Lattner24943d22010-06-08 16:52:24 +00002593// {
2594// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
2595// QualType pointee_type = mem_ptr_type->getPointeeType();
2596//
2597// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
2598// {
2599// return GetIndexOfChildWithName (ast_context,
2600// mem_ptr_type->getPointeeType().getAsOpaquePtr(),
2601// name);
2602// }
2603// }
2604// break;
2605//
Greg Clayton1674b122010-07-21 22:12:05 +00002606 case clang::Type::LValueReference:
2607 case clang::Type::RValueReference:
Chris Lattner24943d22010-06-08 16:52:24 +00002608 {
2609 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
2610 QualType pointee_type = reference_type->getPointeeType();
2611
2612 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
2613 {
2614 return GetIndexOfChildWithName (ast_context,
2615 reference_type->getPointeeType().getAsOpaquePtr(),
2616 name,
2617 omit_empty_base_classes);
2618 }
2619 }
2620 break;
2621
Greg Clayton1674b122010-07-21 22:12:05 +00002622 case clang::Type::Pointer:
Chris Lattner24943d22010-06-08 16:52:24 +00002623 {
2624 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
2625 QualType pointee_type = pointer_type->getPointeeType();
2626
2627 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
2628 {
2629 return GetIndexOfChildWithName (ast_context,
2630 pointer_type->getPointeeType().getAsOpaquePtr(),
2631 name,
2632 omit_empty_base_classes);
2633 }
2634 else
2635 {
2636// if (parent_name)
2637// {
2638// child_name.assign(1, '*');
2639// child_name += parent_name;
2640// }
2641//
2642// // We have a pointer to an simple type
2643// if (idx == 0)
2644// {
2645// std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type);
2646// assert(clang_type_info.first % 8 == 0);
2647// child_byte_size = clang_type_info.first / 8;
2648// child_byte_offset = 0;
2649// return pointee_type.getAsOpaquePtr();
2650// }
2651 }
2652 }
2653 break;
2654
Greg Clayton1674b122010-07-21 22:12:05 +00002655 case clang::Type::Typedef:
Chris Lattner24943d22010-06-08 16:52:24 +00002656 return GetIndexOfChildWithName (ast_context,
2657 cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(),
2658 name,
2659 omit_empty_base_classes);
2660
2661 default:
2662 break;
2663 }
2664 }
2665 return UINT32_MAX;
2666}
2667
2668#pragma mark TagType
2669
2670bool
2671ClangASTContext::SetTagTypeKind (void *tag_clang_type, int kind)
2672{
2673 if (tag_clang_type)
2674 {
2675 QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type));
Greg Clayton1674b122010-07-21 22:12:05 +00002676 clang::Type *clang_type = tag_qual_type.getTypePtr();
Chris Lattner24943d22010-06-08 16:52:24 +00002677 if (clang_type)
2678 {
2679 TagType *tag_type = dyn_cast<TagType>(clang_type);
2680 if (tag_type)
2681 {
2682 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl());
2683 if (tag_decl)
2684 {
2685 tag_decl->setTagKind ((TagDecl::TagKind)kind);
2686 return true;
2687 }
2688 }
2689 }
2690 }
2691 return false;
2692}
2693
2694
2695#pragma mark DeclContext Functions
2696
2697DeclContext *
2698ClangASTContext::GetDeclContextForType (void *clang_type)
2699{
2700 if (clang_type == NULL)
2701 return NULL;
2702
2703 QualType qual_type(QualType::getFromOpaquePtr(clang_type));
Greg Clayton03e0f972010-09-13 03:32:57 +00002704 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2705 switch (type_class)
Chris Lattner24943d22010-06-08 16:52:24 +00002706 {
Greg Clayton9488b742010-07-28 02:04:09 +00002707 case clang::Type::FunctionNoProto: break;
2708 case clang::Type::FunctionProto: break;
2709 case clang::Type::IncompleteArray: break;
2710 case clang::Type::VariableArray: break;
2711 case clang::Type::ConstantArray: break;
2712 case clang::Type::ExtVector: break;
2713 case clang::Type::Vector: break;
2714 case clang::Type::Builtin: break;
2715 case clang::Type::BlockPointer: break;
2716 case clang::Type::Pointer: break;
2717 case clang::Type::LValueReference: break;
2718 case clang::Type::RValueReference: break;
2719 case clang::Type::MemberPointer: break;
2720 case clang::Type::Complex: break;
2721 case clang::Type::ObjCObject: break;
2722 case clang::Type::ObjCInterface: return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface();
2723 case clang::Type::ObjCObjectPointer: return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr());
2724 case clang::Type::Record: return cast<RecordType>(qual_type)->getDecl();
2725 case clang::Type::Enum: return cast<EnumType>(qual_type)->getDecl();
2726 case clang::Type::Typedef: return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr());
Chris Lattner24943d22010-06-08 16:52:24 +00002727
Greg Clayton9488b742010-07-28 02:04:09 +00002728 case clang::Type::TypeOfExpr: break;
2729 case clang::Type::TypeOf: break;
2730 case clang::Type::Decltype: break;
2731 //case clang::Type::QualifiedName: break;
2732 case clang::Type::TemplateSpecialization: break;
Chris Lattner24943d22010-06-08 16:52:24 +00002733 }
2734 // No DeclContext in this type...
2735 return NULL;
2736}
2737
2738#pragma mark Namespace Declarations
2739
2740NamespaceDecl *
2741ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, const Declaration &decl, DeclContext *decl_ctx)
2742{
2743 // TODO: Do something intelligent with the Declaration object passed in
2744 // like maybe filling in the SourceLocation with it...
2745 if (name)
2746 {
2747 ASTContext *ast_context = getASTContext();
2748 if (decl_ctx == NULL)
2749 decl_ctx = ast_context->getTranslationUnitDecl();
2750 return NamespaceDecl::Create(*ast_context, decl_ctx, SourceLocation(), &ast_context->Idents.get(name));
2751 }
2752 return NULL;
2753}
2754
2755
2756#pragma mark Function Types
2757
2758FunctionDecl *
2759ClangASTContext::CreateFunctionDeclaration (const char *name, void *function_clang_type, int storage, bool is_inline)
2760{
2761 if (name)
2762 {
2763 ASTContext *ast_context = getASTContext();
2764 assert (ast_context != NULL);
2765
2766 if (name && name[0])
2767 {
2768 return FunctionDecl::Create(*ast_context,
2769 ast_context->getTranslationUnitDecl(),
2770 SourceLocation(),
2771 DeclarationName (&ast_context->Idents.get(name)),
2772 QualType::getFromOpaquePtr(function_clang_type),
2773 NULL,
2774 (FunctionDecl::StorageClass)storage,
2775 (FunctionDecl::StorageClass)storage,
2776 is_inline);
2777 }
2778 else
2779 {
2780 return FunctionDecl::Create(*ast_context,
2781 ast_context->getTranslationUnitDecl(),
2782 SourceLocation(),
2783 DeclarationName (),
2784 QualType::getFromOpaquePtr(function_clang_type),
2785 NULL,
2786 (FunctionDecl::StorageClass)storage,
2787 (FunctionDecl::StorageClass)storage,
2788 is_inline);
2789 }
2790 }
2791 return NULL;
2792}
2793
2794void *
Sean Callanan2ea8f272010-09-16 20:40:25 +00002795ClangASTContext::CreateFunctionType (clang::ASTContext *ast_context,
2796 void *result_type,
2797 void **args,
2798 unsigned num_args,
2799 bool is_variadic,
2800 unsigned type_quals)
Chris Lattner24943d22010-06-08 16:52:24 +00002801{
Chris Lattner24943d22010-06-08 16:52:24 +00002802 assert (ast_context != NULL);
2803 std::vector<QualType> qual_type_args;
2804 for (unsigned i=0; i<num_args; ++i)
2805 qual_type_args.push_back (QualType::getFromOpaquePtr(args[i]));
2806
2807 // TODO: Detect calling convention in DWARF?
2808 return ast_context->getFunctionType(QualType::getFromOpaquePtr(result_type),
Greg Clayton53d68e72010-07-20 22:52:08 +00002809 qual_type_args.empty() ? NULL : &qual_type_args.front(),
Chris Lattner24943d22010-06-08 16:52:24 +00002810 qual_type_args.size(),
Sean Callanan2ea8f272010-09-16 20:40:25 +00002811 is_variadic,
2812 type_quals,
Chris Lattner24943d22010-06-08 16:52:24 +00002813 false, // hasExceptionSpec
2814 false, // hasAnyExceptionSpec,
2815 0, // NumExs
2816 0, // const QualType *ExArray
2817 FunctionType::ExtInfo ()).getAsOpaquePtr(); // NoReturn);
2818}
2819
2820ParmVarDecl *
Sean Callanan2ea8f272010-09-16 20:40:25 +00002821ClangASTContext::CreateParameterDeclaration (const char *name, void *param_type, int storage)
Chris Lattner24943d22010-06-08 16:52:24 +00002822{
2823 ASTContext *ast_context = getASTContext();
2824 assert (ast_context != NULL);
2825 return ParmVarDecl::Create(*ast_context,
2826 ast_context->getTranslationUnitDecl(),
2827 SourceLocation(),
2828 name && name[0] ? &ast_context->Idents.get(name) : NULL,
Sean Callanan2ea8f272010-09-16 20:40:25 +00002829 QualType::getFromOpaquePtr(param_type),
Chris Lattner24943d22010-06-08 16:52:24 +00002830 NULL,
2831 (VarDecl::StorageClass)storage,
2832 (VarDecl::StorageClass)storage,
2833 0);
2834}
2835
2836void
2837ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
2838{
2839 if (function_decl)
2840 function_decl->setParams (params, num_params);
2841}
2842
2843
2844#pragma mark Array Types
2845
2846void *
2847ClangASTContext::CreateArrayType (void *element_type, size_t element_count, uint32_t bit_stride)
2848{
2849 if (element_type)
2850 {
2851 ASTContext *ast_context = getASTContext();
2852 assert (ast_context != NULL);
2853 llvm::APInt ap_element_count (64, element_count);
2854 return ast_context->getConstantArrayType(QualType::getFromOpaquePtr(element_type),
2855 ap_element_count,
2856 ArrayType::Normal,
2857 0).getAsOpaquePtr(); // ElemQuals
2858 }
2859 return NULL;
2860}
2861
2862
2863#pragma mark TagDecl
2864
2865bool
2866ClangASTContext::StartTagDeclarationDefinition (void *clang_type)
2867{
2868 if (clang_type)
2869 {
2870 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton1674b122010-07-21 22:12:05 +00002871 clang::Type *t = qual_type.getTypePtr();
Chris Lattner24943d22010-06-08 16:52:24 +00002872 if (t)
2873 {
2874 TagType *tag_type = dyn_cast<TagType>(t);
2875 if (tag_type)
2876 {
2877 TagDecl *tag_decl = tag_type->getDecl();
2878 if (tag_decl)
2879 {
2880 tag_decl->startDefinition();
2881 return true;
2882 }
2883 }
2884 }
2885 }
2886 return false;
2887}
2888
2889bool
2890ClangASTContext::CompleteTagDeclarationDefinition (void *clang_type)
2891{
2892 if (clang_type)
2893 {
2894 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton1674b122010-07-21 22:12:05 +00002895 clang::Type *t = qual_type.getTypePtr();
Chris Lattner24943d22010-06-08 16:52:24 +00002896 if (t)
2897 {
2898 TagType *tag_type = dyn_cast<TagType>(t);
2899 if (tag_type)
2900 {
2901 TagDecl *tag_decl = tag_type->getDecl();
2902 if (tag_decl)
2903 {
2904 tag_decl->completeDefinition();
2905 return true;
2906 }
2907 }
2908 }
2909 }
2910 return false;
2911}
2912
2913
2914#pragma mark Enumeration Types
2915
2916void *
Greg Claytone37f23c2010-09-12 23:17:56 +00002917ClangASTContext::CreateEnumerationType (const Declaration &decl, const char *name, void *integer_qual_type)
Chris Lattner24943d22010-06-08 16:52:24 +00002918{
2919 // TODO: Do something intelligent with the Declaration object passed in
2920 // like maybe filling in the SourceLocation with it...
2921 ASTContext *ast_context = getASTContext();
2922 assert (ast_context != NULL);
2923 EnumDecl *enum_decl = EnumDecl::Create(*ast_context,
2924 ast_context->getTranslationUnitDecl(),
2925 SourceLocation(),
2926 name && name[0] ? &ast_context->Idents.get(name) : NULL,
2927 SourceLocation(),
2928 NULL);
2929 if (enum_decl)
Greg Claytone37f23c2010-09-12 23:17:56 +00002930 {
2931 // TODO: check if we should be setting the promotion type too?
2932 enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type));
Chris Lattner24943d22010-06-08 16:52:24 +00002933 return ast_context->getTagDeclType(enum_decl).getAsOpaquePtr();
Greg Claytone37f23c2010-09-12 23:17:56 +00002934 }
Chris Lattner24943d22010-06-08 16:52:24 +00002935 return NULL;
2936}
2937
2938bool
2939ClangASTContext::AddEnumerationValueToEnumerationType
2940(
2941 void *enum_clang_type,
2942 void *enumerator_clang_type,
2943 const Declaration &decl,
2944 const char *name,
2945 int64_t enum_value,
2946 uint32_t enum_value_bit_size
2947)
2948{
2949 if (enum_clang_type && enumerator_clang_type && name)
2950 {
2951 // TODO: Do something intelligent with the Declaration object passed in
2952 // like maybe filling in the SourceLocation with it...
2953 ASTContext *ast_context = getASTContext();
2954 IdentifierTable *identifier_table = getIdentifierTable();
2955
2956 assert (ast_context != NULL);
2957 assert (identifier_table != NULL);
2958 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type));
2959
Greg Clayton1674b122010-07-21 22:12:05 +00002960 clang::Type *clang_type = enum_qual_type.getTypePtr();
Chris Lattner24943d22010-06-08 16:52:24 +00002961 if (clang_type)
2962 {
2963 const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
2964
2965 if (enum_type)
2966 {
2967 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, false);
2968 enum_llvm_apsint = enum_value;
2969 EnumConstantDecl *enumerator_decl =
2970 EnumConstantDecl::Create(*ast_context,
2971 enum_type->getDecl(),
2972 SourceLocation(),
2973 name ? &identifier_table->get(name) : NULL, // Identifier
2974 QualType::getFromOpaquePtr(enumerator_clang_type),
2975 NULL,
2976 enum_llvm_apsint);
2977
2978 if (enumerator_decl)
2979 {
2980 enum_type->getDecl()->addDecl(enumerator_decl);
2981 return true;
2982 }
2983 }
2984 }
2985 }
2986 return false;
2987}
2988
2989#pragma mark Pointers & References
2990
2991void *
2992ClangASTContext::CreatePointerType (void *clang_type)
2993{
2994 if (clang_type)
Greg Clayton7b541032010-07-29 20:06:32 +00002995 {
2996 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
2997
Greg Clayton03e0f972010-09-13 03:32:57 +00002998 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2999 switch (type_class)
Greg Clayton7b541032010-07-29 20:06:32 +00003000 {
3001 case clang::Type::ObjCObject:
3002 case clang::Type::ObjCInterface:
Greg Clayton7b541032010-07-29 20:06:32 +00003003 return getASTContext()->getObjCObjectPointerType(qual_type).getAsOpaquePtr();
3004
Greg Clayton7b541032010-07-29 20:06:32 +00003005 default:
3006 return getASTContext()->getPointerType(qual_type).getAsOpaquePtr();
3007 }
3008 }
Chris Lattner24943d22010-06-08 16:52:24 +00003009 return NULL;
3010}
3011
3012void *
3013ClangASTContext::CreateLValueReferenceType (void *clang_type)
3014{
3015 if (clang_type)
3016 return getASTContext()->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
3017 return NULL;
3018}
3019
3020void *
3021ClangASTContext::CreateRValueReferenceType (void *clang_type)
3022{
3023 if (clang_type)
3024 return getASTContext()->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr();
3025 return NULL;
3026}
3027
Greg Claytonfa970692010-06-12 01:20:30 +00003028void *
Greg Clayton84f80752010-07-22 18:30:50 +00003029ClangASTContext::CreateMemberPointerType (void *clang_pointee_type, void *clang_class_type)
Greg Claytonfa970692010-06-12 01:20:30 +00003030{
3031 if (clang_pointee_type && clang_pointee_type)
3032 return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type),
3033 QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr();
3034 return NULL;
3035}
3036
Chris Lattner24943d22010-06-08 16:52:24 +00003037size_t
3038ClangASTContext::GetPointerBitSize ()
3039{
3040 ASTContext *ast_context = getASTContext();
3041 return ast_context->getTypeSize(ast_context->VoidPtrTy);
3042}
3043
3044bool
3045ClangASTContext::IsPointerOrReferenceType (void *clang_type, void **target_type)
3046{
3047 if (clang_type == NULL)
3048 return false;
3049
3050 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton03e0f972010-09-13 03:32:57 +00003051 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3052 switch (type_class)
Chris Lattner24943d22010-06-08 16:52:24 +00003053 {
Greg Clayton1674b122010-07-21 22:12:05 +00003054 case clang::Type::ObjCObjectPointer:
Chris Lattner24943d22010-06-08 16:52:24 +00003055 if (target_type)
3056 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
3057 return true;
Greg Clayton1674b122010-07-21 22:12:05 +00003058 case clang::Type::BlockPointer:
Chris Lattner24943d22010-06-08 16:52:24 +00003059 if (target_type)
3060 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
3061 return true;
Greg Clayton1674b122010-07-21 22:12:05 +00003062 case clang::Type::Pointer:
Chris Lattner24943d22010-06-08 16:52:24 +00003063 if (target_type)
3064 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
3065 return true;
Greg Clayton1674b122010-07-21 22:12:05 +00003066 case clang::Type::MemberPointer:
Chris Lattner24943d22010-06-08 16:52:24 +00003067 if (target_type)
3068 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
3069 return true;
Greg Clayton1674b122010-07-21 22:12:05 +00003070 case clang::Type::LValueReference:
Chris Lattner24943d22010-06-08 16:52:24 +00003071 if (target_type)
3072 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
3073 return true;
Greg Clayton1674b122010-07-21 22:12:05 +00003074 case clang::Type::RValueReference:
Chris Lattner24943d22010-06-08 16:52:24 +00003075 if (target_type)
3076 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr();
3077 return true;
Greg Clayton1674b122010-07-21 22:12:05 +00003078 case clang::Type::Typedef:
Chris Lattner24943d22010-06-08 16:52:24 +00003079 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr());
3080 default:
3081 break;
3082 }
3083 return false;
3084}
3085
Chris Lattner24943d22010-06-08 16:52:24 +00003086bool
Greg Clayton84f80752010-07-22 18:30:50 +00003087ClangASTContext::IsIntegerType (void *clang_type, bool &is_signed)
Chris Lattner24943d22010-06-08 16:52:24 +00003088{
3089 if (!clang_type)
3090 return false;
3091
3092 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3093 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal());
3094
3095 if (builtin_type)
3096 {
3097 if (builtin_type->isInteger())
3098 is_signed = builtin_type->isSignedInteger();
3099
3100 return true;
3101 }
3102
3103 return false;
3104}
3105
3106bool
3107ClangASTContext::IsPointerType (void *clang_type, void **target_type)
3108{
3109 if (clang_type)
3110 {
3111 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton03e0f972010-09-13 03:32:57 +00003112 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3113 switch (type_class)
Chris Lattner24943d22010-06-08 16:52:24 +00003114 {
Greg Clayton1674b122010-07-21 22:12:05 +00003115 case clang::Type::ObjCObjectPointer:
Chris Lattner24943d22010-06-08 16:52:24 +00003116 if (target_type)
3117 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
3118 return true;
Greg Clayton1674b122010-07-21 22:12:05 +00003119 case clang::Type::BlockPointer:
Chris Lattner24943d22010-06-08 16:52:24 +00003120 if (target_type)
3121 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
3122 return true;
Greg Clayton1674b122010-07-21 22:12:05 +00003123 case clang::Type::Pointer:
Chris Lattner24943d22010-06-08 16:52:24 +00003124 if (target_type)
3125 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
3126 return true;
Greg Clayton1674b122010-07-21 22:12:05 +00003127 case clang::Type::MemberPointer:
Chris Lattner24943d22010-06-08 16:52:24 +00003128 if (target_type)
3129 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr();
3130 return true;
Greg Clayton1674b122010-07-21 22:12:05 +00003131 case clang::Type::Typedef:
Chris Lattner24943d22010-06-08 16:52:24 +00003132 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), target_type);
3133 default:
3134 break;
3135 }
3136 }
3137 return false;
3138}
3139
3140bool
3141ClangASTContext::IsFloatingPointType (void *clang_type, uint32_t &count, bool &is_complex)
3142{
3143 if (clang_type)
3144 {
3145 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3146
3147 if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()))
3148 {
3149 clang::BuiltinType::Kind kind = BT->getKind();
3150 if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble)
3151 {
3152 count = 1;
3153 is_complex = false;
3154 return true;
3155 }
3156 }
3157 else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal()))
3158 {
3159 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex))
3160 {
3161 count = 2;
3162 is_complex = true;
3163 return true;
3164 }
3165 }
3166 else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal()))
3167 {
3168 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex))
3169 {
3170 count = VT->getNumElements();
3171 is_complex = false;
3172 return true;
3173 }
3174 }
3175 }
3176 return false;
3177}
3178
Greg Clayton1d8173f2010-09-24 05:15:53 +00003179bool
3180ClangASTContext::IsCXXClassType (void *clang_type)
3181{
3182 if (clang_type)
3183 {
3184 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3185 if (qual_type->getAsCXXRecordDecl() != NULL)
3186 return true;
3187 }
3188 return false;
3189}
3190
3191bool
3192ClangASTContext::IsObjCClassType (void *clang_type)
3193{
3194 if (clang_type)
3195 {
3196 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3197 if (qual_type->isObjCObjectOrInterfaceType())
3198 return true;
3199 }
3200 return false;
3201}
3202
3203
3204
Chris Lattner24943d22010-06-08 16:52:24 +00003205
3206bool
3207ClangASTContext::IsCStringType (void *clang_type, uint32_t &length)
3208{
3209 if (clang_type)
3210 {
3211 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
Greg Clayton03e0f972010-09-13 03:32:57 +00003212 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3213 switch (type_class)
Chris Lattner24943d22010-06-08 16:52:24 +00003214 {
Greg Clayton1674b122010-07-21 22:12:05 +00003215 case clang::Type::ConstantArray:
Chris Lattner24943d22010-06-08 16:52:24 +00003216 {
3217 ConstantArrayType *array = cast<ConstantArrayType>(qual_type.getTypePtr());
3218 QualType element_qual_type = array->getElementType();
Greg Clayton1674b122010-07-21 22:12:05 +00003219 clang::Type *canonical_type = element_qual_type->getCanonicalTypeInternal().getTypePtr();
Chris Lattner24943d22010-06-08 16:52:24 +00003220 if (canonical_type && canonical_type->isCharType())
3221 {
3222 // We know the size of the array and it could be a C string
3223 // since it is an array of characters
3224 length = array->getSize().getLimitedValue();
3225 return true;
3226 }
3227 }
3228 break;
3229
Greg Clayton1674b122010-07-21 22:12:05 +00003230 case clang::Type::Pointer:
Chris Lattner24943d22010-06-08 16:52:24 +00003231 {
3232 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
Greg Clayton1674b122010-07-21 22:12:05 +00003233 clang::Type *pointee_type_ptr = pointer_type->getPointeeType().getTypePtr();
Chris Lattner24943d22010-06-08 16:52:24 +00003234 if (pointee_type_ptr)
3235 {
Greg Clayton1674b122010-07-21 22:12:05 +00003236 clang::Type *canonical_type_ptr = pointee_type_ptr->getCanonicalTypeInternal().getTypePtr();
Chris Lattner24943d22010-06-08 16:52:24 +00003237 length = 0; // No length info, read until a NULL terminator is received
3238 if (canonical_type_ptr)
3239 return canonical_type_ptr->isCharType();
3240 else
3241 return pointee_type_ptr->isCharType();
3242 }
3243 }
3244 break;
3245
Greg Clayton1674b122010-07-21 22:12:05 +00003246 case clang::Type::Typedef:
Chris Lattner24943d22010-06-08 16:52:24 +00003247 return ClangASTContext::IsCStringType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), length);
3248
Greg Clayton1674b122010-07-21 22:12:05 +00003249 case clang::Type::LValueReference:
3250 case clang::Type::RValueReference:
Chris Lattner24943d22010-06-08 16:52:24 +00003251 {
3252 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
Greg Clayton1674b122010-07-21 22:12:05 +00003253 clang::Type *pointee_type_ptr = reference_type->getPointeeType().getTypePtr();
Chris Lattner24943d22010-06-08 16:52:24 +00003254 if (pointee_type_ptr)
3255 {
Greg Clayton1674b122010-07-21 22:12:05 +00003256 clang::Type *canonical_type_ptr = pointee_type_ptr->getCanonicalTypeInternal().getTypePtr();
Chris Lattner24943d22010-06-08 16:52:24 +00003257 length = 0; // No length info, read until a NULL terminator is received
3258 if (canonical_type_ptr)
3259 return canonical_type_ptr->isCharType();
3260 else
3261 return pointee_type_ptr->isCharType();
3262 }
3263 }
3264 break;
3265 }
3266 }
3267 return false;
3268}
3269
3270bool
Greg Clayton03e0f972010-09-13 03:32:57 +00003271ClangASTContext::IsFunctionPointerType (void *clang_type)
3272{
3273 if (clang_type)
3274 {
3275 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3276
3277 if (qual_type->isFunctionPointerType())
3278 return true;
3279
3280 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3281 switch (type_class)
3282 {
3283 case clang::Type::Typedef:
3284 return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr());
3285
3286 case clang::Type::LValueReference:
3287 case clang::Type::RValueReference:
3288 {
3289 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
3290 if (reference_type)
3291 return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr());
3292 }
3293 break;
3294 }
3295 }
3296 return false;
3297}
3298
3299
3300
3301
3302bool
Greg Clayton84f80752010-07-22 18:30:50 +00003303ClangASTContext::IsArrayType (void *clang_type, void **member_type, uint64_t *size)
Chris Lattner24943d22010-06-08 16:52:24 +00003304{
3305 if (!clang_type)
3306 return false;
3307
3308 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3309
Greg Clayton03e0f972010-09-13 03:32:57 +00003310 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3311 switch (type_class)
Chris Lattner24943d22010-06-08 16:52:24 +00003312 {
Greg Clayton1674b122010-07-21 22:12:05 +00003313 case clang::Type::ConstantArray:
Chris Lattner24943d22010-06-08 16:52:24 +00003314 if (member_type)
3315 *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr();
3316 if (size)
3317 *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULONG_LONG_MAX);
3318 return true;
Greg Clayton1674b122010-07-21 22:12:05 +00003319 case clang::Type::IncompleteArray:
Chris Lattner24943d22010-06-08 16:52:24 +00003320 if (member_type)
3321 *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr();
3322 if (size)
3323 *size = 0;
3324 return true;
Greg Clayton1674b122010-07-21 22:12:05 +00003325 case clang::Type::VariableArray:
Chris Lattner24943d22010-06-08 16:52:24 +00003326 if (member_type)
3327 *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr();
3328 if (size)
3329 *size = 0;
Greg Clayton1674b122010-07-21 22:12:05 +00003330 case clang::Type::DependentSizedArray:
Chris Lattner24943d22010-06-08 16:52:24 +00003331 if (member_type)
3332 *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr();
3333 if (size)
3334 *size = 0;
3335 return true;
3336 }
3337 return false;
3338}
3339
3340
3341#pragma mark Typedefs
3342
3343void *
3344ClangASTContext::CreateTypedefType (const char *name, void *clang_type, DeclContext *decl_ctx)
3345{
3346 if (clang_type)
3347 {
3348 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3349 ASTContext *ast_context = getASTContext();
3350 IdentifierTable *identifier_table = getIdentifierTable();
3351 assert (ast_context != NULL);
3352 assert (identifier_table != NULL);
3353 if (decl_ctx == NULL)
3354 decl_ctx = ast_context->getTranslationUnitDecl();
3355 TypedefDecl *decl = TypedefDecl::Create(*ast_context,
3356 decl_ctx,
3357 SourceLocation(),
3358 name ? &identifier_table->get(name) : NULL, // Identifier
3359 ast_context->CreateTypeSourceInfo(qual_type));
3360
3361 // Get a uniqued QualType for the typedef decl type
3362 return ast_context->getTypedefType (decl).getAsOpaquePtr();
3363 }
3364 return NULL;
3365}
3366
3367
3368std::string
3369ClangASTContext::GetTypeName (void *opaque_qual_type)
3370{
3371 std::string return_name;
3372
3373 clang::QualType qual_type(clang::QualType::getFromOpaquePtr(opaque_qual_type));
3374
3375 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
3376 if (typedef_type)
3377 {
3378 const clang::TypedefDecl *typedef_decl = typedef_type->getDecl();
3379 return_name = typedef_decl->getQualifiedNameAsString();
3380 }
3381 else
3382 {
3383 return_name = qual_type.getAsString();
3384 }
3385
3386 return return_name;
3387}
3388
3389// Disable this for now since I can't seem to get a nicely formatted float
3390// out of the APFloat class without just getting the float, double or quad
3391// and then using a formatted print on it which defeats the purpose. We ideally
3392// would like to get perfect string values for any kind of float semantics
3393// so we can support remote targets. The code below also requires a patch to
3394// llvm::APInt.
3395//bool
3396//ClangASTContext::ConvertFloatValueToString (ASTContext *ast_context, void *clang_type, const uint8_t* bytes, size_t byte_size, int apint_byte_order, std::string &float_str)
3397//{
3398// uint32_t count = 0;
3399// bool is_complex = false;
3400// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
3401// {
3402// unsigned num_bytes_per_float = byte_size / count;
3403// unsigned num_bits_per_float = num_bytes_per_float * 8;
3404//
3405// float_str.clear();
3406// uint32_t i;
3407// for (i=0; i<count; i++)
3408// {
3409// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
3410// bool is_ieee = false;
3411// APFloat ap_float(ap_int, is_ieee);
3412// char s[1024];
3413// unsigned int hex_digits = 0;
3414// bool upper_case = false;
3415//
3416// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
3417// {
3418// if (i > 0)
3419// float_str.append(", ");
3420// float_str.append(s);
3421// if (i == 1 && is_complex)
3422// float_str.append(1, 'i');
3423// }
3424// }
3425// return !float_str.empty();
3426// }
3427// return false;
3428//}
3429
3430size_t
3431ClangASTContext::ConvertStringToFloatValue (ASTContext *ast_context, void *clang_type, const char *s, uint8_t *dst, size_t dst_size)
3432{
3433 if (clang_type)
3434 {
3435 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3436 uint32_t count = 0;
3437 bool is_complex = false;
3438 if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
3439 {
3440 // TODO: handle complex and vector types
3441 if (count != 1)
3442 return false;
3443
3444 StringRef s_sref(s);
3445 APFloat ap_float(ast_context->getFloatTypeSemantics(qual_type), s_sref);
3446
3447 const uint64_t bit_size = ast_context->getTypeSize (qual_type);
3448 const uint64_t byte_size = bit_size / 8;
3449 if (dst_size >= byte_size)
3450 {
3451 if (bit_size == sizeof(float)*8)
3452 {
3453 float float32 = ap_float.convertToFloat();
3454 ::memcpy (dst, &float32, byte_size);
3455 return byte_size;
3456 }
3457 else if (bit_size >= 64)
3458 {
3459 llvm::APInt ap_int(ap_float.bitcastToAPInt());
3460 ::memcpy (dst, ap_int.getRawData(), byte_size);
3461 return byte_size;
3462 }
3463 }
3464 }
3465 }
3466 return 0;
3467}
Sean Callanana751f7b2010-09-17 02:24:29 +00003468
3469unsigned
3470ClangASTContext::GetTypeQualifiers(void *clang_type)
3471{
3472 assert (clang_type);
3473
3474 QualType qual_type (QualType::getFromOpaquePtr(clang_type));
3475
3476 return qual_type.getQualifiers().getCVRQualifiers();
3477}