Skip to content

[NFC] Fold The Tri-State In Optional<ProtocolConformanceRef> #27949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4380,9 +4380,9 @@ class ProtocolDecl final : public NominalTypeDecl {

/// Returns the default associated conformance witness for an associated
/// type, or \c None if there is no default.
Optional<ProtocolConformanceRef> getDefaultAssociatedConformanceWitness(
CanType association,
ProtocolDecl *requirement) const;
ProtocolConformanceRef
getDefaultAssociatedConformanceWitness(CanType association,
ProtocolDecl *requirement) const;

/// Set the default associated conformance witness for the given
/// associated conformance.
Expand Down
4 changes: 2 additions & 2 deletions include/swift/AST/GenericSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
/// Look up a stored conformance in the generic signature. These are formed
/// from same-type constraints placed on associated types of generic
/// parameters which have conformance constraints on them.
Optional<ProtocolConformanceRef>
lookupConformance(CanType depTy, ProtocolDecl *proto) const;
ProtocolConformanceRef lookupConformance(CanType depTy,
ProtocolDecl *proto) const;

/// Iterate over all generic parameters, passing a flag to the callback
/// indicating if the generic parameter is canonical or not.
Expand Down
14 changes: 6 additions & 8 deletions include/swift/AST/GenericSignatureBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,9 @@ class GenericSignatureBuilder {
explicit LookUpConformanceInBuilder(GenericSignatureBuilder *builder)
: builder(builder) {}

Optional<ProtocolConformanceRef>
operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolDecl *conformedProtocol) const {
ProtocolConformanceRef operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolDecl *conformedProtocol) const {
return builder->lookupConformance(dependentType,
conformingReplacementType,
conformedProtocol);
Expand All @@ -534,10 +533,9 @@ class GenericSignatureBuilder {
LookUpConformanceInBuilder getLookupConformanceFn();

/// Lookup a protocol conformance in a module-agnostic manner.
Optional<ProtocolConformanceRef>
lookupConformance(CanType dependentType, Type conformingReplacementType,
ProtocolDecl *conformedProtocol);

ProtocolConformanceRef lookupConformance(CanType dependentType,
Type conformingReplacementType,
ProtocolDecl *conformedProtocol);

/// Retrieve the lazy resolver, if there is one.
LazyResolver *getLazyResolver() const;
Expand Down
11 changes: 5 additions & 6 deletions include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,19 +409,18 @@ class ModuleDecl : public DeclContext, public TypeDecl {
/// \returns The result of the conformance search, which will be
/// None if the type does not conform to the protocol or contain a
/// ProtocolConformanceRef if it does conform.
Optional<ProtocolConformanceRef>
lookupConformance(Type type, ProtocolDecl *protocol);
ProtocolConformanceRef lookupConformance(Type type, ProtocolDecl *protocol);

/// Look for the conformance of the given existential type to the given
/// protocol.
Optional<ProtocolConformanceRef>
lookupExistentialConformance(Type type, ProtocolDecl *protocol);
ProtocolConformanceRef lookupExistentialConformance(Type type,
ProtocolDecl *protocol);

/// Exposes TypeChecker functionality for querying protocol conformance.
/// Returns a valid ProtocolConformanceRef only if all conditional
/// requirements are successfully resolved.
Optional<ProtocolConformanceRef>
conformsToProtocol(Type sourceTy, ProtocolDecl *targetProtocol);
ProtocolConformanceRef conformsToProtocol(Type sourceTy,
ProtocolDecl *targetProtocol);

/// Find a member named \p name in \p container that was declared in this
/// module.
Expand Down
7 changes: 6 additions & 1 deletion include/swift/AST/ProtocolConformanceRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,19 @@ class ProtocolConformanceRef {
"cannot construct ProtocolConformanceRef with null");
}

ProtocolConformanceRef(std::nullptr_t = nullptr)
: Union((ProtocolDecl *)nullptr) {}

static ProtocolConformanceRef forInvalid() {
return ProtocolConformanceRef(UnionType((ProtocolDecl *)nullptr));
return ProtocolConformanceRef();
}

bool isInvalid() const {
return !Union;
}

explicit operator bool() const { return !isInvalid(); }

/// Create either a concrete or an abstract protocol conformance reference,
/// depending on whether ProtocolConformance is null.
explicit ProtocolConformanceRef(ProtocolDecl *protocol,
Expand Down
6 changes: 3 additions & 3 deletions include/swift/AST/Stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ class ForEachStmt : public LabeledStmt {
BraceStmt *Body;

// Set by Sema:
Optional<ProtocolConformanceRef> sequenceConformance;
ProtocolConformanceRef sequenceConformance = ProtocolConformanceRef();
ConcreteDeclRef makeIterator;
ConcreteDeclRef iteratorNext;
VarDecl *iteratorVar = nullptr;
Expand Down Expand Up @@ -842,10 +842,10 @@ class ForEachStmt : public LabeledStmt {
void setIteratorNext(ConcreteDeclRef declRef) { iteratorNext = declRef; }
ConcreteDeclRef getIteratorNext() const { return iteratorNext; }

void setSequenceConformance(Optional<ProtocolConformanceRef> conformance) {
void setSequenceConformance(ProtocolConformanceRef conformance) {
sequenceConformance = conformance;
}
Optional<ProtocolConformanceRef> getSequenceConformance() const {
ProtocolConformanceRef getSequenceConformance() const {
return sequenceConformance;
}

Expand Down
13 changes: 6 additions & 7 deletions include/swift/AST/SubstitutionMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ class SubstitutionMap {
ArrayRef<ProtocolConformanceRef> getConformances() const;

/// Look up a conformance for the given type to the given protocol.
Optional<ProtocolConformanceRef>
lookupConformance(CanType type, ProtocolDecl *proto) const;
ProtocolConformanceRef lookupConformance(CanType type,
ProtocolDecl *proto) const;

/// Whether the substitution map is empty.
bool empty() const;
Expand Down Expand Up @@ -290,11 +290,10 @@ class LookUpConformanceInSubstitutionMap {
public:
explicit LookUpConformanceInSubstitutionMap(SubstitutionMap Subs)
: Subs(Subs) {}

Optional<ProtocolConformanceRef>
operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolDecl *conformedProtocol) const;

ProtocolConformanceRef operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolDecl *conformedProtocol) const;
};

} // end namespace swift
Expand Down
27 changes: 12 additions & 15 deletions include/swift/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ struct QueryTypeSubstitutionMapOrIdentity {
using GenericFunction = auto(CanType dependentType,
Type conformingReplacementType,
ProtocolDecl *conformedProtocol)
-> Optional<ProtocolConformanceRef>;
-> ProtocolConformanceRef;
using LookupConformanceFn = llvm::function_ref<GenericFunction>;

/// Functor class suitable for use as a \c LookupConformanceFn to look up a
Expand All @@ -103,22 +103,20 @@ class LookUpConformanceInModule {
public:
explicit LookUpConformanceInModule(ModuleDecl *M)
: M(M) {}

Optional<ProtocolConformanceRef>
operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolDecl *conformedProtocol) const;

ProtocolConformanceRef operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolDecl *conformedProtocol) const;
};

/// Functor class suitable for use as a \c LookupConformanceFn that provides
/// only abstract conformances for generic types. Asserts that the replacement
/// type is an opaque generic type.
class MakeAbstractConformanceForGenericType {
public:
Optional<ProtocolConformanceRef>
operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolDecl *conformedProtocol) const;
ProtocolConformanceRef operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolDecl *conformedProtocol) const;
};

/// Functor class suitable for use as a \c LookupConformanceFn that fetches
Expand All @@ -130,11 +128,10 @@ class LookUpConformanceInSignature {
: Sig(Sig) {
assert(Sig && "Cannot lookup conformance in null signature!");
}

Optional<ProtocolConformanceRef>
operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolDecl *conformedProtocol) const;

ProtocolConformanceRef operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolDecl *conformedProtocol) const;
};

/// Flags that can be passed when substituting into a type.
Expand Down
81 changes: 32 additions & 49 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3826,7 +3826,7 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
// CanType? // if !isCoro && NumAnyResults > 1, all result cache

llvm::PointerIntPair<CanGenericSignature, 1, bool> GenericSigAndIsImplied;
Optional<ProtocolConformanceRef> WitnessMethodConformance;
ProtocolConformanceRef WitnessMethodConformance;
SubstitutionMap Substitutions;

MutableArrayRef<SILParameterInfo> getMutableParameters() {
Expand Down Expand Up @@ -3888,25 +3888,22 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
ArrayRef<SILYieldInfo> yieldResults,
ArrayRef<SILResultInfo> normalResults,
Optional<SILResultInfo> errorResult,
SubstitutionMap substitutions,
bool genericSigIsImplied,
const ASTContext &ctx,
RecursiveTypeProperties properties,
Optional<ProtocolConformanceRef> witnessMethodConformance);
SubstitutionMap substitutions, bool genericSigIsImplied,
const ASTContext &ctx, RecursiveTypeProperties properties,
ProtocolConformanceRef witnessMethodConformance);

public:
static CanSILFunctionType get(GenericSignature genericSig,
ExtInfo ext,
SILCoroutineKind coroutineKind,
ParameterConvention calleeConvention,
ArrayRef<SILParameterInfo> interfaceParams,
ArrayRef<SILYieldInfo> interfaceYields,
ArrayRef<SILResultInfo> interfaceResults,
Optional<SILResultInfo> interfaceErrorResult,
SubstitutionMap substitutions,
bool genericSigIsImplied,
const ASTContext &ctx,
Optional<ProtocolConformanceRef> witnessMethodConformance = None);
static CanSILFunctionType
get(GenericSignature genericSig, ExtInfo ext, SILCoroutineKind coroutineKind,
ParameterConvention calleeConvention,
ArrayRef<SILParameterInfo> interfaceParams,
ArrayRef<SILYieldInfo> interfaceYields,
ArrayRef<SILResultInfo> interfaceResults,
Optional<SILResultInfo> interfaceErrorResult,
SubstitutionMap substitutions, bool genericSigIsImplied,
const ASTContext &ctx,
ProtocolConformanceRef witnessMethodConformance =
ProtocolConformanceRef());

/// Return a structurally-identical function type with a slightly tweaked
/// ExtInfo.
Expand Down Expand Up @@ -4105,16 +4102,9 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
ClassDecl *getWitnessMethodClass(SILModule &M) const;

/// If this is a @convention(witness_method) function, return the conformance
/// for which the method is a witness.
ProtocolConformanceRef getWitnessMethodConformance() const {
assert(getRepresentation() == Representation::WitnessMethod);
return *WitnessMethodConformance;
}

/// If this is a @convention(witness_method) function, return the conformance
/// for which the method is a witness, if it isn't that convention, return
/// None.
Optional<ProtocolConformanceRef> getWitnessMethodConformanceOrNone() const {
/// for which the method is a witness. If it isn't that convention, return
/// an invalid conformance.
ProtocolConformanceRef getWitnessMethodConformanceOrInvalid() const {
return WitnessMethodConformance;
}

Expand Down Expand Up @@ -4209,24 +4199,17 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,

void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, getSubstGenericSignature(), getExtInfo(), getCoroutineKind(),
getCalleeConvention(), getParameters(), getYields(),
getResults(), getOptionalErrorResult(),
getWitnessMethodConformanceOrNone(),
isGenericSignatureImplied(),
getSubstitutions());
}
static void Profile(llvm::FoldingSetNodeID &ID,
GenericSignature genericSig,
ExtInfo info,
SILCoroutineKind coroutineKind,
ParameterConvention calleeConvention,
ArrayRef<SILParameterInfo> params,
ArrayRef<SILYieldInfo> yields,
ArrayRef<SILResultInfo> results,
Optional<SILResultInfo> errorResult,
Optional<ProtocolConformanceRef> conformance,
bool isGenericSigImplied,
SubstitutionMap substitutions);
getCalleeConvention(), getParameters(), getYields(), getResults(),
getOptionalErrorResult(), getWitnessMethodConformanceOrInvalid(),
isGenericSignatureImplied(), getSubstitutions());
}
static void
Profile(llvm::FoldingSetNodeID &ID, GenericSignature genericSig, ExtInfo info,
SILCoroutineKind coroutineKind, ParameterConvention calleeConvention,
ArrayRef<SILParameterInfo> params, ArrayRef<SILYieldInfo> yields,
ArrayRef<SILResultInfo> results, Optional<SILResultInfo> errorResult,
ProtocolConformanceRef conformance, bool isGenericSigImplied,
SubstitutionMap substitutions);

// Implement isa/cast/dyncast/etc.
static bool classof(const TypeBase *T) {
Expand Down Expand Up @@ -4972,9 +4955,9 @@ class ReplaceOpaqueTypesWithUnderlyingTypes {
Type operator()(SubstitutableType *maybeOpaqueType) const;

/// LookupConformanceFn
Optional<ProtocolConformanceRef> operator()(CanType maybeOpaqueType,
Type replacementType,
ProtocolDecl *protocol) const;
ProtocolConformanceRef operator()(CanType maybeOpaqueType,
Type replacementType,
ProtocolDecl *protocol) const;

OpaqueSubstitutionKind
shouldPerformSubstitution(OpaqueTypeDecl *opaque) const;
Expand Down
5 changes: 2 additions & 3 deletions include/swift/SIL/SILType.h
Original file line number Diff line number Diff line change
Expand Up @@ -587,11 +587,10 @@ NON_SIL_TYPE(LValue)

CanSILFunctionType getNativeSILFunctionType(
Lowering::TypeConverter &TC, Lowering::AbstractionPattern origType,
CanAnyFunctionType substType,
Optional<SILDeclRef> origConstant = None,
CanAnyFunctionType substType, Optional<SILDeclRef> origConstant = None,
Optional<SILDeclRef> constant = None,
Optional<SubstitutionMap> reqtSubs = None,
Optional<ProtocolConformanceRef> witnessMethodConformance = None);
ProtocolConformanceRef witnessMethodConformance = ProtocolConformanceRef());

inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, SILType T) {
T.print(OS);
Expand Down
6 changes: 3 additions & 3 deletions include/swift/SIL/TypeLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ inline CanAnyFunctionType adjustFunctionType(CanAnyFunctionType t,
CanSILFunctionType
adjustFunctionType(CanSILFunctionType type, SILFunctionType::ExtInfo extInfo,
ParameterConvention calleeConv,
Optional<ProtocolConformanceRef> witnessMethodConformance);
ProtocolConformanceRef witnessMethodConformance);
inline CanSILFunctionType
adjustFunctionType(CanSILFunctionType type, SILFunctionType::ExtInfo extInfo,
Optional<ProtocolConformanceRef> witnessMethodConformance) {
ProtocolConformanceRef witnessMethodConformance) {
return adjustFunctionType(type, extInfo, type->getCalleeConvention(),
witnessMethodConformance);
}
inline CanSILFunctionType
adjustFunctionType(CanSILFunctionType t, SILFunctionType::Representation rep,
Optional<ProtocolConformanceRef> witnessMethodConformance) {
ProtocolConformanceRef witnessMethodConformance) {
if (t->getRepresentation() == rep) return t;
auto extInfo = t->getExtInfo().withRepresentation(rep);
auto contextConvention = DefaultThickCalleeConvention;
Expand Down
3 changes: 1 addition & 2 deletions include/swift/SILOptimizer/Utils/Existential.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ struct ConcreteExistentialInfo {
// Do a conformance lookup on ConcreteType with the given requirement, P. If P
// is satisfiable based on the existential's conformance, return the new
// conformance on P. Otherwise return None.
Optional<ProtocolConformanceRef>
lookupExistentialConformance(ProtocolDecl *P) const {
ProtocolConformanceRef lookupExistentialConformance(ProtocolDecl *P) const {
CanType selfTy = P->getSelfInterfaceType()->getCanonicalType();
return ExistentialSubs.lookupConformance(selfTy, P);
}
Expand Down
Loading