static_scope.cpp 1.8 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 "executable_semantics/ast/static_scope.h"
  5. #include "executable_semantics/common/error.h"
  6. namespace Carbon {
  7. NamedEntity::~NamedEntity() = default;
  8. void StaticScope::Add(std::string name, Nonnull<const NamedEntity*> entity) {
  9. auto [it, success] = declared_names_.insert({name, entity});
  10. if (!success && it->second != entity) {
  11. FATAL_COMPILATION_ERROR(entity->source_loc())
  12. << "Duplicate name `" << name << "` also found at "
  13. << it->second->source_loc();
  14. }
  15. }
  16. auto StaticScope::Resolve(const std::string& name,
  17. SourceLocation source_loc) const
  18. -> Nonnull<const NamedEntity*> {
  19. std::optional<Nonnull<const NamedEntity*>> result =
  20. TryResolve(name, source_loc);
  21. if (!result.has_value()) {
  22. FATAL_COMPILATION_ERROR(source_loc) << "could not resolve '" << name << "'";
  23. }
  24. return *result;
  25. }
  26. auto StaticScope::TryResolve(const std::string& name,
  27. SourceLocation source_loc) const
  28. -> std::optional<Nonnull<const NamedEntity*>> {
  29. auto it = declared_names_.find(name);
  30. if (it != declared_names_.end()) {
  31. return it->second;
  32. }
  33. std::optional<Nonnull<const NamedEntity*>> result;
  34. for (Nonnull<const StaticScope*> parent : parent_scopes_) {
  35. auto parent_result = parent->TryResolve(name, source_loc);
  36. if (parent_result.has_value() && result.has_value() &&
  37. *parent_result != *result) {
  38. FATAL_COMPILATION_ERROR(source_loc)
  39. << "'" << name << "' is ambiguous between " << (*result)->source_loc()
  40. << " and " << (*parent_result)->source_loc();
  41. }
  42. result = parent_result;
  43. }
  44. return result;
  45. }
  46. } // namespace Carbon