1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
//===--- GenericSpecializationMangler.cpp - mangling of specializations ---===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
//
//===----------------------------------------------------------------------===//
#include "swift/SIL/GenericSpecializationMangler.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/GenericSignature.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/Demangling/ManglingMacros.h"
using namespace swift;
using namespace Mangle;
void SpecializationMangler::beginMangling() {
ASTMangler::beginManglingWithoutPrefix();
if (Serialized)
ArgOpBuffer << 'q';
if (RemovedEffects.contains(EffectKind::Async))
ArgOpBuffer << 'a';
ArgOpBuffer << char(uint8_t(Pass) + '0');
}
namespace {
/// Utility class for demangling specialization attributes.
class AttributeDemangler : public Demangle::Demangler {
public:
void demangleAndAddAsChildren(StringRef MangledSpecialization,
NodePointer Parent) {
DemangleInitRAII state(*this, MangledSpecialization, nullptr);
if (!parseAndPushNodes()) {
llvm::errs() << "Can't demangle: " << MangledSpecialization << '\n';
abort();
}
for (Node *Nd : NodeStack) {
addChild(Parent, Nd);
}
}
};
} // namespace
std::string SpecializationMangler::finalize() {
StringRef MangledSpecialization(Storage.data(), Storage.size());
AttributeDemangler D;
NodePointer TopLevel = D.createNode(Node::Kind::Global);
D.demangleAndAddAsChildren(MangledSpecialization, TopLevel);
StringRef FuncName = Function ? Function->getName() : StringRef(FunctionName);
NodePointer FuncTopLevel = nullptr;
if (FuncName.starts_with(MANGLING_PREFIX_STR)) {
FuncTopLevel = D.demangleSymbol(FuncName);
assert(FuncTopLevel);
}
if (!FuncTopLevel) {
FuncTopLevel = D.createNode(Node::Kind::Global);
FuncTopLevel->addChild(D.createNode(Node::Kind::Identifier, FuncName), D);
}
for (NodePointer FuncChild : *FuncTopLevel) {
TopLevel->addChild(FuncChild, D);
}
auto mangling = Demangle::mangleNode(TopLevel);
assert(mangling.isSuccess());
std::string mangledName = mangling.result();
verify(mangledName);
return mangledName;
}
//===----------------------------------------------------------------------===//
// Generic Specialization
//===----------------------------------------------------------------------===//
void GenericSpecializationMangler::
appendSubstitutions(GenericSignature sig, SubstitutionMap subs) {
bool First = true;
sig->forEachParam([&](GenericTypeParamType *ParamType, bool Canonical) {
if (Canonical) {
auto ty = Type(ParamType);
auto substTy = ty.subst(subs);
auto canTy = substTy->getCanonicalType();
appendType(canTy, nullptr);
appendListSeparator(First);
}
});
assert(!First && "no generic substitutions");
}
std::string GenericSpecializationMangler::
manglePrespecialized(GenericSignature sig, SubstitutionMap subs) {
beginMangling();
appendSubstitutions(sig, subs);
appendSpecializationOperator("Ts");
return finalize();
}
std::string GenericSpecializationMangler::
mangleNotReabstracted(SubstitutionMap subs,
bool metatyeParamsRemoved) {
beginMangling();
appendSubstitutions(getGenericSignature(), subs);
if (metatyeParamsRemoved) {
appendSpecializationOperator("TGm");
} else {
appendSpecializationOperator("TG");
}
return finalize();
}
std::string GenericSpecializationMangler::
mangleReabstracted(SubstitutionMap subs, bool alternativeMangling,
bool metatyeParamsRemoved) {
beginMangling();
appendSubstitutions(getGenericSignature(), subs);
// See ReabstractionInfo::hasConvertedResilientParams for why and when to use
// the alternative mangling.
if (metatyeParamsRemoved) {
appendSpecializationOperator(alternativeMangling ? "TBm" : "Tgm");
} else {
appendSpecializationOperator(alternativeMangling ? "TB" : "Tg");
}
return finalize();
}
std::string GenericSpecializationMangler::
mangleForDebugInfo(GenericSignature sig, SubstitutionMap subs, bool forInlining) {
beginMangling();
appendSubstitutions(sig, subs);
appendSpecializationOperator(forInlining ? "Ti" : "TG");
return finalize();
}
static SubstitutionMap
getSubstitutionMapForPrespecialization(GenericSignature genericSig,
GenericSignature specSig) {
auto CalleeGenericSig = genericSig;
auto SpecializedGenericSig = specSig;
auto SpecializedGenericEnv = specSig.getGenericEnvironment();
auto CalleeInterfaceToSpecializedInterfaceMap = SubstitutionMap::get(
CalleeGenericSig,
[&](SubstitutableType *type) -> Type {
return type;
},
LookUpConformanceInSignature(CalleeGenericSig.getPointer()));
auto subs = SubstitutionMap::get(
CalleeGenericSig,
[&](SubstitutableType *type) -> Type {
auto SpecializedInterfaceTy =
Type(type).subst(CalleeInterfaceToSpecializedInterfaceMap);
return SpecializedGenericEnv->mapTypeIntoContext(
SpecializedInterfaceTy);
},
LookUpConformanceInSignature(SpecializedGenericSig.getPointer()));
return subs;
}
std::string GenericSpecializationMangler::manglePrespecialization(
std::string unspecializedName, GenericSignature genericSig,
GenericSignature specializedSig) {
auto subs =
getSubstitutionMapForPrespecialization(genericSig, specializedSig);
GenericSpecializationMangler mangler(unspecializedName);
return mangler.manglePrespecialized(genericSig, subs);
}
|