name_scope.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 : extended_scopes_) {
  19. out << scope_sep << id;
  20. }
  21. out << "]";
  22. out << ", names: {";
  23. llvm::ListSeparator sep;
  24. for (auto entry : names_) {
  25. if (entry.result.is_poisoned()) {
  26. continue;
  27. }
  28. out << sep << entry.name_id << ": " << entry.result.target_inst_id();
  29. }
  30. out << "}";
  31. out << "}";
  32. }
  33. auto NameScope::AddRequired(Entry name_entry) -> void {
  34. CARBON_CHECK(!name_entry.result.is_poisoned(),
  35. "Cannot add a poisoned name: {0}.", name_entry.name_id);
  36. auto add_name = [&] {
  37. EntryId index(names_.size());
  38. names_.push_back(name_entry);
  39. return index;
  40. };
  41. auto result = name_map_.Insert(name_entry.name_id, add_name);
  42. if (!result.is_inserted()) {
  43. // A required name can overwrite poison.
  44. auto& name = names_[result.value().index];
  45. CARBON_CHECK(name.result.is_poisoned(), "Failed to add required name: {0}",
  46. name_entry.name_id);
  47. name = name_entry;
  48. }
  49. }
  50. auto NameScope::LookupOrAdd(NameId name_id, InstId inst_id,
  51. AccessKind access_kind)
  52. -> std::pair<bool, EntryId> {
  53. auto insert_result = name_map_.Insert(name_id, EntryId(names_.size()));
  54. if (!insert_result.is_inserted()) {
  55. return {false, EntryId(insert_result.value())};
  56. }
  57. names_.push_back({.name_id = name_id,
  58. .result = ScopeLookupResult::MakeWrappedLookupResult(
  59. inst_id, access_kind)});
  60. return {true, EntryId(names_.size() - 1)};
  61. }
  62. auto NameScope::LookupOrPoison(LocId loc_id, NameId name_id)
  63. -> std::optional<EntryId> {
  64. if (!name_id.AsIdentifierId().has_value()) {
  65. return Lookup(name_id);
  66. }
  67. auto insert_result = name_map_.Insert(name_id, EntryId(names_.size()));
  68. if (insert_result.is_inserted()) {
  69. names_.push_back({.name_id = name_id,
  70. .result = ScopeLookupResult::MakePoisoned(loc_id)});
  71. return std::nullopt;
  72. }
  73. return insert_result.value();
  74. }
  75. auto NameScopeStore::GetInstIfValid(NameScopeId scope_id) const
  76. -> std::pair<InstId, std::optional<Inst>> {
  77. if (!scope_id.has_value()) {
  78. return {InstId::None, std::nullopt};
  79. }
  80. auto inst_id = Get(scope_id).inst_id();
  81. if (!inst_id.has_value()) {
  82. return {InstId::None, std::nullopt};
  83. }
  84. return {inst_id, file_->insts().Get(inst_id)};
  85. }
  86. } // namespace Carbon::SemIR