scope_stack.cpp 6.4 KB

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