This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
/
DummyTypeParameter.cs
134 lines (112 loc) · 4.11 KB
/
DummyTypeParameter.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
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
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// 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.
using System;
using System.Collections.Generic;
using System.Threading;
namespace ICSharpCode.NRefactory.TypeSystem.Implementation
{
public sealed class DummyTypeParameter : AbstractType, ITypeParameter
{
static ITypeParameter[] methodTypeParameters = { new DummyTypeParameter(EntityType.Method, 0) };
static ITypeParameter[] classTypeParameters = { new DummyTypeParameter(EntityType.TypeDefinition, 0) };
public static ITypeParameter GetMethodTypeParameter(int index)
{
return GetTypeParameter(ref methodTypeParameters, EntityType.Method, index);
}
public static ITypeParameter GetClassTypeParameter(int index)
{
return GetTypeParameter(ref classTypeParameters, EntityType.TypeDefinition, index);
}
static ITypeParameter GetTypeParameter(ref ITypeParameter[] typeParameters, EntityType entityType, int index)
{
ITypeParameter[] tps = typeParameters;
while (index >= tps.Length) {
// We don't have a normal type parameter for this index, so we need to extend our array.
// Because the array can be used concurrently from multiple threads, we have to use
// Interlocked.CompareExchange.
ITypeParameter[] newTps = new ITypeParameter[index + 1];
tps.CopyTo(newTps, 0);
for (int i = tps.Length; i < newTps.Length; i++) {
newTps[i] = new DummyTypeParameter(entityType, i);
}
ITypeParameter[] oldTps = Interlocked.CompareExchange(ref typeParameters, newTps, tps);
if (oldTps == tps) {
// exchange successful
tps = newTps;
} else {
// exchange not successful
tps = oldTps;
}
}
return tps[index];
}
readonly EntityType ownerType;
readonly int index;
private DummyTypeParameter(EntityType ownerType, int index)
{
this.ownerType = ownerType;
this.index = index;
}
public override string Name {
get { return "!" + index; }
}
public override bool? IsReferenceType {
get { return null; }
}
public override TypeKind Kind {
get { return TypeKind.TypeParameter; }
}
public override ITypeReference ToTypeReference()
{
return new TypeParameterReference(ownerType, index);
}
public int Index {
get { return index; }
}
IList<IAttribute> ITypeParameter.Attributes {
get { return EmptyList<IAttribute>.Instance; }
}
EntityType ITypeParameter.OwnerType {
get { return ownerType; }
}
VarianceModifier ITypeParameter.Variance {
get { return VarianceModifier.Invariant; }
}
DomRegion ITypeParameter.Region {
get { return DomRegion.Empty; }
}
IEntity ITypeParameter.Owner {
get { return null; }
}
IType ITypeParameter.EffectiveBaseClass {
get { return SpecialType.UnknownType; }
}
IList<IType> ITypeParameter.EffectiveInterfaceSet {
get { return EmptyList<IType>.Instance; }
}
bool ITypeParameter.HasDefaultConstructorConstraint {
get { return false; }
}
bool ITypeParameter.HasReferenceTypeConstraint {
get { return false; }
}
bool ITypeParameter.HasValueTypeConstraint {
get { return false; }
}
}
}