name_scope.cpp 3.0 KB

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