scope_stack.cpp 8.5 KB

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