Skip to content

Commit df97e44

Browse files
committed
[AST] Replace SubstitutionList in NameAliasType with a SubstitutionMap.
Convert NameAliasType’s internal representation from tail-allocating an array of Substitutions (to be treated as a SubstitutionList) to store a single SubstitutionMap. Serialize using that SubstitutionMap.
1 parent ab56fa3 commit df97e44

File tree

7 files changed

+61
-97
lines changed

7 files changed

+61
-97
lines changed

include/swift/AST/GenericSignature.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "swift/AST/PrintOptions.h"
2121
#include "swift/AST/Requirement.h"
2222
#include "swift/AST/SubstitutionList.h"
23-
#include "swift/AST/Types.h"
23+
#include "swift/AST/Type.h"
2424
#include "llvm/ADT/ArrayRef.h"
2525
#include "llvm/ADT/FoldingSet.h"
2626
#include "llvm/ADT/SmallVector.h"
@@ -361,14 +361,6 @@ CanGenericSignature::CanGenericSignature(GenericSignature *Signature)
361361
{
362362
assert(!Signature || Signature->isCanonical());
363363
}
364-
365-
inline ArrayRef<CanTypeWrapper<GenericTypeParamType>>
366-
CanGenericSignature::getGenericParams() const{
367-
auto params = Signature->getGenericParams().getOriginalArray();
368-
auto base = static_cast<const CanTypeWrapper<GenericTypeParamType>*>(
369-
params.data());
370-
return {base, params.size()};
371-
}
372364

373365
} // end namespace swift
374366

include/swift/AST/Types.h

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "swift/AST/Requirement.h"
2626
#include "swift/AST/SILLayout.h"
2727
#include "swift/AST/SubstitutionList.h"
28+
#include "swift/AST/SubstitutionMap.h"
2829
#include "swift/AST/Type.h"
2930
#include "swift/AST/TypeAlignments.h"
3031
#include "swift/Basic/ArrayRefView.h"
@@ -373,14 +374,14 @@ class alignas(1 << TypeAlignInBits) TypeBase {
373374
GenericArgCount : 32
374375
);
375376

376-
SWIFT_INLINE_BITFIELD_FULL(NameAliasType, SugarType, 1+16,
377+
SWIFT_INLINE_BITFIELD_FULL(NameAliasType, SugarType, 1+1,
377378
: NumPadBits,
378379

379380
/// Whether we have a parent type.
380381
HasParent : 1,
381382

382-
/// The number of substitutions.
383-
NumSubstitutions : 16
383+
/// Whether we have a substitution map.
384+
HasSubstitutionMap : 1
384385
);
385386

386387
} Bits;
@@ -1592,39 +1593,28 @@ class SugarType : public TypeBase {
15921593
/// set of substitutions to apply to make the type concrete.
15931594
class NameAliasType final
15941595
: public SugarType, public llvm::FoldingSetNode,
1595-
llvm::TrailingObjects<NameAliasType, Type, GenericSignature *,
1596-
Substitution>
1596+
llvm::TrailingObjects<NameAliasType, Type, SubstitutionMap>
15971597
{
15981598
TypeAliasDecl *typealias;
15991599

16001600
friend class ASTContext;
16011601
friend TrailingObjects;
16021602

16031603
NameAliasType(TypeAliasDecl *typealias, Type parent,
1604-
const SubstitutionMap &substitutions, Type underlying,
1605-
RecursiveTypeProperties properties);
1606-
1607-
unsigned getNumSubstitutions() const {
1608-
return Bits.NameAliasType.NumSubstitutions;
1609-
}
1604+
const SubstitutionMap &substitutions, Type underlying,
1605+
RecursiveTypeProperties properties);
16101606

16111607
size_t numTrailingObjects(OverloadToken<Type>) const {
16121608
return Bits.NameAliasType.HasParent ? 1 : 0;
16131609
}
16141610

1615-
size_t numTrailingObjects(OverloadToken<GenericSignature *>) const {
1616-
return getNumSubstitutions() > 0 ? 1 : 0;
1617-
}
1618-
1619-
size_t numTrailingObjects(OverloadToken<Substitution>) const {
1620-
return getNumSubstitutions();
1611+
size_t numTrailingObjects(OverloadToken<SubstitutionMap>) const {
1612+
return Bits.NameAliasType.HasSubstitutionMap ? 1 : 0;
16211613
}
16221614

16231615
/// Retrieve the generic signature used for substitutions.
16241616
GenericSignature *getGenericSignature() const {
1625-
return getNumSubstitutions() > 0
1626-
? *getTrailingObjects<GenericSignature *>()
1627-
: nullptr;
1617+
return getSubstitutionMap().getGenericSignature();
16281618
}
16291619

16301620
public:
@@ -1642,18 +1632,17 @@ class NameAliasType final
16421632
/// written before ".", if provided.
16431633
Type getParent() const {
16441634
return Bits.NameAliasType.HasParent ? *getTrailingObjects<Type>()
1645-
: Type();
1646-
}
1647-
1648-
/// Retrieve the set of substitutions to be applied to the declaration to
1649-
/// produce the underlying type.
1650-
SubstitutionList getSubstitutionList() const {
1651-
return {getTrailingObjects<Substitution>(), getNumSubstitutions()};
1635+
: Type();
16521636
}
16531637

16541638
/// Retrieve the substitution map applied to the declaration's underlying
16551639
/// to produce the described type.
1656-
SubstitutionMap getSubstitutionMap() const;
1640+
SubstitutionMap getSubstitutionMap() const {
1641+
if (!Bits.NameAliasType.HasSubstitutionMap)
1642+
return SubstitutionMap();
1643+
1644+
return *getTrailingObjects<SubstitutionMap>();
1645+
}
16571646

16581647
/// Get the innermost generic arguments, which correspond to the generic
16591648
/// arguments that are directly applied to the typealias declaration in
@@ -5395,6 +5384,14 @@ inline bool TypeBase::hasSimpleTypeRepr() const {
53955384
}
53965385
}
53975386

5387+
inline ArrayRef<CanTypeWrapper<GenericTypeParamType>>
5388+
CanGenericSignature::getGenericParams() const{
5389+
auto params = Signature->getGenericParams().getOriginalArray();
5390+
auto base = static_cast<const CanTypeWrapper<GenericTypeParamType>*>(
5391+
params.data());
5392+
return {base, params.size()};
5393+
}
5394+
53985395
} // end namespace swift
53995396

54005397
namespace llvm {
@@ -5421,7 +5418,6 @@ struct DenseMapInfo<swift::BuiltinIntegerWidth> {
54215418
}
54225419
};
54235420

5424-
54255421
}
54265422

54275423
#endif

include/swift/Serialization/ModuleFormat.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const uint16_t VERSION_MAJOR = 0;
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
5757
/// Don't worry about adhering to the 80-column limit for this line.
58-
const uint16_t VERSION_MINOR = 409; // Last change: standalone requirement subs
58+
const uint16_t VERSION_MINOR = 410; // Last change: NameAlias substitution map
5959

6060
using DeclIDField = BCFixed<31>;
6161

@@ -654,10 +654,10 @@ namespace decls_block {
654654

655655
using NameAliasTypeLayout = BCRecordLayout<
656656
NAME_ALIAS_TYPE,
657-
DeclIDField, // typealias decl
658-
TypeIDField, // parent type
659-
TypeIDField // underlying type
660-
// trailing substitutions
657+
DeclIDField, // typealias decl
658+
TypeIDField, // parent type
659+
TypeIDField, // underlying type
660+
SubstitutionMapIDField // substitution map
661661
>;
662662

663663
using GenericTypeParamTypeLayout = BCRecordLayout<

lib/AST/ASTContext.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "swift/AST/ParameterList.h"
3131
#include "swift/AST/ProtocolConformance.h"
3232
#include "swift/AST/RawComment.h"
33+
#include "swift/AST/SubstitutionMap.h"
3334
#include "swift/AST/SILLayout.h"
3435
#include "swift/AST/TypeCheckerDebugConsumer.h"
3536
#include "swift/Basic/Compiler.h"
@@ -2883,9 +2884,9 @@ StringRef ASTContext::getSwiftName(KnownFoundationEntity kind) {
28832884
//===----------------------------------------------------------------------===//
28842885

28852886
NameAliasType::NameAliasType(TypeAliasDecl *typealias, Type parent,
2886-
const SubstitutionMap &substitutions,
2887-
Type underlying,
2888-
RecursiveTypeProperties properties)
2887+
const SubstitutionMap &substitutions,
2888+
Type underlying,
2889+
RecursiveTypeProperties properties)
28892890
: SugarType(TypeKind::NameAlias, underlying, properties),
28902891
typealias(typealias) {
28912892
// Record the parent (or absence of a parent).
@@ -2898,15 +2899,10 @@ NameAliasType::NameAliasType(TypeAliasDecl *typealias, Type parent,
28982899

28992900
// Record the substitutions.
29002901
if (auto genericSig = substitutions.getGenericSignature()) {
2901-
SmallVector<Substitution, 4> flatSubs;
2902-
genericSig->getSubstitutions(substitutions, flatSubs);
2903-
Bits.NameAliasType.NumSubstitutions = flatSubs.size();
2904-
std::copy(flatSubs.begin(), flatSubs.end(),
2905-
getTrailingObjects<Substitution>());
2906-
2907-
*getTrailingObjects<GenericSignature *>() = genericSig;
2902+
Bits.NameAliasType.HasSubstitutionMap = true;
2903+
*getTrailingObjects<SubstitutionMap>() = substitutions;
29082904
} else {
2909-
Bits.NameAliasType.NumSubstitutions = 0;
2905+
Bits.NameAliasType.HasSubstitutionMap = false;
29102906
}
29112907
}
29122908

@@ -2947,11 +2943,8 @@ NameAliasType *NameAliasType::get(TypeAliasDecl *typealias, Type parent,
29472943
return result;
29482944

29492945
// Build a new type.
2950-
unsigned numSubstitutions =
2951-
genericSig ? genericSig->getSubstitutionListSize() : 0;
2952-
auto size =
2953-
totalSizeToAlloc<Type, GenericSignature *, Substitution>(
2954-
parent ? 1 : 0, genericSig ? 1 : 0, numSubstitutions);
2946+
auto size = totalSizeToAlloc<Type, SubstitutionMap>(parent ? 1 : 0,
2947+
genericSig ? 1 : 0);
29552948
auto mem = ctx.Allocate(size, alignof(NameAliasType), arena);
29562949
auto result = new (mem) NameAliasType(typealias, parent, substitutions,
29572950
underlying, storedProperties);

lib/AST/Type.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,33 +1340,24 @@ Type SugarType::getSinglyDesugaredTypeSlow() {
13401340
return UnderlyingType;
13411341
}
13421342

1343-
SubstitutionMap NameAliasType::getSubstitutionMap() const {
1344-
if (auto genericSig = getGenericSignature())
1345-
return genericSig->getSubstitutionMap(getSubstitutionList());
1346-
1347-
return SubstitutionMap();
1348-
}
1349-
13501343
SmallVector<Type, 2> NameAliasType::getInnermostGenericArgs() const {
13511344
SmallVector<Type, 2> result;
13521345

13531346
// If the typealias is not generic, there are no generic arguments
13541347
if (!typealias->isGeneric()) return result;
13551348

1356-
auto genericSig = typealias->getGenericSignature();
1357-
if (!genericSig) return result;
1358-
1359-
// If the substitution list was empty, bail out.
1360-
if (getSubstitutionList().empty()) return result;
1349+
// If the substitution map is empty, bail out.
1350+
auto subMap = getSubstitutionMap();
1351+
if (subMap.empty()) return result;
13611352

13621353
// Retrieve the substitutions for the generic parameters (only).
1354+
auto genericSig = subMap.getGenericSignature();
13631355
unsigned numAllGenericParams = genericSig->getGenericParams().size();
1364-
auto genericArgSubs = getSubstitutionList().slice(0, numAllGenericParams);
1365-
1366-
// Copy the replacement types for the innermost generic arguments.
13671356
unsigned numMyGenericParams = typealias->getGenericParams()->size();
1368-
for (const auto &sub : genericArgSubs.take_back(numMyGenericParams)) {
1369-
result.push_back(sub.getReplacement());
1357+
result.reserve(numMyGenericParams);
1358+
unsigned startIndex = numAllGenericParams - numMyGenericParams;
1359+
for (auto gp : genericSig->getGenericParams().slice(startIndex)) {
1360+
result.push_back(Type(gp).subst(subMap, SubstFlags::UseErrorType));
13701361
}
13711362
return result;
13721363
}

lib/Serialization/Deserialization.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4288,9 +4288,11 @@ Expected<Type> ModuleFile::getTypeChecked(TypeID TID) {
42884288
DeclID typealiasID;
42894289
TypeID parentTypeID;
42904290
TypeID underlyingTypeID;
4291+
SubstitutionMapID substitutionsID;
42914292
decls_block::NameAliasTypeLayout::readRecord(scratch, typealiasID,
4292-
parentTypeID,
4293-
underlyingTypeID);
4293+
parentTypeID,
4294+
underlyingTypeID,
4295+
substitutionsID);
42944296
auto aliasOrError = getDeclChecked(typealiasID);
42954297
if (!aliasOrError)
42964298
return aliasOrError.takeError();
@@ -4318,16 +4320,7 @@ Expected<Type> ModuleFile::getTypeChecked(TypeID TID) {
43184320
Type parentType = getType(parentTypeID);
43194321

43204322
// Read the substitutions.
4321-
SubstitutionMap subMap;
4322-
if (auto genericSig = alias->getGenericSignature()) {
4323-
SmallVector<Substitution, 4> substitutions;
4324-
for (unsigned i : range(genericSig->getSubstitutionListSize())) {
4325-
(void)i;
4326-
substitutions.push_back(*maybeReadSubstitution(DeclTypeCursor));
4327-
}
4328-
4329-
subMap = genericSig->getSubstitutionMap(substitutions);
4330-
}
4323+
SubstitutionMap subMap = getSubstitutionMap(substitutionsID);
43314324

43324325
// Look through compatibility aliases that are now unavailable.
43334326
if (alias->getAttrs().isUnavailable(ctx) &&
@@ -4337,7 +4330,7 @@ Expected<Type> ModuleFile::getTypeChecked(TypeID TID) {
43374330
}
43384331

43394332
typeOrOffset = NameAliasType::get(alias, parentType, subMap,
4340-
underlyingType);
4333+
underlyingType);
43414334
break;
43424335
}
43434336
case decls_block::NOMINAL_TYPE: {

lib/Serialization/Serialization.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3666,14 +3666,13 @@ void Serializer::writeType(Type ty) {
36663666

36673667
unsigned abbrCode = DeclTypeAbbrCodes[NameAliasTypeLayout::Code];
36683668
NameAliasTypeLayout::emitRecord(
3669-
Out, ScratchRecord, abbrCode,
3670-
addDeclRef(typeAlias,
3671-
/*forceSerialization*/false,
3672-
/*allowTypeAliasXRef*/true),
3673-
addTypeRef(alias->getParent()),
3674-
addTypeRef(alias->getSinglyDesugaredType()));
3675-
// Write the set of substitutions.
3676-
writeSubstitutions(alias->getSubstitutionList(), DeclTypeAbbrCodes);
3669+
Out, ScratchRecord, abbrCode,
3670+
addDeclRef(typeAlias,
3671+
/*forceSerialization*/false,
3672+
/*allowTypeAliasXRef*/true),
3673+
addTypeRef(alias->getParent()),
3674+
addTypeRef(alias->getSinglyDesugaredType()),
3675+
addSubstitutionMapRef(alias->getSubstitutionMap()));
36773676
break;
36783677
}
36793678

0 commit comments

Comments
 (0)