BOO-999
[boo.git] / src / Boo.Lang.Compiler / TypeSystem / ArrayType.cs
blob61976d2a32eab79ad7e7e5361dafe21884da3813
1 #region license
2 // Copyright (c) 2004, Rodrigo B. de Oliveira (rbo@acm.org)
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of Rodrigo B. de Oliveira nor the names of its
14 // contributors may be used to endorse or promote products derived from this
15 // software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #endregion
29 namespace Boo.Lang.Compiler.TypeSystem
31 public class ArrayType : IArrayType
33 IType _elementType;
35 IType _array;
37 int _rank;
39 IType _enumerable;
41 public ArrayType(TypeSystemServices tagManager, IType elementType) : this(tagManager, elementType, 1)
45 public ArrayType(TypeSystemServices tagManager, IType elementType, int rank)
47 _array = tagManager.ArrayType;
48 _elementType = elementType;
49 _rank = rank;
50 _enumerable = tagManager.IEnumerableGenericType;
54 public string Name
56 get
58 if (_rank > 1)
60 return "(" + _elementType.ToString() + ", " + _rank + ")";
62 return "(" + _elementType.ToString() + ")";
66 public EntityType EntityType
68 get
70 return EntityType.Array;
74 public string FullName
76 get
78 return Name;
82 public IType Type
84 get
86 return this;
90 public bool IsFinal
92 get
94 return true;
98 public bool IsByRef
102 return false;
106 public bool IsClass
110 return false;
114 public bool IsInterface
118 return false;
122 public bool IsAbstract
126 return false;
130 public bool IsEnum
134 return false;
138 public bool IsValueType
142 return false;
146 public bool IsArray
150 return true;
154 public int GetTypeDepth()
156 return 2;
159 public int GetArrayRank()
161 return _rank;
164 public IType GetElementType()
166 return _elementType;
169 public IType BaseType
173 return _array;
177 public IEntity GetDefaultMember()
179 return null;
182 public virtual bool IsSubclassOf(IType other)
184 if (other.IsAssignableFrom(_array)) return true;
186 // Arrays also implement generic IEnumerable of their element type
187 if (other.ConstructedInfo != null &&
188 other.ConstructedInfo.GenericDefinition == _enumerable &&
189 other.ConstructedInfo.GenericArguments[0].IsAssignableFrom(_elementType))
191 return true;
193 return false;
196 public virtual bool IsAssignableFrom(IType other)
198 if (other == this || other == Null.Default)
200 return true;
203 if (other.IsArray)
205 IArrayType otherArray = (IArrayType)other;
207 if (otherArray.GetArrayRank() != _rank)
209 return false;
212 IType otherEntityType = otherArray.GetElementType();
213 if (_elementType.IsValueType || otherEntityType.IsValueType)
215 return _elementType == otherEntityType;
217 return _elementType.IsAssignableFrom(otherEntityType);
220 return false;
223 public IConstructor[] GetConstructors()
225 return new IConstructor[0];
228 public IType[] GetInterfaces()
230 return null;
233 public IEntity[] GetMembers()
235 return _array.GetMembers();
238 public INamespace ParentNamespace
242 return _array.ParentNamespace;
246 public bool Resolve(List targetList, string name, EntityType flags)
248 return _array.Resolve(targetList, name, flags);
251 override public string ToString()
253 return Name;
256 IGenericTypeInfo IType.GenericInfo
258 get { return null; }
261 IConstructedTypeInfo IType.ConstructedInfo
263 get { return null; }