mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".
I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
145 lines
4.2 KiB
C++
145 lines
4.2 KiB
C++
//===--- PatternBindingState.h --------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SWIFT_PARSE_PATTERNBINDINGSTATE_H
|
|
#define SWIFT_PARSE_PATTERNBINDINGSTATE_H
|
|
|
|
#include "swift/AST/Decl.h"
|
|
#include "swift/Parse/Token.h"
|
|
|
|
namespace swift {
|
|
|
|
class Token;
|
|
|
|
struct PatternBindingState {
|
|
enum Kind {
|
|
/// InBindingPattern has this value when not parsing a pattern.
|
|
NotInBinding,
|
|
|
|
/// InBindingPattern has this value when we're in a matching pattern, but
|
|
/// not within a var/let pattern. In this phase, identifiers are references
|
|
/// to the enclosing scopes, not a variable binding.
|
|
InMatchingPattern,
|
|
|
|
/// InBindingPattern has this value when parsing a pattern in which bound
|
|
/// variables are implicitly immutable, but allowed to be marked mutable by
|
|
/// using a 'var' pattern. This happens in for-each loop patterns.
|
|
ImplicitlyImmutable,
|
|
|
|
/// When InBindingPattern has this value, bound variables are mutable, and
|
|
/// nested let/var patterns are not permitted. This happens when parsing a
|
|
/// 'var' decl or when parsing inside a 'var' pattern.
|
|
InVar,
|
|
|
|
/// When InBindingPattern has this value, bound variables are immutable,and
|
|
/// nested let/var patterns are not permitted. This happens when parsing a
|
|
/// 'let' decl or when parsing inside a 'let' pattern.
|
|
InLet,
|
|
|
|
/// When InBindingPattern has this value, bound variables are mutable, and
|
|
/// nested let/var/inout patterns are not permitted. This happens when
|
|
/// parsing an 'inout' decl or when parsing inside an 'inout' pattern.
|
|
InInOut,
|
|
};
|
|
|
|
Kind kind;
|
|
|
|
PatternBindingState(Kind kind) : kind(kind) {}
|
|
|
|
operator bool() const { return kind != Kind::NotInBinding; }
|
|
|
|
operator Kind() const { return kind; }
|
|
|
|
static llvm::Optional<PatternBindingState> get(StringRef str) {
|
|
auto kind = llvm::StringSwitch<Kind>(str)
|
|
.Case("let", Kind::InLet)
|
|
.Case("var", Kind::InVar)
|
|
.Case("inout", Kind::InInOut)
|
|
.Default(Kind::NotInBinding);
|
|
return PatternBindingState(kind);
|
|
}
|
|
|
|
/// Try to explicitly find the new pattern binding state from a token.
|
|
explicit PatternBindingState(Token tok) : kind(NotInBinding) {
|
|
switch (tok.getKind()) {
|
|
case tok::kw_let:
|
|
kind = InLet;
|
|
break;
|
|
case tok::kw_var:
|
|
kind = InVar;
|
|
break;
|
|
case tok::kw_inout:
|
|
kind = InInOut;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// Explicitly initialize from a VarDecl::Introducer.
|
|
explicit PatternBindingState(VarDecl::Introducer introducer)
|
|
: kind(NotInBinding) {
|
|
switch (introducer) {
|
|
case VarDecl::Introducer::Let:
|
|
kind = InLet;
|
|
break;
|
|
case VarDecl::Introducer::Var:
|
|
kind = InVar;
|
|
break;
|
|
case VarDecl::Introducer::InOut:
|
|
kind = InInOut;
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// If there is a direct introducer associated with this pattern binding
|
|
/// state, return that. Return none otherwise.
|
|
llvm::Optional<VarDecl::Introducer> getIntroducer() const {
|
|
switch (kind) {
|
|
case Kind::NotInBinding:
|
|
case Kind::InMatchingPattern:
|
|
case Kind::ImplicitlyImmutable:
|
|
return llvm::None;
|
|
case Kind::InVar:
|
|
return VarDecl::Introducer::Var;
|
|
case Kind::InLet:
|
|
return VarDecl::Introducer::Let;
|
|
case Kind::InInOut:
|
|
return VarDecl::Introducer::InOut;
|
|
}
|
|
}
|
|
|
|
PatternBindingState
|
|
getPatternBindingStateForIntroducer(VarDecl::Introducer defaultValue) {
|
|
return PatternBindingState(getIntroducer().value_or(defaultValue));
|
|
}
|
|
|
|
llvm::Optional<unsigned> getSelectIndexForIntroducer() const {
|
|
switch (kind) {
|
|
case Kind::InLet:
|
|
return 0;
|
|
case Kind::InInOut:
|
|
return 1;
|
|
case Kind::InVar:
|
|
return 2;
|
|
default:
|
|
return llvm::None;
|
|
}
|
|
}
|
|
|
|
bool isLet() const { return kind == Kind::InLet; }
|
|
};
|
|
|
|
} // namespace swift
|
|
|
|
#endif
|