scope_stack.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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(), "{0}", scope_stack_.size());
  10. }
  11. auto ScopeStack::Push(SemIR::InstId scope_inst_id, SemIR::NameScopeId scope_id,
  12. SemIR::SpecificId specific_id,
  13. bool lexical_lookup_has_load_error) -> void {
  14. // If this scope doesn't have a specific of its own, it lives in the enclosing
  15. // scope's specific, if any.
  16. auto enclosing_specific_id = specific_id;
  17. if (!specific_id.is_valid() && !scope_stack_.empty()) {
  18. enclosing_specific_id = PeekSpecificId();
  19. }
  20. compile_time_binding_stack_.PushArray();
  21. scope_stack_.push_back(
  22. {.index = next_scope_index_,
  23. .scope_inst_id = scope_inst_id,
  24. .scope_id = scope_id,
  25. .specific_id = enclosing_specific_id,
  26. .next_compile_time_bind_index = SemIR::CompileTimeBindIndex(
  27. compile_time_binding_stack_.all_values_size()),
  28. .lexical_lookup_has_load_error =
  29. LexicalLookupHasLoadError() || lexical_lookup_has_load_error});
  30. if (scope_id.is_valid()) {
  31. non_lexical_scope_stack_.push_back({.scope_index = next_scope_index_,
  32. .name_scope_id = scope_id,
  33. .specific_id = enclosing_specific_id});
  34. } else {
  35. // For lexical lookups, unqualified lookup doesn't know how to find the
  36. // associated specific, so if we start adding lexical scopes associated with
  37. // specifics, we'll need to somehow track them in lookup.
  38. CARBON_CHECK(!specific_id.is_valid(),
  39. "Lexical scope should not have an associated specific.");
  40. }
  41. // TODO: Handle this case more gracefully.
  42. CARBON_CHECK(next_scope_index_.index != std::numeric_limits<int32_t>::max(),
  43. "Ran out of scopes");
  44. ++next_scope_index_.index;
  45. }
  46. auto ScopeStack::Pop() -> void {
  47. auto scope = scope_stack_.pop_back_val();
  48. scope.names.ForEach([&](SemIR::NameId str_id) {
  49. auto& lexical_results = lexical_lookup_.Get(str_id);
  50. CARBON_CHECK(lexical_results.back().scope_index == scope.index,
  51. "Inconsistent scope index for name {0}", str_id);
  52. lexical_results.pop_back();
  53. });
  54. if (scope.scope_id.is_valid()) {
  55. CARBON_CHECK(non_lexical_scope_stack_.back().scope_index == scope.index);
  56. non_lexical_scope_stack_.pop_back();
  57. }
  58. if (scope.has_returned_var) {
  59. CARBON_CHECK(!return_scope_stack_.empty());
  60. CARBON_CHECK(return_scope_stack_.back().returned_var.is_valid());
  61. return_scope_stack_.back().returned_var = SemIR::InstId::Invalid;
  62. }
  63. CARBON_CHECK(
  64. scope.next_compile_time_bind_index.index ==
  65. static_cast<int32_t>(compile_time_binding_stack_.all_values_size()),
  66. "Wrong number of entries in compile-time binding stack, have {0}, "
  67. "expected {1}",
  68. compile_time_binding_stack_.all_values_size(),
  69. scope.next_compile_time_bind_index.index);
  70. compile_time_binding_stack_.PopArray();
  71. }
  72. auto ScopeStack::PopTo(ScopeIndex index) -> void {
  73. while (PeekIndex() > index) {
  74. Pop();
  75. }
  76. CARBON_CHECK(PeekIndex() == index,
  77. "Scope index {0} does not enclose the current scope {1}", index,
  78. PeekIndex());
  79. }
  80. auto ScopeStack::LookupInCurrentScope(SemIR::NameId name_id) -> SemIR::InstId {
  81. auto& lexical_results = lexical_lookup_.Get(name_id);
  82. if (lexical_results.empty()) {
  83. return SemIR::InstId::Invalid;
  84. }
  85. auto result = lexical_results.back();
  86. if (result.scope_index != PeekIndex()) {
  87. return SemIR::InstId::Invalid;
  88. }
  89. return result.inst_id;
  90. }
  91. auto ScopeStack::LookupInLexicalScopes(SemIR::NameId name_id)
  92. -> std::pair<SemIR::InstId, llvm::ArrayRef<NonLexicalScope>> {
  93. // Find the results from lexical scopes. These will be combined with results
  94. // from non-lexical scopes such as namespaces and classes.
  95. llvm::ArrayRef<LexicalLookup::Result> lexical_results =
  96. lexical_lookup_.Get(name_id);
  97. // If we have no lexical results, check all non-lexical scopes.
  98. if (lexical_results.empty()) {
  99. return {LexicalLookupHasLoadError() ? SemIR::InstId::BuiltinError
  100. : SemIR::InstId::Invalid,
  101. non_lexical_scope_stack_};
  102. }
  103. // Find the first non-lexical scope that is within the scope of the lexical
  104. // lookup result.
  105. auto* first_non_lexical_scope = std::lower_bound(
  106. non_lexical_scope_stack_.begin(), non_lexical_scope_stack_.end(),
  107. lexical_results.back().scope_index,
  108. [](const NonLexicalScope& scope, ScopeIndex index) {
  109. return scope.scope_index < index;
  110. });
  111. return {
  112. lexical_results.back().inst_id,
  113. llvm::ArrayRef(first_non_lexical_scope, non_lexical_scope_stack_.end())};
  114. }
  115. auto ScopeStack::LookupOrAddName(SemIR::NameId name_id, SemIR::InstId target_id)
  116. -> SemIR::InstId {
  117. if (!scope_stack_.back().names.Insert(name_id).is_inserted()) {
  118. auto existing = lexical_lookup_.Get(name_id).back().inst_id;
  119. CARBON_CHECK(existing.is_valid(),
  120. "Name in scope but not in lexical lookups");
  121. return existing;
  122. }
  123. ++scope_stack_.back().num_names;
  124. // TODO: Reject if we previously performed a failed lookup for this name
  125. // in this scope or a scope nested within it.
  126. auto& lexical_results = lexical_lookup_.Get(name_id);
  127. CARBON_CHECK(
  128. lexical_results.empty() ||
  129. lexical_results.back().scope_index < PeekIndex(),
  130. "Failed to clean up after scope nested within the current scope");
  131. lexical_results.push_back({.inst_id = target_id, .scope_index = PeekIndex()});
  132. return SemIR::InstId::Invalid;
  133. }
  134. auto ScopeStack::SetReturnedVarOrGetExisting(SemIR::InstId inst_id)
  135. -> SemIR::InstId {
  136. CARBON_CHECK(!return_scope_stack_.empty(), "`returned var` in no function");
  137. auto& returned_var = return_scope_stack_.back().returned_var;
  138. if (returned_var.is_valid()) {
  139. return returned_var;
  140. }
  141. returned_var = inst_id;
  142. CARBON_CHECK(!scope_stack_.back().has_returned_var,
  143. "Scope has returned var but none is set");
  144. if (inst_id.is_valid()) {
  145. scope_stack_.back().has_returned_var = true;
  146. }
  147. return SemIR::InstId::Invalid;
  148. }
  149. auto ScopeStack::Suspend() -> SuspendedScope {
  150. CARBON_CHECK(!scope_stack_.empty(), "No scope to suspend");
  151. SuspendedScope result = {.entry = scope_stack_.pop_back_val(),
  152. .suspended_items = {}};
  153. if (result.entry.scope_id.is_valid()) {
  154. non_lexical_scope_stack_.pop_back();
  155. }
  156. auto peek_compile_time_bindings = compile_time_binding_stack_.PeekArray();
  157. result.suspended_items.reserve(result.entry.num_names +
  158. peek_compile_time_bindings.size());
  159. result.entry.names.ForEach([&](SemIR::NameId name_id) {
  160. auto [index, inst_id] = lexical_lookup_.Suspend(name_id);
  161. CARBON_CHECK(index !=
  162. SuspendedScope::ScopeItem::IndexForCompileTimeBinding);
  163. result.suspended_items.push_back({.index = index, .inst_id = inst_id});
  164. });
  165. CARBON_CHECK(static_cast<int>(result.suspended_items.size()) ==
  166. result.entry.num_names);
  167. // Move any compile-time bindings into the suspended scope.
  168. for (auto inst_id : peek_compile_time_bindings) {
  169. result.suspended_items.push_back(
  170. {.index = SuspendedScope::ScopeItem::IndexForCompileTimeBinding,
  171. .inst_id = inst_id});
  172. }
  173. compile_time_binding_stack_.PopArray();
  174. // This would be easy to support if we had a need, but currently we do not.
  175. CARBON_CHECK(!result.entry.has_returned_var,
  176. "Should not suspend a scope with a returned var.");
  177. return result;
  178. }
  179. auto ScopeStack::Restore(SuspendedScope scope) -> void {
  180. compile_time_binding_stack_.PushArray();
  181. for (auto [index, inst_id] : scope.suspended_items) {
  182. if (index == SuspendedScope::ScopeItem::IndexForCompileTimeBinding) {
  183. compile_time_binding_stack_.AppendToTop(inst_id);
  184. } else {
  185. lexical_lookup_.Restore({.index = index, .inst_id = inst_id},
  186. scope.entry.index);
  187. }
  188. }
  189. CARBON_CHECK(
  190. scope.entry.next_compile_time_bind_index.index ==
  191. static_cast<int32_t>(compile_time_binding_stack_.all_values_size()),
  192. "Wrong number of entries in compile-time binding stack when restoring, "
  193. "have {0}, expected {1}",
  194. compile_time_binding_stack_.all_values_size(),
  195. scope.entry.next_compile_time_bind_index.index);
  196. if (scope.entry.scope_id.is_valid()) {
  197. non_lexical_scope_stack_.push_back(
  198. {.scope_index = scope.entry.index,
  199. .name_scope_id = scope.entry.scope_id,
  200. .specific_id = scope.entry.specific_id});
  201. }
  202. scope_stack_.push_back(std::move(scope.entry));
  203. }
  204. } // namespace Carbon::Check