name_scope.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. namespace Carbon::SemIR {
  6. auto NameScope::Print(llvm::raw_ostream& out) const -> void {
  7. out << "{inst: " << inst_id_ << ", parent_scope: " << parent_scope_id_
  8. << ", has_error: " << (has_error_ ? "true" : "false");
  9. out << ", extended_scopes: [";
  10. llvm::ListSeparator scope_sep;
  11. for (auto id : extended_scopes_) {
  12. out << scope_sep << id;
  13. }
  14. out << "]";
  15. out << ", names: {";
  16. llvm::ListSeparator sep;
  17. for (auto entry : names_) {
  18. out << sep << entry.name_id << ": " << entry.inst_id;
  19. }
  20. out << "}";
  21. out << "}";
  22. }
  23. auto NameScope::AddRequired(Entry name_entry) -> void {
  24. auto add_name = [&] {
  25. EntryId index(names_.size());
  26. names_.push_back(name_entry);
  27. return index;
  28. };
  29. auto result = name_map_.Insert(name_entry.name_id, add_name);
  30. CARBON_CHECK(result.is_inserted(), "Failed to add required name: {0}",
  31. name_entry.name_id);
  32. }
  33. auto NameScope::LookupOrAdd(SemIR::NameId name_id, InstId inst_id,
  34. AccessKind access_kind)
  35. -> std::pair<bool, EntryId> {
  36. auto insert_result = name_map_.Insert(name_id, EntryId(names_.size()));
  37. if (!insert_result.is_inserted()) {
  38. return {false, EntryId(insert_result.value())};
  39. }
  40. names_.push_back(
  41. {.name_id = name_id, .inst_id = inst_id, .access_kind = access_kind});
  42. return {true, EntryId(names_.size() - 1)};
  43. }
  44. } // namespace Carbon::SemIR