public override bool Equals(IReturnType rt) { if (rt == null || !rt.IsArrayReturnType) { return(false); } ArrayReturnType art = rt.CastToArrayReturnType(); if (art.ArrayDimensions != dimensions) { return(false); } return(elementType.Equals(art.ArrayElementType)); }
public static IReturnType TranslateType(IReturnType input, IList <IReturnType> typeParameters, bool convertForMethod) { if (input == null || typeParameters == null || typeParameters.Count == 0) { return(input); // nothing to do when there are no type parameters specified } if (input.IsGenericReturnType) { GenericReturnType rt = input.CastToGenericReturnType(); if (convertForMethod ? (rt.TypeParameter.Method != null) : (rt.TypeParameter.Method == null)) { if (rt.TypeParameter.Index < typeParameters.Count) { IReturnType newType = typeParameters[rt.TypeParameter.Index]; if (newType != null) { return(newType); } } } } else if (input.IsArrayReturnType) { ArrayReturnType arInput = input.CastToArrayReturnType(); IReturnType e = arInput.ArrayElementType; IReturnType t = TranslateType(e, typeParameters, convertForMethod); if (e != t && t != null) { return(new ArrayReturnType(arInput.ProjectContent, t, arInput.ArrayDimensions)); } } else if (input.IsConstructedReturnType) { ConstructedReturnType cinput = input.CastToConstructedReturnType(); List <IReturnType> para = new List <IReturnType>(cinput.TypeArguments.Count); foreach (IReturnType argument in cinput.TypeArguments) { para.Add(TranslateType(argument, typeParameters, convertForMethod)); } return(new ConstructedReturnType(cinput.UnboundType, para)); } return(input); }
/// <summary> /// Tests if an implicit conversion exists from "from" to "to". /// Conversions from concrete types to generic types are only allowed when the generic type belongs to the /// method "allowGenericTargetsOnThisMethod". /// </summary> static bool ConversionExistsInternal(IReturnType from, IReturnType to, IMethod allowGenericTargetsOnThisMethod) { // ECMA-334, § 13.1 Implicit conversions // Identity conversion: if (from == to) { return(true); } if (from == null || to == null) { return(false); } if (from.Equals(to)) { return(true); } bool fromIsDefault = from.IsDefaultReturnType; bool toIsDefault = to.IsDefaultReturnType; if (fromIsDefault && toIsDefault) { // Implicit numeric conversions: int f = GetPrimitiveType(from); int t = GetPrimitiveType(to); if (f == SByte && (t == Short || t == Int || t == Long || t == Float || t == Double || t == Decimal)) { return(true); } if (f == Byte && (t == Short || t == UShort || t == Int || t == UInt || t == Long || t == ULong || t == Float || t == Double || t == Decimal)) { return(true); } if (f == Short && (t == Int || t == Long || t == Float || t == Double || t == Decimal)) { return(true); } if (f == UShort && (t == Int || t == UInt || t == Long || t == ULong || t == Float || t == Double || t == Decimal)) { return(true); } if (f == Int && (t == Long || t == Float || t == Double || t == Decimal)) { return(true); } if (f == UInt && (t == Long || t == ULong || t == Float || t == Double || t == Decimal)) { return(true); } if ((f == Long || f == ULong) && (t == Float || t == Double || t == Decimal)) { return(true); } if (f == Char && (t == UShort || t == Int || t == UInt || t == Long || t == ULong || t == Float || t == Double || t == Decimal)) { return(true); } if (f == Float && t == Double) { return(true); } } // Implicit reference conversions: if (toIsDefault && to.FullyQualifiedName == "System.Object") { return(true); // from any type to object } if (from == NullReturnType.Instance) { IClass toClass = to.GetUnderlyingClass(); if (toClass != null) { switch (toClass.ClassType) { case ClassType.Class: case ClassType.Delegate: case ClassType.Interface: return(true); case ClassType.Struct: return(toClass.FullyQualifiedName == "System.Nullable"); } } return(false); } if ((toIsDefault || to.IsConstructedReturnType || to.IsGenericReturnType) && (fromIsDefault || from.IsArrayReturnType || from.IsConstructedReturnType)) { foreach (IReturnType baseTypeOfFrom in GetTypeInheritanceTree(from)) { if (IsConstructedConversionToGenericReturnType(baseTypeOfFrom, to, allowGenericTargetsOnThisMethod)) { return(true); } } } if (from.IsArrayReturnType && to.IsArrayReturnType) { ArrayReturnType fromArt = from.CastToArrayReturnType(); ArrayReturnType toArt = to.CastToArrayReturnType(); // from array to other array type if (fromArt.ArrayDimensions == toArt.ArrayDimensions) { return(ConversionExistsInternal(fromArt.ArrayElementType, toArt.ArrayElementType, allowGenericTargetsOnThisMethod)); } } if (from.IsDecoratingReturnType <AnonymousMethodReturnType>() && (toIsDefault || to.IsConstructedReturnType)) { AnonymousMethodReturnType amrt = from.CastToDecoratingReturnType <AnonymousMethodReturnType>(); IMethod method = CSharp.TypeInference.GetDelegateOrExpressionTreeSignature(to, amrt.CanBeConvertedToExpressionTree); if (method != null) { if (amrt.HasParameterList) { if (amrt.MethodParameters.Count != method.Parameters.Count) { return(false); } for (int i = 0; i < amrt.MethodParameters.Count; i++) { if (amrt.MethodParameters[i].ReturnType != null) { if (!object.Equals(amrt.MethodParameters[i].ReturnType, method.Parameters[i].ReturnType)) { return(false); } } } } IReturnType rt = amrt.ResolveReturnType(method.Parameters.Select(p => p.ReturnType).ToArray()); return(ConversionExistsInternal(rt, method.ReturnType, allowGenericTargetsOnThisMethod)); } } return(false); }