scope_stack.cpp 15 KB

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