IType[] OutputTypes(ResolveResult e, IType t) { // C# 4.0 spec: §7.5.2.4 Output types LambdaResolveResult lrr = e as LambdaResolveResult; if (lrr != null || e is MethodGroupResolveResult) { IMethod m = GetDelegateOrExpressionTreeSignature(t); if (m != null) { return(new[] { m.ReturnType }); } } return(emptyTypeArray); }
void MakeExplicitParameterTypeInference(LambdaResolveResult e, IType t) { // C# 4.0 spec: §7.5.2.7 Explicit parameter type inferences if (e.IsImplicitlyTyped || !e.HasParameterList) { return; } Log.WriteLine(" MakeExplicitParameterTypeInference from " + e + " to " + t); IMethod m = GetDelegateOrExpressionTreeSignature(t); if (m == null) { return; } for (int i = 0; i < e.Parameters.Count && i < m.Parameters.Count; i++) { MakeExactInference(e.Parameters[i].Type, m.Parameters[i].Type); } }
IType[] InputTypes(ResolveResult e, IType t) { // C# 4.0 spec: §7.5.2.3 Input types LambdaResolveResult lrr = e as LambdaResolveResult; if (lrr != null && lrr.IsImplicitlyTyped || e is MethodGroupResolveResult) { IMethod m = GetDelegateOrExpressionTreeSignature(t); if (m != null) { IType[] inputTypes = new IType[m.Parameters.Count]; for (int i = 0; i < inputTypes.Length; i++) { inputTypes[i] = m.Parameters[i].Type; } return(inputTypes); } } return(emptyTypeArray); }
void PhaseOne() { // C# 4.0 spec: §7.5.2.1 The first phase Log.WriteLine("Phase One"); for (int i = 0; i < arguments.Length; i++) { ResolveResult Ei = arguments[i]; IType Ti = parameterTypes[i]; LambdaResolveResult lrr = Ei as LambdaResolveResult; if (lrr != null) { MakeExplicitParameterTypeInference(lrr, Ti); } if (lrr != null || Ei is MethodGroupResolveResult) { // this is not in the spec??? if (OutputTypeContainsUnfixed(Ei, Ti) && !InputTypesContainsUnfixed(Ei, Ti)) { MakeOutputTypeInference(Ei, Ti); } } if (IsValidType(Ei.Type)) { if (Ti is ByReferenceType) { MakeExactInference(Ei.Type, Ti); } else { MakeLowerBoundInference(Ei.Type, Ti); } } } }
void MakeOutputTypeInference(ResolveResult e, IType t) { Log.WriteLine(" MakeOutputTypeInference from " + e + " to " + t); // If E is an anonymous function with inferred return type U (§7.5.2.12) and T is a delegate type or expression // tree type with return type Tb, then a lower-bound inference (§7.5.2.9) is made from U to Tb. LambdaResolveResult lrr = e as LambdaResolveResult; if (lrr != null) { IMethod m = GetDelegateOrExpressionTreeSignature(t); if (m != null) { IType inferredReturnType; if (lrr.IsImplicitlyTyped) { if (m.Parameters.Count != lrr.Parameters.Count) { return; // cannot infer due to mismatched parameter lists } TypeParameterSubstitution substitution = GetSubstitutionForFixedTPs(); IType[] inferredParameterTypes = new IType[m.Parameters.Count]; for (int i = 0; i < inferredParameterTypes.Length; i++) { IType parameterType = m.Parameters[i].Type; inferredParameterTypes[i] = parameterType.AcceptVisitor(substitution); } inferredReturnType = lrr.GetInferredReturnType(inferredParameterTypes); } else { inferredReturnType = lrr.GetInferredReturnType(null); } MakeLowerBoundInference(inferredReturnType, m.ReturnType); return; } } // Otherwise, if E is a method group and T is a delegate type or expression tree type // 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 lower-bound // inference is made from U to Tb. MethodGroupResolveResult mgrr = e as MethodGroupResolveResult; if (mgrr != null) { IMethod m = GetDelegateOrExpressionTreeSignature(t); if (m != null) { ResolveResult[] args = new ResolveResult[m.Parameters.Count]; TypeParameterSubstitution substitution = GetSubstitutionForFixedTPs(); for (int i = 0; i < args.Length; i++) { IParameter param = m.Parameters[i]; IType parameterType = param.Type.AcceptVisitor(substitution); if ((param.IsRef || param.IsOut) && parameterType.Kind == TypeKind.ByReference) { parameterType = ((ByReferenceType)parameterType).ElementType; args[i] = new ByReferenceResolveResult(parameterType, param.IsOut); } else { args[i] = new ResolveResult(parameterType); } } var or = mgrr.PerformOverloadResolution(compilation, args, allowExpandingParams: false, allowOptionalParameters: false); if (or.FoundApplicableCandidate && or.BestCandidateAmbiguousWith == null) { IType returnType = or.GetBestCandidateWithSubstitutedTypeArguments().ReturnType; MakeLowerBoundInference(returnType, m.ReturnType); } } return; } // Otherwise, if E is an expression with type U, then a lower-bound inference is made from U to T. if (IsValidType(e.Type)) { MakeLowerBoundInference(e.Type, t); } }
static MethodDeclaration GetMethod (RefactoringContext context, LambdaResolveResult lambda, BlockStatement body, bool noReturnValue = false) { var method = new MethodDeclaration { Name = "Method" }; if (noReturnValue) { method.ReturnType = new PrimitiveType ("void"); } else { var type = lambda.GetInferredReturnType (lambda.Parameters.Select (p => p.Type).ToArray ()); method.ReturnType = type.Name == "?" ? new PrimitiveType ("void") : context.CreateShortType (type); } foreach (var param in lambda.Parameters) method.Parameters.Add (new ParameterDeclaration (context.CreateShortType (param.Type), param.Name)); method.Body = body; if (lambda.IsAsync) method.Modifiers |= Modifiers.Async; return method; }
void MakeExplicitParameterTypeInference(LambdaResolveResult e, IType t) { // C# 4.0 spec: §7.5.2.7 Explicit parameter type inferences if (e.IsImplicitlyTyped || !e.HasParameterList) return; Log.WriteLine(" MakeExplicitParameterTypeInference from " + e + " to " + t); IMethod m = GetDelegateOrExpressionTreeSignature(t); if (m == null) return; for (int i = 0; i < e.Parameters.Count && i < m.Parameters.Count; i++) { MakeExactInference(e.Parameters[i].Type, m.Parameters[i].Type); } }