//
using System;
+
+#if STATIC
+using IKVM.Reflection;
+using IKVM.Reflection.Emit;
+#else
using System.Reflection;
using System.Reflection.Emit;
-using System.Collections.Generic;
+#endif
namespace Mono.CSharp {
//
// Delegate container implementation
//
- public class Delegate : TypeContainer
+ public class Delegate : TypeContainer, IParametersMember
{
FullNamedExpression ReturnType;
- public readonly ParametersCompiled Parameters;
+ readonly ParametersCompiled parameters;
Constructor Constructor;
Method InvokeBuilder;
ModFlags = ModifiersExtensions.Check (AllowedModifiers, mod_flags,
IsTopLevel ? Modifiers.INTERNAL :
Modifiers.PRIVATE, name.Location, Report);
- Parameters = param_list;
+ parameters = param_list;
spec = new TypeSpec (Kind, null, this, null, ModFlags | Modifiers.SEALED);
}
+ #region Properties
+ public TypeSpec MemberType {
+ get {
+ return ReturnType.Type;
+ }
+ }
+
+ public AParametersCollection Parameters {
+ get {
+ return parameters;
+ }
+ }
+ #endregion
+
public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
{
if (a.Target == AttributeTargets.ReturnValue) {
}
);
- Constructor = new Constructor (this, System.Reflection.ConstructorInfo.ConstructorName,
+ Constructor = new Constructor (this, Constructor.ConstructorName,
Modifiers.PUBLIC, null, ctor_parameters, null, Location);
Constructor.Define ();
// First, call the `out of band' special method for
// defining recursively any types we need:
//
- var p = Parameters;
+ var p = parameters;
if (!p.Resolve (this))
return false;
//
// Don't emit async method for compiler generated delegates (e.g. dynamic site containers)
//
- if (TypeManager.iasyncresult_type != null && TypeManager.asynccallback_type != null && !IsCompilerGenerated) {
+ if (!IsCompilerGenerated) {
DefineAsyncMethods (Parameters.CallingConvention);
}
void DefineAsyncMethods (CallingConventions cc)
{
+ var iasync_result = Module.PredefinedTypes.IAsyncResult;
+ var async_callback = Module.PredefinedTypes.AsyncCallback;
+
+ //
+ // It's ok when async types don't exist, the delegate will have Invoke method only
+ //
+ if (!iasync_result.Define () || !async_callback.Define ())
+ return;
+
//
// BeginInvoke
//
- Parameter[] compiled = new Parameter[Parameters.Count];
- for (int i = 0; i < compiled.Length; ++i)
- compiled[i] = new Parameter (new TypeExpression (Parameters.Types[i], Location),
- Parameters.FixedParameters[i].Name,
- Parameters.FixedParameters[i].ModFlags & (Parameter.Modifier.REF | Parameter.Modifier.OUT),
- null, Location);
+ ParametersCompiled async_parameters;
+ if (Parameters.Count == 0) {
+ async_parameters = ParametersCompiled.EmptyReadOnlyParameters;
+ } else {
+ var compiled = new Parameter[Parameters.Count];
+ for (int i = 0; i < compiled.Length; ++i) {
+ var p = parameters[i];
+ compiled[i] = new Parameter (new TypeExpression (parameters.Types[i], Location),
+ p.Name,
+ p.ModFlags & (Parameter.Modifier.REF | Parameter.Modifier.OUT),
+ p.OptAttributes == null ? null : p.OptAttributes.Clone (), Location);
+ }
- ParametersCompiled async_parameters = new ParametersCompiled (Compiler, compiled);
+ async_parameters = new ParametersCompiled (compiled);
+ }
async_parameters = ParametersCompiled.MergeGenerated (Compiler, async_parameters, false,
new Parameter[] {
- new Parameter (new TypeExpression (TypeManager.asynccallback_type, Location), "callback", Parameter.Modifier.NONE, null, Location),
+ new Parameter (new TypeExpression (async_callback.TypeSpec, Location), "callback", Parameter.Modifier.NONE, null, Location),
new Parameter (new TypeExpression (TypeManager.object_type, Location), "object", Parameter.Modifier.NONE, null, Location)
},
new [] {
- TypeManager.asynccallback_type,
+ async_callback.TypeSpec,
TypeManager.object_type
}
);
BeginInvokeBuilder = new Method (this, null,
- new TypeExpression (TypeManager.iasyncresult_type, Location), MethodModifiers,
+ new TypeExpression (iasync_result.TypeSpec, Location), MethodModifiers,
new MemberName ("BeginInvoke"), async_parameters, null);
BeginInvokeBuilder.Define ();
}
if (out_params > 0) {
- var end_param_types = new TypeSpec [out_params];
Parameter[] end_params = new Parameter[out_params];
int param = 0;
for (int i = 0; i < Parameters.FixedParameters.Length; ++i) {
- Parameter p = Parameters [i];
+ Parameter p = parameters [i];
if ((p.ModFlags & Parameter.Modifier.ISBYREF) == 0)
continue;
- end_param_types[param] = Parameters.Types[i];
- end_params[param] = p;
- ++param;
+ end_params [param++] = new Parameter (new TypeExpression (p.Type, Location),
+ p.Name,
+ p.ModFlags & (Parameter.Modifier.REF | Parameter.Modifier.OUT),
+ p.OptAttributes == null ? null : p.OptAttributes.Clone (), Location);
}
- end_parameters = ParametersCompiled.CreateFullyResolved (end_params, end_param_types);
+
+ end_parameters = new ParametersCompiled (end_params);
} else {
end_parameters = ParametersCompiled.EmptyReadOnlyParameters;
}
end_parameters = ParametersCompiled.MergeGenerated (Compiler, end_parameters, false,
new Parameter (
- new TypeExpression (TypeManager.iasyncresult_type, Location),
+ new TypeExpression (iasync_result.TypeSpec, Location),
"result", Parameter.Modifier.NONE, null, Location),
- TypeManager.iasyncresult_type);
+ iasync_result.TypeSpec);
//
// Create method, define parameters, register parameters with type system
public override void DefineConstants ()
{
- if (!Parameters.IsEmpty && Parameters[Parameters.Count - 1].HasDefaultValue) {
- var rc = new ResolveContext (this);
- Parameters.ResolveDefaultValues (rc);
+ if (!Parameters.IsEmpty) {
+ parameters.ResolveDefaultValues (this);
}
}
public override void EmitType ()
{
- if (ReturnType.Type == InternalType.Dynamic) {
- return_attributes = new ReturnParameter (this, InvokeBuilder.MethodBuilder, Location);
- PredefinedAttributes.Get.Dynamic.EmitAttribute (return_attributes.Builder);
- } else {
- var trans_flags = TypeManager.HasDynamicTypeUsed (ReturnType.Type);
- if (trans_flags != null) {
- var pa = PredefinedAttributes.Get.DynamicTransform;
- if (pa.Constructor != null || pa.ResolveConstructor (Location, ArrayContainer.MakeType (TypeManager.bool_type))) {
- return_attributes = new ReturnParameter (this, InvokeBuilder.MethodBuilder, Location);
- return_attributes.Builder.SetCustomAttribute (
- new CustomAttributeBuilder (pa.Constructor, new object [] { trans_flags }));
- }
+ if (ReturnType.Type != null) {
+ if (ReturnType.Type == InternalType.Dynamic) {
+ return_attributes = new ReturnParameter (this, InvokeBuilder.MethodBuilder, Location);
+ Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
+ } else if (ReturnType.Type.HasDynamicElement) {
+ return_attributes = new ReturnParameter (this, InvokeBuilder.MethodBuilder, Location);
+ Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder, ReturnType.Type, Location);
}
}
- Parameters.ApplyAttributes (InvokeBuilder.MethodBuilder);
-
+ Constructor.ParameterInfo.ApplyAttributes (this, Constructor.ConstructorBuilder);
Constructor.ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
+
+ parameters.ApplyAttributes (this, InvokeBuilder.MethodBuilder);
InvokeBuilder.MethodBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
if (BeginInvokeBuilder != null) {
- BeginInvokeBuilder.ParameterInfo.ApplyAttributes (BeginInvokeBuilder.MethodBuilder);
+ BeginInvokeBuilder.ParameterInfo.ApplyAttributes (this, BeginInvokeBuilder.MethodBuilder);
+ EndInvokeBuilder.ParameterInfo.ApplyAttributes (this, EndInvokeBuilder.MethodBuilder);
BeginInvokeBuilder.MethodBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
EndInvokeBuilder.MethodBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
return false;
}
- Parameters.VerifyClsCompliance (this);
+ parameters.VerifyClsCompliance (this);
if (!ReturnType.Type.IsCLSCompliant ()) {
Report.Warning (3002, 1, Location, "Return type of `{0}' is not CLS-compliant",
//
// Base class for `NewDelegate' and `ImplicitDelegateCreation'
//
- public abstract class DelegateCreation : Expression, MethodGroupExpr.IErrorHandler
+ public abstract class DelegateCreation : Expression, OverloadResolver.IErrorHandler
{
protected MethodSpec constructor_method;
protected MethodGroupExpr method_group;
Arguments args = new Arguments (3);
args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
- args.Add (new Argument (new NullLiteral (loc)));
+
+ if (method_group.InstanceExpression == null)
+ args.Add (new Argument (new NullLiteral (loc)));
+ else
+ args.Add (new Argument (method_group.InstanceExpression));
+
args.Add (new Argument (method_group.CreateExpressionTree (ec)));
Expression e = new Invocation (ma, args).Resolve (ec);
if (e == null)
constructor_method = Delegate.GetConstructor (ec.Compiler, ec.CurrentType, type);
var invoke_method = Delegate.GetInvokeMethod (ec.Compiler, type);
- method_group.DelegateType = type;
- method_group.CustomErrorHandler = this;
Arguments arguments = CreateDelegateMethodArguments (invoke_method.Parameters, invoke_method.Parameters.Types, loc);
- method_group = method_group.OverloadResolve (ec, ref arguments, false, loc);
+ method_group = method_group.OverloadResolve (ec, ref arguments, this, OverloadResolver.Restrictions.CovariantDelegate);
if (method_group == null)
return null;
TypeManager.CSharpName (invoke_method.ReturnType), Delegate.FullDelegateDesc (invoke_method));
return;
}
+
if (return_type == null) {
ec.Report.Error (123, loc, "A method or delegate `{0}' parameters do not match delegate `{1}' parameters",
member_name, Delegate.FullDelegateDesc (invoke_method));
if (target_type == TypeManager.delegate_type || target_type == TypeManager.multicast_delegate_type)
return false;
- mg.DelegateType = target_type;
var invoke = Delegate.GetInvokeMethod (ec.Compiler, target_type);
Arguments arguments = CreateDelegateMethodArguments (invoke.Parameters, invoke.Parameters.Types, mg.Location);
- return mg.OverloadResolve (ec, ref arguments, true, mg.Location) != null;
+ return mg.OverloadResolve (ec, ref arguments, null, OverloadResolver.Restrictions.CovariantDelegate | OverloadResolver.Restrictions.ProbingOnly) != null;
}
#region IErrorHandler Members
- public bool NoExactMatch (ResolveContext ec, MethodSpec method)
+ bool OverloadResolver.IErrorHandler.AmbiguousCandidates (ResolveContext ec, MemberSpec best, MemberSpec ambiguous)
{
- if (method.IsGeneric)
- return false;
+ return false;
+ }
- Error_ConversionFailed (ec, method, null);
+ bool OverloadResolver.IErrorHandler.ArgumentMismatch (ResolveContext rc, MemberSpec best, Argument arg, int index)
+ {
+ Error_ConversionFailed (rc, best as MethodSpec, null);
return true;
}
- public bool AmbiguousCall (ResolveContext ec, MethodGroupExpr mg, MethodSpec ambiguous)
+ bool OverloadResolver.IErrorHandler.NoArgumentMatch (ResolveContext rc, MemberSpec best)
+ {
+ Error_ConversionFailed (rc, best as MethodSpec, null);
+ return true;
+ }
+
+ bool OverloadResolver.IErrorHandler.TypeInferenceFailed (ResolveContext rc, MemberSpec best)
{
return false;
}
}
protected override Expression DoResolve (ResolveContext ec)
- {
- if (InstanceExpr is EventExpr) {
- ((EventExpr) InstanceExpr).Error_CannotAssign (ec);
- return null;
- }
-
+ {
TypeSpec del_type = InstanceExpr.Type;
if (del_type == null)
return null;
-
+
+ //
+ // Do only core overload resolution the rest of the checks has been
+ // done on primary expression
+ //
method = Delegate.GetInvokeMethod (ec.Compiler, del_type);
- var mb = method;
- var me = new MethodGroupExpr (mb, del_type, loc);
- me.InstanceExpression = InstanceExpr;
-
- AParametersCollection pd = mb.Parameters;
- int pd_count = pd.Count;
-
- int arg_count = arguments == null ? 0 : arguments.Count;
-
- bool params_method = pd.HasParams;
- bool is_params_applicable = false;
- bool is_applicable = me.IsApplicable (ec, ref arguments, arg_count, ref mb, ref is_params_applicable) == 0;
- if (arguments != null)
- arg_count = arguments.Count;
-
- if (!is_applicable && !params_method && arg_count != pd_count) {
- ec.Report.Error (1593, loc, "Delegate `{0}' does not take `{1}' arguments",
- TypeManager.CSharpName (del_type), arg_count.ToString ());
- } else if (arguments == null || !arguments.HasDynamic) {
- me.VerifyArgumentsCompat (ec, ref arguments, arg_count, mb,
- is_params_applicable || (!is_applicable && params_method), false, loc);
- }
+ var res = new OverloadResolver (new MemberSpec[] { method }, OverloadResolver.Restrictions.DelegateInvoke, loc);
+ var valid = res.ResolveMember<MethodSpec> (ec, ref arguments);
+ if (valid == null && !res.BestCandidateIsDynamic)
+ return null;
type = method.ReturnType;
eclass = ExprClass.Value;