void CreateDelegate(DefaultClass c, string name, AST.TypeReference returnType, IList <AST.TemplateDefinition> templates, IList <AST.ParameterDeclarationExpression> parameters) { c.BaseTypes.Add(c.ProjectContent.SystemTypes.Delegate); if (currentClass.Count > 0) { DefaultClass cur = GetCurrentClass(); cur.InnerClasses.Add(c); c.FullyQualifiedName = cur.FullyQualifiedName + '.' + name; } else { c.FullyQualifiedName = PrependCurrentNamespace(name); cu.Classes.Add(c); } c.UsingScope = currentNamespace; currentClass.Push(c); // necessary for CreateReturnType ConvertTemplates(templates, c); List <IParameter> p = new List <IParameter>(); if (parameters != null) { foreach (AST.ParameterDeclarationExpression param in parameters) { p.Add(CreateParameter(param)); } } AnonymousMethodReturnType.AddDefaultDelegateMethod(c, CreateReturnType(returnType), p); currentClass.Pop(); }
public override object VisitAnonymousMethodExpression(AnonymousMethodExpression anonymousMethodExpression, object data) { AnonymousMethodReturnType amrt = new AnonymousMethodReturnType(resolver.CompilationUnit); foreach (ParameterDeclarationExpression param in anonymousMethodExpression.Parameters) { amrt.MethodParameters.Add(NRefactoryASTConvertVisitor.CreateParameter(param, resolver.CallingMember as IMethod, resolver.CallingClass, resolver.CompilationUnit)); } return(amrt); }
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) )); }
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 }) )); }
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; } }
IEnumerable <IReturnType> InputTypes(IReturnType e, IReturnType T) { AnonymousMethodReturnType amrt = e as AnonymousMethodReturnType; if (amrt != null && amrt.HasImplicitlyTypedParameters || e is MethodGroupReturnType) { IMethod m = GetDelegateOrExpressionTreeSignature(T, amrt != null && amrt.CanBeConvertedToExpressionTree); if (m != null) { return(m.Parameters.Select(p => p.ReturnType)); } } return(EmptyList <IReturnType> .Instance); }
IEnumerable <IReturnType> OutputTypes(IReturnType e, IReturnType T) { AnonymousMethodReturnType amrt = e as AnonymousMethodReturnType; if (amrt != null || e is MethodGroupReturnType) { IMethod m = GetDelegateOrExpressionTreeSignature(T, amrt != null && amrt.CanBeConvertedToExpressionTree); if (m != null) { return(new[] { m.ReturnType }); } } return(EmptyList <IReturnType> .Instance); }
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); }
void MakeExplicitParameterTypeInference(IReturnType e, IReturnType T) { // If e is an explicitly typed anonymous function with parameter types U1…Uk and T is a // delegate type with parameter types V1…Vk then for each Ui an exact inference (§7.4.2.8) // is made from Ui for the corresponding Vi. AnonymousMethodReturnType amrt = e as AnonymousMethodReturnType; if (amrt != null && amrt.HasParameterList) { IMethod m = GetDelegateOrExpressionTreeSignature(T, amrt.CanBeConvertedToExpressionTree); if (m != null && amrt.MethodParameters.Count == m.Parameters.Count) { for (int i = 0; i < amrt.MethodParameters.Count; i++) { MakeExactInference(amrt.MethodParameters[i].ReturnType, m.Parameters[i].ReturnType); } } } }
void MakeOutputTypeInference(IReturnType e, IReturnType T) { //If e is an anonymous function with inferred return type U (§7.4.2.11) and T is // a delegate type or expression tree type with return type Tb, then a lowerbound // inference (§7.4.2.9) is made from U for Tb. AnonymousMethodReturnType amrt = e as AnonymousMethodReturnType; if (amrt != null) { IMethod m = GetDelegateOrExpressionTreeSignature(T, amrt.CanBeConvertedToExpressionTree); if (m != null) { IReturnType inferredReturnType; if (amrt.HasParameterList && amrt.MethodParameters.Count == m.Parameters.Count) { var inferredParameterTypes = m.Parameters.Select(p => SubstituteFixedTypes(p.ReturnType)).ToArray(); inferredReturnType = amrt.ResolveReturnType(inferredParameterTypes); } else { inferredReturnType = amrt.ResolveReturnType(); } MakeLowerBoundInference(inferredReturnType, m.ReturnType); return; } } // Otherwise, if e is a method group and T is a delegate type or expression tree type // return type Tb with parameter types T1…Tk and return type Tb, and overload resolution // of e with the types T1…Tk yields a single method with return type U, then a lowerbound // inference is made from U for Tb. if (e is MethodGroupReturnType) { // the MS C# doesn't seem to implement this rule, so we can safely skip this return; } // Otherwise, if e is an expression with type U, then a lowerbound inference is made from // U for T. MakeLowerBoundInference(e, T); }
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()); } }
public override object VisitAnonymousMethodExpression(AnonymousMethodExpression anonymousMethodExpression, object data) { AnonymousMethodReturnType amrt = new AnonymousMethodReturnType(resolver.CompilationUnit); foreach (ParameterDeclarationExpression param in anonymousMethodExpression.Parameters) { amrt.MethodParameters.Add(NRefactoryASTConvertVisitor.CreateParameter(param, resolver.CallingMember as IMethod, resolver.CallingClass, resolver.CompilationUnit)); } return amrt; }