@@ -144,7 +144,7 @@ SwiftDeclSynthesizer::createVarWithPattern(DeclContext *dc, Identifier name,
144144 VarDecl::Introducer introducer,
145145 bool isImplicit, AccessLevel access,
146146 AccessLevel setterAccess) {
147- ASTContext &ctx = ImporterImpl. SwiftContext ;
147+ ASTContext &ctx = dc-> getASTContext () ;
148148
149149 // Create a variable to store the underlying value.
150150 auto var = new (ctx) VarDecl (
@@ -1720,6 +1720,97 @@ VarDecl *SwiftDeclSynthesizer::makeDereferencedPointeeProperty(
17201720 return result;
17211721}
17221722
1723+ // MARK: C++ increment operator
1724+
1725+ // / Synthesizer callback for a successor function.
1726+ // /
1727+ // / \code
1728+ // / var __copy: Self
1729+ // / __copy = self
1730+ // / __copy.__operatorPlusPlus()
1731+ // / return __copy
1732+ // / \endcode
1733+ static std::pair<BraceStmt *, bool >
1734+ synthesizeSuccessorFuncBody (AbstractFunctionDecl *afd, void *context) {
1735+ auto successorDecl = cast<FuncDecl>(afd);
1736+ auto incrementImpl = static_cast <FuncDecl *>(context);
1737+
1738+ ASTContext &ctx = successorDecl->getASTContext ();
1739+ auto emptyTupleTy = TupleType::getEmpty (ctx);
1740+ auto returnTy = successorDecl->getResultInterfaceType ();
1741+
1742+ auto selfDecl = successorDecl->getImplicitSelfDecl ();
1743+ auto selfRefExpr = new (ctx) DeclRefExpr (selfDecl, DeclNameLoc (),
1744+ /* implicit*/ true );
1745+ selfRefExpr->setType (selfDecl->getInterfaceType ());
1746+
1747+ // Create a `__copy` variable.
1748+ VarDecl *copyDecl = nullptr ;
1749+ PatternBindingDecl *patternDecl = nullptr ;
1750+ std::tie (copyDecl, patternDecl) = SwiftDeclSynthesizer::createVarWithPattern (
1751+ successorDecl, ctx.getIdentifier (" __copy" ), returnTy,
1752+ VarDecl::Introducer::Var,
1753+ /* isImplicit*/ true , AccessLevel::Public, AccessLevel::Public);
1754+
1755+ auto copyRefLValueExpr = new (ctx) DeclRefExpr (copyDecl, DeclNameLoc (),
1756+ /* implicit*/ true );
1757+ copyRefLValueExpr->setType (LValueType::get (copyDecl->getInterfaceType ()));
1758+
1759+ auto inoutCopyExpr = new (ctx) InOutExpr (
1760+ SourceLoc (), copyRefLValueExpr,
1761+ successorDecl->mapTypeIntoContext (copyDecl->getValueInterfaceType ()),
1762+ /* isImplicit*/ true );
1763+ inoutCopyExpr->setType (InOutType::get (copyDecl->getInterfaceType ()));
1764+
1765+ // Copy `self` to `__copy`.
1766+ auto copyAssignExpr = new (ctx) AssignExpr (copyRefLValueExpr, SourceLoc (),
1767+ selfRefExpr, /* implicit*/ true );
1768+ copyAssignExpr->setType (emptyTupleTy);
1769+
1770+ // Call `operator++`.
1771+ auto incrementExpr = createAccessorImplCallExpr (incrementImpl, inoutCopyExpr);
1772+
1773+ auto copyRefRValueExpr = new (ctx) DeclRefExpr (copyDecl, DeclNameLoc (),
1774+ /* implicit*/ true );
1775+ copyRefRValueExpr->setType (copyDecl->getInterfaceType ());
1776+
1777+ auto returnStmt = new (ctx) ReturnStmt (SourceLoc (), copyRefRValueExpr,
1778+ /* implicit*/ true );
1779+
1780+ auto body = BraceStmt::create (ctx, SourceLoc (),
1781+ {
1782+ copyDecl,
1783+ patternDecl,
1784+ copyAssignExpr,
1785+ incrementExpr,
1786+ returnStmt,
1787+ },
1788+ SourceLoc ());
1789+ return {body, /* isTypeChecked*/ true };
1790+ }
1791+
1792+ FuncDecl *SwiftDeclSynthesizer::makeSuccessorFunc (FuncDecl *incrementFunc) {
1793+ auto &ctx = ImporterImpl.SwiftContext ;
1794+ auto dc = incrementFunc->getDeclContext ();
1795+
1796+ auto returnTy = incrementFunc->getImplicitSelfDecl ()->getInterfaceType ();
1797+
1798+ auto nameId = ctx.getIdentifier (" successor" );
1799+ auto *params = ParameterList::createEmpty (ctx);
1800+ DeclName name (ctx, DeclBaseName (nameId), params);
1801+
1802+ auto result = FuncDecl::createImplicit (
1803+ ctx, StaticSpellingKind::None, name, SourceLoc (),
1804+ /* Async*/ false , /* Throws*/ false ,
1805+ /* GenericParams*/ nullptr , params, returnTy, dc);
1806+
1807+ result->setAccess (AccessLevel::Public);
1808+ result->setIsDynamic (false );
1809+ result->setBodySynthesizer (synthesizeSuccessorFuncBody, incrementFunc);
1810+
1811+ return result;
1812+ }
1813+
17231814// MARK: C++ arithmetic operators
17241815
17251816static std::pair<BraceStmt *, bool >
0 commit comments