Skip to content

Commit 0795c99

Browse files
committed
Unify CallArgParam and AnyFunctionType::Param
1 parent 777e1f9 commit 0795c99

File tree

6 files changed

+83
-130
lines changed

6 files changed

+83
-130
lines changed

include/swift/AST/Types.h

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,6 +2308,8 @@ class AnyFunctionType : public TypeBase {
23082308
explicit Param(const TupleTypeElt &tte)
23092309
: Ty(tte.isVararg() ? tte.getVarargBaseTy() : tte.getType()),
23102310
Label(tte.getName()), Flags(tte.getParameterFlags()) {}
2311+
explicit Param(Type t, Identifier l, ParameterTypeFlags f)
2312+
: Ty(t), Label(l), Flags(f) {}
23112313

23122314
private:
23132315
/// The type of the parameter. For a variadic parameter, this is the
@@ -2485,6 +2487,10 @@ class AnyFunctionType : public TypeBase {
24852487
}
24862488

24872489
public:
2490+
/// \brief Break an input type into an array of \c AnyFunctionType::Params.
2491+
static void decomposeInput(Type type,
2492+
SmallVectorImpl<AnyFunctionType::Param> &result);
2493+
24882494
Type getInput() const { return Input; }
24892495
Type getResult() const { return Output; }
24902496
ArrayRef<AnyFunctionType::Param> getParams() const;
@@ -2581,37 +2587,12 @@ BEGIN_CAN_TYPE_WRAPPER(FunctionType, AnyFunctionType)
25812587
return CanFunctionType(cast<FunctionType>(getPointer()->withExtInfo(info)));
25822588
}
25832589
END_CAN_TYPE_WRAPPER(FunctionType, AnyFunctionType)
2584-
2585-
/// A call argument or parameter.
2586-
struct CallArgParam {
2587-
/// The type of the argument or parameter. For a variadic parameter,
2588-
/// this is the element type.
2589-
Type Ty;
2590-
2591-
// The label associated with the argument or parameter, if any.
2592-
Identifier Label;
25932590

2594-
/// Parameter specific flags, not valid for arguments
2595-
ParameterTypeFlags parameterFlags = {};
2596-
2597-
/// Whether the argument or parameter has a label.
2598-
bool hasLabel() const { return !Label.empty(); }
2599-
2600-
/// Whether the parameter is varargs
2601-
bool isVariadic() const { return parameterFlags.isVariadic(); }
2602-
2603-
/// Whether the parameter is autoclosure
2604-
bool isAutoClosure() const { return parameterFlags.isAutoClosure(); }
2605-
2606-
/// Whether the parameter is escaping
2607-
bool isEscaping() const { return parameterFlags.isEscaping(); }
2608-
};
2609-
2610-
/// Break an argument type into an array of \c CallArgParams.
2591+
/// Break an argument type into an array of \c AnyFunctionType::Params.
26112592
///
26122593
/// \param type The type to decompose.
26132594
/// \param argumentLabels The argument labels to use.
2614-
SmallVector<CallArgParam, 4>
2595+
SmallVector<AnyFunctionType::Param, 4>
26152596
decomposeArgType(Type type, ArrayRef<Identifier> argumentLabels);
26162597

26172598
/// Break the parameter list into an array of booleans describing whether
@@ -2622,7 +2603,7 @@ void computeDefaultMap(Type type, const ValueDecl *paramOwner, unsigned level,
26222603

26232604
/// Turn a param list into a symbolic and printable representation that does not
26242605
/// include the types, something like (: , b:, c:)
2625-
std::string getParamListAsString(ArrayRef<CallArgParam> parameters);
2606+
std::string getParamListAsString(ArrayRef<AnyFunctionType::Param> parameters);
26262607

26272608
/// Describes a generic function type.
26282609
///

lib/AST/ASTContext.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3163,47 +3163,48 @@ AnyFunctionType *AnyFunctionType::withExtInfo(ExtInfo info) const {
31633163
llvm_unreachable("unhandled function type");
31643164
}
31653165

3166-
static SmallVector<AnyFunctionType::Param, 4> decomposeInputType(Type type) {
3167-
SmallVector<AnyFunctionType::Param, 4> result;
3166+
void AnyFunctionType::decomposeInput(
3167+
Type type, SmallVectorImpl<AnyFunctionType::Param> &result) {
31683168
switch (type->getKind()) {
31693169
case TypeKind::Tuple: {
31703170
auto tupleTy = cast<TupleType>(type.getPointer());
31713171
for (auto &elt : tupleTy->getElements()) {
31723172
result.push_back(AnyFunctionType::Param(elt));
31733173
}
3174-
return result;
3174+
return;
31753175
}
31763176

31773177
case TypeKind::Paren: {
31783178
auto ty = cast<ParenType>(type.getPointer())->getUnderlyingType();
31793179
result.push_back(AnyFunctionType::Param(ty));
3180-
return result;
3180+
return;
31813181
}
31823182

31833183
default:
31843184
result.push_back(AnyFunctionType::Param(type));
3185-
return result;
3185+
return;
31863186
}
31873187
}
31883188

3189-
FunctionType *FunctionType::get(Type Input, Type Result,
3190-
const ExtInfo &Info) {
3191-
auto properties = getFunctionRecursiveProperties(Input, Result);
3189+
FunctionType *FunctionType::get(Type input, Type result,
3190+
const ExtInfo &info) {
3191+
auto properties = getFunctionRecursiveProperties(input, result);
31923192
auto arena = getArena(properties);
3193-
uint16_t attrKey = Info.getFuncAttrKey();
3193+
uint16_t attrKey = info.getFuncAttrKey();
31943194

3195-
const ASTContext &C = Input->getASTContext();
3195+
const ASTContext &C = input->getASTContext();
31963196

31973197
FunctionType *&Entry
3198-
= C.Impl.getArena(arena).FunctionTypes[{Input, {Result, attrKey} }];
3198+
= C.Impl.getArena(arena).FunctionTypes[{input, {result, attrKey} }];
31993199
if (Entry) return Entry;
32003200

3201-
auto params = decomposeInputType(Input);
3201+
SmallVector<AnyFunctionType::Param, 4> params;
3202+
AnyFunctionType::decomposeInput(input, params);
32023203
void *mem = C.Allocate(sizeof(FunctionType) +
32033204
sizeof(AnyFunctionType::Param) * params.size(),
32043205
alignof(FunctionType));
3205-
return Entry = new (mem) FunctionType(params, Input, Result,
3206-
properties, Info);
3206+
return Entry = new (mem) FunctionType(params, input, result,
3207+
properties, info);
32073208
}
32083209

32093210
// If the input and result types are canonical, then so is the result.
@@ -3265,7 +3266,8 @@ GenericFunctionType::get(GenericSignature *sig,
32653266
return result;
32663267
}
32673268

3268-
auto params = decomposeInputType(input);
3269+
SmallVector<AnyFunctionType::Param, 4> params;
3270+
AnyFunctionType::decomposeInput(input, params);
32693271
void *mem = ctx.Allocate(sizeof(GenericFunctionType) +
32703272
sizeof(AnyFunctionType::Param) * params.size(),
32713273
alignof(GenericFunctionType));

lib/AST/Type.cpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -709,13 +709,13 @@ Type TypeBase::replaceCovariantResultType(Type newResultType,
709709
return FunctionType::get(inputType, resultType, fnType->getExtInfo());
710710
}
711711

712-
SmallVector<CallArgParam, 4>
712+
SmallVector<AnyFunctionType::Param, 4>
713713
swift::decomposeArgType(Type type, ArrayRef<Identifier> argumentLabels) {
714-
SmallVector<CallArgParam, 4> result;
714+
SmallVector<AnyFunctionType::Param, 4> result;
715715
switch (type->getKind()) {
716716
case TypeKind::Tuple: {
717717
auto tupleTy = cast<TupleType>(type.getPointer());
718-
718+
719719
// If we have one argument label but a tuple argument with > 1 element,
720720
// put the whole tuple into the argument.
721721
// FIXME: This horribleness is due to the mis-modeling of arguments as
@@ -724,40 +724,33 @@ swift::decomposeArgType(Type type, ArrayRef<Identifier> argumentLabels) {
724724
// Break out to do the default thing below.
725725
break;
726726
}
727-
727+
728728
for (auto i : range(0, tupleTy->getNumElements())) {
729729
const auto &elt = tupleTy->getElement(i);
730730
assert((elt.getParameterFlags().isNone() ||
731731
elt.getParameterFlags().isInOut()) &&
732732
"Vararg, autoclosure, or escaping argument tuple"
733733
"doesn't make sense");
734-
CallArgParam argParam;
735-
argParam.Ty = elt.getType();
736-
argParam.Label = argumentLabels[i];
737-
result.push_back(argParam);
734+
result.push_back(AnyFunctionType::Param(elt.getType(),
735+
argumentLabels[i], {}));
738736
}
739737
return result;
740738
}
741-
739+
742740
case TypeKind::Paren: {
743-
CallArgParam argParam;
744-
argParam.Ty = cast<ParenType>(type.getPointer())->getUnderlyingType();
745-
result.push_back(argParam);
741+
auto ty = cast<ParenType>(type.getPointer())->getUnderlyingType();
742+
result.push_back(AnyFunctionType::Param(ty, Identifier(), {}));
746743
return result;
747744
}
748-
745+
749746
default:
750747
// Default behavior below; inject the argument as the sole parameter.
751748
break;
752749
}
753-
750+
754751
// Just inject this parameter.
755-
assert(result.empty());
756-
CallArgParam argParam;
757-
argParam.Ty = type;
758-
assert(argumentLabels.size() == 1);
759-
argParam.Label = argumentLabels[0];
760-
result.push_back(argParam);
752+
assert(result.empty() && (argumentLabels.size() == 1));
753+
result.push_back(AnyFunctionType::Param(type, argumentLabels[0], {}));
761754
return result;
762755
}
763756

@@ -805,13 +798,13 @@ void swift::computeDefaultMap(Type type, const ValueDecl *paramOwner,
805798

806799
/// Turn a param list into a symbolic and printable representation that does not
807800
/// include the types, something like (_:, b:, c:)
808-
std::string swift::getParamListAsString(ArrayRef<CallArgParam> params) {
801+
std::string swift::getParamListAsString(ArrayRef<AnyFunctionType::Param> params) {
809802
std::string result = "(";
810803

811804
interleave(params,
812-
[&](const CallArgParam &param) {
813-
if (param.hasLabel())
814-
result += param.Label.str();
805+
[&](const AnyFunctionType::Param &param) {
806+
if (!param.getLabel().empty())
807+
result += param.getLabel().str();
815808
else
816809
result += "_";
817810
result += ":";

0 commit comments

Comments
 (0)