Skip to content

Commit 71fee06

Browse files
committed
Requestify GTPD::getValueType
1 parent b9b6bb7 commit 71fee06

File tree

11 files changed

+113
-123
lines changed

11 files changed

+113
-123
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,9 +1886,6 @@ ERROR(expected_rangle_primary_associated_type_list,PointsToFirstBadToken,
18861886
ERROR(expected_primary_associated_type_name,PointsToFirstBadToken,
18871887
"expected an identifier to name primary associated type", ())
18881888

1889-
ERROR(expected_value_generic_type,none,
1890-
"expected type for value generic type", ())
1891-
18921889
//------------------------------------------------------------------------------
18931890
// MARK: Conditional compilation parsing diagnostics
18941891
//------------------------------------------------------------------------------

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8022,6 +8022,8 @@ ERROR(integer_type_not_accepted,none,
80228022
"integer unexpectedly used in a type position", ())
80238023
ERROR(value_generic_unexpected,none,
80248024
"using value generic %0 here is not allowed", (Type))
8025+
ERROR(missing_value_generic_type,none,
8026+
"value generic 'let %0' must have an explicit value type declared", (Identifier))
80258027

80268028
#define UNDEFINE_DIAGNOSTIC_MACROS
80278029
#include "DefineDiagnosticMacros.h"

include/swift/AST/TypeCheckRequests.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5085,6 +5085,22 @@ class IsUnsafeRequest
50855085
void cacheResult(bool value) const;
50865086
};
50875087

5088+
class GenericTypeParamDeclGetValueTypeRequest
5089+
: public SimpleRequest<GenericTypeParamDeclGetValueTypeRequest,
5090+
Type(GenericTypeParamDecl *decl),
5091+
RequestFlags::Cached> {
5092+
public:
5093+
using SimpleRequest::SimpleRequest;
5094+
5095+
private:
5096+
friend SimpleRequest;
5097+
5098+
Type evaluate(Evaluator &evaluator, GenericTypeParamDecl *decl) const;
5099+
5100+
public:
5101+
bool isCached() const { return true; }
5102+
};
5103+
50885104
#define SWIFT_TYPEID_ZONE TypeChecker
50895105
#define SWIFT_TYPEID_HEADER "swift/AST/TypeCheckerTypeIDZone.def"
50905106
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,3 +596,6 @@ SWIFT_REQUEST(TypeChecker, ParamCaptureInfoRequest,
596596
SWIFT_REQUEST(TypeChecker, IsUnsafeRequest,
597597
bool(Decl *),
598598
SeparatelyCached, NoLocationInfo)
599+
600+
SWIFT_REQUEST(TypeChecker, GenericTypeParamDeclGetValueTypeRequest,
601+
Type(GenericTypeParamDecl *), Cached, NoLocationInfo)

include/swift/AST/Types.h

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,9 @@ class alignas(1 << TypeAlignInBits) TypeBase
437437
NumParams : 16
438438
);
439439

440-
SWIFT_INLINE_BITFIELD_FULL(ArchetypeType, TypeBase, 1+1+1+16,
440+
SWIFT_INLINE_BITFIELD_FULL(ArchetypeType, TypeBase, 1+1+16,
441441
HasSuperclass : 1,
442442
HasLayoutConstraint : 1,
443-
HasValue : 1,
444443
: NumPadBits,
445444
NumProtocols : 16
446445
);
@@ -6536,17 +6535,7 @@ class ArchetypeType : public SubstitutableType,
65366535
}
65376536

65386537
size_t numTrailingObjects(OverloadToken<Type>) const {
6539-
auto numTypes = 0;
6540-
6541-
if (Bits.ArchetypeType.HasSuperclass) {
6542-
numTypes += 1;
6543-
}
6544-
6545-
if (Bits.ArchetypeType.HasValue) {
6546-
numTypes += 1;
6547-
}
6548-
6549-
return numTypes;
6538+
return Bits.ArchetypeType.HasSuperclass ? 1 : 0;
65506539
}
65516540

65526541
size_t numTrailingObjects(OverloadToken<LayoutConstraint>) const {
@@ -6594,7 +6583,7 @@ class ArchetypeType : public SubstitutableType,
65946583
Type getSuperclass() const {
65956584
if (!Bits.ArchetypeType.HasSuperclass) return Type();
65966585

6597-
return getSubclassTrailingObjects<Type>()[0];
6586+
return *getSubclassTrailingObjects<Type>();
65986587
}
65996588

66006589
/// Retrieve the layout constraint of this type, if such a requirement exists.
@@ -6606,15 +6595,7 @@ class ArchetypeType : public SubstitutableType,
66066595

66076596
/// Retrieve the value type of this generic parameter, if such a requirement
66086597
/// exists.
6609-
Type getValueType() const {
6610-
if (!Bits.ArchetypeType.HasValue) return Type();
6611-
6612-
if (Bits.ArchetypeType.HasSuperclass) {
6613-
return getSubclassTrailingObjects<Type>()[1];
6614-
}
6615-
6616-
return getSubclassTrailingObjects<Type>()[0];
6617-
}
6598+
Type getValueType() const;
66186599

66196600
/// Retrieve the nested type with the given associated type.
66206601
Type getNestedType(AssociatedTypeDecl *assocType);
@@ -6651,7 +6632,7 @@ class ArchetypeType : public SubstitutableType,
66516632
Type InterfaceType,
66526633
ArrayRef<ProtocolDecl *> ConformsTo,
66536634
Type Superclass, LayoutConstraint Layout,
6654-
Type ValueType, GenericEnvironment *Environment);
6635+
GenericEnvironment *Environment);
66556636
};
66566637
BEGIN_CAN_TYPE_WRAPPER(ArchetypeType, SubstitutableType)
66576638
END_CAN_TYPE_WRAPPER(ArchetypeType, SubstitutableType)
@@ -6674,8 +6655,7 @@ class PrimaryArchetypeType final : public ArchetypeType,
66746655
GenericEnvironment *GenericEnv,
66756656
Type InterfaceType,
66766657
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
6677-
Type Superclass, LayoutConstraint Layout,
6678-
Type ValueType);
6658+
Type Superclass, LayoutConstraint Layout);
66796659

66806660
static bool classof(const TypeBase *T) {
66816661
return T->getKind() == TypeKind::PrimaryArchetype;
@@ -6686,7 +6666,6 @@ class PrimaryArchetypeType final : public ArchetypeType,
66866666
Type InterfaceType,
66876667
ArrayRef<ProtocolDecl *> ConformsTo,
66886668
Type Superclass, LayoutConstraint Layout,
6689-
Type ValueType,
66906669
RecursiveTypeProperties Properties);
66916670
};
66926671
BEGIN_CAN_TYPE_WRAPPER(PrimaryArchetypeType, ArchetypeType)
@@ -6705,7 +6684,7 @@ class OpaqueTypeArchetypeType final : public ArchetypeType,
67056684
static OpaqueTypeArchetypeType *getNew(
67066685
GenericEnvironment *environment, Type interfaceType,
67076686
ArrayRef<ProtocolDecl*> conformsTo, Type superclass,
6708-
LayoutConstraint layout, Type valueType);
6687+
LayoutConstraint layout);
67096688

67106689
public:
67116690
/// Get an opaque archetype representing the underlying type of the given
@@ -6731,8 +6710,7 @@ class OpaqueTypeArchetypeType final : public ArchetypeType,
67316710
RecursiveTypeProperties properties,
67326711
Type interfaceType,
67336712
ArrayRef<ProtocolDecl*> conformsTo,
6734-
Type superclass, LayoutConstraint layout,
6735-
Type valueType);
6713+
Type superclass, LayoutConstraint layout);
67366714
};
67376715
BEGIN_CAN_TYPE_WRAPPER(OpaqueTypeArchetypeType, ArchetypeType)
67386716
END_CAN_TYPE_WRAPPER(OpaqueTypeArchetypeType, ArchetypeType)
@@ -6834,7 +6812,7 @@ class OpenedArchetypeType final : public LocalArchetypeType,
68346812
static CanTypeWrapper<OpenedArchetypeType>
68356813
getNew(GenericEnvironment *environment, Type interfaceType,
68366814
ArrayRef<ProtocolDecl *> conformsTo, Type superclass,
6837-
LayoutConstraint layout, Type valueType);
6815+
LayoutConstraint layout);
68386816

68396817
public:
68406818
/// Get or create an archetype that represents the opened type
@@ -6865,7 +6843,6 @@ class OpenedArchetypeType final : public LocalArchetypeType,
68656843
ArrayRef<ProtocolDecl *> conformsTo,
68666844
Type superclass,
68676845
LayoutConstraint layout,
6868-
Type valueType,
68696846
RecursiveTypeProperties properties);
68706847
};
68716848
BEGIN_CAN_TYPE_WRAPPER(OpenedArchetypeType, LocalArchetypeType)
@@ -7095,8 +7072,12 @@ class GenericTypeParamType : public SubstitutableType,
70957072
Type getValueType() const;
70967073

70977074
void Profile(llvm::FoldingSetNodeID &ID) {
7075+
// Note: We explicitly don't use 'getName()' because for canonical forms
7076+
// which don't store an identifier we'll go create a tau based form. We
7077+
// really want to just plumb down the null Identifier because that's what's
7078+
// inside the cache.
70987079
Profile(ID, getParamKind(), getDepth(), getIndex(), getValueType(),
7099-
getName());
7080+
Name);
71007081
}
71017082
static void Profile(llvm::FoldingSetNodeID &ID,
71027083
GenericTypeParamKind paramKind, unsigned depth,

lib/AST/ASTContext.cpp

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4772,8 +4772,11 @@ GenericTypeParamType *GenericTypeParamType::get(Identifier name,
47724772
if (paramKind == GenericTypeParamKind::Pack)
47734773
props |= RecursiveTypeProperties::HasParameterPack;
47744774

4775+
auto canType = GenericTypeParamType::get(paramKind, depth, index, valueType,
4776+
ctx);
4777+
47754778
auto result = new (ctx, AllocationArena::Permanent)
4776-
GenericTypeParamType(paramKind, depth, index, valueType, props, ctx);
4779+
GenericTypeParamType(name, canType, ctx);
47774780
ctx.getImpl().GenericParamTypes.InsertNode(result, insertPos);
47784781
return result;
47794782
}
@@ -4791,8 +4794,22 @@ GenericTypeParamType *GenericTypeParamType::get(GenericTypeParamKind paramKind,
47914794
unsigned depth, unsigned index,
47924795
Type valueType,
47934796
const ASTContext &ctx) {
4794-
return GenericTypeParamType::get(Identifier(), paramKind, depth, index,
4795-
valueType, ctx);
4797+
llvm::FoldingSetNodeID id;
4798+
GenericTypeParamType::Profile(id, paramKind, depth, index, valueType,
4799+
Identifier());
4800+
4801+
void *insertPos;
4802+
if (auto gpTy = ctx.getImpl().GenericParamTypes.FindNodeOrInsertPos(id, insertPos))
4803+
return gpTy;
4804+
4805+
RecursiveTypeProperties props = RecursiveTypeProperties::HasTypeParameter;
4806+
if (paramKind == GenericTypeParamKind::Pack)
4807+
props |= RecursiveTypeProperties::HasParameterPack;
4808+
4809+
auto result = new (ctx, AllocationArena::Permanent)
4810+
GenericTypeParamType(paramKind, depth, index, valueType, props, ctx);
4811+
ctx.getImpl().GenericParamTypes.InsertNode(result, insertPos);
4812+
return result;
47964813
}
47974814

47984815
GenericTypeParamType *GenericTypeParamType::getType(unsigned depth,
@@ -5349,29 +5366,18 @@ static RecursiveTypeProperties getOpaqueTypeArchetypeProperties(
53495366
OpaqueTypeArchetypeType *OpaqueTypeArchetypeType::getNew(
53505367
GenericEnvironment *environment, Type interfaceType,
53515368
ArrayRef<ProtocolDecl *> conformsTo, Type superclass,
5352-
LayoutConstraint layout, Type valueType) {
5369+
LayoutConstraint layout) {
53535370
auto properties = getOpaqueTypeArchetypeProperties(
53545371
environment->getOuterSubstitutions());
53555372
auto arena = getArena(properties);
5356-
5357-
auto numTypes = 0;
5358-
5359-
if (superclass) {
5360-
numTypes += 1;
5361-
}
5362-
5363-
if (valueType) {
5364-
numTypes += 1;
5365-
}
5366-
53675373
auto size = OpaqueTypeArchetypeType::totalSizeToAlloc<
53685374
ProtocolDecl *, Type, LayoutConstraint>(
5369-
conformsTo.size(), numTypes, layout ? 1 : 0);
5375+
conformsTo.size(), superclass ? 1 : 0, layout ? 1 : 0);
53705376
ASTContext &ctx = interfaceType->getASTContext();
53715377
auto mem = ctx.Allocate(size, alignof(OpaqueTypeArchetypeType), arena);
53725378
return ::new (mem)
53735379
OpaqueTypeArchetypeType(environment, properties, interfaceType,
5374-
conformsTo, superclass, layout, valueType);
5380+
conformsTo, superclass, layout);
53755381
}
53765382

53775383
Type OpaqueTypeArchetypeType::get(
@@ -5391,32 +5397,21 @@ static RecursiveTypeProperties getOpenedArchetypeProperties(SubstitutionMap subs
53915397
CanTypeWrapper<OpenedArchetypeType> OpenedArchetypeType::getNew(
53925398
GenericEnvironment *environment, Type interfaceType,
53935399
ArrayRef<ProtocolDecl *> conformsTo, Type superclass,
5394-
LayoutConstraint layout, Type valueType) {
5400+
LayoutConstraint layout) {
53955401
auto properties = getOpenedArchetypeProperties(
53965402
environment->getOuterSubstitutions());
53975403
auto arena = getArena(properties);
5398-
5399-
auto numTypes = 0;
5400-
5401-
if (superclass) {
5402-
numTypes += 1;
5403-
}
5404-
5405-
if (valueType) {
5406-
numTypes += 1;
5407-
}
5408-
54095404
auto size = OpenedArchetypeType::totalSizeToAlloc<
54105405
ProtocolDecl *, Type, LayoutConstraint>(
54115406
conformsTo.size(),
5412-
numTypes,
5407+
superclass ? 1 : 0,
54135408
layout ? 1 : 0);
54145409

54155410
ASTContext &ctx = interfaceType->getASTContext();
54165411
void *mem = ctx.Allocate(size, alignof(OpenedArchetypeType), arena);
54175412

54185413
return CanOpenedArchetypeType(::new (mem) OpenedArchetypeType(
5419-
environment, interfaceType, conformsTo, superclass, layout, valueType,
5414+
environment, interfaceType, conformsTo, superclass, layout,
54205415
properties));
54215416
}
54225417

lib/AST/Decl.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5730,11 +5730,9 @@ GenericTypeParamDecl *GenericTypeParamDecl::createImplicit(
57305730
}
57315731

57325732
Type GenericTypeParamDecl::getValueType() const {
5733-
if (!isValue())
5734-
return Type();
5735-
5736-
// GenericTypeParamDecls should only have 1 inherited entry, so get that.
5737-
return getInherited().getResolvedType(0, TypeResolutionStage::Structural);
5733+
return evaluateOrDefault(getASTContext().evaluator,
5734+
GenericTypeParamDeclGetValueTypeRequest{const_cast<GenericTypeParamDecl *>(this)},
5735+
Type());
57385736
}
57395737

57405738
SourceRange GenericTypeParamDecl::getSourceRange() const {

lib/AST/GenericEnvironment.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,7 @@ GenericEnvironment::getOrCreateArchetypeFromInterfaceType(Type depType) {
405405
} else {
406406
result = PrimaryArchetypeType::getNew(ctx, this, requirements.anchor,
407407
requirements.protos, superclass,
408-
requirements.layout,
409-
rootGP->getValueType());
408+
requirements.layout);
410409
}
411410

412411
break;
@@ -422,8 +421,7 @@ GenericEnvironment::getOrCreateArchetypeFromInterfaceType(Type depType) {
422421

423422
result = OpaqueTypeArchetypeType::getNew(this, requirements.anchor,
424423
requirements.protos, superclass,
425-
requirements.layout,
426-
rootGP->getValueType());
424+
requirements.layout);
427425
break;
428426
}
429427

@@ -448,13 +446,11 @@ GenericEnvironment::getOrCreateArchetypeFromInterfaceType(Type depType) {
448446
protos.push_back(proto);
449447

450448
result = OpenedArchetypeType::getNew(this, requirements.anchor, protos,
451-
superclass, requirements.layout,
452-
rootGP->getValueType());
449+
superclass, requirements.layout);
453450
} else {
454451
result = OpenedArchetypeType::getNew(this, requirements.anchor,
455452
requirements.protos, superclass,
456-
requirements.layout,
457-
rootGP->getValueType());
453+
requirements.layout);
458454
}
459455

460456
break;

0 commit comments

Comments
 (0)