scope_stack.cpp 6.2 KB

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