|
| 1 | +//===--- PatternBindingState.h --------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef SWIFT_PARSE_PATTERNBINDINGSTATE_H |
| 14 | +#define SWIFT_PARSE_PATTERNBINDINGSTATE_H |
| 15 | + |
| 16 | +#include "swift/AST/Decl.h" |
| 17 | +#include "swift/Parse/Token.h" |
| 18 | + |
| 19 | +namespace swift { |
| 20 | + |
| 21 | +class Token; |
| 22 | + |
| 23 | +struct PatternBindingState { |
| 24 | + enum Kind { |
| 25 | + /// InBindingPattern has this value when not parsing a pattern. |
| 26 | + NotInBinding, |
| 27 | + |
| 28 | + /// InBindingPattern has this value when we're in a matching pattern, but |
| 29 | + /// not within a var/let pattern. In this phase, identifiers are references |
| 30 | + /// to the enclosing scopes, not a variable binding. |
| 31 | + InMatchingPattern, |
| 32 | + |
| 33 | + /// InBindingPattern has this value when parsing a pattern in which bound |
| 34 | + /// variables are implicitly immutable, but allowed to be marked mutable by |
| 35 | + /// using a 'var' pattern. This happens in for-each loop patterns. |
| 36 | + ImplicitlyImmutable, |
| 37 | + |
| 38 | + /// When InBindingPattern has this value, bound variables are mutable, and |
| 39 | + /// nested let/var patterns are not permitted. This happens when parsing a |
| 40 | + /// 'var' decl or when parsing inside a 'var' pattern. |
| 41 | + InVar, |
| 42 | + |
| 43 | + /// When InBindingPattern has this value, bound variables are immutable,and |
| 44 | + /// nested let/var patterns are not permitted. This happens when parsing a |
| 45 | + /// 'let' decl or when parsing inside a 'let' pattern. |
| 46 | + InLet, |
| 47 | + |
| 48 | + /// When InBindingPattern has this value, bound variables are mutable, and |
| 49 | + /// nested let/var/inout patterns are not permitted. This happens when |
| 50 | + /// parsing an 'inout' decl or when parsing inside an 'inout' pattern. |
| 51 | + InInOut, |
| 52 | + }; |
| 53 | + |
| 54 | + Kind kind; |
| 55 | + |
| 56 | + PatternBindingState(Kind kind) : kind(kind) {} |
| 57 | + |
| 58 | + operator bool() const { return kind != Kind::NotInBinding; } |
| 59 | + |
| 60 | + operator Kind() const { return kind; } |
| 61 | + |
| 62 | + static Optional<PatternBindingState> get(StringRef str) { |
| 63 | + auto kind = llvm::StringSwitch<Kind>(str) |
| 64 | + .Case("let", Kind::InLet) |
| 65 | + .Case("var", Kind::InVar) |
| 66 | + .Case("inout", Kind::InInOut) |
| 67 | + .Default(Kind::NotInBinding); |
| 68 | + return PatternBindingState(kind); |
| 69 | + } |
| 70 | + |
| 71 | + /// Try to explicitly find the new pattern binding state from a token. |
| 72 | + explicit PatternBindingState(Token tok) : kind(NotInBinding) { |
| 73 | + switch (tok.getKind()) { |
| 74 | + case tok::kw_let: |
| 75 | + kind = InLet; |
| 76 | + break; |
| 77 | + case tok::kw_var: |
| 78 | + kind = InVar; |
| 79 | + break; |
| 80 | + case tok::kw_inout: |
| 81 | + kind = InInOut; |
| 82 | + break; |
| 83 | + default: |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// Explicitly initialize from a VarDecl::Introducer. |
| 89 | + explicit PatternBindingState(VarDecl::Introducer introducer) |
| 90 | + : kind(NotInBinding) { |
| 91 | + switch (introducer) { |
| 92 | + case VarDecl::Introducer::Let: |
| 93 | + kind = InLet; |
| 94 | + break; |
| 95 | + case VarDecl::Introducer::Var: |
| 96 | + kind = InVar; |
| 97 | + break; |
| 98 | + case VarDecl::Introducer::InOut: |
| 99 | + kind = InInOut; |
| 100 | + break; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// If there is a direct introducer associated with this pattern binding |
| 105 | + /// state, return that. Return none otherwise. |
| 106 | + Optional<VarDecl::Introducer> getIntroducer() const { |
| 107 | + switch (kind) { |
| 108 | + case Kind::NotInBinding: |
| 109 | + case Kind::InMatchingPattern: |
| 110 | + case Kind::ImplicitlyImmutable: |
| 111 | + return None; |
| 112 | + case Kind::InVar: |
| 113 | + return VarDecl::Introducer::Var; |
| 114 | + case Kind::InLet: |
| 115 | + return VarDecl::Introducer::Let; |
| 116 | + case Kind::InInOut: |
| 117 | + return VarDecl::Introducer::InOut; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + PatternBindingState |
| 122 | + getPatternBindingStateForIntroducer(VarDecl::Introducer defaultValue) { |
| 123 | + return PatternBindingState(getIntroducer().getValueOr(defaultValue)); |
| 124 | + } |
| 125 | + |
| 126 | + Optional<unsigned> getSelectIndexForIntroducer() const { |
| 127 | + switch (kind) { |
| 128 | + case Kind::InLet: |
| 129 | + return 0; |
| 130 | + case Kind::InInOut: |
| 131 | + return 1; |
| 132 | + case Kind::InVar: |
| 133 | + return 2; |
| 134 | + default: |
| 135 | + return None; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + bool isLet() const { return kind == Kind::InLet; } |
| 140 | +}; |
| 141 | + |
| 142 | +} // namespace swift |
| 143 | + |
| 144 | +#endif |
0 commit comments