full_pattern_stack.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. }
  43. // Marks the end of an implicit parameter list, and the presumptive start
  44. // of the corresponding explicit parameter list.
  45. auto EndImplicitParamList() -> void {
  46. CARBON_CHECK(kind_stack_.back() == Kind::ImplicitParamList, "{0}",
  47. kind_stack_.back());
  48. kind_stack_.back() = Kind::ExplicitParamList;
  49. }
  50. // Marks the start of the initializer for the current name binding decl.
  51. auto StartPatternInitializer() -> void {
  52. CARBON_CHECK(kind_stack_.back() == Kind::NameBindingDecl);
  53. for (auto& [name_id, inst_id] : bind_name_stack_.PeekArray()) {
  54. CARBON_CHECK(inst_id == SemIR::InstId::InitTombstone);
  55. auto& lookup_result = lookup_->Get(name_id);
  56. if (!lookup_result.empty()) {
  57. // TODO: find a way to preserve location information, so that we can
  58. // provide good diagnostics for a redeclaration of `name_id` in
  59. // the initializer, if that becomes possible.
  60. std::swap(lookup_result.back().inst_id, inst_id);
  61. }
  62. }
  63. }
  64. // Marks the end of the initializer for the current name-binding decl.
  65. auto EndPatternInitializer() -> void {
  66. for (auto& [name_id, inst_id] : bind_name_stack_.PeekArray()) {
  67. auto& lookup_result = lookup_->Get(name_id);
  68. if (!lookup_result.empty()) {
  69. std::swap(lookup_result.back().inst_id, inst_id);
  70. }
  71. CARBON_CHECK(inst_id == SemIR::InstId::InitTombstone);
  72. }
  73. }
  74. // Marks the end of checking for the current full-pattern. This cannot be
  75. // called while processing an initializer for the top pattern.
  76. auto PopFullPattern() -> void {
  77. kind_stack_.pop_back();
  78. bind_name_stack_.PopArray();
  79. }
  80. // Records that `name_id` was introduced by the current full-pattern.
  81. auto AddBindName(SemIR::NameId name_id) -> void {
  82. bind_name_stack_.AppendToTop(
  83. {.name_id = name_id, .inst_id = SemIR::InstId::InitTombstone});
  84. }
  85. // Runs verification that the processing cleanly finished.
  86. auto VerifyOnFinish() const -> void {
  87. CARBON_CHECK(kind_stack_.empty(),
  88. "full_pattern_stack still has {1} entries",
  89. kind_stack_.size());
  90. }
  91. private:
  92. LexicalLookup* lookup_;
  93. llvm::SmallVector<Kind> kind_stack_;
  94. struct LookupEntry {
  95. SemIR::NameId name_id;
  96. SemIR::InstId inst_id;
  97. };
  98. ArrayStack<LookupEntry> bind_name_stack_;
  99. };
  100. } // namespace Carbon::Check
  101. #endif // CARBON_TOOLCHAIN_CHECK_FULL_PATTERN_STACK_H_