10
10
//
11
11
// ===----------------------------------------------------------------------===//
12
12
13
- #include " CXXMethodBridging.h"
14
13
#include " SwiftDeclSynthesizer.h"
14
+ #include " CXXMethodBridging.h"
15
15
#include " swift/AST/ASTMangler.h"
16
16
#include " swift/AST/Attr.h"
17
17
#include " swift/AST/AttrKind.h"
@@ -47,7 +47,7 @@ static Argument createSelfArg(AccessorDecl *accessorDecl) {
47
47
48
48
static CallExpr *createAccessorImplCallExpr (FuncDecl *accessorImpl,
49
49
Argument selfArg,
50
- DeclRefExpr *keyRefExpr = nullptr ) {
50
+ ArrayRef<Expr *> keyRefExprs ) {
51
51
ASTContext &ctx = accessorImpl->getASTContext ();
52
52
53
53
auto accessorImplExpr =
@@ -60,12 +60,8 @@ static CallExpr *createAccessorImplCallExpr(FuncDecl *accessorImpl,
60
60
accessorImplDotCallExpr->setType (accessorImpl->getMethodInterfaceType ());
61
61
accessorImplDotCallExpr->setThrows (nullptr );
62
62
63
- ArgumentList *argList;
64
- if (keyRefExpr) {
65
- argList = ArgumentList::forImplicitUnlabeled (ctx, {keyRefExpr});
66
- } else {
67
- argList = ArgumentList::forImplicitUnlabeled (ctx, {});
68
- }
63
+ ArgumentList *argList = ArgumentList::forImplicitUnlabeled (ctx, keyRefExprs);
64
+
69
65
auto *accessorImplCallExpr =
70
66
CallExpr::createImplicit (ctx, accessorImplDotCallExpr, argList);
71
67
accessorImplCallExpr->setType (accessorImpl->getResultInterfaceType ());
@@ -1556,33 +1552,36 @@ synthesizeUnwrappingGetterOrAddressGetterBody(AbstractFunctionDecl *afd,
1556
1552
ASTContext &ctx = getterDecl->getASTContext ();
1557
1553
1558
1554
auto selfArg = createSelfArg (getterDecl);
1559
- DeclRefExpr *keyRefExpr = getterDecl->getParameters ()->size () == 0
1560
- ? nullptr
1561
- : createParamRefExpr (getterDecl, 0 );
1555
+ SmallVector<Expr *> arguments;
1556
+ for (size_t idx = 0 , end = getterDecl->getParameters ()->size (); idx < end;
1557
+ ++idx)
1558
+ arguments.push_back (createParamRefExpr (getterDecl, idx));
1562
1559
1563
1560
Type elementTy = getterDecl->getResultInterfaceType ();
1564
1561
1565
1562
auto *getterImplCallExpr =
1566
- createAccessorImplCallExpr (getterImpl, selfArg, keyRefExpr );
1563
+ createAccessorImplCallExpr (getterImpl, selfArg, arguments );
1567
1564
1568
1565
// This default handles C++'s operator[] that returns a value type.
1569
1566
Expr *propertyExpr = getterImplCallExpr;
1570
1567
PointerTypeKind ptrKind;
1571
1568
1572
- // The following check returns true if the subscript operator returns a C++
1573
- // reference type. This check actually checks to see if the type is a pointer
1574
- // type, but this does not apply to C pointers because they are Optional types
1575
- // when imported. TODO: Use a more obvious check here.
1569
+ // The following check returns true if the subscript operator returns a
1570
+ // C++ reference type. This check actually checks to see if the type is
1571
+ // a pointer type, but this does not apply to C pointers because they
1572
+ // are Optional types when imported. TODO: Use a more obvious check
1573
+ // here.
1576
1574
if (!isAddress &&
1577
1575
getterImpl->getResultInterfaceType ()->getAnyPointerElementType (ptrKind)) {
1578
- // `getterImpl` can return either UnsafePointer or UnsafeMutablePointer.
1579
- // Retrieve the corresponding `.pointee` declaration.
1576
+ // `getterImpl` can return either UnsafePointer or
1577
+ // UnsafeMutablePointer. Retrieve the corresponding `.pointee`
1578
+ // declaration.
1580
1579
VarDecl *pointeePropertyDecl = ctx.getPointerPointeePropertyDecl (ptrKind);
1581
1580
1582
1581
// Handle operator[] that returns a reference type.
1583
- SubstitutionMap subMap = SubstitutionMap::get (
1584
- ctx.getUnsafePointerDecl ()->getGenericSignature (), {elementTy} ,
1585
- LookUpConformanceInModule ());
1582
+ SubstitutionMap subMap =
1583
+ SubstitutionMap::get ( ctx.getUnsafePointerDecl ()->getGenericSignature (),
1584
+ {elementTy}, LookUpConformanceInModule ());
1586
1585
auto pointeePropertyRefExpr = new (ctx) MemberRefExpr (
1587
1586
getterImplCallExpr, SourceLoc (),
1588
1587
ConcreteDeclRef (pointeePropertyDecl, subMap), DeclNameLoc (),
@@ -1627,16 +1626,15 @@ synthesizeUnwrappingSetterBody(AbstractFunctionDecl *afd, void *context) {
1627
1626
1628
1627
auto selfArg = createSelfArg (setterDecl);
1629
1628
DeclRefExpr *valueParamRefExpr = createParamRefExpr (setterDecl, 0 );
1630
- // For a subscript this decl will have two parameters, for a pointee property
1631
- // it will only have one.
1632
- DeclRefExpr *keyParamRefExpr = setterDecl->getParameters ()->size () == 1
1633
- ? nullptr
1634
- : createParamRefExpr (setterDecl, 1 );
1629
+ SmallVector<Expr *> arguments;
1630
+ for (size_t idx = 1 , end = setterDecl->getParameters ()->size (); idx < end;
1631
+ ++idx)
1632
+ arguments.push_back (createParamRefExpr (setterDecl, idx));
1635
1633
1636
1634
Type elementTy = valueParamRefExpr->getDecl ()->getInterfaceType ();
1637
1635
1638
1636
auto *setterImplCallExpr =
1639
- createAccessorImplCallExpr (setterImpl, selfArg, keyParamRefExpr );
1637
+ createAccessorImplCallExpr (setterImpl, selfArg, arguments );
1640
1638
1641
1639
VarDecl *pointeePropertyDecl =
1642
1640
ctx.getPointerPointeePropertyDecl (PTK_UnsafeMutablePointer);
@@ -1673,7 +1671,7 @@ synthesizeUnwrappingAddressSetterBody(AbstractFunctionDecl *afd,
1673
1671
1674
1672
auto selfArg = createSelfArg (setterDecl);
1675
1673
auto *setterImplCallExpr =
1676
- createAccessorImplCallExpr (setterImpl, selfArg, nullptr );
1674
+ createAccessorImplCallExpr (setterImpl, selfArg, {} );
1677
1675
1678
1676
auto *returnStmt = ReturnStmt::createImplicit (ctx, setterImplCallExpr);
1679
1677
@@ -1701,15 +1699,16 @@ SubscriptDecl *SwiftDeclSynthesizer::makeSubscript(FuncDecl *getter,
1701
1699
1702
1700
auto &ctx = ImporterImpl.SwiftContext ;
1703
1701
1704
- assert (getterImpl->getParameters ()->size () == 1 &&
1705
- " subscript can only have 1 parameter" );
1706
- auto bodyParam = ParamDecl::clone (ctx, getterImpl->getParameters ()->get (0 ));
1707
- // If the subscript parameter is unnamed, give it a name to make sure SILGen
1708
- // creates a variable for it.
1709
- if (bodyParam->getName ().empty ())
1710
- bodyParam->setName (ctx.getIdentifier (" __index" ));
1711
-
1712
- auto bodyParams = ParameterList::create (ctx, bodyParam);
1702
+ SmallVector<ParamDecl *> paramVec;
1703
+ for (auto [i, param] : llvm::enumerate (*getterImpl->getParameters ())) {
1704
+ auto clonedParam = ParamDecl::clone (ctx, param);
1705
+ // If the subscript parameter is unnamed, give it a name to make sure SILGen
1706
+ // creates a variable for it.
1707
+ if (clonedParam->getName ().empty ())
1708
+ clonedParam->setName (ctx.getIdentifier (" __index" + std::to_string (i)));
1709
+ paramVec.push_back (clonedParam);
1710
+ }
1711
+ auto bodyParams = ParameterList::create (ctx, paramVec);
1713
1712
DeclName name (ctx, DeclBaseName::createSubscript (), bodyParams);
1714
1713
auto dc = getterImpl->getDeclContext ();
1715
1714
@@ -1744,8 +1743,11 @@ SubscriptDecl *SwiftDeclSynthesizer::makeSubscript(FuncDecl *getter,
1744
1743
paramVarDecl->setSpecifier (ParamSpecifier::Default);
1745
1744
paramVarDecl->setInterfaceType (elementTy);
1746
1745
1747
- auto setterParamList =
1748
- ParameterList::create (ctx, {paramVarDecl, bodyParams->get (0 )});
1746
+ SmallVector<ParamDecl *> setterParams;
1747
+ setterParams.push_back (paramVarDecl);
1748
+ setterParams.append (bodyParams->begin (), bodyParams->end ());
1749
+
1750
+ auto setterParamList = ParameterList::create (ctx, setterParams);
1749
1751
1750
1752
setterDecl = AccessorDecl::create (
1751
1753
ctx, setterImpl->getLoc (), setterImpl->getLoc (), AccessorKind::Set,
@@ -1918,7 +1920,7 @@ synthesizeSuccessorFuncBody(AbstractFunctionDecl *afd, void *context) {
1918
1920
1919
1921
// Call `operator++`.
1920
1922
auto incrementExpr = createAccessorImplCallExpr (
1921
- incrementImpl, Argument::implicitInOut (ctx, copyRefLValueExpr));
1923
+ incrementImpl, Argument::implicitInOut (ctx, copyRefLValueExpr), {} );
1922
1924
1923
1925
auto copyRefRValueExpr = new (ctx) DeclRefExpr (copyDecl, DeclNameLoc (),
1924
1926
/* implicit*/ true );
@@ -2331,7 +2333,7 @@ synthesizeComputedGetterFromCXXMethod(AbstractFunctionDecl *afd,
2331
2333
2332
2334
auto selfArg = createSelfArg (accessor);
2333
2335
2334
- auto *getterImplCallExpr = createAccessorImplCallExpr (method, selfArg);
2336
+ auto *getterImplCallExpr = createAccessorImplCallExpr (method, selfArg, {} );
2335
2337
auto &ctx = method->getASTContext ();
2336
2338
auto *returnStmt = ReturnStmt::createImplicit (ctx, getterImplCallExpr);
2337
2339
auto *body = BraceStmt::create (ctx, SourceLoc (), {returnStmt}, SourceLoc ());
@@ -2349,7 +2351,7 @@ synthesizeComputedSetterFromCXXMethod(AbstractFunctionDecl *afd,
2349
2351
DeclRefExpr *valueParamRefExpr = createParamRefExpr (setterDecl, 0 );
2350
2352
2351
2353
auto *getterImplCallExpr =
2352
- createAccessorImplCallExpr (setterImpl, selfArg, valueParamRefExpr);
2354
+ createAccessorImplCallExpr (setterImpl, selfArg, { valueParamRefExpr} );
2353
2355
2354
2356
auto body = BraceStmt::create (setterImpl->getASTContext (), SourceLoc (),
2355
2357
{getterImplCallExpr}, SourceLoc ());
0 commit comments