/
ReflectedMethodNode.cs
104 lines (87 loc) · 4.83 KB
/
ReflectedMethodNode.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using ILCompiler.DependencyAnalysisFramework;
using Internal.TypeSystem;
using Debug = System.Diagnostics.Debug;
namespace ILCompiler.DependencyAnalysis
{
/// <summary>
/// Represents a method that is visible to reflection.
/// The method can be on a non-generic type, generic type definition, or an instantiatied type.
/// To match IL semantics, we maintain that a method on a generic type will be consistently
/// reflection-accessible. Either the method is accessible on all instantiations or on none of them.
/// Similar invariants hold for generic methods.
/// </summary>
public class ReflectedMethodNode : DependencyNodeCore<NodeFactory>
{
private readonly MethodDesc _method;
public ReflectedMethodNode(MethodDesc method)
{
Debug.Assert(!method.IsCanonicalMethod(CanonicalFormKind.Any) ||
method.GetCanonMethodTarget(CanonicalFormKind.Specific) == method);
_method = method;
}
public MethodDesc Method => _method;
public override IEnumerable<DependencyListEntry> GetStaticDependencies(NodeFactory factory)
{
Debug.Assert(!factory.MetadataManager.IsReflectionBlocked(_method.GetTypicalMethodDefinition()));
DependencyList dependencies = new DependencyList();
factory.MetadataManager.GetDependenciesDueToReflectability(ref dependencies, factory, _method);
// No runtime artifacts needed if this is a generic definition
if (_method.IsGenericMethodDefinition || _method.OwningType.IsGenericDefinition)
{
return dependencies;
}
// Ensure we consistently apply reflectability to all methods sharing the same definition.
// Different instantiations of the method have a conditional dependency on the definition node that
// brings a ReflectableMethod of the instantiated method if it's necessary for it to be reflectable.
MethodDesc typicalMethod = _method.GetTypicalMethodDefinition();
if (typicalMethod != _method)
{
dependencies.Add(factory.ReflectedMethod(typicalMethod), "Definition of the reflectable method");
}
MethodDesc canonMethod = _method.GetCanonMethodTarget(CanonicalFormKind.Specific);
if (canonMethod != _method)
{
dependencies.Add(factory.ReflectedMethod(canonMethod), "Canonical version of the reflectable method");
}
// Make sure we generate the method body and other artifacts.
if (MetadataManager.IsMethodSupportedInReflectionInvoke(_method))
{
if (_method.IsVirtual)
{
if (_method.HasInstantiation)
{
dependencies.Add(factory.GVMDependencies(_method.GetCanonMethodTarget(CanonicalFormKind.Specific)), "GVM callable reflectable method");
}
else
{
// Virtual method use is tracked on the slot defining method only.
MethodDesc slotDefiningMethod = MetadataVirtualMethodAlgorithm.FindSlotDefiningMethodForVirtualMethod(_method);
if (!factory.VTable(slotDefiningMethod.OwningType).HasFixedSlots)
dependencies.Add(factory.VirtualMethodUse(slotDefiningMethod), "Virtually callable reflectable method");
}
}
if (!_method.IsAbstract)
{
dependencies.Add(factory.MethodEntrypoint(canonMethod), "Body of a reflectable method");
if (_method.HasInstantiation
&& _method != canonMethod)
dependencies.Add(factory.MethodGenericDictionary(_method), "Dictionary of a reflectable method");
}
}
return dependencies;
}
protected override string GetName(NodeFactory factory)
{
return "Reflectable method: " + _method.ToString();
}
public override bool InterestingForDynamicDependencyAnalysis => false;
public override bool HasDynamicDependencies => false;
public override bool HasConditionalStaticDependencies => false;
public override bool StaticDependenciesAreComputed => true;
public override IEnumerable<CombinedDependencyListEntry> GetConditionalStaticDependencies(NodeFactory factory) => null;
public override IEnumerable<CombinedDependencyListEntry> SearchDynamicDependencies(List<DependencyNodeCore<NodeFactory>> markedNodes, int firstNode, NodeFactory factory) => null;
}
}