Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TypeSystemContext.RuntimeDetermined.cs « RuntimeDetermined « TypeSystem « Common « tools « coreclr « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dbbb7442aa4f4c4d58ca3805bde605f39fcc1965 (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
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
// 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.Diagnostics;

using Internal.NativeFormat;

namespace Internal.TypeSystem
{
    partial class TypeSystemContext
    {
        private struct RuntimeDeterminedTypeKey
        {
            private DefType _plainCanonType;
            private GenericParameterDesc _detailsType;

            public RuntimeDeterminedTypeKey(DefType plainCanonType, GenericParameterDesc detailsType)
            {
                _plainCanonType = plainCanonType;
                _detailsType = detailsType;
            }

            public class RuntimeDeterminedTypeKeyHashtable : LockFreeReaderHashtable<RuntimeDeterminedTypeKey, RuntimeDeterminedType>
            {
                protected override int GetKeyHashCode(RuntimeDeterminedTypeKey key)
                {
                    return key._detailsType.GetHashCode() ^ key._plainCanonType.GetHashCode();
                }

                protected override int GetValueHashCode(RuntimeDeterminedType value)
                {
                    return value.RuntimeDeterminedDetailsType.GetHashCode() ^ value.CanonicalType.GetHashCode();
                }

                protected override bool CompareKeyToValue(RuntimeDeterminedTypeKey key, RuntimeDeterminedType value)
                {
                    return key._detailsType == value.RuntimeDeterminedDetailsType && key._plainCanonType == value.CanonicalType;
                }

                protected override bool CompareValueToValue(RuntimeDeterminedType value1, RuntimeDeterminedType value2)
                {
                    return value1.RuntimeDeterminedDetailsType == value2.RuntimeDeterminedDetailsType
                        && value1.CanonicalType == value2.CanonicalType;
                }

                protected override RuntimeDeterminedType CreateValueFromKey(RuntimeDeterminedTypeKey key)
                {
                    return new RuntimeDeterminedType(key._plainCanonType, key._detailsType);
                }
            }
        }

        private RuntimeDeterminedTypeKey.RuntimeDeterminedTypeKeyHashtable _runtimeDeterminedTypes = new RuntimeDeterminedTypeKey.RuntimeDeterminedTypeKeyHashtable();

        public RuntimeDeterminedType GetRuntimeDeterminedType(DefType plainCanonType, GenericParameterDesc detailsType)
        {
            return _runtimeDeterminedTypes.GetOrCreateValue(new RuntimeDeterminedTypeKey(plainCanonType, detailsType));
        }

        protected internal virtual TypeDesc ConvertToCanon(TypeDesc typeToConvert, ref CanonicalFormKind kind)
        {
            throw new NotSupportedException();
        }

        //
        // Methods for runtime determined type
        //

        private struct MethodForRuntimeDeterminedTypeKey
        {
            private MethodDesc _typicalMethodDef;
            private RuntimeDeterminedType _rdType;
            private int _hashcode;

            public MethodForRuntimeDeterminedTypeKey(MethodDesc typicalMethodDef, RuntimeDeterminedType rdType)
            {
                _typicalMethodDef = typicalMethodDef;
                _rdType = rdType;
                _hashcode = TypeHashingAlgorithms.ComputeMethodHashCode(rdType.CanonicalType.GetHashCode(), TypeHashingAlgorithms.ComputeNameHashCode(typicalMethodDef.Name));
            }

            public MethodDesc TypicalMethodDef
            {
                get
                {
                    return _typicalMethodDef;
                }
            }

            public RuntimeDeterminedType RDType
            {
                get
                {
                    return _rdType;
                }
            }

            public class MethodForRuntimeDeterminedTypeKeyHashtable : LockFreeReaderHashtable<MethodForRuntimeDeterminedTypeKey, MethodForRuntimeDeterminedType>
            {
                protected override int GetKeyHashCode(MethodForRuntimeDeterminedTypeKey key)
                {
                    return key._hashcode;
                }

                protected override int GetValueHashCode(MethodForRuntimeDeterminedType value)
                {
                    return value.GetHashCode();
                }

                protected override bool CompareKeyToValue(MethodForRuntimeDeterminedTypeKey key, MethodForRuntimeDeterminedType value)
                {
                    if (key._typicalMethodDef != value.GetTypicalMethodDefinition())
                        return false;

                    return key._rdType == value.OwningType;
                }

                protected override bool CompareValueToValue(MethodForRuntimeDeterminedType value1, MethodForRuntimeDeterminedType value2)
                {
                    return (value1.GetTypicalMethodDefinition() == value2.GetTypicalMethodDefinition()) && (value1.OwningType == value2.OwningType);
                }

                protected override MethodForRuntimeDeterminedType CreateValueFromKey(MethodForRuntimeDeterminedTypeKey key)
                {
                    return new MethodForRuntimeDeterminedType(key.TypicalMethodDef, key.RDType, key._hashcode);
                }
            }
        }

        private MethodForRuntimeDeterminedTypeKey.MethodForRuntimeDeterminedTypeKeyHashtable _methodForRDTypes = new MethodForRuntimeDeterminedTypeKey.MethodForRuntimeDeterminedTypeKeyHashtable();

        public MethodDesc GetMethodForRuntimeDeterminedType(MethodDesc typicalMethodDef, RuntimeDeterminedType rdType)
        {
            Debug.Assert(!(typicalMethodDef is MethodForRuntimeDeterminedType));
            Debug.Assert(typicalMethodDef.IsTypicalMethodDefinition);

            return _methodForRDTypes.GetOrCreateValue(new MethodForRuntimeDeterminedTypeKey(typicalMethodDef, rdType));
        }
    }
}