Swift: mangle nominal and bound generic types

This commit is contained in:
Paolo Tranquilli
2023-03-14 13:46:01 +01:00
parent 1274aaaf9c
commit b6146478dc
3 changed files with 32 additions and 12 deletions

View File

@@ -70,13 +70,17 @@ SwiftMangledName SwiftMangler::visitModuleDecl(const swift::ModuleDecl* decl) {
}
SwiftMangledName SwiftMangler::visitGenericTypeDecl(const swift::GenericTypeDecl* decl) {
if (auto context = decl->getDeclContext()->getAsDecl()) {
auto ret = initMangled(decl);
ret << dispatcher.fetchLabel(context);
ret << decl->getName().str();
return ret;
auto context = decl->getDeclContext();
if (context->isLocalContext()) {
return {};
}
return {};
auto parent = (context->getContextKind() == swift::DeclContextKind::FileUnit)
? decl->getModuleContext()
: context->getAsDecl();
auto ret = initMangled(decl);
ret << dispatcher.fetchLabel(parent);
ret << decl->getName().str();
return ret;
}
SwiftMangledName SwiftMangler::visitModuleType(const swift::ModuleType* type) {
@@ -112,3 +116,15 @@ SwiftMangledName SwiftMangler::visitAnyGenericType(const swift::AnyGenericType*
ret << dispatcher.fetchLabel(type->getDecl());
return ret;
}
SwiftMangledName SwiftMangler::visitType(const swift::TypeBase* type) {
return {};
}
SwiftMangledName SwiftMangler::visitBoundGenericType(const swift::BoundGenericType* type) {
auto ret = visitAnyGenericType(type);
for (const auto param : type->getGenericArgs()) {
ret << dispatcher.fetchLabel(param);
}
return ret;
}

View File

@@ -50,14 +50,20 @@ class SwiftMangler : private swift::TypeVisitor<SwiftMangler, SwiftMangledName>,
// default fallback for not yet mangled types. This should never be called in normal situations
// will just spawn a random name
// TODO: make it assert once we mangle all types
static SwiftMangledName visitType(const swift::TypeBase* type) { return {}; }
SwiftMangledName visitType(const swift::TypeBase* type);
SwiftMangledName visitModuleType(const swift::ModuleType* type);
SwiftMangledName visitTupleType(const swift::TupleType* type);
SwiftMangledName visitBuiltinType(const swift::BuiltinType* type);
SwiftMangledName visitAnyGenericType(const swift::AnyGenericType* type);
SwiftMangledName visitBuiltinType(const swift::BuiltinType* type);
// shouldn't be required, but they forgot to link `NominalType` to its direct superclass
// in swift/AST/TypeNodes.def, so we need to chain the call manually
SwiftMangledName visitNominalType(const swift::NominalType* type) {
return visitAnyGenericType(type);
}
SwiftMangledName visitBoundGenericType(const swift::BoundGenericType* type);
private:
swift::Mangle::ASTMangler mangler;

View File

@@ -1,10 +1,8 @@
| (Int) -> Int | FunctionType | function(_:) |
| (Int) -> Int | FunctionType | use_function |
| <T> (Generic<T>.Type) -> () -> Generic<T> | GenericFunctionType | Generic<T>.init() |
| Generic<Int> | BoundGenericStructType | instantiated_generic |
| Generic<Int> | BoundGenericStructType | use_instantiated_generic |
| Generic<Int> | BoundGenericStructType | instantiated_generic, use_instantiated_generic |
| Generic<T> | BoundGenericStructType | self |
| Generic<T>.Type | MetatypeType | Generic |
| Int | StructType | _, x |
| Int | StructType | use_x |
| Int | StructType | _, use_x, x |
| T.Type | MetatypeType | T |