@@ -6145,19 +6145,18 @@ static ManagedValue emitDynamicPartialApply(SILGenFunction &SGF,
6145
6145
return result;
6146
6146
}
6147
6147
6148
- RValue SILGenFunction::emitDynamicMemberRefExpr (DynamicMemberRefExpr *e ,
6149
- SGFContext c) {
6150
- // Emit the operand.
6151
- ManagedValue base = emitRValueAsSingleValue (e-> getBase ());
6148
+ RValue SILGenFunction::emitDynamicMemberRef (SILLocation loc, SILValue operand ,
6149
+ ConcreteDeclRef memberRef,
6150
+ CanType refTy, SGFContext C) {
6151
+ assert (refTy-> isOptional ());
6152
6152
6153
- SILValue operand = base.getValue ();
6154
- if (!e->getMember ().getDecl ()->isInstanceMember ()) {
6153
+ if (!memberRef.getDecl ()->isInstanceMember ()) {
6155
6154
auto metatype = operand->getType ().castTo <MetatypeType>();
6156
6155
assert (metatype->getRepresentation () == MetatypeRepresentation::Thick);
6157
6156
metatype = CanMetatypeType::get (metatype.getInstanceType (),
6158
6157
MetatypeRepresentation::ObjC);
6159
- operand = B.createThickToObjCMetatype (e, operand,
6160
- SILType::getPrimitiveObjectType (metatype));
6158
+ operand = B.createThickToObjCMetatype (
6159
+ loc, operand, SILType::getPrimitiveObjectType (metatype));
6161
6160
}
6162
6161
6163
6162
// Create the continuation block.
@@ -6169,39 +6168,34 @@ RValue SILGenFunction::emitDynamicMemberRefExpr(DynamicMemberRefExpr *e,
6169
6168
// Create the has-member block.
6170
6169
SILBasicBlock *hasMemberBB = createBasicBlock ();
6171
6170
6172
- // The continuation block
6173
- auto memberMethodTy = e->getType ()->getOptionalObjectType ();
6174
-
6175
- const TypeLowering &optTL = getTypeLowering (e->getType ());
6171
+ const TypeLowering &optTL = getTypeLowering (refTy);
6176
6172
auto loweredOptTy = optTL.getLoweredType ();
6177
6173
6178
- SILValue optTemp = emitTemporaryAllocation (e , loweredOptTy);
6174
+ SILValue optTemp = emitTemporaryAllocation (loc , loweredOptTy);
6179
6175
6180
6176
// Create the branch.
6181
6177
FuncDecl *memberFunc;
6182
- if (auto *VD = dyn_cast<VarDecl>(e-> getMember () .getDecl ())) {
6178
+ if (auto *VD = dyn_cast<VarDecl>(memberRef .getDecl ())) {
6183
6179
memberFunc = VD->getOpaqueAccessor (AccessorKind::Get);
6184
- // FIXME: Verify ExtInfo state is correct, not working by accident.
6185
- CanFunctionType::ExtInfo info;
6186
- memberMethodTy = FunctionType::get ({}, memberMethodTy, info);
6187
- } else
6188
- memberFunc = cast<FuncDecl>(e->getMember ().getDecl ());
6180
+ } else {
6181
+ memberFunc = cast<FuncDecl>(memberRef.getDecl ());
6182
+ }
6189
6183
auto member = SILDeclRef (memberFunc, SILDeclRef::Kind::Func)
6190
6184
.asForeign ();
6191
- B.createDynamicMethodBranch (e , operand, member, hasMemberBB, noMemberBB);
6185
+ B.createDynamicMethodBranch (loc , operand, member, hasMemberBB, noMemberBB);
6192
6186
6193
6187
// Create the has-member branch.
6194
6188
{
6195
6189
B.emitBlock (hasMemberBB);
6196
6190
6197
- FullExpr hasMemberScope (Cleanups, CleanupLocation (e ));
6191
+ FullExpr hasMemberScope (Cleanups, CleanupLocation (loc ));
6198
6192
6199
6193
// The argument to the has-member block is the uncurried method.
6200
- auto valueTy = e-> getType ()-> getCanonicalType () .getOptionalObjectType ();
6194
+ const CanType valueTy = refTy .getOptionalObjectType ();
6201
6195
CanFunctionType methodTy;
6202
6196
6203
6197
// For a computed variable, we want the getter.
6204
- if (isa<VarDecl>(e-> getMember () .getDecl ())) {
6198
+ if (isa<VarDecl>(memberRef .getDecl ())) {
6205
6199
// FIXME: Verify ExtInfo state is correct, not working by accident.
6206
6200
CanFunctionType::ExtInfo info;
6207
6201
methodTy = CanFunctionType::get ({}, valueTy, info);
@@ -6213,50 +6207,49 @@ RValue SILGenFunction::emitDynamicMemberRefExpr(DynamicMemberRefExpr *e,
6213
6207
// TODO: instead of building this and then potentially converting, we
6214
6208
// should just build a single thunk.
6215
6209
auto foreignMethodTy =
6216
- getPartialApplyOfDynamicMethodFormalType (SGM, member, e-> getMember () );
6210
+ getPartialApplyOfDynamicMethodFormalType (SGM, member, memberRef );
6217
6211
6218
6212
// FIXME: Verify ExtInfo state is correct, not working by accident.
6219
6213
CanFunctionType::ExtInfo info;
6220
6214
FunctionType::Param arg (operand->getType ().getASTType ());
6221
- auto memberFnTy =
6222
- CanFunctionType::get ({arg}, memberMethodTy->getCanonicalType (), info);
6215
+ auto memberFnTy = CanFunctionType::get ({arg}, methodTy, info);
6223
6216
6224
6217
auto loweredMethodTy = getDynamicMethodLoweredType (SGM.M , member,
6225
6218
memberFnTy);
6226
6219
SILValue memberArg =
6227
6220
hasMemberBB->createPhiArgument (loweredMethodTy, OwnershipKind::Owned);
6228
6221
6229
6222
// Create the result value.
6230
- Scope applyScope (Cleanups, CleanupLocation (e));
6231
- ManagedValue result =
6232
- emitDynamicPartialApply (*this , e, memberArg, operand,
6233
- foreignMethodTy, methodTy);
6223
+ Scope applyScope (Cleanups, CleanupLocation (loc));
6224
+ ManagedValue result = emitDynamicPartialApply (
6225
+ *this , loc, memberArg, operand, foreignMethodTy, methodTy);
6234
6226
6235
6227
RValue resultRV;
6236
- if (isa<VarDecl>(e-> getMember () .getDecl ())) {
6237
- resultRV = emitMonomorphicApply (e, result, {},
6238
- foreignMethodTy.getResult (), valueTy ,
6239
- ApplyOptions (), None, None);
6228
+ if (isa<VarDecl>(memberRef .getDecl ())) {
6229
+ resultRV =
6230
+ emitMonomorphicApply (loc, result, {}, foreignMethodTy.getResult (),
6231
+ valueTy, ApplyOptions (), None, None);
6240
6232
} else {
6241
- resultRV = RValue (*this , e , valueTy, result);
6233
+ resultRV = RValue (*this , loc , valueTy, result);
6242
6234
}
6243
6235
6244
6236
// Package up the result in an optional.
6245
- emitInjectOptionalValueInto (e, {e, std::move (resultRV)}, optTemp, optTL);
6237
+ emitInjectOptionalValueInto (loc, {loc, std::move (resultRV)}, optTemp,
6238
+ optTL);
6246
6239
6247
6240
applyScope.pop ();
6248
6241
// Branch to the continuation block.
6249
- B.createBranch (e , contBB);
6242
+ B.createBranch (loc , contBB);
6250
6243
}
6251
6244
6252
6245
// Create the no-member branch.
6253
6246
{
6254
6247
B.emitBlock (noMemberBB);
6255
6248
6256
- emitInjectOptionalNothingInto (e , optTemp, optTL);
6249
+ emitInjectOptionalNothingInto (loc , optTemp, optTL);
6257
6250
6258
6251
// Branch to the continuation block.
6259
- B.createBranch (e , contBB);
6252
+ B.createBranch (loc , contBB);
6260
6253
}
6261
6254
6262
6255
// Emit the continuation block.
@@ -6265,21 +6258,19 @@ RValue SILGenFunction::emitDynamicMemberRefExpr(DynamicMemberRefExpr *e,
6265
6258
// Package up the result.
6266
6259
auto optResult = optTemp;
6267
6260
if (optTL.isLoadable ())
6268
- optResult = optTL.emitLoad (B, e, optResult, LoadOwnershipQualifier::Take);
6269
- return RValue (*this , e, emitManagedRValueWithCleanup (optResult, optTL));
6261
+ optResult = optTL.emitLoad (B, loc, optResult, LoadOwnershipQualifier::Take);
6262
+ return RValue (*this , loc, refTy,
6263
+ emitManagedRValueWithCleanup (optResult, optTL));
6270
6264
}
6271
6265
6272
- RValue SILGenFunction::emitDynamicSubscriptExpr (DynamicSubscriptExpr *e,
6266
+ RValue
6267
+ SILGenFunction::emitDynamicSubscriptGetterApply (SILLocation loc,
6268
+ SILValue operand,
6269
+ ConcreteDeclRef subscriptRef,
6270
+ PreparedArguments &&indexArgs,
6271
+ CanType resultTy,
6273
6272
SGFContext c) {
6274
- // Emit the base operand.
6275
- ManagedValue managedBase = emitRValueAsSingleValue (e->getBase ());
6276
-
6277
- SILValue base = managedBase.getValue ();
6278
-
6279
- // Emit the index.
6280
- auto *indexExpr = e->getArgs ()->getUnaryExpr ();
6281
- assert (indexExpr);
6282
- RValue index = emitRValue (indexExpr);
6273
+ assert (resultTy->isOptional ());
6283
6274
6284
6275
// Create the continuation block.
6285
6276
SILBasicBlock *contBB = createBasicBlock ();
@@ -6290,72 +6281,77 @@ RValue SILGenFunction::emitDynamicSubscriptExpr(DynamicSubscriptExpr *e,
6290
6281
// Create the has-member block.
6291
6282
SILBasicBlock *hasMemberBB = createBasicBlock ();
6292
6283
6293
- const TypeLowering &optTL = getTypeLowering (e->getType ());
6294
- auto loweredOptTy = optTL.getLoweredType ();
6295
- SILValue optTemp = emitTemporaryAllocation (e, loweredOptTy);
6284
+ const TypeLowering &optTL = getTypeLowering (resultTy);
6285
+ const SILValue optTemp = emitTemporaryAllocation (loc, optTL.getLoweredType ());
6296
6286
6297
6287
// Create the branch.
6298
- auto subscriptDecl = cast<SubscriptDecl>(e-> getMember () .getDecl ());
6288
+ auto * subscriptDecl = cast<SubscriptDecl>(subscriptRef .getDecl ());
6299
6289
auto member = SILDeclRef (subscriptDecl->getOpaqueAccessor (AccessorKind::Get),
6300
6290
SILDeclRef::Kind::Func)
6301
6291
.asForeign ();
6302
- B.createDynamicMethodBranch (e, base , member, hasMemberBB, noMemberBB);
6292
+ B.createDynamicMethodBranch (loc, operand , member, hasMemberBB, noMemberBB);
6303
6293
6304
6294
// Create the has-member branch.
6305
6295
{
6306
6296
B.emitBlock (hasMemberBB);
6307
6297
6308
- FullExpr hasMemberScope (Cleanups, CleanupLocation (e ));
6298
+ FullExpr hasMemberScope (Cleanups, CleanupLocation (loc ));
6309
6299
6310
6300
// The argument to the has-member block is the uncurried method.
6311
6301
// Build the substituted getter type from the AST nodes.
6312
- auto valueTy = e-> getType ()-> getCanonicalType () .getOptionalObjectType ();
6302
+ const CanType valueTy = resultTy .getOptionalObjectType ();
6313
6303
6314
- // Objective-C subscripts only ever have a single parameter.
6315
- //
6316
6304
// FIXME: Verify ExtInfo state is correct, not working by accident.
6317
6305
CanFunctionType::ExtInfo methodInfo;
6318
- FunctionType::Param indexArg (indexExpr-> getType ()-> getCanonicalType ());
6319
- auto methodTy = CanFunctionType::get ({indexArg} , valueTy, methodInfo);
6306
+ const auto methodTy =
6307
+ CanFunctionType::get (indexArgs. getParams () , valueTy, methodInfo);
6320
6308
auto foreignMethodTy =
6321
- getPartialApplyOfDynamicMethodFormalType (SGM, member, e-> getMember () );
6309
+ getPartialApplyOfDynamicMethodFormalType (SGM, member, subscriptRef );
6322
6310
6323
6311
// FIXME: Verify ExtInfo state is correct, not working by accident.
6324
6312
CanFunctionType::ExtInfo functionInfo;
6325
- FunctionType::Param baseArg (base ->getType ().getASTType ());
6313
+ FunctionType::Param baseArg (operand ->getType ().getASTType ());
6326
6314
auto functionTy = CanFunctionType::get ({baseArg}, methodTy, functionInfo);
6327
6315
auto loweredMethodTy = getDynamicMethodLoweredType (SGM.M , member,
6328
6316
functionTy);
6329
6317
SILValue memberArg =
6330
6318
hasMemberBB->createPhiArgument (loweredMethodTy, OwnershipKind::Owned);
6331
6319
// Emit the application of 'self'.
6332
- Scope applyScope (Cleanups, CleanupLocation (e));
6333
- ManagedValue result = emitDynamicPartialApply (*this , e, memberArg, base,
6334
- foreignMethodTy, methodTy);
6335
- // Emit the index.
6336
- llvm::SmallVector<ManagedValue, 2 > indexArgs;
6337
- std::move (index).getAll (indexArgs);
6338
-
6339
- auto resultRV = emitMonomorphicApply (e, result, indexArgs,
6320
+ Scope applyScope (Cleanups, CleanupLocation (loc));
6321
+ ManagedValue result = emitDynamicPartialApply (
6322
+ *this , loc, memberArg, operand, foreignMethodTy, methodTy);
6323
+
6324
+ // Collect the index values for application.
6325
+ llvm::SmallVector<ManagedValue, 2 > indexValues;
6326
+ for (auto &source : std::move (indexArgs).getSources ()) {
6327
+ // @objc subscripts cannot have 'inout' indices.
6328
+ RValue rVal = std::move (source).asKnownRValue (*this );
6329
+
6330
+ // @objc subscripts cannot have tuple indices.
6331
+ indexValues.push_back (std::move (rVal).getScalarValue ());
6332
+ }
6333
+
6334
+ auto resultRV = emitMonomorphicApply (loc, result, indexValues,
6340
6335
foreignMethodTy.getResult (), valueTy,
6341
6336
ApplyOptions (), None, None);
6342
6337
6343
6338
// Package up the result in an optional.
6344
- emitInjectOptionalValueInto (e, {e, std::move (resultRV)}, optTemp, optTL);
6339
+ emitInjectOptionalValueInto (loc, {loc, std::move (resultRV)}, optTemp,
6340
+ optTL);
6345
6341
6346
6342
applyScope.pop ();
6347
6343
// Branch to the continuation block.
6348
- B.createBranch (e , contBB);
6344
+ B.createBranch (loc , contBB);
6349
6345
}
6350
6346
6351
6347
// Create the no-member branch.
6352
6348
{
6353
6349
B.emitBlock (noMemberBB);
6354
6350
6355
- emitInjectOptionalNothingInto (e , optTemp, optTL);
6351
+ emitInjectOptionalNothingInto (loc , optTemp, optTL);
6356
6352
6357
6353
// Branch to the continuation block.
6358
- B.createBranch (e , contBB);
6354
+ B.createBranch (loc , contBB);
6359
6355
}
6360
6356
6361
6357
// Emit the continuation block.
@@ -6364,8 +6360,9 @@ RValue SILGenFunction::emitDynamicSubscriptExpr(DynamicSubscriptExpr *e,
6364
6360
// Package up the result.
6365
6361
auto optResult = optTemp;
6366
6362
if (optTL.isLoadable ())
6367
- optResult = optTL.emitLoad (B, e, optResult, LoadOwnershipQualifier::Take);
6368
- return RValue (*this , e, emitManagedRValueWithCleanup (optResult, optTL));
6363
+ optResult = optTL.emitLoad (B, loc, optResult, LoadOwnershipQualifier::Take);
6364
+ return RValue (*this , loc, resultTy,
6365
+ emitManagedRValueWithCleanup (optResult, optTL));
6369
6366
}
6370
6367
6371
6368
SmallVector<ManagedValue, 4 > SILGenFunction::emitKeyPathSubscriptOperands (
0 commit comments