scope_stack.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "toolchain/check/scope_stack.h"
  5. #include "common/check.h"
  6. #include "toolchain/sem_ir/ids.h"
  7. namespace Carbon::Check {
  8. auto ScopeStack::VerifyOnFinish() -> void {
  9. CARBON_CHECK(scope_stack_.empty()) << scope_stack_.size();
  10. }
  11. auto ScopeStack::Push(SemIR::InstId scope_inst_id, SemIR::NameScopeId scope_id,
  12. bool lexical_lookup_has_load_error) -> void {
  13. scope_stack_.push_back(
  14. {.index = next_scope_index_,
  15. .scope_inst_id = scope_inst_id,
  16. .scope_id = scope_id,
  17. .lexical_lookup_has_load_error =
  18. LexicalLookupHasLoadError() || lexical_lookup_has_load_error});
  19. if (scope_id.is_valid()) {
  20. non_lexical_scope_stack_.push_back({next_scope_index_, scope_id});
  21. }
  22. // TODO: Handle this case more gracefully.
  23. CARBON_CHECK(next_scope_index_.index != std::numeric_limits<int32_t>::max())
  24. << "Ran out of scopes";
  25. ++next_scope_index_.index;
  26. }
  27. auto ScopeStack::Pop() -> void {
  28. auto scope = scope_stack_.pop_back_val();
  29. for (const auto& str_id : scope.names) {
  30. auto& lexical_results = lexical_lookup_.Get(str_id);
  31. CARBON_CHECK(lexical_results.back().scope_index == scope.index)
  32. << "Inconsistent scope index for name " << str_id;
  33. lexical_results.pop_back();
  34. }
  35. if (scope.scope_id.is_valid()) {
  36. CARBON_CHECK(non_lexical_scope_stack_.back().scope_index == scope.index);
  37. non_lexical_scope_stack_.pop_back();
  38. }
  39. if (scope.has_returned_var) {
  40. CARBON_CHECK(!return_scope_stack_.empty());
  41. CARBON_CHECK(return_scope_stack_.back().returned_var.is_valid());
  42. return_scope_stack_.back().returned_var = SemIR::InstId::Invalid;
  43. }
  44. }
  45. auto ScopeStack::PopTo(ScopeIndex index) -> void {
  46. while (PeekIndex() > index) {
  47. Pop();
  48. }
  49. CARBON_CHECK(PeekIndex() == index)
  50. << "Scope index " << index << " does not enclose the current scope "
  51. << PeekIndex();
  52. }
  53. auto ScopeStack::LookupInCurrentScope(SemIR::NameId name_id) -> SemIR::InstId {
  54. auto& lexical_results = lexical_lookup_.Get(name_id);
  55. if (lexical_results.empty()) {
  56. return SemIR::InstId::Invalid;
  57. }
  58. auto result = lexical_results.back();
  59. if (result.scope_index != PeekIndex()) {
  60. return SemIR::InstId::Invalid;
  61. }
  62. return result.inst_id;
  63. }
  64. auto ScopeStack::LookupInEnclosingScopes(SemIR::NameId name_id)
  65. -> std::pair<SemIR::InstId, llvm::ArrayRef<NonLexicalScope>> {
  66. // Find the results from enclosing lexical scopes. These will be combined with
  67. // results from non-lexical scopes such as namespaces and classes.
  68. llvm::ArrayRef<LexicalLookup::Result> lexical_results =
  69. lexical_lookup_.Get(name_id);
  70. // If we have no lexical results, check all non-lexical scopes.
  71. if (lexical_results.empty()) {
  72. return {LexicalLookupHasLoadError() ? SemIR::InstId::BuiltinError
  73. : SemIR::InstId::Invalid,
  74. non_lexical_scope_stack_};
  75. }
  76. // Find the first non-lexical scope that is within the scope of the lexical
  77. // lookup result.
  78. auto* first_non_lexical_scope = std::lower_bound(
  79. non_lexical_scope_stack_.begin(), non_lexical_scope_stack_.end(),
  80. lexical_results.back().scope_index,
  81. [](const NonLexicalScope& scope, ScopeIndex index) {
  82. return scope.scope_index < index;
  83. });
  84. return {
  85. lexical_results.back().inst_id,
  86. llvm::ArrayRef(first_non_lexical_scope, non_lexical_scope_stack_.end())};
  87. }
  88. auto ScopeStack::LookupOrAddName(SemIR::NameId name_id, SemIR::InstId target_id)
  89. -> SemIR::InstId {
  90. if (!scope_stack_.back().names.insert(name_id).second) {
  91. auto existing = lexical_lookup_.Get(name_id).back().inst_id;
  92. CARBON_CHECK(existing.is_valid())
  93. << "Name in scope but not in lexical lookups";
  94. return existing;
  95. }
  96. // TODO: Reject if we previously performed a failed lookup for this name
  97. // in this scope or a scope nested within it.
  98. auto& lexical_results = lexical_lookup_.Get(name_id);
  99. CARBON_CHECK(lexical_results.empty() ||
  100. lexical_results.back().scope_index < PeekIndex())
  101. << "Failed to clean up after scope nested within the current scope";
  102. lexical_results.push_back({.inst_id = target_id, .scope_index = PeekIndex()});
  103. return SemIR::InstId::Invalid;
  104. }
  105. auto ScopeStack::SetReturnedVarOrGetExisting(SemIR::InstId inst_id)
  106. -> SemIR::InstId {
  107. CARBON_CHECK(!return_scope_stack_.empty()) << "`returned var` in no function";
  108. auto& returned_var = return_scope_stack_.back().returned_var;
  109. if (returned_var.is_valid()) {
  110. return returned_var;
  111. }
  112. returned_var = inst_id;
  113. CARBON_CHECK(!scope_stack_.back().has_returned_var)
  114. << "Scope has returned var but none is set";
  115. if (inst_id.is_valid()) {
  116. scope_stack_.back().has_returned_var = true;
  117. }
  118. return SemIR::InstId::Invalid;
  119. }
  120. auto ScopeStack::Suspend() -> SuspendedScope {
  121. CARBON_CHECK(!scope_stack_.empty()) << "No scope to suspend";
  122. SuspendedScope result = {scope_stack_.pop_back_val(), {}};
  123. if (result.entry.scope_id.is_valid()) {
  124. non_lexical_scope_stack_.pop_back();
  125. }
  126. for (auto name_id : result.entry.names) {
  127. result.suspended_lookups.push_back(lexical_lookup_.Suspend(name_id));
  128. }
  129. // This would be easy to support if we had a need, but currently we do not.
  130. CARBON_CHECK(!result.entry.has_returned_var)
  131. << "Should not suspend a scope with a returned var.";
  132. return result;
  133. }
  134. auto ScopeStack::Restore(SuspendedScope scope) -> void {
  135. for (auto entry : scope.suspended_lookups) {
  136. // clang-tidy warns that the `std::move` below has no effect. While that's
  137. // true, this `move` defends against the suspended lookup growing more state
  138. // later.
  139. // NOLINTNEXTLINE(performance-move-const-arg)
  140. lexical_lookup_.Restore(std::move(entry), scope.entry.index);
  141. }
  142. if (scope.entry.scope_id.is_valid()) {
  143. non_lexical_scope_stack_.push_back(
  144. {scope.entry.index, scope.entry.scope_id});
  145. }
  146. scope_stack_.push_back(std::move(scope.entry));
  147. }
  148. } // namespace Carbon::Check