Skip to content

Commit aed25d1

Browse files
authored
Merge pull request #26802 from DougGregor/abstract-generic-signature-request
2 parents b111ea4 + 7883dc2 commit aed25d1

Some content is hidden

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

44 files changed

+640
-431
lines changed

include/swift/AST/ASTTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ SWIFT_TYPEID_NAMED(Optional<PropertyWrapperMutability>, PropertyWrapperMutabilit
2929
SWIFT_TYPEID_NAMED(CustomAttr *, CustomAttr)
3030
SWIFT_TYPEID_NAMED(TypeAliasDecl *, TypeAliasDecl)
3131
SWIFT_TYPEID(AncestryFlags)
32+
SWIFT_TYPEID_NAMED(GenericSignature *, GenericSignature)
33+
SWIFT_TYPEID_NAMED(GenericTypeParamType *, GenericTypeParamType)
34+
SWIFT_TYPEID(Requirement)

include/swift/AST/ASTTypeIDs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121
namespace swift {
2222

2323
class CustomAttr;
24+
class GenericSignature;
25+
class GenericTypeParamType;
2426
class NominalTypeDecl;
2527
struct PropertyWrapperBackingPropertyInfo;
2628
struct PropertyWrapperTypeInfo;
2729
enum class CtorInitializerKind;
2830
struct PropertyWrapperMutability;
31+
class Requirement;
2932
class Type;
3033
class VarDecl;
3134
class TypeAliasDecl;

include/swift/AST/Attr.h

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,9 @@ class DeclAttribute : public AttributeBase {
258258
ownership : NumReferenceOwnershipBits
259259
);
260260

261-
SWIFT_INLINE_BITFIELD_FULL(SpecializeAttr, DeclAttribute, 1+1+32,
261+
SWIFT_INLINE_BITFIELD(SpecializeAttr, DeclAttribute, 1+1,
262262
exported : 1,
263-
kind : 1,
264-
: NumPadBits,
265-
numRequirements : 32
263+
kind : 1
266264
);
267265

268266
SWIFT_INLINE_BITFIELD(SynthesizedProtocolAttr, DeclAttribute,
@@ -1236,39 +1234,29 @@ class SpecializeAttr : public DeclAttribute {
12361234

12371235
private:
12381236
TrailingWhereClause *trailingWhereClause;
1239-
1240-
Requirement *getRequirementsData() {
1241-
return reinterpret_cast<Requirement *>(this+1);
1242-
}
1237+
GenericSignature *specializedSignature;
12431238

12441239
SpecializeAttr(SourceLoc atLoc, SourceRange Range,
12451240
TrailingWhereClause *clause, bool exported,
1246-
SpecializationKind kind);
1247-
1248-
SpecializeAttr(SourceLoc atLoc, SourceRange Range,
1249-
ArrayRef<Requirement> requirements,
1250-
bool exported,
1251-
SpecializationKind kind);
1241+
SpecializationKind kind,
1242+
GenericSignature *specializedSignature);
12521243

12531244
public:
12541245
static SpecializeAttr *create(ASTContext &Ctx, SourceLoc atLoc,
12551246
SourceRange Range, TrailingWhereClause *clause,
1256-
bool exported, SpecializationKind kind);
1257-
1258-
static SpecializeAttr *create(ASTContext &Ctx, SourceLoc atLoc,
1259-
SourceRange Range,
1260-
ArrayRef<Requirement> requirement,
1261-
bool exported, SpecializationKind kind);
1247+
bool exported, SpecializationKind kind,
1248+
GenericSignature *specializedSignature
1249+
= nullptr);
12621250

12631251
TrailingWhereClause *getTrailingWhereClause() const;
12641252

1265-
ArrayRef<Requirement> getRequirements() const;
1266-
1267-
MutableArrayRef<Requirement> getRequirements() {
1268-
return { getRequirementsData(), Bits.SpecializeAttr.numRequirements };
1253+
GenericSignature *getSpecializedSgnature() const {
1254+
return specializedSignature;
12691255
}
12701256

1271-
void setRequirements(ASTContext &Ctx, ArrayRef<Requirement> requirements);
1257+
void setSpecializedSignature(GenericSignature *newSig) {
1258+
specializedSignature = newSig;
1259+
}
12721260

12731261
bool isExported() const {
12741262
return Bits.SpecializeAttr.exported;

include/swift/AST/GenericSignature.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ CanGenericSignature::CanGenericSignature(GenericSignature *Signature)
335335
assert(!Signature || Signature->isCanonical());
336336
}
337337

338+
void simple_display(raw_ostream &out, const GenericSignature *sig);
339+
338340
} // end namespace swift
339341

340342
#endif // SWIFT_AST_GENERIC_SIGNATURE_H

include/swift/AST/LayoutConstraint.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/Basic/SourceLoc.h"
2222
#include "llvm/ADT/DenseMap.h"
2323
#include "llvm/ADT/FoldingSet.h"
24+
#include "llvm/ADT/Hashing.h"
2425
#include "llvm/ADT/StringRef.h"
2526
#include "swift/AST/PrintOptions.h"
2627

@@ -290,6 +291,10 @@ class LayoutConstraint {
290291
/// Return the layout constraint as a string, for use in diagnostics only.
291292
std::string getString(const PrintOptions &PO = PrintOptions()) const;
292293

294+
friend llvm::hash_code hash_value(const LayoutConstraint &layout) {
295+
return hash_value(layout.getPointer());
296+
}
297+
293298
bool operator==(LayoutConstraint rhs) const {
294299
if (isNull() && rhs.isNull())
295300
return true;

include/swift/AST/Requirement.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define SWIFT_AST_REQUIREMENT_H
1919

2020
#include "swift/AST/Type.h"
21+
#include "llvm/ADT/Hashing.h"
2122
#include "llvm/ADT/PointerIntPair.h"
2223
#include "llvm/Support/ErrorHandling.h"
2324

@@ -119,12 +120,60 @@ class Requirement {
119120
return SecondLayout;
120121
}
121122

123+
/// Whether this requirement is in its canonical form.
124+
bool isCanonical() const;
125+
126+
/// Get the canonical form of this requirement.
127+
Requirement getCanonical() const;
128+
122129
void dump() const;
123130
void dump(raw_ostream &out) const;
124131
void print(raw_ostream &os, const PrintOptions &opts) const;
125132
void print(ASTPrinter &printer, const PrintOptions &opts) const;
133+
134+
friend llvm::hash_code hash_value(const Requirement &requirement) {
135+
using llvm::hash_value;
136+
137+
llvm::hash_code first =
138+
hash_value(requirement.FirstTypeAndKind.getOpaqueValue());
139+
llvm::hash_code second;
140+
switch (requirement.getKind()) {
141+
case RequirementKind::Conformance:
142+
case RequirementKind::Superclass:
143+
case RequirementKind::SameType:
144+
second = hash_value(requirement.getSecondType());
145+
break;
146+
147+
case RequirementKind::Layout:
148+
second = hash_value(requirement.getLayoutConstraint());
149+
break;
150+
}
151+
152+
return llvm::hash_combine(first, second);
153+
}
154+
155+
friend bool operator==(const Requirement &lhs, const Requirement &rhs) {
156+
if (lhs.FirstTypeAndKind.getOpaqueValue()
157+
!= rhs.FirstTypeAndKind.getOpaqueValue())
158+
return false;
159+
160+
switch (lhs.getKind()) {
161+
case RequirementKind::Conformance:
162+
case RequirementKind::Superclass:
163+
case RequirementKind::SameType:
164+
return lhs.getSecondType().getPointer() ==
165+
rhs.getSecondType().getPointer();
166+
167+
case RequirementKind::Layout:
168+
return lhs.getLayoutConstraint() == rhs.getLayoutConstraint();
169+
}
170+
}
126171
};
127172

173+
inline void simple_display(llvm::raw_ostream &out, const Requirement &req) {
174+
req.print(out, PrintOptions());
175+
}
176+
128177
} // end namespace swift
129178

130179
#endif

include/swift/AST/SimpleRequest.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,11 @@ class SimpleRequest<Derived, Output(Inputs...), Caching> {
261261
};
262262
}
263263

264+
namespace llvm {
265+
template <typename T, unsigned N>
266+
llvm::hash_code hash_value(const SmallVector<T, N> &vec) {
267+
return hash_combine_range(vec.begin(), vec.end());
268+
}
269+
}
270+
264271
#endif // SWIFT_BASIC_SIMPLEREQUEST_H

include/swift/AST/Type.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "llvm/ADT/DenseMap.h"
2121
#include "llvm/ADT/DenseMapInfo.h"
22+
#include "llvm/ADT/Hashing.h"
2223
#include "llvm/ADT/STLExtras.h"
2324
#include "swift/Basic/LLVM.h"
2425
#include "swift/Basic/ArrayRefView.h"
@@ -323,6 +324,11 @@ class Type {
323324
/// Return the name of the type as a string, for use in diagnostics only.
324325
std::string getString(const PrintOptions &PO = PrintOptions()) const;
325326

327+
friend llvm::hash_code hash_value(Type type) {
328+
using llvm::hash_value;
329+
return hash_value(type.getPointer());
330+
}
331+
326332
/// Return the name of the type, adding parens in cases where
327333
/// appending or prepending text to the result would cause that text
328334
/// to be appended to only a portion of the returned type. For

include/swift/AST/TypeCheckRequests.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
namespace swift {
3131

3232
class AbstractStorageDecl;
33+
class AccessorDecl;
34+
enum class AccessorKind;
3335
class GenericParamList;
3436
struct PropertyWrapperBackingPropertyInfo;
3537
struct PropertyWrapperMutability;
@@ -38,8 +40,8 @@ class SpecializeAttr;
3840
class TypeAliasDecl;
3941
struct TypeLoc;
4042
class ValueDecl;
41-
class AbstractStorageDecl;
4243
enum class OpaqueReadOwnership: uint8_t;
44+
class StorageImplInfo;
4345

4446
/// Display a nominal type or extension thereof.
4547
void simple_display(
@@ -1073,6 +1075,35 @@ class ClassAncestryFlagsRequest :
10731075

10741076
void simple_display(llvm::raw_ostream &out, AncestryFlags value);
10751077

1078+
class AbstractGenericSignatureRequest :
1079+
public SimpleRequest<AbstractGenericSignatureRequest,
1080+
GenericSignature *(GenericSignature *,
1081+
SmallVector<GenericTypeParamType *, 2>,
1082+
SmallVector<Requirement, 2>),
1083+
CacheKind::Cached> {
1084+
public:
1085+
using SimpleRequest::SimpleRequest;
1086+
1087+
private:
1088+
friend SimpleRequest;
1089+
1090+
// Evaluation.
1091+
llvm::Expected<GenericSignature *>
1092+
evaluate(Evaluator &evaluator,
1093+
GenericSignature *baseSignature,
1094+
SmallVector<GenericTypeParamType *, 2> addedParameters,
1095+
SmallVector<Requirement, 2> addedRequirements) const;
1096+
1097+
public:
1098+
// Separate caching.
1099+
bool isCached() const;
1100+
1101+
/// Abstract generic signature requests never have source-location info.
1102+
SourceLoc getNearestLoc() const {
1103+
return SourceLoc();
1104+
}
1105+
};
1106+
10761107
// Allow AnyValue to compare two Type values, even though Type doesn't
10771108
// support ==.
10781109
template<>

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ SWIFT_TYPEID(SynthesizeAccessorRequest)
5757
SWIFT_TYPEID(EmittedMembersRequest)
5858
SWIFT_TYPEID(IsImplicitlyUnwrappedOptionalRequest)
5959
SWIFT_TYPEID(ClassAncestryFlagsRequest)
60+
SWIFT_TYPEID(AbstractGenericSignatureRequest)

include/swift/Basic/SimpleDisplay.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ namespace swift {
121121
}
122122
out << "}";
123123
}
124+
125+
template<typename T>
126+
void simple_display(llvm::raw_ostream &out,
127+
const llvm::SmallVectorImpl<T> &vec) {
128+
out << "{";
129+
bool first = true;
130+
for (const T &value : vec) {
131+
if (first) first = false;
132+
else out << ", ";
133+
134+
simple_display(out, value);
135+
}
136+
out << "}";
137+
}
124138
}
125139

126140
#endif // SWIFT_BASIC_SIMPLE_DISPLAY_H

include/swift/SIL/SILFunction.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,9 @@ class SILSpecializeAttr final {
7070
};
7171

7272
static SILSpecializeAttr *create(SILModule &M,
73-
ArrayRef<Requirement> requirements,
73+
GenericSignature *specializedSignature,
7474
bool exported, SpecializationKind kind);
7575

76-
ArrayRef<Requirement> getRequirements() const;
77-
7876
bool isExported() const {
7977
return exported;
8078
}
@@ -91,24 +89,24 @@ class SILSpecializeAttr final {
9189
return kind;
9290
}
9391

92+
GenericSignature *getSpecializedSignature() const {
93+
return specializedSignature;
94+
}
95+
9496
SILFunction *getFunction() const {
9597
return F;
9698
}
9799

98100
void print(llvm::raw_ostream &OS) const;
99101

100102
private:
101-
unsigned numRequirements;
102103
SpecializationKind kind;
103104
bool exported;
104-
SILFunction *F;
105+
GenericSignature *specializedSignature;
106+
SILFunction *F = nullptr;
105107

106-
SILSpecializeAttr(ArrayRef<Requirement> requirements, bool exported,
107-
SpecializationKind kind);
108-
109-
Requirement *getRequirementsData() {
110-
return reinterpret_cast<Requirement *>(this+1);
111-
}
108+
SILSpecializeAttr(bool exported, SpecializationKind kind,
109+
GenericSignature *specializedSignature);
112110
};
113111

114112
/// SILFunction - A function body that has been lowered to SIL. This consists of

include/swift/SILOptimizer/Utils/Generics.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,8 @@ class ReabstractionInfo {
147147
OptRemark::Emitter *ORE = nullptr);
148148

149149
/// Constructs the ReabstractionInfo for generic function \p Callee with
150-
/// additional requirements. Requirements may contain new layout,
151-
/// conformances or same concrete type requirements.
152-
ReabstractionInfo(SILFunction *Callee, ArrayRef<Requirement> Requirements);
150+
/// a specialization signature.
151+
ReabstractionInfo(SILFunction *Callee, GenericSignature *SpecializedSig);
153152

154153
IsSerialized_t isSerialized() const {
155154
return Serialized;

include/swift/Serialization/ModuleFormat.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5252
/// describe what change you made. The content of this comment isn't important;
5353
/// it just ensures a conflict if two people change the module format.
5454
/// Don't worry about adhering to the 80-column limit for this line.
55-
const uint16_t SWIFTMODULE_VERSION_MINOR = 513; // Added copy_unmanaged_value.
55+
const uint16_t SWIFTMODULE_VERSION_MINOR = 514; // specialize attr
5656

5757
using DeclIDField = BCFixed<31>;
5858

@@ -1642,7 +1642,8 @@ namespace decls_block {
16421642
using SpecializeDeclAttrLayout = BCRecordLayout<
16431643
Specialize_DECL_ATTR,
16441644
BCFixed<1>, // exported flag
1645-
BCFixed<1> // specialization kind
1645+
BCFixed<1>, // specialization kind
1646+
GenericSignatureIDField // specialized signature
16461647
>;
16471648

16481649
#define SIMPLE_DECL_ATTR(X, CLASS, ...) \

0 commit comments

Comments
 (0)