name_scope.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.inst_id.is_poisoned()) {
  20. continue;
  21. }
  22. out << sep << entry.name_id << ": " << entry.inst_id;
  23. }
  24. out << "}";
  25. out << "}";
  26. }
  27. auto NameScope::AddRequired(Entry name_entry) -> void {
  28. CARBON_CHECK(!name_entry.inst_id.is_poisoned(),
  29. "Cannot add a poisoned name: {0}. Use AddPoison()",
  30. name_entry.name_id);
  31. auto add_name = [&] {
  32. EntryId index(names_.size());
  33. names_.push_back(name_entry);
  34. return index;
  35. };
  36. auto result = name_map_.Insert(name_entry.name_id, add_name);
  37. CARBON_CHECK(result.is_inserted(), "Failed to add required name: {0}",
  38. name_entry.name_id);
  39. }
  40. auto NameScope::LookupOrAdd(SemIR::NameId name_id, InstId inst_id,
  41. AccessKind access_kind)
  42. -> std::pair<bool, EntryId> {
  43. CARBON_CHECK(!inst_id.is_poisoned(),
  44. "Cannot add a poisoned name: {0}. Use AddPoison()", name_id);
  45. auto insert_result = name_map_.Insert(name_id, EntryId(names_.size()));
  46. if (!insert_result.is_inserted()) {
  47. return {false, EntryId(insert_result.value())};
  48. }
  49. names_.push_back(
  50. {.name_id = name_id, .inst_id = inst_id, .access_kind = access_kind});
  51. return {true, EntryId(names_.size() - 1)};
  52. }
  53. auto NameScope::AddPoison(NameId name_id) -> void {
  54. auto insert_result = name_map_.Insert(name_id, EntryId(names_.size()));
  55. CARBON_CHECK(insert_result.is_inserted(),
  56. "Trying to poison an existing name: {0}", name_id);
  57. names_.push_back({.name_id = name_id,
  58. .inst_id = InstId::PoisonedName,
  59. .access_kind = AccessKind::Public});
  60. }
  61. auto NameScopeStore::GetInstIfValid(NameScopeId scope_id) const
  62. -> std::pair<InstId, std::optional<Inst>> {
  63. if (!scope_id.is_valid()) {
  64. return {InstId::Invalid, std::nullopt};
  65. }
  66. auto inst_id = Get(scope_id).inst_id();
  67. if (!inst_id.is_valid()) {
  68. return {InstId::Invalid, std::nullopt};
  69. }
  70. return {inst_id, file_->insts().Get(inst_id)};
  71. }
  72. auto NameScopeStore::IsCorePackage(NameScopeId scope_id) const -> bool {
  73. if (!IsPackage(scope_id)) {
  74. return false;
  75. }
  76. auto scope_name =
  77. file_->names().GetAsStringIfIdentifier(Get(scope_id).name_id());
  78. return scope_name == "Core";
  79. }
  80. } // namespace Carbon::SemIR