blob: fa3f125019860cce12f5be30dcfc5d2c65983a0e (
plain)
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
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
namespace Internal.TypeSystem
{
/// <summary>
/// Metadata based RuntimeInterfaces algorithm which can be used to compute the
/// RuntimeInterfaces for any MetadataType based on the base types RuntimeInterfaces
/// and the MetadataTypes explicit interfaces.
/// </summary>
public sealed class MetadataRuntimeInterfacesAlgorithm : RuntimeInterfacesAlgorithm
{
public override DefType[] ComputeRuntimeInterfaces(TypeDesc _type)
{
MetadataType type = (MetadataType)_type;
// TODO: need to implement deduplication
// https://github.com/dotnet/corert/issues/208
if (type is InstantiatedType)
{
return ComputeRuntimeInterfacesForInstantiatedType((InstantiatedType)type);
}
else
{
return ComputeRuntimeInterfacesForNonInstantiatedMetadataType(type);
}
}
/// <summary>
/// Instantiated type computation for runtime interfaces. Instantiated types
/// must have the same count of interfaces across all possible instantiations
/// so the algorithm works by computing the uninstantiated form, and then
/// specializing each interface as needed.
/// </summary>
private static DefType[] ComputeRuntimeInterfacesForInstantiatedType(InstantiatedType instantiatedType)
{
MetadataType uninstantiatedType = (MetadataType)instantiatedType.GetTypeDefinition();
return InstantiatedType.InstantiateTypeArray(uninstantiatedType.RuntimeInterfaces, instantiatedType.Instantiation, default(Instantiation));
}
/// <summary>
/// Metadata based computation of interfaces.
/// </summary>
private DefType[] ComputeRuntimeInterfacesForNonInstantiatedMetadataType(MetadataType type)
{
DefType[] explicitInterfaces = type.ExplicitlyImplementedInterfaces;
DefType[] baseTypeInterfaces = (type.BaseType != null) ? (type.BaseType.RuntimeInterfaces) : Array.Empty<DefType>();
// Optimized case for no interfaces newly defined.
if (explicitInterfaces.Length == 0)
return baseTypeInterfaces;
ArrayBuilder<DefType> interfacesArray = default(ArrayBuilder<DefType>);
interfacesArray.Append(baseTypeInterfaces);
foreach (DefType iface in explicitInterfaces)
{
BuildPostOrderInterfaceList(iface, ref interfacesArray);
}
return interfacesArray.ToArray();
}
/// <summary>
/// Add an interface and its required interfaces to the interfacesArray
/// </summary>
private void BuildPostOrderInterfaceList(DefType iface, ref ArrayBuilder<DefType> interfacesArray)
{
if (interfacesArray.Contains(iface))
return;
foreach (DefType implementedInterface in iface.RuntimeInterfaces)
{
BuildPostOrderInterfaceList(implementedInterface, ref interfacesArray);
}
if (interfacesArray.Contains(iface))
return;
interfacesArray.Add(iface);
}
}
}
|