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,18 @@ 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
+ auto i = 0 ;
1704
+ for (auto param : *getterImpl->getParameters ()) {
1705
+ auto clonedParam = ParamDecl::clone (ctx, param);
1706
+ // If the subscript parameter is unnamed, give it a name to make sure SILGen
1707
+ // creates a variable for it.
1708
+ if (clonedParam->getName ().empty ())
1709
+ clonedParam->setName (ctx.getIdentifier (" __index" + std::to_string (i)));
1710
+ paramVec.push_back (clonedParam);
1711
+ ++i;
1712
+ }
1713
+ auto bodyParams = ParameterList::create (ctx, paramVec);
1713
1714
DeclName name (ctx, DeclBaseName::createSubscript (), bodyParams);
1714
1715
auto dc = getterImpl->getDeclContext ();
1715
1716
@@ -1744,8 +1745,11 @@ SubscriptDecl *SwiftDeclSynthesizer::makeSubscript(FuncDecl *getter,
1744
1745
paramVarDecl->setSpecifier (ParamSpecifier::Default);
1745
1746
paramVarDecl->setInterfaceType (elementTy);
1746
1747
1747
- auto setterParamList =
1748
- ParameterList::create (ctx, {paramVarDecl, bodyParams->get (0 )});
1748
+ SmallVector<ParamDecl *> setterParams;
1749
+ setterParams.push_back (paramVarDecl);
1750
+ setterParams.append (bodyParams->begin (), bodyParams->end ());
1751
+
1752
+ auto setterParamList = ParameterList::create (ctx, setterParams);
1749
1753
1750
1754
setterDecl = AccessorDecl::create (
1751
1755
ctx, setterImpl->getLoc (), setterImpl->getLoc (), AccessorKind::Set,
@@ -1918,7 +1922,7 @@ synthesizeSuccessorFuncBody(AbstractFunctionDecl *afd, void *context) {
1918
1922
1919
1923
// Call `operator++`.
1920
1924
auto incrementExpr = createAccessorImplCallExpr (
1921
- incrementImpl, Argument::implicitInOut (ctx, copyRefLValueExpr));
1925
+ incrementImpl, Argument::implicitInOut (ctx, copyRefLValueExpr), {} );
1922
1926
1923
1927
auto copyRefRValueExpr = new (ctx) DeclRefExpr (copyDecl, DeclNameLoc (),
1924
1928
/* implicit*/ true );
@@ -2324,7 +2328,7 @@ synthesizeComputedGetterFromCXXMethod(AbstractFunctionDecl *afd,
2324
2328
2325
2329
auto selfArg = createSelfArg (accessor);
2326
2330
2327
- auto *getterImplCallExpr = createAccessorImplCallExpr (method, selfArg);
2331
+ auto *getterImplCallExpr = createAccessorImplCallExpr (method, selfArg, {} );
2328
2332
auto &ctx = method->getASTContext ();
2329
2333
auto *returnStmt = ReturnStmt::createImplicit (ctx, getterImplCallExpr);
2330
2334
auto *body = BraceStmt::create (ctx, SourceLoc (), {returnStmt}, SourceLoc ());
@@ -2342,7 +2346,7 @@ synthesizeComputedSetterFromCXXMethod(AbstractFunctionDecl *afd,
2342
2346
DeclRefExpr *valueParamRefExpr = createParamRefExpr (setterDecl, 0 );
2343
2347
2344
2348
auto *getterImplCallExpr =
2345
- createAccessorImplCallExpr (setterImpl, selfArg, valueParamRefExpr);
2349
+ createAccessorImplCallExpr (setterImpl, selfArg, { valueParamRefExpr} );
2346
2350
2347
2351
auto body = BraceStmt::create (setterImpl->getASTContext (), SourceLoc (),
2348
2352
{getterImplCallExpr}, SourceLoc ());
0 commit comments