-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathTransformCollectionAndObjectInitializers.cs
More file actions
565 lines (536 loc) · 20.6 KB
/
TransformCollectionAndObjectInitializers.cs
File metadata and controls
565 lines (536 loc) · 20.6 KB
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
// Copyright (c) 2017 Siegfried Pammer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.Decompiler.CSharp.Resolver;
using ICSharpCode.Decompiler.CSharp.TypeSystem;
using ICSharpCode.Decompiler.Semantics;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.Util;
namespace ICSharpCode.Decompiler.IL.Transforms
{
/// <summary>
/// Transforms collection and object initialization patterns.
/// </summary>
public class TransformCollectionAndObjectInitializers : IStatementTransform
{
void IStatementTransform.Run(Block block, int pos, StatementTransformContext context)
{
if (!context.Settings.ObjectOrCollectionInitializers)
return;
ILInstruction inst = block.Instructions[pos];
// Match stloc(v, newobj)
if (!inst.MatchStLoc(out var v, out var initInst) || v.Kind != VariableKind.Local && v.Kind != VariableKind.StackSlot)
return;
IType instType;
var blockKind = BlockKind.CollectionInitializer;
var insertionPos = initInst.ChildIndex;
var siblings = initInst.Parent!.Children;
IMethod currentMethod = context.Function.Method!;
switch (initInst)
{
case NewObj newObjInst:
if (newObjInst.ILStackWasEmpty && v.Kind == VariableKind.Local
&& !currentMethod.IsConstructor
&& !currentMethod.IsCompilerGeneratedOrIsInCompilerGeneratedClass())
{
// on statement level (no other expressions on IL stack),
// prefer to keep local variables (but not stack slots),
// unless we are in a constructor (where inlining object initializers might be critical
// for the base ctor call) or a compiler-generated delegate method, which might be used in a query expression.
return;
}
// Do not try to transform delegate construction.
// DelegateConstruction transform cannot deal with this.
if (DelegateConstruction.MatchDelegateConstruction(newObjInst, out _, out _, out _)
|| TransformDisplayClassUsage.IsPotentialClosure(context, newObjInst))
return;
// Cannot build a collection/object initializer attached to an AnonymousTypeCreateExpression
// anon = new { A = 5 } { 3,4,5 } is invalid syntax.
if (newObjInst.Method.DeclaringType.ContainsAnonymousType())
return;
instType = newObjInst.Method.DeclaringType;
break;
case DefaultValue defaultVal:
instType = defaultVal.Type;
break;
case Call c when c.Method.FullNameIs("System.Activator", "CreateInstance") && c.Method.TypeArguments.Count == 1:
instType = c.Method.TypeArguments[0];
blockKind = BlockKind.ObjectInitializer;
break;
case CallInstruction ci when context.Settings.WithExpressions && IsRecordCloneMethodCall(ci):
instType = ci.Method.DeclaringType;
blockKind = BlockKind.WithInitializer;
initInst = ci.Arguments.Single();
break;
default:
var typeDef = v.Type.GetDefinition();
if (context.Settings.WithExpressions && typeDef?.IsReferenceType == false && typeDef.IsRecord)
{
instType = v.Type;
blockKind = BlockKind.WithInitializer;
break;
}
return;
}
int initializerItemsCount = 0;
bool initializerContainsInitOnlyItems = false;
possibleIndexVariables.Clear();
currentPath.Clear();
isCollection = false;
pathStack.Clear();
pathStack.Push(new HashSet<AccessPathElement>());
// Detect initializer type by scanning the following statements
// each must be a callvirt with ldloc v as first argument
// if the method is a setter we're dealing with an object initializer
// if the method is named Add and has at least 2 arguments we're dealing with a collection/dictionary initializer
while (pos + initializerItemsCount + 1 < block.Instructions.Count
&& IsPartOfInitializer(block.Instructions, pos + initializerItemsCount + 1, v, instType, ref blockKind, ref initializerContainsInitOnlyItems, context))
{
initializerItemsCount++;
}
// Do not convert the statements into an initializer if there's an incompatible usage of the initializer variable
// directly after the possible initializer.
if (!initializerContainsInitOnlyItems && IsMethodCallOnVariable(block.Instructions[pos + initializerItemsCount + 1], v))
return;
// Calculate the correct number of statements inside the initializer:
// All index variables that were used in the initializer have Index set to -1.
// We fetch the first unused variable from the list and remove all instructions after its
// first usage (i.e. the init store) from the initializer.
var index = possibleIndexVariables.Where(info => info.Value.Index > -1).Min(info => (int?)info.Value.Index);
if (index != null)
{
initializerItemsCount = index.Value - pos - 1;
}
// The initializer would be empty, there's nothing to do here.
if (initializerItemsCount <= 0)
return;
context.Step("CollectionOrObjectInitializer", inst);
// Create a new block and final slot (initializer target variable)
var initializerBlock = new Block(blockKind);
ILVariable finalSlot = context.Function.RegisterVariable(VariableKind.InitializerTarget, instType);
initializerBlock.FinalInstruction = new LdLoc(finalSlot);
initializerBlock.Instructions.Add(new StLoc(finalSlot, initInst));
// Move all instructions to the initializer block.
for (int i = 1; i <= initializerItemsCount; i++)
{
switch (block.Instructions[i + pos])
{
case CallInstruction call:
if (!(call is CallVirt || call is Call))
continue;
var newCall = call;
var newTarget = newCall.Arguments[0];
foreach (var load in newTarget.Descendants.OfType<IInstructionWithVariableOperand>())
if ((load is LdLoc || load is LdLoca) && load.Variable == v)
load.Variable = finalSlot;
initializerBlock.Instructions.Add(newCall);
break;
case StObj stObj:
var newStObj = stObj;
foreach (var load in newStObj.Target.Descendants.OfType<IInstructionWithVariableOperand>())
if ((load is LdLoc || load is LdLoca) && load.Variable == v)
load.Variable = finalSlot;
initializerBlock.Instructions.Add(newStObj);
break;
case StLoc stLoc:
var newStLoc = stLoc;
initializerBlock.Instructions.Add(newStLoc);
break;
}
}
block.Instructions.RemoveRange(pos + 1, initializerItemsCount);
siblings[insertionPos] = initializerBlock;
ILInlining.InlineIfPossible(block, pos, context);
}
internal static bool IsRecordCloneMethodCall(CallInstruction ci)
{
if (ci.Method.DeclaringTypeDefinition?.IsRecord != true)
return false;
if (ci.Method.Name != "<Clone>$")
return false;
if (ci.Arguments.Count != 1)
return false;
return true;
}
bool IsMethodCallOnVariable(ILInstruction inst, ILVariable variable)
{
if (inst.MatchLdLocRef(variable))
return true;
if (inst is CallInstruction call && call.Arguments.Count > 0 && !call.Method.IsStatic)
return IsMethodCallOnVariable(call.Arguments[0], variable);
if (inst.MatchLdFld(out var target, out _) || inst.MatchStFld(out target, out _, out _) || inst.MatchLdFlda(out target, out _))
return IsMethodCallOnVariable(target, variable);
return false;
}
readonly Dictionary<ILVariable, (int Index, ILInstruction Value)> possibleIndexVariables = new Dictionary<ILVariable, (int Index, ILInstruction Value)>();
readonly List<AccessPathElement> currentPath = new List<AccessPathElement>();
bool isCollection;
readonly Stack<HashSet<AccessPathElement>> pathStack = new Stack<HashSet<AccessPathElement>>();
bool IsPartOfInitializer(InstructionCollection<ILInstruction> instructions, int pos, ILVariable target, IType rootType, ref BlockKind blockKind, ref bool initializerContainsInitOnlyItems, StatementTransformContext context)
{
// Include any stores to local variables that are single-assigned and do not reference the initializer-variable
// in the list of possible index variables.
// Index variables are used to implement dictionary initializers.
if (instructions[pos] is StLoc stloc && stloc.Variable.Kind == VariableKind.Local && stloc.Variable.IsSingleDefinition)
{
if (!context.Settings.DictionaryInitializers)
return false;
if (stloc.Value.Descendants.OfType<IInstructionWithVariableOperand>().Any(ld => ld.Variable == target && (ld is LdLoc || ld is LdLoca)))
return false;
possibleIndexVariables.Add(stloc.Variable, (stloc.ChildIndex, stloc.Value));
return true;
}
var resolveContext = new CSharpTypeResolveContext(context.TypeSystem.MainModule, context.UsingScope);
(var kind, var newPath, var values, var targetVariable) = AccessPathElement.GetAccessPath(instructions[pos], rootType, context.Settings, resolveContext, possibleIndexVariables);
if (kind == AccessPathKind.Invalid || target != targetVariable)
return false;
// Treat last element separately:
// Can either be an Add method call or property setter.
var lastElement = newPath.Last();
newPath.RemoveLast();
// Compare new path with current path:
int minLen = Math.Min(currentPath.Count, newPath.Count);
int firstDifferenceIndex = 0;
while (firstDifferenceIndex < minLen && newPath[firstDifferenceIndex] == currentPath[firstDifferenceIndex])
firstDifferenceIndex++;
while (currentPath.Count > firstDifferenceIndex)
{
isCollection = false;
currentPath.RemoveAt(currentPath.Count - 1);
pathStack.Pop();
}
while (currentPath.Count < newPath.Count)
{
AccessPathElement newElement = newPath[currentPath.Count];
currentPath.Add(newElement);
if (isCollection || !pathStack.Peek().Add(newElement))
return false;
pathStack.Push(new HashSet<AccessPathElement>());
}
switch (kind)
{
case AccessPathKind.Adder:
isCollection = true;
if (pathStack.Peek().Count != 0)
return false;
return true;
case AccessPathKind.Setter:
if (isCollection || !pathStack.Peek().Add(lastElement))
return false;
if (values?.Count != 1 || !IsValidObjectInitializerTarget(currentPath))
return false;
if (blockKind != BlockKind.ObjectInitializer && blockKind != BlockKind.WithInitializer)
blockKind = BlockKind.ObjectInitializer;
initializerContainsInitOnlyItems |= lastElement.Member is IProperty { Setter.IsInitOnly: true };
return true;
default:
return false;
}
}
bool IsValidObjectInitializerTarget(List<AccessPathElement> path)
{
if (path.Count == 0)
return true;
var element = path.Last();
var previous = path.SkipLast(1).LastOrDefault();
if (element.Member is not IProperty p)
return true;
if (!p.IsIndexer)
return true;
if (previous != default)
{
return NormalizeTypeVisitor.IgnoreNullabilityAndTuples
.EquivalentTypes(previous.Member.ReturnType, element.Member.DeclaringType);
}
return false;
}
}
public enum AccessPathKind
{
Invalid,
Setter,
Adder
}
public struct AccessPathElement : IEquatable<AccessPathElement>
{
public AccessPathElement(OpCode opCode, IMember member, ILInstruction[]? indices = null)
{
this.OpCode = opCode;
this.Member = member;
this.Indices = indices;
}
public readonly OpCode OpCode;
public readonly IMember Member;
public readonly ILInstruction[]? Indices;
public override string ToString() => $"[{Member}, {Indices}]";
public static (AccessPathKind Kind, List<AccessPathElement> Path, List<ILInstruction>? Values, ILVariable? Target) GetAccessPath(
ILInstruction instruction, IType rootType, DecompilerSettings? settings = null,
CSharpTypeResolveContext? resolveContext = null,
Dictionary<ILVariable, (int Index, ILInstruction Value)>? possibleIndexVariables = null)
{
List<AccessPathElement> path = new List<AccessPathElement>();
ILVariable? target = null;
AccessPathKind kind = AccessPathKind.Invalid;
List<ILInstruction>? values = null;
IMethod method;
ILInstruction? inst = instruction;
while (inst != null)
{
switch (inst)
{
case CallInstruction call:
if (!(call is CallVirt || call is Call))
goto default;
method = call.Method;
if (resolveContext != null && !IsMethodApplicable(method, call.Arguments, rootType, resolveContext, settings))
goto default;
inst = call.Arguments[0];
if (inst is LdObjIfRef ldObjIfRef)
{
inst = ldObjIfRef.Target;
}
if (method.IsAccessor)
{
if (method.AccessorOwner is IProperty property &&
!CanBeUsedInInitializer(property, resolveContext, kind))
{
goto default;
}
var isGetter = method.AccessorKind == System.Reflection.MethodSemanticsAttributes.Getter;
var indices = call.Arguments.Skip(1).Take(call.Arguments.Count - (isGetter ? 1 : 2)).ToArray();
if (indices.Length > 0 && settings?.DictionaryInitializers == false)
goto default;
if (possibleIndexVariables != null)
{
// Mark all index variables as used
foreach (var index in indices.OfType<IInstructionWithVariableOperand>())
{
if (possibleIndexVariables.TryGetValue(index.Variable, out var info))
possibleIndexVariables[index.Variable] = (-1, info.Value);
}
}
path.Insert(0, new AccessPathElement(call.OpCode, method.AccessorOwner, indices));
}
else
{
path.Insert(0, new AccessPathElement(call.OpCode, method));
}
if (values == null)
{
if (method.IsAccessor)
{
kind = AccessPathKind.Setter;
values = new List<ILInstruction> { call.Arguments.Last() };
}
else
{
kind = AccessPathKind.Adder;
values = new List<ILInstruction>(call.Arguments.Skip(1));
if (values.Count == 0)
goto default;
}
}
break;
case LdObj ldobj:
{
if (ldobj.Target is LdFlda ldflda && (kind != AccessPathKind.Setter || !ldflda.Field.IsReadOnly))
{
path.Insert(0, new AccessPathElement(ldobj.OpCode, ldflda.Field));
inst = ldflda.Target;
break;
}
goto default;
}
case LdObjIfRef ldobj:
{
if (ldobj.Target is LdFlda ldflda && (kind != AccessPathKind.Setter || !ldflda.Field.IsReadOnly))
{
path.Insert(0, new AccessPathElement(ldobj.OpCode, ldflda.Field));
inst = ldflda.Target;
break;
}
if (ldobj.Target is LdLoca ldloca)
{
target = ldloca.Variable;
inst = null;
break;
}
goto default;
}
case StObj stobj:
{
if (stobj.Target is LdFlda ldflda)
{
path.Insert(0, new AccessPathElement(stobj.OpCode, ldflda.Field));
inst = ldflda.Target;
if (values == null)
{
values = new List<ILInstruction>(new[] { stobj.Value });
kind = AccessPathKind.Setter;
}
break;
}
goto default;
}
case LdLoc ldloc:
target = ldloc.Variable;
inst = null;
break;
case LdLoca ldloca:
target = ldloca.Variable;
inst = null;
break;
case LdFlda ldflda:
path.Insert(0, new AccessPathElement(ldflda.OpCode, ldflda.Field));
inst = ldflda.Target;
break;
default:
kind = AccessPathKind.Invalid;
inst = null;
break;
}
}
if (kind != AccessPathKind.Invalid && values != null && values.SelectMany(v => v.Descendants).OfType<IInstructionWithVariableOperand>().Any(ld => ld.Variable == target && (ld is LdLoc || ld is LdLoca)))
kind = AccessPathKind.Invalid;
return (kind, path, values, target);
}
private static bool CanBeUsedInInitializer(IProperty property, CSharpTypeResolveContext? resolveContext, AccessPathKind kind)
{
if (property.CanSet && (property.Accessibility == property.Setter.Accessibility || IsAccessorAccessible(property.Setter, resolveContext)))
return true;
return kind != AccessPathKind.Setter;
}
private static bool IsAccessorAccessible(IMethod setter, CSharpTypeResolveContext? resolveContext)
{
if (resolveContext == null)
return true;
var lookup = new MemberLookup(resolveContext.CurrentTypeDefinition, resolveContext.CurrentModule);
return lookup.IsAccessible(setter, allowProtectedAccess: setter.DeclaringTypeDefinition == resolveContext.CurrentTypeDefinition);
}
static bool IsMethodApplicable(IMethod method, IReadOnlyList<ILInstruction> arguments, IType rootType, CSharpTypeResolveContext resolveContext, DecompilerSettings? settings)
{
if (method.IsStatic && !method.IsExtensionMethod)
return false;
if (method.AccessorOwner is IProperty)
return true;
if (!"Add".Equals(method.Name, StringComparison.Ordinal) || arguments.Count == 0)
return false;
if (method.IsExtensionMethod)
{
if (settings?.ExtensionMethodsInCollectionInitializers == false)
return false;
if (!CSharp.Transforms.IntroduceExtensionMethods.CanTransformToExtensionMethodCall(method, resolveContext, ignoreTypeArguments: true))
return false;
}
var targetType = GetReturnTypeFromInstruction(arguments[0]) ?? rootType;
if (targetType == null)
return false;
if (!targetType.GetAllBaseTypes().Any(i => i.IsKnownType(KnownTypeCode.IEnumerable) || i.IsKnownType(KnownTypeCode.IEnumerableOfT)))
return false;
return CanInferTypeArgumentsFromParameters(method);
bool CanInferTypeArgumentsFromParameters(IMethod method)
{
if (method.TypeParameters.Count == 0)
return true;
// always use unspecialized member, otherwise type inference fails
method = (IMethod)method.MemberDefinition;
new TypeInference(resolveContext.Compilation)
.InferTypeArguments(
method.TypeParameters,
// TODO : this is not entirely correct... we need argument type information to resolve Add methods properly
method.Parameters.SelectReadOnlyArray(p => new ResolveResult(p.Type)),
method.Parameters.SelectReadOnlyArray(p => p.Type),
out bool success
);
return success;
}
}
static IType? GetReturnTypeFromInstruction(ILInstruction instruction)
{
switch (instruction)
{
case CallInstruction call:
if (!(call is CallVirt || call is Call))
goto default;
return call.Method.ReturnType;
case LdObj ldobj:
if (ldobj.Target is LdFlda ldflda)
return ldflda.Field.ReturnType;
goto default;
case StObj stobj:
if (stobj.Target is LdFlda ldflda2)
return ldflda2.Field.ReturnType;
goto default;
default:
return null;
}
}
public override bool Equals(object? obj)
{
if (obj is AccessPathElement)
return Equals((AccessPathElement)obj);
return false;
}
public override int GetHashCode()
{
int hashCode = 0;
unchecked
{
if (Member != null)
hashCode += 1000000007 * Member.GetHashCode();
}
return hashCode;
}
public bool Equals(AccessPathElement other)
{
return (other.Member == this.Member
|| this.Member.Equals(other.Member))
&& (other.Indices == this.Indices
|| (other.Indices != null && this.Indices != null && this.Indices.SequenceEqual(other.Indices, ILInstructionMatchComparer.Instance)));
}
public static bool operator ==(AccessPathElement lhs, AccessPathElement rhs)
{
return lhs.Equals(rhs);
}
public static bool operator !=(AccessPathElement lhs, AccessPathElement rhs)
{
return !(lhs == rhs);
}
}
class ILInstructionMatchComparer : IEqualityComparer<ILInstruction>
{
public static readonly ILInstructionMatchComparer Instance = new ILInstructionMatchComparer();
public bool Equals(ILInstruction? x, ILInstruction? y)
{
if (x == y)
return true;
if (x == null || y == null)
return false;
return SemanticHelper.IsPure(x.Flags)
&& SemanticHelper.IsPure(y.Flags)
&& x.Match(y).Success;
}
public int GetHashCode(ILInstruction obj)
{
throw new NotSupportedException();
}
}
}