name_scope.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/sem_ir/name_scope.h"
  5. #include "toolchain/sem_ir/file.h"
  6. namespace Carbon::SemIR {
  7. auto NameScope::Print(llvm::raw_ostream& out) const -> void {
  8. out << "{inst: " << inst_id_ << ", parent_scope: " << parent_scope_id_
  9. << ", has_error: " << (has_error_ ? "true" : "false");
  10. out << ", extended_scopes: [";
  11. llvm::ListSeparator scope_sep;
  12. for (auto id : extended_scopes_) {
  13. out << scope_sep << id;
  14. }
  15. out << "]";
  16. out << ", names: {";
  17. llvm::ListSeparator sep;
  18. for (auto entry : names_) {
  19. if (entry.result.is_poisoned()) {
  20. continue;
  21. }
  22. out << sep << entry.name_id << ": " << entry.result.target_inst_id();
  23. }
  24. out << "}";
  25. out << "}";
  26. }
  27. auto NameScope::AddRequired(Entry name_entry) -> void {
  28. CARBON_CHECK(!name_entry.result.is_poisoned(),
  29. "Cannot add a poisoned name: {0}.", name_entry.name_id);
  30. auto add_name = [&] {
  31. EntryId index(names_.size());
  32. names_.push_back(name_entry);
  33. return index;
  34. };
  35. auto result = name_map_.Insert(name_entry.name_id, add_name);
  36. if (!result.is_inserted()) {
  37. // A required name can overwrite poison.
  38. auto& name = names_[result.value().index];
  39. CARBON_CHECK(name.result.is_poisoned(), "Failed to add required name: {0}",
  40. name_entry.name_id);
  41. name = name_entry;
  42. }
  43. }
  44. auto NameScope::LookupOrAdd(NameId name_id, InstId inst_id,
  45. AccessKind access_kind)
  46. -> std::pair<bool, EntryId> {
  47. auto insert_result = name_map_.Insert(name_id, EntryId(names_.size()));
  48. if (!insert_result.is_inserted()) {
  49. return {false, EntryId(insert_result.value())};
  50. }
  51. names_.push_back({.name_id = name_id,
  52. .result = ScopeLookupResult::MakeWrappedLookupResult(
  53. inst_id, access_kind)});
  54. return {true, EntryId(names_.size() - 1)};
  55. }
  56. auto NameScope::LookupOrPoison(LocId loc_id, NameId name_id)
  57. -> std::optional<EntryId> {
  58. auto insert_result = name_map_.Insert(name_id, EntryId(names_.size()));
  59. if (insert_result.is_inserted()) {
  60. names_.push_back({.name_id = name_id,
  61. .result = ScopeLookupResult::MakePoisoned(loc_id)});
  62. return std::nullopt;
  63. }
  64. return insert_result.value();
  65. }
  66. auto NameScopeStore::GetInstIfValid(NameScopeId scope_id) const
  67. -> std::pair<InstId, std::optional<Inst>> {
  68. if (!scope_id.has_value()) {
  69. return {InstId::None, std::nullopt};
  70. }
  71. auto inst_id = Get(scope_id).inst_id();
  72. if (!inst_id.has_value()) {
  73. return {InstId::None, std::nullopt};
  74. }
  75. return {inst_id, file_->insts().Get(inst_id)};
  76. }
  77. auto NameScopeStore::IsCorePackage(NameScopeId scope_id) const -> bool {
  78. if (!IsPackage(scope_id)) {
  79. return false;
  80. }
  81. auto scope_name =
  82. file_->names().GetAsStringIfIdentifier(Get(scope_id).name_id());
  83. return scope_name == "Core";
  84. }
  85. } // namespace Carbon::SemIR