ClangASTContext.cpp revision fa97069b86f30aa44bb23a1de506ae220359b4fa
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 "ClangASTContext.h" 11 12// C Includes 13// C++ Includes 14#include <string> 15 16// Other libraries and framework includes 17#include "clang/AST/ASTContext.h" 18#include "clang/AST/ASTImporter.h" 19#include "clang/AST/CXXInheritance.h" 20#include "clang/AST/RecordLayout.h" 21#include "clang/AST/Type.h" 22#include "clang/Basic/Builtins.h" 23#include "clang/Basic/FileManager.h" 24#include "clang/Basic/SourceManager.h" 25#include "clang/Basic/TargetInfo.h" 26#include "clang/Basic/TargetOptions.h" 27#include "clang/Frontend/FrontendOptions.h" 28#include "clang/Frontend/LangStandard.h" 29 30// Project includes 31#include "lldb/Core/dwarf.h" 32 33using namespace lldb_private; 34using namespace llvm; 35using namespace clang; 36 37 38static void 39ParseLangArgs 40( 41 LangOptions &Opts, 42 FrontendOptions::InputKind IK 43) 44{ 45 // FIXME: Cleanup per-file based stuff. 46 47 // Set some properties which depend soley on the input kind; it would be nice 48 // to move these to the language standard, and have the driver resolve the 49 // input kind + language standard. 50 if (IK == FrontendOptions::IK_Asm) { 51 Opts.AsmPreprocessor = 1; 52 } else if (IK == FrontendOptions::IK_ObjC || 53 IK == FrontendOptions::IK_ObjCXX || 54 IK == FrontendOptions::IK_PreprocessedObjC || 55 IK == FrontendOptions::IK_PreprocessedObjCXX) { 56 Opts.ObjC1 = Opts.ObjC2 = 1; 57 } 58 59 LangStandard::Kind LangStd = LangStandard::lang_unspecified; 60 61 if (LangStd == LangStandard::lang_unspecified) { 62 // Based on the base language, pick one. 63 switch (IK) { 64 case FrontendOptions::IK_None: 65 case FrontendOptions::IK_AST: 66 assert(0 && "Invalid input kind!"); 67 case FrontendOptions::IK_OpenCL: 68 LangStd = LangStandard::lang_opencl; 69 break; 70 case FrontendOptions::IK_Asm: 71 case FrontendOptions::IK_C: 72 case FrontendOptions::IK_PreprocessedC: 73 case FrontendOptions::IK_ObjC: 74 case FrontendOptions::IK_PreprocessedObjC: 75 LangStd = LangStandard::lang_gnu99; 76 break; 77 case FrontendOptions::IK_CXX: 78 case FrontendOptions::IK_PreprocessedCXX: 79 case FrontendOptions::IK_ObjCXX: 80 case FrontendOptions::IK_PreprocessedObjCXX: 81 LangStd = LangStandard::lang_gnucxx98; 82 break; 83 } 84 } 85 86 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd); 87 Opts.BCPLComment = Std.hasBCPLComments(); 88 Opts.C99 = Std.isC99(); 89 Opts.CPlusPlus = Std.isCPlusPlus(); 90 Opts.CPlusPlus0x = Std.isCPlusPlus0x(); 91 Opts.Digraphs = Std.hasDigraphs(); 92 Opts.GNUMode = Std.isGNUMode(); 93 Opts.GNUInline = !Std.isC99(); 94 Opts.HexFloats = Std.hasHexFloats(); 95 Opts.ImplicitInt = Std.hasImplicitInt(); 96 97 // OpenCL has some additional defaults. 98 if (LangStd == LangStandard::lang_opencl) { 99 Opts.OpenCL = 1; 100 Opts.AltiVec = 1; 101 Opts.CXXOperatorNames = 1; 102 Opts.LaxVectorConversions = 1; 103 } 104 105 // OpenCL and C++ both have bool, true, false keywords. 106 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus; 107 108// if (Opts.CPlusPlus) 109// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names); 110// 111// if (Args.hasArg(OPT_fobjc_gc_only)) 112// Opts.setGCMode(LangOptions::GCOnly); 113// else if (Args.hasArg(OPT_fobjc_gc)) 114// Opts.setGCMode(LangOptions::HybridGC); 115// 116// if (Args.hasArg(OPT_print_ivar_layout)) 117// Opts.ObjCGCBitmapPrint = 1; 118// 119// if (Args.hasArg(OPT_faltivec)) 120// Opts.AltiVec = 1; 121// 122// if (Args.hasArg(OPT_pthread)) 123// Opts.POSIXThreads = 1; 124// 125// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility, 126// "default"); 127// if (Vis == "default") 128 Opts.setVisibilityMode(LangOptions::Default); 129// else if (Vis == "hidden") 130// Opts.setVisibilityMode(LangOptions::Hidden); 131// else if (Vis == "protected") 132// Opts.setVisibilityMode(LangOptions::Protected); 133// else 134// Diags.Report(diag::err_drv_invalid_value) 135// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis; 136 137// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv); 138 139 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs 140 // is specified, or -std is set to a conforming mode. 141 Opts.Trigraphs = !Opts.GNUMode; 142// if (Args.hasArg(OPT_trigraphs)) 143// Opts.Trigraphs = 1; 144// 145// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers, 146// OPT_fno_dollars_in_identifiers, 147// !Opts.AsmPreprocessor); 148// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings); 149// Opts.Microsoft = Args.hasArg(OPT_fms_extensions); 150// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings); 151// if (Args.hasArg(OPT_fno_lax_vector_conversions)) 152// Opts.LaxVectorConversions = 0; 153// Opts.Exceptions = Args.hasArg(OPT_fexceptions); 154// Opts.RTTI = !Args.hasArg(OPT_fno_rtti); 155// Opts.Blocks = Args.hasArg(OPT_fblocks); 156// Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char); 157// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar); 158// Opts.Freestanding = Args.hasArg(OPT_ffreestanding); 159// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding; 160// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new); 161// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions); 162// Opts.AccessControl = Args.hasArg(OPT_faccess_control); 163// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors); 164// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno); 165// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99, 166// Diags); 167// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime); 168// Opts.ObjCConstantStringClass = getLastArgValue(Args, 169// OPT_fconstant_string_class); 170// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi); 171// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior); 172// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls); 173// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags); 174// Opts.Static = Args.hasArg(OPT_static_define); 175 Opts.OptimizeSize = 0; 176 177 // FIXME: Eliminate this dependency. 178// unsigned Opt = 179// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags); 180// Opts.Optimize = Opt != 0; 181 unsigned Opt = 0; 182 183 // This is the __NO_INLINE__ define, which just depends on things like the 184 // optimization level and -fno-inline, not actually whether the backend has 185 // inlining enabled. 186 // 187 // FIXME: This is affected by other options (-fno-inline). 188 Opts.NoInline = !Opt; 189 190// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags); 191// switch (SSP) { 192// default: 193// Diags.Report(diag::err_drv_invalid_value) 194// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP; 195// break; 196// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break; 197// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break; 198// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break; 199// } 200} 201 202//---------------------------------------------------------------------- 203// ClangASTContext constructor 204//---------------------------------------------------------------------- 205//ClangASTContext::ClangASTContext(Module *module) : 206// m_target_triple(), 207// m_ast_context_ap(), 208// m_language_options_ap(), 209// m_source_manager_ap(), 210// m_target_info_ap(), 211// m_identifier_table_ap(), 212// m_selector_table_ap(), 213// m_builtins_ap() 214//{ 215// if (module) 216// { 217// ObjectFile * objfile = module->GetObjectFile(); 218// if (objfile) 219// objfile->GetTargetTriple(m_target_triple); 220// } 221//} 222 223//ClangASTContext::ClangASTContext(const ConstString& target_triple) : 224// m_target_triple(target_triple), 225// m_ast_context_ap(), 226// m_language_options_ap(), 227// m_source_manager_ap(), 228// m_target_info_ap(), 229// m_identifier_table_ap(), 230// m_selector_table_ap(), 231// m_builtins_ap() 232//{ 233//} 234ClangASTContext::ClangASTContext(const char *target_triple) : 235 m_target_triple(), 236 m_ast_context_ap(), 237 m_language_options_ap(), 238 m_source_manager_ap(), 239 m_diagnostic_ap(), 240 m_target_options_ap(), 241 m_target_info_ap(), 242 m_identifier_table_ap(), 243 m_selector_table_ap(), 244 m_builtins_ap() 245{ 246 if (target_triple && target_triple[0]) 247 m_target_triple.assign (target_triple); 248} 249 250//---------------------------------------------------------------------- 251// Destructor 252//---------------------------------------------------------------------- 253ClangASTContext::~ClangASTContext() 254{ 255 m_builtins_ap.reset(); 256 m_selector_table_ap.reset(); 257 m_identifier_table_ap.reset(); 258 m_target_info_ap.reset(); 259 m_target_options_ap.reset(); 260 m_diagnostic_ap.reset(); 261 m_source_manager_ap.reset(); 262 m_language_options_ap.reset(); 263 m_ast_context_ap.reset(); 264} 265 266 267void 268ClangASTContext::Clear() 269{ 270 m_ast_context_ap.reset(); 271 m_language_options_ap.reset(); 272 m_source_manager_ap.reset(); 273 m_diagnostic_ap.reset(); 274 m_target_options_ap.reset(); 275 m_target_info_ap.reset(); 276 m_identifier_table_ap.reset(); 277 m_selector_table_ap.reset(); 278 m_builtins_ap.reset(); 279} 280 281const char * 282ClangASTContext::GetTargetTriple () 283{ 284 return m_target_triple.c_str(); 285} 286 287void 288ClangASTContext::SetTargetTriple (const char *target_triple) 289{ 290 Clear(); 291 m_target_triple.assign(target_triple); 292} 293 294 295ASTContext * 296ClangASTContext::getASTContext() 297{ 298 if (m_ast_context_ap.get() == NULL) 299 { 300 m_ast_context_ap.reset( 301 new ASTContext( 302 *getLanguageOptions(), 303 *getSourceManager(), 304 *getTargetInfo(), 305 *getIdentifierTable(), 306 *getSelectorTable(), 307 *getBuiltinContext())); 308 } 309 return m_ast_context_ap.get(); 310} 311 312Builtin::Context * 313ClangASTContext::getBuiltinContext() 314{ 315 if (m_builtins_ap.get() == NULL) 316 m_builtins_ap.reset (new Builtin::Context(*getTargetInfo())); 317 return m_builtins_ap.get(); 318} 319 320IdentifierTable * 321ClangASTContext::getIdentifierTable() 322{ 323 if (m_identifier_table_ap.get() == NULL) 324 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), NULL)); 325 return m_identifier_table_ap.get(); 326} 327 328LangOptions * 329ClangASTContext::getLanguageOptions() 330{ 331 if (m_language_options_ap.get() == NULL) 332 { 333 m_language_options_ap.reset(new LangOptions()); 334 ParseLangArgs(*m_language_options_ap, FrontendOptions::IK_ObjCXX); 335// InitializeLangOptions(*m_language_options_ap, FrontendOptions::IK_ObjCXX); 336 } 337 return m_language_options_ap.get(); 338} 339 340SelectorTable * 341ClangASTContext::getSelectorTable() 342{ 343 if (m_selector_table_ap.get() == NULL) 344 m_selector_table_ap.reset (new SelectorTable()); 345 return m_selector_table_ap.get(); 346} 347 348SourceManager * 349ClangASTContext::getSourceManager() 350{ 351 if (m_source_manager_ap.get() == NULL) 352 m_source_manager_ap.reset(new SourceManager(*getDiagnostic())); 353 return m_source_manager_ap.get(); 354} 355 356Diagnostic * 357ClangASTContext::getDiagnostic() 358{ 359 if (m_diagnostic_ap.get() == NULL) 360 m_diagnostic_ap.reset(new Diagnostic()); 361 return m_diagnostic_ap.get(); 362} 363 364TargetOptions * 365ClangASTContext::getTargetOptions() 366{ 367 if (m_target_options_ap.get() == NULL && !m_target_triple.empty()) 368 { 369 m_target_options_ap.reset (new TargetOptions()); 370 if (m_target_options_ap.get()) 371 m_target_options_ap->Triple = m_target_triple; 372 } 373 return m_target_options_ap.get(); 374} 375 376 377TargetInfo * 378ClangASTContext::getTargetInfo() 379{ 380 // target_triple should be something like "x86_64-apple-darwin10" 381 if (m_target_info_ap.get() == NULL && !m_target_triple.empty()) 382 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnostic(), *getTargetOptions())); 383 return m_target_info_ap.get(); 384} 385 386#pragma mark Basic Types 387 388static inline bool 389QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast_context, QualType qual_type) 390{ 391 uint64_t qual_type_bit_size = ast_context->getTypeSize(qual_type); 392 if (qual_type_bit_size == bit_size) 393 return true; 394 return false; 395} 396 397void * 398ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (lldb::Encoding encoding, uint32_t bit_size) 399{ 400 ASTContext *ast_context = getASTContext(); 401 402 assert (ast_context != NULL); 403 404 return GetBuiltinTypeForEncodingAndBitSize (ast_context, encoding, bit_size); 405} 406 407void * 408ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (clang::ASTContext *ast_context, lldb::Encoding encoding, uint32_t bit_size) 409{ 410 if (!ast_context) 411 return NULL; 412 413 switch (encoding) 414 { 415 case lldb::eEncodingInvalid: 416 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->VoidPtrTy)) 417 return ast_context->VoidPtrTy.getAsOpaquePtr(); 418 break; 419 420 case lldb::eEncodingUint: 421 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy)) 422 return ast_context->UnsignedCharTy.getAsOpaquePtr(); 423 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedShortTy)) 424 return ast_context->UnsignedShortTy.getAsOpaquePtr(); 425 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedIntTy)) 426 return ast_context->UnsignedIntTy.getAsOpaquePtr(); 427 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongTy)) 428 return ast_context->UnsignedLongTy.getAsOpaquePtr(); 429 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongLongTy)) 430 return ast_context->UnsignedLongLongTy.getAsOpaquePtr(); 431 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedInt128Ty)) 432 return ast_context->UnsignedInt128Ty.getAsOpaquePtr(); 433 break; 434 435 case lldb::eEncodingSint: 436 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->CharTy)) 437 return ast_context->CharTy.getAsOpaquePtr(); 438 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->ShortTy)) 439 return ast_context->ShortTy.getAsOpaquePtr(); 440 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->IntTy)) 441 return ast_context->IntTy.getAsOpaquePtr(); 442 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongTy)) 443 return ast_context->LongTy.getAsOpaquePtr(); 444 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongLongTy)) 445 return ast_context->LongLongTy.getAsOpaquePtr(); 446 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->Int128Ty)) 447 return ast_context->Int128Ty.getAsOpaquePtr(); 448 break; 449 450 case lldb::eEncodingIEEE754: 451 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->FloatTy)) 452 return ast_context->FloatTy.getAsOpaquePtr(); 453 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->DoubleTy)) 454 return ast_context->DoubleTy.getAsOpaquePtr(); 455 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongDoubleTy)) 456 return ast_context->LongDoubleTy.getAsOpaquePtr(); 457 break; 458 459 case lldb::eEncodingVector: 460 default: 461 break; 462 } 463 464 return NULL; 465} 466 467void * 468ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size) 469{ 470 ASTContext *ast_context = getASTContext(); 471 472 #define streq(a,b) strcmp(a,b) == 0 473 assert (ast_context != NULL); 474 if (ast_context) 475 { 476 switch (dw_ate) 477 { 478 default: 479 break; 480 481 case DW_ATE_address: 482 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->VoidPtrTy)) 483 return ast_context->VoidPtrTy.getAsOpaquePtr(); 484 break; 485 486 case DW_ATE_boolean: 487 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->BoolTy)) 488 return ast_context->BoolTy.getAsOpaquePtr(); 489 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy)) 490 return ast_context->UnsignedCharTy.getAsOpaquePtr(); 491 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedShortTy)) 492 return ast_context->UnsignedShortTy.getAsOpaquePtr(); 493 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedIntTy)) 494 return ast_context->UnsignedIntTy.getAsOpaquePtr(); 495 break; 496 497 case DW_ATE_complex_float: 498 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->FloatComplexTy)) 499 return ast_context->FloatComplexTy.getAsOpaquePtr(); 500 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->DoubleComplexTy)) 501 return ast_context->DoubleComplexTy.getAsOpaquePtr(); 502 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongDoubleComplexTy)) 503 return ast_context->LongDoubleComplexTy.getAsOpaquePtr(); 504 break; 505 506 case DW_ATE_float: 507 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->FloatTy)) 508 return ast_context->FloatTy.getAsOpaquePtr(); 509 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->DoubleTy)) 510 return ast_context->DoubleTy.getAsOpaquePtr(); 511 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongDoubleTy)) 512 return ast_context->LongDoubleTy.getAsOpaquePtr(); 513 break; 514 515 case DW_ATE_signed: 516 if (type_name) 517 { 518 if (streq(type_name, "int") || 519 streq(type_name, "signed int")) 520 { 521 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->IntTy)) 522 return ast_context->IntTy.getAsOpaquePtr(); 523 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->Int128Ty)) 524 return ast_context->Int128Ty.getAsOpaquePtr(); 525 } 526 527 if (streq(type_name, "long int") || 528 streq(type_name, "long long int") || 529 streq(type_name, "signed long long")) 530 { 531 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongTy)) 532 return ast_context->LongTy.getAsOpaquePtr(); 533 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongLongTy)) 534 return ast_context->LongLongTy.getAsOpaquePtr(); 535 } 536 537 if (streq(type_name, "short") || 538 streq(type_name, "short int") || 539 streq(type_name, "signed short") || 540 streq(type_name, "short signed int")) 541 { 542 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->ShortTy)) 543 return ast_context->ShortTy.getAsOpaquePtr(); 544 } 545 546 if (streq(type_name, "char") || 547 streq(type_name, "signed char")) 548 { 549 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->CharTy)) 550 return ast_context->CharTy.getAsOpaquePtr(); 551 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->SignedCharTy)) 552 return ast_context->SignedCharTy.getAsOpaquePtr(); 553 } 554 555 if (streq(type_name, "wchar_t")) 556 { 557 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->WCharTy)) 558 return ast_context->WCharTy.getAsOpaquePtr(); 559 } 560 561 } 562 // We weren't able to match up a type name, just search by size 563 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->CharTy)) 564 return ast_context->CharTy.getAsOpaquePtr(); 565 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->ShortTy)) 566 return ast_context->ShortTy.getAsOpaquePtr(); 567 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->IntTy)) 568 return ast_context->IntTy.getAsOpaquePtr(); 569 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongTy)) 570 return ast_context->LongTy.getAsOpaquePtr(); 571 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->LongLongTy)) 572 return ast_context->LongLongTy.getAsOpaquePtr(); 573 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->Int128Ty)) 574 return ast_context->Int128Ty.getAsOpaquePtr(); 575 break; 576 577 case DW_ATE_signed_char: 578 if (type_name) 579 { 580 if (streq(type_name, "signed char")) 581 { 582 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->SignedCharTy)) 583 return ast_context->SignedCharTy.getAsOpaquePtr(); 584 } 585 } 586 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->CharTy)) 587 return ast_context->CharTy.getAsOpaquePtr(); 588 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->SignedCharTy)) 589 return ast_context->SignedCharTy.getAsOpaquePtr(); 590 break; 591 592 case DW_ATE_unsigned: 593 if (type_name) 594 { 595 if (streq(type_name, "unsigned int")) 596 { 597 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedIntTy)) 598 return ast_context->UnsignedIntTy.getAsOpaquePtr(); 599 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedInt128Ty)) 600 return ast_context->UnsignedInt128Ty.getAsOpaquePtr(); 601 } 602 603 if (streq(type_name, "unsigned int") || 604 streq(type_name, "long unsigned int") || 605 streq(type_name, "unsigned long long")) 606 { 607 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongTy)) 608 return ast_context->UnsignedLongTy.getAsOpaquePtr(); 609 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongLongTy)) 610 return ast_context->UnsignedLongLongTy.getAsOpaquePtr(); 611 } 612 613 if (streq(type_name, "unsigned short") || 614 streq(type_name, "short unsigned int")) 615 { 616 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedShortTy)) 617 return ast_context->UnsignedShortTy.getAsOpaquePtr(); 618 } 619 if (streq(type_name, "unsigned char")) 620 { 621 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy)) 622 return ast_context->UnsignedCharTy.getAsOpaquePtr(); 623 } 624 625 } 626 // We weren't able to match up a type name, just search by size 627 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy)) 628 return ast_context->UnsignedCharTy.getAsOpaquePtr(); 629 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedShortTy)) 630 return ast_context->UnsignedShortTy.getAsOpaquePtr(); 631 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedIntTy)) 632 return ast_context->UnsignedIntTy.getAsOpaquePtr(); 633 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongTy)) 634 return ast_context->UnsignedLongTy.getAsOpaquePtr(); 635 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedLongLongTy)) 636 return ast_context->UnsignedLongLongTy.getAsOpaquePtr(); 637 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedInt128Ty)) 638 return ast_context->UnsignedInt128Ty.getAsOpaquePtr(); 639 break; 640 641 case DW_ATE_unsigned_char: 642 if (QualTypeMatchesBitSize (bit_size, ast_context, ast_context->UnsignedCharTy)) 643 return ast_context->UnsignedCharTy.getAsOpaquePtr(); 644 break; 645 646 case DW_ATE_imaginary_float: 647 break; 648 } 649 } 650 // This assert should fire for anything that we don't catch above so we know 651 // to fix any issues we run into. 652 assert (!"error: ClangASTContext::GetClangTypeForDWARFEncodingAndSize() contains an unhandled encoding. Fix this ASAP!"); 653 return NULL; 654} 655 656void * 657ClangASTContext::GetVoidBuiltInType() 658{ 659 return getASTContext()->VoidTy.getAsOpaquePtr(); 660} 661 662void * 663ClangASTContext::GetCStringType (bool is_const) 664{ 665 QualType char_type(getASTContext()->CharTy); 666 667 if (is_const) 668 char_type.addConst(); 669 670 return getASTContext()->getPointerType(char_type).getAsOpaquePtr(); 671} 672 673void * 674ClangASTContext::GetVoidPtrType (bool is_const) 675{ 676 return GetVoidPtrType(getASTContext(), is_const); 677} 678 679void * 680ClangASTContext::GetVoidPtrType (clang::ASTContext *ast_context, bool is_const) 681{ 682 QualType void_ptr_type(ast_context->VoidPtrTy); 683 684 if (is_const) 685 void_ptr_type.addConst(); 686 687 return void_ptr_type.getAsOpaquePtr(); 688} 689 690void * 691ClangASTContext::CopyType(clang::ASTContext *dest_context, 692 clang::ASTContext *source_context, 693 void * clang_type) 694{ 695 Diagnostic diagnostics; 696 FileManager file_manager; 697 ASTImporter importer(diagnostics, 698 *dest_context, file_manager, 699 *source_context, file_manager); 700 QualType ret = importer.Import(QualType::getFromOpaquePtr(clang_type)); 701 return ret.getAsOpaquePtr(); 702} 703 704#pragma mark CVR modifiers 705 706void * 707ClangASTContext::AddConstModifier (void *clang_type) 708{ 709 if (clang_type) 710 { 711 QualType result(QualType::getFromOpaquePtr(clang_type)); 712 result.addConst(); 713 return result.getAsOpaquePtr(); 714 } 715 return NULL; 716} 717 718void * 719ClangASTContext::AddRestrictModifier (void *clang_type) 720{ 721 if (clang_type) 722 { 723 QualType result(QualType::getFromOpaquePtr(clang_type)); 724 result.getQualifiers().setRestrict (true); 725 return result.getAsOpaquePtr(); 726 } 727 return NULL; 728} 729 730void * 731ClangASTContext::AddVolatileModifier (void *clang_type) 732{ 733 if (clang_type) 734 { 735 QualType result(QualType::getFromOpaquePtr(clang_type)); 736 result.getQualifiers().setVolatile (true); 737 return result.getAsOpaquePtr(); 738 } 739 return NULL; 740} 741 742#pragma mark Structure, Unions, Classes 743 744void * 745ClangASTContext::CreateRecordType (const char *name, int kind, DeclContext *decl_ctx) 746{ 747 ASTContext *ast_context = getASTContext(); 748 assert (ast_context != NULL); 749 750 if (decl_ctx == NULL) 751 decl_ctx = ast_context->getTranslationUnitDecl(); 752 753 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and 754 // we will need to update this code. I was told to currently always use 755 // the CXXRecordDecl class since we often don't know from debug information 756 // if something is struct or a class, so we default to always use the more 757 // complete definition just in case. 758 CXXRecordDecl *decl = CXXRecordDecl::Create(*ast_context, 759 (TagDecl::TagKind)kind, 760 decl_ctx, 761 SourceLocation(), 762 name && name[0] ? &ast_context->Idents.get(name) : NULL); 763 764 return ast_context->getTagDeclType(decl).getAsOpaquePtr(); 765} 766 767bool 768ClangASTContext::AddFieldToRecordType (void * record_clang_type, const char *name, void * field_type, int access, uint32_t bitfield_bit_size) 769{ 770 if (record_clang_type == NULL || field_type == NULL) 771 return false; 772 773 ASTContext *ast_context = getASTContext(); 774 IdentifierTable *identifier_table = getIdentifierTable(); 775 776 assert (ast_context != NULL); 777 assert (identifier_table != NULL); 778 779 QualType record_qual_type(QualType::getFromOpaquePtr(record_clang_type)); 780 781 Type *clang_type = record_qual_type.getTypePtr(); 782 if (clang_type) 783 { 784 const RecordType *record_type = dyn_cast<RecordType>(clang_type); 785 786 if (record_type) 787 { 788 RecordDecl *record_decl = record_type->getDecl(); 789 790 CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 791 if (cxx_record_decl) 792 cxx_record_decl->setEmpty (false); 793 794 clang::Expr *bit_width = NULL; 795 if (bitfield_bit_size != 0) 796 { 797 APInt bitfield_bit_size_apint(ast_context->getTypeSize(ast_context->IntTy), bitfield_bit_size); 798 bit_width = new (*ast_context)IntegerLiteral (bitfield_bit_size_apint, ast_context->IntTy, SourceLocation()); 799 } 800 FieldDecl *field = FieldDecl::Create(*ast_context, 801 record_decl, 802 SourceLocation(), 803 name ? &identifier_table->get(name) : NULL, // Identifier 804 QualType::getFromOpaquePtr(field_type), // Field type 805 NULL, // DeclaratorInfo * 806 bit_width, // BitWidth 807 false); // Mutable 808 809 field->setAccess((AccessSpecifier)access); 810 811 if (field) 812 { 813 record_decl->addDecl(field); 814 return true; 815 } 816 } 817 } 818 return false; 819} 820 821bool 822ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size) 823{ 824 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size); 825} 826 827bool 828ClangASTContext::FieldIsBitfield 829( 830 ASTContext *ast_context, 831 FieldDecl* field, 832 uint32_t& bitfield_bit_size 833) 834{ 835 if (ast_context == NULL || field == NULL) 836 return false; 837 838 if (field->isBitField()) 839 { 840 Expr* bit_width_expr = field->getBitWidth(); 841 if (bit_width_expr) 842 { 843 llvm::APSInt bit_width_apsint; 844 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast_context)) 845 { 846 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX); 847 return true; 848 } 849 } 850 } 851 return false; 852} 853 854bool 855ClangASTContext::RecordHasFields (const RecordDecl *record_decl) 856{ 857 if (record_decl == NULL) 858 return false; 859 860 if (!record_decl->field_empty()) 861 return true; 862 863 // No fields, lets check this is a CXX record and check the base classes 864 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 865 if (cxx_record_decl) 866 { 867 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 868 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 869 base_class != base_class_end; 870 ++base_class) 871 { 872 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 873 if (RecordHasFields(base_class_decl)) 874 return true; 875 } 876 } 877 return false; 878} 879 880void 881ClangASTContext::SetDefaultAccessForRecordFields (void *clang_qual_type, int default_accessibility, int *assigned_accessibilities, size_t num_assigned_accessibilities) 882{ 883 if (clang_qual_type) 884 { 885 QualType qual_type(QualType::getFromOpaquePtr(clang_qual_type)); 886 Type *clang_type = qual_type.getTypePtr(); 887 if (clang_type) 888 { 889 RecordType *record_type = dyn_cast<RecordType>(clang_type); 890 if (record_type) 891 { 892 RecordDecl *record_decl = record_type->getDecl(); 893 if (record_decl) 894 { 895 uint32_t field_idx; 896 RecordDecl::field_iterator field, field_end; 897 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0; 898 field != field_end; 899 ++field, ++field_idx) 900 { 901 // If no accessibility was assigned, assign the correct one 902 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none) 903 field->setAccess ((AccessSpecifier)default_accessibility); 904 } 905 } 906 } 907 } 908 } 909} 910 911#pragma mark C++ Base Classes 912 913CXXBaseSpecifier * 914ClangASTContext::CreateBaseClassSpecifier (void *base_class_type, int access, bool is_virtual, bool base_of_class) 915{ 916 if (base_class_type) 917 return new CXXBaseSpecifier(SourceRange(), is_virtual, base_of_class, (AccessSpecifier)access, QualType::getFromOpaquePtr(base_class_type)); 918 return NULL; 919} 920 921bool 922ClangASTContext::SetBaseClassesForClassType (void *class_clang_type, CXXBaseSpecifier const * const *base_classes, unsigned num_base_classes) 923{ 924 if (class_clang_type) 925 { 926 ASTContext *ast_context = getASTContext(); 927 IdentifierTable *identifier_table = getIdentifierTable(); 928 929 assert (ast_context != NULL); 930 assert (identifier_table != NULL); 931 932 Type *clang_type = QualType::getFromOpaquePtr(class_clang_type).getTypePtr(); 933 if (clang_type) 934 { 935 RecordType *record_type = dyn_cast<RecordType>(clang_type); 936 if (record_type) 937 { 938 CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_type->getDecl()); 939 if (cxx_record_decl) 940 { 941 //cxx_record_decl->setEmpty (false); 942 cxx_record_decl->setBases(base_classes, num_base_classes); 943 return true; 944 } 945 } 946 } 947 } 948 return false; 949} 950 951 952#pragma mark Aggregate Types 953 954bool 955ClangASTContext::IsAggregateType (void *clang_type) 956{ 957 if (clang_type == NULL) 958 return false; 959 960 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 961 962 if (qual_type->isAggregateType ()) 963 return true; 964 965 switch (qual_type->getTypeClass()) 966 { 967 case Type::IncompleteArray: 968 case Type::VariableArray: 969 case Type::ConstantArray: 970 case Type::ExtVector: 971 case Type::Vector: 972 case Type::Record: 973 return true; 974 975 case Type::Typedef: 976 return ClangASTContext::IsAggregateType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr()); 977 978 default: 979 break; 980 } 981 // The clang type does have a value 982 return false; 983} 984 985uint32_t 986ClangASTContext::GetNumChildren (void *clang_qual_type, bool omit_empty_base_classes) 987{ 988 if (clang_qual_type == NULL) 989 return 0; 990 991 uint32_t num_children = 0; 992 QualType qual_type(QualType::getFromOpaquePtr(clang_qual_type)); 993 switch (qual_type->getTypeClass()) 994 { 995 case Type::Record: 996 { 997 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 998 const RecordDecl *record_decl = record_type->getDecl(); 999 assert(record_decl); 1000 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1001 if (cxx_record_decl) 1002 { 1003 if (omit_empty_base_classes) 1004 { 1005 // Check each base classes to see if it or any of its 1006 // base classes contain any fields. This can help 1007 // limit the noise in variable views by not having to 1008 // show base classes that contain no members. 1009 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1010 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1011 base_class != base_class_end; 1012 ++base_class) 1013 { 1014 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 1015 1016 // Skip empty base classes 1017 if (RecordHasFields(base_class_decl) == false) 1018 continue; 1019 1020 num_children++; 1021 } 1022 } 1023 else 1024 { 1025 // Include all base classes 1026 num_children += cxx_record_decl->getNumBases(); 1027 } 1028 1029 } 1030 RecordDecl::field_iterator field, field_end; 1031 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field) 1032 ++num_children; 1033 } 1034 break; 1035 1036 case Type::ConstantArray: 1037 num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue(); 1038 break; 1039 1040 case Type::Pointer: 1041 { 1042 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 1043 QualType pointee_type = pointer_type->getPointeeType(); 1044 uint32_t num_pointee_children = ClangASTContext::GetNumChildren (pointee_type.getAsOpaquePtr(), omit_empty_base_classes); 1045 // If this type points to a simple type, then it has 1 child 1046 if (num_pointee_children == 0) 1047 num_children = 1; 1048 else 1049 num_children = num_pointee_children; 1050 } 1051 break; 1052 1053 case Type::Typedef: 1054 num_children = ClangASTContext::GetNumChildren (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), omit_empty_base_classes); 1055 break; 1056 1057 default: 1058 break; 1059 } 1060 return num_children; 1061} 1062 1063 1064void * 1065ClangASTContext::GetChildClangTypeAtIndex 1066( 1067 const char *parent_name, 1068 void *parent_clang_type, 1069 uint32_t idx, 1070 bool transparent_pointers, 1071 bool omit_empty_base_classes, 1072 std::string& child_name, 1073 uint32_t &child_byte_size, 1074 int32_t &child_byte_offset, 1075 uint32_t &child_bitfield_bit_size, 1076 uint32_t &child_bitfield_bit_offset 1077) 1078{ 1079 if (parent_clang_type) 1080 1081 return GetChildClangTypeAtIndex (getASTContext(), 1082 parent_name, 1083 parent_clang_type, 1084 idx, 1085 transparent_pointers, 1086 omit_empty_base_classes, 1087 child_name, 1088 child_byte_size, 1089 child_byte_offset, 1090 child_bitfield_bit_size, 1091 child_bitfield_bit_offset); 1092 return NULL; 1093} 1094 1095void * 1096ClangASTContext::GetChildClangTypeAtIndex 1097( 1098 ASTContext *ast_context, 1099 const char *parent_name, 1100 void *parent_clang_type, 1101 uint32_t idx, 1102 bool transparent_pointers, 1103 bool omit_empty_base_classes, 1104 std::string& child_name, 1105 uint32_t &child_byte_size, 1106 int32_t &child_byte_offset, 1107 uint32_t &child_bitfield_bit_size, 1108 uint32_t &child_bitfield_bit_offset 1109) 1110{ 1111 if (parent_clang_type == NULL) 1112 return NULL; 1113 1114 if (idx < ClangASTContext::GetNumChildren (parent_clang_type, omit_empty_base_classes)) 1115 { 1116 uint32_t bit_offset; 1117 child_bitfield_bit_size = 0; 1118 child_bitfield_bit_offset = 0; 1119 QualType parent_qual_type(QualType::getFromOpaquePtr(parent_clang_type)); 1120 switch (parent_qual_type->getTypeClass()) 1121 { 1122 case Type::Record: 1123 { 1124 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr()); 1125 const RecordDecl *record_decl = record_type->getDecl(); 1126 assert(record_decl); 1127 const ASTRecordLayout &record_layout = ast_context->getASTRecordLayout(record_decl); 1128 uint32_t child_idx = 0; 1129 1130 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1131 if (cxx_record_decl) 1132 { 1133 // We might have base classes to print out first 1134 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1135 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1136 base_class != base_class_end; 1137 ++base_class) 1138 { 1139 const CXXRecordDecl *base_class_decl = NULL; 1140 1141 // Skip empty base classes 1142 if (omit_empty_base_classes) 1143 { 1144 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 1145 if (RecordHasFields(base_class_decl) == false) 1146 continue; 1147 } 1148 1149 if (idx == child_idx) 1150 { 1151 if (base_class_decl == NULL) 1152 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 1153 1154 1155 if (base_class->isVirtual()) 1156 bit_offset = record_layout.getVBaseClassOffset(base_class_decl); 1157 else 1158 bit_offset = record_layout.getBaseClassOffset(base_class_decl); 1159 1160 // Base classes should be a multiple of 8 bits in size 1161 assert (bit_offset % 8 == 0); 1162 child_byte_offset = bit_offset/8; 1163 std::string base_class_type_name(base_class->getType().getAsString()); 1164 1165 child_name.assign(base_class_type_name.c_str()); 1166 1167 uint64_t clang_type_info_bit_size = ast_context->getTypeSize(base_class->getType()); 1168 1169 // Base classes biut sizes should be a multiple of 8 bits in size 1170 assert (clang_type_info_bit_size % 8 == 0); 1171 child_byte_size = clang_type_info_bit_size / 8; 1172 return base_class->getType().getAsOpaquePtr(); 1173 } 1174 // We don't increment the child index in the for loop since we might 1175 // be skipping empty base classes 1176 ++child_idx; 1177 } 1178 } 1179 const unsigned num_fields = record_layout.getFieldCount(); 1180 1181 // Make sure index is in range... 1182 uint32_t field_idx = 0; 1183 RecordDecl::field_iterator field, field_end; 1184 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx) 1185 { 1186 if (idx == child_idx) 1187 { 1188 // Print the member type if requested 1189 // Print the member name and equal sign 1190 child_name.assign(field->getNameAsString().c_str()); 1191 1192 // Figure out the type byte size (field_type_info.first) and 1193 // alignment (field_type_info.second) from the AST context. 1194 std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(field->getType()); 1195 assert(field_idx < num_fields); 1196 1197 child_byte_size = field_type_info.first / 8; 1198 1199 // Figure out the field offset within the current struct/union/class type 1200 bit_offset = record_layout.getFieldOffset (field_idx); 1201 child_byte_offset = bit_offset / 8; 1202 if (ClangASTContext::FieldIsBitfield (ast_context, *field, child_bitfield_bit_size)) 1203 child_bitfield_bit_offset = bit_offset % 8; 1204 1205 return field->getType().getAsOpaquePtr(); 1206 } 1207 } 1208 } 1209 break; 1210 1211 case Type::ConstantArray: 1212 { 1213 const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); 1214 const uint64_t element_count = array->getSize().getLimitedValue(); 1215 1216 if (idx < element_count) 1217 { 1218 std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(array->getElementType()); 1219 1220 char element_name[32]; 1221 ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx); 1222 1223 child_name.assign(element_name); 1224 assert(field_type_info.first % 8 == 0); 1225 child_byte_size = field_type_info.first / 8; 1226 child_byte_offset = idx * child_byte_size; 1227 return array->getElementType().getAsOpaquePtr(); 1228 } 1229 } 1230 break; 1231 1232 case Type::Pointer: 1233 { 1234 PointerType *pointer_type = cast<PointerType>(parent_qual_type.getTypePtr()); 1235 QualType pointee_type = pointer_type->getPointeeType(); 1236 1237 if (transparent_pointers && ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 1238 { 1239 return GetChildClangTypeAtIndex (ast_context, 1240 parent_name, 1241 pointer_type->getPointeeType().getAsOpaquePtr(), 1242 idx, 1243 transparent_pointers, 1244 omit_empty_base_classes, 1245 child_name, 1246 child_byte_size, 1247 child_byte_offset, 1248 child_bitfield_bit_size, 1249 child_bitfield_bit_offset); 1250 } 1251 else 1252 { 1253 if (parent_name) 1254 { 1255 child_name.assign(1, '*'); 1256 child_name += parent_name; 1257 } 1258 1259 // We have a pointer to an simple type 1260 if (idx == 0) 1261 { 1262 std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type); 1263 assert(clang_type_info.first % 8 == 0); 1264 child_byte_size = clang_type_info.first / 8; 1265 child_byte_offset = 0; 1266 return pointee_type.getAsOpaquePtr(); 1267 } 1268 } 1269 } 1270 break; 1271 1272 case Type::Typedef: 1273 return GetChildClangTypeAtIndex (ast_context, 1274 parent_name, 1275 cast<TypedefType>(parent_qual_type)->LookThroughTypedefs().getAsOpaquePtr(), 1276 idx, 1277 transparent_pointers, 1278 omit_empty_base_classes, 1279 child_name, 1280 child_byte_size, 1281 child_byte_offset, 1282 child_bitfield_bit_size, 1283 child_bitfield_bit_offset); 1284 break; 1285 1286 default: 1287 break; 1288 } 1289 } 1290 return false; 1291} 1292 1293static inline bool 1294BaseSpecifierIsEmpty (const CXXBaseSpecifier *b) 1295{ 1296 return ClangASTContext::RecordHasFields(cast<CXXRecordDecl>(b->getType()->getAs<RecordType>()->getDecl())) == false; 1297} 1298 1299static uint32_t 1300GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes) 1301{ 1302 uint32_t num_bases = 0; 1303 if (cxx_record_decl) 1304 { 1305 if (omit_empty_base_classes) 1306 { 1307 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1308 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1309 base_class != base_class_end; 1310 ++base_class) 1311 { 1312 // Skip empty base classes 1313 if (omit_empty_base_classes) 1314 { 1315 if (BaseSpecifierIsEmpty (base_class)) 1316 continue; 1317 } 1318 ++num_bases; 1319 } 1320 } 1321 else 1322 num_bases = cxx_record_decl->getNumBases(); 1323 } 1324 return num_bases; 1325} 1326 1327 1328static uint32_t 1329GetIndexForRecordBase 1330( 1331 const RecordDecl *record_decl, 1332 const CXXBaseSpecifier *base_spec, 1333 bool omit_empty_base_classes 1334) 1335{ 1336 uint32_t child_idx = 0; 1337 1338 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1339 1340// const char *super_name = record_decl->getNameAsCString(); 1341// const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString(); 1342// printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name); 1343// 1344 if (cxx_record_decl) 1345 { 1346 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1347 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1348 base_class != base_class_end; 1349 ++base_class) 1350 { 1351 if (omit_empty_base_classes) 1352 { 1353 if (BaseSpecifierIsEmpty (base_class)) 1354 continue; 1355 } 1356 1357// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name, 1358// child_idx, 1359// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString()); 1360// 1361// 1362 if (base_class == base_spec) 1363 return child_idx; 1364 ++child_idx; 1365 } 1366 } 1367 1368 return UINT32_MAX; 1369} 1370 1371 1372static uint32_t 1373GetIndexForRecordChild 1374( 1375 const RecordDecl *record_decl, 1376 NamedDecl *canonical_decl, 1377 bool omit_empty_base_classes 1378) 1379{ 1380 uint32_t child_idx = GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl), omit_empty_base_classes); 1381 1382// const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1383// 1384//// printf ("GetIndexForRecordChild (%s, %s)\n", record_decl->getNameAsCString(), canonical_decl->getNameAsCString()); 1385// if (cxx_record_decl) 1386// { 1387// CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1388// for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1389// base_class != base_class_end; 1390// ++base_class) 1391// { 1392// if (omit_empty_base_classes) 1393// { 1394// if (BaseSpecifierIsEmpty (base_class)) 1395// continue; 1396// } 1397// 1398//// printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", 1399//// record_decl->getNameAsCString(), 1400//// canonical_decl->getNameAsCString(), 1401//// child_idx, 1402//// base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString()); 1403// 1404// 1405// CXXRecordDecl *curr_base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 1406// if (curr_base_class_decl == canonical_decl) 1407// { 1408// return child_idx; 1409// } 1410// ++child_idx; 1411// } 1412// } 1413// 1414// const uint32_t num_bases = child_idx; 1415 RecordDecl::field_iterator field, field_end; 1416 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 1417 field != field_end; 1418 ++field, ++child_idx) 1419 { 1420// printf ("GetIndexForRecordChild (%s, %s) field[%u] = %s\n", 1421// record_decl->getNameAsCString(), 1422// canonical_decl->getNameAsCString(), 1423// child_idx - num_bases, 1424// field->getNameAsCString()); 1425 1426 if (field->getCanonicalDecl() == canonical_decl) 1427 return child_idx; 1428 } 1429 1430 return UINT32_MAX; 1431} 1432 1433// Look for a child member (doesn't include base classes, but it does include 1434// their members) in the type hierarchy. Returns an index path into "clang_type" 1435// on how to reach the appropriate member. 1436// 1437// class A 1438// { 1439// public: 1440// int m_a; 1441// int m_b; 1442// }; 1443// 1444// class B 1445// { 1446// }; 1447// 1448// class C : 1449// public B, 1450// public A 1451// { 1452// }; 1453// 1454// If we have a clang type that describes "class C", and we wanted to looked 1455// "m_b" in it: 1456// 1457// With omit_empty_base_classes == false we would get an integer array back with: 1458// { 1, 1 } 1459// The first index 1 is the child index for "class A" within class C 1460// The second index 1 is the child index for "m_b" within class A 1461// 1462// With omit_empty_base_classes == true we would get an integer array back with: 1463// { 0, 1 } 1464// 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) 1465// The second index 1 is the child index for "m_b" within class A 1466 1467size_t 1468ClangASTContext::GetIndexOfChildMemberWithName 1469( 1470 ASTContext *ast_context, 1471 void *clang_type, 1472 const char *name, 1473 bool omit_empty_base_classes, 1474 std::vector<uint32_t>& child_indexes 1475) 1476{ 1477 if (clang_type && name && name[0]) 1478 { 1479 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 1480 switch (qual_type->getTypeClass()) 1481 { 1482 case Type::Record: 1483 { 1484 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 1485 const RecordDecl *record_decl = record_type->getDecl(); 1486 1487 assert(record_decl); 1488 uint32_t child_idx = 0; 1489 1490 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1491 1492 // Try and find a field that matches NAME 1493 RecordDecl::field_iterator field, field_end; 1494 StringRef name_sref(name); 1495 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 1496 field != field_end; 1497 ++field, ++child_idx) 1498 { 1499 if (field->getName().equals (name_sref)) 1500 { 1501 // We have to add on the number of base classes to this index! 1502 child_indexes.push_back (child_idx + GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes)); 1503 return child_indexes.size(); 1504 } 1505 } 1506 1507 if (cxx_record_decl) 1508 { 1509 const RecordDecl *parent_record_decl = cxx_record_decl; 1510 1511 //printf ("parent = %s\n", parent_record_decl->getNameAsCString()); 1512 1513 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl(); 1514 // Didn't find things easily, lets let clang do its thang... 1515 IdentifierInfo & ident_ref = ast_context->Idents.get(name, name + strlen (name)); 1516 DeclarationName decl_name(&ident_ref); 1517 1518 CXXBasePaths paths; 1519 if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember, 1520 decl_name.getAsOpaquePtr(), 1521 paths)) 1522 { 1523 uint32_t child_idx; 1524 CXXBasePaths::const_paths_iterator path, path_end = paths.end(); 1525 for (path = paths.begin(); path != path_end; ++path) 1526 { 1527 const size_t num_path_elements = path->size(); 1528 for (size_t e=0; e<num_path_elements; ++e) 1529 { 1530 CXXBasePathElement elem = (*path)[e]; 1531 1532 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes); 1533 if (child_idx == UINT32_MAX) 1534 { 1535 child_indexes.clear(); 1536 return 0; 1537 } 1538 else 1539 { 1540 child_indexes.push_back (child_idx); 1541 parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl()); 1542 } 1543 } 1544 DeclContext::lookup_iterator named_decl_pos; 1545 for (named_decl_pos = path->Decls.first; 1546 named_decl_pos != path->Decls.second && parent_record_decl; 1547 ++named_decl_pos) 1548 { 1549 //printf ("path[%zu] = %s\n", child_indexes.size(), (*named_decl_pos)->getNameAsCString()); 1550 1551 child_idx = GetIndexForRecordChild (parent_record_decl, *named_decl_pos, omit_empty_base_classes); 1552 if (child_idx == UINT32_MAX) 1553 { 1554 child_indexes.clear(); 1555 return 0; 1556 } 1557 else 1558 { 1559 child_indexes.push_back (child_idx); 1560 } 1561 } 1562 } 1563 return child_indexes.size(); 1564 } 1565 } 1566 1567 } 1568 break; 1569 1570 case Type::ConstantArray: 1571 { 1572// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); 1573// const uint64_t element_count = array->getSize().getLimitedValue(); 1574// 1575// if (idx < element_count) 1576// { 1577// std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(array->getElementType()); 1578// 1579// char element_name[32]; 1580// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx); 1581// 1582// child_name.assign(element_name); 1583// assert(field_type_info.first % 8 == 0); 1584// child_byte_size = field_type_info.first / 8; 1585// child_byte_offset = idx * child_byte_size; 1586// return array->getElementType().getAsOpaquePtr(); 1587// } 1588 } 1589 break; 1590 1591// case Type::MemberPointerType: 1592// { 1593// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr()); 1594// QualType pointee_type = mem_ptr_type->getPointeeType(); 1595// 1596// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 1597// { 1598// return GetIndexOfChildWithName (ast_context, 1599// mem_ptr_type->getPointeeType().getAsOpaquePtr(), 1600// name); 1601// } 1602// } 1603// break; 1604// 1605 case Type::LValueReference: 1606 case Type::RValueReference: 1607 { 1608 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 1609 QualType pointee_type = reference_type->getPointeeType(); 1610 1611 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 1612 { 1613 return GetIndexOfChildMemberWithName (ast_context, 1614 reference_type->getPointeeType().getAsOpaquePtr(), 1615 name, 1616 omit_empty_base_classes, 1617 child_indexes); 1618 } 1619 } 1620 break; 1621 1622 case Type::Pointer: 1623 { 1624 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 1625 QualType pointee_type = pointer_type->getPointeeType(); 1626 1627 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 1628 { 1629 return GetIndexOfChildMemberWithName (ast_context, 1630 pointer_type->getPointeeType().getAsOpaquePtr(), 1631 name, 1632 omit_empty_base_classes, 1633 child_indexes); 1634 } 1635 else 1636 { 1637// if (parent_name) 1638// { 1639// child_name.assign(1, '*'); 1640// child_name += parent_name; 1641// } 1642// 1643// // We have a pointer to an simple type 1644// if (idx == 0) 1645// { 1646// std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type); 1647// assert(clang_type_info.first % 8 == 0); 1648// child_byte_size = clang_type_info.first / 8; 1649// child_byte_offset = 0; 1650// return pointee_type.getAsOpaquePtr(); 1651// } 1652 } 1653 } 1654 break; 1655 1656 case Type::Typedef: 1657 return GetIndexOfChildMemberWithName (ast_context, 1658 cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), 1659 name, 1660 omit_empty_base_classes, 1661 child_indexes); 1662 1663 default: 1664 break; 1665 } 1666 } 1667 return 0; 1668} 1669 1670 1671// Get the index of the child of "clang_type" whose name matches. This function 1672// doesn't descend into the children, but only looks one level deep and name 1673// matches can include base class names. 1674 1675uint32_t 1676ClangASTContext::GetIndexOfChildWithName 1677( 1678 ASTContext *ast_context, 1679 void *clang_type, 1680 const char *name, 1681 bool omit_empty_base_classes 1682) 1683{ 1684 if (clang_type && name && name[0]) 1685 { 1686 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 1687 switch (qual_type->getTypeClass()) 1688 { 1689 case Type::Record: 1690 { 1691 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr()); 1692 const RecordDecl *record_decl = record_type->getDecl(); 1693 1694 assert(record_decl); 1695 uint32_t child_idx = 0; 1696 1697 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1698 1699 if (cxx_record_decl) 1700 { 1701 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1702 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1703 base_class != base_class_end; 1704 ++base_class) 1705 { 1706 // Skip empty base classes 1707 CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 1708 if (omit_empty_base_classes && RecordHasFields(base_class_decl) == false) 1709 continue; 1710 1711 if (base_class->getType().getAsString().compare (name) == 0) 1712 return child_idx; 1713 ++child_idx; 1714 } 1715 } 1716 1717 // Try and find a field that matches NAME 1718 RecordDecl::field_iterator field, field_end; 1719 StringRef name_sref(name); 1720 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); 1721 field != field_end; 1722 ++field, ++child_idx) 1723 { 1724 if (field->getName().equals (name_sref)) 1725 return child_idx; 1726 } 1727 1728 } 1729 break; 1730 1731 case Type::ConstantArray: 1732 { 1733// const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr()); 1734// const uint64_t element_count = array->getSize().getLimitedValue(); 1735// 1736// if (idx < element_count) 1737// { 1738// std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(array->getElementType()); 1739// 1740// char element_name[32]; 1741// ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx); 1742// 1743// child_name.assign(element_name); 1744// assert(field_type_info.first % 8 == 0); 1745// child_byte_size = field_type_info.first / 8; 1746// child_byte_offset = idx * child_byte_size; 1747// return array->getElementType().getAsOpaquePtr(); 1748// } 1749 } 1750 break; 1751 1752// case Type::MemberPointerType: 1753// { 1754// MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr()); 1755// QualType pointee_type = mem_ptr_type->getPointeeType(); 1756// 1757// if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 1758// { 1759// return GetIndexOfChildWithName (ast_context, 1760// mem_ptr_type->getPointeeType().getAsOpaquePtr(), 1761// name); 1762// } 1763// } 1764// break; 1765// 1766 case Type::LValueReference: 1767 case Type::RValueReference: 1768 { 1769 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 1770 QualType pointee_type = reference_type->getPointeeType(); 1771 1772 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 1773 { 1774 return GetIndexOfChildWithName (ast_context, 1775 reference_type->getPointeeType().getAsOpaquePtr(), 1776 name, 1777 omit_empty_base_classes); 1778 } 1779 } 1780 break; 1781 1782 case Type::Pointer: 1783 { 1784 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 1785 QualType pointee_type = pointer_type->getPointeeType(); 1786 1787 if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr())) 1788 { 1789 return GetIndexOfChildWithName (ast_context, 1790 pointer_type->getPointeeType().getAsOpaquePtr(), 1791 name, 1792 omit_empty_base_classes); 1793 } 1794 else 1795 { 1796// if (parent_name) 1797// { 1798// child_name.assign(1, '*'); 1799// child_name += parent_name; 1800// } 1801// 1802// // We have a pointer to an simple type 1803// if (idx == 0) 1804// { 1805// std::pair<uint64_t, unsigned> clang_type_info = ast_context->getTypeInfo(pointee_type); 1806// assert(clang_type_info.first % 8 == 0); 1807// child_byte_size = clang_type_info.first / 8; 1808// child_byte_offset = 0; 1809// return pointee_type.getAsOpaquePtr(); 1810// } 1811 } 1812 } 1813 break; 1814 1815 case Type::Typedef: 1816 return GetIndexOfChildWithName (ast_context, 1817 cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), 1818 name, 1819 omit_empty_base_classes); 1820 1821 default: 1822 break; 1823 } 1824 } 1825 return UINT32_MAX; 1826} 1827 1828#pragma mark TagType 1829 1830bool 1831ClangASTContext::SetTagTypeKind (void *tag_clang_type, int kind) 1832{ 1833 if (tag_clang_type) 1834 { 1835 QualType tag_qual_type(QualType::getFromOpaquePtr(tag_clang_type)); 1836 Type *clang_type = tag_qual_type.getTypePtr(); 1837 if (clang_type) 1838 { 1839 TagType *tag_type = dyn_cast<TagType>(clang_type); 1840 if (tag_type) 1841 { 1842 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl()); 1843 if (tag_decl) 1844 { 1845 tag_decl->setTagKind ((TagDecl::TagKind)kind); 1846 return true; 1847 } 1848 } 1849 } 1850 } 1851 return false; 1852} 1853 1854 1855#pragma mark DeclContext Functions 1856 1857DeclContext * 1858ClangASTContext::GetDeclContextForType (void *clang_type) 1859{ 1860 if (clang_type == NULL) 1861 return NULL; 1862 1863 QualType qual_type(QualType::getFromOpaquePtr(clang_type)); 1864 switch (qual_type->getTypeClass()) 1865 { 1866 case Type::FunctionNoProto: break; 1867 case Type::FunctionProto: break; 1868 case Type::IncompleteArray: break; 1869 case Type::VariableArray: break; 1870 case Type::ConstantArray: break; 1871 case Type::ExtVector: break; 1872 case Type::Vector: break; 1873 case Type::Builtin: break; 1874 case Type::ObjCObjectPointer: break; 1875 case Type::BlockPointer: break; 1876 case Type::Pointer: break; 1877 case Type::LValueReference: break; 1878 case Type::RValueReference: break; 1879 case Type::MemberPointer: break; 1880 case Type::Complex: break; 1881 case Type::ObjCInterface: break; 1882 case Type::Record: 1883 return cast<RecordType>(qual_type)->getDecl(); 1884 case Type::Enum: 1885 return cast<EnumType>(qual_type)->getDecl(); 1886 case Type::Typedef: 1887 return ClangASTContext::GetDeclContextForType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr()); 1888 1889 case Type::TypeOfExpr: break; 1890 case Type::TypeOf: break; 1891 case Type::Decltype: break; 1892 //case Type::QualifiedName: break; 1893 case Type::TemplateSpecialization: break; 1894 } 1895 // No DeclContext in this type... 1896 return NULL; 1897} 1898 1899#pragma mark Namespace Declarations 1900 1901NamespaceDecl * 1902ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, const Declaration &decl, DeclContext *decl_ctx) 1903{ 1904 // TODO: Do something intelligent with the Declaration object passed in 1905 // like maybe filling in the SourceLocation with it... 1906 if (name) 1907 { 1908 ASTContext *ast_context = getASTContext(); 1909 if (decl_ctx == NULL) 1910 decl_ctx = ast_context->getTranslationUnitDecl(); 1911 return NamespaceDecl::Create(*ast_context, decl_ctx, SourceLocation(), &ast_context->Idents.get(name)); 1912 } 1913 return NULL; 1914} 1915 1916 1917#pragma mark Function Types 1918 1919FunctionDecl * 1920ClangASTContext::CreateFunctionDeclaration (const char *name, void *function_clang_type, int storage, bool is_inline) 1921{ 1922 if (name) 1923 { 1924 ASTContext *ast_context = getASTContext(); 1925 assert (ast_context != NULL); 1926 1927 if (name && name[0]) 1928 { 1929 return FunctionDecl::Create(*ast_context, 1930 ast_context->getTranslationUnitDecl(), 1931 SourceLocation(), 1932 DeclarationName (&ast_context->Idents.get(name)), 1933 QualType::getFromOpaquePtr(function_clang_type), 1934 NULL, 1935 (FunctionDecl::StorageClass)storage, 1936 (FunctionDecl::StorageClass)storage, 1937 is_inline); 1938 } 1939 else 1940 { 1941 return FunctionDecl::Create(*ast_context, 1942 ast_context->getTranslationUnitDecl(), 1943 SourceLocation(), 1944 DeclarationName (), 1945 QualType::getFromOpaquePtr(function_clang_type), 1946 NULL, 1947 (FunctionDecl::StorageClass)storage, 1948 (FunctionDecl::StorageClass)storage, 1949 is_inline); 1950 } 1951 } 1952 return NULL; 1953} 1954 1955void * 1956ClangASTContext::CreateFunctionType (void *result_type, void **args, unsigned num_args, bool isVariadic, unsigned TypeQuals) 1957{ 1958 ASTContext *ast_context = getASTContext(); 1959 assert (ast_context != NULL); 1960 std::vector<QualType> qual_type_args; 1961 for (unsigned i=0; i<num_args; ++i) 1962 qual_type_args.push_back (QualType::getFromOpaquePtr(args[i])); 1963 1964 // TODO: Detect calling convention in DWARF? 1965 return ast_context->getFunctionType(QualType::getFromOpaquePtr(result_type), 1966 qual_type_args.data(), 1967 qual_type_args.size(), 1968 isVariadic, 1969 TypeQuals, 1970 false, // hasExceptionSpec 1971 false, // hasAnyExceptionSpec, 1972 0, // NumExs 1973 0, // const QualType *ExArray 1974 FunctionType::ExtInfo ()).getAsOpaquePtr(); // NoReturn); 1975} 1976 1977ParmVarDecl * 1978ClangASTContext::CreateParmeterDeclaration (const char *name, void * return_type, int storage) 1979{ 1980 ASTContext *ast_context = getASTContext(); 1981 assert (ast_context != NULL); 1982 return ParmVarDecl::Create(*ast_context, 1983 ast_context->getTranslationUnitDecl(), 1984 SourceLocation(), 1985 name && name[0] ? &ast_context->Idents.get(name) : NULL, 1986 QualType::getFromOpaquePtr(return_type), 1987 NULL, 1988 (VarDecl::StorageClass)storage, 1989 (VarDecl::StorageClass)storage, 1990 0); 1991} 1992 1993void 1994ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params) 1995{ 1996 if (function_decl) 1997 function_decl->setParams (params, num_params); 1998} 1999 2000 2001#pragma mark Array Types 2002 2003void * 2004ClangASTContext::CreateArrayType (void *element_type, size_t element_count, uint32_t bit_stride) 2005{ 2006 if (element_type) 2007 { 2008 ASTContext *ast_context = getASTContext(); 2009 assert (ast_context != NULL); 2010 llvm::APInt ap_element_count (64, element_count); 2011 return ast_context->getConstantArrayType(QualType::getFromOpaquePtr(element_type), 2012 ap_element_count, 2013 ArrayType::Normal, 2014 0).getAsOpaquePtr(); // ElemQuals 2015 } 2016 return NULL; 2017} 2018 2019 2020#pragma mark TagDecl 2021 2022bool 2023ClangASTContext::StartTagDeclarationDefinition (void *clang_type) 2024{ 2025 if (clang_type) 2026 { 2027 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2028 Type *t = qual_type.getTypePtr(); 2029 if (t) 2030 { 2031 TagType *tag_type = dyn_cast<TagType>(t); 2032 if (tag_type) 2033 { 2034 TagDecl *tag_decl = tag_type->getDecl(); 2035 if (tag_decl) 2036 { 2037 tag_decl->startDefinition(); 2038 return true; 2039 } 2040 } 2041 } 2042 } 2043 return false; 2044} 2045 2046bool 2047ClangASTContext::CompleteTagDeclarationDefinition (void *clang_type) 2048{ 2049 if (clang_type) 2050 { 2051 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2052 Type *t = qual_type.getTypePtr(); 2053 if (t) 2054 { 2055 TagType *tag_type = dyn_cast<TagType>(t); 2056 if (tag_type) 2057 { 2058 TagDecl *tag_decl = tag_type->getDecl(); 2059 if (tag_decl) 2060 { 2061 tag_decl->completeDefinition(); 2062 return true; 2063 } 2064 } 2065 } 2066 } 2067 return false; 2068} 2069 2070 2071#pragma mark Enumeration Types 2072 2073void * 2074ClangASTContext::CreateEnumerationType (const Declaration &decl, const char *name) 2075{ 2076 // TODO: Do something intelligent with the Declaration object passed in 2077 // like maybe filling in the SourceLocation with it... 2078 ASTContext *ast_context = getASTContext(); 2079 assert (ast_context != NULL); 2080 EnumDecl *enum_decl = EnumDecl::Create(*ast_context, 2081 ast_context->getTranslationUnitDecl(), 2082 SourceLocation(), 2083 name && name[0] ? &ast_context->Idents.get(name) : NULL, 2084 SourceLocation(), 2085 NULL); 2086 if (enum_decl) 2087 return ast_context->getTagDeclType(enum_decl).getAsOpaquePtr(); 2088 return NULL; 2089} 2090 2091bool 2092ClangASTContext::AddEnumerationValueToEnumerationType 2093( 2094 void *enum_clang_type, 2095 void *enumerator_clang_type, 2096 const Declaration &decl, 2097 const char *name, 2098 int64_t enum_value, 2099 uint32_t enum_value_bit_size 2100) 2101{ 2102 if (enum_clang_type && enumerator_clang_type && name) 2103 { 2104 // TODO: Do something intelligent with the Declaration object passed in 2105 // like maybe filling in the SourceLocation with it... 2106 ASTContext *ast_context = getASTContext(); 2107 IdentifierTable *identifier_table = getIdentifierTable(); 2108 2109 assert (ast_context != NULL); 2110 assert (identifier_table != NULL); 2111 QualType enum_qual_type (QualType::getFromOpaquePtr(enum_clang_type)); 2112 2113 Type *clang_type = enum_qual_type.getTypePtr(); 2114 if (clang_type) 2115 { 2116 const EnumType *enum_type = dyn_cast<EnumType>(clang_type); 2117 2118 if (enum_type) 2119 { 2120 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, false); 2121 enum_llvm_apsint = enum_value; 2122 EnumConstantDecl *enumerator_decl = 2123 EnumConstantDecl::Create(*ast_context, 2124 enum_type->getDecl(), 2125 SourceLocation(), 2126 name ? &identifier_table->get(name) : NULL, // Identifier 2127 QualType::getFromOpaquePtr(enumerator_clang_type), 2128 NULL, 2129 enum_llvm_apsint); 2130 2131 if (enumerator_decl) 2132 { 2133 enum_type->getDecl()->addDecl(enumerator_decl); 2134 return true; 2135 } 2136 } 2137 } 2138 } 2139 return false; 2140} 2141 2142#pragma mark Pointers & References 2143 2144void * 2145ClangASTContext::CreatePointerType (void *clang_type) 2146{ 2147 if (clang_type) 2148 return getASTContext()->getPointerType(QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr(); 2149 return NULL; 2150} 2151 2152void * 2153ClangASTContext::CreateLValueReferenceType (void *clang_type) 2154{ 2155 if (clang_type) 2156 return getASTContext()->getLValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr(); 2157 return NULL; 2158} 2159 2160void * 2161ClangASTContext::CreateRValueReferenceType (void *clang_type) 2162{ 2163 if (clang_type) 2164 return getASTContext()->getRValueReferenceType (QualType::getFromOpaquePtr(clang_type)).getAsOpaquePtr(); 2165 return NULL; 2166} 2167 2168void * 2169ClangASTContext::CreateMemberPointerType (void * clang_pointee_type, void * clang_class_type) 2170{ 2171 if (clang_pointee_type && clang_pointee_type) 2172 return getASTContext()->getMemberPointerType(QualType::getFromOpaquePtr(clang_pointee_type), 2173 QualType::getFromOpaquePtr(clang_class_type).getTypePtr()).getAsOpaquePtr(); 2174 return NULL; 2175} 2176 2177size_t 2178ClangASTContext::GetPointerBitSize () 2179{ 2180 ASTContext *ast_context = getASTContext(); 2181 return ast_context->getTypeSize(ast_context->VoidPtrTy); 2182} 2183 2184bool 2185ClangASTContext::IsPointerOrReferenceType (void *clang_type, void **target_type) 2186{ 2187 if (clang_type == NULL) 2188 return false; 2189 2190 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2191 switch (qual_type->getTypeClass()) 2192 { 2193 case Type::ObjCObjectPointer: 2194 if (target_type) 2195 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 2196 return true; 2197 case Type::BlockPointer: 2198 if (target_type) 2199 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 2200 return true; 2201 case Type::Pointer: 2202 if (target_type) 2203 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 2204 return true; 2205 case Type::MemberPointer: 2206 if (target_type) 2207 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 2208 return true; 2209 case Type::LValueReference: 2210 if (target_type) 2211 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr(); 2212 return true; 2213 case Type::RValueReference: 2214 if (target_type) 2215 *target_type = cast<LValueReferenceType>(qual_type)->desugar().getAsOpaquePtr(); 2216 return true; 2217 case Type::Typedef: 2218 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr()); 2219 default: 2220 break; 2221 } 2222 return false; 2223} 2224 2225size_t 2226ClangASTContext::GetTypeBitSize (clang::ASTContext *ast_context, void *clang_type) 2227{ 2228 if (clang_type) 2229 return ast_context->getTypeSize(QualType::getFromOpaquePtr(clang_type)); 2230 return 0; 2231} 2232 2233size_t 2234ClangASTContext::GetTypeBitAlign (clang::ASTContext *ast_context, void *clang_type) 2235{ 2236 if (clang_type) 2237 return ast_context->getTypeAlign(QualType::getFromOpaquePtr(clang_type)); 2238 return 0; 2239} 2240 2241bool 2242ClangASTContext::IsIntegerType (void * clang_type, bool &is_signed) 2243{ 2244 if (!clang_type) 2245 return false; 2246 2247 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2248 const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()); 2249 2250 if (builtin_type) 2251 { 2252 if (builtin_type->isInteger()) 2253 is_signed = builtin_type->isSignedInteger(); 2254 2255 return true; 2256 } 2257 2258 return false; 2259} 2260 2261bool 2262ClangASTContext::IsPointerType (void *clang_type, void **target_type) 2263{ 2264 if (clang_type) 2265 { 2266 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2267 switch (qual_type->getTypeClass()) 2268 { 2269 case Type::ObjCObjectPointer: 2270 if (target_type) 2271 *target_type = cast<ObjCObjectPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 2272 return true; 2273 case Type::BlockPointer: 2274 if (target_type) 2275 *target_type = cast<BlockPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 2276 return true; 2277 case Type::Pointer: 2278 if (target_type) 2279 *target_type = cast<PointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 2280 return true; 2281 case Type::MemberPointer: 2282 if (target_type) 2283 *target_type = cast<MemberPointerType>(qual_type)->getPointeeType().getAsOpaquePtr(); 2284 return true; 2285 case Type::Typedef: 2286 return ClangASTContext::IsPointerOrReferenceType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), target_type); 2287 default: 2288 break; 2289 } 2290 } 2291 return false; 2292} 2293 2294bool 2295ClangASTContext::IsFloatingPointType (void *clang_type, uint32_t &count, bool &is_complex) 2296{ 2297 if (clang_type) 2298 { 2299 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2300 2301 if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal())) 2302 { 2303 clang::BuiltinType::Kind kind = BT->getKind(); 2304 if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble) 2305 { 2306 count = 1; 2307 is_complex = false; 2308 return true; 2309 } 2310 } 2311 else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal())) 2312 { 2313 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, is_complex)) 2314 { 2315 count = 2; 2316 is_complex = true; 2317 return true; 2318 } 2319 } 2320 else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal())) 2321 { 2322 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, is_complex)) 2323 { 2324 count = VT->getNumElements(); 2325 is_complex = false; 2326 return true; 2327 } 2328 } 2329 } 2330 return false; 2331} 2332 2333 2334bool 2335ClangASTContext::IsCStringType (void *clang_type, uint32_t &length) 2336{ 2337 if (clang_type) 2338 { 2339 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2340 switch (qual_type->getTypeClass()) 2341 { 2342 case Type::ConstantArray: 2343 { 2344 ConstantArrayType *array = cast<ConstantArrayType>(qual_type.getTypePtr()); 2345 QualType element_qual_type = array->getElementType(); 2346 Type *canonical_type = element_qual_type->getCanonicalTypeInternal().getTypePtr(); 2347 if (canonical_type && canonical_type->isCharType()) 2348 { 2349 // We know the size of the array and it could be a C string 2350 // since it is an array of characters 2351 length = array->getSize().getLimitedValue(); 2352 return true; 2353 } 2354 } 2355 break; 2356 2357 case Type::Pointer: 2358 { 2359 PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr()); 2360 Type *pointee_type_ptr = pointer_type->getPointeeType().getTypePtr(); 2361 if (pointee_type_ptr) 2362 { 2363 Type *canonical_type_ptr = pointee_type_ptr->getCanonicalTypeInternal().getTypePtr(); 2364 length = 0; // No length info, read until a NULL terminator is received 2365 if (canonical_type_ptr) 2366 return canonical_type_ptr->isCharType(); 2367 else 2368 return pointee_type_ptr->isCharType(); 2369 } 2370 } 2371 break; 2372 2373 case Type::Typedef: 2374 return ClangASTContext::IsCStringType (cast<TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), length); 2375 2376 case Type::LValueReference: 2377 case Type::RValueReference: 2378 { 2379 ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr()); 2380 Type *pointee_type_ptr = reference_type->getPointeeType().getTypePtr(); 2381 if (pointee_type_ptr) 2382 { 2383 Type *canonical_type_ptr = pointee_type_ptr->getCanonicalTypeInternal().getTypePtr(); 2384 length = 0; // No length info, read until a NULL terminator is received 2385 if (canonical_type_ptr) 2386 return canonical_type_ptr->isCharType(); 2387 else 2388 return pointee_type_ptr->isCharType(); 2389 } 2390 } 2391 break; 2392 } 2393 } 2394 return false; 2395} 2396 2397bool 2398ClangASTContext::IsArrayType (void * clang_type, void **member_type, uint64_t *size) 2399{ 2400 if (!clang_type) 2401 return false; 2402 2403 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2404 2405 switch (qual_type->getTypeClass()) 2406 { 2407 case Type::ConstantArray: 2408 if (member_type) 2409 *member_type = cast<ConstantArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 2410 if (size) 2411 *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULONG_LONG_MAX); 2412 return true; 2413 case Type::IncompleteArray: 2414 if (member_type) 2415 *member_type = cast<IncompleteArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 2416 if (size) 2417 *size = 0; 2418 return true; 2419 case Type::VariableArray: 2420 if (member_type) 2421 *member_type = cast<VariableArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 2422 if (size) 2423 *size = 0; 2424 case Type::DependentSizedArray: 2425 if (member_type) 2426 *member_type = cast<DependentSizedArrayType>(qual_type)->getElementType().getAsOpaquePtr(); 2427 if (size) 2428 *size = 0; 2429 return true; 2430 } 2431 return false; 2432} 2433 2434 2435#pragma mark Typedefs 2436 2437void * 2438ClangASTContext::CreateTypedefType (const char *name, void *clang_type, DeclContext *decl_ctx) 2439{ 2440 if (clang_type) 2441 { 2442 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2443 ASTContext *ast_context = getASTContext(); 2444 IdentifierTable *identifier_table = getIdentifierTable(); 2445 assert (ast_context != NULL); 2446 assert (identifier_table != NULL); 2447 if (decl_ctx == NULL) 2448 decl_ctx = ast_context->getTranslationUnitDecl(); 2449 TypedefDecl *decl = TypedefDecl::Create(*ast_context, 2450 decl_ctx, 2451 SourceLocation(), 2452 name ? &identifier_table->get(name) : NULL, // Identifier 2453 ast_context->CreateTypeSourceInfo(qual_type)); 2454 2455 // Get a uniqued QualType for the typedef decl type 2456 return ast_context->getTypedefType (decl).getAsOpaquePtr(); 2457 } 2458 return NULL; 2459} 2460 2461 2462std::string 2463ClangASTContext::GetTypeName (void *opaque_qual_type) 2464{ 2465 std::string return_name; 2466 2467 clang::QualType qual_type(clang::QualType::getFromOpaquePtr(opaque_qual_type)); 2468 2469 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>(); 2470 if (typedef_type) 2471 { 2472 const clang::TypedefDecl *typedef_decl = typedef_type->getDecl(); 2473 return_name = typedef_decl->getQualifiedNameAsString(); 2474 } 2475 else 2476 { 2477 return_name = qual_type.getAsString(); 2478 } 2479 2480 return return_name; 2481} 2482 2483// Disable this for now since I can't seem to get a nicely formatted float 2484// out of the APFloat class without just getting the float, double or quad 2485// and then using a formatted print on it which defeats the purpose. We ideally 2486// would like to get perfect string values for any kind of float semantics 2487// so we can support remote targets. The code below also requires a patch to 2488// llvm::APInt. 2489//bool 2490//ClangASTContext::ConvertFloatValueToString (ASTContext *ast_context, void *clang_type, const uint8_t* bytes, size_t byte_size, int apint_byte_order, std::string &float_str) 2491//{ 2492// uint32_t count = 0; 2493// bool is_complex = false; 2494// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex)) 2495// { 2496// unsigned num_bytes_per_float = byte_size / count; 2497// unsigned num_bits_per_float = num_bytes_per_float * 8; 2498// 2499// float_str.clear(); 2500// uint32_t i; 2501// for (i=0; i<count; i++) 2502// { 2503// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order); 2504// bool is_ieee = false; 2505// APFloat ap_float(ap_int, is_ieee); 2506// char s[1024]; 2507// unsigned int hex_digits = 0; 2508// bool upper_case = false; 2509// 2510// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0) 2511// { 2512// if (i > 0) 2513// float_str.append(", "); 2514// float_str.append(s); 2515// if (i == 1 && is_complex) 2516// float_str.append(1, 'i'); 2517// } 2518// } 2519// return !float_str.empty(); 2520// } 2521// return false; 2522//} 2523 2524size_t 2525ClangASTContext::ConvertStringToFloatValue (ASTContext *ast_context, void *clang_type, const char *s, uint8_t *dst, size_t dst_size) 2526{ 2527 if (clang_type) 2528 { 2529 QualType qual_type (QualType::getFromOpaquePtr(clang_type)); 2530 uint32_t count = 0; 2531 bool is_complex = false; 2532 if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex)) 2533 { 2534 // TODO: handle complex and vector types 2535 if (count != 1) 2536 return false; 2537 2538 StringRef s_sref(s); 2539 APFloat ap_float(ast_context->getFloatTypeSemantics(qual_type), s_sref); 2540 2541 const uint64_t bit_size = ast_context->getTypeSize (qual_type); 2542 const uint64_t byte_size = bit_size / 8; 2543 if (dst_size >= byte_size) 2544 { 2545 if (bit_size == sizeof(float)*8) 2546 { 2547 float float32 = ap_float.convertToFloat(); 2548 ::memcpy (dst, &float32, byte_size); 2549 return byte_size; 2550 } 2551 else if (bit_size >= 64) 2552 { 2553 llvm::APInt ap_int(ap_float.bitcastToAPInt()); 2554 ::memcpy (dst, ap_int.getRawData(), byte_size); 2555 return byte_size; 2556 } 2557 } 2558 } 2559 } 2560 return 0; 2561} 2562