static_scope.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "explorer/ast/static_scope.h"
  5. #include <optional>
  6. #include "explorer/common/error_builders.h"
  7. #include "llvm/Support/Error.h"
  8. namespace Carbon {
  9. auto StaticScope::Add(const std::string& name, ValueNodeView entity,
  10. NameStatus status) -> ErrorOr<Success> {
  11. auto [it, inserted] = declared_names_.insert({name, {entity, status}});
  12. if (!inserted) {
  13. if (it->second.entity != entity) {
  14. return ProgramError(entity.base().source_loc())
  15. << "Duplicate name `" << name << "` also found at "
  16. << it->second.entity.base().source_loc();
  17. }
  18. if (static_cast<int>(status) > static_cast<int>(it->second.status)) {
  19. it->second.status = status;
  20. }
  21. }
  22. return Success();
  23. }
  24. void StaticScope::MarkDeclared(const std::string& name) {
  25. auto it = declared_names_.find(name);
  26. CARBON_CHECK(it != declared_names_.end()) << name << " not found";
  27. if (it->second.status == NameStatus::KnownButNotDeclared) {
  28. it->second.status = NameStatus::DeclaredButNotUsable;
  29. }
  30. }
  31. void StaticScope::MarkUsable(const std::string& name) {
  32. auto it = declared_names_.find(name);
  33. CARBON_CHECK(it != declared_names_.end()) << name << " not found";
  34. it->second.status = NameStatus::Usable;
  35. }
  36. auto StaticScope::Resolve(const std::string& name,
  37. SourceLocation source_loc) const
  38. -> ErrorOr<ValueNodeView> {
  39. CARBON_ASSIGN_OR_RETURN(std::optional<ValueNodeView> result,
  40. TryResolve(name, source_loc));
  41. if (!result) {
  42. return ProgramError(source_loc) << "could not resolve '" << name << "'";
  43. }
  44. return *result;
  45. }
  46. auto StaticScope::TryResolve(const std::string& name,
  47. SourceLocation source_loc) const
  48. -> ErrorOr<std::optional<ValueNodeView>> {
  49. auto it = declared_names_.find(name);
  50. if (it != declared_names_.end()) {
  51. switch (it->second.status) {
  52. case NameStatus::KnownButNotDeclared:
  53. return ProgramError(source_loc)
  54. << "'" << name << "' has not been declared yet";
  55. case NameStatus::DeclaredButNotUsable:
  56. return ProgramError(source_loc)
  57. << "'" << name
  58. << "' is not usable until after it has been completely declared";
  59. case NameStatus::Usable:
  60. return std::make_optional(it->second.entity);
  61. }
  62. }
  63. std::optional<ValueNodeView> result;
  64. for (Nonnull<const StaticScope*> parent : parent_scopes_) {
  65. CARBON_ASSIGN_OR_RETURN(std::optional<ValueNodeView> parent_result,
  66. parent->TryResolve(name, source_loc));
  67. if (parent_result.has_value() && result.has_value() &&
  68. *parent_result != *result) {
  69. return ProgramError(source_loc)
  70. << "'" << name << "' is ambiguous between "
  71. << result->base().source_loc() << " and "
  72. << parent_result->base().source_loc();
  73. }
  74. result = parent_result;
  75. }
  76. return result;
  77. }
  78. auto StaticScope::AddReturnedVar(ValueNodeView returned_var_def_view)
  79. -> ErrorOr<Success> {
  80. std::optional<ValueNodeView> resolved_returned_var = ResolveReturned();
  81. if (resolved_returned_var.has_value()) {
  82. return ProgramError(returned_var_def_view.base().source_loc())
  83. << "Duplicate definition of returned var also found at "
  84. << resolved_returned_var->base().source_loc();
  85. }
  86. returned_var_def_view_ = std::move(returned_var_def_view);
  87. return Success();
  88. }
  89. auto StaticScope::ResolveReturned() const -> std::optional<ValueNodeView> {
  90. if (returned_var_def_view_.has_value()) {
  91. return returned_var_def_view_;
  92. }
  93. for (Nonnull<const StaticScope*> parent : parent_scopes_) {
  94. std::optional<ValueNodeView> parent_returned_var =
  95. parent->ResolveReturned();
  96. if (parent_returned_var.has_value()) {
  97. return parent_returned_var;
  98. }
  99. }
  100. return std::nullopt;
  101. }
  102. } // namespace Carbon