name_scope.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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(NameId name_id) -> std::optional<EntryId> {
  57. auto insert_result = name_map_.Insert(name_id, EntryId(names_.size()));
  58. if (insert_result.is_inserted()) {
  59. names_.push_back(
  60. {.name_id = name_id, .result = ScopeLookupResult::MakePoisoned()});
  61. return std::nullopt;
  62. }
  63. return insert_result.value();
  64. }
  65. auto NameScopeStore::GetInstIfValid(NameScopeId scope_id) const
  66. -> std::pair<InstId, std::optional<Inst>> {
  67. if (!scope_id.has_value()) {
  68. return {InstId::None, std::nullopt};
  69. }
  70. auto inst_id = Get(scope_id).inst_id();
  71. if (!inst_id.has_value()) {
  72. return {InstId::None, std::nullopt};
  73. }
  74. return {inst_id, file_->insts().Get(inst_id)};
  75. }
  76. auto NameScopeStore::IsCorePackage(NameScopeId scope_id) const -> bool {
  77. if (!IsPackage(scope_id)) {
  78. return false;
  79. }
  80. auto scope_name =
  81. file_->names().GetAsStringIfIdentifier(Get(scope_id).name_id());
  82. return scope_name == "Core";
  83. }
  84. } // namespace Carbon::SemIR