|
| 1 | +//===--- RequirementEnvironment.cpp - ASTContext Implementation -----------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 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 | +// This file implements the RequirementEnvironment class, which is used to |
| 14 | +// capture how a witness to a protocol requirement maps type parameters. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#include "swift/AST/RequirementEnvironment.h" |
| 19 | +#include "swift/AST/ASTContext.h" |
| 20 | +#include "swift/AST/Decl.h" |
| 21 | +#include "swift/AST/DeclContext.h" |
| 22 | +#include "swift/AST/GenericSignature.h" |
| 23 | +#include "swift/AST/GenericSignatureBuilder.h" |
| 24 | +#include "swift/AST/ProtocolConformance.h" |
| 25 | +#include "swift/AST/Types.h" |
| 26 | +#include "llvm/ADT/Statistic.h" |
| 27 | + |
| 28 | +#define DEBUG_TYPE "Protocol conformance checking" |
| 29 | + |
| 30 | +using namespace swift; |
| 31 | + |
| 32 | +STATISTIC(NumRequirementEnvironments, "# of requirement environments"); |
| 33 | + |
| 34 | +RequirementEnvironment::RequirementEnvironment( |
| 35 | + DeclContext *conformanceDC, |
| 36 | + GenericSignature *reqSig, |
| 37 | + ProtocolDecl *proto, |
| 38 | + ClassDecl *covariantSelf, |
| 39 | + ProtocolConformance *conformance) |
| 40 | + : reqSig(reqSig) { |
| 41 | + ASTContext &ctx = conformanceDC->getASTContext(); |
| 42 | + |
| 43 | + auto concreteType = conformanceDC->getSelfInterfaceType(); |
| 44 | + auto *conformanceSig = conformanceDC->getGenericSignatureOfContext(); |
| 45 | + |
| 46 | + // Build a substitution map from the generic parameters of the conforming |
| 47 | + // type to the synthetic environment. |
| 48 | + // |
| 49 | + // For structs, enums and protocols, this is a 1:1 mapping; for classes, |
| 50 | + // we increase the depth of each generic parameter by 1 so that we can |
| 51 | + // introduce a class-bound 'Self' parameter. |
| 52 | + auto substConcreteType = concreteType; |
| 53 | + SubstitutionMap conformanceToSyntheticEnvMap; |
| 54 | + if (conformanceSig) { |
| 55 | + conformanceToSyntheticEnvMap = conformanceSig->getSubstitutionMap( |
| 56 | + [&](SubstitutableType *type) { |
| 57 | + auto *genericParam = cast<GenericTypeParamType>(type); |
| 58 | + if (covariantSelf) { |
| 59 | + return GenericTypeParamType::get( |
| 60 | + genericParam->getDepth() + 1, |
| 61 | + genericParam->getIndex(), |
| 62 | + ctx); |
| 63 | + } |
| 64 | + |
| 65 | + return GenericTypeParamType::get( |
| 66 | + genericParam->getDepth(), |
| 67 | + genericParam->getIndex(), |
| 68 | + ctx); |
| 69 | + }, |
| 70 | + MakeAbstractConformanceForGenericType()); |
| 71 | + |
| 72 | + substConcreteType = concreteType.subst(conformanceToSyntheticEnvMap); |
| 73 | + } |
| 74 | + |
| 75 | + // Calculate the depth at which the requirement's generic parameters |
| 76 | + // appear in the synthetic signature. |
| 77 | + unsigned depth = 0; |
| 78 | + if (covariantSelf) { |
| 79 | + depth++; |
| 80 | + } |
| 81 | + if (conformanceSig) { |
| 82 | + depth += conformanceSig->getGenericParams().back()->getDepth() + 1; |
| 83 | + } |
| 84 | + |
| 85 | + // Build a substitution map to replace the protocol's \c Self and the type |
| 86 | + // parameters of the requirement into a combined context that provides the |
| 87 | + // type parameters of the conformance context and the parameters of the |
| 88 | + // requirement. |
| 89 | + auto selfType = cast<GenericTypeParamType>( |
| 90 | + proto->getSelfInterfaceType()->getCanonicalType()); |
| 91 | + |
| 92 | + reqToSyntheticEnvMap = reqSig->getSubstitutionMap( |
| 93 | + [selfType, substConcreteType, depth, covariantSelf, &ctx] |
| 94 | + (SubstitutableType *type) -> Type { |
| 95 | + // If the conforming type is a class, the protocol 'Self' maps to |
| 96 | + // the class-constrained 'Self'. Otherwise, it maps to the concrete |
| 97 | + // type. |
| 98 | + if (type->isEqual(selfType)) { |
| 99 | + if (covariantSelf) |
| 100 | + return GenericTypeParamType::get(/*depth=*/0, /*index=*/0, ctx); |
| 101 | + return substConcreteType; |
| 102 | + } |
| 103 | + // Other requirement generic parameters map 1:1 with their depth |
| 104 | + // increased appropriately. |
| 105 | + auto *genericParam = cast<GenericTypeParamType>(type); |
| 106 | + // In a protocol requirement, the only generic parameter at depth 0 |
| 107 | + // should be 'Self', and all others at depth 1. Anything else is |
| 108 | + // invalid code. |
| 109 | + if (genericParam->getDepth() != 1) |
| 110 | + return Type(); |
| 111 | + auto substGenericParam = |
| 112 | + GenericTypeParamType::get(depth, genericParam->getIndex(), ctx); |
| 113 | + return substGenericParam; |
| 114 | + }, |
| 115 | + [selfType, substConcreteType, conformance, conformanceDC, &ctx]( |
| 116 | + CanType type, Type replacement, ProtocolType *protoType) |
| 117 | + -> Optional<ProtocolConformanceRef> { |
| 118 | + auto proto = protoType->getDecl(); |
| 119 | + |
| 120 | + // The protocol 'Self' conforms concretely to the conforming type. |
| 121 | + if (type->isEqual(selfType)) { |
| 122 | + ProtocolConformance *specialized = conformance; |
| 123 | + if (conformance && conformance->getGenericSignature()) { |
| 124 | + auto concreteSubs = |
| 125 | + substConcreteType->getContextSubstitutionMap( |
| 126 | + conformanceDC->getParentModule(), conformanceDC); |
| 127 | + specialized = |
| 128 | + ctx.getSpecializedConformance(substConcreteType, conformance, |
| 129 | + concreteSubs); |
| 130 | + } |
| 131 | + |
| 132 | + if (specialized) |
| 133 | + return ProtocolConformanceRef(specialized); |
| 134 | + } |
| 135 | + |
| 136 | + // All other generic parameters come from the requirement itself |
| 137 | + // and conform abstractly. |
| 138 | + return ProtocolConformanceRef(proto); |
| 139 | + }); |
| 140 | + |
| 141 | + // If the requirement itself is non-generic, the synthetic signature |
| 142 | + // is that of the conformance context. |
| 143 | + if (!covariantSelf && |
| 144 | + reqSig->getGenericParams().size() == 1 && |
| 145 | + reqSig->getRequirements().size() == 1) { |
| 146 | + syntheticSignature = conformanceDC->getGenericSignatureOfContext(); |
| 147 | + if (syntheticSignature) { |
| 148 | + syntheticSignature = syntheticSignature->getCanonicalSignature(); |
| 149 | + syntheticEnvironment = |
| 150 | + syntheticSignature->createGenericEnvironment(); |
| 151 | + } |
| 152 | + |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + // Construct a generic signature builder by collecting the constraints |
| 157 | + // from the requirement and the context of the conformance together, |
| 158 | + // because both define the capabilities of the requirement. |
| 159 | + GenericSignatureBuilder builder(ctx); |
| 160 | + |
| 161 | + auto source = |
| 162 | + GenericSignatureBuilder::FloatingRequirementSource::forAbstract(); |
| 163 | + |
| 164 | + // If the conforming type is a class, add a class-constrained 'Self' |
| 165 | + // parameter. |
| 166 | + if (covariantSelf) { |
| 167 | + auto paramTy = GenericTypeParamType::get(/*depth=*/0, /*index=*/0, ctx); |
| 168 | + builder.addGenericParameter(paramTy); |
| 169 | + } |
| 170 | + |
| 171 | + // Now, add all generic parameters from the conforming type. |
| 172 | + if (conformanceSig) { |
| 173 | + for (auto param : conformanceSig->getGenericParams()) { |
| 174 | + builder.addGenericParameter( |
| 175 | + Type(param).subst(conformanceToSyntheticEnvMap) |
| 176 | + ->castTo<GenericTypeParamType>()); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + // Next, add requirements. |
| 181 | + if (covariantSelf) { |
| 182 | + auto paramTy = GenericTypeParamType::get(/*depth=*/0, /*index=*/0, ctx); |
| 183 | + Requirement reqt(RequirementKind::Superclass, paramTy, substConcreteType); |
| 184 | + builder.addRequirement(reqt, source, nullptr); |
| 185 | + } |
| 186 | + |
| 187 | + if (conformanceSig) { |
| 188 | + for (auto &rawReq : conformanceSig->getRequirements()) { |
| 189 | + if (auto req = rawReq.subst(conformanceToSyntheticEnvMap)) |
| 190 | + builder.addRequirement(*req, source, nullptr); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + // Finally, add the generic parameters from the requirement. |
| 195 | + for (auto genericParam : reqSig->getGenericParams().slice(1)) { |
| 196 | + // The only depth that makes sense is depth == 1, the generic parameters |
| 197 | + // of the requirement itself. Anything else is from invalid code. |
| 198 | + if (genericParam->getDepth() != 1) { |
| 199 | + return; |
| 200 | + } |
| 201 | + |
| 202 | + // Create an equivalent generic parameter at the next depth. |
| 203 | + auto substGenericParam = |
| 204 | + GenericTypeParamType::get(depth, genericParam->getIndex(), ctx); |
| 205 | + |
| 206 | + builder.addGenericParameter(substGenericParam); |
| 207 | + } |
| 208 | + |
| 209 | + ++NumRequirementEnvironments; |
| 210 | + |
| 211 | + // Next, add each of the requirements (mapped from the requirement's |
| 212 | + // interface types into the abstract type parameters). |
| 213 | + for (auto &rawReq : reqSig->getRequirements()) { |
| 214 | + // FIXME: This should not be necessary, since the constraint is redundant, |
| 215 | + // but we need it to work around some crashes for now. |
| 216 | + if (rawReq.getKind() == RequirementKind::Conformance && |
| 217 | + rawReq.getFirstType()->isEqual(selfType) && |
| 218 | + rawReq.getSecondType()->isEqual(proto->getDeclaredType())) |
| 219 | + continue; |
| 220 | + |
| 221 | + if (auto req = rawReq.subst(reqToSyntheticEnvMap)) |
| 222 | + builder.addRequirement(*req, source, conformanceDC->getParentModule()); |
| 223 | + } |
| 224 | + |
| 225 | + // Produce the generic signature and environment. |
| 226 | + // FIXME: Pass in a source location for the conformance, perhaps? It seems |
| 227 | + // like this could fail. |
| 228 | + syntheticSignature = |
| 229 | + std::move(builder).computeGenericSignature(SourceLoc()); |
| 230 | + syntheticEnvironment = syntheticSignature->createGenericEnvironment(); |
| 231 | +} |
0 commit comments