Skip to content

Commit 28c8c6e

Browse files
committed
[NFC] GenericRequirement vends an llvm::Type *.
The mapping from requirement kind to llvm::Type is repeated several places. Pull it into a helper.
1 parent ea107ae commit 28c8c6e

File tree

4 files changed

+33
-32
lines changed

4 files changed

+33
-32
lines changed

include/swift/IRGen/GenericRequirement.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@
1717
#include "swift/AST/Type.h"
1818
#include "llvm/Support/raw_ostream.h"
1919

20+
namespace llvm {
21+
class Type;
22+
}
23+
2024
namespace swift {
2125

2226
class ProtocolDecl;
27+
namespace irgen {
28+
class IRGenModule;
29+
}
2330

2431
/// The three kinds of entities passed in the runtime calling convention for
2532
/// generic code: pack shapes, type metadata, and witness tables.
@@ -87,6 +94,13 @@ class GenericRequirement {
8794
return GenericRequirement(Kind::WitnessTable, type, proto);
8895
}
8996

97+
static llvm::Type *typeForKind(irgen::IRGenModule &IGM,
98+
GenericRequirement::Kind kind);
99+
100+
llvm::Type *getType(irgen::IRGenModule &IGM) const {
101+
return typeForKind(IGM, getKind());
102+
}
103+
90104
void dump(llvm::raw_ostream &out) const {
91105
switch (kind) {
92106
case Kind::Shape:

lib/IRGen/GenMeta.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3898,13 +3898,11 @@ namespace {
38983898
ClassDecl *forClass) {
38993899
switch (requirement.getKind()) {
39003900
case GenericRequirement::Kind::Shape:
3901-
B.addInt(IGM.SizeTy, 0);
3901+
B.addInt(cast<llvm::IntegerType>(requirement.getType(IGM)), 0);
39023902
break;
39033903
case GenericRequirement::Kind::Metadata:
3904-
B.addNullPointer(IGM.TypeMetadataPtrTy);
3905-
break;
39063904
case GenericRequirement::Kind::WitnessTable:
3907-
B.addNullPointer(IGM.WitnessTablePtrTy);
3905+
B.addNullPointer(cast<llvm::PointerType>(requirement.getType(IGM)));
39083906
break;
39093907
}
39103908
}

lib/IRGen/GenProto.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3600,24 +3600,27 @@ void irgen::bindFromGenericRequirementsBuffer(IRGenFunction &IGF,
36003600
}
36013601

36023602
// Cast if necessary.
3603-
switch (requirements[index].getKind()) {
3604-
case GenericRequirement::Kind::Shape:
3605-
slot = IGF.Builder.CreateElementBitCast(slot, IGF.IGM.SizeTy);
3606-
break;
3607-
case GenericRequirement::Kind::Metadata:
3608-
slot = IGF.Builder.CreateElementBitCast(slot, IGF.IGM.TypeMetadataPtrTy);
3609-
break;
3610-
case GenericRequirement::Kind::WitnessTable:
3611-
slot = IGF.Builder.CreateElementBitCast(slot, IGF.IGM.WitnessTablePtrTy);
3612-
break;
3613-
}
3603+
slot = IGF.Builder.CreateElementBitCast(
3604+
slot, requirements[index].getType(IGF.IGM));
36143605

36153606
llvm::Value *value = IGF.Builder.CreateLoad(slot);
36163607
bindGenericRequirement(IGF, requirements[index], value, metadataState,
36173608
getInContext);
36183609
}
36193610
}
36203611

3612+
llvm::Type *GenericRequirement::typeForKind(IRGenModule &IGM,
3613+
GenericRequirement::Kind kind) {
3614+
switch (kind) {
3615+
case GenericRequirement::Kind::Shape:
3616+
return IGM.SizeTy;
3617+
case GenericRequirement::Kind::Metadata:
3618+
return IGM.TypeMetadataPtrTy;
3619+
case GenericRequirement::Kind::WitnessTable:
3620+
return IGM.WitnessTablePtrTy;
3621+
}
3622+
}
3623+
36213624
void irgen::bindGenericRequirement(IRGenFunction &IGF,
36223625
GenericRequirement requirement,
36233626
llvm::Value *value,
@@ -3626,17 +3629,16 @@ void irgen::bindGenericRequirement(IRGenFunction &IGF,
36263629
// Get the corresponding context type.
36273630
auto type = getInContext(requirement.getTypeParameter());
36283631

3632+
assert(value->getType() == requirement.getType(IGF.IGM));
36293633
switch (requirement.getKind()) {
36303634
case GenericRequirement::Kind::Shape: {
36313635
assert(isa<ArchetypeType>(type));
3632-
assert(value->getType() == IGF.IGM.SizeTy);
36333636
auto kind = LocalTypeDataKind::forPackShapeExpression();
36343637
IGF.setUnscopedLocalTypeData(type, kind, value);
36353638
break;
36363639
}
36373640

36383641
case GenericRequirement::Kind::Metadata: {
3639-
assert(value->getType() == IGF.IGM.TypeMetadataPtrTy);
36403642
setTypeMetadataName(IGF.IGM, value, type);
36413643
IGF.bindLocalTypeDataFromTypeMetadata(type, IsExact, value, metadataState);
36423644
break;
@@ -3645,7 +3647,6 @@ void irgen::bindGenericRequirement(IRGenFunction &IGF,
36453647
case GenericRequirement::Kind::WitnessTable: {
36463648
auto proto = requirement.getProtocol();
36473649
assert(isa<ArchetypeType>(type));
3648-
assert(value->getType() == IGF.IGM.WitnessTablePtrTy);
36493650
setProtocolWitnessTableName(IGF.IGM, value, type, proto);
36503651
auto kind = LocalTypeDataKind::forAbstractProtocolWitnessTable(proto);
36513652
IGF.setUnscopedLocalTypeData(type, kind, value);
@@ -3676,17 +3677,15 @@ namespace {
36763677
enumerateUnfulfilledRequirements([&](GenericRequirement reqt) {
36773678
if (reqs)
36783679
reqs->push_back(reqt);
3680+
out.push_back(reqt.getType(IGM));
36793681
switch (reqt.getKind()) {
36803682
case GenericRequirement::Kind::Shape:
3681-
out.push_back(IGM.SizeTy);
36823683
++numShapes;
36833684
break;
36843685
case GenericRequirement::Kind::Metadata:
3685-
out.push_back(IGM.TypeMetadataPtrTy);
36863686
++numTypeMetadataPtrs;
36873687
break;
36883688
case GenericRequirement::Kind::WitnessTable:
3689-
out.push_back(IGM.WitnessTablePtrTy);
36903689
++numWitnessTablePtrs;
36913690
break;
36923691
}

lib/IRGen/GenericArguments.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,7 @@ struct GenericArguments {
5959
void collectTypes(IRGenModule &IGM,
6060
const GenericTypeRequirements &requirements) {
6161
for (auto &requirement : requirements.getRequirements()) {
62-
switch (requirement.getKind()) {
63-
case GenericRequirement::Kind::Shape:
64-
Types.push_back(IGM.SizeTy);
65-
break;
66-
case GenericRequirement::Kind::Metadata:
67-
Types.push_back(IGM.TypeMetadataPtrTy);
68-
break;
69-
case GenericRequirement::Kind::WitnessTable:
70-
Types.push_back(IGM.WitnessTablePtrTy);
71-
break;
72-
}
62+
Types.push_back(requirement.getType(IGM));
7363
}
7464
}
7565

0 commit comments

Comments
 (0)