1
ļ»æ// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
20
using System.Collections.Generic;
21
using System.Collections.ObjectModel;
22
using System.Diagnostics;
26
using ICSharpCode.NRefactory.TypeSystem.Implementation;
28
namespace ICSharpCode.NRefactory.TypeSystem
31
/// ParameterizedType represents an instance of a generic type.
32
/// Example: List<string>
35
/// When getting the members, this type modifies the lists so that
36
/// type parameters in the signatures of the members are replaced with
37
/// the type arguments.
40
public sealed class ParameterizedType : IType, ICompilationProvider
42
readonly ITypeDefinition genericType;
43
readonly IType[] typeArguments;
45
public ParameterizedType(ITypeDefinition genericType, IEnumerable<IType> typeArguments)
47
if (genericType == null)
48
throw new ArgumentNullException("genericType");
49
if (typeArguments == null)
50
throw new ArgumentNullException("typeArguments");
51
this.genericType = genericType;
52
this.typeArguments = typeArguments.ToArray(); // copy input array to ensure it isn't modified
53
if (this.typeArguments.Length == 0)
54
throw new ArgumentException("Cannot use ParameterizedType with 0 type arguments.");
55
if (genericType.TypeParameterCount != this.typeArguments.Length)
56
throw new ArgumentException("Number of type arguments must match the type definition's number of type parameters");
57
for (int i = 0; i < this.typeArguments.Length; i++) {
58
if (this.typeArguments[i] == null)
59
throw new ArgumentNullException("typeArguments[" + i + "]");
60
ICompilationProvider p = this.typeArguments[i] as ICompilationProvider;
61
if (p != null && p.Compilation != genericType.Compilation)
62
throw new InvalidOperationException("Cannot parameterize a type with type arguments from a different compilation.");
67
/// Fast internal version of the constructor. (no safety checks)
68
/// Keeps the array that was passed and assumes it won't be modified.
70
internal ParameterizedType(ITypeDefinition genericType, IType[] typeArguments)
72
Debug.Assert(genericType.TypeParameterCount == typeArguments.Length);
73
this.genericType = genericType;
74
this.typeArguments = typeArguments;
77
public TypeKind Kind {
78
get { return genericType.Kind; }
81
public ICompilation Compilation {
82
get { return genericType.Compilation; }
85
public bool? IsReferenceType {
86
get { return genericType.IsReferenceType; }
89
public IType DeclaringType {
91
ITypeDefinition declaringTypeDef = genericType.DeclaringTypeDefinition;
92
if (declaringTypeDef != null && declaringTypeDef.TypeParameterCount > 0
93
&& declaringTypeDef.TypeParameterCount <= genericType.TypeParameterCount)
95
IType[] newTypeArgs = new IType[declaringTypeDef.TypeParameterCount];
96
Array.Copy(this.typeArguments, 0, newTypeArgs, 0, newTypeArgs.Length);
97
return new ParameterizedType(declaringTypeDef, newTypeArgs);
99
return declaringTypeDef;
103
public int TypeParameterCount {
104
get { return typeArguments.Length; }
107
public string FullName {
108
get { return genericType.FullName; }
112
get { return genericType.Name; }
115
public string Namespace {
116
get { return genericType.Namespace; }
119
public string ReflectionName {
121
StringBuilder b = new StringBuilder(genericType.ReflectionName);
123
for (int i = 0; i < typeArguments.Length; i++) {
127
b.Append(typeArguments[i].ReflectionName);
135
public override string ToString()
137
return ReflectionName;
140
public IList<IType> TypeArguments {
142
return typeArguments;
146
public bool IsParameterized {
153
/// Same as 'parameterizedType.TypeArguments[index]', but is a bit more efficient (doesn't require the read-only wrapper).
155
public IType GetTypeArgument(int index)
157
return typeArguments[index];
161
/// Gets the definition of the generic type.
162
/// For <c>ParameterizedType</c>, this method never returns null.
164
public ITypeDefinition GetDefinition()
169
public ITypeReference ToTypeReference()
171
return new ParameterizedTypeReference(genericType.ToTypeReference(), typeArguments.Select(t => t.ToTypeReference()));
175
/// Gets a type visitor that performs the substitution of class type parameters with the type arguments
176
/// of this parameterized type.
178
public TypeParameterSubstitution GetSubstitution()
180
return new TypeParameterSubstitution(typeArguments, null);
184
/// Gets a type visitor that performs the substitution of class type parameters with the type arguments
185
/// of this parameterized type,
186
/// and also substitutes method type parameters with the specified method type arguments.
188
public TypeParameterSubstitution GetSubstitution(IList<IType> methodTypeArguments)
190
return new TypeParameterSubstitution(typeArguments, methodTypeArguments);
193
public IEnumerable<IType> DirectBaseTypes {
195
var substitution = GetSubstitution();
196
return genericType.DirectBaseTypes.Select(t => t.AcceptVisitor(substitution));
200
public IEnumerable<IType> GetNestedTypes(Predicate<ITypeDefinition> filter = null, GetMemberOptions options = GetMemberOptions.None)
202
if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions)
203
return genericType.GetNestedTypes(filter, options);
205
return GetMembersHelper.GetNestedTypes(this, filter, options);
208
public IEnumerable<IType> GetNestedTypes(IList<IType> typeArguments, Predicate<ITypeDefinition> filter = null, GetMemberOptions options = GetMemberOptions.None)
210
if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions)
211
return genericType.GetNestedTypes(typeArguments, filter, options);
213
return GetMembersHelper.GetNestedTypes(this, typeArguments, filter, options);
216
public IEnumerable<IMethod> GetConstructors(Predicate<IUnresolvedMethod> filter = null, GetMemberOptions options = GetMemberOptions.IgnoreInheritedMembers)
218
if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions)
219
return genericType.GetConstructors(filter, options);
221
return GetMembersHelper.GetConstructors(this, filter, options);
224
public IEnumerable<IMethod> GetMethods(Predicate<IUnresolvedMethod> filter = null, GetMemberOptions options = GetMemberOptions.None)
226
if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions)
227
return genericType.GetMethods(filter, options);
229
return GetMembersHelper.GetMethods(this, filter, options);
232
public IEnumerable<IMethod> GetMethods(IList<IType> typeArguments, Predicate<IUnresolvedMethod> filter = null, GetMemberOptions options = GetMemberOptions.None)
234
if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions)
235
return genericType.GetMethods(typeArguments, filter, options);
237
return GetMembersHelper.GetMethods(this, typeArguments, filter, options);
240
public IEnumerable<IProperty> GetProperties(Predicate<IUnresolvedProperty> filter = null, GetMemberOptions options = GetMemberOptions.None)
242
if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions)
243
return genericType.GetProperties(filter, options);
245
return GetMembersHelper.GetProperties(this, filter, options);
248
public IEnumerable<IField> GetFields(Predicate<IUnresolvedField> filter = null, GetMemberOptions options = GetMemberOptions.None)
250
if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions)
251
return genericType.GetFields(filter, options);
253
return GetMembersHelper.GetFields(this, filter, options);
256
public IEnumerable<IEvent> GetEvents(Predicate<IUnresolvedEvent> filter = null, GetMemberOptions options = GetMemberOptions.None)
258
if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions)
259
return genericType.GetEvents(filter, options);
261
return GetMembersHelper.GetEvents(this, filter, options);
264
public IEnumerable<IMember> GetMembers(Predicate<IUnresolvedMember> filter = null, GetMemberOptions options = GetMemberOptions.None)
266
if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions)
267
return genericType.GetMembers(filter, options);
269
return GetMembersHelper.GetMembers(this, filter, options);
272
public IEnumerable<IMethod> GetAccessors(Predicate<IUnresolvedMethod> filter = null, GetMemberOptions options = GetMemberOptions.None)
274
if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions)
275
return genericType.GetAccessors(filter, options);
277
return GetMembersHelper.GetAccessors(this, filter, options);
280
public override bool Equals(object obj)
282
return Equals(obj as IType);
285
public bool Equals(IType other)
287
ParameterizedType c = other as ParameterizedType;
288
if (c == null || !genericType.Equals(c.genericType) || typeArguments.Length != c.typeArguments.Length)
290
for (int i = 0; i < typeArguments.Length; i++) {
291
if (!typeArguments[i].Equals(c.typeArguments[i]))
297
public override int GetHashCode()
299
int hashCode = genericType.GetHashCode();
301
foreach (var ta in typeArguments) {
302
hashCode *= 1000000007;
303
hashCode += 1000000009 * ta.GetHashCode();
309
public IType AcceptVisitor(TypeVisitor visitor)
311
return visitor.VisitParameterizedType(this);
314
public IType VisitChildren(TypeVisitor visitor)
316
IType g = genericType.AcceptVisitor(visitor);
317
ITypeDefinition def = g as ITypeDefinition;
320
// Keep ta == null as long as no elements changed, allocate the array only if necessary.
321
IType[] ta = (g != genericType) ? new IType[typeArguments.Length] : null;
322
for (int i = 0; i < typeArguments.Length; i++) {
323
IType r = typeArguments[i].AcceptVisitor(visitor);
325
throw new NullReferenceException("TypeVisitor.Visit-method returned null");
326
if (ta == null && r != typeArguments[i]) {
327
// we found a difference, so we need to allocate the array
328
ta = new IType[typeArguments.Length];
329
for (int j = 0; j < i; j++) {
330
ta[j] = typeArguments[j];
336
if (def == genericType && ta == null)
339
return new ParameterizedType(def, ta ?? typeArguments);
344
/// ParameterizedTypeReference is a reference to generic class that specifies the type parameters.
345
/// Example: List<string>
348
public sealed class ParameterizedTypeReference : ITypeReference, ISupportsInterning
350
readonly ITypeReference genericType;
351
readonly ITypeReference[] typeArguments;
353
public ParameterizedTypeReference(ITypeReference genericType, IEnumerable<ITypeReference> typeArguments)
355
if (genericType == null)
356
throw new ArgumentNullException("genericType");
357
if (typeArguments == null)
358
throw new ArgumentNullException("typeArguments");
359
this.genericType = genericType;
360
this.typeArguments = typeArguments.ToArray();
361
for (int i = 0; i < this.typeArguments.Length; i++) {
362
if (this.typeArguments[i] == null)
363
throw new ArgumentNullException("typeArguments[" + i + "]");
367
public ITypeReference GenericType {
368
get { return genericType; }
371
public ReadOnlyCollection<ITypeReference> TypeArguments {
373
return Array.AsReadOnly(typeArguments);
377
public IType Resolve(ITypeResolveContext context)
379
IType baseType = genericType.Resolve(context);
380
ITypeDefinition baseTypeDef = baseType.GetDefinition();
381
if (baseTypeDef == null)
383
int tpc = baseTypeDef.TypeParameterCount;
386
IType[] resolvedTypes = new IType[tpc];
387
for (int i = 0; i < resolvedTypes.Length; i++) {
388
if (i < typeArguments.Length)
389
resolvedTypes[i] = typeArguments[i].Resolve(context);
391
resolvedTypes[i] = SpecialType.UnknownType;
393
return new ParameterizedType(baseTypeDef, resolvedTypes);
396
public override string ToString()
398
StringBuilder b = new StringBuilder(genericType.ToString());
400
for (int i = 0; i < typeArguments.Length; i++) {
404
b.Append(typeArguments[i].ToString());
411
int ISupportsInterning.GetHashCodeForInterning()
413
int hashCode = genericType.GetHashCode();
415
foreach (ITypeReference t in typeArguments) {
417
hashCode += t.GetHashCode();
423
bool ISupportsInterning.EqualsForInterning(ISupportsInterning other)
425
ParameterizedTypeReference o = other as ParameterizedTypeReference;
426
if (o != null && genericType == o.genericType && typeArguments.Length == o.typeArguments.Length) {
427
for (int i = 0; i < typeArguments.Length; i++) {
428
if (typeArguments[i] != o.typeArguments[i])