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