protected override BlockContext CreateBlockContext(BlockContext bc) { var ctx = base.CreateBlockContext(bc); var am = bc.CurrentAnonymousMethod as AnonymousMethodBody; if (am != null) { return_inference = am.ReturnTypeInference; } ctx.Set(ResolveContext.Options.TryScope); return(ctx); }
public Type InferReturnType(ParseContext ec, TypeInferenceContext typeInferenceContext, Type type) { var parameterCount = _scope.Parameters.Count; var invokeMethod = TypeManager.GetDelegateInvokeMethod(ec, null, type); if ((invokeMethod.GetParameters().Length != _scope.Parameters.Count) || typeInferenceContext.InferredTypeArguments.Length < parameterCount) { return(TypeManager.CoreTypes.Object); } var oldParameters = _scope.Parameters; _scope.Parameters = oldParameters.Clone(); for (int i = 0; i < parameterCount; i++) { if (typeInferenceContext.InferredTypeArguments[i] != null) { if (!typeInferenceContext.InferredTypeArguments[i].IsGenericParameter) { _scope.Parameters[i].ParameterType = typeInferenceContext.InferredTypeArguments[i]; } } } //_scope.Parameters.Resolve(ec); var oldScope = ec.CurrentScope; ec.CurrentScope = _scope; var resolvedBody = _body.Resolve(ec); ec.CurrentScope = oldScope; _scope.Parameters = oldParameters; Type = resolvedBody.Type; for (int i = 0; i < parameterCount; i++) { if ((typeInferenceContext.InferredTypeArguments[i] != null) && typeInferenceContext.InferredTypeArguments[i].IsGenericParameter) { typeInferenceContext.InferredTypeArguments[i] = _scope.Parameters[i].ParameterType; //_scope.Parameters[i].ParameterType = typeInferenceContext.InferredTypeArguments[i]; } } return(Type); }
public bool NoExactMatch(EmitContext ec, MethodBase method) { #if GMCS_SOURCE AParametersCollection pd = TypeManager.GetParameterData(method); Type source_type = pd.ExtensionMethodType; if (source_type != null) { Argument a = (Argument)Arguments [0]; if (source_type.IsGenericType && source_type.ContainsGenericParameters) { TypeInferenceContext tic = new TypeInferenceContext(source_type.GetGenericArguments()); tic.OutputTypeInference(ec, a.Expr, source_type); if (tic.FixAllTypes()) { source_type = source_type.GetGenericTypeDefinition().MakeGenericType(tic.InferredTypeArguments); } } if (!Convert.ImplicitConversionExists(ec, a.Expr, source_type)) { Report.Error(1936, loc, "An implementation of `{0}' query expression pattern for source type `{1}' could not be found", mg.Name, TypeManager.CSharpName(a.Type)); return(true); } } if (!method.IsGenericMethod) { return(false); } if (mg.Name == "SelectMany") { Report.Error(1943, loc, "An expression type is incorrect in a subsequent `from' clause in a query expression with source type `{0}'", ((Argument)Arguments [0]).GetSignatureForError()); } else { Report.Error(1942, loc, "An expression type in `{0}' clause is incorrect. Type inference failed in the call to `{1}'", mg.Name.ToLower(), mg.Name); } return(true); #else return(false); #endif }
public bool ExplicitTypeInference(ParseContext ec, TypeInferenceContext typeInference, Type delegateType) { if (!HasExplicitParameters) { return(false); } if (!TypeManager.IsDelegateType(delegateType)) { if (TypeManager.DropGenericTypeArguments(delegateType) != TypeManager.CoreTypes.GenericExpression) { return(false); } delegateType = delegateType.GetGenericArguments()[0]; if (!TypeManager.IsDelegateType(delegateType)) { return(false); } } var delegateParams = TypeManager.GetDelegateParameters(ec, delegateType); if (delegateParams.Count != _scope.Parameters.Count) { return(false); } for (int i = 0; i < _scope.Parameters.Count; ++i) { var iType = delegateParams.Types[i]; if (!TypeManager.IsGenericParameter(iType)) { if (!TypeManager.HasElementType(iType)) { continue; } if (!TypeManager.IsGenericParameter(iType.GetElementType())) { continue; } } typeInference.ExactInference(_scope.Parameters.Types[i], iType); } return(true); }
protected override ParametersCompiled ResolveParameters (ResolveContext ec, TypeInferenceContext tic, TypeSpec delegateType) { if (!delegateType.IsDelegate) return null; AParametersCollection d_params = Delegate.GetParameters (delegateType); if (HasExplicitParameters) { if (!VerifyExplicitParameters (ec, delegateType, d_params)) return null; return Parameters; } // // If L has an implicitly typed parameter list we make implicit parameters explicit // Set each parameter of L is given the type of the corresponding parameter in D // if (!VerifyParameterCompatibility (ec, delegateType, d_params, ec.IsInProbingMode)) return null; TypeSpec [] ptypes = new TypeSpec [Parameters.Count]; for (int i = 0; i < d_params.Count; i++) { // D has no ref or out parameters if ((d_params.FixedParameters[i].ModFlags & Parameter.Modifier.RefOutMask) != 0) return null; TypeSpec d_param = d_params.Types [i]; // // When type inference context exists try to apply inferred type arguments // if (tic != null) { d_param = tic.InflateGenericArgument (ec, d_param); } ptypes [i] = d_param; ImplicitLambdaParameter ilp = (ImplicitLambdaParameter) Parameters.FixedParameters [i]; ilp.SetParameterType (d_param); ilp.Resolve (null, i); } Parameters.Types = ptypes; return Parameters; }
bool OverloadResolver.IErrorHandler.TypeInferenceFailed(ResolveContext rc, MemberSpec best) { var ms = (MethodSpec)best; TypeSpec source_type = ms.Parameters.ExtensionMethodType; if (source_type != null) { Argument a = arguments[0]; if (TypeManager.IsGenericType(source_type) && InflatedTypeSpec.ContainsTypeParameter(source_type)) { TypeInferenceContext tic = new TypeInferenceContext(source_type.TypeArguments); tic.OutputTypeInference(rc, a.Expr, source_type); if (tic.FixAllTypes(rc)) { source_type = source_type.GetDefinition().MakeGenericType(rc, tic.InferredTypeArguments); } } if (!Convert.ImplicitConversionExists(rc, a.Expr, source_type)) { rc.Report.Error(1936, loc, "An implementation of `{0}' query expression pattern for source type `{1}' could not be found", best.Name, a.Type.GetSignatureForError()); return(true); } } if (best.Name == "SelectMany") { rc.Report.Error(1943, loc, "An expression type is incorrect in a subsequent `from' clause in a query expression with source type `{0}'", arguments[0].GetSignatureForError()); } else { rc.Report.Error(1942, loc, "An expression type in `{0}' clause is incorrect. Type inference failed in the call to `{1}'", best.Name.ToLowerInvariant(), best.Name); } return(true); }
// // Infers type arguments based on explicit arguments // public bool ExplicitTypeInference (TypeInferenceContext type_inference, TypeSpec delegate_type) { if (!HasExplicitParameters) return false; if (!delegate_type.IsDelegate) { if (!delegate_type.IsExpressionTreeType) return false; delegate_type = TypeManager.GetTypeArguments (delegate_type) [0]; if (!delegate_type.IsDelegate) return false; } AParametersCollection d_params = Delegate.GetParameters (delegate_type); if (d_params.Count != Parameters.Count) return false; var ptypes = Parameters.Types; var dtypes = d_params.Types; for (int i = 0; i < Parameters.Count; ++i) { if (type_inference.ExactInference (ptypes[i], dtypes[i]) == 0) { // // Continue when 0 (quick path) does not mean inference failure. Checking for // same type handles cases like int -> int // if (ptypes[i] == dtypes[i]) continue; return false; } } return true; }
private AstExpression InferTypeGenericExpr(AstGenericExpr expr, CheezType expected, TypeInferenceContext context) { foreach (var param in expr.Parameters) { param.Scope = expr.Scope; param.TypeExpr.AttachTo(expr); param.TypeExpr = InferTypeHelper(param.TypeExpr, null, context); param.Type = param.TypeExpr.Value as CheezType; if (!ValidatePolymorphicParameterType(param.Location, param.Type)) { return(expr); } } expr.Type = new GenericType(expr); expr.Value = new PolyValue("generic"); return(expr); }
bool OverloadResolver.IErrorHandler.TypeInferenceFailed (ResolveContext rc, MemberSpec best) { var ms = (MethodSpec) best; TypeSpec source_type = ms.Parameters.ExtensionMethodType; if (source_type != null) { Argument a = arguments[0]; if (TypeManager.IsGenericType (source_type) && TypeManager.ContainsGenericParameters (source_type)) { TypeInferenceContext tic = new TypeInferenceContext (source_type.TypeArguments); tic.OutputTypeInference (rc, a.Expr, source_type); if (tic.FixAllTypes (rc)) { source_type = source_type.GetDefinition ().MakeGenericType (tic.InferredTypeArguments); } } if (!Convert.ImplicitConversionExists (rc, a.Expr, source_type)) { rc.Report.Error (1936, loc, "An implementation of `{0}' query expression pattern for source type `{1}' could not be found", best.Name, TypeManager.CSharpName (a.Type)); return true; } } if (best.Name == "SelectMany") { rc.Report.Error (1943, loc, "An expression type is incorrect in a subsequent `from' clause in a query expression with source type `{0}'", arguments[0].GetSignatureForError ()); } else { rc.Report.Error (1942, loc, "An expression type in `{0}' clause is incorrect. Type inference failed in the call to `{1}'", best.Name.ToLowerInvariant (), best.Name); } return true; }
private AstExpression ResolveType(AstExpression expr, TypeInferenceContext context, out CheezType type) { expr = InferTypeHelper(expr, CheezType.Type, context); return(ResolveTypeHelper(expr, out type)); }
protected ParametersCompiled ResolveParameters(ParseContext ec, TypeInferenceContext tic, Type delegateType) { if (!TypeManager.IsDelegateType(delegateType)) { return(null); } var delegateParameters = TypeManager.GetParameterData(delegateType.GetMethod("Invoke")); if (HasExplicitParameters) { if (!VerifyExplicitParameters(ec, delegateType, delegateParameters)) { return(null); } return(_scope.Parameters.Clone()); } // // If L has an implicitly typed parameter list we make implicit parameters explicit // Set each parameter of L is given the type of the corresponding parameter in D // if (!VerifyParameterCompatibility(ec, delegateType, delegateParameters, ec.IsInProbingMode)) { return(null); } var ptypes = new Type[_scope.Parameters.Count]; for (int i = 0; i < delegateParameters.Count; i++) { // D has no ref or out parameters if ((delegateParameters.FixedParameters[i].ModifierFlags & Parameter.Modifier.IsByRef) != 0) { return(null); } Type dParam = delegateParameters.Types[i]; // Blablabla, because reflection does not work with dynamic types if (dParam.IsGenericParameter) { dParam = delegateType.GetGenericArguments() [dParam.GenericParameterPosition]; } // // When type inference context exists try to apply inferred type arguments // if (tic != null) { dParam = tic.InflateGenericArgument(dParam); } ptypes[i] = dParam; ((ImplicitLambdaParameter)_scope.Parameters.FixedParameters[i]).ParameterType = dParam; } // TODO : FIX THIS //ptypes.CopyTo(_scope.Parameters.Types, 0); // TODO: STOP DOING THIS _scope.Parameters.Types = ptypes; return(_scope.Parameters); }
protected virtual ParametersCompiled ResolveParameters (ResolveContext ec, TypeInferenceContext tic, TypeSpec delegate_type) { var delegate_parameters = Delegate.GetParameters (delegate_type); if (Parameters == ParametersCompiled.Undefined) { // // We provide a set of inaccessible parameters // Parameter[] fixedpars = new Parameter[delegate_parameters.Count]; for (int i = 0; i < delegate_parameters.Count; i++) { Parameter.Modifier i_mod = delegate_parameters.FixedParameters [i].ModFlags; if ((i_mod & Parameter.Modifier.OUT) != 0) { if (!ec.IsInProbingMode) { ec.Report.Error (1688, loc, "Cannot convert anonymous method block without a parameter list to delegate type `{0}' because it has one or more `out' parameters", delegate_type.GetSignatureForError ()); } return null; } fixedpars[i] = new Parameter ( new TypeExpression (delegate_parameters.Types [i], loc), null, delegate_parameters.FixedParameters [i].ModFlags, null, loc); } return ParametersCompiled.CreateFullyResolved (fixedpars, delegate_parameters.Types); } if (!VerifyExplicitParameters (ec, tic, delegate_type, delegate_parameters)) { return null; } return Parameters; }
AnonymousMethodBody CompatibleMethodBody (ResolveContext ec, TypeInferenceContext tic, TypeSpec return_type, TypeSpec delegate_type) { ParametersCompiled p = ResolveParameters (ec, tic, delegate_type); if (p == null) return null; ParametersBlock b = ec.IsInProbingMode ? (ParametersBlock) Block.PerformClone () : Block; if (b.IsAsync) { var rt = return_type; if (rt != null && rt.Kind != MemberKind.Void && rt != ec.Module.PredefinedTypes.Task.TypeSpec && !rt.IsGenericTask) { ec.Report.Error (4010, loc, "Cannot convert async {0} to delegate type `{1}'", GetSignatureForError (), delegate_type.GetSignatureForError ()); return null; } b = b.ConvertToAsyncTask (ec, ec.CurrentMemberDefinition.Parent.PartialContainer, p, return_type, delegate_type, loc); } return CompatibleMethodFactory (return_type ?? InternalType.ErrorType, delegate_type, p, b); }
public TypeSpec InferReturnType (ResolveContext ec, TypeInferenceContext tic, TypeSpec delegate_type) { Expression expr; AnonymousExpression am; if (compatibles.TryGetValue (delegate_type, out expr)) { am = expr as AnonymousExpression; return am == null ? null : am.ReturnType; } using (ec.Set (ResolveContext.Options.ProbingMode | ResolveContext.Options.InferReturnType)) { ReportPrinter prev; if (TypeInferenceReportPrinter != null) { prev = ec.Report.SetPrinter (TypeInferenceReportPrinter); } else { prev = null; } var body = CompatibleMethodBody (ec, tic, null, delegate_type); if (body != null) { am = body.Compatible (ec, body); } else { am = null; } if (TypeInferenceReportPrinter != null) { ec.Report.SetPrinter (prev); } } if (am == null) return null; // compatibles.Add (delegate_type, am); return am.ReturnType; }
public bool NoExactMatch(ResolveContext ec, MethodSpec method) { var pd = method.Parameters; TypeSpec source_type = pd.ExtensionMethodType; if (source_type != null) { Argument a = arguments [0]; if (TypeManager.IsGenericType (source_type) && TypeManager.ContainsGenericParameters (source_type)) { TypeInferenceContext tic = new TypeInferenceContext (source_type.TypeArguments); tic.OutputTypeInference (ec, a.Expr, source_type); if (tic.FixAllTypes (ec)) { source_type = source_type.GetDefinition ().MakeGenericType (tic.InferredTypeArguments); } } if (!Convert.ImplicitConversionExists (ec, a.Expr, source_type)) { ec.Report.Error (1936, loc, "An implementation of `{0}' query expression pattern for source type `{1}' could not be found", mg.Name, TypeManager.CSharpName (a.Type)); return true; } } if (!method.IsGeneric) return false; if (mg.Name == "SelectMany") { ec.Report.Error (1943, loc, "An expression type is incorrect in a subsequent `from' clause in a query expression with source type `{0}'", arguments [0].GetSignatureForError ()); } else { ec.Report.Error (1942, loc, "An expression type in `{0}' clause is incorrect. Type inference failed in the call to `{1}'", mg.Name.ToLower (), mg.Name); } return true; }
protected bool VerifyParameterCompatibility (ResolveContext ec, TypeInferenceContext tic, TypeSpec delegate_type, AParametersCollection invoke_pd, bool ignore_errors) { if (Parameters.Count != invoke_pd.Count) { if (ignore_errors) return false; ec.Report.Error (1593, loc, "Delegate `{0}' does not take `{1}' arguments", delegate_type.GetSignatureForError (), Parameters.Count.ToString ()); return false; } bool has_implicit_parameters = !HasExplicitParameters; bool error = false; for (int i = 0; i < Parameters.Count; ++i) { Parameter.Modifier p_mod = invoke_pd.FixedParameters [i].ModFlags; if (Parameters.FixedParameters [i].ModFlags != p_mod && p_mod != Parameter.Modifier.PARAMS) { if (ignore_errors) return false; if (p_mod == Parameter.Modifier.NONE) ec.Report.Error (1677, Parameters[i].Location, "Parameter `{0}' should not be declared with the `{1}' keyword", (i + 1).ToString (), Parameter.GetModifierSignature (Parameters [i].ModFlags)); else ec.Report.Error (1676, Parameters[i].Location, "Parameter `{0}' must be declared with the `{1}' keyword", (i+1).ToString (), Parameter.GetModifierSignature (p_mod)); error = true; } if (has_implicit_parameters) continue; TypeSpec type = invoke_pd.Types [i]; if (tic != null) type = tic.InflateGenericArgument (ec, type); if (!TypeSpecComparer.IsEqual (type, Parameters.Types [i])) { if (ignore_errors) return false; ec.Report.Error (1678, Parameters [i].Location, "Parameter `{0}' is declared as type `{1}' but should be `{2}'", (i+1).ToString (), Parameters.Types [i].GetSignatureForError (), invoke_pd.Types [i].GetSignatureForError ()); error = true; } } return !error; }
protected bool VerifyExplicitParameters (ResolveContext ec, TypeInferenceContext tic, TypeSpec delegate_type, AParametersCollection parameters) { if (VerifyParameterCompatibility (ec, tic, delegate_type, parameters, ec.IsInProbingMode)) return true; if (!ec.IsInProbingMode) ec.Report.Error (1661, loc, "Cannot convert `{0}' to delegate type `{1}' since there is a parameter mismatch", GetSignatureForError (), delegate_type.GetSignatureForError ()); return false; }
public bool NoExactMatch (EmitContext ec, MethodBase method) { #if GMCS_SOURCE AParametersCollection pd = TypeManager.GetParameterData (method); Type source_type = pd.ExtensionMethodType; if (source_type != null) { Argument a = (Argument) Arguments [0]; if (source_type.IsGenericType && source_type.ContainsGenericParameters) { TypeInferenceContext tic = new TypeInferenceContext (source_type.GetGenericArguments ()); tic.OutputTypeInference (ec, a.Expr, source_type); if (tic.FixAllTypes ()) { source_type = source_type.GetGenericTypeDefinition ().MakeGenericType (tic.InferredTypeArguments); } } if (!Convert.ImplicitConversionExists (ec, a.Expr, source_type)) { Report.Error (1936, loc, "An implementation of `{0}' query expression pattern for source type `{1}' could not be found", mg.Name, TypeManager.CSharpName (a.Type)); return true; } } if (!method.IsGenericMethod) return false; if (mg.Name == "SelectMany") { Report.Error (1943, loc, "An expression type is incorrect in a subsequent `from' clause in a query expression with source type `{0}'", ((Argument) Arguments [0]).GetSignatureForError ()); } else { Report.Error (1942, loc, "An expression type in `{0}' clause is incorrect. Type inference failed in the call to `{1}'", mg.Name.ToLower (), mg.Name); } return true; #else return false; #endif }