full_pattern_stack.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_FULL_PATTERN_STACK_H_
  5. #define CARBON_TOOLCHAIN_CHECK_FULL_PATTERN_STACK_H_
  6. #include "common/array_stack.h"
  7. #include "common/check.h"
  8. #include "toolchain/check/lexical_lookup.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. namespace Carbon::Check {
  11. // Stack of full-patterns currently being checked (a full-pattern is a pattern
  12. // that is not part of an enclosing pattern). It is structured as a stack to
  13. // handle situations like a pattern that contains an initializer, or a pattern
  14. // in a lambda in an expression pattern.
  15. //
  16. // When a pattern is followed by an explicit initializer, name bindings should
  17. // not be used within that initializer, although they are usable before it
  18. // (within the pattern) and after it. This class keeps track of those state
  19. // transitions, as well as the kind of full-pattern (e.g. parameter list or name
  20. // binding pattern).
  21. //
  22. // TODO: Unify this with Context::pattern_block_stack, or differentiate them
  23. // more clearly (and consider unifying this with ScopeStack instead).
  24. class FullPatternStack {
  25. public:
  26. explicit FullPatternStack(LexicalLookup* lookup) : lookup_(lookup) {}
  27. // The kind of a full-pattern. There are two primary kinds: name binding
  28. // declarations and parameterized entity declarations. However, for efficiency
  29. // we also use this enum to track state transitions within a parameterized
  30. // entity declaration. A parameterized entity declaration always starts and
  31. // finishes in the `NotInEitherParamList` state, and can transition to either
  32. // the `ImplicitParamList` or `ExplicitParamList` state, and then back to the
  33. // `NotInEitherParamList` state.
  34. enum class Kind {
  35. // A name-binding declaration, such as a `let` or `var` statement.
  36. NameBindingDecl,
  37. // The implicit parameter list of a function or impl declaration.
  38. ImplicitParamList,
  39. // The explicit parameter list of a function declaration.
  40. ExplicitParamList,
  41. // This kind indicates that we're within the declaration of a parameterized
  42. // entity (such as a function or impl), but not within an explicit or
  43. // implicit parameter list. This is primarily useful for the return part of
  44. // a function declaration, which doesn't contain pattern syntax, but can
  45. // implicitly introduce output parameter patterns. However, the parse tree
  46. // doesn't let us reliably distinguish the return part from the part before
  47. // the parameter lists (or between them), particularly in the case where
  48. // there is no explicit parameter list.
  49. NotInEitherParamList
  50. };
  51. auto empty() const -> bool { return kind_stack_.empty(); }
  52. // The kind of the current full-pattern.
  53. auto CurrentKind() const -> Kind { return kind_stack_.back(); }
  54. // Marks the start of a new full-pattern for a parameterized entity
  55. // declaration, such as a function or impl. The kind is initially
  56. // NotInEitherParamList.
  57. auto PushParameterizedDecl() -> void {
  58. kind_stack_.push_back(Kind::NotInEitherParamList);
  59. bind_name_stack_.PushArray();
  60. }
  61. // Marks the start of a new full-pattern for a name binding declaration.
  62. auto PushNameBindingDecl() -> void {
  63. kind_stack_.push_back(Kind::NameBindingDecl);
  64. bind_name_stack_.PushArray();
  65. }
  66. // Marks the start of the current parameterized entity's implicit parameter
  67. // list.
  68. auto StartImplicitParamList() -> void {
  69. CARBON_CHECK(kind_stack_.back() == Kind::NotInEitherParamList, "{0}",
  70. kind_stack_.back());
  71. kind_stack_.back() = Kind::ImplicitParamList;
  72. }
  73. // Marks the end of the current parameterized entity's implicit parameter
  74. // list.
  75. auto EndImplicitParamList() -> void {
  76. CARBON_CHECK(kind_stack_.back() == Kind::ImplicitParamList, "{0}",
  77. kind_stack_.back());
  78. kind_stack_.back() = Kind::NotInEitherParamList;
  79. }
  80. // Marks the start of the current parameterized entity's explicit parameter
  81. // list.
  82. auto StartExplicitParamList() -> void {
  83. CARBON_CHECK(kind_stack_.back() == Kind::NotInEitherParamList, "{0}",
  84. kind_stack_.back());
  85. kind_stack_.back() = Kind::ExplicitParamList;
  86. }
  87. // Marks the end of the current parameterized entity's explicit parameter
  88. // list.
  89. auto EndExplicitParamList() -> void {
  90. CARBON_CHECK(kind_stack_.back() == Kind::ExplicitParamList, "{0}",
  91. kind_stack_.back());
  92. kind_stack_.back() = Kind::NotInEitherParamList;
  93. }
  94. // Marks the start of the initializer for the current name binding decl.
  95. auto StartPatternInitializer() -> void {
  96. CARBON_CHECK(kind_stack_.back() == Kind::NameBindingDecl);
  97. for (auto& [name_id, inst_id] : bind_name_stack_.PeekArray()) {
  98. CARBON_CHECK(inst_id == SemIR::InstId::InitTombstone);
  99. auto& lookup_result = lookup_->Get(name_id);
  100. if (!lookup_result.empty()) {
  101. // TODO: find a way to preserve location information, so that we can
  102. // provide good diagnostics for a redeclaration of `name_id` in
  103. // the initializer, if that becomes possible.
  104. std::swap(lookup_result.back().inst_id, inst_id);
  105. }
  106. }
  107. }
  108. // Marks the end of the initializer for the current name-binding decl.
  109. auto EndPatternInitializer() -> void {
  110. for (auto& [name_id, inst_id] : bind_name_stack_.PeekArray()) {
  111. auto& lookup_result = lookup_->Get(name_id);
  112. if (!lookup_result.empty()) {
  113. std::swap(lookup_result.back().inst_id, inst_id);
  114. }
  115. CARBON_CHECK(inst_id == SemIR::InstId::InitTombstone);
  116. }
  117. }
  118. // Marks the end of checking for the current full-pattern. This cannot be
  119. // called while processing an initializer for the top pattern.
  120. auto PopFullPattern() -> void {
  121. kind_stack_.pop_back();
  122. bind_name_stack_.PopArray();
  123. }
  124. // Records that `name_id` was introduced by the current full-pattern.
  125. auto AddBindName(SemIR::NameId name_id) -> void {
  126. bind_name_stack_.AppendToTop(
  127. {.name_id = name_id, .inst_id = SemIR::InstId::InitTombstone});
  128. }
  129. // Runs verification that the processing cleanly finished.
  130. auto VerifyOnFinish() const -> void {
  131. CARBON_CHECK(kind_stack_.empty(),
  132. "full_pattern_stack still has {0} entries",
  133. kind_stack_.size());
  134. }
  135. private:
  136. LexicalLookup* lookup_;
  137. llvm::SmallVector<Kind> kind_stack_;
  138. struct LookupEntry {
  139. SemIR::NameId name_id;
  140. SemIR::InstId inst_id;
  141. };
  142. ArrayStack<LookupEntry> bind_name_stack_;
  143. };
  144. } // namespace Carbon::Check
  145. #endif // CARBON_TOOLCHAIN_CHECK_FULL_PATTERN_STACK_H_