Skip to content

Commit f0c783a

Browse files
authored
Merge pull request #82936 from slavapestov/builtin-fixed-array-cleanup
BuiltinFixedArray cleanup
2 parents 8271aee + abd15af commit f0c783a

File tree

7 files changed

+62
-41
lines changed

7 files changed

+62
-41
lines changed

include/swift/AST/Types.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,18 +1773,10 @@ class BuiltinFixedArrayType : public BuiltinType, public llvm::FoldingSetNode {
17731773
CanType Size;
17741774
CanType ElementType;
17751775

1776-
static RecursiveTypeProperties
1777-
getRecursiveTypeProperties(CanType Size, CanType Element) {
1778-
RecursiveTypeProperties properties;
1779-
properties |= Size->getRecursiveProperties();
1780-
properties |= Element->getRecursiveProperties();
1781-
return properties;
1782-
}
1783-
1784-
BuiltinFixedArrayType(CanType Size,
1785-
CanType ElementType)
1776+
BuiltinFixedArrayType(CanType Size, CanType ElementType,
1777+
RecursiveTypeProperties properties)
17861778
: BuiltinType(TypeKind::BuiltinFixedArray, ElementType->getASTContext(),
1787-
getRecursiveTypeProperties(Size, ElementType)),
1779+
properties),
17881780
Size(Size),
17891781
ElementType(ElementType)
17901782
{}

lib/AST/ASTContext.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ struct ASTContext::Implementation {
567567
llvm::DenseMap<Type, ExistentialType *> ExistentialTypes;
568568
llvm::FoldingSet<UnboundGenericType> UnboundGenericTypes;
569569
llvm::FoldingSet<BoundGenericType> BoundGenericTypes;
570+
llvm::FoldingSet<BuiltinFixedArrayType> BuiltinFixedArrayTypes;
570571
llvm::FoldingSet<ProtocolCompositionType> ProtocolCompositionTypes;
571572
llvm::FoldingSet<ParameterizedProtocolType> ParameterizedProtocolTypes;
572573
llvm::FoldingSet<LayoutConstraintInfo> LayoutConstraints;
@@ -632,7 +633,6 @@ struct ASTContext::Implementation {
632633
llvm::DenseMap<BuiltinIntegerWidth, BuiltinIntegerType*> BuiltinIntegerTypes;
633634
llvm::DenseMap<unsigned, BuiltinUnboundGenericType*> BuiltinUnboundGenericTypes;
634635
llvm::FoldingSet<BuiltinVectorType> BuiltinVectorTypes;
635-
llvm::FoldingSet<BuiltinFixedArrayType> BuiltinFixedArrayTypes;
636636
llvm::FoldingSet<DeclName::CompoundDeclName> CompoundNames;
637637
llvm::DenseMap<UUID, GenericEnvironment *> OpenedElementEnvironments;
638638
llvm::FoldingSet<IndexSubset> IndexSubsets;
@@ -3737,20 +3737,26 @@ BuiltinUnboundGenericType::get(TypeKind genericTypeKind,
37373737

37383738
BuiltinFixedArrayType *BuiltinFixedArrayType::get(CanType Size,
37393739
CanType ElementType) {
3740+
RecursiveTypeProperties properties;
3741+
properties |= Size->getRecursiveProperties();
3742+
properties |= ElementType->getRecursiveProperties();
3743+
3744+
AllocationArena arena = getArena(properties);
3745+
37403746
llvm::FoldingSetNodeID id;
37413747
BuiltinFixedArrayType::Profile(id, Size, ElementType);
3742-
auto &context = Size->getASTContext();
3748+
auto &ctx = Size->getASTContext();
37433749

37443750
void *insertPos;
3745-
if (BuiltinFixedArrayType *vecType
3746-
= context.getImpl().BuiltinFixedArrayTypes
3751+
if (BuiltinFixedArrayType *faTy
3752+
= ctx.getImpl().getArena(arena).BuiltinFixedArrayTypes
37473753
.FindNodeOrInsertPos(id, insertPos))
3748-
return vecType;
3754+
return faTy;
37493755

37503756
BuiltinFixedArrayType *faTy
3751-
= new (context, AllocationArena::Permanent)
3752-
BuiltinFixedArrayType(Size, ElementType);
3753-
context.getImpl().BuiltinFixedArrayTypes.InsertNode(faTy, insertPos);
3757+
= new (ctx, arena) BuiltinFixedArrayType(Size, ElementType, properties);
3758+
ctx.getImpl().getArena(arena).BuiltinFixedArrayTypes
3759+
.InsertNode(faTy, insertPos);
37543760
return faTy;
37553761
}
37563762

lib/AST/ASTPrinter.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6241,9 +6241,20 @@ class TypePrinter : public TypeVisitor<TypePrinter, void, NonRecursivePrintOptio
62416241
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinIntegerType)
62426242
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinFloatType)
62436243
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinUnboundGenericType)
6244-
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinFixedArrayType)
62456244
#undef ASTPRINTER_PRINT_BUILTINTYPE
62466245

6246+
void visitBuiltinFixedArrayType(BuiltinFixedArrayType *T,
6247+
NonRecursivePrintOptions nrOptions) {
6248+
SmallString<32> buffer;
6249+
T->getTypeName(buffer);
6250+
Printer << buffer;
6251+
Printer << "<";
6252+
visit(T->getSize());
6253+
Printer << ", ";
6254+
visit(T->getElementType());
6255+
Printer << ">";
6256+
}
6257+
62476258
void visitSILTokenType(SILTokenType *T,
62486259
NonRecursivePrintOptions nrOptions) {
62496260
Printer << BUILTIN_TYPE_NAME_SILTOKEN;

lib/AST/ASTVerifier.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,7 @@ class Verifier : public ASTWalker {
14161416
abort();
14171417
}
14181418

1419-
checkTrivialSubtype(srcTy, destTy, "MetatypeConversionExpr");
1419+
checkTrivialSubtype(E, srcTy, destTy, "MetatypeConversionExpr");
14201420
verifyCheckedBase(E);
14211421
}
14221422

@@ -1641,7 +1641,7 @@ class Verifier : public ASTWalker {
16411641
abort();
16421642
}
16431643

1644-
checkTrivialSubtype(srcTy, destTy, "DerivedToBaseExpr");
1644+
checkTrivialSubtype(E, srcTy, destTy, "DerivedToBaseExpr");
16451645
verifyCheckedBase(E);
16461646
}
16471647

@@ -3590,12 +3590,14 @@ class Verifier : public ASTWalker {
35903590
abort();
35913591
}
35923592

3593-
void checkTrivialSubtype(Type srcTy, Type destTy, const char *what) {
3593+
void checkTrivialSubtype(Expr *E, Type srcTy, Type destTy,
3594+
const char *what) {
35943595
if (srcTy->isEqual(destTy)) return;
35953596

35963597
if (auto srcMetatype = srcTy->getAs<AnyMetatypeType>()) {
35973598
if (auto destMetatype = destTy->getAs<AnyMetatypeType>()) {
3598-
return checkTrivialSubtype(srcMetatype->getInstanceType(),
3599+
return checkTrivialSubtype(E,
3600+
srcMetatype->getInstanceType(),
35993601
destMetatype->getInstanceType(),
36003602
what);
36013603
}
@@ -3625,6 +3627,7 @@ class Verifier : public ASTWalker {
36253627
Out << " to ";
36263628
destTy.print(Out);
36273629
Out << "\n";
3630+
E->dump(Out);
36283631
abort();
36293632
}
36303633

lib/AST/Builtins.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3593,16 +3593,9 @@ StringRef BuiltinType::getTypeName(SmallVectorImpl<char> &result,
35933593
}
35943594
break;
35953595
}
3596-
case BuiltinTypeKind::BuiltinFixedArray: {
3597-
auto bfa = cast<BuiltinFixedArrayType>(this);
3598-
printer << MAYBE_GET_NAMESPACED_BUILTIN(BUILTIN_TYPE_NAME_FIXEDARRAY)
3599-
<< '<';
3600-
bfa->getSize()->print(printer);
3601-
printer << ", ";
3602-
bfa->getElementType()->print(printer);
3603-
printer << '>';
3596+
case BuiltinTypeKind::BuiltinFixedArray:
3597+
printer << MAYBE_GET_NAMESPACED_BUILTIN(BUILTIN_TYPE_NAME_FIXEDARRAY);
36043598
break;
3605-
}
36063599
case BuiltinTypeKind::BuiltinUnboundGeneric: {
36073600
auto bug = cast<BuiltinUnboundGenericType>(this);
36083601
printer << MAYBE_GET_NAMESPACED_BUILTIN(bug->getBuiltinTypeName());

lib/Sema/CSSimplify.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7402,6 +7402,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
74027402

74037403
// Note: Mismatched builtin types fall through to the TypeKind::Error
74047404
// case below.
7405+
#define BUILTIN_GENERIC_TYPE(id, parent)
74057406
#define BUILTIN_TYPE(id, parent) case TypeKind::id:
74067407
#define TYPE(id, parent)
74077408
#include "swift/AST/TypeNodes.def"
@@ -7410,6 +7411,23 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
74107411
case TypeKind::Unresolved:
74117412
return getTypeMatchFailure(locator);
74127413

7414+
case TypeKind::BuiltinFixedArray: {
7415+
auto *fixed1 = cast<BuiltinFixedArrayType>(desugar1);
7416+
auto *fixed2 = cast<BuiltinFixedArrayType>(desugar2);
7417+
7418+
auto result = matchTypes(fixed1->getSize(), fixed2->getSize(),
7419+
ConstraintKind::Bind, subflags,
7420+
locator.withPathElement(
7421+
LocatorPathElt::GenericArgument(0)));
7422+
if (result.isFailure())
7423+
return result;
7424+
7425+
return matchTypes(fixed1->getElementType(), fixed2->getElementType(),
7426+
ConstraintKind::Bind, subflags,
7427+
locator.withPathElement(
7428+
LocatorPathElt::GenericArgument(0)));
7429+
}
7430+
74137431
case TypeKind::Placeholder: {
74147432
// If it's allowed to attempt fixes, let's delegate
74157433
// decision to `repairFailures`, since depending on

lib/Sema/TypeOfReference.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,14 +1640,12 @@ DeclReferenceType ConstraintSystem::getTypeOfMemberReference(
16401640
memberTy = replaceInferableTypesWithTypeVars(
16411641
memberTy, locator, preparedOverload);
16421642

1643-
// Wrap it in a metatype.
1644-
memberTy = MetatypeType::get(memberTy);
1645-
1646-
// If this is a value generic, undo the wrapping. 'substMemberTypeWithBase'
1647-
// returns the underlying value type of the value generic (e.g. 'Int').
1648-
if (isa<GenericTypeParamDecl>(value) &&
1649-
cast<GenericTypeParamDecl>(value)->isValue()) {
1650-
memberTy = memberTy->castTo<MetatypeType>()->getInstanceType();
1643+
// Wrap it in a metatype, unless this is a value generic.
1644+
// 'substMemberTypeWithBase' returns the underlying value type
1645+
// of the value generic (e.g. 'Int').
1646+
if (!isa<GenericTypeParamDecl>(value) ||
1647+
!cast<GenericTypeParamDecl>(value)->isValue()) {
1648+
memberTy = MetatypeType::get(memberTy);
16511649
}
16521650

16531651
auto openedType = FunctionType::get({baseObjParam}, memberTy);

0 commit comments

Comments
 (0)