name_scope.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.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.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. auto insert_result = name_map_.Insert(name_id, EntryId(names_.size()));
  44. if (!insert_result.is_inserted()) {
  45. return {false, EntryId(insert_result.value())};
  46. }
  47. names_.push_back(
  48. {.name_id = name_id, .inst_id = inst_id, .access_kind = access_kind});
  49. return {true, EntryId(names_.size() - 1)};
  50. }
  51. auto NameScope::AddPoison(NameId name_id) -> void {
  52. auto insert_result = name_map_.Insert(name_id, EntryId(names_.size()));
  53. CARBON_CHECK(insert_result.is_inserted(),
  54. "Trying to poison an existing name: {0}", name_id);
  55. names_.push_back({.name_id = name_id,
  56. .inst_id = InstId::Invalid,
  57. .access_kind = AccessKind::Public,
  58. .is_poisoned = true});
  59. }
  60. auto NameScopeStore::GetInstIfValid(NameScopeId scope_id) const
  61. -> std::pair<InstId, std::optional<Inst>> {
  62. if (!scope_id.is_valid()) {
  63. return {InstId::Invalid, std::nullopt};
  64. }
  65. auto inst_id = Get(scope_id).inst_id();
  66. if (!inst_id.is_valid()) {
  67. return {InstId::Invalid, std::nullopt};
  68. }
  69. return {inst_id, file_->insts().Get(inst_id)};
  70. }
  71. auto NameScopeStore::IsCorePackage(NameScopeId scope_id) const -> bool {
  72. if (!IsPackage(scope_id)) {
  73. return false;
  74. }
  75. auto scope_name =
  76. file_->names().GetAsStringIfIdentifier(Get(scope_id).name_id());
  77. return scope_name == "Core";
  78. }
  79. } // namespace Carbon::SemIR