X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fmcs%2Fdelegate.cs;h=853d6902542ca2a2e1e0479b533f1b9227ca358a;hb=b2f4a1cccb1b2ffd72df830a2fdc06caec96c4c4;hp=f353fc5b6c5699ad2727e541b2bf9433a486a1a0;hpb=551f633fa78cfca5a4a1e1c1923e320cfb3b438a;p=mono.git diff --git a/mcs/mcs/delegate.cs b/mcs/mcs/delegate.cs index f353fc5b6c5..853d6902542 100644 --- a/mcs/mcs/delegate.cs +++ b/mcs/mcs/delegate.cs @@ -15,82 +15,89 @@ using System.Reflection; using System.Reflection.Emit; using System.Text; -namespace CIR { - - public class Delegate { +namespace Mono.CSharp { - public readonly string Name; - public readonly string ReturnType; - public int mod_flags; + /// + /// Holds Delegates + /// + public class Delegate : DeclSpace { + public Expression ReturnType; public Parameters Parameters; public Attributes OptAttributes; - public TypeBuilder TypeBuilder; public ConstructorBuilder ConstructorBuilder; public MethodBuilder InvokeBuilder; public MethodBuilder BeginInvokeBuilder; public MethodBuilder EndInvokeBuilder; - public readonly RootContext RootContext; - Type [] param_types; Type ret_type; Expression instance_expr; MethodBase delegate_method; - Location loc; - const int AllowedModifiers = Modifiers.NEW | Modifiers.PUBLIC | Modifiers.PROTECTED | Modifiers.INTERNAL | + Modifiers.UNSAFE | Modifiers.PRIVATE; - public Delegate (RootContext rc, string type, int mod_flags, string name, Parameters param_list, - Attributes attrs, Location loc) + public Delegate (TypeContainer parent, Expression type, int mod_flags, + string name, Parameters param_list, + Attributes attrs, Location l) + : base (parent, name, l) { - this.RootContext = rc; - this.Name = name; this.ReturnType = type; - this.mod_flags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PUBLIC); + ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, + IsTopLevel ? Modifiers.INTERNAL : + Modifiers.PRIVATE, l); Parameters = param_list; OptAttributes = attrs; - this.loc = loc; } - public void DefineDelegate (object parent_builder) + public override TypeBuilder DefineType () { TypeAttributes attr; + + if (TypeBuilder != null) + return TypeBuilder; - if (parent_builder is ModuleBuilder) { - ModuleBuilder builder = (ModuleBuilder) parent_builder; - + if (IsTopLevel) { + ModuleBuilder builder = CodeGen.ModuleBuilder; attr = TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed; - TypeBuilder = builder.DefineType (Name, attr, TypeManager.delegate_type); - + TypeBuilder = builder.DefineType ( + Name, attr, TypeManager.multicast_delegate_type); } else { - TypeBuilder builder = (TypeBuilder) parent_builder; - - attr = TypeAttributes.NestedPublic | TypeAttributes.Class | TypeAttributes.Sealed; - - TypeBuilder = builder.DefineNestedType (Name, attr, TypeManager.delegate_type); + TypeBuilder builder = Parent.TypeBuilder; + attr = TypeAttributes.NestedPublic | TypeAttributes.Class | + TypeAttributes.Sealed; + string name = Name.Substring (1 + Name.LastIndexOf ('.')); + TypeBuilder = builder.DefineNestedType ( + name, attr, TypeManager.multicast_delegate_type); } - RootContext.TypeManager.AddDelegateType (Name, TypeBuilder, this); + TypeManager.AddDelegateType (Name, TypeBuilder, this); + + return TypeBuilder; } - public void Populate (TypeContainer parent) + public override bool DefineMembers (TypeContainer container) { + return true; + } + public override bool Define (TypeContainer container) + { MethodAttributes mattr; int i; - - Type [] const_arg_types = new Type [2]; + // FIXME: POSSIBLY make this static, as it is always constant + // + Type [] const_arg_types = new Type [2]; const_arg_types [0] = TypeManager.object_type; const_arg_types [1] = TypeManager.intptr_type; @@ -100,13 +107,72 @@ namespace CIR { ConstructorBuilder = TypeBuilder.DefineConstructor (mattr, CallingConventions.Standard, const_arg_types); + + ConstructorBuilder.DefineParameter (1, ParameterAttributes.None, "object"); + ConstructorBuilder.DefineParameter (2, ParameterAttributes.None, "method"); + // + // HACK because System.Reflection.Emit is lame + // + // + // FIXME: POSSIBLY make these static, as they are always the same + Parameter [] fixed_pars = new Parameter [2]; + fixed_pars [0] = new Parameter (null, null, Parameter.Modifier.NONE, null); + fixed_pars [1] = new Parameter (null, null, Parameter.Modifier.NONE, null); + Parameters const_parameters = new Parameters (fixed_pars, null, Location); + + TypeManager.RegisterMethod ( + ConstructorBuilder, + new InternalParameters (const_arg_types, const_parameters), + const_arg_types); + ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime); - + + // // Here the various methods like Invoke, BeginInvoke etc are defined + // + // First, call the `out of band' special method for + // defining recursively any types we need: + + if (!Parameters.ComputeAndDefineParameterTypes (this)) + return false; + + param_types = Parameters.GetParameterInfo (this); + if (param_types == null) + return false; + + // + // Invoke method + // + + // Check accessibility + foreach (Type partype in param_types) + if (!container.AsAccessible (partype, ModFlags)) { + Report.Error (59, Location, + "Inconsistent accessibility: parameter type `" + + TypeManager.CSharpName (partype) + "` is less " + + "accessible than delegate `" + Name + "'"); + return false; + } + + ReturnType = ResolveTypeExpr (ReturnType, false, Location); + ret_type = ReturnType.Type; + if (ret_type == null) + return false; + + if (!container.AsAccessible (ret_type, ModFlags)) { + Report.Error (58, Location, + "Inconsistent accessibility: return type `" + + TypeManager.CSharpName (ret_type) + "` is less " + + "accessible than delegate `" + Name + "'"); + return false; + } - param_types = Parameters.GetParameterInfo (parent); - ret_type = parent.LookupType (ReturnType, false); + // + // We don't have to check any others because they are all + // guaranteed to be accessible - they are standard types. + // + CallingConventions cc = Parameters.GetCallingConvention (); mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual; @@ -117,17 +183,34 @@ namespace CIR { ret_type, param_types); - for (i = 0 ; i < param_types.Length; i++) { - Parameter p = Parameters.FixedParameters [i]; - string name = p.Name; - ParameterBuilder pb = InvokeBuilder.DefineParameter (i+1, p.Attributes, name); + i = 0; + if (Parameters.FixedParameters != null){ + int top = Parameters.FixedParameters.Length; + Parameter p; + + for (; i < top; i++) { + p = Parameters.FixedParameters [i]; + + InvokeBuilder.DefineParameter ( + i+1, p.Attributes, p.Name); + } + } + if (Parameters.ArrayParameter != null){ + Parameter p = Parameters.ArrayParameter; + + InvokeBuilder.DefineParameter ( + i+1, p.Attributes, p.Name); } InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime); - TypeContainer.RegisterParameterForBuilder (InvokeBuilder, - new InternalParameters (parent, Parameters)); + TypeManager.RegisterMethod (InvokeBuilder, + new InternalParameters (container, Parameters), + param_types); + // + // BeginInvoke + // int params_num = param_types.Length; Type [] async_param_types = new Type [params_num + 2]; @@ -136,8 +219,8 @@ namespace CIR { async_param_types [params_num] = TypeManager.asynccallback_type; async_param_types [params_num + 1] = TypeManager.object_type; - mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual | - MethodAttributes.NewSlot; + mattr = MethodAttributes.Public | MethodAttributes.HideBySig | + MethodAttributes.Virtual | MethodAttributes.NewSlot; BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke", mattr, @@ -145,28 +228,57 @@ namespace CIR { TypeManager.iasyncresult_type, async_param_types); - for (i = 0 ; i < param_types.Length; i++) { - Parameter p = Parameters.FixedParameters [i]; - string name = p.Name; - BeginInvokeBuilder.DefineParameter (i + 1, p.Attributes, name); + i = 0; + if (Parameters.FixedParameters != null){ + int top = Parameters.FixedParameters.Length; + Parameter p; + + for (i = 0 ; i < top; i++) { + p = Parameters.FixedParameters [i]; + + BeginInvokeBuilder.DefineParameter ( + i+1, p.Attributes, p.Name); + } } - + if (Parameters.ArrayParameter != null){ + Parameter p = Parameters.ArrayParameter; + + BeginInvokeBuilder.DefineParameter ( + i+1, p.Attributes, p.Name); + i++; + } + BeginInvokeBuilder.DefineParameter (i + 1, ParameterAttributes.None, "callback"); BeginInvokeBuilder.DefineParameter (i + 2, ParameterAttributes.None, "object"); BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime); Parameter [] async_params = new Parameter [params_num + 2]; - Parameters.FixedParameters.CopyTo (async_params, 0); - - async_params [params_num] = new Parameter ("System.AsyncCallback", "callback", + int n = 0; + if (Parameters.FixedParameters != null){ + Parameters.FixedParameters.CopyTo (async_params, 0); + n = Parameters.FixedParameters.Length; + } + if (Parameters.ArrayParameter != null) + async_params [n] = Parameters.ArrayParameter; + + async_params [params_num] = new Parameter ( + TypeManager.system_asynccallback_expr, "callback", Parameter.Modifier.NONE, null); - async_params [params_num + 1] = new Parameter ("System.IAsyncResult", "object", + async_params [params_num + 1] = new Parameter ( + TypeManager.system_object_expr, "object", Parameter.Modifier.NONE, null); + + Parameters async_parameters = new Parameters (async_params, null, Location); - TypeContainer.RegisterParameterForBuilder (BeginInvokeBuilder, - new InternalParameters (parent, new Parameters (async_params, null))); + async_parameters.ComputeAndDefineParameterTypes (this); + TypeManager.RegisterMethod (BeginInvokeBuilder, + new InternalParameters (container, async_parameters), + async_param_types); + // + // EndInvoke + // Type [] end_param_types = new Type [1]; end_param_types [0] = TypeManager.iasyncresult_type; @@ -180,59 +292,69 @@ namespace CIR { EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime); Parameter [] end_params = new Parameter [1]; - end_params [0] = new Parameter ("System.IAsyncResult", "result", + end_params [0] = new Parameter ( + TypeManager.system_iasyncresult_expr, "result", Parameter.Modifier.NONE, null); - TypeContainer.RegisterParameterForBuilder (EndInvokeBuilder, - new InternalParameters (parent, new Parameters (end_params, null))); - + TypeManager.RegisterMethod ( + EndInvokeBuilder, new InternalParameters ( + container, + new Parameters ( + end_params, null, Location)), + end_param_types); + + return true; } - // - // Verifies whether the method in question is compatible with the delegate - // Returns the method itself if okay and null if not. - // - public MethodBase VerifyMethod (MethodBase mb, Location loc) + /// + /// Verifies whether the method in question is compatible with the delegate + /// Returns the method itself if okay and null if not. + /// + public static MethodBase VerifyMethod (EmitContext ec, Type delegate_type, MethodBase mb, + Location loc) { ParameterData pd = Invocation.GetParameterData (mb); - bool mismatch = false; - for (int i = param_types.Length; i > 0; ) { - i--; + int pd_count = pd.Count; - if (param_types [i] == pd.ParameterType (i)) - continue; - else { - mismatch = true; - break; - } - } + Expression ml = Expression.MemberLookup ( + ec, delegate_type, "Invoke", loc); - if (mismatch) { - Report.Error (123, loc, "Method '" + Invocation.FullMethodDesc (mb) + "' does not match " + - "delegate '" + FullDelegateDesc () + "'"); + if (!(ml is MethodGroupExpr)) { + Report.Error (-100, loc, "Internal error : could not find Invoke method!"); return null; } - if (ret_type == ((MethodInfo) mb).ReturnType) - return mb; - else - mismatch = true; + MethodBase invoke_mb = ((MethodGroupExpr) ml).Methods [0]; - if (mismatch) { - Report.Error (123, loc, "Method '" + Invocation.FullMethodDesc (mb) + "' does not match " + - "delegate '" + FullDelegateDesc () + "'"); + ParameterData invoke_pd = Invocation.GetParameterData (invoke_mb); + + if (invoke_pd.Count != pd_count) return null; + + for (int i = pd_count; i > 0; ) { + i--; + + if (invoke_pd.ParameterType (i) == pd.ParameterType (i)) + continue; + else + return null; } - return null; + if (((MethodInfo) invoke_mb).ReturnType == ((MethodInfo) mb).ReturnType) + return mb; + else + return null; } // // Verifies whether the invocation arguments are compatible with the // delegate's target method // - public bool VerifyApplicability (EmitContext ec, ArrayList args, Location loc) + public static bool VerifyApplicability (EmitContext ec, + Type delegate_type, + ArrayList args, + Location loc) { int arg_count; @@ -240,83 +362,91 @@ namespace CIR { arg_count = 0; else arg_count = args.Count; + + Expression ml = Expression.MemberLookup ( + ec, delegate_type, "Invoke", loc); - if (param_types.Length != arg_count) { - Report.Error (1593, loc, - "Delegate '" + Name + "' does not take '" + arg_count + "' arguments"); + if (!(ml is MethodGroupExpr)) { + Report.Error (-100, loc, "Internal error : could not find Invoke method!"); return false; } + + MethodBase mb = ((MethodGroupExpr) ml).Methods [0]; + ParameterData pd = Invocation.GetParameterData (mb); - for (int i = arg_count; i > 0;) { - i--; - Expression conv; - Argument a = (Argument) args [i]; - Expression a_expr = a.Expr; - - if (param_types [i] != a_expr.Type) { - - conv = Expression.ConvertImplicitStandard (ec, a_expr, param_types [i], loc); - - if (conv == null) { - Report.Error (1594, loc, - "Delegate '" + Name + "' has some invalid arguments."); - - Report.Error (1503, loc, - "Argument " + (i+1) + - ": Cannot convert from '" + - TypeManager.CSharpName (a_expr.Type) - + "' to '" + TypeManager.CSharpName (param_types [i]) + "'"); - return false; - } - - if (a_expr != conv) - a.Expr = conv; - } + int pd_count = pd.Count; + + bool not_params_method = (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS); + + if (not_params_method && pd_count != arg_count) { + Report.Error (1593, loc, + "Delegate '" + delegate_type.ToString () + + "' does not take '" + arg_count + "' arguments"); + return false; } - return true; + return Invocation.VerifyArgumentsCompat (ec, args, arg_count, mb, !not_params_method, + delegate_type, loc); } - - // - // Verifies whether the delegate in question is compatible with this one in - // order to determine if instantiation from the same is possible. - // - public bool VerifyDelegate (Delegate del) + + /// + /// Verifies whether the delegate in question is compatible with this one in + /// order to determine if instantiation from the same is possible. + /// + public static bool VerifyDelegate (EmitContext ec, Type delegate_type, Type probe_type, Location loc) { - if (ret_type != del.TargetReturnType) + Expression ml = Expression.MemberLookup ( + ec, delegate_type, "Invoke", loc); + + if (!(ml is MethodGroupExpr)) { + Report.Error (-100, loc, "Internal error : could not find Invoke method!"); return false; + } + + MethodBase mb = ((MethodGroupExpr) ml).Methods [0]; + ParameterData pd = Invocation.GetParameterData (mb); - Type [] other_param_types = del.ParameterTypes; + Expression probe_ml = Expression.MemberLookup ( + ec, delegate_type, "Invoke", loc); - if (param_types.Length != other_param_types.Length) + if (!(probe_ml is MethodGroupExpr)) { + Report.Error (-100, loc, "Internal error : could not find Invoke method!"); + return false; + } + + MethodBase probe_mb = ((MethodGroupExpr) probe_ml).Methods [0]; + ParameterData probe_pd = Invocation.GetParameterData (probe_mb); + + if (((MethodInfo) mb).ReturnType != ((MethodInfo) probe_mb).ReturnType) + return false; + + if (pd.Count != probe_pd.Count) return false; - for (int i = param_types.Length; i > 0; ) { + for (int i = pd.Count; i > 0; ) { i--; - if (param_types [i] != other_param_types [i]) + if (pd.ParameterType (i) != probe_pd.ParameterType (i) || + pd.ParameterModifier (i) != probe_pd.ParameterModifier (i)) return false; } - - // FIXME : Hey, what about parameter modifiers ? - + return true; - } - public string FullDelegateDesc () + public static string FullDelegateDesc (Type del_type, MethodBase mb, ParameterData pd) { - StringBuilder sb = new StringBuilder (TypeManager.CSharpName (System.Type.GetType (ReturnType))); + StringBuilder sb = new StringBuilder (TypeManager.CSharpName (((MethodInfo) mb).ReturnType)); - sb.Append (" " + Name); + sb.Append (" " + del_type.ToString ()); sb.Append (" ("); - int length = param_types.Length; + int length = pd.Count; for (int i = length; i > 0; ) { i--; - sb.Append (TypeManager.CSharpName (param_types [length - i - 1])); + sb.Append (TypeManager.CSharpName (pd.ParameterType (length - i - 1))); if (i != 0) sb.Append (", "); } @@ -327,46 +457,43 @@ namespace CIR { } // Hack around System.Reflection as found everywhere else - public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf, MemberFilter filter, object criteria) + public override MemberList FindMembers (MemberTypes mt, BindingFlags bf, + MemberFilter filter, object criteria) { ArrayList members = new ArrayList (); if ((mt & MemberTypes.Method) != 0) { + if (ConstructorBuilder != null) if (filter (ConstructorBuilder, criteria)) members.Add (ConstructorBuilder); + if (InvokeBuilder != null) if (filter (InvokeBuilder, criteria)) members.Add (InvokeBuilder); + if (BeginInvokeBuilder != null) if (filter (BeginInvokeBuilder, criteria)) members.Add (BeginInvokeBuilder); + if (EndInvokeBuilder != null) if (filter (EndInvokeBuilder, criteria)) members.Add (EndInvokeBuilder); } - int count = members.Count; + return new MemberList (members); + } - if (count > 0) { - MemberInfo [] mi = new MemberInfo [count]; - members.CopyTo (mi, 0); - return mi; + public override MemberCache MemberCache { + get { + return null; } - - return null; } - + public void CloseDelegate () { TypeBuilder.CreateType (); } - public int ModFlags { - get { - return mod_flags; - } - } - public Expression InstanceExpression { get { return instance_expr; @@ -407,88 +534,115 @@ namespace CIR { MethodBase delegate_method; Expression delegate_instance_expr; - Location Location; - public NewDelegate (Type type, ArrayList Arguments, Location loc) { this.type = type; this.Arguments = Arguments; - this.Location = loc; + this.loc = loc; } public override Expression DoResolve (EmitContext ec) { - Delegate del = TypeManager.LookupDelegate (type); - constructor_method = del.ConstructorBuilder; - if (Arguments == null) { - Report.Error (-11, Location, + Report.Error (-11, loc, "Delegate creation expression takes only one argument"); return null; } - + if (Arguments.Count != 1) { - Report.Error (-11, Location, + Report.Error (-11, loc, "Delegate creation expression takes only one argument"); return null; } - + + Expression ml = Expression.MemberLookup ( + ec, type, ".ctor", loc); + + if (!(ml is MethodGroupExpr)) { + Report.Error (-100, loc, "Internal error : Could not find delegate constructor!"); + return null; + } + + constructor_method = ((MethodGroupExpr) ml).Methods [0]; Argument a = (Argument) Arguments [0]; - if (!a.Resolve (ec, Location)) + if (!a.ResolveMethodGroup (ec, Location)) return null; Expression e = a.Expr; - + + Expression invoke_method = Expression.MemberLookup ( + ec, type, "Invoke", MemberTypes.Method, + Expression.AllBindingFlags, loc); + + if (invoke_method == null) { + Report.Error (-200, loc, "Internal error ! COuld not find Invoke method!"); + return null; + } + if (e is MethodGroupExpr) { MethodGroupExpr mg = (MethodGroupExpr) e; - - delegate_method = del.VerifyMethod (mg.Methods [0], Location); - - if (delegate_method == null) + + foreach (MethodInfo mi in mg.Methods){ + delegate_method = Delegate.VerifyMethod (ec, type, mi, loc); + + if (delegate_method != null) + break; + } + + if (delegate_method == null) { + string method_desc; + if (mg.Methods.Length > 1) + method_desc = mg.Methods [0].Name; + else + method_desc = Invocation.FullMethodDesc (mg.Methods [0]); + + MethodBase dm = ((MethodGroupExpr) invoke_method).Methods [0]; + ParameterData param = Invocation.GetParameterData (dm); + string delegate_desc = Delegate.FullDelegateDesc (type, dm, param); + + Report.Error (123, loc, "Method '" + method_desc + "' does not " + + "match delegate '" + delegate_desc + "'"); + return null; - + } + if (mg.InstanceExpression != null) delegate_instance_expr = mg.InstanceExpression.Resolve (ec); - else - delegate_instance_expr = null; - + else { + if (!ec.IsStatic) + delegate_instance_expr = ec.This; + else + delegate_instance_expr = null; + } + if (delegate_instance_expr != null) if (delegate_instance_expr.Type.IsValueType) delegate_instance_expr = new BoxedCast (delegate_instance_expr); - - del.InstanceExpression = delegate_instance_expr; - del.TargetMethod = delegate_method; - eclass = ExprClass.Value; return this; } Type e_type = e.Type; - Delegate d = TypeManager.LookupDelegate (e_type); - - if (d == null) { - Report.Error (-12, Location, "Cannot create a delegate from something " + + if (!TypeManager.IsDelegateType (e_type)) { + Report.Error (-12, loc, "Cannot create a delegate from something " + "not a delegate or a method."); return null; } - // This is what MS's compiler reports. We could always choose + // This is what MS' compiler reports. We could always choose // to be more verbose and actually give delegate-level specifics - if (!d.VerifyDelegate (del)) { - Report.Error (29, Location, "Cannot implicitly convert type '" + d.Name + "' " + - "to type '" + del.Name + "'"); + if (!Delegate.VerifyDelegate (ec, type, e_type, loc)) { + Report.Error (29, loc, "Cannot implicitly convert type '" + e_type + "' " + + "to type '" + type + "'"); return null; } - - delegate_instance_expr = d.InstanceExpression; - delegate_method = d.TargetMethod; - - del.InstanceExpression = d.InstanceExpression; - del.TargetMethod = d.TargetMethod; + + delegate_instance_expr = e; + delegate_method = ((MethodGroupExpr) invoke_method).Methods [0]; eclass = ExprClass.Value; return this; @@ -496,12 +650,17 @@ namespace CIR { public override void Emit (EmitContext ec) { - if (delegate_instance_expr == null) + if (delegate_instance_expr == null || + delegate_method.IsStatic) ec.ig.Emit (OpCodes.Ldnull); else delegate_instance_expr.Emit (ec); - ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method); + if (delegate_method.IsVirtual) { + ec.ig.Emit (OpCodes.Dup); + ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method); + } else + ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method); ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method); } } @@ -510,7 +669,6 @@ namespace CIR { public Expression InstanceExpr; public ArrayList Arguments; - public Location Location; MethodBase method; @@ -518,35 +676,58 @@ namespace CIR { { this.InstanceExpr = instance_expr; this.Arguments = args; - this.Location = loc; + this.loc = loc; } public override Expression DoResolve (EmitContext ec) { - Delegate del = TypeManager.LookupDelegate (InstanceExpr.Type); - - if (del == null) - return null; - - if (del.TargetMethod == null) + if (InstanceExpr is EventExpr) { + + EventInfo ei = ((EventExpr) InstanceExpr).EventInfo; + + Expression ml = MemberLookup ( + ec, ec.ContainerType, ei.Name, + MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc); + + if (ml == null) { + // + // If this is the case, then the Event does not belong + // to this Type and so, according to the spec + // cannot be accessed directly + // + // Note that target will not appear as an EventExpr + // in the case it is being referenced within the same type container; + // it will appear as a FieldExpr in that case. + // + + Assign.error70 (ei, loc); + return null; + } + } + + + Type del_type = InstanceExpr.Type; + if (del_type == null) return null; if (Arguments != null){ - for (int i = Arguments.Count; i > 0;){ - --i; - Argument a = (Argument) Arguments [i]; - - if (!a.Resolve (ec, Location)) + foreach (Argument a in Arguments){ + if (!a.Resolve (ec, loc)) return null; } } - if (!del.VerifyApplicability (ec, Arguments, Location)) + if (!Delegate.VerifyApplicability (ec, del_type, Arguments, loc)) + return null; + + Expression lookup = Expression.MemberLookup (ec, del_type, "Invoke", loc); + if (!(lookup is MethodGroupExpr)) { + Report.Error (-100, loc, "Internal error : could not find Invoke method!"); return null; + } - method = del.InvokeBuilder; + method = ((MethodGroupExpr) lookup).Methods [0]; type = ((MethodInfo) method).ReturnType; - eclass = ExprClass.Value; return this; @@ -560,7 +741,7 @@ namespace CIR { // Invocation on delegates call the virtual Invoke member // so we are always `instance' calls // - Invocation.EmitCall (ec, false, InstanceExpr, method, Arguments); + Invocation.EmitCall (ec, false, false, InstanceExpr, method, Arguments, loc); } public override void EmitStatement (EmitContext ec)