full_pattern_stack.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. Note that an implicit parameter list and
  28. // adjacent explicit parameter list together form a single full-pattern,
  29. // but we separate them here in order to represent the state transition
  30. // between them.
  31. enum class Kind {
  32. NameBindingDecl,
  33. ImplicitParamList,
  34. ExplicitParamList,
  35. };
  36. // The kind of the current full-pattern.
  37. auto CurrentKind() const -> Kind { return kind_stack_.back(); }
  38. // Marks the start of a new full-pattern of the specified kind.
  39. auto PushFullPattern(Kind kind) -> void {
  40. kind_stack_.push_back(kind);
  41. bind_name_stack_.PushArray();
  42. param_index_stack_.push_back(SemIR::CallParamIndex(0));
  43. }
  44. // Marks the end of an implicit parameter list, and the presumptive start
  45. // of the corresponding explicit parameter list.
  46. auto EndImplicitParamList() -> void {
  47. CARBON_CHECK(kind_stack_.back() == Kind::ImplicitParamList, "{0}",
  48. kind_stack_.back());
  49. kind_stack_.back() = Kind::ExplicitParamList;
  50. }
  51. // Marks the start of the initializer for the current name binding decl.
  52. auto StartPatternInitializer() -> void {
  53. CARBON_CHECK(kind_stack_.back() == Kind::NameBindingDecl);
  54. for (auto& [name_id, inst_id] : bind_name_stack_.PeekArray()) {
  55. CARBON_CHECK(inst_id == SemIR::InstId::InitTombstone);
  56. auto& lookup_result = lookup_->Get(name_id);
  57. if (!lookup_result.empty()) {
  58. // TODO: find a way to preserve location information, so that we can
  59. // provide good diagnostics for a redeclaration of `name_id` in
  60. // the initializer, if that becomes possible.
  61. std::swap(lookup_result.back().inst_id, inst_id);
  62. }
  63. }
  64. }
  65. // Marks the end of the initializer for the current name-binding decl.
  66. auto EndPatternInitializer() -> void {
  67. for (auto& [name_id, inst_id] : bind_name_stack_.PeekArray()) {
  68. auto& lookup_result = lookup_->Get(name_id);
  69. if (!lookup_result.empty()) {
  70. std::swap(lookup_result.back().inst_id, inst_id);
  71. }
  72. CARBON_CHECK(inst_id == SemIR::InstId::InitTombstone);
  73. }
  74. }
  75. // Marks the end of checking for the current full-pattern. This cannot be
  76. // called while processing an initializer for the top pattern.
  77. auto PopFullPattern() -> void {
  78. kind_stack_.pop_back();
  79. bind_name_stack_.PopArray();
  80. param_index_stack_.pop_back();
  81. }
  82. // Records that `name_id` was introduced by the current full-pattern.
  83. auto AddBindName(SemIR::NameId name_id) -> void {
  84. bind_name_stack_.AppendToTop(
  85. {.name_id = name_id, .inst_id = SemIR::InstId::InitTombstone});
  86. }
  87. // Runs verification that the processing cleanly finished.
  88. auto VerifyOnFinish() const -> void {
  89. CARBON_CHECK(kind_stack_.empty(),
  90. "full_pattern_stack still has {1} entries",
  91. kind_stack_.size());
  92. }
  93. // Allocates the next unallocated CallParamIndex, starting from 0.
  94. auto NextCallParamIndex() -> SemIR::CallParamIndex {
  95. auto result = param_index_stack_.back();
  96. ++param_index_stack_.back().index;
  97. return result;
  98. }
  99. private:
  100. LexicalLookup* lookup_;
  101. llvm::SmallVector<Kind> kind_stack_;
  102. struct LookupEntry {
  103. SemIR::NameId name_id;
  104. SemIR::InstId inst_id;
  105. };
  106. ArrayStack<LookupEntry> bind_name_stack_;
  107. llvm::SmallVector<SemIR::CallParamIndex> param_index_stack_;
  108. };
  109. } // namespace Carbon::Check
  110. #endif // CARBON_TOOLCHAIN_CHECK_FULL_PATTERN_STACK_H_