~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory/TypeSystem/Implementation/DummyTypeParameter.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
ļ»æ// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
 
2
// 
 
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
 
4
// software and associated documentation files (the "Software"), to deal in the Software
 
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
 
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
 
7
// to whom the Software is furnished to do so, subject to the following conditions:
 
8
// 
 
9
// The above copyright notice and this permission notice shall be included in all copies or
 
10
// substantial portions of the Software.
 
11
// 
 
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
17
// DEALINGS IN THE SOFTWARE.
 
18
 
 
19
using System;
 
20
using System.Collections.Generic;
 
21
using System.Threading;
 
22
 
 
23
namespace ICSharpCode.NRefactory.TypeSystem.Implementation
 
24
{
 
25
        public sealed class DummyTypeParameter : AbstractType, ITypeParameter
 
26
        {
 
27
                static ITypeParameter[] methodTypeParameters = { new DummyTypeParameter(EntityType.Method, 0) };
 
28
                static ITypeParameter[] classTypeParameters = { new DummyTypeParameter(EntityType.TypeDefinition, 0) };
 
29
                
 
30
                public static ITypeParameter GetMethodTypeParameter(int index)
 
31
                {
 
32
                        return GetTypeParameter(ref methodTypeParameters, EntityType.Method, index);
 
33
                }
 
34
                
 
35
                public static ITypeParameter GetClassTypeParameter(int index)
 
36
                {
 
37
                        return GetTypeParameter(ref classTypeParameters, EntityType.TypeDefinition, index);
 
38
                }
 
39
                
 
40
                static ITypeParameter GetTypeParameter(ref ITypeParameter[] typeParameters, EntityType entityType, int index)
 
41
                {
 
42
                        ITypeParameter[] tps = typeParameters;
 
43
                        while (index >= tps.Length) {
 
44
                                // We don't have a normal type parameter for this index, so we need to extend our array.
 
45
                                // Because the array can be used concurrently from multiple threads, we have to use
 
46
                                // Interlocked.CompareExchange.
 
47
                                ITypeParameter[] newTps = new ITypeParameter[index + 1];
 
48
                                tps.CopyTo(newTps, 0);
 
49
                                for (int i = tps.Length; i < newTps.Length; i++) {
 
50
                                        newTps[i] = new DummyTypeParameter(entityType, i);
 
51
                                }
 
52
                                ITypeParameter[] oldTps = Interlocked.CompareExchange(ref typeParameters, newTps, tps);
 
53
                                if (oldTps == tps) {
 
54
                                        // exchange successful
 
55
                                        tps = newTps;
 
56
                                } else {
 
57
                                        // exchange not successful
 
58
                                        tps = oldTps;
 
59
                                }
 
60
                        }
 
61
                        return tps[index];
 
62
                }
 
63
                
 
64
                sealed class NormalizeMethodTypeParametersVisitor : TypeVisitor
 
65
                {
 
66
                        public override IType VisitTypeParameter(ITypeParameter type)
 
67
                        {
 
68
                                if (type.OwnerType == EntityType.Method) {
 
69
                                        return DummyTypeParameter.GetMethodTypeParameter(type.Index);
 
70
                                } else {
 
71
                                        return base.VisitTypeParameter(type);
 
72
                                }
 
73
                        }
 
74
                }
 
75
                sealed class NormalizeClassTypeParametersVisitor : TypeVisitor
 
76
                {
 
77
                        public override IType VisitTypeParameter(ITypeParameter type)
 
78
                        {
 
79
                                if (type.OwnerType == EntityType.TypeDefinition) {
 
80
                                        return DummyTypeParameter.GetClassTypeParameter(type.Index);
 
81
                                } else {
 
82
                                        return base.VisitTypeParameter(type);
 
83
                                }
 
84
                        }
 
85
                }
 
86
                
 
87
                static readonly NormalizeMethodTypeParametersVisitor normalizeMethodTypeParameters = new NormalizeMethodTypeParametersVisitor();
 
88
                static readonly NormalizeClassTypeParametersVisitor normalizeClassTypeParameters = new NormalizeClassTypeParametersVisitor();
 
89
                
 
90
                /// <summary>
 
91
                /// Replaces all occurrences of method type parameters in the given type
 
92
                /// by normalized type parameters. This allows comparing parameter types from different
 
93
                /// generic methods.
 
94
                /// </summary>
 
95
                public static IType NormalizeMethodTypeParameters(IType type)
 
96
                {
 
97
                        return type.AcceptVisitor(normalizeMethodTypeParameters);
 
98
                }
 
99
                
 
100
                /// <summary>
 
101
                /// Replaces all occurrences of class type parameters in the given type
 
102
                /// by normalized type parameters. This allows comparing parameter types from different
 
103
                /// generic methods.
 
104
                /// </summary>
 
105
                public static IType NormalizeClassTypeParameters(IType type)
 
106
                {
 
107
                        return type.AcceptVisitor(normalizeClassTypeParameters);
 
108
                }
 
109
                
 
110
                /// <summary>
 
111
                /// Replaces all occurrences of class and method type parameters in the given type
 
112
                /// by normalized type parameters. This allows comparing parameter types from different
 
113
                /// generic methods.
 
114
                /// </summary>
 
115
                public static IType NormalizeAllTypeParameters(IType type)
 
116
                {
 
117
                        return type.AcceptVisitor(normalizeClassTypeParameters).AcceptVisitor(normalizeMethodTypeParameters);
 
118
                }
 
119
                
 
120
                readonly EntityType ownerType;
 
121
                readonly int index;
 
122
                
 
123
                private DummyTypeParameter(EntityType ownerType, int index)
 
124
                {
 
125
                        this.ownerType = ownerType;
 
126
                        this.index = index;
 
127
                }
 
128
                
 
129
                public override string Name {
 
130
                        get {
 
131
                                return (ownerType == EntityType.Method ? "!!" : "!") + index;
 
132
                        }
 
133
                }
 
134
                
 
135
                public override string ReflectionName {
 
136
                        get {
 
137
                                return (ownerType == EntityType.Method ? "``" : "`") + index;
 
138
                        }
 
139
                }
 
140
                
 
141
                public override string ToString()
 
142
                {
 
143
                        return ReflectionName + " (dummy)";
 
144
                }
 
145
                
 
146
                public override bool? IsReferenceType {
 
147
                        get { return null; }
 
148
                }
 
149
                
 
150
                public override TypeKind Kind {
 
151
                        get { return TypeKind.TypeParameter; }
 
152
                }
 
153
                
 
154
                public override ITypeReference ToTypeReference()
 
155
                {
 
156
                        return TypeParameterReference.Create(ownerType, index);
 
157
                }
 
158
                
 
159
                public override IType AcceptVisitor(TypeVisitor visitor)
 
160
                {
 
161
                        return visitor.VisitTypeParameter(this);
 
162
                }
 
163
                
 
164
                public int Index {
 
165
                        get { return index; }
 
166
                }
 
167
                
 
168
                IList<IAttribute> ITypeParameter.Attributes {
 
169
                        get { return EmptyList<IAttribute>.Instance; }
 
170
                }
 
171
                
 
172
                EntityType ITypeParameter.OwnerType {
 
173
                        get { return ownerType; }
 
174
                }
 
175
                
 
176
                VarianceModifier ITypeParameter.Variance {
 
177
                        get { return VarianceModifier.Invariant; }
 
178
                }
 
179
                
 
180
                DomRegion ITypeParameter.Region {
 
181
                        get { return DomRegion.Empty; }
 
182
                }
 
183
                
 
184
                IEntity ITypeParameter.Owner {
 
185
                        get { return null; }
 
186
                }
 
187
                
 
188
                IType ITypeParameter.EffectiveBaseClass {
 
189
                        get { return SpecialType.UnknownType; }
 
190
                }
 
191
                
 
192
                ICollection<IType> ITypeParameter.EffectiveInterfaceSet {
 
193
                        get { return EmptyList<IType>.Instance; }
 
194
                }
 
195
                
 
196
                bool ITypeParameter.HasDefaultConstructorConstraint {
 
197
                        get { return false; }
 
198
                }
 
199
                
 
200
                bool ITypeParameter.HasReferenceTypeConstraint {
 
201
                        get { return false; }
 
202
                }
 
203
                
 
204
                bool ITypeParameter.HasValueTypeConstraint {
 
205
                        get { return false; }
 
206
                }
 
207
        }
 
208
}