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. if (!declared_names_.insert({name, entity}).second) {
  10. FATAL_COMPILATION_ERROR(entity->source_loc())
  11. << "Duplicate name `" << name << "` also found at "
  12. << declared_names_[name]->source_loc();
  13. }
  14. }
  15. auto StaticScope::Resolve(const std::string& name,
  16. SourceLocation source_loc) const
  17. -> Nonnull<const NamedEntity*> {
  18. std::optional<Nonnull<const NamedEntity*>> result =
  19. TryResolve(name, source_loc);
  20. if (!result.has_value()) {
  21. FATAL_COMPILATION_ERROR(source_loc)
  22. << "'" << name << "' is not declared in this scope";
  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