scope_stack.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. .prev_lexical_lookup_has_load_error = lexical_lookup_has_load_error_});
  18. if (scope_id.is_valid()) {
  19. non_lexical_scope_stack_.push_back({next_scope_index_, scope_id});
  20. }
  21. lexical_lookup_has_load_error_ |= lexical_lookup_has_load_error;
  22. // TODO: Handle this case more gracefully.
  23. CARBON_CHECK(next_scope_index_.index != std::numeric_limits<int32_t>::max())
  24. << "Ran out of scopes";
  25. ++next_scope_index_.index;
  26. }
  27. auto ScopeStack::Pop() -> void {
  28. auto scope = scope_stack_.pop_back_val();
  29. lexical_lookup_has_load_error_ = scope.prev_lexical_lookup_has_load_error;
  30. for (const auto& str_id : scope.names) {
  31. auto& lexical_results = lexical_lookup_.Get(str_id);
  32. CARBON_CHECK(lexical_results.back().scope_index == scope.index)
  33. << "Inconsistent scope index for name " << str_id;
  34. lexical_results.pop_back();
  35. }
  36. if (scope.scope_id.is_valid()) {
  37. CARBON_CHECK(non_lexical_scope_stack_.back().scope_index == scope.index);
  38. non_lexical_scope_stack_.pop_back();
  39. }
  40. if (scope.has_returned_var) {
  41. CARBON_CHECK(!return_scope_stack_.empty());
  42. CARBON_CHECK(return_scope_stack_.back().returned_var.is_valid());
  43. return_scope_stack_.back().returned_var = SemIR::InstId::Invalid;
  44. }
  45. }
  46. auto ScopeStack::PopTo(ScopeIndex index) -> void {
  47. while (PeekIndex() > index) {
  48. Pop();
  49. }
  50. CARBON_CHECK(PeekIndex() == index)
  51. << "Scope index " << index << " does not enclose the current scope "
  52. << PeekIndex();
  53. }
  54. auto ScopeStack::LookupInCurrentScope(SemIR::NameId name_id) -> SemIR::InstId {
  55. auto& lexical_results = lexical_lookup_.Get(name_id);
  56. if (lexical_results.empty()) {
  57. return SemIR::InstId::Invalid;
  58. }
  59. auto result = lexical_results.back();
  60. if (result.scope_index != PeekIndex()) {
  61. return SemIR::InstId::Invalid;
  62. }
  63. return result.inst_id;
  64. }
  65. auto ScopeStack::LookupInEnclosingScopes(SemIR::NameId name_id)
  66. -> std::pair<SemIR::InstId, llvm::ArrayRef<NonLexicalScope>> {
  67. // Find the results from enclosing lexical scopes. These will be combined with
  68. // results from non-lexical scopes such as namespaces and classes.
  69. llvm::ArrayRef<LexicalLookup::Result> lexical_results =
  70. lexical_lookup_.Get(name_id);
  71. // If we have no lexical results, check all non-lexical scopes.
  72. if (lexical_results.empty()) {
  73. return {lexical_lookup_has_load_error_ ? SemIR::InstId::BuiltinError
  74. : SemIR::InstId::Invalid,
  75. non_lexical_scope_stack_};
  76. }
  77. // Find the first non-lexical scope that is within the scope of the lexical
  78. // lookup result.
  79. auto* first_non_lexical_scope = std::lower_bound(
  80. non_lexical_scope_stack_.begin(), non_lexical_scope_stack_.end(),
  81. lexical_results.back().scope_index,
  82. [](const NonLexicalScope& scope, ScopeIndex index) {
  83. return scope.scope_index < index;
  84. });
  85. return {
  86. lexical_results.back().inst_id,
  87. llvm::ArrayRef(first_non_lexical_scope, non_lexical_scope_stack_.end())};
  88. }
  89. auto ScopeStack::LookupOrAddName(SemIR::NameId name_id, SemIR::InstId target_id)
  90. -> SemIR::InstId {
  91. if (!scope_stack_.back().names.insert(name_id).second) {
  92. auto existing = lexical_lookup_.Get(name_id).back().inst_id;
  93. CARBON_CHECK(existing.is_valid())
  94. << "Name in scope but not in lexical lookups";
  95. return existing;
  96. }
  97. // TODO: Reject if we previously performed a failed lookup for this name
  98. // in this scope or a scope nested within it.
  99. auto& lexical_results = lexical_lookup_.Get(name_id);
  100. CARBON_CHECK(lexical_results.empty() ||
  101. lexical_results.back().scope_index < PeekIndex())
  102. << "Failed to clean up after scope nested within the current scope";
  103. lexical_results.push_back({.inst_id = target_id, .scope_index = PeekIndex()});
  104. return SemIR::InstId::Invalid;
  105. }
  106. auto ScopeStack::SetReturnedVarOrGetExisting(SemIR::InstId inst_id)
  107. -> SemIR::InstId {
  108. CARBON_CHECK(!return_scope_stack_.empty()) << "`returned var` in no function";
  109. auto& returned_var = return_scope_stack_.back().returned_var;
  110. if (returned_var.is_valid()) {
  111. return returned_var;
  112. }
  113. returned_var = inst_id;
  114. CARBON_CHECK(!scope_stack_.back().has_returned_var)
  115. << "Scope has returned var but none is set";
  116. if (inst_id.is_valid()) {
  117. scope_stack_.back().has_returned_var = true;
  118. }
  119. return SemIR::InstId::Invalid;
  120. }
  121. } // namespace Carbon::Check