scope_stack.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 <utility>
  6. #include "common/check.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. namespace Carbon::Check {
  9. auto ScopeStack::VerifyOnFinish() const -> void {
  10. CARBON_CHECK(return_scope_stack_.empty(), "{0}", return_scope_stack_.size());
  11. CARBON_CHECK(break_continue_stack_.empty(), "{0}",
  12. break_continue_stack_.size());
  13. CARBON_CHECK(scope_stack_.empty(), "{0}", scope_stack_.size());
  14. CARBON_CHECK(destroy_id_stack_.empty(), "{0}",
  15. destroy_id_stack_.all_values_size());
  16. CARBON_CHECK(non_lexical_scope_stack_.empty(), "{0}",
  17. non_lexical_scope_stack_.size());
  18. CARBON_CHECK(compile_time_binding_stack_.empty(), "{0}",
  19. compile_time_binding_stack_.all_values_size());
  20. full_pattern_stack_.VerifyOnFinish();
  21. }
  22. auto ScopeStack::VerifyNextCompileTimeBindIndex(llvm::StringLiteral label,
  23. const ScopeStackEntry& scope)
  24. -> void {
  25. CARBON_CHECK(
  26. static_cast<int32_t>(compile_time_binding_stack_.all_values_size()) ==
  27. scope.next_compile_time_bind_index.index,
  28. "Wrong number of entries in compile-time binding stack after {0}: have "
  29. "{1}, expected {2}",
  30. label, compile_time_binding_stack_.all_values_size(),
  31. scope.next_compile_time_bind_index.index);
  32. }
  33. auto ScopeStack::Push(SemIR::InstId scope_inst_id, SemIR::NameScopeId scope_id,
  34. SemIR::SpecificId specific_id,
  35. bool lexical_lookup_has_load_error) -> void {
  36. // If this scope doesn't have a specific of its own, it lives in the enclosing
  37. // scope's specific, if any.
  38. auto enclosing_specific_id = specific_id;
  39. if (!specific_id.has_value() && !scope_stack_.empty()) {
  40. enclosing_specific_id = PeekSpecificId();
  41. }
  42. compile_time_binding_stack_.PushArray();
  43. scope_stack_.push_back(
  44. {.index = next_scope_index_,
  45. .scope_inst_id = scope_inst_id,
  46. .scope_id = scope_id,
  47. .specific_id = enclosing_specific_id,
  48. .next_compile_time_bind_index = SemIR::CompileTimeBindIndex(
  49. compile_time_binding_stack_.all_values_size()),
  50. .lexical_lookup_has_load_error =
  51. LexicalLookupHasLoadError() || lexical_lookup_has_load_error});
  52. if (scope_stack_.back().is_lexical_scope()) {
  53. // For lexical lookups, unqualified lookup doesn't know how to find the
  54. // associated specific, so if we start adding lexical scopes associated with
  55. // specifics, we'll need to somehow track them in lookup.
  56. CARBON_CHECK(!specific_id.has_value(),
  57. "Lexical scope should not have an associated specific.");
  58. } else {
  59. non_lexical_scope_stack_.push_back({.scope_index = next_scope_index_,
  60. .name_scope_id = scope_id,
  61. .specific_id = enclosing_specific_id});
  62. }
  63. // TODO: Handle this case more gracefully.
  64. CARBON_CHECK(next_scope_index_.index != std::numeric_limits<int32_t>::max(),
  65. "Ran out of scopes");
  66. ++next_scope_index_.index;
  67. VerifyNextCompileTimeBindIndex("Push", scope_stack_.back());
  68. }
  69. auto ScopeStack::PushForDeclName() -> void {
  70. Push(SemIR::InstId::None, SemIR::NameScopeId::None, SemIR::SpecificId::None,
  71. /*lexical_lookup_has_load_error=*/false);
  72. MarkNestingIfInReturnScope();
  73. }
  74. auto ScopeStack::PushForEntity(SemIR::InstId scope_inst_id,
  75. SemIR::NameScopeId scope_id,
  76. SemIR::SpecificId specific_id,
  77. bool lexical_lookup_has_load_error) -> void {
  78. CARBON_CHECK(scope_inst_id.has_value());
  79. CARBON_DCHECK(!sem_ir_->insts().Is<SemIR::FunctionDecl>(scope_inst_id));
  80. Push(scope_inst_id, scope_id, specific_id, lexical_lookup_has_load_error);
  81. MarkNestingIfInReturnScope();
  82. }
  83. auto ScopeStack::PushForSameRegion() -> void {
  84. Push(SemIR::InstId::None, SemIR::NameScopeId::None, SemIR::SpecificId::None,
  85. /*lexical_lookup_has_load_error=*/false);
  86. }
  87. auto ScopeStack::PushForFunctionBody(SemIR::InstId scope_inst_id) -> void {
  88. CARBON_DCHECK(sem_ir_->insts().Is<SemIR::FunctionDecl>(scope_inst_id));
  89. Push(scope_inst_id, SemIR::NameScopeId::None, SemIR::SpecificId::None,
  90. /*lexical_lookup_has_load_error=*/false);
  91. return_scope_stack_.push_back({.decl_id = scope_inst_id});
  92. destroy_id_stack_.PushArray();
  93. }
  94. auto ScopeStack::Pop() -> void {
  95. auto scope = scope_stack_.pop_back_val();
  96. scope.names.ForEach([&](SemIR::NameId str_id) {
  97. auto& lexical_results = lexical_lookup_.Get(str_id);
  98. CARBON_CHECK(lexical_results.back().scope_index == scope.index,
  99. "Inconsistent scope index for name {0}", str_id);
  100. lexical_results.pop_back();
  101. });
  102. if (!scope.is_lexical_scope()) {
  103. CARBON_CHECK(non_lexical_scope_stack_.back().scope_index == scope.index);
  104. non_lexical_scope_stack_.pop_back();
  105. }
  106. if (!return_scope_stack_.empty()) {
  107. if (scope.has_returned_var) {
  108. CARBON_CHECK(return_scope_stack_.back().returned_var.has_value());
  109. return_scope_stack_.back().returned_var = SemIR::InstId::None;
  110. }
  111. if (return_scope_stack_.back().decl_id == scope.scope_inst_id) {
  112. // Leaving the function scope.
  113. return_scope_stack_.pop_back();
  114. destroy_id_stack_.PopArray();
  115. } else if (return_scope_stack_.back().nested_scope_index == scope.index) {
  116. // Returned to a function scope from a non-function nested entity scope.
  117. return_scope_stack_.back().nested_scope_index = ScopeIndex::None;
  118. }
  119. } else {
  120. CARBON_CHECK(!scope.has_returned_var);
  121. }
  122. VerifyNextCompileTimeBindIndex("Pop", scope);
  123. compile_time_binding_stack_.PopArray();
  124. }
  125. auto ScopeStack::PopTo(ScopeIndex index) -> void {
  126. while (PeekIndex() > index) {
  127. Pop();
  128. }
  129. CARBON_CHECK(PeekIndex() == index,
  130. "Scope index {0} does not enclose the current scope {1}", index,
  131. PeekIndex());
  132. }
  133. auto ScopeStack::LookupInLexicalScopesWithin(SemIR::NameId name_id,
  134. ScopeIndex scope_index)
  135. -> SemIR::InstId {
  136. auto& lexical_results = lexical_lookup_.Get(name_id);
  137. if (lexical_results.empty()) {
  138. return SemIR::InstId::None;
  139. }
  140. auto result = lexical_results.back();
  141. if (result.scope_index < scope_index) {
  142. return SemIR::InstId::None;
  143. }
  144. return result.inst_id;
  145. }
  146. auto ScopeStack::LookupInLexicalScopes(SemIR::NameId name_id)
  147. -> std::pair<SemIR::InstId, llvm::ArrayRef<NonLexicalScope>> {
  148. // Find the results from lexical scopes. These will be combined with results
  149. // from non-lexical scopes such as namespaces and classes.
  150. llvm::ArrayRef<LexicalLookup::Result> lexical_results =
  151. lexical_lookup_.Get(name_id);
  152. // If we have no lexical results, check all non-lexical scopes.
  153. if (lexical_results.empty()) {
  154. return {LexicalLookupHasLoadError() ? SemIR::ErrorInst::InstId
  155. : SemIR::InstId::None,
  156. non_lexical_scope_stack_};
  157. }
  158. // Find the first non-lexical scope that is within the scope of the lexical
  159. // lookup result.
  160. auto* first_non_lexical_scope = llvm::lower_bound(
  161. non_lexical_scope_stack_, lexical_results.back().scope_index,
  162. [](const NonLexicalScope& scope, ScopeIndex index) {
  163. return scope.scope_index < index;
  164. });
  165. return {
  166. lexical_results.back().inst_id,
  167. llvm::ArrayRef(first_non_lexical_scope, non_lexical_scope_stack_.end())};
  168. }
  169. auto ScopeStack::LookupOrAddName(SemIR::NameId name_id, SemIR::InstId target_id,
  170. ScopeIndex scope_index) -> SemIR::InstId {
  171. // Find the corresponding scope depth.
  172. //
  173. // TODO: Consider passing in the depth rather than performing a scan for it.
  174. // We only do this scan when declaring an entity such as a class within a
  175. // function, so it should be relatively rare, but it's still not necesasry to
  176. // recompute this.
  177. int scope_depth = scope_stack_.size() - 1;
  178. if (scope_index.has_value()) {
  179. scope_depth =
  180. llvm::lower_bound(scope_stack_, scope_index,
  181. [](const ScopeStackEntry& entry, ScopeIndex index) {
  182. return entry.index < index;
  183. }) -
  184. scope_stack_.begin();
  185. CARBON_CHECK(scope_stack_[scope_depth].index == scope_index,
  186. "Declaring name in scope that has already ended");
  187. } else {
  188. scope_index = scope_stack_[scope_depth].index;
  189. }
  190. // If this name has already been declared in this scope or an inner scope,
  191. // return the existing result.
  192. auto& lexical_results = lexical_lookup_.Get(name_id);
  193. if (!lexical_results.empty() &&
  194. lexical_results.back().scope_index >= scope_index) {
  195. return lexical_results.back().inst_id;
  196. }
  197. // Add the name into the scope.
  198. bool inserted = scope_stack_[scope_depth].names.Insert(name_id).is_inserted();
  199. CARBON_CHECK(inserted, "Name in scope but not in lexical lookups");
  200. ++scope_stack_[scope_depth].num_names;
  201. // Add a corresponding lexical lookup result.
  202. lexical_results.push_back({.inst_id = target_id, .scope_index = scope_index});
  203. return SemIR::InstId::None;
  204. }
  205. auto ScopeStack::SetReturnedVarOrGetExisting(SemIR::InstId inst_id)
  206. -> SemIR::InstId {
  207. CARBON_CHECK(!return_scope_stack_.empty(), "`returned var` in no function");
  208. auto& returned_var = return_scope_stack_.back().returned_var;
  209. if (returned_var.has_value()) {
  210. return returned_var;
  211. }
  212. returned_var = inst_id;
  213. CARBON_CHECK(!scope_stack_.back().has_returned_var,
  214. "Scope has returned var but none is set");
  215. if (inst_id.has_value()) {
  216. scope_stack_.back().has_returned_var = true;
  217. }
  218. return SemIR::InstId::None;
  219. }
  220. auto ScopeStack::Suspend() -> SuspendedScope {
  221. CARBON_CHECK(!scope_stack_.empty(), "No scope to suspend");
  222. SuspendedScope result = {.entry = scope_stack_.pop_back_val(),
  223. .suspended_items = {}};
  224. if (!result.entry.is_lexical_scope()) {
  225. non_lexical_scope_stack_.pop_back();
  226. }
  227. auto peek_compile_time_bindings = compile_time_binding_stack_.PeekArray();
  228. result.suspended_items.reserve(result.entry.num_names +
  229. peek_compile_time_bindings.size());
  230. result.entry.names.ForEach([&](SemIR::NameId name_id) {
  231. auto [index, inst_id] = lexical_lookup_.Suspend(name_id);
  232. CARBON_CHECK(index !=
  233. SuspendedScope::ScopeItem::IndexForCompileTimeBinding);
  234. result.suspended_items.push_back({.index = index, .inst_id = inst_id});
  235. });
  236. CARBON_CHECK(static_cast<int>(result.suspended_items.size()) ==
  237. result.entry.num_names);
  238. // Move any compile-time bindings into the suspended scope.
  239. for (auto inst_id : peek_compile_time_bindings) {
  240. result.suspended_items.push_back(
  241. {.index = SuspendedScope::ScopeItem::IndexForCompileTimeBinding,
  242. .inst_id = inst_id});
  243. }
  244. compile_time_binding_stack_.PopArray();
  245. // This would be easy to support if we had a need, but currently we do not.
  246. CARBON_CHECK(!result.entry.has_returned_var,
  247. "Should not suspend a scope with a returned var.");
  248. return result;
  249. }
  250. auto ScopeStack::Restore(SuspendedScope&& scope) -> void {
  251. compile_time_binding_stack_.PushArray();
  252. for (auto [index, inst_id] : scope.suspended_items) {
  253. if (index == SuspendedScope::ScopeItem::IndexForCompileTimeBinding) {
  254. compile_time_binding_stack_.AppendToTop(inst_id);
  255. } else {
  256. lexical_lookup_.Restore({.index = index, .inst_id = inst_id},
  257. scope.entry.index);
  258. }
  259. }
  260. VerifyNextCompileTimeBindIndex("Restore", scope.entry);
  261. if (!scope.entry.is_lexical_scope()) {
  262. non_lexical_scope_stack_.push_back(
  263. {.scope_index = scope.entry.index,
  264. .name_scope_id = scope.entry.scope_id,
  265. .specific_id = scope.entry.specific_id});
  266. }
  267. scope_stack_.push_back(std::move(scope.entry));
  268. }
  269. } // namespace Carbon::Check