@@ -462,7 +462,8 @@ namespace {
462462 RValue visitKeyPathExpr (KeyPathExpr *E, SGFContext C);
463463 RValue visitMagicIdentifierLiteralExpr (MagicIdentifierLiteralExpr *E,
464464 SGFContext C);
465- RValue visitCollectionExpr (CollectionExpr *E, SGFContext C);
465+ RValue visitArrayExpr (ArrayExpr *E, SGFContext C);
466+ RValue visitDictionaryExpr (DictionaryExpr *E, SGFContext C);
466467 RValue visitRebindSelfInConstructorExpr (RebindSelfInConstructorExpr *E,
467468 SGFContext C);
468469 RValue visitInjectIntoOptionalExpr (InjectIntoOptionalExpr *E, SGFContext C);
@@ -3652,7 +3653,51 @@ visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *E, SGFContext C) {
36523653 llvm_unreachable (" Unhandled MagicIdentifierLiteralExpr in switch." );
36533654}
36543655
3655- RValue RValueEmitter::visitCollectionExpr (CollectionExpr *E, SGFContext C) {
3656+ RValue RValueEmitter::visitArrayExpr (ArrayExpr *E, SGFContext C) {
3657+ auto loc = SILLocation (E);
3658+ ArgumentScope scope (SGF, loc);
3659+ auto init = E->getInitializer ();
3660+ auto *decl = dyn_cast<AbstractFunctionDecl>(init.getDecl ());
3661+ Type elementType = E->getElementType ();
3662+ ArrayRef<Identifier> argLabels = decl->getFullName ().getArgumentNames ();
3663+ CanType arrayTy = ArraySliceType::get (elementType)->getCanonicalType ();
3664+ VarargsInfo varargsInfo =
3665+ emitBeginVarargs (SGF, loc, elementType->getCanonicalType (), arrayTy,
3666+ E->getNumElements (), {});
3667+
3668+ for (unsigned index : range (E->getNumElements ())) {
3669+ auto destAddr = varargsInfo.getBaseAddress ();
3670+ if (index != 0 ) {
3671+ SILValue indexValue = SGF.B .createIntegerLiteral (
3672+ loc, SILType::getBuiltinWordType (SGF.getASTContext ()), index);
3673+ destAddr = SGF.B .createIndexAddr (loc, destAddr, indexValue);
3674+ }
3675+ auto &destTL = varargsInfo.getBaseTypeLowering ();
3676+ // Use an invalid cleanup here because we're relying on the cleanup for
3677+ // the Array constructed inside emitBeginVarargs to destroy these values.
3678+ TemporaryInitialization init (destAddr, CleanupHandle::invalid ());
3679+ ArgumentSource (E->getElements ()[index])
3680+ .forwardInto (SGF, varargsInfo.getBaseAbstractionPattern (), &init,
3681+ destTL);
3682+ }
3683+
3684+ RValue arg (SGF, loc, arrayTy,
3685+ emitEndVarargs (SGF, loc, std::move (varargsInfo)));
3686+
3687+ // Add an argument label for init(arrayLiteral: T...) as the above tuple is of
3688+ // the form (T...) with no label.
3689+ assert (argLabels.size () == 1 && !argLabels[0 ].empty () &&
3690+ !isa<TupleType>(arg.getType ()));
3691+ Type newType = TupleType::get ({TupleTypeElt (arg.getType (), argLabels[0 ])},
3692+ SGF.getASTContext ());
3693+ arg.rewriteType (newType->getCanonicalType ());
3694+
3695+ // Call the builtin initializer.
3696+ return scope.popPreservingValue (SGF.emitApplyAllocatingInitializer (
3697+ loc, init, std::move (arg), E->getType (), C));
3698+ }
3699+
3700+ RValue RValueEmitter::visitDictionaryExpr (DictionaryExpr *E, SGFContext C) {
36563701 return visit (E->getSemanticExpr (), C);
36573702}
36583703
0 commit comments