2 // anonymous.cs: Support for anonymous methods and types
5 // Miguel de Icaza (miguel@ximain.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 // Copyright 2003-2008 Novell, Inc.
14 using System.Collections;
15 using System.Collections.Specialized;
16 using System.Reflection;
17 using System.Reflection.Emit;
19 namespace Mono.CSharp {
21 public abstract class CompilerGeneratedClass : Class
23 public static string MakeName (string host, string typePrefix, string name, int id)
25 return "<" + host + ">" + typePrefix + "__" + name + id.ToString ();
28 protected CompilerGeneratedClass (DeclSpace parent, MemberName name, int mod)
29 : base (parent.NamespaceEntry, parent, name, mod | Modifiers.COMPILER_GENERATED | Modifiers.SEALED, null)
33 protected CompilerGeneratedClass (DeclSpace parent, GenericMethod generic, MemberName name, int mod)
34 : this (parent, name, mod)
36 if (generic != null) {
37 ArrayList list = new ArrayList ();
38 foreach (TypeParameter tparam in generic.TypeParameters) {
39 if (tparam.Constraints != null)
40 list.Add (tparam.Constraints.Clone ());
42 SetParameterInfo (list);
46 protected void CheckMembersDefined ()
49 throw new InternalErrorException ("Helper class already defined!");
54 // Anonymous method storey is created when an anonymous method uses
55 // variable or parameter from outer scope. They are then hoisted to
56 // anonymous method storey (captured)
58 public class AnonymousMethodStorey : CompilerGeneratedClass
60 class StoreyFieldPair {
61 public AnonymousMethodStorey Storey;
64 public StoreyFieldPair (AnonymousMethodStorey storey)
69 public override int GetHashCode ()
71 return Storey.ID.GetHashCode ();
74 public override bool Equals (object obj)
76 return (AnonymousMethodStorey)obj == Storey;
80 class HoistedGenericField : Field
82 public HoistedGenericField (DeclSpace parent, FullNamedExpression type, int mod, string name,
83 Attributes attrs, Location loc)
84 : base (parent, type, mod, name, attrs, loc)
88 public override bool Define ()
90 type_name.Type = ((AnonymousMethodStorey) Parent).MutateType (type_name.Type);
91 return base.Define ();
95 // TODO: Why is it required by debugger ?
96 public readonly int ID;
99 public readonly Block OriginalSourceBlock;
101 // A list of StoreyFieldPair with local field keeping parent storey instance
102 ArrayList used_parent_storeys;
104 // A list of hoisted parameters
105 protected ArrayList hoisted_params;
108 HoistedThis hoisted_this;
110 // Local variable which holds this storey instance
111 public LocalTemporary Instance;
113 bool references_defined;
114 bool has_hoisted_variable;
116 public AnonymousMethodStorey (Block block, DeclSpace parent, MemberBase host, GenericMethod generic, string name)
117 : base (parent, generic, MakeMemberName (host, name, generic, block.StartLocation), Modifiers.PRIVATE)
120 OriginalSourceBlock = block;
124 static MemberName MakeMemberName (MemberBase host, string name, GenericMethod generic, Location loc)
126 string host_name = host == null ? null : host.Name;
127 string tname = MakeName (host_name, "c", name, unique_id);
128 TypeArguments args = null;
129 if (generic != null) {
130 args = new TypeArguments (loc);
131 foreach (TypeParameter tparam in generic.CurrentTypeParameters)
132 args.Add (new SimpleName (tparam.Name, loc));
135 return new MemberName (tname, args, loc);
138 public Field AddCapturedVariable (string name, Type type)
140 CheckMembersDefined ();
142 FullNamedExpression field_type = new TypeExpression (type, Location);
144 return AddCompilerGeneratedField (name, field_type);
146 const int mod = Modifiers.INTERNAL | Modifiers.COMPILER_GENERATED;
147 Field f = new HoistedGenericField (this, field_type, mod, name, null, Location);
152 protected Field AddCompilerGeneratedField (string name, FullNamedExpression type)
154 const int mod = Modifiers.INTERNAL | Modifiers.COMPILER_GENERATED;
155 Field f = new Field (this, type, mod, name, null, Location);
160 public void AddParentStoreyReference (AnonymousMethodStorey s)
162 CheckMembersDefined ();
164 if (used_parent_storeys == null)
165 used_parent_storeys = new ArrayList ();
166 else if (used_parent_storeys.IndexOf (s) != -1)
169 has_hoisted_variable = true;
170 used_parent_storeys.Add (new StoreyFieldPair (s));
173 public void CaptureLocalVariable (EmitContext ec, LocalInfo local_info)
175 if (local_info.HoistedVariableReference != null)
178 HoistedVariable var = new HoistedLocalVariable (this, local_info, GetVariableMangledName (local_info));
179 local_info.HoistedVariableReference = var;
180 has_hoisted_variable = true;
183 public void CaptureParameter (EmitContext ec, ParameterReference param_ref)
185 if (param_ref.HoistedVariable != null)
188 if (hoisted_params == null)
189 hoisted_params = new ArrayList ();
191 HoistedVariable expr = new HoistedParameter (this, param_ref);
192 param_ref.Parameter.HoistedVariableReference = expr;
193 hoisted_params.Add (expr);
196 public HoistedThis CaptureThis (EmitContext ec, This t)
198 hoisted_this = new HoistedThis (this, t);
202 void DefineStoreyReferences ()
204 if (used_parent_storeys == null || references_defined)
207 references_defined = true;
210 // For each used variable from parent scope we allocate its local reference point
212 for (int i = 0; i < used_parent_storeys.Count; ++i) {
213 StoreyFieldPair sf = (StoreyFieldPair) used_parent_storeys [i];
214 AnonymousMethodStorey p_storey = sf.Storey;
215 TypeExpr type_expr = new TypeExpression (p_storey.TypeBuilder, Location);
217 sf.Field = AddCompilerGeneratedField ("<>f__ref$" + p_storey.ID, type_expr);
223 // Initializes all hoisted variables
225 public void EmitHoistedVariables (EmitContext ec)
227 // There can be only one instance variable for each storey type
228 if (Instance != null)
229 throw new InternalErrorException ();
232 // A storey with hoisted `this' is an instance method
234 if (!HasHoistedVariables) {
235 hoisted_this.RemoveHoisting ();
239 DefineStoreyReferences ();
242 // Create an instance of storey type
244 Expression storey_type_expr;
247 // Use current method type parameter (MVAR) for top level storey only. All
248 // nested storeys use class type parameter (VAR)
250 TypeParameter[] tparams = ec.CurrentAnonymousMethod != null ?
251 ec.CurrentAnonymousMethod.Storey.CurrentTypeParameters :
252 ec.GenericDeclContainer.CurrentTypeParameters;
254 if (tparams.Length != CountTypeParameters) {
255 TypeParameter [] full = new TypeParameter [CountTypeParameters];
256 DeclSpace parent = ec.DeclContainer.Parent;
257 parent.CurrentTypeParameters.CopyTo (full, 0);
258 tparams.CopyTo (full, parent.CountTypeParameters);
262 storey_type_expr = new ConstructedType (TypeBuilder, tparams, Location);
264 storey_type_expr = new TypeExpression (TypeBuilder, Location);
267 Expression e = new New (storey_type_expr, new ArrayList (0), Location).Resolve (ec);
270 Instance = new LocalTemporary (storey_type_expr.Type);
273 EmitHoistedFieldsInitialization (ec);
276 void EmitHoistedFieldsInitialization (EmitContext ec)
279 // Initialize all storey reference fields by using local or hoisted variables
281 if (used_parent_storeys != null) {
282 foreach (StoreyFieldPair sf in used_parent_storeys) {
284 // Setting local field
286 Expression instace_expr = GetStoreyInstanceExpression (ec);
287 FieldExpr f_set_expr = TypeManager.IsGenericType (instace_expr.Type) ?
288 new FieldExpr (sf.Field.FieldBuilder, instace_expr.Type, Location) :
289 new FieldExpr (sf.Field.FieldBuilder, Location);
290 f_set_expr.InstanceExpression = instace_expr;
292 SimpleAssign a = new SimpleAssign (f_set_expr, sf.Storey.GetStoreyInstanceExpression (ec));
293 if (a.Resolve (ec) != null)
294 a.EmitStatement (ec);
299 // Setting currect anonymous method to null blocks any further variable hoisting
301 AnonymousExpression ae = ec.CurrentAnonymousMethod;
302 ec.CurrentAnonymousMethod = null;
304 if (hoisted_params != null) {
305 foreach (HoistedParameter hp in hoisted_params) {
306 hp.EmitHoistingAssignment (ec);
310 if (hoisted_this != null) {
311 hoisted_this.EmitHoistingAssignment (ec);
314 ec.CurrentAnonymousMethod = ae;
317 public override void EmitType ()
319 DefineStoreyReferences ();
324 // Returns a field which holds referenced storey instance
326 Field GetReferencedStoreyField (AnonymousMethodStorey storey)
328 if (used_parent_storeys == null)
331 foreach (StoreyFieldPair sf in used_parent_storeys) {
332 if (sf.Storey == storey)
340 // Creates storey instance expression regardless of currect IP
342 public Expression GetStoreyInstanceExpression (EmitContext ec)
344 AnonymousExpression am = ec.CurrentAnonymousMethod;
347 // Access from original block -> storey
353 // Access from anonymous method implemented as a static -> storey
355 if (am.Storey == null)
358 Field f = am.Storey.GetReferencedStoreyField (this);
360 if (am.Storey == this) {
362 // Access inside of same storey (S -> S)
364 return new CompilerGeneratedThis (TypeBuilder, Location);
367 // External field access
373 // Storey was cached to local field
375 FieldExpr f_ind = new FieldExpr (f.FieldBuilder, Location);
376 f_ind.InstanceExpression = new CompilerGeneratedThis (TypeBuilder, Location);
380 protected virtual string GetVariableMangledName (LocalInfo local_info)
383 // No need to mangle anonymous method hoisted variables cause they
384 // are hoisted in their own scopes
386 return local_info.Name;
390 // Returns true when at least one local variable or parameter is
391 // hoisted, or story is transitioned
393 public bool HasHoistedVariables {
395 return has_hoisted_variable || hoisted_params != null;
398 has_hoisted_variable = value;
403 // Mutate type dispatcher
405 public Type MutateType (Type type)
408 if (TypeManager.IsGenericType (type))
409 return MutateGenericType (type);
411 if (TypeManager.IsGenericParameter (type))
412 return MutateGenericArgument (type);
415 return MutateArrayType (type);
421 // Changes method type arguments (MVAR) to storey (VAR) type arguments
423 public MethodInfo MutateGenericMethod (MethodInfo method)
426 Type [] t_args = TypeManager.GetGenericArguments (method);
427 if (TypeManager.IsGenericType (method.DeclaringType)) {
428 Type t = MutateGenericType (method.DeclaringType);
429 if (t != method.DeclaringType) {
430 method = (MethodInfo) TypeManager.DropGenericMethodArguments (method);
431 if (method.Module == CodeGen.Module.Builder)
432 method = TypeBuilder.GetMethod (t, method);
434 method = (MethodInfo) MethodInfo.GetMethodFromHandle (method.MethodHandle, t.TypeHandle);
438 if (t_args == null || t_args.Length == 0)
441 for (int i = 0; i < t_args.Length; ++i)
442 t_args [i] = MutateType (t_args [i]);
444 return method.GetGenericMethodDefinition ().MakeGenericMethod (t_args);
446 throw new NotSupportedException ();
450 public ConstructorInfo MutateConstructor (ConstructorInfo ctor)
453 if (TypeManager.IsGenericType (ctor.DeclaringType)) {
454 Type t = MutateGenericType (ctor.DeclaringType);
455 if (t != ctor.DeclaringType) {
456 // TODO: It should throw on imported types
457 return TypeBuilder.GetConstructor (t, ctor);
464 public FieldInfo MutateField (FieldInfo field)
467 if (TypeManager.IsGenericType (field.DeclaringType)) {
468 Type t = MutateGenericType (field.DeclaringType);
469 if (t != field.DeclaringType) {
470 // TODO: It should throw on imported types
471 return TypeBuilder.GetField (t, field);
479 protected Type MutateArrayType (Type array)
481 int rank = array.GetArrayRank ();
482 Type element = TypeManager.GetElementType (array);
484 throw new NotImplementedException ();
486 if (TypeManager.IsGenericParameter (element)) {
487 element = MutateGenericArgument (element);
488 } else if (TypeManager.IsGenericType (element)) {
489 element = MutateGenericType (element);
494 return element.MakeArrayType (rank);
497 protected Type MutateGenericType (Type type)
499 Type [] t_args = TypeManager.GetTypeArguments (type);
500 if (t_args == null || t_args.Length == 0)
503 for (int i = 0; i < t_args.Length; ++i)
504 t_args [i] = MutateType (t_args [i]);
506 return type.GetGenericTypeDefinition ().MakeGenericType (t_args);
511 // Changes method generic argument (MVAR) to type generic argument (VAR)
513 public Type MutateGenericArgument (Type type)
515 foreach (TypeParameter tp in CurrentTypeParameters) {
516 if (tp.Name == type.Name) {
524 public static void Reset ()
531 if (hoisted_this != null)
532 hoisted_this.RemoveHoisting ();
536 public abstract class HoistedVariable
538 class ExpressionTreeProxy : Expression
540 readonly HoistedVariable hv;
542 public ExpressionTreeProxy (HoistedVariable hv)
547 public override Expression CreateExpressionTree (EmitContext ec)
549 throw new NotSupportedException ("ET");
552 public override Expression DoResolve (EmitContext ec)
554 eclass = ExprClass.Value;
555 type = TypeManager.expression_type_expr.Type;
559 public override void Emit (EmitContext ec)
561 Expression e = hv.GetFieldExpression (ec).CreateExpressionTree (ec);
562 // This should never fail
569 protected readonly AnonymousMethodStorey storey;
570 protected Field field;
571 Hashtable cached_inner_access; // TODO: Hashtable is too heavyweight
573 protected HoistedVariable (AnonymousMethodStorey storey, string name, Type type)
575 this.storey = storey;
577 this.field = storey.AddCapturedVariable (name, type);
580 public void AddressOf (EmitContext ec, AddressOp mode)
582 GetFieldExpression (ec).AddressOf (ec, mode);
585 public Expression CreateExpressionTree (EmitContext ec)
587 return new ExpressionTreeProxy (this);
590 public void Emit (EmitContext ec)
592 GetFieldExpression (ec).Emit (ec);
596 // Creates field access expression for hoisted variable
598 protected FieldExpr GetFieldExpression (EmitContext ec)
600 if (ec.CurrentAnonymousMethod == null) {
602 // When setting top-level hoisted variable in generic storey
603 // change storey generic types to method generic types (VAR -> MVAR)
605 FieldExpr outer_access = storey.MemberName.IsGeneric ?
606 new FieldExpr (field.FieldBuilder, storey.Instance.Type, field.Location) :
607 new FieldExpr (field.FieldBuilder, field.Location);
609 outer_access.InstanceExpression = storey.GetStoreyInstanceExpression (ec);
610 outer_access.Resolve (ec);
614 FieldExpr inner_access;
615 if (cached_inner_access != null) {
616 inner_access = (FieldExpr) cached_inner_access [ec.CurrentAnonymousMethod];
619 cached_inner_access = new Hashtable (4);
622 if (inner_access == null) {
623 inner_access = new FieldExpr (field.FieldBuilder, field.Location);
624 inner_access.InstanceExpression = storey.GetStoreyInstanceExpression (ec);
625 inner_access.Resolve (ec);
626 cached_inner_access.Add (ec.CurrentAnonymousMethod, inner_access);
632 public abstract void EmitSymbolInfo ();
634 public void Emit (EmitContext ec, bool leave_copy)
636 GetFieldExpression (ec).Emit (ec, leave_copy);
639 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
641 GetFieldExpression (ec).EmitAssign (ec, source, leave_copy, false);
645 class HoistedParameter : HoistedVariable
647 class HoistedFieldAssign : Assign
649 public HoistedFieldAssign (Expression target, Expression source)
650 : base (target, source, source.Location)
654 protected override Expression ResolveConversions (EmitContext ec)
657 // Implicit conversion check fails for hoisted type arguments
658 // as they are of different types (!!0 x !0)
664 readonly ParameterReference parameter;
666 public HoistedParameter (AnonymousMethodStorey scope, ParameterReference par)
667 : base (scope, par.Name, par.Type)
669 this.parameter = par;
672 public void EmitHoistingAssignment (EmitContext ec)
675 // Remove hoisted redirection to emit assignment from original parameter
677 HoistedVariable temp = parameter.Parameter.HoistedVariableReference;
678 parameter.Parameter.HoistedVariableReference = null;
680 Assign a = new HoistedFieldAssign (GetFieldExpression (ec), parameter);
681 if (a.Resolve (ec) != null)
682 a.EmitStatement (ec);
684 parameter.Parameter.HoistedVariableReference = temp;
687 public override void EmitSymbolInfo ()
689 SymbolWriter.DefineCapturedParameter (storey.ID, field.Name, field.Name);
693 get { return field; }
697 class HoistedLocalVariable : HoistedVariable
699 public HoistedLocalVariable (AnonymousMethodStorey scope, LocalInfo local, string name)
700 : base (scope, name, local.VariableType)
704 public override void EmitSymbolInfo ()
706 SymbolWriter.DefineCapturedLocal (storey.ID, field.Name, field.Name);
710 public class HoistedThis : HoistedVariable
712 readonly This this_reference;
714 public HoistedThis (AnonymousMethodStorey storey, This this_reference)
715 : base (storey, "<>f__this", this_reference.Type)
717 this.this_reference = this_reference;
720 public void EmitHoistingAssignment (EmitContext ec)
722 SimpleAssign a = new SimpleAssign (GetFieldExpression (ec), this_reference);
723 if (a.Resolve (ec) != null)
724 a.EmitStatement (ec);
727 public override void EmitSymbolInfo ()
729 SymbolWriter.DefineCapturedThis (storey.ID, field.Name);
732 public void RemoveHoisting ()
734 this_reference.RemoveHoisting ();
739 // Anonymous method expression as created by parser
741 public class AnonymousMethodExpression : Expression
743 protected readonly TypeContainer Host;
744 public readonly Parameters Parameters;
745 ListDictionary compatibles;
746 public ToplevelBlock Block;
748 public AnonymousMethodExpression (TypeContainer host, Parameters parameters, Location loc)
751 this.Parameters = parameters;
753 this.compatibles = new ListDictionary ();
756 public override string ExprClassName {
758 return "anonymous method";
762 public virtual bool HasExplicitParameters {
764 return Parameters != null;
769 // Returns true if the body of lambda expression can be implicitly
770 // converted to the delegate of type `delegate_type'
772 public bool ImplicitStandardConversionExists (EmitContext ec, Type delegate_type)
774 using (ec.Set (EmitContext.Flags.ProbingMode)) {
775 return Compatible (ec, delegate_type) != null;
779 protected Type CompatibleChecks (EmitContext ec, Type delegate_type)
781 if (TypeManager.IsDelegateType (delegate_type))
782 return delegate_type;
785 if (TypeManager.DropGenericTypeArguments (delegate_type) == TypeManager.expression_type) {
786 delegate_type = TypeManager.GetTypeArguments (delegate_type) [0];
787 if (TypeManager.IsDelegateType (delegate_type))
788 return delegate_type;
790 Report.Error (835, loc, "Cannot convert `{0}' to an expression tree of non-delegate type `{1}'",
791 GetSignatureForError (), TypeManager.CSharpName (delegate_type));
796 Report.Error (1660, loc, "Cannot convert `{0}' to non-delegate type `{1}'",
797 GetSignatureForError (), TypeManager.CSharpName (delegate_type));
801 protected bool VerifyExplicitParameters (Type delegate_type, ParameterData parameters, bool ignore_error)
803 if (VerifyParameterCompatibility (delegate_type, parameters, ignore_error))
807 Report.Error (1661, loc,
808 "Cannot convert `{0}' to delegate type `{1}' since there is a parameter mismatch",
809 GetSignatureForError (), TypeManager.CSharpName (delegate_type));
814 protected bool VerifyParameterCompatibility (Type delegate_type, ParameterData invoke_pd, bool ignore_errors)
816 if (Parameters.Count != invoke_pd.Count) {
820 Report.Error (1593, loc, "Delegate `{0}' does not take `{1}' arguments",
821 TypeManager.CSharpName (delegate_type), Parameters.Count.ToString ());
825 if (!HasExplicitParameters)
829 for (int i = 0; i < Parameters.Count; ++i) {
830 Parameter.Modifier p_mod = invoke_pd.ParameterModifier (i);
831 if (Parameters.ParameterModifier (i) != p_mod && p_mod != Parameter.Modifier.PARAMS) {
835 if (p_mod == Parameter.Modifier.NONE)
836 Report.Error (1677, loc, "Parameter `{0}' should not be declared with the `{1}' keyword",
837 (i + 1).ToString (), Parameter.GetModifierSignature (Parameters.ParameterModifier (i)));
839 Report.Error (1676, loc, "Parameter `{0}' must be declared with the `{1}' keyword",
840 (i+1).ToString (), Parameter.GetModifierSignature (p_mod));
845 Type type = invoke_pd.Types [i];
847 // We assume that generic parameters are always inflated
848 if (TypeManager.IsGenericParameter (type))
851 if (TypeManager.HasElementType (type) && TypeManager.IsGenericParameter (TypeManager.GetElementType (type)))
854 if (invoke_pd.ParameterType (i) != Parameters.ParameterType (i)) {
858 Report.Error (1678, loc, "Parameter `{0}' is declared as type `{1}' but should be `{2}'",
860 TypeManager.CSharpName (Parameters.ParameterType (i)),
861 TypeManager.CSharpName (invoke_pd.ParameterType (i)));
870 // Infers type arguments based on explicit arguments
872 public bool ExplicitTypeInference (TypeInferenceContext type_inference, Type delegate_type)
874 if (!HasExplicitParameters)
877 if (!TypeManager.IsDelegateType (delegate_type)) {
879 if (TypeManager.DropGenericTypeArguments (delegate_type) != TypeManager.expression_type)
882 delegate_type = delegate_type.GetGenericArguments () [0];
883 if (!TypeManager.IsDelegateType (delegate_type))
890 ParameterData d_params = TypeManager.GetDelegateParameters (delegate_type);
891 if (d_params.Count != Parameters.Count)
894 for (int i = 0; i < Parameters.Count; ++i) {
895 Type itype = d_params.Types [i];
896 if (!TypeManager.IsGenericParameter (itype)) {
897 if (!TypeManager.HasElementType (itype))
900 if (!TypeManager.IsGenericParameter (itype.GetElementType ()))
903 type_inference.ExactInference (Parameters.FixedParameters[i].ParameterType, itype);
908 public Type InferReturnType (EmitContext ec, TypeInferenceContext tic, Type delegate_type)
910 AnonymousMethodBody am;
911 using (ec.Set (EmitContext.Flags.ProbingMode | EmitContext.Flags.InferReturnType)) {
912 am = CompatibleMethod (ec, tic, GetType (), delegate_type);
918 if (am.ReturnType == TypeManager.null_type)
919 am.ReturnType = null;
921 return am.ReturnType;
925 // Returns AnonymousMethod container if this anonymous method
926 // expression can be implicitly converted to the delegate type `delegate_type'
928 public Expression Compatible (EmitContext ec, Type type)
930 Expression am = (Expression) compatibles [type];
934 Type delegate_type = CompatibleChecks (ec, type);
935 if (delegate_type == null)
939 // At this point its the first time we know the return type that is
940 // needed for the anonymous method. We create the method here.
943 MethodInfo invoke_mb = Delegate.GetInvokeMethod (
944 ec.ContainerType, delegate_type);
945 Type return_type = TypeManager.TypeToCoreType (invoke_mb.ReturnType);
948 Type[] g_args = delegate_type.GetGenericArguments ();
949 if (return_type.IsGenericParameter)
950 return_type = g_args [return_type.GenericParameterPosition];
954 // Second: the return type of the delegate must be compatible with
955 // the anonymous type. Instead of doing a pass to examine the block
956 // we satisfy the rule by setting the return type on the EmitContext
957 // to be the delegate type return type.
961 int errors = Report.Errors;
962 am = CompatibleMethod (ec, null, return_type, delegate_type);
963 if (am != null && delegate_type != type && errors == Report.Errors)
964 am = CreateExpressionTree (ec, delegate_type);
966 if (!ec.IsInProbingMode)
967 compatibles.Add (type, am);
970 } catch (Exception e) {
971 throw new InternalErrorException (e, loc);
975 protected virtual Expression CreateExpressionTree (EmitContext ec, Type delegate_type)
977 return CreateExpressionTree (ec);
980 public override Expression CreateExpressionTree (EmitContext ec)
982 Report.Error (1946, loc, "An anonymous method cannot be converted to an expression tree");
986 protected virtual Parameters ResolveParameters (EmitContext ec, TypeInferenceContext tic, Type delegate_type)
988 ParameterData delegate_parameters = TypeManager.GetDelegateParameters (delegate_type);
990 if (Parameters == null) {
992 // We provide a set of inaccessible parameters
994 Parameter[] fixedpars = new Parameter[delegate_parameters.Count];
996 for (int i = 0; i < delegate_parameters.Count; i++) {
997 Parameter.Modifier i_mod = delegate_parameters.ParameterModifier (i);
998 if ((i_mod & Parameter.Modifier.OUTMASK) != 0) {
999 Report.Error (1688, loc, "Cannot convert anonymous " +
1000 "method block without a parameter list " +
1001 "to delegate type `{0}' because it has " +
1002 "one or more `out' parameters.",
1003 TypeManager.CSharpName (delegate_type));
1006 fixedpars[i] = new Parameter (
1007 delegate_parameters.ParameterType (i), null,
1008 delegate_parameters.ParameterModifier (i), null, loc);
1011 return Parameters.CreateFullyResolved (fixedpars, delegate_parameters.Types);
1014 if (!VerifyExplicitParameters (delegate_type, delegate_parameters, ec.IsInProbingMode)) {
1021 public override Expression DoResolve (EmitContext ec)
1023 if (!ec.IsAnonymousMethodAllowed) {
1024 Report.Error (1706, loc, "Anonymous methods and lambda expressions cannot be used in the current context");
1029 // Set class type, set type
1032 eclass = ExprClass.Value;
1035 // This hack means `The type is not accessible
1036 // anywhere', we depend on special conversion
1039 type = TypeManager.anonymous_method_type;
1041 if ((Parameters != null) && !Parameters.Resolve (ec))
1044 // FIXME: The emitted code isn't very careful about reachability
1045 // so, ensure we have a 'ret' at the end
1046 if (ec.CurrentBranching != null &&
1047 ec.CurrentBranching.CurrentUsageVector.IsUnreachable)
1048 ec.NeedReturnLabel ();
1053 public override void Emit (EmitContext ec)
1055 // nothing, as we only exist to not do anything.
1058 public override string GetSignatureForError ()
1060 return ExprClassName;
1063 protected AnonymousMethodBody CompatibleMethod (EmitContext ec, TypeInferenceContext tic, Type return_type, Type delegate_type)
1065 Parameters p = ResolveParameters (ec, tic, delegate_type);
1069 ToplevelBlock b = ec.IsInProbingMode ? (ToplevelBlock) Block.PerformClone () : Block;
1071 AnonymousMethodBody anonymous = CompatibleMethodFactory (return_type, delegate_type, p, b);
1072 if (!anonymous.Compatible (ec))
1078 protected virtual AnonymousMethodBody CompatibleMethodFactory (Type return_type, Type delegate_type, Parameters p, ToplevelBlock b)
1080 return new AnonymousMethodBody (Host,
1082 delegate_type, loc);
1085 protected override void CloneTo (CloneContext clonectx, Expression t)
1087 AnonymousMethodExpression target = (AnonymousMethodExpression) t;
1089 target.Block = (ToplevelBlock) clonectx.LookupBlock (Block);
1094 // Abstract expression for any block which requires variables hoisting
1096 public abstract class AnonymousExpression : Expression
1098 protected class AnonymousMethodMethod : Method
1100 public readonly AnonymousExpression AnonymousMethod;
1101 public readonly AnonymousMethodStorey Storey;
1102 readonly string RealName;
1104 public AnonymousMethodMethod (DeclSpace parent, AnonymousExpression am, AnonymousMethodStorey storey,
1105 GenericMethod generic, TypeExpr return_type,
1106 int mod, string real_name, MemberName name,
1107 Parameters parameters)
1108 : base (parent, generic, return_type, mod | Modifiers.COMPILER_GENERATED,
1109 false, name, parameters, null)
1111 this.AnonymousMethod = am;
1112 this.Storey = storey;
1113 this.RealName = real_name;
1115 Parent.PartialContainer.AddMethod (this);
1119 public override EmitContext CreateEmitContext (DeclSpace tc, ILGenerator ig)
1121 EmitContext aec = AnonymousMethod.aec;
1123 aec.IsStatic = (ModFlags & Modifiers.STATIC) != 0;
1127 public override bool Define ()
1129 if (Storey != null && Storey.IsGeneric && Storey.HasHoistedVariables) {
1130 if (!Parameters.Empty) {
1131 Type [] ptypes = Parameters.Types;
1132 for (int i = 0; i < ptypes.Length; ++i)
1133 ptypes [i] = Storey.MutateType (ptypes [i]);
1136 member_type = Storey.MutateType (ReturnType);
1139 return base.Define ();
1142 public override void Emit ()
1145 // Before emitting any code we have to change all MVAR references to VAR
1146 // when the method is of generic type and has hoisted variables
1148 if (Storey == Parent && Storey.IsGeneric) {
1149 AnonymousMethod.aec.ReturnType = Storey.MutateType (ReturnType);
1150 block.MutateHoistedGenericType (Storey);
1153 if (MethodBuilder == null) {
1161 public override void EmitExtraSymbolInfo (SourceMethod source)
1163 source.SetRealMethodName (RealName);
1168 // The block that makes up the body for the anonymous method
1170 protected readonly ToplevelBlock Block;
1172 public Type ReturnType;
1173 public readonly TypeContainer Host;
1176 // The implicit method we create
1178 protected AnonymousMethodMethod method;
1179 protected EmitContext aec;
1181 protected AnonymousExpression (TypeContainer host, ToplevelBlock block, Type return_type, Location loc)
1183 this.ReturnType = return_type;
1190 public abstract void AddStoreyReference (AnonymousMethodStorey storey);
1191 public abstract string ContainerType { get; }
1192 public abstract bool IsIterator { get; }
1193 public abstract AnonymousMethodStorey Storey { get; }
1195 public bool Compatible (EmitContext ec)
1197 // TODO: Implement clone
1198 aec = new EmitContext (
1199 ec.ResolveContext, ec.TypeContainer, ec.DeclContainer,
1200 Location, null, ReturnType,
1201 (ec.InUnsafe ? Modifiers.UNSAFE : 0), /* No constructor */ false);
1203 aec.CurrentAnonymousMethod = this;
1204 aec.IsStatic = ec.IsStatic;
1206 IDisposable aec_dispose = null;
1207 EmitContext.Flags flags = 0;
1208 if (ec.InferReturnType)
1209 flags |= EmitContext.Flags.InferReturnType;
1211 if (ec.IsInProbingMode)
1212 flags |= EmitContext.Flags.ProbingMode;
1214 if (ec.IsInFieldInitializer)
1215 flags |= EmitContext.Flags.InFieldInitializer;
1217 if (ec.IsInUnsafeScope)
1218 flags |= EmitContext.Flags.InUnsafe;
1220 // HACK: Flag with 0 cannot be set
1222 aec_dispose = aec.Set (flags);
1225 bool res = aec.ResolveTopBlock (ec, Block, Block.Parameters, null, out unreachable);
1227 if (ec.InferReturnType)
1228 ReturnType = aec.ReturnType;
1230 if (aec_dispose != null) {
1231 aec_dispose.Dispose ();
1238 public class AnonymousMethodBody : AnonymousExpression
1240 ArrayList referenced_storeys;
1241 protected readonly Parameters parameters;
1242 static int unique_id;
1244 public AnonymousMethodBody (TypeContainer host, Parameters parameters,
1245 ToplevelBlock block, Type return_type, Type delegate_type,
1247 : base (host, block, return_type, loc)
1249 this.type = delegate_type;
1250 this.parameters = parameters;
1253 public override string ContainerType {
1254 get { return "anonymous method"; }
1257 public override AnonymousMethodStorey Storey {
1258 get { return method.Storey; }
1261 public override bool IsIterator {
1262 get { return false; }
1266 // Adds new storey reference to track out of scope variables
1268 public override void AddStoreyReference (AnonymousMethodStorey storey)
1270 if (referenced_storeys == null) {
1271 referenced_storeys = new ArrayList (2);
1273 foreach (AnonymousMethodStorey ams in referenced_storeys) {
1279 referenced_storeys.Add (storey);
1282 public override Expression CreateExpressionTree (EmitContext ec)
1284 Report.Error (1945, loc, "An expression tree cannot contain an anonymous method expression");
1288 bool Define (EmitContext ec)
1290 if (aec == null && !Compatible (ec))
1294 // Don't create anonymous expression method when we are in probing
1295 // mode or unreachable block
1297 if (ec.IsVariableCapturingRequired)
1298 method = DoCreateMethodHost (ec);
1304 // Creates a host for the anonymous method
1306 AnonymousMethodMethod DoCreateMethodHost (EmitContext ec)
1308 AnonymousMethodStorey storey = FindBestMethodStorey ();
1310 if (referenced_storeys != null) {
1311 foreach (AnonymousMethodStorey s in referenced_storeys) {
1315 storey.AddParentStoreyReference (s);
1316 s.HasHoistedVariables = true;
1317 Block.Parent.Explicit.PropagateStoreyReference (s);
1319 referenced_storeys = null;
1322 // Ensure we have a reference between this block and a storey
1323 // where this anymous method is created
1325 if (Block.Parent != null)
1326 Block.Parent.Explicit.PropagateStoreyReference (storey);
1330 // Anonymous method body can be converted to
1332 // 1, an instance method in current scope when only `this' is hoisted
1333 // 2, a static method in current scope when neither `this' nor any variable is hoisted
1334 // 3, an instance method in compiler generated storey when any hoisted variable exists
1338 if (storey != null) {
1339 modifiers = storey.HasHoistedVariables ? Modifiers.INTERNAL : Modifiers.PRIVATE;
1341 modifiers = Modifiers.STATIC | Modifiers.PRIVATE;
1344 DeclSpace parent = (modifiers & Modifiers.PRIVATE) != 0 ? Host : storey;
1346 MemberCore mc = ec.ResolveContext as MemberCore;
1347 string name = CompilerGeneratedClass.MakeName (parent != storey ? mc.Name : null,
1348 "m", null, unique_id++);
1350 MemberName member_name;
1351 GenericMethod generic_method;
1352 if ((modifiers & Modifiers.PRIVATE) != 0 && mc.MemberName.IsGeneric) {
1353 member_name = new MemberName (name, mc.MemberName.TypeArguments.Clone (), Location);
1355 generic_method = new GenericMethod (
1356 Host.NamespaceEntry, storey, member_name,
1357 new TypeExpression (ReturnType, Location), parameters);
1358 generic_method.SetParameterInfo (null);
1360 member_name = new MemberName (name, Location);
1361 generic_method = null;
1364 string real_name = String.Format (
1365 "{0}~{1}{2}", mc.GetSignatureForError (), GetSignatureForError (),
1366 parameters.GetSignatureForError ());
1368 return new AnonymousMethodMethod (parent,
1369 this, storey, generic_method, new TypeExpression (ReturnType, Location), modifiers,
1370 real_name, member_name, parameters);
1373 public override Expression DoResolve (EmitContext ec)
1375 if (eclass == ExprClass.Invalid) {
1380 eclass = ExprClass.Value;
1384 public override void Emit (EmitContext ec)
1387 // It has to be delayed not to polute expression trees
1389 if (method.MethodBuilder == null) {
1390 method.ResolveMembers ();
1395 // Don't cache generic delegates when contains MVAR argument
1397 Field am_cache = null;
1398 if ((method.ModFlags & Modifiers.STATIC) != 0 && !HasGenericParameter (type)) {
1399 TypeContainer parent = method.Parent.PartialContainer;
1400 int id = parent.Fields == null ? 0 : parent.Fields.Count;
1401 am_cache = new Field (Host, new TypeExpression (type, loc),
1402 Modifiers.STATIC | Modifiers.PRIVATE | Modifiers.COMPILER_GENERATED,
1403 CompilerGeneratedClass.MakeName (null, "f", "am$cache", id), null, loc);
1405 parent.AddField (am_cache);
1408 ILGenerator ig = ec.ig;
1409 Label l_initialized = ig.DefineLabel ();
1411 if (am_cache != null) {
1412 ig.Emit (OpCodes.Ldsfld, am_cache.FieldBuilder);
1413 ig.Emit (OpCodes.Brtrue_S, l_initialized);
1417 // Load method delegate implementation
1419 if ((method.ModFlags & Modifiers.STATIC) != 0) {
1420 ig.Emit (OpCodes.Ldnull);
1421 } else if (Storey.HasHoistedVariables) {
1422 Expression e = Storey.GetStoreyInstanceExpression (ec).Resolve (ec);
1426 ig.Emit (OpCodes.Ldarg_0);
1429 MethodInfo delegate_method = method.MethodBuilder;
1431 if (Storey != null && Storey.MemberName.IsGeneric && Storey.HasHoistedVariables)
1432 delegate_method = TypeBuilder.GetMethod (Storey.Instance.Type, delegate_method);
1434 ig.Emit (OpCodes.Ldftn, delegate_method);
1436 ConstructorInfo constructor_method = Delegate.GetConstructor (ec.ContainerType, type);
1438 if (type.IsGenericType && type is TypeBuilder)
1439 constructor_method = TypeBuilder.GetConstructor (type, constructor_method);
1441 ig.Emit (OpCodes.Newobj, constructor_method);
1443 if (am_cache != null) {
1444 ig.Emit (OpCodes.Stsfld, am_cache.FieldBuilder);
1445 ig.MarkLabel (l_initialized);
1446 ig.Emit (OpCodes.Ldsfld, am_cache.FieldBuilder);
1451 // Look for the best storey for this anonymous method
1453 AnonymousMethodStorey FindBestMethodStorey ()
1456 // Use the nearest parent block which has a storey
1458 for (Block b = Block.Parent; b != null; b = b.Parent) {
1459 AnonymousMethodStorey s = b.Explicit.AnonymousMethodStorey;
1467 public override string GetSignatureForError ()
1469 return TypeManager.CSharpName (type);
1472 static bool HasGenericParameter (Type type)
1475 if (type.IsGenericParameter)
1476 return type.DeclaringMethod != null;
1478 if (!type.IsGenericType)
1481 foreach (Type t in type.GetGenericArguments ()) {
1482 if (HasGenericParameter (t))
1489 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
1491 type = storey.MutateType (type);
1494 public static void Error_AddressOfCapturedVar (string name, Location loc)
1496 Report.Error (1686, loc,
1497 "Local variable `{0}' or its members cannot have their " +
1498 "address taken and be used inside an anonymous method block",
1502 public static void Reset ()
1509 // Anonymous type container
1511 public class AnonymousTypeClass : CompilerGeneratedClass
1513 static int types_counter;
1514 public const string ClassNamePrefix = "<>__AnonType";
1515 public const string SignatureForError = "anonymous type";
1517 readonly ArrayList parameters;
1519 private AnonymousTypeClass (DeclSpace parent, MemberName name, ArrayList parameters, Location loc)
1520 : base (parent, name, Modifiers.SEALED)
1522 this.parameters = parameters;
1525 public static AnonymousTypeClass Create (TypeContainer parent, ArrayList parameters, Location loc)
1527 if (RootContext.Version <= LanguageVersion.ISO_2)
1528 Report.FeatureIsNotAvailable (loc, "anonymous types");
1530 string name = ClassNamePrefix + types_counter++;
1532 SimpleName [] t_args = new SimpleName [parameters.Count];
1533 Parameter [] ctor_params = new Parameter [parameters.Count];
1534 for (int i = 0; i < parameters.Count; ++i) {
1535 AnonymousTypeParameter p = (AnonymousTypeParameter) parameters [i];
1537 t_args [i] = new SimpleName ("<" + p.Name + ">__T", p.Location);
1538 ctor_params [i] = new Parameter (t_args [i], p.Name, 0, null, p.Location);
1542 // Create generic anonymous type host with generic arguments
1543 // named upon properties names
1545 AnonymousTypeClass a_type = new AnonymousTypeClass (parent.NamespaceEntry.SlaveDeclSpace,
1546 new MemberName (name, new TypeArguments (loc, t_args), loc), parameters, loc);
1548 if (parameters.Count > 0)
1549 a_type.SetParameterInfo (null);
1551 Constructor c = new Constructor (a_type, name, Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN,
1552 new Parameters (ctor_params), null, loc);
1553 c.Block = new ToplevelBlock (c.Parameters, loc);
1556 // Create fields and contructor body with field initialization
1559 for (int i = 0; i < parameters.Count; ++i) {
1560 AnonymousTypeParameter p = (AnonymousTypeParameter) parameters [i];
1562 Field f = new Field (a_type, t_args [i], Modifiers.PRIVATE | Modifiers.READONLY,
1563 "<" + p.Name + ">", null, p.Location);
1565 if (!a_type.AddField (f)) {
1567 Report.Error (833, p.Location, "`{0}': An anonymous type cannot have multiple properties with the same name",
1572 c.Block.AddStatement (new StatementExpression (
1573 new SimpleAssign (new MemberAccess (new This (p.Location), f.Name),
1574 c.Block.GetParameterReference (p.Name, p.Location))));
1576 ToplevelBlock get_block = new ToplevelBlock (p.Location);
1577 get_block.AddStatement (new Return (
1578 new MemberAccess (new This (p.Location), f.Name), p.Location));
1579 Accessor get_accessor = new Accessor (get_block, 0, null, p.Location);
1580 Property prop = new Property (a_type, t_args [i], Modifiers.PUBLIC, false,
1581 new MemberName (p.Name, p.Location), null, get_accessor, null, false);
1582 a_type.AddProperty (prop);
1588 a_type.AddConstructor (c);
1592 public static void Reset ()
1597 protected override bool AddToContainer (MemberCore symbol, string name)
1599 MemberCore mc = (MemberCore) defined_names [name];
1602 defined_names.Add (name, symbol);
1606 Report.SymbolRelatedToPreviousError (mc);
1610 void DefineOverrides ()
1612 Location loc = Location;
1614 Method equals = new Method (this, null, TypeManager.system_boolean_expr,
1615 Modifiers.PUBLIC | Modifiers.OVERRIDE | Modifiers.DEBUGGER_HIDDEN, false, new MemberName ("Equals", loc),
1616 new Parameters (new Parameter (TypeManager.system_object_expr, "obj", 0, null, loc)), null);
1618 Method tostring = new Method (this, null, TypeManager.system_string_expr,
1619 Modifiers.PUBLIC | Modifiers.OVERRIDE | Modifiers.DEBUGGER_HIDDEN, false, new MemberName ("ToString", loc),
1620 Mono.CSharp.Parameters.EmptyReadOnlyParameters, null);
1622 ToplevelBlock equals_block = new ToplevelBlock (equals.Parameters, loc);
1623 TypeExpr current_type;
1625 current_type = new ConstructedType (TypeBuilder, TypeParameters, loc);
1627 current_type = new TypeExpression (TypeBuilder, loc);
1629 equals_block.AddVariable (current_type, "other", loc);
1630 LocalVariableReference other_variable = new LocalVariableReference (equals_block, "other", loc);
1632 MemberAccess system_collections_generic = new MemberAccess (new MemberAccess (
1633 new QualifiedAliasMember ("global", "System", loc), "Collections", loc), "Generic", loc);
1635 Expression rs_equals = null;
1636 Expression string_concat = new StringConstant ("<empty type>", loc);
1637 Expression rs_hashcode = new IntConstant (-2128831035, loc);
1638 for (int i = 0; i < parameters.Count; ++i) {
1639 AnonymousTypeParameter p = (AnonymousTypeParameter) parameters [i];
1640 Field f = (Field) Fields [i];
1642 MemberAccess equality_comparer = new MemberAccess (new MemberAccess (
1643 system_collections_generic, "EqualityComparer",
1644 new TypeArguments (loc, new SimpleName (TypeParameters [i].Name, loc)), loc),
1647 ArrayList arguments_equal = new ArrayList (2);
1648 arguments_equal.Add (new Argument (new MemberAccess (new This (f.Location), f.Name)));
1649 arguments_equal.Add (new Argument (new MemberAccess (other_variable, f.Name)));
1651 Expression field_equal = new Invocation (new MemberAccess (equality_comparer,
1652 "Equals", loc), arguments_equal);
1654 ArrayList arguments_hashcode = new ArrayList (1);
1655 arguments_hashcode.Add (new Argument (new MemberAccess (new This (f.Location), f.Name)));
1656 Expression field_hashcode = new Invocation (new MemberAccess (equality_comparer,
1657 "GetHashCode", loc), arguments_hashcode);
1659 IntConstant FNV_prime = new IntConstant (16777619, loc);
1660 rs_hashcode = new Binary (Binary.Operator.Multiply,
1661 new Binary (Binary.Operator.ExclusiveOr, rs_hashcode, field_hashcode),
1664 Expression field_to_string = new Conditional (new Binary (Binary.Operator.Inequality,
1665 new MemberAccess (new This (f.Location), f.Name), new NullLiteral (loc)),
1666 new Invocation (new MemberAccess (
1667 new MemberAccess (new This (f.Location), f.Name), "ToString"), null),
1668 new StringConstant ("<null>", loc));
1670 if (rs_equals == null) {
1671 rs_equals = field_equal;
1672 string_concat = new Binary (Binary.Operator.Addition,
1673 new StringConstant (p.Name + " = ", loc),
1679 // Implementation of ToString () body using string concatenation
1681 string_concat = new Binary (Binary.Operator.Addition,
1682 new Binary (Binary.Operator.Addition,
1684 new StringConstant (", " + p.Name + " = ", loc)),
1687 rs_equals = new Binary (Binary.Operator.LogicalAnd, rs_equals, field_equal);
1691 // Equals (object obj) override
1693 equals_block.AddStatement (new StatementExpression (
1694 new SimpleAssign (other_variable,
1695 new As (equals_block.GetParameterReference ("obj", loc),
1696 current_type, loc), loc)));
1698 Expression equals_test = new Binary (Binary.Operator.Inequality, other_variable, new NullLiteral (loc));
1699 if (rs_equals != null)
1700 equals_test = new Binary (Binary.Operator.LogicalAnd, equals_test, rs_equals);
1701 equals_block.AddStatement (new Return (equals_test, loc));
1703 equals.Block = equals_block;
1704 equals.ResolveMembers ();
1708 // GetHashCode () override
1710 Method hashcode = new Method (this, null, TypeManager.system_int32_expr,
1711 Modifiers.PUBLIC | Modifiers.OVERRIDE | Modifiers.DEBUGGER_HIDDEN,
1712 false, new MemberName ("GetHashCode", loc),
1713 Mono.CSharp.Parameters.EmptyReadOnlyParameters, null);
1716 // Modified FNV with good avalanche behavior and uniform
1717 // distribution with larger hash sizes.
1719 // const int FNV_prime = 16777619;
1720 // int hash = (int) 2166136261;
1721 // foreach (int d in data)
1722 // hash = (hash ^ d) * FNV_prime;
1723 // hash += hash << 13;
1724 // hash ^= hash >> 7;
1725 // hash += hash << 3;
1726 // hash ^= hash >> 17;
1727 // hash += hash << 5;
1729 ToplevelBlock hashcode_block = new ToplevelBlock (loc);
1730 hashcode_block.AddVariable (TypeManager.system_int32_expr, "hash", loc);
1731 LocalVariableReference hash_variable = new LocalVariableReference (hashcode_block, "hash", loc);
1732 hashcode_block.AddStatement (new StatementExpression (
1733 new SimpleAssign (hash_variable, rs_hashcode)));
1735 hashcode_block.AddStatement (new StatementExpression (
1736 new CompoundAssign (Binary.Operator.Addition, hash_variable,
1737 new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (13, loc)))));
1738 hashcode_block.AddStatement (new StatementExpression (
1739 new CompoundAssign (Binary.Operator.ExclusiveOr, hash_variable,
1740 new Binary (Binary.Operator.RightShift, hash_variable, new IntConstant (7, loc)))));
1741 hashcode_block.AddStatement (new StatementExpression (
1742 new CompoundAssign (Binary.Operator.Addition, hash_variable,
1743 new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (3, loc)))));
1744 hashcode_block.AddStatement (new StatementExpression (
1745 new CompoundAssign (Binary.Operator.ExclusiveOr, hash_variable,
1746 new Binary (Binary.Operator.RightShift, hash_variable, new IntConstant (17, loc)))));
1747 hashcode_block.AddStatement (new StatementExpression (
1748 new CompoundAssign (Binary.Operator.Addition, hash_variable,
1749 new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (5, loc)))));
1751 hashcode_block.AddStatement (new Return (hash_variable, loc));
1752 hashcode.Block = hashcode_block;
1753 hashcode.ResolveMembers ();
1754 AddMethod (hashcode);
1757 // ToString () override
1760 ToplevelBlock tostring_block = new ToplevelBlock (loc);
1761 tostring_block.AddStatement (new Return (string_concat, loc));
1762 tostring.Block = tostring_block;
1763 tostring.ResolveMembers ();
1764 AddMethod (tostring);
1767 public override bool DefineMembers ()
1771 return base.DefineMembers ();
1774 public override string GetSignatureForError ()
1776 return SignatureForError;
1779 public ArrayList Parameters {