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