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