2 // Copyright (c) 2003, 2004, 2005 Rodrigo B. de Oliveira (rbo@acm.org)
3 // All rights reserved.
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
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.
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.
29 namespace Boo
.Lang
.Compiler
.TypeSystem
32 using System
.Collections
.Generic
;
34 public abstract class AbstractExternalGenericInfo
<T
> where T
: IEntity
36 protected TypeSystemServices _tss
;
37 private IGenericParameter
[] _parameters
;
38 private Dictionary
<IType
[], T
> _instances
= new Dictionary
<IType
[], T
>(ArrayEqualityComparer
<IType
>.Default
);
40 public AbstractExternalGenericInfo(TypeSystemServices tss
)
45 public IGenericParameter
[] GenericParameters
49 if (null == _parameters
)
51 _parameters
= Array
.ConvertAll
<Type
, ExternalGenericParameter
>(
52 GetActualGenericParameters(),
53 delegate(Type t
) { return (ExternalGenericParameter)_tss.Map(t); }
);
59 protected T
ConstructEntity(IType
[] arguments
)
61 if (Array
.TrueForAll(arguments
, IsExternal
))
63 Type
[] actualTypes
= Array
.ConvertAll
<IType
, Type
>(arguments
, GetSystemType
);
65 return ConstructExternalEntity(actualTypes
);
67 else if (_instances
.ContainsKey(arguments
))
69 return _instances
[arguments
];
73 T instance
= ConstructInternalEntity(arguments
);
74 _instances
.Add(arguments
, instance
);
80 protected abstract Type
[] GetActualGenericParameters();
81 protected abstract T
ConstructInternalEntity(IType
[] arguments
);
82 protected abstract T
ConstructExternalEntity(Type
[] arguments
);
84 private bool IsExternal(IType type
)
86 if (type
is ExternalType
)
91 if (type
is ArrayType
)
93 return IsExternal(type
.GetElementType());
99 private Type
GetSystemType(IType type
)
101 // Get system type from external types
102 if (type
is ExternalType
)
104 return ((ExternalType
)type
).ActualType
;
107 // Get system array types from arrays of external types
108 ArrayType arrayType
= type
as ArrayType
;
109 if (arrayType
!= null)
111 Type elementType
= GetSystemType(arrayType
.GetElementType());
112 int rank
= arrayType
.GetArrayRank();
114 // Calling MakeArrayType(1) gives a multi-dimensional array with 1 dimensions,
115 // which is (surprisingly) not the same as calling MakeArrayType() which gives
116 // a single-dimensional array
117 return (rank
== 1 ? elementType
.MakeArrayType() : elementType
.MakeArrayType(rank
));
120 // This shouldn't happen since we only call GetSystemType on external types or arrays of such