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 ICSharpCode.NRefactory.TypeSystem.Implementation;
23
namespace ICSharpCode.NRefactory.TypeSystem
26
/// Represents an array type.
28
public sealed class ArrayType : TypeWithElementType, ICompilationProvider
30
readonly int dimensions;
31
readonly ICompilation compilation;
33
public ArrayType(ICompilation compilation, IType elementType, int dimensions = 1) : base(elementType)
35
if (compilation == null)
36
throw new ArgumentNullException("compilation");
38
throw new ArgumentOutOfRangeException("dimensions", dimensions, "dimensions must be positive");
39
this.compilation = compilation;
40
this.dimensions = dimensions;
42
ICompilationProvider p = elementType as ICompilationProvider;
43
if (p != null && p.Compilation != compilation)
44
throw new InvalidOperationException("Cannot create an array type using a different compilation from the element type.");
47
public override TypeKind Kind {
48
get { return TypeKind.Array; }
51
public ICompilation Compilation {
52
get { return compilation; }
55
public int Dimensions {
56
get { return dimensions; }
59
public override string NameSuffix {
61
return "[" + new string(',', dimensions-1) + "]";
65
public override bool? IsReferenceType {
69
public override int GetHashCode()
71
return unchecked(elementType.GetHashCode() * 71681 + dimensions);
74
public override bool Equals(IType other)
76
ArrayType a = other as ArrayType;
77
return a != null && elementType.Equals(a.elementType) && a.dimensions == dimensions;
80
public override ITypeReference ToTypeReference()
82
return new ArrayTypeReference(elementType.ToTypeReference(), dimensions);
85
public override IEnumerable<IType> DirectBaseTypes {
87
List<IType> baseTypes = new List<IType>();
88
IType t = compilation.FindType(KnownTypeCode.Array);
89
if (t.Kind != TypeKind.Unknown)
91
if (dimensions == 1 && elementType.Kind != TypeKind.Pointer) {
92
// single-dimensional arrays implement IList<T>
93
ITypeDefinition def = compilation.FindType(KnownTypeCode.IListOfT) as ITypeDefinition;
95
baseTypes.Add(new ParameterizedType(def, new[] { elementType }));
96
// And in .NET 4.5 they also implement IReadOnlyList<T>
97
def = compilation.FindType(KnownTypeCode.IReadOnlyListOfT) as ITypeDefinition;
99
baseTypes.Add(new ParameterizedType(def, new[] { elementType }));
105
public override IEnumerable<IMethod> GetMethods(Predicate<IUnresolvedMethod> filter = null, GetMemberOptions options = GetMemberOptions.None)
107
if ((options & GetMemberOptions.IgnoreInheritedMembers) == GetMemberOptions.IgnoreInheritedMembers)
108
return EmptyList<IMethod>.Instance;
110
return compilation.FindType(KnownTypeCode.Array).GetMethods(filter, options);
113
public override IEnumerable<IMethod> GetMethods(IList<IType> typeArguments, Predicate<IUnresolvedMethod> filter = null, GetMemberOptions options = GetMemberOptions.None)
115
if ((options & GetMemberOptions.IgnoreInheritedMembers) == GetMemberOptions.IgnoreInheritedMembers)
116
return EmptyList<IMethod>.Instance;
118
return compilation.FindType(KnownTypeCode.Array).GetMethods(typeArguments, filter, options);
121
public override IEnumerable<IMethod> GetAccessors(Predicate<IUnresolvedMethod> filter = null, GetMemberOptions options = GetMemberOptions.None)
123
if ((options & GetMemberOptions.IgnoreInheritedMembers) == GetMemberOptions.IgnoreInheritedMembers)
124
return EmptyList<IMethod>.Instance;
126
return compilation.FindType(KnownTypeCode.Array).GetAccessors(filter, options);
129
public override IEnumerable<IProperty> GetProperties(Predicate<IUnresolvedProperty> filter = null, GetMemberOptions options = GetMemberOptions.None)
131
if ((options & GetMemberOptions.IgnoreInheritedMembers) == GetMemberOptions.IgnoreInheritedMembers)
132
return EmptyList<IProperty>.Instance;
134
return compilation.FindType(KnownTypeCode.Array).GetProperties(filter, options);
137
// NestedTypes, Events, Fields: System.Array doesn't have any; so we can use the AbstractType default implementation
138
// that simply returns an empty list
140
public override IType AcceptVisitor(TypeVisitor visitor)
142
return visitor.VisitArrayType(this);
145
public override IType VisitChildren(TypeVisitor visitor)
147
IType e = elementType.AcceptVisitor(visitor);
148
if (e == elementType)
151
return new ArrayType(compilation, e, dimensions);
156
public sealed class ArrayTypeReference : ITypeReference, ISupportsInterning
158
readonly ITypeReference elementType;
159
readonly int dimensions;
161
public ArrayTypeReference(ITypeReference elementType, int dimensions = 1)
163
if (elementType == null)
164
throw new ArgumentNullException("elementType");
166
throw new ArgumentOutOfRangeException("dimensions", dimensions, "dimensions must be positive");
167
this.elementType = elementType;
168
this.dimensions = dimensions;
171
public ITypeReference ElementType {
172
get { return elementType; }
175
public int Dimensions {
176
get { return dimensions; }
179
public IType Resolve(ITypeResolveContext context)
181
return new ArrayType(context.Compilation, elementType.Resolve(context), dimensions);
184
public override string ToString()
186
return elementType.ToString() + "[" + new string(',', dimensions - 1) + "]";
189
int ISupportsInterning.GetHashCodeForInterning()
191
return elementType.GetHashCode() ^ dimensions;
194
bool ISupportsInterning.EqualsForInterning(ISupportsInterning other)
196
ArrayTypeReference o = other as ArrayTypeReference;
197
return o != null && elementType == o.elementType && dimensions == o.dimensions;