static_scope.cpp 4.0 KB

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