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 18 // Clang headers like to use NDEBUG inside of them to enable/disable debug 19 // related features using "#ifndef NDEBUG" preprocessor blocks to do one thing 20 // or another. This is bad because it means that if clang was built in release 21 // mode, it assumes that you are building in release mode which is not always 22 // the case. You can end up with functions that are defined as empty in header 23 // files when NDEBUG is not defined, and this can cause link errors with the 24 // clang .a files that you have since you might be missing functions in the .a 25 // file. So we have to define NDEBUG when including clang headers to avoid any 26 // mismatches. This is covered by rdar://problem/8691220 27 28 #if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF) 29 #define LLDB_DEFINED_NDEBUG_FOR_CLANG 30 #define NDEBUG 31 // Need to include assert.h so it is as clang would expect it to be (disabled) 32 #include <assert.h> 33 #endif 34 35 #include "clang/AST/ASTContext.h" 36 #include "clang/AST/ASTImporter.h" 37 #include "clang/AST/Attr.h" 38 #include "clang/AST/CXXInheritance.h" 39 #include "clang/AST/DeclObjC.h" 40 #include "clang/AST/DeclTemplate.h" 41 #include "clang/AST/RecordLayout.h" 42 #include "clang/AST/Type.h" 43 #include "clang/Basic/Builtins.h" 44 #include "clang/Basic/Diagnostic.h" 45 #include "clang/Basic/FileManager.h" 46 #include "clang/Basic/FileSystemOptions.h" 47 #include "clang/Basic/SourceManager.h" 48 #include "clang/Basic/TargetInfo.h" 49 #include "clang/Basic/TargetOptions.h" 50 #include "clang/Frontend/FrontendOptions.h" 51 #include "clang/Frontend/LangStandard.h" 52 53 #ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG 54 #undef NDEBUG 55 #undef LLDB_DEFINED_NDEBUG_FOR_CLANG 56 // Need to re-include assert.h so it is as _we_ would expect it to be (enabled) 57 #include <assert.h> 58 #endif 59 60 #include "lldb/Core/ArchSpec.h" 61 #include "lldb/Core/dwarf.h" 62 #include "lldb/Core/Flags.h" 63 #include "lldb/Core/Log.h" 64 #include "lldb/Core/RegularExpression.h" 65 #include "lldb/Core/UniqueCStringMap.h" 66 #include "lldb/Expression/ASTDumper.h" 67 #include "lldb/Symbol/ClangExternalASTSourceCommon.h" 68 #include "lldb/Symbol/VerifyDecl.h" 69 #include "lldb/Target/ExecutionContext.h" 70 #include "lldb/Target/Process.h" 71 #include "lldb/Target/ObjCLanguageRuntime.h" 72 73 #include <stdio.h> 74 75 #include <mutex> 76 77 using namespace lldb; 78 using namespace lldb_private; 79 using namespace llvm; 80 using namespace clang; 81 82 typedef llvm::DenseMap<clang::ASTContext *, ClangASTContext*> ClangASTMap; 83 84 static ClangASTMap & 85 GetASTMap() 86 { 87 static ClangASTMap g_map; 88 return g_map; 89 } 90 91 92 clang::AccessSpecifier 93 ClangASTContext::ConvertAccessTypeToAccessSpecifier (AccessType access) 94 { 95 switch (access) 96 { 97 default: break; 98 case eAccessNone: return AS_none; 99 case eAccessPublic: return AS_public; 100 case eAccessPrivate: return AS_private; 101 case eAccessProtected: return AS_protected; 102 } 103 return AS_none; 104 } 105 106 107 static void 108 ParseLangArgs 109 ( 110 LangOptions &Opts, 111 InputKind IK 112 ) 113 { 114 // FIXME: Cleanup per-file based stuff. 115 116 // Set some properties which depend solely on the input kind; it would be nice 117 // to move these to the language standard, and have the driver resolve the 118 // input kind + language standard. 119 if (IK == IK_Asm) { 120 Opts.AsmPreprocessor = 1; 121 } else if (IK == IK_ObjC || 122 IK == IK_ObjCXX || 123 IK == IK_PreprocessedObjC || 124 IK == IK_PreprocessedObjCXX) { 125 Opts.ObjC1 = Opts.ObjC2 = 1; 126 } 127 128 LangStandard::Kind LangStd = LangStandard::lang_unspecified; 129 130 if (LangStd == LangStandard::lang_unspecified) { 131 // Based on the base language, pick one. 132 switch (IK) { 133 case IK_None: 134 case IK_AST: 135 case IK_LLVM_IR: 136 assert (!"Invalid input kind!"); 137 case IK_OpenCL: 138 LangStd = LangStandard::lang_opencl; 139 break; 140 case IK_CUDA: 141 LangStd = LangStandard::lang_cuda; 142 break; 143 case IK_Asm: 144 case IK_C: 145 case IK_PreprocessedC: 146 case IK_ObjC: 147 case IK_PreprocessedObjC: 148 LangStd = LangStandard::lang_gnu99; 149 break; 150 case IK_CXX: 151 case IK_PreprocessedCXX: 152 case IK_ObjCXX: 153 case IK_PreprocessedObjCXX: 154 LangStd = LangStandard::lang_gnucxx98; 155 break; 156 } 157 } 158 159 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd); 160 Opts.LineComment = Std.hasLineComments(); 161 Opts.C99 = Std.isC99(); 162 Opts.CPlusPlus = Std.isCPlusPlus(); 163 Opts.CPlusPlus11 = Std.isCPlusPlus11(); 164 Opts.Digraphs = Std.hasDigraphs(); 165 Opts.GNUMode = Std.isGNUMode(); 166 Opts.GNUInline = !Std.isC99(); 167 Opts.HexFloats = Std.hasHexFloats(); 168 Opts.ImplicitInt = Std.hasImplicitInt(); 169 170 Opts.WChar = true; 171 172 // OpenCL has some additional defaults. 173 if (LangStd == LangStandard::lang_opencl) { 174 Opts.OpenCL = 1; 175 Opts.AltiVec = 1; 176 Opts.CXXOperatorNames = 1; 177 Opts.LaxVectorConversions = 1; 178 } 179 180 // OpenCL and C++ both have bool, true, false keywords. 181 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus; 182 183 // if (Opts.CPlusPlus) 184 // Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names); 185 // 186 // if (Args.hasArg(OPT_fobjc_gc_only)) 187 // Opts.setGCMode(LangOptions::GCOnly); 188 // else if (Args.hasArg(OPT_fobjc_gc)) 189 // Opts.setGCMode(LangOptions::HybridGC); 190 // 191 // if (Args.hasArg(OPT_print_ivar_layout)) 192 // Opts.ObjCGCBitmapPrint = 1; 193 // 194 // if (Args.hasArg(OPT_faltivec)) 195 // Opts.AltiVec = 1; 196 // 197 // if (Args.hasArg(OPT_pthread)) 198 // Opts.POSIXThreads = 1; 199 // 200 // llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility, 201 // "default"); 202 // if (Vis == "default") 203 Opts.setValueVisibilityMode(DefaultVisibility); 204 // else if (Vis == "hidden") 205 // Opts.setVisibilityMode(LangOptions::Hidden); 206 // else if (Vis == "protected") 207 // Opts.setVisibilityMode(LangOptions::Protected); 208 // else 209 // Diags.Report(diag::err_drv_invalid_value) 210 // << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis; 211 212 // Opts.OverflowChecking = Args.hasArg(OPT_ftrapv); 213 214 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs 215 // is specified, or -std is set to a conforming mode. 216 Opts.Trigraphs = !Opts.GNUMode; 217 // if (Args.hasArg(OPT_trigraphs)) 218 // Opts.Trigraphs = 1; 219 // 220 // Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers, 221 // OPT_fno_dollars_in_identifiers, 222 // !Opts.AsmPreprocessor); 223 // Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings); 224 // Opts.Microsoft = Args.hasArg(OPT_fms_extensions); 225 // Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings); 226 // if (Args.hasArg(OPT_fno_lax_vector_conversions)) 227 // Opts.LaxVectorConversions = 0; 228 // Opts.Exceptions = Args.hasArg(OPT_fexceptions); 229 // Opts.RTTI = !Args.hasArg(OPT_fno_rtti); 230 // Opts.Blocks = Args.hasArg(OPT_fblocks); 231 // Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char); 232 // Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar); 233 // Opts.Freestanding = Args.hasArg(OPT_ffreestanding); 234 // Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding; 235 // Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new); 236 // Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions); 237 // Opts.AccessControl = Args.hasArg(OPT_faccess_control); 238 // Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors); 239 // Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno); 240 // Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99, 241 // Diags); 242 // Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime); 243 // Opts.ObjCConstantStringClass = getLastArgValue(Args, 244 // OPT_fconstant_string_class); 245 // Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi); 246 // Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior); 247 // Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls); 248 // Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags); 249 // Opts.Static = Args.hasArg(OPT_static_define); 250 Opts.OptimizeSize = 0; 251 252 // FIXME: Eliminate this dependency. 253 // unsigned Opt = 254 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags); 255 // Opts.Optimize = Opt != 0; 256 unsigned Opt = 0; 257 258 // This is the __NO_INLINE__ define, which just depends on things like the 259 // optimization level and -fno-inline, not actually whether the backend has 260 // inlining enabled. 261 // 262 // FIXME: This is affected by other options (-fno-inline). 263 Opts.NoInlineDefine = !Opt; 264 265 // unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags); 266 // switch (SSP) { 267 // default: 268 // Diags.Report(diag::err_drv_invalid_value) 269 // << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP; 270 // break; 271 // case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break; 272 // case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break; 273 // case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break; 274 // } 275 } 276 277 278 ClangASTContext::ClangASTContext (const char *target_triple) : 279 m_target_triple(), 280 m_ast_ap(), 281 m_language_options_ap(), 282 m_source_manager_ap(), 283 m_diagnostics_engine_ap(), 284 m_target_options_rp(), 285 m_target_info_ap(), 286 m_identifier_table_ap(), 287 m_selector_table_ap(), 288 m_builtins_ap(), 289 m_callback_tag_decl (nullptr), 290 m_callback_objc_decl (nullptr), 291 m_callback_baton (nullptr), 292 m_pointer_byte_size (0) 293 294 { 295 if (target_triple && target_triple[0]) 296 SetTargetTriple (target_triple); 297 } 298 299 //---------------------------------------------------------------------- 300 // Destructor 301 //---------------------------------------------------------------------- 302 ClangASTContext::~ClangASTContext() 303 { 304 if (m_ast_ap.get()) 305 { 306 GetASTMap().erase(m_ast_ap.get()); 307 } 308 309 m_builtins_ap.reset(); 310 m_selector_table_ap.reset(); 311 m_identifier_table_ap.reset(); 312 m_target_info_ap.reset(); 313 m_target_options_rp.reset(); 314 m_diagnostics_engine_ap.reset(); 315 m_source_manager_ap.reset(); 316 m_language_options_ap.reset(); 317 m_ast_ap.reset(); 318 } 319 320 321 void 322 ClangASTContext::Clear() 323 { 324 m_ast_ap.reset(); 325 m_language_options_ap.reset(); 326 m_source_manager_ap.reset(); 327 m_diagnostics_engine_ap.reset(); 328 m_target_options_rp.reset(); 329 m_target_info_ap.reset(); 330 m_identifier_table_ap.reset(); 331 m_selector_table_ap.reset(); 332 m_builtins_ap.reset(); 333 m_pointer_byte_size = 0; 334 } 335 336 const char * 337 ClangASTContext::GetTargetTriple () 338 { 339 return m_target_triple.c_str(); 340 } 341 342 void 343 ClangASTContext::SetTargetTriple (const char *target_triple) 344 { 345 Clear(); 346 m_target_triple.assign(target_triple); 347 } 348 349 void 350 ClangASTContext::SetArchitecture (const ArchSpec &arch) 351 { 352 SetTargetTriple(arch.GetTriple().str().c_str()); 353 } 354 355 bool 356 ClangASTContext::HasExternalSource () 357 { 358 ASTContext *ast = getASTContext(); 359 if (ast) 360 return ast->getExternalSource () != nullptr; 361 return false; 362 } 363 364 void 365 ClangASTContext::SetExternalSource (llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) 366 { 367 ASTContext *ast = getASTContext(); 368 if (ast) 369 { 370 ast->setExternalSource (ast_source_ap); 371 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true); 372 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true); 373 } 374 } 375 376 void 377 ClangASTContext::RemoveExternalSource () 378 { 379 ASTContext *ast = getASTContext(); 380 381 if (ast) 382 { 383 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap; 384 ast->setExternalSource (empty_ast_source_ap); 385 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false); 386 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false); 387 } 388 } 389 390 391 392 ASTContext * 393 ClangASTContext::getASTContext() 394 { 395 if (m_ast_ap.get() == nullptr) 396 { 397 m_ast_ap.reset(new ASTContext (*getLanguageOptions(), 398 *getSourceManager(), 399 *getIdentifierTable(), 400 *getSelectorTable(), 401 *getBuiltinContext())); 402 m_ast_ap->InitBuiltinTypes(*getTargetInfo()); 403 404 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) 405 { 406 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage(); 407 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage(); 408 } 409 410 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false); 411 412 GetASTMap().insert(std::make_pair(m_ast_ap.get(), this)); 413 } 414 return m_ast_ap.get(); 415 } 416 417 ClangASTContext* 418 ClangASTContext::GetASTContext (clang::ASTContext* ast) 419 { 420 ClangASTContext *clang_ast = GetASTMap().lookup(ast); 421 return clang_ast; 422 } 423 424 Builtin::Context * 425 ClangASTContext::getBuiltinContext() 426 { 427 if (m_builtins_ap.get() == nullptr) 428 m_builtins_ap.reset (new Builtin::Context()); 429 return m_builtins_ap.get(); 430 } 431 432 IdentifierTable * 433 ClangASTContext::getIdentifierTable() 434 { 435 if (m_identifier_table_ap.get() == nullptr) 436 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), nullptr)); 437 return m_identifier_table_ap.get(); 438 } 439 440 LangOptions * 441 ClangASTContext::getLanguageOptions() 442 { 443 if (m_language_options_ap.get() == nullptr) 444 { 445 m_language_options_ap.reset(new LangOptions()); 446 ParseLangArgs(*m_language_options_ap, IK_ObjCXX); 447 // InitializeLangOptions(*m_language_options_ap, IK_ObjCXX); 448 } 449 return m_language_options_ap.get(); 450 } 451 452 SelectorTable * 453 ClangASTContext::getSelectorTable() 454 { 455 if (m_selector_table_ap.get() == nullptr) 456 m_selector_table_ap.reset (new SelectorTable()); 457 return m_selector_table_ap.get(); 458 } 459 460 clang::FileManager * 461 ClangASTContext::getFileManager() 462 { 463 if (m_file_manager_ap.get() == nullptr) 464 { 465 clang::FileSystemOptions file_system_options; 466 m_file_manager_ap.reset(new clang::FileManager(file_system_options)); 467 } 468 return m_file_manager_ap.get(); 469 } 470 471 clang::SourceManager * 472 ClangASTContext::getSourceManager() 473 { 474 if (m_source_manager_ap.get() == nullptr) 475 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager())); 476 return m_source_manager_ap.get(); 477 } 478 479 clang::DiagnosticsEngine * 480 ClangASTContext::getDiagnosticsEngine() 481 { 482 if (m_diagnostics_engine_ap.get() == nullptr) 483 { 484 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs()); 485 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions())); 486 } 487 return m_diagnostics_engine_ap.get(); 488 } 489 490 class NullDiagnosticConsumer : public DiagnosticConsumer 491 { 492 public: 493 NullDiagnosticConsumer () 494 { 495 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); 496 } 497 498 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const Diagnostic &info) 499 { 500 if (m_log) 501 { 502 llvm::SmallVector<char, 32> diag_str(10); 503 info.FormatDiagnostic(diag_str); 504 diag_str.push_back('\0'); 505 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data()); 506 } 507 } 508 509 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const 510 { 511 return new NullDiagnosticConsumer (); 512 } 513 private: 514 Log * m_log; 515 }; 516 517 DiagnosticConsumer * 518 ClangASTContext::getDiagnosticConsumer() 519 { 520 if (m_diagnostic_consumer_ap.get() == nullptr) 521 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer); 522 523 return m_diagnostic_consumer_ap.get(); 524 } 525 526 std::shared_ptr<TargetOptions> & 527 ClangASTContext::getTargetOptions() { 528 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) 529 { 530 m_target_options_rp = std::make_shared<TargetOptions>(); 531 if (m_target_options_rp.get() != nullptr) 532 m_target_options_rp->Triple = m_target_triple; 533 } 534 return m_target_options_rp; 535 } 536 537 538 TargetInfo * 539 ClangASTContext::getTargetInfo() 540 { 541 // target_triple should be something like "x86_64-apple-macosx" 542 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty()) 543 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), getTargetOptions())); 544 return m_target_info_ap.get(); 545 } 546 547 #pragma mark Basic Types 548 549 static inline bool 550 QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type) 551 { 552 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type); 553 if (qual_type_bit_size == bit_size) 554 return true; 555 return false; 556 } 557 ClangASTType 558 ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size) 559 { 560 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (getASTContext(), encoding, bit_size); 561 } 562 563 ClangASTType 564 ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size) 565 { 566 if (!ast) 567 return ClangASTType(); 568 569 switch (encoding) 570 { 571 case eEncodingInvalid: 572 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy)) 573 return ClangASTType (ast, ast->VoidPtrTy.getAsOpaquePtr()); 574 break; 575 576 case eEncodingUint: 577 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy)) 578 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr()); 579 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy)) 580 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr()); 581 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy)) 582 return ClangASTType (ast, ast->UnsignedIntTy.getAsOpaquePtr()); 583 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy)) 584 return ClangASTType (ast, ast->UnsignedLongTy.getAsOpaquePtr()); 585 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy)) 586 return ClangASTType (ast, ast->UnsignedLongLongTy.getAsOpaquePtr()); 587 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty)) 588 return ClangASTType (ast, ast->UnsignedInt128Ty.getAsOpaquePtr()); 589 break; 590 591 case eEncodingSint: 592 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy)) 593 return ClangASTType (ast, ast->CharTy.getAsOpaquePtr()); 594 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy)) 595 return ClangASTType (ast, ast->ShortTy.getAsOpaquePtr()); 596 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy)) 597 return ClangASTType (ast, ast->IntTy.getAsOpaquePtr()); 598 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy)) 599 return ClangASTType (ast, ast->LongTy.getAsOpaquePtr()); 600 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy)) 601 return ClangASTType (ast, ast->LongLongTy.getAsOpaquePtr()); 602 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty)) 603 return ClangASTType (ast, ast->Int128Ty.getAsOpaquePtr()); 604 break; 605 606 case eEncodingIEEE754: 607 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy)) 608 return ClangASTType (ast, ast->FloatTy.getAsOpaquePtr()); 609 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy)) 610 return ClangASTType (ast, ast->DoubleTy.getAsOpaquePtr()); 611 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy)) 612 return ClangASTType (ast, ast->LongDoubleTy.getAsOpaquePtr()); 613 break; 614 615 case eEncodingVector: 616 // Sanity check that bit_size is a multiple of 8's. 617 if (bit_size && !(bit_size & 0x7u)) 618 return ClangASTType (ast, ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8).getAsOpaquePtr()); 619 break; 620 } 621 622 return ClangASTType(); 623 } 624 625 626 627 lldb::BasicType 628 ClangASTContext::GetBasicTypeEnumeration (const ConstString &name) 629 { 630 if (name) 631 { 632 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap; 633 static TypeNameToBasicTypeMap g_type_map; 634 static std::once_flag g_once_flag; 635 std::call_once(g_once_flag, [](){ 636 // "void" 637 g_type_map.Append(ConstString("void").GetCString(), eBasicTypeVoid); 638 639 // "char" 640 g_type_map.Append(ConstString("char").GetCString(), eBasicTypeChar); 641 g_type_map.Append(ConstString("signed char").GetCString(), eBasicTypeSignedChar); 642 g_type_map.Append(ConstString("unsigned char").GetCString(), eBasicTypeUnsignedChar); 643 g_type_map.Append(ConstString("wchar_t").GetCString(), eBasicTypeWChar); 644 g_type_map.Append(ConstString("signed wchar_t").GetCString(), eBasicTypeSignedWChar); 645 g_type_map.Append(ConstString("unsigned wchar_t").GetCString(), eBasicTypeUnsignedWChar); 646 // "short" 647 g_type_map.Append(ConstString("short").GetCString(), eBasicTypeShort); 648 g_type_map.Append(ConstString("short int").GetCString(), eBasicTypeShort); 649 g_type_map.Append(ConstString("unsigned short").GetCString(), eBasicTypeUnsignedShort); 650 g_type_map.Append(ConstString("unsigned short int").GetCString(), eBasicTypeUnsignedShort); 651 652 // "int" 653 g_type_map.Append(ConstString("int").GetCString(), eBasicTypeInt); 654 g_type_map.Append(ConstString("signed int").GetCString(), eBasicTypeInt); 655 g_type_map.Append(ConstString("unsigned int").GetCString(), eBasicTypeUnsignedInt); 656 g_type_map.Append(ConstString("unsigned").GetCString(), eBasicTypeUnsignedInt); 657 658 // "long" 659 g_type_map.Append(ConstString("long").GetCString(), eBasicTypeLong); 660 g_type_map.Append(ConstString("long int").GetCString(), eBasicTypeLong); 661 g_type_map.Append(ConstString("unsigned long").GetCString(), eBasicTypeUnsignedLong); 662 g_type_map.Append(ConstString("unsigned long int").GetCString(), eBasicTypeUnsignedLong); 663 664 // "long long" 665 g_type_map.Append(ConstString("long long").GetCString(), eBasicTypeLongLong); 666 g_type_map.Append(ConstString("long long int").GetCString(), eBasicTypeLongLong); 667 g_type_map.Append(ConstString("unsigned long long").GetCString(), eBasicTypeUnsignedLongLong); 668 g_type_map.Append(ConstString("unsigned long long int").GetCString(), eBasicTypeUnsignedLongLong); 669 670 // "int128" 671 g_type_map.Append(ConstString("__int128_t").GetCString(), eBasicTypeInt128); 672 g_type_map.Append(ConstString("__uint128_t").GetCString(), eBasicTypeUnsignedInt128); 673 674 // Miscellaneous 675 g_type_map.Append(ConstString("bool").GetCString(), eBasicTypeBool); 676 g_type_map.Append(ConstString("float").GetCString(), eBasicTypeFloat); 677 g_type_map.Append(ConstString("double").GetCString(), eBasicTypeDouble); 678 g_type_map.Append(ConstString("long double").GetCString(), eBasicTypeLongDouble); 679 g_type_map.Append(ConstString("id").GetCString(), eBasicTypeObjCID); 680 g_type_map.Append(ConstString("SEL").GetCString(), eBasicTypeObjCSel); 681 g_type_map.Append(ConstString("nullptr").GetCString(), eBasicTypeNullPtr); 682 g_type_map.Sort(); 683 }); 684 685 return g_type_map.Find(name.GetCString(), eBasicTypeInvalid); 686 } 687 return eBasicTypeInvalid; 688 } 689 690 ClangASTType 691 ClangASTContext::GetBasicType (ASTContext *ast, const ConstString &name) 692 { 693 if (ast) 694 { 695 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration (name); 696 return ClangASTContext::GetBasicType (ast, basic_type); 697 } 698 return ClangASTType(); 699 } 700 701 uint32_t 702 ClangASTContext::GetPointerByteSize () 703 { 704 if (m_pointer_byte_size == 0) 705 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid).GetPointerType().GetByteSize(); 706 return m_pointer_byte_size; 707 } 708 709 ClangASTType 710 ClangASTContext::GetBasicType (lldb::BasicType basic_type) 711 { 712 return GetBasicType (getASTContext(), basic_type); 713 } 714 715 ClangASTType 716 ClangASTContext::GetBasicType (ASTContext *ast, lldb::BasicType basic_type) 717 { 718 if (ast) 719 { 720 clang_type_t clang_type = nullptr; 721 722 switch (basic_type) 723 { 724 case eBasicTypeInvalid: 725 case eBasicTypeOther: 726 break; 727 case eBasicTypeVoid: 728 clang_type = ast->VoidTy.getAsOpaquePtr(); 729 break; 730 case eBasicTypeChar: 731 clang_type = ast->CharTy.getAsOpaquePtr(); 732 break; 733 case eBasicTypeSignedChar: 734 clang_type = ast->SignedCharTy.getAsOpaquePtr(); 735 break; 736 case eBasicTypeUnsignedChar: 737 clang_type = ast->UnsignedCharTy.getAsOpaquePtr(); 738 break; 739 case eBasicTypeWChar: 740 clang_type = ast->getWCharType().getAsOpaquePtr(); 741 break; 742 case eBasicTypeSignedWChar: 743 clang_type = ast->getSignedWCharType().getAsOpaquePtr(); 744 break; 745 case eBasicTypeUnsignedWChar: 746 clang_type = ast->getUnsignedWCharType().getAsOpaquePtr(); 747 break; 748 case eBasicTypeChar16: 749 clang_type = ast->Char16Ty.getAsOpaquePtr(); 750 break; 751 case eBasicTypeChar32: 752 clang_type = ast->Char32Ty.getAsOpaquePtr(); 753 break; 754 case eBasicTypeShort: 755 clang_type = ast->ShortTy.getAsOpaquePtr(); 756 break; 757 case eBasicTypeUnsignedShort: 758 clang_type = ast->UnsignedShortTy.getAsOpaquePtr(); 759 break; 760 case eBasicTypeInt: 761 clang_type = ast->IntTy.getAsOpaquePtr(); 762 break; 763 case eBasicTypeUnsignedInt: 764 clang_type = ast->UnsignedIntTy.getAsOpaquePtr(); 765 break; 766 case eBasicTypeLong: 767 clang_type = ast->LongTy.getAsOpaquePtr(); 768 break; 769 case eBasicTypeUnsignedLong: 770 clang_type = ast->UnsignedLongTy.getAsOpaquePtr(); 771 break; 772 case eBasicTypeLongLong: 773 clang_type = ast->LongLongTy.getAsOpaquePtr(); 774 break; 775 case eBasicTypeUnsignedLongLong: 776 clang_type = ast->UnsignedLongLongTy.getAsOpaquePtr(); 777 break; 778 case eBasicTypeInt128: 779 clang_type = ast->Int128Ty.getAsOpaquePtr(); 780 break; 781 case eBasicTypeUnsignedInt128: 782 clang_type = ast->UnsignedInt128Ty.getAsOpaquePtr(); 783 break; 784 case eBasicTypeBool: 785 clang_type = ast->BoolTy.getAsOpaquePtr(); 786 break; 787 case eBasicTypeHalf: 788 clang_type = ast->HalfTy.getAsOpaquePtr(); 789 break; 790 case eBasicTypeFloat: 791 clang_type = ast->FloatTy.getAsOpaquePtr(); 792 break; 793 case eBasicTypeDouble: 794 clang_type = ast->DoubleTy.getAsOpaquePtr(); 795 break; 796 case eBasicTypeLongDouble: 797 clang_type = ast->LongDoubleTy.getAsOpaquePtr(); 798 break; 799 case eBasicTypeFloatComplex: 800 clang_type = ast->FloatComplexTy.getAsOpaquePtr(); 801 break; 802 case eBasicTypeDoubleComplex: 803 clang_type = ast->DoubleComplexTy.getAsOpaquePtr(); 804 break; 805 case eBasicTypeLongDoubleComplex: 806 clang_type = ast->LongDoubleComplexTy.getAsOpaquePtr(); 807 break; 808 case eBasicTypeObjCID: 809 clang_type = ast->getObjCIdType().getAsOpaquePtr(); 810 break; 811 case eBasicTypeObjCClass: 812 clang_type = ast->getObjCClassType().getAsOpaquePtr(); 813 break; 814 case eBasicTypeObjCSel: 815 clang_type = ast->getObjCSelType().getAsOpaquePtr(); 816 break; 817 case eBasicTypeNullPtr: 818 clang_type = ast->NullPtrTy.getAsOpaquePtr(); 819 break; 820 } 821 822 if (clang_type) 823 return ClangASTType (ast, clang_type); 824 } 825 return ClangASTType(); 826 } 827 828 829 ClangASTType 830 ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size) 831 { 832 ASTContext *ast = getASTContext(); 833 834 #define streq(a,b) strcmp(a,b) == 0 835 assert (ast != nullptr); 836 if (ast) 837 { 838 switch (dw_ate) 839 { 840 default: 841 break; 842 843 case DW_ATE_address: 844 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy)) 845 return ClangASTType (ast, ast->VoidPtrTy.getAsOpaquePtr()); 846 break; 847 848 case DW_ATE_boolean: 849 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy)) 850 return ClangASTType (ast, ast->BoolTy.getAsOpaquePtr()); 851 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy)) 852 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr()); 853 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy)) 854 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr()); 855 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy)) 856 return ClangASTType (ast, ast->UnsignedIntTy.getAsOpaquePtr()); 857 break; 858 859 case DW_ATE_lo_user: 860 // This has been seen to mean DW_AT_complex_integer 861 if (type_name) 862 { 863 if (::strstr(type_name, "complex")) 864 { 865 ClangASTType complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2); 866 return ClangASTType (ast, ast->getComplexType (complex_int_clang_type.GetQualType()).getAsOpaquePtr()); 867 } 868 } 869 break; 870 871 case DW_ATE_complex_float: 872 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy)) 873 return ClangASTType (ast, ast->FloatComplexTy.getAsOpaquePtr()); 874 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy)) 875 return ClangASTType (ast, ast->DoubleComplexTy.getAsOpaquePtr()); 876 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy)) 877 return ClangASTType (ast, ast->LongDoubleComplexTy.getAsOpaquePtr()); 878 else 879 { 880 ClangASTType complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2); 881 return ClangASTType (ast, ast->getComplexType (complex_float_clang_type.GetQualType()).getAsOpaquePtr()); 882 } 883 break; 884 885 case DW_ATE_float: 886 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy)) 887 return ClangASTType (ast, ast->FloatTy.getAsOpaquePtr()); 888 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy)) 889 return ClangASTType (ast, ast->DoubleTy.getAsOpaquePtr()); 890 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy)) 891 return ClangASTType (ast, ast->LongDoubleTy.getAsOpaquePtr()); 892 break; 893 894 case DW_ATE_signed: 895 if (type_name) 896 { 897 if (streq(type_name, "wchar_t") && 898 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy)) 899 return ClangASTType (ast, ast->WCharTy.getAsOpaquePtr()); 900 if (streq(type_name, "void") && 901 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy)) 902 return ClangASTType (ast, ast->VoidTy.getAsOpaquePtr()); 903 if (strstr(type_name, "long long") && 904 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy)) 905 return ClangASTType (ast, ast->LongLongTy.getAsOpaquePtr()); 906 if (strstr(type_name, "long") && 907 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy)) 908 return ClangASTType (ast, ast->LongTy.getAsOpaquePtr()); 909 if (strstr(type_name, "short") && 910 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy)) 911 return ClangASTType (ast, ast->ShortTy.getAsOpaquePtr()); 912 if (strstr(type_name, "char")) 913 { 914 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy)) 915 return ClangASTType (ast, ast->CharTy.getAsOpaquePtr()); 916 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy)) 917 return ClangASTType (ast, ast->SignedCharTy.getAsOpaquePtr()); 918 } 919 if (strstr(type_name, "int")) 920 { 921 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy)) 922 return ClangASTType (ast, ast->IntTy.getAsOpaquePtr()); 923 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty)) 924 return ClangASTType (ast, ast->Int128Ty.getAsOpaquePtr()); 925 } 926 } 927 // We weren't able to match up a type name, just search by size 928 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy)) 929 return ClangASTType (ast, ast->CharTy.getAsOpaquePtr()); 930 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy)) 931 return ClangASTType (ast, ast->ShortTy.getAsOpaquePtr()); 932 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy)) 933 return ClangASTType (ast, ast->IntTy.getAsOpaquePtr()); 934 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy)) 935 return ClangASTType (ast, ast->LongTy.getAsOpaquePtr()); 936 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy)) 937 return ClangASTType (ast, ast->LongLongTy.getAsOpaquePtr()); 938 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty)) 939 return ClangASTType (ast, ast->Int128Ty.getAsOpaquePtr()); 940 break; 941 942 case DW_ATE_signed_char: 943 if (type_name) 944 { 945 if (streq(type_name, "signed char")) 946 { 947 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy)) 948 return ClangASTType (ast, ast->SignedCharTy.getAsOpaquePtr()); 949 } 950 } 951 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy)) 952 return ClangASTType (ast, ast->CharTy.getAsOpaquePtr()); 953 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy)) 954 return ClangASTType (ast, ast->SignedCharTy.getAsOpaquePtr()); 955 break; 956 957 case DW_ATE_unsigned: 958 if (type_name) 959 { 960 if (strstr(type_name, "long long")) 961 { 962 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy)) 963 return ClangASTType (ast, ast->UnsignedLongLongTy.getAsOpaquePtr()); 964 } 965 else if (strstr(type_name, "long")) 966 { 967 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy)) 968 return ClangASTType (ast, ast->UnsignedLongTy.getAsOpaquePtr()); 969 } 970 else if (strstr(type_name, "short")) 971 { 972 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy)) 973 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr()); 974 } 975 else if (strstr(type_name, "char")) 976 { 977 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy)) 978 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr()); 979 } 980 else if (strstr(type_name, "int")) 981 { 982 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy)) 983 return ClangASTType (ast, ast->UnsignedIntTy.getAsOpaquePtr()); 984 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty)) 985 return ClangASTType (ast, ast->UnsignedInt128Ty.getAsOpaquePtr()); 986 } 987 } 988 // We weren't able to match up a type name, just search by size 989 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy)) 990 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr()); 991 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy)) 992 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr()); 993 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy)) 994 return ClangASTType (ast, ast->UnsignedIntTy.getAsOpaquePtr()); 995 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy)) 996 return ClangASTType (ast, ast->UnsignedLongTy.getAsOpaquePtr()); 997 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy)) 998 return ClangASTType (ast, ast->UnsignedLongLongTy.getAsOpaquePtr()); 999 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty)) 1000 return ClangASTType (ast, ast->UnsignedInt128Ty.getAsOpaquePtr()); 1001 break; 1002 1003 case DW_ATE_unsigned_char: 1004 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy)) 1005 return ClangASTType (ast, ast->UnsignedCharTy.getAsOpaquePtr()); 1006 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy)) 1007 return ClangASTType (ast, ast->UnsignedShortTy.getAsOpaquePtr()); 1008 break; 1009 1010 case DW_ATE_imaginary_float: 1011 break; 1012 1013 case DW_ATE_UTF: 1014 if (type_name) 1015 { 1016 if (streq(type_name, "char16_t")) 1017 { 1018 return ClangASTType (ast, ast->Char16Ty.getAsOpaquePtr()); 1019 } 1020 else if (streq(type_name, "char32_t")) 1021 { 1022 return ClangASTType (ast, ast->Char32Ty.getAsOpaquePtr()); 1023 } 1024 } 1025 break; 1026 } 1027 } 1028 // This assert should fire for anything that we don't catch above so we know 1029 // to fix any issues we run into. 1030 if (type_name) 1031 { 1032 Host::SystemLog (Host::eSystemLogError, "error: need to add support for DW_TAG_base_type '%s' encoded with DW_ATE = 0x%x, bit_size = %u\n", type_name, dw_ate, bit_size); 1033 } 1034 else 1035 { 1036 Host::SystemLog (Host::eSystemLogError, "error: need to add support for DW_TAG_base_type encoded with DW_ATE = 0x%x, bit_size = %u\n", dw_ate, bit_size); 1037 } 1038 return ClangASTType (); 1039 } 1040 1041 ClangASTType 1042 ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) 1043 { 1044 if (ast) 1045 return ClangASTType (ast, ast->UnknownAnyTy.getAsOpaquePtr()); 1046 return ClangASTType(); 1047 } 1048 1049 ClangASTType 1050 ClangASTContext::GetCStringType (bool is_const) 1051 { 1052 ASTContext *ast = getASTContext(); 1053 QualType char_type(ast->CharTy); 1054 1055 if (is_const) 1056 char_type.addConst(); 1057 1058 return ClangASTType (ast, ast->getPointerType(char_type).getAsOpaquePtr()); 1059 } 1060 1061 clang::DeclContext * 1062 ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast) 1063 { 1064 return ast->getTranslationUnitDecl(); 1065 } 1066 1067 ClangASTType 1068 ClangASTContext::CopyType (ASTContext *dst_ast, 1069 ClangASTType src) 1070 { 1071 FileSystemOptions file_system_options; 1072 ASTContext *src_ast = src.GetASTContext(); 1073 FileManager file_manager (file_system_options); 1074 ASTImporter importer(*dst_ast, file_manager, 1075 *src_ast, file_manager, 1076 false); 1077 1078 QualType dst (importer.Import(src.GetQualType())); 1079 1080 return ClangASTType (dst_ast, dst.getAsOpaquePtr()); 1081 } 1082 1083 1084 clang::Decl * 1085 ClangASTContext::CopyDecl (ASTContext *dst_ast, 1086 ASTContext *src_ast, 1087 clang::Decl *source_decl) 1088 { 1089 FileSystemOptions file_system_options; 1090 FileManager file_manager (file_system_options); 1091 ASTImporter importer(*dst_ast, file_manager, 1092 *src_ast, file_manager, 1093 false); 1094 1095 return importer.Import(source_decl); 1096 } 1097 1098 bool 1099 ClangASTContext::AreTypesSame (ClangASTType type1, 1100 ClangASTType type2, 1101 bool ignore_qualifiers) 1102 { 1103 ASTContext *ast = type1.GetASTContext(); 1104 if (ast != type2.GetASTContext()) 1105 return false; 1106 1107 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType()) 1108 return true; 1109 1110 QualType type1_qual = type1.GetQualType(); 1111 QualType type2_qual = type2.GetQualType(); 1112 1113 if (ignore_qualifiers) 1114 { 1115 type1_qual = type1_qual.getUnqualifiedType(); 1116 type2_qual = type2_qual.getUnqualifiedType(); 1117 } 1118 1119 return ast->hasSameType (type1_qual, type2_qual); 1120 } 1121 1122 1123 ClangASTType 1124 ClangASTContext::GetTypeForDecl (TagDecl *decl) 1125 { 1126 // No need to call the getASTContext() accessor (which can create the AST 1127 // if it isn't created yet, because we can't have created a decl in this 1128 // AST if our AST didn't already exist... 1129 ASTContext *ast = m_ast_ap.get(); 1130 if (ast) 1131 return ClangASTType (ast, ast->getTagDeclType(decl).getAsOpaquePtr()); 1132 return ClangASTType(); 1133 } 1134 1135 ClangASTType 1136 ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl) 1137 { 1138 // No need to call the getASTContext() accessor (which can create the AST 1139 // if it isn't created yet, because we can't have created a decl in this 1140 // AST if our AST didn't already exist... 1141 ASTContext *ast = m_ast_ap.get(); 1142 if (ast) 1143 return ClangASTType (ast, ast->getObjCInterfaceType(decl).getAsOpaquePtr()); 1144 return ClangASTType(); 1145 } 1146 1147 #pragma mark Structure, Unions, Classes 1148 1149 ClangASTType 1150 ClangASTContext::CreateRecordType (DeclContext *decl_ctx, 1151 AccessType access_type, 1152 const char *name, 1153 int kind, 1154 LanguageType language, 1155 ClangASTMetadata *metadata) 1156 { 1157 ASTContext *ast = getASTContext(); 1158 assert (ast != nullptr); 1159 1160 if (decl_ctx == nullptr) 1161 decl_ctx = ast->getTranslationUnitDecl(); 1162 1163 1164 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus) 1165 { 1166 bool isForwardDecl = true; 1167 bool isInternal = false; 1168 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal, metadata); 1169 } 1170 1171 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and 1172 // we will need to update this code. I was told to currently always use 1173 // the CXXRecordDecl class since we often don't know from debug information 1174 // if something is struct or a class, so we default to always use the more 1175 // complete definition just in case. 1176 1177 bool is_anonymous = (!name) || (!name[0]); 1178 1179 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast, 1180 (TagDecl::TagKind)kind, 1181 decl_ctx, 1182 SourceLocation(), 1183 SourceLocation(), 1184 is_anonymous ? nullptr : &ast->Idents.get(name)); 1185 1186 if (is_anonymous) 1187 decl->setAnonymousStructOrUnion(true); 1188 1189 if (decl) 1190 { 1191 if (metadata) 1192 SetMetadata(ast, decl, *metadata); 1193 1194 if (access_type != eAccessNone) 1195 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type)); 1196 1197 if (decl_ctx) 1198 decl_ctx->addDecl (decl); 1199 1200 return ClangASTType(ast, ast->getTagDeclType(decl).getAsOpaquePtr()); 1201 } 1202 return ClangASTType(); 1203 } 1204 1205 static TemplateParameterList * 1206 CreateTemplateParameterList (ASTContext *ast, 1207 const ClangASTContext::TemplateParameterInfos &template_param_infos, 1208 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) 1209 { 1210 const bool parameter_pack = false; 1211 const bool is_typename = false; 1212 const unsigned depth = 0; 1213 const size_t num_template_params = template_param_infos.GetSize(); 1214 for (size_t i=0; i<num_template_params; ++i) 1215 { 1216 const char *name = template_param_infos.names[i]; 1217 1218 IdentifierInfo *identifier_info = nullptr; 1219 if (name && name[0]) 1220 identifier_info = &ast->Idents.get(name); 1221 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral) 1222 { 1223 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast, 1224 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc, 1225 SourceLocation(), 1226 SourceLocation(), 1227 depth, 1228 i, 1229 identifier_info, 1230 template_param_infos.args[i].getIntegralType(), 1231 parameter_pack, 1232 nullptr)); 1233 1234 } 1235 else 1236 { 1237 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast, 1238 ast->getTranslationUnitDecl(), // Is this the right decl context? 1239 SourceLocation(), 1240 SourceLocation(), 1241 depth, 1242 i, 1243 identifier_info, 1244 is_typename, 1245 parameter_pack)); 1246 } 1247 } 1248 1249 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast, 1250 SourceLocation(), 1251 SourceLocation(), 1252 &template_param_decls.front(), 1253 template_param_decls.size(), 1254 SourceLocation()); 1255 return template_param_list; 1256 } 1257 1258 clang::FunctionTemplateDecl * 1259 ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx, 1260 clang::FunctionDecl *func_decl, 1261 const char *name, 1262 const TemplateParameterInfos &template_param_infos) 1263 { 1264 // /// \brief Create a function template node. 1265 ASTContext *ast = getASTContext(); 1266 1267 llvm::SmallVector<NamedDecl *, 8> template_param_decls; 1268 1269 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast, 1270 template_param_infos, 1271 template_param_decls); 1272 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast, 1273 decl_ctx, 1274 func_decl->getLocation(), 1275 func_decl->getDeclName(), 1276 template_param_list, 1277 func_decl); 1278 1279 for (size_t i=0, template_param_decl_count = template_param_decls.size(); 1280 i < template_param_decl_count; 1281 ++i) 1282 { 1283 // TODO: verify which decl context we should put template_param_decls into.. 1284 template_param_decls[i]->setDeclContext (func_decl); 1285 } 1286 1287 return func_tmpl_decl; 1288 } 1289 1290 void 1291 ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl, 1292 clang::FunctionTemplateDecl *func_tmpl_decl, 1293 const TemplateParameterInfos &infos) 1294 { 1295 TemplateArgumentList template_args (TemplateArgumentList::OnStack, 1296 infos.args.data(), 1297 infos.args.size()); 1298 1299 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl, 1300 &template_args, 1301 nullptr); 1302 } 1303 1304 1305 ClassTemplateDecl * 1306 ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx, 1307 lldb::AccessType access_type, 1308 const char *class_name, 1309 int kind, 1310 const TemplateParameterInfos &template_param_infos) 1311 { 1312 ASTContext *ast = getASTContext(); 1313 1314 ClassTemplateDecl *class_template_decl = nullptr; 1315 if (decl_ctx == nullptr) 1316 decl_ctx = ast->getTranslationUnitDecl(); 1317 1318 IdentifierInfo &identifier_info = ast->Idents.get(class_name); 1319 DeclarationName decl_name (&identifier_info); 1320 1321 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name); 1322 1323 for (NamedDecl *decl : result) 1324 { 1325 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl); 1326 if (class_template_decl) 1327 return class_template_decl; 1328 } 1329 1330 llvm::SmallVector<NamedDecl *, 8> template_param_decls; 1331 1332 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast, 1333 template_param_infos, 1334 template_param_decls); 1335 1336 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast, 1337 (TagDecl::TagKind)kind, 1338 decl_ctx, // What decl context do we use here? TU? The actual decl context? 1339 SourceLocation(), 1340 SourceLocation(), 1341 &identifier_info); 1342 1343 for (size_t i=0, template_param_decl_count = template_param_decls.size(); 1344 i < template_param_decl_count; 1345 ++i) 1346 { 1347 template_param_decls[i]->setDeclContext (template_cxx_decl); 1348 } 1349 1350 // With templated classes, we say that a class is templated with 1351 // specializations, but that the bare class has no functions. 1352 //template_cxx_decl->startDefinition(); 1353 //template_cxx_decl->completeDefinition(); 1354 1355 class_template_decl = ClassTemplateDecl::Create (*ast, 1356 decl_ctx, // What decl context do we use here? TU? The actual decl context? 1357 SourceLocation(), 1358 decl_name, 1359 template_param_list, 1360 template_cxx_decl, 1361 nullptr); 1362 1363 if (class_template_decl) 1364 { 1365 if (access_type != eAccessNone) 1366 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type)); 1367 1368 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx)) 1369 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl)); 1370 1371 decl_ctx->addDecl (class_template_decl); 1372 1373 #ifdef LLDB_CONFIGURATION_DEBUG 1374 VerifyDecl(class_template_decl); 1375 #endif 1376 } 1377 1378 return class_template_decl; 1379 } 1380 1381 1382 ClassTemplateSpecializationDecl * 1383 ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx, 1384 ClassTemplateDecl *class_template_decl, 1385 int kind, 1386 const TemplateParameterInfos &template_param_infos) 1387 { 1388 ASTContext *ast = getASTContext(); 1389 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast, 1390 (TagDecl::TagKind)kind, 1391 decl_ctx, 1392 SourceLocation(), 1393 SourceLocation(), 1394 class_template_decl, 1395 &template_param_infos.args.front(), 1396 template_param_infos.args.size(), 1397 nullptr); 1398 1399 class_template_specialization_decl->setSpecializationKind(TSK_ExplicitSpecialization); 1400 1401 return class_template_specialization_decl; 1402 } 1403 1404 ClangASTType 1405 ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl) 1406 { 1407 if (class_template_specialization_decl) 1408 { 1409 ASTContext *ast = getASTContext(); 1410 if (ast) 1411 return ClangASTType(ast, ast->getTagDeclType(class_template_specialization_decl).getAsOpaquePtr()); 1412 } 1413 return ClangASTType(); 1414 } 1415 1416 static inline bool 1417 check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params) 1418 { 1419 // Special-case call since it can take any number of operands 1420 if(op_kind == OO_Call) 1421 return true; 1422 1423 // The parameter count doesn't include "this" 1424 if (num_params == 0) 1425 return unary; 1426 if (num_params == 1) 1427 return binary; 1428 else 1429 return false; 1430 } 1431 1432 bool 1433 ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params) 1434 { 1435 switch (op_kind) 1436 { 1437 default: 1438 break; 1439 // C++ standard allows any number of arguments to new/delete 1440 case OO_New: 1441 case OO_Array_New: 1442 case OO_Delete: 1443 case OO_Array_Delete: 1444 return true; 1445 } 1446 1447 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) case OO_##Name: return check_op_param (op_kind, Unary, Binary, num_params); 1448 switch (op_kind) 1449 { 1450 #include "clang/Basic/OperatorKinds.def" 1451 default: break; 1452 } 1453 return false; 1454 } 1455 1456 clang::AccessSpecifier 1457 ClangASTContext::UnifyAccessSpecifiers (clang::AccessSpecifier lhs, clang::AccessSpecifier rhs) 1458 { 1459 clang::AccessSpecifier ret = lhs; 1460 1461 // Make the access equal to the stricter of the field and the nested field's access 1462 switch (ret) 1463 { 1464 case clang::AS_none: 1465 break; 1466 case clang::AS_private: 1467 break; 1468 case clang::AS_protected: 1469 if (rhs == AS_private) 1470 ret = AS_private; 1471 break; 1472 case clang::AS_public: 1473 ret = rhs; 1474 break; 1475 } 1476 1477 return ret; 1478 } 1479 1480 bool 1481 ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size) 1482 { 1483 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size); 1484 } 1485 1486 bool 1487 ClangASTContext::FieldIsBitfield 1488 ( 1489 ASTContext *ast, 1490 FieldDecl* field, 1491 uint32_t& bitfield_bit_size 1492 ) 1493 { 1494 if (ast == nullptr || field == nullptr) 1495 return false; 1496 1497 if (field->isBitField()) 1498 { 1499 Expr* bit_width_expr = field->getBitWidth(); 1500 if (bit_width_expr) 1501 { 1502 llvm::APSInt bit_width_apsint; 1503 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) 1504 { 1505 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX); 1506 return true; 1507 } 1508 } 1509 } 1510 return false; 1511 } 1512 1513 bool 1514 ClangASTContext::RecordHasFields (const RecordDecl *record_decl) 1515 { 1516 if (record_decl == nullptr) 1517 return false; 1518 1519 if (!record_decl->field_empty()) 1520 return true; 1521 1522 // No fields, lets check this is a CXX record and check the base classes 1523 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); 1524 if (cxx_record_decl) 1525 { 1526 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1527 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1528 base_class != base_class_end; 1529 ++base_class) 1530 { 1531 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl()); 1532 if (RecordHasFields(base_class_decl)) 1533 return true; 1534 } 1535 } 1536 return false; 1537 } 1538 1539 #pragma mark Objective C Classes 1540 1541 ClangASTType 1542 ClangASTContext::CreateObjCClass 1543 ( 1544 const char *name, 1545 DeclContext *decl_ctx, 1546 bool isForwardDecl, 1547 bool isInternal, 1548 ClangASTMetadata *metadata 1549 ) 1550 { 1551 ASTContext *ast = getASTContext(); 1552 assert (ast != nullptr); 1553 assert (name && name[0]); 1554 if (decl_ctx == nullptr) 1555 decl_ctx = ast->getTranslationUnitDecl(); 1556 1557 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast, 1558 decl_ctx, 1559 SourceLocation(), 1560 &ast->Idents.get(name), 1561 nullptr, 1562 SourceLocation(), 1563 /*isForwardDecl,*/ 1564 isInternal); 1565 1566 if (decl && metadata) 1567 SetMetadata(ast, decl, *metadata); 1568 1569 return ClangASTType (ast, ast->getObjCInterfaceType(decl)); 1570 } 1571 1572 static inline bool 1573 BaseSpecifierIsEmpty (const CXXBaseSpecifier *b) 1574 { 1575 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false; 1576 } 1577 1578 uint32_t 1579 ClangASTContext::GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes) 1580 { 1581 uint32_t num_bases = 0; 1582 if (cxx_record_decl) 1583 { 1584 if (omit_empty_base_classes) 1585 { 1586 CXXRecordDecl::base_class_const_iterator base_class, base_class_end; 1587 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end(); 1588 base_class != base_class_end; 1589 ++base_class) 1590 { 1591 // Skip empty base classes 1592 if (omit_empty_base_classes) 1593 { 1594 if (BaseSpecifierIsEmpty (base_class)) 1595 continue; 1596 } 1597 ++num_bases; 1598 } 1599 } 1600 else 1601 num_bases = cxx_record_decl->getNumBases(); 1602 } 1603 return num_bases; 1604 } 1605 1606 1607 #pragma mark Namespace Declarations 1608 1609 NamespaceDecl * 1610 ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx) 1611 { 1612 NamespaceDecl *namespace_decl = nullptr; 1613 ASTContext *ast = getASTContext(); 1614 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl (); 1615 if (decl_ctx == nullptr) 1616 decl_ctx = translation_unit_decl; 1617 1618 if (name) 1619 { 1620 IdentifierInfo &identifier_info = ast->Idents.get(name); 1621 DeclarationName decl_name (&identifier_info); 1622 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name); 1623 for (NamedDecl *decl : result) 1624 { 1625 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl); 1626 if (namespace_decl) 1627 return namespace_decl; 1628 } 1629 1630 namespace_decl = NamespaceDecl::Create(*ast, 1631 decl_ctx, 1632 false, 1633 SourceLocation(), 1634 SourceLocation(), 1635 &identifier_info, 1636 nullptr); 1637 1638 decl_ctx->addDecl (namespace_decl); 1639 } 1640 else 1641 { 1642 if (decl_ctx == translation_unit_decl) 1643 { 1644 namespace_decl = translation_unit_decl->getAnonymousNamespace(); 1645 if (namespace_decl) 1646 return namespace_decl; 1647 1648 namespace_decl = NamespaceDecl::Create(*ast, 1649 decl_ctx, 1650 false, 1651 SourceLocation(), 1652 SourceLocation(), 1653 nullptr, 1654 nullptr); 1655 translation_unit_decl->setAnonymousNamespace (namespace_decl); 1656 translation_unit_decl->addDecl (namespace_decl); 1657 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace()); 1658 } 1659 else 1660 { 1661 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx); 1662 if (parent_namespace_decl) 1663 { 1664 namespace_decl = parent_namespace_decl->getAnonymousNamespace(); 1665 if (namespace_decl) 1666 return namespace_decl; 1667 namespace_decl = NamespaceDecl::Create(*ast, 1668 decl_ctx, 1669 false, 1670 SourceLocation(), 1671 SourceLocation(), 1672 nullptr, 1673 nullptr); 1674 parent_namespace_decl->setAnonymousNamespace (namespace_decl); 1675 parent_namespace_decl->addDecl (namespace_decl); 1676 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace()); 1677 } 1678 else 1679 { 1680 // BAD!!! 1681 } 1682 } 1683 1684 1685 if (namespace_decl) 1686 { 1687 // If we make it here, we are creating the anonymous namespace decl 1688 // for the first time, so we need to do the using directive magic 1689 // like SEMA does 1690 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast, 1691 decl_ctx, 1692 SourceLocation(), 1693 SourceLocation(), 1694 NestedNameSpecifierLoc(), 1695 SourceLocation(), 1696 namespace_decl, 1697 decl_ctx); 1698 using_directive_decl->setImplicit(); 1699 decl_ctx->addDecl(using_directive_decl); 1700 } 1701 } 1702 #ifdef LLDB_CONFIGURATION_DEBUG 1703 VerifyDecl(namespace_decl); 1704 #endif 1705 return namespace_decl; 1706 } 1707 1708 1709 #pragma mark Function Types 1710 1711 FunctionDecl * 1712 ClangASTContext::CreateFunctionDeclaration (DeclContext *decl_ctx, 1713 const char *name, 1714 const ClangASTType &function_clang_type, 1715 int storage, 1716 bool is_inline) 1717 { 1718 FunctionDecl *func_decl = nullptr; 1719 ASTContext *ast = getASTContext(); 1720 if (decl_ctx == nullptr) 1721 decl_ctx = ast->getTranslationUnitDecl(); 1722 1723 1724 const bool hasWrittenPrototype = true; 1725 const bool isConstexprSpecified = false; 1726 1727 if (name && name[0]) 1728 { 1729 func_decl = FunctionDecl::Create (*ast, 1730 decl_ctx, 1731 SourceLocation(), 1732 SourceLocation(), 1733 DeclarationName (&ast->Idents.get(name)), 1734 function_clang_type.GetQualType(), 1735 nullptr, 1736 (FunctionDecl::StorageClass)storage, 1737 is_inline, 1738 hasWrittenPrototype, 1739 isConstexprSpecified); 1740 } 1741 else 1742 { 1743 func_decl = FunctionDecl::Create (*ast, 1744 decl_ctx, 1745 SourceLocation(), 1746 SourceLocation(), 1747 DeclarationName (), 1748 function_clang_type.GetQualType(), 1749 nullptr, 1750 (FunctionDecl::StorageClass)storage, 1751 is_inline, 1752 hasWrittenPrototype, 1753 isConstexprSpecified); 1754 } 1755 if (func_decl) 1756 decl_ctx->addDecl (func_decl); 1757 1758 #ifdef LLDB_CONFIGURATION_DEBUG 1759 VerifyDecl(func_decl); 1760 #endif 1761 1762 return func_decl; 1763 } 1764 1765 ClangASTType 1766 ClangASTContext::CreateFunctionType (ASTContext *ast, 1767 const ClangASTType& result_type, 1768 const ClangASTType *args, 1769 unsigned num_args, 1770 bool is_variadic, 1771 unsigned type_quals) 1772 { 1773 assert (ast != nullptr); 1774 std::vector<QualType> qual_type_args; 1775 for (unsigned i=0; i<num_args; ++i) 1776 qual_type_args.push_back (args[i].GetQualType()); 1777 1778 // TODO: Detect calling convention in DWARF? 1779 FunctionProtoType::ExtProtoInfo proto_info; 1780 proto_info.Variadic = is_variadic; 1781 proto_info.ExceptionSpecType = EST_None; 1782 proto_info.TypeQuals = type_quals; 1783 proto_info.RefQualifier = RQ_None; 1784 proto_info.NumExceptions = 0; 1785 proto_info.Exceptions = nullptr; 1786 1787 return ClangASTType (ast, ast->getFunctionType (result_type.GetQualType(), 1788 qual_type_args, 1789 proto_info).getAsOpaquePtr()); 1790 } 1791 1792 ParmVarDecl * 1793 ClangASTContext::CreateParameterDeclaration (const char *name, const ClangASTType ¶m_type, int storage) 1794 { 1795 ASTContext *ast = getASTContext(); 1796 assert (ast != nullptr); 1797 return ParmVarDecl::Create(*ast, 1798 ast->getTranslationUnitDecl(), 1799 SourceLocation(), 1800 SourceLocation(), 1801 name && name[0] ? &ast->Idents.get(name) : nullptr, 1802 param_type.GetQualType(), 1803 nullptr, 1804 (VarDecl::StorageClass)storage, 1805 nullptr); 1806 } 1807 1808 void 1809 ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params) 1810 { 1811 if (function_decl) 1812 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params)); 1813 } 1814 1815 1816 #pragma mark Array Types 1817 1818 ClangASTType 1819 ClangASTContext::CreateArrayType (const ClangASTType &element_type, 1820 size_t element_count, 1821 bool is_vector) 1822 { 1823 if (element_type.IsValid()) 1824 { 1825 ASTContext *ast = getASTContext(); 1826 assert (ast != nullptr); 1827 1828 if (is_vector) 1829 { 1830 return ClangASTType (ast, ast->getExtVectorType(element_type.GetQualType(), element_count).getAsOpaquePtr()); 1831 } 1832 else 1833 { 1834 1835 llvm::APInt ap_element_count (64, element_count); 1836 if (element_count == 0) 1837 { 1838 return ClangASTType (ast, ast->getIncompleteArrayType (element_type.GetQualType(), 1839 ArrayType::Normal, 1840 0).getAsOpaquePtr()); 1841 } 1842 else 1843 { 1844 return ClangASTType (ast, ast->getConstantArrayType (element_type.GetQualType(), 1845 ap_element_count, 1846 ArrayType::Normal, 1847 0).getAsOpaquePtr()); 1848 } 1849 } 1850 } 1851 return ClangASTType(); 1852 } 1853 1854 1855 1856 #pragma mark Enumeration Types 1857 1858 ClangASTType 1859 ClangASTContext::CreateEnumerationType 1860 ( 1861 const char *name, 1862 DeclContext *decl_ctx, 1863 const Declaration &decl, 1864 const ClangASTType &integer_clang_type 1865 ) 1866 { 1867 // TODO: Do something intelligent with the Declaration object passed in 1868 // like maybe filling in the SourceLocation with it... 1869 ASTContext *ast = getASTContext(); 1870 1871 // TODO: ask about these... 1872 // const bool IsScoped = false; 1873 // const bool IsFixed = false; 1874 1875 EnumDecl *enum_decl = EnumDecl::Create (*ast, 1876 decl_ctx, 1877 SourceLocation(), 1878 SourceLocation(), 1879 name && name[0] ? &ast->Idents.get(name) : nullptr, 1880 nullptr, 1881 false, // IsScoped 1882 false, // IsScopedUsingClassTag 1883 false); // IsFixed 1884 1885 1886 if (enum_decl) 1887 { 1888 // TODO: check if we should be setting the promotion type too? 1889 enum_decl->setIntegerType(integer_clang_type.GetQualType()); 1890 1891 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info 1892 1893 return ClangASTType (ast, ast->getTagDeclType(enum_decl).getAsOpaquePtr()); 1894 } 1895 return ClangASTType(); 1896 } 1897 1898 // Disable this for now since I can't seem to get a nicely formatted float 1899 // out of the APFloat class without just getting the float, double or quad 1900 // and then using a formatted print on it which defeats the purpose. We ideally 1901 // would like to get perfect string values for any kind of float semantics 1902 // so we can support remote targets. The code below also requires a patch to 1903 // llvm::APInt. 1904 //bool 1905 //ClangASTContext::ConvertFloatValueToString (ASTContext *ast, clang_type_t clang_type, const uint8_t* bytes, size_t byte_size, int apint_byte_order, std::string &float_str) 1906 //{ 1907 // uint32_t count = 0; 1908 // bool is_complex = false; 1909 // if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex)) 1910 // { 1911 // unsigned num_bytes_per_float = byte_size / count; 1912 // unsigned num_bits_per_float = num_bytes_per_float * 8; 1913 // 1914 // float_str.clear(); 1915 // uint32_t i; 1916 // for (i=0; i<count; i++) 1917 // { 1918 // APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order); 1919 // bool is_ieee = false; 1920 // APFloat ap_float(ap_int, is_ieee); 1921 // char s[1024]; 1922 // unsigned int hex_digits = 0; 1923 // bool upper_case = false; 1924 // 1925 // if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0) 1926 // { 1927 // if (i > 0) 1928 // float_str.append(", "); 1929 // float_str.append(s); 1930 // if (i == 1 && is_complex) 1931 // float_str.append(1, 'i'); 1932 // } 1933 // } 1934 // return !float_str.empty(); 1935 // } 1936 // return false; 1937 //} 1938 1939 ClangASTType 1940 ClangASTContext::GetIntTypeFromBitSize (clang::ASTContext *ast, 1941 size_t bit_size, bool is_signed) 1942 { 1943 if (ast) 1944 { 1945 if (is_signed) 1946 { 1947 if (bit_size == ast->getTypeSize(ast->SignedCharTy)) 1948 return ClangASTType(ast, ast->SignedCharTy.getAsOpaquePtr()); 1949 1950 if (bit_size == ast->getTypeSize(ast->ShortTy)) 1951 return ClangASTType(ast, ast->ShortTy.getAsOpaquePtr()); 1952 1953 if (bit_size == ast->getTypeSize(ast->IntTy)) 1954 return ClangASTType(ast, ast->IntTy.getAsOpaquePtr()); 1955 1956 if (bit_size == ast->getTypeSize(ast->LongTy)) 1957 return ClangASTType(ast, ast->LongTy.getAsOpaquePtr()); 1958 1959 if (bit_size == ast->getTypeSize(ast->LongLongTy)) 1960 return ClangASTType(ast, ast->LongLongTy.getAsOpaquePtr()); 1961 1962 if (bit_size == ast->getTypeSize(ast->Int128Ty)) 1963 return ClangASTType(ast, ast->Int128Ty.getAsOpaquePtr()); 1964 } 1965 else 1966 { 1967 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy)) 1968 return ClangASTType(ast, ast->UnsignedCharTy.getAsOpaquePtr()); 1969 1970 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy)) 1971 return ClangASTType(ast, ast->UnsignedShortTy.getAsOpaquePtr()); 1972 1973 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy)) 1974 return ClangASTType(ast, ast->UnsignedIntTy.getAsOpaquePtr()); 1975 1976 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy)) 1977 return ClangASTType(ast, ast->UnsignedLongTy.getAsOpaquePtr()); 1978 1979 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy)) 1980 return ClangASTType(ast, ast->UnsignedLongLongTy.getAsOpaquePtr()); 1981 1982 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty)) 1983 return ClangASTType(ast, ast->UnsignedInt128Ty.getAsOpaquePtr()); 1984 } 1985 } 1986 return ClangASTType(); 1987 } 1988 1989 ClangASTType 1990 ClangASTContext::GetPointerSizedIntType (clang::ASTContext *ast, bool is_signed) 1991 { 1992 if (ast) 1993 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy), is_signed); 1994 return ClangASTType(); 1995 } 1996 1997 ClangASTType 1998 ClangASTContext::GetFloatTypeFromBitSize (clang::ASTContext *ast, 1999 size_t bit_size) 2000 { 2001 if (ast) 2002 { 2003 if (bit_size == ast->getTypeSize(ast->FloatTy)) 2004 return ClangASTType(ast, ast->FloatTy.getAsOpaquePtr()); 2005 else if (bit_size == ast->getTypeSize(ast->DoubleTy)) 2006 return ClangASTType(ast, ast->DoubleTy.getAsOpaquePtr()); 2007 else if (bit_size == ast->getTypeSize(ast->LongDoubleTy)) 2008 return ClangASTType(ast, ast->LongDoubleTy.getAsOpaquePtr()); 2009 else if (bit_size == ast->getTypeSize(ast->HalfTy)) 2010 return ClangASTType(ast, ast->HalfTy.getAsOpaquePtr()); 2011 } 2012 return ClangASTType(); 2013 } 2014 2015 bool 2016 ClangASTContext::GetCompleteDecl (clang::ASTContext *ast, 2017 clang::Decl *decl) 2018 { 2019 if (!decl) 2020 return false; 2021 2022 ExternalASTSource *ast_source = ast->getExternalSource(); 2023 2024 if (!ast_source) 2025 return false; 2026 2027 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) 2028 { 2029 if (tag_decl->isCompleteDefinition()) 2030 return true; 2031 2032 if (!tag_decl->hasExternalLexicalStorage()) 2033 return false; 2034 2035 ast_source->CompleteType(tag_decl); 2036 2037 return !tag_decl->getTypeForDecl()->isIncompleteType(); 2038 } 2039 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) 2040 { 2041 if (objc_interface_decl->getDefinition()) 2042 return true; 2043 2044 if (!objc_interface_decl->hasExternalLexicalStorage()) 2045 return false; 2046 2047 ast_source->CompleteType(objc_interface_decl); 2048 2049 return !objc_interface_decl->getTypeForDecl()->isIncompleteType(); 2050 } 2051 else 2052 { 2053 return false; 2054 } 2055 } 2056 2057 void 2058 ClangASTContext::SetMetadataAsUserID (const void *object, 2059 user_id_t user_id) 2060 { 2061 ClangASTMetadata meta_data; 2062 meta_data.SetUserID (user_id); 2063 SetMetadata (object, meta_data); 2064 } 2065 2066 void 2067 ClangASTContext::SetMetadata (clang::ASTContext *ast, 2068 const void *object, 2069 ClangASTMetadata &metadata) 2070 { 2071 ClangExternalASTSourceCommon *external_source = 2072 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource()); 2073 2074 if (external_source) 2075 external_source->SetMetadata(object, metadata); 2076 } 2077 2078 ClangASTMetadata * 2079 ClangASTContext::GetMetadata (clang::ASTContext *ast, 2080 const void *object) 2081 { 2082 ClangExternalASTSourceCommon *external_source = 2083 static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource()); 2084 2085 if (external_source && external_source->HasMetadata(object)) 2086 return external_source->GetMetadata(object); 2087 else 2088 return nullptr; 2089 } 2090 2091 clang::DeclContext * 2092 ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl) 2093 { 2094 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl); 2095 } 2096 2097 clang::DeclContext * 2098 ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl) 2099 { 2100 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl); 2101 } 2102 2103 2104 bool 2105 ClangASTContext::GetClassMethodInfoForDeclContext (clang::DeclContext *decl_ctx, 2106 lldb::LanguageType &language, 2107 bool &is_instance_method, 2108 ConstString &language_object_name) 2109 { 2110 language_object_name.Clear(); 2111 language = eLanguageTypeUnknown; 2112 is_instance_method = false; 2113 2114 if (decl_ctx) 2115 { 2116 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) 2117 { 2118 if (method_decl->isStatic()) 2119 { 2120 is_instance_method = false; 2121 } 2122 else 2123 { 2124 language_object_name.SetCString("this"); 2125 is_instance_method = true; 2126 } 2127 language = eLanguageTypeC_plus_plus; 2128 return true; 2129 } 2130 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) 2131 { 2132 // Both static and instance methods have a "self" object in objective C 2133 language_object_name.SetCString("self"); 2134 if (method_decl->isInstanceMethod()) 2135 { 2136 is_instance_method = true; 2137 } 2138 else 2139 { 2140 is_instance_method = false; 2141 } 2142 language = eLanguageTypeObjC; 2143 return true; 2144 } 2145 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) 2146 { 2147 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), function_decl); 2148 if (metadata && metadata->HasObjectPtr()) 2149 { 2150 language_object_name.SetCString (metadata->GetObjectPtrName()); 2151 language = eLanguageTypeObjC; 2152 is_instance_method = true; 2153 } 2154 return true; 2155 } 2156 } 2157 return false; 2158 } 2159 2160