[Mono-list] trivial patch

Ravi Pratap M ravi@ximian.com
Thu, 18 Oct 2001 16:29:18 +0530


This is a multi-part message in MIME format.

------=_NextPart_000_002F_01C157F2.09150840
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hey Miguel,

    Just committed this stuff. Delegate Instantiation has been split out
into the NewDelegate class and invocation into DelegateInvocation.

    The code is much neater, thanks :-) I have done it exactly like what you
did with ElementAccess ;-)

    Regards,

                Ravi



------=_NextPart_000_002F_01C157F2.09150840
Content-Type: application/octet-stream;
	name="mcs-oct-18"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="mcs-oct-18"

? mcs.pdb=0A=
Index: ChangeLog=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: /cvs/public/mcs/mcs/ChangeLog,v=0A=
retrieving revision 1.137=0A=
diff -u -r1.137 ChangeLog=0A=
--- ChangeLog	2001/10/17 09:25:26	1.137=0A=
+++ ChangeLog	2001/10/18 06:59:56=0A=
@@ -1,3 +1,17 @@=0A=
+2001-10-18  Ravi Pratap  <ravi@ximian.com>=0A=
+=0A=
+	* delegate.cs (NewDelegate): New class to take care of delegate=0A=
+	instantiation.=0A=
+=0A=
+	* expression.cs (New): Split the delegate related code out into =0A=
+	the NewDelegate class.=0A=
+=0A=
+	* delegate.cs (DelegateInvocation): New class to handle delegate =0A=
+	invocation.=0A=
+=0A=
+	* expression.cs (Invocation): Split out delegate related code into=0A=
+	the DelegateInvocation class.=0A=
+=0A=
 2001-10-17  Ravi Pratap  <ravi@ximian.com>=0A=
 =0A=
 	* expression.cs (New::DoResolve): Implement delegate creation fully=0A=
Index: delegate.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: /cvs/public/mcs/mcs/delegate.cs,v=0A=
retrieving revision 1.8=0A=
diff -u -r1.8 delegate.cs=0A=
--- delegate.cs	2001/10/17 09:25:26	1.8=0A=
+++ delegate.cs	2001/10/18 06:59:57=0A=
@@ -283,5 +283,140 @@=0A=
 			}
 		}
 	}
-=09
+
+	public class NewDelegate : Expression {
+
+		public ArrayList Arguments;
+
+		MethodBase constructor_method;
+		MethodBase delegate_method;
+		Expression delegate_instance_expr;
+
+		Location Location;
+	=09
+		public NewDelegate (Type type, ArrayList Arguments, Location loc)
+		{
+			this.type =3D type;
+			this.Arguments =3D Arguments;
+			this.Location  =3D loc;=20
+		}
+
+		public override Expression DoResolve (EmitContext ec)
+		{
+			Delegate del =3D TypeManager.LookupDelegate (type);
+			constructor_method =3D del.ConstructorBuilder;
+		=09
+			if (Arguments =3D=3D null) {
+				Report.Error (-11, Location,
+					      "Delegate creation expression takes only one argument");
+				return null;
+			}
+		=09
+			if (Arguments.Count !=3D 1) {
+				Report.Error (-11, Location,
+					      "Delegate creation expression takes only one argument");
+				return null;
+			}
+		=09
+			Argument a =3D (Argument) Arguments [0];
+		=09
+			if (!a.Resolve (ec))
+				return null;
+		=09
+			Expression e =3D a.Expr;
+		=09
+			if (e is MethodGroupExpr) {
+				MethodGroupExpr mg =3D (MethodGroupExpr) e;
+			=09
+				delegate_method  =3D del.VerifyMethod (mg.Methods [0], Location);
+			=09
+				if (delegate_method =3D=3D null)
+					return null;
+			=09
+				if (mg.InstanceExpression !=3D null)
+					delegate_instance_expr =3D mg.InstanceExpression.Resolve (ec);
+				else
+					delegate_instance_expr =3D null;
+			=09
+				if (delegate_instance_expr !=3D null)
+					if (delegate_instance_expr.Type.IsValueType)
+						delegate_instance_expr =3D new BoxedCast =
(delegate_instance_expr);
+			=09
+			=09
+				del.InstanceExpression =3D delegate_instance_expr;
+				del.TargetMethod =3D delegate_method;
+			=09
+				eclass =3D ExprClass.Value;
+				return this;
+			} else {
+				Report.Error (-200, Location, "Cannot handle delegate instantiation =
from other delegates");
+				return null;
+			}
+		}
+	=09
+		public override void Emit (EmitContext ec)
+		{
+			if (delegate_instance_expr =3D=3D null)
+				ec.ig.Emit (OpCodes.Ldnull);
+			else
+				delegate_instance_expr.Emit (ec);
+		=09
+			ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
+			ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
+		}
+	}
+
+	public class DelegateInvocation : Expression {
+
+		public Expression InstanceExpr;
+		public ArrayList  Arguments;
+		public Location   Location;
+
+		MethodBase method;
+	=09
+		public DelegateInvocation (Expression instance_expr, ArrayList args, =
Location loc)
+		{
+			this.InstanceExpr =3D instance_expr;
+			this.Arguments =3D args;
+			this.Location =3D loc;
+		}
+
+		public override Expression DoResolve (EmitContext ec)
+		{
+			Delegate del =3D TypeManager.LookupDelegate (InstanceExpr.Type);
+
+			if (del =3D=3D null)
+				return null;
+
+			if (del.TargetMethod =3D=3D null)
+				return null;
+		=09
+			if (Arguments !=3D null){
+				for (int i =3D Arguments.Count; i > 0;){
+					--i;
+					Argument a =3D (Argument) Arguments [i];
+				=09
+					if (!a.Resolve (ec))
+						return null;
+				}
+			}
+		=09
+			if (!del.VerifyApplicability (ec, Arguments, Location))
+				return null;
+		=09
+			method =3D del.InvokeBuilder;
+			type =3D ((MethodInfo) method).ReturnType;
+		=09
+			eclass =3D ExprClass.Value;
+		=09
+			return this;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+			Delegate del =3D TypeManager.LookupDelegate (InstanceExpr.Type);
+			Invocation.EmitCall (ec, del.TargetMethod.IsStatic, InstanceExpr, =
method, Arguments);
+		}
+
+	}
 }
Index: expression.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: /cvs/public/mcs/mcs/expression.cs,v=0A=
retrieving revision 1.87=0A=
diff -u -r1.87 expression.cs=0A=
--- expression.cs	2001/10/17 09:25:26	1.87=0A=
+++ expression.cs	2001/10/18 07:00:08=0A=
@@ -3568,7 +3568,6 @@=0A=
 		public readonly ArrayList Arguments;=0A=
 		public readonly Location Location;=0A=
 =0A=
-		public bool IsDelegate;=0A=
 		Expression expr;=0A=
 		MethodBase method =3D null;=0A=
 			=0A=
@@ -4077,7 +4076,7 @@=0A=
 			=0A=
 		public override Expression DoResolve (EmitContext ec)=0A=
 		{=0A=
-			IsDelegate =3D false;=0A=
+			bool IsDelegate =3D false;=0A=
 			//=0A=
 			// First, resolve the expression that is used to=0A=
 			// trigger the invocation=0A=
@@ -4092,31 +4091,10 @@=0A=
 				IsDelegate =3D TypeManager.IsDelegateType (expr_type);=0A=
 			}=0A=
 			=0A=
-			if (IsDelegate) {=0A=
-				=0A=
-				Delegate del =3D TypeManager.LookupDelegate (expr_type);=0A=
-=0A=
-				if (Arguments !=3D null){=0A=
-					for (int i =3D Arguments.Count; i > 0;){=0A=
-						--i;=0A=
-						Argument a =3D (Argument) Arguments [i];=0A=
-						=0A=
-						if (!a.Resolve (ec))=0A=
-							return null;=0A=
-					}=0A=
-				}=0A=
-				=0A=
-				if (!del.VerifyApplicability (ec, Arguments, Location))=0A=
-					return null;=0A=
-=0A=
-				method =3D del.InvokeBuilder;=0A=
-				type =3D ((MethodInfo) method).ReturnType;=0A=
-=0A=
-				eclass =3D ExprClass.Value;=0A=
-=0A=
-				return this;=0A=
-			}=0A=
+			if (IsDelegate)=0A=
+				return (new DelegateInvocation (this.expr, Arguments, =
Location)).Resolve (ec);=0A=
 			=0A=
+			=0A=
 			if (!(this.expr is MethodGroupExpr)){=0A=
 				report118 (Location, this.expr, "method group");=0A=
 				return null;=0A=
@@ -4228,14 +4206,8 @@=0A=
 		=0A=
 		public override void Emit (EmitContext ec)=0A=
 		{=0A=
-			if (IsDelegate) {=0A=
-				Delegate del =3D TypeManager.LookupDelegate (this.expr.Type);=0A=
-				EmitCall (ec, del.TargetMethod.IsStatic, this.expr, method, =
Arguments);=0A=
-=0A=
-			} else {=0A=
-				MethodGroupExpr mg =3D (MethodGroupExpr) this.expr;=0A=
-				EmitCall (ec, method.IsStatic, mg.InstanceExpression, method, =
Arguments);=0A=
-			}=0A=
+			MethodGroupExpr mg =3D (MethodGroupExpr) this.expr;=0A=
+			EmitCall (ec, method.IsStatic, mg.InstanceExpression, method, =
Arguments);=0A=
 		}=0A=
 		=0A=
 		public override void EmitStatement (EmitContext ec)=0A=
@@ -4267,11 +4239,6 @@=0A=
 		public readonly string    Rank;=0A=
 		public readonly ArrayList Initializers;=0A=
 =0A=
-		public bool IsDelegate;=0A=
-		=0A=
-		Expression delegate_instance_expr =3D null;=0A=
-		MethodBase delegate_method =3D null;=0A=
-		=0A=
 		Location Location;=0A=
 		MethodBase method =3D null;=0A=
 =0A=
@@ -4337,108 +4304,52 @@=0A=
 				if (type =3D=3D null)=0A=
 					return null;=0A=
 =0A=
-				IsDelegate =3D TypeManager.IsDelegateType (type);=0A=
+				bool IsDelegate =3D TypeManager.IsDelegateType (type);=0A=
 				=0A=
-				if (IsDelegate) {=0A=
-					// So we have a delegate creation expression=0A=
-					=0A=
-					Delegate del =3D TypeManager.LookupDelegate (type);=0A=
-					method =3D del.ConstructorBuilder;=0A=
-=0A=
-					if (Arguments =3D=3D null) {=0A=
-						Report.Error (-11, Location,=0A=
-							      "Delegate creation expression takes only one argument");=0A=
-						return null;=0A=
-					}=0A=
-					=0A=
-					if (Arguments.Count !=3D 1) {=0A=
-						Report.Error (-11, Location,=0A=
-							      "Delegate creation expression takes only one argument");=0A=
-						return null;=0A=
-					}=0A=
-					=0A=
-					Argument a =3D (Argument) Arguments [0];=0A=
-=0A=
-					if (!a.Resolve (ec))=0A=
-						return null;=0A=
-					=0A=
-					Expression e =3D a.Expr;=0A=
-					=0A=
-					if (e is MethodGroupExpr) {=0A=
-						MethodGroupExpr mg =3D (MethodGroupExpr) e;=0A=
-						=0A=
-						delegate_method  =3D del.VerifyMethod (mg.Methods [0], Location);=0A=
-=0A=
-						if (delegate_method =3D=3D null)=0A=
-							return null;=0A=
-						=0A=
-						if (mg.InstanceExpression !=3D null)=0A=
-							delegate_instance_expr =3D mg.InstanceExpression.Resolve (ec);=0A=
-						else=0A=
-							delegate_instance_expr =3D null;=0A=
-						=0A=
-						if (delegate_instance_expr !=3D null) {=0A=
-							if (delegate_instance_expr.Type.IsValueType)=0A=
-							      delegate_instance_expr =3D new BoxedCast=0A=
-								      (delegate_instance_expr);=0A=
-						}=0A=
-=0A=
-						del.InstanceExpression =3D delegate_instance_expr;=0A=
-						del.TargetMethod =3D delegate_method;=0A=
-=0A=
-						eclass =3D ExprClass.Value;=0A=
-						return this;=0A=
-						=0A=
-					} else {=0A=
-						Report.Error (-200, Location,=0A=
-							      "Cannot handle delegate creation from another delegate =
yet");=0A=
+				if (IsDelegate)=0A=
+					return (new NewDelegate (type, Arguments, Location)).Resolve (ec);=0A=
+				=0A=
+				Expression ml;=0A=
+				=0A=
+				ml =3D MemberLookup (ec, type, ".ctor", false,=0A=
+						   MemberTypes.Constructor, AllBindingsFlags, Location);=0A=
+				=0A=
+				bool is_struct =3D false;=0A=
+				is_struct =3D type.IsSubclassOf (TypeManager.value_type);=0A=
+				=0A=
+				if (! (ml is MethodGroupExpr)){=0A=
+					if (!is_struct){=0A=
+						report118 (Location, ml, "method group");=0A=
 						return null;=0A=
 					}=0A=
-					=0A=
-				} else {=0A=
-					=0A=
-					Expression ml;=0A=
-					=0A=
-					ml =3D MemberLookup (ec, type, ".ctor", false,=0A=
-							   MemberTypes.Constructor, AllBindingsFlags, Location);=0A=
-=0A=
-					bool is_struct =3D false;=0A=
-					is_struct =3D type.IsSubclassOf (TypeManager.value_type);=0A=
-					=0A=
-					if (! (ml is MethodGroupExpr)){=0A=
-						if (!is_struct){=0A=
-							report118 (Location, ml, "method group");=0A=
-							return null;=0A=
-						}=0A=
-					}=0A=
-					=0A=
-					if (ml !=3D null) {=0A=
-						if (Arguments !=3D null){=0A=
-							for (int i =3D Arguments.Count; i > 0;){=0A=
-								--i;=0A=
-								Argument a =3D (Argument) Arguments [i];=0A=
-								=0A=
-								if (!a.Resolve (ec))=0A=
-									return null;=0A=
-							}=0A=
+				}=0A=
+				=0A=
+				if (ml !=3D null) {=0A=
+					if (Arguments !=3D null){=0A=
+						for (int i =3D Arguments.Count; i > 0;){=0A=
+							--i;=0A=
+							Argument a =3D (Argument) Arguments [i];=0A=
+							=0A=
+							if (!a.Resolve (ec))=0A=
+								return null;=0A=
 						}=0A=
-						=0A=
-						method =3D Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,=0A=
-										     Arguments, Location);=0A=
 					}=0A=
 					=0A=
-					=0A=
-					if (method =3D=3D null && !is_struct) {=0A=
-						Error (-6, Location,=0A=
-						       "New invocation: Can not find a constructor for " +=0A=
-						       "this argument list");=0A=
-						return null;=0A=
-					}=0A=
-					=0A=
-					eclass =3D ExprClass.Value;=0A=
-					return this;=0A=
+					method =3D Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,=0A=
+									     Arguments, Location);=0A=
 				}=0A=
-=0A=
+				=0A=
+				=0A=
+				if (method =3D=3D null && !is_struct) {=0A=
+					Error (-6, Location,=0A=
+					       "New invocation: Can not find a constructor for " +=0A=
+					       "this argument list");=0A=
+					return null;=0A=
+				}=0A=
+				=0A=
+				eclass =3D ExprClass.Value;=0A=
+				return this;=0A=
+				=0A=
 			}=0A=
 			=0A=
 			if (NewType =3D=3D NType.Array) {=0A=
@@ -4447,7 +4358,7 @@=0A=
 			=0A=
 			return null;=0A=
 		}=0A=
-=0A=
+		=0A=
 		//=0A=
 		// This DoEmit can be invoked in two contexts:=0A=
 		//    * As a mechanism that will leave a value on the stack (new =
object)=0A=
@@ -4466,18 +4377,6 @@=0A=
 =0A=
 				ml.AddressOf (ec);=0A=
 			} else {=0A=
-=0A=
-				if (IsDelegate) {=0A=
-					if (delegate_instance_expr =3D=3D null)=0A=
-						ec.ig.Emit (OpCodes.Ldnull);=0A=
-					else=0A=
-						delegate_instance_expr.Emit (ec);=0A=
-=0A=
-					ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);=0A=
-					ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);=0A=
-					return true;=0A=
-				} =0A=
-				=0A=
 				Invocation.EmitArguments (ec, method, Arguments);=0A=
 				ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);=0A=
 				return true;=0A=

------=_NextPart_000_002F_01C157F2.09150840--