Skip to content

NFC: Add GenericSignature::getCanonicalSignature. #29105

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
Jan 12, 2020
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
9 changes: 7 additions & 2 deletions include/swift/AST/GenericSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class ConformanceAccessPath {
SWIFT_DEBUG_DUMP;
};

class CanGenericSignature;
class GenericSignatureImpl;
class GenericTypeParamType;

Expand Down Expand Up @@ -140,6 +141,10 @@ class GenericSignature {
SWIFT_DEBUG_DUMP;
std::string getAsString() const;

/// Returns the canonical generic signature, or \c nullptr if the underlying
/// pointer is \c nullptr. The result is cached.
CanGenericSignature getCanonicalSignature() const;

// Support for FoldingSet.
void Profile(llvm::FoldingSetNodeID &id) const;

Expand Down Expand Up @@ -300,8 +305,8 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
bool isCanonical() const;

ASTContext &getASTContext() const;
/// Canonicalize the components of a generic signature.

/// Returns the canonical generic signature. The result is cached.
CanGenericSignature getCanonicalSignature() const;

/// Retrieve the generic signature builder for the given generic signature.
Expand Down
12 changes: 6 additions & 6 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,7 @@ static AllocationArena getArena(GenericSignature genericSig) {
void ASTContext::registerGenericSignatureBuilder(
GenericSignature sig,
GenericSignatureBuilder &&builder) {
auto canSig = sig->getCanonicalSignature();
auto canSig = sig.getCanonicalSignature();
auto arena = getArena(sig);
auto &genericSignatureBuilders =
getImpl().getArena(arena).GenericSignatureBuilders;
Expand Down Expand Up @@ -1564,12 +1564,12 @@ GenericSignatureBuilder *ASTContext::getOrCreateGenericSignatureBuilder(
auto builderSig =
builder->computeGenericSignature(SourceLoc(),
/*allowConcreteGenericParams=*/true);
if (builderSig->getCanonicalSignature() != sig) {
if (builderSig.getCanonicalSignature() != sig) {
llvm::errs() << "ERROR: generic signature builder is not idempotent.\n";
llvm::errs() << "Original generic signature : ";
sig->print(llvm::errs());
llvm::errs() << "\nReprocessed generic signature: ";
auto reprocessedSig = builderSig->getCanonicalSignature();
auto reprocessedSig = builderSig.getCanonicalSignature();

reprocessedSig->print(llvm::errs());
llvm::errs() << "\n";
Expand Down Expand Up @@ -3277,9 +3277,9 @@ SILFunctionType::SILFunctionType(
"types and substitutions");

if (substitutions) {
assert(substitutions.getGenericSignature()->getCanonicalSignature()
== genericSig->getCanonicalSignature()
&& "substitutions must match generic signature");
assert(substitutions.getGenericSignature().getCanonicalSignature() ==
genericSig.getCanonicalSignature() &&
"substitutions must match generic signature");
}

if (genericSig) {
Expand Down
18 changes: 9 additions & 9 deletions lib/AST/ASTDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ Type ASTBuilder::createImplFunctionType(
ArrayRef<Demangle::ImplFunctionResult<Type>> results,
Optional<Demangle::ImplFunctionResult<Type>> errorResult,
ImplFunctionTypeFlags flags) {
GenericSignature genericSig = GenericSignature();
GenericSignature genericSig;

SILCoroutineKind funcCoroutineKind = SILCoroutineKind::None;
ParameterConvention funcCalleeConvention =
Expand Down Expand Up @@ -865,12 +865,13 @@ CanGenericSignature ASTBuilder::demangleGenericSignature(
}
}

return evaluateOrDefault(
Ctx.evaluator,
AbstractGenericSignatureRequest{
nominalDecl->getGenericSignature().getPointer(), { },
std::move(requirements)},
GenericSignature())->getCanonicalSignature();
return evaluateOrDefault(Ctx.evaluator,
AbstractGenericSignatureRequest{
nominalDecl->getGenericSignature().getPointer(),
{},
std::move(requirements)},
GenericSignature())
.getCanonicalSignature();
}

DeclContext *
Expand Down Expand Up @@ -980,8 +981,7 @@ ASTBuilder::findDeclContext(NodePointer node) {
continue;
}

if (ext->getGenericSignature()->getCanonicalSignature()
== genericSig) {
if (ext->getGenericSignature().getCanonicalSignature() == genericSig) {
return ext;
}
}
Expand Down
18 changes: 9 additions & 9 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ std::string ASTMangler::mangleReabstractionThunkHelper(
assert(ThunkType->getSubstitutions().empty() && "not implemented");
GenericSignature GenSig = ThunkType->getSubstGenericSignature();
if (GenSig)
CurGenericSignature = GenSig->getCanonicalSignature();
CurGenericSignature = GenSig.getCanonicalSignature();

beginMangling();
appendType(FromType);
Expand Down Expand Up @@ -1199,7 +1199,7 @@ void ASTMangler::bindGenericParameters(CanGenericSignature sig) {
/// Bind the generic parameters from the given context and its parents.
void ASTMangler::bindGenericParameters(const DeclContext *DC) {
if (auto sig = DC->getGenericSignatureOfContext())
bindGenericParameters(sig->getCanonicalSignature());
bindGenericParameters(sig.getCanonicalSignature());
}

void ASTMangler::appendFlatGenericArgs(SubstitutionMap subs) {
Expand Down Expand Up @@ -2121,7 +2121,7 @@ void ASTMangler::appendTypeListElement(Identifier name, Type elementType,

bool ASTMangler::appendGenericSignature(GenericSignature sig,
GenericSignature contextSig) {
auto canSig = sig->getCanonicalSignature();
auto canSig = sig.getCanonicalSignature();
CurGenericSignature = canSig;

unsigned initialParamDepth;
Expand All @@ -2131,7 +2131,7 @@ bool ASTMangler::appendGenericSignature(GenericSignature sig,
if (contextSig) {
// If the signature is the same as the context signature, there's nothing
// to do.
if (contextSig->getCanonicalSignature() == canSig) {
if (contextSig.getCanonicalSignature() == canSig) {
return false;
}

Expand Down Expand Up @@ -2442,8 +2442,8 @@ CanType ASTMangler::getDeclTypeForMangling(

void ASTMangler::appendDeclType(const ValueDecl *decl, bool isFunctionMangling) {
Mod = decl->getModuleContext();
GenericSignature genericSig = GenericSignature();
GenericSignature parentGenericSig = GenericSignature();
GenericSignature genericSig;
GenericSignature parentGenericSig;
auto type = getDeclTypeForMangling(decl, genericSig, parentGenericSig);

if (AnyFunctionType *FuncTy = type->getAs<AnyFunctionType>()) {
Expand Down Expand Up @@ -2559,7 +2559,7 @@ void ASTMangler::appendEntity(const ValueDecl *decl) {

void
ASTMangler::appendProtocolConformance(const ProtocolConformance *conformance) {
GenericSignature contextSig = GenericSignature();
GenericSignature contextSig;
auto topLevelContext =
conformance->getDeclContext()->getModuleScopeContext();
Mod = topLevelContext->getParentModule();
Expand Down Expand Up @@ -2702,7 +2702,7 @@ void ASTMangler::appendConcreteProtocolConformance(
auto shouldUseConformanceSig = !CurGenericSignature && conformanceSig;
llvm::SaveAndRestore<CanGenericSignature> savedSignature(
CurGenericSignature, shouldUseConformanceSig
? conformanceSig->getCanonicalSignature()
? conformanceSig.getCanonicalSignature()
: CurGenericSignature);

// Conforming type.
Expand Down Expand Up @@ -2745,7 +2745,7 @@ void ASTMangler::appendConcreteProtocolConformance(
// Append the conformance access path with the signature of the opaque type.
{
llvm::SaveAndRestore<CanGenericSignature> savedSignature(
CurGenericSignature, opaqueSignature->getCanonicalSignature());
CurGenericSignature, opaqueSignature.getCanonicalSignature());
appendDependentProtocolConformance(conformanceAccessPath);
}
appendType(canType);
Expand Down
14 changes: 11 additions & 3 deletions lib/AST/GenericSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ GenericSignatureBuilder *GenericSignatureImpl::getGenericSignatureBuilder() {
}

bool GenericSignatureImpl::isEqual(GenericSignature Other) {
return getCanonicalSignature() == Other.getPointer()->getCanonicalSignature();
return getCanonicalSignature() == Other.getCanonicalSignature();
}

bool GenericSignatureImpl::isCanonical() const {
if (CanonicalSignatureOrASTContext.is<ASTContext*>()) return true;

if (CanonicalSignatureOrASTContext.is<ASTContext *>())
return true;
return getCanonicalSignature().getPointer() == this;
}

Expand Down Expand Up @@ -310,6 +310,14 @@ CanGenericSignature::getCanonical(TypeArrayView<GenericTypeParamType> params,
return CanGenericSignature(canSig);
}

CanGenericSignature GenericSignature::getCanonicalSignature() const {
// If the underlying pointer is null, return `CanGenericSignature()`.
if (isNull())
return CanGenericSignature();
// Otherwise, return the canonical signature of the underlying pointer.
return getPointer()->getCanonicalSignature();
}

CanGenericSignature GenericSignatureImpl::getCanonicalSignature() const {
// If we haven't computed the canonical signature yet, do so now.
if (CanonicalSignatureOrASTContext.isNull()) {
Expand Down
6 changes: 3 additions & 3 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7276,7 +7276,7 @@ void GenericSignatureBuilder::verifyGenericSignature(ASTContext &context,
}

// Canonicalize the signature to check that it is canonical.
(void)newSig->getCanonicalSignature();
(void)newSig.getCanonicalSignature();
}
}

Expand All @@ -7298,7 +7298,7 @@ void GenericSignatureBuilder::verifyGenericSignaturesInModule(
for (auto genericSig : allGenericSignatures) {
// Check whether this is the first time we've checked this (canonical)
// signature.
auto canGenericSig = genericSig->getCanonicalSignature();
auto canGenericSig = genericSig.getCanonicalSignature();
if (!knownGenericSignatures.insert(canGenericSig).second) continue;

verifyGenericSignature(context, canGenericSig);
Expand Down Expand Up @@ -7367,7 +7367,7 @@ AbstractGenericSignatureRequest::evaluate(
// generic signature builder.
if (!isCanonicalRequest(baseSignature, addedParameters, addedRequirements)) {
// Canonicalize the inputs so we can form the canonical request.
GenericSignature canBaseSignature = GenericSignature();
GenericSignature canBaseSignature;
if (baseSignature)
canBaseSignature = baseSignature->getCanonicalSignature();

Expand Down
4 changes: 2 additions & 2 deletions lib/AST/ProtocolConformance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,8 @@ void NormalProtocolConformance::differenceAndStoreConditionalRequirements()
}

auto extensionSig = ext->getGenericSignature();
auto canExtensionSig = extensionSig->getCanonicalSignature();
auto canTypeSig = typeSig->getCanonicalSignature();
auto canExtensionSig = extensionSig.getCanonicalSignature();
auto canTypeSig = typeSig.getCanonicalSignature();
if (canTypeSig == canExtensionSig) {
success({});
return;
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/RequirementEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ RequirementEnvironment::RequirementEnvironment(
reqSig->getRequirements().size() == 1) {
syntheticSignature = conformanceDC->getGenericSignatureOfContext();
if (syntheticSignature) {
syntheticSignature = syntheticSignature->getCanonicalSignature();
syntheticSignature = syntheticSignature.getCanonicalSignature();
syntheticEnvironment =
syntheticSignature->getGenericEnvironment();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/SubstitutionMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ bool SubstitutionMap::isCanonical() const {
SubstitutionMap SubstitutionMap::getCanonical() const {
if (empty()) return *this;

auto canonicalSig = getGenericSignature()->getCanonicalSignature();
auto canonicalSig = getGenericSignature().getCanonicalSignature();
SmallVector<Type, 4> replacementTypes;
for (Type replacementType : getReplacementTypesBuffer()) {
if (replacementType)
Expand Down
8 changes: 4 additions & 4 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@ CanType TypeBase::computeCanonicalType() {

CanGenericSignature genericSig;
if (auto *genericFnTy = dyn_cast<GenericFunctionType>(this))
genericSig = genericFnTy->getGenericSignature()->getCanonicalSignature();
genericSig = genericFnTy->getGenericSignature().getCanonicalSignature();

// Transform the parameter and result types.
SmallVector<AnyFunctionType::Param, 8> canParams;
Expand Down Expand Up @@ -2570,8 +2570,8 @@ static bool matches(CanType t1, CanType t2, TypeMatchOptions matchMode,
if (matchMode.contains(TypeMatchFlags::AllowCompatibleOpaqueTypeArchetypes))
if (auto opaque1 = t1->getAs<OpaqueTypeArchetypeType>())
if (auto opaque2 = t2->getAs<OpaqueTypeArchetypeType>())
return opaque1->getBoundSignature()->getCanonicalSignature() ==
opaque2->getBoundSignature()->getCanonicalSignature() &&
return opaque1->getBoundSignature().getCanonicalSignature() ==
opaque2->getBoundSignature().getCanonicalSignature() &&
opaque1->getInterfaceType()->getCanonicalType()->matches(
opaque2->getInterfaceType()->getCanonicalType(), matchMode);

Expand Down Expand Up @@ -3526,7 +3526,7 @@ static Type substGenericFunctionType(GenericFunctionType *genericFnType,
requirements.push_back(*substReqt);
}

GenericSignature genericSig = GenericSignature();
GenericSignature genericSig;
if (anySemanticChanges) {
// If there were semantic changes, we need to build a new generic
// signature.
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ namespace {
importObjCTypeParamDecl(const clang::ObjCTypeParamDecl *objcTypeParamDecl) {
// Pull the corresponding generic type parameter from the imported class.
const auto *typeParamContext = objcTypeParamDecl->getDeclContext();
GenericSignature genericSig = GenericSignature();
GenericSignature genericSig;
if (auto *category =
dyn_cast<clang::ObjCCategoryDecl>(typeParamContext)) {
auto ext = cast_or_null<ExtensionDecl>(
Expand Down
31 changes: 16 additions & 15 deletions lib/IRGen/GenArchetype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ llvm::Value *irgen::emitArchetypeWitnessTableRef(IRGenFunction &IGF,
// concretely available; we really ought to be comparing the full paths
// to this conformance from concrete sources.

auto signature = environment->getGenericSignature()->getCanonicalSignature();
auto signature = environment->getGenericSignature().getCanonicalSignature();
auto archetypeDepType = archetype->getInterfaceType();

auto astPath = signature->getConformanceAccessPath(archetypeDepType,
Expand Down Expand Up @@ -414,21 +414,22 @@ withOpaqueTypeGenericArgs(IRGenFunction &IGF,
} else {
SmallVector<llvm::Value *, 4> args;
SmallVector<llvm::Type *, 4> types;

enumerateGenericSignatureRequirements(
opaqueDecl->getGenericSignature()->getCanonicalSignature(),
[&](GenericRequirement reqt) {
auto ty = reqt.TypeParameter.subst(archetype->getSubstitutions())
->getCanonicalType(opaqueDecl->getGenericSignature());
if (reqt.Protocol) {
auto ref = ProtocolConformanceRef(reqt.Protocol)
.subst(reqt.TypeParameter, archetype->getSubstitutions());
args.push_back(emitWitnessTableRef(IGF, ty, ref));
} else {
args.push_back(IGF.emitAbstractTypeMetadataRef(ty));
}
types.push_back(args.back()->getType());
});
opaqueDecl->getGenericSignature().getCanonicalSignature(),
[&](GenericRequirement reqt) {
auto ty = reqt.TypeParameter.subst(archetype->getSubstitutions())
->getCanonicalType(opaqueDecl->getGenericSignature());
if (reqt.Protocol) {
auto ref =
ProtocolConformanceRef(reqt.Protocol)
.subst(reqt.TypeParameter, archetype->getSubstitutions());
args.push_back(emitWitnessTableRef(IGF, ty, ref));
} else {
args.push_back(IGF.emitAbstractTypeMetadataRef(ty));
}
types.push_back(args.back()->getType());
});
auto bufTy = llvm::StructType::get(IGF.IGM.LLVMContext, types);
alloca = IGF.createAlloca(bufTy, IGF.IGM.getPointerAlignment());
allocaSize = IGF.IGM.getPointerSize() * args.size();
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,7 @@ class FixedBoxTypeInfoBase : public BoxTypeInfo {

auto boxDescriptor = IGF.IGM.getAddrOfBoxDescriptor(
boxedInterfaceType,
env ? env->getGenericSignature()->getCanonicalSignature()
env ? env->getGenericSignature().getCanonicalSignature()
: CanGenericSignature());
llvm::Value *allocation = IGF.emitUnmanagedAlloc(layout, name,
boxDescriptor);
Expand Down
Loading