name_scope.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <optional>
  6. #include <utility>
  7. #include "toolchain/sem_ir/file.h"
  8. namespace Carbon::SemIR {
  9. NameScopeStore::NameScopeStore(const File* file)
  10. // 1 reserved untagged id because the Package NameScope is used across
  11. // Files.
  12. : file_(file), values_(file->check_ir_id(), 1) {}
  13. auto NameScope::Print(llvm::raw_ostream& out) const -> void {
  14. out << "{inst: " << inst_id_ << ", parent_scope: " << parent_scope_id_
  15. << ", has_error: " << (has_error_ ? "true" : "false");
  16. out << ", extended_scopes: [";
  17. llvm::ListSeparator scope_sep;
  18. for (auto [id, inner_self_id] : extended_scopes_) {
  19. if (inner_self_id.has_value()) {
  20. out << scope_sep << "(" << id << ", inner self: " << inner_self_id << ")";
  21. } else {
  22. out << scope_sep << id;
  23. }
  24. }
  25. out << "]";
  26. out << ", names: {";
  27. llvm::ListSeparator sep;
  28. for (auto entry : names_) {
  29. if (entry.result.is_poisoned()) {
  30. continue;
  31. }
  32. out << sep << entry.name_id << ": " << entry.result.target_inst_id();
  33. }
  34. out << "}";
  35. out << "}";
  36. }
  37. auto NameScope::AddRequired(Entry name_entry) -> void {
  38. CARBON_CHECK(!name_entry.result.is_poisoned(),
  39. "Cannot add a poisoned name: {0}.", name_entry.name_id);
  40. auto add_name = [&] {
  41. EntryId index(names_.size());
  42. names_.push_back(name_entry);
  43. return index;
  44. };
  45. auto result = name_map_.Insert(name_entry.name_id, add_name);
  46. if (!result.is_inserted()) {
  47. // A required name can overwrite poison.
  48. auto& name = names_[result.value().index];
  49. CARBON_CHECK(name.result.is_poisoned(), "Failed to add required name: {0}",
  50. name_entry.name_id);
  51. name = name_entry;
  52. }
  53. }
  54. auto NameScope::LookupOrAdd(NameId name_id, InstId inst_id,
  55. AccessKind access_kind)
  56. -> std::pair<bool, EntryId> {
  57. auto insert_result = name_map_.Insert(name_id, EntryId(names_.size()));
  58. if (!insert_result.is_inserted()) {
  59. return {false, EntryId(insert_result.value())};
  60. }
  61. names_.push_back({.name_id = name_id,
  62. .result = ScopeLookupResult::MakeWrappedLookupResult(
  63. inst_id, access_kind)});
  64. return {true, EntryId(names_.size() - 1)};
  65. }
  66. auto NameScope::LookupOrPoison(LocId loc_id, NameId name_id)
  67. -> std::optional<EntryId> {
  68. if (!name_id.AsIdentifierId().has_value()) {
  69. return Lookup(name_id);
  70. }
  71. auto insert_result = name_map_.Insert(name_id, EntryId(names_.size()));
  72. if (insert_result.is_inserted()) {
  73. names_.push_back({.name_id = name_id,
  74. .result = ScopeLookupResult::MakePoisoned(loc_id)});
  75. return std::nullopt;
  76. }
  77. return insert_result.value();
  78. }
  79. auto NameScopeStore::GetInstIfValid(NameScopeId scope_id) const
  80. -> std::pair<InstId, std::optional<Inst>> {
  81. if (!scope_id.has_value()) {
  82. return {InstId::None, std::nullopt};
  83. }
  84. auto inst_id = Get(scope_id).inst_id();
  85. if (!inst_id.has_value()) {
  86. return {InstId::None, std::nullopt};
  87. }
  88. return {inst_id, file_->insts().Get(inst_id)};
  89. }
  90. } // namespace Carbon::SemIR