コード例 #1
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);
        }
コード例 #2
0
		public static IReturnType CreateReturnType(AST.TypeReference reference, IClass callingClass,
		                                           IMethodOrProperty callingMember, int caretLine, int caretColumn,
		                                           IProjectContent projectContent)
		{
			System.Diagnostics.Debug.Assert(projectContent != null);
			if (reference == null) {
				return GetDefaultReturnType(projectContent);
			}
			if (reference is AST.ArrayTypeReference) {
				AST.ArrayTypeReference arr = (AST.ArrayTypeReference)reference;
				return new ArrayReturnType(projectContent,
				                           CreateReturnType(arr.ElementType, callingClass, callingMember,
				                                            caretLine, caretColumn, projectContent),
				                           (arr.Rank != null) ? (int)arr.Rank.Value : 1);
			} else if (reference is AST.SimpleTypeReference) {
				string name = ((AST.SimpleTypeReference)reference).Name;
				IReturnType rt;
				int typeParameterCount = (reference is AST.GenericTypeReference) ? ((AST.GenericTypeReference)reference).GenericArguments.Count : 0;
				if (name == "duck")
					rt = new BooResolver.DuckClass(new DefaultCompilationUnit(projectContent)).DefaultReturnType;
				else if (BooAmbience.ReverseTypeConversionTable.ContainsKey(name))
					rt = new GetClassReturnType(projectContent, BooAmbience.ReverseTypeConversionTable[name], typeParameterCount);
				else if (callingClass == null)
					rt = new GetClassReturnType(projectContent, name, typeParameterCount);
				else
					rt = new SearchClassReturnType(projectContent, callingClass, caretLine, caretColumn,
					                               name, typeParameterCount);
				if (typeParameterCount > 0) {
					AST.TypeReferenceCollection arguments = ((AST.GenericTypeReference)reference).GenericArguments;
					// GenericTypeReference derives from SimpleTypeReference
					IReturnType[] typeArguments = new IReturnType[arguments.Count];
					for (int i = 0; i < typeArguments.Length; i++) {
						typeArguments[i] = CreateReturnType(arguments[i], callingClass, callingMember, caretLine, caretColumn,
						                                    projectContent);
					}
					rt = new ConstructedReturnType(rt, typeArguments);
				}
				return rt;
			} else if (reference is AST.CallableTypeReference) {
				AST.CallableTypeReference ctr = (AST.CallableTypeReference)reference;
				AnonymousMethodReturnType amrt = new AnonymousMethodReturnType(new DefaultCompilationUnit(projectContent));
				if (ctr.ReturnType != null) {
					amrt.MethodReturnType = CreateReturnType(ctr.ReturnType, callingClass, callingMember, caretLine, caretColumn, projectContent);
				}
				amrt.MethodParameters = new List<IParameter>();
				AddParameters(ctr.Parameters, amrt.MethodParameters, callingMember, callingClass ?? new DefaultClass(new DefaultCompilationUnit(projectContent), "__Dummy"));
				return amrt;
			} else {
				throw new NotSupportedException("unknown reference type: " + reference.ToString());
			}
		}
コード例 #3
0
        /// <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);
        }
コード例 #4
0
		public void ConversionDoesNotExistFromAnonymousDelegateWithParameterToSystemPredicateWhenParameterTypeIsIncompatible()
		{
			AnonymousMethodReturnType amrt = new AnonymousMethodReturnType(new DefaultCompilationUnit(msc));
			amrt.MethodReturnType = msc.SystemTypes.Boolean;
			amrt.MethodParameters = new List<IParameter>();
			amrt.MethodParameters.Add(new DefaultParameter("test", msc.SystemTypes.String, DomRegion.Empty));
			Assert.IsFalse(MemberLookupHelper.ConversionExists(
				amrt,
				new ConstructedReturnType(new GetClassReturnType(msc, "System.Predicate", 1),
				                          new IReturnType[] { msc.SystemTypes.Int32 })
			));
		}
コード例 #5
0
		public void NoConversionExistsFromParameterlessAnonymousDelegateToSystemPredicate()
		{
			AnonymousMethodReturnType amrt = new AnonymousMethodReturnType(new DefaultCompilationUnit(msc));
			amrt.MethodParameters = new List<IParameter>();
			Assert.IsFalse(MemberLookupHelper.ConversionExists(
				amrt,
				new GetClassReturnType(msc, "System.Predicate", 1)
			));
		}
コード例 #6
0
		void MakeMethodResult(IReturnType type, string methodName)
		{
			resolveResult = new MethodGroupResolveResult(callingClass, resolver.CallingMember, type, methodName);
			IMethod m = (resolveResult as MethodGroupResolveResult).GetMethodIfSingleOverload();
			if (m != null) {
				AnonymousMethodReturnType amrt = new AnonymousMethodReturnType(cu);
				amrt.MethodReturnType = m.ReturnType;
				amrt.MethodParameters = m.Parameters;
				resolveResult.ResolvedType = amrt;
			}
		}
コード例 #7
0
		public override void OnBlockExpression(BlockExpression node)
		{
			AnonymousMethodReturnType amrt = new AnonymousMethodReturnType(cu);
			if (node.ReturnType != null) {
				amrt.MethodReturnType = ConvertType(node.ReturnType);
			} else {
				amrt.MethodReturnType = new BooInferredReturnType(node.Body, resolver.CallingClass,
				                                               node.ContainsAnnotation("inline"));
			}
			amrt.MethodParameters = new List<IParameter>();
			ConvertVisitor.AddParameters(node.Parameters, amrt.MethodParameters, resolver.CallingMember, resolver.CallingClass ?? new DefaultClass(resolver.CompilationUnit, "__Dummy"));
			MakeResult(amrt);
		}