Skip to content

Commit 24abf3e

Browse files
committed
[NFC] Refactor default argument info
Refactor the interface to return a bit vector. Along the way, fix up the callers and remove some dead usages of the defaults information that were copied and pasted around Sema.
1 parent 1ba5415 commit 24abf3e

File tree

9 files changed

+68
-75
lines changed

9 files changed

+68
-75
lines changed

include/swift/AST/Types.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "llvm/ADT/FoldingSet.h"
3636
#include "llvm/ADT/PointerEmbeddedInt.h"
3737
#include "llvm/ADT/PointerUnion.h"
38+
#include "llvm/ADT/SmallBitVector.h"
3839
#include "llvm/Support/ErrorHandling.h"
3940
#include "llvm/Support/TrailingObjects.h"
4041

@@ -2991,13 +2992,13 @@ END_CAN_TYPE_WRAPPER(FunctionType, AnyFunctionType)
29912992
SmallVector<AnyFunctionType::Param, 4>
29922993
decomposeArgType(Type type, ArrayRef<Identifier> argumentLabels);
29932994

2994-
/// Break the parameter list into an array of booleans describing whether
2995+
/// Map the given parameter list onto a bitvector describing whether
29952996
/// the argument type at each index has a default argument associated with
29962997
/// it.
2997-
void computeDefaultMap(ArrayRef<AnyFunctionType::Param> params,
2998-
const ValueDecl *paramOwner, unsigned level,
2999-
SmallVectorImpl<bool> &outDefaultMap);
3000-
2998+
llvm::SmallBitVector
2999+
computeDefaultMap(ArrayRef<AnyFunctionType::Param> params,
3000+
const ValueDecl *paramOwner, unsigned level);
3001+
30013002
/// Turn a param list into a symbolic and printable representation that does not
30023003
/// include the types, something like (: , b:, c:)
30033004
std::string getParamListAsString(ArrayRef<AnyFunctionType::Param> parameters);

lib/AST/Type.cpp

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -798,46 +798,54 @@ swift::decomposeArgType(Type type, ArrayRef<Identifier> argumentLabels) {
798798
return result;
799799
}
800800

801-
void swift::computeDefaultMap(ArrayRef<AnyFunctionType::Param> params,
802-
const ValueDecl *paramOwner, unsigned level,
803-
SmallVectorImpl<bool> &outDefaultMap) {
801+
llvm::SmallBitVector
802+
swift::computeDefaultMap(ArrayRef<AnyFunctionType::Param> params,
803+
const ValueDecl *paramOwner, unsigned level) {
804+
llvm::SmallBitVector resultVector(params.size());
805+
// No parameter owner means no parameter list means no default arguments
806+
// - hand back the zeroed bitvector.
807+
//
808+
// FIXME: We ought to not request default argument info in this case.
809+
if (!paramOwner)
810+
return resultVector;
811+
804812
// Find the corresponding parameter list.
805813
const ParameterList *paramList = nullptr;
806-
if (paramOwner) {
807-
if (auto *func = dyn_cast<AbstractFunctionDecl>(paramOwner)) {
808-
if (level < func->getNumParameterLists())
809-
paramList = func->getParameterList(level);
810-
} else if (auto *subscript = dyn_cast<SubscriptDecl>(paramOwner)) {
811-
if (level == 1)
812-
paramList = subscript->getIndices();
813-
} else if (auto *enumElement = dyn_cast<EnumElementDecl>(paramOwner)) {
814-
if (level == 1)
815-
paramList = enumElement->getParameterList();
816-
}
817-
}
814+
if (auto *func = dyn_cast<AbstractFunctionDecl>(paramOwner)) {
815+
if (level < func->getNumParameterLists())
816+
paramList = func->getParameterList(level);
817+
} else if (auto *subscript = dyn_cast<SubscriptDecl>(paramOwner)) {
818+
if (level == 1)
819+
paramList = subscript->getIndices();
820+
} else if (auto *enumElement = dyn_cast<EnumElementDecl>(paramOwner)) {
821+
if (level == 1)
822+
paramList = enumElement->getParameterList();
823+
}
824+
825+
// No parameter list means no default arguments - hand back the zeroed
826+
// bitvector.
827+
if (!paramList)
828+
return resultVector;
818829

819830
switch (params.size()) {
820831
case 0:
821-
break;
822-
823-
case 1:
824-
outDefaultMap.push_back(paramList && paramList->size() == 1 &&
825-
paramList->get(0)->isDefaultArgument());
826-
break;
832+
return resultVector;
827833

828834
default:
829835
// Arguments and parameters are not guaranteed to always line-up
830836
// perfectly, e.g. failure diagnostics tries to match argument type
831837
// to different "candidate" parameters.
832-
if (paramList && params.size() != paramList->size())
833-
paramList = nullptr;
838+
if (params.size() != paramList->size())
839+
return resultVector;
834840

835841
for (auto i : range(0, params.size())) {
836-
outDefaultMap.push_back(paramList &&
837-
paramList->get(i)->isDefaultArgument());
842+
if (paramList->get(i)->isDefaultArgument()) {
843+
resultVector.set(i);
844+
}
838845
}
839846
break;
840847
}
848+
return resultVector;
841849
}
842850

843851
/// Turn a param list into a symbolic and printable representation that does not

lib/IDE/CodeCompletion.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,17 +1543,11 @@ protocolForLiteralKind(CodeCompletionLiteralKind kind) {
15431543
/// that is of type () -> ().
15441544
static bool hasTrivialTrailingClosure(const FuncDecl *FD,
15451545
AnyFunctionType *funcType) {
1546-
SmallVector<bool, 4> defaultMap;
1547-
computeDefaultMap(funcType->getParams(), FD,
1548-
/*level*/ FD->isInstanceMember() ? 1 : 0, defaultMap);
1546+
llvm::SmallBitVector defaultMap =
1547+
computeDefaultMap(funcType->getParams(), FD,
1548+
/*level*/ FD->isInstanceMember() ? 1 : 0);
15491549

1550-
bool OneArg = defaultMap.size() == 1;
1551-
if (defaultMap.size() > 1) {
1552-
auto NonDefault = std::count(defaultMap.begin(), defaultMap.end() - 1, false);
1553-
OneArg = (NonDefault == 0);
1554-
}
1555-
1556-
if (OneArg) {
1550+
if (defaultMap.size() - defaultMap.count() == 1) {
15571551
auto param = funcType->getParams().back();
15581552
if (!param.isAutoClosure()) {
15591553
if (auto Fn = param.getType()->getAs<AnyFunctionType>()) {

lib/Sema/CSApply.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5764,8 +5764,8 @@ Expr *ExprRewriter::coerceCallArguments(
57645764
unsigned level = apply ? computeCallLevel(cs, callee, apply) : 0;
57655765

57665766
// Determine the parameter bindings.
5767-
SmallVector<bool, 4> defaultMap;
5768-
computeDefaultMap(params, callee.getDecl(), level, defaultMap);
5767+
llvm::SmallBitVector defaultMap
5768+
= computeDefaultMap(params, callee.getDecl(), level);
57695769
auto args = decomposeArgType(cs.getType(arg), argLabels);
57705770

57715771
// Quickly test if any further fix-ups for the argument types are necessary.

lib/Sema/CSDiag.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3460,12 +3460,10 @@ typeCheckArgumentChildIndependently(Expr *argExpr, Type argType,
34603460

34613461
// If we have a candidate function around, compute the position of its
34623462
// default arguments.
3463-
SmallVector<bool, 4> defaultMap;
3464-
if (candidates.empty()) {
3465-
defaultMap.assign(params.size(), false);
3466-
} else {
3467-
computeDefaultMap(params, candidates[0].getDecl(),
3468-
candidates[0].level, defaultMap);
3463+
llvm::SmallBitVector defaultMap(params.size());
3464+
if (!candidates.empty()) {
3465+
defaultMap = computeDefaultMap(params, candidates[0].getDecl(),
3466+
candidates[0].level);
34693467
}
34703468

34713469
// Form a set of call arguments, using a dummy type (Void), because the
@@ -3873,9 +3871,6 @@ diagnoseInstanceMethodAsCurriedMemberOnType(CalleeCandidateInfo &CCI,
38733871
continue;
38743872

38753873
auto params = candidate.getParameters();
3876-
3877-
SmallVector<bool, 4> defaultMap;
3878-
computeDefaultMap(params, decl, candidate.level, defaultMap);
38793874
// If one of the candidates is an instance method with a single parameter
38803875
// at the level 0, this might be viable situation for calling instance
38813876
// method as curried member of type problem.
@@ -4071,7 +4066,7 @@ class ArgumentMatcher : public MatchCallArgumentListener {
40714066
Expr *FnExpr;
40724067
Expr *ArgExpr;
40734068
ArrayRef<AnyFunctionType::Param> &Parameters;
4074-
SmallVectorImpl<bool> &DefaultMap;
4069+
const llvm::SmallBitVector &DefaultMap;
40754070
SmallVectorImpl<AnyFunctionType::Param> &Arguments;
40764071

40774072
CalleeCandidateInfo CandidateInfo;
@@ -4087,7 +4082,7 @@ class ArgumentMatcher : public MatchCallArgumentListener {
40874082
public:
40884083
ArgumentMatcher(Expr *fnExpr, Expr *argExpr,
40894084
ArrayRef<AnyFunctionType::Param> &params,
4090-
SmallVectorImpl<bool> &defaultMap,
4085+
const llvm::SmallBitVector &defaultMap,
40914086
SmallVectorImpl<AnyFunctionType::Param> &args,
40924087
CalleeCandidateInfo &CCI, bool isSubscript)
40934088
: TC(CCI.CS.TC), FnExpr(fnExpr), ArgExpr(argExpr), Parameters(params),
@@ -4380,8 +4375,8 @@ diagnoseSingleCandidateFailures(CalleeCandidateInfo &CCI, Expr *fnExpr,
43804375

43814376
auto params = candidate.getParameters();
43824377

4383-
SmallVector<bool, 4> defaultMap;
4384-
computeDefaultMap(params, candidate.getDecl(), candidate.level, defaultMap);
4378+
llvm::SmallBitVector defaultMap =
4379+
computeDefaultMap(params, candidate.getDecl(), candidate.level);
43854380
auto args = decomposeArgType(CCI.CS.getType(argExpr), argLabels);
43864381

43874382
// Check the case where a raw-representable type is constructed from an
@@ -4500,9 +4495,6 @@ static bool diagnoseRawRepresentableMismatch(CalleeCandidateInfo &CCI,
45004495
continue;
45014496

45024497
auto parameters = candidate.getParameters();
4503-
4504-
SmallVector<bool, 4> defaultMap;
4505-
computeDefaultMap(parameters, decl, candidate.level, defaultMap);
45064498
if (parameters.size() != arguments.size())
45074499
continue;
45084500

@@ -5007,8 +4999,8 @@ bool FailureDiagnosis::diagnoseArgumentGenericRequirements(
50074999
return false;
50085000

50095001
auto params = candidate.getParameters();
5010-
SmallVector<bool, 4> defaultMap;
5011-
computeDefaultMap(params, candidate.getDecl(), candidate.level, defaultMap);
5002+
llvm::SmallBitVector defaultMap =
5003+
computeDefaultMap(params, candidate.getDecl(), candidate.level);
50125004
auto args = decomposeArgType(CS.getType(argExpr), argLabels);
50135005

50145006
SmallVector<ParamBinding, 4> bindings;

lib/Sema/CSRanking.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,8 @@ static bool isDeclAsSpecializedAs(TypeChecker &tc, DeclContext *dc,
602602
auto funcTy2 = openedType2->castTo<FunctionType>();
603603
auto params1 = funcTy1->getParams();
604604
auto params2 = funcTy2->getParams();
605-
SmallVector<bool, 4> defaultMapType2;
606-
computeDefaultMap(params2, decl2, outerDC2->isTypeContext(),
607-
defaultMapType2);
605+
llvm::SmallBitVector defaultMapType2 =
606+
computeDefaultMap(params2, decl2, outerDC2->isTypeContext());
608607

609608
unsigned numParams1 = params1.size();
610609
unsigned numParams2 = params2.size();

lib/Sema/CSSimplify.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ areConservativelyCompatibleArgumentLabels(ValueDecl *decl,
102102
}
103103

104104
auto params = levelTy->getParams();
105-
SmallVector<bool, 4> defaultMap;
106-
computeDefaultMap(params, decl, parameterDepth, defaultMap);
105+
llvm::SmallBitVector defaultMap =
106+
computeDefaultMap(params, decl, parameterDepth);
107107

108108
MatchCallArgumentListener listener;
109109
SmallVector<ParamBinding, 8> unusedParamBindings;
@@ -126,7 +126,7 @@ static ConstraintSystem::TypeMatchOptions getDefaultDecompositionOptions(
126126
bool constraints::
127127
matchCallArguments(ArrayRef<AnyFunctionType::Param> args,
128128
ArrayRef<AnyFunctionType::Param> params,
129-
const SmallVectorImpl<bool> &defaultMap,
129+
const llvm::SmallBitVector &defaultMap,
130130
bool hasTrailingClosure,
131131
bool allowFixes,
132132
MatchCallArgumentListener &listener,
@@ -441,7 +441,7 @@ matchCallArguments(ArrayRef<AnyFunctionType::Param> args,
441441
continue;
442442

443443
// Parameters with defaults can be unfulfilled.
444-
if (defaultMap[paramIdx])
444+
if (defaultMap.test(paramIdx))
445445
continue;
446446

447447
listener.missingArgument(paramIdx);
@@ -701,8 +701,8 @@ matchCallArguments(ConstraintSystem &cs, ConstraintKind kind,
701701
SmallVector<AnyFunctionType::Param, 4> params;
702702
AnyFunctionType::decomposeInput(paramType, params);
703703

704-
SmallVector<bool, 4> defaultMap;
705-
computeDefaultMap(params, callee, calleeLevel, defaultMap);
704+
llvm::SmallBitVector defaultMap =
705+
computeDefaultMap(params, callee, calleeLevel);
706706

707707
if (callee && cs.getASTContext().isSwiftVersion3()
708708
&& argType->is<TupleType>()) {
@@ -1032,8 +1032,8 @@ ConstraintSystem::matchFunctionParamTypes(ArrayRef<AnyFunctionType::Param> type1
10321032
return matchTypes(argType, paramType, kind, flags, locator);
10331033
}
10341034

1035-
SmallVector<bool, 4> defaultMap;
1036-
computeDefaultMap(type1, callee, calleeLevel, defaultMap);
1035+
llvm::SmallBitVector defaultMap =
1036+
computeDefaultMap(type1, callee, calleeLevel);
10371037

10381038
// Match up the call arguments to the parameters.
10391039
MatchCallArgumentListener listener;

lib/Sema/CalleeCandidateInfo.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,8 @@ CalleeCandidateInfo::evaluateCloseness(UncurriedCandidate candidate,
308308
return {CC_GeneralMismatch, {}};
309309

310310
auto candArgs = candidate.getParameters();
311-
SmallVector<bool, 4> candDefaultMap;
312-
computeDefaultMap(candArgs, candidate.getDecl(), candidate.level,
313-
candDefaultMap);
311+
llvm::SmallBitVector candDefaultMap =
312+
computeDefaultMap(candArgs, candidate.getDecl(), candidate.level);
314313

315314
struct OurListener : public MatchCallArgumentListener {
316315
CandidateCloseness result = CC_ExactMatch;

lib/Sema/ConstraintSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3226,7 +3226,7 @@ class MatchCallArgumentListener {
32263226
/// \returns true if the call arguments could not be matched to the parameters.
32273227
bool matchCallArguments(ArrayRef<AnyFunctionType::Param> argTuple,
32283228
ArrayRef<AnyFunctionType::Param> params,
3229-
const SmallVectorImpl<bool> &defaultMap,
3229+
const llvm::SmallBitVector &defaultMap,
32303230
bool hasTrailingClosure,
32313231
bool allowFixes,
32323232
MatchCallArgumentListener &listener,

0 commit comments

Comments
 (0)