static_scope.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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(std::string_view 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(std::string_view 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(std::string_view 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(std::string_view 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::ResolveHere(std::optional<ValueNodeView> this_scope,
  47. std::string_view name, SourceLocation source_loc,
  48. bool allow_undeclared) const
  49. -> ErrorOr<ValueNodeView> {
  50. CARBON_ASSIGN_OR_RETURN(std::optional<ValueNodeView> result,
  51. TryResolveHere(name, source_loc, allow_undeclared));
  52. if (!result) {
  53. if (this_scope) {
  54. return ProgramError(source_loc)
  55. << "name '" << name << "' has not been declared in "
  56. << PrintAsID(this_scope->base());
  57. } else {
  58. return ProgramError(source_loc)
  59. << "name '" << name << "' has not been declared in this scope";
  60. }
  61. }
  62. return *result;
  63. }
  64. auto StaticScope::TryResolve(std::string_view name,
  65. SourceLocation source_loc) const
  66. -> ErrorOr<std::optional<ValueNodeView>> {
  67. for (const StaticScope* scope = this; scope;
  68. scope = scope->parent_scope_.value_or(nullptr)) {
  69. CARBON_ASSIGN_OR_RETURN(
  70. std::optional<ValueNodeView> value,
  71. scope->TryResolveHere(name, source_loc, /*allow_undeclared=*/false));
  72. if (value) {
  73. return value;
  74. }
  75. }
  76. return {std::nullopt};
  77. }
  78. auto StaticScope::TryResolveHere(std::string_view name,
  79. SourceLocation source_loc,
  80. bool allow_undeclared) const
  81. -> ErrorOr<std::optional<ValueNodeView>> {
  82. auto it = declared_names_.find(name);
  83. if (it == declared_names_.end()) {
  84. return {std::nullopt};
  85. }
  86. if (allow_undeclared) {
  87. return {it->second.entity};
  88. }
  89. switch (it->second.status) {
  90. case NameStatus::KnownButNotDeclared:
  91. return ProgramError(source_loc)
  92. << "'" << name << "' has not been declared yet";
  93. case NameStatus::DeclaredButNotUsable:
  94. return ProgramError(source_loc) << "'" << name
  95. << "' is not usable until after it "
  96. "has been completely declared";
  97. case NameStatus::Usable:
  98. return {it->second.entity};
  99. }
  100. }
  101. auto StaticScope::AddReturnedVar(ValueNodeView returned_var_def_view)
  102. -> ErrorOr<Success> {
  103. std::optional<ValueNodeView> resolved_returned_var = ResolveReturned();
  104. if (resolved_returned_var.has_value()) {
  105. return ProgramError(returned_var_def_view.base().source_loc())
  106. << "Duplicate definition of returned var also found at "
  107. << resolved_returned_var->base().source_loc();
  108. }
  109. returned_var_def_view_ = std::move(returned_var_def_view);
  110. return Success();
  111. }
  112. auto StaticScope::ResolveReturned() const -> std::optional<ValueNodeView> {
  113. for (const StaticScope* scope = this; scope;
  114. scope = scope->parent_scope_.value_or(nullptr)) {
  115. if (scope->returned_var_def_view_.has_value()) {
  116. return scope->returned_var_def_view_;
  117. }
  118. }
  119. return std::nullopt;
  120. }
  121. } // namespace Carbon