Skip to content

Commit

Permalink
AST: ProtocolConformanceRef::getTypeWitnessByName() should be an inst…
Browse files Browse the repository at this point in the history
…ance method
  • Loading branch information
slavapestov committed May 14, 2019
1 parent d93695f commit 6e3609d
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 37 deletions.
5 changes: 1 addition & 4 deletions include/swift/AST/ProtocolConformanceRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,7 @@ class ProtocolConformanceRef {
return llvm::hash_value(conformance.Union.getOpaqueValue());
}

static Type
getTypeWitnessByName(Type type,
ProtocolConformanceRef conformance,
Identifier name);
Type getTypeWitnessByName(Type type, Identifier name) const;

/// Determine whether this conformance is canonical.
bool isCanonical() const;
Expand Down
3 changes: 1 addition & 2 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4225,8 +4225,7 @@ Type ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
*bridgedValueType = type;

// Find the Objective-C class type we bridge to.
return ProtocolConformanceRef::getTypeWitnessByName(
type, *conformance, Id_ObjectiveCType);
return conformance->getTypeWitnessByName(type, Id_ObjectiveCType);
}

// Do we conform to Error?
Expand Down
13 changes: 6 additions & 7 deletions lib/AST/ProtocolConformance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,14 @@ ProtocolConformanceRef::subst(Type origType,
}

Type
ProtocolConformanceRef::getTypeWitnessByName(Type type,
ProtocolConformanceRef conformance,
Identifier name) {
assert(!conformance.isInvalid());
ProtocolConformanceRef::getTypeWitnessByName(Type type, Identifier name) const {
assert(!isInvalid());

// Find the named requirement.
ProtocolDecl *proto = conformance.getRequirement();
ProtocolDecl *proto = getRequirement();
AssociatedTypeDecl *assocType = nullptr;
auto members = proto->lookupDirect(name);
auto members = proto->lookupDirect(name,
NominalTypeDecl::LookupDirectFlags::IgnoreNewExtensions);
for (auto member : members) {
assocType = dyn_cast<AssociatedTypeDecl>(member);
if (assocType)
Expand All @@ -162,7 +161,7 @@ ProtocolConformanceRef::getTypeWitnessByName(Type type,
return nullptr;

return assocType->getDeclaredInterfaceType().subst(
SubstitutionMap::getProtocolSubstitutions(proto, type, conformance));
SubstitutionMap::getProtocolSubstitutions(proto, type, *this));
}

void *ProtocolConformance::operator new(size_t bytes, ASTContext &context,
Expand Down
7 changes: 3 additions & 4 deletions lib/PrintAsObjC/PrintAsObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1377,10 +1377,9 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,

// Dig out the Objective-C type.
auto conformance = conformances.front();
Type objcType = ProtocolConformanceRef::getTypeWitnessByName(
nominal->getDeclaredType(),
ProtocolConformanceRef(conformance),
ctx.Id_ObjectiveCType);
Type objcType = ProtocolConformanceRef(conformance).getTypeWitnessByName(
nominal->getDeclaredType(),
ctx.Id_ObjectiveCType);
if (!objcType) return nullptr;

// Dig out the Objective-C class.
Expand Down
5 changes: 2 additions & 3 deletions lib/SIL/Bridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,8 @@ Type TypeConverter::getLoweredCBridgedType(AbstractionPattern pattern,
auto conformance = foreignRepresentation.second;
assert(conformance && "Missing conformance?");
Type bridgedTy =
ProtocolConformanceRef::getTypeWitnessByName(
t, ProtocolConformanceRef(conformance),
M.getASTContext().Id_ObjectiveCType);
ProtocolConformanceRef(conformance).getTypeWitnessByName(
t, M.getASTContext().Id_ObjectiveCType);
assert(bridgedTy && "Missing _ObjectiveCType witness?");
if (purpose == BridgedTypePurpose::ForResult && clangTy)
bridgedTy = OptionalType::get(bridgedTy);
Expand Down
23 changes: 11 additions & 12 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1740,8 +1740,8 @@ static Type isRawRepresentable(Type fromType, const ConstraintSystem &CS) {
if (!conformance)
return Type();

Type rawTy = ProtocolConformanceRef::getTypeWitnessByName(
fromType, *conformance, CS.getASTContext().Id_RawValue);
Type rawTy = conformance->getTypeWitnessByName(
fromType, CS.getASTContext().Id_RawValue);
return rawTy;
}

Expand Down Expand Up @@ -2119,9 +2119,9 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
ConformanceCheckFlags::InExpression)) {
Type errorCodeType = CS.getType(expr);
Type errorType =
ProtocolConformanceRef::getTypeWitnessByName(errorCodeType, *conformance,
TC.Context.Id_ErrorType)
->getCanonicalType();
conformance->getTypeWitnessByName(errorCodeType,
TC.Context.Id_ErrorType)
->getCanonicalType();
if (errorType) {
auto diag = diagnose(expr->getLoc(), diag::cannot_throw_error_code,
errorCodeType, errorType);
Expand Down Expand Up @@ -6253,9 +6253,8 @@ bool FailureDiagnosis::visitArrayExpr(ArrayExpr *E) {
= CS.TC.conformsToProtocol(contextualType, ALC, CS.DC,
ConformanceCheckFlags::InExpression)) {
Type contextualElementType =
ProtocolConformanceRef::getTypeWitnessByName(
contextualType, *Conformance,
CS.getASTContext().Id_ArrayLiteralElement)
Conformance->getTypeWitnessByName(
contextualType, CS.getASTContext().Id_ArrayLiteralElement)
->getDesugaredType();

// Type check each of the subexpressions in place, passing down the contextual
Expand Down Expand Up @@ -6343,13 +6342,13 @@ bool FailureDiagnosis::visitDictionaryExpr(DictionaryExpr *E) {
}

contextualKeyType =
ProtocolConformanceRef::getTypeWitnessByName(
contextualType, *Conformance, CS.getASTContext().Id_Key)
Conformance->getTypeWitnessByName(
contextualType, CS.getASTContext().Id_Key)
->getDesugaredType();

contextualValueType =
ProtocolConformanceRef::getTypeWitnessByName(
contextualType, *Conformance, CS.getASTContext().Id_Value)
Conformance->getTypeWitnessByName(
contextualType, CS.getASTContext().Id_Value)
->getDesugaredType();

assert(contextualKeyType && contextualValueType &&
Expand Down
10 changes: 5 additions & 5 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4325,8 +4325,8 @@ void TypeChecker::useBridgedNSErrorConformances(DeclContext *dc, Type type) {
// Hack: If we've used a conformance to the _BridgedStoredNSError
// protocol, also use the RawRepresentable and _ErrorCodeProtocol
// conformances on the Code associated type witness.
if (auto codeType = ProtocolConformanceRef::getTypeWitnessByName(
type, *conformance, Context.Id_Code)) {
if (auto codeType = conformance->getTypeWitnessByName(
type, Context.Id_Code)) {
(void)conformsToProtocol(codeType, errorCodeProto, dc,
ConformanceCheckFlags::Used);
(void)conformsToProtocol(codeType, rawProto, dc,
Expand All @@ -4340,8 +4340,8 @@ void TypeChecker::useBridgedNSErrorConformances(DeclContext *dc, Type type) {
(ConformanceCheckFlags::SuppressDependencyTracking|
ConformanceCheckFlags::Used));
if (conformance && conformance->isConcrete()) {
if (Type errorType = ProtocolConformanceRef::getTypeWitnessByName(
type, *conformance, Context.Id_ErrorType)) {
if (Type errorType = conformance->getTypeWitnessByName(
type, Context.Id_ErrorType)) {
(void)conformsToProtocol(errorType, bridgedStoredNSError, dc,
ConformanceCheckFlags::Used);
}
Expand Down Expand Up @@ -5663,7 +5663,7 @@ Type TypeChecker::getWitnessType(Type type, ProtocolDecl *protocol,
ProtocolConformanceRef conformance,
Identifier name,
Diag<> brokenProtocolDiag) {
Type ty = ProtocolConformanceRef::getTypeWitnessByName(type, conformance, name);
Type ty = conformance.getTypeWitnessByName(type, name);
if (!ty &&
!(conformance.isConcrete() && conformance.getConcrete()->isInvalid()))
diagnose(protocol->getLoc(), brokenProtocolDiag);
Expand Down

0 comments on commit 6e3609d

Please sign in to comment.