ClangASTContext.cpp revision ad60bf486dee699dd524225b63271ddc5b6329b9
1//===-- 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 10#include "lldb/Symbol/ClangASTContext.h" 11 12// C Includes 13// C++ Includes 14#include <string> 15 16// Other libraries and framework includes 17#define NDEBUG 18#include "clang/AST/ASTContext.h" 19#include "clang/AST/ASTImporter.h" 20#include "clang/AST/CXXInheritance.h" 21#include "clang/AST/DeclObjC.h" 22#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" 31#undef NDEBUG 32 33#include "lldb/Core/dwarf.h" 34 35#include <stdio.h> 36 37using namespace lldb; 38using namespace lldb_private; 39using namespace llvm; 40using namespace clang; 41 42static AccessSpecifier 43ConvertAccessTypeToAccessSpecifier (AccessType access) 44{ 45 switch (access) 46 { 47 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; 52 } 53 return AS_none; 54} 55 56static ObjCIvarDecl::AccessControl 57ConvertAccessTypeToObjCIvarAccessControl (AccessType access) 58{ 59 switch (access) 60 { 61 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; 67 } 68 return ObjCIvarDecl::None; 69} 70 71 72static void 73ParseLangArgs 74( 75 LangOptions &Opts, 76 InputKind IK 77) 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. 84 if (IK == IK_Asm) { 85 Opts.AsmPreprocessor = 1; 86 } else if (IK == IK_ObjC || 87 IK == IK_ObjCXX || 88 IK == IK_PreprocessedObjC || 89 IK == IK_PreprocessedObjCXX) { 90 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) { 98 case IK_None: 99 case IK_AST: 100 assert(0 && "Invalid input kind!"); 101 case IK_OpenCL: 102 LangStd = LangStandard::lang_opencl; 103 break; 104 case IK_Asm: 105 case IK_C: 106 case IK_PreprocessedC: 107 case IK_ObjC: 108 case IK_PreprocessedObjC: 109 LangStd = LangStandard::lang_gnu99; 110 break; 111 case IK_CXX: 112 case IK_PreprocessedCXX: 113 case IK_ObjCXX: 114 case IK_PreprocessedObjCXX: 115 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 236 237ClangASTContext::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(), 310 *getBuiltinContext(), 311 0)); 312 } 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()); 338 ParseLangArgs(*m_language_options_ap, IK_ObjCXX); 339// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX); 340 } 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 352clang::SourceManager * 353ClangASTContext::getSourceManager() 354{ 355 if (m_source_manager_ap.get() == NULL) 356 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnostic())); 357 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 401clang_type_t 402ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size) 403{ 404 ASTContext *ast_context = getASTContext(); 405 406 assert (ast_context != NULL); 407 408 return GetBuiltinTypeForEncodingAndBitSize (ast_context, encoding, bit_size); 409} 410 411clang_type_t 412ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast_context, Encoding encoding, uint32_t bit_size) 413{ 414 if (!ast_context) 415 return NULL; 416 417 switch (encoding) 418 { 419 case eEncodingInvalid: 420 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->VoidPtrTy)) 421 return ast_context->VoidPtrTy.getAsOpaquePtr(); 422 break; 423 424 case eEncodingUint: 425 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 439 case eEncodingSint: 440 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 454 case eEncodingIEEE754: 455 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 463 case eEncodingVector: 464 default: 465 break; 466 } 467 468 return NULL; 469} 470 471clang_type_t 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 660clang_type_t 661ClangASTContext::GetBuiltInType_void(ASTContext *ast_context) 662{ 663 return ast_context->VoidTy.getAsOpaquePtr(); 664} 665 666clang_type_t 667ClangASTContext::GetBuiltInType_objc_id() 668{ 669 return getASTContext()->getObjCIdType().getAsOpaquePtr(); 670} 671 672clang_type_t 673ClangASTContext::GetBuiltInType_objc_Class() 674{ 675 return getASTContext()->getObjCClassType().getAsOpaquePtr(); 676} 677 678clang_type_t 679ClangASTContext::GetBuiltInType_objc_selector() 680{ 681 return getASTContext()->getObjCSelType().getAsOpaquePtr(); 682} 683 684clang_type_t 685ClangASTContext::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 695clang_type_t 696ClangASTContext::GetVoidPtrType (bool is_const) 697{ 698 return GetVoidPtrType(getASTContext(), is_const); 699} 700 701clang_type_t 702ClangASTContext::GetVoidPtrType (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 712clang_type_t 713ClangASTContext::CopyType (ASTContext *dest_context, 714 ASTContext *source_context, 715 clang_type_t clang_type) 716{ 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 726bool 727ClangASTContext::AreTypesSame(ASTContext *ast_context, 728 clang_type_t type1, 729 clang_type_t type2) 730{ 731 return ast_context->hasSameType(QualType::getFromOpaquePtr(type1), 732 QualType::getFromOpaquePtr(type2)); 733} 734 735#pragma mark CVR modifiers 736 737clang_type_t 738ClangASTContext::AddConstModifier (clang_type_t 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 749clang_type_t 750ClangASTContext::AddRestrictModifier (clang_type_t 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 761clang_type_t 762ClangASTContext::AddVolatileModifier (clang_type_t 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 775clang_type_t 776ClangASTContext::CreateRecordType (const char *name, int kind, DeclContext *decl_ctx, LanguageType language) 777{ 778 ASTContext *ast_context = getASTContext(); 779 assert (ast_context != NULL); 780 781 if (decl_ctx == NULL) 782 decl_ctx = ast_context->getTranslationUnitDecl(); 783 784 785 if (language == eLanguageTypeObjC) 786 { 787 bool isForwardDecl = true; 788 bool isInternal = false; 789 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal); 790 } 791 792 // 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 806static bool 807IsOperator (const char *name, OverloadedOperatorKind &op_kind) 808{ 809 if (name == NULL || name[0] == '\0') 810 return false; 811 812 if (::strstr(name, "operator ") != name) 813 return false; 814 815 const char *post_op_name = name + 9; 816 817 // This is an operator, set the overloaded operator kind to invalid 818 // in case this is a conversion operator... 819 op_kind = NUM_OVERLOADED_OPERATORS; 820 821 switch (post_op_name[0]) 822 { 823 case 'n': 824 if (strcmp (post_op_name, "new") == 0) 825 op_kind = OO_New; 826 else if (strcmp (post_op_name, "new[]") == 0) 827 op_kind = OO_Array_New; 828 break; 829 830 case 'd': 831 if (strcmp (post_op_name, "delete") == 0) 832 op_kind = OO_Delete; 833 else if (strcmp (post_op_name, "delete[]") == 0) 834 op_kind = OO_Array_Delete; 835 break; 836 837 case '+': 838 if (post_op_name[1] == '\0') 839 op_kind = OO_Plus; 840 else if (post_op_name[2] == '\0') 841 { 842 if (post_op_name[1] == '=') 843 op_kind = OO_PlusEqual; 844 else if (post_op_name[1] == '+') 845 op_kind = OO_PlusPlus; 846 } 847 break; 848 849 case '-': 850 if (post_op_name[1] == '\0') 851 op_kind = OO_Minus; 852 else if (post_op_name[2] == '\0') 853 { 854 switch (post_op_name[1]) 855 { 856 case '=': op_kind = OO_MinusEqual; break; 857 case '-': op_kind = OO_MinusMinus; break; 858 case '>': op_kind = OO_Arrow; break; 859 } 860 } 861 else if (post_op_name[3] == '\0') 862 { 863 if (post_op_name[2] == '*') 864 op_kind = OO_ArrowStar; break; 865 } 866 break; 867 868 case '*': 869 if (post_op_name[1] == '\0') 870 op_kind = OO_Star; 871 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 872 op_kind = OO_StarEqual; 873 break; 874 875 case '/': 876 if (post_op_name[1] == '\0') 877 op_kind = OO_Slash; 878 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 879 op_kind = OO_SlashEqual; 880 break; 881 882 case '%': 883 if (post_op_name[1] == '\0') 884 op_kind = OO_Percent; 885 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 886 op_kind = OO_PercentEqual; 887 break; 888 889 890 case '^': 891 if (post_op_name[1] == '\0') 892 op_kind = OO_Caret; 893 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 894 op_kind = OO_CaretEqual; 895 break; 896 897 case '&': 898 if (post_op_name[1] == '\0') 899 op_kind = OO_Amp; 900 else if (post_op_name[2] == '\0') 901 { 902 switch (post_op_name[1]) 903 { 904 case '=': op_kind = OO_AmpEqual; break; 905 case '&': op_kind = OO_AmpAmp; break; 906 } 907 } 908 break; 909 910 case '|': 911 if (post_op_name[1] == '\0') 912 op_kind = OO_Pipe; 913 else if (post_op_name[2] == '\0') 914 { 915 switch (post_op_name[1]) 916 { 917 case '=': op_kind = OO_PipeEqual; break; 918 case '|': op_kind = OO_PipePipe; break; 919 } 920 } 921 break; 922 923 case '~': 924 if (post_op_name[1] == '\0') 925 op_kind = OO_Tilde; 926 break; 927 928 case '!': 929 if (post_op_name[1] == '\0') 930 op_kind = OO_Exclaim; 931 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 932 op_kind = OO_ExclaimEqual; 933 break; 934 935 case '=': 936 if (post_op_name[1] == '\0') 937 op_kind = OO_Equal; 938 else if (post_op_name[1] == '=' && post_op_name[2] == '\0') 939 op_kind = OO_EqualEqual; 940 break; 941 942 case '<': 943 if (post_op_name[1] == '\0') 944 op_kind = OO_Less; 945 else if (post_op_name[2] == '\0') 946 { 947 switch (post_op_name[1]) 948 { 949 case '<': op_kind = OO_LessLess; break; 950 case '=': op_kind = OO_LessEqual; break; 951 } 952 } 953 else if (post_op_name[3] == '\0') 954 { 955 if (post_op_name[2] == '=') 956 op_kind = OO_LessLessEqual; 957 } 958 break; 959 960 case '>': 961 if (post_op_name[1] == '\0') 962 op_kind = OO_Greater; 963 else if (post_op_name[2] == '\0') 964 { 965 switch (post_op_name[1]) 966 { 967 case '>': op_kind = OO_GreaterGreater; break; 968 case '=': op_kind = OO_GreaterEqual; break; 969 } 970 } 971 else if (post_op_name[1] == '>' && 972 post_op_name[2] == '=' && 973 post_op_name[3] == '\0') 974 { 975 op_kind = OO_GreaterGreaterEqual; 976 } 977 break; 978 979 case ',': 980 if (post_op_name[1] == '\0') 981 op_kind = OO_Comma; 982 break; 983 984 case '(': 985 if (post_op_name[1] == ')' && post_op_name[2] == '\0') 986 op_kind = OO_Call; 987 break; 988 989 case '[': 990 if (post_op_name[1] == ']' && post_op_name[2] == '\0') 991 op_kind = OO_Subscript; 992 break; 993 } 994 995 return true; 996} 997CXXMethodDecl * 998ClangASTContext::AddMethodToCXXRecordType 999( 1000 ASTContext *ast_context, 1001 clang_type_t record_opaque_type, 1002 const char *name, 1003 clang_type_t method_opaque_type, 1004 lldb::AccessType access, 1005 bool is_virtual, 1006 bool is_static, 1007 bool is_inline, 1008 bool is_explicit 1009) 1010{ 1011 if (!record_opaque_type || !method_opaque_type || !name) 1012 return NULL; 1013 1014 assert(ast_context); 1015 1016 IdentifierTable *identifier_table = &ast_context->Idents; 1017 1018 assert(identifier_table); 1019 1020 QualType record_qual_type(QualType::getFromOpaquePtr(record_opaque_type)); 1021 1022 clang::Type *clang_type(record_qual_type.getTypePtr()); 1023 1024 if (clang_type == NULL) 1025 return NULL; 1026 1027 RecordType *record_clang_type(dyn_cast<RecordType>(clang_type)); 1028 1029 if (record_clang_type == NULL) 1030 return NULL; 1031 1032 RecordDecl *record_decl = record_clang_type->getDecl(); 1033 1034 if (record_decl == NULL) 1035 return NULL; 1036 1037 CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1038 1039 if (cxx_record_decl == NULL) 1040 return NULL; 1041 1042 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type)); 1043 1044 CXXMethodDecl *cxx_method_decl = NULL; 1045 1046 DeclarationName decl_name (&identifier_table->get(name)); 1047 1048 const bool is_implicitly_declared = false; 1049 1050 clang::FunctionType *function_Type = dyn_cast<FunctionType>(method_qual_type.getTypePtr()); 1051 1052 if (function_Type == NULL) 1053 return NULL; 1054 1055 FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(function_Type)); 1056 1057 if (!method_function_prototype) 1058 return NULL; 1059 1060 unsigned int num_params = method_function_prototype->getNumArgs(); 1061 1062 if (name[0] == '~') 1063 { 1064 cxx_method_decl = CXXDestructorDecl::Create (*ast_context, 1065 cxx_record_decl, 1066 DeclarationNameInfo (ast_context->DeclarationNames.getCXXDestructorName (ast_context->getCanonicalType (record_qual_type)), SourceLocation()), 1067 method_qual_type, 1068 is_inline, 1069 is_implicitly_declared); 1070 } 1071 else if (decl_name == record_decl->getDeclName()) 1072 { 1073 cxx_method_decl = CXXConstructorDecl::Create (*ast_context, 1074 cxx_record_decl, 1075 DeclarationNameInfo (ast_context->DeclarationNames.getCXXConstructorName (ast_context->getCanonicalType (record_qual_type)), SourceLocation()), 1076 method_qual_type, 1077 NULL, // TypeSourceInfo * 1078 is_explicit, 1079 is_inline, 1080 is_implicitly_declared); 1081 } 1082 else 1083 { 1084 1085 OverloadedOperatorKind op_kind = NUM_OVERLOADED_OPERATORS; 1086 if (IsOperator (name, op_kind)) 1087 { 1088 if (op_kind != NUM_OVERLOADED_OPERATORS) 1089 { 1090 cxx_method_decl = CXXMethodDecl::Create (*ast_context, 1091 cxx_record_decl, 1092 DeclarationNameInfo (ast_context->DeclarationNames.getCXXOperatorName (op_kind), SourceLocation()), 1093 method_qual_type, 1094 NULL, // TypeSourceInfo * 1095 is_static, 1096 SC_None, 1097 is_inline); 1098 } 1099 else if (num_params == 0) 1100 { 1101 // Conversion operators don't take params... 1102 cxx_method_decl = CXXConversionDecl::Create (*ast_context, 1103 cxx_record_decl, 1104 DeclarationNameInfo (ast_context->DeclarationNames.getCXXConversionFunctionName (ast_context->getCanonicalType (function_Type->getResultType())), SourceLocation()), 1105 method_qual_type, 1106 NULL, // TypeSourceInfo * 1107 is_inline, 1108 is_explicit); 1109 } 1110 } 1111 1112 if (cxx_method_decl == NULL) 1113 { 1114 cxx_method_decl = CXXMethodDecl::Create (*ast_context, 1115 cxx_record_decl, 1116 DeclarationNameInfo (decl_name, SourceLocation()), 1117 method_qual_type, 1118 NULL, // TypeSourceInfo * 1119 is_static, 1120 SC_None, 1121 is_inline); 1122 } 1123 } 1124 1125 AccessSpecifier access_specifier = ConvertAccessTypeToAccessSpecifier (access); 1126 1127 cxx_method_decl->setAccess (access_specifier); 1128 cxx_method_decl->setVirtualAsWritten (is_virtual); 1129 1130 // Populate the method decl with parameter decls 1131 1132 ParmVarDecl *params[num_params]; 1133 1134 for (int param_index = 0; 1135 param_index < num_params; 1136 ++param_index) 1137 { 1138 params[param_index] = ParmVarDecl::Create (*ast_context, 1139 cxx_method_decl, 1140 SourceLocation(), 1141 NULL, // anonymous 1142 method_function_prototype->getArgType(param_index), 1143 NULL, 1144 SC_None, 1145 SC_None, 1146 NULL); 1147 } 1148 1149 cxx_method_decl->setParams (params, num_params); 1150 1151 cxx_record_decl->addDecl (cxx_method_decl); 1152 1153 return cxx_method_decl; 1154} 1155 1156bool 1157ClangASTContext::AddFieldToRecordType 1158( 1159 ASTContext *ast_context, 1160 clang_type_t record_clang_type, 1161 const char *name, 1162 clang_type_t field_type, 1163 AccessType access, 1164 uint32_t bitfield_bit_size 1165) 1166{ 1167 if (record_clang_type == NULL || field_type == NULL) 1168 return false; 1169 1170 IdentifierTable *identifier_table = &ast_context->Idents; 1171 1172 assert (ast_context != NULL); 1173 assert (identifier_table != NULL); 1174 1175 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type)); 1176 1177 clang::Type *clang_type = record_qual_type.getTypePtr(); 1178 if (clang_type) 1179 { 1180 const RecordType *record_type = dyn_cast<RecordType>(clang_type); 1181 1182 if (record_type) 1183 { 1184 RecordDecl *record_decl = record_type->getDecl(); 1185 1186 clang::Expr *bit_width = NULL; 1187 if (bitfield_bit_size != 0) 1188 { 1189 APInt bitfield_bit_size_apint(ast_context->getTypeSize(ast_context->IntTy), bitfield_bit_size); 1190 bit_width = new (*ast_context)IntegerLiteral (*ast_context, bitfield_bit_size_apint, ast_context->IntTy, SourceLocation()); 1191 } 1192 FieldDecl *field = FieldDecl::Create (*ast_context, 1193 record_decl, 1194 SourceLocation(), 1195 name ? &identifier_table->get(name) : NULL, // Identifier 1196 QualType::getFromOpaquePtr(field_type), // Field type 1197 NULL, // DeclaratorInfo * 1198 bit_width, // BitWidth 1199 false); // Mutable 1200 1201 field->setAccess (ConvertAccessTypeToAccessSpecifier (access)); 1202 1203 if (field) 1204 { 1205 record_decl->addDecl(field); 1206 } 1207 } 1208 else 1209 { 1210 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(clang_type); 1211 if (objc_class_type) 1212 { 1213 bool is_synthesized = false; 1214 ClangASTContext::AddObjCClassIVar (ast_context, 1215 record_clang_type, 1216 name, 1217 field_type, 1218 access, 1219 bitfield_bit_size, 1220 is_synthesized); 1221 } 1222 } 1223 } 1224 return false; 1225} 1226 1227bool 1228ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size) 1229{ 1230 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size); 1231} 1232 1233bool 1234ClangASTContext::FieldIsBitfield 1235( 1236 ASTContext *ast_context, 1237 FieldDecl* field, 1238 uint32_t& bitfield_bit_size 1239) 1240{ 1241 if (ast_context == NULL || field == NULL) 1242 return false; 1243 1244 if (field->isBitField()) 1245 { 1246 Expr* bit_width_expr = field->getBitWidth(); 1247 if (bit_width_expr) 1248 { 1249 llvm::APSInt bit_width_apsint; 1250 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast_context)) 1251 { 1252 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX); 1253 return true; 1254 } 1255 } 1256 } 1257 return false; 1258} 1259 1260bool 1261ClangASTContext::RecordHasFields (const RecordDecl *record_decl) 1262{ 1263 if (record_decl == NULL) 1264 return false; 1265 1266 if (!record_decl->field_empty()) 1267 return true; 1268 1269 // No fields, lets check this is a CXX record and check the base classes 1270 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1271 if (cxx_record_decl) 1272 { 1273 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1274 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1275 base_class != base_class_end; 1276 ++base_class) 1277 { 1278 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 1279 if (RecordHasFields(base_class_decl)) 1280 return true; 1281 } 1282 } 1283 return false; 1284} 1285 1286void 1287ClangASTContext::SetDefaultAccessForRecordFields (clang_type_t clang_qual_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities) 1288{ 1289 if (clang_qual_type) 1290 { 1291 QualType qual_type(QualType::getFromOpaquePtr(clang_qual_type)); 1292 clang::Type *clang_type = qual_type.getTypePtr(); 1293 if (clang_type) 1294 { 1295 RecordType *record_type = dyn_cast<RecordType>(clang_type); 1296 if (record_type) 1297 { 1298 RecordDecl *record_decl = record_type->getDecl(); 1299 if (record_decl) 1300 { 1301 uint32_t field_idx; 1302 RecordDecl::field_iterator field, field_end; 1303 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0; 1304 field != field_end; 1305 ++field, ++field_idx) 1306 { 1307 // If no accessibility was assigned, assign the correct one 1308 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none) 1309 field->setAccess ((AccessSpecifier)default_accessibility); 1310 } 1311 } 1312 } 1313 } 1314 } 1315} 1316 1317#pragma mark C++ Base Classes 1318 1319CXXBaseSpecifier * 1320ClangASTContext::CreateBaseClassSpecifier (clang_type_t base_class_type, AccessType access, bool is_virtual, bool base_of_class) 1321{ 1322 if (base_class_type) 1323 return new CXXBaseSpecifier (SourceRange(), 1324 is_virtual, 1325 base_of_class, 1326 ConvertAccessTypeToAccessSpecifier (access), 1327 getASTContext()->CreateTypeSourceInfo (QualType::getFromOpaquePtr(base_class_type))); 1328 return NULL; 1329} 1330 1331void 1332ClangASTContext::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes) 1333{ 1334 for (unsigned i=0; i<num_base_classes; ++i) 1335 { 1336 delete base_classes[i]; 1337 base_classes[i] = NULL; 1338 } 1339} 1340 1341bool 1342ClangASTContext::SetBaseClassesForClassType (clang_type_t class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes) 1343{ 1344 if (class_clang_type) 1345 { 1346 clang::Type *clang_type = QualType::getFromOpaquePtr(class_clang_type).getTypePtr(); 1347 if (clang_type) 1348 { 1349 RecordType *record_type = dyn_cast<RecordType>(clang_type); 1350 if (record_type) 1351 { 1352 CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_type->getDecl()); 1353 if (cxx_record_decl) 1354 { 1355 cxx_record_decl->setBases(base_classes, num_base_classes); 1356 return true; 1357 } 1358 } 1359 } 1360 } 1361 return false; 1362} 1363#pragma mark Objective C Classes 1364 1365clang_type_t 1366ClangASTContext::CreateObjCClass 1367( 1368 const char *name, 1369 DeclContext *decl_ctx, 1370 bool isForwardDecl, 1371 bool isInternal 1372) 1373{ 1374 ASTContext *ast_context = getASTContext(); 1375 assert (ast_context != NULL); 1376 assert (name && name[0]); 1377 if (decl_ctx == NULL) 1378 decl_ctx = ast_context->getTranslationUnitDecl(); 1379 1380 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and 1381 // we will need to update this code. I was told to currently always use 1382 // the CXXRecordDecl class since we often don't know from debug information 1383 // if something is struct or a class, so we default to always use the more 1384 // complete definition just in case. 1385 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast_context, 1386 decl_ctx, 1387 SourceLocation(), 1388 &ast_context->Idents.get(name), 1389 SourceLocation(), 1390 isForwardDecl, 1391 isInternal); 1392 1393 return ast_context->getObjCInterfaceType(decl).getAsOpaquePtr(); 1394} 1395 1396bool 1397ClangASTContext::SetObjCSuperClass (clang_type_t class_opaque_type, clang_type_t super_opaque_type) 1398{ 1399 if (class_opaque_type && super_opaque_type) 1400 { 1401 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 1402 QualType super_qual_type(QualType::getFromOpaquePtr(super_opaque_type)); 1403 clang::Type *class_type = class_qual_type.getTypePtr(); 1404 clang::Type *super_type = super_qual_type.getTypePtr(); 1405 if (class_type && super_type) 1406 { 1407 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 1408 ObjCObjectType *objc_super_type = dyn_cast<ObjCObjectType>(super_type); 1409 if (objc_class_type && objc_super_type) 1410 { 1411 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 1412 ObjCInterfaceDecl *super_interface_decl = objc_super_type->getInterface(); 1413 if (class_interface_decl && super_interface_decl) 1414 { 1415 class_interface_decl->setSuperClass(super_interface_decl); 1416 return true; 1417 } 1418 } 1419 } 1420 } 1421 return false; 1422} 1423 1424 1425bool 1426ClangASTContext::AddObjCClassIVar 1427( 1428 ASTContext *ast_context, 1429 clang_type_t class_opaque_type, 1430 const char *name, 1431 clang_type_t ivar_opaque_type, 1432 AccessType access, 1433 uint32_t bitfield_bit_size, 1434 bool is_synthesized 1435) 1436{ 1437 if (class_opaque_type == NULL || ivar_opaque_type == NULL) 1438 return false; 1439 1440 IdentifierTable *identifier_table = &ast_context->Idents; 1441 1442 assert (ast_context != NULL); 1443 assert (identifier_table != NULL); 1444 1445 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 1446 1447 clang::Type *class_type = class_qual_type.getTypePtr(); 1448 if (class_type) 1449 { 1450 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 1451 1452 if (objc_class_type) 1453 { 1454 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 1455 1456 if (class_interface_decl) 1457 { 1458 clang::Expr *bit_width = NULL; 1459 if (bitfield_bit_size != 0) 1460 { 1461 APInt bitfield_bit_size_apint(ast_context->getTypeSize(ast_context->IntTy), bitfield_bit_size); 1462 bit_width = new (*ast_context)IntegerLiteral (*ast_context, bitfield_bit_size_apint, ast_context->IntTy, SourceLocation()); 1463 } 1464 1465 ObjCIvarDecl *field = ObjCIvarDecl::Create (*ast_context, 1466 class_interface_decl, 1467 SourceLocation(), 1468 &identifier_table->get(name), // Identifier 1469 QualType::getFromOpaquePtr(ivar_opaque_type), // Field type 1470 NULL, // TypeSourceInfo * 1471 ConvertAccessTypeToObjCIvarAccessControl (access), 1472 bit_width, 1473 is_synthesized); 1474 1475 if (field) 1476 { 1477 class_interface_decl->addDecl(field); 1478 return true; 1479 } 1480 } 1481 } 1482 } 1483 return false; 1484} 1485 1486 1487bool 1488ClangASTContext::ObjCTypeHasIVars (clang_type_t class_opaque_type, bool check_superclass) 1489{ 1490 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 1491 1492 clang::Type *class_type = class_qual_type.getTypePtr(); 1493 if (class_type) 1494 { 1495 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 1496 1497 if (objc_class_type) 1498 return ObjCDeclHasIVars (objc_class_type->getInterface(), check_superclass); 1499 } 1500 return false; 1501} 1502 1503bool 1504ClangASTContext::ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass) 1505{ 1506 while (class_interface_decl) 1507 { 1508 if (class_interface_decl->ivar_size() > 0) 1509 return true; 1510 1511 if (check_superclass) 1512 class_interface_decl = class_interface_decl->getSuperClass(); 1513 else 1514 break; 1515 } 1516 return false; 1517} 1518 1519ObjCMethodDecl * 1520ClangASTContext::AddMethodToObjCObjectType 1521( 1522 ASTContext *ast_context, 1523 clang_type_t class_opaque_type, 1524 const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]") 1525 clang_type_t method_opaque_type, 1526 lldb::AccessType access 1527) 1528{ 1529 if (class_opaque_type == NULL || method_opaque_type == NULL) 1530 return NULL; 1531 1532 IdentifierTable *identifier_table = &ast_context->Idents; 1533 1534 assert (ast_context != NULL); 1535 assert (identifier_table != NULL); 1536 1537 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 1538 1539 clang::Type *class_type = class_qual_type.getTypePtr(); 1540 if (class_type == NULL) 1541 return NULL; 1542 1543 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 1544 1545 if (objc_class_type == NULL) 1546 return NULL; 1547 1548 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 1549 1550 if (class_interface_decl == NULL) 1551 return NULL; 1552 1553 const char *selector_start = ::strchr (name, ' '); 1554 if (selector_start == NULL) 1555 return NULL; 1556 1557 selector_start++; 1558 if (!(::isalpha (selector_start[0]) || selector_start[0] == '_')) 1559 return NULL; 1560 llvm::SmallVector<IdentifierInfo *, 12> selector_idents; 1561 1562 size_t len = 0; 1563 const char *start; 1564 //printf ("name = '%s'\n", name); 1565 1566 unsigned num_selectors_with_args = 0; 1567 for (start = selector_start; 1568 start && *start != '\0' && *start != ']'; 1569 start += len) 1570 { 1571 len = ::strcspn(start, ":]"); 1572 if (start[len] == ':') 1573 { 1574 ++num_selectors_with_args; 1575 len += 1; 1576 } 1577 //printf ("@selector[%zu] = '%.*s'\n", selector_idents.size(), (int)len, start); 1578 selector_idents.push_back (&identifier_table->get (StringRef (start, len))); 1579 } 1580 1581 1582 if (selector_idents.size() == 0) 1583 return 0; 1584 1585 clang::Selector method_selector = ast_context->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0, 1586 selector_idents.data()); 1587 1588 QualType method_qual_type (QualType::getFromOpaquePtr (method_opaque_type)); 1589 1590 // Populate the method decl with parameter decls 1591 clang::Type *method_type(method_qual_type.getTypePtr()); 1592 1593 if (method_type == NULL) 1594 return NULL; 1595 1596 FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type)); 1597 1598 if (!method_function_prototype) 1599 return NULL; 1600 1601 1602 bool is_variadic = false; 1603 bool is_synthesized = false; 1604 bool is_defined = false; 1605 ObjCMethodDecl::ImplementationControl imp_control = ObjCMethodDecl::None; 1606 1607 const unsigned num_args = method_function_prototype->getNumArgs(); 1608 1609 ObjCMethodDecl *objc_method_decl = ObjCMethodDecl::Create (*ast_context, 1610 SourceLocation(), // beginLoc, 1611 SourceLocation(), // endLoc, 1612 method_selector, 1613 method_function_prototype->getResultType(), 1614 NULL, // TypeSourceInfo *ResultTInfo, 1615 GetDeclContextForType (class_opaque_type), 1616 name[0] == '-', 1617 is_variadic, 1618 is_synthesized, 1619 is_defined, 1620 imp_control, 1621 num_args); 1622 1623 1624 if (objc_method_decl == NULL) 1625 return NULL; 1626 1627 if (num_args > 0) 1628 { 1629 llvm::SmallVector<ParmVarDecl *, 12> params; 1630 1631 for (int param_index = 0; param_index < num_args; ++param_index) 1632 { 1633 params.push_back (ParmVarDecl::Create (*ast_context, 1634 objc_method_decl, 1635 SourceLocation(), 1636 NULL, // anonymous 1637 method_function_prototype->getArgType(param_index), 1638 NULL, 1639 SC_Auto, 1640 SC_Auto, 1641 NULL)); 1642 } 1643 1644 objc_method_decl->setMethodParams(*ast_context, params.data(), params.size(), num_args); 1645 } 1646 1647 class_interface_decl->addDecl (objc_method_decl); 1648 1649 1650 return objc_method_decl; 1651} 1652 1653 1654 1655#pragma mark Aggregate Types 1656 1657bool 1658ClangASTContext::IsAggregateType (clang_type_t clang_type) 1659{ 1660 if (clang_type == NULL) 1661 return false; 1662 1663 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 1664 1665 if (qual_type->isAggregateType ()) 1666 return true; 1667 1668 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 1669 switch (type_class) 1670 { 1671 case clang::Type::IncompleteArray: 1672 case clang::Type::VariableArray: 1673 case clang::Type::ConstantArray: 1674 case clang::Type::ExtVector: 1675 case clang::Type::Vector: 1676 case clang::Type::Record: 1677 case clang::Type::ObjCObject: 1678 case clang::Type::ObjCInterface: 1679 return true; 1680 1681 case clang::Type::Typedef: 1682 return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr()); 1683 1684 default: 1685 break; 1686 } 1687 // The clang type does have a value 1688 return false; 1689} 1690 1691uint32_t 1692ClangASTContext::GetNumChildren (clang_type_t clang_qual_type, bool omit_empty_base_classes) 1693{ 1694 if (clang_qual_type == NULL) 1695 return 0; 1696 1697 uint32_t num_children = 0; 1698 QualType qual_type(QualType::getFromOpaquePtr(clang_qual_type)); 1699 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 1700 switch (type_class) 1701 { 1702 case clang::Type::Builtin: 1703 switch (cast<clang::BuiltinType>(qual_type)->getKind()) 1704 { 1705 case clang::BuiltinType::ObjCId: // Child is Class 1706 case clang::BuiltinType::ObjCClass: // child is Class 1707 case clang::BuiltinType::ObjCSel: // child is const char * 1708 num_children = 1; 1709 1710 default: 1711 break; 1712 } 1713 break; 1714 1715 case clang::Type::Record: 1716 { 1717 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 1718 const RecordDecl *record_decl = record_type->getDecl(); 1719 assert(record_decl); 1720 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1721 if (cxx_record_decl) 1722 { 1723 if (omit_empty_base_classes) 1724 { 1725 // Check each base classes to see if it or any of its 1726 // base classes contain any fields. This can help 1727 // limit the noise in variable views by not having to 1728 // show base classes that contain no members. 1729 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1730 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1731 base_class != base_class_end; 1732 ++base_class) 1733 { 1734 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 1735 1736 // Skip empty base classes 1737 if (RecordHasFields(base_class_decl) == false) 1738 continue; 1739 1740 num_children++; 1741 } 1742 } 1743 else 1744 { 1745 // Include all base classes 1746 num_children += cxx_record_decl->getNumBases(); 1747 } 1748 1749 } 1750 RecordDecl::field_iterator field, field_end; 1751 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field) 1752 ++num_children; 1753 } 1754 break; 1755 1756 case clang::Type::ObjCObject: 1757 case clang::Type::ObjCInterface: 1758 { 1759 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 1760 assert (objc_class_type); 1761 if (objc_class_type) 1762 { 1763 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 1764 1765 if (class_interface_decl) 1766 { 1767 1768 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 1769 if (superclass_interface_decl) 1770 { 1771 if (omit_empty_base_classes) 1772 { 1773 if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true)) 1774 ++num_children; 1775 } 1776 else 1777 ++num_children; 1778 } 1779 1780 num_children += class_interface_decl->ivar_size(); 1781 } 1782 } 1783 } 1784 break; 1785 1786 case clang::Type::ObjCObjectPointer: 1787 { 1788 ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr()); 1789 QualType pointee_type = pointer_type->getPointeeType(); 1790 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (pointee_type.getAsOpaquePtr(), 1791 omit_empty_base_classes); 1792 // If this type points to a simple type, then it has 1 child 1793 if (num_pointee_children == 0) 1794 num_children = 1; 1795 else 1796 num_children = num_pointee_children; 1797 } 1798 break; 1799 1800 case clang::Type::ConstantArray: 1801 num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue(); 1802 break; 1803 1804 case clang::Type::Pointer: 1805 { 1806 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 1807 QualType pointee_type = pointer_type->getPointeeType(); 1808 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (pointee_type.getAsOpaquePtr(), 1809 omit_empty_base_classes); 1810 // If this type points to a simple type, then it has 1 child 1811 if (num_pointee_children == 0) 1812 num_children = 1; 1813 else 1814 num_children = num_pointee_children; 1815 } 1816 break; 1817 1818 case clang::Type::Typedef: 1819 num_children = ClangASTContext::GetNumChildren (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), omit_empty_base_classes); 1820 break; 1821 1822 default: 1823 break; 1824 } 1825 return num_children; 1826} 1827 1828 1829clang_type_t 1830ClangASTContext::GetChildClangTypeAtIndex 1831( 1832 const char *parent_name, 1833 clang_type_t parent_clang_type, 1834 uint32_t idx, 1835 bool transparent_pointers, 1836 bool omit_empty_base_classes, 1837 std::string& child_name, 1838 uint32_t &child_byte_size, 1839 int32_t &child_byte_offset, 1840 uint32_t &child_bitfield_bit_size, 1841 uint32_t &child_bitfield_bit_offset 1842) 1843{ 1844 if (parent_clang_type) 1845 1846 return GetChildClangTypeAtIndex (getASTContext(), 1847 parent_name, 1848 parent_clang_type, 1849 idx, 1850 transparent_pointers, 1851 omit_empty_base_classes, 1852 child_name, 1853 child_byte_size, 1854 child_byte_offset, 1855 child_bitfield_bit_size, 1856 child_bitfield_bit_offset); 1857 return NULL; 1858} 1859 1860clang_type_t 1861ClangASTContext::GetChildClangTypeAtIndex 1862( 1863 ASTContext *ast_context, 1864 const char *parent_name, 1865 clang_type_t parent_clang_type, 1866 uint32_t idx, 1867 bool transparent_pointers, 1868 bool omit_empty_base_classes, 1869 std::string& child_name, 1870 uint32_t &child_byte_size, 1871 int32_t &child_byte_offset, 1872 uint32_t &child_bitfield_bit_size, 1873 uint32_t &child_bitfield_bit_offset 1874) 1875{ 1876 if (parent_clang_type == NULL) 1877 return NULL; 1878 1879 if (idx < ClangASTContext::GetNumChildren (parent_clang_type, omit_empty_base_classes)) 1880 { 1881 uint32_t bit_offset; 1882 child_bitfield_bit_size = 0; 1883 child_bitfield_bit_offset = 0; 1884 QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type)); 1885 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass(); 1886 switch (parent_type_class) 1887 { 1888 case clang::Type::Builtin: 1889 switch (cast<clang::BuiltinType>(parent_qual_type)->getKind()) 1890 { 1891 case clang::BuiltinType::ObjCId: 1892 case clang::BuiltinType::ObjCClass: 1893 return ast_context->ObjCBuiltinClassTy.getAsOpaquePtr(); 1894 1895 case clang::BuiltinType::ObjCSel: 1896 { 1897 QualType char_type(ast_context->CharTy); 1898 char_type.addConst(); 1899 return ast_context->getPointerType(char_type).getAsOpaquePtr(); 1900 } 1901 break; 1902 1903 default: 1904 break; 1905 } 1906 break; 1907 1908 1909 case clang::Type::Record: 1910 { 1911 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr()); 1912 const RecordDecl *record_decl = record_type->getDecl(); 1913 assert(record_decl); 1914 const ASTRecordLayout &record_layout = ast_context->getASTRecordLayout(record_decl); 1915 uint32_t child_idx = 0; 1916 1917 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1918 if (cxx_record_decl) 1919 { 1920 // We might have base classes to print out first 1921 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1922 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1923 base_class != base_class_end; 1924 ++base_class) 1925 { 1926 const CXXRecordDecl *base_class_decl = NULL; 1927 1928 // Skip empty base classes 1929 if (omit_empty_base_classes) 1930 { 1931 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 1932 if (RecordHasFields(base_class_decl) == false) 1933 continue; 1934 } 1935 1936 if (idx == child_idx) 1937 { 1938 if (base_class_decl == NULL) 1939 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 1940 1941 1942 if (base_class->isVirtual()) 1943 bit_offset = record_layout.getVBaseClassOffset(base_class_decl); 1944 else 1945 bit_offset = record_layout.getBaseClassOffset(base_class_decl); 1946 1947 // Base classes should be a multiple of 8 bits in size 1948 assert (bit_offset % 8 == 0); 1949 child_byte_offset = bit_offset/8; 1950 std::string base_class_type_name(base_class->getType().getAsString()); 1951 1952 child_name.assign(base_class_type_name.c_str()); 1953 1954 uint64_t clang_type_info_bit_size = ast_context->getTypeSize(base_class->getType()); 1955 1956 // Base classes biut sizes should be a multiple of 8 bits in size 1957 assert (clang_type_info_bit_size % 8 == 0); 1958 child_byte_size = clang_type_info_bit_size / 8; 1959 return base_class->getType().getAsOpaquePtr(); 1960 } 1961 // We don't increment the child index in the for loop since we might 1962 // be skipping empty base classes 1963 ++child_idx; 1964 } 1965 } 1966 // Make sure index is in range... 1967 uint32_t field_idx = 0; 1968 RecordDecl::field_iterator field, field_end; 1969 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx) 1970 { 1971 if (idx == child_idx) 1972 { 1973 // Print the member type if requested 1974 // Print the member name and equal sign 1975 child_name.assign(field->getNameAsString().c_str()); 1976 1977 // Figure out the type byte size (field_type_info.first) and 1978 // alignment (field_type_info.second) from the AST context. 1979 std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(field->getType()); 1980 assert(field_idx < record_layout.getFieldCount()); 1981 1982 child_byte_size = field_type_info.first / 8; 1983 1984 // Figure out the field offset within the current struct/union/class type 1985 bit_offset = record_layout.getFieldOffset (field_idx); 1986 child_byte_offset = bit_offset / 8; 1987 if (ClangASTContext::FieldIsBitfield (ast_context, *field, child_bitfield_bit_size)) 1988 child_bitfield_bit_offset = bit_offset % 8; 1989 1990 return field->getType().getAsOpaquePtr(); 1991 } 1992 } 1993 } 1994 break; 1995 1996 case clang::Type::ObjCObject: 1997 case clang::Type::ObjCInterface: 1998 { 1999 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr()); 2000 assert (objc_class_type); 2001 if (objc_class_type) 2002 { 2003 uint32_t child_idx = 0; 2004 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2005 2006 if (class_interface_decl) 2007 { 2008 2009 const ASTRecordLayout &interface_layout = ast_context->getASTObjCInterfaceLayout(class_interface_decl); 2010 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 2011 if (superclass_interface_decl) 2012 { 2013 if (omit_empty_base_classes) 2014 { 2015 if (ClangASTContext::GetNumChildren(ast_context->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0) 2016 { 2017 if (idx == 0) 2018 { 2019 QualType ivar_qual_type(ast_context->getObjCInterfaceType(superclass_interface_decl)); 2020 2021 2022 child_name.assign(superclass_interface_decl->getNameAsString().c_str()); 2023 2024 std::pair<uint64_t, unsigned> ivar_type_info = ast_context->getTypeInfo(ivar_qual_type.getTypePtr()); 2025 2026 child_byte_size = ivar_type_info.first / 8; 2027 child_byte_offset = 0; 2028 2029 return ivar_qual_type.getAsOpaquePtr(); 2030 } 2031 2032 ++child_idx; 2033 } 2034 } 2035 else 2036 ++child_idx; 2037 } 2038 2039 const uint32_t superclass_idx = child_idx; 2040 2041 if (idx < (child_idx + class_interface_decl->ivar_size())) 2042 { 2043 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); 2044 2045 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos) 2046 { 2047 if (child_idx == idx) 2048 { 2049 const ObjCIvarDecl* ivar_decl = *ivar_pos; 2050 2051 QualType ivar_qual_type(ivar_decl->getType()); 2052 2053 child_name.assign(ivar_decl->getNameAsString().c_str()); 2054 2055 std::pair<uint64_t, unsigned> ivar_type_info = ast_context->getTypeInfo(ivar_qual_type.getTypePtr()); 2056 2057 child_byte_size = ivar_type_info.first / 8; 2058 2059 // Figure out the field offset within the current struct/union/class type 2060 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx); 2061 child_byte_offset = bit_offset / 8; 2062 2063 return ivar_qual_type.getAsOpaquePtr(); 2064 } 2065 ++child_idx; 2066 } 2067 } 2068 } 2069 } 2070 } 2071 break; 2072 2073 case clang::Type::ObjCObjectPointer: 2074 { 2075 ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr()); 2076 QualType pointee_type = pointer_type->getPointeeType(); 2077 2078 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 2079 { 2080 return GetChildClangTypeAtIndex (ast_context, 2081 parent_name, 2082 pointer_type->getPointeeType().getAsOpaquePtr(), 2083 idx, 2084 transparent_pointers, 2085 omit_empty_base_classes, 2086 child_name, 2087 child_byte_size, 2088 child_byte_offset, 2089 child_bitfield_bit_size, 2090 child_bitfield_bit_offset); 2091 } 2092 else 2093 { 2094 if (parent_name) 2095 { 2096 child_name.assign(1, '*'); 2097 child_name += parent_name; 2098 } 2099 2100 // We have a pointer to an simple type 2101 if (idx == 0) 2102 { 2103 std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type); 2104 assert(clang_type_info.first % 8 == 0); 2105 child_byte_size = clang_type_info.first / 8; 2106 child_byte_offset = 0; 2107 return pointee_type.getAsOpaquePtr(); 2108 } 2109 } 2110 } 2111 break; 2112 2113 case clang::Type::ConstantArray: 2114 { 2115 const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); 2116 const uint64_t element_count = array->getSize().getLimitedValue(); 2117 2118 if (idx < element_count) 2119 { 2120 std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(array->getElementType()); 2121 2122 char element_name[32]; 2123 ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx); 2124 2125 child_name.assign(element_name); 2126 assert(field_type_info.first % 8 == 0); 2127 child_byte_size = field_type_info.first / 8; 2128 child_byte_offset = idx * child_byte_size; 2129 return array->getElementType().getAsOpaquePtr(); 2130 } 2131 } 2132 break; 2133 2134 case clang::Type::Pointer: 2135 { 2136 PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr()); 2137 QualType pointee_type = pointer_type->getPointeeType(); 2138 2139 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 2140 { 2141 return GetChildClangTypeAtIndex (ast_context, 2142 parent_name, 2143 pointer_type->getPointeeType().getAsOpaquePtr(), 2144 idx, 2145 transparent_pointers, 2146 omit_empty_base_classes, 2147 child_name, 2148 child_byte_size, 2149 child_byte_offset, 2150 child_bitfield_bit_size, 2151 child_bitfield_bit_offset); 2152 } 2153 else 2154 { 2155 if (parent_name) 2156 { 2157 child_name.assign(1, '*'); 2158 child_name += parent_name; 2159 } 2160 2161 // We have a pointer to an simple type 2162 if (idx == 0) 2163 { 2164 std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type); 2165 assert(clang_type_info.first % 8 == 0); 2166 child_byte_size = clang_type_info.first / 8; 2167 child_byte_offset = 0; 2168 return pointee_type.getAsOpaquePtr(); 2169 } 2170 } 2171 } 2172 break; 2173 2174 case clang::Type::Typedef: 2175 return GetChildClangTypeAtIndex (ast_context, 2176 parent_name, 2177 cast<TypedefType>(parent_qual_type)->LookThroughTypedefs().getAsOpaquePtr(), 2178 idx, 2179 transparent_pointers, 2180 omit_empty_base_classes, 2181 child_name, 2182 child_byte_size, 2183 child_byte_offset, 2184 child_bitfield_bit_size, 2185 child_bitfield_bit_offset); 2186 break; 2187 2188 default: 2189 break; 2190 } 2191 } 2192 return NULL; 2193} 2194 2195static inline bool 2196BaseSpecifierIsEmpty (const CXXBaseSpecifier *b) 2197{ 2198 return ClangASTContext::RecordHasFields(cast<CXXRecordDecl>(b->getType()->getAs<RecordType>()->getDecl())) == false; 2199} 2200 2201static uint32_t 2202GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes) 2203{ 2204 uint32_t num_bases = 0; 2205 if (cxx_record_decl) 2206 { 2207 if (omit_empty_base_classes) 2208 { 2209 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 2210 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 2211 base_class != base_class_end; 2212 ++base_class) 2213 { 2214 // Skip empty base classes 2215 if (omit_empty_base_classes) 2216 { 2217 if (BaseSpecifierIsEmpty (base_class)) 2218 continue; 2219 } 2220 ++num_bases; 2221 } 2222 } 2223 else 2224 num_bases = cxx_record_decl->getNumBases(); 2225 } 2226 return num_bases; 2227} 2228 2229 2230static uint32_t 2231GetIndexForRecordBase 2232( 2233 const RecordDecl *record_decl, 2234 const CXXBaseSpecifier *base_spec, 2235 bool omit_empty_base_classes 2236) 2237{ 2238 uint32_t child_idx = 0; 2239 2240 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 2241 2242// const char *super_name = record_decl->getNameAsCString(); 2243// const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString(); 2244// printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name); 2245// 2246 if (cxx_record_decl) 2247 { 2248 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 2249 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 2250 base_class != base_class_end; 2251 ++base_class) 2252 { 2253 if (omit_empty_base_classes) 2254 { 2255 if (BaseSpecifierIsEmpty (base_class)) 2256 continue; 2257 } 2258 2259// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name, 2260// child_idx, 2261// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString()); 2262// 2263// 2264 if (base_class == base_spec) 2265 return child_idx; 2266 ++child_idx; 2267 } 2268 } 2269 2270 return UINT32_MAX; 2271} 2272 2273 2274static uint32_t 2275GetIndexForRecordChild 2276( 2277 const RecordDecl *record_decl, 2278 NamedDecl *canonical_decl, 2279 bool omit_empty_base_classes 2280) 2281{ 2282 uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes); 2283 2284// const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 2285// 2286//// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString()); 2287// if (cxx_record_decl) 2288// { 2289// CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 2290// for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 2291// base_class != base_class_end; 2292// ++base_class) 2293// { 2294// if (omit_empty_base_classes) 2295// { 2296// if (BaseSpecifierIsEmpty (base_class)) 2297// continue; 2298// } 2299// 2300//// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", 2301//// record_decl->getNameAsCString(), 2302//// canonical_decl->getNameAsCString(), 2303//// child_idx, 2304//// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString()); 2305// 2306// 2307// CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 2308// if (curr_base_class_decl == canonical_decl) 2309// { 2310// return child_idx; 2311// } 2312// ++child_idx; 2313// } 2314// } 2315// 2316// const uint32_t num_bases = child_idx; 2317 RecordDecl::field_iterator field, field_end; 2318 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 2319 field != field_end; 2320 ++field, ++child_idx) 2321 { 2322// printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n", 2323// record_decl->getNameAsCString(), 2324// canonical_decl->getNameAsCString(), 2325// child_idx - num_bases, 2326// field->getNameAsCString()); 2327 2328 if (field->getCanonicalDecl() == canonical_decl) 2329 return child_idx; 2330 } 2331 2332 return UINT32_MAX; 2333} 2334 2335// Look for a child member (doesn't include base classes, but it does include 2336// their members) in the type hierarchy. Returns an index path into "clang_type" 2337// on how to reach the appropriate member. 2338// 2339// class A 2340// { 2341// public: 2342// int m_a; 2343// int m_b; 2344// }; 2345// 2346// class B 2347// { 2348// }; 2349// 2350// class C : 2351// public B, 2352// public A 2353// { 2354// }; 2355// 2356// If we have a clang type that describes "class C", and we wanted to looked 2357// "m_b" in it: 2358// 2359// With omit_empty_base_classes == false we would get an integer array back with: 2360// { 1, 1 } 2361// The first index 1 is the child index for "class A" within class C 2362// The second index 1 is the child index for "m_b" within class A 2363// 2364// With omit_empty_base_classes == true we would get an integer array back with: 2365// { 0, 1 } 2366// 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) 2367// The second index 1 is the child index for "m_b" within class A 2368 2369size_t 2370ClangASTContext::GetIndexOfChildMemberWithName 2371( 2372 ASTContext *ast_context, 2373 clang_type_t clang_type, 2374 const char *name, 2375 bool omit_empty_base_classes, 2376 std::vector<uint32_t>& child_indexes 2377) 2378{ 2379 if (clang_type && name && name[0]) 2380 { 2381 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2382 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2383 switch (type_class) 2384 { 2385 case clang::Type::Record: 2386 { 2387 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 2388 const RecordDecl *record_decl = record_type->getDecl(); 2389 2390 assert(record_decl); 2391 uint32_t child_idx = 0; 2392 2393 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 2394 2395 // Try and find a field that matches NAME 2396 RecordDecl::field_iterator field, field_end; 2397 StringRef name_sref(name); 2398 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 2399 field != field_end; 2400 ++field, ++child_idx) 2401 { 2402 if (field->getName().equals (name_sref)) 2403 { 2404 // We have to add on the number of base classes to this index! 2405 child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes)); 2406 return child_indexes.size(); 2407 } 2408 } 2409 2410 if (cxx_record_decl) 2411 { 2412 const RecordDecl *parent_record_decl = cxx_record_decl; 2413 2414 //printf ("parent = %s\n", parent_record_decl->getNameAsCString()); 2415 2416 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl(); 2417 // Didn't find things easily, lets let clang do its thang... 2418 IdentifierInfo & ident_ref = ast_context->Idents.get(name, name + strlen (name)); 2419 DeclarationName decl_name(&ident_ref); 2420 2421 CXXBasePaths paths; 2422 if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember, 2423 decl_name.getAsOpaquePtr(), 2424 paths)) 2425 { 2426 CXXBasePaths::const_paths_iterator path, path_end = paths.end(); 2427 for (path = paths.begin(); path != path_end; ++path) 2428 { 2429 const size_t num_path_elements = path->size(); 2430 for (size_t e=0; e<num_path_elements; ++e) 2431 { 2432 CXXBasePathElement elem = (*path)[e]; 2433 2434 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes); 2435 if (child_idx == UINT32_MAX) 2436 { 2437 child_indexes.clear(); 2438 return 0; 2439 } 2440 else 2441 { 2442 child_indexes.push_back (child_idx); 2443 parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl()); 2444 } 2445 } 2446 DeclContext::lookup_iterator named_decl_pos; 2447 for (named_decl_pos = path->Decls.first; 2448 named_decl_pos != path->Decls.second && parent_record_decl; 2449 ++named_decl_pos) 2450 { 2451 //printf ("path[%zu] = %s\n", child_indexes.size(), (*named_decl_pos)->getNameAsCString()); 2452 2453 child_idx = GetIndexForRecordChild (parent_record_decl, *named_decl_pos, omit_empty_base_classes); 2454 if (child_idx == UINT32_MAX) 2455 { 2456 child_indexes.clear(); 2457 return 0; 2458 } 2459 else 2460 { 2461 child_indexes.push_back (child_idx); 2462 } 2463 } 2464 } 2465 return child_indexes.size(); 2466 } 2467 } 2468 2469 } 2470 break; 2471 2472 case clang::Type::ObjCObject: 2473 case clang::Type::ObjCInterface: 2474 { 2475 StringRef name_sref(name); 2476 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 2477 assert (objc_class_type); 2478 if (objc_class_type) 2479 { 2480 uint32_t child_idx = 0; 2481 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2482 2483 if (class_interface_decl) 2484 { 2485 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); 2486 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 2487 2488 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx) 2489 { 2490 const ObjCIvarDecl* ivar_decl = *ivar_pos; 2491 2492 if (ivar_decl->getName().equals (name_sref)) 2493 { 2494 if ((!omit_empty_base_classes && superclass_interface_decl) || 2495 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true))) 2496 ++child_idx; 2497 2498 child_indexes.push_back (child_idx); 2499 return child_indexes.size(); 2500 } 2501 } 2502 2503 if (superclass_interface_decl) 2504 { 2505 // The super class index is always zero for ObjC classes, 2506 // so we push it onto the child indexes in case we find 2507 // an ivar in our superclass... 2508 child_indexes.push_back (0); 2509 2510 if (GetIndexOfChildMemberWithName (ast_context, 2511 ast_context->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), 2512 name, 2513 omit_empty_base_classes, 2514 child_indexes)) 2515 { 2516 // We did find an ivar in a superclass so just 2517 // return the results! 2518 return child_indexes.size(); 2519 } 2520 2521 // We didn't find an ivar matching "name" in our 2522 // superclass, pop the superclass zero index that 2523 // we pushed on above. 2524 child_indexes.pop_back(); 2525 } 2526 } 2527 } 2528 } 2529 break; 2530 2531 case clang::Type::ObjCObjectPointer: 2532 { 2533 return GetIndexOfChildMemberWithName (ast_context, 2534 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(), 2535 name, 2536 omit_empty_base_classes, 2537 child_indexes); 2538 } 2539 break; 2540 2541 2542 case clang::Type::ConstantArray: 2543 { 2544// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); 2545// const uint64_t element_count = array->getSize().getLimitedValue(); 2546// 2547// if (idx < element_count) 2548// { 2549// std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(array->getElementType()); 2550// 2551// char element_name[32]; 2552// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx); 2553// 2554// child_name.assign(element_name); 2555// assert(field_type_info.first % 8 == 0); 2556// child_byte_size = field_type_info.first / 8; 2557// child_byte_offset = idx * child_byte_size; 2558// return array->getElementType().getAsOpaquePtr(); 2559// } 2560 } 2561 break; 2562 2563// case clang::Type::MemberPointerType: 2564// { 2565// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr()); 2566// QualType pointee_type = mem_ptr_type->getPointeeType(); 2567// 2568// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 2569// { 2570// return GetIndexOfChildWithName (ast_context, 2571// mem_ptr_type->getPointeeType().getAsOpaquePtr(), 2572// name); 2573// } 2574// } 2575// break; 2576// 2577 case clang::Type::LValueReference: 2578 case clang::Type::RValueReference: 2579 { 2580 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 2581 QualType pointee_type = reference_type->getPointeeType(); 2582 2583 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 2584 { 2585 return GetIndexOfChildMemberWithName (ast_context, 2586 reference_type->getPointeeType().getAsOpaquePtr(), 2587 name, 2588 omit_empty_base_classes, 2589 child_indexes); 2590 } 2591 } 2592 break; 2593 2594 case clang::Type::Pointer: 2595 { 2596 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 2597 QualType pointee_type = pointer_type->getPointeeType(); 2598 2599 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 2600 { 2601 return GetIndexOfChildMemberWithName (ast_context, 2602 pointer_type->getPointeeType().getAsOpaquePtr(), 2603 name, 2604 omit_empty_base_classes, 2605 child_indexes); 2606 } 2607 else 2608 { 2609// if (parent_name) 2610// { 2611// child_name.assign(1, '*'); 2612// child_name += parent_name; 2613// } 2614// 2615// // We have a pointer to an simple type 2616// if (idx == 0) 2617// { 2618// std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type); 2619// assert(clang_type_info.first % 8 == 0); 2620// child_byte_size = clang_type_info.first / 8; 2621// child_byte_offset = 0; 2622// return pointee_type.getAsOpaquePtr(); 2623// } 2624 } 2625 } 2626 break; 2627 2628 case clang::Type::Typedef: 2629 return GetIndexOfChildMemberWithName (ast_context, 2630 cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), 2631 name, 2632 omit_empty_base_classes, 2633 child_indexes); 2634 2635 default: 2636 break; 2637 } 2638 } 2639 return 0; 2640} 2641 2642 2643// Get the index of the child of "clang_type" whose name matches. This function 2644// doesn't descend into the children, but only looks one level deep and name 2645// matches can include base class names. 2646 2647uint32_t 2648ClangASTContext::GetIndexOfChildWithName 2649( 2650 ASTContext *ast_context, 2651 clang_type_t clang_type, 2652 const char *name, 2653 bool omit_empty_base_classes 2654) 2655{ 2656 if (clang_type && name && name[0]) 2657 { 2658 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2659 2660 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2661 2662 switch (type_class) 2663 { 2664 case clang::Type::Record: 2665 { 2666 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 2667 const RecordDecl *record_decl = record_type->getDecl(); 2668 2669 assert(record_decl); 2670 uint32_t child_idx = 0; 2671 2672 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 2673 2674 if (cxx_record_decl) 2675 { 2676 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 2677 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 2678 base_class != base_class_end; 2679 ++base_class) 2680 { 2681 // Skip empty base classes 2682 CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 2683 if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false) 2684 continue; 2685 2686 if (base_class->getType().getAsString().compare (name) == 0) 2687 return child_idx; 2688 ++child_idx; 2689 } 2690 } 2691 2692 // Try and find a field that matches NAME 2693 RecordDecl::field_iterator field, field_end; 2694 StringRef name_sref(name); 2695 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 2696 field != field_end; 2697 ++field, ++child_idx) 2698 { 2699 if (field->getName().equals (name_sref)) 2700 return child_idx; 2701 } 2702 2703 } 2704 break; 2705 2706 case clang::Type::ObjCObject: 2707 case clang::Type::ObjCInterface: 2708 { 2709 StringRef name_sref(name); 2710 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 2711 assert (objc_class_type); 2712 if (objc_class_type) 2713 { 2714 uint32_t child_idx = 0; 2715 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2716 2717 if (class_interface_decl) 2718 { 2719 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); 2720 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 2721 2722 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos) 2723 { 2724 const ObjCIvarDecl* ivar_decl = *ivar_pos; 2725 2726 if (ivar_decl->getName().equals (name_sref)) 2727 { 2728 if ((!omit_empty_base_classes && superclass_interface_decl) || 2729 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true))) 2730 ++child_idx; 2731 2732 return child_idx; 2733 } 2734 } 2735 2736 if (superclass_interface_decl) 2737 { 2738 if (superclass_interface_decl->getName().equals (name_sref)) 2739 return 0; 2740 } 2741 } 2742 } 2743 } 2744 break; 2745 2746 case clang::Type::ObjCObjectPointer: 2747 { 2748 return GetIndexOfChildWithName (ast_context, 2749 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(), 2750 name, 2751 omit_empty_base_classes); 2752 } 2753 break; 2754 2755 case clang::Type::ConstantArray: 2756 { 2757// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); 2758// const uint64_t element_count = array->getSize().getLimitedValue(); 2759// 2760// if (idx < element_count) 2761// { 2762// std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(array->getElementType()); 2763// 2764// char element_name[32]; 2765// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx); 2766// 2767// child_name.assign(element_name); 2768// assert(field_type_info.first % 8 == 0); 2769// child_byte_size = field_type_info.first / 8; 2770// child_byte_offset = idx * child_byte_size; 2771// return array->getElementType().getAsOpaquePtr(); 2772// } 2773 } 2774 break; 2775 2776// case clang::Type::MemberPointerType: 2777// { 2778// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr()); 2779// QualType pointee_type = mem_ptr_type->getPointeeType(); 2780// 2781// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 2782// { 2783// return GetIndexOfChildWithName (ast_context, 2784// mem_ptr_type->getPointeeType().getAsOpaquePtr(), 2785// name); 2786// } 2787// } 2788// break; 2789// 2790 case clang::Type::LValueReference: 2791 case clang::Type::RValueReference: 2792 { 2793 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 2794 QualType pointee_type = reference_type->getPointeeType(); 2795 2796 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 2797 { 2798 return GetIndexOfChildWithName (ast_context, 2799 reference_type->getPointeeType().getAsOpaquePtr(), 2800 name, 2801 omit_empty_base_classes); 2802 } 2803 } 2804 break; 2805 2806 case clang::Type::Pointer: 2807 { 2808 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 2809 QualType pointee_type = pointer_type->getPointeeType(); 2810 2811 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 2812 { 2813 return GetIndexOfChildWithName (ast_context, 2814 pointer_type->getPointeeType().getAsOpaquePtr(), 2815 name, 2816 omit_empty_base_classes); 2817 } 2818 else 2819 { 2820// if (parent_name) 2821// { 2822// child_name.assign(1, '*'); 2823// child_name += parent_name; 2824// } 2825// 2826// // We have a pointer to an simple type 2827// if (idx == 0) 2828// { 2829// std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type); 2830// assert(clang_type_info.first % 8 == 0); 2831// child_byte_size = clang_type_info.first / 8; 2832// child_byte_offset = 0; 2833// return pointee_type.getAsOpaquePtr(); 2834// } 2835 } 2836 } 2837 break; 2838 2839 case clang::Type::Typedef: 2840 return GetIndexOfChildWithName (ast_context, 2841 cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), 2842 name, 2843 omit_empty_base_classes); 2844 2845 default: 2846 break; 2847 } 2848 } 2849 return UINT32_MAX; 2850} 2851 2852#pragma mark TagType 2853 2854bool 2855ClangASTContext::SetTagTypeKind (clang_type_t tag_clang_type, int kind) 2856{ 2857 if (tag_clang_type) 2858 { 2859 QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type)); 2860 clang::Type *clang_type = tag_qual_type.getTypePtr(); 2861 if (clang_type) 2862 { 2863 TagType *tag_type = dyn_cast<TagType>(clang_type); 2864 if (tag_type) 2865 { 2866 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl()); 2867 if (tag_decl) 2868 { 2869 tag_decl->setTagKind ((TagDecl::TagKind)kind); 2870 return true; 2871 } 2872 } 2873 } 2874 } 2875 return false; 2876} 2877 2878 2879#pragma mark DeclContext Functions 2880 2881DeclContext * 2882ClangASTContext::GetDeclContextForType (clang_type_t clang_type) 2883{ 2884 if (clang_type == NULL) 2885 return NULL; 2886 2887 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2888 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2889 switch (type_class) 2890 { 2891 case clang::Type::FunctionNoProto: break; 2892 case clang::Type::FunctionProto: break; 2893 case clang::Type::IncompleteArray: break; 2894 case clang::Type::VariableArray: break; 2895 case clang::Type::ConstantArray: break; 2896 case clang::Type::ExtVector: break; 2897 case clang::Type::Vector: break; 2898 case clang::Type::Builtin: break; 2899 case clang::Type::BlockPointer: break; 2900 case clang::Type::Pointer: break; 2901 case clang::Type::LValueReference: break; 2902 case clang::Type::RValueReference: break; 2903 case clang::Type::MemberPointer: break; 2904 case clang::Type::Complex: break; 2905 case clang::Type::ObjCObject: break; 2906 case clang::Type::ObjCInterface: return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface(); 2907 case clang::Type::ObjCObjectPointer: return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr()); 2908 case clang::Type::Record: return cast<RecordType>(qual_type)->getDecl(); 2909 case clang::Type::Enum: return cast<EnumType>(qual_type)->getDecl(); 2910 case clang::Type::Typedef: return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr()); 2911 2912 case clang::Type::TypeOfExpr: break; 2913 case clang::Type::TypeOf: break; 2914 case clang::Type::Decltype: break; 2915 //case clang::Type::QualifiedName: break; 2916 case clang::Type::TemplateSpecialization: break; 2917 } 2918 // No DeclContext in this type... 2919 return NULL; 2920} 2921 2922#pragma mark Namespace Declarations 2923 2924NamespaceDecl * 2925ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, const Declaration &decl, DeclContext *decl_ctx) 2926{ 2927 // TODO: Do something intelligent with the Declaration object passed in 2928 // like maybe filling in the SourceLocation with it... 2929 if (name) 2930 { 2931 ASTContext *ast_context = getASTContext(); 2932 if (decl_ctx == NULL) 2933 decl_ctx = ast_context->getTranslationUnitDecl(); 2934 return NamespaceDecl::Create(*ast_context, decl_ctx, SourceLocation(), &ast_context->Idents.get(name)); 2935 } 2936 return NULL; 2937} 2938 2939 2940#pragma mark Function Types 2941 2942FunctionDecl * 2943ClangASTContext::CreateFunctionDeclaration (const char *name, clang_type_t function_clang_type, int storage, bool is_inline) 2944{ 2945 if (name) 2946 { 2947 ASTContext *ast_context = getASTContext(); 2948 assert (ast_context != NULL); 2949 2950 if (name && name[0]) 2951 { 2952 return FunctionDecl::Create(*ast_context, 2953 ast_context->getTranslationUnitDecl(), 2954 SourceLocation(), 2955 DeclarationName (&ast_context->Idents.get(name)), 2956 QualType::getFromOpaquePtr(function_clang_type), 2957 NULL, 2958 (FunctionDecl::StorageClass)storage, 2959 (FunctionDecl::StorageClass)storage, 2960 is_inline); 2961 } 2962 else 2963 { 2964 return FunctionDecl::Create(*ast_context, 2965 ast_context->getTranslationUnitDecl(), 2966 SourceLocation(), 2967 DeclarationName (), 2968 QualType::getFromOpaquePtr(function_clang_type), 2969 NULL, 2970 (FunctionDecl::StorageClass)storage, 2971 (FunctionDecl::StorageClass)storage, 2972 is_inline); 2973 } 2974 } 2975 return NULL; 2976} 2977 2978clang_type_t 2979ClangASTContext::CreateFunctionType (ASTContext *ast_context, 2980 clang_type_t result_type, 2981 clang_type_t *args, 2982 unsigned num_args, 2983 bool is_variadic, 2984 unsigned type_quals) 2985{ 2986 assert (ast_context != NULL); 2987 std::vector<QualType> qual_type_args; 2988 for (unsigned i=0; i<num_args; ++i) 2989 qual_type_args.push_back (QualType::getFromOpaquePtr(args[i])); 2990 2991 // TODO: Detect calling convention in DWARF? 2992 return ast_context->getFunctionType(QualType::getFromOpaquePtr(result_type), 2993 qual_type_args.empty() ? NULL : &qual_type_args.front(), 2994 qual_type_args.size(), 2995 is_variadic, 2996 type_quals, 2997 false, // hasExceptionSpec 2998 false, // hasAnyExceptionSpec, 2999 0, // NumExs 3000 0, // const QualType *ExArray 3001 FunctionType::ExtInfo ()).getAsOpaquePtr(); // NoReturn); 3002} 3003 3004ParmVarDecl * 3005ClangASTContext::CreateParameterDeclaration (const char *name, clang_type_t param_type, int storage) 3006{ 3007 ASTContext *ast_context = getASTContext(); 3008 assert (ast_context != NULL); 3009 return ParmVarDecl::Create(*ast_context, 3010 ast_context->getTranslationUnitDecl(), 3011 SourceLocation(), 3012 name && name[0] ? &ast_context->Idents.get(name) : NULL, 3013 QualType::getFromOpaquePtr(param_type), 3014 NULL, 3015 (VarDecl::StorageClass)storage, 3016 (VarDecl::StorageClass)storage, 3017 0); 3018} 3019 3020void 3021ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params) 3022{ 3023 if (function_decl) 3024 function_decl->setParams (params, num_params); 3025} 3026 3027 3028#pragma mark Array Types 3029 3030clang_type_t 3031ClangASTContext::CreateArrayType (clang_type_t element_type, size_t element_count, uint32_t bit_stride) 3032{ 3033 if (element_type) 3034 { 3035 ASTContext *ast_context = getASTContext(); 3036 assert (ast_context != NULL); 3037 llvm::APInt ap_element_count (64, element_count); 3038 return ast_context->getConstantArrayType(QualType::getFromOpaquePtr(element_type), 3039 ap_element_count, 3040 ArrayType::Normal, 3041 0).getAsOpaquePtr(); // ElemQuals 3042 } 3043 return NULL; 3044} 3045 3046 3047#pragma mark TagDecl 3048 3049bool 3050ClangASTContext::StartTagDeclarationDefinition (clang_type_t clang_type) 3051{ 3052 if (clang_type) 3053 { 3054 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3055 clang::Type *t = qual_type.getTypePtr(); 3056 if (t) 3057 { 3058 TagType *tag_type = dyn_cast<TagType>(t); 3059 if (tag_type) 3060 { 3061 TagDecl *tag_decl = tag_type->getDecl(); 3062 if (tag_decl) 3063 { 3064 tag_decl->startDefinition(); 3065 return true; 3066 } 3067 } 3068 } 3069 } 3070 return false; 3071} 3072 3073bool 3074ClangASTContext::CompleteTagDeclarationDefinition (clang_type_t clang_type) 3075{ 3076 if (clang_type) 3077 { 3078 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3079 3080 CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); 3081 3082 if (cxx_record_decl) 3083 { 3084 cxx_record_decl->completeDefinition(); 3085 3086 return true; 3087 } 3088 3089 const EnumType *enum_type = dyn_cast<EnumType>(qual_type.getTypePtr()); 3090 3091 if (enum_type) 3092 { 3093 EnumDecl *enum_decl = enum_type->getDecl(); 3094 3095 if (enum_decl) 3096 { 3097 /// TODO This really needs to be fixed. 3098 3099 unsigned NumPositiveBits = 1; 3100 unsigned NumNegativeBits = 0; 3101 3102 enum_decl->completeDefinition(enum_decl->getIntegerType(), enum_decl->getIntegerType(), NumPositiveBits, NumNegativeBits); 3103 return true; 3104 } 3105 } 3106 } 3107 return false; 3108} 3109 3110 3111#pragma mark Enumeration Types 3112 3113clang_type_t 3114ClangASTContext::CreateEnumerationType (const Declaration &decl, const char *name, clang_type_t integer_qual_type) 3115{ 3116 // TODO: Do something intelligent with the Declaration object passed in 3117 // like maybe filling in the SourceLocation with it... 3118 ASTContext *ast_context = getASTContext(); 3119 assert (ast_context != NULL); 3120 EnumDecl *enum_decl = EnumDecl::Create(*ast_context, 3121 ast_context->getTranslationUnitDecl(), 3122 SourceLocation(), 3123 name && name[0] ? &ast_context->Idents.get(name) : NULL, 3124 SourceLocation(), 3125 NULL); 3126 if (enum_decl) 3127 { 3128 // TODO: check if we should be setting the promotion type too? 3129 enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type)); 3130 return ast_context->getTagDeclType(enum_decl).getAsOpaquePtr(); 3131 } 3132 return NULL; 3133} 3134 3135clang_type_t 3136ClangASTContext::GetEnumerationIntegerType (clang_type_t enum_clang_type) 3137{ 3138 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type)); 3139 3140 clang::Type *clang_type = enum_qual_type.getTypePtr(); 3141 if (clang_type) 3142 { 3143 const EnumType *enum_type = dyn_cast<EnumType>(clang_type); 3144 if (enum_type) 3145 { 3146 EnumDecl *enum_decl = enum_type->getDecl(); 3147 if (enum_decl) 3148 return enum_decl->getIntegerType().getAsOpaquePtr(); 3149 } 3150 } 3151 return NULL; 3152} 3153bool 3154ClangASTContext::AddEnumerationValueToEnumerationType 3155( 3156 clang_type_t enum_clang_type, 3157 clang_type_t enumerator_clang_type, 3158 const Declaration &decl, 3159 const char *name, 3160 int64_t enum_value, 3161 uint32_t enum_value_bit_size 3162) 3163{ 3164 if (enum_clang_type && enumerator_clang_type && name) 3165 { 3166 // TODO: Do something intelligent with the Declaration object passed in 3167 // like maybe filling in the SourceLocation with it... 3168 ASTContext *ast_context = getASTContext(); 3169 IdentifierTable *identifier_table = getIdentifierTable(); 3170 3171 assert (ast_context != NULL); 3172 assert (identifier_table != NULL); 3173 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type)); 3174 3175 clang::Type *clang_type = enum_qual_type.getTypePtr(); 3176 if (clang_type) 3177 { 3178 const EnumType *enum_type = dyn_cast<EnumType>(clang_type); 3179 3180 if (enum_type) 3181 { 3182 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, false); 3183 enum_llvm_apsint = enum_value; 3184 EnumConstantDecl *enumerator_decl = 3185 EnumConstantDecl::Create(*ast_context, 3186 enum_type->getDecl(), 3187 SourceLocation(), 3188 name ? &identifier_table->get(name) : NULL, // Identifier 3189 QualType::getFromOpaquePtr(enumerator_clang_type), 3190 NULL, 3191 enum_llvm_apsint); 3192 3193 if (enumerator_decl) 3194 { 3195 enum_type->getDecl()->addDecl(enumerator_decl); 3196 return true; 3197 } 3198 } 3199 } 3200 } 3201 return false; 3202} 3203 3204#pragma mark Pointers & References 3205 3206clang_type_t 3207ClangASTContext::CreatePointerType (clang_type_t clang_type) 3208{ 3209 if (clang_type) 3210 { 3211 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3212 3213 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3214 switch (type_class) 3215 { 3216 case clang::Type::ObjCObject: 3217 case clang::Type::ObjCInterface: 3218 return getASTContext()->getObjCObjectPointerType(qual_type).getAsOpaquePtr(); 3219 3220 default: 3221 return getASTContext()->getPointerType(qual_type).getAsOpaquePtr(); 3222 } 3223 } 3224 return NULL; 3225} 3226 3227clang_type_t 3228ClangASTContext::CreateLValueReferenceType (clang_type_t clang_type) 3229{ 3230 if (clang_type) 3231 return getASTContext()->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr(); 3232 return NULL; 3233} 3234 3235clang_type_t 3236ClangASTContext::CreateRValueReferenceType (clang_type_t clang_type) 3237{ 3238 if (clang_type) 3239 return getASTContext()->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr(); 3240 return NULL; 3241} 3242 3243clang_type_t 3244ClangASTContext::CreateMemberPointerType (clang_type_t clang_pointee_type, clang_type_t clang_class_type) 3245{ 3246 if (clang_pointee_type && clang_pointee_type) 3247 return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type), 3248 QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr(); 3249 return NULL; 3250} 3251 3252size_t 3253ClangASTContext::GetPointerBitSize () 3254{ 3255 ASTContext *ast_context = getASTContext(); 3256 return ast_context->getTypeSize(ast_context->VoidPtrTy); 3257} 3258 3259bool 3260ClangASTContext::IsPointerOrReferenceType (clang_type_t clang_type, clang_type_t*target_type) 3261{ 3262 if (clang_type == NULL) 3263 return false; 3264 3265 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3266 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3267 switch (type_class) 3268 { 3269 case clang::Type::ObjCObjectPointer: 3270 if (target_type) 3271 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 3272 return true; 3273 case clang::Type::BlockPointer: 3274 if (target_type) 3275 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 3276 return true; 3277 case clang::Type::Pointer: 3278 if (target_type) 3279 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 3280 return true; 3281 case clang::Type::MemberPointer: 3282 if (target_type) 3283 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 3284 return true; 3285 case clang::Type::LValueReference: 3286 if (target_type) 3287 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr(); 3288 return true; 3289 case clang::Type::RValueReference: 3290 if (target_type) 3291 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr(); 3292 return true; 3293 case clang::Type::Typedef: 3294 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr()); 3295 default: 3296 break; 3297 } 3298 return false; 3299} 3300 3301bool 3302ClangASTContext::IsIntegerType (clang_type_t clang_type, bool &is_signed) 3303{ 3304 if (!clang_type) 3305 return false; 3306 3307 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3308 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()); 3309 3310 if (builtin_type) 3311 { 3312 if (builtin_type->isInteger()) 3313 is_signed = builtin_type->isSignedInteger(); 3314 3315 return true; 3316 } 3317 3318 return false; 3319} 3320 3321bool 3322ClangASTContext::IsPointerType (clang_type_t clang_type, clang_type_t*target_type) 3323{ 3324 if (clang_type) 3325 { 3326 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3327 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3328 switch (type_class) 3329 { 3330 case clang::Type::ObjCObjectPointer: 3331 if (target_type) 3332 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 3333 return true; 3334 case clang::Type::BlockPointer: 3335 if (target_type) 3336 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 3337 return true; 3338 case clang::Type::Pointer: 3339 if (target_type) 3340 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 3341 return true; 3342 case clang::Type::MemberPointer: 3343 if (target_type) 3344 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 3345 return true; 3346 case clang::Type::Typedef: 3347 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), target_type); 3348 default: 3349 break; 3350 } 3351 } 3352 return false; 3353} 3354 3355bool 3356ClangASTContext::IsFloatingPointType (clang_type_t clang_type, uint32_t &count, bool &is_complex) 3357{ 3358 if (clang_type) 3359 { 3360 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3361 3362 if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal())) 3363 { 3364 clang::BuiltinType::Kind kind = BT->getKind(); 3365 if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble) 3366 { 3367 count = 1; 3368 is_complex = false; 3369 return true; 3370 } 3371 } 3372 else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal())) 3373 { 3374 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex)) 3375 { 3376 count = 2; 3377 is_complex = true; 3378 return true; 3379 } 3380 } 3381 else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal())) 3382 { 3383 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex)) 3384 { 3385 count = VT->getNumElements(); 3386 is_complex = false; 3387 return true; 3388 } 3389 } 3390 } 3391 return false; 3392} 3393 3394bool 3395ClangASTContext::IsCXXClassType (clang_type_t clang_type) 3396{ 3397 if (clang_type) 3398 { 3399 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3400 if (qual_type->getAsCXXRecordDecl() != NULL) 3401 return true; 3402 } 3403 return false; 3404} 3405 3406bool 3407ClangASTContext::IsObjCClassType (clang_type_t clang_type) 3408{ 3409 if (clang_type) 3410 { 3411 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3412 if (qual_type->isObjCObjectOrInterfaceType()) 3413 return true; 3414 } 3415 return false; 3416} 3417 3418 3419 3420 3421bool 3422ClangASTContext::IsCStringType (clang_type_t clang_type, uint32_t &length) 3423{ 3424 if (clang_type) 3425 { 3426 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3427 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3428 switch (type_class) 3429 { 3430 case clang::Type::ConstantArray: 3431 { 3432 ConstantArrayType *array = cast<ConstantArrayType>(qual_type.getTypePtr()); 3433 QualType element_qual_type = array->getElementType(); 3434 clang::Type *canonical_type = element_qual_type->getCanonicalTypeInternal().getTypePtr(); 3435 if (canonical_type && canonical_type->isCharType()) 3436 { 3437 // We know the size of the array and it could be a C string 3438 // since it is an array of characters 3439 length = array->getSize().getLimitedValue(); 3440 return true; 3441 } 3442 } 3443 break; 3444 3445 case clang::Type::Pointer: 3446 { 3447 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 3448 clang::Type *pointee_type_ptr = pointer_type->getPointeeType().getTypePtr(); 3449 if (pointee_type_ptr) 3450 { 3451 clang::Type *canonical_type_ptr = pointee_type_ptr->getCanonicalTypeInternal().getTypePtr(); 3452 length = 0; // No length info, read until a NULL terminator is received 3453 if (canonical_type_ptr) 3454 return canonical_type_ptr->isCharType(); 3455 else 3456 return pointee_type_ptr->isCharType(); 3457 } 3458 } 3459 break; 3460 3461 case clang::Type::Typedef: 3462 return ClangASTContext::IsCStringType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), length); 3463 3464 case clang::Type::LValueReference: 3465 case clang::Type::RValueReference: 3466 { 3467 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 3468 clang::Type *pointee_type_ptr = reference_type->getPointeeType().getTypePtr(); 3469 if (pointee_type_ptr) 3470 { 3471 clang::Type *canonical_type_ptr = pointee_type_ptr->getCanonicalTypeInternal().getTypePtr(); 3472 length = 0; // No length info, read until a NULL terminator is received 3473 if (canonical_type_ptr) 3474 return canonical_type_ptr->isCharType(); 3475 else 3476 return pointee_type_ptr->isCharType(); 3477 } 3478 } 3479 break; 3480 } 3481 } 3482 return false; 3483} 3484 3485bool 3486ClangASTContext::IsFunctionPointerType (clang_type_t clang_type) 3487{ 3488 if (clang_type) 3489 { 3490 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3491 3492 if (qual_type->isFunctionPointerType()) 3493 return true; 3494 3495 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3496 switch (type_class) 3497 { 3498 case clang::Type::Typedef: 3499 return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr()); 3500 3501 case clang::Type::LValueReference: 3502 case clang::Type::RValueReference: 3503 { 3504 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 3505 if (reference_type) 3506 return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr()); 3507 } 3508 break; 3509 } 3510 } 3511 return false; 3512} 3513 3514 3515 3516 3517bool 3518ClangASTContext::IsArrayType (clang_type_t clang_type, clang_type_t*member_type, uint64_t *size) 3519{ 3520 if (!clang_type) 3521 return false; 3522 3523 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3524 3525 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3526 switch (type_class) 3527 { 3528 case clang::Type::ConstantArray: 3529 if (member_type) 3530 *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 3531 if (size) 3532 *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULONG_LONG_MAX); 3533 return true; 3534 case clang::Type::IncompleteArray: 3535 if (member_type) 3536 *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 3537 if (size) 3538 *size = 0; 3539 return true; 3540 case clang::Type::VariableArray: 3541 if (member_type) 3542 *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 3543 if (size) 3544 *size = 0; 3545 case clang::Type::DependentSizedArray: 3546 if (member_type) 3547 *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 3548 if (size) 3549 *size = 0; 3550 return true; 3551 } 3552 return false; 3553} 3554 3555 3556#pragma mark Typedefs 3557 3558clang_type_t 3559ClangASTContext::CreateTypedefType (const char *name, clang_type_t clang_type, DeclContext *decl_ctx) 3560{ 3561 if (clang_type) 3562 { 3563 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3564 ASTContext *ast_context = getASTContext(); 3565 IdentifierTable *identifier_table = getIdentifierTable(); 3566 assert (ast_context != NULL); 3567 assert (identifier_table != NULL); 3568 if (decl_ctx == NULL) 3569 decl_ctx = ast_context->getTranslationUnitDecl(); 3570 TypedefDecl *decl = TypedefDecl::Create(*ast_context, 3571 decl_ctx, 3572 SourceLocation(), 3573 name ? &identifier_table->get(name) : NULL, // Identifier 3574 ast_context->CreateTypeSourceInfo(qual_type)); 3575 3576 // Get a uniqued QualType for the typedef decl type 3577 return ast_context->getTypedefType (decl).getAsOpaquePtr(); 3578 } 3579 return NULL; 3580} 3581 3582 3583std::string 3584ClangASTContext::GetTypeName (clang_type_t opaque_qual_type) 3585{ 3586 std::string return_name; 3587 3588 QualType qual_type(QualType::getFromOpaquePtr(opaque_qual_type)); 3589 3590 const TypedefType *typedef_type = qual_type->getAs<TypedefType>(); 3591 if (typedef_type) 3592 { 3593 const TypedefDecl *typedef_decl = typedef_type->getDecl(); 3594 return_name = typedef_decl->getQualifiedNameAsString(); 3595 } 3596 else 3597 { 3598 return_name = qual_type.getAsString(); 3599 } 3600 3601 return return_name; 3602} 3603 3604// Disable this for now since I can't seem to get a nicely formatted float 3605// out of the APFloat class without just getting the float, double or quad 3606// and then using a formatted print on it which defeats the purpose. We ideally 3607// would like to get perfect string values for any kind of float semantics 3608// so we can support remote targets. The code below also requires a patch to 3609// llvm::APInt. 3610//bool 3611//ClangASTContext::ConvertFloatValueToString (ASTContext *ast_context, clang_type_t clang_type, const uint8_t* bytes, size_t byte_size, int apint_byte_order, std::string &float_str) 3612//{ 3613// uint32_t count = 0; 3614// bool is_complex = false; 3615// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex)) 3616// { 3617// unsigned num_bytes_per_float = byte_size / count; 3618// unsigned num_bits_per_float = num_bytes_per_float * 8; 3619// 3620// float_str.clear(); 3621// uint32_t i; 3622// for (i=0; i<count; i++) 3623// { 3624// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order); 3625// bool is_ieee = false; 3626// APFloat ap_float(ap_int, is_ieee); 3627// char s[1024]; 3628// unsigned int hex_digits = 0; 3629// bool upper_case = false; 3630// 3631// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0) 3632// { 3633// if (i > 0) 3634// float_str.append(", "); 3635// float_str.append(s); 3636// if (i == 1 && is_complex) 3637// float_str.append(1, 'i'); 3638// } 3639// } 3640// return !float_str.empty(); 3641// } 3642// return false; 3643//} 3644 3645size_t 3646ClangASTContext::ConvertStringToFloatValue (ASTContext *ast_context, clang_type_t clang_type, const char *s, uint8_t *dst, size_t dst_size) 3647{ 3648 if (clang_type) 3649 { 3650 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3651 uint32_t count = 0; 3652 bool is_complex = false; 3653 if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex)) 3654 { 3655 // TODO: handle complex and vector types 3656 if (count != 1) 3657 return false; 3658 3659 StringRef s_sref(s); 3660 APFloat ap_float(ast_context->getFloatTypeSemantics(qual_type), s_sref); 3661 3662 const uint64_t bit_size = ast_context->getTypeSize (qual_type); 3663 const uint64_t byte_size = bit_size / 8; 3664 if (dst_size >= byte_size) 3665 { 3666 if (bit_size == sizeof(float)*8) 3667 { 3668 float float32 = ap_float.convertToFloat(); 3669 ::memcpy (dst, &float32, byte_size); 3670 return byte_size; 3671 } 3672 else if (bit_size >= 64) 3673 { 3674 llvm::APInt ap_int(ap_float.bitcastToAPInt()); 3675 ::memcpy (dst, ap_int.getRawData(), byte_size); 3676 return byte_size; 3677 } 3678 } 3679 } 3680 } 3681 return 0; 3682} 3683 3684unsigned 3685ClangASTContext::GetTypeQualifiers(clang_type_t clang_type) 3686{ 3687 assert (clang_type); 3688 3689 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3690 3691 return qual_type.getQualifiers().getCVRQualifiers(); 3692} 3693