Skip to content

Commit 4797fae

Browse files
authored
Merge pull request #15497 from DougGregor/remove-name-alias-type
[AST] Fully replace NameAliasType with the new, sugary-sweet BoundNameAliasType
2 parents 6c5d2e9 + f5fd678 commit 4797fae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1209
-1195
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ namespace swift {
6565
class GenericTypeParamType;
6666
class LazyResolver;
6767
class ModuleDecl;
68-
class NameAliasType;
6968
class EnumCaseDecl;
7069
class EnumElementDecl;
7170
class ParameterList;

include/swift/AST/Module.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ namespace swift {
6262
class LinkLibrary;
6363
class LookupCache;
6464
class ModuleLoader;
65-
class NameAliasType;
6665
class NominalTypeDecl;
6766
class EnumElementDecl;
6867
class OperatorDecl;

include/swift/AST/TypeNodes.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ UNCHECKED_TYPE(TypeVariable, Type)
146146
ABSTRACT_SUGARED_TYPE(Sugar, Type)
147147
SUGARED_TYPE(Paren, SugarType)
148148
SUGARED_TYPE(NameAlias, SugarType)
149-
SUGARED_TYPE(BoundNameAlias, SugarType)
150149
ABSTRACT_SUGARED_TYPE(SyntaxSugar, SugarType)
151150
ABSTRACT_SUGARED_TYPE(UnarySyntaxSugar, SyntaxSugarType)
152151
SUGARED_TYPE(ArraySlice, UnarySyntaxSugarType)

include/swift/AST/Types.h

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
373373
GenericArgCount : 32
374374
);
375375

376-
SWIFT_INLINE_BITFIELD_FULL(BoundNameAliasType, SugarType, 1+16,
376+
SWIFT_INLINE_BITFIELD_FULL(NameAliasType, SugarType, 1+16,
377377
: NumPadBits,
378378

379379
/// Whether we have a parent type.
@@ -1512,8 +1512,8 @@ class SugarType : public TypeBase {
15121512
SugarType(TypeKind K, const ASTContext *ctx,
15131513
RecursiveTypeProperties properties)
15141514
: TypeBase(K, nullptr, properties), Context(ctx) {
1515-
if (K != TypeKind::NameAlias)
1516-
assert(ctx != nullptr && "Context for SugarType should not be null");
1515+
assert(ctx != nullptr &&
1516+
"Context for SugarType should not be null");
15171517
Bits.SugarType.HasCachedType = false;
15181518
}
15191519

@@ -1548,49 +1548,32 @@ class SugarType : public TypeBase {
15481548
}
15491549
};
15501550

1551-
/// NameAliasType - An alias type is a name for another type, just like a
1552-
/// typedef in C.
1553-
class NameAliasType : public SugarType {
1554-
friend class TypeAliasDecl;
1555-
// NameAliasType are never canonical.
1556-
NameAliasType(TypeAliasDecl *d)
1557-
: SugarType(TypeKind::NameAlias, (ASTContext*)nullptr,
1558-
RecursiveTypeProperties()),
1559-
TheDecl(d) {}
1560-
TypeAliasDecl *const TheDecl;
1561-
1562-
public:
1563-
TypeAliasDecl *getDecl() const { return TheDecl; }
1564-
1565-
using TypeBase::setRecursiveProperties;
1566-
1567-
// Implement isa/cast/dyncast/etc.
1568-
static bool classof(const TypeBase *T) {
1569-
return T->getKind() == TypeKind::NameAlias;
1570-
}
1571-
};
1572-
15731551
/// A reference to a type alias that is somehow generic, along with the
15741552
/// set of substitutions to apply to make the type concrete.
1575-
class BoundNameAliasType final
1553+
class NameAliasType final
15761554
: public SugarType, public llvm::FoldingSetNode,
1577-
llvm::TrailingObjects<BoundNameAliasType, Type, Substitution>
1555+
llvm::TrailingObjects<NameAliasType, Type, GenericSignature *,
1556+
Substitution>
15781557
{
15791558
TypeAliasDecl *typealias;
15801559

15811560
friend class ASTContext;
15821561
friend TrailingObjects;
15831562

1584-
BoundNameAliasType(TypeAliasDecl *typealias, Type parent,
1563+
NameAliasType(TypeAliasDecl *typealias, Type parent,
15851564
const SubstitutionMap &substitutions, Type underlying,
15861565
RecursiveTypeProperties properties);
15871566

15881567
unsigned getNumSubstitutions() const {
1589-
return Bits.BoundNameAliasType.NumSubstitutions;
1568+
return Bits.NameAliasType.NumSubstitutions;
15901569
}
15911570

15921571
size_t numTrailingObjects(OverloadToken<Type>) const {
1593-
return Bits.BoundNameAliasType.HasParent ? 1 : 0;
1572+
return Bits.NameAliasType.HasParent ? 1 : 0;
1573+
}
1574+
1575+
size_t numTrailingObjects(OverloadToken<GenericSignature *>) const {
1576+
return getNumSubstitutions() > 0 ? 1 : 0;
15941577
}
15951578

15961579
size_t numTrailingObjects(OverloadToken<Substitution>) const {
@@ -1603,8 +1586,15 @@ class BoundNameAliasType final
16031586
return {getTrailingObjects<Substitution>(), getNumSubstitutions()};
16041587
}
16051588

1589+
/// Retrieve the generic signature used for substitutions.
1590+
GenericSignature *getGenericSignature() const {
1591+
return getNumSubstitutions() > 0
1592+
? *getTrailingObjects<GenericSignature *>()
1593+
: nullptr;
1594+
}
1595+
16061596
public:
1607-
static BoundNameAliasType *get(TypeAliasDecl *typealias, Type parent,
1597+
static NameAliasType *get(TypeAliasDecl *typealias, Type parent,
16081598
const SubstitutionMap &substitutions,
16091599
Type underlying);
16101600

@@ -1617,7 +1607,7 @@ class BoundNameAliasType final
16171607
/// Retrieve the parent of this type as written, e.g., the part that was
16181608
/// written before ".", if provided.
16191609
Type getParent() const {
1620-
return Bits.BoundNameAliasType.HasParent ? *getTrailingObjects<Type>()
1610+
return Bits.NameAliasType.HasParent ? *getTrailingObjects<Type>()
16211611
: Type();
16221612
}
16231613

@@ -1637,11 +1627,12 @@ class BoundNameAliasType final
16371627
void Profile(llvm::FoldingSetNodeID &id) const;
16381628

16391629
static void Profile(llvm::FoldingSetNodeID &id, TypeAliasDecl *typealias,
1640-
Type parent, const SubstitutionMap &substitutions);
1630+
Type parent, const SubstitutionMap &substitutions,
1631+
Type underlying);
16411632

16421633
// Implement isa/cast/dyncast/etc.
16431634
static bool classof(const TypeBase *T) {
1644-
return T->getKind() == TypeKind::BoundNameAlias;
1635+
return T->getKind() == TypeKind::NameAlias;
16451636
}
16461637
};
16471638

include/swift/Serialization/DeclTypeRecordNodes.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878

7979
// These IDs must \em not be renumbered or reordered without incrementing
8080
// VERSION_MAJOR in ModuleFormat.h. Names, however, may change.
81-
FIRST_TYPE(NAME_ALIAS, 1)
81+
FIRST_TYPE(BUILTIN_ALIAS, 1)
8282
TYPE(GENERIC_TYPE_PARAM)
8383
TYPE(DEPENDENT_MEMBER)
8484
TYPE(NOMINAL)
@@ -106,7 +106,7 @@ TYPE(OPENED_EXISTENTIAL)
106106
TYPE(EXISTENTIAL_METATYPE)
107107
TYPE(SIL_BLOCK_STORAGE)
108108
TYPE(SIL_BOX)
109-
TYPE(BOUND_NAME_ALIAS)
109+
TYPE(NAME_ALIAS)
110110

111111
FIRST_DECL(TYPE_ALIAS, 60)
112112
DECL(GENERIC_TYPE_PARAM)

include/swift/Serialization/ModuleFormat.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -641,14 +641,14 @@ namespace decls_block {
641641
#include "swift/Serialization/DeclTypeRecordNodes.def"
642642
};
643643

644-
using NameAliasTypeLayout = BCRecordLayout<
645-
NAME_ALIAS_TYPE,
644+
using BuiltinAliasTypeLayout = BCRecordLayout<
645+
BUILTIN_ALIAS_TYPE,
646646
DeclIDField, // typealias decl
647647
TypeIDField // canonical type (a fallback)
648648
>;
649649

650-
using BoundNameAliasTypeLayout = BCRecordLayout<
651-
BOUND_NAME_ALIAS_TYPE,
650+
using NameAliasTypeLayout = BCRecordLayout<
651+
NAME_ALIAS_TYPE,
652652
DeclIDField, // typealias decl
653653
TypeIDField, // parent type
654654
TypeIDField // underlying type

lib/AST/ASTContext.cpp

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ FOR_KNOWN_FOUNDATION_TYPES(CACHE_FOUNDATION_DECL)
278278
/// arenas.
279279
struct Arena {
280280
llvm::DenseMap<Type, ErrorType *> ErrorTypesWithOriginal;
281-
llvm::FoldingSet<BoundNameAliasType> BoundNameAliasTypes;
281+
llvm::FoldingSet<NameAliasType> NameAliasTypes;
282282
llvm::FoldingSet<TupleType> TupleTypes;
283283
llvm::DenseMap<std::pair<Type,char>, MetatypeType*> MetatypeTypes;
284284
llvm::DenseMap<std::pair<Type,char>,
@@ -2887,46 +2887,55 @@ StringRef ASTContext::getSwiftName(KnownFoundationEntity kind) {
28872887
// Type manipulation routines.
28882888
//===----------------------------------------------------------------------===//
28892889

2890-
BoundNameAliasType::BoundNameAliasType(TypeAliasDecl *typealias, Type parent,
2890+
NameAliasType::NameAliasType(TypeAliasDecl *typealias, Type parent,
28912891
const SubstitutionMap &substitutions,
28922892
Type underlying,
28932893
RecursiveTypeProperties properties)
2894-
: SugarType(TypeKind::BoundNameAlias, underlying, properties),
2894+
: SugarType(TypeKind::NameAlias, underlying, properties),
28952895
typealias(typealias) {
28962896
// Record the parent (or absence of a parent).
28972897
if (parent) {
2898-
Bits.BoundNameAliasType.HasParent = true;
2898+
Bits.NameAliasType.HasParent = true;
28992899
*getTrailingObjects<Type>() = parent;
29002900
} else {
2901-
Bits.BoundNameAliasType.HasParent = false;
2901+
Bits.NameAliasType.HasParent = false;
29022902
}
29032903

29042904
// Record the substitutions.
2905-
if (auto genericSig = typealias->getGenericSignature()) {
2905+
if (auto genericSig = substitutions.getGenericSignature()) {
29062906
SmallVector<Substitution, 4> flatSubs;
29072907
genericSig->getSubstitutions(substitutions, flatSubs);
2908-
Bits.BoundNameAliasType.NumSubstitutions = flatSubs.size();
2908+
Bits.NameAliasType.NumSubstitutions = flatSubs.size();
29092909
std::copy(flatSubs.begin(), flatSubs.end(),
29102910
getTrailingObjects<Substitution>());
2911+
2912+
*getTrailingObjects<GenericSignature *>() = genericSig;
29112913
} else {
2912-
Bits.BoundNameAliasType.NumSubstitutions = 0;
2914+
Bits.NameAliasType.NumSubstitutions = 0;
29132915
}
29142916
}
29152917

2916-
BoundNameAliasType *BoundNameAliasType::get(
2918+
NameAliasType *NameAliasType::get(
29172919
TypeAliasDecl *typealias,
29182920
Type parent,
29192921
const SubstitutionMap &substitutions,
29202922
Type underlying) {
29212923
// Compute the recursive properties.
2924+
//
29222925
auto properties = underlying->getRecursiveProperties();
2923-
if (parent)
2926+
auto storedProperties = properties;
2927+
if (parent) {
29242928
properties |= parent->getRecursiveProperties();
2925-
auto genericSig = typealias->getGenericSignature();
2929+
if (parent->hasTypeVariable())
2930+
storedProperties |= RecursiveTypeProperties::HasTypeVariable;
2931+
}
2932+
auto genericSig = substitutions.getGenericSignature();
29262933
if (genericSig) {
29272934
for (Type gp : genericSig->getGenericParams()) {
2928-
properties |= gp.subst(substitutions, SubstFlags::UseErrorType)
2929-
->getRecursiveProperties();
2935+
auto substGP = gp.subst(substitutions, SubstFlags::UseErrorType);
2936+
properties |= substGP->getRecursiveProperties();
2937+
if (substGP->hasTypeVariable())
2938+
storedProperties |= RecursiveTypeProperties::HasTypeVariable;
29302939
}
29312940
}
29322941

@@ -2936,37 +2945,43 @@ BoundNameAliasType *BoundNameAliasType::get(
29362945

29372946
// Profile the type.
29382947
llvm::FoldingSetNodeID id;
2939-
BoundNameAliasType::Profile(id, typealias, parent, substitutions);
2948+
NameAliasType::Profile(id, typealias, parent, substitutions,
2949+
underlying);
29402950

29412951
// Did we already record this type?
29422952
void *insertPos;
2943-
auto &types = ctx.Impl.getArena(arena).BoundNameAliasTypes;
2953+
auto &types = ctx.Impl.getArena(arena).NameAliasTypes;
29442954
if (auto result = types.FindNodeOrInsertPos(id, insertPos))
29452955
return result;
29462956

29472957
// Build a new type.
29482958
unsigned numSubstitutions =
29492959
genericSig ? genericSig->getSubstitutionListSize() : 0;
2960+
assert(static_cast<bool>(genericSig) == numSubstitutions > 0);
29502961
auto size =
2951-
totalSizeToAlloc<Type, Substitution>(parent ? 1 : 0, numSubstitutions);
2952-
auto mem = ctx.Allocate(size, alignof(BoundNameAliasType), arena);
2953-
auto result = new (mem) BoundNameAliasType(typealias, parent, substitutions,
2954-
underlying, properties);
2962+
totalSizeToAlloc<Type, GenericSignature *, Substitution>(
2963+
parent ? 1 : 0, genericSig ? 1 : 0, numSubstitutions);
2964+
auto mem = ctx.Allocate(size, alignof(NameAliasType), arena);
2965+
auto result = new (mem) NameAliasType(typealias, parent, substitutions,
2966+
underlying, storedProperties);
29552967
types.InsertNode(result, insertPos);
29562968
return result;
29572969
}
29582970

2959-
void BoundNameAliasType::Profile(llvm::FoldingSetNodeID &id) const {
2960-
Profile(id, getDecl(), getParent(), getSubstitutionMap());
2971+
void NameAliasType::Profile(llvm::FoldingSetNodeID &id) const {
2972+
Profile(id, getDecl(), getParent(), getSubstitutionMap(),
2973+
Type(getSinglyDesugaredType()));
29612974
}
29622975

2963-
void BoundNameAliasType::Profile(
2976+
void NameAliasType::Profile(
29642977
llvm::FoldingSetNodeID &id,
29652978
TypeAliasDecl *typealias,
2966-
Type parent, const SubstitutionMap &substitutions) {
2979+
Type parent, const SubstitutionMap &substitutions,
2980+
Type underlying) {
29672981
id.AddPointer(typealias);
29682982
id.AddPointer(parent.getPointer());
29692983
substitutions.profile(id);
2984+
id.AddPointer(underlying.getPointer());
29702985
}
29712986

29722987
// Simple accessors.

lib/AST/ASTDumper.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3032,12 +3032,6 @@ namespace {
30323032
void visitNameAliasType(NameAliasType *T, StringRef label) {
30333033
printCommon(label, "name_alias_type");
30343034
printField("decl", T->getDecl()->printRef());
3035-
OS << ")";
3036-
}
3037-
3038-
void visitBoundNameAliasType(BoundNameAliasType *T, StringRef label) {
3039-
printCommon(label, "bound_name_alias_type");
3040-
printField("decl", T->getDecl()->printRef());
30413035
if (T->getParent())
30423036
printRec("parent", T->getParent());
30433037

lib/AST/ASTMangler.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -735,24 +735,20 @@ void ASTMangler::appendType(Type type) {
735735
cast<BuiltinVectorType>(tybase)->getNumElements());
736736
case TypeKind::NameAlias: {
737737
assert(DWARFMangling && "sugared types are only legal for the debugger");
738-
auto NameAliasTy = cast<NameAliasType>(tybase);
739-
TypeAliasDecl *decl = NameAliasTy->getDecl();
738+
auto aliasTy = cast<NameAliasType>(tybase);
739+
740+
// It's not possible to mangle the context of the builtin module.
741+
// FIXME: We also cannot yet mangle references to typealiases that
742+
// involve generics.
743+
TypeAliasDecl *decl = aliasTy->getDecl();
740744
if (decl->getModuleContext() == decl->getASTContext().TheBuiltinModule) {
741-
// It's not possible to mangle the context of the builtin module.
742-
return appendType(NameAliasTy->getSinglyDesugaredType());
745+
return appendType(aliasTy->getSinglyDesugaredType());
743746
}
744747

745748
// For the DWARF output we want to mangle the type alias + context,
746749
// unless the type alias references a builtin type.
747750
return appendAnyGenericType(decl);
748751
}
749-
case TypeKind::BoundNameAlias: {
750-
assert(DWARFMangling && "sugared types are only legal for the debugger");
751-
auto boundAliasTy = cast<BoundNameAliasType>(tybase);
752-
753-
// FIXME: Mangle as a generic type.
754-
return appendType(boundAliasTy->getSinglyDesugaredType());
755-
}
756752

757753
case TypeKind::Paren:
758754
return appendSugaredType<ParenType>(type);

lib/AST/ASTPrinter.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3269,26 +3269,6 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
32693269
return;
32703270
}
32713271

3272-
auto ParentDC = T->getDecl()->getDeclContext();
3273-
auto ParentNominal = ParentDC ?
3274-
ParentDC->getAsNominalTypeOrNominalTypeExtensionContext() : nullptr;
3275-
3276-
if (ParentNominal) {
3277-
visit(ParentNominal->getDeclaredType());
3278-
Printer << ".";
3279-
} else if (shouldPrintFullyQualified(T)) {
3280-
printModuleContext(T);
3281-
}
3282-
3283-
printTypeDeclName(T);
3284-
}
3285-
3286-
void visitBoundNameAliasType(BoundNameAliasType *T) {
3287-
if (Options.PrintForSIL || Options.PrintNameAliasUnderlyingType) {
3288-
visit(T->getSinglyDesugaredType());
3289-
return;
3290-
}
3291-
32923272
if (auto parent = T->getParent()) {
32933273
visit(parent);
32943274
Printer << ".";

0 commit comments

Comments
 (0)