Skip to content

Commit 987a86e

Browse files
authored
Break up ValueNode.cs so each Value has it's own file (#2713)
* Break up ValueNode.cs so each Value has it's own file * Update ValueNode.cs namespace
1 parent 4f0d349 commit 987a86e

File tree

10 files changed

+437
-349
lines changed

10 files changed

+437
-349
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
using ILLink.Shared.DataFlow;
8+
using Mono.Cecil;
9+
using Mono.Linker.Dataflow;
10+
using MultiValue = ILLink.Shared.DataFlow.ValueSet<ILLink.Shared.DataFlow.SingleValue>;
11+
12+
13+
namespace ILLink.Shared.TrimAnalysis
14+
{
15+
partial record ArrayValue
16+
{
17+
public static MultiValue Create (MultiValue size, TypeReference elementType)
18+
{
19+
MultiValue result = MultiValueLattice.Top;
20+
foreach (var sizeValue in size) {
21+
result = MultiValueLattice.Meet (result, new MultiValue (new ArrayValue (sizeValue, elementType)));
22+
}
23+
24+
return result;
25+
}
26+
27+
public static MultiValue Create (int size, TypeReference elementType)
28+
{
29+
return new MultiValue (new ArrayValue (new ConstIntValue (size), elementType));
30+
}
31+
32+
/// <summary>
33+
/// Constructs an array value of the given size
34+
/// </summary>
35+
ArrayValue (SingleValue size, TypeReference elementType)
36+
{
37+
Size = size;
38+
ElementType = elementType;
39+
IndexValues = new Dictionary<int, ValueBasicBlockPair> ();
40+
}
41+
42+
public TypeReference ElementType { get; }
43+
public Dictionary<int, ValueBasicBlockPair> IndexValues { get; }
44+
45+
public partial bool TryGetValueByIndex (int index, out MultiValue value)
46+
{
47+
if (IndexValues.TryGetValue (index, out var valuePair)) {
48+
value = valuePair.Value;
49+
return true;
50+
}
51+
52+
value = default;
53+
return false;
54+
}
55+
56+
public override int GetHashCode ()
57+
{
58+
return HashCode.Combine (GetType ().GetHashCode (), Size);
59+
}
60+
61+
public bool Equals (ArrayValue? otherArr)
62+
{
63+
if (otherArr == null)
64+
return false;
65+
66+
bool equals = Size.Equals (otherArr.Size);
67+
equals &= IndexValues.Count == otherArr.IndexValues.Count;
68+
if (!equals)
69+
return false;
70+
71+
// If both sets T and O are the same size and "T intersect O" is empty, then T == O.
72+
HashSet<KeyValuePair<int, ValueBasicBlockPair>> thisValueSet = new (IndexValues);
73+
HashSet<KeyValuePair<int, ValueBasicBlockPair>> otherValueSet = new (otherArr.IndexValues);
74+
thisValueSet.ExceptWith (otherValueSet);
75+
return thisValueSet.Count == 0;
76+
}
77+
78+
public override SingleValue DeepCopy ()
79+
{
80+
var newValue = new ArrayValue (Size.DeepCopy (), ElementType);
81+
foreach (var kvp in IndexValues) {
82+
newValue.IndexValues.Add (kvp.Key, new ValueBasicBlockPair (kvp.Value.Value.Clone (), kvp.Value.BasicBlockIndex));
83+
}
84+
85+
return newValue;
86+
}
87+
88+
public override string ToString ()
89+
{
90+
StringBuilder result = new ();
91+
result.Append ("Array Size:");
92+
result.Append (this.ValueToString (Size));
93+
94+
result.Append (", Values:(");
95+
bool first = true;
96+
foreach (var element in IndexValues) {
97+
if (!first) {
98+
result.Append (",");
99+
first = false;
100+
}
101+
102+
result.Append ("(");
103+
result.Append (element.Key);
104+
result.Append (",(");
105+
bool firstValue = true;
106+
foreach (var v in element.Value.Value) {
107+
if (firstValue) {
108+
result.Append (",");
109+
firstValue = false;
110+
}
111+
112+
result.Append (v.ToString ());
113+
}
114+
result.Append ("))");
115+
}
116+
result.Append (')');
117+
118+
return result.ToString ();
119+
}
120+
}
121+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Diagnostics.CodeAnalysis;
6+
using ILLink.Shared.DataFlow;
7+
using Mono.Linker;
8+
using Mono.Linker.Dataflow;
9+
using FieldDefinition = Mono.Cecil.FieldDefinition;
10+
using TypeDefinition = Mono.Cecil.TypeDefinition;
11+
12+
13+
namespace ILLink.Shared.TrimAnalysis
14+
{
15+
16+
/// <summary>
17+
/// A representation of a field. Typically a result of ldfld.
18+
/// </summary>
19+
sealed partial record FieldValue : IValueWithStaticType
20+
{
21+
public FieldValue (TypeDefinition? staticType, FieldDefinition fieldToLoad, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes)
22+
{
23+
StaticType = staticType;
24+
Field = fieldToLoad;
25+
DynamicallyAccessedMemberTypes = dynamicallyAccessedMemberTypes;
26+
}
27+
28+
public readonly FieldDefinition Field;
29+
30+
public override DynamicallyAccessedMemberTypes DynamicallyAccessedMemberTypes { get; }
31+
32+
public override IEnumerable<string> GetDiagnosticArgumentsForAnnotationMismatch ()
33+
=> new string[] { Field.GetDisplayName () };
34+
35+
public TypeDefinition? StaticType { get; }
36+
37+
public override SingleValue DeepCopy () => this; // This value is immutable
38+
39+
public override string ToString () => this.ValueToString (Field, DynamicallyAccessedMemberTypes);
40+
}
41+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Diagnostics.CodeAnalysis;
6+
using ILLink.Shared.DataFlow;
7+
using Mono.Linker.Dataflow;
8+
using GenericParameter = Mono.Cecil.GenericParameter;
9+
10+
namespace ILLink.Shared.TrimAnalysis
11+
{
12+
/// <summary>
13+
/// This is a System.Type value which represents generic parameter (basically result of typeof(T))
14+
/// Its actual type is unknown, but it can have annotations.
15+
/// </summary>
16+
partial record GenericParameterValue
17+
{
18+
public GenericParameterValue (GenericParameter genericParameter, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes)
19+
{
20+
GenericParameter = new (genericParameter);
21+
DynamicallyAccessedMemberTypes = dynamicallyAccessedMemberTypes;
22+
}
23+
24+
public partial bool HasDefaultConstructorConstraint () => GenericParameter.GenericParameter.HasDefaultConstructorConstraint;
25+
26+
public override DynamicallyAccessedMemberTypes DynamicallyAccessedMemberTypes { get; }
27+
28+
public override IEnumerable<string> GetDiagnosticArgumentsForAnnotationMismatch ()
29+
=> new string[] { GenericParameter.GenericParameter.Name, DiagnosticUtilities.GetGenericParameterDeclaringMemberDisplayName (GenericParameter.GenericParameter) };
30+
31+
public override SingleValue DeepCopy () => this; // This value is immutable
32+
33+
public override string ToString () => this.ValueToString (GenericParameter, DynamicallyAccessedMemberTypes);
34+
}
35+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Diagnostics.CodeAnalysis;
6+
using ILLink.Shared.DataFlow;
7+
using Mono.Cecil;
8+
using Mono.Linker.Dataflow;
9+
using TypeDefinition = Mono.Cecil.TypeDefinition;
10+
11+
12+
namespace ILLink.Shared.TrimAnalysis
13+
{
14+
15+
/// <summary>
16+
/// A value that came from a method parameter - such as the result of a ldarg.
17+
/// </summary>
18+
partial record MethodParameterValue : IValueWithStaticType
19+
{
20+
public MethodParameterValue (TypeDefinition? staticType, MethodDefinition method, int parameterIndex, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes)
21+
{
22+
StaticType = staticType;
23+
Method = method;
24+
ParameterIndex = parameterIndex;
25+
DynamicallyAccessedMemberTypes = dynamicallyAccessedMemberTypes;
26+
}
27+
28+
public readonly MethodDefinition Method;
29+
30+
/// <summary>
31+
/// This is the index of non-implicit parameter - so the index into MethodDefinition.Parameters array.
32+
/// It's NOT the IL parameter index which could be offset by 1 if the method has an implicit this.
33+
/// </summary>
34+
public readonly int ParameterIndex;
35+
36+
public ParameterDefinition ParameterDefinition => Method.Parameters[ParameterIndex];
37+
38+
public override DynamicallyAccessedMemberTypes DynamicallyAccessedMemberTypes { get; }
39+
40+
public override IEnumerable<string> GetDiagnosticArgumentsForAnnotationMismatch ()
41+
=> new string[] { DiagnosticUtilities.GetParameterNameForErrorMessage (ParameterDefinition), DiagnosticUtilities.GetMethodSignatureDisplayName (Method) };
42+
43+
public TypeDefinition? StaticType { get; }
44+
45+
public override SingleValue DeepCopy () => this; // This value is immutable
46+
47+
public override string ToString () => this.ValueToString (Method, ParameterIndex, DynamicallyAccessedMemberTypes);
48+
}
49+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Diagnostics.CodeAnalysis;
6+
using ILLink.Shared.DataFlow;
7+
using Mono.Cecil;
8+
using Mono.Linker.Dataflow;
9+
using TypeDefinition = Mono.Cecil.TypeDefinition;
10+
11+
12+
namespace ILLink.Shared.TrimAnalysis
13+
{
14+
/// <summary>
15+
/// Return value from a method
16+
/// </summary>
17+
partial record MethodReturnValue : IValueWithStaticType
18+
{
19+
public MethodReturnValue (TypeDefinition? staticType, MethodDefinition method, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes)
20+
{
21+
StaticType = staticType;
22+
Method = method;
23+
DynamicallyAccessedMemberTypes = dynamicallyAccessedMemberTypes;
24+
}
25+
26+
public readonly MethodDefinition Method;
27+
28+
public override DynamicallyAccessedMemberTypes DynamicallyAccessedMemberTypes { get; }
29+
30+
public override IEnumerable<string> GetDiagnosticArgumentsForAnnotationMismatch ()
31+
=> new string[] { DiagnosticUtilities.GetMethodSignatureDisplayName (Method) };
32+
33+
public TypeDefinition? StaticType { get; }
34+
35+
public override SingleValue DeepCopy () => this; // This value is immutable
36+
37+
public override string ToString () => this.ValueToString (Method, DynamicallyAccessedMemberTypes);
38+
}
39+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Diagnostics.CodeAnalysis;
6+
using ILLink.Shared.DataFlow;
7+
using Mono.Cecil;
8+
using Mono.Linker;
9+
using Mono.Linker.Dataflow;
10+
using TypeDefinition = Mono.Cecil.TypeDefinition;
11+
12+
13+
namespace ILLink.Shared.TrimAnalysis
14+
{
15+
16+
/// <summary>
17+
/// A value that came from the implicit this parameter of a method
18+
/// </summary>
19+
partial record MethodThisParameterValue : IValueWithStaticType
20+
{
21+
public MethodThisParameterValue (MethodDefinition method, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes)
22+
{
23+
Method = method;
24+
DynamicallyAccessedMemberTypes = dynamicallyAccessedMemberTypes;
25+
}
26+
27+
public readonly MethodDefinition Method;
28+
29+
public override DynamicallyAccessedMemberTypes DynamicallyAccessedMemberTypes { get; }
30+
31+
public override IEnumerable<string> GetDiagnosticArgumentsForAnnotationMismatch ()
32+
=> new string[] { Method.GetDisplayName () };
33+
34+
public TypeDefinition? StaticType => Method.DeclaringType;
35+
36+
public override SingleValue DeepCopy () => this; // This value is immutable
37+
38+
public override string ToString () => this.ValueToString (Method, DynamicallyAccessedMemberTypes);
39+
}
40+
}

src/linker/Linker.Dataflow/ReflectionMethodBodyScanner.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
using Mono.Cecil;
1515
using Mono.Cecil.Cil;
1616
using Mono.Linker.Steps;
17-
1817
using BindingFlags = System.Reflection.BindingFlags;
19-
2018
using MultiValue = ILLink.Shared.DataFlow.ValueSet<ILLink.Shared.DataFlow.SingleValue>;
2119

2220
namespace Mono.Linker.Dataflow
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using ILLink.Shared.DataFlow;
5+
using Mono.Cecil;
6+
7+
8+
namespace ILLink.Shared.TrimAnalysis
9+
{
10+
11+
/// <summary>
12+
/// This is the System.RuntimeMethodHandle equivalent to a <see cref="SystemReflectionMethodBaseValue"/> node.
13+
/// </summary>
14+
partial record RuntimeMethodHandleValue
15+
{
16+
public RuntimeMethodHandleValue (MethodDefinition methodRepresented) => MethodRepresented = methodRepresented;
17+
18+
public readonly MethodDefinition MethodRepresented;
19+
20+
public override SingleValue DeepCopy () => this; // This value is immutable
21+
22+
public override string ToString () => this.ValueToString (MethodRepresented);
23+
}
24+
}

0 commit comments

Comments
 (0)