Skip to content

Commit a5ddd53

Browse files
authored
Merge pull request swiftlang#18812 from slavapestov/easy-function-type-conversions
Port almost all remaining FunctionType::get() usages to new representation
2 parents 93e9b1d + 527ff37 commit a5ddd53

31 files changed

+304
-330
lines changed

include/swift/AST/Types.h

Lines changed: 55 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ namespace swift {
5454
class GenericParamList;
5555
class GenericSignature;
5656
class Identifier;
57+
class InOutType;
5758
enum class ReferenceCounting : uint8_t;
5859
enum class ResilienceExpansion : unsigned;
5960
class SILModule;
@@ -2627,9 +2628,13 @@ class AnyFunctionType : public TypeBase {
26272628

26282629
class Param {
26292630
public:
2630-
explicit Param(const TupleTypeElt &tte);
2631-
explicit Param(Type t, Identifier l, ParameterTypeFlags f);
2632-
2631+
explicit Param(Type t,
2632+
Identifier l = Identifier(),
2633+
ParameterTypeFlags f = ParameterTypeFlags())
2634+
: Ty(t), Label(l), Flags(f) {
2635+
assert(!t || !t->is<InOutType>() && "set flags instead");
2636+
}
2637+
26332638
private:
26342639
/// The type of the parameter. For a variadic parameter, this is the
26352640
/// element type.
@@ -2642,14 +2647,10 @@ class AnyFunctionType : public TypeBase {
26422647
ParameterTypeFlags Flags = {};
26432648

26442649
public:
2645-
Type getType() const;
2646-
CanType getCanType() const {
2647-
assert(getType()->isCanonical());
2648-
return CanType(getType());
2649-
}
2650-
26512650
/// FIXME(Remove InOutType): This is mostly for copying between param
26522651
/// types and should go away.
2652+
Type getType() const;
2653+
26532654
Type getPlainType() const { return Ty; }
26542655

26552656
bool hasLabel() const { return !Label.empty(); }
@@ -2694,6 +2695,7 @@ class AnyFunctionType : public TypeBase {
26942695
static CanParam getFromParam(const Param &param) { return CanParam(param); }
26952696

26962697
CanType getType() const { return CanType(Param::getType()); }
2698+
CanType getPlainType() const { return CanType(Param::getPlainType()); }
26972699
};
26982700

26992701
using CanParamArrayRef =
@@ -2890,7 +2892,7 @@ class AnyFunctionType : public TypeBase {
28902892
protected:
28912893
AnyFunctionType(TypeKind Kind, const ASTContext *CanTypeContext,
28922894
Type Input, Type Output, RecursiveTypeProperties properties,
2893-
unsigned NumParams, const ExtInfo &Info)
2895+
unsigned NumParams, ExtInfo Info)
28942896
: TypeBase(Kind, CanTypeContext, properties), Input(Input), Output(Output) {
28952897
Bits.AnyFunctionType.ExtInfo = Info.Bits;
28962898
Bits.AnyFunctionType.NumParams = NumParams;
@@ -2904,7 +2906,7 @@ class AnyFunctionType : public TypeBase {
29042906
public:
29052907
/// \brief Break an input type into an array of \c AnyFunctionType::Params.
29062908
static void decomposeInput(Type type,
2907-
SmallVectorImpl<AnyFunctionType::Param> &result);
2909+
SmallVectorImpl<Param> &result);
29082910

29092911
/// \brief Take an array of parameters and turn it into an input type.
29102912
///
@@ -2918,15 +2920,14 @@ class AnyFunctionType : public TypeBase {
29182920
}
29192921

29202922
/// \brief Given two arrays of parameters determine if they are equal.
2921-
static bool equalParams(ArrayRef<AnyFunctionType::Param> a,
2922-
ArrayRef<AnyFunctionType::Param> b);
2923+
static bool equalParams(ArrayRef<Param> a, ArrayRef<Param> b);
29232924

29242925
/// \brief Given two arrays of parameters determine if they are equal.
29252926
static bool equalParams(CanParamArrayRef a, CanParamArrayRef b);
29262927

29272928
Type getInput() const { return Input; }
29282929
Type getResult() const { return Output; }
2929-
ArrayRef<AnyFunctionType::Param> getParams() const;
2930+
ArrayRef<Param> getParams() const;
29302931
unsigned getNumParams() const { return Bits.AnyFunctionType.NumParams; }
29312932

29322933
GenericSignature *getOptGenericSignature() const;
@@ -2978,14 +2979,13 @@ BEGIN_CAN_TYPE_WRAPPER(AnyFunctionType, Type)
29782979
using ExtInfo = AnyFunctionType::ExtInfo;
29792980
using CanParamArrayRef = AnyFunctionType::CanParamArrayRef;
29802981

2981-
static CanAnyFunctionType get(CanGenericSignature signature,
2982-
CanType input, CanType result);
2983-
static CanAnyFunctionType get(CanGenericSignature signature,
2984-
CanType input, CanType result,
2985-
const ExtInfo &extInfo);
2982+
static CanAnyFunctionType getOld(CanGenericSignature signature,
2983+
CanType input, CanType result,
2984+
ExtInfo extInfo = ExtInfo());
29862985
static CanAnyFunctionType get(CanGenericSignature signature,
29872986
CanParamArrayRef params,
2988-
CanType result, const ExtInfo &info);
2987+
CanType result,
2988+
ExtInfo info = ExtInfo());
29892989

29902990
CanGenericSignature getOptGenericSignature() const;
29912991

@@ -3018,19 +3018,17 @@ class FunctionType final : public AnyFunctionType,
30183018

30193019
public:
30203020
/// 'Constructor' Factory Function
3021-
static FunctionType *get(Type Input, Type Result) {
3022-
return get(Input, Result, ExtInfo());
3023-
}
3024-
3025-
static FunctionType *get(Type Input, Type Result, const ExtInfo &Info);
3021+
static FunctionType *getOld(Type Input, Type Result,
3022+
ExtInfo Info = ExtInfo());
30263023

3027-
static FunctionType *get(ArrayRef<AnyFunctionType::Param> params,
3028-
Type result, const ExtInfo &info,
3024+
static FunctionType *get(ArrayRef<Param> params,
3025+
Type result,
3026+
ExtInfo info = ExtInfo(),
30293027
bool canonicalVararg = false);
30303028

30313029
// Retrieve the input parameters of this function type.
3032-
ArrayRef<AnyFunctionType::Param> getParams() const {
3033-
return {getTrailingObjects<AnyFunctionType::Param>(), getNumParams()};
3030+
ArrayRef<Param> getParams() const {
3031+
return {getTrailingObjects<Param>(), getNumParams()};
30343032
}
30353033

30363034
// Implement isa/cast/dyncast/etc.
@@ -3039,23 +3037,19 @@ class FunctionType final : public AnyFunctionType,
30393037
}
30403038

30413039
private:
3042-
FunctionType(ArrayRef<AnyFunctionType::Param> params,
3040+
FunctionType(ArrayRef<Param> params,
30433041
Type Input, Type Result,
30443042
RecursiveTypeProperties properties,
3045-
const ExtInfo &Info);
3043+
ExtInfo Info);
30463044
};
30473045
BEGIN_CAN_TYPE_WRAPPER(FunctionType, AnyFunctionType)
3048-
static CanFunctionType get(CanType input, CanType result) {
3049-
auto fnType = FunctionType::get(input, result);
3050-
return cast<FunctionType>(fnType->getCanonicalType());
3051-
}
3052-
static CanFunctionType get(CanType input, CanType result,
3053-
const ExtInfo &info) {
3054-
auto fnType = FunctionType::get(input, result, info);
3046+
static CanFunctionType getOld(CanType input, CanType result,
3047+
ExtInfo info = ExtInfo()) {
3048+
auto fnType = FunctionType::getOld(input, result, info);
30553049
return cast<FunctionType>(fnType->getCanonicalType());
30563050
}
30573051
static CanFunctionType get(CanParamArrayRef params, CanType result,
3058-
const ExtInfo &info) {
3052+
ExtInfo info = ExtInfo()) {
30593053
auto fnType = FunctionType::get(params.getOriginalArray(),
30603054
result, info, /*canonicalVararg=*/true);
30613055
return cast<FunctionType>(fnType->getCanonicalType());
@@ -3100,30 +3094,30 @@ class GenericFunctionType final : public AnyFunctionType,
31003094

31013095
/// Construct a new generic function type.
31023096
GenericFunctionType(GenericSignature *sig,
3103-
ArrayRef<AnyFunctionType::Param> params,
3097+
ArrayRef<Param> params,
31043098
Type input,
31053099
Type result,
3106-
const ExtInfo &info,
3100+
ExtInfo info,
31073101
const ASTContext *ctx,
31083102
RecursiveTypeProperties properties);
31093103

31103104
public:
31113105
/// Create a new generic function type.
3112-
static GenericFunctionType *get(GenericSignature *sig,
3113-
Type input,
3114-
Type result,
3115-
const ExtInfo &info);
3106+
static GenericFunctionType *getOld(GenericSignature *sig,
3107+
Type input,
3108+
Type result,
3109+
ExtInfo info = ExtInfo());
31163110

31173111
/// Create a new generic function type.
31183112
static GenericFunctionType *get(GenericSignature *sig,
31193113
ArrayRef<Param> params,
31203114
Type result,
3121-
const ExtInfo &info,
3115+
ExtInfo info = ExtInfo(),
31223116
bool canonicalVararg = false);
31233117

31243118
// Retrieve the input parameters of this function type.
3125-
ArrayRef<AnyFunctionType::Param> getParams() const {
3126-
return {getTrailingObjects<AnyFunctionType::Param>(), getNumParams()};
3119+
ArrayRef<Param> getParams() const {
3120+
return {getTrailingObjects<Param>(), getNumParams()};
31273121
}
31283122

31293123
/// Retrieve the generic signature of this function type.
@@ -3149,7 +3143,7 @@ class GenericFunctionType final : public AnyFunctionType,
31493143
GenericSignature *sig,
31503144
Type input,
31513145
Type result,
3152-
const ExtInfo &info);
3146+
ExtInfo info);
31533147

31543148
// Implement isa/cast/dyncast/etc.
31553149
static bool classof(const TypeBase *T) {
@@ -3158,19 +3152,20 @@ class GenericFunctionType final : public AnyFunctionType,
31583152
};
31593153

31603154
BEGIN_CAN_TYPE_WRAPPER(GenericFunctionType, AnyFunctionType)
3161-
static CanGenericFunctionType get(CanGenericSignature sig,
3162-
CanType input, CanType result,
3163-
const ExtInfo &info) {
3155+
static CanGenericFunctionType getOld(CanGenericSignature sig,
3156+
CanType input, CanType result,
3157+
ExtInfo info = ExtInfo()) {
31643158
// Knowing that the argument types are independently canonical is
31653159
// not sufficient to guarantee that the function type will be canonical.
3166-
auto fnType = GenericFunctionType::get(sig, input, result, info);
3160+
auto fnType = GenericFunctionType::getOld(sig, input, result, info);
31673161
return cast<GenericFunctionType>(fnType->getCanonicalType());
31683162
}
31693163

31703164
/// Create a new generic function type.
31713165
static CanGenericFunctionType get(CanGenericSignature sig,
3172-
CanParamArrayRef params, CanType result,
3173-
const ExtInfo &info) {
3166+
CanParamArrayRef params,
3167+
CanType result,
3168+
ExtInfo info = ExtInfo()) {
31743169
// Knowing that the argument types are independently canonical is
31753170
// not sufficient to guarantee that the function type will be canonical.
31763171
auto fnType = GenericFunctionType::get(sig, params.getOriginalArray(),
@@ -3194,24 +3189,18 @@ BEGIN_CAN_TYPE_WRAPPER(GenericFunctionType, AnyFunctionType)
31943189
END_CAN_TYPE_WRAPPER(GenericFunctionType, AnyFunctionType)
31953190

31963191
inline CanAnyFunctionType
3197-
CanAnyFunctionType::get(CanGenericSignature signature,
3198-
CanType input, CanType result) {
3199-
return get(signature, input, result, ExtInfo());
3200-
}
3201-
3202-
inline CanAnyFunctionType
3203-
CanAnyFunctionType::get(CanGenericSignature signature,
3204-
CanType input, CanType result, const ExtInfo &extInfo) {
3192+
CanAnyFunctionType::getOld(CanGenericSignature signature,
3193+
CanType input, CanType result, ExtInfo extInfo) {
32053194
if (signature) {
3206-
return CanGenericFunctionType::get(signature, input, result, extInfo);
3195+
return CanGenericFunctionType::getOld(signature, input, result, extInfo);
32073196
} else {
3208-
return CanFunctionType::get(input, result, extInfo);
3197+
return CanFunctionType::getOld(input, result, extInfo);
32093198
}
32103199
}
32113200

32123201
inline CanAnyFunctionType
32133202
CanAnyFunctionType::get(CanGenericSignature signature, CanParamArrayRef params,
3214-
CanType result, const ExtInfo &extInfo) {
3203+
CanType result, ExtInfo extInfo) {
32153204
if (signature) {
32163205
return CanGenericFunctionType::get(signature, params, result, extInfo);
32173206
} else {

0 commit comments

Comments
 (0)