ClangASTContext.cpp revision 3c9c5eb466869ede185e879d14a47335fb43194d
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 401void * 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 411void * 412ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (clang::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 471void * 472ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size) 473{ 474 ASTContext *ast_context = getASTContext(); 475 476 #define streq(a,b) strcmp(a,b) == 0 477 assert (ast_context != NULL); 478 if (ast_context) 479 { 480 switch (dw_ate) 481 { 482 default: 483 break; 484 485 case DW_ATE_address: 486 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->VoidPtrTy)) 487 return ast_context->VoidPtrTy.getAsOpaquePtr(); 488 break; 489 490 case DW_ATE_boolean: 491 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->BoolTy)) 492 return ast_context->BoolTy.getAsOpaquePtr(); 493 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy)) 494 return ast_context->UnsignedCharTy.getAsOpaquePtr(); 495 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedShortTy)) 496 return ast_context->UnsignedShortTy.getAsOpaquePtr(); 497 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedIntTy)) 498 return ast_context->UnsignedIntTy.getAsOpaquePtr(); 499 break; 500 501 case DW_ATE_complex_float: 502 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->FloatComplexTy)) 503 return ast_context->FloatComplexTy.getAsOpaquePtr(); 504 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->DoubleComplexTy)) 505 return ast_context->DoubleComplexTy.getAsOpaquePtr(); 506 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongDoubleComplexTy)) 507 return ast_context->LongDoubleComplexTy.getAsOpaquePtr(); 508 break; 509 510 case DW_ATE_float: 511 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->FloatTy)) 512 return ast_context->FloatTy.getAsOpaquePtr(); 513 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->DoubleTy)) 514 return ast_context->DoubleTy.getAsOpaquePtr(); 515 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongDoubleTy)) 516 return ast_context->LongDoubleTy.getAsOpaquePtr(); 517 break; 518 519 case DW_ATE_signed: 520 if (type_name) 521 { 522 if (streq(type_name, "int") || 523 streq(type_name, "signed int")) 524 { 525 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->IntTy)) 526 return ast_context->IntTy.getAsOpaquePtr(); 527 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->Int128Ty)) 528 return ast_context->Int128Ty.getAsOpaquePtr(); 529 } 530 531 if (streq(type_name, "long int") || 532 streq(type_name, "long long int") || 533 streq(type_name, "signed long long")) 534 { 535 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongTy)) 536 return ast_context->LongTy.getAsOpaquePtr(); 537 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongLongTy)) 538 return ast_context->LongLongTy.getAsOpaquePtr(); 539 } 540 541 if (streq(type_name, "short") || 542 streq(type_name, "short int") || 543 streq(type_name, "signed short") || 544 streq(type_name, "short signed int")) 545 { 546 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->ShortTy)) 547 return ast_context->ShortTy.getAsOpaquePtr(); 548 } 549 550 if (streq(type_name, "char") || 551 streq(type_name, "signed char")) 552 { 553 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->CharTy)) 554 return ast_context->CharTy.getAsOpaquePtr(); 555 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->SignedCharTy)) 556 return ast_context->SignedCharTy.getAsOpaquePtr(); 557 } 558 559 if (streq(type_name, "wchar_t")) 560 { 561 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->WCharTy)) 562 return ast_context->WCharTy.getAsOpaquePtr(); 563 } 564 565 } 566 // We weren't able to match up a type name, just search by size 567 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->CharTy)) 568 return ast_context->CharTy.getAsOpaquePtr(); 569 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->ShortTy)) 570 return ast_context->ShortTy.getAsOpaquePtr(); 571 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->IntTy)) 572 return ast_context->IntTy.getAsOpaquePtr(); 573 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongTy)) 574 return ast_context->LongTy.getAsOpaquePtr(); 575 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongLongTy)) 576 return ast_context->LongLongTy.getAsOpaquePtr(); 577 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->Int128Ty)) 578 return ast_context->Int128Ty.getAsOpaquePtr(); 579 break; 580 581 case DW_ATE_signed_char: 582 if (type_name) 583 { 584 if (streq(type_name, "signed char")) 585 { 586 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->SignedCharTy)) 587 return ast_context->SignedCharTy.getAsOpaquePtr(); 588 } 589 } 590 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->CharTy)) 591 return ast_context->CharTy.getAsOpaquePtr(); 592 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->SignedCharTy)) 593 return ast_context->SignedCharTy.getAsOpaquePtr(); 594 break; 595 596 case DW_ATE_unsigned: 597 if (type_name) 598 { 599 if (streq(type_name, "unsigned int")) 600 { 601 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedIntTy)) 602 return ast_context->UnsignedIntTy.getAsOpaquePtr(); 603 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedInt128Ty)) 604 return ast_context->UnsignedInt128Ty.getAsOpaquePtr(); 605 } 606 607 if (streq(type_name, "unsigned int") || 608 streq(type_name, "long unsigned int") || 609 streq(type_name, "unsigned long long")) 610 { 611 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongTy)) 612 return ast_context->UnsignedLongTy.getAsOpaquePtr(); 613 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongLongTy)) 614 return ast_context->UnsignedLongLongTy.getAsOpaquePtr(); 615 } 616 617 if (streq(type_name, "unsigned short") || 618 streq(type_name, "short unsigned int")) 619 { 620 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedShortTy)) 621 return ast_context->UnsignedShortTy.getAsOpaquePtr(); 622 } 623 if (streq(type_name, "unsigned char")) 624 { 625 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy)) 626 return ast_context->UnsignedCharTy.getAsOpaquePtr(); 627 } 628 629 } 630 // We weren't able to match up a type name, just search by size 631 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy)) 632 return ast_context->UnsignedCharTy.getAsOpaquePtr(); 633 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedShortTy)) 634 return ast_context->UnsignedShortTy.getAsOpaquePtr(); 635 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedIntTy)) 636 return ast_context->UnsignedIntTy.getAsOpaquePtr(); 637 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongTy)) 638 return ast_context->UnsignedLongTy.getAsOpaquePtr(); 639 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongLongTy)) 640 return ast_context->UnsignedLongLongTy.getAsOpaquePtr(); 641 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedInt128Ty)) 642 return ast_context->UnsignedInt128Ty.getAsOpaquePtr(); 643 break; 644 645 case DW_ATE_unsigned_char: 646 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy)) 647 return ast_context->UnsignedCharTy.getAsOpaquePtr(); 648 break; 649 650 case DW_ATE_imaginary_float: 651 break; 652 } 653 } 654 // This assert should fire for anything that we don't catch above so we know 655 // to fix any issues we run into. 656 assert (!"error: ClangASTContext::GetClangTypeForDWARFEncodingAndSize() contains an unhandled encoding. Fix this ASAP!"); 657 return NULL; 658} 659 660void * 661ClangASTContext::GetBuiltInType_void(clang::ASTContext *ast_context) 662{ 663 return ast_context->VoidTy.getAsOpaquePtr(); 664} 665 666void * 667ClangASTContext::GetBuiltInType_objc_id() 668{ 669 return getASTContext()->getObjCIdType().getAsOpaquePtr(); 670} 671 672void * 673ClangASTContext::GetBuiltInType_objc_Class() 674{ 675 return getASTContext()->getObjCClassType().getAsOpaquePtr(); 676} 677 678void * 679ClangASTContext::GetBuiltInType_objc_selector() 680{ 681 return getASTContext()->getObjCSelType().getAsOpaquePtr(); 682} 683 684void * 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 695void * 696ClangASTContext::GetVoidPtrType (bool is_const) 697{ 698 return GetVoidPtrType(getASTContext(), is_const); 699} 700 701void * 702ClangASTContext::GetVoidPtrType (clang::ASTContext *ast_context, bool is_const) 703{ 704 QualType void_ptr_type(ast_context->VoidPtrTy); 705 706 if (is_const) 707 void_ptr_type.addConst(); 708 709 return void_ptr_type.getAsOpaquePtr(); 710} 711 712void * 713ClangASTContext::CopyType(clang::ASTContext *dest_context, 714 clang::ASTContext *source_context, 715 void *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(clang::ASTContext *ast_context, 728 void *type1, 729 void *type2) 730{ 731 return ast_context->hasSameType(QualType::getFromOpaquePtr(type1), 732 QualType::getFromOpaquePtr(type2)); 733} 734 735#pragma mark CVR modifiers 736 737void * 738ClangASTContext::AddConstModifier (void *clang_type) 739{ 740 if (clang_type) 741 { 742 QualType result(QualType::getFromOpaquePtr(clang_type)); 743 result.addConst(); 744 return result.getAsOpaquePtr(); 745 } 746 return NULL; 747} 748 749void * 750ClangASTContext::AddRestrictModifier (void *clang_type) 751{ 752 if (clang_type) 753 { 754 QualType result(QualType::getFromOpaquePtr(clang_type)); 755 result.getQualifiers().setRestrict (true); 756 return result.getAsOpaquePtr(); 757 } 758 return NULL; 759} 760 761void * 762ClangASTContext::AddVolatileModifier (void *clang_type) 763{ 764 if (clang_type) 765 { 766 QualType result(QualType::getFromOpaquePtr(clang_type)); 767 result.getQualifiers().setVolatile (true); 768 return result.getAsOpaquePtr(); 769 } 770 return NULL; 771} 772 773#pragma mark Structure, Unions, Classes 774 775void * 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 = false; 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 806bool 807ClangASTContext::AddMethodToCXXRecordType 808( 809 clang::ASTContext *ast_context, 810 void *record_opaque_type, 811 const char *name, 812 void *method_opaque_type 813 ) 814{ 815 if (!record_opaque_type || !method_opaque_type || !name) 816 return false; 817 818 assert(ast_context); 819 820 IdentifierTable *identifier_table = &ast_context->Idents; 821 822 assert(identifier_table); 823 824 QualType record_qual_type(QualType::getFromOpaquePtr(record_opaque_type)); 825 clang::Type *record_type(record_qual_type.getTypePtr()); 826 827 if (!record_type) 828 return false; 829 830 RecordType *record_recty(dyn_cast<RecordType>(record_type)); 831 832 if (!record_recty) 833 return false; 834 835 RecordDecl *record_decl = record_recty->getDecl(); 836 837 if (!record_decl) 838 return false; 839 840 CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 841 842 if (!cxx_record_decl) 843 return false; 844 845 QualType method_qual_type(QualType::getFromOpaquePtr(method_opaque_type)); 846 847 CXXMethodDecl *cxx_method_decl = CXXMethodDecl::Create(*ast_context, 848 cxx_record_decl, 849 SourceLocation(), 850 DeclarationName(&identifier_table->get(name)), 851 method_qual_type, 852 NULL); 853 854 // Populate the method decl with parameter decls 855 856 clang::Type *method_type(method_qual_type.getTypePtr()); 857 858 if (!method_type) 859 return false; 860 861 FunctionProtoType *method_funprototy(dyn_cast<FunctionProtoType>(method_type)); 862 863 if (!method_funprototy) 864 return false; 865 866 unsigned int num_params = method_funprototy->getNumArgs(); 867 868 ParmVarDecl *params[num_params]; 869 870 for (int param_index = 0; 871 param_index < num_params; 872 ++param_index) 873 { 874 params[param_index] = ParmVarDecl::Create(*ast_context, 875 cxx_method_decl, 876 SourceLocation(), 877 NULL, // anonymous 878 method_funprototy->getArgType(param_index), 879 NULL, 880 VarDecl::Auto, 881 VarDecl::Auto, 882 NULL); 883 } 884 885 cxx_method_decl->setParams(params, num_params); 886 887 cxx_record_decl->addDecl(cxx_method_decl); 888 889 return true; 890} 891 892bool 893ClangASTContext::AddFieldToRecordType 894( 895 clang::ASTContext *ast_context, 896 void *record_clang_type, 897 const char *name, 898 void *field_type, 899 AccessType access, 900 uint32_t bitfield_bit_size 901) 902{ 903 if (record_clang_type == NULL || field_type == NULL) 904 return false; 905 906 IdentifierTable *identifier_table = &ast_context->Idents; 907 908 assert (ast_context != NULL); 909 assert (identifier_table != NULL); 910 911 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type)); 912 913 clang::Type *clang_type = record_qual_type.getTypePtr(); 914 if (clang_type) 915 { 916 const RecordType *record_type = dyn_cast<RecordType>(clang_type); 917 918 if (record_type) 919 { 920 RecordDecl *record_decl = record_type->getDecl(); 921 922 CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 923 if (cxx_record_decl) 924 cxx_record_decl->setEmpty (false); 925 926 clang::Expr *bit_width = NULL; 927 if (bitfield_bit_size != 0) 928 { 929 APInt bitfield_bit_size_apint(ast_context->getTypeSize(ast_context->IntTy), bitfield_bit_size); 930 bit_width = new (*ast_context)IntegerLiteral (bitfield_bit_size_apint, ast_context->IntTy, SourceLocation()); 931 } 932 FieldDecl *field = FieldDecl::Create (*ast_context, 933 record_decl, 934 SourceLocation(), 935 name ? &identifier_table->get(name) : NULL, // Identifier 936 QualType::getFromOpaquePtr(field_type), // Field type 937 NULL, // DeclaratorInfo * 938 bit_width, // BitWidth 939 false); // Mutable 940 941 field->setAccess (ConvertAccessTypeToAccessSpecifier (access)); 942 943 if (field) 944 { 945 record_decl->addDecl(field); 946 return true; 947 } 948 } 949 else 950 { 951 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(clang_type); 952 if (objc_class_type) 953 { 954 bool isSynthesized = false; 955 ClangASTContext::AddObjCClassIVar (ast_context, 956 record_clang_type, 957 name, 958 field_type, 959 access, 960 bitfield_bit_size, 961 isSynthesized); 962 } 963 } 964 } 965 return false; 966} 967 968bool 969ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size) 970{ 971 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size); 972} 973 974bool 975ClangASTContext::FieldIsBitfield 976( 977 ASTContext *ast_context, 978 FieldDecl* field, 979 uint32_t& bitfield_bit_size 980) 981{ 982 if (ast_context == NULL || field == NULL) 983 return false; 984 985 if (field->isBitField()) 986 { 987 Expr* bit_width_expr = field->getBitWidth(); 988 if (bit_width_expr) 989 { 990 llvm::APSInt bit_width_apsint; 991 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast_context)) 992 { 993 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX); 994 return true; 995 } 996 } 997 } 998 return false; 999} 1000 1001bool 1002ClangASTContext::RecordHasFields (const RecordDecl *record_decl) 1003{ 1004 if (record_decl == NULL) 1005 return false; 1006 1007 if (!record_decl->field_empty()) 1008 return true; 1009 1010 // No fields, lets check this is a CXX record and check the base classes 1011 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1012 if (cxx_record_decl) 1013 { 1014 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1015 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1016 base_class != base_class_end; 1017 ++base_class) 1018 { 1019 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 1020 if (RecordHasFields(base_class_decl)) 1021 return true; 1022 } 1023 } 1024 return false; 1025} 1026 1027void 1028ClangASTContext::SetDefaultAccessForRecordFields (void *clang_qual_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities) 1029{ 1030 if (clang_qual_type) 1031 { 1032 QualType qual_type(QualType::getFromOpaquePtr(clang_qual_type)); 1033 clang::Type *clang_type = qual_type.getTypePtr(); 1034 if (clang_type) 1035 { 1036 RecordType *record_type = dyn_cast<RecordType>(clang_type); 1037 if (record_type) 1038 { 1039 RecordDecl *record_decl = record_type->getDecl(); 1040 if (record_decl) 1041 { 1042 uint32_t field_idx; 1043 RecordDecl::field_iterator field, field_end; 1044 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0; 1045 field != field_end; 1046 ++field, ++field_idx) 1047 { 1048 // If no accessibility was assigned, assign the correct one 1049 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none) 1050 field->setAccess ((AccessSpecifier)default_accessibility); 1051 } 1052 } 1053 } 1054 } 1055 } 1056} 1057 1058#pragma mark C++ Base Classes 1059 1060CXXBaseSpecifier * 1061ClangASTContext::CreateBaseClassSpecifier (void *base_class_type, AccessType access, bool is_virtual, bool base_of_class) 1062{ 1063 if (base_class_type) 1064 return new CXXBaseSpecifier (SourceRange(), 1065 is_virtual, 1066 base_of_class, 1067 ConvertAccessTypeToAccessSpecifier (access), 1068 getASTContext()->CreateTypeSourceInfo (QualType::getFromOpaquePtr(base_class_type))); 1069 return NULL; 1070} 1071 1072void 1073ClangASTContext::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes) 1074{ 1075 for (unsigned i=0; i<num_base_classes; ++i) 1076 { 1077 delete base_classes[i]; 1078 base_classes[i] = NULL; 1079 } 1080} 1081 1082bool 1083ClangASTContext::SetBaseClassesForClassType (void *class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes) 1084{ 1085 if (class_clang_type) 1086 { 1087 clang::Type *clang_type = QualType::getFromOpaquePtr(class_clang_type).getTypePtr(); 1088 if (clang_type) 1089 { 1090 RecordType *record_type = dyn_cast<RecordType>(clang_type); 1091 if (record_type) 1092 { 1093 CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_type->getDecl()); 1094 if (cxx_record_decl) 1095 { 1096 //cxx_record_decl->setEmpty (false); 1097 cxx_record_decl->setBases(base_classes, num_base_classes); 1098 return true; 1099 } 1100 } 1101 } 1102 } 1103 return false; 1104} 1105#pragma mark Objective C Classes 1106 1107void * 1108ClangASTContext::CreateObjCClass 1109( 1110 const char *name, 1111 DeclContext *decl_ctx, 1112 bool isForwardDecl, 1113 bool isInternal 1114) 1115{ 1116 ASTContext *ast_context = getASTContext(); 1117 assert (ast_context != NULL); 1118 assert (name && name[0]); 1119 if (decl_ctx == NULL) 1120 decl_ctx = ast_context->getTranslationUnitDecl(); 1121 1122 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and 1123 // we will need to update this code. I was told to currently always use 1124 // the CXXRecordDecl class since we often don't know from debug information 1125 // if something is struct or a class, so we default to always use the more 1126 // complete definition just in case. 1127 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast_context, 1128 decl_ctx, 1129 SourceLocation(), 1130 &ast_context->Idents.get(name), 1131 SourceLocation(), 1132 isForwardDecl, 1133 isInternal); 1134 1135 return ast_context->getObjCInterfaceType(decl).getAsOpaquePtr(); 1136} 1137 1138bool 1139ClangASTContext::SetObjCSuperClass (void *class_opaque_type, void *super_opaque_type) 1140{ 1141 if (class_opaque_type && super_opaque_type) 1142 { 1143 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 1144 QualType super_qual_type(QualType::getFromOpaquePtr(super_opaque_type)); 1145 clang::Type *class_type = class_qual_type.getTypePtr(); 1146 clang::Type *super_type = super_qual_type.getTypePtr(); 1147 if (class_type && super_type) 1148 { 1149 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 1150 ObjCObjectType *objc_super_type = dyn_cast<ObjCObjectType>(super_type); 1151 if (objc_class_type && objc_super_type) 1152 { 1153 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 1154 ObjCInterfaceDecl *super_interface_decl = objc_super_type->getInterface(); 1155 if (class_interface_decl && super_interface_decl) 1156 { 1157 class_interface_decl->setSuperClass(super_interface_decl); 1158 return true; 1159 } 1160 } 1161 } 1162 } 1163 return false; 1164} 1165 1166 1167bool 1168ClangASTContext::AddObjCClassIVar 1169( 1170 clang::ASTContext *ast_context, 1171 void *class_opaque_type, 1172 const char *name, 1173 void *ivar_opaque_type, 1174 AccessType access, 1175 uint32_t bitfield_bit_size, 1176 bool isSynthesized 1177) 1178{ 1179 if (class_opaque_type == NULL || ivar_opaque_type == NULL) 1180 return false; 1181 1182 IdentifierTable *identifier_table = &ast_context->Idents; 1183 1184 assert (ast_context != NULL); 1185 assert (identifier_table != NULL); 1186 1187 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 1188 1189 clang::Type *class_type = class_qual_type.getTypePtr(); 1190 if (class_type) 1191 { 1192 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 1193 1194 if (objc_class_type) 1195 { 1196 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 1197 1198 if (class_interface_decl) 1199 { 1200 clang::Expr *bit_width = NULL; 1201 if (bitfield_bit_size != 0) 1202 { 1203 APInt bitfield_bit_size_apint(ast_context->getTypeSize(ast_context->IntTy), bitfield_bit_size); 1204 bit_width = new (*ast_context)IntegerLiteral (bitfield_bit_size_apint, ast_context->IntTy, SourceLocation()); 1205 } 1206 1207 ObjCIvarDecl *field = ObjCIvarDecl::Create (*ast_context, 1208 class_interface_decl, 1209 SourceLocation(), 1210 &identifier_table->get(name), // Identifier 1211 QualType::getFromOpaquePtr(ivar_opaque_type), // Field type 1212 NULL, // TypeSourceInfo * 1213 ConvertAccessTypeToObjCIvarAccessControl (access), 1214 bit_width, 1215 isSynthesized); 1216 1217 if (field) 1218 { 1219 class_interface_decl->addDecl(field); 1220 return true; 1221 } 1222 } 1223 } 1224 } 1225 return false; 1226} 1227 1228 1229bool 1230ClangASTContext::ObjCTypeHasIVars (void *class_opaque_type, bool check_superclass) 1231{ 1232 QualType class_qual_type(QualType::getFromOpaquePtr(class_opaque_type)); 1233 1234 clang::Type *class_type = class_qual_type.getTypePtr(); 1235 if (class_type) 1236 { 1237 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(class_type); 1238 1239 if (objc_class_type) 1240 return ObjCDeclHasIVars (objc_class_type->getInterface(), check_superclass); 1241 } 1242 return false; 1243} 1244 1245bool 1246ClangASTContext::ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass) 1247{ 1248 while (class_interface_decl) 1249 { 1250 if (class_interface_decl->ivar_size() > 0) 1251 return true; 1252 1253 if (check_superclass) 1254 class_interface_decl = class_interface_decl->getSuperClass(); 1255 else 1256 break; 1257 } 1258 return false; 1259} 1260 1261 1262#pragma mark Aggregate Types 1263 1264bool 1265ClangASTContext::IsAggregateType (void *clang_type) 1266{ 1267 if (clang_type == NULL) 1268 return false; 1269 1270 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 1271 1272 if (qual_type->isAggregateType ()) 1273 return true; 1274 1275 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 1276 switch (type_class) 1277 { 1278 case clang::Type::IncompleteArray: 1279 case clang::Type::VariableArray: 1280 case clang::Type::ConstantArray: 1281 case clang::Type::ExtVector: 1282 case clang::Type::Vector: 1283 case clang::Type::Record: 1284 case clang::Type::ObjCObject: 1285 case clang::Type::ObjCInterface: 1286 return true; 1287 1288 case clang::Type::Typedef: 1289 return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr()); 1290 1291 default: 1292 break; 1293 } 1294 // The clang type does have a value 1295 return false; 1296} 1297 1298uint32_t 1299ClangASTContext::GetNumChildren (void *clang_qual_type, bool omit_empty_base_classes) 1300{ 1301 if (clang_qual_type == NULL) 1302 return 0; 1303 1304 uint32_t num_children = 0; 1305 QualType qual_type(QualType::getFromOpaquePtr(clang_qual_type)); 1306 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 1307 switch (type_class) 1308 { 1309 case clang::Type::Builtin: 1310 switch (cast<clang::BuiltinType>(qual_type)->getKind()) 1311 { 1312 case clang::BuiltinType::ObjCId: // Child is Class 1313 case clang::BuiltinType::ObjCClass: // child is Class 1314 case clang::BuiltinType::ObjCSel: // child is const char * 1315 num_children = 1; 1316 1317 default: 1318 break; 1319 } 1320 break; 1321 1322 case clang::Type::Record: 1323 { 1324 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 1325 const RecordDecl *record_decl = record_type->getDecl(); 1326 assert(record_decl); 1327 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1328 if (cxx_record_decl) 1329 { 1330 if (omit_empty_base_classes) 1331 { 1332 // Check each base classes to see if it or any of its 1333 // base classes contain any fields. This can help 1334 // limit the noise in variable views by not having to 1335 // show base classes that contain no members. 1336 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1337 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1338 base_class != base_class_end; 1339 ++base_class) 1340 { 1341 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 1342 1343 // Skip empty base classes 1344 if (RecordHasFields(base_class_decl) == false) 1345 continue; 1346 1347 num_children++; 1348 } 1349 } 1350 else 1351 { 1352 // Include all base classes 1353 num_children += cxx_record_decl->getNumBases(); 1354 } 1355 1356 } 1357 RecordDecl::field_iterator field, field_end; 1358 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field) 1359 ++num_children; 1360 } 1361 break; 1362 1363 case clang::Type::ObjCObject: 1364 case clang::Type::ObjCInterface: 1365 { 1366 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 1367 assert (objc_class_type); 1368 if (objc_class_type) 1369 { 1370 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 1371 1372 if (class_interface_decl) 1373 { 1374 1375 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 1376 if (superclass_interface_decl) 1377 { 1378 if (omit_empty_base_classes) 1379 { 1380 if (ClangASTContext::ObjCDeclHasIVars (superclass_interface_decl, true)) 1381 ++num_children; 1382 } 1383 else 1384 ++num_children; 1385 } 1386 1387 num_children += class_interface_decl->ivar_size(); 1388 } 1389 } 1390 } 1391 break; 1392 1393 case clang::Type::ObjCObjectPointer: 1394 { 1395 ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr()); 1396 QualType pointee_type = pointer_type->getPointeeType(); 1397 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (pointee_type.getAsOpaquePtr(), 1398 omit_empty_base_classes); 1399 // If this type points to a simple type, then it has 1 child 1400 if (num_pointee_children == 0) 1401 num_children = 1; 1402 else 1403 num_children = num_pointee_children; 1404 } 1405 break; 1406 1407 case clang::Type::ConstantArray: 1408 num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue(); 1409 break; 1410 1411 case clang::Type::Pointer: 1412 { 1413 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 1414 QualType pointee_type = pointer_type->getPointeeType(); 1415 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (pointee_type.getAsOpaquePtr(), 1416 omit_empty_base_classes); 1417 // If this type points to a simple type, then it has 1 child 1418 if (num_pointee_children == 0) 1419 num_children = 1; 1420 else 1421 num_children = num_pointee_children; 1422 } 1423 break; 1424 1425 case clang::Type::Typedef: 1426 num_children = ClangASTContext::GetNumChildren (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), omit_empty_base_classes); 1427 break; 1428 1429 default: 1430 break; 1431 } 1432 return num_children; 1433} 1434 1435 1436void * 1437ClangASTContext::GetChildClangTypeAtIndex 1438( 1439 const char *parent_name, 1440 void *parent_clang_type, 1441 uint32_t idx, 1442 bool transparent_pointers, 1443 bool omit_empty_base_classes, 1444 std::string& child_name, 1445 uint32_t &child_byte_size, 1446 int32_t &child_byte_offset, 1447 uint32_t &child_bitfield_bit_size, 1448 uint32_t &child_bitfield_bit_offset 1449) 1450{ 1451 if (parent_clang_type) 1452 1453 return GetChildClangTypeAtIndex (getASTContext(), 1454 parent_name, 1455 parent_clang_type, 1456 idx, 1457 transparent_pointers, 1458 omit_empty_base_classes, 1459 child_name, 1460 child_byte_size, 1461 child_byte_offset, 1462 child_bitfield_bit_size, 1463 child_bitfield_bit_offset); 1464 return NULL; 1465} 1466 1467void * 1468ClangASTContext::GetChildClangTypeAtIndex 1469( 1470 ASTContext *ast_context, 1471 const char *parent_name, 1472 void *parent_clang_type, 1473 uint32_t idx, 1474 bool transparent_pointers, 1475 bool omit_empty_base_classes, 1476 std::string& child_name, 1477 uint32_t &child_byte_size, 1478 int32_t &child_byte_offset, 1479 uint32_t &child_bitfield_bit_size, 1480 uint32_t &child_bitfield_bit_offset 1481) 1482{ 1483 if (parent_clang_type == NULL) 1484 return NULL; 1485 1486 if (idx < ClangASTContext::GetNumChildren (parent_clang_type, omit_empty_base_classes)) 1487 { 1488 uint32_t bit_offset; 1489 child_bitfield_bit_size = 0; 1490 child_bitfield_bit_offset = 0; 1491 QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type)); 1492 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass(); 1493 switch (parent_type_class) 1494 { 1495 case clang::Type::Builtin: 1496 switch (cast<clang::BuiltinType>(parent_qual_type)->getKind()) 1497 { 1498 case clang::BuiltinType::ObjCId: 1499 case clang::BuiltinType::ObjCClass: 1500 return ast_context->ObjCBuiltinClassTy.getAsOpaquePtr(); 1501 1502 case clang::BuiltinType::ObjCSel: 1503 { 1504 QualType char_type(ast_context->CharTy); 1505 char_type.addConst(); 1506 return ast_context->getPointerType(char_type).getAsOpaquePtr(); 1507 } 1508 break; 1509 1510 default: 1511 break; 1512 } 1513 break; 1514 1515 1516 case clang::Type::Record: 1517 { 1518 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr()); 1519 const RecordDecl *record_decl = record_type->getDecl(); 1520 assert(record_decl); 1521 const ASTRecordLayout &record_layout = ast_context->getASTRecordLayout(record_decl); 1522 uint32_t child_idx = 0; 1523 1524 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1525 if (cxx_record_decl) 1526 { 1527 // We might have base classes to print out first 1528 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1529 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1530 base_class != base_class_end; 1531 ++base_class) 1532 { 1533 const CXXRecordDecl *base_class_decl = NULL; 1534 1535 // Skip empty base classes 1536 if (omit_empty_base_classes) 1537 { 1538 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 1539 if (RecordHasFields(base_class_decl) == false) 1540 continue; 1541 } 1542 1543 if (idx == child_idx) 1544 { 1545 if (base_class_decl == NULL) 1546 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 1547 1548 1549 if (base_class->isVirtual()) 1550 bit_offset = record_layout.getVBaseClassOffset(base_class_decl); 1551 else 1552 bit_offset = record_layout.getBaseClassOffset(base_class_decl); 1553 1554 // Base classes should be a multiple of 8 bits in size 1555 assert (bit_offset % 8 == 0); 1556 child_byte_offset = bit_offset/8; 1557 std::string base_class_type_name(base_class->getType().getAsString()); 1558 1559 child_name.assign(base_class_type_name.c_str()); 1560 1561 uint64_t clang_type_info_bit_size = ast_context->getTypeSize(base_class->getType()); 1562 1563 // Base classes biut sizes should be a multiple of 8 bits in size 1564 assert (clang_type_info_bit_size % 8 == 0); 1565 child_byte_size = clang_type_info_bit_size / 8; 1566 return base_class->getType().getAsOpaquePtr(); 1567 } 1568 // We don't increment the child index in the for loop since we might 1569 // be skipping empty base classes 1570 ++child_idx; 1571 } 1572 } 1573 // Make sure index is in range... 1574 uint32_t field_idx = 0; 1575 RecordDecl::field_iterator field, field_end; 1576 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx) 1577 { 1578 if (idx == child_idx) 1579 { 1580 // Print the member type if requested 1581 // Print the member name and equal sign 1582 child_name.assign(field->getNameAsString().c_str()); 1583 1584 // Figure out the type byte size (field_type_info.first) and 1585 // alignment (field_type_info.second) from the AST context. 1586 std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(field->getType()); 1587 assert(field_idx < record_layout.getFieldCount()); 1588 1589 child_byte_size = field_type_info.first / 8; 1590 1591 // Figure out the field offset within the current struct/union/class type 1592 bit_offset = record_layout.getFieldOffset (field_idx); 1593 child_byte_offset = bit_offset / 8; 1594 if (ClangASTContext::FieldIsBitfield (ast_context, *field, child_bitfield_bit_size)) 1595 child_bitfield_bit_offset = bit_offset % 8; 1596 1597 return field->getType().getAsOpaquePtr(); 1598 } 1599 } 1600 } 1601 break; 1602 1603 case clang::Type::ObjCObject: 1604 case clang::Type::ObjCInterface: 1605 { 1606 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr()); 1607 assert (objc_class_type); 1608 if (objc_class_type) 1609 { 1610 uint32_t child_idx = 0; 1611 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 1612 1613 if (class_interface_decl) 1614 { 1615 1616 const ASTRecordLayout &interface_layout = ast_context->getASTObjCInterfaceLayout(class_interface_decl); 1617 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 1618 if (superclass_interface_decl) 1619 { 1620 if (omit_empty_base_classes) 1621 { 1622 if (ClangASTContext::GetNumChildren(ast_context->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), omit_empty_base_classes) > 0) 1623 { 1624 if (idx == 0) 1625 { 1626 QualType ivar_qual_type(ast_context->getObjCInterfaceType(superclass_interface_decl)); 1627 1628 1629 child_name.assign(superclass_interface_decl->getNameAsString().c_str()); 1630 1631 std::pair<uint64_t, unsigned> ivar_type_info = ast_context->getTypeInfo(ivar_qual_type.getTypePtr()); 1632 1633 child_byte_size = ivar_type_info.first / 8; 1634 child_byte_offset = 0; 1635 1636 return ivar_qual_type.getAsOpaquePtr(); 1637 } 1638 1639 ++child_idx; 1640 } 1641 } 1642 else 1643 ++child_idx; 1644 } 1645 1646 const uint32_t superclass_idx = child_idx; 1647 1648 if (idx < (child_idx + class_interface_decl->ivar_size())) 1649 { 1650 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); 1651 1652 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos) 1653 { 1654 if (child_idx == idx) 1655 { 1656 const ObjCIvarDecl* ivar_decl = *ivar_pos; 1657 1658 QualType ivar_qual_type(ivar_decl->getType()); 1659 1660 child_name.assign(ivar_decl->getNameAsString().c_str()); 1661 1662 std::pair<uint64_t, unsigned> ivar_type_info = ast_context->getTypeInfo(ivar_qual_type.getTypePtr()); 1663 1664 child_byte_size = ivar_type_info.first / 8; 1665 1666 // Figure out the field offset within the current struct/union/class type 1667 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx); 1668 child_byte_offset = bit_offset / 8; 1669 1670 return ivar_qual_type.getAsOpaquePtr(); 1671 } 1672 ++child_idx; 1673 } 1674 } 1675 } 1676 } 1677 } 1678 break; 1679 1680 case clang::Type::ObjCObjectPointer: 1681 { 1682 ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(parent_qual_type.getTypePtr()); 1683 QualType pointee_type = pointer_type->getPointeeType(); 1684 1685 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 1686 { 1687 return GetChildClangTypeAtIndex (ast_context, 1688 parent_name, 1689 pointer_type->getPointeeType().getAsOpaquePtr(), 1690 idx, 1691 transparent_pointers, 1692 omit_empty_base_classes, 1693 child_name, 1694 child_byte_size, 1695 child_byte_offset, 1696 child_bitfield_bit_size, 1697 child_bitfield_bit_offset); 1698 } 1699 else 1700 { 1701 if (parent_name) 1702 { 1703 child_name.assign(1, '*'); 1704 child_name += parent_name; 1705 } 1706 1707 // We have a pointer to an simple type 1708 if (idx == 0) 1709 { 1710 std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type); 1711 assert(clang_type_info.first % 8 == 0); 1712 child_byte_size = clang_type_info.first / 8; 1713 child_byte_offset = 0; 1714 return pointee_type.getAsOpaquePtr(); 1715 } 1716 } 1717 } 1718 break; 1719 1720 case clang::Type::ConstantArray: 1721 { 1722 const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); 1723 const uint64_t element_count = array->getSize().getLimitedValue(); 1724 1725 if (idx < element_count) 1726 { 1727 std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(array->getElementType()); 1728 1729 char element_name[32]; 1730 ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx); 1731 1732 child_name.assign(element_name); 1733 assert(field_type_info.first % 8 == 0); 1734 child_byte_size = field_type_info.first / 8; 1735 child_byte_offset = idx * child_byte_size; 1736 return array->getElementType().getAsOpaquePtr(); 1737 } 1738 } 1739 break; 1740 1741 case clang::Type::Pointer: 1742 { 1743 PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr()); 1744 QualType pointee_type = pointer_type->getPointeeType(); 1745 1746 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 1747 { 1748 return GetChildClangTypeAtIndex (ast_context, 1749 parent_name, 1750 pointer_type->getPointeeType().getAsOpaquePtr(), 1751 idx, 1752 transparent_pointers, 1753 omit_empty_base_classes, 1754 child_name, 1755 child_byte_size, 1756 child_byte_offset, 1757 child_bitfield_bit_size, 1758 child_bitfield_bit_offset); 1759 } 1760 else 1761 { 1762 if (parent_name) 1763 { 1764 child_name.assign(1, '*'); 1765 child_name += parent_name; 1766 } 1767 1768 // We have a pointer to an simple type 1769 if (idx == 0) 1770 { 1771 std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type); 1772 assert(clang_type_info.first % 8 == 0); 1773 child_byte_size = clang_type_info.first / 8; 1774 child_byte_offset = 0; 1775 return pointee_type.getAsOpaquePtr(); 1776 } 1777 } 1778 } 1779 break; 1780 1781 case clang::Type::Typedef: 1782 return GetChildClangTypeAtIndex (ast_context, 1783 parent_name, 1784 cast<TypedefType>(parent_qual_type)->LookThroughTypedefs().getAsOpaquePtr(), 1785 idx, 1786 transparent_pointers, 1787 omit_empty_base_classes, 1788 child_name, 1789 child_byte_size, 1790 child_byte_offset, 1791 child_bitfield_bit_size, 1792 child_bitfield_bit_offset); 1793 break; 1794 1795 default: 1796 break; 1797 } 1798 } 1799 return NULL; 1800} 1801 1802static inline bool 1803BaseSpecifierIsEmpty (const CXXBaseSpecifier *b) 1804{ 1805 return ClangASTContext::RecordHasFields(cast<CXXRecordDecl>(b->getType()->getAs<RecordType>()->getDecl())) == false; 1806} 1807 1808static uint32_t 1809GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes) 1810{ 1811 uint32_t num_bases = 0; 1812 if (cxx_record_decl) 1813 { 1814 if (omit_empty_base_classes) 1815 { 1816 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1817 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1818 base_class != base_class_end; 1819 ++base_class) 1820 { 1821 // Skip empty base classes 1822 if (omit_empty_base_classes) 1823 { 1824 if (BaseSpecifierIsEmpty (base_class)) 1825 continue; 1826 } 1827 ++num_bases; 1828 } 1829 } 1830 else 1831 num_bases = cxx_record_decl->getNumBases(); 1832 } 1833 return num_bases; 1834} 1835 1836 1837static uint32_t 1838GetIndexForRecordBase 1839( 1840 const RecordDecl *record_decl, 1841 const CXXBaseSpecifier *base_spec, 1842 bool omit_empty_base_classes 1843) 1844{ 1845 uint32_t child_idx = 0; 1846 1847 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1848 1849// const char *super_name = record_decl->getNameAsCString(); 1850// const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString(); 1851// printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name); 1852// 1853 if (cxx_record_decl) 1854 { 1855 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1856 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1857 base_class != base_class_end; 1858 ++base_class) 1859 { 1860 if (omit_empty_base_classes) 1861 { 1862 if (BaseSpecifierIsEmpty (base_class)) 1863 continue; 1864 } 1865 1866// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name, 1867// child_idx, 1868// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString()); 1869// 1870// 1871 if (base_class == base_spec) 1872 return child_idx; 1873 ++child_idx; 1874 } 1875 } 1876 1877 return UINT32_MAX; 1878} 1879 1880 1881static uint32_t 1882GetIndexForRecordChild 1883( 1884 const RecordDecl *record_decl, 1885 NamedDecl *canonical_decl, 1886 bool omit_empty_base_classes 1887) 1888{ 1889 uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes); 1890 1891// const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1892// 1893//// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString()); 1894// if (cxx_record_decl) 1895// { 1896// CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1897// for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1898// base_class != base_class_end; 1899// ++base_class) 1900// { 1901// if (omit_empty_base_classes) 1902// { 1903// if (BaseSpecifierIsEmpty (base_class)) 1904// continue; 1905// } 1906// 1907//// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", 1908//// record_decl->getNameAsCString(), 1909//// canonical_decl->getNameAsCString(), 1910//// child_idx, 1911//// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString()); 1912// 1913// 1914// CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 1915// if (curr_base_class_decl == canonical_decl) 1916// { 1917// return child_idx; 1918// } 1919// ++child_idx; 1920// } 1921// } 1922// 1923// const uint32_t num_bases = child_idx; 1924 RecordDecl::field_iterator field, field_end; 1925 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 1926 field != field_end; 1927 ++field, ++child_idx) 1928 { 1929// printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n", 1930// record_decl->getNameAsCString(), 1931// canonical_decl->getNameAsCString(), 1932// child_idx - num_bases, 1933// field->getNameAsCString()); 1934 1935 if (field->getCanonicalDecl() == canonical_decl) 1936 return child_idx; 1937 } 1938 1939 return UINT32_MAX; 1940} 1941 1942// Look for a child member (doesn't include base classes, but it does include 1943// their members) in the type hierarchy. Returns an index path into "clang_type" 1944// on how to reach the appropriate member. 1945// 1946// class A 1947// { 1948// public: 1949// int m_a; 1950// int m_b; 1951// }; 1952// 1953// class B 1954// { 1955// }; 1956// 1957// class C : 1958// public B, 1959// public A 1960// { 1961// }; 1962// 1963// If we have a clang type that describes "class C", and we wanted to looked 1964// "m_b" in it: 1965// 1966// With omit_empty_base_classes == false we would get an integer array back with: 1967// { 1, 1 } 1968// The first index 1 is the child index for "class A" within class C 1969// The second index 1 is the child index for "m_b" within class A 1970// 1971// With omit_empty_base_classes == true we would get an integer array back with: 1972// { 0, 1 } 1973// 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) 1974// The second index 1 is the child index for "m_b" within class A 1975 1976size_t 1977ClangASTContext::GetIndexOfChildMemberWithName 1978( 1979 ASTContext *ast_context, 1980 void *clang_type, 1981 const char *name, 1982 bool omit_empty_base_classes, 1983 std::vector<uint32_t>& child_indexes 1984) 1985{ 1986 if (clang_type && name && name[0]) 1987 { 1988 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 1989 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 1990 switch (type_class) 1991 { 1992 case clang::Type::Record: 1993 { 1994 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 1995 const RecordDecl *record_decl = record_type->getDecl(); 1996 1997 assert(record_decl); 1998 uint32_t child_idx = 0; 1999 2000 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 2001 2002 // Try and find a field that matches NAME 2003 RecordDecl::field_iterator field, field_end; 2004 StringRef name_sref(name); 2005 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 2006 field != field_end; 2007 ++field, ++child_idx) 2008 { 2009 if (field->getName().equals (name_sref)) 2010 { 2011 // We have to add on the number of base classes to this index! 2012 child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes)); 2013 return child_indexes.size(); 2014 } 2015 } 2016 2017 if (cxx_record_decl) 2018 { 2019 const RecordDecl *parent_record_decl = cxx_record_decl; 2020 2021 //printf ("parent = %s\n", parent_record_decl->getNameAsCString()); 2022 2023 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl(); 2024 // Didn't find things easily, lets let clang do its thang... 2025 IdentifierInfo & ident_ref = ast_context->Idents.get(name, name + strlen (name)); 2026 DeclarationName decl_name(&ident_ref); 2027 2028 CXXBasePaths paths; 2029 if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember, 2030 decl_name.getAsOpaquePtr(), 2031 paths)) 2032 { 2033 CXXBasePaths::const_paths_iterator path, path_end = paths.end(); 2034 for (path = paths.begin(); path != path_end; ++path) 2035 { 2036 const size_t num_path_elements = path->size(); 2037 for (size_t e=0; e<num_path_elements; ++e) 2038 { 2039 CXXBasePathElement elem = (*path)[e]; 2040 2041 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes); 2042 if (child_idx == UINT32_MAX) 2043 { 2044 child_indexes.clear(); 2045 return 0; 2046 } 2047 else 2048 { 2049 child_indexes.push_back (child_idx); 2050 parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl()); 2051 } 2052 } 2053 DeclContext::lookup_iterator named_decl_pos; 2054 for (named_decl_pos = path->Decls.first; 2055 named_decl_pos != path->Decls.second && parent_record_decl; 2056 ++named_decl_pos) 2057 { 2058 //printf ("path[%zu] = %s\n", child_indexes.size(), (*named_decl_pos)->getNameAsCString()); 2059 2060 child_idx = GetIndexForRecordChild (parent_record_decl, *named_decl_pos, omit_empty_base_classes); 2061 if (child_idx == UINT32_MAX) 2062 { 2063 child_indexes.clear(); 2064 return 0; 2065 } 2066 else 2067 { 2068 child_indexes.push_back (child_idx); 2069 } 2070 } 2071 } 2072 return child_indexes.size(); 2073 } 2074 } 2075 2076 } 2077 break; 2078 2079 case clang::Type::ObjCObject: 2080 case clang::Type::ObjCInterface: 2081 { 2082 StringRef name_sref(name); 2083 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 2084 assert (objc_class_type); 2085 if (objc_class_type) 2086 { 2087 uint32_t child_idx = 0; 2088 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2089 2090 if (class_interface_decl) 2091 { 2092 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); 2093 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 2094 2095 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx) 2096 { 2097 const ObjCIvarDecl* ivar_decl = *ivar_pos; 2098 2099 if (ivar_decl->getName().equals (name_sref)) 2100 { 2101 if ((!omit_empty_base_classes && superclass_interface_decl) || 2102 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true))) 2103 ++child_idx; 2104 2105 child_indexes.push_back (child_idx); 2106 return child_indexes.size(); 2107 } 2108 } 2109 2110 if (superclass_interface_decl) 2111 { 2112 // The super class index is always zero for ObjC classes, 2113 // so we push it onto the child indexes in case we find 2114 // an ivar in our superclass... 2115 child_indexes.push_back (0); 2116 2117 if (GetIndexOfChildMemberWithName (ast_context, 2118 ast_context->getObjCInterfaceType(superclass_interface_decl).getAsOpaquePtr(), 2119 name, 2120 omit_empty_base_classes, 2121 child_indexes)) 2122 { 2123 // We did find an ivar in a superclass so just 2124 // return the results! 2125 return child_indexes.size(); 2126 } 2127 2128 // We didn't find an ivar matching "name" in our 2129 // superclass, pop the superclass zero index that 2130 // we pushed on above. 2131 child_indexes.pop_back(); 2132 } 2133 } 2134 } 2135 } 2136 break; 2137 2138 case clang::Type::ObjCObjectPointer: 2139 { 2140 return GetIndexOfChildMemberWithName (ast_context, 2141 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(), 2142 name, 2143 omit_empty_base_classes, 2144 child_indexes); 2145 } 2146 break; 2147 2148 2149 case clang::Type::ConstantArray: 2150 { 2151// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); 2152// const uint64_t element_count = array->getSize().getLimitedValue(); 2153// 2154// if (idx < element_count) 2155// { 2156// std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(array->getElementType()); 2157// 2158// char element_name[32]; 2159// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx); 2160// 2161// child_name.assign(element_name); 2162// assert(field_type_info.first % 8 == 0); 2163// child_byte_size = field_type_info.first / 8; 2164// child_byte_offset = idx * child_byte_size; 2165// return array->getElementType().getAsOpaquePtr(); 2166// } 2167 } 2168 break; 2169 2170// case clang::Type::MemberPointerType: 2171// { 2172// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr()); 2173// QualType pointee_type = mem_ptr_type->getPointeeType(); 2174// 2175// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 2176// { 2177// return GetIndexOfChildWithName (ast_context, 2178// mem_ptr_type->getPointeeType().getAsOpaquePtr(), 2179// name); 2180// } 2181// } 2182// break; 2183// 2184 case clang::Type::LValueReference: 2185 case clang::Type::RValueReference: 2186 { 2187 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 2188 QualType pointee_type = reference_type->getPointeeType(); 2189 2190 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 2191 { 2192 return GetIndexOfChildMemberWithName (ast_context, 2193 reference_type->getPointeeType().getAsOpaquePtr(), 2194 name, 2195 omit_empty_base_classes, 2196 child_indexes); 2197 } 2198 } 2199 break; 2200 2201 case clang::Type::Pointer: 2202 { 2203 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 2204 QualType pointee_type = pointer_type->getPointeeType(); 2205 2206 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 2207 { 2208 return GetIndexOfChildMemberWithName (ast_context, 2209 pointer_type->getPointeeType().getAsOpaquePtr(), 2210 name, 2211 omit_empty_base_classes, 2212 child_indexes); 2213 } 2214 else 2215 { 2216// if (parent_name) 2217// { 2218// child_name.assign(1, '*'); 2219// child_name += parent_name; 2220// } 2221// 2222// // We have a pointer to an simple type 2223// if (idx == 0) 2224// { 2225// std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type); 2226// assert(clang_type_info.first % 8 == 0); 2227// child_byte_size = clang_type_info.first / 8; 2228// child_byte_offset = 0; 2229// return pointee_type.getAsOpaquePtr(); 2230// } 2231 } 2232 } 2233 break; 2234 2235 case clang::Type::Typedef: 2236 return GetIndexOfChildMemberWithName (ast_context, 2237 cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), 2238 name, 2239 omit_empty_base_classes, 2240 child_indexes); 2241 2242 default: 2243 break; 2244 } 2245 } 2246 return 0; 2247} 2248 2249 2250// Get the index of the child of "clang_type" whose name matches. This function 2251// doesn't descend into the children, but only looks one level deep and name 2252// matches can include base class names. 2253 2254uint32_t 2255ClangASTContext::GetIndexOfChildWithName 2256( 2257 ASTContext *ast_context, 2258 void *clang_type, 2259 const char *name, 2260 bool omit_empty_base_classes 2261) 2262{ 2263 if (clang_type && name && name[0]) 2264 { 2265 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2266 2267 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2268 2269 switch (type_class) 2270 { 2271 case clang::Type::Record: 2272 { 2273 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 2274 const RecordDecl *record_decl = record_type->getDecl(); 2275 2276 assert(record_decl); 2277 uint32_t child_idx = 0; 2278 2279 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 2280 2281 if (cxx_record_decl) 2282 { 2283 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 2284 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 2285 base_class != base_class_end; 2286 ++base_class) 2287 { 2288 // Skip empty base classes 2289 CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 2290 if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false) 2291 continue; 2292 2293 if (base_class->getType().getAsString().compare (name) == 0) 2294 return child_idx; 2295 ++child_idx; 2296 } 2297 } 2298 2299 // Try and find a field that matches NAME 2300 RecordDecl::field_iterator field, field_end; 2301 StringRef name_sref(name); 2302 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 2303 field != field_end; 2304 ++field, ++child_idx) 2305 { 2306 if (field->getName().equals (name_sref)) 2307 return child_idx; 2308 } 2309 2310 } 2311 break; 2312 2313 case clang::Type::ObjCObject: 2314 case clang::Type::ObjCInterface: 2315 { 2316 StringRef name_sref(name); 2317 ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr()); 2318 assert (objc_class_type); 2319 if (objc_class_type) 2320 { 2321 uint32_t child_idx = 0; 2322 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface(); 2323 2324 if (class_interface_decl) 2325 { 2326 ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end(); 2327 ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass(); 2328 2329 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos) 2330 { 2331 const ObjCIvarDecl* ivar_decl = *ivar_pos; 2332 2333 if (ivar_decl->getName().equals (name_sref)) 2334 { 2335 if ((!omit_empty_base_classes && superclass_interface_decl) || 2336 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true))) 2337 ++child_idx; 2338 2339 return child_idx; 2340 } 2341 } 2342 2343 if (superclass_interface_decl) 2344 { 2345 if (superclass_interface_decl->getName().equals (name_sref)) 2346 return 0; 2347 } 2348 } 2349 } 2350 } 2351 break; 2352 2353 case clang::Type::ObjCObjectPointer: 2354 { 2355 return GetIndexOfChildWithName (ast_context, 2356 cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr(), 2357 name, 2358 omit_empty_base_classes); 2359 } 2360 break; 2361 2362 case clang::Type::ConstantArray: 2363 { 2364// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); 2365// const uint64_t element_count = array->getSize().getLimitedValue(); 2366// 2367// if (idx < element_count) 2368// { 2369// std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(array->getElementType()); 2370// 2371// char element_name[32]; 2372// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx); 2373// 2374// child_name.assign(element_name); 2375// assert(field_type_info.first % 8 == 0); 2376// child_byte_size = field_type_info.first / 8; 2377// child_byte_offset = idx * child_byte_size; 2378// return array->getElementType().getAsOpaquePtr(); 2379// } 2380 } 2381 break; 2382 2383// case clang::Type::MemberPointerType: 2384// { 2385// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr()); 2386// QualType pointee_type = mem_ptr_type->getPointeeType(); 2387// 2388// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 2389// { 2390// return GetIndexOfChildWithName (ast_context, 2391// mem_ptr_type->getPointeeType().getAsOpaquePtr(), 2392// name); 2393// } 2394// } 2395// break; 2396// 2397 case clang::Type::LValueReference: 2398 case clang::Type::RValueReference: 2399 { 2400 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 2401 QualType pointee_type = reference_type->getPointeeType(); 2402 2403 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 2404 { 2405 return GetIndexOfChildWithName (ast_context, 2406 reference_type->getPointeeType().getAsOpaquePtr(), 2407 name, 2408 omit_empty_base_classes); 2409 } 2410 } 2411 break; 2412 2413 case clang::Type::Pointer: 2414 { 2415 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 2416 QualType pointee_type = pointer_type->getPointeeType(); 2417 2418 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 2419 { 2420 return GetIndexOfChildWithName (ast_context, 2421 pointer_type->getPointeeType().getAsOpaquePtr(), 2422 name, 2423 omit_empty_base_classes); 2424 } 2425 else 2426 { 2427// if (parent_name) 2428// { 2429// child_name.assign(1, '*'); 2430// child_name += parent_name; 2431// } 2432// 2433// // We have a pointer to an simple type 2434// if (idx == 0) 2435// { 2436// std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type); 2437// assert(clang_type_info.first % 8 == 0); 2438// child_byte_size = clang_type_info.first / 8; 2439// child_byte_offset = 0; 2440// return pointee_type.getAsOpaquePtr(); 2441// } 2442 } 2443 } 2444 break; 2445 2446 case clang::Type::Typedef: 2447 return GetIndexOfChildWithName (ast_context, 2448 cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), 2449 name, 2450 omit_empty_base_classes); 2451 2452 default: 2453 break; 2454 } 2455 } 2456 return UINT32_MAX; 2457} 2458 2459#pragma mark TagType 2460 2461bool 2462ClangASTContext::SetTagTypeKind (void *tag_clang_type, int kind) 2463{ 2464 if (tag_clang_type) 2465 { 2466 QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type)); 2467 clang::Type *clang_type = tag_qual_type.getTypePtr(); 2468 if (clang_type) 2469 { 2470 TagType *tag_type = dyn_cast<TagType>(clang_type); 2471 if (tag_type) 2472 { 2473 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl()); 2474 if (tag_decl) 2475 { 2476 tag_decl->setTagKind ((TagDecl::TagKind)kind); 2477 return true; 2478 } 2479 } 2480 } 2481 } 2482 return false; 2483} 2484 2485 2486#pragma mark DeclContext Functions 2487 2488DeclContext * 2489ClangASTContext::GetDeclContextForType (void *clang_type) 2490{ 2491 if (clang_type == NULL) 2492 return NULL; 2493 2494 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 2495 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2496 switch (type_class) 2497 { 2498 case clang::Type::FunctionNoProto: break; 2499 case clang::Type::FunctionProto: break; 2500 case clang::Type::IncompleteArray: break; 2501 case clang::Type::VariableArray: break; 2502 case clang::Type::ConstantArray: break; 2503 case clang::Type::ExtVector: break; 2504 case clang::Type::Vector: break; 2505 case clang::Type::Builtin: break; 2506 case clang::Type::BlockPointer: break; 2507 case clang::Type::Pointer: break; 2508 case clang::Type::LValueReference: break; 2509 case clang::Type::RValueReference: break; 2510 case clang::Type::MemberPointer: break; 2511 case clang::Type::Complex: break; 2512 case clang::Type::ObjCObject: break; 2513 case clang::Type::ObjCInterface: return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface(); 2514 case clang::Type::ObjCObjectPointer: return ClangASTContext::GetDeclContextForType (cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType().getAsOpaquePtr()); 2515 case clang::Type::Record: return cast<RecordType>(qual_type)->getDecl(); 2516 case clang::Type::Enum: return cast<EnumType>(qual_type)->getDecl(); 2517 case clang::Type::Typedef: return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr()); 2518 2519 case clang::Type::TypeOfExpr: break; 2520 case clang::Type::TypeOf: break; 2521 case clang::Type::Decltype: break; 2522 //case clang::Type::QualifiedName: break; 2523 case clang::Type::TemplateSpecialization: break; 2524 } 2525 // No DeclContext in this type... 2526 return NULL; 2527} 2528 2529#pragma mark Namespace Declarations 2530 2531NamespaceDecl * 2532ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, const Declaration &decl, DeclContext *decl_ctx) 2533{ 2534 // TODO: Do something intelligent with the Declaration object passed in 2535 // like maybe filling in the SourceLocation with it... 2536 if (name) 2537 { 2538 ASTContext *ast_context = getASTContext(); 2539 if (decl_ctx == NULL) 2540 decl_ctx = ast_context->getTranslationUnitDecl(); 2541 return NamespaceDecl::Create(*ast_context, decl_ctx, SourceLocation(), &ast_context->Idents.get(name)); 2542 } 2543 return NULL; 2544} 2545 2546 2547#pragma mark Function Types 2548 2549FunctionDecl * 2550ClangASTContext::CreateFunctionDeclaration (const char *name, void *function_clang_type, int storage, bool is_inline) 2551{ 2552 if (name) 2553 { 2554 ASTContext *ast_context = getASTContext(); 2555 assert (ast_context != NULL); 2556 2557 if (name && name[0]) 2558 { 2559 return FunctionDecl::Create(*ast_context, 2560 ast_context->getTranslationUnitDecl(), 2561 SourceLocation(), 2562 DeclarationName (&ast_context->Idents.get(name)), 2563 QualType::getFromOpaquePtr(function_clang_type), 2564 NULL, 2565 (FunctionDecl::StorageClass)storage, 2566 (FunctionDecl::StorageClass)storage, 2567 is_inline); 2568 } 2569 else 2570 { 2571 return FunctionDecl::Create(*ast_context, 2572 ast_context->getTranslationUnitDecl(), 2573 SourceLocation(), 2574 DeclarationName (), 2575 QualType::getFromOpaquePtr(function_clang_type), 2576 NULL, 2577 (FunctionDecl::StorageClass)storage, 2578 (FunctionDecl::StorageClass)storage, 2579 is_inline); 2580 } 2581 } 2582 return NULL; 2583} 2584 2585void * 2586ClangASTContext::CreateFunctionType (clang::ASTContext *ast_context, 2587 void *result_type, 2588 void **args, 2589 unsigned num_args, 2590 bool is_variadic, 2591 unsigned type_quals) 2592{ 2593 assert (ast_context != NULL); 2594 std::vector<QualType> qual_type_args; 2595 for (unsigned i=0; i<num_args; ++i) 2596 qual_type_args.push_back (QualType::getFromOpaquePtr(args[i])); 2597 2598 // TODO: Detect calling convention in DWARF? 2599 return ast_context->getFunctionType(QualType::getFromOpaquePtr(result_type), 2600 qual_type_args.empty() ? NULL : &qual_type_args.front(), 2601 qual_type_args.size(), 2602 is_variadic, 2603 type_quals, 2604 false, // hasExceptionSpec 2605 false, // hasAnyExceptionSpec, 2606 0, // NumExs 2607 0, // const QualType *ExArray 2608 FunctionType::ExtInfo ()).getAsOpaquePtr(); // NoReturn); 2609} 2610 2611ParmVarDecl * 2612ClangASTContext::CreateParameterDeclaration (const char *name, void *param_type, int storage) 2613{ 2614 ASTContext *ast_context = getASTContext(); 2615 assert (ast_context != NULL); 2616 return ParmVarDecl::Create(*ast_context, 2617 ast_context->getTranslationUnitDecl(), 2618 SourceLocation(), 2619 name && name[0] ? &ast_context->Idents.get(name) : NULL, 2620 QualType::getFromOpaquePtr(param_type), 2621 NULL, 2622 (VarDecl::StorageClass)storage, 2623 (VarDecl::StorageClass)storage, 2624 0); 2625} 2626 2627void 2628ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params) 2629{ 2630 if (function_decl) 2631 function_decl->setParams (params, num_params); 2632} 2633 2634 2635#pragma mark Array Types 2636 2637void * 2638ClangASTContext::CreateArrayType (void *element_type, size_t element_count, uint32_t bit_stride) 2639{ 2640 if (element_type) 2641 { 2642 ASTContext *ast_context = getASTContext(); 2643 assert (ast_context != NULL); 2644 llvm::APInt ap_element_count (64, element_count); 2645 return ast_context->getConstantArrayType(QualType::getFromOpaquePtr(element_type), 2646 ap_element_count, 2647 ArrayType::Normal, 2648 0).getAsOpaquePtr(); // ElemQuals 2649 } 2650 return NULL; 2651} 2652 2653 2654#pragma mark TagDecl 2655 2656bool 2657ClangASTContext::StartTagDeclarationDefinition (void *clang_type) 2658{ 2659 if (clang_type) 2660 { 2661 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2662 clang::Type *t = qual_type.getTypePtr(); 2663 if (t) 2664 { 2665 TagType *tag_type = dyn_cast<TagType>(t); 2666 if (tag_type) 2667 { 2668 TagDecl *tag_decl = tag_type->getDecl(); 2669 if (tag_decl) 2670 { 2671 tag_decl->startDefinition(); 2672 return true; 2673 } 2674 } 2675 } 2676 } 2677 return false; 2678} 2679 2680bool 2681ClangASTContext::CompleteTagDeclarationDefinition (void *clang_type) 2682{ 2683 if (clang_type) 2684 { 2685 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2686 clang::Type *t = qual_type.getTypePtr(); 2687 if (t) 2688 { 2689 TagType *tag_type = dyn_cast<TagType>(t); 2690 if (tag_type) 2691 { 2692 TagDecl *tag_decl = tag_type->getDecl(); 2693 if (tag_decl) 2694 { 2695 tag_decl->completeDefinition(); 2696 return true; 2697 } 2698 } 2699 } 2700 } 2701 return false; 2702} 2703 2704 2705#pragma mark Enumeration Types 2706 2707void * 2708ClangASTContext::CreateEnumerationType (const Declaration &decl, const char *name, void *integer_qual_type) 2709{ 2710 // TODO: Do something intelligent with the Declaration object passed in 2711 // like maybe filling in the SourceLocation with it... 2712 ASTContext *ast_context = getASTContext(); 2713 assert (ast_context != NULL); 2714 EnumDecl *enum_decl = EnumDecl::Create(*ast_context, 2715 ast_context->getTranslationUnitDecl(), 2716 SourceLocation(), 2717 name && name[0] ? &ast_context->Idents.get(name) : NULL, 2718 SourceLocation(), 2719 NULL); 2720 if (enum_decl) 2721 { 2722 // TODO: check if we should be setting the promotion type too? 2723 enum_decl->setIntegerType(QualType::getFromOpaquePtr (integer_qual_type)); 2724 return ast_context->getTagDeclType(enum_decl).getAsOpaquePtr(); 2725 } 2726 return NULL; 2727} 2728 2729bool 2730ClangASTContext::AddEnumerationValueToEnumerationType 2731( 2732 void *enum_clang_type, 2733 void *enumerator_clang_type, 2734 const Declaration &decl, 2735 const char *name, 2736 int64_t enum_value, 2737 uint32_t enum_value_bit_size 2738) 2739{ 2740 if (enum_clang_type && enumerator_clang_type && name) 2741 { 2742 // TODO: Do something intelligent with the Declaration object passed in 2743 // like maybe filling in the SourceLocation with it... 2744 ASTContext *ast_context = getASTContext(); 2745 IdentifierTable *identifier_table = getIdentifierTable(); 2746 2747 assert (ast_context != NULL); 2748 assert (identifier_table != NULL); 2749 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type)); 2750 2751 clang::Type *clang_type = enum_qual_type.getTypePtr(); 2752 if (clang_type) 2753 { 2754 const EnumType *enum_type = dyn_cast<EnumType>(clang_type); 2755 2756 if (enum_type) 2757 { 2758 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, false); 2759 enum_llvm_apsint = enum_value; 2760 EnumConstantDecl *enumerator_decl = 2761 EnumConstantDecl::Create(*ast_context, 2762 enum_type->getDecl(), 2763 SourceLocation(), 2764 name ? &identifier_table->get(name) : NULL, // Identifier 2765 QualType::getFromOpaquePtr(enumerator_clang_type), 2766 NULL, 2767 enum_llvm_apsint); 2768 2769 if (enumerator_decl) 2770 { 2771 enum_type->getDecl()->addDecl(enumerator_decl); 2772 return true; 2773 } 2774 } 2775 } 2776 } 2777 return false; 2778} 2779 2780#pragma mark Pointers & References 2781 2782void * 2783ClangASTContext::CreatePointerType (void *clang_type) 2784{ 2785 if (clang_type) 2786 { 2787 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2788 2789 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2790 switch (type_class) 2791 { 2792 case clang::Type::ObjCObject: 2793 case clang::Type::ObjCInterface: 2794 return getASTContext()->getObjCObjectPointerType(qual_type).getAsOpaquePtr(); 2795 2796 default: 2797 return getASTContext()->getPointerType(qual_type).getAsOpaquePtr(); 2798 } 2799 } 2800 return NULL; 2801} 2802 2803void * 2804ClangASTContext::CreateLValueReferenceType (void *clang_type) 2805{ 2806 if (clang_type) 2807 return getASTContext()->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr(); 2808 return NULL; 2809} 2810 2811void * 2812ClangASTContext::CreateRValueReferenceType (void *clang_type) 2813{ 2814 if (clang_type) 2815 return getASTContext()->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr(); 2816 return NULL; 2817} 2818 2819void * 2820ClangASTContext::CreateMemberPointerType (void *clang_pointee_type, void *clang_class_type) 2821{ 2822 if (clang_pointee_type && clang_pointee_type) 2823 return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type), 2824 QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr(); 2825 return NULL; 2826} 2827 2828size_t 2829ClangASTContext::GetPointerBitSize () 2830{ 2831 ASTContext *ast_context = getASTContext(); 2832 return ast_context->getTypeSize(ast_context->VoidPtrTy); 2833} 2834 2835bool 2836ClangASTContext::IsPointerOrReferenceType (void *clang_type, void **target_type) 2837{ 2838 if (clang_type == NULL) 2839 return false; 2840 2841 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2842 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2843 switch (type_class) 2844 { 2845 case clang::Type::ObjCObjectPointer: 2846 if (target_type) 2847 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 2848 return true; 2849 case clang::Type::BlockPointer: 2850 if (target_type) 2851 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 2852 return true; 2853 case clang::Type::Pointer: 2854 if (target_type) 2855 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 2856 return true; 2857 case clang::Type::MemberPointer: 2858 if (target_type) 2859 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 2860 return true; 2861 case clang::Type::LValueReference: 2862 if (target_type) 2863 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr(); 2864 return true; 2865 case clang::Type::RValueReference: 2866 if (target_type) 2867 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr(); 2868 return true; 2869 case clang::Type::Typedef: 2870 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr()); 2871 default: 2872 break; 2873 } 2874 return false; 2875} 2876 2877bool 2878ClangASTContext::IsIntegerType (void *clang_type, bool &is_signed) 2879{ 2880 if (!clang_type) 2881 return false; 2882 2883 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2884 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()); 2885 2886 if (builtin_type) 2887 { 2888 if (builtin_type->isInteger()) 2889 is_signed = builtin_type->isSignedInteger(); 2890 2891 return true; 2892 } 2893 2894 return false; 2895} 2896 2897bool 2898ClangASTContext::IsPointerType (void *clang_type, void **target_type) 2899{ 2900 if (clang_type) 2901 { 2902 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2903 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2904 switch (type_class) 2905 { 2906 case clang::Type::ObjCObjectPointer: 2907 if (target_type) 2908 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 2909 return true; 2910 case clang::Type::BlockPointer: 2911 if (target_type) 2912 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 2913 return true; 2914 case clang::Type::Pointer: 2915 if (target_type) 2916 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 2917 return true; 2918 case clang::Type::MemberPointer: 2919 if (target_type) 2920 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 2921 return true; 2922 case clang::Type::Typedef: 2923 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), target_type); 2924 default: 2925 break; 2926 } 2927 } 2928 return false; 2929} 2930 2931bool 2932ClangASTContext::IsFloatingPointType (void *clang_type, uint32_t &count, bool &is_complex) 2933{ 2934 if (clang_type) 2935 { 2936 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2937 2938 if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal())) 2939 { 2940 clang::BuiltinType::Kind kind = BT->getKind(); 2941 if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble) 2942 { 2943 count = 1; 2944 is_complex = false; 2945 return true; 2946 } 2947 } 2948 else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal())) 2949 { 2950 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex)) 2951 { 2952 count = 2; 2953 is_complex = true; 2954 return true; 2955 } 2956 } 2957 else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal())) 2958 { 2959 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex)) 2960 { 2961 count = VT->getNumElements(); 2962 is_complex = false; 2963 return true; 2964 } 2965 } 2966 } 2967 return false; 2968} 2969 2970 2971bool 2972ClangASTContext::IsCStringType (void *clang_type, uint32_t &length) 2973{ 2974 if (clang_type) 2975 { 2976 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2977 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 2978 switch (type_class) 2979 { 2980 case clang::Type::ConstantArray: 2981 { 2982 ConstantArrayType *array = cast<ConstantArrayType>(qual_type.getTypePtr()); 2983 QualType element_qual_type = array->getElementType(); 2984 clang::Type *canonical_type = element_qual_type->getCanonicalTypeInternal().getTypePtr(); 2985 if (canonical_type && canonical_type->isCharType()) 2986 { 2987 // We know the size of the array and it could be a C string 2988 // since it is an array of characters 2989 length = array->getSize().getLimitedValue(); 2990 return true; 2991 } 2992 } 2993 break; 2994 2995 case clang::Type::Pointer: 2996 { 2997 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 2998 clang::Type *pointee_type_ptr = pointer_type->getPointeeType().getTypePtr(); 2999 if (pointee_type_ptr) 3000 { 3001 clang::Type *canonical_type_ptr = pointee_type_ptr->getCanonicalTypeInternal().getTypePtr(); 3002 length = 0; // No length info, read until a NULL terminator is received 3003 if (canonical_type_ptr) 3004 return canonical_type_ptr->isCharType(); 3005 else 3006 return pointee_type_ptr->isCharType(); 3007 } 3008 } 3009 break; 3010 3011 case clang::Type::Typedef: 3012 return ClangASTContext::IsCStringType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), length); 3013 3014 case clang::Type::LValueReference: 3015 case clang::Type::RValueReference: 3016 { 3017 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 3018 clang::Type *pointee_type_ptr = reference_type->getPointeeType().getTypePtr(); 3019 if (pointee_type_ptr) 3020 { 3021 clang::Type *canonical_type_ptr = pointee_type_ptr->getCanonicalTypeInternal().getTypePtr(); 3022 length = 0; // No length info, read until a NULL terminator is received 3023 if (canonical_type_ptr) 3024 return canonical_type_ptr->isCharType(); 3025 else 3026 return pointee_type_ptr->isCharType(); 3027 } 3028 } 3029 break; 3030 } 3031 } 3032 return false; 3033} 3034 3035bool 3036ClangASTContext::IsFunctionPointerType (void *clang_type) 3037{ 3038 if (clang_type) 3039 { 3040 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3041 3042 if (qual_type->isFunctionPointerType()) 3043 return true; 3044 3045 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3046 switch (type_class) 3047 { 3048 case clang::Type::Typedef: 3049 return ClangASTContext::IsFunctionPointerType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr()); 3050 3051 case clang::Type::LValueReference: 3052 case clang::Type::RValueReference: 3053 { 3054 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 3055 if (reference_type) 3056 return ClangASTContext::IsFunctionPointerType (reference_type->getPointeeType().getAsOpaquePtr()); 3057 } 3058 break; 3059 } 3060 } 3061 return false; 3062} 3063 3064 3065 3066 3067bool 3068ClangASTContext::IsArrayType (void *clang_type, void **member_type, uint64_t *size) 3069{ 3070 if (!clang_type) 3071 return false; 3072 3073 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3074 3075 const clang::Type::TypeClass type_class = qual_type->getTypeClass(); 3076 switch (type_class) 3077 { 3078 case clang::Type::ConstantArray: 3079 if (member_type) 3080 *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 3081 if (size) 3082 *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULONG_LONG_MAX); 3083 return true; 3084 case clang::Type::IncompleteArray: 3085 if (member_type) 3086 *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 3087 if (size) 3088 *size = 0; 3089 return true; 3090 case clang::Type::VariableArray: 3091 if (member_type) 3092 *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 3093 if (size) 3094 *size = 0; 3095 case clang::Type::DependentSizedArray: 3096 if (member_type) 3097 *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 3098 if (size) 3099 *size = 0; 3100 return true; 3101 } 3102 return false; 3103} 3104 3105 3106#pragma mark Typedefs 3107 3108void * 3109ClangASTContext::CreateTypedefType (const char *name, void *clang_type, DeclContext *decl_ctx) 3110{ 3111 if (clang_type) 3112 { 3113 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3114 ASTContext *ast_context = getASTContext(); 3115 IdentifierTable *identifier_table = getIdentifierTable(); 3116 assert (ast_context != NULL); 3117 assert (identifier_table != NULL); 3118 if (decl_ctx == NULL) 3119 decl_ctx = ast_context->getTranslationUnitDecl(); 3120 TypedefDecl *decl = TypedefDecl::Create(*ast_context, 3121 decl_ctx, 3122 SourceLocation(), 3123 name ? &identifier_table->get(name) : NULL, // Identifier 3124 ast_context->CreateTypeSourceInfo(qual_type)); 3125 3126 // Get a uniqued QualType for the typedef decl type 3127 return ast_context->getTypedefType (decl).getAsOpaquePtr(); 3128 } 3129 return NULL; 3130} 3131 3132 3133std::string 3134ClangASTContext::GetTypeName (void *opaque_qual_type) 3135{ 3136 std::string return_name; 3137 3138 clang::QualType qual_type(clang::QualType::getFromOpaquePtr(opaque_qual_type)); 3139 3140 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>(); 3141 if (typedef_type) 3142 { 3143 const clang::TypedefDecl *typedef_decl = typedef_type->getDecl(); 3144 return_name = typedef_decl->getQualifiedNameAsString(); 3145 } 3146 else 3147 { 3148 return_name = qual_type.getAsString(); 3149 } 3150 3151 return return_name; 3152} 3153 3154// Disable this for now since I can't seem to get a nicely formatted float 3155// out of the APFloat class without just getting the float, double or quad 3156// and then using a formatted print on it which defeats the purpose. We ideally 3157// would like to get perfect string values for any kind of float semantics 3158// so we can support remote targets. The code below also requires a patch to 3159// llvm::APInt. 3160//bool 3161//ClangASTContext::ConvertFloatValueToString (ASTContext *ast_context, void *clang_type, const uint8_t* bytes, size_t byte_size, int apint_byte_order, std::string &float_str) 3162//{ 3163// uint32_t count = 0; 3164// bool is_complex = false; 3165// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex)) 3166// { 3167// unsigned num_bytes_per_float = byte_size / count; 3168// unsigned num_bits_per_float = num_bytes_per_float * 8; 3169// 3170// float_str.clear(); 3171// uint32_t i; 3172// for (i=0; i<count; i++) 3173// { 3174// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order); 3175// bool is_ieee = false; 3176// APFloat ap_float(ap_int, is_ieee); 3177// char s[1024]; 3178// unsigned int hex_digits = 0; 3179// bool upper_case = false; 3180// 3181// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0) 3182// { 3183// if (i > 0) 3184// float_str.append(", "); 3185// float_str.append(s); 3186// if (i == 1 && is_complex) 3187// float_str.append(1, 'i'); 3188// } 3189// } 3190// return !float_str.empty(); 3191// } 3192// return false; 3193//} 3194 3195size_t 3196ClangASTContext::ConvertStringToFloatValue (ASTContext *ast_context, void *clang_type, const char *s, uint8_t *dst, size_t dst_size) 3197{ 3198 if (clang_type) 3199 { 3200 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3201 uint32_t count = 0; 3202 bool is_complex = false; 3203 if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex)) 3204 { 3205 // TODO: handle complex and vector types 3206 if (count != 1) 3207 return false; 3208 3209 StringRef s_sref(s); 3210 APFloat ap_float(ast_context->getFloatTypeSemantics(qual_type), s_sref); 3211 3212 const uint64_t bit_size = ast_context->getTypeSize (qual_type); 3213 const uint64_t byte_size = bit_size / 8; 3214 if (dst_size >= byte_size) 3215 { 3216 if (bit_size == sizeof(float)*8) 3217 { 3218 float float32 = ap_float.convertToFloat(); 3219 ::memcpy (dst, &float32, byte_size); 3220 return byte_size; 3221 } 3222 else if (bit_size >= 64) 3223 { 3224 llvm::APInt ap_int(ap_float.bitcastToAPInt()); 3225 ::memcpy (dst, ap_int.getRawData(), byte_size); 3226 return byte_size; 3227 } 3228 } 3229 } 3230 } 3231 return 0; 3232} 3233 3234unsigned 3235ClangASTContext::GetTypeQualifiers(void *clang_type) 3236{ 3237 assert (clang_type); 3238 3239 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 3240 3241 return qual_type.getQualifiers().getCVRQualifiers(); 3242} 3243