예제 #1
0
        public override object VisitUnaryOperatorExpression(UnaryOperatorExpression unaryOperatorExpression, object data)
        {
            IReturnType type = ResolveType(unaryOperatorExpression.Expression);

            if (type == null)
            {
                return(null);
            }
            switch (unaryOperatorExpression.Op)
            {
            case UnaryOperatorType.AddressOf:
                return(CreateResolveResult(new PointerReturnType(type)));

            case UnaryOperatorType.Dereference:
                PointerReturnType prt = type.CastToDecoratingReturnType <PointerReturnType>();
                if (prt != null)
                {
                    return(CreateResolveResult(prt.BaseType));
                }
                else
                {
                    return(null);
                }

            default:
                return(CreateResolveResult(type));
            }
        }
예제 #2
0
 void WriteType(IReturnType rt)
 {
     if (rt == null)
     {
         writer.Write(NullRTReferenceCode);
         return;
     }
     if (rt.IsDefaultReturnType)
     {
         string name = rt.FullyQualifiedName;
         if (name == "System.Void")
         {
             writer.Write(VoidRTCode);
         }
         else
         {
             writer.Write(classIndices[new ClassNameTypeCountPair(rt)]);
         }
     }
     else if (rt.IsGenericReturnType)
     {
         GenericReturnType grt = rt.CastToGenericReturnType();
         if (grt.TypeParameter.Method != null)
         {
             writer.Write(MethodGenericRTCode);
         }
         else
         {
             writer.Write(TypeGenericRTCode);
         }
         writer.Write(grt.TypeParameter.Index);
     }
     else if (rt.IsArrayReturnType)
     {
         writer.Write(ArrayRTCode);
         writer.Write(rt.CastToArrayReturnType().ArrayDimensions);
         WriteType(rt.CastToArrayReturnType().ArrayElementType);
     }
     else if (rt.IsConstructedReturnType)
     {
         ConstructedReturnType crt = rt.CastToConstructedReturnType();
         writer.Write(ConstructedRTCode);
         WriteType(crt.UnboundType);
         writer.Write((byte)crt.TypeArguments.Count);
         foreach (IReturnType typeArgument in crt.TypeArguments)
         {
             WriteType(typeArgument);
         }
     }
     else if (rt.IsDecoratingReturnType <PointerReturnType>())
     {
         writer.Write(PointerRTCode);
         WriteType(rt.CastToDecoratingReturnType <PointerReturnType>().BaseType);
     }
     else
     {
         writer.Write(NullRTReferenceCode);
         LoggingService.Warn("Unknown return type: " + rt.ToString());
     }
 }
예제 #3
0
        public virtual T CastToDecoratingReturnType <T>() where T : DecoratingReturnType
        {
            IReturnType baseType = BaseType;

            using (var l = busyManager.Enter(this)) {
                return((l.Success && baseType != null) ? baseType.CastToDecoratingReturnType <T>() : null);
            }
        }
예제 #4
0
        public override bool Equals(IReturnType rt)
        {
            if (rt == null)
            {
                return(false);
            }
            PointerReturnType prt = rt.CastToDecoratingReturnType <PointerReturnType>();

            if (prt == null)
            {
                return(false);
            }
            return(baseType.Equals(prt.baseType));
        }
예제 #5
0
        public override bool Equals(IReturnType other)
        {
            if (other == null)
            {
                return(false);
            }
            AnonymousMethodReturnType o = other.CastToDecoratingReturnType <AnonymousMethodReturnType>();

            if (o == null)
            {
                return(false);
            }
            return(this.FullyQualifiedName == o.FullyQualifiedName);
        }
예제 #6
0
 public override T CastToDecoratingReturnType <T>()
 {
     if (typeof(T) == typeof(ReferenceReturnType))
     {
         return((T)(object)this);
     }
     else if (baseType != null)
     {
         return(baseType.CastToDecoratingReturnType <T>());
     }
     else
     {
         return(null);
     }
 }
        public virtual T CastToDecoratingReturnType <T>() where T : DecoratingReturnType
        {
            IReturnType baseType = BaseType;
            T           temp;

            if (baseType != null && TryEnter())
            {
                temp = baseType.CastToDecoratingReturnType <T>();
            }
            else
            {
                temp = null;
            }
            Leave();
            return(temp);
        }
예제 #8
0
        public override object VisitUnaryOperatorExpression(UnaryOperatorExpression unaryOperatorExpression, object data)
        {
            IReturnType type = ResolveType(unaryOperatorExpression.Expression);

            if (type == null)
            {
                return(null);
            }
            switch (unaryOperatorExpression.Op)
            {
            case UnaryOperatorType.AddressOf:
                return(CreateResolveResult(new PointerReturnType(type)));

            case UnaryOperatorType.Dereference:
                PointerReturnType prt = type.CastToDecoratingReturnType <PointerReturnType>();
                if (prt != null)
                {
                    return(CreateResolveResult(prt.BaseType));
                }
                else
                {
                    return(null);
                }

            case UnaryOperatorType.Await:
                var crt = type.CastToConstructedReturnType();
                if (crt != null && crt.Name == "Task" && crt.TypeArguments.Count == 1)
                {
                    return(CreateResolveResult(crt.TypeArguments[0]));
                }
                else
                {
                    return(null);
                }

            default:
                return(CreateResolveResult(type));
            }
        }
예제 #9
0
 void AddExternalType(IReturnType rt, List <ClassNameTypeCountPair> externalTypes, int classCount)
 {
     if (rt.IsDefaultReturnType)
     {
         ClassNameTypeCountPair pair = new ClassNameTypeCountPair(rt);
         if (!classIndices.ContainsKey(pair))
         {
             classIndices.Add(pair, externalTypes.Count + classCount);
             externalTypes.Add(pair);
         }
     }
     else if (rt.IsGenericReturnType)
     {
         // ignore
     }
     else if (rt.IsArrayReturnType)
     {
         AddExternalType(rt.CastToArrayReturnType().ArrayElementType, externalTypes, classCount);
     }
     else if (rt.IsConstructedReturnType)
     {
         AddExternalType(rt.CastToConstructedReturnType().UnboundType, externalTypes, classCount);
         foreach (IReturnType typeArgument in rt.CastToConstructedReturnType().TypeArguments)
         {
             AddExternalType(typeArgument, externalTypes, classCount);
         }
     }
     else if (rt.IsDecoratingReturnType <PointerReturnType>())
     {
         AddExternalType(rt.CastToDecoratingReturnType <PointerReturnType>().BaseType, externalTypes, classCount);
     }
     else
     {
         LoggingService.Warn("Unknown return type: " + rt.ToString());
     }
 }
			void WriteType(IReturnType rt)
			{
				if (rt == null) {
					writer.Write(NullRTReferenceCode);
					return;
				}
				if (rt.IsDefaultReturnType) {
					string name = rt.FullyQualifiedName;
					if (name == "System.Void") {
						writer.Write(VoidRTCode);
					} else {
						writer.Write(classIndices[new ClassNameTypeCountPair(rt)]);
					}
				} else if (rt.IsGenericReturnType) {
					GenericReturnType grt = rt.CastToGenericReturnType();
					if (grt.TypeParameter.Method != null) {
						writer.Write(MethodGenericRTCode);
					} else {
						writer.Write(TypeGenericRTCode);
					}
					writer.Write(grt.TypeParameter.Index);
				} else if (rt.IsArrayReturnType) {
					writer.Write(ArrayRTCode);
					writer.Write(rt.CastToArrayReturnType().ArrayDimensions);
					WriteType(rt.CastToArrayReturnType().ArrayElementType);
				} else if (rt.IsConstructedReturnType) {
					ConstructedReturnType crt = rt.CastToConstructedReturnType();
					writer.Write(ConstructedRTCode);
					WriteType(crt.UnboundType);
					writer.Write((byte)crt.TypeArguments.Count);
					foreach (IReturnType typeArgument in crt.TypeArguments) {
						WriteType(typeArgument);
					}
				} else if (rt.IsDecoratingReturnType<PointerReturnType>()) {
					writer.Write(PointerRTCode);
					WriteType(rt.CastToDecoratingReturnType<PointerReturnType>().BaseType);
				} else {
					writer.Write(NullRTReferenceCode);
					LoggingService.Warn("Unknown return type: " + rt.ToString());
				}
			}
			void AddExternalType(IReturnType rt, List<ClassNameTypeCountPair> externalTypes, int classCount)
			{
				if (rt.IsDefaultReturnType) {
					ClassNameTypeCountPair pair = new ClassNameTypeCountPair(rt);
					if (!classIndices.ContainsKey(pair)) {
						classIndices.Add(pair, externalTypes.Count + classCount);
						externalTypes.Add(pair);
					}
				} else if (rt.IsGenericReturnType) {
					// ignore
				} else if (rt.IsArrayReturnType) {
					AddExternalType(rt.CastToArrayReturnType().ArrayElementType, externalTypes, classCount);
				} else if (rt.IsConstructedReturnType) {
					AddExternalType(rt.CastToConstructedReturnType().UnboundType, externalTypes, classCount);
					foreach (IReturnType typeArgument in rt.CastToConstructedReturnType().TypeArguments) {
						AddExternalType(typeArgument, externalTypes, classCount);
					}
				} else if (rt.IsDecoratingReturnType<PointerReturnType>()) {
					AddExternalType(rt.CastToDecoratingReturnType<PointerReturnType>().BaseType, externalTypes, classCount);
				} else {
					LoggingService.Warn("Unknown return type: " + rt.ToString());
				}
			}
        public static TypeReference ConvertType(IReturnType returnType, ClassFinder context)
        {
            if (returnType == null)
            {
                return(TypeReference.Null);
            }
            if (returnType is NullReturnType)
            {
                return(TypeReference.Null);
            }

            ArrayReturnType arrayReturnType = returnType.CastToArrayReturnType();

            if (arrayReturnType != null)
            {
                TypeReference typeRef = ConvertType(arrayReturnType.ArrayElementType, context);
                int[]         rank    = typeRef.RankSpecifier ?? new int[0];
                Array.Resize(ref rank, rank.Length + 1);
                rank[rank.Length - 1] = arrayReturnType.ArrayDimensions - 1;
                typeRef.RankSpecifier = rank;
                return(typeRef);
            }
            PointerReturnType pointerReturnType = returnType.CastToDecoratingReturnType <PointerReturnType>();

            if (pointerReturnType != null)
            {
                TypeReference typeRef = ConvertType(pointerReturnType.BaseType, context);
                typeRef.PointerNestingLevel++;
                return(typeRef);
            }

            IList <IReturnType> typeArguments = EmptyList <IReturnType> .Instance;

            if (returnType.IsConstructedReturnType)
            {
                typeArguments = returnType.CastToConstructedReturnType().TypeArguments;
            }
            IClass c = returnType.GetUnderlyingClass();

            if (c != null)
            {
                return(CreateTypeReference(c, typeArguments, context));
            }
            else
            {
                TypeReference typeRef;
                if (IsPrimitiveType(returnType))
                {
                    typeRef = new TypeReference(returnType.FullyQualifiedName, true);
                }
                else if (context != null && CanUseShortTypeName(returnType, context))
                {
                    typeRef = new TypeReference(returnType.Name);
                }
                else
                {
                    string fullName = returnType.FullyQualifiedName;
                    if (string.IsNullOrEmpty(fullName))
                    {
                        fullName = returnType.Name;
                    }
                    typeRef = new TypeReference(fullName);
                }
                foreach (IReturnType typeArgument in typeArguments)
                {
                    typeRef.GenericTypes.Add(ConvertType(typeArgument, context));
                }
                return(typeRef);
            }
        }
        /// <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);
        }