scope_stack.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #ifndef CARBON_TOOLCHAIN_CHECK_SCOPE_STACK_H_
  5. #define CARBON_TOOLCHAIN_CHECK_SCOPE_STACK_H_
  6. #include "llvm/ADT/DenseSet.h"
  7. #include "llvm/ADT/SmallVector.h"
  8. #include "toolchain/check/lexical_lookup.h"
  9. #include "toolchain/check/scope_index.h"
  10. #include "toolchain/sem_ir/file.h"
  11. #include "toolchain/sem_ir/ids.h"
  12. namespace Carbon::Check {
  13. // A stack of lexical and semantic scopes that we are currently performing
  14. // checking within.
  15. class ScopeStack {
  16. public:
  17. explicit ScopeStack(const StringStoreWrapper<IdentifierId>& identifiers)
  18. : lexical_lookup_(identifiers) {}
  19. // A scope in which `break` and `continue` can be used.
  20. struct BreakContinueScope {
  21. SemIR::InstBlockId break_target;
  22. SemIR::InstBlockId continue_target;
  23. };
  24. // A scope in which `return` can be used.
  25. struct ReturnScope {
  26. // The declaration from which we can return. Inside a function, this will
  27. // be a `FunctionDecl`.
  28. SemIR::InstId decl_id;
  29. // The value corresponding to the current `returned var`, if any. Will be
  30. // set and unset as `returned var`s are declared and go out of scope.
  31. SemIR::InstId returned_var = SemIR::InstId::Invalid;
  32. };
  33. // A non-lexical scope in which unqualified lookup may be required.
  34. struct NonLexicalScope {
  35. // The index of the scope in the scope stack.
  36. ScopeIndex scope_index;
  37. // The corresponding name scope.
  38. SemIR::NameScopeId name_scope_id;
  39. };
  40. // Pushes a scope onto scope_stack_. NameScopeId::Invalid is used for new
  41. // scopes. lexical_lookup_has_load_error is used to limit diagnostics when a
  42. // given namespace may contain a mix of both successful and failed name
  43. // imports.
  44. auto Push(SemIR::InstId scope_inst_id = SemIR::InstId::Invalid,
  45. SemIR::NameScopeId scope_id = SemIR::NameScopeId::Invalid,
  46. bool lexical_lookup_has_load_error = false) -> void;
  47. // Pops the top scope from scope_stack_, cleaning up names from
  48. // lexical_lookup_.
  49. auto Pop() -> void;
  50. // Pops the top scope from scope_stack_ if it contains no names.
  51. auto PopIfEmpty() -> void {
  52. if (scope_stack_.back().names.empty()) {
  53. Pop();
  54. }
  55. }
  56. // Pops scopes until we return to the specified scope index.
  57. auto PopTo(ScopeIndex index) -> void;
  58. // Returns the scope index associated with the current scope.
  59. auto PeekIndex() const -> ScopeIndex { return Peek().index; }
  60. // Returns the name scope associated with the current lexical scope, if any.
  61. auto PeekNameScopeId() const -> SemIR::NameScopeId { return Peek().scope_id; }
  62. // Returns the instruction associated with the current scope, or Invalid if
  63. // there is no such instruction, such as for a block scope.
  64. auto PeekInstId() const -> SemIR::InstId { return Peek().scope_inst_id; }
  65. // Returns the current scope, if it is of the specified kind. Otherwise,
  66. // returns nullopt.
  67. template <typename InstT>
  68. auto GetCurrentScopeAs(const SemIR::File& sem_ir) -> std::optional<InstT> {
  69. auto inst_id = PeekInstId();
  70. if (!inst_id.is_valid()) {
  71. return std::nullopt;
  72. }
  73. return sem_ir.insts().TryGetAs<InstT>(inst_id);
  74. }
  75. // If there is no `returned var` in scope, sets the given instruction to be
  76. // the current `returned var` and returns an invalid instruction ID. If there
  77. // is already a `returned var`, returns it instead.
  78. auto SetReturnedVarOrGetExisting(SemIR::InstId inst_id) -> SemIR::InstId;
  79. // Looks up the name `name_id` in the current scope. Returns the existing
  80. // lookup result, if any.
  81. auto LookupInCurrentScope(SemIR::NameId name_id) -> SemIR::InstId;
  82. // Looks up the name `name_id` in the current scope and its enclosing scopes.
  83. // Returns the innermost lexical lookup result, if any, along with a list of
  84. // non-lexical scopes in which lookup should also be performed, ordered from
  85. // outermost to innermost.
  86. auto LookupInEnclosingScopes(SemIR::NameId name_id)
  87. -> std::pair<SemIR::InstId, llvm::ArrayRef<NonLexicalScope>>;
  88. // Looks up the name `name_id` in the current scope. Returns the existing
  89. // instruction if any, and otherwise adds the name with the value `target_id`
  90. // and returns Invalid.
  91. auto LookupOrAddName(SemIR::NameId name_id, SemIR::InstId target_id)
  92. -> SemIR::InstId;
  93. // Runs verification that the processing cleanly finished.
  94. auto VerifyOnFinish() -> void;
  95. auto return_scope_stack() -> llvm::SmallVector<ReturnScope>& {
  96. return return_scope_stack_;
  97. }
  98. auto break_continue_stack() -> llvm::SmallVector<BreakContinueScope>& {
  99. return break_continue_stack_;
  100. }
  101. private:
  102. // An entry in scope_stack_.
  103. struct ScopeStackEntry {
  104. // The sequential index of this scope entry within the file.
  105. ScopeIndex index;
  106. // The instruction associated with this entry, if any. This can be one of:
  107. //
  108. // - A `ClassDecl`, for a class definition scope.
  109. // - A `FunctionDecl`, for the outermost scope in a function
  110. // definition.
  111. // - Invalid, for any other scope.
  112. SemIR::InstId scope_inst_id;
  113. // The name scope associated with this entry, if any.
  114. SemIR::NameScopeId scope_id;
  115. // The previous state of lexical_lookup_has_load_error_, restored on pop.
  116. bool prev_lexical_lookup_has_load_error;
  117. // Names which are registered with lexical_lookup_, and will need to be
  118. // unregistered when the scope ends.
  119. llvm::DenseSet<SemIR::NameId> names;
  120. // Whether a `returned var` was introduced in this scope, and needs to be
  121. // unregistered when the scope ends.
  122. bool has_returned_var = false;
  123. // TODO: This likely needs to track things which need to be destructed.
  124. };
  125. auto Peek() const -> const ScopeStackEntry& { return scope_stack_.back(); }
  126. // A stack of scopes from which we can `return`.
  127. llvm::SmallVector<ReturnScope> return_scope_stack_;
  128. // A stack of `break` and `continue` targets.
  129. llvm::SmallVector<BreakContinueScope> break_continue_stack_;
  130. // A stack for scope context.
  131. llvm::SmallVector<ScopeStackEntry> scope_stack_;
  132. // Information about non-lexical scopes. This is a subset of the entries and
  133. // the information in scope_stack_.
  134. llvm::SmallVector<NonLexicalScope> non_lexical_scope_stack_;
  135. // The index of the next scope that will be pushed onto scope_stack_. The
  136. // first is always the package scope.
  137. ScopeIndex next_scope_index_ = ScopeIndex::Package;
  138. // Tracks lexical lookup results.
  139. LexicalLookup lexical_lookup_;
  140. // Whether lexical_lookup_ has load errors, updated whenever scope_stack_ is
  141. // pushed or popped.
  142. bool lexical_lookup_has_load_error_ = false;
  143. };
  144. } // namespace Carbon::Check
  145. #endif // CARBON_TOOLCHAIN_CHECK_SCOPE_STACK_H_