/
LocalRewriter_ForEachStatement.cs
1162 lines (996 loc) · 56.7 KB
/
LocalRewriter_ForEachStatement.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.PooledObjects;
namespace Microsoft.CodeAnalysis.CSharp
{
internal partial class LocalRewriter
{
/// <summary>
/// This is the entry point for foreach-loop lowering. It delegates to
/// RewriteEnumeratorForEachStatement
/// RewriteSingleDimensionalArrayForEachStatement
/// RewriteMultiDimensionalArrayForEachStatement
/// CanRewriteForEachAsFor
/// </summary>
/// <remarks>
/// We are diverging from the C# 4 spec (and Dev10) to follow the C# 5 spec.
/// The iteration variable will be declared *inside* each loop iteration,
/// rather than outside the loop.
/// </remarks>
public override BoundNode VisitForEachStatement(BoundForEachStatement node)
{
// No point in performing this lowering if the node won't be emitted.
if (node.HasErrors)
{
return node;
}
BoundExpression collectionExpression = GetUnconvertedCollectionExpression(node);
TypeSymbol? nodeExpressionType = collectionExpression.Type;
Debug.Assert(nodeExpressionType is { });
if (nodeExpressionType.Kind == SymbolKind.ArrayType)
{
ArrayTypeSymbol arrayType = (ArrayTypeSymbol)nodeExpressionType;
if (arrayType.IsSZArray)
{
return RewriteSingleDimensionalArrayForEachStatement(node);
}
else
{
return RewriteMultiDimensionalArrayForEachStatement(node);
}
}
else if (node.AwaitOpt is null && CanRewriteForEachAsFor(node.Syntax, nodeExpressionType, out var indexerGet, out var lengthGetter))
{
return RewriteForEachStatementAsFor(node, indexerGet, lengthGetter);
}
else
{
return RewriteEnumeratorForEachStatement(node);
}
}
private bool CanRewriteForEachAsFor(SyntaxNode forEachSyntax, TypeSymbol nodeExpressionType, [NotNullWhen(true)] out MethodSymbol? indexerGet, [NotNullWhen(true)] out MethodSymbol? lengthGet)
{
lengthGet = indexerGet = null;
var origDefinition = nodeExpressionType.OriginalDefinition;
if (origDefinition.SpecialType == SpecialType.System_String)
{
lengthGet = UnsafeGetSpecialTypeMethod(forEachSyntax, SpecialMember.System_String__Length);
indexerGet = UnsafeGetSpecialTypeMethod(forEachSyntax, SpecialMember.System_String__Chars);
}
else if ((object)origDefinition == this._compilation.GetWellKnownType(WellKnownType.System_Span_T))
{
var spanType = (NamedTypeSymbol)nodeExpressionType;
lengthGet = (MethodSymbol?)_factory.WellKnownMember(WellKnownMember.System_Span_T__get_Length, isOptional: true)?.SymbolAsMember(spanType);
indexerGet = (MethodSymbol?)_factory.WellKnownMember(WellKnownMember.System_Span_T__get_Item, isOptional: true)?.SymbolAsMember(spanType);
}
else if ((object)origDefinition == this._compilation.GetWellKnownType(WellKnownType.System_ReadOnlySpan_T))
{
var spanType = (NamedTypeSymbol)nodeExpressionType;
lengthGet = (MethodSymbol?)_factory.WellKnownMember(WellKnownMember.System_ReadOnlySpan_T__get_Length, isOptional: true)?.SymbolAsMember(spanType);
indexerGet = (MethodSymbol?)_factory.WellKnownMember(WellKnownMember.System_ReadOnlySpan_T__get_Item, isOptional: true)?.SymbolAsMember(spanType);
}
return lengthGet is { } && indexerGet is { };
}
/// <summary>
/// Lower a foreach loop that will enumerate a collection using an enumerator.
///
/// <![CDATA[
/// E e = ((C)(x)).GetEnumerator() OR ((C)(x)).GetAsyncEnumerator()
/// try {
/// while (e.MoveNext()) OR while (await e.MoveNextAsync())
/// {
/// V v = (V)(T)e.Current; -OR- (D1 d1, ...) = (V)(T)e.Current;
/// // body
/// }
/// }
/// finally {
/// // clean up e
/// }
/// ]]>
/// </summary>
private BoundStatement RewriteEnumeratorForEachStatement(BoundForEachStatement node)
{
var forEachSyntax = (CommonForEachStatementSyntax)node.Syntax;
bool isAsync = node.AwaitOpt != null;
ForEachEnumeratorInfo? enumeratorInfo = node.EnumeratorInfoOpt;
Debug.Assert(enumeratorInfo != null);
BoundExpression collectionExpression = GetUnconvertedCollectionExpression(node);
BoundExpression rewrittenExpression = VisitExpression(collectionExpression);
BoundStatement? rewrittenBody = VisitStatement(node.Body);
Debug.Assert(rewrittenBody is { });
MethodArgumentInfo getEnumeratorInfo = enumeratorInfo.GetEnumeratorInfo;
TypeSymbol enumeratorType = getEnumeratorInfo.Method.ReturnType;
TypeSymbol elementType = enumeratorInfo.ElementType;
// E e
LocalSymbol enumeratorVar = _factory.SynthesizedLocal(enumeratorType, syntax: forEachSyntax, kind: SynthesizedLocalKind.ForEachEnumerator);
// Reference to e.
BoundLocal boundEnumeratorVar = MakeBoundLocal(forEachSyntax, enumeratorVar, enumeratorType);
var receiver = ConvertReceiverForInvocation(forEachSyntax, rewrittenExpression, getEnumeratorInfo.Method, enumeratorInfo.CollectionConversion, enumeratorInfo.CollectionType);
// If the GetEnumerator call is an extension method, then the first argument is the receiver. We want to replace
// the first argument with our converted receiver and pass null as the receiver instead.
if (getEnumeratorInfo.Method.IsExtensionMethod)
{
var builder = ArrayBuilder<BoundExpression>.GetInstance(getEnumeratorInfo.Arguments.Length);
builder.Add(receiver);
builder.AddRange(getEnumeratorInfo.Arguments, 1, getEnumeratorInfo.Arguments.Length - 1);
getEnumeratorInfo = getEnumeratorInfo with { Arguments = builder.ToImmutableAndFree() };
receiver = null;
}
// ((C)(x)).GetEnumerator(); OR (x).GetEnumerator(); OR async variants (which fill-in arguments for optional parameters)
BoundExpression enumeratorVarInitValue = SynthesizeCall(getEnumeratorInfo, forEachSyntax, receiver,
allowExtensionAndOptionalParameters: isAsync || getEnumeratorInfo.Method.IsExtensionMethod,
// C# 8 shipped allowing the CancellationToken of `IAsyncEnumerable.GetAsyncEnumerator` to be non-optional.
// https://github.com/dotnet/roslyn/issues/50182 tracks making this an error and breaking the scenario.
assertParametersAreOptional: false);
// E e = ((C)(x)).GetEnumerator();
BoundStatement enumeratorVarDecl = MakeLocalDeclaration(forEachSyntax, enumeratorVar, enumeratorVarInitValue);
InstrumentForEachStatementCollectionVarDeclaration(node, ref enumeratorVarDecl);
// (V)(T)e.Current
BoundExpression iterationVarAssignValue = MakeConversionNode(
syntax: forEachSyntax,
rewrittenOperand: MakeConversionNode(
syntax: forEachSyntax,
rewrittenOperand: BoundCall.Synthesized(
syntax: forEachSyntax,
receiverOpt: boundEnumeratorVar,
method: enumeratorInfo.CurrentPropertyGetter),
conversion: enumeratorInfo.CurrentConversion,
rewrittenType: elementType,
@checked: node.Checked),
conversion: node.ElementConversion,
rewrittenType: node.IterationVariableType.Type,
@checked: node.Checked);
// V v = (V)(T)e.Current; -OR- (D1 d1, ...) = (V)(T)e.Current;
ImmutableArray<LocalSymbol> iterationVariables = node.IterationVariables;
BoundStatement iterationVarDecl = LocalOrDeconstructionDeclaration(node, iterationVariables, iterationVarAssignValue);
InstrumentForEachStatementIterationVarDeclaration(node, ref iterationVarDecl);
// while (e.MoveNext()) -OR- while (await e.MoveNextAsync())
// {
// V v = (V)(T)e.Current; -OR- (D1 d1, ...) = (V)(T)e.Current;
// /* node.Body */
// }
var rewrittenBodyBlock = CreateBlockDeclaringIterationVariables(iterationVariables, iterationVarDecl, rewrittenBody, forEachSyntax);
BoundExpression rewrittenCondition = SynthesizeCall(
methodArgumentInfo: enumeratorInfo.MoveNextInfo,
syntax: forEachSyntax,
receiver: boundEnumeratorVar,
allowExtensionAndOptionalParameters: isAsync);
if (isAsync)
{
Debug.Assert(node.AwaitOpt is { GetResult: { } });
rewrittenCondition = RewriteAwaitExpression(forEachSyntax, rewrittenCondition, node.AwaitOpt, node.AwaitOpt.GetResult.ReturnType, used: true);
}
BoundStatement whileLoop = RewriteWhileStatement(
loop: node,
rewrittenCondition,
rewrittenBody: rewrittenBodyBlock,
breakLabel: node.BreakLabel,
continueLabel: node.ContinueLabel,
hasErrors: false);
BoundStatement result;
if (enumeratorInfo.NeedsDisposal)
{
BoundStatement tryFinally = WrapWithTryFinallyDispose(forEachSyntax, enumeratorInfo, enumeratorType, boundEnumeratorVar, whileLoop);
// E e = ((C)(x)).GetEnumerator();
// try {
// /* as above */
result = new BoundBlock(
syntax: forEachSyntax,
locals: ImmutableArray.Create(enumeratorVar),
statements: ImmutableArray.Create<BoundStatement>(enumeratorVarDecl, tryFinally));
}
else
{
// E e = ((C)(x)).GetEnumerator();
// while (e.MoveNext()) {
// V v = (V)(T)e.Current; -OR- (D1 d1, ...) = (V)(T)e.Current;
// /* loop body */
// }
result = new BoundBlock(
syntax: forEachSyntax,
locals: ImmutableArray.Create(enumeratorVar),
statements: ImmutableArray.Create<BoundStatement>(enumeratorVarDecl, whileLoop));
}
InstrumentForEachStatement(node, ref result);
return result;
}
private bool TryGetDisposeMethod(CommonForEachStatementSyntax forEachSyntax, ForEachEnumeratorInfo enumeratorInfo, out MethodSymbol disposeMethod)
{
if (enumeratorInfo.IsAsync)
{
disposeMethod = (MethodSymbol)Binder.GetWellKnownTypeMember(_compilation, WellKnownMember.System_IAsyncDisposable__DisposeAsync, _diagnostics, syntax: forEachSyntax);
return (object)disposeMethod != null;
}
return Binder.TryGetSpecialTypeMember(_compilation, SpecialMember.System_IDisposable__Dispose, forEachSyntax, _diagnostics, out disposeMethod);
}
/// <summary>
/// There are three possible cases where we need disposal:
/// - pattern-based disposal (we have a Dispose/DisposeAsync method)
/// - interface-based disposal (the enumerator type converts to IDisposable/IAsyncDisposable)
/// - we need to do a runtime check for IDisposable
/// </summary>
private BoundStatement WrapWithTryFinallyDispose(
CommonForEachStatementSyntax forEachSyntax,
ForEachEnumeratorInfo enumeratorInfo,
TypeSymbol enumeratorType,
BoundLocal boundEnumeratorVar,
BoundStatement rewrittenBody)
{
Debug.Assert(enumeratorInfo.NeedsDisposal);
NamedTypeSymbol? idisposableTypeSymbol = null;
bool isImplicit = false;
MethodSymbol? disposeMethod = enumeratorInfo.PatternDisposeInfo?.Method; // pattern-based
if (disposeMethod is null)
{
TryGetDisposeMethod(forEachSyntax, enumeratorInfo, out disposeMethod); // interface-based
// This is a temporary workaround for https://github.com/dotnet/roslyn/issues/39948
if (disposeMethod is null)
{
return rewrittenBody;
}
idisposableTypeSymbol = disposeMethod.ContainingType;
Debug.Assert(_factory.CurrentFunction is { });
var conversions = new TypeConversions(_factory.CurrentFunction.ContainingAssembly.CorLibrary);
CompoundUseSiteInfo<AssemblySymbol> useSiteInfo = GetNewCompoundUseSiteInfo();
isImplicit = conversions.ClassifyImplicitConversionFromType(enumeratorType, idisposableTypeSymbol, ref useSiteInfo).IsImplicit;
_diagnostics.Add(forEachSyntax, useSiteInfo);
}
Binder.ReportDiagnosticsIfObsolete(_diagnostics, disposeMethod, forEachSyntax,
hasBaseReceiver: false,
containingMember: _factory.CurrentFunction,
containingType: _factory.CurrentType,
location: enumeratorInfo.Location);
BoundBlock finallyBlockOpt;
if (isImplicit || !(enumeratorInfo.PatternDisposeInfo is null))
{
Conversion receiverConversion = enumeratorType.IsStructType() ?
Conversion.Boxing :
Conversion.ImplicitReference;
BoundExpression receiver;
BoundExpression disposeCall;
var disposeInfo = enumeratorInfo.PatternDisposeInfo;
if (disposeInfo is null)
{
Debug.Assert(idisposableTypeSymbol is { });
disposeInfo = MethodArgumentInfo.CreateParameterlessMethod(disposeMethod);
receiver = ConvertReceiverForInvocation(forEachSyntax, boundEnumeratorVar, disposeMethod, receiverConversion, idisposableTypeSymbol);
}
else
{
receiver = boundEnumeratorVar;
}
// ((IDisposable)e).Dispose() or e.Dispose() or await ((IAsyncDisposable)e).DisposeAsync() or await e.DisposeAsync()
disposeCall = MakeCallWithNoExplicitArgument(disposeInfo, forEachSyntax, receiver);
BoundStatement disposeCallStatement;
var disposeAwaitableInfoOpt = enumeratorInfo.DisposeAwaitableInfo;
if (disposeAwaitableInfoOpt != null)
{
// await /* disposeCall */
disposeCallStatement = WrapWithAwait(forEachSyntax, disposeCall, disposeAwaitableInfoOpt);
_sawAwaitInExceptionHandler = true;
}
else
{
// ((IDisposable)e).Dispose(); or e.Dispose();
disposeCallStatement = new BoundExpressionStatement(forEachSyntax, disposeCall);
}
BoundStatement alwaysOrMaybeDisposeStmt;
if (enumeratorType.IsValueType)
{
// No way for the struct to be nullable and disposable.
Debug.Assert(enumeratorType.OriginalDefinition.SpecialType != SpecialType.System_Nullable_T);
// For non-nullable structs, no null check is required.
alwaysOrMaybeDisposeStmt = disposeCallStatement;
}
else
{
// NB: cast to object missing from spec. Needed to ignore user-defined operators and box type parameters.
// if ((object)e != null) ((IDisposable)e).Dispose();
alwaysOrMaybeDisposeStmt = RewriteIfStatement(
syntax: forEachSyntax,
rewrittenCondition: new BoundBinaryOperator(forEachSyntax,
operatorKind: BinaryOperatorKind.NotEqual,
left: MakeConversionNode(
syntax: forEachSyntax,
rewrittenOperand: boundEnumeratorVar,
conversion: enumeratorInfo.EnumeratorConversion,
rewrittenType: _compilation.GetSpecialType(SpecialType.System_Object),
@checked: false),
right: MakeLiteral(forEachSyntax,
constantValue: ConstantValue.Null,
type: null),
constantValueOpt: null,
methodOpt: null,
resultKind: LookupResultKind.Viable,
type: _compilation.GetSpecialType(SpecialType.System_Boolean)),
rewrittenConsequence: disposeCallStatement,
rewrittenAlternativeOpt: null,
hasErrors: false);
}
finallyBlockOpt = new BoundBlock(forEachSyntax,
locals: ImmutableArray<LocalSymbol>.Empty,
statements: ImmutableArray.Create(alwaysOrMaybeDisposeStmt));
}
else
{
// If we couldn't find either pattern-based or interface-based disposal, and the enumerator type isn't sealed,
// and the loop isn't async, then we include a runtime check.
Debug.Assert(!enumeratorType.IsSealed);
Debug.Assert(!enumeratorInfo.IsAsync);
Debug.Assert(idisposableTypeSymbol is { });
Debug.Assert(disposeMethod is { });
// IDisposable d
LocalSymbol disposableVar = _factory.SynthesizedLocal(idisposableTypeSymbol);
// Reference to d.
BoundLocal boundDisposableVar = MakeBoundLocal(forEachSyntax, disposableVar, idisposableTypeSymbol);
BoundTypeExpression boundIDisposableTypeExpr = new BoundTypeExpression(forEachSyntax,
aliasOpt: null,
type: idisposableTypeSymbol);
// e as IDisposable
BoundExpression disposableVarInitValue = new BoundAsOperator(forEachSyntax,
operand: boundEnumeratorVar,
targetType: boundIDisposableTypeExpr,
conversion: Conversion.ExplicitReference, // Explicit so the emitter won't optimize it away.
type: idisposableTypeSymbol);
// IDisposable d = e as IDisposable;
BoundStatement disposableVarDecl = MakeLocalDeclaration(forEachSyntax, disposableVar, disposableVarInitValue);
// d.Dispose()
BoundExpression disposeCall = BoundCall.Synthesized(syntax: forEachSyntax, receiverOpt: boundDisposableVar, method: disposeMethod);
BoundStatement disposeCallStatement = new BoundExpressionStatement(forEachSyntax, expression: disposeCall);
// if (d != null) d.Dispose();
BoundStatement ifStmt = RewriteIfStatement(
syntax: forEachSyntax,
rewrittenCondition: new BoundBinaryOperator(forEachSyntax,
operatorKind: BinaryOperatorKind.NotEqual, // reference equality
left: boundDisposableVar,
right: MakeLiteral(forEachSyntax, constantValue: ConstantValue.Null, type: null),
constantValueOpt: null,
methodOpt: null,
resultKind: LookupResultKind.Viable,
type: _compilation.GetSpecialType(SpecialType.System_Boolean)),
rewrittenConsequence: disposeCallStatement,
rewrittenAlternativeOpt: null,
hasErrors: false);
// IDisposable d = e as IDisposable;
// if (d != null) d.Dispose();
finallyBlockOpt = new BoundBlock(forEachSyntax,
locals: ImmutableArray.Create(disposableVar),
statements: ImmutableArray.Create(disposableVarDecl, ifStmt));
}
// try {
// while (e.MoveNext()) {
// V v = (V)(T)e.Current; -OR- (D1 d1, ...) = (V)(T)e.Current;
// /* loop body */
// }
// }
// finally {
// /* dispose of e */
// }
BoundStatement tryFinally = new BoundTryStatement(forEachSyntax,
tryBlock: new BoundBlock(forEachSyntax,
locals: ImmutableArray<LocalSymbol>.Empty,
statements: ImmutableArray.Create<BoundStatement>(rewrittenBody)),
catchBlocks: ImmutableArray<BoundCatchBlock>.Empty,
finallyBlockOpt: finallyBlockOpt);
return tryFinally;
}
/// <summary>
/// Produce:
/// await /* disposeCall */;
/// </summary>
private BoundStatement WrapWithAwait(CommonForEachStatementSyntax forEachSyntax, BoundExpression disposeCall, BoundAwaitableInfo disposeAwaitableInfoOpt)
{
TypeSymbol awaitExpressionType = disposeAwaitableInfoOpt.GetResult?.ReturnType ?? _compilation.DynamicType;
var awaitExpr = RewriteAwaitExpression(forEachSyntax, disposeCall, disposeAwaitableInfoOpt, awaitExpressionType, used: false);
return new BoundExpressionStatement(forEachSyntax, awaitExpr);
}
/// <summary>
/// Optionally apply a conversion to the receiver.
///
/// If the receiver is of struct type and the method is an interface method, then skip the conversion.
/// When we call the interface method directly - the code generator will detect it and generate a
/// constrained virtual call.
/// </summary>
/// <param name="syntax">A syntax node to attach to the synthesized bound node.</param>
/// <param name="receiver">Receiver of method call.</param>
/// <param name="method">Method to invoke.</param>
/// <param name="receiverConversion">Conversion to be applied to the receiver if not calling an interface method on a struct.</param>
/// <param name="convertedReceiverType">Type of the receiver after applying the conversion.</param>
private BoundExpression ConvertReceiverForInvocation(CSharpSyntaxNode syntax, BoundExpression receiver, MethodSymbol method, Conversion receiverConversion, TypeSymbol convertedReceiverType)
{
Debug.Assert(receiver.Type is { });
if (!receiver.Type.IsReferenceType && method.ContainingType.IsInterface)
{
Debug.Assert(receiverConversion.IsImplicit && !receiverConversion.IsUserDefined);
// NOTE: The spec says that disposing of a struct enumerator won't cause any
// unnecessary boxing to occur. However, Dev10 extends this improvement to the
// GetEnumerator call as well.
// We're going to let the emitter take care of avoiding the extra boxing.
// When it sees an interface call to a struct, it will generate a constrained
// virtual call, which will skip boxing, if possible.
// CONSIDER: In cases where the struct implicitly implements the interface method
// (i.e. with a public method), we could save a few bytes of IL by creating a
// BoundCall to the struct method rather than the interface method (so that the
// emitter wouldn't need to create a constrained virtual call). It is not clear
// what effect this would have on back compat.
// NOTE: This call does not correspond to anything that can be written in C# source.
// We're invoking the interface method directly on the struct (which may have a private
// explicit implementation). The code generator knows how to handle it though.
// receiver.InterfaceMethod()
}
else
{
// ((Interface)receiver).InterfaceMethod()
Debug.Assert(!receiverConversion.IsNumeric);
receiver = MakeConversionNode(
syntax: syntax,
rewrittenOperand: receiver,
conversion: receiverConversion,
@checked: false,
rewrittenType: convertedReceiverType);
}
return receiver;
}
private BoundExpression SynthesizeCall(MethodArgumentInfo methodArgumentInfo, CSharpSyntaxNode syntax, BoundExpression? receiver, bool allowExtensionAndOptionalParameters, bool assertParametersAreOptional = true)
{
if (allowExtensionAndOptionalParameters)
{
// Generate a call with zero explicit arguments, but with implicit arguments for optional and params parameters.
return MakeCallWithNoExplicitArgument(methodArgumentInfo, syntax, receiver, assertParametersAreOptional);
}
// Generate a call with literally zero arguments
Debug.Assert(methodArgumentInfo.Arguments.IsEmpty);
return BoundCall.Synthesized(syntax, receiver, methodArgumentInfo.Method, arguments: ImmutableArray<BoundExpression>.Empty);
}
/// <summary>
/// Lower a foreach loop that will enumerate a collection via indexing.
///
/// <![CDATA[
///
/// Indexable a = x;
/// for (int p = 0; p < a.Length; p = p + 1) {
/// V v = (V)a[p]; /* OR */ (D1 d1, ...) = (V)a[p];
/// // body
/// }
///
/// ]]>
/// </summary>
/// <remarks>
/// NOTE: We're assuming that sequence points have already been generated.
/// Otherwise, lowering to for-loops would generated spurious ones.
/// </remarks>
private BoundStatement RewriteForEachStatementAsFor(BoundForEachStatement node, MethodSymbol indexerGet, MethodSymbol lengthGet)
{
var forEachSyntax = (CommonForEachStatementSyntax)node.Syntax;
BoundExpression collectionExpression = GetUnconvertedCollectionExpression(node);
NamedTypeSymbol? collectionType = (NamedTypeSymbol?)collectionExpression.Type;
Debug.Assert(collectionType is { });
TypeSymbol intType = _compilation.GetSpecialType(SpecialType.System_Int32);
TypeSymbol boolType = _compilation.GetSpecialType(SpecialType.System_Boolean);
BoundExpression rewrittenExpression = VisitExpression(collectionExpression);
BoundStatement? rewrittenBody = VisitStatement(node.Body);
Debug.Assert(rewrittenBody is { });
// Collection a
LocalSymbol collectionTemp = _factory.SynthesizedLocal(collectionType, forEachSyntax, kind: SynthesizedLocalKind.ForEachArray);
// Collection a = /*node.Expression*/;
BoundStatement arrayVarDecl = MakeLocalDeclaration(forEachSyntax, collectionTemp, rewrittenExpression);
InstrumentForEachStatementCollectionVarDeclaration(node, ref arrayVarDecl);
// Reference to a.
BoundLocal boundArrayVar = MakeBoundLocal(forEachSyntax, collectionTemp, collectionType);
// int p
LocalSymbol positionVar = _factory.SynthesizedLocal(intType, syntax: forEachSyntax, kind: SynthesizedLocalKind.ForEachArrayIndex);
// Reference to p.
BoundLocal boundPositionVar = MakeBoundLocal(forEachSyntax, positionVar, intType);
// int p = 0;
BoundStatement positionVarDecl = MakeLocalDeclaration(forEachSyntax, positionVar,
MakeLiteral(forEachSyntax, ConstantValue.Default(SpecialType.System_Int32), intType));
// (V)a[p]
BoundExpression iterationVarInitValue = MakeConversionNode(
syntax: forEachSyntax,
rewrittenOperand: BoundCall.Synthesized(
syntax: forEachSyntax,
receiverOpt: boundArrayVar,
indexerGet,
boundPositionVar),
conversion: node.ElementConversion,
rewrittenType: node.IterationVariableType.Type,
@checked: node.Checked);
// V v = (V)a[p]; /* OR */ (D1 d1, ...) = (V)a[p];
ImmutableArray<LocalSymbol> iterationVariables = node.IterationVariables;
BoundStatement iterationVariableDecl = LocalOrDeconstructionDeclaration(node, iterationVariables, iterationVarInitValue);
InstrumentForEachStatementIterationVarDeclaration(node, ref iterationVariableDecl);
BoundStatement initializer = new BoundStatementList(forEachSyntax,
statements: ImmutableArray.Create<BoundStatement>(arrayVarDecl, positionVarDecl));
// a.Length
BoundExpression arrayLength = BoundCall.Synthesized(
syntax: forEachSyntax,
receiverOpt: boundArrayVar,
lengthGet);
// p < a.Length
BoundExpression exitCondition = new BoundBinaryOperator(
syntax: forEachSyntax,
operatorKind: BinaryOperatorKind.IntLessThan,
left: boundPositionVar,
right: arrayLength,
constantValueOpt: null,
methodOpt: null,
resultKind: LookupResultKind.Viable,
type: boolType);
// p = p + 1;
BoundStatement positionIncrement = MakePositionIncrement(forEachSyntax, boundPositionVar, intType);
// {
// V v = (V)a[p]; /* OR */ (D1 d1, ...) = (V)a[p];
// /*node.Body*/
// }
BoundStatement loopBody = CreateBlockDeclaringIterationVariables(iterationVariables, iterationVariableDecl, rewrittenBody, forEachSyntax);
// for (Collection a = /*node.Expression*/, int p = 0; p < a.Length; p = p + 1) {
// V v = (V)a[p]; /* OR */ (D1 d1, ...) = (V)a[p];
// /*node.Body*/
// }
BoundStatement result = RewriteForStatementWithoutInnerLocals(
original: node,
outerLocals: ImmutableArray.Create<LocalSymbol>(collectionTemp, positionVar),
rewrittenInitializer: initializer,
rewrittenCondition: exitCondition,
rewrittenIncrement: positionIncrement,
rewrittenBody: loopBody,
breakLabel: node.BreakLabel,
continueLabel: node.ContinueLabel,
hasErrors: node.HasErrors);
InstrumentForEachStatement(node, ref result);
return result;
}
/// <summary>
/// Takes the expression for the current value of the iteration variable and either
/// (1) assigns it into a local, or
/// (2) deconstructs it into multiple locals (if there is a deconstruct step).
///
/// Produces <c>V v = /* expression */</c> or <c>(D1 d1, ...) = /* expression */</c>.
/// </summary>
private BoundStatement LocalOrDeconstructionDeclaration(
BoundForEachStatement forEachBound,
ImmutableArray<LocalSymbol> iterationVariables,
BoundExpression iterationVarValue)
{
var forEachSyntax = (CommonForEachStatementSyntax)forEachBound.Syntax;
BoundStatement iterationVarDecl;
BoundForEachDeconstructStep? deconstruction = forEachBound.DeconstructionOpt;
if (deconstruction == null)
{
// V v = /* expression */
Debug.Assert(iterationVariables.Length == 1);
iterationVarDecl = MakeLocalDeclaration(forEachSyntax, iterationVariables[0], iterationVarValue);
}
else
{
// (D1 d1, ...) = /* expression */
var assignment = deconstruction.DeconstructionAssignment;
AddPlaceholderReplacement(deconstruction.TargetPlaceholder, iterationVarValue);
BoundExpression loweredAssignment = VisitExpression(assignment);
iterationVarDecl = new BoundExpressionStatement(assignment.Syntax, loweredAssignment);
RemovePlaceholderReplacement(deconstruction.TargetPlaceholder);
}
return iterationVarDecl;
}
private static BoundBlock CreateBlockDeclaringIterationVariables(
ImmutableArray<LocalSymbol> iterationVariables,
BoundStatement iteratorVariableInitialization,
BoundStatement rewrittenBody,
CommonForEachStatementSyntax forEachSyntax)
{
// The scope of the iteration variable is the embedded statement syntax.
// However consider the following foreach statement:
//
// foreach (int x in ...) { int y = ...; F(() => x); F(() => y));
//
// We currently generate 2 closures. One containing variable x, the other variable y.
// The EnC source mapping infrastructure requires each closure within a method body
// to have a unique syntax offset. Hence we associate the bound block declaring the
// iteration variable with the foreach statement, not the embedded statement.
return new BoundBlock(
forEachSyntax,
locals: iterationVariables,
statements: ImmutableArray.Create(iteratorVariableInitialization, rewrittenBody));
}
private static BoundBlock CreateBlockDeclaringIterationVariables(
ImmutableArray<LocalSymbol> iterationVariables,
BoundStatement iteratorVariableInitialization,
BoundStatement checkAndBreak,
BoundStatement rewrittenBody,
LabelSymbol continueLabel,
CommonForEachStatementSyntax forEachSyntax)
{
// The scope of the iteration variable is the embedded statement syntax.
// However consider the following foreach statement:
//
// await foreach (int x in ...) { int y = ...; F(() => x); F(() => y));
//
// We currently generate 2 closures. One containing variable x, the other variable y.
// The EnC source mapping infrastructure requires each closure within a method body
// to have a unique syntax offset. Hence we associate the bound block declaring the
// iteration variable with the foreach statement, not the embedded statement.
return new BoundBlock(
forEachSyntax,
locals: iterationVariables,
statements: ImmutableArray.Create(
iteratorVariableInitialization,
checkAndBreak,
rewrittenBody,
new BoundLabelStatement(forEachSyntax, continueLabel)));
}
/// <summary>
/// Lower a foreach loop that will enumerate a single-dimensional array.
///
/// A[] a = x;
/// for (int p = 0; p < a.Length; p = p + 1) {
/// V v = (V)a[p]; /* OR */ (D1 d1, ...) = (V)a[p];
/// // body
/// }
/// </summary>
/// <remarks>
/// We will follow Dev10 in diverging from the C# 4 spec by ignoring Array's
/// implementation of IEnumerable and just indexing into its elements.
///
/// NOTE: We're assuming that sequence points have already been generated.
/// Otherwise, lowering to for-loops would generated spurious ones.
/// </remarks>
private BoundStatement RewriteSingleDimensionalArrayForEachStatement(BoundForEachStatement node)
{
var forEachSyntax = (CommonForEachStatementSyntax)node.Syntax;
BoundExpression collectionExpression = GetUnconvertedCollectionExpression(node);
Debug.Assert(collectionExpression.Type is { TypeKind: TypeKind.Array });
ArrayTypeSymbol arrayType = (ArrayTypeSymbol)collectionExpression.Type;
Debug.Assert(arrayType is { IsSZArray: true });
TypeSymbol intType = _compilation.GetSpecialType(SpecialType.System_Int32);
TypeSymbol boolType = _compilation.GetSpecialType(SpecialType.System_Boolean);
BoundExpression rewrittenExpression = VisitExpression(collectionExpression);
BoundStatement? rewrittenBody = VisitStatement(node.Body);
Debug.Assert(rewrittenBody is { });
// A[] a
LocalSymbol arrayVar = _factory.SynthesizedLocal(arrayType, syntax: forEachSyntax, kind: SynthesizedLocalKind.ForEachArray);
// A[] a = /*node.Expression*/;
BoundStatement arrayVarDecl = MakeLocalDeclaration(forEachSyntax, arrayVar, rewrittenExpression);
InstrumentForEachStatementCollectionVarDeclaration(node, ref arrayVarDecl);
// Reference to a.
BoundLocal boundArrayVar = MakeBoundLocal(forEachSyntax, arrayVar, arrayType);
// int p
LocalSymbol positionVar = _factory.SynthesizedLocal(intType, syntax: forEachSyntax, kind: SynthesizedLocalKind.ForEachArrayIndex);
// Reference to p.
BoundLocal boundPositionVar = MakeBoundLocal(forEachSyntax, positionVar, intType);
// int p = 0;
BoundStatement positionVarDecl = MakeLocalDeclaration(forEachSyntax, positionVar,
MakeLiteral(forEachSyntax, ConstantValue.Default(SpecialType.System_Int32), intType));
// (V)a[p]
BoundExpression iterationVarInitValue = MakeConversionNode(
syntax: forEachSyntax,
rewrittenOperand: new BoundArrayAccess(
syntax: forEachSyntax,
expression: boundArrayVar,
indices: ImmutableArray.Create<BoundExpression>(boundPositionVar),
type: arrayType.ElementType),
conversion: node.ElementConversion,
rewrittenType: node.IterationVariableType.Type,
@checked: node.Checked);
// V v = (V)a[p]; /* OR */ (D1 d1, ...) = (V)a[p];
ImmutableArray<LocalSymbol> iterationVariables = node.IterationVariables;
BoundStatement iterationVariableDecl = LocalOrDeconstructionDeclaration(node, iterationVariables, iterationVarInitValue);
InstrumentForEachStatementIterationVarDeclaration(node, ref iterationVariableDecl);
BoundStatement initializer = new BoundStatementList(forEachSyntax,
statements: ImmutableArray.Create<BoundStatement>(arrayVarDecl, positionVarDecl));
// a.Length
BoundExpression arrayLength = new BoundArrayLength(
syntax: forEachSyntax,
expression: boundArrayVar,
type: intType);
// p < a.Length
BoundExpression exitCondition = new BoundBinaryOperator(
syntax: forEachSyntax,
operatorKind: BinaryOperatorKind.IntLessThan,
left: boundPositionVar,
right: arrayLength,
constantValueOpt: null,
methodOpt: null,
resultKind: LookupResultKind.Viable,
type: boolType);
// p = p + 1;
BoundStatement positionIncrement = MakePositionIncrement(forEachSyntax, boundPositionVar, intType);
// {
// V v = (V)a[p]; /* OR */ (D1 d1, ...) = (V)a[p];
// /*node.Body*/
// }
BoundStatement loopBody = CreateBlockDeclaringIterationVariables(iterationVariables, iterationVariableDecl, rewrittenBody, forEachSyntax);
// for (A[] a = /*node.Expression*/, int p = 0; p < a.Length; p = p + 1) {
// V v = (V)a[p]; /* OR */ (D1 d1, ...) = (V)a[p];
// /*node.Body*/
// }
BoundStatement result = RewriteForStatementWithoutInnerLocals(
original: node,
outerLocals: ImmutableArray.Create<LocalSymbol>(arrayVar, positionVar),
rewrittenInitializer: initializer,
rewrittenCondition: exitCondition,
rewrittenIncrement: positionIncrement,
rewrittenBody: loopBody,
breakLabel: node.BreakLabel,
continueLabel: node.ContinueLabel,
hasErrors: node.HasErrors);
InstrumentForEachStatement(node, ref result);
return result;
}
/// <summary>
/// Lower a foreach loop that will enumerate a multi-dimensional array.
///
/// A[...] a = x;
/// int q_0 = a.GetUpperBound(0), q_1 = a.GetUpperBound(1), ...;
/// for (int p_0 = a.GetLowerBound(0); p_0 <= q_0; p_0 = p_0 + 1)
/// for (int p_1 = a.GetLowerBound(1); p_1 <= q_1; p_1 = p_1 + 1)
/// ...
/// {
/// V v = (V)a[p_0, p_1, ...]; /* OR */ (D1 d1, ...) = (V)a[p_0, p_1, ...];
/// /* body */
/// }
/// </summary>
/// <remarks>
/// We will follow Dev10 in diverging from the C# 4 spec by ignoring Array's
/// implementation of IEnumerable and just indexing into its elements.
///
/// NOTE: We're assuming that sequence points have already been generated.
/// Otherwise, lowering to nested for-loops would generated spurious ones.
/// </remarks>
private BoundStatement RewriteMultiDimensionalArrayForEachStatement(BoundForEachStatement node)
{
var forEachSyntax = (CommonForEachStatementSyntax)node.Syntax;
BoundExpression collectionExpression = GetUnconvertedCollectionExpression(node);
Debug.Assert(collectionExpression.Type is { TypeKind: TypeKind.Array });
ArrayTypeSymbol arrayType = (ArrayTypeSymbol)collectionExpression.Type;
int rank = arrayType.Rank;
Debug.Assert(!arrayType.IsSZArray);
TypeSymbol intType = _compilation.GetSpecialType(SpecialType.System_Int32);
TypeSymbol boolType = _compilation.GetSpecialType(SpecialType.System_Boolean);
// Values we'll use every iteration
MethodSymbol getLowerBoundMethod = UnsafeGetSpecialTypeMethod(forEachSyntax, SpecialMember.System_Array__GetLowerBound);
MethodSymbol getUpperBoundMethod = UnsafeGetSpecialTypeMethod(forEachSyntax, SpecialMember.System_Array__GetUpperBound);
BoundExpression rewrittenExpression = VisitExpression(collectionExpression);
BoundStatement? rewrittenBody = VisitStatement(node.Body);
Debug.Assert(rewrittenBody is { });
// A[...] a
LocalSymbol arrayVar = _factory.SynthesizedLocal(arrayType, syntax: forEachSyntax, kind: SynthesizedLocalKind.ForEachArray);
BoundLocal boundArrayVar = MakeBoundLocal(forEachSyntax, arrayVar, arrayType);
// A[...] a = /*node.Expression*/;
BoundStatement arrayVarDecl = MakeLocalDeclaration(forEachSyntax, arrayVar, rewrittenExpression);
InstrumentForEachStatementCollectionVarDeclaration(node, ref arrayVarDecl);
// NOTE: dev10 initializes all of the upper bound temps before entering the loop (as opposed to
// initializing each one at the corresponding level of nesting). Doing it at the same time as
// the lower bound would make this code a bit simpler, but it would make it harder to compare
// the roslyn and dev10 IL.
// int q_0, q_1, ...
LocalSymbol[] upperVar = new LocalSymbol[rank];
BoundLocal[] boundUpperVar = new BoundLocal[rank];
BoundStatement[] upperVarDecl = new BoundStatement[rank];
for (int dimension = 0; dimension < rank; dimension++)
{
// int q_dimension
upperVar[dimension] = _factory.SynthesizedLocal(intType, syntax: forEachSyntax, kind: SynthesizedLocalKind.ForEachArrayLimit);
boundUpperVar[dimension] = MakeBoundLocal(forEachSyntax, upperVar[dimension], intType);
ImmutableArray<BoundExpression> dimensionArgument = ImmutableArray.Create(
MakeLiteral(forEachSyntax,
constantValue: ConstantValue.Create(dimension, ConstantValueTypeDiscriminator.Int32),
type: intType));
// a.GetUpperBound(dimension)
BoundExpression currentDimensionUpperBound = BoundCall.Synthesized(forEachSyntax, boundArrayVar, getUpperBoundMethod, dimensionArgument);
// int q_dimension = a.GetUpperBound(dimension);
upperVarDecl[dimension] = MakeLocalDeclaration(forEachSyntax, upperVar[dimension], currentDimensionUpperBound);
}
// int p_0, p_1, ...
LocalSymbol[] positionVar = new LocalSymbol[rank];
BoundLocal[] boundPositionVar = new BoundLocal[rank];
for (int dimension = 0; dimension < rank; dimension++)
{
positionVar[dimension] = _factory.SynthesizedLocal(intType, syntax: forEachSyntax, kind: SynthesizedLocalKind.ForEachArrayIndex);
boundPositionVar[dimension] = MakeBoundLocal(forEachSyntax, positionVar[dimension], intType);
}
// (V)a[p_0, p_1, ...]
BoundExpression iterationVarInitValue = MakeConversionNode(
syntax: forEachSyntax,
rewrittenOperand: new BoundArrayAccess(forEachSyntax,
expression: boundArrayVar,
indices: ImmutableArray.Create((BoundExpression[])boundPositionVar),
type: arrayType.ElementType),
conversion: node.ElementConversion,
rewrittenType: node.IterationVariableType.Type,
@checked: node.Checked);
// V v = (V)a[p_0, p_1, ...]; /* OR */ (D1 d1, ...) = (V)a[p_0, p_1, ...];
ImmutableArray<LocalSymbol> iterationVariables = node.IterationVariables;
BoundStatement iterationVarDecl = LocalOrDeconstructionDeclaration(node, iterationVariables, iterationVarInitValue);
InstrumentForEachStatementIterationVarDeclaration(node, ref iterationVarDecl);
// {
// V v = (V)a[p_0, p_1, ...]; /* OR */ (D1 d1, ...) = (V)a[p_0, p_1, ...];
// /* node.Body */
// }
BoundStatement innermostLoopBody = CreateBlockDeclaringIterationVariables(iterationVariables, iterationVarDecl, rewrittenBody, forEachSyntax);
// work from most-nested to least-nested
// for (int p_0 = a.GetLowerBound(0); p_0 <= q_0; p_0 = p_0 + 1)
// for (int p_1 = a.GetLowerBound(0); p_1 <= q_1; p_1 = p_1 + 1)
// ...
// {
// V v = (V)a[p_0, p_1, ...]; /* OR */ (D1 d1, ...) = (V)a[p_0, p_1, ...];
// /* body */
// }
BoundStatement? forLoop = null;
for (int dimension = rank - 1; dimension >= 0; dimension--)
{
ImmutableArray<BoundExpression> dimensionArgument = ImmutableArray.Create(
MakeLiteral(forEachSyntax,
constantValue: ConstantValue.Create(dimension, ConstantValueTypeDiscriminator.Int32),
type: intType));
// a.GetLowerBound(dimension)
BoundExpression currentDimensionLowerBound = BoundCall.Synthesized(forEachSyntax, boundArrayVar, getLowerBoundMethod, dimensionArgument);
// int p_dimension = a.GetLowerBound(dimension);
BoundStatement positionVarDecl = MakeLocalDeclaration(forEachSyntax, positionVar[dimension], currentDimensionLowerBound);
GeneratedLabelSymbol breakLabel = dimension == 0 // outermost for-loop
? node.BreakLabel // i.e. the one that break statements will jump to
: new GeneratedLabelSymbol("break"); // Should not affect emitted code since unused
// p_dimension <= q_dimension //NB: OrEqual
BoundExpression exitCondition = new BoundBinaryOperator(
syntax: forEachSyntax,
operatorKind: BinaryOperatorKind.IntLessThanOrEqual,
left: boundPositionVar[dimension],
right: boundUpperVar[dimension],
constantValueOpt: null,
methodOpt: null,
resultKind: LookupResultKind.Viable,
type: boolType);
// p_dimension = p_dimension + 1;
BoundStatement positionIncrement = MakePositionIncrement(forEachSyntax, boundPositionVar[dimension], intType);
BoundStatement body;